1 /* GStreamer GdkPixbuf sink
2 * Copyright (C) 2006-2008 Tim-Philipp Müller <tim centricular net>
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.
9 * This library is free software; you can redistribute it and/or
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.
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.
21 * SECTION:element-gdkpixbufsink
23 * This sink element takes RGB or RGBA images as input and wraps them into
24 * #GdkPixbuf objects, for easy saving to file via the
25 * GdkPixbuf library API or displaying in Gtk+ applications (e.g. using
26 * the #GtkImage widget).
28 * There are two ways to use this element and obtain the #GdkPixbuf objects
32 * Watching for element messages named <classname>"preroll-pixbuf"
33 * </classname> or <classname>"pixbuf"</classname> on the bus, which
34 * will be posted whenever an image would usually be rendered. See below for
35 * more details on these messages and how to extract the pixbuf object
39 * Retrieving the current pixbuf via the #GstGdkPixbufSink:last-pixbuf property
40 * when needed. This is the easiest way to get at pixbufs for snapshotting
41 * purposes - just wait until the pipeline is prerolled (ASYNC_DONE message
42 * on the bus), then read the property. If you use this method, you may want
43 * to disable message posting by setting the #GstGdkPixbufSink:post-messages
44 * property to %FALSE. This avoids unnecessary memory overhead.
48 * The primary purpose of this element is to abstract away the #GstBuffer to
49 * #GdkPixbuf conversion. Other than that it's very similar to the fakesink
52 * This element is meant for easy no-hassle video snapshotting. It is not
53 * suitable for video playback or video display at high framerates. Use
54 * ximagesink, xvimagesink or some other suitable video sink in connection
55 * with the #GstXOverlay interface instead if you want to do video playback.
58 * <title>Message details</title>
59 * As mentioned above, this element will by default post element messages
60 * containing structures named <classname>"preroll-pixbuf"
61 * </classname> or <classname>"pixbuf"</classname> on the bus (this
62 * can be disabled by setting the #GstGdkPixbufSink:post-messages property
63 * to %FALSE though). The element message structure has the following fields:
66 * <classname>"pixbuf"</classname>: the #GdkPixbuf object
69 * <classname>"pixel-aspect-ratio"</classname>: the pixel aspect
70 * ratio (PAR) of the input image (this field contains a #GstFraction); the
71 * PAR is usually 1:1 for images, but is often something non-1:1 in the case
72 * of video input. In this case the image may be distorted and you may need
73 * to rescale it accordingly before saving it to file or displaying it. This
74 * can easily be done using gdk_pixbuf_scale() (the reason this is not done
75 * automatically is that the application will often scale the image anyway
76 * according to the size of the output window, in which case it is much more
77 * efficient to only scale once rather than twice). You can put a videoscale
78 * element and a capsfilter element with
79 * <literal>video/x-raw-rgb,pixel-aspect-ratio=(fraction)1/1</literal> caps
80 * in front of this element to make sure the pixbufs always have a 1:1 PAR.
86 * <title>Example pipeline</title>
88 * gst-launch-1.0 -m -v videotestsrc num-buffers=1 ! gdkpixbufsink
89 * ]| Process one single test image as pixbuf (note that the output you see will
90 * be slightly misleading. The message structure does contain a valid pixbuf
91 * object even if the structure string says '(NULL)').
99 #include "gstgdkpixbufsink.h"
101 #include <gst/video/video.h>
103 #define DEFAULT_SEND_MESSAGES TRUE
104 #define DEFAULT_POST_MESSAGES TRUE
115 G_DEFINE_TYPE (GstGdkPixbufSink, gst_gdk_pixbuf_sink, GST_TYPE_VIDEO_SINK);
117 static void gst_gdk_pixbuf_sink_set_property (GObject * object, guint prop_id,
118 const GValue * value, GParamSpec * pspec);
119 static void gst_gdk_pixbuf_sink_get_property (GObject * object, guint prop_id,
120 GValue * value, GParamSpec * pspec);
122 static gboolean gst_gdk_pixbuf_sink_start (GstBaseSink * basesink);
123 static gboolean gst_gdk_pixbuf_sink_stop (GstBaseSink * basesink);
124 static gboolean gst_gdk_pixbuf_sink_set_caps (GstBaseSink * basesink,
126 static GstFlowReturn gst_gdk_pixbuf_sink_render (GstBaseSink * bsink,
128 static GstFlowReturn gst_gdk_pixbuf_sink_preroll (GstBaseSink * bsink,
130 static GdkPixbuf *gst_gdk_pixbuf_sink_get_pixbuf_from_buffer (GstGdkPixbufSink *
131 sink, GstBuffer * buf);
133 static GstStaticPadTemplate pixbufsink_sink_factory =
134 GST_STATIC_PAD_TEMPLATE ("sink",
137 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE ("RGB") ";"
138 GST_VIDEO_CAPS_MAKE ("RGBA"))
142 gst_gdk_pixbuf_sink_class_init (GstGdkPixbufSinkClass * klass)
144 GstBaseSinkClass *basesink_class = GST_BASE_SINK_CLASS (klass);
145 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
146 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
148 gst_element_class_set_static_metadata (element_class, "GdkPixbuf sink",
149 "Sink/Video", "Output images as GdkPixbuf objects in bus messages",
150 "Tim-Philipp Müller <tim centricular net>");
152 gst_element_class_add_static_pad_template (element_class,
153 &pixbufsink_sink_factory);
155 gobject_class->set_property = gst_gdk_pixbuf_sink_set_property;
156 gobject_class->get_property = gst_gdk_pixbuf_sink_get_property;
159 * GstGdkPixbuf:post-messages:
161 * Post messages on the bus containing pixbufs.
163 g_object_class_install_property (gobject_class, PROP_POST_MESSAGES,
164 g_param_spec_boolean ("post-messages", "Post Messages",
165 "Whether to post messages containing pixbufs on the bus",
166 DEFAULT_POST_MESSAGES, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
168 g_object_class_install_property (gobject_class, PROP_LAST_PIXBUF,
169 g_param_spec_object ("last-pixbuf", "Last Pixbuf",
170 "Last GdkPixbuf object rendered", GDK_TYPE_PIXBUF,
171 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
173 basesink_class->start = GST_DEBUG_FUNCPTR (gst_gdk_pixbuf_sink_start);
174 basesink_class->stop = GST_DEBUG_FUNCPTR (gst_gdk_pixbuf_sink_stop);
175 basesink_class->render = GST_DEBUG_FUNCPTR (gst_gdk_pixbuf_sink_render);
176 basesink_class->preroll = GST_DEBUG_FUNCPTR (gst_gdk_pixbuf_sink_preroll);
177 basesink_class->set_caps = GST_DEBUG_FUNCPTR (gst_gdk_pixbuf_sink_set_caps);
181 gst_gdk_pixbuf_sink_init (GstGdkPixbufSink * sink)
185 sink->has_alpha = FALSE;
186 sink->last_pixbuf = NULL;
187 sink->post_messages = DEFAULT_POST_MESSAGES;
189 /* we're not a real video sink, we just derive from GstVideoSink in case
190 * anything interesting is added to it in future */
191 gst_base_sink_set_max_lateness (GST_BASE_SINK (sink), -1);
192 gst_base_sink_set_qos_enabled (GST_BASE_SINK (sink), FALSE);
197 gst_gdk_pixbuf_sink_start (GstBaseSink * basesink)
199 GST_LOG_OBJECT (basesink, "start");
205 gst_gdk_pixbuf_sink_stop (GstBaseSink * basesink)
207 GstGdkPixbufSink *sink = GST_GDK_PIXBUF_SINK (basesink);
209 GST_VIDEO_SINK_WIDTH (sink) = 0;
210 GST_VIDEO_SINK_HEIGHT (sink) = 0;
214 sink->has_alpha = FALSE;
216 if (sink->last_pixbuf) {
217 g_object_unref (sink->last_pixbuf);
218 sink->last_pixbuf = NULL;
221 GST_LOG_OBJECT (sink, "stop");
227 gst_gdk_pixbuf_sink_set_caps (GstBaseSink * basesink, GstCaps * caps)
229 GstGdkPixbufSink *sink = GST_GDK_PIXBUF_SINK (basesink);
232 gint w, h, par_n, par_d;
234 GST_LOG_OBJECT (sink, "caps: %" GST_PTR_FORMAT, caps);
236 if (!gst_video_info_from_caps (&info, caps)) {
237 GST_WARNING_OBJECT (sink, "parse_caps failed");
241 fmt = GST_VIDEO_INFO_FORMAT (&info);
242 w = GST_VIDEO_INFO_WIDTH (&info);
243 h = GST_VIDEO_INFO_HEIGHT (&info);
244 par_n = GST_VIDEO_INFO_PAR_N (&info);
245 par_d = GST_VIDEO_INFO_PAR_N (&info);
247 #ifndef G_DISABLE_ASSERT
250 s = GST_VIDEO_INFO_COMP_PSTRIDE (&info, 0);
251 g_assert ((fmt == GST_VIDEO_FORMAT_RGB && s == 3) ||
252 (fmt == GST_VIDEO_FORMAT_RGBA && s == 4));
256 GST_VIDEO_SINK_WIDTH (sink) = w;
257 GST_VIDEO_SINK_HEIGHT (sink) = h;
262 sink->has_alpha = GST_VIDEO_INFO_HAS_ALPHA (&info);
264 GST_INFO_OBJECT (sink, "format : %d", fmt);
265 GST_INFO_OBJECT (sink, "width x height : %d x %d", w, h);
266 GST_INFO_OBJECT (sink, "pixel-aspect-ratio : %d/%d", par_n, par_d);
274 gst_gdk_pixbuf_sink_pixbuf_destroy_notify (guchar * pixels,
275 GstVideoFrame * frame)
277 gst_video_frame_unmap (frame);
278 gst_buffer_unref (frame->buffer);
279 g_slice_free (GstVideoFrame, frame);
283 gst_gdk_pixbuf_sink_get_pixbuf_from_buffer (GstGdkPixbufSink * sink,
286 GdkPixbuf *pix = NULL;
287 GstVideoFrame *frame;
288 gint minsize, bytes_per_pixel;
290 g_return_val_if_fail (GST_VIDEO_SINK_WIDTH (sink) > 0, NULL);
291 g_return_val_if_fail (GST_VIDEO_SINK_HEIGHT (sink) > 0, NULL);
293 frame = g_slice_new0 (GstVideoFrame);
294 gst_video_frame_map (frame, &sink->info, buf, GST_MAP_READ);
296 bytes_per_pixel = (sink->has_alpha) ? 4 : 3;
298 /* last row needn't have row padding */
299 minsize = (GST_VIDEO_FRAME_COMP_STRIDE (frame, 0) *
300 (GST_VIDEO_SINK_HEIGHT (sink) - 1)) +
301 (bytes_per_pixel * GST_VIDEO_SINK_WIDTH (sink));
303 g_return_val_if_fail (gst_buffer_get_size (buf) >= minsize, NULL);
305 gst_buffer_ref (buf);
306 pix = gdk_pixbuf_new_from_data (GST_VIDEO_FRAME_COMP_DATA (frame, 0),
307 GDK_COLORSPACE_RGB, sink->has_alpha, 8, GST_VIDEO_SINK_WIDTH (sink),
308 GST_VIDEO_SINK_HEIGHT (sink), GST_VIDEO_FRAME_COMP_STRIDE (frame, 0),
309 (GdkPixbufDestroyNotify) gst_gdk_pixbuf_sink_pixbuf_destroy_notify,
316 gst_gdk_pixbuf_sink_handle_buffer (GstBaseSink * basesink, GstBuffer * buf,
317 const gchar * msg_name)
319 GstGdkPixbufSink *sink;
323 sink = GST_GDK_PIXBUF_SINK (basesink);
325 pixbuf = gst_gdk_pixbuf_sink_get_pixbuf_from_buffer (sink, buf);
327 GST_OBJECT_LOCK (sink);
329 do_post = sink->post_messages;
331 if (sink->last_pixbuf)
332 g_object_unref (sink->last_pixbuf);
334 sink->last_pixbuf = pixbuf; /* take ownership */
336 GST_OBJECT_UNLOCK (sink);
338 if (G_UNLIKELY (pixbuf == NULL))
345 GstClockTime timestamp;
346 GstClockTime running_time, stream_time;
348 GstSegment *segment = &basesink->segment;
349 format = segment->format;
351 timestamp = GST_BUFFER_PTS (buf);
352 running_time = gst_segment_to_running_time (segment, format, timestamp);
353 stream_time = gst_segment_to_stream_time (segment, format, timestamp);
355 /* it's okay to keep using pixbuf here, we can be sure no one is going to
356 * unref or change sink->last_pixbuf before we return from this function.
357 * The structure will take its own ref to the pixbuf. */
358 s = gst_structure_new (msg_name,
359 "pixbuf", GDK_TYPE_PIXBUF, pixbuf,
360 "pixel-aspect-ratio", GST_TYPE_FRACTION, sink->par_n, sink->par_d,
361 "timestamp", G_TYPE_UINT64, timestamp,
362 "stream-time", G_TYPE_UINT64, stream_time,
363 "running-time", G_TYPE_UINT64, running_time, NULL);
365 msg = gst_message_new_element (GST_OBJECT_CAST (sink), s);
366 gst_element_post_message (GST_ELEMENT_CAST (sink), msg);
369 g_object_notify (G_OBJECT (sink), "last-pixbuf");
376 /* This shouldn't really happen */
377 GST_ELEMENT_ERROR (sink, LIBRARY, FAILED,
378 ("Couldn't create pixbuf from RGB image."),
379 ("Probably not enough free memory"));
380 return GST_FLOW_ERROR;
385 gst_gdk_pixbuf_sink_preroll (GstBaseSink * basesink, GstBuffer * buf)
387 return gst_gdk_pixbuf_sink_handle_buffer (basesink, buf, "preroll-pixbuf");
391 gst_gdk_pixbuf_sink_render (GstBaseSink * basesink, GstBuffer * buf)
393 return gst_gdk_pixbuf_sink_handle_buffer (basesink, buf, "pixbuf");
397 gst_gdk_pixbuf_sink_set_property (GObject * object, guint prop_id,
398 const GValue * value, GParamSpec * pspec)
400 GstGdkPixbufSink *sink;
402 sink = GST_GDK_PIXBUF_SINK (object);
405 case PROP_POST_MESSAGES:
406 GST_OBJECT_LOCK (sink);
407 sink->post_messages = g_value_get_boolean (value);
408 GST_OBJECT_UNLOCK (sink);
411 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
417 gst_gdk_pixbuf_sink_get_property (GObject * object, guint prop_id,
418 GValue * value, GParamSpec * pspec)
420 GstGdkPixbufSink *sink;
422 sink = GST_GDK_PIXBUF_SINK (object);
425 case PROP_POST_MESSAGES:
426 GST_OBJECT_LOCK (sink);
427 g_value_set_boolean (value, sink->post_messages);
428 GST_OBJECT_UNLOCK (sink);
430 case PROP_LAST_PIXBUF:
431 GST_OBJECT_LOCK (sink);
432 g_value_set_object (value, sink->last_pixbuf);
433 GST_OBJECT_UNLOCK (sink);
436 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);