3 * Copyright (C) <2015> Centricular Ltd
4 * @author: Edward Hervey <edward@centricular.com>
5 * @author: Jan Schmidt <jan@centricular.com>
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.
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.
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.
28 #include <glib-object.h>
29 #include <glib/gprintf.h>
31 #include <gst/pbutils/pbutils.h>
33 #include "gstplayback.h"
34 #include "gstplay-enum.h"
35 #include "gstrawcaps.h"
38 * SECTION:element-decodebin3
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.
44 * decodebin3 differs from the previous decodebin (decodebin2) in important ways:
48 * supports publication and selection of stream information via
49 * GstStreamCollection messages and #GST_EVENT_SELECT_STREAM events.
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.
59 * supports multiple input pads for the parallel decoding of auxilliary streams
60 * not muxed with the primary stream.
63 * does not handle network stream buffering. decodebin3 expects that network stream
64 * buffering is handled upstream, before data is passed to it.
68 * <emphasis>decodebin3 is still experimental API and a technology preview.
69 * Its behaviour and exposed API is subject to change.</emphasis>
76 * 1) From sink pad to elementary streams (GstParseBin)
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
83 * All elementary streams (whether decoded or not, whether exposed or not) are
84 * fed through multiqueue. There is only *one* multiqueue in decodebin3.
86 * => MultiQueue is the cornerstone.
87 * => No buffering before multiqueue
89 * 2) Elementary streams
91 * After GstParseBin, there are 3 main components:
92 * 1) Input Streams (provided by GstParseBin)
96 * Input Streams correspond to the stream coming from GstParseBin and that gets
97 * fed into a multiqueue slot.
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, ...).
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
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:
114 * the stream is done => Mark that component as done, optionally freeing/removing it
116 * a new stream is starting => link it further if needed
119 * 3) Gradual replacement
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.
124 * This is handled by the probes in various locations:
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)
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)
141 * Input(s) Slots Streams
142 * /-------------------------------------------\ /-----\ /------------- \
144 * +-------------------------------------------------------------------------+
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 * | +---------------------------------------------+ ^ | |
156 * +-----------------------------------------------+--------+-------------+--+
159 * Probes --/--------/-------------/
163 * We want to ensure we re-use decoders when switching streams. This takes place
164 * at the multiqueue output level.
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().
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
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
186 GST_DEBUG_CATEGORY_STATIC (decodebin3_debug);
187 #define GST_CAT_DEFAULT decodebin3_debug
189 #define GST_TYPE_DECODEBIN3 (gst_decodebin3_get_type ())
191 #define EXTRA_DEBUG 1
193 typedef struct _GstDecodebin3 GstDecodebin3;
194 typedef struct _GstDecodebin3Class GstDecodebin3Class;
196 typedef struct _DecodebinInputStream DecodebinInputStream;
197 typedef struct _DecodebinInput DecodebinInput;
198 typedef struct _DecodebinOutputStream DecodebinOutputStream;
200 struct _GstDecodebin3
204 /* input_lock protects the following variables */
206 /* Main input (static sink pad) */
207 DecodebinInput *main_input;
208 /* Supplementary input (request sink pads) */
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 */
217 GstElement *multiqueue;
219 /* FIXME : Mutex for protecting values below */
220 GstStreamCollection *collection; /* Active collection */
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 */
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) */
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 */
244 /* List of pending collections.
245 * FIXME : Is this really needed ? */
246 GList *pending_collection;
250 GMutex factories_lock;
251 guint32 factories_cookie;
252 /* All DECODABLE factories */
254 /* Only DECODER factories */
255 GList *decoder_factories;
256 /* DECODABLE but not DECODER factories */
257 GList *decodable_factories;
259 /* counters for pads */
260 guint32 apadcount, vpadcount, tpadcount, opadcount;
266 struct _GstDecodebin3Class
270 gint (*select_stream) (GstDecodebin3 * dbin,
271 GstStreamCollection * collection, GstStream * stream);
274 /* Input of decodebin, controls input pad and parsebin */
275 struct _DecodebinInput
282 GstPad *parsebin_sink;
284 GstStreamCollection *collection; /* Active collection */
288 GstElement *parsebin;
290 gulong pad_added_sigid;
291 gulong pad_removed_sigid;
293 /* HACK : Remove these fields */
294 /* List of PendingPad structures */
298 /* Multiqueue Slots */
299 typedef struct _MultiQueueSlot
304 /* Type of stream handled by this slot */
307 /* Linked input and output */
308 DecodebinInputStream *input;
310 /* pending => last stream received on sink pad */
311 GstStream *pending_stream;
312 /* active => last stream outputted on source pad */
313 GstStream *active_stream;
315 GstPad *sink_pad, *src_pad;
317 /* id of the MQ src_pad event probe */
322 DecodebinOutputStream *output;
325 /* Streams that are exposed downstream (i.e. output) */
326 struct _DecodebinOutputStream
329 /* The type of stream handled by this output stream */
332 /* The slot to which this output stream is currently connected to */
333 MultiQueueSlot *slot;
335 GstElement *decoder; /* Optional */
336 GstPad *decoder_sink, *decoder_src;
341 /* Flag if ghost pad is exposed */
342 gboolean src_exposed;
344 /* keyframe dropping probe */
345 gulong drop_probe_id;
348 /* Pending pads from parsebin */
349 typedef struct _PendingPad
352 DecodebinInput *input;
361 #define DEFAULT_CAPS (gst_static_caps_get (&default_raw_caps))
372 SIGNAL_SELECT_STREAM,
375 static guint gst_decodebin3_signals[LAST_SIGNAL] = { 0 };
377 #define SELECTION_LOCK(dbin) G_STMT_START { \
378 GST_LOG_OBJECT (dbin, \
379 "selection locking from thread %p", \
381 g_mutex_lock (&dbin->selection_lock); \
382 GST_LOG_OBJECT (dbin, \
383 "selection locked from thread %p", \
387 #define SELECTION_UNLOCK(dbin) G_STMT_START { \
388 GST_LOG_OBJECT (dbin, \
389 "selection unlocking from thread %p", \
391 g_mutex_unlock (&dbin->selection_lock); \
394 #define INPUT_LOCK(dbin) G_STMT_START { \
395 GST_LOG_OBJECT (dbin, \
396 "input locking from thread %p", \
398 g_mutex_lock (&dbin->input_lock); \
399 GST_LOG_OBJECT (dbin, \
400 "input locked from thread %p", \
404 #define INPUT_UNLOCK(dbin) G_STMT_START { \
405 GST_LOG_OBJECT (dbin, \
406 "input unlocking from thread %p", \
408 g_mutex_unlock (&dbin->input_lock); \
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);
415 static GstStaticCaps default_raw_caps = GST_STATIC_CAPS (DEFAULT_RAW_CAPS);
417 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
420 GST_STATIC_CAPS_ANY);
422 static GstStaticPadTemplate request_sink_template =
423 GST_STATIC_PAD_TEMPLATE ("sink_%u",
426 GST_STATIC_CAPS_ANY);
428 static GstStaticPadTemplate video_src_template =
429 GST_STATIC_PAD_TEMPLATE ("video_%u",
432 GST_STATIC_CAPS_ANY);
434 static GstStaticPadTemplate audio_src_template =
435 GST_STATIC_PAD_TEMPLATE ("audio_%u",
438 GST_STATIC_CAPS_ANY);
440 static GstStaticPadTemplate text_src_template =
441 GST_STATIC_PAD_TEMPLATE ("text_%u",
444 GST_STATIC_CAPS_ANY);
446 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src_%u",
449 GST_STATIC_CAPS_ANY);
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);
458 static gboolean parsebin_autoplug_continue_cb (GstElement *
459 parsebin, GstPad * pad, GstCaps * caps, GstDecodebin3 * dbin);
462 gst_decodebin3_select_stream (GstDecodebin3 * dbin,
463 GstStreamCollection * collection, GstStream * stream)
465 GST_LOG_OBJECT (dbin, "default select-stream, returning -1");
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,
478 static void gst_decode_bin_update_factories_list (GstDecodebin3 * dbin);
480 static gboolean have_factory (GstDecodebin3 * dbin, GstCaps * caps,
481 GstElementFactoryListType ftype);
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);
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,
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);
504 /* FIXME: Really make all the parser stuff a self-contained helper object */
505 #include "gstdecodebin3-parse.c"
508 _gst_int_accumulator (GSignalInvocationHint * ihint,
509 GValue * return_accu, const GValue * handler_return, gpointer dummy)
511 gint res = g_value_get_int (handler_return);
513 if (!(ihint->run_type & G_SIGNAL_RUN_CLEANUP))
514 g_value_set_int (return_accu, res);
523 gst_decodebin3_class_init (GstDecodebin3Class * klass)
525 GObjectClass *gobject_klass = (GObjectClass *) klass;
526 GstElementClass *element_class = (GstElementClass *) klass;
527 GstBinClass *bin_klass = (GstBinClass *) klass;
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;
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));
539 /* FIXME : ADD SIGNALS ! */
541 * GstDecodebin3::select-stream
542 * @decodebin: a #GstDecodebin3
543 * @collection: a #GstStreamCollection
544 * @stream: a #GstStream
546 * This signal is emitted whenever @decodebin needs to decide whether
547 * to expose a @stream of a given @collection.
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.
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);
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);
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));
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>");
582 bin_klass->handle_message = gst_decodebin3_handle_message;
584 klass->select_stream = gst_decodebin3_select_stream;
588 gst_decodebin3_init (GstDecodebin3 * dbin)
590 /* Create main input */
591 dbin->main_input = create_new_input (dbin, TRUE);
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);
598 dbin->current_group_id = G_MAXUINT32;
600 g_mutex_init (&dbin->factories_lock);
601 g_mutex_init (&dbin->selection_lock);
602 g_mutex_init (&dbin->input_lock);
604 dbin->caps = gst_static_caps_get (&default_raw_caps);
608 gst_decodebin3_dispose (GObject * object)
610 GstDecodebin3 *dbin = (GstDecodebin3 *) object;
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);
624 free_input (dbin, dbin->main_input);
625 /* FIXME : GO OVER INPUTS */
627 G_OBJECT_CLASS (parent_class)->dispose (object);
631 gst_decodebin3_set_property (GObject * object, guint prop_id,
632 const GValue * value, GParamSpec * pspec)
634 GstDecodebin3 *dbin = (GstDecodebin3 *) object;
636 /* FIXME : IMPLEMENT */
639 GST_OBJECT_LOCK (dbin);
641 gst_caps_unref (dbin->caps);
642 dbin->caps = g_value_dup_boxed (value);
643 GST_OBJECT_UNLOCK (dbin);
646 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
652 gst_decodebin3_get_property (GObject * object, guint prop_id, GValue * value,
655 GstDecodebin3 *dbin = (GstDecodebin3 *) object;
657 /* FIXME : IMPLEMENT */
660 GST_OBJECT_LOCK (dbin);
661 g_value_set_boxed (value, dbin->caps);
662 GST_OBJECT_UNLOCK (dbin);
665 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
671 parsebin_autoplug_continue_cb (GstElement * parsebin, GstPad * pad,
672 GstCaps * caps, GstDecodebin3 * dbin)
674 GST_DEBUG_OBJECT (pad, "caps %" GST_PTR_FORMAT, caps);
676 /* If it matches our target caps, expose it */
677 if (gst_caps_can_intersect (caps, dbin->caps))
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 */
687 set_input_group_id (DecodebinInput * input, guint32 * group_id)
689 GstDecodebin3 *dbin = input->dbin;
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;
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,
703 dbin->current_group_id = *group_id;
705 *group_id = dbin->current_group_id;
712 /* Call with INPUT_LOCK taken */
714 ensure_input_parsebin (GstDecodebin3 * dbin, DecodebinInput * input)
716 gboolean set_state = FALSE;
718 if (input->parsebin == NULL) {
719 input->parsebin = gst_element_factory_make ("parsebin", NULL);
720 if (input->parsebin == NULL)
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);
734 if (GST_OBJECT_PARENT (GST_OBJECT (input->parsebin)) != GST_OBJECT (dbin)) {
735 gst_bin_add (GST_BIN (dbin), input->parsebin);
739 gst_ghost_pad_set_target (GST_GHOST_PAD (input->ghost_sink),
740 input->parsebin_sink);
742 gst_element_sync_state_with_parent (input->parsebin);
749 gst_element_post_message ((GstElement *) dbin,
750 gst_missing_element_message_new ((GstElement *) dbin, "parsebin"));
755 static GstPadLinkReturn
756 gst_decodebin3_input_pad_link (GstPad * pad, GstObject * parent, GstPad * peer)
758 GstDecodebin3 *dbin = (GstDecodebin3 *) parent;
759 GstPadLinkReturn res = GST_PAD_LINK_OK;
760 DecodebinInput *input;
762 GST_LOG_OBJECT (parent, "Got link on input pad %" GST_PTR_FORMAT
763 ". Creating parsebin if needed", pad);
765 if ((input = g_object_get_data (G_OBJECT (pad), "decodebin.input")) == NULL)
769 if (!ensure_input_parsebin (dbin, input))
770 res = GST_PAD_LINK_REFUSED;
775 GST_ERROR_OBJECT (parent, "Failed to retrieve input state from ghost pad");
776 return GST_PAD_LINK_REFUSED;
780 gst_decodebin3_input_pad_unlink (GstPad * pad, GstObject * parent)
782 GstDecodebin3 *dbin = (GstDecodebin3 *) parent;
783 DecodebinInput *input;
785 GST_LOG_OBJECT (parent, "Got unlink on input pad %" GST_PTR_FORMAT
786 ". Removing parsebin.", pad);
788 if ((input = g_object_get_data (G_OBJECT (pad), "decodebin.input")) == NULL)
792 if (input->parsebin == NULL) {
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);
805 GST_ERROR_OBJECT (parent, "Failed to retrieve input state from ghost pad");
810 free_input (GstDecodebin3 * dbin, DecodebinInput * input)
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);
822 if (input->collection)
823 gst_object_unref (input->collection);
827 /* Call with INPUT_LOCK taken */
828 static DecodebinInput *
829 create_new_input (GstDecodebin3 * dbin, gboolean main)
831 DecodebinInput *input;
833 input = g_new0 (DecodebinInput, 1);
835 input->is_main = main;
836 input->group_id = G_MAXUINT32;
838 input->ghost_sink = gst_ghost_pad_new_no_target ("sink", GST_PAD_SINK);
840 gchar *pad_name = g_strdup_printf ("sink_%u", dbin->input_counter++);
841 input->ghost_sink = gst_ghost_pad_new_no_target (pad_name, GST_PAD_SINK);
844 g_object_set_data (G_OBJECT (input->ghost_sink), "decodebin.input", input);
845 gst_pad_set_link_function (input->ghost_sink, gst_decodebin3_input_pad_link);
846 gst_pad_set_unlink_function (input->ghost_sink,
847 gst_decodebin3_input_pad_unlink);
849 gst_pad_set_active (input->ghost_sink, TRUE);
850 gst_element_add_pad ((GstElement *) dbin, input->ghost_sink);
857 gst_decodebin3_request_new_pad (GstElement * element, GstPadTemplate * temp,
858 const gchar * name, const GstCaps * caps)
860 GstDecodebin3 *dbin = (GstDecodebin3 *) element;
861 DecodebinInput *input;
864 /* We are ignoring names for the time being, not sure it makes any sense
865 * within the context of decodebin3 ... */
867 input = create_new_input (dbin, FALSE);
869 dbin->other_inputs = g_list_append (dbin->other_inputs, input);
870 res = input->ghost_sink;
877 /* Must be called with factories lock! */
879 gst_decode_bin_update_factories_list (GstDecodebin3 * dbin)
883 cookie = gst_registry_get_feature_list_cookie (gst_registry_get ());
884 if (!dbin->factories || dbin->factories_cookie != cookie) {
887 gst_plugin_feature_list_free (dbin->factories);
888 if (dbin->decoder_factories)
889 g_list_free (dbin->decoder_factories);
890 if (dbin->decodable_factories)
891 g_list_free (dbin->decodable_factories);
893 gst_element_factory_list_get_elements
894 (GST_ELEMENT_FACTORY_TYPE_DECODABLE, GST_RANK_MARGINAL);
896 g_list_sort (dbin->factories, gst_plugin_feature_rank_compare_func);
897 dbin->factories_cookie = cookie;
899 /* Filter decoder and other decodables */
900 dbin->decoder_factories = NULL;
901 dbin->decodable_factories = NULL;
902 for (tmp = dbin->factories; tmp; tmp = tmp->next) {
903 GstElementFactory *fact = (GstElementFactory *) tmp->data;
904 if (gst_element_factory_list_is_type (fact,
905 GST_ELEMENT_FACTORY_TYPE_DECODER))
906 dbin->decoder_factories = g_list_append (dbin->decoder_factories, fact);
908 dbin->decodable_factories =
909 g_list_append (dbin->decodable_factories, fact);
914 /* Must be called with appropriate lock if list is a protected variable */
916 stream_in_list (GList * list, const gchar * sid)
921 for (tmp = list; tmp; tmp = tmp->next) {
922 gchar *osid = (gchar *) tmp->data;
923 GST_DEBUG ("Checking %s against %s", sid, osid);
927 for (tmp = list; tmp; tmp = tmp->next) {
928 gchar *osid = (gchar *) tmp->data;
929 if (!g_strcmp0 (sid, osid))
937 update_requested_selection (GstDecodebin3 * dbin,
938 GstStreamCollection * collection)
942 GstStreamType used_types = 0;
944 nb = gst_stream_collection_get_size (collection);
946 /* 1. Is there a pending SELECT_STREAMS we can return straight away since
947 * the switch handler will take care of the pending selection */
948 SELECTION_LOCK (dbin);
949 if (dbin->pending_select_streams) {
950 GST_DEBUG_OBJECT (dbin,
951 "No need to create pending selection, SELECT_STREAMS underway");
955 /* 2. If not, are we in EXPOSE_ALL_MODE ? If so, match everything */
956 GST_FIXME_OBJECT (dbin, "Implement EXPOSE_ALL_MODE");
958 /* 3. If not, check if we already have some of the streams in the
959 * existing active/requested selection */
960 for (i = 0; i < nb; i++) {
961 GstStream *stream = gst_stream_collection_get_stream (collection, i);
962 const gchar *sid = gst_stream_get_stream_id (stream);
964 /* Fire select-stream signal to see if outside components want to
965 * hint at which streams should be selected */
966 g_signal_emit (G_OBJECT (dbin),
967 gst_decodebin3_signals[SIGNAL_SELECT_STREAM], 0, collection, stream,
969 GST_DEBUG_OBJECT (dbin, "stream %s , request:%d", sid, request);
970 if (request == 1 || (request == -1
971 && (stream_in_list (dbin->requested_selection, sid)
972 || stream_in_list (dbin->active_selection, sid)))) {
973 GstStreamType curtype = gst_stream_get_stream_type (stream);
975 GST_DEBUG_OBJECT (dbin,
976 "Using stream requested by 'select-stream' signal : %s", sid);
978 GST_DEBUG_OBJECT (dbin,
979 "Re-using stream already present in requested or active selection : %s",
981 tmp = g_list_append (tmp, (gchar *) sid);
982 used_types |= curtype;
986 /* 4. If not, match one stream of each type */
987 for (i = 0; i < nb; i++) {
988 GstStream *stream = gst_stream_collection_get_stream (collection, i);
989 GstStreamType curtype = gst_stream_get_stream_type (stream);
990 if (!(used_types & curtype)) {
991 const gchar *sid = gst_stream_get_stream_id (stream);
992 GST_DEBUG_OBJECT (dbin, "Selecting stream '%s' of type %s",
993 sid, gst_stream_type_get_name (curtype));
994 tmp = g_list_append (tmp, (gchar *) sid);
995 used_types |= curtype;
1000 /* Finally set the requested selection */
1002 if (dbin->requested_selection) {
1003 GST_FIXME_OBJECT (dbin,
1004 "Replacing non-NULL requested_selection, what should we do ??");
1005 g_list_free (dbin->requested_selection);
1007 dbin->requested_selection = tmp;
1008 dbin->selection_updated = TRUE;
1010 SELECTION_UNLOCK (dbin);
1013 /* Call with INPUT_LOCK taken */
1014 static GstStreamCollection *
1015 get_merged_collection (GstDecodebin3 * dbin)
1017 gboolean needs_merge = FALSE;
1018 GstStreamCollection *res = NULL;
1022 /* First check if we need to do a merge or just return the only collection */
1023 res = dbin->main_input->collection;
1025 for (tmp = dbin->other_inputs; tmp; tmp = tmp->next) {
1026 DecodebinInput *input = (DecodebinInput *) tmp->data;
1027 if (input->collection) {
1032 res = input->collection;
1037 GST_DEBUG_OBJECT (dbin, "No need to merge, returning %p", res);
1038 return gst_object_ref (res);
1041 /* We really need to create a new collection */
1042 /* FIXME : Some numbering scheme maybe ?? */
1043 res = gst_stream_collection_new ("decodebin3");
1044 if (dbin->main_input->collection) {
1045 nb_stream = gst_stream_collection_get_size (dbin->main_input->collection);
1046 GST_DEBUG_OBJECT (dbin, "main input %p %d", dbin->main_input, nb_stream);
1047 for (i = 0; i < nb_stream; i++) {
1049 gst_stream_collection_get_stream (dbin->main_input->collection, i);
1050 gst_stream_collection_add_stream (res, gst_object_ref (stream));
1054 for (tmp = dbin->other_inputs; tmp; tmp = tmp->next) {
1055 DecodebinInput *input = (DecodebinInput *) tmp->data;
1056 GST_DEBUG_OBJECT (dbin, "input %p , collection %p", input,
1058 if (input->collection) {
1059 nb_stream = gst_stream_collection_get_size (input->collection);
1060 GST_DEBUG_OBJECT (dbin, "nb_stream : %d", nb_stream);
1061 for (i = 0; i < nb_stream; i++) {
1063 gst_stream_collection_get_stream (input->collection, i);
1064 gst_stream_collection_add_stream (res, gst_object_ref (stream));
1072 /* Call with INPUT_LOCK taken */
1073 static DecodebinInput *
1074 find_message_parsebin (GstDecodebin3 * dbin, GstElement * child)
1076 DecodebinInput *input = NULL;
1077 GstElement *parent = gst_object_ref (child);
1081 GstElement *next_parent;
1083 GST_DEBUG_OBJECT (dbin, "parent %s",
1084 parent ? GST_ELEMENT_NAME (parent) : "<NONE>");
1086 if (parent == dbin->main_input->parsebin) {
1087 input = dbin->main_input;
1090 for (tmp = dbin->other_inputs; tmp; tmp = tmp->next) {
1091 DecodebinInput *cur = (DecodebinInput *) tmp->data;
1092 if (parent == cur->parsebin) {
1097 next_parent = (GstElement *) gst_element_get_parent (parent);
1098 gst_object_unref (parent);
1099 parent = next_parent;
1101 } while (parent && parent != (GstElement *) dbin);
1104 gst_object_unref (parent);
1110 stream_in_collection (GstDecodebin3 * dbin, gchar * sid)
1114 if (dbin->collection == NULL)
1116 len = gst_stream_collection_get_size (dbin->collection);
1117 for (i = 0; i < len; i++) {
1118 GstStream *stream = gst_stream_collection_get_stream (dbin->collection, i);
1119 const gchar *osid = gst_stream_get_stream_id (stream);
1120 if (!g_strcmp0 (sid, osid))
1127 /* Call with INPUT_LOCK taken */
1129 handle_stream_collection (GstDecodebin3 * dbin,
1130 GstStreamCollection * collection, GstElement * child)
1132 #ifndef GST_DISABLE_GST_DEBUG
1133 const gchar *upstream_id;
1136 DecodebinInput *input = find_message_parsebin (dbin, child);
1139 GST_DEBUG_OBJECT (dbin,
1140 "Couldn't find corresponding input, most likely shutting down");
1144 /* Replace collection in input */
1145 if (input->collection)
1146 gst_object_unref (input->collection);
1147 input->collection = gst_object_ref (collection);
1148 GST_DEBUG_OBJECT (dbin, "Setting collection %p on input %p", collection,
1151 /* Merge collection if needed */
1152 collection = get_merged_collection (dbin);
1154 #ifndef GST_DISABLE_GST_DEBUG
1155 /* Just some debugging */
1156 upstream_id = gst_stream_collection_get_upstream_id (collection);
1157 GST_DEBUG ("Received Stream Collection. Upstream_id : %s", upstream_id);
1158 GST_DEBUG ("From input %p", input);
1159 GST_DEBUG (" %d streams", gst_stream_collection_get_size (collection));
1160 for (i = 0; i < gst_stream_collection_get_size (collection); i++) {
1161 GstStream *stream = gst_stream_collection_get_stream (collection, i);
1162 GstTagList *taglist;
1165 GST_DEBUG (" Stream '%s'", gst_stream_get_stream_id (stream));
1166 GST_DEBUG (" type : %s",
1167 gst_stream_type_get_name (gst_stream_get_stream_type (stream)));
1168 GST_DEBUG (" flags : 0x%x", gst_stream_get_stream_flags (stream));
1169 taglist = gst_stream_get_tags (stream);
1170 GST_DEBUG (" tags : %" GST_PTR_FORMAT, taglist);
1171 caps = gst_stream_get_caps (stream);
1172 GST_DEBUG (" caps : %" GST_PTR_FORMAT, caps);
1174 gst_tag_list_unref (taglist);
1175 gst_caps_unref (caps);
1179 /* Store collection for later usage */
1180 if (dbin->collection == NULL) {
1181 dbin->collection = collection;
1183 /* We need to check who emitted this collection (the owner).
1184 * If we already had a collection from that user, this one is an update,
1185 * that is to say that we need to figure out how we are going to re-use
1186 * the streams/slot */
1187 GST_FIXME_OBJECT (dbin, "New collection but already had one ...");
1188 /* FIXME : When do we switch from pending collection to active collection ?
1189 * When all streams from active collection are drained in multiqueue output ? */
1190 gst_object_unref (dbin->collection);
1191 dbin->collection = collection;
1192 /* dbin->pending_collection = */
1193 /* g_list_append (dbin->pending_collection, collection); */
1198 gst_decodebin3_handle_message (GstBin * bin, GstMessage * message)
1200 GstDecodebin3 *dbin = (GstDecodebin3 *) bin;
1201 gboolean posting_collection = FALSE;
1203 GST_DEBUG_OBJECT (bin, "Got Message %s", GST_MESSAGE_TYPE_NAME (message));
1205 switch (GST_MESSAGE_TYPE (message)) {
1206 case GST_MESSAGE_STREAM_COLLECTION:
1208 GstStreamCollection *collection = NULL;
1209 gst_message_parse_stream_collection (message, &collection);
1212 handle_stream_collection (dbin, collection,
1213 (GstElement *) GST_MESSAGE_SRC (message));
1214 posting_collection = TRUE;
1215 INPUT_UNLOCK (dbin);
1217 if (dbin->collection && collection != dbin->collection) {
1218 /* Replace collection message, we most likely aggregated it */
1219 GstMessage *new_msg;
1221 gst_message_new_stream_collection ((GstObject *) dbin,
1223 gst_message_unref (message);
1227 gst_object_unref (collection);
1234 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
1236 if (posting_collection) {
1237 /* Figure out a selection for that collection */
1238 update_requested_selection (dbin, dbin->collection);
1242 static DecodebinOutputStream *
1243 find_free_compatible_output (GstDecodebin3 * dbin, GstStream * stream)
1246 GstStreamType stype = gst_stream_get_stream_type (stream);
1248 for (tmp = dbin->output_streams; tmp; tmp = tmp->next) {
1249 DecodebinOutputStream *output = (DecodebinOutputStream *) tmp->data;
1250 if (output->type == stype && output->slot && output->slot->active_stream) {
1251 GstStream *tstream = output->slot->active_stream;
1252 if (!stream_in_list (dbin->requested_selection,
1253 (gchar *) gst_stream_get_stream_id (tstream))) {
1262 /* Give a certain slot, figure out if it should be linked to an
1264 * CALL WITH SELECTION LOCK TAKEN !*/
1265 static DecodebinOutputStream *
1266 get_output_for_slot (MultiQueueSlot * slot)
1268 GstDecodebin3 *dbin = slot->dbin;
1269 DecodebinOutputStream *output = NULL;
1270 const gchar *stream_id;
1273 /* If we already have a configured output, just use it */
1274 if (slot->output != NULL)
1275 return slot->output;
1280 * This method needs to be split into multiple parts
1282 * 1) Figure out whether stream should be exposed or not
1283 * This is based on autoplug-continue, EXPOSE_ALL_MODE, or presence
1284 * in the default stream attribution
1286 * 2) Figure out whether an output stream should be created, whether
1287 * we can re-use the output stream already linked to the slot, or
1288 * whether we need to get re-assigned another (currently used) output
1292 stream_id = gst_stream_get_stream_id (slot->active_stream);
1293 caps = gst_stream_get_caps (slot->active_stream);
1294 GST_DEBUG_OBJECT (dbin, "stream %s , %" GST_PTR_FORMAT, stream_id, caps);
1295 gst_caps_unref (caps);
1297 /* 0. Emit autoplug-continue signal for pending caps ? */
1298 GST_FIXME_OBJECT (dbin, "emit autoplug-continue");
1300 /* 1. if in EXPOSE_ALL_MODE, just accept */
1301 GST_FIXME_OBJECT (dbin, "Handle EXPOSE_ALL_MODE");
1304 /* FIXME : The idea around this was to avoid activating a stream for
1305 * which we have no decoder. Unfortunately it is way too
1306 * expensive. Need to figure out a better solution */
1307 /* 2. Is there a potential decoder (if one is required) */
1308 if (!gst_caps_can_intersect (caps, dbin->caps)
1309 && !have_factory (dbin, (GstCaps *) caps,
1310 GST_ELEMENT_FACTORY_TYPE_DECODER)) {
1311 GST_WARNING_OBJECT (dbin, "Don't have a decoder for %" GST_PTR_FORMAT,
1313 SELECTION_UNLOCK (dbin);
1314 gst_element_post_message (GST_ELEMENT_CAST (dbin),
1315 gst_missing_decoder_message_new (GST_ELEMENT_CAST (dbin), caps));
1316 SELECTION_LOCK (dbin);
1321 /* 3. In default mode check if we should expose */
1322 if (stream_in_list (dbin->requested_selection, stream_id)) {
1323 /* Check if we can steal an existing output stream we could re-use.
1325 * * an output stream whose slot->stream is not in requested
1326 * * and is of the same type as this stream
1328 output = find_free_compatible_output (dbin, slot->active_stream);
1330 /* Move this output from its current slot to this slot */
1332 g_list_append (dbin->to_activate, (gchar *) stream_id);
1333 dbin->requested_selection =
1334 g_list_remove (dbin->requested_selection, stream_id);
1335 SELECTION_UNLOCK (dbin);
1336 gst_pad_add_probe (output->slot->src_pad, GST_PAD_PROBE_TYPE_IDLE,
1337 (GstPadProbeCallback) slot_unassign_probe, output->slot, NULL);
1338 SELECTION_LOCK (dbin);
1342 output = create_output_stream (dbin, slot->type);
1343 output->slot = slot;
1344 GST_DEBUG ("Linking slot %p to new output %p", slot, output);
1345 slot->output = output;
1346 dbin->active_selection =
1347 g_list_append (dbin->active_selection, (gchar *) stream_id);
1349 GST_DEBUG ("Not creating any output for slot %p", slot);
1354 /* Returns SELECTED_STREAMS message if active_selection is equal to
1355 * requested_selection, else NULL.
1356 * Must be called with LOCK taken */
1358 is_selection_done (GstDecodebin3 * dbin)
1363 if (!dbin->selection_updated)
1366 GST_LOG_OBJECT (dbin, "Checking");
1368 if (dbin->to_activate != NULL) {
1369 GST_DEBUG ("Still have streams to activate");
1372 for (tmp = dbin->requested_selection; tmp; tmp = tmp->next) {
1373 GST_DEBUG ("Checking requested stream %s", (gchar *) tmp->data);
1374 if (!stream_in_list (dbin->active_selection, (gchar *) tmp->data)) {
1375 GST_DEBUG ("Not in active selection, returning");
1380 GST_DEBUG_OBJECT (dbin, "Selection active, creating message");
1382 /* We are completely active */
1383 msg = gst_message_new_streams_selected ((GstObject *) dbin, dbin->collection);
1384 GST_MESSAGE_SEQNUM (msg) = dbin->select_streams_seqnum;
1385 for (tmp = dbin->output_streams; tmp; tmp = tmp->next) {
1386 DecodebinOutputStream *output = (DecodebinOutputStream *) tmp->data;
1388 GST_DEBUG_OBJECT (dbin, "Adding stream %s",
1389 gst_stream_get_stream_id (output->slot->active_stream));
1391 gst_message_streams_selected_add (msg, output->slot->active_stream);
1393 GST_WARNING_OBJECT (dbin, "No valid slot for output %p", output);
1395 dbin->selection_updated = FALSE;
1399 static GstPadProbeReturn
1400 multiqueue_src_probe (GstPad * pad, GstPadProbeInfo * info,
1401 MultiQueueSlot * slot)
1403 GstPadProbeReturn ret = GST_PAD_PROBE_OK;
1404 GstDecodebin3 *dbin = slot->dbin;
1406 if (GST_IS_EVENT (GST_PAD_PROBE_INFO_DATA (info))) {
1407 GstEvent *ev = GST_PAD_PROBE_INFO_EVENT (info);
1409 GST_DEBUG_OBJECT (pad, "Got event %p %s", ev, GST_EVENT_TYPE_NAME (ev));
1410 switch (GST_EVENT_TYPE (ev)) {
1411 case GST_EVENT_STREAM_START:
1413 GstStream *stream = NULL;
1414 const gchar *stream_id;
1416 gst_event_parse_stream (ev, &stream);
1417 if (stream == NULL) {
1418 GST_ERROR_OBJECT (pad,
1419 "Got a STREAM_START event without a GstStream");
1422 stream_id = gst_stream_get_stream_id (stream);
1423 GST_DEBUG_OBJECT (pad, "Stream Start '%s'", stream_id);
1424 if (slot->active_stream == NULL) {
1425 slot->active_stream = stream;
1426 } else if (slot->active_stream != stream) {
1427 GST_FIXME_OBJECT (pad, "Handle stream changes (%s => %s) !",
1428 gst_stream_get_stream_id (slot->active_stream),
1429 gst_stream_get_stream_id (stream));
1430 gst_object_unref (slot->active_stream);
1431 slot->active_stream = stream;
1433 gst_object_unref (stream);
1434 #if 0 /* Disabled because stream-start is pushed for every buffer on every unlinked pad */
1436 gboolean is_active, is_requested;
1437 /* Quick check to see if we're in the current selection */
1438 /* FIXME : Re-check all slot<=>output mappings based on requested_selection */
1439 SELECTION_LOCK (dbin);
1440 GST_DEBUG_OBJECT (dbin, "Checking active selection");
1441 is_active = stream_in_list (dbin->active_selection, stream_id);
1442 GST_DEBUG_OBJECT (dbin, "Checking requested selection");
1443 is_requested = stream_in_list (dbin->requested_selection, stream_id);
1444 SELECTION_UNLOCK (dbin);
1446 GST_DEBUG_OBJECT (pad, "Slot in ACTIVE selection (output:%p)",
1449 GST_DEBUG_OBJECT (pad, "Slot in REQUESTED selection (output:%p)",
1451 else if (slot->output) {
1452 GST_DEBUG_OBJECT (pad,
1453 "Slot needs to be deactivated ? It's no longer in requested selection");
1454 } else if (!is_active)
1455 GST_DEBUG_OBJECT (pad,
1456 "Slot in neither active nor requested selection");
1461 case GST_EVENT_CAPS:
1463 /* Configure the output slot if needed */
1464 DecodebinOutputStream *output;
1465 GstMessage *msg = NULL;
1466 SELECTION_LOCK (dbin);
1467 output = get_output_for_slot (slot);
1469 reconfigure_output_stream (output, slot);
1470 msg = is_selection_done (dbin);
1472 SELECTION_UNLOCK (dbin);
1474 gst_element_post_message ((GstElement *) slot->dbin, msg);
1478 /* FIXME : Figure out */
1479 GST_FIXME_OBJECT (pad, "EOS on multiqueue source pad. input:%p",
1481 if (slot->input == NULL) {
1483 GST_DEBUG_OBJECT (pad,
1484 "last EOS for input, forwarding and removing slot");
1485 peer = gst_pad_get_peer (pad);
1487 gst_pad_send_event (peer, ev);
1488 gst_object_unref (peer);
1490 gst_event_unref (ev);
1492 SELECTION_LOCK (dbin);
1493 /* FIXME : Shouldn't we try to re-assign the output instead of just
1495 /* Remove the output */
1497 DecodebinOutputStream *output = slot->output;
1498 dbin->output_streams = g_list_remove (dbin->output_streams, output);
1499 free_output_stream (dbin, output);
1501 SELECTION_UNLOCK (dbin);
1502 ret = GST_PAD_PROBE_HANDLED;
1508 } else if (GST_IS_QUERY (GST_PAD_PROBE_INFO_DATA (info))) {
1509 GstQuery *query = GST_PAD_PROBE_INFO_QUERY (info);
1510 switch (GST_QUERY_TYPE (query)) {
1511 case GST_QUERY_CAPS:
1513 GST_DEBUG_OBJECT (pad, "Intercepting CAPS query");
1514 gst_query_set_caps_result (query, GST_CAPS_ANY);
1515 ret = GST_PAD_PROBE_HANDLED;
1519 case GST_QUERY_ACCEPT_CAPS:
1521 GST_DEBUG_OBJECT (pad, "Intercepting Accept Caps query");
1522 /* If the current decoder doesn't accept caps, we'll reconfigure
1523 * on the actual caps event. So accept any caps. */
1524 gst_query_set_accept_caps_result (query, TRUE);
1525 ret = GST_PAD_PROBE_HANDLED;
1535 /* Create a new multiqueue slot for the given type
1537 * It is up to the caller to know whether that slot is needed or not
1538 * (and release it when no longer needed) */
1539 static MultiQueueSlot *
1540 create_new_slot (GstDecodebin3 * dbin, GstStreamType type)
1542 MultiQueueSlot *slot;
1543 GstIterator *it = NULL;
1544 GValue item = { 0, };
1546 GST_DEBUG_OBJECT (dbin, "Creating new slot for type %s",
1547 gst_stream_type_get_name (type));
1548 slot = g_new0 (MultiQueueSlot, 1);
1550 slot->id = dbin->slot_id++;
1552 slot->sink_pad = gst_element_get_request_pad (dbin->multiqueue, "sink_%u");
1553 if (slot->sink_pad == NULL)
1555 it = gst_pad_iterate_internal_links (slot->sink_pad);
1556 if (!it || (gst_iterator_next (it, &item)) != GST_ITERATOR_OK
1557 || ((slot->src_pad = g_value_dup_object (&item)) == NULL)) {
1558 GST_ERROR ("Couldn't get srcpad from multiqueue for sink pad %s:%s",
1559 GST_DEBUG_PAD_NAME (slot->src_pad));
1562 gst_iterator_free (it);
1563 g_value_reset (&item);
1565 g_object_set (slot->sink_pad, "group-id", (guint) type, NULL);
1567 /* Add event probe */
1569 gst_pad_add_probe (slot->src_pad,
1570 GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM | GST_PAD_PROBE_TYPE_QUERY_DOWNSTREAM,
1571 (GstPadProbeCallback) multiqueue_src_probe, slot, NULL);
1573 GST_DEBUG ("Created new slot %u (%p) (%s:%s)", slot->id, slot,
1574 GST_DEBUG_PAD_NAME (slot->src_pad));
1575 dbin->slots = g_list_append (dbin->slots, slot);
1582 gst_element_release_request_pad (dbin->multiqueue, slot->sink_pad);
1588 static MultiQueueSlot *
1589 get_slot_for_input (GstDecodebin3 * dbin, DecodebinInputStream * input)
1592 MultiQueueSlot *empty_slot = NULL;
1593 GstStreamType input_type = 0;
1594 gchar *stream_id = NULL;
1596 GST_DEBUG_OBJECT (dbin, "input %p (stream %p %s)",
1597 input, input->active_stream,
1599 active_stream ? gst_stream_get_stream_id (input->active_stream) : "");
1601 if (input->active_stream) {
1602 input_type = gst_stream_get_stream_type (input->active_stream);
1603 stream_id = (gchar *) gst_stream_get_stream_id (input->active_stream);
1606 /* Go over existing slots and check if there is already one for it */
1607 for (tmp = dbin->slots; tmp; tmp = tmp->next) {
1608 MultiQueueSlot *slot = (MultiQueueSlot *) tmp->data;
1609 /* Already used input, return that one */
1610 if (slot->input == input) {
1611 GST_DEBUG_OBJECT (dbin, "Returning already specified slot %d", slot->id);
1616 /* Go amongst all unused slots of the right type and try to find a candidate */
1617 for (tmp = dbin->slots; tmp; tmp = tmp->next) {
1618 MultiQueueSlot *slot = (MultiQueueSlot *) tmp->data;
1619 if (slot->input == NULL && input_type == slot->type) {
1620 /* Remember this empty slot for later */
1622 /* Check if available slot is of the same stream_id */
1623 GST_LOG_OBJECT (dbin, "Checking candidate slot %d (active_stream:%p)",
1624 slot->id, slot->active_stream);
1625 if (stream_id && slot->active_stream) {
1627 (gchar *) gst_stream_get_stream_id (slot->active_stream);
1628 GST_DEBUG_OBJECT (dbin, "Checking slot %d %s against %s", slot->id,
1629 ostream_id, stream_id);
1630 if (!g_strcmp0 (stream_id, ostream_id))
1637 GST_DEBUG_OBJECT (dbin, "Re-using existing unused slot %d", empty_slot->id);
1638 empty_slot->input = input;
1643 return create_new_slot (dbin, input_type);
1649 link_input_to_slot (DecodebinInputStream * input, MultiQueueSlot * slot)
1652 if (slot->input != NULL && slot->input != input) {
1653 GST_ERROR_OBJECT (slot->dbin,
1654 "Trying to link input to an already used slot");
1657 gst_pad_link_full (input->srcpad, slot->sink_pad, GST_PAD_LINK_CHECK_NOTHING);
1658 slot->pending_stream = input->active_stream;
1659 slot->input = input;
1660 event = gst_pad_get_sticky_event (input->srcpad, GST_EVENT_STREAM_START, 0);
1662 gst_pad_send_event (slot->sink_pad, event);
1667 have_factory (GstDecodebin3 * dbin, GstCaps * caps,
1668 GstElementFactoryListType ftype)
1670 gboolean ret = FALSE;
1673 g_mutex_lock (&dbin->factories_lock);
1674 gst_decode_bin_update_factories_list (dbin);
1675 if (ftype == GST_ELEMENT_FACTORY_TYPE_DECODER)
1677 gst_element_factory_list_filter (dbin->decoder_factories,
1678 caps, GST_PAD_SINK, TRUE);
1681 gst_element_factory_list_filter (dbin->decodable_factories,
1682 caps, GST_PAD_SINK, TRUE);
1683 g_mutex_unlock (&dbin->factories_lock);
1687 gst_plugin_feature_list_free (res);
1695 create_element (GstDecodebin3 * dbin, GstStream * stream,
1696 GstElementFactoryListType ftype)
1699 GstElement *element = NULL;
1702 g_mutex_lock (&dbin->factories_lock);
1703 gst_decode_bin_update_factories_list (dbin);
1704 caps = gst_stream_get_caps (stream);
1705 if (ftype == GST_ELEMENT_FACTORY_TYPE_DECODER)
1707 gst_element_factory_list_filter (dbin->decoder_factories,
1708 caps, GST_PAD_SINK, TRUE);
1711 gst_element_factory_list_filter (dbin->decodable_factories,
1712 caps, GST_PAD_SINK, TRUE);
1713 g_mutex_unlock (&dbin->factories_lock);
1717 gst_element_factory_create ((GstElementFactory *) res->data, NULL);
1718 GST_DEBUG ("Created element '%s'", GST_ELEMENT_NAME (element));
1719 gst_plugin_feature_list_free (res);
1721 GST_DEBUG ("Could not find an element for caps %" GST_PTR_FORMAT, caps);
1724 gst_caps_unref (caps);
1728 /* FIXME : VERY NAIVE. ASSUMING FIRST ONE WILL WORK */
1730 create_decoder (GstDecodebin3 * dbin, GstStream * stream)
1732 return create_element (dbin, stream, GST_ELEMENT_FACTORY_TYPE_DECODER);
1735 static GstPadProbeReturn
1736 keyframe_waiter_probe (GstPad * pad, GstPadProbeInfo * info,
1737 DecodebinOutputStream * output)
1739 GstBuffer *buf = GST_PAD_PROBE_INFO_BUFFER (info);
1740 /* If we have a keyframe, remove the probe and let all data through */
1741 /* FIXME : HANDLE HEADER BUFFER ?? */
1742 if (!GST_BUFFER_FLAG_IS_SET (buf, GST_BUFFER_FLAG_DELTA_UNIT) ||
1743 GST_BUFFER_FLAG_IS_SET (buf, GST_BUFFER_FLAG_HEADER)) {
1744 GST_DEBUG_OBJECT (pad,
1745 "Buffer is keyframe or header, letting through and removing probe");
1746 output->drop_probe_id = 0;
1747 return GST_PAD_PROBE_REMOVE;
1749 GST_DEBUG_OBJECT (pad, "Buffer is not a keyframe, dropping");
1750 return GST_PAD_PROBE_DROP;
1754 reconfigure_output_stream (DecodebinOutputStream * output,
1755 MultiQueueSlot * slot)
1757 GstDecodebin3 *dbin = output->dbin;
1758 GstCaps *new_caps = (GstCaps *) gst_stream_get_caps (slot->active_stream);
1759 gboolean needs_decoder;
1761 needs_decoder = gst_caps_can_intersect (new_caps, dbin->caps) != TRUE;
1763 GST_DEBUG_OBJECT (dbin,
1764 "Reconfiguring output %p to slot %p, needs_decoder:%d", output, slot,
1767 /* FIXME : Maybe make the output un-hook itself automatically ? */
1768 if (output->slot != NULL && output->slot != slot) {
1769 GST_WARNING_OBJECT (dbin,
1770 "Output still linked to another slot (%p)", output->slot);
1771 gst_caps_unref (new_caps);
1775 /* Check if existing config is reusable as-is by checking if
1776 * the existing decoder accepts the new caps, if not delete
1777 * it and create a new one */
1778 if (output->decoder) {
1779 gboolean can_reuse_decoder;
1781 if (needs_decoder) {
1783 gst_pad_query_accept_caps (output->decoder_sink, new_caps);
1785 can_reuse_decoder = FALSE;
1787 if (can_reuse_decoder) {
1788 if (output->type == GST_STREAM_TYPE_VIDEO && output->drop_probe_id == 0) {
1789 GST_DEBUG_OBJECT (dbin, "Adding keyframe-waiter probe");
1790 output->drop_probe_id =
1791 gst_pad_add_probe (slot->src_pad, GST_PAD_PROBE_TYPE_BUFFER,
1792 (GstPadProbeCallback) keyframe_waiter_probe, output, NULL);
1794 GST_DEBUG_OBJECT (dbin, "Reusing existing decoder for slot %p", slot);
1795 if (output->linked == FALSE) {
1796 gst_pad_link_full (slot->src_pad, output->decoder_sink,
1797 GST_PAD_LINK_CHECK_NOTHING);
1798 output->linked = TRUE;
1800 gst_caps_unref (new_caps);
1804 GST_DEBUG_OBJECT (dbin, "Removing old decoder for slot %p", slot);
1807 gst_pad_unlink (slot->src_pad, output->decoder_sink);
1808 output->linked = FALSE;
1809 if (output->drop_probe_id) {
1810 gst_pad_remove_probe (slot->src_pad, output->drop_probe_id);
1811 output->drop_probe_id = 0;
1814 if (!gst_ghost_pad_set_target ((GstGhostPad *) output->src_pad, NULL)) {
1815 GST_ERROR_OBJECT (dbin, "Could not release decoder pad");
1816 gst_caps_unref (new_caps);
1820 gst_object_replace ((GstObject **) & output->decoder_sink, NULL);
1821 gst_object_replace ((GstObject **) & output->decoder_src, NULL);
1823 gst_element_set_locked_state (output->decoder, TRUE);
1824 gst_element_set_state (output->decoder, GST_STATE_NULL);
1826 gst_bin_remove ((GstBin *) dbin, output->decoder);
1827 output->decoder = NULL;
1830 gst_caps_unref (new_caps);
1832 /* If a decoder is required, create one */
1833 if (needs_decoder) {
1834 /* If we don't have a decoder yet, instantiate one */
1835 output->decoder = create_decoder (dbin, slot->active_stream);
1836 if (output->decoder == NULL) {
1839 SELECTION_UNLOCK (dbin);
1840 /* FIXME : Should we be smarter if there's a missing decoder ?
1841 * Should we deactivate that stream ? */
1842 caps = gst_stream_get_caps (slot->active_stream);
1843 gst_element_post_message (GST_ELEMENT_CAST (dbin),
1844 gst_missing_decoder_message_new (GST_ELEMENT_CAST (dbin), caps));
1845 gst_caps_unref (caps);
1846 SELECTION_LOCK (dbin);
1849 if (!gst_bin_add ((GstBin *) dbin, output->decoder)) {
1850 GST_ERROR_OBJECT (dbin, "could not add decoder to pipeline");
1853 output->decoder_sink = gst_element_get_static_pad (output->decoder, "sink");
1854 output->decoder_src = gst_element_get_static_pad (output->decoder, "src");
1855 if (output->type == GST_STREAM_TYPE_VIDEO) {
1856 GST_DEBUG_OBJECT (dbin, "Adding keyframe-waiter probe");
1857 output->drop_probe_id =
1858 gst_pad_add_probe (slot->src_pad, GST_PAD_PROBE_TYPE_BUFFER,
1859 (GstPadProbeCallback) keyframe_waiter_probe, output, NULL);
1861 if (gst_pad_link_full (slot->src_pad, output->decoder_sink,
1862 GST_PAD_LINK_CHECK_NOTHING) != GST_PAD_LINK_OK) {
1863 GST_ERROR_OBJECT (dbin, "could not link to %s:%s",
1864 GST_DEBUG_PAD_NAME (output->decoder_sink));
1868 output->decoder_src = gst_object_ref (slot->src_pad);
1869 output->decoder_sink = NULL;
1871 output->linked = TRUE;
1872 if (!gst_ghost_pad_set_target ((GstGhostPad *) output->src_pad,
1873 output->decoder_src)) {
1874 GST_ERROR_OBJECT (dbin, "Could not expose decoder pad");
1877 if (output->src_exposed == FALSE) {
1878 output->src_exposed = TRUE;
1879 gst_element_add_pad (GST_ELEMENT_CAST (dbin), output->src_pad);
1882 if (output->decoder)
1883 gst_element_sync_state_with_parent (output->decoder);
1885 output->slot = slot;
1890 GST_DEBUG_OBJECT (dbin, "Cleanup");
1891 if (output->decoder_sink) {
1892 gst_object_unref (output->decoder_sink);
1893 output->decoder_sink = NULL;
1895 if (output->decoder_src) {
1896 gst_object_unref (output->decoder_src);
1897 output->decoder_src = NULL;
1899 if (output->decoder) {
1900 gst_element_set_state (output->decoder, GST_STATE_NULL);
1901 gst_bin_remove ((GstBin *) dbin, output->decoder);
1902 output->decoder = NULL;
1907 static GstPadProbeReturn
1908 idle_reconfigure (GstPad * pad, GstPadProbeInfo * info, MultiQueueSlot * slot)
1910 GstMessage *msg = NULL;
1911 DecodebinOutputStream *output;
1913 SELECTION_LOCK (slot->dbin);
1914 output = get_output_for_slot (slot);
1916 GST_DEBUG_OBJECT (pad, "output : %p", output);
1919 reconfigure_output_stream (output, slot);
1920 msg = is_selection_done (slot->dbin);
1922 SELECTION_UNLOCK (slot->dbin);
1924 gst_element_post_message ((GstElement *) slot->dbin, msg);
1926 return GST_PAD_PROBE_REMOVE;
1929 static MultiQueueSlot *
1930 find_slot_for_stream_id (GstDecodebin3 * dbin, const gchar * sid)
1934 for (tmp = dbin->slots; tmp; tmp = tmp->next) {
1935 MultiQueueSlot *slot = (MultiQueueSlot *) tmp->data;
1936 const gchar *stream_id;
1937 if (slot->active_stream) {
1938 stream_id = gst_stream_get_stream_id (slot->active_stream);
1939 if (!g_strcmp0 (sid, stream_id))
1942 if (slot->pending_stream && slot->pending_stream != slot->active_stream) {
1943 stream_id = gst_stream_get_stream_id (slot->pending_stream);
1944 if (!g_strcmp0 (sid, stream_id))
1952 /* This function handles the reassignment of a slot. Call this from
1953 * the streaming thread of a slot. */
1955 reassign_slot (GstDecodebin3 * dbin, MultiQueueSlot * slot)
1957 DecodebinOutputStream *output;
1958 MultiQueueSlot *target_slot = NULL;
1960 const gchar *sid, *tsid;
1962 SELECTION_LOCK (dbin);
1963 output = slot->output;
1965 if (G_UNLIKELY (slot->active_stream == NULL)) {
1966 GST_DEBUG_OBJECT (slot->src_pad,
1967 "Called on inactive slot (active_stream == NULL)");
1968 SELECTION_UNLOCK (dbin);
1972 if (G_UNLIKELY (output == NULL)) {
1973 GST_DEBUG_OBJECT (slot->src_pad,
1974 "Slot doesn't have any output to be removed");
1975 SELECTION_UNLOCK (dbin);
1979 sid = gst_stream_get_stream_id (slot->active_stream);
1980 GST_DEBUG_OBJECT (slot->src_pad, "slot %s %p", sid, slot);
1982 /* Recheck whether this stream is still in the list of streams to deactivate */
1983 if (stream_in_list (dbin->requested_selection, sid)) {
1984 /* Stream is in the list of requested streams, don't remove */
1985 SELECTION_UNLOCK (dbin);
1986 GST_DEBUG_OBJECT (slot->src_pad,
1987 "Stream '%s' doesn't need to be deactivated", sid);
1991 /* Unlink slot from output */
1992 /* FIXME : Handle flushing ? */
1993 /* FIXME : Handle outputs without decoders */
1994 GST_DEBUG_OBJECT (slot->src_pad, "Unlinking from decoder %p",
1995 output->decoder_sink);
1996 if (output->decoder_sink)
1997 gst_pad_unlink (slot->src_pad, output->decoder_sink);
1998 output->linked = FALSE;
1999 slot->output = NULL;
2000 output->slot = NULL;
2001 /* Remove sid from active selection */
2002 for (tmp = dbin->active_selection; tmp; tmp = tmp->next)
2003 if (!g_strcmp0 (sid, tmp->data)) {
2004 dbin->active_selection = g_list_delete_link (dbin->active_selection, tmp);
2008 /* Can we re-assign this output to a requested stream ? */
2009 GST_DEBUG_OBJECT (slot->src_pad, "Attempting to re-assing output stream");
2010 for (tmp = dbin->to_activate; tmp; tmp = tmp->next) {
2011 MultiQueueSlot *tslot = find_slot_for_stream_id (dbin, tmp->data);
2012 GST_LOG_OBJECT (tslot->src_pad, "Checking slot %p (output:%p , stream:%s)",
2013 tslot, tslot->output, gst_stream_get_stream_id (tslot->active_stream));
2014 if (tslot && tslot->type == output->type && tslot->output == NULL) {
2015 GST_DEBUG_OBJECT (tslot->src_pad, "Using as reassigned slot");
2016 target_slot = tslot;
2018 /* Pass target stream id to requested selection */
2019 dbin->requested_selection =
2020 g_list_append (dbin->requested_selection, tmp->data);
2021 dbin->to_activate = g_list_remove (dbin->to_activate, tmp->data);
2027 GST_DEBUG_OBJECT (slot->src_pad, "Assigning output to slot %p '%s'",
2029 target_slot->output = output;
2030 output->slot = target_slot;
2031 dbin->active_selection =
2032 g_list_append (dbin->active_selection, (gchar *) tsid);
2033 SELECTION_UNLOCK (dbin);
2035 /* Wakeup the target slot so that it retries to send events/buffers
2036 * thereby triggering the output reconfiguration codepath */
2037 gst_pad_add_probe (target_slot->src_pad, GST_PAD_PROBE_TYPE_IDLE,
2038 (GstPadProbeCallback) idle_reconfigure, target_slot, NULL);
2039 /* gst_pad_send_event (target_slot->src_pad, gst_event_new_reconfigure ()); */
2043 dbin->output_streams = g_list_remove (dbin->output_streams, output);
2044 free_output_stream (dbin, output);
2045 msg = is_selection_done (slot->dbin);
2046 SELECTION_UNLOCK (dbin);
2049 gst_element_post_message ((GstElement *) slot->dbin, msg);
2055 /* Idle probe called when a slot should be unassigned from its output stream.
2056 * This is needed to ensure nothing is flowing when unlinking the slot.
2058 * Also, this method will search for a pending stream which could re-use
2059 * the output stream. */
2060 static GstPadProbeReturn
2061 slot_unassign_probe (GstPad * pad, GstPadProbeInfo * info,
2062 MultiQueueSlot * slot)
2064 GstDecodebin3 *dbin = slot->dbin;
2066 reassign_slot (dbin, slot);
2068 return GST_PAD_PROBE_REMOVE;
2072 handle_stream_switch (GstDecodebin3 * dbin, GList * select_streams,
2075 gboolean ret = TRUE;
2077 /* List of slots to (de)activate. */
2078 GList *to_deactivate = NULL;
2079 GList *to_activate = NULL;
2080 /* List of unknown stream id, most likely means the event
2081 * should be sent upstream so that elements can expose the requested stream */
2082 GList *unknown = NULL;
2083 GList *to_reassign = NULL;
2084 GList *future_request_streams = NULL;
2085 GList *pending_streams = NULL;
2086 GList *slots_to_reassign = NULL;
2088 SELECTION_LOCK (dbin);
2089 if (G_UNLIKELY (seqnum != dbin->select_streams_seqnum)) {
2090 GST_DEBUG_OBJECT (dbin, "New SELECT_STREAMS has arrived in the meantime");
2091 SELECTION_UNLOCK (dbin);
2094 /* Remove pending select_streams */
2095 g_list_free (dbin->pending_select_streams);
2096 dbin->pending_select_streams = NULL;
2098 /* COMPARE the requested streams to the active and requested streams
2101 /* First check the slots to activate and which ones are unknown */
2102 for (tmp = select_streams; tmp; tmp = tmp->next) {
2103 const gchar *sid = (const gchar *) tmp->data;
2104 MultiQueueSlot *slot;
2105 GST_DEBUG_OBJECT (dbin, "Checking stream '%s'", sid);
2106 slot = find_slot_for_stream_id (dbin, sid);
2107 /* Find the corresponding slot */
2109 if (stream_in_collection (dbin, (gchar *) sid)) {
2110 pending_streams = g_list_append (pending_streams, (gchar *) sid);
2112 GST_DEBUG_OBJECT (dbin, "We don't have a slot for stream '%s'", sid);
2113 unknown = g_list_append (unknown, (gchar *) sid);
2115 } else if (slot->output == NULL) {
2116 GST_DEBUG_OBJECT (dbin, "We need to activate slot %p for stream '%s')",
2118 to_activate = g_list_append (to_activate, slot);
2120 GST_DEBUG_OBJECT (dbin,
2121 "Stream '%s' from slot %p is already active on output %p", sid, slot,
2123 future_request_streams =
2124 g_list_append (future_request_streams, (gchar *) sid);
2128 for (tmp = dbin->slots; tmp; tmp = tmp->next) {
2129 MultiQueueSlot *slot = (MultiQueueSlot *) tmp->data;
2130 /* For slots that have an output, check if it's part of the streams to
2133 gboolean slot_to_deactivate = TRUE;
2135 if (slot->active_stream) {
2136 if (stream_in_list (select_streams,
2137 gst_stream_get_stream_id (slot->active_stream)))
2138 slot_to_deactivate = FALSE;
2140 if (slot_to_deactivate && slot->pending_stream
2141 && slot->pending_stream != slot->active_stream) {
2142 if (stream_in_list (select_streams,
2143 gst_stream_get_stream_id (slot->pending_stream)))
2144 slot_to_deactivate = FALSE;
2146 if (slot_to_deactivate) {
2147 GST_DEBUG_OBJECT (dbin,
2148 "Slot %p (%s) should be deactivated, no longer used", slot,
2149 gst_stream_get_stream_id (slot->active_stream));
2150 to_deactivate = g_list_append (to_deactivate, slot);
2155 if (to_deactivate != NULL) {
2156 GST_DEBUG_OBJECT (dbin, "Check if we can reassign slots");
2157 /* We need to compare what needs to be activated and deactivated in order
2158 * to determine whether there are outputs that can be transferred */
2159 /* Take the stream-id of the slots that are to be activated, for which there
2160 * is a slot of the same type that needs to be deactivated */
2161 tmp = to_deactivate;
2163 MultiQueueSlot *slot_to_deactivate = (MultiQueueSlot *) tmp->data;
2164 gboolean removeit = FALSE;
2166 GST_DEBUG_OBJECT (dbin,
2167 "Checking if slot to deactivate (%p) has a candidate slot to activate",
2168 slot_to_deactivate);
2169 for (tmp2 = to_activate; tmp2; tmp2 = tmp2->next) {
2170 MultiQueueSlot *slot_to_activate = (MultiQueueSlot *) tmp2->data;
2171 GST_DEBUG_OBJECT (dbin, "Comparing to slot %p", slot_to_activate);
2172 if (slot_to_activate->type == slot_to_deactivate->type) {
2173 GST_DEBUG_OBJECT (dbin, "Re-using");
2174 to_reassign = g_list_append (to_reassign, (gchar *)
2175 gst_stream_get_stream_id (slot_to_activate->active_stream));
2177 g_list_append (slots_to_reassign, slot_to_deactivate);
2178 to_activate = g_list_remove (to_activate, slot_to_activate);
2185 to_deactivate = g_list_delete_link (to_deactivate, tmp);
2190 for (tmp = to_deactivate; tmp; tmp = tmp->next) {
2191 MultiQueueSlot *slot = (MultiQueueSlot *) tmp->data;
2192 GST_DEBUG_OBJECT (dbin,
2193 "Really need to deactivate slot %p, but no available alternative",
2196 slots_to_reassign = g_list_append (slots_to_reassign, slot);
2199 /* The only slots left to activate are the ones that won't be reassigned and
2200 * therefore really need to have a new output created */
2201 for (tmp = to_activate; tmp; tmp = tmp->next) {
2202 MultiQueueSlot *slot = (MultiQueueSlot *) tmp->data;
2203 if (slot->active_stream)
2204 future_request_streams =
2205 g_list_append (future_request_streams,
2206 (gchar *) gst_stream_get_stream_id (slot->active_stream));
2207 else if (slot->pending_stream)
2208 future_request_streams =
2209 g_list_append (future_request_streams,
2210 (gchar *) gst_stream_get_stream_id (slot->pending_stream));
2212 GST_ERROR_OBJECT (dbin, "No stream for slot %p !!", slot);
2215 if (to_activate == NULL && pending_streams != NULL) {
2216 GST_DEBUG_OBJECT (dbin, "Stream switch requested for future collection");
2217 if (dbin->requested_selection)
2218 g_list_free (dbin->requested_selection);
2219 dbin->requested_selection = select_streams;
2220 g_list_free (to_deactivate);
2221 g_list_free (pending_streams);
2222 to_deactivate = NULL;
2224 if (dbin->requested_selection)
2225 g_list_free (dbin->requested_selection);
2226 dbin->requested_selection = future_request_streams;
2227 dbin->requested_selection =
2228 g_list_concat (dbin->requested_selection, pending_streams);
2229 if (dbin->to_activate)
2230 g_list_free (dbin->to_activate);
2231 dbin->to_activate = to_reassign;
2234 dbin->selection_updated = TRUE;
2235 SELECTION_UNLOCK (dbin);
2238 GST_FIXME_OBJECT (dbin, "Got request for an unknown stream");
2240 /* For all streams to deactivate, add an idle probe where we will do
2241 * the unassignment and switch over */
2242 for (tmp = slots_to_reassign; tmp; tmp = tmp->next) {
2243 MultiQueueSlot *slot = (MultiQueueSlot *) tmp->data;
2244 gst_pad_add_probe (slot->src_pad, GST_PAD_PROBE_TYPE_IDLE,
2245 (GstPadProbeCallback) slot_unassign_probe, slot, NULL);
2251 static GstPadProbeReturn
2252 ghost_pad_event_probe (GstPad * pad, GstPadProbeInfo * info,
2253 DecodebinOutputStream * output)
2255 GstPadProbeReturn ret = GST_PAD_PROBE_OK;
2256 GstDecodebin3 *dbin = output->dbin;
2257 GstEvent *event = GST_PAD_PROBE_INFO_EVENT (info);
2259 GST_DEBUG_OBJECT (pad, "Got event %p %s", event, GST_EVENT_TYPE_NAME (event));
2261 switch (GST_EVENT_TYPE (event)) {
2262 case GST_EVENT_SELECT_STREAMS:
2265 GList *streams = NULL;
2266 guint32 seqnum = gst_event_get_seqnum (event);
2268 SELECTION_LOCK (dbin);
2269 if (seqnum == dbin->select_streams_seqnum) {
2270 SELECTION_UNLOCK (dbin);
2271 GST_DEBUG_OBJECT (pad,
2272 "Already handled/handling that SELECT_STREAMS event");
2275 dbin->select_streams_seqnum = seqnum;
2276 if (dbin->pending_select_streams != NULL) {
2277 GST_LOG_OBJECT (dbin, "Replacing pending select streams");
2278 g_list_free (dbin->pending_select_streams);
2279 dbin->pending_select_streams = NULL;
2281 gst_event_parse_select_streams (event, &streams);
2282 dbin->pending_select_streams = g_list_copy (streams);
2283 SELECTION_UNLOCK (dbin);
2285 /* Send event upstream */
2286 if ((peer = gst_pad_get_peer (pad))) {
2287 gst_pad_send_event (peer, event);
2288 gst_object_unref (peer);
2290 gst_event_unref (event);
2292 /* Finally handle the switch */
2294 handle_stream_switch (dbin, streams, seqnum);
2295 ret = GST_PAD_PROBE_HANDLED;
2306 gst_decodebin3_send_event (GstElement * element, GstEvent * event)
2308 GST_DEBUG_OBJECT (element, "event %s", GST_EVENT_TYPE_NAME (event));
2309 if (GST_EVENT_TYPE (event) == GST_EVENT_SELECT_STREAMS) {
2310 GstDecodebin3 *dbin = (GstDecodebin3 *) element;
2311 GList *streams = NULL;
2312 guint32 seqnum = gst_event_get_seqnum (event);
2314 SELECTION_LOCK (dbin);
2315 if (seqnum == dbin->select_streams_seqnum) {
2316 SELECTION_UNLOCK (dbin);
2317 GST_DEBUG_OBJECT (dbin,
2318 "Already handled/handling that SELECT_STREAMS event");
2321 dbin->select_streams_seqnum = seqnum;
2322 if (dbin->pending_select_streams != NULL) {
2323 GST_LOG_OBJECT (dbin, "Replacing pending select streams");
2324 g_list_free (dbin->pending_select_streams);
2325 dbin->pending_select_streams = NULL;
2327 gst_event_parse_select_streams (event, &streams);
2328 dbin->pending_select_streams = g_list_copy (streams);
2329 SELECTION_UNLOCK (dbin);
2331 /* FIXME : We don't have an upstream ?? */
2333 /* Send event upstream */
2334 if ((peer = gst_pad_get_peer (pad))) {
2335 gst_pad_send_event (peer, event);
2336 gst_object_unref (peer);
2339 /* Finally handle the switch */
2341 handle_stream_switch (dbin, streams, seqnum);
2343 gst_event_unref (event);
2346 return GST_ELEMENT_CLASS (parent_class)->send_event (element, event);
2351 free_multiqueue_slot (GstDecodebin3 * dbin, MultiQueueSlot * slot)
2354 gst_pad_remove_probe (slot->src_pad, slot->probe_id);
2356 if (slot->input->srcpad)
2357 gst_pad_unlink (slot->input->srcpad, slot->sink_pad);
2360 gst_element_release_request_pad (dbin->multiqueue, slot->sink_pad);
2361 gst_object_replace ((GstObject **) & slot->sink_pad, NULL);
2362 gst_object_replace ((GstObject **) & slot->src_pad, NULL);
2363 gst_object_replace ((GstObject **) & slot->active_stream, NULL);
2367 /* Create a DecodebinOutputStream for a given type
2368 * Note: It will be empty initially, it needs to be configured
2370 static DecodebinOutputStream *
2371 create_output_stream (GstDecodebin3 * dbin, GstStreamType type)
2373 DecodebinOutputStream *res = g_new0 (DecodebinOutputStream, 1);
2375 const gchar *prefix;
2376 GstStaticPadTemplate *templ;
2377 GstPadTemplate *ptmpl;
2379 GstPad *internal_pad;
2381 GST_DEBUG_OBJECT (dbin, "Created new output stream %p for type %s",
2382 res, gst_stream_type_get_name (type));
2388 case GST_STREAM_TYPE_VIDEO:
2389 templ = &video_src_template;
2390 counter = &dbin->vpadcount;
2393 case GST_STREAM_TYPE_AUDIO:
2394 templ = &audio_src_template;
2395 counter = &dbin->apadcount;
2398 case GST_STREAM_TYPE_TEXT:
2399 templ = &text_src_template;
2400 counter = &dbin->tpadcount;
2404 templ = &src_template;
2405 counter = &dbin->opadcount;
2410 pad_name = g_strdup_printf ("%s_%u", prefix, *counter);
2412 ptmpl = gst_static_pad_template_get (templ);
2413 res->src_pad = gst_ghost_pad_new_no_target_from_template (pad_name, ptmpl);
2414 gst_object_unref (ptmpl);
2416 gst_pad_set_active (res->src_pad, TRUE);
2417 /* Put an event probe on the internal proxy pad to detect upstream
2420 (GstPad *) gst_proxy_pad_get_internal ((GstProxyPad *) res->src_pad);
2421 gst_pad_add_probe (internal_pad, GST_PAD_PROBE_TYPE_EVENT_UPSTREAM,
2422 (GstPadProbeCallback) ghost_pad_event_probe, res, NULL);
2423 gst_object_unref (internal_pad);
2425 dbin->output_streams = g_list_append (dbin->output_streams, res);
2431 free_output_stream (GstDecodebin3 * dbin, DecodebinOutputStream * output)
2434 if (output->decoder_sink && output->decoder)
2435 gst_pad_unlink (output->slot->src_pad, output->decoder_sink);
2437 output->slot->output = NULL;
2438 output->slot = NULL;
2440 gst_object_replace ((GstObject **) & output->decoder_sink, NULL);
2441 gst_ghost_pad_set_target ((GstGhostPad *) output->src_pad, NULL);
2442 gst_object_replace ((GstObject **) & output->decoder_src, NULL);
2443 if (output->src_exposed) {
2444 gst_element_remove_pad ((GstElement *) dbin, output->src_pad);
2446 if (output->decoder) {
2447 gst_element_set_locked_state (output->decoder, TRUE);
2448 gst_element_set_state (output->decoder, GST_STATE_NULL);
2449 gst_bin_remove ((GstBin *) dbin, output->decoder);
2454 static GstStateChangeReturn
2455 gst_decodebin3_change_state (GstElement * element, GstStateChange transition)
2457 GstDecodebin3 *dbin = (GstDecodebin3 *) element;
2458 GstStateChangeReturn ret;
2461 switch (transition) {
2465 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
2466 if (ret == GST_STATE_CHANGE_FAILURE)
2469 switch (transition) {
2470 case GST_STATE_CHANGE_PAUSED_TO_READY:
2474 /* Free output streams */
2475 for (tmp = dbin->output_streams; tmp; tmp = tmp->next) {
2476 DecodebinOutputStream *output = (DecodebinOutputStream *) tmp->data;
2477 free_output_stream (dbin, output);
2479 g_list_free (dbin->output_streams);
2480 dbin->output_streams = NULL;
2481 /* Free multiqueue slots */
2482 for (tmp = dbin->slots; tmp; tmp = tmp->next) {
2483 MultiQueueSlot *slot = (MultiQueueSlot *) tmp->data;
2484 free_multiqueue_slot (dbin, slot);
2486 g_list_free (dbin->slots);
2499 gst_decodebin3_plugin_init (GstPlugin * plugin)
2501 GST_DEBUG_CATEGORY_INIT (decodebin3_debug, "decodebin3", 0, "decoder bin");
2503 return gst_element_register (plugin, "decodebin3", GST_RANK_NONE,
2504 GST_TYPE_DECODEBIN3);