2 * Copyright (C) <2004> Wim Taymans <wim.taymans@gmail.com>
3 * Copyright (C) 2011 Hewlett-Packard Development Company, L.P.
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.
22 * SECTION:element-decodebin
24 * #GstBin that auto-magically constructs a decoding pipeline using available
25 * decoders and demuxers via auto-plugging.
27 * When using decodebin in your application, connect a signal handler to
28 * #GstDecodeBin::new-decoded-pad and connect your sinks from within the
32 * This element is deprecated and no longer supported. You should use the
33 * #uridecodebin or #decodebin2 element instead (or, even better: #playbin2).
36 * Deprecated: use uridecodebin or decodebin2 instead.
43 /* FIXME 0.11: suppress warnings for deprecated API such as GStaticRecMutex
44 * with newer GLib versions (>= 2.31.0) */
45 #define GLIB_DISABLE_DEPRECATION_WARNINGS
47 #include <gst/gst-i18n-plugin.h>
51 #include <gst/pbutils/pbutils.h>
52 #include "gst/glib-compat-private.h"
54 #include "gstplay-marshal.h"
56 /* generic templates */
57 static GstStaticPadTemplate decoder_bin_sink_template =
58 GST_STATIC_PAD_TEMPLATE ("sink",
63 static GstStaticPadTemplate decoder_bin_src_template =
64 GST_STATIC_PAD_TEMPLATE ("src%d",
69 GST_DEBUG_CATEGORY_STATIC (gst_decode_bin_debug);
70 #define GST_CAT_DEFAULT gst_decode_bin_debug
72 #define GST_TYPE_DECODE_BIN (gst_decode_bin_get_type())
73 #define GST_DECODE_BIN(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_DECODE_BIN,GstDecodeBin))
74 #define GST_DECODE_BIN_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_DECODE_BIN,GstDecodeBinClass))
75 #define GST_IS_DECODE_BIN(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_DECODE_BIN))
76 #define GST_IS_DECODE_BIN_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_DECODE_BIN))
78 typedef struct _GstDecodeBin GstDecodeBin;
79 typedef struct _GstDecodeBinClass GstDecodeBinClass;
84 * Auto-plugging decoder element structure
88 GstBin bin; /* we extend GstBin */
90 GstElement *typefind; /* this holds the typefind object */
93 GList *dynamics; /* list of dynamic connections */
95 GList *queues; /* list of demuxer-decoder queues */
97 GList *probes; /* list of PadProbeData */
99 GList *factories; /* factories we can use for selecting elements */
104 guint have_type_id; /* signal id for the typefind element */
106 gboolean shutting_down; /* stop pluggin if we're shutting down */
108 GType queue_type; /* store the GType of queues, to aid in recognising them */
110 GMutex *cb_mutex; /* Mutex for multi-threaded callbacks, such as removing the fakesink */
113 struct _GstDecodeBinClass
115 GstBinClass parent_class;
117 /* signal we fire when a new pad has been decoded into raw audio/video */
118 void (*new_decoded_pad) (GstElement * element, GstPad * pad, gboolean last);
119 /* signal we fire when a pad has been removed */
120 void (*removed_decoded_pad) (GstElement * element, GstPad * pad);
121 /* signal fired when we found a pad that we cannot decode */
122 void (*unknown_type) (GstElement * element, GstPad * pad, GstCaps * caps);
128 SIGNAL_NEW_DECODED_PAD,
129 SIGNAL_REMOVED_DECODED_PAD,
150 /* this structure is created for all dynamic pads that could get created
154 GstDecodeBin *decode_bin; /* pointer to ourself */
156 GstElement *element; /* the element sending the signal */
157 gint np_sig_id; /* signal id of new_pad */
158 gint nmp_sig_id; /* signal id of no_more_pads */
160 GstPad *pad; /* the pad sending the signal */
161 gint caps_sig_id; /* signal id of caps */
165 static void gst_decode_bin_class_init (GstDecodeBinClass * klass);
166 static void gst_decode_bin_init (GstDecodeBin * decode_bin);
167 static void gst_decode_bin_set_property (GObject * object, guint prop_id,
168 const GValue * value, GParamSpec * pspec);
169 static void gst_decode_bin_get_property (GObject * object, guint prop_id,
170 GValue * value, GParamSpec * pspec);
171 static void gst_decode_bin_dispose (GObject * object);
172 static void gst_decode_bin_finalize (GObject * object);
174 static GstStateChangeReturn gst_decode_bin_change_state (GstElement * element,
175 GstStateChange transition);
177 static gboolean add_fakesink (GstDecodeBin * decode_bin);
178 static void remove_fakesink (GstDecodeBin * decode_bin);
180 static void dynamic_free (GstDynamic * dyn);
181 static void free_dynamics (GstDecodeBin * decode_bin);
182 static void type_found (GstElement * typefind, guint probability,
183 GstCaps * caps, GstDecodeBin * decode_bin);
184 static GstElement *try_to_link_1 (GstDecodeBin * decode_bin,
185 GstElement * origelement, GstPad * pad, GList * factories);
186 static void close_link (GstElement * element, GstDecodeBin * decode_bin);
187 static void close_pad_link (GstElement * element, GstPad * pad,
188 GstCaps * caps, GstDecodeBin * decode_bin, gboolean more);
189 static void unlinked (GstPad * pad, GstPad * peerpad,
190 GstDecodeBin * decode_bin);
191 static void new_pad (GstElement * element, GstPad * pad, GstDynamic * dynamic);
192 static void no_more_pads (GstElement * element, GstDynamic * dynamic);
193 static void new_caps (GstPad * pad, GParamSpec * unused, GstDynamic * dynamic);
195 static void queue_filled_cb (GstElement * queue, GstDecodeBin * decode_bin);
196 static void queue_underrun_cb (GstElement * queue, GstDecodeBin * decode_bin);
198 static gboolean is_demuxer_element (GstElement * srcelement);
200 static GstElementClass *parent_class;
201 static guint gst_decode_bin_signals[LAST_SIGNAL] = { 0 };
205 gst_decode_bin_get_type (void)
207 static GType gst_decode_bin_type = 0;
209 if (!gst_decode_bin_type) {
210 static const GTypeInfo gst_decode_bin_info = {
211 sizeof (GstDecodeBinClass),
214 (GClassInitFunc) gst_decode_bin_class_init,
217 sizeof (GstDecodeBin),
219 (GInstanceInitFunc) gst_decode_bin_init,
223 gst_decode_bin_type =
224 g_type_register_static (GST_TYPE_BIN, "GstDecodeBin",
225 &gst_decode_bin_info, 0);
228 return gst_decode_bin_type;
232 gst_decode_bin_class_init (GstDecodeBinClass * klass)
234 GObjectClass *gobject_klass;
235 GstElementClass *gstelement_klass;
237 gobject_klass = (GObjectClass *) klass;
238 gstelement_klass = (GstElementClass *) klass;
240 parent_class = g_type_class_peek_parent (klass);
242 gobject_klass->set_property = gst_decode_bin_set_property;
243 gobject_klass->get_property = gst_decode_bin_get_property;
244 gobject_klass->dispose = gst_decode_bin_dispose;
245 gobject_klass->finalize = gst_decode_bin_finalize;
248 * GstDecodeBin::new-decoded-pad:
249 * @bin: The decodebin
250 * @pad: The newly created pad
251 * @islast: #TRUE if this is the last pad to be added. Deprecated.
253 * This signal gets emitted as soon as a new pad of the same type as one of
254 * the valid 'raw' types is added.
256 gst_decode_bin_signals[SIGNAL_NEW_DECODED_PAD] =
257 g_signal_new ("new-decoded-pad", G_TYPE_FROM_CLASS (klass),
259 G_STRUCT_OFFSET (GstDecodeBinClass, new_decoded_pad), NULL, NULL,
260 gst_play_marshal_VOID__OBJECT_BOOLEAN, G_TYPE_NONE, 2, GST_TYPE_PAD,
263 * GstDecodeBin::removed-decoded-pad:
264 * @bin: The decodebin
265 * @pad: The pad that was removed
267 * This signal is emitted when a 'final' caps pad has been removed.
269 gst_decode_bin_signals[SIGNAL_REMOVED_DECODED_PAD] =
270 g_signal_new ("removed-decoded-pad", G_TYPE_FROM_CLASS (klass),
272 G_STRUCT_OFFSET (GstDecodeBinClass, removed_decoded_pad), NULL, NULL,
273 gst_marshal_VOID__OBJECT, G_TYPE_NONE, 1, GST_TYPE_PAD);
275 * GstDecodeBin::unknown-type:
276 * @bin: The decodebin
277 * @pad: The new pad containing caps that cannot be resolved to a 'final'
279 * @caps: The #GstCaps of the pad that cannot be resolved.
281 * This signal is emitted when a pad for which there is no further possible
282 * decoding is added to the decodebin.
284 gst_decode_bin_signals[SIGNAL_UNKNOWN_TYPE] =
285 g_signal_new ("unknown-type", G_TYPE_FROM_CLASS (klass),
286 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstDecodeBinClass, unknown_type),
287 NULL, NULL, gst_marshal_VOID__OBJECT_BOXED, G_TYPE_NONE, 2,
288 GST_TYPE_PAD, GST_TYPE_CAPS);
290 g_object_class_install_property (gobject_klass, PROP_SINK_CAPS,
291 g_param_spec_boxed ("sink-caps", "Sink Caps",
292 "The caps of the input data. (NULL = use typefind element)",
293 GST_TYPE_CAPS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
295 gst_element_class_add_static_pad_template (gstelement_klass,
296 &decoder_bin_sink_template);
297 gst_element_class_add_static_pad_template (gstelement_klass,
298 &decoder_bin_src_template);
300 gst_element_class_set_details_simple (gstelement_klass,
301 "Decoder Bin", "Generic/Bin/Decoder",
302 "Autoplug and decode to raw media",
303 "Wim Taymans <wim.taymans@gmail.com>");
305 gstelement_klass->change_state =
306 GST_DEBUG_FUNCPTR (gst_decode_bin_change_state);
309 /* check if the bin is dynamic.
311 * If there are no outstanding dynamic connections, the bin is
312 * considered to be non-dynamic.
315 gst_decode_bin_is_dynamic (GstDecodeBin * decode_bin)
317 return decode_bin->dynamics != NULL;
320 /* the filter function for selecting the elements we can use in
323 gst_decode_bin_factory_filter (GstPluginFeature * feature,
324 GstDecodeBin * decode_bin)
329 /* we only care about element factories */
330 if (!GST_IS_ELEMENT_FACTORY (feature))
333 klass = gst_element_factory_get_klass (GST_ELEMENT_FACTORY (feature));
334 /* only demuxers, decoders and parsers can play */
335 if (strstr (klass, "Demux") == NULL &&
336 strstr (klass, "Decoder") == NULL && strstr (klass, "Parse") == NULL &&
337 strstr (klass, "Depayloader") == NULL) {
341 /* only select elements with autoplugging rank */
342 rank = gst_plugin_feature_get_rank (feature);
343 if (rank < GST_RANK_MARGINAL)
349 /* function used to sort element features */
351 compare_ranks (GstPluginFeature * f1, GstPluginFeature * f2)
354 const gchar *rname1, *rname2;
356 diff = gst_plugin_feature_get_rank (f2) - gst_plugin_feature_get_rank (f1);
360 rname1 = gst_plugin_feature_get_name (f1);
361 rname2 = gst_plugin_feature_get_name (f2);
363 diff = strcmp (rname2, rname1);
369 print_feature (GstPluginFeature * feature)
373 rname = gst_plugin_feature_get_name (feature);
375 GST_DEBUG ("%s", rname);
379 gst_decode_bin_init (GstDecodeBin * decode_bin)
383 decode_bin->cb_mutex = g_mutex_new ();
385 /* first filter out the interesting element factories */
386 factories = gst_default_registry_feature_filter (
387 (GstPluginFeatureFilter) gst_decode_bin_factory_filter,
390 /* sort them according to their ranks */
391 decode_bin->factories = g_list_sort (factories, (GCompareFunc) compare_ranks);
392 /* do some debugging */
393 g_list_foreach (decode_bin->factories, (GFunc) print_feature, NULL);
395 /* we create the typefind element only once */
396 decode_bin->typefind = gst_element_factory_make ("typefind", "typefind");
397 if (!decode_bin->typefind) {
398 g_warning ("can't find typefind element, decodebin will not work");
401 GstPadTemplate *pad_tmpl;
403 /* add the typefind element */
404 if (!gst_bin_add (GST_BIN (decode_bin), decode_bin->typefind)) {
405 g_warning ("Could not add typefind element, decodebin will not work");
406 gst_object_unref (decode_bin->typefind);
407 decode_bin->typefind = NULL;
410 /* get the sinkpad */
411 pad = gst_element_get_static_pad (decode_bin->typefind, "sink");
413 /* get the pad template */
414 pad_tmpl = gst_static_pad_template_get (&decoder_bin_sink_template);
416 /* ghost the sink pad to ourself */
417 gpad = gst_ghost_pad_new_from_template ("sink", pad, pad_tmpl);
418 gst_pad_set_active (gpad, TRUE);
419 gst_element_add_pad (GST_ELEMENT (decode_bin), gpad);
421 gst_object_unref (pad_tmpl);
422 gst_object_unref (pad);
424 /* connect a signal to find out when the typefind element found
426 decode_bin->have_type_id =
427 g_signal_connect (G_OBJECT (decode_bin->typefind), "have_type",
428 G_CALLBACK (type_found), decode_bin);
430 add_fakesink (decode_bin);
432 decode_bin->dynamics = NULL;
433 decode_bin->queues = NULL;
434 decode_bin->probes = NULL;
438 gst_decode_bin_dispose (GObject * object)
440 GstDecodeBin *decode_bin;
442 decode_bin = GST_DECODE_BIN (object);
444 if (decode_bin->factories)
445 gst_plugin_feature_list_free (decode_bin->factories);
446 decode_bin->factories = NULL;
448 G_OBJECT_CLASS (parent_class)->dispose (object);
450 /* our parent dispose might trigger new signals when pads are unlinked
451 * etc. clean up the mess here. */
452 /* FIXME do proper cleanup when going to NULL */
453 free_dynamics (decode_bin);
457 gst_decode_bin_set_sink_caps (GstDecodeBin * dbin, GstCaps * caps)
459 GST_DEBUG_OBJECT (dbin, "Setting new caps: %" GST_PTR_FORMAT, caps);
461 g_object_set (dbin->typefind, "force-caps", caps, NULL);
465 gst_decode_bin_get_sink_caps (GstDecodeBin * dbin)
469 GST_DEBUG_OBJECT (dbin, "Getting currently set caps");
471 g_object_get (dbin->typefind, "force-caps", &caps, NULL);
477 gst_decode_bin_set_property (GObject * object, guint prop_id,
478 const GValue * value, GParamSpec * pspec)
482 dbin = GST_DECODE_BIN (object);
486 gst_decode_bin_set_sink_caps (dbin, g_value_get_boxed (value));
489 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
495 gst_decode_bin_get_property (GObject * object, guint prop_id,
496 GValue * value, GParamSpec * pspec)
500 dbin = GST_DECODE_BIN (object);
503 g_value_take_boxed (value, gst_decode_bin_get_sink_caps (dbin));
506 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
512 gst_decode_bin_finalize (GObject * object)
514 GstDecodeBin *decode_bin = GST_DECODE_BIN (object);
516 g_mutex_free (decode_bin->cb_mutex);
518 G_OBJECT_CLASS (parent_class)->finalize (object);
528 find_dynamic (GstDynamic * dyn, struct DynFind *info)
530 if (dyn->element == info->elem && dyn->pad == info->pad)
535 /* Add either an element (for dynamic pads/pad-added watching) or a
536 * pad (for delayed caps/notify::caps watching) to the dynamic list,
537 * taking care to ignore repeat entries so we don't end up handling a
538 * pad twice, for example */
540 dynamic_add (GstElement * element, GstPad * pad, GstDecodeBin * decode_bin)
543 struct DynFind find_info;
546 g_return_if_fail (element != NULL);
548 /* do a search that this entry doesn't already exist */
549 find_info.elem = element;
551 found = g_list_find_custom (decode_bin->dynamics, &find_info,
552 (GCompareFunc) find_dynamic);
557 dyn = g_new0 (GstDynamic, 1);
558 dyn->element = gst_object_ref (element);
559 dyn->decode_bin = gst_object_ref (decode_bin);
561 dyn->pad = gst_object_ref (pad);
562 GST_DEBUG_OBJECT (decode_bin, "dynamic create for pad %" GST_PTR_FORMAT,
564 dyn->caps_sig_id = g_signal_connect (G_OBJECT (pad), "notify::caps",
565 G_CALLBACK (new_caps), dyn);
567 GST_DEBUG_OBJECT (decode_bin, "dynamic create for element %"
568 GST_PTR_FORMAT, element);
569 dyn->np_sig_id = g_signal_connect (G_OBJECT (element), "pad-added",
570 G_CALLBACK (new_pad), dyn);
571 dyn->nmp_sig_id = g_signal_connect (G_OBJECT (element), "no-more-pads",
572 G_CALLBACK (no_more_pads), dyn);
575 /* and add this element to the dynamic elements */
576 decode_bin->dynamics = g_list_prepend (decode_bin->dynamics, dyn);
581 GST_DEBUG_OBJECT (decode_bin, "Dynamic element already added: %"
582 GST_PTR_FORMAT, element);
584 GST_DEBUG_OBJECT (decode_bin, "Dynamic pad already added: %"
585 GST_PTR_FORMAT, pad);
590 dynamic_free (GstDynamic * dyn)
592 GST_DEBUG_OBJECT (dyn->decode_bin, "dynamic free");
594 /* disconnect signals */
596 g_signal_handler_disconnect (G_OBJECT (dyn->element), dyn->np_sig_id);
598 g_signal_handler_disconnect (G_OBJECT (dyn->element), dyn->nmp_sig_id);
599 if (dyn->caps_sig_id)
600 g_signal_handler_disconnect (G_OBJECT (dyn->pad), dyn->caps_sig_id);
603 gst_object_unref (dyn->pad);
606 gst_object_unref (dyn->element);
609 gst_object_unref (dyn->decode_bin);
610 dyn->decode_bin = NULL;
616 free_dynamics (GstDecodeBin * decode_bin)
620 for (dyns = decode_bin->dynamics; dyns; dyns = g_list_next (dyns)) {
621 GstDynamic *dynamic = (GstDynamic *) dyns->data;
623 dynamic_free (dynamic);
625 g_list_free (decode_bin->dynamics);
626 decode_bin->dynamics = NULL;
629 /* this function runs through the element factories and returns a list
630 * of all elements that are able to sink the given caps
633 find_compatibles (GstDecodeBin * decode_bin, const GstCaps * caps)
636 GList *to_try = NULL;
638 /* loop over all the factories */
639 for (factories = decode_bin->factories; factories;
640 factories = g_list_next (factories)) {
641 GstElementFactory *factory = GST_ELEMENT_FACTORY (factories->data);
642 const GList *templates;
645 /* get the templates from the element factory */
646 templates = gst_element_factory_get_static_pad_templates (factory);
647 for (walk = (GList *) templates; walk; walk = g_list_next (walk)) {
648 GstStaticPadTemplate *templ = walk->data;
650 /* we only care about the sink templates */
651 if (templ->direction == GST_PAD_SINK) {
652 gboolean can_intersect;
655 /* try to intersect the caps with the caps of the template */
656 tmpl_caps = gst_static_caps_get (&templ->static_caps);
658 can_intersect = gst_caps_can_intersect (caps, tmpl_caps);
659 gst_caps_unref (tmpl_caps);
661 /* check if the intersection is empty */
663 /* non empty intersection, we can use this element */
664 to_try = g_list_prepend (to_try, factory);
670 to_try = g_list_reverse (to_try);
676 mimetype_is_raw (const gchar * mimetype)
678 return g_str_has_prefix (mimetype, "video/x-raw") ||
679 g_str_has_prefix (mimetype, "audio/x-raw") ||
680 g_str_has_prefix (mimetype, "text/plain") ||
681 g_str_has_prefix (mimetype, "text/x-pango-markup");
685 free_pad_probes (GstDecodeBin * decode_bin)
689 /* Remove pad probes */
690 for (tmp = decode_bin->probes; tmp; tmp = g_list_next (tmp)) {
691 PadProbeData *data = (PadProbeData *) tmp->data;
693 gst_pad_remove_data_probe (data->pad, data->sigid);
696 g_list_free (decode_bin->probes);
697 decode_bin->probes = NULL;
700 /* used when we need to remove a probe because the decoder we plugged failed
703 free_pad_probe_for_element (GstDecodeBin * decode_bin, GstElement * element)
707 for (l = decode_bin->probes; l != NULL; l = g_list_next (l)) {
708 PadProbeData *data = (PadProbeData *) l->data;
710 if (GST_ELEMENT_CAST (GST_PAD_PARENT (data->pad)) == element) {
711 gst_pad_remove_data_probe (data->pad, data->sigid);
712 decode_bin->probes = g_list_delete_link (decode_bin->probes, l);
720 add_fakesink (GstDecodeBin * decode_bin)
722 if (decode_bin->fakesink != NULL)
725 g_mutex_lock (decode_bin->cb_mutex);
727 decode_bin->fakesink = gst_element_factory_make ("fakesink", "fakesink");
728 if (!decode_bin->fakesink)
731 /* hacky, remove sink flag, we don't want our decodebin to become a sink
732 * just because we add a fakesink element to make us ASYNC */
733 GST_OBJECT_FLAG_UNSET (decode_bin->fakesink, GST_ELEMENT_IS_SINK);
735 /* takes ownership */
736 if (!gst_bin_add (GST_BIN (decode_bin), decode_bin->fakesink)) {
737 g_warning ("Could not add fakesink element, decodebin will not work");
738 gst_object_unref (decode_bin->fakesink);
739 decode_bin->fakesink = NULL;
741 g_mutex_unlock (decode_bin->cb_mutex);
747 g_warning ("can't find fakesink element, decodebin will not work");
748 g_mutex_unlock (decode_bin->cb_mutex);
754 remove_fakesink (GstDecodeBin * decode_bin)
756 gboolean removed_fakesink = FALSE;
758 if (decode_bin->fakesink == NULL)
761 g_mutex_lock (decode_bin->cb_mutex);
762 if (decode_bin->fakesink) {
763 GST_DEBUG_OBJECT (decode_bin, "Removing fakesink and marking state dirty");
765 /* Lock the state to prevent it from changing state to non-NULL
766 * before it's removed */
767 gst_element_set_locked_state (decode_bin->fakesink, TRUE);
768 /* setting the state to NULL is never async */
769 gst_element_set_state (decode_bin->fakesink, GST_STATE_NULL);
770 gst_bin_remove (GST_BIN (decode_bin), decode_bin->fakesink);
771 decode_bin->fakesink = NULL;
773 removed_fakesink = TRUE;
775 g_mutex_unlock (decode_bin->cb_mutex);
777 if (removed_fakesink) {
778 free_pad_probes (decode_bin);
782 /* this should be implemented with _pad_block() */
784 pad_probe (GstPad * pad, GstMiniObject * data, GstDecodeBin * decode_bin)
787 gboolean alldone = TRUE;
789 for (tmp = decode_bin->probes; tmp; tmp = g_list_next (tmp)) {
790 PadProbeData *pdata = (PadProbeData *) tmp->data;
792 if (pdata->pad == pad) {
793 if (GST_IS_BUFFER (data)) {
795 decode_bin->numwaiting--;
797 } else if (GST_IS_EVENT (data) &&
798 ((GST_EVENT_TYPE (data) == GST_EVENT_EOS) ||
799 (GST_EVENT_TYPE (data) == GST_EVENT_TAG) ||
800 (GST_EVENT_TYPE (data) == GST_EVENT_FLUSH_START))) {
801 /* FIXME, what about NEWSEGMENT? really, use _pad_block()... */
803 decode_bin->numwaiting--;
808 if (!(pdata->done)) {
809 GST_LOG_OBJECT (decode_bin, "Pad probe on pad %" GST_PTR_FORMAT
810 " but pad %" GST_PTR_FORMAT " still needs data.", pad, pdata->pad);
815 remove_fakesink (decode_bin);
819 /* FIXME: this should be somehow merged with the queue code in
820 * try_to_link_1() to reduce code duplication */
822 add_raw_queue (GstDecodeBin * decode_bin, GstPad * pad)
824 GstElement *queue = NULL;
825 GstPad *queuesinkpad = NULL, *queuesrcpad = NULL;
827 queue = gst_element_factory_make ("queue", NULL);
828 decode_bin->queue_type = G_OBJECT_TYPE (queue);
830 g_object_set (G_OBJECT (queue), "max-size-buffers", 0, NULL);
831 g_object_set (G_OBJECT (queue), "max-size-time", G_GINT64_CONSTANT (0), NULL);
832 g_object_set (G_OBJECT (queue), "max-size-bytes", 8192, NULL);
833 gst_bin_add (GST_BIN (decode_bin), queue);
834 gst_element_set_state (queue, GST_STATE_READY);
835 queuesinkpad = gst_element_get_static_pad (queue, "sink");
836 queuesrcpad = gst_element_get_static_pad (queue, "src");
838 if (gst_pad_link (pad, queuesinkpad) != GST_PAD_LINK_OK) {
839 GST_WARNING_OBJECT (decode_bin,
840 "Linking queue failed, trying without queue");
841 gst_element_set_state (queue, GST_STATE_NULL);
842 gst_object_unref (queuesrcpad);
843 gst_object_unref (queuesinkpad);
844 gst_bin_remove (GST_BIN (decode_bin), queue);
845 return gst_object_ref (pad);
848 decode_bin->queues = g_list_append (decode_bin->queues, queue);
849 g_signal_connect (G_OBJECT (queue),
850 "overrun", G_CALLBACK (queue_filled_cb), decode_bin);
851 g_signal_connect (G_OBJECT (queue),
852 "underrun", G_CALLBACK (queue_underrun_cb), decode_bin);
854 gst_element_set_state (queue, GST_STATE_PAUSED);
855 gst_object_unref (queuesinkpad);
860 /* given a pad and a caps from an element, find the list of elements
861 * that could connect to the pad
863 * If the pad has a raw format, this function will create a ghostpad
864 * for the pad onto the decodebin.
866 * If no compatible elements could be found, this function will signal
867 * the unknown_type signal.
870 close_pad_link (GstElement * element, GstPad * pad, GstCaps * caps,
871 GstDecodeBin * decode_bin, gboolean more)
873 GstStructure *structure;
874 const gchar *mimetype;
878 padname = gst_pad_get_name (pad);
879 diff = strncmp (padname, "current_", 8);
882 /* hack.. ignore current pads */
886 /* the caps is empty, this means the pad has no type, we can only
887 * decide to fire the unknown_type signal. */
888 if (caps == NULL || gst_caps_is_empty (caps))
891 /* the caps is any, this means the pad can be anything and
892 * we don't know yet */
893 if (gst_caps_is_any (caps))
896 GST_LOG_OBJECT (element, "trying to close %" GST_PTR_FORMAT, caps);
898 /* FIXME, iterate over more structures? I guess it is possible that
899 * this pad has some encoded and some raw pads. This code will fail
900 * then if the first structure is not the raw type... */
901 structure = gst_caps_get_structure (caps, 0);
902 mimetype = gst_structure_get_name (structure);
904 /* first see if this is raw. If the type is raw, we can
905 * create a ghostpad for this pad. It's possible that the caps are not
907 if (mimetype_is_raw (mimetype)) {
908 GstPadTemplate *tmpl;
913 /* If we're at a demuxer element but have raw data already
914 * we have to add a queue here. For non-raw data this is done
915 * in try_to_link_1() */
916 if (is_demuxer_element (element)) {
917 GST_DEBUG_OBJECT (decode_bin,
918 "Element %s is a demuxer, inserting a queue",
919 GST_OBJECT_NAME (element));
921 pad = add_raw_queue (decode_bin, pad);
924 /* make a unique name for this new pad */
925 padname = g_strdup_printf ("src%d", decode_bin->numpads);
926 decode_bin->numpads++;
928 /* make it a ghostpad */
929 tmpl = gst_static_pad_template_get (&decoder_bin_src_template);
930 ghost = gst_ghost_pad_new_from_template (padname, pad, tmpl);
931 gst_object_unref (tmpl);
933 gst_pad_set_active (ghost, TRUE);
934 gst_element_add_pad (GST_ELEMENT (decode_bin), ghost);
936 data = g_new0 (PadProbeData, 1);
940 /* FIXME, use _pad_block() */
941 data->sigid = gst_pad_add_data_probe (pad, G_CALLBACK (pad_probe),
943 decode_bin->numwaiting++;
945 decode_bin->probes = g_list_append (decode_bin->probes, data);
947 GST_LOG_OBJECT (element, "closed pad %s", padname);
949 /* our own signal with an extra flag that this is the only pad */
950 GST_DEBUG_OBJECT (decode_bin, "emitting new-decoded-pad");
951 g_signal_emit (G_OBJECT (decode_bin),
952 gst_decode_bin_signals[SIGNAL_NEW_DECODED_PAD], 0, ghost, !more);
953 GST_DEBUG_OBJECT (decode_bin, "emitted new-decoded-pad");
957 /* If we're at a demuxer element pad was set to a queue's
958 * srcpad and must be unref'd here */
959 if (is_demuxer_element (element))
960 gst_object_unref (pad);
964 /* if the caps has many types, we need to delay */
965 if (!gst_caps_is_fixed (caps))
968 /* continue plugging, first find all compatible elements */
969 to_try = find_compatibles (decode_bin, caps);
971 /* no compatible elements, we cannot go on */
974 if (try_to_link_1 (decode_bin, element, pad, to_try) == NULL) {
975 g_list_free (to_try);
976 GST_LOG_OBJECT (pad, "none of the allegedly available elements usable");
980 /* can free the list again now */
981 g_list_free (to_try);
988 GST_LOG_OBJECT (pad, "unknown type found, fire signal");
989 g_signal_emit (G_OBJECT (decode_bin),
990 gst_decode_bin_signals[SIGNAL_UNKNOWN_TYPE], 0, pad, caps);
992 gst_element_post_message (GST_ELEMENT_CAST (decode_bin),
993 gst_missing_decoder_message_new (GST_ELEMENT_CAST (decode_bin), caps));
995 if (element == decode_bin->typefind) {
998 desc = gst_pb_utils_get_decoder_description (caps);
999 GST_ELEMENT_ERROR (decode_bin, STREAM, CODEC_NOT_FOUND,
1000 (_("A %s plugin is required to play this stream, but not installed."),
1002 ("No decoder to handle media type '%s'",
1003 gst_structure_get_name (gst_caps_get_structure (caps, 0))));
1011 GST_LOG_OBJECT (pad, "type is not known yet");
1012 goto setup_caps_delay;
1016 GST_LOG_OBJECT (pad, "many possible types");
1017 goto setup_caps_delay;
1021 GST_LOG_OBJECT (pad, "setting up a delayed link");
1022 dynamic_add (element, pad, decode_bin);
1027 /* Decide whether an element is a demuxer based on the
1028 * klass and number/type of src pad templates it has */
1030 is_demuxer_element (GstElement * srcelement)
1032 GstElementFactory *srcfactory;
1033 GstElementClass *elemclass;
1036 gint potential_src_pads = 0;
1038 srcfactory = gst_element_get_factory (srcelement);
1039 klass = gst_element_factory_get_klass (srcfactory);
1041 /* Can't be a demuxer unless it has Demux in the klass name */
1042 if (klass == NULL || !strstr (klass, "Demux"))
1045 /* Walk the src pad templates and count how many the element
1047 elemclass = GST_ELEMENT_GET_CLASS (srcelement);
1049 walk = gst_element_class_get_pad_template_list (elemclass);
1050 while (walk != NULL) {
1051 GstPadTemplate *templ;
1053 templ = (GstPadTemplate *) walk->data;
1054 if (GST_PAD_TEMPLATE_DIRECTION (templ) == GST_PAD_SRC) {
1055 switch (GST_PAD_TEMPLATE_PRESENCE (templ)) {
1056 case GST_PAD_ALWAYS:
1057 case GST_PAD_SOMETIMES:
1058 if (strstr (GST_PAD_TEMPLATE_NAME_TEMPLATE (templ), "%"))
1059 potential_src_pads += 2; /* Might make multiple pads */
1061 potential_src_pads += 1;
1063 case GST_PAD_REQUEST:
1064 potential_src_pads += 2;
1068 walk = g_list_next (walk);
1071 if (potential_src_pads < 2)
1078 * given a list of element factories, try to link one of the factories
1081 * The function returns the element that was successfully linked to the
1085 try_to_link_1 (GstDecodeBin * decode_bin, GstElement * srcelement, GstPad * pad,
1089 GstElement *result = NULL;
1090 gboolean isdemux = FALSE;
1091 GstPad *queuesinkpad = NULL, *queuesrcpad = NULL;
1092 GstElement *queue = NULL;
1093 GstPad *usedsrcpad = pad;
1095 /* Check if the parent of the src pad is a demuxer */
1096 isdemux = is_demuxer_element (srcelement);
1098 if (isdemux && factories != NULL) {
1099 GstPadLinkReturn dqlink;
1101 /* Insert a queue between demuxer and decoder */
1102 GST_DEBUG_OBJECT (decode_bin,
1103 "Element %s is a demuxer, inserting a queue",
1104 GST_OBJECT_NAME (srcelement));
1105 queue = gst_element_factory_make ("queue", NULL);
1106 decode_bin->queue_type = G_OBJECT_TYPE (queue);
1108 g_object_set (G_OBJECT (queue), "max-size-buffers", 0, NULL);
1109 g_object_set (G_OBJECT (queue), "max-size-time", G_GINT64_CONSTANT (0),
1111 g_object_set (G_OBJECT (queue), "max-size-bytes", 8192, NULL);
1112 gst_bin_add (GST_BIN (decode_bin), queue);
1113 gst_element_set_state (queue, GST_STATE_READY);
1114 queuesinkpad = gst_element_get_static_pad (queue, "sink");
1115 usedsrcpad = queuesrcpad = gst_element_get_static_pad (queue, "src");
1117 dqlink = gst_pad_link (pad, queuesinkpad);
1118 g_return_val_if_fail (dqlink == GST_PAD_LINK_OK, NULL);
1121 /* loop over the factories */
1122 for (walk = factories; walk; walk = g_list_next (walk)) {
1123 GstElementFactory *factory = GST_ELEMENT_FACTORY (walk->data);
1124 GstElementFactory *src_factory;
1125 GstElement *element;
1126 GstPadLinkReturn ret;
1129 GST_DEBUG_OBJECT (decode_bin, "trying to link %s to %s",
1130 gst_plugin_feature_get_name (GST_PLUGIN_FEATURE (factory)),
1131 GST_OBJECT_NAME (srcelement));
1133 /* don't plug the same parser twice, but allow multiple
1134 * instances of other elements (e.g. id3demux) in a row */
1135 src_factory = gst_element_get_factory (srcelement);
1136 if (src_factory == factory
1137 && gst_element_factory_list_is_type (factory,
1138 GST_ELEMENT_FACTORY_TYPE_PARSER)) {
1139 GST_DEBUG_OBJECT (decode_bin,
1140 "not inserting parser element %s twice in a row, skipping",
1141 GST_PLUGIN_FEATURE_NAME (factory));
1145 /* make an element from the factory first */
1146 if ((element = gst_element_factory_create (factory, NULL)) == NULL) {
1147 /* hmm, strange. Like with all things in life, let's move on.. */
1148 GST_WARNING_OBJECT (decode_bin, "could not create an element from %s",
1149 gst_plugin_feature_get_name (GST_PLUGIN_FEATURE (factory)));
1153 /* try to link the given pad to a sinkpad */
1154 /* FIXME, find the sinkpad by looping over the pads instead of
1155 * looking it up by name */
1156 if ((sinkpad = gst_element_get_static_pad (element, "sink")) == NULL) {
1157 /* if no pad is found we can't do anything */
1158 GST_WARNING_OBJECT (decode_bin, "could not find sinkpad in element");
1162 /* now add the element to the bin first */
1163 GST_DEBUG_OBJECT (decode_bin, "adding %s", GST_OBJECT_NAME (element));
1164 gst_bin_add (GST_BIN (decode_bin), element);
1166 /* set to READY first so it is ready, duh. */
1167 if (gst_element_set_state (element,
1168 GST_STATE_READY) == GST_STATE_CHANGE_FAILURE) {
1169 GST_WARNING_OBJECT (decode_bin, "Couldn't set %s to READY",
1170 GST_ELEMENT_NAME (element));
1171 /* get rid of the sinkpad */
1172 gst_object_unref (sinkpad);
1173 /* this element did not work, remove it again and continue trying
1174 * other elements, the element will be disposed. */
1175 /* FIXME: shouldn't we do this before adding it to the bin so that no
1176 * error messages get through to the app? (tpm) */
1177 gst_bin_remove (GST_BIN (decode_bin), element);
1181 if ((ret = gst_pad_link (usedsrcpad, sinkpad)) != GST_PAD_LINK_OK) {
1182 GST_DEBUG_OBJECT (decode_bin, "link failed on pad %s:%s, reason %d",
1183 GST_DEBUG_PAD_NAME (pad), ret);
1184 /* get rid of the sinkpad */
1185 gst_object_unref (sinkpad);
1186 /* this element did not work, remove it again and continue trying
1187 * other elements, the element will be disposed. */
1188 gst_element_set_state (element, GST_STATE_NULL);
1189 gst_bin_remove (GST_BIN (decode_bin), element);
1191 GST_DEBUG_OBJECT (decode_bin, "linked on pad %s:%s",
1192 GST_DEBUG_PAD_NAME (usedsrcpad));
1194 /* configure the queue some more */
1195 if (queue != NULL) {
1196 decode_bin->queues = g_list_append (decode_bin->queues, queue);
1197 g_signal_connect (G_OBJECT (queue),
1198 "overrun", G_CALLBACK (queue_filled_cb), decode_bin);
1199 g_signal_connect (G_OBJECT (queue),
1200 "underrun", G_CALLBACK (queue_underrun_cb), decode_bin);
1203 /* The link worked, now figure out what it was that we connected */
1205 /* make sure we catch unlink signals */
1206 g_signal_connect (G_OBJECT (pad), "unlinked",
1207 G_CALLBACK (unlinked), decode_bin);
1209 /* now that we added the element we can try to continue autoplugging
1210 * on it until we have a raw type */
1211 close_link (element, decode_bin);
1213 /* change the state of the element to that of the parent */
1214 if ((gst_element_set_state (element,
1215 GST_STATE_PAUSED)) == GST_STATE_CHANGE_FAILURE) {
1216 GST_WARNING_OBJECT (decode_bin, "Couldn't set %s to PAUSED",
1217 GST_ELEMENT_NAME (element));
1218 /* close_link -> close_pad_link -> might have set up a pad probe */
1219 free_pad_probe_for_element (decode_bin, element);
1220 gst_element_set_state (element, GST_STATE_NULL);
1221 gst_bin_remove (GST_BIN (decode_bin), element);
1227 /* get rid of the sinkpad now */
1228 gst_object_unref (sinkpad);
1230 /* Set the queue to paused and set the pointer to NULL so we don't
1231 * remove it below */
1232 if (queue != NULL) {
1233 gst_element_set_state (queue, GST_STATE_PAUSED);
1235 gst_object_unref (queuesrcpad);
1236 gst_object_unref (queuesinkpad);
1244 if (queue != NULL) {
1245 /* We didn't successfully connect to the queue */
1246 gst_pad_unlink (pad, queuesinkpad);
1247 gst_element_set_state (queue, GST_STATE_NULL);
1248 gst_object_unref (queuesrcpad);
1249 gst_object_unref (queuesinkpad);
1250 gst_bin_remove (GST_BIN (decode_bin), queue);
1256 get_our_ghost_pad (GstDecodeBin * decode_bin, GstPad * pad)
1258 GstIterator *pad_it = NULL;
1259 GstPad *db_pad = NULL;
1260 gboolean done = FALSE;
1262 if (pad == NULL || !GST_PAD_IS_SRC (pad)) {
1263 GST_DEBUG_OBJECT (decode_bin, "pad NULL or not SRC pad");
1267 /* our ghostpads are the sourcepads */
1268 pad_it = gst_element_iterate_src_pads (GST_ELEMENT (decode_bin));
1271 switch (gst_iterator_next (pad_it, (gpointer) & db_pad)) {
1272 case GST_ITERATOR_OK:
1273 GST_DEBUG_OBJECT (decode_bin, "looking at pad %s:%s",
1274 GST_DEBUG_PAD_NAME (db_pad));
1275 if (GST_IS_GHOST_PAD (db_pad)) {
1276 GstPad *target_pad = NULL;
1278 target_pad = gst_ghost_pad_get_target (GST_GHOST_PAD (db_pad));
1279 done = (target_pad == pad);
1281 gst_object_unref (target_pad);
1284 /* Found our ghost pad */
1285 GST_DEBUG_OBJECT (decode_bin, "found ghostpad %s:%s for pad %s:%s",
1286 GST_DEBUG_PAD_NAME (db_pad), GST_DEBUG_PAD_NAME (pad));
1290 /* Not the right one */
1291 gst_object_unref (db_pad);
1293 case GST_ITERATOR_RESYNC:
1294 gst_iterator_resync (pad_it);
1296 case GST_ITERATOR_ERROR:
1299 case GST_ITERATOR_DONE:
1304 gst_iterator_free (pad_it);
1309 /* remove all downstream elements starting from the given pad.
1310 * Also make sure to remove the ghostpad we created for the raw
1314 remove_element_chain (GstDecodeBin * decode_bin, GstPad * pad)
1317 gboolean done = FALSE;
1319 GstElement *elem = GST_ELEMENT (GST_OBJECT_PARENT (pad));
1321 while (GST_OBJECT_PARENT (elem) &&
1322 GST_OBJECT_PARENT (elem) != GST_OBJECT (decode_bin))
1323 elem = GST_ELEMENT (GST_OBJECT_PARENT (elem));
1325 if (G_OBJECT_TYPE (elem) == decode_bin->queue_type) {
1326 GST_DEBUG_OBJECT (decode_bin,
1327 "Encountered demuxer output queue while removing element chain");
1328 decode_bin->queues = g_list_remove (decode_bin->queues, elem);
1331 GST_DEBUG_OBJECT (decode_bin, "%s:%s", GST_DEBUG_PAD_NAME (pad));
1332 iter = gst_pad_iterate_internal_links (pad);
1336 /* remove all elements linked to this pad up to the ghostpad
1337 * that we created for this stream */
1339 switch (gst_iterator_next (iter, &item)) {
1340 case GST_ITERATOR_OK:{
1345 pad = GST_PAD (item);
1346 GST_DEBUG_OBJECT (decode_bin, "inspecting internal pad %s:%s",
1347 GST_DEBUG_PAD_NAME (pad));
1349 ghostpad = get_our_ghost_pad (decode_bin, pad);
1351 GST_DEBUG_OBJECT (decode_bin, "found our ghost pad %s:%s for %s:%s",
1352 GST_DEBUG_PAD_NAME (ghostpad), GST_DEBUG_PAD_NAME (pad));
1354 g_signal_emit (G_OBJECT (decode_bin),
1355 gst_decode_bin_signals[SIGNAL_REMOVED_DECODED_PAD], 0, ghostpad);
1357 gst_element_remove_pad (GST_ELEMENT (decode_bin), ghostpad);
1358 gst_object_unref (ghostpad);
1361 GST_DEBUG_OBJECT (decode_bin, "not one of our ghostpads");
1364 peer = gst_pad_get_peer (pad);
1366 GstObject *parent = gst_pad_get_parent (peer);
1368 GST_DEBUG_OBJECT (decode_bin,
1369 "internal pad %s:%s linked to pad %s:%s",
1370 GST_DEBUG_PAD_NAME (pad), GST_DEBUG_PAD_NAME (peer));
1373 GstObject *grandparent = gst_object_get_parent (parent);
1375 if (grandparent != NULL) {
1376 if (GST_ELEMENT (grandparent) != GST_ELEMENT (decode_bin)) {
1377 GST_DEBUG_OBJECT (decode_bin, "dead end pad %s:%s parent %s",
1378 GST_DEBUG_PAD_NAME (peer), GST_OBJECT_NAME (grandparent));
1380 GST_DEBUG_OBJECT (decode_bin,
1381 "recursing element %s on pad %s:%s",
1382 GST_ELEMENT_NAME (elem), GST_DEBUG_PAD_NAME (pad));
1383 remove_element_chain (decode_bin, peer);
1385 gst_object_unref (grandparent);
1387 gst_object_unref (parent);
1389 gst_object_unref (peer);
1391 gst_object_unref (item);
1394 case GST_ITERATOR_RESYNC:
1395 gst_iterator_resync (iter);
1397 case GST_ITERATOR_ERROR:
1398 GST_ERROR_OBJECT (pad, "Could not iterate over internally linked pads");
1401 case GST_ITERATOR_DONE:
1406 GST_DEBUG_OBJECT (decode_bin, "removing %s", GST_ELEMENT_NAME (elem));
1408 gst_iterator_free (iter);
1411 gst_element_set_state (elem, GST_STATE_NULL);
1412 gst_bin_remove (GST_BIN (decode_bin), elem);
1415 /* there are @bytes bytes in @queue, enlarge it
1417 * Returns: new max number of bytes in @queue
1420 queue_enlarge (GstElement * queue, guint bytes, GstDecodeBin * decode_bin)
1422 /* Increase the queue size by 1Mbyte if it is over 1Mb, else double its current limit
1424 if (bytes > 1024 * 1024)
1425 bytes += 1024 * 1024;
1429 GST_DEBUG_OBJECT (decode_bin,
1430 "increasing queue %s max-size-bytes to %d", GST_ELEMENT_NAME (queue),
1432 g_object_set (G_OBJECT (queue), "max-size-bytes", bytes, NULL);
1437 /* this callback is called when our queues fills up or are empty
1438 * We then check the status of all other queues to make sure we
1439 * never have an empty and full queue at the same time since that
1440 * would block dataflow. In the case of a filled queue, we make
1444 queue_underrun_cb (GstElement * queue, GstDecodeBin * decode_bin)
1446 /* FIXME: we don't really do anything here for now. Ideally we should
1447 * see if some of the queues are filled and increase their values
1449 * Note: be very careful with thread safety here as this underrun
1450 * signal is done from the streaming thread of queue srcpad which
1451 * is different from the pad_added (where we add the queue to the
1452 * list) and the overrun signals that are signalled from the
1455 GST_DEBUG_OBJECT (decode_bin, "got underrun");
1458 /* Make sure we don't have a full queue and empty queue situation */
1460 queue_filled_cb (GstElement * queue, GstDecodeBin * decode_bin)
1463 gboolean increase = FALSE;
1466 /* get current byte level from the queue that is filled */
1467 g_object_get (G_OBJECT (queue), "current-level-bytes", &bytes, NULL);
1468 GST_DEBUG_OBJECT (decode_bin, "One of the queues is full at %d bytes", bytes);
1470 /* we do not buffer more than 20Mb */
1471 if (bytes > (20 * 1024 * 1024))
1474 /* check all other queue to see if one is empty, in that case
1475 * we need to enlarge @queue */
1476 for (tmp = decode_bin->queues; tmp; tmp = g_list_next (tmp)) {
1477 GstElement *aqueue = GST_ELEMENT (tmp->data);
1478 guint levelbytes = 0;
1480 if (aqueue != queue) {
1481 g_object_get (G_OBJECT (aqueue), "current-level-bytes", &levelbytes,
1483 if (levelbytes == 0) {
1484 /* yup, found an empty queue, we can stop the search and
1485 * need to enlarge the queue */
1493 /* enlarge @queue */
1494 queue_enlarge (queue, bytes, decode_bin);
1496 GST_DEBUG_OBJECT (decode_bin,
1497 "Queue is full but other queues are not empty, not doing anything");
1504 GST_WARNING_OBJECT (decode_bin,
1505 "Queue is bigger than 20Mbytes, something else is going wrong");
1510 /* This function will be called when a dynamic pad is created on an element.
1511 * We try to continue autoplugging on this new pad. */
1513 new_pad (GstElement * element, GstPad * pad, GstDynamic * dynamic)
1515 GstDecodeBin *decode_bin = dynamic->decode_bin;
1519 GST_OBJECT_LOCK (decode_bin);
1520 if (decode_bin->shutting_down)
1521 goto shutting_down1;
1522 GST_OBJECT_UNLOCK (decode_bin);
1524 GST_STATE_LOCK (decode_bin);
1525 if (decode_bin->shutting_down)
1526 goto shutting_down2;
1528 /* see if any more pending dynamic connections exist */
1529 more = gst_decode_bin_is_dynamic (decode_bin);
1531 caps = gst_pad_get_caps (pad);
1532 close_pad_link (element, pad, caps, decode_bin, more);
1534 gst_caps_unref (caps);
1535 GST_STATE_UNLOCK (decode_bin);
1541 GST_DEBUG_OBJECT (decode_bin, "we are shutting down");
1542 GST_OBJECT_UNLOCK (decode_bin);
1547 GST_DEBUG_OBJECT (decode_bin, "we are shutting down");
1548 GST_STATE_UNLOCK (decode_bin);
1554 dynamic_remove (GstDynamic * dynamic)
1556 GstDecodeBin *decode_bin = dynamic->decode_bin;
1558 /* remove the dynamic from the list of dynamics */
1559 decode_bin->dynamics = g_list_remove (decode_bin->dynamics, dynamic);
1560 dynamic_free (dynamic);
1562 /* if we have no more dynamic elements, we have no chance of creating
1563 * more pads, so we fire the no_more_pads signal */
1564 if (decode_bin->dynamics == NULL) {
1565 if (decode_bin->numwaiting == 0) {
1566 GST_DEBUG_OBJECT (decode_bin,
1567 "no more dynamic elements, removing fakesink");
1568 remove_fakesink (decode_bin);
1570 GST_DEBUG_OBJECT (decode_bin,
1571 "no more dynamic elements, signaling no_more_pads");
1572 gst_element_no_more_pads (GST_ELEMENT (decode_bin));
1574 GST_DEBUG_OBJECT (decode_bin, "we have more dynamic elements");
1578 /* this signal is fired when an element signals the no_more_pads signal.
1579 * This means that the element will not generate more dynamic pads and
1580 * we can remove the element from the list of dynamic elements. When we
1581 * have no more dynamic elements in the pipeline, we can fire a no_more_pads
1582 * signal ourselves. */
1584 no_more_pads (GstElement * element, GstDynamic * dynamic)
1586 GST_DEBUG_OBJECT (dynamic->decode_bin, "no more pads on element %s",
1587 GST_ELEMENT_NAME (element));
1589 dynamic_remove (dynamic);
1593 new_caps (GstPad * pad, GParamSpec * unused, GstDynamic * dynamic)
1595 GST_DEBUG_OBJECT (dynamic->decode_bin, "delayed link triggered");
1597 new_pad (dynamic->element, pad, dynamic);
1599 /* assume it worked and remove the dynamic */
1600 dynamic_remove (dynamic);
1606 is_our_kid (GstElement * e, GstDecodeBin * decode_bin)
1611 parent = (GstElement *) gst_object_get_parent ((GstObject *) e);
1612 ret = (parent == (GstElement *) decode_bin);
1615 gst_object_unref ((GstObject *) parent);
1621 elem_is_dynamic (GstElement * element, GstDecodeBin * decode_bin)
1625 /* loop over all the padtemplates */
1626 for (pads = GST_ELEMENT_GET_CLASS (element)->padtemplates; pads;
1627 pads = g_list_next (pads)) {
1628 GstPadTemplate *templ = GST_PAD_TEMPLATE (pads->data);
1629 const gchar *templ_name;
1631 /* we are only interested in source pads */
1632 if (GST_PAD_TEMPLATE_DIRECTION (templ) != GST_PAD_SRC)
1635 templ_name = GST_PAD_TEMPLATE_NAME_TEMPLATE (templ);
1636 GST_DEBUG_OBJECT (decode_bin, "got a source pad template %s", templ_name);
1638 /* figure out what kind of pad this is */
1639 switch (GST_PAD_TEMPLATE_PRESENCE (templ)) {
1640 case GST_PAD_SOMETIMES:
1642 /* try to get the pad to see if it is already created or
1644 GstPad *pad = gst_element_get_static_pad (element, templ_name);
1647 GST_DEBUG_OBJECT (decode_bin, "got the pad for sometimes template %s",
1649 gst_object_unref (pad);
1651 GST_DEBUG_OBJECT (decode_bin,
1652 "did not get the sometimes pad of template %s", templ_name);
1653 /* we have an element that will create dynamic pads */
1659 /* Don't care about ALWAYS or REQUEST pads */
1666 /* This function will be called when a pad is disconnected for some reason */
1668 unlinked (GstPad * pad, GstPad * peerpad, GstDecodeBin * decode_bin)
1670 GstElement *element, *peer;
1672 /* inactivate pad */
1673 gst_pad_set_active (pad, GST_ACTIVATE_NONE);
1675 peer = gst_pad_get_parent_element (peerpad);
1677 if (!is_our_kid (peer, decode_bin))
1680 GST_DEBUG_OBJECT (decode_bin, "pad %s:%s removal while alive - chained?",
1681 GST_DEBUG_PAD_NAME (pad));
1683 /* remove all elements linked to the peerpad */
1684 remove_element_chain (decode_bin, peerpad);
1686 /* Re-add as a dynamic element if needed, via close_link */
1687 element = gst_pad_get_parent_element (pad);
1689 if (elem_is_dynamic (element, decode_bin))
1690 dynamic_add (element, NULL, decode_bin);
1692 gst_object_unref (element);
1696 gst_object_unref (peer);
1699 /* this function inspects the given element and tries to connect something
1700 * on the srcpads. If there are dynamic pads, it sets up a signal handler to
1701 * continue autoplugging when they become available */
1703 close_link (GstElement * element, GstDecodeBin * decode_bin)
1706 gboolean dynamic = FALSE;
1707 GList *to_connect = NULL;
1710 GST_DEBUG_OBJECT (decode_bin, "closing links with element %s",
1711 GST_ELEMENT_NAME (element));
1713 /* loop over all the padtemplates */
1714 for (pads = GST_ELEMENT_GET_CLASS (element)->padtemplates; pads;
1715 pads = g_list_next (pads)) {
1716 GstPadTemplate *templ = GST_PAD_TEMPLATE (pads->data);
1717 const gchar *templ_name;
1719 /* we are only interested in source pads */
1720 if (GST_PAD_TEMPLATE_DIRECTION (templ) != GST_PAD_SRC)
1723 templ_name = GST_PAD_TEMPLATE_NAME_TEMPLATE (templ);
1724 GST_DEBUG_OBJECT (decode_bin, "got a source pad template %s", templ_name);
1726 /* figure out what kind of pad this is */
1727 switch (GST_PAD_TEMPLATE_PRESENCE (templ)) {
1728 case GST_PAD_ALWAYS:
1730 /* get the pad that we need to autoplug */
1731 GstPad *pad = gst_element_get_static_pad (element, templ_name);
1734 GST_DEBUG_OBJECT (decode_bin, "got the pad for always template %s",
1736 /* here is the pad, we need to autoplug it */
1737 to_connect = g_list_prepend (to_connect, pad);
1739 /* strange, pad is marked as always but it's not
1740 * there. Fix the element */
1741 GST_WARNING_OBJECT (decode_bin,
1742 "could not get the pad for always template %s", templ_name);
1746 case GST_PAD_SOMETIMES:
1748 /* try to get the pad to see if it is already created or
1750 GstPad *pad = gst_element_get_static_pad (element, templ_name);
1753 GST_DEBUG_OBJECT (decode_bin, "got the pad for sometimes template %s",
1755 /* the pad is created, we need to autoplug it */
1756 to_connect = g_list_prepend (to_connect, pad);
1758 GST_DEBUG_OBJECT (decode_bin,
1759 "did not get the sometimes pad of template %s", templ_name);
1760 /* we have an element that will create dynamic pads */
1765 case GST_PAD_REQUEST:
1766 /* ignore request pads */
1767 GST_DEBUG_OBJECT (decode_bin, "ignoring request padtemplate %s",
1773 GST_DEBUG_OBJECT (decode_bin, "got a dynamic element here");
1774 /* ok, this element has dynamic pads, set up the signal handlers to be
1775 * notified of them */
1777 dynamic_add (element, NULL, decode_bin);
1780 /* Check if this is an element with more than 1 pad. If this element
1781 * has more than 1 pad, we need to be careful not to signal the
1782 * no_more_pads signal after connecting the first pad. */
1783 more = g_list_length (to_connect) > 1;
1785 /* now loop over all the pads we need to connect */
1786 for (pads = to_connect; pads; pads = g_list_next (pads)) {
1787 GstPad *pad = GST_PAD_CAST (pads->data);
1790 /* we have more pads if we have more than 1 pad to connect or
1791 * dynamics. If we have only 1 pad and no dynamics, more will be
1792 * set to FALSE and the no-more-pads signal will be fired. Note
1793 * that this can change after the close_pad_link call. */
1794 more |= gst_decode_bin_is_dynamic (decode_bin);
1796 GST_DEBUG_OBJECT (decode_bin, "closing pad link for %s",
1797 GST_OBJECT_NAME (pad));
1799 /* continue autoplugging on the pads */
1800 caps = gst_pad_get_caps (pad);
1801 close_pad_link (element, pad, caps, decode_bin, more);
1803 gst_caps_unref (caps);
1805 gst_object_unref (pad);
1807 g_list_free (to_connect);
1810 /* this is the signal handler for the typefind element have_type signal.
1811 * It tries to continue autoplugging on the typefind src pad */
1813 type_found (GstElement * typefind, guint probability, GstCaps * caps,
1814 GstDecodeBin * decode_bin)
1819 GST_DEBUG_OBJECT (decode_bin, "typefind found caps %" GST_PTR_FORMAT, caps);
1821 GST_OBJECT_LOCK (decode_bin);
1822 if (decode_bin->shutting_down)
1824 GST_OBJECT_UNLOCK (decode_bin);
1826 GST_STATE_LOCK (decode_bin);
1827 if (decode_bin->shutting_down)
1830 /* don't need the typefind anymore if we already found a type, we're not going
1831 * to be able to do anything with it anyway except for generating errors */
1832 if (decode_bin->have_type)
1835 decode_bin->have_type = TRUE;
1837 /* special-case text/plain: we only want to accept it as a raw type if it
1838 * comes from a subtitle parser element or a demuxer, but not if it is the
1839 * type of the entire stream, in which case we just want to error out */
1840 if (typefind == decode_bin->typefind &&
1841 gst_structure_has_name (gst_caps_get_structure (caps, 0), "text/plain")) {
1842 gst_element_no_more_pads (GST_ELEMENT (decode_bin));
1843 /* we can't handle this type of stream */
1844 GST_ELEMENT_ERROR (decode_bin, STREAM, WRONG_TYPE,
1845 (_("This appears to be a text file")),
1846 ("decodebin cannot decode plain text files"));
1850 /* autoplug the new pad with the caps that the signal gave us. */
1851 pad = gst_element_get_static_pad (typefind, "src");
1852 close_pad_link (typefind, pad, caps, decode_bin, FALSE);
1853 gst_object_unref (pad);
1855 dynamic = gst_decode_bin_is_dynamic (decode_bin);
1856 if (dynamic == FALSE) {
1857 GST_DEBUG_OBJECT (decode_bin, "we have no dynamic elements anymore");
1858 /* if we have no dynamic elements, we know that no new pads
1859 * will be created and we can signal out no_more_pads signal */
1860 gst_element_no_more_pads (GST_ELEMENT (decode_bin));
1862 /* more dynamic elements exist that could create new pads */
1863 GST_DEBUG_OBJECT (decode_bin, "we have more dynamic elements");
1867 GST_STATE_UNLOCK (decode_bin);
1872 GST_DEBUG_OBJECT (decode_bin, "we are shutting down");
1873 GST_OBJECT_UNLOCK (decode_bin);
1879 disconnect_unlinked_signals (GstDecodeBin * decode_bin, GstElement * element)
1881 GstIterator *pad_it = NULL;
1882 gboolean done = FALSE;
1884 pad_it = gst_element_iterate_src_pads (element);
1888 switch (gst_iterator_next (pad_it, (gpointer) & pad)) {
1889 case GST_ITERATOR_OK:
1890 g_signal_handlers_disconnect_by_func (pad, (gpointer) unlinked,
1892 gst_object_unref (pad);
1894 case GST_ITERATOR_RESYNC:
1895 gst_iterator_resync (pad_it);
1902 gst_iterator_free (pad_it);
1907 cleanup_decodebin (GstDecodeBin * decode_bin)
1909 GstIterator *elem_it = NULL, *gpad_it = NULL;
1910 GstPad *typefind_pad = NULL;
1911 gboolean done = FALSE;
1913 g_return_if_fail (GST_IS_DECODE_BIN (decode_bin));
1915 GST_DEBUG_OBJECT (decode_bin, "cleaning up decodebin");
1917 typefind_pad = gst_element_get_static_pad (decode_bin->typefind, "src");
1918 if (GST_IS_PAD (typefind_pad)) {
1919 g_signal_handlers_block_by_func (typefind_pad, (gpointer) unlinked,
1923 elem_it = gst_bin_iterate_elements (GST_BIN (decode_bin));
1925 GstElement *element = NULL;
1927 switch (gst_iterator_next (elem_it, (gpointer) & element)) {
1928 case GST_ITERATOR_OK:
1929 if (element != decode_bin->typefind && element != decode_bin->fakesink) {
1930 GST_DEBUG_OBJECT (element, "removing autoplugged element");
1931 disconnect_unlinked_signals (decode_bin, element);
1932 gst_element_set_state (element, GST_STATE_NULL);
1933 gst_bin_remove (GST_BIN (decode_bin), element);
1935 gst_object_unref (element);
1937 case GST_ITERATOR_RESYNC:
1938 gst_iterator_resync (elem_it);
1940 case GST_ITERATOR_ERROR:
1943 case GST_ITERATOR_DONE:
1948 gst_iterator_free (elem_it);
1951 gpad_it = gst_element_iterate_pads (GST_ELEMENT (decode_bin));
1955 switch (gst_iterator_next (gpad_it, (gpointer) & pad)) {
1956 case GST_ITERATOR_OK:
1957 GST_DEBUG_OBJECT (pad, "inspecting pad %s:%s",
1958 GST_DEBUG_PAD_NAME (pad));
1959 if (GST_IS_GHOST_PAD (pad) && GST_PAD_IS_SRC (pad)) {
1960 GST_DEBUG_OBJECT (pad, "removing ghost pad");
1961 gst_element_remove_pad (GST_ELEMENT (decode_bin), pad);
1963 gst_object_unref (pad);
1965 case GST_ITERATOR_RESYNC:
1966 gst_iterator_resync (gpad_it);
1968 case GST_ITERATOR_ERROR:
1971 case GST_ITERATOR_DONE:
1976 gst_iterator_free (gpad_it);
1978 if (GST_IS_PAD (typefind_pad)) {
1979 g_signal_handlers_unblock_by_func (typefind_pad, (gpointer) unlinked,
1981 g_signal_handlers_disconnect_by_func (typefind_pad, (gpointer) unlinked,
1983 gst_object_unref (typefind_pad);
1987 static GstStateChangeReturn
1988 gst_decode_bin_change_state (GstElement * element, GstStateChange transition)
1990 GstStateChangeReturn ret;
1991 GstDecodeBin *decode_bin;
1993 decode_bin = GST_DECODE_BIN (element);
1995 switch (transition) {
1996 case GST_STATE_CHANGE_NULL_TO_READY:
1997 decode_bin->numpads = 0;
1998 decode_bin->numwaiting = 0;
1999 decode_bin->dynamics = NULL;
2000 if (decode_bin->typefind == NULL)
2001 goto missing_typefind;
2003 case GST_STATE_CHANGE_READY_TO_PAUSED:
2004 GST_OBJECT_LOCK (decode_bin);
2005 decode_bin->shutting_down = FALSE;
2006 decode_bin->have_type = FALSE;
2007 GST_OBJECT_UNLOCK (decode_bin);
2009 if (!add_fakesink (decode_bin))
2010 goto missing_fakesink;
2012 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
2013 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
2015 case GST_STATE_CHANGE_PAUSED_TO_READY:
2016 GST_OBJECT_LOCK (decode_bin);
2017 decode_bin->shutting_down = TRUE;
2018 GST_OBJECT_UNLOCK (decode_bin);
2024 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
2025 if (ret == GST_STATE_CHANGE_FAILURE)
2028 switch (transition) {
2029 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
2031 case GST_STATE_CHANGE_PAUSED_TO_READY:
2032 case GST_STATE_CHANGE_READY_TO_NULL:
2033 free_dynamics (decode_bin);
2034 free_pad_probes (decode_bin);
2035 cleanup_decodebin (decode_bin);
2045 gst_element_post_message (element,
2046 gst_missing_element_message_new (element, "typefind"));
2047 GST_ELEMENT_ERROR (element, CORE, MISSING_PLUGIN, (NULL), ("no typefind!"));
2048 return GST_STATE_CHANGE_FAILURE;
2052 gst_element_post_message (element,
2053 gst_missing_element_message_new (element, "fakesink"));
2054 GST_ELEMENT_ERROR (element, CORE, MISSING_PLUGIN, (NULL), ("no fakesink!"));
2055 return GST_STATE_CHANGE_FAILURE;
2060 plugin_init (GstPlugin * plugin)
2062 GST_DEBUG_CATEGORY_INIT (gst_decode_bin_debug, "decodebin", 0, "decoder bin");
2065 GST_DEBUG ("binding text domain %s to locale dir %s", GETTEXT_PACKAGE,
2067 bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
2068 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
2069 #endif /* ENABLE_NLS */
2071 return gst_element_register (plugin, "decodebin", GST_RANK_NONE,
2072 GST_TYPE_DECODE_BIN);
2075 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
2078 "decoder bin", plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME,