2 * Copyright (C) <2011> Sebastian Dröge <sebastian.droege@collabora.co.uk>
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 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.
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.
24 #include "gstplaysinkvideoconvert.h"
26 #include <gst/pbutils/pbutils.h>
27 #include <gst/gst-i18n-plugin.h>
29 GST_DEBUG_CATEGORY_STATIC (gst_play_sink_video_convert_debug);
30 #define GST_CAT_DEFAULT gst_play_sink_video_convert_debug
32 #define parent_class gst_play_sink_video_convert_parent_class
34 G_DEFINE_TYPE (GstPlaySinkVideoConvert, gst_play_sink_video_convert,
37 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
42 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
48 is_raw_caps (GstCaps * caps)
54 n = gst_caps_get_size (caps);
55 for (i = 0; i < n; i++) {
56 s = gst_caps_get_structure (caps, i);
57 name = gst_structure_get_name (s);
58 if (!g_str_has_prefix (name, "video/x-raw"))
66 post_missing_element_message (GstPlaySinkVideoConvert * self,
71 msg = gst_missing_element_message_new (GST_ELEMENT_CAST (self), name);
72 gst_element_post_message (GST_ELEMENT_CAST (self), msg);
76 distribute_running_time (GstElement * element, const GstSegment * segment)
81 pad = gst_element_get_static_pad (element, "sink");
84 event = gst_event_new_new_segment_full (FALSE, segment->rate,
85 segment->applied_rate, segment->format, 0, segment->accum, 0);
86 gst_pad_send_event (pad, event);
89 event = gst_event_new_new_segment_full (FALSE, segment->rate,
90 segment->applied_rate, segment->format,
91 segment->start, segment->stop, segment->time);
92 gst_pad_send_event (pad, event);
94 gst_object_unref (pad);
98 pad_blocked_cb (GstPad * pad, GstProbeType type, gpointer type_data,
101 GstPlaySinkVideoConvert *self = user_data;
106 GST_PLAY_SINK_VIDEO_CONVERT_LOCK (self);
107 GST_DEBUG_OBJECT (self, "Pad blocked");
109 /* There must be a peer at this point */
110 peer = gst_pad_get_peer (self->sinkpad);
111 caps = gst_pad_get_current_caps (peer);
113 caps = gst_pad_get_caps (peer, NULL);
114 gst_object_unref (peer);
116 raw = is_raw_caps (caps);
117 GST_DEBUG_OBJECT (self, "Caps %" GST_PTR_FORMAT " are raw: %d", caps, raw);
118 gst_caps_unref (caps);
120 if (raw == self->raw)
125 GstBin *bin = GST_BIN_CAST (self);
126 GstElement *head = NULL, *prev = NULL;
129 GST_DEBUG_OBJECT (self, "Creating raw conversion pipeline");
131 gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (self->sinkpad), NULL);
132 gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (self->srcpad), NULL);
134 self->conv = gst_element_factory_make ("videoconvert", "conv");
135 if (self->conv == NULL) {
136 post_missing_element_message (self, "videoconvert");
137 GST_ELEMENT_WARNING (self, CORE, MISSING_PLUGIN,
138 (_("Missing element '%s' - check your GStreamer installation."),
139 "videoconvert"), ("video rendering might fail"));
141 gst_bin_add (bin, self->conv);
142 gst_element_sync_state_with_parent (self->conv);
143 prev = head = self->conv;
146 self->scale = gst_element_factory_make ("videoscale", "scale");
147 if (self->scale == NULL) {
148 post_missing_element_message (self, "videoscale");
149 GST_ELEMENT_WARNING (self, CORE, MISSING_PLUGIN,
150 (_("Missing element '%s' - check your GStreamer installation."),
151 "videoscale"), ("possibly a liboil version mismatch?"));
153 /* Add black borders if necessary to keep the DAR */
154 g_object_set (self->scale, "add-borders", TRUE, NULL);
155 gst_bin_add (bin, self->scale);
156 gst_element_sync_state_with_parent (self->scale);
158 if (!gst_element_link_pads_full (prev, "src", self->scale, "sink",
159 GST_PAD_LINK_CHECK_TEMPLATE_CAPS))
168 pad = gst_element_get_static_pad (head, "sink");
169 gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (self->sinkpad), pad);
170 gst_object_unref (pad);
174 pad = gst_element_get_static_pad (prev, "src");
175 gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (self->srcpad), pad);
176 gst_object_unref (pad);
179 if (!head && !prev) {
180 gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (self->srcpad),
181 self->sink_proxypad);
184 GST_DEBUG_OBJECT (self, "Raw conversion pipeline created");
186 GstBin *bin = GST_BIN_CAST (self);
188 GST_DEBUG_OBJECT (self, "Removing raw conversion pipeline");
190 gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (self->sinkpad), NULL);
191 gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (self->srcpad), NULL);
194 gst_element_set_state (self->conv, GST_STATE_NULL);
195 gst_bin_remove (bin, self->conv);
199 gst_element_set_state (self->scale, GST_STATE_NULL);
200 gst_bin_remove (bin, self->scale);
204 gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (self->srcpad),
205 self->sink_proxypad);
207 GST_DEBUG_OBJECT (self, "Raw conversion pipeline removed");
211 self->sink_proxypad_block_id = 0;
212 GST_PLAY_SINK_VIDEO_CONVERT_UNLOCK (self);
214 return GST_PROBE_REMOVE;
218 GST_ELEMENT_ERROR (self, CORE, PAD,
219 (NULL), ("Failed to configure the video converter."));
220 gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (self->srcpad),
221 self->sink_proxypad);
222 self->sink_proxypad_block_id = 0;
223 GST_PLAY_SINK_VIDEO_CONVERT_UNLOCK (self);
225 return GST_PROBE_REMOVE;
230 block_proxypad (GstPlaySinkVideoConvert * self)
232 if (self->sink_proxypad_block_id == 0) {
233 self->sink_proxypad_block_id =
234 gst_pad_add_probe (self->sink_proxypad, GST_PROBE_TYPE_BLOCK,
235 pad_blocked_cb, gst_object_ref (self),
236 (GDestroyNotify) gst_object_unref);
241 unblock_proxypad (GstPlaySinkVideoConvert * self)
243 if (self->sink_proxypad_block_id != 0) {
244 gst_pad_remove_probe (self->sink_proxypad, self->sink_proxypad_block_id);
245 self->sink_proxypad_block_id = 0;
250 gst_play_sink_video_convert_sink_setcaps (GstPlaySinkVideoConvert * self,
255 gboolean reconfigure = FALSE;
257 GST_PLAY_SINK_VIDEO_CONVERT_LOCK (self);
258 s = gst_caps_get_structure (caps, 0);
259 name = gst_structure_get_name (s);
261 if (g_str_has_prefix (name, "video/x-raw")) {
262 if (!self->raw && !gst_pad_is_blocked (self->sink_proxypad)) {
263 GST_DEBUG_OBJECT (self, "Changing caps from non-raw to raw");
265 block_proxypad (self);
268 if (self->raw && !gst_pad_is_blocked (self->sink_proxypad)) {
269 GST_DEBUG_OBJECT (self, "Changing caps from raw to non-raw");
271 block_proxypad (self);
275 /* Otherwise the setcaps below fails */
277 gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (self->sinkpad), NULL);
278 gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (self->srcpad), NULL);
280 GST_PLAY_SINK_VIDEO_CONVERT_UNLOCK (self);
282 GST_DEBUG_OBJECT (self, "Setting sink caps %" GST_PTR_FORMAT, caps);
288 gst_play_sink_video_convert_sink_event (GstPad * pad, GstEvent * event)
290 GstPlaySinkVideoConvert *self =
291 GST_PLAY_SINK_VIDEO_CONVERT (gst_pad_get_parent (pad));
294 switch (GST_EVENT_TYPE (event)) {
299 gst_event_parse_caps (event, &caps);
300 ret = gst_play_sink_video_convert_sink_setcaps (self, caps);
307 ret = gst_proxy_pad_event_default (pad, gst_event_ref (event));
309 switch (GST_EVENT_TYPE (event)) {
310 case GST_EVENT_SEGMENT:
311 GST_PLAY_SINK_VIDEO_CONVERT_LOCK (self);
312 GST_DEBUG_OBJECT (self, "Segment before %" GST_SEGMENT_FORMAT,
314 gst_event_copy_segment (event, &self->segment);
315 GST_DEBUG_OBJECT (self, "Segment after %" GST_SEGMENT_FORMAT,
317 GST_PLAY_SINK_VIDEO_CONVERT_UNLOCK (self);
319 case GST_EVENT_FLUSH_STOP:
320 GST_PLAY_SINK_VIDEO_CONVERT_LOCK (self);
321 GST_DEBUG_OBJECT (self, "Resetting segment");
322 gst_segment_init (&self->segment, GST_FORMAT_UNDEFINED);
323 GST_PLAY_SINK_VIDEO_CONVERT_UNLOCK (self);
329 gst_event_unref (event);
330 gst_object_unref (self);
336 gst_play_sink_video_convert_getcaps (GstPad * pad, GstCaps * filter)
338 GstPlaySinkVideoConvert *self =
339 GST_PLAY_SINK_VIDEO_CONVERT (gst_pad_get_parent (pad));
341 GstPad *otherpad, *peer;
343 GST_PLAY_SINK_VIDEO_CONVERT_LOCK (self);
344 if (pad == self->srcpad)
345 otherpad = gst_object_ref (self->sinkpad);
347 otherpad = gst_object_ref (self->srcpad);
348 GST_PLAY_SINK_VIDEO_CONVERT_UNLOCK (self);
350 peer = gst_pad_get_peer (otherpad);
352 ret = gst_pad_get_caps (peer, filter);
353 gst_object_unref (peer);
355 ret = (filter ? gst_caps_ref (filter) : gst_caps_new_any ());
358 gst_object_unref (otherpad);
359 gst_object_unref (self);
365 gst_play_sink_video_convert_finalize (GObject * object)
367 GstPlaySinkVideoConvert *self = GST_PLAY_SINK_VIDEO_CONVERT_CAST (object);
369 gst_object_unref (self->sink_proxypad);
370 g_mutex_free (self->lock);
372 G_OBJECT_CLASS (parent_class)->finalize (object);
375 static GstStateChangeReturn
376 gst_play_sink_video_convert_change_state (GstElement * element,
377 GstStateChange transition)
379 GstStateChangeReturn ret;
380 GstPlaySinkVideoConvert *self = GST_PLAY_SINK_VIDEO_CONVERT_CAST (element);
382 switch (transition) {
383 case GST_STATE_CHANGE_PAUSED_TO_READY:
384 GST_PLAY_SINK_VIDEO_CONVERT_LOCK (self);
385 unblock_proxypad (self);
386 GST_PLAY_SINK_VIDEO_CONVERT_UNLOCK (self);
392 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
393 if (ret == GST_STATE_CHANGE_FAILURE)
396 switch (transition) {
397 case GST_STATE_CHANGE_PAUSED_TO_READY:
398 GST_PLAY_SINK_VIDEO_CONVERT_LOCK (self);
399 gst_segment_init (&self->segment, GST_FORMAT_UNDEFINED);
401 gst_element_set_state (self->conv, GST_STATE_NULL);
402 gst_bin_remove (GST_BIN_CAST (self), self->conv);
406 gst_element_set_state (self->scale, GST_STATE_NULL);
407 gst_bin_remove (GST_BIN_CAST (self), self->scale);
410 gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (self->srcpad),
411 self->sink_proxypad);
413 GST_PLAY_SINK_VIDEO_CONVERT_UNLOCK (self);
415 case GST_STATE_CHANGE_READY_TO_PAUSED:
416 GST_PLAY_SINK_VIDEO_CONVERT_LOCK (self);
417 if (!gst_pad_is_blocked (self->sink_proxypad))
418 block_proxypad (self);
419 GST_PLAY_SINK_VIDEO_CONVERT_UNLOCK (self);
428 gst_play_sink_video_convert_class_init (GstPlaySinkVideoConvertClass * klass)
430 GObjectClass *gobject_class;
431 GstElementClass *gstelement_class;
433 GST_DEBUG_CATEGORY_INIT (gst_play_sink_video_convert_debug,
434 "playsinkvideoconvert", 0, "play bin");
436 gobject_class = (GObjectClass *) klass;
437 gstelement_class = (GstElementClass *) klass;
439 gobject_class->finalize = gst_play_sink_video_convert_finalize;
441 gst_element_class_add_pad_template (gstelement_class,
442 gst_static_pad_template_get (&srctemplate));
443 gst_element_class_add_pad_template (gstelement_class,
444 gst_static_pad_template_get (&sinktemplate));
445 gst_element_class_set_details_simple (gstelement_class,
446 "Player Sink Video Converter", "Video/Bin/Converter",
447 "Convenience bin for video conversion",
448 "Sebastian Dröge <sebastian.droege@collabora.co.uk>");
450 gstelement_class->change_state =
451 GST_DEBUG_FUNCPTR (gst_play_sink_video_convert_change_state);
455 gst_play_sink_video_convert_init (GstPlaySinkVideoConvert * self)
457 GstPadTemplate *templ;
459 self->lock = g_mutex_new ();
460 gst_segment_init (&self->segment, GST_FORMAT_UNDEFINED);
462 templ = gst_static_pad_template_get (&sinktemplate);
463 self->sinkpad = gst_ghost_pad_new_no_target_from_template ("sink", templ);
464 gst_pad_set_event_function (self->sinkpad,
465 GST_DEBUG_FUNCPTR (gst_play_sink_video_convert_sink_event));
466 gst_pad_set_getcaps_function (self->sinkpad,
467 GST_DEBUG_FUNCPTR (gst_play_sink_video_convert_getcaps));
469 self->sink_proxypad =
470 GST_PAD_CAST (gst_proxy_pad_get_internal (GST_PROXY_PAD (self->sinkpad)));
472 gst_element_add_pad (GST_ELEMENT_CAST (self), self->sinkpad);
473 gst_object_unref (templ);
475 templ = gst_static_pad_template_get (&srctemplate);
476 self->srcpad = gst_ghost_pad_new_no_target_from_template ("src", templ);
477 gst_pad_set_getcaps_function (self->srcpad,
478 GST_DEBUG_FUNCPTR (gst_play_sink_video_convert_getcaps));
479 gst_element_add_pad (GST_ELEMENT_CAST (self), self->srcpad);
480 gst_object_unref (templ);
482 gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (self->srcpad),
483 self->sink_proxypad);