2 * Copyright (C) <2004> Wim Taymans <wim@fluendo.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.
27 #include "gstplay-marshal.h"
29 /* generic templates */
30 static GstStaticPadTemplate decoder_bin_sink_template =
31 GST_STATIC_PAD_TEMPLATE ("sink",
36 static GstStaticPadTemplate decoder_bin_src_template =
37 GST_STATIC_PAD_TEMPLATE ("src%d",
42 GST_DEBUG_CATEGORY_STATIC (gst_decode_bin_debug);
43 #define GST_CAT_DEFAULT gst_decode_bin_debug
45 #define GST_TYPE_DECODE_BIN (gst_decode_bin_get_type())
46 #define GST_DECODE_BIN(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_DECODE_BIN,GstDecodeBin))
47 #define GST_DECODE_BIN_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_DECODE_BIN,GstDecodeBinClass))
48 #define GST_IS_DECODE_BIN(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_DECODE_BIN))
49 #define GST_IS_DECODE_BIN_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_DECODE_BIN))
51 typedef struct _GstDecodeBin GstDecodeBin;
52 typedef struct _GstDecodeBinClass GstDecodeBinClass;
56 GstBin bin; /* we extend GstBin */
58 GstElement *typefind; /* this holds the typefind object */
61 GList *dynamics; /* list of dynamic connections */
63 GList *queues; /* list of demuxer-decoder queues */
65 GList *factories; /* factories we can use for selecting elements */
69 guint have_type_id; /* signal id for the typefind element */
71 gboolean shutting_down; /* stop pluggin if we're shutting down */
74 struct _GstDecodeBinClass
76 GstBinClass parent_class;
78 /* signal we fire when a new pad has been decoded into raw audio/video */
79 void (*new_decoded_pad) (GstElement * element, GstPad * pad, gboolean last);
80 /* signal we fire when a pad has been removed */
81 void (*removed_decoded_pad) (GstElement * element, GstPad * pad);
82 /* signal fired when we found a pad that we cannot decode */
83 void (*unknown_type) (GstElement * element, GstPad * pad, GstCaps * caps);
89 SIGNAL_NEW_DECODED_PAD,
90 SIGNAL_REMOVED_DECODED_PAD,
96 /* this structure is created for all dynamic pads that could get created
100 gint np_sig_id; /* signal id of new_pad */
101 gint unlink_sig_id; /* signal id of unlinked */
102 gint nmp_sig_id; /* signal id of no_more_pads */
103 GstElement *element; /* the element sending the signal */
104 GstDecodeBin *decode_bin; /* pointer to ourself */
108 static void gst_decode_bin_class_init (GstDecodeBinClass * klass);
109 static void gst_decode_bin_init (GstDecodeBin * decode_bin);
110 static void gst_decode_bin_dispose (GObject * object);
112 static GstStateChangeReturn gst_decode_bin_change_state (GstElement * element,
113 GstStateChange transition);
115 static void free_dynamics (GstDecodeBin * decode_bin);
116 static void type_found (GstElement * typefind, guint probability,
117 GstCaps * caps, GstDecodeBin * decode_bin);
118 static GstElement *try_to_link_1 (GstDecodeBin * decode_bin,
119 GstElement * origelement, GstPad * pad, GList * factories);
120 static void close_link (GstElement * element, GstDecodeBin * decode_bin);
121 static void close_pad_link (GstElement * element, GstPad * pad,
122 GstCaps * caps, GstDecodeBin * decode_bin, gboolean more);
123 static void unlinked (GstPad * pad, GstPad * peerpad,
124 GstDecodeBin * decode_bin);
125 static void new_pad (GstElement * element, GstPad * pad, GstDynamic * dynamic);
126 static void no_more_pads (GstElement * element, GstDynamic * dynamic);
128 static void queue_filled_cb (GstElement * queue, GstDecodeBin * decode_bin);
130 static GstElementClass *parent_class;
131 static guint gst_decode_bin_signals[LAST_SIGNAL] = { 0 };
133 static GstElementDetails gst_decode_bin_details = {
135 "Generic/Bin/Decoder",
136 "Autoplug and decode to raw media",
137 "Wim Taymans <wim@fluendo.com>"
142 gst_decode_bin_get_type (void)
144 static GType gst_decode_bin_type = 0;
146 if (!gst_decode_bin_type) {
147 static const GTypeInfo gst_decode_bin_info = {
148 sizeof (GstDecodeBinClass),
151 (GClassInitFunc) gst_decode_bin_class_init,
154 sizeof (GstDecodeBin),
156 (GInstanceInitFunc) gst_decode_bin_init,
160 gst_decode_bin_type =
161 g_type_register_static (GST_TYPE_BIN, "GstDecodeBin",
162 &gst_decode_bin_info, 0);
165 return gst_decode_bin_type;
169 gst_decode_bin_class_init (GstDecodeBinClass * klass)
171 GObjectClass *gobject_klass;
172 GstElementClass *gstelement_klass;
173 GstBinClass *gstbin_klass;
175 gobject_klass = (GObjectClass *) klass;
176 gstelement_klass = (GstElementClass *) klass;
177 gstbin_klass = (GstBinClass *) klass;
179 parent_class = g_type_class_ref (gst_bin_get_type ());
181 gst_decode_bin_signals[SIGNAL_NEW_DECODED_PAD] =
182 g_signal_new ("new-decoded-pad", G_TYPE_FROM_CLASS (klass),
184 G_STRUCT_OFFSET (GstDecodeBinClass, new_decoded_pad), NULL, NULL,
185 gst_play_marshal_VOID__OBJECT_BOOLEAN, G_TYPE_NONE, 2, GST_TYPE_PAD,
187 gst_decode_bin_signals[SIGNAL_REMOVED_DECODED_PAD] =
188 g_signal_new ("removed-decoded-pad", G_TYPE_FROM_CLASS (klass),
190 G_STRUCT_OFFSET (GstDecodeBinClass, removed_decoded_pad), NULL, NULL,
191 gst_marshal_VOID__OBJECT, G_TYPE_NONE, 1, GST_TYPE_PAD);
192 gst_decode_bin_signals[SIGNAL_UNKNOWN_TYPE] =
193 g_signal_new ("unknown-type", G_TYPE_FROM_CLASS (klass),
194 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstDecodeBinClass, unknown_type),
195 NULL, NULL, gst_marshal_VOID__OBJECT_OBJECT, G_TYPE_NONE, 2,
196 GST_TYPE_PAD, GST_TYPE_CAPS);
198 gobject_klass->dispose = GST_DEBUG_FUNCPTR (gst_decode_bin_dispose);
200 gst_element_class_add_pad_template (gstelement_klass,
201 gst_static_pad_template_get (&decoder_bin_sink_template));
202 gst_element_class_add_pad_template (gstelement_klass,
203 gst_static_pad_template_get (&decoder_bin_src_template));
205 gst_element_class_set_details (gstelement_klass, &gst_decode_bin_details);
207 gstelement_klass->change_state =
208 GST_DEBUG_FUNCPTR (gst_decode_bin_change_state);
211 /* check if the bin is dynamic.
213 * If there are no outstanding dynamic connections, the bin is
214 * considered to be non-dynamic.
217 gst_decode_bin_is_dynamic (GstDecodeBin * decode_bin)
219 return decode_bin->dynamics != NULL;
222 /* the filter function for selecting the elements we can use in
225 gst_decode_bin_factory_filter (GstPluginFeature * feature,
226 GstDecodeBin * decode_bin)
231 /* we only care about element factories */
232 if (!GST_IS_ELEMENT_FACTORY (feature))
235 klass = gst_element_factory_get_klass (GST_ELEMENT_FACTORY (feature));
236 /* only demuxers and decoders can play */
237 if (strstr (klass, "Demux") == NULL &&
238 strstr (klass, "Decoder") == NULL && strstr (klass, "Parse") == NULL) {
242 /* only select elements with autoplugging rank */
243 rank = gst_plugin_feature_get_rank (feature);
244 if (rank < GST_RANK_MARGINAL)
250 /* function used to sort element features */
252 compare_ranks (GstPluginFeature * f1, GstPluginFeature * f2)
255 const gchar *rname1, *rname2;
257 diff = gst_plugin_feature_get_rank (f2) - gst_plugin_feature_get_rank (f1);
261 rname1 = gst_plugin_feature_get_name (f1);
262 rname2 = gst_plugin_feature_get_name (f2);
264 diff = strcmp (rname2, rname1);
270 print_feature (GstPluginFeature * feature)
274 rname = gst_plugin_feature_get_name (feature);
276 GST_DEBUG ("%s", rname);
280 gst_decode_bin_init (GstDecodeBin * decode_bin)
284 /* first filter out the interesting element factories */
285 factories = gst_default_registry_feature_filter (
286 (GstPluginFeatureFilter) gst_decode_bin_factory_filter,
289 /* sort them according to their ranks */
290 decode_bin->factories = g_list_sort (factories, (GCompareFunc) compare_ranks);
291 /* do some debugging */
292 g_list_foreach (decode_bin->factories, (GFunc) print_feature, NULL);
294 /* we create the typefind element only once */
295 decode_bin->typefind = gst_element_factory_make ("typefind", "typefind");
296 if (!decode_bin->typefind) {
297 g_warning ("can't find typefind element, decodebin will not work");
301 /* add the typefind element */
302 if (!gst_bin_add (GST_BIN (decode_bin), decode_bin->typefind)) {
303 g_warning ("Could not add typefind element, decodebin will not work");
304 gst_object_unref (decode_bin->typefind);
305 decode_bin->typefind = NULL;
308 /* get the sinkpad */
309 pad = gst_element_get_pad (decode_bin->typefind, "sink");
311 /* ghost the sink pad to ourself */
312 gst_element_add_pad (GST_ELEMENT (decode_bin),
313 gst_ghost_pad_new ("sink", pad));
315 gst_object_unref (pad);
317 /* connect a signal to find out when the typefind element found
319 decode_bin->have_type_id =
320 g_signal_connect (G_OBJECT (decode_bin->typefind), "have_type",
321 G_CALLBACK (type_found), decode_bin);
323 decode_bin->fakesink = gst_element_factory_make ("fakesink", "fakesink");
324 if (!decode_bin->fakesink) {
325 g_warning ("can't find fakesink element, decodebin will not work");
327 GST_OBJECT_FLAG_UNSET (decode_bin->fakesink, GST_ELEMENT_IS_SINK);
328 if (!gst_bin_add (GST_BIN (decode_bin), decode_bin->fakesink)) {
329 g_warning ("Could not add fakesink element, decodebin will not work");
330 gst_object_unref (decode_bin->fakesink);
331 decode_bin->fakesink = NULL;
335 decode_bin->dynamics = NULL;
336 decode_bin->queues = NULL;
339 static void dynamic_free (GstDynamic * dyn);
342 gst_decode_bin_dispose (GObject * object)
344 GstDecodeBin *decode_bin;
346 decode_bin = GST_DECODE_BIN (object);
348 if (decode_bin->factories)
349 gst_plugin_feature_list_free (decode_bin->factories);
350 decode_bin->factories = NULL;
352 G_OBJECT_CLASS (parent_class)->dispose (object);
354 /* our parent dispose might trigger new signals when pads are unlinked
355 * etc. clean up the mess here. */
356 /* FIXME do proper cleanup when going to NULL */
357 free_dynamics (decode_bin);
361 dynamic_create (GstElement * element, GstDecodeBin * decode_bin)
365 GST_DEBUG_OBJECT (element, "dynamic create");
368 gst_object_ref (element);
369 gst_object_ref (decode_bin);
371 dyn = g_new0 (GstDynamic, 1);
372 dyn->element = element;
373 dyn->decode_bin = decode_bin;
374 dyn->np_sig_id = g_signal_connect (G_OBJECT (element), "pad-added",
375 G_CALLBACK (new_pad), dyn);
376 dyn->nmp_sig_id = g_signal_connect (G_OBJECT (element), "no-more-pads",
377 G_CALLBACK (no_more_pads), dyn);
383 dynamic_free (GstDynamic * dyn)
385 GST_DEBUG_OBJECT (dyn->decode_bin, "dynamic free");
387 /* disconnect signals */
388 g_signal_handler_disconnect (G_OBJECT (dyn->element), dyn->np_sig_id);
389 g_signal_handler_disconnect (G_OBJECT (dyn->element), dyn->nmp_sig_id);
391 gst_object_unref (dyn->element);
392 gst_object_unref (dyn->decode_bin);
394 dyn->decode_bin = NULL;
399 free_dynamics (GstDecodeBin * decode_bin)
403 for (dyns = decode_bin->dynamics; dyns; dyns = g_list_next (dyns)) {
404 GstDynamic *dynamic = (GstDynamic *) dyns->data;
406 dynamic_free (dynamic);
408 g_list_free (decode_bin->dynamics);
409 decode_bin->dynamics = NULL;
412 /* this function runs through the element factories and returns a list
413 * of all elements that are able to sink the given caps
416 find_compatibles (GstDecodeBin * decode_bin, const GstCaps * caps)
419 GList *to_try = NULL;
421 /* loop over all the factories */
422 for (factories = decode_bin->factories; factories;
423 factories = g_list_next (factories)) {
424 GstElementFactory *factory = GST_ELEMENT_FACTORY (factories->data);
425 const GList *templates;
428 /* get the templates from the element factory */
429 templates = gst_element_factory_get_static_pad_templates (factory);
430 for (walk = (GList *) templates; walk; walk = g_list_next (walk)) {
431 GstStaticPadTemplate *templ = walk->data;
433 /* we only care about the sink templates */
434 if (templ->direction == GST_PAD_SINK) {
437 /* try to intersect the caps with the caps of the template */
438 intersect = gst_caps_intersect (caps,
439 gst_static_caps_get (&templ->static_caps));
440 /* check if the intersection is empty */
441 if (!gst_caps_is_empty (intersect)) {
442 /* non empty intersection, we can use this element */
443 to_try = g_list_prepend (to_try, factory);
444 gst_caps_unref (intersect);
447 gst_caps_unref (intersect);
451 to_try = g_list_reverse (to_try);
457 mimetype_is_raw (const gchar * mimetype)
459 return g_str_has_prefix (mimetype, "video/x-raw") ||
460 g_str_has_prefix (mimetype, "audio/x-raw") ||
461 g_str_has_prefix (mimetype, "text/plain");
465 remove_fakesink (GstDecodeBin * decode_bin)
467 if (decode_bin->fakesink) {
468 gst_object_ref (decode_bin->fakesink);
469 gst_bin_remove (GST_BIN (decode_bin), decode_bin->fakesink);
471 gst_element_set_state (decode_bin->fakesink, GST_STATE_NULL);
472 gst_element_get_state (decode_bin->fakesink, NULL, NULL,
473 GST_CLOCK_TIME_NONE);
475 gst_object_unref (decode_bin->fakesink);
476 decode_bin->fakesink = NULL;
478 gst_element_post_message (GST_ELEMENT_CAST (decode_bin),
479 gst_message_new_state_dirty (GST_OBJECT_CAST (decode_bin)));
484 pad_blocked (GstPad * pad, gboolean blocked, GstDecodeBin * decode_bin)
487 decode_bin->numwaiting--;
488 if (decode_bin->numwaiting == 0) {
489 remove_fakesink (decode_bin);
491 gst_pad_set_blocked_async (pad, FALSE, (GstPadBlockCallback) pad_blocked,
496 /* given a pad and a caps from an element, find the list of elements
497 * that could connect to the pad
499 * If the pad has a raw format, this function will create a ghostpad
500 * for the pad onto the decodebin.
502 * If no compatible elements could be found, this function will signal
503 * the unknown_type signal.
506 close_pad_link (GstElement * element, GstPad * pad, GstCaps * caps,
507 GstDecodeBin * decode_bin, gboolean more)
509 GstStructure *structure;
510 const gchar *mimetype;
514 padname = gst_pad_get_name (pad);
515 diff = strncmp (padname, "current_", 8);
518 /* hack.. ignore current pads */
522 /* the caps is empty, this means the pad has no type, we can only
523 * decide to fire the unknown_type signal. */
524 if (caps == NULL || gst_caps_is_empty (caps))
527 /* the caps is any, this means the pad can be anything and
528 * we don't know yet */
529 if (gst_caps_is_any (caps))
532 GST_LOG_OBJECT (element, "trying to close %" GST_PTR_FORMAT, caps);
534 /* FIXME, iterate over more structures? I guess it is possible that
535 * this pad has some encoded and some raw pads. This code will fail
536 * then if the first structure is not the raw type... */
537 structure = gst_caps_get_structure (caps, 0);
538 mimetype = gst_structure_get_name (structure);
540 /* first see if this is raw. If the type is raw, we can
541 * create a ghostpad for this pad. */
542 if (mimetype_is_raw (mimetype)) {
546 /* make a unique name for this new pad */
547 padname = g_strdup_printf ("src%d", decode_bin->numpads);
548 decode_bin->numpads++;
550 /* make it a ghostpad */
551 ghost = gst_ghost_pad_new (padname, pad);
552 gst_element_add_pad (GST_ELEMENT (decode_bin), ghost);
554 if (gst_pad_set_blocked_async (pad, TRUE, (GstPadBlockCallback) pad_blocked,
556 decode_bin->numwaiting++;
559 GST_LOG_OBJECT (element, "closed pad %s", padname);
561 /* our own signal with an extra flag that this is the only pad */
562 GST_DEBUG_OBJECT (decode_bin, "emitting new-decoded-pad");
563 g_signal_emit (G_OBJECT (decode_bin),
564 gst_decode_bin_signals[SIGNAL_NEW_DECODED_PAD], 0, ghost, !more);
565 GST_DEBUG_OBJECT (decode_bin, "emitted new-decoded-pad");
571 /* if the caps has many types, we need to delay */
572 if (gst_caps_get_size (caps) != 1)
575 /* continue plugging, first find all compatible elements */
576 to_try = find_compatibles (decode_bin, caps);
578 /* no compatible elements, we cannot go on */
581 try_to_link_1 (decode_bin, element, pad, to_try);
582 /* can free the list again now */
583 g_list_free (to_try);
589 GST_LOG_OBJECT (pad, "unkown type found, fire signal");
590 g_signal_emit (G_OBJECT (decode_bin),
591 gst_decode_bin_signals[SIGNAL_UNKNOWN_TYPE], 0, pad, caps);
596 GST_LOG_OBJECT (pad, "type is not known yet, waiting to close link");
601 GST_LOG_OBJECT (pad, "many possible types, waiting to close link");
607 * given a list of element factories, try to link one of the factories
610 * The function returns the element that was successfully linked to the
614 try_to_link_1 (GstDecodeBin * decode_bin, GstElement * srcelement, GstPad * pad,
618 GstElementFactory *srcfactory = NULL;
619 GstElement *result = NULL;
620 gboolean isdemux = FALSE;
623 /* Check if the parent of the src pad is a demuxer */
624 srcfactory = gst_element_get_factory (srcelement);
625 klass = gst_element_factory_get_klass (srcfactory);
626 isdemux = !!(strstr (klass, "Demux"));
628 /* loop over the factories */
629 for (walk = factories; walk; walk = g_list_next (walk)) {
630 GstElementFactory *factory = GST_ELEMENT_FACTORY (walk->data);
632 GstElement *queue = NULL;
633 GstPadLinkReturn ret;
635 GstPad *usedsrcpad = pad;
636 GstPad *queuesinkpad = NULL, *queuesrcpad = NULL;
638 GST_DEBUG_OBJECT (decode_bin, "trying to link %s",
639 gst_plugin_feature_get_name (GST_PLUGIN_FEATURE (factory)));
641 /* make an element from the factory first */
642 if ((element = gst_element_factory_create (factory, NULL)) == NULL) {
643 /* hmm, strange. Like with all things in life, let's move on.. */
644 GST_WARNING_OBJECT (decode_bin, "could not create an element from %s",
645 gst_plugin_feature_get_name (GST_PLUGIN_FEATURE (factory)));
649 /* try to link the given pad to a sinkpad */
650 /* FIXME, find the sinkpad by looping over the pads instead of
651 * looking it up by name */
652 if ((sinkpad = gst_element_get_pad (element, "sink")) == NULL) {
653 /* if no pad is found we can't do anything */
654 GST_WARNING_OBJECT (decode_bin, "could not find sinkpad in element");
658 /* now add the element to the bin first */
659 GST_DEBUG_OBJECT (decode_bin, "adding %s", GST_OBJECT_NAME (element));
660 gst_bin_add (GST_BIN (decode_bin), element);
662 /* set to ready first so it is ready */
663 gst_element_set_state (element, GST_STATE_READY);
666 /* Insert a queue between demuxer and decoder */
667 GST_DEBUG_OBJECT (decode_bin,
668 "Element %s is a demuxer, inserting a queue",
669 GST_OBJECT_NAME (srcelement));
671 queue = gst_element_factory_make ("queue", NULL);
672 g_object_set (G_OBJECT (queue), "max-size-buffers", 0, NULL);
673 g_object_set (G_OBJECT (queue), "max-size-time", 0LL, NULL);
674 g_object_set (G_OBJECT (queue), "max-size-bytes", 8192, NULL);
675 gst_bin_add (GST_BIN (decode_bin), queue);
676 gst_element_set_state (queue, GST_STATE_READY);
677 queuesinkpad = gst_element_get_pad (queue, "sink");
678 usedsrcpad = queuesrcpad = gst_element_get_pad (queue, "src");
680 gst_pad_link (pad, queuesinkpad);
683 if ((ret = gst_pad_link (usedsrcpad, sinkpad)) != GST_PAD_LINK_OK) {
684 GST_DEBUG_OBJECT (decode_bin, "link failed on pad %s:%s, reason %d",
685 GST_DEBUG_PAD_NAME (pad), ret);
686 /* get rid of the sinkpad */
687 gst_object_unref (sinkpad);
688 /* this element did not work, remove it again and continue trying
689 * other elements, the element will be disposed. */
691 gst_element_set_state (queue, GST_STATE_NULL);
692 gst_element_set_state (element, GST_STATE_NULL);
694 gst_pad_unlink (pad, queuesrcpad);
695 gst_object_unref (queuesrcpad);
696 gst_object_unref (queuesinkpad);
697 gst_bin_remove (GST_BIN (decode_bin), queue);
699 gst_bin_remove (GST_BIN (decode_bin), element);
704 GST_DEBUG_OBJECT (decode_bin, "linked on pad %s:%s",
705 GST_DEBUG_PAD_NAME (usedsrcpad));
708 decode_bin->queues = g_list_append (decode_bin->queues, queue);
709 g_signal_connect (G_OBJECT (queue),
710 "overrun", G_CALLBACK (queue_filled_cb), decode_bin);
713 /* The link worked, now figure out what it was that we connected */
715 /* make sure we catch unlink signals */
716 sig = g_signal_connect (G_OBJECT (usedsrcpad), "unlinked",
717 G_CALLBACK (unlinked), decode_bin);
719 /* keep a ref to the signal id so that we can disconnect the signal callback */
720 g_object_set_data (G_OBJECT (pad), "unlinked_id", GINT_TO_POINTER (sig));
722 /* now that we added the element we can try to continue autoplugging
723 * on it until we have a raw type */
724 close_link (element, decode_bin);
725 /* change the state of the element to that of the parent */
726 gst_element_set_state (element, GST_STATE_PAUSED);
730 /* get rid of the sinkpad now */
731 gst_object_unref (sinkpad);
733 gst_element_set_state (queue, GST_STATE_PAUSED);
734 gst_object_unref (queuesrcpad);
735 gst_object_unref (queuesinkpad);
747 get_our_ghost_pad (GstDecodeBin * decode_bin, GstPad * pad)
752 if (pad == NULL || !GST_PAD_IS_SRC (pad)) {
753 GST_DEBUG_OBJECT (decode_bin, "pad NULL or not SRC pad");
757 if (GST_IS_GHOST_PAD (pad)) {
758 GstElement *parent = gst_pad_get_parent (pad);
760 GST_DEBUG_OBJECT (decode_bin, "pad parent %s", GST_ELEMENT_NAME (parent));
762 if (parent == GST_ELEMENT (decode_bin)) {
763 GST_DEBUG_OBJECT (decode_bin, "pad is our ghostpad");
764 gst_object_unref (parent);
767 GST_DEBUG_OBJECT (decode_bin, "pad is ghostpad but not ours");
768 gst_object_unref (parent);
773 GST_DEBUG_OBJECT (decode_bin, "looping over ghostpads");
774 ghostpads = GST_REAL_PAD (pad)->ghostpads;
778 ghostpad = get_our_ghost_pad (decode_bin, GST_PAD (ghostpads->data));
782 ghostpads = g_list_next (ghostpads);
784 GST_DEBUG_OBJECT (decode_bin, "done looping over ghostpads, nothing found");
790 /* remove all downstream elements starting from the given pad.
791 * Also make sure to remove the ghostpad we created for the raw
795 remove_element_chain (GstDecodeBin * decode_bin, GstPad * pad)
797 GList *int_links, *walk;
798 GstElement *elem = GST_ELEMENT (GST_OBJECT_PARENT (pad));
800 while (GST_OBJECT_PARENT (elem) &&
801 GST_OBJECT_PARENT (elem) != GST_OBJECT (decode_bin))
802 elem = GST_ELEMENT (GST_OBJECT_PARENT (elem));
804 GST_DEBUG_OBJECT (decode_bin, "%s:%s", GST_DEBUG_PAD_NAME (pad));
805 int_links = gst_pad_get_internal_links (pad);
807 /* remove all elements linked to this pad up to the ghostpad
808 * that we created for this stream */
809 for (walk = int_links; walk; walk = g_list_next (walk)) {
814 pad = GST_PAD (walk->data);
815 GST_DEBUG_OBJECT (decode_bin, "inspecting internal pad %s:%s",
816 GST_DEBUG_PAD_NAME (pad));
818 ghostpad = get_our_ghost_pad (decode_bin, pad);
820 GST_DEBUG_OBJECT (decode_bin, "found our ghost pad %s:%s for %s:%s",
821 GST_DEBUG_PAD_NAME (ghostpad), GST_DEBUG_PAD_NAME (pad));
823 g_signal_emit (G_OBJECT (decode_bin),
824 gst_decode_bin_signals[SIGNAL_REMOVED_DECODED_PAD], 0, ghostpad);
826 gst_element_remove_pad (GST_ELEMENT (decode_bin), ghostpad);
829 GST_DEBUG_OBJECT (decode_bin, "not one of our ghostpads");
832 peer = gst_pad_get_peer (pad);
836 GST_DEBUG_OBJECT (decode_bin, "internal pad %s:%s linked to pad %s:%s",
837 GST_DEBUG_PAD_NAME (pad), GST_DEBUG_PAD_NAME (peer));
840 GstElement *parent = gst_pad_get_parent_element (peer);
843 if (parent != GST_ELEMENT (decode_bin)) {
844 GST_DEBUG_OBJECT (decode_bin, "dead end pad %s:%s",
845 GST_DEBUG_PAD_NAME (peer));
847 GST_DEBUG_OBJECT (decode_bin, "recursing element %s on pad %s:%s",
848 GST_ELEMENT_NAME (elem), GST_DEBUG_PAD_NAME (pad));
849 remove_element_chain (decode_bin, peer);
851 gst_object_unref (parent);
854 gst_object_unref (peer);
856 GST_DEBUG_OBJECT (decode_bin, "removing %s", GST_ELEMENT_NAME (elem));
858 g_list_free (int_links);
860 gst_element_set_state (elem, GST_STATE_NULL);
862 gst_bin_remove (GST_BIN (decode_bin), elem);
865 /* Make sure we don't have a full queue and empty queue situation */
867 queue_filled_cb (GstElement * queue, GstDecodeBin * decode_bin)
870 gboolean increase = FALSE;
873 g_object_get (G_OBJECT (queue), "current-level-bytes", &bytes, NULL);
874 GST_DEBUG_OBJECT (decode_bin, "One of the queues is full at %d bytes", bytes);
876 if (bytes > (20 * 1024 * 1024)) {
877 GST_WARNING_OBJECT (decode_bin,
878 "Queue is bigger than 20Mbytes, something else is going wrong");
882 for (tmp = decode_bin->queues; tmp; tmp = g_list_next (tmp)) {
883 GstElement *aqueue = GST_ELEMENT (tmp->data);
884 guint levelbytes = -1;
886 if (aqueue != queue) {
887 g_object_get (G_OBJECT (aqueue),
888 "current-level-bytes", &levelbytes, NULL);
889 if (levelbytes == 0) {
897 * Increase the queue size by 1Mbyte if it is over 1Mb, else double its current limit
899 if (bytes > 1024 * 1024)
900 bytes += 1024 * 1024;
903 GST_DEBUG_OBJECT (decode_bin,
904 "One of the other queues is empty, increasing queue byte limit to %d",
906 g_object_set (G_OBJECT (queue), "max-size-bytes", bytes, NULL);
908 GST_DEBUG_OBJECT (decode_bin,
909 "Queue is full but other queues are not empty, not doing anything");
912 /* This function will be called when a dynamic pad is created on an element.
913 * We try to continue autoplugging on this new pad. */
915 new_pad (GstElement * element, GstPad * pad, GstDynamic * dynamic)
917 GstDecodeBin *decode_bin = dynamic->decode_bin;
920 GST_OBJECT_LOCK (decode_bin);
921 if (decode_bin->shutting_down)
923 GST_OBJECT_UNLOCK (decode_bin);
925 GST_STATE_LOCK (decode_bin);
926 if (decode_bin->shutting_down)
929 /* see if any more pending dynamic connections exist */
930 gboolean more = gst_decode_bin_is_dynamic (decode_bin);
932 caps = gst_pad_get_caps (pad);
933 close_pad_link (element, pad, caps, decode_bin, more);
935 gst_caps_unref (caps);
936 GST_STATE_UNLOCK (decode_bin);
941 GST_OBJECT_UNLOCK (decode_bin);
945 GST_STATE_UNLOCK (decode_bin);
949 /* this signal is fired when an element signals the no_more_pads signal.
950 * This means that the element will not generate more dynamic pads and
951 * we can remove the element from the list of dynamic elements. When we
952 * have no more dynamic elements in the pipeline, we can fire a no_more_pads
953 * signal ourselves. */
955 no_more_pads (GstElement * element, GstDynamic * dynamic)
957 GstDecodeBin *decode_bin = dynamic->decode_bin;
959 GST_DEBUG_OBJECT (decode_bin, "no more pads on element %s",
960 GST_ELEMENT_NAME (element));
962 /* remove the element from the list of dynamic elements */
963 decode_bin->dynamics = g_list_remove (decode_bin->dynamics, dynamic);
964 dynamic_free (dynamic);
966 /* if we have no more dynamic elements, we have no chance of creating
967 * more pads, so we fire the no_more_pads signal */
968 if (decode_bin->dynamics == NULL) {
969 if (decode_bin->numwaiting == 0) {
970 GST_DEBUG_OBJECT (decode_bin,
971 "no more dynamic elements, removing fakesink");
972 remove_fakesink (decode_bin);
974 GST_DEBUG_OBJECT (decode_bin,
975 "no more dynamic elements, signaling no_more_pads");
976 gst_element_no_more_pads (GST_ELEMENT (decode_bin));
978 GST_DEBUG_OBJECT (decode_bin, "we have more dynamic elements");
983 is_our_kid (GstElement * e, GstDecodeBin * decode_bin)
988 parent = (GstElement *) gst_object_get_parent ((GstObject *) e);
989 ret = (parent == (GstElement *) decode_bin);
992 gst_object_unref ((GstObject *) parent);
997 /* This function will be called when a pad is disconnected for some reason */
999 unlinked (GstPad * pad, GstPad * peerpad, GstDecodeBin * decode_bin)
1002 GstElement *element, *peer;
1004 /* inactivate pad */
1005 gst_pad_set_active (pad, GST_ACTIVATE_NONE);
1007 element = gst_pad_get_parent_element (pad);
1008 peer = gst_pad_get_parent_element (peerpad);
1010 if (!is_our_kid (peer, decode_bin))
1013 /* remove all elements linked to the peerpad */
1014 remove_element_chain (decode_bin, peerpad);
1016 /* if an element removes two pads, then we don't want this twice */
1017 if (g_list_find (decode_bin->dynamics, element) != NULL)
1020 GST_DEBUG_OBJECT (decode_bin, "pad removal while alive - chained?");
1022 dyn = dynamic_create (element, decode_bin);
1023 /* and add this element to the dynamic elements */
1024 decode_bin->dynamics = g_list_prepend (decode_bin->dynamics, dyn);
1027 gst_object_unref (element);
1028 gst_object_unref (peer);
1031 /* this function inspects the given element and tries to connect something
1032 * on the srcpads. If there are dynamic pads, it sets up a signal handler to
1033 * continue autoplugging when they become available */
1035 close_link (GstElement * element, GstDecodeBin * decode_bin)
1038 gboolean dynamic = FALSE;
1039 GList *to_connect = NULL;
1042 GST_DEBUG_OBJECT (decode_bin, "closing links with element %s",
1043 GST_ELEMENT_NAME (element));
1045 /* loop over all the padtemplates */
1046 for (pads = GST_ELEMENT_GET_CLASS (element)->padtemplates; pads;
1047 pads = g_list_next (pads)) {
1048 GstPadTemplate *templ = GST_PAD_TEMPLATE (pads->data);
1049 const gchar *templ_name;
1051 /* we are only interested in source pads */
1052 if (GST_PAD_TEMPLATE_DIRECTION (templ) != GST_PAD_SRC)
1055 templ_name = GST_PAD_TEMPLATE_NAME_TEMPLATE (templ);
1056 GST_DEBUG_OBJECT (decode_bin, "got a source pad template %s", templ_name);
1058 /* figure out what kind of pad this is */
1059 switch (GST_PAD_TEMPLATE_PRESENCE (templ)) {
1060 case GST_PAD_ALWAYS:
1062 /* get the pad that we need to autoplug */
1063 GstPad *pad = gst_element_get_pad (element, templ_name);
1066 GST_DEBUG_OBJECT (decode_bin, "got the pad for always template %s",
1068 /* here is the pad, we need to autoplug it */
1069 to_connect = g_list_prepend (to_connect, pad);
1071 /* strange, pad is marked as always but it's not
1072 * there. Fix the element */
1073 GST_WARNING_OBJECT (decode_bin,
1074 "could not get the pad for always template %s", templ_name);
1078 case GST_PAD_SOMETIMES:
1080 /* try to get the pad to see if it is already created or
1082 GstPad *pad = gst_element_get_pad (element, templ_name);
1085 GST_DEBUG_OBJECT (decode_bin, "got the pad for sometimes template %s",
1087 /* the pad is created, we need to autoplug it */
1088 to_connect = g_list_prepend (to_connect, pad);
1090 GST_DEBUG_OBJECT (decode_bin,
1091 "did not get the sometimes pad of template %s", templ_name);
1092 /* we have an element that will create dynamic pads */
1097 case GST_PAD_REQUEST:
1098 /* ignore request pads */
1099 GST_DEBUG_OBJECT (decode_bin, "ignoring request padtemplate %s",
1107 GST_DEBUG_OBJECT (decode_bin, "got a dynamic element here");
1108 /* ok, this element has dynamic pads, set up the signal handlers to be
1109 * notified of them */
1111 dyn = dynamic_create (element, decode_bin);
1112 /* and add this element to the dynamic elements */
1113 decode_bin->dynamics = g_list_prepend (decode_bin->dynamics, dyn);
1116 /* Check if this is an element with more than 1 pad. If this element
1117 * has more than 1 pad, we need to be carefull not to signal the
1118 * no_more_pads signal after connecting the first pad. */
1119 more = g_list_length (to_connect) > 1;
1121 /* now loop over all the pads we need to connect */
1122 for (pads = to_connect; pads; pads = g_list_next (pads)) {
1123 GstPad *pad = GST_PAD_CAST (pads->data);
1126 /* we have more pads if we have more than 1 pad to connect or
1127 * dynamics. If we have only 1 pad and no dynamics, more will be
1128 * set to FALSE and the no-more-pads signal will be fired. Note
1129 * that this can change after the close_pad_link call. */
1130 more |= gst_decode_bin_is_dynamic (decode_bin);
1132 GST_DEBUG_OBJECT (decode_bin, "closing pad link for %s",
1133 GST_OBJECT_NAME (pad));
1135 /* continue autoplugging on the pads */
1136 caps = gst_pad_get_caps (pad);
1137 close_pad_link (element, pad, caps, decode_bin, more);
1139 gst_caps_unref (caps);
1141 gst_object_unref (pad);
1143 g_list_free (to_connect);
1146 /* this is the signal handler for the typefind element have_type signal.
1147 * It tries to continue autoplugging on the typefind src pad */
1149 type_found (GstElement * typefind, guint probability, GstCaps * caps,
1150 GstDecodeBin * decode_bin)
1155 GST_STATE_LOCK (decode_bin);
1156 if (decode_bin->shutting_down)
1159 GST_DEBUG_OBJECT (decode_bin, "typefind found caps %" GST_PTR_FORMAT, caps);
1161 /* autoplug the new pad with the caps that the signal gave us. */
1162 pad = gst_element_get_pad (typefind, "src");
1163 close_pad_link (typefind, pad, caps, decode_bin, FALSE);
1164 gst_object_unref (pad);
1166 dynamic = gst_decode_bin_is_dynamic (decode_bin);
1167 if (dynamic == FALSE) {
1168 GST_DEBUG_OBJECT (decode_bin, "we have no dynamic elements anymore");
1169 /* if we have no dynamic elements, we know that no new pads
1170 * will be created and we can signal out no_more_pads signal */
1171 gst_element_no_more_pads (GST_ELEMENT (decode_bin));
1173 /* more dynamic elements exist that could create new pads */
1174 GST_DEBUG_OBJECT (decode_bin, "we have more dynamic elements");
1178 GST_STATE_UNLOCK (decode_bin);
1182 static GstStateChangeReturn
1183 gst_decode_bin_change_state (GstElement * element, GstStateChange transition)
1185 GstStateChangeReturn ret;
1186 GstDecodeBin *decode_bin;
1188 decode_bin = GST_DECODE_BIN (element);
1190 switch (transition) {
1191 case GST_STATE_CHANGE_NULL_TO_READY:
1192 decode_bin->numpads = 0;
1193 decode_bin->numwaiting = 0;
1194 decode_bin->dynamics = NULL;
1196 case GST_STATE_CHANGE_READY_TO_PAUSED:
1197 GST_OBJECT_LOCK (decode_bin);
1198 decode_bin->shutting_down = FALSE;
1199 GST_OBJECT_UNLOCK (decode_bin);
1201 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
1202 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
1204 case GST_STATE_CHANGE_PAUSED_TO_READY:
1205 GST_OBJECT_LOCK (decode_bin);
1206 decode_bin->shutting_down = TRUE;
1207 GST_OBJECT_UNLOCK (decode_bin);
1213 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1215 switch (transition) {
1216 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
1217 case GST_STATE_CHANGE_PAUSED_TO_READY:
1219 case GST_STATE_CHANGE_READY_TO_NULL:
1220 free_dynamics (decode_bin);
1230 plugin_init (GstPlugin * plugin)
1232 GST_DEBUG_CATEGORY_INIT (gst_decode_bin_debug, "decodebin", 0, "decoder bin");
1234 return gst_element_register (plugin, "decodebin", GST_RANK_NONE,
1235 GST_TYPE_DECODE_BIN);
1238 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
1241 "decoder bin", plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME,