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, NULL);
141 gst_bin_add (GST_BIN_CAST (self), self->identity);
146 gst_play_sink_convert_bin_set_targets (GstPlaySinkConvertBin * self,
147 gboolean passthrough)
150 GstElement *head, *tail;
152 GST_DEBUG_OBJECT (self, "Setting pad targets with passthrough %d",
154 if (self->conversion_elements == NULL || passthrough) {
155 GST_DEBUG_OBJECT (self, "no conversion elements, using identity (%p) as "
156 "head/tail", self->identity);
158 GST_WARNING_OBJECT (self,
159 "Doing passthrough as no converter elements were added");
161 head = tail = self->identity;
163 head = GST_ELEMENT (g_list_first (self->conversion_elements)->data);
164 tail = GST_ELEMENT (g_list_last (self->conversion_elements)->data);
165 GST_DEBUG_OBJECT (self, "conversion elements in use, picking "
166 "head:%s and tail:%s", GST_OBJECT_NAME (head), GST_OBJECT_NAME (tail));
169 g_return_if_fail (head != NULL);
170 g_return_if_fail (tail != NULL);
172 pad = gst_element_get_static_pad (head, "sink");
173 GST_DEBUG_OBJECT (self, "Ghosting bin sink pad to %" GST_PTR_FORMAT, pad);
174 gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (self->sinkpad), pad);
175 gst_object_unref (pad);
177 pad = gst_element_get_static_pad (tail, "src");
178 GST_DEBUG_OBJECT (self, "Ghosting bin src pad to %" GST_PTR_FORMAT, pad);
179 gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (self->srcpad), pad);
180 gst_object_unref (pad);
184 gst_play_sink_convert_bin_remove_element (GstElement * element,
185 GstPlaySinkConvertBin * self)
187 gst_element_set_state (element, GST_STATE_NULL);
188 gst_bin_remove (GST_BIN_CAST (self), element);
192 gst_play_sink_convert_bin_on_element_added (GstElement * element,
193 GstPlaySinkConvertBin * self)
195 gst_element_sync_state_with_parent (element);
196 distribute_running_time (element, &self->segment);
199 static GstPadProbeReturn
200 pad_blocked_cb (GstPad * pad, GstPadProbeInfo * info, gpointer user_data)
202 GstPlaySinkConvertBin *self = user_data;
207 GST_PLAY_SINK_CONVERT_BIN_LOCK (self);
208 GST_DEBUG_OBJECT (self, "Pad blocked");
210 /* There must be a peer at this point */
211 peer = gst_pad_get_peer (self->sinkpad);
212 caps = gst_pad_get_current_caps (peer);
214 caps = gst_pad_query_caps (peer, NULL);
215 gst_object_unref (peer);
217 raw = is_raw_caps (caps, self->audio);
218 GST_DEBUG_OBJECT (self, "Caps %" GST_PTR_FORMAT " are raw: %d", caps, raw);
219 gst_caps_unref (caps);
221 if (raw == self->raw)
225 gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (self->sinkpad), NULL);
226 gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (self->srcpad), NULL);
229 GST_DEBUG_OBJECT (self, "Switching to raw conversion pipeline");
231 if (self->conversion_elements)
232 g_list_foreach (self->conversion_elements,
233 (GFunc) gst_play_sink_convert_bin_on_element_added, self);
236 GST_DEBUG_OBJECT (self, "Switch to passthrough pipeline");
238 gst_play_sink_convert_bin_on_element_added (self->identity, self);
241 gst_play_sink_convert_bin_set_targets (self, !raw);
244 self->sink_proxypad_block_id = 0;
245 GST_PLAY_SINK_CONVERT_BIN_UNLOCK (self);
247 return GST_PAD_PROBE_REMOVE;
251 gst_play_sink_convert_bin_sink_event (GstPad * pad, GstEvent * event)
253 GstPlaySinkConvertBin *self =
254 GST_PLAY_SINK_CONVERT_BIN (gst_pad_get_parent (pad));
257 switch (GST_EVENT_TYPE (event)) {
262 gst_event_parse_caps (event, &caps);
263 ret = gst_play_sink_convert_bin_sink_setcaps (self, caps);
270 ret = gst_proxy_pad_event_default (pad, gst_event_ref (event));
272 if (GST_EVENT_TYPE (event) == GST_EVENT_SEGMENT) {
275 GST_PLAY_SINK_CONVERT_BIN_LOCK (self);
276 gst_event_copy_segment (event, &seg);
278 GST_DEBUG_OBJECT (self, "Segment before %" GST_SEGMENT_FORMAT,
281 GST_DEBUG_OBJECT (self, "Segment after %" GST_SEGMENT_FORMAT,
283 GST_PLAY_SINK_CONVERT_BIN_UNLOCK (self);
284 } else if (GST_EVENT_TYPE (event) == GST_EVENT_FLUSH_STOP) {
285 GST_PLAY_SINK_CONVERT_BIN_LOCK (self);
286 GST_DEBUG_OBJECT (self, "Resetting segment");
287 gst_segment_init (&self->segment, GST_FORMAT_UNDEFINED);
288 GST_PLAY_SINK_CONVERT_BIN_UNLOCK (self);
291 gst_event_unref (event);
292 gst_object_unref (self);
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));
340 if (!self->raw && !gst_pad_is_blocked (self->sink_proxypad)) {
341 GST_DEBUG_OBJECT (self, "Changing caps from non-raw to raw");
343 block_proxypad (self);
346 if (self->raw && !gst_pad_is_blocked (self->sink_proxypad)) {
347 GST_DEBUG_OBJECT (self, "Changing caps from raw to non-raw");
349 block_proxypad (self);
353 /* Otherwise the setcaps below fails */
355 gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (self->sinkpad), NULL);
356 gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (self->srcpad), NULL);
359 GST_PLAY_SINK_CONVERT_BIN_UNLOCK (self);
361 GST_DEBUG_OBJECT (self, "Setting sink caps %" GST_PTR_FORMAT, caps);
367 gst_play_sink_convert_bin_getcaps (GstPad * pad, GstCaps * filter)
369 GstPlaySinkConvertBin *self =
370 GST_PLAY_SINK_CONVERT_BIN (gst_pad_get_parent (pad));
372 GstPad *otherpad, *peer;
374 GST_PLAY_SINK_CONVERT_BIN_LOCK (self);
375 if (pad == self->srcpad) {
376 otherpad = self->sinkpad;
377 } else if (pad == self->sinkpad) {
378 otherpad = self->srcpad;
380 GST_ERROR_OBJECT (pad, "Not one of our pads");
385 peer = gst_pad_get_peer (otherpad);
387 GstCaps *peer_caps = gst_pad_query_caps (peer, filter);
388 gst_object_unref (peer);
389 if (self->converter_caps) {
390 peer_caps = gst_caps_make_writable (peer_caps);
391 gst_caps_merge (peer_caps, gst_caps_ref (self->converter_caps));
397 ret = gst_caps_ref (self->converter_caps);
400 ret = gst_caps_new_any ();
402 GST_PLAY_SINK_CONVERT_BIN_UNLOCK (self);
404 gst_object_unref (self);
410 gst_play_sink_convert_bin_query (GstPad * pad, GstQuery * query)
412 gboolean res = FALSE;
414 switch (GST_QUERY_TYPE (query)) {
417 GstCaps *filter, *caps;
419 gst_query_parse_caps (query, &filter);
420 caps = gst_play_sink_convert_bin_getcaps (pad, filter);
421 gst_query_set_caps_result (query, caps);
422 gst_caps_unref (caps);
427 res = gst_pad_query_default (pad, query);
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_finalize (GObject * object)
451 GstPlaySinkConvertBin *self = GST_PLAY_SINK_CONVERT_BIN_CAST (object);
453 gst_play_sink_convert_bin_remove_elements (self);
455 gst_object_unref (self->sink_proxypad);
456 g_mutex_free (self->lock);
458 G_OBJECT_CLASS (parent_class)->finalize (object);
462 gst_play_sink_convert_bin_cache_converter_caps (GstPlaySinkConvertBin * self)
467 if (self->converter_caps) {
468 gst_caps_unref (self->converter_caps);
469 self->converter_caps = NULL;
472 if (!self->conversion_elements) {
473 GST_WARNING_OBJECT (self, "No conversion elements");
477 head = GST_ELEMENT (g_list_first (self->conversion_elements)->data);
478 pad = gst_element_get_static_pad (head, "sink");
480 GST_WARNING_OBJECT (self, "No sink pad found");
484 self->converter_caps = gst_pad_query_caps (pad, NULL);
485 GST_INFO_OBJECT (self, "Converter caps: %" GST_PTR_FORMAT,
486 self->converter_caps);
488 gst_object_unref (pad);
491 static GstStateChangeReturn
492 gst_play_sink_convert_bin_change_state (GstElement * element,
493 GstStateChange transition)
495 GstStateChangeReturn ret;
496 GstPlaySinkConvertBin *self = GST_PLAY_SINK_CONVERT_BIN_CAST (element);
498 switch (transition) {
499 case GST_STATE_CHANGE_PAUSED_TO_READY:
500 GST_PLAY_SINK_CONVERT_BIN_LOCK (self);
501 unblock_proxypad (self);
502 GST_PLAY_SINK_CONVERT_BIN_UNLOCK (self);
504 case GST_STATE_CHANGE_READY_TO_PAUSED:
505 GST_PLAY_SINK_CONVERT_BIN_LOCK (self);
506 gst_segment_init (&self->segment, GST_FORMAT_UNDEFINED);
507 gst_play_sink_convert_bin_set_targets (self, TRUE);
509 GST_PLAY_SINK_CONVERT_BIN_UNLOCK (self);
515 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
516 if (ret == GST_STATE_CHANGE_FAILURE)
519 switch (transition) {
520 case GST_STATE_CHANGE_PAUSED_TO_READY:
521 GST_PLAY_SINK_CONVERT_BIN_LOCK (self);
522 gst_segment_init (&self->segment, GST_FORMAT_UNDEFINED);
523 gst_play_sink_convert_bin_set_targets (self, TRUE);
525 GST_PLAY_SINK_CONVERT_BIN_UNLOCK (self);
527 case GST_STATE_CHANGE_READY_TO_PAUSED:
528 GST_PLAY_SINK_CONVERT_BIN_LOCK (self);
529 unblock_proxypad (self);
530 GST_PLAY_SINK_CONVERT_BIN_UNLOCK (self);
540 gst_play_sink_convert_bin_class_init (GstPlaySinkConvertBinClass * klass)
542 GObjectClass *gobject_class;
543 GstElementClass *gstelement_class;
545 GST_DEBUG_CATEGORY_INIT (gst_play_sink_convert_bin_debug,
546 "playsinkconvertbin", 0, "play bin");
548 gobject_class = (GObjectClass *) klass;
549 gstelement_class = (GstElementClass *) klass;
551 gobject_class->finalize = gst_play_sink_convert_bin_finalize;
553 gst_element_class_add_pad_template (gstelement_class,
554 gst_static_pad_template_get (&srctemplate));
555 gst_element_class_add_pad_template (gstelement_class,
556 gst_static_pad_template_get (&sinktemplate));
557 gst_element_class_set_details_simple (gstelement_class,
558 "Player Sink Converter Bin", "Bin/Converter",
559 "Convenience bin for audio/video conversion",
560 "Sebastian Dröge <sebastian.droege@collabora.co.uk>");
562 gstelement_class->change_state =
563 GST_DEBUG_FUNCPTR (gst_play_sink_convert_bin_change_state);
567 gst_play_sink_convert_bin_init (GstPlaySinkConvertBin * self)
569 GstPadTemplate *templ;
571 self->lock = g_mutex_new ();
572 gst_segment_init (&self->segment, GST_FORMAT_UNDEFINED);
574 templ = gst_static_pad_template_get (&sinktemplate);
575 self->sinkpad = gst_ghost_pad_new_no_target_from_template ("sink", templ);
576 gst_pad_set_event_function (self->sinkpad,
577 GST_DEBUG_FUNCPTR (gst_play_sink_convert_bin_sink_event));
578 gst_pad_set_query_function (self->sinkpad,
579 GST_DEBUG_FUNCPTR (gst_play_sink_convert_bin_query));
581 self->sink_proxypad =
582 GST_PAD_CAST (gst_proxy_pad_get_internal (GST_PROXY_PAD (self->sinkpad)));
584 gst_element_add_pad (GST_ELEMENT_CAST (self), self->sinkpad);
585 gst_object_unref (templ);
587 templ = gst_static_pad_template_get (&srctemplate);
588 self->srcpad = gst_ghost_pad_new_no_target_from_template ("src", templ);
589 gst_pad_set_query_function (self->srcpad,
590 GST_DEBUG_FUNCPTR (gst_play_sink_convert_bin_query));
591 gst_element_add_pad (GST_ELEMENT_CAST (self), self->srcpad);
592 gst_object_unref (templ);
594 gst_play_sink_convert_bin_add_identity (self);