decodebin2: Ignore no-more-pads from non-demuxer elements
[platform/upstream/gstreamer.git] / gst / playback / gstdecodebin2.c
1 /* GStreamer
2  * Copyright (C) <2006> Edward Hervey <edward@fluendo.com>
3  * Copyright (C) <2009> Sebastian Dröge <sebastian.droege@collabora.co.uk>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  */
20
21 /**
22  * SECTION:element-decodebin2
23  *
24  * #GstBin that auto-magically constructs a decoding pipeline using available
25  * decoders and demuxers via auto-plugging.
26  *
27  * At this stage, decodebin2 is considered UNSTABLE. The API provided in the
28  * signals is expected to change in the near future.
29  *
30  * To try out decodebin2, you can set the USE_DECODEBIN2 environment
31  * variable (USE_DECODEBIN2=1 for example). This will cause playbin to use
32  * decodebin2 instead of the older #GstDecodeBin for its internal auto-plugging.
33  */
34
35 /* Implementation notes:
36  *
37  * The following section describes how decodebin2 works internally.
38  *
39  * The first part of decodebin2 is it's typefind element, which tries
40  * to get the type of the input stream. If the type is found autoplugging starts.
41  *
42  * decodebin2 internally organizes the elements it autoplugged into GstDecodeChains
43  * and GstDecodeGroups. A decode chain is a single chain of decoding, this
44  * means that if decodebin2 every autoplugs an element with two+ srcpads
45  * (e.g. a demuxer) this will end the chain and everything following this
46  * demuxer will be put into decode groups below the chain. Otherwise,
47  * if an element has a single srcpad that outputs raw data the decode chain
48  * is ended too and a GstDecodePad is stored and blocked.
49  *
50  * A decode group combines a number of chains that are created by a
51  * demuxer element. All those chains are connected through a multiqueue to
52  * the demuxer. A new group for the same demuxer is only created if the
53  * demuxer has signaled no-more pads, in which case all following pads
54  * create a new chain in the new group.
55  *
56  * This continues until the top-level decode chain is complete. A decode
57  * chain is complete if it either ends with a blocked endpad, if autoplugging
58  * stopped because no suitable plugins could be found or if the active group
59  * is complete. A decode group OTOH is complete if all child chains are complete.
60  *
61  * If this happens at some point, all endpads of all active groups are exposed.
62  * For this decodebin2 adds the endpads, signals no-more-pads and then unblocks
63  * them. Now playback starts.
64  *
65  * If one of the chains that end on a endpad receives EOS decodebin2 checks upwards
66  * via the parent pointers if all chains and groups are drained. In that case
67  * everything goes into EOS.
68  * If there is a chain where the active group is drained but there exist next groups
69  * the active group is hidden (endpads are removed) and the next group is exposed.
70  *
71  * Note 1: If we're talking about blocked endpads this really means that the
72  * *target* pads of the endpads are blocked. Pads that are exposed to the outside
73  * should never ever be blocked!
74  *
75  * Note 2: If a group is complete and the parent's chain demuxer adds new pads
76  * but never signaled no-more-pads this additional pads will be ignored!
77  *
78  */
79
80 #ifdef HAVE_CONFIG_H
81 #include "config.h"
82 #endif
83
84 #include <gst/gst-i18n-plugin.h>
85
86 #include <string.h>
87 #include <gst/gst.h>
88 #include <gst/pbutils/pbutils.h>
89
90 #include "gstplay-marshal.h"
91 #include "gstplay-enum.h"
92 #include "gstfactorylists.h"
93
94 /* generic templates */
95 static GstStaticPadTemplate decoder_bin_sink_template =
96 GST_STATIC_PAD_TEMPLATE ("sink",
97     GST_PAD_SINK,
98     GST_PAD_ALWAYS,
99     GST_STATIC_CAPS_ANY);
100
101 static GstStaticPadTemplate decoder_bin_src_template =
102 GST_STATIC_PAD_TEMPLATE ("src%d",
103     GST_PAD_SRC,
104     GST_PAD_SOMETIMES,
105     GST_STATIC_CAPS_ANY);
106
107 GST_DEBUG_CATEGORY_STATIC (gst_decode_bin_debug);
108 #define GST_CAT_DEFAULT gst_decode_bin_debug
109
110 typedef struct _GstDecodeChain GstDecodeChain;
111 typedef struct _GstDecodeGroup GstDecodeGroup;
112 typedef struct _GstDecodePad GstDecodePad;
113 typedef GstGhostPadClass GstDecodePadClass;
114 typedef struct _GstDecodeBin GstDecodeBin;
115 typedef struct _GstDecodeBin GstDecodeBin2;
116 typedef struct _GstDecodeBinClass GstDecodeBinClass;
117
118 #define GST_TYPE_DECODE_BIN             (gst_decode_bin_get_type())
119 #define GST_DECODE_BIN_CAST(obj)        ((GstDecodeBin*)(obj))
120 #define GST_DECODE_BIN(obj)             (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_DECODE_BIN,GstDecodeBin))
121 #define GST_DECODE_BIN_CLASS(klass)     (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_DECODE_BIN,GstDecodeBinClass))
122 #define GST_IS_DECODE_BIN(obj)          (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_DECODE_BIN))
123 #define GST_IS_DECODE_BIN_CLASS(klass)  (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_DECODE_BIN))
124
125 /**
126  *  GstDecodeBin2:
127  *
128  *  The opaque #DecodeBin2 data structure
129  */
130 struct _GstDecodeBin
131 {
132   GstBin bin;                   /* we extend GstBin */
133
134   /* properties */
135   GstCaps *caps;                /* caps on which to stop decoding */
136   gchar *encoding;              /* encoding of subtitles */
137
138   GstElement *typefind;         /* this holds the typefind object */
139
140   GMutex *expose_lock;          /* Protects exposal and removal of groups */
141   GstDecodeChain *decode_chain; /* Top level decode chain */
142   gint nbpads;                  /* unique identifier for source pads */
143
144   GValueArray *factories;       /* factories we can use for selecting elements */
145
146   GList *subtitles;             /* List of elements with subtitle-encoding,
147                                  * protected by object lock! */
148
149   gboolean have_type;           /* if we received the have_type signal */
150   guint have_type_id;           /* signal id for have-type from typefind */
151
152   gboolean async_pending;       /* async-start has been emited */
153
154   GMutex *dyn_lock;             /* lock protecting pad blocking */
155   gboolean shutdown;            /* if we are shutting down */
156   GList *blocked_pads;          /* pads that have set to block */
157 };
158
159 struct _GstDecodeBinClass
160 {
161   GstBinClass parent_class;
162
163   /* signal we fire when a new pad has been decoded into raw audio/video */
164   void (*new_decoded_pad) (GstElement * element, GstPad * pad, gboolean last);
165   /* signal we fire when a pad has been removed */
166   void (*removed_decoded_pad) (GstElement * element, GstPad * pad);
167   /* signal fired when we found a pad that we cannot decode */
168   void (*unknown_type) (GstElement * element, GstPad * pad, GstCaps * caps);
169
170   /* signal fired to know if we continue trying to decode the given caps */
171     gboolean (*autoplug_continue) (GstElement * element, GstPad * pad,
172       GstCaps * caps);
173   /* signal fired to get a list of factories to try to autoplug */
174   GValueArray *(*autoplug_factories) (GstElement * element, GstPad * pad,
175       GstCaps * caps);
176   /* signal fired to sort the factories */
177   GValueArray *(*autoplug_sort) (GstElement * element, GstPad * pad,
178       GstCaps * caps, GValueArray * factories);
179   /* signal fired to select from the proposed list of factories */
180     GstAutoplugSelectResult (*autoplug_select) (GstElement * element,
181       GstPad * pad, GstCaps * caps, GstElementFactory * factory);
182
183   /* fired when the last group is drained */
184   void (*drained) (GstElement * element);
185 };
186
187 /* signals */
188 enum
189 {
190   SIGNAL_NEW_DECODED_PAD,
191   SIGNAL_REMOVED_DECODED_PAD,
192   SIGNAL_UNKNOWN_TYPE,
193   SIGNAL_AUTOPLUG_CONTINUE,
194   SIGNAL_AUTOPLUG_FACTORIES,
195   SIGNAL_AUTOPLUG_SELECT,
196   SIGNAL_AUTOPLUG_SORT,
197   SIGNAL_DRAINED,
198   LAST_SIGNAL
199 };
200
201 /* Properties */
202 enum
203 {
204   PROP_0,
205   PROP_CAPS,
206   PROP_SUBTITLE_ENCODING,
207   PROP_SINK_CAPS,
208   PROP_LAST
209 };
210
211 static GstBinClass *parent_class;
212 static guint gst_decode_bin_signals[LAST_SIGNAL] = { 0 };
213
214 static const GstElementDetails gst_decode_bin_details =
215 GST_ELEMENT_DETAILS ("Decoder Bin",
216     "Generic/Bin/Decoder",
217     "Autoplug and decode to raw media",
218     "Edward Hervey <edward.hervey@collabora.co.uk>, "
219     "Sebastian Dröge <sebastian.droege@collabora.co.uk>");
220
221 static void do_async_start (GstDecodeBin * dbin);
222 static void do_async_done (GstDecodeBin * dbin);
223
224 static void type_found (GstElement * typefind, guint probability,
225     GstCaps * caps, GstDecodeBin * decode_bin);
226
227 static gboolean gst_decode_bin_autoplug_continue (GstElement * element,
228     GstPad * pad, GstCaps * caps);
229 static GValueArray *gst_decode_bin_autoplug_factories (GstElement *
230     element, GstPad * pad, GstCaps * caps);
231 static GValueArray *gst_decode_bin_autoplug_sort (GstElement * element,
232     GstPad * pad, GstCaps * caps, GValueArray * factories);
233 static GstAutoplugSelectResult gst_decode_bin_autoplug_select (GstElement *
234     element, GstPad * pad, GstCaps * caps, GstElementFactory * factory);
235
236 static void gst_decode_bin_set_property (GObject * object, guint prop_id,
237     const GValue * value, GParamSpec * pspec);
238 static void gst_decode_bin_get_property (GObject * object, guint prop_id,
239     GValue * value, GParamSpec * pspec);
240 static void gst_decode_bin_set_caps (GstDecodeBin * dbin, GstCaps * caps);
241 static GstCaps *gst_decode_bin_get_caps (GstDecodeBin * dbin);
242 static void caps_notify_cb (GstPad * pad, GParamSpec * unused,
243     GstDecodeChain * chain);
244
245 static GstPad *find_sink_pad (GstElement * element);
246 static GstStateChangeReturn gst_decode_bin_change_state (GstElement * element,
247     GstStateChange transition);
248
249 #define EXPOSE_LOCK(dbin) G_STMT_START {                                \
250     GST_LOG_OBJECT (dbin,                                               \
251                     "expose locking from thread %p",                    \
252                     g_thread_self ());                                  \
253     g_mutex_lock (GST_DECODE_BIN_CAST(dbin)->expose_lock);              \
254     GST_LOG_OBJECT (dbin,                                               \
255                     "expose locked from thread %p",                     \
256                     g_thread_self ());                                  \
257 } G_STMT_END
258
259 #define EXPOSE_UNLOCK(dbin) G_STMT_START {                              \
260     GST_LOG_OBJECT (dbin,                                               \
261                     "expose unlocking from thread %p",                  \
262                     g_thread_self ());                                  \
263     g_mutex_unlock (GST_DECODE_BIN_CAST(dbin)->expose_lock);            \
264 } G_STMT_END
265
266 #define DYN_LOCK(dbin) G_STMT_START {                   \
267     GST_LOG_OBJECT (dbin,                                               \
268                     "dynlocking from thread %p",                        \
269                     g_thread_self ());                                  \
270     g_mutex_lock (GST_DECODE_BIN_CAST(dbin)->dyn_lock);                 \
271     GST_LOG_OBJECT (dbin,                                               \
272                     "dynlocked from thread %p",                         \
273                     g_thread_self ());                                  \
274 } G_STMT_END
275
276 #define DYN_UNLOCK(dbin) G_STMT_START {                 \
277     GST_LOG_OBJECT (dbin,                                               \
278                     "dynunlocking from thread %p",                      \
279                     g_thread_self ());                                  \
280     g_mutex_unlock (GST_DECODE_BIN_CAST(dbin)->dyn_lock);               \
281 } G_STMT_END
282
283 /* GstDecodeGroup
284  *
285  * Streams belonging to the same group/chain of a media file
286  *
287  * When changing something here lock the parent chain!
288  */
289 struct _GstDecodeGroup
290 {
291   GstDecodeBin *dbin;
292   GstDecodeChain *parent;
293
294   GstElement *multiqueue;       /* Used for linking all child chains */
295   gulong overrunsig;            /* the overrun signal for multiqueue */
296
297   gboolean overrun;             /* TRUE if the multiqueue signaled overrun. This
298                                  * means that we should really expose the group */
299
300   gboolean no_more_pads;        /* TRUE if the demuxer signaled no-more-pads */
301   gboolean drained;             /* TRUE if the all children are drained */
302
303   GList *children;              /* List of GstDecodeChains in this group */
304
305   GList *reqpads;               /* List of RequestPads for multiqueue, there is
306                                  * exactly one RequestPad per child chain */
307 };
308
309 struct _GstDecodeChain
310 {
311   GstDecodeGroup *parent;
312   GstDecodeBin *dbin;
313
314   GMutex *lock;                 /* Protects this chain and its groups */
315
316   gboolean demuxer;             /* TRUE if elements->data is a demuxer */
317   GList *elements;              /* All elements in this group, first
318                                    is the latest and most downstream element */
319
320   /* Note: there are only groups if the last element of this chain
321    * is a demuxer, otherwise the chain will end with an endpad.
322    * The other way around this means, that endpad only exists if this
323    * chain doesn't end with a demuxer! */
324
325   GstDecodeGroup *active_group; /* Currently active group */
326   GList *next_groups;           /* head is newest group, tail is next group.
327                                    a new group will be created only if the head
328                                    group had no-more-pads. If it's only exposed
329                                    all new pads will be ignored! */
330   GList *pending_pads;          /* Pads that have no fixed caps yet */
331
332   GstDecodePad *endpad;         /* Pad of this chain that could be exposed */
333   gboolean deadend;             /* This chain is incomplete and can't be completed,
334                                    e.g. no suitable decoder could be found
335                                  */
336
337   /* FIXME: This should be done directly via a thread! */
338   GList *old_groups;            /* Groups that should be freed later */
339 };
340
341 static void gst_decode_chain_free (GstDecodeChain * chain);
342 static GstDecodeChain *gst_decode_chain_new (GstDecodeBin * dbin,
343     GstDecodeGroup * group);
344 static void gst_decode_group_hide (GstDecodeGroup * group);
345 static void gst_decode_group_free (GstDecodeGroup * group);
346 static GstDecodeGroup *gst_decode_group_new (GstDecodeBin * dbin,
347     GstDecodeChain * chain);
348 static gboolean gst_decode_chain_is_complete (GstDecodeChain * chain);
349 static void gst_decode_chain_handle_eos (GstDecodeChain * chain);
350 static gboolean gst_decode_chain_expose (GstDecodeChain * chain,
351     GList ** endpads);
352 static gboolean gst_decode_chain_is_drained (GstDecodeChain * chain);
353 static gboolean gst_decode_group_is_complete (GstDecodeGroup * group);
354 static GstPad *gst_decode_group_control_demuxer_pad (GstDecodeGroup * group,
355     GstPad * pad);
356 static gboolean gst_decode_group_is_drained (GstDecodeGroup * group);
357
358 static gboolean gst_decode_bin_expose (GstDecodeBin * dbin);
359
360 #define CHAIN_MUTEX_LOCK(chain) G_STMT_START {                          \
361     GST_LOG_OBJECT (chain->dbin,                                        \
362                     "locking chain %p from thread %p",                  \
363                     chain, g_thread_self ());                           \
364     g_mutex_lock (chain->lock);                                         \
365     GST_LOG_OBJECT (chain->dbin,                                        \
366                     "locked chain %p from thread %p",                   \
367                     chain, g_thread_self ());                           \
368 } G_STMT_END
369
370 #define CHAIN_MUTEX_UNLOCK(chain) G_STMT_START {                        \
371     GST_LOG_OBJECT (chain->dbin,                                        \
372                     "unlocking chain %p from thread %p",                \
373                     chain, g_thread_self ());                           \
374     g_mutex_unlock (chain->lock);                                       \
375 } G_STMT_END
376
377 /* GstDecodePad
378  *
379  * GstPad private used for source pads of chains
380  */
381 struct _GstDecodePad
382 {
383   GstGhostPad parent;
384   GstDecodeBin *dbin;
385   GstDecodeChain *chain;
386
387   gboolean blocked;             /* the *target* pad is blocked */
388   gboolean exposed;             /* the pad is exposed */
389   gboolean drained;             /* an EOS has been seen on the pad */
390 };
391
392 G_DEFINE_TYPE (GstDecodePad, gst_decode_pad, GST_TYPE_GHOST_PAD);
393 #define GST_TYPE_DECODE_PAD (gst_decode_pad_get_type ())
394 #define GST_DECODE_PAD(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_DECODE_PAD,GstDecodePad))
395
396 static GstDecodePad *gst_decode_pad_new (GstDecodeBin * dbin, GstPad * pad,
397     GstDecodeChain * chain);
398 static void gst_decode_pad_activate (GstDecodePad * dpad,
399     GstDecodeChain * chain);
400 static void gst_decode_pad_unblock (GstDecodePad * dpad);
401 static void gst_decode_pad_set_blocked (GstDecodePad * dpad, gboolean blocked);
402
403 /********************************
404  * Standard GObject boilerplate *
405  ********************************/
406
407 static void gst_decode_bin_class_init (GstDecodeBinClass * klass);
408 static void gst_decode_bin_init (GstDecodeBin * decode_bin);
409 static void gst_decode_bin_dispose (GObject * object);
410 static void gst_decode_bin_finalize (GObject * object);
411
412 static GType
413 gst_decode_bin_get_type (void)
414 {
415   static GType gst_decode_bin_type = 0;
416
417   if (!gst_decode_bin_type) {
418     static const GTypeInfo gst_decode_bin_info = {
419       sizeof (GstDecodeBinClass),
420       NULL,
421       NULL,
422       (GClassInitFunc) gst_decode_bin_class_init,
423       NULL,
424       NULL,
425       sizeof (GstDecodeBin),
426       0,
427       (GInstanceInitFunc) gst_decode_bin_init,
428       NULL
429     };
430
431     gst_decode_bin_type =
432         g_type_register_static (GST_TYPE_BIN, "GstDecodeBin2",
433         &gst_decode_bin_info, 0);
434   }
435
436   return gst_decode_bin_type;
437 }
438
439 static gboolean
440 _gst_boolean_accumulator (GSignalInvocationHint * ihint,
441     GValue * return_accu, const GValue * handler_return, gpointer dummy)
442 {
443   gboolean myboolean;
444
445   myboolean = g_value_get_boolean (handler_return);
446   if (!(ihint->run_type & G_SIGNAL_RUN_CLEANUP))
447     g_value_set_boolean (return_accu, myboolean);
448
449   /* stop emission if FALSE */
450   return myboolean;
451 }
452
453 /* we collect the first result */
454 static gboolean
455 _gst_array_accumulator (GSignalInvocationHint * ihint,
456     GValue * return_accu, const GValue * handler_return, gpointer dummy)
457 {
458   gpointer array;
459
460   array = g_value_get_boxed (handler_return);
461   if (!(ihint->run_type & G_SIGNAL_RUN_CLEANUP))
462     g_value_set_boxed (return_accu, array);
463
464   return FALSE;
465 }
466
467 static gboolean
468 _gst_select_accumulator (GSignalInvocationHint * ihint,
469     GValue * return_accu, const GValue * handler_return, gpointer dummy)
470 {
471   GstAutoplugSelectResult res;
472
473   res = g_value_get_enum (handler_return);
474   if (!(ihint->run_type & G_SIGNAL_RUN_CLEANUP))
475     g_value_set_enum (return_accu, res);
476
477   return FALSE;
478 }
479
480 static void
481 gst_decode_bin_class_init (GstDecodeBinClass * klass)
482 {
483   GObjectClass *gobject_klass;
484   GstElementClass *gstelement_klass;
485
486   gobject_klass = (GObjectClass *) klass;
487   gstelement_klass = (GstElementClass *) klass;
488
489   parent_class = g_type_class_peek_parent (klass);
490
491   gobject_klass->dispose = GST_DEBUG_FUNCPTR (gst_decode_bin_dispose);
492   gobject_klass->finalize = GST_DEBUG_FUNCPTR (gst_decode_bin_finalize);
493   gobject_klass->set_property = GST_DEBUG_FUNCPTR (gst_decode_bin_set_property);
494   gobject_klass->get_property = GST_DEBUG_FUNCPTR (gst_decode_bin_get_property);
495
496   /**
497    * GstDecodeBin2::new-decoded-pad:
498    * @bin: The decodebin
499    * @pad: The newly created pad
500    * @islast: #TRUE if this is the last pad to be added. Deprecated.
501    *
502    * This signal gets emitted as soon as a new pad of the same type as one of
503    * the valid 'raw' types is added.
504    */
505   gst_decode_bin_signals[SIGNAL_NEW_DECODED_PAD] =
506       g_signal_new ("new-decoded-pad", G_TYPE_FROM_CLASS (klass),
507       G_SIGNAL_RUN_LAST,
508       G_STRUCT_OFFSET (GstDecodeBinClass, new_decoded_pad), NULL, NULL,
509       gst_play_marshal_VOID__OBJECT_BOOLEAN, G_TYPE_NONE, 2, GST_TYPE_PAD,
510       G_TYPE_BOOLEAN);
511
512   /**
513    * GstDecodeBin2::removed-decoded-pad:
514    * @bin: The decodebin
515    * @pad: The pad that was removed
516    *
517    * This signal is emitted when a 'final' caps pad has been removed.
518    */
519   gst_decode_bin_signals[SIGNAL_REMOVED_DECODED_PAD] =
520       g_signal_new ("removed-decoded-pad", G_TYPE_FROM_CLASS (klass),
521       G_SIGNAL_RUN_LAST,
522       G_STRUCT_OFFSET (GstDecodeBinClass, removed_decoded_pad), NULL, NULL,
523       gst_marshal_VOID__OBJECT, G_TYPE_NONE, 1, GST_TYPE_PAD);
524
525   /**
526    * GstDecodeBin2::unknown-type:
527    * @bin: The decodebin
528    * @pad: The new pad containing caps that cannot be resolved to a 'final'
529    *       stream type.
530    * @caps: The #GstCaps of the pad that cannot be resolved.
531    *
532    * This signal is emitted when a pad for which there is no further possible
533    * decoding is added to the decodebin.
534    */
535   gst_decode_bin_signals[SIGNAL_UNKNOWN_TYPE] =
536       g_signal_new ("unknown-type", G_TYPE_FROM_CLASS (klass),
537       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstDecodeBinClass, unknown_type),
538       NULL, NULL, gst_marshal_VOID__OBJECT_BOXED, G_TYPE_NONE, 2,
539       GST_TYPE_PAD, GST_TYPE_CAPS);
540
541   /**
542    * GstDecodeBin2::autoplug-continue:
543    * @bin: The decodebin
544    * @pad: The #GstPad.
545    * @caps: The #GstCaps found.
546    *
547    * This signal is emitted whenever decodebin2 finds a new stream. It is
548    * emitted before looking for any elements that can handle that stream.
549    *
550    * Returns: #TRUE if you wish decodebin2 to look for elements that can
551    * handle the given @caps. If #FALSE, those caps will be considered as
552    * final and the pad will be exposed as such (see 'new-decoded-pad'
553    * signal).
554    */
555   gst_decode_bin_signals[SIGNAL_AUTOPLUG_CONTINUE] =
556       g_signal_new ("autoplug-continue", G_TYPE_FROM_CLASS (klass),
557       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstDecodeBinClass, autoplug_continue),
558       _gst_boolean_accumulator, NULL, gst_play_marshal_BOOLEAN__OBJECT_BOXED,
559       G_TYPE_BOOLEAN, 2, GST_TYPE_PAD, GST_TYPE_CAPS);
560
561   /**
562    * GstDecodeBin2::autoplug-factories:
563    * @bin: The decodebin
564    * @pad: The #GstPad.
565    * @caps: The #GstCaps found.
566    *
567    * This function is emited when an array of possible factories for @caps on
568    * @pad is needed. Decodebin2 will by default return an array with all
569    * compatible factories, sorted by rank. 
570    *
571    * If this function returns NULL, @pad will be exposed as a final caps.
572    *
573    * If this function returns an empty array, the pad will be considered as 
574    * having an unhandled type media type.
575    *
576    * Returns: a #GValueArray* with a list of factories to try. The factories are
577    * by default tried in the returned order or based on the index returned by
578    * "autoplug-select".
579    */
580   gst_decode_bin_signals[SIGNAL_AUTOPLUG_FACTORIES] =
581       g_signal_new ("autoplug-factories", G_TYPE_FROM_CLASS (klass),
582       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstDecodeBinClass,
583           autoplug_factories), _gst_array_accumulator, NULL,
584       gst_play_marshal_BOXED__OBJECT_BOXED, G_TYPE_VALUE_ARRAY, 2,
585       GST_TYPE_PAD, GST_TYPE_CAPS);
586
587   /**
588    * GstDecodeBin2::autoplug-sort:
589    * @bin: The decodebin
590    * @pad: The #GstPad.
591    * @caps: The #GstCaps.
592    * @factories: A #GValueArray of possible #GstElementFactory to use.
593    *
594    * Once decodebin2 has found the possible #GstElementFactory objects to try
595    * for @caps on @pad, this signal is emited. The purpose of the signal is for
596    * the application to perform additional sorting or filtering on the element
597    * factory array.
598    *
599    * The callee should copy and modify @factories.
600    *
601    * Returns: A new sorted array of #GstElementFactory objects.
602    */
603   gst_decode_bin_signals[SIGNAL_AUTOPLUG_SORT] =
604       g_signal_new ("autoplug-sort", G_TYPE_FROM_CLASS (klass),
605       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstDecodeBinClass, autoplug_sort),
606       NULL, NULL, gst_play_marshal_BOXED__OBJECT_BOXED_BOXED,
607       G_TYPE_VALUE_ARRAY, 3, GST_TYPE_PAD, GST_TYPE_CAPS, G_TYPE_VALUE_ARRAY);
608
609   /**
610    * GstDecodeBin2::autoplug-select:
611    * @bin: The decodebin
612    * @pad: The #GstPad.
613    * @caps: The #GstCaps.
614    * @factory: A #GstElementFactory to use
615    *
616    * This signal is emitted once decodebin2 has found all the possible
617    * #GstElementFactory that can be used to handle the given @caps. For each of
618    * those factories, this signal is emited.
619    *
620    * The signal handler should return a #GST_TYPE_AUTOPLUG_SELECT_RESULT enum
621    * value indicating what decodebin2 should do next.
622    *
623    * A value of #GST_AUTOPLUG_SELECT_TRY will try to autoplug an element from
624    * @factory.
625    *
626    * A value of #GST_AUTOPLUG_SELECT_EXPOSE will expose @pad without plugging
627    * any element to it.
628    *
629    * A value of #GST_AUTOPLUG_SELECT_SKIP will skip @factory and move to the
630    * next factory.
631    *
632    * Returns: a #GST_TYPE_AUTOPLUG_SELECT_RESULT that indicates the required
633    * operation. the default handler will always return
634    * #GST_AUTOPLUG_SELECT_TRY.
635    */
636   gst_decode_bin_signals[SIGNAL_AUTOPLUG_SELECT] =
637       g_signal_new ("autoplug-select", G_TYPE_FROM_CLASS (klass),
638       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstDecodeBinClass, autoplug_select),
639       _gst_select_accumulator, NULL,
640       gst_play_marshal_ENUM__OBJECT_BOXED_OBJECT,
641       GST_TYPE_AUTOPLUG_SELECT_RESULT, 3, GST_TYPE_PAD, GST_TYPE_CAPS,
642       GST_TYPE_ELEMENT_FACTORY);
643
644   /**
645    * GstDecodeBin2::drained
646    * @bin: The decodebin
647    *
648    * This signal is emitted once decodebin2 has finished decoding all the data.
649    *
650    * Since: 0.10.16
651    */
652   gst_decode_bin_signals[SIGNAL_DRAINED] =
653       g_signal_new ("drained", G_TYPE_FROM_CLASS (klass),
654       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstDecodeBinClass, drained),
655       NULL, NULL, gst_marshal_VOID__VOID, G_TYPE_NONE, 0, G_TYPE_NONE);
656
657   g_object_class_install_property (gobject_klass, PROP_CAPS,
658       g_param_spec_boxed ("caps", "Caps", "The caps on which to stop decoding.",
659           GST_TYPE_CAPS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
660
661   g_object_class_install_property (gobject_klass, PROP_SUBTITLE_ENCODING,
662       g_param_spec_string ("subtitle-encoding", "subtitle encoding",
663           "Encoding to assume if input subtitles are not in UTF-8 encoding. "
664           "If not set, the GST_SUBTITLE_ENCODING environment variable will "
665           "be checked for an encoding to use. If that is not set either, "
666           "ISO-8859-15 will be assumed.", NULL,
667           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
668
669   g_object_class_install_property (gobject_klass, PROP_SINK_CAPS,
670       g_param_spec_boxed ("sink-caps", "Sink Caps",
671           "The caps of the input data. (NULL = use typefind element)",
672           GST_TYPE_CAPS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
673
674   klass->autoplug_continue =
675       GST_DEBUG_FUNCPTR (gst_decode_bin_autoplug_continue);
676   klass->autoplug_factories =
677       GST_DEBUG_FUNCPTR (gst_decode_bin_autoplug_factories);
678   klass->autoplug_sort = GST_DEBUG_FUNCPTR (gst_decode_bin_autoplug_sort);
679   klass->autoplug_select = GST_DEBUG_FUNCPTR (gst_decode_bin_autoplug_select);
680
681   gst_element_class_add_pad_template (gstelement_klass,
682       gst_static_pad_template_get (&decoder_bin_sink_template));
683   gst_element_class_add_pad_template (gstelement_klass,
684       gst_static_pad_template_get (&decoder_bin_src_template));
685
686   gst_element_class_set_details (gstelement_klass, &gst_decode_bin_details);
687
688   gstelement_klass->change_state =
689       GST_DEBUG_FUNCPTR (gst_decode_bin_change_state);
690 }
691
692 static void
693 gst_decode_bin_init (GstDecodeBin * decode_bin)
694 {
695   /* first filter out the interesting element factories */
696   decode_bin->factories =
697       gst_factory_list_get_elements (GST_FACTORY_LIST_DECODER);
698
699   /* we create the typefind element only once */
700   decode_bin->typefind = gst_element_factory_make ("typefind", "typefind");
701   if (!decode_bin->typefind) {
702     g_warning ("can't find typefind element, decodebin will not work");
703   } else {
704     GstPad *pad;
705     GstPad *gpad;
706
707     /* add the typefind element */
708     if (!gst_bin_add (GST_BIN (decode_bin), decode_bin->typefind)) {
709       g_warning ("Could not add typefind element, decodebin will not work");
710       gst_object_unref (decode_bin->typefind);
711       decode_bin->typefind = NULL;
712     }
713
714     /* get the sinkpad */
715     pad = gst_element_get_static_pad (decode_bin->typefind, "sink");
716
717     /* ghost the sink pad to ourself */
718     gpad = gst_ghost_pad_new ("sink", pad);
719     gst_pad_set_active (gpad, TRUE);
720     gst_element_add_pad (GST_ELEMENT (decode_bin), gpad);
721
722     gst_object_unref (pad);
723
724     /* connect a signal to find out when the typefind element found
725      * a type */
726     decode_bin->have_type_id =
727         g_signal_connect (G_OBJECT (decode_bin->typefind), "have-type",
728         G_CALLBACK (type_found), decode_bin);
729   }
730
731   decode_bin->expose_lock = g_mutex_new ();
732   decode_bin->decode_chain = NULL;
733
734   decode_bin->dyn_lock = g_mutex_new ();
735   decode_bin->shutdown = FALSE;
736   decode_bin->blocked_pads = NULL;
737
738   decode_bin->caps =
739       gst_caps_from_string ("video/x-raw-yuv;video/x-raw-rgb;video/x-raw-gray;"
740       "audio/x-raw-int;audio/x-raw-float;" "text/plain;text/x-pango-markup;"
741       "video/x-dvd-subpicture; subpicture/x-pgs");
742 }
743
744 static void
745 gst_decode_bin_dispose (GObject * object)
746 {
747   GstDecodeBin *decode_bin;
748
749   decode_bin = GST_DECODE_BIN (object);
750
751   if (decode_bin->factories)
752     g_value_array_free (decode_bin->factories);
753   decode_bin->factories = NULL;
754
755   if (decode_bin->decode_chain)
756     gst_decode_chain_free (decode_bin->decode_chain);
757   decode_bin->decode_chain = NULL;
758
759   if (decode_bin->caps)
760     gst_caps_unref (decode_bin->caps);
761   decode_bin->caps = NULL;
762
763   g_free (decode_bin->encoding);
764   decode_bin->encoding = NULL;
765
766   g_list_free (decode_bin->subtitles);
767   decode_bin->subtitles = NULL;
768
769   G_OBJECT_CLASS (parent_class)->dispose (object);
770 }
771
772 static void
773 gst_decode_bin_finalize (GObject * object)
774 {
775   GstDecodeBin *decode_bin;
776
777   decode_bin = GST_DECODE_BIN (object);
778
779   if (decode_bin->expose_lock) {
780     g_mutex_free (decode_bin->expose_lock);
781     decode_bin->expose_lock = NULL;
782   }
783
784   if (decode_bin->dyn_lock) {
785     g_mutex_free (decode_bin->dyn_lock);
786     decode_bin->dyn_lock = NULL;
787   }
788
789   G_OBJECT_CLASS (parent_class)->finalize (object);
790 }
791
792 /* _set_caps
793  * Changes the caps on which decodebin will stop decoding.
794  * Will unref the previously set one. The refcount of the given caps will be
795  * increased.
796  * @caps can be NULL.
797  *
798  * MT-safe
799  */
800 static void
801 gst_decode_bin_set_caps (GstDecodeBin * dbin, GstCaps * caps)
802 {
803   GST_DEBUG_OBJECT (dbin, "Setting new caps: %" GST_PTR_FORMAT, caps);
804
805   GST_OBJECT_LOCK (dbin);
806   gst_caps_replace (&dbin->caps, caps);
807   GST_OBJECT_UNLOCK (dbin);
808 }
809
810 /* _get_caps
811  * Returns the currently configured caps on which decodebin will stop decoding.
812  * The returned caps (if not NULL), will have its refcount incremented.
813  *
814  * MT-safe
815  */
816 static GstCaps *
817 gst_decode_bin_get_caps (GstDecodeBin * dbin)
818 {
819   GstCaps *caps;
820
821   GST_DEBUG_OBJECT (dbin, "Getting currently set caps");
822
823   GST_OBJECT_LOCK (dbin);
824   caps = dbin->caps;
825   if (caps)
826     gst_caps_ref (caps);
827   GST_OBJECT_UNLOCK (dbin);
828
829   return caps;
830 }
831
832 static void
833 gst_decode_bin_set_sink_caps (GstDecodeBin * dbin, GstCaps * caps)
834 {
835   GST_DEBUG_OBJECT (dbin, "Setting new caps: %" GST_PTR_FORMAT, caps);
836
837   g_object_set (dbin->typefind, "force-caps", caps, NULL);
838 }
839
840 static GstCaps *
841 gst_decode_bin_get_sink_caps (GstDecodeBin * dbin)
842 {
843   GstCaps *caps;
844
845   GST_DEBUG_OBJECT (dbin, "Getting currently set caps");
846
847   g_object_get (dbin->typefind, "force-caps", &caps, NULL);
848
849   return caps;
850 }
851
852 static void
853 gst_decode_bin_set_subs_encoding (GstDecodeBin * dbin, const gchar * encoding)
854 {
855   GList *walk;
856
857   GST_DEBUG_OBJECT (dbin, "Setting new encoding: %s", GST_STR_NULL (encoding));
858
859   GST_OBJECT_LOCK (dbin);
860   g_free (dbin->encoding);
861   dbin->encoding = g_strdup (encoding);
862
863   /* set the subtitle encoding on all added elements */
864   for (walk = dbin->subtitles; walk; walk = g_list_next (walk)) {
865     g_object_set (G_OBJECT (walk->data), "subtitle-encoding", dbin->encoding,
866         NULL);
867   }
868   GST_OBJECT_UNLOCK (dbin);
869 }
870
871 static gchar *
872 gst_decode_bin_get_subs_encoding (GstDecodeBin * dbin)
873 {
874   gchar *encoding;
875
876   GST_DEBUG_OBJECT (dbin, "Getting currently set encoding");
877
878   GST_OBJECT_LOCK (dbin);
879   encoding = g_strdup (dbin->encoding);
880   GST_OBJECT_UNLOCK (dbin);
881
882   return encoding;
883 }
884
885 static void
886 gst_decode_bin_set_property (GObject * object, guint prop_id,
887     const GValue * value, GParamSpec * pspec)
888 {
889   GstDecodeBin *dbin;
890
891   dbin = GST_DECODE_BIN (object);
892
893   switch (prop_id) {
894     case PROP_CAPS:
895       gst_decode_bin_set_caps (dbin, g_value_get_boxed (value));
896       break;
897     case PROP_SUBTITLE_ENCODING:
898       gst_decode_bin_set_subs_encoding (dbin, g_value_get_string (value));
899       break;
900     case PROP_SINK_CAPS:
901       gst_decode_bin_set_sink_caps (dbin, g_value_get_boxed (value));
902       break;
903     default:
904       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
905       break;
906   }
907 }
908
909 static void
910 gst_decode_bin_get_property (GObject * object, guint prop_id,
911     GValue * value, GParamSpec * pspec)
912 {
913   GstDecodeBin *dbin;
914
915   dbin = GST_DECODE_BIN (object);
916   switch (prop_id) {
917     case PROP_CAPS:
918       g_value_take_boxed (value, gst_decode_bin_get_caps (dbin));
919       break;
920     case PROP_SUBTITLE_ENCODING:
921       g_value_take_string (value, gst_decode_bin_get_subs_encoding (dbin));
922       break;
923     case PROP_SINK_CAPS:
924       g_value_take_boxed (value, gst_decode_bin_get_sink_caps (dbin));
925       break;
926     default:
927       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
928       break;
929   }
930 }
931
932
933 /*****
934  * Default autoplug signal handlers
935  *****/
936 static gboolean
937 gst_decode_bin_autoplug_continue (GstElement * element, GstPad * pad,
938     GstCaps * caps)
939 {
940   GST_DEBUG_OBJECT (element, "autoplug-continue returns TRUE");
941
942   /* by default we always continue */
943   return TRUE;
944 }
945
946 static GValueArray *
947 gst_decode_bin_autoplug_factories (GstElement * element, GstPad * pad,
948     GstCaps * caps)
949 {
950   GValueArray *result;
951
952   GST_DEBUG_OBJECT (element, "finding factories");
953
954   /* return all compatible factories for caps */
955   result =
956       gst_factory_list_filter (GST_DECODE_BIN_CAST (element)->factories, caps);
957
958   GST_DEBUG_OBJECT (element, "autoplug-factories returns %p", result);
959
960   return result;
961 }
962
963 static GValueArray *
964 gst_decode_bin_autoplug_sort (GstElement * element, GstPad * pad,
965     GstCaps * caps, GValueArray * factories)
966 {
967   GValueArray *result;
968
969   result = g_value_array_copy (factories);
970
971   GST_DEBUG_OBJECT (element, "autoplug-sort returns %p", result);
972
973   /* return input */
974   return result;
975 }
976
977 static GstAutoplugSelectResult
978 gst_decode_bin_autoplug_select (GstElement * element, GstPad * pad,
979     GstCaps * caps, GstElementFactory * factory)
980 {
981   GST_DEBUG_OBJECT (element, "default autoplug-select returns TRY");
982
983   /* Try factory. */
984   return GST_AUTOPLUG_SELECT_TRY;
985 }
986
987 /********
988  * Discovery methods
989  *****/
990
991 static gboolean are_raw_caps (GstDecodeBin * dbin, GstCaps * caps);
992 static gboolean is_demuxer_element (GstElement * srcelement);
993
994 static gboolean connect_pad (GstDecodeBin * dbin, GstElement * src,
995     GstDecodePad * dpad, GstPad * pad, GstCaps * caps, GValueArray * factories,
996     GstDecodeChain * chain);
997 static gboolean connect_element (GstDecodeBin * dbin, GstElement * element,
998     GstDecodeChain * chain);
999 static void expose_pad (GstDecodeBin * dbin, GstElement * src,
1000     GstDecodePad * dpad, GstPad * pad, GstDecodeChain * chain);
1001
1002 static void pad_added_cb (GstElement * element, GstPad * pad,
1003     GstDecodeChain * chain);
1004 static void pad_removed_cb (GstElement * element, GstPad * pad,
1005     GstDecodeChain * chain);
1006 static void no_more_pads_cb (GstElement * element, GstDecodeChain * chain);
1007
1008 static GstDecodeGroup *gst_decode_chain_get_current_group (GstDecodeChain *
1009     chain);
1010
1011 /* called when a new pad is discovered. It will perform some basic actions
1012  * before trying to link something to it.
1013  *
1014  *  - Check the caps, don't do anything when there are no caps or when they have
1015  *    no good type.
1016  *  - signal AUTOPLUG_CONTINUE to check if we need to continue autoplugging this
1017  *    pad.
1018  *  - if the caps are non-fixed, setup a handler to continue autoplugging when
1019  *    the caps become fixed (connect to notify::caps).
1020  *  - get list of factories to autoplug.
1021  *  - continue autoplugging to one of the factories.
1022  */
1023 static void
1024 analyze_new_pad (GstDecodeBin * dbin, GstElement * src, GstPad * pad,
1025     GstCaps * caps, GstDecodeChain * chain)
1026 {
1027   gboolean apcontinue = TRUE;
1028   GValueArray *factories = NULL, *result = NULL;
1029   GstDecodePad *dpad;
1030
1031   GST_DEBUG_OBJECT (dbin, "Pad %s:%s caps:%" GST_PTR_FORMAT,
1032       GST_DEBUG_PAD_NAME (pad), caps);
1033
1034   if (chain->elements && src != chain->elements->data) {
1035     GST_ERROR_OBJECT (dbin, "New pad from not the last element in this chain");
1036     return;
1037   }
1038
1039   if (chain->endpad) {
1040     GST_ERROR_OBJECT (dbin, "New pad in a chain that is already complete");
1041     return;
1042   }
1043
1044   if (chain->demuxer) {
1045     GstDecodeGroup *group;
1046     GstDecodeChain *oldchain = chain;
1047
1048     CHAIN_MUTEX_LOCK (oldchain);
1049     group = gst_decode_chain_get_current_group (chain);
1050     if (group) {
1051       chain = gst_decode_chain_new (dbin, group);
1052       group->children = g_list_prepend (group->children, chain);
1053     }
1054     CHAIN_MUTEX_UNLOCK (oldchain);
1055     if (!group) {
1056       GST_WARNING_OBJECT (dbin, "No current group");
1057       return;
1058     }
1059   }
1060
1061   if ((caps == NULL) || gst_caps_is_empty (caps))
1062     goto unknown_type;
1063
1064   if (gst_caps_is_any (caps))
1065     goto any_caps;
1066
1067   dpad = gst_decode_pad_new (dbin, pad, chain);
1068
1069   /* 1. Emit 'autoplug-continue' the result will tell us if this pads needs
1070    * further autoplugging. */
1071   g_signal_emit (G_OBJECT (dbin),
1072       gst_decode_bin_signals[SIGNAL_AUTOPLUG_CONTINUE], 0, dpad, caps,
1073       &apcontinue);
1074
1075   /* 1.a if autoplug-continue is FALSE or caps is a raw format, goto pad_is_final */
1076   if ((!apcontinue) || are_raw_caps (dbin, caps))
1077     goto expose_pad;
1078
1079   /* 1.b when the caps are not fixed yet, we can't be sure what element to
1080    * connect. We delay autoplugging until the caps are fixed */
1081   if (!gst_caps_is_fixed (caps))
1082     goto non_fixed;
1083
1084   /* 1.c else get the factories and if there's no compatible factory goto
1085    * unknown_type */
1086   g_signal_emit (G_OBJECT (dbin),
1087       gst_decode_bin_signals[SIGNAL_AUTOPLUG_FACTORIES], 0, dpad, caps,
1088       &factories);
1089
1090   /* NULL means that we can expose the pad */
1091   if (factories == NULL)
1092     goto expose_pad;
1093
1094   /* if the array is empty, we have an unknown type */
1095   if (factories->n_values == 0) {
1096     /* no compatible factories */
1097     g_value_array_free (factories);
1098     gst_object_unref (dpad);
1099     goto unknown_type;
1100   }
1101
1102   /* 1.d sort some more. */
1103   g_signal_emit (G_OBJECT (dbin),
1104       gst_decode_bin_signals[SIGNAL_AUTOPLUG_SORT], 0, dpad, caps, factories,
1105       &result);
1106   g_value_array_free (factories);
1107   factories = result;
1108
1109   /* 1.e else continue autoplugging something from the list. */
1110   GST_LOG_OBJECT (pad, "Let's continue discovery on this pad");
1111   connect_pad (dbin, src, dpad, pad, caps, factories, chain);
1112
1113   gst_object_unref (dpad);
1114   g_value_array_free (factories);
1115
1116   return;
1117
1118 expose_pad:
1119   {
1120     GST_LOG_OBJECT (dbin, "Pad is final. autoplug-continue:%d", apcontinue);
1121     expose_pad (dbin, src, dpad, pad, chain);
1122     gst_object_unref (dpad);
1123     return;
1124   }
1125 unknown_type:
1126   {
1127     GST_LOG_OBJECT (pad, "Unknown type, firing signal");
1128     g_signal_emit (G_OBJECT (dbin),
1129         gst_decode_bin_signals[SIGNAL_UNKNOWN_TYPE], 0, pad, caps);
1130
1131     chain->deadend = TRUE;
1132
1133     gst_element_post_message (GST_ELEMENT_CAST (dbin),
1134         gst_missing_decoder_message_new (GST_ELEMENT_CAST (dbin), caps));
1135
1136     /* Try to expose anything */
1137     EXPOSE_LOCK (dbin);
1138     if (gst_decode_chain_is_complete (dbin->decode_chain)) {
1139       gst_decode_bin_expose (dbin);
1140     }
1141     EXPOSE_UNLOCK (dbin);
1142     do_async_done (dbin);
1143
1144     if (src == dbin->typefind) {
1145       gchar *desc;
1146
1147       if (caps && !gst_caps_is_empty (caps)) {
1148         desc = gst_pb_utils_get_decoder_description (caps);
1149         GST_ELEMENT_ERROR (dbin, STREAM, CODEC_NOT_FOUND,
1150             (_("A %s plugin is required to play this stream, "
1151                     "but not installed."), desc),
1152             ("No decoder to handle media type '%s'",
1153                 gst_structure_get_name (gst_caps_get_structure (caps, 0))));
1154         g_free (desc);
1155       } else {
1156         GST_ELEMENT_ERROR (dbin, STREAM, TYPE_NOT_FOUND,
1157             (_("Could not determine type of stream")),
1158             ("Stream caps %" GST_PTR_FORMAT, caps));
1159       }
1160     }
1161     return;
1162   }
1163 non_fixed:
1164   {
1165     GST_DEBUG_OBJECT (pad, "pad has non-fixed caps delay autoplugging");
1166     gst_object_unref (dpad);
1167     goto setup_caps_delay;
1168   }
1169 any_caps:
1170   {
1171     GST_WARNING_OBJECT (pad,
1172         "pad has ANY caps, not able to autoplug to anything");
1173     goto setup_caps_delay;
1174   }
1175 setup_caps_delay:
1176   {
1177     /* connect to caps notification */
1178     CHAIN_MUTEX_LOCK (chain);
1179     GST_LOG_OBJECT (dbin, "Chain %p has now %d dynamic pads", chain,
1180         g_list_length (chain->pending_pads));
1181     chain->pending_pads =
1182         g_list_prepend (chain->pending_pads, gst_object_ref (pad));
1183     CHAIN_MUTEX_UNLOCK (chain);
1184     g_signal_connect (G_OBJECT (pad), "notify::caps",
1185         G_CALLBACK (caps_notify_cb), chain);
1186     return;
1187   }
1188 }
1189
1190
1191 /* connect_pad:
1192  *
1193  * Try to connect the given pad to an element created from one of the factories,
1194  * and recursively.
1195  *
1196  * Note that dpad is ghosting pad, and so pad is linked; be sure to unset dpad's
1197  * target before trying to link pad.
1198  *
1199  * Returns TRUE if an element was properly created and linked
1200  */
1201 static gboolean
1202 connect_pad (GstDecodeBin * dbin, GstElement * src, GstDecodePad * dpad,
1203     GstPad * pad, GstCaps * caps, GValueArray * factories,
1204     GstDecodeChain * chain)
1205 {
1206   gboolean res = FALSE;
1207   GstPad *mqpad = NULL;
1208   gboolean is_demuxer = chain->parent && !chain->elements;      /* First pad after the demuxer */
1209
1210   g_return_val_if_fail (factories != NULL, FALSE);
1211   g_return_val_if_fail (factories->n_values > 0, FALSE);
1212
1213   GST_DEBUG_OBJECT (dbin, "pad %s:%s , chain:%p",
1214       GST_DEBUG_PAD_NAME (pad), chain);
1215
1216   /* 1. is element demuxer or parser */
1217   if (is_demuxer) {
1218     GST_LOG_OBJECT (src,
1219         "is a demuxer, connecting the pad through multiqueue '%s'",
1220         GST_OBJECT_NAME (chain->parent->multiqueue));
1221
1222     gst_ghost_pad_set_target (GST_GHOST_PAD (dpad), NULL);
1223     if (!(mqpad = gst_decode_group_control_demuxer_pad (chain->parent, pad)))
1224       goto beach;
1225     src = chain->parent->multiqueue;
1226     pad = mqpad;
1227     gst_ghost_pad_set_target (GST_GHOST_PAD (dpad), pad);
1228   }
1229
1230   /* 2. Try to create an element and link to it */
1231   while (factories->n_values > 0) {
1232     GstAutoplugSelectResult ret;
1233     GstElementFactory *factory;
1234     GstElement *element;
1235     GstPad *sinkpad;
1236     gboolean subtitle;
1237
1238     /* Set dpad target to pad again, it might've been unset
1239      * below but we came back here because something failed
1240      */
1241     gst_ghost_pad_set_target (GST_GHOST_PAD (dpad), pad);
1242
1243     /* take first factory */
1244     factory = g_value_get_object (g_value_array_get_nth (factories, 0));
1245     /* Remove selected factory from the list. */
1246     g_value_array_remove (factories, 0);
1247
1248     /* emit autoplug-select to see what we should do with it. */
1249     g_signal_emit (G_OBJECT (dbin),
1250         gst_decode_bin_signals[SIGNAL_AUTOPLUG_SELECT],
1251         0, dpad, caps, factory, &ret);
1252
1253     switch (ret) {
1254       case GST_AUTOPLUG_SELECT_TRY:
1255         GST_DEBUG_OBJECT (dbin, "autoplug select requested try");
1256         break;
1257       case GST_AUTOPLUG_SELECT_EXPOSE:
1258         GST_DEBUG_OBJECT (dbin, "autoplug select requested expose");
1259         /* expose the pad, we don't have the source element */
1260         expose_pad (dbin, src, dpad, pad, chain);
1261         res = TRUE;
1262         goto beach;
1263       case GST_AUTOPLUG_SELECT_SKIP:
1264         GST_DEBUG_OBJECT (dbin, "autoplug select requested skip");
1265         continue;
1266       default:
1267         GST_WARNING_OBJECT (dbin, "autoplug select returned unhandled %d", ret);
1268         break;
1269     }
1270
1271     /* 2.0. Unlink pad */
1272     gst_ghost_pad_set_target (GST_GHOST_PAD (dpad), NULL);
1273
1274     /* 2.1. Try to create an element */
1275     if ((element = gst_element_factory_create (factory, NULL)) == NULL) {
1276       GST_WARNING_OBJECT (dbin, "Could not create an element from %s",
1277           gst_plugin_feature_get_name (GST_PLUGIN_FEATURE (factory)));
1278       continue;
1279     }
1280
1281     /* ... activate it ... We do this before adding it to the bin so that we
1282      * don't accidentally make it post error messages that will stop
1283      * everything. */
1284     if ((gst_element_set_state (element,
1285                 GST_STATE_READY)) == GST_STATE_CHANGE_FAILURE) {
1286       GST_WARNING_OBJECT (dbin, "Couldn't set %s to READY",
1287           GST_ELEMENT_NAME (element));
1288       gst_object_unref (element);
1289       continue;
1290     }
1291
1292     /* 2.3. Find its sink pad, this should work after activating it. */
1293     if (!(sinkpad = find_sink_pad (element))) {
1294       GST_WARNING_OBJECT (dbin, "Element %s doesn't have a sink pad",
1295           GST_ELEMENT_NAME (element));
1296       gst_element_set_state (element, GST_STATE_NULL);
1297       gst_object_unref (element);
1298       continue;
1299     }
1300
1301     /* 2.4 add it ... */
1302     if (!(gst_bin_add (GST_BIN_CAST (dbin), element))) {
1303       GST_WARNING_OBJECT (dbin, "Couldn't add %s to the bin",
1304           GST_ELEMENT_NAME (element));
1305       gst_object_unref (sinkpad);
1306       gst_element_set_state (element, GST_STATE_NULL);
1307       gst_object_unref (element);
1308       continue;
1309     }
1310
1311     /* 2.5 ...and try to link */
1312     if ((gst_pad_link (pad, sinkpad)) != GST_PAD_LINK_OK) {
1313       GST_WARNING_OBJECT (dbin, "Link failed on pad %s:%s",
1314           GST_DEBUG_PAD_NAME (sinkpad));
1315       gst_element_set_state (element, GST_STATE_NULL);
1316       gst_object_unref (sinkpad);
1317       gst_bin_remove (GST_BIN (dbin), element);
1318       continue;
1319     }
1320     gst_object_unref (sinkpad);
1321     GST_LOG_OBJECT (dbin, "linked on pad %s:%s", GST_DEBUG_PAD_NAME (pad));
1322
1323     CHAIN_MUTEX_LOCK (chain);
1324     chain->elements =
1325         g_list_prepend (chain->elements, gst_object_ref (element));
1326     chain->demuxer = is_demuxer_element (element);
1327     CHAIN_MUTEX_UNLOCK (chain);
1328
1329     /* link this element further */
1330     connect_element (dbin, element, chain);
1331
1332     /* try to configure the subtitle encoding property when we can */
1333     if (g_object_class_find_property (G_OBJECT_GET_CLASS (element),
1334             "subtitle-encoding")) {
1335       GST_DEBUG_OBJECT (dbin,
1336           "setting subtitle-encoding=%s to element", dbin->encoding);
1337       g_object_set (G_OBJECT (element), "subtitle-encoding", dbin->encoding,
1338           NULL);
1339       subtitle = TRUE;
1340     } else
1341       subtitle = FALSE;
1342
1343     /* Bring the element to the state of the parent */
1344     if ((gst_element_set_state (element,
1345                 GST_STATE_PAUSED)) == GST_STATE_CHANGE_FAILURE) {
1346       GstElement *tmp = NULL;
1347
1348       GST_WARNING_OBJECT (dbin, "Couldn't set %s to PAUSED",
1349           GST_ELEMENT_NAME (element));
1350
1351       /* Remove all elements in this chain that were just added. No
1352        * other thread could've added elements in the meantime */
1353       CHAIN_MUTEX_LOCK (chain);
1354       do {
1355         tmp = chain->elements->data;
1356         gst_element_set_state (tmp, GST_STATE_NULL);
1357         gst_bin_remove (GST_BIN (dbin), tmp);
1358         chain->elements = g_list_delete_link (chain->elements, chain->elements);
1359       } while (tmp != element);
1360       CHAIN_MUTEX_UNLOCK (chain);
1361
1362       continue;
1363     }
1364     if (subtitle) {
1365       GST_OBJECT_LOCK (dbin);
1366       /* we added the element now, add it to the list of subtitle-encoding
1367        * elements when we can set the property */
1368       dbin->subtitles = g_list_prepend (dbin->subtitles, element);
1369       GST_OBJECT_UNLOCK (dbin);
1370     }
1371
1372     res = TRUE;
1373     break;
1374   }
1375
1376 beach:
1377   if (mqpad)
1378     gst_object_unref (mqpad);
1379
1380   return res;
1381 }
1382
1383 static gboolean
1384 connect_element (GstDecodeBin * dbin, GstElement * element,
1385     GstDecodeChain * chain)
1386 {
1387   GList *pads;
1388   gboolean res = TRUE;
1389   gboolean dynamic = FALSE;
1390   GList *to_connect = NULL;
1391
1392   GST_DEBUG_OBJECT (dbin, "Attempting to connect element %s [chain:%p] further",
1393       GST_ELEMENT_NAME (element), chain);
1394
1395   /* 1. Loop over pad templates, grabbing existing pads along the way */
1396   for (pads = GST_ELEMENT_GET_CLASS (element)->padtemplates; pads;
1397       pads = g_list_next (pads)) {
1398     GstPadTemplate *templ = GST_PAD_TEMPLATE (pads->data);
1399     const gchar *templ_name;
1400
1401     /* we are only interested in source pads */
1402     if (GST_PAD_TEMPLATE_DIRECTION (templ) != GST_PAD_SRC)
1403       continue;
1404
1405     templ_name = GST_PAD_TEMPLATE_NAME_TEMPLATE (templ);
1406     GST_DEBUG_OBJECT (dbin, "got a source pad template %s", templ_name);
1407
1408     /* figure out what kind of pad this is */
1409     switch (GST_PAD_TEMPLATE_PRESENCE (templ)) {
1410       case GST_PAD_ALWAYS:
1411       {
1412         /* get the pad that we need to autoplug */
1413         GstPad *pad = gst_element_get_static_pad (element, templ_name);
1414
1415         if (pad) {
1416           GST_DEBUG_OBJECT (dbin, "got the pad for always template %s",
1417               templ_name);
1418           /* here is the pad, we need to autoplug it */
1419           to_connect = g_list_prepend (to_connect, pad);
1420         } else {
1421           /* strange, pad is marked as always but it's not
1422            * there. Fix the element */
1423           GST_WARNING_OBJECT (dbin,
1424               "could not get the pad for always template %s", templ_name);
1425         }
1426         break;
1427       }
1428       case GST_PAD_SOMETIMES:
1429       {
1430         /* try to get the pad to see if it is already created or
1431          * not */
1432         GstPad *pad = gst_element_get_static_pad (element, templ_name);
1433
1434         if (pad) {
1435           GST_DEBUG_OBJECT (dbin, "got the pad for sometimes template %s",
1436               templ_name);
1437           /* the pad is created, we need to autoplug it */
1438           to_connect = g_list_prepend (to_connect, pad);
1439         } else {
1440           GST_DEBUG_OBJECT (dbin,
1441               "did not get the sometimes pad of template %s", templ_name);
1442           /* we have an element that will create dynamic pads */
1443           dynamic = TRUE;
1444         }
1445         break;
1446       }
1447       case GST_PAD_REQUEST:
1448         /* ignore request pads */
1449         GST_DEBUG_OBJECT (dbin, "ignoring request padtemplate %s", templ_name);
1450         break;
1451     }
1452   }
1453
1454   /* 2. if there are more potential pads, connect to relevant signals */
1455   if (dynamic) {
1456     GST_LOG_OBJECT (dbin, "Adding signals to element %s in chain %p",
1457         GST_ELEMENT_NAME (element), chain);
1458     g_signal_connect (G_OBJECT (element), "pad-added",
1459         G_CALLBACK (pad_added_cb), chain);
1460     g_signal_connect (G_OBJECT (element), "pad-removed",
1461         G_CALLBACK (pad_removed_cb), chain);
1462     g_signal_connect (G_OBJECT (element), "no-more-pads",
1463         G_CALLBACK (no_more_pads_cb), chain);
1464   }
1465
1466   /* 3. for every available pad, connect it */
1467   for (pads = to_connect; pads; pads = g_list_next (pads)) {
1468     GstPad *pad = GST_PAD_CAST (pads->data);
1469     GstCaps *caps;
1470
1471     caps = gst_pad_get_caps (pad);
1472     analyze_new_pad (dbin, element, pad, caps, chain);
1473     if (caps)
1474       gst_caps_unref (caps);
1475
1476     gst_object_unref (pad);
1477   }
1478   g_list_free (to_connect);
1479
1480   return res;
1481 }
1482
1483 /* expose_pad:
1484  *
1485  * Expose the given pad on the chain as a decoded pad.
1486  */
1487 static void
1488 expose_pad (GstDecodeBin * dbin, GstElement * src, GstDecodePad * dpad,
1489     GstPad * pad, GstDecodeChain * chain)
1490 {
1491   GstPad *mqpad = NULL;
1492
1493   GST_DEBUG_OBJECT (dbin, "pad %s:%s, chain:%p",
1494       GST_DEBUG_PAD_NAME (pad), chain);
1495
1496   /* If this is the first pad for this chain, there are no other elements
1497    * and the source element is not the multiqueue we must link through the
1498    * multiqueue.
1499    *
1500    * This is the case if a demuxer directly exposed a raw pad.
1501    */
1502   if (chain->parent && !chain->elements && src != chain->parent->multiqueue) {
1503     GST_LOG_OBJECT (src, "connecting the pad through multiqueue");
1504
1505     gst_ghost_pad_set_target (GST_GHOST_PAD (dpad), NULL);
1506     if (!(mqpad = gst_decode_group_control_demuxer_pad (chain->parent, pad)))
1507       goto beach;
1508     pad = mqpad;
1509     gst_ghost_pad_set_target (GST_GHOST_PAD (dpad), pad);
1510   }
1511
1512   gst_decode_pad_activate (dpad, chain);
1513   chain->endpad = gst_object_ref (dpad);
1514
1515   EXPOSE_LOCK (dbin);
1516   if (gst_decode_chain_is_complete (dbin->decode_chain)) {
1517     gst_decode_bin_expose (dbin);
1518   }
1519   EXPOSE_UNLOCK (dbin);
1520
1521   if (mqpad)
1522     gst_object_unref (mqpad);
1523
1524 beach:
1525   return;
1526 }
1527
1528 static void
1529 type_found (GstElement * typefind, guint probability,
1530     GstCaps * caps, GstDecodeBin * decode_bin)
1531 {
1532   GstPad *pad;
1533
1534   GST_DEBUG_OBJECT (decode_bin, "typefind found caps %" GST_PTR_FORMAT, caps);
1535
1536   /* If the typefinder (but not something else) finds text/plain - i.e. that's
1537    * the top-level type of the file - then error out.
1538    */
1539   if (gst_structure_has_name (gst_caps_get_structure (caps, 0), "text/plain")) {
1540     GST_ELEMENT_ERROR (decode_bin, STREAM, WRONG_TYPE,
1541         (_("This appears to be a text file")),
1542         ("decodebin2 cannot decode plain text files"));
1543     goto exit;
1544   }
1545
1546   /* FIXME: we can only deal with one type, we don't yet support dynamically changing
1547    * caps from the typefind element */
1548   if (decode_bin->have_type || decode_bin->decode_chain)
1549     goto exit;
1550
1551   decode_bin->have_type = TRUE;
1552
1553   pad = gst_element_get_static_pad (typefind, "src");
1554
1555   decode_bin->decode_chain = gst_decode_chain_new (decode_bin, NULL);
1556   analyze_new_pad (decode_bin, typefind, pad, caps, decode_bin->decode_chain);
1557
1558   gst_object_unref (pad);
1559
1560 exit:
1561   return;
1562 }
1563
1564 static void
1565 pad_added_cb (GstElement * element, GstPad * pad, GstDecodeChain * chain)
1566 {
1567   GstCaps *caps;
1568   GstDecodeBin *dbin;
1569
1570   dbin = chain->dbin;
1571
1572   GST_DEBUG_OBJECT (pad, "pad added, chain:%p", chain);
1573
1574   caps = gst_pad_get_caps (pad);
1575   analyze_new_pad (dbin, element, pad, caps, chain);
1576   if (caps)
1577     gst_caps_unref (caps);
1578
1579   EXPOSE_LOCK (dbin);
1580   if (gst_decode_chain_is_complete (dbin->decode_chain)) {
1581     GST_LOG_OBJECT (dbin,
1582         "That was the last dynamic object, now attempting to expose the group");
1583     if (!gst_decode_bin_expose (dbin))
1584       GST_WARNING_OBJECT (dbin, "Couldn't expose group");
1585   }
1586   EXPOSE_UNLOCK (dbin);
1587 }
1588
1589 static void
1590 pad_removed_cb (GstElement * element, GstPad * pad, GstDecodeChain * chain)
1591 {
1592   GList *l;
1593
1594   GST_LOG_OBJECT (pad, "pad removed, chain:%p", chain);
1595
1596   /* In fact, we don't have to do anything here, the active group will be
1597    * removed when the group's multiqueue is drained */
1598   CHAIN_MUTEX_LOCK (chain);
1599   for (l = chain->pending_pads; l; l = l->next) {
1600     GstPad *opad = l->data;
1601
1602     if (pad == opad) {
1603       g_signal_handlers_disconnect_by_func (pad, caps_notify_cb, chain);
1604       gst_object_unref (pad);
1605       chain->pending_pads = g_list_delete_link (chain->pending_pads, l);
1606       break;
1607     }
1608   }
1609   CHAIN_MUTEX_UNLOCK (chain);
1610 }
1611
1612 static void
1613 no_more_pads_cb (GstElement * element, GstDecodeChain * chain)
1614 {
1615   GstDecodeGroup *group = NULL;
1616
1617   GST_LOG_OBJECT (element, "got no more pads");
1618
1619   if (!chain->elements || (GstElement *) chain->elements->data != element) {
1620     GST_LOG_OBJECT (chain->dbin, "no-more-pads from old chain element '%s'",
1621         GST_OBJECT_NAME (element));
1622     return;
1623   } else if (!chain->demuxer) {
1624     GST_LOG_OBJECT (chain->dbin, "no-more-pads from a non-demuxer element '%s'",
1625         GST_OBJECT_NAME (element));
1626     return;
1627   }
1628
1629   CHAIN_MUTEX_LOCK (chain);
1630   /* when we received no_more_pads, we can complete the pads of the chain */
1631   if (!chain->next_groups && chain->active_group) {
1632     group = chain->active_group;
1633   } else if (chain->next_groups) {
1634     group = chain->next_groups->data;
1635   }
1636   if (!group) {
1637     GST_ERROR_OBJECT (chain->dbin, "can't find group for element");
1638     CHAIN_MUTEX_UNLOCK (chain);
1639     return;
1640   }
1641
1642   GST_DEBUG_OBJECT (element, "Setting group %p to complete", group);
1643
1644   group->no_more_pads = TRUE;
1645   CHAIN_MUTEX_UNLOCK (chain);
1646
1647   EXPOSE_LOCK (chain->dbin);
1648   if (gst_decode_chain_is_complete (chain->dbin->decode_chain)) {
1649     gst_decode_bin_expose (chain->dbin);
1650   }
1651   EXPOSE_UNLOCK (chain->dbin);
1652 }
1653
1654 static void
1655 caps_notify_cb (GstPad * pad, GParamSpec * unused, GstDecodeChain * chain)
1656 {
1657   GstElement *element;
1658   GList *l;
1659
1660   GST_LOG_OBJECT (pad, "Notified caps for pad %s:%s", GST_DEBUG_PAD_NAME (pad));
1661
1662   /* Disconnect this; if we still need it, we'll reconnect to this in
1663    * analyze_new_pad */
1664   g_signal_handlers_disconnect_by_func (pad, caps_notify_cb, chain);
1665
1666   element = GST_ELEMENT_CAST (gst_pad_get_parent (pad));
1667
1668   CHAIN_MUTEX_LOCK (chain);
1669   for (l = chain->pending_pads; l; l = l->next) {
1670     if (l->data == pad) {
1671       gst_object_unref (GST_OBJECT_CAST (l->data));
1672       chain->pending_pads = g_list_delete_link (chain->pending_pads, l);
1673       break;
1674     }
1675   }
1676   CHAIN_MUTEX_UNLOCK (chain);
1677
1678   pad_added_cb (element, pad, chain);
1679
1680   gst_object_unref (element);
1681 }
1682
1683 /* Decide whether an element is a demuxer based on the 
1684  * klass and number/type of src pad templates it has */
1685 static gboolean
1686 is_demuxer_element (GstElement * srcelement)
1687 {
1688   GstElementFactory *srcfactory;
1689   GstElementClass *elemclass;
1690   GList *walk;
1691   const gchar *klass;
1692   gint potential_src_pads = 0;
1693
1694   srcfactory = gst_element_get_factory (srcelement);
1695   klass = gst_element_factory_get_klass (srcfactory);
1696
1697   /* Can't be a demuxer unless it has Demux in the klass name */
1698   if (!strstr (klass, "Demux"))
1699     return FALSE;
1700
1701   /* Walk the src pad templates and count how many the element
1702    * might produce */
1703   elemclass = GST_ELEMENT_GET_CLASS (srcelement);
1704
1705   walk = gst_element_class_get_pad_template_list (elemclass);
1706   while (walk != NULL) {
1707     GstPadTemplate *templ;
1708
1709     templ = (GstPadTemplate *) walk->data;
1710     if (GST_PAD_TEMPLATE_DIRECTION (templ) == GST_PAD_SRC) {
1711       switch (GST_PAD_TEMPLATE_PRESENCE (templ)) {
1712         case GST_PAD_ALWAYS:
1713         case GST_PAD_SOMETIMES:
1714           if (strstr (GST_PAD_TEMPLATE_NAME_TEMPLATE (templ), "%"))
1715             potential_src_pads += 2;    /* Might make multiple pads */
1716           else
1717             potential_src_pads += 1;
1718           break;
1719         case GST_PAD_REQUEST:
1720           potential_src_pads += 2;
1721           break;
1722       }
1723     }
1724     walk = g_list_next (walk);
1725   }
1726
1727   if (potential_src_pads < 2)
1728     return FALSE;
1729
1730   return TRUE;
1731 }
1732
1733 /* Returns TRUE if the caps are raw, or if they are compatible with the caps 
1734  * specified in the 'caps' property 
1735  * 
1736  * The decodebin_lock should be taken !
1737  */
1738 static gboolean
1739 are_raw_caps (GstDecodeBin * dbin, GstCaps * caps)
1740 {
1741   gboolean res;
1742
1743   GST_LOG_OBJECT (dbin, "Checking with caps %" GST_PTR_FORMAT, caps);
1744
1745   /* lock for getting the caps */
1746   GST_OBJECT_LOCK (dbin);
1747   res = gst_caps_can_intersect (dbin->caps, caps);
1748   GST_OBJECT_UNLOCK (dbin);
1749
1750   GST_LOG_OBJECT (dbin, "Caps are %sfinal caps", res ? "" : "not ");
1751
1752   return res;
1753 }
1754
1755 /****
1756  * GstDecodeChain functions
1757  ****/
1758
1759 /* gst_decode_chain_get_current_group:
1760  *
1761  * Returns the current group of this chain, to which
1762  * new chains should be attached or NULL if the last
1763  * group didn't have no-more-pads.
1764  *
1765  * Not MT-safe: Call with parent chain lock!
1766  */
1767 static GstDecodeGroup *
1768 gst_decode_chain_get_current_group (GstDecodeChain * chain)
1769 {
1770   GstDecodeGroup *group;
1771
1772   if (!chain->next_groups && chain->active_group
1773       && chain->active_group->overrun && !chain->active_group->no_more_pads) {
1774     GST_WARNING_OBJECT (chain->dbin,
1775         "Currently active group %p is exposed"
1776         " and wants to add a new pad without having signaled no-more-pads",
1777         chain->active_group);
1778     return NULL;
1779   }
1780
1781   if (chain->next_groups && (group = chain->next_groups->data) && group->overrun
1782       && !group->no_more_pads) {
1783     GST_WARNING_OBJECT (chain->dbin,
1784         "Currently newest pending group %p "
1785         "had overflow but didn't signal no-more-pads", group);
1786     return NULL;
1787   }
1788
1789   /* Now we know that we can really return something useful */
1790   if (!chain->active_group) {
1791     chain->active_group = group = gst_decode_group_new (chain->dbin, chain);
1792   } else if (!chain->active_group->overrun
1793       && !chain->active_group->no_more_pads) {
1794     group = chain->active_group;
1795   } else if (chain->next_groups && (group = chain->next_groups->data)
1796       && !group->overrun && !group->no_more_pads) {
1797     /* group = chain->next_groups->data */
1798   } else {
1799     group = gst_decode_group_new (chain->dbin, chain);
1800     chain->next_groups = g_list_prepend (chain->next_groups, group);
1801   }
1802
1803   return group;
1804 }
1805
1806 static void gst_decode_group_free_internal (GstDecodeGroup * group,
1807     gboolean hide);
1808
1809 static void
1810 gst_decode_chain_free_internal (GstDecodeChain * chain, gboolean hide)
1811 {
1812   GList *l;
1813
1814   CHAIN_MUTEX_LOCK (chain);
1815
1816   GST_DEBUG_OBJECT (chain->dbin, "%s chain %p", (hide ? "Hiding" : "Freeing"),
1817       chain);
1818
1819   if (chain->active_group) {
1820     gst_decode_group_free_internal (chain->active_group, hide);
1821     if (!hide)
1822       chain->active_group = NULL;
1823   }
1824
1825   for (l = chain->next_groups; l; l = l->next) {
1826     gst_decode_group_free_internal ((GstDecodeGroup *) l->data, hide);
1827     if (!hide)
1828       l->data = NULL;
1829   }
1830   if (!hide) {
1831     g_list_free (chain->next_groups);
1832     chain->next_groups = NULL;
1833   }
1834
1835   if (!hide) {
1836     for (l = chain->old_groups; l; l = l->next) {
1837       GstDecodeGroup *group = l->data;
1838
1839       gst_decode_group_free (group);
1840     }
1841     g_list_free (chain->old_groups);
1842     chain->old_groups = NULL;
1843   }
1844
1845   for (l = chain->pending_pads; l; l = l->next) {
1846     GstPad *pad = GST_PAD (l->data);
1847
1848     g_signal_handlers_disconnect_by_func (pad, caps_notify_cb, chain);
1849     gst_object_unref (pad);
1850     l->data = NULL;
1851   }
1852   g_list_free (chain->pending_pads);
1853   chain->pending_pads = NULL;
1854
1855   for (l = chain->elements; l; l = l->next) {
1856     GstElement *element = GST_ELEMENT (l->data);
1857
1858     g_signal_handlers_disconnect_by_func (element, pad_added_cb, chain);
1859     g_signal_handlers_disconnect_by_func (element, pad_removed_cb, chain);
1860     g_signal_handlers_disconnect_by_func (element, no_more_pads_cb, chain);
1861
1862     if (GST_OBJECT_PARENT (element) == GST_OBJECT_CAST (chain->dbin))
1863       gst_bin_remove (GST_BIN_CAST (chain->dbin), element);
1864     if (!hide) {
1865       gst_element_set_state (element, GST_STATE_NULL);
1866     }
1867
1868     GST_OBJECT_LOCK (chain->dbin);
1869     /* remove possible subtitle element */
1870     chain->dbin->subtitles = g_list_remove (chain->dbin->subtitles, element);
1871     GST_OBJECT_UNLOCK (chain->dbin);
1872
1873     if (!hide) {
1874       gst_object_unref (element);
1875       l->data = NULL;
1876     }
1877   }
1878   if (!hide) {
1879     g_list_free (chain->elements);
1880     chain->elements = NULL;
1881   }
1882
1883   if (chain->endpad) {
1884     if (chain->endpad->exposed)
1885       gst_element_remove_pad (GST_ELEMENT_CAST (chain->dbin),
1886           GST_PAD_CAST (chain->endpad));
1887
1888     chain->endpad->exposed = FALSE;
1889     if (!hide) {
1890       gst_object_unref (chain->endpad);
1891       chain->endpad = NULL;
1892     }
1893   }
1894
1895   GST_DEBUG_OBJECT (chain->dbin, "%s chain %p", (hide ? "Hided" : "Freed"),
1896       chain);
1897   CHAIN_MUTEX_UNLOCK (chain);
1898   if (!hide) {
1899     g_mutex_free (chain->lock);
1900     g_slice_free (GstDecodeChain, chain);
1901   }
1902 }
1903
1904 /* gst_decode_chain_free:
1905  *
1906  * Completely frees and removes the chain and all
1907  * child groups from decodebin2.
1908  *
1909  * MT-safe, don't hold the chain lock or any child chain's lock
1910  * when calling this!
1911  */
1912 static void
1913 gst_decode_chain_free (GstDecodeChain * chain)
1914 {
1915   gst_decode_chain_free_internal (chain, FALSE);
1916 }
1917
1918 /* gst_decode_chain_new:
1919  *
1920  * Creates a new decode chain and initializes it.
1921  *
1922  * It's up to the caller to add it to the list of child chains of
1923  * a group!
1924  */
1925 static GstDecodeChain *
1926 gst_decode_chain_new (GstDecodeBin * dbin, GstDecodeGroup * parent)
1927 {
1928   GstDecodeChain *chain = g_slice_new0 (GstDecodeChain);
1929
1930   GST_DEBUG_OBJECT (dbin, "Creating new chain %p with parent group %p", chain,
1931       parent);
1932
1933   chain->dbin = dbin;
1934   chain->parent = parent;
1935   chain->lock = g_mutex_new ();
1936
1937   return chain;
1938 }
1939
1940 /****
1941  * GstDecodeGroup functions
1942  ****/
1943
1944 /* The overrun callback is used to expose groups that have not yet had their
1945  * no_more_pads called while the (large) multiqueue overflowed. When this
1946  * happens we must assume that the no_more_pads will not arrive anymore and we
1947  * must expose the pads that we have. 
1948  */
1949 static void
1950 multi_queue_overrun_cb (GstElement * queue, GstDecodeGroup * group)
1951 {
1952   GstDecodeBin *dbin;
1953
1954   dbin = group->dbin;
1955
1956   GST_LOG_OBJECT (dbin, "multiqueue '%s' (%p) is full", GST_OBJECT_NAME (queue),
1957       queue);
1958
1959   group->overrun = TRUE;
1960
1961   /* FIXME: We should make sure that everything gets exposed now
1962    * even if child chains are not complete because the will never
1963    * be complete! Ignore any non-complete chains when exposing
1964    * and never expose them later
1965    */
1966
1967   EXPOSE_LOCK (dbin);
1968   if (gst_decode_chain_is_complete (dbin->decode_chain)) {
1969     if (!gst_decode_bin_expose (dbin))
1970       GST_WARNING_OBJECT (dbin, "Couldn't expose group");
1971   }
1972   EXPOSE_UNLOCK (group->dbin);
1973 }
1974
1975 static void
1976 gst_decode_group_free_internal (GstDecodeGroup * group, gboolean hide)
1977 {
1978   GList *l;
1979
1980   GST_DEBUG_OBJECT (group->dbin, "%s group %p", (hide ? "Hiding" : "Freeing"),
1981       group);
1982   for (l = group->children; l; l = l->next) {
1983     GstDecodeChain *chain = (GstDecodeChain *) l->data;
1984
1985     gst_decode_chain_free_internal (chain, hide);
1986     if (!hide)
1987       l->data = NULL;
1988   }
1989   if (!hide) {
1990     g_list_free (group->children);
1991     group->children = NULL;
1992   }
1993
1994   if (!hide) {
1995     for (l = group->reqpads; l; l = l->next) {
1996       GstPad *pad = l->data;
1997
1998       gst_element_release_request_pad (group->multiqueue, pad);
1999       gst_object_unref (pad);
2000       l->data = NULL;
2001     }
2002     g_list_free (group->reqpads);
2003     group->reqpads = NULL;
2004   }
2005
2006   if (group->multiqueue) {
2007     if (group->overrunsig) {
2008       g_signal_handler_disconnect (group->multiqueue, group->overrunsig);
2009       group->overrunsig = 0;
2010     }
2011
2012     if (GST_OBJECT_PARENT (group->multiqueue) == GST_OBJECT_CAST (group->dbin))
2013       gst_bin_remove (GST_BIN_CAST (group->dbin), group->multiqueue);
2014     if (!hide) {
2015       gst_element_set_state (group->multiqueue, GST_STATE_NULL);
2016       gst_object_unref (group->multiqueue);
2017       group->multiqueue = NULL;
2018     }
2019   }
2020
2021   GST_DEBUG_OBJECT (group->dbin, "%s group %p", (hide ? "Hided" : "Freed"),
2022       group);
2023   if (!hide)
2024     g_slice_free (GstDecodeGroup, group);
2025 }
2026
2027 /* gst_decode_group_free:
2028  *
2029  * Completely frees and removes the decode group and all
2030  * it's children.
2031  *
2032  * Never call this from any streaming thread!
2033  *
2034  * Not MT-safe, call with parent's chain lock!
2035  */
2036 static void
2037 gst_decode_group_free (GstDecodeGroup * group)
2038 {
2039   gst_decode_group_free_internal (group, FALSE);
2040 }
2041
2042 /* gst_decode_group_hide:
2043  *
2044  * Hide the decode group only, this means that
2045  * all child endpads are removed from decodebin2
2046  * and all signals are unconnected.
2047  *
2048  * No element is set to NULL state and completely
2049  * unreffed here.
2050  *
2051  * Can be called from streaming threads.
2052  * 
2053  * Not MT-safe, call with parent's chain lock!
2054  */
2055 static void
2056 gst_decode_group_hide (GstDecodeGroup * group)
2057 {
2058   gst_decode_group_free_internal (group, TRUE);
2059 }
2060
2061 /* gst_decode_group_new:
2062  * @dbin: Parent decodebin
2063  * @parent: Parent chain or %NULL
2064  *
2065  * Creates a new GstDecodeGroup. It is up to the caller to add it to the list
2066  * of groups.
2067  */
2068 static GstDecodeGroup *
2069 gst_decode_group_new (GstDecodeBin * dbin, GstDecodeChain * parent)
2070 {
2071   GstDecodeGroup *group = g_slice_new0 (GstDecodeGroup);
2072   GstElement *mq;
2073
2074   GST_DEBUG_OBJECT (dbin, "Creating new group %p with parent chain %p", group,
2075       parent);
2076
2077   group->dbin = dbin;
2078   group->parent = parent;
2079
2080   mq = group->multiqueue = gst_element_factory_make ("multiqueue", NULL);
2081   if (G_UNLIKELY (!group->multiqueue)) {
2082     gst_element_post_message (GST_ELEMENT_CAST (dbin),
2083         gst_missing_element_message_new (GST_ELEMENT_CAST (dbin),
2084             "multiqueue"));
2085     GST_ELEMENT_ERROR (dbin, CORE, MISSING_PLUGIN, (NULL), ("no multiqueue!"));
2086     g_slice_free (GstDecodeGroup, group);
2087     return NULL;
2088   }
2089
2090   g_object_set (G_OBJECT (mq),
2091       "max-size-bytes", (guint) 2 * 1024 * 1024,
2092       "max-size-time", (guint64) 0, "max-size-buffers", (guint) 0, NULL);
2093
2094   group->overrunsig = g_signal_connect (G_OBJECT (mq), "overrun",
2095       G_CALLBACK (multi_queue_overrun_cb), group);
2096
2097   gst_bin_add (GST_BIN (dbin), gst_object_ref (mq));
2098   gst_element_set_state (mq, GST_STATE_PAUSED);
2099
2100   return group;
2101 }
2102
2103 /* gst_decode_group_control_demuxer_pad
2104  *
2105  * Adds a new demuxer srcpad to the given group.
2106  *
2107  * Returns the srcpad of the multiqueue corresponding the given pad.
2108  * Returns NULL if there was an error.
2109  */
2110 static GstPad *
2111 gst_decode_group_control_demuxer_pad (GstDecodeGroup * group, GstPad * pad)
2112 {
2113   GstDecodeBin *dbin;
2114   GstPad *srcpad, *sinkpad;
2115   GstIterator *it = NULL;
2116
2117   dbin = group->dbin;
2118
2119   GST_LOG_OBJECT (dbin, "group:%p pad %s:%s", group, GST_DEBUG_PAD_NAME (pad));
2120
2121   srcpad = NULL;
2122
2123   if (G_UNLIKELY (!group->multiqueue))
2124     return NULL;
2125
2126   if (!(sinkpad = gst_element_get_request_pad (group->multiqueue, "sink%d"))) {
2127     GST_ERROR_OBJECT (dbin, "Couldn't get sinkpad from multiqueue");
2128     return NULL;
2129   }
2130
2131   if ((gst_pad_link (pad, sinkpad) != GST_PAD_LINK_OK)) {
2132     GST_ERROR_OBJECT (dbin, "Couldn't link demuxer and multiqueue");
2133     goto error;
2134   }
2135
2136   it = gst_pad_iterate_internal_links (sinkpad);
2137
2138   if (!it || (gst_iterator_next (it, (gpointer) & srcpad)) != GST_ITERATOR_OK
2139       || srcpad == NULL) {
2140     GST_ERROR_OBJECT (dbin,
2141         "Couldn't get srcpad from multiqueue for sinkpad %" GST_PTR_FORMAT,
2142         sinkpad);
2143     goto error;
2144   }
2145
2146   CHAIN_MUTEX_LOCK (group->parent);
2147   group->reqpads = g_list_prepend (group->reqpads, gst_object_ref (sinkpad));
2148   CHAIN_MUTEX_UNLOCK (group->parent);
2149
2150 beach:
2151   if (it)
2152     gst_iterator_free (it);
2153   gst_object_unref (sinkpad);
2154   return srcpad;
2155
2156 error:
2157   gst_element_release_request_pad (group->multiqueue, sinkpad);
2158   goto beach;
2159 }
2160
2161 /* gst_decode_group_is_complete:
2162  *
2163  * Checks if the group is complete, this means that
2164  * a) overrun of the multiqueue or no-more-pads happened
2165  * b) all child chains are complete
2166  *
2167  * Not MT-safe, always call with decodebin expose lock
2168  */
2169 static gboolean
2170 gst_decode_group_is_complete (GstDecodeGroup * group)
2171 {
2172   GList *l;
2173   gboolean complete = TRUE;
2174
2175   if (!group->overrun && !group->no_more_pads) {
2176     complete = FALSE;
2177     goto out;
2178   }
2179
2180   for (l = group->children; l; l = l->next) {
2181     GstDecodeChain *chain = l->data;
2182
2183     if (!gst_decode_chain_is_complete (chain)) {
2184       complete = FALSE;
2185       goto out;
2186     }
2187   }
2188
2189 out:
2190   GST_DEBUG_OBJECT (group->dbin, "Group %p is complete: %d", group, complete);
2191   return complete;
2192 }
2193
2194 /* gst_decode_chain_is_complete:
2195  *
2196  * Returns TRUE if the chain is complete, this means either
2197  * a) This chain is a dead end, i.e. we have no suitable plugins
2198  * b) This chain ends in an endpad and this is blocked or exposed
2199  *
2200  * Not MT-safe, always call with decodebin expose lock
2201  */
2202 static gboolean
2203 gst_decode_chain_is_complete (GstDecodeChain * chain)
2204 {
2205   gboolean complete = FALSE;
2206
2207   if (chain->deadend) {
2208     complete = TRUE;
2209     goto out;
2210   }
2211
2212   if (chain->endpad && (chain->endpad->blocked || chain->endpad->exposed)) {
2213     complete = TRUE;
2214     goto out;
2215   }
2216
2217   if (chain->demuxer) {
2218     if (chain->active_group
2219         && gst_decode_group_is_complete (chain->active_group)) {
2220       complete = TRUE;
2221       goto out;
2222     }
2223   }
2224
2225 out:
2226   GST_DEBUG_OBJECT (chain->dbin, "Chain %p is complete: %d", chain, complete);
2227   return complete;
2228 }
2229
2230 /* check if the group is drained, meaning all pads have seen an EOS 
2231  * event.  */
2232 static void
2233 gst_decode_pad_handle_eos (GstDecodePad * pad)
2234 {
2235   GstDecodeChain *chain = pad->chain;
2236
2237   GST_LOG_OBJECT (pad->dbin, "chain : %p, pad %p", chain, pad);
2238   pad->drained = TRUE;
2239   gst_decode_chain_handle_eos (chain);
2240 }
2241
2242 /* gst_decode_chain_handle_eos:
2243  * 
2244  * Checks if there are next groups in any parent chain
2245  * to which we can switch or if everything is drained.
2246  *
2247  * If there are groups to switch to, hide the current active
2248  * one and expose the new one.
2249  *
2250  * MT-safe, don't call with chain lock!
2251  */
2252 static void
2253 gst_decode_chain_handle_eos (GstDecodeChain * eos_chain)
2254 {
2255   GstDecodeBin *dbin = eos_chain->dbin;
2256   GstDecodeGroup *group = eos_chain->parent;
2257   GstDecodeChain *chain = eos_chain;
2258   gboolean drained;
2259
2260   g_return_if_fail (eos_chain->endpad);
2261
2262   CHAIN_MUTEX_LOCK (chain);
2263   while ((group = chain->parent)) {
2264     CHAIN_MUTEX_UNLOCK (chain);
2265     chain = group->parent;
2266     CHAIN_MUTEX_LOCK (chain);
2267
2268     if (gst_decode_group_is_drained (group)) {
2269       continue;
2270     }
2271     break;
2272   }
2273
2274   drained = chain->active_group ?
2275       gst_decode_group_is_drained (chain->active_group) : TRUE;
2276
2277   /* Now either group == NULL and chain == dbin->decode_chain
2278    * or chain is the lowest chain that has a non-drained group */
2279   if (chain->active_group && drained && chain->next_groups) {
2280     GST_DEBUG_OBJECT (dbin, "Hiding current group %p", chain->active_group);
2281     gst_decode_group_hide (chain->active_group);
2282     chain->old_groups = g_list_prepend (chain->old_groups, chain->active_group);
2283     GST_DEBUG_OBJECT (dbin, "Switching to next group %p",
2284         chain->next_groups->data);
2285     chain->active_group = chain->next_groups->data;
2286     chain->next_groups =
2287         g_list_delete_link (chain->next_groups, chain->next_groups);
2288     CHAIN_MUTEX_UNLOCK (chain);
2289     EXPOSE_LOCK (dbin);
2290     if (gst_decode_chain_is_complete (dbin->decode_chain))
2291       gst_decode_bin_expose (dbin);
2292     EXPOSE_UNLOCK (dbin);
2293   } else if (!chain->active_group || drained) {
2294     g_assert (chain == dbin->decode_chain);
2295     CHAIN_MUTEX_UNLOCK (chain);
2296
2297     GST_LOG_OBJECT (dbin, "all groups drained, fire signal");
2298     g_signal_emit (G_OBJECT (dbin), gst_decode_bin_signals[SIGNAL_DRAINED], 0,
2299         NULL);
2300   } else {
2301     CHAIN_MUTEX_UNLOCK (chain);
2302     GST_DEBUG_OBJECT (dbin,
2303         "Current active group in chain %p is not drained yet", chain);
2304   }
2305 }
2306
2307 /* gst_decode_group_is_drained:
2308  *
2309  * Check is this group is drained and cache this result.
2310  * The group is drained if all child chains are drained.
2311  *
2312  * Not MT-safe, call with group->parent's lock */
2313 static gboolean
2314 gst_decode_group_is_drained (GstDecodeGroup * group)
2315 {
2316   GList *l;
2317   gboolean drained = TRUE;
2318
2319   if (group->drained) {
2320     drained = TRUE;
2321     goto out;
2322   }
2323
2324   for (l = group->children; l; l = l->next) {
2325     GstDecodeChain *chain = l->data;
2326
2327     CHAIN_MUTEX_LOCK (chain);
2328     if (!gst_decode_chain_is_drained (chain))
2329       drained = FALSE;
2330     CHAIN_MUTEX_UNLOCK (chain);
2331     if (!drained)
2332       goto out;
2333   }
2334   group->drained = drained;
2335
2336 out:
2337   GST_DEBUG_OBJECT (group->dbin, "Group %p is drained: %d", group, drained);
2338   return drained;
2339 }
2340
2341 /* gst_decode_chain_is_drained:
2342  *
2343  * Check is the chain is drained, which means that
2344  * either
2345  *
2346  * a) it's endpad is drained
2347  * b) there are no pending pads, the active group is drained
2348  *    and there are no next groups
2349  *
2350  * Not MT-safe, call with chain lock
2351  */
2352 static gboolean
2353 gst_decode_chain_is_drained (GstDecodeChain * chain)
2354 {
2355   gboolean drained = FALSE;
2356
2357   if (chain->endpad) {
2358     drained = chain->endpad->drained;
2359     goto out;
2360   }
2361
2362   if (chain->pending_pads) {
2363     drained = FALSE;
2364     goto out;
2365   }
2366
2367   if (chain->active_group && gst_decode_group_is_drained (chain->active_group)
2368       && !chain->next_groups) {
2369     drained = TRUE;
2370     goto out;
2371   }
2372
2373 out:
2374   GST_DEBUG_OBJECT (chain->dbin, "Chain %p is drained: %d", chain, drained);
2375   return drained;
2376 }
2377
2378 /* sort_end_pads:
2379  * GCompareFunc to use with lists of GstPad.
2380  * Sorts pads by mime type.
2381  * First video (raw, then non-raw), then audio (raw, then non-raw),
2382  * then others.
2383  *
2384  * Return: negative if a<b, 0 if a==b, positive if a>b
2385  */
2386 static gint
2387 sort_end_pads (GstDecodePad * da, GstDecodePad * db)
2388 {
2389   gint va, vb;
2390   GstCaps *capsa, *capsb;
2391   GstStructure *sa, *sb;
2392   const gchar *namea, *nameb;
2393
2394   capsa = gst_pad_get_caps (GST_PAD (da));
2395   capsb = gst_pad_get_caps (GST_PAD (db));
2396
2397   sa = gst_caps_get_structure ((const GstCaps *) capsa, 0);
2398   sb = gst_caps_get_structure ((const GstCaps *) capsb, 0);
2399
2400   namea = gst_structure_get_name (sa);
2401   nameb = gst_structure_get_name (sb);
2402
2403   if (g_strrstr (namea, "video/x-raw-"))
2404     va = 0;
2405   else if (g_strrstr (namea, "video/"))
2406     va = 1;
2407   else if (g_strrstr (namea, "audio/x-raw"))
2408     va = 2;
2409   else if (g_strrstr (namea, "audio/"))
2410     va = 3;
2411   else
2412     va = 4;
2413
2414   if (g_strrstr (nameb, "video/x-raw-"))
2415     vb = 0;
2416   else if (g_strrstr (nameb, "video/"))
2417     vb = 1;
2418   else if (g_strrstr (nameb, "audio/x-raw"))
2419     vb = 2;
2420   else if (g_strrstr (nameb, "audio/"))
2421     vb = 3;
2422   else
2423     vb = 4;
2424
2425   gst_caps_unref (capsa);
2426   gst_caps_unref (capsb);
2427
2428   return va - vb;
2429 }
2430
2431 /* Must only be called if the toplevel chain is complete and blocked! */
2432 /* Not MT-safe, call with decodebin expose lock! */
2433 static gboolean
2434 gst_decode_bin_expose (GstDecodeBin * dbin)
2435 {
2436   GList *tmp, *endpads = NULL;
2437   gboolean already_exposed = TRUE;
2438
2439   GST_DEBUG_OBJECT (dbin, "Exposing currently active chains/groups");
2440
2441   /* Get the pads that we're going to expose and mark things as exposed */
2442   if (!gst_decode_chain_expose (dbin->decode_chain, &endpads)) {
2443     g_list_foreach (endpads, (GFunc) gst_object_unref, NULL);
2444     g_list_free (endpads);
2445     GST_ERROR_OBJECT (dbin, "Broken chain/group tree");
2446     g_return_val_if_reached (FALSE);
2447     return FALSE;
2448   }
2449   if (endpads == NULL) {
2450     GST_WARNING_OBJECT (dbin, "No suitable plugins found");
2451     GST_ELEMENT_ERROR (dbin, CORE, MISSING_PLUGIN, (NULL),
2452         ("no suitable plugins found"));
2453     return FALSE;
2454   }
2455
2456   /* Check if this was called when everything was exposed already */
2457   for (tmp = endpads; tmp && already_exposed; tmp = tmp->next) {
2458     GstDecodePad *dpad = tmp->data;
2459
2460     already_exposed &= dpad->exposed;
2461     if (!already_exposed)
2462       break;
2463   }
2464   if (already_exposed) {
2465     GST_DEBUG_OBJECT (dbin, "Everything was exposed already!");
2466     g_list_foreach (endpads, (GFunc) gst_object_unref, NULL);
2467     g_list_free (endpads);
2468     return TRUE;
2469   }
2470
2471   /* Set all already exposed pads to blocked */
2472   for (tmp = endpads; tmp; tmp = tmp->next) {
2473     GstDecodePad *dpad = tmp->data;
2474
2475     if (dpad->exposed)
2476       gst_decode_pad_set_blocked (dpad, TRUE);
2477   }
2478
2479   /* re-order pads : video, then audio, then others */
2480   endpads = g_list_sort (endpads, (GCompareFunc) sort_end_pads);
2481
2482   /* Expose pads */
2483   for (tmp = endpads; tmp; tmp = tmp->next) {
2484     GstDecodePad *dpad = (GstDecodePad *) tmp->data;
2485     gchar *padname;
2486
2487     /* 1. rewrite name */
2488     padname = g_strdup_printf ("src%d", dbin->nbpads);
2489     dbin->nbpads++;
2490     GST_DEBUG_OBJECT (dbin, "About to expose dpad %s as %s",
2491         GST_OBJECT_NAME (dpad), padname);
2492     gst_object_set_name (GST_OBJECT (dpad), padname);
2493     g_free (padname);
2494
2495     /* 2. activate and add */
2496     if (!dpad->exposed
2497         && !gst_element_add_pad (GST_ELEMENT (dbin), GST_PAD (dpad))) {
2498       /* not really fatal, we can try to add the other pads */
2499       g_warning ("error adding pad to decodebin2");
2500       continue;
2501     }
2502     dpad->exposed = TRUE;
2503
2504     /* 3. emit signal */
2505     GST_DEBUG_OBJECT (dbin, "emitting new-decoded-pad");
2506     g_signal_emit (G_OBJECT (dbin),
2507         gst_decode_bin_signals[SIGNAL_NEW_DECODED_PAD], 0, dpad,
2508         (tmp->next == NULL));
2509     GST_DEBUG_OBJECT (dbin, "emitted new-decoded-pad");
2510   }
2511
2512   /* signal no-more-pads. This allows the application to hook stuff to the
2513    * exposed pads */
2514   GST_LOG_OBJECT (dbin, "signalling no-more-pads");
2515   gst_element_no_more_pads (GST_ELEMENT (dbin));
2516
2517   /* 4. Unblock internal pads. The application should have connected stuff now
2518    * so that streaming can continue. */
2519   for (tmp = endpads; tmp; tmp = tmp->next) {
2520     GstDecodePad *dpad = (GstDecodePad *) tmp->data;
2521
2522     GST_DEBUG_OBJECT (dpad, "unblocking");
2523     gst_decode_pad_unblock (dpad);
2524     GST_DEBUG_OBJECT (dpad, "unblocked");
2525     gst_object_unref (dpad);
2526   }
2527   g_list_free (endpads);
2528
2529   do_async_done (dbin);
2530   GST_DEBUG_OBJECT (dbin, "Exposed everything");
2531   return TRUE;
2532 }
2533
2534 /* gst_decode_chain_expose:
2535  *
2536  * Check if the chain can be exposed and add all endpads
2537  * to the endpads list.
2538  *
2539  * Also update the active group's multiqueue to the
2540  * runtime limits.
2541  *
2542  * Not MT-safe, call with decodebin expose lock! *
2543  */
2544 static gboolean
2545 gst_decode_chain_expose (GstDecodeChain * chain, GList ** endpads)
2546 {
2547   GstDecodeGroup *group;
2548   GList *l;
2549
2550   if (chain->deadend)
2551     return TRUE;
2552
2553   if (chain->endpad) {
2554     if (!chain->endpad->blocked && !chain->endpad->exposed)
2555       return FALSE;
2556     *endpads = g_list_prepend (*endpads, gst_object_ref (chain->endpad));
2557     return TRUE;
2558   }
2559
2560   group = chain->active_group;
2561   if (!group)
2562     return FALSE;
2563   if (!group->no_more_pads && !group->overrun)
2564     return FALSE;
2565
2566   /* update runtime limits. At runtime, we try to keep the amount of buffers
2567    * in the queues as low as possible (but at least 5 buffers). */
2568   g_object_set (G_OBJECT (group->multiqueue),
2569       "max-size-bytes", 2 * 1024 * 1024, "max-size-buffers", 5, NULL);
2570   /* we can now disconnect any overrun signal, which is used to expose the
2571    * group. */
2572   if (group->overrunsig) {
2573     GST_LOG_OBJECT (group->dbin, "Disconnecting overrun");
2574     g_signal_handler_disconnect (group->multiqueue, group->overrunsig);
2575     group->overrunsig = 0;
2576   }
2577
2578   for (l = group->children; l; l = l->next) {
2579     GstDecodeChain *childchain = l->data;
2580
2581     if (!gst_decode_chain_expose (childchain, endpads))
2582       return FALSE;
2583   }
2584
2585   return TRUE;
2586 }
2587
2588 /*************************
2589  * GstDecodePad functions
2590  *************************/
2591
2592 static void
2593 gst_decode_pad_class_init (GstDecodePadClass * klass)
2594 {
2595 }
2596
2597 static void
2598 gst_decode_pad_init (GstDecodePad * pad)
2599 {
2600   pad->chain = NULL;
2601   pad->blocked = FALSE;
2602   pad->exposed = FALSE;
2603   pad->drained = FALSE;
2604   gst_object_ref (pad);
2605   gst_object_sink (pad);
2606 }
2607
2608 static void
2609 source_pad_blocked_cb (GstPad * pad, gboolean blocked, GstDecodePad * dpad)
2610 {
2611   GstDecodeChain *chain;
2612   GstDecodeBin *dbin;
2613
2614   chain = dpad->chain;
2615   dbin = chain->dbin;
2616
2617   GST_LOG_OBJECT (dpad, "blocked:%d, dpad->chain:%p", blocked, chain);
2618
2619   dpad->blocked = blocked;
2620
2621   if (dpad->blocked) {
2622     EXPOSE_LOCK (dbin);
2623     if (gst_decode_chain_is_complete (dbin->decode_chain)) {
2624       if (!gst_decode_bin_expose (dbin))
2625         GST_WARNING_OBJECT (dbin, "Couldn't expose group");
2626     }
2627     EXPOSE_UNLOCK (dbin);
2628   }
2629 }
2630
2631 static gboolean
2632 source_pad_event_probe (GstPad * pad, GstEvent * event, GstDecodePad * dpad)
2633 {
2634   GST_LOG_OBJECT (pad, "%s dpad:%p", GST_EVENT_TYPE_NAME (event), dpad);
2635
2636   if (GST_EVENT_TYPE (event) == GST_EVENT_EOS) {
2637     GST_DEBUG_OBJECT (pad, "we received EOS");
2638
2639     /* Check if all pads are drained. If there is a next group to expose, we
2640      * will remove the ghostpad of the current group first, which unlinks the
2641      * peer and so drops the EOS. */
2642     gst_decode_pad_handle_eos (dpad);
2643   }
2644   /* never drop events */
2645   return TRUE;
2646 }
2647
2648 static void
2649 gst_decode_pad_set_blocked (GstDecodePad * dpad, gboolean blocked)
2650 {
2651   GstDecodeBin *dbin = dpad->dbin;
2652   GstPad *opad;
2653
2654   DYN_LOCK (dbin);
2655   opad = gst_ghost_pad_get_target (GST_GHOST_PAD_CAST (dpad));
2656   if (!opad)
2657     goto out;
2658
2659   gst_pad_set_blocked_async_full (opad, blocked,
2660       (GstPadBlockCallback) source_pad_blocked_cb, gst_object_ref (dpad),
2661       (GDestroyNotify) gst_object_unref);
2662   if (blocked) {
2663     if (dbin->shutdown) {
2664       /* deactivate to force flushing state to prevent NOT_LINKED errors */
2665       gst_pad_set_active (GST_PAD (dpad), FALSE);
2666     } else {
2667       gst_object_ref (dpad);
2668       dbin->blocked_pads = g_list_prepend (dbin->blocked_pads, dpad);
2669     }
2670   } else {
2671     GList *l;
2672
2673     if ((l = g_list_find (dbin->blocked_pads, dpad))) {
2674       gst_object_unref (dpad);
2675       dbin->blocked_pads = g_list_delete_link (dbin->blocked_pads, l);
2676     }
2677   }
2678   gst_object_unref (opad);
2679 out:
2680   DYN_UNLOCK (dbin);
2681 }
2682
2683 static void
2684 gst_decode_pad_add_drained_check (GstDecodePad * dpad)
2685 {
2686   gst_pad_add_event_probe (GST_PAD (dpad),
2687       G_CALLBACK (source_pad_event_probe), dpad);
2688 }
2689
2690 static void
2691 gst_decode_pad_activate (GstDecodePad * dpad, GstDecodeChain * chain)
2692 {
2693   g_return_if_fail (chain != NULL);
2694
2695   dpad->chain = chain;
2696   gst_pad_set_active (GST_PAD (dpad), TRUE);
2697   gst_decode_pad_set_blocked (dpad, TRUE);
2698   gst_decode_pad_add_drained_check (dpad);
2699 }
2700
2701 static void
2702 gst_decode_pad_unblock (GstDecodePad * dpad)
2703 {
2704   gst_decode_pad_set_blocked (dpad, FALSE);
2705 }
2706
2707 /*gst_decode_pad_new:
2708  *
2709  * Creates a new GstDecodePad for the given pad.
2710  */
2711 static GstDecodePad *
2712 gst_decode_pad_new (GstDecodeBin * dbin, GstPad * pad, GstDecodeChain * chain)
2713 {
2714   GstDecodePad *dpad;
2715
2716   dpad =
2717       g_object_new (GST_TYPE_DECODE_PAD, "direction", GST_PAD_DIRECTION (pad),
2718       NULL);
2719   gst_ghost_pad_construct (GST_GHOST_PAD (dpad));
2720   gst_ghost_pad_set_target (GST_GHOST_PAD (dpad), pad);
2721   dpad->chain = chain;
2722   dpad->dbin = dbin;
2723
2724   return dpad;
2725 }
2726
2727
2728 /*****
2729  * Element add/remove
2730  *****/
2731
2732 static void
2733 do_async_start (GstDecodeBin * dbin)
2734 {
2735   GstMessage *message;
2736
2737   dbin->async_pending = TRUE;
2738
2739   message = gst_message_new_async_start (GST_OBJECT_CAST (dbin), FALSE);
2740   parent_class->handle_message (GST_BIN_CAST (dbin), message);
2741 }
2742
2743 static void
2744 do_async_done (GstDecodeBin * dbin)
2745 {
2746   GstMessage *message;
2747
2748   if (dbin->async_pending) {
2749     message = gst_message_new_async_done (GST_OBJECT_CAST (dbin));
2750     parent_class->handle_message (GST_BIN_CAST (dbin), message);
2751
2752     dbin->async_pending = FALSE;
2753   }
2754 }
2755
2756 /*****
2757  * convenience functions
2758  *****/
2759
2760 /* find_sink_pad
2761  *
2762  * Returns the first sink pad of the given element, or NULL if it doesn't have
2763  * any.
2764  */
2765
2766 static GstPad *
2767 find_sink_pad (GstElement * element)
2768 {
2769   GstIterator *it;
2770   GstPad *pad = NULL;
2771   gpointer point;
2772
2773   it = gst_element_iterate_sink_pads (element);
2774
2775   if ((gst_iterator_next (it, &point)) == GST_ITERATOR_OK)
2776     pad = (GstPad *) point;
2777
2778   gst_iterator_free (it);
2779
2780   return pad;
2781 }
2782
2783 /* call with dyn_lock held */
2784 static void
2785 unblock_pads (GstDecodeBin * dbin)
2786 {
2787   GList *tmp;
2788
2789   for (tmp = dbin->blocked_pads; tmp; tmp = tmp->next) {
2790     GstDecodePad *dpad = (GstDecodePad *) tmp->data;
2791     GstPad *opad = gst_ghost_pad_get_target (GST_GHOST_PAD_CAST (dpad));
2792
2793     if (!opad)
2794       continue;
2795
2796     GST_DEBUG_OBJECT (dpad, "unblocking");
2797     gst_pad_set_blocked_async_full (opad, FALSE,
2798         (GstPadBlockCallback) source_pad_blocked_cb, gst_object_ref (dpad),
2799         (GDestroyNotify) gst_object_unref);
2800     /* make flushing, prevent NOT_LINKED */
2801     GST_PAD_SET_FLUSHING (GST_PAD (dpad));
2802     gst_object_unref (dpad);
2803     gst_object_unref (opad);
2804     GST_DEBUG_OBJECT (dpad, "unblocked");
2805   }
2806
2807   /* clear, no more blocked pads */
2808   g_list_free (dbin->blocked_pads);
2809   dbin->blocked_pads = NULL;
2810 }
2811
2812 static GstStateChangeReturn
2813 gst_decode_bin_change_state (GstElement * element, GstStateChange transition)
2814 {
2815   GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
2816   GstDecodeBin *dbin = GST_DECODE_BIN (element);
2817
2818   switch (transition) {
2819     case GST_STATE_CHANGE_NULL_TO_READY:
2820       if (dbin->typefind == NULL)
2821         goto missing_typefind;
2822       break;
2823     case GST_STATE_CHANGE_READY_TO_PAUSED:
2824       DYN_LOCK (dbin);
2825       GST_LOG_OBJECT (dbin, "clearing shutdown flag");
2826       dbin->shutdown = FALSE;
2827       DYN_UNLOCK (dbin);
2828       dbin->have_type = FALSE;
2829       ret = GST_STATE_CHANGE_ASYNC;
2830       do_async_start (dbin);
2831       break;
2832     case GST_STATE_CHANGE_PAUSED_TO_READY:
2833       DYN_LOCK (dbin);
2834       GST_LOG_OBJECT (dbin, "setting shutdown flag");
2835       dbin->shutdown = TRUE;
2836       unblock_pads (dbin);
2837       DYN_UNLOCK (dbin);
2838     default:
2839       break;
2840   }
2841
2842   {
2843     GstStateChangeReturn bret;
2844
2845     bret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
2846     if (G_UNLIKELY (bret == GST_STATE_CHANGE_FAILURE))
2847       goto activate_failed;
2848     else if (G_UNLIKELY (bret == GST_STATE_CHANGE_NO_PREROLL)) {
2849       do_async_done (dbin);
2850       ret = bret;
2851     }
2852   }
2853   switch (transition) {
2854     case GST_STATE_CHANGE_PAUSED_TO_READY:
2855       do_async_done (dbin);
2856       if (dbin->decode_chain) {
2857         gst_decode_chain_free (dbin->decode_chain);
2858         dbin->decode_chain = NULL;
2859       }
2860       break;
2861     case GST_STATE_CHANGE_READY_TO_NULL:
2862     default:
2863       break;
2864   }
2865
2866   return ret;
2867
2868 /* ERRORS */
2869 missing_typefind:
2870   {
2871     gst_element_post_message (element,
2872         gst_missing_element_message_new (element, "typefind"));
2873     GST_ELEMENT_ERROR (dbin, CORE, MISSING_PLUGIN, (NULL), ("no typefind!"));
2874     return GST_STATE_CHANGE_FAILURE;
2875   }
2876 activate_failed:
2877   {
2878     GST_DEBUG_OBJECT (element,
2879         "element failed to change states -- activation problem?");
2880     return GST_STATE_CHANGE_FAILURE;
2881   }
2882 }
2883
2884 gboolean
2885 gst_decode_bin_plugin_init (GstPlugin * plugin)
2886 {
2887   GST_DEBUG_CATEGORY_INIT (gst_decode_bin_debug, "decodebin2", 0,
2888       "decoder bin");
2889
2890 #ifdef ENABLE_NLS
2891   GST_DEBUG ("binding text domain %s to locale dir %s", GETTEXT_PACKAGE,
2892       LOCALEDIR);
2893   bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
2894   bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
2895 #endif /* ENABLE_NLS */
2896
2897   return gst_element_register (plugin, "decodebin2", GST_RANK_NONE,
2898       GST_TYPE_DECODE_BIN);
2899 }