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>
29 #include "gst/glib-compat-private.h"
31 GST_DEBUG_CATEGORY_STATIC (gst_play_sink_convert_bin_debug);
32 #define GST_CAT_DEFAULT gst_play_sink_convert_bin_debug
34 #define parent_class gst_play_sink_convert_bin_parent_class
36 G_DEFINE_TYPE (GstPlaySinkConvertBin, gst_play_sink_convert_bin, GST_TYPE_BIN);
38 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
43 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
49 is_raw_caps (GstCaps * caps, gboolean audio)
54 const gchar *prefix = audio ? "audio/x-raw-" : "video/x-raw-";
56 n = gst_caps_get_size (caps);
57 for (i = 0; i < n; i++) {
58 s = gst_caps_get_structure (caps, i);
59 name = gst_structure_get_name (s);
60 if (!g_str_has_prefix (name, prefix))
68 gst_play_sink_convert_bin_post_missing_element_message (GstPlaySinkConvertBin *
69 self, const gchar * name)
73 msg = gst_missing_element_message_new (GST_ELEMENT_CAST (self), name);
74 gst_element_post_message (GST_ELEMENT_CAST (self), msg);
78 distribute_running_time (GstElement * element, const GstSegment * segment)
83 pad = gst_element_get_static_pad (element, "sink");
85 gst_pad_send_event (pad, gst_event_new_flush_start ());
86 gst_pad_send_event (pad, gst_event_new_flush_stop ());
88 if (segment->accum && segment->format != GST_FORMAT_UNDEFINED) {
89 event = gst_event_new_new_segment_full (FALSE, segment->rate,
90 segment->applied_rate, segment->format, 0, segment->accum, 0);
91 gst_pad_send_event (pad, event);
94 if (segment->format != GST_FORMAT_UNDEFINED) {
95 event = gst_event_new_new_segment_full (FALSE, segment->rate,
96 segment->applied_rate, segment->format,
97 segment->start, segment->stop, segment->time);
98 gst_pad_send_event (pad, event);
101 gst_object_unref (pad);
105 gst_play_sink_convert_bin_add_conversion_element (GstPlaySinkConvertBin * self,
108 self->conversion_elements = g_list_append (self->conversion_elements, el);
109 gst_bin_add (GST_BIN (self), gst_object_ref (el));
113 gst_play_sink_convert_bin_add_conversion_element_factory (GstPlaySinkConvertBin
114 * self, const char *factory, const char *name)
118 el = gst_element_factory_make (factory, name);
120 gst_play_sink_convert_bin_post_missing_element_message (self, factory);
121 GST_ELEMENT_WARNING (self, CORE, MISSING_PLUGIN,
122 (_("Missing element '%s' - check your GStreamer installation."),
124 (self->audio ? "audio rendering might fail" :
125 "video rendering might fail"));
127 gst_play_sink_convert_bin_add_conversion_element (self, el);
133 gst_play_sink_convert_bin_add_identity (GstPlaySinkConvertBin * self)
138 self->identity = gst_element_factory_make ("identity", "identity");
139 if (self->identity == NULL) {
140 gst_play_sink_convert_bin_post_missing_element_message (self, "identity");
141 GST_ELEMENT_WARNING (self, CORE, MISSING_PLUGIN,
142 (_("Missing element '%s' - check your GStreamer installation."),
143 "identity"), (self->audio ?
144 "audio rendering might fail" : "video rendering might fail")
148 g_object_set (self->identity, "silent", TRUE, "signal-handoffs", FALSE,
150 gst_bin_add (GST_BIN_CAST (self), self->identity);
155 gst_play_sink_convert_bin_set_targets (GstPlaySinkConvertBin * self,
156 gboolean passthrough)
159 GstElement *head, *tail;
161 GST_DEBUG_OBJECT (self, "Setting pad targets with passthrough %d",
163 if (self->conversion_elements == NULL || passthrough) {
164 GST_DEBUG_OBJECT (self, "no conversion elements, using identity (%p) as "
165 "head/tail", self->identity);
167 GST_WARNING_OBJECT (self,
168 "Doing passthrough as no converter elements were added");
170 head = tail = self->identity;
172 head = GST_ELEMENT (g_list_first (self->conversion_elements)->data);
173 tail = GST_ELEMENT (g_list_last (self->conversion_elements)->data);
174 GST_DEBUG_OBJECT (self, "conversion elements in use, picking "
175 "head:%s and tail:%s", GST_OBJECT_NAME (head), GST_OBJECT_NAME (tail));
178 g_return_if_fail (head != NULL);
179 g_return_if_fail (tail != NULL);
181 pad = gst_element_get_static_pad (head, "sink");
182 GST_DEBUG_OBJECT (self, "Ghosting bin sink pad to %" GST_PTR_FORMAT, pad);
183 gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (self->sinkpad), pad);
184 gst_object_unref (pad);
186 pad = gst_element_get_static_pad (tail, "src");
187 GST_DEBUG_OBJECT (self, "Ghosting bin src pad to %" GST_PTR_FORMAT, pad);
188 gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (self->srcpad), pad);
189 gst_object_unref (pad);
193 gst_play_sink_convert_bin_remove_element (GstElement * element,
194 GstPlaySinkConvertBin * self)
196 gst_element_set_state (element, GST_STATE_NULL);
197 gst_bin_remove (GST_BIN_CAST (self), element);
201 gst_play_sink_convert_bin_on_element_added (GstElement * element,
202 GstPlaySinkConvertBin * self)
204 gst_element_sync_state_with_parent (element);
205 distribute_running_time (element, &self->segment);
209 pad_blocked_cb (GstPad * pad, gboolean blocked, GstPlaySinkConvertBin * self)
215 GST_PLAY_SINK_CONVERT_BIN_LOCK (self);
216 self->sink_proxypad_blocked = blocked;
217 GST_DEBUG_OBJECT (self, "Pad blocked: %d", blocked);
221 /* There must be a peer at this point */
222 peer = gst_pad_get_peer (self->sinkpad);
223 caps = gst_pad_get_negotiated_caps (peer);
225 caps = gst_pad_get_caps_reffed (peer);
226 gst_object_unref (peer);
228 raw = is_raw_caps (caps, self->audio);
229 GST_DEBUG_OBJECT (self, "Caps %" GST_PTR_FORMAT " are raw: %d", caps, raw);
230 gst_caps_unref (caps);
232 if (raw == self->raw)
236 gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (self->sinkpad), NULL);
237 gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (self->srcpad), NULL);
240 GST_DEBUG_OBJECT (self, "Switching to raw conversion pipeline");
242 if (self->conversion_elements)
243 g_list_foreach (self->conversion_elements,
244 (GFunc) gst_play_sink_convert_bin_on_element_added, self);
247 GST_DEBUG_OBJECT (self, "Switch to passthrough pipeline");
249 gst_play_sink_convert_bin_on_element_added (self->identity, self);
252 gst_play_sink_convert_bin_set_targets (self, !raw);
255 gst_pad_set_blocked_async_full (self->sink_proxypad, FALSE,
256 (GstPadBlockCallback) pad_blocked_cb, gst_object_ref (self),
257 (GDestroyNotify) gst_object_unref);
260 GST_PLAY_SINK_CONVERT_BIN_UNLOCK (self);
265 gst_play_sink_convert_bin_sink_event (GstPad * pad, GstEvent * event)
267 GstPlaySinkConvertBin *self =
268 GST_PLAY_SINK_CONVERT_BIN (gst_pad_get_parent (pad));
271 ret = gst_proxy_pad_event_default (pad, gst_event_ref (event));
273 if (GST_EVENT_TYPE (event) == GST_EVENT_NEWSEGMENT) {
275 gdouble rate, applied_rate;
277 gint64 start, stop, position;
279 GST_PLAY_SINK_CONVERT_BIN_LOCK (self);
280 gst_event_parse_new_segment_full (event, &update, &rate, &applied_rate,
281 &format, &start, &stop, &position);
283 GST_DEBUG_OBJECT (self, "Segment before %" GST_SEGMENT_FORMAT,
285 gst_segment_set_newsegment_full (&self->segment, update, rate, applied_rate,
286 format, start, stop, position);
287 GST_DEBUG_OBJECT (self, "Segment after %" GST_SEGMENT_FORMAT,
289 GST_PLAY_SINK_CONVERT_BIN_UNLOCK (self);
290 } else if (GST_EVENT_TYPE (event) == GST_EVENT_FLUSH_STOP) {
291 GST_PLAY_SINK_CONVERT_BIN_LOCK (self);
292 GST_DEBUG_OBJECT (self, "Resetting segment");
293 gst_segment_init (&self->segment, GST_FORMAT_UNDEFINED);
294 GST_PLAY_SINK_CONVERT_BIN_UNLOCK (self);
297 gst_event_unref (event);
298 gst_object_unref (self);
304 gst_play_sink_convert_bin_sink_setcaps (GstPad * pad, GstCaps * caps)
306 GstPlaySinkConvertBin *self =
307 GST_PLAY_SINK_CONVERT_BIN (gst_pad_get_parent (pad));
311 gboolean reconfigure = FALSE;
314 GST_DEBUG_OBJECT (pad, "setcaps: %" GST_PTR_FORMAT, caps);
315 GST_PLAY_SINK_CONVERT_BIN_LOCK (self);
316 s = gst_caps_get_structure (caps, 0);
317 name = gst_structure_get_name (s);
320 raw = g_str_has_prefix (name, "audio/x-raw-");
322 raw = g_str_has_prefix (name, "video/x-raw-");
325 GST_DEBUG_OBJECT (self, "raw %d, self->raw %d, blocked %d",
326 raw, self->raw, gst_pad_is_blocked (self->sink_proxypad));
329 if (!gst_pad_is_blocked (self->sink_proxypad)) {
330 GstPad *target = gst_ghost_pad_get_target (GST_GHOST_PAD (self->sinkpad));
332 if (!self->raw || (target && !gst_pad_accept_caps (target, caps))) {
334 GST_DEBUG_OBJECT (self, "Changing caps from non-raw to raw");
336 GST_DEBUG_OBJECT (self, "Changing caps in an incompatible way");
339 gst_pad_set_blocked_async_full (self->sink_proxypad, TRUE,
340 (GstPadBlockCallback) pad_blocked_cb, gst_object_ref (self),
341 (GDestroyNotify) gst_object_unref);
345 gst_object_unref (target);
348 if (self->raw && !gst_pad_is_blocked (self->sink_proxypad)) {
349 GST_DEBUG_OBJECT (self, "Changing caps from raw to non-raw");
351 gst_pad_set_blocked_async_full (self->sink_proxypad, TRUE,
352 (GstPadBlockCallback) pad_blocked_cb, gst_object_ref (self),
353 (GDestroyNotify) gst_object_unref);
357 /* Otherwise the setcaps below fails */
359 gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (self->sinkpad), NULL);
360 gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (self->srcpad), NULL);
363 GST_PLAY_SINK_CONVERT_BIN_UNLOCK (self);
364 ret = gst_ghost_pad_setcaps_default (pad, caps);
366 GST_DEBUG_OBJECT (self, "Setting sink caps %" GST_PTR_FORMAT ": %d", caps,
369 gst_object_unref (self);
375 gst_play_sink_convert_bin_getcaps (GstPad * pad)
377 GstPlaySinkConvertBin *self =
378 GST_PLAY_SINK_CONVERT_BIN (gst_pad_get_parent (pad));
380 GstPad *otherpad, *peer;
382 GST_PLAY_SINK_CONVERT_BIN_LOCK (self);
383 if (pad == self->srcpad) {
384 otherpad = self->sinkpad;
385 } else if (pad == self->sinkpad) {
386 otherpad = self->srcpad;
388 GST_ERROR_OBJECT (pad, "Not one of our pads");
393 peer = gst_pad_get_peer (otherpad);
395 GstCaps *peer_caps = gst_pad_get_caps_reffed (peer);
396 gst_object_unref (peer);
397 if (self->converter_caps && is_raw_caps (peer_caps, self->audio)) {
398 peer_caps = gst_caps_make_writable (peer_caps);
399 gst_caps_merge (peer_caps, gst_caps_ref (self->converter_caps));
405 ret = gst_caps_ref (self->converter_caps);
408 ret = gst_caps_new_any ();
410 GST_PLAY_SINK_CONVERT_BIN_UNLOCK (self);
412 gst_object_unref (self);
414 GST_DEBUG_OBJECT (pad, "Returning caps %" GST_PTR_FORMAT, ret);
420 gst_play_sink_convert_bin_acceptcaps (GstPad * pad, GstCaps * caps)
422 GstCaps *allowed_caps;
425 allowed_caps = gst_pad_get_caps_reffed (pad);
426 /* FIXME 0.11: Should be a subset check now */
427 ret = gst_caps_can_intersect (caps, allowed_caps);
428 gst_caps_unref (allowed_caps);
434 gst_play_sink_convert_bin_remove_elements (GstPlaySinkConvertBin * self)
436 if (self->conversion_elements) {
437 g_list_foreach (self->conversion_elements,
438 (GFunc) gst_play_sink_convert_bin_remove_element, self);
439 g_list_free (self->conversion_elements);
440 self->conversion_elements = NULL;
442 if (self->converter_caps) {
443 gst_caps_unref (self->converter_caps);
444 self->converter_caps = NULL;
449 gst_play_sink_convert_bin_dispose (GObject * object)
451 GstPlaySinkConvertBin *self = GST_PLAY_SINK_CONVERT_BIN_CAST (object);
453 gst_play_sink_convert_bin_remove_elements (self);
455 G_OBJECT_CLASS (parent_class)->dispose (object);
459 gst_play_sink_convert_bin_finalize (GObject * object)
461 GstPlaySinkConvertBin *self = GST_PLAY_SINK_CONVERT_BIN_CAST (object);
463 gst_object_unref (self->sink_proxypad);
464 g_mutex_free (self->lock);
466 G_OBJECT_CLASS (parent_class)->finalize (object);
470 gst_play_sink_convert_bin_cache_converter_caps (GstPlaySinkConvertBin * self)
475 if (self->converter_caps) {
476 gst_caps_unref (self->converter_caps);
477 self->converter_caps = NULL;
480 if (!self->conversion_elements) {
481 GST_WARNING_OBJECT (self, "No conversion elements");
485 head = GST_ELEMENT (g_list_first (self->conversion_elements)->data);
486 pad = gst_element_get_static_pad (head, "sink");
488 GST_WARNING_OBJECT (self, "No sink pad found");
492 self->converter_caps = gst_pad_get_caps_reffed (pad);
493 GST_INFO_OBJECT (self, "Converter caps: %" GST_PTR_FORMAT,
494 self->converter_caps);
496 gst_object_unref (pad);
499 static GstStateChangeReturn
500 gst_play_sink_convert_bin_change_state (GstElement * element,
501 GstStateChange transition)
503 GstStateChangeReturn ret;
504 GstPlaySinkConvertBin *self = GST_PLAY_SINK_CONVERT_BIN_CAST (element);
506 switch (transition) {
507 case GST_STATE_CHANGE_PAUSED_TO_READY:
508 GST_PLAY_SINK_CONVERT_BIN_LOCK (self);
509 if (gst_pad_is_blocked (self->sink_proxypad))
510 gst_pad_set_blocked_async_full (self->sink_proxypad, FALSE,
511 (GstPadBlockCallback) pad_blocked_cb, gst_object_ref (self),
512 (GDestroyNotify) gst_object_unref);
513 GST_PLAY_SINK_CONVERT_BIN_UNLOCK (self);
515 case GST_STATE_CHANGE_READY_TO_PAUSED:
516 GST_PLAY_SINK_CONVERT_BIN_LOCK (self);
517 gst_segment_init (&self->segment, GST_FORMAT_UNDEFINED);
518 gst_play_sink_convert_bin_set_targets (self, TRUE);
520 GST_PLAY_SINK_CONVERT_BIN_UNLOCK (self);
526 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
527 if (ret == GST_STATE_CHANGE_FAILURE)
530 switch (transition) {
531 case GST_STATE_CHANGE_PAUSED_TO_READY:
532 GST_PLAY_SINK_CONVERT_BIN_LOCK (self);
533 gst_segment_init (&self->segment, GST_FORMAT_UNDEFINED);
534 gst_play_sink_convert_bin_set_targets (self, TRUE);
536 GST_PLAY_SINK_CONVERT_BIN_UNLOCK (self);
538 case GST_STATE_CHANGE_READY_TO_PAUSED:
539 GST_PLAY_SINK_CONVERT_BIN_LOCK (self);
540 if (!gst_pad_is_blocked (self->sink_proxypad))
541 gst_pad_set_blocked_async_full (self->sink_proxypad, TRUE,
542 (GstPadBlockCallback) pad_blocked_cb, gst_object_ref (self),
543 (GDestroyNotify) gst_object_unref);
544 GST_PLAY_SINK_CONVERT_BIN_UNLOCK (self);
554 gst_play_sink_convert_bin_class_init (GstPlaySinkConvertBinClass * klass)
556 GObjectClass *gobject_class;
557 GstElementClass *gstelement_class;
559 GST_DEBUG_CATEGORY_INIT (gst_play_sink_convert_bin_debug,
560 "playsinkconvertbin", 0, "play bin");
562 gobject_class = (GObjectClass *) klass;
563 gstelement_class = (GstElementClass *) klass;
565 gobject_class->dispose = gst_play_sink_convert_bin_dispose;
566 gobject_class->finalize = gst_play_sink_convert_bin_finalize;
568 gst_element_class_add_pad_template (gstelement_class,
569 gst_static_pad_template_get (&srctemplate));
570 gst_element_class_add_pad_template (gstelement_class,
571 gst_static_pad_template_get (&sinktemplate));
572 gst_element_class_set_details_simple (gstelement_class,
573 "Player Sink Converter Bin", "Bin/Converter",
574 "Convenience bin for audio/video conversion",
575 "Sebastian Dröge <sebastian.droege@collabora.co.uk>");
577 gstelement_class->change_state =
578 GST_DEBUG_FUNCPTR (gst_play_sink_convert_bin_change_state);
582 gst_play_sink_convert_bin_init (GstPlaySinkConvertBin * self)
584 GstPadTemplate *templ;
586 self->lock = g_mutex_new ();
587 gst_segment_init (&self->segment, GST_FORMAT_UNDEFINED);
589 templ = gst_static_pad_template_get (&sinktemplate);
590 self->sinkpad = gst_ghost_pad_new_no_target_from_template ("sink", templ);
591 gst_pad_set_event_function (self->sinkpad,
592 GST_DEBUG_FUNCPTR (gst_play_sink_convert_bin_sink_event));
593 gst_pad_set_setcaps_function (self->sinkpad,
594 GST_DEBUG_FUNCPTR (gst_play_sink_convert_bin_sink_setcaps));
595 gst_pad_set_getcaps_function (self->sinkpad,
596 GST_DEBUG_FUNCPTR (gst_play_sink_convert_bin_getcaps));
597 gst_pad_set_acceptcaps_function (self->sinkpad,
598 GST_DEBUG_FUNCPTR (gst_play_sink_convert_bin_acceptcaps));
600 self->sink_proxypad =
601 GST_PAD_CAST (gst_proxy_pad_get_internal (GST_PROXY_PAD (self->sinkpad)));
603 gst_element_add_pad (GST_ELEMENT_CAST (self), self->sinkpad);
604 gst_object_unref (templ);
606 templ = gst_static_pad_template_get (&srctemplate);
607 self->srcpad = gst_ghost_pad_new_no_target_from_template ("src", templ);
608 gst_pad_set_getcaps_function (self->srcpad,
609 GST_DEBUG_FUNCPTR (gst_play_sink_convert_bin_getcaps));
610 gst_pad_set_acceptcaps_function (self->srcpad,
611 GST_DEBUG_FUNCPTR (gst_play_sink_convert_bin_acceptcaps));
612 gst_element_add_pad (GST_ELEMENT_CAST (self), self->srcpad);
613 gst_object_unref (templ);
615 gst_play_sink_convert_bin_add_identity (self);