2 * Copyright (C) <2006> Edward Hervey <edward@fluendo.com>
3 * Copyright (C) <2009> Sebastian Dröge <sebastian.droege@collabora.co.uk>
4 * Copyright (C) <2011> Hewlett-Packard Development Company, L.P.
5 * Author: Sebastian Dröge <sebastian.droege@collabora.co.uk>, Collabora Ltd.
6 * Copyright (C) <2013> Collabora Ltd.
7 * Author: Sebastian Dröge <sebastian.droege@collabora.co.uk>
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Library General Public
11 * License as published by the Free Software Foundation; either
12 * version 2 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Library General Public License for more details.
19 * You should have received a copy of the GNU Library General Public
20 * License along with this library; if not, write to the
21 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
22 * Boston, MA 02110-1301, USA.
26 * SECTION:element-decodebin
29 * #GstBin that auto-magically constructs a decoding pipeline using available
30 * decoders and demuxers via auto-plugging.
32 * decodebin is considered stable now and replaces the old #decodebin element.
33 * #uridecodebin uses decodebin internally and is often more convenient to
34 * use, as it creates a suitable source element as well.
37 /* Implementation notes:
39 * The following section describes how decodebin works internally.
41 * The first part of decodebin is its typefind element, which tries
42 * to determine the media type of the input stream. If the type is found
43 * autoplugging starts.
45 * decodebin internally organizes the elements it autoplugged into GstDecodeChains
46 * and GstDecodeGroups. A decode chain is a single chain of decoding, this
47 * means that if decodebin every autoplugs an element with two+ srcpads
48 * (e.g. a demuxer) this will end the chain and everything following this
49 * demuxer will be put into decode groups below the chain. Otherwise,
50 * if an element has a single srcpad that outputs raw data the decode chain
51 * is ended too and a GstDecodePad is stored and blocked.
53 * A decode group combines a number of chains that are created by a
54 * demuxer element. All those chains are connected through a multiqueue to
55 * the demuxer. A new group for the same demuxer is only created if the
56 * demuxer has signaled no-more-pads, in which case all following pads
57 * create a new chain in the new group.
59 * This continues until the top-level decode chain is complete. A decode
60 * chain is complete if it either ends with a blocked endpad, if autoplugging
61 * stopped because no suitable plugins could be found or if the active group
62 * is complete. A decode group on the other hand is complete if all child
63 * chains are complete.
65 * If this happens at some point, all endpads of all active groups are exposed.
66 * For this decodebin adds the endpads, signals no-more-pads and then unblocks
67 * them. Now playback starts.
69 * If one of the chains that end on a endpad receives EOS decodebin checks
70 * if all chains and groups are drained. In that case everything goes into EOS.
71 * If there is a chain where the active group is drained but there exist next
72 * groups, the active group is hidden (endpads are removed) and the next group
73 * is exposed. This means that in some cases more pads may be created even
74 * after the initial no-more-pads signal. This happens for example with
75 * so-called "chained oggs", most commonly found among ogg/vorbis internet
78 * Note 1: If we're talking about blocked endpads this really means that the
79 * *target* pads of the endpads are blocked. Pads that are exposed to the outside
80 * should never ever be blocked!
82 * Note 2: If a group is complete and the parent's chain demuxer adds new pads
83 * but never signaled no-more-pads this additional pads will be ignored!
87 /* FIXME 0.11: suppress warnings for deprecated API such as GValueArray
88 * with newer GLib versions (>= 2.31.0) */
89 #define GLIB_DISABLE_DEPRECATION_WARNINGS
95 #include <gst/gst-i18n-plugin.h>
99 #include <gst/pbutils/pbutils.h>
101 #include "gstplay-enum.h"
102 #include "gstplaybackelements.h"
103 #include "gstrawcaps.h"
104 #include "gstplaybackutils.h"
106 /* generic templates */
107 static GstStaticPadTemplate decoder_bin_sink_template =
108 GST_STATIC_PAD_TEMPLATE ("sink",
111 GST_STATIC_CAPS_ANY);
113 static GstStaticPadTemplate decoder_bin_src_template =
114 GST_STATIC_PAD_TEMPLATE ("src_%u",
117 GST_STATIC_CAPS_ANY);
119 GST_DEBUG_CATEGORY_STATIC (gst_decode_bin_debug);
120 #define GST_CAT_DEFAULT gst_decode_bin_debug
122 typedef struct _GstPendingPad GstPendingPad;
123 typedef struct _GstDecodeElement GstDecodeElement;
124 typedef struct _GstDemuxerPad GstDemuxerPad;
125 typedef struct _GstDecodeChain GstDecodeChain;
126 typedef struct _GstDecodeGroup GstDecodeGroup;
127 typedef struct _GstDecodePad GstDecodePad;
128 typedef GstGhostPadClass GstDecodePadClass;
129 typedef struct _GstDecodeBin GstDecodeBin;
130 typedef struct _GstDecodeBinClass GstDecodeBinClass;
132 #define GST_TYPE_DECODE_BIN (gst_decode_bin_get_type())
133 #define GST_DECODE_BIN_CAST(obj) ((GstDecodeBin*)(obj))
134 #define GST_DECODE_BIN(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_DECODE_BIN,GstDecodeBin))
135 #define GST_DECODE_BIN_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_DECODE_BIN,GstDecodeBinClass))
136 #define GST_IS_DECODE_BIN(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_DECODE_BIN))
137 #define GST_IS_DECODE_BIN_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_DECODE_BIN))
142 * The opaque #GstDecodeBin data structure
146 GstBin bin; /* we extend GstBin */
149 GstCaps *caps; /* caps on which to stop decoding */
150 gchar *encoding; /* encoding of subtitles */
151 gboolean use_buffering; /* configure buffering on multiqueues */
152 gboolean force_sw_decoders;
155 guint max_size_bytes;
156 guint max_size_buffers;
157 guint64 max_size_time;
158 gboolean post_stream_topology;
159 guint64 connection_speed;
161 GstElement *typefind; /* this holds the typefind object */
163 GMutex expose_lock; /* Protects exposal and removal of groups */
164 GstDecodeChain *decode_chain; /* Top level decode chain */
165 guint nbpads; /* unique identifier for source pads */
167 GMutex factories_lock;
168 guint32 factories_cookie; /* Cookie from last time when factories was updated */
169 GList *factories; /* factories we can use for selecting elements */
171 GMutex subtitle_lock; /* Protects changes to subtitles and encoding */
172 GList *subtitles; /* List of elements with subtitle-encoding,
173 * protected by above mutex! */
175 gboolean have_type; /* if we received the have_type signal */
176 guint have_type_id; /* signal id for have-type from typefind */
178 gboolean async_pending; /* async-start has been emitted */
180 GMutex dyn_lock; /* lock protecting pad blocking */
181 gboolean shutdown; /* if we are shutting down */
182 GList *blocked_pads; /* pads that have set to block */
184 gboolean expose_allstreams; /* Whether to expose unknown type streams or not */
186 GList *filtered; /* elements for which error messages are filtered */
187 GList *filtered_errors; /* filtered error messages */
189 GList *buffering_status; /* element currently buffering messages */
190 GMutex buffering_lock;
191 GMutex buffering_post_lock;
193 GMutex cleanup_lock; /* Mutex used to protect the cleanup thread */
194 GThread *cleanup_thread; /* thread used to free chains asynchronously.
195 * We store it to make sure we end up joining it
196 * before stopping the element.
197 * Protected by the object lock */
198 GList *cleanup_groups; /* List of groups to free */
201 struct _GstDecodeBinClass
203 GstBinClass parent_class;
205 /* signal fired when we found a pad that we cannot decode */
206 void (*unknown_type) (GstElement * element, GstPad * pad, GstCaps * caps);
208 /* signal fired to know if we continue trying to decode the given caps */
209 gboolean (*autoplug_continue) (GstElement * element, GstPad * pad,
211 /* signal fired to get a list of factories to try to autoplug */
212 GValueArray *(*autoplug_factories) (GstElement * element, GstPad * pad,
214 /* signal fired to sort the factories */
215 GValueArray *(*autoplug_sort) (GstElement * element, GstPad * pad,
216 GstCaps * caps, GValueArray * factories);
217 /* signal fired to select from the proposed list of factories */
218 GstAutoplugSelectResult (*autoplug_select) (GstElement * element,
219 GstPad * pad, GstCaps * caps, GstElementFactory * factory);
220 /* signal fired when a autoplugged element that is not linked downstream
221 * or exposed wants to query something */
222 gboolean (*autoplug_query) (GstElement * element, GstPad * pad,
225 /* fired when the last group is drained */
226 void (*drained) (GstElement * element);
233 SIGNAL_AUTOPLUG_CONTINUE,
234 SIGNAL_AUTOPLUG_FACTORIES,
235 SIGNAL_AUTOPLUG_SELECT,
236 SIGNAL_AUTOPLUG_SORT,
237 SIGNAL_AUTOPLUG_QUERY,
242 /* automatic sizes, while prerolling we buffer up to 2MB, we ignore time
243 * and buffers in this case. */
244 #define AUTO_PREROLL_SIZE_BYTES 2 * 1024 * 1024
245 #define AUTO_PREROLL_SIZE_BUFFERS 0
246 #define AUTO_PREROLL_NOT_SEEKABLE_SIZE_TIME 10 * GST_SECOND
247 #define AUTO_PREROLL_SEEKABLE_SIZE_TIME 0
249 /* when playing, keep a max of 2MB of data but try to keep the number of buffers
250 * as low as possible (try to aim for 5 buffers) */
251 #define AUTO_PLAY_SIZE_BYTES 2 * 1024 * 1024
252 #define AUTO_PLAY_SIZE_BUFFERS 5
253 #define AUTO_PLAY_SIZE_TIME 0
255 #define DEFAULT_SUBTITLE_ENCODING NULL
256 #define DEFAULT_USE_BUFFERING FALSE
257 #define DEFAULT_FORCE_SW_DECODERS FALSE
258 #define DEFAULT_LOW_PERCENT 10
259 #define DEFAULT_HIGH_PERCENT 99
260 /* by default we use the automatic values above */
261 #define DEFAULT_MAX_SIZE_BYTES 0
262 #define DEFAULT_MAX_SIZE_BUFFERS 0
263 #define DEFAULT_MAX_SIZE_TIME 0
264 #define DEFAULT_POST_STREAM_TOPOLOGY FALSE
265 #define DEFAULT_EXPOSE_ALL_STREAMS TRUE
266 #define DEFAULT_CONNECTION_SPEED 0
273 PROP_SUBTITLE_ENCODING,
276 PROP_FORCE_SW_DECODERS,
280 PROP_MAX_SIZE_BUFFERS,
282 PROP_POST_STREAM_TOPOLOGY,
283 PROP_EXPOSE_ALL_STREAMS,
284 PROP_CONNECTION_SPEED
287 static GstBinClass *parent_class;
288 static guint gst_decode_bin_signals[LAST_SIGNAL] = { 0 };
290 static GstStaticCaps default_raw_caps = GST_STATIC_CAPS (DEFAULT_RAW_CAPS);
292 static void do_async_start (GstDecodeBin * dbin);
293 static void do_async_done (GstDecodeBin * dbin);
295 static void type_found (GstElement * typefind, guint probability,
296 GstCaps * caps, GstDecodeBin * decode_bin);
298 static void decodebin_set_queue_size (GstDecodeBin * dbin,
299 GstElement * multiqueue, gboolean preroll, gboolean seekable);
300 static void decodebin_set_queue_size_full (GstDecodeBin * dbin,
301 GstElement * multiqueue, gboolean use_buffering, gboolean preroll,
304 static gboolean gst_decode_bin_autoplug_continue (GstElement * element,
305 GstPad * pad, GstCaps * caps);
306 static GValueArray *gst_decode_bin_autoplug_factories (GstElement *
307 element, GstPad * pad, GstCaps * caps);
308 static GValueArray *gst_decode_bin_autoplug_sort (GstElement * element,
309 GstPad * pad, GstCaps * caps, GValueArray * factories);
310 static GstAutoplugSelectResult gst_decode_bin_autoplug_select (GstElement *
311 element, GstPad * pad, GstCaps * caps, GstElementFactory * factory);
312 static gboolean gst_decode_bin_autoplug_query (GstElement * element,
313 GstPad * pad, GstQuery * query);
315 static void gst_decode_bin_set_property (GObject * object, guint prop_id,
316 const GValue * value, GParamSpec * pspec);
317 static void gst_decode_bin_get_property (GObject * object, guint prop_id,
318 GValue * value, GParamSpec * pspec);
319 static void gst_decode_bin_set_caps (GstDecodeBin * dbin, GstCaps * caps);
320 static GstCaps *gst_decode_bin_get_caps (GstDecodeBin * dbin);
321 static void caps_notify_cb (GstPad * pad, GParamSpec * unused,
322 GstDecodeChain * chain);
324 static void flush_chain (GstDecodeChain * chain, gboolean flushing);
325 static void flush_group (GstDecodeGroup * group, gboolean flushing);
326 static GstPad *find_sink_pad (GstElement * element);
327 static GstStateChangeReturn gst_decode_bin_change_state (GstElement * element,
328 GstStateChange transition);
329 static void gst_decode_bin_handle_message (GstBin * bin, GstMessage * message);
330 static gboolean gst_decode_bin_remove_element (GstBin * bin,
331 GstElement * element);
333 static gboolean check_upstream_seekable (GstDecodeBin * dbin, GstPad * pad);
335 static GstCaps *get_pad_caps (GstPad * pad);
336 static void unblock_pads (GstDecodeBin * dbin);
338 #define EXPOSE_LOCK(dbin) G_STMT_START { \
339 GST_LOG_OBJECT (dbin, \
340 "expose locking from thread %p", \
342 g_mutex_lock (&GST_DECODE_BIN_CAST(dbin)->expose_lock); \
343 GST_LOG_OBJECT (dbin, \
344 "expose locked from thread %p", \
348 #define EXPOSE_UNLOCK(dbin) G_STMT_START { \
349 GST_LOG_OBJECT (dbin, \
350 "expose unlocking from thread %p", \
352 g_mutex_unlock (&GST_DECODE_BIN_CAST(dbin)->expose_lock); \
355 #define DYN_LOCK(dbin) G_STMT_START { \
356 GST_LOG_OBJECT (dbin, \
357 "dynlocking from thread %p", \
359 g_mutex_lock (&GST_DECODE_BIN_CAST(dbin)->dyn_lock); \
360 GST_LOG_OBJECT (dbin, \
361 "dynlocked from thread %p", \
365 #define DYN_UNLOCK(dbin) G_STMT_START { \
366 GST_LOG_OBJECT (dbin, \
367 "dynunlocking from thread %p", \
369 g_mutex_unlock (&GST_DECODE_BIN_CAST(dbin)->dyn_lock); \
372 #define SUBTITLE_LOCK(dbin) G_STMT_START { \
373 GST_LOG_OBJECT (dbin, \
374 "subtitle locking from thread %p", \
376 g_mutex_lock (&GST_DECODE_BIN_CAST(dbin)->subtitle_lock); \
377 GST_LOG_OBJECT (dbin, \
378 "subtitle lock from thread %p", \
382 #define SUBTITLE_UNLOCK(dbin) G_STMT_START { \
383 GST_LOG_OBJECT (dbin, \
384 "subtitle unlocking from thread %p", \
386 g_mutex_unlock (&GST_DECODE_BIN_CAST(dbin)->subtitle_lock); \
389 #define BUFFERING_LOCK(dbin) G_STMT_START { \
390 GST_LOG_OBJECT (dbin, \
391 "buffering locking from thread %p", \
393 g_mutex_lock (&GST_DECODE_BIN_CAST(dbin)->buffering_lock); \
394 GST_LOG_OBJECT (dbin, \
395 "buffering lock from thread %p", \
399 #define BUFFERING_UNLOCK(dbin) G_STMT_START { \
400 GST_LOG_OBJECT (dbin, \
401 "buffering unlocking from thread %p", \
403 g_mutex_unlock (&GST_DECODE_BIN_CAST(dbin)->buffering_lock); \
406 struct _GstPendingPad
409 GstDecodeChain *chain;
410 gulong event_probe_id;
411 gulong notify_caps_id;
414 struct _GstDecodeElement
417 GstElement *capsfilter; /* Optional capsfilter for Parser/Convert */
419 gulong pad_removed_id;
420 gulong no_more_pads_id;
423 struct _GstDemuxerPad
426 gulong event_probe_id;
427 gulong query_probe_id;
433 * Streams belonging to the same group/chain of a media file
435 * When changing something here lock the parent chain!
437 struct _GstDecodeGroup
440 GstDecodeChain *parent;
442 GstElement *multiqueue; /* Used for linking all child chains */
443 gulong overrunsig; /* the overrun signal for multiqueue */
445 gboolean overrun; /* TRUE if the multiqueue signaled overrun. This
446 * means that we should really expose the group */
448 gboolean no_more_pads; /* TRUE if the demuxer signaled no-more-pads */
449 gboolean drained; /* TRUE if the all children are drained */
451 GList *children; /* List of GstDecodeChains in this group */
452 GList *demuxer_pad_probe_ids;
454 GList *reqpads; /* List of RequestPads for multiqueue, there is
455 * exactly one RequestPad per child chain */
458 struct _GstDecodeChain
460 GstDecodeGroup *parent;
463 gint refs; /* Number of references to this object */
465 GMutex lock; /* Protects this chain and its groups */
467 GstPad *pad; /* srcpad that caused creation of this chain */
468 gulong pad_probe_id; /* id for the demuxer_source_pad_probe probe */
470 gboolean drained; /* TRUE if the all children are drained */
471 gboolean demuxer; /* TRUE if elements->data is a demuxer */
472 gboolean adaptive_demuxer; /* TRUE if elements->data is an adaptive streaming demuxer */
473 gboolean seekable; /* TRUE if this chain ends on a demuxer and is seekable */
474 GList *elements; /* All elements in this group, first
475 is the latest and most downstream element */
477 /* Note: there are only groups if the last element of this chain
478 * is a demuxer, otherwise the chain will end with an endpad.
479 * The other way around this means, that endpad only exists if this
480 * chain doesn't end with a demuxer! */
482 GstDecodeGroup *active_group; /* Currently active group */
483 GList *next_groups; /* head is newest group, tail is next group.
484 a new group will be created only if the head
485 group had no-more-pads. If it's only exposed
486 all new pads will be ignored! */
487 GList *pending_pads; /* Pads that have no fixed caps yet */
489 GstDecodePad *current_pad; /* Current ending pad of the chain that can't
490 * be exposed yet but would be the same as endpad
491 * once it can be exposed */
492 GstDecodePad *endpad; /* Pad of this chain that could be exposed */
493 gboolean deadend; /* This chain is incomplete and can't be completed,
494 e.g. no suitable decoder could be found
495 e.g. stream got EOS without buffers
497 gchar *deadend_details;
498 GstCaps *endcaps; /* Caps that were used when linking to the endpad
499 or that resulted in the deadend
502 /* FIXME: This should be done directly via a thread! */
503 GList *old_groups; /* Groups that should be freed later */
506 static GstDecodeChain *gst_decode_chain_ref (GstDecodeChain * chain);
507 static void gst_decode_chain_unref (GstDecodeChain * chain);
508 static void gst_decode_chain_free (GstDecodeChain * chain);
509 static GstDecodeChain *gst_decode_chain_new (GstDecodeBin * dbin,
510 GstDecodeGroup * group, GstPad * pad);
511 static void gst_decode_group_hide (GstDecodeGroup * group);
512 static void gst_decode_group_free (GstDecodeGroup * group);
513 static GstDecodeGroup *gst_decode_group_new (GstDecodeBin * dbin,
514 GstDecodeChain * chain);
515 static gboolean gst_decode_chain_is_complete (GstDecodeChain * chain);
516 static gboolean gst_decode_chain_expose (GstDecodeChain * chain,
517 GList ** endpads, gboolean * missing_plugin,
518 GString * missing_plugin_details, gboolean * last_group);
519 static gboolean gst_decode_chain_is_drained (GstDecodeChain * chain);
520 static gboolean gst_decode_chain_reset_buffering (GstDecodeChain * chain);
521 static gboolean gst_decode_group_is_complete (GstDecodeGroup * group);
522 static GstPad *gst_decode_group_control_demuxer_pad (GstDecodeGroup * group,
524 static gboolean gst_decode_group_is_drained (GstDecodeGroup * group);
525 static gboolean gst_decode_group_reset_buffering (GstDecodeGroup * group);
527 static gboolean gst_decode_bin_expose (GstDecodeBin * dbin);
528 static void gst_decode_bin_reset_buffering (GstDecodeBin * dbin);
530 #define CHAIN_MUTEX_LOCK(chain) G_STMT_START { \
531 GST_LOG_OBJECT (chain->dbin, \
532 "locking chain %p from thread %p", \
533 chain, g_thread_self ()); \
534 g_mutex_lock (&chain->lock); \
535 GST_LOG_OBJECT (chain->dbin, \
536 "locked chain %p from thread %p", \
537 chain, g_thread_self ()); \
540 #define CHAIN_MUTEX_UNLOCK(chain) G_STMT_START { \
541 GST_LOG_OBJECT (chain->dbin, \
542 "unlocking chain %p from thread %p", \
543 chain, g_thread_self ()); \
544 g_mutex_unlock (&chain->lock); \
549 * GstPad private used for source pads of chains
555 GstDecodeChain *chain;
557 gboolean blocked; /* the *target* pad is blocked */
558 gboolean exposed; /* the pad is exposed */
559 gboolean drained; /* an EOS has been seen on the pad */
564 GType gst_decode_pad_get_type (void);
565 G_DEFINE_TYPE (GstDecodePad, gst_decode_pad, GST_TYPE_GHOST_PAD);
566 #define GST_TYPE_DECODE_PAD (gst_decode_pad_get_type ())
567 #define GST_DECODE_PAD(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_DECODE_PAD,GstDecodePad))
569 static GstDecodePad *gst_decode_pad_new (GstDecodeBin * dbin,
570 GstDecodeChain * chain);
571 static void gst_decode_pad_activate (GstDecodePad * dpad,
572 GstDecodeChain * chain);
573 static void gst_decode_pad_unblock (GstDecodePad * dpad);
574 static void gst_decode_pad_set_blocked (GstDecodePad * dpad, gboolean blocked);
575 static gboolean gst_decode_pad_query (GstPad * pad, GstObject * parent,
577 static gboolean gst_decode_pad_is_exposable (GstDecodePad * endpad);
579 static void gst_pending_pad_free (GstPendingPad * ppad);
580 static GstPadProbeReturn pad_event_cb (GstPad * pad, GstPadProbeInfo * info,
583 /********************************
584 * Standard GObject boilerplate *
585 ********************************/
587 static void gst_decode_bin_dispose (GObject * object);
588 static void gst_decode_bin_finalize (GObject * object);
590 /* Register some quarks here for the stream topology message */
591 static GQuark topology_structure_name = 0;
592 static GQuark topology_caps = 0;
593 static GQuark topology_next = 0;
594 static GQuark topology_pad = 0;
595 static GQuark topology_element_srcpad = 0;
597 GType gst_decode_bin_get_type (void);
598 G_DEFINE_TYPE (GstDecodeBin, gst_decode_bin, GST_TYPE_BIN);
600 GST_DEBUG_CATEGORY_INIT (gst_decode_bin_debug, "decodebin", 0, "decoder bin");\
601 topology_structure_name = g_quark_from_static_string ("stream-topology"); \
602 topology_caps = g_quark_from_static_string ("caps");\
603 topology_next = g_quark_from_static_string ("next");\
604 topology_pad = g_quark_from_static_string ("pad");\
605 topology_element_srcpad = g_quark_from_static_string ("element-srcpad");\
606 playback_element_init (plugin);\
608 GST_ELEMENT_REGISTER_DEFINE_WITH_CODE (decodebin, "decodebin", GST_RANK_NONE,
609 GST_TYPE_DECODE_BIN, _do_init);
612 _gst_boolean_accumulator (GSignalInvocationHint * ihint,
613 GValue * return_accu, const GValue * handler_return, gpointer dummy)
617 myboolean = g_value_get_boolean (handler_return);
618 g_value_set_boolean (return_accu, myboolean);
620 /* stop emission if FALSE */
625 _gst_boolean_or_accumulator (GSignalInvocationHint * ihint,
626 GValue * return_accu, const GValue * handler_return, gpointer dummy)
631 myboolean = g_value_get_boolean (handler_return);
632 retboolean = g_value_get_boolean (return_accu);
634 g_value_set_boolean (return_accu, myboolean || retboolean);
639 /* we collect the first result */
641 _gst_array_accumulator (GSignalInvocationHint * ihint,
642 GValue * return_accu, const GValue * handler_return, gpointer dummy)
646 array = g_value_get_boxed (handler_return);
647 g_value_set_boxed (return_accu, array);
653 _gst_select_accumulator (GSignalInvocationHint * ihint,
654 GValue * return_accu, const GValue * handler_return, gpointer dummy)
656 GstAutoplugSelectResult res;
658 res = g_value_get_enum (handler_return);
659 g_value_set_enum (return_accu, res);
661 /* Call the next handler in the chain (if any) when the current callback
662 * returns TRY. This makes it possible to register separate autoplug-select
663 * handlers that implement different TRY/EXPOSE/SKIP strategies.
665 if (res == GST_AUTOPLUG_SELECT_TRY)
672 _gst_array_hasvalue_accumulator (GSignalInvocationHint * ihint,
673 GValue * return_accu, const GValue * handler_return, gpointer dummy)
677 array = g_value_get_boxed (handler_return);
678 g_value_set_boxed (return_accu, array);
687 gst_decode_bin_class_init (GstDecodeBinClass * klass)
689 GObjectClass *gobject_klass;
690 GstElementClass *gstelement_klass;
691 GstBinClass *gstbin_klass;
693 gobject_klass = (GObjectClass *) klass;
694 gstelement_klass = (GstElementClass *) klass;
695 gstbin_klass = (GstBinClass *) klass;
697 parent_class = g_type_class_peek_parent (klass);
699 gobject_klass->dispose = gst_decode_bin_dispose;
700 gobject_klass->finalize = gst_decode_bin_finalize;
701 gobject_klass->set_property = gst_decode_bin_set_property;
702 gobject_klass->get_property = gst_decode_bin_get_property;
705 * GstDecodeBin::unknown-type:
706 * @bin: The decodebin.
707 * @pad: The new pad containing caps that cannot be resolved to a 'final'
709 * @caps: The #GstCaps of the pad that cannot be resolved.
711 * This signal is emitted when a pad for which there is no further possible
712 * decoding is added to the decodebin.
714 gst_decode_bin_signals[SIGNAL_UNKNOWN_TYPE] =
715 g_signal_new ("unknown-type", G_TYPE_FROM_CLASS (klass),
716 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstDecodeBinClass, unknown_type),
717 NULL, NULL, NULL, G_TYPE_NONE, 2, GST_TYPE_PAD, GST_TYPE_CAPS);
720 * GstDecodeBin::autoplug-continue:
721 * @bin: The decodebin.
723 * @caps: The #GstCaps found.
725 * This signal is emitted whenever decodebin finds a new stream. It is
726 * emitted before looking for any elements that can handle that stream.
728 * > Invocation of signal handlers stops after the first signal handler
729 * > returns %FALSE. Signal handlers are invoked in the order they were
732 * Returns: %TRUE if you wish decodebin to look for elements that can
733 * handle the given @caps. If %FALSE, those caps will be considered as
734 * final and the pad will be exposed as such (see 'pad-added' signal of
737 gst_decode_bin_signals[SIGNAL_AUTOPLUG_CONTINUE] =
738 g_signal_new ("autoplug-continue", G_TYPE_FROM_CLASS (klass),
739 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstDecodeBinClass, autoplug_continue),
740 _gst_boolean_accumulator, NULL, NULL, G_TYPE_BOOLEAN, 2, GST_TYPE_PAD,
744 * GstDecodeBin::autoplug-factories:
745 * @bin: The decodebin.
747 * @caps: The #GstCaps found.
749 * This signal is emitted when an array of possible factories for @caps on
750 * @pad is needed. Decodebin will by default return an array with all
751 * compatible factories, sorted by rank.
753 * If this function returns NULL, @pad will be exposed as a final caps.
755 * If this function returns an empty array, the pad will be considered as
756 * having an unhandled type media type.
758 * > Only the signal handler that is connected first will ever by invoked.
759 * > Don't connect signal handlers with the #G_CONNECT_AFTER flag to this
760 * > signal, they will never be invoked!
762 * Returns: a #GValueArray* with a list of factories to try. The factories are
763 * by default tried in the returned order or based on the index returned by
766 gst_decode_bin_signals[SIGNAL_AUTOPLUG_FACTORIES] =
767 g_signal_new ("autoplug-factories", G_TYPE_FROM_CLASS (klass),
768 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstDecodeBinClass,
769 autoplug_factories), _gst_array_accumulator, NULL,
770 NULL, G_TYPE_VALUE_ARRAY, 2, GST_TYPE_PAD, GST_TYPE_CAPS);
773 * GstDecodeBin::autoplug-sort:
774 * @bin: The decodebin.
776 * @caps: The #GstCaps.
777 * @factories: A #GValueArray of possible #GstElementFactory to use.
779 * Once decodebin has found the possible #GstElementFactory objects to try
780 * for @caps on @pad, this signal is emitted. The purpose of the signal is for
781 * the application to perform additional sorting or filtering on the element
784 * The callee should copy and modify @factories or return %NULL if the
785 * order should not change.
787 * > Invocation of signal handlers stops after one signal handler has
788 * > returned something else than %NULL. Signal handlers are invoked in
789 * > the order they were connected in.
790 * > Don't connect signal handlers with the #G_CONNECT_AFTER flag to this
791 * > signal, they will never be invoked!
793 * Returns: A new sorted array of #GstElementFactory objects.
795 gst_decode_bin_signals[SIGNAL_AUTOPLUG_SORT] =
796 g_signal_new ("autoplug-sort", G_TYPE_FROM_CLASS (klass),
797 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstDecodeBinClass, autoplug_sort),
798 _gst_array_hasvalue_accumulator, NULL,
799 NULL, G_TYPE_VALUE_ARRAY, 3, GST_TYPE_PAD, GST_TYPE_CAPS,
800 G_TYPE_VALUE_ARRAY | G_SIGNAL_TYPE_STATIC_SCOPE);
803 * GstDecodeBin::autoplug-select:
804 * @bin: The decodebin.
806 * @caps: The #GstCaps.
807 * @factory: A #GstElementFactory to use.
809 * This signal is emitted once decodebin has found all the possible
810 * #GstElementFactory that can be used to handle the given @caps. For each of
811 * those factories, this signal is emitted.
813 * The signal handler should return a #GstAutoplugSelectResult enum
814 * value indicating what decodebin should do next.
816 * A value of #GstAutoplugSelectResult::try will try to autoplug an element from
819 * A value of #GstAutoplugSelectResult::expose will expose @pad without plugging
822 * A value of #GstAutoplugSelectResult::skip will skip @factory and move to the
825 * > The signal handler will not be invoked if any of the previously
826 * > registered signal handlers (if any) return a value other than
827 * > GST_AUTOPLUG_SELECT_TRY. Which also means that if you return
828 * > GST_AUTOPLUG_SELECT_TRY from one signal handler, handlers that get
829 * > registered next (again, if any) can override that decision.
831 * Returns: a #GstAutoplugSelectResult that indicates the required
832 * operation. the default handler will always return
833 * #GstAutoplugSelectResult::try.
835 gst_decode_bin_signals[SIGNAL_AUTOPLUG_SELECT] =
836 g_signal_new ("autoplug-select", G_TYPE_FROM_CLASS (klass),
837 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstDecodeBinClass, autoplug_select),
838 _gst_select_accumulator, NULL,
839 NULL, GST_TYPE_AUTOPLUG_SELECT_RESULT, 3, GST_TYPE_PAD, GST_TYPE_CAPS,
840 GST_TYPE_ELEMENT_FACTORY);
843 * GstDecodeBin::autoplug-query:
844 * @bin: The decodebin.
846 * @child: The child element doing the query
847 * @query: The #GstQuery.
849 * This signal is emitted whenever an autoplugged element that is
850 * not linked downstream yet and not exposed does a query. It can
851 * be used to tell the element about the downstream supported caps
854 * Returns: %TRUE if the query was handled, %FALSE otherwise.
856 gst_decode_bin_signals[SIGNAL_AUTOPLUG_QUERY] =
857 g_signal_new ("autoplug-query", G_TYPE_FROM_CLASS (klass),
858 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstDecodeBinClass, autoplug_query),
859 _gst_boolean_or_accumulator, NULL, NULL, G_TYPE_BOOLEAN, 3, GST_TYPE_PAD,
860 GST_TYPE_ELEMENT, GST_TYPE_QUERY | G_SIGNAL_TYPE_STATIC_SCOPE);
863 * GstDecodeBin::drained
864 * @bin: The decodebin
866 * This signal is emitted once decodebin has finished decoding all the data.
868 gst_decode_bin_signals[SIGNAL_DRAINED] =
869 g_signal_new ("drained", G_TYPE_FROM_CLASS (klass),
870 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstDecodeBinClass, drained),
871 NULL, NULL, NULL, G_TYPE_NONE, 0, G_TYPE_NONE);
873 g_object_class_install_property (gobject_klass, PROP_CAPS,
874 g_param_spec_boxed ("caps", "Caps", "The caps on which to stop decoding.",
875 GST_TYPE_CAPS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
877 g_object_class_install_property (gobject_klass, PROP_SUBTITLE_ENCODING,
878 g_param_spec_string ("subtitle-encoding", "subtitle encoding",
879 "Encoding to assume if input subtitles are not in UTF-8 encoding. "
880 "If not set, the GST_SUBTITLE_ENCODING environment variable will "
881 "be checked for an encoding to use. If that is not set either, "
882 "ISO-8859-15 will be assumed.", NULL,
883 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
885 g_object_class_install_property (gobject_klass, PROP_SINK_CAPS,
886 g_param_spec_boxed ("sink-caps", "Sink Caps",
887 "The caps of the input data. (NULL = use typefind element)",
888 GST_TYPE_CAPS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
891 * GstDecodeBin::use-buffering
893 * Activate buffering in decodebin. This will instruct the multiqueues behind
894 * decoders to emit BUFFERING messages.
896 g_object_class_install_property (gobject_klass, PROP_USE_BUFFERING,
897 g_param_spec_boolean ("use-buffering", "Use Buffering",
898 "Emit GST_MESSAGE_BUFFERING based on low-/high-percent thresholds",
899 DEFAULT_USE_BUFFERING, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
902 * GstDecodeBin::force-sw-decoders:
904 * While auto-plugging, if set to %TRUE, those decoders within
905 * "Hardware" klass will be ignored. Otherwise they will be tried.
909 g_object_class_install_property (gobject_klass, PROP_FORCE_SW_DECODERS,
910 g_param_spec_boolean ("force-sw-decoders", "Software Docoders Only",
911 "Use only sofware decoders to process streams",
912 DEFAULT_FORCE_SW_DECODERS,
913 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
916 * GstDecodeBin:low-percent
918 * Low threshold percent for buffering to start.
920 g_object_class_install_property (gobject_klass, PROP_LOW_PERCENT,
921 g_param_spec_int ("low-percent", "Low percent",
922 "Low threshold for buffering to start", 0, 100,
923 DEFAULT_LOW_PERCENT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
925 * GstDecodeBin:high-percent
927 * High threshold percent for buffering to finish.
929 g_object_class_install_property (gobject_klass, PROP_HIGH_PERCENT,
930 g_param_spec_int ("high-percent", "High percent",
931 "High threshold for buffering to finish", 0, 100,
932 DEFAULT_HIGH_PERCENT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
935 * GstDecodeBin:max-size-bytes
937 * Max amount of bytes in the queue (0=automatic).
939 g_object_class_install_property (gobject_klass, PROP_MAX_SIZE_BYTES,
940 g_param_spec_uint ("max-size-bytes", "Max. size (bytes)",
941 "Max. amount of bytes in the queue (0=automatic)",
942 0, G_MAXUINT, DEFAULT_MAX_SIZE_BYTES,
943 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
945 * GstDecodeBin:max-size-buffers
947 * Max amount of buffers in the queue (0=automatic).
949 g_object_class_install_property (gobject_klass, PROP_MAX_SIZE_BUFFERS,
950 g_param_spec_uint ("max-size-buffers", "Max. size (buffers)",
951 "Max. number of buffers in the queue (0=automatic)",
952 0, G_MAXUINT, DEFAULT_MAX_SIZE_BUFFERS,
953 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
955 * GstDecodeBin:max-size-time
957 * Max amount of time in the queue (in ns, 0=automatic).
959 g_object_class_install_property (gobject_klass, PROP_MAX_SIZE_TIME,
960 g_param_spec_uint64 ("max-size-time", "Max. size (ns)",
961 "Max. amount of data in the queue (in ns, 0=automatic)",
963 DEFAULT_MAX_SIZE_TIME, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
966 * GstDecodeBin::post-stream-topology
968 * Post stream-topology messages on the bus every time the topology changes.
970 g_object_class_install_property (gobject_klass, PROP_POST_STREAM_TOPOLOGY,
971 g_param_spec_boolean ("post-stream-topology", "Post Stream Topology",
972 "Post stream-topology messages",
973 DEFAULT_POST_STREAM_TOPOLOGY,
974 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
977 * GstDecodeBin::expose-all-streams
979 * Expose streams of unknown type.
981 * If set to %FALSE, then only the streams that can be decoded to the final
982 * caps (see 'caps' property) will have a pad exposed. Streams that do not
983 * match those caps but could have been decoded will not have decoder plugged
984 * in internally and will not have a pad exposed.
986 g_object_class_install_property (gobject_klass, PROP_EXPOSE_ALL_STREAMS,
987 g_param_spec_boolean ("expose-all-streams", "Expose All Streams",
988 "Expose all streams, including those of unknown type or that don't match the 'caps' property",
989 DEFAULT_EXPOSE_ALL_STREAMS,
990 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
993 * GstDecodeBin2::connection-speed
995 * Network connection speed in kbps (0 = unknownw)
997 g_object_class_install_property (gobject_klass, PROP_CONNECTION_SPEED,
998 g_param_spec_uint64 ("connection-speed", "Connection Speed",
999 "Network connection speed in kbps (0 = unknown)",
1000 0, G_MAXUINT64 / 1000, DEFAULT_CONNECTION_SPEED,
1001 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1005 klass->autoplug_continue =
1006 GST_DEBUG_FUNCPTR (gst_decode_bin_autoplug_continue);
1007 klass->autoplug_factories =
1008 GST_DEBUG_FUNCPTR (gst_decode_bin_autoplug_factories);
1009 klass->autoplug_sort = GST_DEBUG_FUNCPTR (gst_decode_bin_autoplug_sort);
1010 klass->autoplug_select = GST_DEBUG_FUNCPTR (gst_decode_bin_autoplug_select);
1011 klass->autoplug_query = GST_DEBUG_FUNCPTR (gst_decode_bin_autoplug_query);
1013 gst_element_class_add_static_pad_template (gstelement_klass,
1014 &decoder_bin_sink_template);
1015 gst_element_class_add_static_pad_template (gstelement_klass,
1016 &decoder_bin_src_template);
1018 gst_element_class_set_static_metadata (gstelement_klass,
1019 "Decoder Bin", "Generic/Bin/Decoder",
1020 "Autoplug and decode to raw media",
1021 "Edward Hervey <edward.hervey@collabora.co.uk>, "
1022 "Sebastian Dröge <sebastian.droege@collabora.co.uk>");
1024 gstelement_klass->change_state =
1025 GST_DEBUG_FUNCPTR (gst_decode_bin_change_state);
1027 gstbin_klass->handle_message =
1028 GST_DEBUG_FUNCPTR (gst_decode_bin_handle_message);
1030 gstbin_klass->remove_element =
1031 GST_DEBUG_FUNCPTR (gst_decode_bin_remove_element);
1033 g_type_class_ref (GST_TYPE_DECODE_PAD);
1035 gst_type_mark_as_plugin_api (GST_TYPE_AUTOPLUG_SELECT_RESULT, 0);
1038 /* Must be called with factories lock! */
1040 gst_decode_bin_update_factories_list (GstDecodeBin * dbin)
1043 GList *factories, *tmp;
1045 cookie = gst_registry_get_feature_list_cookie (gst_registry_get ());
1046 if (!dbin->factories || dbin->factories_cookie != cookie) {
1047 if (dbin->factories)
1048 gst_plugin_feature_list_free (dbin->factories);
1050 gst_element_factory_list_get_elements
1051 (GST_ELEMENT_FACTORY_TYPE_DECODABLE, GST_RANK_MARGINAL);
1053 if (dbin->force_sw_decoders) {
1054 /* filter out Hardware class elements */
1055 dbin->factories = NULL;
1056 for (tmp = factories; tmp; tmp = g_list_next (tmp)) {
1057 GstElementFactory *factory = GST_ELEMENT_FACTORY_CAST (tmp->data);
1058 if (!gst_element_factory_list_is_type (factory,
1059 GST_ELEMENT_FACTORY_TYPE_HARDWARE)) {
1060 dbin->factories = g_list_prepend (dbin->factories, factory);
1062 gst_object_unref (factory);
1065 g_list_free (factories);
1067 dbin->factories = factories;
1071 g_list_sort (dbin->factories,
1072 gst_playback_utils_compare_factories_func);
1073 dbin->factories_cookie = cookie;
1078 gst_decode_bin_init (GstDecodeBin * decode_bin)
1080 /* first filter out the interesting element factories */
1081 g_mutex_init (&decode_bin->factories_lock);
1083 /* we create the typefind element only once */
1084 decode_bin->typefind = gst_element_factory_make ("typefind", "typefind");
1085 if (!decode_bin->typefind) {
1086 g_warning ("can't find typefind element, decodebin will not work");
1090 GstPadTemplate *pad_tmpl;
1092 /* add the typefind element */
1093 if (!gst_bin_add (GST_BIN (decode_bin), decode_bin->typefind)) {
1094 g_warning ("Could not add typefind element, decodebin will not work");
1095 gst_object_unref (decode_bin->typefind);
1096 decode_bin->typefind = NULL;
1099 /* get the sinkpad */
1100 pad = gst_element_get_static_pad (decode_bin->typefind, "sink");
1102 /* get the pad template */
1103 pad_tmpl = gst_static_pad_template_get (&decoder_bin_sink_template);
1105 /* ghost the sink pad to ourself */
1106 gpad = gst_ghost_pad_new_from_template ("sink", pad, pad_tmpl);
1107 gst_pad_set_active (gpad, TRUE);
1108 gst_element_add_pad (GST_ELEMENT (decode_bin), gpad);
1110 gst_object_unref (pad_tmpl);
1111 gst_object_unref (pad);
1114 g_mutex_init (&decode_bin->expose_lock);
1115 decode_bin->decode_chain = NULL;
1117 g_mutex_init (&decode_bin->dyn_lock);
1118 decode_bin->shutdown = FALSE;
1119 decode_bin->blocked_pads = NULL;
1121 g_mutex_init (&decode_bin->subtitle_lock);
1122 g_mutex_init (&decode_bin->buffering_lock);
1123 g_mutex_init (&decode_bin->buffering_post_lock);
1125 g_mutex_init (&decode_bin->cleanup_lock);
1126 decode_bin->cleanup_thread = NULL;
1128 decode_bin->encoding = g_strdup (DEFAULT_SUBTITLE_ENCODING);
1129 decode_bin->caps = gst_static_caps_get (&default_raw_caps);
1130 decode_bin->use_buffering = DEFAULT_USE_BUFFERING;
1131 decode_bin->force_sw_decoders = DEFAULT_FORCE_SW_DECODERS;
1132 decode_bin->low_percent = DEFAULT_LOW_PERCENT;
1133 decode_bin->high_percent = DEFAULT_HIGH_PERCENT;
1135 decode_bin->max_size_bytes = DEFAULT_MAX_SIZE_BYTES;
1136 decode_bin->max_size_buffers = DEFAULT_MAX_SIZE_BUFFERS;
1137 decode_bin->max_size_time = DEFAULT_MAX_SIZE_TIME;
1139 decode_bin->expose_allstreams = DEFAULT_EXPOSE_ALL_STREAMS;
1140 decode_bin->connection_speed = DEFAULT_CONNECTION_SPEED;
1144 gst_decode_bin_dispose (GObject * object)
1146 GstDecodeBin *decode_bin;
1148 decode_bin = GST_DECODE_BIN (object);
1150 if (decode_bin->factories)
1151 gst_plugin_feature_list_free (decode_bin->factories);
1152 decode_bin->factories = NULL;
1154 if (decode_bin->decode_chain)
1155 gst_decode_chain_free (decode_bin->decode_chain);
1156 decode_bin->decode_chain = NULL;
1158 if (decode_bin->caps)
1159 gst_caps_unref (decode_bin->caps);
1160 decode_bin->caps = NULL;
1162 g_free (decode_bin->encoding);
1163 decode_bin->encoding = NULL;
1165 g_list_free (decode_bin->subtitles);
1166 decode_bin->subtitles = NULL;
1168 unblock_pads (decode_bin);
1170 G_OBJECT_CLASS (parent_class)->dispose (object);
1174 gst_decode_bin_finalize (GObject * object)
1176 GstDecodeBin *decode_bin;
1178 decode_bin = GST_DECODE_BIN (object);
1180 g_mutex_clear (&decode_bin->expose_lock);
1181 g_mutex_clear (&decode_bin->dyn_lock);
1182 g_mutex_clear (&decode_bin->subtitle_lock);
1183 g_mutex_clear (&decode_bin->buffering_lock);
1184 g_mutex_clear (&decode_bin->buffering_post_lock);
1185 g_mutex_clear (&decode_bin->factories_lock);
1186 g_mutex_clear (&decode_bin->cleanup_lock);
1188 G_OBJECT_CLASS (parent_class)->finalize (object);
1192 * Changes the caps on which decodebin will stop decoding.
1193 * Will unref the previously set one. The refcount of the given caps will be
1195 * @caps can be NULL.
1200 gst_decode_bin_set_caps (GstDecodeBin * dbin, GstCaps * caps)
1202 GST_DEBUG_OBJECT (dbin, "Setting new caps: %" GST_PTR_FORMAT, caps);
1204 GST_OBJECT_LOCK (dbin);
1205 gst_caps_replace (&dbin->caps, caps);
1206 GST_OBJECT_UNLOCK (dbin);
1210 * Returns the currently configured caps on which decodebin will stop decoding.
1211 * The returned caps (if not NULL), will have its refcount incremented.
1216 gst_decode_bin_get_caps (GstDecodeBin * dbin)
1220 GST_DEBUG_OBJECT (dbin, "Getting currently set caps");
1222 GST_OBJECT_LOCK (dbin);
1225 gst_caps_ref (caps);
1226 GST_OBJECT_UNLOCK (dbin);
1232 gst_decode_bin_set_sink_caps (GstDecodeBin * dbin, GstCaps * caps)
1234 GST_DEBUG_OBJECT (dbin, "Setting new caps: %" GST_PTR_FORMAT, caps);
1236 g_object_set (dbin->typefind, "force-caps", caps, NULL);
1240 gst_decode_bin_get_sink_caps (GstDecodeBin * dbin)
1244 GST_DEBUG_OBJECT (dbin, "Getting currently set caps");
1246 g_object_get (dbin->typefind, "force-caps", &caps, NULL);
1252 gst_decode_bin_set_subs_encoding (GstDecodeBin * dbin, const gchar * encoding)
1256 GST_DEBUG_OBJECT (dbin, "Setting new encoding: %s", GST_STR_NULL (encoding));
1258 SUBTITLE_LOCK (dbin);
1259 g_free (dbin->encoding);
1260 dbin->encoding = g_strdup (encoding);
1262 /* set the subtitle encoding on all added elements */
1263 for (walk = dbin->subtitles; walk; walk = g_list_next (walk)) {
1264 g_object_set (G_OBJECT (walk->data), "subtitle-encoding", dbin->encoding,
1267 SUBTITLE_UNLOCK (dbin);
1271 gst_decode_bin_get_subs_encoding (GstDecodeBin * dbin)
1275 GST_DEBUG_OBJECT (dbin, "Getting currently set encoding");
1277 SUBTITLE_LOCK (dbin);
1278 encoding = g_strdup (dbin->encoding);
1279 SUBTITLE_UNLOCK (dbin);
1285 gst_decode_bin_set_property (GObject * object, guint prop_id,
1286 const GValue * value, GParamSpec * pspec)
1290 dbin = GST_DECODE_BIN (object);
1294 gst_decode_bin_set_caps (dbin, g_value_get_boxed (value));
1296 case PROP_SUBTITLE_ENCODING:
1297 gst_decode_bin_set_subs_encoding (dbin, g_value_get_string (value));
1299 case PROP_SINK_CAPS:
1300 gst_decode_bin_set_sink_caps (dbin, g_value_get_boxed (value));
1302 case PROP_USE_BUFFERING:
1303 dbin->use_buffering = g_value_get_boolean (value);
1305 case PROP_FORCE_SW_DECODERS:
1306 dbin->force_sw_decoders = g_value_get_boolean (value);
1308 case PROP_LOW_PERCENT:
1309 dbin->low_percent = g_value_get_int (value);
1311 case PROP_HIGH_PERCENT:
1312 dbin->high_percent = g_value_get_int (value);
1314 case PROP_MAX_SIZE_BYTES:
1315 dbin->max_size_bytes = g_value_get_uint (value);
1317 case PROP_MAX_SIZE_BUFFERS:
1318 dbin->max_size_buffers = g_value_get_uint (value);
1320 case PROP_MAX_SIZE_TIME:
1321 dbin->max_size_time = g_value_get_uint64 (value);
1323 case PROP_POST_STREAM_TOPOLOGY:
1324 dbin->post_stream_topology = g_value_get_boolean (value);
1326 case PROP_EXPOSE_ALL_STREAMS:
1327 dbin->expose_allstreams = g_value_get_boolean (value);
1329 case PROP_CONNECTION_SPEED:
1330 GST_OBJECT_LOCK (dbin);
1331 dbin->connection_speed = g_value_get_uint64 (value) * 1000;
1332 GST_OBJECT_UNLOCK (dbin);
1335 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1341 gst_decode_bin_get_property (GObject * object, guint prop_id,
1342 GValue * value, GParamSpec * pspec)
1346 dbin = GST_DECODE_BIN (object);
1349 g_value_take_boxed (value, gst_decode_bin_get_caps (dbin));
1351 case PROP_SUBTITLE_ENCODING:
1352 g_value_take_string (value, gst_decode_bin_get_subs_encoding (dbin));
1354 case PROP_SINK_CAPS:
1355 g_value_take_boxed (value, gst_decode_bin_get_sink_caps (dbin));
1357 case PROP_USE_BUFFERING:
1358 g_value_set_boolean (value, dbin->use_buffering);
1360 case PROP_FORCE_SW_DECODERS:
1361 g_value_set_boolean (value, dbin->force_sw_decoders);
1363 case PROP_LOW_PERCENT:
1364 g_value_set_int (value, dbin->low_percent);
1366 case PROP_HIGH_PERCENT:
1367 g_value_set_int (value, dbin->high_percent);
1369 case PROP_MAX_SIZE_BYTES:
1370 g_value_set_uint (value, dbin->max_size_bytes);
1372 case PROP_MAX_SIZE_BUFFERS:
1373 g_value_set_uint (value, dbin->max_size_buffers);
1375 case PROP_MAX_SIZE_TIME:
1376 g_value_set_uint64 (value, dbin->max_size_time);
1378 case PROP_POST_STREAM_TOPOLOGY:
1379 g_value_set_boolean (value, dbin->post_stream_topology);
1381 case PROP_EXPOSE_ALL_STREAMS:
1382 g_value_set_boolean (value, dbin->expose_allstreams);
1384 case PROP_CONNECTION_SPEED:
1385 GST_OBJECT_LOCK (dbin);
1386 g_value_set_uint64 (value, dbin->connection_speed / 1000);
1387 GST_OBJECT_UNLOCK (dbin);
1390 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1397 * Default autoplug signal handlers
1400 gst_decode_bin_autoplug_continue (GstElement * element, GstPad * pad,
1403 GST_DEBUG_OBJECT (element, "autoplug-continue returns TRUE");
1405 /* by default we always continue */
1409 static GValueArray *
1410 gst_decode_bin_autoplug_factories (GstElement * element, GstPad * pad,
1414 GValueArray *result;
1415 GstDecodeBin *dbin = GST_DECODE_BIN_CAST (element);
1417 GST_DEBUG_OBJECT (element, "finding factories");
1419 /* return all compatible factories for caps */
1420 g_mutex_lock (&dbin->factories_lock);
1421 gst_decode_bin_update_factories_list (dbin);
1423 gst_element_factory_list_filter (dbin->factories, caps, GST_PAD_SINK,
1424 gst_caps_is_fixed (caps));
1425 g_mutex_unlock (&dbin->factories_lock);
1427 result = g_value_array_new (g_list_length (list));
1428 for (tmp = list; tmp; tmp = tmp->next) {
1429 GstElementFactory *factory = GST_ELEMENT_FACTORY_CAST (tmp->data);
1430 GValue val = { 0, };
1432 g_value_init (&val, G_TYPE_OBJECT);
1433 g_value_set_object (&val, factory);
1434 g_value_array_append (result, &val);
1435 g_value_unset (&val);
1437 gst_plugin_feature_list_free (list);
1439 GST_DEBUG_OBJECT (element, "autoplug-factories returns %p", result);
1444 static GValueArray *
1445 gst_decode_bin_autoplug_sort (GstElement * element, GstPad * pad,
1446 GstCaps * caps, GValueArray * factories)
1451 static GstAutoplugSelectResult
1452 gst_decode_bin_autoplug_select (GstElement * element, GstPad * pad,
1453 GstCaps * caps, GstElementFactory * factory)
1455 GST_DEBUG_OBJECT (element, "default autoplug-select returns TRY");
1458 return GST_AUTOPLUG_SELECT_TRY;
1462 gst_decode_bin_autoplug_query (GstElement * element, GstPad * pad,
1465 /* No query handled here */
1473 static gboolean are_final_caps (GstDecodeBin * dbin, GstCaps * caps);
1474 static gboolean is_demuxer_element (GstElement * srcelement);
1475 static gboolean is_adaptive_demuxer_element (GstElement * srcelement);
1477 static gboolean connect_pad (GstDecodeBin * dbin, GstElement * src,
1478 GstDecodePad * dpad, GstPad * pad, GstCaps * caps, GValueArray * factories,
1479 GstDecodeChain * chain, gchar ** deadend_details);
1480 static GList *connect_element (GstDecodeBin * dbin, GstDecodeElement * delem,
1481 GstDecodeChain * chain);
1482 static void expose_pad (GstDecodeBin * dbin, GstElement * src,
1483 GstDecodePad * dpad, GstPad * pad, GstCaps * caps, GstDecodeChain * chain);
1485 static void pad_added_cb (GstElement * element, GstPad * pad,
1486 GstDecodeChain * chain);
1487 static void pad_removed_cb (GstElement * element, GstPad * pad,
1488 GstDecodeChain * chain);
1489 static void no_more_pads_cb (GstElement * element, GstDecodeChain * chain);
1491 static GstDecodeGroup *gst_decode_chain_get_current_group (GstDecodeChain *
1495 clear_sticky_events (GstPad * pad, GstEvent ** event, gpointer user_data)
1497 GST_DEBUG_OBJECT (pad, "clearing sticky event %" GST_PTR_FORMAT, *event);
1498 gst_event_unref (*event);
1504 copy_sticky_events (GstPad * pad, GstEvent ** event, gpointer user_data)
1506 GstPad *gpad = GST_PAD_CAST (user_data);
1508 GST_DEBUG_OBJECT (gpad, "store sticky event %" GST_PTR_FORMAT, *event);
1509 gst_pad_store_sticky_event (gpad, *event);
1515 decode_pad_set_target (GstDecodePad * dpad, GstPad * target)
1517 gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (dpad), target);
1519 gst_pad_sticky_events_foreach (GST_PAD_CAST (dpad), clear_sticky_events,
1522 gst_pad_sticky_events_foreach (target, copy_sticky_events, dpad);
1525 /* called when a new pad is discovered. It will perform some basic actions
1526 * before trying to link something to it.
1528 * - Check the caps, don't do anything when there are no caps or when they have
1530 * - signal AUTOPLUG_CONTINUE to check if we need to continue autoplugging this
1532 * - if the caps are non-fixed, setup a handler to continue autoplugging when
1533 * the caps become fixed (connect to notify::caps).
1534 * - get list of factories to autoplug.
1535 * - continue autoplugging to one of the factories.
1537 /* returns whether to expose the pad */
1539 analyze_new_pad (GstDecodeBin * dbin, GstElement * src, GstPad * pad,
1540 GstCaps * caps, GstDecodeChain * chain, GstDecodeChain ** new_chain)
1542 gboolean apcontinue = TRUE;
1543 GValueArray *factories = NULL, *result = NULL;
1545 GstElementFactory *factory;
1546 const gchar *classification;
1547 gboolean is_parser_converter = FALSE;
1549 gchar *deadend_details = NULL;
1551 GST_DEBUG_OBJECT (dbin, "Pad %s:%s caps:%" GST_PTR_FORMAT,
1552 GST_DEBUG_PAD_NAME (pad), caps);
1558 && src != ((GstDecodeElement *) chain->elements->data)->element
1559 && src != ((GstDecodeElement *) chain->elements->data)->capsfilter) {
1560 GST_ERROR_OBJECT (dbin, "New pad from not the last element in this chain");
1564 if (chain->endpad) {
1565 GST_ERROR_OBJECT (dbin, "New pad in a chain that is already complete");
1569 if (chain->demuxer) {
1570 GstDecodeGroup *group;
1571 GstDecodeChain *oldchain = chain;
1572 GstDecodeElement *demux = (chain->elements ? chain->elements->data : NULL);
1574 if (chain->current_pad)
1575 gst_object_unref (chain->current_pad);
1576 chain->current_pad = NULL;
1578 /* we are adding a new pad for a demuxer (see is_demuxer_element(),
1579 * start a new chain for it */
1580 CHAIN_MUTEX_LOCK (oldchain);
1581 group = gst_decode_chain_get_current_group (chain);
1582 if (group && !g_list_find (group->children, chain)) {
1583 g_assert (new_chain != NULL);
1584 *new_chain = chain = gst_decode_chain_new (dbin, group, pad);
1585 group->children = g_list_prepend (group->children, chain);
1587 CHAIN_MUTEX_UNLOCK (oldchain);
1589 GST_WARNING_OBJECT (dbin, "No current group");
1593 /* If this is not a dynamic pad demuxer, we're no-more-pads
1594 * already before anything else happens
1596 if (demux == NULL || !demux->no_more_pads_id)
1597 group->no_more_pads = TRUE;
1600 /* From here on we own a reference to the caps as
1601 * we might create new caps below and would need
1602 * to unref them later */
1604 gst_caps_ref (caps);
1606 if ((caps == NULL) || gst_caps_is_empty (caps))
1609 if (gst_caps_is_any (caps))
1612 if (!chain->current_pad)
1613 chain->current_pad = gst_decode_pad_new (dbin, chain);
1615 dpad = gst_object_ref (chain->current_pad);
1616 gst_pad_set_active (GST_PAD_CAST (dpad), TRUE);
1617 decode_pad_set_target (dpad, pad);
1619 /* 1. Emit 'autoplug-continue' the result will tell us if this pads needs
1620 * further autoplugging. Only do this for fixed caps, for unfixed caps
1621 * we will later come here again from the notify::caps handler. The
1622 * problem with unfixed caps is that, we can't reliably tell if the output
1623 * is e.g. accepted by a sink because only parts of the possible final
1624 * caps might be accepted by the sink. */
1625 if (gst_caps_is_fixed (caps))
1626 g_signal_emit (G_OBJECT (dbin),
1627 gst_decode_bin_signals[SIGNAL_AUTOPLUG_CONTINUE], 0, dpad, caps,
1632 /* 1.a if autoplug-continue is FALSE or caps is a raw format, goto pad_is_final */
1633 if ((!apcontinue) || are_final_caps (dbin, caps))
1636 /* 1.b For Parser/Converter that can output different stream formats
1637 * we insert a capsfilter with the sorted caps of all possible next
1638 * elements and continue with the capsfilter srcpad */
1639 factory = gst_element_get_factory (src);
1641 gst_element_factory_get_metadata (factory, GST_ELEMENT_METADATA_KLASS);
1642 is_parser_converter = (strstr (classification, "Parser")
1643 && strstr (classification, "Converter"));
1645 /* 1.c when the caps are not fixed yet, we can't be sure what element to
1646 * connect. We delay autoplugging until the caps are fixed */
1647 if (!is_parser_converter && !gst_caps_is_fixed (caps)) {
1649 } else if (!is_parser_converter) {
1650 gst_caps_unref (caps);
1651 caps = gst_pad_get_current_caps (pad);
1653 GST_DEBUG_OBJECT (dbin, "No final caps set yet, delaying autoplugging");
1654 gst_object_unref (dpad);
1655 goto setup_caps_delay;
1659 /* 1.d else get the factories and if there's no compatible factory goto
1661 g_signal_emit (G_OBJECT (dbin),
1662 gst_decode_bin_signals[SIGNAL_AUTOPLUG_FACTORIES], 0, dpad, caps,
1665 /* NULL means that we can expose the pad */
1666 if (factories == NULL)
1669 /* if the array is empty, we have a type for which we have no decoder */
1670 if (factories->n_values == 0) {
1671 if (!dbin->expose_allstreams) {
1672 GstCaps *raw = gst_static_caps_get (&default_raw_caps);
1674 /* If the caps are raw, this just means we don't want to expose them */
1675 if (gst_caps_is_subset (caps, raw)) {
1676 g_value_array_free (factories);
1677 gst_caps_unref (raw);
1678 gst_object_unref (dpad);
1679 goto discarded_type;
1681 gst_caps_unref (raw);
1684 /* if not we have a unhandled type with no compatible factories */
1685 g_value_array_free (factories);
1686 gst_object_unref (dpad);
1690 /* 1.e sort some more. */
1691 g_signal_emit (G_OBJECT (dbin),
1692 gst_decode_bin_signals[SIGNAL_AUTOPLUG_SORT], 0, dpad, caps, factories,
1695 g_value_array_free (factories);
1699 /* At this point we have a potential decoder, but we might not need it
1700 * if it doesn't match the output caps */
1701 if (!dbin->expose_allstreams && gst_caps_is_fixed (caps)) {
1704 gboolean dontuse = FALSE;
1706 GST_DEBUG ("Checking if we can abort early");
1708 /* 1.f Do an early check to see if the candidates are potential decoders, but
1709 * due to the fact that they decode to a mediatype that is not final we don't
1712 for (i = 0; i < factories->n_values && !dontuse; i++) {
1713 GstElementFactory *factory =
1714 g_value_get_object (g_value_array_get_nth (factories, i));
1717 /* We are only interested in skipping decoders */
1718 if (strstr (gst_element_factory_get_metadata (factory,
1719 GST_ELEMENT_METADATA_KLASS), "Decoder")) {
1721 GST_DEBUG ("Trying factory %s",
1722 gst_plugin_feature_get_name (GST_PLUGIN_FEATURE (factory)));
1724 /* Check the source pad template caps to see if they match raw caps but don't match
1726 for (tmps = gst_element_factory_get_static_pad_templates (factory);
1727 tmps && !dontuse; tmps = tmps->next) {
1728 GstStaticPadTemplate *st = (GstStaticPadTemplate *) tmps->data;
1729 if (st->direction != GST_PAD_SRC)
1731 tcaps = gst_static_pad_template_get_caps (st);
1735 /* Emit autoplug-continue to see if the caps are considered to be raw caps */
1736 g_signal_emit (G_OBJECT (dbin),
1737 gst_decode_bin_signals[SIGNAL_AUTOPLUG_CONTINUE], 0, dpad, tcaps,
1740 /* If autoplug-continue returns TRUE and the caps are not final, don't use them */
1741 if (apcontinue && !are_final_caps (dbin, tcaps))
1743 gst_caps_unref (tcaps);
1749 gst_object_unref (dpad);
1750 g_value_array_free (factories);
1751 goto discarded_type;
1755 /* 1.g now get the factory template caps and insert the capsfilter if this
1756 * is a parser/converter
1758 if (is_parser_converter) {
1759 GstCaps *filter_caps;
1761 GstElement *capsfilter;
1763 GstDecodeElement *delem;
1765 filter_caps = gst_caps_new_empty ();
1766 for (i = 0; i < factories->n_values; i++) {
1767 GstElementFactory *factory =
1768 g_value_get_object (g_value_array_get_nth (factories, i));
1769 GstCaps *tcaps, *intersection;
1772 GST_DEBUG ("Trying factory %s",
1773 gst_plugin_feature_get_name (GST_PLUGIN_FEATURE (factory)));
1775 if (gst_element_get_factory (src) == factory ||
1776 gst_element_factory_list_is_type (factory,
1777 GST_ELEMENT_FACTORY_TYPE_PARSER)) {
1778 GST_DEBUG ("Skipping factory");
1782 for (tmps = gst_element_factory_get_static_pad_templates (factory); tmps;
1783 tmps = tmps->next) {
1784 GstStaticPadTemplate *st = (GstStaticPadTemplate *) tmps->data;
1785 if (st->direction != GST_PAD_SINK || st->presence != GST_PAD_ALWAYS)
1787 tcaps = gst_static_pad_template_get_caps (st);
1789 gst_caps_intersect_full (tcaps, caps, GST_CAPS_INTERSECT_FIRST);
1790 filter_caps = gst_caps_merge (filter_caps, intersection);
1791 gst_caps_unref (tcaps);
1795 /* Append the parser caps to prevent any not-negotiated errors */
1796 filter_caps = gst_caps_merge (filter_caps, gst_caps_ref (caps));
1798 if (chain->elements) {
1799 delem = (GstDecodeElement *) chain->elements->data;
1800 capsfilter = delem->capsfilter =
1801 gst_element_factory_make ("capsfilter", NULL);
1803 delem = g_slice_new0 (GstDecodeElement);
1804 capsfilter = delem->element =
1805 gst_element_factory_make ("capsfilter", NULL);
1806 delem->capsfilter = NULL;
1807 chain->elements = g_list_prepend (chain->elements, delem);
1810 g_object_set (G_OBJECT (capsfilter), "caps", filter_caps, NULL);
1811 gst_caps_unref (filter_caps);
1812 gst_element_set_state (capsfilter, GST_STATE_PAUSED);
1813 gst_bin_add (GST_BIN_CAST (dbin), gst_object_ref (capsfilter));
1815 decode_pad_set_target (dpad, NULL);
1816 p = gst_element_get_static_pad (capsfilter, "sink");
1817 gst_pad_link_full (pad, p, GST_PAD_LINK_CHECK_NOTHING);
1818 gst_object_unref (p);
1819 p = gst_element_get_static_pad (capsfilter, "src");
1820 decode_pad_set_target (dpad, p);
1823 gst_caps_unref (caps);
1825 caps = gst_pad_get_current_caps (pad);
1827 GST_DEBUG_OBJECT (dbin, "No final caps set yet, delaying autoplugging");
1828 gst_object_unref (dpad);
1829 g_value_array_free (factories);
1830 goto setup_caps_delay;
1834 /* 1.h else continue autoplugging something from the list. */
1835 GST_LOG_OBJECT (pad, "Let's continue discovery on this pad");
1837 connect_pad (dbin, src, dpad, pad, caps, factories, chain,
1840 /* Need to unref the capsfilter srcpad here if
1841 * we inserted a capsfilter */
1842 if (is_parser_converter)
1843 gst_object_unref (pad);
1845 gst_object_unref (dpad);
1846 g_value_array_free (factories);
1851 gst_caps_unref (caps);
1857 GST_LOG_OBJECT (dbin, "Pad is final and should expose the pad. "
1858 "autoplug-continue:%d", apcontinue);
1859 gst_object_unref (dpad);
1860 gst_caps_unref (caps);
1866 GST_LOG_OBJECT (pad, "Known type, but discarded because not final caps");
1867 chain->deadend = TRUE;
1868 chain->endcaps = caps;
1869 gst_object_replace ((GstObject **) & chain->current_pad, NULL);
1871 /* Try to expose anything */
1873 if (dbin->decode_chain) {
1874 if (gst_decode_chain_is_complete (dbin->decode_chain)) {
1875 gst_decode_bin_expose (dbin);
1878 EXPOSE_UNLOCK (dbin);
1879 do_async_done (dbin);
1886 GST_LOG_OBJECT (pad, "Unknown type, posting message and firing signal");
1888 chain->deadend_details = deadend_details;
1889 chain->deadend = TRUE;
1890 chain->endcaps = caps;
1891 gst_object_replace ((GstObject **) & chain->current_pad, NULL);
1893 gst_element_post_message (GST_ELEMENT_CAST (dbin),
1894 gst_missing_decoder_message_new (GST_ELEMENT_CAST (dbin), caps));
1896 g_signal_emit (G_OBJECT (dbin),
1897 gst_decode_bin_signals[SIGNAL_UNKNOWN_TYPE], 0, pad, caps);
1899 /* Try to expose anything */
1901 if (dbin->decode_chain) {
1902 if (gst_decode_chain_is_complete (dbin->decode_chain)) {
1903 gst_decode_bin_expose (dbin);
1906 EXPOSE_UNLOCK (dbin);
1908 if (src == dbin->typefind) {
1909 if (!caps || gst_caps_is_empty (caps)) {
1910 GST_ELEMENT_ERROR (dbin, STREAM, TYPE_NOT_FOUND,
1911 (_("Could not determine type of stream")), (NULL));
1913 do_async_done (dbin);
1919 GST_DEBUG_OBJECT (pad, "pad has non-fixed caps delay autoplugging");
1920 gst_object_unref (dpad);
1921 goto setup_caps_delay;
1925 GST_DEBUG_OBJECT (pad, "pad has ANY caps, delaying auto-pluggin");
1926 goto setup_caps_delay;
1930 GstPendingPad *ppad;
1932 /* connect to caps notification */
1933 CHAIN_MUTEX_LOCK (chain);
1934 GST_LOG_OBJECT (dbin, "Chain %p has now %d dynamic pads", chain,
1935 g_list_length (chain->pending_pads));
1936 ppad = g_slice_new0 (GstPendingPad);
1937 ppad->pad = gst_object_ref (pad);
1938 ppad->chain = chain;
1939 ppad->event_probe_id =
1940 gst_pad_add_probe (pad, GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM,
1941 pad_event_cb, ppad, NULL);
1942 chain->pending_pads = g_list_prepend (chain->pending_pads, ppad);
1943 ppad->notify_caps_id = g_signal_connect (pad, "notify::caps",
1944 G_CALLBACK (caps_notify_cb), chain);
1945 CHAIN_MUTEX_UNLOCK (chain);
1947 /* If we're here because we have a Parser/Converter
1948 * we have to unref the pad */
1949 if (is_parser_converter)
1950 gst_object_unref (pad);
1952 gst_caps_unref (caps);
1959 add_error_filter (GstDecodeBin * dbin, GstElement * element)
1961 GST_OBJECT_LOCK (dbin);
1962 dbin->filtered = g_list_prepend (dbin->filtered, element);
1963 GST_OBJECT_UNLOCK (dbin);
1967 remove_error_filter (GstDecodeBin * dbin, GstElement * element,
1968 GstMessage ** error)
1972 GST_OBJECT_LOCK (dbin);
1973 dbin->filtered = g_list_remove (dbin->filtered, element);
1978 l = dbin->filtered_errors;
1980 GstMessage *msg = l->data;
1982 if (GST_MESSAGE_SRC (msg) == GST_OBJECT_CAST (element)) {
1983 /* Get the last error of this element, i.e. the earliest */
1985 gst_message_replace (error, msg);
1986 gst_message_unref (msg);
1987 l = dbin->filtered_errors = g_list_delete_link (dbin->filtered_errors, l);
1992 GST_OBJECT_UNLOCK (dbin);
1999 } SendStickyEventsData;
2002 send_sticky_event (GstPad * pad, GstEvent ** event, gpointer user_data)
2004 SendStickyEventsData *data = user_data;
2007 ret = gst_pad_send_event (data->peer, gst_event_ref (*event));
2015 send_sticky_events (GstDecodeBin * dbin, GstPad * pad)
2017 SendStickyEventsData data;
2020 data.peer = gst_pad_get_peer (pad);
2022 gst_pad_sticky_events_foreach (pad, send_sticky_event, &data);
2024 gst_object_unref (data.peer);
2030 error_message_to_string (GstMessage * msg)
2033 gchar *debug, *message, *full_message;
2035 gst_message_parse_error (msg, &err, &debug);
2037 message = gst_error_get_message (err->domain, err->code);
2040 full_message = g_strdup_printf ("%s\n%s\n%s", message, err->message, debug);
2042 full_message = g_strdup_printf ("%s\n%s", message, err->message);
2046 g_clear_error (&err);
2048 return full_message;
2051 static GstPadProbeReturn
2052 demuxer_source_pad_probe (GstPad * pad, GstPadProbeInfo * info,
2055 GstEvent *event = GST_PAD_PROBE_INFO_EVENT (info);
2056 GstDecodeGroup *group = (GstDecodeGroup *) user_data;
2057 GstDecodeChain *parent_chain = group->parent;
2059 GST_DEBUG_OBJECT (pad, "Saw event %s", GST_EVENT_TYPE_NAME (event));
2060 /* Check if we are the active group, if not we need to proxy the flush
2061 * events to the other groups (of which at least one is exposed, ensuring
2062 * flushing properly propagates downstream of decodebin */
2063 if (parent_chain->active_group == group)
2064 return GST_PAD_PROBE_OK;
2066 switch (GST_EVENT_TYPE (event)) {
2067 case GST_EVENT_FLUSH_START:
2068 case GST_EVENT_FLUSH_STOP:
2071 GST_DEBUG_OBJECT (pad, "Proxying flush events to inactive groups");
2072 /* Proxy to active group */
2073 for (tmp = parent_chain->active_group->reqpads; tmp; tmp = tmp->next) {
2074 GstPad *reqpad = (GstPad *) tmp->data;
2075 gst_pad_send_event (reqpad, gst_event_ref (event));
2077 /* Proxy to other non-active groups (except ourself) */
2078 for (tmp = parent_chain->next_groups; tmp; tmp = tmp->next) {
2080 GstDecodeGroup *tmpgroup = (GstDecodeGroup *) tmp->data;
2081 if (tmpgroup != group) {
2082 for (tmp2 = tmpgroup->reqpads; tmp2; tmp2 = tmp2->next) {
2083 GstPad *reqpad = (GstPad *) tmp2->data;
2084 gst_pad_send_event (reqpad, gst_event_ref (event));
2088 flush_chain (parent_chain,
2089 GST_EVENT_TYPE (event) == GST_EVENT_FLUSH_START);
2096 return GST_PAD_PROBE_OK;
2101 GstDecodeChain *chain;
2107 * Try to connect the given pad to an element created from one of the factories,
2110 * Note that dpad is ghosting pad, and so pad is linked; be sure to unset dpad's
2111 * target before trying to link pad.
2113 * Returns TRUE if an element was properly created and linked
2116 connect_pad (GstDecodeBin * dbin, GstElement * src, GstDecodePad * dpad,
2117 GstPad * pad, GstCaps * caps, GValueArray * factories,
2118 GstDecodeChain * chain, gchar ** deadend_details)
2120 gboolean res = FALSE;
2121 GstPad *mqpad = NULL;
2122 gboolean is_demuxer = chain->parent && !chain->elements; /* First pad after the demuxer */
2123 GString *error_details = NULL;
2125 g_return_val_if_fail (factories != NULL, FALSE);
2126 g_return_val_if_fail (factories->n_values > 0, FALSE);
2128 GST_DEBUG_OBJECT (dbin,
2129 "pad %s:%s , chain:%p, %d factories, caps %" GST_PTR_FORMAT,
2130 GST_DEBUG_PAD_NAME (pad), chain, factories->n_values, caps);
2132 /* 1. is element demuxer or parser */
2134 GST_LOG_OBJECT (src,
2135 "is a demuxer, connecting the pad through multiqueue '%s'",
2136 GST_OBJECT_NAME (chain->parent->multiqueue));
2138 /* Set a flush-start/-stop probe on the downstream events */
2139 chain->pad_probe_id =
2140 gst_pad_add_probe (pad, GST_PAD_PROBE_TYPE_EVENT_FLUSH,
2141 demuxer_source_pad_probe, chain->parent, NULL);
2143 decode_pad_set_target (dpad, NULL);
2144 if (!(mqpad = gst_decode_group_control_demuxer_pad (chain->parent, pad)))
2146 src = chain->parent->multiqueue;
2147 /* Forward sticky events to mq src pad to allow factory initialization */
2148 gst_pad_sticky_events_foreach (pad, copy_sticky_events, mqpad);
2150 decode_pad_set_target (dpad, pad);
2153 error_details = g_string_new ("");
2155 /* 2. Try to create an element and link to it */
2156 while (factories->n_values > 0) {
2157 GstAutoplugSelectResult ret;
2158 GstElementFactory *factory;
2159 GstDecodeElement *delem;
2160 GstElement *element;
2164 GList *to_connect = NULL;
2165 GList *to_expose = NULL;
2166 gboolean is_parser = FALSE;
2167 gboolean is_decoder = FALSE;
2169 /* Set dpad target to pad again, it might've been unset
2170 * below but we came back here because something failed
2172 decode_pad_set_target (dpad, pad);
2174 /* take first factory */
2175 factory = g_value_get_object (g_value_array_get_nth (factories, 0));
2176 /* Remove selected factory from the list. */
2177 g_value_array_remove (factories, 0);
2179 GST_LOG_OBJECT (src, "trying factory %" GST_PTR_FORMAT, factory);
2181 /* Check if the caps are really supported by the factory. The
2182 * factory list is non-empty-subset filtered while caps
2183 * are only accepted by a pad if they are a subset of the
2186 * FIXME: Only do this for fixed caps here. Non-fixed caps
2187 * can happen if a Parser/Converter was autoplugged before
2188 * this. We then assume that it will be able to convert to
2189 * everything that the decoder would want.
2191 * A subset check will fail here because the parser caps
2192 * will be generic and while the decoder will only
2193 * support a subset of the parser caps.
2195 if (gst_caps_is_fixed (caps)) {
2196 const GList *templs;
2197 gboolean skip = FALSE;
2199 templs = gst_element_factory_get_static_pad_templates (factory);
2202 GstStaticPadTemplate *templ = (GstStaticPadTemplate *) templs->data;
2204 if (templ->direction == GST_PAD_SINK) {
2205 GstCaps *templcaps = gst_static_caps_get (&templ->static_caps);
2207 if (!gst_caps_is_subset (caps, templcaps)) {
2208 GST_DEBUG_OBJECT (src,
2209 "caps %" GST_PTR_FORMAT " not subset of %" GST_PTR_FORMAT, caps,
2211 gst_caps_unref (templcaps);
2216 gst_caps_unref (templcaps);
2218 templs = g_list_next (templs);
2224 /* If the factory is for a parser we first check if the factory
2225 * was already used for the current chain. If it was used already
2226 * we would otherwise create an infinite loop here because the
2227 * parser apparently accepts its own output as input.
2228 * This is only done for parsers because it's perfectly valid
2229 * to have other element classes after each other because a
2230 * parser is the only one that does not change the data. A
2231 * valid example for this would be multiple id3demux in a row.
2233 is_parser = strstr (gst_element_factory_get_metadata (factory,
2234 GST_ELEMENT_METADATA_KLASS), "Parser") != NULL;
2237 gboolean skip = FALSE;
2240 CHAIN_MUTEX_LOCK (chain);
2241 for (l = chain->elements; l; l = l->next) {
2242 GstDecodeElement *delem = (GstDecodeElement *) l->data;
2243 GstElement *otherelement = delem->element;
2245 if (gst_element_get_factory (otherelement) == factory) {
2251 if (!skip && chain->parent && chain->parent->parent) {
2252 GstDecodeChain *parent_chain = chain->parent->parent;
2253 GstDecodeElement *pelem =
2254 parent_chain->elements ? parent_chain->elements->data : NULL;
2256 if (pelem && gst_element_get_factory (pelem->element) == factory)
2259 CHAIN_MUTEX_UNLOCK (chain);
2261 GST_DEBUG_OBJECT (dbin,
2262 "Skipping factory '%s' because it was already used in this chain",
2263 gst_plugin_feature_get_name (GST_PLUGIN_FEATURE_CAST (factory)));
2269 /* emit autoplug-select to see what we should do with it. */
2270 g_signal_emit (G_OBJECT (dbin),
2271 gst_decode_bin_signals[SIGNAL_AUTOPLUG_SELECT],
2272 0, dpad, caps, factory, &ret);
2275 case GST_AUTOPLUG_SELECT_TRY:
2276 GST_DEBUG_OBJECT (dbin, "autoplug select requested try");
2278 case GST_AUTOPLUG_SELECT_EXPOSE:
2279 GST_DEBUG_OBJECT (dbin, "autoplug select requested expose");
2280 /* expose the pad, we don't have the source element */
2281 expose_pad (dbin, src, dpad, pad, caps, chain);
2284 case GST_AUTOPLUG_SELECT_SKIP:
2285 GST_DEBUG_OBJECT (dbin, "autoplug select requested skip");
2288 GST_WARNING_OBJECT (dbin, "autoplug select returned unhandled %d", ret);
2292 /* 2.0. Unlink pad */
2293 decode_pad_set_target (dpad, NULL);
2295 /* 2.1. Try to create an element */
2296 if ((element = gst_element_factory_create (factory, NULL)) == NULL) {
2297 GST_WARNING_OBJECT (dbin, "Could not create an element from %s",
2298 gst_plugin_feature_get_name (GST_PLUGIN_FEATURE (factory)));
2299 g_string_append_printf (error_details,
2300 "Could not create an element from %s\n",
2301 gst_plugin_feature_get_name (GST_PLUGIN_FEATURE (factory)));
2305 /* Filter errors, this will prevent the element from causing the pipeline
2306 * to error while we test it using READY state. */
2307 add_error_filter (dbin, element);
2309 /* We don't yet want the bin to control the element's state */
2310 gst_element_set_locked_state (element, TRUE);
2312 /* ... add it ... */
2313 if (!(gst_bin_add (GST_BIN_CAST (dbin), element))) {
2314 GST_WARNING_OBJECT (dbin, "Couldn't add %s to the bin",
2315 GST_ELEMENT_NAME (element));
2316 remove_error_filter (dbin, element, NULL);
2317 g_string_append_printf (error_details, "Couldn't add %s to the bin\n",
2318 GST_ELEMENT_NAME (element));
2319 gst_object_unref (element);
2323 /* Find its sink pad. */
2324 if (!(sinkpad = find_sink_pad (element))) {
2325 GST_WARNING_OBJECT (dbin, "Element %s doesn't have a sink pad",
2326 GST_ELEMENT_NAME (element));
2327 remove_error_filter (dbin, element, NULL);
2328 g_string_append_printf (error_details,
2329 "Element %s doesn't have a sink pad", GST_ELEMENT_NAME (element));
2330 gst_bin_remove (GST_BIN (dbin), element);
2334 /* ... and try to link */
2335 if ((gst_pad_link_full (pad, sinkpad,
2336 GST_PAD_LINK_CHECK_NOTHING)) != GST_PAD_LINK_OK) {
2337 GST_WARNING_OBJECT (dbin, "Link failed on pad %s:%s",
2338 GST_DEBUG_PAD_NAME (sinkpad));
2339 remove_error_filter (dbin, element, NULL);
2340 g_string_append_printf (error_details, "Link failed on pad %s:%s",
2341 GST_DEBUG_PAD_NAME (sinkpad));
2342 gst_object_unref (sinkpad);
2343 gst_bin_remove (GST_BIN (dbin), element);
2347 /* ... activate it ... */
2348 if ((gst_element_set_state (element,
2349 GST_STATE_READY)) == GST_STATE_CHANGE_FAILURE) {
2350 GstMessage *error_msg;
2352 GST_WARNING_OBJECT (dbin, "Couldn't set %s to READY",
2353 GST_ELEMENT_NAME (element));
2354 remove_error_filter (dbin, element, &error_msg);
2357 gchar *error_string = error_message_to_string (error_msg);
2358 g_string_append_printf (error_details, "Couldn't set %s to READY:\n%s",
2359 GST_ELEMENT_NAME (element), error_string);
2360 gst_message_unref (error_msg);
2361 g_free (error_string);
2363 g_string_append_printf (error_details, "Couldn't set %s to READY",
2364 GST_ELEMENT_NAME (element));
2366 gst_object_unref (sinkpad);
2367 gst_bin_remove (GST_BIN (dbin), element);
2371 /* check if we still accept the caps on the pad after setting
2372 * the element to READY */
2373 if (!gst_pad_query_accept_caps (sinkpad, caps)) {
2374 GstMessage *error_msg;
2376 GST_WARNING_OBJECT (dbin, "Element %s does not accept caps",
2377 GST_ELEMENT_NAME (element));
2379 remove_error_filter (dbin, element, &error_msg);
2382 gchar *error_string = error_message_to_string (error_msg);
2383 g_string_append_printf (error_details,
2384 "Element %s does not accept caps:\n%s", GST_ELEMENT_NAME (element),
2386 gst_message_unref (error_msg);
2387 g_free (error_string);
2389 g_string_append_printf (error_details,
2390 "Element %s does not accept caps", GST_ELEMENT_NAME (element));
2393 gst_element_set_state (element, GST_STATE_NULL);
2394 gst_object_unref (sinkpad);
2395 gst_bin_remove (GST_BIN (dbin), element);
2399 gst_object_unref (sinkpad);
2400 GST_LOG_OBJECT (dbin, "linked on pad %s:%s", GST_DEBUG_PAD_NAME (pad));
2402 CHAIN_MUTEX_LOCK (chain);
2403 delem = g_slice_new0 (GstDecodeElement);
2404 delem->element = gst_object_ref (element);
2405 delem->capsfilter = NULL;
2406 chain->elements = g_list_prepend (chain->elements, delem);
2407 chain->demuxer = is_demuxer_element (element);
2408 chain->adaptive_demuxer = is_adaptive_demuxer_element (element);
2410 is_decoder = strstr (gst_element_factory_get_metadata (factory,
2411 GST_ELEMENT_METADATA_KLASS), "Decoder") != NULL;
2413 /* For adaptive streaming demuxer we insert a multiqueue after
2415 * Now for the case where we have a container stream inside these
2416 * buffers, another demuxer will be plugged and after this second
2417 * demuxer there will be a second multiqueue. This second multiqueue
2418 * will get smaller buffers and will be the one emitting buffering
2420 * If we don't have a container stream inside the fragment buffers,
2421 * we'll insert a multiqueue below right after the next element after
2422 * the adaptive streaming demuxer. This is going to be a parser or
2423 * decoder, and will output smaller buffers.
2425 if (chain->parent && chain->parent->parent) {
2426 GstDecodeChain *parent_chain = chain->parent->parent;
2428 if (parent_chain->adaptive_demuxer && (is_parser || is_decoder))
2429 chain->demuxer = TRUE;
2432 /* If we are configured to use buffering and there is no demuxer in the
2433 * chain, we still want a multiqueue, otherwise we will ignore the
2434 * use-buffering property. In that case, we will insert a multiqueue after
2435 * the parser or decoder - not elsewhere, otherwise we won't have
2439 if (!chain->parent && (is_parser || is_decoder) && dbin->use_buffering) {
2440 chain->demuxer = TRUE;
2442 GST_WARNING_OBJECT (dbin,
2443 "Buffering messages used for decoded and non-parsed data");
2447 CHAIN_MUTEX_UNLOCK (chain);
2449 /* Set connection-speed property if needed */
2450 if (chain->demuxer) {
2453 if ((pspec = g_object_class_find_property (G_OBJECT_GET_CLASS (element),
2454 "connection-speed"))) {
2455 guint64 speed = dbin->connection_speed / 1000;
2456 gboolean wrong_type = FALSE;
2458 if (G_PARAM_SPEC_TYPE (pspec) == G_TYPE_PARAM_UINT) {
2459 GParamSpecUInt *pspecuint = G_PARAM_SPEC_UINT (pspec);
2461 speed = CLAMP (speed, pspecuint->minimum, pspecuint->maximum);
2462 } else if (G_PARAM_SPEC_TYPE (pspec) == G_TYPE_PARAM_INT) {
2463 GParamSpecInt *pspecint = G_PARAM_SPEC_INT (pspec);
2465 speed = CLAMP (speed, pspecint->minimum, pspecint->maximum);
2466 } else if (G_PARAM_SPEC_TYPE (pspec) == G_TYPE_PARAM_UINT64) {
2467 GParamSpecUInt64 *pspecuint = G_PARAM_SPEC_UINT64 (pspec);
2469 speed = CLAMP (speed, pspecuint->minimum, pspecuint->maximum);
2470 } else if (G_PARAM_SPEC_TYPE (pspec) == G_TYPE_PARAM_INT64) {
2471 GParamSpecInt64 *pspecint = G_PARAM_SPEC_INT64 (pspec);
2473 speed = CLAMP (speed, pspecint->minimum, pspecint->maximum);
2475 GST_WARNING_OBJECT (dbin,
2476 "The connection speed property %" G_GUINT64_FORMAT " of type %s"
2477 " is not useful not setting it", speed,
2478 g_type_name (G_PARAM_SPEC_TYPE (pspec)));
2483 GST_DEBUG_OBJECT (dbin, "setting connection-speed=%" G_GUINT64_FORMAT
2484 " to demuxer element", speed);
2486 g_object_set (element, "connection-speed", speed, NULL);
2491 /* try to configure the subtitle encoding property when we can */
2492 pspec = g_object_class_find_property (G_OBJECT_GET_CLASS (element),
2493 "subtitle-encoding");
2494 if (pspec && G_PARAM_SPEC_VALUE_TYPE (pspec) == G_TYPE_STRING) {
2495 SUBTITLE_LOCK (dbin);
2496 GST_DEBUG_OBJECT (dbin,
2497 "setting subtitle-encoding=%s to element", dbin->encoding);
2498 g_object_set (G_OBJECT (element), "subtitle-encoding", dbin->encoding,
2500 SUBTITLE_UNLOCK (dbin);
2506 /* link this element further */
2507 to_connect = connect_element (dbin, delem, chain);
2509 while (to_connect) {
2510 GstPad *opad = to_connect->data;
2511 gboolean expose_pad = FALSE;
2512 GstDecodeChain *new_chain;
2515 ocaps = get_pad_caps (opad);
2517 analyze_new_pad (dbin, delem->element, opad, ocaps, chain,
2521 gst_caps_unref (ocaps);
2524 PadExposeData *expose_data = g_new0 (PadExposeData, 1);
2525 expose_data->chain = new_chain;
2526 expose_data->pad = gst_object_ref (opad);
2527 to_expose = g_list_prepend (to_expose, expose_data);
2530 gst_object_unref (opad);
2531 to_connect = g_list_delete_link (to_connect, to_connect);
2533 /* any pads left in to_expose are to be exposed */
2535 /* Bring the element to the state of the parent */
2537 /* First lock element's sinkpad stream lock so no data reaches
2538 * the possible new element added when caps are sent by element
2539 * while we're still sending sticky events */
2540 GST_PAD_STREAM_LOCK (sinkpad);
2542 if ((gst_element_set_state (element,
2543 GST_STATE_PAUSED)) == GST_STATE_CHANGE_FAILURE ||
2544 !send_sticky_events (dbin, pad)) {
2545 GstDecodeElement *dtmp = NULL;
2546 GstElement *tmp = NULL;
2547 GstMessage *error_msg;
2549 GST_PAD_STREAM_UNLOCK (sinkpad);
2551 GST_WARNING_OBJECT (dbin, "Couldn't set %s to PAUSED",
2552 GST_ELEMENT_NAME (element));
2555 PadExposeData *expose_data = to_expose->data;
2556 gst_object_unref (expose_data->pad);
2557 g_free (expose_data);
2558 to_expose = g_list_delete_link (to_expose, to_expose);
2561 remove_error_filter (dbin, element, &error_msg);
2564 gchar *error_string = error_message_to_string (error_msg);
2565 g_string_append_printf (error_details, "Couldn't set %s to PAUSED:\n%s",
2566 GST_ELEMENT_NAME (element), error_string);
2567 gst_message_unref (error_msg);
2568 g_free (error_string);
2570 g_string_append_printf (error_details, "Couldn't set %s to PAUSED",
2571 GST_ELEMENT_NAME (element));
2574 /* Remove all elements in this chain that were just added. No
2575 * other thread could've added elements in the meantime */
2576 CHAIN_MUTEX_LOCK (chain);
2580 dtmp = chain->elements->data;
2581 tmp = dtmp->element;
2583 /* Disconnect any signal handlers that might be connected
2584 * in connect_element() or analyze_pad() */
2585 if (dtmp->pad_added_id)
2586 g_signal_handler_disconnect (tmp, dtmp->pad_added_id);
2587 if (dtmp->pad_removed_id)
2588 g_signal_handler_disconnect (tmp, dtmp->pad_removed_id);
2589 if (dtmp->no_more_pads_id)
2590 g_signal_handler_disconnect (tmp, dtmp->no_more_pads_id);
2592 for (l = chain->pending_pads; l;) {
2593 GstPendingPad *pp = l->data;
2596 if (GST_PAD_PARENT (pp->pad) != tmp) {
2601 gst_pending_pad_free (pp);
2603 /* Remove element from the list, update list head and go to the
2604 * next element in the list */
2606 chain->pending_pads = g_list_delete_link (chain->pending_pads, l);
2610 if (dtmp->capsfilter) {
2611 gst_bin_remove (GST_BIN (dbin), dtmp->capsfilter);
2612 gst_element_set_state (dtmp->capsfilter, GST_STATE_NULL);
2613 gst_object_unref (dtmp->capsfilter);
2616 gst_bin_remove (GST_BIN (dbin), tmp);
2617 gst_element_set_state (tmp, GST_STATE_NULL);
2619 gst_object_unref (tmp);
2620 g_slice_free (GstDecodeElement, dtmp);
2622 chain->elements = g_list_delete_link (chain->elements, chain->elements);
2623 } while (tmp != element);
2624 CHAIN_MUTEX_UNLOCK (chain);
2628 /* Everything went well, the spice must flow now */
2629 GST_PAD_STREAM_UNLOCK (sinkpad);
2632 /* Remove error filter now, from now on we can't gracefully
2633 * handle errors of the element anymore */
2634 remove_error_filter (dbin, element, NULL);
2636 /* Now let the bin handle the state */
2637 gst_element_set_locked_state (element, FALSE);
2640 SUBTITLE_LOCK (dbin);
2641 /* we added the element now, add it to the list of subtitle-encoding
2642 * elements when we can set the property */
2643 dbin->subtitles = g_list_prepend (dbin->subtitles, element);
2644 SUBTITLE_UNLOCK (dbin);
2648 PadExposeData *expose_data = to_expose->data;
2651 ocaps = get_pad_caps (expose_data->pad);
2652 expose_pad (dbin, delem->element, expose_data->chain->current_pad,
2653 expose_data->pad, ocaps, expose_data->chain);
2656 gst_caps_unref (ocaps);
2658 gst_object_unref (expose_data->pad);
2659 g_free (expose_data);
2660 to_expose = g_list_delete_link (to_expose, to_expose);
2669 gst_object_unref (mqpad);
2672 *deadend_details = g_string_free (error_details, (error_details->len == 0
2675 *deadend_details = NULL;
2681 get_pad_caps (GstPad * pad)
2685 /* first check the pad caps, if this is set, we are positively sure it is
2686 * fixed and exactly what the element will produce. */
2687 caps = gst_pad_get_current_caps (pad);
2689 /* then use the getcaps function if we don't have caps. These caps might not
2690 * be fixed in some cases, in which case analyze_new_pad will set up a
2691 * notify::caps signal to continue autoplugging. */
2693 caps = gst_pad_query_caps (pad, NULL);
2698 /* Returns a list of pads that can be connected to already and
2699 * connects to pad-added and related signals */
2701 connect_element (GstDecodeBin * dbin, GstDecodeElement * delem,
2702 GstDecodeChain * chain)
2704 GstElement *element = delem->element;
2706 gboolean dynamic = FALSE;
2707 GList *to_connect = NULL;
2709 GST_DEBUG_OBJECT (dbin, "Attempting to connect element %s [chain:%p] further",
2710 GST_ELEMENT_NAME (element), chain);
2712 /* 1. Loop over pad templates, grabbing existing pads along the way */
2713 for (pads = GST_ELEMENT_GET_CLASS (element)->padtemplates; pads;
2714 pads = g_list_next (pads)) {
2715 GstPadTemplate *templ = GST_PAD_TEMPLATE (pads->data);
2716 const gchar *templ_name;
2718 /* we are only interested in source pads */
2719 if (GST_PAD_TEMPLATE_DIRECTION (templ) != GST_PAD_SRC)
2722 templ_name = GST_PAD_TEMPLATE_NAME_TEMPLATE (templ);
2723 GST_DEBUG_OBJECT (dbin, "got a source pad template %s", templ_name);
2725 /* figure out what kind of pad this is */
2726 switch (GST_PAD_TEMPLATE_PRESENCE (templ)) {
2727 case GST_PAD_ALWAYS:
2729 /* get the pad that we need to autoplug */
2730 GstPad *pad = gst_element_get_static_pad (element, templ_name);
2733 GST_DEBUG_OBJECT (dbin, "got the pad for always template %s",
2735 /* here is the pad, we need to autoplug it */
2736 to_connect = g_list_prepend (to_connect, pad);
2738 /* strange, pad is marked as always but it's not
2739 * there. Fix the element */
2740 GST_WARNING_OBJECT (dbin,
2741 "could not get the pad for always template %s", templ_name);
2745 case GST_PAD_SOMETIMES:
2747 /* try to get the pad to see if it is already created or
2749 GstPad *pad = gst_element_get_static_pad (element, templ_name);
2752 GST_DEBUG_OBJECT (dbin, "got the pad for sometimes template %s",
2754 /* the pad is created, we need to autoplug it */
2755 to_connect = g_list_prepend (to_connect, pad);
2757 GST_DEBUG_OBJECT (dbin,
2758 "did not get the sometimes pad of template %s", templ_name);
2759 /* we have an element that will create dynamic pads */
2764 case GST_PAD_REQUEST:
2765 /* ignore request pads */
2766 GST_DEBUG_OBJECT (dbin, "ignoring request padtemplate %s", templ_name);
2771 /* 2. if there are more potential pads, connect to relevant signals */
2773 GST_LOG_OBJECT (dbin, "Adding signals to element %s in chain %p",
2774 GST_ELEMENT_NAME (element), chain);
2775 delem->pad_added_id = g_signal_connect (element, "pad-added",
2776 G_CALLBACK (pad_added_cb), chain);
2777 delem->pad_removed_id = g_signal_connect (element, "pad-removed",
2778 G_CALLBACK (pad_removed_cb), chain);
2779 delem->no_more_pads_id = g_signal_connect (element, "no-more-pads",
2780 G_CALLBACK (no_more_pads_cb), chain);
2783 /* 3. return all pads that can be connected to already */
2790 * Expose the given pad on the chain as a decoded pad.
2793 expose_pad (GstDecodeBin * dbin, GstElement * src, GstDecodePad * dpad,
2794 GstPad * pad, GstCaps * caps, GstDecodeChain * chain)
2796 GstPad *mqpad = NULL;
2798 GST_DEBUG_OBJECT (dbin, "pad %s:%s, chain:%p",
2799 GST_DEBUG_PAD_NAME (pad), chain);
2801 /* If this is the first pad for this chain, there are no other elements
2802 * and the source element is not the multiqueue we must link through the
2805 * This is the case if a demuxer directly exposed a raw pad.
2807 if (chain->parent && !chain->elements && src != chain->parent->multiqueue) {
2808 GST_LOG_OBJECT (src, "connecting the pad through multiqueue");
2810 decode_pad_set_target (dpad, NULL);
2811 if (!(mqpad = gst_decode_group_control_demuxer_pad (chain->parent, pad)))
2814 decode_pad_set_target (dpad, pad);
2817 gst_decode_pad_activate (dpad, chain);
2818 chain->endpad = gst_object_ref (dpad);
2819 chain->endcaps = gst_caps_ref (caps);
2822 if (dbin->decode_chain) {
2823 if (gst_decode_chain_is_complete (dbin->decode_chain)) {
2824 gst_decode_bin_expose (dbin);
2827 EXPOSE_UNLOCK (dbin);
2830 gst_object_unref (mqpad);
2836 /* check_upstream_seekable:
2838 * Check if upstream is seekable.
2841 check_upstream_seekable (GstDecodeBin * dbin, GstPad * pad)
2844 gint64 start = -1, stop = -1;
2845 gboolean seekable = FALSE;
2847 query = gst_query_new_seeking (GST_FORMAT_BYTES);
2848 if (!gst_pad_peer_query (pad, query)) {
2849 GST_DEBUG_OBJECT (dbin, "seeking query failed");
2853 gst_query_parse_seeking (query, NULL, &seekable, &start, &stop);
2855 /* try harder to query upstream size if we didn't get it the first time */
2856 if (seekable && stop == -1) {
2857 GST_DEBUG_OBJECT (dbin, "doing duration query to fix up unset stop");
2858 gst_pad_peer_query_duration (pad, GST_FORMAT_BYTES, &stop);
2861 /* if upstream doesn't know the size, it's likely that it's not seekable in
2862 * practice even if it technically may be seekable */
2863 if (seekable && (start != 0 || stop <= start)) {
2864 GST_DEBUG_OBJECT (dbin, "seekable but unknown start/stop -> disable");
2867 GST_DEBUG_OBJECT (dbin, "upstream seekable: %d", seekable);
2871 gst_query_unref (query);
2876 type_found (GstElement * typefind, guint probability,
2877 GstCaps * caps, GstDecodeBin * decode_bin)
2879 GstPad *pad, *sink_pad;
2880 GstDecodeChain *chain;
2882 GST_DEBUG_OBJECT (decode_bin, "typefind found caps %" GST_PTR_FORMAT, caps);
2884 /* If the typefinder (but not something else) finds text/plain - i.e. that's
2885 * the top-level type of the file - then error out.
2887 if (gst_structure_has_name (gst_caps_get_structure (caps, 0), "text/plain")) {
2888 GST_ELEMENT_ERROR (decode_bin, STREAM, WRONG_TYPE,
2889 (_("This appears to be a text file")),
2890 ("decodebin cannot decode plain text files"));
2894 pad = gst_element_get_static_pad (typefind, "src");
2895 sink_pad = gst_element_get_static_pad (typefind, "sink");
2897 /* need some lock here to prevent race with shutdown state change
2898 * which might yank away e.g. decode_chain while building stuff here.
2899 * In typical cases, STREAM_LOCK is held and handles that, it need not
2900 * be held (if called from a proxied setcaps), so grab it anyway */
2901 GST_PAD_STREAM_LOCK (sink_pad);
2902 /* FIXME: we can only deal with one type, we don't yet support dynamically changing
2903 * caps from the typefind element */
2904 if (decode_bin->have_type || decode_bin->decode_chain) {
2906 decode_bin->have_type = TRUE;
2908 decode_bin->decode_chain = gst_decode_chain_new (decode_bin, NULL, pad);
2909 chain = gst_decode_chain_ref (decode_bin->decode_chain);
2911 if (analyze_new_pad (decode_bin, typefind, pad, caps,
2912 decode_bin->decode_chain, NULL))
2913 expose_pad (decode_bin, typefind, decode_bin->decode_chain->current_pad,
2914 pad, caps, decode_bin->decode_chain);
2916 gst_decode_chain_unref (chain);
2919 GST_PAD_STREAM_UNLOCK (sink_pad);
2920 gst_object_unref (sink_pad);
2921 gst_object_unref (pad);
2927 static GstPadProbeReturn
2928 pad_event_cb (GstPad * pad, GstPadProbeInfo * info, gpointer data)
2930 GstEvent *event = GST_PAD_PROBE_INFO_EVENT (info);
2931 GstPendingPad *ppad = (GstPendingPad *) data;
2932 GstDecodeChain *chain = ppad->chain;
2933 GstDecodeBin *dbin = chain->dbin;
2938 switch (GST_EVENT_TYPE (event)) {
2940 GST_DEBUG_OBJECT (pad, "Received EOS on a non final pad, this stream "
2942 chain->deadend = TRUE;
2943 chain->drained = TRUE;
2944 gst_object_replace ((GstObject **) & chain->current_pad, NULL);
2945 /* we don't set the endcaps because NULL endcaps means early EOS */
2948 if (dbin->decode_chain)
2949 if (gst_decode_chain_is_complete (dbin->decode_chain))
2950 gst_decode_bin_expose (dbin);
2951 EXPOSE_UNLOCK (dbin);
2956 return GST_PAD_PROBE_OK;
2960 pad_added_cb (GstElement * element, GstPad * pad, GstDecodeChain * chain)
2964 GstDecodeChain *new_chain;
2968 GST_DEBUG_OBJECT (pad, "pad added, chain:%p", chain);
2969 GST_PAD_STREAM_LOCK (pad);
2970 if (!gst_pad_is_active (pad)) {
2971 GST_PAD_STREAM_UNLOCK (pad);
2972 GST_DEBUG_OBJECT (pad, "Ignoring pad-added from a deactivated pad");
2976 caps = get_pad_caps (pad);
2977 if (analyze_new_pad (dbin, element, pad, caps, chain, &new_chain))
2978 expose_pad (dbin, element, new_chain->current_pad, pad, caps, new_chain);
2980 gst_caps_unref (caps);
2982 GST_PAD_STREAM_UNLOCK (pad);
2985 static GstPadProbeReturn
2986 sink_pad_event_probe (GstPad * pad, GstPadProbeInfo * info, gpointer user_data)
2988 GstDecodeGroup *group = (GstDecodeGroup *) user_data;
2989 GstEvent *event = GST_PAD_PROBE_INFO_EVENT (info);
2990 GstPad *peer = gst_pad_get_peer (pad);
2991 GstPadProbeReturn proberet = GST_PAD_PROBE_OK;
2993 GST_DEBUG_OBJECT (pad, "Got upstream event %s", GST_EVENT_TYPE_NAME (event));
2996 GST_DEBUG_OBJECT (pad, "We are unlinked !");
2997 if (group->parent && group->parent->next_groups) {
2998 GstDecodeGroup *last_group =
2999 g_list_last (group->parent->next_groups)->data;
3000 GST_DEBUG_OBJECT (pad, "We could send the event to another group (%p)",
3002 /* Grab another sinkpad for that last group through which we will forward this event */
3003 if (last_group->reqpads) {
3004 GstPad *sinkpad = (GstPad *) last_group->reqpads->data;
3005 GstPad *otherpeer = gst_pad_get_peer (sinkpad);
3007 GST_DEBUG_OBJECT (otherpeer, "Attempting to forward event");
3008 if (gst_pad_send_event (otherpeer, gst_event_ref (event))) {
3009 gst_event_unref (event);
3010 proberet = GST_PAD_PROBE_HANDLED;
3012 gst_object_unref (otherpeer);
3015 GST_DEBUG_OBJECT (pad, "No request pads, can't forward event");
3019 gst_object_unref (peer);
3025 static GstPadProbeReturn
3026 sink_pad_query_probe (GstPad * pad, GstPadProbeInfo * info, gpointer user_data)
3028 GstDecodeGroup *group = (GstDecodeGroup *) user_data;
3029 GstPad *peer = gst_pad_get_peer (pad);
3030 GstQuery *query = GST_PAD_PROBE_INFO_QUERY (info);
3031 GstPadProbeReturn proberet = GST_PAD_PROBE_OK;
3033 GST_DEBUG_OBJECT (pad, "Got upstream query %s", GST_QUERY_TYPE_NAME (query));
3036 GST_DEBUG_OBJECT (pad, "We are unlinked !");
3037 if (group->parent && group->parent->next_groups) {
3038 GstDecodeGroup *last_group =
3039 g_list_last (group->parent->next_groups)->data;
3040 GST_DEBUG_OBJECT (pad, "We could send the query to another group");
3041 /* Grab another sinkpad for that last group through which we will forward this event */
3042 if (last_group->reqpads) {
3043 GstPad *sinkpad = (GstPad *) last_group->reqpads->data;
3044 GstPad *otherpeer = gst_pad_get_peer (sinkpad);
3046 GST_DEBUG_OBJECT (otherpeer, "Attempting to forward query");
3047 if (gst_pad_query (otherpeer, query)) {
3048 proberet = GST_PAD_PROBE_HANDLED;
3050 GST_DEBUG ("FAILURE");
3051 gst_object_unref (otherpeer);
3053 GST_DEBUG_OBJECT (sinkpad, "request pad not connected ??");
3055 GST_DEBUG_OBJECT (pad, "No request pads ???");
3058 gst_object_unref (peer);
3064 pad_removed_cb (GstElement * element, GstPad * pad, GstDecodeChain * chain)
3068 GST_LOG_OBJECT (pad, "pad removed, chain:%p", chain);
3070 /* In fact, we don't have to do anything here, the active group will be
3071 * removed when the group's multiqueue is drained */
3072 CHAIN_MUTEX_LOCK (chain);
3073 for (l = chain->pending_pads; l; l = l->next) {
3074 GstPendingPad *ppad = l->data;
3075 GstPad *opad = ppad->pad;
3078 gst_pending_pad_free (ppad);
3079 chain->pending_pads = g_list_delete_link (chain->pending_pads, l);
3083 CHAIN_MUTEX_UNLOCK (chain);
3087 no_more_pads_cb (GstElement * element, GstDecodeChain * chain)
3089 GstDecodeGroup *group = NULL;
3091 GST_LOG_OBJECT (element, "got no more pads");
3093 CHAIN_MUTEX_LOCK (chain);
3094 if (!chain->elements
3095 || ((GstDecodeElement *) chain->elements->data)->element != element) {
3096 GST_LOG_OBJECT (chain->dbin, "no-more-pads from old chain element '%s'",
3097 GST_OBJECT_NAME (element));
3098 CHAIN_MUTEX_UNLOCK (chain);
3100 } else if (!chain->demuxer) {
3101 GST_LOG_OBJECT (chain->dbin, "no-more-pads from a non-demuxer element '%s'",
3102 GST_OBJECT_NAME (element));
3103 CHAIN_MUTEX_UNLOCK (chain);
3107 /* when we received no_more_pads, we can complete the pads of the chain */
3108 if (!chain->next_groups && chain->active_group) {
3109 group = chain->active_group;
3110 } else if (chain->next_groups) {
3112 for (iter = chain->next_groups; iter; iter = g_list_next (iter)) {
3114 if (!group->no_more_pads)
3119 GST_ERROR_OBJECT (chain->dbin, "can't find group for element");
3120 CHAIN_MUTEX_UNLOCK (chain);
3124 GST_DEBUG_OBJECT (element, "Setting group %p to complete", group);
3126 group->no_more_pads = TRUE;
3127 /* this group has prerolled enough to not need more pads,
3128 * we can probably set its buffering state to playing now */
3129 GST_DEBUG_OBJECT (group->dbin, "Setting group %p multiqueue to "
3130 "'playing' buffering mode", group);
3131 decodebin_set_queue_size (group->dbin, group->multiqueue, FALSE,
3132 (group->parent ? group->parent->seekable : TRUE));
3133 CHAIN_MUTEX_UNLOCK (chain);
3135 EXPOSE_LOCK (chain->dbin);
3136 if (chain->dbin->decode_chain) {
3137 if (gst_decode_chain_is_complete (chain->dbin->decode_chain)) {
3138 gst_decode_bin_expose (chain->dbin);
3141 EXPOSE_UNLOCK (chain->dbin);
3145 caps_notify_cb (GstPad * pad, GParamSpec * unused, GstDecodeChain * chain)
3147 GstElement *element;
3150 GST_LOG_OBJECT (pad, "Notified caps for pad %s:%s", GST_DEBUG_PAD_NAME (pad));
3152 /* Disconnect this; if we still need it, we'll reconnect to this in
3153 * analyze_new_pad */
3154 element = GST_ELEMENT_CAST (gst_pad_get_parent (pad));
3156 CHAIN_MUTEX_LOCK (chain);
3157 for (l = chain->pending_pads; l; l = l->next) {
3158 GstPendingPad *ppad = l->data;
3159 if (ppad->pad == pad) {
3160 gst_pending_pad_free (ppad);
3161 chain->pending_pads = g_list_delete_link (chain->pending_pads, l);
3165 CHAIN_MUTEX_UNLOCK (chain);
3167 pad_added_cb (element, pad, chain);
3169 gst_object_unref (element);
3172 /* Decide whether an element is a demuxer based on the
3173 * klass and number/type of src pad templates it has */
3175 is_demuxer_element (GstElement * srcelement)
3177 GstElementFactory *srcfactory;
3178 GstElementClass *elemclass;
3181 gint potential_src_pads = 0;
3183 srcfactory = gst_element_get_factory (srcelement);
3185 gst_element_factory_get_metadata (srcfactory, GST_ELEMENT_METADATA_KLASS);
3187 /* Can't be a demuxer unless it has Demux in the klass name */
3188 if (!strstr (klass, "Demux"))
3191 /* Walk the src pad templates and count how many the element
3193 elemclass = GST_ELEMENT_GET_CLASS (srcelement);
3195 walk = gst_element_class_get_pad_template_list (elemclass);
3196 while (walk != NULL) {
3197 GstPadTemplate *templ;
3199 templ = (GstPadTemplate *) walk->data;
3200 if (GST_PAD_TEMPLATE_DIRECTION (templ) == GST_PAD_SRC) {
3201 switch (GST_PAD_TEMPLATE_PRESENCE (templ)) {
3202 case GST_PAD_ALWAYS:
3203 case GST_PAD_SOMETIMES:
3204 if (strstr (GST_PAD_TEMPLATE_NAME_TEMPLATE (templ), "%"))
3205 potential_src_pads += 2; /* Might make multiple pads */
3207 potential_src_pads += 1;
3209 case GST_PAD_REQUEST:
3210 potential_src_pads += 2;
3214 walk = g_list_next (walk);
3217 if (potential_src_pads < 2)
3224 is_adaptive_demuxer_element (GstElement * srcelement)
3226 GstElementFactory *srcfactory;
3229 srcfactory = gst_element_get_factory (srcelement);
3231 gst_element_factory_get_metadata (srcfactory, GST_ELEMENT_METADATA_KLASS);
3233 /* Can't be a demuxer unless it has Demux in the klass name */
3234 if (!strstr (klass, "Demux") || !strstr (klass, "Adaptive"))
3240 /* Returns TRUE if the caps are compatible with the caps specified in the 'caps'
3241 * property (which by default are the raw caps)
3243 * The decodebin_lock should be taken !
3246 are_final_caps (GstDecodeBin * dbin, GstCaps * caps)
3250 GST_LOG_OBJECT (dbin, "Checking with caps %" GST_PTR_FORMAT, caps);
3252 /* lock for getting the caps */
3253 GST_OBJECT_LOCK (dbin);
3254 res = gst_caps_is_subset (caps, dbin->caps);
3255 GST_OBJECT_UNLOCK (dbin);
3257 GST_LOG_OBJECT (dbin, "Caps are %sfinal caps", res ? "" : "not ");
3262 /* gst_decode_bin_reset_buffering:
3264 * Enables buffering on the last multiqueue of each group only,
3265 * disabling the rest
3269 gst_decode_bin_reset_buffering (GstDecodeBin * dbin)
3271 if (!dbin->use_buffering)
3274 GST_DEBUG_OBJECT (dbin, "Resetting multiqueues buffering");
3275 if (dbin->decode_chain) {
3276 CHAIN_MUTEX_LOCK (dbin->decode_chain);
3277 gst_decode_chain_reset_buffering (dbin->decode_chain);
3278 CHAIN_MUTEX_UNLOCK (dbin->decode_chain);
3283 * GstDecodeChain functions
3287 gst_decode_chain_reset_buffering (GstDecodeChain * chain)
3289 GstDecodeGroup *group;
3291 group = chain->active_group;
3292 GST_LOG_OBJECT (chain->dbin, "Resetting chain %p buffering, active group: %p",
3295 return gst_decode_group_reset_buffering (group);
3300 /* gst_decode_chain_get_current_group:
3302 * Returns the current group of this chain, to which
3303 * new chains should be attached or NULL if the last
3304 * group didn't have no-more-pads.
3306 * Not MT-safe: Call with parent chain lock!
3308 static GstDecodeGroup *
3309 gst_decode_chain_get_current_group (GstDecodeChain * chain)
3311 GstDecodeGroup *group;
3313 if (!chain->next_groups && chain->active_group
3314 && chain->active_group->overrun && !chain->active_group->no_more_pads) {
3315 GST_WARNING_OBJECT (chain->dbin,
3316 "Currently active group %p is exposed"
3317 " and wants to add a new pad without having signaled no-more-pads",
3318 chain->active_group);
3322 if (chain->next_groups && (group = chain->next_groups->data) && group->overrun
3323 && !group->no_more_pads) {
3324 GST_WARNING_OBJECT (chain->dbin,
3325 "Currently newest pending group %p "
3326 "had overflow but didn't signal no-more-pads", group);
3330 /* Now we know that we can really return something useful */
3331 if (!chain->active_group) {
3332 chain->active_group = group = gst_decode_group_new (chain->dbin, chain);
3333 } else if (!chain->active_group->overrun
3334 && !chain->active_group->no_more_pads) {
3335 group = chain->active_group;
3339 for (iter = chain->next_groups; iter; iter = g_list_next (iter)) {
3340 GstDecodeGroup *next_group = iter->data;
3342 if (!next_group->overrun && !next_group->no_more_pads) {
3349 group = gst_decode_group_new (chain->dbin, chain);
3350 chain->next_groups = g_list_append (chain->next_groups, group);
3356 static void gst_decode_group_free_internal (GstDecodeGroup * group,
3360 gst_decode_chain_unref (GstDecodeChain * chain)
3362 if (g_atomic_int_dec_and_test (&chain->refs)) {
3363 g_mutex_clear (&chain->lock);
3364 g_slice_free (GstDecodeChain, chain);
3368 static GstDecodeChain *
3369 gst_decode_chain_ref (GstDecodeChain * chain)
3371 g_atomic_int_inc (&chain->refs);
3376 gst_decode_chain_free_internal (GstDecodeChain * chain, gboolean hide)
3378 GList *l, *set_to_null = NULL;
3380 CHAIN_MUTEX_LOCK (chain);
3382 GST_DEBUG_OBJECT (chain->dbin, "%s chain %p", (hide ? "Hiding" : "Freeing"),
3385 if (chain->active_group) {
3386 gst_decode_group_free_internal (chain->active_group, hide);
3388 chain->active_group = NULL;
3391 for (l = chain->next_groups; l; l = l->next) {
3392 gst_decode_group_free_internal ((GstDecodeGroup *) l->data, hide);
3397 g_list_free (chain->next_groups);
3398 chain->next_groups = NULL;
3402 for (l = chain->old_groups; l; l = l->next) {
3403 GstDecodeGroup *group = l->data;
3405 gst_decode_group_free (group);
3407 g_list_free (chain->old_groups);
3408 chain->old_groups = NULL;
3411 for (l = chain->pending_pads; l; l = l->next) {
3412 GstPendingPad *ppad = l->data;
3413 gst_pending_pad_free (ppad);
3416 g_list_free (chain->pending_pads);
3417 chain->pending_pads = NULL;
3419 for (l = chain->elements; l; l = l->next) {
3420 GstDecodeElement *delem = l->data;
3421 GstElement *element = delem->element;
3423 if (delem->pad_added_id)
3424 g_signal_handler_disconnect (element, delem->pad_added_id);
3425 delem->pad_added_id = 0;
3426 if (delem->pad_removed_id)
3427 g_signal_handler_disconnect (element, delem->pad_removed_id);
3428 delem->pad_removed_id = 0;
3429 if (delem->no_more_pads_id)
3430 g_signal_handler_disconnect (element, delem->no_more_pads_id);
3431 delem->no_more_pads_id = 0;
3433 if (delem->capsfilter) {
3434 if (GST_OBJECT_PARENT (delem->capsfilter) ==
3435 GST_OBJECT_CAST (chain->dbin))
3436 gst_bin_remove (GST_BIN_CAST (chain->dbin), delem->capsfilter);
3439 g_list_append (set_to_null, gst_object_ref (delem->capsfilter));
3443 if (GST_OBJECT_PARENT (element) == GST_OBJECT_CAST (chain->dbin))
3444 gst_bin_remove (GST_BIN_CAST (chain->dbin), element);
3446 set_to_null = g_list_append (set_to_null, gst_object_ref (element));
3449 SUBTITLE_LOCK (chain->dbin);
3450 /* remove possible subtitle element */
3451 chain->dbin->subtitles = g_list_remove (chain->dbin->subtitles, element);
3452 SUBTITLE_UNLOCK (chain->dbin);
3455 if (delem->capsfilter) {
3456 gst_object_unref (delem->capsfilter);
3457 delem->capsfilter = NULL;
3460 gst_object_unref (element);
3463 g_slice_free (GstDecodeElement, delem);
3467 g_list_free (chain->elements);
3468 chain->elements = NULL;
3471 if (chain->endpad) {
3472 if (chain->endpad->exposed) {
3473 gst_element_remove_pad (GST_ELEMENT_CAST (chain->dbin),
3474 GST_PAD_CAST (chain->endpad));
3477 decode_pad_set_target (chain->endpad, NULL);
3478 chain->endpad->exposed = FALSE;
3480 gst_object_unref (chain->endpad);
3481 chain->endpad = NULL;
3485 if (!hide && chain->current_pad) {
3486 gst_object_unref (chain->current_pad);
3487 chain->current_pad = NULL;
3491 gst_object_unref (chain->pad);
3495 if (chain->endcaps) {
3496 gst_caps_unref (chain->endcaps);
3497 chain->endcaps = NULL;
3499 g_free (chain->deadend_details);
3500 chain->deadend_details = NULL;
3502 GST_DEBUG_OBJECT (chain->dbin, "%s chain %p", (hide ? "Hidden" : "Freed"),
3504 CHAIN_MUTEX_UNLOCK (chain);
3506 while (set_to_null) {
3507 GstElement *element = set_to_null->data;
3508 set_to_null = g_list_delete_link (set_to_null, set_to_null);
3509 gst_element_set_state (element, GST_STATE_NULL);
3510 gst_object_unref (element);
3514 gst_decode_chain_unref (chain);
3517 /* gst_decode_chain_free:
3519 * Completely frees and removes the chain and all
3520 * child groups from decodebin.
3522 * MT-safe, don't hold the chain lock or any child chain's lock
3523 * when calling this!
3526 gst_decode_chain_free (GstDecodeChain * chain)
3528 gst_decode_chain_free_internal (chain, FALSE);
3531 /* gst_decode_chain_new:
3533 * Creates a new decode chain and initializes it.
3535 * It's up to the caller to add it to the list of child chains of
3538 static GstDecodeChain *
3539 gst_decode_chain_new (GstDecodeBin * dbin, GstDecodeGroup * parent,
3542 GstDecodeChain *chain = g_slice_new0 (GstDecodeChain);
3544 GST_DEBUG_OBJECT (dbin, "Creating new chain %p with parent group %p", chain,
3548 chain->parent = parent;
3550 g_mutex_init (&chain->lock);
3551 chain->pad = gst_object_ref (pad);
3557 * GstDecodeGroup functions
3560 /* The overrun callback is used to expose groups that have not yet had their
3561 * no_more_pads called while the (large) multiqueue overflowed. When this
3562 * happens we must assume that the no_more_pads will not arrive anymore and we
3563 * must expose the pads that we have.
3566 multi_queue_overrun_cb (GstElement * queue, GstDecodeGroup * group)
3572 GST_LOG_OBJECT (dbin, "multiqueue '%s' (%p) is full", GST_OBJECT_NAME (queue),
3575 group->overrun = TRUE;
3576 /* this group has prerolled enough to not need more pads,
3577 * we can probably set its buffering state to playing now */
3578 GST_DEBUG_OBJECT (group->dbin, "Setting group %p multiqueue to "
3579 "'playing' buffering mode", group);
3580 decodebin_set_queue_size (group->dbin, group->multiqueue, FALSE,
3581 (group->parent ? group->parent->seekable : TRUE));
3583 /* FIXME: We should make sure that everything gets exposed now
3584 * even if child chains are not complete because the will never
3585 * be complete! Ignore any non-complete chains when exposing
3586 * and never expose them later
3590 if (dbin->decode_chain) {
3591 if (gst_decode_chain_is_complete (dbin->decode_chain)) {
3592 if (!gst_decode_bin_expose (dbin))
3593 GST_WARNING_OBJECT (dbin, "Couldn't expose group");
3596 EXPOSE_UNLOCK (dbin);
3600 gst_decode_group_free_internal (GstDecodeGroup * group, gboolean hide)
3604 GST_DEBUG_OBJECT (group->dbin, "%s group %p", (hide ? "Hiding" : "Freeing"),
3608 for (l = group->demuxer_pad_probe_ids; l != NULL; l = l->next) {
3609 GstDemuxerPad *demuxer_pad = l->data;
3610 GstPad *sinkpad = g_weak_ref_get (&demuxer_pad->weakPad);
3612 if (sinkpad != NULL) {
3613 gst_pad_remove_probe (sinkpad, demuxer_pad->event_probe_id);
3614 gst_pad_remove_probe (sinkpad, demuxer_pad->query_probe_id);
3615 g_weak_ref_clear (&demuxer_pad->weakPad);
3616 gst_object_unref (sinkpad);
3618 g_free (demuxer_pad);
3620 g_list_free (group->demuxer_pad_probe_ids);
3621 group->demuxer_pad_probe_ids = NULL;
3624 for (l = group->children; l; l = l->next) {
3625 GstDecodeChain *chain = (GstDecodeChain *) l->data;
3627 gst_decode_chain_free_internal (chain, hide);
3632 g_list_free (group->children);
3633 group->children = NULL;
3637 for (l = group->reqpads; l; l = l->next) {
3638 GstPad *pad = l->data;
3640 gst_element_release_request_pad (group->multiqueue, pad);
3641 gst_object_unref (pad);
3644 g_list_free (group->reqpads);
3645 group->reqpads = NULL;
3648 if (group->multiqueue) {
3649 if (group->overrunsig) {
3650 g_signal_handler_disconnect (group->multiqueue, group->overrunsig);
3651 group->overrunsig = 0;
3654 if (GST_OBJECT_PARENT (group->multiqueue) == GST_OBJECT_CAST (group->dbin))
3655 gst_bin_remove (GST_BIN_CAST (group->dbin), group->multiqueue);
3657 gst_element_set_state (group->multiqueue, GST_STATE_NULL);
3658 gst_object_unref (group->multiqueue);
3659 group->multiqueue = NULL;
3663 GST_DEBUG_OBJECT (group->dbin, "%s group %p", (hide ? "Hid" : "Freed"),
3666 g_slice_free (GstDecodeGroup, group);
3669 /* gst_decode_group_free:
3671 * Completely frees and removes the decode group and all
3674 * Never call this from any streaming thread!
3676 * Not MT-safe, call with parent's chain lock!
3679 gst_decode_group_free (GstDecodeGroup * group)
3681 gst_decode_group_free_internal (group, FALSE);
3684 /* gst_decode_group_hide:
3686 * Hide the decode group only, this means that
3687 * all child endpads are removed from decodebin
3688 * and all signals are unconnected.
3690 * No element is set to NULL state and completely
3693 * Can be called from streaming threads.
3695 * Not MT-safe, call with parent's chain lock!
3698 gst_decode_group_hide (GstDecodeGroup * group)
3700 gst_decode_group_free_internal (group, TRUE);
3703 /* gst_decode_chain_free_hidden_groups:
3705 * Frees any decode groups that were hidden previously.
3706 * This allows keeping memory use from ballooning when
3707 * switching chains repeatedly.
3709 * A new throwaway thread will be created to free the
3710 * groups, so any delay does not block the setup of a
3713 * Not MT-safe, call with parent's chain lock!
3716 gst_decode_chain_free_hidden_groups (GList * old_groups)
3720 for (l = old_groups; l; l = l->next) {
3721 GstDecodeGroup *group = l->data;
3723 gst_decode_group_free (group);
3725 g_list_free (old_groups);
3729 gst_decode_chain_start_free_hidden_groups_thread (GstDecodeChain * chain)
3732 GError *error = NULL;
3734 GstDecodeBin *dbin = chain->dbin;
3736 old_groups = chain->old_groups;
3740 /* If we already have a thread running, wait for it to finish */
3741 g_mutex_lock (&dbin->cleanup_lock);
3742 if (dbin->cleanup_thread) {
3743 g_thread_join (dbin->cleanup_thread);
3744 dbin->cleanup_thread = NULL;
3747 chain->old_groups = NULL;
3749 if (dbin->shutdown) {
3750 /* If we're shutting down, add the groups to be cleaned up in the
3751 * state change handler (which *is* another thread). Also avoids
3752 * playing racy games with the state change handler */
3753 dbin->cleanup_groups = g_list_concat (dbin->cleanup_groups, old_groups);
3754 g_mutex_unlock (&dbin->cleanup_lock);
3758 thread = g_thread_try_new ("free-hidden-groups",
3759 (GThreadFunc) gst_decode_chain_free_hidden_groups, old_groups, &error);
3760 if (!thread || error) {
3761 GST_ERROR ("Failed to start free-hidden-groups thread: %s",
3762 error ? error->message : "unknown reason");
3763 g_clear_error (&error);
3764 chain->old_groups = old_groups;
3765 g_mutex_unlock (&dbin->cleanup_lock);
3769 dbin->cleanup_thread = thread;
3770 g_mutex_unlock (&dbin->cleanup_lock);
3772 GST_DEBUG_OBJECT (chain->dbin, "Started free-hidden-groups thread");
3776 decodebin_set_queue_size (GstDecodeBin * dbin, GstElement * multiqueue,
3777 gboolean preroll, gboolean seekable)
3779 gboolean use_buffering;
3781 /* get the current config from the multiqueue */
3782 g_object_get (multiqueue, "use-buffering", &use_buffering, NULL);
3784 decodebin_set_queue_size_full (dbin, multiqueue, use_buffering, preroll,
3788 /* configure queue sizes, this depends on the buffering method and if we are
3789 * playing or prerolling. */
3791 decodebin_set_queue_size_full (GstDecodeBin * dbin, GstElement * multiqueue,
3792 gboolean use_buffering, gboolean preroll, gboolean seekable)
3794 guint max_bytes, max_buffers;
3797 GST_DEBUG_OBJECT (multiqueue, "use buffering %d", use_buffering);
3799 if (preroll || use_buffering) {
3800 /* takes queue limits, initially we only queue up up to the max bytes limit,
3801 * with a default of 2MB. we use the same values for buffering mode. */
3802 if (preroll || (max_bytes = dbin->max_size_bytes) == 0)
3803 max_bytes = AUTO_PREROLL_SIZE_BYTES;
3804 if (preroll || (max_buffers = dbin->max_size_buffers) == 0)
3805 max_buffers = AUTO_PREROLL_SIZE_BUFFERS;
3806 if (preroll || (max_time = dbin->max_size_time) == 0) {
3807 if (dbin->use_buffering && !preroll)
3808 max_time = 5 * GST_SECOND;
3810 max_time = seekable ? AUTO_PREROLL_SEEKABLE_SIZE_TIME :
3811 AUTO_PREROLL_NOT_SEEKABLE_SIZE_TIME;
3814 /* update runtime limits. At runtime, we try to keep the amount of buffers
3815 * in the queues as low as possible (but at least 5 buffers). */
3816 if (dbin->use_buffering)
3818 else if ((max_bytes = dbin->max_size_bytes) == 0)
3819 max_bytes = AUTO_PLAY_SIZE_BYTES;
3820 if ((max_buffers = dbin->max_size_buffers) == 0)
3821 max_buffers = AUTO_PLAY_SIZE_BUFFERS;
3822 /* this is a multiqueue with disabled buffering, don't limit max_time */
3823 if (dbin->use_buffering)
3825 else if ((max_time = dbin->max_size_time) == 0)
3826 max_time = AUTO_PLAY_SIZE_TIME;
3829 GST_DEBUG_OBJECT (multiqueue, "setting limits %u bytes, %u buffers, "
3830 "%" G_GUINT64_FORMAT " time", max_bytes, max_buffers, max_time);
3831 g_object_set (multiqueue,
3832 "max-size-bytes", max_bytes, "max-size-time", max_time,
3833 "max-size-buffers", max_buffers, NULL);
3836 /* gst_decode_group_new:
3837 * @dbin: Parent decodebin
3838 * @parent: Parent chain or %NULL
3840 * Creates a new GstDecodeGroup. It is up to the caller to add it to the list
3843 static GstDecodeGroup *
3844 gst_decode_group_new (GstDecodeBin * dbin, GstDecodeChain * parent)
3846 GstDecodeGroup *group = g_slice_new0 (GstDecodeGroup);
3850 GST_DEBUG_OBJECT (dbin, "Creating new group %p with parent chain %p", group,
3854 group->parent = parent;
3856 mq = group->multiqueue = gst_element_factory_make ("multiqueue", NULL);
3857 if (G_UNLIKELY (!group->multiqueue))
3858 goto missing_multiqueue;
3860 /* configure queue sizes for preroll */
3862 if (parent && parent->demuxer) {
3863 GstElement *element =
3864 ((GstDecodeElement *) parent->elements->data)->element;
3865 GstPad *pad = gst_element_get_static_pad (element, "sink");
3867 seekable = parent->seekable = check_upstream_seekable (dbin, pad);
3868 gst_object_unref (pad);
3871 decodebin_set_queue_size_full (dbin, mq, FALSE, TRUE, seekable);
3873 group->overrunsig = g_signal_connect (mq, "overrun",
3874 G_CALLBACK (multi_queue_overrun_cb), group);
3875 group->demuxer_pad_probe_ids = NULL;
3877 gst_element_set_state (mq, GST_STATE_PAUSED);
3878 gst_bin_add (GST_BIN (dbin), gst_object_ref (mq));
3885 gst_element_post_message (GST_ELEMENT_CAST (dbin),
3886 gst_missing_element_message_new (GST_ELEMENT_CAST (dbin),
3888 GST_ELEMENT_ERROR (dbin, CORE, MISSING_PLUGIN, (NULL), ("no multiqueue!"));
3889 g_slice_free (GstDecodeGroup, group);
3894 /* gst_decode_group_control_demuxer_pad
3896 * Adds a new demuxer srcpad to the given group.
3898 * Returns the srcpad of the multiqueue corresponding the given pad.
3899 * Returns NULL if there was an error.
3902 gst_decode_group_control_demuxer_pad (GstDecodeGroup * group, GstPad * pad)
3905 GstDemuxerPad *demuxer_pad;
3906 GstPad *srcpad, *sinkpad;
3907 GstIterator *it = NULL;
3908 GValue item = { 0, };
3912 GST_LOG_OBJECT (dbin, "group:%p pad %s:%s", group, GST_DEBUG_PAD_NAME (pad));
3916 if (G_UNLIKELY (!group->multiqueue))
3920 gst_element_request_pad_simple (group->multiqueue, "sink_%u"))) {
3921 GST_ERROR_OBJECT (dbin, "Couldn't get sinkpad from multiqueue");
3925 if ((gst_pad_link_full (pad, sinkpad,
3926 GST_PAD_LINK_CHECK_NOTHING) != GST_PAD_LINK_OK)) {
3927 GST_ERROR_OBJECT (dbin, "Couldn't link demuxer and multiqueue");
3931 it = gst_pad_iterate_internal_links (sinkpad);
3933 if (!it || (gst_iterator_next (it, &item)) != GST_ITERATOR_OK
3934 || ((srcpad = g_value_dup_object (&item)) == NULL)) {
3935 GST_ERROR_OBJECT (dbin,
3936 "Couldn't get srcpad from multiqueue for sinkpad %" GST_PTR_FORMAT,
3941 CHAIN_MUTEX_LOCK (group->parent);
3943 /* Note: GWeakRefs can't be moved in memory once they're in use, so do a
3944 * dedicated alloc for the GstDemuxerPad struct that contains it */
3945 demuxer_pad = g_new0 (GstDemuxerPad, 1);
3946 demuxer_pad->event_probe_id = gst_pad_add_probe (sinkpad,
3947 GST_PAD_PROBE_TYPE_EVENT_UPSTREAM, sink_pad_event_probe, group, NULL);
3948 demuxer_pad->query_probe_id = gst_pad_add_probe (sinkpad,
3949 GST_PAD_PROBE_TYPE_QUERY_UPSTREAM, sink_pad_query_probe, group, NULL);
3951 g_weak_ref_set (&demuxer_pad->weakPad, sinkpad);
3952 group->demuxer_pad_probe_ids =
3953 g_list_prepend (group->demuxer_pad_probe_ids, demuxer_pad);
3955 group->reqpads = g_list_prepend (group->reqpads, gst_object_ref (sinkpad));
3956 CHAIN_MUTEX_UNLOCK (group->parent);
3959 if (G_IS_VALUE (&item))
3960 g_value_unset (&item);
3962 gst_iterator_free (it);
3963 gst_object_unref (sinkpad);
3967 gst_element_release_request_pad (group->multiqueue, sinkpad);
3971 /* gst_decode_group_is_complete:
3973 * Checks if the group is complete, this means that
3974 * a) overrun of the multiqueue or no-more-pads happened
3975 * b) all child chains are complete
3977 * Not MT-safe, always call with decodebin expose lock
3980 gst_decode_group_is_complete (GstDecodeGroup * group)
3983 gboolean complete = TRUE;
3985 if (!group->overrun && !group->no_more_pads) {
3990 for (l = group->children; l; l = l->next) {
3991 GstDecodeChain *chain = l->data;
3993 if (!gst_decode_chain_is_complete (chain)) {
4000 GST_DEBUG_OBJECT (group->dbin, "Group %p is complete: %d", group, complete);
4004 /* gst_decode_chain_is_complete:
4006 * Returns TRUE if the chain is complete, this means either
4007 * a) This chain is a dead end, i.e. we have no suitable plugins
4008 * b) This chain ends in an endpad and this is blocked or exposed
4010 * Not MT-safe, always call with decodebin expose lock
4013 gst_decode_chain_is_complete (GstDecodeChain * chain)
4015 gboolean complete = FALSE;
4017 CHAIN_MUTEX_LOCK (chain);
4018 if (chain->dbin->shutdown)
4021 if (chain->deadend) {
4026 if (chain->endpad && gst_decode_pad_is_exposable (chain->endpad)) {
4031 if (chain->demuxer) {
4032 if (chain->active_group
4033 && gst_decode_group_is_complete (chain->active_group)) {
4040 CHAIN_MUTEX_UNLOCK (chain);
4041 GST_DEBUG_OBJECT (chain->dbin, "Chain %p is complete: %d", chain, complete);
4045 /* Flushing group/chains */
4047 flush_group (GstDecodeGroup * group, gboolean flushing)
4051 GST_DEBUG ("group %p flushing:%d", group, flushing);
4053 if (group->drained == flushing)
4055 for (tmp = group->children; tmp; tmp = tmp->next) {
4056 GstDecodeChain *chain = (GstDecodeChain *) tmp->data;
4057 flush_chain (chain, flushing);
4059 GST_DEBUG ("Setting group %p to drained:%d", group, flushing);
4060 group->drained = flushing;
4064 flush_chain (GstDecodeChain * chain, gboolean flushing)
4067 GstDecodeBin *dbin = chain->dbin;
4069 GST_DEBUG_OBJECT (dbin, "chain %p (pad %s:%s) flushing:%d", chain,
4070 GST_DEBUG_PAD_NAME (chain->pad), flushing);
4071 if (chain->drained == flushing)
4073 /* if unflushing, check if we should switch to last group */
4074 if (flushing == FALSE && chain->next_groups) {
4075 GstDecodeGroup *target_group =
4076 (GstDecodeGroup *) g_list_last (chain->next_groups)->data;
4077 gst_decode_chain_start_free_hidden_groups_thread (chain);
4078 /* Hide active group (we're sure it's not that one we'll be using) */
4079 GST_DEBUG_OBJECT (dbin, "Switching from active group %p to group %p",
4080 chain->active_group, target_group);
4081 gst_decode_group_hide (chain->active_group);
4082 chain->old_groups = g_list_prepend (chain->old_groups, chain->active_group);
4083 chain->active_group = target_group;
4084 /* Hide all groups but the target_group */
4085 for (tmp = chain->next_groups; tmp; tmp = tmp->next) {
4086 GstDecodeGroup *group = (GstDecodeGroup *) tmp->data;
4087 if (group != target_group) {
4088 gst_decode_group_hide (group);
4089 chain->old_groups = g_list_prepend (chain->old_groups, group);
4092 /* Clear next groups */
4093 g_list_free (chain->next_groups);
4094 chain->next_groups = NULL;
4096 /* Mark all groups as flushing */
4097 if (chain->active_group)
4098 flush_group (chain->active_group, flushing);
4099 for (tmp = chain->next_groups; tmp; tmp = tmp->next) {
4100 GstDecodeGroup *group = (GstDecodeGroup *) tmp->data;
4101 flush_group (group, flushing);
4103 GST_DEBUG ("Setting chain %p to drained:%d", chain, flushing);
4104 chain->drained = flushing;
4108 drain_and_switch_chains (GstDecodeChain * chain, GstDecodePad * drainpad,
4109 gboolean * last_group, gboolean * drained, gboolean * switched);
4110 /* drain_and_switch_chains/groups:
4112 * CALL WITH CHAIN LOCK (or group parent) TAKEN !
4114 * Goes down the chains/groups until it finds the chain
4115 * to which the drainpad belongs.
4117 * It marks that pad/chain as drained and then will figure
4118 * out which group to switch to or not.
4120 * last_chain will be set to TRUE if the group to which the
4121 * pad belongs is the last one.
4123 * drained will be set to TRUE if the chain/group is drained.
4125 * Returns: TRUE if the chain contained the target pad */
4127 drain_and_switch_group (GstDecodeGroup * group, GstDecodePad * drainpad,
4128 gboolean * last_group, gboolean * drained, gboolean * switched)
4130 gboolean handled = FALSE;
4133 GST_DEBUG ("Checking group %p (target pad %s:%s)",
4134 group, GST_DEBUG_PAD_NAME (drainpad));
4136 /* Definitely can't be in drained groups */
4137 if (G_UNLIKELY (group->drained)) {
4141 /* Figure out if all our chains are drained with the
4142 * new information */
4143 group->drained = TRUE;
4144 for (tmp = group->children; tmp; tmp = tmp->next) {
4145 GstDecodeChain *chain = (GstDecodeChain *) tmp->data;
4146 gboolean subdrained = FALSE;
4149 drain_and_switch_chains (chain, drainpad, last_group, &subdrained,
4152 group->drained = FALSE;
4156 GST_DEBUG ("group %p (last_group:%d, drained:%d, switched:%d, handled:%d)",
4157 group, *last_group, group->drained, *switched, handled);
4158 *drained = group->drained;
4163 drain_and_switch_chains (GstDecodeChain * chain, GstDecodePad * drainpad,
4164 gboolean * last_group, gboolean * drained, gboolean * switched)
4166 gboolean handled = FALSE;
4167 GstDecodeBin *dbin = chain->dbin;
4169 GST_DEBUG ("Checking chain %p %s:%s (target pad %s:%s)",
4170 chain, GST_DEBUG_PAD_NAME (chain->pad), GST_DEBUG_PAD_NAME (drainpad));
4172 CHAIN_MUTEX_LOCK (chain);
4174 if (chain->pad_probe_id) {
4175 gst_pad_remove_probe (chain->pad, chain->pad_probe_id);
4176 chain->pad_probe_id = 0;
4179 /* Definitely can't be in drained chains */
4180 if (G_UNLIKELY (chain->drained)) {
4184 if (chain->endpad) {
4185 /* Check if we're reached the target endchain */
4186 if (drainpad != NULL && chain == drainpad->chain) {
4187 GST_DEBUG ("Found the target chain");
4188 drainpad->drained = TRUE;
4192 chain->drained = chain->endpad->drained;
4196 /* We known there are groups to switch to */
4197 if (chain->next_groups)
4198 *last_group = FALSE;
4200 /* Check the active group */
4201 if (chain->active_group) {
4202 gboolean subdrained = FALSE;
4203 handled = drain_and_switch_group (chain->active_group, drainpad,
4204 last_group, &subdrained, switched);
4206 /* The group is drained, see if we can switch to another */
4207 if ((handled || drainpad == NULL) && subdrained && !*switched) {
4208 if (chain->next_groups) {
4209 /* Switch to next group */
4210 GST_DEBUG_OBJECT (dbin, "Hiding current group %p", chain->active_group);
4211 gst_decode_group_hide (chain->active_group);
4213 g_list_prepend (chain->old_groups, chain->active_group);
4214 GST_DEBUG_OBJECT (dbin, "Switching to next group %p",
4215 chain->next_groups->data);
4216 chain->active_group = chain->next_groups->data;
4217 chain->next_groups =
4218 g_list_delete_link (chain->next_groups, chain->next_groups);
4219 gst_decode_chain_start_free_hidden_groups_thread (chain);
4221 chain->drained = FALSE;
4223 GST_DEBUG ("Group %p was the last in chain %p", chain->active_group,
4225 chain->drained = TRUE;
4226 /* We're drained ! */
4229 if (subdrained && !chain->next_groups)
4235 CHAIN_MUTEX_UNLOCK (chain);
4237 GST_DEBUG ("Chain %p (handled:%d, last_group:%d, drained:%d, switched:%d)",
4238 chain, handled, *last_group, chain->drained, *switched);
4240 *drained = chain->drained;
4242 if (*drained && !chain->parent) /* only emit signal from top chain */
4243 g_signal_emit (dbin, gst_decode_bin_signals[SIGNAL_DRAINED], 0, NULL);
4248 /* check if the group is drained, meaning all pads have seen an EOS
4251 gst_decode_pad_handle_eos (GstDecodePad * pad)
4253 gboolean last_group = TRUE;
4254 gboolean switched = FALSE;
4255 gboolean drained = FALSE;
4256 GstDecodeChain *chain = pad->chain;
4257 GstDecodeBin *dbin = chain->dbin;
4260 GST_LOG_OBJECT (dbin, "pad %p", pad);
4262 /* Send a stream-group-done event in case downstream needs
4263 * to unblock before we can drain */
4264 tmp = gst_pad_get_sticky_event (GST_PAD (pad), GST_EVENT_STREAM_START, 0);
4267 if (gst_event_parse_group_id (tmp, &group_id)) {
4268 GstPad *peer = gst_pad_get_peer (GST_PAD (pad));
4271 GST_DEBUG_OBJECT (dbin,
4272 "Sending stream-group-done for group %u to pad %"
4273 GST_PTR_FORMAT, group_id, pad);
4274 gst_pad_send_event (peer, gst_event_new_stream_group_done (group_id));
4275 gst_object_unref (peer);
4278 GST_DEBUG_OBJECT (dbin,
4279 "No group ID to send stream-group-done on pad %" GST_PTR_FORMAT, pad);
4281 gst_event_unref (tmp);
4285 if (dbin->decode_chain) {
4286 drain_and_switch_chains (dbin->decode_chain, pad, &last_group, &drained,
4290 /* If we resulted in a group switch, expose what's needed */
4291 if (gst_decode_chain_is_complete (dbin->decode_chain))
4292 gst_decode_bin_expose (dbin);
4295 EXPOSE_UNLOCK (dbin);
4300 /* gst_decode_group_is_drained:
4302 * Check is this group is drained and cache this result.
4303 * The group is drained if all child chains are drained.
4305 * Not MT-safe, call with group->parent's lock */
4307 gst_decode_group_is_drained (GstDecodeGroup * group)
4310 gboolean drained = TRUE;
4312 if (group->drained) {
4317 for (l = group->children; l; l = l->next) {
4318 GstDecodeChain *chain = l->data;
4320 CHAIN_MUTEX_LOCK (chain);
4321 if (!gst_decode_chain_is_drained (chain))
4323 CHAIN_MUTEX_UNLOCK (chain);
4327 group->drained = drained;
4330 GST_DEBUG_OBJECT (group->dbin, "Group %p is drained: %d", group, drained);
4334 /* gst_decode_chain_is_drained:
4336 * Check is the chain is drained, which means that
4339 * a) it's endpad is drained
4340 * b) there are no pending pads, the active group is drained
4341 * and there are no next groups
4343 * Not MT-safe, call with chain lock
4346 gst_decode_chain_is_drained (GstDecodeChain * chain)
4348 gboolean drained = FALSE;
4350 if (chain->endpad) {
4351 drained = chain->endpad->drained;
4355 if (chain->pending_pads) {
4360 if (chain->active_group && gst_decode_group_is_drained (chain->active_group)
4361 && !chain->next_groups) {
4367 GST_DEBUG_OBJECT (chain->dbin, "Chain %p is drained: %d", chain, drained);
4372 gst_decode_group_reset_buffering (GstDecodeGroup * group)
4375 gboolean ret = TRUE;
4377 GST_DEBUG_OBJECT (group->dbin, "Group reset buffering %p %s", group,
4378 GST_ELEMENT_NAME (group->multiqueue));
4379 for (l = group->children; l; l = l->next) {
4380 GstDecodeChain *chain = l->data;
4382 CHAIN_MUTEX_LOCK (chain);
4383 if (!gst_decode_chain_reset_buffering (chain)) {
4386 CHAIN_MUTEX_UNLOCK (chain);
4389 decodebin_set_queue_size_full (group->dbin, group->multiqueue, !ret,
4390 FALSE, (group->parent ? group->parent->seekable : TRUE));
4393 /* all chains are buffering already, no need to do it here */
4394 g_object_set (group->multiqueue, "use-buffering", FALSE, NULL);
4396 g_object_set (group->multiqueue, "use-buffering", TRUE,
4397 "low-percent", group->dbin->low_percent,
4398 "high-percent", group->dbin->high_percent, NULL);
4401 GST_DEBUG_OBJECT (group->dbin, "Setting %s buffering to %d",
4402 GST_ELEMENT_NAME (group->multiqueue), !ret);
4408 * GCompareFunc to use with lists of GstPad.
4409 * Sorts pads by mime type.
4410 * First video (raw, then non-raw), then audio (raw, then non-raw),
4413 * Return: negative if a<b, 0 if a==b, positive if a>b
4416 sort_end_pads (GstDecodePad * da, GstDecodePad * db)
4419 GstCaps *capsa, *capsb;
4420 GstStructure *sa, *sb;
4421 const gchar *namea, *nameb;
4425 capsa = get_pad_caps (GST_PAD_CAST (da));
4426 capsb = get_pad_caps (GST_PAD_CAST (db));
4428 sa = gst_caps_get_structure ((const GstCaps *) capsa, 0);
4429 sb = gst_caps_get_structure ((const GstCaps *) capsb, 0);
4431 namea = gst_structure_get_name (sa);
4432 nameb = gst_structure_get_name (sb);
4434 if (g_strrstr (namea, "video/x-raw"))
4436 else if (g_strrstr (namea, "video/"))
4438 else if (g_strrstr (namea, "audio/x-raw"))
4440 else if (g_strrstr (namea, "audio/"))
4445 if (g_strrstr (nameb, "video/x-raw"))
4447 else if (g_strrstr (nameb, "video/"))
4449 else if (g_strrstr (nameb, "audio/x-raw"))
4451 else if (g_strrstr (nameb, "audio/"))
4456 gst_caps_unref (capsa);
4457 gst_caps_unref (capsb);
4462 /* if otherwise the same, sort by stream-id */
4463 ida = gst_pad_get_stream_id (GST_PAD_CAST (da));
4464 idb = gst_pad_get_stream_id (GST_PAD_CAST (db));
4465 ret = (ida) ? ((idb) ? strcmp (ida, idb) : -1) : 1;
4473 _gst_element_get_linked_caps (GstElement * src, GstElement * sink,
4474 GstElement * capsfilter, GstPad ** srcpad)
4479 gboolean done = FALSE;
4480 GstCaps *caps = NULL;
4481 GValue item = { 0, };
4483 it = gst_element_iterate_src_pads (src);
4485 switch (gst_iterator_next (it, &item)) {
4486 case GST_ITERATOR_OK:
4487 pad = g_value_get_object (&item);
4488 peer = gst_pad_get_peer (pad);
4490 parent = gst_pad_get_parent_element (peer);
4491 if (parent == sink || (capsfilter != NULL && parent == capsfilter)) {
4492 caps = gst_pad_get_current_caps (pad);
4493 *srcpad = gst_object_ref (pad);
4498 gst_object_unref (parent);
4499 gst_object_unref (peer);
4501 g_value_reset (&item);
4503 case GST_ITERATOR_RESYNC:
4504 gst_iterator_resync (it);
4506 case GST_ITERATOR_ERROR:
4507 case GST_ITERATOR_DONE:
4512 g_value_unset (&item);
4513 gst_iterator_free (it);
4518 /* FIXME: Invent gst_structure_take_structure() to prevent all the
4519 * structure copying for nothing
4521 static GstStructure *
4522 gst_decode_chain_get_topology (GstDecodeChain * chain)
4524 GstStructure *s, *u;
4528 if (G_UNLIKELY ((chain->endpad || chain->deadend)
4529 && (chain->endcaps == NULL))) {
4530 GST_WARNING ("End chain without valid caps !");
4534 u = gst_structure_new_id_empty (topology_structure_name);
4536 /* Now at the last element */
4537 if ((chain->elements || !chain->active_group) &&
4538 (chain->endpad || chain->deadend)) {
4541 s = gst_structure_new_id_empty (topology_structure_name);
4542 gst_structure_id_set (u, topology_caps, GST_TYPE_CAPS, chain->endcaps,
4545 if (chain->endpad) {
4546 gst_structure_id_set (u, topology_pad, GST_TYPE_PAD, chain->endpad, NULL);
4548 srcpad = gst_ghost_pad_get_target (GST_GHOST_PAD_CAST (chain->endpad));
4549 gst_structure_id_set (u, topology_element_srcpad, GST_TYPE_PAD,
4552 gst_object_unref (srcpad);
4555 gst_structure_id_set (s, topology_next, GST_TYPE_STRUCTURE, u, NULL);
4556 gst_structure_free (u);
4558 } else if (chain->active_group) {
4559 GValue list = { 0, };
4560 GValue item = { 0, };
4562 g_value_init (&list, GST_TYPE_LIST);
4563 g_value_init (&item, GST_TYPE_STRUCTURE);
4564 for (l = chain->active_group->children; l; l = l->next) {
4565 s = gst_decode_chain_get_topology (l->data);
4567 gst_value_set_structure (&item, s);
4568 gst_value_list_append_value (&list, &item);
4569 g_value_reset (&item);
4570 gst_structure_free (s);
4573 gst_structure_id_set_value (u, topology_next, &list);
4574 g_value_unset (&list);
4575 g_value_unset (&item);
4578 /* Get caps between all elements in this chain */
4579 l = (chain->elements && chain->elements->next) ? chain->elements : NULL;
4580 for (; l && l->next; l = l->next) {
4581 GstDecodeElement *delem, *delem_next;
4582 GstElement *elem, *capsfilter, *elem_next;
4587 elem = delem->element;
4588 delem_next = l->next->data;
4589 elem_next = delem_next->element;
4590 capsfilter = delem_next->capsfilter;
4593 caps = _gst_element_get_linked_caps (elem_next, elem, capsfilter, &srcpad);
4596 s = gst_structure_new_id_empty (topology_structure_name);
4597 gst_structure_id_set (u, topology_caps, GST_TYPE_CAPS, caps, NULL);
4598 gst_caps_unref (caps);
4600 gst_structure_id_set (s, topology_next, GST_TYPE_STRUCTURE, u, NULL);
4601 gst_structure_free (u);
4606 gst_structure_id_set (u, topology_element_srcpad, GST_TYPE_PAD, srcpad,
4608 gst_object_unref (srcpad);
4612 /* Caps that resulted in this chain */
4613 caps = get_pad_caps (chain->pad);
4614 if (G_UNLIKELY (!caps)) {
4615 GST_WARNING_OBJECT (chain->pad, "Couldn't get the caps of decode chain");
4618 gst_structure_id_set (u, topology_caps, GST_TYPE_CAPS, caps, NULL);
4619 gst_structure_id_set (u, topology_element_srcpad, GST_TYPE_PAD, chain->pad,
4621 gst_caps_unref (caps);
4627 gst_decode_bin_post_topology_message (GstDecodeBin * dbin)
4632 s = gst_decode_chain_get_topology (dbin->decode_chain);
4634 if (G_UNLIKELY (s == NULL))
4636 msg = gst_message_new_element (GST_OBJECT (dbin), s);
4637 gst_element_post_message (GST_ELEMENT (dbin), msg);
4641 debug_sticky_event (GstPad * pad, GstEvent ** event, gpointer user_data)
4643 GST_DEBUG_OBJECT (pad, "sticky event %s (%p)", GST_EVENT_TYPE_NAME (*event),
4649 /* Must only be called if the toplevel chain is complete and blocked! */
4650 /* Not MT-safe, call with decodebin expose lock! */
4652 gst_decode_bin_expose (GstDecodeBin * dbin)
4654 GList *tmp, *endpads;
4655 gboolean missing_plugin;
4656 GString *missing_plugin_details;
4657 gboolean already_exposed;
4658 gboolean last_group;
4662 missing_plugin = FALSE;
4663 already_exposed = TRUE;
4666 missing_plugin_details = g_string_new ("");
4668 GST_DEBUG_OBJECT (dbin, "Exposing currently active chains/groups");
4670 /* Don't expose if we're currently shutting down */
4672 if (G_UNLIKELY (dbin->shutdown)) {
4673 GST_WARNING_OBJECT (dbin, "Currently, shutting down, aborting exposing");
4679 /* Get the pads that we're going to expose and mark things as exposed */
4680 if (!gst_decode_chain_expose (dbin->decode_chain, &endpads, &missing_plugin,
4681 missing_plugin_details, &last_group)) {
4682 g_list_foreach (endpads, (GFunc) gst_object_unref, NULL);
4683 g_list_free (endpads);
4684 g_string_free (missing_plugin_details, TRUE);
4685 /* Failures could be due to the fact that we are currently shutting down (recheck) */
4687 if (G_UNLIKELY (dbin->shutdown)) {
4688 GST_WARNING_OBJECT (dbin, "Currently, shutting down, aborting exposing");
4693 GST_ERROR_OBJECT (dbin, "Broken chain/group tree");
4694 g_return_val_if_reached (FALSE);
4697 if (endpads == NULL) {
4698 if (missing_plugin) {
4699 if (missing_plugin_details->len > 0) {
4700 gchar *details = g_string_free (missing_plugin_details, FALSE);
4701 GST_ELEMENT_ERROR (dbin, CORE, MISSING_PLUGIN, (NULL),
4702 ("no suitable plugins found:\n%s", details));
4705 g_string_free (missing_plugin_details, TRUE);
4706 GST_ELEMENT_ERROR (dbin, CORE, MISSING_PLUGIN, (NULL),
4707 ("no suitable plugins found"));
4710 /* in this case, the stream ended without buffers,
4711 * just post a warning */
4712 g_string_free (missing_plugin_details, TRUE);
4714 GST_WARNING_OBJECT (dbin, "All streams finished without buffers. "
4715 "Last group: %d", last_group);
4717 GST_ELEMENT_ERROR (dbin, STREAM, FAILED, (NULL),
4718 ("all streams without buffers"));
4720 gboolean switched = FALSE;
4721 gboolean drained = FALSE;
4723 drain_and_switch_chains (dbin->decode_chain, NULL, &last_group,
4724 &drained, &switched);
4725 GST_ELEMENT_WARNING (dbin, STREAM, FAILED, (NULL),
4726 ("all streams without buffers"));
4728 if (gst_decode_chain_is_complete (dbin->decode_chain))
4736 do_async_done (dbin);
4740 g_string_free (missing_plugin_details, TRUE);
4742 /* Check if this was called when everything was exposed already */
4743 for (tmp = endpads; tmp && already_exposed; tmp = tmp->next) {
4744 GstDecodePad *dpad = tmp->data;
4746 already_exposed &= dpad->exposed;
4747 if (!already_exposed)
4750 if (already_exposed) {
4751 GST_DEBUG_OBJECT (dbin, "Everything was exposed already!");
4752 g_list_foreach (endpads, (GFunc) gst_object_unref, NULL);
4753 g_list_free (endpads);
4757 /* going to expose something, reset buffering */
4758 gst_decode_bin_reset_buffering (dbin);
4760 /* Set all already exposed pads to blocked */
4761 for (tmp = endpads; tmp; tmp = tmp->next) {
4762 GstDecodePad *dpad = tmp->data;
4764 if (dpad->exposed) {
4765 GST_DEBUG_OBJECT (dpad, "blocking exposed pad");
4766 gst_decode_pad_set_blocked (dpad, TRUE);
4770 /* re-order pads : video, then audio, then others */
4771 endpads = g_list_sort (endpads, (GCompareFunc) sort_end_pads);
4773 /* Don't add pads if we are shutting down */
4775 if (G_UNLIKELY (dbin->shutdown)) {
4776 GST_WARNING_OBJECT (dbin, "Currently, shutting down, aborting exposing");
4782 for (tmp = endpads; tmp; tmp = tmp->next) {
4783 GstDecodePad *dpad = (GstDecodePad *) tmp->data;
4786 /* 1. rewrite name */
4787 padname = g_strdup_printf ("src_%u", dbin->nbpads);
4789 GST_DEBUG_OBJECT (dbin, "About to expose dpad %s as %s",
4790 GST_OBJECT_NAME (dpad), padname);
4791 gst_object_set_name (GST_OBJECT (dpad), padname);
4794 gst_pad_sticky_events_foreach (GST_PAD_CAST (dpad), debug_sticky_event,
4797 /* 2. activate and add */
4798 if (!dpad->exposed) {
4799 dpad->exposed = TRUE;
4800 if (!gst_element_add_pad (GST_ELEMENT (dbin), GST_PAD_CAST (dpad))) {
4801 /* not really fatal, we can try to add the other pads */
4802 g_warning ("error adding pad to decodebin");
4803 dpad->exposed = FALSE;
4808 /* 3. emit signal */
4809 GST_INFO_OBJECT (dpad, "added new decoded pad");
4813 /* 4. Signal no-more-pads. This allows the application to hook stuff to the
4815 GST_LOG_OBJECT (dbin, "signaling no-more-pads");
4816 gst_element_no_more_pads (GST_ELEMENT (dbin));
4818 /* 5. Send a custom element message with the stream topology */
4819 if (dbin->post_stream_topology)
4820 gst_decode_bin_post_topology_message (dbin);
4822 /* 6. Unblock internal pads. The application should have connected stuff now
4823 * so that streaming can continue. */
4824 for (tmp = endpads; tmp; tmp = tmp->next) {
4825 GstDecodePad *dpad = (GstDecodePad *) tmp->data;
4827 GST_DEBUG_OBJECT (dpad, "unblocking");
4828 gst_decode_pad_unblock (dpad);
4829 GST_DEBUG_OBJECT (dpad, "unblocked");
4830 gst_object_unref (dpad);
4832 g_list_free (endpads);
4834 do_async_done (dbin);
4835 GST_DEBUG_OBJECT (dbin, "Exposed everything");
4839 /* gst_decode_chain_expose:
4841 * Check if the chain can be exposed and add all endpads
4842 * to the endpads list.
4844 * Also update the active group's multiqueue to the
4847 * Not MT-safe, call with decodebin expose lock! *
4850 gst_decode_chain_expose (GstDecodeChain * chain, GList ** endpads,
4851 gboolean * missing_plugin, GString * missing_plugin_details,
4852 gboolean * last_group)
4854 GstDecodeGroup *group;
4858 if (chain->deadend) {
4859 if (chain->endcaps) {
4860 if (chain->deadend_details) {
4861 g_string_append (missing_plugin_details, chain->deadend_details);
4862 g_string_append_c (missing_plugin_details, '\n');
4864 gchar *desc = gst_pb_utils_get_codec_description (chain->endcaps);
4865 gchar *caps_str = gst_caps_to_string (chain->endcaps);
4866 g_string_append_printf (missing_plugin_details,
4867 "Missing decoder: %s (%s)\n", desc, caps_str);
4871 *missing_plugin = TRUE;
4876 if (chain->endpad) {
4877 if (!gst_decode_pad_is_exposable (chain->endpad) && !chain->endpad->exposed)
4879 *endpads = g_list_prepend (*endpads, gst_object_ref (chain->endpad));
4883 if (chain->next_groups)
4884 *last_group = FALSE;
4886 group = chain->active_group;
4889 if (!group->no_more_pads && !group->overrun)
4894 /* we can now disconnect any overrun signal, which is used to expose the
4896 if (group->overrunsig) {
4897 GST_LOG_OBJECT (dbin, "Disconnecting overrun");
4898 g_signal_handler_disconnect (group->multiqueue, group->overrunsig);
4899 group->overrunsig = 0;
4902 for (l = group->children; l; l = l->next) {
4903 GstDecodeChain *childchain = l->data;
4905 if (!gst_decode_chain_expose (childchain, endpads, missing_plugin,
4906 missing_plugin_details, last_group))
4913 /*************************
4914 * GstDecodePad functions
4915 *************************/
4918 gst_decode_pad_class_init (GstDecodePadClass * klass)
4923 gst_decode_pad_init (GstDecodePad * pad)
4926 pad->blocked = FALSE;
4927 pad->exposed = FALSE;
4928 pad->drained = FALSE;
4929 gst_object_ref_sink (pad);
4932 static GstPadProbeReturn
4933 source_pad_blocked_cb (GstPad * pad, GstPadProbeInfo * info, gpointer user_data)
4935 GstDecodePad *dpad = user_data;
4936 GstDecodeChain *chain;
4938 GstPadProbeReturn ret = GST_PAD_PROBE_OK;
4940 if (GST_PAD_PROBE_INFO_TYPE (info) & GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM) {
4941 GstEvent *event = GST_PAD_PROBE_INFO_EVENT (info);
4943 GST_LOG_OBJECT (pad, "Seeing event '%s'", GST_EVENT_TYPE_NAME (event));
4945 if (!GST_EVENT_IS_SERIALIZED (event)) {
4946 /* do not block on sticky or out of band events otherwise the allocation query
4947 from demuxer might block the loop thread */
4948 GST_LOG_OBJECT (pad, "Letting OOB event through");
4949 return GST_PAD_PROBE_PASS;
4952 if (GST_EVENT_IS_STICKY (event) && GST_EVENT_TYPE (event) != GST_EVENT_EOS) {
4953 /* manually push sticky events to ghost pad to avoid exposing pads
4954 * that don't have the sticky events. Handle EOS separately as we
4955 * want to block the pad on it if we didn't get any buffers before
4956 * EOS and expose the pad then. */
4957 gst_pad_push_event (GST_PAD_CAST (dpad), gst_event_ref (event));
4959 /* let the sticky events pass */
4960 ret = GST_PAD_PROBE_PASS;
4962 /* we only want to try to expose on CAPS events */
4963 if (GST_EVENT_TYPE (event) != GST_EVENT_CAPS) {
4964 GST_LOG_OBJECT (pad, "Letting sticky non-CAPS event through");
4968 } else if (GST_PAD_PROBE_INFO_TYPE (info) &
4969 GST_PAD_PROBE_TYPE_QUERY_DOWNSTREAM) {
4970 GstQuery *query = GST_PAD_PROBE_INFO_QUERY (info);
4972 if (!GST_QUERY_IS_SERIALIZED (query)) {
4973 /* do not block on non-serialized queries */
4974 GST_LOG_OBJECT (pad, "Letting non-serialized query through");
4975 return GST_PAD_PROBE_PASS;
4977 if (!gst_pad_has_current_caps (pad)) {
4978 /* do not block on allocation queries before we have caps,
4979 * this would deadlock because we are doing no autoplugging
4981 * TODO: Try to do autoplugging based on the query caps
4983 GST_LOG_OBJECT (pad, "Letting serialized query before caps through");
4984 return GST_PAD_PROBE_PASS;
4987 chain = dpad->chain;
4990 GST_LOG_OBJECT (dpad, "blocked: dpad->chain:%p", chain);
4992 dpad->blocked = TRUE;
4995 if (dbin->decode_chain) {
4996 if (gst_decode_chain_is_complete (dbin->decode_chain)) {
4997 if (!gst_decode_bin_expose (dbin))
4998 GST_WARNING_OBJECT (dbin, "Couldn't expose group");
5001 EXPOSE_UNLOCK (dbin);
5007 static GstPadProbeReturn
5008 source_pad_event_probe (GstPad * pad, GstPadProbeInfo * info,
5011 GstEvent *event = GST_PAD_PROBE_INFO_EVENT (info);
5012 GstDecodePad *dpad = user_data;
5013 gboolean res = TRUE;
5015 GST_LOG_OBJECT (pad, "%s dpad:%p", GST_EVENT_TYPE_NAME (event), dpad);
5017 if (GST_EVENT_TYPE (event) == GST_EVENT_EOS) {
5018 GST_DEBUG_OBJECT (pad, "we received EOS");
5020 /* Check if all pads are drained.
5021 * * If there is no next group, we will let the EOS go through.
5022 * * If there is a next group but the current group isn't completely
5023 * drained, we will drop the EOS event.
5024 * * If there is a next group to expose and this was the last non-drained
5025 * pad for that group, we will remove the ghostpad of the current group
5026 * first, which unlinks the peer and so drops the EOS. */
5027 res = gst_decode_pad_handle_eos (dpad);
5030 return GST_PAD_PROBE_OK;
5032 return GST_PAD_PROBE_DROP;
5036 gst_decode_pad_set_blocked (GstDecodePad * dpad, gboolean blocked)
5038 GstDecodeBin *dbin = dpad->dbin;
5043 GST_DEBUG_OBJECT (dpad, "blocking pad: %d", blocked);
5045 opad = gst_ghost_pad_get_target (GST_GHOST_PAD_CAST (dpad));
5049 /* do not block if shutting down.
5050 * we do not consider/expect it blocked further below, but use other trick */
5051 if (!blocked || !dbin->shutdown) {
5053 if (dpad->block_id == 0)
5055 gst_pad_add_probe (opad,
5056 GST_PAD_PROBE_TYPE_BLOCK_DOWNSTREAM |
5057 GST_PAD_PROBE_TYPE_QUERY_DOWNSTREAM, source_pad_blocked_cb,
5058 gst_object_ref (dpad), (GDestroyNotify) gst_object_unref);
5060 if (dpad->block_id != 0) {
5061 gst_pad_remove_probe (opad, dpad->block_id);
5064 dpad->blocked = FALSE;
5069 if (dbin->shutdown) {
5070 /* deactivate to force flushing state to prevent NOT_LINKED errors */
5071 gst_pad_set_active (GST_PAD_CAST (dpad), FALSE);
5072 /* note that deactivating the target pad would have no effect here,
5073 * since elements are typically connected first (and pads exposed),
5074 * and only then brought to PAUSED state (so pads activated) */
5076 gst_object_ref (dpad);
5077 dbin->blocked_pads = g_list_prepend (dbin->blocked_pads, dpad);
5082 if ((l = g_list_find (dbin->blocked_pads, dpad))) {
5083 gst_object_unref (dpad);
5084 dbin->blocked_pads = g_list_delete_link (dbin->blocked_pads, l);
5087 gst_object_unref (opad);
5093 gst_decode_pad_add_drained_check (GstDecodePad * dpad)
5095 gst_pad_add_probe (GST_PAD_CAST (dpad), GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM,
5096 source_pad_event_probe, dpad, NULL);
5100 gst_decode_pad_activate (GstDecodePad * dpad, GstDecodeChain * chain)
5102 g_return_if_fail (chain != NULL);
5104 dpad->chain = chain;
5105 gst_pad_set_active (GST_PAD_CAST (dpad), TRUE);
5106 gst_decode_pad_set_blocked (dpad, TRUE);
5107 gst_decode_pad_add_drained_check (dpad);
5111 gst_decode_pad_unblock (GstDecodePad * dpad)
5113 gst_decode_pad_set_blocked (dpad, FALSE);
5117 gst_decode_pad_event (GstPad * pad, GstObject * parent, GstEvent * event)
5119 GstDecodeBin *dbin = GST_DECODE_BIN (parent);
5121 if (GST_EVENT_TYPE (event) == GST_EVENT_SEEK && dbin && dbin->decode_chain) {
5122 GstElement *demuxer = NULL;
5124 /* For adaptive demuxers we send the seek event directly to the demuxer.
5125 * See https://bugzilla.gnome.org/show_bug.cgi?id=606382
5127 CHAIN_MUTEX_LOCK (dbin->decode_chain);
5128 if (dbin->decode_chain->adaptive_demuxer) {
5129 GstDecodeElement *delem = dbin->decode_chain->elements->data;
5130 demuxer = gst_object_ref (delem->element);
5132 CHAIN_MUTEX_UNLOCK (dbin->decode_chain);
5137 GST_DEBUG_OBJECT (dbin,
5138 "Sending SEEK event directly to adaptive streaming demuxer %s",
5139 GST_OBJECT_NAME (demuxer));
5140 ret = gst_element_send_event (demuxer, event);
5141 gst_object_unref (demuxer);
5146 return gst_pad_event_default (pad, parent, event);
5150 gst_decode_pad_query (GstPad * pad, GstObject * parent, GstQuery * query)
5152 GstDecodePad *dpad = GST_DECODE_PAD (parent);
5153 gboolean ret = FALSE;
5155 CHAIN_MUTEX_LOCK (dpad->chain);
5156 if (!dpad->exposed && !dpad->dbin->shutdown && !dpad->chain->deadend
5157 && dpad->chain->elements) {
5158 GstDecodeElement *delem = dpad->chain->elements->data;
5161 GST_DEBUG_OBJECT (dpad->dbin,
5162 "calling autoplug-query for %s (element %s): %" GST_PTR_FORMAT,
5163 GST_PAD_NAME (dpad), GST_ELEMENT_NAME (delem->element), query);
5164 g_signal_emit (G_OBJECT (dpad->dbin),
5165 gst_decode_bin_signals[SIGNAL_AUTOPLUG_QUERY], 0, dpad, delem->element,
5169 GST_DEBUG_OBJECT (dpad->dbin,
5170 "autoplug-query returned %d: %" GST_PTR_FORMAT, ret, query);
5172 GST_DEBUG_OBJECT (dpad->dbin, "autoplug-query returned %d", ret);
5174 CHAIN_MUTEX_UNLOCK (dpad->chain);
5176 /* If exposed or nothing handled the query use the default handler */
5178 ret = gst_pad_query_default (pad, parent, query);
5184 gst_decode_pad_is_exposable (GstDecodePad * endpad)
5186 if (endpad->blocked || endpad->exposed)
5189 return gst_pad_has_current_caps (GST_PAD_CAST (endpad));
5192 /*gst_decode_pad_new:
5194 * Creates a new GstDecodePad for the given pad.
5196 static GstDecodePad *
5197 gst_decode_pad_new (GstDecodeBin * dbin, GstDecodeChain * chain)
5201 GstPadTemplate *pad_tmpl;
5203 GST_DEBUG_OBJECT (dbin, "making new decodepad");
5204 pad_tmpl = gst_static_pad_template_get (&decoder_bin_src_template);
5206 g_object_new (GST_TYPE_DECODE_PAD, "direction", GST_PAD_SRC,
5207 "template", pad_tmpl, NULL);
5208 dpad->chain = chain;
5210 gst_object_unref (pad_tmpl);
5212 ppad = gst_proxy_pad_get_internal (GST_PROXY_PAD (dpad));
5213 gst_pad_set_query_function (GST_PAD_CAST (ppad), gst_decode_pad_query);
5214 gst_pad_set_event_function (GST_PAD_CAST (dpad), gst_decode_pad_event);
5215 gst_object_unref (ppad);
5221 gst_pending_pad_free (GstPendingPad * ppad)
5224 g_assert (ppad->pad);
5226 if (ppad->event_probe_id != 0)
5227 gst_pad_remove_probe (ppad->pad, ppad->event_probe_id);
5228 if (ppad->notify_caps_id)
5229 g_signal_handler_disconnect (ppad->pad, ppad->notify_caps_id);
5230 gst_object_unref (ppad->pad);
5231 g_slice_free (GstPendingPad, ppad);
5235 * Element add/remove
5239 do_async_start (GstDecodeBin * dbin)
5241 GstMessage *message;
5243 dbin->async_pending = TRUE;
5245 message = gst_message_new_async_start (GST_OBJECT_CAST (dbin));
5246 parent_class->handle_message (GST_BIN_CAST (dbin), message);
5250 do_async_done (GstDecodeBin * dbin)
5252 GstMessage *message;
5254 if (dbin->async_pending) {
5256 gst_message_new_async_done (GST_OBJECT_CAST (dbin),
5257 GST_CLOCK_TIME_NONE);
5258 parent_class->handle_message (GST_BIN_CAST (dbin), message);
5260 dbin->async_pending = FALSE;
5265 * convenience functions
5270 * Returns the first sink pad of the given element, or NULL if it doesn't have
5275 find_sink_pad (GstElement * element)
5279 GValue item = { 0, };
5281 it = gst_element_iterate_sink_pads (element);
5283 if ((gst_iterator_next (it, &item)) == GST_ITERATOR_OK)
5284 pad = g_value_dup_object (&item);
5285 g_value_unset (&item);
5286 gst_iterator_free (it);
5291 /* call with dyn_lock held */
5293 unblock_pads (GstDecodeBin * dbin)
5295 GST_LOG_OBJECT (dbin, "unblocking pads");
5297 while (dbin->blocked_pads) {
5298 GList *tmp = dbin->blocked_pads;
5299 GstDecodePad *dpad = (GstDecodePad *) tmp->data;
5302 dbin->blocked_pads = g_list_delete_link (dbin->blocked_pads, tmp);
5303 opad = gst_ghost_pad_get_target (GST_GHOST_PAD_CAST (dpad));
5306 GST_DEBUG_OBJECT (dpad, "unblocking");
5307 if (dpad->block_id != 0) {
5308 gst_pad_remove_probe (opad, dpad->block_id);
5311 gst_object_unref (opad);
5314 dpad->blocked = FALSE;
5316 /* We release the dyn lock since we want to allow the streaming threads
5317 * to properly stop and not be blocked in our various probes */
5319 /* make flushing, prevent NOT_LINKED */
5320 gst_pad_set_active (GST_PAD_CAST (dpad), FALSE);
5323 GST_DEBUG_OBJECT (dpad, "unblocked");
5324 gst_object_unref (dpad);
5329 gst_decode_chain_stop (GstDecodeBin * dbin, GstDecodeChain * chain,
5332 GQueue *internal_elements, internal_elements_ = G_QUEUE_INIT;
5335 CHAIN_MUTEX_LOCK (chain);
5337 internal_elements = elements;
5339 internal_elements = &internal_elements_;
5342 for (l = chain->next_groups; l; l = l->next) {
5343 GstDecodeGroup *group = l->data;
5346 for (m = group->children; m; m = m->next) {
5347 GstDecodeChain *chain2 = m->data;
5348 gst_decode_chain_stop (dbin, chain2, internal_elements);
5350 if (group->multiqueue)
5351 g_queue_push_head (internal_elements, gst_object_ref (group->multiqueue));
5354 if (chain->active_group) {
5355 for (l = chain->active_group->children; l; l = l->next) {
5356 GstDecodeChain *chain2 = l->data;
5357 gst_decode_chain_stop (dbin, chain2, internal_elements);
5359 if (chain->active_group->multiqueue)
5360 g_queue_push_head (internal_elements,
5361 gst_object_ref (chain->active_group->multiqueue));
5364 for (l = chain->old_groups; l; l = l->next) {
5365 GstDecodeGroup *group = l->data;
5368 for (m = group->children; m; m = m->next) {
5369 GstDecodeChain *chain2 = m->data;
5370 gst_decode_chain_stop (dbin, chain2, internal_elements);
5372 if (group->multiqueue)
5373 g_queue_push_head (internal_elements, gst_object_ref (group->multiqueue));
5376 for (l = chain->elements; l; l = l->next) {
5377 GstDecodeElement *delem = l->data;
5379 if (delem->capsfilter)
5380 g_queue_push_head (internal_elements, gst_object_ref (delem->capsfilter));
5381 g_queue_push_head (internal_elements, gst_object_ref (delem->element));
5384 CHAIN_MUTEX_UNLOCK (chain);
5387 GstElement *element;
5389 EXPOSE_UNLOCK (dbin);
5390 /* Shut down from bottom to top */
5391 while ((element = g_queue_pop_tail (internal_elements))) {
5392 /* The bin must never ever change the state of this element anymore */
5393 gst_element_set_locked_state (element, TRUE);
5394 gst_element_set_state (element, GST_STATE_NULL);
5395 gst_object_unref (element);
5397 g_queue_clear (internal_elements);
5402 static GstStateChangeReturn
5403 gst_decode_bin_change_state (GstElement * element, GstStateChange transition)
5405 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
5406 GstDecodeBin *dbin = GST_DECODE_BIN (element);
5407 GstDecodeChain *chain_to_free = NULL;
5409 switch (transition) {
5410 case GST_STATE_CHANGE_NULL_TO_READY:
5411 if (dbin->typefind == NULL)
5412 goto missing_typefind;
5414 case GST_STATE_CHANGE_READY_TO_PAUSED:
5415 /* Make sure we've cleared all existing chains */
5417 if (dbin->decode_chain) {
5418 gst_decode_chain_free (dbin->decode_chain);
5419 dbin->decode_chain = NULL;
5421 EXPOSE_UNLOCK (dbin);
5423 GST_LOG_OBJECT (dbin, "clearing shutdown flag");
5424 dbin->shutdown = FALSE;
5426 dbin->have_type = FALSE;
5427 ret = GST_STATE_CHANGE_ASYNC;
5428 do_async_start (dbin);
5431 /* connect a signal to find out when the typefind element found
5433 dbin->have_type_id =
5434 g_signal_connect (dbin->typefind, "have-type",
5435 G_CALLBACK (type_found), dbin);
5437 case GST_STATE_CHANGE_PAUSED_TO_READY:
5438 case GST_STATE_CHANGE_READY_TO_NULL:
5439 if (dbin->have_type_id)
5440 g_signal_handler_disconnect (dbin->typefind, dbin->have_type_id);
5441 dbin->have_type_id = 0;
5443 GST_LOG_OBJECT (dbin, "setting shutdown flag");
5444 dbin->shutdown = TRUE;
5445 unblock_pads (dbin);
5448 /* Make sure we don't have cleanup races where
5449 * we might be trying to deactivate pads (in the cleanup thread)
5450 * at the same time as the default element deactivation
5451 * (in PAUSED=>READY) */
5452 g_mutex_lock (&dbin->cleanup_lock);
5453 if (dbin->cleanup_thread) {
5454 g_thread_join (dbin->cleanup_thread);
5455 dbin->cleanup_thread = NULL;
5457 g_mutex_unlock (&dbin->cleanup_lock);
5463 GstStateChangeReturn bret;
5465 bret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
5466 if (G_UNLIKELY (bret == GST_STATE_CHANGE_FAILURE))
5467 goto activate_failed;
5468 else if (G_UNLIKELY (bret == GST_STATE_CHANGE_NO_PREROLL)) {
5469 do_async_done (dbin);
5473 switch (transition) {
5474 case GST_STATE_CHANGE_PAUSED_TO_READY:
5475 do_async_done (dbin);
5477 if (dbin->decode_chain) {
5478 gst_decode_chain_stop (dbin, dbin->decode_chain, NULL);
5479 chain_to_free = dbin->decode_chain;
5480 gst_decode_chain_free_internal (dbin->decode_chain, TRUE);
5481 dbin->decode_chain = NULL;
5483 EXPOSE_UNLOCK (dbin);
5485 gst_decode_chain_free (chain_to_free);
5486 g_list_free_full (dbin->buffering_status,
5487 (GDestroyNotify) gst_message_unref);
5488 dbin->buffering_status = NULL;
5489 /* Let's do a final check of leftover groups to free */
5490 g_mutex_lock (&dbin->cleanup_lock);
5491 if (dbin->cleanup_groups) {
5492 gst_decode_chain_free_hidden_groups (dbin->cleanup_groups);
5493 dbin->cleanup_groups = NULL;
5495 g_mutex_unlock (&dbin->cleanup_lock);
5497 case GST_STATE_CHANGE_READY_TO_NULL:
5498 /* Let's do a final check of leftover groups to free */
5499 g_mutex_lock (&dbin->cleanup_lock);
5500 if (dbin->cleanup_groups) {
5501 gst_decode_chain_free_hidden_groups (dbin->cleanup_groups);
5502 dbin->cleanup_groups = NULL;
5504 g_mutex_unlock (&dbin->cleanup_lock);
5515 gst_element_post_message (element,
5516 gst_missing_element_message_new (element, "typefind"));
5517 GST_ELEMENT_ERROR (dbin, CORE, MISSING_PLUGIN, (NULL), ("no typefind!"));
5518 return GST_STATE_CHANGE_FAILURE;
5522 GST_DEBUG_OBJECT (element,
5523 "element failed to change states -- activation problem?");
5524 do_async_done (dbin);
5525 return GST_STATE_CHANGE_FAILURE;
5530 gst_decode_bin_handle_message (GstBin * bin, GstMessage * msg)
5532 GstDecodeBin *dbin = GST_DECODE_BIN (bin);
5533 gboolean drop = FALSE;
5535 if (GST_MESSAGE_TYPE (msg) == GST_MESSAGE_ERROR) {
5536 /* Don't pass errors when shutting down. Sometimes,
5537 * elements can generate spurious errors because we set the
5538 * output pads to flushing, and they can't detect that if they
5539 * send an event at exactly the wrong moment */
5541 drop = dbin->shutdown;
5545 GST_OBJECT_LOCK (dbin);
5546 drop = (g_list_find (dbin->filtered, GST_MESSAGE_SRC (msg)) != NULL);
5548 dbin->filtered_errors =
5549 g_list_prepend (dbin->filtered_errors, gst_message_ref (msg));
5550 GST_OBJECT_UNLOCK (dbin);
5552 } else if (GST_MESSAGE_TYPE (msg) == GST_MESSAGE_BUFFERING) {
5553 gint perc, msg_perc;
5554 gint smaller_perc = 100;
5555 GstMessage *smaller = NULL;
5556 GList *found = NULL;
5559 /* buffering messages must be aggregated as there might be multiple
5560 * multiqueue in the pipeline and their independent buffering messages
5561 * will confuse the application
5563 * decodebin keeps a list of messages received from elements that are
5566 * 1) Always post the smaller buffering %
5567 * 2) If an element posts a 100% buffering message, remove it from the list
5568 * 3) When there are no more messages on the list, post 100% message
5569 * 4) When an element posts a new buffering message, update the one
5570 * on the list to this new value
5573 BUFFERING_LOCK (dbin);
5574 gst_message_parse_buffering (msg, &msg_perc);
5576 GST_DEBUG_OBJECT (dbin, "Got buffering msg %" GST_PTR_FORMAT, msg);
5578 g_mutex_lock (&dbin->buffering_post_lock);
5581 * Single loop for 2 things:
5582 * 1) Look for a message with the same source
5583 * 1.1) If the received message is 100%, remove it from the list
5584 * 2) Find the minimum buffering from the list
5586 for (iter = dbin->buffering_status; iter;) {
5587 GstMessage *bufstats = iter->data;
5588 if (GST_MESSAGE_SRC (bufstats) == GST_MESSAGE_SRC (msg)) {
5590 if (msg_perc < 100) {
5591 GST_DEBUG_OBJECT (dbin, "Replacing old buffering msg %"
5592 GST_PTR_FORMAT, iter->data);
5593 gst_message_unref (iter->data);
5594 bufstats = iter->data = gst_message_ref (msg);
5596 GList *current = iter;
5598 /* remove the element here and avoid confusing the loop */
5599 iter = g_list_next (iter);
5601 GST_DEBUG_OBJECT (dbin, "Deleting old buffering msg %"
5602 GST_PTR_FORMAT, current->data);
5604 gst_message_unref (current->data);
5605 dbin->buffering_status =
5606 g_list_delete_link (dbin->buffering_status, current);
5612 gst_message_parse_buffering (bufstats, &perc);
5613 if (perc < smaller_perc) {
5614 smaller_perc = perc;
5617 iter = g_list_next (iter);
5620 if (found == NULL && msg_perc < 100) {
5621 if (msg_perc < smaller_perc) {
5622 smaller_perc = msg_perc;
5625 GST_DEBUG_OBJECT (dbin, "Storing buffering msg %" GST_PTR_FORMAT, msg);
5626 dbin->buffering_status =
5627 g_list_prepend (dbin->buffering_status, gst_message_ref (msg));
5630 /* now compute the buffering message that should be posted */
5631 if (smaller_perc == 100) {
5632 g_assert (dbin->buffering_status == NULL);
5633 /* we are posting the original received msg */
5635 gst_message_replace (&msg, smaller);
5637 BUFFERING_UNLOCK (dbin);
5639 GST_DEBUG_OBJECT (dbin, "Forwarding buffering msg %" GST_PTR_FORMAT, msg);
5640 GST_BIN_CLASS (parent_class)->handle_message (bin, msg);
5642 g_mutex_unlock (&dbin->buffering_post_lock);
5647 gst_message_unref (msg);
5649 GST_DEBUG_OBJECT (dbin, "Forwarding msg %" GST_PTR_FORMAT, msg);
5650 GST_BIN_CLASS (parent_class)->handle_message (bin, msg);
5655 gst_decode_bin_remove_element (GstBin * bin, GstElement * element)
5657 GstDecodeBin *dbin = GST_DECODE_BIN (bin);
5658 gboolean removed = FALSE, post = FALSE;
5661 BUFFERING_LOCK (bin);
5662 g_mutex_lock (&dbin->buffering_post_lock);
5663 for (iter = dbin->buffering_status; iter; iter = iter->next) {
5664 GstMessage *bufstats = iter->data;
5666 if (GST_MESSAGE_SRC (bufstats) == GST_OBJECT_CAST (element) ||
5667 gst_object_has_as_ancestor (GST_MESSAGE_SRC (bufstats),
5668 GST_OBJECT_CAST (element))) {
5669 gst_message_unref (bufstats);
5670 dbin->buffering_status =
5671 g_list_delete_link (dbin->buffering_status, iter);
5677 if (removed && dbin->buffering_status == NULL)
5679 BUFFERING_UNLOCK (bin);
5682 gst_element_post_message (GST_ELEMENT_CAST (bin),
5683 gst_message_new_buffering (GST_OBJECT_CAST (dbin), 100));
5685 g_mutex_unlock (&dbin->buffering_post_lock);
5687 return GST_BIN_CLASS (parent_class)->remove_element (bin, element);