playback: Flesh out docs a bit for new elements
[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   g_free (input);
823 }
824
825 /* Call with INPUT_LOCK taken */
826 static DecodebinInput *
827 create_new_input (GstDecodebin3 * dbin, gboolean main)
828 {
829   DecodebinInput *input;
830
831   input = g_new0 (DecodebinInput, 1);
832   input->dbin = dbin;
833   input->is_main = main;
834   input->group_id = G_MAXUINT32;
835   if (main)
836     input->ghost_sink = gst_ghost_pad_new_no_target ("sink", GST_PAD_SINK);
837   else {
838     gchar *pad_name = g_strdup_printf ("sink_%u", dbin->input_counter++);
839     input->ghost_sink = gst_ghost_pad_new_no_target (pad_name, GST_PAD_SINK);
840     g_free (pad_name);
841   }
842   g_object_set_data (G_OBJECT (input->ghost_sink), "decodebin.input", input);
843   gst_pad_set_link_function (input->ghost_sink, gst_decodebin3_input_pad_link);
844   gst_pad_set_unlink_function (input->ghost_sink,
845       gst_decodebin3_input_pad_unlink);
846
847   gst_pad_set_active (input->ghost_sink, TRUE);
848   gst_element_add_pad ((GstElement *) dbin, input->ghost_sink);
849
850   return input;
851
852 }
853
854 static GstPad *
855 gst_decodebin3_request_new_pad (GstElement * element, GstPadTemplate * temp,
856     const gchar * name, const GstCaps * caps)
857 {
858   GstDecodebin3 *dbin = (GstDecodebin3 *) element;
859   DecodebinInput *input;
860   GstPad *res = NULL;
861
862   /* We are ignoring names for the time being, not sure it makes any sense
863    * within the context of decodebin3 ... */
864   INPUT_LOCK (dbin);
865   input = create_new_input (dbin, FALSE);
866   if (input) {
867     dbin->other_inputs = g_list_append (dbin->other_inputs, input);
868     res = input->ghost_sink;
869   }
870   INPUT_UNLOCK (dbin);
871
872   return res;
873 }
874
875 /* Must be called with factories lock! */
876 static void
877 gst_decode_bin_update_factories_list (GstDecodebin3 * dbin)
878 {
879   guint cookie;
880
881   cookie = gst_registry_get_feature_list_cookie (gst_registry_get ());
882   if (!dbin->factories || dbin->factories_cookie != cookie) {
883     GList *tmp;
884     if (dbin->factories)
885       gst_plugin_feature_list_free (dbin->factories);
886     if (dbin->decoder_factories)
887       g_list_free (dbin->decoder_factories);
888     if (dbin->decodable_factories)
889       g_list_free (dbin->decodable_factories);
890     dbin->factories =
891         gst_element_factory_list_get_elements
892         (GST_ELEMENT_FACTORY_TYPE_DECODABLE, GST_RANK_MARGINAL);
893     dbin->factories =
894         g_list_sort (dbin->factories, gst_plugin_feature_rank_compare_func);
895     dbin->factories_cookie = cookie;
896
897     /* Filter decoder and other decodables */
898     dbin->decoder_factories = NULL;
899     dbin->decodable_factories = NULL;
900     for (tmp = dbin->factories; tmp; tmp = tmp->next) {
901       GstElementFactory *fact = (GstElementFactory *) tmp->data;
902       if (gst_element_factory_list_is_type (fact,
903               GST_ELEMENT_FACTORY_TYPE_DECODER))
904         dbin->decoder_factories = g_list_append (dbin->decoder_factories, fact);
905       else
906         dbin->decodable_factories =
907             g_list_append (dbin->decodable_factories, fact);
908     }
909   }
910 }
911
912 /* Must be called with appropriate lock if list is a protected variable */
913 static gboolean
914 stream_in_list (GList * list, const gchar * sid)
915 {
916   GList *tmp;
917
918 #if EXTRA_DEBUG
919   for (tmp = list; tmp; tmp = tmp->next) {
920     gchar *osid = (gchar *) tmp->data;
921     GST_DEBUG ("Checking %s against %s", sid, osid);
922   }
923 #endif
924
925   for (tmp = list; tmp; tmp = tmp->next) {
926     gchar *osid = (gchar *) tmp->data;
927     if (!g_strcmp0 (sid, osid))
928       return TRUE;
929   }
930
931   return FALSE;
932 }
933
934 static void
935 update_requested_selection (GstDecodebin3 * dbin,
936     GstStreamCollection * collection)
937 {
938   guint i, nb;
939   GList *tmp = NULL;
940   GstStreamType used_types = 0;
941
942   nb = gst_stream_collection_get_size (collection);
943
944   /* 1. Is there a pending SELECT_STREAMS we can return straight away since
945    *  the switch handler will take care of the pending selection */
946   SELECTION_LOCK (dbin);
947   if (dbin->pending_select_streams) {
948     GST_DEBUG_OBJECT (dbin,
949         "No need to create pending selection, SELECT_STREAMS underway");
950     goto beach;
951   }
952
953   /* 2. If not, are we in EXPOSE_ALL_MODE ? If so, match everything */
954   GST_FIXME_OBJECT (dbin, "Implement EXPOSE_ALL_MODE");
955
956   /* 3. If not, check if we already have some of the streams in the
957    * existing active/requested selection */
958   for (i = 0; i < nb; i++) {
959     GstStream *stream = gst_stream_collection_get_stream (collection, i);
960     const gchar *sid = gst_stream_get_stream_id (stream);
961     gint request = -1;
962     /* Fire select-stream signal to see if outside components want to
963      * hint at which streams should be selected */
964     g_signal_emit (G_OBJECT (dbin),
965         gst_decodebin3_signals[SIGNAL_SELECT_STREAM], 0, collection, stream,
966         &request);
967     GST_DEBUG_OBJECT (dbin, "stream %s , request:%d", sid, request);
968     if (request == 1 || (request == -1
969             && (stream_in_list (dbin->requested_selection, sid)
970                 || stream_in_list (dbin->active_selection, sid)))) {
971       GstStreamType curtype = gst_stream_get_stream_type (stream);
972       if (request == 1)
973         GST_DEBUG_OBJECT (dbin,
974             "Using stream requested by 'select-stream' signal : %s", sid);
975       else
976         GST_DEBUG_OBJECT (dbin,
977             "Re-using stream already present in requested or active selection : %s",
978             sid);
979       tmp = g_list_append (tmp, (gchar *) sid);
980       used_types |= curtype;
981     }
982   }
983
984   /* 4. If not, match one stream of each type */
985   for (i = 0; i < nb; i++) {
986     GstStream *stream = gst_stream_collection_get_stream (collection, i);
987     GstStreamType curtype = gst_stream_get_stream_type (stream);
988     if (!(used_types & curtype)) {
989       const gchar *sid = gst_stream_get_stream_id (stream);
990       GST_DEBUG_OBJECT (dbin, "Selecting stream '%s' of type %s",
991           sid, gst_stream_type_get_name (curtype));
992       tmp = g_list_append (tmp, (gchar *) sid);
993       used_types |= curtype;
994     }
995   }
996
997 beach:
998   /* Finally set the requested selection */
999   if (tmp) {
1000     if (dbin->requested_selection) {
1001       GST_FIXME_OBJECT (dbin,
1002           "Replacing non-NULL requested_selection, what should we do ??");
1003       g_list_free (dbin->requested_selection);
1004     }
1005     dbin->requested_selection = tmp;
1006     dbin->selection_updated = TRUE;
1007   }
1008   SELECTION_UNLOCK (dbin);
1009 }
1010
1011 /* Call with INPUT_LOCK taken */
1012 static GstStreamCollection *
1013 get_merged_collection (GstDecodebin3 * dbin)
1014 {
1015   gboolean needs_merge = FALSE;
1016   GstStreamCollection *res = NULL;
1017   GList *tmp;
1018   guint i, nb_stream;
1019
1020   /* First check if we need to do a merge or just return the only collection */
1021   res = dbin->main_input->collection;
1022
1023   for (tmp = dbin->other_inputs; tmp; tmp = tmp->next) {
1024     DecodebinInput *input = (DecodebinInput *) tmp->data;
1025     if (input->collection) {
1026       if (res) {
1027         needs_merge = TRUE;
1028         break;
1029       }
1030       res = input->collection;
1031     }
1032   }
1033
1034   if (!needs_merge) {
1035     GST_DEBUG_OBJECT (dbin, "No need to merge, returning %p", res);
1036     return res;
1037   }
1038
1039   /* We really need to create a new collection */
1040   /* FIXME : Some numbering scheme maybe ?? */
1041   res = gst_stream_collection_new ("decodebin3");
1042   if (dbin->main_input->collection) {
1043     nb_stream = gst_stream_collection_get_size (dbin->main_input->collection);
1044     GST_DEBUG_OBJECT (dbin, "main input %p %d", dbin->main_input, nb_stream);
1045     for (i = 0; i < nb_stream; i++) {
1046       GstStream *stream =
1047           gst_stream_collection_get_stream (dbin->main_input->collection, i);
1048       gst_stream_collection_add_stream (res, stream);
1049     }
1050   }
1051
1052   for (tmp = dbin->other_inputs; tmp; tmp = tmp->next) {
1053     DecodebinInput *input = (DecodebinInput *) tmp->data;
1054     GST_DEBUG_OBJECT (dbin, "input %p , collection %p", input,
1055         input->collection);
1056     if (input->collection) {
1057       nb_stream = gst_stream_collection_get_size (input->collection);
1058       GST_DEBUG_OBJECT (dbin, "nb_stream : %d", nb_stream);
1059       for (i = 0; i < nb_stream; i++) {
1060         GstStream *stream =
1061             gst_stream_collection_get_stream (input->collection, i);
1062         gst_stream_collection_add_stream (res, stream);
1063       }
1064     }
1065   }
1066
1067   return res;
1068 }
1069
1070 /* Call with INPUT_LOCK taken */
1071 static DecodebinInput *
1072 find_message_parsebin (GstDecodebin3 * dbin, GstElement * child)
1073 {
1074   DecodebinInput *input = NULL;
1075   GstElement *parent = gst_object_ref (child);
1076   GList *tmp;
1077
1078   do {
1079     GstElement *next_parent;
1080
1081     GST_DEBUG_OBJECT (dbin, "parent %s",
1082         parent ? GST_ELEMENT_NAME (parent) : "<NONE>");
1083
1084     if (parent == dbin->main_input->parsebin) {
1085       input = dbin->main_input;
1086       break;
1087     }
1088     for (tmp = dbin->other_inputs; tmp; tmp = tmp->next) {
1089       DecodebinInput *cur = (DecodebinInput *) tmp->data;
1090       if (parent == cur->parsebin) {
1091         input = cur;
1092         break;
1093       }
1094     }
1095     next_parent = (GstElement *) gst_element_get_parent (parent);
1096     gst_object_unref (parent);
1097     parent = next_parent;
1098
1099   } while (parent && parent != (GstElement *) dbin);
1100
1101   if (parent)
1102     gst_object_unref (parent);
1103
1104   return input;
1105 }
1106
1107 static gboolean
1108 stream_in_collection (GstDecodebin3 * dbin, gchar * sid)
1109 {
1110   guint i, len;
1111
1112   if (dbin->collection == NULL)
1113     return FALSE;
1114   len = gst_stream_collection_get_size (dbin->collection);
1115   for (i = 0; i < len; i++) {
1116     GstStream *stream = gst_stream_collection_get_stream (dbin->collection, i);
1117     const gchar *osid = gst_stream_get_stream_id (stream);
1118     if (!g_strcmp0 (sid, osid))
1119       return TRUE;
1120   }
1121
1122   return FALSE;
1123 }
1124
1125 /* Call with INPUT_LOCK taken */
1126 static void
1127 handle_stream_collection (GstDecodebin3 * dbin,
1128     GstStreamCollection * collection, GstElement * child)
1129 {
1130 #ifndef GST_DISABLE_GST_DEBUG
1131   const gchar *upstream_id;
1132   guint i;
1133 #endif
1134   DecodebinInput *input = find_message_parsebin (dbin, child);
1135
1136   if (!input) {
1137     GST_DEBUG_OBJECT (dbin,
1138         "Couldn't find corresponding input, most likely shutting down");
1139     return;
1140   }
1141
1142   /* Replace collection in input */
1143   if (input->collection)
1144     gst_object_unref (input->collection);
1145   input->collection = collection;
1146   GST_DEBUG_OBJECT (dbin, "Setting collection %p on input %p", collection,
1147       input);
1148
1149   /* Merge collection if needed */
1150   collection = get_merged_collection (dbin);
1151
1152 #ifndef GST_DISABLE_GST_DEBUG
1153   /* Just some debugging */
1154   upstream_id = gst_stream_collection_get_upstream_id (collection);
1155   GST_DEBUG ("Received Stream Collection. Upstream_id : %s", upstream_id);
1156   GST_DEBUG ("From input %p", input);
1157   GST_DEBUG ("  %d streams", gst_stream_collection_get_size (collection));
1158   for (i = 0; i < gst_stream_collection_get_size (collection); i++) {
1159     GstStream *stream = gst_stream_collection_get_stream (collection, i);
1160     const GstTagList *taglist;
1161     GstCaps *caps;
1162
1163     GST_DEBUG ("   Stream '%s'", gst_stream_get_stream_id (stream));
1164     GST_DEBUG ("     type  : %s",
1165         gst_stream_type_get_name (gst_stream_get_stream_type (stream)));
1166     GST_DEBUG ("     flags : 0x%x", gst_stream_get_stream_flags (stream));
1167     taglist = gst_stream_get_tags (stream);
1168     GST_DEBUG ("     tags  : %" GST_PTR_FORMAT, taglist);
1169     caps = gst_stream_get_caps (stream);
1170     GST_DEBUG ("     caps  : %" GST_PTR_FORMAT, caps);
1171     gst_caps_unref (caps);
1172   }
1173 #endif
1174
1175   /* Store collection for later usage */
1176   if (dbin->collection == NULL) {
1177     dbin->collection = collection;
1178   } else {
1179     /* We need to check who emitted this collection (the owner).
1180      * If we already had a collection from that user, this one is an update,
1181      * that is to say that we need to figure out how we are going to re-use
1182      * the streams/slot */
1183     GST_FIXME_OBJECT (dbin, "New collection but already had one ...");
1184     /* FIXME : When do we switch from pending collection to active collection ?
1185      * When all streams from active collection are drained in multiqueue output ? */
1186     gst_object_unref (dbin->collection);
1187     dbin->collection = collection;
1188     /* dbin->pending_collection = */
1189     /*     g_list_append (dbin->pending_collection, collection); */
1190   }
1191 }
1192
1193 static void
1194 gst_decodebin3_handle_message (GstBin * bin, GstMessage * message)
1195 {
1196   GstDecodebin3 *dbin = (GstDecodebin3 *) bin;
1197   gboolean posting_collection = FALSE;
1198
1199   GST_DEBUG_OBJECT (bin, "Got Message %s", GST_MESSAGE_TYPE_NAME (message));
1200
1201   switch (GST_MESSAGE_TYPE (message)) {
1202     case GST_MESSAGE_STREAM_COLLECTION:
1203     {
1204       GstStreamCollection *collection = NULL;
1205       gst_message_parse_stream_collection (message, &collection);
1206       if (collection) {
1207         INPUT_LOCK (dbin);
1208         handle_stream_collection (dbin, collection,
1209             (GstElement *) GST_MESSAGE_SRC (message));
1210         posting_collection = TRUE;
1211         INPUT_UNLOCK (dbin);
1212       }
1213       if (dbin->collection && collection != dbin->collection) {
1214         /* Replace collection message, we most likely aggregated it */
1215         GstMessage *new_msg;
1216         new_msg =
1217             gst_message_new_stream_collection ((GstObject *) dbin,
1218             dbin->collection);
1219         gst_message_unref (message);
1220         message = new_msg;
1221       }
1222       if (collection)
1223         gst_object_unref (collection);
1224       break;
1225     }
1226     default:
1227       break;
1228   }
1229
1230   GST_BIN_CLASS (parent_class)->handle_message (bin, message);
1231
1232   if (posting_collection) {
1233     /* Figure out a selection for that collection */
1234     update_requested_selection (dbin, dbin->collection);
1235   }
1236 }
1237
1238 static DecodebinOutputStream *
1239 find_free_compatible_output (GstDecodebin3 * dbin, GstStream * stream)
1240 {
1241   GList *tmp;
1242   GstStreamType stype = gst_stream_get_stream_type (stream);
1243
1244   for (tmp = dbin->output_streams; tmp; tmp = tmp->next) {
1245     DecodebinOutputStream *output = (DecodebinOutputStream *) tmp->data;
1246     if (output->type == stype && output->slot && output->slot->active_stream) {
1247       GstStream *tstream = output->slot->active_stream;
1248       if (!stream_in_list (dbin->requested_selection,
1249               (gchar *) gst_stream_get_stream_id (tstream))) {
1250         return output;
1251       }
1252     }
1253   }
1254
1255   return NULL;
1256 }
1257
1258 /* Give a certain slot, figure out if it should be linked to an
1259  * output stream
1260  * CALL WITH SELECTION LOCK TAKEN !*/
1261 static DecodebinOutputStream *
1262 get_output_for_slot (MultiQueueSlot * slot)
1263 {
1264   GstDecodebin3 *dbin = slot->dbin;
1265   DecodebinOutputStream *output = NULL;
1266   const gchar *stream_id;
1267   GstCaps *caps;
1268
1269   /* If we already have a configured output, just use it */
1270   if (slot->output != NULL)
1271     return slot->output;
1272
1273   /*
1274    * FIXME
1275    * 
1276    * This method needs to be split into multiple parts
1277    *
1278    * 1) Figure out whether stream should be exposed or not
1279    *   This is based on autoplug-continue, EXPOSE_ALL_MODE, or presence
1280    *   in the default stream attribution
1281    *
1282    * 2) Figure out whether an output stream should be created, whether
1283    *   we can re-use the output stream already linked to the slot, or
1284    *   whether we need to get re-assigned another (currently used) output
1285    *   stream.
1286    */
1287
1288   stream_id = gst_stream_get_stream_id (slot->active_stream);
1289   caps = gst_stream_get_caps (slot->active_stream);
1290   GST_DEBUG_OBJECT (dbin, "stream %s , %" GST_PTR_FORMAT, stream_id, caps);
1291   gst_caps_unref (caps);
1292
1293   /* 0. Emit autoplug-continue signal for pending caps ? */
1294   GST_FIXME_OBJECT (dbin, "emit autoplug-continue");
1295
1296   /* 1. if in EXPOSE_ALL_MODE, just accept */
1297   GST_FIXME_OBJECT (dbin, "Handle EXPOSE_ALL_MODE");
1298
1299 #if 0
1300   /* FIXME : The idea around this was to avoid activating a stream for
1301    *     which we have no decoder. Unfortunately it is way too
1302    *     expensive. Need to figure out a better solution */
1303   /* 2. Is there a potential decoder (if one is required) */
1304   if (!gst_caps_can_intersect (caps, dbin->caps)
1305       && !have_factory (dbin, (GstCaps *) caps,
1306           GST_ELEMENT_FACTORY_TYPE_DECODER)) {
1307     GST_WARNING_OBJECT (dbin, "Don't have a decoder for %" GST_PTR_FORMAT,
1308         caps);
1309     SELECTION_UNLOCK (dbin);
1310     gst_element_post_message (GST_ELEMENT_CAST (dbin),
1311         gst_missing_decoder_message_new (GST_ELEMENT_CAST (dbin), caps));
1312     SELECTION_LOCK (dbin);
1313     return NULL;
1314   }
1315 #endif
1316
1317   /* 3. In default mode check if we should expose */
1318   if (stream_in_list (dbin->requested_selection, stream_id)) {
1319     /* Check if we can steal an existing output stream we could re-use.
1320      * that is:
1321      * * an output stream whose slot->stream is not in requested
1322      * * and is of the same type as this stream
1323      */
1324     output = find_free_compatible_output (dbin, slot->active_stream);
1325     if (output) {
1326       /* Move this output from its current slot to this slot */
1327       dbin->to_activate =
1328           g_list_append (dbin->to_activate, (gchar *) stream_id);
1329       dbin->requested_selection =
1330           g_list_remove (dbin->requested_selection, stream_id);
1331       SELECTION_UNLOCK (dbin);
1332       gst_pad_add_probe (output->slot->src_pad, GST_PAD_PROBE_TYPE_IDLE,
1333           (GstPadProbeCallback) slot_unassign_probe, output->slot, NULL);
1334       SELECTION_LOCK (dbin);
1335       return NULL;
1336     }
1337
1338     output = create_output_stream (dbin, slot->type);
1339     output->slot = slot;
1340     GST_DEBUG ("Linking slot %p to new output %p", slot, output);
1341     slot->output = output;
1342     dbin->active_selection =
1343         g_list_append (dbin->active_selection, (gchar *) stream_id);
1344   } else
1345     GST_DEBUG ("Not creating any output for slot %p", slot);
1346
1347   return output;
1348 }
1349
1350 /* Returns SELECTED_STREAMS message if active_selection is equal to
1351  * requested_selection, else NULL.
1352  * Must be called with LOCK taken */
1353 static GstMessage *
1354 is_selection_done (GstDecodebin3 * dbin)
1355 {
1356   GList *tmp;
1357   GstMessage *msg;
1358
1359   if (!dbin->selection_updated)
1360     return NULL;
1361
1362   GST_LOG_OBJECT (dbin, "Checking");
1363
1364   if (dbin->to_activate != NULL) {
1365     GST_DEBUG ("Still have streams to activate");
1366     return NULL;
1367   }
1368   for (tmp = dbin->requested_selection; tmp; tmp = tmp->next) {
1369     GST_DEBUG ("Checking requested stream %s", (gchar *) tmp->data);
1370     if (!stream_in_list (dbin->active_selection, (gchar *) tmp->data)) {
1371       GST_DEBUG ("Not in active selection, returning");
1372       return NULL;
1373     }
1374   }
1375
1376   GST_DEBUG_OBJECT (dbin, "Selection active, creating message");
1377
1378   /* We are completely active */
1379   msg = gst_message_new_streams_selected ((GstObject *) dbin, dbin->collection);
1380   GST_MESSAGE_SEQNUM (msg) = dbin->select_streams_seqnum;
1381   for (tmp = dbin->output_streams; tmp; tmp = tmp->next) {
1382     DecodebinOutputStream *output = (DecodebinOutputStream *) tmp->data;
1383     if (output->slot) {
1384       GST_DEBUG_OBJECT (dbin, "Adding stream %s",
1385           gst_stream_get_stream_id (output->slot->active_stream));
1386
1387       gst_message_streams_selected_add (msg, output->slot->active_stream);
1388     } else
1389       GST_WARNING_OBJECT (dbin, "No valid slot for output %p", output);
1390   }
1391   dbin->selection_updated = FALSE;
1392   return msg;
1393 }
1394
1395 static GstPadProbeReturn
1396 multiqueue_src_probe (GstPad * pad, GstPadProbeInfo * info,
1397     MultiQueueSlot * slot)
1398 {
1399   GstPadProbeReturn ret = GST_PAD_PROBE_OK;
1400   GstDecodebin3 *dbin = slot->dbin;
1401
1402   if (GST_IS_EVENT (GST_PAD_PROBE_INFO_DATA (info))) {
1403     GstEvent *ev = GST_PAD_PROBE_INFO_EVENT (info);
1404
1405     GST_DEBUG_OBJECT (pad, "Got event %p %s", ev, GST_EVENT_TYPE_NAME (ev));
1406     switch (GST_EVENT_TYPE (ev)) {
1407       case GST_EVENT_STREAM_START:
1408       {
1409         GstStream *stream = NULL;
1410         const gchar *stream_id;
1411
1412         gst_event_parse_stream (ev, &stream);
1413         if (stream == NULL) {
1414           GST_ERROR_OBJECT (pad,
1415               "Got a STREAM_START event without a GstStream");
1416           break;
1417         }
1418         stream_id = gst_stream_get_stream_id (stream);
1419         GST_DEBUG_OBJECT (pad, "Stream Start '%s'", stream_id);
1420         if (slot->active_stream == NULL) {
1421           slot->active_stream = stream;
1422         } else if (slot->active_stream != stream) {
1423           GST_FIXME_OBJECT (pad, "Handle stream changes (%s => %s) !",
1424               gst_stream_get_stream_id (slot->active_stream),
1425               gst_stream_get_stream_id (stream));
1426           gst_object_unref (slot->active_stream);
1427           slot->active_stream = stream;
1428         } else
1429           gst_object_unref (stream);
1430 #if 0                           /* Disabled because stream-start is pushed for every buffer on every unlinked pad */
1431         {
1432           gboolean is_active, is_requested;
1433           /* Quick check to see if we're in the current selection */
1434           /* FIXME : Re-check all slot<=>output mappings based on requested_selection */
1435           SELECTION_LOCK (dbin);
1436           GST_DEBUG_OBJECT (dbin, "Checking active selection");
1437           is_active = stream_in_list (dbin->active_selection, stream_id);
1438           GST_DEBUG_OBJECT (dbin, "Checking requested selection");
1439           is_requested = stream_in_list (dbin->requested_selection, stream_id);
1440           SELECTION_UNLOCK (dbin);
1441           if (is_active)
1442             GST_DEBUG_OBJECT (pad, "Slot in ACTIVE selection (output:%p)",
1443                 slot->output);
1444           if (is_requested)
1445             GST_DEBUG_OBJECT (pad, "Slot in REQUESTED selection (output:%p)",
1446                 slot->output);
1447           else if (slot->output) {
1448             GST_DEBUG_OBJECT (pad,
1449                 "Slot needs to be deactivated ? It's no longer in requested selection");
1450           } else if (!is_active)
1451             GST_DEBUG_OBJECT (pad,
1452                 "Slot in neither active nor requested selection");
1453         }
1454 #endif
1455       }
1456         break;
1457       case GST_EVENT_CAPS:
1458       {
1459         /* Configure the output slot if needed */
1460         DecodebinOutputStream *output;
1461         GstMessage *msg = NULL;
1462         SELECTION_LOCK (dbin);
1463         output = get_output_for_slot (slot);
1464         if (output) {
1465           reconfigure_output_stream (output, slot);
1466           msg = is_selection_done (dbin);
1467         }
1468         SELECTION_UNLOCK (dbin);
1469         if (msg)
1470           gst_element_post_message ((GstElement *) slot->dbin, msg);
1471       }
1472         break;
1473       case GST_EVENT_EOS:
1474         /* FIXME : Figure out */
1475         GST_FIXME_OBJECT (pad, "EOS on multiqueue source pad. input:%p",
1476             slot->input);
1477         if (slot->input == NULL) {
1478           GstPad *peer;
1479           GST_DEBUG_OBJECT (pad,
1480               "last EOS for input, forwarding and removing slot");
1481           peer = gst_pad_get_peer (pad);
1482           if (peer) {
1483             gst_pad_send_event (peer, ev);
1484             gst_object_unref (peer);
1485           } else {
1486             gst_event_unref (ev);
1487           }
1488           SELECTION_LOCK (dbin);
1489           /* FIXME : Shouldn't we try to re-assign the output instead of just
1490            * removing it ? */
1491           /* Remove the output */
1492           if (slot->output) {
1493             DecodebinOutputStream *output = slot->output;
1494             dbin->output_streams = g_list_remove (dbin->output_streams, output);
1495             free_output_stream (dbin, output);
1496           }
1497           SELECTION_UNLOCK (dbin);
1498           ret = GST_PAD_PROBE_HANDLED;
1499         }
1500         break;
1501       default:
1502         break;
1503     }
1504   } else if (GST_IS_QUERY (GST_PAD_PROBE_INFO_DATA (info))) {
1505     GstQuery *query = GST_PAD_PROBE_INFO_QUERY (info);
1506     switch (GST_QUERY_TYPE (query)) {
1507       case GST_QUERY_CAPS:
1508       {
1509         GST_DEBUG_OBJECT (pad, "Intercepting CAPS query");
1510         gst_query_set_caps_result (query, GST_CAPS_ANY);
1511         ret = GST_PAD_PROBE_HANDLED;
1512       }
1513         break;
1514
1515       case GST_QUERY_ACCEPT_CAPS:
1516       {
1517         GST_DEBUG_OBJECT (pad, "Intercepting Accept Caps query");
1518         /* If the current decoder doesn't accept caps, we'll reconfigure
1519          * on the actual caps event. So accept any caps. */
1520         gst_query_set_accept_caps_result (query, TRUE);
1521         ret = GST_PAD_PROBE_HANDLED;
1522       }
1523       default:
1524         break;
1525     }
1526   }
1527
1528   return ret;
1529 }
1530
1531 /* Create a new multiqueue slot for the given type
1532  *
1533  * It is up to the caller to know whether that slot is needed or not
1534  * (and release it when no longer needed) */
1535 static MultiQueueSlot *
1536 create_new_slot (GstDecodebin3 * dbin, GstStreamType type)
1537 {
1538   MultiQueueSlot *slot;
1539   GstIterator *it = NULL;
1540   GValue item = { 0, };
1541
1542   GST_DEBUG_OBJECT (dbin, "Creating new slot for type %s",
1543       gst_stream_type_get_name (type));
1544   slot = g_new0 (MultiQueueSlot, 1);
1545   slot->dbin = dbin;
1546   slot->id = dbin->slot_id++;
1547   slot->type = type;
1548   slot->sink_pad = gst_element_get_request_pad (dbin->multiqueue, "sink_%u");
1549   if (slot->sink_pad == NULL)
1550     goto fail;
1551   it = gst_pad_iterate_internal_links (slot->sink_pad);
1552   if (!it || (gst_iterator_next (it, &item)) != GST_ITERATOR_OK
1553       || ((slot->src_pad = g_value_dup_object (&item)) == NULL)) {
1554     GST_ERROR ("Couldn't get srcpad from multiqueue for sink pad %s:%s",
1555         GST_DEBUG_PAD_NAME (slot->src_pad));
1556     goto fail;
1557   }
1558   gst_iterator_free (it);
1559   g_value_reset (&item);
1560
1561   g_object_set (slot->sink_pad, "group-id", (guint) type, NULL);
1562
1563   /* Add event probe */
1564   slot->probe_id =
1565       gst_pad_add_probe (slot->src_pad,
1566       GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM | GST_PAD_PROBE_TYPE_QUERY_DOWNSTREAM,
1567       (GstPadProbeCallback) multiqueue_src_probe, slot, NULL);
1568
1569   GST_DEBUG ("Created new slot %u (%p) (%s:%s)", slot->id, slot,
1570       GST_DEBUG_PAD_NAME (slot->src_pad));
1571   dbin->slots = g_list_append (dbin->slots, slot);
1572   return slot;
1573
1574   /* ERRORS */
1575 fail:
1576   {
1577     if (slot->sink_pad)
1578       gst_element_release_request_pad (dbin->multiqueue, slot->sink_pad);
1579     g_free (slot);
1580     return NULL;
1581   }
1582 }
1583
1584 static MultiQueueSlot *
1585 get_slot_for_input (GstDecodebin3 * dbin, DecodebinInputStream * input)
1586 {
1587   GList *tmp;
1588   MultiQueueSlot *empty_slot = NULL;
1589   GstStreamType input_type = 0;
1590   gchar *stream_id = NULL;
1591
1592   GST_DEBUG_OBJECT (dbin, "input %p (stream %p %s)",
1593       input, input->active_stream,
1594       input->
1595       active_stream ? gst_stream_get_stream_id (input->active_stream) : "");
1596
1597   if (input->active_stream) {
1598     input_type = gst_stream_get_stream_type (input->active_stream);
1599     stream_id = (gchar *) gst_stream_get_stream_id (input->active_stream);
1600   }
1601
1602   /* Go over existing slots and check if there is already one for it */
1603   for (tmp = dbin->slots; tmp; tmp = tmp->next) {
1604     MultiQueueSlot *slot = (MultiQueueSlot *) tmp->data;
1605     /* Already used input, return that one */
1606     if (slot->input == input) {
1607       GST_DEBUG_OBJECT (dbin, "Returning already specified slot %d", slot->id);
1608       return slot;
1609     }
1610   }
1611
1612   /* Go amongst all unused slots of the right type and try to find a candidate */
1613   for (tmp = dbin->slots; tmp; tmp = tmp->next) {
1614     MultiQueueSlot *slot = (MultiQueueSlot *) tmp->data;
1615     if (slot->input == NULL && input_type == slot->type) {
1616       /* Remember this empty slot for later */
1617       empty_slot = slot;
1618       /* Check if available slot is of the same stream_id */
1619       GST_LOG_OBJECT (dbin, "Checking candidate slot %d (active_stream:%p)",
1620           slot->id, slot->active_stream);
1621       if (stream_id && slot->active_stream) {
1622         gchar *ostream_id =
1623             (gchar *) gst_stream_get_stream_id (slot->active_stream);
1624         GST_DEBUG_OBJECT (dbin, "Checking slot %d %s against %s", slot->id,
1625             ostream_id, stream_id);
1626         if (!g_strcmp0 (stream_id, ostream_id))
1627           break;
1628       }
1629     }
1630   }
1631
1632   if (empty_slot) {
1633     GST_DEBUG_OBJECT (dbin, "Re-using existing unused slot %d", empty_slot->id);
1634     empty_slot->input = input;
1635     return empty_slot;
1636   }
1637
1638   if (input_type)
1639     return create_new_slot (dbin, input_type);
1640
1641   return NULL;
1642 }
1643
1644 static void
1645 link_input_to_slot (DecodebinInputStream * input, MultiQueueSlot * slot)
1646 {
1647   GstEvent *event;
1648   if (slot->input != NULL && slot->input != input) {
1649     GST_ERROR_OBJECT (slot->dbin,
1650         "Trying to link input to an already used slot");
1651     return;
1652   }
1653   gst_pad_link_full (input->srcpad, slot->sink_pad, GST_PAD_LINK_CHECK_NOTHING);
1654   slot->pending_stream = input->active_stream;
1655   slot->input = input;
1656   event = gst_pad_get_sticky_event (input->srcpad, GST_EVENT_STREAM_START, 0);
1657   if (event)
1658     gst_pad_send_event (slot->sink_pad, event);
1659 }
1660
1661 #if 0
1662 static gboolean
1663 have_factory (GstDecodebin3 * dbin, GstCaps * caps,
1664     GstElementFactoryListType ftype)
1665 {
1666   gboolean ret = FALSE;
1667   GList *res;
1668
1669   g_mutex_lock (&dbin->factories_lock);
1670   gst_decode_bin_update_factories_list (dbin);
1671   if (ftype == GST_ELEMENT_FACTORY_TYPE_DECODER)
1672     res =
1673         gst_element_factory_list_filter (dbin->decoder_factories,
1674         caps, GST_PAD_SINK, TRUE);
1675   else
1676     res =
1677         gst_element_factory_list_filter (dbin->decodable_factories,
1678         caps, GST_PAD_SINK, TRUE);
1679   g_mutex_unlock (&dbin->factories_lock);
1680
1681   if (res) {
1682     ret = TRUE;
1683     gst_plugin_feature_list_free (res);
1684   }
1685
1686   return ret;
1687 }
1688 #endif
1689
1690 static GstElement *
1691 create_element (GstDecodebin3 * dbin, GstStream * stream,
1692     GstElementFactoryListType ftype)
1693 {
1694   GList *res;
1695   GstElement *element = NULL;
1696   GstCaps *caps;
1697
1698   g_mutex_lock (&dbin->factories_lock);
1699   gst_decode_bin_update_factories_list (dbin);
1700   caps = gst_stream_get_caps (stream);
1701   if (ftype == GST_ELEMENT_FACTORY_TYPE_DECODER)
1702     res =
1703         gst_element_factory_list_filter (dbin->decoder_factories,
1704         caps, GST_PAD_SINK, TRUE);
1705   else
1706     res =
1707         gst_element_factory_list_filter (dbin->decodable_factories,
1708         caps, GST_PAD_SINK, TRUE);
1709   g_mutex_unlock (&dbin->factories_lock);
1710
1711   if (res) {
1712     element =
1713         gst_element_factory_create ((GstElementFactory *) res->data, NULL);
1714     GST_DEBUG ("Created element '%s'", GST_ELEMENT_NAME (element));
1715     gst_plugin_feature_list_free (res);
1716   } else {
1717     GST_DEBUG ("Could not find an element for caps %" GST_PTR_FORMAT, caps);
1718   }
1719
1720   gst_caps_unref (caps);
1721   return element;
1722 }
1723
1724 /* FIXME : VERY NAIVE. ASSUMING FIRST ONE WILL WORK */
1725 static GstElement *
1726 create_decoder (GstDecodebin3 * dbin, GstStream * stream)
1727 {
1728   return create_element (dbin, stream, GST_ELEMENT_FACTORY_TYPE_DECODER);
1729 }
1730
1731 static GstPadProbeReturn
1732 keyframe_waiter_probe (GstPad * pad, GstPadProbeInfo * info,
1733     DecodebinOutputStream * output)
1734 {
1735   GstBuffer *buf = GST_PAD_PROBE_INFO_BUFFER (info);
1736   /* If we have a keyframe, remove the probe and let all data through */
1737   /* FIXME : HANDLE HEADER BUFFER ?? */
1738   if (!GST_BUFFER_FLAG_IS_SET (buf, GST_BUFFER_FLAG_DELTA_UNIT) ||
1739       GST_BUFFER_FLAG_IS_SET (buf, GST_BUFFER_FLAG_HEADER)) {
1740     GST_DEBUG_OBJECT (pad,
1741         "Buffer is keyframe or header, letting through and removing probe");
1742     output->drop_probe_id = 0;
1743     return GST_PAD_PROBE_REMOVE;
1744   }
1745   GST_DEBUG_OBJECT (pad, "Buffer is not a keyframe, dropping");
1746   return GST_PAD_PROBE_DROP;
1747 }
1748
1749 static void
1750 reconfigure_output_stream (DecodebinOutputStream * output,
1751     MultiQueueSlot * slot)
1752 {
1753   GstDecodebin3 *dbin = output->dbin;
1754   GstCaps *new_caps = (GstCaps *) gst_stream_get_caps (slot->active_stream);
1755   gboolean needs_decoder;
1756
1757   needs_decoder = gst_caps_can_intersect (new_caps, dbin->caps) != TRUE;
1758
1759   GST_DEBUG_OBJECT (dbin,
1760       "Reconfiguring output %p to slot %p, needs_decoder:%d", output, slot,
1761       needs_decoder);
1762
1763   /* FIXME : Maybe make the output un-hook itself automatically ? */
1764   if (output->slot != NULL && output->slot != slot) {
1765     GST_WARNING_OBJECT (dbin,
1766         "Output still linked to another slot (%p)", output->slot);
1767     gst_caps_unref (new_caps);
1768     return;
1769   }
1770
1771   /* Check if existing config is reusable as-is by checking if
1772    * the existing decoder accepts the new caps, if not delete
1773    * it and create a new one */
1774   if (output->decoder) {
1775     gboolean can_reuse_decoder;
1776
1777     if (needs_decoder) {
1778       can_reuse_decoder =
1779           gst_pad_query_accept_caps (output->decoder_sink, new_caps);
1780     } else
1781       can_reuse_decoder = FALSE;
1782
1783     if (can_reuse_decoder) {
1784       if (output->type == GST_STREAM_TYPE_VIDEO && output->drop_probe_id == 0) {
1785         GST_DEBUG_OBJECT (dbin, "Adding keyframe-waiter probe");
1786         output->drop_probe_id =
1787             gst_pad_add_probe (slot->src_pad, GST_PAD_PROBE_TYPE_BUFFER,
1788             (GstPadProbeCallback) keyframe_waiter_probe, output, NULL);
1789       }
1790       GST_DEBUG_OBJECT (dbin, "Reusing existing decoder for slot %p", slot);
1791       if (output->linked == FALSE) {
1792         gst_pad_link_full (slot->src_pad, output->decoder_sink,
1793             GST_PAD_LINK_CHECK_NOTHING);
1794         output->linked = TRUE;
1795       }
1796       gst_caps_unref (new_caps);
1797       return;
1798     }
1799
1800     GST_DEBUG_OBJECT (dbin, "Removing old decoder for slot %p", slot);
1801
1802     if (output->linked)
1803       gst_pad_unlink (slot->src_pad, output->decoder_sink);
1804     output->linked = FALSE;
1805     if (output->drop_probe_id) {
1806       gst_pad_remove_probe (slot->src_pad, output->drop_probe_id);
1807       output->drop_probe_id = 0;
1808     }
1809
1810     if (!gst_ghost_pad_set_target ((GstGhostPad *) output->src_pad, NULL)) {
1811       GST_ERROR_OBJECT (dbin, "Could not release decoder pad");
1812       gst_caps_unref (new_caps);
1813       goto cleanup;
1814     }
1815
1816     gst_object_replace ((GstObject **) & output->decoder_sink, NULL);
1817     gst_object_replace ((GstObject **) & output->decoder_src, NULL);
1818
1819     gst_element_set_locked_state (output->decoder, TRUE);
1820     gst_element_set_state (output->decoder, GST_STATE_NULL);
1821
1822     gst_bin_remove ((GstBin *) dbin, output->decoder);
1823     output->decoder = NULL;
1824   }
1825
1826   gst_caps_unref (new_caps);
1827
1828   /* If a decoder is required, create one */
1829   if (needs_decoder) {
1830     /* If we don't have a decoder yet, instantiate one */
1831     output->decoder = create_decoder (dbin, slot->active_stream);
1832     if (output->decoder == NULL) {
1833       GstCaps *caps;
1834
1835       SELECTION_UNLOCK (dbin);
1836       /* FIXME : Should we be smarter if there's a missing decoder ?
1837        * Should we deactivate that stream ? */
1838       caps = gst_stream_get_caps (slot->active_stream);
1839       gst_element_post_message (GST_ELEMENT_CAST (dbin),
1840           gst_missing_decoder_message_new (GST_ELEMENT_CAST (dbin), caps));
1841       gst_caps_unref (caps);
1842       SELECTION_LOCK (dbin);
1843       goto cleanup;
1844     }
1845     if (!gst_bin_add ((GstBin *) dbin, output->decoder)) {
1846       GST_ERROR_OBJECT (dbin, "could not add decoder to pipeline");
1847       goto cleanup;
1848     }
1849     output->decoder_sink = gst_element_get_static_pad (output->decoder, "sink");
1850     output->decoder_src = gst_element_get_static_pad (output->decoder, "src");
1851     if (output->type == GST_STREAM_TYPE_VIDEO) {
1852       GST_DEBUG_OBJECT (dbin, "Adding keyframe-waiter probe");
1853       output->drop_probe_id =
1854           gst_pad_add_probe (slot->src_pad, GST_PAD_PROBE_TYPE_BUFFER,
1855           (GstPadProbeCallback) keyframe_waiter_probe, output, NULL);
1856     }
1857     if (gst_pad_link_full (slot->src_pad, output->decoder_sink,
1858             GST_PAD_LINK_CHECK_NOTHING) != GST_PAD_LINK_OK) {
1859       GST_ERROR_OBJECT (dbin, "could not link to %s:%s",
1860           GST_DEBUG_PAD_NAME (output->decoder_sink));
1861       goto cleanup;
1862     }
1863   } else {
1864     output->decoder_src = gst_object_ref (slot->src_pad);
1865     output->decoder_sink = NULL;
1866   }
1867   output->linked = TRUE;
1868   if (!gst_ghost_pad_set_target ((GstGhostPad *) output->src_pad,
1869           output->decoder_src)) {
1870     GST_ERROR_OBJECT (dbin, "Could not expose decoder pad");
1871     goto cleanup;
1872   }
1873   if (output->src_exposed == FALSE) {
1874     output->src_exposed = TRUE;
1875     gst_element_add_pad (GST_ELEMENT_CAST (dbin), output->src_pad);
1876   }
1877
1878   if (output->decoder)
1879     gst_element_sync_state_with_parent (output->decoder);
1880
1881   output->slot = slot;
1882   return;
1883
1884 cleanup:
1885   {
1886     GST_DEBUG_OBJECT (dbin, "Cleanup");
1887     if (output->decoder_sink) {
1888       gst_object_unref (output->decoder_sink);
1889       output->decoder_sink = NULL;
1890     }
1891     if (output->decoder_src) {
1892       gst_object_unref (output->decoder_src);
1893       output->decoder_src = NULL;
1894     }
1895     if (output->decoder) {
1896       gst_element_set_state (output->decoder, GST_STATE_NULL);
1897       gst_bin_remove ((GstBin *) dbin, output->decoder);
1898       output->decoder = NULL;
1899     }
1900   }
1901 }
1902
1903 static GstPadProbeReturn
1904 idle_reconfigure (GstPad * pad, GstPadProbeInfo * info, MultiQueueSlot * slot)
1905 {
1906   GstMessage *msg = NULL;
1907   DecodebinOutputStream *output;
1908
1909   SELECTION_LOCK (slot->dbin);
1910   output = get_output_for_slot (slot);
1911
1912   GST_DEBUG_OBJECT (pad, "output : %p", output);
1913
1914   if (output) {
1915     reconfigure_output_stream (output, slot);
1916     msg = is_selection_done (slot->dbin);
1917   }
1918   SELECTION_UNLOCK (slot->dbin);
1919   if (msg)
1920     gst_element_post_message ((GstElement *) slot->dbin, msg);
1921
1922   return GST_PAD_PROBE_REMOVE;
1923 }
1924
1925 static MultiQueueSlot *
1926 find_slot_for_stream_id (GstDecodebin3 * dbin, const gchar * sid)
1927 {
1928   GList *tmp;
1929
1930   for (tmp = dbin->slots; tmp; tmp = tmp->next) {
1931     MultiQueueSlot *slot = (MultiQueueSlot *) tmp->data;
1932     const gchar *stream_id;
1933     if (slot->active_stream) {
1934       stream_id = gst_stream_get_stream_id (slot->active_stream);
1935       if (!g_strcmp0 (sid, stream_id))
1936         return slot;
1937     }
1938     if (slot->pending_stream && slot->pending_stream != slot->active_stream) {
1939       stream_id = gst_stream_get_stream_id (slot->pending_stream);
1940       if (!g_strcmp0 (sid, stream_id))
1941         return slot;
1942     }
1943   }
1944
1945   return NULL;
1946 }
1947
1948 /* This function handles the reassignment of a slot. Call this from
1949  * the streaming thread of a slot. */
1950 static gboolean
1951 reassign_slot (GstDecodebin3 * dbin, MultiQueueSlot * slot)
1952 {
1953   DecodebinOutputStream *output;
1954   MultiQueueSlot *target_slot = NULL;
1955   GList *tmp;
1956   const gchar *sid, *tsid;
1957
1958   SELECTION_LOCK (dbin);
1959   output = slot->output;
1960
1961   if (G_UNLIKELY (slot->active_stream == NULL)) {
1962     GST_DEBUG_OBJECT (slot->src_pad,
1963         "Called on inactive slot (active_stream == NULL)");
1964     SELECTION_UNLOCK (dbin);
1965     return FALSE;
1966   }
1967
1968   if (G_UNLIKELY (output == NULL)) {
1969     GST_DEBUG_OBJECT (slot->src_pad,
1970         "Slot doesn't have any output to be removed");
1971     SELECTION_UNLOCK (dbin);
1972     return FALSE;
1973   }
1974
1975   sid = gst_stream_get_stream_id (slot->active_stream);
1976   GST_DEBUG_OBJECT (slot->src_pad, "slot %s %p", sid, slot);
1977
1978   /* Recheck whether this stream is still in the list of streams to deactivate */
1979   if (stream_in_list (dbin->requested_selection, sid)) {
1980     /* Stream is in the list of requested streams, don't remove */
1981     SELECTION_UNLOCK (dbin);
1982     GST_DEBUG_OBJECT (slot->src_pad,
1983         "Stream '%s' doesn't need to be deactivated", sid);
1984     return FALSE;
1985   }
1986
1987   /* Unlink slot from output */
1988   /* FIXME : Handle flushing ? */
1989   /* FIXME : Handle outputs without decoders */
1990   GST_DEBUG_OBJECT (slot->src_pad, "Unlinking from decoder %p",
1991       output->decoder_sink);
1992   if (output->decoder_sink)
1993     gst_pad_unlink (slot->src_pad, output->decoder_sink);
1994   output->linked = FALSE;
1995   slot->output = NULL;
1996   output->slot = NULL;
1997   /* Remove sid from active selection */
1998   for (tmp = dbin->active_selection; tmp; tmp = tmp->next)
1999     if (!g_strcmp0 (sid, tmp->data)) {
2000       dbin->active_selection = g_list_delete_link (dbin->active_selection, tmp);
2001       break;
2002     }
2003
2004   /* Can we re-assign this output to a requested stream ? */
2005   GST_DEBUG_OBJECT (slot->src_pad, "Attempting to re-assing output stream");
2006   for (tmp = dbin->to_activate; tmp; tmp = tmp->next) {
2007     MultiQueueSlot *tslot = find_slot_for_stream_id (dbin, tmp->data);
2008     GST_LOG_OBJECT (tslot->src_pad, "Checking slot %p (output:%p , stream:%s)",
2009         tslot, tslot->output, gst_stream_get_stream_id (tslot->active_stream));
2010     if (tslot && tslot->type == output->type && tslot->output == NULL) {
2011       GST_DEBUG_OBJECT (tslot->src_pad, "Using as reassigned slot");
2012       target_slot = tslot;
2013       tsid = tmp->data;
2014       /* Pass target stream id to requested selection */
2015       dbin->requested_selection =
2016           g_list_append (dbin->requested_selection, tmp->data);
2017       dbin->to_activate = g_list_remove (dbin->to_activate, tmp->data);
2018       break;
2019     }
2020   }
2021
2022   if (target_slot) {
2023     GST_DEBUG_OBJECT (slot->src_pad, "Assigning output to slot %p '%s'",
2024         target_slot, tsid);
2025     target_slot->output = output;
2026     output->slot = target_slot;
2027     dbin->active_selection =
2028         g_list_append (dbin->active_selection, (gchar *) tsid);
2029     SELECTION_UNLOCK (dbin);
2030
2031     /* Wakeup the target slot so that it retries to send events/buffers
2032      * thereby triggering the output reconfiguration codepath */
2033     gst_pad_add_probe (target_slot->src_pad, GST_PAD_PROBE_TYPE_IDLE,
2034         (GstPadProbeCallback) idle_reconfigure, target_slot, NULL);
2035     /* gst_pad_send_event (target_slot->src_pad, gst_event_new_reconfigure ()); */
2036   } else {
2037     SELECTION_UNLOCK (dbin);
2038     /* FIXME : Remove output if no longer needed ? The tricky part is knowing
2039      * if it's really no longer needed or not */
2040     GST_FIXME_OBJECT (slot->src_pad, "Remove unused output stream ?");
2041   }
2042
2043   return TRUE;
2044 }
2045
2046 /* Idle probe called when a slot should be unassigned from its output stream.
2047  * This is needed to ensure nothing is flowing when unlinking the slot.
2048  *
2049  * Also, this method will search for a pending stream which could re-use
2050  * the output stream. */
2051 static GstPadProbeReturn
2052 slot_unassign_probe (GstPad * pad, GstPadProbeInfo * info,
2053     MultiQueueSlot * slot)
2054 {
2055   GstDecodebin3 *dbin = slot->dbin;
2056
2057   reassign_slot (dbin, slot);
2058
2059   return GST_PAD_PROBE_REMOVE;
2060 }
2061
2062 static gboolean
2063 handle_stream_switch (GstDecodebin3 * dbin, GList * select_streams,
2064     guint32 seqnum)
2065 {
2066   gboolean ret = TRUE;
2067   GList *tmp;
2068   /* List of slots to (de)activate. */
2069   GList *to_deactivate = NULL;
2070   GList *to_activate = NULL;
2071   /* List of unknown stream id, most likely means the event
2072    * should be sent upstream so that elements can expose the requested stream */
2073   GList *unknown = NULL;
2074   GList *to_reassign = NULL;
2075   GList *future_request_streams = NULL;
2076   GList *pending_streams = NULL;
2077   GList *slots_to_reassign = NULL;
2078
2079   SELECTION_LOCK (dbin);
2080   if (G_UNLIKELY (seqnum != dbin->select_streams_seqnum)) {
2081     GST_DEBUG_OBJECT (dbin, "New SELECT_STREAMS has arrived in the meantime");
2082     SELECTION_UNLOCK (dbin);
2083     return TRUE;
2084   }
2085   /* Remove pending select_streams */
2086   g_list_free (dbin->pending_select_streams);
2087   dbin->pending_select_streams = NULL;
2088
2089   /* COMPARE the requested streams to the active and requested streams
2090    * on multiqueue. */
2091
2092   /* First check the slots to activate and which ones are unknown */
2093   for (tmp = select_streams; tmp; tmp = tmp->next) {
2094     const gchar *sid = (const gchar *) tmp->data;
2095     MultiQueueSlot *slot;
2096     GST_DEBUG_OBJECT (dbin, "Checking stream '%s'", sid);
2097     slot = find_slot_for_stream_id (dbin, sid);
2098     /* Find the corresponding slot */
2099     if (slot == NULL) {
2100       if (stream_in_collection (dbin, (gchar *) sid)) {
2101         pending_streams = g_list_append (pending_streams, (gchar *) sid);
2102       } else {
2103         GST_DEBUG_OBJECT (dbin, "We don't have a slot for stream '%s'", sid);
2104         unknown = g_list_append (unknown, (gchar *) sid);
2105       }
2106     } else if (slot->output == NULL) {
2107       GST_DEBUG_OBJECT (dbin, "We need to activate slot %p for stream '%s')",
2108           slot, sid);
2109       to_activate = g_list_append (to_activate, slot);
2110     } else {
2111       GST_DEBUG_OBJECT (dbin,
2112           "Stream '%s' from slot %p is already active on output %p", sid, slot,
2113           slot->output);
2114       future_request_streams =
2115           g_list_append (future_request_streams, (gchar *) sid);
2116     }
2117   }
2118
2119   for (tmp = dbin->slots; tmp; tmp = tmp->next) {
2120     MultiQueueSlot *slot = (MultiQueueSlot *) tmp->data;
2121     /* For slots that have an output, check if it's part of the streams to
2122      * be active */
2123     if (slot->output) {
2124       gboolean slot_to_deactivate = TRUE;
2125
2126       if (slot->active_stream) {
2127         if (stream_in_list (select_streams,
2128                 gst_stream_get_stream_id (slot->active_stream)))
2129           slot_to_deactivate = FALSE;
2130       }
2131       if (slot_to_deactivate && slot->pending_stream
2132           && slot->pending_stream != slot->active_stream) {
2133         if (stream_in_list (select_streams,
2134                 gst_stream_get_stream_id (slot->pending_stream)))
2135           slot_to_deactivate = FALSE;
2136       }
2137       if (slot_to_deactivate) {
2138         GST_DEBUG_OBJECT (dbin,
2139             "Slot %p (%s) should be deactivated, no longer used", slot,
2140             gst_stream_get_stream_id (slot->active_stream));
2141         to_deactivate = g_list_append (to_deactivate, slot);
2142       }
2143     }
2144   }
2145
2146   if (to_deactivate != NULL) {
2147     GST_DEBUG_OBJECT (dbin, "Check if we can reassign slots");
2148     /* We need to compare what needs to be activated and deactivated in order
2149      * to determine whether there are outputs that can be transferred */
2150     /* Take the stream-id of the slots that are to be activated, for which there
2151      * is a slot of the same type that needs to be deactivated */
2152     tmp = to_deactivate;
2153     while (tmp) {
2154       MultiQueueSlot *slot_to_deactivate = (MultiQueueSlot *) tmp->data;
2155       gboolean removeit = FALSE;
2156       GList *tmp2, *next;
2157       GST_DEBUG_OBJECT (dbin,
2158           "Checking if slot to deactivate (%p) has a candidate slot to activate",
2159           slot_to_deactivate);
2160       for (tmp2 = to_activate; tmp2; tmp2 = tmp2->next) {
2161         MultiQueueSlot *slot_to_activate = (MultiQueueSlot *) tmp2->data;
2162         GST_DEBUG_OBJECT (dbin, "Comparing to slot %p", slot_to_activate);
2163         if (slot_to_activate->type == slot_to_deactivate->type) {
2164           GST_DEBUG_OBJECT (dbin, "Re-using");
2165           to_reassign = g_list_append (to_reassign, (gchar *)
2166               gst_stream_get_stream_id (slot_to_activate->active_stream));
2167           slots_to_reassign =
2168               g_list_append (slots_to_reassign, slot_to_deactivate);
2169           to_activate = g_list_remove (to_activate, slot_to_activate);
2170           removeit = TRUE;
2171           break;
2172         }
2173       }
2174       next = tmp->next;
2175       if (removeit)
2176         to_deactivate = g_list_delete_link (to_deactivate, tmp);
2177       tmp = next;
2178     }
2179   }
2180
2181   for (tmp = to_deactivate; tmp; tmp = tmp->next) {
2182     MultiQueueSlot *slot = (MultiQueueSlot *) tmp->data;
2183     GST_DEBUG_OBJECT (dbin,
2184         "Really need to deactivate slot %p, but no available alternative",
2185         slot);
2186   }
2187
2188   /* The only slots left to activate are the ones that won't be reassigned and
2189    * therefore really need to have a new output created */
2190   for (tmp = to_activate; tmp; tmp = tmp->next) {
2191     MultiQueueSlot *slot = (MultiQueueSlot *) tmp->data;
2192     if (slot->active_stream)
2193       future_request_streams =
2194           g_list_append (future_request_streams,
2195           (gchar *) gst_stream_get_stream_id (slot->active_stream));
2196     else if (slot->pending_stream)
2197       future_request_streams =
2198           g_list_append (future_request_streams,
2199           (gchar *) gst_stream_get_stream_id (slot->pending_stream));
2200     else
2201       GST_ERROR_OBJECT (dbin, "No stream for slot %p !!", slot);
2202   }
2203
2204   if (to_activate == NULL && pending_streams != NULL) {
2205     GST_DEBUG_OBJECT (dbin, "Stream switch requested for future collection");
2206     if (dbin->requested_selection)
2207       g_list_free (dbin->requested_selection);
2208     dbin->requested_selection = select_streams;
2209     g_list_free (to_deactivate);
2210     g_list_free (pending_streams);
2211     to_deactivate = NULL;
2212   } else {
2213     if (dbin->requested_selection)
2214       g_list_free (dbin->requested_selection);
2215     dbin->requested_selection = future_request_streams;
2216     dbin->requested_selection =
2217         g_list_concat (dbin->requested_selection, pending_streams);
2218     if (dbin->to_activate)
2219       g_list_free (dbin->to_activate);
2220     dbin->to_activate = to_reassign;
2221   }
2222
2223   dbin->selection_updated = TRUE;
2224   SELECTION_UNLOCK (dbin);
2225
2226   if (unknown)
2227     GST_FIXME_OBJECT (dbin, "Got request for an unknown stream");
2228
2229   /* For all streams to deactivate, add an idle probe where we will do
2230    * the unassignment and switch over */
2231   for (tmp = slots_to_reassign; tmp; tmp = tmp->next) {
2232     MultiQueueSlot *slot = (MultiQueueSlot *) tmp->data;
2233     gst_pad_add_probe (slot->src_pad, GST_PAD_PROBE_TYPE_IDLE,
2234         (GstPadProbeCallback) slot_unassign_probe, slot, NULL);
2235   }
2236
2237   return ret;
2238 }
2239
2240 static GstPadProbeReturn
2241 ghost_pad_event_probe (GstPad * pad, GstPadProbeInfo * info,
2242     DecodebinOutputStream * output)
2243 {
2244   GstPadProbeReturn ret = GST_PAD_PROBE_OK;
2245   GstDecodebin3 *dbin = output->dbin;
2246   GstEvent *event = GST_PAD_PROBE_INFO_EVENT (info);
2247
2248   GST_DEBUG_OBJECT (pad, "Got event %p %s", event, GST_EVENT_TYPE_NAME (event));
2249
2250   switch (GST_EVENT_TYPE (event)) {
2251     case GST_EVENT_SELECT_STREAMS:
2252     {
2253       GstPad *peer;
2254       GList *streams = NULL;
2255       guint32 seqnum = gst_event_get_seqnum (event);
2256
2257       SELECTION_LOCK (dbin);
2258       if (seqnum == dbin->select_streams_seqnum) {
2259         SELECTION_UNLOCK (dbin);
2260         GST_DEBUG_OBJECT (pad,
2261             "Already handled/handling that SELECT_STREAMS event");
2262         break;
2263       }
2264       dbin->select_streams_seqnum = seqnum;
2265       if (dbin->pending_select_streams != NULL) {
2266         GST_LOG_OBJECT (dbin, "Replacing pending select streams");
2267         g_list_free (dbin->pending_select_streams);
2268         dbin->pending_select_streams = NULL;
2269       }
2270       gst_event_parse_select_streams (event, &streams);
2271       dbin->pending_select_streams = g_list_copy (streams);
2272       SELECTION_UNLOCK (dbin);
2273
2274       /* Send event upstream */
2275       if ((peer = gst_pad_get_peer (pad))) {
2276         gst_pad_send_event (peer, event);
2277         gst_object_unref (peer);
2278       } else {
2279         gst_event_unref (event);
2280       }
2281       /* Finally handle the switch */
2282       if (streams)
2283         handle_stream_switch (dbin, streams, seqnum);
2284       ret = GST_PAD_PROBE_HANDLED;
2285     }
2286       break;
2287     default:
2288       break;
2289   }
2290
2291   return ret;
2292 }
2293
2294 static gboolean
2295 gst_decodebin3_send_event (GstElement * element, GstEvent * event)
2296 {
2297   GST_DEBUG_OBJECT (element, "event %s", GST_EVENT_TYPE_NAME (event));
2298   if (GST_EVENT_TYPE (event) == GST_EVENT_SELECT_STREAMS) {
2299     GstDecodebin3 *dbin = (GstDecodebin3 *) element;
2300     GList *streams = NULL;
2301     guint32 seqnum = gst_event_get_seqnum (event);
2302
2303     SELECTION_LOCK (dbin);
2304     if (seqnum == dbin->select_streams_seqnum) {
2305       SELECTION_UNLOCK (dbin);
2306       GST_DEBUG_OBJECT (dbin,
2307           "Already handled/handling that SELECT_STREAMS event");
2308       return TRUE;
2309     }
2310     dbin->select_streams_seqnum = seqnum;
2311     if (dbin->pending_select_streams != NULL) {
2312       GST_LOG_OBJECT (dbin, "Replacing pending select streams");
2313       g_list_free (dbin->pending_select_streams);
2314       dbin->pending_select_streams = NULL;
2315     }
2316     gst_event_parse_select_streams (event, &streams);
2317     dbin->pending_select_streams = g_list_copy (streams);
2318     SELECTION_UNLOCK (dbin);
2319
2320     /* FIXME : We don't have an upstream ?? */
2321 #if 0
2322     /* Send event upstream */
2323     if ((peer = gst_pad_get_peer (pad))) {
2324       gst_pad_send_event (peer, event);
2325       gst_object_unref (peer);
2326     }
2327 #endif
2328     /* Finally handle the switch */
2329     if (streams)
2330       handle_stream_switch (dbin, streams, seqnum);
2331
2332     return TRUE;
2333   }
2334   return GST_ELEMENT_CLASS (parent_class)->send_event (element, event);
2335 }
2336
2337
2338 static void
2339 free_multiqueue_slot (GstDecodebin3 * dbin, MultiQueueSlot * slot)
2340 {
2341   if (slot->probe_id)
2342     gst_pad_remove_probe (slot->src_pad, slot->probe_id);
2343   if (slot->input) {
2344     if (slot->input->srcpad)
2345       gst_pad_unlink (slot->input->srcpad, slot->sink_pad);
2346   }
2347
2348   gst_element_release_request_pad (dbin->multiqueue, slot->sink_pad);
2349   gst_object_replace ((GstObject **) & slot->sink_pad, NULL);
2350   gst_object_replace ((GstObject **) & slot->src_pad, NULL);
2351   gst_object_replace ((GstObject **) & slot->active_stream, NULL);
2352   g_free (slot);
2353 }
2354
2355 /* Create a DecodebinOutputStream for a given type
2356  * Note: It will be empty initially, it needs to be configured
2357  * afterwards */
2358 static DecodebinOutputStream *
2359 create_output_stream (GstDecodebin3 * dbin, GstStreamType type)
2360 {
2361   DecodebinOutputStream *res = g_new0 (DecodebinOutputStream, 1);
2362   gchar *pad_name;
2363   const gchar *prefix;
2364   GstStaticPadTemplate *templ;
2365   GstPadTemplate *ptmpl;
2366   guint32 *counter;
2367   GstPad *internal_pad;
2368
2369   GST_DEBUG_OBJECT (dbin, "Created new output stream %p for type %s",
2370       res, gst_stream_type_get_name (type));
2371
2372   res->type = type;
2373   res->dbin = dbin;
2374
2375   switch (type) {
2376     case GST_STREAM_TYPE_VIDEO:
2377       templ = &video_src_template;
2378       counter = &dbin->vpadcount;
2379       prefix = "video";
2380       break;
2381     case GST_STREAM_TYPE_AUDIO:
2382       templ = &audio_src_template;
2383       counter = &dbin->apadcount;
2384       prefix = "audio";
2385       break;
2386     case GST_STREAM_TYPE_TEXT:
2387       templ = &text_src_template;
2388       counter = &dbin->tpadcount;
2389       prefix = "text";
2390       break;
2391     default:
2392       templ = &src_template;
2393       counter = &dbin->opadcount;
2394       prefix = "src";
2395       break;
2396   }
2397
2398   pad_name = g_strdup_printf ("%s_%u", prefix, *counter);
2399   *counter += 1;
2400   ptmpl = gst_static_pad_template_get (templ);
2401   res->src_pad = gst_ghost_pad_new_no_target_from_template (pad_name, ptmpl);
2402   gst_object_unref (ptmpl);
2403   g_free (pad_name);
2404   gst_pad_set_active (res->src_pad, TRUE);
2405   /* Put an event probe on the internal proxy pad to detect upstream
2406    * events */
2407   internal_pad =
2408       (GstPad *) gst_proxy_pad_get_internal ((GstProxyPad *) res->src_pad);
2409   gst_pad_add_probe (internal_pad, GST_PAD_PROBE_TYPE_EVENT_UPSTREAM,
2410       (GstPadProbeCallback) ghost_pad_event_probe, res, NULL);
2411   gst_object_unref (internal_pad);
2412
2413   dbin->output_streams = g_list_append (dbin->output_streams, res);
2414
2415   return res;
2416 }
2417
2418 static void
2419 free_output_stream (GstDecodebin3 * dbin, DecodebinOutputStream * output)
2420 {
2421   if (output->slot) {
2422     if (output->decoder_sink && output->decoder)
2423       gst_pad_unlink (output->slot->src_pad, output->decoder_sink);
2424
2425     output->slot->output = NULL;
2426     output->slot = NULL;
2427   }
2428   gst_object_replace ((GstObject **) & output->decoder_sink, NULL);
2429   gst_ghost_pad_set_target ((GstGhostPad *) output->src_pad, NULL);
2430   gst_object_replace ((GstObject **) & output->decoder_src, NULL);
2431   if (output->src_exposed) {
2432     gst_element_remove_pad ((GstElement *) dbin, output->src_pad);
2433   }
2434   if (output->decoder) {
2435     gst_element_set_locked_state (output->decoder, TRUE);
2436     gst_element_set_state (output->decoder, GST_STATE_NULL);
2437     gst_bin_remove ((GstBin *) dbin, output->decoder);
2438   }
2439   g_free (output);
2440 }
2441
2442 static GstStateChangeReturn
2443 gst_decodebin3_change_state (GstElement * element, GstStateChange transition)
2444 {
2445   GstDecodebin3 *dbin = (GstDecodebin3 *) element;
2446   GstStateChangeReturn ret;
2447
2448   /* Upwards */
2449   switch (transition) {
2450     default:
2451       break;
2452   }
2453   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
2454   if (ret == GST_STATE_CHANGE_FAILURE)
2455     goto beach;
2456
2457   switch (transition) {
2458     case GST_STATE_CHANGE_PAUSED_TO_READY:
2459     {
2460       GList *tmp;
2461
2462       /* Free output streams */
2463       for (tmp = dbin->output_streams; tmp; tmp = tmp->next) {
2464         DecodebinOutputStream *output = (DecodebinOutputStream *) tmp->data;
2465         free_output_stream (dbin, output);
2466       }
2467       g_list_free (dbin->output_streams);
2468       dbin->output_streams = NULL;
2469       /* Free multiqueue slots */
2470       for (tmp = dbin->slots; tmp; tmp = tmp->next) {
2471         MultiQueueSlot *slot = (MultiQueueSlot *) tmp->data;
2472         free_multiqueue_slot (dbin, slot);
2473       }
2474       g_list_free (dbin->slots);
2475       dbin->slots = NULL;
2476       /* Free inputs */
2477     }
2478       break;
2479     default:
2480       break;
2481   }
2482 beach:
2483   return ret;
2484 }
2485
2486 gboolean
2487 gst_decodebin3_plugin_init (GstPlugin * plugin)
2488 {
2489   GST_DEBUG_CATEGORY_INIT (decodebin3_debug, "decodebin3", 0, "decoder bin");
2490
2491   return gst_element_register (plugin, "decodebin3", GST_RANK_NONE,
2492       GST_TYPE_DECODEBIN3);
2493 }