decodebin3: fix tag list leak
[platform/upstream/gstreamer.git] / gst / playback / gstdecodebin3.c
1 /* GStreamer
2  *
3  * Copyright (C) <2015> Centricular Ltd
4  *  @author: Edward Hervey <edward@centricular.com>
5  *  @author: Jan Schmidt <jan@centricular.com>
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 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26
27 #include <glib.h>
28 #include <glib-object.h>
29 #include <glib/gprintf.h>
30 #include <gst/gst.h>
31 #include <gst/pbutils/pbutils.h>
32
33 #include "gstplayback.h"
34 #include "gstplay-enum.h"
35 #include "gstrawcaps.h"
36
37 /**
38  * SECTION:element-decodebin3
39  *
40  * #GstBin that auto-magically constructs a decoding pipeline using available
41  * decoders and demuxers via auto-plugging. The output is raw audio, video
42  * or subtitle streams.
43  *
44  * decodebin3 differs from the previous decodebin (decodebin2) in important ways:
45  *
46  * <itemizedlist>
47  * <listitem>
48  * supports publication and selection of stream information via
49  * GstStreamCollection messages and #GST_EVENT_SELECT_STREAM events.
50  * </listitem>
51  * <listitem>
52  * dynamically switches stream connections internally, and
53  * reuses decoder elements when stream selections change, so that in
54  * the normal case it maintains 1 decoder of each type (video/audio/subtitle)
55  * and only creates new elements when streams change and an existing decoder
56  * is not capable of handling the new format.
57  * </listitem>
58  * <listitem>
59  * supports multiple input pads for the parallel decoding of auxilliary streams
60  * not muxed with the primary stream.
61  * </listitem>
62  * <listitem>
63  * does not handle network stream buffering. decodebin3 expects that network stream
64  * buffering is handled upstream, before data is passed to it.
65  * </listitem>
66  * </itemizedlist>
67  *
68  * <emphasis>decodebin3 is still experimental API and a technology preview.
69  * Its behaviour and exposed API is subject to change.</emphasis>
70  *
71  */
72
73 /**
74  * Global design
75  *
76  * 1) From sink pad to elementary streams (GstParseBin)
77  * 
78  * The input sink pads are fed to GstParseBin. GstParseBin will feed them
79  * through typefind. When the caps are detected (or changed) we recursively
80  * figure out which demuxer, parser or depayloader is needed until we get to
81  * elementary streams.
82  *
83  * All elementary streams (whether decoded or not, whether exposed or not) are
84  * fed through multiqueue. There is only *one* multiqueue in decodebin3.
85  *
86  * => MultiQueue is the cornerstone.
87  * => No buffering before multiqueue
88  *
89  * 2) Elementary streams
90  *
91  * After GstParseBin, there are 3 main components:
92  *  1) Input Streams (provided by GstParseBin)
93  *  2) Multiqueue slots
94  *  3) Output Streams
95  *
96  * Input Streams correspond to the stream coming from GstParseBin and that gets
97  * fed into a multiqueue slot.
98  *
99  * Output Streams correspond to the combination of a (optional) decoder and an
100  * output ghostpad. Output Streams can be moved from one multiqueue slot to
101  * another, can reconfigure itself (different decoders), and can be
102  * added/removed depending on the configuration (all streams outputted, only one
103  * of each type, ...).
104  *
105  * Multiqueue slots correspond to a pair of sink/src pad from multiqueue. For
106  * each 'active' Input Stream there is a corresponding slot.
107  * Slots might have different streams on input and output (due to internal
108  * buffering).
109  *
110  * Due to internal queuing/buffering/..., all those components (might) behave
111  * asynchronously. Therefore probes will be used on each component source pad to
112  * detect various key-points:
113  *  * EOS :
114  *     the stream is done => Mark that component as done, optionally freeing/removing it
115  *  * STREAM_START :
116  *     a new stream is starting => link it further if needed
117  *
118  *
119  * 3) Gradual replacement
120  *
121  * If the caps change at any point in decodebin (input sink pad, demuxer output,
122  * multiqueue output, ..), we gradually replace (if needed) the following elements.
123  *
124  * This is handled by the probes in various locations:
125  *  a) typefind output
126  *  b) multiqueue input (source pad of Input Streams)
127  *  c) multiqueue output (source pad of Multiqueue Slots)
128  *  d) final output (target of source ghostpads)
129  *
130  * When CAPS event arrive at those points, one of three things can happen:
131  * a) There is no elements downstream yet, just create/link-to following elements
132  * b) There are downstream elements, do a ACCEPT_CAPS query
133  *  b.1) The new CAPS are accepted, keep current configuration
134  *  b.2) The new CAPS are not accepted, remove following elements then do a)
135  *
136  *
137  *
138  *    Components:
139  *
140  *                                                   MultiQ     Output
141  *                     Input(s)                      Slots      Streams
142  *  /-------------------------------------------\   /-----\  /------------- \
143  *
144  * +-------------------------------------------------------------------------+
145  * |                                                                         |
146  * | +---------------------------------------------+                         |
147  * | |   GstParseBin(s)                            |                         |
148  * | |                +--------------+             |  +-----+                |
149  * | |                |              |---[parser]-[|--| Mul |---[ decoder ]-[|
150  * |]--[ typefind ]---|  demuxer(s)  |------------[|  | ti  |                |
151  * | |                |  (if needed) |---[parser]-[|--| qu  |                |
152  * | |                |              |---[parser]-[|--| eu  |---[ decoder ]-[|
153  * | |                +--------------+             |  +------             ^  |
154  * | +---------------------------------------------+        ^             |  |
155  * |                                               ^        |             |  |
156  * +-----------------------------------------------+--------+-------------+--+
157  *                                                 |        |             |
158  *                                                 |        |             |
159  *                                       Probes  --/--------/-------------/
160  *
161  * ATOMIC SWITCHING
162  *
163  * We want to ensure we re-use decoders when switching streams. This takes place
164  * at the multiqueue output level.
165  *
166  * MAIN CONCEPTS
167  *  1) Activating a stream (i.e. linking a slot to an output) is only done within
168  *    the streaming thread in the multiqueue_src_probe() and only if the
169       stream is in the REQUESTED selection.
170  *  2) Deactivating a stream (i.e. unlinking a slot from an output) is also done
171  *    within the stream thread, but only in a purposefully called IDLE probe
172  *    that calls reassign_slot().
173  *
174  * Based on those two principles, 3 "selection" of streams (stream-id) are used:
175  * 1) requested_selection
176  *    All streams within that list should be activated
177  * 2) active_selection
178  *    List of streams that are exposed by decodebin
179  * 3) to_activate
180  *    List of streams that will be moved to requested_selection in the
181  *    reassign_slot() method (i.e. once a stream was deactivated, and the output
182  *    was retargetted)
183  */
184
185
186 GST_DEBUG_CATEGORY_STATIC (decodebin3_debug);
187 #define GST_CAT_DEFAULT decodebin3_debug
188
189 #define GST_TYPE_DECODEBIN3      (gst_decodebin3_get_type ())
190
191 #define EXTRA_DEBUG 1
192
193 typedef struct _GstDecodebin3 GstDecodebin3;
194 typedef struct _GstDecodebin3Class GstDecodebin3Class;
195
196 typedef struct _DecodebinInputStream DecodebinInputStream;
197 typedef struct _DecodebinInput DecodebinInput;
198 typedef struct _DecodebinOutputStream DecodebinOutputStream;
199
200 struct _GstDecodebin3
201 {
202   GstBin bin;
203
204   /* input_lock protects the following variables */
205   GMutex input_lock;
206   /* Main input (static sink pad) */
207   DecodebinInput *main_input;
208   /* Supplementary input (request sink pads) */
209   GList *other_inputs;
210   /* counter for input */
211   guint32 input_counter;
212   /* Current stream group_id (default : G_MAXUINT32) */
213   /* FIXME : Needs to be resetted appropriately (when upstream changes ?) */
214   guint32 current_group_id;
215   /* End of variables protected by input_lock */
216
217   GstElement *multiqueue;
218
219   /* FIXME : Mutex for protecting values below */
220   GstStreamCollection *collection;      /* Active collection */
221
222   GList *input_streams;         /* List of DecodebinInputStream for active collection */
223   GList *output_streams;        /* List of DecodebinOutputStream used for output */
224   GList *slots;                 /* List of MultiQueueSlot */
225   guint slot_id;
226
227   /* selection_lock protects access to following variables */
228   GMutex selection_lock;
229   /* requested selection of stream-id to activate post-multiqueue */
230   GList *requested_selection;
231   /* list of stream-id currently activated in output */
232   GList *active_selection;
233   /* List of stream-id that need to be activated (after a stream switch for ex) */
234   GList *to_activate;
235   /* Pending select streams event */
236   guint32 select_streams_seqnum;
237   /* pending list of streams to select (from downstream) */
238   GList *pending_select_streams;
239   /* TRUE if requested_selection was updated, will become FALSE once
240    * it has fully transitioned to active */
241   gboolean selection_updated;
242   /* End of variables protected by selection_lock */
243
244   /* List of pending collections.
245    * FIXME : Is this really needed ? */
246   GList *pending_collection;
247
248
249   /* Factories */
250   GMutex factories_lock;
251   guint32 factories_cookie;
252   /* All DECODABLE factories */
253   GList *factories;
254   /* Only DECODER factories */
255   GList *decoder_factories;
256   /* DECODABLE but not DECODER factories */
257   GList *decodable_factories;
258
259   /* counters for pads */
260   guint32 apadcount, vpadcount, tpadcount, opadcount;
261
262   /* Properties */
263   GstCaps *caps;
264 };
265
266 struct _GstDecodebin3Class
267 {
268   GstBinClass class;
269
270     gint (*select_stream) (GstDecodebin3 * dbin,
271       GstStreamCollection * collection, GstStream * stream);
272 };
273
274 /* Input of decodebin, controls input pad and parsebin */
275 struct _DecodebinInput
276 {
277   GstDecodebin3 *dbin;
278
279   gboolean is_main;
280
281   GstPad *ghost_sink;
282   GstPad *parsebin_sink;
283
284   GstStreamCollection *collection;      /* Active collection */
285
286   guint group_id;
287
288   GstElement *parsebin;
289
290   gulong pad_added_sigid;
291   gulong pad_removed_sigid;
292
293   /* HACK : Remove these fields */
294   /* List of PendingPad structures */
295   GList *pending_pads;
296 };
297
298 /* Multiqueue Slots */
299 typedef struct _MultiQueueSlot
300 {
301   guint id;
302
303   GstDecodebin3 *dbin;
304   /* Type of stream handled by this slot */
305   GstStreamType type;
306
307   /* Linked input and output */
308   DecodebinInputStream *input;
309
310   /* pending => last stream received on sink pad */
311   GstStream *pending_stream;
312   /* active => last stream outputted on source pad */
313   GstStream *active_stream;
314
315   GstPad *sink_pad, *src_pad;
316
317   /* id of the MQ src_pad event probe */
318   gulong probe_id;
319
320   gboolean drain_eos;
321
322   DecodebinOutputStream *output;
323 } MultiQueueSlot;
324
325 /* Streams that are exposed downstream (i.e. output) */
326 struct _DecodebinOutputStream
327 {
328   GstDecodebin3 *dbin;
329   /* The type of stream handled by this output stream */
330   GstStreamType type;
331
332   /* The slot to which this output stream is currently connected to */
333   MultiQueueSlot *slot;
334
335   GstElement *decoder;          /* Optional */
336   GstPad *decoder_sink, *decoder_src;
337   gboolean linked;
338
339   /* ghostpad */
340   GstPad *src_pad;
341   /* Flag if ghost pad is exposed */
342   gboolean src_exposed;
343
344   /* keyframe dropping probe */
345   gulong drop_probe_id;
346 };
347
348 /* Pending pads from parsebin */
349 typedef struct _PendingPad
350 {
351   GstDecodebin3 *dbin;
352   DecodebinInput *input;
353   GstPad *pad;
354
355   gulong buffer_probe;
356   gulong event_probe;
357   gboolean saw_eos;
358 } PendingPad;
359
360 /* properties */
361 #define DEFAULT_CAPS (gst_static_caps_get (&default_raw_caps))
362
363 enum
364 {
365   PROP_0,
366   PROP_CAPS
367 };
368
369 /* signals */
370 enum
371 {
372   SIGNAL_SELECT_STREAM,
373   LAST_SIGNAL
374 };
375 static guint gst_decodebin3_signals[LAST_SIGNAL] = { 0 };
376
377 #define SELECTION_LOCK(dbin) G_STMT_START {                             \
378     GST_LOG_OBJECT (dbin,                                               \
379                     "selection locking from thread %p",                 \
380                     g_thread_self ());                                  \
381     g_mutex_lock (&dbin->selection_lock);                               \
382     GST_LOG_OBJECT (dbin,                                               \
383                     "selection locked from thread %p",                  \
384                     g_thread_self ());                                  \
385   } G_STMT_END
386
387 #define SELECTION_UNLOCK(dbin) G_STMT_START {                           \
388     GST_LOG_OBJECT (dbin,                                               \
389                     "selection unlocking from thread %p",               \
390                     g_thread_self ());                                  \
391     g_mutex_unlock (&dbin->selection_lock);                             \
392   } G_STMT_END
393
394 #define INPUT_LOCK(dbin) G_STMT_START {                         \
395     GST_LOG_OBJECT (dbin,                                               \
396                     "input locking from thread %p",                     \
397                     g_thread_self ());                                  \
398     g_mutex_lock (&dbin->input_lock);                           \
399     GST_LOG_OBJECT (dbin,                                               \
400                     "input locked from thread %p",                      \
401                     g_thread_self ());                                  \
402   } G_STMT_END
403
404 #define INPUT_UNLOCK(dbin) G_STMT_START {                               \
405     GST_LOG_OBJECT (dbin,                                               \
406                     "input unlocking from thread %p",           \
407                     g_thread_self ());                                  \
408     g_mutex_unlock (&dbin->input_lock);                         \
409   } G_STMT_END
410
411 GType gst_decodebin3_get_type (void);
412 #define gst_decodebin3_parent_class parent_class
413 G_DEFINE_TYPE (GstDecodebin3, gst_decodebin3, GST_TYPE_BIN);
414
415 static GstStaticCaps default_raw_caps = GST_STATIC_CAPS (DEFAULT_RAW_CAPS);
416
417 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
418     GST_PAD_SINK,
419     GST_PAD_ALWAYS,
420     GST_STATIC_CAPS_ANY);
421
422 static GstStaticPadTemplate request_sink_template =
423 GST_STATIC_PAD_TEMPLATE ("sink_%u",
424     GST_PAD_SINK,
425     GST_PAD_REQUEST,
426     GST_STATIC_CAPS_ANY);
427
428 static GstStaticPadTemplate video_src_template =
429 GST_STATIC_PAD_TEMPLATE ("video_%u",
430     GST_PAD_SRC,
431     GST_PAD_SOMETIMES,
432     GST_STATIC_CAPS_ANY);
433
434 static GstStaticPadTemplate audio_src_template =
435 GST_STATIC_PAD_TEMPLATE ("audio_%u",
436     GST_PAD_SRC,
437     GST_PAD_SOMETIMES,
438     GST_STATIC_CAPS_ANY);
439
440 static GstStaticPadTemplate text_src_template =
441 GST_STATIC_PAD_TEMPLATE ("text_%u",
442     GST_PAD_SRC,
443     GST_PAD_SOMETIMES,
444     GST_STATIC_CAPS_ANY);
445
446 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src_%u",
447     GST_PAD_SRC,
448     GST_PAD_SOMETIMES,
449     GST_STATIC_CAPS_ANY);
450
451
452 static void gst_decodebin3_dispose (GObject * object);
453 static void gst_decodebin3_set_property (GObject * object, guint prop_id,
454     const GValue * value, GParamSpec * pspec);
455 static void gst_decodebin3_get_property (GObject * object, guint prop_id,
456     GValue * value, GParamSpec * pspec);
457
458 static gboolean parsebin_autoplug_continue_cb (GstElement *
459     parsebin, GstPad * pad, GstCaps * caps, GstDecodebin3 * dbin);
460
461 static gint
462 gst_decodebin3_select_stream (GstDecodebin3 * dbin,
463     GstStreamCollection * collection, GstStream * stream)
464 {
465   GST_LOG_OBJECT (dbin, "default select-stream, returning -1");
466
467   return -1;
468 }
469
470 static GstPad *gst_decodebin3_request_new_pad (GstElement * element,
471     GstPadTemplate * temp, const gchar * name, const GstCaps * caps);
472 static void gst_decodebin3_handle_message (GstBin * bin, GstMessage * message);
473 static GstStateChangeReturn gst_decodebin3_change_state (GstElement * element,
474     GstStateChange transition);
475 static gboolean gst_decodebin3_send_event (GstElement * element,
476     GstEvent * event);
477
478 static void gst_decode_bin_update_factories_list (GstDecodebin3 * dbin);
479 #if 0
480 static gboolean have_factory (GstDecodebin3 * dbin, GstCaps * caps,
481     GstElementFactoryListType ftype);
482 #endif
483
484 static void free_input (GstDecodebin3 * dbin, DecodebinInput * input);
485 static DecodebinInput *create_new_input (GstDecodebin3 * dbin, gboolean main);
486 static gboolean set_input_group_id (DecodebinInput * input, guint32 * group_id);
487
488 static void reconfigure_output_stream (DecodebinOutputStream * output,
489     MultiQueueSlot * slot);
490 static void free_output_stream (GstDecodebin3 * dbin,
491     DecodebinOutputStream * output);
492 static DecodebinOutputStream *create_output_stream (GstDecodebin3 * dbin,
493     GstStreamType type);
494
495 static GstPadProbeReturn slot_unassign_probe (GstPad * pad,
496     GstPadProbeInfo * info, MultiQueueSlot * slot);
497 static gboolean reassign_slot (GstDecodebin3 * dbin, MultiQueueSlot * slot);
498 static MultiQueueSlot *get_slot_for_input (GstDecodebin3 * dbin,
499     DecodebinInputStream * input);
500 static void link_input_to_slot (DecodebinInputStream * input,
501     MultiQueueSlot * slot);
502 static void free_multiqueue_slot (GstDecodebin3 * dbin, MultiQueueSlot * slot);
503
504 /* FIXME: Really make all the parser stuff a self-contained helper object */
505 #include "gstdecodebin3-parse.c"
506
507 static gboolean
508 _gst_int_accumulator (GSignalInvocationHint * ihint,
509     GValue * return_accu, const GValue * handler_return, gpointer dummy)
510 {
511   gint res = g_value_get_int (handler_return);
512
513   if (!(ihint->run_type & G_SIGNAL_RUN_CLEANUP))
514     g_value_set_int (return_accu, res);
515
516   if (res == -1)
517     return TRUE;
518
519   return FALSE;
520 }
521
522 static void
523 gst_decodebin3_class_init (GstDecodebin3Class * klass)
524 {
525   GObjectClass *gobject_klass = (GObjectClass *) klass;
526   GstElementClass *element_class = (GstElementClass *) klass;
527   GstBinClass *bin_klass = (GstBinClass *) klass;
528
529   gobject_klass->dispose = gst_decodebin3_dispose;
530   gobject_klass->set_property = gst_decodebin3_set_property;
531   gobject_klass->get_property = gst_decodebin3_get_property;
532
533   /* FIXME : ADD PROPERTIES ! */
534   g_object_class_install_property (gobject_klass, PROP_CAPS,
535       g_param_spec_boxed ("caps", "Caps",
536           "The caps on which to stop decoding. (NULL = default)",
537           GST_TYPE_CAPS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
538
539   /* FIXME : ADD SIGNALS ! */
540   /**
541    * GstDecodebin3::select-stream
542    * @decodebin: a #GstDecodebin3
543    * @collection: a #GstStreamCollection
544    * @stream: a #GstStream
545    *
546    * This signal is emitted whenever @decodebin needs to decide whether
547    * to expose a @stream of a given @collection.
548    *
549    * Returns: 1 if the stream should be selected, 0 if it shouldn't be selected.
550    * A value of -1 (default) lets @decodebin decide what to do with the stream.
551    * */
552   gst_decodebin3_signals[SIGNAL_SELECT_STREAM] =
553       g_signal_new ("select-stream", G_TYPE_FROM_CLASS (klass),
554       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstDecodebin3Class, select_stream),
555       _gst_int_accumulator, NULL, g_cclosure_marshal_generic,
556       G_TYPE_INT, 2, GST_TYPE_STREAM_COLLECTION, GST_TYPE_STREAM);
557
558
559   element_class->request_new_pad =
560       GST_DEBUG_FUNCPTR (gst_decodebin3_request_new_pad);
561   element_class->change_state = GST_DEBUG_FUNCPTR (gst_decodebin3_change_state);
562   element_class->send_event = GST_DEBUG_FUNCPTR (gst_decodebin3_send_event);
563
564   gst_element_class_add_pad_template (element_class,
565       gst_static_pad_template_get (&sink_template));
566   gst_element_class_add_pad_template (element_class,
567       gst_static_pad_template_get (&request_sink_template));
568   gst_element_class_add_pad_template (element_class,
569       gst_static_pad_template_get (&video_src_template));
570   gst_element_class_add_pad_template (element_class,
571       gst_static_pad_template_get (&audio_src_template));
572   gst_element_class_add_pad_template (element_class,
573       gst_static_pad_template_get (&text_src_template));
574   gst_element_class_add_pad_template (element_class,
575       gst_static_pad_template_get (&src_template));
576
577   gst_element_class_set_static_metadata (element_class,
578       "Decoder Bin 3", "Generic/Bin/Decoder",
579       "Autoplug and decode to raw media",
580       "Edward Hervey <edward@centricular.com>");
581
582   bin_klass->handle_message = gst_decodebin3_handle_message;
583
584   klass->select_stream = gst_decodebin3_select_stream;
585 }
586
587 static void
588 gst_decodebin3_init (GstDecodebin3 * dbin)
589 {
590   /* Create main input */
591   dbin->main_input = create_new_input (dbin, TRUE);
592
593   dbin->multiqueue = gst_element_factory_make ("multiqueue", NULL);
594   g_object_set (dbin->multiqueue, "sync-by-running-time", TRUE,
595       "max-size-buffers", 0, "use-interleave", TRUE, NULL);
596   gst_bin_add ((GstBin *) dbin, dbin->multiqueue);
597
598   dbin->current_group_id = G_MAXUINT32;
599
600   g_mutex_init (&dbin->factories_lock);
601   g_mutex_init (&dbin->selection_lock);
602   g_mutex_init (&dbin->input_lock);
603
604   dbin->caps = gst_static_caps_get (&default_raw_caps);
605 }
606
607 static void
608 gst_decodebin3_dispose (GObject * object)
609 {
610   GstDecodebin3 *dbin = (GstDecodebin3 *) object;
611
612   if (dbin->factories)
613     gst_plugin_feature_list_free (dbin->factories);
614   if (dbin->decoder_factories)
615     g_list_free (dbin->decoder_factories);
616   if (dbin->decodable_factories)
617     g_list_free (dbin->decodable_factories);
618   g_list_free (dbin->requested_selection);
619   g_list_free (dbin->active_selection);
620   g_list_free (dbin->to_activate);
621   g_list_free (dbin->pending_select_streams);
622   g_clear_object (&dbin->collection);
623
624   free_input (dbin, dbin->main_input);
625   /* FIXME : GO OVER INPUTS */
626
627   G_OBJECT_CLASS (parent_class)->dispose (object);
628 }
629
630 static void
631 gst_decodebin3_set_property (GObject * object, guint prop_id,
632     const GValue * value, GParamSpec * pspec)
633 {
634   GstDecodebin3 *dbin = (GstDecodebin3 *) object;
635
636   /* FIXME : IMPLEMENT */
637   switch (prop_id) {
638     case PROP_CAPS:
639       GST_OBJECT_LOCK (dbin);
640       if (dbin->caps)
641         gst_caps_unref (dbin->caps);
642       dbin->caps = g_value_dup_boxed (value);
643       GST_OBJECT_UNLOCK (dbin);
644       break;
645     default:
646       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
647       break;
648   }
649 }
650
651 static void
652 gst_decodebin3_get_property (GObject * object, guint prop_id, GValue * value,
653     GParamSpec * pspec)
654 {
655   GstDecodebin3 *dbin = (GstDecodebin3 *) object;
656
657   /* FIXME : IMPLEMENT */
658   switch (prop_id) {
659     case PROP_CAPS:
660       GST_OBJECT_LOCK (dbin);
661       g_value_set_boxed (value, dbin->caps);
662       GST_OBJECT_UNLOCK (dbin);
663       break;
664     default:
665       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
666       break;
667   }
668 }
669
670 static gboolean
671 parsebin_autoplug_continue_cb (GstElement * parsebin, GstPad * pad,
672     GstCaps * caps, GstDecodebin3 * dbin)
673 {
674   GST_DEBUG_OBJECT (pad, "caps %" GST_PTR_FORMAT, caps);
675
676   /* If it matches our target caps, expose it */
677   if (gst_caps_can_intersect (caps, dbin->caps))
678     return FALSE;
679
680   return TRUE;
681 }
682
683 /* This method should be called whenever a STREAM_START event
684  * comes out of a given parsebin.
685  * The caller shall replace the group_id if the function returns TRUE */
686 static gboolean
687 set_input_group_id (DecodebinInput * input, guint32 * group_id)
688 {
689   GstDecodebin3 *dbin = input->dbin;
690
691   if (input->group_id != *group_id) {
692     if (input->group_id != G_MAXUINT32)
693       GST_WARNING_OBJECT (dbin,
694           "Group id changed (%" G_GUINT32_FORMAT " -> %" G_GUINT32_FORMAT
695           ") on input %p ", input->group_id, *group_id, input);
696     input->group_id = *group_id;
697   }
698
699   if (*group_id != dbin->current_group_id) {
700     if (dbin->current_group_id == G_MAXUINT32) {
701       GST_DEBUG_OBJECT (dbin, "Setting current group id to %" G_GUINT32_FORMAT,
702           *group_id);
703       dbin->current_group_id = *group_id;
704     }
705     *group_id = dbin->current_group_id;
706     return TRUE;
707   }
708
709   return FALSE;
710 }
711
712 /* Call with INPUT_LOCK taken */
713 static gboolean
714 ensure_input_parsebin (GstDecodebin3 * dbin, DecodebinInput * input)
715 {
716   gboolean set_state = FALSE;
717
718   if (input->parsebin == NULL) {
719     input->parsebin = gst_element_factory_make ("parsebin", NULL);
720     if (input->parsebin == NULL)
721       goto no_parsebin;
722     input->parsebin = gst_object_ref (input->parsebin);
723     input->parsebin_sink = gst_element_get_static_pad (input->parsebin, "sink");
724     input->pad_added_sigid =
725         g_signal_connect (input->parsebin, "pad-added",
726         (GCallback) parsebin_pad_added_cb, input);
727     input->pad_removed_sigid =
728         g_signal_connect (input->parsebin, "pad-removed",
729         (GCallback) parsebin_pad_removed_cb, input);
730     g_signal_connect (input->parsebin, "autoplug-continue",
731         (GCallback) parsebin_autoplug_continue_cb, dbin);
732   }
733
734   if (GST_OBJECT_PARENT (GST_OBJECT (input->parsebin)) != GST_OBJECT (dbin)) {
735     gst_bin_add (GST_BIN (dbin), input->parsebin);
736     set_state = TRUE;
737   }
738
739   gst_ghost_pad_set_target (GST_GHOST_PAD (input->ghost_sink),
740       input->parsebin_sink);
741   if (set_state)
742     gst_element_sync_state_with_parent (input->parsebin);
743
744   return TRUE;
745
746   /* ERRORS */
747 no_parsebin:
748   {
749     gst_element_post_message ((GstElement *) dbin,
750         gst_missing_element_message_new ((GstElement *) dbin, "parsebin"));
751     return FALSE;
752   }
753 }
754
755 static GstPadLinkReturn
756 gst_decodebin3_input_pad_link (GstPad * pad, GstObject * parent, GstPad * peer)
757 {
758   GstDecodebin3 *dbin = (GstDecodebin3 *) parent;
759   GstPadLinkReturn res = GST_PAD_LINK_OK;
760   DecodebinInput *input;
761
762   GST_LOG_OBJECT (parent, "Got link on input pad %" GST_PTR_FORMAT
763       ". Creating parsebin if needed", pad);
764
765   if ((input = g_object_get_data (G_OBJECT (pad), "decodebin.input")) == NULL)
766     goto fail;
767
768   INPUT_LOCK (dbin);
769   if (!ensure_input_parsebin (dbin, input))
770     res = GST_PAD_LINK_REFUSED;
771   INPUT_UNLOCK (dbin);
772
773   return res;
774 fail:
775   GST_ERROR_OBJECT (parent, "Failed to retrieve input state from ghost pad");
776   return GST_PAD_LINK_REFUSED;
777 }
778
779 static void
780 gst_decodebin3_input_pad_unlink (GstPad * pad, GstObject * parent)
781 {
782   GstDecodebin3 *dbin = (GstDecodebin3 *) parent;
783   DecodebinInput *input;
784
785   GST_LOG_OBJECT (parent, "Got unlink on input pad %" GST_PTR_FORMAT
786       ". Removing parsebin.", pad);
787
788   if ((input = g_object_get_data (G_OBJECT (pad), "decodebin.input")) == NULL)
789     goto fail;
790
791   INPUT_LOCK (dbin);
792   if (input->parsebin == NULL) {
793     INPUT_UNLOCK (dbin);
794     return;
795   }
796
797   if (GST_OBJECT_PARENT (GST_OBJECT (input->parsebin)) == GST_OBJECT (dbin)) {
798     gst_bin_remove (GST_BIN (dbin), input->parsebin);
799     gst_element_set_state (input->parsebin, GST_STATE_NULL);
800   }
801   INPUT_UNLOCK (dbin);
802   return;
803
804 fail:
805   GST_ERROR_OBJECT (parent, "Failed to retrieve input state from ghost pad");
806   return;
807 }
808
809 static void
810 free_input (GstDecodebin3 * dbin, DecodebinInput * input)
811 {
812   GST_DEBUG ("Freeing input %p", input);
813   gst_ghost_pad_set_target (GST_GHOST_PAD (input->ghost_sink), NULL);
814   gst_element_remove_pad (GST_ELEMENT (dbin), input->ghost_sink);
815   if (input->parsebin) {
816     g_signal_handler_disconnect (input->parsebin, input->pad_removed_sigid);
817     g_signal_handler_disconnect (input->parsebin, input->pad_added_sigid);
818     gst_element_set_state (input->parsebin, GST_STATE_NULL);
819     gst_object_unref (input->parsebin);
820     gst_object_unref (input->parsebin_sink);
821   }
822   if (input->collection)
823     gst_object_unref (input->collection);
824   g_free (input);
825 }
826
827 /* Call with INPUT_LOCK taken */
828 static DecodebinInput *
829 create_new_input (GstDecodebin3 * dbin, gboolean main)
830 {
831   DecodebinInput *input;
832
833   input = g_new0 (DecodebinInput, 1);
834   input->dbin = dbin;
835   input->is_main = main;
836   input->group_id = G_MAXUINT32;
837   if (main)
838     input->ghost_sink = gst_ghost_pad_new_no_target ("sink", GST_PAD_SINK);
839   else {
840     gchar *pad_name = g_strdup_printf ("sink_%u", dbin->input_counter++);
841     input->ghost_sink = gst_ghost_pad_new_no_target (pad_name, GST_PAD_SINK);
842     g_free (pad_name);
843   }
844   g_object_set_data (G_OBJECT (input->ghost_sink), "decodebin.input", input);
845   gst_pad_set_link_function (input->ghost_sink, gst_decodebin3_input_pad_link);
846   gst_pad_set_unlink_function (input->ghost_sink,
847       gst_decodebin3_input_pad_unlink);
848
849   gst_pad_set_active (input->ghost_sink, TRUE);
850   gst_element_add_pad ((GstElement *) dbin, input->ghost_sink);
851
852   return input;
853
854 }
855
856 static GstPad *
857 gst_decodebin3_request_new_pad (GstElement * element, GstPadTemplate * temp,
858     const gchar * name, const GstCaps * caps)
859 {
860   GstDecodebin3 *dbin = (GstDecodebin3 *) element;
861   DecodebinInput *input;
862   GstPad *res = NULL;
863
864   /* We are ignoring names for the time being, not sure it makes any sense
865    * within the context of decodebin3 ... */
866   INPUT_LOCK (dbin);
867   input = create_new_input (dbin, FALSE);
868   if (input) {
869     dbin->other_inputs = g_list_append (dbin->other_inputs, input);
870     res = input->ghost_sink;
871   }
872   INPUT_UNLOCK (dbin);
873
874   return res;
875 }
876
877 /* Must be called with factories lock! */
878 static void
879 gst_decode_bin_update_factories_list (GstDecodebin3 * dbin)
880 {
881   guint cookie;
882
883   cookie = gst_registry_get_feature_list_cookie (gst_registry_get ());
884   if (!dbin->factories || dbin->factories_cookie != cookie) {
885     GList *tmp;
886     if (dbin->factories)
887       gst_plugin_feature_list_free (dbin->factories);
888     if (dbin->decoder_factories)
889       g_list_free (dbin->decoder_factories);
890     if (dbin->decodable_factories)
891       g_list_free (dbin->decodable_factories);
892     dbin->factories =
893         gst_element_factory_list_get_elements
894         (GST_ELEMENT_FACTORY_TYPE_DECODABLE, GST_RANK_MARGINAL);
895     dbin->factories =
896         g_list_sort (dbin->factories, gst_plugin_feature_rank_compare_func);
897     dbin->factories_cookie = cookie;
898
899     /* Filter decoder and other decodables */
900     dbin->decoder_factories = NULL;
901     dbin->decodable_factories = NULL;
902     for (tmp = dbin->factories; tmp; tmp = tmp->next) {
903       GstElementFactory *fact = (GstElementFactory *) tmp->data;
904       if (gst_element_factory_list_is_type (fact,
905               GST_ELEMENT_FACTORY_TYPE_DECODER))
906         dbin->decoder_factories = g_list_append (dbin->decoder_factories, fact);
907       else
908         dbin->decodable_factories =
909             g_list_append (dbin->decodable_factories, fact);
910     }
911   }
912 }
913
914 /* Must be called with appropriate lock if list is a protected variable */
915 static gboolean
916 stream_in_list (GList * list, const gchar * sid)
917 {
918   GList *tmp;
919
920 #if EXTRA_DEBUG
921   for (tmp = list; tmp; tmp = tmp->next) {
922     gchar *osid = (gchar *) tmp->data;
923     GST_DEBUG ("Checking %s against %s", sid, osid);
924   }
925 #endif
926
927   for (tmp = list; tmp; tmp = tmp->next) {
928     gchar *osid = (gchar *) tmp->data;
929     if (!g_strcmp0 (sid, osid))
930       return TRUE;
931   }
932
933   return FALSE;
934 }
935
936 static void
937 update_requested_selection (GstDecodebin3 * dbin,
938     GstStreamCollection * collection)
939 {
940   guint i, nb;
941   GList *tmp = NULL;
942   GstStreamType used_types = 0;
943
944   nb = gst_stream_collection_get_size (collection);
945
946   /* 1. Is there a pending SELECT_STREAMS we can return straight away since
947    *  the switch handler will take care of the pending selection */
948   SELECTION_LOCK (dbin);
949   if (dbin->pending_select_streams) {
950     GST_DEBUG_OBJECT (dbin,
951         "No need to create pending selection, SELECT_STREAMS underway");
952     goto beach;
953   }
954
955   /* 2. If not, are we in EXPOSE_ALL_MODE ? If so, match everything */
956   GST_FIXME_OBJECT (dbin, "Implement EXPOSE_ALL_MODE");
957
958   /* 3. If not, check if we already have some of the streams in the
959    * existing active/requested selection */
960   for (i = 0; i < nb; i++) {
961     GstStream *stream = gst_stream_collection_get_stream (collection, i);
962     const gchar *sid = gst_stream_get_stream_id (stream);
963     gint request = -1;
964     /* Fire select-stream signal to see if outside components want to
965      * hint at which streams should be selected */
966     g_signal_emit (G_OBJECT (dbin),
967         gst_decodebin3_signals[SIGNAL_SELECT_STREAM], 0, collection, stream,
968         &request);
969     GST_DEBUG_OBJECT (dbin, "stream %s , request:%d", sid, request);
970     if (request == 1 || (request == -1
971             && (stream_in_list (dbin->requested_selection, sid)
972                 || stream_in_list (dbin->active_selection, sid)))) {
973       GstStreamType curtype = gst_stream_get_stream_type (stream);
974       if (request == 1)
975         GST_DEBUG_OBJECT (dbin,
976             "Using stream requested by 'select-stream' signal : %s", sid);
977       else
978         GST_DEBUG_OBJECT (dbin,
979             "Re-using stream already present in requested or active selection : %s",
980             sid);
981       tmp = g_list_append (tmp, (gchar *) sid);
982       used_types |= curtype;
983     }
984   }
985
986   /* 4. If not, match one stream of each type */
987   for (i = 0; i < nb; i++) {
988     GstStream *stream = gst_stream_collection_get_stream (collection, i);
989     GstStreamType curtype = gst_stream_get_stream_type (stream);
990     if (!(used_types & curtype)) {
991       const gchar *sid = gst_stream_get_stream_id (stream);
992       GST_DEBUG_OBJECT (dbin, "Selecting stream '%s' of type %s",
993           sid, gst_stream_type_get_name (curtype));
994       tmp = g_list_append (tmp, (gchar *) sid);
995       used_types |= curtype;
996     }
997   }
998
999 beach:
1000   /* Finally set the requested selection */
1001   if (tmp) {
1002     if (dbin->requested_selection) {
1003       GST_FIXME_OBJECT (dbin,
1004           "Replacing non-NULL requested_selection, what should we do ??");
1005       g_list_free (dbin->requested_selection);
1006     }
1007     dbin->requested_selection = tmp;
1008     dbin->selection_updated = TRUE;
1009   }
1010   SELECTION_UNLOCK (dbin);
1011 }
1012
1013 /* Call with INPUT_LOCK taken */
1014 static GstStreamCollection *
1015 get_merged_collection (GstDecodebin3 * dbin)
1016 {
1017   gboolean needs_merge = FALSE;
1018   GstStreamCollection *res = NULL;
1019   GList *tmp;
1020   guint i, nb_stream;
1021
1022   /* First check if we need to do a merge or just return the only collection */
1023   res = dbin->main_input->collection;
1024
1025   for (tmp = dbin->other_inputs; tmp; tmp = tmp->next) {
1026     DecodebinInput *input = (DecodebinInput *) tmp->data;
1027     if (input->collection) {
1028       if (res) {
1029         needs_merge = TRUE;
1030         break;
1031       }
1032       res = input->collection;
1033     }
1034   }
1035
1036   if (!needs_merge) {
1037     GST_DEBUG_OBJECT (dbin, "No need to merge, returning %p", res);
1038     return gst_object_ref (res);
1039   }
1040
1041   /* We really need to create a new collection */
1042   /* FIXME : Some numbering scheme maybe ?? */
1043   res = gst_stream_collection_new ("decodebin3");
1044   if (dbin->main_input->collection) {
1045     nb_stream = gst_stream_collection_get_size (dbin->main_input->collection);
1046     GST_DEBUG_OBJECT (dbin, "main input %p %d", dbin->main_input, nb_stream);
1047     for (i = 0; i < nb_stream; i++) {
1048       GstStream *stream =
1049           gst_stream_collection_get_stream (dbin->main_input->collection, i);
1050       gst_stream_collection_add_stream (res, gst_object_ref (stream));
1051     }
1052   }
1053
1054   for (tmp = dbin->other_inputs; tmp; tmp = tmp->next) {
1055     DecodebinInput *input = (DecodebinInput *) tmp->data;
1056     GST_DEBUG_OBJECT (dbin, "input %p , collection %p", input,
1057         input->collection);
1058     if (input->collection) {
1059       nb_stream = gst_stream_collection_get_size (input->collection);
1060       GST_DEBUG_OBJECT (dbin, "nb_stream : %d", nb_stream);
1061       for (i = 0; i < nb_stream; i++) {
1062         GstStream *stream =
1063             gst_stream_collection_get_stream (input->collection, i);
1064         gst_stream_collection_add_stream (res, gst_object_ref (stream));
1065       }
1066     }
1067   }
1068
1069   return res;
1070 }
1071
1072 /* Call with INPUT_LOCK taken */
1073 static DecodebinInput *
1074 find_message_parsebin (GstDecodebin3 * dbin, GstElement * child)
1075 {
1076   DecodebinInput *input = NULL;
1077   GstElement *parent = gst_object_ref (child);
1078   GList *tmp;
1079
1080   do {
1081     GstElement *next_parent;
1082
1083     GST_DEBUG_OBJECT (dbin, "parent %s",
1084         parent ? GST_ELEMENT_NAME (parent) : "<NONE>");
1085
1086     if (parent == dbin->main_input->parsebin) {
1087       input = dbin->main_input;
1088       break;
1089     }
1090     for (tmp = dbin->other_inputs; tmp; tmp = tmp->next) {
1091       DecodebinInput *cur = (DecodebinInput *) tmp->data;
1092       if (parent == cur->parsebin) {
1093         input = cur;
1094         break;
1095       }
1096     }
1097     next_parent = (GstElement *) gst_element_get_parent (parent);
1098     gst_object_unref (parent);
1099     parent = next_parent;
1100
1101   } while (parent && parent != (GstElement *) dbin);
1102
1103   if (parent)
1104     gst_object_unref (parent);
1105
1106   return input;
1107 }
1108
1109 static gboolean
1110 stream_in_collection (GstDecodebin3 * dbin, gchar * sid)
1111 {
1112   guint i, len;
1113
1114   if (dbin->collection == NULL)
1115     return FALSE;
1116   len = gst_stream_collection_get_size (dbin->collection);
1117   for (i = 0; i < len; i++) {
1118     GstStream *stream = gst_stream_collection_get_stream (dbin->collection, i);
1119     const gchar *osid = gst_stream_get_stream_id (stream);
1120     if (!g_strcmp0 (sid, osid))
1121       return TRUE;
1122   }
1123
1124   return FALSE;
1125 }
1126
1127 /* Call with INPUT_LOCK taken */
1128 static void
1129 handle_stream_collection (GstDecodebin3 * dbin,
1130     GstStreamCollection * collection, GstElement * child)
1131 {
1132 #ifndef GST_DISABLE_GST_DEBUG
1133   const gchar *upstream_id;
1134   guint i;
1135 #endif
1136   DecodebinInput *input = find_message_parsebin (dbin, child);
1137
1138   if (!input) {
1139     GST_DEBUG_OBJECT (dbin,
1140         "Couldn't find corresponding input, most likely shutting down");
1141     return;
1142   }
1143
1144   /* Replace collection in input */
1145   if (input->collection)
1146     gst_object_unref (input->collection);
1147   input->collection = gst_object_ref (collection);
1148   GST_DEBUG_OBJECT (dbin, "Setting collection %p on input %p", collection,
1149       input);
1150
1151   /* Merge collection if needed */
1152   collection = get_merged_collection (dbin);
1153
1154 #ifndef GST_DISABLE_GST_DEBUG
1155   /* Just some debugging */
1156   upstream_id = gst_stream_collection_get_upstream_id (collection);
1157   GST_DEBUG ("Received Stream Collection. Upstream_id : %s", upstream_id);
1158   GST_DEBUG ("From input %p", input);
1159   GST_DEBUG ("  %d streams", gst_stream_collection_get_size (collection));
1160   for (i = 0; i < gst_stream_collection_get_size (collection); i++) {
1161     GstStream *stream = gst_stream_collection_get_stream (collection, i);
1162     GstTagList *taglist;
1163     GstCaps *caps;
1164
1165     GST_DEBUG ("   Stream '%s'", gst_stream_get_stream_id (stream));
1166     GST_DEBUG ("     type  : %s",
1167         gst_stream_type_get_name (gst_stream_get_stream_type (stream)));
1168     GST_DEBUG ("     flags : 0x%x", gst_stream_get_stream_flags (stream));
1169     taglist = gst_stream_get_tags (stream);
1170     GST_DEBUG ("     tags  : %" GST_PTR_FORMAT, taglist);
1171     caps = gst_stream_get_caps (stream);
1172     GST_DEBUG ("     caps  : %" GST_PTR_FORMAT, caps);
1173     if (taglist)
1174       gst_tag_list_unref (taglist);
1175     gst_caps_unref (caps);
1176   }
1177 #endif
1178
1179   /* Store collection for later usage */
1180   if (dbin->collection == NULL) {
1181     dbin->collection = collection;
1182   } else {
1183     /* We need to check who emitted this collection (the owner).
1184      * If we already had a collection from that user, this one is an update,
1185      * that is to say that we need to figure out how we are going to re-use
1186      * the streams/slot */
1187     GST_FIXME_OBJECT (dbin, "New collection but already had one ...");
1188     /* FIXME : When do we switch from pending collection to active collection ?
1189      * When all streams from active collection are drained in multiqueue output ? */
1190     gst_object_unref (dbin->collection);
1191     dbin->collection = collection;
1192     /* dbin->pending_collection = */
1193     /*     g_list_append (dbin->pending_collection, collection); */
1194   }
1195 }
1196
1197 static void
1198 gst_decodebin3_handle_message (GstBin * bin, GstMessage * message)
1199 {
1200   GstDecodebin3 *dbin = (GstDecodebin3 *) bin;
1201   gboolean posting_collection = FALSE;
1202
1203   GST_DEBUG_OBJECT (bin, "Got Message %s", GST_MESSAGE_TYPE_NAME (message));
1204
1205   switch (GST_MESSAGE_TYPE (message)) {
1206     case GST_MESSAGE_STREAM_COLLECTION:
1207     {
1208       GstStreamCollection *collection = NULL;
1209       gst_message_parse_stream_collection (message, &collection);
1210       if (collection) {
1211         INPUT_LOCK (dbin);
1212         handle_stream_collection (dbin, collection,
1213             (GstElement *) GST_MESSAGE_SRC (message));
1214         posting_collection = TRUE;
1215         INPUT_UNLOCK (dbin);
1216       }
1217       if (dbin->collection && collection != dbin->collection) {
1218         /* Replace collection message, we most likely aggregated it */
1219         GstMessage *new_msg;
1220         new_msg =
1221             gst_message_new_stream_collection ((GstObject *) dbin,
1222             dbin->collection);
1223         gst_message_unref (message);
1224         message = new_msg;
1225       }
1226       if (collection)
1227         gst_object_unref (collection);
1228       break;
1229     }
1230     default:
1231       break;
1232   }
1233
1234   GST_BIN_CLASS (parent_class)->handle_message (bin, message);
1235
1236   if (posting_collection) {
1237     /* Figure out a selection for that collection */
1238     update_requested_selection (dbin, dbin->collection);
1239   }
1240 }
1241
1242 static DecodebinOutputStream *
1243 find_free_compatible_output (GstDecodebin3 * dbin, GstStream * stream)
1244 {
1245   GList *tmp;
1246   GstStreamType stype = gst_stream_get_stream_type (stream);
1247
1248   for (tmp = dbin->output_streams; tmp; tmp = tmp->next) {
1249     DecodebinOutputStream *output = (DecodebinOutputStream *) tmp->data;
1250     if (output->type == stype && output->slot && output->slot->active_stream) {
1251       GstStream *tstream = output->slot->active_stream;
1252       if (!stream_in_list (dbin->requested_selection,
1253               (gchar *) gst_stream_get_stream_id (tstream))) {
1254         return output;
1255       }
1256     }
1257   }
1258
1259   return NULL;
1260 }
1261
1262 /* Give a certain slot, figure out if it should be linked to an
1263  * output stream
1264  * CALL WITH SELECTION LOCK TAKEN !*/
1265 static DecodebinOutputStream *
1266 get_output_for_slot (MultiQueueSlot * slot)
1267 {
1268   GstDecodebin3 *dbin = slot->dbin;
1269   DecodebinOutputStream *output = NULL;
1270   const gchar *stream_id;
1271   GstCaps *caps;
1272
1273   /* If we already have a configured output, just use it */
1274   if (slot->output != NULL)
1275     return slot->output;
1276
1277   /*
1278    * FIXME
1279    * 
1280    * This method needs to be split into multiple parts
1281    *
1282    * 1) Figure out whether stream should be exposed or not
1283    *   This is based on autoplug-continue, EXPOSE_ALL_MODE, or presence
1284    *   in the default stream attribution
1285    *
1286    * 2) Figure out whether an output stream should be created, whether
1287    *   we can re-use the output stream already linked to the slot, or
1288    *   whether we need to get re-assigned another (currently used) output
1289    *   stream.
1290    */
1291
1292   stream_id = gst_stream_get_stream_id (slot->active_stream);
1293   caps = gst_stream_get_caps (slot->active_stream);
1294   GST_DEBUG_OBJECT (dbin, "stream %s , %" GST_PTR_FORMAT, stream_id, caps);
1295   gst_caps_unref (caps);
1296
1297   /* 0. Emit autoplug-continue signal for pending caps ? */
1298   GST_FIXME_OBJECT (dbin, "emit autoplug-continue");
1299
1300   /* 1. if in EXPOSE_ALL_MODE, just accept */
1301   GST_FIXME_OBJECT (dbin, "Handle EXPOSE_ALL_MODE");
1302
1303 #if 0
1304   /* FIXME : The idea around this was to avoid activating a stream for
1305    *     which we have no decoder. Unfortunately it is way too
1306    *     expensive. Need to figure out a better solution */
1307   /* 2. Is there a potential decoder (if one is required) */
1308   if (!gst_caps_can_intersect (caps, dbin->caps)
1309       && !have_factory (dbin, (GstCaps *) caps,
1310           GST_ELEMENT_FACTORY_TYPE_DECODER)) {
1311     GST_WARNING_OBJECT (dbin, "Don't have a decoder for %" GST_PTR_FORMAT,
1312         caps);
1313     SELECTION_UNLOCK (dbin);
1314     gst_element_post_message (GST_ELEMENT_CAST (dbin),
1315         gst_missing_decoder_message_new (GST_ELEMENT_CAST (dbin), caps));
1316     SELECTION_LOCK (dbin);
1317     return NULL;
1318   }
1319 #endif
1320
1321   /* 3. In default mode check if we should expose */
1322   if (stream_in_list (dbin->requested_selection, stream_id)) {
1323     /* Check if we can steal an existing output stream we could re-use.
1324      * that is:
1325      * * an output stream whose slot->stream is not in requested
1326      * * and is of the same type as this stream
1327      */
1328     output = find_free_compatible_output (dbin, slot->active_stream);
1329     if (output) {
1330       /* Move this output from its current slot to this slot */
1331       dbin->to_activate =
1332           g_list_append (dbin->to_activate, (gchar *) stream_id);
1333       dbin->requested_selection =
1334           g_list_remove (dbin->requested_selection, stream_id);
1335       SELECTION_UNLOCK (dbin);
1336       gst_pad_add_probe (output->slot->src_pad, GST_PAD_PROBE_TYPE_IDLE,
1337           (GstPadProbeCallback) slot_unassign_probe, output->slot, NULL);
1338       SELECTION_LOCK (dbin);
1339       return NULL;
1340     }
1341
1342     output = create_output_stream (dbin, slot->type);
1343     output->slot = slot;
1344     GST_DEBUG ("Linking slot %p to new output %p", slot, output);
1345     slot->output = output;
1346     dbin->active_selection =
1347         g_list_append (dbin->active_selection, (gchar *) stream_id);
1348   } else
1349     GST_DEBUG ("Not creating any output for slot %p", slot);
1350
1351   return output;
1352 }
1353
1354 /* Returns SELECTED_STREAMS message if active_selection is equal to
1355  * requested_selection, else NULL.
1356  * Must be called with LOCK taken */
1357 static GstMessage *
1358 is_selection_done (GstDecodebin3 * dbin)
1359 {
1360   GList *tmp;
1361   GstMessage *msg;
1362
1363   if (!dbin->selection_updated)
1364     return NULL;
1365
1366   GST_LOG_OBJECT (dbin, "Checking");
1367
1368   if (dbin->to_activate != NULL) {
1369     GST_DEBUG ("Still have streams to activate");
1370     return NULL;
1371   }
1372   for (tmp = dbin->requested_selection; tmp; tmp = tmp->next) {
1373     GST_DEBUG ("Checking requested stream %s", (gchar *) tmp->data);
1374     if (!stream_in_list (dbin->active_selection, (gchar *) tmp->data)) {
1375       GST_DEBUG ("Not in active selection, returning");
1376       return NULL;
1377     }
1378   }
1379
1380   GST_DEBUG_OBJECT (dbin, "Selection active, creating message");
1381
1382   /* We are completely active */
1383   msg = gst_message_new_streams_selected ((GstObject *) dbin, dbin->collection);
1384   GST_MESSAGE_SEQNUM (msg) = dbin->select_streams_seqnum;
1385   for (tmp = dbin->output_streams; tmp; tmp = tmp->next) {
1386     DecodebinOutputStream *output = (DecodebinOutputStream *) tmp->data;
1387     if (output->slot) {
1388       GST_DEBUG_OBJECT (dbin, "Adding stream %s",
1389           gst_stream_get_stream_id (output->slot->active_stream));
1390
1391       gst_message_streams_selected_add (msg, output->slot->active_stream);
1392     } else
1393       GST_WARNING_OBJECT (dbin, "No valid slot for output %p", output);
1394   }
1395   dbin->selection_updated = FALSE;
1396   return msg;
1397 }
1398
1399 static GstPadProbeReturn
1400 multiqueue_src_probe (GstPad * pad, GstPadProbeInfo * info,
1401     MultiQueueSlot * slot)
1402 {
1403   GstPadProbeReturn ret = GST_PAD_PROBE_OK;
1404   GstDecodebin3 *dbin = slot->dbin;
1405
1406   if (GST_IS_EVENT (GST_PAD_PROBE_INFO_DATA (info))) {
1407     GstEvent *ev = GST_PAD_PROBE_INFO_EVENT (info);
1408
1409     GST_DEBUG_OBJECT (pad, "Got event %p %s", ev, GST_EVENT_TYPE_NAME (ev));
1410     switch (GST_EVENT_TYPE (ev)) {
1411       case GST_EVENT_STREAM_START:
1412       {
1413         GstStream *stream = NULL;
1414         const gchar *stream_id;
1415
1416         gst_event_parse_stream (ev, &stream);
1417         if (stream == NULL) {
1418           GST_ERROR_OBJECT (pad,
1419               "Got a STREAM_START event without a GstStream");
1420           break;
1421         }
1422         stream_id = gst_stream_get_stream_id (stream);
1423         GST_DEBUG_OBJECT (pad, "Stream Start '%s'", stream_id);
1424         if (slot->active_stream == NULL) {
1425           slot->active_stream = stream;
1426         } else if (slot->active_stream != stream) {
1427           GST_FIXME_OBJECT (pad, "Handle stream changes (%s => %s) !",
1428               gst_stream_get_stream_id (slot->active_stream),
1429               gst_stream_get_stream_id (stream));
1430           gst_object_unref (slot->active_stream);
1431           slot->active_stream = stream;
1432         } else
1433           gst_object_unref (stream);
1434 #if 0                           /* Disabled because stream-start is pushed for every buffer on every unlinked pad */
1435         {
1436           gboolean is_active, is_requested;
1437           /* Quick check to see if we're in the current selection */
1438           /* FIXME : Re-check all slot<=>output mappings based on requested_selection */
1439           SELECTION_LOCK (dbin);
1440           GST_DEBUG_OBJECT (dbin, "Checking active selection");
1441           is_active = stream_in_list (dbin->active_selection, stream_id);
1442           GST_DEBUG_OBJECT (dbin, "Checking requested selection");
1443           is_requested = stream_in_list (dbin->requested_selection, stream_id);
1444           SELECTION_UNLOCK (dbin);
1445           if (is_active)
1446             GST_DEBUG_OBJECT (pad, "Slot in ACTIVE selection (output:%p)",
1447                 slot->output);
1448           if (is_requested)
1449             GST_DEBUG_OBJECT (pad, "Slot in REQUESTED selection (output:%p)",
1450                 slot->output);
1451           else if (slot->output) {
1452             GST_DEBUG_OBJECT (pad,
1453                 "Slot needs to be deactivated ? It's no longer in requested selection");
1454           } else if (!is_active)
1455             GST_DEBUG_OBJECT (pad,
1456                 "Slot in neither active nor requested selection");
1457         }
1458 #endif
1459       }
1460         break;
1461       case GST_EVENT_CAPS:
1462       {
1463         /* Configure the output slot if needed */
1464         DecodebinOutputStream *output;
1465         GstMessage *msg = NULL;
1466         SELECTION_LOCK (dbin);
1467         output = get_output_for_slot (slot);
1468         if (output) {
1469           reconfigure_output_stream (output, slot);
1470           msg = is_selection_done (dbin);
1471         }
1472         SELECTION_UNLOCK (dbin);
1473         if (msg)
1474           gst_element_post_message ((GstElement *) slot->dbin, msg);
1475       }
1476         break;
1477       case GST_EVENT_EOS:
1478         /* FIXME : Figure out */
1479         GST_FIXME_OBJECT (pad, "EOS on multiqueue source pad. input:%p",
1480             slot->input);
1481         if (slot->input == NULL) {
1482           GstPad *peer;
1483           GST_DEBUG_OBJECT (pad,
1484               "last EOS for input, forwarding and removing slot");
1485           peer = gst_pad_get_peer (pad);
1486           if (peer) {
1487             gst_pad_send_event (peer, ev);
1488             gst_object_unref (peer);
1489           } else {
1490             gst_event_unref (ev);
1491           }
1492           SELECTION_LOCK (dbin);
1493           /* FIXME : Shouldn't we try to re-assign the output instead of just
1494            * removing it ? */
1495           /* Remove the output */
1496           if (slot->output) {
1497             DecodebinOutputStream *output = slot->output;
1498             dbin->output_streams = g_list_remove (dbin->output_streams, output);
1499             free_output_stream (dbin, output);
1500           }
1501           SELECTION_UNLOCK (dbin);
1502           ret = GST_PAD_PROBE_HANDLED;
1503         }
1504         break;
1505       default:
1506         break;
1507     }
1508   } else if (GST_IS_QUERY (GST_PAD_PROBE_INFO_DATA (info))) {
1509     GstQuery *query = GST_PAD_PROBE_INFO_QUERY (info);
1510     switch (GST_QUERY_TYPE (query)) {
1511       case GST_QUERY_CAPS:
1512       {
1513         GST_DEBUG_OBJECT (pad, "Intercepting CAPS query");
1514         gst_query_set_caps_result (query, GST_CAPS_ANY);
1515         ret = GST_PAD_PROBE_HANDLED;
1516       }
1517         break;
1518
1519       case GST_QUERY_ACCEPT_CAPS:
1520       {
1521         GST_DEBUG_OBJECT (pad, "Intercepting Accept Caps query");
1522         /* If the current decoder doesn't accept caps, we'll reconfigure
1523          * on the actual caps event. So accept any caps. */
1524         gst_query_set_accept_caps_result (query, TRUE);
1525         ret = GST_PAD_PROBE_HANDLED;
1526       }
1527       default:
1528         break;
1529     }
1530   }
1531
1532   return ret;
1533 }
1534
1535 /* Create a new multiqueue slot for the given type
1536  *
1537  * It is up to the caller to know whether that slot is needed or not
1538  * (and release it when no longer needed) */
1539 static MultiQueueSlot *
1540 create_new_slot (GstDecodebin3 * dbin, GstStreamType type)
1541 {
1542   MultiQueueSlot *slot;
1543   GstIterator *it = NULL;
1544   GValue item = { 0, };
1545
1546   GST_DEBUG_OBJECT (dbin, "Creating new slot for type %s",
1547       gst_stream_type_get_name (type));
1548   slot = g_new0 (MultiQueueSlot, 1);
1549   slot->dbin = dbin;
1550   slot->id = dbin->slot_id++;
1551   slot->type = type;
1552   slot->sink_pad = gst_element_get_request_pad (dbin->multiqueue, "sink_%u");
1553   if (slot->sink_pad == NULL)
1554     goto fail;
1555   it = gst_pad_iterate_internal_links (slot->sink_pad);
1556   if (!it || (gst_iterator_next (it, &item)) != GST_ITERATOR_OK
1557       || ((slot->src_pad = g_value_dup_object (&item)) == NULL)) {
1558     GST_ERROR ("Couldn't get srcpad from multiqueue for sink pad %s:%s",
1559         GST_DEBUG_PAD_NAME (slot->src_pad));
1560     goto fail;
1561   }
1562   gst_iterator_free (it);
1563   g_value_reset (&item);
1564
1565   g_object_set (slot->sink_pad, "group-id", (guint) type, NULL);
1566
1567   /* Add event probe */
1568   slot->probe_id =
1569       gst_pad_add_probe (slot->src_pad,
1570       GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM | GST_PAD_PROBE_TYPE_QUERY_DOWNSTREAM,
1571       (GstPadProbeCallback) multiqueue_src_probe, slot, NULL);
1572
1573   GST_DEBUG ("Created new slot %u (%p) (%s:%s)", slot->id, slot,
1574       GST_DEBUG_PAD_NAME (slot->src_pad));
1575   dbin->slots = g_list_append (dbin->slots, slot);
1576   return slot;
1577
1578   /* ERRORS */
1579 fail:
1580   {
1581     if (slot->sink_pad)
1582       gst_element_release_request_pad (dbin->multiqueue, slot->sink_pad);
1583     g_free (slot);
1584     return NULL;
1585   }
1586 }
1587
1588 static MultiQueueSlot *
1589 get_slot_for_input (GstDecodebin3 * dbin, DecodebinInputStream * input)
1590 {
1591   GList *tmp;
1592   MultiQueueSlot *empty_slot = NULL;
1593   GstStreamType input_type = 0;
1594   gchar *stream_id = NULL;
1595
1596   GST_DEBUG_OBJECT (dbin, "input %p (stream %p %s)",
1597       input, input->active_stream,
1598       input->
1599       active_stream ? gst_stream_get_stream_id (input->active_stream) : "");
1600
1601   if (input->active_stream) {
1602     input_type = gst_stream_get_stream_type (input->active_stream);
1603     stream_id = (gchar *) gst_stream_get_stream_id (input->active_stream);
1604   }
1605
1606   /* Go over existing slots and check if there is already one for it */
1607   for (tmp = dbin->slots; tmp; tmp = tmp->next) {
1608     MultiQueueSlot *slot = (MultiQueueSlot *) tmp->data;
1609     /* Already used input, return that one */
1610     if (slot->input == input) {
1611       GST_DEBUG_OBJECT (dbin, "Returning already specified slot %d", slot->id);
1612       return slot;
1613     }
1614   }
1615
1616   /* Go amongst all unused slots of the right type and try to find a candidate */
1617   for (tmp = dbin->slots; tmp; tmp = tmp->next) {
1618     MultiQueueSlot *slot = (MultiQueueSlot *) tmp->data;
1619     if (slot->input == NULL && input_type == slot->type) {
1620       /* Remember this empty slot for later */
1621       empty_slot = slot;
1622       /* Check if available slot is of the same stream_id */
1623       GST_LOG_OBJECT (dbin, "Checking candidate slot %d (active_stream:%p)",
1624           slot->id, slot->active_stream);
1625       if (stream_id && slot->active_stream) {
1626         gchar *ostream_id =
1627             (gchar *) gst_stream_get_stream_id (slot->active_stream);
1628         GST_DEBUG_OBJECT (dbin, "Checking slot %d %s against %s", slot->id,
1629             ostream_id, stream_id);
1630         if (!g_strcmp0 (stream_id, ostream_id))
1631           break;
1632       }
1633     }
1634   }
1635
1636   if (empty_slot) {
1637     GST_DEBUG_OBJECT (dbin, "Re-using existing unused slot %d", empty_slot->id);
1638     empty_slot->input = input;
1639     return empty_slot;
1640   }
1641
1642   if (input_type)
1643     return create_new_slot (dbin, input_type);
1644
1645   return NULL;
1646 }
1647
1648 static void
1649 link_input_to_slot (DecodebinInputStream * input, MultiQueueSlot * slot)
1650 {
1651   GstEvent *event;
1652   if (slot->input != NULL && slot->input != input) {
1653     GST_ERROR_OBJECT (slot->dbin,
1654         "Trying to link input to an already used slot");
1655     return;
1656   }
1657   gst_pad_link_full (input->srcpad, slot->sink_pad, GST_PAD_LINK_CHECK_NOTHING);
1658   slot->pending_stream = input->active_stream;
1659   slot->input = input;
1660   event = gst_pad_get_sticky_event (input->srcpad, GST_EVENT_STREAM_START, 0);
1661   if (event)
1662     gst_pad_send_event (slot->sink_pad, event);
1663 }
1664
1665 #if 0
1666 static gboolean
1667 have_factory (GstDecodebin3 * dbin, GstCaps * caps,
1668     GstElementFactoryListType ftype)
1669 {
1670   gboolean ret = FALSE;
1671   GList *res;
1672
1673   g_mutex_lock (&dbin->factories_lock);
1674   gst_decode_bin_update_factories_list (dbin);
1675   if (ftype == GST_ELEMENT_FACTORY_TYPE_DECODER)
1676     res =
1677         gst_element_factory_list_filter (dbin->decoder_factories,
1678         caps, GST_PAD_SINK, TRUE);
1679   else
1680     res =
1681         gst_element_factory_list_filter (dbin->decodable_factories,
1682         caps, GST_PAD_SINK, TRUE);
1683   g_mutex_unlock (&dbin->factories_lock);
1684
1685   if (res) {
1686     ret = TRUE;
1687     gst_plugin_feature_list_free (res);
1688   }
1689
1690   return ret;
1691 }
1692 #endif
1693
1694 static GstElement *
1695 create_element (GstDecodebin3 * dbin, GstStream * stream,
1696     GstElementFactoryListType ftype)
1697 {
1698   GList *res;
1699   GstElement *element = NULL;
1700   GstCaps *caps;
1701
1702   g_mutex_lock (&dbin->factories_lock);
1703   gst_decode_bin_update_factories_list (dbin);
1704   caps = gst_stream_get_caps (stream);
1705   if (ftype == GST_ELEMENT_FACTORY_TYPE_DECODER)
1706     res =
1707         gst_element_factory_list_filter (dbin->decoder_factories,
1708         caps, GST_PAD_SINK, TRUE);
1709   else
1710     res =
1711         gst_element_factory_list_filter (dbin->decodable_factories,
1712         caps, GST_PAD_SINK, TRUE);
1713   g_mutex_unlock (&dbin->factories_lock);
1714
1715   if (res) {
1716     element =
1717         gst_element_factory_create ((GstElementFactory *) res->data, NULL);
1718     GST_DEBUG ("Created element '%s'", GST_ELEMENT_NAME (element));
1719     gst_plugin_feature_list_free (res);
1720   } else {
1721     GST_DEBUG ("Could not find an element for caps %" GST_PTR_FORMAT, caps);
1722   }
1723
1724   gst_caps_unref (caps);
1725   return element;
1726 }
1727
1728 /* FIXME : VERY NAIVE. ASSUMING FIRST ONE WILL WORK */
1729 static GstElement *
1730 create_decoder (GstDecodebin3 * dbin, GstStream * stream)
1731 {
1732   return create_element (dbin, stream, GST_ELEMENT_FACTORY_TYPE_DECODER);
1733 }
1734
1735 static GstPadProbeReturn
1736 keyframe_waiter_probe (GstPad * pad, GstPadProbeInfo * info,
1737     DecodebinOutputStream * output)
1738 {
1739   GstBuffer *buf = GST_PAD_PROBE_INFO_BUFFER (info);
1740   /* If we have a keyframe, remove the probe and let all data through */
1741   /* FIXME : HANDLE HEADER BUFFER ?? */
1742   if (!GST_BUFFER_FLAG_IS_SET (buf, GST_BUFFER_FLAG_DELTA_UNIT) ||
1743       GST_BUFFER_FLAG_IS_SET (buf, GST_BUFFER_FLAG_HEADER)) {
1744     GST_DEBUG_OBJECT (pad,
1745         "Buffer is keyframe or header, letting through and removing probe");
1746     output->drop_probe_id = 0;
1747     return GST_PAD_PROBE_REMOVE;
1748   }
1749   GST_DEBUG_OBJECT (pad, "Buffer is not a keyframe, dropping");
1750   return GST_PAD_PROBE_DROP;
1751 }
1752
1753 static void
1754 reconfigure_output_stream (DecodebinOutputStream * output,
1755     MultiQueueSlot * slot)
1756 {
1757   GstDecodebin3 *dbin = output->dbin;
1758   GstCaps *new_caps = (GstCaps *) gst_stream_get_caps (slot->active_stream);
1759   gboolean needs_decoder;
1760
1761   needs_decoder = gst_caps_can_intersect (new_caps, dbin->caps) != TRUE;
1762
1763   GST_DEBUG_OBJECT (dbin,
1764       "Reconfiguring output %p to slot %p, needs_decoder:%d", output, slot,
1765       needs_decoder);
1766
1767   /* FIXME : Maybe make the output un-hook itself automatically ? */
1768   if (output->slot != NULL && output->slot != slot) {
1769     GST_WARNING_OBJECT (dbin,
1770         "Output still linked to another slot (%p)", output->slot);
1771     gst_caps_unref (new_caps);
1772     return;
1773   }
1774
1775   /* Check if existing config is reusable as-is by checking if
1776    * the existing decoder accepts the new caps, if not delete
1777    * it and create a new one */
1778   if (output->decoder) {
1779     gboolean can_reuse_decoder;
1780
1781     if (needs_decoder) {
1782       can_reuse_decoder =
1783           gst_pad_query_accept_caps (output->decoder_sink, new_caps);
1784     } else
1785       can_reuse_decoder = FALSE;
1786
1787     if (can_reuse_decoder) {
1788       if (output->type == GST_STREAM_TYPE_VIDEO && output->drop_probe_id == 0) {
1789         GST_DEBUG_OBJECT (dbin, "Adding keyframe-waiter probe");
1790         output->drop_probe_id =
1791             gst_pad_add_probe (slot->src_pad, GST_PAD_PROBE_TYPE_BUFFER,
1792             (GstPadProbeCallback) keyframe_waiter_probe, output, NULL);
1793       }
1794       GST_DEBUG_OBJECT (dbin, "Reusing existing decoder for slot %p", slot);
1795       if (output->linked == FALSE) {
1796         gst_pad_link_full (slot->src_pad, output->decoder_sink,
1797             GST_PAD_LINK_CHECK_NOTHING);
1798         output->linked = TRUE;
1799       }
1800       gst_caps_unref (new_caps);
1801       return;
1802     }
1803
1804     GST_DEBUG_OBJECT (dbin, "Removing old decoder for slot %p", slot);
1805
1806     if (output->linked)
1807       gst_pad_unlink (slot->src_pad, output->decoder_sink);
1808     output->linked = FALSE;
1809     if (output->drop_probe_id) {
1810       gst_pad_remove_probe (slot->src_pad, output->drop_probe_id);
1811       output->drop_probe_id = 0;
1812     }
1813
1814     if (!gst_ghost_pad_set_target ((GstGhostPad *) output->src_pad, NULL)) {
1815       GST_ERROR_OBJECT (dbin, "Could not release decoder pad");
1816       gst_caps_unref (new_caps);
1817       goto cleanup;
1818     }
1819
1820     gst_object_replace ((GstObject **) & output->decoder_sink, NULL);
1821     gst_object_replace ((GstObject **) & output->decoder_src, NULL);
1822
1823     gst_element_set_locked_state (output->decoder, TRUE);
1824     gst_element_set_state (output->decoder, GST_STATE_NULL);
1825
1826     gst_bin_remove ((GstBin *) dbin, output->decoder);
1827     output->decoder = NULL;
1828   }
1829
1830   gst_caps_unref (new_caps);
1831
1832   /* If a decoder is required, create one */
1833   if (needs_decoder) {
1834     /* If we don't have a decoder yet, instantiate one */
1835     output->decoder = create_decoder (dbin, slot->active_stream);
1836     if (output->decoder == NULL) {
1837       GstCaps *caps;
1838
1839       SELECTION_UNLOCK (dbin);
1840       /* FIXME : Should we be smarter if there's a missing decoder ?
1841        * Should we deactivate that stream ? */
1842       caps = gst_stream_get_caps (slot->active_stream);
1843       gst_element_post_message (GST_ELEMENT_CAST (dbin),
1844           gst_missing_decoder_message_new (GST_ELEMENT_CAST (dbin), caps));
1845       gst_caps_unref (caps);
1846       SELECTION_LOCK (dbin);
1847       goto cleanup;
1848     }
1849     if (!gst_bin_add ((GstBin *) dbin, output->decoder)) {
1850       GST_ERROR_OBJECT (dbin, "could not add decoder to pipeline");
1851       goto cleanup;
1852     }
1853     output->decoder_sink = gst_element_get_static_pad (output->decoder, "sink");
1854     output->decoder_src = gst_element_get_static_pad (output->decoder, "src");
1855     if (output->type == GST_STREAM_TYPE_VIDEO) {
1856       GST_DEBUG_OBJECT (dbin, "Adding keyframe-waiter probe");
1857       output->drop_probe_id =
1858           gst_pad_add_probe (slot->src_pad, GST_PAD_PROBE_TYPE_BUFFER,
1859           (GstPadProbeCallback) keyframe_waiter_probe, output, NULL);
1860     }
1861     if (gst_pad_link_full (slot->src_pad, output->decoder_sink,
1862             GST_PAD_LINK_CHECK_NOTHING) != GST_PAD_LINK_OK) {
1863       GST_ERROR_OBJECT (dbin, "could not link to %s:%s",
1864           GST_DEBUG_PAD_NAME (output->decoder_sink));
1865       goto cleanup;
1866     }
1867   } else {
1868     output->decoder_src = gst_object_ref (slot->src_pad);
1869     output->decoder_sink = NULL;
1870   }
1871   output->linked = TRUE;
1872   if (!gst_ghost_pad_set_target ((GstGhostPad *) output->src_pad,
1873           output->decoder_src)) {
1874     GST_ERROR_OBJECT (dbin, "Could not expose decoder pad");
1875     goto cleanup;
1876   }
1877   if (output->src_exposed == FALSE) {
1878     output->src_exposed = TRUE;
1879     gst_element_add_pad (GST_ELEMENT_CAST (dbin), output->src_pad);
1880   }
1881
1882   if (output->decoder)
1883     gst_element_sync_state_with_parent (output->decoder);
1884
1885   output->slot = slot;
1886   return;
1887
1888 cleanup:
1889   {
1890     GST_DEBUG_OBJECT (dbin, "Cleanup");
1891     if (output->decoder_sink) {
1892       gst_object_unref (output->decoder_sink);
1893       output->decoder_sink = NULL;
1894     }
1895     if (output->decoder_src) {
1896       gst_object_unref (output->decoder_src);
1897       output->decoder_src = NULL;
1898     }
1899     if (output->decoder) {
1900       gst_element_set_state (output->decoder, GST_STATE_NULL);
1901       gst_bin_remove ((GstBin *) dbin, output->decoder);
1902       output->decoder = NULL;
1903     }
1904   }
1905 }
1906
1907 static GstPadProbeReturn
1908 idle_reconfigure (GstPad * pad, GstPadProbeInfo * info, MultiQueueSlot * slot)
1909 {
1910   GstMessage *msg = NULL;
1911   DecodebinOutputStream *output;
1912
1913   SELECTION_LOCK (slot->dbin);
1914   output = get_output_for_slot (slot);
1915
1916   GST_DEBUG_OBJECT (pad, "output : %p", output);
1917
1918   if (output) {
1919     reconfigure_output_stream (output, slot);
1920     msg = is_selection_done (slot->dbin);
1921   }
1922   SELECTION_UNLOCK (slot->dbin);
1923   if (msg)
1924     gst_element_post_message ((GstElement *) slot->dbin, msg);
1925
1926   return GST_PAD_PROBE_REMOVE;
1927 }
1928
1929 static MultiQueueSlot *
1930 find_slot_for_stream_id (GstDecodebin3 * dbin, const gchar * sid)
1931 {
1932   GList *tmp;
1933
1934   for (tmp = dbin->slots; tmp; tmp = tmp->next) {
1935     MultiQueueSlot *slot = (MultiQueueSlot *) tmp->data;
1936     const gchar *stream_id;
1937     if (slot->active_stream) {
1938       stream_id = gst_stream_get_stream_id (slot->active_stream);
1939       if (!g_strcmp0 (sid, stream_id))
1940         return slot;
1941     }
1942     if (slot->pending_stream && slot->pending_stream != slot->active_stream) {
1943       stream_id = gst_stream_get_stream_id (slot->pending_stream);
1944       if (!g_strcmp0 (sid, stream_id))
1945         return slot;
1946     }
1947   }
1948
1949   return NULL;
1950 }
1951
1952 /* This function handles the reassignment of a slot. Call this from
1953  * the streaming thread of a slot. */
1954 static gboolean
1955 reassign_slot (GstDecodebin3 * dbin, MultiQueueSlot * slot)
1956 {
1957   DecodebinOutputStream *output;
1958   MultiQueueSlot *target_slot = NULL;
1959   GList *tmp;
1960   const gchar *sid, *tsid;
1961
1962   SELECTION_LOCK (dbin);
1963   output = slot->output;
1964
1965   if (G_UNLIKELY (slot->active_stream == NULL)) {
1966     GST_DEBUG_OBJECT (slot->src_pad,
1967         "Called on inactive slot (active_stream == NULL)");
1968     SELECTION_UNLOCK (dbin);
1969     return FALSE;
1970   }
1971
1972   if (G_UNLIKELY (output == NULL)) {
1973     GST_DEBUG_OBJECT (slot->src_pad,
1974         "Slot doesn't have any output to be removed");
1975     SELECTION_UNLOCK (dbin);
1976     return FALSE;
1977   }
1978
1979   sid = gst_stream_get_stream_id (slot->active_stream);
1980   GST_DEBUG_OBJECT (slot->src_pad, "slot %s %p", sid, slot);
1981
1982   /* Recheck whether this stream is still in the list of streams to deactivate */
1983   if (stream_in_list (dbin->requested_selection, sid)) {
1984     /* Stream is in the list of requested streams, don't remove */
1985     SELECTION_UNLOCK (dbin);
1986     GST_DEBUG_OBJECT (slot->src_pad,
1987         "Stream '%s' doesn't need to be deactivated", sid);
1988     return FALSE;
1989   }
1990
1991   /* Unlink slot from output */
1992   /* FIXME : Handle flushing ? */
1993   /* FIXME : Handle outputs without decoders */
1994   GST_DEBUG_OBJECT (slot->src_pad, "Unlinking from decoder %p",
1995       output->decoder_sink);
1996   if (output->decoder_sink)
1997     gst_pad_unlink (slot->src_pad, output->decoder_sink);
1998   output->linked = FALSE;
1999   slot->output = NULL;
2000   output->slot = NULL;
2001   /* Remove sid from active selection */
2002   for (tmp = dbin->active_selection; tmp; tmp = tmp->next)
2003     if (!g_strcmp0 (sid, tmp->data)) {
2004       dbin->active_selection = g_list_delete_link (dbin->active_selection, tmp);
2005       break;
2006     }
2007
2008   /* Can we re-assign this output to a requested stream ? */
2009   GST_DEBUG_OBJECT (slot->src_pad, "Attempting to re-assing output stream");
2010   for (tmp = dbin->to_activate; tmp; tmp = tmp->next) {
2011     MultiQueueSlot *tslot = find_slot_for_stream_id (dbin, tmp->data);
2012     GST_LOG_OBJECT (tslot->src_pad, "Checking slot %p (output:%p , stream:%s)",
2013         tslot, tslot->output, gst_stream_get_stream_id (tslot->active_stream));
2014     if (tslot && tslot->type == output->type && tslot->output == NULL) {
2015       GST_DEBUG_OBJECT (tslot->src_pad, "Using as reassigned slot");
2016       target_slot = tslot;
2017       tsid = tmp->data;
2018       /* Pass target stream id to requested selection */
2019       dbin->requested_selection =
2020           g_list_append (dbin->requested_selection, tmp->data);
2021       dbin->to_activate = g_list_remove (dbin->to_activate, tmp->data);
2022       break;
2023     }
2024   }
2025
2026   if (target_slot) {
2027     GST_DEBUG_OBJECT (slot->src_pad, "Assigning output to slot %p '%s'",
2028         target_slot, tsid);
2029     target_slot->output = output;
2030     output->slot = target_slot;
2031     dbin->active_selection =
2032         g_list_append (dbin->active_selection, (gchar *) tsid);
2033     SELECTION_UNLOCK (dbin);
2034
2035     /* Wakeup the target slot so that it retries to send events/buffers
2036      * thereby triggering the output reconfiguration codepath */
2037     gst_pad_add_probe (target_slot->src_pad, GST_PAD_PROBE_TYPE_IDLE,
2038         (GstPadProbeCallback) idle_reconfigure, target_slot, NULL);
2039     /* gst_pad_send_event (target_slot->src_pad, gst_event_new_reconfigure ()); */
2040   } else {
2041     GstMessage *msg;
2042
2043     dbin->output_streams = g_list_remove (dbin->output_streams, output);
2044     free_output_stream (dbin, output);
2045     msg = is_selection_done (slot->dbin);
2046     SELECTION_UNLOCK (dbin);
2047
2048     if (msg)
2049       gst_element_post_message ((GstElement *) slot->dbin, msg);
2050   }
2051
2052   return TRUE;
2053 }
2054
2055 /* Idle probe called when a slot should be unassigned from its output stream.
2056  * This is needed to ensure nothing is flowing when unlinking the slot.
2057  *
2058  * Also, this method will search for a pending stream which could re-use
2059  * the output stream. */
2060 static GstPadProbeReturn
2061 slot_unassign_probe (GstPad * pad, GstPadProbeInfo * info,
2062     MultiQueueSlot * slot)
2063 {
2064   GstDecodebin3 *dbin = slot->dbin;
2065
2066   reassign_slot (dbin, slot);
2067
2068   return GST_PAD_PROBE_REMOVE;
2069 }
2070
2071 static gboolean
2072 handle_stream_switch (GstDecodebin3 * dbin, GList * select_streams,
2073     guint32 seqnum)
2074 {
2075   gboolean ret = TRUE;
2076   GList *tmp;
2077   /* List of slots to (de)activate. */
2078   GList *to_deactivate = NULL;
2079   GList *to_activate = NULL;
2080   /* List of unknown stream id, most likely means the event
2081    * should be sent upstream so that elements can expose the requested stream */
2082   GList *unknown = NULL;
2083   GList *to_reassign = NULL;
2084   GList *future_request_streams = NULL;
2085   GList *pending_streams = NULL;
2086   GList *slots_to_reassign = NULL;
2087
2088   SELECTION_LOCK (dbin);
2089   if (G_UNLIKELY (seqnum != dbin->select_streams_seqnum)) {
2090     GST_DEBUG_OBJECT (dbin, "New SELECT_STREAMS has arrived in the meantime");
2091     SELECTION_UNLOCK (dbin);
2092     return TRUE;
2093   }
2094   /* Remove pending select_streams */
2095   g_list_free (dbin->pending_select_streams);
2096   dbin->pending_select_streams = NULL;
2097
2098   /* COMPARE the requested streams to the active and requested streams
2099    * on multiqueue. */
2100
2101   /* First check the slots to activate and which ones are unknown */
2102   for (tmp = select_streams; tmp; tmp = tmp->next) {
2103     const gchar *sid = (const gchar *) tmp->data;
2104     MultiQueueSlot *slot;
2105     GST_DEBUG_OBJECT (dbin, "Checking stream '%s'", sid);
2106     slot = find_slot_for_stream_id (dbin, sid);
2107     /* Find the corresponding slot */
2108     if (slot == NULL) {
2109       if (stream_in_collection (dbin, (gchar *) sid)) {
2110         pending_streams = g_list_append (pending_streams, (gchar *) sid);
2111       } else {
2112         GST_DEBUG_OBJECT (dbin, "We don't have a slot for stream '%s'", sid);
2113         unknown = g_list_append (unknown, (gchar *) sid);
2114       }
2115     } else if (slot->output == NULL) {
2116       GST_DEBUG_OBJECT (dbin, "We need to activate slot %p for stream '%s')",
2117           slot, sid);
2118       to_activate = g_list_append (to_activate, slot);
2119     } else {
2120       GST_DEBUG_OBJECT (dbin,
2121           "Stream '%s' from slot %p is already active on output %p", sid, slot,
2122           slot->output);
2123       future_request_streams =
2124           g_list_append (future_request_streams, (gchar *) sid);
2125     }
2126   }
2127
2128   for (tmp = dbin->slots; tmp; tmp = tmp->next) {
2129     MultiQueueSlot *slot = (MultiQueueSlot *) tmp->data;
2130     /* For slots that have an output, check if it's part of the streams to
2131      * be active */
2132     if (slot->output) {
2133       gboolean slot_to_deactivate = TRUE;
2134
2135       if (slot->active_stream) {
2136         if (stream_in_list (select_streams,
2137                 gst_stream_get_stream_id (slot->active_stream)))
2138           slot_to_deactivate = FALSE;
2139       }
2140       if (slot_to_deactivate && slot->pending_stream
2141           && slot->pending_stream != slot->active_stream) {
2142         if (stream_in_list (select_streams,
2143                 gst_stream_get_stream_id (slot->pending_stream)))
2144           slot_to_deactivate = FALSE;
2145       }
2146       if (slot_to_deactivate) {
2147         GST_DEBUG_OBJECT (dbin,
2148             "Slot %p (%s) should be deactivated, no longer used", slot,
2149             gst_stream_get_stream_id (slot->active_stream));
2150         to_deactivate = g_list_append (to_deactivate, slot);
2151       }
2152     }
2153   }
2154
2155   if (to_deactivate != NULL) {
2156     GST_DEBUG_OBJECT (dbin, "Check if we can reassign slots");
2157     /* We need to compare what needs to be activated and deactivated in order
2158      * to determine whether there are outputs that can be transferred */
2159     /* Take the stream-id of the slots that are to be activated, for which there
2160      * is a slot of the same type that needs to be deactivated */
2161     tmp = to_deactivate;
2162     while (tmp) {
2163       MultiQueueSlot *slot_to_deactivate = (MultiQueueSlot *) tmp->data;
2164       gboolean removeit = FALSE;
2165       GList *tmp2, *next;
2166       GST_DEBUG_OBJECT (dbin,
2167           "Checking if slot to deactivate (%p) has a candidate slot to activate",
2168           slot_to_deactivate);
2169       for (tmp2 = to_activate; tmp2; tmp2 = tmp2->next) {
2170         MultiQueueSlot *slot_to_activate = (MultiQueueSlot *) tmp2->data;
2171         GST_DEBUG_OBJECT (dbin, "Comparing to slot %p", slot_to_activate);
2172         if (slot_to_activate->type == slot_to_deactivate->type) {
2173           GST_DEBUG_OBJECT (dbin, "Re-using");
2174           to_reassign = g_list_append (to_reassign, (gchar *)
2175               gst_stream_get_stream_id (slot_to_activate->active_stream));
2176           slots_to_reassign =
2177               g_list_append (slots_to_reassign, slot_to_deactivate);
2178           to_activate = g_list_remove (to_activate, slot_to_activate);
2179           removeit = TRUE;
2180           break;
2181         }
2182       }
2183       next = tmp->next;
2184       if (removeit)
2185         to_deactivate = g_list_delete_link (to_deactivate, tmp);
2186       tmp = next;
2187     }
2188   }
2189
2190   for (tmp = to_deactivate; tmp; tmp = tmp->next) {
2191     MultiQueueSlot *slot = (MultiQueueSlot *) tmp->data;
2192     GST_DEBUG_OBJECT (dbin,
2193         "Really need to deactivate slot %p, but no available alternative",
2194         slot);
2195
2196     slots_to_reassign = g_list_append (slots_to_reassign, slot);
2197   }
2198
2199   /* The only slots left to activate are the ones that won't be reassigned and
2200    * therefore really need to have a new output created */
2201   for (tmp = to_activate; tmp; tmp = tmp->next) {
2202     MultiQueueSlot *slot = (MultiQueueSlot *) tmp->data;
2203     if (slot->active_stream)
2204       future_request_streams =
2205           g_list_append (future_request_streams,
2206           (gchar *) gst_stream_get_stream_id (slot->active_stream));
2207     else if (slot->pending_stream)
2208       future_request_streams =
2209           g_list_append (future_request_streams,
2210           (gchar *) gst_stream_get_stream_id (slot->pending_stream));
2211     else
2212       GST_ERROR_OBJECT (dbin, "No stream for slot %p !!", slot);
2213   }
2214
2215   if (to_activate == NULL && pending_streams != NULL) {
2216     GST_DEBUG_OBJECT (dbin, "Stream switch requested for future collection");
2217     if (dbin->requested_selection)
2218       g_list_free (dbin->requested_selection);
2219     dbin->requested_selection = select_streams;
2220     g_list_free (to_deactivate);
2221     g_list_free (pending_streams);
2222     to_deactivate = NULL;
2223   } else {
2224     if (dbin->requested_selection)
2225       g_list_free (dbin->requested_selection);
2226     dbin->requested_selection = future_request_streams;
2227     dbin->requested_selection =
2228         g_list_concat (dbin->requested_selection, pending_streams);
2229     if (dbin->to_activate)
2230       g_list_free (dbin->to_activate);
2231     dbin->to_activate = to_reassign;
2232   }
2233
2234   dbin->selection_updated = TRUE;
2235   SELECTION_UNLOCK (dbin);
2236
2237   if (unknown)
2238     GST_FIXME_OBJECT (dbin, "Got request for an unknown stream");
2239
2240   /* For all streams to deactivate, add an idle probe where we will do
2241    * the unassignment and switch over */
2242   for (tmp = slots_to_reassign; tmp; tmp = tmp->next) {
2243     MultiQueueSlot *slot = (MultiQueueSlot *) tmp->data;
2244     gst_pad_add_probe (slot->src_pad, GST_PAD_PROBE_TYPE_IDLE,
2245         (GstPadProbeCallback) slot_unassign_probe, slot, NULL);
2246   }
2247
2248   return ret;
2249 }
2250
2251 static GstPadProbeReturn
2252 ghost_pad_event_probe (GstPad * pad, GstPadProbeInfo * info,
2253     DecodebinOutputStream * output)
2254 {
2255   GstPadProbeReturn ret = GST_PAD_PROBE_OK;
2256   GstDecodebin3 *dbin = output->dbin;
2257   GstEvent *event = GST_PAD_PROBE_INFO_EVENT (info);
2258
2259   GST_DEBUG_OBJECT (pad, "Got event %p %s", event, GST_EVENT_TYPE_NAME (event));
2260
2261   switch (GST_EVENT_TYPE (event)) {
2262     case GST_EVENT_SELECT_STREAMS:
2263     {
2264       GstPad *peer;
2265       GList *streams = NULL;
2266       guint32 seqnum = gst_event_get_seqnum (event);
2267
2268       SELECTION_LOCK (dbin);
2269       if (seqnum == dbin->select_streams_seqnum) {
2270         SELECTION_UNLOCK (dbin);
2271         GST_DEBUG_OBJECT (pad,
2272             "Already handled/handling that SELECT_STREAMS event");
2273         break;
2274       }
2275       dbin->select_streams_seqnum = seqnum;
2276       if (dbin->pending_select_streams != NULL) {
2277         GST_LOG_OBJECT (dbin, "Replacing pending select streams");
2278         g_list_free (dbin->pending_select_streams);
2279         dbin->pending_select_streams = NULL;
2280       }
2281       gst_event_parse_select_streams (event, &streams);
2282       dbin->pending_select_streams = g_list_copy (streams);
2283       SELECTION_UNLOCK (dbin);
2284
2285       /* Send event upstream */
2286       if ((peer = gst_pad_get_peer (pad))) {
2287         gst_pad_send_event (peer, event);
2288         gst_object_unref (peer);
2289       } else {
2290         gst_event_unref (event);
2291       }
2292       /* Finally handle the switch */
2293       if (streams)
2294         handle_stream_switch (dbin, streams, seqnum);
2295       ret = GST_PAD_PROBE_HANDLED;
2296     }
2297       break;
2298     default:
2299       break;
2300   }
2301
2302   return ret;
2303 }
2304
2305 static gboolean
2306 gst_decodebin3_send_event (GstElement * element, GstEvent * event)
2307 {
2308   GST_DEBUG_OBJECT (element, "event %s", GST_EVENT_TYPE_NAME (event));
2309   if (GST_EVENT_TYPE (event) == GST_EVENT_SELECT_STREAMS) {
2310     GstDecodebin3 *dbin = (GstDecodebin3 *) element;
2311     GList *streams = NULL;
2312     guint32 seqnum = gst_event_get_seqnum (event);
2313
2314     SELECTION_LOCK (dbin);
2315     if (seqnum == dbin->select_streams_seqnum) {
2316       SELECTION_UNLOCK (dbin);
2317       GST_DEBUG_OBJECT (dbin,
2318           "Already handled/handling that SELECT_STREAMS event");
2319       return TRUE;
2320     }
2321     dbin->select_streams_seqnum = seqnum;
2322     if (dbin->pending_select_streams != NULL) {
2323       GST_LOG_OBJECT (dbin, "Replacing pending select streams");
2324       g_list_free (dbin->pending_select_streams);
2325       dbin->pending_select_streams = NULL;
2326     }
2327     gst_event_parse_select_streams (event, &streams);
2328     dbin->pending_select_streams = g_list_copy (streams);
2329     SELECTION_UNLOCK (dbin);
2330
2331     /* FIXME : We don't have an upstream ?? */
2332 #if 0
2333     /* Send event upstream */
2334     if ((peer = gst_pad_get_peer (pad))) {
2335       gst_pad_send_event (peer, event);
2336       gst_object_unref (peer);
2337     }
2338 #endif
2339     /* Finally handle the switch */
2340     if (streams)
2341       handle_stream_switch (dbin, streams, seqnum);
2342
2343     gst_event_unref (event);
2344     return TRUE;
2345   }
2346   return GST_ELEMENT_CLASS (parent_class)->send_event (element, event);
2347 }
2348
2349
2350 static void
2351 free_multiqueue_slot (GstDecodebin3 * dbin, MultiQueueSlot * slot)
2352 {
2353   if (slot->probe_id)
2354     gst_pad_remove_probe (slot->src_pad, slot->probe_id);
2355   if (slot->input) {
2356     if (slot->input->srcpad)
2357       gst_pad_unlink (slot->input->srcpad, slot->sink_pad);
2358   }
2359
2360   gst_element_release_request_pad (dbin->multiqueue, slot->sink_pad);
2361   gst_object_replace ((GstObject **) & slot->sink_pad, NULL);
2362   gst_object_replace ((GstObject **) & slot->src_pad, NULL);
2363   gst_object_replace ((GstObject **) & slot->active_stream, NULL);
2364   g_free (slot);
2365 }
2366
2367 /* Create a DecodebinOutputStream for a given type
2368  * Note: It will be empty initially, it needs to be configured
2369  * afterwards */
2370 static DecodebinOutputStream *
2371 create_output_stream (GstDecodebin3 * dbin, GstStreamType type)
2372 {
2373   DecodebinOutputStream *res = g_new0 (DecodebinOutputStream, 1);
2374   gchar *pad_name;
2375   const gchar *prefix;
2376   GstStaticPadTemplate *templ;
2377   GstPadTemplate *ptmpl;
2378   guint32 *counter;
2379   GstPad *internal_pad;
2380
2381   GST_DEBUG_OBJECT (dbin, "Created new output stream %p for type %s",
2382       res, gst_stream_type_get_name (type));
2383
2384   res->type = type;
2385   res->dbin = dbin;
2386
2387   switch (type) {
2388     case GST_STREAM_TYPE_VIDEO:
2389       templ = &video_src_template;
2390       counter = &dbin->vpadcount;
2391       prefix = "video";
2392       break;
2393     case GST_STREAM_TYPE_AUDIO:
2394       templ = &audio_src_template;
2395       counter = &dbin->apadcount;
2396       prefix = "audio";
2397       break;
2398     case GST_STREAM_TYPE_TEXT:
2399       templ = &text_src_template;
2400       counter = &dbin->tpadcount;
2401       prefix = "text";
2402       break;
2403     default:
2404       templ = &src_template;
2405       counter = &dbin->opadcount;
2406       prefix = "src";
2407       break;
2408   }
2409
2410   pad_name = g_strdup_printf ("%s_%u", prefix, *counter);
2411   *counter += 1;
2412   ptmpl = gst_static_pad_template_get (templ);
2413   res->src_pad = gst_ghost_pad_new_no_target_from_template (pad_name, ptmpl);
2414   gst_object_unref (ptmpl);
2415   g_free (pad_name);
2416   gst_pad_set_active (res->src_pad, TRUE);
2417   /* Put an event probe on the internal proxy pad to detect upstream
2418    * events */
2419   internal_pad =
2420       (GstPad *) gst_proxy_pad_get_internal ((GstProxyPad *) res->src_pad);
2421   gst_pad_add_probe (internal_pad, GST_PAD_PROBE_TYPE_EVENT_UPSTREAM,
2422       (GstPadProbeCallback) ghost_pad_event_probe, res, NULL);
2423   gst_object_unref (internal_pad);
2424
2425   dbin->output_streams = g_list_append (dbin->output_streams, res);
2426
2427   return res;
2428 }
2429
2430 static void
2431 free_output_stream (GstDecodebin3 * dbin, DecodebinOutputStream * output)
2432 {
2433   if (output->slot) {
2434     if (output->decoder_sink && output->decoder)
2435       gst_pad_unlink (output->slot->src_pad, output->decoder_sink);
2436
2437     output->slot->output = NULL;
2438     output->slot = NULL;
2439   }
2440   gst_object_replace ((GstObject **) & output->decoder_sink, NULL);
2441   gst_ghost_pad_set_target ((GstGhostPad *) output->src_pad, NULL);
2442   gst_object_replace ((GstObject **) & output->decoder_src, NULL);
2443   if (output->src_exposed) {
2444     gst_element_remove_pad ((GstElement *) dbin, output->src_pad);
2445   }
2446   if (output->decoder) {
2447     gst_element_set_locked_state (output->decoder, TRUE);
2448     gst_element_set_state (output->decoder, GST_STATE_NULL);
2449     gst_bin_remove ((GstBin *) dbin, output->decoder);
2450   }
2451   g_free (output);
2452 }
2453
2454 static GstStateChangeReturn
2455 gst_decodebin3_change_state (GstElement * element, GstStateChange transition)
2456 {
2457   GstDecodebin3 *dbin = (GstDecodebin3 *) element;
2458   GstStateChangeReturn ret;
2459
2460   /* Upwards */
2461   switch (transition) {
2462     default:
2463       break;
2464   }
2465   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
2466   if (ret == GST_STATE_CHANGE_FAILURE)
2467     goto beach;
2468
2469   switch (transition) {
2470     case GST_STATE_CHANGE_PAUSED_TO_READY:
2471     {
2472       GList *tmp;
2473
2474       /* Free output streams */
2475       for (tmp = dbin->output_streams; tmp; tmp = tmp->next) {
2476         DecodebinOutputStream *output = (DecodebinOutputStream *) tmp->data;
2477         free_output_stream (dbin, output);
2478       }
2479       g_list_free (dbin->output_streams);
2480       dbin->output_streams = NULL;
2481       /* Free multiqueue slots */
2482       for (tmp = dbin->slots; tmp; tmp = tmp->next) {
2483         MultiQueueSlot *slot = (MultiQueueSlot *) tmp->data;
2484         free_multiqueue_slot (dbin, slot);
2485       }
2486       g_list_free (dbin->slots);
2487       dbin->slots = NULL;
2488       /* Free inputs */
2489     }
2490       break;
2491     default:
2492       break;
2493   }
2494 beach:
2495   return ret;
2496 }
2497
2498 gboolean
2499 gst_decodebin3_plugin_init (GstPlugin * plugin)
2500 {
2501   GST_DEBUG_CATEGORY_INIT (decodebin3_debug, "decodebin3", 0, "decoder bin");
2502
2503   return gst_element_register (plugin, "decodebin3", GST_RANK_NONE,
2504       GST_TYPE_DECODEBIN3);
2505 }