2 * Copyright (C) <2006> Edward Hervey <edward@fluendo.com>
3 * Copyright (C) <2009> Sebastian Dröge <sebastian.droege@collabora.co.uk>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
22 * SECTION:element-decodebin2
24 * #GstBin that auto-magically constructs a decoding pipeline using available
25 * decoders and demuxers via auto-plugging.
27 * At this stage, decodebin2 is considered UNSTABLE. The API provided in the
28 * signals is expected to change in the near future.
30 * To try out decodebin2, you can set the USE_DECODEBIN2 environment
31 * variable (USE_DECODEBIN2=1 for example). This will cause playbin to use
32 * decodebin2 instead of the older #GstDecodeBin for its internal auto-plugging.
35 /* Implementation notes:
37 * The following section describes how decodebin2 works internally.
39 * The first part of decodebin2 is it's typefind element, which tries
40 * to get the type of the input stream. If the type is found autoplugging starts.
42 * decodebin2 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 decodebin2 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 OTOH is complete if all child chains are complete.
61 * If this happens at some point, all endpads of all active groups are exposed.
62 * For this decodebin2 adds the endpads, signals no-more-pads and then unblocks
63 * them. Now playback starts.
65 * If one of the chains that end on a endpad receives EOS decodebin2 checks upwards
66 * via the parent pointers if all chains and groups are drained. In that case
67 * everything goes into EOS.
68 * If there is a chain where the active group is drained but there exist next groups
69 * the active group is hidden (endpads are removed) and the next group is exposed.
71 * Note 1: If we're talking about blocked endpads this really means that the
72 * *target* pads of the endpads are blocked. Pads that are exposed to the outside
73 * should never ever be blocked!
75 * Note 2: If a group is complete and the parent's chain demuxer adds new pads
76 * but never signaled no-more-pads this additional pads will be ignored!
84 #include <gst/gst-i18n-plugin.h>
88 #include <gst/pbutils/pbutils.h>
90 #include "gstplay-marshal.h"
91 #include "gstplay-enum.h"
92 #include "gstfactorylists.h"
94 /* generic templates */
95 static GstStaticPadTemplate decoder_bin_sink_template =
96 GST_STATIC_PAD_TEMPLATE ("sink",
101 static GstStaticPadTemplate decoder_bin_src_template =
102 GST_STATIC_PAD_TEMPLATE ("src%d",
105 GST_STATIC_CAPS_ANY);
107 GST_DEBUG_CATEGORY_STATIC (gst_decode_bin_debug);
108 #define GST_CAT_DEFAULT gst_decode_bin_debug
110 typedef struct _GstDecodeChain GstDecodeChain;
111 typedef struct _GstDecodeGroup GstDecodeGroup;
112 typedef struct _GstDecodePad GstDecodePad;
113 typedef GstGhostPadClass GstDecodePadClass;
114 typedef struct _GstDecodeBin GstDecodeBin;
115 typedef struct _GstDecodeBin GstDecodeBin2;
116 typedef struct _GstDecodeBinClass GstDecodeBinClass;
118 #define GST_TYPE_DECODE_BIN (gst_decode_bin_get_type())
119 #define GST_DECODE_BIN_CAST(obj) ((GstDecodeBin*)(obj))
120 #define GST_DECODE_BIN(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_DECODE_BIN,GstDecodeBin))
121 #define GST_DECODE_BIN_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_DECODE_BIN,GstDecodeBinClass))
122 #define GST_IS_DECODE_BIN(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_DECODE_BIN))
123 #define GST_IS_DECODE_BIN_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_DECODE_BIN))
128 * The opaque #DecodeBin2 data structure
132 GstBin bin; /* we extend GstBin */
135 GstCaps *caps; /* caps on which to stop decoding */
136 gchar *encoding; /* encoding of subtitles */
138 GstElement *typefind; /* this holds the typefind object */
140 GMutex *expose_lock; /* Protects exposal and removal of groups */
141 GstDecodeChain *decode_chain; /* Top level decode chain */
142 gint nbpads; /* unique identifier for source pads */
144 GValueArray *factories; /* factories we can use for selecting elements */
146 GList *subtitles; /* List of elements with subtitle-encoding,
147 * protected by object lock! */
149 gboolean have_type; /* if we received the have_type signal */
150 guint have_type_id; /* signal id for have-type from typefind */
152 gboolean async_pending; /* async-start has been emited */
154 GMutex *dyn_lock; /* lock protecting pad blocking */
155 gboolean shutdown; /* if we are shutting down */
156 GList *blocked_pads; /* pads that have set to block */
159 struct _GstDecodeBinClass
161 GstBinClass parent_class;
163 /* signal we fire when a new pad has been decoded into raw audio/video */
164 void (*new_decoded_pad) (GstElement * element, GstPad * pad, gboolean last);
165 /* signal we fire when a pad has been removed */
166 void (*removed_decoded_pad) (GstElement * element, GstPad * pad);
167 /* signal fired when we found a pad that we cannot decode */
168 void (*unknown_type) (GstElement * element, GstPad * pad, GstCaps * caps);
170 /* signal fired to know if we continue trying to decode the given caps */
171 gboolean (*autoplug_continue) (GstElement * element, GstPad * pad,
173 /* signal fired to get a list of factories to try to autoplug */
174 GValueArray *(*autoplug_factories) (GstElement * element, GstPad * pad,
176 /* signal fired to sort the factories */
177 GValueArray *(*autoplug_sort) (GstElement * element, GstPad * pad,
178 GstCaps * caps, GValueArray * factories);
179 /* signal fired to select from the proposed list of factories */
180 GstAutoplugSelectResult (*autoplug_select) (GstElement * element,
181 GstPad * pad, GstCaps * caps, GstElementFactory * factory);
183 /* fired when the last group is drained */
184 void (*drained) (GstElement * element);
190 SIGNAL_NEW_DECODED_PAD,
191 SIGNAL_REMOVED_DECODED_PAD,
193 SIGNAL_AUTOPLUG_CONTINUE,
194 SIGNAL_AUTOPLUG_FACTORIES,
195 SIGNAL_AUTOPLUG_SELECT,
196 SIGNAL_AUTOPLUG_SORT,
206 PROP_SUBTITLE_ENCODING,
211 static GstBinClass *parent_class;
212 static guint gst_decode_bin_signals[LAST_SIGNAL] = { 0 };
214 static const GstElementDetails gst_decode_bin_details =
215 GST_ELEMENT_DETAILS ("Decoder Bin",
216 "Generic/Bin/Decoder",
217 "Autoplug and decode to raw media",
218 "Edward Hervey <edward.hervey@collabora.co.uk>, "
219 "Sebastian Dröge <sebastian.droege@collabora.co.uk>");
221 static void do_async_start (GstDecodeBin * dbin);
222 static void do_async_done (GstDecodeBin * dbin);
224 static void type_found (GstElement * typefind, guint probability,
225 GstCaps * caps, GstDecodeBin * decode_bin);
227 static gboolean gst_decode_bin_autoplug_continue (GstElement * element,
228 GstPad * pad, GstCaps * caps);
229 static GValueArray *gst_decode_bin_autoplug_factories (GstElement *
230 element, GstPad * pad, GstCaps * caps);
231 static GValueArray *gst_decode_bin_autoplug_sort (GstElement * element,
232 GstPad * pad, GstCaps * caps, GValueArray * factories);
233 static GstAutoplugSelectResult gst_decode_bin_autoplug_select (GstElement *
234 element, GstPad * pad, GstCaps * caps, GstElementFactory * factory);
236 static void gst_decode_bin_set_property (GObject * object, guint prop_id,
237 const GValue * value, GParamSpec * pspec);
238 static void gst_decode_bin_get_property (GObject * object, guint prop_id,
239 GValue * value, GParamSpec * pspec);
240 static void gst_decode_bin_set_caps (GstDecodeBin * dbin, GstCaps * caps);
241 static GstCaps *gst_decode_bin_get_caps (GstDecodeBin * dbin);
242 static void caps_notify_cb (GstPad * pad, GParamSpec * unused,
243 GstDecodeChain * chain);
245 static GstPad *find_sink_pad (GstElement * element);
246 static GstStateChangeReturn gst_decode_bin_change_state (GstElement * element,
247 GstStateChange transition);
249 #define EXPOSE_LOCK(dbin) G_STMT_START { \
250 GST_LOG_OBJECT (dbin, \
251 "expose locking from thread %p", \
253 g_mutex_lock (GST_DECODE_BIN_CAST(dbin)->expose_lock); \
254 GST_LOG_OBJECT (dbin, \
255 "expose locked from thread %p", \
259 #define EXPOSE_UNLOCK(dbin) G_STMT_START { \
260 GST_LOG_OBJECT (dbin, \
261 "expose unlocking from thread %p", \
263 g_mutex_unlock (GST_DECODE_BIN_CAST(dbin)->expose_lock); \
266 #define DYN_LOCK(dbin) G_STMT_START { \
267 GST_LOG_OBJECT (dbin, \
268 "dynlocking from thread %p", \
270 g_mutex_lock (GST_DECODE_BIN_CAST(dbin)->dyn_lock); \
271 GST_LOG_OBJECT (dbin, \
272 "dynlocked from thread %p", \
276 #define DYN_UNLOCK(dbin) G_STMT_START { \
277 GST_LOG_OBJECT (dbin, \
278 "dynunlocking from thread %p", \
280 g_mutex_unlock (GST_DECODE_BIN_CAST(dbin)->dyn_lock); \
285 * Streams belonging to the same group/chain of a media file
287 * When changing something here lock the parent chain!
289 struct _GstDecodeGroup
292 GstDecodeChain *parent;
294 GstElement *multiqueue; /* Used for linking all child chains */
295 gulong overrunsig; /* the overrun signal for multiqueue */
297 gboolean overrun; /* TRUE if the multiqueue signaled overrun. This
298 * means that we should really expose the group */
300 gboolean no_more_pads; /* TRUE if the demuxer signaled no-more-pads */
301 gboolean drained; /* TRUE if the all children are drained */
303 GList *children; /* List of GstDecodeChains in this group */
305 GList *reqpads; /* List of RequestPads for multiqueue, there is
306 * exactly one RequestPad per child chain */
309 struct _GstDecodeChain
311 GstDecodeGroup *parent;
314 GMutex *lock; /* Protects this chain and its groups */
316 gboolean demuxer; /* TRUE if elements->data is a demuxer */
317 GList *elements; /* All elements in this group, first
318 is the latest and most downstream element */
320 /* Note: there are only groups if the last element of this chain
321 * is a demuxer, otherwise the chain will end with an endpad.
322 * The other way around this means, that endpad only exists if this
323 * chain doesn't end with a demuxer! */
325 GstDecodeGroup *active_group; /* Currently active group */
326 GList *next_groups; /* head is newest group, tail is next group.
327 a new group will be created only if the head
328 group had no-more-pads. If it's only exposed
329 all new pads will be ignored! */
330 GList *pending_pads; /* Pads that have no fixed caps yet */
332 GstDecodePad *endpad; /* Pad of this chain that could be exposed */
333 gboolean deadend; /* This chain is incomplete and can't be completed,
334 e.g. no suitable decoder could be found
337 /* FIXME: This should be done directly via a thread! */
338 GList *old_groups; /* Groups that should be freed later */
341 static void gst_decode_chain_free (GstDecodeChain * chain);
342 static GstDecodeChain *gst_decode_chain_new (GstDecodeBin * dbin,
343 GstDecodeGroup * group);
344 static void gst_decode_group_hide (GstDecodeGroup * group);
345 static void gst_decode_group_free (GstDecodeGroup * group);
346 static GstDecodeGroup *gst_decode_group_new (GstDecodeBin * dbin,
347 GstDecodeChain * chain);
348 static gboolean gst_decode_chain_is_complete (GstDecodeChain * chain);
349 static void gst_decode_chain_handle_eos (GstDecodeChain * chain);
350 static gboolean gst_decode_chain_expose (GstDecodeChain * chain,
352 static gboolean gst_decode_chain_is_drained (GstDecodeChain * chain);
353 static gboolean gst_decode_group_is_complete (GstDecodeGroup * group);
354 static GstPad *gst_decode_group_control_demuxer_pad (GstDecodeGroup * group,
356 static gboolean gst_decode_group_is_drained (GstDecodeGroup * group);
358 static gboolean gst_decode_bin_expose (GstDecodeBin * dbin);
360 #define CHAIN_MUTEX_LOCK(chain) G_STMT_START { \
361 GST_LOG_OBJECT (chain->dbin, \
362 "locking chain %p from thread %p", \
363 chain, g_thread_self ()); \
364 g_mutex_lock (chain->lock); \
365 GST_LOG_OBJECT (chain->dbin, \
366 "locked chain %p from thread %p", \
367 chain, g_thread_self ()); \
370 #define CHAIN_MUTEX_UNLOCK(chain) G_STMT_START { \
371 GST_LOG_OBJECT (chain->dbin, \
372 "unlocking chain %p from thread %p", \
373 chain, g_thread_self ()); \
374 g_mutex_unlock (chain->lock); \
379 * GstPad private used for source pads of chains
385 GstDecodeChain *chain;
387 gboolean blocked; /* the *target* pad is blocked */
388 gboolean exposed; /* the pad is exposed */
389 gboolean drained; /* an EOS has been seen on the pad */
392 G_DEFINE_TYPE (GstDecodePad, gst_decode_pad, GST_TYPE_GHOST_PAD);
393 #define GST_TYPE_DECODE_PAD (gst_decode_pad_get_type ())
394 #define GST_DECODE_PAD(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_DECODE_PAD,GstDecodePad))
396 static GstDecodePad *gst_decode_pad_new (GstDecodeBin * dbin, GstPad * pad,
397 GstDecodeChain * chain);
398 static void gst_decode_pad_activate (GstDecodePad * dpad,
399 GstDecodeChain * chain);
400 static void gst_decode_pad_unblock (GstDecodePad * dpad);
401 static void gst_decode_pad_set_blocked (GstDecodePad * dpad, gboolean blocked);
403 /********************************
404 * Standard GObject boilerplate *
405 ********************************/
407 static void gst_decode_bin_class_init (GstDecodeBinClass * klass);
408 static void gst_decode_bin_init (GstDecodeBin * decode_bin);
409 static void gst_decode_bin_dispose (GObject * object);
410 static void gst_decode_bin_finalize (GObject * object);
413 gst_decode_bin_get_type (void)
415 static GType gst_decode_bin_type = 0;
417 if (!gst_decode_bin_type) {
418 static const GTypeInfo gst_decode_bin_info = {
419 sizeof (GstDecodeBinClass),
422 (GClassInitFunc) gst_decode_bin_class_init,
425 sizeof (GstDecodeBin),
427 (GInstanceInitFunc) gst_decode_bin_init,
431 gst_decode_bin_type =
432 g_type_register_static (GST_TYPE_BIN, "GstDecodeBin2",
433 &gst_decode_bin_info, 0);
436 return gst_decode_bin_type;
440 _gst_boolean_accumulator (GSignalInvocationHint * ihint,
441 GValue * return_accu, const GValue * handler_return, gpointer dummy)
445 myboolean = g_value_get_boolean (handler_return);
446 if (!(ihint->run_type & G_SIGNAL_RUN_CLEANUP))
447 g_value_set_boolean (return_accu, myboolean);
449 /* stop emission if FALSE */
453 /* we collect the first result */
455 _gst_array_accumulator (GSignalInvocationHint * ihint,
456 GValue * return_accu, const GValue * handler_return, gpointer dummy)
460 array = g_value_get_boxed (handler_return);
461 if (!(ihint->run_type & G_SIGNAL_RUN_CLEANUP))
462 g_value_set_boxed (return_accu, array);
468 _gst_select_accumulator (GSignalInvocationHint * ihint,
469 GValue * return_accu, const GValue * handler_return, gpointer dummy)
471 GstAutoplugSelectResult res;
473 res = g_value_get_enum (handler_return);
474 if (!(ihint->run_type & G_SIGNAL_RUN_CLEANUP))
475 g_value_set_enum (return_accu, res);
481 gst_decode_bin_class_init (GstDecodeBinClass * klass)
483 GObjectClass *gobject_klass;
484 GstElementClass *gstelement_klass;
486 gobject_klass = (GObjectClass *) klass;
487 gstelement_klass = (GstElementClass *) klass;
489 parent_class = g_type_class_peek_parent (klass);
491 gobject_klass->dispose = GST_DEBUG_FUNCPTR (gst_decode_bin_dispose);
492 gobject_klass->finalize = GST_DEBUG_FUNCPTR (gst_decode_bin_finalize);
493 gobject_klass->set_property = GST_DEBUG_FUNCPTR (gst_decode_bin_set_property);
494 gobject_klass->get_property = GST_DEBUG_FUNCPTR (gst_decode_bin_get_property);
497 * GstDecodeBin2::new-decoded-pad:
498 * @bin: The decodebin
499 * @pad: The newly created pad
500 * @islast: #TRUE if this is the last pad to be added. Deprecated.
502 * This signal gets emitted as soon as a new pad of the same type as one of
503 * the valid 'raw' types is added.
505 gst_decode_bin_signals[SIGNAL_NEW_DECODED_PAD] =
506 g_signal_new ("new-decoded-pad", G_TYPE_FROM_CLASS (klass),
508 G_STRUCT_OFFSET (GstDecodeBinClass, new_decoded_pad), NULL, NULL,
509 gst_play_marshal_VOID__OBJECT_BOOLEAN, G_TYPE_NONE, 2, GST_TYPE_PAD,
513 * GstDecodeBin2::removed-decoded-pad:
514 * @bin: The decodebin
515 * @pad: The pad that was removed
517 * This signal is emitted when a 'final' caps pad has been removed.
519 gst_decode_bin_signals[SIGNAL_REMOVED_DECODED_PAD] =
520 g_signal_new ("removed-decoded-pad", G_TYPE_FROM_CLASS (klass),
522 G_STRUCT_OFFSET (GstDecodeBinClass, removed_decoded_pad), NULL, NULL,
523 gst_marshal_VOID__OBJECT, G_TYPE_NONE, 1, GST_TYPE_PAD);
526 * GstDecodeBin2::unknown-type:
527 * @bin: The decodebin
528 * @pad: The new pad containing caps that cannot be resolved to a 'final'
530 * @caps: The #GstCaps of the pad that cannot be resolved.
532 * This signal is emitted when a pad for which there is no further possible
533 * decoding is added to the decodebin.
535 gst_decode_bin_signals[SIGNAL_UNKNOWN_TYPE] =
536 g_signal_new ("unknown-type", G_TYPE_FROM_CLASS (klass),
537 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstDecodeBinClass, unknown_type),
538 NULL, NULL, gst_marshal_VOID__OBJECT_BOXED, G_TYPE_NONE, 2,
539 GST_TYPE_PAD, GST_TYPE_CAPS);
542 * GstDecodeBin2::autoplug-continue:
543 * @bin: The decodebin
545 * @caps: The #GstCaps found.
547 * This signal is emitted whenever decodebin2 finds a new stream. It is
548 * emitted before looking for any elements that can handle that stream.
550 * Returns: #TRUE if you wish decodebin2 to look for elements that can
551 * handle the given @caps. If #FALSE, those caps will be considered as
552 * final and the pad will be exposed as such (see 'new-decoded-pad'
555 gst_decode_bin_signals[SIGNAL_AUTOPLUG_CONTINUE] =
556 g_signal_new ("autoplug-continue", G_TYPE_FROM_CLASS (klass),
557 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstDecodeBinClass, autoplug_continue),
558 _gst_boolean_accumulator, NULL, gst_play_marshal_BOOLEAN__OBJECT_BOXED,
559 G_TYPE_BOOLEAN, 2, GST_TYPE_PAD, GST_TYPE_CAPS);
562 * GstDecodeBin2::autoplug-factories:
563 * @bin: The decodebin
565 * @caps: The #GstCaps found.
567 * This function is emited when an array of possible factories for @caps on
568 * @pad is needed. Decodebin2 will by default return an array with all
569 * compatible factories, sorted by rank.
571 * If this function returns NULL, @pad will be exposed as a final caps.
573 * If this function returns an empty array, the pad will be considered as
574 * having an unhandled type media type.
576 * Returns: a #GValueArray* with a list of factories to try. The factories are
577 * by default tried in the returned order or based on the index returned by
580 gst_decode_bin_signals[SIGNAL_AUTOPLUG_FACTORIES] =
581 g_signal_new ("autoplug-factories", G_TYPE_FROM_CLASS (klass),
582 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstDecodeBinClass,
583 autoplug_factories), _gst_array_accumulator, NULL,
584 gst_play_marshal_BOXED__OBJECT_BOXED, G_TYPE_VALUE_ARRAY, 2,
585 GST_TYPE_PAD, GST_TYPE_CAPS);
588 * GstDecodeBin2::autoplug-sort:
589 * @bin: The decodebin
591 * @caps: The #GstCaps.
592 * @factories: A #GValueArray of possible #GstElementFactory to use.
594 * Once decodebin2 has found the possible #GstElementFactory objects to try
595 * for @caps on @pad, this signal is emited. The purpose of the signal is for
596 * the application to perform additional sorting or filtering on the element
599 * The callee should copy and modify @factories.
601 * Returns: A new sorted array of #GstElementFactory objects.
603 gst_decode_bin_signals[SIGNAL_AUTOPLUG_SORT] =
604 g_signal_new ("autoplug-sort", G_TYPE_FROM_CLASS (klass),
605 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstDecodeBinClass, autoplug_sort),
606 NULL, NULL, gst_play_marshal_BOXED__OBJECT_BOXED_BOXED,
607 G_TYPE_VALUE_ARRAY, 3, GST_TYPE_PAD, GST_TYPE_CAPS, G_TYPE_VALUE_ARRAY);
610 * GstDecodeBin2::autoplug-select:
611 * @bin: The decodebin
613 * @caps: The #GstCaps.
614 * @factory: A #GstElementFactory to use
616 * This signal is emitted once decodebin2 has found all the possible
617 * #GstElementFactory that can be used to handle the given @caps. For each of
618 * those factories, this signal is emited.
620 * The signal handler should return a #GST_TYPE_AUTOPLUG_SELECT_RESULT enum
621 * value indicating what decodebin2 should do next.
623 * A value of #GST_AUTOPLUG_SELECT_TRY will try to autoplug an element from
626 * A value of #GST_AUTOPLUG_SELECT_EXPOSE will expose @pad without plugging
629 * A value of #GST_AUTOPLUG_SELECT_SKIP will skip @factory and move to the
632 * Returns: a #GST_TYPE_AUTOPLUG_SELECT_RESULT that indicates the required
633 * operation. the default handler will always return
634 * #GST_AUTOPLUG_SELECT_TRY.
636 gst_decode_bin_signals[SIGNAL_AUTOPLUG_SELECT] =
637 g_signal_new ("autoplug-select", G_TYPE_FROM_CLASS (klass),
638 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstDecodeBinClass, autoplug_select),
639 _gst_select_accumulator, NULL,
640 gst_play_marshal_ENUM__OBJECT_BOXED_OBJECT,
641 GST_TYPE_AUTOPLUG_SELECT_RESULT, 3, GST_TYPE_PAD, GST_TYPE_CAPS,
642 GST_TYPE_ELEMENT_FACTORY);
645 * GstDecodeBin2::drained
646 * @bin: The decodebin
648 * This signal is emitted once decodebin2 has finished decoding all the data.
652 gst_decode_bin_signals[SIGNAL_DRAINED] =
653 g_signal_new ("drained", G_TYPE_FROM_CLASS (klass),
654 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstDecodeBinClass, drained),
655 NULL, NULL, gst_marshal_VOID__VOID, G_TYPE_NONE, 0, G_TYPE_NONE);
657 g_object_class_install_property (gobject_klass, PROP_CAPS,
658 g_param_spec_boxed ("caps", "Caps", "The caps on which to stop decoding.",
659 GST_TYPE_CAPS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
661 g_object_class_install_property (gobject_klass, PROP_SUBTITLE_ENCODING,
662 g_param_spec_string ("subtitle-encoding", "subtitle encoding",
663 "Encoding to assume if input subtitles are not in UTF-8 encoding. "
664 "If not set, the GST_SUBTITLE_ENCODING environment variable will "
665 "be checked for an encoding to use. If that is not set either, "
666 "ISO-8859-15 will be assumed.", NULL,
667 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
669 g_object_class_install_property (gobject_klass, PROP_SINK_CAPS,
670 g_param_spec_boxed ("sink-caps", "Sink Caps",
671 "The caps of the input data. (NULL = use typefind element)",
672 GST_TYPE_CAPS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
674 klass->autoplug_continue =
675 GST_DEBUG_FUNCPTR (gst_decode_bin_autoplug_continue);
676 klass->autoplug_factories =
677 GST_DEBUG_FUNCPTR (gst_decode_bin_autoplug_factories);
678 klass->autoplug_sort = GST_DEBUG_FUNCPTR (gst_decode_bin_autoplug_sort);
679 klass->autoplug_select = GST_DEBUG_FUNCPTR (gst_decode_bin_autoplug_select);
681 gst_element_class_add_pad_template (gstelement_klass,
682 gst_static_pad_template_get (&decoder_bin_sink_template));
683 gst_element_class_add_pad_template (gstelement_klass,
684 gst_static_pad_template_get (&decoder_bin_src_template));
686 gst_element_class_set_details (gstelement_klass, &gst_decode_bin_details);
688 gstelement_klass->change_state =
689 GST_DEBUG_FUNCPTR (gst_decode_bin_change_state);
693 gst_decode_bin_init (GstDecodeBin * decode_bin)
695 /* first filter out the interesting element factories */
696 decode_bin->factories =
697 gst_factory_list_get_elements (GST_FACTORY_LIST_DECODER);
699 /* we create the typefind element only once */
700 decode_bin->typefind = gst_element_factory_make ("typefind", "typefind");
701 if (!decode_bin->typefind) {
702 g_warning ("can't find typefind element, decodebin will not work");
707 /* add the typefind element */
708 if (!gst_bin_add (GST_BIN (decode_bin), decode_bin->typefind)) {
709 g_warning ("Could not add typefind element, decodebin will not work");
710 gst_object_unref (decode_bin->typefind);
711 decode_bin->typefind = NULL;
714 /* get the sinkpad */
715 pad = gst_element_get_static_pad (decode_bin->typefind, "sink");
717 /* ghost the sink pad to ourself */
718 gpad = gst_ghost_pad_new ("sink", pad);
719 gst_pad_set_active (gpad, TRUE);
720 gst_element_add_pad (GST_ELEMENT (decode_bin), gpad);
722 gst_object_unref (pad);
724 /* connect a signal to find out when the typefind element found
726 decode_bin->have_type_id =
727 g_signal_connect (G_OBJECT (decode_bin->typefind), "have-type",
728 G_CALLBACK (type_found), decode_bin);
731 decode_bin->expose_lock = g_mutex_new ();
732 decode_bin->decode_chain = NULL;
734 decode_bin->dyn_lock = g_mutex_new ();
735 decode_bin->shutdown = FALSE;
736 decode_bin->blocked_pads = NULL;
739 gst_caps_from_string ("video/x-raw-yuv;video/x-raw-rgb;video/x-raw-gray;"
740 "audio/x-raw-int;audio/x-raw-float;" "text/plain;text/x-pango-markup;"
741 "video/x-dvd-subpicture; subpicture/x-pgs");
745 gst_decode_bin_dispose (GObject * object)
747 GstDecodeBin *decode_bin;
749 decode_bin = GST_DECODE_BIN (object);
751 if (decode_bin->factories)
752 g_value_array_free (decode_bin->factories);
753 decode_bin->factories = NULL;
755 if (decode_bin->decode_chain)
756 gst_decode_chain_free (decode_bin->decode_chain);
757 decode_bin->decode_chain = NULL;
759 if (decode_bin->caps)
760 gst_caps_unref (decode_bin->caps);
761 decode_bin->caps = NULL;
763 g_free (decode_bin->encoding);
764 decode_bin->encoding = NULL;
766 g_list_free (decode_bin->subtitles);
767 decode_bin->subtitles = NULL;
769 G_OBJECT_CLASS (parent_class)->dispose (object);
773 gst_decode_bin_finalize (GObject * object)
775 GstDecodeBin *decode_bin;
777 decode_bin = GST_DECODE_BIN (object);
779 if (decode_bin->expose_lock) {
780 g_mutex_free (decode_bin->expose_lock);
781 decode_bin->expose_lock = NULL;
784 if (decode_bin->dyn_lock) {
785 g_mutex_free (decode_bin->dyn_lock);
786 decode_bin->dyn_lock = NULL;
789 G_OBJECT_CLASS (parent_class)->finalize (object);
793 * Changes the caps on which decodebin will stop decoding.
794 * Will unref the previously set one. The refcount of the given caps will be
801 gst_decode_bin_set_caps (GstDecodeBin * dbin, GstCaps * caps)
803 GST_DEBUG_OBJECT (dbin, "Setting new caps: %" GST_PTR_FORMAT, caps);
805 GST_OBJECT_LOCK (dbin);
806 gst_caps_replace (&dbin->caps, caps);
807 GST_OBJECT_UNLOCK (dbin);
811 * Returns the currently configured caps on which decodebin will stop decoding.
812 * The returned caps (if not NULL), will have its refcount incremented.
817 gst_decode_bin_get_caps (GstDecodeBin * dbin)
821 GST_DEBUG_OBJECT (dbin, "Getting currently set caps");
823 GST_OBJECT_LOCK (dbin);
827 GST_OBJECT_UNLOCK (dbin);
833 gst_decode_bin_set_sink_caps (GstDecodeBin * dbin, GstCaps * caps)
835 GST_DEBUG_OBJECT (dbin, "Setting new caps: %" GST_PTR_FORMAT, caps);
837 g_object_set (dbin->typefind, "force-caps", caps, NULL);
841 gst_decode_bin_get_sink_caps (GstDecodeBin * dbin)
845 GST_DEBUG_OBJECT (dbin, "Getting currently set caps");
847 g_object_get (dbin->typefind, "force-caps", &caps, NULL);
853 gst_decode_bin_set_subs_encoding (GstDecodeBin * dbin, const gchar * encoding)
857 GST_DEBUG_OBJECT (dbin, "Setting new encoding: %s", GST_STR_NULL (encoding));
859 GST_OBJECT_LOCK (dbin);
860 g_free (dbin->encoding);
861 dbin->encoding = g_strdup (encoding);
863 /* set the subtitle encoding on all added elements */
864 for (walk = dbin->subtitles; walk; walk = g_list_next (walk)) {
865 g_object_set (G_OBJECT (walk->data), "subtitle-encoding", dbin->encoding,
868 GST_OBJECT_UNLOCK (dbin);
872 gst_decode_bin_get_subs_encoding (GstDecodeBin * dbin)
876 GST_DEBUG_OBJECT (dbin, "Getting currently set encoding");
878 GST_OBJECT_LOCK (dbin);
879 encoding = g_strdup (dbin->encoding);
880 GST_OBJECT_UNLOCK (dbin);
886 gst_decode_bin_set_property (GObject * object, guint prop_id,
887 const GValue * value, GParamSpec * pspec)
891 dbin = GST_DECODE_BIN (object);
895 gst_decode_bin_set_caps (dbin, g_value_get_boxed (value));
897 case PROP_SUBTITLE_ENCODING:
898 gst_decode_bin_set_subs_encoding (dbin, g_value_get_string (value));
901 gst_decode_bin_set_sink_caps (dbin, g_value_get_boxed (value));
904 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
910 gst_decode_bin_get_property (GObject * object, guint prop_id,
911 GValue * value, GParamSpec * pspec)
915 dbin = GST_DECODE_BIN (object);
918 g_value_take_boxed (value, gst_decode_bin_get_caps (dbin));
920 case PROP_SUBTITLE_ENCODING:
921 g_value_take_string (value, gst_decode_bin_get_subs_encoding (dbin));
924 g_value_take_boxed (value, gst_decode_bin_get_sink_caps (dbin));
927 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
934 * Default autoplug signal handlers
937 gst_decode_bin_autoplug_continue (GstElement * element, GstPad * pad,
940 GST_DEBUG_OBJECT (element, "autoplug-continue returns TRUE");
942 /* by default we always continue */
947 gst_decode_bin_autoplug_factories (GstElement * element, GstPad * pad,
952 GST_DEBUG_OBJECT (element, "finding factories");
954 /* return all compatible factories for caps */
956 gst_factory_list_filter (GST_DECODE_BIN_CAST (element)->factories, caps);
958 GST_DEBUG_OBJECT (element, "autoplug-factories returns %p", result);
964 gst_decode_bin_autoplug_sort (GstElement * element, GstPad * pad,
965 GstCaps * caps, GValueArray * factories)
969 result = g_value_array_copy (factories);
971 GST_DEBUG_OBJECT (element, "autoplug-sort returns %p", result);
977 static GstAutoplugSelectResult
978 gst_decode_bin_autoplug_select (GstElement * element, GstPad * pad,
979 GstCaps * caps, GstElementFactory * factory)
981 GST_DEBUG_OBJECT (element, "default autoplug-select returns TRY");
984 return GST_AUTOPLUG_SELECT_TRY;
991 static gboolean are_raw_caps (GstDecodeBin * dbin, GstCaps * caps);
992 static gboolean is_demuxer_element (GstElement * srcelement);
994 static gboolean connect_pad (GstDecodeBin * dbin, GstElement * src,
995 GstDecodePad * dpad, GstPad * pad, GstCaps * caps, GValueArray * factories,
996 GstDecodeChain * chain);
997 static gboolean connect_element (GstDecodeBin * dbin, GstElement * element,
998 GstDecodeChain * chain);
999 static void expose_pad (GstDecodeBin * dbin, GstElement * src,
1000 GstDecodePad * dpad, GstPad * pad, GstDecodeChain * chain);
1002 static void pad_added_cb (GstElement * element, GstPad * pad,
1003 GstDecodeChain * chain);
1004 static void pad_removed_cb (GstElement * element, GstPad * pad,
1005 GstDecodeChain * chain);
1006 static void no_more_pads_cb (GstElement * element, GstDecodeChain * chain);
1008 static GstDecodeGroup *gst_decode_chain_get_current_group (GstDecodeChain *
1011 /* called when a new pad is discovered. It will perform some basic actions
1012 * before trying to link something to it.
1014 * - Check the caps, don't do anything when there are no caps or when they have
1016 * - signal AUTOPLUG_CONTINUE to check if we need to continue autoplugging this
1018 * - if the caps are non-fixed, setup a handler to continue autoplugging when
1019 * the caps become fixed (connect to notify::caps).
1020 * - get list of factories to autoplug.
1021 * - continue autoplugging to one of the factories.
1024 analyze_new_pad (GstDecodeBin * dbin, GstElement * src, GstPad * pad,
1025 GstCaps * caps, GstDecodeChain * chain)
1027 gboolean apcontinue = TRUE;
1028 GValueArray *factories = NULL, *result = NULL;
1031 GST_DEBUG_OBJECT (dbin, "Pad %s:%s caps:%" GST_PTR_FORMAT,
1032 GST_DEBUG_PAD_NAME (pad), caps);
1034 if (chain->elements && src != chain->elements->data) {
1035 GST_ERROR_OBJECT (dbin, "New pad from not the last element in this chain");
1039 if (chain->endpad) {
1040 GST_ERROR_OBJECT (dbin, "New pad in a chain that is already complete");
1044 if (chain->demuxer) {
1045 GstDecodeGroup *group;
1046 GstDecodeChain *oldchain = chain;
1048 CHAIN_MUTEX_LOCK (oldchain);
1049 group = gst_decode_chain_get_current_group (chain);
1051 chain = gst_decode_chain_new (dbin, group);
1052 group->children = g_list_prepend (group->children, chain);
1054 CHAIN_MUTEX_UNLOCK (oldchain);
1056 GST_WARNING_OBJECT (dbin, "No current group");
1061 if ((caps == NULL) || gst_caps_is_empty (caps))
1064 if (gst_caps_is_any (caps))
1067 dpad = gst_decode_pad_new (dbin, pad, chain);
1069 /* 1. Emit 'autoplug-continue' the result will tell us if this pads needs
1070 * further autoplugging. */
1071 g_signal_emit (G_OBJECT (dbin),
1072 gst_decode_bin_signals[SIGNAL_AUTOPLUG_CONTINUE], 0, dpad, caps,
1075 /* 1.a if autoplug-continue is FALSE or caps is a raw format, goto pad_is_final */
1076 if ((!apcontinue) || are_raw_caps (dbin, caps))
1079 /* 1.b when the caps are not fixed yet, we can't be sure what element to
1080 * connect. We delay autoplugging until the caps are fixed */
1081 if (!gst_caps_is_fixed (caps))
1084 /* 1.c else get the factories and if there's no compatible factory goto
1086 g_signal_emit (G_OBJECT (dbin),
1087 gst_decode_bin_signals[SIGNAL_AUTOPLUG_FACTORIES], 0, dpad, caps,
1090 /* NULL means that we can expose the pad */
1091 if (factories == NULL)
1094 /* if the array is empty, we have an unknown type */
1095 if (factories->n_values == 0) {
1096 /* no compatible factories */
1097 g_value_array_free (factories);
1098 gst_object_unref (dpad);
1102 /* 1.d sort some more. */
1103 g_signal_emit (G_OBJECT (dbin),
1104 gst_decode_bin_signals[SIGNAL_AUTOPLUG_SORT], 0, dpad, caps, factories,
1106 g_value_array_free (factories);
1109 /* 1.e else continue autoplugging something from the list. */
1110 GST_LOG_OBJECT (pad, "Let's continue discovery on this pad");
1111 connect_pad (dbin, src, dpad, pad, caps, factories, chain);
1113 gst_object_unref (dpad);
1114 g_value_array_free (factories);
1120 GST_LOG_OBJECT (dbin, "Pad is final. autoplug-continue:%d", apcontinue);
1121 expose_pad (dbin, src, dpad, pad, chain);
1122 gst_object_unref (dpad);
1127 GST_LOG_OBJECT (pad, "Unknown type, firing signal");
1128 g_signal_emit (G_OBJECT (dbin),
1129 gst_decode_bin_signals[SIGNAL_UNKNOWN_TYPE], 0, pad, caps);
1131 chain->deadend = TRUE;
1133 gst_element_post_message (GST_ELEMENT_CAST (dbin),
1134 gst_missing_decoder_message_new (GST_ELEMENT_CAST (dbin), caps));
1136 /* Try to expose anything */
1138 if (gst_decode_chain_is_complete (dbin->decode_chain)) {
1139 gst_decode_bin_expose (dbin);
1141 EXPOSE_UNLOCK (dbin);
1142 do_async_done (dbin);
1144 if (src == dbin->typefind) {
1147 if (caps && !gst_caps_is_empty (caps)) {
1148 desc = gst_pb_utils_get_decoder_description (caps);
1149 GST_ELEMENT_ERROR (dbin, STREAM, CODEC_NOT_FOUND,
1150 (_("A %s plugin is required to play this stream, "
1151 "but not installed."), desc),
1152 ("No decoder to handle media type '%s'",
1153 gst_structure_get_name (gst_caps_get_structure (caps, 0))));
1156 GST_ELEMENT_ERROR (dbin, STREAM, TYPE_NOT_FOUND,
1157 (_("Could not determine type of stream")),
1158 ("Stream caps %" GST_PTR_FORMAT, caps));
1165 GST_DEBUG_OBJECT (pad, "pad has non-fixed caps delay autoplugging");
1166 gst_object_unref (dpad);
1167 goto setup_caps_delay;
1171 GST_WARNING_OBJECT (pad,
1172 "pad has ANY caps, not able to autoplug to anything");
1173 goto setup_caps_delay;
1177 /* connect to caps notification */
1178 CHAIN_MUTEX_LOCK (chain);
1179 GST_LOG_OBJECT (dbin, "Chain %p has now %d dynamic pads", chain,
1180 g_list_length (chain->pending_pads));
1181 chain->pending_pads =
1182 g_list_prepend (chain->pending_pads, gst_object_ref (pad));
1183 CHAIN_MUTEX_UNLOCK (chain);
1184 g_signal_connect (G_OBJECT (pad), "notify::caps",
1185 G_CALLBACK (caps_notify_cb), chain);
1193 * Try to connect the given pad to an element created from one of the factories,
1196 * Note that dpad is ghosting pad, and so pad is linked; be sure to unset dpad's
1197 * target before trying to link pad.
1199 * Returns TRUE if an element was properly created and linked
1202 connect_pad (GstDecodeBin * dbin, GstElement * src, GstDecodePad * dpad,
1203 GstPad * pad, GstCaps * caps, GValueArray * factories,
1204 GstDecodeChain * chain)
1206 gboolean res = FALSE;
1207 GstPad *mqpad = NULL;
1208 gboolean is_demuxer = chain->parent && !chain->elements; /* First pad after the demuxer */
1210 g_return_val_if_fail (factories != NULL, FALSE);
1211 g_return_val_if_fail (factories->n_values > 0, FALSE);
1213 GST_DEBUG_OBJECT (dbin, "pad %s:%s , chain:%p",
1214 GST_DEBUG_PAD_NAME (pad), chain);
1216 /* 1. is element demuxer or parser */
1218 GST_LOG_OBJECT (src,
1219 "is a demuxer, connecting the pad through multiqueue '%s'",
1220 GST_OBJECT_NAME (chain->parent->multiqueue));
1222 gst_ghost_pad_set_target (GST_GHOST_PAD (dpad), NULL);
1223 if (!(mqpad = gst_decode_group_control_demuxer_pad (chain->parent, pad)))
1225 src = chain->parent->multiqueue;
1227 gst_ghost_pad_set_target (GST_GHOST_PAD (dpad), pad);
1230 /* 2. Try to create an element and link to it */
1231 while (factories->n_values > 0) {
1232 GstAutoplugSelectResult ret;
1233 GstElementFactory *factory;
1234 GstElement *element;
1238 /* Set dpad target to pad again, it might've been unset
1239 * below but we came back here because something failed
1241 gst_ghost_pad_set_target (GST_GHOST_PAD (dpad), pad);
1243 /* take first factory */
1244 factory = g_value_get_object (g_value_array_get_nth (factories, 0));
1245 /* Remove selected factory from the list. */
1246 g_value_array_remove (factories, 0);
1248 /* emit autoplug-select to see what we should do with it. */
1249 g_signal_emit (G_OBJECT (dbin),
1250 gst_decode_bin_signals[SIGNAL_AUTOPLUG_SELECT],
1251 0, dpad, caps, factory, &ret);
1254 case GST_AUTOPLUG_SELECT_TRY:
1255 GST_DEBUG_OBJECT (dbin, "autoplug select requested try");
1257 case GST_AUTOPLUG_SELECT_EXPOSE:
1258 GST_DEBUG_OBJECT (dbin, "autoplug select requested expose");
1259 /* expose the pad, we don't have the source element */
1260 expose_pad (dbin, src, dpad, pad, chain);
1263 case GST_AUTOPLUG_SELECT_SKIP:
1264 GST_DEBUG_OBJECT (dbin, "autoplug select requested skip");
1267 GST_WARNING_OBJECT (dbin, "autoplug select returned unhandled %d", ret);
1271 /* 2.0. Unlink pad */
1272 gst_ghost_pad_set_target (GST_GHOST_PAD (dpad), NULL);
1274 /* 2.1. Try to create an element */
1275 if ((element = gst_element_factory_create (factory, NULL)) == NULL) {
1276 GST_WARNING_OBJECT (dbin, "Could not create an element from %s",
1277 gst_plugin_feature_get_name (GST_PLUGIN_FEATURE (factory)));
1281 /* ... activate it ... We do this before adding it to the bin so that we
1282 * don't accidentally make it post error messages that will stop
1284 if ((gst_element_set_state (element,
1285 GST_STATE_READY)) == GST_STATE_CHANGE_FAILURE) {
1286 GST_WARNING_OBJECT (dbin, "Couldn't set %s to READY",
1287 GST_ELEMENT_NAME (element));
1288 gst_object_unref (element);
1292 /* 2.3. Find its sink pad, this should work after activating it. */
1293 if (!(sinkpad = find_sink_pad (element))) {
1294 GST_WARNING_OBJECT (dbin, "Element %s doesn't have a sink pad",
1295 GST_ELEMENT_NAME (element));
1296 gst_element_set_state (element, GST_STATE_NULL);
1297 gst_object_unref (element);
1301 /* 2.4 add it ... */
1302 if (!(gst_bin_add (GST_BIN_CAST (dbin), element))) {
1303 GST_WARNING_OBJECT (dbin, "Couldn't add %s to the bin",
1304 GST_ELEMENT_NAME (element));
1305 gst_object_unref (sinkpad);
1306 gst_element_set_state (element, GST_STATE_NULL);
1307 gst_object_unref (element);
1311 /* 2.5 ...and try to link */
1312 if ((gst_pad_link (pad, sinkpad)) != GST_PAD_LINK_OK) {
1313 GST_WARNING_OBJECT (dbin, "Link failed on pad %s:%s",
1314 GST_DEBUG_PAD_NAME (sinkpad));
1315 gst_element_set_state (element, GST_STATE_NULL);
1316 gst_object_unref (sinkpad);
1317 gst_bin_remove (GST_BIN (dbin), element);
1320 gst_object_unref (sinkpad);
1321 GST_LOG_OBJECT (dbin, "linked on pad %s:%s", GST_DEBUG_PAD_NAME (pad));
1323 CHAIN_MUTEX_LOCK (chain);
1325 g_list_prepend (chain->elements, gst_object_ref (element));
1326 chain->demuxer = is_demuxer_element (element);
1327 CHAIN_MUTEX_UNLOCK (chain);
1329 /* link this element further */
1330 connect_element (dbin, element, chain);
1332 /* try to configure the subtitle encoding property when we can */
1333 if (g_object_class_find_property (G_OBJECT_GET_CLASS (element),
1334 "subtitle-encoding")) {
1335 GST_DEBUG_OBJECT (dbin,
1336 "setting subtitle-encoding=%s to element", dbin->encoding);
1337 g_object_set (G_OBJECT (element), "subtitle-encoding", dbin->encoding,
1343 /* Bring the element to the state of the parent */
1344 if ((gst_element_set_state (element,
1345 GST_STATE_PAUSED)) == GST_STATE_CHANGE_FAILURE) {
1346 GstElement *tmp = NULL;
1348 GST_WARNING_OBJECT (dbin, "Couldn't set %s to PAUSED",
1349 GST_ELEMENT_NAME (element));
1351 /* Remove all elements in this chain that were just added. No
1352 * other thread could've added elements in the meantime */
1353 CHAIN_MUTEX_LOCK (chain);
1355 tmp = chain->elements->data;
1356 gst_element_set_state (tmp, GST_STATE_NULL);
1357 gst_bin_remove (GST_BIN (dbin), tmp);
1358 chain->elements = g_list_delete_link (chain->elements, chain->elements);
1359 } while (tmp != element);
1360 CHAIN_MUTEX_UNLOCK (chain);
1365 GST_OBJECT_LOCK (dbin);
1366 /* we added the element now, add it to the list of subtitle-encoding
1367 * elements when we can set the property */
1368 dbin->subtitles = g_list_prepend (dbin->subtitles, element);
1369 GST_OBJECT_UNLOCK (dbin);
1378 gst_object_unref (mqpad);
1384 connect_element (GstDecodeBin * dbin, GstElement * element,
1385 GstDecodeChain * chain)
1388 gboolean res = TRUE;
1389 gboolean dynamic = FALSE;
1390 GList *to_connect = NULL;
1392 GST_DEBUG_OBJECT (dbin, "Attempting to connect element %s [chain:%p] further",
1393 GST_ELEMENT_NAME (element), chain);
1395 /* 1. Loop over pad templates, grabbing existing pads along the way */
1396 for (pads = GST_ELEMENT_GET_CLASS (element)->padtemplates; pads;
1397 pads = g_list_next (pads)) {
1398 GstPadTemplate *templ = GST_PAD_TEMPLATE (pads->data);
1399 const gchar *templ_name;
1401 /* we are only interested in source pads */
1402 if (GST_PAD_TEMPLATE_DIRECTION (templ) != GST_PAD_SRC)
1405 templ_name = GST_PAD_TEMPLATE_NAME_TEMPLATE (templ);
1406 GST_DEBUG_OBJECT (dbin, "got a source pad template %s", templ_name);
1408 /* figure out what kind of pad this is */
1409 switch (GST_PAD_TEMPLATE_PRESENCE (templ)) {
1410 case GST_PAD_ALWAYS:
1412 /* get the pad that we need to autoplug */
1413 GstPad *pad = gst_element_get_static_pad (element, templ_name);
1416 GST_DEBUG_OBJECT (dbin, "got the pad for always template %s",
1418 /* here is the pad, we need to autoplug it */
1419 to_connect = g_list_prepend (to_connect, pad);
1421 /* strange, pad is marked as always but it's not
1422 * there. Fix the element */
1423 GST_WARNING_OBJECT (dbin,
1424 "could not get the pad for always template %s", templ_name);
1428 case GST_PAD_SOMETIMES:
1430 /* try to get the pad to see if it is already created or
1432 GstPad *pad = gst_element_get_static_pad (element, templ_name);
1435 GST_DEBUG_OBJECT (dbin, "got the pad for sometimes template %s",
1437 /* the pad is created, we need to autoplug it */
1438 to_connect = g_list_prepend (to_connect, pad);
1440 GST_DEBUG_OBJECT (dbin,
1441 "did not get the sometimes pad of template %s", templ_name);
1442 /* we have an element that will create dynamic pads */
1447 case GST_PAD_REQUEST:
1448 /* ignore request pads */
1449 GST_DEBUG_OBJECT (dbin, "ignoring request padtemplate %s", templ_name);
1454 /* 2. if there are more potential pads, connect to relevant signals */
1456 GST_LOG_OBJECT (dbin, "Adding signals to element %s in chain %p",
1457 GST_ELEMENT_NAME (element), chain);
1458 g_signal_connect (G_OBJECT (element), "pad-added",
1459 G_CALLBACK (pad_added_cb), chain);
1460 g_signal_connect (G_OBJECT (element), "pad-removed",
1461 G_CALLBACK (pad_removed_cb), chain);
1462 g_signal_connect (G_OBJECT (element), "no-more-pads",
1463 G_CALLBACK (no_more_pads_cb), chain);
1466 /* 3. for every available pad, connect it */
1467 for (pads = to_connect; pads; pads = g_list_next (pads)) {
1468 GstPad *pad = GST_PAD_CAST (pads->data);
1471 caps = gst_pad_get_caps (pad);
1472 analyze_new_pad (dbin, element, pad, caps, chain);
1474 gst_caps_unref (caps);
1476 gst_object_unref (pad);
1478 g_list_free (to_connect);
1485 * Expose the given pad on the chain as a decoded pad.
1488 expose_pad (GstDecodeBin * dbin, GstElement * src, GstDecodePad * dpad,
1489 GstPad * pad, GstDecodeChain * chain)
1491 GstPad *mqpad = NULL;
1493 GST_DEBUG_OBJECT (dbin, "pad %s:%s, chain:%p",
1494 GST_DEBUG_PAD_NAME (pad), chain);
1496 /* If this is the first pad for this chain, there are no other elements
1497 * and the source element is not the multiqueue we must link through the
1500 * This is the case if a demuxer directly exposed a raw pad.
1502 if (chain->parent && !chain->elements && src != chain->parent->multiqueue) {
1503 GST_LOG_OBJECT (src, "connecting the pad through multiqueue");
1505 gst_ghost_pad_set_target (GST_GHOST_PAD (dpad), NULL);
1506 if (!(mqpad = gst_decode_group_control_demuxer_pad (chain->parent, pad)))
1509 gst_ghost_pad_set_target (GST_GHOST_PAD (dpad), pad);
1512 gst_decode_pad_activate (dpad, chain);
1513 chain->endpad = gst_object_ref (dpad);
1516 if (gst_decode_chain_is_complete (dbin->decode_chain)) {
1517 gst_decode_bin_expose (dbin);
1519 EXPOSE_UNLOCK (dbin);
1522 gst_object_unref (mqpad);
1529 type_found (GstElement * typefind, guint probability,
1530 GstCaps * caps, GstDecodeBin * decode_bin)
1534 GST_DEBUG_OBJECT (decode_bin, "typefind found caps %" GST_PTR_FORMAT, caps);
1536 /* If the typefinder (but not something else) finds text/plain - i.e. that's
1537 * the top-level type of the file - then error out.
1539 if (gst_structure_has_name (gst_caps_get_structure (caps, 0), "text/plain")) {
1540 GST_ELEMENT_ERROR (decode_bin, STREAM, WRONG_TYPE,
1541 (_("This appears to be a text file")),
1542 ("decodebin2 cannot decode plain text files"));
1546 /* FIXME: we can only deal with one type, we don't yet support dynamically changing
1547 * caps from the typefind element */
1548 if (decode_bin->have_type || decode_bin->decode_chain)
1551 decode_bin->have_type = TRUE;
1553 pad = gst_element_get_static_pad (typefind, "src");
1555 decode_bin->decode_chain = gst_decode_chain_new (decode_bin, NULL);
1556 analyze_new_pad (decode_bin, typefind, pad, caps, decode_bin->decode_chain);
1558 gst_object_unref (pad);
1565 pad_added_cb (GstElement * element, GstPad * pad, GstDecodeChain * chain)
1572 GST_DEBUG_OBJECT (pad, "pad added, chain:%p", chain);
1574 caps = gst_pad_get_caps (pad);
1575 analyze_new_pad (dbin, element, pad, caps, chain);
1577 gst_caps_unref (caps);
1580 if (gst_decode_chain_is_complete (dbin->decode_chain)) {
1581 GST_LOG_OBJECT (dbin,
1582 "That was the last dynamic object, now attempting to expose the group");
1583 if (!gst_decode_bin_expose (dbin))
1584 GST_WARNING_OBJECT (dbin, "Couldn't expose group");
1586 EXPOSE_UNLOCK (dbin);
1590 pad_removed_cb (GstElement * element, GstPad * pad, GstDecodeChain * chain)
1594 GST_LOG_OBJECT (pad, "pad removed, chain:%p", chain);
1596 /* In fact, we don't have to do anything here, the active group will be
1597 * removed when the group's multiqueue is drained */
1598 CHAIN_MUTEX_LOCK (chain);
1599 for (l = chain->pending_pads; l; l = l->next) {
1600 GstPad *opad = l->data;
1603 g_signal_handlers_disconnect_by_func (pad, caps_notify_cb, chain);
1604 gst_object_unref (pad);
1605 chain->pending_pads = g_list_delete_link (chain->pending_pads, l);
1609 CHAIN_MUTEX_UNLOCK (chain);
1613 no_more_pads_cb (GstElement * element, GstDecodeChain * chain)
1615 GstDecodeGroup *group = NULL;
1617 GST_LOG_OBJECT (element, "got no more pads");
1619 if (!chain->elements || (GstElement *) chain->elements->data != element) {
1620 GST_LOG_OBJECT (chain->dbin, "no-more-pads from old chain element '%s'",
1621 GST_OBJECT_NAME (element));
1623 } else if (!chain->demuxer) {
1624 GST_LOG_OBJECT (chain->dbin, "no-more-pads from a non-demuxer element '%s'",
1625 GST_OBJECT_NAME (element));
1629 CHAIN_MUTEX_LOCK (chain);
1630 /* when we received no_more_pads, we can complete the pads of the chain */
1631 if (!chain->next_groups && chain->active_group) {
1632 group = chain->active_group;
1633 } else if (chain->next_groups) {
1634 group = chain->next_groups->data;
1637 GST_ERROR_OBJECT (chain->dbin, "can't find group for element");
1638 CHAIN_MUTEX_UNLOCK (chain);
1642 GST_DEBUG_OBJECT (element, "Setting group %p to complete", group);
1644 group->no_more_pads = TRUE;
1645 CHAIN_MUTEX_UNLOCK (chain);
1647 EXPOSE_LOCK (chain->dbin);
1648 if (gst_decode_chain_is_complete (chain->dbin->decode_chain)) {
1649 gst_decode_bin_expose (chain->dbin);
1651 EXPOSE_UNLOCK (chain->dbin);
1655 caps_notify_cb (GstPad * pad, GParamSpec * unused, GstDecodeChain * chain)
1657 GstElement *element;
1660 GST_LOG_OBJECT (pad, "Notified caps for pad %s:%s", GST_DEBUG_PAD_NAME (pad));
1662 /* Disconnect this; if we still need it, we'll reconnect to this in
1663 * analyze_new_pad */
1664 g_signal_handlers_disconnect_by_func (pad, caps_notify_cb, chain);
1666 element = GST_ELEMENT_CAST (gst_pad_get_parent (pad));
1668 CHAIN_MUTEX_LOCK (chain);
1669 for (l = chain->pending_pads; l; l = l->next) {
1670 if (l->data == pad) {
1671 gst_object_unref (GST_OBJECT_CAST (l->data));
1672 chain->pending_pads = g_list_delete_link (chain->pending_pads, l);
1676 CHAIN_MUTEX_UNLOCK (chain);
1678 pad_added_cb (element, pad, chain);
1680 gst_object_unref (element);
1683 /* Decide whether an element is a demuxer based on the
1684 * klass and number/type of src pad templates it has */
1686 is_demuxer_element (GstElement * srcelement)
1688 GstElementFactory *srcfactory;
1689 GstElementClass *elemclass;
1692 gint potential_src_pads = 0;
1694 srcfactory = gst_element_get_factory (srcelement);
1695 klass = gst_element_factory_get_klass (srcfactory);
1697 /* Can't be a demuxer unless it has Demux in the klass name */
1698 if (!strstr (klass, "Demux"))
1701 /* Walk the src pad templates and count how many the element
1703 elemclass = GST_ELEMENT_GET_CLASS (srcelement);
1705 walk = gst_element_class_get_pad_template_list (elemclass);
1706 while (walk != NULL) {
1707 GstPadTemplate *templ;
1709 templ = (GstPadTemplate *) walk->data;
1710 if (GST_PAD_TEMPLATE_DIRECTION (templ) == GST_PAD_SRC) {
1711 switch (GST_PAD_TEMPLATE_PRESENCE (templ)) {
1712 case GST_PAD_ALWAYS:
1713 case GST_PAD_SOMETIMES:
1714 if (strstr (GST_PAD_TEMPLATE_NAME_TEMPLATE (templ), "%"))
1715 potential_src_pads += 2; /* Might make multiple pads */
1717 potential_src_pads += 1;
1719 case GST_PAD_REQUEST:
1720 potential_src_pads += 2;
1724 walk = g_list_next (walk);
1727 if (potential_src_pads < 2)
1733 /* Returns TRUE if the caps are raw, or if they are compatible with the caps
1734 * specified in the 'caps' property
1736 * The decodebin_lock should be taken !
1739 are_raw_caps (GstDecodeBin * dbin, GstCaps * caps)
1743 GST_LOG_OBJECT (dbin, "Checking with caps %" GST_PTR_FORMAT, caps);
1745 /* lock for getting the caps */
1746 GST_OBJECT_LOCK (dbin);
1747 res = gst_caps_can_intersect (dbin->caps, caps);
1748 GST_OBJECT_UNLOCK (dbin);
1750 GST_LOG_OBJECT (dbin, "Caps are %sfinal caps", res ? "" : "not ");
1756 * GstDecodeChain functions
1759 /* gst_decode_chain_get_current_group:
1761 * Returns the current group of this chain, to which
1762 * new chains should be attached or NULL if the last
1763 * group didn't have no-more-pads.
1765 * Not MT-safe: Call with parent chain lock!
1767 static GstDecodeGroup *
1768 gst_decode_chain_get_current_group (GstDecodeChain * chain)
1770 GstDecodeGroup *group;
1772 if (!chain->next_groups && chain->active_group
1773 && chain->active_group->overrun && !chain->active_group->no_more_pads) {
1774 GST_WARNING_OBJECT (chain->dbin,
1775 "Currently active group %p is exposed"
1776 " and wants to add a new pad without having signaled no-more-pads",
1777 chain->active_group);
1781 if (chain->next_groups && (group = chain->next_groups->data) && group->overrun
1782 && !group->no_more_pads) {
1783 GST_WARNING_OBJECT (chain->dbin,
1784 "Currently newest pending group %p "
1785 "had overflow but didn't signal no-more-pads", group);
1789 /* Now we know that we can really return something useful */
1790 if (!chain->active_group) {
1791 chain->active_group = group = gst_decode_group_new (chain->dbin, chain);
1792 } else if (!chain->active_group->overrun
1793 && !chain->active_group->no_more_pads) {
1794 group = chain->active_group;
1795 } else if (chain->next_groups && (group = chain->next_groups->data)
1796 && !group->overrun && !group->no_more_pads) {
1797 /* group = chain->next_groups->data */
1799 group = gst_decode_group_new (chain->dbin, chain);
1800 chain->next_groups = g_list_prepend (chain->next_groups, group);
1806 static void gst_decode_group_free_internal (GstDecodeGroup * group,
1810 gst_decode_chain_free_internal (GstDecodeChain * chain, gboolean hide)
1814 CHAIN_MUTEX_LOCK (chain);
1816 GST_DEBUG_OBJECT (chain->dbin, "%s chain %p", (hide ? "Hiding" : "Freeing"),
1819 if (chain->active_group) {
1820 gst_decode_group_free_internal (chain->active_group, hide);
1822 chain->active_group = NULL;
1825 for (l = chain->next_groups; l; l = l->next) {
1826 gst_decode_group_free_internal ((GstDecodeGroup *) l->data, hide);
1831 g_list_free (chain->next_groups);
1832 chain->next_groups = NULL;
1836 for (l = chain->old_groups; l; l = l->next) {
1837 GstDecodeGroup *group = l->data;
1839 gst_decode_group_free (group);
1841 g_list_free (chain->old_groups);
1842 chain->old_groups = NULL;
1845 for (l = chain->pending_pads; l; l = l->next) {
1846 GstPad *pad = GST_PAD (l->data);
1848 g_signal_handlers_disconnect_by_func (pad, caps_notify_cb, chain);
1849 gst_object_unref (pad);
1852 g_list_free (chain->pending_pads);
1853 chain->pending_pads = NULL;
1855 for (l = chain->elements; l; l = l->next) {
1856 GstElement *element = GST_ELEMENT (l->data);
1858 g_signal_handlers_disconnect_by_func (element, pad_added_cb, chain);
1859 g_signal_handlers_disconnect_by_func (element, pad_removed_cb, chain);
1860 g_signal_handlers_disconnect_by_func (element, no_more_pads_cb, chain);
1862 if (GST_OBJECT_PARENT (element) == GST_OBJECT_CAST (chain->dbin))
1863 gst_bin_remove (GST_BIN_CAST (chain->dbin), element);
1865 gst_element_set_state (element, GST_STATE_NULL);
1868 GST_OBJECT_LOCK (chain->dbin);
1869 /* remove possible subtitle element */
1870 chain->dbin->subtitles = g_list_remove (chain->dbin->subtitles, element);
1871 GST_OBJECT_UNLOCK (chain->dbin);
1874 gst_object_unref (element);
1879 g_list_free (chain->elements);
1880 chain->elements = NULL;
1883 if (chain->endpad) {
1884 if (chain->endpad->exposed)
1885 gst_element_remove_pad (GST_ELEMENT_CAST (chain->dbin),
1886 GST_PAD_CAST (chain->endpad));
1888 chain->endpad->exposed = FALSE;
1890 gst_object_unref (chain->endpad);
1891 chain->endpad = NULL;
1895 GST_DEBUG_OBJECT (chain->dbin, "%s chain %p", (hide ? "Hided" : "Freed"),
1897 CHAIN_MUTEX_UNLOCK (chain);
1899 g_mutex_free (chain->lock);
1900 g_slice_free (GstDecodeChain, chain);
1904 /* gst_decode_chain_free:
1906 * Completely frees and removes the chain and all
1907 * child groups from decodebin2.
1909 * MT-safe, don't hold the chain lock or any child chain's lock
1910 * when calling this!
1913 gst_decode_chain_free (GstDecodeChain * chain)
1915 gst_decode_chain_free_internal (chain, FALSE);
1918 /* gst_decode_chain_new:
1920 * Creates a new decode chain and initializes it.
1922 * It's up to the caller to add it to the list of child chains of
1925 static GstDecodeChain *
1926 gst_decode_chain_new (GstDecodeBin * dbin, GstDecodeGroup * parent)
1928 GstDecodeChain *chain = g_slice_new0 (GstDecodeChain);
1930 GST_DEBUG_OBJECT (dbin, "Creating new chain %p with parent group %p", chain,
1934 chain->parent = parent;
1935 chain->lock = g_mutex_new ();
1941 * GstDecodeGroup functions
1944 /* The overrun callback is used to expose groups that have not yet had their
1945 * no_more_pads called while the (large) multiqueue overflowed. When this
1946 * happens we must assume that the no_more_pads will not arrive anymore and we
1947 * must expose the pads that we have.
1950 multi_queue_overrun_cb (GstElement * queue, GstDecodeGroup * group)
1956 GST_LOG_OBJECT (dbin, "multiqueue '%s' (%p) is full", GST_OBJECT_NAME (queue),
1959 group->overrun = TRUE;
1961 /* FIXME: We should make sure that everything gets exposed now
1962 * even if child chains are not complete because the will never
1963 * be complete! Ignore any non-complete chains when exposing
1964 * and never expose them later
1968 if (gst_decode_chain_is_complete (dbin->decode_chain)) {
1969 if (!gst_decode_bin_expose (dbin))
1970 GST_WARNING_OBJECT (dbin, "Couldn't expose group");
1972 EXPOSE_UNLOCK (group->dbin);
1976 gst_decode_group_free_internal (GstDecodeGroup * group, gboolean hide)
1980 GST_DEBUG_OBJECT (group->dbin, "%s group %p", (hide ? "Hiding" : "Freeing"),
1982 for (l = group->children; l; l = l->next) {
1983 GstDecodeChain *chain = (GstDecodeChain *) l->data;
1985 gst_decode_chain_free_internal (chain, hide);
1990 g_list_free (group->children);
1991 group->children = NULL;
1995 for (l = group->reqpads; l; l = l->next) {
1996 GstPad *pad = l->data;
1998 gst_element_release_request_pad (group->multiqueue, pad);
1999 gst_object_unref (pad);
2002 g_list_free (group->reqpads);
2003 group->reqpads = NULL;
2006 if (group->multiqueue) {
2007 if (group->overrunsig) {
2008 g_signal_handler_disconnect (group->multiqueue, group->overrunsig);
2009 group->overrunsig = 0;
2012 if (GST_OBJECT_PARENT (group->multiqueue) == GST_OBJECT_CAST (group->dbin))
2013 gst_bin_remove (GST_BIN_CAST (group->dbin), group->multiqueue);
2015 gst_element_set_state (group->multiqueue, GST_STATE_NULL);
2016 gst_object_unref (group->multiqueue);
2017 group->multiqueue = NULL;
2021 GST_DEBUG_OBJECT (group->dbin, "%s group %p", (hide ? "Hided" : "Freed"),
2024 g_slice_free (GstDecodeGroup, group);
2027 /* gst_decode_group_free:
2029 * Completely frees and removes the decode group and all
2032 * Never call this from any streaming thread!
2034 * Not MT-safe, call with parent's chain lock!
2037 gst_decode_group_free (GstDecodeGroup * group)
2039 gst_decode_group_free_internal (group, FALSE);
2042 /* gst_decode_group_hide:
2044 * Hide the decode group only, this means that
2045 * all child endpads are removed from decodebin2
2046 * and all signals are unconnected.
2048 * No element is set to NULL state and completely
2051 * Can be called from streaming threads.
2053 * Not MT-safe, call with parent's chain lock!
2056 gst_decode_group_hide (GstDecodeGroup * group)
2058 gst_decode_group_free_internal (group, TRUE);
2061 /* gst_decode_group_new:
2062 * @dbin: Parent decodebin
2063 * @parent: Parent chain or %NULL
2065 * Creates a new GstDecodeGroup. It is up to the caller to add it to the list
2068 static GstDecodeGroup *
2069 gst_decode_group_new (GstDecodeBin * dbin, GstDecodeChain * parent)
2071 GstDecodeGroup *group = g_slice_new0 (GstDecodeGroup);
2074 GST_DEBUG_OBJECT (dbin, "Creating new group %p with parent chain %p", group,
2078 group->parent = parent;
2080 mq = group->multiqueue = gst_element_factory_make ("multiqueue", NULL);
2081 if (G_UNLIKELY (!group->multiqueue)) {
2082 gst_element_post_message (GST_ELEMENT_CAST (dbin),
2083 gst_missing_element_message_new (GST_ELEMENT_CAST (dbin),
2085 GST_ELEMENT_ERROR (dbin, CORE, MISSING_PLUGIN, (NULL), ("no multiqueue!"));
2086 g_slice_free (GstDecodeGroup, group);
2090 g_object_set (G_OBJECT (mq),
2091 "max-size-bytes", (guint) 2 * 1024 * 1024,
2092 "max-size-time", (guint64) 0, "max-size-buffers", (guint) 0, NULL);
2094 group->overrunsig = g_signal_connect (G_OBJECT (mq), "overrun",
2095 G_CALLBACK (multi_queue_overrun_cb), group);
2097 gst_bin_add (GST_BIN (dbin), gst_object_ref (mq));
2098 gst_element_set_state (mq, GST_STATE_PAUSED);
2103 /* gst_decode_group_control_demuxer_pad
2105 * Adds a new demuxer srcpad to the given group.
2107 * Returns the srcpad of the multiqueue corresponding the given pad.
2108 * Returns NULL if there was an error.
2111 gst_decode_group_control_demuxer_pad (GstDecodeGroup * group, GstPad * pad)
2114 GstPad *srcpad, *sinkpad;
2115 GstIterator *it = NULL;
2119 GST_LOG_OBJECT (dbin, "group:%p pad %s:%s", group, GST_DEBUG_PAD_NAME (pad));
2123 if (G_UNLIKELY (!group->multiqueue))
2126 if (!(sinkpad = gst_element_get_request_pad (group->multiqueue, "sink%d"))) {
2127 GST_ERROR_OBJECT (dbin, "Couldn't get sinkpad from multiqueue");
2131 if ((gst_pad_link (pad, sinkpad) != GST_PAD_LINK_OK)) {
2132 GST_ERROR_OBJECT (dbin, "Couldn't link demuxer and multiqueue");
2136 it = gst_pad_iterate_internal_links (sinkpad);
2138 if (!it || (gst_iterator_next (it, (gpointer) & srcpad)) != GST_ITERATOR_OK
2139 || srcpad == NULL) {
2140 GST_ERROR_OBJECT (dbin,
2141 "Couldn't get srcpad from multiqueue for sinkpad %" GST_PTR_FORMAT,
2146 CHAIN_MUTEX_LOCK (group->parent);
2147 group->reqpads = g_list_prepend (group->reqpads, gst_object_ref (sinkpad));
2148 CHAIN_MUTEX_UNLOCK (group->parent);
2152 gst_iterator_free (it);
2153 gst_object_unref (sinkpad);
2157 gst_element_release_request_pad (group->multiqueue, sinkpad);
2161 /* gst_decode_group_is_complete:
2163 * Checks if the group is complete, this means that
2164 * a) overrun of the multiqueue or no-more-pads happened
2165 * b) all child chains are complete
2167 * Not MT-safe, always call with decodebin expose lock
2170 gst_decode_group_is_complete (GstDecodeGroup * group)
2173 gboolean complete = TRUE;
2175 if (!group->overrun && !group->no_more_pads) {
2180 for (l = group->children; l; l = l->next) {
2181 GstDecodeChain *chain = l->data;
2183 if (!gst_decode_chain_is_complete (chain)) {
2190 GST_DEBUG_OBJECT (group->dbin, "Group %p is complete: %d", group, complete);
2194 /* gst_decode_chain_is_complete:
2196 * Returns TRUE if the chain is complete, this means either
2197 * a) This chain is a dead end, i.e. we have no suitable plugins
2198 * b) This chain ends in an endpad and this is blocked or exposed
2200 * Not MT-safe, always call with decodebin expose lock
2203 gst_decode_chain_is_complete (GstDecodeChain * chain)
2205 gboolean complete = FALSE;
2207 if (chain->deadend) {
2212 if (chain->endpad && (chain->endpad->blocked || chain->endpad->exposed)) {
2217 if (chain->demuxer) {
2218 if (chain->active_group
2219 && gst_decode_group_is_complete (chain->active_group)) {
2226 GST_DEBUG_OBJECT (chain->dbin, "Chain %p is complete: %d", chain, complete);
2230 /* check if the group is drained, meaning all pads have seen an EOS
2233 gst_decode_pad_handle_eos (GstDecodePad * pad)
2235 GstDecodeChain *chain = pad->chain;
2237 GST_LOG_OBJECT (pad->dbin, "chain : %p, pad %p", chain, pad);
2238 pad->drained = TRUE;
2239 gst_decode_chain_handle_eos (chain);
2242 /* gst_decode_chain_handle_eos:
2244 * Checks if there are next groups in any parent chain
2245 * to which we can switch or if everything is drained.
2247 * If there are groups to switch to, hide the current active
2248 * one and expose the new one.
2250 * MT-safe, don't call with chain lock!
2253 gst_decode_chain_handle_eos (GstDecodeChain * eos_chain)
2255 GstDecodeBin *dbin = eos_chain->dbin;
2256 GstDecodeGroup *group = eos_chain->parent;
2257 GstDecodeChain *chain = eos_chain;
2260 g_return_if_fail (eos_chain->endpad);
2262 CHAIN_MUTEX_LOCK (chain);
2263 while ((group = chain->parent)) {
2264 CHAIN_MUTEX_UNLOCK (chain);
2265 chain = group->parent;
2266 CHAIN_MUTEX_LOCK (chain);
2268 if (gst_decode_group_is_drained (group)) {
2274 drained = chain->active_group ?
2275 gst_decode_group_is_drained (chain->active_group) : TRUE;
2277 /* Now either group == NULL and chain == dbin->decode_chain
2278 * or chain is the lowest chain that has a non-drained group */
2279 if (chain->active_group && drained && chain->next_groups) {
2280 GST_DEBUG_OBJECT (dbin, "Hiding current group %p", chain->active_group);
2281 gst_decode_group_hide (chain->active_group);
2282 chain->old_groups = g_list_prepend (chain->old_groups, chain->active_group);
2283 GST_DEBUG_OBJECT (dbin, "Switching to next group %p",
2284 chain->next_groups->data);
2285 chain->active_group = chain->next_groups->data;
2286 chain->next_groups =
2287 g_list_delete_link (chain->next_groups, chain->next_groups);
2288 CHAIN_MUTEX_UNLOCK (chain);
2290 if (gst_decode_chain_is_complete (dbin->decode_chain))
2291 gst_decode_bin_expose (dbin);
2292 EXPOSE_UNLOCK (dbin);
2293 } else if (!chain->active_group || drained) {
2294 g_assert (chain == dbin->decode_chain);
2295 CHAIN_MUTEX_UNLOCK (chain);
2297 GST_LOG_OBJECT (dbin, "all groups drained, fire signal");
2298 g_signal_emit (G_OBJECT (dbin), gst_decode_bin_signals[SIGNAL_DRAINED], 0,
2301 CHAIN_MUTEX_UNLOCK (chain);
2302 GST_DEBUG_OBJECT (dbin,
2303 "Current active group in chain %p is not drained yet", chain);
2307 /* gst_decode_group_is_drained:
2309 * Check is this group is drained and cache this result.
2310 * The group is drained if all child chains are drained.
2312 * Not MT-safe, call with group->parent's lock */
2314 gst_decode_group_is_drained (GstDecodeGroup * group)
2317 gboolean drained = TRUE;
2319 if (group->drained) {
2324 for (l = group->children; l; l = l->next) {
2325 GstDecodeChain *chain = l->data;
2327 CHAIN_MUTEX_LOCK (chain);
2328 if (!gst_decode_chain_is_drained (chain))
2330 CHAIN_MUTEX_UNLOCK (chain);
2334 group->drained = drained;
2337 GST_DEBUG_OBJECT (group->dbin, "Group %p is drained: %d", group, drained);
2341 /* gst_decode_chain_is_drained:
2343 * Check is the chain is drained, which means that
2346 * a) it's endpad is drained
2347 * b) there are no pending pads, the active group is drained
2348 * and there are no next groups
2350 * Not MT-safe, call with chain lock
2353 gst_decode_chain_is_drained (GstDecodeChain * chain)
2355 gboolean drained = FALSE;
2357 if (chain->endpad) {
2358 drained = chain->endpad->drained;
2362 if (chain->pending_pads) {
2367 if (chain->active_group && gst_decode_group_is_drained (chain->active_group)
2368 && !chain->next_groups) {
2374 GST_DEBUG_OBJECT (chain->dbin, "Chain %p is drained: %d", chain, drained);
2379 * GCompareFunc to use with lists of GstPad.
2380 * Sorts pads by mime type.
2381 * First video (raw, then non-raw), then audio (raw, then non-raw),
2384 * Return: negative if a<b, 0 if a==b, positive if a>b
2387 sort_end_pads (GstDecodePad * da, GstDecodePad * db)
2390 GstCaps *capsa, *capsb;
2391 GstStructure *sa, *sb;
2392 const gchar *namea, *nameb;
2394 capsa = gst_pad_get_caps (GST_PAD (da));
2395 capsb = gst_pad_get_caps (GST_PAD (db));
2397 sa = gst_caps_get_structure ((const GstCaps *) capsa, 0);
2398 sb = gst_caps_get_structure ((const GstCaps *) capsb, 0);
2400 namea = gst_structure_get_name (sa);
2401 nameb = gst_structure_get_name (sb);
2403 if (g_strrstr (namea, "video/x-raw-"))
2405 else if (g_strrstr (namea, "video/"))
2407 else if (g_strrstr (namea, "audio/x-raw"))
2409 else if (g_strrstr (namea, "audio/"))
2414 if (g_strrstr (nameb, "video/x-raw-"))
2416 else if (g_strrstr (nameb, "video/"))
2418 else if (g_strrstr (nameb, "audio/x-raw"))
2420 else if (g_strrstr (nameb, "audio/"))
2425 gst_caps_unref (capsa);
2426 gst_caps_unref (capsb);
2431 /* Must only be called if the toplevel chain is complete and blocked! */
2432 /* Not MT-safe, call with decodebin expose lock! */
2434 gst_decode_bin_expose (GstDecodeBin * dbin)
2436 GList *tmp, *endpads = NULL;
2437 gboolean already_exposed = TRUE;
2439 GST_DEBUG_OBJECT (dbin, "Exposing currently active chains/groups");
2441 /* Get the pads that we're going to expose and mark things as exposed */
2442 if (!gst_decode_chain_expose (dbin->decode_chain, &endpads)) {
2443 g_list_foreach (endpads, (GFunc) gst_object_unref, NULL);
2444 g_list_free (endpads);
2445 GST_ERROR_OBJECT (dbin, "Broken chain/group tree");
2446 g_return_val_if_reached (FALSE);
2449 if (endpads == NULL) {
2450 GST_WARNING_OBJECT (dbin, "No suitable plugins found");
2451 GST_ELEMENT_ERROR (dbin, CORE, MISSING_PLUGIN, (NULL),
2452 ("no suitable plugins found"));
2456 /* Check if this was called when everything was exposed already */
2457 for (tmp = endpads; tmp && already_exposed; tmp = tmp->next) {
2458 GstDecodePad *dpad = tmp->data;
2460 already_exposed &= dpad->exposed;
2461 if (!already_exposed)
2464 if (already_exposed) {
2465 GST_DEBUG_OBJECT (dbin, "Everything was exposed already!");
2466 g_list_foreach (endpads, (GFunc) gst_object_unref, NULL);
2467 g_list_free (endpads);
2471 /* Set all already exposed pads to blocked */
2472 for (tmp = endpads; tmp; tmp = tmp->next) {
2473 GstDecodePad *dpad = tmp->data;
2476 gst_decode_pad_set_blocked (dpad, TRUE);
2479 /* re-order pads : video, then audio, then others */
2480 endpads = g_list_sort (endpads, (GCompareFunc) sort_end_pads);
2483 for (tmp = endpads; tmp; tmp = tmp->next) {
2484 GstDecodePad *dpad = (GstDecodePad *) tmp->data;
2487 /* 1. rewrite name */
2488 padname = g_strdup_printf ("src%d", dbin->nbpads);
2490 GST_DEBUG_OBJECT (dbin, "About to expose dpad %s as %s",
2491 GST_OBJECT_NAME (dpad), padname);
2492 gst_object_set_name (GST_OBJECT (dpad), padname);
2495 /* 2. activate and add */
2497 && !gst_element_add_pad (GST_ELEMENT (dbin), GST_PAD (dpad))) {
2498 /* not really fatal, we can try to add the other pads */
2499 g_warning ("error adding pad to decodebin2");
2502 dpad->exposed = TRUE;
2504 /* 3. emit signal */
2505 GST_DEBUG_OBJECT (dbin, "emitting new-decoded-pad");
2506 g_signal_emit (G_OBJECT (dbin),
2507 gst_decode_bin_signals[SIGNAL_NEW_DECODED_PAD], 0, dpad,
2508 (tmp->next == NULL));
2509 GST_DEBUG_OBJECT (dbin, "emitted new-decoded-pad");
2512 /* signal no-more-pads. This allows the application to hook stuff to the
2514 GST_LOG_OBJECT (dbin, "signalling no-more-pads");
2515 gst_element_no_more_pads (GST_ELEMENT (dbin));
2517 /* 4. Unblock internal pads. The application should have connected stuff now
2518 * so that streaming can continue. */
2519 for (tmp = endpads; tmp; tmp = tmp->next) {
2520 GstDecodePad *dpad = (GstDecodePad *) tmp->data;
2522 GST_DEBUG_OBJECT (dpad, "unblocking");
2523 gst_decode_pad_unblock (dpad);
2524 GST_DEBUG_OBJECT (dpad, "unblocked");
2525 gst_object_unref (dpad);
2527 g_list_free (endpads);
2529 do_async_done (dbin);
2530 GST_DEBUG_OBJECT (dbin, "Exposed everything");
2534 /* gst_decode_chain_expose:
2536 * Check if the chain can be exposed and add all endpads
2537 * to the endpads list.
2539 * Also update the active group's multiqueue to the
2542 * Not MT-safe, call with decodebin expose lock! *
2545 gst_decode_chain_expose (GstDecodeChain * chain, GList ** endpads)
2547 GstDecodeGroup *group;
2553 if (chain->endpad) {
2554 if (!chain->endpad->blocked && !chain->endpad->exposed)
2556 *endpads = g_list_prepend (*endpads, gst_object_ref (chain->endpad));
2560 group = chain->active_group;
2563 if (!group->no_more_pads && !group->overrun)
2566 /* update runtime limits. At runtime, we try to keep the amount of buffers
2567 * in the queues as low as possible (but at least 5 buffers). */
2568 g_object_set (G_OBJECT (group->multiqueue),
2569 "max-size-bytes", 2 * 1024 * 1024, "max-size-buffers", 5, NULL);
2570 /* we can now disconnect any overrun signal, which is used to expose the
2572 if (group->overrunsig) {
2573 GST_LOG_OBJECT (group->dbin, "Disconnecting overrun");
2574 g_signal_handler_disconnect (group->multiqueue, group->overrunsig);
2575 group->overrunsig = 0;
2578 for (l = group->children; l; l = l->next) {
2579 GstDecodeChain *childchain = l->data;
2581 if (!gst_decode_chain_expose (childchain, endpads))
2588 /*************************
2589 * GstDecodePad functions
2590 *************************/
2593 gst_decode_pad_class_init (GstDecodePadClass * klass)
2598 gst_decode_pad_init (GstDecodePad * pad)
2601 pad->blocked = FALSE;
2602 pad->exposed = FALSE;
2603 pad->drained = FALSE;
2604 gst_object_ref (pad);
2605 gst_object_sink (pad);
2609 source_pad_blocked_cb (GstPad * pad, gboolean blocked, GstDecodePad * dpad)
2611 GstDecodeChain *chain;
2614 chain = dpad->chain;
2617 GST_LOG_OBJECT (dpad, "blocked:%d, dpad->chain:%p", blocked, chain);
2619 dpad->blocked = blocked;
2621 if (dpad->blocked) {
2623 if (gst_decode_chain_is_complete (dbin->decode_chain)) {
2624 if (!gst_decode_bin_expose (dbin))
2625 GST_WARNING_OBJECT (dbin, "Couldn't expose group");
2627 EXPOSE_UNLOCK (dbin);
2632 source_pad_event_probe (GstPad * pad, GstEvent * event, GstDecodePad * dpad)
2634 GST_LOG_OBJECT (pad, "%s dpad:%p", GST_EVENT_TYPE_NAME (event), dpad);
2636 if (GST_EVENT_TYPE (event) == GST_EVENT_EOS) {
2637 GST_DEBUG_OBJECT (pad, "we received EOS");
2639 /* Check if all pads are drained. If there is a next group to expose, we
2640 * will remove the ghostpad of the current group first, which unlinks the
2641 * peer and so drops the EOS. */
2642 gst_decode_pad_handle_eos (dpad);
2644 /* never drop events */
2649 gst_decode_pad_set_blocked (GstDecodePad * dpad, gboolean blocked)
2651 GstDecodeBin *dbin = dpad->dbin;
2655 opad = gst_ghost_pad_get_target (GST_GHOST_PAD_CAST (dpad));
2659 gst_pad_set_blocked_async_full (opad, blocked,
2660 (GstPadBlockCallback) source_pad_blocked_cb, gst_object_ref (dpad),
2661 (GDestroyNotify) gst_object_unref);
2663 if (dbin->shutdown) {
2664 /* deactivate to force flushing state to prevent NOT_LINKED errors */
2665 gst_pad_set_active (GST_PAD (dpad), FALSE);
2667 gst_object_ref (dpad);
2668 dbin->blocked_pads = g_list_prepend (dbin->blocked_pads, dpad);
2673 if ((l = g_list_find (dbin->blocked_pads, dpad))) {
2674 gst_object_unref (dpad);
2675 dbin->blocked_pads = g_list_delete_link (dbin->blocked_pads, l);
2678 gst_object_unref (opad);
2684 gst_decode_pad_add_drained_check (GstDecodePad * dpad)
2686 gst_pad_add_event_probe (GST_PAD (dpad),
2687 G_CALLBACK (source_pad_event_probe), dpad);
2691 gst_decode_pad_activate (GstDecodePad * dpad, GstDecodeChain * chain)
2693 g_return_if_fail (chain != NULL);
2695 dpad->chain = chain;
2696 gst_pad_set_active (GST_PAD (dpad), TRUE);
2697 gst_decode_pad_set_blocked (dpad, TRUE);
2698 gst_decode_pad_add_drained_check (dpad);
2702 gst_decode_pad_unblock (GstDecodePad * dpad)
2704 gst_decode_pad_set_blocked (dpad, FALSE);
2707 /*gst_decode_pad_new:
2709 * Creates a new GstDecodePad for the given pad.
2711 static GstDecodePad *
2712 gst_decode_pad_new (GstDecodeBin * dbin, GstPad * pad, GstDecodeChain * chain)
2717 g_object_new (GST_TYPE_DECODE_PAD, "direction", GST_PAD_DIRECTION (pad),
2719 gst_ghost_pad_construct (GST_GHOST_PAD (dpad));
2720 gst_ghost_pad_set_target (GST_GHOST_PAD (dpad), pad);
2721 dpad->chain = chain;
2729 * Element add/remove
2733 do_async_start (GstDecodeBin * dbin)
2735 GstMessage *message;
2737 dbin->async_pending = TRUE;
2739 message = gst_message_new_async_start (GST_OBJECT_CAST (dbin), FALSE);
2740 parent_class->handle_message (GST_BIN_CAST (dbin), message);
2744 do_async_done (GstDecodeBin * dbin)
2746 GstMessage *message;
2748 if (dbin->async_pending) {
2749 message = gst_message_new_async_done (GST_OBJECT_CAST (dbin));
2750 parent_class->handle_message (GST_BIN_CAST (dbin), message);
2752 dbin->async_pending = FALSE;
2757 * convenience functions
2762 * Returns the first sink pad of the given element, or NULL if it doesn't have
2767 find_sink_pad (GstElement * element)
2773 it = gst_element_iterate_sink_pads (element);
2775 if ((gst_iterator_next (it, &point)) == GST_ITERATOR_OK)
2776 pad = (GstPad *) point;
2778 gst_iterator_free (it);
2783 /* call with dyn_lock held */
2785 unblock_pads (GstDecodeBin * dbin)
2789 for (tmp = dbin->blocked_pads; tmp; tmp = tmp->next) {
2790 GstDecodePad *dpad = (GstDecodePad *) tmp->data;
2791 GstPad *opad = gst_ghost_pad_get_target (GST_GHOST_PAD_CAST (dpad));
2796 GST_DEBUG_OBJECT (dpad, "unblocking");
2797 gst_pad_set_blocked_async_full (opad, FALSE,
2798 (GstPadBlockCallback) source_pad_blocked_cb, gst_object_ref (dpad),
2799 (GDestroyNotify) gst_object_unref);
2800 /* make flushing, prevent NOT_LINKED */
2801 GST_PAD_SET_FLUSHING (GST_PAD (dpad));
2802 gst_object_unref (dpad);
2803 gst_object_unref (opad);
2804 GST_DEBUG_OBJECT (dpad, "unblocked");
2807 /* clear, no more blocked pads */
2808 g_list_free (dbin->blocked_pads);
2809 dbin->blocked_pads = NULL;
2812 static GstStateChangeReturn
2813 gst_decode_bin_change_state (GstElement * element, GstStateChange transition)
2815 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
2816 GstDecodeBin *dbin = GST_DECODE_BIN (element);
2818 switch (transition) {
2819 case GST_STATE_CHANGE_NULL_TO_READY:
2820 if (dbin->typefind == NULL)
2821 goto missing_typefind;
2823 case GST_STATE_CHANGE_READY_TO_PAUSED:
2825 GST_LOG_OBJECT (dbin, "clearing shutdown flag");
2826 dbin->shutdown = FALSE;
2828 dbin->have_type = FALSE;
2829 ret = GST_STATE_CHANGE_ASYNC;
2830 do_async_start (dbin);
2832 case GST_STATE_CHANGE_PAUSED_TO_READY:
2834 GST_LOG_OBJECT (dbin, "setting shutdown flag");
2835 dbin->shutdown = TRUE;
2836 unblock_pads (dbin);
2843 GstStateChangeReturn bret;
2845 bret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
2846 if (G_UNLIKELY (bret == GST_STATE_CHANGE_FAILURE))
2847 goto activate_failed;
2848 else if (G_UNLIKELY (bret == GST_STATE_CHANGE_NO_PREROLL)) {
2849 do_async_done (dbin);
2853 switch (transition) {
2854 case GST_STATE_CHANGE_PAUSED_TO_READY:
2855 do_async_done (dbin);
2856 if (dbin->decode_chain) {
2857 gst_decode_chain_free (dbin->decode_chain);
2858 dbin->decode_chain = NULL;
2861 case GST_STATE_CHANGE_READY_TO_NULL:
2871 gst_element_post_message (element,
2872 gst_missing_element_message_new (element, "typefind"));
2873 GST_ELEMENT_ERROR (dbin, CORE, MISSING_PLUGIN, (NULL), ("no typefind!"));
2874 return GST_STATE_CHANGE_FAILURE;
2878 GST_DEBUG_OBJECT (element,
2879 "element failed to change states -- activation problem?");
2880 return GST_STATE_CHANGE_FAILURE;
2885 gst_decode_bin_plugin_init (GstPlugin * plugin)
2887 GST_DEBUG_CATEGORY_INIT (gst_decode_bin_debug, "decodebin2", 0,
2891 GST_DEBUG ("binding text domain %s to locale dir %s", GETTEXT_PACKAGE,
2893 bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
2894 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
2895 #endif /* ENABLE_NLS */
2897 return gst_element_register (plugin, "decodebin2", GST_RANK_NONE,
2898 GST_TYPE_DECODE_BIN);