2 * Copyright (C) <2011> Sebastian Dröge <sebastian.droege@collabora.co.uk>
3 * Copyright (C) <2011> Vincent Penquerch <vincent.penquerch@collabora.co.uk>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
25 #include "gstplaysinkconvertbin.h"
27 #include <gst/pbutils/pbutils.h>
28 #include <gst/gst-i18n-plugin.h>
30 GST_DEBUG_CATEGORY_STATIC (gst_play_sink_convert_bin_debug);
31 #define GST_CAT_DEFAULT gst_play_sink_convert_bin_debug
33 #define parent_class gst_play_sink_convert_bin_parent_class
35 static gboolean gst_play_sink_convert_bin_sink_setcaps (GstPlaySinkConvertBin *
36 self, GstCaps * caps);
38 G_DEFINE_TYPE (GstPlaySinkConvertBin, gst_play_sink_convert_bin, GST_TYPE_BIN);
40 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
45 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
51 is_raw_caps (GstCaps * caps, gboolean audio)
56 const gchar *prefix = audio ? "audio/x-raw-" : "video/x-raw-";
58 n = gst_caps_get_size (caps);
59 for (i = 0; i < n; i++) {
60 s = gst_caps_get_structure (caps, i);
61 name = gst_structure_get_name (s);
62 if (!g_str_has_prefix (name, prefix))
70 gst_play_sink_convert_bin_post_missing_element_message (GstPlaySinkConvertBin *
71 self, const gchar * name)
75 msg = gst_missing_element_message_new (GST_ELEMENT_CAST (self), name);
76 gst_element_post_message (GST_ELEMENT_CAST (self), msg);
80 distribute_running_time (GstElement * element, const GstSegment * segment)
85 pad = gst_element_get_static_pad (element, "sink");
87 gst_pad_send_event (pad, gst_event_new_flush_start ());
88 gst_pad_send_event (pad, gst_event_new_flush_stop (FALSE));
90 event = gst_event_new_segment (segment);
91 gst_pad_send_event (pad, event);
93 gst_object_unref (pad);
97 gst_play_sink_convert_bin_add_conversion_element (GstPlaySinkConvertBin * self,
100 self->conversion_elements = g_list_append (self->conversion_elements, el);
101 gst_bin_add (GST_BIN (self), gst_object_ref (el));
105 gst_play_sink_convert_bin_add_conversion_element_factory (GstPlaySinkConvertBin
106 * self, const char *factory, const char *name)
110 el = gst_element_factory_make (factory, name);
112 gst_play_sink_convert_bin_post_missing_element_message (self, factory);
113 GST_ELEMENT_WARNING (self, CORE, MISSING_PLUGIN,
114 (_("Missing element '%s' - check your GStreamer installation."),
116 (self->audio ? "audio rendering might fail" :
117 "video rendering might fail"));
119 gst_play_sink_convert_bin_add_conversion_element (self, el);
125 gst_play_sink_convert_bin_add_identity (GstPlaySinkConvertBin * self)
130 self->identity = gst_element_factory_make ("identity", "identity");
131 if (self->identity == NULL) {
132 gst_play_sink_convert_bin_post_missing_element_message (self, "identity");
133 GST_ELEMENT_WARNING (self, CORE, MISSING_PLUGIN,
134 (_("Missing element '%s' - check your GStreamer installation."),
135 "identity"), (self->audio ?
136 "audio rendering might fail" : "video rendering might fail")
140 g_object_set (self->identity, "silent", TRUE, "signal-handoffs", FALSE,
142 gst_bin_add (GST_BIN_CAST (self), self->identity);
147 gst_play_sink_convert_bin_set_targets (GstPlaySinkConvertBin * self,
148 gboolean passthrough)
151 GstElement *head, *tail;
153 GST_DEBUG_OBJECT (self, "Setting pad targets with passthrough %d",
155 if (self->conversion_elements == NULL || passthrough) {
156 GST_DEBUG_OBJECT (self, "no conversion elements, using identity (%p) as "
157 "head/tail", self->identity);
159 GST_WARNING_OBJECT (self,
160 "Doing passthrough as no converter elements were added");
162 head = tail = self->identity;
164 head = GST_ELEMENT (g_list_first (self->conversion_elements)->data);
165 tail = GST_ELEMENT (g_list_last (self->conversion_elements)->data);
166 GST_DEBUG_OBJECT (self, "conversion elements in use, picking "
167 "head:%s and tail:%s", GST_OBJECT_NAME (head), GST_OBJECT_NAME (tail));
170 g_return_if_fail (head != NULL);
171 g_return_if_fail (tail != NULL);
173 pad = gst_element_get_static_pad (head, "sink");
174 GST_DEBUG_OBJECT (self, "Ghosting bin sink pad to %" GST_PTR_FORMAT, pad);
175 gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (self->sinkpad), pad);
176 gst_object_unref (pad);
178 pad = gst_element_get_static_pad (tail, "src");
179 GST_DEBUG_OBJECT (self, "Ghosting bin src pad to %" GST_PTR_FORMAT, pad);
180 gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (self->srcpad), pad);
181 gst_object_unref (pad);
185 gst_play_sink_convert_bin_remove_element (GstElement * element,
186 GstPlaySinkConvertBin * self)
188 gst_element_set_state (element, GST_STATE_NULL);
189 gst_bin_remove (GST_BIN_CAST (self), element);
193 gst_play_sink_convert_bin_on_element_added (GstElement * element,
194 GstPlaySinkConvertBin * self)
196 gst_element_sync_state_with_parent (element);
197 distribute_running_time (element, &self->segment);
200 static GstPadProbeReturn
201 pad_blocked_cb (GstPad * pad, GstPadProbeInfo * info, gpointer user_data)
203 GstPlaySinkConvertBin *self = user_data;
208 GST_PLAY_SINK_CONVERT_BIN_LOCK (self);
209 GST_DEBUG_OBJECT (self, "Pad blocked");
211 /* There must be a peer at this point */
212 peer = gst_pad_get_peer (self->sinkpad);
213 caps = gst_pad_get_current_caps (peer);
215 caps = gst_pad_query_caps (peer, NULL);
216 gst_object_unref (peer);
218 raw = is_raw_caps (caps, self->audio);
219 GST_DEBUG_OBJECT (self, "Caps %" GST_PTR_FORMAT " are raw: %d", caps, raw);
220 gst_caps_unref (caps);
222 if (raw == self->raw)
226 gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (self->sinkpad), NULL);
227 gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (self->srcpad), NULL);
230 GST_DEBUG_OBJECT (self, "Switching to raw conversion pipeline");
232 if (self->conversion_elements)
233 g_list_foreach (self->conversion_elements,
234 (GFunc) gst_play_sink_convert_bin_on_element_added, self);
237 GST_DEBUG_OBJECT (self, "Switch to passthrough pipeline");
239 gst_play_sink_convert_bin_on_element_added (self->identity, self);
242 gst_play_sink_convert_bin_set_targets (self, !raw);
245 self->sink_proxypad_block_id = 0;
246 GST_PLAY_SINK_CONVERT_BIN_UNLOCK (self);
248 return GST_PAD_PROBE_REMOVE;
252 gst_play_sink_convert_bin_sink_event (GstPad * pad, GstObject * parent,
255 GstPlaySinkConvertBin *self = GST_PLAY_SINK_CONVERT_BIN (parent);
258 switch (GST_EVENT_TYPE (event)) {
263 gst_event_parse_caps (event, &caps);
264 ret = gst_play_sink_convert_bin_sink_setcaps (self, caps);
271 ret = gst_proxy_pad_event_default (pad, parent, gst_event_ref (event));
273 if (GST_EVENT_TYPE (event) == GST_EVENT_SEGMENT) {
276 GST_PLAY_SINK_CONVERT_BIN_LOCK (self);
277 gst_event_copy_segment (event, &seg);
279 GST_DEBUG_OBJECT (self, "Segment before %" GST_SEGMENT_FORMAT,
282 GST_DEBUG_OBJECT (self, "Segment after %" GST_SEGMENT_FORMAT,
284 GST_PLAY_SINK_CONVERT_BIN_UNLOCK (self);
285 } else if (GST_EVENT_TYPE (event) == GST_EVENT_FLUSH_STOP) {
286 GST_PLAY_SINK_CONVERT_BIN_LOCK (self);
287 GST_DEBUG_OBJECT (self, "Resetting segment");
288 gst_segment_init (&self->segment, GST_FORMAT_UNDEFINED);
289 GST_PLAY_SINK_CONVERT_BIN_UNLOCK (self);
292 gst_event_unref (event);
298 block_proxypad (GstPlaySinkConvertBin * self)
300 if (self->sink_proxypad_block_id == 0) {
301 self->sink_proxypad_block_id =
302 gst_pad_add_probe (self->sink_proxypad,
303 GST_PAD_PROBE_TYPE_BLOCK_DOWNSTREAM, pad_blocked_cb,
304 gst_object_ref (self), (GDestroyNotify) gst_object_unref);
309 unblock_proxypad (GstPlaySinkConvertBin * self)
311 if (self->sink_proxypad_block_id != 0) {
312 gst_pad_remove_probe (self->sink_proxypad, self->sink_proxypad_block_id);
313 self->sink_proxypad_block_id = 0;
318 gst_play_sink_convert_bin_sink_setcaps (GstPlaySinkConvertBin * self,
323 gboolean reconfigure = FALSE;
326 GST_DEBUG_OBJECT (self, "setcaps");
327 GST_PLAY_SINK_CONVERT_BIN_LOCK (self);
328 s = gst_caps_get_structure (caps, 0);
329 name = gst_structure_get_name (s);
332 raw = g_str_has_prefix (name, "audio/x-raw-");
334 raw = g_str_has_prefix (name, "video/x-raw-");
337 GST_DEBUG_OBJECT (self, "raw %d, self->raw %d, blocked %d",
338 raw, self->raw, gst_pad_is_blocked (self->sink_proxypad));
341 if (!gst_pad_is_blocked (self->sink_proxypad)) {
342 GstPad *target = gst_ghost_pad_get_target (GST_GHOST_PAD (self->sinkpad));
344 if (!self->raw || (target && !gst_pad_query_accept_caps (target, caps))) {
346 GST_DEBUG_OBJECT (self, "Changing caps from non-raw to raw");
348 GST_DEBUG_OBJECT (self, "Changing caps in an incompatible way");
351 block_proxypad (self);
355 gst_object_unref (target);
358 if (self->raw && !gst_pad_is_blocked (self->sink_proxypad)) {
359 GST_DEBUG_OBJECT (self, "Changing caps from raw to non-raw");
361 block_proxypad (self);
365 /* Otherwise the setcaps below fails */
367 gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (self->sinkpad), NULL);
368 gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (self->srcpad), NULL);
371 GST_PLAY_SINK_CONVERT_BIN_UNLOCK (self);
373 GST_DEBUG_OBJECT (self, "Setting sink caps %" GST_PTR_FORMAT, caps);
379 gst_play_sink_convert_bin_getcaps (GstPad * pad, GstCaps * filter)
381 GstPlaySinkConvertBin *self =
382 GST_PLAY_SINK_CONVERT_BIN (gst_pad_get_parent (pad));
384 GstPad *otherpad, *peer;
386 GST_PLAY_SINK_CONVERT_BIN_LOCK (self);
387 if (pad == self->srcpad) {
388 otherpad = self->sinkpad;
389 } else if (pad == self->sinkpad) {
390 otherpad = self->srcpad;
392 GST_ERROR_OBJECT (pad, "Not one of our pads");
397 peer = gst_pad_get_peer (otherpad);
399 GstCaps *peer_caps = gst_pad_query_caps (peer, filter);
400 gst_object_unref (peer);
401 if (self->converter_caps) {
402 peer_caps = gst_caps_make_writable (peer_caps);
403 gst_caps_merge (peer_caps, gst_caps_ref (self->converter_caps));
409 ret = gst_caps_ref (self->converter_caps);
412 ret = gst_caps_new_any ();
414 GST_PLAY_SINK_CONVERT_BIN_UNLOCK (self);
416 gst_object_unref (self);
418 GST_DEBUG_OBJECT (pad, "Returning caps %" GST_PTR_FORMAT, ret);
424 gst_play_sink_convert_bin_acceptcaps (GstPad * pad, GstCaps * caps)
426 GstCaps *allowed_caps;
429 allowed_caps = gst_pad_query_caps (pad, NULL);
430 /* FIXME 0.11: Should be a subset check now */
431 ret = gst_caps_can_intersect (caps, allowed_caps);
432 gst_caps_unref (allowed_caps);
438 gst_play_sink_convert_bin_query (GstPad * pad, GstObject * parent,
441 gboolean res = FALSE;
443 switch (GST_QUERY_TYPE (query)) {
444 case GST_QUERY_ACCEPT_CAPS:
448 gst_query_parse_accept_caps (query, &caps);
449 gst_query_set_accept_caps_result (query,
450 gst_play_sink_convert_bin_acceptcaps (pad, caps));
456 GstCaps *filter, *caps;
458 gst_query_parse_caps (query, &filter);
459 caps = gst_play_sink_convert_bin_getcaps (pad, filter);
460 gst_query_set_caps_result (query, caps);
461 gst_caps_unref (caps);
466 res = gst_pad_query_default (pad, parent, query);
473 gst_play_sink_convert_bin_remove_elements (GstPlaySinkConvertBin * self)
475 if (self->conversion_elements) {
476 g_list_foreach (self->conversion_elements,
477 (GFunc) gst_play_sink_convert_bin_remove_element, self);
478 g_list_free (self->conversion_elements);
479 self->conversion_elements = NULL;
481 if (self->converter_caps) {
482 gst_caps_unref (self->converter_caps);
483 self->converter_caps = NULL;
488 gst_play_sink_convert_bin_dispose (GObject * object)
490 GstPlaySinkConvertBin *self = GST_PLAY_SINK_CONVERT_BIN_CAST (object);
492 gst_play_sink_convert_bin_remove_elements (self);
494 G_OBJECT_CLASS (parent_class)->dispose (object);
498 gst_play_sink_convert_bin_finalize (GObject * object)
500 GstPlaySinkConvertBin *self = GST_PLAY_SINK_CONVERT_BIN_CAST (object);
502 gst_object_unref (self->sink_proxypad);
503 g_mutex_free (self->lock);
505 G_OBJECT_CLASS (parent_class)->finalize (object);
509 gst_play_sink_convert_bin_cache_converter_caps (GstPlaySinkConvertBin * self)
514 if (self->converter_caps) {
515 gst_caps_unref (self->converter_caps);
516 self->converter_caps = NULL;
519 if (!self->conversion_elements) {
520 GST_WARNING_OBJECT (self, "No conversion elements");
524 head = GST_ELEMENT (g_list_first (self->conversion_elements)->data);
525 pad = gst_element_get_static_pad (head, "sink");
527 GST_WARNING_OBJECT (self, "No sink pad found");
531 self->converter_caps = gst_pad_query_caps (pad, NULL);
532 GST_INFO_OBJECT (self, "Converter caps: %" GST_PTR_FORMAT,
533 self->converter_caps);
535 gst_object_unref (pad);
538 static GstStateChangeReturn
539 gst_play_sink_convert_bin_change_state (GstElement * element,
540 GstStateChange transition)
542 GstStateChangeReturn ret;
543 GstPlaySinkConvertBin *self = GST_PLAY_SINK_CONVERT_BIN_CAST (element);
545 switch (transition) {
546 case GST_STATE_CHANGE_PAUSED_TO_READY:
547 GST_PLAY_SINK_CONVERT_BIN_LOCK (self);
548 unblock_proxypad (self);
549 GST_PLAY_SINK_CONVERT_BIN_UNLOCK (self);
551 case GST_STATE_CHANGE_READY_TO_PAUSED:
552 GST_PLAY_SINK_CONVERT_BIN_LOCK (self);
553 gst_segment_init (&self->segment, GST_FORMAT_UNDEFINED);
554 gst_play_sink_convert_bin_set_targets (self, TRUE);
556 GST_PLAY_SINK_CONVERT_BIN_UNLOCK (self);
562 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
563 if (ret == GST_STATE_CHANGE_FAILURE)
566 switch (transition) {
567 case GST_STATE_CHANGE_PAUSED_TO_READY:
568 GST_PLAY_SINK_CONVERT_BIN_LOCK (self);
569 gst_segment_init (&self->segment, GST_FORMAT_UNDEFINED);
570 gst_play_sink_convert_bin_set_targets (self, TRUE);
572 GST_PLAY_SINK_CONVERT_BIN_UNLOCK (self);
574 case GST_STATE_CHANGE_READY_TO_PAUSED:
575 GST_PLAY_SINK_CONVERT_BIN_LOCK (self);
576 unblock_proxypad (self);
577 GST_PLAY_SINK_CONVERT_BIN_UNLOCK (self);
587 gst_play_sink_convert_bin_class_init (GstPlaySinkConvertBinClass * klass)
589 GObjectClass *gobject_class;
590 GstElementClass *gstelement_class;
592 GST_DEBUG_CATEGORY_INIT (gst_play_sink_convert_bin_debug,
593 "playsinkconvertbin", 0, "play bin");
595 gobject_class = (GObjectClass *) klass;
596 gstelement_class = (GstElementClass *) klass;
598 gobject_class->dispose = gst_play_sink_convert_bin_dispose;
599 gobject_class->finalize = gst_play_sink_convert_bin_finalize;
601 gst_element_class_add_pad_template (gstelement_class,
602 gst_static_pad_template_get (&srctemplate));
603 gst_element_class_add_pad_template (gstelement_class,
604 gst_static_pad_template_get (&sinktemplate));
605 gst_element_class_set_details_simple (gstelement_class,
606 "Player Sink Converter Bin", "Bin/Converter",
607 "Convenience bin for audio/video conversion",
608 "Sebastian Dröge <sebastian.droege@collabora.co.uk>");
610 gstelement_class->change_state =
611 GST_DEBUG_FUNCPTR (gst_play_sink_convert_bin_change_state);
615 gst_play_sink_convert_bin_init (GstPlaySinkConvertBin * self)
617 GstPadTemplate *templ;
619 self->lock = g_mutex_new ();
620 gst_segment_init (&self->segment, GST_FORMAT_UNDEFINED);
622 templ = gst_static_pad_template_get (&sinktemplate);
623 self->sinkpad = gst_ghost_pad_new_no_target_from_template ("sink", templ);
624 gst_pad_set_event_function (self->sinkpad,
625 GST_DEBUG_FUNCPTR (gst_play_sink_convert_bin_sink_event));
626 gst_pad_set_query_function (self->sinkpad,
627 GST_DEBUG_FUNCPTR (gst_play_sink_convert_bin_query));
629 self->sink_proxypad =
630 GST_PAD_CAST (gst_proxy_pad_get_internal (GST_PROXY_PAD (self->sinkpad)));
632 gst_element_add_pad (GST_ELEMENT_CAST (self), self->sinkpad);
633 gst_object_unref (templ);
635 templ = gst_static_pad_template_get (&srctemplate);
636 self->srcpad = gst_ghost_pad_new_no_target_from_template ("src", templ);
637 gst_pad_set_query_function (self->srcpad,
638 GST_DEBUG_FUNCPTR (gst_play_sink_convert_bin_query));
639 gst_element_add_pad (GST_ELEMENT_CAST (self), self->srcpad);
640 gst_object_unref (templ);
642 gst_play_sink_convert_bin_add_identity (self);