Added gst-plugins-base-subtitles0.10-0.10.34 for Meego Harmattan 1.2
[mafwsubrenderer] / gst-plugins-base-subtitles0.10 / gst / playback / gstdecodebin.c
1 /* GStreamer
2  * Copyright (C) <2004> Wim Taymans <wim.taymans@gmail.com>
3  *
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.
8  *
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.
13  *
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.
18  */
19
20 /**
21  * SECTION:element-decodebin
22  *
23  * #GstBin that auto-magically constructs a decoding pipeline using available
24  * decoders and demuxers via auto-plugging.
25  *
26  * When using decodebin in your application, connect a signal handler to
27  * #GstDecodeBin::new-decoded-pad and connect your sinks from within the
28  * callback function.
29  *
30  * <note>
31  * This element is deprecated and no longer supported. You should use the
32  * #uridecodebin or #decodebin2 element instead (or, even better: #playbin2).
33  * </note>
34  *
35  * Deprecated: use uridecodebin or decodebin2 instead.
36  */
37
38 #ifdef HAVE_CONFIG_H
39 #include "config.h"
40 #endif
41
42 #include <gst/gst-i18n-plugin.h>
43
44 #include <string.h>
45 #include <gst/gst.h>
46 #include <gst/pbutils/pbutils.h>
47
48 #include "gstplay-marshal.h"
49
50 /* generic templates */
51 static GstStaticPadTemplate decoder_bin_sink_template =
52 GST_STATIC_PAD_TEMPLATE ("sink",
53     GST_PAD_SINK,
54     GST_PAD_ALWAYS,
55     GST_STATIC_CAPS_ANY);
56
57 static GstStaticPadTemplate decoder_bin_src_template =
58 GST_STATIC_PAD_TEMPLATE ("src%d",
59     GST_PAD_SRC,
60     GST_PAD_SOMETIMES,
61     GST_STATIC_CAPS_ANY);
62
63 GST_DEBUG_CATEGORY_STATIC (gst_decode_bin_debug);
64 #define GST_CAT_DEFAULT gst_decode_bin_debug
65
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))
71
72 typedef struct _GstDecodeBin GstDecodeBin;
73 typedef struct _GstDecodeBinClass GstDecodeBinClass;
74
75 /**
76  * GstDecodeBin:
77  *
78  * Auto-plugging decoder element structure
79  */
80 struct _GstDecodeBin
81 {
82   GstBin bin;                   /* we extend GstBin */
83
84   GstElement *typefind;         /* this holds the typefind object */
85   GstElement *fakesink;
86
87   GList *dynamics;              /* list of dynamic connections */
88
89   GList *queues;                /* list of demuxer-decoder queues */
90
91   GList *probes;                /* list of PadProbeData */
92
93   GList *factories;             /* factories we can use for selecting elements */
94   gint numpads;
95   gint numwaiting;
96
97   gboolean have_type;
98   guint have_type_id;           /* signal id for the typefind element */
99
100   gboolean shutting_down;       /* stop pluggin if we're shutting down */
101
102   GType queue_type;             /* store the GType of queues, to aid in recognising them */
103
104   GMutex *cb_mutex;             /* Mutex for multi-threaded callbacks, such as removing the fakesink */
105 };
106
107 struct _GstDecodeBinClass
108 {
109   GstBinClass parent_class;
110
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);
117 };
118
119 /* signals */
120 enum
121 {
122   SIGNAL_NEW_DECODED_PAD,
123   SIGNAL_REMOVED_DECODED_PAD,
124   SIGNAL_UNKNOWN_TYPE,
125   SIGNAL_REDIRECT,
126   LAST_SIGNAL
127 };
128
129 /* Properties */
130 enum
131 {
132   PROP_0,
133   PROP_SINK_CAPS,
134 };
135
136
137 typedef struct
138 {
139   GstPad *pad;
140   gulong sigid;
141   gboolean done;
142 } PadProbeData;
143
144 /* this structure is created for all dynamic pads that could get created
145  * at runtime */
146 typedef struct
147 {
148   GstDecodeBin *decode_bin;     /* pointer to ourself */
149
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 */
153
154   GstPad *pad;                  /* the pad sending the signal */
155   gint caps_sig_id;             /* signal id of caps */
156 }
157 GstDynamic;
158
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);
167
168 static GstStateChangeReturn gst_decode_bin_change_state (GstElement * element,
169     GstStateChange transition);
170
171 static gboolean add_fakesink (GstDecodeBin * decode_bin);
172 static void remove_fakesink (GstDecodeBin * decode_bin);
173
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);
188
189 static void queue_filled_cb (GstElement * queue, GstDecodeBin * decode_bin);
190 static void queue_underrun_cb (GstElement * queue, GstDecodeBin * decode_bin);
191
192 static gboolean is_demuxer_element (GstElement * srcelement);
193
194 static GstElementClass *parent_class;
195 static guint gst_decode_bin_signals[LAST_SIGNAL] = { 0 };
196
197
198 static GType
199 gst_decode_bin_get_type (void)
200 {
201   static GType gst_decode_bin_type = 0;
202
203   if (!gst_decode_bin_type) {
204     static const GTypeInfo gst_decode_bin_info = {
205       sizeof (GstDecodeBinClass),
206       NULL,
207       NULL,
208       (GClassInitFunc) gst_decode_bin_class_init,
209       NULL,
210       NULL,
211       sizeof (GstDecodeBin),
212       0,
213       (GInstanceInitFunc) gst_decode_bin_init,
214       NULL
215     };
216
217     gst_decode_bin_type =
218         g_type_register_static (GST_TYPE_BIN, "GstDecodeBin",
219         &gst_decode_bin_info, 0);
220   }
221
222   return gst_decode_bin_type;
223 }
224
225 static void
226 gst_decode_bin_class_init (GstDecodeBinClass * klass)
227 {
228   GObjectClass *gobject_klass;
229   GstElementClass *gstelement_klass;
230
231   gobject_klass = (GObjectClass *) klass;
232   gstelement_klass = (GstElementClass *) klass;
233
234   parent_class = g_type_class_peek_parent (klass);
235
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;
240
241   /**
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.
246    *
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.
249    */
250   gst_decode_bin_signals[SIGNAL_NEW_DECODED_PAD] =
251       g_signal_new ("new-decoded-pad", G_TYPE_FROM_CLASS (klass),
252       G_SIGNAL_RUN_LAST,
253       G_STRUCT_OFFSET (GstDecodeBinClass, new_decoded_pad), NULL, NULL,
254       gst_play_marshal_VOID__OBJECT_BOOLEAN, G_TYPE_NONE, 2, GST_TYPE_PAD,
255       G_TYPE_BOOLEAN);
256   /**
257    * GstDecodeBin::removed-decoded-pad:
258    * @bin: The decodebin
259    * @pad: The pad that was removed
260    *
261    * This signal is emitted when a 'final' caps pad has been removed.
262    */
263   gst_decode_bin_signals[SIGNAL_REMOVED_DECODED_PAD] =
264       g_signal_new ("removed-decoded-pad", G_TYPE_FROM_CLASS (klass),
265       G_SIGNAL_RUN_LAST,
266       G_STRUCT_OFFSET (GstDecodeBinClass, removed_decoded_pad), NULL, NULL,
267       gst_marshal_VOID__OBJECT, G_TYPE_NONE, 1, GST_TYPE_PAD);
268   /**
269    * GstDecodeBin::unknown-type:
270    * @bin: The decodebin
271    * @pad: The new pad containing caps that cannot be resolved to a 'final'
272    *       stream type.
273    * @caps: The #GstCaps of the pad that cannot be resolved.
274    *
275    * This signal is emitted when a pad for which there is no further possible
276    * decoding is added to the decodebin.
277    */
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);
283
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));
288
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));
293
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>");
298
299   gstelement_klass->change_state =
300       GST_DEBUG_FUNCPTR (gst_decode_bin_change_state);
301 }
302
303 /* check if the bin is dynamic.
304  *
305  * If there are no outstanding dynamic connections, the bin is
306  * considered to be non-dynamic.
307  */
308 static gboolean
309 gst_decode_bin_is_dynamic (GstDecodeBin * decode_bin)
310 {
311   return decode_bin->dynamics != NULL;
312 }
313
314 /* the filter function for selecting the elements we can use in
315  * autoplugging */
316 static gboolean
317 gst_decode_bin_factory_filter (GstPluginFeature * feature,
318     GstDecodeBin * decode_bin)
319 {
320   guint rank;
321   const gchar *klass;
322
323   /* we only care about element factories */
324   if (!GST_IS_ELEMENT_FACTORY (feature))
325     return FALSE;
326
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) {
332     return FALSE;
333   }
334
335   /* only select elements with autoplugging rank */
336   rank = gst_plugin_feature_get_rank (feature);
337   if (rank < GST_RANK_MARGINAL)
338     return FALSE;
339
340   return TRUE;
341 }
342
343 /* function used to sort element features */
344 static gint
345 compare_ranks (GstPluginFeature * f1, GstPluginFeature * f2)
346 {
347   gint diff;
348   const gchar *rname1, *rname2;
349
350   diff = gst_plugin_feature_get_rank (f2) - gst_plugin_feature_get_rank (f1);
351   if (diff != 0)
352     return diff;
353
354   rname1 = gst_plugin_feature_get_name (f1);
355   rname2 = gst_plugin_feature_get_name (f2);
356
357   diff = strcmp (rname2, rname1);
358
359   return diff;
360 }
361
362 static void
363 print_feature (GstPluginFeature * feature)
364 {
365   const gchar *rname;
366
367   rname = gst_plugin_feature_get_name (feature);
368
369   GST_DEBUG ("%s", rname);
370 }
371
372 static void
373 gst_decode_bin_init (GstDecodeBin * decode_bin)
374 {
375   GList *factories;
376
377   decode_bin->cb_mutex = g_mutex_new ();
378
379   /* first filter out the interesting element factories */
380   factories = gst_default_registry_feature_filter (
381       (GstPluginFeatureFilter) gst_decode_bin_factory_filter,
382       FALSE, decode_bin);
383
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);
388
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");
393   } else {
394     GstPad *pad, *gpad;
395     GstPadTemplate *pad_tmpl;
396
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;
402     }
403
404     /* get the sinkpad */
405     pad = gst_element_get_static_pad (decode_bin->typefind, "sink");
406
407     /* get the pad template */
408     pad_tmpl = gst_static_pad_template_get (&decoder_bin_sink_template);
409
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);
414
415     gst_object_unref (pad_tmpl);
416     gst_object_unref (pad);
417
418     /* connect a signal to find out when the typefind element found
419      * a type */
420     decode_bin->have_type_id =
421         g_signal_connect (G_OBJECT (decode_bin->typefind), "have_type",
422         G_CALLBACK (type_found), decode_bin);
423   }
424   add_fakesink (decode_bin);
425
426   decode_bin->dynamics = NULL;
427   decode_bin->queues = NULL;
428   decode_bin->probes = NULL;
429 }
430
431 static void
432 gst_decode_bin_dispose (GObject * object)
433 {
434   GstDecodeBin *decode_bin;
435
436   decode_bin = GST_DECODE_BIN (object);
437
438   if (decode_bin->factories)
439     gst_plugin_feature_list_free (decode_bin->factories);
440   decode_bin->factories = NULL;
441
442   G_OBJECT_CLASS (parent_class)->dispose (object);
443
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);
448 }
449
450 static void
451 gst_decode_bin_set_sink_caps (GstDecodeBin * dbin, GstCaps * caps)
452 {
453   GST_DEBUG_OBJECT (dbin, "Setting new caps: %" GST_PTR_FORMAT, caps);
454
455   g_object_set (dbin->typefind, "force-caps", caps, NULL);
456 }
457
458 static GstCaps *
459 gst_decode_bin_get_sink_caps (GstDecodeBin * dbin)
460 {
461   GstCaps *caps;
462
463   GST_DEBUG_OBJECT (dbin, "Getting currently set caps");
464
465   g_object_get (dbin->typefind, "force-caps", &caps, NULL);
466
467   return caps;
468 }
469
470 static void
471 gst_decode_bin_set_property (GObject * object, guint prop_id,
472     const GValue * value, GParamSpec * pspec)
473 {
474   GstDecodeBin *dbin;
475
476   dbin = GST_DECODE_BIN (object);
477
478   switch (prop_id) {
479     case PROP_SINK_CAPS:
480       gst_decode_bin_set_sink_caps (dbin, g_value_get_boxed (value));
481       break;
482     default:
483       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
484       break;
485   }
486 }
487
488 static void
489 gst_decode_bin_get_property (GObject * object, guint prop_id,
490     GValue * value, GParamSpec * pspec)
491 {
492   GstDecodeBin *dbin;
493
494   dbin = GST_DECODE_BIN (object);
495   switch (prop_id) {
496     case PROP_SINK_CAPS:
497       g_value_take_boxed (value, gst_decode_bin_get_sink_caps (dbin));
498       break;
499     default:
500       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
501       break;
502   }
503 }
504
505 static void
506 gst_decode_bin_finalize (GObject * object)
507 {
508   GstDecodeBin *decode_bin = GST_DECODE_BIN (object);
509
510   g_mutex_free (decode_bin->cb_mutex);
511
512   G_OBJECT_CLASS (parent_class)->finalize (object);
513 }
514
515 struct DynFind
516 {
517   GstElement *elem;
518   GstPad *pad;
519 };
520
521 static gint
522 find_dynamic (GstDynamic * dyn, struct DynFind *info)
523 {
524   if (dyn->element == info->elem && dyn->pad == info->pad)
525     return 0;
526   return 1;
527 }
528
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 */
533 static void
534 dynamic_add (GstElement * element, GstPad * pad, GstDecodeBin * decode_bin)
535 {
536   GstDynamic *dyn;
537   struct DynFind find_info;
538   GList *found;
539
540   g_return_if_fail (element != NULL);
541
542   /* do a search that this entry doesn't already exist */
543   find_info.elem = element;
544   find_info.pad = pad;
545   found = g_list_find_custom (decode_bin->dynamics, &find_info,
546       (GCompareFunc) find_dynamic);
547   if (found != NULL)
548     goto exit;
549
550   /* take refs */
551   dyn = g_new0 (GstDynamic, 1);
552   dyn->element = gst_object_ref (element);
553   dyn->decode_bin = gst_object_ref (decode_bin);
554   if (pad) {
555     dyn->pad = gst_object_ref (pad);
556     GST_DEBUG_OBJECT (decode_bin, "dynamic create for pad %" GST_PTR_FORMAT,
557         pad);
558     dyn->caps_sig_id = g_signal_connect (G_OBJECT (pad), "notify::caps",
559         G_CALLBACK (new_caps), dyn);
560   } else {
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);
567   }
568
569   /* and add this element to the dynamic elements */
570   decode_bin->dynamics = g_list_prepend (decode_bin->dynamics, dyn);
571
572   return;
573 exit:
574   if (element) {
575     GST_DEBUG_OBJECT (decode_bin, "Dynamic element already added: %"
576         GST_PTR_FORMAT, element);
577   } else {
578     GST_DEBUG_OBJECT (decode_bin, "Dynamic pad already added: %"
579         GST_PTR_FORMAT, pad);
580   }
581 }
582
583 static void
584 dynamic_free (GstDynamic * dyn)
585 {
586   GST_DEBUG_OBJECT (dyn->decode_bin, "dynamic free");
587
588   /* disconnect signals */
589   if (dyn->np_sig_id)
590     g_signal_handler_disconnect (G_OBJECT (dyn->element), dyn->np_sig_id);
591   if (dyn->nmp_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);
595
596   if (dyn->pad)
597     gst_object_unref (dyn->pad);
598   dyn->pad = NULL;
599   if (dyn->element)
600     gst_object_unref (dyn->element);
601   dyn->element = NULL;
602
603   gst_object_unref (dyn->decode_bin);
604   dyn->decode_bin = NULL;
605
606   g_free (dyn);
607 }
608
609 static void
610 free_dynamics (GstDecodeBin * decode_bin)
611 {
612   GList *dyns;
613
614   for (dyns = decode_bin->dynamics; dyns; dyns = g_list_next (dyns)) {
615     GstDynamic *dynamic = (GstDynamic *) dyns->data;
616
617     dynamic_free (dynamic);
618   }
619   g_list_free (decode_bin->dynamics);
620   decode_bin->dynamics = NULL;
621 }
622
623 /* this function runs through the element factories and returns a list
624  * of all elements that are able to sink the given caps
625  */
626 static GList *
627 find_compatibles (GstDecodeBin * decode_bin, const GstCaps * caps)
628 {
629   GList *factories;
630   GList *to_try = NULL;
631
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;
637     GList *walk;
638
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;
643
644       /* we only care about the sink templates */
645       if (templ->direction == GST_PAD_SINK) {
646         gboolean can_intersect;
647         GstCaps *tmpl_caps;
648
649         /* try to intersect the caps with the caps of the template */
650         tmpl_caps = gst_static_caps_get (&templ->static_caps);
651
652         can_intersect = gst_caps_can_intersect (caps, tmpl_caps);
653         gst_caps_unref (tmpl_caps);
654
655         /* check if the intersection is empty */
656         if (can_intersect) {
657           /* non empty intersection, we can use this element */
658           to_try = g_list_prepend (to_try, factory);
659           break;
660         }
661       }
662     }
663   }
664   to_try = g_list_reverse (to_try);
665
666   return to_try;
667 }
668
669 static gboolean
670 mimetype_is_raw (const gchar * mimetype)
671 {
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");
676 }
677
678 static void
679 free_pad_probes (GstDecodeBin * decode_bin)
680 {
681   GList *tmp;
682
683   /* Remove pad probes */
684   for (tmp = decode_bin->probes; tmp; tmp = g_list_next (tmp)) {
685     PadProbeData *data = (PadProbeData *) tmp->data;
686
687     gst_pad_remove_data_probe (data->pad, data->sigid);
688     g_free (data);
689   }
690   g_list_free (decode_bin->probes);
691   decode_bin->probes = NULL;
692 }
693
694 /* used when we need to remove a probe because the decoder we plugged failed
695  * to activate */
696 static void
697 free_pad_probe_for_element (GstDecodeBin * decode_bin, GstElement * element)
698 {
699   GList *l;
700
701   for (l = decode_bin->probes; l != NULL; l = g_list_next (l)) {
702     PadProbeData *data = (PadProbeData *) l->data;
703
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);
707       g_free (data);
708       return;
709     }
710   }
711 }
712
713 static gboolean
714 add_fakesink (GstDecodeBin * decode_bin)
715 {
716   if (decode_bin->fakesink != NULL)
717     return TRUE;
718
719   g_mutex_lock (decode_bin->cb_mutex);
720
721   decode_bin->fakesink = gst_element_factory_make ("fakesink", "fakesink");
722   if (!decode_bin->fakesink)
723     goto no_fakesink;
724
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);
728
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;
734   }
735   g_mutex_unlock (decode_bin->cb_mutex);
736   return TRUE;
737
738   /* ERRORS */
739 no_fakesink:
740   {
741     g_warning ("can't find fakesink element, decodebin will not work");
742     g_mutex_unlock (decode_bin->cb_mutex);
743     return FALSE;
744   }
745 }
746
747 static void
748 remove_fakesink (GstDecodeBin * decode_bin)
749 {
750   gboolean removed_fakesink = FALSE;
751
752   if (decode_bin->fakesink == NULL)
753     return;
754
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");
758
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;
766
767     removed_fakesink = TRUE;
768   }
769   g_mutex_unlock (decode_bin->cb_mutex);
770
771   if (removed_fakesink) {
772     free_pad_probes (decode_bin);
773   }
774 }
775
776 /* this should be implemented with _pad_block() */
777 static gboolean
778 pad_probe (GstPad * pad, GstMiniObject * data, GstDecodeBin * decode_bin)
779 {
780   GList *tmp;
781   gboolean alldone = TRUE;
782
783   for (tmp = decode_bin->probes; tmp; tmp = g_list_next (tmp)) {
784     PadProbeData *pdata = (PadProbeData *) tmp->data;
785
786     if (pdata->pad == pad) {
787       if (GST_IS_BUFFER (data)) {
788         if (!pdata->done)
789           decode_bin->numwaiting--;
790         pdata->done = TRUE;
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()... */
796         if (!pdata->done)
797           decode_bin->numwaiting--;
798         pdata->done = TRUE;
799       }
800     }
801
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);
805       alldone = FALSE;
806     }
807   }
808   if (alldone)
809     remove_fakesink (decode_bin);
810   return TRUE;
811 }
812
813 /* FIXME: this should be somehow merged with the queue code in
814  * try_to_link_1() to reduce code duplication */
815 static GstPad *
816 add_raw_queue (GstDecodeBin * decode_bin, GstPad * pad)
817 {
818   GstElement *queue = NULL;
819   GstPad *queuesinkpad = NULL, *queuesrcpad = NULL;
820
821   queue = gst_element_factory_make ("queue", NULL);
822   decode_bin->queue_type = G_OBJECT_TYPE (queue);
823
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");
831
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);
840   }
841
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);
847
848   gst_element_set_state (queue, GST_STATE_PAUSED);
849   gst_object_unref (queuesinkpad);
850
851   return queuesrcpad;
852 }
853
854 /* given a pad and a caps from an element, find the list of elements
855  * that could connect to the pad
856  *
857  * If the pad has a raw format, this function will create a ghostpad
858  * for the pad onto the decodebin.
859  *
860  * If no compatible elements could be found, this function will signal
861  * the unknown_type signal.
862  */
863 static void
864 close_pad_link (GstElement * element, GstPad * pad, GstCaps * caps,
865     GstDecodeBin * decode_bin, gboolean more)
866 {
867   GstStructure *structure;
868   const gchar *mimetype;
869   gchar *padname;
870   gint diff;
871
872   padname = gst_pad_get_name (pad);
873   diff = strncmp (padname, "current_", 8);
874   g_free (padname);
875
876   /* hack.. ignore current pads */
877   if (!diff)
878     return;
879
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))
883     goto unknown_type;
884
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))
888     goto dont_know_yet;
889
890   GST_LOG_OBJECT (element, "trying to close %" GST_PTR_FORMAT, caps);
891
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);
897
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
900    * fixed. */
901   if (mimetype_is_raw (mimetype)) {
902     GstPadTemplate *tmpl;
903     gchar *padname;
904     GstPad *ghost;
905     PadProbeData *data;
906
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));
914
915       pad = add_raw_queue (decode_bin, pad);
916     }
917
918     /* make a unique name for this new pad */
919     padname = g_strdup_printf ("src%d", decode_bin->numpads);
920     decode_bin->numpads++;
921
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);
926
927     gst_pad_set_active (ghost, TRUE);
928     gst_element_add_pad (GST_ELEMENT (decode_bin), ghost);
929
930     data = g_new0 (PadProbeData, 1);
931     data->pad = pad;
932     data->done = FALSE;
933
934     /* FIXME, use _pad_block() */
935     data->sigid = gst_pad_add_data_probe (pad, G_CALLBACK (pad_probe),
936         decode_bin);
937     decode_bin->numwaiting++;
938
939     decode_bin->probes = g_list_append (decode_bin->probes, data);
940
941     GST_LOG_OBJECT (element, "closed pad %s", padname);
942
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");
948
949     g_free (padname);
950
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);
955   } else {
956     GList *to_try;
957
958     /* if the caps has many types, we need to delay */
959     if (!gst_caps_is_fixed (caps))
960       goto many_types;
961
962     /* continue plugging, first find all compatible elements */
963     to_try = find_compatibles (decode_bin, caps);
964     if (to_try == NULL)
965       /* no compatible elements, we cannot go on */
966       goto unknown_type;
967
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");
971       goto unknown_type;
972     }
973
974     /* can free the list again now */
975     g_list_free (to_try);
976   }
977   return;
978
979   /* ERRORS */
980 unknown_type:
981   {
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);
985
986     gst_element_post_message (GST_ELEMENT_CAST (decode_bin),
987         gst_missing_decoder_message_new (GST_ELEMENT_CAST (decode_bin), caps));
988
989     if (element == decode_bin->typefind) {
990       gchar *desc;
991
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."),
995               desc),
996           ("No decoder to handle media type '%s'",
997               gst_structure_get_name (gst_caps_get_structure (caps, 0))));
998       g_free (desc);
999     }
1000
1001     return;
1002   }
1003 dont_know_yet:
1004   {
1005     GST_LOG_OBJECT (pad, "type is not known yet");
1006     goto setup_caps_delay;
1007   }
1008 many_types:
1009   {
1010     GST_LOG_OBJECT (pad, "many possible types");
1011     goto setup_caps_delay;
1012   }
1013 setup_caps_delay:
1014   {
1015     GST_LOG_OBJECT (pad, "setting up a delayed link");
1016     dynamic_add (element, pad, decode_bin);
1017     return;
1018   }
1019 }
1020
1021 /* Decide whether an element is a demuxer based on the
1022  * klass and number/type of src pad templates it has */
1023 static gboolean
1024 is_demuxer_element (GstElement * srcelement)
1025 {
1026   GstElementFactory *srcfactory;
1027   GstElementClass *elemclass;
1028   GList *walk;
1029   const gchar *klass;
1030   gint potential_src_pads = 0;
1031
1032   srcfactory = gst_element_get_factory (srcelement);
1033   klass = gst_element_factory_get_klass (srcfactory);
1034
1035   /* Can't be a demuxer unless it has Demux in the klass name */
1036   if (klass == NULL || !strstr (klass, "Demux"))
1037     return FALSE;
1038
1039   /* Walk the src pad templates and count how many the element
1040    * might produce */
1041   elemclass = GST_ELEMENT_GET_CLASS (srcelement);
1042
1043   walk = gst_element_class_get_pad_template_list (elemclass);
1044   while (walk != NULL) {
1045     GstPadTemplate *templ;
1046
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 */
1054           else
1055             potential_src_pads += 1;
1056           break;
1057         case GST_PAD_REQUEST:
1058           potential_src_pads += 2;
1059           break;
1060       }
1061     }
1062     walk = g_list_next (walk);
1063   }
1064
1065   if (potential_src_pads < 2)
1066     return FALSE;
1067
1068   return TRUE;
1069 }
1070
1071 /*
1072  * given a list of element factories, try to link one of the factories
1073  * to the given pad.
1074  *
1075  * The function returns the element that was successfully linked to the
1076  * pad.
1077  */
1078 static GstElement *
1079 try_to_link_1 (GstDecodeBin * decode_bin, GstElement * srcelement, GstPad * pad,
1080     GList * factories)
1081 {
1082   GList *walk;
1083   GstElement *result = NULL;
1084   gboolean isdemux = FALSE;
1085   GstPad *queuesinkpad = NULL, *queuesrcpad = NULL;
1086   GstElement *queue = NULL;
1087   GstPad *usedsrcpad = pad;
1088
1089   /* Check if the parent of the src pad is a demuxer */
1090   isdemux = is_demuxer_element (srcelement);
1091
1092   if (isdemux && factories != NULL) {
1093     GstPadLinkReturn dqlink;
1094
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);
1101
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),
1104         NULL);
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");
1110
1111     dqlink = gst_pad_link (pad, queuesinkpad);
1112     g_return_val_if_fail (dqlink == GST_PAD_LINK_OK, NULL);
1113   }
1114
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;
1120     GstPad *sinkpad;
1121
1122     GST_DEBUG_OBJECT (decode_bin, "trying to link %s",
1123         gst_plugin_feature_get_name (GST_PLUGIN_FEATURE (factory)));
1124
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)));
1130       continue;
1131     }
1132
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");
1139       continue;
1140     }
1141
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);
1145
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);
1158       continue;
1159     }
1160
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);
1170     } else {
1171       GST_DEBUG_OBJECT (decode_bin, "linked on pad %s:%s",
1172           GST_DEBUG_PAD_NAME (usedsrcpad));
1173
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);
1181       }
1182
1183       /* The link worked, now figure out what it was that we connected */
1184
1185       /* make sure we catch unlink signals */
1186       g_signal_connect (G_OBJECT (pad), "unlinked",
1187           G_CALLBACK (unlinked), decode_bin);
1188
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);
1192
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);
1202         continue;
1203       }
1204
1205       result = element;
1206
1207       /* get rid of the sinkpad now */
1208       gst_object_unref (sinkpad);
1209
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);
1214         queue = NULL;
1215         gst_object_unref (queuesrcpad);
1216         gst_object_unref (queuesinkpad);
1217       }
1218
1219       /* and exit */
1220       goto done;
1221     }
1222   }
1223 done:
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);
1231   }
1232   return result;
1233 }
1234
1235 static GstPad *
1236 get_our_ghost_pad (GstDecodeBin * decode_bin, GstPad * pad)
1237 {
1238   GstIterator *pad_it = NULL;
1239   GstPad *db_pad = NULL;
1240   gboolean done = FALSE;
1241
1242   if (pad == NULL || !GST_PAD_IS_SRC (pad)) {
1243     GST_DEBUG_OBJECT (decode_bin, "pad NULL or not SRC pad");
1244     return NULL;
1245   }
1246
1247   /* our ghostpads are the sourcepads */
1248   pad_it = gst_element_iterate_src_pads (GST_ELEMENT (decode_bin));
1249   while (!done) {
1250     db_pad = NULL;
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;
1257
1258           target_pad = gst_ghost_pad_get_target (GST_GHOST_PAD (db_pad));
1259           done = (target_pad == pad);
1260           if (target_pad)
1261             gst_object_unref (target_pad);
1262
1263           if (done) {
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));
1267             break;
1268           }
1269         }
1270         /* Not the right one */
1271         gst_object_unref (db_pad);
1272         break;
1273       case GST_ITERATOR_RESYNC:
1274         gst_iterator_resync (pad_it);
1275         break;
1276       case GST_ITERATOR_ERROR:
1277         done = TRUE;
1278         break;
1279       case GST_ITERATOR_DONE:
1280         done = TRUE;
1281         break;
1282     }
1283   }
1284   gst_iterator_free (pad_it);
1285
1286   return db_pad;
1287 }
1288
1289 /* remove all downstream elements starting from the given pad.
1290  * Also make sure to remove the ghostpad we created for the raw
1291  * decoded stream.
1292  */
1293 static void
1294 remove_element_chain (GstDecodeBin * decode_bin, GstPad * pad)
1295 {
1296   GstIterator *iter;
1297   gboolean done = FALSE;
1298   gpointer item;
1299   GstElement *elem = GST_ELEMENT (GST_OBJECT_PARENT (pad));
1300
1301   while (GST_OBJECT_PARENT (elem) &&
1302       GST_OBJECT_PARENT (elem) != GST_OBJECT (decode_bin))
1303     elem = GST_ELEMENT (GST_OBJECT_PARENT (elem));
1304
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);
1309   }
1310
1311   GST_DEBUG_OBJECT (decode_bin, "%s:%s", GST_DEBUG_PAD_NAME (pad));
1312   iter = gst_pad_iterate_internal_links (pad);
1313   if (!iter)
1314     goto no_iter;
1315
1316   /* remove all elements linked to this pad up to the ghostpad
1317    * that we created for this stream */
1318   while (!done) {
1319     switch (gst_iterator_next (iter, &item)) {
1320       case GST_ITERATOR_OK:{
1321         GstPad *pad;
1322         GstPad *ghostpad;
1323         GstPad *peer;
1324
1325         pad = GST_PAD (item);
1326         GST_DEBUG_OBJECT (decode_bin, "inspecting internal pad %s:%s",
1327             GST_DEBUG_PAD_NAME (pad));
1328
1329         ghostpad = get_our_ghost_pad (decode_bin, pad);
1330         if (ghostpad) {
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));
1333
1334           g_signal_emit (G_OBJECT (decode_bin),
1335               gst_decode_bin_signals[SIGNAL_REMOVED_DECODED_PAD], 0, ghostpad);
1336
1337           gst_element_remove_pad (GST_ELEMENT (decode_bin), ghostpad);
1338           gst_object_unref (ghostpad);
1339           continue;
1340         } else {
1341           GST_DEBUG_OBJECT (decode_bin, "not one of our ghostpads");
1342         }
1343
1344         peer = gst_pad_get_peer (pad);
1345         if (peer) {
1346           GstObject *parent = gst_pad_get_parent (peer);
1347
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));
1351
1352           if (parent) {
1353             GstObject *grandparent = gst_object_get_parent (parent);
1354
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));
1359               } else {
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);
1364               }
1365               gst_object_unref (grandparent);
1366             }
1367             gst_object_unref (parent);
1368           }
1369           gst_object_unref (peer);
1370         }
1371         gst_object_unref (item);
1372       }
1373         break;
1374       case GST_ITERATOR_RESYNC:
1375         gst_iterator_resync (iter);
1376         break;
1377       case GST_ITERATOR_ERROR:
1378         GST_ERROR_OBJECT (pad, "Could not iterate over internally linked pads");
1379         done = TRUE;
1380         break;
1381       case GST_ITERATOR_DONE:
1382         done = TRUE;
1383         break;
1384     }
1385   }
1386   GST_DEBUG_OBJECT (decode_bin, "removing %s", GST_ELEMENT_NAME (elem));
1387
1388   gst_iterator_free (iter);
1389
1390 no_iter:
1391   gst_element_set_state (elem, GST_STATE_NULL);
1392   gst_bin_remove (GST_BIN (decode_bin), elem);
1393 }
1394
1395 /* there are @bytes bytes in @queue, enlarge it
1396  *
1397  * Returns: new max number of bytes in @queue
1398  */
1399 static guint
1400 queue_enlarge (GstElement * queue, guint bytes, GstDecodeBin * decode_bin)
1401 {
1402   /* Increase the queue size by 1Mbyte if it is over 1Mb, else double its current limit
1403    */
1404   if (bytes > 1024 * 1024)
1405     bytes += 1024 * 1024;
1406   else
1407     bytes *= 2;
1408
1409   GST_DEBUG_OBJECT (decode_bin,
1410       "increasing queue %s max-size-bytes to %d", GST_ELEMENT_NAME (queue),
1411       bytes);
1412   g_object_set (G_OBJECT (queue), "max-size-bytes", bytes, NULL);
1413
1414   return bytes;
1415 }
1416
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
1421  * it larger.
1422  */
1423 static void
1424 queue_underrun_cb (GstElement * queue, GstDecodeBin * decode_bin)
1425 {
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
1428    * in that case.
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
1433    * demuxer thread.
1434    */
1435   GST_DEBUG_OBJECT (decode_bin, "got underrun");
1436 }
1437
1438 /* Make sure we don't have a full queue and empty queue situation */
1439 static void
1440 queue_filled_cb (GstElement * queue, GstDecodeBin * decode_bin)
1441 {
1442   GList *tmp;
1443   gboolean increase = FALSE;
1444   guint bytes;
1445
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);
1449
1450   /* we do not buffer more than 20Mb */
1451   if (bytes > (20 * 1024 * 1024))
1452     goto too_large;
1453
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;
1459
1460     if (aqueue != queue) {
1461       g_object_get (G_OBJECT (aqueue), "current-level-bytes", &levelbytes,
1462           NULL);
1463       if (levelbytes == 0) {
1464         /* yup, found an empty queue, we can stop the search and
1465          * need to enlarge the queue */
1466         increase = TRUE;
1467         break;
1468       }
1469     }
1470   }
1471
1472   if (increase) {
1473     /* enlarge @queue */
1474     queue_enlarge (queue, bytes, decode_bin);
1475   } else {
1476     GST_DEBUG_OBJECT (decode_bin,
1477         "Queue is full but other queues are not empty, not doing anything");
1478   }
1479   return;
1480
1481   /* errors */
1482 too_large:
1483   {
1484     GST_WARNING_OBJECT (decode_bin,
1485         "Queue is bigger than 20Mbytes, something else is going wrong");
1486     return;
1487   }
1488 }
1489
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. */
1492 static void
1493 new_pad (GstElement * element, GstPad * pad, GstDynamic * dynamic)
1494 {
1495   GstDecodeBin *decode_bin = dynamic->decode_bin;
1496   GstCaps *caps;
1497   gboolean more;
1498
1499   GST_OBJECT_LOCK (decode_bin);
1500   if (decode_bin->shutting_down)
1501     goto shutting_down1;
1502   GST_OBJECT_UNLOCK (decode_bin);
1503
1504   GST_STATE_LOCK (decode_bin);
1505   if (decode_bin->shutting_down)
1506     goto shutting_down2;
1507
1508   /* see if any more pending dynamic connections exist */
1509   more = gst_decode_bin_is_dynamic (decode_bin);
1510
1511   caps = gst_pad_get_caps (pad);
1512   close_pad_link (element, pad, caps, decode_bin, more);
1513   if (caps)
1514     gst_caps_unref (caps);
1515   GST_STATE_UNLOCK (decode_bin);
1516
1517   return;
1518
1519 shutting_down1:
1520   {
1521     GST_DEBUG_OBJECT (decode_bin, "we are shutting down");
1522     GST_OBJECT_UNLOCK (decode_bin);
1523     return;
1524   }
1525 shutting_down2:
1526   {
1527     GST_DEBUG_OBJECT (decode_bin, "we are shutting down");
1528     GST_STATE_UNLOCK (decode_bin);
1529     return;
1530   }
1531 }
1532
1533 static void
1534 dynamic_remove (GstDynamic * dynamic)
1535 {
1536   GstDecodeBin *decode_bin = dynamic->decode_bin;
1537
1538   /* remove the dynamic from the list of dynamics */
1539   decode_bin->dynamics = g_list_remove (decode_bin->dynamics, dynamic);
1540   dynamic_free (dynamic);
1541
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);
1549     }
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));
1553   } else {
1554     GST_DEBUG_OBJECT (decode_bin, "we have more dynamic elements");
1555   }
1556 }
1557
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. */
1563 static void
1564 no_more_pads (GstElement * element, GstDynamic * dynamic)
1565 {
1566   GST_DEBUG_OBJECT (dynamic->decode_bin, "no more pads on element %s",
1567       GST_ELEMENT_NAME (element));
1568
1569   dynamic_remove (dynamic);
1570 }
1571
1572 static void
1573 new_caps (GstPad * pad, GParamSpec * unused, GstDynamic * dynamic)
1574 {
1575   GST_DEBUG_OBJECT (dynamic->decode_bin, "delayed link triggered");
1576
1577   new_pad (dynamic->element, pad, dynamic);
1578
1579   /* assume it worked and remove the dynamic */
1580   dynamic_remove (dynamic);
1581
1582   return;
1583 }
1584
1585 static gboolean
1586 is_our_kid (GstElement * e, GstDecodeBin * decode_bin)
1587 {
1588   gboolean ret;
1589   GstElement *parent;
1590
1591   parent = (GstElement *) gst_object_get_parent ((GstObject *) e);
1592   ret = (parent == (GstElement *) decode_bin);
1593
1594   if (parent)
1595     gst_object_unref ((GstObject *) parent);
1596
1597   return ret;
1598 }
1599
1600 static gboolean
1601 elem_is_dynamic (GstElement * element, GstDecodeBin * decode_bin)
1602 {
1603   GList *pads;
1604
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;
1610
1611     /* we are only interested in source pads */
1612     if (GST_PAD_TEMPLATE_DIRECTION (templ) != GST_PAD_SRC)
1613       continue;
1614
1615     templ_name = GST_PAD_TEMPLATE_NAME_TEMPLATE (templ);
1616     GST_DEBUG_OBJECT (decode_bin, "got a source pad template %s", templ_name);
1617
1618     /* figure out what kind of pad this is */
1619     switch (GST_PAD_TEMPLATE_PRESENCE (templ)) {
1620       case GST_PAD_SOMETIMES:
1621       {
1622         /* try to get the pad to see if it is already created or
1623          * not */
1624         GstPad *pad = gst_element_get_static_pad (element, templ_name);
1625
1626         if (pad) {
1627           GST_DEBUG_OBJECT (decode_bin, "got the pad for sometimes template %s",
1628               templ_name);
1629           gst_object_unref (pad);
1630         } else {
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 */
1634           return TRUE;
1635         }
1636         break;
1637       }
1638       default:
1639         /* Don't care about ALWAYS or REQUEST pads */
1640         break;
1641     }
1642   }
1643   return FALSE;
1644 }
1645
1646 /* This function will be called when a pad is disconnected for some reason */
1647 static void
1648 unlinked (GstPad * pad, GstPad * peerpad, GstDecodeBin * decode_bin)
1649 {
1650   GstElement *element, *peer;
1651
1652   /* inactivate pad */
1653   gst_pad_set_active (pad, GST_ACTIVATE_NONE);
1654
1655   peer = gst_pad_get_parent_element (peerpad);
1656
1657   if (!is_our_kid (peer, decode_bin))
1658     goto exit;
1659
1660   GST_DEBUG_OBJECT (decode_bin, "pad %s:%s removal while alive - chained?",
1661       GST_DEBUG_PAD_NAME (pad));
1662
1663   /* remove all elements linked to the peerpad */
1664   remove_element_chain (decode_bin, peerpad);
1665
1666   /* Re-add as a dynamic element if needed, via close_link */
1667   element = gst_pad_get_parent_element (pad);
1668   if (element) {
1669     if (elem_is_dynamic (element, decode_bin))
1670       dynamic_add (element, NULL, decode_bin);
1671
1672     gst_object_unref (element);
1673   }
1674
1675 exit:
1676   gst_object_unref (peer);
1677 }
1678
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 */
1682 static void
1683 close_link (GstElement * element, GstDecodeBin * decode_bin)
1684 {
1685   GList *pads;
1686   gboolean dynamic = FALSE;
1687   GList *to_connect = NULL;
1688   gboolean more;
1689
1690   GST_DEBUG_OBJECT (decode_bin, "closing links with element %s",
1691       GST_ELEMENT_NAME (element));
1692
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;
1698
1699     /* we are only interested in source pads */
1700     if (GST_PAD_TEMPLATE_DIRECTION (templ) != GST_PAD_SRC)
1701       continue;
1702
1703     templ_name = GST_PAD_TEMPLATE_NAME_TEMPLATE (templ);
1704     GST_DEBUG_OBJECT (decode_bin, "got a source pad template %s", templ_name);
1705
1706     /* figure out what kind of pad this is */
1707     switch (GST_PAD_TEMPLATE_PRESENCE (templ)) {
1708       case GST_PAD_ALWAYS:
1709       {
1710         /* get the pad that we need to autoplug */
1711         GstPad *pad = gst_element_get_static_pad (element, templ_name);
1712
1713         if (pad) {
1714           GST_DEBUG_OBJECT (decode_bin, "got the pad for always template %s",
1715               templ_name);
1716           /* here is the pad, we need to autoplug it */
1717           to_connect = g_list_prepend (to_connect, pad);
1718         } else {
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);
1723         }
1724         break;
1725       }
1726       case GST_PAD_SOMETIMES:
1727       {
1728         /* try to get the pad to see if it is already created or
1729          * not */
1730         GstPad *pad = gst_element_get_static_pad (element, templ_name);
1731
1732         if (pad) {
1733           GST_DEBUG_OBJECT (decode_bin, "got the pad for sometimes template %s",
1734               templ_name);
1735           /* the pad is created, we need to autoplug it */
1736           to_connect = g_list_prepend (to_connect, pad);
1737         } else {
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 */
1741           dynamic = TRUE;
1742         }
1743         break;
1744       }
1745       case GST_PAD_REQUEST:
1746         /* ignore request pads */
1747         GST_DEBUG_OBJECT (decode_bin, "ignoring request padtemplate %s",
1748             templ_name);
1749         break;
1750     }
1751   }
1752   if (dynamic) {
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 */
1756
1757     dynamic_add (element, NULL, decode_bin);
1758   }
1759
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;
1764
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);
1768     GstCaps *caps;
1769
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);
1775
1776     GST_DEBUG_OBJECT (decode_bin, "closing pad link for %s",
1777         GST_OBJECT_NAME (pad));
1778
1779     /* continue autoplugging on the pads */
1780     caps = gst_pad_get_caps (pad);
1781     close_pad_link (element, pad, caps, decode_bin, more);
1782     if (caps)
1783       gst_caps_unref (caps);
1784
1785     gst_object_unref (pad);
1786   }
1787   g_list_free (to_connect);
1788 }
1789
1790 /* this is the signal handler for the typefind element have_type signal.
1791  * It tries to continue autoplugging on the typefind src pad */
1792 static void
1793 type_found (GstElement * typefind, guint probability, GstCaps * caps,
1794     GstDecodeBin * decode_bin)
1795 {
1796   gboolean dynamic;
1797   GstPad *pad;
1798
1799   GST_DEBUG_OBJECT (decode_bin, "typefind found caps %" GST_PTR_FORMAT, caps);
1800
1801   GST_OBJECT_LOCK (decode_bin);
1802   if (decode_bin->shutting_down)
1803     goto shutting_down;
1804   GST_OBJECT_UNLOCK (decode_bin);
1805
1806   GST_STATE_LOCK (decode_bin);
1807   if (decode_bin->shutting_down)
1808     goto exit;
1809
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)
1813     goto exit;
1814
1815   decode_bin->have_type = TRUE;
1816
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"));
1827     goto exit;
1828   }
1829
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);
1834
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));
1841   } else {
1842     /* more dynamic elements exist that could create new pads */
1843     GST_DEBUG_OBJECT (decode_bin, "we have more dynamic elements");
1844   }
1845
1846 exit:
1847   GST_STATE_UNLOCK (decode_bin);
1848   return;
1849
1850 shutting_down:
1851   {
1852     GST_DEBUG_OBJECT (decode_bin, "we are shutting down");
1853     GST_OBJECT_UNLOCK (decode_bin);
1854     return;
1855   }
1856 }
1857
1858 static void
1859 disconnect_unlinked_signals (GstDecodeBin * decode_bin, GstElement * element)
1860 {
1861   GstIterator *pad_it = NULL;
1862   gboolean done = FALSE;
1863
1864   pad_it = gst_element_iterate_src_pads (element);
1865   while (!done) {
1866     GstPad *pad = NULL;
1867
1868     switch (gst_iterator_next (pad_it, (gpointer) & pad)) {
1869       case GST_ITERATOR_OK:
1870         g_signal_handlers_disconnect_by_func (pad, (gpointer) unlinked,
1871             decode_bin);
1872         gst_object_unref (pad);
1873         break;
1874       case GST_ITERATOR_RESYNC:
1875         gst_iterator_resync (pad_it);
1876         break;
1877       default:
1878         done = TRUE;
1879         break;
1880     }
1881   }
1882   gst_iterator_free (pad_it);
1883 }
1884
1885
1886 static void
1887 cleanup_decodebin (GstDecodeBin * decode_bin)
1888 {
1889   GstIterator *elem_it = NULL, *gpad_it = NULL;
1890   GstPad *typefind_pad = NULL;
1891   gboolean done = FALSE;
1892
1893   g_return_if_fail (GST_IS_DECODE_BIN (decode_bin));
1894
1895   GST_DEBUG_OBJECT (decode_bin, "cleaning up decodebin");
1896
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,
1900         decode_bin);
1901   }
1902
1903   elem_it = gst_bin_iterate_elements (GST_BIN (decode_bin));
1904   while (!done) {
1905     GstElement *element = NULL;
1906
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);
1914         }
1915         gst_object_unref (element);
1916         break;
1917       case GST_ITERATOR_RESYNC:
1918         gst_iterator_resync (elem_it);
1919         break;
1920       case GST_ITERATOR_ERROR:
1921         done = TRUE;
1922         break;
1923       case GST_ITERATOR_DONE:
1924         done = TRUE;
1925         break;
1926     }
1927   }
1928   gst_iterator_free (elem_it);
1929
1930   done = FALSE;
1931   gpad_it = gst_element_iterate_pads (GST_ELEMENT (decode_bin));
1932   while (!done) {
1933     GstPad *pad = NULL;
1934
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);
1942         }
1943         gst_object_unref (pad);
1944         break;
1945       case GST_ITERATOR_RESYNC:
1946         gst_iterator_resync (gpad_it);
1947         break;
1948       case GST_ITERATOR_ERROR:
1949         done = TRUE;
1950         break;
1951       case GST_ITERATOR_DONE:
1952         done = TRUE;
1953         break;
1954     }
1955   }
1956   gst_iterator_free (gpad_it);
1957
1958   if (GST_IS_PAD (typefind_pad)) {
1959     g_signal_handlers_unblock_by_func (typefind_pad, (gpointer) unlinked,
1960         decode_bin);
1961     g_signal_handlers_disconnect_by_func (typefind_pad, (gpointer) unlinked,
1962         decode_bin);
1963     gst_object_unref (typefind_pad);
1964   }
1965 }
1966
1967 static GstStateChangeReturn
1968 gst_decode_bin_change_state (GstElement * element, GstStateChange transition)
1969 {
1970   GstStateChangeReturn ret;
1971   GstDecodeBin *decode_bin;
1972
1973   decode_bin = GST_DECODE_BIN (element);
1974
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;
1982       break;
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);
1988
1989       if (!add_fakesink (decode_bin))
1990         goto missing_fakesink;
1991       break;
1992     case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
1993     case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
1994       break;
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);
1999       break;
2000     default:
2001       break;
2002   }
2003
2004   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
2005   if (ret == GST_STATE_CHANGE_FAILURE)
2006     return ret;
2007
2008   switch (transition) {
2009     case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
2010       break;
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);
2016       break;
2017     default:
2018       break;
2019   }
2020
2021   return ret;
2022 /* ERRORS */
2023 missing_typefind:
2024   {
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;
2029   }
2030 missing_fakesink:
2031   {
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;
2036   }
2037 }
2038
2039 static gboolean
2040 plugin_init (GstPlugin * plugin)
2041 {
2042   GST_DEBUG_CATEGORY_INIT (gst_decode_bin_debug, "decodebin", 0, "decoder bin");
2043
2044 #ifdef ENABLE_NLS
2045   GST_DEBUG ("binding text domain %s to locale dir %s", GETTEXT_PACKAGE,
2046       LOCALEDIR);
2047   bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
2048   bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
2049 #endif /* ENABLE_NLS */
2050
2051   return gst_element_register (plugin, "decodebin", GST_RANK_NONE,
2052       GST_TYPE_DECODE_BIN);
2053 }
2054
2055 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
2056     GST_VERSION_MINOR,
2057     "decodebin",
2058     "decoder bin", plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME,
2059     GST_PACKAGE_ORIGIN)