• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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., 51 Franklin St, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19 
20 #include <gst/gst.h>
21 #include <gdk-pixbuf/gdk-pixbuf.h>
22 
23 #include <stdlib.h>
24 
25 #define CAPS "video/x-raw,format=RGB,width=160,pixel-aspect-ratio=1/1"
26 
27 int
main(int argc,char * argv[])28 main (int argc, char *argv[])
29 {
30   GstElement *pipeline, *sink;
31   gint width, height;
32   GstSample *sample;
33   gchar *descr;
34   GError *error = NULL;
35   GdkPixbuf *pixbuf;
36   gint64 duration, position;
37   GstStateChangeReturn ret;
38   gboolean res;
39   GstMapInfo map;
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\n",
45         argv[0]);
46     exit (-1);
47   }
48 
49   /* create a new pipeline */
50   descr =
51       g_strdup_printf ("uridecodebin uri=%s ! videoconvert ! 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\n", 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   gst_element_query_duration (pipeline, GST_FORMAT_TIME, &duration);
89 
90   if (duration != -1)
91     /* we have a duration, seek to 5% */
92     position = duration * 5 / 100;
93   else
94     /* no duration, seek to 1 second, this could EOS */
95     position = 1 * GST_SECOND;
96 
97   /* seek to the a position in the file. Most files have a black first frame so
98    * by seeking to somewhere else we have a bigger chance of getting something
99    * more interesting. An optimisation would be to detect black images and then
100    * seek a little more */
101   gst_element_seek_simple (pipeline, GST_FORMAT_TIME,
102       GST_SEEK_FLAG_KEY_UNIT | GST_SEEK_FLAG_FLUSH, position);
103 
104   /* get the preroll buffer from appsink, this block untils appsink really
105    * prerolls */
106   g_signal_emit_by_name (sink, "pull-preroll", &sample, NULL);
107   gst_object_unref (sink);
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 (sample) {
112     GstBuffer *buffer;
113     GstCaps *caps;
114     GstStructure *s;
115 
116     /* get the snapshot buffer format now. We set the caps on the appsink so
117      * that it can only be an rgb buffer. The only thing we have not specified
118      * on the caps is the height, which is dependent on the pixel-aspect-ratio
119      * of the source material */
120     caps = gst_sample_get_caps (sample);
121     if (!caps) {
122       g_print ("could not get snapshot format\n");
123       exit (-1);
124     }
125     s = gst_caps_get_structure (caps, 0);
126 
127     /* we need to get the final caps on the buffer to get the size */
128     res = gst_structure_get_int (s, "width", &width);
129     res |= gst_structure_get_int (s, "height", &height);
130     if (!res) {
131       g_print ("could not get snapshot dimension\n");
132       exit (-1);
133     }
134 
135     /* create pixmap from buffer and save, gstreamer video buffers have a stride
136      * that is rounded up to the nearest multiple of 4 */
137     buffer = gst_sample_get_buffer (sample);
138     gst_buffer_map (buffer, &map, GST_MAP_READ);
139     pixbuf = gdk_pixbuf_new_from_data (map.data,
140         GDK_COLORSPACE_RGB, FALSE, 8, width, height,
141         GST_ROUND_UP_4 (width * 3), NULL, NULL);
142 
143     /* save the pixbuf */
144     gdk_pixbuf_save (pixbuf, "snapshot.png", "png", &error, NULL);
145     gst_buffer_unmap (buffer, &map);
146   } else {
147     g_print ("could not make snapshot\n");
148   }
149 
150   /* cleanup and exit */
151   gst_element_set_state (pipeline, GST_STATE_NULL);
152   gst_object_unref (pipeline);
153 
154   exit (0);
155 }
156