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.
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
24 * SECTION:element-decodebin
26 * #GstBin that auto-magically constructs a decoding pipeline using available
27 * decoders and demuxers via auto-plugging.
29 * decodebin is considered stable now and replaces the old #decodebin element.
30 * #uridecodebin uses decodebin internally and is often more convenient to
31 * use, as it creates a suitable source element as well.
34 /* Implementation notes:
36 * The following section describes how decodebin works internally.
38 * The first part of decodebin is its typefind element, which tries
39 * to determine the media type of the input stream. If the type is found
40 * autoplugging starts.
42 * decodebin internally organizes the elements it autoplugged into GstDecodeChains
43 * and GstDecodeGroups. A decode chain is a single chain of decoding, this
44 * means that if decodebin every autoplugs an element with two+ srcpads
45 * (e.g. a demuxer) this will end the chain and everything following this
46 * demuxer will be put into decode groups below the chain. Otherwise,
47 * if an element has a single srcpad that outputs raw data the decode chain
48 * is ended too and a GstDecodePad is stored and blocked.
50 * A decode group combines a number of chains that are created by a
51 * demuxer element. All those chains are connected through a multiqueue to
52 * the demuxer. A new group for the same demuxer is only created if the
53 * demuxer has signaled no-more-pads, in which case all following pads
54 * create a new chain in the new group.
56 * This continues until the top-level decode chain is complete. A decode
57 * chain is complete if it either ends with a blocked endpad, if autoplugging
58 * stopped because no suitable plugins could be found or if the active group
59 * is complete. A decode group on the other hand is complete if all child
60 * chains are complete.
62 * If this happens at some point, all endpads of all active groups are exposed.
63 * For this decodebin adds the endpads, signals no-more-pads and then unblocks
64 * them. Now playback starts.
66 * If one of the chains that end on a endpad receives EOS decodebin checks
67 * if all chains and groups are drained. In that case everything goes into EOS.
68 * If there is a chain where the active group is drained but there exist next
69 * groups, the active group is hidden (endpads are removed) and the next group
70 * is exposed. This means that in some cases more pads may be created even
71 * after the initial no-more-pads signal. This happens for example with
72 * so-called "chained oggs", most commonly found among ogg/vorbis internet
75 * Note 1: If we're talking about blocked endpads this really means that the
76 * *target* pads of the endpads are blocked. Pads that are exposed to the outside
77 * should never ever be blocked!
79 * Note 2: If a group is complete and the parent's chain demuxer adds new pads
80 * but never signaled no-more-pads this additional pads will be ignored!
84 /* FIXME 0.11: suppress warnings for deprecated API such as GValueArray
85 * with newer GLib versions (>= 2.31.0) */
86 #define GLIB_DISABLE_DEPRECATION_WARNINGS
92 #include <gst/gst-i18n-plugin.h>
96 #include <gst/pbutils/pbutils.h>
98 #include "gstplay-enum.h"
99 #include "gstplayback.h"
100 #include "gstrawcaps.h"
102 #include "gst/glib-compat-private.h"
104 /* generic templates */
105 static GstStaticPadTemplate decoder_bin_sink_template =
106 GST_STATIC_PAD_TEMPLATE ("sink",
109 GST_STATIC_CAPS_ANY);
111 static GstStaticPadTemplate decoder_bin_src_template =
112 GST_STATIC_PAD_TEMPLATE ("src_%u",
115 GST_STATIC_CAPS_ANY);
117 GST_DEBUG_CATEGORY_STATIC (gst_decode_bin_debug);
118 #define GST_CAT_DEFAULT gst_decode_bin_debug
120 typedef struct _GstPendingPad GstPendingPad;
121 typedef struct _GstDecodeElement GstDecodeElement;
122 typedef struct _GstDecodeChain GstDecodeChain;
123 typedef struct _GstDecodeGroup GstDecodeGroup;
124 typedef struct _GstDecodePad GstDecodePad;
125 typedef GstGhostPadClass GstDecodePadClass;
126 typedef struct _GstDecodeBin GstDecodeBin;
127 typedef struct _GstDecodeBinClass GstDecodeBinClass;
129 #define GST_TYPE_DECODE_BIN (gst_decode_bin_get_type())
130 #define GST_DECODE_BIN_CAST(obj) ((GstDecodeBin*)(obj))
131 #define GST_DECODE_BIN(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_DECODE_BIN,GstDecodeBin))
132 #define GST_DECODE_BIN_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_DECODE_BIN,GstDecodeBinClass))
133 #define GST_IS_DECODE_BIN(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_DECODE_BIN))
134 #define GST_IS_DECODE_BIN_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_DECODE_BIN))
139 * The opaque #GstDecodeBin data structure
143 GstBin bin; /* we extend GstBin */
146 GstCaps *caps; /* caps on which to stop decoding */
147 gchar *encoding; /* encoding of subtitles */
148 gboolean use_buffering; /* configure buffering on multiqueues */
151 guint max_size_bytes;
152 guint max_size_buffers;
153 guint64 max_size_time;
154 gboolean post_stream_topology;
156 GstElement *typefind; /* this holds the typefind object */
158 GMutex *expose_lock; /* Protects exposal and removal of groups */
159 GstDecodeChain *decode_chain; /* Top level decode chain */
160 guint nbpads; /* unique identifier for source pads */
162 GMutex *factories_lock;
163 guint32 factories_cookie; /* Cookie from last time when factories was updated */
164 GList *factories; /* factories we can use for selecting elements */
166 GMutex *subtitle_lock; /* Protects changes to subtitles and encoding */
167 GList *subtitles; /* List of elements with subtitle-encoding,
168 * protected by above mutex! */
170 gboolean have_type; /* if we received the have_type signal */
171 guint have_type_id; /* signal id for have-type from typefind */
173 gboolean async_pending; /* async-start has been emitted */
175 GMutex *dyn_lock; /* lock protecting pad blocking */
176 gboolean shutdown; /* if we are shutting down */
177 GList *blocked_pads; /* pads that have set to block */
179 gboolean expose_allstreams; /* Whether to expose unknow type streams or not */
181 GList *filtered; /* elements for which error messages are filtered */
184 struct _GstDecodeBinClass
186 GstBinClass parent_class;
188 /* signal fired when we found a pad that we cannot decode */
189 void (*unknown_type) (GstElement * element, GstPad * pad, GstCaps * caps);
191 /* signal fired to know if we continue trying to decode the given caps */
192 gboolean (*autoplug_continue) (GstElement * element, GstPad * pad,
194 /* signal fired to get a list of factories to try to autoplug */
195 GValueArray *(*autoplug_factories) (GstElement * element, GstPad * pad,
197 /* signal fired to sort the factories */
198 GValueArray *(*autoplug_sort) (GstElement * element, GstPad * pad,
199 GstCaps * caps, GValueArray * factories);
200 /* signal fired to select from the proposed list of factories */
201 GstAutoplugSelectResult (*autoplug_select) (GstElement * element,
202 GstPad * pad, GstCaps * caps, GstElementFactory * factory);
204 /* fired when the last group is drained */
205 void (*drained) (GstElement * element);
212 SIGNAL_AUTOPLUG_CONTINUE,
213 SIGNAL_AUTOPLUG_FACTORIES,
214 SIGNAL_AUTOPLUG_SELECT,
215 SIGNAL_AUTOPLUG_SORT,
220 /* automatic sizes, while prerolling we buffer up to 2MB, we ignore time
221 * and buffers in this case. */
222 #define AUTO_PREROLL_SIZE_BYTES 2 * 1024 * 1024
223 #define AUTO_PREROLL_SIZE_BUFFERS 0
224 #define AUTO_PREROLL_NOT_SEEKABLE_SIZE_TIME 10 * GST_SECOND
225 #define AUTO_PREROLL_SEEKABLE_SIZE_TIME 0
227 /* whan playing, keep a max of 2MB of data but try to keep the number of buffers
228 * as low as possible (try to aim for 5 buffers) */
229 #define AUTO_PLAY_SIZE_BYTES 2 * 1024 * 1024
230 #define AUTO_PLAY_SIZE_BUFFERS 5
231 #define AUTO_PLAY_SIZE_TIME 0
233 #define DEFAULT_SUBTITLE_ENCODING NULL
234 #define DEFAULT_USE_BUFFERING FALSE
235 #define DEFAULT_LOW_PERCENT 10
236 #define DEFAULT_HIGH_PERCENT 99
237 /* by default we use the automatic values above */
238 #define DEFAULT_MAX_SIZE_BYTES 0
239 #define DEFAULT_MAX_SIZE_BUFFERS 0
240 #define DEFAULT_MAX_SIZE_TIME 0
241 #define DEFAULT_POST_STREAM_TOPOLOGY FALSE
242 #define DEFAULT_EXPOSE_ALL_STREAMS TRUE
249 PROP_SUBTITLE_ENCODING,
255 PROP_MAX_SIZE_BUFFERS,
257 PROP_POST_STREAM_TOPOLOGY,
258 PROP_EXPOSE_ALL_STREAMS,
262 static GstBinClass *parent_class;
263 static guint gst_decode_bin_signals[LAST_SIGNAL] = { 0 };
265 static GstStaticCaps default_raw_caps = GST_STATIC_CAPS (DEFAULT_RAW_CAPS);
267 static void do_async_start (GstDecodeBin * dbin);
268 static void do_async_done (GstDecodeBin * dbin);
270 static void type_found (GstElement * typefind, guint probability,
271 GstCaps * caps, GstDecodeBin * decode_bin);
273 static void decodebin_set_queue_size (GstDecodeBin * dbin,
274 GstElement * multiqueue, gboolean preroll, gboolean seekable);
276 static gboolean gst_decode_bin_autoplug_continue (GstElement * element,
277 GstPad * pad, GstCaps * caps);
278 static GValueArray *gst_decode_bin_autoplug_factories (GstElement *
279 element, GstPad * pad, GstCaps * caps);
280 static GValueArray *gst_decode_bin_autoplug_sort (GstElement * element,
281 GstPad * pad, GstCaps * caps, GValueArray * factories);
282 static GstAutoplugSelectResult gst_decode_bin_autoplug_select (GstElement *
283 element, GstPad * pad, GstCaps * caps, GstElementFactory * factory);
285 static void gst_decode_bin_set_property (GObject * object, guint prop_id,
286 const GValue * value, GParamSpec * pspec);
287 static void gst_decode_bin_get_property (GObject * object, guint prop_id,
288 GValue * value, GParamSpec * pspec);
289 static void gst_decode_bin_set_caps (GstDecodeBin * dbin, GstCaps * caps);
290 static GstCaps *gst_decode_bin_get_caps (GstDecodeBin * dbin);
291 static void caps_notify_cb (GstPad * pad, GParamSpec * unused,
292 GstDecodeChain * chain);
294 static GstPad *find_sink_pad (GstElement * element);
295 static GstStateChangeReturn gst_decode_bin_change_state (GstElement * element,
296 GstStateChange transition);
297 static void gst_decode_bin_handle_message (GstBin * bin, GstMessage * message);
299 static gboolean check_upstream_seekable (GstDecodeBin * dbin, GstPad * pad);
301 #define EXPOSE_LOCK(dbin) G_STMT_START { \
302 GST_LOG_OBJECT (dbin, \
303 "expose locking from thread %p", \
305 g_mutex_lock (GST_DECODE_BIN_CAST(dbin)->expose_lock); \
306 GST_LOG_OBJECT (dbin, \
307 "expose locked from thread %p", \
311 #define EXPOSE_UNLOCK(dbin) G_STMT_START { \
312 GST_LOG_OBJECT (dbin, \
313 "expose unlocking from thread %p", \
315 g_mutex_unlock (GST_DECODE_BIN_CAST(dbin)->expose_lock); \
318 #define DYN_LOCK(dbin) G_STMT_START { \
319 GST_LOG_OBJECT (dbin, \
320 "dynlocking from thread %p", \
322 g_mutex_lock (GST_DECODE_BIN_CAST(dbin)->dyn_lock); \
323 GST_LOG_OBJECT (dbin, \
324 "dynlocked from thread %p", \
328 #define DYN_UNLOCK(dbin) G_STMT_START { \
329 GST_LOG_OBJECT (dbin, \
330 "dynunlocking from thread %p", \
332 g_mutex_unlock (GST_DECODE_BIN_CAST(dbin)->dyn_lock); \
335 #define SUBTITLE_LOCK(dbin) G_STMT_START { \
336 GST_LOG_OBJECT (dbin, \
337 "subtitle locking from thread %p", \
339 g_mutex_lock (GST_DECODE_BIN_CAST(dbin)->subtitle_lock); \
340 GST_LOG_OBJECT (dbin, \
341 "subtitle lock from thread %p", \
345 #define SUBTITLE_UNLOCK(dbin) G_STMT_START { \
346 GST_LOG_OBJECT (dbin, \
347 "subtitle unlocking from thread %p", \
349 g_mutex_unlock (GST_DECODE_BIN_CAST(dbin)->subtitle_lock); \
352 struct _GstPendingPad
355 GstDecodeChain *chain;
356 gulong event_probe_id;
359 struct _GstDecodeElement
362 GstElement *capsfilter; /* Optional capsfilter for Parser/Convert */
367 * Streams belonging to the same group/chain of a media file
369 * When changing something here lock the parent chain!
371 struct _GstDecodeGroup
374 GstDecodeChain *parent;
376 GstElement *multiqueue; /* Used for linking all child chains */
377 gulong overrunsig; /* the overrun signal for multiqueue */
379 gboolean overrun; /* TRUE if the multiqueue signaled overrun. This
380 * means that we should really expose the group */
382 gboolean no_more_pads; /* TRUE if the demuxer signaled no-more-pads */
383 gboolean drained; /* TRUE if the all children are drained */
385 GList *children; /* List of GstDecodeChains in this group */
387 GList *reqpads; /* List of RequestPads for multiqueue, there is
388 * exactly one RequestPad per child chain */
391 struct _GstDecodeChain
393 GstDecodeGroup *parent;
396 GMutex *lock; /* Protects this chain and its groups */
398 GstPad *pad; /* srcpad that caused creation of this chain */
400 gboolean demuxer; /* TRUE if elements->data is a demuxer */
401 gboolean seekable; /* TRUE if this chain ends on a demuxer and is seekable */
402 GList *elements; /* All elements in this group, first
403 is the latest and most downstream element */
405 /* Note: there are only groups if the last element of this chain
406 * is a demuxer, otherwise the chain will end with an endpad.
407 * The other way around this means, that endpad only exists if this
408 * chain doesn't end with a demuxer! */
410 GstDecodeGroup *active_group; /* Currently active group */
411 GList *next_groups; /* head is newest group, tail is next group.
412 a new group will be created only if the head
413 group had no-more-pads. If it's only exposed
414 all new pads will be ignored! */
415 GList *pending_pads; /* Pads that have no fixed caps yet */
417 GstDecodePad *endpad; /* Pad of this chain that could be exposed */
418 gboolean deadend; /* This chain is incomplete and can't be completed,
419 e.g. no suitable decoder could be found
420 e.g. stream got EOS without buffers
422 GstCaps *endcaps; /* Caps that were used when linking to the endpad
423 or that resulted in the deadend
426 /* FIXME: This should be done directly via a thread! */
427 GList *old_groups; /* Groups that should be freed later */
430 static void gst_decode_chain_free (GstDecodeChain * chain);
431 static GstDecodeChain *gst_decode_chain_new (GstDecodeBin * dbin,
432 GstDecodeGroup * group, GstPad * pad);
433 static void gst_decode_group_hide (GstDecodeGroup * group);
434 static void gst_decode_group_free (GstDecodeGroup * group);
435 static GstDecodeGroup *gst_decode_group_new (GstDecodeBin * dbin,
436 GstDecodeChain * chain);
437 static gboolean gst_decode_chain_is_complete (GstDecodeChain * chain);
438 static gboolean gst_decode_chain_expose (GstDecodeChain * chain,
439 GList ** endpads, gboolean * missing_plugin);
440 static gboolean gst_decode_chain_is_drained (GstDecodeChain * chain);
441 static gboolean gst_decode_group_is_complete (GstDecodeGroup * group);
442 static GstPad *gst_decode_group_control_demuxer_pad (GstDecodeGroup * group,
444 static gboolean gst_decode_group_is_drained (GstDecodeGroup * group);
446 static gboolean gst_decode_bin_expose (GstDecodeBin * dbin);
448 #define CHAIN_MUTEX_LOCK(chain) G_STMT_START { \
449 GST_LOG_OBJECT (chain->dbin, \
450 "locking chain %p from thread %p", \
451 chain, g_thread_self ()); \
452 g_mutex_lock (chain->lock); \
453 GST_LOG_OBJECT (chain->dbin, \
454 "locked chain %p from thread %p", \
455 chain, g_thread_self ()); \
458 #define CHAIN_MUTEX_UNLOCK(chain) G_STMT_START { \
459 GST_LOG_OBJECT (chain->dbin, \
460 "unlocking chain %p from thread %p", \
461 chain, g_thread_self ()); \
462 g_mutex_unlock (chain->lock); \
467 * GstPad private used for source pads of chains
473 GstDecodeChain *chain;
475 gboolean blocked; /* the *target* pad is blocked */
476 gboolean exposed; /* the pad is exposed */
477 gboolean drained; /* an EOS has been seen on the pad */
482 GType gst_decode_pad_get_type (void);
483 G_DEFINE_TYPE (GstDecodePad, gst_decode_pad, GST_TYPE_GHOST_PAD);
484 #define GST_TYPE_DECODE_PAD (gst_decode_pad_get_type ())
485 #define GST_DECODE_PAD(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_DECODE_PAD,GstDecodePad))
487 static GstDecodePad *gst_decode_pad_new (GstDecodeBin * dbin, GstPad * pad,
488 GstDecodeChain * chain);
489 static void gst_decode_pad_activate (GstDecodePad * dpad,
490 GstDecodeChain * chain);
491 static void gst_decode_pad_unblock (GstDecodePad * dpad);
492 static void gst_decode_pad_set_blocked (GstDecodePad * dpad, gboolean blocked);
494 static void gst_pending_pad_free (GstPendingPad * ppad);
495 static GstPadProbeReturn pad_event_cb (GstPad * pad, GstPadProbeInfo * info,
498 /********************************
499 * Standard GObject boilerplate *
500 ********************************/
502 static void gst_decode_bin_class_init (GstDecodeBinClass * klass);
503 static void gst_decode_bin_init (GstDecodeBin * decode_bin);
504 static void gst_decode_bin_dispose (GObject * object);
505 static void gst_decode_bin_finalize (GObject * object);
508 gst_decode_bin_get_type (void)
510 static GType gst_decode_bin_type = 0;
512 if (!gst_decode_bin_type) {
513 static const GTypeInfo gst_decode_bin_info = {
514 sizeof (GstDecodeBinClass),
517 (GClassInitFunc) gst_decode_bin_class_init,
520 sizeof (GstDecodeBin),
522 (GInstanceInitFunc) gst_decode_bin_init,
526 gst_decode_bin_type =
527 g_type_register_static (GST_TYPE_BIN, "GstDecodeBin",
528 &gst_decode_bin_info, 0);
531 return gst_decode_bin_type;
535 _gst_boolean_accumulator (GSignalInvocationHint * ihint,
536 GValue * return_accu, const GValue * handler_return, gpointer dummy)
540 myboolean = g_value_get_boolean (handler_return);
541 if (!(ihint->run_type & G_SIGNAL_RUN_CLEANUP))
542 g_value_set_boolean (return_accu, myboolean);
544 /* stop emission if FALSE */
548 /* we collect the first result */
550 _gst_array_accumulator (GSignalInvocationHint * ihint,
551 GValue * return_accu, const GValue * handler_return, gpointer dummy)
555 array = g_value_get_boxed (handler_return);
556 if (!(ihint->run_type & G_SIGNAL_RUN_CLEANUP))
557 g_value_set_boxed (return_accu, array);
563 _gst_select_accumulator (GSignalInvocationHint * ihint,
564 GValue * return_accu, const GValue * handler_return, gpointer dummy)
566 GstAutoplugSelectResult res;
568 res = g_value_get_enum (handler_return);
569 if (!(ihint->run_type & G_SIGNAL_RUN_CLEANUP))
570 g_value_set_enum (return_accu, res);
576 _gst_array_hasvalue_accumulator (GSignalInvocationHint * ihint,
577 GValue * return_accu, const GValue * handler_return, gpointer dummy)
581 array = g_value_get_boxed (handler_return);
582 if (!(ihint->run_type & G_SIGNAL_RUN_CLEANUP))
583 g_value_set_boxed (return_accu, array);
592 gst_decode_bin_class_init (GstDecodeBinClass * klass)
594 GObjectClass *gobject_klass;
595 GstElementClass *gstelement_klass;
596 GstBinClass *gstbin_klass;
598 gobject_klass = (GObjectClass *) klass;
599 gstelement_klass = (GstElementClass *) klass;
600 gstbin_klass = (GstBinClass *) klass;
602 parent_class = g_type_class_peek_parent (klass);
604 gobject_klass->dispose = gst_decode_bin_dispose;
605 gobject_klass->finalize = gst_decode_bin_finalize;
606 gobject_klass->set_property = gst_decode_bin_set_property;
607 gobject_klass->get_property = gst_decode_bin_get_property;
610 * GstDecodeBin::unknown-type:
611 * @bin: The decodebin.
612 * @pad: The new pad containing caps that cannot be resolved to a 'final'
614 * @caps: The #GstCaps of the pad that cannot be resolved.
616 * This signal is emitted when a pad for which there is no further possible
617 * decoding is added to the decodebin.
619 gst_decode_bin_signals[SIGNAL_UNKNOWN_TYPE] =
620 g_signal_new ("unknown-type", G_TYPE_FROM_CLASS (klass),
621 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstDecodeBinClass, unknown_type),
622 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 2,
623 GST_TYPE_PAD, GST_TYPE_CAPS);
626 * GstDecodeBin::autoplug-continue:
627 * @bin: The decodebin.
629 * @caps: The #GstCaps found.
631 * This signal is emitted whenever decodebin finds a new stream. It is
632 * emitted before looking for any elements that can handle that stream.
635 * Invocation of signal handlers stops after the first signal handler
636 * returns #FALSE. Signal handlers are invoked in the order they were
640 * Returns: #TRUE if you wish decodebin to look for elements that can
641 * handle the given @caps. If #FALSE, those caps will be considered as
642 * final and the pad will be exposed as such (see 'pad-added' signal of
645 gst_decode_bin_signals[SIGNAL_AUTOPLUG_CONTINUE] =
646 g_signal_new ("autoplug-continue", G_TYPE_FROM_CLASS (klass),
647 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstDecodeBinClass, autoplug_continue),
648 _gst_boolean_accumulator, NULL, g_cclosure_marshal_generic,
649 G_TYPE_BOOLEAN, 2, GST_TYPE_PAD, GST_TYPE_CAPS);
652 * GstDecodeBin::autoplug-factories:
653 * @bin: The decodebin.
655 * @caps: The #GstCaps found.
657 * This function is emited when an array of possible factories for @caps on
658 * @pad is needed. Decodebin will by default return an array with all
659 * compatible factories, sorted by rank.
661 * If this function returns NULL, @pad will be exposed as a final caps.
663 * If this function returns an empty array, the pad will be considered as
664 * having an unhandled type media type.
667 * Only the signal handler that is connected first will ever by invoked.
668 * Don't connect signal handlers with the #G_CONNECT_AFTER flag to this
669 * signal, they will never be invoked!
672 * Returns: a #GValueArray* with a list of factories to try. The factories are
673 * by default tried in the returned order or based on the index returned by
676 gst_decode_bin_signals[SIGNAL_AUTOPLUG_FACTORIES] =
677 g_signal_new ("autoplug-factories", G_TYPE_FROM_CLASS (klass),
678 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstDecodeBinClass,
679 autoplug_factories), _gst_array_accumulator, NULL,
680 g_cclosure_marshal_generic, G_TYPE_VALUE_ARRAY, 2,
681 GST_TYPE_PAD, GST_TYPE_CAPS);
684 * GstDecodeBin::autoplug-sort:
685 * @bin: The decodebin.
687 * @caps: The #GstCaps.
688 * @factories: A #GValueArray of possible #GstElementFactory to use.
690 * Once decodebin has found the possible #GstElementFactory objects to try
691 * for @caps on @pad, this signal is emited. The purpose of the signal is for
692 * the application to perform additional sorting or filtering on the element
695 * The callee should copy and modify @factories or return #NULL if the
696 * order should not change.
699 * Invocation of signal handlers stops after one signal handler has
700 * returned something else than #NULL. Signal handlers are invoked in
701 * the order they were connected in.
702 * Don't connect signal handlers with the #G_CONNECT_AFTER flag to this
703 * signal, they will never be invoked!
706 * Returns: A new sorted array of #GstElementFactory objects.
708 gst_decode_bin_signals[SIGNAL_AUTOPLUG_SORT] =
709 g_signal_new ("autoplug-sort", G_TYPE_FROM_CLASS (klass),
710 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstDecodeBinClass, autoplug_sort),
711 _gst_array_hasvalue_accumulator, NULL,
712 g_cclosure_marshal_generic, G_TYPE_VALUE_ARRAY, 3,
713 GST_TYPE_PAD, GST_TYPE_CAPS, G_TYPE_VALUE_ARRAY);
716 * GstDecodeBin::autoplug-select:
717 * @bin: The decodebin.
719 * @caps: The #GstCaps.
720 * @factory: A #GstElementFactory to use.
722 * This signal is emitted once decodebin has found all the possible
723 * #GstElementFactory that can be used to handle the given @caps. For each of
724 * those factories, this signal is emitted.
726 * The signal handler should return a #GST_TYPE_AUTOPLUG_SELECT_RESULT enum
727 * value indicating what decodebin should do next.
729 * A value of #GST_AUTOPLUG_SELECT_TRY will try to autoplug an element from
732 * A value of #GST_AUTOPLUG_SELECT_EXPOSE will expose @pad without plugging
735 * A value of #GST_AUTOPLUG_SELECT_SKIP will skip @factory and move to the
739 * Only the signal handler that is connected first will ever by invoked.
740 * Don't connect signal handlers with the #G_CONNECT_AFTER flag to this
741 * signal, they will never be invoked!
744 * Returns: a #GST_TYPE_AUTOPLUG_SELECT_RESULT that indicates the required
745 * operation. the default handler will always return
746 * #GST_AUTOPLUG_SELECT_TRY.
748 gst_decode_bin_signals[SIGNAL_AUTOPLUG_SELECT] =
749 g_signal_new ("autoplug-select", G_TYPE_FROM_CLASS (klass),
750 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstDecodeBinClass, autoplug_select),
751 _gst_select_accumulator, NULL,
752 g_cclosure_marshal_generic,
753 GST_TYPE_AUTOPLUG_SELECT_RESULT, 3, GST_TYPE_PAD, GST_TYPE_CAPS,
754 GST_TYPE_ELEMENT_FACTORY);
757 * GstDecodeBin::drained
758 * @bin: The decodebin
760 * This signal is emitted once decodebin has finished decoding all the data.
764 gst_decode_bin_signals[SIGNAL_DRAINED] =
765 g_signal_new ("drained", G_TYPE_FROM_CLASS (klass),
766 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstDecodeBinClass, drained),
767 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 0, G_TYPE_NONE);
769 g_object_class_install_property (gobject_klass, PROP_CAPS,
770 g_param_spec_boxed ("caps", "Caps", "The caps on which to stop decoding.",
771 GST_TYPE_CAPS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
773 g_object_class_install_property (gobject_klass, PROP_SUBTITLE_ENCODING,
774 g_param_spec_string ("subtitle-encoding", "subtitle encoding",
775 "Encoding to assume if input subtitles are not in UTF-8 encoding. "
776 "If not set, the GST_SUBTITLE_ENCODING environment variable will "
777 "be checked for an encoding to use. If that is not set either, "
778 "ISO-8859-15 will be assumed.", NULL,
779 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
781 g_object_class_install_property (gobject_klass, PROP_SINK_CAPS,
782 g_param_spec_boxed ("sink-caps", "Sink Caps",
783 "The caps of the input data. (NULL = use typefind element)",
784 GST_TYPE_CAPS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
787 * GstDecodeBin::use-buffering
789 * Activate buffering in decodebin. This will instruct the multiqueues behind
790 * decoders to emit BUFFERING messages.
794 g_object_class_install_property (gobject_klass, PROP_USE_BUFFERING,
795 g_param_spec_boolean ("use-buffering", "Use Buffering",
796 "Emit GST_MESSAGE_BUFFERING based on low-/high-percent thresholds",
797 DEFAULT_USE_BUFFERING, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
800 * GstDecodeBin:low-percent
802 * Low threshold percent for buffering to start.
806 g_object_class_install_property (gobject_klass, PROP_LOW_PERCENT,
807 g_param_spec_int ("low-percent", "Low percent",
808 "Low threshold for buffering to start", 0, 100,
809 DEFAULT_LOW_PERCENT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
811 * GstDecodeBin:high-percent
813 * High threshold percent for buffering to finish.
817 g_object_class_install_property (gobject_klass, PROP_HIGH_PERCENT,
818 g_param_spec_int ("high-percent", "High percent",
819 "High threshold for buffering to finish", 0, 100,
820 DEFAULT_HIGH_PERCENT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
823 * GstDecodeBin:max-size-bytes
825 * Max amount of bytes in the queue (0=automatic).
829 g_object_class_install_property (gobject_klass, PROP_MAX_SIZE_BYTES,
830 g_param_spec_uint ("max-size-bytes", "Max. size (bytes)",
831 "Max. amount of bytes in the queue (0=automatic)",
832 0, G_MAXUINT, DEFAULT_MAX_SIZE_BYTES,
833 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
835 * GstDecodeBin:max-size-buffers
837 * Max amount of buffers in the queue (0=automatic).
841 g_object_class_install_property (gobject_klass, PROP_MAX_SIZE_BUFFERS,
842 g_param_spec_uint ("max-size-buffers", "Max. size (buffers)",
843 "Max. number of buffers in the queue (0=automatic)",
844 0, G_MAXUINT, DEFAULT_MAX_SIZE_BUFFERS,
845 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
847 * GstDecodeBin:max-size-time
849 * Max amount of time in the queue (in ns, 0=automatic).
853 g_object_class_install_property (gobject_klass, PROP_MAX_SIZE_TIME,
854 g_param_spec_uint64 ("max-size-time", "Max. size (ns)",
855 "Max. amount of data in the queue (in ns, 0=automatic)",
857 DEFAULT_MAX_SIZE_TIME, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
860 * GstDecodeBin::post-stream-topology
862 * Post stream-topology messages on the bus every time the topology changes.
866 g_object_class_install_property (gobject_klass, PROP_POST_STREAM_TOPOLOGY,
867 g_param_spec_boolean ("post-stream-topology", "Post Stream Topology",
868 "Post stream-topology messages",
869 DEFAULT_POST_STREAM_TOPOLOGY,
870 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
873 * GstDecodeBin::expose-all-streams
875 * Expose streams of unknown type.
877 * If set to %FALSE, then only the streams that can be decoded to the final
878 * caps (see 'caps' property) will have a pad exposed. Streams that do not
879 * match those caps but could have been decoded will not have decoder plugged
880 * in internally and will not have a pad exposed.
884 g_object_class_install_property (gobject_klass, PROP_EXPOSE_ALL_STREAMS,
885 g_param_spec_boolean ("expose-all-streams", "Expose All Streams",
886 "Expose all streams, including those of unknown type or that don't match the 'caps' property",
887 DEFAULT_EXPOSE_ALL_STREAMS,
888 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
892 klass->autoplug_continue =
893 GST_DEBUG_FUNCPTR (gst_decode_bin_autoplug_continue);
894 klass->autoplug_factories =
895 GST_DEBUG_FUNCPTR (gst_decode_bin_autoplug_factories);
896 klass->autoplug_sort = GST_DEBUG_FUNCPTR (gst_decode_bin_autoplug_sort);
897 klass->autoplug_select = GST_DEBUG_FUNCPTR (gst_decode_bin_autoplug_select);
899 gst_element_class_add_pad_template (gstelement_klass,
900 gst_static_pad_template_get (&decoder_bin_sink_template));
901 gst_element_class_add_pad_template (gstelement_klass,
902 gst_static_pad_template_get (&decoder_bin_src_template));
904 gst_element_class_set_static_metadata (gstelement_klass,
905 "Decoder Bin", "Generic/Bin/Decoder",
906 "Autoplug and decode to raw media",
907 "Edward Hervey <edward.hervey@collabora.co.uk>, "
908 "Sebastian Dröge <sebastian.droege@collabora.co.uk>");
910 gstelement_klass->change_state =
911 GST_DEBUG_FUNCPTR (gst_decode_bin_change_state);
913 gstbin_klass->handle_message =
914 GST_DEBUG_FUNCPTR (gst_decode_bin_handle_message);
917 /* Must be called with factories lock! */
919 gst_decode_bin_update_factories_list (GstDecodeBin * dbin)
923 cookie = gst_registry_get_feature_list_cookie (gst_registry_get ());
924 if (!dbin->factories || dbin->factories_cookie != cookie) {
926 gst_plugin_feature_list_free (dbin->factories);
928 gst_element_factory_list_get_elements
929 (GST_ELEMENT_FACTORY_TYPE_DECODABLE, GST_RANK_MARGINAL);
930 dbin->factories_cookie = cookie;
935 gst_decode_bin_init (GstDecodeBin * decode_bin)
937 /* first filter out the interesting element factories */
938 decode_bin->factories_lock = g_mutex_new ();
940 /* we create the typefind element only once */
941 decode_bin->typefind = gst_element_factory_make ("typefind", "typefind");
942 if (!decode_bin->typefind) {
943 g_warning ("can't find typefind element, decodebin will not work");
947 GstPadTemplate *pad_tmpl;
949 /* add the typefind element */
950 if (!gst_bin_add (GST_BIN (decode_bin), decode_bin->typefind)) {
951 g_warning ("Could not add typefind element, decodebin will not work");
952 gst_object_unref (decode_bin->typefind);
953 decode_bin->typefind = NULL;
956 /* get the sinkpad */
957 pad = gst_element_get_static_pad (decode_bin->typefind, "sink");
959 /* get the pad template */
960 pad_tmpl = gst_static_pad_template_get (&decoder_bin_sink_template);
962 /* ghost the sink pad to ourself */
963 gpad = gst_ghost_pad_new_from_template ("sink", pad, pad_tmpl);
964 gst_pad_set_active (gpad, TRUE);
965 gst_element_add_pad (GST_ELEMENT (decode_bin), gpad);
967 gst_object_unref (pad_tmpl);
968 gst_object_unref (pad);
970 /* connect a signal to find out when the typefind element found
972 decode_bin->have_type_id =
973 g_signal_connect (G_OBJECT (decode_bin->typefind), "have-type",
974 G_CALLBACK (type_found), decode_bin);
977 decode_bin->expose_lock = g_mutex_new ();
978 decode_bin->decode_chain = NULL;
980 decode_bin->dyn_lock = g_mutex_new ();
981 decode_bin->shutdown = FALSE;
982 decode_bin->blocked_pads = NULL;
984 decode_bin->subtitle_lock = g_mutex_new ();
986 decode_bin->encoding = g_strdup (DEFAULT_SUBTITLE_ENCODING);
987 decode_bin->caps = gst_static_caps_get (&default_raw_caps);
988 decode_bin->use_buffering = DEFAULT_USE_BUFFERING;
989 decode_bin->low_percent = DEFAULT_LOW_PERCENT;
990 decode_bin->high_percent = DEFAULT_HIGH_PERCENT;
992 decode_bin->max_size_bytes = DEFAULT_MAX_SIZE_BYTES;
993 decode_bin->max_size_buffers = DEFAULT_MAX_SIZE_BUFFERS;
994 decode_bin->max_size_time = DEFAULT_MAX_SIZE_TIME;
996 decode_bin->expose_allstreams = DEFAULT_EXPOSE_ALL_STREAMS;
1000 gst_decode_bin_dispose (GObject * object)
1002 GstDecodeBin *decode_bin;
1004 decode_bin = GST_DECODE_BIN (object);
1006 if (decode_bin->factories)
1007 gst_plugin_feature_list_free (decode_bin->factories);
1008 decode_bin->factories = NULL;
1010 if (decode_bin->decode_chain)
1011 gst_decode_chain_free (decode_bin->decode_chain);
1012 decode_bin->decode_chain = NULL;
1014 if (decode_bin->caps)
1015 gst_caps_unref (decode_bin->caps);
1016 decode_bin->caps = NULL;
1018 g_free (decode_bin->encoding);
1019 decode_bin->encoding = NULL;
1021 g_list_free (decode_bin->subtitles);
1022 decode_bin->subtitles = NULL;
1024 G_OBJECT_CLASS (parent_class)->dispose (object);
1028 gst_decode_bin_finalize (GObject * object)
1030 GstDecodeBin *decode_bin;
1032 decode_bin = GST_DECODE_BIN (object);
1034 if (decode_bin->expose_lock) {
1035 g_mutex_free (decode_bin->expose_lock);
1036 decode_bin->expose_lock = NULL;
1039 if (decode_bin->dyn_lock) {
1040 g_mutex_free (decode_bin->dyn_lock);
1041 decode_bin->dyn_lock = NULL;
1044 if (decode_bin->subtitle_lock) {
1045 g_mutex_free (decode_bin->subtitle_lock);
1046 decode_bin->subtitle_lock = NULL;
1049 if (decode_bin->factories_lock) {
1050 g_mutex_free (decode_bin->factories_lock);
1051 decode_bin->factories_lock = NULL;
1054 G_OBJECT_CLASS (parent_class)->finalize (object);
1058 * Changes the caps on which decodebin will stop decoding.
1059 * Will unref the previously set one. The refcount of the given caps will be
1061 * @caps can be NULL.
1066 gst_decode_bin_set_caps (GstDecodeBin * dbin, GstCaps * caps)
1068 GST_DEBUG_OBJECT (dbin, "Setting new caps: %" GST_PTR_FORMAT, caps);
1070 GST_OBJECT_LOCK (dbin);
1071 gst_caps_replace (&dbin->caps, caps);
1072 GST_OBJECT_UNLOCK (dbin);
1076 * Returns the currently configured caps on which decodebin will stop decoding.
1077 * The returned caps (if not NULL), will have its refcount incremented.
1082 gst_decode_bin_get_caps (GstDecodeBin * dbin)
1086 GST_DEBUG_OBJECT (dbin, "Getting currently set caps");
1088 GST_OBJECT_LOCK (dbin);
1091 gst_caps_ref (caps);
1092 GST_OBJECT_UNLOCK (dbin);
1098 gst_decode_bin_set_sink_caps (GstDecodeBin * dbin, GstCaps * caps)
1100 GST_DEBUG_OBJECT (dbin, "Setting new caps: %" GST_PTR_FORMAT, caps);
1102 g_object_set (dbin->typefind, "force-caps", caps, NULL);
1106 gst_decode_bin_get_sink_caps (GstDecodeBin * dbin)
1110 GST_DEBUG_OBJECT (dbin, "Getting currently set caps");
1112 g_object_get (dbin->typefind, "force-caps", &caps, NULL);
1118 gst_decode_bin_set_subs_encoding (GstDecodeBin * dbin, const gchar * encoding)
1122 GST_DEBUG_OBJECT (dbin, "Setting new encoding: %s", GST_STR_NULL (encoding));
1124 SUBTITLE_LOCK (dbin);
1125 g_free (dbin->encoding);
1126 dbin->encoding = g_strdup (encoding);
1128 /* set the subtitle encoding on all added elements */
1129 for (walk = dbin->subtitles; walk; walk = g_list_next (walk)) {
1130 g_object_set (G_OBJECT (walk->data), "subtitle-encoding", dbin->encoding,
1133 SUBTITLE_UNLOCK (dbin);
1137 gst_decode_bin_get_subs_encoding (GstDecodeBin * dbin)
1141 GST_DEBUG_OBJECT (dbin, "Getting currently set encoding");
1143 SUBTITLE_LOCK (dbin);
1144 encoding = g_strdup (dbin->encoding);
1145 SUBTITLE_UNLOCK (dbin);
1151 gst_decode_bin_set_property (GObject * object, guint prop_id,
1152 const GValue * value, GParamSpec * pspec)
1156 dbin = GST_DECODE_BIN (object);
1160 gst_decode_bin_set_caps (dbin, g_value_get_boxed (value));
1162 case PROP_SUBTITLE_ENCODING:
1163 gst_decode_bin_set_subs_encoding (dbin, g_value_get_string (value));
1165 case PROP_SINK_CAPS:
1166 gst_decode_bin_set_sink_caps (dbin, g_value_get_boxed (value));
1168 case PROP_USE_BUFFERING:
1169 dbin->use_buffering = g_value_get_boolean (value);
1171 case PROP_LOW_PERCENT:
1172 dbin->low_percent = g_value_get_int (value);
1174 case PROP_HIGH_PERCENT:
1175 dbin->high_percent = g_value_get_int (value);
1177 case PROP_MAX_SIZE_BYTES:
1178 dbin->max_size_bytes = g_value_get_uint (value);
1180 case PROP_MAX_SIZE_BUFFERS:
1181 dbin->max_size_buffers = g_value_get_uint (value);
1183 case PROP_MAX_SIZE_TIME:
1184 dbin->max_size_time = g_value_get_uint64 (value);
1186 case PROP_POST_STREAM_TOPOLOGY:
1187 dbin->post_stream_topology = g_value_get_boolean (value);
1189 case PROP_EXPOSE_ALL_STREAMS:
1190 dbin->expose_allstreams = g_value_get_boolean (value);
1193 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1199 gst_decode_bin_get_property (GObject * object, guint prop_id,
1200 GValue * value, GParamSpec * pspec)
1204 dbin = GST_DECODE_BIN (object);
1207 g_value_take_boxed (value, gst_decode_bin_get_caps (dbin));
1209 case PROP_SUBTITLE_ENCODING:
1210 g_value_take_string (value, gst_decode_bin_get_subs_encoding (dbin));
1212 case PROP_SINK_CAPS:
1213 g_value_take_boxed (value, gst_decode_bin_get_sink_caps (dbin));
1215 case PROP_USE_BUFFERING:
1216 g_value_set_boolean (value, dbin->use_buffering);
1218 case PROP_LOW_PERCENT:
1219 g_value_set_int (value, dbin->low_percent);
1221 case PROP_HIGH_PERCENT:
1222 g_value_set_int (value, dbin->high_percent);
1224 case PROP_MAX_SIZE_BYTES:
1225 g_value_set_uint (value, dbin->max_size_bytes);
1227 case PROP_MAX_SIZE_BUFFERS:
1228 g_value_set_uint (value, dbin->max_size_buffers);
1230 case PROP_MAX_SIZE_TIME:
1231 g_value_set_uint64 (value, dbin->max_size_time);
1233 case PROP_POST_STREAM_TOPOLOGY:
1234 g_value_set_boolean (value, dbin->post_stream_topology);
1236 case PROP_EXPOSE_ALL_STREAMS:
1237 g_value_set_boolean (value, dbin->expose_allstreams);
1240 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1247 * Default autoplug signal handlers
1250 gst_decode_bin_autoplug_continue (GstElement * element, GstPad * pad,
1253 GST_DEBUG_OBJECT (element, "autoplug-continue returns TRUE");
1255 /* by default we always continue */
1259 static GValueArray *
1260 gst_decode_bin_autoplug_factories (GstElement * element, GstPad * pad,
1264 GValueArray *result;
1265 GstDecodeBin *dbin = GST_DECODE_BIN_CAST (element);
1267 GST_DEBUG_OBJECT (element, "finding factories");
1269 /* return all compatible factories for caps */
1270 g_mutex_lock (dbin->factories_lock);
1271 gst_decode_bin_update_factories_list (dbin);
1273 gst_element_factory_list_filter (dbin->factories, caps, GST_PAD_SINK,
1275 g_mutex_unlock (dbin->factories_lock);
1277 result = g_value_array_new (g_list_length (list));
1278 for (tmp = list; tmp; tmp = tmp->next) {
1279 GstElementFactory *factory = GST_ELEMENT_FACTORY_CAST (tmp->data);
1280 GValue val = { 0, };
1282 g_value_init (&val, G_TYPE_OBJECT);
1283 g_value_set_object (&val, factory);
1284 g_value_array_append (result, &val);
1285 g_value_unset (&val);
1287 gst_plugin_feature_list_free (list);
1289 GST_DEBUG_OBJECT (element, "autoplug-factories returns %p", result);
1294 static GValueArray *
1295 gst_decode_bin_autoplug_sort (GstElement * element, GstPad * pad,
1296 GstCaps * caps, GValueArray * factories)
1301 static GstAutoplugSelectResult
1302 gst_decode_bin_autoplug_select (GstElement * element, GstPad * pad,
1303 GstCaps * caps, GstElementFactory * factory)
1305 GST_DEBUG_OBJECT (element, "default autoplug-select returns TRY");
1308 return GST_AUTOPLUG_SELECT_TRY;
1315 static gboolean are_final_caps (GstDecodeBin * dbin, GstCaps * caps);
1316 static gboolean is_demuxer_element (GstElement * srcelement);
1318 static gboolean connect_pad (GstDecodeBin * dbin, GstElement * src,
1319 GstDecodePad * dpad, GstPad * pad, GstCaps * caps, GValueArray * factories,
1320 GstDecodeChain * chain);
1321 static gboolean connect_element (GstDecodeBin * dbin, GstElement * element,
1322 GstDecodeChain * chain);
1323 static void expose_pad (GstDecodeBin * dbin, GstElement * src,
1324 GstDecodePad * dpad, GstPad * pad, GstCaps * caps, GstDecodeChain * chain);
1326 static void pad_added_cb (GstElement * element, GstPad * pad,
1327 GstDecodeChain * chain);
1328 static void pad_removed_cb (GstElement * element, GstPad * pad,
1329 GstDecodeChain * chain);
1330 static void no_more_pads_cb (GstElement * element, GstDecodeChain * chain);
1332 static GstDecodeGroup *gst_decode_chain_get_current_group (GstDecodeChain *
1335 /* called when a new pad is discovered. It will perform some basic actions
1336 * before trying to link something to it.
1338 * - Check the caps, don't do anything when there are no caps or when they have
1340 * - signal AUTOPLUG_CONTINUE to check if we need to continue autoplugging this
1342 * - if the caps are non-fixed, setup a handler to continue autoplugging when
1343 * the caps become fixed (connect to notify::caps).
1344 * - get list of factories to autoplug.
1345 * - continue autoplugging to one of the factories.
1348 analyze_new_pad (GstDecodeBin * dbin, GstElement * src, GstPad * pad,
1349 GstCaps * caps, GstDecodeChain * chain)
1351 gboolean apcontinue = TRUE;
1352 GValueArray *factories = NULL, *result = NULL;
1354 GstElementFactory *factory;
1355 const gchar *classification;
1356 gboolean is_parser_converter = FALSE;
1359 GST_DEBUG_OBJECT (dbin, "Pad %s:%s caps:%" GST_PTR_FORMAT,
1360 GST_DEBUG_PAD_NAME (pad), caps);
1363 && src != ((GstDecodeElement *) chain->elements->data)->element
1364 && src != ((GstDecodeElement *) chain->elements->data)->capsfilter) {
1365 GST_ERROR_OBJECT (dbin, "New pad from not the last element in this chain");
1369 if (chain->endpad) {
1370 GST_ERROR_OBJECT (dbin, "New pad in a chain that is already complete");
1374 if (chain->demuxer) {
1375 GstDecodeGroup *group;
1376 GstDecodeChain *oldchain = chain;
1378 /* we are adding a new pad for a demuxer (see is_demuxer_element(),
1379 * start a new chain for it */
1380 CHAIN_MUTEX_LOCK (oldchain);
1381 group = gst_decode_chain_get_current_group (chain);
1383 chain = gst_decode_chain_new (dbin, group, pad);
1384 group->children = g_list_prepend (group->children, chain);
1386 CHAIN_MUTEX_UNLOCK (oldchain);
1388 GST_WARNING_OBJECT (dbin, "No current group");
1393 if ((caps == NULL) || gst_caps_is_empty (caps))
1396 if (gst_caps_is_any (caps))
1399 dpad = gst_decode_pad_new (dbin, pad, chain);
1401 /* 1. Emit 'autoplug-continue' the result will tell us if this pads needs
1402 * further autoplugging. Only do this for fixed caps, for unfixed caps
1403 * we will later come here again from the notify::caps handler. The
1404 * problem with unfixed caps is that we can reliably tell if the output
1405 * is e.g. accepted by a sink because only parts of the possible final
1406 * caps might be accepted by the sink. */
1407 if (gst_caps_is_fixed (caps))
1408 g_signal_emit (G_OBJECT (dbin),
1409 gst_decode_bin_signals[SIGNAL_AUTOPLUG_CONTINUE], 0, dpad, caps,
1414 /* 1.a if autoplug-continue is FALSE or caps is a raw format, goto pad_is_final */
1415 if ((!apcontinue) || are_final_caps (dbin, caps))
1418 /* 1.b For Parser/Converter that can output different stream formats
1419 * we insert a capsfilter with the sorted caps of all possible next
1420 * elements and continue with the capsfilter srcpad */
1421 factory = gst_element_get_factory (src);
1422 classification = gst_element_factory_get_klass (factory);
1423 is_parser_converter = (strstr (classification, "Parser")
1424 && strstr (classification, "Converter"));
1426 /* 1.c when the caps are not fixed yet, we can't be sure what element to
1427 * connect. We delay autoplugging until the caps are fixed */
1428 if (!is_parser_converter && !gst_caps_is_fixed (caps))
1431 /* 1.d else get the factories and if there's no compatible factory goto
1433 g_signal_emit (G_OBJECT (dbin),
1434 gst_decode_bin_signals[SIGNAL_AUTOPLUG_FACTORIES], 0, dpad, caps,
1437 /* NULL means that we can expose the pad */
1438 if (factories == NULL)
1441 /* if the array is empty, we have a type for which we have no decoder */
1442 if (factories->n_values == 0) {
1443 if (!dbin->expose_allstreams) {
1444 GstCaps *raw = gst_static_caps_get (&default_raw_caps);
1446 /* If the caps are raw, this just means we don't want to expose them */
1447 if (gst_caps_can_intersect (raw, caps)) {
1448 g_value_array_free (factories);
1449 gst_caps_unref (raw);
1450 gst_object_unref (dpad);
1451 goto discarded_type;
1453 gst_caps_unref (raw);
1456 /* if not we have a unhandled type with no compatible factories */
1457 g_value_array_free (factories);
1458 gst_object_unref (dpad);
1462 /* 1.e sort some more. */
1463 g_signal_emit (G_OBJECT (dbin),
1464 gst_decode_bin_signals[SIGNAL_AUTOPLUG_SORT], 0, dpad, caps, factories,
1467 g_value_array_free (factories);
1471 /* At this point we have a potential decoder, but we might not need it
1472 * if it doesn't match the output caps */
1473 if (!dbin->expose_allstreams && gst_caps_is_fixed (caps)) {
1476 gboolean dontuse = FALSE;
1478 GST_DEBUG ("Checking if we can abort early");
1480 /* 1.f Do an early check to see if the candidates are potential decoders, but
1481 * due to the fact that they decode to a mediatype that is not final we don't
1484 for (i = 0; i < factories->n_values && !dontuse; i++) {
1485 GstElementFactory *factory =
1486 g_value_get_object (g_value_array_get_nth (factories, i));
1489 /* We are only interested in skipping decoders */
1490 if (strstr (gst_element_factory_get_klass (factory), "Decoder")) {
1492 GST_DEBUG ("Trying factory %s",
1493 gst_plugin_feature_get_name (GST_PLUGIN_FEATURE (factory)));
1495 /* Check the source pad template caps to see if they match raw caps but don't match
1497 for (tmps = gst_element_factory_get_static_pad_templates (factory);
1498 tmps && !dontuse; tmps = tmps->next) {
1499 GstStaticPadTemplate *st = (GstStaticPadTemplate *) tmps->data;
1500 if (st->direction != GST_PAD_SRC)
1502 tcaps = gst_static_pad_template_get_caps (st);
1506 /* Emit autoplug-continue to see if the caps are considered to be raw caps */
1507 g_signal_emit (G_OBJECT (dbin),
1508 gst_decode_bin_signals[SIGNAL_AUTOPLUG_CONTINUE], 0, dpad, tcaps,
1511 /* If autoplug-continue returns TRUE and the caps are not final, don't use them */
1512 if (apcontinue && !are_final_caps (dbin, tcaps))
1514 gst_caps_unref (tcaps);
1520 gst_object_unref (dpad);
1521 g_value_array_free (factories);
1522 goto discarded_type;
1526 /* 1.g now get the factory template caps and insert the capsfilter if this
1527 * is a parser/converter
1529 if (is_parser_converter) {
1530 GstCaps *filter_caps;
1533 GstDecodeElement *delem;
1535 g_assert (chain->elements != NULL);
1536 delem = (GstDecodeElement *) chain->elements->data;
1538 filter_caps = gst_caps_new_empty ();
1539 for (i = 0; i < factories->n_values; i++) {
1540 GstElementFactory *factory =
1541 g_value_get_object (g_value_array_get_nth (factories, i));
1542 GstCaps *tcaps, *intersection;
1545 GST_DEBUG ("Trying factory %s",
1546 gst_plugin_feature_get_name (GST_PLUGIN_FEATURE (factory)));
1548 if (gst_element_get_factory (src) == factory) {
1549 GST_DEBUG ("Skipping factory");
1553 for (tmps = gst_element_factory_get_static_pad_templates (factory); tmps;
1554 tmps = tmps->next) {
1555 GstStaticPadTemplate *st = (GstStaticPadTemplate *) tmps->data;
1556 if (st->direction != GST_PAD_SINK || st->presence != GST_PAD_ALWAYS)
1558 tcaps = gst_static_pad_template_get_caps (st);
1560 gst_caps_intersect_full (tcaps, caps, GST_CAPS_INTERSECT_FIRST);
1561 filter_caps = gst_caps_merge (filter_caps, intersection);
1562 gst_caps_unref (tcaps);
1566 /* Append the parser caps to prevent any not-negotiated errors */
1567 filter_caps = gst_caps_merge (filter_caps, gst_caps_ref (caps));
1569 delem->capsfilter = gst_element_factory_make ("capsfilter", NULL);
1570 g_object_set (G_OBJECT (delem->capsfilter), "caps", filter_caps, NULL);
1571 gst_caps_unref (filter_caps);
1572 gst_element_set_state (delem->capsfilter, GST_STATE_PAUSED);
1573 gst_bin_add (GST_BIN_CAST (dbin), gst_object_ref (delem->capsfilter));
1575 gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (dpad), NULL);
1576 p = gst_element_get_static_pad (delem->capsfilter, "sink");
1577 gst_pad_link_full (pad, p, GST_PAD_LINK_CHECK_NOTHING);
1578 gst_object_unref (p);
1579 p = gst_element_get_static_pad (delem->capsfilter, "src");
1580 gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (dpad), p);
1583 if (!gst_caps_is_fixed (caps)) {
1584 g_value_array_free (factories);
1589 /* 1.h else continue autoplugging something from the list. */
1590 GST_LOG_OBJECT (pad, "Let's continue discovery on this pad");
1591 res = connect_pad (dbin, src, dpad, pad, caps, factories, chain);
1593 /* Need to unref the capsfilter srcpad here if
1594 * we inserted a capsfilter */
1595 if (is_parser_converter)
1596 gst_object_unref (pad);
1598 gst_object_unref (dpad);
1599 g_value_array_free (factories);
1608 GST_LOG_OBJECT (dbin, "Pad is final. autoplug-continue:%d", apcontinue);
1609 expose_pad (dbin, src, dpad, pad, caps, chain);
1610 gst_object_unref (dpad);
1616 GST_LOG_OBJECT (pad, "Known type, but discarded because not final caps");
1617 chain->deadend = TRUE;
1618 chain->endcaps = gst_caps_ref (caps);
1620 /* Try to expose anything */
1622 if (gst_decode_chain_is_complete (dbin->decode_chain)) {
1623 gst_decode_bin_expose (dbin);
1625 EXPOSE_UNLOCK (dbin);
1626 do_async_done (dbin);
1633 GST_LOG_OBJECT (pad, "Unknown type, posting message and firing signal");
1635 chain->deadend = TRUE;
1636 chain->endcaps = gst_caps_ref (caps);
1638 gst_element_post_message (GST_ELEMENT_CAST (dbin),
1639 gst_missing_decoder_message_new (GST_ELEMENT_CAST (dbin), caps));
1641 g_signal_emit (G_OBJECT (dbin),
1642 gst_decode_bin_signals[SIGNAL_UNKNOWN_TYPE], 0, pad, caps);
1644 /* Try to expose anything */
1646 if (gst_decode_chain_is_complete (dbin->decode_chain)) {
1647 gst_decode_bin_expose (dbin);
1649 EXPOSE_UNLOCK (dbin);
1651 if (src == dbin->typefind) {
1654 if (caps && !gst_caps_is_empty (caps)) {
1655 desc = gst_pb_utils_get_decoder_description (caps);
1656 GST_ELEMENT_ERROR (dbin, STREAM, CODEC_NOT_FOUND,
1657 (_("A %s plugin is required to play this stream, "
1658 "but not installed."), desc),
1659 ("No decoder to handle media type '%s'",
1660 gst_structure_get_name (gst_caps_get_structure (caps, 0))));
1663 GST_ELEMENT_ERROR (dbin, STREAM, TYPE_NOT_FOUND,
1664 (_("Could not determine type of stream")),
1665 ("Stream caps %" GST_PTR_FORMAT, caps));
1667 do_async_done (dbin);
1673 GST_DEBUG_OBJECT (pad, "pad has non-fixed caps delay autoplugging");
1674 gst_object_unref (dpad);
1675 goto setup_caps_delay;
1679 GST_WARNING_OBJECT (pad,
1680 "pad has ANY caps, not able to autoplug to anything");
1681 goto setup_caps_delay;
1685 GstPendingPad *ppad;
1687 /* connect to caps notification */
1688 CHAIN_MUTEX_LOCK (chain);
1689 GST_LOG_OBJECT (dbin, "Chain %p has now %d dynamic pads", chain,
1690 g_list_length (chain->pending_pads));
1691 ppad = g_slice_new0 (GstPendingPad);
1692 ppad->pad = gst_object_ref (pad);
1693 ppad->chain = chain;
1694 ppad->event_probe_id =
1695 gst_pad_add_probe (pad, GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM,
1696 pad_event_cb, ppad, NULL);
1697 chain->pending_pads = g_list_prepend (chain->pending_pads, ppad);
1698 g_signal_connect (G_OBJECT (pad), "notify::caps",
1699 G_CALLBACK (caps_notify_cb), chain);
1700 CHAIN_MUTEX_UNLOCK (chain);
1702 /* If we're here because we have a Parser/Converter
1703 * we have to unref the pad */
1704 if (is_parser_converter)
1705 gst_object_unref (pad);
1712 add_error_filter (GstDecodeBin * dbin, GstElement * element)
1714 GST_OBJECT_LOCK (dbin);
1715 dbin->filtered = g_list_prepend (dbin->filtered, element);
1716 GST_OBJECT_UNLOCK (dbin);
1720 remove_error_filter (GstDecodeBin * dbin, GstElement * element)
1722 GST_OBJECT_LOCK (dbin);
1723 dbin->filtered = g_list_remove (dbin->filtered, element);
1724 GST_OBJECT_UNLOCK (dbin);
1729 * Try to connect the given pad to an element created from one of the factories,
1732 * Note that dpad is ghosting pad, and so pad is linked; be sure to unset dpad's
1733 * target before trying to link pad.
1735 * Returns TRUE if an element was properly created and linked
1738 connect_pad (GstDecodeBin * dbin, GstElement * src, GstDecodePad * dpad,
1739 GstPad * pad, GstCaps * caps, GValueArray * factories,
1740 GstDecodeChain * chain)
1742 gboolean res = FALSE;
1743 GstPad *mqpad = NULL;
1744 gboolean is_demuxer = chain->parent && !chain->elements; /* First pad after the demuxer */
1746 g_return_val_if_fail (factories != NULL, FALSE);
1747 g_return_val_if_fail (factories->n_values > 0, FALSE);
1749 GST_DEBUG_OBJECT (dbin, "pad %s:%s , chain:%p",
1750 GST_DEBUG_PAD_NAME (pad), chain);
1752 /* 1. is element demuxer or parser */
1754 GST_LOG_OBJECT (src,
1755 "is a demuxer, connecting the pad through multiqueue '%s'",
1756 GST_OBJECT_NAME (chain->parent->multiqueue));
1758 gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (dpad), NULL);
1759 if (!(mqpad = gst_decode_group_control_demuxer_pad (chain->parent, pad)))
1761 src = chain->parent->multiqueue;
1763 gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (dpad), pad);
1766 /* 2. Try to create an element and link to it */
1767 while (factories->n_values > 0) {
1768 GstAutoplugSelectResult ret;
1769 GstElementFactory *factory;
1770 GstDecodeElement *delem;
1771 GstElement *element;
1775 /* Set dpad target to pad again, it might've been unset
1776 * below but we came back here because something failed
1778 gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (dpad), pad);
1780 /* take first factory */
1781 factory = g_value_get_object (g_value_array_get_nth (factories, 0));
1782 /* Remove selected factory from the list. */
1783 g_value_array_remove (factories, 0);
1785 /* Check if the caps are really supported by the factory. The
1786 * factory list is non-empty-subset filtered while caps
1787 * are only accepted by a pad if they are a subset of the
1790 * FIXME: Only do this for fixed caps here. Non-fixed caps
1791 * can happen if a Parser/Converter was autoplugged before
1792 * this. We then assume that it will be able to convert to
1793 * everything that the decoder would want.
1795 * A subset check will fail here because the parser caps
1796 * will be generic and while the decoder will only
1797 * support a subset of the parser caps.
1799 if (gst_caps_is_fixed (caps)) {
1800 const GList *templs;
1801 gboolean skip = FALSE;
1803 templs = gst_element_factory_get_static_pad_templates (factory);
1806 GstStaticPadTemplate *templ = (GstStaticPadTemplate *) templs->data;
1808 if (templ->direction == GST_PAD_SINK) {
1809 GstCaps *templcaps = gst_static_caps_get (&templ->static_caps);
1811 if (!gst_caps_is_subset (caps, templcaps)) {
1812 gst_caps_unref (templcaps);
1817 gst_caps_unref (templcaps);
1819 templs = g_list_next (templs);
1825 /* If the factory is for a parser we first check if the factory
1826 * was already used for the current chain. If it was used already
1827 * we would otherwise create an infinite loop here because the
1828 * parser apparently accepts its own output as input.
1829 * This is only done for parsers because it's perfectly valid
1830 * to have other element classes after each other because a
1831 * parser is the only one that does not change the data. A
1832 * valid example for this would be multiple id3demux in a row.
1834 if (strstr (gst_element_factory_get_klass (factory), "Parser")) {
1835 gboolean skip = FALSE;
1838 CHAIN_MUTEX_LOCK (chain);
1839 for (l = chain->elements; l; l = l->next) {
1840 GstDecodeElement *delem = (GstDecodeElement *) l->data;
1841 GstElement *otherelement = delem->element;
1843 if (gst_element_get_factory (otherelement) == factory) {
1848 CHAIN_MUTEX_UNLOCK (chain);
1850 GST_DEBUG_OBJECT (dbin,
1851 "Skipping factory '%s' because it was already used in this chain",
1852 gst_plugin_feature_get_name (GST_PLUGIN_FEATURE_CAST (factory)));
1857 /* emit autoplug-select to see what we should do with it. */
1858 g_signal_emit (G_OBJECT (dbin),
1859 gst_decode_bin_signals[SIGNAL_AUTOPLUG_SELECT],
1860 0, dpad, caps, factory, &ret);
1863 case GST_AUTOPLUG_SELECT_TRY:
1864 GST_DEBUG_OBJECT (dbin, "autoplug select requested try");
1866 case GST_AUTOPLUG_SELECT_EXPOSE:
1867 GST_DEBUG_OBJECT (dbin, "autoplug select requested expose");
1868 /* expose the pad, we don't have the source element */
1869 expose_pad (dbin, src, dpad, pad, caps, chain);
1872 case GST_AUTOPLUG_SELECT_SKIP:
1873 GST_DEBUG_OBJECT (dbin, "autoplug select requested skip");
1876 GST_WARNING_OBJECT (dbin, "autoplug select returned unhandled %d", ret);
1880 /* 2.0. Unlink pad */
1881 gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (dpad), NULL);
1883 /* 2.1. Try to create an element */
1884 if ((element = gst_element_factory_create (factory, NULL)) == NULL) {
1885 GST_WARNING_OBJECT (dbin, "Could not create an element from %s",
1886 gst_plugin_feature_get_name (GST_PLUGIN_FEATURE (factory)));
1890 /* Filter errors, this will prevent the element from causing the pipeline
1891 * to error while we test it using READY state. */
1892 add_error_filter (dbin, element);
1894 /* ... add it ... */
1895 if (!(gst_bin_add (GST_BIN_CAST (dbin), element))) {
1896 GST_WARNING_OBJECT (dbin, "Couldn't add %s to the bin",
1897 GST_ELEMENT_NAME (element));
1898 remove_error_filter (dbin, element);
1899 gst_object_unref (element);
1903 /* Find its sink pad. */
1904 if (!(sinkpad = find_sink_pad (element))) {
1905 GST_WARNING_OBJECT (dbin, "Element %s doesn't have a sink pad",
1906 GST_ELEMENT_NAME (element));
1907 remove_error_filter (dbin, element);
1908 gst_bin_remove (GST_BIN (dbin), element);
1912 /* ... and try to link */
1913 if ((gst_pad_link (pad, sinkpad)) != GST_PAD_LINK_OK) {
1914 GST_WARNING_OBJECT (dbin, "Link failed on pad %s:%s",
1915 GST_DEBUG_PAD_NAME (sinkpad));
1916 remove_error_filter (dbin, element);
1917 gst_object_unref (sinkpad);
1918 gst_bin_remove (GST_BIN (dbin), element);
1922 /* ... activate it ... */
1923 if ((gst_element_set_state (element,
1924 GST_STATE_READY)) == GST_STATE_CHANGE_FAILURE) {
1925 GST_WARNING_OBJECT (dbin, "Couldn't set %s to READY",
1926 GST_ELEMENT_NAME (element));
1927 remove_error_filter (dbin, element);
1928 gst_object_unref (sinkpad);
1929 gst_bin_remove (GST_BIN (dbin), element);
1933 /* Stop filtering errors. */
1934 remove_error_filter (dbin, element);
1936 gst_object_unref (sinkpad);
1937 GST_LOG_OBJECT (dbin, "linked on pad %s:%s", GST_DEBUG_PAD_NAME (pad));
1939 CHAIN_MUTEX_LOCK (chain);
1940 delem = g_slice_new (GstDecodeElement);
1941 delem->element = gst_object_ref (element);
1942 delem->capsfilter = NULL;
1943 chain->elements = g_list_prepend (chain->elements, delem);
1944 chain->demuxer = is_demuxer_element (element);
1945 CHAIN_MUTEX_UNLOCK (chain);
1947 /* link this element further */
1948 connect_element (dbin, element, chain);
1950 /* try to configure the subtitle encoding property when we can */
1951 if (g_object_class_find_property (G_OBJECT_GET_CLASS (element),
1952 "subtitle-encoding")) {
1953 SUBTITLE_LOCK (dbin);
1954 GST_DEBUG_OBJECT (dbin,
1955 "setting subtitle-encoding=%s to element", dbin->encoding);
1956 g_object_set (G_OBJECT (element), "subtitle-encoding", dbin->encoding,
1958 SUBTITLE_UNLOCK (dbin);
1963 /* Bring the element to the state of the parent */
1964 if ((gst_element_set_state (element,
1965 GST_STATE_PAUSED)) == GST_STATE_CHANGE_FAILURE) {
1966 GstDecodeElement *dtmp = NULL;
1967 GstElement *tmp = NULL;
1969 GST_WARNING_OBJECT (dbin, "Couldn't set %s to PAUSED",
1970 GST_ELEMENT_NAME (element));
1972 /* Remove all elements in this chain that were just added. No
1973 * other thread could've added elements in the meantime */
1974 CHAIN_MUTEX_LOCK (chain);
1978 dtmp = chain->elements->data;
1979 tmp = dtmp->element;
1981 /* Disconnect any signal handlers that might be connected
1982 * in connect_element() or analyze_pad() */
1983 g_signal_handlers_disconnect_by_func (tmp, pad_added_cb, chain);
1984 g_signal_handlers_disconnect_by_func (tmp, pad_removed_cb, chain);
1985 g_signal_handlers_disconnect_by_func (tmp, no_more_pads_cb, chain);
1987 for (l = chain->pending_pads; l;) {
1988 GstPendingPad *pp = l->data;
1991 if (GST_PAD_PARENT (pp->pad) != tmp) {
1996 g_signal_handlers_disconnect_by_func (pp->pad, caps_notify_cb, chain);
1997 gst_pad_remove_probe (pp->pad, pp->event_probe_id);
1998 gst_object_unref (pp->pad);
1999 g_slice_free (GstPendingPad, pp);
2001 /* Remove element from the list, update list head and go to the
2002 * next element in the list */
2004 chain->pending_pads = g_list_delete_link (chain->pending_pads, l);
2008 if (dtmp->capsfilter) {
2009 gst_bin_remove (GST_BIN (dbin), dtmp->capsfilter);
2010 gst_element_set_state (dtmp->capsfilter, GST_STATE_NULL);
2011 gst_object_unref (dtmp->capsfilter);
2014 gst_bin_remove (GST_BIN (dbin), tmp);
2015 gst_element_set_state (tmp, GST_STATE_NULL);
2017 gst_object_unref (tmp);
2018 g_slice_free (GstDecodeElement, dtmp);
2020 chain->elements = g_list_delete_link (chain->elements, chain->elements);
2021 } while (tmp != element);
2022 CHAIN_MUTEX_UNLOCK (chain);
2027 SUBTITLE_LOCK (dbin);
2028 /* we added the element now, add it to the list of subtitle-encoding
2029 * elements when we can set the property */
2030 dbin->subtitles = g_list_prepend (dbin->subtitles, element);
2031 SUBTITLE_UNLOCK (dbin);
2040 gst_object_unref (mqpad);
2046 get_pad_caps (GstPad * pad)
2050 /* first check the pad caps, if this is set, we are positively sure it is
2051 * fixed and exactly what the element will produce. */
2052 caps = gst_pad_get_current_caps (pad);
2054 /* then use the getcaps function if we don't have caps. These caps might not
2055 * be fixed in some cases, in which case analyze_new_pad will set up a
2056 * notify::caps signal to continue autoplugging. */
2058 caps = gst_pad_query_caps (pad, NULL);
2064 connect_element (GstDecodeBin * dbin, GstElement * element,
2065 GstDecodeChain * chain)
2068 gboolean res = TRUE;
2069 gboolean dynamic = FALSE;
2070 GList *to_connect = NULL;
2072 GST_DEBUG_OBJECT (dbin, "Attempting to connect element %s [chain:%p] further",
2073 GST_ELEMENT_NAME (element), chain);
2075 /* 1. Loop over pad templates, grabbing existing pads along the way */
2076 for (pads = GST_ELEMENT_GET_CLASS (element)->padtemplates; pads;
2077 pads = g_list_next (pads)) {
2078 GstPadTemplate *templ = GST_PAD_TEMPLATE (pads->data);
2079 const gchar *templ_name;
2081 /* we are only interested in source pads */
2082 if (GST_PAD_TEMPLATE_DIRECTION (templ) != GST_PAD_SRC)
2085 templ_name = GST_PAD_TEMPLATE_NAME_TEMPLATE (templ);
2086 GST_DEBUG_OBJECT (dbin, "got a source pad template %s", templ_name);
2088 /* figure out what kind of pad this is */
2089 switch (GST_PAD_TEMPLATE_PRESENCE (templ)) {
2090 case GST_PAD_ALWAYS:
2092 /* get the pad that we need to autoplug */
2093 GstPad *pad = gst_element_get_static_pad (element, templ_name);
2096 GST_DEBUG_OBJECT (dbin, "got the pad for always template %s",
2098 /* here is the pad, we need to autoplug it */
2099 to_connect = g_list_prepend (to_connect, pad);
2101 /* strange, pad is marked as always but it's not
2102 * there. Fix the element */
2103 GST_WARNING_OBJECT (dbin,
2104 "could not get the pad for always template %s", templ_name);
2108 case GST_PAD_SOMETIMES:
2110 /* try to get the pad to see if it is already created or
2112 GstPad *pad = gst_element_get_static_pad (element, templ_name);
2115 GST_DEBUG_OBJECT (dbin, "got the pad for sometimes template %s",
2117 /* the pad is created, we need to autoplug it */
2118 to_connect = g_list_prepend (to_connect, pad);
2120 GST_DEBUG_OBJECT (dbin,
2121 "did not get the sometimes pad of template %s", templ_name);
2122 /* we have an element that will create dynamic pads */
2127 case GST_PAD_REQUEST:
2128 /* ignore request pads */
2129 GST_DEBUG_OBJECT (dbin, "ignoring request padtemplate %s", templ_name);
2134 /* 2. if there are more potential pads, connect to relevant signals */
2136 GST_LOG_OBJECT (dbin, "Adding signals to element %s in chain %p",
2137 GST_ELEMENT_NAME (element), chain);
2138 g_signal_connect (G_OBJECT (element), "pad-added",
2139 G_CALLBACK (pad_added_cb), chain);
2140 g_signal_connect (G_OBJECT (element), "pad-removed",
2141 G_CALLBACK (pad_removed_cb), chain);
2142 g_signal_connect (G_OBJECT (element), "no-more-pads",
2143 G_CALLBACK (no_more_pads_cb), chain);
2146 /* 3. for every available pad, connect it */
2147 for (pads = to_connect; pads; pads = g_list_next (pads)) {
2148 GstPad *pad = GST_PAD_CAST (pads->data);
2151 caps = get_pad_caps (pad);
2152 analyze_new_pad (dbin, element, pad, caps, chain);
2154 gst_caps_unref (caps);
2156 gst_object_unref (pad);
2158 g_list_free (to_connect);
2165 * Expose the given pad on the chain as a decoded pad.
2168 expose_pad (GstDecodeBin * dbin, GstElement * src, GstDecodePad * dpad,
2169 GstPad * pad, GstCaps * caps, GstDecodeChain * chain)
2171 GstPad *mqpad = NULL;
2173 GST_DEBUG_OBJECT (dbin, "pad %s:%s, chain:%p",
2174 GST_DEBUG_PAD_NAME (pad), chain);
2176 /* If this is the first pad for this chain, there are no other elements
2177 * and the source element is not the multiqueue we must link through the
2180 * This is the case if a demuxer directly exposed a raw pad.
2182 if (chain->parent && !chain->elements && src != chain->parent->multiqueue) {
2183 GST_LOG_OBJECT (src, "connecting the pad through multiqueue");
2185 gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (dpad), NULL);
2186 if (!(mqpad = gst_decode_group_control_demuxer_pad (chain->parent, pad)))
2189 gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (dpad), pad);
2192 gst_decode_pad_activate (dpad, chain);
2193 chain->endpad = gst_object_ref (dpad);
2194 chain->endcaps = gst_caps_ref (caps);
2197 if (gst_decode_chain_is_complete (dbin->decode_chain)) {
2198 gst_decode_bin_expose (dbin);
2200 EXPOSE_UNLOCK (dbin);
2203 gst_object_unref (mqpad);
2209 /* check_upstream_seekable:
2211 * Check if upstream is seekable.
2214 check_upstream_seekable (GstDecodeBin * dbin, GstPad * pad)
2217 gint64 start = -1, stop = -1;
2218 gboolean seekable = FALSE;
2220 query = gst_query_new_seeking (GST_FORMAT_BYTES);
2221 if (!gst_pad_peer_query (pad, query)) {
2222 GST_DEBUG_OBJECT (dbin, "seeking query failed");
2223 gst_query_unref (query);
2227 gst_query_parse_seeking (query, NULL, &seekable, &start, &stop);
2229 gst_query_unref (query);
2231 /* try harder to query upstream size if we didn't get it the first time */
2232 if (seekable && stop == -1) {
2233 GST_DEBUG_OBJECT (dbin, "doing duration query to fix up unset stop");
2234 gst_pad_peer_query_duration (pad, GST_FORMAT_BYTES, &stop);
2237 /* if upstream doesn't know the size, it's likely that it's not seekable in
2238 * practice even if it technically may be seekable */
2239 if (seekable && (start != 0 || stop <= start)) {
2240 GST_DEBUG_OBJECT (dbin, "seekable but unknown start/stop -> disable");
2244 GST_DEBUG_OBJECT (dbin, "upstream seekable: %d", seekable);
2249 type_found (GstElement * typefind, guint probability,
2250 GstCaps * caps, GstDecodeBin * decode_bin)
2252 GstPad *pad, *sink_pad;
2254 GST_DEBUG_OBJECT (decode_bin, "typefind found caps %" GST_PTR_FORMAT, caps);
2256 /* If the typefinder (but not something else) finds text/plain - i.e. that's
2257 * the top-level type of the file - then error out.
2259 if (gst_structure_has_name (gst_caps_get_structure (caps, 0), "text/plain")) {
2260 GST_ELEMENT_ERROR (decode_bin, STREAM, WRONG_TYPE,
2261 (_("This appears to be a text file")),
2262 ("decodebin cannot decode plain text files"));
2266 /* FIXME: we can only deal with one type, we don't yet support dynamically changing
2267 * caps from the typefind element */
2268 if (decode_bin->have_type || decode_bin->decode_chain)
2271 decode_bin->have_type = TRUE;
2273 pad = gst_element_get_static_pad (typefind, "src");
2274 sink_pad = gst_element_get_static_pad (typefind, "sink");
2276 /* need some lock here to prevent race with shutdown state change
2277 * which might yank away e.g. decode_chain while building stuff here.
2278 * In typical cases, STREAM_LOCK is held and handles that, it need not
2279 * be held (if called from a proxied setcaps), so grab it anyway */
2280 GST_PAD_STREAM_LOCK (sink_pad);
2281 decode_bin->decode_chain = gst_decode_chain_new (decode_bin, NULL, pad);
2282 analyze_new_pad (decode_bin, typefind, pad, caps, decode_bin->decode_chain);
2283 GST_PAD_STREAM_UNLOCK (sink_pad);
2285 gst_object_unref (sink_pad);
2286 gst_object_unref (pad);
2292 static GstPadProbeReturn
2293 pad_event_cb (GstPad * pad, GstPadProbeInfo * info, gpointer data)
2295 GstEvent *event = GST_PAD_PROBE_INFO_EVENT (info);
2296 GstPendingPad *ppad = (GstPendingPad *) data;
2297 GstDecodeChain *chain = ppad->chain;
2298 GstDecodeBin *dbin = chain->dbin;
2303 switch (GST_EVENT_TYPE (event)) {
2305 GST_DEBUG_OBJECT (dbin, "Received EOS on a non final pad, this stream "
2307 chain->deadend = TRUE;
2308 /* we don't set the endcaps because NULL endcaps means early EOS */
2310 if (gst_decode_chain_is_complete (dbin->decode_chain))
2311 gst_decode_bin_expose (dbin);
2312 EXPOSE_UNLOCK (dbin);
2317 return GST_PAD_PROBE_OK;
2321 pad_added_cb (GstElement * element, GstPad * pad, GstDecodeChain * chain)
2328 GST_DEBUG_OBJECT (pad, "pad added, chain:%p", chain);
2330 caps = get_pad_caps (pad);
2331 analyze_new_pad (dbin, element, pad, caps, chain);
2333 gst_caps_unref (caps);
2336 if (gst_decode_chain_is_complete (dbin->decode_chain)) {
2337 GST_LOG_OBJECT (dbin,
2338 "That was the last dynamic object, now attempting to expose the group");
2339 if (!gst_decode_bin_expose (dbin))
2340 GST_WARNING_OBJECT (dbin, "Couldn't expose group");
2342 EXPOSE_UNLOCK (dbin);
2346 pad_removed_cb (GstElement * element, GstPad * pad, GstDecodeChain * chain)
2350 GST_LOG_OBJECT (pad, "pad removed, chain:%p", chain);
2352 /* In fact, we don't have to do anything here, the active group will be
2353 * removed when the group's multiqueue is drained */
2354 CHAIN_MUTEX_LOCK (chain);
2355 for (l = chain->pending_pads; l; l = l->next) {
2356 GstPendingPad *ppad = l->data;
2357 GstPad *opad = ppad->pad;
2360 g_signal_handlers_disconnect_by_func (pad, caps_notify_cb, chain);
2361 gst_pending_pad_free (ppad);
2362 chain->pending_pads = g_list_delete_link (chain->pending_pads, l);
2366 CHAIN_MUTEX_UNLOCK (chain);
2370 no_more_pads_cb (GstElement * element, GstDecodeChain * chain)
2372 GstDecodeGroup *group = NULL;
2374 GST_LOG_OBJECT (element, "got no more pads");
2376 CHAIN_MUTEX_LOCK (chain);
2377 if (!chain->elements
2378 || ((GstDecodeElement *) chain->elements->data)->element != element) {
2379 GST_LOG_OBJECT (chain->dbin, "no-more-pads from old chain element '%s'",
2380 GST_OBJECT_NAME (element));
2381 CHAIN_MUTEX_UNLOCK (chain);
2383 } else if (!chain->demuxer) {
2384 GST_LOG_OBJECT (chain->dbin, "no-more-pads from a non-demuxer element '%s'",
2385 GST_OBJECT_NAME (element));
2386 CHAIN_MUTEX_UNLOCK (chain);
2390 /* when we received no_more_pads, we can complete the pads of the chain */
2391 if (!chain->next_groups && chain->active_group) {
2392 group = chain->active_group;
2393 } else if (chain->next_groups) {
2394 group = chain->next_groups->data;
2397 GST_ERROR_OBJECT (chain->dbin, "can't find group for element");
2398 CHAIN_MUTEX_UNLOCK (chain);
2402 GST_DEBUG_OBJECT (element, "Setting group %p to complete", group);
2404 group->no_more_pads = TRUE;
2405 /* this group has prerolled enough to not need more pads,
2406 * we can probably set its buffering state to playing now */
2407 GST_DEBUG_OBJECT (group->dbin, "Setting group %p multiqueue to "
2408 "'playing' buffering mode", group);
2409 decodebin_set_queue_size (group->dbin, group->multiqueue, FALSE,
2410 (group->parent ? group->parent->seekable : TRUE));
2411 CHAIN_MUTEX_UNLOCK (chain);
2413 EXPOSE_LOCK (chain->dbin);
2414 if (gst_decode_chain_is_complete (chain->dbin->decode_chain)) {
2415 gst_decode_bin_expose (chain->dbin);
2417 EXPOSE_UNLOCK (chain->dbin);
2421 caps_notify_cb (GstPad * pad, GParamSpec * unused, GstDecodeChain * chain)
2423 GstElement *element;
2426 GST_LOG_OBJECT (pad, "Notified caps for pad %s:%s", GST_DEBUG_PAD_NAME (pad));
2428 /* Disconnect this; if we still need it, we'll reconnect to this in
2429 * analyze_new_pad */
2430 g_signal_handlers_disconnect_by_func (pad, caps_notify_cb, chain);
2432 element = GST_ELEMENT_CAST (gst_pad_get_parent (pad));
2434 CHAIN_MUTEX_LOCK (chain);
2435 for (l = chain->pending_pads; l; l = l->next) {
2436 GstPendingPad *ppad = l->data;
2437 if (ppad->pad == pad) {
2438 gst_pending_pad_free (ppad);
2439 chain->pending_pads = g_list_delete_link (chain->pending_pads, l);
2443 CHAIN_MUTEX_UNLOCK (chain);
2445 pad_added_cb (element, pad, chain);
2447 gst_object_unref (element);
2450 /* Decide whether an element is a demuxer based on the
2451 * klass and number/type of src pad templates it has */
2453 is_demuxer_element (GstElement * srcelement)
2455 GstElementFactory *srcfactory;
2456 GstElementClass *elemclass;
2459 gint potential_src_pads = 0;
2461 srcfactory = gst_element_get_factory (srcelement);
2462 klass = gst_element_factory_get_klass (srcfactory);
2464 /* Can't be a demuxer unless it has Demux in the klass name */
2465 if (!strstr (klass, "Demux"))
2468 /* Walk the src pad templates and count how many the element
2470 elemclass = GST_ELEMENT_GET_CLASS (srcelement);
2472 walk = gst_element_class_get_pad_template_list (elemclass);
2473 while (walk != NULL) {
2474 GstPadTemplate *templ;
2476 templ = (GstPadTemplate *) walk->data;
2477 if (GST_PAD_TEMPLATE_DIRECTION (templ) == GST_PAD_SRC) {
2478 switch (GST_PAD_TEMPLATE_PRESENCE (templ)) {
2479 case GST_PAD_ALWAYS:
2480 case GST_PAD_SOMETIMES:
2481 if (strstr (GST_PAD_TEMPLATE_NAME_TEMPLATE (templ), "%"))
2482 potential_src_pads += 2; /* Might make multiple pads */
2484 potential_src_pads += 1;
2486 case GST_PAD_REQUEST:
2487 potential_src_pads += 2;
2491 walk = g_list_next (walk);
2494 if (potential_src_pads < 2)
2500 /* Returns TRUE if the caps are compatible with the caps specified in the 'caps'
2501 * property (which by default are the raw caps)
2503 * The decodebin_lock should be taken !
2506 are_final_caps (GstDecodeBin * dbin, GstCaps * caps)
2510 GST_LOG_OBJECT (dbin, "Checking with caps %" GST_PTR_FORMAT, caps);
2512 /* lock for getting the caps */
2513 GST_OBJECT_LOCK (dbin);
2514 res = gst_caps_can_intersect (dbin->caps, caps);
2515 GST_OBJECT_UNLOCK (dbin);
2517 GST_LOG_OBJECT (dbin, "Caps are %sfinal caps", res ? "" : "not ");
2523 * GstDecodeChain functions
2526 /* gst_decode_chain_get_current_group:
2528 * Returns the current group of this chain, to which
2529 * new chains should be attached or NULL if the last
2530 * group didn't have no-more-pads.
2532 * Not MT-safe: Call with parent chain lock!
2534 static GstDecodeGroup *
2535 gst_decode_chain_get_current_group (GstDecodeChain * chain)
2537 GstDecodeGroup *group;
2539 if (!chain->next_groups && chain->active_group
2540 && chain->active_group->overrun && !chain->active_group->no_more_pads) {
2541 GST_WARNING_OBJECT (chain->dbin,
2542 "Currently active group %p is exposed"
2543 " and wants to add a new pad without having signaled no-more-pads",
2544 chain->active_group);
2548 if (chain->next_groups && (group = chain->next_groups->data) && group->overrun
2549 && !group->no_more_pads) {
2550 GST_WARNING_OBJECT (chain->dbin,
2551 "Currently newest pending group %p "
2552 "had overflow but didn't signal no-more-pads", group);
2556 /* Now we know that we can really return something useful */
2557 if (!chain->active_group) {
2558 chain->active_group = group = gst_decode_group_new (chain->dbin, chain);
2559 } else if (!chain->active_group->overrun
2560 && !chain->active_group->no_more_pads) {
2561 group = chain->active_group;
2562 } else if (chain->next_groups && (group = chain->next_groups->data)
2563 && !group->overrun && !group->no_more_pads) {
2564 /* group = chain->next_groups->data */
2566 group = gst_decode_group_new (chain->dbin, chain);
2567 chain->next_groups = g_list_prepend (chain->next_groups, group);
2573 static void gst_decode_group_free_internal (GstDecodeGroup * group,
2577 gst_decode_chain_free_internal (GstDecodeChain * chain, gboolean hide)
2581 CHAIN_MUTEX_LOCK (chain);
2583 GST_DEBUG_OBJECT (chain->dbin, "%s chain %p", (hide ? "Hiding" : "Freeing"),
2586 if (chain->active_group) {
2587 gst_decode_group_free_internal (chain->active_group, hide);
2589 chain->active_group = NULL;
2592 for (l = chain->next_groups; l; l = l->next) {
2593 gst_decode_group_free_internal ((GstDecodeGroup *) l->data, hide);
2598 g_list_free (chain->next_groups);
2599 chain->next_groups = NULL;
2603 for (l = chain->old_groups; l; l = l->next) {
2604 GstDecodeGroup *group = l->data;
2606 gst_decode_group_free (group);
2608 g_list_free (chain->old_groups);
2609 chain->old_groups = NULL;
2612 for (l = chain->pending_pads; l; l = l->next) {
2613 GstPendingPad *ppad = l->data;
2614 GstPad *pad = ppad->pad;
2616 g_signal_handlers_disconnect_by_func (pad, caps_notify_cb, chain);
2617 gst_pending_pad_free (ppad);
2620 g_list_free (chain->pending_pads);
2621 chain->pending_pads = NULL;
2623 for (l = chain->elements; l; l = l->next) {
2624 GstDecodeElement *delem = l->data;
2625 GstElement *element = delem->element;
2627 g_signal_handlers_disconnect_by_func (element, pad_added_cb, chain);
2628 g_signal_handlers_disconnect_by_func (element, pad_removed_cb, chain);
2629 g_signal_handlers_disconnect_by_func (element, no_more_pads_cb, chain);
2631 if (delem->capsfilter) {
2632 if (GST_OBJECT_PARENT (delem->capsfilter) ==
2633 GST_OBJECT_CAST (chain->dbin))
2634 gst_bin_remove (GST_BIN_CAST (chain->dbin), delem->capsfilter);
2636 gst_element_set_state (delem->capsfilter, GST_STATE_NULL);
2640 if (GST_OBJECT_PARENT (element) == GST_OBJECT_CAST (chain->dbin))
2641 gst_bin_remove (GST_BIN_CAST (chain->dbin), element);
2643 gst_element_set_state (element, GST_STATE_NULL);
2646 SUBTITLE_LOCK (chain->dbin);
2647 /* remove possible subtitle element */
2648 chain->dbin->subtitles = g_list_remove (chain->dbin->subtitles, element);
2649 SUBTITLE_UNLOCK (chain->dbin);
2652 if (delem->capsfilter) {
2653 gst_object_unref (delem->capsfilter);
2654 delem->capsfilter = NULL;
2657 gst_object_unref (element);
2660 g_slice_free (GstDecodeElement, delem);
2664 g_list_free (chain->elements);
2665 chain->elements = NULL;
2668 if (chain->endpad) {
2669 if (chain->endpad->exposed) {
2670 gst_element_remove_pad (GST_ELEMENT_CAST (chain->dbin),
2671 GST_PAD_CAST (chain->endpad));
2674 gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (chain->endpad), NULL);
2675 chain->endpad->exposed = FALSE;
2677 gst_object_unref (chain->endpad);
2678 chain->endpad = NULL;
2683 gst_object_unref (chain->pad);
2687 if (chain->endcaps) {
2688 gst_caps_unref (chain->endcaps);
2689 chain->endcaps = NULL;
2692 GST_DEBUG_OBJECT (chain->dbin, "%s chain %p", (hide ? "Hidden" : "Freed"),
2694 CHAIN_MUTEX_UNLOCK (chain);
2696 g_mutex_free (chain->lock);
2697 g_slice_free (GstDecodeChain, chain);
2701 /* gst_decode_chain_free:
2703 * Completely frees and removes the chain and all
2704 * child groups from decodebin.
2706 * MT-safe, don't hold the chain lock or any child chain's lock
2707 * when calling this!
2710 gst_decode_chain_free (GstDecodeChain * chain)
2712 gst_decode_chain_free_internal (chain, FALSE);
2715 /* gst_decode_chain_new:
2717 * Creates a new decode chain and initializes it.
2719 * It's up to the caller to add it to the list of child chains of
2722 static GstDecodeChain *
2723 gst_decode_chain_new (GstDecodeBin * dbin, GstDecodeGroup * parent,
2726 GstDecodeChain *chain = g_slice_new0 (GstDecodeChain);
2728 GST_DEBUG_OBJECT (dbin, "Creating new chain %p with parent group %p", chain,
2732 chain->parent = parent;
2733 chain->lock = g_mutex_new ();
2734 chain->pad = gst_object_ref (pad);
2740 * GstDecodeGroup functions
2743 /* The overrun callback is used to expose groups that have not yet had their
2744 * no_more_pads called while the (large) multiqueue overflowed. When this
2745 * happens we must assume that the no_more_pads will not arrive anymore and we
2746 * must expose the pads that we have.
2749 multi_queue_overrun_cb (GstElement * queue, GstDecodeGroup * group)
2755 GST_LOG_OBJECT (dbin, "multiqueue '%s' (%p) is full", GST_OBJECT_NAME (queue),
2758 group->overrun = TRUE;
2759 /* this group has prerolled enough to not need more pads,
2760 * we can probably set its buffering state to playing now */
2761 GST_DEBUG_OBJECT (group->dbin, "Setting group %p multiqueue to "
2762 "'playing' buffering mode", group);
2763 decodebin_set_queue_size (group->dbin, group->multiqueue, FALSE,
2764 (group->parent ? group->parent->seekable : TRUE));
2766 /* FIXME: We should make sure that everything gets exposed now
2767 * even if child chains are not complete because the will never
2768 * be complete! Ignore any non-complete chains when exposing
2769 * and never expose them later
2773 if (gst_decode_chain_is_complete (dbin->decode_chain)) {
2774 if (!gst_decode_bin_expose (dbin))
2775 GST_WARNING_OBJECT (dbin, "Couldn't expose group");
2777 EXPOSE_UNLOCK (group->dbin);
2781 gst_decode_group_free_internal (GstDecodeGroup * group, gboolean hide)
2785 GST_DEBUG_OBJECT (group->dbin, "%s group %p", (hide ? "Hiding" : "Freeing"),
2787 for (l = group->children; l; l = l->next) {
2788 GstDecodeChain *chain = (GstDecodeChain *) l->data;
2790 gst_decode_chain_free_internal (chain, hide);
2795 g_list_free (group->children);
2796 group->children = NULL;
2800 for (l = group->reqpads; l; l = l->next) {
2801 GstPad *pad = l->data;
2803 gst_element_release_request_pad (group->multiqueue, pad);
2804 gst_object_unref (pad);
2807 g_list_free (group->reqpads);
2808 group->reqpads = NULL;
2811 if (group->multiqueue) {
2812 if (group->overrunsig) {
2813 g_signal_handler_disconnect (group->multiqueue, group->overrunsig);
2814 group->overrunsig = 0;
2817 if (GST_OBJECT_PARENT (group->multiqueue) == GST_OBJECT_CAST (group->dbin))
2818 gst_bin_remove (GST_BIN_CAST (group->dbin), group->multiqueue);
2820 gst_element_set_state (group->multiqueue, GST_STATE_NULL);
2821 gst_object_unref (group->multiqueue);
2822 group->multiqueue = NULL;
2826 GST_DEBUG_OBJECT (group->dbin, "%s group %p", (hide ? "Hided" : "Freed"),
2829 g_slice_free (GstDecodeGroup, group);
2832 /* gst_decode_group_free:
2834 * Completely frees and removes the decode group and all
2837 * Never call this from any streaming thread!
2839 * Not MT-safe, call with parent's chain lock!
2842 gst_decode_group_free (GstDecodeGroup * group)
2844 gst_decode_group_free_internal (group, FALSE);
2847 /* gst_decode_group_hide:
2849 * Hide the decode group only, this means that
2850 * all child endpads are removed from decodebin
2851 * and all signals are unconnected.
2853 * No element is set to NULL state and completely
2856 * Can be called from streaming threads.
2858 * Not MT-safe, call with parent's chain lock!
2861 gst_decode_group_hide (GstDecodeGroup * group)
2863 gst_decode_group_free_internal (group, TRUE);
2866 /* configure queue sizes, this depends on the buffering method and if we are
2867 * playing or prerolling. */
2869 decodebin_set_queue_size (GstDecodeBin * dbin, GstElement * multiqueue,
2870 gboolean preroll, gboolean seekable)
2872 guint max_bytes, max_buffers;
2875 if (preroll || dbin->use_buffering) {
2876 /* takes queue limits, initially we only queue up up to the max bytes limit,
2877 * with a default of 2MB. we use the same values for buffering mode. */
2878 if ((max_bytes = dbin->max_size_bytes) == 0)
2879 max_bytes = AUTO_PREROLL_SIZE_BYTES;
2880 if ((max_buffers = dbin->max_size_buffers) == 0)
2881 max_buffers = AUTO_PREROLL_SIZE_BUFFERS;
2882 if ((max_time = dbin->max_size_time) == 0)
2883 max_time = seekable ? AUTO_PREROLL_SEEKABLE_SIZE_TIME :
2884 AUTO_PREROLL_NOT_SEEKABLE_SIZE_TIME;
2886 /* update runtime limits. At runtime, we try to keep the amount of buffers
2887 * in the queues as low as possible (but at least 5 buffers). */
2888 if ((max_bytes = dbin->max_size_bytes) == 0)
2889 max_bytes = AUTO_PLAY_SIZE_BYTES;
2890 if ((max_buffers = dbin->max_size_buffers) == 0)
2891 max_buffers = AUTO_PLAY_SIZE_BUFFERS;
2892 if ((max_time = dbin->max_size_time) == 0)
2893 max_time = AUTO_PLAY_SIZE_TIME;
2896 g_object_set (multiqueue,
2897 "max-size-bytes", max_bytes, "max-size-time", max_time,
2898 "max-size-buffers", max_buffers, NULL);
2901 /* gst_decode_group_new:
2902 * @dbin: Parent decodebin
2903 * @parent: Parent chain or %NULL
2905 * Creates a new GstDecodeGroup. It is up to the caller to add it to the list
2908 static GstDecodeGroup *
2909 gst_decode_group_new (GstDecodeBin * dbin, GstDecodeChain * parent)
2911 GstDecodeGroup *group = g_slice_new0 (GstDecodeGroup);
2915 GST_DEBUG_OBJECT (dbin, "Creating new group %p with parent chain %p", group,
2919 group->parent = parent;
2921 mq = group->multiqueue = gst_element_factory_make ("multiqueue", NULL);
2922 if (G_UNLIKELY (!group->multiqueue))
2923 goto missing_multiqueue;
2925 /* default is for use-buffering is FALSE */
2926 if (dbin->use_buffering) {
2928 "use-buffering", TRUE,
2929 "low-percent", dbin->low_percent,
2930 "high-percent", dbin->high_percent, NULL);
2933 /* configure queue sizes for preroll */
2935 if (parent && parent->demuxer) {
2936 GstElement *element =
2937 ((GstDecodeElement *) parent->elements->data)->element;
2938 GstPad *pad = gst_element_get_static_pad (element, "sink");
2940 seekable = parent->seekable = check_upstream_seekable (dbin, pad);
2941 gst_object_unref (pad);
2944 decodebin_set_queue_size (dbin, mq, TRUE, seekable);
2946 group->overrunsig = g_signal_connect (G_OBJECT (mq), "overrun",
2947 G_CALLBACK (multi_queue_overrun_cb), group);
2949 gst_bin_add (GST_BIN (dbin), gst_object_ref (mq));
2950 gst_element_set_state (mq, GST_STATE_PAUSED);
2957 gst_element_post_message (GST_ELEMENT_CAST (dbin),
2958 gst_missing_element_message_new (GST_ELEMENT_CAST (dbin),
2960 GST_ELEMENT_ERROR (dbin, CORE, MISSING_PLUGIN, (NULL), ("no multiqueue!"));
2961 g_slice_free (GstDecodeGroup, group);
2966 /* gst_decode_group_control_demuxer_pad
2968 * Adds a new demuxer srcpad to the given group.
2970 * Returns the srcpad of the multiqueue corresponding the given pad.
2971 * Returns NULL if there was an error.
2974 gst_decode_group_control_demuxer_pad (GstDecodeGroup * group, GstPad * pad)
2977 GstPad *srcpad, *sinkpad;
2978 GstIterator *it = NULL;
2979 GValue item = { 0, };
2983 GST_LOG_OBJECT (dbin, "group:%p pad %s:%s", group, GST_DEBUG_PAD_NAME (pad));
2987 if (G_UNLIKELY (!group->multiqueue))
2990 if (!(sinkpad = gst_element_get_request_pad (group->multiqueue, "sink_%u"))) {
2991 GST_ERROR_OBJECT (dbin, "Couldn't get sinkpad from multiqueue");
2995 if ((gst_pad_link (pad, sinkpad) != GST_PAD_LINK_OK)) {
2996 GST_ERROR_OBJECT (dbin, "Couldn't link demuxer and multiqueue");
3000 it = gst_pad_iterate_internal_links (sinkpad);
3002 if (!it || (gst_iterator_next (it, &item)) != GST_ITERATOR_OK
3003 || ((srcpad = g_value_dup_object (&item)) == NULL)) {
3004 GST_ERROR_OBJECT (dbin,
3005 "Couldn't get srcpad from multiqueue for sinkpad %" GST_PTR_FORMAT,
3009 CHAIN_MUTEX_LOCK (group->parent);
3010 group->reqpads = g_list_prepend (group->reqpads, gst_object_ref (sinkpad));
3011 CHAIN_MUTEX_UNLOCK (group->parent);
3014 g_value_unset (&item);
3016 gst_iterator_free (it);
3017 gst_object_unref (sinkpad);
3021 gst_element_release_request_pad (group->multiqueue, sinkpad);
3025 /* gst_decode_group_is_complete:
3027 * Checks if the group is complete, this means that
3028 * a) overrun of the multiqueue or no-more-pads happened
3029 * b) all child chains are complete
3031 * Not MT-safe, always call with decodebin expose lock
3034 gst_decode_group_is_complete (GstDecodeGroup * group)
3037 gboolean complete = TRUE;
3039 if (!group->overrun && !group->no_more_pads) {
3044 for (l = group->children; l; l = l->next) {
3045 GstDecodeChain *chain = l->data;
3047 if (!gst_decode_chain_is_complete (chain)) {
3054 GST_DEBUG_OBJECT (group->dbin, "Group %p is complete: %d", group, complete);
3058 /* gst_decode_chain_is_complete:
3060 * Returns TRUE if the chain is complete, this means either
3061 * a) This chain is a dead end, i.e. we have no suitable plugins
3062 * b) This chain ends in an endpad and this is blocked or exposed
3064 * Not MT-safe, always call with decodebin expose lock
3067 gst_decode_chain_is_complete (GstDecodeChain * chain)
3069 gboolean complete = FALSE;
3071 CHAIN_MUTEX_LOCK (chain);
3072 if (chain->deadend) {
3077 if (chain->endpad && (chain->endpad->blocked || chain->endpad->exposed)) {
3082 if (chain->demuxer) {
3083 if (chain->active_group
3084 && gst_decode_group_is_complete (chain->active_group)) {
3091 CHAIN_MUTEX_UNLOCK (chain);
3092 GST_DEBUG_OBJECT (chain->dbin, "Chain %p is complete: %d", chain, complete);
3097 drain_and_switch_chains (GstDecodeChain * chain, GstDecodePad * drainpad,
3098 gboolean * last_group, gboolean * drained, gboolean * switched);
3099 /* drain_and_switch_chains/groups:
3101 * CALL WITH CHAIN LOCK (or group parent) TAKEN !
3103 * Goes down the chains/groups until it finds the chain
3104 * to which the drainpad belongs.
3106 * It marks that pad/chain as drained and then will figure
3107 * out which group to switch to or not.
3109 * last_chain will be set to TRUE if the group to which the
3110 * pad belongs is the last one.
3112 * drained will be set to TRUE if the chain/group is drained.
3114 * Returns: TRUE if the chain contained the target pad */
3116 drain_and_switch_group (GstDecodeGroup * group, GstDecodePad * drainpad,
3117 gboolean * last_group, gboolean * drained, gboolean * switched)
3119 gboolean handled = FALSE;
3120 gboolean alldrained = TRUE;
3123 GST_DEBUG ("Checking group %p (target pad %s:%s)",
3124 group, GST_DEBUG_PAD_NAME (drainpad));
3126 /* Definitely can't be in drained groups */
3127 if (G_UNLIKELY (group->drained)) {
3131 /* Figure out if all our chains are drained with the
3132 * new information */
3133 for (tmp = group->children; tmp; tmp = tmp->next) {
3134 GstDecodeChain *chain = (GstDecodeChain *) tmp->data;
3135 gboolean subdrained = FALSE;
3138 drain_and_switch_chains (chain, drainpad, last_group, &subdrained,
3145 GST_DEBUG ("group %p (last_group:%d, drained:%d, switched:%d, handled:%d)",
3146 group, *last_group, alldrained, *switched, handled);
3147 *drained = alldrained;
3152 drain_and_switch_chains (GstDecodeChain * chain, GstDecodePad * drainpad,
3153 gboolean * last_group, gboolean * drained, gboolean * switched)
3155 gboolean handled = FALSE;
3156 GstDecodeBin *dbin = chain->dbin;
3158 GST_DEBUG ("Checking chain %p (target pad %s:%s)",
3159 chain, GST_DEBUG_PAD_NAME (drainpad));
3161 CHAIN_MUTEX_LOCK (chain);
3163 if (chain->endpad) {
3164 /* Check if we're reached the target endchain */
3165 if (chain == drainpad->chain) {
3166 GST_DEBUG ("Found the target chain");
3167 drainpad->drained = TRUE;
3171 *drained = chain->endpad->drained;
3175 /* We known there are groups to switch to */
3176 if (chain->next_groups)
3177 *last_group = FALSE;
3179 /* Check the active group */
3180 if (chain->active_group) {
3181 gboolean subdrained = FALSE;
3182 handled = drain_and_switch_group (chain->active_group, drainpad,
3183 last_group, &subdrained, switched);
3185 /* The group is drained, see if we can switch to another */
3186 if (handled && subdrained && !*switched) {
3187 if (chain->next_groups) {
3188 /* Switch to next group */
3189 GST_DEBUG_OBJECT (dbin, "Hiding current group %p", chain->active_group);
3190 gst_decode_group_hide (chain->active_group);
3192 g_list_prepend (chain->old_groups, chain->active_group);
3193 GST_DEBUG_OBJECT (dbin, "Switching to next group %p",
3194 chain->next_groups->data);
3195 chain->active_group = chain->next_groups->data;
3196 chain->next_groups =
3197 g_list_delete_link (chain->next_groups, chain->next_groups);
3201 GST_DEBUG ("Group %p was the last in chain %p", chain->active_group,
3204 /* We're drained ! */
3210 CHAIN_MUTEX_UNLOCK (chain);
3212 GST_DEBUG ("Chain %p (handled:%d, last_group:%d, drained:%d, switched:%d)",
3213 chain, handled, *last_group, *drained, *switched);
3216 g_signal_emit (dbin, gst_decode_bin_signals[SIGNAL_DRAINED], 0, NULL);
3221 /* check if the group is drained, meaning all pads have seen an EOS
3224 gst_decode_pad_handle_eos (GstDecodePad * pad)
3226 gboolean last_group = TRUE;
3227 gboolean switched = FALSE;
3228 gboolean drained = FALSE;
3229 GstDecodeChain *chain = pad->chain;
3230 GstDecodeBin *dbin = chain->dbin;
3232 GST_LOG_OBJECT (dbin, "pad %p", pad);
3233 drain_and_switch_chains (dbin->decode_chain, pad, &last_group, &drained,
3237 /* If we resulted in a group switch, expose what's needed */
3239 if (gst_decode_chain_is_complete (dbin->decode_chain))
3240 gst_decode_bin_expose (dbin);
3241 EXPOSE_UNLOCK (dbin);
3247 /* gst_decode_group_is_drained:
3249 * Check is this group is drained and cache this result.
3250 * The group is drained if all child chains are drained.
3252 * Not MT-safe, call with group->parent's lock */
3254 gst_decode_group_is_drained (GstDecodeGroup * group)
3257 gboolean drained = TRUE;
3259 if (group->drained) {
3264 for (l = group->children; l; l = l->next) {
3265 GstDecodeChain *chain = l->data;
3267 CHAIN_MUTEX_LOCK (chain);
3268 if (!gst_decode_chain_is_drained (chain))
3270 CHAIN_MUTEX_UNLOCK (chain);
3274 group->drained = drained;
3277 GST_DEBUG_OBJECT (group->dbin, "Group %p is drained: %d", group, drained);
3281 /* gst_decode_chain_is_drained:
3283 * Check is the chain is drained, which means that
3286 * a) it's endpad is drained
3287 * b) there are no pending pads, the active group is drained
3288 * and there are no next groups
3290 * Not MT-safe, call with chain lock
3293 gst_decode_chain_is_drained (GstDecodeChain * chain)
3295 gboolean drained = FALSE;
3297 if (chain->endpad) {
3298 drained = chain->endpad->drained;
3302 if (chain->pending_pads) {
3307 if (chain->active_group && gst_decode_group_is_drained (chain->active_group)
3308 && !chain->next_groups) {
3314 GST_DEBUG_OBJECT (chain->dbin, "Chain %p is drained: %d", chain, drained);
3319 * GCompareFunc to use with lists of GstPad.
3320 * Sorts pads by mime type.
3321 * First video (raw, then non-raw), then audio (raw, then non-raw),
3324 * Return: negative if a<b, 0 if a==b, positive if a>b
3327 sort_end_pads (GstDecodePad * da, GstDecodePad * db)
3330 GstCaps *capsa, *capsb;
3331 GstStructure *sa, *sb;
3332 const gchar *namea, *nameb;
3334 capsa = get_pad_caps (GST_PAD_CAST (da));
3335 capsb = get_pad_caps (GST_PAD_CAST (db));
3337 sa = gst_caps_get_structure ((const GstCaps *) capsa, 0);
3338 sb = gst_caps_get_structure ((const GstCaps *) capsb, 0);
3340 namea = gst_structure_get_name (sa);
3341 nameb = gst_structure_get_name (sb);
3343 if (g_strrstr (namea, "video/x-raw"))
3345 else if (g_strrstr (namea, "video/"))
3347 else if (g_strrstr (namea, "audio/x-raw"))
3349 else if (g_strrstr (namea, "audio/"))
3354 if (g_strrstr (nameb, "video/x-raw"))
3356 else if (g_strrstr (nameb, "video/"))
3358 else if (g_strrstr (nameb, "audio/x-raw"))
3360 else if (g_strrstr (nameb, "audio/"))
3365 gst_caps_unref (capsa);
3366 gst_caps_unref (capsb);
3372 _gst_element_get_linked_caps (GstElement * src, GstElement * sink,
3378 gboolean done = FALSE;
3379 GstCaps *caps = NULL;
3380 GValue item = { 0, };
3382 it = gst_element_iterate_src_pads (src);
3384 switch (gst_iterator_next (it, &item)) {
3385 case GST_ITERATOR_OK:
3386 pad = g_value_get_object (&item);
3387 peer = gst_pad_get_peer (pad);
3389 parent = gst_pad_get_parent_element (peer);
3390 if (parent == sink) {
3391 caps = gst_pad_get_current_caps (pad);
3393 gst_object_ref (pad);
3400 gst_object_unref (parent);
3401 gst_object_unref (peer);
3403 g_value_reset (&item);
3405 case GST_ITERATOR_RESYNC:
3406 gst_iterator_resync (it);
3408 case GST_ITERATOR_ERROR:
3409 case GST_ITERATOR_DONE:
3414 g_value_unset (&item);
3415 gst_iterator_free (it);
3420 static GQuark topology_structure_name = 0;
3421 static GQuark topology_caps = 0;
3422 static GQuark topology_next = 0;
3423 static GQuark topology_pad = 0;
3424 static GQuark topology_element_srcpad = 0;
3426 /* FIXME: Invent gst_structure_take_structure() to prevent all the
3427 * structure copying for nothing
3429 static GstStructure *
3430 gst_decode_chain_get_topology (GstDecodeChain * chain)
3432 GstStructure *s, *u;
3436 if (G_UNLIKELY ((chain->endpad || chain->deadend)
3437 && (chain->endcaps == NULL))) {
3438 GST_WARNING ("End chain without valid caps !");
3442 u = gst_structure_new_id_empty (topology_structure_name);
3444 /* Now at the last element */
3445 if ((chain->elements || !chain->active_group) &&
3446 (chain->endpad || chain->deadend)) {
3447 s = gst_structure_new_id_empty (topology_structure_name);
3448 gst_structure_id_set (u, topology_caps, GST_TYPE_CAPS, chain->endcaps,
3451 if (chain->endpad) {
3452 gst_structure_id_set (u, topology_pad, GST_TYPE_PAD, chain->endpad, NULL);
3453 gst_structure_id_set (u, topology_element_srcpad, GST_TYPE_PAD,
3454 chain->endpad, NULL);
3456 gst_structure_id_set (s, topology_next, GST_TYPE_STRUCTURE, u, NULL);
3457 gst_structure_free (u);
3459 } else if (chain->active_group) {
3460 GValue list = { 0, };
3461 GValue item = { 0, };
3463 g_value_init (&list, GST_TYPE_LIST);
3464 g_value_init (&item, GST_TYPE_STRUCTURE);
3465 for (l = chain->active_group->children; l; l = l->next) {
3466 s = gst_decode_chain_get_topology (l->data);
3468 gst_value_set_structure (&item, s);
3469 gst_value_list_append_value (&list, &item);
3470 g_value_reset (&item);
3471 gst_structure_free (s);
3474 gst_structure_id_set_value (u, topology_next, &list);
3475 g_value_unset (&list);
3476 g_value_unset (&item);
3479 /* Get caps between all elements in this chain */
3480 l = (chain->elements && chain->elements->next) ? chain->elements : NULL;
3481 for (; l && l->next; l = l->next) {
3482 GstDecodeElement *delem, *delem_next;
3483 GstElement *elem, *elem_next;
3488 elem = delem->element;
3489 delem_next = l->next->data;
3490 elem_next = delem_next->element;
3493 caps = _gst_element_get_linked_caps (elem_next, elem, &srcpad);
3496 s = gst_structure_new_id_empty (topology_structure_name);
3497 gst_structure_id_set (u, topology_caps, GST_TYPE_CAPS, caps, NULL);
3498 gst_caps_unref (caps);
3500 gst_structure_id_set (s, topology_next, GST_TYPE_STRUCTURE, u, NULL);
3501 gst_structure_free (u);
3506 gst_structure_id_set (u, topology_element_srcpad, GST_TYPE_PAD, srcpad,
3508 gst_object_unref (srcpad);
3512 /* Caps that resulted in this chain */
3513 caps = gst_pad_get_current_caps (chain->pad);
3515 caps = get_pad_caps (chain->pad);
3516 if (G_UNLIKELY (!gst_caps_is_fixed (caps))) {
3517 GST_ERROR_OBJECT (chain->pad,
3518 "Couldn't get fixed caps, got %" GST_PTR_FORMAT, caps);
3519 gst_caps_unref (caps);
3523 gst_structure_id_set (u, topology_caps, GST_TYPE_CAPS, caps, NULL);
3524 gst_structure_id_set (u, topology_element_srcpad, GST_TYPE_PAD, chain->pad,
3526 gst_caps_unref (caps);
3532 gst_decode_bin_post_topology_message (GstDecodeBin * dbin)
3537 s = gst_decode_chain_get_topology (dbin->decode_chain);
3539 msg = gst_message_new_element (GST_OBJECT (dbin), s);
3540 gst_element_post_message (GST_ELEMENT (dbin), msg);
3543 /* Must only be called if the toplevel chain is complete and blocked! */
3544 /* Not MT-safe, call with decodebin expose lock! */
3546 gst_decode_bin_expose (GstDecodeBin * dbin)
3548 GList *tmp, *endpads = NULL;
3549 gboolean missing_plugin = FALSE;
3550 gboolean already_exposed = TRUE;
3552 GST_DEBUG_OBJECT (dbin, "Exposing currently active chains/groups");
3554 /* Don't expose if we're currently shutting down */
3556 if (G_UNLIKELY (dbin->shutdown == TRUE)) {
3557 GST_WARNING_OBJECT (dbin, "Currently, shutting down, aborting exposing");
3563 /* Get the pads that we're going to expose and mark things as exposed */
3564 if (!gst_decode_chain_expose (dbin->decode_chain, &endpads, &missing_plugin)) {
3565 g_list_foreach (endpads, (GFunc) gst_object_unref, NULL);
3566 g_list_free (endpads);
3567 GST_ERROR_OBJECT (dbin, "Broken chain/group tree");
3568 g_return_val_if_reached (FALSE);
3571 if (endpads == NULL) {
3572 if (missing_plugin) {
3573 GST_WARNING_OBJECT (dbin, "No suitable plugins found");
3574 GST_ELEMENT_ERROR (dbin, CORE, MISSING_PLUGIN, (NULL),
3575 ("no suitable plugins found"));
3577 /* in this case, the stream ended without buffers,
3578 * just post a warning */
3579 GST_WARNING_OBJECT (dbin, "All streams finished without buffers");
3580 GST_ELEMENT_ERROR (dbin, STREAM, FAILED, (NULL),
3581 ("all streams without buffers"));
3586 /* Check if this was called when everything was exposed already */
3587 for (tmp = endpads; tmp && already_exposed; tmp = tmp->next) {
3588 GstDecodePad *dpad = tmp->data;
3590 already_exposed &= dpad->exposed;
3591 if (!already_exposed)
3594 if (already_exposed) {
3595 GST_DEBUG_OBJECT (dbin, "Everything was exposed already!");
3596 g_list_foreach (endpads, (GFunc) gst_object_unref, NULL);
3597 g_list_free (endpads);
3601 /* Set all already exposed pads to blocked */
3602 for (tmp = endpads; tmp; tmp = tmp->next) {
3603 GstDecodePad *dpad = tmp->data;
3605 if (dpad->exposed) {
3606 GST_DEBUG_OBJECT (dpad, "blocking exposed pad");
3607 gst_decode_pad_set_blocked (dpad, TRUE);
3611 /* re-order pads : video, then audio, then others */
3612 endpads = g_list_sort (endpads, (GCompareFunc) sort_end_pads);
3615 for (tmp = endpads; tmp; tmp = tmp->next) {
3616 GstDecodePad *dpad = (GstDecodePad *) tmp->data;
3619 /* 1. rewrite name */
3620 padname = g_strdup_printf ("src_%u", dbin->nbpads);
3622 GST_DEBUG_OBJECT (dbin, "About to expose dpad %s as %s",
3623 GST_OBJECT_NAME (dpad), padname);
3624 gst_object_set_name (GST_OBJECT (dpad), padname);
3627 /* 2. activate and add */
3629 && !gst_element_add_pad (GST_ELEMENT (dbin), GST_PAD_CAST (dpad))) {
3630 /* not really fatal, we can try to add the other pads */
3631 g_warning ("error adding pad to decodebin");
3634 dpad->exposed = TRUE;
3636 /* 3. emit signal */
3637 GST_INFO_OBJECT (dpad, "added new decoded pad");
3640 /* 4. Signal no-more-pads. This allows the application to hook stuff to the
3642 GST_LOG_OBJECT (dbin, "signaling no-more-pads");
3643 gst_element_no_more_pads (GST_ELEMENT (dbin));
3645 /* 5. Send a custom element message with the stream topology */
3646 if (dbin->post_stream_topology)
3647 gst_decode_bin_post_topology_message (dbin);
3649 /* 6. Unblock internal pads. The application should have connected stuff now
3650 * so that streaming can continue. */
3651 for (tmp = endpads; tmp; tmp = tmp->next) {
3652 GstDecodePad *dpad = (GstDecodePad *) tmp->data;
3654 GST_DEBUG_OBJECT (dpad, "unblocking");
3655 gst_decode_pad_unblock (dpad);
3656 GST_DEBUG_OBJECT (dpad, "unblocked");
3657 gst_object_unref (dpad);
3659 g_list_free (endpads);
3661 do_async_done (dbin);
3662 GST_DEBUG_OBJECT (dbin, "Exposed everything");
3666 /* gst_decode_chain_expose:
3668 * Check if the chain can be exposed and add all endpads
3669 * to the endpads list.
3671 * Also update the active group's multiqueue to the
3674 * Not MT-safe, call with decodebin expose lock! *
3677 gst_decode_chain_expose (GstDecodeChain * chain, GList ** endpads,
3678 gboolean * missing_plugin)
3680 GstDecodeGroup *group;
3684 if (chain->deadend) {
3686 *missing_plugin = TRUE;
3690 if (chain->endpad) {
3691 if (!chain->endpad->blocked && !chain->endpad->exposed)
3693 *endpads = g_list_prepend (*endpads, gst_object_ref (chain->endpad));
3697 group = chain->active_group;
3700 if (!group->no_more_pads && !group->overrun)
3705 /* configure queues for playback */
3706 decodebin_set_queue_size (dbin, group->multiqueue, FALSE, TRUE);
3708 /* we can now disconnect any overrun signal, which is used to expose the
3710 if (group->overrunsig) {
3711 GST_LOG_OBJECT (dbin, "Disconnecting overrun");
3712 g_signal_handler_disconnect (group->multiqueue, group->overrunsig);
3713 group->overrunsig = 0;
3716 for (l = group->children; l; l = l->next) {
3717 GstDecodeChain *childchain = l->data;
3719 if (!gst_decode_chain_expose (childchain, endpads, missing_plugin))
3726 /*************************
3727 * GstDecodePad functions
3728 *************************/
3731 gst_decode_pad_class_init (GstDecodePadClass * klass)
3736 gst_decode_pad_init (GstDecodePad * pad)
3739 pad->blocked = FALSE;
3740 pad->exposed = FALSE;
3741 pad->drained = FALSE;
3742 gst_object_ref_sink (pad);
3745 static GstPadProbeReturn
3746 source_pad_blocked_cb (GstPad * pad, GstPadProbeInfo * info, gpointer user_data)
3748 GstDecodePad *dpad = user_data;
3749 GstDecodeChain *chain;
3752 chain = dpad->chain;
3755 GST_LOG_OBJECT (dpad, "blocked: dpad->chain:%p", chain);
3757 dpad->blocked = TRUE;
3760 if (gst_decode_chain_is_complete (dbin->decode_chain)) {
3761 if (!gst_decode_bin_expose (dbin))
3762 GST_WARNING_OBJECT (dbin, "Couldn't expose group");
3764 EXPOSE_UNLOCK (dbin);
3766 return GST_PAD_PROBE_OK;
3769 static GstPadProbeReturn
3770 source_pad_event_probe (GstPad * pad, GstPadProbeInfo * info,
3773 GstEvent *event = GST_PAD_PROBE_INFO_EVENT (info);
3774 GstDecodePad *dpad = user_data;
3775 gboolean res = TRUE;
3777 GST_LOG_OBJECT (pad, "%s dpad:%p", GST_EVENT_TYPE_NAME (event), dpad);
3779 if (GST_EVENT_TYPE (event) == GST_EVENT_EOS) {
3780 GST_DEBUG_OBJECT (pad, "we received EOS");
3782 /* Check if all pads are drained.
3783 * * If there is no next group, we will let the EOS go through.
3784 * * If there is a next group but the current group isn't completely
3785 * drained, we will drop the EOS event.
3786 * * If there is a next group to expose and this was the last non-drained
3787 * pad for that group, we will remove the ghostpad of the current group
3788 * first, which unlinks the peer and so drops the EOS. */
3789 res = gst_decode_pad_handle_eos (dpad);
3792 return GST_PAD_PROBE_OK;
3794 return GST_PAD_PROBE_DROP;
3798 gst_decode_pad_set_blocked (GstDecodePad * dpad, gboolean blocked)
3800 GstDecodeBin *dbin = dpad->dbin;
3805 GST_DEBUG_OBJECT (dpad, "blocking pad: %d", blocked);
3807 opad = gst_ghost_pad_get_target (GST_GHOST_PAD_CAST (dpad));
3811 /* do not block if shutting down.
3812 * we do not consider/expect it blocked further below, but use other trick */
3813 if (!blocked || !dbin->shutdown) {
3815 if (dpad->block_id == 0)
3817 gst_pad_add_probe (opad, GST_PAD_PROBE_TYPE_BLOCK_DOWNSTREAM,
3818 source_pad_blocked_cb, gst_object_ref (dpad),
3819 (GDestroyNotify) gst_object_unref);
3821 if (dpad->block_id != 0) {
3822 gst_pad_remove_probe (opad, dpad->block_id);
3825 dpad->blocked = FALSE;
3830 if (dbin->shutdown) {
3831 /* deactivate to force flushing state to prevent NOT_LINKED errors */
3832 gst_pad_set_active (GST_PAD_CAST (dpad), FALSE);
3833 /* note that deactivating the target pad would have no effect here,
3834 * since elements are typically connected first (and pads exposed),
3835 * and only then brought to PAUSED state (so pads activated) */
3837 gst_object_ref (dpad);
3838 dbin->blocked_pads = g_list_prepend (dbin->blocked_pads, dpad);
3843 if ((l = g_list_find (dbin->blocked_pads, dpad))) {
3844 gst_object_unref (dpad);
3845 dbin->blocked_pads = g_list_delete_link (dbin->blocked_pads, l);
3848 gst_object_unref (opad);
3854 gst_decode_pad_add_drained_check (GstDecodePad * dpad)
3856 gst_pad_add_probe (GST_PAD_CAST (dpad), GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM,
3857 source_pad_event_probe, dpad, NULL);
3861 gst_decode_pad_activate (GstDecodePad * dpad, GstDecodeChain * chain)
3863 g_return_if_fail (chain != NULL);
3865 dpad->chain = chain;
3866 gst_pad_set_active (GST_PAD_CAST (dpad), TRUE);
3867 gst_decode_pad_set_blocked (dpad, TRUE);
3868 gst_decode_pad_add_drained_check (dpad);
3872 gst_decode_pad_unblock (GstDecodePad * dpad)
3874 gst_decode_pad_set_blocked (dpad, FALSE);
3877 /*gst_decode_pad_new:
3879 * Creates a new GstDecodePad for the given pad.
3881 static GstDecodePad *
3882 gst_decode_pad_new (GstDecodeBin * dbin, GstPad * pad, GstDecodeChain * chain)
3885 GstPadTemplate *pad_tmpl;
3887 GST_DEBUG_OBJECT (dbin, "making new decodepad");
3888 pad_tmpl = gst_static_pad_template_get (&decoder_bin_src_template);
3890 g_object_new (GST_TYPE_DECODE_PAD, "direction", GST_PAD_DIRECTION (pad),
3891 "template", pad_tmpl, NULL);
3892 gst_ghost_pad_construct (GST_GHOST_PAD_CAST (dpad));
3893 gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (dpad), pad);
3894 dpad->chain = chain;
3896 gst_object_unref (pad_tmpl);
3902 gst_pending_pad_free (GstPendingPad * ppad)
3905 g_assert (ppad->pad);
3907 if (ppad->event_probe_id != 0)
3908 gst_pad_remove_probe (ppad->pad, ppad->event_probe_id);
3909 gst_object_unref (ppad->pad);
3910 g_slice_free (GstPendingPad, ppad);
3914 * Element add/remove
3918 do_async_start (GstDecodeBin * dbin)
3920 GstMessage *message;
3922 dbin->async_pending = TRUE;
3924 message = gst_message_new_async_start (GST_OBJECT_CAST (dbin));
3925 parent_class->handle_message (GST_BIN_CAST (dbin), message);
3929 do_async_done (GstDecodeBin * dbin)
3931 GstMessage *message;
3933 if (dbin->async_pending) {
3934 message = gst_message_new_async_done (GST_OBJECT_CAST (dbin), FALSE);
3935 parent_class->handle_message (GST_BIN_CAST (dbin), message);
3937 dbin->async_pending = FALSE;
3942 * convenience functions
3947 * Returns the first sink pad of the given element, or NULL if it doesn't have
3952 find_sink_pad (GstElement * element)
3956 GValue item = { 0, };
3958 it = gst_element_iterate_sink_pads (element);
3960 if ((gst_iterator_next (it, &item)) == GST_ITERATOR_OK)
3961 pad = g_value_dup_object (&item);
3962 g_value_unset (&item);
3963 gst_iterator_free (it);
3968 /* call with dyn_lock held */
3970 unblock_pads (GstDecodeBin * dbin)
3974 GST_LOG_OBJECT (dbin, "unblocking pads");
3976 for (tmp = dbin->blocked_pads; tmp; tmp = tmp->next) {
3977 GstDecodePad *dpad = (GstDecodePad *) tmp->data;
3980 opad = gst_ghost_pad_get_target (GST_GHOST_PAD_CAST (dpad));
3984 GST_DEBUG_OBJECT (dpad, "unblocking");
3985 if (dpad->block_id != 0) {
3986 gst_pad_remove_probe (opad, dpad->block_id);
3989 dpad->blocked = FALSE;
3990 /* make flushing, prevent NOT_LINKED */
3991 GST_PAD_SET_FLUSHING (GST_PAD_CAST (dpad));
3992 gst_object_unref (dpad);
3993 gst_object_unref (opad);
3994 GST_DEBUG_OBJECT (dpad, "unblocked");
3997 /* clear, no more blocked pads */
3998 g_list_free (dbin->blocked_pads);
3999 dbin->blocked_pads = NULL;
4002 static GstStateChangeReturn
4003 gst_decode_bin_change_state (GstElement * element, GstStateChange transition)
4005 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
4006 GstDecodeBin *dbin = GST_DECODE_BIN (element);
4008 switch (transition) {
4009 case GST_STATE_CHANGE_NULL_TO_READY:
4010 if (dbin->typefind == NULL)
4011 goto missing_typefind;
4013 case GST_STATE_CHANGE_READY_TO_PAUSED:
4014 /* Make sure we've cleared all existing chains */
4015 if (dbin->decode_chain) {
4016 gst_decode_chain_free (dbin->decode_chain);
4017 dbin->decode_chain = NULL;
4020 GST_LOG_OBJECT (dbin, "clearing shutdown flag");
4021 dbin->shutdown = FALSE;
4023 dbin->have_type = FALSE;
4024 ret = GST_STATE_CHANGE_ASYNC;
4025 do_async_start (dbin);
4027 case GST_STATE_CHANGE_PAUSED_TO_READY:
4029 GST_LOG_OBJECT (dbin, "setting shutdown flag");
4030 dbin->shutdown = TRUE;
4031 unblock_pads (dbin);
4038 GstStateChangeReturn bret;
4040 bret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
4041 if (G_UNLIKELY (bret == GST_STATE_CHANGE_FAILURE))
4042 goto activate_failed;
4043 else if (G_UNLIKELY (bret == GST_STATE_CHANGE_NO_PREROLL)) {
4044 do_async_done (dbin);
4048 switch (transition) {
4049 case GST_STATE_CHANGE_PAUSED_TO_READY:
4050 do_async_done (dbin);
4051 if (dbin->decode_chain) {
4052 gst_decode_chain_free (dbin->decode_chain);
4053 dbin->decode_chain = NULL;
4056 case GST_STATE_CHANGE_READY_TO_NULL:
4066 gst_element_post_message (element,
4067 gst_missing_element_message_new (element, "typefind"));
4068 GST_ELEMENT_ERROR (dbin, CORE, MISSING_PLUGIN, (NULL), ("no typefind!"));
4069 return GST_STATE_CHANGE_FAILURE;
4073 GST_DEBUG_OBJECT (element,
4074 "element failed to change states -- activation problem?");
4075 return GST_STATE_CHANGE_FAILURE;
4080 gst_decode_bin_handle_message (GstBin * bin, GstMessage * msg)
4082 GstDecodeBin *dbin = GST_DECODE_BIN (bin);
4083 gboolean drop = FALSE;
4085 if (GST_MESSAGE_TYPE (msg) == GST_MESSAGE_ERROR) {
4086 GST_OBJECT_LOCK (dbin);
4087 drop = (g_list_find (dbin->filtered, GST_MESSAGE_SRC (msg)) != NULL);
4088 GST_OBJECT_UNLOCK (dbin);
4092 gst_message_unref (msg);
4094 GST_BIN_CLASS (parent_class)->handle_message (bin, msg);
4098 gst_decode_bin_plugin_init (GstPlugin * plugin)
4100 GST_DEBUG_CATEGORY_INIT (gst_decode_bin_debug, "decodebin", 0, "decoder bin");
4102 /* Register some quarks here for the stream topology message */
4103 topology_structure_name = g_quark_from_static_string ("stream-topology");
4104 topology_caps = g_quark_from_static_string ("caps");
4105 topology_next = g_quark_from_static_string ("next");
4106 topology_pad = g_quark_from_static_string ("pad");
4107 topology_element_srcpad = g_quark_from_static_string ("element-srcpad");
4109 return gst_element_register (plugin, "decodebin", GST_RANK_NONE,
4110 GST_TYPE_DECODE_BIN);