2 * Copyright (C) <2004> Wim Taymans <wim.taymans@gmail.com>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * SECTION:element-decodebin
23 * #GstBin that auto-magically constructs a decoding pipeline using available
24 * decoders and demuxers via auto-plugging.
26 * When using decodebin in your application, connect a signal handler to
27 * #GstDecodeBin::new-decoded-pad and connect your sinks from within the
31 * This element is deprecated and no longer supported. You should use the
32 * #uridecodebin or #decodebin2 element instead (or, even better: #playbin2).
35 * Deprecated: use uridecodebin or decodebin2 instead.
42 #include <gst/gst-i18n-plugin.h>
46 #include <gst/pbutils/pbutils.h>
48 #include "gstplay-marshal.h"
50 /* generic templates */
51 static GstStaticPadTemplate decoder_bin_sink_template =
52 GST_STATIC_PAD_TEMPLATE ("sink",
57 static GstStaticPadTemplate decoder_bin_src_template =
58 GST_STATIC_PAD_TEMPLATE ("src%d",
63 GST_DEBUG_CATEGORY_STATIC (gst_decode_bin_debug);
64 #define GST_CAT_DEFAULT gst_decode_bin_debug
66 #define GST_TYPE_DECODE_BIN (gst_decode_bin_get_type())
67 #define GST_DECODE_BIN(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_DECODE_BIN,GstDecodeBin))
68 #define GST_DECODE_BIN_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_DECODE_BIN,GstDecodeBinClass))
69 #define GST_IS_DECODE_BIN(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_DECODE_BIN))
70 #define GST_IS_DECODE_BIN_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_DECODE_BIN))
72 typedef struct _GstDecodeBin GstDecodeBin;
73 typedef struct _GstDecodeBinClass GstDecodeBinClass;
78 * Auto-plugging decoder element structure
82 GstBin bin; /* we extend GstBin */
84 GstElement *typefind; /* this holds the typefind object */
87 GList *dynamics; /* list of dynamic connections */
89 GList *queues; /* list of demuxer-decoder queues */
91 GList *probes; /* list of PadProbeData */
93 GList *factories; /* factories we can use for selecting elements */
98 guint have_type_id; /* signal id for the typefind element */
100 gboolean shutting_down; /* stop pluggin if we're shutting down */
102 GType queue_type; /* store the GType of queues, to aid in recognising them */
104 GMutex *cb_mutex; /* Mutex for multi-threaded callbacks, such as removing the fakesink */
107 struct _GstDecodeBinClass
109 GstBinClass parent_class;
111 /* signal we fire when a new pad has been decoded into raw audio/video */
112 void (*new_decoded_pad) (GstElement * element, GstPad * pad, gboolean last);
113 /* signal we fire when a pad has been removed */
114 void (*removed_decoded_pad) (GstElement * element, GstPad * pad);
115 /* signal fired when we found a pad that we cannot decode */
116 void (*unknown_type) (GstElement * element, GstPad * pad, GstCaps * caps);
122 SIGNAL_NEW_DECODED_PAD,
123 SIGNAL_REMOVED_DECODED_PAD,
144 /* this structure is created for all dynamic pads that could get created
148 GstDecodeBin *decode_bin; /* pointer to ourself */
150 GstElement *element; /* the element sending the signal */
151 gint np_sig_id; /* signal id of new_pad */
152 gint nmp_sig_id; /* signal id of no_more_pads */
154 GstPad *pad; /* the pad sending the signal */
155 gint caps_sig_id; /* signal id of caps */
159 static void gst_decode_bin_class_init (GstDecodeBinClass * klass);
160 static void gst_decode_bin_init (GstDecodeBin * decode_bin);
161 static void gst_decode_bin_set_property (GObject * object, guint prop_id,
162 const GValue * value, GParamSpec * pspec);
163 static void gst_decode_bin_get_property (GObject * object, guint prop_id,
164 GValue * value, GParamSpec * pspec);
165 static void gst_decode_bin_dispose (GObject * object);
166 static void gst_decode_bin_finalize (GObject * object);
168 static GstStateChangeReturn gst_decode_bin_change_state (GstElement * element,
169 GstStateChange transition);
171 static gboolean add_fakesink (GstDecodeBin * decode_bin);
172 static void remove_fakesink (GstDecodeBin * decode_bin);
174 static void dynamic_free (GstDynamic * dyn);
175 static void free_dynamics (GstDecodeBin * decode_bin);
176 static void type_found (GstElement * typefind, guint probability,
177 GstCaps * caps, GstDecodeBin * decode_bin);
178 static GstElement *try_to_link_1 (GstDecodeBin * decode_bin,
179 GstElement * origelement, GstPad * pad, GList * factories);
180 static void close_link (GstElement * element, GstDecodeBin * decode_bin);
181 static void close_pad_link (GstElement * element, GstPad * pad,
182 GstCaps * caps, GstDecodeBin * decode_bin, gboolean more);
183 static void unlinked (GstPad * pad, GstPad * peerpad,
184 GstDecodeBin * decode_bin);
185 static void new_pad (GstElement * element, GstPad * pad, GstDynamic * dynamic);
186 static void no_more_pads (GstElement * element, GstDynamic * dynamic);
187 static void new_caps (GstPad * pad, GParamSpec * unused, GstDynamic * dynamic);
189 static void queue_filled_cb (GstElement * queue, GstDecodeBin * decode_bin);
190 static void queue_underrun_cb (GstElement * queue, GstDecodeBin * decode_bin);
192 static gboolean is_demuxer_element (GstElement * srcelement);
194 static GstElementClass *parent_class;
195 static guint gst_decode_bin_signals[LAST_SIGNAL] = { 0 };
199 gst_decode_bin_get_type (void)
201 static GType gst_decode_bin_type = 0;
203 if (!gst_decode_bin_type) {
204 static const GTypeInfo gst_decode_bin_info = {
205 sizeof (GstDecodeBinClass),
208 (GClassInitFunc) gst_decode_bin_class_init,
211 sizeof (GstDecodeBin),
213 (GInstanceInitFunc) gst_decode_bin_init,
217 gst_decode_bin_type =
218 g_type_register_static (GST_TYPE_BIN, "GstDecodeBin",
219 &gst_decode_bin_info, 0);
222 return gst_decode_bin_type;
226 gst_decode_bin_class_init (GstDecodeBinClass * klass)
228 GObjectClass *gobject_klass;
229 GstElementClass *gstelement_klass;
231 gobject_klass = (GObjectClass *) klass;
232 gstelement_klass = (GstElementClass *) klass;
234 parent_class = g_type_class_peek_parent (klass);
236 gobject_klass->set_property = gst_decode_bin_set_property;
237 gobject_klass->get_property = gst_decode_bin_get_property;
238 gobject_klass->dispose = gst_decode_bin_dispose;
239 gobject_klass->finalize = gst_decode_bin_finalize;
242 * GstDecodeBin::new-decoded-pad:
243 * @bin: The decodebin
244 * @pad: The newly created pad
245 * @islast: #TRUE if this is the last pad to be added. Deprecated.
247 * This signal gets emitted as soon as a new pad of the same type as one of
248 * the valid 'raw' types is added.
250 gst_decode_bin_signals[SIGNAL_NEW_DECODED_PAD] =
251 g_signal_new ("new-decoded-pad", G_TYPE_FROM_CLASS (klass),
253 G_STRUCT_OFFSET (GstDecodeBinClass, new_decoded_pad), NULL, NULL,
254 gst_play_marshal_VOID__OBJECT_BOOLEAN, G_TYPE_NONE, 2, GST_TYPE_PAD,
257 * GstDecodeBin::removed-decoded-pad:
258 * @bin: The decodebin
259 * @pad: The pad that was removed
261 * This signal is emitted when a 'final' caps pad has been removed.
263 gst_decode_bin_signals[SIGNAL_REMOVED_DECODED_PAD] =
264 g_signal_new ("removed-decoded-pad", G_TYPE_FROM_CLASS (klass),
266 G_STRUCT_OFFSET (GstDecodeBinClass, removed_decoded_pad), NULL, NULL,
267 gst_marshal_VOID__OBJECT, G_TYPE_NONE, 1, GST_TYPE_PAD);
269 * GstDecodeBin::unknown-type:
270 * @bin: The decodebin
271 * @pad: The new pad containing caps that cannot be resolved to a 'final'
273 * @caps: The #GstCaps of the pad that cannot be resolved.
275 * This signal is emitted when a pad for which there is no further possible
276 * decoding is added to the decodebin.
278 gst_decode_bin_signals[SIGNAL_UNKNOWN_TYPE] =
279 g_signal_new ("unknown-type", G_TYPE_FROM_CLASS (klass),
280 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstDecodeBinClass, unknown_type),
281 NULL, NULL, gst_marshal_VOID__OBJECT_BOXED, G_TYPE_NONE, 2,
282 GST_TYPE_PAD, GST_TYPE_CAPS);
284 g_object_class_install_property (gobject_klass, PROP_SINK_CAPS,
285 g_param_spec_boxed ("sink-caps", "Sink Caps",
286 "The caps of the input data. (NULL = use typefind element)",
287 GST_TYPE_CAPS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
289 gst_element_class_add_pad_template (gstelement_klass,
290 gst_static_pad_template_get (&decoder_bin_sink_template));
291 gst_element_class_add_pad_template (gstelement_klass,
292 gst_static_pad_template_get (&decoder_bin_src_template));
294 gst_element_class_set_details_simple (gstelement_klass,
295 "Decoder Bin", "Generic/Bin/Decoder",
296 "Autoplug and decode to raw media",
297 "Wim Taymans <wim.taymans@gmail.com>");
299 gstelement_klass->change_state =
300 GST_DEBUG_FUNCPTR (gst_decode_bin_change_state);
303 /* check if the bin is dynamic.
305 * If there are no outstanding dynamic connections, the bin is
306 * considered to be non-dynamic.
309 gst_decode_bin_is_dynamic (GstDecodeBin * decode_bin)
311 return decode_bin->dynamics != NULL;
314 /* the filter function for selecting the elements we can use in
317 gst_decode_bin_factory_filter (GstPluginFeature * feature,
318 GstDecodeBin * decode_bin)
323 /* we only care about element factories */
324 if (!GST_IS_ELEMENT_FACTORY (feature))
327 klass = gst_element_factory_get_klass (GST_ELEMENT_FACTORY (feature));
328 /* only demuxers, decoders and parsers can play */
329 if (strstr (klass, "Demux") == NULL &&
330 strstr (klass, "Decoder") == NULL && strstr (klass, "Parse") == NULL &&
331 strstr (klass, "Depayloader") == NULL) {
335 /* only select elements with autoplugging rank */
336 rank = gst_plugin_feature_get_rank (feature);
337 if (rank < GST_RANK_MARGINAL)
343 /* function used to sort element features */
345 compare_ranks (GstPluginFeature * f1, GstPluginFeature * f2)
348 const gchar *rname1, *rname2;
350 diff = gst_plugin_feature_get_rank (f2) - gst_plugin_feature_get_rank (f1);
354 rname1 = gst_plugin_feature_get_name (f1);
355 rname2 = gst_plugin_feature_get_name (f2);
357 diff = strcmp (rname2, rname1);
363 print_feature (GstPluginFeature * feature)
367 rname = gst_plugin_feature_get_name (feature);
369 GST_DEBUG ("%s", rname);
373 gst_decode_bin_init (GstDecodeBin * decode_bin)
377 decode_bin->cb_mutex = g_mutex_new ();
379 /* first filter out the interesting element factories */
380 factories = gst_default_registry_feature_filter (
381 (GstPluginFeatureFilter) gst_decode_bin_factory_filter,
384 /* sort them according to their ranks */
385 decode_bin->factories = g_list_sort (factories, (GCompareFunc) compare_ranks);
386 /* do some debugging */
387 g_list_foreach (decode_bin->factories, (GFunc) print_feature, NULL);
389 /* we create the typefind element only once */
390 decode_bin->typefind = gst_element_factory_make ("typefind", "typefind");
391 if (!decode_bin->typefind) {
392 g_warning ("can't find typefind element, decodebin will not work");
395 GstPadTemplate *pad_tmpl;
397 /* add the typefind element */
398 if (!gst_bin_add (GST_BIN (decode_bin), decode_bin->typefind)) {
399 g_warning ("Could not add typefind element, decodebin will not work");
400 gst_object_unref (decode_bin->typefind);
401 decode_bin->typefind = NULL;
404 /* get the sinkpad */
405 pad = gst_element_get_static_pad (decode_bin->typefind, "sink");
407 /* get the pad template */
408 pad_tmpl = gst_static_pad_template_get (&decoder_bin_sink_template);
410 /* ghost the sink pad to ourself */
411 gpad = gst_ghost_pad_new_from_template ("sink", pad, pad_tmpl);
412 gst_pad_set_active (gpad, TRUE);
413 gst_element_add_pad (GST_ELEMENT (decode_bin), gpad);
415 gst_object_unref (pad_tmpl);
416 gst_object_unref (pad);
418 /* connect a signal to find out when the typefind element found
420 decode_bin->have_type_id =
421 g_signal_connect (G_OBJECT (decode_bin->typefind), "have_type",
422 G_CALLBACK (type_found), decode_bin);
424 add_fakesink (decode_bin);
426 decode_bin->dynamics = NULL;
427 decode_bin->queues = NULL;
428 decode_bin->probes = NULL;
432 gst_decode_bin_dispose (GObject * object)
434 GstDecodeBin *decode_bin;
436 decode_bin = GST_DECODE_BIN (object);
438 if (decode_bin->factories)
439 gst_plugin_feature_list_free (decode_bin->factories);
440 decode_bin->factories = NULL;
442 G_OBJECT_CLASS (parent_class)->dispose (object);
444 /* our parent dispose might trigger new signals when pads are unlinked
445 * etc. clean up the mess here. */
446 /* FIXME do proper cleanup when going to NULL */
447 free_dynamics (decode_bin);
451 gst_decode_bin_set_sink_caps (GstDecodeBin * dbin, GstCaps * caps)
453 GST_DEBUG_OBJECT (dbin, "Setting new caps: %" GST_PTR_FORMAT, caps);
455 g_object_set (dbin->typefind, "force-caps", caps, NULL);
459 gst_decode_bin_get_sink_caps (GstDecodeBin * dbin)
463 GST_DEBUG_OBJECT (dbin, "Getting currently set caps");
465 g_object_get (dbin->typefind, "force-caps", &caps, NULL);
471 gst_decode_bin_set_property (GObject * object, guint prop_id,
472 const GValue * value, GParamSpec * pspec)
476 dbin = GST_DECODE_BIN (object);
480 gst_decode_bin_set_sink_caps (dbin, g_value_get_boxed (value));
483 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
489 gst_decode_bin_get_property (GObject * object, guint prop_id,
490 GValue * value, GParamSpec * pspec)
494 dbin = GST_DECODE_BIN (object);
497 g_value_take_boxed (value, gst_decode_bin_get_sink_caps (dbin));
500 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
506 gst_decode_bin_finalize (GObject * object)
508 GstDecodeBin *decode_bin = GST_DECODE_BIN (object);
510 g_mutex_free (decode_bin->cb_mutex);
512 G_OBJECT_CLASS (parent_class)->finalize (object);
522 find_dynamic (GstDynamic * dyn, struct DynFind *info)
524 if (dyn->element == info->elem && dyn->pad == info->pad)
529 /* Add either an element (for dynamic pads/pad-added watching) or a
530 * pad (for delayed caps/notify::caps watching) to the dynamic list,
531 * taking care to ignore repeat entries so we don't end up handling a
532 * pad twice, for example */
534 dynamic_add (GstElement * element, GstPad * pad, GstDecodeBin * decode_bin)
537 struct DynFind find_info;
540 g_return_if_fail (element != NULL);
542 /* do a search that this entry doesn't already exist */
543 find_info.elem = element;
545 found = g_list_find_custom (decode_bin->dynamics, &find_info,
546 (GCompareFunc) find_dynamic);
551 dyn = g_new0 (GstDynamic, 1);
552 dyn->element = gst_object_ref (element);
553 dyn->decode_bin = gst_object_ref (decode_bin);
555 dyn->pad = gst_object_ref (pad);
556 GST_DEBUG_OBJECT (decode_bin, "dynamic create for pad %" GST_PTR_FORMAT,
558 dyn->caps_sig_id = g_signal_connect (G_OBJECT (pad), "notify::caps",
559 G_CALLBACK (new_caps), dyn);
561 GST_DEBUG_OBJECT (decode_bin, "dynamic create for element %"
562 GST_PTR_FORMAT, element);
563 dyn->np_sig_id = g_signal_connect (G_OBJECT (element), "pad-added",
564 G_CALLBACK (new_pad), dyn);
565 dyn->nmp_sig_id = g_signal_connect (G_OBJECT (element), "no-more-pads",
566 G_CALLBACK (no_more_pads), dyn);
569 /* and add this element to the dynamic elements */
570 decode_bin->dynamics = g_list_prepend (decode_bin->dynamics, dyn);
575 GST_DEBUG_OBJECT (decode_bin, "Dynamic element already added: %"
576 GST_PTR_FORMAT, element);
578 GST_DEBUG_OBJECT (decode_bin, "Dynamic pad already added: %"
579 GST_PTR_FORMAT, pad);
584 dynamic_free (GstDynamic * dyn)
586 GST_DEBUG_OBJECT (dyn->decode_bin, "dynamic free");
588 /* disconnect signals */
590 g_signal_handler_disconnect (G_OBJECT (dyn->element), dyn->np_sig_id);
592 g_signal_handler_disconnect (G_OBJECT (dyn->element), dyn->nmp_sig_id);
593 if (dyn->caps_sig_id)
594 g_signal_handler_disconnect (G_OBJECT (dyn->pad), dyn->caps_sig_id);
597 gst_object_unref (dyn->pad);
600 gst_object_unref (dyn->element);
603 gst_object_unref (dyn->decode_bin);
604 dyn->decode_bin = NULL;
610 free_dynamics (GstDecodeBin * decode_bin)
614 for (dyns = decode_bin->dynamics; dyns; dyns = g_list_next (dyns)) {
615 GstDynamic *dynamic = (GstDynamic *) dyns->data;
617 dynamic_free (dynamic);
619 g_list_free (decode_bin->dynamics);
620 decode_bin->dynamics = NULL;
623 /* this function runs through the element factories and returns a list
624 * of all elements that are able to sink the given caps
627 find_compatibles (GstDecodeBin * decode_bin, const GstCaps * caps)
630 GList *to_try = NULL;
632 /* loop over all the factories */
633 for (factories = decode_bin->factories; factories;
634 factories = g_list_next (factories)) {
635 GstElementFactory *factory = GST_ELEMENT_FACTORY (factories->data);
636 const GList *templates;
639 /* get the templates from the element factory */
640 templates = gst_element_factory_get_static_pad_templates (factory);
641 for (walk = (GList *) templates; walk; walk = g_list_next (walk)) {
642 GstStaticPadTemplate *templ = walk->data;
644 /* we only care about the sink templates */
645 if (templ->direction == GST_PAD_SINK) {
646 gboolean can_intersect;
649 /* try to intersect the caps with the caps of the template */
650 tmpl_caps = gst_static_caps_get (&templ->static_caps);
652 can_intersect = gst_caps_can_intersect (caps, tmpl_caps);
653 gst_caps_unref (tmpl_caps);
655 /* check if the intersection is empty */
657 /* non empty intersection, we can use this element */
658 to_try = g_list_prepend (to_try, factory);
664 to_try = g_list_reverse (to_try);
670 mimetype_is_raw (const gchar * mimetype)
672 return g_str_has_prefix (mimetype, "video/x-raw") ||
673 g_str_has_prefix (mimetype, "audio/x-raw") ||
674 g_str_has_prefix (mimetype, "text/plain") ||
675 g_str_has_prefix (mimetype, "text/x-pango-markup");
679 free_pad_probes (GstDecodeBin * decode_bin)
683 /* Remove pad probes */
684 for (tmp = decode_bin->probes; tmp; tmp = g_list_next (tmp)) {
685 PadProbeData *data = (PadProbeData *) tmp->data;
687 gst_pad_remove_data_probe (data->pad, data->sigid);
690 g_list_free (decode_bin->probes);
691 decode_bin->probes = NULL;
694 /* used when we need to remove a probe because the decoder we plugged failed
697 free_pad_probe_for_element (GstDecodeBin * decode_bin, GstElement * element)
701 for (l = decode_bin->probes; l != NULL; l = g_list_next (l)) {
702 PadProbeData *data = (PadProbeData *) l->data;
704 if (GST_ELEMENT_CAST (GST_PAD_PARENT (data->pad)) == element) {
705 gst_pad_remove_data_probe (data->pad, data->sigid);
706 decode_bin->probes = g_list_delete_link (decode_bin->probes, l);
714 add_fakesink (GstDecodeBin * decode_bin)
716 if (decode_bin->fakesink != NULL)
719 g_mutex_lock (decode_bin->cb_mutex);
721 decode_bin->fakesink = gst_element_factory_make ("fakesink", "fakesink");
722 if (!decode_bin->fakesink)
725 /* hacky, remove sink flag, we don't want our decodebin to become a sink
726 * just because we add a fakesink element to make us ASYNC */
727 GST_OBJECT_FLAG_UNSET (decode_bin->fakesink, GST_ELEMENT_IS_SINK);
729 /* takes ownership */
730 if (!gst_bin_add (GST_BIN (decode_bin), decode_bin->fakesink)) {
731 g_warning ("Could not add fakesink element, decodebin will not work");
732 gst_object_unref (decode_bin->fakesink);
733 decode_bin->fakesink = NULL;
735 g_mutex_unlock (decode_bin->cb_mutex);
741 g_warning ("can't find fakesink element, decodebin will not work");
742 g_mutex_unlock (decode_bin->cb_mutex);
748 remove_fakesink (GstDecodeBin * decode_bin)
750 gboolean removed_fakesink = FALSE;
752 if (decode_bin->fakesink == NULL)
755 g_mutex_lock (decode_bin->cb_mutex);
756 if (decode_bin->fakesink) {
757 GST_DEBUG_OBJECT (decode_bin, "Removing fakesink and marking state dirty");
759 /* Lock the state to prevent it from changing state to non-NULL
760 * before it's removed */
761 gst_element_set_locked_state (decode_bin->fakesink, TRUE);
762 /* setting the state to NULL is never async */
763 gst_element_set_state (decode_bin->fakesink, GST_STATE_NULL);
764 gst_bin_remove (GST_BIN (decode_bin), decode_bin->fakesink);
765 decode_bin->fakesink = NULL;
767 removed_fakesink = TRUE;
769 g_mutex_unlock (decode_bin->cb_mutex);
771 if (removed_fakesink) {
772 free_pad_probes (decode_bin);
776 /* this should be implemented with _pad_block() */
778 pad_probe (GstPad * pad, GstMiniObject * data, GstDecodeBin * decode_bin)
781 gboolean alldone = TRUE;
783 for (tmp = decode_bin->probes; tmp; tmp = g_list_next (tmp)) {
784 PadProbeData *pdata = (PadProbeData *) tmp->data;
786 if (pdata->pad == pad) {
787 if (GST_IS_BUFFER (data)) {
789 decode_bin->numwaiting--;
791 } else if (GST_IS_EVENT (data) &&
792 ((GST_EVENT_TYPE (data) == GST_EVENT_EOS) ||
793 (GST_EVENT_TYPE (data) == GST_EVENT_TAG) ||
794 (GST_EVENT_TYPE (data) == GST_EVENT_FLUSH_START))) {
795 /* FIXME, what about NEWSEGMENT? really, use _pad_block()... */
797 decode_bin->numwaiting--;
802 if (!(pdata->done)) {
803 GST_LOG_OBJECT (decode_bin, "Pad probe on pad %" GST_PTR_FORMAT
804 " but pad %" GST_PTR_FORMAT " still needs data.", pad, pdata->pad);
809 remove_fakesink (decode_bin);
813 /* FIXME: this should be somehow merged with the queue code in
814 * try_to_link_1() to reduce code duplication */
816 add_raw_queue (GstDecodeBin * decode_bin, GstPad * pad)
818 GstElement *queue = NULL;
819 GstPad *queuesinkpad = NULL, *queuesrcpad = NULL;
821 queue = gst_element_factory_make ("queue", NULL);
822 decode_bin->queue_type = G_OBJECT_TYPE (queue);
824 g_object_set (G_OBJECT (queue), "max-size-buffers", 0, NULL);
825 g_object_set (G_OBJECT (queue), "max-size-time", G_GINT64_CONSTANT (0), NULL);
826 g_object_set (G_OBJECT (queue), "max-size-bytes", 8192, NULL);
827 gst_bin_add (GST_BIN (decode_bin), queue);
828 gst_element_set_state (queue, GST_STATE_READY);
829 queuesinkpad = gst_element_get_static_pad (queue, "sink");
830 queuesrcpad = gst_element_get_static_pad (queue, "src");
832 if (gst_pad_link (pad, queuesinkpad) != GST_PAD_LINK_OK) {
833 GST_WARNING_OBJECT (decode_bin,
834 "Linking queue failed, trying without queue");
835 gst_element_set_state (queue, GST_STATE_NULL);
836 gst_object_unref (queuesrcpad);
837 gst_object_unref (queuesinkpad);
838 gst_bin_remove (GST_BIN (decode_bin), queue);
839 return gst_object_ref (pad);
842 decode_bin->queues = g_list_append (decode_bin->queues, queue);
843 g_signal_connect (G_OBJECT (queue),
844 "overrun", G_CALLBACK (queue_filled_cb), decode_bin);
845 g_signal_connect (G_OBJECT (queue),
846 "underrun", G_CALLBACK (queue_underrun_cb), decode_bin);
848 gst_element_set_state (queue, GST_STATE_PAUSED);
849 gst_object_unref (queuesinkpad);
854 /* given a pad and a caps from an element, find the list of elements
855 * that could connect to the pad
857 * If the pad has a raw format, this function will create a ghostpad
858 * for the pad onto the decodebin.
860 * If no compatible elements could be found, this function will signal
861 * the unknown_type signal.
864 close_pad_link (GstElement * element, GstPad * pad, GstCaps * caps,
865 GstDecodeBin * decode_bin, gboolean more)
867 GstStructure *structure;
868 const gchar *mimetype;
872 padname = gst_pad_get_name (pad);
873 diff = strncmp (padname, "current_", 8);
876 /* hack.. ignore current pads */
880 /* the caps is empty, this means the pad has no type, we can only
881 * decide to fire the unknown_type signal. */
882 if (caps == NULL || gst_caps_is_empty (caps))
885 /* the caps is any, this means the pad can be anything and
886 * we don't know yet */
887 if (gst_caps_is_any (caps))
890 GST_LOG_OBJECT (element, "trying to close %" GST_PTR_FORMAT, caps);
892 /* FIXME, iterate over more structures? I guess it is possible that
893 * this pad has some encoded and some raw pads. This code will fail
894 * then if the first structure is not the raw type... */
895 structure = gst_caps_get_structure (caps, 0);
896 mimetype = gst_structure_get_name (structure);
898 /* first see if this is raw. If the type is raw, we can
899 * create a ghostpad for this pad. It's possible that the caps are not
901 if (mimetype_is_raw (mimetype)) {
902 GstPadTemplate *tmpl;
907 /* If we're at a demuxer element but have raw data already
908 * we have to add a queue here. For non-raw data this is done
909 * in try_to_link_1() */
910 if (is_demuxer_element (element)) {
911 GST_DEBUG_OBJECT (decode_bin,
912 "Element %s is a demuxer, inserting a queue",
913 GST_OBJECT_NAME (element));
915 pad = add_raw_queue (decode_bin, pad);
918 /* make a unique name for this new pad */
919 padname = g_strdup_printf ("src%d", decode_bin->numpads);
920 decode_bin->numpads++;
922 /* make it a ghostpad */
923 tmpl = gst_static_pad_template_get (&decoder_bin_src_template);
924 ghost = gst_ghost_pad_new_from_template (padname, pad, tmpl);
925 gst_object_unref (tmpl);
927 gst_pad_set_active (ghost, TRUE);
928 gst_element_add_pad (GST_ELEMENT (decode_bin), ghost);
930 data = g_new0 (PadProbeData, 1);
934 /* FIXME, use _pad_block() */
935 data->sigid = gst_pad_add_data_probe (pad, G_CALLBACK (pad_probe),
937 decode_bin->numwaiting++;
939 decode_bin->probes = g_list_append (decode_bin->probes, data);
941 GST_LOG_OBJECT (element, "closed pad %s", padname);
943 /* our own signal with an extra flag that this is the only pad */
944 GST_DEBUG_OBJECT (decode_bin, "emitting new-decoded-pad");
945 g_signal_emit (G_OBJECT (decode_bin),
946 gst_decode_bin_signals[SIGNAL_NEW_DECODED_PAD], 0, ghost, !more);
947 GST_DEBUG_OBJECT (decode_bin, "emitted new-decoded-pad");
951 /* If we're at a demuxer element pad was set to a queue's
952 * srcpad and must be unref'd here */
953 if (is_demuxer_element (element))
954 gst_object_unref (pad);
958 /* if the caps has many types, we need to delay */
959 if (!gst_caps_is_fixed (caps))
962 /* continue plugging, first find all compatible elements */
963 to_try = find_compatibles (decode_bin, caps);
965 /* no compatible elements, we cannot go on */
968 if (try_to_link_1 (decode_bin, element, pad, to_try) == NULL) {
969 g_list_free (to_try);
970 GST_LOG_OBJECT (pad, "none of the allegedly available elements usable");
974 /* can free the list again now */
975 g_list_free (to_try);
982 GST_LOG_OBJECT (pad, "unknown type found, fire signal");
983 g_signal_emit (G_OBJECT (decode_bin),
984 gst_decode_bin_signals[SIGNAL_UNKNOWN_TYPE], 0, pad, caps);
986 gst_element_post_message (GST_ELEMENT_CAST (decode_bin),
987 gst_missing_decoder_message_new (GST_ELEMENT_CAST (decode_bin), caps));
989 if (element == decode_bin->typefind) {
992 desc = gst_pb_utils_get_decoder_description (caps);
993 GST_ELEMENT_ERROR (decode_bin, STREAM, CODEC_NOT_FOUND,
994 (_("A %s plugin is required to play this stream, but not installed."),
996 ("No decoder to handle media type '%s'",
997 gst_structure_get_name (gst_caps_get_structure (caps, 0))));
1005 GST_LOG_OBJECT (pad, "type is not known yet");
1006 goto setup_caps_delay;
1010 GST_LOG_OBJECT (pad, "many possible types");
1011 goto setup_caps_delay;
1015 GST_LOG_OBJECT (pad, "setting up a delayed link");
1016 dynamic_add (element, pad, decode_bin);
1021 /* Decide whether an element is a demuxer based on the
1022 * klass and number/type of src pad templates it has */
1024 is_demuxer_element (GstElement * srcelement)
1026 GstElementFactory *srcfactory;
1027 GstElementClass *elemclass;
1030 gint potential_src_pads = 0;
1032 srcfactory = gst_element_get_factory (srcelement);
1033 klass = gst_element_factory_get_klass (srcfactory);
1035 /* Can't be a demuxer unless it has Demux in the klass name */
1036 if (klass == NULL || !strstr (klass, "Demux"))
1039 /* Walk the src pad templates and count how many the element
1041 elemclass = GST_ELEMENT_GET_CLASS (srcelement);
1043 walk = gst_element_class_get_pad_template_list (elemclass);
1044 while (walk != NULL) {
1045 GstPadTemplate *templ;
1047 templ = (GstPadTemplate *) walk->data;
1048 if (GST_PAD_TEMPLATE_DIRECTION (templ) == GST_PAD_SRC) {
1049 switch (GST_PAD_TEMPLATE_PRESENCE (templ)) {
1050 case GST_PAD_ALWAYS:
1051 case GST_PAD_SOMETIMES:
1052 if (strstr (GST_PAD_TEMPLATE_NAME_TEMPLATE (templ), "%"))
1053 potential_src_pads += 2; /* Might make multiple pads */
1055 potential_src_pads += 1;
1057 case GST_PAD_REQUEST:
1058 potential_src_pads += 2;
1062 walk = g_list_next (walk);
1065 if (potential_src_pads < 2)
1072 * given a list of element factories, try to link one of the factories
1075 * The function returns the element that was successfully linked to the
1079 try_to_link_1 (GstDecodeBin * decode_bin, GstElement * srcelement, GstPad * pad,
1083 GstElement *result = NULL;
1084 gboolean isdemux = FALSE;
1085 GstPad *queuesinkpad = NULL, *queuesrcpad = NULL;
1086 GstElement *queue = NULL;
1087 GstPad *usedsrcpad = pad;
1089 /* Check if the parent of the src pad is a demuxer */
1090 isdemux = is_demuxer_element (srcelement);
1092 if (isdemux && factories != NULL) {
1093 GstPadLinkReturn dqlink;
1095 /* Insert a queue between demuxer and decoder */
1096 GST_DEBUG_OBJECT (decode_bin,
1097 "Element %s is a demuxer, inserting a queue",
1098 GST_OBJECT_NAME (srcelement));
1099 queue = gst_element_factory_make ("queue", NULL);
1100 decode_bin->queue_type = G_OBJECT_TYPE (queue);
1102 g_object_set (G_OBJECT (queue), "max-size-buffers", 0, NULL);
1103 g_object_set (G_OBJECT (queue), "max-size-time", G_GINT64_CONSTANT (0),
1105 g_object_set (G_OBJECT (queue), "max-size-bytes", 8192, NULL);
1106 gst_bin_add (GST_BIN (decode_bin), queue);
1107 gst_element_set_state (queue, GST_STATE_READY);
1108 queuesinkpad = gst_element_get_static_pad (queue, "sink");
1109 usedsrcpad = queuesrcpad = gst_element_get_static_pad (queue, "src");
1111 dqlink = gst_pad_link (pad, queuesinkpad);
1112 g_return_val_if_fail (dqlink == GST_PAD_LINK_OK, NULL);
1115 /* loop over the factories */
1116 for (walk = factories; walk; walk = g_list_next (walk)) {
1117 GstElementFactory *factory = GST_ELEMENT_FACTORY (walk->data);
1118 GstElement *element;
1119 GstPadLinkReturn ret;
1122 GST_DEBUG_OBJECT (decode_bin, "trying to link %s",
1123 gst_plugin_feature_get_name (GST_PLUGIN_FEATURE (factory)));
1125 /* make an element from the factory first */
1126 if ((element = gst_element_factory_create (factory, NULL)) == NULL) {
1127 /* hmm, strange. Like with all things in life, let's move on.. */
1128 GST_WARNING_OBJECT (decode_bin, "could not create an element from %s",
1129 gst_plugin_feature_get_name (GST_PLUGIN_FEATURE (factory)));
1133 /* try to link the given pad to a sinkpad */
1134 /* FIXME, find the sinkpad by looping over the pads instead of
1135 * looking it up by name */
1136 if ((sinkpad = gst_element_get_static_pad (element, "sink")) == NULL) {
1137 /* if no pad is found we can't do anything */
1138 GST_WARNING_OBJECT (decode_bin, "could not find sinkpad in element");
1142 /* now add the element to the bin first */
1143 GST_DEBUG_OBJECT (decode_bin, "adding %s", GST_OBJECT_NAME (element));
1144 gst_bin_add (GST_BIN (decode_bin), element);
1146 /* set to READY first so it is ready, duh. */
1147 if (gst_element_set_state (element,
1148 GST_STATE_READY) == GST_STATE_CHANGE_FAILURE) {
1149 GST_WARNING_OBJECT (decode_bin, "Couldn't set %s to READY",
1150 GST_ELEMENT_NAME (element));
1151 /* get rid of the sinkpad */
1152 gst_object_unref (sinkpad);
1153 /* this element did not work, remove it again and continue trying
1154 * other elements, the element will be disposed. */
1155 /* FIXME: shouldn't we do this before adding it to the bin so that no
1156 * error messages get through to the app? (tpm) */
1157 gst_bin_remove (GST_BIN (decode_bin), element);
1161 if ((ret = gst_pad_link (usedsrcpad, sinkpad)) != GST_PAD_LINK_OK) {
1162 GST_DEBUG_OBJECT (decode_bin, "link failed on pad %s:%s, reason %d",
1163 GST_DEBUG_PAD_NAME (pad), ret);
1164 /* get rid of the sinkpad */
1165 gst_object_unref (sinkpad);
1166 /* this element did not work, remove it again and continue trying
1167 * other elements, the element will be disposed. */
1168 gst_element_set_state (element, GST_STATE_NULL);
1169 gst_bin_remove (GST_BIN (decode_bin), element);
1171 GST_DEBUG_OBJECT (decode_bin, "linked on pad %s:%s",
1172 GST_DEBUG_PAD_NAME (usedsrcpad));
1174 /* configure the queue some more */
1175 if (queue != NULL) {
1176 decode_bin->queues = g_list_append (decode_bin->queues, queue);
1177 g_signal_connect (G_OBJECT (queue),
1178 "overrun", G_CALLBACK (queue_filled_cb), decode_bin);
1179 g_signal_connect (G_OBJECT (queue),
1180 "underrun", G_CALLBACK (queue_underrun_cb), decode_bin);
1183 /* The link worked, now figure out what it was that we connected */
1185 /* make sure we catch unlink signals */
1186 g_signal_connect (G_OBJECT (pad), "unlinked",
1187 G_CALLBACK (unlinked), decode_bin);
1189 /* now that we added the element we can try to continue autoplugging
1190 * on it until we have a raw type */
1191 close_link (element, decode_bin);
1193 /* change the state of the element to that of the parent */
1194 if ((gst_element_set_state (element,
1195 GST_STATE_PAUSED)) == GST_STATE_CHANGE_FAILURE) {
1196 GST_WARNING_OBJECT (decode_bin, "Couldn't set %s to PAUSED",
1197 GST_ELEMENT_NAME (element));
1198 /* close_link -> close_pad_link -> might have set up a pad probe */
1199 free_pad_probe_for_element (decode_bin, element);
1200 gst_element_set_state (element, GST_STATE_NULL);
1201 gst_bin_remove (GST_BIN (decode_bin), element);
1207 /* get rid of the sinkpad now */
1208 gst_object_unref (sinkpad);
1210 /* Set the queue to paused and set the pointer to NULL so we don't
1211 * remove it below */
1212 if (queue != NULL) {
1213 gst_element_set_state (queue, GST_STATE_PAUSED);
1215 gst_object_unref (queuesrcpad);
1216 gst_object_unref (queuesinkpad);
1224 if (queue != NULL) {
1225 /* We didn't successfully connect to the queue */
1226 gst_pad_unlink (pad, queuesinkpad);
1227 gst_element_set_state (queue, GST_STATE_NULL);
1228 gst_object_unref (queuesrcpad);
1229 gst_object_unref (queuesinkpad);
1230 gst_bin_remove (GST_BIN (decode_bin), queue);
1236 get_our_ghost_pad (GstDecodeBin * decode_bin, GstPad * pad)
1238 GstIterator *pad_it = NULL;
1239 GstPad *db_pad = NULL;
1240 gboolean done = FALSE;
1242 if (pad == NULL || !GST_PAD_IS_SRC (pad)) {
1243 GST_DEBUG_OBJECT (decode_bin, "pad NULL or not SRC pad");
1247 /* our ghostpads are the sourcepads */
1248 pad_it = gst_element_iterate_src_pads (GST_ELEMENT (decode_bin));
1251 switch (gst_iterator_next (pad_it, (gpointer) & db_pad)) {
1252 case GST_ITERATOR_OK:
1253 GST_DEBUG_OBJECT (decode_bin, "looking at pad %s:%s",
1254 GST_DEBUG_PAD_NAME (db_pad));
1255 if (GST_IS_GHOST_PAD (db_pad)) {
1256 GstPad *target_pad = NULL;
1258 target_pad = gst_ghost_pad_get_target (GST_GHOST_PAD (db_pad));
1259 done = (target_pad == pad);
1261 gst_object_unref (target_pad);
1264 /* Found our ghost pad */
1265 GST_DEBUG_OBJECT (decode_bin, "found ghostpad %s:%s for pad %s:%s",
1266 GST_DEBUG_PAD_NAME (db_pad), GST_DEBUG_PAD_NAME (pad));
1270 /* Not the right one */
1271 gst_object_unref (db_pad);
1273 case GST_ITERATOR_RESYNC:
1274 gst_iterator_resync (pad_it);
1276 case GST_ITERATOR_ERROR:
1279 case GST_ITERATOR_DONE:
1284 gst_iterator_free (pad_it);
1289 /* remove all downstream elements starting from the given pad.
1290 * Also make sure to remove the ghostpad we created for the raw
1294 remove_element_chain (GstDecodeBin * decode_bin, GstPad * pad)
1297 gboolean done = FALSE;
1299 GstElement *elem = GST_ELEMENT (GST_OBJECT_PARENT (pad));
1301 while (GST_OBJECT_PARENT (elem) &&
1302 GST_OBJECT_PARENT (elem) != GST_OBJECT (decode_bin))
1303 elem = GST_ELEMENT (GST_OBJECT_PARENT (elem));
1305 if (G_OBJECT_TYPE (elem) == decode_bin->queue_type) {
1306 GST_DEBUG_OBJECT (decode_bin,
1307 "Encountered demuxer output queue while removing element chain");
1308 decode_bin->queues = g_list_remove (decode_bin->queues, elem);
1311 GST_DEBUG_OBJECT (decode_bin, "%s:%s", GST_DEBUG_PAD_NAME (pad));
1312 iter = gst_pad_iterate_internal_links (pad);
1316 /* remove all elements linked to this pad up to the ghostpad
1317 * that we created for this stream */
1319 switch (gst_iterator_next (iter, &item)) {
1320 case GST_ITERATOR_OK:{
1325 pad = GST_PAD (item);
1326 GST_DEBUG_OBJECT (decode_bin, "inspecting internal pad %s:%s",
1327 GST_DEBUG_PAD_NAME (pad));
1329 ghostpad = get_our_ghost_pad (decode_bin, pad);
1331 GST_DEBUG_OBJECT (decode_bin, "found our ghost pad %s:%s for %s:%s",
1332 GST_DEBUG_PAD_NAME (ghostpad), GST_DEBUG_PAD_NAME (pad));
1334 g_signal_emit (G_OBJECT (decode_bin),
1335 gst_decode_bin_signals[SIGNAL_REMOVED_DECODED_PAD], 0, ghostpad);
1337 gst_element_remove_pad (GST_ELEMENT (decode_bin), ghostpad);
1338 gst_object_unref (ghostpad);
1341 GST_DEBUG_OBJECT (decode_bin, "not one of our ghostpads");
1344 peer = gst_pad_get_peer (pad);
1346 GstObject *parent = gst_pad_get_parent (peer);
1348 GST_DEBUG_OBJECT (decode_bin,
1349 "internal pad %s:%s linked to pad %s:%s",
1350 GST_DEBUG_PAD_NAME (pad), GST_DEBUG_PAD_NAME (peer));
1353 GstObject *grandparent = gst_object_get_parent (parent);
1355 if (grandparent != NULL) {
1356 if (GST_ELEMENT (grandparent) != GST_ELEMENT (decode_bin)) {
1357 GST_DEBUG_OBJECT (decode_bin, "dead end pad %s:%s parent %s",
1358 GST_DEBUG_PAD_NAME (peer), GST_OBJECT_NAME (grandparent));
1360 GST_DEBUG_OBJECT (decode_bin,
1361 "recursing element %s on pad %s:%s",
1362 GST_ELEMENT_NAME (elem), GST_DEBUG_PAD_NAME (pad));
1363 remove_element_chain (decode_bin, peer);
1365 gst_object_unref (grandparent);
1367 gst_object_unref (parent);
1369 gst_object_unref (peer);
1371 gst_object_unref (item);
1374 case GST_ITERATOR_RESYNC:
1375 gst_iterator_resync (iter);
1377 case GST_ITERATOR_ERROR:
1378 GST_ERROR_OBJECT (pad, "Could not iterate over internally linked pads");
1381 case GST_ITERATOR_DONE:
1386 GST_DEBUG_OBJECT (decode_bin, "removing %s", GST_ELEMENT_NAME (elem));
1388 gst_iterator_free (iter);
1391 gst_element_set_state (elem, GST_STATE_NULL);
1392 gst_bin_remove (GST_BIN (decode_bin), elem);
1395 /* there are @bytes bytes in @queue, enlarge it
1397 * Returns: new max number of bytes in @queue
1400 queue_enlarge (GstElement * queue, guint bytes, GstDecodeBin * decode_bin)
1402 /* Increase the queue size by 1Mbyte if it is over 1Mb, else double its current limit
1404 if (bytes > 1024 * 1024)
1405 bytes += 1024 * 1024;
1409 GST_DEBUG_OBJECT (decode_bin,
1410 "increasing queue %s max-size-bytes to %d", GST_ELEMENT_NAME (queue),
1412 g_object_set (G_OBJECT (queue), "max-size-bytes", bytes, NULL);
1417 /* this callback is called when our queues fills up or are empty
1418 * We then check the status of all other queues to make sure we
1419 * never have an empty and full queue at the same time since that
1420 * would block dataflow. In the case of a filled queue, we make
1424 queue_underrun_cb (GstElement * queue, GstDecodeBin * decode_bin)
1426 /* FIXME: we don't really do anything here for now. Ideally we should
1427 * see if some of the queues are filled and increase their values
1429 * Note: be very carefull with thread safety here as this underrun
1430 * signal is done from the streaming thread of queue srcpad which
1431 * is different from the pad_added (where we add the queue to the
1432 * list) and the overrun signals that are signalled from the
1435 GST_DEBUG_OBJECT (decode_bin, "got underrun");
1438 /* Make sure we don't have a full queue and empty queue situation */
1440 queue_filled_cb (GstElement * queue, GstDecodeBin * decode_bin)
1443 gboolean increase = FALSE;
1446 /* get current byte level from the queue that is filled */
1447 g_object_get (G_OBJECT (queue), "current-level-bytes", &bytes, NULL);
1448 GST_DEBUG_OBJECT (decode_bin, "One of the queues is full at %d bytes", bytes);
1450 /* we do not buffer more than 20Mb */
1451 if (bytes > (20 * 1024 * 1024))
1454 /* check all other queue to see if one is empty, in that case
1455 * we need to enlarge @queue */
1456 for (tmp = decode_bin->queues; tmp; tmp = g_list_next (tmp)) {
1457 GstElement *aqueue = GST_ELEMENT (tmp->data);
1458 guint levelbytes = 0;
1460 if (aqueue != queue) {
1461 g_object_get (G_OBJECT (aqueue), "current-level-bytes", &levelbytes,
1463 if (levelbytes == 0) {
1464 /* yup, found an empty queue, we can stop the search and
1465 * need to enlarge the queue */
1473 /* enlarge @queue */
1474 queue_enlarge (queue, bytes, decode_bin);
1476 GST_DEBUG_OBJECT (decode_bin,
1477 "Queue is full but other queues are not empty, not doing anything");
1484 GST_WARNING_OBJECT (decode_bin,
1485 "Queue is bigger than 20Mbytes, something else is going wrong");
1490 /* This function will be called when a dynamic pad is created on an element.
1491 * We try to continue autoplugging on this new pad. */
1493 new_pad (GstElement * element, GstPad * pad, GstDynamic * dynamic)
1495 GstDecodeBin *decode_bin = dynamic->decode_bin;
1499 GST_OBJECT_LOCK (decode_bin);
1500 if (decode_bin->shutting_down)
1501 goto shutting_down1;
1502 GST_OBJECT_UNLOCK (decode_bin);
1504 GST_STATE_LOCK (decode_bin);
1505 if (decode_bin->shutting_down)
1506 goto shutting_down2;
1508 /* see if any more pending dynamic connections exist */
1509 more = gst_decode_bin_is_dynamic (decode_bin);
1511 caps = gst_pad_get_caps (pad);
1512 close_pad_link (element, pad, caps, decode_bin, more);
1514 gst_caps_unref (caps);
1515 GST_STATE_UNLOCK (decode_bin);
1521 GST_DEBUG_OBJECT (decode_bin, "we are shutting down");
1522 GST_OBJECT_UNLOCK (decode_bin);
1527 GST_DEBUG_OBJECT (decode_bin, "we are shutting down");
1528 GST_STATE_UNLOCK (decode_bin);
1534 dynamic_remove (GstDynamic * dynamic)
1536 GstDecodeBin *decode_bin = dynamic->decode_bin;
1538 /* remove the dynamic from the list of dynamics */
1539 decode_bin->dynamics = g_list_remove (decode_bin->dynamics, dynamic);
1540 dynamic_free (dynamic);
1542 /* if we have no more dynamic elements, we have no chance of creating
1543 * more pads, so we fire the no_more_pads signal */
1544 if (decode_bin->dynamics == NULL) {
1545 if (decode_bin->numwaiting == 0) {
1546 GST_DEBUG_OBJECT (decode_bin,
1547 "no more dynamic elements, removing fakesink");
1548 remove_fakesink (decode_bin);
1550 GST_DEBUG_OBJECT (decode_bin,
1551 "no more dynamic elements, signaling no_more_pads");
1552 gst_element_no_more_pads (GST_ELEMENT (decode_bin));
1554 GST_DEBUG_OBJECT (decode_bin, "we have more dynamic elements");
1558 /* this signal is fired when an element signals the no_more_pads signal.
1559 * This means that the element will not generate more dynamic pads and
1560 * we can remove the element from the list of dynamic elements. When we
1561 * have no more dynamic elements in the pipeline, we can fire a no_more_pads
1562 * signal ourselves. */
1564 no_more_pads (GstElement * element, GstDynamic * dynamic)
1566 GST_DEBUG_OBJECT (dynamic->decode_bin, "no more pads on element %s",
1567 GST_ELEMENT_NAME (element));
1569 dynamic_remove (dynamic);
1573 new_caps (GstPad * pad, GParamSpec * unused, GstDynamic * dynamic)
1575 GST_DEBUG_OBJECT (dynamic->decode_bin, "delayed link triggered");
1577 new_pad (dynamic->element, pad, dynamic);
1579 /* assume it worked and remove the dynamic */
1580 dynamic_remove (dynamic);
1586 is_our_kid (GstElement * e, GstDecodeBin * decode_bin)
1591 parent = (GstElement *) gst_object_get_parent ((GstObject *) e);
1592 ret = (parent == (GstElement *) decode_bin);
1595 gst_object_unref ((GstObject *) parent);
1601 elem_is_dynamic (GstElement * element, GstDecodeBin * decode_bin)
1605 /* loop over all the padtemplates */
1606 for (pads = GST_ELEMENT_GET_CLASS (element)->padtemplates; pads;
1607 pads = g_list_next (pads)) {
1608 GstPadTemplate *templ = GST_PAD_TEMPLATE (pads->data);
1609 const gchar *templ_name;
1611 /* we are only interested in source pads */
1612 if (GST_PAD_TEMPLATE_DIRECTION (templ) != GST_PAD_SRC)
1615 templ_name = GST_PAD_TEMPLATE_NAME_TEMPLATE (templ);
1616 GST_DEBUG_OBJECT (decode_bin, "got a source pad template %s", templ_name);
1618 /* figure out what kind of pad this is */
1619 switch (GST_PAD_TEMPLATE_PRESENCE (templ)) {
1620 case GST_PAD_SOMETIMES:
1622 /* try to get the pad to see if it is already created or
1624 GstPad *pad = gst_element_get_static_pad (element, templ_name);
1627 GST_DEBUG_OBJECT (decode_bin, "got the pad for sometimes template %s",
1629 gst_object_unref (pad);
1631 GST_DEBUG_OBJECT (decode_bin,
1632 "did not get the sometimes pad of template %s", templ_name);
1633 /* we have an element that will create dynamic pads */
1639 /* Don't care about ALWAYS or REQUEST pads */
1646 /* This function will be called when a pad is disconnected for some reason */
1648 unlinked (GstPad * pad, GstPad * peerpad, GstDecodeBin * decode_bin)
1650 GstElement *element, *peer;
1652 /* inactivate pad */
1653 gst_pad_set_active (pad, GST_ACTIVATE_NONE);
1655 peer = gst_pad_get_parent_element (peerpad);
1657 if (!is_our_kid (peer, decode_bin))
1660 GST_DEBUG_OBJECT (decode_bin, "pad %s:%s removal while alive - chained?",
1661 GST_DEBUG_PAD_NAME (pad));
1663 /* remove all elements linked to the peerpad */
1664 remove_element_chain (decode_bin, peerpad);
1666 /* Re-add as a dynamic element if needed, via close_link */
1667 element = gst_pad_get_parent_element (pad);
1669 if (elem_is_dynamic (element, decode_bin))
1670 dynamic_add (element, NULL, decode_bin);
1672 gst_object_unref (element);
1676 gst_object_unref (peer);
1679 /* this function inspects the given element and tries to connect something
1680 * on the srcpads. If there are dynamic pads, it sets up a signal handler to
1681 * continue autoplugging when they become available */
1683 close_link (GstElement * element, GstDecodeBin * decode_bin)
1686 gboolean dynamic = FALSE;
1687 GList *to_connect = NULL;
1690 GST_DEBUG_OBJECT (decode_bin, "closing links with element %s",
1691 GST_ELEMENT_NAME (element));
1693 /* loop over all the padtemplates */
1694 for (pads = GST_ELEMENT_GET_CLASS (element)->padtemplates; pads;
1695 pads = g_list_next (pads)) {
1696 GstPadTemplate *templ = GST_PAD_TEMPLATE (pads->data);
1697 const gchar *templ_name;
1699 /* we are only interested in source pads */
1700 if (GST_PAD_TEMPLATE_DIRECTION (templ) != GST_PAD_SRC)
1703 templ_name = GST_PAD_TEMPLATE_NAME_TEMPLATE (templ);
1704 GST_DEBUG_OBJECT (decode_bin, "got a source pad template %s", templ_name);
1706 /* figure out what kind of pad this is */
1707 switch (GST_PAD_TEMPLATE_PRESENCE (templ)) {
1708 case GST_PAD_ALWAYS:
1710 /* get the pad that we need to autoplug */
1711 GstPad *pad = gst_element_get_static_pad (element, templ_name);
1714 GST_DEBUG_OBJECT (decode_bin, "got the pad for always template %s",
1716 /* here is the pad, we need to autoplug it */
1717 to_connect = g_list_prepend (to_connect, pad);
1719 /* strange, pad is marked as always but it's not
1720 * there. Fix the element */
1721 GST_WARNING_OBJECT (decode_bin,
1722 "could not get the pad for always template %s", templ_name);
1726 case GST_PAD_SOMETIMES:
1728 /* try to get the pad to see if it is already created or
1730 GstPad *pad = gst_element_get_static_pad (element, templ_name);
1733 GST_DEBUG_OBJECT (decode_bin, "got the pad for sometimes template %s",
1735 /* the pad is created, we need to autoplug it */
1736 to_connect = g_list_prepend (to_connect, pad);
1738 GST_DEBUG_OBJECT (decode_bin,
1739 "did not get the sometimes pad of template %s", templ_name);
1740 /* we have an element that will create dynamic pads */
1745 case GST_PAD_REQUEST:
1746 /* ignore request pads */
1747 GST_DEBUG_OBJECT (decode_bin, "ignoring request padtemplate %s",
1753 GST_DEBUG_OBJECT (decode_bin, "got a dynamic element here");
1754 /* ok, this element has dynamic pads, set up the signal handlers to be
1755 * notified of them */
1757 dynamic_add (element, NULL, decode_bin);
1760 /* Check if this is an element with more than 1 pad. If this element
1761 * has more than 1 pad, we need to be carefull not to signal the
1762 * no_more_pads signal after connecting the first pad. */
1763 more = g_list_length (to_connect) > 1;
1765 /* now loop over all the pads we need to connect */
1766 for (pads = to_connect; pads; pads = g_list_next (pads)) {
1767 GstPad *pad = GST_PAD_CAST (pads->data);
1770 /* we have more pads if we have more than 1 pad to connect or
1771 * dynamics. If we have only 1 pad and no dynamics, more will be
1772 * set to FALSE and the no-more-pads signal will be fired. Note
1773 * that this can change after the close_pad_link call. */
1774 more |= gst_decode_bin_is_dynamic (decode_bin);
1776 GST_DEBUG_OBJECT (decode_bin, "closing pad link for %s",
1777 GST_OBJECT_NAME (pad));
1779 /* continue autoplugging on the pads */
1780 caps = gst_pad_get_caps (pad);
1781 close_pad_link (element, pad, caps, decode_bin, more);
1783 gst_caps_unref (caps);
1785 gst_object_unref (pad);
1787 g_list_free (to_connect);
1790 /* this is the signal handler for the typefind element have_type signal.
1791 * It tries to continue autoplugging on the typefind src pad */
1793 type_found (GstElement * typefind, guint probability, GstCaps * caps,
1794 GstDecodeBin * decode_bin)
1799 GST_DEBUG_OBJECT (decode_bin, "typefind found caps %" GST_PTR_FORMAT, caps);
1801 GST_OBJECT_LOCK (decode_bin);
1802 if (decode_bin->shutting_down)
1804 GST_OBJECT_UNLOCK (decode_bin);
1806 GST_STATE_LOCK (decode_bin);
1807 if (decode_bin->shutting_down)
1810 /* don't need the typefind anymore if we already found a type, we're not going
1811 * to be able to do anything with it anyway except for generating errors */
1812 if (decode_bin->have_type)
1815 decode_bin->have_type = TRUE;
1817 /* special-case text/plain: we only want to accept it as a raw type if it
1818 * comes from a subtitle parser element or a demuxer, but not if it is the
1819 * type of the entire stream, in which case we just want to error out */
1820 if (typefind == decode_bin->typefind &&
1821 gst_structure_has_name (gst_caps_get_structure (caps, 0), "text/plain")) {
1822 gst_element_no_more_pads (GST_ELEMENT (decode_bin));
1823 /* we can't handle this type of stream */
1824 GST_ELEMENT_ERROR (decode_bin, STREAM, WRONG_TYPE,
1825 (_("This appears to be a text file")),
1826 ("decodebin cannot decode plain text files"));
1830 /* autoplug the new pad with the caps that the signal gave us. */
1831 pad = gst_element_get_static_pad (typefind, "src");
1832 close_pad_link (typefind, pad, caps, decode_bin, FALSE);
1833 gst_object_unref (pad);
1835 dynamic = gst_decode_bin_is_dynamic (decode_bin);
1836 if (dynamic == FALSE) {
1837 GST_DEBUG_OBJECT (decode_bin, "we have no dynamic elements anymore");
1838 /* if we have no dynamic elements, we know that no new pads
1839 * will be created and we can signal out no_more_pads signal */
1840 gst_element_no_more_pads (GST_ELEMENT (decode_bin));
1842 /* more dynamic elements exist that could create new pads */
1843 GST_DEBUG_OBJECT (decode_bin, "we have more dynamic elements");
1847 GST_STATE_UNLOCK (decode_bin);
1852 GST_DEBUG_OBJECT (decode_bin, "we are shutting down");
1853 GST_OBJECT_UNLOCK (decode_bin);
1859 disconnect_unlinked_signals (GstDecodeBin * decode_bin, GstElement * element)
1861 GstIterator *pad_it = NULL;
1862 gboolean done = FALSE;
1864 pad_it = gst_element_iterate_src_pads (element);
1868 switch (gst_iterator_next (pad_it, (gpointer) & pad)) {
1869 case GST_ITERATOR_OK:
1870 g_signal_handlers_disconnect_by_func (pad, (gpointer) unlinked,
1872 gst_object_unref (pad);
1874 case GST_ITERATOR_RESYNC:
1875 gst_iterator_resync (pad_it);
1882 gst_iterator_free (pad_it);
1887 cleanup_decodebin (GstDecodeBin * decode_bin)
1889 GstIterator *elem_it = NULL, *gpad_it = NULL;
1890 GstPad *typefind_pad = NULL;
1891 gboolean done = FALSE;
1893 g_return_if_fail (GST_IS_DECODE_BIN (decode_bin));
1895 GST_DEBUG_OBJECT (decode_bin, "cleaning up decodebin");
1897 typefind_pad = gst_element_get_static_pad (decode_bin->typefind, "src");
1898 if (GST_IS_PAD (typefind_pad)) {
1899 g_signal_handlers_block_by_func (typefind_pad, (gpointer) unlinked,
1903 elem_it = gst_bin_iterate_elements (GST_BIN (decode_bin));
1905 GstElement *element = NULL;
1907 switch (gst_iterator_next (elem_it, (gpointer) & element)) {
1908 case GST_ITERATOR_OK:
1909 if (element != decode_bin->typefind && element != decode_bin->fakesink) {
1910 GST_DEBUG_OBJECT (element, "removing autoplugged element");
1911 disconnect_unlinked_signals (decode_bin, element);
1912 gst_element_set_state (element, GST_STATE_NULL);
1913 gst_bin_remove (GST_BIN (decode_bin), element);
1915 gst_object_unref (element);
1917 case GST_ITERATOR_RESYNC:
1918 gst_iterator_resync (elem_it);
1920 case GST_ITERATOR_ERROR:
1923 case GST_ITERATOR_DONE:
1928 gst_iterator_free (elem_it);
1931 gpad_it = gst_element_iterate_pads (GST_ELEMENT (decode_bin));
1935 switch (gst_iterator_next (gpad_it, (gpointer) & pad)) {
1936 case GST_ITERATOR_OK:
1937 GST_DEBUG_OBJECT (pad, "inspecting pad %s:%s",
1938 GST_DEBUG_PAD_NAME (pad));
1939 if (GST_IS_GHOST_PAD (pad) && GST_PAD_IS_SRC (pad)) {
1940 GST_DEBUG_OBJECT (pad, "removing ghost pad");
1941 gst_element_remove_pad (GST_ELEMENT (decode_bin), pad);
1943 gst_object_unref (pad);
1945 case GST_ITERATOR_RESYNC:
1946 gst_iterator_resync (gpad_it);
1948 case GST_ITERATOR_ERROR:
1951 case GST_ITERATOR_DONE:
1956 gst_iterator_free (gpad_it);
1958 if (GST_IS_PAD (typefind_pad)) {
1959 g_signal_handlers_unblock_by_func (typefind_pad, (gpointer) unlinked,
1961 g_signal_handlers_disconnect_by_func (typefind_pad, (gpointer) unlinked,
1963 gst_object_unref (typefind_pad);
1967 static GstStateChangeReturn
1968 gst_decode_bin_change_state (GstElement * element, GstStateChange transition)
1970 GstStateChangeReturn ret;
1971 GstDecodeBin *decode_bin;
1973 decode_bin = GST_DECODE_BIN (element);
1975 switch (transition) {
1976 case GST_STATE_CHANGE_NULL_TO_READY:
1977 decode_bin->numpads = 0;
1978 decode_bin->numwaiting = 0;
1979 decode_bin->dynamics = NULL;
1980 if (decode_bin->typefind == NULL)
1981 goto missing_typefind;
1983 case GST_STATE_CHANGE_READY_TO_PAUSED:
1984 GST_OBJECT_LOCK (decode_bin);
1985 decode_bin->shutting_down = FALSE;
1986 decode_bin->have_type = FALSE;
1987 GST_OBJECT_UNLOCK (decode_bin);
1989 if (!add_fakesink (decode_bin))
1990 goto missing_fakesink;
1992 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
1993 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
1995 case GST_STATE_CHANGE_PAUSED_TO_READY:
1996 GST_OBJECT_LOCK (decode_bin);
1997 decode_bin->shutting_down = TRUE;
1998 GST_OBJECT_UNLOCK (decode_bin);
2004 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
2005 if (ret == GST_STATE_CHANGE_FAILURE)
2008 switch (transition) {
2009 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
2011 case GST_STATE_CHANGE_PAUSED_TO_READY:
2012 case GST_STATE_CHANGE_READY_TO_NULL:
2013 free_dynamics (decode_bin);
2014 free_pad_probes (decode_bin);
2015 cleanup_decodebin (decode_bin);
2025 gst_element_post_message (element,
2026 gst_missing_element_message_new (element, "typefind"));
2027 GST_ELEMENT_ERROR (element, CORE, MISSING_PLUGIN, (NULL), ("no typefind!"));
2028 return GST_STATE_CHANGE_FAILURE;
2032 gst_element_post_message (element,
2033 gst_missing_element_message_new (element, "fakesink"));
2034 GST_ELEMENT_ERROR (element, CORE, MISSING_PLUGIN, (NULL), ("no fakesink!"));
2035 return GST_STATE_CHANGE_FAILURE;
2040 plugin_init (GstPlugin * plugin)
2042 GST_DEBUG_CATEGORY_INIT (gst_decode_bin_debug, "decodebin", 0, "decoder bin");
2045 GST_DEBUG ("binding text domain %s to locale dir %s", GETTEXT_PACKAGE,
2047 bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
2048 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
2049 #endif /* ENABLE_NLS */
2051 return gst_element_register (plugin, "decodebin", GST_RANK_NONE,
2052 GST_TYPE_DECODE_BIN);
2055 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
2058 "decoder bin", plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME,