2 * Copyright (C) <2006> Edward Hervey <edward@fluendo.com>
3 * Copyright (C) <2009> Sebastian Dröge <sebastian.droege@collabora.co.uk>
4 * Copyright (C) <2011> Hewlett-Packard Development Company, L.P.
5 * Author: Sebastian Dröge <sebastian.droege@collabora.co.uk>, Collabora Ltd.
6 * Copyright (C) <2013> Collabora Ltd.
7 * Author: Sebastian Dröge <sebastian.droege@collabora.co.uk>
8 * Copyright (C) <2015-2016> Centricular Ltd
9 * @author: Edward Hervey <edward@centricular.com>
10 * @author: Jan Schmidt <jan@centricular.com>
12 * This library is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Library General Public
14 * License as published by the Free Software Foundation; either
15 * version 2 of the License, or (at your option) any later version.
17 * This library is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Library General Public License for more details.
22 * You should have received a copy of the GNU Library General Public
23 * License along with this library; if not, write to the
24 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
25 * Boston, MA 02110-1301, USA.
29 * SECTION:element-ParseBin
31 * #GstBin that auto-magically constructs a parsing pipeline
32 * using available parsers and demuxers via auto-plugging.
36 /* Implementation notes:
38 * The following section describes how ParseBin works internally.
40 * The first part of ParseBin is its typefind element, which tries
41 * to determine the media type of the input stream. If the type is found
42 * autoplugging starts.
44 * ParseBin internally organizes the elements it autoplugged into
45 * GstParseChains and GstParseGroups. A parse chain is a single chain
47 * means that if ParseBin ever autoplugs an element with two+ srcpads
48 * (e.g. a demuxer) this will end the chain and everything following this
49 * demuxer will be put into parse groups below the chain. Otherwise,
50 * if an element has a single srcpad that outputs raw data the parse chain
51 * is ended too and a GstParsePad is stored and blocked.
53 * A parse group combines a number of chains that are created by a
56 * This continues until the top-level parse chain is complete. A parse
57 * chain is complete if it either ends with a blocked elementary stream,
58 * if autoplugging stopped because no suitable plugins could be found
59 * or if the active group is complete. A parse group on the other hand
60 * is complete if all child chains are complete.
62 * If this happens at some point, all end pads of all active groups are exposed.
63 * For this ParseBin adds the end pads, and then unblocks them. Now playback starts.
65 * If one of the chains that end on a endpad receives EOS ParseBin checks
66 * if all chains and groups are drained. In that case everything goes into EOS.
67 * If there is a chain where the active group is drained but there exist next
68 * groups, the active group is hidden (endpads are removed) and the next group
69 * is exposed. This means that in some cases more pads may be created even
70 * after the initial no-more-pads signal. This happens for example with
71 * so-called "chained oggs", most commonly found among ogg/vorbis internet
74 * Note 1: If we're talking about blocked endpads this really means that the
75 * *target* pads of the endpads are blocked. Pads that are exposed to the outside
76 * should never ever be blocked!
78 * Note 2: If a group is complete and the parent's chain demuxer adds new pads
79 * but never signaled no-more-pads this additional pads will be ignored!
83 /* FIXME 0.11: suppress warnings for deprecated API such as GValueArray
84 * with newer GLib versions (>= 2.31.0) */
85 #define GLIB_DISABLE_DEPRECATION_WARNINGS
91 #include <gst/gst-i18n-plugin.h>
95 #include <gst/pbutils/pbutils.h>
97 #include "gstplay-enum.h"
98 #include "gstplayback.h"
100 /* Also used by gsturidecodebin.c */
101 gint _parse_bin_compare_factories_func (gconstpointer p1, gconstpointer p2);
103 /* generic templates */
104 static GstStaticPadTemplate decoder_bin_sink_template =
105 GST_STATIC_PAD_TEMPLATE ("sink",
108 GST_STATIC_CAPS_ANY);
110 static GstStaticPadTemplate decoder_bin_src_template =
111 GST_STATIC_PAD_TEMPLATE ("src_%u",
114 GST_STATIC_CAPS_ANY);
116 GST_DEBUG_CATEGORY_STATIC (gst_parse_bin_debug);
117 #define GST_CAT_DEFAULT gst_parse_bin_debug
119 typedef struct _GstPendingPad GstPendingPad;
120 typedef struct _GstParseElement GstParseElement;
121 typedef struct _GstParseChain GstParseChain;
122 typedef struct _GstParseGroup GstParseGroup;
123 typedef struct _GstParsePad GstParsePad;
124 typedef GstGhostPadClass GstParsePadClass;
125 typedef struct _GstParseBin GstParseBin;
126 typedef struct _GstParseBinClass GstParseBinClass;
128 #define GST_TYPE_PARSE_BIN (gst_parse_bin_get_type())
129 #define GST_PARSE_BIN_CAST(obj) ((GstParseBin*)(obj))
130 #define GST_PARSE_BIN(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_PARSE_BIN,GstParseBin))
131 #define GST_PARSE_BIN_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_PARSE_BIN,GstParseBinClass))
132 #define GST_IS_parse_bin(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_PARSE_BIN))
133 #define GST_IS_parse_bin_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_PARSE_BIN))
138 * The opaque #GstParseBin data structure
142 GstBin bin; /* we extend GstBin */
145 gchar *encoding; /* encoding of subtitles */
146 guint64 connection_speed;
148 GstElement *typefind; /* this holds the typefind object */
150 GMutex expose_lock; /* Protects exposal and removal of groups */
151 GstParseChain *parse_chain; /* Top level parse chain */
152 guint nbpads; /* unique identifier for source pads */
154 GMutex factories_lock;
155 guint32 factories_cookie; /* Cookie from last time when factories was updated */
156 GList *factories; /* factories we can use for selecting elements */
158 GMutex subtitle_lock; /* Protects changes to subtitles and encoding */
159 GList *subtitles; /* List of elements with subtitle-encoding,
160 * protected by above mutex! */
162 gboolean have_type; /* if we received the have_type signal */
163 guint have_type_id; /* signal id for have-type from typefind */
165 gboolean async_pending; /* async-start has been emitted */
167 GMutex dyn_lock; /* lock protecting pad blocking */
168 gboolean shutdown; /* if we are shutting down */
169 GList *blocked_pads; /* pads that have set to block */
171 gboolean expose_allstreams; /* Whether to expose unknow type streams or not */
173 GList *filtered; /* elements for which error messages are filtered */
174 GList *filtered_errors; /* filtered error messages */
177 struct _GstParseBinClass
179 GstBinClass parent_class;
181 /* signal fired when we found a pad that we cannot decode */
182 void (*unknown_type) (GstElement * element, GstPad * pad, GstCaps * caps);
184 /* signal fired to know if we continue trying to decode the given caps */
185 gboolean (*autoplug_continue) (GstElement * element, GstPad * pad,
187 /* signal fired to get a list of factories to try to autoplug */
188 GValueArray *(*autoplug_factories) (GstElement * element, GstPad * pad,
190 /* signal fired to sort the factories */
191 GValueArray *(*autoplug_sort) (GstElement * element, GstPad * pad,
192 GstCaps * caps, GValueArray * factories);
193 /* signal fired to select from the proposed list of factories */
194 GstAutoplugSelectResult (*autoplug_select) (GstElement * element,
195 GstPad * pad, GstCaps * caps, GstElementFactory * factory);
196 /* signal fired when a autoplugged element that is not linked downstream
197 * or exposed wants to query something */
198 gboolean (*autoplug_query) (GstElement * element, GstPad * pad,
201 /* fired when the last group is drained */
202 void (*drained) (GstElement * element);
209 SIGNAL_AUTOPLUG_CONTINUE,
210 SIGNAL_AUTOPLUG_FACTORIES,
211 SIGNAL_AUTOPLUG_SELECT,
212 SIGNAL_AUTOPLUG_SORT,
213 SIGNAL_AUTOPLUG_QUERY,
218 #define DEFAULT_SUBTITLE_ENCODING NULL
219 #define DEFAULT_USE_BUFFERING FALSE
220 #define DEFAULT_LOW_PERCENT 10
221 #define DEFAULT_HIGH_PERCENT 99
222 /* by default we use the automatic values above */
223 #define DEFAULT_EXPOSE_ALL_STREAMS TRUE
224 #define DEFAULT_CONNECTION_SPEED 0
230 PROP_SUBTITLE_ENCODING,
232 PROP_EXPOSE_ALL_STREAMS,
233 PROP_CONNECTION_SPEED
236 static GstBinClass *parent_class;
237 static guint gst_parse_bin_signals[LAST_SIGNAL] = { 0 };
239 static void do_async_start (GstParseBin * parsebin);
240 static void do_async_done (GstParseBin * parsebin);
242 static void type_found (GstElement * typefind, guint probability,
243 GstCaps * caps, GstParseBin * parse_bin);
245 static gboolean gst_parse_bin_autoplug_continue (GstElement * element,
246 GstPad * pad, GstCaps * caps);
247 static GValueArray *gst_parse_bin_autoplug_factories (GstElement *
248 element, GstPad * pad, GstCaps * caps);
249 static GValueArray *gst_parse_bin_autoplug_sort (GstElement * element,
250 GstPad * pad, GstCaps * caps, GValueArray * factories);
251 static GstAutoplugSelectResult gst_parse_bin_autoplug_select (GstElement *
252 element, GstPad * pad, GstCaps * caps, GstElementFactory * factory);
253 static gboolean gst_parse_bin_autoplug_query (GstElement * element,
254 GstPad * pad, GstQuery * query);
256 static void gst_parse_bin_set_property (GObject * object, guint prop_id,
257 const GValue * value, GParamSpec * pspec);
258 static void gst_parse_bin_get_property (GObject * object, guint prop_id,
259 GValue * value, GParamSpec * pspec);
260 static void caps_notify_cb (GstPad * pad, GParamSpec * unused,
261 GstParseChain * chain);
263 static GstStateChangeReturn gst_parse_bin_change_state (GstElement * element,
264 GstStateChange transition);
265 static void gst_parse_bin_handle_message (GstBin * bin, GstMessage * message);
266 static void gst_parse_pad_update_caps (GstParsePad * parsepad, GstCaps * caps);
267 static void gst_parse_pad_update_tags (GstParsePad * parsepad,
269 static GstEvent *gst_parse_pad_stream_start_event (GstParsePad * parsepad,
271 static void gst_parse_pad_update_stream_collection (GstParsePad * parsepad,
272 GstStreamCollection * collection);
274 static GstCaps *get_pad_caps (GstPad * pad);
276 #define EXPOSE_LOCK(parsebin) G_STMT_START { \
277 GST_LOG_OBJECT (parsebin, \
278 "expose locking from thread %p", \
280 g_mutex_lock (&GST_PARSE_BIN_CAST(parsebin)->expose_lock); \
281 GST_LOG_OBJECT (parsebin, \
282 "expose locked from thread %p", \
286 #define EXPOSE_UNLOCK(parsebin) G_STMT_START { \
287 GST_LOG_OBJECT (parsebin, \
288 "expose unlocking from thread %p", \
290 g_mutex_unlock (&GST_PARSE_BIN_CAST(parsebin)->expose_lock); \
293 #define DYN_LOCK(parsebin) G_STMT_START { \
294 GST_LOG_OBJECT (parsebin, \
295 "dynlocking from thread %p", \
297 g_mutex_lock (&GST_PARSE_BIN_CAST(parsebin)->dyn_lock); \
298 GST_LOG_OBJECT (parsebin, \
299 "dynlocked from thread %p", \
303 #define DYN_UNLOCK(parsebin) G_STMT_START { \
304 GST_LOG_OBJECT (parsebin, \
305 "dynunlocking from thread %p", \
307 g_mutex_unlock (&GST_PARSE_BIN_CAST(parsebin)->dyn_lock); \
310 #define SUBTITLE_LOCK(parsebin) G_STMT_START { \
311 GST_LOG_OBJECT (parsebin, \
312 "subtitle locking from thread %p", \
314 g_mutex_lock (&GST_PARSE_BIN_CAST(parsebin)->subtitle_lock); \
315 GST_LOG_OBJECT (parsebin, \
316 "subtitle lock from thread %p", \
320 #define SUBTITLE_UNLOCK(parsebin) G_STMT_START { \
321 GST_LOG_OBJECT (parsebin, \
322 "subtitle unlocking from thread %p", \
324 g_mutex_unlock (&GST_PARSE_BIN_CAST(parsebin)->subtitle_lock); \
327 struct _GstPendingPad
330 GstParseChain *chain;
331 gulong event_probe_id;
332 gulong notify_caps_id;
335 struct _GstParseElement
338 GstElement *capsfilter; /* Optional capsfilter for Parser/Convert */
340 gulong pad_removed_id;
341 gulong no_more_pads_id;
346 * Streams belonging to the same group/chain of a media file
348 * When changing something here lock the parent chain!
350 struct _GstParseGroup
352 GstParseBin *parsebin;
353 GstParseChain *parent;
355 gboolean no_more_pads; /* TRUE if the demuxer signaled no-more-pads */
356 gboolean drained; /* TRUE if the all children are drained */
358 GList *children; /* List of GstParseChains in this group */
361 struct _GstParseChain
363 GstParseGroup *parent;
364 GstParseBin *parsebin;
366 GMutex lock; /* Protects this chain and its groups */
368 GstPad *pad; /* srcpad that caused creation of this chain */
369 GstCaps *start_caps; /* The initial caps of this chain */
371 gboolean drained; /* TRUE if the all children are drained */
372 gboolean demuxer; /* TRUE if elements->data is a demuxer */
373 gboolean parsed; /* TRUE if any elements are a parser */
374 GList *elements; /* All elements in this group, first
375 is the latest and most downstream element */
377 /* Note: there are only groups if the last element of this chain
378 * is a demuxer, otherwise the chain will end with an endpad.
379 * The other way around this means, that endpad only exists if this
380 * chain doesn't end with a demuxer! */
382 GstParseGroup *active_group; /* Currently active group */
383 GList *next_groups; /* head is newest group, tail is next group.
384 a new group will be created only if the head
385 group had no-more-pads. If it's only exposed
386 all new pads will be ignored! */
387 GList *pending_pads; /* Pads that have no fixed caps yet */
389 GstParsePad *current_pad; /* Current ending pad of the chain that can't
390 * be exposed yet but would be the same as endpad
391 * once it can be exposed */
392 GstParsePad *endpad; /* Pad of this chain that could be exposed */
393 gboolean deadend; /* This chain is incomplete and can't be completed,
394 e.g. no suitable decoder could be found
395 e.g. stream got EOS without buffers
397 gchar *deadend_details;
398 GstCaps *endcaps; /* Caps that were used when linking to the endpad
399 or that resulted in the deadend
402 /* FIXME: This should be done directly via a thread! */
403 GList *old_groups; /* Groups that should be freed later */
406 static void gst_parse_chain_free (GstParseChain * chain);
407 static GstParseChain *gst_parse_chain_new (GstParseBin * parsebin,
408 GstParseGroup * group, GstPad * pad, GstCaps * start_caps);
409 static void gst_parse_group_hide (GstParseGroup * group);
410 static void gst_parse_group_free (GstParseGroup * group);
411 static GstParseGroup *gst_parse_group_new (GstParseBin * parsebin,
412 GstParseChain * chain);
413 static gboolean gst_parse_chain_is_complete (GstParseChain * chain);
414 static gboolean gst_parse_chain_expose (GstParseChain * chain,
415 GList ** endpads, gboolean * missing_plugin,
416 GString * missing_plugin_details, gboolean * last_group,
417 gboolean * uncollected_streams);
418 static void build_fallback_collection (GstParseChain * chain,
419 GstStreamCollection * collection);
420 static gboolean gst_parse_chain_is_drained (GstParseChain * chain);
421 static gboolean gst_parse_group_is_complete (GstParseGroup * group);
422 static gboolean gst_parse_group_is_drained (GstParseGroup * group);
424 static gboolean gst_parse_bin_expose (GstParseBin * parsebin);
426 #define CHAIN_MUTEX_LOCK(chain) G_STMT_START { \
427 GST_LOG_OBJECT (chain->parsebin, \
428 "locking chain %p from thread %p", \
429 chain, g_thread_self ()); \
430 g_mutex_lock (&chain->lock); \
431 GST_LOG_OBJECT (chain->parsebin, \
432 "locked chain %p from thread %p", \
433 chain, g_thread_self ()); \
436 #define CHAIN_MUTEX_UNLOCK(chain) G_STMT_START { \
437 GST_LOG_OBJECT (chain->parsebin, \
438 "unlocking chain %p from thread %p", \
439 chain, g_thread_self ()); \
440 g_mutex_unlock (&chain->lock); \
445 * GstPad private used for source pads of chains
450 GstParseBin *parsebin;
451 GstParseChain *chain;
453 gboolean blocked; /* the *target* pad is blocked */
454 gboolean exposed; /* the pad is exposed */
455 gboolean drained; /* an EOS has been seen on the pad */
459 gboolean in_a_fallback_collection;
460 GstStreamCollection *active_collection;
461 GstStream *active_stream;
464 GType gst_parse_pad_get_type (void);
465 G_DEFINE_TYPE (GstParsePad, gst_parse_pad, GST_TYPE_GHOST_PAD);
466 #define GST_TYPE_PARSE_PAD (gst_parse_pad_get_type ())
467 #define GST_PARSE_PAD(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_PARSE_PAD,GstParsePad))
469 static GstParsePad *gst_parse_pad_new (GstParseBin * parsebin,
470 GstParseChain * chain);
471 static void gst_parse_pad_activate (GstParsePad * parsepad,
472 GstParseChain * chain);
473 static void gst_parse_pad_unblock (GstParsePad * parsepad);
474 static void gst_parse_pad_set_blocked (GstParsePad * parsepad,
476 static gboolean gst_parse_pad_query (GstPad * pad, GstObject * parent,
478 static GstPadProbeReturn
479 gst_parse_pad_event (GstPad * pad, GstPadProbeInfo * info, gpointer user_data);
482 static void gst_pending_pad_free (GstPendingPad * ppad);
483 static GstPadProbeReturn pad_event_cb (GstPad * pad, GstPadProbeInfo * info,
486 /********************************
487 * Standard GObject boilerplate *
488 ********************************/
490 static void gst_parse_bin_class_init (GstParseBinClass * klass);
491 static void gst_parse_bin_init (GstParseBin * parse_bin);
492 static void gst_parse_bin_dispose (GObject * object);
493 static void gst_parse_bin_finalize (GObject * object);
496 gst_parse_bin_get_type (void)
498 static GType gst_parse_bin_type = 0;
500 if (!gst_parse_bin_type) {
501 static const GTypeInfo gst_parse_bin_info = {
502 sizeof (GstParseBinClass),
505 (GClassInitFunc) gst_parse_bin_class_init,
508 sizeof (GstParseBin),
510 (GInstanceInitFunc) gst_parse_bin_init,
515 g_type_register_static (GST_TYPE_BIN, "GstParseBin",
516 &gst_parse_bin_info, 0);
519 return gst_parse_bin_type;
523 _gst_boolean_accumulator (GSignalInvocationHint * ihint,
524 GValue * return_accu, const GValue * handler_return, gpointer dummy)
528 myboolean = g_value_get_boolean (handler_return);
529 if (!(ihint->run_type & G_SIGNAL_RUN_CLEANUP))
530 g_value_set_boolean (return_accu, myboolean);
532 /* stop emission if FALSE */
537 _gst_boolean_or_accumulator (GSignalInvocationHint * ihint,
538 GValue * return_accu, const GValue * handler_return, gpointer dummy)
543 myboolean = g_value_get_boolean (handler_return);
544 retboolean = g_value_get_boolean (return_accu);
546 if (!(ihint->run_type & G_SIGNAL_RUN_CLEANUP))
547 g_value_set_boolean (return_accu, myboolean || retboolean);
552 /* we collect the first result */
554 _gst_array_accumulator (GSignalInvocationHint * ihint,
555 GValue * return_accu, const GValue * handler_return, gpointer dummy)
559 array = g_value_get_boxed (handler_return);
560 if (!(ihint->run_type & G_SIGNAL_RUN_CLEANUP))
561 g_value_set_boxed (return_accu, array);
567 _gst_select_accumulator (GSignalInvocationHint * ihint,
568 GValue * return_accu, const GValue * handler_return, gpointer dummy)
570 GstAutoplugSelectResult res;
572 res = g_value_get_enum (handler_return);
573 if (!(ihint->run_type & G_SIGNAL_RUN_CLEANUP))
574 g_value_set_enum (return_accu, res);
576 /* Call the next handler in the chain (if any) when the current callback
577 * returns TRY. This makes it possible to register separate autoplug-select
578 * handlers that implement different TRY/EXPOSE/SKIP strategies.
580 if (res == GST_AUTOPLUG_SELECT_TRY)
587 _gst_array_hasvalue_accumulator (GSignalInvocationHint * ihint,
588 GValue * return_accu, const GValue * handler_return, gpointer dummy)
592 array = g_value_get_boxed (handler_return);
593 if (!(ihint->run_type & G_SIGNAL_RUN_CLEANUP))
594 g_value_set_boxed (return_accu, array);
603 gst_parse_bin_class_init (GstParseBinClass * klass)
605 GObjectClass *gobject_klass;
606 GstElementClass *gstelement_klass;
607 GstBinClass *gstbin_klass;
609 gobject_klass = (GObjectClass *) klass;
610 gstelement_klass = (GstElementClass *) klass;
611 gstbin_klass = (GstBinClass *) klass;
613 parent_class = g_type_class_peek_parent (klass);
615 gobject_klass->dispose = gst_parse_bin_dispose;
616 gobject_klass->finalize = gst_parse_bin_finalize;
617 gobject_klass->set_property = gst_parse_bin_set_property;
618 gobject_klass->get_property = gst_parse_bin_get_property;
621 * GstParseBin::unknown-type:
622 * @bin: The ParseBin.
623 * @pad: The new pad containing caps that cannot be resolved to a 'final'
625 * @caps: The #GstCaps of the pad that cannot be resolved.
627 * This signal is emitted when a pad for which there is no further possible
628 * decoding is added to the ParseBin.
630 gst_parse_bin_signals[SIGNAL_UNKNOWN_TYPE] =
631 g_signal_new ("unknown-type", G_TYPE_FROM_CLASS (klass),
632 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstParseBinClass, unknown_type),
633 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 2,
634 GST_TYPE_PAD, GST_TYPE_CAPS);
637 * GstParseBin::autoplug-continue:
638 * @bin: The ParseBin.
640 * @caps: The #GstCaps found.
642 * This signal is emitted whenever ParseBin finds a new stream. It is
643 * emitted before looking for any elements that can handle that stream.
646 * Invocation of signal handlers stops after the first signal handler
647 * returns #FALSE. Signal handlers are invoked in the order they were
651 * Returns: #TRUE if you wish ParseBin to look for elements that can
652 * handle the given @caps. If #FALSE, those caps will be considered as
653 * final and the pad will be exposed as such (see 'pad-added' signal of
656 gst_parse_bin_signals[SIGNAL_AUTOPLUG_CONTINUE] =
657 g_signal_new ("autoplug-continue", G_TYPE_FROM_CLASS (klass),
658 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstParseBinClass, autoplug_continue),
659 _gst_boolean_accumulator, NULL, g_cclosure_marshal_generic,
660 G_TYPE_BOOLEAN, 2, GST_TYPE_PAD, GST_TYPE_CAPS);
663 * GstParseBin::autoplug-factories:
664 * @bin: The ParseBin.
666 * @caps: The #GstCaps found.
668 * This function is emited when an array of possible factories for @caps on
669 * @pad is needed. ParseBin will by default return an array with all
670 * compatible factories, sorted by rank.
672 * If this function returns NULL, @pad will be exposed as a final caps.
674 * If this function returns an empty array, the pad will be considered as
675 * having an unhandled type media type.
678 * Only the signal handler that is connected first will ever by invoked.
679 * Don't connect signal handlers with the #G_CONNECT_AFTER flag to this
680 * signal, they will never be invoked!
683 * Returns: a #GValueArray* with a list of factories to try. The factories are
684 * by default tried in the returned order or based on the index returned by
687 gst_parse_bin_signals[SIGNAL_AUTOPLUG_FACTORIES] =
688 g_signal_new ("autoplug-factories", G_TYPE_FROM_CLASS (klass),
689 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstParseBinClass,
690 autoplug_factories), _gst_array_accumulator, NULL,
691 g_cclosure_marshal_generic, G_TYPE_VALUE_ARRAY, 2,
692 GST_TYPE_PAD, GST_TYPE_CAPS);
695 * GstParseBin::autoplug-sort:
696 * @bin: The ParseBin.
698 * @caps: The #GstCaps.
699 * @factories: A #GValueArray of possible #GstElementFactory to use.
701 * Once ParseBin has found the possible #GstElementFactory objects to try
702 * for @caps on @pad, this signal is emited. The purpose of the signal is for
703 * the application to perform additional sorting or filtering on the element
706 * The callee should copy and modify @factories or return #NULL if the
707 * order should not change.
710 * Invocation of signal handlers stops after one signal handler has
711 * returned something else than #NULL. Signal handlers are invoked in
712 * the order they were connected in.
713 * Don't connect signal handlers with the #G_CONNECT_AFTER flag to this
714 * signal, they will never be invoked!
717 * Returns: A new sorted array of #GstElementFactory objects.
719 gst_parse_bin_signals[SIGNAL_AUTOPLUG_SORT] =
720 g_signal_new ("autoplug-sort", G_TYPE_FROM_CLASS (klass),
721 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstParseBinClass, autoplug_sort),
722 _gst_array_hasvalue_accumulator, NULL,
723 g_cclosure_marshal_generic, G_TYPE_VALUE_ARRAY, 3, GST_TYPE_PAD,
724 GST_TYPE_CAPS, G_TYPE_VALUE_ARRAY | G_SIGNAL_TYPE_STATIC_SCOPE);
727 * GstParseBin::autoplug-select:
728 * @bin: The ParseBin.
730 * @caps: The #GstCaps.
731 * @factory: A #GstElementFactory to use.
733 * This signal is emitted once ParseBin has found all the possible
734 * #GstElementFactory that can be used to handle the given @caps. For each of
735 * those factories, this signal is emitted.
737 * The signal handler should return a #GST_TYPE_AUTOPLUG_SELECT_RESULT enum
738 * value indicating what ParseBin should do next.
740 * A value of #GST_AUTOPLUG_SELECT_TRY will try to autoplug an element from
743 * A value of #GST_AUTOPLUG_SELECT_EXPOSE will expose @pad without plugging
746 * A value of #GST_AUTOPLUG_SELECT_SKIP will skip @factory and move to the
750 * The signal handler will not be invoked if any of the previously
751 * registered signal handlers (if any) return a value other than
752 * GST_AUTOPLUG_SELECT_TRY. Which also means that if you return
753 * GST_AUTOPLUG_SELECT_TRY from one signal handler, handlers that get
754 * registered next (again, if any) can override that decision.
757 * Returns: a #GST_TYPE_AUTOPLUG_SELECT_RESULT that indicates the required
758 * operation. the default handler will always return
759 * #GST_AUTOPLUG_SELECT_TRY.
761 gst_parse_bin_signals[SIGNAL_AUTOPLUG_SELECT] =
762 g_signal_new ("autoplug-select", G_TYPE_FROM_CLASS (klass),
763 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstParseBinClass, autoplug_select),
764 _gst_select_accumulator, NULL,
765 g_cclosure_marshal_generic,
766 GST_TYPE_AUTOPLUG_SELECT_RESULT, 3, GST_TYPE_PAD, GST_TYPE_CAPS,
767 GST_TYPE_ELEMENT_FACTORY);
770 * GstParseBin::autoplug-query:
771 * @bin: The ParseBin.
772 * @child: The child element doing the query
774 * @element: The #GstElement.
775 * @query: The #GstQuery.
777 * This signal is emitted whenever an autoplugged element that is
778 * not linked downstream yet and not exposed does a query. It can
779 * be used to tell the element about the downstream supported caps
782 * Returns: #TRUE if the query was handled, #FALSE otherwise.
784 gst_parse_bin_signals[SIGNAL_AUTOPLUG_QUERY] =
785 g_signal_new ("autoplug-query", G_TYPE_FROM_CLASS (klass),
786 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstParseBinClass, autoplug_query),
787 _gst_boolean_or_accumulator, NULL, g_cclosure_marshal_generic,
788 G_TYPE_BOOLEAN, 3, GST_TYPE_PAD, GST_TYPE_ELEMENT,
789 GST_TYPE_QUERY | G_SIGNAL_TYPE_STATIC_SCOPE);
792 * GstParseBin::drained
795 * This signal is emitted once ParseBin has finished decoding all the data.
797 gst_parse_bin_signals[SIGNAL_DRAINED] =
798 g_signal_new ("drained", G_TYPE_FROM_CLASS (klass),
799 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstParseBinClass, drained),
800 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 0, G_TYPE_NONE);
802 g_object_class_install_property (gobject_klass, PROP_SUBTITLE_ENCODING,
803 g_param_spec_string ("subtitle-encoding", "subtitle encoding",
804 "Encoding to assume if input subtitles are not in UTF-8 encoding. "
805 "If not set, the GST_SUBTITLE_ENCODING environment variable will "
806 "be checked for an encoding to use. If that is not set either, "
807 "ISO-8859-15 will be assumed.", NULL,
808 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
810 g_object_class_install_property (gobject_klass, PROP_SINK_CAPS,
811 g_param_spec_boxed ("sink-caps", "Sink Caps",
812 "The caps of the input data. (NULL = use typefind element)",
813 GST_TYPE_CAPS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
816 * GstParseBin::expose-all-streams
818 * Expose streams of unknown type.
820 * If set to %FALSE, then only the streams that can be decoded to the final
821 * caps (see 'caps' property) will have a pad exposed. Streams that do not
822 * match those caps but could have been decoded will not have decoder plugged
823 * in internally and will not have a pad exposed.
825 g_object_class_install_property (gobject_klass, PROP_EXPOSE_ALL_STREAMS,
826 g_param_spec_boolean ("expose-all-streams", "Expose All Streams",
827 "Expose all streams, including those of unknown type or that don't match the 'caps' property",
828 DEFAULT_EXPOSE_ALL_STREAMS,
829 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
832 * GstParseBin2::connection-speed
834 * Network connection speed in kbps (0 = unknownw)
836 g_object_class_install_property (gobject_klass, PROP_CONNECTION_SPEED,
837 g_param_spec_uint64 ("connection-speed", "Connection Speed",
838 "Network connection speed in kbps (0 = unknown)",
839 0, G_MAXUINT64 / 1000, DEFAULT_CONNECTION_SPEED,
840 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
844 klass->autoplug_continue =
845 GST_DEBUG_FUNCPTR (gst_parse_bin_autoplug_continue);
846 klass->autoplug_factories =
847 GST_DEBUG_FUNCPTR (gst_parse_bin_autoplug_factories);
848 klass->autoplug_sort = GST_DEBUG_FUNCPTR (gst_parse_bin_autoplug_sort);
849 klass->autoplug_select = GST_DEBUG_FUNCPTR (gst_parse_bin_autoplug_select);
850 klass->autoplug_query = GST_DEBUG_FUNCPTR (gst_parse_bin_autoplug_query);
852 gst_element_class_add_pad_template (gstelement_klass,
853 gst_static_pad_template_get (&decoder_bin_sink_template));
854 gst_element_class_add_pad_template (gstelement_klass,
855 gst_static_pad_template_get (&decoder_bin_src_template));
857 gst_element_class_set_static_metadata (gstelement_klass,
858 "Parse Bin", "Generic/Bin/Parser",
859 "Parse and de-multiplex to elementary stream",
860 "Jan Schmidt <jan@centricular.com>, "
861 "Edward Hervey <edward@centricular.com>");
863 gstelement_klass->change_state =
864 GST_DEBUG_FUNCPTR (gst_parse_bin_change_state);
866 gstbin_klass->handle_message =
867 GST_DEBUG_FUNCPTR (gst_parse_bin_handle_message);
869 g_type_class_ref (GST_TYPE_PARSE_PAD);
873 _parse_bin_compare_factories_func (gconstpointer p1, gconstpointer p2)
875 GstPluginFeature *f1, *f2;
876 gboolean is_parser1, is_parser2;
878 f1 = (GstPluginFeature *) p1;
879 f2 = (GstPluginFeature *) p2;
881 is_parser1 = gst_element_factory_list_is_type (GST_ELEMENT_FACTORY_CAST (f1),
882 GST_ELEMENT_FACTORY_TYPE_PARSER);
883 is_parser2 = gst_element_factory_list_is_type (GST_ELEMENT_FACTORY_CAST (f2),
884 GST_ELEMENT_FACTORY_TYPE_PARSER);
887 /* We want all parsers first as we always want to plug parsers
889 if (is_parser1 && !is_parser2)
891 else if (!is_parser1 && is_parser2)
894 /* And if it's a both a parser we first sort by rank
895 * and then by factory name */
896 return gst_plugin_feature_rank_compare_func (p1, p2);
899 /* Must be called with factories lock! */
901 gst_parse_bin_update_factories_list (GstParseBin * parsebin)
905 cookie = gst_registry_get_feature_list_cookie (gst_registry_get ());
906 if (!parsebin->factories || parsebin->factories_cookie != cookie) {
907 if (parsebin->factories)
908 gst_plugin_feature_list_free (parsebin->factories);
909 parsebin->factories =
910 gst_element_factory_list_get_elements
911 (GST_ELEMENT_FACTORY_TYPE_DECODABLE, GST_RANK_MARGINAL);
912 parsebin->factories =
913 g_list_sort (parsebin->factories, _parse_bin_compare_factories_func);
914 parsebin->factories_cookie = cookie;
919 gst_parse_bin_init (GstParseBin * parse_bin)
921 /* first filter out the interesting element factories */
922 g_mutex_init (&parse_bin->factories_lock);
924 /* we create the typefind element only once */
925 parse_bin->typefind = gst_element_factory_make ("typefind", "typefind");
926 if (!parse_bin->typefind) {
927 g_warning ("can't find typefind element, ParseBin will not work");
931 GstPadTemplate *pad_tmpl;
933 /* add the typefind element */
934 if (!gst_bin_add (GST_BIN (parse_bin), parse_bin->typefind)) {
935 g_warning ("Could not add typefind element, ParseBin will not work");
936 gst_object_unref (parse_bin->typefind);
937 parse_bin->typefind = NULL;
940 /* get the sinkpad */
941 pad = gst_element_get_static_pad (parse_bin->typefind, "sink");
943 /* get the pad template */
944 pad_tmpl = gst_static_pad_template_get (&decoder_bin_sink_template);
946 /* ghost the sink pad to ourself */
947 gpad = gst_ghost_pad_new_from_template ("sink", pad, pad_tmpl);
948 gst_pad_set_active (gpad, TRUE);
949 gst_element_add_pad (GST_ELEMENT (parse_bin), gpad);
951 gst_object_unref (pad_tmpl);
952 gst_object_unref (pad);
955 g_mutex_init (&parse_bin->expose_lock);
956 parse_bin->parse_chain = NULL;
958 g_mutex_init (&parse_bin->dyn_lock);
959 parse_bin->shutdown = FALSE;
960 parse_bin->blocked_pads = NULL;
962 g_mutex_init (&parse_bin->subtitle_lock);
964 parse_bin->encoding = g_strdup (DEFAULT_SUBTITLE_ENCODING);
966 parse_bin->expose_allstreams = DEFAULT_EXPOSE_ALL_STREAMS;
967 parse_bin->connection_speed = DEFAULT_CONNECTION_SPEED;
971 gst_parse_bin_dispose (GObject * object)
973 GstParseBin *parse_bin;
975 parse_bin = GST_PARSE_BIN (object);
977 if (parse_bin->factories)
978 gst_plugin_feature_list_free (parse_bin->factories);
979 parse_bin->factories = NULL;
981 if (parse_bin->parse_chain)
982 gst_parse_chain_free (parse_bin->parse_chain);
983 parse_bin->parse_chain = NULL;
985 g_free (parse_bin->encoding);
986 parse_bin->encoding = NULL;
988 g_list_free (parse_bin->subtitles);
989 parse_bin->subtitles = NULL;
991 G_OBJECT_CLASS (parent_class)->dispose (object);
995 gst_parse_bin_finalize (GObject * object)
997 GstParseBin *parse_bin;
999 parse_bin = GST_PARSE_BIN (object);
1001 g_mutex_clear (&parse_bin->expose_lock);
1002 g_mutex_clear (&parse_bin->dyn_lock);
1003 g_mutex_clear (&parse_bin->subtitle_lock);
1004 g_mutex_clear (&parse_bin->factories_lock);
1006 G_OBJECT_CLASS (parent_class)->finalize (object);
1010 gst_parse_bin_set_sink_caps (GstParseBin * parsebin, GstCaps * caps)
1012 GST_DEBUG_OBJECT (parsebin, "Setting new caps: %" GST_PTR_FORMAT, caps);
1014 g_object_set (parsebin->typefind, "force-caps", caps, NULL);
1018 gst_parse_bin_get_sink_caps (GstParseBin * parsebin)
1022 GST_DEBUG_OBJECT (parsebin, "Getting currently set caps");
1024 g_object_get (parsebin->typefind, "force-caps", &caps, NULL);
1030 gst_parse_bin_set_subs_encoding (GstParseBin * parsebin, const gchar * encoding)
1034 GST_DEBUG_OBJECT (parsebin, "Setting new encoding: %s",
1035 GST_STR_NULL (encoding));
1037 SUBTITLE_LOCK (parsebin);
1038 g_free (parsebin->encoding);
1039 parsebin->encoding = g_strdup (encoding);
1041 /* set the subtitle encoding on all added elements */
1042 for (walk = parsebin->subtitles; walk; walk = g_list_next (walk)) {
1043 g_object_set (G_OBJECT (walk->data), "subtitle-encoding",
1044 parsebin->encoding, NULL);
1046 SUBTITLE_UNLOCK (parsebin);
1050 gst_parse_bin_get_subs_encoding (GstParseBin * parsebin)
1054 GST_DEBUG_OBJECT (parsebin, "Getting currently set encoding");
1056 SUBTITLE_LOCK (parsebin);
1057 encoding = g_strdup (parsebin->encoding);
1058 SUBTITLE_UNLOCK (parsebin);
1064 gst_parse_bin_set_property (GObject * object, guint prop_id,
1065 const GValue * value, GParamSpec * pspec)
1067 GstParseBin *parsebin;
1069 parsebin = GST_PARSE_BIN (object);
1072 case PROP_SUBTITLE_ENCODING:
1073 gst_parse_bin_set_subs_encoding (parsebin, g_value_get_string (value));
1075 case PROP_SINK_CAPS:
1076 gst_parse_bin_set_sink_caps (parsebin, g_value_get_boxed (value));
1078 case PROP_EXPOSE_ALL_STREAMS:
1079 parsebin->expose_allstreams = g_value_get_boolean (value);
1081 case PROP_CONNECTION_SPEED:
1082 GST_OBJECT_LOCK (parsebin);
1083 parsebin->connection_speed = g_value_get_uint64 (value) * 1000;
1084 GST_OBJECT_UNLOCK (parsebin);
1087 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1093 gst_parse_bin_get_property (GObject * object, guint prop_id,
1094 GValue * value, GParamSpec * pspec)
1096 GstParseBin *parsebin;
1098 parsebin = GST_PARSE_BIN (object);
1100 case PROP_SUBTITLE_ENCODING:
1101 g_value_take_string (value, gst_parse_bin_get_subs_encoding (parsebin));
1103 case PROP_SINK_CAPS:
1104 g_value_take_boxed (value, gst_parse_bin_get_sink_caps (parsebin));
1106 case PROP_EXPOSE_ALL_STREAMS:
1107 g_value_set_boolean (value, parsebin->expose_allstreams);
1109 case PROP_CONNECTION_SPEED:
1110 GST_OBJECT_LOCK (parsebin);
1111 g_value_set_uint64 (value, parsebin->connection_speed / 1000);
1112 GST_OBJECT_UNLOCK (parsebin);
1115 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1122 * Default autoplug signal handlers
1125 gst_parse_bin_autoplug_continue (GstElement * element, GstPad * pad,
1128 GST_DEBUG_OBJECT (element, "autoplug-continue returns TRUE");
1130 /* by default we always continue */
1134 static GValueArray *
1135 gst_parse_bin_autoplug_factories (GstElement * element, GstPad * pad,
1139 GValueArray *result;
1140 GstParseBin *parsebin = GST_PARSE_BIN_CAST (element);
1142 GST_DEBUG_OBJECT (element, "finding factories");
1144 /* return all compatible factories for caps */
1145 g_mutex_lock (&parsebin->factories_lock);
1146 gst_parse_bin_update_factories_list (parsebin);
1148 gst_element_factory_list_filter (parsebin->factories, caps, GST_PAD_SINK,
1149 gst_caps_is_fixed (caps));
1150 g_mutex_unlock (&parsebin->factories_lock);
1152 result = g_value_array_new (g_list_length (list));
1153 for (tmp = list; tmp; tmp = tmp->next) {
1154 GstElementFactory *factory = GST_ELEMENT_FACTORY_CAST (tmp->data);
1155 GValue val = { 0, };
1157 g_value_init (&val, G_TYPE_OBJECT);
1158 g_value_set_object (&val, factory);
1159 g_value_array_append (result, &val);
1160 g_value_unset (&val);
1162 gst_plugin_feature_list_free (list);
1164 GST_DEBUG_OBJECT (element, "autoplug-factories returns %p", result);
1169 static GValueArray *
1170 gst_parse_bin_autoplug_sort (GstElement * element, GstPad * pad,
1171 GstCaps * caps, GValueArray * factories)
1176 static GstAutoplugSelectResult
1177 gst_parse_bin_autoplug_select (GstElement * element, GstPad * pad,
1178 GstCaps * caps, GstElementFactory * factory)
1181 return GST_AUTOPLUG_SELECT_TRY;
1185 gst_parse_bin_autoplug_query (GstElement * element, GstPad * pad,
1188 /* No query handled here */
1196 static gboolean is_demuxer_element (GstElement * srcelement);
1198 static gboolean connect_pad (GstParseBin * parsebin, GstElement * src,
1199 GstParsePad * parsepad, GstPad * pad, GstCaps * caps,
1200 GValueArray * factories, GstParseChain * chain, gchar ** deadend_details);
1201 static GList *connect_element (GstParseBin * parsebin, GstParseElement * delem,
1202 GstParseChain * chain);
1203 static void expose_pad (GstParseBin * parsebin, GstElement * src,
1204 GstParsePad * parsepad, GstPad * pad, GstCaps * caps,
1205 GstParseChain * chain);
1207 static void pad_added_cb (GstElement * element, GstPad * pad,
1208 GstParseChain * chain);
1209 static void pad_removed_cb (GstElement * element, GstPad * pad,
1210 GstParseChain * chain);
1211 static void no_more_pads_cb (GstElement * element, GstParseChain * chain);
1213 static GstParseGroup *gst_parse_chain_get_current_group (GstParseChain * chain);
1216 clear_sticky_events (GstPad * pad, GstEvent ** event, gpointer user_data)
1218 GST_DEBUG_OBJECT (pad, "clearing sticky event %" GST_PTR_FORMAT, *event);
1219 gst_event_unref (*event);
1225 copy_sticky_events (GstPad * pad, GstEvent ** eventptr, gpointer user_data)
1227 GstParsePad *ppad = GST_PARSE_PAD (user_data);
1228 GstEvent *event = gst_event_ref (*eventptr);
1230 switch (GST_EVENT_TYPE (event)) {
1231 case GST_EVENT_CAPS:{
1232 GstCaps *caps = NULL;
1233 gst_event_parse_caps (event, &caps);
1234 gst_parse_pad_update_caps (ppad, caps);
1237 case GST_EVENT_STREAM_START:{
1238 event = gst_parse_pad_stream_start_event (ppad, event);
1241 case GST_EVENT_STREAM_COLLECTION:{
1242 GstStreamCollection *collection = NULL;
1243 gst_event_parse_stream_collection (event, &collection);
1244 gst_parse_pad_update_stream_collection (ppad, collection);
1251 GST_DEBUG_OBJECT (ppad, "store sticky event %" GST_PTR_FORMAT, event);
1252 gst_pad_store_sticky_event (GST_PAD_CAST (ppad), event);
1253 gst_event_unref (event);
1259 parse_pad_set_target (GstParsePad * parsepad, GstPad * target)
1261 GstPad *old_target = gst_ghost_pad_get_target (GST_GHOST_PAD_CAST (parsepad));
1263 gst_object_unref (old_target);
1265 if (old_target == target)
1268 gst_pad_sticky_events_foreach (GST_PAD_CAST (parsepad),
1269 clear_sticky_events, NULL);
1270 gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (parsepad), target);
1272 if (target == NULL) {
1273 GST_LOG_OBJECT (parsepad->parsebin, "Setting pad %" GST_PTR_FORMAT
1274 " target to NULL", parsepad);
1276 GST_LOG_OBJECT (parsepad->parsebin, "Setting pad %" GST_PTR_FORMAT
1277 " target to %" GST_PTR_FORMAT, parsepad, target);
1278 gst_pad_sticky_events_foreach (target, copy_sticky_events, parsepad);
1282 /* called when a new pad is discovered. It will perform some basic actions
1283 * before trying to link something to it.
1285 * - Check the caps, don't do anything when there are no caps or when they have
1287 * - signal AUTOPLUG_CONTINUE to check if we need to continue autoplugging this
1289 * - if the caps are non-fixed, setup a handler to continue autoplugging when
1290 * the caps become fixed (connect to notify::caps).
1291 * - get list of factories to autoplug.
1292 * - continue autoplugging to one of the factories.
1295 analyze_new_pad (GstParseBin * parsebin, GstElement * src, GstPad * pad,
1296 GstCaps * caps, GstParseChain * chain)
1298 gboolean apcontinue = TRUE;
1299 GValueArray *factories = NULL, *result = NULL;
1300 GstParsePad *parsepad;
1301 GstElementFactory *factory;
1302 const gchar *classification;
1303 gboolean is_parser_converter = FALSE;
1305 gchar *deadend_details = NULL;
1307 GST_DEBUG_OBJECT (parsebin, "Pad %s:%s caps:%" GST_PTR_FORMAT,
1308 GST_DEBUG_PAD_NAME (pad), caps);
1311 && src != ((GstParseElement *) chain->elements->data)->element
1312 && src != ((GstParseElement *) chain->elements->data)->capsfilter) {
1313 GST_ERROR_OBJECT (parsebin,
1314 "New pad from not the last element in this chain");
1318 if (chain->demuxer) {
1319 GstParseGroup *group;
1320 GstParseChain *oldchain = chain;
1321 GstParseElement *demux = (chain->elements ? chain->elements->data : NULL);
1323 if (chain->current_pad)
1324 gst_object_unref (chain->current_pad);
1325 chain->current_pad = NULL;
1327 /* we are adding a new pad for a demuxer (see is_demuxer_element(),
1328 * start a new chain for it */
1329 CHAIN_MUTEX_LOCK (oldchain);
1330 group = gst_parse_chain_get_current_group (chain);
1331 if (group && !g_list_find (group->children, chain)) {
1332 chain = gst_parse_chain_new (parsebin, group, pad, caps);
1333 group->children = g_list_prepend (group->children, chain);
1335 CHAIN_MUTEX_UNLOCK (oldchain);
1337 GST_WARNING_OBJECT (parsebin, "No current group");
1341 /* If this is not a dynamic pad demuxer, we're no-more-pads
1342 * already before anything else happens
1344 if (demux == NULL || !demux->no_more_pads_id)
1345 group->no_more_pads = TRUE;
1348 /* From here on we own a reference to the caps as
1349 * we might create new caps below and would need
1350 * to unref them later */
1352 gst_caps_ref (caps);
1354 if ((caps == NULL) || gst_caps_is_empty (caps))
1357 if (gst_caps_is_any (caps))
1360 if (!chain->current_pad)
1361 chain->current_pad = gst_parse_pad_new (parsebin, chain);
1363 parsepad = gst_object_ref (chain->current_pad);
1364 gst_pad_set_active (GST_PAD_CAST (parsepad), TRUE);
1365 parse_pad_set_target (parsepad, pad);
1367 /* 1. Emit 'autoplug-continue' the result will tell us if this pads needs
1368 * further autoplugging. Only do this for fixed caps, for unfixed caps
1369 * we will later come here again from the notify::caps handler. The
1370 * problem with unfixed caps is that, we can't reliably tell if the output
1371 * is e.g. accepted by a sink because only parts of the possible final
1372 * caps might be accepted by the sink. */
1373 if (gst_caps_is_fixed (caps))
1374 g_signal_emit (G_OBJECT (parsebin),
1375 gst_parse_bin_signals[SIGNAL_AUTOPLUG_CONTINUE], 0, parsepad, caps,
1380 /* 1.a if autoplug-continue is FALSE or caps is a raw format, goto pad_is_final */
1384 /* 1.b For Parser/Converter that can output different stream formats
1385 * we insert a capsfilter with the sorted caps of all possible next
1386 * elements and continue with the capsfilter srcpad */
1387 factory = gst_element_get_factory (src);
1389 gst_element_factory_get_metadata (factory, GST_ELEMENT_METADATA_KLASS);
1390 is_parser_converter = (strstr (classification, "Parser")
1391 && strstr (classification, "Converter"));
1393 /* FIXME: We just need to be sure that the next element is not a parser */
1394 /* 1.c when the caps are not fixed yet, we can't be sure what element to
1395 * connect. We delay autoplugging until the caps are fixed */
1396 if (!is_parser_converter && !gst_caps_is_fixed (caps)) {
1398 } else if (!is_parser_converter) {
1399 gst_caps_unref (caps);
1400 caps = gst_pad_get_current_caps (pad);
1402 GST_DEBUG_OBJECT (parsebin,
1403 "No final caps set yet, delaying autoplugging");
1404 gst_object_unref (parsepad);
1405 goto setup_caps_delay;
1409 /* 1.d else get the factories and if there's no compatible factory goto
1411 g_signal_emit (G_OBJECT (parsebin),
1412 gst_parse_bin_signals[SIGNAL_AUTOPLUG_FACTORIES], 0, parsepad, caps,
1415 /* NULL means that we can expose the pad */
1416 if (factories == NULL)
1419 /* if the array is empty, we have a type for which we have no decoder */
1420 if (factories->n_values == 0) {
1421 /* if not we have a unhandled type with no compatible factories */
1422 g_value_array_free (factories);
1423 gst_object_unref (parsepad);
1427 /* 1.e sort some more. */
1428 g_signal_emit (G_OBJECT (parsebin),
1429 gst_parse_bin_signals[SIGNAL_AUTOPLUG_SORT], 0, parsepad, caps, factories,
1432 g_value_array_free (factories);
1436 /* 1.g now get the factory template caps and insert the capsfilter if this
1437 * is a parser/converter
1439 if (is_parser_converter) {
1440 GstCaps *filter_caps;
1443 GstParseElement *delem;
1445 g_assert (chain->elements != NULL);
1446 delem = (GstParseElement *) chain->elements->data;
1448 filter_caps = gst_caps_new_empty ();
1449 for (i = 0; i < factories->n_values; i++) {
1450 GstElementFactory *factory =
1451 g_value_get_object (g_value_array_get_nth (factories, i));
1452 GstCaps *tcaps, *intersection;
1455 GST_DEBUG ("Trying factory %s",
1456 gst_plugin_feature_get_name (GST_PLUGIN_FEATURE (factory)));
1458 if (gst_element_get_factory (src) == factory ||
1459 gst_element_factory_list_is_type (factory,
1460 GST_ELEMENT_FACTORY_TYPE_PARSER)) {
1461 GST_DEBUG ("Skipping factory");
1465 for (tmps = gst_element_factory_get_static_pad_templates (factory); tmps;
1466 tmps = tmps->next) {
1467 GstStaticPadTemplate *st = (GstStaticPadTemplate *) tmps->data;
1468 if (st->direction != GST_PAD_SINK || st->presence != GST_PAD_ALWAYS)
1470 tcaps = gst_static_pad_template_get_caps (st);
1472 gst_caps_intersect_full (tcaps, caps, GST_CAPS_INTERSECT_FIRST);
1473 filter_caps = gst_caps_merge (filter_caps, intersection);
1474 gst_caps_unref (tcaps);
1478 /* Append the parser caps to prevent any not-negotiated errors */
1479 filter_caps = gst_caps_merge (filter_caps, gst_caps_ref (caps));
1481 delem->capsfilter = gst_element_factory_make ("capsfilter", NULL);
1482 g_object_set (G_OBJECT (delem->capsfilter), "caps", filter_caps, NULL);
1483 gst_caps_unref (filter_caps);
1484 gst_element_set_state (delem->capsfilter, GST_STATE_PAUSED);
1485 gst_bin_add (GST_BIN_CAST (parsebin), gst_object_ref (delem->capsfilter));
1487 parse_pad_set_target (parsepad, NULL);
1488 p = gst_element_get_static_pad (delem->capsfilter, "sink");
1489 gst_pad_link_full (pad, p, GST_PAD_LINK_CHECK_NOTHING);
1490 gst_object_unref (p);
1491 p = gst_element_get_static_pad (delem->capsfilter, "src");
1492 parse_pad_set_target (parsepad, p);
1495 gst_caps_unref (caps);
1497 caps = gst_pad_get_current_caps (pad);
1499 GST_DEBUG_OBJECT (parsebin,
1500 "No final caps set yet, delaying autoplugging");
1501 gst_object_unref (parsepad);
1502 g_value_array_free (factories);
1503 goto setup_caps_delay;
1507 /* 1.h else continue autoplugging something from the list. */
1508 GST_LOG_OBJECT (pad, "Let's continue discovery on this pad");
1510 connect_pad (parsebin, src, parsepad, pad, caps, factories, chain,
1513 /* Need to unref the capsfilter srcpad here if
1514 * we inserted a capsfilter */
1515 if (is_parser_converter)
1516 gst_object_unref (pad);
1518 gst_object_unref (parsepad);
1519 g_value_array_free (factories);
1524 gst_caps_unref (caps);
1530 GST_LOG_OBJECT (parsebin, "Pad is final. autoplug-continue:%d", apcontinue);
1531 expose_pad (parsebin, src, parsepad, pad, caps, chain);
1532 gst_object_unref (parsepad);
1533 gst_caps_unref (caps);
1539 GST_LOG_OBJECT (pad, "Unknown type, posting message and firing signal");
1541 chain->deadend_details = deadend_details;
1542 chain->deadend = TRUE;
1543 chain->endcaps = caps;
1544 gst_object_replace ((GstObject **) & chain->current_pad, NULL);
1546 gst_element_post_message (GST_ELEMENT_CAST (parsebin),
1547 gst_missing_decoder_message_new (GST_ELEMENT_CAST (parsebin), caps));
1549 g_signal_emit (G_OBJECT (parsebin),
1550 gst_parse_bin_signals[SIGNAL_UNKNOWN_TYPE], 0, pad, caps);
1552 /* Try to expose anything */
1553 EXPOSE_LOCK (parsebin);
1554 if (parsebin->parse_chain) {
1555 if (gst_parse_chain_is_complete (parsebin->parse_chain)) {
1556 gst_parse_bin_expose (parsebin);
1559 EXPOSE_UNLOCK (parsebin);
1561 if (src == parsebin->typefind) {
1562 if (!caps || gst_caps_is_empty (caps)) {
1563 GST_ELEMENT_ERROR (parsebin, STREAM, TYPE_NOT_FOUND,
1564 (_("Could not determine type of stream")), (NULL));
1566 do_async_done (parsebin);
1573 GST_DEBUG_OBJECT (pad, "pad has non-fixed caps delay autoplugging");
1574 gst_object_unref (parsepad);
1575 goto setup_caps_delay;
1580 GST_DEBUG_OBJECT (pad, "pad has ANY caps, delaying auto-plugging");
1581 goto setup_caps_delay;
1585 GstPendingPad *ppad;
1587 /* connect to caps notification */
1588 CHAIN_MUTEX_LOCK (chain);
1589 GST_LOG_OBJECT (parsebin, "Chain %p has now %d dynamic pads", chain,
1590 g_list_length (chain->pending_pads));
1591 ppad = g_slice_new0 (GstPendingPad);
1592 ppad->pad = gst_object_ref (pad);
1593 ppad->chain = chain;
1594 ppad->event_probe_id =
1595 gst_pad_add_probe (pad, GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM,
1596 pad_event_cb, ppad, NULL);
1597 chain->pending_pads = g_list_prepend (chain->pending_pads, ppad);
1598 ppad->notify_caps_id = g_signal_connect (pad, "notify::caps",
1599 G_CALLBACK (caps_notify_cb), chain);
1600 CHAIN_MUTEX_UNLOCK (chain);
1602 /* If we're here because we have a Parser/Converter
1603 * we have to unref the pad */
1604 if (is_parser_converter)
1605 gst_object_unref (pad);
1607 gst_caps_unref (caps);
1614 add_error_filter (GstParseBin * parsebin, GstElement * element)
1616 GST_OBJECT_LOCK (parsebin);
1617 parsebin->filtered = g_list_prepend (parsebin->filtered, element);
1618 GST_OBJECT_UNLOCK (parsebin);
1622 remove_error_filter (GstParseBin * parsebin, GstElement * element,
1623 GstMessage ** error)
1627 GST_OBJECT_LOCK (parsebin);
1628 parsebin->filtered = g_list_remove (parsebin->filtered, element);
1633 l = parsebin->filtered_errors;
1635 GstMessage *msg = l->data;
1637 if (GST_MESSAGE_SRC (msg) == GST_OBJECT_CAST (element)) {
1638 /* Get the last error of this element, i.e. the earliest */
1640 gst_message_replace (error, msg);
1641 gst_message_unref (msg);
1642 l = parsebin->filtered_errors =
1643 g_list_delete_link (parsebin->filtered_errors, l);
1648 GST_OBJECT_UNLOCK (parsebin);
1655 } SendStickyEventsData;
1658 send_sticky_event (GstPad * pad, GstEvent ** event, gpointer user_data)
1660 SendStickyEventsData *data = user_data;
1663 ret = gst_pad_send_event (data->peer, gst_event_ref (*event));
1671 send_sticky_events (GstParseBin * parsebin, GstPad * pad)
1673 SendStickyEventsData data;
1676 data.peer = gst_pad_get_peer (pad);
1678 gst_pad_sticky_events_foreach (pad, send_sticky_event, &data);
1680 gst_object_unref (data.peer);
1686 error_message_to_string (GstMessage * msg)
1689 gchar *debug, *message, *full_message;
1691 gst_message_parse_error (msg, &err, &debug);
1693 message = gst_error_get_message (err->domain, err->code);
1696 full_message = g_strdup_printf ("%s\n%s\n%s", message, err->message, debug);
1698 full_message = g_strdup_printf ("%s\n%s", message, err->message);
1702 g_clear_error (&err);
1704 return full_message;
1707 /* We consider elements as "simple demuxer" when they are a demuxer
1708 * with one and only one ALWAYS source pad.
1711 is_simple_demuxer_factory (GstElementFactory * factory)
1713 if (strstr (gst_element_factory_get_metadata (factory,
1714 GST_ELEMENT_METADATA_KLASS), "Demuxer")) {
1716 gint num_alway_srcpads = 0;
1718 for (tmp = gst_element_factory_get_static_pad_templates (factory);
1719 tmp; tmp = tmp->next) {
1720 GstStaticPadTemplate *template = tmp->data;
1722 if (template->direction == GST_PAD_SRC) {
1723 if (template->presence == GST_PAD_ALWAYS) {
1724 if (num_alway_srcpads >= 0)
1725 num_alway_srcpads++;
1727 num_alway_srcpads = -1;
1733 if (num_alway_srcpads == 1)
1742 * Try to connect the given pad to an element created from one of the factories,
1745 * Note that parsepad is ghosting pad, and so pad is linked; be sure to unset parsepad's
1746 * target before trying to link pad.
1748 * Returns TRUE if an element was properly created and linked
1751 connect_pad (GstParseBin * parsebin, GstElement * src, GstParsePad * parsepad,
1752 GstPad * pad, GstCaps * caps, GValueArray * factories,
1753 GstParseChain * chain, gchar ** deadend_details)
1755 gboolean res = FALSE;
1756 GString *error_details = NULL;
1758 g_return_val_if_fail (factories != NULL, FALSE);
1759 g_return_val_if_fail (factories->n_values > 0, FALSE);
1761 GST_DEBUG_OBJECT (parsebin,
1762 "pad %s:%s , chain:%p, %d factories, caps %" GST_PTR_FORMAT,
1763 GST_DEBUG_PAD_NAME (pad), chain, factories->n_values, caps);
1765 error_details = g_string_new ("");
1767 /* 2. Try to create an element and link to it */
1768 while (factories->n_values > 0) {
1769 GstAutoplugSelectResult ret;
1770 GstElementFactory *factory;
1771 GstParseElement *delem;
1772 GstElement *element;
1776 GList *to_connect = NULL;
1777 gboolean is_parser_converter = FALSE, is_simple_demuxer = FALSE;
1779 /* Set parsepad target to pad again, it might've been unset
1780 * below but we came back here because something failed
1782 parse_pad_set_target (parsepad, pad);
1784 /* take first factory */
1785 factory = g_value_get_object (g_value_array_get_nth (factories, 0));
1786 /* Remove selected factory from the list. */
1787 g_value_array_remove (factories, 0);
1789 GST_LOG_OBJECT (src, "trying factory %" GST_PTR_FORMAT, factory);
1791 /* Check if the caps are really supported by the factory. The
1792 * factory list is non-empty-subset filtered while caps
1793 * are only accepted by a pad if they are a subset of the
1796 * FIXME: Only do this for fixed caps here. Non-fixed caps
1797 * can happen if a Parser/Converter was autoplugged before
1798 * this. We then assume that it will be able to convert to
1799 * everything that the decoder would want.
1801 * A subset check will fail here because the parser caps
1802 * will be generic and while the decoder will only
1803 * support a subset of the parser caps.
1805 if (gst_caps_is_fixed (caps)) {
1806 const GList *templs;
1807 gboolean skip = FALSE;
1809 templs = gst_element_factory_get_static_pad_templates (factory);
1812 GstStaticPadTemplate *templ = (GstStaticPadTemplate *) templs->data;
1814 if (templ->direction == GST_PAD_SINK) {
1815 GstCaps *templcaps = gst_static_caps_get (&templ->static_caps);
1817 if (!gst_caps_is_subset (caps, templcaps)) {
1818 GST_DEBUG_OBJECT (src,
1819 "caps %" GST_PTR_FORMAT " not subset of %" GST_PTR_FORMAT, caps,
1821 gst_caps_unref (templcaps);
1826 gst_caps_unref (templcaps);
1828 templs = g_list_next (templs);
1834 /* If the factory is for a parser we first check if the factory
1835 * was already used for the current chain. If it was used already
1836 * we would otherwise create an infinite loop here because the
1837 * parser apparently accepts its own output as input.
1838 * This is only done for parsers because it's perfectly valid
1839 * to have other element classes after each other because a
1840 * parser is the only one that does not change the data. A
1841 * valid example for this would be multiple id3demux in a row.
1843 is_parser_converter = strstr (gst_element_factory_get_metadata (factory,
1844 GST_ELEMENT_METADATA_KLASS), "Parser") != NULL;
1845 is_simple_demuxer = is_simple_demuxer_factory (factory);
1847 if (is_parser_converter) {
1848 gboolean skip = FALSE;
1851 CHAIN_MUTEX_LOCK (chain);
1852 for (l = chain->elements; l; l = l->next) {
1853 GstParseElement *delem = (GstParseElement *) l->data;
1854 GstElement *otherelement = delem->element;
1856 if (gst_element_get_factory (otherelement) == factory) {
1862 if (!skip && chain->parent && chain->parent->parent) {
1863 GstParseChain *parent_chain = chain->parent->parent;
1864 GstParseElement *pelem =
1865 parent_chain->elements ? parent_chain->elements->data : NULL;
1867 if (pelem && gst_element_get_factory (pelem->element) == factory)
1870 CHAIN_MUTEX_UNLOCK (chain);
1872 GST_DEBUG_OBJECT (parsebin,
1873 "Skipping factory '%s' because it was already used in this chain",
1874 gst_plugin_feature_get_name (GST_PLUGIN_FEATURE_CAST (factory)));
1880 /* Expose pads if the next factory is a decoder */
1881 if (gst_element_factory_list_is_type (factory,
1882 GST_ELEMENT_FACTORY_TYPE_DECODER)) {
1883 ret = GST_AUTOPLUG_SELECT_EXPOSE;
1885 /* emit autoplug-select to see what we should do with it. */
1886 g_signal_emit (G_OBJECT (parsebin),
1887 gst_parse_bin_signals[SIGNAL_AUTOPLUG_SELECT],
1888 0, parsepad, caps, factory, &ret);
1892 case GST_AUTOPLUG_SELECT_TRY:
1893 GST_DEBUG_OBJECT (parsebin, "autoplug select requested try");
1895 case GST_AUTOPLUG_SELECT_EXPOSE:
1896 GST_DEBUG_OBJECT (parsebin, "autoplug select requested expose");
1897 /* expose the pad, we don't have the source element */
1898 expose_pad (parsebin, src, parsepad, pad, caps, chain);
1901 case GST_AUTOPLUG_SELECT_SKIP:
1902 GST_DEBUG_OBJECT (parsebin, "autoplug select requested skip");
1905 GST_WARNING_OBJECT (parsebin, "autoplug select returned unhandled %d",
1910 /* 2.0. Unlink pad */
1911 parse_pad_set_target (parsepad, NULL);
1913 /* 2.1. Try to create an element */
1914 if ((element = gst_element_factory_create (factory, NULL)) == NULL) {
1915 GST_WARNING_OBJECT (parsebin, "Could not create an element from %s",
1916 gst_plugin_feature_get_name (GST_PLUGIN_FEATURE (factory)));
1917 g_string_append_printf (error_details,
1918 "Could not create an element from %s\n",
1919 gst_plugin_feature_get_name (GST_PLUGIN_FEATURE (factory)));
1923 /* Filter errors, this will prevent the element from causing the pipeline
1924 * to error while we test it using READY state. */
1925 add_error_filter (parsebin, element);
1927 /* We don't yet want the bin to control the element's state */
1928 gst_element_set_locked_state (element, TRUE);
1930 /* ... add it ... */
1931 if (!(gst_bin_add (GST_BIN_CAST (parsebin), element))) {
1932 GST_WARNING_OBJECT (parsebin, "Couldn't add %s to the bin",
1933 GST_ELEMENT_NAME (element));
1934 remove_error_filter (parsebin, element, NULL);
1935 g_string_append_printf (error_details, "Couldn't add %s to the bin\n",
1936 GST_ELEMENT_NAME (element));
1937 gst_object_unref (element);
1941 /* Find its sink pad. */
1943 GST_OBJECT_LOCK (element);
1944 if (element->sinkpads != NULL)
1945 sinkpad = gst_object_ref (element->sinkpads->data);
1946 GST_OBJECT_UNLOCK (element);
1948 if (sinkpad == NULL) {
1949 GST_WARNING_OBJECT (parsebin, "Element %s doesn't have a sink pad",
1950 GST_ELEMENT_NAME (element));
1951 remove_error_filter (parsebin, element, NULL);
1952 g_string_append_printf (error_details,
1953 "Element %s doesn't have a sink pad", GST_ELEMENT_NAME (element));
1954 gst_bin_remove (GST_BIN (parsebin), element);
1958 /* ... and try to link */
1959 if ((gst_pad_link_full (pad, sinkpad,
1960 GST_PAD_LINK_CHECK_NOTHING)) != GST_PAD_LINK_OK) {
1961 GST_WARNING_OBJECT (parsebin, "Link failed on pad %s:%s",
1962 GST_DEBUG_PAD_NAME (sinkpad));
1963 remove_error_filter (parsebin, element, NULL);
1964 g_string_append_printf (error_details, "Link failed on pad %s:%s",
1965 GST_DEBUG_PAD_NAME (sinkpad));
1966 gst_object_unref (sinkpad);
1967 gst_bin_remove (GST_BIN (parsebin), element);
1971 /* ... activate it ... */
1972 if ((gst_element_set_state (element,
1973 GST_STATE_READY)) == GST_STATE_CHANGE_FAILURE) {
1974 GstMessage *error_msg;
1976 GST_WARNING_OBJECT (parsebin, "Couldn't set %s to READY",
1977 GST_ELEMENT_NAME (element));
1978 remove_error_filter (parsebin, element, &error_msg);
1981 gchar *error_string = error_message_to_string (error_msg);
1982 g_string_append_printf (error_details, "Couldn't set %s to READY:\n%s",
1983 GST_ELEMENT_NAME (element), error_string);
1984 gst_message_unref (error_msg);
1985 g_free (error_string);
1987 g_string_append_printf (error_details, "Couldn't set %s to READY",
1988 GST_ELEMENT_NAME (element));
1990 gst_object_unref (sinkpad);
1991 gst_bin_remove (GST_BIN (parsebin), element);
1995 /* check if we still accept the caps on the pad after setting
1996 * the element to READY */
1997 if (!gst_pad_query_accept_caps (sinkpad, caps)) {
1998 GstMessage *error_msg;
2000 GST_WARNING_OBJECT (parsebin, "Element %s does not accept caps",
2001 GST_ELEMENT_NAME (element));
2003 remove_error_filter (parsebin, element, &error_msg);
2006 gchar *error_string = error_message_to_string (error_msg);
2007 g_string_append_printf (error_details,
2008 "Element %s does not accept caps:\n%s", GST_ELEMENT_NAME (element),
2010 gst_message_unref (error_msg);
2011 g_free (error_string);
2013 g_string_append_printf (error_details,
2014 "Element %s does not accept caps", GST_ELEMENT_NAME (element));
2017 gst_element_set_state (element, GST_STATE_NULL);
2018 gst_object_unref (sinkpad);
2019 gst_bin_remove (GST_BIN (parsebin), element);
2023 gst_object_unref (sinkpad);
2024 GST_LOG_OBJECT (parsebin, "linked on pad %s:%s", GST_DEBUG_PAD_NAME (pad));
2026 CHAIN_MUTEX_LOCK (chain);
2027 delem = g_slice_new0 (GstParseElement);
2028 delem->element = gst_object_ref (element);
2029 delem->capsfilter = NULL;
2030 chain->elements = g_list_prepend (chain->elements, delem);
2031 chain->demuxer = is_demuxer_element (element);
2033 /* If we plugging a parser, mark the chain as parsed */
2034 chain->parsed |= is_parser_converter;
2036 CHAIN_MUTEX_UNLOCK (chain);
2038 /* Set connection-speed property if needed */
2039 if (chain->demuxer) {
2042 if ((pspec = g_object_class_find_property (G_OBJECT_GET_CLASS (element),
2043 "connection-speed"))) {
2044 guint64 speed = parsebin->connection_speed / 1000;
2045 gboolean wrong_type = FALSE;
2047 if (G_PARAM_SPEC_TYPE (pspec) == G_TYPE_PARAM_UINT) {
2048 GParamSpecUInt *pspecuint = G_PARAM_SPEC_UINT (pspec);
2050 speed = CLAMP (speed, pspecuint->minimum, pspecuint->maximum);
2051 } else if (G_PARAM_SPEC_TYPE (pspec) == G_TYPE_PARAM_INT) {
2052 GParamSpecInt *pspecint = G_PARAM_SPEC_INT (pspec);
2054 speed = CLAMP (speed, pspecint->minimum, pspecint->maximum);
2055 } else if (G_PARAM_SPEC_TYPE (pspec) == G_TYPE_PARAM_UINT64) {
2056 GParamSpecUInt64 *pspecuint = G_PARAM_SPEC_UINT64 (pspec);
2058 speed = CLAMP (speed, pspecuint->minimum, pspecuint->maximum);
2059 } else if (G_PARAM_SPEC_TYPE (pspec) == G_TYPE_PARAM_INT64) {
2060 GParamSpecInt64 *pspecint = G_PARAM_SPEC_INT64 (pspec);
2062 speed = CLAMP (speed, pspecint->minimum, pspecint->maximum);
2064 GST_WARNING_OBJECT (parsebin,
2065 "The connection speed property %" G_GUINT64_FORMAT " of type %s"
2066 " is not usefull not setting it", speed,
2067 g_type_name (G_PARAM_SPEC_TYPE (pspec)));
2072 GST_DEBUG_OBJECT (parsebin,
2073 "setting connection-speed=%" G_GUINT64_FORMAT
2074 " to demuxer element", speed);
2076 g_object_set (element, "connection-speed", speed, NULL);
2081 /* try to configure the subtitle encoding property when we can */
2082 pspec = g_object_class_find_property (G_OBJECT_GET_CLASS (element),
2083 "subtitle-encoding");
2084 if (pspec && G_PARAM_SPEC_VALUE_TYPE (pspec) == G_TYPE_STRING) {
2085 SUBTITLE_LOCK (parsebin);
2086 GST_DEBUG_OBJECT (parsebin,
2087 "setting subtitle-encoding=%s to element", parsebin->encoding);
2088 g_object_set (G_OBJECT (element), "subtitle-encoding", parsebin->encoding,
2090 SUBTITLE_UNLOCK (parsebin);
2096 /* link this element further */
2097 to_connect = connect_element (parsebin, delem, chain);
2099 if ((is_simple_demuxer || is_parser_converter) && to_connect) {
2101 for (l = to_connect; l; l = g_list_next (l)) {
2102 GstPad *opad = GST_PAD_CAST (l->data);
2105 ocaps = get_pad_caps (opad);
2106 analyze_new_pad (parsebin, delem->element, opad, ocaps, chain);
2108 gst_caps_unref (ocaps);
2110 gst_object_unref (opad);
2112 g_list_free (to_connect);
2116 /* Bring the element to the state of the parent */
2118 /* First lock element's sinkpad stream lock so no data reaches
2119 * the possible new element added when caps are sent by element
2120 * while we're still sending sticky events */
2121 GST_PAD_STREAM_LOCK (sinkpad);
2123 if ((gst_element_set_state (element,
2124 GST_STATE_PAUSED)) == GST_STATE_CHANGE_FAILURE ||
2125 !send_sticky_events (parsebin, pad)) {
2126 GstParseElement *dtmp = NULL;
2127 GstElement *tmp = NULL;
2128 GstMessage *error_msg;
2130 GST_PAD_STREAM_UNLOCK (sinkpad);
2132 GST_WARNING_OBJECT (parsebin, "Couldn't set %s to PAUSED",
2133 GST_ELEMENT_NAME (element));
2135 g_list_free_full (to_connect, (GDestroyNotify) gst_object_unref);
2138 remove_error_filter (parsebin, element, &error_msg);
2141 gchar *error_string = error_message_to_string (error_msg);
2142 g_string_append_printf (error_details, "Couldn't set %s to PAUSED:\n%s",
2143 GST_ELEMENT_NAME (element), error_string);
2144 gst_message_unref (error_msg);
2145 g_free (error_string);
2147 g_string_append_printf (error_details, "Couldn't set %s to PAUSED",
2148 GST_ELEMENT_NAME (element));
2151 /* Remove all elements in this chain that were just added. No
2152 * other thread could've added elements in the meantime */
2153 CHAIN_MUTEX_LOCK (chain);
2157 dtmp = chain->elements->data;
2158 tmp = dtmp->element;
2160 /* Disconnect any signal handlers that might be connected
2161 * in connect_element() or analyze_pad() */
2162 if (dtmp->pad_added_id)
2163 g_signal_handler_disconnect (tmp, dtmp->pad_added_id);
2164 if (dtmp->pad_removed_id)
2165 g_signal_handler_disconnect (tmp, dtmp->pad_removed_id);
2166 if (dtmp->no_more_pads_id)
2167 g_signal_handler_disconnect (tmp, dtmp->no_more_pads_id);
2169 for (l = chain->pending_pads; l;) {
2170 GstPendingPad *pp = l->data;
2173 if (GST_PAD_PARENT (pp->pad) != tmp) {
2178 gst_pending_pad_free (pp);
2180 /* Remove element from the list, update list head and go to the
2181 * next element in the list */
2183 chain->pending_pads = g_list_delete_link (chain->pending_pads, l);
2187 if (dtmp->capsfilter) {
2188 gst_bin_remove (GST_BIN (parsebin), dtmp->capsfilter);
2189 gst_element_set_state (dtmp->capsfilter, GST_STATE_NULL);
2190 gst_object_unref (dtmp->capsfilter);
2193 gst_bin_remove (GST_BIN (parsebin), tmp);
2194 gst_element_set_state (tmp, GST_STATE_NULL);
2196 gst_object_unref (tmp);
2197 g_slice_free (GstParseElement, dtmp);
2199 chain->elements = g_list_delete_link (chain->elements, chain->elements);
2200 } while (tmp != element);
2201 CHAIN_MUTEX_UNLOCK (chain);
2205 /* Everything went well, the spice must flow now */
2206 GST_PAD_STREAM_UNLOCK (sinkpad);
2209 /* Remove error filter now, from now on we can't gracefully
2210 * handle errors of the element anymore */
2211 remove_error_filter (parsebin, element, NULL);
2213 /* Now let the bin handle the state */
2214 gst_element_set_locked_state (element, FALSE);
2217 SUBTITLE_LOCK (parsebin);
2218 /* we added the element now, add it to the list of subtitle-encoding
2219 * elements when we can set the property */
2220 parsebin->subtitles = g_list_prepend (parsebin->subtitles, element);
2221 SUBTITLE_UNLOCK (parsebin);
2226 for (l = to_connect; l; l = g_list_next (l)) {
2227 GstPad *opad = GST_PAD_CAST (l->data);
2230 ocaps = get_pad_caps (opad);
2231 analyze_new_pad (parsebin, delem->element, opad, ocaps, chain);
2233 gst_caps_unref (ocaps);
2235 gst_object_unref (opad);
2237 g_list_free (to_connect);
2247 *deadend_details = g_string_free (error_details, (error_details->len == 0
2250 *deadend_details = NULL;
2256 get_pad_caps (GstPad * pad)
2260 /* first check the pad caps, if this is set, we are positively sure it is
2261 * fixed and exactly what the element will produce. */
2262 caps = gst_pad_get_current_caps (pad);
2264 /* then use the getcaps function if we don't have caps. These caps might not
2265 * be fixed in some cases, in which case analyze_new_pad will set up a
2266 * notify::caps signal to continue autoplugging. */
2268 caps = gst_pad_query_caps (pad, NULL);
2273 /* Returns a list of pads that can be connected to already and
2274 * connects to pad-added and related signals */
2276 connect_element (GstParseBin * parsebin, GstParseElement * delem,
2277 GstParseChain * chain)
2279 GstElement *element = delem->element;
2281 gboolean dynamic = FALSE;
2282 GList *to_connect = NULL;
2284 GST_DEBUG_OBJECT (parsebin,
2285 "Attempting to connect element %s [chain:%p] further",
2286 GST_ELEMENT_NAME (element), chain);
2288 /* 1. Loop over pad templates, grabbing existing pads along the way */
2289 for (pads = GST_ELEMENT_GET_CLASS (element)->padtemplates; pads;
2290 pads = g_list_next (pads)) {
2291 GstPadTemplate *templ = GST_PAD_TEMPLATE (pads->data);
2292 const gchar *templ_name;
2294 /* we are only interested in source pads */
2295 if (GST_PAD_TEMPLATE_DIRECTION (templ) != GST_PAD_SRC)
2298 templ_name = GST_PAD_TEMPLATE_NAME_TEMPLATE (templ);
2299 GST_DEBUG_OBJECT (parsebin, "got a source pad template %s", templ_name);
2301 /* figure out what kind of pad this is */
2302 switch (GST_PAD_TEMPLATE_PRESENCE (templ)) {
2303 case GST_PAD_ALWAYS:
2305 /* get the pad that we need to autoplug */
2306 GstPad *pad = gst_element_get_static_pad (element, templ_name);
2309 GST_DEBUG_OBJECT (parsebin, "got the pad for always template %s",
2311 /* here is the pad, we need to autoplug it */
2312 to_connect = g_list_prepend (to_connect, pad);
2314 /* strange, pad is marked as always but it's not
2315 * there. Fix the element */
2316 GST_WARNING_OBJECT (parsebin,
2317 "could not get the pad for always template %s", templ_name);
2321 case GST_PAD_SOMETIMES:
2323 /* try to get the pad to see if it is already created or
2325 GstPad *pad = gst_element_get_static_pad (element, templ_name);
2328 GST_DEBUG_OBJECT (parsebin, "got the pad for sometimes template %s",
2330 /* the pad is created, we need to autoplug it */
2331 to_connect = g_list_prepend (to_connect, pad);
2333 GST_DEBUG_OBJECT (parsebin,
2334 "did not get the sometimes pad of template %s", templ_name);
2335 /* we have an element that will create dynamic pads */
2340 case GST_PAD_REQUEST:
2341 /* ignore request pads */
2342 GST_DEBUG_OBJECT (parsebin, "ignoring request padtemplate %s",
2348 /* 2. if there are more potential pads, connect to relevant signals */
2350 GST_LOG_OBJECT (parsebin, "Adding signals to element %s in chain %p",
2351 GST_ELEMENT_NAME (element), chain);
2352 delem->pad_added_id = g_signal_connect (element, "pad-added",
2353 G_CALLBACK (pad_added_cb), chain);
2354 delem->pad_removed_id = g_signal_connect (element, "pad-removed",
2355 G_CALLBACK (pad_removed_cb), chain);
2356 delem->no_more_pads_id = g_signal_connect (element, "no-more-pads",
2357 G_CALLBACK (no_more_pads_cb), chain);
2360 /* 3. return all pads that can be connected to already */
2367 * Expose the given pad on the chain as a decoded pad.
2370 expose_pad (GstParseBin * parsebin, GstElement * src, GstParsePad * parsepad,
2371 GstPad * pad, GstCaps * caps, GstParseChain * chain)
2373 GST_DEBUG_OBJECT (parsebin, "pad %s:%s, chain:%p",
2374 GST_DEBUG_PAD_NAME (pad), chain);
2376 gst_parse_pad_activate (parsepad, chain);
2377 chain->endpad = gst_object_ref (parsepad);
2379 chain->endcaps = gst_caps_ref (caps);
2381 chain->endcaps = NULL;
2385 type_found (GstElement * typefind, guint probability,
2386 GstCaps * caps, GstParseBin * parse_bin)
2388 GstPad *pad, *sink_pad;
2390 GST_DEBUG_OBJECT (parse_bin, "typefind found caps %" GST_PTR_FORMAT, caps);
2392 /* If the typefinder (but not something else) finds text/plain - i.e. that's
2393 * the top-level type of the file - then error out.
2395 if (gst_structure_has_name (gst_caps_get_structure (caps, 0), "text/plain")) {
2396 GST_ELEMENT_ERROR (parse_bin, STREAM, WRONG_TYPE,
2397 (_("This appears to be a text file")),
2398 ("ParseBin cannot decode plain text files"));
2402 /* FIXME: we can only deal with one type, we don't yet support dynamically changing
2403 * caps from the typefind element */
2404 if (parse_bin->have_type || parse_bin->parse_chain)
2407 parse_bin->have_type = TRUE;
2409 pad = gst_element_get_static_pad (typefind, "src");
2410 sink_pad = gst_element_get_static_pad (typefind, "sink");
2412 /* need some lock here to prevent race with shutdown state change
2413 * which might yank away e.g. parse_chain while building stuff here.
2414 * In typical cases, STREAM_LOCK is held and handles that, it need not
2415 * be held (if called from a proxied setcaps), so grab it anyway */
2416 GST_PAD_STREAM_LOCK (sink_pad);
2417 parse_bin->parse_chain = gst_parse_chain_new (parse_bin, NULL, pad, caps);
2418 analyze_new_pad (parse_bin, typefind, pad, caps, parse_bin->parse_chain);
2419 GST_PAD_STREAM_UNLOCK (sink_pad);
2421 gst_object_unref (sink_pad);
2422 gst_object_unref (pad);
2428 static GstPadProbeReturn
2429 pad_event_cb (GstPad * pad, GstPadProbeInfo * info, gpointer data)
2431 GstEvent *event = GST_PAD_PROBE_INFO_EVENT (info);
2432 GstPendingPad *ppad = (GstPendingPad *) data;
2433 GstParseChain *chain = ppad->chain;
2434 GstParseBin *parsebin = chain->parsebin;
2438 g_assert (parsebin);
2439 switch (GST_EVENT_TYPE (event)) {
2441 GST_DEBUG_OBJECT (pad, "Received EOS on a non final pad, this stream "
2443 chain->deadend = TRUE;
2444 chain->drained = TRUE;
2445 gst_object_replace ((GstObject **) & chain->current_pad, NULL);
2446 /* we don't set the endcaps because NULL endcaps means early EOS */
2448 EXPOSE_LOCK (parsebin);
2449 if (parsebin->parse_chain)
2450 if (gst_parse_chain_is_complete (parsebin->parse_chain))
2451 gst_parse_bin_expose (parsebin);
2452 EXPOSE_UNLOCK (parsebin);
2457 return GST_PAD_PROBE_OK;
2461 pad_added_cb (GstElement * element, GstPad * pad, GstParseChain * chain)
2464 GstParseBin *parsebin;
2466 parsebin = chain->parsebin;
2468 GST_DEBUG_OBJECT (pad, "pad added, chain:%p", chain);
2470 caps = get_pad_caps (pad);
2471 analyze_new_pad (parsebin, element, pad, caps, chain);
2473 gst_caps_unref (caps);
2475 EXPOSE_LOCK (parsebin);
2476 if (parsebin->parse_chain) {
2477 if (gst_parse_chain_is_complete (parsebin->parse_chain)) {
2478 GST_LOG_OBJECT (parsebin,
2479 "That was the last dynamic object, now attempting to expose the group");
2480 if (!gst_parse_bin_expose (parsebin))
2481 GST_WARNING_OBJECT (parsebin, "Couldn't expose group");
2484 GST_DEBUG_OBJECT (parsebin, "No parse chain, new pad ignored");
2486 EXPOSE_UNLOCK (parsebin);
2490 pad_removed_cb (GstElement * element, GstPad * pad, GstParseChain * chain)
2494 GST_LOG_OBJECT (pad, "pad removed, chain:%p", chain);
2496 /* In fact, we don't have to do anything here, the active group will be
2497 * removed when the group's multiqueue is drained */
2498 CHAIN_MUTEX_LOCK (chain);
2499 for (l = chain->pending_pads; l; l = l->next) {
2500 GstPendingPad *ppad = l->data;
2501 GstPad *opad = ppad->pad;
2504 gst_pending_pad_free (ppad);
2505 chain->pending_pads = g_list_delete_link (chain->pending_pads, l);
2509 CHAIN_MUTEX_UNLOCK (chain);
2513 no_more_pads_cb (GstElement * element, GstParseChain * chain)
2515 GstParseGroup *group = NULL;
2517 GST_LOG_OBJECT (element, "got no more pads");
2519 CHAIN_MUTEX_LOCK (chain);
2520 if (!chain->elements
2521 || ((GstParseElement *) chain->elements->data)->element != element) {
2522 GST_LOG_OBJECT (chain->parsebin, "no-more-pads from old chain element '%s'",
2523 GST_OBJECT_NAME (element));
2524 CHAIN_MUTEX_UNLOCK (chain);
2526 } else if (!chain->demuxer) {
2527 GST_LOG_OBJECT (chain->parsebin,
2528 "no-more-pads from a non-demuxer element '%s'",
2529 GST_OBJECT_NAME (element));
2530 CHAIN_MUTEX_UNLOCK (chain);
2534 /* when we received no_more_pads, we can complete the pads of the chain */
2535 if (!chain->next_groups && chain->active_group) {
2536 group = chain->active_group;
2537 } else if (chain->next_groups) {
2539 for (iter = chain->next_groups; iter; iter = g_list_next (iter)) {
2541 if (!group->no_more_pads)
2546 GST_ERROR_OBJECT (chain->parsebin, "can't find group for element");
2547 CHAIN_MUTEX_UNLOCK (chain);
2551 GST_DEBUG_OBJECT (element, "Setting group %p to complete", group);
2553 group->no_more_pads = TRUE;
2554 CHAIN_MUTEX_UNLOCK (chain);
2556 EXPOSE_LOCK (chain->parsebin);
2557 if (chain->parsebin->parse_chain) {
2558 if (gst_parse_chain_is_complete (chain->parsebin->parse_chain)) {
2559 gst_parse_bin_expose (chain->parsebin);
2562 EXPOSE_UNLOCK (chain->parsebin);
2566 caps_notify_cb (GstPad * pad, GParamSpec * unused, GstParseChain * chain)
2568 GstElement *element;
2571 GST_LOG_OBJECT (pad, "Notified caps for pad %s:%s", GST_DEBUG_PAD_NAME (pad));
2573 /* Disconnect this; if we still need it, we'll reconnect to this in
2574 * analyze_new_pad */
2575 element = GST_ELEMENT_CAST (gst_pad_get_parent (pad));
2577 CHAIN_MUTEX_LOCK (chain);
2578 for (l = chain->pending_pads; l; l = l->next) {
2579 GstPendingPad *ppad = l->data;
2580 if (ppad->pad == pad) {
2581 gst_pending_pad_free (ppad);
2582 chain->pending_pads = g_list_delete_link (chain->pending_pads, l);
2586 CHAIN_MUTEX_UNLOCK (chain);
2588 pad_added_cb (element, pad, chain);
2590 gst_object_unref (element);
2593 /* Decide whether an element is a demuxer based on the
2594 * klass and number/type of src pad templates it has */
2596 is_demuxer_element (GstElement * srcelement)
2598 GstElementFactory *srcfactory;
2599 GstElementClass *elemclass;
2602 gint potential_src_pads = 0;
2604 srcfactory = gst_element_get_factory (srcelement);
2606 gst_element_factory_get_metadata (srcfactory, GST_ELEMENT_METADATA_KLASS);
2608 /* Can't be a demuxer unless it has Demux in the klass name */
2609 if (!strstr (klass, "Demux"))
2612 /* Walk the src pad templates and count how many the element
2614 elemclass = GST_ELEMENT_GET_CLASS (srcelement);
2616 walk = gst_element_class_get_pad_template_list (elemclass);
2617 while (walk != NULL) {
2618 GstPadTemplate *templ;
2620 templ = (GstPadTemplate *) walk->data;
2621 if (GST_PAD_TEMPLATE_DIRECTION (templ) == GST_PAD_SRC) {
2622 switch (GST_PAD_TEMPLATE_PRESENCE (templ)) {
2623 case GST_PAD_ALWAYS:
2624 case GST_PAD_SOMETIMES:
2625 if (strstr (GST_PAD_TEMPLATE_NAME_TEMPLATE (templ), "%"))
2626 potential_src_pads += 2; /* Might make multiple pads */
2628 potential_src_pads += 1;
2630 case GST_PAD_REQUEST:
2631 potential_src_pads += 2;
2635 walk = g_list_next (walk);
2638 if (potential_src_pads < 2)
2644 /* gst_parse_chain_get_current_group:
2646 * Returns the current group of this chain, to which
2647 * new chains should be attached or NULL if the last
2648 * group didn't have no-more-pads.
2650 * Not MT-safe: Call with parent chain lock!
2652 static GstParseGroup *
2653 gst_parse_chain_get_current_group (GstParseChain * chain)
2655 GstParseGroup *group;
2657 /* Now we know that we can really return something useful */
2658 if (!chain->active_group) {
2659 chain->active_group = group = gst_parse_group_new (chain->parsebin, chain);
2660 } else if (!chain->active_group->no_more_pads) {
2661 group = chain->active_group;
2665 for (iter = chain->next_groups; iter; iter = g_list_next (iter)) {
2666 GstParseGroup *next_group = iter->data;
2668 if (!next_group->no_more_pads) {
2675 group = gst_parse_group_new (chain->parsebin, chain);
2676 chain->next_groups = g_list_append (chain->next_groups, group);
2682 static void gst_parse_group_free_internal (GstParseGroup * group,
2686 gst_parse_chain_free_internal (GstParseChain * chain, gboolean hide)
2688 GList *l, *set_to_null = NULL;
2690 CHAIN_MUTEX_LOCK (chain);
2692 GST_DEBUG_OBJECT (chain->parsebin, "%s chain %p",
2693 (hide ? "Hiding" : "Freeing"), chain);
2695 if (chain->active_group) {
2696 gst_parse_group_free_internal (chain->active_group, hide);
2698 chain->active_group = NULL;
2701 for (l = chain->next_groups; l; l = l->next) {
2702 gst_parse_group_free_internal ((GstParseGroup *) l->data, hide);
2707 g_list_free (chain->next_groups);
2708 chain->next_groups = NULL;
2712 for (l = chain->old_groups; l; l = l->next) {
2713 GstParseGroup *group = l->data;
2715 gst_parse_group_free (group);
2717 g_list_free (chain->old_groups);
2718 chain->old_groups = NULL;
2721 gst_object_replace ((GstObject **) & chain->current_pad, NULL);
2723 for (l = chain->pending_pads; l; l = l->next) {
2724 GstPendingPad *ppad = l->data;
2725 gst_pending_pad_free (ppad);
2728 g_list_free (chain->pending_pads);
2729 chain->pending_pads = NULL;
2731 for (l = chain->elements; l; l = l->next) {
2732 GstParseElement *delem = l->data;
2733 GstElement *element = delem->element;
2735 if (delem->pad_added_id)
2736 g_signal_handler_disconnect (element, delem->pad_added_id);
2737 delem->pad_added_id = 0;
2738 if (delem->pad_removed_id)
2739 g_signal_handler_disconnect (element, delem->pad_removed_id);
2740 delem->pad_removed_id = 0;
2741 if (delem->no_more_pads_id)
2742 g_signal_handler_disconnect (element, delem->no_more_pads_id);
2743 delem->no_more_pads_id = 0;
2745 if (delem->capsfilter) {
2746 if (GST_OBJECT_PARENT (delem->capsfilter) ==
2747 GST_OBJECT_CAST (chain->parsebin))
2748 gst_bin_remove (GST_BIN_CAST (chain->parsebin), delem->capsfilter);
2751 g_list_append (set_to_null, gst_object_ref (delem->capsfilter));
2755 if (GST_OBJECT_PARENT (element) == GST_OBJECT_CAST (chain->parsebin))
2756 gst_bin_remove (GST_BIN_CAST (chain->parsebin), element);
2758 set_to_null = g_list_append (set_to_null, gst_object_ref (element));
2761 SUBTITLE_LOCK (chain->parsebin);
2762 /* remove possible subtitle element */
2763 chain->parsebin->subtitles =
2764 g_list_remove (chain->parsebin->subtitles, element);
2765 SUBTITLE_UNLOCK (chain->parsebin);
2768 if (delem->capsfilter) {
2769 gst_object_unref (delem->capsfilter);
2770 delem->capsfilter = NULL;
2773 gst_object_unref (element);
2776 g_slice_free (GstParseElement, delem);
2780 g_list_free (chain->elements);
2781 chain->elements = NULL;
2784 if (chain->endpad) {
2785 if (chain->endpad->exposed) {
2786 GstPad *endpad = GST_PAD_CAST (chain->endpad);
2787 GST_DEBUG_OBJECT (chain->parsebin, "Removing pad %s:%s",
2788 GST_DEBUG_PAD_NAME (endpad));
2789 gst_pad_push_event (endpad, gst_event_new_eos ());
2790 gst_element_remove_pad (GST_ELEMENT_CAST (chain->parsebin), endpad);
2793 parse_pad_set_target (chain->endpad, NULL);
2794 chain->endpad->exposed = FALSE;
2796 gst_object_unref (chain->endpad);
2797 chain->endpad = NULL;
2801 if (!hide && chain->current_pad) {
2802 gst_object_unref (chain->current_pad);
2803 chain->current_pad = NULL;
2807 gst_object_unref (chain->pad);
2810 if (chain->start_caps) {
2811 gst_caps_unref (chain->start_caps);
2812 chain->start_caps = NULL;
2815 if (chain->endcaps) {
2816 gst_caps_unref (chain->endcaps);
2817 chain->endcaps = NULL;
2819 g_free (chain->deadend_details);
2820 chain->deadend_details = NULL;
2822 GST_DEBUG_OBJECT (chain->parsebin, "%s chain %p", (hide ? "Hidden" : "Freed"),
2824 CHAIN_MUTEX_UNLOCK (chain);
2826 while (set_to_null) {
2827 GstElement *element = set_to_null->data;
2828 set_to_null = g_list_delete_link (set_to_null, set_to_null);
2829 gst_element_set_state (element, GST_STATE_NULL);
2830 gst_object_unref (element);
2834 g_mutex_clear (&chain->lock);
2835 g_slice_free (GstParseChain, chain);
2839 /* gst_parse_chain_free:
2841 * Completely frees and removes the chain and all
2842 * child groups from ParseBin.
2844 * MT-safe, don't hold the chain lock or any child chain's lock
2845 * when calling this!
2848 gst_parse_chain_free (GstParseChain * chain)
2850 gst_parse_chain_free_internal (chain, FALSE);
2853 /* gst_parse_chain_new:
2855 * Creates a new parse chain and initializes it.
2857 * It's up to the caller to add it to the list of child chains of
2860 static GstParseChain *
2861 gst_parse_chain_new (GstParseBin * parsebin, GstParseGroup * parent,
2862 GstPad * pad, GstCaps * start_caps)
2864 GstParseChain *chain = g_slice_new0 (GstParseChain);
2866 GST_DEBUG_OBJECT (parsebin, "Creating new chain %p with parent group %p",
2869 chain->parsebin = parsebin;
2870 chain->parent = parent;
2871 g_mutex_init (&chain->lock);
2872 chain->pad = gst_object_ref (pad);
2874 chain->start_caps = gst_caps_ref (start_caps);
2880 * GstParseGroup functions
2884 gst_parse_group_free_internal (GstParseGroup * group, gboolean hide)
2888 GST_DEBUG_OBJECT (group->parsebin, "%s group %p",
2889 (hide ? "Hiding" : "Freeing"), group);
2890 for (l = group->children; l; l = l->next) {
2891 GstParseChain *chain = (GstParseChain *) l->data;
2893 gst_parse_chain_free_internal (chain, hide);
2898 g_list_free (group->children);
2899 group->children = NULL;
2902 GST_DEBUG_OBJECT (group->parsebin, "%s group %p", (hide ? "Hid" : "Freed"),
2905 g_slice_free (GstParseGroup, group);
2908 /* gst_parse_group_free:
2910 * Completely frees and removes the parse group and all
2913 * Never call this from any streaming thread!
2915 * Not MT-safe, call with parent's chain lock!
2918 gst_parse_group_free (GstParseGroup * group)
2920 gst_parse_group_free_internal (group, FALSE);
2923 /* gst_parse_group_hide:
2925 * Hide the parse group only, this means that
2926 * all child endpads are removed from ParseBin
2927 * and all signals are unconnected.
2929 * No element is set to NULL state and completely
2932 * Can be called from streaming threads.
2934 * Not MT-safe, call with parent's chain lock!
2937 gst_parse_group_hide (GstParseGroup * group)
2939 gst_parse_group_free_internal (group, TRUE);
2942 /* gst_parse_chain_free_hidden_groups:
2944 * Frees any parse groups that were hidden previously.
2945 * This allows keeping memory use from ballooning when
2946 * switching chains repeatedly.
2948 * A new throwaway thread will be created to free the
2949 * groups, so any delay does not block the setup of a
2952 * Not MT-safe, call with parent's chain lock!
2955 gst_parse_chain_free_hidden_groups (GList * old_groups)
2959 for (l = old_groups; l; l = l->next) {
2960 GstParseGroup *group = l->data;
2962 gst_parse_group_free (group);
2964 g_list_free (old_groups);
2968 gst_parse_chain_start_free_hidden_groups_thread (GstParseChain * chain)
2971 GError *error = NULL;
2974 old_groups = chain->old_groups;
2978 chain->old_groups = NULL;
2979 thread = g_thread_try_new ("free-hidden-groups",
2980 (GThreadFunc) gst_parse_chain_free_hidden_groups, old_groups, &error);
2981 if (!thread || error) {
2982 GST_ERROR ("Failed to start free-hidden-groups thread: %s",
2983 error ? error->message : "unknown reason");
2984 g_clear_error (&error);
2985 chain->old_groups = old_groups;
2988 GST_DEBUG_OBJECT (chain->parsebin, "Started free-hidden-groups thread");
2989 /* We do not need to wait for it or get any results from it */
2990 g_thread_unref (thread);
2993 /* gst_parse_group_new:
2994 * @parsebin: Parent ParseBin
2995 * @parent: Parent chain or %NULL
2997 * Creates a new GstParseGroup. It is up to the caller to add it to the list
3000 static GstParseGroup *
3001 gst_parse_group_new (GstParseBin * parsebin, GstParseChain * parent)
3003 GstParseGroup *group = g_slice_new0 (GstParseGroup);
3005 GST_DEBUG_OBJECT (parsebin, "Creating new group %p with parent chain %p",
3008 group->parsebin = parsebin;
3009 group->parent = parent;
3014 /* gst_parse_group_is_complete:
3016 * Checks if the group is complete, this means that
3017 * a) no-more-pads happened
3018 * b) all child chains are complete
3020 * Not MT-safe, always call with ParseBin expose lock
3023 gst_parse_group_is_complete (GstParseGroup * group)
3026 gboolean complete = TRUE;
3028 if (!group->no_more_pads) {
3033 for (l = group->children; l; l = l->next) {
3034 GstParseChain *chain = l->data;
3036 /* Any blocked chain requires we complete this group
3037 * since everything is synchronous, we can't proceed otherwise */
3038 if (chain->endpad && chain->endpad->blocked)
3041 if (!gst_parse_chain_is_complete (chain)) {
3048 GST_DEBUG_OBJECT (group->parsebin, "Group %p is complete: %d", group,
3053 /* gst_parse_chain_is_complete:
3055 * Returns TRUE if the chain is complete, this means either
3056 * a) This chain is a dead end, i.e. we have no suitable plugins
3057 * b) This chain ends in an endpad and this is blocked or exposed
3058 * c) The chain has gotten far enough to have plugged 1 parser at least.
3060 * Not MT-safe, always call with ParseBin expose lock
3063 gst_parse_chain_is_complete (GstParseChain * chain)
3065 gboolean complete = FALSE;
3067 CHAIN_MUTEX_LOCK (chain);
3068 if (chain->parsebin->shutdown)
3071 if (chain->deadend) {
3076 if (chain->endpad && (chain->endpad->blocked || chain->endpad->exposed)) {
3081 if (chain->demuxer) {
3082 if (chain->active_group
3083 && gst_parse_group_is_complete (chain->active_group)) {
3089 if (chain->parsed) {
3095 CHAIN_MUTEX_UNLOCK (chain);
3096 GST_DEBUG_OBJECT (chain->parsebin, "Chain %p is complete: %d", chain,
3102 chain_remove_old_groups (GstParseChain * chain)
3106 /* First go in child */
3107 if (chain->active_group) {
3108 for (tmp = chain->active_group->children; tmp; tmp = tmp->next) {
3109 GstParseChain *child = (GstParseChain *) tmp->data;
3110 chain_remove_old_groups (child);
3114 if (chain->old_groups) {
3115 gst_parse_group_hide (chain->old_groups->data);
3116 gst_parse_chain_start_free_hidden_groups_thread (chain);
3121 drain_and_switch_chains (GstParseChain * chain, GstParsePad * drainpad,
3122 gboolean * last_group, gboolean * drained, gboolean * switched);
3123 /* drain_and_switch_chains/groups:
3125 * CALL WITH CHAIN LOCK (or group parent) TAKEN !
3127 * Goes down the chains/groups until it finds the chain
3128 * to which the drainpad belongs.
3130 * It marks that pad/chain as drained and then will figure
3131 * out which group to switch to or not.
3133 * last_chain will be set to TRUE if the group to which the
3134 * pad belongs is the last one.
3136 * drained will be set to TRUE if the chain/group is drained.
3138 * Returns: TRUE if the chain contained the target pad */
3140 drain_and_switch_group (GstParseGroup * group, GstParsePad * drainpad,
3141 gboolean * last_group, gboolean * drained, gboolean * switched)
3143 gboolean handled = FALSE;
3146 GST_DEBUG ("Checking group %p (target pad %s:%s)",
3147 group, GST_DEBUG_PAD_NAME (drainpad));
3149 /* Definitely can't be in drained groups */
3150 if (G_UNLIKELY (group->drained)) {
3154 /* Figure out if all our chains are drained with the
3155 * new information */
3156 group->drained = TRUE;
3157 for (tmp = group->children; tmp; tmp = tmp->next) {
3158 GstParseChain *chain = (GstParseChain *) tmp->data;
3159 gboolean subdrained = FALSE;
3162 drain_and_switch_chains (chain, drainpad, last_group, &subdrained,
3165 group->drained = FALSE;
3169 GST_DEBUG ("group %p (last_group:%d, drained:%d, switched:%d, handled:%d)",
3170 group, *last_group, group->drained, *switched, handled);
3171 *drained = group->drained;
3176 drain_and_switch_chains (GstParseChain * chain, GstParsePad * drainpad,
3177 gboolean * last_group, gboolean * drained, gboolean * switched)
3179 gboolean handled = FALSE;
3180 GstParseBin *parsebin = chain->parsebin;
3182 GST_DEBUG ("Checking chain %p %s:%s (target pad %s:%s)",
3183 chain, GST_DEBUG_PAD_NAME (chain->pad), GST_DEBUG_PAD_NAME (drainpad));
3185 CHAIN_MUTEX_LOCK (chain);
3187 /* Definitely can't be in drained chains */
3188 if (G_UNLIKELY (chain->drained)) {
3192 if (chain->endpad) {
3193 /* Check if we're reached the target endchain */
3194 if (drainpad != NULL && chain == drainpad->chain) {
3195 GST_DEBUG ("Found the target chain");
3196 drainpad->drained = TRUE;
3200 chain->drained = chain->endpad->drained;
3204 /* We known there are groups to switch to */
3205 if (chain->next_groups)
3206 *last_group = FALSE;
3208 /* Check the active group */
3209 if (chain->active_group) {
3210 gboolean subdrained = FALSE;
3211 handled = drain_and_switch_group (chain->active_group, drainpad,
3212 last_group, &subdrained, switched);
3214 /* The group is drained, see if we can switch to another */
3215 if ((handled || drainpad == NULL) && subdrained && !*switched) {
3216 if (chain->next_groups) {
3217 /* Switch to next group, the actual removal of the current group will
3218 * be done when the next one is activated */
3219 GST_DEBUG_OBJECT (parsebin, "Moving current group %p to old groups",
3220 chain->active_group);
3222 g_list_prepend (chain->old_groups, chain->active_group);
3223 GST_DEBUG_OBJECT (parsebin, "Switching to next group %p",
3224 chain->next_groups->data);
3225 chain->active_group = chain->next_groups->data;
3226 chain->next_groups =
3227 g_list_delete_link (chain->next_groups, chain->next_groups);
3229 chain->drained = FALSE;
3231 GST_DEBUG ("Group %p was the last in chain %p", chain->active_group,
3233 chain->drained = TRUE;
3234 /* We're drained ! */
3237 if (subdrained && !chain->next_groups)
3243 CHAIN_MUTEX_UNLOCK (chain);
3245 GST_DEBUG ("Chain %p (handled:%d, last_group:%d, drained:%d, switched:%d)",
3246 chain, handled, *last_group, chain->drained, *switched);
3248 *drained = chain->drained;
3251 g_signal_emit (parsebin, gst_parse_bin_signals[SIGNAL_DRAINED], 0, NULL);
3256 /* check if the group is drained, meaning all pads have seen an EOS
3259 gst_parse_pad_handle_eos (GstParsePad * pad)
3261 gboolean last_group = TRUE;
3262 gboolean switched = FALSE;
3263 gboolean drained = FALSE;
3264 GstParseChain *chain = pad->chain;
3265 GstParseBin *parsebin = chain->parsebin;
3267 GST_LOG_OBJECT (parsebin, "pad %p", pad);
3268 EXPOSE_LOCK (parsebin);
3269 if (parsebin->parse_chain) {
3270 drain_and_switch_chains (parsebin->parse_chain, pad, &last_group, &drained,
3274 /* If we resulted in a group switch, expose what's needed */
3275 if (gst_parse_chain_is_complete (parsebin->parse_chain))
3276 gst_parse_bin_expose (parsebin);
3279 EXPOSE_UNLOCK (parsebin);
3284 /* gst_parse_group_is_drained:
3286 * Check is this group is drained and cache this result.
3287 * The group is drained if all child chains are drained.
3289 * Not MT-safe, call with group->parent's lock */
3291 gst_parse_group_is_drained (GstParseGroup * group)
3294 gboolean drained = TRUE;
3296 if (group->drained) {
3301 for (l = group->children; l; l = l->next) {
3302 GstParseChain *chain = l->data;
3304 CHAIN_MUTEX_LOCK (chain);
3305 if (!gst_parse_chain_is_drained (chain))
3307 CHAIN_MUTEX_UNLOCK (chain);
3311 group->drained = drained;
3314 GST_DEBUG_OBJECT (group->parsebin, "Group %p is drained: %d", group, drained);
3318 /* gst_parse_chain_is_drained:
3320 * Check is the chain is drained, which means that
3323 * a) it's endpad is drained
3324 * b) there are no pending pads, the active group is drained
3325 * and there are no next groups
3327 * Not MT-safe, call with chain lock
3330 gst_parse_chain_is_drained (GstParseChain * chain)
3332 gboolean drained = FALSE;
3334 if (chain->endpad) {
3335 drained = chain->endpad->drained;
3339 if (chain->pending_pads) {
3344 if (chain->active_group && gst_parse_group_is_drained (chain->active_group)
3345 && !chain->next_groups) {
3351 GST_DEBUG_OBJECT (chain->parsebin, "Chain %p is drained: %d", chain, drained);
3356 * GCompareFunc to use with lists of GstPad.
3357 * Sorts pads by mime type.
3358 * First video (raw, then non-raw), then audio (raw, then non-raw),
3361 * Return: negative if a<b, 0 if a==b, positive if a>b
3364 sort_end_pads (GstParsePad * da, GstParsePad * db)
3367 GstCaps *capsa, *capsb;
3368 GstStructure *sa, *sb;
3369 const gchar *namea, *nameb;
3373 capsa = get_pad_caps (GST_PAD_CAST (da));
3374 capsb = get_pad_caps (GST_PAD_CAST (db));
3376 sa = gst_caps_get_structure ((const GstCaps *) capsa, 0);
3377 sb = gst_caps_get_structure ((const GstCaps *) capsb, 0);
3379 namea = gst_structure_get_name (sa);
3380 nameb = gst_structure_get_name (sb);
3382 if (g_strrstr (namea, "video/x-raw"))
3384 else if (g_strrstr (namea, "video/"))
3386 else if (g_strrstr (namea, "image/"))
3388 else if (g_strrstr (namea, "audio/x-raw"))
3390 else if (g_strrstr (namea, "audio/"))
3395 if (g_strrstr (nameb, "video/x-raw"))
3397 else if (g_strrstr (nameb, "video/"))
3399 else if (g_strrstr (nameb, "image/"))
3401 else if (g_strrstr (nameb, "audio/x-raw"))
3403 else if (g_strrstr (nameb, "audio/"))
3408 gst_caps_unref (capsa);
3409 gst_caps_unref (capsb);
3414 /* if otherwise the same, sort by stream-id */
3415 ida = gst_pad_get_stream_id (GST_PAD_CAST (da));
3416 idb = gst_pad_get_stream_id (GST_PAD_CAST (db));
3417 ret = (ida) ? ((idb) ? strcmp (ida, idb) : -1) : 1;
3425 debug_sticky_event (GstPad * pad, GstEvent ** event, gpointer user_data)
3427 GST_DEBUG_OBJECT (pad, "sticky event %s (%p)", GST_EVENT_TYPE_NAME (*event),
3432 /* Must only be called if the toplevel chain is complete and blocked! */
3433 /* Not MT-safe, call with ParseBin expose lock! */
3435 gst_parse_bin_expose (GstParseBin * parsebin)
3437 GList *tmp, *endpads;
3438 gboolean missing_plugin;
3439 GString *missing_plugin_details;
3440 gboolean already_exposed;
3441 gboolean last_group;
3442 gboolean uncollected_streams;
3443 GstStreamCollection *fallback_collection = NULL;
3447 missing_plugin = FALSE;
3448 already_exposed = TRUE;
3451 missing_plugin_details = g_string_new ("");
3453 GST_DEBUG_OBJECT (parsebin, "Exposing currently active chains/groups");
3455 /* Don't expose if we're currently shutting down */
3456 DYN_LOCK (parsebin);
3457 if (G_UNLIKELY (parsebin->shutdown)) {
3458 GST_WARNING_OBJECT (parsebin,
3459 "Currently, shutting down, aborting exposing");
3460 DYN_UNLOCK (parsebin);
3463 DYN_UNLOCK (parsebin);
3465 /* Get the pads that we're going to expose and mark things as exposed */
3466 uncollected_streams = FALSE;
3467 if (!gst_parse_chain_expose (parsebin->parse_chain, &endpads, &missing_plugin,
3468 missing_plugin_details, &last_group, &uncollected_streams)) {
3469 g_list_free_full (endpads, (GDestroyNotify) gst_object_unref);
3470 g_string_free (missing_plugin_details, TRUE);
3471 GST_ERROR_OBJECT (parsebin, "Broken chain/group tree");
3472 g_return_val_if_reached (FALSE);
3475 if (endpads == NULL) {
3476 if (missing_plugin) {
3477 if (missing_plugin_details->len > 0) {
3478 gchar *details = g_string_free (missing_plugin_details, FALSE);
3479 GST_ELEMENT_ERROR (parsebin, CORE, MISSING_PLUGIN, (NULL),
3480 ("no suitable plugins found:\n%s", details));
3483 g_string_free (missing_plugin_details, TRUE);
3484 GST_ELEMENT_ERROR (parsebin, CORE, MISSING_PLUGIN, (NULL),
3485 ("no suitable plugins found"));
3488 /* in this case, the stream ended without buffers,
3489 * just post a warning */
3490 g_string_free (missing_plugin_details, TRUE);
3492 GST_WARNING_OBJECT (parsebin, "All streams finished without buffers. "
3493 "Last group: %d", last_group);
3495 GST_ELEMENT_ERROR (parsebin, STREAM, FAILED, (NULL),
3496 ("all streams without buffers"));
3498 gboolean switched = FALSE;
3499 gboolean drained = FALSE;
3501 drain_and_switch_chains (parsebin->parse_chain, NULL, &last_group,
3502 &drained, &switched);
3503 GST_ELEMENT_WARNING (parsebin, STREAM, FAILED, (NULL),
3504 ("all streams without buffers"));
3506 if (gst_parse_chain_is_complete (parsebin->parse_chain))
3514 do_async_done (parsebin);
3518 if (uncollected_streams) {
3519 /* FIXME: Collect and use a stream id from the top chain as
3521 fallback_collection = gst_stream_collection_new (NULL);
3523 build_fallback_collection (parsebin->parse_chain, fallback_collection);
3525 gst_element_post_message (GST_ELEMENT (parsebin),
3526 gst_message_new_stream_collection (GST_OBJECT (parsebin),
3527 fallback_collection));
3530 g_string_free (missing_plugin_details, TRUE);
3532 /* Check if this was called when everything was exposed already,
3533 * and see if we need to post a new fallback collection */
3534 for (tmp = endpads; tmp && already_exposed; tmp = tmp->next) {
3535 GstParsePad *parsepad = tmp->data;
3537 already_exposed &= parsepad->exposed;
3539 if (already_exposed) {
3540 GST_DEBUG_OBJECT (parsebin, "Everything was exposed already!");
3541 if (fallback_collection)
3542 gst_object_unref (fallback_collection);
3543 g_list_free_full (endpads, (GDestroyNotify) gst_object_unref);
3547 /* Set all already exposed pads to blocked */
3548 for (tmp = endpads; tmp; tmp = tmp->next) {
3549 GstParsePad *parsepad = tmp->data;
3551 if (parsepad->exposed) {
3552 GST_DEBUG_OBJECT (parsepad, "blocking exposed pad");
3553 gst_parse_pad_set_blocked (parsepad, TRUE);
3557 /* re-order pads : video, then audio, then others */
3558 endpads = g_list_sort (endpads, (GCompareFunc) sort_end_pads);
3561 for (tmp = endpads; tmp; tmp = tmp->next) {
3562 GstParsePad *parsepad = (GstParsePad *) tmp->data;
3565 //if (!parsepad->blocked)
3568 /* 1. rewrite name */
3569 padname = g_strdup_printf ("src_%u", parsebin->nbpads);
3571 GST_DEBUG_OBJECT (parsebin, "About to expose parsepad %s as %s",
3572 GST_OBJECT_NAME (parsepad), padname);
3573 gst_object_set_name (GST_OBJECT (parsepad), padname);
3576 gst_pad_sticky_events_foreach (GST_PAD_CAST (parsepad), debug_sticky_event,
3579 /* 2. activate and add */
3580 if (!parsepad->exposed) {
3581 parsepad->exposed = TRUE;
3582 if (!gst_element_add_pad (GST_ELEMENT (parsebin),
3583 GST_PAD_CAST (parsepad))) {
3584 /* not really fatal, we can try to add the other pads */
3585 g_warning ("error adding pad to ParseBin");
3586 parsepad->exposed = FALSE;
3590 /* HACK: Send an empty gap event to push sticky events */
3591 gst_pad_push_event (GST_PAD (parsepad),
3592 gst_event_new_gap (0, GST_CLOCK_TIME_NONE));
3596 GST_INFO_OBJECT (parsepad, "added new decoded pad");
3599 /* Unblock internal pads. The application should have connected stuff now
3600 * so that streaming can continue. */
3601 for (tmp = endpads; tmp; tmp = tmp->next) {
3602 GstParsePad *parsepad = (GstParsePad *) tmp->data;
3604 if (parsepad->exposed) {
3605 GST_DEBUG_OBJECT (parsepad, "unblocking");
3606 gst_parse_pad_unblock (parsepad);
3607 GST_DEBUG_OBJECT (parsepad, "unblocked");
3610 /* Send stream-collection events for any pads that don't have them,
3611 * and post a stream-collection onto the bus */
3612 if (parsepad->active_collection == NULL && fallback_collection) {
3613 gst_pad_push_event (GST_PAD (parsepad),
3614 gst_event_new_stream_collection (gst_object_ref
3615 (fallback_collection)));
3617 gst_object_unref (parsepad);
3619 g_list_free (endpads);
3621 if (fallback_collection)
3622 gst_object_unref (fallback_collection);
3624 /* Remove old groups */
3625 chain_remove_old_groups (parsebin->parse_chain);
3627 do_async_done (parsebin);
3628 GST_DEBUG_OBJECT (parsebin, "Exposed everything");
3632 /* gst_parse_chain_expose:
3634 * Check if the chain can be exposed and add all endpads
3635 * to the endpads list.
3637 * Not MT-safe, call with ParseBin expose lock! *
3640 gst_parse_chain_expose (GstParseChain * chain, GList ** endpads,
3641 gboolean * missing_plugin, GString * missing_plugin_details,
3642 gboolean * last_group, gboolean * uncollected_streams)
3644 GstParseGroup *group;
3646 gboolean ret = FALSE;
3648 if (chain->deadend) {
3649 if (chain->endcaps) {
3650 if (chain->deadend_details) {
3651 g_string_append (missing_plugin_details, chain->deadend_details);
3652 g_string_append_c (missing_plugin_details, '\n');
3654 gchar *desc = gst_pb_utils_get_codec_description (chain->endcaps);
3655 gchar *caps_str = gst_caps_to_string (chain->endcaps);
3656 g_string_append_printf (missing_plugin_details,
3657 "Missing decoder: %s (%s)\n", desc, caps_str);
3661 *missing_plugin = TRUE;
3666 if (chain->endpad == NULL && chain->parsed && chain->pending_pads) {
3667 /* The chain has a pending pad from a parser, let's just
3668 * expose that now as the endpad */
3669 GList *cur = chain->pending_pads;
3670 GstPendingPad *ppad = (GstPendingPad *) (cur->data);
3671 GstPad *endpad = gst_object_ref (ppad->pad);
3673 GST_ELEMENT (gst_object_get_parent (GST_OBJECT (endpad)));
3675 chain->pending_pads = g_list_remove (chain->pending_pads, ppad);
3677 gst_pending_pad_free (ppad);
3679 GST_DEBUG_OBJECT (chain->parsebin,
3680 "Exposing pad %" GST_PTR_FORMAT " with incomplete caps "
3681 "because it's parsed", endpad);
3683 expose_pad (chain->parsebin, elem, chain->current_pad, endpad, NULL, chain);
3684 gst_object_unref (endpad);
3685 gst_object_unref (elem);
3688 if (chain->endpad) {
3689 GstParsePad *p = chain->endpad;
3691 if (p->active_stream && p->active_collection == NULL
3692 && !p->in_a_fallback_collection)
3693 *uncollected_streams = TRUE;
3695 *endpads = g_list_prepend (*endpads, gst_object_ref (p));
3699 if (chain->next_groups)
3700 *last_group = FALSE;
3702 group = chain->active_group;
3704 GstParsePad *p = chain->current_pad;
3706 if (p->active_stream && p->active_collection == NULL
3707 && !p->in_a_fallback_collection)
3708 *uncollected_streams = TRUE;
3713 for (l = group->children; l; l = l->next) {
3714 GstParseChain *childchain = l->data;
3716 ret |= gst_parse_chain_expose (childchain, endpads, missing_plugin,
3717 missing_plugin_details, last_group, uncollected_streams);
3724 build_fallback_collection (GstParseChain * chain,
3725 GstStreamCollection * collection)
3727 GstParseGroup *group = chain->active_group;
3730 /* If it's an end pad, or a not-finished chain that's
3731 * not a group, put it in the collection */
3732 if (chain->endpad || (chain->current_pad && group == NULL)) {
3733 GstParsePad *p = chain->current_pad;
3735 if (p->active_stream != NULL && p->active_collection == NULL) {
3736 GST_DEBUG_OBJECT (p, "Adding stream to fallback collection");
3737 gst_stream_collection_add_stream (collection,
3738 gst_object_ref (p->active_stream));
3739 p->in_a_fallback_collection = TRUE;
3746 for (l = group->children; l; l = l->next) {
3747 GstParseChain *childchain = l->data;
3749 build_fallback_collection (childchain, collection);
3753 /*************************
3754 * GstParsePad functions
3755 *************************/
3757 static void gst_parse_pad_dispose (GObject * object);
3760 gst_parse_pad_class_init (GstParsePadClass * klass)
3762 GObjectClass *gobject_klass;
3764 gobject_klass = (GObjectClass *) klass;
3766 gobject_klass->dispose = gst_parse_pad_dispose;
3770 gst_parse_pad_init (GstParsePad * pad)
3773 pad->blocked = FALSE;
3774 pad->exposed = FALSE;
3775 pad->drained = FALSE;
3776 gst_object_ref_sink (pad);
3780 gst_parse_pad_dispose (GObject * object)
3782 GstParsePad *parsepad = (GstParsePad *) (object);
3783 parse_pad_set_target (parsepad, NULL);
3785 gst_object_replace ((GstObject **) & parsepad->active_collection, NULL);
3786 gst_object_replace ((GstObject **) & parsepad->active_stream, NULL);
3788 G_OBJECT_CLASS (gst_parse_pad_parent_class)->dispose (object);
3791 static GstPadProbeReturn
3792 source_pad_blocked_cb (GstPad * pad, GstPadProbeInfo * info, gpointer user_data)
3794 GstParsePad *parsepad = user_data;
3795 GstParseChain *chain;
3796 GstParseBin *parsebin;
3797 GstPadProbeReturn ret = GST_PAD_PROBE_OK;
3799 if (GST_PAD_PROBE_INFO_TYPE (info) & GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM) {
3800 GstEvent *event = GST_PAD_PROBE_INFO_EVENT (info);
3802 GST_LOG_OBJECT (pad, "Seeing event '%s'", GST_EVENT_TYPE_NAME (event));
3804 if (!GST_EVENT_IS_SERIALIZED (event)) {
3805 /* do not block on sticky or out of band events otherwise the allocation query
3806 from demuxer might block the loop thread */
3807 GST_LOG_OBJECT (pad, "Letting OOB event through");
3808 return GST_PAD_PROBE_PASS;
3811 if (GST_EVENT_IS_STICKY (event) && GST_EVENT_TYPE (event) != GST_EVENT_EOS) {
3814 /* manually push sticky events to ghost pad to avoid exposing pads
3815 * that don't have the sticky events. Handle EOS separately as we
3816 * want to block the pad on it if we didn't get any buffers before
3817 * EOS and expose the pad then. */
3818 peer = gst_pad_get_peer (pad);
3819 gst_pad_send_event (peer, gst_event_ref (event));
3820 gst_object_unref (peer);
3821 GST_LOG_OBJECT (pad, "Manually pushed sticky event through");
3822 ret = GST_PAD_PROBE_HANDLED;
3825 } else if (GST_PAD_PROBE_INFO_TYPE (info) &
3826 GST_PAD_PROBE_TYPE_QUERY_DOWNSTREAM) {
3827 GstQuery *query = GST_PAD_PROBE_INFO_QUERY (info);
3829 if (!GST_QUERY_IS_SERIALIZED (query)) {
3830 /* do not block on non-serialized queries */
3831 GST_LOG_OBJECT (pad, "Letting non-serialized query through");
3832 return GST_PAD_PROBE_PASS;
3834 if (!gst_pad_has_current_caps (pad)) {
3835 /* do not block on allocation queries before we have caps,
3836 * this would deadlock because we are doing no autoplugging
3838 * TODO: Try to do autoplugging based on the query caps
3840 GST_LOG_OBJECT (pad, "Letting serialized query before caps through");
3841 return GST_PAD_PROBE_PASS;
3844 chain = parsepad->chain;
3845 parsebin = chain->parsebin;
3847 GST_LOG_OBJECT (parsepad, "blocked: parsepad->chain:%p", chain);
3849 parsepad->blocked = TRUE;
3851 EXPOSE_LOCK (parsebin);
3852 if (parsebin->parse_chain) {
3853 if (!gst_parse_bin_expose (parsebin))
3854 GST_WARNING_OBJECT (parsebin, "Couldn't expose group");
3856 EXPOSE_UNLOCK (parsebin);
3862 /* FIXME: We can probably do some cleverer things, and maybe move this into
3864 * if there are tags look if it's got an AUDIO_CODEC VIDEO_CODEC CONTAINER_FORMAT tag
3865 * Look at the factory klass designation of parsers in the chain
3866 * Consider demuxer pad names as well, sometimes they give the type away
3868 static GstStreamType
3869 guess_stream_type_from_caps (GstCaps * caps)
3874 if (gst_caps_get_size (caps) < 1)
3875 return GST_STREAM_TYPE_UNKNOWN;
3877 s = gst_caps_get_structure (caps, 0);
3878 name = gst_structure_get_name (s);
3880 if (g_str_has_prefix (name, "video/") || g_str_has_prefix (name, "image/"))
3881 return GST_STREAM_TYPE_VIDEO;
3882 if (g_str_has_prefix (name, "audio/"))
3883 return GST_STREAM_TYPE_AUDIO;
3884 if (g_str_has_prefix (name, "text/") ||
3885 g_str_has_prefix (name, "subpicture/"))
3886 return GST_STREAM_TYPE_TEXT;
3888 return GST_STREAM_TYPE_UNKNOWN;
3892 gst_parse_pad_update_caps (GstParsePad * parsepad, GstCaps * caps)
3894 if (caps && parsepad->active_stream) {
3895 GST_DEBUG_OBJECT (parsepad, "Storing caps %" GST_PTR_FORMAT
3896 " on stream %" GST_PTR_FORMAT, caps, parsepad->active_stream);
3898 if (gst_caps_is_fixed (caps))
3899 gst_stream_set_caps (parsepad->active_stream, caps);
3901 if (gst_stream_get_stream_type (parsepad->active_stream) ==
3902 GST_STREAM_TYPE_UNKNOWN) {
3903 GstStreamType new_type = guess_stream_type_from_caps (caps);
3904 if (new_type != GST_STREAM_TYPE_UNKNOWN)
3905 gst_stream_set_stream_type (parsepad->active_stream, new_type);
3911 gst_parse_pad_update_tags (GstParsePad * parsepad, GstTagList * tags)
3913 if (tags && parsepad->active_stream) {
3914 GST_DEBUG_OBJECT (parsepad, "Storing new tags %" GST_PTR_FORMAT
3915 " on stream %" GST_PTR_FORMAT, tags, parsepad->active_stream);
3916 gst_stream_set_tags (parsepad->active_stream, tags);
3921 gst_parse_pad_stream_start_event (GstParsePad * parsepad, GstEvent * event)
3923 GstStream *stream = NULL;
3924 const gchar *stream_id = NULL;
3925 gboolean repeat_event = FALSE;
3927 gst_event_parse_stream_start (event, &stream_id);
3929 if (parsepad->active_stream != NULL &&
3930 g_str_equal (parsepad->active_stream->stream_id, stream_id))
3931 repeat_event = TRUE;
3933 /* A new stream requires a new collection event, or else
3934 * we'll place it in a fallback collection later */
3935 gst_object_replace ((GstObject **) & parsepad->active_collection, NULL);
3936 parsepad->in_a_fallback_collection = FALSE;
3939 gst_event_parse_stream (event, &stream);
3940 if (stream == NULL) {
3941 GstCaps *caps = gst_pad_get_current_caps (GST_PAD_CAST (parsepad));
3943 /* Try and get caps from the parsepad peer */
3944 GstPad *peer = gst_ghost_pad_get_target (GST_GHOST_PAD (parsepad));
3945 caps = gst_pad_get_current_caps (peer);
3946 gst_object_unref (peer);
3948 if (caps == NULL && parsepad->chain && parsepad->chain->start_caps) {
3949 /* Still no caps, use the chain start caps */
3950 caps = gst_caps_ref (parsepad->chain->start_caps);
3953 GST_DEBUG_OBJECT (parsepad,
3954 "Saw stream_start with no GstStream. Adding one. Caps %"
3955 GST_PTR_FORMAT, caps);
3958 stream = gst_object_ref (parsepad->active_stream);
3961 gst_stream_new (stream_id, NULL, GST_STREAM_TYPE_UNKNOWN,
3962 GST_STREAM_FLAG_NONE);
3963 gst_object_replace ((GstObject **) & parsepad->active_stream,
3964 (GstObject *) stream);
3967 gst_parse_pad_update_caps (parsepad, caps);
3968 gst_caps_unref (caps);
3971 event = gst_event_make_writable (event);
3972 gst_event_set_stream (event, stream);
3974 gst_object_unref (stream);
3975 GST_LOG_OBJECT (parsepad, "Saw stream %s (GstStream %p)",
3976 stream->stream_id, stream);
3982 gst_parse_pad_update_stream_collection (GstParsePad * parsepad,
3983 GstStreamCollection * collection)
3985 GST_LOG_OBJECT (parsepad, "Got new stream collection %p", collection);
3986 gst_object_replace ((GstObject **) & parsepad->active_collection,
3987 (GstObject *) collection);
3988 parsepad->in_a_fallback_collection = FALSE;
3991 static GstPadProbeReturn
3992 gst_parse_pad_event (GstPad * pad, GstPadProbeInfo * info, gpointer user_data)
3994 GstEvent *event = GST_PAD_PROBE_INFO_EVENT (info);
3995 GstObject *parent = gst_pad_get_parent (pad);
3996 GstParsePad *parsepad = GST_PARSE_PAD (parent);
3997 gboolean forwardit = TRUE;
3999 GST_LOG_OBJECT (pad, "%s parsepad:%p", GST_EVENT_TYPE_NAME (event), parsepad);
4001 switch (GST_EVENT_TYPE (event)) {
4002 case GST_EVENT_CAPS:{
4003 GstCaps *caps = NULL;
4004 gst_event_parse_caps (event, &caps);
4005 gst_parse_pad_update_caps (parsepad, caps);
4008 case GST_EVENT_TAG:{
4010 gst_event_parse_tag (event, &tags);
4011 gst_parse_pad_update_tags (parsepad, tags);
4014 case GST_EVENT_STREAM_START:{
4015 GST_PAD_PROBE_INFO_DATA (info) =
4016 gst_parse_pad_stream_start_event (parsepad, event);
4019 case GST_EVENT_STREAM_COLLECTION:{
4020 GstStreamCollection *collection = NULL;
4021 gst_event_parse_stream_collection (event, &collection);
4022 gst_parse_pad_update_stream_collection (parsepad, collection);
4025 case GST_EVENT_EOS:{
4026 GST_DEBUG_OBJECT (pad, "we received EOS");
4028 /* Check if all pads are drained.
4029 * * If there is no next group, we will let the EOS go through.
4030 * * If there is a next group but the current group isn't completely
4031 * drained, we will drop the EOS event.
4032 * * If there is a next group to expose and this was the last non-drained
4033 * pad for that group, we will remove the ghostpad of the current group
4034 * first, which unlinks the peer and so drops the EOS. */
4035 forwardit = gst_parse_pad_handle_eos (parsepad);
4040 gst_object_unref (parent);
4042 return GST_PAD_PROBE_OK;
4044 return GST_PAD_PROBE_DROP;
4048 gst_parse_pad_set_blocked (GstParsePad * parsepad, gboolean blocked)
4050 GstParseBin *parsebin = parsepad->parsebin;
4053 DYN_LOCK (parsebin);
4055 GST_DEBUG_OBJECT (parsepad, "blocking pad: %d", blocked);
4057 opad = gst_ghost_pad_get_target (GST_GHOST_PAD_CAST (parsepad));
4061 /* do not block if shutting down.
4062 * we do not consider/expect it blocked further below, but use other trick */
4063 if (!blocked || !parsebin->shutdown) {
4065 if (parsepad->block_id == 0)
4066 parsepad->block_id =
4067 gst_pad_add_probe (opad,
4068 GST_PAD_PROBE_TYPE_BLOCK_DOWNSTREAM |
4069 GST_PAD_PROBE_TYPE_QUERY_DOWNSTREAM, source_pad_blocked_cb,
4070 gst_object_ref (parsepad), (GDestroyNotify) gst_object_unref);
4072 if (parsepad->block_id != 0) {
4073 gst_pad_remove_probe (opad, parsepad->block_id);
4074 parsepad->block_id = 0;
4076 parsepad->blocked = FALSE;
4081 if (parsebin->shutdown) {
4082 /* deactivate to force flushing state to prevent NOT_LINKED errors */
4083 gst_pad_set_active (GST_PAD_CAST (parsepad), FALSE);
4084 /* note that deactivating the target pad would have no effect here,
4085 * since elements are typically connected first (and pads exposed),
4086 * and only then brought to PAUSED state (so pads activated) */
4088 gst_object_ref (parsepad);
4089 parsebin->blocked_pads =
4090 g_list_prepend (parsebin->blocked_pads, parsepad);
4095 if ((l = g_list_find (parsebin->blocked_pads, parsepad))) {
4096 gst_object_unref (parsepad);
4097 parsebin->blocked_pads = g_list_delete_link (parsebin->blocked_pads, l);
4100 gst_object_unref (opad);
4102 DYN_UNLOCK (parsebin);
4106 gst_parse_pad_activate (GstParsePad * parsepad, GstParseChain * chain)
4108 g_return_if_fail (chain != NULL);
4110 parsepad->chain = chain;
4111 gst_pad_set_active (GST_PAD_CAST (parsepad), TRUE);
4112 gst_parse_pad_set_blocked (parsepad, TRUE);
4116 gst_parse_pad_unblock (GstParsePad * parsepad)
4118 gst_parse_pad_set_blocked (parsepad, FALSE);
4122 gst_parse_pad_query (GstPad * pad, GstObject * parent, GstQuery * query)
4124 GstParsePad *parsepad = GST_PARSE_PAD (parent);
4125 gboolean ret = FALSE;
4127 CHAIN_MUTEX_LOCK (parsepad->chain);
4128 if (!parsepad->exposed && !parsepad->parsebin->shutdown
4129 && !parsepad->chain->deadend && parsepad->chain->elements) {
4130 GstParseElement *delem = parsepad->chain->elements->data;
4133 GST_DEBUG_OBJECT (parsepad->parsebin,
4134 "calling autoplug-query for %s (element %s): %" GST_PTR_FORMAT,
4135 GST_PAD_NAME (parsepad), GST_ELEMENT_NAME (delem->element), query);
4136 g_signal_emit (G_OBJECT (parsepad->parsebin),
4137 gst_parse_bin_signals[SIGNAL_AUTOPLUG_QUERY], 0, parsepad,
4138 delem->element, query, &ret);
4141 GST_DEBUG_OBJECT (parsepad->parsebin,
4142 "autoplug-query returned %d: %" GST_PTR_FORMAT, ret, query);
4144 GST_DEBUG_OBJECT (parsepad->parsebin, "autoplug-query returned %d", ret);
4146 CHAIN_MUTEX_UNLOCK (parsepad->chain);
4148 /* If exposed or nothing handled the query use the default handler */
4150 ret = gst_pad_query_default (pad, parent, query);
4155 /*gst_parse_pad_new:
4157 * Creates a new GstParsePad for the given pad.
4159 static GstParsePad *
4160 gst_parse_pad_new (GstParseBin * parsebin, GstParseChain * chain)
4162 GstParsePad *parsepad;
4164 GstPadTemplate *pad_tmpl;
4166 GST_DEBUG_OBJECT (parsebin, "making new decodepad");
4167 pad_tmpl = gst_static_pad_template_get (&decoder_bin_src_template);
4169 g_object_new (GST_TYPE_PARSE_PAD, "direction", GST_PAD_SRC,
4170 "template", pad_tmpl, NULL);
4171 gst_ghost_pad_construct (GST_GHOST_PAD_CAST (parsepad));
4172 parsepad->chain = chain;
4173 parsepad->parsebin = parsebin;
4174 gst_object_unref (pad_tmpl);
4176 ppad = gst_proxy_pad_get_internal (GST_PROXY_PAD (parsepad));
4177 gst_pad_set_query_function (GST_PAD_CAST (ppad), gst_parse_pad_query);
4179 /* Add downstream event probe */
4180 GST_LOG_OBJECT (parsepad, "Adding event probe on internal pad %"
4181 GST_PTR_FORMAT, ppad);
4182 gst_pad_add_probe (GST_PAD_CAST (ppad),
4183 GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM, gst_parse_pad_event, parsepad, NULL);
4184 gst_object_unref (ppad);
4190 gst_pending_pad_free (GstPendingPad * ppad)
4193 g_assert (ppad->pad);
4195 if (ppad->event_probe_id != 0)
4196 gst_pad_remove_probe (ppad->pad, ppad->event_probe_id);
4197 if (ppad->notify_caps_id)
4198 g_signal_handler_disconnect (ppad->pad, ppad->notify_caps_id);
4199 gst_object_unref (ppad->pad);
4200 g_slice_free (GstPendingPad, ppad);
4204 * Element add/remove
4208 do_async_start (GstParseBin * parsebin)
4210 GstMessage *message;
4212 parsebin->async_pending = TRUE;
4214 message = gst_message_new_async_start (GST_OBJECT_CAST (parsebin));
4215 parent_class->handle_message (GST_BIN_CAST (parsebin), message);
4219 do_async_done (GstParseBin * parsebin)
4221 GstMessage *message;
4223 if (parsebin->async_pending) {
4225 gst_message_new_async_done (GST_OBJECT_CAST (parsebin),
4226 GST_CLOCK_TIME_NONE);
4227 parent_class->handle_message (GST_BIN_CAST (parsebin), message);
4229 parsebin->async_pending = FALSE;
4233 /* call with dyn_lock held */
4235 unblock_pads (GstParseBin * parsebin)
4239 GST_LOG_OBJECT (parsebin, "unblocking pads");
4241 for (tmp = parsebin->blocked_pads; tmp; tmp = tmp->next) {
4242 GstParsePad *parsepad = (GstParsePad *) tmp->data;
4245 opad = gst_ghost_pad_get_target (GST_GHOST_PAD_CAST (parsepad));
4249 GST_DEBUG_OBJECT (parsepad, "unblocking");
4250 if (parsepad->block_id != 0) {
4251 gst_pad_remove_probe (opad, parsepad->block_id);
4252 parsepad->block_id = 0;
4254 parsepad->blocked = FALSE;
4255 /* make flushing, prevent NOT_LINKED */
4256 gst_pad_set_active (GST_PAD_CAST (parsepad), FALSE);
4257 gst_object_unref (parsepad);
4258 gst_object_unref (opad);
4259 GST_DEBUG_OBJECT (parsepad, "unblocked");
4262 /* clear, no more blocked pads */
4263 g_list_free (parsebin->blocked_pads);
4264 parsebin->blocked_pads = NULL;
4267 static GstStateChangeReturn
4268 gst_parse_bin_change_state (GstElement * element, GstStateChange transition)
4270 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
4271 GstParseBin *parsebin = GST_PARSE_BIN (element);
4272 GstParseChain *chain_to_free = NULL;
4274 switch (transition) {
4275 case GST_STATE_CHANGE_NULL_TO_READY:
4276 if (parsebin->typefind == NULL)
4277 goto missing_typefind;
4279 case GST_STATE_CHANGE_READY_TO_PAUSED:
4280 /* Make sure we've cleared all existing chains */
4281 EXPOSE_LOCK (parsebin);
4282 if (parsebin->parse_chain) {
4283 gst_parse_chain_free (parsebin->parse_chain);
4284 parsebin->parse_chain = NULL;
4286 EXPOSE_UNLOCK (parsebin);
4287 DYN_LOCK (parsebin);
4288 GST_LOG_OBJECT (parsebin, "clearing shutdown flag");
4289 parsebin->shutdown = FALSE;
4290 DYN_UNLOCK (parsebin);
4291 parsebin->have_type = FALSE;
4292 ret = GST_STATE_CHANGE_ASYNC;
4293 do_async_start (parsebin);
4296 /* connect a signal to find out when the typefind element found
4298 parsebin->have_type_id =
4299 g_signal_connect (parsebin->typefind, "have-type",
4300 G_CALLBACK (type_found), parsebin);
4302 case GST_STATE_CHANGE_PAUSED_TO_READY:
4303 if (parsebin->have_type_id)
4304 g_signal_handler_disconnect (parsebin->typefind,
4305 parsebin->have_type_id);
4306 parsebin->have_type_id = 0;
4307 DYN_LOCK (parsebin);
4308 GST_LOG_OBJECT (parsebin, "setting shutdown flag");
4309 parsebin->shutdown = TRUE;
4310 unblock_pads (parsebin);
4311 DYN_UNLOCK (parsebin);
4317 GstStateChangeReturn bret;
4319 bret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
4320 if (G_UNLIKELY (bret == GST_STATE_CHANGE_FAILURE))
4321 goto activate_failed;
4322 else if (G_UNLIKELY (bret == GST_STATE_CHANGE_NO_PREROLL)) {
4323 do_async_done (parsebin);
4327 switch (transition) {
4328 case GST_STATE_CHANGE_PAUSED_TO_READY:
4329 do_async_done (parsebin);
4330 EXPOSE_LOCK (parsebin);
4331 if (parsebin->parse_chain) {
4332 chain_to_free = parsebin->parse_chain;
4333 gst_parse_chain_free_internal (parsebin->parse_chain, TRUE);
4334 parsebin->parse_chain = NULL;
4336 EXPOSE_UNLOCK (parsebin);
4338 gst_parse_chain_free (chain_to_free);
4340 case GST_STATE_CHANGE_READY_TO_NULL:
4350 gst_element_post_message (element,
4351 gst_missing_element_message_new (element, "typefind"));
4352 GST_ELEMENT_ERROR (parsebin, CORE, MISSING_PLUGIN, (NULL),
4354 return GST_STATE_CHANGE_FAILURE;
4358 GST_DEBUG_OBJECT (element,
4359 "element failed to change states -- activation problem?");
4360 do_async_done (parsebin);
4361 return GST_STATE_CHANGE_FAILURE;
4366 gst_parse_bin_handle_message (GstBin * bin, GstMessage * msg)
4368 GstParseBin *parsebin = GST_PARSE_BIN (bin);
4369 gboolean drop = FALSE;
4371 switch (GST_MESSAGE_TYPE (msg)) {
4372 case GST_MESSAGE_ERROR:{
4373 GST_OBJECT_LOCK (parsebin);
4374 drop = (g_list_find (parsebin->filtered, GST_MESSAGE_SRC (msg)) != NULL);
4376 parsebin->filtered_errors =
4377 g_list_prepend (parsebin->filtered_errors, gst_message_ref (msg));
4378 GST_OBJECT_UNLOCK (parsebin);
4386 gst_message_unref (msg);
4388 GST_BIN_CLASS (parent_class)->handle_message (bin, msg);
4392 gst_parse_bin_plugin_init (GstPlugin * plugin)
4394 GST_DEBUG_CATEGORY_INIT (gst_parse_bin_debug, "parsebin", 0, "parser bin");
4396 return gst_element_register (plugin, "parsebin", GST_RANK_NONE,
4397 GST_TYPE_PARSE_BIN);