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);
825 /* Call with INPUT_LOCK taken */
826 static DecodebinInput *
827 create_new_input (GstDecodebin3 * dbin, gboolean main)
829 DecodebinInput *input;
831 input = g_new0 (DecodebinInput, 1);
833 input->is_main = main;
834 input->group_id = G_MAXUINT32;
836 input->ghost_sink = gst_ghost_pad_new_no_target ("sink", GST_PAD_SINK);
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);
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);
847 gst_pad_set_active (input->ghost_sink, TRUE);
848 gst_element_add_pad ((GstElement *) dbin, input->ghost_sink);
855 gst_decodebin3_request_new_pad (GstElement * element, GstPadTemplate * temp,
856 const gchar * name, const GstCaps * caps)
858 GstDecodebin3 *dbin = (GstDecodebin3 *) element;
859 DecodebinInput *input;
862 /* We are ignoring names for the time being, not sure it makes any sense
863 * within the context of decodebin3 ... */
865 input = create_new_input (dbin, FALSE);
867 dbin->other_inputs = g_list_append (dbin->other_inputs, input);
868 res = input->ghost_sink;
875 /* Must be called with factories lock! */
877 gst_decode_bin_update_factories_list (GstDecodebin3 * dbin)
881 cookie = gst_registry_get_feature_list_cookie (gst_registry_get ());
882 if (!dbin->factories || dbin->factories_cookie != cookie) {
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);
891 gst_element_factory_list_get_elements
892 (GST_ELEMENT_FACTORY_TYPE_DECODABLE, GST_RANK_MARGINAL);
894 g_list_sort (dbin->factories, gst_plugin_feature_rank_compare_func);
895 dbin->factories_cookie = cookie;
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);
906 dbin->decodable_factories =
907 g_list_append (dbin->decodable_factories, fact);
912 /* Must be called with appropriate lock if list is a protected variable */
914 stream_in_list (GList * list, const gchar * sid)
919 for (tmp = list; tmp; tmp = tmp->next) {
920 gchar *osid = (gchar *) tmp->data;
921 GST_DEBUG ("Checking %s against %s", sid, osid);
925 for (tmp = list; tmp; tmp = tmp->next) {
926 gchar *osid = (gchar *) tmp->data;
927 if (!g_strcmp0 (sid, osid))
935 update_requested_selection (GstDecodebin3 * dbin,
936 GstStreamCollection * collection)
940 GstStreamType used_types = 0;
942 nb = gst_stream_collection_get_size (collection);
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");
953 /* 2. If not, are we in EXPOSE_ALL_MODE ? If so, match everything */
954 GST_FIXME_OBJECT (dbin, "Implement EXPOSE_ALL_MODE");
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);
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,
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);
973 GST_DEBUG_OBJECT (dbin,
974 "Using stream requested by 'select-stream' signal : %s", sid);
976 GST_DEBUG_OBJECT (dbin,
977 "Re-using stream already present in requested or active selection : %s",
979 tmp = g_list_append (tmp, (gchar *) sid);
980 used_types |= curtype;
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;
998 /* Finally set the requested selection */
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);
1005 dbin->requested_selection = tmp;
1006 dbin->selection_updated = TRUE;
1008 SELECTION_UNLOCK (dbin);
1011 /* Call with INPUT_LOCK taken */
1012 static GstStreamCollection *
1013 get_merged_collection (GstDecodebin3 * dbin)
1015 gboolean needs_merge = FALSE;
1016 GstStreamCollection *res = NULL;
1020 /* First check if we need to do a merge or just return the only collection */
1021 res = dbin->main_input->collection;
1023 for (tmp = dbin->other_inputs; tmp; tmp = tmp->next) {
1024 DecodebinInput *input = (DecodebinInput *) tmp->data;
1025 if (input->collection) {
1030 res = input->collection;
1035 GST_DEBUG_OBJECT (dbin, "No need to merge, returning %p", res);
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++) {
1047 gst_stream_collection_get_stream (dbin->main_input->collection, i);
1048 gst_stream_collection_add_stream (res, stream);
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,
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++) {
1061 gst_stream_collection_get_stream (input->collection, i);
1062 gst_stream_collection_add_stream (res, stream);
1070 /* Call with INPUT_LOCK taken */
1071 static DecodebinInput *
1072 find_message_parsebin (GstDecodebin3 * dbin, GstElement * child)
1074 DecodebinInput *input = NULL;
1075 GstElement *parent = gst_object_ref (child);
1079 GstElement *next_parent;
1081 GST_DEBUG_OBJECT (dbin, "parent %s",
1082 parent ? GST_ELEMENT_NAME (parent) : "<NONE>");
1084 if (parent == dbin->main_input->parsebin) {
1085 input = dbin->main_input;
1088 for (tmp = dbin->other_inputs; tmp; tmp = tmp->next) {
1089 DecodebinInput *cur = (DecodebinInput *) tmp->data;
1090 if (parent == cur->parsebin) {
1095 next_parent = (GstElement *) gst_element_get_parent (parent);
1096 gst_object_unref (parent);
1097 parent = next_parent;
1099 } while (parent && parent != (GstElement *) dbin);
1102 gst_object_unref (parent);
1108 stream_in_collection (GstDecodebin3 * dbin, gchar * sid)
1112 if (dbin->collection == NULL)
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))
1125 /* Call with INPUT_LOCK taken */
1127 handle_stream_collection (GstDecodebin3 * dbin,
1128 GstStreamCollection * collection, GstElement * child)
1130 #ifndef GST_DISABLE_GST_DEBUG
1131 const gchar *upstream_id;
1134 DecodebinInput *input = find_message_parsebin (dbin, child);
1137 GST_DEBUG_OBJECT (dbin,
1138 "Couldn't find corresponding input, most likely shutting down");
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,
1149 /* Merge collection if needed */
1150 collection = get_merged_collection (dbin);
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;
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);
1175 /* Store collection for later usage */
1176 if (dbin->collection == NULL) {
1177 dbin->collection = collection;
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); */
1194 gst_decodebin3_handle_message (GstBin * bin, GstMessage * message)
1196 GstDecodebin3 *dbin = (GstDecodebin3 *) bin;
1197 gboolean posting_collection = FALSE;
1199 GST_DEBUG_OBJECT (bin, "Got Message %s", GST_MESSAGE_TYPE_NAME (message));
1201 switch (GST_MESSAGE_TYPE (message)) {
1202 case GST_MESSAGE_STREAM_COLLECTION:
1204 GstStreamCollection *collection = NULL;
1205 gst_message_parse_stream_collection (message, &collection);
1208 handle_stream_collection (dbin, collection,
1209 (GstElement *) GST_MESSAGE_SRC (message));
1210 posting_collection = TRUE;
1211 INPUT_UNLOCK (dbin);
1213 if (dbin->collection && collection != dbin->collection) {
1214 /* Replace collection message, we most likely aggregated it */
1215 GstMessage *new_msg;
1217 gst_message_new_stream_collection ((GstObject *) dbin,
1219 gst_message_unref (message);
1223 gst_object_unref (collection);
1230 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
1232 if (posting_collection) {
1233 /* Figure out a selection for that collection */
1234 update_requested_selection (dbin, dbin->collection);
1238 static DecodebinOutputStream *
1239 find_free_compatible_output (GstDecodebin3 * dbin, GstStream * stream)
1242 GstStreamType stype = gst_stream_get_stream_type (stream);
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))) {
1258 /* Give a certain slot, figure out if it should be linked to an
1260 * CALL WITH SELECTION LOCK TAKEN !*/
1261 static DecodebinOutputStream *
1262 get_output_for_slot (MultiQueueSlot * slot)
1264 GstDecodebin3 *dbin = slot->dbin;
1265 DecodebinOutputStream *output = NULL;
1266 const gchar *stream_id;
1269 /* If we already have a configured output, just use it */
1270 if (slot->output != NULL)
1271 return slot->output;
1276 * This method needs to be split into multiple parts
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
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
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);
1293 /* 0. Emit autoplug-continue signal for pending caps ? */
1294 GST_FIXME_OBJECT (dbin, "emit autoplug-continue");
1296 /* 1. if in EXPOSE_ALL_MODE, just accept */
1297 GST_FIXME_OBJECT (dbin, "Handle EXPOSE_ALL_MODE");
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,
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);
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.
1321 * * an output stream whose slot->stream is not in requested
1322 * * and is of the same type as this stream
1324 output = find_free_compatible_output (dbin, slot->active_stream);
1326 /* Move this output from its current slot to this slot */
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);
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);
1345 GST_DEBUG ("Not creating any output for slot %p", slot);
1350 /* Returns SELECTED_STREAMS message if active_selection is equal to
1351 * requested_selection, else NULL.
1352 * Must be called with LOCK taken */
1354 is_selection_done (GstDecodebin3 * dbin)
1359 if (!dbin->selection_updated)
1362 GST_LOG_OBJECT (dbin, "Checking");
1364 if (dbin->to_activate != NULL) {
1365 GST_DEBUG ("Still have streams to activate");
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");
1376 GST_DEBUG_OBJECT (dbin, "Selection active, creating message");
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;
1384 GST_DEBUG_OBJECT (dbin, "Adding stream %s",
1385 gst_stream_get_stream_id (output->slot->active_stream));
1387 gst_message_streams_selected_add (msg, output->slot->active_stream);
1389 GST_WARNING_OBJECT (dbin, "No valid slot for output %p", output);
1391 dbin->selection_updated = FALSE;
1395 static GstPadProbeReturn
1396 multiqueue_src_probe (GstPad * pad, GstPadProbeInfo * info,
1397 MultiQueueSlot * slot)
1399 GstPadProbeReturn ret = GST_PAD_PROBE_OK;
1400 GstDecodebin3 *dbin = slot->dbin;
1402 if (GST_IS_EVENT (GST_PAD_PROBE_INFO_DATA (info))) {
1403 GstEvent *ev = GST_PAD_PROBE_INFO_EVENT (info);
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:
1409 GstStream *stream = NULL;
1410 const gchar *stream_id;
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");
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;
1429 gst_object_unref (stream);
1430 #if 0 /* Disabled because stream-start is pushed for every buffer on every unlinked pad */
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);
1442 GST_DEBUG_OBJECT (pad, "Slot in ACTIVE selection (output:%p)",
1445 GST_DEBUG_OBJECT (pad, "Slot in REQUESTED selection (output:%p)",
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");
1457 case GST_EVENT_CAPS:
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);
1465 reconfigure_output_stream (output, slot);
1466 msg = is_selection_done (dbin);
1468 SELECTION_UNLOCK (dbin);
1470 gst_element_post_message ((GstElement *) slot->dbin, msg);
1474 /* FIXME : Figure out */
1475 GST_FIXME_OBJECT (pad, "EOS on multiqueue source pad. input:%p",
1477 if (slot->input == NULL) {
1479 GST_DEBUG_OBJECT (pad,
1480 "last EOS for input, forwarding and removing slot");
1481 peer = gst_pad_get_peer (pad);
1483 gst_pad_send_event (peer, ev);
1484 gst_object_unref (peer);
1486 gst_event_unref (ev);
1488 SELECTION_LOCK (dbin);
1489 /* FIXME : Shouldn't we try to re-assign the output instead of just
1491 /* Remove the output */
1493 DecodebinOutputStream *output = slot->output;
1494 dbin->output_streams = g_list_remove (dbin->output_streams, output);
1495 free_output_stream (dbin, output);
1497 SELECTION_UNLOCK (dbin);
1498 ret = GST_PAD_PROBE_HANDLED;
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:
1509 GST_DEBUG_OBJECT (pad, "Intercepting CAPS query");
1510 gst_query_set_caps_result (query, GST_CAPS_ANY);
1511 ret = GST_PAD_PROBE_HANDLED;
1515 case GST_QUERY_ACCEPT_CAPS:
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;
1531 /* Create a new multiqueue slot for the given type
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)
1538 MultiQueueSlot *slot;
1539 GstIterator *it = NULL;
1540 GValue item = { 0, };
1542 GST_DEBUG_OBJECT (dbin, "Creating new slot for type %s",
1543 gst_stream_type_get_name (type));
1544 slot = g_new0 (MultiQueueSlot, 1);
1546 slot->id = dbin->slot_id++;
1548 slot->sink_pad = gst_element_get_request_pad (dbin->multiqueue, "sink_%u");
1549 if (slot->sink_pad == NULL)
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));
1558 gst_iterator_free (it);
1559 g_value_reset (&item);
1561 g_object_set (slot->sink_pad, "group-id", (guint) type, NULL);
1563 /* Add event probe */
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);
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);
1578 gst_element_release_request_pad (dbin->multiqueue, slot->sink_pad);
1584 static MultiQueueSlot *
1585 get_slot_for_input (GstDecodebin3 * dbin, DecodebinInputStream * input)
1588 MultiQueueSlot *empty_slot = NULL;
1589 GstStreamType input_type = 0;
1590 gchar *stream_id = NULL;
1592 GST_DEBUG_OBJECT (dbin, "input %p (stream %p %s)",
1593 input, input->active_stream,
1595 active_stream ? gst_stream_get_stream_id (input->active_stream) : "");
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);
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);
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 */
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) {
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))
1633 GST_DEBUG_OBJECT (dbin, "Re-using existing unused slot %d", empty_slot->id);
1634 empty_slot->input = input;
1639 return create_new_slot (dbin, input_type);
1645 link_input_to_slot (DecodebinInputStream * input, MultiQueueSlot * slot)
1648 if (slot->input != NULL && slot->input != input) {
1649 GST_ERROR_OBJECT (slot->dbin,
1650 "Trying to link input to an already used slot");
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);
1658 gst_pad_send_event (slot->sink_pad, event);
1663 have_factory (GstDecodebin3 * dbin, GstCaps * caps,
1664 GstElementFactoryListType ftype)
1666 gboolean ret = FALSE;
1669 g_mutex_lock (&dbin->factories_lock);
1670 gst_decode_bin_update_factories_list (dbin);
1671 if (ftype == GST_ELEMENT_FACTORY_TYPE_DECODER)
1673 gst_element_factory_list_filter (dbin->decoder_factories,
1674 caps, GST_PAD_SINK, TRUE);
1677 gst_element_factory_list_filter (dbin->decodable_factories,
1678 caps, GST_PAD_SINK, TRUE);
1679 g_mutex_unlock (&dbin->factories_lock);
1683 gst_plugin_feature_list_free (res);
1691 create_element (GstDecodebin3 * dbin, GstStream * stream,
1692 GstElementFactoryListType ftype)
1695 GstElement *element = NULL;
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)
1703 gst_element_factory_list_filter (dbin->decoder_factories,
1704 caps, GST_PAD_SINK, TRUE);
1707 gst_element_factory_list_filter (dbin->decodable_factories,
1708 caps, GST_PAD_SINK, TRUE);
1709 g_mutex_unlock (&dbin->factories_lock);
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);
1717 GST_DEBUG ("Could not find an element for caps %" GST_PTR_FORMAT, caps);
1720 gst_caps_unref (caps);
1724 /* FIXME : VERY NAIVE. ASSUMING FIRST ONE WILL WORK */
1726 create_decoder (GstDecodebin3 * dbin, GstStream * stream)
1728 return create_element (dbin, stream, GST_ELEMENT_FACTORY_TYPE_DECODER);
1731 static GstPadProbeReturn
1732 keyframe_waiter_probe (GstPad * pad, GstPadProbeInfo * info,
1733 DecodebinOutputStream * output)
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;
1745 GST_DEBUG_OBJECT (pad, "Buffer is not a keyframe, dropping");
1746 return GST_PAD_PROBE_DROP;
1750 reconfigure_output_stream (DecodebinOutputStream * output,
1751 MultiQueueSlot * slot)
1753 GstDecodebin3 *dbin = output->dbin;
1754 GstCaps *new_caps = (GstCaps *) gst_stream_get_caps (slot->active_stream);
1755 gboolean needs_decoder;
1757 needs_decoder = gst_caps_can_intersect (new_caps, dbin->caps) != TRUE;
1759 GST_DEBUG_OBJECT (dbin,
1760 "Reconfiguring output %p to slot %p, needs_decoder:%d", output, slot,
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);
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;
1777 if (needs_decoder) {
1779 gst_pad_query_accept_caps (output->decoder_sink, new_caps);
1781 can_reuse_decoder = FALSE;
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);
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;
1796 gst_caps_unref (new_caps);
1800 GST_DEBUG_OBJECT (dbin, "Removing old decoder for slot %p", slot);
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;
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);
1816 gst_object_replace ((GstObject **) & output->decoder_sink, NULL);
1817 gst_object_replace ((GstObject **) & output->decoder_src, NULL);
1819 gst_element_set_locked_state (output->decoder, TRUE);
1820 gst_element_set_state (output->decoder, GST_STATE_NULL);
1822 gst_bin_remove ((GstBin *) dbin, output->decoder);
1823 output->decoder = NULL;
1826 gst_caps_unref (new_caps);
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) {
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);
1845 if (!gst_bin_add ((GstBin *) dbin, output->decoder)) {
1846 GST_ERROR_OBJECT (dbin, "could not add decoder to pipeline");
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);
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));
1864 output->decoder_src = gst_object_ref (slot->src_pad);
1865 output->decoder_sink = NULL;
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");
1873 if (output->src_exposed == FALSE) {
1874 output->src_exposed = TRUE;
1875 gst_element_add_pad (GST_ELEMENT_CAST (dbin), output->src_pad);
1878 if (output->decoder)
1879 gst_element_sync_state_with_parent (output->decoder);
1881 output->slot = slot;
1886 GST_DEBUG_OBJECT (dbin, "Cleanup");
1887 if (output->decoder_sink) {
1888 gst_object_unref (output->decoder_sink);
1889 output->decoder_sink = NULL;
1891 if (output->decoder_src) {
1892 gst_object_unref (output->decoder_src);
1893 output->decoder_src = NULL;
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;
1903 static GstPadProbeReturn
1904 idle_reconfigure (GstPad * pad, GstPadProbeInfo * info, MultiQueueSlot * slot)
1906 GstMessage *msg = NULL;
1907 DecodebinOutputStream *output;
1909 SELECTION_LOCK (slot->dbin);
1910 output = get_output_for_slot (slot);
1912 GST_DEBUG_OBJECT (pad, "output : %p", output);
1915 reconfigure_output_stream (output, slot);
1916 msg = is_selection_done (slot->dbin);
1918 SELECTION_UNLOCK (slot->dbin);
1920 gst_element_post_message ((GstElement *) slot->dbin, msg);
1922 return GST_PAD_PROBE_REMOVE;
1925 static MultiQueueSlot *
1926 find_slot_for_stream_id (GstDecodebin3 * dbin, const gchar * sid)
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))
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))
1948 /* This function handles the reassignment of a slot. Call this from
1949 * the streaming thread of a slot. */
1951 reassign_slot (GstDecodebin3 * dbin, MultiQueueSlot * slot)
1953 DecodebinOutputStream *output;
1954 MultiQueueSlot *target_slot = NULL;
1956 const gchar *sid, *tsid;
1958 SELECTION_LOCK (dbin);
1959 output = slot->output;
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);
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);
1975 sid = gst_stream_get_stream_id (slot->active_stream);
1976 GST_DEBUG_OBJECT (slot->src_pad, "slot %s %p", sid, slot);
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);
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);
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;
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);
2023 GST_DEBUG_OBJECT (slot->src_pad, "Assigning output to slot %p '%s'",
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);
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 ()); */
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 ?");
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.
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)
2055 GstDecodebin3 *dbin = slot->dbin;
2057 reassign_slot (dbin, slot);
2059 return GST_PAD_PROBE_REMOVE;
2063 handle_stream_switch (GstDecodebin3 * dbin, GList * select_streams,
2066 gboolean ret = TRUE;
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;
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);
2085 /* Remove pending select_streams */
2086 g_list_free (dbin->pending_select_streams);
2087 dbin->pending_select_streams = NULL;
2089 /* COMPARE the requested streams to the active and requested streams
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 */
2100 if (stream_in_collection (dbin, (gchar *) sid)) {
2101 pending_streams = g_list_append (pending_streams, (gchar *) sid);
2103 GST_DEBUG_OBJECT (dbin, "We don't have a slot for stream '%s'", sid);
2104 unknown = g_list_append (unknown, (gchar *) sid);
2106 } else if (slot->output == NULL) {
2107 GST_DEBUG_OBJECT (dbin, "We need to activate slot %p for stream '%s')",
2109 to_activate = g_list_append (to_activate, slot);
2111 GST_DEBUG_OBJECT (dbin,
2112 "Stream '%s' from slot %p is already active on output %p", sid, slot,
2114 future_request_streams =
2115 g_list_append (future_request_streams, (gchar *) sid);
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
2124 gboolean slot_to_deactivate = TRUE;
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;
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;
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);
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;
2154 MultiQueueSlot *slot_to_deactivate = (MultiQueueSlot *) tmp->data;
2155 gboolean removeit = FALSE;
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));
2168 g_list_append (slots_to_reassign, slot_to_deactivate);
2169 to_activate = g_list_remove (to_activate, slot_to_activate);
2176 to_deactivate = g_list_delete_link (to_deactivate, tmp);
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",
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));
2201 GST_ERROR_OBJECT (dbin, "No stream for slot %p !!", slot);
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;
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;
2223 dbin->selection_updated = TRUE;
2224 SELECTION_UNLOCK (dbin);
2227 GST_FIXME_OBJECT (dbin, "Got request for an unknown stream");
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);
2240 static GstPadProbeReturn
2241 ghost_pad_event_probe (GstPad * pad, GstPadProbeInfo * info,
2242 DecodebinOutputStream * output)
2244 GstPadProbeReturn ret = GST_PAD_PROBE_OK;
2245 GstDecodebin3 *dbin = output->dbin;
2246 GstEvent *event = GST_PAD_PROBE_INFO_EVENT (info);
2248 GST_DEBUG_OBJECT (pad, "Got event %p %s", event, GST_EVENT_TYPE_NAME (event));
2250 switch (GST_EVENT_TYPE (event)) {
2251 case GST_EVENT_SELECT_STREAMS:
2254 GList *streams = NULL;
2255 guint32 seqnum = gst_event_get_seqnum (event);
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");
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;
2270 gst_event_parse_select_streams (event, &streams);
2271 dbin->pending_select_streams = g_list_copy (streams);
2272 SELECTION_UNLOCK (dbin);
2274 /* Send event upstream */
2275 if ((peer = gst_pad_get_peer (pad))) {
2276 gst_pad_send_event (peer, event);
2277 gst_object_unref (peer);
2279 gst_event_unref (event);
2281 /* Finally handle the switch */
2283 handle_stream_switch (dbin, streams, seqnum);
2284 ret = GST_PAD_PROBE_HANDLED;
2295 gst_decodebin3_send_event (GstElement * element, GstEvent * event)
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);
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");
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;
2316 gst_event_parse_select_streams (event, &streams);
2317 dbin->pending_select_streams = g_list_copy (streams);
2318 SELECTION_UNLOCK (dbin);
2320 /* FIXME : We don't have an upstream ?? */
2322 /* Send event upstream */
2323 if ((peer = gst_pad_get_peer (pad))) {
2324 gst_pad_send_event (peer, event);
2325 gst_object_unref (peer);
2328 /* Finally handle the switch */
2330 handle_stream_switch (dbin, streams, seqnum);
2334 return GST_ELEMENT_CLASS (parent_class)->send_event (element, event);
2339 free_multiqueue_slot (GstDecodebin3 * dbin, MultiQueueSlot * slot)
2342 gst_pad_remove_probe (slot->src_pad, slot->probe_id);
2344 if (slot->input->srcpad)
2345 gst_pad_unlink (slot->input->srcpad, slot->sink_pad);
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);
2355 /* Create a DecodebinOutputStream for a given type
2356 * Note: It will be empty initially, it needs to be configured
2358 static DecodebinOutputStream *
2359 create_output_stream (GstDecodebin3 * dbin, GstStreamType type)
2361 DecodebinOutputStream *res = g_new0 (DecodebinOutputStream, 1);
2363 const gchar *prefix;
2364 GstStaticPadTemplate *templ;
2365 GstPadTemplate *ptmpl;
2367 GstPad *internal_pad;
2369 GST_DEBUG_OBJECT (dbin, "Created new output stream %p for type %s",
2370 res, gst_stream_type_get_name (type));
2376 case GST_STREAM_TYPE_VIDEO:
2377 templ = &video_src_template;
2378 counter = &dbin->vpadcount;
2381 case GST_STREAM_TYPE_AUDIO:
2382 templ = &audio_src_template;
2383 counter = &dbin->apadcount;
2386 case GST_STREAM_TYPE_TEXT:
2387 templ = &text_src_template;
2388 counter = &dbin->tpadcount;
2392 templ = &src_template;
2393 counter = &dbin->opadcount;
2398 pad_name = g_strdup_printf ("%s_%u", prefix, *counter);
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);
2404 gst_pad_set_active (res->src_pad, TRUE);
2405 /* Put an event probe on the internal proxy pad to detect upstream
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);
2413 dbin->output_streams = g_list_append (dbin->output_streams, res);
2419 free_output_stream (GstDecodebin3 * dbin, DecodebinOutputStream * output)
2422 if (output->decoder_sink && output->decoder)
2423 gst_pad_unlink (output->slot->src_pad, output->decoder_sink);
2425 output->slot->output = NULL;
2426 output->slot = NULL;
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);
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);
2442 static GstStateChangeReturn
2443 gst_decodebin3_change_state (GstElement * element, GstStateChange transition)
2445 GstDecodebin3 *dbin = (GstDecodebin3 *) element;
2446 GstStateChangeReturn ret;
2449 switch (transition) {
2453 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
2454 if (ret == GST_STATE_CHANGE_FAILURE)
2457 switch (transition) {
2458 case GST_STATE_CHANGE_PAUSED_TO_READY:
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);
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);
2474 g_list_free (dbin->slots);
2487 gst_decodebin3_plugin_init (GstPlugin * plugin)
2489 GST_DEBUG_CATEGORY_INIT (decodebin3_debug, "decodebin3", 0, "decoder bin");
2491 return gst_element_register (plugin, "decodebin3", GST_RANK_NONE,
2492 GST_TYPE_DECODEBIN3);