uridecodebin: remove commented code
[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  * Copyright (C) <2011> Hewlett-Packard Development Company, L.P.
5  *   Author: Sebastian Dröge <sebastian.droege@collabora.co.uk>, Collabora Ltd.
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public
18  * License along with this library; if not, write to the
19  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
20  * Boston, MA 02110-1301, USA.
21  */
22
23 /**
24  * SECTION:element-decodebin
25  *
26  * #GstBin that auto-magically constructs a decoding pipeline using available
27  * decoders and demuxers via auto-plugging.
28  *
29  * decodebin is considered stable now and replaces the old #decodebin element.
30  * #uridecodebin uses decodebin internally and is often more convenient to
31  * use, as it creates a suitable source element as well.
32  */
33
34 /* Implementation notes:
35  *
36  * The following section describes how decodebin works internally.
37  *
38  * The first part of decodebin is its typefind element, which tries
39  * to determine the media type of the input stream. If the type is found
40  * autoplugging starts.
41  *
42  * decodebin 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 decodebin 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 on the other hand is complete if all child
60  * chains are complete.
61  *
62  * If this happens at some point, all endpads of all active groups are exposed.
63  * For this decodebin adds the endpads, signals no-more-pads and then unblocks
64  * them. Now playback starts.
65  *
66  * If one of the chains that end on a endpad receives EOS decodebin checks
67  * if all chains and groups are drained. In that case everything goes into EOS.
68  * If there is a chain where the active group is drained but there exist next
69  * groups, the active group is hidden (endpads are removed) and the next group
70  * is exposed. This means that in some cases more pads may be created even
71  * after the initial no-more-pads signal. This happens for example with
72  * so-called "chained oggs", most commonly found among ogg/vorbis internet
73  * radio streams.
74  *
75  * Note 1: If we're talking about blocked endpads this really means that the
76  * *target* pads of the endpads are blocked. Pads that are exposed to the outside
77  * should never ever be blocked!
78  *
79  * Note 2: If a group is complete and the parent's chain demuxer adds new pads
80  * but never signaled no-more-pads this additional pads will be ignored!
81  *
82  */
83
84 /* FIXME 0.11: suppress warnings for deprecated API such as GValueArray
85  * with newer GLib versions (>= 2.31.0) */
86 #define GLIB_DISABLE_DEPRECATION_WARNINGS
87
88 #ifdef HAVE_CONFIG_H
89 #include "config.h"
90 #endif
91
92 #include <gst/gst-i18n-plugin.h>
93
94 #include <string.h>
95 #include <gst/gst.h>
96 #include <gst/pbutils/pbutils.h>
97
98 #include "gstplay-enum.h"
99 #include "gstplayback.h"
100 #include "gstrawcaps.h"
101
102 /* Also used by gsturidecodebin.c */
103 gint _decode_bin_compare_factories_func (gconstpointer p1, gconstpointer p2);
104
105 /* generic templates */
106 static GstStaticPadTemplate decoder_bin_sink_template =
107 GST_STATIC_PAD_TEMPLATE ("sink",
108     GST_PAD_SINK,
109     GST_PAD_ALWAYS,
110     GST_STATIC_CAPS_ANY);
111
112 static GstStaticPadTemplate decoder_bin_src_template =
113 GST_STATIC_PAD_TEMPLATE ("src_%u",
114     GST_PAD_SRC,
115     GST_PAD_SOMETIMES,
116     GST_STATIC_CAPS_ANY);
117
118 GST_DEBUG_CATEGORY_STATIC (gst_decode_bin_debug);
119 #define GST_CAT_DEFAULT gst_decode_bin_debug
120
121 typedef struct _GstPendingPad GstPendingPad;
122 typedef struct _GstDecodeElement GstDecodeElement;
123 typedef struct _GstDecodeChain GstDecodeChain;
124 typedef struct _GstDecodeGroup GstDecodeGroup;
125 typedef struct _GstDecodePad GstDecodePad;
126 typedef GstGhostPadClass GstDecodePadClass;
127 typedef struct _GstDecodeBin GstDecodeBin;
128 typedef struct _GstDecodeBinClass GstDecodeBinClass;
129
130 #define GST_TYPE_DECODE_BIN             (gst_decode_bin_get_type())
131 #define GST_DECODE_BIN_CAST(obj)        ((GstDecodeBin*)(obj))
132 #define GST_DECODE_BIN(obj)             (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_DECODE_BIN,GstDecodeBin))
133 #define GST_DECODE_BIN_CLASS(klass)     (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_DECODE_BIN,GstDecodeBinClass))
134 #define GST_IS_DECODE_BIN(obj)          (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_DECODE_BIN))
135 #define GST_IS_DECODE_BIN_CLASS(klass)  (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_DECODE_BIN))
136
137 /**
138  *  GstDecodeBin:
139  *
140  *  The opaque #GstDecodeBin data structure
141  */
142 struct _GstDecodeBin
143 {
144   GstBin bin;                   /* we extend GstBin */
145
146   /* properties */
147   GstCaps *caps;                /* caps on which to stop decoding */
148   gchar *encoding;              /* encoding of subtitles */
149   gboolean use_buffering;       /* configure buffering on multiqueues */
150   gint low_percent;
151   gint high_percent;
152   guint max_size_bytes;
153   guint max_size_buffers;
154   guint64 max_size_time;
155   gboolean post_stream_topology;
156   guint64 connection_speed;
157
158   GstElement *typefind;         /* this holds the typefind object */
159
160   GMutex expose_lock;           /* Protects exposal and removal of groups */
161   GstDecodeChain *decode_chain; /* Top level decode chain */
162   guint nbpads;                 /* unique identifier for source pads */
163
164   GMutex factories_lock;
165   guint32 factories_cookie;     /* Cookie from last time when factories was updated */
166   GList *factories;             /* factories we can use for selecting elements */
167
168   GMutex subtitle_lock;         /* Protects changes to subtitles and encoding */
169   GList *subtitles;             /* List of elements with subtitle-encoding,
170                                  * protected by above mutex! */
171
172   gboolean have_type;           /* if we received the have_type signal */
173   guint have_type_id;           /* signal id for have-type from typefind */
174
175   gboolean async_pending;       /* async-start has been emitted */
176
177   GMutex dyn_lock;              /* lock protecting pad blocking */
178   gboolean shutdown;            /* if we are shutting down */
179   GList *blocked_pads;          /* pads that have set to block */
180
181   gboolean expose_allstreams;   /* Whether to expose unknow type streams or not */
182
183   GList *filtered;              /* elements for which error messages are filtered */
184 };
185
186 struct _GstDecodeBinClass
187 {
188   GstBinClass parent_class;
189
190   /* signal fired when we found a pad that we cannot decode */
191   void (*unknown_type) (GstElement * element, GstPad * pad, GstCaps * caps);
192
193   /* signal fired to know if we continue trying to decode the given caps */
194     gboolean (*autoplug_continue) (GstElement * element, GstPad * pad,
195       GstCaps * caps);
196   /* signal fired to get a list of factories to try to autoplug */
197   GValueArray *(*autoplug_factories) (GstElement * element, GstPad * pad,
198       GstCaps * caps);
199   /* signal fired to sort the factories */
200   GValueArray *(*autoplug_sort) (GstElement * element, GstPad * pad,
201       GstCaps * caps, GValueArray * factories);
202   /* signal fired to select from the proposed list of factories */
203     GstAutoplugSelectResult (*autoplug_select) (GstElement * element,
204       GstPad * pad, GstCaps * caps, GstElementFactory * factory);
205
206   /* fired when the last group is drained */
207   void (*drained) (GstElement * element);
208 };
209
210 /* signals */
211 enum
212 {
213   SIGNAL_UNKNOWN_TYPE,
214   SIGNAL_AUTOPLUG_CONTINUE,
215   SIGNAL_AUTOPLUG_FACTORIES,
216   SIGNAL_AUTOPLUG_SELECT,
217   SIGNAL_AUTOPLUG_SORT,
218   SIGNAL_DRAINED,
219   LAST_SIGNAL
220 };
221
222 /* automatic sizes, while prerolling we buffer up to 2MB, we ignore time
223  * and buffers in this case. */
224 #define AUTO_PREROLL_SIZE_BYTES                  2 * 1024 * 1024
225 #define AUTO_PREROLL_SIZE_BUFFERS                0
226 #define AUTO_PREROLL_NOT_SEEKABLE_SIZE_TIME      10 * GST_SECOND
227 #define AUTO_PREROLL_SEEKABLE_SIZE_TIME          0
228
229 /* whan playing, keep a max of 2MB of data but try to keep the number of buffers
230  * as low as possible (try to aim for 5 buffers) */
231 #define AUTO_PLAY_SIZE_BYTES        2 * 1024 * 1024
232 #define AUTO_PLAY_SIZE_BUFFERS      5
233 #define AUTO_PLAY_SIZE_TIME         0
234
235 #define DEFAULT_SUBTITLE_ENCODING NULL
236 #define DEFAULT_USE_BUFFERING     FALSE
237 #define DEFAULT_LOW_PERCENT       10
238 #define DEFAULT_HIGH_PERCENT      99
239 /* by default we use the automatic values above */
240 #define DEFAULT_MAX_SIZE_BYTES    0
241 #define DEFAULT_MAX_SIZE_BUFFERS  0
242 #define DEFAULT_MAX_SIZE_TIME     0
243 #define DEFAULT_POST_STREAM_TOPOLOGY FALSE
244 #define DEFAULT_EXPOSE_ALL_STREAMS  TRUE
245 #define DEFAULT_CONNECTION_SPEED    0
246
247 /* Properties */
248 enum
249 {
250   PROP_0,
251   PROP_CAPS,
252   PROP_SUBTITLE_ENCODING,
253   PROP_SINK_CAPS,
254   PROP_USE_BUFFERING,
255   PROP_LOW_PERCENT,
256   PROP_HIGH_PERCENT,
257   PROP_MAX_SIZE_BYTES,
258   PROP_MAX_SIZE_BUFFERS,
259   PROP_MAX_SIZE_TIME,
260   PROP_POST_STREAM_TOPOLOGY,
261   PROP_EXPOSE_ALL_STREAMS,
262   PROP_CONNECTION_SPEED,
263   PROP_LAST
264 };
265
266 static GstBinClass *parent_class;
267 static guint gst_decode_bin_signals[LAST_SIGNAL] = { 0 };
268
269 static GstStaticCaps default_raw_caps = GST_STATIC_CAPS (DEFAULT_RAW_CAPS);
270
271 static void do_async_start (GstDecodeBin * dbin);
272 static void do_async_done (GstDecodeBin * dbin);
273
274 static void type_found (GstElement * typefind, guint probability,
275     GstCaps * caps, GstDecodeBin * decode_bin);
276
277 static void decodebin_set_queue_size (GstDecodeBin * dbin,
278     GstElement * multiqueue, gboolean preroll, gboolean seekable);
279
280 static gboolean gst_decode_bin_autoplug_continue (GstElement * element,
281     GstPad * pad, GstCaps * caps);
282 static GValueArray *gst_decode_bin_autoplug_factories (GstElement *
283     element, GstPad * pad, GstCaps * caps);
284 static GValueArray *gst_decode_bin_autoplug_sort (GstElement * element,
285     GstPad * pad, GstCaps * caps, GValueArray * factories);
286 static GstAutoplugSelectResult gst_decode_bin_autoplug_select (GstElement *
287     element, GstPad * pad, GstCaps * caps, GstElementFactory * factory);
288
289 static void gst_decode_bin_set_property (GObject * object, guint prop_id,
290     const GValue * value, GParamSpec * pspec);
291 static void gst_decode_bin_get_property (GObject * object, guint prop_id,
292     GValue * value, GParamSpec * pspec);
293 static void gst_decode_bin_set_caps (GstDecodeBin * dbin, GstCaps * caps);
294 static GstCaps *gst_decode_bin_get_caps (GstDecodeBin * dbin);
295 static void caps_notify_cb (GstPad * pad, GParamSpec * unused,
296     GstDecodeChain * chain);
297
298 static GstPad *find_sink_pad (GstElement * element);
299 static GstStateChangeReturn gst_decode_bin_change_state (GstElement * element,
300     GstStateChange transition);
301 static void gst_decode_bin_handle_message (GstBin * bin, GstMessage * message);
302
303 static gboolean check_upstream_seekable (GstDecodeBin * dbin, GstPad * pad);
304
305 #define EXPOSE_LOCK(dbin) G_STMT_START {                                \
306     GST_LOG_OBJECT (dbin,                                               \
307                     "expose locking from thread %p",                    \
308                     g_thread_self ());                                  \
309     g_mutex_lock (&GST_DECODE_BIN_CAST(dbin)->expose_lock);             \
310     GST_LOG_OBJECT (dbin,                                               \
311                     "expose locked from thread %p",                     \
312                     g_thread_self ());                                  \
313 } G_STMT_END
314
315 #define EXPOSE_UNLOCK(dbin) G_STMT_START {                              \
316     GST_LOG_OBJECT (dbin,                                               \
317                     "expose unlocking from thread %p",                  \
318                     g_thread_self ());                                  \
319     g_mutex_unlock (&GST_DECODE_BIN_CAST(dbin)->expose_lock);           \
320 } G_STMT_END
321
322 #define DYN_LOCK(dbin) G_STMT_START {                   \
323     GST_LOG_OBJECT (dbin,                                               \
324                     "dynlocking from thread %p",                        \
325                     g_thread_self ());                                  \
326     g_mutex_lock (&GST_DECODE_BIN_CAST(dbin)->dyn_lock);                        \
327     GST_LOG_OBJECT (dbin,                                               \
328                     "dynlocked from thread %p",                         \
329                     g_thread_self ());                                  \
330 } G_STMT_END
331
332 #define DYN_UNLOCK(dbin) G_STMT_START {                 \
333     GST_LOG_OBJECT (dbin,                                               \
334                     "dynunlocking from thread %p",                      \
335                     g_thread_self ());                                  \
336     g_mutex_unlock (&GST_DECODE_BIN_CAST(dbin)->dyn_lock);              \
337 } G_STMT_END
338
339 #define SUBTITLE_LOCK(dbin) G_STMT_START {                              \
340     GST_LOG_OBJECT (dbin,                                               \
341                     "subtitle locking from thread %p",                  \
342                     g_thread_self ());                                  \
343     g_mutex_lock (&GST_DECODE_BIN_CAST(dbin)->subtitle_lock);           \
344     GST_LOG_OBJECT (dbin,                                               \
345                     "subtitle lock from thread %p",                     \
346                     g_thread_self ());                                  \
347 } G_STMT_END
348
349 #define SUBTITLE_UNLOCK(dbin) G_STMT_START {                            \
350     GST_LOG_OBJECT (dbin,                                               \
351                     "subtitle unlocking from thread %p",                \
352                     g_thread_self ());                                  \
353     g_mutex_unlock (&GST_DECODE_BIN_CAST(dbin)->subtitle_lock);         \
354 } G_STMT_END
355
356 struct _GstPendingPad
357 {
358   GstPad *pad;
359   GstDecodeChain *chain;
360   gulong event_probe_id;
361 };
362
363 struct _GstDecodeElement
364 {
365   GstElement *element;
366   GstElement *capsfilter;       /* Optional capsfilter for Parser/Convert */
367 };
368
369 /* GstDecodeGroup
370  *
371  * Streams belonging to the same group/chain of a media file
372  *
373  * When changing something here lock the parent chain!
374  */
375 struct _GstDecodeGroup
376 {
377   GstDecodeBin *dbin;
378   GstDecodeChain *parent;
379
380   GstElement *multiqueue;       /* Used for linking all child chains */
381   gulong overrunsig;            /* the overrun signal for multiqueue */
382
383   gboolean overrun;             /* TRUE if the multiqueue signaled overrun. This
384                                  * means that we should really expose the group */
385
386   gboolean no_more_pads;        /* TRUE if the demuxer signaled no-more-pads */
387   gboolean drained;             /* TRUE if the all children are drained */
388
389   GList *children;              /* List of GstDecodeChains in this group */
390
391   GList *reqpads;               /* List of RequestPads for multiqueue, there is
392                                  * exactly one RequestPad per child chain */
393 };
394
395 struct _GstDecodeChain
396 {
397   GstDecodeGroup *parent;
398   GstDecodeBin *dbin;
399
400   GMutex lock;                  /* Protects this chain and its groups */
401
402   GstPad *pad;                  /* srcpad that caused creation of this chain */
403
404   gboolean drained;             /* TRUE if the all children are drained */
405   gboolean demuxer;             /* TRUE if elements->data is a demuxer */
406   gboolean seekable;            /* TRUE if this chain ends on a demuxer and is seekable */
407   GList *elements;              /* All elements in this group, first
408                                    is the latest and most downstream element */
409
410   /* Note: there are only groups if the last element of this chain
411    * is a demuxer, otherwise the chain will end with an endpad.
412    * The other way around this means, that endpad only exists if this
413    * chain doesn't end with a demuxer! */
414
415   GstDecodeGroup *active_group; /* Currently active group */
416   GList *next_groups;           /* head is newest group, tail is next group.
417                                    a new group will be created only if the head
418                                    group had no-more-pads. If it's only exposed
419                                    all new pads will be ignored! */
420   GList *pending_pads;          /* Pads that have no fixed caps yet */
421
422   GstDecodePad *endpad;         /* Pad of this chain that could be exposed */
423   gboolean deadend;             /* This chain is incomplete and can't be completed,
424                                    e.g. no suitable decoder could be found
425                                    e.g. stream got EOS without buffers
426                                  */
427   GstCaps *endcaps;             /* Caps that were used when linking to the endpad
428                                    or that resulted in the deadend
429                                  */
430
431   /* FIXME: This should be done directly via a thread! */
432   GList *old_groups;            /* Groups that should be freed later */
433 };
434
435 static void gst_decode_chain_free (GstDecodeChain * chain);
436 static GstDecodeChain *gst_decode_chain_new (GstDecodeBin * dbin,
437     GstDecodeGroup * group, GstPad * pad);
438 static void gst_decode_group_hide (GstDecodeGroup * group);
439 static void gst_decode_group_free (GstDecodeGroup * group);
440 static GstDecodeGroup *gst_decode_group_new (GstDecodeBin * dbin,
441     GstDecodeChain * chain);
442 static gboolean gst_decode_chain_is_complete (GstDecodeChain * chain);
443 static gboolean gst_decode_chain_expose (GstDecodeChain * chain,
444     GList ** endpads, gboolean * missing_plugin);
445 static gboolean gst_decode_chain_is_drained (GstDecodeChain * chain);
446 static gboolean gst_decode_group_is_complete (GstDecodeGroup * group);
447 static GstPad *gst_decode_group_control_demuxer_pad (GstDecodeGroup * group,
448     GstPad * pad);
449 static gboolean gst_decode_group_is_drained (GstDecodeGroup * group);
450
451 static gboolean gst_decode_bin_expose (GstDecodeBin * dbin);
452
453 #define CHAIN_MUTEX_LOCK(chain) G_STMT_START {                          \
454     GST_LOG_OBJECT (chain->dbin,                                        \
455                     "locking chain %p from thread %p",                  \
456                     chain, g_thread_self ());                           \
457     g_mutex_lock (&chain->lock);                                                \
458     GST_LOG_OBJECT (chain->dbin,                                        \
459                     "locked chain %p from thread %p",                   \
460                     chain, g_thread_self ());                           \
461 } G_STMT_END
462
463 #define CHAIN_MUTEX_UNLOCK(chain) G_STMT_START {                        \
464     GST_LOG_OBJECT (chain->dbin,                                        \
465                     "unlocking chain %p from thread %p",                \
466                     chain, g_thread_self ());                           \
467     g_mutex_unlock (&chain->lock);                                      \
468 } G_STMT_END
469
470 /* GstDecodePad
471  *
472  * GstPad private used for source pads of chains
473  */
474 struct _GstDecodePad
475 {
476   GstGhostPad parent;
477   GstDecodeBin *dbin;
478   GstDecodeChain *chain;
479
480   gboolean blocked;             /* the *target* pad is blocked */
481   gboolean exposed;             /* the pad is exposed */
482   gboolean drained;             /* an EOS has been seen on the pad */
483
484   gulong block_id;
485 };
486
487 GType gst_decode_pad_get_type (void);
488 G_DEFINE_TYPE (GstDecodePad, gst_decode_pad, GST_TYPE_GHOST_PAD);
489 #define GST_TYPE_DECODE_PAD (gst_decode_pad_get_type ())
490 #define GST_DECODE_PAD(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_DECODE_PAD,GstDecodePad))
491
492 static GstDecodePad *gst_decode_pad_new (GstDecodeBin * dbin, GstPad * pad,
493     GstDecodeChain * chain);
494 static void gst_decode_pad_activate (GstDecodePad * dpad,
495     GstDecodeChain * chain);
496 static void gst_decode_pad_unblock (GstDecodePad * dpad);
497 static void gst_decode_pad_set_blocked (GstDecodePad * dpad, gboolean blocked);
498
499 static void gst_pending_pad_free (GstPendingPad * ppad);
500 static GstPadProbeReturn pad_event_cb (GstPad * pad, GstPadProbeInfo * info,
501     gpointer data);
502
503 /********************************
504  * Standard GObject boilerplate *
505  ********************************/
506
507 static void gst_decode_bin_class_init (GstDecodeBinClass * klass);
508 static void gst_decode_bin_init (GstDecodeBin * decode_bin);
509 static void gst_decode_bin_dispose (GObject * object);
510 static void gst_decode_bin_finalize (GObject * object);
511
512 static GType
513 gst_decode_bin_get_type (void)
514 {
515   static GType gst_decode_bin_type = 0;
516
517   if (!gst_decode_bin_type) {
518     static const GTypeInfo gst_decode_bin_info = {
519       sizeof (GstDecodeBinClass),
520       NULL,
521       NULL,
522       (GClassInitFunc) gst_decode_bin_class_init,
523       NULL,
524       NULL,
525       sizeof (GstDecodeBin),
526       0,
527       (GInstanceInitFunc) gst_decode_bin_init,
528       NULL
529     };
530
531     gst_decode_bin_type =
532         g_type_register_static (GST_TYPE_BIN, "GstDecodeBin",
533         &gst_decode_bin_info, 0);
534   }
535
536   return gst_decode_bin_type;
537 }
538
539 static gboolean
540 _gst_boolean_accumulator (GSignalInvocationHint * ihint,
541     GValue * return_accu, const GValue * handler_return, gpointer dummy)
542 {
543   gboolean myboolean;
544
545   myboolean = g_value_get_boolean (handler_return);
546   if (!(ihint->run_type & G_SIGNAL_RUN_CLEANUP))
547     g_value_set_boolean (return_accu, myboolean);
548
549   /* stop emission if FALSE */
550   return myboolean;
551 }
552
553 /* we collect the first result */
554 static gboolean
555 _gst_array_accumulator (GSignalInvocationHint * ihint,
556     GValue * return_accu, const GValue * handler_return, gpointer dummy)
557 {
558   gpointer array;
559
560   array = g_value_get_boxed (handler_return);
561   if (!(ihint->run_type & G_SIGNAL_RUN_CLEANUP))
562     g_value_set_boxed (return_accu, array);
563
564   return FALSE;
565 }
566
567 static gboolean
568 _gst_select_accumulator (GSignalInvocationHint * ihint,
569     GValue * return_accu, const GValue * handler_return, gpointer dummy)
570 {
571   GstAutoplugSelectResult res;
572
573   res = g_value_get_enum (handler_return);
574   if (!(ihint->run_type & G_SIGNAL_RUN_CLEANUP))
575     g_value_set_enum (return_accu, res);
576
577   return FALSE;
578 }
579
580 static gboolean
581 _gst_array_hasvalue_accumulator (GSignalInvocationHint * ihint,
582     GValue * return_accu, const GValue * handler_return, gpointer dummy)
583 {
584   gpointer array;
585
586   array = g_value_get_boxed (handler_return);
587   if (!(ihint->run_type & G_SIGNAL_RUN_CLEANUP))
588     g_value_set_boxed (return_accu, array);
589
590   if (array != NULL)
591     return FALSE;
592
593   return TRUE;
594 }
595
596 static void
597 gst_decode_bin_class_init (GstDecodeBinClass * klass)
598 {
599   GObjectClass *gobject_klass;
600   GstElementClass *gstelement_klass;
601   GstBinClass *gstbin_klass;
602
603   gobject_klass = (GObjectClass *) klass;
604   gstelement_klass = (GstElementClass *) klass;
605   gstbin_klass = (GstBinClass *) klass;
606
607   parent_class = g_type_class_peek_parent (klass);
608
609   gobject_klass->dispose = gst_decode_bin_dispose;
610   gobject_klass->finalize = gst_decode_bin_finalize;
611   gobject_klass->set_property = gst_decode_bin_set_property;
612   gobject_klass->get_property = gst_decode_bin_get_property;
613
614   /**
615    * GstDecodeBin::unknown-type:
616    * @bin: The decodebin.
617    * @pad: The new pad containing caps that cannot be resolved to a 'final'
618    *       stream type.
619    * @caps: The #GstCaps of the pad that cannot be resolved.
620    *
621    * This signal is emitted when a pad for which there is no further possible
622    * decoding is added to the decodebin.
623    */
624   gst_decode_bin_signals[SIGNAL_UNKNOWN_TYPE] =
625       g_signal_new ("unknown-type", G_TYPE_FROM_CLASS (klass),
626       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstDecodeBinClass, unknown_type),
627       NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 2,
628       GST_TYPE_PAD, GST_TYPE_CAPS);
629
630   /**
631    * GstDecodeBin::autoplug-continue:
632    * @bin: The decodebin.
633    * @pad: The #GstPad.
634    * @caps: The #GstCaps found.
635    *
636    * This signal is emitted whenever decodebin finds a new stream. It is
637    * emitted before looking for any elements that can handle that stream.
638    *
639    * <note>
640    *   Invocation of signal handlers stops after the first signal handler
641    *   returns #FALSE. Signal handlers are invoked in the order they were
642    *   connected in.
643    * </note>
644    *
645    * Returns: #TRUE if you wish decodebin to look for elements that can
646    * handle the given @caps. If #FALSE, those caps will be considered as
647    * final and the pad will be exposed as such (see 'pad-added' signal of
648    * #GstElement).
649    */
650   gst_decode_bin_signals[SIGNAL_AUTOPLUG_CONTINUE] =
651       g_signal_new ("autoplug-continue", G_TYPE_FROM_CLASS (klass),
652       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstDecodeBinClass, autoplug_continue),
653       _gst_boolean_accumulator, NULL, g_cclosure_marshal_generic,
654       G_TYPE_BOOLEAN, 2, GST_TYPE_PAD, GST_TYPE_CAPS);
655
656   /**
657    * GstDecodeBin::autoplug-factories:
658    * @bin: The decodebin.
659    * @pad: The #GstPad.
660    * @caps: The #GstCaps found.
661    *
662    * This function is emited when an array of possible factories for @caps on
663    * @pad is needed. Decodebin will by default return an array with all
664    * compatible factories, sorted by rank.
665    *
666    * If this function returns NULL, @pad will be exposed as a final caps.
667    *
668    * If this function returns an empty array, the pad will be considered as
669    * having an unhandled type media type.
670    *
671    * <note>
672    *   Only the signal handler that is connected first will ever by invoked.
673    *   Don't connect signal handlers with the #G_CONNECT_AFTER flag to this
674    *   signal, they will never be invoked!
675    * </note>
676    *
677    * Returns: a #GValueArray* with a list of factories to try. The factories are
678    * by default tried in the returned order or based on the index returned by
679    * "autoplug-select".
680    */
681   gst_decode_bin_signals[SIGNAL_AUTOPLUG_FACTORIES] =
682       g_signal_new ("autoplug-factories", G_TYPE_FROM_CLASS (klass),
683       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstDecodeBinClass,
684           autoplug_factories), _gst_array_accumulator, NULL,
685       g_cclosure_marshal_generic, G_TYPE_VALUE_ARRAY, 2,
686       GST_TYPE_PAD, GST_TYPE_CAPS);
687
688   /**
689    * GstDecodeBin::autoplug-sort:
690    * @bin: The decodebin.
691    * @pad: The #GstPad.
692    * @caps: The #GstCaps.
693    * @factories: A #GValueArray of possible #GstElementFactory to use.
694    *
695    * Once decodebin has found the possible #GstElementFactory objects to try
696    * for @caps on @pad, this signal is emited. The purpose of the signal is for
697    * the application to perform additional sorting or filtering on the element
698    * factory array.
699    *
700    * The callee should copy and modify @factories or return #NULL if the
701    * order should not change.
702    *
703    * <note>
704    *   Invocation of signal handlers stops after one signal handler has
705    *   returned something else than #NULL. Signal handlers are invoked in
706    *   the order they were connected in.
707    *   Don't connect signal handlers with the #G_CONNECT_AFTER flag to this
708    *   signal, they will never be invoked!
709    * </note>
710    *
711    * Returns: A new sorted array of #GstElementFactory objects.
712    */
713   gst_decode_bin_signals[SIGNAL_AUTOPLUG_SORT] =
714       g_signal_new ("autoplug-sort", G_TYPE_FROM_CLASS (klass),
715       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstDecodeBinClass, autoplug_sort),
716       _gst_array_hasvalue_accumulator, NULL,
717       g_cclosure_marshal_generic, G_TYPE_VALUE_ARRAY, 3, GST_TYPE_PAD,
718       GST_TYPE_CAPS, G_TYPE_VALUE_ARRAY | G_SIGNAL_TYPE_STATIC_SCOPE);
719
720   /**
721    * GstDecodeBin::autoplug-select:
722    * @bin: The decodebin.
723    * @pad: The #GstPad.
724    * @caps: The #GstCaps.
725    * @factory: A #GstElementFactory to use.
726    *
727    * This signal is emitted once decodebin has found all the possible
728    * #GstElementFactory that can be used to handle the given @caps. For each of
729    * those factories, this signal is emitted.
730    *
731    * The signal handler should return a #GST_TYPE_AUTOPLUG_SELECT_RESULT enum
732    * value indicating what decodebin should do next.
733    *
734    * A value of #GST_AUTOPLUG_SELECT_TRY will try to autoplug an element from
735    * @factory.
736    *
737    * A value of #GST_AUTOPLUG_SELECT_EXPOSE will expose @pad without plugging
738    * any element to it.
739    *
740    * A value of #GST_AUTOPLUG_SELECT_SKIP will skip @factory and move to the
741    * next factory.
742    *
743    * <note>
744    *   Only the signal handler that is connected first will ever by invoked.
745    *   Don't connect signal handlers with the #G_CONNECT_AFTER flag to this
746    *   signal, they will never be invoked!
747    * </note>
748    *
749    * Returns: a #GST_TYPE_AUTOPLUG_SELECT_RESULT that indicates the required
750    * operation. the default handler will always return
751    * #GST_AUTOPLUG_SELECT_TRY.
752    */
753   gst_decode_bin_signals[SIGNAL_AUTOPLUG_SELECT] =
754       g_signal_new ("autoplug-select", G_TYPE_FROM_CLASS (klass),
755       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstDecodeBinClass, autoplug_select),
756       _gst_select_accumulator, NULL,
757       g_cclosure_marshal_generic,
758       GST_TYPE_AUTOPLUG_SELECT_RESULT, 3, GST_TYPE_PAD, GST_TYPE_CAPS,
759       GST_TYPE_ELEMENT_FACTORY);
760
761   /**
762    * GstDecodeBin::drained
763    * @bin: The decodebin
764    *
765    * This signal is emitted once decodebin has finished decoding all the data.
766    *
767    * Since: 0.10.16
768    */
769   gst_decode_bin_signals[SIGNAL_DRAINED] =
770       g_signal_new ("drained", G_TYPE_FROM_CLASS (klass),
771       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstDecodeBinClass, drained),
772       NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 0, G_TYPE_NONE);
773
774   g_object_class_install_property (gobject_klass, PROP_CAPS,
775       g_param_spec_boxed ("caps", "Caps", "The caps on which to stop decoding.",
776           GST_TYPE_CAPS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
777
778   g_object_class_install_property (gobject_klass, PROP_SUBTITLE_ENCODING,
779       g_param_spec_string ("subtitle-encoding", "subtitle encoding",
780           "Encoding to assume if input subtitles are not in UTF-8 encoding. "
781           "If not set, the GST_SUBTITLE_ENCODING environment variable will "
782           "be checked for an encoding to use. If that is not set either, "
783           "ISO-8859-15 will be assumed.", NULL,
784           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
785
786   g_object_class_install_property (gobject_klass, PROP_SINK_CAPS,
787       g_param_spec_boxed ("sink-caps", "Sink Caps",
788           "The caps of the input data. (NULL = use typefind element)",
789           GST_TYPE_CAPS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
790
791   /**
792    * GstDecodeBin::use-buffering
793    *
794    * Activate buffering in decodebin. This will instruct the multiqueues behind
795    * decoders to emit BUFFERING messages.
796
797    * Since: 0.10.26
798    */
799   g_object_class_install_property (gobject_klass, PROP_USE_BUFFERING,
800       g_param_spec_boolean ("use-buffering", "Use Buffering",
801           "Emit GST_MESSAGE_BUFFERING based on low-/high-percent thresholds",
802           DEFAULT_USE_BUFFERING, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
803
804   /**
805    * GstDecodeBin:low-percent
806    *
807    * Low threshold percent for buffering to start.
808    *
809    * Since: 0.10.26
810    */
811   g_object_class_install_property (gobject_klass, PROP_LOW_PERCENT,
812       g_param_spec_int ("low-percent", "Low percent",
813           "Low threshold for buffering to start", 0, 100,
814           DEFAULT_LOW_PERCENT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
815   /**
816    * GstDecodeBin:high-percent
817    *
818    * High threshold percent for buffering to finish.
819    *
820    * Since: 0.10.26
821    */
822   g_object_class_install_property (gobject_klass, PROP_HIGH_PERCENT,
823       g_param_spec_int ("high-percent", "High percent",
824           "High threshold for buffering to finish", 0, 100,
825           DEFAULT_HIGH_PERCENT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
826
827   /**
828    * GstDecodeBin:max-size-bytes
829    *
830    * Max amount of bytes in the queue (0=automatic).
831    *
832    * Since: 0.10.26
833    */
834   g_object_class_install_property (gobject_klass, PROP_MAX_SIZE_BYTES,
835       g_param_spec_uint ("max-size-bytes", "Max. size (bytes)",
836           "Max. amount of bytes in the queue (0=automatic)",
837           0, G_MAXUINT, DEFAULT_MAX_SIZE_BYTES,
838           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
839   /**
840    * GstDecodeBin:max-size-buffers
841    *
842    * Max amount of buffers in the queue (0=automatic).
843    *
844    * Since: 0.10.26
845    */
846   g_object_class_install_property (gobject_klass, PROP_MAX_SIZE_BUFFERS,
847       g_param_spec_uint ("max-size-buffers", "Max. size (buffers)",
848           "Max. number of buffers in the queue (0=automatic)",
849           0, G_MAXUINT, DEFAULT_MAX_SIZE_BUFFERS,
850           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
851   /**
852    * GstDecodeBin:max-size-time
853    *
854    * Max amount of time in the queue (in ns, 0=automatic).
855    *
856    * Since: 0.10.26
857    */
858   g_object_class_install_property (gobject_klass, PROP_MAX_SIZE_TIME,
859       g_param_spec_uint64 ("max-size-time", "Max. size (ns)",
860           "Max. amount of data in the queue (in ns, 0=automatic)",
861           0, G_MAXUINT64,
862           DEFAULT_MAX_SIZE_TIME, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
863
864   /**
865    * GstDecodeBin::post-stream-topology
866    *
867    * Post stream-topology messages on the bus every time the topology changes.
868    *
869    * Since: 0.10.26
870    */
871   g_object_class_install_property (gobject_klass, PROP_POST_STREAM_TOPOLOGY,
872       g_param_spec_boolean ("post-stream-topology", "Post Stream Topology",
873           "Post stream-topology messages",
874           DEFAULT_POST_STREAM_TOPOLOGY,
875           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
876
877   /**
878    * GstDecodeBin::expose-all-streams
879    *
880    * Expose streams of unknown type.
881    *
882    * If set to %FALSE, then only the streams that can be decoded to the final
883    * caps (see 'caps' property) will have a pad exposed. Streams that do not
884    * match those caps but could have been decoded will not have decoder plugged
885    * in internally and will not have a pad exposed.
886    *
887    * Since: 0.10.30
888    */
889   g_object_class_install_property (gobject_klass, PROP_EXPOSE_ALL_STREAMS,
890       g_param_spec_boolean ("expose-all-streams", "Expose All Streams",
891           "Expose all streams, including those of unknown type or that don't match the 'caps' property",
892           DEFAULT_EXPOSE_ALL_STREAMS,
893           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
894
895   /**
896    * GstDecodeBin2::connection-speed
897    *
898    * Network connection speed in kbps (0 = unknownw)
899    *
900    * Since: 0.10.XX
901    */
902   g_object_class_install_property (gobject_klass, PROP_CONNECTION_SPEED,
903       g_param_spec_uint64 ("connection-speed", "Connection Speed",
904           "Network connection speed in kbps (0 = unknown)",
905           0, G_MAXUINT64 / 1000, DEFAULT_CONNECTION_SPEED,
906           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
907
908
909
910   klass->autoplug_continue =
911       GST_DEBUG_FUNCPTR (gst_decode_bin_autoplug_continue);
912   klass->autoplug_factories =
913       GST_DEBUG_FUNCPTR (gst_decode_bin_autoplug_factories);
914   klass->autoplug_sort = GST_DEBUG_FUNCPTR (gst_decode_bin_autoplug_sort);
915   klass->autoplug_select = GST_DEBUG_FUNCPTR (gst_decode_bin_autoplug_select);
916
917   gst_element_class_add_pad_template (gstelement_klass,
918       gst_static_pad_template_get (&decoder_bin_sink_template));
919   gst_element_class_add_pad_template (gstelement_klass,
920       gst_static_pad_template_get (&decoder_bin_src_template));
921
922   gst_element_class_set_static_metadata (gstelement_klass,
923       "Decoder Bin", "Generic/Bin/Decoder",
924       "Autoplug and decode to raw media",
925       "Edward Hervey <edward.hervey@collabora.co.uk>, "
926       "Sebastian Dröge <sebastian.droege@collabora.co.uk>");
927
928   gstelement_klass->change_state =
929       GST_DEBUG_FUNCPTR (gst_decode_bin_change_state);
930
931   gstbin_klass->handle_message =
932       GST_DEBUG_FUNCPTR (gst_decode_bin_handle_message);
933
934   g_type_class_ref (GST_TYPE_DECODE_PAD);
935 }
936
937 gint
938 _decode_bin_compare_factories_func (gconstpointer p1, gconstpointer p2)
939 {
940   GstPluginFeature *f1, *f2;
941   gint diff;
942   gboolean is_parser1, is_parser2;
943
944   f1 = (GstPluginFeature *) p1;
945   f2 = (GstPluginFeature *) p2;
946
947   is_parser1 = gst_element_factory_list_is_type (GST_ELEMENT_FACTORY_CAST (f1),
948       GST_ELEMENT_FACTORY_TYPE_PARSER);
949   is_parser2 = gst_element_factory_list_is_type (GST_ELEMENT_FACTORY_CAST (f2),
950       GST_ELEMENT_FACTORY_TYPE_PARSER);
951
952
953   /* We want all parsers first as we always want to plug parsers
954    * before decoders */
955   if (is_parser1 && !is_parser2)
956     return -1;
957   else if (!is_parser1 && is_parser2)
958     return 1;
959
960   /* And if it's a both a parser we first sort by rank
961    * and then by factory name */
962   diff = gst_plugin_feature_get_rank (f2) - gst_plugin_feature_get_rank (f1);
963   if (diff != 0)
964     return diff;
965
966   diff = strcmp (GST_OBJECT_NAME (f2), GST_OBJECT_NAME (f1));
967
968   return diff;
969 }
970
971 /* Must be called with factories lock! */
972 static void
973 gst_decode_bin_update_factories_list (GstDecodeBin * dbin)
974 {
975   guint cookie;
976
977   cookie = gst_registry_get_feature_list_cookie (gst_registry_get ());
978   if (!dbin->factories || dbin->factories_cookie != cookie) {
979     if (dbin->factories)
980       gst_plugin_feature_list_free (dbin->factories);
981     dbin->factories =
982         gst_element_factory_list_get_elements
983         (GST_ELEMENT_FACTORY_TYPE_DECODABLE, GST_RANK_MARGINAL);
984     dbin->factories =
985         g_list_sort (dbin->factories, _decode_bin_compare_factories_func);
986     dbin->factories_cookie = cookie;
987   }
988 }
989
990 static void
991 gst_decode_bin_init (GstDecodeBin * decode_bin)
992 {
993   GST_OBJECT_FLAG_SET (decode_bin, GST_BIN_FLAG_NO_RESYNC);
994
995   /* first filter out the interesting element factories */
996   g_mutex_init (&decode_bin->factories_lock);
997
998   /* we create the typefind element only once */
999   decode_bin->typefind = gst_element_factory_make ("typefind", "typefind");
1000   if (!decode_bin->typefind) {
1001     g_warning ("can't find typefind element, decodebin will not work");
1002   } else {
1003     GstPad *pad;
1004     GstPad *gpad;
1005     GstPadTemplate *pad_tmpl;
1006
1007     /* add the typefind element */
1008     if (!gst_bin_add (GST_BIN (decode_bin), decode_bin->typefind)) {
1009       g_warning ("Could not add typefind element, decodebin will not work");
1010       gst_object_unref (decode_bin->typefind);
1011       decode_bin->typefind = NULL;
1012     }
1013
1014     /* get the sinkpad */
1015     pad = gst_element_get_static_pad (decode_bin->typefind, "sink");
1016
1017     /* get the pad template */
1018     pad_tmpl = gst_static_pad_template_get (&decoder_bin_sink_template);
1019
1020     /* ghost the sink pad to ourself */
1021     gpad = gst_ghost_pad_new_from_template ("sink", pad, pad_tmpl);
1022     gst_pad_set_active (gpad, TRUE);
1023     gst_element_add_pad (GST_ELEMENT (decode_bin), gpad);
1024
1025     gst_object_unref (pad_tmpl);
1026     gst_object_unref (pad);
1027
1028     /* connect a signal to find out when the typefind element found
1029      * a type */
1030     decode_bin->have_type_id =
1031         g_signal_connect (G_OBJECT (decode_bin->typefind), "have-type",
1032         G_CALLBACK (type_found), decode_bin);
1033   }
1034
1035   g_mutex_init (&decode_bin->expose_lock);
1036   decode_bin->decode_chain = NULL;
1037
1038   g_mutex_init (&decode_bin->dyn_lock);
1039   decode_bin->shutdown = FALSE;
1040   decode_bin->blocked_pads = NULL;
1041
1042   g_mutex_init (&decode_bin->subtitle_lock);
1043
1044   decode_bin->encoding = g_strdup (DEFAULT_SUBTITLE_ENCODING);
1045   decode_bin->caps = gst_static_caps_get (&default_raw_caps);
1046   decode_bin->use_buffering = DEFAULT_USE_BUFFERING;
1047   decode_bin->low_percent = DEFAULT_LOW_PERCENT;
1048   decode_bin->high_percent = DEFAULT_HIGH_PERCENT;
1049
1050   decode_bin->max_size_bytes = DEFAULT_MAX_SIZE_BYTES;
1051   decode_bin->max_size_buffers = DEFAULT_MAX_SIZE_BUFFERS;
1052   decode_bin->max_size_time = DEFAULT_MAX_SIZE_TIME;
1053
1054   decode_bin->expose_allstreams = DEFAULT_EXPOSE_ALL_STREAMS;
1055   decode_bin->connection_speed = DEFAULT_CONNECTION_SPEED;
1056 }
1057
1058 static void
1059 gst_decode_bin_dispose (GObject * object)
1060 {
1061   GstDecodeBin *decode_bin;
1062
1063   decode_bin = GST_DECODE_BIN (object);
1064
1065   if (decode_bin->factories)
1066     gst_plugin_feature_list_free (decode_bin->factories);
1067   decode_bin->factories = NULL;
1068
1069   if (decode_bin->decode_chain)
1070     gst_decode_chain_free (decode_bin->decode_chain);
1071   decode_bin->decode_chain = NULL;
1072
1073   if (decode_bin->caps)
1074     gst_caps_unref (decode_bin->caps);
1075   decode_bin->caps = NULL;
1076
1077   g_free (decode_bin->encoding);
1078   decode_bin->encoding = NULL;
1079
1080   g_list_free (decode_bin->subtitles);
1081   decode_bin->subtitles = NULL;
1082
1083   G_OBJECT_CLASS (parent_class)->dispose (object);
1084 }
1085
1086 static void
1087 gst_decode_bin_finalize (GObject * object)
1088 {
1089   GstDecodeBin *decode_bin;
1090
1091   decode_bin = GST_DECODE_BIN (object);
1092
1093   g_mutex_clear (&decode_bin->expose_lock);
1094   g_mutex_clear (&decode_bin->dyn_lock);
1095   g_mutex_clear (&decode_bin->subtitle_lock);
1096   g_mutex_clear (&decode_bin->factories_lock);
1097
1098   G_OBJECT_CLASS (parent_class)->finalize (object);
1099 }
1100
1101 /* _set_caps
1102  * Changes the caps on which decodebin will stop decoding.
1103  * Will unref the previously set one. The refcount of the given caps will be
1104  * increased.
1105  * @caps can be NULL.
1106  *
1107  * MT-safe
1108  */
1109 static void
1110 gst_decode_bin_set_caps (GstDecodeBin * dbin, GstCaps * caps)
1111 {
1112   GST_DEBUG_OBJECT (dbin, "Setting new caps: %" GST_PTR_FORMAT, caps);
1113
1114   GST_OBJECT_LOCK (dbin);
1115   gst_caps_replace (&dbin->caps, caps);
1116   GST_OBJECT_UNLOCK (dbin);
1117 }
1118
1119 /* _get_caps
1120  * Returns the currently configured caps on which decodebin will stop decoding.
1121  * The returned caps (if not NULL), will have its refcount incremented.
1122  *
1123  * MT-safe
1124  */
1125 static GstCaps *
1126 gst_decode_bin_get_caps (GstDecodeBin * dbin)
1127 {
1128   GstCaps *caps;
1129
1130   GST_DEBUG_OBJECT (dbin, "Getting currently set caps");
1131
1132   GST_OBJECT_LOCK (dbin);
1133   caps = dbin->caps;
1134   if (caps)
1135     gst_caps_ref (caps);
1136   GST_OBJECT_UNLOCK (dbin);
1137
1138   return caps;
1139 }
1140
1141 static void
1142 gst_decode_bin_set_sink_caps (GstDecodeBin * dbin, GstCaps * caps)
1143 {
1144   GST_DEBUG_OBJECT (dbin, "Setting new caps: %" GST_PTR_FORMAT, caps);
1145
1146   g_object_set (dbin->typefind, "force-caps", caps, NULL);
1147 }
1148
1149 static GstCaps *
1150 gst_decode_bin_get_sink_caps (GstDecodeBin * dbin)
1151 {
1152   GstCaps *caps;
1153
1154   GST_DEBUG_OBJECT (dbin, "Getting currently set caps");
1155
1156   g_object_get (dbin->typefind, "force-caps", &caps, NULL);
1157
1158   return caps;
1159 }
1160
1161 static void
1162 gst_decode_bin_set_subs_encoding (GstDecodeBin * dbin, const gchar * encoding)
1163 {
1164   GList *walk;
1165
1166   GST_DEBUG_OBJECT (dbin, "Setting new encoding: %s", GST_STR_NULL (encoding));
1167
1168   SUBTITLE_LOCK (dbin);
1169   g_free (dbin->encoding);
1170   dbin->encoding = g_strdup (encoding);
1171
1172   /* set the subtitle encoding on all added elements */
1173   for (walk = dbin->subtitles; walk; walk = g_list_next (walk)) {
1174     g_object_set (G_OBJECT (walk->data), "subtitle-encoding", dbin->encoding,
1175         NULL);
1176   }
1177   SUBTITLE_UNLOCK (dbin);
1178 }
1179
1180 static gchar *
1181 gst_decode_bin_get_subs_encoding (GstDecodeBin * dbin)
1182 {
1183   gchar *encoding;
1184
1185   GST_DEBUG_OBJECT (dbin, "Getting currently set encoding");
1186
1187   SUBTITLE_LOCK (dbin);
1188   encoding = g_strdup (dbin->encoding);
1189   SUBTITLE_UNLOCK (dbin);
1190
1191   return encoding;
1192 }
1193
1194 static void
1195 gst_decode_bin_set_property (GObject * object, guint prop_id,
1196     const GValue * value, GParamSpec * pspec)
1197 {
1198   GstDecodeBin *dbin;
1199
1200   dbin = GST_DECODE_BIN (object);
1201
1202   switch (prop_id) {
1203     case PROP_CAPS:
1204       gst_decode_bin_set_caps (dbin, g_value_get_boxed (value));
1205       break;
1206     case PROP_SUBTITLE_ENCODING:
1207       gst_decode_bin_set_subs_encoding (dbin, g_value_get_string (value));
1208       break;
1209     case PROP_SINK_CAPS:
1210       gst_decode_bin_set_sink_caps (dbin, g_value_get_boxed (value));
1211       break;
1212     case PROP_USE_BUFFERING:
1213       dbin->use_buffering = g_value_get_boolean (value);
1214       break;
1215     case PROP_LOW_PERCENT:
1216       dbin->low_percent = g_value_get_int (value);
1217       break;
1218     case PROP_HIGH_PERCENT:
1219       dbin->high_percent = g_value_get_int (value);
1220       break;
1221     case PROP_MAX_SIZE_BYTES:
1222       dbin->max_size_bytes = g_value_get_uint (value);
1223       break;
1224     case PROP_MAX_SIZE_BUFFERS:
1225       dbin->max_size_buffers = g_value_get_uint (value);
1226       break;
1227     case PROP_MAX_SIZE_TIME:
1228       dbin->max_size_time = g_value_get_uint64 (value);
1229       break;
1230     case PROP_POST_STREAM_TOPOLOGY:
1231       dbin->post_stream_topology = g_value_get_boolean (value);
1232       break;
1233     case PROP_EXPOSE_ALL_STREAMS:
1234       dbin->expose_allstreams = g_value_get_boolean (value);
1235       break;
1236     case PROP_CONNECTION_SPEED:
1237       GST_OBJECT_LOCK (dbin);
1238       dbin->connection_speed = g_value_get_uint64 (value) * 1000;
1239       GST_OBJECT_UNLOCK (dbin);
1240       break;
1241     default:
1242       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1243       break;
1244   }
1245 }
1246
1247 static void
1248 gst_decode_bin_get_property (GObject * object, guint prop_id,
1249     GValue * value, GParamSpec * pspec)
1250 {
1251   GstDecodeBin *dbin;
1252
1253   dbin = GST_DECODE_BIN (object);
1254   switch (prop_id) {
1255     case PROP_CAPS:
1256       g_value_take_boxed (value, gst_decode_bin_get_caps (dbin));
1257       break;
1258     case PROP_SUBTITLE_ENCODING:
1259       g_value_take_string (value, gst_decode_bin_get_subs_encoding (dbin));
1260       break;
1261     case PROP_SINK_CAPS:
1262       g_value_take_boxed (value, gst_decode_bin_get_sink_caps (dbin));
1263       break;
1264     case PROP_USE_BUFFERING:
1265       g_value_set_boolean (value, dbin->use_buffering);
1266       break;
1267     case PROP_LOW_PERCENT:
1268       g_value_set_int (value, dbin->low_percent);
1269       break;
1270     case PROP_HIGH_PERCENT:
1271       g_value_set_int (value, dbin->high_percent);
1272       break;
1273     case PROP_MAX_SIZE_BYTES:
1274       g_value_set_uint (value, dbin->max_size_bytes);
1275       break;
1276     case PROP_MAX_SIZE_BUFFERS:
1277       g_value_set_uint (value, dbin->max_size_buffers);
1278       break;
1279     case PROP_MAX_SIZE_TIME:
1280       g_value_set_uint64 (value, dbin->max_size_time);
1281       break;
1282     case PROP_POST_STREAM_TOPOLOGY:
1283       g_value_set_boolean (value, dbin->post_stream_topology);
1284       break;
1285     case PROP_EXPOSE_ALL_STREAMS:
1286       g_value_set_boolean (value, dbin->expose_allstreams);
1287       break;
1288     case PROP_CONNECTION_SPEED:
1289       GST_OBJECT_LOCK (dbin);
1290       g_value_set_uint64 (value, dbin->connection_speed / 1000);
1291       GST_OBJECT_UNLOCK (dbin);
1292       break;
1293     default:
1294       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1295       break;
1296   }
1297 }
1298
1299
1300 /*****
1301  * Default autoplug signal handlers
1302  *****/
1303 static gboolean
1304 gst_decode_bin_autoplug_continue (GstElement * element, GstPad * pad,
1305     GstCaps * caps)
1306 {
1307   GST_DEBUG_OBJECT (element, "autoplug-continue returns TRUE");
1308
1309   /* by default we always continue */
1310   return TRUE;
1311 }
1312
1313 static GValueArray *
1314 gst_decode_bin_autoplug_factories (GstElement * element, GstPad * pad,
1315     GstCaps * caps)
1316 {
1317   GList *list, *tmp;
1318   GValueArray *result;
1319   GstDecodeBin *dbin = GST_DECODE_BIN_CAST (element);
1320
1321   GST_DEBUG_OBJECT (element, "finding factories");
1322
1323   /* return all compatible factories for caps */
1324   g_mutex_lock (&dbin->factories_lock);
1325   gst_decode_bin_update_factories_list (dbin);
1326   list =
1327       gst_element_factory_list_filter (dbin->factories, caps, GST_PAD_SINK,
1328       FALSE);
1329   g_mutex_unlock (&dbin->factories_lock);
1330
1331   result = g_value_array_new (g_list_length (list));
1332   for (tmp = list; tmp; tmp = tmp->next) {
1333     GstElementFactory *factory = GST_ELEMENT_FACTORY_CAST (tmp->data);
1334     GValue val = { 0, };
1335
1336     g_value_init (&val, G_TYPE_OBJECT);
1337     g_value_set_object (&val, factory);
1338     g_value_array_append (result, &val);
1339     g_value_unset (&val);
1340   }
1341   gst_plugin_feature_list_free (list);
1342
1343   GST_DEBUG_OBJECT (element, "autoplug-factories returns %p", result);
1344
1345   return result;
1346 }
1347
1348 static GValueArray *
1349 gst_decode_bin_autoplug_sort (GstElement * element, GstPad * pad,
1350     GstCaps * caps, GValueArray * factories)
1351 {
1352   return NULL;
1353 }
1354
1355 static GstAutoplugSelectResult
1356 gst_decode_bin_autoplug_select (GstElement * element, GstPad * pad,
1357     GstCaps * caps, GstElementFactory * factory)
1358 {
1359   GST_DEBUG_OBJECT (element, "default autoplug-select returns TRY");
1360
1361   /* Try factory. */
1362   return GST_AUTOPLUG_SELECT_TRY;
1363 }
1364
1365 /********
1366  * Discovery methods
1367  *****/
1368
1369 static gboolean are_final_caps (GstDecodeBin * dbin, GstCaps * caps);
1370 static gboolean is_demuxer_element (GstElement * srcelement);
1371
1372 static gboolean connect_pad (GstDecodeBin * dbin, GstElement * src,
1373     GstDecodePad * dpad, GstPad * pad, GstCaps * caps, GValueArray * factories,
1374     GstDecodeChain * chain);
1375 static gboolean connect_element (GstDecodeBin * dbin, GstElement * element,
1376     GstDecodeChain * chain);
1377 static void expose_pad (GstDecodeBin * dbin, GstElement * src,
1378     GstDecodePad * dpad, GstPad * pad, GstCaps * caps, GstDecodeChain * chain);
1379
1380 static void pad_added_cb (GstElement * element, GstPad * pad,
1381     GstDecodeChain * chain);
1382 static void pad_removed_cb (GstElement * element, GstPad * pad,
1383     GstDecodeChain * chain);
1384 static void no_more_pads_cb (GstElement * element, GstDecodeChain * chain);
1385
1386 static GstDecodeGroup *gst_decode_chain_get_current_group (GstDecodeChain *
1387     chain);
1388
1389 /* called when a new pad is discovered. It will perform some basic actions
1390  * before trying to link something to it.
1391  *
1392  *  - Check the caps, don't do anything when there are no caps or when they have
1393  *    no good type.
1394  *  - signal AUTOPLUG_CONTINUE to check if we need to continue autoplugging this
1395  *    pad.
1396  *  - if the caps are non-fixed, setup a handler to continue autoplugging when
1397  *    the caps become fixed (connect to notify::caps).
1398  *  - get list of factories to autoplug.
1399  *  - continue autoplugging to one of the factories.
1400  */
1401 static void
1402 analyze_new_pad (GstDecodeBin * dbin, GstElement * src, GstPad * pad,
1403     GstCaps * caps, GstDecodeChain * chain)
1404 {
1405   gboolean apcontinue = TRUE;
1406   GValueArray *factories = NULL, *result = NULL;
1407   GstDecodePad *dpad;
1408   GstElementFactory *factory;
1409   const gchar *classification;
1410   gboolean is_parser_converter = FALSE;
1411   gboolean res;
1412
1413   GST_DEBUG_OBJECT (dbin, "Pad %s:%s caps:%" GST_PTR_FORMAT,
1414       GST_DEBUG_PAD_NAME (pad), caps);
1415
1416   if (chain->elements
1417       && src != ((GstDecodeElement *) chain->elements->data)->element
1418       && src != ((GstDecodeElement *) chain->elements->data)->capsfilter) {
1419     GST_ERROR_OBJECT (dbin, "New pad from not the last element in this chain");
1420     return;
1421   }
1422
1423   if (chain->endpad) {
1424     GST_ERROR_OBJECT (dbin, "New pad in a chain that is already complete");
1425     return;
1426   }
1427
1428   if (chain->demuxer) {
1429     GstDecodeGroup *group;
1430     GstDecodeChain *oldchain = chain;
1431
1432     /* we are adding a new pad for a demuxer (see is_demuxer_element(),
1433      * start a new chain for it */
1434     CHAIN_MUTEX_LOCK (oldchain);
1435     group = gst_decode_chain_get_current_group (chain);
1436     if (group) {
1437       chain = gst_decode_chain_new (dbin, group, pad);
1438       group->children = g_list_prepend (group->children, chain);
1439     }
1440     CHAIN_MUTEX_UNLOCK (oldchain);
1441     if (!group) {
1442       GST_WARNING_OBJECT (dbin, "No current group");
1443       return;
1444     }
1445   }
1446
1447   if ((caps == NULL) || gst_caps_is_empty (caps))
1448     goto unknown_type;
1449
1450   if (gst_caps_is_any (caps))
1451     goto any_caps;
1452
1453   dpad = gst_decode_pad_new (dbin, pad, chain);
1454
1455   /* 1. Emit 'autoplug-continue' the result will tell us if this pads needs
1456    * further autoplugging. Only do this for fixed caps, for unfixed caps
1457    * we will later come here again from the notify::caps handler. The
1458    * problem with unfixed caps is that we can reliably tell if the output
1459    * is e.g. accepted by a sink because only parts of the possible final
1460    * caps might be accepted by the sink. */
1461   if (gst_caps_is_fixed (caps))
1462     g_signal_emit (G_OBJECT (dbin),
1463         gst_decode_bin_signals[SIGNAL_AUTOPLUG_CONTINUE], 0, dpad, caps,
1464         &apcontinue);
1465   else
1466     apcontinue = TRUE;
1467
1468   /* 1.a if autoplug-continue is FALSE or caps is a raw format, goto pad_is_final */
1469   if ((!apcontinue) || are_final_caps (dbin, caps))
1470     goto expose_pad;
1471
1472   /* 1.b For Parser/Converter that can output different stream formats
1473    * we insert a capsfilter with the sorted caps of all possible next
1474    * elements and continue with the capsfilter srcpad */
1475   factory = gst_element_get_factory (src);
1476   classification =
1477       gst_element_factory_get_metadata (factory, GST_ELEMENT_METADATA_KLASS);
1478   is_parser_converter = (strstr (classification, "Parser")
1479       && strstr (classification, "Converter"));
1480
1481   /* 1.c when the caps are not fixed yet, we can't be sure what element to
1482    * connect. We delay autoplugging until the caps are fixed */
1483   if (!is_parser_converter && !gst_caps_is_fixed (caps))
1484     goto non_fixed;
1485
1486   /* 1.d else get the factories and if there's no compatible factory goto
1487    * unknown_type */
1488   g_signal_emit (G_OBJECT (dbin),
1489       gst_decode_bin_signals[SIGNAL_AUTOPLUG_FACTORIES], 0, dpad, caps,
1490       &factories);
1491
1492   /* NULL means that we can expose the pad */
1493   if (factories == NULL)
1494     goto expose_pad;
1495
1496   /* if the array is empty, we have a type for which we have no decoder */
1497   if (factories->n_values == 0) {
1498     if (!dbin->expose_allstreams) {
1499       GstCaps *raw = gst_static_caps_get (&default_raw_caps);
1500
1501       /* If the caps are raw, this just means we don't want to expose them */
1502       if (gst_caps_can_intersect (raw, caps)) {
1503         g_value_array_free (factories);
1504         gst_caps_unref (raw);
1505         gst_object_unref (dpad);
1506         goto discarded_type;
1507       }
1508       gst_caps_unref (raw);
1509     }
1510
1511     /* if not we have a unhandled type with no compatible factories */
1512     g_value_array_free (factories);
1513     gst_object_unref (dpad);
1514     goto unknown_type;
1515   }
1516
1517   /* 1.e sort some more. */
1518   g_signal_emit (G_OBJECT (dbin),
1519       gst_decode_bin_signals[SIGNAL_AUTOPLUG_SORT], 0, dpad, caps, factories,
1520       &result);
1521   if (result) {
1522     g_value_array_free (factories);
1523     factories = result;
1524   }
1525
1526   /* At this point we have a potential decoder, but we might not need it
1527    * if it doesn't match the output caps  */
1528   if (!dbin->expose_allstreams && gst_caps_is_fixed (caps)) {
1529     guint i;
1530     const GList *tmps;
1531     gboolean dontuse = FALSE;
1532
1533     GST_DEBUG ("Checking if we can abort early");
1534
1535     /* 1.f Do an early check to see if the candidates are potential decoders, but
1536      * due to the fact that they decode to a mediatype that is not final we don't
1537      * need them */
1538
1539     for (i = 0; i < factories->n_values && !dontuse; i++) {
1540       GstElementFactory *factory =
1541           g_value_get_object (g_value_array_get_nth (factories, i));
1542       GstCaps *tcaps;
1543
1544       /* We are only interested in skipping decoders */
1545       if (strstr (gst_element_factory_get_metadata (factory,
1546                   GST_ELEMENT_METADATA_KLASS), "Decoder")) {
1547
1548         GST_DEBUG ("Trying factory %s",
1549             gst_plugin_feature_get_name (GST_PLUGIN_FEATURE (factory)));
1550
1551         /* Check the source pad template caps to see if they match raw caps but don't match
1552          * our final caps*/
1553         for (tmps = gst_element_factory_get_static_pad_templates (factory);
1554             tmps && !dontuse; tmps = tmps->next) {
1555           GstStaticPadTemplate *st = (GstStaticPadTemplate *) tmps->data;
1556           if (st->direction != GST_PAD_SRC)
1557             continue;
1558           tcaps = gst_static_pad_template_get_caps (st);
1559
1560           apcontinue = TRUE;
1561
1562           /* Emit autoplug-continue to see if the caps are considered to be raw caps */
1563           g_signal_emit (G_OBJECT (dbin),
1564               gst_decode_bin_signals[SIGNAL_AUTOPLUG_CONTINUE], 0, dpad, tcaps,
1565               &apcontinue);
1566
1567           /* If autoplug-continue returns TRUE and the caps are not final, don't use them */
1568           if (apcontinue && !are_final_caps (dbin, tcaps))
1569             dontuse = TRUE;
1570           gst_caps_unref (tcaps);
1571         }
1572       }
1573     }
1574
1575     if (dontuse) {
1576       gst_object_unref (dpad);
1577       g_value_array_free (factories);
1578       goto discarded_type;
1579     }
1580   }
1581
1582   /* 1.g now get the factory template caps and insert the capsfilter if this
1583    * is a parser/converter
1584    */
1585   if (is_parser_converter) {
1586     GstCaps *filter_caps;
1587     gint i;
1588     GstPad *p;
1589     GstDecodeElement *delem;
1590
1591     g_assert (chain->elements != NULL);
1592     delem = (GstDecodeElement *) chain->elements->data;
1593
1594     filter_caps = gst_caps_new_empty ();
1595     for (i = 0; i < factories->n_values; i++) {
1596       GstElementFactory *factory =
1597           g_value_get_object (g_value_array_get_nth (factories, i));
1598       GstCaps *tcaps, *intersection;
1599       const GList *tmps;
1600
1601       GST_DEBUG ("Trying factory %s",
1602           gst_plugin_feature_get_name (GST_PLUGIN_FEATURE (factory)));
1603
1604       if (gst_element_get_factory (src) == factory) {
1605         GST_DEBUG ("Skipping factory");
1606         continue;
1607       }
1608
1609       for (tmps = gst_element_factory_get_static_pad_templates (factory); tmps;
1610           tmps = tmps->next) {
1611         GstStaticPadTemplate *st = (GstStaticPadTemplate *) tmps->data;
1612         if (st->direction != GST_PAD_SINK || st->presence != GST_PAD_ALWAYS)
1613           continue;
1614         tcaps = gst_static_pad_template_get_caps (st);
1615         intersection =
1616             gst_caps_intersect_full (tcaps, caps, GST_CAPS_INTERSECT_FIRST);
1617         filter_caps = gst_caps_merge (filter_caps, intersection);
1618         gst_caps_unref (tcaps);
1619       }
1620     }
1621
1622     /* Append the parser caps to prevent any not-negotiated errors */
1623     filter_caps = gst_caps_merge (filter_caps, gst_caps_ref (caps));
1624
1625     delem->capsfilter = gst_element_factory_make ("capsfilter", NULL);
1626     g_object_set (G_OBJECT (delem->capsfilter), "caps", filter_caps, NULL);
1627     gst_caps_unref (filter_caps);
1628     gst_element_set_state (delem->capsfilter, GST_STATE_PAUSED);
1629     gst_bin_add (GST_BIN_CAST (dbin), gst_object_ref (delem->capsfilter));
1630
1631     gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (dpad), NULL);
1632     p = gst_element_get_static_pad (delem->capsfilter, "sink");
1633     gst_pad_link_full (pad, p, GST_PAD_LINK_CHECK_NOTHING);
1634     gst_object_unref (p);
1635     p = gst_element_get_static_pad (delem->capsfilter, "src");
1636     gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (dpad), p);
1637     pad = p;
1638
1639     if (!gst_caps_is_fixed (caps)) {
1640       g_value_array_free (factories);
1641       goto non_fixed;
1642     }
1643   }
1644
1645   /* 1.h else continue autoplugging something from the list. */
1646   GST_LOG_OBJECT (pad, "Let's continue discovery on this pad");
1647   res = connect_pad (dbin, src, dpad, pad, caps, factories, chain);
1648
1649   /* Need to unref the capsfilter srcpad here if
1650    * we inserted a capsfilter */
1651   if (is_parser_converter)
1652     gst_object_unref (pad);
1653
1654   gst_object_unref (dpad);
1655   g_value_array_free (factories);
1656
1657   if (!res)
1658     goto unknown_type;
1659
1660   return;
1661
1662 expose_pad:
1663   {
1664     GST_LOG_OBJECT (dbin, "Pad is final. autoplug-continue:%d", apcontinue);
1665     expose_pad (dbin, src, dpad, pad, caps, chain);
1666     gst_object_unref (dpad);
1667     return;
1668   }
1669
1670 discarded_type:
1671   {
1672     GST_LOG_OBJECT (pad, "Known type, but discarded because not final caps");
1673     chain->deadend = TRUE;
1674     chain->endcaps = gst_caps_ref (caps);
1675
1676     /* Try to expose anything */
1677     EXPOSE_LOCK (dbin);
1678     if (gst_decode_chain_is_complete (dbin->decode_chain)) {
1679       gst_decode_bin_expose (dbin);
1680     }
1681     EXPOSE_UNLOCK (dbin);
1682     do_async_done (dbin);
1683
1684     return;
1685   }
1686
1687 unknown_type:
1688   {
1689     GST_LOG_OBJECT (pad, "Unknown type, posting message and firing signal");
1690
1691     chain->deadend = TRUE;
1692     chain->endcaps = gst_caps_ref (caps);
1693
1694     gst_element_post_message (GST_ELEMENT_CAST (dbin),
1695         gst_missing_decoder_message_new (GST_ELEMENT_CAST (dbin), caps));
1696
1697     g_signal_emit (G_OBJECT (dbin),
1698         gst_decode_bin_signals[SIGNAL_UNKNOWN_TYPE], 0, pad, caps);
1699
1700     /* Try to expose anything */
1701     EXPOSE_LOCK (dbin);
1702     if (gst_decode_chain_is_complete (dbin->decode_chain)) {
1703       gst_decode_bin_expose (dbin);
1704     }
1705     EXPOSE_UNLOCK (dbin);
1706
1707     if (src == dbin->typefind) {
1708       gchar *desc;
1709
1710       if (caps && !gst_caps_is_empty (caps)) {
1711         desc = gst_pb_utils_get_decoder_description (caps);
1712         GST_ELEMENT_ERROR (dbin, STREAM, CODEC_NOT_FOUND,
1713             (_("A %s plugin is required to play this stream, "
1714                     "but not installed."), desc),
1715             ("No decoder to handle media type '%s'",
1716                 gst_structure_get_name (gst_caps_get_structure (caps, 0))));
1717         g_free (desc);
1718       } else {
1719         GST_ELEMENT_ERROR (dbin, STREAM, TYPE_NOT_FOUND,
1720             (_("Could not determine type of stream")),
1721             ("Stream caps %" GST_PTR_FORMAT, caps));
1722       }
1723       do_async_done (dbin);
1724     }
1725     return;
1726   }
1727 non_fixed:
1728   {
1729     GST_DEBUG_OBJECT (pad, "pad has non-fixed caps delay autoplugging");
1730     gst_object_unref (dpad);
1731     goto setup_caps_delay;
1732   }
1733 any_caps:
1734   {
1735     GST_DEBUG_OBJECT (pad, "pad has ANY caps, delaying auto-pluggin");
1736     goto setup_caps_delay;
1737   }
1738 setup_caps_delay:
1739   {
1740     GstPendingPad *ppad;
1741
1742     /* connect to caps notification */
1743     CHAIN_MUTEX_LOCK (chain);
1744     GST_LOG_OBJECT (dbin, "Chain %p has now %d dynamic pads", chain,
1745         g_list_length (chain->pending_pads));
1746     ppad = g_slice_new0 (GstPendingPad);
1747     ppad->pad = gst_object_ref (pad);
1748     ppad->chain = chain;
1749     ppad->event_probe_id =
1750         gst_pad_add_probe (pad, GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM,
1751         pad_event_cb, ppad, NULL);
1752     chain->pending_pads = g_list_prepend (chain->pending_pads, ppad);
1753     g_signal_connect (G_OBJECT (pad), "notify::caps",
1754         G_CALLBACK (caps_notify_cb), chain);
1755     CHAIN_MUTEX_UNLOCK (chain);
1756
1757     /* If we're here because we have a Parser/Converter
1758      * we have to unref the pad */
1759     if (is_parser_converter)
1760       gst_object_unref (pad);
1761
1762     return;
1763   }
1764 }
1765
1766 static void
1767 add_error_filter (GstDecodeBin * dbin, GstElement * element)
1768 {
1769   GST_OBJECT_LOCK (dbin);
1770   dbin->filtered = g_list_prepend (dbin->filtered, element);
1771   GST_OBJECT_UNLOCK (dbin);
1772 }
1773
1774 static void
1775 remove_error_filter (GstDecodeBin * dbin, GstElement * element)
1776 {
1777   GST_OBJECT_LOCK (dbin);
1778   dbin->filtered = g_list_remove (dbin->filtered, element);
1779   GST_OBJECT_UNLOCK (dbin);
1780 }
1781
1782 /* connect_pad:
1783  *
1784  * Try to connect the given pad to an element created from one of the factories,
1785  * and recursively.
1786  *
1787  * Note that dpad is ghosting pad, and so pad is linked; be sure to unset dpad's
1788  * target before trying to link pad.
1789  *
1790  * Returns TRUE if an element was properly created and linked
1791  */
1792 static gboolean
1793 connect_pad (GstDecodeBin * dbin, GstElement * src, GstDecodePad * dpad,
1794     GstPad * pad, GstCaps * caps, GValueArray * factories,
1795     GstDecodeChain * chain)
1796 {
1797   gboolean res = FALSE;
1798   GstPad *mqpad = NULL;
1799   gboolean is_demuxer = chain->parent && !chain->elements;      /* First pad after the demuxer */
1800
1801   g_return_val_if_fail (factories != NULL, FALSE);
1802   g_return_val_if_fail (factories->n_values > 0, FALSE);
1803
1804   GST_DEBUG_OBJECT (dbin,
1805       "pad %s:%s , chain:%p, %d factories, caps %" GST_PTR_FORMAT,
1806       GST_DEBUG_PAD_NAME (pad), chain, factories->n_values, caps);
1807
1808   /* 1. is element demuxer or parser */
1809   if (is_demuxer) {
1810     GST_LOG_OBJECT (src,
1811         "is a demuxer, connecting the pad through multiqueue '%s'",
1812         GST_OBJECT_NAME (chain->parent->multiqueue));
1813
1814     gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (dpad), NULL);
1815     if (!(mqpad = gst_decode_group_control_demuxer_pad (chain->parent, pad)))
1816       goto beach;
1817     src = chain->parent->multiqueue;
1818     pad = mqpad;
1819     gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (dpad), pad);
1820   }
1821
1822   /* 2. Try to create an element and link to it */
1823   while (factories->n_values > 0) {
1824     GstAutoplugSelectResult ret;
1825     GstElementFactory *factory;
1826     GstDecodeElement *delem;
1827     GstElement *element;
1828     GstPad *sinkpad;
1829     GParamSpec *pspec;
1830     gboolean subtitle;
1831
1832     /* Set dpad target to pad again, it might've been unset
1833      * below but we came back here because something failed
1834      */
1835     gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (dpad), pad);
1836
1837     /* take first factory */
1838     factory = g_value_get_object (g_value_array_get_nth (factories, 0));
1839     /* Remove selected factory from the list. */
1840     g_value_array_remove (factories, 0);
1841
1842     GST_LOG_OBJECT (src, "trying factory %" GST_PTR_FORMAT, factory);
1843
1844     /* Check if the caps are really supported by the factory. The
1845      * factory list is non-empty-subset filtered while caps
1846      * are only accepted by a pad if they are a subset of the
1847      * pad caps.
1848      *
1849      * FIXME: Only do this for fixed caps here. Non-fixed caps
1850      * can happen if a Parser/Converter was autoplugged before
1851      * this. We then assume that it will be able to convert to
1852      * everything that the decoder would want.
1853      *
1854      * A subset check will fail here because the parser caps
1855      * will be generic and while the decoder will only
1856      * support a subset of the parser caps.
1857      */
1858     if (gst_caps_is_fixed (caps)) {
1859       const GList *templs;
1860       gboolean skip = FALSE;
1861
1862       templs = gst_element_factory_get_static_pad_templates (factory);
1863
1864       while (templs) {
1865         GstStaticPadTemplate *templ = (GstStaticPadTemplate *) templs->data;
1866
1867         if (templ->direction == GST_PAD_SINK) {
1868           GstCaps *templcaps = gst_static_caps_get (&templ->static_caps);
1869
1870           if (!gst_caps_is_subset (caps, templcaps)) {
1871             GST_DEBUG_OBJECT (src,
1872                 "caps %" GST_PTR_FORMAT " not subset of %" GST_PTR_FORMAT, caps,
1873                 templcaps);
1874             gst_caps_unref (templcaps);
1875             skip = TRUE;
1876             break;
1877           }
1878
1879           gst_caps_unref (templcaps);
1880         }
1881         templs = g_list_next (templs);
1882       }
1883       if (skip)
1884         continue;
1885     }
1886
1887     /* If the factory is for a parser we first check if the factory
1888      * was already used for the current chain. If it was used already
1889      * we would otherwise create an infinite loop here because the
1890      * parser apparently accepts its own output as input.
1891      * This is only done for parsers because it's perfectly valid
1892      * to have other element classes after each other because a
1893      * parser is the only one that does not change the data. A
1894      * valid example for this would be multiple id3demux in a row.
1895      */
1896     if (strstr (gst_element_factory_get_metadata (factory,
1897                 GST_ELEMENT_METADATA_KLASS), "Parser")) {
1898       gboolean skip = FALSE;
1899       GList *l;
1900
1901       CHAIN_MUTEX_LOCK (chain);
1902       for (l = chain->elements; l; l = l->next) {
1903         GstDecodeElement *delem = (GstDecodeElement *) l->data;
1904         GstElement *otherelement = delem->element;
1905
1906         if (gst_element_get_factory (otherelement) == factory) {
1907           skip = TRUE;
1908           break;
1909         }
1910       }
1911       CHAIN_MUTEX_UNLOCK (chain);
1912       if (skip) {
1913         GST_DEBUG_OBJECT (dbin,
1914             "Skipping factory '%s' because it was already used in this chain",
1915             gst_plugin_feature_get_name (GST_PLUGIN_FEATURE_CAST (factory)));
1916         continue;
1917       }
1918     }
1919
1920     /* emit autoplug-select to see what we should do with it. */
1921     g_signal_emit (G_OBJECT (dbin),
1922         gst_decode_bin_signals[SIGNAL_AUTOPLUG_SELECT],
1923         0, dpad, caps, factory, &ret);
1924
1925     switch (ret) {
1926       case GST_AUTOPLUG_SELECT_TRY:
1927         GST_DEBUG_OBJECT (dbin, "autoplug select requested try");
1928         break;
1929       case GST_AUTOPLUG_SELECT_EXPOSE:
1930         GST_DEBUG_OBJECT (dbin, "autoplug select requested expose");
1931         /* expose the pad, we don't have the source element */
1932         expose_pad (dbin, src, dpad, pad, caps, chain);
1933         res = TRUE;
1934         goto beach;
1935       case GST_AUTOPLUG_SELECT_SKIP:
1936         GST_DEBUG_OBJECT (dbin, "autoplug select requested skip");
1937         continue;
1938       default:
1939         GST_WARNING_OBJECT (dbin, "autoplug select returned unhandled %d", ret);
1940         break;
1941     }
1942
1943     /* 2.0. Unlink pad */
1944     gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (dpad), NULL);
1945
1946     /* 2.1. Try to create an element */
1947     if ((element = gst_element_factory_create (factory, NULL)) == NULL) {
1948       GST_WARNING_OBJECT (dbin, "Could not create an element from %s",
1949           gst_plugin_feature_get_name (GST_PLUGIN_FEATURE (factory)));
1950       continue;
1951     }
1952
1953     /* Filter errors, this will prevent the element from causing the pipeline
1954      * to error while we test it using READY state. */
1955     add_error_filter (dbin, element);
1956
1957     /* ... add it ... */
1958     if (!(gst_bin_add (GST_BIN_CAST (dbin), element))) {
1959       GST_WARNING_OBJECT (dbin, "Couldn't add %s to the bin",
1960           GST_ELEMENT_NAME (element));
1961       remove_error_filter (dbin, element);
1962       gst_object_unref (element);
1963       continue;
1964     }
1965
1966     /* Find its sink pad. */
1967     if (!(sinkpad = find_sink_pad (element))) {
1968       GST_WARNING_OBJECT (dbin, "Element %s doesn't have a sink pad",
1969           GST_ELEMENT_NAME (element));
1970       remove_error_filter (dbin, element);
1971       gst_bin_remove (GST_BIN (dbin), element);
1972       continue;
1973     }
1974
1975     /* ... and try to link */
1976     if ((gst_pad_link (pad, sinkpad)) != GST_PAD_LINK_OK) {
1977       GST_WARNING_OBJECT (dbin, "Link failed on pad %s:%s",
1978           GST_DEBUG_PAD_NAME (sinkpad));
1979       remove_error_filter (dbin, element);
1980       gst_object_unref (sinkpad);
1981       gst_bin_remove (GST_BIN (dbin), element);
1982       continue;
1983     }
1984
1985     /* ... activate it ... */
1986     if ((gst_element_set_state (element,
1987                 GST_STATE_READY)) == GST_STATE_CHANGE_FAILURE) {
1988       GST_WARNING_OBJECT (dbin, "Couldn't set %s to READY",
1989           GST_ELEMENT_NAME (element));
1990       remove_error_filter (dbin, element);
1991       gst_object_unref (sinkpad);
1992       gst_bin_remove (GST_BIN (dbin), element);
1993       continue;
1994     }
1995
1996     /* Stop filtering errors. */
1997     remove_error_filter (dbin, element);
1998
1999     /* check if we still accept the caps on the pad after setting
2000      * the element to READY */
2001     if (!gst_pad_query_accept_caps (sinkpad, caps)) {
2002       GST_WARNING_OBJECT (dbin, "Element %s does not accept caps",
2003           GST_ELEMENT_NAME (element));
2004       gst_element_set_state (element, GST_STATE_NULL);
2005       gst_object_unref (sinkpad);
2006       gst_bin_remove (GST_BIN (dbin), element);
2007       continue;
2008     }
2009
2010     gst_object_unref (sinkpad);
2011     GST_LOG_OBJECT (dbin, "linked on pad %s:%s", GST_DEBUG_PAD_NAME (pad));
2012
2013     CHAIN_MUTEX_LOCK (chain);
2014     delem = g_slice_new (GstDecodeElement);
2015     delem->element = gst_object_ref (element);
2016     delem->capsfilter = NULL;
2017     chain->elements = g_list_prepend (chain->elements, delem);
2018     chain->demuxer = is_demuxer_element (element);
2019     CHAIN_MUTEX_UNLOCK (chain);
2020
2021     /* Set connection-speed property if needed */
2022     if (chain->demuxer == TRUE) {
2023       GParamSpec *pspec;
2024
2025       if ((pspec = g_object_class_find_property (G_OBJECT_GET_CLASS (element),
2026                   "connection-speed"))) {
2027         guint64 speed = dbin->connection_speed / 1000;
2028         gboolean wrong_type = FALSE;
2029
2030         if (G_PARAM_SPEC_TYPE (pspec) == G_TYPE_PARAM_UINT) {
2031           GParamSpecUInt *pspecuint = G_PARAM_SPEC_UINT (pspec);
2032
2033           speed = CLAMP (speed, pspecuint->minimum, pspecuint->maximum);
2034         } else if (G_PARAM_SPEC_TYPE (pspec) == G_TYPE_PARAM_INT) {
2035           GParamSpecInt *pspecint = G_PARAM_SPEC_INT (pspec);
2036
2037           speed = CLAMP (speed, pspecint->minimum, pspecint->maximum);
2038         } else if (G_PARAM_SPEC_TYPE (pspec) == G_TYPE_PARAM_UINT64) {
2039           GParamSpecUInt64 *pspecuint = G_PARAM_SPEC_UINT64 (pspec);
2040
2041           speed = CLAMP (speed, pspecuint->minimum, pspecuint->maximum);
2042         } else if (G_PARAM_SPEC_TYPE (pspec) == G_TYPE_PARAM_INT64) {
2043           GParamSpecInt64 *pspecint = G_PARAM_SPEC_INT64 (pspec);
2044
2045           speed = CLAMP (speed, pspecint->minimum, pspecint->maximum);
2046         } else {
2047           GST_WARNING_OBJECT (dbin,
2048               "The connection speed property %" G_GUINT64_FORMAT " of type %s"
2049               " is not usefull not setting it", speed,
2050               g_type_name (G_PARAM_SPEC_TYPE (pspec)));
2051           wrong_type = TRUE;
2052         }
2053
2054         if (wrong_type == FALSE) {
2055           GST_DEBUG_OBJECT (dbin, "setting connection-speed=%" G_GUINT64_FORMAT
2056               " to demuxer element", speed);
2057
2058           g_object_set (element, "connection-speed", speed, NULL);
2059         }
2060       }
2061     }
2062
2063     /* link this element further */
2064     connect_element (dbin, element, chain);
2065
2066     /* try to configure the subtitle encoding property when we can */
2067     pspec = g_object_class_find_property (G_OBJECT_GET_CLASS (element),
2068         "subtitle-encoding");
2069     if (pspec && G_PARAM_SPEC_VALUE_TYPE (pspec) == G_TYPE_STRING) {
2070       SUBTITLE_LOCK (dbin);
2071       GST_DEBUG_OBJECT (dbin,
2072           "setting subtitle-encoding=%s to element", dbin->encoding);
2073       g_object_set (G_OBJECT (element), "subtitle-encoding", dbin->encoding,
2074           NULL);
2075       SUBTITLE_UNLOCK (dbin);
2076       subtitle = TRUE;
2077     } else {
2078       subtitle = FALSE;
2079     }
2080
2081     /* Bring the element to the state of the parent */
2082     if ((gst_element_set_state (element,
2083                 GST_STATE_PAUSED)) == GST_STATE_CHANGE_FAILURE) {
2084       GstDecodeElement *dtmp = NULL;
2085       GstElement *tmp = NULL;
2086
2087       GST_WARNING_OBJECT (dbin, "Couldn't set %s to PAUSED",
2088           GST_ELEMENT_NAME (element));
2089
2090       /* Remove all elements in this chain that were just added. No
2091        * other thread could've added elements in the meantime */
2092       CHAIN_MUTEX_LOCK (chain);
2093       do {
2094         GList *l;
2095
2096         dtmp = chain->elements->data;
2097         tmp = dtmp->element;
2098
2099         /* Disconnect any signal handlers that might be connected
2100          * in connect_element() or analyze_pad() */
2101         g_signal_handlers_disconnect_by_func (tmp, pad_added_cb, chain);
2102         g_signal_handlers_disconnect_by_func (tmp, pad_removed_cb, chain);
2103         g_signal_handlers_disconnect_by_func (tmp, no_more_pads_cb, chain);
2104
2105         for (l = chain->pending_pads; l;) {
2106           GstPendingPad *pp = l->data;
2107           GList *n;
2108
2109           if (GST_PAD_PARENT (pp->pad) != tmp) {
2110             l = l->next;
2111             continue;
2112           }
2113
2114           g_signal_handlers_disconnect_by_func (pp->pad, caps_notify_cb, chain);
2115           gst_pad_remove_probe (pp->pad, pp->event_probe_id);
2116           gst_object_unref (pp->pad);
2117           g_slice_free (GstPendingPad, pp);
2118
2119           /* Remove element from the list, update list head and go to the
2120            * next element in the list */
2121           n = l->next;
2122           chain->pending_pads = g_list_delete_link (chain->pending_pads, l);
2123           l = n;
2124         }
2125
2126         if (dtmp->capsfilter) {
2127           gst_bin_remove (GST_BIN (dbin), dtmp->capsfilter);
2128           gst_element_set_state (dtmp->capsfilter, GST_STATE_NULL);
2129           gst_object_unref (dtmp->capsfilter);
2130         }
2131
2132         gst_bin_remove (GST_BIN (dbin), tmp);
2133         gst_element_set_state (tmp, GST_STATE_NULL);
2134
2135         gst_object_unref (tmp);
2136         g_slice_free (GstDecodeElement, dtmp);
2137
2138         chain->elements = g_list_delete_link (chain->elements, chain->elements);
2139       } while (tmp != element);
2140       CHAIN_MUTEX_UNLOCK (chain);
2141
2142       continue;
2143     }
2144     if (subtitle) {
2145       SUBTITLE_LOCK (dbin);
2146       /* we added the element now, add it to the list of subtitle-encoding
2147        * elements when we can set the property */
2148       dbin->subtitles = g_list_prepend (dbin->subtitles, element);
2149       SUBTITLE_UNLOCK (dbin);
2150     }
2151
2152     res = TRUE;
2153     break;
2154   }
2155
2156 beach:
2157   if (mqpad)
2158     gst_object_unref (mqpad);
2159
2160   return res;
2161 }
2162
2163 static GstCaps *
2164 get_pad_caps (GstPad * pad)
2165 {
2166   GstCaps *caps;
2167
2168   /* first check the pad caps, if this is set, we are positively sure it is
2169    * fixed and exactly what the element will produce. */
2170   caps = gst_pad_get_current_caps (pad);
2171
2172   /* then use the getcaps function if we don't have caps. These caps might not
2173    * be fixed in some cases, in which case analyze_new_pad will set up a
2174    * notify::caps signal to continue autoplugging. */
2175   if (caps == NULL)
2176     caps = gst_pad_query_caps (pad, NULL);
2177
2178   return caps;
2179 }
2180
2181 static gboolean
2182 connect_element (GstDecodeBin * dbin, GstElement * element,
2183     GstDecodeChain * chain)
2184 {
2185   GList *pads;
2186   gboolean res = TRUE;
2187   gboolean dynamic = FALSE;
2188   GList *to_connect = NULL;
2189
2190   GST_DEBUG_OBJECT (dbin, "Attempting to connect element %s [chain:%p] further",
2191       GST_ELEMENT_NAME (element), chain);
2192
2193   /* 1. Loop over pad templates, grabbing existing pads along the way */
2194   for (pads = GST_ELEMENT_GET_CLASS (element)->padtemplates; pads;
2195       pads = g_list_next (pads)) {
2196     GstPadTemplate *templ = GST_PAD_TEMPLATE (pads->data);
2197     const gchar *templ_name;
2198
2199     /* we are only interested in source pads */
2200     if (GST_PAD_TEMPLATE_DIRECTION (templ) != GST_PAD_SRC)
2201       continue;
2202
2203     templ_name = GST_PAD_TEMPLATE_NAME_TEMPLATE (templ);
2204     GST_DEBUG_OBJECT (dbin, "got a source pad template %s", templ_name);
2205
2206     /* figure out what kind of pad this is */
2207     switch (GST_PAD_TEMPLATE_PRESENCE (templ)) {
2208       case GST_PAD_ALWAYS:
2209       {
2210         /* get the pad that we need to autoplug */
2211         GstPad *pad = gst_element_get_static_pad (element, templ_name);
2212
2213         if (pad) {
2214           GST_DEBUG_OBJECT (dbin, "got the pad for always template %s",
2215               templ_name);
2216           /* here is the pad, we need to autoplug it */
2217           to_connect = g_list_prepend (to_connect, pad);
2218         } else {
2219           /* strange, pad is marked as always but it's not
2220            * there. Fix the element */
2221           GST_WARNING_OBJECT (dbin,
2222               "could not get the pad for always template %s", templ_name);
2223         }
2224         break;
2225       }
2226       case GST_PAD_SOMETIMES:
2227       {
2228         /* try to get the pad to see if it is already created or
2229          * not */
2230         GstPad *pad = gst_element_get_static_pad (element, templ_name);
2231
2232         if (pad) {
2233           GST_DEBUG_OBJECT (dbin, "got the pad for sometimes template %s",
2234               templ_name);
2235           /* the pad is created, we need to autoplug it */
2236           to_connect = g_list_prepend (to_connect, pad);
2237         } else {
2238           GST_DEBUG_OBJECT (dbin,
2239               "did not get the sometimes pad of template %s", templ_name);
2240           /* we have an element that will create dynamic pads */
2241           dynamic = TRUE;
2242         }
2243         break;
2244       }
2245       case GST_PAD_REQUEST:
2246         /* ignore request pads */
2247         GST_DEBUG_OBJECT (dbin, "ignoring request padtemplate %s", templ_name);
2248         break;
2249     }
2250   }
2251
2252   /* 2. if there are more potential pads, connect to relevant signals */
2253   if (dynamic) {
2254     GST_LOG_OBJECT (dbin, "Adding signals to element %s in chain %p",
2255         GST_ELEMENT_NAME (element), chain);
2256     g_signal_connect (G_OBJECT (element), "pad-added",
2257         G_CALLBACK (pad_added_cb), chain);
2258     g_signal_connect (G_OBJECT (element), "pad-removed",
2259         G_CALLBACK (pad_removed_cb), chain);
2260     g_signal_connect (G_OBJECT (element), "no-more-pads",
2261         G_CALLBACK (no_more_pads_cb), chain);
2262   }
2263
2264   /* 3. for every available pad, connect it */
2265   for (pads = to_connect; pads; pads = g_list_next (pads)) {
2266     GstPad *pad = GST_PAD_CAST (pads->data);
2267     GstCaps *caps;
2268
2269     caps = get_pad_caps (pad);
2270     analyze_new_pad (dbin, element, pad, caps, chain);
2271     if (caps)
2272       gst_caps_unref (caps);
2273
2274     gst_object_unref (pad);
2275   }
2276   g_list_free (to_connect);
2277
2278   return res;
2279 }
2280
2281 /* expose_pad:
2282  *
2283  * Expose the given pad on the chain as a decoded pad.
2284  */
2285 static void
2286 expose_pad (GstDecodeBin * dbin, GstElement * src, GstDecodePad * dpad,
2287     GstPad * pad, GstCaps * caps, GstDecodeChain * chain)
2288 {
2289   GstPad *mqpad = NULL;
2290
2291   GST_DEBUG_OBJECT (dbin, "pad %s:%s, chain:%p",
2292       GST_DEBUG_PAD_NAME (pad), chain);
2293
2294   /* If this is the first pad for this chain, there are no other elements
2295    * and the source element is not the multiqueue we must link through the
2296    * multiqueue.
2297    *
2298    * This is the case if a demuxer directly exposed a raw pad.
2299    */
2300   if (chain->parent && !chain->elements && src != chain->parent->multiqueue) {
2301     GST_LOG_OBJECT (src, "connecting the pad through multiqueue");
2302
2303     gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (dpad), NULL);
2304     if (!(mqpad = gst_decode_group_control_demuxer_pad (chain->parent, pad)))
2305       goto beach;
2306     pad = mqpad;
2307     gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (dpad), pad);
2308   }
2309
2310   gst_decode_pad_activate (dpad, chain);
2311   chain->endpad = gst_object_ref (dpad);
2312   chain->endcaps = gst_caps_ref (caps);
2313
2314   EXPOSE_LOCK (dbin);
2315   if (gst_decode_chain_is_complete (dbin->decode_chain)) {
2316     gst_decode_bin_expose (dbin);
2317   }
2318   EXPOSE_UNLOCK (dbin);
2319
2320   if (mqpad)
2321     gst_object_unref (mqpad);
2322
2323 beach:
2324   return;
2325 }
2326
2327 /* check_upstream_seekable:
2328  *
2329  * Check if upstream is seekable.
2330  */
2331 static gboolean
2332 check_upstream_seekable (GstDecodeBin * dbin, GstPad * pad)
2333 {
2334   GstQuery *query;
2335   gint64 start = -1, stop = -1;
2336   gboolean seekable = FALSE;
2337
2338   query = gst_query_new_seeking (GST_FORMAT_BYTES);
2339   if (!gst_pad_peer_query (pad, query)) {
2340     GST_DEBUG_OBJECT (dbin, "seeking query failed");
2341     gst_query_unref (query);
2342     return FALSE;
2343   }
2344
2345   gst_query_parse_seeking (query, NULL, &seekable, &start, &stop);
2346
2347   gst_query_unref (query);
2348
2349   /* try harder to query upstream size if we didn't get it the first time */
2350   if (seekable && stop == -1) {
2351     GST_DEBUG_OBJECT (dbin, "doing duration query to fix up unset stop");
2352     gst_pad_peer_query_duration (pad, GST_FORMAT_BYTES, &stop);
2353   }
2354
2355   /* if upstream doesn't know the size, it's likely that it's not seekable in
2356    * practice even if it technically may be seekable */
2357   if (seekable && (start != 0 || stop <= start)) {
2358     GST_DEBUG_OBJECT (dbin, "seekable but unknown start/stop -> disable");
2359     return FALSE;
2360   }
2361
2362   GST_DEBUG_OBJECT (dbin, "upstream seekable: %d", seekable);
2363   return seekable;
2364 }
2365
2366 static void
2367 type_found (GstElement * typefind, guint probability,
2368     GstCaps * caps, GstDecodeBin * decode_bin)
2369 {
2370   GstPad *pad, *sink_pad;
2371
2372   GST_DEBUG_OBJECT (decode_bin, "typefind found caps %" GST_PTR_FORMAT, caps);
2373
2374   /* If the typefinder (but not something else) finds text/plain - i.e. that's
2375    * the top-level type of the file - then error out.
2376    */
2377   if (gst_structure_has_name (gst_caps_get_structure (caps, 0), "text/plain")) {
2378     GST_ELEMENT_ERROR (decode_bin, STREAM, WRONG_TYPE,
2379         (_("This appears to be a text file")),
2380         ("decodebin cannot decode plain text files"));
2381     goto exit;
2382   }
2383
2384   /* FIXME: we can only deal with one type, we don't yet support dynamically changing
2385    * caps from the typefind element */
2386   if (decode_bin->have_type || decode_bin->decode_chain)
2387     goto exit;
2388
2389   decode_bin->have_type = TRUE;
2390
2391   pad = gst_element_get_static_pad (typefind, "src");
2392   sink_pad = gst_element_get_static_pad (typefind, "sink");
2393
2394   /* need some lock here to prevent race with shutdown state change
2395    * which might yank away e.g. decode_chain while building stuff here.
2396    * In typical cases, STREAM_LOCK is held and handles that, it need not
2397    * be held (if called from a proxied setcaps), so grab it anyway */
2398   GST_PAD_STREAM_LOCK (sink_pad);
2399   decode_bin->decode_chain = gst_decode_chain_new (decode_bin, NULL, pad);
2400   analyze_new_pad (decode_bin, typefind, pad, caps, decode_bin->decode_chain);
2401   GST_PAD_STREAM_UNLOCK (sink_pad);
2402
2403   gst_object_unref (sink_pad);
2404   gst_object_unref (pad);
2405
2406 exit:
2407   return;
2408 }
2409
2410 static GstPadProbeReturn
2411 pad_event_cb (GstPad * pad, GstPadProbeInfo * info, gpointer data)
2412 {
2413   GstEvent *event = GST_PAD_PROBE_INFO_EVENT (info);
2414   GstPendingPad *ppad = (GstPendingPad *) data;
2415   GstDecodeChain *chain = ppad->chain;
2416   GstDecodeBin *dbin = chain->dbin;
2417
2418   g_assert (ppad);
2419   g_assert (chain);
2420   g_assert (dbin);
2421   switch (GST_EVENT_TYPE (event)) {
2422     case GST_EVENT_EOS:
2423       GST_DEBUG_OBJECT (dbin, "Received EOS on a non final pad, this stream "
2424           "ended too early");
2425       chain->deadend = TRUE;
2426       /* we don't set the endcaps because NULL endcaps means early EOS */
2427       EXPOSE_LOCK (dbin);
2428       if (gst_decode_chain_is_complete (dbin->decode_chain))
2429         gst_decode_bin_expose (dbin);
2430       EXPOSE_UNLOCK (dbin);
2431       break;
2432     default:
2433       break;
2434   }
2435   return GST_PAD_PROBE_OK;
2436 }
2437
2438 static void
2439 pad_added_cb (GstElement * element, GstPad * pad, GstDecodeChain * chain)
2440 {
2441   GstCaps *caps;
2442   GstDecodeBin *dbin;
2443
2444   dbin = chain->dbin;
2445
2446   GST_DEBUG_OBJECT (pad, "pad added, chain:%p", chain);
2447
2448   caps = get_pad_caps (pad);
2449   analyze_new_pad (dbin, element, pad, caps, chain);
2450   if (caps)
2451     gst_caps_unref (caps);
2452
2453   EXPOSE_LOCK (dbin);
2454   if (gst_decode_chain_is_complete (dbin->decode_chain)) {
2455     GST_LOG_OBJECT (dbin,
2456         "That was the last dynamic object, now attempting to expose the group");
2457     if (!gst_decode_bin_expose (dbin))
2458       GST_WARNING_OBJECT (dbin, "Couldn't expose group");
2459   }
2460   EXPOSE_UNLOCK (dbin);
2461 }
2462
2463 static void
2464 pad_removed_cb (GstElement * element, GstPad * pad, GstDecodeChain * chain)
2465 {
2466   GList *l;
2467
2468   GST_LOG_OBJECT (pad, "pad removed, chain:%p", chain);
2469
2470   /* In fact, we don't have to do anything here, the active group will be
2471    * removed when the group's multiqueue is drained */
2472   CHAIN_MUTEX_LOCK (chain);
2473   for (l = chain->pending_pads; l; l = l->next) {
2474     GstPendingPad *ppad = l->data;
2475     GstPad *opad = ppad->pad;
2476
2477     if (pad == opad) {
2478       g_signal_handlers_disconnect_by_func (pad, caps_notify_cb, chain);
2479       gst_pending_pad_free (ppad);
2480       chain->pending_pads = g_list_delete_link (chain->pending_pads, l);
2481       break;
2482     }
2483   }
2484   CHAIN_MUTEX_UNLOCK (chain);
2485 }
2486
2487 static void
2488 no_more_pads_cb (GstElement * element, GstDecodeChain * chain)
2489 {
2490   GstDecodeGroup *group = NULL;
2491
2492   GST_LOG_OBJECT (element, "got no more pads");
2493
2494   CHAIN_MUTEX_LOCK (chain);
2495   if (!chain->elements
2496       || ((GstDecodeElement *) chain->elements->data)->element != element) {
2497     GST_LOG_OBJECT (chain->dbin, "no-more-pads from old chain element '%s'",
2498         GST_OBJECT_NAME (element));
2499     CHAIN_MUTEX_UNLOCK (chain);
2500     return;
2501   } else if (!chain->demuxer) {
2502     GST_LOG_OBJECT (chain->dbin, "no-more-pads from a non-demuxer element '%s'",
2503         GST_OBJECT_NAME (element));
2504     CHAIN_MUTEX_UNLOCK (chain);
2505     return;
2506   }
2507
2508   /* when we received no_more_pads, we can complete the pads of the chain */
2509   if (!chain->next_groups && chain->active_group) {
2510     group = chain->active_group;
2511   } else if (chain->next_groups) {
2512     group = chain->next_groups->data;
2513   }
2514   if (!group) {
2515     GST_ERROR_OBJECT (chain->dbin, "can't find group for element");
2516     CHAIN_MUTEX_UNLOCK (chain);
2517     return;
2518   }
2519
2520   GST_DEBUG_OBJECT (element, "Setting group %p to complete", group);
2521
2522   group->no_more_pads = TRUE;
2523   /* this group has prerolled enough to not need more pads,
2524    * we can probably set its buffering state to playing now */
2525   GST_DEBUG_OBJECT (group->dbin, "Setting group %p multiqueue to "
2526       "'playing' buffering mode", group);
2527   decodebin_set_queue_size (group->dbin, group->multiqueue, FALSE,
2528       (group->parent ? group->parent->seekable : TRUE));
2529   CHAIN_MUTEX_UNLOCK (chain);
2530
2531   EXPOSE_LOCK (chain->dbin);
2532   if (gst_decode_chain_is_complete (chain->dbin->decode_chain)) {
2533     gst_decode_bin_expose (chain->dbin);
2534   }
2535   EXPOSE_UNLOCK (chain->dbin);
2536 }
2537
2538 static void
2539 caps_notify_cb (GstPad * pad, GParamSpec * unused, GstDecodeChain * chain)
2540 {
2541   GstElement *element;
2542   GList *l;
2543
2544   GST_LOG_OBJECT (pad, "Notified caps for pad %s:%s", GST_DEBUG_PAD_NAME (pad));
2545
2546   /* Disconnect this; if we still need it, we'll reconnect to this in
2547    * analyze_new_pad */
2548   g_signal_handlers_disconnect_by_func (pad, caps_notify_cb, chain);
2549
2550   element = GST_ELEMENT_CAST (gst_pad_get_parent (pad));
2551
2552   CHAIN_MUTEX_LOCK (chain);
2553   for (l = chain->pending_pads; l; l = l->next) {
2554     GstPendingPad *ppad = l->data;
2555     if (ppad->pad == pad) {
2556       gst_pending_pad_free (ppad);
2557       chain->pending_pads = g_list_delete_link (chain->pending_pads, l);
2558       break;
2559     }
2560   }
2561   CHAIN_MUTEX_UNLOCK (chain);
2562
2563   pad_added_cb (element, pad, chain);
2564
2565   gst_object_unref (element);
2566 }
2567
2568 /* Decide whether an element is a demuxer based on the
2569  * klass and number/type of src pad templates it has */
2570 static gboolean
2571 is_demuxer_element (GstElement * srcelement)
2572 {
2573   GstElementFactory *srcfactory;
2574   GstElementClass *elemclass;
2575   GList *walk;
2576   const gchar *klass;
2577   gint potential_src_pads = 0;
2578
2579   srcfactory = gst_element_get_factory (srcelement);
2580   klass =
2581       gst_element_factory_get_metadata (srcfactory, GST_ELEMENT_METADATA_KLASS);
2582
2583   /* Can't be a demuxer unless it has Demux in the klass name */
2584   if (!strstr (klass, "Demux"))
2585     return FALSE;
2586
2587   /* Walk the src pad templates and count how many the element
2588    * might produce */
2589   elemclass = GST_ELEMENT_GET_CLASS (srcelement);
2590
2591   walk = gst_element_class_get_pad_template_list (elemclass);
2592   while (walk != NULL) {
2593     GstPadTemplate *templ;
2594
2595     templ = (GstPadTemplate *) walk->data;
2596     if (GST_PAD_TEMPLATE_DIRECTION (templ) == GST_PAD_SRC) {
2597       switch (GST_PAD_TEMPLATE_PRESENCE (templ)) {
2598         case GST_PAD_ALWAYS:
2599         case GST_PAD_SOMETIMES:
2600           if (strstr (GST_PAD_TEMPLATE_NAME_TEMPLATE (templ), "%"))
2601             potential_src_pads += 2;    /* Might make multiple pads */
2602           else
2603             potential_src_pads += 1;
2604           break;
2605         case GST_PAD_REQUEST:
2606           potential_src_pads += 2;
2607           break;
2608       }
2609     }
2610     walk = g_list_next (walk);
2611   }
2612
2613   if (potential_src_pads < 2)
2614     return FALSE;
2615
2616   return TRUE;
2617 }
2618
2619 /* Returns TRUE if the caps are compatible with the caps specified in the 'caps'
2620  * property (which by default are the raw caps)
2621  *
2622  * The decodebin_lock should be taken !
2623  */
2624 static gboolean
2625 are_final_caps (GstDecodeBin * dbin, GstCaps * caps)
2626 {
2627   gboolean res;
2628
2629   GST_LOG_OBJECT (dbin, "Checking with caps %" GST_PTR_FORMAT, caps);
2630
2631   /* lock for getting the caps */
2632   GST_OBJECT_LOCK (dbin);
2633   res = gst_caps_can_intersect (dbin->caps, caps);
2634   GST_OBJECT_UNLOCK (dbin);
2635
2636   GST_LOG_OBJECT (dbin, "Caps are %sfinal caps", res ? "" : "not ");
2637
2638   return res;
2639 }
2640
2641 /****
2642  * GstDecodeChain functions
2643  ****/
2644
2645 /* gst_decode_chain_get_current_group:
2646  *
2647  * Returns the current group of this chain, to which
2648  * new chains should be attached or NULL if the last
2649  * group didn't have no-more-pads.
2650  *
2651  * Not MT-safe: Call with parent chain lock!
2652  */
2653 static GstDecodeGroup *
2654 gst_decode_chain_get_current_group (GstDecodeChain * chain)
2655 {
2656   GstDecodeGroup *group;
2657
2658   if (!chain->next_groups && chain->active_group
2659       && chain->active_group->overrun && !chain->active_group->no_more_pads) {
2660     GST_WARNING_OBJECT (chain->dbin,
2661         "Currently active group %p is exposed"
2662         " and wants to add a new pad without having signaled no-more-pads",
2663         chain->active_group);
2664     return NULL;
2665   }
2666
2667   if (chain->next_groups && (group = chain->next_groups->data) && group->overrun
2668       && !group->no_more_pads) {
2669     GST_WARNING_OBJECT (chain->dbin,
2670         "Currently newest pending group %p "
2671         "had overflow but didn't signal no-more-pads", group);
2672     return NULL;
2673   }
2674
2675   /* Now we know that we can really return something useful */
2676   if (!chain->active_group) {
2677     chain->active_group = group = gst_decode_group_new (chain->dbin, chain);
2678   } else if (!chain->active_group->overrun
2679       && !chain->active_group->no_more_pads) {
2680     group = chain->active_group;
2681   } else if (chain->next_groups && (group = chain->next_groups->data)
2682       && !group->overrun && !group->no_more_pads) {
2683     /* group = chain->next_groups->data */
2684   } else {
2685     group = gst_decode_group_new (chain->dbin, chain);
2686     chain->next_groups = g_list_prepend (chain->next_groups, group);
2687   }
2688
2689   return group;
2690 }
2691
2692 static void gst_decode_group_free_internal (GstDecodeGroup * group,
2693     gboolean hide);
2694
2695 static void
2696 gst_decode_chain_free_internal (GstDecodeChain * chain, gboolean hide)
2697 {
2698   GList *l;
2699
2700   CHAIN_MUTEX_LOCK (chain);
2701
2702   GST_DEBUG_OBJECT (chain->dbin, "%s chain %p", (hide ? "Hiding" : "Freeing"),
2703       chain);
2704
2705   if (chain->active_group) {
2706     gst_decode_group_free_internal (chain->active_group, hide);
2707     if (!hide)
2708       chain->active_group = NULL;
2709   }
2710
2711   for (l = chain->next_groups; l; l = l->next) {
2712     gst_decode_group_free_internal ((GstDecodeGroup *) l->data, hide);
2713     if (!hide)
2714       l->data = NULL;
2715   }
2716   if (!hide) {
2717     g_list_free (chain->next_groups);
2718     chain->next_groups = NULL;
2719   }
2720
2721   if (!hide) {
2722     for (l = chain->old_groups; l; l = l->next) {
2723       GstDecodeGroup *group = l->data;
2724
2725       gst_decode_group_free (group);
2726     }
2727     g_list_free (chain->old_groups);
2728     chain->old_groups = NULL;
2729   }
2730
2731   for (l = chain->pending_pads; l; l = l->next) {
2732     GstPendingPad *ppad = l->data;
2733     GstPad *pad = ppad->pad;
2734
2735     g_signal_handlers_disconnect_by_func (pad, caps_notify_cb, chain);
2736     gst_pending_pad_free (ppad);
2737     l->data = NULL;
2738   }
2739   g_list_free (chain->pending_pads);
2740   chain->pending_pads = NULL;
2741
2742   for (l = chain->elements; l; l = l->next) {
2743     GstDecodeElement *delem = l->data;
2744     GstElement *element = delem->element;
2745
2746     g_signal_handlers_disconnect_by_func (element, pad_added_cb, chain);
2747     g_signal_handlers_disconnect_by_func (element, pad_removed_cb, chain);
2748     g_signal_handlers_disconnect_by_func (element, no_more_pads_cb, chain);
2749
2750     if (delem->capsfilter) {
2751       if (GST_OBJECT_PARENT (delem->capsfilter) ==
2752           GST_OBJECT_CAST (chain->dbin))
2753         gst_bin_remove (GST_BIN_CAST (chain->dbin), delem->capsfilter);
2754       if (!hide) {
2755         gst_element_set_state (delem->capsfilter, GST_STATE_NULL);
2756       }
2757     }
2758
2759     if (GST_OBJECT_PARENT (element) == GST_OBJECT_CAST (chain->dbin))
2760       gst_bin_remove (GST_BIN_CAST (chain->dbin), element);
2761     if (!hide) {
2762       gst_element_set_state (element, GST_STATE_NULL);
2763     }
2764
2765     SUBTITLE_LOCK (chain->dbin);
2766     /* remove possible subtitle element */
2767     chain->dbin->subtitles = g_list_remove (chain->dbin->subtitles, element);
2768     SUBTITLE_UNLOCK (chain->dbin);
2769
2770     if (!hide) {
2771       if (delem->capsfilter) {
2772         gst_object_unref (delem->capsfilter);
2773         delem->capsfilter = NULL;
2774       }
2775
2776       gst_object_unref (element);
2777       l->data = NULL;
2778
2779       g_slice_free (GstDecodeElement, delem);
2780     }
2781   }
2782   if (!hide) {
2783     g_list_free (chain->elements);
2784     chain->elements = NULL;
2785   }
2786
2787   if (chain->endpad) {
2788     if (chain->endpad->exposed) {
2789       gst_element_remove_pad (GST_ELEMENT_CAST (chain->dbin),
2790           GST_PAD_CAST (chain->endpad));
2791     }
2792
2793     gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (chain->endpad), NULL);
2794     chain->endpad->exposed = FALSE;
2795     if (!hide) {
2796       gst_object_unref (chain->endpad);
2797       chain->endpad = NULL;
2798     }
2799   }
2800
2801   if (chain->pad) {
2802     gst_object_unref (chain->pad);
2803     chain->pad = NULL;
2804   }
2805
2806   if (chain->endcaps) {
2807     gst_caps_unref (chain->endcaps);
2808     chain->endcaps = NULL;
2809   }
2810
2811   GST_DEBUG_OBJECT (chain->dbin, "%s chain %p", (hide ? "Hidden" : "Freed"),
2812       chain);
2813   CHAIN_MUTEX_UNLOCK (chain);
2814   if (!hide) {
2815     g_mutex_clear (&chain->lock);
2816     g_slice_free (GstDecodeChain, chain);
2817   }
2818 }
2819
2820 /* gst_decode_chain_free:
2821  *
2822  * Completely frees and removes the chain and all
2823  * child groups from decodebin.
2824  *
2825  * MT-safe, don't hold the chain lock or any child chain's lock
2826  * when calling this!
2827  */
2828 static void
2829 gst_decode_chain_free (GstDecodeChain * chain)
2830 {
2831   gst_decode_chain_free_internal (chain, FALSE);
2832 }
2833
2834 /* gst_decode_chain_new:
2835  *
2836  * Creates a new decode chain and initializes it.
2837  *
2838  * It's up to the caller to add it to the list of child chains of
2839  * a group!
2840  */
2841 static GstDecodeChain *
2842 gst_decode_chain_new (GstDecodeBin * dbin, GstDecodeGroup * parent,
2843     GstPad * pad)
2844 {
2845   GstDecodeChain *chain = g_slice_new0 (GstDecodeChain);
2846
2847   GST_DEBUG_OBJECT (dbin, "Creating new chain %p with parent group %p", chain,
2848       parent);
2849
2850   chain->dbin = dbin;
2851   chain->parent = parent;
2852   g_mutex_init (&chain->lock);
2853   chain->pad = gst_object_ref (pad);
2854
2855   return chain;
2856 }
2857
2858 /****
2859  * GstDecodeGroup functions
2860  ****/
2861
2862 /* The overrun callback is used to expose groups that have not yet had their
2863  * no_more_pads called while the (large) multiqueue overflowed. When this
2864  * happens we must assume that the no_more_pads will not arrive anymore and we
2865  * must expose the pads that we have.
2866  */
2867 static void
2868 multi_queue_overrun_cb (GstElement * queue, GstDecodeGroup * group)
2869 {
2870   GstDecodeBin *dbin;
2871
2872   dbin = group->dbin;
2873
2874   GST_LOG_OBJECT (dbin, "multiqueue '%s' (%p) is full", GST_OBJECT_NAME (queue),
2875       queue);
2876
2877   group->overrun = TRUE;
2878   /* this group has prerolled enough to not need more pads,
2879    * we can probably set its buffering state to playing now */
2880   GST_DEBUG_OBJECT (group->dbin, "Setting group %p multiqueue to "
2881       "'playing' buffering mode", group);
2882   decodebin_set_queue_size (group->dbin, group->multiqueue, FALSE,
2883       (group->parent ? group->parent->seekable : TRUE));
2884
2885   /* FIXME: We should make sure that everything gets exposed now
2886    * even if child chains are not complete because the will never
2887    * be complete! Ignore any non-complete chains when exposing
2888    * and never expose them later
2889    */
2890
2891   EXPOSE_LOCK (dbin);
2892   if (gst_decode_chain_is_complete (dbin->decode_chain)) {
2893     if (!gst_decode_bin_expose (dbin))
2894       GST_WARNING_OBJECT (dbin, "Couldn't expose group");
2895   }
2896   EXPOSE_UNLOCK (group->dbin);
2897 }
2898
2899 static void
2900 gst_decode_group_free_internal (GstDecodeGroup * group, gboolean hide)
2901 {
2902   GList *l;
2903
2904   GST_DEBUG_OBJECT (group->dbin, "%s group %p", (hide ? "Hiding" : "Freeing"),
2905       group);
2906   for (l = group->children; l; l = l->next) {
2907     GstDecodeChain *chain = (GstDecodeChain *) l->data;
2908
2909     gst_decode_chain_free_internal (chain, hide);
2910     if (!hide)
2911       l->data = NULL;
2912   }
2913   if (!hide) {
2914     g_list_free (group->children);
2915     group->children = NULL;
2916   }
2917
2918   if (!hide) {
2919     for (l = group->reqpads; l; l = l->next) {
2920       GstPad *pad = l->data;
2921
2922       gst_element_release_request_pad (group->multiqueue, pad);
2923       gst_object_unref (pad);
2924       l->data = NULL;
2925     }
2926     g_list_free (group->reqpads);
2927     group->reqpads = NULL;
2928   }
2929
2930   if (group->multiqueue) {
2931     if (group->overrunsig) {
2932       g_signal_handler_disconnect (group->multiqueue, group->overrunsig);
2933       group->overrunsig = 0;
2934     }
2935
2936     if (GST_OBJECT_PARENT (group->multiqueue) == GST_OBJECT_CAST (group->dbin))
2937       gst_bin_remove (GST_BIN_CAST (group->dbin), group->multiqueue);
2938     if (!hide) {
2939       gst_element_set_state (group->multiqueue, GST_STATE_NULL);
2940       gst_object_unref (group->multiqueue);
2941       group->multiqueue = NULL;
2942     }
2943   }
2944
2945   GST_DEBUG_OBJECT (group->dbin, "%s group %p", (hide ? "Hided" : "Freed"),
2946       group);
2947   if (!hide)
2948     g_slice_free (GstDecodeGroup, group);
2949 }
2950
2951 /* gst_decode_group_free:
2952  *
2953  * Completely frees and removes the decode group and all
2954  * it's children.
2955  *
2956  * Never call this from any streaming thread!
2957  *
2958  * Not MT-safe, call with parent's chain lock!
2959  */
2960 static void
2961 gst_decode_group_free (GstDecodeGroup * group)
2962 {
2963   gst_decode_group_free_internal (group, FALSE);
2964 }
2965
2966 /* gst_decode_group_hide:
2967  *
2968  * Hide the decode group only, this means that
2969  * all child endpads are removed from decodebin
2970  * and all signals are unconnected.
2971  *
2972  * No element is set to NULL state and completely
2973  * unrefed here.
2974  *
2975  * Can be called from streaming threads.
2976  *
2977  * Not MT-safe, call with parent's chain lock!
2978  */
2979 static void
2980 gst_decode_group_hide (GstDecodeGroup * group)
2981 {
2982   gst_decode_group_free_internal (group, TRUE);
2983 }
2984
2985 /* configure queue sizes, this depends on the buffering method and if we are
2986  * playing or prerolling. */
2987 static void
2988 decodebin_set_queue_size (GstDecodeBin * dbin, GstElement * multiqueue,
2989     gboolean preroll, gboolean seekable)
2990 {
2991   guint max_bytes, max_buffers;
2992   guint64 max_time;
2993
2994   if (preroll || dbin->use_buffering) {
2995     /* takes queue limits, initially we only queue up up to the max bytes limit,
2996      * with a default of 2MB. we use the same values for buffering mode. */
2997     if ((max_bytes = dbin->max_size_bytes) == 0)
2998       max_bytes = AUTO_PREROLL_SIZE_BYTES;
2999     if ((max_buffers = dbin->max_size_buffers) == 0)
3000       max_buffers = AUTO_PREROLL_SIZE_BUFFERS;
3001     if ((max_time = dbin->max_size_time) == 0)
3002       max_time = seekable ? AUTO_PREROLL_SEEKABLE_SIZE_TIME :
3003           AUTO_PREROLL_NOT_SEEKABLE_SIZE_TIME;
3004   } else {
3005     /* update runtime limits. At runtime, we try to keep the amount of buffers
3006      * in the queues as low as possible (but at least 5 buffers). */
3007     if ((max_bytes = dbin->max_size_bytes) == 0)
3008       max_bytes = AUTO_PLAY_SIZE_BYTES;
3009     if ((max_buffers = dbin->max_size_buffers) == 0)
3010       max_buffers = AUTO_PLAY_SIZE_BUFFERS;
3011     if ((max_time = dbin->max_size_time) == 0)
3012       max_time = AUTO_PLAY_SIZE_TIME;
3013   }
3014
3015   g_object_set (multiqueue,
3016       "max-size-bytes", max_bytes, "max-size-time", max_time,
3017       "max-size-buffers", max_buffers, NULL);
3018 }
3019
3020 /* gst_decode_group_new:
3021  * @dbin: Parent decodebin
3022  * @parent: Parent chain or %NULL
3023  *
3024  * Creates a new GstDecodeGroup. It is up to the caller to add it to the list
3025  * of groups.
3026  */
3027 static GstDecodeGroup *
3028 gst_decode_group_new (GstDecodeBin * dbin, GstDecodeChain * parent)
3029 {
3030   GstDecodeGroup *group = g_slice_new0 (GstDecodeGroup);
3031   GstElement *mq;
3032   gboolean seekable;
3033
3034   GST_DEBUG_OBJECT (dbin, "Creating new group %p with parent chain %p", group,
3035       parent);
3036
3037   group->dbin = dbin;
3038   group->parent = parent;
3039
3040   mq = group->multiqueue = gst_element_factory_make ("multiqueue", NULL);
3041   if (G_UNLIKELY (!group->multiqueue))
3042     goto missing_multiqueue;
3043
3044   /* default is for use-buffering is FALSE */
3045   if (dbin->use_buffering) {
3046     g_object_set (mq,
3047         "use-buffering", TRUE,
3048         "low-percent", dbin->low_percent,
3049         "high-percent", dbin->high_percent, NULL);
3050   }
3051
3052   /* configure queue sizes for preroll */
3053   seekable = FALSE;
3054   if (parent && parent->demuxer) {
3055     GstElement *element =
3056         ((GstDecodeElement *) parent->elements->data)->element;
3057     GstPad *pad = gst_element_get_static_pad (element, "sink");
3058     if (pad) {
3059       seekable = parent->seekable = check_upstream_seekable (dbin, pad);
3060       gst_object_unref (pad);
3061     }
3062   }
3063   decodebin_set_queue_size (dbin, mq, TRUE, seekable);
3064
3065   group->overrunsig = g_signal_connect (G_OBJECT (mq), "overrun",
3066       G_CALLBACK (multi_queue_overrun_cb), group);
3067
3068   gst_bin_add (GST_BIN (dbin), gst_object_ref (mq));
3069   gst_element_set_state (mq, GST_STATE_PAUSED);
3070
3071   return group;
3072
3073   /* ERRORS */
3074 missing_multiqueue:
3075   {
3076     gst_element_post_message (GST_ELEMENT_CAST (dbin),
3077         gst_missing_element_message_new (GST_ELEMENT_CAST (dbin),
3078             "multiqueue"));
3079     GST_ELEMENT_ERROR (dbin, CORE, MISSING_PLUGIN, (NULL), ("no multiqueue!"));
3080     g_slice_free (GstDecodeGroup, group);
3081     return NULL;
3082   }
3083 }
3084
3085 /* gst_decode_group_control_demuxer_pad
3086  *
3087  * Adds a new demuxer srcpad to the given group.
3088  *
3089  * Returns the srcpad of the multiqueue corresponding the given pad.
3090  * Returns NULL if there was an error.
3091  */
3092 static GstPad *
3093 gst_decode_group_control_demuxer_pad (GstDecodeGroup * group, GstPad * pad)
3094 {
3095   GstDecodeBin *dbin;
3096   GstPad *srcpad, *sinkpad;
3097   GstIterator *it = NULL;
3098   GValue item = { 0, };
3099
3100   dbin = group->dbin;
3101
3102   GST_LOG_OBJECT (dbin, "group:%p pad %s:%s", group, GST_DEBUG_PAD_NAME (pad));
3103
3104   srcpad = NULL;
3105
3106   if (G_UNLIKELY (!group->multiqueue))
3107     return NULL;
3108
3109   if (!(sinkpad = gst_element_get_request_pad (group->multiqueue, "sink_%u"))) {
3110     GST_ERROR_OBJECT (dbin, "Couldn't get sinkpad from multiqueue");
3111     return NULL;
3112   }
3113
3114   if ((gst_pad_link (pad, sinkpad) != GST_PAD_LINK_OK)) {
3115     GST_ERROR_OBJECT (dbin, "Couldn't link demuxer and multiqueue");
3116     goto error;
3117   }
3118
3119   it = gst_pad_iterate_internal_links (sinkpad);
3120
3121   if (!it || (gst_iterator_next (it, &item)) != GST_ITERATOR_OK
3122       || ((srcpad = g_value_dup_object (&item)) == NULL)) {
3123     GST_ERROR_OBJECT (dbin,
3124         "Couldn't get srcpad from multiqueue for sinkpad %" GST_PTR_FORMAT,
3125         sinkpad);
3126     goto error;
3127   }
3128   CHAIN_MUTEX_LOCK (group->parent);
3129   group->reqpads = g_list_prepend (group->reqpads, gst_object_ref (sinkpad));
3130   CHAIN_MUTEX_UNLOCK (group->parent);
3131
3132 beach:
3133   if (G_IS_VALUE (&item))
3134     g_value_unset (&item);
3135   if (it)
3136     gst_iterator_free (it);
3137   gst_object_unref (sinkpad);
3138   return srcpad;
3139
3140 error:
3141   gst_element_release_request_pad (group->multiqueue, sinkpad);
3142   goto beach;
3143 }
3144
3145 /* gst_decode_group_is_complete:
3146  *
3147  * Checks if the group is complete, this means that
3148  * a) overrun of the multiqueue or no-more-pads happened
3149  * b) all child chains are complete
3150  *
3151  * Not MT-safe, always call with decodebin expose lock
3152  */
3153 static gboolean
3154 gst_decode_group_is_complete (GstDecodeGroup * group)
3155 {
3156   GList *l;
3157   gboolean complete = TRUE;
3158
3159   if (!group->overrun && !group->no_more_pads) {
3160     complete = FALSE;
3161     goto out;
3162   }
3163
3164   for (l = group->children; l; l = l->next) {
3165     GstDecodeChain *chain = l->data;
3166
3167     if (!gst_decode_chain_is_complete (chain)) {
3168       complete = FALSE;
3169       goto out;
3170     }
3171   }
3172
3173 out:
3174   GST_DEBUG_OBJECT (group->dbin, "Group %p is complete: %d", group, complete);
3175   return complete;
3176 }
3177
3178 /* gst_decode_chain_is_complete:
3179  *
3180  * Returns TRUE if the chain is complete, this means either
3181  * a) This chain is a dead end, i.e. we have no suitable plugins
3182  * b) This chain ends in an endpad and this is blocked or exposed
3183  *
3184  * Not MT-safe, always call with decodebin expose lock
3185  */
3186 static gboolean
3187 gst_decode_chain_is_complete (GstDecodeChain * chain)
3188 {
3189   gboolean complete = FALSE;
3190
3191   CHAIN_MUTEX_LOCK (chain);
3192   if (chain->deadend) {
3193     complete = TRUE;
3194     goto out;
3195   }
3196
3197   if (chain->endpad && (chain->endpad->blocked || chain->endpad->exposed)) {
3198     complete = TRUE;
3199     goto out;
3200   }
3201
3202   if (chain->demuxer) {
3203     if (chain->active_group
3204         && gst_decode_group_is_complete (chain->active_group)) {
3205       complete = TRUE;
3206       goto out;
3207     }
3208   }
3209
3210 out:
3211   CHAIN_MUTEX_UNLOCK (chain);
3212   GST_DEBUG_OBJECT (chain->dbin, "Chain %p is complete: %d", chain, complete);
3213   return complete;
3214 }
3215
3216 static gboolean
3217 drain_and_switch_chains (GstDecodeChain * chain, GstDecodePad * drainpad,
3218     gboolean * last_group, gboolean * drained, gboolean * switched);
3219 /* drain_and_switch_chains/groups:
3220  *
3221  * CALL WITH CHAIN LOCK (or group parent) TAKEN !
3222  *
3223  * Goes down the chains/groups until it finds the chain
3224  * to which the drainpad belongs.
3225  *
3226  * It marks that pad/chain as drained and then will figure
3227  * out which group to switch to or not.
3228  *
3229  * last_chain will be set to TRUE if the group to which the
3230  * pad belongs is the last one.
3231  *
3232  * drained will be set to TRUE if the chain/group is drained.
3233  *
3234  * Returns: TRUE if the chain contained the target pad */
3235 static gboolean
3236 drain_and_switch_group (GstDecodeGroup * group, GstDecodePad * drainpad,
3237     gboolean * last_group, gboolean * drained, gboolean * switched)
3238 {
3239   gboolean handled = FALSE;
3240   GList *tmp;
3241
3242   GST_DEBUG ("Checking group %p (target pad %s:%s)",
3243       group, GST_DEBUG_PAD_NAME (drainpad));
3244
3245   /* Definitely can't be in drained groups */
3246   if (G_UNLIKELY (group->drained)) {
3247     goto beach;
3248   }
3249
3250   /* Figure out if all our chains are drained with the
3251    * new information */
3252   group->drained = TRUE;
3253   for (tmp = group->children; tmp; tmp = tmp->next) {
3254     GstDecodeChain *chain = (GstDecodeChain *) tmp->data;
3255     gboolean subdrained = FALSE;
3256
3257     handled |=
3258         drain_and_switch_chains (chain, drainpad, last_group, &subdrained,
3259         switched);
3260     if (!subdrained)
3261       group->drained = FALSE;
3262   }
3263
3264 beach:
3265   GST_DEBUG ("group %p (last_group:%d, drained:%d, switched:%d, handled:%d)",
3266       group, *last_group, group->drained, *switched, handled);
3267   *drained = group->drained;
3268   return handled;
3269 }
3270
3271 static gboolean
3272 drain_and_switch_chains (GstDecodeChain * chain, GstDecodePad * drainpad,
3273     gboolean * last_group, gboolean * drained, gboolean * switched)
3274 {
3275   gboolean handled = FALSE;
3276   GstDecodeBin *dbin = chain->dbin;
3277
3278   GST_DEBUG ("Checking chain %p (target pad %s:%s)",
3279       chain, GST_DEBUG_PAD_NAME (drainpad));
3280
3281   CHAIN_MUTEX_LOCK (chain);
3282
3283   /* Definitely can't be in drained chains */
3284   if (G_UNLIKELY (chain->drained)) {
3285     goto beach;
3286   }
3287
3288   if (chain->endpad) {
3289     /* Check if we're reached the target endchain */
3290     if (chain == drainpad->chain) {
3291       GST_DEBUG ("Found the target chain");
3292       drainpad->drained = TRUE;
3293       handled = TRUE;
3294     }
3295
3296     chain->drained = chain->endpad->drained;
3297     goto beach;
3298   }
3299
3300   /* We known there are groups to switch to */
3301   if (chain->next_groups)
3302     *last_group = FALSE;
3303
3304   /* Check the active group */
3305   if (chain->active_group) {
3306     gboolean subdrained = FALSE;
3307     handled = drain_and_switch_group (chain->active_group, drainpad,
3308         last_group, &subdrained, switched);
3309
3310     /* The group is drained, see if we can switch to another */
3311     if (handled && subdrained && !*switched) {
3312       if (chain->next_groups) {
3313         /* Switch to next group */
3314         GST_DEBUG_OBJECT (dbin, "Hiding current group %p", chain->active_group);
3315         gst_decode_group_hide (chain->active_group);
3316         chain->old_groups =
3317             g_list_prepend (chain->old_groups, chain->active_group);
3318         GST_DEBUG_OBJECT (dbin, "Switching to next group %p",
3319             chain->next_groups->data);
3320         chain->active_group = chain->next_groups->data;
3321         chain->next_groups =
3322             g_list_delete_link (chain->next_groups, chain->next_groups);
3323         *switched = TRUE;
3324         chain->drained = FALSE;
3325       } else {
3326         GST_DEBUG ("Group %p was the last in chain %p", chain->active_group,
3327             chain);
3328         chain->drained = TRUE;
3329         /* We're drained ! */
3330       }
3331     }
3332   }
3333
3334 beach:
3335   CHAIN_MUTEX_UNLOCK (chain);
3336
3337   GST_DEBUG ("Chain %p (handled:%d, last_group:%d, drained:%d, switched:%d)",
3338       chain, handled, *last_group, chain->drained, *switched);
3339
3340   *drained = chain->drained;
3341
3342   if (*drained)
3343     g_signal_emit (dbin, gst_decode_bin_signals[SIGNAL_DRAINED], 0, NULL);
3344
3345   return handled;
3346 }
3347
3348 /* check if the group is drained, meaning all pads have seen an EOS
3349  * event.  */
3350 static gboolean
3351 gst_decode_pad_handle_eos (GstDecodePad * pad)
3352 {
3353   gboolean last_group = TRUE;
3354   gboolean switched = FALSE;
3355   gboolean drained = FALSE;
3356   GstDecodeChain *chain = pad->chain;
3357   GstDecodeBin *dbin = chain->dbin;
3358
3359   GST_LOG_OBJECT (dbin, "pad %p", pad);
3360   drain_and_switch_chains (dbin->decode_chain, pad, &last_group, &drained,
3361       &switched);
3362
3363   if (switched) {
3364     /* If we resulted in a group switch, expose what's needed */
3365     EXPOSE_LOCK (dbin);
3366     if (gst_decode_chain_is_complete (dbin->decode_chain))
3367       gst_decode_bin_expose (dbin);
3368     EXPOSE_UNLOCK (dbin);
3369   }
3370
3371   return last_group;
3372 }
3373
3374 /* gst_decode_group_is_drained:
3375  *
3376  * Check is this group is drained and cache this result.
3377  * The group is drained if all child chains are drained.
3378  *
3379  * Not MT-safe, call with group->parent's lock */
3380 static gboolean
3381 gst_decode_group_is_drained (GstDecodeGroup * group)
3382 {
3383   GList *l;
3384   gboolean drained = TRUE;
3385
3386   if (group->drained) {
3387     drained = TRUE;
3388     goto out;
3389   }
3390
3391   for (l = group->children; l; l = l->next) {
3392     GstDecodeChain *chain = l->data;
3393
3394     CHAIN_MUTEX_LOCK (chain);
3395     if (!gst_decode_chain_is_drained (chain))
3396       drained = FALSE;
3397     CHAIN_MUTEX_UNLOCK (chain);
3398     if (!drained)
3399       goto out;
3400   }
3401   group->drained = drained;
3402
3403 out:
3404   GST_DEBUG_OBJECT (group->dbin, "Group %p is drained: %d", group, drained);
3405   return drained;
3406 }
3407
3408 /* gst_decode_chain_is_drained:
3409  *
3410  * Check is the chain is drained, which means that
3411  * either
3412  *
3413  * a) it's endpad is drained
3414  * b) there are no pending pads, the active group is drained
3415  *    and there are no next groups
3416  *
3417  * Not MT-safe, call with chain lock
3418  */
3419 static gboolean
3420 gst_decode_chain_is_drained (GstDecodeChain * chain)
3421 {
3422   gboolean drained = FALSE;
3423
3424   if (chain->endpad) {
3425     drained = chain->endpad->drained;
3426     goto out;
3427   }
3428
3429   if (chain->pending_pads) {
3430     drained = FALSE;
3431     goto out;
3432   }
3433
3434   if (chain->active_group && gst_decode_group_is_drained (chain->active_group)
3435       && !chain->next_groups) {
3436     drained = TRUE;
3437     goto out;
3438   }
3439
3440 out:
3441   GST_DEBUG_OBJECT (chain->dbin, "Chain %p is drained: %d", chain, drained);
3442   return drained;
3443 }
3444
3445 /* sort_end_pads:
3446  * GCompareFunc to use with lists of GstPad.
3447  * Sorts pads by mime type.
3448  * First video (raw, then non-raw), then audio (raw, then non-raw),
3449  * then others.
3450  *
3451  * Return: negative if a<b, 0 if a==b, positive if a>b
3452  */
3453 static gint
3454 sort_end_pads (GstDecodePad * da, GstDecodePad * db)
3455 {
3456   gint va, vb;
3457   GstCaps *capsa, *capsb;
3458   GstStructure *sa, *sb;
3459   const gchar *namea, *nameb;
3460   gchar *ida, *idb;
3461   gint ret;
3462
3463   capsa = get_pad_caps (GST_PAD_CAST (da));
3464   capsb = get_pad_caps (GST_PAD_CAST (db));
3465
3466   sa = gst_caps_get_structure ((const GstCaps *) capsa, 0);
3467   sb = gst_caps_get_structure ((const GstCaps *) capsb, 0);
3468
3469   namea = gst_structure_get_name (sa);
3470   nameb = gst_structure_get_name (sb);
3471
3472   if (g_strrstr (namea, "video/x-raw"))
3473     va = 0;
3474   else if (g_strrstr (namea, "video/"))
3475     va = 1;
3476   else if (g_strrstr (namea, "audio/x-raw"))
3477     va = 2;
3478   else if (g_strrstr (namea, "audio/"))
3479     va = 3;
3480   else
3481     va = 4;
3482
3483   if (g_strrstr (nameb, "video/x-raw"))
3484     vb = 0;
3485   else if (g_strrstr (nameb, "video/"))
3486     vb = 1;
3487   else if (g_strrstr (nameb, "audio/x-raw"))
3488     vb = 2;
3489   else if (g_strrstr (nameb, "audio/"))
3490     vb = 3;
3491   else
3492     vb = 4;
3493
3494   gst_caps_unref (capsa);
3495   gst_caps_unref (capsb);
3496
3497   if (va != vb)
3498     return va - vb;
3499
3500   /* if otherwise the same, sort by stream-id */
3501   ida = gst_pad_get_stream_id (GST_PAD_CAST (da));
3502   idb = gst_pad_get_stream_id (GST_PAD_CAST (db));
3503   ret = (ida) ? ((idb) ? strcmp (ida, idb) : -1) : 1;
3504   g_free (ida);
3505   g_free (idb);
3506
3507   return ret;
3508 }
3509
3510 static GstCaps *
3511 _gst_element_get_linked_caps (GstElement * src, GstElement * sink,
3512     GstPad ** srcpad)
3513 {
3514   GstIterator *it;
3515   GstElement *parent;
3516   GstPad *pad, *peer;
3517   gboolean done = FALSE;
3518   GstCaps *caps = NULL;
3519   GValue item = { 0, };
3520
3521   it = gst_element_iterate_src_pads (src);
3522   while (!done) {
3523     switch (gst_iterator_next (it, &item)) {
3524       case GST_ITERATOR_OK:
3525         pad = g_value_get_object (&item);
3526         peer = gst_pad_get_peer (pad);
3527         if (peer) {
3528           parent = gst_pad_get_parent_element (peer);
3529           if (parent == sink) {
3530             caps = gst_pad_get_current_caps (pad);
3531             if (srcpad) {
3532               gst_object_ref (pad);
3533               *srcpad = pad;
3534             }
3535             done = TRUE;
3536           }
3537
3538           if (parent)
3539             gst_object_unref (parent);
3540           gst_object_unref (peer);
3541         }
3542         g_value_reset (&item);
3543         break;
3544       case GST_ITERATOR_RESYNC:
3545         gst_iterator_resync (it);
3546         break;
3547       case GST_ITERATOR_ERROR:
3548       case GST_ITERATOR_DONE:
3549         done = TRUE;
3550         break;
3551     }
3552   }
3553   g_value_unset (&item);
3554   gst_iterator_free (it);
3555
3556   return caps;
3557 }
3558
3559 static GQuark topology_structure_name = 0;
3560 static GQuark topology_caps = 0;
3561 static GQuark topology_next = 0;
3562 static GQuark topology_pad = 0;
3563 static GQuark topology_element_srcpad = 0;
3564
3565 /* FIXME: Invent gst_structure_take_structure() to prevent all the
3566  * structure copying for nothing
3567  */
3568 static GstStructure *
3569 gst_decode_chain_get_topology (GstDecodeChain * chain)
3570 {
3571   GstStructure *s, *u;
3572   GList *l;
3573   GstCaps *caps;
3574
3575   if (G_UNLIKELY ((chain->endpad || chain->deadend)
3576           && (chain->endcaps == NULL))) {
3577     GST_WARNING ("End chain without valid caps !");
3578     return NULL;
3579   }
3580
3581   u = gst_structure_new_id_empty (topology_structure_name);
3582
3583   /* Now at the last element */
3584   if ((chain->elements || !chain->active_group) &&
3585       (chain->endpad || chain->deadend)) {
3586     s = gst_structure_new_id_empty (topology_structure_name);
3587     gst_structure_id_set (u, topology_caps, GST_TYPE_CAPS, chain->endcaps,
3588         NULL);
3589
3590     if (chain->endpad) {
3591       gst_structure_id_set (u, topology_pad, GST_TYPE_PAD, chain->endpad, NULL);
3592       gst_structure_id_set (u, topology_element_srcpad, GST_TYPE_PAD,
3593           chain->endpad, NULL);
3594     }
3595     gst_structure_id_set (s, topology_next, GST_TYPE_STRUCTURE, u, NULL);
3596     gst_structure_free (u);
3597     u = s;
3598   } else if (chain->active_group) {
3599     GValue list = { 0, };
3600     GValue item = { 0, };
3601
3602     g_value_init (&list, GST_TYPE_LIST);
3603     g_value_init (&item, GST_TYPE_STRUCTURE);
3604     for (l = chain->active_group->children; l; l = l->next) {
3605       s = gst_decode_chain_get_topology (l->data);
3606       if (s) {
3607         gst_value_set_structure (&item, s);
3608         gst_value_list_append_value (&list, &item);
3609         g_value_reset (&item);
3610         gst_structure_free (s);
3611       }
3612     }
3613     gst_structure_id_set_value (u, topology_next, &list);
3614     g_value_unset (&list);
3615     g_value_unset (&item);
3616   }
3617
3618   /* Get caps between all elements in this chain */
3619   l = (chain->elements && chain->elements->next) ? chain->elements : NULL;
3620   for (; l && l->next; l = l->next) {
3621     GstDecodeElement *delem, *delem_next;
3622     GstElement *elem, *elem_next;
3623     GstCaps *caps;
3624     GstPad *srcpad;
3625
3626     delem = l->data;
3627     elem = delem->element;
3628     delem_next = l->next->data;
3629     elem_next = delem_next->element;
3630     srcpad = NULL;
3631
3632     caps = _gst_element_get_linked_caps (elem_next, elem, &srcpad);
3633
3634     if (caps) {
3635       s = gst_structure_new_id_empty (topology_structure_name);
3636       gst_structure_id_set (u, topology_caps, GST_TYPE_CAPS, caps, NULL);
3637       gst_caps_unref (caps);
3638
3639       gst_structure_id_set (s, topology_next, GST_TYPE_STRUCTURE, u, NULL);
3640       gst_structure_free (u);
3641       u = s;
3642     }
3643
3644     if (srcpad) {
3645       gst_structure_id_set (u, topology_element_srcpad, GST_TYPE_PAD, srcpad,
3646           NULL);
3647       gst_object_unref (srcpad);
3648     }
3649   }
3650
3651   /* Caps that resulted in this chain */
3652   caps = gst_pad_get_current_caps (chain->pad);
3653   if (!caps) {
3654     caps = get_pad_caps (chain->pad);
3655     if (G_UNLIKELY (!gst_caps_is_fixed (caps))) {
3656       GST_ERROR_OBJECT (chain->pad,
3657           "Couldn't get fixed caps, got %" GST_PTR_FORMAT, caps);
3658       gst_caps_unref (caps);
3659       caps = NULL;
3660     }
3661   }
3662   gst_structure_id_set (u, topology_caps, GST_TYPE_CAPS, caps, NULL);
3663   gst_structure_id_set (u, topology_element_srcpad, GST_TYPE_PAD, chain->pad,
3664       NULL);
3665   gst_caps_unref (caps);
3666
3667   return u;
3668 }
3669
3670 static void
3671 gst_decode_bin_post_topology_message (GstDecodeBin * dbin)
3672 {
3673   GstStructure *s;
3674   GstMessage *msg;
3675
3676   s = gst_decode_chain_get_topology (dbin->decode_chain);
3677
3678   msg = gst_message_new_element (GST_OBJECT (dbin), s);
3679   gst_element_post_message (GST_ELEMENT (dbin), msg);
3680 }
3681
3682 /* Must only be called if the toplevel chain is complete and blocked! */
3683 /* Not MT-safe, call with decodebin expose lock! */
3684 static gboolean
3685 gst_decode_bin_expose (GstDecodeBin * dbin)
3686 {
3687   GList *tmp, *endpads = NULL;
3688   gboolean missing_plugin = FALSE;
3689   gboolean already_exposed = TRUE;
3690
3691   GST_DEBUG_OBJECT (dbin, "Exposing currently active chains/groups");
3692
3693   /* Don't expose if we're currently shutting down */
3694   DYN_LOCK (dbin);
3695   if (G_UNLIKELY (dbin->shutdown == TRUE)) {
3696     GST_WARNING_OBJECT (dbin, "Currently, shutting down, aborting exposing");
3697     DYN_UNLOCK (dbin);
3698     return FALSE;
3699   }
3700   DYN_UNLOCK (dbin);
3701
3702   /* Get the pads that we're going to expose and mark things as exposed */
3703   if (!gst_decode_chain_expose (dbin->decode_chain, &endpads, &missing_plugin)) {
3704     g_list_foreach (endpads, (GFunc) gst_object_unref, NULL);
3705     g_list_free (endpads);
3706     GST_ERROR_OBJECT (dbin, "Broken chain/group tree");
3707     g_return_val_if_reached (FALSE);
3708     return FALSE;
3709   }
3710   if (endpads == NULL) {
3711     if (missing_plugin) {
3712       GST_WARNING_OBJECT (dbin, "No suitable plugins found");
3713       GST_ELEMENT_ERROR (dbin, CORE, MISSING_PLUGIN, (NULL),
3714           ("no suitable plugins found"));
3715     } else {
3716       /* in this case, the stream ended without buffers,
3717        * just post a warning */
3718       GST_WARNING_OBJECT (dbin, "All streams finished without buffers");
3719       GST_ELEMENT_ERROR (dbin, STREAM, FAILED, (NULL),
3720           ("all streams without buffers"));
3721     }
3722     return FALSE;
3723   }
3724
3725   /* Check if this was called when everything was exposed already */
3726   for (tmp = endpads; tmp && already_exposed; tmp = tmp->next) {
3727     GstDecodePad *dpad = tmp->data;
3728
3729     already_exposed &= dpad->exposed;
3730     if (!already_exposed)
3731       break;
3732   }
3733   if (already_exposed) {
3734     GST_DEBUG_OBJECT (dbin, "Everything was exposed already!");
3735     g_list_foreach (endpads, (GFunc) gst_object_unref, NULL);
3736     g_list_free (endpads);
3737     return TRUE;
3738   }
3739
3740   /* Set all already exposed pads to blocked */
3741   for (tmp = endpads; tmp; tmp = tmp->next) {
3742     GstDecodePad *dpad = tmp->data;
3743
3744     if (dpad->exposed) {
3745       GST_DEBUG_OBJECT (dpad, "blocking exposed pad");
3746       gst_decode_pad_set_blocked (dpad, TRUE);
3747     }
3748   }
3749
3750   /* re-order pads : video, then audio, then others */
3751   endpads = g_list_sort (endpads, (GCompareFunc) sort_end_pads);
3752
3753   /* Expose pads */
3754   for (tmp = endpads; tmp; tmp = tmp->next) {
3755     GstDecodePad *dpad = (GstDecodePad *) tmp->data;
3756     gchar *padname;
3757
3758     /* 1. rewrite name */
3759     padname = g_strdup_printf ("src_%u", dbin->nbpads);
3760     dbin->nbpads++;
3761     GST_DEBUG_OBJECT (dbin, "About to expose dpad %s as %s",
3762         GST_OBJECT_NAME (dpad), padname);
3763     gst_object_set_name (GST_OBJECT (dpad), padname);
3764     g_free (padname);
3765
3766     /* 2. activate and add */
3767     if (!dpad->exposed
3768         && !gst_element_add_pad (GST_ELEMENT (dbin), GST_PAD_CAST (dpad))) {
3769       /* not really fatal, we can try to add the other pads */
3770       g_warning ("error adding pad to decodebin");
3771       continue;
3772     }
3773     dpad->exposed = TRUE;
3774
3775     /* 3. emit signal */
3776     GST_INFO_OBJECT (dpad, "added new decoded pad");
3777   }
3778
3779   /* 4. Signal no-more-pads. This allows the application to hook stuff to the
3780    * exposed pads */
3781   GST_LOG_OBJECT (dbin, "signaling no-more-pads");
3782   gst_element_no_more_pads (GST_ELEMENT (dbin));
3783
3784   /* 5. Send a custom element message with the stream topology */
3785   if (dbin->post_stream_topology)
3786     gst_decode_bin_post_topology_message (dbin);
3787
3788   /* 6. Unblock internal pads. The application should have connected stuff now
3789    * so that streaming can continue. */
3790   for (tmp = endpads; tmp; tmp = tmp->next) {
3791     GstDecodePad *dpad = (GstDecodePad *) tmp->data;
3792
3793     GST_DEBUG_OBJECT (dpad, "unblocking");
3794     gst_decode_pad_unblock (dpad);
3795     GST_DEBUG_OBJECT (dpad, "unblocked");
3796     gst_object_unref (dpad);
3797   }
3798   g_list_free (endpads);
3799
3800   do_async_done (dbin);
3801   GST_DEBUG_OBJECT (dbin, "Exposed everything");
3802   return TRUE;
3803 }
3804
3805 /* gst_decode_chain_expose:
3806  *
3807  * Check if the chain can be exposed and add all endpads
3808  * to the endpads list.
3809  *
3810  * Also update the active group's multiqueue to the
3811  * runtime limits.
3812  *
3813  * Not MT-safe, call with decodebin expose lock! *
3814  */
3815 static gboolean
3816 gst_decode_chain_expose (GstDecodeChain * chain, GList ** endpads,
3817     gboolean * missing_plugin)
3818 {
3819   GstDecodeGroup *group;
3820   GList *l;
3821   GstDecodeBin *dbin;
3822
3823   if (chain->deadend) {
3824     if (chain->endcaps)
3825       *missing_plugin = TRUE;
3826     return TRUE;
3827   }
3828
3829   if (chain->endpad) {
3830     if (!chain->endpad->blocked && !chain->endpad->exposed)
3831       return FALSE;
3832     *endpads = g_list_prepend (*endpads, gst_object_ref (chain->endpad));
3833     return TRUE;
3834   }
3835
3836   group = chain->active_group;
3837   if (!group)
3838     return FALSE;
3839   if (!group->no_more_pads && !group->overrun)
3840     return FALSE;
3841
3842   dbin = group->dbin;
3843
3844   /* configure queues for playback */
3845   decodebin_set_queue_size (dbin, group->multiqueue, FALSE, TRUE);
3846
3847   /* we can now disconnect any overrun signal, which is used to expose the
3848    * group. */
3849   if (group->overrunsig) {
3850     GST_LOG_OBJECT (dbin, "Disconnecting overrun");
3851     g_signal_handler_disconnect (group->multiqueue, group->overrunsig);
3852     group->overrunsig = 0;
3853   }
3854
3855   for (l = group->children; l; l = l->next) {
3856     GstDecodeChain *childchain = l->data;
3857
3858     if (!gst_decode_chain_expose (childchain, endpads, missing_plugin))
3859       return FALSE;
3860   }
3861
3862   return TRUE;
3863 }
3864
3865 /*************************
3866  * GstDecodePad functions
3867  *************************/
3868
3869 static void
3870 gst_decode_pad_class_init (GstDecodePadClass * klass)
3871 {
3872 }
3873
3874 static void
3875 gst_decode_pad_init (GstDecodePad * pad)
3876 {
3877   pad->chain = NULL;
3878   pad->blocked = FALSE;
3879   pad->exposed = FALSE;
3880   pad->drained = FALSE;
3881   gst_object_ref_sink (pad);
3882 }
3883
3884 static GstPadProbeReturn
3885 source_pad_blocked_cb (GstPad * pad, GstPadProbeInfo * info, gpointer user_data)
3886 {
3887   GstDecodePad *dpad = user_data;
3888   GstDecodeChain *chain;
3889   GstDecodeBin *dbin;
3890
3891   if (GST_PAD_PROBE_INFO_TYPE (info) & GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM) {
3892     GST_LOG_OBJECT (pad, "Seeing event '%s'",
3893         GST_EVENT_TYPE_NAME (GST_PAD_PROBE_INFO_EVENT (info)));
3894
3895     if (GST_EVENT_TYPE (GST_PAD_PROBE_INFO_EVENT (info)) == GST_EVENT_CAPS) {
3896       /* manually push event to ghost pad to avoid exposing pads
3897        * that don't have the sticky caps event */
3898       gst_pad_push_event (GST_PAD_CAST (dpad),
3899           gst_event_ref (GST_PAD_PROBE_INFO_EVENT (info)));
3900     } else if ((GST_EVENT_IS_STICKY (GST_PAD_PROBE_INFO_EVENT (info))
3901             || !GST_EVENT_IS_SERIALIZED (GST_PAD_PROBE_INFO_EVENT (info)))) {
3902       /* do not block on sticky or out of band events otherwise the allocation query
3903          from demuxer might block the loop thread */
3904       GST_LOG_OBJECT (pad, "Letting event through");
3905       return GST_PAD_PROBE_PASS;
3906     }
3907   }
3908   chain = dpad->chain;
3909   dbin = chain->dbin;
3910
3911   GST_LOG_OBJECT (dpad, "blocked: dpad->chain:%p", chain);
3912
3913   dpad->blocked = TRUE;
3914
3915   EXPOSE_LOCK (dbin);
3916   if (gst_decode_chain_is_complete (dbin->decode_chain)) {
3917     if (!gst_decode_bin_expose (dbin))
3918       GST_WARNING_OBJECT (dbin, "Couldn't expose group");
3919   }
3920   EXPOSE_UNLOCK (dbin);
3921
3922   /* If we unblocked due to a caps event, let it go through */
3923   if ((GST_PAD_PROBE_INFO_TYPE (info) & GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM) &&
3924       (GST_EVENT_TYPE (GST_PAD_PROBE_INFO_EVENT (info)) == GST_EVENT_CAPS))
3925     return GST_PAD_PROBE_PASS;
3926
3927   return GST_PAD_PROBE_OK;
3928 }
3929
3930 static GstPadProbeReturn
3931 source_pad_event_probe (GstPad * pad, GstPadProbeInfo * info,
3932     gpointer user_data)
3933 {
3934   GstEvent *event = GST_PAD_PROBE_INFO_EVENT (info);
3935   GstDecodePad *dpad = user_data;
3936   gboolean res = TRUE;
3937
3938   GST_LOG_OBJECT (pad, "%s dpad:%p", GST_EVENT_TYPE_NAME (event), dpad);
3939
3940   if (GST_EVENT_TYPE (event) == GST_EVENT_EOS) {
3941     GST_DEBUG_OBJECT (pad, "we received EOS");
3942
3943     /* Check if all pads are drained.
3944      * * If there is no next group, we will let the EOS go through.
3945      * * If there is a next group but the current group isn't completely
3946      *   drained, we will drop the EOS event.
3947      * * If there is a next group to expose and this was the last non-drained
3948      *   pad for that group, we will remove the ghostpad of the current group
3949      *   first, which unlinks the peer and so drops the EOS. */
3950     res = gst_decode_pad_handle_eos (dpad);
3951   }
3952   if (res)
3953     return GST_PAD_PROBE_OK;
3954   else
3955     return GST_PAD_PROBE_DROP;
3956 }
3957
3958 static void
3959 gst_decode_pad_set_blocked (GstDecodePad * dpad, gboolean blocked)
3960 {
3961   GstDecodeBin *dbin = dpad->dbin;
3962   GstPad *opad;
3963
3964   DYN_LOCK (dbin);
3965
3966   GST_DEBUG_OBJECT (dpad, "blocking pad: %d", blocked);
3967
3968   opad = gst_ghost_pad_get_target (GST_GHOST_PAD_CAST (dpad));
3969   if (!opad)
3970     goto out;
3971
3972   /* do not block if shutting down.
3973    * we do not consider/expect it blocked further below, but use other trick */
3974   if (!blocked || !dbin->shutdown) {
3975     if (blocked) {
3976       if (dpad->block_id == 0)
3977         dpad->block_id =
3978             gst_pad_add_probe (opad, GST_PAD_PROBE_TYPE_BLOCK_DOWNSTREAM,
3979             source_pad_blocked_cb, gst_object_ref (dpad),
3980             (GDestroyNotify) gst_object_unref);
3981     } else {
3982       if (dpad->block_id != 0) {
3983         gst_pad_remove_probe (opad, dpad->block_id);
3984         dpad->block_id = 0;
3985       }
3986       dpad->blocked = FALSE;
3987     }
3988   }
3989
3990   if (blocked) {
3991     if (dbin->shutdown) {
3992       /* deactivate to force flushing state to prevent NOT_LINKED errors */
3993       gst_pad_set_active (GST_PAD_CAST (dpad), FALSE);
3994       /* note that deactivating the target pad would have no effect here,
3995        * since elements are typically connected first (and pads exposed),
3996        * and only then brought to PAUSED state (so pads activated) */
3997     } else {
3998       gst_object_ref (dpad);
3999       dbin->blocked_pads = g_list_prepend (dbin->blocked_pads, dpad);
4000     }
4001   } else {
4002     GList *l;
4003
4004     if ((l = g_list_find (dbin->blocked_pads, dpad))) {
4005       gst_object_unref (dpad);
4006       dbin->blocked_pads = g_list_delete_link (dbin->blocked_pads, l);
4007     }
4008   }
4009   gst_object_unref (opad);
4010 out:
4011   DYN_UNLOCK (dbin);
4012 }
4013
4014 static void
4015 gst_decode_pad_add_drained_check (GstDecodePad * dpad)
4016 {
4017   gst_pad_add_probe (GST_PAD_CAST (dpad), GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM,
4018       source_pad_event_probe, dpad, NULL);
4019 }
4020
4021 static void
4022 gst_decode_pad_activate (GstDecodePad * dpad, GstDecodeChain * chain)
4023 {
4024   g_return_if_fail (chain != NULL);
4025
4026   dpad->chain = chain;
4027   gst_pad_set_active (GST_PAD_CAST (dpad), TRUE);
4028   gst_decode_pad_set_blocked (dpad, TRUE);
4029   gst_decode_pad_add_drained_check (dpad);
4030 }
4031
4032 static void
4033 gst_decode_pad_unblock (GstDecodePad * dpad)
4034 {
4035   gst_decode_pad_set_blocked (dpad, FALSE);
4036 }
4037
4038 /*gst_decode_pad_new:
4039  *
4040  * Creates a new GstDecodePad for the given pad.
4041  */
4042 static GstDecodePad *
4043 gst_decode_pad_new (GstDecodeBin * dbin, GstPad * pad, GstDecodeChain * chain)
4044 {
4045   GstDecodePad *dpad;
4046   GstPadTemplate *pad_tmpl;
4047
4048   GST_DEBUG_OBJECT (dbin, "making new decodepad");
4049   pad_tmpl = gst_static_pad_template_get (&decoder_bin_src_template);
4050   dpad =
4051       g_object_new (GST_TYPE_DECODE_PAD, "direction", GST_PAD_DIRECTION (pad),
4052       "template", pad_tmpl, NULL);
4053   gst_ghost_pad_construct (GST_GHOST_PAD_CAST (dpad));
4054   gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (dpad), pad);
4055   dpad->chain = chain;
4056   dpad->dbin = dbin;
4057   gst_object_unref (pad_tmpl);
4058
4059   return dpad;
4060 }
4061
4062 static void
4063 gst_pending_pad_free (GstPendingPad * ppad)
4064 {
4065   g_assert (ppad);
4066   g_assert (ppad->pad);
4067
4068   if (ppad->event_probe_id != 0)
4069     gst_pad_remove_probe (ppad->pad, ppad->event_probe_id);
4070   gst_object_unref (ppad->pad);
4071   g_slice_free (GstPendingPad, ppad);
4072 }
4073
4074 /*****
4075  * Element add/remove
4076  *****/
4077
4078 static void
4079 do_async_start (GstDecodeBin * dbin)
4080 {
4081   GstMessage *message;
4082
4083   dbin->async_pending = TRUE;
4084
4085   message = gst_message_new_async_start (GST_OBJECT_CAST (dbin));
4086   parent_class->handle_message (GST_BIN_CAST (dbin), message);
4087 }
4088
4089 static void
4090 do_async_done (GstDecodeBin * dbin)
4091 {
4092   GstMessage *message;
4093
4094   if (dbin->async_pending) {
4095     message =
4096         gst_message_new_async_done (GST_OBJECT_CAST (dbin),
4097         GST_CLOCK_TIME_NONE);
4098     parent_class->handle_message (GST_BIN_CAST (dbin), message);
4099
4100     dbin->async_pending = FALSE;
4101   }
4102 }
4103
4104 /*****
4105  * convenience functions
4106  *****/
4107
4108 /* find_sink_pad
4109  *
4110  * Returns the first sink pad of the given element, or NULL if it doesn't have
4111  * any.
4112  */
4113
4114 static GstPad *
4115 find_sink_pad (GstElement * element)
4116 {
4117   GstIterator *it;
4118   GstPad *pad = NULL;
4119   GValue item = { 0, };
4120
4121   it = gst_element_iterate_sink_pads (element);
4122
4123   if ((gst_iterator_next (it, &item)) == GST_ITERATOR_OK)
4124     pad = g_value_dup_object (&item);
4125   g_value_unset (&item);
4126   gst_iterator_free (it);
4127
4128   return pad;
4129 }
4130
4131 /* call with dyn_lock held */
4132 static void
4133 unblock_pads (GstDecodeBin * dbin)
4134 {
4135   GList *tmp;
4136
4137   GST_LOG_OBJECT (dbin, "unblocking pads");
4138
4139   for (tmp = dbin->blocked_pads; tmp; tmp = tmp->next) {
4140     GstDecodePad *dpad = (GstDecodePad *) tmp->data;
4141     GstPad *opad;
4142
4143     opad = gst_ghost_pad_get_target (GST_GHOST_PAD_CAST (dpad));
4144     if (!opad)
4145       continue;
4146
4147     GST_DEBUG_OBJECT (dpad, "unblocking");
4148     if (dpad->block_id != 0) {
4149       gst_pad_remove_probe (opad, dpad->block_id);
4150       dpad->block_id = 0;
4151     }
4152     dpad->blocked = FALSE;
4153     /* make flushing, prevent NOT_LINKED */
4154     GST_PAD_SET_FLUSHING (GST_PAD_CAST (dpad));
4155     gst_object_unref (dpad);
4156     gst_object_unref (opad);
4157     GST_DEBUG_OBJECT (dpad, "unblocked");
4158   }
4159
4160   /* clear, no more blocked pads */
4161   g_list_free (dbin->blocked_pads);
4162   dbin->blocked_pads = NULL;
4163 }
4164
4165 static GstStateChangeReturn
4166 gst_decode_bin_change_state (GstElement * element, GstStateChange transition)
4167 {
4168   GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
4169   GstDecodeBin *dbin = GST_DECODE_BIN (element);
4170
4171   switch (transition) {
4172     case GST_STATE_CHANGE_NULL_TO_READY:
4173       if (dbin->typefind == NULL)
4174         goto missing_typefind;
4175       break;
4176     case GST_STATE_CHANGE_READY_TO_PAUSED:
4177       /* Make sure we've cleared all existing chains */
4178       if (dbin->decode_chain) {
4179         gst_decode_chain_free (dbin->decode_chain);
4180         dbin->decode_chain = NULL;
4181       }
4182       DYN_LOCK (dbin);
4183       GST_LOG_OBJECT (dbin, "clearing shutdown flag");
4184       dbin->shutdown = FALSE;
4185       DYN_UNLOCK (dbin);
4186       dbin->have_type = FALSE;
4187       ret = GST_STATE_CHANGE_ASYNC;
4188       do_async_start (dbin);
4189       break;
4190     case GST_STATE_CHANGE_PAUSED_TO_READY:
4191       DYN_LOCK (dbin);
4192       GST_LOG_OBJECT (dbin, "setting shutdown flag");
4193       dbin->shutdown = TRUE;
4194       unblock_pads (dbin);
4195       DYN_UNLOCK (dbin);
4196     default:
4197       break;
4198   }
4199
4200   {
4201     GstStateChangeReturn bret;
4202
4203     bret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
4204     if (G_UNLIKELY (bret == GST_STATE_CHANGE_FAILURE))
4205       goto activate_failed;
4206     else if (G_UNLIKELY (bret == GST_STATE_CHANGE_NO_PREROLL)) {
4207       do_async_done (dbin);
4208       ret = bret;
4209     }
4210   }
4211   switch (transition) {
4212     case GST_STATE_CHANGE_PAUSED_TO_READY:
4213       do_async_done (dbin);
4214       if (dbin->decode_chain) {
4215         gst_decode_chain_free (dbin->decode_chain);
4216         dbin->decode_chain = NULL;
4217       }
4218       break;
4219     case GST_STATE_CHANGE_READY_TO_NULL:
4220     default:
4221       break;
4222   }
4223
4224   return ret;
4225
4226 /* ERRORS */
4227 missing_typefind:
4228   {
4229     gst_element_post_message (element,
4230         gst_missing_element_message_new (element, "typefind"));
4231     GST_ELEMENT_ERROR (dbin, CORE, MISSING_PLUGIN, (NULL), ("no typefind!"));
4232     return GST_STATE_CHANGE_FAILURE;
4233   }
4234 activate_failed:
4235   {
4236     GST_DEBUG_OBJECT (element,
4237         "element failed to change states -- activation problem?");
4238     return GST_STATE_CHANGE_FAILURE;
4239   }
4240 }
4241
4242 static void
4243 gst_decode_bin_handle_message (GstBin * bin, GstMessage * msg)
4244 {
4245   GstDecodeBin *dbin = GST_DECODE_BIN (bin);
4246   gboolean drop = FALSE;
4247
4248   if (GST_MESSAGE_TYPE (msg) == GST_MESSAGE_ERROR) {
4249     GST_OBJECT_LOCK (dbin);
4250     drop = (g_list_find (dbin->filtered, GST_MESSAGE_SRC (msg)) != NULL);
4251     GST_OBJECT_UNLOCK (dbin);
4252   }
4253
4254   if (drop)
4255     gst_message_unref (msg);
4256   else
4257     GST_BIN_CLASS (parent_class)->handle_message (bin, msg);
4258 }
4259
4260 gboolean
4261 gst_decode_bin_plugin_init (GstPlugin * plugin)
4262 {
4263   GST_DEBUG_CATEGORY_INIT (gst_decode_bin_debug, "decodebin", 0, "decoder bin");
4264
4265   /* Register some quarks here for the stream topology message */
4266   topology_structure_name = g_quark_from_static_string ("stream-topology");
4267   topology_caps = g_quark_from_static_string ("caps");
4268   topology_next = g_quark_from_static_string ("next");
4269   topology_pad = g_quark_from_static_string ("pad");
4270   topology_element_srcpad = g_quark_from_static_string ("element-srcpad");
4271
4272   return gst_element_register (plugin, "decodebin", GST_RANK_NONE,
4273       GST_TYPE_DECODE_BIN);
4274 }