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 if (segment->format != GST_FORMAT_UNDEFINED) {
91 event = gst_event_new_segment (segment);
92 gst_pad_send_event (pad, event);
95 gst_object_unref (pad);
99 gst_play_sink_convert_bin_add_conversion_element (GstPlaySinkConvertBin * self,
102 self->conversion_elements = g_list_append (self->conversion_elements, el);
103 gst_bin_add (GST_BIN (self), gst_object_ref (el));
107 gst_play_sink_convert_bin_add_conversion_element_factory (GstPlaySinkConvertBin
108 * self, const char *factory, const char *name)
112 el = gst_element_factory_make (factory, name);
114 gst_play_sink_convert_bin_post_missing_element_message (self, factory);
115 GST_ELEMENT_WARNING (self, CORE, MISSING_PLUGIN,
116 (_("Missing element '%s' - check your GStreamer installation."),
118 (self->audio ? "audio rendering might fail" :
119 "video rendering might fail"));
121 gst_play_sink_convert_bin_add_conversion_element (self, el);
127 gst_play_sink_convert_bin_add_identity (GstPlaySinkConvertBin * self)
132 self->identity = gst_element_factory_make ("identity", "identity");
133 if (self->identity == NULL) {
134 gst_play_sink_convert_bin_post_missing_element_message (self, "identity");
135 GST_ELEMENT_WARNING (self, CORE, MISSING_PLUGIN,
136 (_("Missing element '%s' - check your GStreamer installation."),
137 "identity"), (self->audio ?
138 "audio rendering might fail" : "video rendering might fail")
142 g_object_set (self->identity, "silent", TRUE, "signal-handoffs", FALSE,
144 gst_bin_add (GST_BIN_CAST (self), self->identity);
149 gst_play_sink_convert_bin_set_targets (GstPlaySinkConvertBin * self,
150 gboolean passthrough)
153 GstElement *head, *tail;
155 GST_DEBUG_OBJECT (self, "Setting pad targets with passthrough %d",
157 if (self->conversion_elements == NULL || passthrough) {
158 GST_DEBUG_OBJECT (self, "no conversion elements, using identity (%p) as "
159 "head/tail", self->identity);
161 GST_WARNING_OBJECT (self,
162 "Doing passthrough as no converter elements were added");
164 head = tail = self->identity;
166 head = GST_ELEMENT (g_list_first (self->conversion_elements)->data);
167 tail = GST_ELEMENT (g_list_last (self->conversion_elements)->data);
168 GST_DEBUG_OBJECT (self, "conversion elements in use, picking "
169 "head:%s and tail:%s", GST_OBJECT_NAME (head), GST_OBJECT_NAME (tail));
172 g_return_if_fail (head != NULL);
173 g_return_if_fail (tail != NULL);
175 pad = gst_element_get_static_pad (head, "sink");
176 GST_DEBUG_OBJECT (self, "Ghosting bin sink pad to %" GST_PTR_FORMAT, pad);
177 gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (self->sinkpad), pad);
178 gst_object_unref (pad);
180 pad = gst_element_get_static_pad (tail, "src");
181 GST_DEBUG_OBJECT (self, "Ghosting bin src pad to %" GST_PTR_FORMAT, pad);
182 gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (self->srcpad), pad);
183 gst_object_unref (pad);
187 gst_play_sink_convert_bin_remove_element (GstElement * element,
188 GstPlaySinkConvertBin * self)
190 gst_element_set_state (element, GST_STATE_NULL);
191 gst_bin_remove (GST_BIN_CAST (self), element);
195 gst_play_sink_convert_bin_on_element_added (GstElement * element,
196 GstPlaySinkConvertBin * self)
198 gst_element_sync_state_with_parent (element);
199 distribute_running_time (element, &self->segment);
202 static GstPadProbeReturn
203 pad_blocked_cb (GstPad * pad, GstPadProbeInfo * info, gpointer user_data)
205 GstPlaySinkConvertBin *self = user_data;
210 GST_PLAY_SINK_CONVERT_BIN_LOCK (self);
211 GST_DEBUG_OBJECT (self, "Pad blocked");
213 /* There must be a peer at this point */
214 peer = gst_pad_get_peer (self->sinkpad);
215 caps = gst_pad_get_current_caps (peer);
217 caps = gst_pad_query_caps (peer, NULL);
218 gst_object_unref (peer);
220 raw = is_raw_caps (caps, self->audio);
221 GST_DEBUG_OBJECT (self, "Caps %" GST_PTR_FORMAT " are raw: %d", caps, raw);
222 gst_caps_unref (caps);
224 if (raw == self->raw)
228 gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (self->sinkpad), NULL);
229 gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (self->srcpad), NULL);
232 GST_DEBUG_OBJECT (self, "Switching to raw conversion pipeline");
234 if (self->conversion_elements)
235 g_list_foreach (self->conversion_elements,
236 (GFunc) gst_play_sink_convert_bin_on_element_added, self);
239 GST_DEBUG_OBJECT (self, "Switch to passthrough pipeline");
241 gst_play_sink_convert_bin_on_element_added (self->identity, self);
244 gst_play_sink_convert_bin_set_targets (self, !raw);
247 self->sink_proxypad_block_id = 0;
248 GST_PLAY_SINK_CONVERT_BIN_UNLOCK (self);
250 return GST_PAD_PROBE_REMOVE;
254 gst_play_sink_convert_bin_sink_event (GstPad * pad, GstObject * parent,
257 GstPlaySinkConvertBin *self = GST_PLAY_SINK_CONVERT_BIN (parent);
260 switch (GST_EVENT_TYPE (event)) {
265 gst_event_parse_caps (event, &caps);
266 ret = gst_play_sink_convert_bin_sink_setcaps (self, caps);
273 ret = gst_proxy_pad_event_default (pad, parent, gst_event_ref (event));
275 if (GST_EVENT_TYPE (event) == GST_EVENT_SEGMENT) {
278 GST_PLAY_SINK_CONVERT_BIN_LOCK (self);
279 gst_event_copy_segment (event, &seg);
281 GST_DEBUG_OBJECT (self, "Segment before %" GST_SEGMENT_FORMAT,
284 GST_DEBUG_OBJECT (self, "Segment after %" GST_SEGMENT_FORMAT,
286 GST_PLAY_SINK_CONVERT_BIN_UNLOCK (self);
287 } else if (GST_EVENT_TYPE (event) == GST_EVENT_FLUSH_STOP) {
288 GST_PLAY_SINK_CONVERT_BIN_LOCK (self);
289 GST_DEBUG_OBJECT (self, "Resetting segment");
290 gst_segment_init (&self->segment, GST_FORMAT_UNDEFINED);
291 GST_PLAY_SINK_CONVERT_BIN_UNLOCK (self);
294 gst_event_unref (event);
300 block_proxypad (GstPlaySinkConvertBin * self)
302 if (self->sink_proxypad_block_id == 0) {
303 self->sink_proxypad_block_id =
304 gst_pad_add_probe (self->sink_proxypad,
305 GST_PAD_PROBE_TYPE_BLOCK_DOWNSTREAM, pad_blocked_cb,
306 gst_object_ref (self), (GDestroyNotify) gst_object_unref);
311 unblock_proxypad (GstPlaySinkConvertBin * self)
313 if (self->sink_proxypad_block_id != 0) {
314 gst_pad_remove_probe (self->sink_proxypad, self->sink_proxypad_block_id);
315 self->sink_proxypad_block_id = 0;
320 gst_play_sink_convert_bin_sink_setcaps (GstPlaySinkConvertBin * self,
325 gboolean reconfigure = FALSE;
328 GST_DEBUG_OBJECT (self, "setcaps");
329 GST_PLAY_SINK_CONVERT_BIN_LOCK (self);
330 s = gst_caps_get_structure (caps, 0);
331 name = gst_structure_get_name (s);
334 raw = g_str_has_prefix (name, "audio/x-raw-");
336 raw = g_str_has_prefix (name, "video/x-raw-");
339 GST_DEBUG_OBJECT (self, "raw %d, self->raw %d, blocked %d",
340 raw, self->raw, gst_pad_is_blocked (self->sink_proxypad));
343 if (!gst_pad_is_blocked (self->sink_proxypad)) {
344 GstPad *target = gst_ghost_pad_get_target (GST_GHOST_PAD (self->sinkpad));
346 if (!self->raw || (target && !gst_pad_query_accept_caps (target, caps))) {
348 GST_DEBUG_OBJECT (self, "Changing caps from non-raw to raw");
350 GST_DEBUG_OBJECT (self, "Changing caps in an incompatible way");
353 block_proxypad (self);
357 gst_object_unref (target);
360 if (self->raw && !gst_pad_is_blocked (self->sink_proxypad)) {
361 GST_DEBUG_OBJECT (self, "Changing caps from raw to non-raw");
363 block_proxypad (self);
367 /* Otherwise the setcaps below fails */
369 gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (self->sinkpad), NULL);
370 gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (self->srcpad), NULL);
373 GST_PLAY_SINK_CONVERT_BIN_UNLOCK (self);
375 GST_DEBUG_OBJECT (self, "Setting sink caps %" GST_PTR_FORMAT, caps);
381 gst_play_sink_convert_bin_getcaps (GstPad * pad, GstCaps * filter)
383 GstPlaySinkConvertBin *self =
384 GST_PLAY_SINK_CONVERT_BIN (gst_pad_get_parent (pad));
386 GstPad *otherpad, *peer;
388 GST_PLAY_SINK_CONVERT_BIN_LOCK (self);
389 if (pad == self->srcpad) {
390 otherpad = self->sinkpad;
391 } else if (pad == self->sinkpad) {
392 otherpad = self->srcpad;
394 GST_ERROR_OBJECT (pad, "Not one of our pads");
399 peer = gst_pad_get_peer (otherpad);
401 GstCaps *peer_caps = gst_pad_query_caps (peer, filter);
402 gst_object_unref (peer);
403 if (self->converter_caps && is_raw_caps (peer_caps, self->audio)) {
404 peer_caps = gst_caps_make_writable (peer_caps);
405 gst_caps_merge (peer_caps, gst_caps_ref (self->converter_caps));
411 ret = gst_caps_ref (self->converter_caps);
414 ret = gst_caps_new_any ();
416 GST_PLAY_SINK_CONVERT_BIN_UNLOCK (self);
418 gst_object_unref (self);
420 GST_DEBUG_OBJECT (pad, "Returning caps %" GST_PTR_FORMAT, ret);
426 gst_play_sink_convert_bin_acceptcaps (GstPad * pad, GstCaps * caps)
428 GstCaps *allowed_caps;
431 allowed_caps = gst_pad_query_caps (pad, NULL);
432 /* FIXME 0.11: Should be a subset check now */
433 ret = gst_caps_can_intersect (caps, allowed_caps);
434 gst_caps_unref (allowed_caps);
440 gst_play_sink_convert_bin_query (GstPad * pad, GstObject * parent,
443 gboolean res = FALSE;
445 switch (GST_QUERY_TYPE (query)) {
446 case GST_QUERY_ACCEPT_CAPS:
450 gst_query_parse_accept_caps (query, &caps);
451 gst_query_set_accept_caps_result (query,
452 gst_play_sink_convert_bin_acceptcaps (pad, caps));
458 GstCaps *filter, *caps;
460 gst_query_parse_caps (query, &filter);
461 caps = gst_play_sink_convert_bin_getcaps (pad, filter);
462 gst_query_set_caps_result (query, caps);
463 gst_caps_unref (caps);
468 res = gst_pad_query_default (pad, parent, query);
475 gst_play_sink_convert_bin_remove_elements (GstPlaySinkConvertBin * self)
477 if (self->conversion_elements) {
478 g_list_foreach (self->conversion_elements,
479 (GFunc) gst_play_sink_convert_bin_remove_element, self);
480 g_list_free (self->conversion_elements);
481 self->conversion_elements = NULL;
483 if (self->converter_caps) {
484 gst_caps_unref (self->converter_caps);
485 self->converter_caps = NULL;
490 gst_play_sink_convert_bin_dispose (GObject * object)
492 GstPlaySinkConvertBin *self = GST_PLAY_SINK_CONVERT_BIN_CAST (object);
494 gst_play_sink_convert_bin_remove_elements (self);
496 G_OBJECT_CLASS (parent_class)->dispose (object);
500 gst_play_sink_convert_bin_finalize (GObject * object)
502 GstPlaySinkConvertBin *self = GST_PLAY_SINK_CONVERT_BIN_CAST (object);
504 gst_object_unref (self->sink_proxypad);
505 g_mutex_free (self->lock);
507 G_OBJECT_CLASS (parent_class)->finalize (object);
511 gst_play_sink_convert_bin_cache_converter_caps (GstPlaySinkConvertBin * self)
516 if (self->converter_caps) {
517 gst_caps_unref (self->converter_caps);
518 self->converter_caps = NULL;
521 if (!self->conversion_elements) {
522 GST_WARNING_OBJECT (self, "No conversion elements");
526 head = GST_ELEMENT (g_list_first (self->conversion_elements)->data);
527 pad = gst_element_get_static_pad (head, "sink");
529 GST_WARNING_OBJECT (self, "No sink pad found");
533 self->converter_caps = gst_pad_query_caps (pad, NULL);
534 GST_INFO_OBJECT (self, "Converter caps: %" GST_PTR_FORMAT,
535 self->converter_caps);
537 gst_object_unref (pad);
540 static GstStateChangeReturn
541 gst_play_sink_convert_bin_change_state (GstElement * element,
542 GstStateChange transition)
544 GstStateChangeReturn ret;
545 GstPlaySinkConvertBin *self = GST_PLAY_SINK_CONVERT_BIN_CAST (element);
547 switch (transition) {
548 case GST_STATE_CHANGE_PAUSED_TO_READY:
549 GST_PLAY_SINK_CONVERT_BIN_LOCK (self);
550 unblock_proxypad (self);
551 GST_PLAY_SINK_CONVERT_BIN_UNLOCK (self);
553 case GST_STATE_CHANGE_READY_TO_PAUSED:
554 GST_PLAY_SINK_CONVERT_BIN_LOCK (self);
555 gst_segment_init (&self->segment, GST_FORMAT_UNDEFINED);
556 gst_play_sink_convert_bin_set_targets (self, TRUE);
558 GST_PLAY_SINK_CONVERT_BIN_UNLOCK (self);
564 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
565 if (ret == GST_STATE_CHANGE_FAILURE)
568 switch (transition) {
569 case GST_STATE_CHANGE_PAUSED_TO_READY:
570 GST_PLAY_SINK_CONVERT_BIN_LOCK (self);
571 gst_segment_init (&self->segment, GST_FORMAT_UNDEFINED);
572 gst_play_sink_convert_bin_set_targets (self, TRUE);
574 GST_PLAY_SINK_CONVERT_BIN_UNLOCK (self);
576 case GST_STATE_CHANGE_READY_TO_PAUSED:
577 GST_PLAY_SINK_CONVERT_BIN_LOCK (self);
578 unblock_proxypad (self);
579 GST_PLAY_SINK_CONVERT_BIN_UNLOCK (self);
589 gst_play_sink_convert_bin_class_init (GstPlaySinkConvertBinClass * klass)
591 GObjectClass *gobject_class;
592 GstElementClass *gstelement_class;
594 GST_DEBUG_CATEGORY_INIT (gst_play_sink_convert_bin_debug,
595 "playsinkconvertbin", 0, "play bin");
597 gobject_class = (GObjectClass *) klass;
598 gstelement_class = (GstElementClass *) klass;
600 gobject_class->dispose = gst_play_sink_convert_bin_dispose;
601 gobject_class->finalize = gst_play_sink_convert_bin_finalize;
603 gst_element_class_add_pad_template (gstelement_class,
604 gst_static_pad_template_get (&srctemplate));
605 gst_element_class_add_pad_template (gstelement_class,
606 gst_static_pad_template_get (&sinktemplate));
607 gst_element_class_set_details_simple (gstelement_class,
608 "Player Sink Converter Bin", "Bin/Converter",
609 "Convenience bin for audio/video conversion",
610 "Sebastian Dröge <sebastian.droege@collabora.co.uk>");
612 gstelement_class->change_state =
613 GST_DEBUG_FUNCPTR (gst_play_sink_convert_bin_change_state);
617 gst_play_sink_convert_bin_init (GstPlaySinkConvertBin * self)
619 GstPadTemplate *templ;
621 self->lock = g_mutex_new ();
622 gst_segment_init (&self->segment, GST_FORMAT_UNDEFINED);
624 templ = gst_static_pad_template_get (&sinktemplate);
625 self->sinkpad = gst_ghost_pad_new_no_target_from_template ("sink", templ);
626 gst_pad_set_event_function (self->sinkpad,
627 GST_DEBUG_FUNCPTR (gst_play_sink_convert_bin_sink_event));
628 gst_pad_set_query_function (self->sinkpad,
629 GST_DEBUG_FUNCPTR (gst_play_sink_convert_bin_query));
631 self->sink_proxypad =
632 GST_PAD_CAST (gst_proxy_pad_get_internal (GST_PROXY_PAD (self->sinkpad)));
634 gst_element_add_pad (GST_ELEMENT_CAST (self), self->sinkpad);
635 gst_object_unref (templ);
637 templ = gst_static_pad_template_get (&srctemplate);
638 self->srcpad = gst_ghost_pad_new_no_target_from_template ("src", templ);
639 gst_pad_set_query_function (self->srcpad,
640 GST_DEBUG_FUNCPTR (gst_play_sink_convert_bin_query));
641 gst_element_add_pad (GST_ELEMENT_CAST (self), self->srcpad);
642 gst_object_unref (templ);
644 gst_play_sink_convert_bin_add_identity (self);