expand tabs
[platform/upstream/gstreamer.git] / gst / playback / gstdecodebin.c
1 /* GStreamer
2  * Copyright (C) <2004> Wim Taymans <wim@fluendo.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 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23
24 #include <string.h>
25 #include <gst/gst.h>
26
27 #include "gstplay-marshal.h"
28
29 /* generic templates */
30 static GstStaticPadTemplate decoder_bin_sink_template =
31 GST_STATIC_PAD_TEMPLATE ("sink",
32     GST_PAD_SINK,
33     GST_PAD_ALWAYS,
34     GST_STATIC_CAPS_ANY);
35
36 static GstStaticPadTemplate decoder_bin_src_template =
37 GST_STATIC_PAD_TEMPLATE ("src%d",
38     GST_PAD_SRC,
39     GST_PAD_SOMETIMES,
40     GST_STATIC_CAPS_ANY);
41
42 GST_DEBUG_CATEGORY_STATIC (gst_decode_bin_debug);
43 #define GST_CAT_DEFAULT gst_decode_bin_debug
44
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))
50
51 typedef struct _GstDecodeBin GstDecodeBin;
52 typedef struct _GstDecodeBinClass GstDecodeBinClass;
53
54 struct _GstDecodeBin
55 {
56   GstBin bin;                   /* we extend GstBin */
57
58   GstElement *typefind;         /* this holds the typefind object */
59   GstElement *fakesink;
60
61   GList *dynamics;              /* list of dynamic connections */
62
63   GList *queues;                /* list of demuxer-decoder queues */
64
65   GList *factories;             /* factories we can use for selecting elements */
66   gint numpads;
67   gint numwaiting;
68
69   guint have_type_id;           /* signal id for the typefind element */
70
71   gboolean shutting_down;       /* stop pluggin if we're shutting down */
72 };
73
74 struct _GstDecodeBinClass
75 {
76   GstBinClass parent_class;
77
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);
84 };
85
86 /* signals */
87 enum
88 {
89   SIGNAL_NEW_DECODED_PAD,
90   SIGNAL_REMOVED_DECODED_PAD,
91   SIGNAL_UNKNOWN_TYPE,
92   SIGNAL_REDIRECT,
93   LAST_SIGNAL
94 };
95
96 /* this structure is created for all dynamic pads that could get created
97  * at runtime */
98 typedef struct
99 {
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 */
105 }
106 GstDynamic;
107
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);
111
112 static GstStateChangeReturn gst_decode_bin_change_state (GstElement * element,
113     GstStateChange transition);
114
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);
127
128 static void queue_filled_cb (GstElement * queue, GstDecodeBin * decode_bin);
129
130 static GstElementClass *parent_class;
131 static guint gst_decode_bin_signals[LAST_SIGNAL] = { 0 };
132
133 static GstElementDetails gst_decode_bin_details = {
134   "Decoder Bin",
135   "Generic/Bin/Decoder",
136   "Autoplug and decode to raw media",
137   "Wim Taymans <wim@fluendo.com>"
138 };
139
140
141 static GType
142 gst_decode_bin_get_type (void)
143 {
144   static GType gst_decode_bin_type = 0;
145
146   if (!gst_decode_bin_type) {
147     static const GTypeInfo gst_decode_bin_info = {
148       sizeof (GstDecodeBinClass),
149       NULL,
150       NULL,
151       (GClassInitFunc) gst_decode_bin_class_init,
152       NULL,
153       NULL,
154       sizeof (GstDecodeBin),
155       0,
156       (GInstanceInitFunc) gst_decode_bin_init,
157       NULL
158     };
159
160     gst_decode_bin_type =
161         g_type_register_static (GST_TYPE_BIN, "GstDecodeBin",
162         &gst_decode_bin_info, 0);
163   }
164
165   return gst_decode_bin_type;
166 }
167
168 static void
169 gst_decode_bin_class_init (GstDecodeBinClass * klass)
170 {
171   GObjectClass *gobject_klass;
172   GstElementClass *gstelement_klass;
173   GstBinClass *gstbin_klass;
174
175   gobject_klass = (GObjectClass *) klass;
176   gstelement_klass = (GstElementClass *) klass;
177   gstbin_klass = (GstBinClass *) klass;
178
179   parent_class = g_type_class_ref (gst_bin_get_type ());
180
181   gst_decode_bin_signals[SIGNAL_NEW_DECODED_PAD] =
182       g_signal_new ("new-decoded-pad", G_TYPE_FROM_CLASS (klass),
183       G_SIGNAL_RUN_LAST,
184       G_STRUCT_OFFSET (GstDecodeBinClass, new_decoded_pad), NULL, NULL,
185       gst_play_marshal_VOID__OBJECT_BOOLEAN, G_TYPE_NONE, 2, GST_TYPE_PAD,
186       G_TYPE_BOOLEAN);
187   gst_decode_bin_signals[SIGNAL_REMOVED_DECODED_PAD] =
188       g_signal_new ("removed-decoded-pad", G_TYPE_FROM_CLASS (klass),
189       G_SIGNAL_RUN_LAST,
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);
197
198   gobject_klass->dispose = GST_DEBUG_FUNCPTR (gst_decode_bin_dispose);
199
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));
204
205   gst_element_class_set_details (gstelement_klass, &gst_decode_bin_details);
206
207   gstelement_klass->change_state =
208       GST_DEBUG_FUNCPTR (gst_decode_bin_change_state);
209 }
210
211 /* check if the bin is dynamic.
212  *
213  * If there are no outstanding dynamic connections, the bin is 
214  * considered to be non-dynamic.
215  */
216 static gboolean
217 gst_decode_bin_is_dynamic (GstDecodeBin * decode_bin)
218 {
219   return decode_bin->dynamics != NULL;
220 }
221
222 /* the filter function for selecting the elements we can use in
223  * autoplugging */
224 static gboolean
225 gst_decode_bin_factory_filter (GstPluginFeature * feature,
226     GstDecodeBin * decode_bin)
227 {
228   guint rank;
229   const gchar *klass;
230
231   /* we only care about element factories */
232   if (!GST_IS_ELEMENT_FACTORY (feature))
233     return FALSE;
234
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) {
239     return FALSE;
240   }
241
242   /* only select elements with autoplugging rank */
243   rank = gst_plugin_feature_get_rank (feature);
244   if (rank < GST_RANK_MARGINAL)
245     return FALSE;
246
247   return TRUE;
248 }
249
250 /* function used to sort element features */
251 static gint
252 compare_ranks (GstPluginFeature * f1, GstPluginFeature * f2)
253 {
254   gint diff;
255   const gchar *rname1, *rname2;
256
257   diff = gst_plugin_feature_get_rank (f2) - gst_plugin_feature_get_rank (f1);
258   if (diff != 0)
259     return diff;
260
261   rname1 = gst_plugin_feature_get_name (f1);
262   rname2 = gst_plugin_feature_get_name (f2);
263
264   diff = strcmp (rname2, rname1);
265
266   return diff;
267 }
268
269 static void
270 print_feature (GstPluginFeature * feature)
271 {
272   const gchar *rname;
273
274   rname = gst_plugin_feature_get_name (feature);
275
276   GST_DEBUG ("%s", rname);
277 }
278
279 static void
280 gst_decode_bin_init (GstDecodeBin * decode_bin)
281 {
282   GList *factories;
283
284   /* first filter out the interesting element factories */
285   factories = gst_default_registry_feature_filter (
286       (GstPluginFeatureFilter) gst_decode_bin_factory_filter,
287       FALSE, decode_bin);
288
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);
293
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");
298   } else {
299     GstPad *pad;
300
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;
306     }
307
308     /* get the sinkpad */
309     pad = gst_element_get_pad (decode_bin->typefind, "sink");
310
311     /* ghost the sink pad to ourself */
312     gst_element_add_pad (GST_ELEMENT (decode_bin),
313         gst_ghost_pad_new ("sink", pad));
314
315     gst_object_unref (pad);
316
317     /* connect a signal to find out when the typefind element found
318      * a type */
319     decode_bin->have_type_id =
320         g_signal_connect (G_OBJECT (decode_bin->typefind), "have_type",
321         G_CALLBACK (type_found), decode_bin);
322   }
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");
326   } else {
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;
332     }
333   }
334
335   decode_bin->dynamics = NULL;
336   decode_bin->queues = NULL;
337 }
338
339 static void dynamic_free (GstDynamic * dyn);
340
341 static void
342 gst_decode_bin_dispose (GObject * object)
343 {
344   GstDecodeBin *decode_bin;
345
346   decode_bin = GST_DECODE_BIN (object);
347
348   if (decode_bin->factories)
349     gst_plugin_feature_list_free (decode_bin->factories);
350   decode_bin->factories = NULL;
351
352   G_OBJECT_CLASS (parent_class)->dispose (object);
353
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);
358 }
359
360 static GstDynamic *
361 dynamic_create (GstElement * element, GstDecodeBin * decode_bin)
362 {
363   GstDynamic *dyn;
364
365   GST_DEBUG_OBJECT (element, "dynamic create");
366
367   /* take refs */
368   gst_object_ref (element);
369   gst_object_ref (decode_bin);
370
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);
378
379   return dyn;
380 }
381
382 static void
383 dynamic_free (GstDynamic * dyn)
384 {
385   GST_DEBUG_OBJECT (dyn->decode_bin, "dynamic free");
386
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);
390
391   gst_object_unref (dyn->element);
392   gst_object_unref (dyn->decode_bin);
393   dyn->element = NULL;
394   dyn->decode_bin = NULL;
395   g_free (dyn);
396 }
397
398 static void
399 free_dynamics (GstDecodeBin * decode_bin)
400 {
401   GList *dyns;
402
403   for (dyns = decode_bin->dynamics; dyns; dyns = g_list_next (dyns)) {
404     GstDynamic *dynamic = (GstDynamic *) dyns->data;
405
406     dynamic_free (dynamic);
407   }
408   g_list_free (decode_bin->dynamics);
409   decode_bin->dynamics = NULL;
410 }
411
412 /* this function runs through the element factories and returns a list
413  * of all elements that are able to sink the given caps 
414  */
415 static GList *
416 find_compatibles (GstDecodeBin * decode_bin, const GstCaps * caps)
417 {
418   GList *factories;
419   GList *to_try = NULL;
420
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;
426     GList *walk;
427
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;
432
433       /* we only care about the sink templates */
434       if (templ->direction == GST_PAD_SINK) {
435         GstCaps *intersect;
436
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);
445           break;
446         }
447         gst_caps_unref (intersect);
448       }
449     }
450   }
451   to_try = g_list_reverse (to_try);
452
453   return to_try;
454 }
455
456 static gboolean
457 mimetype_is_raw (const gchar * mimetype)
458 {
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");
462 }
463
464 static void
465 remove_fakesink (GstDecodeBin * decode_bin)
466 {
467   if (decode_bin->fakesink) {
468     gst_object_ref (decode_bin->fakesink);
469     gst_bin_remove (GST_BIN (decode_bin), decode_bin->fakesink);
470
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);
474
475     gst_object_unref (decode_bin->fakesink);
476     decode_bin->fakesink = NULL;
477
478     gst_element_post_message (GST_ELEMENT_CAST (decode_bin),
479         gst_message_new_state_dirty (GST_OBJECT_CAST (decode_bin)));
480   }
481 }
482
483 static void
484 pad_blocked (GstPad * pad, gboolean blocked, GstDecodeBin * decode_bin)
485 {
486   if (blocked) {
487     decode_bin->numwaiting--;
488     if (decode_bin->numwaiting == 0) {
489       remove_fakesink (decode_bin);
490     }
491     gst_pad_set_blocked_async (pad, FALSE, (GstPadBlockCallback) pad_blocked,
492         NULL);
493   }
494 }
495
496 /* given a pad and a caps from an element, find the list of elements
497  * that could connect to the pad
498  *
499  * If the pad has a raw format, this function will create a ghostpad
500  * for the pad onto the decodebin.
501  *
502  * If no compatible elements could be found, this function will signal 
503  * the unknown_type signal.
504  */
505 static void
506 close_pad_link (GstElement * element, GstPad * pad, GstCaps * caps,
507     GstDecodeBin * decode_bin, gboolean more)
508 {
509   GstStructure *structure;
510   const gchar *mimetype;
511   gchar *padname;
512   gint diff;
513
514   padname = gst_pad_get_name (pad);
515   diff = strncmp (padname, "current_", 8);
516   g_free (padname);
517
518   /* hack.. ignore current pads */
519   if (!diff)
520     return;
521
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))
525     goto unknown_type;
526
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))
530     goto dont_know_yet;
531
532   GST_LOG_OBJECT (element, "trying to close %" GST_PTR_FORMAT, caps);
533
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);
539
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)) {
543     gchar *padname;
544     GstPad *ghost;
545
546     /* make a unique name for this new pad */
547     padname = g_strdup_printf ("src%d", decode_bin->numpads);
548     decode_bin->numpads++;
549
550     /* make it a ghostpad */
551     ghost = gst_ghost_pad_new (padname, pad);
552     gst_element_add_pad (GST_ELEMENT (decode_bin), ghost);
553
554     if (gst_pad_set_blocked_async (pad, TRUE, (GstPadBlockCallback) pad_blocked,
555             decode_bin)) {
556       decode_bin->numwaiting++;
557     }
558
559     GST_LOG_OBJECT (element, "closed pad %s", padname);
560
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");
566
567     g_free (padname);
568   } else {
569     GList *to_try;
570
571     /* if the caps has many types, we need to delay */
572     if (gst_caps_get_size (caps) != 1)
573       goto many_types;
574
575     /* continue plugging, first find all compatible elements */
576     to_try = find_compatibles (decode_bin, caps);
577     if (to_try == NULL)
578       /* no compatible elements, we cannot go on */
579       goto unknown_type;
580
581     try_to_link_1 (decode_bin, element, pad, to_try);
582     /* can free the list again now */
583     g_list_free (to_try);
584   }
585   return;
586
587 unknown_type:
588   {
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);
592     return;
593   }
594 dont_know_yet:
595   {
596     GST_LOG_OBJECT (pad, "type is not known yet, waiting to close link");
597     return;
598   }
599 many_types:
600   {
601     GST_LOG_OBJECT (pad, "many possible types, waiting to close link");
602     return;
603   }
604 }
605
606 /*
607  * given a list of element factories, try to link one of the factories
608  * to the given pad.
609  *
610  * The function returns the element that was successfully linked to the
611  * pad.
612  */
613 static GstElement *
614 try_to_link_1 (GstDecodeBin * decode_bin, GstElement * srcelement, GstPad * pad,
615     GList * factories)
616 {
617   GList *walk;
618   GstElementFactory *srcfactory = NULL;
619   GstElement *result = NULL;
620   gboolean isdemux = FALSE;
621   const gchar *klass;
622
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"));
627
628   /* loop over the factories */
629   for (walk = factories; walk; walk = g_list_next (walk)) {
630     GstElementFactory *factory = GST_ELEMENT_FACTORY (walk->data);
631     GstElement *element;
632     GstElement *queue = NULL;
633     GstPadLinkReturn ret;
634     GstPad *sinkpad;
635     GstPad *usedsrcpad = pad;
636     GstPad *queuesinkpad = NULL, *queuesrcpad = NULL;
637
638     GST_DEBUG_OBJECT (decode_bin, "trying to link %s",
639         gst_plugin_feature_get_name (GST_PLUGIN_FEATURE (factory)));
640
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)));
646       continue;
647     }
648
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");
655       continue;
656     }
657
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);
661
662     /* set to ready first so it is ready */
663     gst_element_set_state (element, GST_STATE_READY);
664
665     if (isdemux) {
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));
670
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");
679
680       gst_pad_link (pad, queuesinkpad);
681     }
682
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. */
690       if (isdemux)
691         gst_element_set_state (queue, GST_STATE_NULL);
692       gst_element_set_state (element, GST_STATE_NULL);
693       if (isdemux) {
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);
698       }
699       gst_bin_remove (GST_BIN (decode_bin), element);
700
701     } else {
702       guint sig;
703
704       GST_DEBUG_OBJECT (decode_bin, "linked on pad %s:%s",
705           GST_DEBUG_PAD_NAME (usedsrcpad));
706
707       if (isdemux) {
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);
711       }
712
713       /* The link worked, now figure out what it was that we connected */
714
715       /* make sure we catch unlink signals */
716       sig = g_signal_connect (G_OBJECT (usedsrcpad), "unlinked",
717           G_CALLBACK (unlinked), decode_bin);
718
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));
721
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);
727
728       result = element;
729
730       /* get rid of the sinkpad now */
731       gst_object_unref (sinkpad);
732       if (isdemux) {
733         gst_element_set_state (queue, GST_STATE_PAUSED);
734         gst_object_unref (queuesrcpad);
735         gst_object_unref (queuesinkpad);
736       }
737
738       /* and exit */
739       goto done;
740     }
741   }
742 done:
743   return result;
744 }
745
746 static GstPad *
747 get_our_ghost_pad (GstDecodeBin * decode_bin, GstPad * pad)
748 {
749 #if 0
750   GList *ghostpads;
751
752   if (pad == NULL || !GST_PAD_IS_SRC (pad)) {
753     GST_DEBUG_OBJECT (decode_bin, "pad NULL or not SRC pad");
754     return NULL;
755   }
756
757   if (GST_IS_GHOST_PAD (pad)) {
758     GstElement *parent = gst_pad_get_parent (pad);
759
760     GST_DEBUG_OBJECT (decode_bin, "pad parent %s", GST_ELEMENT_NAME (parent));
761
762     if (parent == GST_ELEMENT (decode_bin)) {
763       GST_DEBUG_OBJECT (decode_bin, "pad is our ghostpad");
764       gst_object_unref (parent);
765       return pad;
766     } else {
767       GST_DEBUG_OBJECT (decode_bin, "pad is ghostpad but not ours");
768       gst_object_unref (parent);
769       return NULL;
770     }
771   }
772
773   GST_DEBUG_OBJECT (decode_bin, "looping over ghostpads");
774   ghostpads = GST_REAL_PAD (pad)->ghostpads;
775   while (ghostpads) {
776     GstPad *ghostpad;
777
778     ghostpad = get_our_ghost_pad (decode_bin, GST_PAD (ghostpads->data));
779     if (ghostpad)
780       return ghostpad;
781
782     ghostpads = g_list_next (ghostpads);
783   }
784   GST_DEBUG_OBJECT (decode_bin, "done looping over ghostpads, nothing found");
785 #endif
786
787   return NULL;
788 }
789
790 /* remove all downstream elements starting from the given pad.
791  * Also make sure to remove the ghostpad we created for the raw 
792  * decoded stream.
793  */
794 static void
795 remove_element_chain (GstDecodeBin * decode_bin, GstPad * pad)
796 {
797   GList *int_links, *walk;
798   GstElement *elem = GST_ELEMENT (GST_OBJECT_PARENT (pad));
799
800   while (GST_OBJECT_PARENT (elem) &&
801       GST_OBJECT_PARENT (elem) != GST_OBJECT (decode_bin))
802     elem = GST_ELEMENT (GST_OBJECT_PARENT (elem));
803
804   GST_DEBUG_OBJECT (decode_bin, "%s:%s", GST_DEBUG_PAD_NAME (pad));
805   int_links = gst_pad_get_internal_links (pad);
806
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)) {
810     GstPad *pad;
811     GstPad *ghostpad;
812     GstPad *peer;
813
814     pad = GST_PAD (walk->data);
815     GST_DEBUG_OBJECT (decode_bin, "inspecting internal pad %s:%s",
816         GST_DEBUG_PAD_NAME (pad));
817
818     ghostpad = get_our_ghost_pad (decode_bin, pad);
819     if (ghostpad) {
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));
822
823       g_signal_emit (G_OBJECT (decode_bin),
824           gst_decode_bin_signals[SIGNAL_REMOVED_DECODED_PAD], 0, ghostpad);
825
826       gst_element_remove_pad (GST_ELEMENT (decode_bin), ghostpad);
827       continue;
828     } else {
829       GST_DEBUG_OBJECT (decode_bin, "not one of our ghostpads");
830     }
831
832     peer = gst_pad_get_peer (pad);
833     if (peer == NULL)
834       continue;
835
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));
838
839     {
840       GstElement *parent = gst_pad_get_parent_element (peer);
841
842       if (parent) {
843         if (parent != GST_ELEMENT (decode_bin)) {
844           GST_DEBUG_OBJECT (decode_bin, "dead end pad %s:%s",
845               GST_DEBUG_PAD_NAME (peer));
846         } else {
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);
850         }
851         gst_object_unref (parent);
852       }
853     }
854     gst_object_unref (peer);
855   }
856   GST_DEBUG_OBJECT (decode_bin, "removing %s", GST_ELEMENT_NAME (elem));
857
858   g_list_free (int_links);
859
860   gst_element_set_state (elem, GST_STATE_NULL);
861
862   gst_bin_remove (GST_BIN (decode_bin), elem);
863 }
864
865 /* Make sure we don't have a full queue and empty queue situation */
866 static void
867 queue_filled_cb (GstElement * queue, GstDecodeBin * decode_bin)
868 {
869   GList *tmp;
870   gboolean increase = FALSE;
871   guint bytes;
872
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);
875
876   if (bytes > (20 * 1024 * 1024)) {
877     GST_WARNING_OBJECT (decode_bin,
878         "Queue is bigger than 20Mbytes, something else is going wrong");
879     return;
880   }
881
882   for (tmp = decode_bin->queues; tmp; tmp = g_list_next (tmp)) {
883     GstElement *aqueue = GST_ELEMENT (tmp->data);
884     guint levelbytes = -1;
885
886     if (aqueue != queue) {
887       g_object_get (G_OBJECT (aqueue),
888           "current-level-bytes", &levelbytes, NULL);
889       if (levelbytes == 0) {
890         increase = TRUE;
891       }
892     }
893   }
894
895   if (increase) {
896     /* 
897      * Increase the queue size by 1Mbyte if it is over 1Mb, else double its current limit
898      */
899     if (bytes > 1024 * 1024)
900       bytes += 1024 * 1024;
901     else
902       bytes *= 2;
903     GST_DEBUG_OBJECT (decode_bin,
904         "One of the other queues is empty, increasing queue byte limit to %d",
905         bytes);
906     g_object_set (G_OBJECT (queue), "max-size-bytes", bytes, NULL);
907   } else
908     GST_DEBUG_OBJECT (decode_bin,
909         "Queue is full but other queues are not empty, not doing anything");
910 }
911
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. */
914 static void
915 new_pad (GstElement * element, GstPad * pad, GstDynamic * dynamic)
916 {
917   GstDecodeBin *decode_bin = dynamic->decode_bin;
918   GstCaps *caps;
919
920   GST_OBJECT_LOCK (decode_bin);
921   if (decode_bin->shutting_down)
922     goto shutting_down1;
923   GST_OBJECT_UNLOCK (decode_bin);
924
925   GST_STATE_LOCK (decode_bin);
926   if (decode_bin->shutting_down)
927     goto shutting_down2;
928
929   /* see if any more pending dynamic connections exist */
930   gboolean more = gst_decode_bin_is_dynamic (decode_bin);
931
932   caps = gst_pad_get_caps (pad);
933   close_pad_link (element, pad, caps, decode_bin, more);
934   if (caps)
935     gst_caps_unref (caps);
936   GST_STATE_UNLOCK (decode_bin);
937
938   return;
939
940 shutting_down1:
941   GST_OBJECT_UNLOCK (decode_bin);
942   return;
943
944 shutting_down2:
945   GST_STATE_UNLOCK (decode_bin);
946   return;
947 }
948
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. */
954 static void
955 no_more_pads (GstElement * element, GstDynamic * dynamic)
956 {
957   GstDecodeBin *decode_bin = dynamic->decode_bin;
958
959   GST_DEBUG_OBJECT (decode_bin, "no more pads on element %s",
960       GST_ELEMENT_NAME (element));
961
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);
965
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);
973     }
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));
977   } else {
978     GST_DEBUG_OBJECT (decode_bin, "we have more dynamic elements");
979   }
980 }
981
982 static gboolean
983 is_our_kid (GstElement * e, GstDecodeBin * decode_bin)
984 {
985   gboolean ret;
986   GstElement *parent;
987
988   parent = (GstElement *) gst_object_get_parent ((GstObject *) e);
989   ret = (parent == (GstElement *) decode_bin);
990
991   if (parent)
992     gst_object_unref ((GstObject *) parent);
993
994   return ret;
995 }
996
997 /* This function will be called when a pad is disconnected for some reason */
998 static void
999 unlinked (GstPad * pad, GstPad * peerpad, GstDecodeBin * decode_bin)
1000 {
1001   GstDynamic *dyn;
1002   GstElement *element, *peer;
1003
1004   /* inactivate pad */
1005   gst_pad_set_active (pad, GST_ACTIVATE_NONE);
1006
1007   element = gst_pad_get_parent_element (pad);
1008   peer = gst_pad_get_parent_element (peerpad);
1009
1010   if (!is_our_kid (peer, decode_bin))
1011     goto exit;
1012
1013   /* remove all elements linked to the peerpad */
1014   remove_element_chain (decode_bin, peerpad);
1015
1016   /* if an element removes two pads, then we don't want this twice */
1017   if (g_list_find (decode_bin->dynamics, element) != NULL)
1018     goto exit;
1019
1020   GST_DEBUG_OBJECT (decode_bin, "pad removal while alive - chained?");
1021
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);
1025
1026 exit:
1027   gst_object_unref (element);
1028   gst_object_unref (peer);
1029 }
1030
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 */
1034 static void
1035 close_link (GstElement * element, GstDecodeBin * decode_bin)
1036 {
1037   GList *pads;
1038   gboolean dynamic = FALSE;
1039   GList *to_connect = NULL;
1040   gboolean more;
1041
1042   GST_DEBUG_OBJECT (decode_bin, "closing links with element %s",
1043       GST_ELEMENT_NAME (element));
1044
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;
1050
1051     /* we are only interested in source pads */
1052     if (GST_PAD_TEMPLATE_DIRECTION (templ) != GST_PAD_SRC)
1053       continue;
1054
1055     templ_name = GST_PAD_TEMPLATE_NAME_TEMPLATE (templ);
1056     GST_DEBUG_OBJECT (decode_bin, "got a source pad template %s", templ_name);
1057
1058     /* figure out what kind of pad this is */
1059     switch (GST_PAD_TEMPLATE_PRESENCE (templ)) {
1060       case GST_PAD_ALWAYS:
1061       {
1062         /* get the pad that we need to autoplug */
1063         GstPad *pad = gst_element_get_pad (element, templ_name);
1064
1065         if (pad) {
1066           GST_DEBUG_OBJECT (decode_bin, "got the pad for always template %s",
1067               templ_name);
1068           /* here is the pad, we need to autoplug it */
1069           to_connect = g_list_prepend (to_connect, pad);
1070         } else {
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);
1075         }
1076         break;
1077       }
1078       case GST_PAD_SOMETIMES:
1079       {
1080         /* try to get the pad to see if it is already created or
1081          * not */
1082         GstPad *pad = gst_element_get_pad (element, templ_name);
1083
1084         if (pad) {
1085           GST_DEBUG_OBJECT (decode_bin, "got the pad for sometimes template %s",
1086               templ_name);
1087           /* the pad is created, we need to autoplug it */
1088           to_connect = g_list_prepend (to_connect, pad);
1089         } else {
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 */
1093           dynamic = TRUE;
1094         }
1095         break;
1096       }
1097       case GST_PAD_REQUEST:
1098         /* ignore request pads */
1099         GST_DEBUG_OBJECT (decode_bin, "ignoring request padtemplate %s",
1100             templ_name);
1101         break;
1102     }
1103   }
1104   if (dynamic) {
1105     GstDynamic *dyn;
1106
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 */
1110
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);
1114   }
1115
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;
1120
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);
1124     GstCaps *caps;
1125
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);
1131
1132     GST_DEBUG_OBJECT (decode_bin, "closing pad link for %s",
1133         GST_OBJECT_NAME (pad));
1134
1135     /* continue autoplugging on the pads */
1136     caps = gst_pad_get_caps (pad);
1137     close_pad_link (element, pad, caps, decode_bin, more);
1138     if (caps)
1139       gst_caps_unref (caps);
1140
1141     gst_object_unref (pad);
1142   }
1143   g_list_free (to_connect);
1144 }
1145
1146 /* this is the signal handler for the typefind element have_type signal.
1147  * It tries to continue autoplugging on the typefind src pad */
1148 static void
1149 type_found (GstElement * typefind, guint probability, GstCaps * caps,
1150     GstDecodeBin * decode_bin)
1151 {
1152   gboolean dynamic;
1153   GstPad *pad;
1154
1155   GST_STATE_LOCK (decode_bin);
1156   if (decode_bin->shutting_down)
1157     goto shutting_down;
1158
1159   GST_DEBUG_OBJECT (decode_bin, "typefind found caps %" GST_PTR_FORMAT, caps);
1160
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);
1165
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));
1172   } else {
1173     /* more dynamic elements exist that could create new pads */
1174     GST_DEBUG_OBJECT (decode_bin, "we have more dynamic elements");
1175   }
1176
1177 shutting_down:
1178   GST_STATE_UNLOCK (decode_bin);
1179   return;
1180 }
1181
1182 static GstStateChangeReturn
1183 gst_decode_bin_change_state (GstElement * element, GstStateChange transition)
1184 {
1185   GstStateChangeReturn ret;
1186   GstDecodeBin *decode_bin;
1187
1188   decode_bin = GST_DECODE_BIN (element);
1189
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;
1195       break;
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);
1200       break;
1201     case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
1202     case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
1203       break;
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);
1208       break;
1209     default:
1210       break;
1211   }
1212
1213   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1214
1215   switch (transition) {
1216     case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
1217     case GST_STATE_CHANGE_PAUSED_TO_READY:
1218       break;
1219     case GST_STATE_CHANGE_READY_TO_NULL:
1220       free_dynamics (decode_bin);
1221       break;
1222     default:
1223       break;
1224   }
1225
1226   return ret;
1227 }
1228
1229 static gboolean
1230 plugin_init (GstPlugin * plugin)
1231 {
1232   GST_DEBUG_CATEGORY_INIT (gst_decode_bin_debug, "decodebin", 0, "decoder bin");
1233
1234   return gst_element_register (plugin, "decodebin", GST_RANK_NONE,
1235       GST_TYPE_DECODE_BIN);
1236 }
1237
1238 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
1239     GST_VERSION_MINOR,
1240     "decodebin",
1241     "decoder bin", plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME,
1242     GST_PACKAGE_ORIGIN)