gst_plugins_base/tests/examples/snapshot/snapshot.c
changeset 2 5505e8908944
parent 0 0e761a78d257
equal deleted inserted replaced
1:4c282e7dd6d3 2:5505e8908944
       
     1 /* GStreamer snapshot example
       
     2  * Copyright (C) <2007> Wim Taymans <wim.taymans@gmail.com>
       
     3  *
       
     4  * This library is free software; you can redistribute it and/or
       
     5  * modify it under the terms of the GNU Library General Public
       
     6  * License as published by the Free Software Foundation; either
       
     7  * version 2 of the License, or (at your option) any later version.
       
     8  *
       
     9  * This library is distributed in the hope that it will be useful,
       
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
       
    12  * Library General Public License for more details.
       
    13  *
       
    14  * You should have received a copy of the GNU Library General Public
       
    15  * License along with this library; if not, write to the
       
    16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
       
    17  * Boston, MA 02111-1307, USA.
       
    18  */
       
    19 
       
    20 #include <gst/gst.h>
       
    21 #include <gtk/gtk.h>
       
    22 
       
    23 #include <stdlib.h>
       
    24 
       
    25 #define CAPS "video/x-raw-rgb,width=160,pixel-aspect-ratio=1/1,bpp=(int)24,depth=(int)24,endianness=(int)4321,red_mask=(int)0xff0000, green_mask=(int)0x00ff00, blue_mask=(int)0x0000ff"
       
    26 
       
    27 int
       
    28 main (int argc, char *argv[])
       
    29 {
       
    30   GstElement *pipeline, *sink;
       
    31   gint width, height;
       
    32   GstBuffer *buffer;
       
    33   gchar *descr;
       
    34   GError *error = NULL;
       
    35   GdkPixbuf *pixbuf;
       
    36   gint64 duration, position;
       
    37   GstFormat format;
       
    38   GstStateChangeReturn ret;
       
    39   gboolean res;
       
    40 
       
    41   gst_init (&argc, &argv);
       
    42 
       
    43   if (argc != 2) {
       
    44     g_print ("usage: %s <uri>\n Writes snapshot.png in the current directory",
       
    45         argv[0]);
       
    46     exit (-1);
       
    47   }
       
    48 
       
    49   /* create a new pipeline */
       
    50   descr =
       
    51       g_strdup_printf ("uridecodebin uri=%s ! ffmpegcolorspace ! videoscale ! "
       
    52       " appsink name=sink caps=\"" CAPS "\"", argv[1]);
       
    53   pipeline = gst_parse_launch (descr, &error);
       
    54 
       
    55   if (error != NULL) {
       
    56     g_print ("could not construct pipeline: %s", error->message);
       
    57     g_error_free (error);
       
    58     exit (-1);
       
    59   }
       
    60 
       
    61   /* get sink */
       
    62   sink = gst_bin_get_by_name (GST_BIN (pipeline), "sink");
       
    63 
       
    64   /* set to PAUSED to make the first frame arrive in the sink */
       
    65   ret = gst_element_set_state (pipeline, GST_STATE_PAUSED);
       
    66   switch (ret) {
       
    67     case GST_STATE_CHANGE_FAILURE:
       
    68       g_print ("failed to play the file\n");
       
    69       exit (-1);
       
    70     case GST_STATE_CHANGE_NO_PREROLL:
       
    71       /* for live sources, we need to set the pipeline to PLAYING before we can
       
    72        * receive a buffer. We don't do that yet */
       
    73       g_print ("live sources not supported yet\n");
       
    74       exit (-1);
       
    75     default:
       
    76       break;
       
    77   }
       
    78   /* This can block for up to 5 seconds. If your machine is really overloaded,
       
    79    * it might time out before the pipeline prerolled and we generate an error. A
       
    80    * better way is to run a mainloop and catch errors there. */
       
    81   ret = gst_element_get_state (pipeline, NULL, NULL, 5 * GST_SECOND);
       
    82   if (ret == GST_STATE_CHANGE_FAILURE) {
       
    83     g_print ("failed to play the file\n");
       
    84     exit (-1);
       
    85   }
       
    86 
       
    87   /* get the duration */
       
    88   format = GST_FORMAT_TIME;
       
    89   gst_element_query_duration (pipeline, &format, &duration);
       
    90 
       
    91   if (duration != -1)
       
    92     /* we have a duration, seek to 5% */
       
    93     position = duration * 5 / 100;
       
    94   else
       
    95     /* no duration, seek to 1 second, this could EOS */
       
    96     position = 1 * GST_SECOND;
       
    97 
       
    98   /* seek to the a position in the file. Most files have a black first frame so
       
    99    * by seeking to somewhere else we have a bigger chance of getting something
       
   100    * more interesting. An optimisation would be to detect black images and then
       
   101    * seek a little more */
       
   102   gst_element_seek_simple (pipeline, GST_FORMAT_TIME, GST_SEEK_FLAG_FLUSH,
       
   103       position);
       
   104 
       
   105   /* get the preroll buffer from appsink, this block untils appsink really
       
   106    * prerolls */
       
   107   g_signal_emit_by_name (sink, "pull-preroll", &buffer, NULL);
       
   108 
       
   109   /* if we have a buffer now, convert it to a pixbuf. It's possible that we
       
   110    * don't have a buffer because we went EOS right away or had an error. */
       
   111   if (buffer) {
       
   112     GstCaps *caps;
       
   113     GstStructure *s;
       
   114 
       
   115     /* get the snapshot buffer format now. We set the caps on the appsink so
       
   116      * that it can only be an rgb buffer. The only thing we have not specified
       
   117      * on the caps is the height, which is dependant on the pixel-aspect-ratio
       
   118      * of the source material */
       
   119     caps = GST_BUFFER_CAPS (buffer);
       
   120     if (!caps) {
       
   121       g_print ("could not get snapshot format\n");
       
   122       exit (-1);
       
   123     }
       
   124     s = gst_caps_get_structure (caps, 0);
       
   125 
       
   126     /* we need to get the final caps on the buffer to get the size */
       
   127     res = gst_structure_get_int (s, "width", &width);
       
   128     res |= gst_structure_get_int (s, "height", &height);
       
   129     if (!res) {
       
   130       g_print ("could not get snapshot dimension\n");
       
   131       exit (-1);
       
   132     }
       
   133 
       
   134     /* create pixmap from buffer and save, gstreamer video buffers have a stride
       
   135      * that is rounded up to the nearest multiple of 4 */
       
   136     pixbuf = gdk_pixbuf_new_from_data (GST_BUFFER_DATA (buffer),
       
   137         GDK_COLORSPACE_RGB, FALSE, 8, width, height,
       
   138         GST_ROUND_UP_4 (width * 3), NULL, NULL);
       
   139 
       
   140     /* save the pixbuf */
       
   141     gdk_pixbuf_save (pixbuf, "snapshot.png", "png", &error, NULL);
       
   142   } else {
       
   143     g_print ("could not make snapshot\n");
       
   144   }
       
   145 
       
   146   /* cleanup and exit */
       
   147   gst_element_set_state (pipeline, GST_STATE_NULL);
       
   148   gst_object_unref (pipeline);
       
   149 
       
   150   exit (0);
       
   151 }