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);
606 GST_OBJECT_FLAG_SET (dbin, GST_BIN_FLAG_STREAMS_AWARE);
610 gst_decodebin3_dispose (GObject * object)
612 GstDecodebin3 *dbin = (GstDecodebin3 *) object;
616 gst_plugin_feature_list_free (dbin->factories);
617 if (dbin->decoder_factories)
618 g_list_free (dbin->decoder_factories);
619 if (dbin->decodable_factories)
620 g_list_free (dbin->decodable_factories);
621 g_list_free (dbin->requested_selection);
622 g_list_free (dbin->active_selection);
623 g_list_free (dbin->to_activate);
624 g_list_free (dbin->pending_select_streams);
625 g_clear_object (&dbin->collection);
627 free_input (dbin, dbin->main_input);
629 for (walk = dbin->other_inputs; walk; walk = next) {
630 DecodebinInput *input = walk->data;
632 next = g_list_next (walk);
634 free_input (dbin, input);
635 dbin->other_inputs = g_list_delete_link (dbin->other_inputs, walk);
638 G_OBJECT_CLASS (parent_class)->dispose (object);
642 gst_decodebin3_set_property (GObject * object, guint prop_id,
643 const GValue * value, GParamSpec * pspec)
645 GstDecodebin3 *dbin = (GstDecodebin3 *) object;
647 /* FIXME : IMPLEMENT */
650 GST_OBJECT_LOCK (dbin);
652 gst_caps_unref (dbin->caps);
653 dbin->caps = g_value_dup_boxed (value);
654 GST_OBJECT_UNLOCK (dbin);
657 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
663 gst_decodebin3_get_property (GObject * object, guint prop_id, GValue * value,
666 GstDecodebin3 *dbin = (GstDecodebin3 *) object;
668 /* FIXME : IMPLEMENT */
671 GST_OBJECT_LOCK (dbin);
672 g_value_set_boxed (value, dbin->caps);
673 GST_OBJECT_UNLOCK (dbin);
676 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
682 parsebin_autoplug_continue_cb (GstElement * parsebin, GstPad * pad,
683 GstCaps * caps, GstDecodebin3 * dbin)
685 GST_DEBUG_OBJECT (pad, "caps %" GST_PTR_FORMAT, caps);
687 /* If it matches our target caps, expose it */
688 if (gst_caps_can_intersect (caps, dbin->caps))
694 /* This method should be called whenever a STREAM_START event
695 * comes out of a given parsebin.
696 * The caller shall replace the group_id if the function returns TRUE */
698 set_input_group_id (DecodebinInput * input, guint32 * group_id)
700 GstDecodebin3 *dbin = input->dbin;
702 if (input->group_id != *group_id) {
703 if (input->group_id != G_MAXUINT32)
704 GST_WARNING_OBJECT (dbin,
705 "Group id changed (%" G_GUINT32_FORMAT " -> %" G_GUINT32_FORMAT
706 ") on input %p ", input->group_id, *group_id, input);
707 input->group_id = *group_id;
710 if (*group_id != dbin->current_group_id) {
711 if (dbin->current_group_id == G_MAXUINT32) {
712 GST_DEBUG_OBJECT (dbin, "Setting current group id to %" G_GUINT32_FORMAT,
714 dbin->current_group_id = *group_id;
716 *group_id = dbin->current_group_id;
723 /* Call with INPUT_LOCK taken */
725 ensure_input_parsebin (GstDecodebin3 * dbin, DecodebinInput * input)
727 gboolean set_state = FALSE;
729 if (input->parsebin == NULL) {
730 input->parsebin = gst_element_factory_make ("parsebin", NULL);
731 if (input->parsebin == NULL)
733 input->parsebin = gst_object_ref (input->parsebin);
734 input->parsebin_sink = gst_element_get_static_pad (input->parsebin, "sink");
735 input->pad_added_sigid =
736 g_signal_connect (input->parsebin, "pad-added",
737 (GCallback) parsebin_pad_added_cb, input);
738 input->pad_removed_sigid =
739 g_signal_connect (input->parsebin, "pad-removed",
740 (GCallback) parsebin_pad_removed_cb, input);
741 g_signal_connect (input->parsebin, "autoplug-continue",
742 (GCallback) parsebin_autoplug_continue_cb, dbin);
745 if (GST_OBJECT_PARENT (GST_OBJECT (input->parsebin)) != GST_OBJECT (dbin)) {
746 gst_bin_add (GST_BIN (dbin), input->parsebin);
750 gst_ghost_pad_set_target (GST_GHOST_PAD (input->ghost_sink),
751 input->parsebin_sink);
753 gst_element_sync_state_with_parent (input->parsebin);
760 gst_element_post_message ((GstElement *) dbin,
761 gst_missing_element_message_new ((GstElement *) dbin, "parsebin"));
766 static GstPadLinkReturn
767 gst_decodebin3_input_pad_link (GstPad * pad, GstObject * parent, GstPad * peer)
769 GstDecodebin3 *dbin = (GstDecodebin3 *) parent;
770 GstPadLinkReturn res = GST_PAD_LINK_OK;
771 DecodebinInput *input;
773 GST_LOG_OBJECT (parent, "Got link on input pad %" GST_PTR_FORMAT
774 ". Creating parsebin if needed", pad);
776 if ((input = g_object_get_data (G_OBJECT (pad), "decodebin.input")) == NULL)
780 if (!ensure_input_parsebin (dbin, input))
781 res = GST_PAD_LINK_REFUSED;
786 GST_ERROR_OBJECT (parent, "Failed to retrieve input state from ghost pad");
787 return GST_PAD_LINK_REFUSED;
791 gst_decodebin3_input_pad_unlink (GstPad * pad, GstObject * parent)
793 GstDecodebin3 *dbin = (GstDecodebin3 *) parent;
794 DecodebinInput *input;
796 GST_LOG_OBJECT (parent, "Got unlink on input pad %" GST_PTR_FORMAT
797 ". Removing parsebin.", pad);
799 if ((input = g_object_get_data (G_OBJECT (pad), "decodebin.input")) == NULL)
803 if (input->parsebin == NULL) {
808 if (GST_OBJECT_PARENT (GST_OBJECT (input->parsebin)) == GST_OBJECT (dbin)) {
809 gst_bin_remove (GST_BIN (dbin), input->parsebin);
810 gst_element_set_state (input->parsebin, GST_STATE_NULL);
816 GST_ERROR_OBJECT (parent, "Failed to retrieve input state from ghost pad");
821 free_input (GstDecodebin3 * dbin, DecodebinInput * input)
823 GST_DEBUG ("Freeing input %p", input);
824 gst_ghost_pad_set_target (GST_GHOST_PAD (input->ghost_sink), NULL);
825 gst_element_remove_pad (GST_ELEMENT (dbin), input->ghost_sink);
826 if (input->parsebin) {
827 g_signal_handler_disconnect (input->parsebin, input->pad_removed_sigid);
828 g_signal_handler_disconnect (input->parsebin, input->pad_added_sigid);
829 gst_element_set_state (input->parsebin, GST_STATE_NULL);
830 gst_object_unref (input->parsebin);
831 gst_object_unref (input->parsebin_sink);
833 if (input->collection)
834 gst_object_unref (input->collection);
838 /* Call with INPUT_LOCK taken */
839 static DecodebinInput *
840 create_new_input (GstDecodebin3 * dbin, gboolean main)
842 DecodebinInput *input;
844 input = g_new0 (DecodebinInput, 1);
846 input->is_main = main;
847 input->group_id = G_MAXUINT32;
849 input->ghost_sink = gst_ghost_pad_new_no_target ("sink", GST_PAD_SINK);
851 gchar *pad_name = g_strdup_printf ("sink_%u", dbin->input_counter++);
852 input->ghost_sink = gst_ghost_pad_new_no_target (pad_name, GST_PAD_SINK);
855 g_object_set_data (G_OBJECT (input->ghost_sink), "decodebin.input", input);
856 gst_pad_set_link_function (input->ghost_sink, gst_decodebin3_input_pad_link);
857 gst_pad_set_unlink_function (input->ghost_sink,
858 gst_decodebin3_input_pad_unlink);
860 gst_pad_set_active (input->ghost_sink, TRUE);
861 gst_element_add_pad ((GstElement *) dbin, input->ghost_sink);
868 gst_decodebin3_request_new_pad (GstElement * element, GstPadTemplate * temp,
869 const gchar * name, const GstCaps * caps)
871 GstDecodebin3 *dbin = (GstDecodebin3 *) element;
872 DecodebinInput *input;
875 /* We are ignoring names for the time being, not sure it makes any sense
876 * within the context of decodebin3 ... */
878 input = create_new_input (dbin, FALSE);
880 dbin->other_inputs = g_list_append (dbin->other_inputs, input);
881 res = input->ghost_sink;
888 /* Must be called with factories lock! */
890 gst_decode_bin_update_factories_list (GstDecodebin3 * dbin)
894 cookie = gst_registry_get_feature_list_cookie (gst_registry_get ());
895 if (!dbin->factories || dbin->factories_cookie != cookie) {
898 gst_plugin_feature_list_free (dbin->factories);
899 if (dbin->decoder_factories)
900 g_list_free (dbin->decoder_factories);
901 if (dbin->decodable_factories)
902 g_list_free (dbin->decodable_factories);
904 gst_element_factory_list_get_elements
905 (GST_ELEMENT_FACTORY_TYPE_DECODABLE, GST_RANK_MARGINAL);
907 g_list_sort (dbin->factories, gst_plugin_feature_rank_compare_func);
908 dbin->factories_cookie = cookie;
910 /* Filter decoder and other decodables */
911 dbin->decoder_factories = NULL;
912 dbin->decodable_factories = NULL;
913 for (tmp = dbin->factories; tmp; tmp = tmp->next) {
914 GstElementFactory *fact = (GstElementFactory *) tmp->data;
915 if (gst_element_factory_list_is_type (fact,
916 GST_ELEMENT_FACTORY_TYPE_DECODER))
917 dbin->decoder_factories = g_list_append (dbin->decoder_factories, fact);
919 dbin->decodable_factories =
920 g_list_append (dbin->decodable_factories, fact);
925 /* Must be called with appropriate lock if list is a protected variable */
927 stream_in_list (GList * list, const gchar * sid)
932 for (tmp = list; tmp; tmp = tmp->next) {
933 gchar *osid = (gchar *) tmp->data;
934 GST_DEBUG ("Checking %s against %s", sid, osid);
938 for (tmp = list; tmp; tmp = tmp->next) {
939 gchar *osid = (gchar *) tmp->data;
940 if (!g_strcmp0 (sid, osid))
948 update_requested_selection (GstDecodebin3 * dbin,
949 GstStreamCollection * collection)
953 GstStreamType used_types = 0;
955 nb = gst_stream_collection_get_size (collection);
957 /* 1. Is there a pending SELECT_STREAMS we can return straight away since
958 * the switch handler will take care of the pending selection */
959 SELECTION_LOCK (dbin);
960 if (dbin->pending_select_streams) {
961 GST_DEBUG_OBJECT (dbin,
962 "No need to create pending selection, SELECT_STREAMS underway");
966 /* 2. If not, are we in EXPOSE_ALL_MODE ? If so, match everything */
967 GST_FIXME_OBJECT (dbin, "Implement EXPOSE_ALL_MODE");
969 /* 3. If not, check if we already have some of the streams in the
970 * existing active/requested selection */
971 for (i = 0; i < nb; i++) {
972 GstStream *stream = gst_stream_collection_get_stream (collection, i);
973 const gchar *sid = gst_stream_get_stream_id (stream);
975 /* Fire select-stream signal to see if outside components want to
976 * hint at which streams should be selected */
977 g_signal_emit (G_OBJECT (dbin),
978 gst_decodebin3_signals[SIGNAL_SELECT_STREAM], 0, collection, stream,
980 GST_DEBUG_OBJECT (dbin, "stream %s , request:%d", sid, request);
981 if (request == 1 || (request == -1
982 && (stream_in_list (dbin->requested_selection, sid)
983 || stream_in_list (dbin->active_selection, sid)))) {
984 GstStreamType curtype = gst_stream_get_stream_type (stream);
986 GST_DEBUG_OBJECT (dbin,
987 "Using stream requested by 'select-stream' signal : %s", sid);
989 GST_DEBUG_OBJECT (dbin,
990 "Re-using stream already present in requested or active selection : %s",
992 tmp = g_list_append (tmp, (gchar *) sid);
993 used_types |= curtype;
997 /* 4. If not, match one stream of each type */
998 for (i = 0; i < nb; i++) {
999 GstStream *stream = gst_stream_collection_get_stream (collection, i);
1000 GstStreamType curtype = gst_stream_get_stream_type (stream);
1001 if (!(used_types & curtype)) {
1002 const gchar *sid = gst_stream_get_stream_id (stream);
1003 GST_DEBUG_OBJECT (dbin, "Selecting stream '%s' of type %s",
1004 sid, gst_stream_type_get_name (curtype));
1005 tmp = g_list_append (tmp, (gchar *) sid);
1006 used_types |= curtype;
1011 /* Finally set the requested selection */
1013 if (dbin->requested_selection) {
1014 GST_FIXME_OBJECT (dbin,
1015 "Replacing non-NULL requested_selection, what should we do ??");
1016 g_list_free (dbin->requested_selection);
1018 dbin->requested_selection = tmp;
1019 dbin->selection_updated = TRUE;
1021 SELECTION_UNLOCK (dbin);
1024 /* Call with INPUT_LOCK taken */
1025 static GstStreamCollection *
1026 get_merged_collection (GstDecodebin3 * dbin)
1028 gboolean needs_merge = FALSE;
1029 GstStreamCollection *res = NULL;
1033 /* First check if we need to do a merge or just return the only collection */
1034 res = dbin->main_input->collection;
1036 for (tmp = dbin->other_inputs; tmp; tmp = tmp->next) {
1037 DecodebinInput *input = (DecodebinInput *) tmp->data;
1038 if (input->collection) {
1043 res = input->collection;
1048 GST_DEBUG_OBJECT (dbin, "No need to merge, returning %p", res);
1049 return gst_object_ref (res);
1052 /* We really need to create a new collection */
1053 /* FIXME : Some numbering scheme maybe ?? */
1054 res = gst_stream_collection_new ("decodebin3");
1055 if (dbin->main_input->collection) {
1056 nb_stream = gst_stream_collection_get_size (dbin->main_input->collection);
1057 GST_DEBUG_OBJECT (dbin, "main input %p %d", dbin->main_input, nb_stream);
1058 for (i = 0; i < nb_stream; i++) {
1060 gst_stream_collection_get_stream (dbin->main_input->collection, i);
1061 gst_stream_collection_add_stream (res, gst_object_ref (stream));
1065 for (tmp = dbin->other_inputs; tmp; tmp = tmp->next) {
1066 DecodebinInput *input = (DecodebinInput *) tmp->data;
1067 GST_DEBUG_OBJECT (dbin, "input %p , collection %p", input,
1069 if (input->collection) {
1070 nb_stream = gst_stream_collection_get_size (input->collection);
1071 GST_DEBUG_OBJECT (dbin, "nb_stream : %d", nb_stream);
1072 for (i = 0; i < nb_stream; i++) {
1074 gst_stream_collection_get_stream (input->collection, i);
1075 gst_stream_collection_add_stream (res, gst_object_ref (stream));
1083 /* Call with INPUT_LOCK taken */
1084 static DecodebinInput *
1085 find_message_parsebin (GstDecodebin3 * dbin, GstElement * child)
1087 DecodebinInput *input = NULL;
1088 GstElement *parent = gst_object_ref (child);
1092 GstElement *next_parent;
1094 GST_DEBUG_OBJECT (dbin, "parent %s",
1095 parent ? GST_ELEMENT_NAME (parent) : "<NONE>");
1097 if (parent == dbin->main_input->parsebin) {
1098 input = dbin->main_input;
1101 for (tmp = dbin->other_inputs; tmp; tmp = tmp->next) {
1102 DecodebinInput *cur = (DecodebinInput *) tmp->data;
1103 if (parent == cur->parsebin) {
1108 next_parent = (GstElement *) gst_element_get_parent (parent);
1109 gst_object_unref (parent);
1110 parent = next_parent;
1112 } while (parent && parent != (GstElement *) dbin);
1115 gst_object_unref (parent);
1121 stream_in_collection (GstDecodebin3 * dbin, gchar * sid)
1125 if (dbin->collection == NULL)
1127 len = gst_stream_collection_get_size (dbin->collection);
1128 for (i = 0; i < len; i++) {
1129 GstStream *stream = gst_stream_collection_get_stream (dbin->collection, i);
1130 const gchar *osid = gst_stream_get_stream_id (stream);
1131 if (!g_strcmp0 (sid, osid))
1138 /* Call with INPUT_LOCK taken */
1140 handle_stream_collection (GstDecodebin3 * dbin,
1141 GstStreamCollection * collection, GstElement * child)
1143 #ifndef GST_DISABLE_GST_DEBUG
1144 const gchar *upstream_id;
1147 DecodebinInput *input = find_message_parsebin (dbin, child);
1150 GST_DEBUG_OBJECT (dbin,
1151 "Couldn't find corresponding input, most likely shutting down");
1155 /* Replace collection in input */
1156 if (input->collection)
1157 gst_object_unref (input->collection);
1158 input->collection = gst_object_ref (collection);
1159 GST_DEBUG_OBJECT (dbin, "Setting collection %p on input %p", collection,
1162 /* Merge collection if needed */
1163 collection = get_merged_collection (dbin);
1165 #ifndef GST_DISABLE_GST_DEBUG
1166 /* Just some debugging */
1167 upstream_id = gst_stream_collection_get_upstream_id (collection);
1168 GST_DEBUG ("Received Stream Collection. Upstream_id : %s", upstream_id);
1169 GST_DEBUG ("From input %p", input);
1170 GST_DEBUG (" %d streams", gst_stream_collection_get_size (collection));
1171 for (i = 0; i < gst_stream_collection_get_size (collection); i++) {
1172 GstStream *stream = gst_stream_collection_get_stream (collection, i);
1173 GstTagList *taglist;
1176 GST_DEBUG (" Stream '%s'", gst_stream_get_stream_id (stream));
1177 GST_DEBUG (" type : %s",
1178 gst_stream_type_get_name (gst_stream_get_stream_type (stream)));
1179 GST_DEBUG (" flags : 0x%x", gst_stream_get_stream_flags (stream));
1180 taglist = gst_stream_get_tags (stream);
1181 GST_DEBUG (" tags : %" GST_PTR_FORMAT, taglist);
1182 caps = gst_stream_get_caps (stream);
1183 GST_DEBUG (" caps : %" GST_PTR_FORMAT, caps);
1185 gst_tag_list_unref (taglist);
1187 gst_caps_unref (caps);
1191 /* Store collection for later usage */
1192 if (dbin->collection == NULL) {
1193 dbin->collection = collection;
1195 /* We need to check who emitted this collection (the owner).
1196 * If we already had a collection from that user, this one is an update,
1197 * that is to say that we need to figure out how we are going to re-use
1198 * the streams/slot */
1199 GST_FIXME_OBJECT (dbin, "New collection but already had one ...");
1200 /* FIXME : When do we switch from pending collection to active collection ?
1201 * When all streams from active collection are drained in multiqueue output ? */
1202 gst_object_unref (dbin->collection);
1203 dbin->collection = collection;
1204 /* dbin->pending_collection = */
1205 /* g_list_append (dbin->pending_collection, collection); */
1210 gst_decodebin3_handle_message (GstBin * bin, GstMessage * message)
1212 GstDecodebin3 *dbin = (GstDecodebin3 *) bin;
1213 gboolean posting_collection = FALSE;
1215 GST_DEBUG_OBJECT (bin, "Got Message %s", GST_MESSAGE_TYPE_NAME (message));
1217 switch (GST_MESSAGE_TYPE (message)) {
1218 case GST_MESSAGE_STREAM_COLLECTION:
1220 GstStreamCollection *collection = NULL;
1221 gst_message_parse_stream_collection (message, &collection);
1224 handle_stream_collection (dbin, collection,
1225 (GstElement *) GST_MESSAGE_SRC (message));
1226 posting_collection = TRUE;
1227 INPUT_UNLOCK (dbin);
1229 if (dbin->collection && collection != dbin->collection) {
1230 /* Replace collection message, we most likely aggregated it */
1231 GstMessage *new_msg;
1233 gst_message_new_stream_collection ((GstObject *) dbin,
1235 gst_message_unref (message);
1239 gst_object_unref (collection);
1246 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
1248 if (posting_collection) {
1249 /* Figure out a selection for that collection */
1250 update_requested_selection (dbin, dbin->collection);
1254 static DecodebinOutputStream *
1255 find_free_compatible_output (GstDecodebin3 * dbin, GstStream * stream)
1258 GstStreamType stype = gst_stream_get_stream_type (stream);
1260 for (tmp = dbin->output_streams; tmp; tmp = tmp->next) {
1261 DecodebinOutputStream *output = (DecodebinOutputStream *) tmp->data;
1262 if (output->type == stype && output->slot && output->slot->active_stream) {
1263 GstStream *tstream = output->slot->active_stream;
1264 if (!stream_in_list (dbin->requested_selection,
1265 (gchar *) gst_stream_get_stream_id (tstream))) {
1274 /* Give a certain slot, figure out if it should be linked to an
1276 * CALL WITH SELECTION LOCK TAKEN !*/
1277 static DecodebinOutputStream *
1278 get_output_for_slot (MultiQueueSlot * slot)
1280 GstDecodebin3 *dbin = slot->dbin;
1281 DecodebinOutputStream *output = NULL;
1282 const gchar *stream_id;
1285 /* If we already have a configured output, just use it */
1286 if (slot->output != NULL)
1287 return slot->output;
1292 * This method needs to be split into multiple parts
1294 * 1) Figure out whether stream should be exposed or not
1295 * This is based on autoplug-continue, EXPOSE_ALL_MODE, or presence
1296 * in the default stream attribution
1298 * 2) Figure out whether an output stream should be created, whether
1299 * we can re-use the output stream already linked to the slot, or
1300 * whether we need to get re-assigned another (currently used) output
1304 stream_id = gst_stream_get_stream_id (slot->active_stream);
1305 caps = gst_stream_get_caps (slot->active_stream);
1306 GST_DEBUG_OBJECT (dbin, "stream %s , %" GST_PTR_FORMAT, stream_id, caps);
1307 gst_caps_unref (caps);
1309 /* 0. Emit autoplug-continue signal for pending caps ? */
1310 GST_FIXME_OBJECT (dbin, "emit autoplug-continue");
1312 /* 1. if in EXPOSE_ALL_MODE, just accept */
1313 GST_FIXME_OBJECT (dbin, "Handle EXPOSE_ALL_MODE");
1316 /* FIXME : The idea around this was to avoid activating a stream for
1317 * which we have no decoder. Unfortunately it is way too
1318 * expensive. Need to figure out a better solution */
1319 /* 2. Is there a potential decoder (if one is required) */
1320 if (!gst_caps_can_intersect (caps, dbin->caps)
1321 && !have_factory (dbin, (GstCaps *) caps,
1322 GST_ELEMENT_FACTORY_TYPE_DECODER)) {
1323 GST_WARNING_OBJECT (dbin, "Don't have a decoder for %" GST_PTR_FORMAT,
1325 SELECTION_UNLOCK (dbin);
1326 gst_element_post_message (GST_ELEMENT_CAST (dbin),
1327 gst_missing_decoder_message_new (GST_ELEMENT_CAST (dbin), caps));
1328 SELECTION_LOCK (dbin);
1333 /* 3. In default mode check if we should expose */
1334 if (stream_in_list (dbin->requested_selection, stream_id)) {
1335 /* Check if we can steal an existing output stream we could re-use.
1337 * * an output stream whose slot->stream is not in requested
1338 * * and is of the same type as this stream
1340 output = find_free_compatible_output (dbin, slot->active_stream);
1342 /* Move this output from its current slot to this slot */
1344 g_list_append (dbin->to_activate, (gchar *) stream_id);
1345 dbin->requested_selection =
1346 g_list_remove (dbin->requested_selection, stream_id);
1347 SELECTION_UNLOCK (dbin);
1348 gst_pad_add_probe (output->slot->src_pad, GST_PAD_PROBE_TYPE_IDLE,
1349 (GstPadProbeCallback) slot_unassign_probe, output->slot, NULL);
1350 SELECTION_LOCK (dbin);
1354 output = create_output_stream (dbin, slot->type);
1355 output->slot = slot;
1356 GST_DEBUG ("Linking slot %p to new output %p", slot, output);
1357 slot->output = output;
1358 dbin->active_selection =
1359 g_list_append (dbin->active_selection, (gchar *) stream_id);
1361 GST_DEBUG ("Not creating any output for slot %p", slot);
1366 /* Returns SELECTED_STREAMS message if active_selection is equal to
1367 * requested_selection, else NULL.
1368 * Must be called with LOCK taken */
1370 is_selection_done (GstDecodebin3 * dbin)
1375 if (!dbin->selection_updated)
1378 GST_LOG_OBJECT (dbin, "Checking");
1380 if (dbin->to_activate != NULL) {
1381 GST_DEBUG ("Still have streams to activate");
1384 for (tmp = dbin->requested_selection; tmp; tmp = tmp->next) {
1385 GST_DEBUG ("Checking requested stream %s", (gchar *) tmp->data);
1386 if (!stream_in_list (dbin->active_selection, (gchar *) tmp->data)) {
1387 GST_DEBUG ("Not in active selection, returning");
1392 GST_DEBUG_OBJECT (dbin, "Selection active, creating message");
1394 /* We are completely active */
1395 msg = gst_message_new_streams_selected ((GstObject *) dbin, dbin->collection);
1396 GST_MESSAGE_SEQNUM (msg) = dbin->select_streams_seqnum;
1397 for (tmp = dbin->output_streams; tmp; tmp = tmp->next) {
1398 DecodebinOutputStream *output = (DecodebinOutputStream *) tmp->data;
1400 GST_DEBUG_OBJECT (dbin, "Adding stream %s",
1401 gst_stream_get_stream_id (output->slot->active_stream));
1403 gst_message_streams_selected_add (msg, output->slot->active_stream);
1405 GST_WARNING_OBJECT (dbin, "No valid slot for output %p", output);
1407 dbin->selection_updated = FALSE;
1411 static GstPadProbeReturn
1412 multiqueue_src_probe (GstPad * pad, GstPadProbeInfo * info,
1413 MultiQueueSlot * slot)
1415 GstPadProbeReturn ret = GST_PAD_PROBE_OK;
1416 GstDecodebin3 *dbin = slot->dbin;
1418 if (GST_IS_EVENT (GST_PAD_PROBE_INFO_DATA (info))) {
1419 GstEvent *ev = GST_PAD_PROBE_INFO_EVENT (info);
1421 GST_DEBUG_OBJECT (pad, "Got event %p %s", ev, GST_EVENT_TYPE_NAME (ev));
1422 switch (GST_EVENT_TYPE (ev)) {
1423 case GST_EVENT_STREAM_START:
1425 GstStream *stream = NULL;
1426 const gchar *stream_id;
1428 gst_event_parse_stream (ev, &stream);
1429 if (stream == NULL) {
1430 GST_ERROR_OBJECT (pad,
1431 "Got a STREAM_START event without a GstStream");
1434 stream_id = gst_stream_get_stream_id (stream);
1435 GST_DEBUG_OBJECT (pad, "Stream Start '%s'", stream_id);
1436 if (slot->active_stream == NULL) {
1437 slot->active_stream = stream;
1438 } else if (slot->active_stream != stream) {
1439 GST_FIXME_OBJECT (pad, "Handle stream changes (%s => %s) !",
1440 gst_stream_get_stream_id (slot->active_stream),
1441 gst_stream_get_stream_id (stream));
1442 gst_object_unref (slot->active_stream);
1443 slot->active_stream = stream;
1445 gst_object_unref (stream);
1446 #if 0 /* Disabled because stream-start is pushed for every buffer on every unlinked pad */
1448 gboolean is_active, is_requested;
1449 /* Quick check to see if we're in the current selection */
1450 /* FIXME : Re-check all slot<=>output mappings based on requested_selection */
1451 SELECTION_LOCK (dbin);
1452 GST_DEBUG_OBJECT (dbin, "Checking active selection");
1453 is_active = stream_in_list (dbin->active_selection, stream_id);
1454 GST_DEBUG_OBJECT (dbin, "Checking requested selection");
1455 is_requested = stream_in_list (dbin->requested_selection, stream_id);
1456 SELECTION_UNLOCK (dbin);
1458 GST_DEBUG_OBJECT (pad, "Slot in ACTIVE selection (output:%p)",
1461 GST_DEBUG_OBJECT (pad, "Slot in REQUESTED selection (output:%p)",
1463 else if (slot->output) {
1464 GST_DEBUG_OBJECT (pad,
1465 "Slot needs to be deactivated ? It's no longer in requested selection");
1466 } else if (!is_active)
1467 GST_DEBUG_OBJECT (pad,
1468 "Slot in neither active nor requested selection");
1473 case GST_EVENT_CAPS:
1475 /* Configure the output slot if needed */
1476 DecodebinOutputStream *output;
1477 GstMessage *msg = NULL;
1478 SELECTION_LOCK (dbin);
1479 output = get_output_for_slot (slot);
1481 reconfigure_output_stream (output, slot);
1482 msg = is_selection_done (dbin);
1484 SELECTION_UNLOCK (dbin);
1486 gst_element_post_message ((GstElement *) slot->dbin, msg);
1490 /* FIXME : Figure out */
1491 GST_FIXME_OBJECT (pad, "EOS on multiqueue source pad. input:%p",
1493 if (slot->input == NULL) {
1495 GST_DEBUG_OBJECT (pad,
1496 "last EOS for input, forwarding and removing slot");
1497 peer = gst_pad_get_peer (pad);
1499 gst_pad_send_event (peer, ev);
1500 gst_object_unref (peer);
1502 gst_event_unref (ev);
1504 SELECTION_LOCK (dbin);
1505 /* FIXME : Shouldn't we try to re-assign the output instead of just
1507 /* Remove the output */
1509 DecodebinOutputStream *output = slot->output;
1510 dbin->output_streams = g_list_remove (dbin->output_streams, output);
1511 free_output_stream (dbin, output);
1513 SELECTION_UNLOCK (dbin);
1514 ret = GST_PAD_PROBE_HANDLED;
1520 } else if (GST_IS_QUERY (GST_PAD_PROBE_INFO_DATA (info))) {
1521 GstQuery *query = GST_PAD_PROBE_INFO_QUERY (info);
1522 switch (GST_QUERY_TYPE (query)) {
1523 case GST_QUERY_CAPS:
1525 GST_DEBUG_OBJECT (pad, "Intercepting CAPS query");
1526 gst_query_set_caps_result (query, GST_CAPS_ANY);
1527 ret = GST_PAD_PROBE_HANDLED;
1531 case GST_QUERY_ACCEPT_CAPS:
1533 GST_DEBUG_OBJECT (pad, "Intercepting Accept Caps query");
1534 /* If the current decoder doesn't accept caps, we'll reconfigure
1535 * on the actual caps event. So accept any caps. */
1536 gst_query_set_accept_caps_result (query, TRUE);
1537 ret = GST_PAD_PROBE_HANDLED;
1547 /* Create a new multiqueue slot for the given type
1549 * It is up to the caller to know whether that slot is needed or not
1550 * (and release it when no longer needed) */
1551 static MultiQueueSlot *
1552 create_new_slot (GstDecodebin3 * dbin, GstStreamType type)
1554 MultiQueueSlot *slot;
1555 GstIterator *it = NULL;
1556 GValue item = { 0, };
1558 GST_DEBUG_OBJECT (dbin, "Creating new slot for type %s",
1559 gst_stream_type_get_name (type));
1560 slot = g_new0 (MultiQueueSlot, 1);
1562 slot->id = dbin->slot_id++;
1564 slot->sink_pad = gst_element_get_request_pad (dbin->multiqueue, "sink_%u");
1565 if (slot->sink_pad == NULL)
1567 it = gst_pad_iterate_internal_links (slot->sink_pad);
1568 if (!it || (gst_iterator_next (it, &item)) != GST_ITERATOR_OK
1569 || ((slot->src_pad = g_value_dup_object (&item)) == NULL)) {
1570 GST_ERROR ("Couldn't get srcpad from multiqueue for sink pad %s:%s",
1571 GST_DEBUG_PAD_NAME (slot->src_pad));
1574 gst_iterator_free (it);
1575 g_value_reset (&item);
1577 g_object_set (slot->sink_pad, "group-id", (guint) type, NULL);
1579 /* Add event probe */
1581 gst_pad_add_probe (slot->src_pad,
1582 GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM | GST_PAD_PROBE_TYPE_QUERY_DOWNSTREAM,
1583 (GstPadProbeCallback) multiqueue_src_probe, slot, NULL);
1585 GST_DEBUG ("Created new slot %u (%p) (%s:%s)", slot->id, slot,
1586 GST_DEBUG_PAD_NAME (slot->src_pad));
1587 dbin->slots = g_list_append (dbin->slots, slot);
1594 gst_element_release_request_pad (dbin->multiqueue, slot->sink_pad);
1600 /* Must be called with SELECTION_LOCK */
1601 static MultiQueueSlot *
1602 get_slot_for_input (GstDecodebin3 * dbin, DecodebinInputStream * input)
1605 MultiQueueSlot *empty_slot = NULL;
1606 GstStreamType input_type = 0;
1607 gchar *stream_id = NULL;
1609 GST_DEBUG_OBJECT (dbin, "input %p (stream %p %s)",
1610 input, input->active_stream,
1612 active_stream ? gst_stream_get_stream_id (input->active_stream) : "");
1614 if (input->active_stream) {
1615 input_type = gst_stream_get_stream_type (input->active_stream);
1616 stream_id = (gchar *) gst_stream_get_stream_id (input->active_stream);
1619 /* Go over existing slots and check if there is already one for it */
1620 for (tmp = dbin->slots; tmp; tmp = tmp->next) {
1621 MultiQueueSlot *slot = (MultiQueueSlot *) tmp->data;
1622 /* Already used input, return that one */
1623 if (slot->input == input) {
1624 GST_DEBUG_OBJECT (dbin, "Returning already specified slot %d", slot->id);
1629 /* Go amongst all unused slots of the right type and try to find a candidate */
1630 for (tmp = dbin->slots; tmp; tmp = tmp->next) {
1631 MultiQueueSlot *slot = (MultiQueueSlot *) tmp->data;
1632 if (slot->input == NULL && input_type == slot->type) {
1633 /* Remember this empty slot for later */
1635 /* Check if available slot is of the same stream_id */
1636 GST_LOG_OBJECT (dbin, "Checking candidate slot %d (active_stream:%p)",
1637 slot->id, slot->active_stream);
1638 if (stream_id && slot->active_stream) {
1640 (gchar *) gst_stream_get_stream_id (slot->active_stream);
1641 GST_DEBUG_OBJECT (dbin, "Checking slot %d %s against %s", slot->id,
1642 ostream_id, stream_id);
1643 if (!g_strcmp0 (stream_id, ostream_id))
1650 GST_DEBUG_OBJECT (dbin, "Re-using existing unused slot %d", empty_slot->id);
1651 empty_slot->input = input;
1656 return create_new_slot (dbin, input_type);
1662 link_input_to_slot (DecodebinInputStream * input, MultiQueueSlot * slot)
1665 if (slot->input != NULL && slot->input != input) {
1666 GST_ERROR_OBJECT (slot->dbin,
1667 "Trying to link input to an already used slot");
1670 gst_pad_link_full (input->srcpad, slot->sink_pad, GST_PAD_LINK_CHECK_NOTHING);
1671 slot->pending_stream = input->active_stream;
1672 slot->input = input;
1673 event = gst_pad_get_sticky_event (input->srcpad, GST_EVENT_STREAM_START, 0);
1675 gst_pad_send_event (slot->sink_pad, event);
1680 have_factory (GstDecodebin3 * dbin, GstCaps * caps,
1681 GstElementFactoryListType ftype)
1683 gboolean ret = FALSE;
1686 g_mutex_lock (&dbin->factories_lock);
1687 gst_decode_bin_update_factories_list (dbin);
1688 if (ftype == GST_ELEMENT_FACTORY_TYPE_DECODER)
1690 gst_element_factory_list_filter (dbin->decoder_factories,
1691 caps, GST_PAD_SINK, TRUE);
1694 gst_element_factory_list_filter (dbin->decodable_factories,
1695 caps, GST_PAD_SINK, TRUE);
1696 g_mutex_unlock (&dbin->factories_lock);
1700 gst_plugin_feature_list_free (res);
1708 create_element (GstDecodebin3 * dbin, GstStream * stream,
1709 GstElementFactoryListType ftype)
1712 GstElement *element = NULL;
1715 g_mutex_lock (&dbin->factories_lock);
1716 gst_decode_bin_update_factories_list (dbin);
1717 caps = gst_stream_get_caps (stream);
1718 if (ftype == GST_ELEMENT_FACTORY_TYPE_DECODER)
1720 gst_element_factory_list_filter (dbin->decoder_factories,
1721 caps, GST_PAD_SINK, TRUE);
1724 gst_element_factory_list_filter (dbin->decodable_factories,
1725 caps, GST_PAD_SINK, TRUE);
1726 g_mutex_unlock (&dbin->factories_lock);
1730 gst_element_factory_create ((GstElementFactory *) res->data, NULL);
1731 GST_DEBUG ("Created element '%s'", GST_ELEMENT_NAME (element));
1732 gst_plugin_feature_list_free (res);
1734 GST_DEBUG ("Could not find an element for caps %" GST_PTR_FORMAT, caps);
1737 gst_caps_unref (caps);
1741 /* FIXME : VERY NAIVE. ASSUMING FIRST ONE WILL WORK */
1743 create_decoder (GstDecodebin3 * dbin, GstStream * stream)
1745 return create_element (dbin, stream, GST_ELEMENT_FACTORY_TYPE_DECODER);
1748 static GstPadProbeReturn
1749 keyframe_waiter_probe (GstPad * pad, GstPadProbeInfo * info,
1750 DecodebinOutputStream * output)
1752 GstBuffer *buf = GST_PAD_PROBE_INFO_BUFFER (info);
1753 /* If we have a keyframe, remove the probe and let all data through */
1754 /* FIXME : HANDLE HEADER BUFFER ?? */
1755 if (!GST_BUFFER_FLAG_IS_SET (buf, GST_BUFFER_FLAG_DELTA_UNIT) ||
1756 GST_BUFFER_FLAG_IS_SET (buf, GST_BUFFER_FLAG_HEADER)) {
1757 GST_DEBUG_OBJECT (pad,
1758 "Buffer is keyframe or header, letting through and removing probe");
1759 output->drop_probe_id = 0;
1760 return GST_PAD_PROBE_REMOVE;
1762 GST_DEBUG_OBJECT (pad, "Buffer is not a keyframe, dropping");
1763 return GST_PAD_PROBE_DROP;
1767 reconfigure_output_stream (DecodebinOutputStream * output,
1768 MultiQueueSlot * slot)
1770 GstDecodebin3 *dbin = output->dbin;
1771 GstCaps *new_caps = (GstCaps *) gst_stream_get_caps (slot->active_stream);
1772 gboolean needs_decoder;
1774 needs_decoder = gst_caps_can_intersect (new_caps, dbin->caps) != TRUE;
1776 GST_DEBUG_OBJECT (dbin,
1777 "Reconfiguring output %p to slot %p, needs_decoder:%d", output, slot,
1780 /* FIXME : Maybe make the output un-hook itself automatically ? */
1781 if (output->slot != NULL && output->slot != slot) {
1782 GST_WARNING_OBJECT (dbin,
1783 "Output still linked to another slot (%p)", output->slot);
1784 gst_caps_unref (new_caps);
1788 /* Check if existing config is reusable as-is by checking if
1789 * the existing decoder accepts the new caps, if not delete
1790 * it and create a new one */
1791 if (output->decoder) {
1792 gboolean can_reuse_decoder;
1794 if (needs_decoder) {
1796 gst_pad_query_accept_caps (output->decoder_sink, new_caps);
1798 can_reuse_decoder = FALSE;
1800 if (can_reuse_decoder) {
1801 if (output->type & GST_STREAM_TYPE_VIDEO && output->drop_probe_id == 0) {
1802 GST_DEBUG_OBJECT (dbin, "Adding keyframe-waiter probe");
1803 output->drop_probe_id =
1804 gst_pad_add_probe (slot->src_pad, GST_PAD_PROBE_TYPE_BUFFER,
1805 (GstPadProbeCallback) keyframe_waiter_probe, output, NULL);
1807 GST_DEBUG_OBJECT (dbin, "Reusing existing decoder for slot %p", slot);
1808 if (output->linked == FALSE) {
1809 gst_pad_link_full (slot->src_pad, output->decoder_sink,
1810 GST_PAD_LINK_CHECK_NOTHING);
1811 output->linked = TRUE;
1813 gst_caps_unref (new_caps);
1817 GST_DEBUG_OBJECT (dbin, "Removing old decoder for slot %p", slot);
1820 gst_pad_unlink (slot->src_pad, output->decoder_sink);
1821 output->linked = FALSE;
1822 if (output->drop_probe_id) {
1823 gst_pad_remove_probe (slot->src_pad, output->drop_probe_id);
1824 output->drop_probe_id = 0;
1827 if (!gst_ghost_pad_set_target ((GstGhostPad *) output->src_pad, NULL)) {
1828 GST_ERROR_OBJECT (dbin, "Could not release decoder pad");
1829 gst_caps_unref (new_caps);
1833 gst_element_set_locked_state (output->decoder, TRUE);
1834 gst_element_set_state (output->decoder, GST_STATE_NULL);
1836 gst_bin_remove ((GstBin *) dbin, output->decoder);
1837 output->decoder = NULL;
1840 gst_caps_unref (new_caps);
1842 gst_object_replace ((GstObject **) & output->decoder_sink, NULL);
1843 gst_object_replace ((GstObject **) & output->decoder_src, NULL);
1845 /* If a decoder is required, create one */
1846 if (needs_decoder) {
1847 /* If we don't have a decoder yet, instantiate one */
1848 output->decoder = create_decoder (dbin, slot->active_stream);
1849 if (output->decoder == NULL) {
1852 SELECTION_UNLOCK (dbin);
1853 /* FIXME : Should we be smarter if there's a missing decoder ?
1854 * Should we deactivate that stream ? */
1855 caps = gst_stream_get_caps (slot->active_stream);
1856 gst_element_post_message (GST_ELEMENT_CAST (dbin),
1857 gst_missing_decoder_message_new (GST_ELEMENT_CAST (dbin), caps));
1858 gst_caps_unref (caps);
1859 SELECTION_LOCK (dbin);
1862 if (!gst_bin_add ((GstBin *) dbin, output->decoder)) {
1863 GST_ERROR_OBJECT (dbin, "could not add decoder to pipeline");
1866 output->decoder_sink = gst_element_get_static_pad (output->decoder, "sink");
1867 output->decoder_src = gst_element_get_static_pad (output->decoder, "src");
1868 if (output->type & GST_STREAM_TYPE_VIDEO) {
1869 GST_DEBUG_OBJECT (dbin, "Adding keyframe-waiter probe");
1870 output->drop_probe_id =
1871 gst_pad_add_probe (slot->src_pad, GST_PAD_PROBE_TYPE_BUFFER,
1872 (GstPadProbeCallback) keyframe_waiter_probe, output, NULL);
1874 if (gst_pad_link_full (slot->src_pad, output->decoder_sink,
1875 GST_PAD_LINK_CHECK_NOTHING) != GST_PAD_LINK_OK) {
1876 GST_ERROR_OBJECT (dbin, "could not link to %s:%s",
1877 GST_DEBUG_PAD_NAME (output->decoder_sink));
1881 output->decoder_src = gst_object_ref (slot->src_pad);
1882 output->decoder_sink = NULL;
1884 output->linked = TRUE;
1885 if (!gst_ghost_pad_set_target ((GstGhostPad *) output->src_pad,
1886 output->decoder_src)) {
1887 GST_ERROR_OBJECT (dbin, "Could not expose decoder pad");
1890 if (output->src_exposed == FALSE) {
1891 output->src_exposed = TRUE;
1892 gst_element_add_pad (GST_ELEMENT_CAST (dbin), output->src_pad);
1895 if (output->decoder)
1896 gst_element_sync_state_with_parent (output->decoder);
1898 output->slot = slot;
1903 GST_DEBUG_OBJECT (dbin, "Cleanup");
1904 if (output->decoder_sink) {
1905 gst_object_unref (output->decoder_sink);
1906 output->decoder_sink = NULL;
1908 if (output->decoder_src) {
1909 gst_object_unref (output->decoder_src);
1910 output->decoder_src = NULL;
1912 if (output->decoder) {
1913 gst_element_set_state (output->decoder, GST_STATE_NULL);
1914 gst_bin_remove ((GstBin *) dbin, output->decoder);
1915 output->decoder = NULL;
1920 static GstPadProbeReturn
1921 idle_reconfigure (GstPad * pad, GstPadProbeInfo * info, MultiQueueSlot * slot)
1923 GstMessage *msg = NULL;
1924 DecodebinOutputStream *output;
1926 SELECTION_LOCK (slot->dbin);
1927 output = get_output_for_slot (slot);
1929 GST_DEBUG_OBJECT (pad, "output : %p", output);
1932 reconfigure_output_stream (output, slot);
1933 msg = is_selection_done (slot->dbin);
1935 SELECTION_UNLOCK (slot->dbin);
1937 gst_element_post_message ((GstElement *) slot->dbin, msg);
1939 return GST_PAD_PROBE_REMOVE;
1942 static MultiQueueSlot *
1943 find_slot_for_stream_id (GstDecodebin3 * dbin, const gchar * sid)
1947 for (tmp = dbin->slots; tmp; tmp = tmp->next) {
1948 MultiQueueSlot *slot = (MultiQueueSlot *) tmp->data;
1949 const gchar *stream_id;
1950 if (slot->active_stream) {
1951 stream_id = gst_stream_get_stream_id (slot->active_stream);
1952 if (!g_strcmp0 (sid, stream_id))
1955 if (slot->pending_stream && slot->pending_stream != slot->active_stream) {
1956 stream_id = gst_stream_get_stream_id (slot->pending_stream);
1957 if (!g_strcmp0 (sid, stream_id))
1965 /* This function handles the reassignment of a slot. Call this from
1966 * the streaming thread of a slot. */
1968 reassign_slot (GstDecodebin3 * dbin, MultiQueueSlot * slot)
1970 DecodebinOutputStream *output;
1971 MultiQueueSlot *target_slot = NULL;
1973 const gchar *sid, *tsid;
1975 SELECTION_LOCK (dbin);
1976 output = slot->output;
1978 if (G_UNLIKELY (slot->active_stream == NULL)) {
1979 GST_DEBUG_OBJECT (slot->src_pad,
1980 "Called on inactive slot (active_stream == NULL)");
1981 SELECTION_UNLOCK (dbin);
1985 if (G_UNLIKELY (output == NULL)) {
1986 GST_DEBUG_OBJECT (slot->src_pad,
1987 "Slot doesn't have any output to be removed");
1988 SELECTION_UNLOCK (dbin);
1992 sid = gst_stream_get_stream_id (slot->active_stream);
1993 GST_DEBUG_OBJECT (slot->src_pad, "slot %s %p", sid, slot);
1995 /* Recheck whether this stream is still in the list of streams to deactivate */
1996 if (stream_in_list (dbin->requested_selection, sid)) {
1997 /* Stream is in the list of requested streams, don't remove */
1998 SELECTION_UNLOCK (dbin);
1999 GST_DEBUG_OBJECT (slot->src_pad,
2000 "Stream '%s' doesn't need to be deactivated", sid);
2004 /* Unlink slot from output */
2005 /* FIXME : Handle flushing ? */
2006 /* FIXME : Handle outputs without decoders */
2007 GST_DEBUG_OBJECT (slot->src_pad, "Unlinking from decoder %p",
2008 output->decoder_sink);
2009 if (output->decoder_sink)
2010 gst_pad_unlink (slot->src_pad, output->decoder_sink);
2011 output->linked = FALSE;
2012 slot->output = NULL;
2013 output->slot = NULL;
2014 /* Remove sid from active selection */
2015 for (tmp = dbin->active_selection; tmp; tmp = tmp->next)
2016 if (!g_strcmp0 (sid, tmp->data)) {
2017 dbin->active_selection = g_list_delete_link (dbin->active_selection, tmp);
2021 /* Can we re-assign this output to a requested stream ? */
2022 GST_DEBUG_OBJECT (slot->src_pad, "Attempting to re-assing output stream");
2023 for (tmp = dbin->to_activate; tmp; tmp = tmp->next) {
2024 MultiQueueSlot *tslot = find_slot_for_stream_id (dbin, tmp->data);
2025 GST_LOG_OBJECT (tslot->src_pad, "Checking slot %p (output:%p , stream:%s)",
2026 tslot, tslot->output, gst_stream_get_stream_id (tslot->active_stream));
2027 if (tslot && tslot->type == output->type && tslot->output == NULL) {
2028 GST_DEBUG_OBJECT (tslot->src_pad, "Using as reassigned slot");
2029 target_slot = tslot;
2031 /* Pass target stream id to requested selection */
2032 dbin->requested_selection =
2033 g_list_append (dbin->requested_selection, tmp->data);
2034 dbin->to_activate = g_list_remove (dbin->to_activate, tmp->data);
2040 GST_DEBUG_OBJECT (slot->src_pad, "Assigning output to slot %p '%s'",
2042 target_slot->output = output;
2043 output->slot = target_slot;
2044 dbin->active_selection =
2045 g_list_append (dbin->active_selection, (gchar *) tsid);
2046 SELECTION_UNLOCK (dbin);
2048 /* Wakeup the target slot so that it retries to send events/buffers
2049 * thereby triggering the output reconfiguration codepath */
2050 gst_pad_add_probe (target_slot->src_pad, GST_PAD_PROBE_TYPE_IDLE,
2051 (GstPadProbeCallback) idle_reconfigure, target_slot, NULL);
2052 /* gst_pad_send_event (target_slot->src_pad, gst_event_new_reconfigure ()); */
2056 dbin->output_streams = g_list_remove (dbin->output_streams, output);
2057 free_output_stream (dbin, output);
2058 msg = is_selection_done (slot->dbin);
2059 SELECTION_UNLOCK (dbin);
2062 gst_element_post_message ((GstElement *) slot->dbin, msg);
2068 /* Idle probe called when a slot should be unassigned from its output stream.
2069 * This is needed to ensure nothing is flowing when unlinking the slot.
2071 * Also, this method will search for a pending stream which could re-use
2072 * the output stream. */
2073 static GstPadProbeReturn
2074 slot_unassign_probe (GstPad * pad, GstPadProbeInfo * info,
2075 MultiQueueSlot * slot)
2077 GstDecodebin3 *dbin = slot->dbin;
2079 reassign_slot (dbin, slot);
2081 return GST_PAD_PROBE_REMOVE;
2085 handle_stream_switch (GstDecodebin3 * dbin, GList * select_streams,
2088 gboolean ret = TRUE;
2090 /* List of slots to (de)activate. */
2091 GList *to_deactivate = NULL;
2092 GList *to_activate = NULL;
2093 /* List of unknown stream id, most likely means the event
2094 * should be sent upstream so that elements can expose the requested stream */
2095 GList *unknown = NULL;
2096 GList *to_reassign = NULL;
2097 GList *future_request_streams = NULL;
2098 GList *pending_streams = NULL;
2099 GList *slots_to_reassign = NULL;
2101 SELECTION_LOCK (dbin);
2102 if (G_UNLIKELY (seqnum != dbin->select_streams_seqnum)) {
2103 GST_DEBUG_OBJECT (dbin, "New SELECT_STREAMS has arrived in the meantime");
2104 SELECTION_UNLOCK (dbin);
2107 /* Remove pending select_streams */
2108 g_list_free (dbin->pending_select_streams);
2109 dbin->pending_select_streams = NULL;
2111 /* COMPARE the requested streams to the active and requested streams
2114 /* First check the slots to activate and which ones are unknown */
2115 for (tmp = select_streams; tmp; tmp = tmp->next) {
2116 const gchar *sid = (const gchar *) tmp->data;
2117 MultiQueueSlot *slot;
2118 GST_DEBUG_OBJECT (dbin, "Checking stream '%s'", sid);
2119 slot = find_slot_for_stream_id (dbin, sid);
2120 /* Find the corresponding slot */
2122 if (stream_in_collection (dbin, (gchar *) sid)) {
2123 pending_streams = g_list_append (pending_streams, (gchar *) sid);
2125 GST_DEBUG_OBJECT (dbin, "We don't have a slot for stream '%s'", sid);
2126 unknown = g_list_append (unknown, (gchar *) sid);
2128 } else if (slot->output == NULL) {
2129 GST_DEBUG_OBJECT (dbin, "We need to activate slot %p for stream '%s')",
2131 to_activate = g_list_append (to_activate, slot);
2133 GST_DEBUG_OBJECT (dbin,
2134 "Stream '%s' from slot %p is already active on output %p", sid, slot,
2136 future_request_streams =
2137 g_list_append (future_request_streams, (gchar *) sid);
2141 for (tmp = dbin->slots; tmp; tmp = tmp->next) {
2142 MultiQueueSlot *slot = (MultiQueueSlot *) tmp->data;
2143 /* For slots that have an output, check if it's part of the streams to
2146 gboolean slot_to_deactivate = TRUE;
2148 if (slot->active_stream) {
2149 if (stream_in_list (select_streams,
2150 gst_stream_get_stream_id (slot->active_stream)))
2151 slot_to_deactivate = FALSE;
2153 if (slot_to_deactivate && slot->pending_stream
2154 && slot->pending_stream != slot->active_stream) {
2155 if (stream_in_list (select_streams,
2156 gst_stream_get_stream_id (slot->pending_stream)))
2157 slot_to_deactivate = FALSE;
2159 if (slot_to_deactivate) {
2160 GST_DEBUG_OBJECT (dbin,
2161 "Slot %p (%s) should be deactivated, no longer used", slot,
2162 gst_stream_get_stream_id (slot->active_stream));
2163 to_deactivate = g_list_append (to_deactivate, slot);
2168 if (to_deactivate != NULL) {
2169 GST_DEBUG_OBJECT (dbin, "Check if we can reassign slots");
2170 /* We need to compare what needs to be activated and deactivated in order
2171 * to determine whether there are outputs that can be transferred */
2172 /* Take the stream-id of the slots that are to be activated, for which there
2173 * is a slot of the same type that needs to be deactivated */
2174 tmp = to_deactivate;
2176 MultiQueueSlot *slot_to_deactivate = (MultiQueueSlot *) tmp->data;
2177 gboolean removeit = FALSE;
2179 GST_DEBUG_OBJECT (dbin,
2180 "Checking if slot to deactivate (%p) has a candidate slot to activate",
2181 slot_to_deactivate);
2182 for (tmp2 = to_activate; tmp2; tmp2 = tmp2->next) {
2183 MultiQueueSlot *slot_to_activate = (MultiQueueSlot *) tmp2->data;
2184 GST_DEBUG_OBJECT (dbin, "Comparing to slot %p", slot_to_activate);
2185 if (slot_to_activate->type == slot_to_deactivate->type) {
2186 GST_DEBUG_OBJECT (dbin, "Re-using");
2187 to_reassign = g_list_append (to_reassign, (gchar *)
2188 gst_stream_get_stream_id (slot_to_activate->active_stream));
2190 g_list_append (slots_to_reassign, slot_to_deactivate);
2191 to_activate = g_list_remove (to_activate, slot_to_activate);
2198 to_deactivate = g_list_delete_link (to_deactivate, tmp);
2203 for (tmp = to_deactivate; tmp; tmp = tmp->next) {
2204 MultiQueueSlot *slot = (MultiQueueSlot *) tmp->data;
2205 GST_DEBUG_OBJECT (dbin,
2206 "Really need to deactivate slot %p, but no available alternative",
2209 slots_to_reassign = g_list_append (slots_to_reassign, slot);
2212 /* The only slots left to activate are the ones that won't be reassigned and
2213 * therefore really need to have a new output created */
2214 for (tmp = to_activate; tmp; tmp = tmp->next) {
2215 MultiQueueSlot *slot = (MultiQueueSlot *) tmp->data;
2216 if (slot->active_stream)
2217 future_request_streams =
2218 g_list_append (future_request_streams,
2219 (gchar *) gst_stream_get_stream_id (slot->active_stream));
2220 else if (slot->pending_stream)
2221 future_request_streams =
2222 g_list_append (future_request_streams,
2223 (gchar *) gst_stream_get_stream_id (slot->pending_stream));
2225 GST_ERROR_OBJECT (dbin, "No stream for slot %p !!", slot);
2228 if (to_activate == NULL && pending_streams != NULL) {
2229 GST_DEBUG_OBJECT (dbin, "Stream switch requested for future collection");
2230 if (dbin->requested_selection)
2231 g_list_free (dbin->requested_selection);
2232 dbin->requested_selection = select_streams;
2233 g_list_free (to_deactivate);
2234 g_list_free (pending_streams);
2235 to_deactivate = NULL;
2237 if (dbin->requested_selection)
2238 g_list_free (dbin->requested_selection);
2239 dbin->requested_selection = future_request_streams;
2240 dbin->requested_selection =
2241 g_list_concat (dbin->requested_selection, pending_streams);
2242 if (dbin->to_activate)
2243 g_list_free (dbin->to_activate);
2244 dbin->to_activate = to_reassign;
2247 dbin->selection_updated = TRUE;
2248 SELECTION_UNLOCK (dbin);
2251 GST_FIXME_OBJECT (dbin, "Got request for an unknown stream");
2253 /* For all streams to deactivate, add an idle probe where we will do
2254 * the unassignment and switch over */
2255 for (tmp = slots_to_reassign; tmp; tmp = tmp->next) {
2256 MultiQueueSlot *slot = (MultiQueueSlot *) tmp->data;
2257 gst_pad_add_probe (slot->src_pad, GST_PAD_PROBE_TYPE_IDLE,
2258 (GstPadProbeCallback) slot_unassign_probe, slot, NULL);
2264 static GstPadProbeReturn
2265 ghost_pad_event_probe (GstPad * pad, GstPadProbeInfo * info,
2266 DecodebinOutputStream * output)
2268 GstPadProbeReturn ret = GST_PAD_PROBE_OK;
2269 GstDecodebin3 *dbin = output->dbin;
2270 GstEvent *event = GST_PAD_PROBE_INFO_EVENT (info);
2272 GST_DEBUG_OBJECT (pad, "Got event %p %s", event, GST_EVENT_TYPE_NAME (event));
2274 switch (GST_EVENT_TYPE (event)) {
2275 case GST_EVENT_SELECT_STREAMS:
2278 GList *streams = NULL;
2279 guint32 seqnum = gst_event_get_seqnum (event);
2281 SELECTION_LOCK (dbin);
2282 if (seqnum == dbin->select_streams_seqnum) {
2283 SELECTION_UNLOCK (dbin);
2284 GST_DEBUG_OBJECT (pad,
2285 "Already handled/handling that SELECT_STREAMS event");
2288 dbin->select_streams_seqnum = seqnum;
2289 if (dbin->pending_select_streams != NULL) {
2290 GST_LOG_OBJECT (dbin, "Replacing pending select streams");
2291 g_list_free (dbin->pending_select_streams);
2292 dbin->pending_select_streams = NULL;
2294 gst_event_parse_select_streams (event, &streams);
2295 dbin->pending_select_streams = g_list_copy (streams);
2296 SELECTION_UNLOCK (dbin);
2298 /* Send event upstream */
2299 if ((peer = gst_pad_get_peer (pad))) {
2300 gst_pad_send_event (peer, event);
2301 gst_object_unref (peer);
2303 gst_event_unref (event);
2305 /* Finally handle the switch */
2307 handle_stream_switch (dbin, streams, seqnum);
2308 ret = GST_PAD_PROBE_HANDLED;
2319 gst_decodebin3_send_event (GstElement * element, GstEvent * event)
2321 GST_DEBUG_OBJECT (element, "event %s", GST_EVENT_TYPE_NAME (event));
2322 if (GST_EVENT_TYPE (event) == GST_EVENT_SELECT_STREAMS) {
2323 GstDecodebin3 *dbin = (GstDecodebin3 *) element;
2324 GList *streams = NULL;
2325 guint32 seqnum = gst_event_get_seqnum (event);
2327 SELECTION_LOCK (dbin);
2328 if (seqnum == dbin->select_streams_seqnum) {
2329 SELECTION_UNLOCK (dbin);
2330 GST_DEBUG_OBJECT (dbin,
2331 "Already handled/handling that SELECT_STREAMS event");
2334 dbin->select_streams_seqnum = seqnum;
2335 if (dbin->pending_select_streams != NULL) {
2336 GST_LOG_OBJECT (dbin, "Replacing pending select streams");
2337 g_list_free (dbin->pending_select_streams);
2338 dbin->pending_select_streams = NULL;
2340 gst_event_parse_select_streams (event, &streams);
2341 dbin->pending_select_streams = g_list_copy (streams);
2342 SELECTION_UNLOCK (dbin);
2344 /* FIXME : We don't have an upstream ?? */
2346 /* Send event upstream */
2347 if ((peer = gst_pad_get_peer (pad))) {
2348 gst_pad_send_event (peer, event);
2349 gst_object_unref (peer);
2352 /* Finally handle the switch */
2354 handle_stream_switch (dbin, streams, seqnum);
2356 gst_event_unref (event);
2359 return GST_ELEMENT_CLASS (parent_class)->send_event (element, event);
2364 free_multiqueue_slot (GstDecodebin3 * dbin, MultiQueueSlot * slot)
2367 gst_pad_remove_probe (slot->src_pad, slot->probe_id);
2369 if (slot->input->srcpad)
2370 gst_pad_unlink (slot->input->srcpad, slot->sink_pad);
2373 gst_element_release_request_pad (dbin->multiqueue, slot->sink_pad);
2374 gst_object_replace ((GstObject **) & slot->sink_pad, NULL);
2375 gst_object_replace ((GstObject **) & slot->src_pad, NULL);
2376 gst_object_replace ((GstObject **) & slot->active_stream, NULL);
2380 /* Create a DecodebinOutputStream for a given type
2381 * Note: It will be empty initially, it needs to be configured
2383 static DecodebinOutputStream *
2384 create_output_stream (GstDecodebin3 * dbin, GstStreamType type)
2386 DecodebinOutputStream *res = g_new0 (DecodebinOutputStream, 1);
2388 const gchar *prefix;
2389 GstStaticPadTemplate *templ;
2390 GstPadTemplate *ptmpl;
2392 GstPad *internal_pad;
2394 GST_DEBUG_OBJECT (dbin, "Created new output stream %p for type %s",
2395 res, gst_stream_type_get_name (type));
2400 if (type & GST_STREAM_TYPE_VIDEO) {
2401 templ = &video_src_template;
2402 counter = &dbin->vpadcount;
2404 } else if (type & GST_STREAM_TYPE_AUDIO) {
2405 templ = &audio_src_template;
2406 counter = &dbin->apadcount;
2408 } else if (type & GST_STREAM_TYPE_TEXT) {
2409 templ = &text_src_template;
2410 counter = &dbin->tpadcount;
2413 templ = &src_template;
2414 counter = &dbin->opadcount;
2418 pad_name = g_strdup_printf ("%s_%u", prefix, *counter);
2420 ptmpl = gst_static_pad_template_get (templ);
2421 res->src_pad = gst_ghost_pad_new_no_target_from_template (pad_name, ptmpl);
2422 gst_object_unref (ptmpl);
2424 gst_pad_set_active (res->src_pad, TRUE);
2425 /* Put an event probe on the internal proxy pad to detect upstream
2428 (GstPad *) gst_proxy_pad_get_internal ((GstProxyPad *) res->src_pad);
2429 gst_pad_add_probe (internal_pad, GST_PAD_PROBE_TYPE_EVENT_UPSTREAM,
2430 (GstPadProbeCallback) ghost_pad_event_probe, res, NULL);
2431 gst_object_unref (internal_pad);
2433 dbin->output_streams = g_list_append (dbin->output_streams, res);
2439 free_output_stream (GstDecodebin3 * dbin, DecodebinOutputStream * output)
2442 if (output->decoder_sink && output->decoder)
2443 gst_pad_unlink (output->slot->src_pad, output->decoder_sink);
2445 output->slot->output = NULL;
2446 output->slot = NULL;
2448 gst_object_replace ((GstObject **) & output->decoder_sink, NULL);
2449 gst_ghost_pad_set_target ((GstGhostPad *) output->src_pad, NULL);
2450 gst_object_replace ((GstObject **) & output->decoder_src, NULL);
2451 if (output->src_exposed) {
2452 gst_element_remove_pad ((GstElement *) dbin, output->src_pad);
2454 if (output->decoder) {
2455 gst_element_set_locked_state (output->decoder, TRUE);
2456 gst_element_set_state (output->decoder, GST_STATE_NULL);
2457 gst_bin_remove ((GstBin *) dbin, output->decoder);
2462 static GstStateChangeReturn
2463 gst_decodebin3_change_state (GstElement * element, GstStateChange transition)
2465 GstDecodebin3 *dbin = (GstDecodebin3 *) element;
2466 GstStateChangeReturn ret;
2469 switch (transition) {
2473 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
2474 if (ret == GST_STATE_CHANGE_FAILURE)
2477 switch (transition) {
2478 case GST_STATE_CHANGE_PAUSED_TO_READY:
2482 /* Free output streams */
2483 for (tmp = dbin->output_streams; tmp; tmp = tmp->next) {
2484 DecodebinOutputStream *output = (DecodebinOutputStream *) tmp->data;
2485 free_output_stream (dbin, output);
2487 g_list_free (dbin->output_streams);
2488 dbin->output_streams = NULL;
2489 /* Free multiqueue slots */
2490 for (tmp = dbin->slots; tmp; tmp = tmp->next) {
2491 MultiQueueSlot *slot = (MultiQueueSlot *) tmp->data;
2492 free_multiqueue_slot (dbin, slot);
2494 g_list_free (dbin->slots);
2507 gst_decodebin3_plugin_init (GstPlugin * plugin)
2509 GST_DEBUG_CATEGORY_INIT (decodebin3_debug, "decodebin3", 0, "decoder bin");
2511 return gst_element_register (plugin, "decodebin3", GST_RANK_NONE,
2512 GST_TYPE_DECODEBIN3);