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 "gstplayback.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 */
154 guint max_size_bytes;
155 guint max_size_buffers;
156 guint64 max_size_time;
157 gboolean post_stream_topology;
158 guint64 connection_speed;
160 GstElement *typefind; /* this holds the typefind object */
162 GMutex expose_lock; /* Protects exposal and removal of groups */
163 GstDecodeChain *decode_chain; /* Top level decode chain */
164 guint nbpads; /* unique identifier for source pads */
166 GMutex factories_lock;
167 guint32 factories_cookie; /* Cookie from last time when factories was updated */
168 GList *factories; /* factories we can use for selecting elements */
170 GMutex subtitle_lock; /* Protects changes to subtitles and encoding */
171 GList *subtitles; /* List of elements with subtitle-encoding,
172 * protected by above mutex! */
174 gboolean have_type; /* if we received the have_type signal */
175 guint have_type_id; /* signal id for have-type from typefind */
177 gboolean async_pending; /* async-start has been emitted */
179 GMutex dyn_lock; /* lock protecting pad blocking */
180 gboolean shutdown; /* if we are shutting down */
181 GList *blocked_pads; /* pads that have set to block */
183 gboolean expose_allstreams; /* Whether to expose unknow type streams or not */
185 GList *filtered; /* elements for which error messages are filtered */
186 GList *filtered_errors; /* filtered error messages */
188 GList *buffering_status; /* element currently buffering messages */
189 GMutex buffering_lock;
190 GMutex buffering_post_lock;
192 GMutex cleanup_lock; /* Mutex used to protect the cleanup thread */
193 GThread *cleanup_thread; /* thread used to free chains asynchronously.
194 * We store it to make sure we end up joining it
195 * before stopping the element.
196 * Protected by the object lock */
197 GList *cleanup_groups; /* List of groups to free */
200 struct _GstDecodeBinClass
202 GstBinClass parent_class;
204 /* signal fired when we found a pad that we cannot decode */
205 void (*unknown_type) (GstElement * element, GstPad * pad, GstCaps * caps);
207 /* signal fired to know if we continue trying to decode the given caps */
208 gboolean (*autoplug_continue) (GstElement * element, GstPad * pad,
210 /* signal fired to get a list of factories to try to autoplug */
211 GValueArray *(*autoplug_factories) (GstElement * element, GstPad * pad,
213 /* signal fired to sort the factories */
214 GValueArray *(*autoplug_sort) (GstElement * element, GstPad * pad,
215 GstCaps * caps, GValueArray * factories);
216 /* signal fired to select from the proposed list of factories */
217 GstAutoplugSelectResult (*autoplug_select) (GstElement * element,
218 GstPad * pad, GstCaps * caps, GstElementFactory * factory);
219 /* signal fired when a autoplugged element that is not linked downstream
220 * or exposed wants to query something */
221 gboolean (*autoplug_query) (GstElement * element, GstPad * pad,
224 /* fired when the last group is drained */
225 void (*drained) (GstElement * element);
232 SIGNAL_AUTOPLUG_CONTINUE,
233 SIGNAL_AUTOPLUG_FACTORIES,
234 SIGNAL_AUTOPLUG_SELECT,
235 SIGNAL_AUTOPLUG_SORT,
236 SIGNAL_AUTOPLUG_QUERY,
241 /* automatic sizes, while prerolling we buffer up to 2MB, we ignore time
242 * and buffers in this case. */
243 #define AUTO_PREROLL_SIZE_BYTES 2 * 1024 * 1024
244 #define AUTO_PREROLL_SIZE_BUFFERS 0
245 #define AUTO_PREROLL_NOT_SEEKABLE_SIZE_TIME 10 * GST_SECOND
246 #define AUTO_PREROLL_SEEKABLE_SIZE_TIME 0
248 /* when playing, keep a max of 2MB of data but try to keep the number of buffers
249 * as low as possible (try to aim for 5 buffers) */
250 #define AUTO_PLAY_SIZE_BYTES 2 * 1024 * 1024
251 #define AUTO_PLAY_SIZE_BUFFERS 5
252 #define AUTO_PLAY_SIZE_TIME 0
254 #define DEFAULT_SUBTITLE_ENCODING NULL
255 #define DEFAULT_USE_BUFFERING FALSE
256 #define DEFAULT_LOW_PERCENT 10
257 #define DEFAULT_HIGH_PERCENT 99
258 /* by default we use the automatic values above */
259 #define DEFAULT_MAX_SIZE_BYTES 0
260 #define DEFAULT_MAX_SIZE_BUFFERS 0
261 #define DEFAULT_MAX_SIZE_TIME 0
262 #define DEFAULT_POST_STREAM_TOPOLOGY FALSE
263 #define DEFAULT_EXPOSE_ALL_STREAMS TRUE
264 #define DEFAULT_CONNECTION_SPEED 0
271 PROP_SUBTITLE_ENCODING,
277 PROP_MAX_SIZE_BUFFERS,
279 PROP_POST_STREAM_TOPOLOGY,
280 PROP_EXPOSE_ALL_STREAMS,
281 PROP_CONNECTION_SPEED
284 static GstBinClass *parent_class;
285 static guint gst_decode_bin_signals[LAST_SIGNAL] = { 0 };
287 static GstStaticCaps default_raw_caps = GST_STATIC_CAPS (DEFAULT_RAW_CAPS);
289 static void do_async_start (GstDecodeBin * dbin);
290 static void do_async_done (GstDecodeBin * dbin);
292 static void type_found (GstElement * typefind, guint probability,
293 GstCaps * caps, GstDecodeBin * decode_bin);
295 static void decodebin_set_queue_size (GstDecodeBin * dbin,
296 GstElement * multiqueue, gboolean preroll, gboolean seekable);
297 static void decodebin_set_queue_size_full (GstDecodeBin * dbin,
298 GstElement * multiqueue, gboolean use_buffering, gboolean preroll,
301 static gboolean gst_decode_bin_autoplug_continue (GstElement * element,
302 GstPad * pad, GstCaps * caps);
303 static GValueArray *gst_decode_bin_autoplug_factories (GstElement *
304 element, GstPad * pad, GstCaps * caps);
305 static GValueArray *gst_decode_bin_autoplug_sort (GstElement * element,
306 GstPad * pad, GstCaps * caps, GValueArray * factories);
307 static GstAutoplugSelectResult gst_decode_bin_autoplug_select (GstElement *
308 element, GstPad * pad, GstCaps * caps, GstElementFactory * factory);
309 static gboolean gst_decode_bin_autoplug_query (GstElement * element,
310 GstPad * pad, GstQuery * query);
312 static void gst_decode_bin_set_property (GObject * object, guint prop_id,
313 const GValue * value, GParamSpec * pspec);
314 static void gst_decode_bin_get_property (GObject * object, guint prop_id,
315 GValue * value, GParamSpec * pspec);
316 static void gst_decode_bin_set_caps (GstDecodeBin * dbin, GstCaps * caps);
317 static GstCaps *gst_decode_bin_get_caps (GstDecodeBin * dbin);
318 static void caps_notify_cb (GstPad * pad, GParamSpec * unused,
319 GstDecodeChain * chain);
321 static void flush_chain (GstDecodeChain * chain, gboolean flushing);
322 static void flush_group (GstDecodeGroup * group, gboolean flushing);
323 static GstPad *find_sink_pad (GstElement * element);
324 static GstStateChangeReturn gst_decode_bin_change_state (GstElement * element,
325 GstStateChange transition);
326 static void gst_decode_bin_handle_message (GstBin * bin, GstMessage * message);
327 static gboolean gst_decode_bin_remove_element (GstBin * bin,
328 GstElement * element);
330 static gboolean check_upstream_seekable (GstDecodeBin * dbin, GstPad * pad);
332 static GstCaps *get_pad_caps (GstPad * pad);
333 static void unblock_pads (GstDecodeBin * dbin);
335 #define EXPOSE_LOCK(dbin) G_STMT_START { \
336 GST_LOG_OBJECT (dbin, \
337 "expose locking from thread %p", \
339 g_mutex_lock (&GST_DECODE_BIN_CAST(dbin)->expose_lock); \
340 GST_LOG_OBJECT (dbin, \
341 "expose locked from thread %p", \
345 #define EXPOSE_UNLOCK(dbin) G_STMT_START { \
346 GST_LOG_OBJECT (dbin, \
347 "expose unlocking from thread %p", \
349 g_mutex_unlock (&GST_DECODE_BIN_CAST(dbin)->expose_lock); \
352 #define DYN_LOCK(dbin) G_STMT_START { \
353 GST_LOG_OBJECT (dbin, \
354 "dynlocking from thread %p", \
356 g_mutex_lock (&GST_DECODE_BIN_CAST(dbin)->dyn_lock); \
357 GST_LOG_OBJECT (dbin, \
358 "dynlocked from thread %p", \
362 #define DYN_UNLOCK(dbin) G_STMT_START { \
363 GST_LOG_OBJECT (dbin, \
364 "dynunlocking from thread %p", \
366 g_mutex_unlock (&GST_DECODE_BIN_CAST(dbin)->dyn_lock); \
369 #define SUBTITLE_LOCK(dbin) G_STMT_START { \
370 GST_LOG_OBJECT (dbin, \
371 "subtitle locking from thread %p", \
373 g_mutex_lock (&GST_DECODE_BIN_CAST(dbin)->subtitle_lock); \
374 GST_LOG_OBJECT (dbin, \
375 "subtitle lock from thread %p", \
379 #define SUBTITLE_UNLOCK(dbin) G_STMT_START { \
380 GST_LOG_OBJECT (dbin, \
381 "subtitle unlocking from thread %p", \
383 g_mutex_unlock (&GST_DECODE_BIN_CAST(dbin)->subtitle_lock); \
386 #define BUFFERING_LOCK(dbin) G_STMT_START { \
387 GST_LOG_OBJECT (dbin, \
388 "buffering locking from thread %p", \
390 g_mutex_lock (&GST_DECODE_BIN_CAST(dbin)->buffering_lock); \
391 GST_LOG_OBJECT (dbin, \
392 "buffering lock from thread %p", \
396 #define BUFFERING_UNLOCK(dbin) G_STMT_START { \
397 GST_LOG_OBJECT (dbin, \
398 "buffering unlocking from thread %p", \
400 g_mutex_unlock (&GST_DECODE_BIN_CAST(dbin)->buffering_lock); \
403 struct _GstPendingPad
406 GstDecodeChain *chain;
407 gulong event_probe_id;
408 gulong notify_caps_id;
411 struct _GstDecodeElement
414 GstElement *capsfilter; /* Optional capsfilter for Parser/Convert */
416 gulong pad_removed_id;
417 gulong no_more_pads_id;
420 struct _GstDemuxerPad
423 gulong event_probe_id;
424 gulong query_probe_id;
430 * Streams belonging to the same group/chain of a media file
432 * When changing something here lock the parent chain!
434 struct _GstDecodeGroup
437 GstDecodeChain *parent;
439 GstElement *multiqueue; /* Used for linking all child chains */
440 gulong overrunsig; /* the overrun signal for multiqueue */
442 gboolean overrun; /* TRUE if the multiqueue signaled overrun. This
443 * means that we should really expose the group */
445 gboolean no_more_pads; /* TRUE if the demuxer signaled no-more-pads */
446 gboolean drained; /* TRUE if the all children are drained */
448 GList *children; /* List of GstDecodeChains in this group */
449 GList *demuxer_pad_probe_ids;
451 GList *reqpads; /* List of RequestPads for multiqueue, there is
452 * exactly one RequestPad per child chain */
455 struct _GstDecodeChain
457 GstDecodeGroup *parent;
460 gint refs; /* Number of references to this object */
462 GMutex lock; /* Protects this chain and its groups */
464 GstPad *pad; /* srcpad that caused creation of this chain */
465 gulong pad_probe_id; /* id for the demuxer_source_pad_probe probe */
467 gboolean drained; /* TRUE if the all children are drained */
468 gboolean demuxer; /* TRUE if elements->data is a demuxer */
469 gboolean adaptive_demuxer; /* TRUE if elements->data is an adaptive streaming demuxer */
470 gboolean seekable; /* TRUE if this chain ends on a demuxer and is seekable */
471 GList *elements; /* All elements in this group, first
472 is the latest and most downstream element */
474 /* Note: there are only groups if the last element of this chain
475 * is a demuxer, otherwise the chain will end with an endpad.
476 * The other way around this means, that endpad only exists if this
477 * chain doesn't end with a demuxer! */
479 GstDecodeGroup *active_group; /* Currently active group */
480 GList *next_groups; /* head is newest group, tail is next group.
481 a new group will be created only if the head
482 group had no-more-pads. If it's only exposed
483 all new pads will be ignored! */
484 GList *pending_pads; /* Pads that have no fixed caps yet */
486 GstDecodePad *current_pad; /* Current ending pad of the chain that can't
487 * be exposed yet but would be the same as endpad
488 * once it can be exposed */
489 GstDecodePad *endpad; /* Pad of this chain that could be exposed */
490 gboolean deadend; /* This chain is incomplete and can't be completed,
491 e.g. no suitable decoder could be found
492 e.g. stream got EOS without buffers
494 gchar *deadend_details;
495 GstCaps *endcaps; /* Caps that were used when linking to the endpad
496 or that resulted in the deadend
499 /* FIXME: This should be done directly via a thread! */
500 GList *old_groups; /* Groups that should be freed later */
503 static GstDecodeChain *gst_decode_chain_ref (GstDecodeChain * chain);
504 static void gst_decode_chain_unref (GstDecodeChain * chain);
505 static void gst_decode_chain_free (GstDecodeChain * chain);
506 static GstDecodeChain *gst_decode_chain_new (GstDecodeBin * dbin,
507 GstDecodeGroup * group, GstPad * pad);
508 static void gst_decode_group_hide (GstDecodeGroup * group);
509 static void gst_decode_group_free (GstDecodeGroup * group);
510 static GstDecodeGroup *gst_decode_group_new (GstDecodeBin * dbin,
511 GstDecodeChain * chain);
512 static gboolean gst_decode_chain_is_complete (GstDecodeChain * chain);
513 static gboolean gst_decode_chain_expose (GstDecodeChain * chain,
514 GList ** endpads, gboolean * missing_plugin,
515 GString * missing_plugin_details, gboolean * last_group);
516 static gboolean gst_decode_chain_is_drained (GstDecodeChain * chain);
517 static gboolean gst_decode_chain_reset_buffering (GstDecodeChain * chain);
518 static gboolean gst_decode_group_is_complete (GstDecodeGroup * group);
519 static GstPad *gst_decode_group_control_demuxer_pad (GstDecodeGroup * group,
521 static gboolean gst_decode_group_is_drained (GstDecodeGroup * group);
522 static gboolean gst_decode_group_reset_buffering (GstDecodeGroup * group);
524 static gboolean gst_decode_bin_expose (GstDecodeBin * dbin);
525 static void gst_decode_bin_reset_buffering (GstDecodeBin * dbin);
527 #define CHAIN_MUTEX_LOCK(chain) G_STMT_START { \
528 GST_LOG_OBJECT (chain->dbin, \
529 "locking chain %p from thread %p", \
530 chain, g_thread_self ()); \
531 g_mutex_lock (&chain->lock); \
532 GST_LOG_OBJECT (chain->dbin, \
533 "locked chain %p from thread %p", \
534 chain, g_thread_self ()); \
537 #define CHAIN_MUTEX_UNLOCK(chain) G_STMT_START { \
538 GST_LOG_OBJECT (chain->dbin, \
539 "unlocking chain %p from thread %p", \
540 chain, g_thread_self ()); \
541 g_mutex_unlock (&chain->lock); \
546 * GstPad private used for source pads of chains
552 GstDecodeChain *chain;
554 gboolean blocked; /* the *target* pad is blocked */
555 gboolean exposed; /* the pad is exposed */
556 gboolean drained; /* an EOS has been seen on the pad */
561 GType gst_decode_pad_get_type (void);
562 G_DEFINE_TYPE (GstDecodePad, gst_decode_pad, GST_TYPE_GHOST_PAD);
563 #define GST_TYPE_DECODE_PAD (gst_decode_pad_get_type ())
564 #define GST_DECODE_PAD(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_DECODE_PAD,GstDecodePad))
566 static GstDecodePad *gst_decode_pad_new (GstDecodeBin * dbin,
567 GstDecodeChain * chain);
568 static void gst_decode_pad_activate (GstDecodePad * dpad,
569 GstDecodeChain * chain);
570 static void gst_decode_pad_unblock (GstDecodePad * dpad);
571 static void gst_decode_pad_set_blocked (GstDecodePad * dpad, gboolean blocked);
572 static gboolean gst_decode_pad_query (GstPad * pad, GstObject * parent,
574 static gboolean gst_decode_pad_is_exposable (GstDecodePad * endpad);
576 static void gst_pending_pad_free (GstPendingPad * ppad);
577 static GstPadProbeReturn pad_event_cb (GstPad * pad, GstPadProbeInfo * info,
580 /********************************
581 * Standard GObject boilerplate *
582 ********************************/
584 static void gst_decode_bin_class_init (GstDecodeBinClass * klass);
585 static void gst_decode_bin_init (GstDecodeBin * decode_bin);
586 static void gst_decode_bin_dispose (GObject * object);
587 static void gst_decode_bin_finalize (GObject * object);
590 gst_decode_bin_get_type (void)
592 static GType gst_decode_bin_type = 0;
594 if (!gst_decode_bin_type) {
595 static const GTypeInfo gst_decode_bin_info = {
596 sizeof (GstDecodeBinClass),
599 (GClassInitFunc) gst_decode_bin_class_init,
602 sizeof (GstDecodeBin),
604 (GInstanceInitFunc) gst_decode_bin_init,
608 gst_decode_bin_type =
609 g_type_register_static (GST_TYPE_BIN, "GstDecodeBin",
610 &gst_decode_bin_info, 0);
613 return gst_decode_bin_type;
617 _gst_boolean_accumulator (GSignalInvocationHint * ihint,
618 GValue * return_accu, const GValue * handler_return, gpointer dummy)
622 myboolean = g_value_get_boolean (handler_return);
623 if (!(ihint->run_type & G_SIGNAL_RUN_CLEANUP))
624 g_value_set_boolean (return_accu, myboolean);
626 /* stop emission if FALSE */
631 _gst_boolean_or_accumulator (GSignalInvocationHint * ihint,
632 GValue * return_accu, const GValue * handler_return, gpointer dummy)
637 myboolean = g_value_get_boolean (handler_return);
638 retboolean = g_value_get_boolean (return_accu);
640 if (!(ihint->run_type & G_SIGNAL_RUN_CLEANUP))
641 g_value_set_boolean (return_accu, myboolean || retboolean);
646 /* we collect the first result */
648 _gst_array_accumulator (GSignalInvocationHint * ihint,
649 GValue * return_accu, const GValue * handler_return, gpointer dummy)
653 array = g_value_get_boxed (handler_return);
654 if (!(ihint->run_type & G_SIGNAL_RUN_CLEANUP))
655 g_value_set_boxed (return_accu, array);
661 _gst_select_accumulator (GSignalInvocationHint * ihint,
662 GValue * return_accu, const GValue * handler_return, gpointer dummy)
664 GstAutoplugSelectResult res;
666 res = g_value_get_enum (handler_return);
667 if (!(ihint->run_type & G_SIGNAL_RUN_CLEANUP))
668 g_value_set_enum (return_accu, res);
670 /* Call the next handler in the chain (if any) when the current callback
671 * returns TRY. This makes it possible to register separate autoplug-select
672 * handlers that implement different TRY/EXPOSE/SKIP strategies.
674 if (res == GST_AUTOPLUG_SELECT_TRY)
681 _gst_array_hasvalue_accumulator (GSignalInvocationHint * ihint,
682 GValue * return_accu, const GValue * handler_return, gpointer dummy)
686 array = g_value_get_boxed (handler_return);
687 if (!(ihint->run_type & G_SIGNAL_RUN_CLEANUP))
688 g_value_set_boxed (return_accu, array);
697 gst_decode_bin_class_init (GstDecodeBinClass * klass)
699 GObjectClass *gobject_klass;
700 GstElementClass *gstelement_klass;
701 GstBinClass *gstbin_klass;
703 gobject_klass = (GObjectClass *) klass;
704 gstelement_klass = (GstElementClass *) klass;
705 gstbin_klass = (GstBinClass *) klass;
707 parent_class = g_type_class_peek_parent (klass);
709 gobject_klass->dispose = gst_decode_bin_dispose;
710 gobject_klass->finalize = gst_decode_bin_finalize;
711 gobject_klass->set_property = gst_decode_bin_set_property;
712 gobject_klass->get_property = gst_decode_bin_get_property;
715 * GstDecodeBin::unknown-type:
716 * @bin: The decodebin.
717 * @pad: The new pad containing caps that cannot be resolved to a 'final'
719 * @caps: The #GstCaps of the pad that cannot be resolved.
721 * This signal is emitted when a pad for which there is no further possible
722 * decoding is added to the decodebin.
724 gst_decode_bin_signals[SIGNAL_UNKNOWN_TYPE] =
725 g_signal_new ("unknown-type", G_TYPE_FROM_CLASS (klass),
726 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstDecodeBinClass, unknown_type),
727 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 2,
728 GST_TYPE_PAD, GST_TYPE_CAPS);
731 * GstDecodeBin::autoplug-continue:
732 * @bin: The decodebin.
734 * @caps: The #GstCaps found.
736 * This signal is emitted whenever decodebin finds a new stream. It is
737 * emitted before looking for any elements that can handle that stream.
739 * > Invocation of signal handlers stops after the first signal handler
740 * > returns %FALSE. Signal handlers are invoked in the order they were
743 * Returns: %TRUE if you wish decodebin to look for elements that can
744 * handle the given @caps. If %FALSE, those caps will be considered as
745 * final and the pad will be exposed as such (see 'pad-added' signal of
748 gst_decode_bin_signals[SIGNAL_AUTOPLUG_CONTINUE] =
749 g_signal_new ("autoplug-continue", G_TYPE_FROM_CLASS (klass),
750 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstDecodeBinClass, autoplug_continue),
751 _gst_boolean_accumulator, NULL, g_cclosure_marshal_generic,
752 G_TYPE_BOOLEAN, 2, GST_TYPE_PAD, GST_TYPE_CAPS);
755 * GstDecodeBin::autoplug-factories:
756 * @bin: The decodebin.
758 * @caps: The #GstCaps found.
760 * This signal is emitted when an array of possible factories for @caps on
761 * @pad is needed. Decodebin will by default return an array with all
762 * compatible factories, sorted by rank.
764 * If this function returns NULL, @pad will be exposed as a final caps.
766 * If this function returns an empty array, the pad will be considered as
767 * having an unhandled type media type.
769 * > Only the signal handler that is connected first will ever by invoked.
770 * > Don't connect signal handlers with the #G_CONNECT_AFTER flag to this
771 * > signal, they will never be invoked!
773 * Returns: a #GValueArray* with a list of factories to try. The factories are
774 * by default tried in the returned order or based on the index returned by
777 gst_decode_bin_signals[SIGNAL_AUTOPLUG_FACTORIES] =
778 g_signal_new ("autoplug-factories", G_TYPE_FROM_CLASS (klass),
779 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstDecodeBinClass,
780 autoplug_factories), _gst_array_accumulator, NULL,
781 g_cclosure_marshal_generic, G_TYPE_VALUE_ARRAY, 2,
782 GST_TYPE_PAD, GST_TYPE_CAPS);
785 * GstDecodeBin::autoplug-sort:
786 * @bin: The decodebin.
788 * @caps: The #GstCaps.
789 * @factories: A #GValueArray of possible #GstElementFactory to use.
791 * Once decodebin has found the possible #GstElementFactory objects to try
792 * for @caps on @pad, this signal is emitted. The purpose of the signal is for
793 * the application to perform additional sorting or filtering on the element
796 * The callee should copy and modify @factories or return %NULL if the
797 * order should not change.
799 * > Invocation of signal handlers stops after one signal handler has
800 * > returned something else than %NULL. Signal handlers are invoked in
801 * > the order they were connected in.
802 * > Don't connect signal handlers with the #G_CONNECT_AFTER flag to this
803 * > signal, they will never be invoked!
805 * Returns: A new sorted array of #GstElementFactory objects.
807 gst_decode_bin_signals[SIGNAL_AUTOPLUG_SORT] =
808 g_signal_new ("autoplug-sort", G_TYPE_FROM_CLASS (klass),
809 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstDecodeBinClass, autoplug_sort),
810 _gst_array_hasvalue_accumulator, NULL,
811 g_cclosure_marshal_generic, G_TYPE_VALUE_ARRAY, 3, GST_TYPE_PAD,
812 GST_TYPE_CAPS, G_TYPE_VALUE_ARRAY | G_SIGNAL_TYPE_STATIC_SCOPE);
815 * GstDecodeBin::autoplug-select:
816 * @bin: The decodebin.
818 * @caps: The #GstCaps.
819 * @factory: A #GstElementFactory to use.
821 * This signal is emitted once decodebin has found all the possible
822 * #GstElementFactory that can be used to handle the given @caps. For each of
823 * those factories, this signal is emitted.
825 * The signal handler should return a #GST_TYPE_AUTOPLUG_SELECT_RESULT enum
826 * value indicating what decodebin should do next.
828 * A value of #GST_AUTOPLUG_SELECT_TRY will try to autoplug an element from
831 * A value of #GST_AUTOPLUG_SELECT_EXPOSE will expose @pad without plugging
834 * A value of #GST_AUTOPLUG_SELECT_SKIP will skip @factory and move to the
837 * > The signal handler will not be invoked if any of the previously
838 * > registered signal handlers (if any) return a value other than
839 * > GST_AUTOPLUG_SELECT_TRY. Which also means that if you return
840 * > GST_AUTOPLUG_SELECT_TRY from one signal handler, handlers that get
841 * > registered next (again, if any) can override that decision.
843 * Returns: a #GST_TYPE_AUTOPLUG_SELECT_RESULT that indicates the required
844 * operation. the default handler will always return
845 * #GST_AUTOPLUG_SELECT_TRY.
847 gst_decode_bin_signals[SIGNAL_AUTOPLUG_SELECT] =
848 g_signal_new ("autoplug-select", G_TYPE_FROM_CLASS (klass),
849 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstDecodeBinClass, autoplug_select),
850 _gst_select_accumulator, NULL,
851 g_cclosure_marshal_generic,
852 GST_TYPE_AUTOPLUG_SELECT_RESULT, 3, GST_TYPE_PAD, GST_TYPE_CAPS,
853 GST_TYPE_ELEMENT_FACTORY);
856 * GstDecodeBin::autoplug-query:
857 * @bin: The decodebin.
859 * @child: The child element doing the query
860 * @query: The #GstQuery.
862 * This signal is emitted whenever an autoplugged element that is
863 * not linked downstream yet and not exposed does a query. It can
864 * be used to tell the element about the downstream supported caps
867 * Returns: %TRUE if the query was handled, %FALSE otherwise.
869 gst_decode_bin_signals[SIGNAL_AUTOPLUG_QUERY] =
870 g_signal_new ("autoplug-query", G_TYPE_FROM_CLASS (klass),
871 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstDecodeBinClass, autoplug_query),
872 _gst_boolean_or_accumulator, NULL, g_cclosure_marshal_generic,
873 G_TYPE_BOOLEAN, 3, GST_TYPE_PAD, GST_TYPE_ELEMENT,
874 GST_TYPE_QUERY | G_SIGNAL_TYPE_STATIC_SCOPE);
877 * GstDecodeBin::drained
878 * @bin: The decodebin
880 * This signal is emitted once decodebin has finished decoding all the data.
882 gst_decode_bin_signals[SIGNAL_DRAINED] =
883 g_signal_new ("drained", G_TYPE_FROM_CLASS (klass),
884 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstDecodeBinClass, drained),
885 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 0, G_TYPE_NONE);
887 g_object_class_install_property (gobject_klass, PROP_CAPS,
888 g_param_spec_boxed ("caps", "Caps", "The caps on which to stop decoding.",
889 GST_TYPE_CAPS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
891 g_object_class_install_property (gobject_klass, PROP_SUBTITLE_ENCODING,
892 g_param_spec_string ("subtitle-encoding", "subtitle encoding",
893 "Encoding to assume if input subtitles are not in UTF-8 encoding. "
894 "If not set, the GST_SUBTITLE_ENCODING environment variable will "
895 "be checked for an encoding to use. If that is not set either, "
896 "ISO-8859-15 will be assumed.", NULL,
897 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
899 g_object_class_install_property (gobject_klass, PROP_SINK_CAPS,
900 g_param_spec_boxed ("sink-caps", "Sink Caps",
901 "The caps of the input data. (NULL = use typefind element)",
902 GST_TYPE_CAPS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
905 * GstDecodeBin::use-buffering
907 * Activate buffering in decodebin. This will instruct the multiqueues behind
908 * decoders to emit BUFFERING messages.
910 g_object_class_install_property (gobject_klass, PROP_USE_BUFFERING,
911 g_param_spec_boolean ("use-buffering", "Use Buffering",
912 "Emit GST_MESSAGE_BUFFERING based on low-/high-percent thresholds",
913 DEFAULT_USE_BUFFERING, 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);
1036 /* Must be called with factories lock! */
1038 gst_decode_bin_update_factories_list (GstDecodeBin * dbin)
1042 cookie = gst_registry_get_feature_list_cookie (gst_registry_get ());
1043 if (!dbin->factories || dbin->factories_cookie != cookie) {
1044 if (dbin->factories)
1045 gst_plugin_feature_list_free (dbin->factories);
1047 gst_element_factory_list_get_elements
1048 (GST_ELEMENT_FACTORY_TYPE_DECODABLE, GST_RANK_MARGINAL);
1050 g_list_sort (dbin->factories,
1051 gst_playback_utils_compare_factories_func);
1052 dbin->factories_cookie = cookie;
1057 gst_decode_bin_init (GstDecodeBin * decode_bin)
1059 /* first filter out the interesting element factories */
1060 g_mutex_init (&decode_bin->factories_lock);
1062 /* we create the typefind element only once */
1063 decode_bin->typefind = gst_element_factory_make ("typefind", "typefind");
1064 if (!decode_bin->typefind) {
1065 g_warning ("can't find typefind element, decodebin will not work");
1069 GstPadTemplate *pad_tmpl;
1071 /* add the typefind element */
1072 if (!gst_bin_add (GST_BIN (decode_bin), decode_bin->typefind)) {
1073 g_warning ("Could not add typefind element, decodebin will not work");
1074 gst_object_unref (decode_bin->typefind);
1075 decode_bin->typefind = NULL;
1078 /* get the sinkpad */
1079 pad = gst_element_get_static_pad (decode_bin->typefind, "sink");
1081 /* get the pad template */
1082 pad_tmpl = gst_static_pad_template_get (&decoder_bin_sink_template);
1084 /* ghost the sink pad to ourself */
1085 gpad = gst_ghost_pad_new_from_template ("sink", pad, pad_tmpl);
1086 gst_pad_set_active (gpad, TRUE);
1087 gst_element_add_pad (GST_ELEMENT (decode_bin), gpad);
1089 gst_object_unref (pad_tmpl);
1090 gst_object_unref (pad);
1093 g_mutex_init (&decode_bin->expose_lock);
1094 decode_bin->decode_chain = NULL;
1096 g_mutex_init (&decode_bin->dyn_lock);
1097 decode_bin->shutdown = FALSE;
1098 decode_bin->blocked_pads = NULL;
1100 g_mutex_init (&decode_bin->subtitle_lock);
1101 g_mutex_init (&decode_bin->buffering_lock);
1102 g_mutex_init (&decode_bin->buffering_post_lock);
1104 g_mutex_init (&decode_bin->cleanup_lock);
1105 decode_bin->cleanup_thread = NULL;
1107 decode_bin->encoding = g_strdup (DEFAULT_SUBTITLE_ENCODING);
1108 decode_bin->caps = gst_static_caps_get (&default_raw_caps);
1109 decode_bin->use_buffering = DEFAULT_USE_BUFFERING;
1110 decode_bin->low_percent = DEFAULT_LOW_PERCENT;
1111 decode_bin->high_percent = DEFAULT_HIGH_PERCENT;
1113 decode_bin->max_size_bytes = DEFAULT_MAX_SIZE_BYTES;
1114 decode_bin->max_size_buffers = DEFAULT_MAX_SIZE_BUFFERS;
1115 decode_bin->max_size_time = DEFAULT_MAX_SIZE_TIME;
1117 decode_bin->expose_allstreams = DEFAULT_EXPOSE_ALL_STREAMS;
1118 decode_bin->connection_speed = DEFAULT_CONNECTION_SPEED;
1122 gst_decode_bin_dispose (GObject * object)
1124 GstDecodeBin *decode_bin;
1126 decode_bin = GST_DECODE_BIN (object);
1128 if (decode_bin->factories)
1129 gst_plugin_feature_list_free (decode_bin->factories);
1130 decode_bin->factories = NULL;
1132 if (decode_bin->decode_chain)
1133 gst_decode_chain_free (decode_bin->decode_chain);
1134 decode_bin->decode_chain = NULL;
1136 if (decode_bin->caps)
1137 gst_caps_unref (decode_bin->caps);
1138 decode_bin->caps = NULL;
1140 g_free (decode_bin->encoding);
1141 decode_bin->encoding = NULL;
1143 g_list_free (decode_bin->subtitles);
1144 decode_bin->subtitles = NULL;
1146 unblock_pads (decode_bin);
1148 G_OBJECT_CLASS (parent_class)->dispose (object);
1152 gst_decode_bin_finalize (GObject * object)
1154 GstDecodeBin *decode_bin;
1156 decode_bin = GST_DECODE_BIN (object);
1158 g_mutex_clear (&decode_bin->expose_lock);
1159 g_mutex_clear (&decode_bin->dyn_lock);
1160 g_mutex_clear (&decode_bin->subtitle_lock);
1161 g_mutex_clear (&decode_bin->buffering_lock);
1162 g_mutex_clear (&decode_bin->buffering_post_lock);
1163 g_mutex_clear (&decode_bin->factories_lock);
1164 g_mutex_clear (&decode_bin->cleanup_lock);
1166 G_OBJECT_CLASS (parent_class)->finalize (object);
1170 * Changes the caps on which decodebin will stop decoding.
1171 * Will unref the previously set one. The refcount of the given caps will be
1173 * @caps can be NULL.
1178 gst_decode_bin_set_caps (GstDecodeBin * dbin, GstCaps * caps)
1180 GST_DEBUG_OBJECT (dbin, "Setting new caps: %" GST_PTR_FORMAT, caps);
1182 GST_OBJECT_LOCK (dbin);
1183 gst_caps_replace (&dbin->caps, caps);
1184 GST_OBJECT_UNLOCK (dbin);
1188 * Returns the currently configured caps on which decodebin will stop decoding.
1189 * The returned caps (if not NULL), will have its refcount incremented.
1194 gst_decode_bin_get_caps (GstDecodeBin * dbin)
1198 GST_DEBUG_OBJECT (dbin, "Getting currently set caps");
1200 GST_OBJECT_LOCK (dbin);
1203 gst_caps_ref (caps);
1204 GST_OBJECT_UNLOCK (dbin);
1210 gst_decode_bin_set_sink_caps (GstDecodeBin * dbin, GstCaps * caps)
1212 GST_DEBUG_OBJECT (dbin, "Setting new caps: %" GST_PTR_FORMAT, caps);
1214 g_object_set (dbin->typefind, "force-caps", caps, NULL);
1218 gst_decode_bin_get_sink_caps (GstDecodeBin * dbin)
1222 GST_DEBUG_OBJECT (dbin, "Getting currently set caps");
1224 g_object_get (dbin->typefind, "force-caps", &caps, NULL);
1230 gst_decode_bin_set_subs_encoding (GstDecodeBin * dbin, const gchar * encoding)
1234 GST_DEBUG_OBJECT (dbin, "Setting new encoding: %s", GST_STR_NULL (encoding));
1236 SUBTITLE_LOCK (dbin);
1237 g_free (dbin->encoding);
1238 dbin->encoding = g_strdup (encoding);
1240 /* set the subtitle encoding on all added elements */
1241 for (walk = dbin->subtitles; walk; walk = g_list_next (walk)) {
1242 g_object_set (G_OBJECT (walk->data), "subtitle-encoding", dbin->encoding,
1245 SUBTITLE_UNLOCK (dbin);
1249 gst_decode_bin_get_subs_encoding (GstDecodeBin * dbin)
1253 GST_DEBUG_OBJECT (dbin, "Getting currently set encoding");
1255 SUBTITLE_LOCK (dbin);
1256 encoding = g_strdup (dbin->encoding);
1257 SUBTITLE_UNLOCK (dbin);
1263 gst_decode_bin_set_property (GObject * object, guint prop_id,
1264 const GValue * value, GParamSpec * pspec)
1268 dbin = GST_DECODE_BIN (object);
1272 gst_decode_bin_set_caps (dbin, g_value_get_boxed (value));
1274 case PROP_SUBTITLE_ENCODING:
1275 gst_decode_bin_set_subs_encoding (dbin, g_value_get_string (value));
1277 case PROP_SINK_CAPS:
1278 gst_decode_bin_set_sink_caps (dbin, g_value_get_boxed (value));
1280 case PROP_USE_BUFFERING:
1281 dbin->use_buffering = g_value_get_boolean (value);
1283 case PROP_LOW_PERCENT:
1284 dbin->low_percent = g_value_get_int (value);
1286 case PROP_HIGH_PERCENT:
1287 dbin->high_percent = g_value_get_int (value);
1289 case PROP_MAX_SIZE_BYTES:
1290 dbin->max_size_bytes = g_value_get_uint (value);
1292 case PROP_MAX_SIZE_BUFFERS:
1293 dbin->max_size_buffers = g_value_get_uint (value);
1295 case PROP_MAX_SIZE_TIME:
1296 dbin->max_size_time = g_value_get_uint64 (value);
1298 case PROP_POST_STREAM_TOPOLOGY:
1299 dbin->post_stream_topology = g_value_get_boolean (value);
1301 case PROP_EXPOSE_ALL_STREAMS:
1302 dbin->expose_allstreams = g_value_get_boolean (value);
1304 case PROP_CONNECTION_SPEED:
1305 GST_OBJECT_LOCK (dbin);
1306 dbin->connection_speed = g_value_get_uint64 (value) * 1000;
1307 GST_OBJECT_UNLOCK (dbin);
1310 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1316 gst_decode_bin_get_property (GObject * object, guint prop_id,
1317 GValue * value, GParamSpec * pspec)
1321 dbin = GST_DECODE_BIN (object);
1324 g_value_take_boxed (value, gst_decode_bin_get_caps (dbin));
1326 case PROP_SUBTITLE_ENCODING:
1327 g_value_take_string (value, gst_decode_bin_get_subs_encoding (dbin));
1329 case PROP_SINK_CAPS:
1330 g_value_take_boxed (value, gst_decode_bin_get_sink_caps (dbin));
1332 case PROP_USE_BUFFERING:
1333 g_value_set_boolean (value, dbin->use_buffering);
1335 case PROP_LOW_PERCENT:
1336 g_value_set_int (value, dbin->low_percent);
1338 case PROP_HIGH_PERCENT:
1339 g_value_set_int (value, dbin->high_percent);
1341 case PROP_MAX_SIZE_BYTES:
1342 g_value_set_uint (value, dbin->max_size_bytes);
1344 case PROP_MAX_SIZE_BUFFERS:
1345 g_value_set_uint (value, dbin->max_size_buffers);
1347 case PROP_MAX_SIZE_TIME:
1348 g_value_set_uint64 (value, dbin->max_size_time);
1350 case PROP_POST_STREAM_TOPOLOGY:
1351 g_value_set_boolean (value, dbin->post_stream_topology);
1353 case PROP_EXPOSE_ALL_STREAMS:
1354 g_value_set_boolean (value, dbin->expose_allstreams);
1356 case PROP_CONNECTION_SPEED:
1357 GST_OBJECT_LOCK (dbin);
1358 g_value_set_uint64 (value, dbin->connection_speed / 1000);
1359 GST_OBJECT_UNLOCK (dbin);
1362 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1369 * Default autoplug signal handlers
1372 gst_decode_bin_autoplug_continue (GstElement * element, GstPad * pad,
1375 GST_DEBUG_OBJECT (element, "autoplug-continue returns TRUE");
1377 /* by default we always continue */
1381 static GValueArray *
1382 gst_decode_bin_autoplug_factories (GstElement * element, GstPad * pad,
1386 GValueArray *result;
1387 GstDecodeBin *dbin = GST_DECODE_BIN_CAST (element);
1389 GST_DEBUG_OBJECT (element, "finding factories");
1391 /* return all compatible factories for caps */
1392 g_mutex_lock (&dbin->factories_lock);
1393 gst_decode_bin_update_factories_list (dbin);
1395 gst_element_factory_list_filter (dbin->factories, caps, GST_PAD_SINK,
1396 gst_caps_is_fixed (caps));
1397 g_mutex_unlock (&dbin->factories_lock);
1399 result = g_value_array_new (g_list_length (list));
1400 for (tmp = list; tmp; tmp = tmp->next) {
1401 GstElementFactory *factory = GST_ELEMENT_FACTORY_CAST (tmp->data);
1402 GValue val = { 0, };
1404 g_value_init (&val, G_TYPE_OBJECT);
1405 g_value_set_object (&val, factory);
1406 g_value_array_append (result, &val);
1407 g_value_unset (&val);
1409 gst_plugin_feature_list_free (list);
1411 GST_DEBUG_OBJECT (element, "autoplug-factories returns %p", result);
1416 static GValueArray *
1417 gst_decode_bin_autoplug_sort (GstElement * element, GstPad * pad,
1418 GstCaps * caps, GValueArray * factories)
1423 static GstAutoplugSelectResult
1424 gst_decode_bin_autoplug_select (GstElement * element, GstPad * pad,
1425 GstCaps * caps, GstElementFactory * factory)
1427 GST_DEBUG_OBJECT (element, "default autoplug-select returns TRY");
1430 return GST_AUTOPLUG_SELECT_TRY;
1434 gst_decode_bin_autoplug_query (GstElement * element, GstPad * pad,
1437 /* No query handled here */
1445 static gboolean are_final_caps (GstDecodeBin * dbin, GstCaps * caps);
1446 static gboolean is_demuxer_element (GstElement * srcelement);
1447 static gboolean is_adaptive_demuxer_element (GstElement * srcelement);
1449 static gboolean connect_pad (GstDecodeBin * dbin, GstElement * src,
1450 GstDecodePad * dpad, GstPad * pad, GstCaps * caps, GValueArray * factories,
1451 GstDecodeChain * chain, gchar ** deadend_details);
1452 static GList *connect_element (GstDecodeBin * dbin, GstDecodeElement * delem,
1453 GstDecodeChain * chain);
1454 static void expose_pad (GstDecodeBin * dbin, GstElement * src,
1455 GstDecodePad * dpad, GstPad * pad, GstCaps * caps, GstDecodeChain * chain);
1457 static void pad_added_cb (GstElement * element, GstPad * pad,
1458 GstDecodeChain * chain);
1459 static void pad_removed_cb (GstElement * element, GstPad * pad,
1460 GstDecodeChain * chain);
1461 static void no_more_pads_cb (GstElement * element, GstDecodeChain * chain);
1463 static GstDecodeGroup *gst_decode_chain_get_current_group (GstDecodeChain *
1467 clear_sticky_events (GstPad * pad, GstEvent ** event, gpointer user_data)
1469 GST_DEBUG_OBJECT (pad, "clearing sticky event %" GST_PTR_FORMAT, *event);
1470 gst_event_unref (*event);
1476 copy_sticky_events (GstPad * pad, GstEvent ** event, gpointer user_data)
1478 GstPad *gpad = GST_PAD_CAST (user_data);
1480 GST_DEBUG_OBJECT (gpad, "store sticky event %" GST_PTR_FORMAT, *event);
1481 gst_pad_store_sticky_event (gpad, *event);
1487 decode_pad_set_target (GstDecodePad * dpad, GstPad * target)
1489 gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (dpad), target);
1491 gst_pad_sticky_events_foreach (GST_PAD_CAST (dpad), clear_sticky_events,
1494 gst_pad_sticky_events_foreach (target, copy_sticky_events, dpad);
1497 /* called when a new pad is discovered. It will perform some basic actions
1498 * before trying to link something to it.
1500 * - Check the caps, don't do anything when there are no caps or when they have
1502 * - signal AUTOPLUG_CONTINUE to check if we need to continue autoplugging this
1504 * - if the caps are non-fixed, setup a handler to continue autoplugging when
1505 * the caps become fixed (connect to notify::caps).
1506 * - get list of factories to autoplug.
1507 * - continue autoplugging to one of the factories.
1509 /* returns whether to expose the pad */
1511 analyze_new_pad (GstDecodeBin * dbin, GstElement * src, GstPad * pad,
1512 GstCaps * caps, GstDecodeChain * chain, GstDecodeChain ** new_chain)
1514 gboolean apcontinue = TRUE;
1515 GValueArray *factories = NULL, *result = NULL;
1517 GstElementFactory *factory;
1518 const gchar *classification;
1519 gboolean is_parser_converter = FALSE;
1521 gchar *deadend_details = NULL;
1523 GST_DEBUG_OBJECT (dbin, "Pad %s:%s caps:%" GST_PTR_FORMAT,
1524 GST_DEBUG_PAD_NAME (pad), caps);
1530 && src != ((GstDecodeElement *) chain->elements->data)->element
1531 && src != ((GstDecodeElement *) chain->elements->data)->capsfilter) {
1532 GST_ERROR_OBJECT (dbin, "New pad from not the last element in this chain");
1536 if (chain->endpad) {
1537 GST_ERROR_OBJECT (dbin, "New pad in a chain that is already complete");
1541 if (chain->demuxer) {
1542 GstDecodeGroup *group;
1543 GstDecodeChain *oldchain = chain;
1544 GstDecodeElement *demux = (chain->elements ? chain->elements->data : NULL);
1546 if (chain->current_pad)
1547 gst_object_unref (chain->current_pad);
1548 chain->current_pad = NULL;
1550 /* we are adding a new pad for a demuxer (see is_demuxer_element(),
1551 * start a new chain for it */
1552 CHAIN_MUTEX_LOCK (oldchain);
1553 group = gst_decode_chain_get_current_group (chain);
1554 if (group && !g_list_find (group->children, chain)) {
1555 g_assert (new_chain != NULL);
1556 *new_chain = chain = gst_decode_chain_new (dbin, group, pad);
1557 group->children = g_list_prepend (group->children, chain);
1559 CHAIN_MUTEX_UNLOCK (oldchain);
1561 GST_WARNING_OBJECT (dbin, "No current group");
1565 /* If this is not a dynamic pad demuxer, we're no-more-pads
1566 * already before anything else happens
1568 if (demux == NULL || !demux->no_more_pads_id)
1569 group->no_more_pads = TRUE;
1572 /* From here on we own a reference to the caps as
1573 * we might create new caps below and would need
1574 * to unref them later */
1576 gst_caps_ref (caps);
1578 if ((caps == NULL) || gst_caps_is_empty (caps))
1581 if (gst_caps_is_any (caps))
1584 if (!chain->current_pad)
1585 chain->current_pad = gst_decode_pad_new (dbin, chain);
1587 dpad = gst_object_ref (chain->current_pad);
1588 gst_pad_set_active (GST_PAD_CAST (dpad), TRUE);
1589 decode_pad_set_target (dpad, pad);
1591 /* 1. Emit 'autoplug-continue' the result will tell us if this pads needs
1592 * further autoplugging. Only do this for fixed caps, for unfixed caps
1593 * we will later come here again from the notify::caps handler. The
1594 * problem with unfixed caps is that, we can't reliably tell if the output
1595 * is e.g. accepted by a sink because only parts of the possible final
1596 * caps might be accepted by the sink. */
1597 if (gst_caps_is_fixed (caps))
1598 g_signal_emit (G_OBJECT (dbin),
1599 gst_decode_bin_signals[SIGNAL_AUTOPLUG_CONTINUE], 0, dpad, caps,
1604 /* 1.a if autoplug-continue is FALSE or caps is a raw format, goto pad_is_final */
1605 if ((!apcontinue) || are_final_caps (dbin, caps))
1608 /* 1.b For Parser/Converter that can output different stream formats
1609 * we insert a capsfilter with the sorted caps of all possible next
1610 * elements and continue with the capsfilter srcpad */
1611 factory = gst_element_get_factory (src);
1613 gst_element_factory_get_metadata (factory, GST_ELEMENT_METADATA_KLASS);
1614 is_parser_converter = (strstr (classification, "Parser")
1615 && strstr (classification, "Converter"));
1617 /* 1.c when the caps are not fixed yet, we can't be sure what element to
1618 * connect. We delay autoplugging until the caps are fixed */
1619 if (!is_parser_converter && !gst_caps_is_fixed (caps)) {
1621 } else if (!is_parser_converter) {
1622 gst_caps_unref (caps);
1623 caps = gst_pad_get_current_caps (pad);
1625 GST_DEBUG_OBJECT (dbin, "No final caps set yet, delaying autoplugging");
1626 gst_object_unref (dpad);
1627 goto setup_caps_delay;
1631 /* 1.d else get the factories and if there's no compatible factory goto
1633 g_signal_emit (G_OBJECT (dbin),
1634 gst_decode_bin_signals[SIGNAL_AUTOPLUG_FACTORIES], 0, dpad, caps,
1637 /* NULL means that we can expose the pad */
1638 if (factories == NULL)
1641 /* if the array is empty, we have a type for which we have no decoder */
1642 if (factories->n_values == 0) {
1643 if (!dbin->expose_allstreams) {
1644 GstCaps *raw = gst_static_caps_get (&default_raw_caps);
1646 /* If the caps are raw, this just means we don't want to expose them */
1647 if (gst_caps_is_subset (caps, raw)) {
1648 g_value_array_free (factories);
1649 gst_caps_unref (raw);
1650 gst_object_unref (dpad);
1651 goto discarded_type;
1653 gst_caps_unref (raw);
1656 /* if not we have a unhandled type with no compatible factories */
1657 g_value_array_free (factories);
1658 gst_object_unref (dpad);
1662 /* 1.e sort some more. */
1663 g_signal_emit (G_OBJECT (dbin),
1664 gst_decode_bin_signals[SIGNAL_AUTOPLUG_SORT], 0, dpad, caps, factories,
1667 g_value_array_free (factories);
1671 /* At this point we have a potential decoder, but we might not need it
1672 * if it doesn't match the output caps */
1673 if (!dbin->expose_allstreams && gst_caps_is_fixed (caps)) {
1676 gboolean dontuse = FALSE;
1678 GST_DEBUG ("Checking if we can abort early");
1680 /* 1.f Do an early check to see if the candidates are potential decoders, but
1681 * due to the fact that they decode to a mediatype that is not final we don't
1684 for (i = 0; i < factories->n_values && !dontuse; i++) {
1685 GstElementFactory *factory =
1686 g_value_get_object (g_value_array_get_nth (factories, i));
1689 /* We are only interested in skipping decoders */
1690 if (strstr (gst_element_factory_get_metadata (factory,
1691 GST_ELEMENT_METADATA_KLASS), "Decoder")) {
1693 GST_DEBUG ("Trying factory %s",
1694 gst_plugin_feature_get_name (GST_PLUGIN_FEATURE (factory)));
1696 /* Check the source pad template caps to see if they match raw caps but don't match
1698 for (tmps = gst_element_factory_get_static_pad_templates (factory);
1699 tmps && !dontuse; tmps = tmps->next) {
1700 GstStaticPadTemplate *st = (GstStaticPadTemplate *) tmps->data;
1701 if (st->direction != GST_PAD_SRC)
1703 tcaps = gst_static_pad_template_get_caps (st);
1707 /* Emit autoplug-continue to see if the caps are considered to be raw caps */
1708 g_signal_emit (G_OBJECT (dbin),
1709 gst_decode_bin_signals[SIGNAL_AUTOPLUG_CONTINUE], 0, dpad, tcaps,
1712 /* If autoplug-continue returns TRUE and the caps are not final, don't use them */
1713 if (apcontinue && !are_final_caps (dbin, tcaps))
1715 gst_caps_unref (tcaps);
1721 gst_object_unref (dpad);
1722 g_value_array_free (factories);
1723 goto discarded_type;
1727 /* 1.g now get the factory template caps and insert the capsfilter if this
1728 * is a parser/converter
1730 if (is_parser_converter) {
1731 GstCaps *filter_caps;
1733 GstElement *capsfilter;
1735 GstDecodeElement *delem;
1737 filter_caps = gst_caps_new_empty ();
1738 for (i = 0; i < factories->n_values; i++) {
1739 GstElementFactory *factory =
1740 g_value_get_object (g_value_array_get_nth (factories, i));
1741 GstCaps *tcaps, *intersection;
1744 GST_DEBUG ("Trying factory %s",
1745 gst_plugin_feature_get_name (GST_PLUGIN_FEATURE (factory)));
1747 if (gst_element_get_factory (src) == factory ||
1748 gst_element_factory_list_is_type (factory,
1749 GST_ELEMENT_FACTORY_TYPE_PARSER)) {
1750 GST_DEBUG ("Skipping factory");
1754 for (tmps = gst_element_factory_get_static_pad_templates (factory); tmps;
1755 tmps = tmps->next) {
1756 GstStaticPadTemplate *st = (GstStaticPadTemplate *) tmps->data;
1757 if (st->direction != GST_PAD_SINK || st->presence != GST_PAD_ALWAYS)
1759 tcaps = gst_static_pad_template_get_caps (st);
1761 gst_caps_intersect_full (tcaps, caps, GST_CAPS_INTERSECT_FIRST);
1762 filter_caps = gst_caps_merge (filter_caps, intersection);
1763 gst_caps_unref (tcaps);
1767 /* Append the parser caps to prevent any not-negotiated errors */
1768 filter_caps = gst_caps_merge (filter_caps, gst_caps_ref (caps));
1770 if (chain->elements) {
1771 delem = (GstDecodeElement *) chain->elements->data;
1772 capsfilter = delem->capsfilter =
1773 gst_element_factory_make ("capsfilter", NULL);
1775 delem = g_slice_new0 (GstDecodeElement);
1776 capsfilter = delem->element =
1777 gst_element_factory_make ("capsfilter", NULL);
1778 delem->capsfilter = NULL;
1779 chain->elements = g_list_prepend (chain->elements, delem);
1782 g_object_set (G_OBJECT (capsfilter), "caps", filter_caps, NULL);
1783 gst_caps_unref (filter_caps);
1784 gst_element_set_state (capsfilter, GST_STATE_PAUSED);
1785 gst_bin_add (GST_BIN_CAST (dbin), gst_object_ref (capsfilter));
1787 decode_pad_set_target (dpad, NULL);
1788 p = gst_element_get_static_pad (capsfilter, "sink");
1789 gst_pad_link_full (pad, p, GST_PAD_LINK_CHECK_NOTHING);
1790 gst_object_unref (p);
1791 p = gst_element_get_static_pad (capsfilter, "src");
1792 decode_pad_set_target (dpad, p);
1795 gst_caps_unref (caps);
1797 caps = gst_pad_get_current_caps (pad);
1799 GST_DEBUG_OBJECT (dbin, "No final caps set yet, delaying autoplugging");
1800 gst_object_unref (dpad);
1801 g_value_array_free (factories);
1802 goto setup_caps_delay;
1806 /* 1.h else continue autoplugging something from the list. */
1807 GST_LOG_OBJECT (pad, "Let's continue discovery on this pad");
1809 connect_pad (dbin, src, dpad, pad, caps, factories, chain,
1812 /* Need to unref the capsfilter srcpad here if
1813 * we inserted a capsfilter */
1814 if (is_parser_converter)
1815 gst_object_unref (pad);
1817 gst_object_unref (dpad);
1818 g_value_array_free (factories);
1823 gst_caps_unref (caps);
1829 GST_LOG_OBJECT (dbin, "Pad is final and should expose the pad. "
1830 "autoplug-continue:%d", apcontinue);
1831 gst_object_unref (dpad);
1832 gst_caps_unref (caps);
1838 GST_LOG_OBJECT (pad, "Known type, but discarded because not final caps");
1839 chain->deadend = TRUE;
1840 chain->endcaps = caps;
1841 gst_object_replace ((GstObject **) & chain->current_pad, NULL);
1843 /* Try to expose anything */
1845 if (dbin->decode_chain) {
1846 if (gst_decode_chain_is_complete (dbin->decode_chain)) {
1847 gst_decode_bin_expose (dbin);
1850 EXPOSE_UNLOCK (dbin);
1851 do_async_done (dbin);
1858 GST_LOG_OBJECT (pad, "Unknown type, posting message and firing signal");
1860 chain->deadend_details = deadend_details;
1861 chain->deadend = TRUE;
1862 chain->endcaps = caps;
1863 gst_object_replace ((GstObject **) & chain->current_pad, NULL);
1865 gst_element_post_message (GST_ELEMENT_CAST (dbin),
1866 gst_missing_decoder_message_new (GST_ELEMENT_CAST (dbin), caps));
1868 g_signal_emit (G_OBJECT (dbin),
1869 gst_decode_bin_signals[SIGNAL_UNKNOWN_TYPE], 0, pad, caps);
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);
1880 if (src == dbin->typefind) {
1881 if (!caps || gst_caps_is_empty (caps)) {
1882 GST_ELEMENT_ERROR (dbin, STREAM, TYPE_NOT_FOUND,
1883 (_("Could not determine type of stream")), (NULL));
1885 do_async_done (dbin);
1891 GST_DEBUG_OBJECT (pad, "pad has non-fixed caps delay autoplugging");
1892 gst_object_unref (dpad);
1893 goto setup_caps_delay;
1897 GST_DEBUG_OBJECT (pad, "pad has ANY caps, delaying auto-pluggin");
1898 goto setup_caps_delay;
1902 GstPendingPad *ppad;
1904 /* connect to caps notification */
1905 CHAIN_MUTEX_LOCK (chain);
1906 GST_LOG_OBJECT (dbin, "Chain %p has now %d dynamic pads", chain,
1907 g_list_length (chain->pending_pads));
1908 ppad = g_slice_new0 (GstPendingPad);
1909 ppad->pad = gst_object_ref (pad);
1910 ppad->chain = chain;
1911 ppad->event_probe_id =
1912 gst_pad_add_probe (pad, GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM,
1913 pad_event_cb, ppad, NULL);
1914 chain->pending_pads = g_list_prepend (chain->pending_pads, ppad);
1915 ppad->notify_caps_id = g_signal_connect (pad, "notify::caps",
1916 G_CALLBACK (caps_notify_cb), chain);
1917 CHAIN_MUTEX_UNLOCK (chain);
1919 /* If we're here because we have a Parser/Converter
1920 * we have to unref the pad */
1921 if (is_parser_converter)
1922 gst_object_unref (pad);
1924 gst_caps_unref (caps);
1931 add_error_filter (GstDecodeBin * dbin, GstElement * element)
1933 GST_OBJECT_LOCK (dbin);
1934 dbin->filtered = g_list_prepend (dbin->filtered, element);
1935 GST_OBJECT_UNLOCK (dbin);
1939 remove_error_filter (GstDecodeBin * dbin, GstElement * element,
1940 GstMessage ** error)
1944 GST_OBJECT_LOCK (dbin);
1945 dbin->filtered = g_list_remove (dbin->filtered, element);
1950 l = dbin->filtered_errors;
1952 GstMessage *msg = l->data;
1954 if (GST_MESSAGE_SRC (msg) == GST_OBJECT_CAST (element)) {
1955 /* Get the last error of this element, i.e. the earliest */
1957 gst_message_replace (error, msg);
1958 gst_message_unref (msg);
1959 l = dbin->filtered_errors = g_list_delete_link (dbin->filtered_errors, l);
1964 GST_OBJECT_UNLOCK (dbin);
1971 } SendStickyEventsData;
1974 send_sticky_event (GstPad * pad, GstEvent ** event, gpointer user_data)
1976 SendStickyEventsData *data = user_data;
1979 ret = gst_pad_send_event (data->peer, gst_event_ref (*event));
1987 send_sticky_events (GstDecodeBin * dbin, GstPad * pad)
1989 SendStickyEventsData data;
1992 data.peer = gst_pad_get_peer (pad);
1994 gst_pad_sticky_events_foreach (pad, send_sticky_event, &data);
1996 gst_object_unref (data.peer);
2002 error_message_to_string (GstMessage * msg)
2005 gchar *debug, *message, *full_message;
2007 gst_message_parse_error (msg, &err, &debug);
2009 message = gst_error_get_message (err->domain, err->code);
2012 full_message = g_strdup_printf ("%s\n%s\n%s", message, err->message, debug);
2014 full_message = g_strdup_printf ("%s\n%s", message, err->message);
2018 g_clear_error (&err);
2020 return full_message;
2023 static GstPadProbeReturn
2024 demuxer_source_pad_probe (GstPad * pad, GstPadProbeInfo * info,
2027 GstEvent *event = GST_PAD_PROBE_INFO_EVENT (info);
2028 GstDecodeGroup *group = (GstDecodeGroup *) user_data;
2029 GstDecodeChain *parent_chain = group->parent;
2031 GST_DEBUG_OBJECT (pad, "Saw event %s", GST_EVENT_TYPE_NAME (event));
2032 /* Check if we are the active group, if not we need to proxy the flush
2033 * events to the other groups (of which at least one is exposed, ensuring
2034 * flushing properly propagates downstream of decodebin */
2035 if (parent_chain->active_group == group)
2036 return GST_PAD_PROBE_OK;
2038 switch (GST_EVENT_TYPE (event)) {
2039 case GST_EVENT_FLUSH_START:
2040 case GST_EVENT_FLUSH_STOP:
2043 GST_DEBUG_OBJECT (pad, "Proxying flush events to inactive groups");
2044 /* Proxy to active group */
2045 for (tmp = parent_chain->active_group->reqpads; tmp; tmp = tmp->next) {
2046 GstPad *reqpad = (GstPad *) tmp->data;
2047 gst_pad_send_event (reqpad, gst_event_ref (event));
2049 /* Proxy to other non-active groups (except ourself) */
2050 for (tmp = parent_chain->next_groups; tmp; tmp = tmp->next) {
2052 GstDecodeGroup *tmpgroup = (GstDecodeGroup *) tmp->data;
2053 if (tmpgroup != group) {
2054 for (tmp2 = tmpgroup->reqpads; tmp2; tmp2 = tmp2->next) {
2055 GstPad *reqpad = (GstPad *) tmp2->data;
2056 gst_pad_send_event (reqpad, gst_event_ref (event));
2060 flush_chain (parent_chain,
2061 GST_EVENT_TYPE (event) == GST_EVENT_FLUSH_START);
2068 return GST_PAD_PROBE_OK;
2073 GstDecodeChain *chain;
2079 * Try to connect the given pad to an element created from one of the factories,
2082 * Note that dpad is ghosting pad, and so pad is linked; be sure to unset dpad's
2083 * target before trying to link pad.
2085 * Returns TRUE if an element was properly created and linked
2088 connect_pad (GstDecodeBin * dbin, GstElement * src, GstDecodePad * dpad,
2089 GstPad * pad, GstCaps * caps, GValueArray * factories,
2090 GstDecodeChain * chain, gchar ** deadend_details)
2092 gboolean res = FALSE;
2093 GstPad *mqpad = NULL;
2094 gboolean is_demuxer = chain->parent && !chain->elements; /* First pad after the demuxer */
2095 GString *error_details = NULL;
2097 g_return_val_if_fail (factories != NULL, FALSE);
2098 g_return_val_if_fail (factories->n_values > 0, FALSE);
2100 GST_DEBUG_OBJECT (dbin,
2101 "pad %s:%s , chain:%p, %d factories, caps %" GST_PTR_FORMAT,
2102 GST_DEBUG_PAD_NAME (pad), chain, factories->n_values, caps);
2104 /* 1. is element demuxer or parser */
2106 GST_LOG_OBJECT (src,
2107 "is a demuxer, connecting the pad through multiqueue '%s'",
2108 GST_OBJECT_NAME (chain->parent->multiqueue));
2110 /* Set a flush-start/-stop probe on the downstream events */
2111 chain->pad_probe_id =
2112 gst_pad_add_probe (pad, GST_PAD_PROBE_TYPE_EVENT_FLUSH,
2113 demuxer_source_pad_probe, chain->parent, NULL);
2115 decode_pad_set_target (dpad, NULL);
2116 if (!(mqpad = gst_decode_group_control_demuxer_pad (chain->parent, pad)))
2118 src = chain->parent->multiqueue;
2119 /* Forward sticky events to mq src pad to allow factory initialization */
2120 gst_pad_sticky_events_foreach (pad, copy_sticky_events, mqpad);
2122 decode_pad_set_target (dpad, pad);
2125 error_details = g_string_new ("");
2127 /* 2. Try to create an element and link to it */
2128 while (factories->n_values > 0) {
2129 GstAutoplugSelectResult ret;
2130 GstElementFactory *factory;
2131 GstDecodeElement *delem;
2132 GstElement *element;
2136 GList *to_connect = NULL;
2137 GList *to_expose = NULL;
2138 gboolean is_parser = FALSE;
2139 gboolean is_decoder = FALSE;
2141 /* Set dpad target to pad again, it might've been unset
2142 * below but we came back here because something failed
2144 decode_pad_set_target (dpad, pad);
2146 /* take first factory */
2147 factory = g_value_get_object (g_value_array_get_nth (factories, 0));
2148 /* Remove selected factory from the list. */
2149 g_value_array_remove (factories, 0);
2151 GST_LOG_OBJECT (src, "trying factory %" GST_PTR_FORMAT, factory);
2153 /* Check if the caps are really supported by the factory. The
2154 * factory list is non-empty-subset filtered while caps
2155 * are only accepted by a pad if they are a subset of the
2158 * FIXME: Only do this for fixed caps here. Non-fixed caps
2159 * can happen if a Parser/Converter was autoplugged before
2160 * this. We then assume that it will be able to convert to
2161 * everything that the decoder would want.
2163 * A subset check will fail here because the parser caps
2164 * will be generic and while the decoder will only
2165 * support a subset of the parser caps.
2167 if (gst_caps_is_fixed (caps)) {
2168 const GList *templs;
2169 gboolean skip = FALSE;
2171 templs = gst_element_factory_get_static_pad_templates (factory);
2174 GstStaticPadTemplate *templ = (GstStaticPadTemplate *) templs->data;
2176 if (templ->direction == GST_PAD_SINK) {
2177 GstCaps *templcaps = gst_static_caps_get (&templ->static_caps);
2179 if (!gst_caps_is_subset (caps, templcaps)) {
2180 GST_DEBUG_OBJECT (src,
2181 "caps %" GST_PTR_FORMAT " not subset of %" GST_PTR_FORMAT, caps,
2183 gst_caps_unref (templcaps);
2188 gst_caps_unref (templcaps);
2190 templs = g_list_next (templs);
2196 /* If the factory is for a parser we first check if the factory
2197 * was already used for the current chain. If it was used already
2198 * we would otherwise create an infinite loop here because the
2199 * parser apparently accepts its own output as input.
2200 * This is only done for parsers because it's perfectly valid
2201 * to have other element classes after each other because a
2202 * parser is the only one that does not change the data. A
2203 * valid example for this would be multiple id3demux in a row.
2205 is_parser = strstr (gst_element_factory_get_metadata (factory,
2206 GST_ELEMENT_METADATA_KLASS), "Parser") != NULL;
2209 gboolean skip = FALSE;
2212 CHAIN_MUTEX_LOCK (chain);
2213 for (l = chain->elements; l; l = l->next) {
2214 GstDecodeElement *delem = (GstDecodeElement *) l->data;
2215 GstElement *otherelement = delem->element;
2217 if (gst_element_get_factory (otherelement) == factory) {
2223 if (!skip && chain->parent && chain->parent->parent) {
2224 GstDecodeChain *parent_chain = chain->parent->parent;
2225 GstDecodeElement *pelem =
2226 parent_chain->elements ? parent_chain->elements->data : NULL;
2228 if (pelem && gst_element_get_factory (pelem->element) == factory)
2231 CHAIN_MUTEX_UNLOCK (chain);
2233 GST_DEBUG_OBJECT (dbin,
2234 "Skipping factory '%s' because it was already used in this chain",
2235 gst_plugin_feature_get_name (GST_PLUGIN_FEATURE_CAST (factory)));
2241 /* emit autoplug-select to see what we should do with it. */
2242 g_signal_emit (G_OBJECT (dbin),
2243 gst_decode_bin_signals[SIGNAL_AUTOPLUG_SELECT],
2244 0, dpad, caps, factory, &ret);
2247 case GST_AUTOPLUG_SELECT_TRY:
2248 GST_DEBUG_OBJECT (dbin, "autoplug select requested try");
2250 case GST_AUTOPLUG_SELECT_EXPOSE:
2251 GST_DEBUG_OBJECT (dbin, "autoplug select requested expose");
2252 /* expose the pad, we don't have the source element */
2253 expose_pad (dbin, src, dpad, pad, caps, chain);
2256 case GST_AUTOPLUG_SELECT_SKIP:
2257 GST_DEBUG_OBJECT (dbin, "autoplug select requested skip");
2260 GST_WARNING_OBJECT (dbin, "autoplug select returned unhandled %d", ret);
2264 /* 2.0. Unlink pad */
2265 decode_pad_set_target (dpad, NULL);
2267 /* 2.1. Try to create an element */
2268 if ((element = gst_element_factory_create (factory, NULL)) == NULL) {
2269 GST_WARNING_OBJECT (dbin, "Could not create an element from %s",
2270 gst_plugin_feature_get_name (GST_PLUGIN_FEATURE (factory)));
2271 g_string_append_printf (error_details,
2272 "Could not create an element from %s\n",
2273 gst_plugin_feature_get_name (GST_PLUGIN_FEATURE (factory)));
2277 /* Filter errors, this will prevent the element from causing the pipeline
2278 * to error while we test it using READY state. */
2279 add_error_filter (dbin, element);
2281 /* We don't yet want the bin to control the element's state */
2282 gst_element_set_locked_state (element, TRUE);
2284 /* ... add it ... */
2285 if (!(gst_bin_add (GST_BIN_CAST (dbin), element))) {
2286 GST_WARNING_OBJECT (dbin, "Couldn't add %s to the bin",
2287 GST_ELEMENT_NAME (element));
2288 remove_error_filter (dbin, element, NULL);
2289 g_string_append_printf (error_details, "Couldn't add %s to the bin\n",
2290 GST_ELEMENT_NAME (element));
2291 gst_object_unref (element);
2295 /* Find its sink pad. */
2296 if (!(sinkpad = find_sink_pad (element))) {
2297 GST_WARNING_OBJECT (dbin, "Element %s doesn't have a sink pad",
2298 GST_ELEMENT_NAME (element));
2299 remove_error_filter (dbin, element, NULL);
2300 g_string_append_printf (error_details,
2301 "Element %s doesn't have a sink pad", GST_ELEMENT_NAME (element));
2302 gst_bin_remove (GST_BIN (dbin), element);
2306 /* ... and try to link */
2307 if ((gst_pad_link_full (pad, sinkpad,
2308 GST_PAD_LINK_CHECK_NOTHING)) != GST_PAD_LINK_OK) {
2309 GST_WARNING_OBJECT (dbin, "Link failed on pad %s:%s",
2310 GST_DEBUG_PAD_NAME (sinkpad));
2311 remove_error_filter (dbin, element, NULL);
2312 g_string_append_printf (error_details, "Link failed on pad %s:%s",
2313 GST_DEBUG_PAD_NAME (sinkpad));
2314 gst_object_unref (sinkpad);
2315 gst_bin_remove (GST_BIN (dbin), element);
2319 /* ... activate it ... */
2320 if ((gst_element_set_state (element,
2321 GST_STATE_READY)) == GST_STATE_CHANGE_FAILURE) {
2322 GstMessage *error_msg;
2324 GST_WARNING_OBJECT (dbin, "Couldn't set %s to READY",
2325 GST_ELEMENT_NAME (element));
2326 remove_error_filter (dbin, element, &error_msg);
2329 gchar *error_string = error_message_to_string (error_msg);
2330 g_string_append_printf (error_details, "Couldn't set %s to READY:\n%s",
2331 GST_ELEMENT_NAME (element), error_string);
2332 gst_message_unref (error_msg);
2333 g_free (error_string);
2335 g_string_append_printf (error_details, "Couldn't set %s to READY",
2336 GST_ELEMENT_NAME (element));
2338 gst_object_unref (sinkpad);
2339 gst_bin_remove (GST_BIN (dbin), element);
2343 /* check if we still accept the caps on the pad after setting
2344 * the element to READY */
2345 if (!gst_pad_query_accept_caps (sinkpad, caps)) {
2346 GstMessage *error_msg;
2348 GST_WARNING_OBJECT (dbin, "Element %s does not accept caps",
2349 GST_ELEMENT_NAME (element));
2351 remove_error_filter (dbin, element, &error_msg);
2354 gchar *error_string = error_message_to_string (error_msg);
2355 g_string_append_printf (error_details,
2356 "Element %s does not accept caps:\n%s", GST_ELEMENT_NAME (element),
2358 gst_message_unref (error_msg);
2359 g_free (error_string);
2361 g_string_append_printf (error_details,
2362 "Element %s does not accept caps", GST_ELEMENT_NAME (element));
2365 gst_element_set_state (element, GST_STATE_NULL);
2366 gst_object_unref (sinkpad);
2367 gst_bin_remove (GST_BIN (dbin), element);
2371 gst_object_unref (sinkpad);
2372 GST_LOG_OBJECT (dbin, "linked on pad %s:%s", GST_DEBUG_PAD_NAME (pad));
2374 CHAIN_MUTEX_LOCK (chain);
2375 delem = g_slice_new0 (GstDecodeElement);
2376 delem->element = gst_object_ref (element);
2377 delem->capsfilter = NULL;
2378 chain->elements = g_list_prepend (chain->elements, delem);
2379 chain->demuxer = is_demuxer_element (element);
2380 chain->adaptive_demuxer = is_adaptive_demuxer_element (element);
2382 is_decoder = strstr (gst_element_factory_get_metadata (factory,
2383 GST_ELEMENT_METADATA_KLASS), "Decoder") != NULL;
2385 /* For adaptive streaming demuxer we insert a multiqueue after
2387 * Now for the case where we have a container stream inside these
2388 * buffers, another demuxer will be plugged and after this second
2389 * demuxer there will be a second multiqueue. This second multiqueue
2390 * will get smaller buffers and will be the one emitting buffering
2392 * If we don't have a container stream inside the fragment buffers,
2393 * we'll insert a multiqueue below right after the next element after
2394 * the adaptive streaming demuxer. This is going to be a parser or
2395 * decoder, and will output smaller buffers.
2397 if (chain->parent && chain->parent->parent) {
2398 GstDecodeChain *parent_chain = chain->parent->parent;
2400 if (parent_chain->adaptive_demuxer && (is_parser || is_decoder))
2401 chain->demuxer = TRUE;
2404 /* If we are configured to use buffering and there is no demuxer in the
2405 * chain, we still want a multiqueue, otherwise we will ignore the
2406 * use-buffering property. In that case, we will insert a multiqueue after
2407 * the parser or decoder - not elsewhere, otherwise we won't have
2411 if (!chain->parent && (is_parser || is_decoder) && dbin->use_buffering) {
2412 chain->demuxer = TRUE;
2414 GST_WARNING_OBJECT (dbin,
2415 "Buffering messages used for decoded and non-parsed data");
2419 CHAIN_MUTEX_UNLOCK (chain);
2421 /* Set connection-speed property if needed */
2422 if (chain->demuxer) {
2425 if ((pspec = g_object_class_find_property (G_OBJECT_GET_CLASS (element),
2426 "connection-speed"))) {
2427 guint64 speed = dbin->connection_speed / 1000;
2428 gboolean wrong_type = FALSE;
2430 if (G_PARAM_SPEC_TYPE (pspec) == G_TYPE_PARAM_UINT) {
2431 GParamSpecUInt *pspecuint = G_PARAM_SPEC_UINT (pspec);
2433 speed = CLAMP (speed, pspecuint->minimum, pspecuint->maximum);
2434 } else if (G_PARAM_SPEC_TYPE (pspec) == G_TYPE_PARAM_INT) {
2435 GParamSpecInt *pspecint = G_PARAM_SPEC_INT (pspec);
2437 speed = CLAMP (speed, pspecint->minimum, pspecint->maximum);
2438 } else if (G_PARAM_SPEC_TYPE (pspec) == G_TYPE_PARAM_UINT64) {
2439 GParamSpecUInt64 *pspecuint = G_PARAM_SPEC_UINT64 (pspec);
2441 speed = CLAMP (speed, pspecuint->minimum, pspecuint->maximum);
2442 } else if (G_PARAM_SPEC_TYPE (pspec) == G_TYPE_PARAM_INT64) {
2443 GParamSpecInt64 *pspecint = G_PARAM_SPEC_INT64 (pspec);
2445 speed = CLAMP (speed, pspecint->minimum, pspecint->maximum);
2447 GST_WARNING_OBJECT (dbin,
2448 "The connection speed property %" G_GUINT64_FORMAT " of type %s"
2449 " is not usefull not setting it", speed,
2450 g_type_name (G_PARAM_SPEC_TYPE (pspec)));
2455 GST_DEBUG_OBJECT (dbin, "setting connection-speed=%" G_GUINT64_FORMAT
2456 " to demuxer element", speed);
2458 g_object_set (element, "connection-speed", speed, NULL);
2463 /* try to configure the subtitle encoding property when we can */
2464 pspec = g_object_class_find_property (G_OBJECT_GET_CLASS (element),
2465 "subtitle-encoding");
2466 if (pspec && G_PARAM_SPEC_VALUE_TYPE (pspec) == G_TYPE_STRING) {
2467 SUBTITLE_LOCK (dbin);
2468 GST_DEBUG_OBJECT (dbin,
2469 "setting subtitle-encoding=%s to element", dbin->encoding);
2470 g_object_set (G_OBJECT (element), "subtitle-encoding", dbin->encoding,
2472 SUBTITLE_UNLOCK (dbin);
2478 /* link this element further */
2479 to_connect = connect_element (dbin, delem, chain);
2481 while (to_connect) {
2482 GstPad *opad = to_connect->data;
2483 gboolean expose_pad = FALSE;
2484 GstDecodeChain *new_chain;
2487 ocaps = get_pad_caps (opad);
2489 analyze_new_pad (dbin, delem->element, opad, ocaps, chain,
2493 gst_caps_unref (ocaps);
2496 PadExposeData *expose_data = g_new0 (PadExposeData, 1);
2497 expose_data->chain = new_chain;
2498 expose_data->pad = gst_object_ref (opad);
2499 to_expose = g_list_prepend (to_expose, expose_data);
2502 gst_object_unref (opad);
2503 to_connect = g_list_delete_link (to_connect, to_connect);
2505 /* any pads left in to_expose are to be exposed */
2507 /* Bring the element to the state of the parent */
2509 /* First lock element's sinkpad stream lock so no data reaches
2510 * the possible new element added when caps are sent by element
2511 * while we're still sending sticky events */
2512 GST_PAD_STREAM_LOCK (sinkpad);
2514 if ((gst_element_set_state (element,
2515 GST_STATE_PAUSED)) == GST_STATE_CHANGE_FAILURE ||
2516 !send_sticky_events (dbin, pad)) {
2517 GstDecodeElement *dtmp = NULL;
2518 GstElement *tmp = NULL;
2519 GstMessage *error_msg;
2521 GST_PAD_STREAM_UNLOCK (sinkpad);
2523 GST_WARNING_OBJECT (dbin, "Couldn't set %s to PAUSED",
2524 GST_ELEMENT_NAME (element));
2527 PadExposeData *expose_data = to_expose->data;
2528 gst_object_unref (expose_data->pad);
2529 g_free (expose_data);
2530 to_expose = g_list_delete_link (to_expose, to_expose);
2533 remove_error_filter (dbin, element, &error_msg);
2536 gchar *error_string = error_message_to_string (error_msg);
2537 g_string_append_printf (error_details, "Couldn't set %s to PAUSED:\n%s",
2538 GST_ELEMENT_NAME (element), error_string);
2539 gst_message_unref (error_msg);
2540 g_free (error_string);
2542 g_string_append_printf (error_details, "Couldn't set %s to PAUSED",
2543 GST_ELEMENT_NAME (element));
2546 /* Remove all elements in this chain that were just added. No
2547 * other thread could've added elements in the meantime */
2548 CHAIN_MUTEX_LOCK (chain);
2552 dtmp = chain->elements->data;
2553 tmp = dtmp->element;
2555 /* Disconnect any signal handlers that might be connected
2556 * in connect_element() or analyze_pad() */
2557 if (dtmp->pad_added_id)
2558 g_signal_handler_disconnect (tmp, dtmp->pad_added_id);
2559 if (dtmp->pad_removed_id)
2560 g_signal_handler_disconnect (tmp, dtmp->pad_removed_id);
2561 if (dtmp->no_more_pads_id)
2562 g_signal_handler_disconnect (tmp, dtmp->no_more_pads_id);
2564 for (l = chain->pending_pads; l;) {
2565 GstPendingPad *pp = l->data;
2568 if (GST_PAD_PARENT (pp->pad) != tmp) {
2573 gst_pending_pad_free (pp);
2575 /* Remove element from the list, update list head and go to the
2576 * next element in the list */
2578 chain->pending_pads = g_list_delete_link (chain->pending_pads, l);
2582 if (dtmp->capsfilter) {
2583 gst_bin_remove (GST_BIN (dbin), dtmp->capsfilter);
2584 gst_element_set_state (dtmp->capsfilter, GST_STATE_NULL);
2585 gst_object_unref (dtmp->capsfilter);
2588 gst_bin_remove (GST_BIN (dbin), tmp);
2589 gst_element_set_state (tmp, GST_STATE_NULL);
2591 gst_object_unref (tmp);
2592 g_slice_free (GstDecodeElement, dtmp);
2594 chain->elements = g_list_delete_link (chain->elements, chain->elements);
2595 } while (tmp != element);
2596 CHAIN_MUTEX_UNLOCK (chain);
2600 /* Everything went well, the spice must flow now */
2601 GST_PAD_STREAM_UNLOCK (sinkpad);
2604 /* Remove error filter now, from now on we can't gracefully
2605 * handle errors of the element anymore */
2606 remove_error_filter (dbin, element, NULL);
2608 /* Now let the bin handle the state */
2609 gst_element_set_locked_state (element, FALSE);
2612 SUBTITLE_LOCK (dbin);
2613 /* we added the element now, add it to the list of subtitle-encoding
2614 * elements when we can set the property */
2615 dbin->subtitles = g_list_prepend (dbin->subtitles, element);
2616 SUBTITLE_UNLOCK (dbin);
2620 PadExposeData *expose_data = to_expose->data;
2623 ocaps = get_pad_caps (expose_data->pad);
2624 expose_pad (dbin, delem->element, expose_data->chain->current_pad,
2625 expose_data->pad, ocaps, expose_data->chain);
2628 gst_caps_unref (ocaps);
2630 gst_object_unref (expose_data->pad);
2631 g_free (expose_data);
2632 to_expose = g_list_delete_link (to_expose, to_expose);
2641 gst_object_unref (mqpad);
2644 *deadend_details = g_string_free (error_details, (error_details->len == 0
2647 *deadend_details = NULL;
2653 get_pad_caps (GstPad * pad)
2657 /* first check the pad caps, if this is set, we are positively sure it is
2658 * fixed and exactly what the element will produce. */
2659 caps = gst_pad_get_current_caps (pad);
2661 /* then use the getcaps function if we don't have caps. These caps might not
2662 * be fixed in some cases, in which case analyze_new_pad will set up a
2663 * notify::caps signal to continue autoplugging. */
2665 caps = gst_pad_query_caps (pad, NULL);
2670 /* Returns a list of pads that can be connected to already and
2671 * connects to pad-added and related signals */
2673 connect_element (GstDecodeBin * dbin, GstDecodeElement * delem,
2674 GstDecodeChain * chain)
2676 GstElement *element = delem->element;
2678 gboolean dynamic = FALSE;
2679 GList *to_connect = NULL;
2681 GST_DEBUG_OBJECT (dbin, "Attempting to connect element %s [chain:%p] further",
2682 GST_ELEMENT_NAME (element), chain);
2684 /* 1. Loop over pad templates, grabbing existing pads along the way */
2685 for (pads = GST_ELEMENT_GET_CLASS (element)->padtemplates; pads;
2686 pads = g_list_next (pads)) {
2687 GstPadTemplate *templ = GST_PAD_TEMPLATE (pads->data);
2688 const gchar *templ_name;
2690 /* we are only interested in source pads */
2691 if (GST_PAD_TEMPLATE_DIRECTION (templ) != GST_PAD_SRC)
2694 templ_name = GST_PAD_TEMPLATE_NAME_TEMPLATE (templ);
2695 GST_DEBUG_OBJECT (dbin, "got a source pad template %s", templ_name);
2697 /* figure out what kind of pad this is */
2698 switch (GST_PAD_TEMPLATE_PRESENCE (templ)) {
2699 case GST_PAD_ALWAYS:
2701 /* get the pad that we need to autoplug */
2702 GstPad *pad = gst_element_get_static_pad (element, templ_name);
2705 GST_DEBUG_OBJECT (dbin, "got the pad for always template %s",
2707 /* here is the pad, we need to autoplug it */
2708 to_connect = g_list_prepend (to_connect, pad);
2710 /* strange, pad is marked as always but it's not
2711 * there. Fix the element */
2712 GST_WARNING_OBJECT (dbin,
2713 "could not get the pad for always template %s", templ_name);
2717 case GST_PAD_SOMETIMES:
2719 /* try to get the pad to see if it is already created or
2721 GstPad *pad = gst_element_get_static_pad (element, templ_name);
2724 GST_DEBUG_OBJECT (dbin, "got the pad for sometimes template %s",
2726 /* the pad is created, we need to autoplug it */
2727 to_connect = g_list_prepend (to_connect, pad);
2729 GST_DEBUG_OBJECT (dbin,
2730 "did not get the sometimes pad of template %s", templ_name);
2731 /* we have an element that will create dynamic pads */
2736 case GST_PAD_REQUEST:
2737 /* ignore request pads */
2738 GST_DEBUG_OBJECT (dbin, "ignoring request padtemplate %s", templ_name);
2743 /* 2. if there are more potential pads, connect to relevant signals */
2745 GST_LOG_OBJECT (dbin, "Adding signals to element %s in chain %p",
2746 GST_ELEMENT_NAME (element), chain);
2747 delem->pad_added_id = g_signal_connect (element, "pad-added",
2748 G_CALLBACK (pad_added_cb), chain);
2749 delem->pad_removed_id = g_signal_connect (element, "pad-removed",
2750 G_CALLBACK (pad_removed_cb), chain);
2751 delem->no_more_pads_id = g_signal_connect (element, "no-more-pads",
2752 G_CALLBACK (no_more_pads_cb), chain);
2755 /* 3. return all pads that can be connected to already */
2762 * Expose the given pad on the chain as a decoded pad.
2765 expose_pad (GstDecodeBin * dbin, GstElement * src, GstDecodePad * dpad,
2766 GstPad * pad, GstCaps * caps, GstDecodeChain * chain)
2768 GstPad *mqpad = NULL;
2770 GST_DEBUG_OBJECT (dbin, "pad %s:%s, chain:%p",
2771 GST_DEBUG_PAD_NAME (pad), chain);
2773 /* If this is the first pad for this chain, there are no other elements
2774 * and the source element is not the multiqueue we must link through the
2777 * This is the case if a demuxer directly exposed a raw pad.
2779 if (chain->parent && !chain->elements && src != chain->parent->multiqueue) {
2780 GST_LOG_OBJECT (src, "connecting the pad through multiqueue");
2782 decode_pad_set_target (dpad, NULL);
2783 if (!(mqpad = gst_decode_group_control_demuxer_pad (chain->parent, pad)))
2786 decode_pad_set_target (dpad, pad);
2789 gst_decode_pad_activate (dpad, chain);
2790 chain->endpad = gst_object_ref (dpad);
2791 chain->endcaps = gst_caps_ref (caps);
2794 if (dbin->decode_chain) {
2795 if (gst_decode_chain_is_complete (dbin->decode_chain)) {
2796 gst_decode_bin_expose (dbin);
2799 EXPOSE_UNLOCK (dbin);
2802 gst_object_unref (mqpad);
2808 /* check_upstream_seekable:
2810 * Check if upstream is seekable.
2813 check_upstream_seekable (GstDecodeBin * dbin, GstPad * pad)
2816 gint64 start = -1, stop = -1;
2817 gboolean seekable = FALSE;
2819 query = gst_query_new_seeking (GST_FORMAT_BYTES);
2820 if (!gst_pad_peer_query (pad, query)) {
2821 GST_DEBUG_OBJECT (dbin, "seeking query failed");
2825 gst_query_parse_seeking (query, NULL, &seekable, &start, &stop);
2827 /* try harder to query upstream size if we didn't get it the first time */
2828 if (seekable && stop == -1) {
2829 GST_DEBUG_OBJECT (dbin, "doing duration query to fix up unset stop");
2830 gst_pad_peer_query_duration (pad, GST_FORMAT_BYTES, &stop);
2833 /* if upstream doesn't know the size, it's likely that it's not seekable in
2834 * practice even if it technically may be seekable */
2835 if (seekable && (start != 0 || stop <= start)) {
2836 GST_DEBUG_OBJECT (dbin, "seekable but unknown start/stop -> disable");
2839 GST_DEBUG_OBJECT (dbin, "upstream seekable: %d", seekable);
2843 gst_query_unref (query);
2848 type_found (GstElement * typefind, guint probability,
2849 GstCaps * caps, GstDecodeBin * decode_bin)
2851 GstPad *pad, *sink_pad;
2852 GstDecodeChain *chain;
2854 GST_DEBUG_OBJECT (decode_bin, "typefind found caps %" GST_PTR_FORMAT, caps);
2856 /* If the typefinder (but not something else) finds text/plain - i.e. that's
2857 * the top-level type of the file - then error out.
2859 if (gst_structure_has_name (gst_caps_get_structure (caps, 0), "text/plain")) {
2860 GST_ELEMENT_ERROR (decode_bin, STREAM, WRONG_TYPE,
2861 (_("This appears to be a text file")),
2862 ("decodebin cannot decode plain text files"));
2866 pad = gst_element_get_static_pad (typefind, "src");
2867 sink_pad = gst_element_get_static_pad (typefind, "sink");
2869 /* need some lock here to prevent race with shutdown state change
2870 * which might yank away e.g. decode_chain while building stuff here.
2871 * In typical cases, STREAM_LOCK is held and handles that, it need not
2872 * be held (if called from a proxied setcaps), so grab it anyway */
2873 GST_PAD_STREAM_LOCK (sink_pad);
2874 /* FIXME: we can only deal with one type, we don't yet support dynamically changing
2875 * caps from the typefind element */
2876 if (decode_bin->have_type || decode_bin->decode_chain) {
2878 decode_bin->have_type = TRUE;
2880 decode_bin->decode_chain = gst_decode_chain_new (decode_bin, NULL, pad);
2881 chain = gst_decode_chain_ref (decode_bin->decode_chain);
2883 if (analyze_new_pad (decode_bin, typefind, pad, caps,
2884 decode_bin->decode_chain, NULL))
2885 expose_pad (decode_bin, typefind, decode_bin->decode_chain->current_pad,
2886 pad, caps, decode_bin->decode_chain);
2888 gst_decode_chain_unref (chain);
2891 GST_PAD_STREAM_UNLOCK (sink_pad);
2892 gst_object_unref (sink_pad);
2893 gst_object_unref (pad);
2899 static GstPadProbeReturn
2900 pad_event_cb (GstPad * pad, GstPadProbeInfo * info, gpointer data)
2902 GstEvent *event = GST_PAD_PROBE_INFO_EVENT (info);
2903 GstPendingPad *ppad = (GstPendingPad *) data;
2904 GstDecodeChain *chain = ppad->chain;
2905 GstDecodeBin *dbin = chain->dbin;
2910 switch (GST_EVENT_TYPE (event)) {
2912 GST_DEBUG_OBJECT (pad, "Received EOS on a non final pad, this stream "
2914 chain->deadend = TRUE;
2915 chain->drained = TRUE;
2916 gst_object_replace ((GstObject **) & chain->current_pad, NULL);
2917 /* we don't set the endcaps because NULL endcaps means early EOS */
2920 if (dbin->decode_chain)
2921 if (gst_decode_chain_is_complete (dbin->decode_chain))
2922 gst_decode_bin_expose (dbin);
2923 EXPOSE_UNLOCK (dbin);
2928 return GST_PAD_PROBE_OK;
2932 pad_added_cb (GstElement * element, GstPad * pad, GstDecodeChain * chain)
2936 GstDecodeChain *new_chain;
2940 GST_DEBUG_OBJECT (pad, "pad added, chain:%p", chain);
2941 GST_PAD_STREAM_LOCK (pad);
2942 if (!gst_pad_is_active (pad)) {
2943 GST_PAD_STREAM_UNLOCK (pad);
2944 GST_DEBUG_OBJECT (pad, "Ignoring pad-added from a deactivated pad");
2948 caps = get_pad_caps (pad);
2949 if (analyze_new_pad (dbin, element, pad, caps, chain, &new_chain))
2950 expose_pad (dbin, element, new_chain->current_pad, pad, caps, new_chain);
2952 gst_caps_unref (caps);
2954 GST_PAD_STREAM_UNLOCK (pad);
2957 static GstPadProbeReturn
2958 sink_pad_event_probe (GstPad * pad, GstPadProbeInfo * info, gpointer user_data)
2960 GstDecodeGroup *group = (GstDecodeGroup *) user_data;
2961 GstEvent *event = GST_PAD_PROBE_INFO_EVENT (info);
2962 GstPad *peer = gst_pad_get_peer (pad);
2963 GstPadProbeReturn proberet = GST_PAD_PROBE_OK;
2965 GST_DEBUG_OBJECT (pad, "Got upstream event %s", GST_EVENT_TYPE_NAME (event));
2968 GST_DEBUG_OBJECT (pad, "We are unlinked !");
2969 if (group->parent && group->parent->next_groups) {
2970 GstDecodeGroup *last_group =
2971 g_list_last (group->parent->next_groups)->data;
2972 GST_DEBUG_OBJECT (pad, "We could send the event to another group (%p)",
2974 /* Grab another sinkpad for that last group through which we will forward this event */
2975 if (last_group->reqpads) {
2976 GstPad *sinkpad = (GstPad *) last_group->reqpads->data;
2977 GstPad *otherpeer = gst_pad_get_peer (sinkpad);
2979 GST_DEBUG_OBJECT (otherpeer, "Attempting to forward event");
2980 if (gst_pad_send_event (otherpeer, gst_event_ref (event))) {
2981 gst_event_unref (event);
2982 proberet = GST_PAD_PROBE_HANDLED;
2984 gst_object_unref (otherpeer);
2987 GST_DEBUG_OBJECT (pad, "No request pads, can't forward event");
2991 gst_object_unref (peer);
2997 static GstPadProbeReturn
2998 sink_pad_query_probe (GstPad * pad, GstPadProbeInfo * info, gpointer user_data)
3000 GstDecodeGroup *group = (GstDecodeGroup *) user_data;
3001 GstPad *peer = gst_pad_get_peer (pad);
3002 GstQuery *query = GST_PAD_PROBE_INFO_QUERY (info);
3003 GstPadProbeReturn proberet = GST_PAD_PROBE_OK;
3005 GST_DEBUG_OBJECT (pad, "Got upstream query %s", GST_QUERY_TYPE_NAME (query));
3008 GST_DEBUG_OBJECT (pad, "We are unlinked !");
3009 if (group->parent && group->parent->next_groups) {
3010 GstDecodeGroup *last_group =
3011 g_list_last (group->parent->next_groups)->data;
3012 GST_DEBUG_OBJECT (pad, "We could send the query to another group");
3013 /* Grab another sinkpad for that last group through which we will forward this event */
3014 if (last_group->reqpads) {
3015 GstPad *sinkpad = (GstPad *) last_group->reqpads->data;
3016 GstPad *otherpeer = gst_pad_get_peer (sinkpad);
3018 GST_DEBUG_OBJECT (otherpeer, "Attempting to forward query");
3019 if (gst_pad_query (otherpeer, query)) {
3020 proberet = GST_PAD_PROBE_HANDLED;
3022 GST_DEBUG ("FAILURE");
3023 gst_object_unref (otherpeer);
3025 GST_DEBUG_OBJECT (sinkpad, "request pad not connected ??");
3027 GST_DEBUG_OBJECT (pad, "No request pads ???");
3030 gst_object_unref (peer);
3036 pad_removed_cb (GstElement * element, GstPad * pad, GstDecodeChain * chain)
3040 GST_LOG_OBJECT (pad, "pad removed, chain:%p", chain);
3042 /* In fact, we don't have to do anything here, the active group will be
3043 * removed when the group's multiqueue is drained */
3044 CHAIN_MUTEX_LOCK (chain);
3045 for (l = chain->pending_pads; l; l = l->next) {
3046 GstPendingPad *ppad = l->data;
3047 GstPad *opad = ppad->pad;
3050 gst_pending_pad_free (ppad);
3051 chain->pending_pads = g_list_delete_link (chain->pending_pads, l);
3055 CHAIN_MUTEX_UNLOCK (chain);
3059 no_more_pads_cb (GstElement * element, GstDecodeChain * chain)
3061 GstDecodeGroup *group = NULL;
3063 GST_LOG_OBJECT (element, "got no more pads");
3065 CHAIN_MUTEX_LOCK (chain);
3066 if (!chain->elements
3067 || ((GstDecodeElement *) chain->elements->data)->element != element) {
3068 GST_LOG_OBJECT (chain->dbin, "no-more-pads from old chain element '%s'",
3069 GST_OBJECT_NAME (element));
3070 CHAIN_MUTEX_UNLOCK (chain);
3072 } else if (!chain->demuxer) {
3073 GST_LOG_OBJECT (chain->dbin, "no-more-pads from a non-demuxer element '%s'",
3074 GST_OBJECT_NAME (element));
3075 CHAIN_MUTEX_UNLOCK (chain);
3079 /* when we received no_more_pads, we can complete the pads of the chain */
3080 if (!chain->next_groups && chain->active_group) {
3081 group = chain->active_group;
3082 } else if (chain->next_groups) {
3084 for (iter = chain->next_groups; iter; iter = g_list_next (iter)) {
3086 if (!group->no_more_pads)
3091 GST_ERROR_OBJECT (chain->dbin, "can't find group for element");
3092 CHAIN_MUTEX_UNLOCK (chain);
3096 GST_DEBUG_OBJECT (element, "Setting group %p to complete", group);
3098 group->no_more_pads = TRUE;
3099 /* this group has prerolled enough to not need more pads,
3100 * we can probably set its buffering state to playing now */
3101 GST_DEBUG_OBJECT (group->dbin, "Setting group %p multiqueue to "
3102 "'playing' buffering mode", group);
3103 decodebin_set_queue_size (group->dbin, group->multiqueue, FALSE,
3104 (group->parent ? group->parent->seekable : TRUE));
3105 CHAIN_MUTEX_UNLOCK (chain);
3107 EXPOSE_LOCK (chain->dbin);
3108 if (chain->dbin->decode_chain) {
3109 if (gst_decode_chain_is_complete (chain->dbin->decode_chain)) {
3110 gst_decode_bin_expose (chain->dbin);
3113 EXPOSE_UNLOCK (chain->dbin);
3117 caps_notify_cb (GstPad * pad, GParamSpec * unused, GstDecodeChain * chain)
3119 GstElement *element;
3122 GST_LOG_OBJECT (pad, "Notified caps for pad %s:%s", GST_DEBUG_PAD_NAME (pad));
3124 /* Disconnect this; if we still need it, we'll reconnect to this in
3125 * analyze_new_pad */
3126 element = GST_ELEMENT_CAST (gst_pad_get_parent (pad));
3128 CHAIN_MUTEX_LOCK (chain);
3129 for (l = chain->pending_pads; l; l = l->next) {
3130 GstPendingPad *ppad = l->data;
3131 if (ppad->pad == pad) {
3132 gst_pending_pad_free (ppad);
3133 chain->pending_pads = g_list_delete_link (chain->pending_pads, l);
3137 CHAIN_MUTEX_UNLOCK (chain);
3139 pad_added_cb (element, pad, chain);
3141 gst_object_unref (element);
3144 /* Decide whether an element is a demuxer based on the
3145 * klass and number/type of src pad templates it has */
3147 is_demuxer_element (GstElement * srcelement)
3149 GstElementFactory *srcfactory;
3150 GstElementClass *elemclass;
3153 gint potential_src_pads = 0;
3155 srcfactory = gst_element_get_factory (srcelement);
3157 gst_element_factory_get_metadata (srcfactory, GST_ELEMENT_METADATA_KLASS);
3159 /* Can't be a demuxer unless it has Demux in the klass name */
3160 if (!strstr (klass, "Demux"))
3163 /* Walk the src pad templates and count how many the element
3165 elemclass = GST_ELEMENT_GET_CLASS (srcelement);
3167 walk = gst_element_class_get_pad_template_list (elemclass);
3168 while (walk != NULL) {
3169 GstPadTemplate *templ;
3171 templ = (GstPadTemplate *) walk->data;
3172 if (GST_PAD_TEMPLATE_DIRECTION (templ) == GST_PAD_SRC) {
3173 switch (GST_PAD_TEMPLATE_PRESENCE (templ)) {
3174 case GST_PAD_ALWAYS:
3175 case GST_PAD_SOMETIMES:
3176 if (strstr (GST_PAD_TEMPLATE_NAME_TEMPLATE (templ), "%"))
3177 potential_src_pads += 2; /* Might make multiple pads */
3179 potential_src_pads += 1;
3181 case GST_PAD_REQUEST:
3182 potential_src_pads += 2;
3186 walk = g_list_next (walk);
3189 if (potential_src_pads < 2)
3196 is_adaptive_demuxer_element (GstElement * srcelement)
3198 GstElementFactory *srcfactory;
3201 srcfactory = gst_element_get_factory (srcelement);
3203 gst_element_factory_get_metadata (srcfactory, GST_ELEMENT_METADATA_KLASS);
3205 /* Can't be a demuxer unless it has Demux in the klass name */
3206 if (!strstr (klass, "Demux") || !strstr (klass, "Adaptive"))
3212 /* Returns TRUE if the caps are compatible with the caps specified in the 'caps'
3213 * property (which by default are the raw caps)
3215 * The decodebin_lock should be taken !
3218 are_final_caps (GstDecodeBin * dbin, GstCaps * caps)
3222 GST_LOG_OBJECT (dbin, "Checking with caps %" GST_PTR_FORMAT, caps);
3224 /* lock for getting the caps */
3225 GST_OBJECT_LOCK (dbin);
3226 res = gst_caps_is_subset (caps, dbin->caps);
3227 GST_OBJECT_UNLOCK (dbin);
3229 GST_LOG_OBJECT (dbin, "Caps are %sfinal caps", res ? "" : "not ");
3234 /* gst_decode_bin_reset_buffering:
3236 * Enables buffering on the last multiqueue of each group only,
3237 * disabling the rest
3241 gst_decode_bin_reset_buffering (GstDecodeBin * dbin)
3243 if (!dbin->use_buffering)
3246 GST_DEBUG_OBJECT (dbin, "Reseting multiqueues buffering");
3247 if (dbin->decode_chain) {
3248 CHAIN_MUTEX_LOCK (dbin->decode_chain);
3249 gst_decode_chain_reset_buffering (dbin->decode_chain);
3250 CHAIN_MUTEX_UNLOCK (dbin->decode_chain);
3255 * GstDecodeChain functions
3259 gst_decode_chain_reset_buffering (GstDecodeChain * chain)
3261 GstDecodeGroup *group;
3263 group = chain->active_group;
3264 GST_LOG_OBJECT (chain->dbin, "Resetting chain %p buffering, active group: %p",
3267 return gst_decode_group_reset_buffering (group);
3272 /* gst_decode_chain_get_current_group:
3274 * Returns the current group of this chain, to which
3275 * new chains should be attached or NULL if the last
3276 * group didn't have no-more-pads.
3278 * Not MT-safe: Call with parent chain lock!
3280 static GstDecodeGroup *
3281 gst_decode_chain_get_current_group (GstDecodeChain * chain)
3283 GstDecodeGroup *group;
3285 if (!chain->next_groups && chain->active_group
3286 && chain->active_group->overrun && !chain->active_group->no_more_pads) {
3287 GST_WARNING_OBJECT (chain->dbin,
3288 "Currently active group %p is exposed"
3289 " and wants to add a new pad without having signaled no-more-pads",
3290 chain->active_group);
3294 if (chain->next_groups && (group = chain->next_groups->data) && group->overrun
3295 && !group->no_more_pads) {
3296 GST_WARNING_OBJECT (chain->dbin,
3297 "Currently newest pending group %p "
3298 "had overflow but didn't signal no-more-pads", group);
3302 /* Now we know that we can really return something useful */
3303 if (!chain->active_group) {
3304 chain->active_group = group = gst_decode_group_new (chain->dbin, chain);
3305 } else if (!chain->active_group->overrun
3306 && !chain->active_group->no_more_pads) {
3307 group = chain->active_group;
3311 for (iter = chain->next_groups; iter; iter = g_list_next (iter)) {
3312 GstDecodeGroup *next_group = iter->data;
3314 if (!next_group->overrun && !next_group->no_more_pads) {
3321 group = gst_decode_group_new (chain->dbin, chain);
3322 chain->next_groups = g_list_append (chain->next_groups, group);
3328 static void gst_decode_group_free_internal (GstDecodeGroup * group,
3332 gst_decode_chain_unref (GstDecodeChain * chain)
3334 if (g_atomic_int_dec_and_test (&chain->refs)) {
3335 g_mutex_clear (&chain->lock);
3336 g_slice_free (GstDecodeChain, chain);
3340 static GstDecodeChain *
3341 gst_decode_chain_ref (GstDecodeChain * chain)
3343 g_atomic_int_inc (&chain->refs);
3348 gst_decode_chain_free_internal (GstDecodeChain * chain, gboolean hide)
3350 GList *l, *set_to_null = NULL;
3352 CHAIN_MUTEX_LOCK (chain);
3354 GST_DEBUG_OBJECT (chain->dbin, "%s chain %p", (hide ? "Hiding" : "Freeing"),
3357 if (chain->active_group) {
3358 gst_decode_group_free_internal (chain->active_group, hide);
3360 chain->active_group = NULL;
3363 for (l = chain->next_groups; l; l = l->next) {
3364 gst_decode_group_free_internal ((GstDecodeGroup *) l->data, hide);
3369 g_list_free (chain->next_groups);
3370 chain->next_groups = NULL;
3374 for (l = chain->old_groups; l; l = l->next) {
3375 GstDecodeGroup *group = l->data;
3377 gst_decode_group_free (group);
3379 g_list_free (chain->old_groups);
3380 chain->old_groups = NULL;
3383 for (l = chain->pending_pads; l; l = l->next) {
3384 GstPendingPad *ppad = l->data;
3385 gst_pending_pad_free (ppad);
3388 g_list_free (chain->pending_pads);
3389 chain->pending_pads = NULL;
3391 for (l = chain->elements; l; l = l->next) {
3392 GstDecodeElement *delem = l->data;
3393 GstElement *element = delem->element;
3395 if (delem->pad_added_id)
3396 g_signal_handler_disconnect (element, delem->pad_added_id);
3397 delem->pad_added_id = 0;
3398 if (delem->pad_removed_id)
3399 g_signal_handler_disconnect (element, delem->pad_removed_id);
3400 delem->pad_removed_id = 0;
3401 if (delem->no_more_pads_id)
3402 g_signal_handler_disconnect (element, delem->no_more_pads_id);
3403 delem->no_more_pads_id = 0;
3405 if (delem->capsfilter) {
3406 if (GST_OBJECT_PARENT (delem->capsfilter) ==
3407 GST_OBJECT_CAST (chain->dbin))
3408 gst_bin_remove (GST_BIN_CAST (chain->dbin), delem->capsfilter);
3411 g_list_append (set_to_null, gst_object_ref (delem->capsfilter));
3415 if (GST_OBJECT_PARENT (element) == GST_OBJECT_CAST (chain->dbin))
3416 gst_bin_remove (GST_BIN_CAST (chain->dbin), element);
3418 set_to_null = g_list_append (set_to_null, gst_object_ref (element));
3421 SUBTITLE_LOCK (chain->dbin);
3422 /* remove possible subtitle element */
3423 chain->dbin->subtitles = g_list_remove (chain->dbin->subtitles, element);
3424 SUBTITLE_UNLOCK (chain->dbin);
3427 if (delem->capsfilter) {
3428 gst_object_unref (delem->capsfilter);
3429 delem->capsfilter = NULL;
3432 gst_object_unref (element);
3435 g_slice_free (GstDecodeElement, delem);
3439 g_list_free (chain->elements);
3440 chain->elements = NULL;
3443 if (chain->endpad) {
3444 if (chain->endpad->exposed) {
3445 gst_element_remove_pad (GST_ELEMENT_CAST (chain->dbin),
3446 GST_PAD_CAST (chain->endpad));
3449 decode_pad_set_target (chain->endpad, NULL);
3450 chain->endpad->exposed = FALSE;
3452 gst_object_unref (chain->endpad);
3453 chain->endpad = NULL;
3457 if (!hide && chain->current_pad) {
3458 gst_object_unref (chain->current_pad);
3459 chain->current_pad = NULL;
3463 gst_object_unref (chain->pad);
3467 if (chain->endcaps) {
3468 gst_caps_unref (chain->endcaps);
3469 chain->endcaps = NULL;
3471 g_free (chain->deadend_details);
3472 chain->deadend_details = NULL;
3474 GST_DEBUG_OBJECT (chain->dbin, "%s chain %p", (hide ? "Hidden" : "Freed"),
3476 CHAIN_MUTEX_UNLOCK (chain);
3478 while (set_to_null) {
3479 GstElement *element = set_to_null->data;
3480 set_to_null = g_list_delete_link (set_to_null, set_to_null);
3481 gst_element_set_state (element, GST_STATE_NULL);
3482 gst_object_unref (element);
3486 gst_decode_chain_unref (chain);
3489 /* gst_decode_chain_free:
3491 * Completely frees and removes the chain and all
3492 * child groups from decodebin.
3494 * MT-safe, don't hold the chain lock or any child chain's lock
3495 * when calling this!
3498 gst_decode_chain_free (GstDecodeChain * chain)
3500 gst_decode_chain_free_internal (chain, FALSE);
3503 /* gst_decode_chain_new:
3505 * Creates a new decode chain and initializes it.
3507 * It's up to the caller to add it to the list of child chains of
3510 static GstDecodeChain *
3511 gst_decode_chain_new (GstDecodeBin * dbin, GstDecodeGroup * parent,
3514 GstDecodeChain *chain = g_slice_new0 (GstDecodeChain);
3516 GST_DEBUG_OBJECT (dbin, "Creating new chain %p with parent group %p", chain,
3520 chain->parent = parent;
3522 g_mutex_init (&chain->lock);
3523 chain->pad = gst_object_ref (pad);
3529 * GstDecodeGroup functions
3532 /* The overrun callback is used to expose groups that have not yet had their
3533 * no_more_pads called while the (large) multiqueue overflowed. When this
3534 * happens we must assume that the no_more_pads will not arrive anymore and we
3535 * must expose the pads that we have.
3538 multi_queue_overrun_cb (GstElement * queue, GstDecodeGroup * group)
3544 GST_LOG_OBJECT (dbin, "multiqueue '%s' (%p) is full", GST_OBJECT_NAME (queue),
3547 group->overrun = TRUE;
3548 /* this group has prerolled enough to not need more pads,
3549 * we can probably set its buffering state to playing now */
3550 GST_DEBUG_OBJECT (group->dbin, "Setting group %p multiqueue to "
3551 "'playing' buffering mode", group);
3552 decodebin_set_queue_size (group->dbin, group->multiqueue, FALSE,
3553 (group->parent ? group->parent->seekable : TRUE));
3555 /* FIXME: We should make sure that everything gets exposed now
3556 * even if child chains are not complete because the will never
3557 * be complete! Ignore any non-complete chains when exposing
3558 * and never expose them later
3562 if (dbin->decode_chain) {
3563 if (gst_decode_chain_is_complete (dbin->decode_chain)) {
3564 if (!gst_decode_bin_expose (dbin))
3565 GST_WARNING_OBJECT (dbin, "Couldn't expose group");
3568 EXPOSE_UNLOCK (dbin);
3572 gst_decode_group_free_internal (GstDecodeGroup * group, gboolean hide)
3576 GST_DEBUG_OBJECT (group->dbin, "%s group %p", (hide ? "Hiding" : "Freeing"),
3580 for (l = group->demuxer_pad_probe_ids; l != NULL; l = l->next) {
3581 GstDemuxerPad *demuxer_pad = l->data;
3582 GstPad *sinkpad = g_weak_ref_get (&demuxer_pad->weakPad);
3584 if (sinkpad != NULL) {
3585 gst_pad_remove_probe (sinkpad, demuxer_pad->event_probe_id);
3586 gst_pad_remove_probe (sinkpad, demuxer_pad->query_probe_id);
3587 g_weak_ref_clear (&demuxer_pad->weakPad);
3588 gst_object_unref (sinkpad);
3590 g_free (demuxer_pad);
3592 g_list_free (group->demuxer_pad_probe_ids);
3593 group->demuxer_pad_probe_ids = NULL;
3596 for (l = group->children; l; l = l->next) {
3597 GstDecodeChain *chain = (GstDecodeChain *) l->data;
3599 gst_decode_chain_free_internal (chain, hide);
3604 g_list_free (group->children);
3605 group->children = NULL;
3609 for (l = group->reqpads; l; l = l->next) {
3610 GstPad *pad = l->data;
3612 gst_element_release_request_pad (group->multiqueue, pad);
3613 gst_object_unref (pad);
3616 g_list_free (group->reqpads);
3617 group->reqpads = NULL;
3620 if (group->multiqueue) {
3621 if (group->overrunsig) {
3622 g_signal_handler_disconnect (group->multiqueue, group->overrunsig);
3623 group->overrunsig = 0;
3626 if (GST_OBJECT_PARENT (group->multiqueue) == GST_OBJECT_CAST (group->dbin))
3627 gst_bin_remove (GST_BIN_CAST (group->dbin), group->multiqueue);
3629 gst_element_set_state (group->multiqueue, GST_STATE_NULL);
3630 gst_object_unref (group->multiqueue);
3631 group->multiqueue = NULL;
3635 GST_DEBUG_OBJECT (group->dbin, "%s group %p", (hide ? "Hid" : "Freed"),
3638 g_slice_free (GstDecodeGroup, group);
3641 /* gst_decode_group_free:
3643 * Completely frees and removes the decode group and all
3646 * Never call this from any streaming thread!
3648 * Not MT-safe, call with parent's chain lock!
3651 gst_decode_group_free (GstDecodeGroup * group)
3653 gst_decode_group_free_internal (group, FALSE);
3656 /* gst_decode_group_hide:
3658 * Hide the decode group only, this means that
3659 * all child endpads are removed from decodebin
3660 * and all signals are unconnected.
3662 * No element is set to NULL state and completely
3665 * Can be called from streaming threads.
3667 * Not MT-safe, call with parent's chain lock!
3670 gst_decode_group_hide (GstDecodeGroup * group)
3672 gst_decode_group_free_internal (group, TRUE);
3675 /* gst_decode_chain_free_hidden_groups:
3677 * Frees any decode groups that were hidden previously.
3678 * This allows keeping memory use from ballooning when
3679 * switching chains repeatedly.
3681 * A new throwaway thread will be created to free the
3682 * groups, so any delay does not block the setup of a
3685 * Not MT-safe, call with parent's chain lock!
3688 gst_decode_chain_free_hidden_groups (GList * old_groups)
3692 for (l = old_groups; l; l = l->next) {
3693 GstDecodeGroup *group = l->data;
3695 gst_decode_group_free (group);
3697 g_list_free (old_groups);
3701 gst_decode_chain_start_free_hidden_groups_thread (GstDecodeChain * chain)
3704 GError *error = NULL;
3706 GstDecodeBin *dbin = chain->dbin;
3708 old_groups = chain->old_groups;
3712 /* If we already have a thread running, wait for it to finish */
3713 g_mutex_lock (&dbin->cleanup_lock);
3714 if (dbin->cleanup_thread) {
3715 g_thread_join (dbin->cleanup_thread);
3716 dbin->cleanup_thread = NULL;
3719 chain->old_groups = NULL;
3721 if (dbin->shutdown) {
3722 /* If we're shutting down, add the groups to be cleaned up in the
3723 * state change handler (which *is* another thread). Also avoids
3724 * playing racy games with the state change handler */
3725 dbin->cleanup_groups = g_list_concat (dbin->cleanup_groups, old_groups);
3726 g_mutex_unlock (&dbin->cleanup_lock);
3730 thread = g_thread_try_new ("free-hidden-groups",
3731 (GThreadFunc) gst_decode_chain_free_hidden_groups, old_groups, &error);
3732 if (!thread || error) {
3733 GST_ERROR ("Failed to start free-hidden-groups thread: %s",
3734 error ? error->message : "unknown reason");
3735 g_clear_error (&error);
3736 chain->old_groups = old_groups;
3737 g_mutex_unlock (&dbin->cleanup_lock);
3741 dbin->cleanup_thread = thread;
3742 g_mutex_unlock (&dbin->cleanup_lock);
3744 GST_DEBUG_OBJECT (chain->dbin, "Started free-hidden-groups thread");
3748 decodebin_set_queue_size (GstDecodeBin * dbin, GstElement * multiqueue,
3749 gboolean preroll, gboolean seekable)
3751 gboolean use_buffering;
3753 /* get the current config from the multiqueue */
3754 g_object_get (multiqueue, "use-buffering", &use_buffering, NULL);
3756 decodebin_set_queue_size_full (dbin, multiqueue, use_buffering, preroll,
3760 /* configure queue sizes, this depends on the buffering method and if we are
3761 * playing or prerolling. */
3763 decodebin_set_queue_size_full (GstDecodeBin * dbin, GstElement * multiqueue,
3764 gboolean use_buffering, gboolean preroll, gboolean seekable)
3766 guint max_bytes, max_buffers;
3769 GST_DEBUG_OBJECT (multiqueue, "use buffering %d", use_buffering);
3771 if (preroll || use_buffering) {
3772 /* takes queue limits, initially we only queue up up to the max bytes limit,
3773 * with a default of 2MB. we use the same values for buffering mode. */
3774 if (preroll || (max_bytes = dbin->max_size_bytes) == 0)
3775 max_bytes = AUTO_PREROLL_SIZE_BYTES;
3776 if (preroll || (max_buffers = dbin->max_size_buffers) == 0)
3777 max_buffers = AUTO_PREROLL_SIZE_BUFFERS;
3778 if (preroll || (max_time = dbin->max_size_time) == 0) {
3779 if (dbin->use_buffering && !preroll)
3780 max_time = 5 * GST_SECOND;
3782 max_time = seekable ? AUTO_PREROLL_SEEKABLE_SIZE_TIME :
3783 AUTO_PREROLL_NOT_SEEKABLE_SIZE_TIME;
3786 /* update runtime limits. At runtime, we try to keep the amount of buffers
3787 * in the queues as low as possible (but at least 5 buffers). */
3788 if (dbin->use_buffering)
3790 else if ((max_bytes = dbin->max_size_bytes) == 0)
3791 max_bytes = AUTO_PLAY_SIZE_BYTES;
3792 if ((max_buffers = dbin->max_size_buffers) == 0)
3793 max_buffers = AUTO_PLAY_SIZE_BUFFERS;
3794 /* this is a multiqueue with disabled buffering, don't limit max_time */
3795 if (dbin->use_buffering)
3797 else if ((max_time = dbin->max_size_time) == 0)
3798 max_time = AUTO_PLAY_SIZE_TIME;
3801 GST_DEBUG_OBJECT (multiqueue, "setting limits %u bytes, %u buffers, "
3802 "%" G_GUINT64_FORMAT " time", max_bytes, max_buffers, max_time);
3803 g_object_set (multiqueue,
3804 "max-size-bytes", max_bytes, "max-size-time", max_time,
3805 "max-size-buffers", max_buffers, NULL);
3808 /* gst_decode_group_new:
3809 * @dbin: Parent decodebin
3810 * @parent: Parent chain or %NULL
3812 * Creates a new GstDecodeGroup. It is up to the caller to add it to the list
3815 static GstDecodeGroup *
3816 gst_decode_group_new (GstDecodeBin * dbin, GstDecodeChain * parent)
3818 GstDecodeGroup *group = g_slice_new0 (GstDecodeGroup);
3822 GST_DEBUG_OBJECT (dbin, "Creating new group %p with parent chain %p", group,
3826 group->parent = parent;
3828 mq = group->multiqueue = gst_element_factory_make ("multiqueue", NULL);
3829 if (G_UNLIKELY (!group->multiqueue))
3830 goto missing_multiqueue;
3832 /* configure queue sizes for preroll */
3834 if (parent && parent->demuxer) {
3835 GstElement *element =
3836 ((GstDecodeElement *) parent->elements->data)->element;
3837 GstPad *pad = gst_element_get_static_pad (element, "sink");
3839 seekable = parent->seekable = check_upstream_seekable (dbin, pad);
3840 gst_object_unref (pad);
3843 decodebin_set_queue_size_full (dbin, mq, FALSE, TRUE, seekable);
3845 group->overrunsig = g_signal_connect (mq, "overrun",
3846 G_CALLBACK (multi_queue_overrun_cb), group);
3847 group->demuxer_pad_probe_ids = NULL;
3849 gst_element_set_state (mq, GST_STATE_PAUSED);
3850 gst_bin_add (GST_BIN (dbin), gst_object_ref (mq));
3857 gst_element_post_message (GST_ELEMENT_CAST (dbin),
3858 gst_missing_element_message_new (GST_ELEMENT_CAST (dbin),
3860 GST_ELEMENT_ERROR (dbin, CORE, MISSING_PLUGIN, (NULL), ("no multiqueue!"));
3861 g_slice_free (GstDecodeGroup, group);
3866 /* gst_decode_group_control_demuxer_pad
3868 * Adds a new demuxer srcpad to the given group.
3870 * Returns the srcpad of the multiqueue corresponding the given pad.
3871 * Returns NULL if there was an error.
3874 gst_decode_group_control_demuxer_pad (GstDecodeGroup * group, GstPad * pad)
3877 GstDemuxerPad *demuxer_pad;
3878 GstPad *srcpad, *sinkpad;
3879 GstIterator *it = NULL;
3880 GValue item = { 0, };
3884 GST_LOG_OBJECT (dbin, "group:%p pad %s:%s", group, GST_DEBUG_PAD_NAME (pad));
3888 if (G_UNLIKELY (!group->multiqueue))
3891 if (!(sinkpad = gst_element_get_request_pad (group->multiqueue, "sink_%u"))) {
3892 GST_ERROR_OBJECT (dbin, "Couldn't get sinkpad from multiqueue");
3896 if ((gst_pad_link_full (pad, sinkpad,
3897 GST_PAD_LINK_CHECK_NOTHING) != GST_PAD_LINK_OK)) {
3898 GST_ERROR_OBJECT (dbin, "Couldn't link demuxer and multiqueue");
3902 it = gst_pad_iterate_internal_links (sinkpad);
3904 if (!it || (gst_iterator_next (it, &item)) != GST_ITERATOR_OK
3905 || ((srcpad = g_value_dup_object (&item)) == NULL)) {
3906 GST_ERROR_OBJECT (dbin,
3907 "Couldn't get srcpad from multiqueue for sinkpad %" GST_PTR_FORMAT,
3912 CHAIN_MUTEX_LOCK (group->parent);
3914 /* Note: GWeakRefs can't be moved in memory once they're in use, so do a
3915 * dedicated alloc for the GstDemuxerPad struct that contains it */
3916 demuxer_pad = g_new0 (GstDemuxerPad, 1);
3917 demuxer_pad->event_probe_id = gst_pad_add_probe (sinkpad,
3918 GST_PAD_PROBE_TYPE_EVENT_UPSTREAM, sink_pad_event_probe, group, NULL);
3919 demuxer_pad->query_probe_id = gst_pad_add_probe (sinkpad,
3920 GST_PAD_PROBE_TYPE_QUERY_UPSTREAM, sink_pad_query_probe, group, NULL);
3922 g_weak_ref_set (&demuxer_pad->weakPad, sinkpad);
3923 group->demuxer_pad_probe_ids =
3924 g_list_prepend (group->demuxer_pad_probe_ids, demuxer_pad);
3926 group->reqpads = g_list_prepend (group->reqpads, gst_object_ref (sinkpad));
3927 CHAIN_MUTEX_UNLOCK (group->parent);
3930 if (G_IS_VALUE (&item))
3931 g_value_unset (&item);
3933 gst_iterator_free (it);
3934 gst_object_unref (sinkpad);
3938 gst_element_release_request_pad (group->multiqueue, sinkpad);
3942 /* gst_decode_group_is_complete:
3944 * Checks if the group is complete, this means that
3945 * a) overrun of the multiqueue or no-more-pads happened
3946 * b) all child chains are complete
3948 * Not MT-safe, always call with decodebin expose lock
3951 gst_decode_group_is_complete (GstDecodeGroup * group)
3954 gboolean complete = TRUE;
3956 if (!group->overrun && !group->no_more_pads) {
3961 for (l = group->children; l; l = l->next) {
3962 GstDecodeChain *chain = l->data;
3964 if (!gst_decode_chain_is_complete (chain)) {
3971 GST_DEBUG_OBJECT (group->dbin, "Group %p is complete: %d", group, complete);
3975 /* gst_decode_chain_is_complete:
3977 * Returns TRUE if the chain is complete, this means either
3978 * a) This chain is a dead end, i.e. we have no suitable plugins
3979 * b) This chain ends in an endpad and this is blocked or exposed
3981 * Not MT-safe, always call with decodebin expose lock
3984 gst_decode_chain_is_complete (GstDecodeChain * chain)
3986 gboolean complete = FALSE;
3988 CHAIN_MUTEX_LOCK (chain);
3989 if (chain->dbin->shutdown)
3992 if (chain->deadend) {
3997 if (chain->endpad && gst_decode_pad_is_exposable (chain->endpad)) {
4002 if (chain->demuxer) {
4003 if (chain->active_group
4004 && gst_decode_group_is_complete (chain->active_group)) {
4011 CHAIN_MUTEX_UNLOCK (chain);
4012 GST_DEBUG_OBJECT (chain->dbin, "Chain %p is complete: %d", chain, complete);
4016 /* Flushing group/chains */
4018 flush_group (GstDecodeGroup * group, gboolean flushing)
4022 GST_DEBUG ("group %p flushing:%d", group, flushing);
4024 if (group->drained == flushing)
4026 for (tmp = group->children; tmp; tmp = tmp->next) {
4027 GstDecodeChain *chain = (GstDecodeChain *) tmp->data;
4028 flush_chain (chain, flushing);
4030 GST_DEBUG ("Setting group %p to drained:%d", group, flushing);
4031 group->drained = flushing;
4035 flush_chain (GstDecodeChain * chain, gboolean flushing)
4038 GstDecodeBin *dbin = chain->dbin;
4040 GST_DEBUG_OBJECT (dbin, "chain %p (pad %s:%s) flushing:%d", chain,
4041 GST_DEBUG_PAD_NAME (chain->pad), flushing);
4042 if (chain->drained == flushing)
4044 /* if unflushing, check if we should switch to last group */
4045 if (flushing == FALSE && chain->next_groups) {
4046 GstDecodeGroup *target_group =
4047 (GstDecodeGroup *) g_list_last (chain->next_groups)->data;
4048 gst_decode_chain_start_free_hidden_groups_thread (chain);
4049 /* Hide active group (we're sure it's not that one we'll be using) */
4050 GST_DEBUG_OBJECT (dbin, "Switching from active group %p to group %p",
4051 chain->active_group, target_group);
4052 gst_decode_group_hide (chain->active_group);
4053 chain->old_groups = g_list_prepend (chain->old_groups, chain->active_group);
4054 chain->active_group = target_group;
4055 /* Hide all groups but the target_group */
4056 for (tmp = chain->next_groups; tmp; tmp = tmp->next) {
4057 GstDecodeGroup *group = (GstDecodeGroup *) tmp->data;
4058 if (group != target_group) {
4059 gst_decode_group_hide (group);
4060 chain->old_groups = g_list_prepend (chain->old_groups, group);
4063 /* Clear next groups */
4064 g_list_free (chain->next_groups);
4065 chain->next_groups = NULL;
4067 /* Mark all groups as flushing */
4068 if (chain->active_group)
4069 flush_group (chain->active_group, flushing);
4070 for (tmp = chain->next_groups; tmp; tmp = tmp->next) {
4071 GstDecodeGroup *group = (GstDecodeGroup *) tmp->data;
4072 flush_group (group, flushing);
4074 GST_DEBUG ("Setting chain %p to drained:%d", chain, flushing);
4075 chain->drained = flushing;
4079 drain_and_switch_chains (GstDecodeChain * chain, GstDecodePad * drainpad,
4080 gboolean * last_group, gboolean * drained, gboolean * switched);
4081 /* drain_and_switch_chains/groups:
4083 * CALL WITH CHAIN LOCK (or group parent) TAKEN !
4085 * Goes down the chains/groups until it finds the chain
4086 * to which the drainpad belongs.
4088 * It marks that pad/chain as drained and then will figure
4089 * out which group to switch to or not.
4091 * last_chain will be set to TRUE if the group to which the
4092 * pad belongs is the last one.
4094 * drained will be set to TRUE if the chain/group is drained.
4096 * Returns: TRUE if the chain contained the target pad */
4098 drain_and_switch_group (GstDecodeGroup * group, GstDecodePad * drainpad,
4099 gboolean * last_group, gboolean * drained, gboolean * switched)
4101 gboolean handled = FALSE;
4104 GST_DEBUG ("Checking group %p (target pad %s:%s)",
4105 group, GST_DEBUG_PAD_NAME (drainpad));
4107 /* Definitely can't be in drained groups */
4108 if (G_UNLIKELY (group->drained)) {
4112 /* Figure out if all our chains are drained with the
4113 * new information */
4114 group->drained = TRUE;
4115 for (tmp = group->children; tmp; tmp = tmp->next) {
4116 GstDecodeChain *chain = (GstDecodeChain *) tmp->data;
4117 gboolean subdrained = FALSE;
4120 drain_and_switch_chains (chain, drainpad, last_group, &subdrained,
4123 group->drained = FALSE;
4127 GST_DEBUG ("group %p (last_group:%d, drained:%d, switched:%d, handled:%d)",
4128 group, *last_group, group->drained, *switched, handled);
4129 *drained = group->drained;
4134 drain_and_switch_chains (GstDecodeChain * chain, GstDecodePad * drainpad,
4135 gboolean * last_group, gboolean * drained, gboolean * switched)
4137 gboolean handled = FALSE;
4138 GstDecodeBin *dbin = chain->dbin;
4140 GST_DEBUG ("Checking chain %p %s:%s (target pad %s:%s)",
4141 chain, GST_DEBUG_PAD_NAME (chain->pad), GST_DEBUG_PAD_NAME (drainpad));
4143 CHAIN_MUTEX_LOCK (chain);
4145 if (chain->pad_probe_id) {
4146 gst_pad_remove_probe (chain->pad, chain->pad_probe_id);
4147 chain->pad_probe_id = 0;
4150 /* Definitely can't be in drained chains */
4151 if (G_UNLIKELY (chain->drained)) {
4155 if (chain->endpad) {
4156 /* Check if we're reached the target endchain */
4157 if (drainpad != NULL && chain == drainpad->chain) {
4158 GST_DEBUG ("Found the target chain");
4159 drainpad->drained = TRUE;
4163 chain->drained = chain->endpad->drained;
4167 /* We known there are groups to switch to */
4168 if (chain->next_groups)
4169 *last_group = FALSE;
4171 /* Check the active group */
4172 if (chain->active_group) {
4173 gboolean subdrained = FALSE;
4174 handled = drain_and_switch_group (chain->active_group, drainpad,
4175 last_group, &subdrained, switched);
4177 /* The group is drained, see if we can switch to another */
4178 if ((handled || drainpad == NULL) && subdrained && !*switched) {
4179 if (chain->next_groups) {
4180 /* Switch to next group */
4181 GST_DEBUG_OBJECT (dbin, "Hiding current group %p", chain->active_group);
4182 gst_decode_group_hide (chain->active_group);
4184 g_list_prepend (chain->old_groups, chain->active_group);
4185 GST_DEBUG_OBJECT (dbin, "Switching to next group %p",
4186 chain->next_groups->data);
4187 chain->active_group = chain->next_groups->data;
4188 chain->next_groups =
4189 g_list_delete_link (chain->next_groups, chain->next_groups);
4190 gst_decode_chain_start_free_hidden_groups_thread (chain);
4192 chain->drained = FALSE;
4194 GST_DEBUG ("Group %p was the last in chain %p", chain->active_group,
4196 chain->drained = TRUE;
4197 /* We're drained ! */
4200 if (subdrained && !chain->next_groups)
4206 CHAIN_MUTEX_UNLOCK (chain);
4208 GST_DEBUG ("Chain %p (handled:%d, last_group:%d, drained:%d, switched:%d)",
4209 chain, handled, *last_group, chain->drained, *switched);
4211 *drained = chain->drained;
4214 g_signal_emit (dbin, gst_decode_bin_signals[SIGNAL_DRAINED], 0, NULL);
4219 /* check if the group is drained, meaning all pads have seen an EOS
4222 gst_decode_pad_handle_eos (GstDecodePad * pad)
4224 gboolean last_group = TRUE;
4225 gboolean switched = FALSE;
4226 gboolean drained = FALSE;
4227 GstDecodeChain *chain = pad->chain;
4228 GstDecodeBin *dbin = chain->dbin;
4231 GST_LOG_OBJECT (dbin, "pad %p", pad);
4233 /* Send a stream-group-done event in case downstream needs
4234 * to unblock before we can drain */
4235 tmp = gst_pad_get_sticky_event (GST_PAD (pad), GST_EVENT_STREAM_START, 0);
4238 if (gst_event_parse_group_id (tmp, &group_id)) {
4239 GstPad *peer = gst_pad_get_peer (GST_PAD (pad));
4242 GST_DEBUG_OBJECT (dbin,
4243 "Sending stream-group-done for group %u to pad %"
4244 GST_PTR_FORMAT, group_id, pad);
4245 gst_pad_send_event (peer, gst_event_new_stream_group_done (group_id));
4246 gst_object_unref (peer);
4249 GST_DEBUG_OBJECT (dbin,
4250 "No group ID to send stream-group-done on pad %" GST_PTR_FORMAT, pad);
4252 gst_event_unref (tmp);
4256 if (dbin->decode_chain) {
4257 drain_and_switch_chains (dbin->decode_chain, pad, &last_group, &drained,
4261 /* If we resulted in a group switch, expose what's needed */
4262 if (gst_decode_chain_is_complete (dbin->decode_chain))
4263 gst_decode_bin_expose (dbin);
4266 EXPOSE_UNLOCK (dbin);
4271 /* gst_decode_group_is_drained:
4273 * Check is this group is drained and cache this result.
4274 * The group is drained if all child chains are drained.
4276 * Not MT-safe, call with group->parent's lock */
4278 gst_decode_group_is_drained (GstDecodeGroup * group)
4281 gboolean drained = TRUE;
4283 if (group->drained) {
4288 for (l = group->children; l; l = l->next) {
4289 GstDecodeChain *chain = l->data;
4291 CHAIN_MUTEX_LOCK (chain);
4292 if (!gst_decode_chain_is_drained (chain))
4294 CHAIN_MUTEX_UNLOCK (chain);
4298 group->drained = drained;
4301 GST_DEBUG_OBJECT (group->dbin, "Group %p is drained: %d", group, drained);
4305 /* gst_decode_chain_is_drained:
4307 * Check is the chain is drained, which means that
4310 * a) it's endpad is drained
4311 * b) there are no pending pads, the active group is drained
4312 * and there are no next groups
4314 * Not MT-safe, call with chain lock
4317 gst_decode_chain_is_drained (GstDecodeChain * chain)
4319 gboolean drained = FALSE;
4321 if (chain->endpad) {
4322 drained = chain->endpad->drained;
4326 if (chain->pending_pads) {
4331 if (chain->active_group && gst_decode_group_is_drained (chain->active_group)
4332 && !chain->next_groups) {
4338 GST_DEBUG_OBJECT (chain->dbin, "Chain %p is drained: %d", chain, drained);
4343 gst_decode_group_reset_buffering (GstDecodeGroup * group)
4346 gboolean ret = TRUE;
4348 GST_DEBUG_OBJECT (group->dbin, "Group reset buffering %p %s", group,
4349 GST_ELEMENT_NAME (group->multiqueue));
4350 for (l = group->children; l; l = l->next) {
4351 GstDecodeChain *chain = l->data;
4353 CHAIN_MUTEX_LOCK (chain);
4354 if (!gst_decode_chain_reset_buffering (chain)) {
4357 CHAIN_MUTEX_UNLOCK (chain);
4360 decodebin_set_queue_size_full (group->dbin, group->multiqueue, !ret,
4361 FALSE, (group->parent ? group->parent->seekable : TRUE));
4364 /* all chains are buffering already, no need to do it here */
4365 g_object_set (group->multiqueue, "use-buffering", FALSE, NULL);
4367 g_object_set (group->multiqueue, "use-buffering", TRUE,
4368 "low-percent", group->dbin->low_percent,
4369 "high-percent", group->dbin->high_percent, NULL);
4372 GST_DEBUG_OBJECT (group->dbin, "Setting %s buffering to %d",
4373 GST_ELEMENT_NAME (group->multiqueue), !ret);
4379 * GCompareFunc to use with lists of GstPad.
4380 * Sorts pads by mime type.
4381 * First video (raw, then non-raw), then audio (raw, then non-raw),
4384 * Return: negative if a<b, 0 if a==b, positive if a>b
4387 sort_end_pads (GstDecodePad * da, GstDecodePad * db)
4390 GstCaps *capsa, *capsb;
4391 GstStructure *sa, *sb;
4392 const gchar *namea, *nameb;
4396 capsa = get_pad_caps (GST_PAD_CAST (da));
4397 capsb = get_pad_caps (GST_PAD_CAST (db));
4399 sa = gst_caps_get_structure ((const GstCaps *) capsa, 0);
4400 sb = gst_caps_get_structure ((const GstCaps *) capsb, 0);
4402 namea = gst_structure_get_name (sa);
4403 nameb = gst_structure_get_name (sb);
4405 if (g_strrstr (namea, "video/x-raw"))
4407 else if (g_strrstr (namea, "video/"))
4409 else if (g_strrstr (namea, "audio/x-raw"))
4411 else if (g_strrstr (namea, "audio/"))
4416 if (g_strrstr (nameb, "video/x-raw"))
4418 else if (g_strrstr (nameb, "video/"))
4420 else if (g_strrstr (nameb, "audio/x-raw"))
4422 else if (g_strrstr (nameb, "audio/"))
4427 gst_caps_unref (capsa);
4428 gst_caps_unref (capsb);
4433 /* if otherwise the same, sort by stream-id */
4434 ida = gst_pad_get_stream_id (GST_PAD_CAST (da));
4435 idb = gst_pad_get_stream_id (GST_PAD_CAST (db));
4436 ret = (ida) ? ((idb) ? strcmp (ida, idb) : -1) : 1;
4444 _gst_element_get_linked_caps (GstElement * src, GstElement * sink,
4445 GstElement * capsfilter, GstPad ** srcpad)
4450 gboolean done = FALSE;
4451 GstCaps *caps = NULL;
4452 GValue item = { 0, };
4454 it = gst_element_iterate_src_pads (src);
4456 switch (gst_iterator_next (it, &item)) {
4457 case GST_ITERATOR_OK:
4458 pad = g_value_get_object (&item);
4459 peer = gst_pad_get_peer (pad);
4461 parent = gst_pad_get_parent_element (peer);
4462 if (parent == sink || (capsfilter != NULL && parent == capsfilter)) {
4463 caps = gst_pad_get_current_caps (pad);
4464 *srcpad = gst_object_ref (pad);
4469 gst_object_unref (parent);
4470 gst_object_unref (peer);
4472 g_value_reset (&item);
4474 case GST_ITERATOR_RESYNC:
4475 gst_iterator_resync (it);
4477 case GST_ITERATOR_ERROR:
4478 case GST_ITERATOR_DONE:
4483 g_value_unset (&item);
4484 gst_iterator_free (it);
4489 static GQuark topology_structure_name = 0;
4490 static GQuark topology_caps = 0;
4491 static GQuark topology_next = 0;
4492 static GQuark topology_pad = 0;
4493 static GQuark topology_element_srcpad = 0;
4495 /* FIXME: Invent gst_structure_take_structure() to prevent all the
4496 * structure copying for nothing
4498 static GstStructure *
4499 gst_decode_chain_get_topology (GstDecodeChain * chain)
4501 GstStructure *s, *u;
4505 if (G_UNLIKELY ((chain->endpad || chain->deadend)
4506 && (chain->endcaps == NULL))) {
4507 GST_WARNING ("End chain without valid caps !");
4511 u = gst_structure_new_id_empty (topology_structure_name);
4513 /* Now at the last element */
4514 if ((chain->elements || !chain->active_group) &&
4515 (chain->endpad || chain->deadend)) {
4518 s = gst_structure_new_id_empty (topology_structure_name);
4519 gst_structure_id_set (u, topology_caps, GST_TYPE_CAPS, chain->endcaps,
4522 if (chain->endpad) {
4523 gst_structure_id_set (u, topology_pad, GST_TYPE_PAD, chain->endpad, NULL);
4525 srcpad = gst_ghost_pad_get_target (GST_GHOST_PAD_CAST (chain->endpad));
4526 gst_structure_id_set (u, topology_element_srcpad, GST_TYPE_PAD,
4529 gst_object_unref (srcpad);
4532 gst_structure_id_set (s, topology_next, GST_TYPE_STRUCTURE, u, NULL);
4533 gst_structure_free (u);
4535 } else if (chain->active_group) {
4536 GValue list = { 0, };
4537 GValue item = { 0, };
4539 g_value_init (&list, GST_TYPE_LIST);
4540 g_value_init (&item, GST_TYPE_STRUCTURE);
4541 for (l = chain->active_group->children; l; l = l->next) {
4542 s = gst_decode_chain_get_topology (l->data);
4544 gst_value_set_structure (&item, s);
4545 gst_value_list_append_value (&list, &item);
4546 g_value_reset (&item);
4547 gst_structure_free (s);
4550 gst_structure_id_set_value (u, topology_next, &list);
4551 g_value_unset (&list);
4552 g_value_unset (&item);
4555 /* Get caps between all elements in this chain */
4556 l = (chain->elements && chain->elements->next) ? chain->elements : NULL;
4557 for (; l && l->next; l = l->next) {
4558 GstDecodeElement *delem, *delem_next;
4559 GstElement *elem, *capsfilter, *elem_next;
4564 elem = delem->element;
4565 delem_next = l->next->data;
4566 elem_next = delem_next->element;
4567 capsfilter = delem_next->capsfilter;
4570 caps = _gst_element_get_linked_caps (elem_next, elem, capsfilter, &srcpad);
4573 s = gst_structure_new_id_empty (topology_structure_name);
4574 gst_structure_id_set (u, topology_caps, GST_TYPE_CAPS, caps, NULL);
4575 gst_caps_unref (caps);
4577 gst_structure_id_set (s, topology_next, GST_TYPE_STRUCTURE, u, NULL);
4578 gst_structure_free (u);
4583 gst_structure_id_set (u, topology_element_srcpad, GST_TYPE_PAD, srcpad,
4585 gst_object_unref (srcpad);
4589 /* Caps that resulted in this chain */
4590 caps = get_pad_caps (chain->pad);
4591 if (G_UNLIKELY (!caps)) {
4592 GST_WARNING_OBJECT (chain->pad, "Couldn't get the caps of decode chain");
4595 gst_structure_id_set (u, topology_caps, GST_TYPE_CAPS, caps, NULL);
4596 gst_structure_id_set (u, topology_element_srcpad, GST_TYPE_PAD, chain->pad,
4598 gst_caps_unref (caps);
4604 gst_decode_bin_post_topology_message (GstDecodeBin * dbin)
4609 s = gst_decode_chain_get_topology (dbin->decode_chain);
4611 if (G_UNLIKELY (s == NULL))
4613 msg = gst_message_new_element (GST_OBJECT (dbin), s);
4614 gst_element_post_message (GST_ELEMENT (dbin), msg);
4618 debug_sticky_event (GstPad * pad, GstEvent ** event, gpointer user_data)
4620 GST_DEBUG_OBJECT (pad, "sticky event %s (%p)", GST_EVENT_TYPE_NAME (*event),
4626 /* Must only be called if the toplevel chain is complete and blocked! */
4627 /* Not MT-safe, call with decodebin expose lock! */
4629 gst_decode_bin_expose (GstDecodeBin * dbin)
4631 GList *tmp, *endpads;
4632 gboolean missing_plugin;
4633 GString *missing_plugin_details;
4634 gboolean already_exposed;
4635 gboolean last_group;
4639 missing_plugin = FALSE;
4640 already_exposed = TRUE;
4643 missing_plugin_details = g_string_new ("");
4645 GST_DEBUG_OBJECT (dbin, "Exposing currently active chains/groups");
4647 /* Don't expose if we're currently shutting down */
4649 if (G_UNLIKELY (dbin->shutdown)) {
4650 GST_WARNING_OBJECT (dbin, "Currently, shutting down, aborting exposing");
4656 /* Get the pads that we're going to expose and mark things as exposed */
4657 if (!gst_decode_chain_expose (dbin->decode_chain, &endpads, &missing_plugin,
4658 missing_plugin_details, &last_group)) {
4659 g_list_foreach (endpads, (GFunc) gst_object_unref, NULL);
4660 g_list_free (endpads);
4661 g_string_free (missing_plugin_details, TRUE);
4662 /* Failures could be due to the fact that we are currently shutting down (recheck) */
4664 if (G_UNLIKELY (dbin->shutdown)) {
4665 GST_WARNING_OBJECT (dbin, "Currently, shutting down, aborting exposing");
4670 GST_ERROR_OBJECT (dbin, "Broken chain/group tree");
4671 g_return_val_if_reached (FALSE);
4674 if (endpads == NULL) {
4675 if (missing_plugin) {
4676 if (missing_plugin_details->len > 0) {
4677 gchar *details = g_string_free (missing_plugin_details, FALSE);
4678 GST_ELEMENT_ERROR (dbin, CORE, MISSING_PLUGIN, (NULL),
4679 ("no suitable plugins found:\n%s", details));
4682 g_string_free (missing_plugin_details, TRUE);
4683 GST_ELEMENT_ERROR (dbin, CORE, MISSING_PLUGIN, (NULL),
4684 ("no suitable plugins found"));
4687 /* in this case, the stream ended without buffers,
4688 * just post a warning */
4689 g_string_free (missing_plugin_details, TRUE);
4691 GST_WARNING_OBJECT (dbin, "All streams finished without buffers. "
4692 "Last group: %d", last_group);
4694 GST_ELEMENT_ERROR (dbin, STREAM, FAILED, (NULL),
4695 ("all streams without buffers"));
4697 gboolean switched = FALSE;
4698 gboolean drained = FALSE;
4700 drain_and_switch_chains (dbin->decode_chain, NULL, &last_group,
4701 &drained, &switched);
4702 GST_ELEMENT_WARNING (dbin, STREAM, FAILED, (NULL),
4703 ("all streams without buffers"));
4705 if (gst_decode_chain_is_complete (dbin->decode_chain))
4713 do_async_done (dbin);
4717 g_string_free (missing_plugin_details, TRUE);
4719 /* Check if this was called when everything was exposed already */
4720 for (tmp = endpads; tmp && already_exposed; tmp = tmp->next) {
4721 GstDecodePad *dpad = tmp->data;
4723 already_exposed &= dpad->exposed;
4724 if (!already_exposed)
4727 if (already_exposed) {
4728 GST_DEBUG_OBJECT (dbin, "Everything was exposed already!");
4729 g_list_foreach (endpads, (GFunc) gst_object_unref, NULL);
4730 g_list_free (endpads);
4734 /* going to expose something, reset buffering */
4735 gst_decode_bin_reset_buffering (dbin);
4737 /* Set all already exposed pads to blocked */
4738 for (tmp = endpads; tmp; tmp = tmp->next) {
4739 GstDecodePad *dpad = tmp->data;
4741 if (dpad->exposed) {
4742 GST_DEBUG_OBJECT (dpad, "blocking exposed pad");
4743 gst_decode_pad_set_blocked (dpad, TRUE);
4747 /* re-order pads : video, then audio, then others */
4748 endpads = g_list_sort (endpads, (GCompareFunc) sort_end_pads);
4750 /* Don't add pads if we are shutting down */
4752 if (G_UNLIKELY (dbin->shutdown)) {
4753 GST_WARNING_OBJECT (dbin, "Currently, shutting down, aborting exposing");
4759 for (tmp = endpads; tmp; tmp = tmp->next) {
4760 GstDecodePad *dpad = (GstDecodePad *) tmp->data;
4763 /* 1. rewrite name */
4764 padname = g_strdup_printf ("src_%u", dbin->nbpads);
4766 GST_DEBUG_OBJECT (dbin, "About to expose dpad %s as %s",
4767 GST_OBJECT_NAME (dpad), padname);
4768 gst_object_set_name (GST_OBJECT (dpad), padname);
4771 gst_pad_sticky_events_foreach (GST_PAD_CAST (dpad), debug_sticky_event,
4774 /* 2. activate and add */
4775 if (!dpad->exposed) {
4776 dpad->exposed = TRUE;
4777 if (!gst_element_add_pad (GST_ELEMENT (dbin), GST_PAD_CAST (dpad))) {
4778 /* not really fatal, we can try to add the other pads */
4779 g_warning ("error adding pad to decodebin");
4780 dpad->exposed = FALSE;
4785 /* 3. emit signal */
4786 GST_INFO_OBJECT (dpad, "added new decoded pad");
4790 /* 4. Signal no-more-pads. This allows the application to hook stuff to the
4792 GST_LOG_OBJECT (dbin, "signaling no-more-pads");
4793 gst_element_no_more_pads (GST_ELEMENT (dbin));
4795 /* 5. Send a custom element message with the stream topology */
4796 if (dbin->post_stream_topology)
4797 gst_decode_bin_post_topology_message (dbin);
4799 /* 6. Unblock internal pads. The application should have connected stuff now
4800 * so that streaming can continue. */
4801 for (tmp = endpads; tmp; tmp = tmp->next) {
4802 GstDecodePad *dpad = (GstDecodePad *) tmp->data;
4804 GST_DEBUG_OBJECT (dpad, "unblocking");
4805 gst_decode_pad_unblock (dpad);
4806 GST_DEBUG_OBJECT (dpad, "unblocked");
4807 gst_object_unref (dpad);
4809 g_list_free (endpads);
4811 do_async_done (dbin);
4812 GST_DEBUG_OBJECT (dbin, "Exposed everything");
4816 /* gst_decode_chain_expose:
4818 * Check if the chain can be exposed and add all endpads
4819 * to the endpads list.
4821 * Also update the active group's multiqueue to the
4824 * Not MT-safe, call with decodebin expose lock! *
4827 gst_decode_chain_expose (GstDecodeChain * chain, GList ** endpads,
4828 gboolean * missing_plugin, GString * missing_plugin_details,
4829 gboolean * last_group)
4831 GstDecodeGroup *group;
4835 if (chain->deadend) {
4836 if (chain->endcaps) {
4837 if (chain->deadend_details) {
4838 g_string_append (missing_plugin_details, chain->deadend_details);
4839 g_string_append_c (missing_plugin_details, '\n');
4841 gchar *desc = gst_pb_utils_get_codec_description (chain->endcaps);
4842 gchar *caps_str = gst_caps_to_string (chain->endcaps);
4843 g_string_append_printf (missing_plugin_details,
4844 "Missing decoder: %s (%s)\n", desc, caps_str);
4848 *missing_plugin = TRUE;
4853 if (chain->endpad) {
4854 if (!gst_decode_pad_is_exposable (chain->endpad) && !chain->endpad->exposed)
4856 *endpads = g_list_prepend (*endpads, gst_object_ref (chain->endpad));
4860 if (chain->next_groups)
4861 *last_group = FALSE;
4863 group = chain->active_group;
4866 if (!group->no_more_pads && !group->overrun)
4871 /* we can now disconnect any overrun signal, which is used to expose the
4873 if (group->overrunsig) {
4874 GST_LOG_OBJECT (dbin, "Disconnecting overrun");
4875 g_signal_handler_disconnect (group->multiqueue, group->overrunsig);
4876 group->overrunsig = 0;
4879 for (l = group->children; l; l = l->next) {
4880 GstDecodeChain *childchain = l->data;
4882 if (!gst_decode_chain_expose (childchain, endpads, missing_plugin,
4883 missing_plugin_details, last_group))
4890 /*************************
4891 * GstDecodePad functions
4892 *************************/
4895 gst_decode_pad_class_init (GstDecodePadClass * klass)
4900 gst_decode_pad_init (GstDecodePad * pad)
4903 pad->blocked = FALSE;
4904 pad->exposed = FALSE;
4905 pad->drained = FALSE;
4906 gst_object_ref_sink (pad);
4909 static GstPadProbeReturn
4910 source_pad_blocked_cb (GstPad * pad, GstPadProbeInfo * info, gpointer user_data)
4912 GstDecodePad *dpad = user_data;
4913 GstDecodeChain *chain;
4915 GstPadProbeReturn ret = GST_PAD_PROBE_OK;
4917 if (GST_PAD_PROBE_INFO_TYPE (info) & GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM) {
4918 GstEvent *event = GST_PAD_PROBE_INFO_EVENT (info);
4920 GST_LOG_OBJECT (pad, "Seeing event '%s'", GST_EVENT_TYPE_NAME (event));
4922 if (!GST_EVENT_IS_SERIALIZED (event)) {
4923 /* do not block on sticky or out of band events otherwise the allocation query
4924 from demuxer might block the loop thread */
4925 GST_LOG_OBJECT (pad, "Letting OOB event through");
4926 return GST_PAD_PROBE_PASS;
4929 if (GST_EVENT_IS_STICKY (event) && GST_EVENT_TYPE (event) != GST_EVENT_EOS) {
4930 /* manually push sticky events to ghost pad to avoid exposing pads
4931 * that don't have the sticky events. Handle EOS separately as we
4932 * want to block the pad on it if we didn't get any buffers before
4933 * EOS and expose the pad then. */
4934 gst_pad_push_event (GST_PAD_CAST (dpad), gst_event_ref (event));
4936 /* let the sticky events pass */
4937 ret = GST_PAD_PROBE_PASS;
4939 /* we only want to try to expose on CAPS events */
4940 if (GST_EVENT_TYPE (event) != GST_EVENT_CAPS) {
4941 GST_LOG_OBJECT (pad, "Letting sticky non-CAPS event through");
4945 } else if (GST_PAD_PROBE_INFO_TYPE (info) &
4946 GST_PAD_PROBE_TYPE_QUERY_DOWNSTREAM) {
4947 GstQuery *query = GST_PAD_PROBE_INFO_QUERY (info);
4949 if (!GST_QUERY_IS_SERIALIZED (query)) {
4950 /* do not block on non-serialized queries */
4951 GST_LOG_OBJECT (pad, "Letting non-serialized query through");
4952 return GST_PAD_PROBE_PASS;
4954 if (!gst_pad_has_current_caps (pad)) {
4955 /* do not block on allocation queries before we have caps,
4956 * this would deadlock because we are doing no autoplugging
4958 * TODO: Try to do autoplugging based on the query caps
4960 GST_LOG_OBJECT (pad, "Letting serialized query before caps through");
4961 return GST_PAD_PROBE_PASS;
4964 chain = dpad->chain;
4967 GST_LOG_OBJECT (dpad, "blocked: dpad->chain:%p", chain);
4969 dpad->blocked = TRUE;
4972 if (dbin->decode_chain) {
4973 if (gst_decode_chain_is_complete (dbin->decode_chain)) {
4974 if (!gst_decode_bin_expose (dbin))
4975 GST_WARNING_OBJECT (dbin, "Couldn't expose group");
4978 EXPOSE_UNLOCK (dbin);
4984 static GstPadProbeReturn
4985 source_pad_event_probe (GstPad * pad, GstPadProbeInfo * info,
4988 GstEvent *event = GST_PAD_PROBE_INFO_EVENT (info);
4989 GstDecodePad *dpad = user_data;
4990 gboolean res = TRUE;
4992 GST_LOG_OBJECT (pad, "%s dpad:%p", GST_EVENT_TYPE_NAME (event), dpad);
4994 if (GST_EVENT_TYPE (event) == GST_EVENT_EOS) {
4995 GST_DEBUG_OBJECT (pad, "we received EOS");
4997 /* Check if all pads are drained.
4998 * * If there is no next group, we will let the EOS go through.
4999 * * If there is a next group but the current group isn't completely
5000 * drained, we will drop the EOS event.
5001 * * If there is a next group to expose and this was the last non-drained
5002 * pad for that group, we will remove the ghostpad of the current group
5003 * first, which unlinks the peer and so drops the EOS. */
5004 res = gst_decode_pad_handle_eos (dpad);
5007 return GST_PAD_PROBE_OK;
5009 return GST_PAD_PROBE_DROP;
5013 gst_decode_pad_set_blocked (GstDecodePad * dpad, gboolean blocked)
5015 GstDecodeBin *dbin = dpad->dbin;
5020 GST_DEBUG_OBJECT (dpad, "blocking pad: %d", blocked);
5022 opad = gst_ghost_pad_get_target (GST_GHOST_PAD_CAST (dpad));
5026 /* do not block if shutting down.
5027 * we do not consider/expect it blocked further below, but use other trick */
5028 if (!blocked || !dbin->shutdown) {
5030 if (dpad->block_id == 0)
5032 gst_pad_add_probe (opad,
5033 GST_PAD_PROBE_TYPE_BLOCK_DOWNSTREAM |
5034 GST_PAD_PROBE_TYPE_QUERY_DOWNSTREAM, source_pad_blocked_cb,
5035 gst_object_ref (dpad), (GDestroyNotify) gst_object_unref);
5037 if (dpad->block_id != 0) {
5038 gst_pad_remove_probe (opad, dpad->block_id);
5041 dpad->blocked = FALSE;
5046 if (dbin->shutdown) {
5047 /* deactivate to force flushing state to prevent NOT_LINKED errors */
5048 gst_pad_set_active (GST_PAD_CAST (dpad), FALSE);
5049 /* note that deactivating the target pad would have no effect here,
5050 * since elements are typically connected first (and pads exposed),
5051 * and only then brought to PAUSED state (so pads activated) */
5053 gst_object_ref (dpad);
5054 dbin->blocked_pads = g_list_prepend (dbin->blocked_pads, dpad);
5059 if ((l = g_list_find (dbin->blocked_pads, dpad))) {
5060 gst_object_unref (dpad);
5061 dbin->blocked_pads = g_list_delete_link (dbin->blocked_pads, l);
5064 gst_object_unref (opad);
5070 gst_decode_pad_add_drained_check (GstDecodePad * dpad)
5072 gst_pad_add_probe (GST_PAD_CAST (dpad), GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM,
5073 source_pad_event_probe, dpad, NULL);
5077 gst_decode_pad_activate (GstDecodePad * dpad, GstDecodeChain * chain)
5079 g_return_if_fail (chain != NULL);
5081 dpad->chain = chain;
5082 gst_pad_set_active (GST_PAD_CAST (dpad), TRUE);
5083 gst_decode_pad_set_blocked (dpad, TRUE);
5084 gst_decode_pad_add_drained_check (dpad);
5088 gst_decode_pad_unblock (GstDecodePad * dpad)
5090 gst_decode_pad_set_blocked (dpad, FALSE);
5094 gst_decode_pad_event (GstPad * pad, GstObject * parent, GstEvent * event)
5096 GstDecodeBin *dbin = GST_DECODE_BIN (parent);
5098 if (GST_EVENT_TYPE (event) == GST_EVENT_SEEK && dbin && dbin->decode_chain) {
5099 GstElement *demuxer = NULL;
5101 /* For adaptive demuxers we send the seek event directly to the demuxer.
5102 * See https://bugzilla.gnome.org/show_bug.cgi?id=606382
5104 CHAIN_MUTEX_LOCK (dbin->decode_chain);
5105 if (dbin->decode_chain->adaptive_demuxer) {
5106 GstDecodeElement *delem = dbin->decode_chain->elements->data;
5107 demuxer = gst_object_ref (delem->element);
5109 CHAIN_MUTEX_UNLOCK (dbin->decode_chain);
5114 GST_DEBUG_OBJECT (dbin,
5115 "Sending SEEK event directly to adaptive streaming demuxer %s",
5116 GST_OBJECT_NAME (demuxer));
5117 ret = gst_element_send_event (demuxer, event);
5118 gst_object_unref (demuxer);
5123 return gst_pad_event_default (pad, parent, event);
5127 gst_decode_pad_query (GstPad * pad, GstObject * parent, GstQuery * query)
5129 GstDecodePad *dpad = GST_DECODE_PAD (parent);
5130 gboolean ret = FALSE;
5132 CHAIN_MUTEX_LOCK (dpad->chain);
5133 if (!dpad->exposed && !dpad->dbin->shutdown && !dpad->chain->deadend
5134 && dpad->chain->elements) {
5135 GstDecodeElement *delem = dpad->chain->elements->data;
5138 GST_DEBUG_OBJECT (dpad->dbin,
5139 "calling autoplug-query for %s (element %s): %" GST_PTR_FORMAT,
5140 GST_PAD_NAME (dpad), GST_ELEMENT_NAME (delem->element), query);
5141 g_signal_emit (G_OBJECT (dpad->dbin),
5142 gst_decode_bin_signals[SIGNAL_AUTOPLUG_QUERY], 0, dpad, delem->element,
5146 GST_DEBUG_OBJECT (dpad->dbin,
5147 "autoplug-query returned %d: %" GST_PTR_FORMAT, ret, query);
5149 GST_DEBUG_OBJECT (dpad->dbin, "autoplug-query returned %d", ret);
5151 CHAIN_MUTEX_UNLOCK (dpad->chain);
5153 /* If exposed or nothing handled the query use the default handler */
5155 ret = gst_pad_query_default (pad, parent, query);
5161 gst_decode_pad_is_exposable (GstDecodePad * endpad)
5163 if (endpad->blocked || endpad->exposed)
5166 return gst_pad_has_current_caps (GST_PAD_CAST (endpad));
5169 /*gst_decode_pad_new:
5171 * Creates a new GstDecodePad for the given pad.
5173 static GstDecodePad *
5174 gst_decode_pad_new (GstDecodeBin * dbin, GstDecodeChain * chain)
5178 GstPadTemplate *pad_tmpl;
5180 GST_DEBUG_OBJECT (dbin, "making new decodepad");
5181 pad_tmpl = gst_static_pad_template_get (&decoder_bin_src_template);
5183 g_object_new (GST_TYPE_DECODE_PAD, "direction", GST_PAD_SRC,
5184 "template", pad_tmpl, NULL);
5185 gst_ghost_pad_construct (GST_GHOST_PAD_CAST (dpad));
5186 dpad->chain = chain;
5188 gst_object_unref (pad_tmpl);
5190 ppad = gst_proxy_pad_get_internal (GST_PROXY_PAD (dpad));
5191 gst_pad_set_query_function (GST_PAD_CAST (ppad), gst_decode_pad_query);
5192 gst_pad_set_event_function (GST_PAD_CAST (dpad), gst_decode_pad_event);
5193 gst_object_unref (ppad);
5199 gst_pending_pad_free (GstPendingPad * ppad)
5202 g_assert (ppad->pad);
5204 if (ppad->event_probe_id != 0)
5205 gst_pad_remove_probe (ppad->pad, ppad->event_probe_id);
5206 if (ppad->notify_caps_id)
5207 g_signal_handler_disconnect (ppad->pad, ppad->notify_caps_id);
5208 gst_object_unref (ppad->pad);
5209 g_slice_free (GstPendingPad, ppad);
5213 * Element add/remove
5217 do_async_start (GstDecodeBin * dbin)
5219 GstMessage *message;
5221 dbin->async_pending = TRUE;
5223 message = gst_message_new_async_start (GST_OBJECT_CAST (dbin));
5224 parent_class->handle_message (GST_BIN_CAST (dbin), message);
5228 do_async_done (GstDecodeBin * dbin)
5230 GstMessage *message;
5232 if (dbin->async_pending) {
5234 gst_message_new_async_done (GST_OBJECT_CAST (dbin),
5235 GST_CLOCK_TIME_NONE);
5236 parent_class->handle_message (GST_BIN_CAST (dbin), message);
5238 dbin->async_pending = FALSE;
5243 * convenience functions
5248 * Returns the first sink pad of the given element, or NULL if it doesn't have
5253 find_sink_pad (GstElement * element)
5257 GValue item = { 0, };
5259 it = gst_element_iterate_sink_pads (element);
5261 if ((gst_iterator_next (it, &item)) == GST_ITERATOR_OK)
5262 pad = g_value_dup_object (&item);
5263 g_value_unset (&item);
5264 gst_iterator_free (it);
5269 /* call with dyn_lock held */
5271 unblock_pads (GstDecodeBin * dbin)
5273 GST_LOG_OBJECT (dbin, "unblocking pads");
5275 while (dbin->blocked_pads) {
5276 GList *tmp = dbin->blocked_pads;
5277 GstDecodePad *dpad = (GstDecodePad *) tmp->data;
5280 dbin->blocked_pads = g_list_delete_link (dbin->blocked_pads, tmp);
5281 opad = gst_ghost_pad_get_target (GST_GHOST_PAD_CAST (dpad));
5284 GST_DEBUG_OBJECT (dpad, "unblocking");
5285 if (dpad->block_id != 0) {
5286 gst_pad_remove_probe (opad, dpad->block_id);
5289 gst_object_unref (opad);
5292 dpad->blocked = FALSE;
5294 /* We release the dyn lock since we want to allow the streaming threads
5295 * to properly stop and not be blocked in our various probes */
5297 /* make flushing, prevent NOT_LINKED */
5298 gst_pad_set_active (GST_PAD_CAST (dpad), FALSE);
5301 GST_DEBUG_OBJECT (dpad, "unblocked");
5302 gst_object_unref (dpad);
5307 gst_decode_chain_stop (GstDecodeBin * dbin, GstDecodeChain * chain,
5310 GQueue *internal_elements, internal_elements_ = G_QUEUE_INIT;
5313 CHAIN_MUTEX_LOCK (chain);
5315 internal_elements = elements;
5317 internal_elements = &internal_elements_;
5320 for (l = chain->next_groups; l; l = l->next) {
5321 GstDecodeGroup *group = l->data;
5324 for (m = group->children; m; m = m->next) {
5325 GstDecodeChain *chain2 = m->data;
5326 gst_decode_chain_stop (dbin, chain2, internal_elements);
5328 if (group->multiqueue)
5329 g_queue_push_head (internal_elements, gst_object_ref (group->multiqueue));
5332 if (chain->active_group) {
5333 for (l = chain->active_group->children; l; l = l->next) {
5334 GstDecodeChain *chain2 = l->data;
5335 gst_decode_chain_stop (dbin, chain2, internal_elements);
5337 if (chain->active_group->multiqueue)
5338 g_queue_push_head (internal_elements,
5339 gst_object_ref (chain->active_group->multiqueue));
5342 for (l = chain->old_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 for (l = chain->elements; l; l = l->next) {
5355 GstDecodeElement *delem = l->data;
5357 if (delem->capsfilter)
5358 g_queue_push_head (internal_elements, gst_object_ref (delem->capsfilter));
5359 g_queue_push_head (internal_elements, gst_object_ref (delem->element));
5362 CHAIN_MUTEX_UNLOCK (chain);
5365 GstElement *element;
5367 EXPOSE_UNLOCK (dbin);
5368 /* Shut down from bottom to top */
5369 while ((element = g_queue_pop_tail (internal_elements))) {
5370 /* The bin must never ever change the state of this element anymore */
5371 gst_element_set_locked_state (element, TRUE);
5372 gst_element_set_state (element, GST_STATE_NULL);
5373 gst_object_unref (element);
5375 g_queue_clear (internal_elements);
5380 static GstStateChangeReturn
5381 gst_decode_bin_change_state (GstElement * element, GstStateChange transition)
5383 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
5384 GstDecodeBin *dbin = GST_DECODE_BIN (element);
5385 GstDecodeChain *chain_to_free = NULL;
5387 switch (transition) {
5388 case GST_STATE_CHANGE_NULL_TO_READY:
5389 if (dbin->typefind == NULL)
5390 goto missing_typefind;
5392 case GST_STATE_CHANGE_READY_TO_PAUSED:
5393 /* Make sure we've cleared all existing chains */
5395 if (dbin->decode_chain) {
5396 gst_decode_chain_free (dbin->decode_chain);
5397 dbin->decode_chain = NULL;
5399 EXPOSE_UNLOCK (dbin);
5401 GST_LOG_OBJECT (dbin, "clearing shutdown flag");
5402 dbin->shutdown = FALSE;
5404 dbin->have_type = FALSE;
5405 ret = GST_STATE_CHANGE_ASYNC;
5406 do_async_start (dbin);
5409 /* connect a signal to find out when the typefind element found
5411 dbin->have_type_id =
5412 g_signal_connect (dbin->typefind, "have-type",
5413 G_CALLBACK (type_found), dbin);
5415 case GST_STATE_CHANGE_PAUSED_TO_READY:
5416 case GST_STATE_CHANGE_READY_TO_NULL:
5417 if (dbin->have_type_id)
5418 g_signal_handler_disconnect (dbin->typefind, dbin->have_type_id);
5419 dbin->have_type_id = 0;
5421 GST_LOG_OBJECT (dbin, "setting shutdown flag");
5422 dbin->shutdown = TRUE;
5423 unblock_pads (dbin);
5426 /* Make sure we don't have cleanup races where
5427 * we might be trying to deactivate pads (in the cleanup thread)
5428 * at the same time as the default element deactivation
5429 * (in PAUSED=>READY) */
5430 g_mutex_lock (&dbin->cleanup_lock);
5431 if (dbin->cleanup_thread) {
5432 g_thread_join (dbin->cleanup_thread);
5433 dbin->cleanup_thread = NULL;
5435 g_mutex_unlock (&dbin->cleanup_lock);
5441 GstStateChangeReturn bret;
5443 bret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
5444 if (G_UNLIKELY (bret == GST_STATE_CHANGE_FAILURE))
5445 goto activate_failed;
5446 else if (G_UNLIKELY (bret == GST_STATE_CHANGE_NO_PREROLL)) {
5447 do_async_done (dbin);
5451 switch (transition) {
5452 case GST_STATE_CHANGE_PAUSED_TO_READY:
5453 do_async_done (dbin);
5455 if (dbin->decode_chain) {
5456 gst_decode_chain_stop (dbin, dbin->decode_chain, NULL);
5457 chain_to_free = dbin->decode_chain;
5458 gst_decode_chain_free_internal (dbin->decode_chain, TRUE);
5459 dbin->decode_chain = NULL;
5461 EXPOSE_UNLOCK (dbin);
5463 gst_decode_chain_free (chain_to_free);
5464 g_list_free_full (dbin->buffering_status,
5465 (GDestroyNotify) gst_message_unref);
5466 dbin->buffering_status = NULL;
5467 /* Let's do a final check of leftover groups to free */
5468 g_mutex_lock (&dbin->cleanup_lock);
5469 if (dbin->cleanup_groups) {
5470 gst_decode_chain_free_hidden_groups (dbin->cleanup_groups);
5471 dbin->cleanup_groups = NULL;
5473 g_mutex_unlock (&dbin->cleanup_lock);
5475 case GST_STATE_CHANGE_READY_TO_NULL:
5476 /* Let's do a final check of leftover groups to free */
5477 g_mutex_lock (&dbin->cleanup_lock);
5478 if (dbin->cleanup_groups) {
5479 gst_decode_chain_free_hidden_groups (dbin->cleanup_groups);
5480 dbin->cleanup_groups = NULL;
5482 g_mutex_unlock (&dbin->cleanup_lock);
5493 gst_element_post_message (element,
5494 gst_missing_element_message_new (element, "typefind"));
5495 GST_ELEMENT_ERROR (dbin, CORE, MISSING_PLUGIN, (NULL), ("no typefind!"));
5496 return GST_STATE_CHANGE_FAILURE;
5500 GST_DEBUG_OBJECT (element,
5501 "element failed to change states -- activation problem?");
5502 do_async_done (dbin);
5503 return GST_STATE_CHANGE_FAILURE;
5508 gst_decode_bin_handle_message (GstBin * bin, GstMessage * msg)
5510 GstDecodeBin *dbin = GST_DECODE_BIN (bin);
5511 gboolean drop = FALSE;
5513 if (GST_MESSAGE_TYPE (msg) == GST_MESSAGE_ERROR) {
5514 /* Don't pass errors when shutting down. Sometimes,
5515 * elements can generate spurious errors because we set the
5516 * output pads to flushing, and they can't detect that if they
5517 * send an event at exactly the wrong moment */
5519 drop = dbin->shutdown;
5523 GST_OBJECT_LOCK (dbin);
5524 drop = (g_list_find (dbin->filtered, GST_MESSAGE_SRC (msg)) != NULL);
5526 dbin->filtered_errors =
5527 g_list_prepend (dbin->filtered_errors, gst_message_ref (msg));
5528 GST_OBJECT_UNLOCK (dbin);
5530 } else if (GST_MESSAGE_TYPE (msg) == GST_MESSAGE_BUFFERING) {
5531 gint perc, msg_perc;
5532 gint smaller_perc = 100;
5533 GstMessage *smaller = NULL;
5534 GList *found = NULL;
5537 /* buffering messages must be aggregated as there might be multiple
5538 * multiqueue in the pipeline and their independent buffering messages
5539 * will confuse the application
5541 * decodebin keeps a list of messages received from elements that are
5544 * 1) Always post the smaller buffering %
5545 * 2) If an element posts a 100% buffering message, remove it from the list
5546 * 3) When there are no more messages on the list, post 100% message
5547 * 4) When an element posts a new buffering message, update the one
5548 * on the list to this new value
5551 BUFFERING_LOCK (dbin);
5552 gst_message_parse_buffering (msg, &msg_perc);
5554 GST_DEBUG_OBJECT (dbin, "Got buffering msg %" GST_PTR_FORMAT, msg);
5556 g_mutex_lock (&dbin->buffering_post_lock);
5559 * Single loop for 2 things:
5560 * 1) Look for a message with the same source
5561 * 1.1) If the received message is 100%, remove it from the list
5562 * 2) Find the minimum buffering from the list
5564 for (iter = dbin->buffering_status; iter;) {
5565 GstMessage *bufstats = iter->data;
5566 if (GST_MESSAGE_SRC (bufstats) == GST_MESSAGE_SRC (msg)) {
5568 if (msg_perc < 100) {
5569 GST_DEBUG_OBJECT (dbin, "Replacing old buffering msg %"
5570 GST_PTR_FORMAT, iter->data);
5571 gst_message_unref (iter->data);
5572 bufstats = iter->data = gst_message_ref (msg);
5574 GList *current = iter;
5576 /* remove the element here and avoid confusing the loop */
5577 iter = g_list_next (iter);
5579 GST_DEBUG_OBJECT (dbin, "Deleting old buffering msg %"
5580 GST_PTR_FORMAT, current->data);
5582 gst_message_unref (current->data);
5583 dbin->buffering_status =
5584 g_list_delete_link (dbin->buffering_status, current);
5590 gst_message_parse_buffering (bufstats, &perc);
5591 if (perc < smaller_perc) {
5592 smaller_perc = perc;
5595 iter = g_list_next (iter);
5598 if (found == NULL && msg_perc < 100) {
5599 if (msg_perc < smaller_perc) {
5600 smaller_perc = msg_perc;
5603 GST_DEBUG_OBJECT (dbin, "Storing buffering msg %" GST_PTR_FORMAT, msg);
5604 dbin->buffering_status =
5605 g_list_prepend (dbin->buffering_status, gst_message_ref (msg));
5608 /* now compute the buffering message that should be posted */
5609 if (smaller_perc == 100) {
5610 g_assert (dbin->buffering_status == NULL);
5611 /* we are posting the original received msg */
5613 gst_message_replace (&msg, smaller);
5615 BUFFERING_UNLOCK (dbin);
5617 GST_DEBUG_OBJECT (dbin, "Forwarding buffering msg %" GST_PTR_FORMAT, msg);
5618 GST_BIN_CLASS (parent_class)->handle_message (bin, msg);
5620 g_mutex_unlock (&dbin->buffering_post_lock);
5625 gst_message_unref (msg);
5627 GST_DEBUG_OBJECT (dbin, "Forwarding msg %" GST_PTR_FORMAT, msg);
5628 GST_BIN_CLASS (parent_class)->handle_message (bin, msg);
5633 gst_decode_bin_remove_element (GstBin * bin, GstElement * element)
5635 GstDecodeBin *dbin = GST_DECODE_BIN (bin);
5636 gboolean removed = FALSE, post = FALSE;
5639 BUFFERING_LOCK (bin);
5640 g_mutex_lock (&dbin->buffering_post_lock);
5641 for (iter = dbin->buffering_status; iter; iter = iter->next) {
5642 GstMessage *bufstats = iter->data;
5644 if (GST_MESSAGE_SRC (bufstats) == GST_OBJECT_CAST (element) ||
5645 gst_object_has_as_ancestor (GST_MESSAGE_SRC (bufstats),
5646 GST_OBJECT_CAST (element))) {
5647 gst_message_unref (bufstats);
5648 dbin->buffering_status =
5649 g_list_delete_link (dbin->buffering_status, iter);
5655 if (removed && dbin->buffering_status == NULL)
5657 BUFFERING_UNLOCK (bin);
5660 gst_element_post_message (GST_ELEMENT_CAST (bin),
5661 gst_message_new_buffering (GST_OBJECT_CAST (dbin), 100));
5663 g_mutex_unlock (&dbin->buffering_post_lock);
5665 return GST_BIN_CLASS (parent_class)->remove_element (bin, element);
5669 gst_decode_bin_plugin_init (GstPlugin * plugin)
5671 GST_DEBUG_CATEGORY_INIT (gst_decode_bin_debug, "decodebin", 0, "decoder bin");
5673 /* Register some quarks here for the stream topology message */
5674 topology_structure_name = g_quark_from_static_string ("stream-topology");
5675 topology_caps = g_quark_from_static_string ("caps");
5676 topology_next = g_quark_from_static_string ("next");
5677 topology_pad = g_quark_from_static_string ("pad");
5678 topology_element_srcpad = g_quark_from_static_string ("element-srcpad");
5680 return gst_element_register (plugin, "decodebin", GST_RANK_NONE,
5681 GST_TYPE_DECODE_BIN);