Blame tests/examples/snapshot/snapshot.c

Packit 971217
/* GStreamer snapshot example
Packit 971217
 * Copyright (C) <2007> Wim Taymans <wim.taymans@gmail.com>
Packit 971217
 *
Packit 971217
 * This library is free software; you can redistribute it and/or
Packit 971217
 * modify it under the terms of the GNU Library General Public
Packit 971217
 * License as published by the Free Software Foundation; either
Packit 971217
 * version 2 of the License, or (at your option) any later version.
Packit 971217
 *
Packit 971217
 * This library is distributed in the hope that it will be useful,
Packit 971217
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 971217
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 971217
 * Library General Public License for more details.
Packit 971217
 *
Packit 971217
 * You should have received a copy of the GNU Library General Public
Packit 971217
 * License along with this library; if not, write to the
Packit 971217
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
Packit 971217
 * Boston, MA 02110-1301, USA.
Packit 971217
 */
Packit 971217
Packit 971217
#include <gst/gst.h>
Packit 971217
#include <gdk-pixbuf/gdk-pixbuf.h>
Packit 971217
Packit 971217
#include <stdlib.h>
Packit 971217
Packit 971217
#define CAPS "video/x-raw,format=RGB,width=160,pixel-aspect-ratio=1/1"
Packit 971217
Packit 971217
int
Packit 971217
main (int argc, char *argv[])
Packit 971217
{
Packit 971217
  GstElement *pipeline, *sink;
Packit 971217
  gint width, height;
Packit 971217
  GstSample *sample;
Packit 971217
  gchar *descr;
Packit 971217
  GError *error = NULL;
Packit 971217
  GdkPixbuf *pixbuf;
Packit 971217
  gint64 duration, position;
Packit 971217
  GstStateChangeReturn ret;
Packit 971217
  gboolean res;
Packit 971217
  GstMapInfo map;
Packit 971217
Packit 971217
  gst_init (&argc, &argv);
Packit 971217
Packit 971217
  if (argc != 2) {
Packit 971217
    g_print ("usage: %s <uri>\n Writes snapshot.png in the current directory\n",
Packit 971217
        argv[0]);
Packit 971217
    exit (-1);
Packit 971217
  }
Packit 971217
Packit 971217
  /* create a new pipeline */
Packit 971217
  descr =
Packit 971217
      g_strdup_printf ("uridecodebin uri=%s ! videoconvert ! videoscale ! "
Packit 971217
      " appsink name=sink caps=\"" CAPS "\"", argv[1]);
Packit 971217
  pipeline = gst_parse_launch (descr, &error);
Packit 971217
Packit 971217
  if (error != NULL) {
Packit 971217
    g_print ("could not construct pipeline: %s\n", error->message);
Packit 971217
    g_error_free (error);
Packit 971217
    exit (-1);
Packit 971217
  }
Packit 971217
Packit 971217
  /* get sink */
Packit 971217
  sink = gst_bin_get_by_name (GST_BIN (pipeline), "sink");
Packit 971217
Packit 971217
  /* set to PAUSED to make the first frame arrive in the sink */
Packit 971217
  ret = gst_element_set_state (pipeline, GST_STATE_PAUSED);
Packit 971217
  switch (ret) {
Packit 971217
    case GST_STATE_CHANGE_FAILURE:
Packit 971217
      g_print ("failed to play the file\n");
Packit 971217
      exit (-1);
Packit 971217
    case GST_STATE_CHANGE_NO_PREROLL:
Packit 971217
      /* for live sources, we need to set the pipeline to PLAYING before we can
Packit 971217
       * receive a buffer. We don't do that yet */
Packit 971217
      g_print ("live sources not supported yet\n");
Packit 971217
      exit (-1);
Packit 971217
    default:
Packit 971217
      break;
Packit 971217
  }
Packit 971217
  /* This can block for up to 5 seconds. If your machine is really overloaded,
Packit 971217
   * it might time out before the pipeline prerolled and we generate an error. A
Packit 971217
   * better way is to run a mainloop and catch errors there. */
Packit 971217
  ret = gst_element_get_state (pipeline, NULL, NULL, 5 * GST_SECOND);
Packit 971217
  if (ret == GST_STATE_CHANGE_FAILURE) {
Packit 971217
    g_print ("failed to play the file\n");
Packit 971217
    exit (-1);
Packit 971217
  }
Packit 971217
Packit 971217
  /* get the duration */
Packit 971217
  gst_element_query_duration (pipeline, GST_FORMAT_TIME, &duration);
Packit 971217
Packit 971217
  if (duration != -1)
Packit 971217
    /* we have a duration, seek to 5% */
Packit 971217
    position = duration * 5 / 100;
Packit 971217
  else
Packit 971217
    /* no duration, seek to 1 second, this could EOS */
Packit 971217
    position = 1 * GST_SECOND;
Packit 971217
Packit 971217
  /* seek to the a position in the file. Most files have a black first frame so
Packit 971217
   * by seeking to somewhere else we have a bigger chance of getting something
Packit 971217
   * more interesting. An optimisation would be to detect black images and then
Packit 971217
   * seek a little more */
Packit 971217
  gst_element_seek_simple (pipeline, GST_FORMAT_TIME,
Packit 971217
      GST_SEEK_FLAG_KEY_UNIT | GST_SEEK_FLAG_FLUSH, position);
Packit 971217
Packit 971217
  /* get the preroll buffer from appsink, this block untils appsink really
Packit 971217
   * prerolls */
Packit 971217
  g_signal_emit_by_name (sink, "pull-preroll", &sample, NULL);
Packit 971217
  gst_object_unref (sink);
Packit 971217
Packit 971217
  /* if we have a buffer now, convert it to a pixbuf. It's possible that we
Packit 971217
   * don't have a buffer because we went EOS right away or had an error. */
Packit 971217
  if (sample) {
Packit 971217
    GstBuffer *buffer;
Packit 971217
    GstCaps *caps;
Packit 971217
    GstStructure *s;
Packit 971217
Packit 971217
    /* get the snapshot buffer format now. We set the caps on the appsink so
Packit 971217
     * that it can only be an rgb buffer. The only thing we have not specified
Packit 971217
     * on the caps is the height, which is dependant on the pixel-aspect-ratio
Packit 971217
     * of the source material */
Packit 971217
    caps = gst_sample_get_caps (sample);
Packit 971217
    if (!caps) {
Packit 971217
      g_print ("could not get snapshot format\n");
Packit 971217
      exit (-1);
Packit 971217
    }
Packit 971217
    s = gst_caps_get_structure (caps, 0);
Packit 971217
Packit 971217
    /* we need to get the final caps on the buffer to get the size */
Packit 971217
    res = gst_structure_get_int (s, "width", &width);
Packit 971217
    res |= gst_structure_get_int (s, "height", &height);
Packit 971217
    if (!res) {
Packit 971217
      g_print ("could not get snapshot dimension\n");
Packit 971217
      exit (-1);
Packit 971217
    }
Packit 971217
Packit 971217
    /* create pixmap from buffer and save, gstreamer video buffers have a stride
Packit 971217
     * that is rounded up to the nearest multiple of 4 */
Packit 971217
    buffer = gst_sample_get_buffer (sample);
Packit 971217
    gst_buffer_map (buffer, &map, GST_MAP_READ);
Packit 971217
    pixbuf = gdk_pixbuf_new_from_data (map.data,
Packit 971217
        GDK_COLORSPACE_RGB, FALSE, 8, width, height,
Packit 971217
        GST_ROUND_UP_4 (width * 3), NULL, NULL);
Packit 971217
Packit 971217
    /* save the pixbuf */
Packit 971217
    gdk_pixbuf_save (pixbuf, "snapshot.png", "png", &error, NULL);
Packit 971217
    gst_buffer_unmap (buffer, &map);
Packit 971217
  } else {
Packit 971217
    g_print ("could not make snapshot\n");
Packit 971217
  }
Packit 971217
Packit 971217
  /* cleanup and exit */
Packit 971217
  gst_element_set_state (pipeline, GST_STATE_NULL);
Packit 971217
  gst_object_unref (pipeline);
Packit 971217
Packit 971217
  exit (0);
Packit 971217
}