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;
155 guint64 connection_speed;
157 GstElement *typefind; /* this holds the typefind object */
159 GMutex *expose_lock; /* Protects exposal and removal of groups */
160 GstDecodeChain *decode_chain; /* Top level decode chain */
161 guint nbpads; /* unique identifier for source pads */
163 GMutex *factories_lock;
164 guint32 factories_cookie; /* Cookie from last time when factories was updated */
165 GList *factories; /* factories we can use for selecting elements */
167 GMutex *subtitle_lock; /* Protects changes to subtitles and encoding */
168 GList *subtitles; /* List of elements with subtitle-encoding,
169 * protected by above mutex! */
171 gboolean have_type; /* if we received the have_type signal */
172 guint have_type_id; /* signal id for have-type from typefind */
174 gboolean async_pending; /* async-start has been emitted */
176 GMutex *dyn_lock; /* lock protecting pad blocking */
177 gboolean shutdown; /* if we are shutting down */
178 GList *blocked_pads; /* pads that have set to block */
180 gboolean expose_allstreams; /* Whether to expose unknow type streams or not */
182 GList *filtered; /* elements for which error messages are filtered */
185 struct _GstDecodeBinClass
187 GstBinClass parent_class;
189 /* signal fired when we found a pad that we cannot decode */
190 void (*unknown_type) (GstElement * element, GstPad * pad, GstCaps * caps);
192 /* signal fired to know if we continue trying to decode the given caps */
193 gboolean (*autoplug_continue) (GstElement * element, GstPad * pad,
195 /* signal fired to get a list of factories to try to autoplug */
196 GValueArray *(*autoplug_factories) (GstElement * element, GstPad * pad,
198 /* signal fired to sort the factories */
199 GValueArray *(*autoplug_sort) (GstElement * element, GstPad * pad,
200 GstCaps * caps, GValueArray * factories);
201 /* signal fired to select from the proposed list of factories */
202 GstAutoplugSelectResult (*autoplug_select) (GstElement * element,
203 GstPad * pad, GstCaps * caps, GstElementFactory * factory);
205 /* fired when the last group is drained */
206 void (*drained) (GstElement * element);
213 SIGNAL_AUTOPLUG_CONTINUE,
214 SIGNAL_AUTOPLUG_FACTORIES,
215 SIGNAL_AUTOPLUG_SELECT,
216 SIGNAL_AUTOPLUG_SORT,
221 /* automatic sizes, while prerolling we buffer up to 2MB, we ignore time
222 * and buffers in this case. */
223 #define AUTO_PREROLL_SIZE_BYTES 2 * 1024 * 1024
224 #define AUTO_PREROLL_SIZE_BUFFERS 0
225 #define AUTO_PREROLL_NOT_SEEKABLE_SIZE_TIME 10 * GST_SECOND
226 #define AUTO_PREROLL_SEEKABLE_SIZE_TIME 0
228 /* whan playing, keep a max of 2MB of data but try to keep the number of buffers
229 * as low as possible (try to aim for 5 buffers) */
230 #define AUTO_PLAY_SIZE_BYTES 2 * 1024 * 1024
231 #define AUTO_PLAY_SIZE_BUFFERS 5
232 #define AUTO_PLAY_SIZE_TIME 0
234 #define DEFAULT_SUBTITLE_ENCODING NULL
235 #define DEFAULT_USE_BUFFERING FALSE
236 #define DEFAULT_LOW_PERCENT 10
237 #define DEFAULT_HIGH_PERCENT 99
238 /* by default we use the automatic values above */
239 #define DEFAULT_MAX_SIZE_BYTES 0
240 #define DEFAULT_MAX_SIZE_BUFFERS 0
241 #define DEFAULT_MAX_SIZE_TIME 0
242 #define DEFAULT_POST_STREAM_TOPOLOGY FALSE
243 #define DEFAULT_EXPOSE_ALL_STREAMS TRUE
244 #define DEFAULT_CONNECTION_SPEED 0
251 PROP_SUBTITLE_ENCODING,
257 PROP_MAX_SIZE_BUFFERS,
259 PROP_POST_STREAM_TOPOLOGY,
260 PROP_EXPOSE_ALL_STREAMS,
261 PROP_CONNECTION_SPEED,
265 static GstBinClass *parent_class;
266 static guint gst_decode_bin_signals[LAST_SIGNAL] = { 0 };
268 static GstStaticCaps default_raw_caps = GST_STATIC_CAPS (DEFAULT_RAW_CAPS);
270 static void do_async_start (GstDecodeBin * dbin);
271 static void do_async_done (GstDecodeBin * dbin);
273 static void type_found (GstElement * typefind, guint probability,
274 GstCaps * caps, GstDecodeBin * decode_bin);
276 static void decodebin_set_queue_size (GstDecodeBin * dbin,
277 GstElement * multiqueue, gboolean preroll, gboolean seekable);
279 static gboolean gst_decode_bin_autoplug_continue (GstElement * element,
280 GstPad * pad, GstCaps * caps);
281 static GValueArray *gst_decode_bin_autoplug_factories (GstElement *
282 element, GstPad * pad, GstCaps * caps);
283 static GValueArray *gst_decode_bin_autoplug_sort (GstElement * element,
284 GstPad * pad, GstCaps * caps, GValueArray * factories);
285 static GstAutoplugSelectResult gst_decode_bin_autoplug_select (GstElement *
286 element, GstPad * pad, GstCaps * caps, GstElementFactory * factory);
288 static void gst_decode_bin_set_property (GObject * object, guint prop_id,
289 const GValue * value, GParamSpec * pspec);
290 static void gst_decode_bin_get_property (GObject * object, guint prop_id,
291 GValue * value, GParamSpec * pspec);
292 static void gst_decode_bin_set_caps (GstDecodeBin * dbin, GstCaps * caps);
293 static GstCaps *gst_decode_bin_get_caps (GstDecodeBin * dbin);
294 static void caps_notify_cb (GstPad * pad, GParamSpec * unused,
295 GstDecodeChain * chain);
297 static GstPad *find_sink_pad (GstElement * element);
298 static GstStateChangeReturn gst_decode_bin_change_state (GstElement * element,
299 GstStateChange transition);
300 static void gst_decode_bin_handle_message (GstBin * bin, GstMessage * message);
302 static gboolean check_upstream_seekable (GstDecodeBin * dbin, GstPad * pad);
304 #define EXPOSE_LOCK(dbin) G_STMT_START { \
305 GST_LOG_OBJECT (dbin, \
306 "expose locking from thread %p", \
308 g_mutex_lock (GST_DECODE_BIN_CAST(dbin)->expose_lock); \
309 GST_LOG_OBJECT (dbin, \
310 "expose locked from thread %p", \
314 #define EXPOSE_UNLOCK(dbin) G_STMT_START { \
315 GST_LOG_OBJECT (dbin, \
316 "expose unlocking from thread %p", \
318 g_mutex_unlock (GST_DECODE_BIN_CAST(dbin)->expose_lock); \
321 #define DYN_LOCK(dbin) G_STMT_START { \
322 GST_LOG_OBJECT (dbin, \
323 "dynlocking from thread %p", \
325 g_mutex_lock (GST_DECODE_BIN_CAST(dbin)->dyn_lock); \
326 GST_LOG_OBJECT (dbin, \
327 "dynlocked from thread %p", \
331 #define DYN_UNLOCK(dbin) G_STMT_START { \
332 GST_LOG_OBJECT (dbin, \
333 "dynunlocking from thread %p", \
335 g_mutex_unlock (GST_DECODE_BIN_CAST(dbin)->dyn_lock); \
338 #define SUBTITLE_LOCK(dbin) G_STMT_START { \
339 GST_LOG_OBJECT (dbin, \
340 "subtitle locking from thread %p", \
342 g_mutex_lock (GST_DECODE_BIN_CAST(dbin)->subtitle_lock); \
343 GST_LOG_OBJECT (dbin, \
344 "subtitle lock from thread %p", \
348 #define SUBTITLE_UNLOCK(dbin) G_STMT_START { \
349 GST_LOG_OBJECT (dbin, \
350 "subtitle unlocking from thread %p", \
352 g_mutex_unlock (GST_DECODE_BIN_CAST(dbin)->subtitle_lock); \
355 struct _GstPendingPad
358 GstDecodeChain *chain;
359 gulong event_probe_id;
362 struct _GstDecodeElement
365 GstElement *capsfilter; /* Optional capsfilter for Parser/Convert */
370 * Streams belonging to the same group/chain of a media file
372 * When changing something here lock the parent chain!
374 struct _GstDecodeGroup
377 GstDecodeChain *parent;
379 GstElement *multiqueue; /* Used for linking all child chains */
380 gulong overrunsig; /* the overrun signal for multiqueue */
382 gboolean overrun; /* TRUE if the multiqueue signaled overrun. This
383 * means that we should really expose the group */
385 gboolean no_more_pads; /* TRUE if the demuxer signaled no-more-pads */
386 gboolean drained; /* TRUE if the all children are drained */
388 GList *children; /* List of GstDecodeChains in this group */
390 GList *reqpads; /* List of RequestPads for multiqueue, there is
391 * exactly one RequestPad per child chain */
394 struct _GstDecodeChain
396 GstDecodeGroup *parent;
399 GMutex *lock; /* Protects this chain and its groups */
401 GstPad *pad; /* srcpad that caused creation of this chain */
403 gboolean demuxer; /* TRUE if elements->data is a demuxer */
404 gboolean seekable; /* TRUE if this chain ends on a demuxer and is seekable */
405 GList *elements; /* All elements in this group, first
406 is the latest and most downstream element */
408 /* Note: there are only groups if the last element of this chain
409 * is a demuxer, otherwise the chain will end with an endpad.
410 * The other way around this means, that endpad only exists if this
411 * chain doesn't end with a demuxer! */
413 GstDecodeGroup *active_group; /* Currently active group */
414 GList *next_groups; /* head is newest group, tail is next group.
415 a new group will be created only if the head
416 group had no-more-pads. If it's only exposed
417 all new pads will be ignored! */
418 GList *pending_pads; /* Pads that have no fixed caps yet */
420 GstDecodePad *endpad; /* Pad of this chain that could be exposed */
421 gboolean deadend; /* This chain is incomplete and can't be completed,
422 e.g. no suitable decoder could be found
423 e.g. stream got EOS without buffers
425 GstCaps *endcaps; /* Caps that were used when linking to the endpad
426 or that resulted in the deadend
429 /* FIXME: This should be done directly via a thread! */
430 GList *old_groups; /* Groups that should be freed later */
433 static void gst_decode_chain_free (GstDecodeChain * chain);
434 static GstDecodeChain *gst_decode_chain_new (GstDecodeBin * dbin,
435 GstDecodeGroup * group, GstPad * pad);
436 static void gst_decode_group_hide (GstDecodeGroup * group);
437 static void gst_decode_group_free (GstDecodeGroup * group);
438 static GstDecodeGroup *gst_decode_group_new (GstDecodeBin * dbin,
439 GstDecodeChain * chain);
440 static gboolean gst_decode_chain_is_complete (GstDecodeChain * chain);
441 static gboolean gst_decode_chain_expose (GstDecodeChain * chain,
442 GList ** endpads, gboolean * missing_plugin);
443 static gboolean gst_decode_chain_is_drained (GstDecodeChain * chain);
444 static gboolean gst_decode_group_is_complete (GstDecodeGroup * group);
445 static GstPad *gst_decode_group_control_demuxer_pad (GstDecodeGroup * group,
447 static gboolean gst_decode_group_is_drained (GstDecodeGroup * group);
449 static gboolean gst_decode_bin_expose (GstDecodeBin * dbin);
451 #define CHAIN_MUTEX_LOCK(chain) G_STMT_START { \
452 GST_LOG_OBJECT (chain->dbin, \
453 "locking chain %p from thread %p", \
454 chain, g_thread_self ()); \
455 g_mutex_lock (chain->lock); \
456 GST_LOG_OBJECT (chain->dbin, \
457 "locked chain %p from thread %p", \
458 chain, g_thread_self ()); \
461 #define CHAIN_MUTEX_UNLOCK(chain) G_STMT_START { \
462 GST_LOG_OBJECT (chain->dbin, \
463 "unlocking chain %p from thread %p", \
464 chain, g_thread_self ()); \
465 g_mutex_unlock (chain->lock); \
470 * GstPad private used for source pads of chains
476 GstDecodeChain *chain;
478 gboolean blocked; /* the *target* pad is blocked */
479 gboolean exposed; /* the pad is exposed */
480 gboolean drained; /* an EOS has been seen on the pad */
485 GType gst_decode_pad_get_type (void);
486 G_DEFINE_TYPE (GstDecodePad, gst_decode_pad, GST_TYPE_GHOST_PAD);
487 #define GST_TYPE_DECODE_PAD (gst_decode_pad_get_type ())
488 #define GST_DECODE_PAD(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_DECODE_PAD,GstDecodePad))
490 static GstDecodePad *gst_decode_pad_new (GstDecodeBin * dbin, GstPad * pad,
491 GstDecodeChain * chain);
492 static void gst_decode_pad_activate (GstDecodePad * dpad,
493 GstDecodeChain * chain);
494 static void gst_decode_pad_unblock (GstDecodePad * dpad);
495 static void gst_decode_pad_set_blocked (GstDecodePad * dpad, gboolean blocked);
497 static void gst_pending_pad_free (GstPendingPad * ppad);
498 static GstPadProbeReturn pad_event_cb (GstPad * pad, GstPadProbeInfo * info,
501 /********************************
502 * Standard GObject boilerplate *
503 ********************************/
505 static void gst_decode_bin_class_init (GstDecodeBinClass * klass);
506 static void gst_decode_bin_init (GstDecodeBin * decode_bin);
507 static void gst_decode_bin_dispose (GObject * object);
508 static void gst_decode_bin_finalize (GObject * object);
511 gst_decode_bin_get_type (void)
513 static GType gst_decode_bin_type = 0;
515 if (!gst_decode_bin_type) {
516 static const GTypeInfo gst_decode_bin_info = {
517 sizeof (GstDecodeBinClass),
520 (GClassInitFunc) gst_decode_bin_class_init,
523 sizeof (GstDecodeBin),
525 (GInstanceInitFunc) gst_decode_bin_init,
529 gst_decode_bin_type =
530 g_type_register_static (GST_TYPE_BIN, "GstDecodeBin",
531 &gst_decode_bin_info, 0);
534 return gst_decode_bin_type;
538 _gst_boolean_accumulator (GSignalInvocationHint * ihint,
539 GValue * return_accu, const GValue * handler_return, gpointer dummy)
543 myboolean = g_value_get_boolean (handler_return);
544 if (!(ihint->run_type & G_SIGNAL_RUN_CLEANUP))
545 g_value_set_boolean (return_accu, myboolean);
547 /* stop emission if FALSE */
551 /* we collect the first result */
553 _gst_array_accumulator (GSignalInvocationHint * ihint,
554 GValue * return_accu, const GValue * handler_return, gpointer dummy)
558 array = g_value_get_boxed (handler_return);
559 if (!(ihint->run_type & G_SIGNAL_RUN_CLEANUP))
560 g_value_set_boxed (return_accu, array);
566 _gst_select_accumulator (GSignalInvocationHint * ihint,
567 GValue * return_accu, const GValue * handler_return, gpointer dummy)
569 GstAutoplugSelectResult res;
571 res = g_value_get_enum (handler_return);
572 if (!(ihint->run_type & G_SIGNAL_RUN_CLEANUP))
573 g_value_set_enum (return_accu, res);
579 _gst_array_hasvalue_accumulator (GSignalInvocationHint * ihint,
580 GValue * return_accu, const GValue * handler_return, gpointer dummy)
584 array = g_value_get_boxed (handler_return);
585 if (!(ihint->run_type & G_SIGNAL_RUN_CLEANUP))
586 g_value_set_boxed (return_accu, array);
595 gst_decode_bin_class_init (GstDecodeBinClass * klass)
597 GObjectClass *gobject_klass;
598 GstElementClass *gstelement_klass;
599 GstBinClass *gstbin_klass;
601 gobject_klass = (GObjectClass *) klass;
602 gstelement_klass = (GstElementClass *) klass;
603 gstbin_klass = (GstBinClass *) klass;
605 parent_class = g_type_class_peek_parent (klass);
607 gobject_klass->dispose = gst_decode_bin_dispose;
608 gobject_klass->finalize = gst_decode_bin_finalize;
609 gobject_klass->set_property = gst_decode_bin_set_property;
610 gobject_klass->get_property = gst_decode_bin_get_property;
613 * GstDecodeBin::unknown-type:
614 * @bin: The decodebin.
615 * @pad: The new pad containing caps that cannot be resolved to a 'final'
617 * @caps: The #GstCaps of the pad that cannot be resolved.
619 * This signal is emitted when a pad for which there is no further possible
620 * decoding is added to the decodebin.
622 gst_decode_bin_signals[SIGNAL_UNKNOWN_TYPE] =
623 g_signal_new ("unknown-type", G_TYPE_FROM_CLASS (klass),
624 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstDecodeBinClass, unknown_type),
625 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 2,
626 GST_TYPE_PAD, GST_TYPE_CAPS);
629 * GstDecodeBin::autoplug-continue:
630 * @bin: The decodebin.
632 * @caps: The #GstCaps found.
634 * This signal is emitted whenever decodebin finds a new stream. It is
635 * emitted before looking for any elements that can handle that stream.
638 * Invocation of signal handlers stops after the first signal handler
639 * returns #FALSE. Signal handlers are invoked in the order they were
643 * Returns: #TRUE if you wish decodebin to look for elements that can
644 * handle the given @caps. If #FALSE, those caps will be considered as
645 * final and the pad will be exposed as such (see 'pad-added' signal of
648 gst_decode_bin_signals[SIGNAL_AUTOPLUG_CONTINUE] =
649 g_signal_new ("autoplug-continue", G_TYPE_FROM_CLASS (klass),
650 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstDecodeBinClass, autoplug_continue),
651 _gst_boolean_accumulator, NULL, g_cclosure_marshal_generic,
652 G_TYPE_BOOLEAN, 2, GST_TYPE_PAD, GST_TYPE_CAPS);
655 * GstDecodeBin::autoplug-factories:
656 * @bin: The decodebin.
658 * @caps: The #GstCaps found.
660 * This function is emited when an array of possible factories for @caps on
661 * @pad is needed. Decodebin will by default return an array with all
662 * compatible factories, sorted by rank.
664 * If this function returns NULL, @pad will be exposed as a final caps.
666 * If this function returns an empty array, the pad will be considered as
667 * having an unhandled type media type.
670 * Only the signal handler that is connected first will ever by invoked.
671 * Don't connect signal handlers with the #G_CONNECT_AFTER flag to this
672 * signal, they will never be invoked!
675 * Returns: a #GValueArray* with a list of factories to try. The factories are
676 * by default tried in the returned order or based on the index returned by
679 gst_decode_bin_signals[SIGNAL_AUTOPLUG_FACTORIES] =
680 g_signal_new ("autoplug-factories", G_TYPE_FROM_CLASS (klass),
681 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstDecodeBinClass,
682 autoplug_factories), _gst_array_accumulator, NULL,
683 g_cclosure_marshal_generic, G_TYPE_VALUE_ARRAY, 2,
684 GST_TYPE_PAD, GST_TYPE_CAPS);
687 * GstDecodeBin::autoplug-sort:
688 * @bin: The decodebin.
690 * @caps: The #GstCaps.
691 * @factories: A #GValueArray of possible #GstElementFactory to use.
693 * Once decodebin has found the possible #GstElementFactory objects to try
694 * for @caps on @pad, this signal is emited. The purpose of the signal is for
695 * the application to perform additional sorting or filtering on the element
698 * The callee should copy and modify @factories or return #NULL if the
699 * order should not change.
702 * Invocation of signal handlers stops after one signal handler has
703 * returned something else than #NULL. Signal handlers are invoked in
704 * the order they were connected in.
705 * Don't connect signal handlers with the #G_CONNECT_AFTER flag to this
706 * signal, they will never be invoked!
709 * Returns: A new sorted array of #GstElementFactory objects.
711 gst_decode_bin_signals[SIGNAL_AUTOPLUG_SORT] =
712 g_signal_new ("autoplug-sort", G_TYPE_FROM_CLASS (klass),
713 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstDecodeBinClass, autoplug_sort),
714 _gst_array_hasvalue_accumulator, NULL,
715 g_cclosure_marshal_generic, G_TYPE_VALUE_ARRAY, 3,
716 GST_TYPE_PAD, GST_TYPE_CAPS, G_TYPE_VALUE_ARRAY);
719 * GstDecodeBin::autoplug-select:
720 * @bin: The decodebin.
722 * @caps: The #GstCaps.
723 * @factory: A #GstElementFactory to use.
725 * This signal is emitted once decodebin has found all the possible
726 * #GstElementFactory that can be used to handle the given @caps. For each of
727 * those factories, this signal is emitted.
729 * The signal handler should return a #GST_TYPE_AUTOPLUG_SELECT_RESULT enum
730 * value indicating what decodebin should do next.
732 * A value of #GST_AUTOPLUG_SELECT_TRY will try to autoplug an element from
735 * A value of #GST_AUTOPLUG_SELECT_EXPOSE will expose @pad without plugging
738 * A value of #GST_AUTOPLUG_SELECT_SKIP will skip @factory and move to the
742 * Only the signal handler that is connected first will ever by invoked.
743 * Don't connect signal handlers with the #G_CONNECT_AFTER flag to this
744 * signal, they will never be invoked!
747 * Returns: a #GST_TYPE_AUTOPLUG_SELECT_RESULT that indicates the required
748 * operation. the default handler will always return
749 * #GST_AUTOPLUG_SELECT_TRY.
751 gst_decode_bin_signals[SIGNAL_AUTOPLUG_SELECT] =
752 g_signal_new ("autoplug-select", G_TYPE_FROM_CLASS (klass),
753 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstDecodeBinClass, autoplug_select),
754 _gst_select_accumulator, NULL,
755 g_cclosure_marshal_generic,
756 GST_TYPE_AUTOPLUG_SELECT_RESULT, 3, GST_TYPE_PAD, GST_TYPE_CAPS,
757 GST_TYPE_ELEMENT_FACTORY);
760 * GstDecodeBin::drained
761 * @bin: The decodebin
763 * This signal is emitted once decodebin has finished decoding all the data.
767 gst_decode_bin_signals[SIGNAL_DRAINED] =
768 g_signal_new ("drained", G_TYPE_FROM_CLASS (klass),
769 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstDecodeBinClass, drained),
770 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 0, G_TYPE_NONE);
772 g_object_class_install_property (gobject_klass, PROP_CAPS,
773 g_param_spec_boxed ("caps", "Caps", "The caps on which to stop decoding.",
774 GST_TYPE_CAPS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
776 g_object_class_install_property (gobject_klass, PROP_SUBTITLE_ENCODING,
777 g_param_spec_string ("subtitle-encoding", "subtitle encoding",
778 "Encoding to assume if input subtitles are not in UTF-8 encoding. "
779 "If not set, the GST_SUBTITLE_ENCODING environment variable will "
780 "be checked for an encoding to use. If that is not set either, "
781 "ISO-8859-15 will be assumed.", NULL,
782 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
784 g_object_class_install_property (gobject_klass, PROP_SINK_CAPS,
785 g_param_spec_boxed ("sink-caps", "Sink Caps",
786 "The caps of the input data. (NULL = use typefind element)",
787 GST_TYPE_CAPS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
790 * GstDecodeBin::use-buffering
792 * Activate buffering in decodebin. This will instruct the multiqueues behind
793 * decoders to emit BUFFERING messages.
797 g_object_class_install_property (gobject_klass, PROP_USE_BUFFERING,
798 g_param_spec_boolean ("use-buffering", "Use Buffering",
799 "Emit GST_MESSAGE_BUFFERING based on low-/high-percent thresholds",
800 DEFAULT_USE_BUFFERING, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
803 * GstDecodeBin:low-percent
805 * Low threshold percent for buffering to start.
809 g_object_class_install_property (gobject_klass, PROP_LOW_PERCENT,
810 g_param_spec_int ("low-percent", "Low percent",
811 "Low threshold for buffering to start", 0, 100,
812 DEFAULT_LOW_PERCENT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
814 * GstDecodeBin:high-percent
816 * High threshold percent for buffering to finish.
820 g_object_class_install_property (gobject_klass, PROP_HIGH_PERCENT,
821 g_param_spec_int ("high-percent", "High percent",
822 "High threshold for buffering to finish", 0, 100,
823 DEFAULT_HIGH_PERCENT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
826 * GstDecodeBin:max-size-bytes
828 * Max amount of bytes in the queue (0=automatic).
832 g_object_class_install_property (gobject_klass, PROP_MAX_SIZE_BYTES,
833 g_param_spec_uint ("max-size-bytes", "Max. size (bytes)",
834 "Max. amount of bytes in the queue (0=automatic)",
835 0, G_MAXUINT, DEFAULT_MAX_SIZE_BYTES,
836 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
838 * GstDecodeBin:max-size-buffers
840 * Max amount of buffers in the queue (0=automatic).
844 g_object_class_install_property (gobject_klass, PROP_MAX_SIZE_BUFFERS,
845 g_param_spec_uint ("max-size-buffers", "Max. size (buffers)",
846 "Max. number of buffers in the queue (0=automatic)",
847 0, G_MAXUINT, DEFAULT_MAX_SIZE_BUFFERS,
848 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
850 * GstDecodeBin:max-size-time
852 * Max amount of time in the queue (in ns, 0=automatic).
856 g_object_class_install_property (gobject_klass, PROP_MAX_SIZE_TIME,
857 g_param_spec_uint64 ("max-size-time", "Max. size (ns)",
858 "Max. amount of data in the queue (in ns, 0=automatic)",
860 DEFAULT_MAX_SIZE_TIME, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
863 * GstDecodeBin::post-stream-topology
865 * Post stream-topology messages on the bus every time the topology changes.
869 g_object_class_install_property (gobject_klass, PROP_POST_STREAM_TOPOLOGY,
870 g_param_spec_boolean ("post-stream-topology", "Post Stream Topology",
871 "Post stream-topology messages",
872 DEFAULT_POST_STREAM_TOPOLOGY,
873 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
876 * GstDecodeBin::expose-all-streams
878 * Expose streams of unknown type.
880 * If set to %FALSE, then only the streams that can be decoded to the final
881 * caps (see 'caps' property) will have a pad exposed. Streams that do not
882 * match those caps but could have been decoded will not have decoder plugged
883 * in internally and will not have a pad exposed.
887 g_object_class_install_property (gobject_klass, PROP_EXPOSE_ALL_STREAMS,
888 g_param_spec_boolean ("expose-all-streams", "Expose All Streams",
889 "Expose all streams, including those of unknown type or that don't match the 'caps' property",
890 DEFAULT_EXPOSE_ALL_STREAMS,
891 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
894 * GstDecodeBin2::connection-speed
896 * Network connection speed in kbps (0 = unknownw)
900 g_object_class_install_property (gobject_klass, PROP_CONNECTION_SPEED,
901 g_param_spec_uint64 ("connection-speed", "Connection Speed",
902 "Network connection speed in kbps (0 = unknown)",
903 0, G_MAXUINT64 / 1000, DEFAULT_CONNECTION_SPEED,
904 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
908 klass->autoplug_continue =
909 GST_DEBUG_FUNCPTR (gst_decode_bin_autoplug_continue);
910 klass->autoplug_factories =
911 GST_DEBUG_FUNCPTR (gst_decode_bin_autoplug_factories);
912 klass->autoplug_sort = GST_DEBUG_FUNCPTR (gst_decode_bin_autoplug_sort);
913 klass->autoplug_select = GST_DEBUG_FUNCPTR (gst_decode_bin_autoplug_select);
915 gst_element_class_add_pad_template (gstelement_klass,
916 gst_static_pad_template_get (&decoder_bin_sink_template));
917 gst_element_class_add_pad_template (gstelement_klass,
918 gst_static_pad_template_get (&decoder_bin_src_template));
920 gst_element_class_set_static_metadata (gstelement_klass,
921 "Decoder Bin", "Generic/Bin/Decoder",
922 "Autoplug and decode to raw media",
923 "Edward Hervey <edward.hervey@collabora.co.uk>, "
924 "Sebastian Dröge <sebastian.droege@collabora.co.uk>");
926 gstelement_klass->change_state =
927 GST_DEBUG_FUNCPTR (gst_decode_bin_change_state);
929 gstbin_klass->handle_message =
930 GST_DEBUG_FUNCPTR (gst_decode_bin_handle_message);
933 /* Must be called with factories lock! */
935 gst_decode_bin_update_factories_list (GstDecodeBin * dbin)
939 cookie = gst_registry_get_feature_list_cookie (gst_registry_get ());
940 if (!dbin->factories || dbin->factories_cookie != cookie) {
942 gst_plugin_feature_list_free (dbin->factories);
944 gst_element_factory_list_get_elements
945 (GST_ELEMENT_FACTORY_TYPE_DECODABLE, GST_RANK_MARGINAL);
946 dbin->factories_cookie = cookie;
951 gst_decode_bin_init (GstDecodeBin * decode_bin)
953 /* first filter out the interesting element factories */
954 decode_bin->factories_lock = g_mutex_new ();
956 /* we create the typefind element only once */
957 decode_bin->typefind = gst_element_factory_make ("typefind", "typefind");
958 if (!decode_bin->typefind) {
959 g_warning ("can't find typefind element, decodebin will not work");
963 GstPadTemplate *pad_tmpl;
965 /* add the typefind element */
966 if (!gst_bin_add (GST_BIN (decode_bin), decode_bin->typefind)) {
967 g_warning ("Could not add typefind element, decodebin will not work");
968 gst_object_unref (decode_bin->typefind);
969 decode_bin->typefind = NULL;
972 /* get the sinkpad */
973 pad = gst_element_get_static_pad (decode_bin->typefind, "sink");
975 /* get the pad template */
976 pad_tmpl = gst_static_pad_template_get (&decoder_bin_sink_template);
978 /* ghost the sink pad to ourself */
979 gpad = gst_ghost_pad_new_from_template ("sink", pad, pad_tmpl);
980 gst_pad_set_active (gpad, TRUE);
981 gst_element_add_pad (GST_ELEMENT (decode_bin), gpad);
983 gst_object_unref (pad_tmpl);
984 gst_object_unref (pad);
986 /* connect a signal to find out when the typefind element found
988 decode_bin->have_type_id =
989 g_signal_connect (G_OBJECT (decode_bin->typefind), "have-type",
990 G_CALLBACK (type_found), decode_bin);
993 decode_bin->expose_lock = g_mutex_new ();
994 decode_bin->decode_chain = NULL;
996 decode_bin->dyn_lock = g_mutex_new ();
997 decode_bin->shutdown = FALSE;
998 decode_bin->blocked_pads = NULL;
1000 decode_bin->subtitle_lock = g_mutex_new ();
1002 decode_bin->encoding = g_strdup (DEFAULT_SUBTITLE_ENCODING);
1003 decode_bin->caps = gst_static_caps_get (&default_raw_caps);
1004 decode_bin->use_buffering = DEFAULT_USE_BUFFERING;
1005 decode_bin->low_percent = DEFAULT_LOW_PERCENT;
1006 decode_bin->high_percent = DEFAULT_HIGH_PERCENT;
1008 decode_bin->max_size_bytes = DEFAULT_MAX_SIZE_BYTES;
1009 decode_bin->max_size_buffers = DEFAULT_MAX_SIZE_BUFFERS;
1010 decode_bin->max_size_time = DEFAULT_MAX_SIZE_TIME;
1012 decode_bin->expose_allstreams = DEFAULT_EXPOSE_ALL_STREAMS;
1013 decode_bin->connection_speed = DEFAULT_CONNECTION_SPEED;
1017 gst_decode_bin_dispose (GObject * object)
1019 GstDecodeBin *decode_bin;
1021 decode_bin = GST_DECODE_BIN (object);
1023 if (decode_bin->factories)
1024 gst_plugin_feature_list_free (decode_bin->factories);
1025 decode_bin->factories = NULL;
1027 if (decode_bin->decode_chain)
1028 gst_decode_chain_free (decode_bin->decode_chain);
1029 decode_bin->decode_chain = NULL;
1031 if (decode_bin->caps)
1032 gst_caps_unref (decode_bin->caps);
1033 decode_bin->caps = NULL;
1035 g_free (decode_bin->encoding);
1036 decode_bin->encoding = NULL;
1038 g_list_free (decode_bin->subtitles);
1039 decode_bin->subtitles = NULL;
1041 G_OBJECT_CLASS (parent_class)->dispose (object);
1045 gst_decode_bin_finalize (GObject * object)
1047 GstDecodeBin *decode_bin;
1049 decode_bin = GST_DECODE_BIN (object);
1051 if (decode_bin->expose_lock) {
1052 g_mutex_free (decode_bin->expose_lock);
1053 decode_bin->expose_lock = NULL;
1056 if (decode_bin->dyn_lock) {
1057 g_mutex_free (decode_bin->dyn_lock);
1058 decode_bin->dyn_lock = NULL;
1061 if (decode_bin->subtitle_lock) {
1062 g_mutex_free (decode_bin->subtitle_lock);
1063 decode_bin->subtitle_lock = NULL;
1066 if (decode_bin->factories_lock) {
1067 g_mutex_free (decode_bin->factories_lock);
1068 decode_bin->factories_lock = NULL;
1071 G_OBJECT_CLASS (parent_class)->finalize (object);
1075 * Changes the caps on which decodebin will stop decoding.
1076 * Will unref the previously set one. The refcount of the given caps will be
1078 * @caps can be NULL.
1083 gst_decode_bin_set_caps (GstDecodeBin * dbin, GstCaps * caps)
1085 GST_DEBUG_OBJECT (dbin, "Setting new caps: %" GST_PTR_FORMAT, caps);
1087 GST_OBJECT_LOCK (dbin);
1088 gst_caps_replace (&dbin->caps, caps);
1089 GST_OBJECT_UNLOCK (dbin);
1093 * Returns the currently configured caps on which decodebin will stop decoding.
1094 * The returned caps (if not NULL), will have its refcount incremented.
1099 gst_decode_bin_get_caps (GstDecodeBin * dbin)
1103 GST_DEBUG_OBJECT (dbin, "Getting currently set caps");
1105 GST_OBJECT_LOCK (dbin);
1108 gst_caps_ref (caps);
1109 GST_OBJECT_UNLOCK (dbin);
1115 gst_decode_bin_set_sink_caps (GstDecodeBin * dbin, GstCaps * caps)
1117 GST_DEBUG_OBJECT (dbin, "Setting new caps: %" GST_PTR_FORMAT, caps);
1119 g_object_set (dbin->typefind, "force-caps", caps, NULL);
1123 gst_decode_bin_get_sink_caps (GstDecodeBin * dbin)
1127 GST_DEBUG_OBJECT (dbin, "Getting currently set caps");
1129 g_object_get (dbin->typefind, "force-caps", &caps, NULL);
1135 gst_decode_bin_set_subs_encoding (GstDecodeBin * dbin, const gchar * encoding)
1139 GST_DEBUG_OBJECT (dbin, "Setting new encoding: %s", GST_STR_NULL (encoding));
1141 SUBTITLE_LOCK (dbin);
1142 g_free (dbin->encoding);
1143 dbin->encoding = g_strdup (encoding);
1145 /* set the subtitle encoding on all added elements */
1146 for (walk = dbin->subtitles; walk; walk = g_list_next (walk)) {
1147 g_object_set (G_OBJECT (walk->data), "subtitle-encoding", dbin->encoding,
1150 SUBTITLE_UNLOCK (dbin);
1154 gst_decode_bin_get_subs_encoding (GstDecodeBin * dbin)
1158 GST_DEBUG_OBJECT (dbin, "Getting currently set encoding");
1160 SUBTITLE_LOCK (dbin);
1161 encoding = g_strdup (dbin->encoding);
1162 SUBTITLE_UNLOCK (dbin);
1168 gst_decode_bin_set_property (GObject * object, guint prop_id,
1169 const GValue * value, GParamSpec * pspec)
1173 dbin = GST_DECODE_BIN (object);
1177 gst_decode_bin_set_caps (dbin, g_value_get_boxed (value));
1179 case PROP_SUBTITLE_ENCODING:
1180 gst_decode_bin_set_subs_encoding (dbin, g_value_get_string (value));
1182 case PROP_SINK_CAPS:
1183 gst_decode_bin_set_sink_caps (dbin, g_value_get_boxed (value));
1185 case PROP_USE_BUFFERING:
1186 dbin->use_buffering = g_value_get_boolean (value);
1188 case PROP_LOW_PERCENT:
1189 dbin->low_percent = g_value_get_int (value);
1191 case PROP_HIGH_PERCENT:
1192 dbin->high_percent = g_value_get_int (value);
1194 case PROP_MAX_SIZE_BYTES:
1195 dbin->max_size_bytes = g_value_get_uint (value);
1197 case PROP_MAX_SIZE_BUFFERS:
1198 dbin->max_size_buffers = g_value_get_uint (value);
1200 case PROP_MAX_SIZE_TIME:
1201 dbin->max_size_time = g_value_get_uint64 (value);
1203 case PROP_POST_STREAM_TOPOLOGY:
1204 dbin->post_stream_topology = g_value_get_boolean (value);
1206 case PROP_EXPOSE_ALL_STREAMS:
1207 dbin->expose_allstreams = g_value_get_boolean (value);
1209 case PROP_CONNECTION_SPEED:
1210 GST_OBJECT_LOCK (dbin);
1211 dbin->connection_speed = g_value_get_uint64 (value) * 1000;
1212 GST_OBJECT_UNLOCK (dbin);
1215 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1221 gst_decode_bin_get_property (GObject * object, guint prop_id,
1222 GValue * value, GParamSpec * pspec)
1226 dbin = GST_DECODE_BIN (object);
1229 g_value_take_boxed (value, gst_decode_bin_get_caps (dbin));
1231 case PROP_SUBTITLE_ENCODING:
1232 g_value_take_string (value, gst_decode_bin_get_subs_encoding (dbin));
1234 case PROP_SINK_CAPS:
1235 g_value_take_boxed (value, gst_decode_bin_get_sink_caps (dbin));
1237 case PROP_USE_BUFFERING:
1238 g_value_set_boolean (value, dbin->use_buffering);
1240 case PROP_LOW_PERCENT:
1241 g_value_set_int (value, dbin->low_percent);
1243 case PROP_HIGH_PERCENT:
1244 g_value_set_int (value, dbin->high_percent);
1246 case PROP_MAX_SIZE_BYTES:
1247 g_value_set_uint (value, dbin->max_size_bytes);
1249 case PROP_MAX_SIZE_BUFFERS:
1250 g_value_set_uint (value, dbin->max_size_buffers);
1252 case PROP_MAX_SIZE_TIME:
1253 g_value_set_uint64 (value, dbin->max_size_time);
1255 case PROP_POST_STREAM_TOPOLOGY:
1256 g_value_set_boolean (value, dbin->post_stream_topology);
1258 case PROP_EXPOSE_ALL_STREAMS:
1259 g_value_set_boolean (value, dbin->expose_allstreams);
1261 case PROP_CONNECTION_SPEED:
1262 GST_OBJECT_LOCK (dbin);
1263 g_value_set_uint64 (value, dbin->connection_speed / 1000);
1264 GST_OBJECT_UNLOCK (dbin);
1267 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1274 * Default autoplug signal handlers
1277 gst_decode_bin_autoplug_continue (GstElement * element, GstPad * pad,
1280 GST_DEBUG_OBJECT (element, "autoplug-continue returns TRUE");
1282 /* by default we always continue */
1286 static GValueArray *
1287 gst_decode_bin_autoplug_factories (GstElement * element, GstPad * pad,
1291 GValueArray *result;
1292 GstDecodeBin *dbin = GST_DECODE_BIN_CAST (element);
1294 GST_DEBUG_OBJECT (element, "finding factories");
1296 /* return all compatible factories for caps */
1297 g_mutex_lock (dbin->factories_lock);
1298 gst_decode_bin_update_factories_list (dbin);
1300 gst_element_factory_list_filter (dbin->factories, caps, GST_PAD_SINK,
1302 g_mutex_unlock (dbin->factories_lock);
1304 result = g_value_array_new (g_list_length (list));
1305 for (tmp = list; tmp; tmp = tmp->next) {
1306 GstElementFactory *factory = GST_ELEMENT_FACTORY_CAST (tmp->data);
1307 GValue val = { 0, };
1309 g_value_init (&val, G_TYPE_OBJECT);
1310 g_value_set_object (&val, factory);
1311 g_value_array_append (result, &val);
1312 g_value_unset (&val);
1314 gst_plugin_feature_list_free (list);
1316 GST_DEBUG_OBJECT (element, "autoplug-factories returns %p", result);
1321 static GValueArray *
1322 gst_decode_bin_autoplug_sort (GstElement * element, GstPad * pad,
1323 GstCaps * caps, GValueArray * factories)
1328 static GstAutoplugSelectResult
1329 gst_decode_bin_autoplug_select (GstElement * element, GstPad * pad,
1330 GstCaps * caps, GstElementFactory * factory)
1332 GST_DEBUG_OBJECT (element, "default autoplug-select returns TRY");
1335 return GST_AUTOPLUG_SELECT_TRY;
1342 static gboolean are_final_caps (GstDecodeBin * dbin, GstCaps * caps);
1343 static gboolean is_demuxer_element (GstElement * srcelement);
1345 static gboolean connect_pad (GstDecodeBin * dbin, GstElement * src,
1346 GstDecodePad * dpad, GstPad * pad, GstCaps * caps, GValueArray * factories,
1347 GstDecodeChain * chain);
1348 static gboolean connect_element (GstDecodeBin * dbin, GstElement * element,
1349 GstDecodeChain * chain);
1350 static void expose_pad (GstDecodeBin * dbin, GstElement * src,
1351 GstDecodePad * dpad, GstPad * pad, GstCaps * caps, GstDecodeChain * chain);
1353 static void pad_added_cb (GstElement * element, GstPad * pad,
1354 GstDecodeChain * chain);
1355 static void pad_removed_cb (GstElement * element, GstPad * pad,
1356 GstDecodeChain * chain);
1357 static void no_more_pads_cb (GstElement * element, GstDecodeChain * chain);
1359 static GstDecodeGroup *gst_decode_chain_get_current_group (GstDecodeChain *
1362 /* called when a new pad is discovered. It will perform some basic actions
1363 * before trying to link something to it.
1365 * - Check the caps, don't do anything when there are no caps or when they have
1367 * - signal AUTOPLUG_CONTINUE to check if we need to continue autoplugging this
1369 * - if the caps are non-fixed, setup a handler to continue autoplugging when
1370 * the caps become fixed (connect to notify::caps).
1371 * - get list of factories to autoplug.
1372 * - continue autoplugging to one of the factories.
1375 analyze_new_pad (GstDecodeBin * dbin, GstElement * src, GstPad * pad,
1376 GstCaps * caps, GstDecodeChain * chain)
1378 gboolean apcontinue = TRUE;
1379 GValueArray *factories = NULL, *result = NULL;
1381 GstElementFactory *factory;
1382 const gchar *classification;
1383 gboolean is_parser_converter = FALSE;
1386 GST_DEBUG_OBJECT (dbin, "Pad %s:%s caps:%" GST_PTR_FORMAT,
1387 GST_DEBUG_PAD_NAME (pad), caps);
1390 && src != ((GstDecodeElement *) chain->elements->data)->element
1391 && src != ((GstDecodeElement *) chain->elements->data)->capsfilter) {
1392 GST_ERROR_OBJECT (dbin, "New pad from not the last element in this chain");
1396 if (chain->endpad) {
1397 GST_ERROR_OBJECT (dbin, "New pad in a chain that is already complete");
1401 if (chain->demuxer) {
1402 GstDecodeGroup *group;
1403 GstDecodeChain *oldchain = chain;
1405 /* we are adding a new pad for a demuxer (see is_demuxer_element(),
1406 * start a new chain for it */
1407 CHAIN_MUTEX_LOCK (oldchain);
1408 group = gst_decode_chain_get_current_group (chain);
1410 chain = gst_decode_chain_new (dbin, group, pad);
1411 group->children = g_list_prepend (group->children, chain);
1413 CHAIN_MUTEX_UNLOCK (oldchain);
1415 GST_WARNING_OBJECT (dbin, "No current group");
1420 if ((caps == NULL) || gst_caps_is_empty (caps))
1423 if (gst_caps_is_any (caps))
1426 dpad = gst_decode_pad_new (dbin, pad, chain);
1428 /* 1. Emit 'autoplug-continue' the result will tell us if this pads needs
1429 * further autoplugging. Only do this for fixed caps, for unfixed caps
1430 * we will later come here again from the notify::caps handler. The
1431 * problem with unfixed caps is that we can reliably tell if the output
1432 * is e.g. accepted by a sink because only parts of the possible final
1433 * caps might be accepted by the sink. */
1434 if (gst_caps_is_fixed (caps))
1435 g_signal_emit (G_OBJECT (dbin),
1436 gst_decode_bin_signals[SIGNAL_AUTOPLUG_CONTINUE], 0, dpad, caps,
1441 /* 1.a if autoplug-continue is FALSE or caps is a raw format, goto pad_is_final */
1442 if ((!apcontinue) || are_final_caps (dbin, caps))
1445 /* 1.b For Parser/Converter that can output different stream formats
1446 * we insert a capsfilter with the sorted caps of all possible next
1447 * elements and continue with the capsfilter srcpad */
1448 factory = gst_element_get_factory (src);
1449 classification = gst_element_factory_get_klass (factory);
1450 is_parser_converter = (strstr (classification, "Parser")
1451 && strstr (classification, "Converter"));
1453 /* 1.c when the caps are not fixed yet, we can't be sure what element to
1454 * connect. We delay autoplugging until the caps are fixed */
1455 if (!is_parser_converter && !gst_caps_is_fixed (caps))
1458 /* 1.d else get the factories and if there's no compatible factory goto
1460 g_signal_emit (G_OBJECT (dbin),
1461 gst_decode_bin_signals[SIGNAL_AUTOPLUG_FACTORIES], 0, dpad, caps,
1464 /* NULL means that we can expose the pad */
1465 if (factories == NULL)
1468 /* if the array is empty, we have a type for which we have no decoder */
1469 if (factories->n_values == 0) {
1470 if (!dbin->expose_allstreams) {
1471 GstCaps *raw = gst_static_caps_get (&default_raw_caps);
1473 /* If the caps are raw, this just means we don't want to expose them */
1474 if (gst_caps_can_intersect (raw, caps)) {
1475 g_value_array_free (factories);
1476 gst_caps_unref (raw);
1477 gst_object_unref (dpad);
1478 goto discarded_type;
1480 gst_caps_unref (raw);
1483 /* if not we have a unhandled type with no compatible factories */
1484 g_value_array_free (factories);
1485 gst_object_unref (dpad);
1489 /* 1.e sort some more. */
1490 g_signal_emit (G_OBJECT (dbin),
1491 gst_decode_bin_signals[SIGNAL_AUTOPLUG_SORT], 0, dpad, caps, factories,
1494 g_value_array_free (factories);
1498 /* At this point we have a potential decoder, but we might not need it
1499 * if it doesn't match the output caps */
1500 if (!dbin->expose_allstreams && gst_caps_is_fixed (caps)) {
1503 gboolean dontuse = FALSE;
1505 GST_DEBUG ("Checking if we can abort early");
1507 /* 1.f Do an early check to see if the candidates are potential decoders, but
1508 * due to the fact that they decode to a mediatype that is not final we don't
1511 for (i = 0; i < factories->n_values && !dontuse; i++) {
1512 GstElementFactory *factory =
1513 g_value_get_object (g_value_array_get_nth (factories, i));
1516 /* We are only interested in skipping decoders */
1517 if (strstr (gst_element_factory_get_klass (factory), "Decoder")) {
1519 GST_DEBUG ("Trying factory %s",
1520 gst_plugin_feature_get_name (GST_PLUGIN_FEATURE (factory)));
1522 /* Check the source pad template caps to see if they match raw caps but don't match
1524 for (tmps = gst_element_factory_get_static_pad_templates (factory);
1525 tmps && !dontuse; tmps = tmps->next) {
1526 GstStaticPadTemplate *st = (GstStaticPadTemplate *) tmps->data;
1527 if (st->direction != GST_PAD_SRC)
1529 tcaps = gst_static_pad_template_get_caps (st);
1533 /* Emit autoplug-continue to see if the caps are considered to be raw caps */
1534 g_signal_emit (G_OBJECT (dbin),
1535 gst_decode_bin_signals[SIGNAL_AUTOPLUG_CONTINUE], 0, dpad, tcaps,
1538 /* If autoplug-continue returns TRUE and the caps are not final, don't use them */
1539 if (apcontinue && !are_final_caps (dbin, tcaps))
1541 gst_caps_unref (tcaps);
1547 gst_object_unref (dpad);
1548 g_value_array_free (factories);
1549 goto discarded_type;
1553 /* 1.g now get the factory template caps and insert the capsfilter if this
1554 * is a parser/converter
1556 if (is_parser_converter) {
1557 GstCaps *filter_caps;
1560 GstDecodeElement *delem;
1562 g_assert (chain->elements != NULL);
1563 delem = (GstDecodeElement *) chain->elements->data;
1565 filter_caps = gst_caps_new_empty ();
1566 for (i = 0; i < factories->n_values; i++) {
1567 GstElementFactory *factory =
1568 g_value_get_object (g_value_array_get_nth (factories, i));
1569 GstCaps *tcaps, *intersection;
1572 GST_DEBUG ("Trying factory %s",
1573 gst_plugin_feature_get_name (GST_PLUGIN_FEATURE (factory)));
1575 if (gst_element_get_factory (src) == factory) {
1576 GST_DEBUG ("Skipping factory");
1580 for (tmps = gst_element_factory_get_static_pad_templates (factory); tmps;
1581 tmps = tmps->next) {
1582 GstStaticPadTemplate *st = (GstStaticPadTemplate *) tmps->data;
1583 if (st->direction != GST_PAD_SINK || st->presence != GST_PAD_ALWAYS)
1585 tcaps = gst_static_pad_template_get_caps (st);
1587 gst_caps_intersect_full (tcaps, caps, GST_CAPS_INTERSECT_FIRST);
1588 filter_caps = gst_caps_merge (filter_caps, intersection);
1589 gst_caps_unref (tcaps);
1593 /* Append the parser caps to prevent any not-negotiated errors */
1594 filter_caps = gst_caps_merge (filter_caps, gst_caps_ref (caps));
1596 delem->capsfilter = gst_element_factory_make ("capsfilter", NULL);
1597 g_object_set (G_OBJECT (delem->capsfilter), "caps", filter_caps, NULL);
1598 gst_caps_unref (filter_caps);
1599 gst_element_set_state (delem->capsfilter, GST_STATE_PAUSED);
1600 gst_bin_add (GST_BIN_CAST (dbin), gst_object_ref (delem->capsfilter));
1602 gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (dpad), NULL);
1603 p = gst_element_get_static_pad (delem->capsfilter, "sink");
1604 gst_pad_link_full (pad, p, GST_PAD_LINK_CHECK_NOTHING);
1605 gst_object_unref (p);
1606 p = gst_element_get_static_pad (delem->capsfilter, "src");
1607 gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (dpad), p);
1610 if (!gst_caps_is_fixed (caps)) {
1611 g_value_array_free (factories);
1616 /* 1.h else continue autoplugging something from the list. */
1617 GST_LOG_OBJECT (pad, "Let's continue discovery on this pad");
1618 res = connect_pad (dbin, src, dpad, pad, caps, factories, chain);
1620 /* Need to unref the capsfilter srcpad here if
1621 * we inserted a capsfilter */
1622 if (is_parser_converter)
1623 gst_object_unref (pad);
1625 gst_object_unref (dpad);
1626 g_value_array_free (factories);
1635 GST_LOG_OBJECT (dbin, "Pad is final. autoplug-continue:%d", apcontinue);
1636 expose_pad (dbin, src, dpad, pad, caps, chain);
1637 gst_object_unref (dpad);
1643 GST_LOG_OBJECT (pad, "Known type, but discarded because not final caps");
1644 chain->deadend = TRUE;
1645 chain->endcaps = gst_caps_ref (caps);
1647 /* Try to expose anything */
1649 if (gst_decode_chain_is_complete (dbin->decode_chain)) {
1650 gst_decode_bin_expose (dbin);
1652 EXPOSE_UNLOCK (dbin);
1653 do_async_done (dbin);
1660 GST_LOG_OBJECT (pad, "Unknown type, posting message and firing signal");
1662 chain->deadend = TRUE;
1663 chain->endcaps = gst_caps_ref (caps);
1665 gst_element_post_message (GST_ELEMENT_CAST (dbin),
1666 gst_missing_decoder_message_new (GST_ELEMENT_CAST (dbin), caps));
1668 g_signal_emit (G_OBJECT (dbin),
1669 gst_decode_bin_signals[SIGNAL_UNKNOWN_TYPE], 0, pad, caps);
1671 /* Try to expose anything */
1673 if (gst_decode_chain_is_complete (dbin->decode_chain)) {
1674 gst_decode_bin_expose (dbin);
1676 EXPOSE_UNLOCK (dbin);
1678 if (src == dbin->typefind) {
1681 if (caps && !gst_caps_is_empty (caps)) {
1682 desc = gst_pb_utils_get_decoder_description (caps);
1683 GST_ELEMENT_ERROR (dbin, STREAM, CODEC_NOT_FOUND,
1684 (_("A %s plugin is required to play this stream, "
1685 "but not installed."), desc),
1686 ("No decoder to handle media type '%s'",
1687 gst_structure_get_name (gst_caps_get_structure (caps, 0))));
1690 GST_ELEMENT_ERROR (dbin, STREAM, TYPE_NOT_FOUND,
1691 (_("Could not determine type of stream")),
1692 ("Stream caps %" GST_PTR_FORMAT, caps));
1694 do_async_done (dbin);
1700 GST_DEBUG_OBJECT (pad, "pad has non-fixed caps delay autoplugging");
1701 gst_object_unref (dpad);
1702 goto setup_caps_delay;
1706 GST_WARNING_OBJECT (pad,
1707 "pad has ANY caps, not able to autoplug to anything");
1708 goto setup_caps_delay;
1712 GstPendingPad *ppad;
1714 /* connect to caps notification */
1715 CHAIN_MUTEX_LOCK (chain);
1716 GST_LOG_OBJECT (dbin, "Chain %p has now %d dynamic pads", chain,
1717 g_list_length (chain->pending_pads));
1718 ppad = g_slice_new0 (GstPendingPad);
1719 ppad->pad = gst_object_ref (pad);
1720 ppad->chain = chain;
1721 ppad->event_probe_id =
1722 gst_pad_add_probe (pad, GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM,
1723 pad_event_cb, ppad, NULL);
1724 chain->pending_pads = g_list_prepend (chain->pending_pads, ppad);
1725 g_signal_connect (G_OBJECT (pad), "notify::caps",
1726 G_CALLBACK (caps_notify_cb), chain);
1727 CHAIN_MUTEX_UNLOCK (chain);
1729 /* If we're here because we have a Parser/Converter
1730 * we have to unref the pad */
1731 if (is_parser_converter)
1732 gst_object_unref (pad);
1739 add_error_filter (GstDecodeBin * dbin, GstElement * element)
1741 GST_OBJECT_LOCK (dbin);
1742 dbin->filtered = g_list_prepend (dbin->filtered, element);
1743 GST_OBJECT_UNLOCK (dbin);
1747 remove_error_filter (GstDecodeBin * dbin, GstElement * element)
1749 GST_OBJECT_LOCK (dbin);
1750 dbin->filtered = g_list_remove (dbin->filtered, element);
1751 GST_OBJECT_UNLOCK (dbin);
1756 * Try to connect the given pad to an element created from one of the factories,
1759 * Note that dpad is ghosting pad, and so pad is linked; be sure to unset dpad's
1760 * target before trying to link pad.
1762 * Returns TRUE if an element was properly created and linked
1765 connect_pad (GstDecodeBin * dbin, GstElement * src, GstDecodePad * dpad,
1766 GstPad * pad, GstCaps * caps, GValueArray * factories,
1767 GstDecodeChain * chain)
1769 gboolean res = FALSE;
1770 GstPad *mqpad = NULL;
1771 gboolean is_demuxer = chain->parent && !chain->elements; /* First pad after the demuxer */
1773 g_return_val_if_fail (factories != NULL, FALSE);
1774 g_return_val_if_fail (factories->n_values > 0, FALSE);
1776 GST_DEBUG_OBJECT (dbin, "pad %s:%s , chain:%p",
1777 GST_DEBUG_PAD_NAME (pad), chain);
1779 /* 1. is element demuxer or parser */
1781 GST_LOG_OBJECT (src,
1782 "is a demuxer, connecting the pad through multiqueue '%s'",
1783 GST_OBJECT_NAME (chain->parent->multiqueue));
1785 gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (dpad), NULL);
1786 if (!(mqpad = gst_decode_group_control_demuxer_pad (chain->parent, pad)))
1788 src = chain->parent->multiqueue;
1790 gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (dpad), pad);
1793 /* 2. Try to create an element and link to it */
1794 while (factories->n_values > 0) {
1795 GstAutoplugSelectResult ret;
1796 GstElementFactory *factory;
1797 GstDecodeElement *delem;
1798 GstElement *element;
1803 /* Set dpad target to pad again, it might've been unset
1804 * below but we came back here because something failed
1806 gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (dpad), pad);
1808 /* take first factory */
1809 factory = g_value_get_object (g_value_array_get_nth (factories, 0));
1810 /* Remove selected factory from the list. */
1811 g_value_array_remove (factories, 0);
1813 /* Check if the caps are really supported by the factory. The
1814 * factory list is non-empty-subset filtered while caps
1815 * are only accepted by a pad if they are a subset of the
1818 * FIXME: Only do this for fixed caps here. Non-fixed caps
1819 * can happen if a Parser/Converter was autoplugged before
1820 * this. We then assume that it will be able to convert to
1821 * everything that the decoder would want.
1823 * A subset check will fail here because the parser caps
1824 * will be generic and while the decoder will only
1825 * support a subset of the parser caps.
1827 if (gst_caps_is_fixed (caps)) {
1828 const GList *templs;
1829 gboolean skip = FALSE;
1831 templs = gst_element_factory_get_static_pad_templates (factory);
1834 GstStaticPadTemplate *templ = (GstStaticPadTemplate *) templs->data;
1836 if (templ->direction == GST_PAD_SINK) {
1837 GstCaps *templcaps = gst_static_caps_get (&templ->static_caps);
1839 if (!gst_caps_is_subset (caps, templcaps)) {
1840 gst_caps_unref (templcaps);
1845 gst_caps_unref (templcaps);
1847 templs = g_list_next (templs);
1853 /* If the factory is for a parser we first check if the factory
1854 * was already used for the current chain. If it was used already
1855 * we would otherwise create an infinite loop here because the
1856 * parser apparently accepts its own output as input.
1857 * This is only done for parsers because it's perfectly valid
1858 * to have other element classes after each other because a
1859 * parser is the only one that does not change the data. A
1860 * valid example for this would be multiple id3demux in a row.
1862 if (strstr (gst_element_factory_get_klass (factory), "Parser")) {
1863 gboolean skip = FALSE;
1866 CHAIN_MUTEX_LOCK (chain);
1867 for (l = chain->elements; l; l = l->next) {
1868 GstDecodeElement *delem = (GstDecodeElement *) l->data;
1869 GstElement *otherelement = delem->element;
1871 if (gst_element_get_factory (otherelement) == factory) {
1876 CHAIN_MUTEX_UNLOCK (chain);
1878 GST_DEBUG_OBJECT (dbin,
1879 "Skipping factory '%s' because it was already used in this chain",
1880 gst_plugin_feature_get_name (GST_PLUGIN_FEATURE_CAST (factory)));
1885 /* emit autoplug-select to see what we should do with it. */
1886 g_signal_emit (G_OBJECT (dbin),
1887 gst_decode_bin_signals[SIGNAL_AUTOPLUG_SELECT],
1888 0, dpad, caps, factory, &ret);
1891 case GST_AUTOPLUG_SELECT_TRY:
1892 GST_DEBUG_OBJECT (dbin, "autoplug select requested try");
1894 case GST_AUTOPLUG_SELECT_EXPOSE:
1895 GST_DEBUG_OBJECT (dbin, "autoplug select requested expose");
1896 /* expose the pad, we don't have the source element */
1897 expose_pad (dbin, src, dpad, pad, caps, chain);
1900 case GST_AUTOPLUG_SELECT_SKIP:
1901 GST_DEBUG_OBJECT (dbin, "autoplug select requested skip");
1904 GST_WARNING_OBJECT (dbin, "autoplug select returned unhandled %d", ret);
1908 /* 2.0. Unlink pad */
1909 gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (dpad), NULL);
1911 /* 2.1. Try to create an element */
1912 if ((element = gst_element_factory_create (factory, NULL)) == NULL) {
1913 GST_WARNING_OBJECT (dbin, "Could not create an element from %s",
1914 gst_plugin_feature_get_name (GST_PLUGIN_FEATURE (factory)));
1918 /* Filter errors, this will prevent the element from causing the pipeline
1919 * to error while we test it using READY state. */
1920 add_error_filter (dbin, element);
1922 /* ... add it ... */
1923 if (!(gst_bin_add (GST_BIN_CAST (dbin), element))) {
1924 GST_WARNING_OBJECT (dbin, "Couldn't add %s to the bin",
1925 GST_ELEMENT_NAME (element));
1926 remove_error_filter (dbin, element);
1927 gst_object_unref (element);
1931 /* Find its sink pad. */
1932 if (!(sinkpad = find_sink_pad (element))) {
1933 GST_WARNING_OBJECT (dbin, "Element %s doesn't have a sink pad",
1934 GST_ELEMENT_NAME (element));
1935 remove_error_filter (dbin, element);
1936 gst_bin_remove (GST_BIN (dbin), element);
1940 /* ... and try to link */
1941 if ((gst_pad_link (pad, sinkpad)) != GST_PAD_LINK_OK) {
1942 GST_WARNING_OBJECT (dbin, "Link failed on pad %s:%s",
1943 GST_DEBUG_PAD_NAME (sinkpad));
1944 remove_error_filter (dbin, element);
1945 gst_object_unref (sinkpad);
1946 gst_bin_remove (GST_BIN (dbin), element);
1950 /* ... activate it ... */
1951 if ((gst_element_set_state (element,
1952 GST_STATE_READY)) == GST_STATE_CHANGE_FAILURE) {
1953 GST_WARNING_OBJECT (dbin, "Couldn't set %s to READY",
1954 GST_ELEMENT_NAME (element));
1955 remove_error_filter (dbin, element);
1956 gst_object_unref (sinkpad);
1957 gst_bin_remove (GST_BIN (dbin), element);
1961 /* Stop filtering errors. */
1962 remove_error_filter (dbin, element);
1964 gst_object_unref (sinkpad);
1965 GST_LOG_OBJECT (dbin, "linked on pad %s:%s", GST_DEBUG_PAD_NAME (pad));
1967 CHAIN_MUTEX_LOCK (chain);
1968 delem = g_slice_new (GstDecodeElement);
1969 delem->element = gst_object_ref (element);
1970 delem->capsfilter = NULL;
1971 chain->elements = g_list_prepend (chain->elements, delem);
1972 chain->demuxer = is_demuxer_element (element);
1973 CHAIN_MUTEX_UNLOCK (chain);
1975 /* Set connection-speed property if needed */
1976 if (chain->demuxer == TRUE) {
1979 if ((pspec = g_object_class_find_property (G_OBJECT_GET_CLASS (element),
1980 "connection-speed"))) {
1981 guint64 speed = dbin->connection_speed / 1000;
1982 gboolean wrong_type = FALSE;
1984 if (G_PARAM_SPEC_TYPE (pspec) == G_TYPE_PARAM_UINT) {
1985 GParamSpecUInt *pspecuint = G_PARAM_SPEC_UINT (pspec);
1987 speed = CLAMP (speed, pspecuint->minimum, pspecuint->maximum);
1988 } else if (G_PARAM_SPEC_TYPE (pspec) == G_TYPE_PARAM_INT) {
1989 GParamSpecInt *pspecint = G_PARAM_SPEC_INT (pspec);
1991 speed = CLAMP (speed, pspecint->minimum, pspecint->maximum);
1992 } else if (G_PARAM_SPEC_TYPE (pspec) == G_TYPE_PARAM_UINT64) {
1993 GParamSpecUInt64 *pspecuint = G_PARAM_SPEC_UINT64 (pspec);
1995 speed = CLAMP (speed, pspecuint->minimum, pspecuint->maximum);
1996 } else if (G_PARAM_SPEC_TYPE (pspec) == G_TYPE_PARAM_INT64) {
1997 GParamSpecInt64 *pspecint = G_PARAM_SPEC_INT64 (pspec);
1999 speed = CLAMP (speed, pspecint->minimum, pspecint->maximum);
2001 GST_WARNING_OBJECT (dbin,
2002 "The connection speed property %" G_GUINT64_FORMAT " of type %s"
2003 " is not usefull not setting it", speed,
2004 g_type_name (G_PARAM_SPEC_TYPE (pspec)));
2008 if (wrong_type == FALSE) {
2009 GST_DEBUG_OBJECT (dbin, "setting connection-speed=%" G_GUINT64_FORMAT
2010 " to demuxer element", speed);
2012 g_object_set (element, "connection-speed", speed, NULL);
2017 /* link this element further */
2018 connect_element (dbin, element, chain);
2020 /* try to configure the subtitle encoding property when we can */
2021 pspec = g_object_class_find_property (G_OBJECT_GET_CLASS (element),
2022 "subtitle-encoding");
2023 if (pspec && G_PARAM_SPEC_VALUE_TYPE (pspec) == G_TYPE_STRING) {
2024 SUBTITLE_LOCK (dbin);
2025 GST_DEBUG_OBJECT (dbin,
2026 "setting subtitle-encoding=%s to element", dbin->encoding);
2027 g_object_set (G_OBJECT (element), "subtitle-encoding", dbin->encoding,
2029 SUBTITLE_UNLOCK (dbin);
2035 /* Bring the element to the state of the parent */
2036 if ((gst_element_set_state (element,
2037 GST_STATE_PAUSED)) == GST_STATE_CHANGE_FAILURE) {
2038 GstDecodeElement *dtmp = NULL;
2039 GstElement *tmp = NULL;
2041 GST_WARNING_OBJECT (dbin, "Couldn't set %s to PAUSED",
2042 GST_ELEMENT_NAME (element));
2044 /* Remove all elements in this chain that were just added. No
2045 * other thread could've added elements in the meantime */
2046 CHAIN_MUTEX_LOCK (chain);
2050 dtmp = chain->elements->data;
2051 tmp = dtmp->element;
2053 /* Disconnect any signal handlers that might be connected
2054 * in connect_element() or analyze_pad() */
2055 g_signal_handlers_disconnect_by_func (tmp, pad_added_cb, chain);
2056 g_signal_handlers_disconnect_by_func (tmp, pad_removed_cb, chain);
2057 g_signal_handlers_disconnect_by_func (tmp, no_more_pads_cb, chain);
2059 for (l = chain->pending_pads; l;) {
2060 GstPendingPad *pp = l->data;
2063 if (GST_PAD_PARENT (pp->pad) != tmp) {
2068 g_signal_handlers_disconnect_by_func (pp->pad, caps_notify_cb, chain);
2069 gst_pad_remove_probe (pp->pad, pp->event_probe_id);
2070 gst_object_unref (pp->pad);
2071 g_slice_free (GstPendingPad, pp);
2073 /* Remove element from the list, update list head and go to the
2074 * next element in the list */
2076 chain->pending_pads = g_list_delete_link (chain->pending_pads, l);
2080 if (dtmp->capsfilter) {
2081 gst_bin_remove (GST_BIN (dbin), dtmp->capsfilter);
2082 gst_element_set_state (dtmp->capsfilter, GST_STATE_NULL);
2083 gst_object_unref (dtmp->capsfilter);
2086 gst_bin_remove (GST_BIN (dbin), tmp);
2087 gst_element_set_state (tmp, GST_STATE_NULL);
2089 gst_object_unref (tmp);
2090 g_slice_free (GstDecodeElement, dtmp);
2092 chain->elements = g_list_delete_link (chain->elements, chain->elements);
2093 } while (tmp != element);
2094 CHAIN_MUTEX_UNLOCK (chain);
2099 SUBTITLE_LOCK (dbin);
2100 /* we added the element now, add it to the list of subtitle-encoding
2101 * elements when we can set the property */
2102 dbin->subtitles = g_list_prepend (dbin->subtitles, element);
2103 SUBTITLE_UNLOCK (dbin);
2112 gst_object_unref (mqpad);
2118 get_pad_caps (GstPad * pad)
2122 /* first check the pad caps, if this is set, we are positively sure it is
2123 * fixed and exactly what the element will produce. */
2124 caps = gst_pad_get_current_caps (pad);
2126 /* then use the getcaps function if we don't have caps. These caps might not
2127 * be fixed in some cases, in which case analyze_new_pad will set up a
2128 * notify::caps signal to continue autoplugging. */
2130 caps = gst_pad_query_caps (pad, NULL);
2136 connect_element (GstDecodeBin * dbin, GstElement * element,
2137 GstDecodeChain * chain)
2140 gboolean res = TRUE;
2141 gboolean dynamic = FALSE;
2142 GList *to_connect = NULL;
2144 GST_DEBUG_OBJECT (dbin, "Attempting to connect element %s [chain:%p] further",
2145 GST_ELEMENT_NAME (element), chain);
2147 /* 1. Loop over pad templates, grabbing existing pads along the way */
2148 for (pads = GST_ELEMENT_GET_CLASS (element)->padtemplates; pads;
2149 pads = g_list_next (pads)) {
2150 GstPadTemplate *templ = GST_PAD_TEMPLATE (pads->data);
2151 const gchar *templ_name;
2153 /* we are only interested in source pads */
2154 if (GST_PAD_TEMPLATE_DIRECTION (templ) != GST_PAD_SRC)
2157 templ_name = GST_PAD_TEMPLATE_NAME_TEMPLATE (templ);
2158 GST_DEBUG_OBJECT (dbin, "got a source pad template %s", templ_name);
2160 /* figure out what kind of pad this is */
2161 switch (GST_PAD_TEMPLATE_PRESENCE (templ)) {
2162 case GST_PAD_ALWAYS:
2164 /* get the pad that we need to autoplug */
2165 GstPad *pad = gst_element_get_static_pad (element, templ_name);
2168 GST_DEBUG_OBJECT (dbin, "got the pad for always template %s",
2170 /* here is the pad, we need to autoplug it */
2171 to_connect = g_list_prepend (to_connect, pad);
2173 /* strange, pad is marked as always but it's not
2174 * there. Fix the element */
2175 GST_WARNING_OBJECT (dbin,
2176 "could not get the pad for always template %s", templ_name);
2180 case GST_PAD_SOMETIMES:
2182 /* try to get the pad to see if it is already created or
2184 GstPad *pad = gst_element_get_static_pad (element, templ_name);
2187 GST_DEBUG_OBJECT (dbin, "got the pad for sometimes template %s",
2189 /* the pad is created, we need to autoplug it */
2190 to_connect = g_list_prepend (to_connect, pad);
2192 GST_DEBUG_OBJECT (dbin,
2193 "did not get the sometimes pad of template %s", templ_name);
2194 /* we have an element that will create dynamic pads */
2199 case GST_PAD_REQUEST:
2200 /* ignore request pads */
2201 GST_DEBUG_OBJECT (dbin, "ignoring request padtemplate %s", templ_name);
2206 /* 2. if there are more potential pads, connect to relevant signals */
2208 GST_LOG_OBJECT (dbin, "Adding signals to element %s in chain %p",
2209 GST_ELEMENT_NAME (element), chain);
2210 g_signal_connect (G_OBJECT (element), "pad-added",
2211 G_CALLBACK (pad_added_cb), chain);
2212 g_signal_connect (G_OBJECT (element), "pad-removed",
2213 G_CALLBACK (pad_removed_cb), chain);
2214 g_signal_connect (G_OBJECT (element), "no-more-pads",
2215 G_CALLBACK (no_more_pads_cb), chain);
2218 /* 3. for every available pad, connect it */
2219 for (pads = to_connect; pads; pads = g_list_next (pads)) {
2220 GstPad *pad = GST_PAD_CAST (pads->data);
2223 caps = get_pad_caps (pad);
2224 analyze_new_pad (dbin, element, pad, caps, chain);
2226 gst_caps_unref (caps);
2228 gst_object_unref (pad);
2230 g_list_free (to_connect);
2237 * Expose the given pad on the chain as a decoded pad.
2240 expose_pad (GstDecodeBin * dbin, GstElement * src, GstDecodePad * dpad,
2241 GstPad * pad, GstCaps * caps, GstDecodeChain * chain)
2243 GstPad *mqpad = NULL;
2245 GST_DEBUG_OBJECT (dbin, "pad %s:%s, chain:%p",
2246 GST_DEBUG_PAD_NAME (pad), chain);
2248 /* If this is the first pad for this chain, there are no other elements
2249 * and the source element is not the multiqueue we must link through the
2252 * This is the case if a demuxer directly exposed a raw pad.
2254 if (chain->parent && !chain->elements && src != chain->parent->multiqueue) {
2255 GST_LOG_OBJECT (src, "connecting the pad through multiqueue");
2257 gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (dpad), NULL);
2258 if (!(mqpad = gst_decode_group_control_demuxer_pad (chain->parent, pad)))
2261 gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (dpad), pad);
2264 gst_decode_pad_activate (dpad, chain);
2265 chain->endpad = gst_object_ref (dpad);
2266 chain->endcaps = gst_caps_ref (caps);
2269 if (gst_decode_chain_is_complete (dbin->decode_chain)) {
2270 gst_decode_bin_expose (dbin);
2272 EXPOSE_UNLOCK (dbin);
2275 gst_object_unref (mqpad);
2281 /* check_upstream_seekable:
2283 * Check if upstream is seekable.
2286 check_upstream_seekable (GstDecodeBin * dbin, GstPad * pad)
2289 gint64 start = -1, stop = -1;
2290 gboolean seekable = FALSE;
2292 query = gst_query_new_seeking (GST_FORMAT_BYTES);
2293 if (!gst_pad_peer_query (pad, query)) {
2294 GST_DEBUG_OBJECT (dbin, "seeking query failed");
2295 gst_query_unref (query);
2299 gst_query_parse_seeking (query, NULL, &seekable, &start, &stop);
2301 gst_query_unref (query);
2303 /* try harder to query upstream size if we didn't get it the first time */
2304 if (seekable && stop == -1) {
2305 GST_DEBUG_OBJECT (dbin, "doing duration query to fix up unset stop");
2306 gst_pad_peer_query_duration (pad, GST_FORMAT_BYTES, &stop);
2309 /* if upstream doesn't know the size, it's likely that it's not seekable in
2310 * practice even if it technically may be seekable */
2311 if (seekable && (start != 0 || stop <= start)) {
2312 GST_DEBUG_OBJECT (dbin, "seekable but unknown start/stop -> disable");
2316 GST_DEBUG_OBJECT (dbin, "upstream seekable: %d", seekable);
2321 type_found (GstElement * typefind, guint probability,
2322 GstCaps * caps, GstDecodeBin * decode_bin)
2324 GstPad *pad, *sink_pad;
2326 GST_DEBUG_OBJECT (decode_bin, "typefind found caps %" GST_PTR_FORMAT, caps);
2328 /* If the typefinder (but not something else) finds text/plain - i.e. that's
2329 * the top-level type of the file - then error out.
2331 if (gst_structure_has_name (gst_caps_get_structure (caps, 0), "text/plain")) {
2332 GST_ELEMENT_ERROR (decode_bin, STREAM, WRONG_TYPE,
2333 (_("This appears to be a text file")),
2334 ("decodebin cannot decode plain text files"));
2338 /* FIXME: we can only deal with one type, we don't yet support dynamically changing
2339 * caps from the typefind element */
2340 if (decode_bin->have_type || decode_bin->decode_chain)
2343 decode_bin->have_type = TRUE;
2345 pad = gst_element_get_static_pad (typefind, "src");
2346 sink_pad = gst_element_get_static_pad (typefind, "sink");
2348 /* need some lock here to prevent race with shutdown state change
2349 * which might yank away e.g. decode_chain while building stuff here.
2350 * In typical cases, STREAM_LOCK is held and handles that, it need not
2351 * be held (if called from a proxied setcaps), so grab it anyway */
2352 GST_PAD_STREAM_LOCK (sink_pad);
2353 decode_bin->decode_chain = gst_decode_chain_new (decode_bin, NULL, pad);
2354 analyze_new_pad (decode_bin, typefind, pad, caps, decode_bin->decode_chain);
2355 GST_PAD_STREAM_UNLOCK (sink_pad);
2357 gst_object_unref (sink_pad);
2358 gst_object_unref (pad);
2364 static GstPadProbeReturn
2365 pad_event_cb (GstPad * pad, GstPadProbeInfo * info, gpointer data)
2367 GstEvent *event = GST_PAD_PROBE_INFO_EVENT (info);
2368 GstPendingPad *ppad = (GstPendingPad *) data;
2369 GstDecodeChain *chain = ppad->chain;
2370 GstDecodeBin *dbin = chain->dbin;
2375 switch (GST_EVENT_TYPE (event)) {
2377 GST_DEBUG_OBJECT (dbin, "Received EOS on a non final pad, this stream "
2379 chain->deadend = TRUE;
2380 /* we don't set the endcaps because NULL endcaps means early EOS */
2382 if (gst_decode_chain_is_complete (dbin->decode_chain))
2383 gst_decode_bin_expose (dbin);
2384 EXPOSE_UNLOCK (dbin);
2389 return GST_PAD_PROBE_OK;
2393 pad_added_cb (GstElement * element, GstPad * pad, GstDecodeChain * chain)
2400 GST_DEBUG_OBJECT (pad, "pad added, chain:%p", chain);
2402 caps = get_pad_caps (pad);
2403 analyze_new_pad (dbin, element, pad, caps, chain);
2405 gst_caps_unref (caps);
2408 if (gst_decode_chain_is_complete (dbin->decode_chain)) {
2409 GST_LOG_OBJECT (dbin,
2410 "That was the last dynamic object, now attempting to expose the group");
2411 if (!gst_decode_bin_expose (dbin))
2412 GST_WARNING_OBJECT (dbin, "Couldn't expose group");
2414 EXPOSE_UNLOCK (dbin);
2418 pad_removed_cb (GstElement * element, GstPad * pad, GstDecodeChain * chain)
2422 GST_LOG_OBJECT (pad, "pad removed, chain:%p", chain);
2424 /* In fact, we don't have to do anything here, the active group will be
2425 * removed when the group's multiqueue is drained */
2426 CHAIN_MUTEX_LOCK (chain);
2427 for (l = chain->pending_pads; l; l = l->next) {
2428 GstPendingPad *ppad = l->data;
2429 GstPad *opad = ppad->pad;
2432 g_signal_handlers_disconnect_by_func (pad, caps_notify_cb, chain);
2433 gst_pending_pad_free (ppad);
2434 chain->pending_pads = g_list_delete_link (chain->pending_pads, l);
2438 CHAIN_MUTEX_UNLOCK (chain);
2442 no_more_pads_cb (GstElement * element, GstDecodeChain * chain)
2444 GstDecodeGroup *group = NULL;
2446 GST_LOG_OBJECT (element, "got no more pads");
2448 CHAIN_MUTEX_LOCK (chain);
2449 if (!chain->elements
2450 || ((GstDecodeElement *) chain->elements->data)->element != element) {
2451 GST_LOG_OBJECT (chain->dbin, "no-more-pads from old chain element '%s'",
2452 GST_OBJECT_NAME (element));
2453 CHAIN_MUTEX_UNLOCK (chain);
2455 } else if (!chain->demuxer) {
2456 GST_LOG_OBJECT (chain->dbin, "no-more-pads from a non-demuxer element '%s'",
2457 GST_OBJECT_NAME (element));
2458 CHAIN_MUTEX_UNLOCK (chain);
2462 /* when we received no_more_pads, we can complete the pads of the chain */
2463 if (!chain->next_groups && chain->active_group) {
2464 group = chain->active_group;
2465 } else if (chain->next_groups) {
2466 group = chain->next_groups->data;
2469 GST_ERROR_OBJECT (chain->dbin, "can't find group for element");
2470 CHAIN_MUTEX_UNLOCK (chain);
2474 GST_DEBUG_OBJECT (element, "Setting group %p to complete", group);
2476 group->no_more_pads = TRUE;
2477 /* this group has prerolled enough to not need more pads,
2478 * we can probably set its buffering state to playing now */
2479 GST_DEBUG_OBJECT (group->dbin, "Setting group %p multiqueue to "
2480 "'playing' buffering mode", group);
2481 decodebin_set_queue_size (group->dbin, group->multiqueue, FALSE,
2482 (group->parent ? group->parent->seekable : TRUE));
2483 CHAIN_MUTEX_UNLOCK (chain);
2485 EXPOSE_LOCK (chain->dbin);
2486 if (gst_decode_chain_is_complete (chain->dbin->decode_chain)) {
2487 gst_decode_bin_expose (chain->dbin);
2489 EXPOSE_UNLOCK (chain->dbin);
2493 caps_notify_cb (GstPad * pad, GParamSpec * unused, GstDecodeChain * chain)
2495 GstElement *element;
2498 GST_LOG_OBJECT (pad, "Notified caps for pad %s:%s", GST_DEBUG_PAD_NAME (pad));
2500 /* Disconnect this; if we still need it, we'll reconnect to this in
2501 * analyze_new_pad */
2502 g_signal_handlers_disconnect_by_func (pad, caps_notify_cb, chain);
2504 element = GST_ELEMENT_CAST (gst_pad_get_parent (pad));
2506 CHAIN_MUTEX_LOCK (chain);
2507 for (l = chain->pending_pads; l; l = l->next) {
2508 GstPendingPad *ppad = l->data;
2509 if (ppad->pad == pad) {
2510 gst_pending_pad_free (ppad);
2511 chain->pending_pads = g_list_delete_link (chain->pending_pads, l);
2515 CHAIN_MUTEX_UNLOCK (chain);
2517 pad_added_cb (element, pad, chain);
2519 gst_object_unref (element);
2522 /* Decide whether an element is a demuxer based on the
2523 * klass and number/type of src pad templates it has */
2525 is_demuxer_element (GstElement * srcelement)
2527 GstElementFactory *srcfactory;
2528 GstElementClass *elemclass;
2531 gint potential_src_pads = 0;
2533 srcfactory = gst_element_get_factory (srcelement);
2534 klass = gst_element_factory_get_klass (srcfactory);
2536 /* Can't be a demuxer unless it has Demux in the klass name */
2537 if (!strstr (klass, "Demux"))
2540 /* Walk the src pad templates and count how many the element
2542 elemclass = GST_ELEMENT_GET_CLASS (srcelement);
2544 walk = gst_element_class_get_pad_template_list (elemclass);
2545 while (walk != NULL) {
2546 GstPadTemplate *templ;
2548 templ = (GstPadTemplate *) walk->data;
2549 if (GST_PAD_TEMPLATE_DIRECTION (templ) == GST_PAD_SRC) {
2550 switch (GST_PAD_TEMPLATE_PRESENCE (templ)) {
2551 case GST_PAD_ALWAYS:
2552 case GST_PAD_SOMETIMES:
2553 if (strstr (GST_PAD_TEMPLATE_NAME_TEMPLATE (templ), "%"))
2554 potential_src_pads += 2; /* Might make multiple pads */
2556 potential_src_pads += 1;
2558 case GST_PAD_REQUEST:
2559 potential_src_pads += 2;
2563 walk = g_list_next (walk);
2566 if (potential_src_pads < 2)
2572 /* Returns TRUE if the caps are compatible with the caps specified in the 'caps'
2573 * property (which by default are the raw caps)
2575 * The decodebin_lock should be taken !
2578 are_final_caps (GstDecodeBin * dbin, GstCaps * caps)
2582 GST_LOG_OBJECT (dbin, "Checking with caps %" GST_PTR_FORMAT, caps);
2584 /* lock for getting the caps */
2585 GST_OBJECT_LOCK (dbin);
2586 res = gst_caps_can_intersect (dbin->caps, caps);
2587 GST_OBJECT_UNLOCK (dbin);
2589 GST_LOG_OBJECT (dbin, "Caps are %sfinal caps", res ? "" : "not ");
2595 * GstDecodeChain functions
2598 /* gst_decode_chain_get_current_group:
2600 * Returns the current group of this chain, to which
2601 * new chains should be attached or NULL if the last
2602 * group didn't have no-more-pads.
2604 * Not MT-safe: Call with parent chain lock!
2606 static GstDecodeGroup *
2607 gst_decode_chain_get_current_group (GstDecodeChain * chain)
2609 GstDecodeGroup *group;
2611 if (!chain->next_groups && chain->active_group
2612 && chain->active_group->overrun && !chain->active_group->no_more_pads) {
2613 GST_WARNING_OBJECT (chain->dbin,
2614 "Currently active group %p is exposed"
2615 " and wants to add a new pad without having signaled no-more-pads",
2616 chain->active_group);
2620 if (chain->next_groups && (group = chain->next_groups->data) && group->overrun
2621 && !group->no_more_pads) {
2622 GST_WARNING_OBJECT (chain->dbin,
2623 "Currently newest pending group %p "
2624 "had overflow but didn't signal no-more-pads", group);
2628 /* Now we know that we can really return something useful */
2629 if (!chain->active_group) {
2630 chain->active_group = group = gst_decode_group_new (chain->dbin, chain);
2631 } else if (!chain->active_group->overrun
2632 && !chain->active_group->no_more_pads) {
2633 group = chain->active_group;
2634 } else if (chain->next_groups && (group = chain->next_groups->data)
2635 && !group->overrun && !group->no_more_pads) {
2636 /* group = chain->next_groups->data */
2638 group = gst_decode_group_new (chain->dbin, chain);
2639 chain->next_groups = g_list_prepend (chain->next_groups, group);
2645 static void gst_decode_group_free_internal (GstDecodeGroup * group,
2649 gst_decode_chain_free_internal (GstDecodeChain * chain, gboolean hide)
2653 CHAIN_MUTEX_LOCK (chain);
2655 GST_DEBUG_OBJECT (chain->dbin, "%s chain %p", (hide ? "Hiding" : "Freeing"),
2658 if (chain->active_group) {
2659 gst_decode_group_free_internal (chain->active_group, hide);
2661 chain->active_group = NULL;
2664 for (l = chain->next_groups; l; l = l->next) {
2665 gst_decode_group_free_internal ((GstDecodeGroup *) l->data, hide);
2670 g_list_free (chain->next_groups);
2671 chain->next_groups = NULL;
2675 for (l = chain->old_groups; l; l = l->next) {
2676 GstDecodeGroup *group = l->data;
2678 gst_decode_group_free (group);
2680 g_list_free (chain->old_groups);
2681 chain->old_groups = NULL;
2684 for (l = chain->pending_pads; l; l = l->next) {
2685 GstPendingPad *ppad = l->data;
2686 GstPad *pad = ppad->pad;
2688 g_signal_handlers_disconnect_by_func (pad, caps_notify_cb, chain);
2689 gst_pending_pad_free (ppad);
2692 g_list_free (chain->pending_pads);
2693 chain->pending_pads = NULL;
2695 for (l = chain->elements; l; l = l->next) {
2696 GstDecodeElement *delem = l->data;
2697 GstElement *element = delem->element;
2699 g_signal_handlers_disconnect_by_func (element, pad_added_cb, chain);
2700 g_signal_handlers_disconnect_by_func (element, pad_removed_cb, chain);
2701 g_signal_handlers_disconnect_by_func (element, no_more_pads_cb, chain);
2703 if (delem->capsfilter) {
2704 if (GST_OBJECT_PARENT (delem->capsfilter) ==
2705 GST_OBJECT_CAST (chain->dbin))
2706 gst_bin_remove (GST_BIN_CAST (chain->dbin), delem->capsfilter);
2708 gst_element_set_state (delem->capsfilter, GST_STATE_NULL);
2712 if (GST_OBJECT_PARENT (element) == GST_OBJECT_CAST (chain->dbin))
2713 gst_bin_remove (GST_BIN_CAST (chain->dbin), element);
2715 gst_element_set_state (element, GST_STATE_NULL);
2718 SUBTITLE_LOCK (chain->dbin);
2719 /* remove possible subtitle element */
2720 chain->dbin->subtitles = g_list_remove (chain->dbin->subtitles, element);
2721 SUBTITLE_UNLOCK (chain->dbin);
2724 if (delem->capsfilter) {
2725 gst_object_unref (delem->capsfilter);
2726 delem->capsfilter = NULL;
2729 gst_object_unref (element);
2732 g_slice_free (GstDecodeElement, delem);
2736 g_list_free (chain->elements);
2737 chain->elements = NULL;
2740 if (chain->endpad) {
2741 if (chain->endpad->exposed) {
2742 gst_element_remove_pad (GST_ELEMENT_CAST (chain->dbin),
2743 GST_PAD_CAST (chain->endpad));
2746 gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (chain->endpad), NULL);
2747 chain->endpad->exposed = FALSE;
2749 gst_object_unref (chain->endpad);
2750 chain->endpad = NULL;
2755 gst_object_unref (chain->pad);
2759 if (chain->endcaps) {
2760 gst_caps_unref (chain->endcaps);
2761 chain->endcaps = NULL;
2764 GST_DEBUG_OBJECT (chain->dbin, "%s chain %p", (hide ? "Hidden" : "Freed"),
2766 CHAIN_MUTEX_UNLOCK (chain);
2768 g_mutex_free (chain->lock);
2769 g_slice_free (GstDecodeChain, chain);
2773 /* gst_decode_chain_free:
2775 * Completely frees and removes the chain and all
2776 * child groups from decodebin.
2778 * MT-safe, don't hold the chain lock or any child chain's lock
2779 * when calling this!
2782 gst_decode_chain_free (GstDecodeChain * chain)
2784 gst_decode_chain_free_internal (chain, FALSE);
2787 /* gst_decode_chain_new:
2789 * Creates a new decode chain and initializes it.
2791 * It's up to the caller to add it to the list of child chains of
2794 static GstDecodeChain *
2795 gst_decode_chain_new (GstDecodeBin * dbin, GstDecodeGroup * parent,
2798 GstDecodeChain *chain = g_slice_new0 (GstDecodeChain);
2800 GST_DEBUG_OBJECT (dbin, "Creating new chain %p with parent group %p", chain,
2804 chain->parent = parent;
2805 chain->lock = g_mutex_new ();
2806 chain->pad = gst_object_ref (pad);
2812 * GstDecodeGroup functions
2815 /* The overrun callback is used to expose groups that have not yet had their
2816 * no_more_pads called while the (large) multiqueue overflowed. When this
2817 * happens we must assume that the no_more_pads will not arrive anymore and we
2818 * must expose the pads that we have.
2821 multi_queue_overrun_cb (GstElement * queue, GstDecodeGroup * group)
2827 GST_LOG_OBJECT (dbin, "multiqueue '%s' (%p) is full", GST_OBJECT_NAME (queue),
2830 group->overrun = TRUE;
2831 /* this group has prerolled enough to not need more pads,
2832 * we can probably set its buffering state to playing now */
2833 GST_DEBUG_OBJECT (group->dbin, "Setting group %p multiqueue to "
2834 "'playing' buffering mode", group);
2835 decodebin_set_queue_size (group->dbin, group->multiqueue, FALSE,
2836 (group->parent ? group->parent->seekable : TRUE));
2838 /* FIXME: We should make sure that everything gets exposed now
2839 * even if child chains are not complete because the will never
2840 * be complete! Ignore any non-complete chains when exposing
2841 * and never expose them later
2845 if (gst_decode_chain_is_complete (dbin->decode_chain)) {
2846 if (!gst_decode_bin_expose (dbin))
2847 GST_WARNING_OBJECT (dbin, "Couldn't expose group");
2849 EXPOSE_UNLOCK (group->dbin);
2853 gst_decode_group_free_internal (GstDecodeGroup * group, gboolean hide)
2857 GST_DEBUG_OBJECT (group->dbin, "%s group %p", (hide ? "Hiding" : "Freeing"),
2859 for (l = group->children; l; l = l->next) {
2860 GstDecodeChain *chain = (GstDecodeChain *) l->data;
2862 gst_decode_chain_free_internal (chain, hide);
2867 g_list_free (group->children);
2868 group->children = NULL;
2872 for (l = group->reqpads; l; l = l->next) {
2873 GstPad *pad = l->data;
2875 gst_element_release_request_pad (group->multiqueue, pad);
2876 gst_object_unref (pad);
2879 g_list_free (group->reqpads);
2880 group->reqpads = NULL;
2883 if (group->multiqueue) {
2884 if (group->overrunsig) {
2885 g_signal_handler_disconnect (group->multiqueue, group->overrunsig);
2886 group->overrunsig = 0;
2889 if (GST_OBJECT_PARENT (group->multiqueue) == GST_OBJECT_CAST (group->dbin))
2890 gst_bin_remove (GST_BIN_CAST (group->dbin), group->multiqueue);
2892 gst_element_set_state (group->multiqueue, GST_STATE_NULL);
2893 gst_object_unref (group->multiqueue);
2894 group->multiqueue = NULL;
2898 GST_DEBUG_OBJECT (group->dbin, "%s group %p", (hide ? "Hided" : "Freed"),
2901 g_slice_free (GstDecodeGroup, group);
2904 /* gst_decode_group_free:
2906 * Completely frees and removes the decode group and all
2909 * Never call this from any streaming thread!
2911 * Not MT-safe, call with parent's chain lock!
2914 gst_decode_group_free (GstDecodeGroup * group)
2916 gst_decode_group_free_internal (group, FALSE);
2919 /* gst_decode_group_hide:
2921 * Hide the decode group only, this means that
2922 * all child endpads are removed from decodebin
2923 * and all signals are unconnected.
2925 * No element is set to NULL state and completely
2928 * Can be called from streaming threads.
2930 * Not MT-safe, call with parent's chain lock!
2933 gst_decode_group_hide (GstDecodeGroup * group)
2935 gst_decode_group_free_internal (group, TRUE);
2938 /* configure queue sizes, this depends on the buffering method and if we are
2939 * playing or prerolling. */
2941 decodebin_set_queue_size (GstDecodeBin * dbin, GstElement * multiqueue,
2942 gboolean preroll, gboolean seekable)
2944 guint max_bytes, max_buffers;
2947 if (preroll || dbin->use_buffering) {
2948 /* takes queue limits, initially we only queue up up to the max bytes limit,
2949 * with a default of 2MB. we use the same values for buffering mode. */
2950 if ((max_bytes = dbin->max_size_bytes) == 0)
2951 max_bytes = AUTO_PREROLL_SIZE_BYTES;
2952 if ((max_buffers = dbin->max_size_buffers) == 0)
2953 max_buffers = AUTO_PREROLL_SIZE_BUFFERS;
2954 if ((max_time = dbin->max_size_time) == 0)
2955 max_time = seekable ? AUTO_PREROLL_SEEKABLE_SIZE_TIME :
2956 AUTO_PREROLL_NOT_SEEKABLE_SIZE_TIME;
2958 /* update runtime limits. At runtime, we try to keep the amount of buffers
2959 * in the queues as low as possible (but at least 5 buffers). */
2960 if ((max_bytes = dbin->max_size_bytes) == 0)
2961 max_bytes = AUTO_PLAY_SIZE_BYTES;
2962 if ((max_buffers = dbin->max_size_buffers) == 0)
2963 max_buffers = AUTO_PLAY_SIZE_BUFFERS;
2964 if ((max_time = dbin->max_size_time) == 0)
2965 max_time = AUTO_PLAY_SIZE_TIME;
2968 g_object_set (multiqueue,
2969 "max-size-bytes", max_bytes, "max-size-time", max_time,
2970 "max-size-buffers", max_buffers, NULL);
2973 /* gst_decode_group_new:
2974 * @dbin: Parent decodebin
2975 * @parent: Parent chain or %NULL
2977 * Creates a new GstDecodeGroup. It is up to the caller to add it to the list
2980 static GstDecodeGroup *
2981 gst_decode_group_new (GstDecodeBin * dbin, GstDecodeChain * parent)
2983 GstDecodeGroup *group = g_slice_new0 (GstDecodeGroup);
2987 GST_DEBUG_OBJECT (dbin, "Creating new group %p with parent chain %p", group,
2991 group->parent = parent;
2993 mq = group->multiqueue = gst_element_factory_make ("multiqueue", NULL);
2994 if (G_UNLIKELY (!group->multiqueue))
2995 goto missing_multiqueue;
2997 /* default is for use-buffering is FALSE */
2998 if (dbin->use_buffering) {
3000 "use-buffering", TRUE,
3001 "low-percent", dbin->low_percent,
3002 "high-percent", dbin->high_percent, NULL);
3005 /* configure queue sizes for preroll */
3007 if (parent && parent->demuxer) {
3008 GstElement *element =
3009 ((GstDecodeElement *) parent->elements->data)->element;
3010 GstPad *pad = gst_element_get_static_pad (element, "sink");
3012 seekable = parent->seekable = check_upstream_seekable (dbin, pad);
3013 gst_object_unref (pad);
3016 decodebin_set_queue_size (dbin, mq, TRUE, seekable);
3018 group->overrunsig = g_signal_connect (G_OBJECT (mq), "overrun",
3019 G_CALLBACK (multi_queue_overrun_cb), group);
3021 gst_bin_add (GST_BIN (dbin), gst_object_ref (mq));
3022 gst_element_set_state (mq, GST_STATE_PAUSED);
3029 gst_element_post_message (GST_ELEMENT_CAST (dbin),
3030 gst_missing_element_message_new (GST_ELEMENT_CAST (dbin),
3032 GST_ELEMENT_ERROR (dbin, CORE, MISSING_PLUGIN, (NULL), ("no multiqueue!"));
3033 g_slice_free (GstDecodeGroup, group);
3038 /* gst_decode_group_control_demuxer_pad
3040 * Adds a new demuxer srcpad to the given group.
3042 * Returns the srcpad of the multiqueue corresponding the given pad.
3043 * Returns NULL if there was an error.
3046 gst_decode_group_control_demuxer_pad (GstDecodeGroup * group, GstPad * pad)
3049 GstPad *srcpad, *sinkpad;
3050 GstIterator *it = NULL;
3051 GValue item = { 0, };
3055 GST_LOG_OBJECT (dbin, "group:%p pad %s:%s", group, GST_DEBUG_PAD_NAME (pad));
3059 if (G_UNLIKELY (!group->multiqueue))
3062 if (!(sinkpad = gst_element_get_request_pad (group->multiqueue, "sink_%u"))) {
3063 GST_ERROR_OBJECT (dbin, "Couldn't get sinkpad from multiqueue");
3067 if ((gst_pad_link (pad, sinkpad) != GST_PAD_LINK_OK)) {
3068 GST_ERROR_OBJECT (dbin, "Couldn't link demuxer and multiqueue");
3072 it = gst_pad_iterate_internal_links (sinkpad);
3074 if (!it || (gst_iterator_next (it, &item)) != GST_ITERATOR_OK
3075 || ((srcpad = g_value_dup_object (&item)) == NULL)) {
3076 GST_ERROR_OBJECT (dbin,
3077 "Couldn't get srcpad from multiqueue for sinkpad %" GST_PTR_FORMAT,
3081 CHAIN_MUTEX_LOCK (group->parent);
3082 group->reqpads = g_list_prepend (group->reqpads, gst_object_ref (sinkpad));
3083 CHAIN_MUTEX_UNLOCK (group->parent);
3086 g_value_unset (&item);
3088 gst_iterator_free (it);
3089 gst_object_unref (sinkpad);
3093 gst_element_release_request_pad (group->multiqueue, sinkpad);
3097 /* gst_decode_group_is_complete:
3099 * Checks if the group is complete, this means that
3100 * a) overrun of the multiqueue or no-more-pads happened
3101 * b) all child chains are complete
3103 * Not MT-safe, always call with decodebin expose lock
3106 gst_decode_group_is_complete (GstDecodeGroup * group)
3109 gboolean complete = TRUE;
3111 if (!group->overrun && !group->no_more_pads) {
3116 for (l = group->children; l; l = l->next) {
3117 GstDecodeChain *chain = l->data;
3119 if (!gst_decode_chain_is_complete (chain)) {
3126 GST_DEBUG_OBJECT (group->dbin, "Group %p is complete: %d", group, complete);
3130 /* gst_decode_chain_is_complete:
3132 * Returns TRUE if the chain is complete, this means either
3133 * a) This chain is a dead end, i.e. we have no suitable plugins
3134 * b) This chain ends in an endpad and this is blocked or exposed
3136 * Not MT-safe, always call with decodebin expose lock
3139 gst_decode_chain_is_complete (GstDecodeChain * chain)
3141 gboolean complete = FALSE;
3143 CHAIN_MUTEX_LOCK (chain);
3144 if (chain->deadend) {
3149 if (chain->endpad && (chain->endpad->blocked || chain->endpad->exposed)) {
3154 if (chain->demuxer) {
3155 if (chain->active_group
3156 && gst_decode_group_is_complete (chain->active_group)) {
3163 CHAIN_MUTEX_UNLOCK (chain);
3164 GST_DEBUG_OBJECT (chain->dbin, "Chain %p is complete: %d", chain, complete);
3169 drain_and_switch_chains (GstDecodeChain * chain, GstDecodePad * drainpad,
3170 gboolean * last_group, gboolean * drained, gboolean * switched);
3171 /* drain_and_switch_chains/groups:
3173 * CALL WITH CHAIN LOCK (or group parent) TAKEN !
3175 * Goes down the chains/groups until it finds the chain
3176 * to which the drainpad belongs.
3178 * It marks that pad/chain as drained and then will figure
3179 * out which group to switch to or not.
3181 * last_chain will be set to TRUE if the group to which the
3182 * pad belongs is the last one.
3184 * drained will be set to TRUE if the chain/group is drained.
3186 * Returns: TRUE if the chain contained the target pad */
3188 drain_and_switch_group (GstDecodeGroup * group, GstDecodePad * drainpad,
3189 gboolean * last_group, gboolean * drained, gboolean * switched)
3191 gboolean handled = FALSE;
3192 gboolean alldrained = TRUE;
3195 GST_DEBUG ("Checking group %p (target pad %s:%s)",
3196 group, GST_DEBUG_PAD_NAME (drainpad));
3198 /* Definitely can't be in drained groups */
3199 if (G_UNLIKELY (group->drained)) {
3203 /* Figure out if all our chains are drained with the
3204 * new information */
3205 for (tmp = group->children; tmp; tmp = tmp->next) {
3206 GstDecodeChain *chain = (GstDecodeChain *) tmp->data;
3207 gboolean subdrained = FALSE;
3210 drain_and_switch_chains (chain, drainpad, last_group, &subdrained,
3217 GST_DEBUG ("group %p (last_group:%d, drained:%d, switched:%d, handled:%d)",
3218 group, *last_group, alldrained, *switched, handled);
3219 *drained = alldrained;
3224 drain_and_switch_chains (GstDecodeChain * chain, GstDecodePad * drainpad,
3225 gboolean * last_group, gboolean * drained, gboolean * switched)
3227 gboolean handled = FALSE;
3228 GstDecodeBin *dbin = chain->dbin;
3230 GST_DEBUG ("Checking chain %p (target pad %s:%s)",
3231 chain, GST_DEBUG_PAD_NAME (drainpad));
3233 CHAIN_MUTEX_LOCK (chain);
3235 if (chain->endpad) {
3236 /* Check if we're reached the target endchain */
3237 if (chain == drainpad->chain) {
3238 GST_DEBUG ("Found the target chain");
3239 drainpad->drained = TRUE;
3243 *drained = chain->endpad->drained;
3247 /* We known there are groups to switch to */
3248 if (chain->next_groups)
3249 *last_group = FALSE;
3251 /* Check the active group */
3252 if (chain->active_group) {
3253 gboolean subdrained = FALSE;
3254 handled = drain_and_switch_group (chain->active_group, drainpad,
3255 last_group, &subdrained, switched);
3257 /* The group is drained, see if we can switch to another */
3258 if (handled && subdrained && !*switched) {
3259 if (chain->next_groups) {
3260 /* Switch to next group */
3261 GST_DEBUG_OBJECT (dbin, "Hiding current group %p", chain->active_group);
3262 gst_decode_group_hide (chain->active_group);
3264 g_list_prepend (chain->old_groups, chain->active_group);
3265 GST_DEBUG_OBJECT (dbin, "Switching to next group %p",
3266 chain->next_groups->data);
3267 chain->active_group = chain->next_groups->data;
3268 chain->next_groups =
3269 g_list_delete_link (chain->next_groups, chain->next_groups);
3273 GST_DEBUG ("Group %p was the last in chain %p", chain->active_group,
3276 /* We're drained ! */
3282 CHAIN_MUTEX_UNLOCK (chain);
3284 GST_DEBUG ("Chain %p (handled:%d, last_group:%d, drained:%d, switched:%d)",
3285 chain, handled, *last_group, *drained, *switched);
3288 g_signal_emit (dbin, gst_decode_bin_signals[SIGNAL_DRAINED], 0, NULL);
3293 /* check if the group is drained, meaning all pads have seen an EOS
3296 gst_decode_pad_handle_eos (GstDecodePad * pad)
3298 gboolean last_group = TRUE;
3299 gboolean switched = FALSE;
3300 gboolean drained = FALSE;
3301 GstDecodeChain *chain = pad->chain;
3302 GstDecodeBin *dbin = chain->dbin;
3304 GST_LOG_OBJECT (dbin, "pad %p", pad);
3305 drain_and_switch_chains (dbin->decode_chain, pad, &last_group, &drained,
3309 /* If we resulted in a group switch, expose what's needed */
3311 if (gst_decode_chain_is_complete (dbin->decode_chain))
3312 gst_decode_bin_expose (dbin);
3313 EXPOSE_UNLOCK (dbin);
3319 /* gst_decode_group_is_drained:
3321 * Check is this group is drained and cache this result.
3322 * The group is drained if all child chains are drained.
3324 * Not MT-safe, call with group->parent's lock */
3326 gst_decode_group_is_drained (GstDecodeGroup * group)
3329 gboolean drained = TRUE;
3331 if (group->drained) {
3336 for (l = group->children; l; l = l->next) {
3337 GstDecodeChain *chain = l->data;
3339 CHAIN_MUTEX_LOCK (chain);
3340 if (!gst_decode_chain_is_drained (chain))
3342 CHAIN_MUTEX_UNLOCK (chain);
3346 group->drained = drained;
3349 GST_DEBUG_OBJECT (group->dbin, "Group %p is drained: %d", group, drained);
3353 /* gst_decode_chain_is_drained:
3355 * Check is the chain is drained, which means that
3358 * a) it's endpad is drained
3359 * b) there are no pending pads, the active group is drained
3360 * and there are no next groups
3362 * Not MT-safe, call with chain lock
3365 gst_decode_chain_is_drained (GstDecodeChain * chain)
3367 gboolean drained = FALSE;
3369 if (chain->endpad) {
3370 drained = chain->endpad->drained;
3374 if (chain->pending_pads) {
3379 if (chain->active_group && gst_decode_group_is_drained (chain->active_group)
3380 && !chain->next_groups) {
3386 GST_DEBUG_OBJECT (chain->dbin, "Chain %p is drained: %d", chain, drained);
3391 * GCompareFunc to use with lists of GstPad.
3392 * Sorts pads by mime type.
3393 * First video (raw, then non-raw), then audio (raw, then non-raw),
3396 * Return: negative if a<b, 0 if a==b, positive if a>b
3399 sort_end_pads (GstDecodePad * da, GstDecodePad * db)
3402 GstCaps *capsa, *capsb;
3403 GstStructure *sa, *sb;
3404 const gchar *namea, *nameb;
3406 capsa = get_pad_caps (GST_PAD_CAST (da));
3407 capsb = get_pad_caps (GST_PAD_CAST (db));
3409 sa = gst_caps_get_structure ((const GstCaps *) capsa, 0);
3410 sb = gst_caps_get_structure ((const GstCaps *) capsb, 0);
3412 namea = gst_structure_get_name (sa);
3413 nameb = gst_structure_get_name (sb);
3415 if (g_strrstr (namea, "video/x-raw"))
3417 else if (g_strrstr (namea, "video/"))
3419 else if (g_strrstr (namea, "audio/x-raw"))
3421 else if (g_strrstr (namea, "audio/"))
3426 if (g_strrstr (nameb, "video/x-raw"))
3428 else if (g_strrstr (nameb, "video/"))
3430 else if (g_strrstr (nameb, "audio/x-raw"))
3432 else if (g_strrstr (nameb, "audio/"))
3437 gst_caps_unref (capsa);
3438 gst_caps_unref (capsb);
3444 _gst_element_get_linked_caps (GstElement * src, GstElement * sink,
3450 gboolean done = FALSE;
3451 GstCaps *caps = NULL;
3452 GValue item = { 0, };
3454 it = gst_element_iterate_src_pads (src);
3456 switch (gst_iterator_next (it, &item)) {
3457 case GST_ITERATOR_OK:
3458 pad = g_value_get_object (&item);
3459 peer = gst_pad_get_peer (pad);
3461 parent = gst_pad_get_parent_element (peer);
3462 if (parent == sink) {
3463 caps = gst_pad_get_current_caps (pad);
3465 gst_object_ref (pad);
3472 gst_object_unref (parent);
3473 gst_object_unref (peer);
3475 g_value_reset (&item);
3477 case GST_ITERATOR_RESYNC:
3478 gst_iterator_resync (it);
3480 case GST_ITERATOR_ERROR:
3481 case GST_ITERATOR_DONE:
3486 g_value_unset (&item);
3487 gst_iterator_free (it);
3492 static GQuark topology_structure_name = 0;
3493 static GQuark topology_caps = 0;
3494 static GQuark topology_next = 0;
3495 static GQuark topology_pad = 0;
3496 static GQuark topology_element_srcpad = 0;
3498 /* FIXME: Invent gst_structure_take_structure() to prevent all the
3499 * structure copying for nothing
3501 static GstStructure *
3502 gst_decode_chain_get_topology (GstDecodeChain * chain)
3504 GstStructure *s, *u;
3508 if (G_UNLIKELY ((chain->endpad || chain->deadend)
3509 && (chain->endcaps == NULL))) {
3510 GST_WARNING ("End chain without valid caps !");
3514 u = gst_structure_new_id_empty (topology_structure_name);
3516 /* Now at the last element */
3517 if ((chain->elements || !chain->active_group) &&
3518 (chain->endpad || chain->deadend)) {
3519 s = gst_structure_new_id_empty (topology_structure_name);
3520 gst_structure_id_set (u, topology_caps, GST_TYPE_CAPS, chain->endcaps,
3523 if (chain->endpad) {
3524 gst_structure_id_set (u, topology_pad, GST_TYPE_PAD, chain->endpad, NULL);
3525 gst_structure_id_set (u, topology_element_srcpad, GST_TYPE_PAD,
3526 chain->endpad, NULL);
3528 gst_structure_id_set (s, topology_next, GST_TYPE_STRUCTURE, u, NULL);
3529 gst_structure_free (u);
3531 } else if (chain->active_group) {
3532 GValue list = { 0, };
3533 GValue item = { 0, };
3535 g_value_init (&list, GST_TYPE_LIST);
3536 g_value_init (&item, GST_TYPE_STRUCTURE);
3537 for (l = chain->active_group->children; l; l = l->next) {
3538 s = gst_decode_chain_get_topology (l->data);
3540 gst_value_set_structure (&item, s);
3541 gst_value_list_append_value (&list, &item);
3542 g_value_reset (&item);
3543 gst_structure_free (s);
3546 gst_structure_id_set_value (u, topology_next, &list);
3547 g_value_unset (&list);
3548 g_value_unset (&item);
3551 /* Get caps between all elements in this chain */
3552 l = (chain->elements && chain->elements->next) ? chain->elements : NULL;
3553 for (; l && l->next; l = l->next) {
3554 GstDecodeElement *delem, *delem_next;
3555 GstElement *elem, *elem_next;
3560 elem = delem->element;
3561 delem_next = l->next->data;
3562 elem_next = delem_next->element;
3565 caps = _gst_element_get_linked_caps (elem_next, elem, &srcpad);
3568 s = gst_structure_new_id_empty (topology_structure_name);
3569 gst_structure_id_set (u, topology_caps, GST_TYPE_CAPS, caps, NULL);
3570 gst_caps_unref (caps);
3572 gst_structure_id_set (s, topology_next, GST_TYPE_STRUCTURE, u, NULL);
3573 gst_structure_free (u);
3578 gst_structure_id_set (u, topology_element_srcpad, GST_TYPE_PAD, srcpad,
3580 gst_object_unref (srcpad);
3584 /* Caps that resulted in this chain */
3585 caps = gst_pad_get_current_caps (chain->pad);
3587 caps = get_pad_caps (chain->pad);
3588 if (G_UNLIKELY (!gst_caps_is_fixed (caps))) {
3589 GST_ERROR_OBJECT (chain->pad,
3590 "Couldn't get fixed caps, got %" GST_PTR_FORMAT, caps);
3591 gst_caps_unref (caps);
3595 gst_structure_id_set (u, topology_caps, GST_TYPE_CAPS, caps, NULL);
3596 gst_structure_id_set (u, topology_element_srcpad, GST_TYPE_PAD, chain->pad,
3598 gst_caps_unref (caps);
3604 gst_decode_bin_post_topology_message (GstDecodeBin * dbin)
3609 s = gst_decode_chain_get_topology (dbin->decode_chain);
3611 msg = gst_message_new_element (GST_OBJECT (dbin), s);
3612 gst_element_post_message (GST_ELEMENT (dbin), msg);
3615 /* Must only be called if the toplevel chain is complete and blocked! */
3616 /* Not MT-safe, call with decodebin expose lock! */
3618 gst_decode_bin_expose (GstDecodeBin * dbin)
3620 GList *tmp, *endpads = NULL;
3621 gboolean missing_plugin = FALSE;
3622 gboolean already_exposed = TRUE;
3624 GST_DEBUG_OBJECT (dbin, "Exposing currently active chains/groups");
3626 /* Don't expose if we're currently shutting down */
3628 if (G_UNLIKELY (dbin->shutdown == TRUE)) {
3629 GST_WARNING_OBJECT (dbin, "Currently, shutting down, aborting exposing");
3635 /* Get the pads that we're going to expose and mark things as exposed */
3636 if (!gst_decode_chain_expose (dbin->decode_chain, &endpads, &missing_plugin)) {
3637 g_list_foreach (endpads, (GFunc) gst_object_unref, NULL);
3638 g_list_free (endpads);
3639 GST_ERROR_OBJECT (dbin, "Broken chain/group tree");
3640 g_return_val_if_reached (FALSE);
3643 if (endpads == NULL) {
3644 if (missing_plugin) {
3645 GST_WARNING_OBJECT (dbin, "No suitable plugins found");
3646 GST_ELEMENT_ERROR (dbin, CORE, MISSING_PLUGIN, (NULL),
3647 ("no suitable plugins found"));
3649 /* in this case, the stream ended without buffers,
3650 * just post a warning */
3651 GST_WARNING_OBJECT (dbin, "All streams finished without buffers");
3652 GST_ELEMENT_ERROR (dbin, STREAM, FAILED, (NULL),
3653 ("all streams without buffers"));
3658 /* Check if this was called when everything was exposed already */
3659 for (tmp = endpads; tmp && already_exposed; tmp = tmp->next) {
3660 GstDecodePad *dpad = tmp->data;
3662 already_exposed &= dpad->exposed;
3663 if (!already_exposed)
3666 if (already_exposed) {
3667 GST_DEBUG_OBJECT (dbin, "Everything was exposed already!");
3668 g_list_foreach (endpads, (GFunc) gst_object_unref, NULL);
3669 g_list_free (endpads);
3673 /* Set all already exposed pads to blocked */
3674 for (tmp = endpads; tmp; tmp = tmp->next) {
3675 GstDecodePad *dpad = tmp->data;
3677 if (dpad->exposed) {
3678 GST_DEBUG_OBJECT (dpad, "blocking exposed pad");
3679 gst_decode_pad_set_blocked (dpad, TRUE);
3683 /* re-order pads : video, then audio, then others */
3684 endpads = g_list_sort (endpads, (GCompareFunc) sort_end_pads);
3687 for (tmp = endpads; tmp; tmp = tmp->next) {
3688 GstDecodePad *dpad = (GstDecodePad *) tmp->data;
3691 /* 1. rewrite name */
3692 padname = g_strdup_printf ("src_%u", dbin->nbpads);
3694 GST_DEBUG_OBJECT (dbin, "About to expose dpad %s as %s",
3695 GST_OBJECT_NAME (dpad), padname);
3696 gst_object_set_name (GST_OBJECT (dpad), padname);
3699 /* 2. activate and add */
3701 && !gst_element_add_pad (GST_ELEMENT (dbin), GST_PAD_CAST (dpad))) {
3702 /* not really fatal, we can try to add the other pads */
3703 g_warning ("error adding pad to decodebin");
3706 dpad->exposed = TRUE;
3708 /* 3. emit signal */
3709 GST_INFO_OBJECT (dpad, "added new decoded pad");
3712 /* 4. Signal no-more-pads. This allows the application to hook stuff to the
3714 GST_LOG_OBJECT (dbin, "signaling no-more-pads");
3715 gst_element_no_more_pads (GST_ELEMENT (dbin));
3717 /* 5. Send a custom element message with the stream topology */
3718 if (dbin->post_stream_topology)
3719 gst_decode_bin_post_topology_message (dbin);
3721 /* 6. Unblock internal pads. The application should have connected stuff now
3722 * so that streaming can continue. */
3723 for (tmp = endpads; tmp; tmp = tmp->next) {
3724 GstDecodePad *dpad = (GstDecodePad *) tmp->data;
3726 GST_DEBUG_OBJECT (dpad, "unblocking");
3727 gst_decode_pad_unblock (dpad);
3728 GST_DEBUG_OBJECT (dpad, "unblocked");
3729 gst_object_unref (dpad);
3731 g_list_free (endpads);
3733 do_async_done (dbin);
3734 GST_DEBUG_OBJECT (dbin, "Exposed everything");
3738 /* gst_decode_chain_expose:
3740 * Check if the chain can be exposed and add all endpads
3741 * to the endpads list.
3743 * Also update the active group's multiqueue to the
3746 * Not MT-safe, call with decodebin expose lock! *
3749 gst_decode_chain_expose (GstDecodeChain * chain, GList ** endpads,
3750 gboolean * missing_plugin)
3752 GstDecodeGroup *group;
3756 if (chain->deadend) {
3758 *missing_plugin = TRUE;
3762 if (chain->endpad) {
3763 if (!chain->endpad->blocked && !chain->endpad->exposed)
3765 *endpads = g_list_prepend (*endpads, gst_object_ref (chain->endpad));
3769 group = chain->active_group;
3772 if (!group->no_more_pads && !group->overrun)
3777 /* configure queues for playback */
3778 decodebin_set_queue_size (dbin, group->multiqueue, FALSE, TRUE);
3780 /* we can now disconnect any overrun signal, which is used to expose the
3782 if (group->overrunsig) {
3783 GST_LOG_OBJECT (dbin, "Disconnecting overrun");
3784 g_signal_handler_disconnect (group->multiqueue, group->overrunsig);
3785 group->overrunsig = 0;
3788 for (l = group->children; l; l = l->next) {
3789 GstDecodeChain *childchain = l->data;
3791 if (!gst_decode_chain_expose (childchain, endpads, missing_plugin))
3798 /*************************
3799 * GstDecodePad functions
3800 *************************/
3803 gst_decode_pad_class_init (GstDecodePadClass * klass)
3808 gst_decode_pad_init (GstDecodePad * pad)
3811 pad->blocked = FALSE;
3812 pad->exposed = FALSE;
3813 pad->drained = FALSE;
3814 gst_object_ref_sink (pad);
3817 static GstPadProbeReturn
3818 source_pad_blocked_cb (GstPad * pad, GstPadProbeInfo * info, gpointer user_data)
3820 GstDecodePad *dpad = user_data;
3821 GstDecodeChain *chain;
3824 if ((GST_PAD_PROBE_INFO_TYPE (info) & GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM) &&
3825 (GST_EVENT_IS_STICKY (GST_PAD_PROBE_INFO_EVENT (info))
3826 || !GST_EVENT_IS_SERIALIZED (GST_PAD_PROBE_INFO_EVENT (info)))) {
3827 /* do not block on sticky or out of band events otherwise the allocation query
3828 from demuxer might block the loop thread */
3829 return GST_PAD_PROBE_PASS;
3832 chain = dpad->chain;
3835 GST_LOG_OBJECT (dpad, "blocked: dpad->chain:%p", chain);
3837 dpad->blocked = TRUE;
3840 if (gst_decode_chain_is_complete (dbin->decode_chain)) {
3841 if (!gst_decode_bin_expose (dbin))
3842 GST_WARNING_OBJECT (dbin, "Couldn't expose group");
3844 EXPOSE_UNLOCK (dbin);
3846 return GST_PAD_PROBE_OK;
3849 static GstPadProbeReturn
3850 source_pad_event_probe (GstPad * pad, GstPadProbeInfo * info,
3853 GstEvent *event = GST_PAD_PROBE_INFO_EVENT (info);
3854 GstDecodePad *dpad = user_data;
3855 gboolean res = TRUE;
3857 GST_LOG_OBJECT (pad, "%s dpad:%p", GST_EVENT_TYPE_NAME (event), dpad);
3859 if (GST_EVENT_TYPE (event) == GST_EVENT_EOS) {
3860 GST_DEBUG_OBJECT (pad, "we received EOS");
3862 /* Check if all pads are drained.
3863 * * If there is no next group, we will let the EOS go through.
3864 * * If there is a next group but the current group isn't completely
3865 * drained, we will drop the EOS event.
3866 * * If there is a next group to expose and this was the last non-drained
3867 * pad for that group, we will remove the ghostpad of the current group
3868 * first, which unlinks the peer and so drops the EOS. */
3869 res = gst_decode_pad_handle_eos (dpad);
3872 return GST_PAD_PROBE_OK;
3874 return GST_PAD_PROBE_DROP;
3878 gst_decode_pad_set_blocked (GstDecodePad * dpad, gboolean blocked)
3880 GstDecodeBin *dbin = dpad->dbin;
3885 GST_DEBUG_OBJECT (dpad, "blocking pad: %d", blocked);
3887 opad = gst_ghost_pad_get_target (GST_GHOST_PAD_CAST (dpad));
3891 /* do not block if shutting down.
3892 * we do not consider/expect it blocked further below, but use other trick */
3893 if (!blocked || !dbin->shutdown) {
3895 if (dpad->block_id == 0)
3897 gst_pad_add_probe (opad, GST_PAD_PROBE_TYPE_BLOCK_DOWNSTREAM,
3898 source_pad_blocked_cb, gst_object_ref (dpad),
3899 (GDestroyNotify) gst_object_unref);
3901 if (dpad->block_id != 0) {
3902 gst_pad_remove_probe (opad, dpad->block_id);
3905 dpad->blocked = FALSE;
3910 if (dbin->shutdown) {
3911 /* deactivate to force flushing state to prevent NOT_LINKED errors */
3912 gst_pad_set_active (GST_PAD_CAST (dpad), FALSE);
3913 /* note that deactivating the target pad would have no effect here,
3914 * since elements are typically connected first (and pads exposed),
3915 * and only then brought to PAUSED state (so pads activated) */
3917 gst_object_ref (dpad);
3918 dbin->blocked_pads = g_list_prepend (dbin->blocked_pads, dpad);
3923 if ((l = g_list_find (dbin->blocked_pads, dpad))) {
3924 gst_object_unref (dpad);
3925 dbin->blocked_pads = g_list_delete_link (dbin->blocked_pads, l);
3928 gst_object_unref (opad);
3934 gst_decode_pad_add_drained_check (GstDecodePad * dpad)
3936 gst_pad_add_probe (GST_PAD_CAST (dpad), GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM,
3937 source_pad_event_probe, dpad, NULL);
3941 gst_decode_pad_activate (GstDecodePad * dpad, GstDecodeChain * chain)
3943 g_return_if_fail (chain != NULL);
3945 dpad->chain = chain;
3946 gst_pad_set_active (GST_PAD_CAST (dpad), TRUE);
3947 gst_decode_pad_set_blocked (dpad, TRUE);
3948 gst_decode_pad_add_drained_check (dpad);
3952 gst_decode_pad_unblock (GstDecodePad * dpad)
3954 gst_decode_pad_set_blocked (dpad, FALSE);
3957 /*gst_decode_pad_new:
3959 * Creates a new GstDecodePad for the given pad.
3961 static GstDecodePad *
3962 gst_decode_pad_new (GstDecodeBin * dbin, GstPad * pad, GstDecodeChain * chain)
3965 GstPadTemplate *pad_tmpl;
3967 GST_DEBUG_OBJECT (dbin, "making new decodepad");
3968 pad_tmpl = gst_static_pad_template_get (&decoder_bin_src_template);
3970 g_object_new (GST_TYPE_DECODE_PAD, "direction", GST_PAD_DIRECTION (pad),
3971 "template", pad_tmpl, NULL);
3972 gst_ghost_pad_construct (GST_GHOST_PAD_CAST (dpad));
3973 gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (dpad), pad);
3974 dpad->chain = chain;
3976 gst_object_unref (pad_tmpl);
3982 gst_pending_pad_free (GstPendingPad * ppad)
3985 g_assert (ppad->pad);
3987 if (ppad->event_probe_id != 0)
3988 gst_pad_remove_probe (ppad->pad, ppad->event_probe_id);
3989 gst_object_unref (ppad->pad);
3990 g_slice_free (GstPendingPad, ppad);
3994 * Element add/remove
3998 do_async_start (GstDecodeBin * dbin)
4000 GstMessage *message;
4002 dbin->async_pending = TRUE;
4004 message = gst_message_new_async_start (GST_OBJECT_CAST (dbin));
4005 parent_class->handle_message (GST_BIN_CAST (dbin), message);
4009 do_async_done (GstDecodeBin * dbin)
4011 GstMessage *message;
4013 if (dbin->async_pending) {
4014 message = gst_message_new_async_done (GST_OBJECT_CAST (dbin), FALSE);
4015 parent_class->handle_message (GST_BIN_CAST (dbin), message);
4017 dbin->async_pending = FALSE;
4022 * convenience functions
4027 * Returns the first sink pad of the given element, or NULL if it doesn't have
4032 find_sink_pad (GstElement * element)
4036 GValue item = { 0, };
4038 it = gst_element_iterate_sink_pads (element);
4040 if ((gst_iterator_next (it, &item)) == GST_ITERATOR_OK)
4041 pad = g_value_dup_object (&item);
4042 g_value_unset (&item);
4043 gst_iterator_free (it);
4048 /* call with dyn_lock held */
4050 unblock_pads (GstDecodeBin * dbin)
4054 GST_LOG_OBJECT (dbin, "unblocking pads");
4056 for (tmp = dbin->blocked_pads; tmp; tmp = tmp->next) {
4057 GstDecodePad *dpad = (GstDecodePad *) tmp->data;
4060 opad = gst_ghost_pad_get_target (GST_GHOST_PAD_CAST (dpad));
4064 GST_DEBUG_OBJECT (dpad, "unblocking");
4065 if (dpad->block_id != 0) {
4066 gst_pad_remove_probe (opad, dpad->block_id);
4069 dpad->blocked = FALSE;
4070 /* make flushing, prevent NOT_LINKED */
4071 GST_PAD_SET_FLUSHING (GST_PAD_CAST (dpad));
4072 gst_object_unref (dpad);
4073 gst_object_unref (opad);
4074 GST_DEBUG_OBJECT (dpad, "unblocked");
4077 /* clear, no more blocked pads */
4078 g_list_free (dbin->blocked_pads);
4079 dbin->blocked_pads = NULL;
4082 static GstStateChangeReturn
4083 gst_decode_bin_change_state (GstElement * element, GstStateChange transition)
4085 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
4086 GstDecodeBin *dbin = GST_DECODE_BIN (element);
4088 switch (transition) {
4089 case GST_STATE_CHANGE_NULL_TO_READY:
4090 if (dbin->typefind == NULL)
4091 goto missing_typefind;
4093 case GST_STATE_CHANGE_READY_TO_PAUSED:
4094 /* Make sure we've cleared all existing chains */
4095 if (dbin->decode_chain) {
4096 gst_decode_chain_free (dbin->decode_chain);
4097 dbin->decode_chain = NULL;
4100 GST_LOG_OBJECT (dbin, "clearing shutdown flag");
4101 dbin->shutdown = FALSE;
4103 dbin->have_type = FALSE;
4104 ret = GST_STATE_CHANGE_ASYNC;
4105 do_async_start (dbin);
4107 case GST_STATE_CHANGE_PAUSED_TO_READY:
4109 GST_LOG_OBJECT (dbin, "setting shutdown flag");
4110 dbin->shutdown = TRUE;
4111 unblock_pads (dbin);
4118 GstStateChangeReturn bret;
4120 bret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
4121 if (G_UNLIKELY (bret == GST_STATE_CHANGE_FAILURE))
4122 goto activate_failed;
4123 else if (G_UNLIKELY (bret == GST_STATE_CHANGE_NO_PREROLL)) {
4124 do_async_done (dbin);
4128 switch (transition) {
4129 case GST_STATE_CHANGE_PAUSED_TO_READY:
4130 do_async_done (dbin);
4131 if (dbin->decode_chain) {
4132 gst_decode_chain_free (dbin->decode_chain);
4133 dbin->decode_chain = NULL;
4136 case GST_STATE_CHANGE_READY_TO_NULL:
4146 gst_element_post_message (element,
4147 gst_missing_element_message_new (element, "typefind"));
4148 GST_ELEMENT_ERROR (dbin, CORE, MISSING_PLUGIN, (NULL), ("no typefind!"));
4149 return GST_STATE_CHANGE_FAILURE;
4153 GST_DEBUG_OBJECT (element,
4154 "element failed to change states -- activation problem?");
4155 return GST_STATE_CHANGE_FAILURE;
4160 gst_decode_bin_handle_message (GstBin * bin, GstMessage * msg)
4162 GstDecodeBin *dbin = GST_DECODE_BIN (bin);
4163 gboolean drop = FALSE;
4165 if (GST_MESSAGE_TYPE (msg) == GST_MESSAGE_ERROR) {
4166 GST_OBJECT_LOCK (dbin);
4167 drop = (g_list_find (dbin->filtered, GST_MESSAGE_SRC (msg)) != NULL);
4168 GST_OBJECT_UNLOCK (dbin);
4172 gst_message_unref (msg);
4174 GST_BIN_CLASS (parent_class)->handle_message (bin, msg);
4178 gst_decode_bin_plugin_init (GstPlugin * plugin)
4180 GST_DEBUG_CATEGORY_INIT (gst_decode_bin_debug, "decodebin", 0, "decoder bin");
4182 /* Register some quarks here for the stream topology message */
4183 topology_structure_name = g_quark_from_static_string ("stream-topology");
4184 topology_caps = g_quark_from_static_string ("caps");
4185 topology_next = g_quark_from_static_string ("next");
4186 topology_pad = g_quark_from_static_string ("pad");
4187 topology_element_srcpad = g_quark_from_static_string ("element-srcpad");
4189 return gst_element_register (plugin, "decodebin", GST_RANK_NONE,
4190 GST_TYPE_DECODE_BIN);