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., 51 Franklin St, Fifth Floor,
18 * Boston, MA 02110-1301, 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 void 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_equal (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 gst_play_sink_convert_bin_add_conversion_element (GstPlaySinkConvertBin * self,
83 self->conversion_elements = g_list_append (self->conversion_elements, el);
84 gst_bin_add (GST_BIN (self), gst_object_ref (el));
88 gst_play_sink_convert_bin_add_conversion_element_factory (GstPlaySinkConvertBin
89 * self, const char *factory, const char *name)
93 el = gst_element_factory_make (factory, name);
95 gst_play_sink_convert_bin_post_missing_element_message (self, factory);
96 GST_ELEMENT_WARNING (self, CORE, MISSING_PLUGIN,
97 (_("Missing element '%s' - check your GStreamer installation."),
99 (self->audio ? "audio rendering might fail" :
100 "video rendering might fail"));
102 gst_play_sink_convert_bin_add_conversion_element (self, el);
108 gst_play_sink_convert_bin_add_identity (GstPlaySinkConvertBin * self)
113 self->identity = gst_element_factory_make ("identity", "identity");
114 if (self->identity == NULL) {
115 gst_play_sink_convert_bin_post_missing_element_message (self, "identity");
116 GST_ELEMENT_WARNING (self, CORE, MISSING_PLUGIN,
117 (_("Missing element '%s' - check your GStreamer installation."),
118 "identity"), (self->audio ?
119 "audio rendering might fail" : "video rendering might fail")
123 g_object_set (self->identity, "silent", TRUE, "signal-handoffs", FALSE,
125 gst_bin_add (GST_BIN_CAST (self), self->identity);
130 gst_play_sink_convert_bin_set_targets (GstPlaySinkConvertBin * self,
131 gboolean passthrough)
134 GstElement *head, *tail;
136 GST_DEBUG_OBJECT (self, "Setting pad targets with passthrough %d",
138 if (self->conversion_elements == NULL || passthrough) {
139 GST_DEBUG_OBJECT (self, "no conversion elements, using identity (%p) as "
140 "head/tail", self->identity);
142 GST_WARNING_OBJECT (self,
143 "Doing passthrough as no converter elements were added");
145 head = tail = self->identity;
147 head = GST_ELEMENT (g_list_first (self->conversion_elements)->data);
148 tail = GST_ELEMENT (g_list_last (self->conversion_elements)->data);
149 GST_DEBUG_OBJECT (self, "conversion elements in use, picking "
150 "head:%s and tail:%s", GST_OBJECT_NAME (head), GST_OBJECT_NAME (tail));
153 g_return_if_fail (head != NULL);
154 g_return_if_fail (tail != NULL);
156 pad = gst_element_get_static_pad (head, "sink");
157 GST_DEBUG_OBJECT (self, "Ghosting bin sink pad to %" GST_PTR_FORMAT, pad);
158 gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (self->sinkpad), pad);
159 gst_object_unref (pad);
161 pad = gst_element_get_static_pad (tail, "src");
162 GST_DEBUG_OBJECT (self, "Ghosting bin src pad to %" GST_PTR_FORMAT, pad);
163 gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (self->srcpad), pad);
164 gst_object_unref (pad);
168 gst_play_sink_convert_bin_remove_element (GstElement * element,
169 GstPlaySinkConvertBin * self)
171 gst_element_set_state (element, GST_STATE_NULL);
172 gst_object_unref (element);
173 gst_bin_remove (GST_BIN_CAST (self), element);
177 gst_play_sink_convert_bin_on_element_added (GstElement * element,
178 GstPlaySinkConvertBin * self)
180 gst_element_sync_state_with_parent (element);
183 static GstPadProbeReturn
184 pad_blocked_cb (GstPad * pad, GstPadProbeInfo * info, gpointer user_data)
186 GstPlaySinkConvertBin *self = user_data;
191 if (GST_IS_EVENT (info->data) && !GST_EVENT_IS_SERIALIZED (info->data)) {
192 GST_DEBUG_OBJECT (self, "Letting non-serialized event %s pass",
193 GST_EVENT_TYPE_NAME (info->data));
194 return GST_PAD_PROBE_PASS;
197 GST_PLAY_SINK_CONVERT_BIN_LOCK (self);
198 GST_DEBUG_OBJECT (self, "Pad blocked");
200 /* There must be a peer at this point */
201 peer = gst_pad_get_peer (self->sinkpad);
202 caps = gst_pad_get_current_caps (peer);
204 caps = gst_pad_query_caps (peer, NULL);
205 gst_object_unref (peer);
207 raw = is_raw_caps (caps, self->audio);
208 GST_DEBUG_OBJECT (self, "Caps %" GST_PTR_FORMAT " are raw: %d", caps, raw);
209 gst_caps_unref (caps);
211 if (raw == self->raw)
215 gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (self->sinkpad), NULL);
216 gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (self->srcpad), NULL);
219 GST_DEBUG_OBJECT (self, "Switching to raw conversion pipeline");
221 if (self->conversion_elements)
222 g_list_foreach (self->conversion_elements,
223 (GFunc) gst_play_sink_convert_bin_on_element_added, self);
226 GST_DEBUG_OBJECT (self, "Switch to passthrough pipeline");
228 gst_play_sink_convert_bin_on_element_added (self->identity, self);
231 gst_play_sink_convert_bin_set_targets (self, !raw);
234 self->sink_proxypad_block_id = 0;
235 GST_PLAY_SINK_CONVERT_BIN_UNLOCK (self);
237 return GST_PAD_PROBE_REMOVE;
241 gst_play_sink_convert_bin_sink_event (GstPad * pad, GstObject * parent,
244 GstPlaySinkConvertBin *self = GST_PLAY_SINK_CONVERT_BIN (parent);
247 switch (GST_EVENT_TYPE (event)) {
252 gst_event_parse_caps (event, &caps);
253 gst_play_sink_convert_bin_sink_setcaps (self, caps);
260 ret = gst_pad_event_default (pad, parent, gst_event_ref (event));
262 gst_event_unref (event);
268 block_proxypad (GstPlaySinkConvertBin * self)
270 if (self->sink_proxypad_block_id == 0) {
271 self->sink_proxypad_block_id =
272 gst_pad_add_probe (self->sink_proxypad,
273 GST_PAD_PROBE_TYPE_BLOCK_DOWNSTREAM, pad_blocked_cb, self, NULL);
278 unblock_proxypad (GstPlaySinkConvertBin * self)
280 if (self->sink_proxypad_block_id != 0) {
281 gst_pad_remove_probe (self->sink_proxypad, self->sink_proxypad_block_id);
282 self->sink_proxypad_block_id = 0;
287 gst_play_sink_convert_bin_sink_setcaps (GstPlaySinkConvertBin * self,
292 gboolean reconfigure = FALSE;
295 GST_DEBUG_OBJECT (self, "Setting sink caps %" GST_PTR_FORMAT, caps);
297 GST_PLAY_SINK_CONVERT_BIN_LOCK (self);
298 s = gst_caps_get_structure (caps, 0);
299 name = gst_structure_get_name (s);
302 raw = g_str_equal (name, "audio/x-raw");
304 raw = g_str_equal (name, "video/x-raw");
307 GST_DEBUG_OBJECT (self, "raw %d, self->raw %d, blocked %d",
308 raw, self->raw, gst_pad_is_blocked (self->sink_proxypad));
311 if (!gst_pad_is_blocked (self->sink_proxypad)) {
312 GstPad *target = gst_ghost_pad_get_target (GST_GHOST_PAD (self->sinkpad));
314 if (!self->raw || (target && !gst_pad_query_accept_caps (target, caps))) {
316 GST_DEBUG_OBJECT (self, "Changing caps from non-raw to raw");
318 GST_DEBUG_OBJECT (self, "Changing caps in an incompatible way");
321 block_proxypad (self);
325 gst_object_unref (target);
328 if (self->raw && !gst_pad_is_blocked (self->sink_proxypad)) {
329 GST_DEBUG_OBJECT (self, "Changing caps from raw to non-raw");
331 block_proxypad (self);
335 /* Otherwise the setcaps below fails */
337 gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (self->sinkpad), NULL);
338 gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (self->srcpad), NULL);
341 GST_PLAY_SINK_CONVERT_BIN_UNLOCK (self);
344 #define GST_PLAY_SINK_CONVERT_BIN_FILTER_CAPS(filter,caps) G_STMT_START { \
346 GstCaps *intersection = \
347 gst_caps_intersect_full ((filter), (caps), GST_CAPS_INTERSECT_FIRST); \
348 gst_caps_unref ((caps)); \
349 (caps) = intersection; \
354 gst_play_sink_convert_bin_acceptcaps (GstPad * pad, GstCaps * caps)
356 GstPlaySinkConvertBin *self =
357 GST_PLAY_SINK_CONVERT_BIN (gst_pad_get_parent (pad));
361 GST_PLAY_SINK_CONVERT_BIN_LOCK (self);
362 if (pad == self->srcpad) {
363 otherpad = self->sinkpad;
364 } else if (pad == self->sinkpad) {
365 otherpad = self->srcpad;
367 GST_ERROR_OBJECT (pad, "Not one of our pads");
372 ret = gst_pad_peer_query_accept_caps (otherpad, caps);
373 if (!ret && self->converter_caps) {
374 /* maybe we can convert */
375 ret = gst_caps_can_intersect (caps, self->converter_caps);
380 GST_PLAY_SINK_CONVERT_BIN_UNLOCK (self);
382 gst_object_unref (self);
384 GST_DEBUG_OBJECT (pad, "Accept caps: '%" GST_PTR_FORMAT "' %d", caps, ret);
390 gst_play_sink_convert_bin_getcaps (GstPad * pad, GstCaps * filter)
392 GstPlaySinkConvertBin *self =
393 GST_PLAY_SINK_CONVERT_BIN (gst_pad_get_parent (pad));
395 GstPad *otherpad, *peer;
397 GST_PLAY_SINK_CONVERT_BIN_LOCK (self);
398 if (pad == self->srcpad) {
399 otherpad = self->sinkpad;
400 } else if (pad == self->sinkpad) {
401 otherpad = self->srcpad;
403 GST_ERROR_OBJECT (pad, "Not one of our pads");
408 peer = gst_pad_get_peer (otherpad);
411 GstCaps *downstream_filter = NULL;
413 /* Add all the caps that we can convert to to the filter caps,
414 * otherwise downstream might just return EMPTY caps because
415 * it doesn't handle the filter caps but we could still convert
420 downstream_filter = gst_caps_new_empty ();
422 /* Intersect raw video caps in the filter caps with the converter
423 * caps. This makes sure that we don't accept raw video that we
424 * can't handle, e.g. because of caps features */
425 n = gst_caps_get_size (filter);
426 for (i = 0; i < n; i++) {
430 s = gst_structure_copy (gst_caps_get_structure (filter, i));
431 if (gst_structure_has_name (s,
432 self->audio ? "audio/x-raw" : "video/x-raw")) {
433 tmp = gst_caps_new_full (s, NULL);
434 tmp2 = gst_caps_intersect (tmp, self->converter_caps);
435 gst_caps_append (downstream_filter, tmp2);
436 gst_caps_unref (tmp);
438 gst_caps_append_structure (downstream_filter, s);
442 gst_caps_merge (downstream_filter,
443 gst_caps_ref (self->converter_caps));
446 peer_caps = gst_pad_query_caps (peer, downstream_filter);
447 if (downstream_filter)
448 gst_caps_unref (downstream_filter);
449 gst_object_unref (peer);
450 if (self->converter_caps && is_raw_caps (peer_caps, self->audio)) {
451 GstCaps *converter_caps = gst_caps_ref (self->converter_caps);
456 ret = gst_caps_make_writable (peer_caps);
458 /* Filter out ANY capsfeatures from the converter caps. We can't
459 * convert to ANY capsfeatures, they are only there so that we
460 * can passthrough whatever downstream can support... but we
461 * definitely don't want to return them here
463 n = gst_caps_get_size (converter_caps);
464 for (i = 0; i < n; i++) {
465 s = gst_caps_get_structure (converter_caps, i);
466 cf = gst_caps_get_features (converter_caps, i);
468 if (cf && gst_caps_features_is_any (cf))
471 gst_caps_merge_structure_full (ret, gst_structure_copy (s),
472 (cf ? gst_caps_features_copy (cf) : NULL));
474 gst_caps_unref (converter_caps);
479 ret = gst_caps_ref (self->converter_caps);
481 GST_PLAY_SINK_CONVERT_BIN_FILTER_CAPS (filter, ret);
484 ret = filter ? gst_caps_ref (filter) : gst_caps_new_any ();
486 GST_PLAY_SINK_CONVERT_BIN_UNLOCK (self);
488 gst_object_unref (self);
490 GST_DEBUG_OBJECT (pad, "Returning caps %" GST_PTR_FORMAT, ret);
496 gst_play_sink_convert_bin_query (GstPad * pad, GstObject * parent,
499 gboolean res = FALSE;
501 switch (GST_QUERY_TYPE (query)) {
504 GstCaps *filter, *caps;
506 gst_query_parse_caps (query, &filter);
507 caps = gst_play_sink_convert_bin_getcaps (pad, filter);
508 gst_query_set_caps_result (query, caps);
509 gst_caps_unref (caps);
513 case GST_QUERY_ACCEPT_CAPS:
518 gst_query_parse_accept_caps (query, &caps);
519 ret = gst_play_sink_convert_bin_acceptcaps (pad, caps);
520 gst_query_set_accept_caps_result (query, ret);
525 res = gst_pad_query_default (pad, parent, query);
532 gst_play_sink_convert_bin_remove_elements (GstPlaySinkConvertBin * self)
534 if (self->conversion_elements) {
535 g_list_foreach (self->conversion_elements,
536 (GFunc) gst_play_sink_convert_bin_remove_element, self);
537 g_list_free (self->conversion_elements);
538 self->conversion_elements = NULL;
540 if (self->converter_caps) {
541 gst_caps_unref (self->converter_caps);
542 self->converter_caps = NULL;
547 gst_play_sink_convert_bin_dispose (GObject * object)
549 GstPlaySinkConvertBin *self = GST_PLAY_SINK_CONVERT_BIN_CAST (object);
551 gst_play_sink_convert_bin_remove_elements (self);
553 G_OBJECT_CLASS (parent_class)->dispose (object);
557 gst_play_sink_convert_bin_finalize (GObject * object)
559 GstPlaySinkConvertBin *self = GST_PLAY_SINK_CONVERT_BIN_CAST (object);
561 gst_object_unref (self->sink_proxypad);
562 g_mutex_clear (&self->lock);
564 G_OBJECT_CLASS (parent_class)->finalize (object);
568 gst_play_sink_convert_bin_cache_converter_caps (GstPlaySinkConvertBin * self)
573 if (self->converter_caps) {
574 gst_caps_unref (self->converter_caps);
575 self->converter_caps = NULL;
578 if (!self->conversion_elements) {
579 GST_INFO_OBJECT (self, "No conversion elements");
583 head = GST_ELEMENT (g_list_first (self->conversion_elements)->data);
584 pad = gst_element_get_static_pad (head, "sink");
586 GST_WARNING_OBJECT (self, "No sink pad found");
590 self->converter_caps = gst_pad_query_caps (pad, NULL);
591 GST_INFO_OBJECT (self, "Converter caps: %" GST_PTR_FORMAT,
592 self->converter_caps);
594 gst_object_unref (pad);
597 static GstStateChangeReturn
598 gst_play_sink_convert_bin_change_state (GstElement * element,
599 GstStateChange transition)
601 GstStateChangeReturn ret;
602 GstPlaySinkConvertBin *self = GST_PLAY_SINK_CONVERT_BIN_CAST (element);
604 switch (transition) {
605 case GST_STATE_CHANGE_PAUSED_TO_READY:
606 GST_PLAY_SINK_CONVERT_BIN_LOCK (self);
607 unblock_proxypad (self);
608 GST_PLAY_SINK_CONVERT_BIN_UNLOCK (self);
610 case GST_STATE_CHANGE_READY_TO_PAUSED:
611 GST_PLAY_SINK_CONVERT_BIN_LOCK (self);
612 gst_play_sink_convert_bin_set_targets (self, TRUE);
614 GST_PLAY_SINK_CONVERT_BIN_UNLOCK (self);
620 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
621 if (ret == GST_STATE_CHANGE_FAILURE)
624 switch (transition) {
625 case GST_STATE_CHANGE_PAUSED_TO_READY:
626 GST_PLAY_SINK_CONVERT_BIN_LOCK (self);
627 gst_play_sink_convert_bin_set_targets (self, TRUE);
629 GST_PLAY_SINK_CONVERT_BIN_UNLOCK (self);
631 case GST_STATE_CHANGE_READY_TO_PAUSED:
632 GST_PLAY_SINK_CONVERT_BIN_LOCK (self);
633 unblock_proxypad (self);
634 GST_PLAY_SINK_CONVERT_BIN_UNLOCK (self);
644 gst_play_sink_convert_bin_class_init (GstPlaySinkConvertBinClass * klass)
646 GObjectClass *gobject_class;
647 GstElementClass *gstelement_class;
649 GST_DEBUG_CATEGORY_INIT (gst_play_sink_convert_bin_debug,
650 "playsinkconvertbin", 0, "play bin");
652 gobject_class = (GObjectClass *) klass;
653 gstelement_class = (GstElementClass *) klass;
655 gobject_class->dispose = gst_play_sink_convert_bin_dispose;
656 gobject_class->finalize = gst_play_sink_convert_bin_finalize;
658 gst_element_class_add_static_pad_template (gstelement_class, &srctemplate);
659 gst_element_class_add_static_pad_template (gstelement_class, &sinktemplate);
660 gst_element_class_set_static_metadata (gstelement_class,
661 "Player Sink Converter Bin", "Bin/Converter",
662 "Convenience bin for audio/video conversion",
663 "Sebastian Dröge <sebastian.droege@collabora.co.uk>");
665 gstelement_class->change_state =
666 GST_DEBUG_FUNCPTR (gst_play_sink_convert_bin_change_state);
670 gst_play_sink_convert_bin_init (GstPlaySinkConvertBin * self)
672 GstPadTemplate *templ;
674 g_mutex_init (&self->lock);
676 templ = gst_static_pad_template_get (&sinktemplate);
677 self->sinkpad = gst_ghost_pad_new_no_target_from_template ("sink", templ);
678 gst_pad_set_event_function (self->sinkpad,
679 GST_DEBUG_FUNCPTR (gst_play_sink_convert_bin_sink_event));
680 gst_pad_set_query_function (self->sinkpad,
681 GST_DEBUG_FUNCPTR (gst_play_sink_convert_bin_query));
683 self->sink_proxypad =
684 GST_PAD_CAST (gst_proxy_pad_get_internal (GST_PROXY_PAD (self->sinkpad)));
686 gst_element_add_pad (GST_ELEMENT_CAST (self), self->sinkpad);
687 gst_object_unref (templ);
689 templ = gst_static_pad_template_get (&srctemplate);
690 self->srcpad = gst_ghost_pad_new_no_target_from_template ("src", templ);
691 gst_pad_set_query_function (self->srcpad,
692 GST_DEBUG_FUNCPTR (gst_play_sink_convert_bin_query));
693 gst_element_add_pad (GST_ELEMENT_CAST (self), self->srcpad);
694 gst_object_unref (templ);
696 gst_play_sink_convert_bin_add_identity (self);