2 * Copyright (C) <2006> Edward Hervey <edward@fluendo.com>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * SECTION:element-decodebin2
22 * @short_description: Next-generation automatic decoding bin
24 * #GstBin that auto-magically constructs a decoding pipeline using available
25 * decoders and demuxers via auto-plugging.
27 * At this stage, decodebin2 is considered UNSTABLE. The API provided in the
28 * signals is expected to change in the near future.
30 * To try out decodebin2, you can set the USE_DECODEBIN2 environment
31 * variable (USE_DECODEBIN2=1 for example). This will cause playbin to use
32 * decodebin2 instead of the older decodebin for its internal auto-plugging.
42 #include "gstplay-marshal.h"
44 /* generic templates */
45 static GstStaticPadTemplate decoder_bin_sink_template =
46 GST_STATIC_PAD_TEMPLATE ("sink",
51 static GstStaticPadTemplate decoder_bin_src_template =
52 GST_STATIC_PAD_TEMPLATE ("src%d",
57 GST_DEBUG_CATEGORY_STATIC (gst_decode_bin_debug);
58 #define GST_CAT_DEFAULT gst_decode_bin_debug
60 typedef struct _GstDecodeGroup GstDecodeGroup;
61 typedef struct _GstDecodePad GstDecodePad;
62 typedef struct _GstDecodeBin GstDecodeBin;
63 typedef struct _GstDecodeBin GstDecodeBin2;
64 typedef struct _GstDecodeBinClass GstDecodeBinClass;
66 #define GST_TYPE_DECODE_BIN (gst_decode_bin_get_type())
67 #define GST_DECODE_BIN_CAST(obj) ((GstDecodeBin*)(obj))
68 #define GST_DECODE_BIN(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_DECODE_BIN,GstDecodeBin))
69 #define GST_DECODE_BIN_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_DECODE_BIN,GstDecodeBinClass))
70 #define GST_IS_DECODE_BIN(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_DECODE_BIN))
71 #define GST_IS_DECODE_BIN_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_DECODE_BIN))
76 * The opaque #DecodeBin2 data structure
80 GstBin bin; /* we extend GstBin */
82 GstElement *typefind; /* this holds the typefind object */
85 GMutex *lock; /* Protects activegroup and groups */
86 GstDecodeGroup *activegroup; /* group currently active */
87 GList *groups; /* List of non-active GstDecodeGroups, sorted in
88 * order of creation. */
89 GList *oldgroups; /* List of no-longer-used GstDecodeGroups.
90 * Should be freed in dispose */
91 gint nbpads; /* unique identifier for source pads */
92 GstCaps *caps; /* caps on which to stop decoding */
94 GList *factories; /* factories we can use for selecting elements */
97 struct _GstDecodeBinClass
99 GstBinClass parent_class;
101 /* signal we fire when a new pad has been decoded into raw audio/video */
102 void (*new_decoded_pad) (GstElement * element, GstPad * pad, gboolean last);
103 /* signal we fire when a pad has been removed */
104 void (*removed_decoded_pad) (GstElement * element, GstPad * pad);
105 /* signal fired when we found a pad that we cannot decode */
106 void (*unknown_type) (GstElement * element, GstPad * pad, GstCaps * caps);
107 /* signal fired to know if we continue trying to decode the given caps */
108 gboolean (*autoplug_continue) (GstElement * element, GstCaps * caps);
109 /* signal fired to reorder the proposed list of factories */
110 gboolean (*autoplug_sort) (GstElement * element, GstCaps * caps,
117 SIGNAL_NEW_DECODED_PAD,
118 SIGNAL_REMOVED_DECODED_PAD,
120 SIGNAL_AUTOPLUG_CONTINUE,
121 SIGNAL_AUTOPLUG_SORT,
132 static GstBinClass *parent_class;
133 static guint gst_decode_bin_signals[LAST_SIGNAL] = { 0 };
135 static const GstElementDetails gst_decode_bin_details =
136 GST_ELEMENT_DETAILS ("Decoder Bin",
137 "Generic/Bin/Decoder",
138 "Autoplug and decode to raw media",
139 "Edward Hervey <edward@fluendo.com>");
142 static gboolean add_fakesink (GstDecodeBin * decode_bin);
143 static void remove_fakesink (GstDecodeBin * decode_bin);
145 static void type_found (GstElement * typefind, guint probability,
146 GstCaps * caps, GstDecodeBin * decode_bin);
148 static gboolean gst_decode_bin_autoplug_continue (GstElement * element,
150 static gboolean gst_decode_bin_autoplug_sort (GstElement * element,
151 GstCaps * caps, GList ** list);
152 static void gst_decode_bin_set_property (GObject * object, guint prop_id,
153 const GValue * value, GParamSpec * pspec);
154 static void gst_decode_bin_get_property (GObject * object, guint prop_id,
155 GValue * value, GParamSpec * pspec);
156 static void gst_decode_bin_set_caps (GstDecodeBin * dbin, GstCaps * caps);
157 static GstCaps *gst_decode_bin_get_caps (GstDecodeBin * dbin);
159 static GstPad *find_sink_pad (GstElement * element);
160 static GstStateChangeReturn gst_decode_bin_change_state (GstElement * element,
161 GstStateChange transition);
163 #define DECODE_BIN_LOCK(dbin) G_STMT_START { \
164 GST_LOG_OBJECT (dbin, \
165 "locking from thread %p", \
167 g_mutex_lock (GST_DECODE_BIN_CAST(dbin)->lock); \
168 GST_LOG_OBJECT (dbin, \
169 "locked from thread %p", \
173 #define DECODE_BIN_UNLOCK(dbin) G_STMT_START { \
174 GST_LOG_OBJECT (dbin, \
175 "unlocking from thread %p", \
177 g_mutex_unlock (GST_DECODE_BIN_CAST(dbin)->lock); \
182 * Streams belonging to the same group/chain of a media file
186 struct _GstDecodeGroup
190 GstElement *multiqueue;
191 gboolean exposed; /* TRUE if this group is exposed */
192 gboolean drained; /* TRUE if EOS went throug all endpads */
193 gboolean blocked; /* TRUE if all endpads are blocked */
194 gboolean complete; /* TRUE if we are not expecting anymore streams
198 guint nbdynamic; /* number of dynamic pads in the group. */
200 GList *endpads; /* List of GstDecodePad of source pads to be exposed */
201 GList *ghosts; /* List of GstGhostPad for the endpads */
204 #define GROUP_MUTEX_LOCK(group) G_STMT_START { \
205 GST_LOG_OBJECT (group->dbin, \
206 "locking group %p from thread %p", \
207 group, g_thread_self ()); \
208 g_mutex_lock (group->lock); \
209 GST_LOG_OBJECT (group->dbin, \
210 "locked group %p from thread %p", \
211 group, g_thread_self ()); \
214 #define GROUP_MUTEX_UNLOCK(group) G_STMT_START { \
215 GST_LOG_OBJECT (group->dbin, \
216 "unlocking group %p from thread %p", \
217 group, g_thread_self ()); \
218 g_mutex_unlock (group->lock); \
222 static GstDecodeGroup *gst_decode_group_new (GstDecodeBin * decode_bin);
223 static GstPad *gst_decode_group_control_demuxer_pad (GstDecodeGroup * group,
225 static gboolean gst_decode_group_control_source_pad (GstDecodeGroup * group,
227 static gboolean gst_decode_group_expose (GstDecodeGroup * group);
228 static void gst_decode_group_check_if_blocked (GstDecodeGroup * group);
229 static void gst_decode_group_set_complete (GstDecodeGroup * group);
230 static void gst_decode_group_hide (GstDecodeGroup * group);
231 static void gst_decode_group_free (GstDecodeGroup * group);
235 * GstPad private used for source pads of groups
241 GstDecodeGroup *group;
246 static GstDecodePad *gst_decode_pad_new (GstDecodeGroup * group, GstPad * pad,
248 static void source_pad_blocked_cb (GstPad * pad, gboolean blocked,
249 GstDecodePad * dpad);
252 * Internal structure used for pads which have more than one structure.
254 typedef struct _TempPadStruct
257 GstDecodeGroup *group;
260 /********************************
261 * Standard GObject boilerplate *
262 ********************************/
264 static void gst_decode_bin_class_init (GstDecodeBinClass * klass);
265 static void gst_decode_bin_init (GstDecodeBin * decode_bin);
266 static void gst_decode_bin_dispose (GObject * object);
267 static void gst_decode_bin_finalize (GObject * object);
270 gst_decode_bin_get_type (void)
272 static GType gst_decode_bin_type = 0;
274 if (!gst_decode_bin_type) {
275 static const GTypeInfo gst_decode_bin_info = {
276 sizeof (GstDecodeBinClass),
279 (GClassInitFunc) gst_decode_bin_class_init,
282 sizeof (GstDecodeBin),
284 (GInstanceInitFunc) gst_decode_bin_init,
288 gst_decode_bin_type =
289 g_type_register_static (GST_TYPE_BIN, "GstDecodeBin2",
290 &gst_decode_bin_info, 0);
293 return gst_decode_bin_type;
297 _gst_boolean_accumulator (GSignalInvocationHint * ihint,
298 GValue * return_accu, const GValue * handler_return, gpointer dummy)
302 myboolean = g_value_get_boolean (handler_return);
303 if (!(ihint->run_type & G_SIGNAL_RUN_CLEANUP))
304 g_value_set_boolean (return_accu, myboolean);
306 /* stop emission if FALSE */
311 gst_decode_bin_class_init (GstDecodeBinClass * klass)
313 GObjectClass *gobject_klass;
314 GstElementClass *gstelement_klass;
315 GstBinClass *gstbin_klass;
317 gobject_klass = (GObjectClass *) klass;
318 gstelement_klass = (GstElementClass *) klass;
319 gstbin_klass = (GstBinClass *) klass;
321 parent_class = g_type_class_peek_parent (klass);
323 gobject_klass->dispose = GST_DEBUG_FUNCPTR (gst_decode_bin_dispose);
324 gobject_klass->finalize = GST_DEBUG_FUNCPTR (gst_decode_bin_finalize);
325 gobject_klass->set_property = GST_DEBUG_FUNCPTR (gst_decode_bin_set_property);
326 gobject_klass->get_property = GST_DEBUG_FUNCPTR (gst_decode_bin_get_property);
329 * GstDecodeBin2::new-decoded-pad:
330 * @pad: the newly created pad
331 * @islast: #TRUE if this is the last pad to be added. Deprecated.
333 * This signal gets emitted as soon as a new pad of the same type as one of
334 * the valid 'raw' types is added.
337 gst_decode_bin_signals[SIGNAL_NEW_DECODED_PAD] =
338 g_signal_new ("new-decoded-pad", G_TYPE_FROM_CLASS (klass),
340 G_STRUCT_OFFSET (GstDecodeBinClass, new_decoded_pad), NULL, NULL,
341 gst_play_marshal_VOID__OBJECT_BOOLEAN, G_TYPE_NONE, 2, GST_TYPE_PAD,
345 * GstDecodeBin2::removed-decoded-pad:
346 * @pad: the pad that was removed
348 * This signal is emitted when a 'final' caps pad has been removed.
351 gst_decode_bin_signals[SIGNAL_REMOVED_DECODED_PAD] =
352 g_signal_new ("removed-decoded-pad", G_TYPE_FROM_CLASS (klass),
354 G_STRUCT_OFFSET (GstDecodeBinClass, removed_decoded_pad), NULL, NULL,
355 gst_marshal_VOID__OBJECT, G_TYPE_NONE, 1, GST_TYPE_PAD);
358 * GstDecodeBin2::unknown-type:
359 * @pad: the new pad containing caps that cannot be resolved to a 'final' stream type.
360 * @caps: the #GstCaps of the pad that cannot be resolved.
362 * This signal is emitted when a pad for which there is no further possible
363 * decoding is added to the decodebin.
366 gst_decode_bin_signals[SIGNAL_UNKNOWN_TYPE] =
367 g_signal_new ("unknown-type", G_TYPE_FROM_CLASS (klass),
368 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstDecodeBinClass, unknown_type),
369 NULL, NULL, gst_marshal_VOID__OBJECT_OBJECT, G_TYPE_NONE, 2,
370 GST_TYPE_PAD, GST_TYPE_CAPS);
373 * GstDecodeBin2::autoplug-continue:
374 * @caps: The #GstCaps found.
376 * This signal is emitted whenever decodebin2 finds a new stream. It is
377 * emitted before looking for any elements that can handle that stream.
379 * Returns: #TRUE if you wish decodebin2 to look for elements that can
380 * handle the given @caps. If #FALSE, those caps will be considered as
381 * final and the pad will be exposed as such (see 'new-decoded-pad'
385 gst_decode_bin_signals[SIGNAL_AUTOPLUG_CONTINUE] =
386 g_signal_new ("autoplug-continue", G_TYPE_FROM_CLASS (klass),
387 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstDecodeBinClass, autoplug_continue),
388 _gst_boolean_accumulator, NULL, gst_play_marshal_BOOLEAN__OBJECT,
389 G_TYPE_BOOLEAN, 1, GST_TYPE_CAPS);
392 * GstDecodeBin2::autoplug-sort:
393 * @caps: The #GstCaps.
394 * @factories: A #GList of possible #GstElementFactory to use.
396 * This signal is emitted once decodebin2 has found all the possible
397 * #GstElementFactory that can be used to handle the given @caps.
399 * UNSTABLE API. Will change soon.
401 * Returns: #TRUE if you wish decodebin2 to start trying to decode
402 * the given @caps with the list of factories. #FALSE if you do not want
403 * these #GstCaps, if so the pad will be exposed as unknown (see
404 * 'unknown-type' signal).
407 gst_decode_bin_signals[SIGNAL_AUTOPLUG_SORT] =
408 g_signal_new ("autoplug-sort", G_TYPE_FROM_CLASS (klass),
409 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstDecodeBinClass, autoplug_sort),
410 _gst_boolean_accumulator, NULL, gst_play_marshal_BOOLEAN__OBJECT_POINTER,
411 G_TYPE_BOOLEAN, 2, GST_TYPE_CAPS, G_TYPE_POINTER);
413 g_object_class_install_property (gobject_klass, PROP_CAPS,
414 g_param_spec_boxed ("caps", "Caps", "The caps on which to stop decoding.",
415 GST_TYPE_CAPS, G_PARAM_READWRITE));
417 klass->autoplug_continue =
418 GST_DEBUG_FUNCPTR (gst_decode_bin_autoplug_continue);
419 klass->autoplug_sort = GST_DEBUG_FUNCPTR (gst_decode_bin_autoplug_sort);
421 gst_element_class_add_pad_template (gstelement_klass,
422 gst_static_pad_template_get (&decoder_bin_sink_template));
423 gst_element_class_add_pad_template (gstelement_klass,
424 gst_static_pad_template_get (&decoder_bin_src_template));
426 gst_element_class_set_details (gstelement_klass, &gst_decode_bin_details);
428 gstelement_klass->change_state =
429 GST_DEBUG_FUNCPTR (gst_decode_bin_change_state);
432 /* the filter function for selecting the elements we can use in
435 gst_decode_bin_factory_filter (GstPluginFeature * feature,
436 GstDecodeBin * decode_bin)
441 /* we only care about element factories */
442 if (!GST_IS_ELEMENT_FACTORY (feature))
445 klass = gst_element_factory_get_klass (GST_ELEMENT_FACTORY (feature));
446 /* only demuxers, decoders and parsers can play */
447 if (strstr (klass, "Demux") == NULL &&
448 strstr (klass, "Decoder") == NULL && strstr (klass, "Parse") == NULL) {
452 /* only select elements with autoplugging rank */
453 rank = gst_plugin_feature_get_rank (feature);
454 if (rank < GST_RANK_MARGINAL)
460 /* function used to sort element features */
462 compare_ranks (GstPluginFeature * f1, GstPluginFeature * f2)
465 const gchar *rname1, *rname2;
467 diff = gst_plugin_feature_get_rank (f2) - gst_plugin_feature_get_rank (f1);
471 rname1 = gst_plugin_feature_get_name (f1);
472 rname2 = gst_plugin_feature_get_name (f2);
474 diff = strcmp (rname2, rname1);
480 print_feature (GstPluginFeature * feature)
484 rname = gst_plugin_feature_get_name (feature);
486 GST_DEBUG ("%s", rname);
490 gst_decode_bin_init (GstDecodeBin * decode_bin)
494 /* first filter out the interesting element factories */
495 factories = gst_default_registry_feature_filter (
496 (GstPluginFeatureFilter) gst_decode_bin_factory_filter,
499 /* sort them according to their ranks */
500 decode_bin->factories = g_list_sort (factories, (GCompareFunc) compare_ranks);
501 /* do some debugging */
502 g_list_foreach (decode_bin->factories, (GFunc) print_feature, NULL);
505 /* we create the typefind element only once */
506 decode_bin->typefind = gst_element_factory_make ("typefind", "typefind");
507 if (!decode_bin->typefind) {
508 g_warning ("can't find typefind element, decodebin will not work");
513 /* add the typefind element */
514 if (!gst_bin_add (GST_BIN (decode_bin), decode_bin->typefind)) {
515 g_warning ("Could not add typefind element, decodebin will not work");
516 gst_object_unref (decode_bin->typefind);
517 decode_bin->typefind = NULL;
520 /* get the sinkpad */
521 pad = gst_element_get_pad (decode_bin->typefind, "sink");
523 /* ghost the sink pad to ourself */
524 gpad = gst_ghost_pad_new ("sink", pad);
525 gst_pad_set_active (gpad, TRUE);
526 gst_element_add_pad (GST_ELEMENT (decode_bin), gpad);
528 gst_object_unref (pad);
530 /* connect a signal to find out when the typefind element found
532 g_signal_connect (G_OBJECT (decode_bin->typefind), "have_type",
533 G_CALLBACK (type_found), decode_bin);
536 decode_bin->lock = g_mutex_new ();
537 decode_bin->activegroup = NULL;
538 decode_bin->groups = NULL;
541 gst_caps_from_string ("video/x-raw-yuv;video/x-raw-rgb;video/x-raw-gray;"
542 "audio/x-raw-int;audio/x-raw-float;" "text/plain;text/x-pango-markup");
544 add_fakesink (decode_bin);
550 gst_decode_bin_dispose (GObject * object)
552 GstDecodeBin *decode_bin;
555 decode_bin = GST_DECODE_BIN (object);
557 if (decode_bin->factories)
558 gst_plugin_feature_list_free (decode_bin->factories);
559 decode_bin->factories = NULL;
561 if (decode_bin->activegroup) {
562 gst_decode_group_free (decode_bin->activegroup);
563 decode_bin->activegroup = NULL;
567 for (tmp = decode_bin->groups; tmp; tmp = g_list_next (tmp)) {
568 GstDecodeGroup *group = (GstDecodeGroup *) tmp->data;
570 gst_decode_group_free (group);
572 g_list_free (decode_bin->groups);
573 decode_bin->groups = NULL;
575 for (tmp = decode_bin->oldgroups; tmp; tmp = g_list_next (tmp)) {
576 GstDecodeGroup *group = (GstDecodeGroup *) tmp->data;
578 gst_decode_group_free (group);
580 g_list_free (decode_bin->oldgroups);
581 decode_bin->oldgroups = NULL;
583 if (decode_bin->caps)
584 gst_caps_unref (decode_bin->caps);
585 decode_bin->caps = NULL;
586 remove_fakesink (decode_bin);
588 G_OBJECT_CLASS (parent_class)->dispose (object);
592 gst_decode_bin_finalize (GObject * object)
594 GstDecodeBin *decode_bin;
596 decode_bin = GST_DECODE_BIN (object);
598 if (decode_bin->lock) {
599 g_mutex_free (decode_bin->lock);
600 decode_bin->lock = NULL;
603 G_OBJECT_CLASS (parent_class)->finalize (object);
607 gst_decode_bin_set_property (GObject * object, guint prop_id,
608 const GValue * value, GParamSpec * pspec)
612 dbin = GST_DECODE_BIN (object);
616 gst_decode_bin_set_caps (dbin, (GstCaps *) g_value_dup_boxed (value));
619 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
625 gst_decode_bin_get_property (GObject * object, guint prop_id,
626 GValue * value, GParamSpec * pspec)
630 dbin = GST_DECODE_BIN (object);
633 g_value_take_boxed (value, gst_decode_bin_get_caps (dbin));
637 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
644 * Changes the caps on which decodebin will stop decoding.
645 * Will unref the previously set one. The refcount of the given caps will be
653 gst_decode_bin_set_caps (GstDecodeBin * dbin, GstCaps * caps)
655 GST_DEBUG_OBJECT (dbin, "Setting new caps: %" GST_PTR_FORMAT, caps);
657 DECODE_BIN_LOCK (dbin);
659 gst_caps_unref (dbin->caps);
661 DECODE_BIN_UNLOCK (dbin);
665 * Returns the currently configured caps on which decodebin will stop decoding.
666 * The returned caps (if not NULL), will have its refcount incremented.
672 gst_decode_bin_get_caps (GstDecodeBin * dbin)
676 GST_DEBUG_OBJECT (dbin, "Getting currently set caps");
678 DECODE_BIN_LOCK (dbin);
682 DECODE_BIN_UNLOCK (dbin);
688 * Default autoplug signal handlers
692 gst_decode_bin_autoplug_continue (GstElement * element, GstCaps * caps)
698 gst_decode_bin_autoplug_sort (GstElement * element, GstCaps * caps,
710 static gboolean are_raw_caps (GstDecodeBin * dbin, GstCaps * caps);
711 static gboolean is_demuxer_element (GstElement * srcelement);
712 static GList *find_compatibles (GstDecodeBin * decode_bin,
713 const GstCaps * caps);
715 static gboolean connect_pad (GstDecodeBin * dbin, GstElement * src,
716 GstPad * pad, GList * factories, GstDecodeGroup * group);
717 static gboolean connect_element (GstDecodeBin * dbin, GstElement * element,
718 GstDecodeGroup * group);
719 static void expose_pad (GstDecodeBin * dbin, GstElement * src, GstPad * pad,
720 GstDecodeGroup * group);
722 static void pad_added_group_cb (GstElement * element, GstPad * pad,
723 GstDecodeGroup * group);
724 static void pad_removed_group_cb (GstElement * element, GstPad * pad,
725 GstDecodeGroup * group);
726 static void no_more_pads_group_cb (GstElement * element,
727 GstDecodeGroup * group);
728 static void pad_added_cb (GstElement * element, GstPad * pad,
729 GstDecodeBin * dbin);
730 static void pad_removed_cb (GstElement * element, GstPad * pad,
731 GstDecodeBin * dbin);
732 static void no_more_pads_cb (GstElement * element, GstDecodeBin * dbin);
734 static GstDecodeGroup *get_current_group (GstDecodeBin * dbin);
737 analyze_new_pad (GstDecodeBin * dbin, GstElement * src, GstPad * pad,
738 GstCaps * caps, GstDecodeGroup * group)
740 gboolean apcontinue = TRUE;
741 GList *factories = NULL;
742 gboolean apsort = TRUE;
744 GST_DEBUG_OBJECT (dbin, "Pad %s:%s caps:%" GST_PTR_FORMAT,
745 GST_DEBUG_PAD_NAME (pad), caps);
747 if ((caps == NULL) || gst_caps_is_empty (caps))
750 if (gst_caps_is_any (caps))
753 /* 1. Emit 'autoplug-continue' */
754 g_signal_emit (G_OBJECT (dbin),
755 gst_decode_bin_signals[SIGNAL_AUTOPLUG_CONTINUE], 0, caps, &apcontinue);
757 /* 1.a if autoplug-continue is FALSE or caps is a raw format, goto pad_is_final */
758 if ((!apcontinue) || are_raw_caps (dbin, caps))
761 /* 1.b else if there's no compatible factory or 'autoplug-sort' returned FALSE, goto pad_not_used */
762 if ((factories = find_compatibles (dbin, caps))) {
763 /* emit autoplug-sort */
764 g_signal_emit (G_OBJECT (dbin),
765 gst_decode_bin_signals[SIGNAL_AUTOPLUG_SORT],
766 0, caps, &factories, &apsort);
768 g_list_free (factories);
769 /* User doesn't want that pad */
773 /* no compatible factories */
777 /* 1.c else goto pad_is_valid */
778 GST_LOG_OBJECT (pad, "Let's continue discovery on this pad");
780 connect_pad (dbin, src, pad, factories, group);
781 g_list_free (factories);
787 GST_LOG_OBJECT (dbin, "Pad is final. autoplug-continue:%d", apcontinue);
788 expose_pad (dbin, src, pad, group);
794 GST_LOG_OBJECT (pad, "User doesn't want this pad, stopping discovery");
800 GST_LOG_OBJECT (pad, "Unknown type, firing signal");
801 g_signal_emit (G_OBJECT (dbin),
802 gst_decode_bin_signals[SIGNAL_UNKNOWN_TYPE], 0, pad, caps);
804 /* Check if there are no pending groups, if so, remove fakesink */
805 if (dbin->groups == NULL)
806 remove_fakesink (dbin);
813 GST_WARNING_OBJECT (pad,
814 "pad has ANY caps, not able to autoplug to anything");
815 /* FIXME : connect to caps notification */
823 * Try to connect the given pad to an element created from one of the factories,
826 * Returns TRUE if an element was properly created and linked
830 connect_pad (GstDecodeBin * dbin, GstElement * src, GstPad * pad,
831 GList * factories, GstDecodeGroup * group)
833 gboolean res = FALSE;
836 g_return_val_if_fail (factories != NULL, FALSE);
837 GST_DEBUG_OBJECT (dbin, "pad %s:%s , group:%p",
838 GST_DEBUG_PAD_NAME (pad), group);
840 /* 1. is element demuxer or parser */
841 if (is_demuxer_element (src)) {
844 GST_LOG_OBJECT (src, "is a demuxer, connecting the pad through multiqueue");
847 if (!(group = get_current_group (dbin))) {
848 group = gst_decode_group_new (dbin);
849 DECODE_BIN_LOCK (dbin);
850 dbin->groups = g_list_append (dbin->groups, group);
851 DECODE_BIN_UNLOCK (dbin);
854 if (!(mqpad = gst_decode_group_control_demuxer_pad (group, pad)))
859 /* 2. Try to create an element and link to it */
860 for (tmp = factories; tmp; tmp = g_list_next (tmp)) {
861 GstElementFactory *factory = (GstElementFactory *) tmp->data;
865 /* 2.1. Try to create an element */
866 if ((element = gst_element_factory_create (factory, NULL)) == NULL) {
867 GST_WARNING_OBJECT (dbin, "Could not create an element from %s",
868 gst_plugin_feature_get_name (GST_PLUGIN_FEATURE (factory)));
872 /* 2.3. Find its sink pad */
873 if (!(sinkpad = find_sink_pad (element))) {
874 GST_WARNING_OBJECT (dbin, "Element %s doesn't have a sink pad",
875 GST_ELEMENT_NAME (element));
876 gst_object_unref (element);
881 if (!(gst_bin_add (GST_BIN (dbin), element))) {
882 GST_WARNING_OBJECT (dbin, "Couldn't add %s to the bin",
883 GST_ELEMENT_NAME (element));
884 gst_object_unref (sinkpad);
885 gst_object_unref (element);
889 /* ... activate it ... */
890 if ((gst_element_set_state (element,
891 GST_STATE_READY)) == GST_STATE_CHANGE_FAILURE) {
892 GST_WARNING_OBJECT (dbin, "Couldn't set %s to READY",
893 GST_ELEMENT_NAME (element));
894 gst_object_unref (sinkpad);
895 gst_bin_remove (GST_BIN (dbin), element);
899 /* 2.5 ...and try to link */
900 if ((gst_pad_link (pad, sinkpad)) != GST_PAD_LINK_OK) {
901 GST_WARNING_OBJECT (dbin, "Link failed on pad %s:%s",
902 GST_DEBUG_PAD_NAME (sinkpad));
903 gst_element_set_state (element, GST_STATE_NULL);
904 gst_object_unref (sinkpad);
905 gst_bin_remove (GST_BIN (dbin), element);
909 GST_LOG_OBJECT (dbin, "linked on pad %s:%s", GST_DEBUG_PAD_NAME (pad));
911 /* link this element further */
912 connect_element (dbin, element, group);
914 /* Bring the element to the state of the parent */
915 if ((gst_element_set_state (element,
916 GST_STATE_PAUSED)) == GST_STATE_CHANGE_FAILURE) {
917 GST_WARNING_OBJECT (dbin, "Couldn't set %s to PAUSED",
918 GST_ELEMENT_NAME (element));
919 gst_element_set_state (element, GST_STATE_NULL);
920 gst_object_unref (sinkpad);
921 gst_bin_remove (GST_BIN (dbin), element);
934 connect_element (GstDecodeBin * dbin, GstElement * element,
935 GstDecodeGroup * group)
939 gboolean dynamic = FALSE;
940 GList *to_connect = NULL;
942 GST_DEBUG_OBJECT (dbin, "Attempting to connect element %s [group:%p] further",
943 GST_ELEMENT_NAME (element), group);
945 /* 1. Loop over pad templates, grabbing existing pads along the way */
946 for (pads = GST_ELEMENT_GET_CLASS (element)->padtemplates; pads;
947 pads = g_list_next (pads)) {
948 GstPadTemplate *templ = GST_PAD_TEMPLATE (pads->data);
949 const gchar *templ_name;
951 /* we are only interested in source pads */
952 if (GST_PAD_TEMPLATE_DIRECTION (templ) != GST_PAD_SRC)
955 templ_name = GST_PAD_TEMPLATE_NAME_TEMPLATE (templ);
956 GST_DEBUG_OBJECT (dbin, "got a source pad template %s", templ_name);
958 /* figure out what kind of pad this is */
959 switch (GST_PAD_TEMPLATE_PRESENCE (templ)) {
962 /* get the pad that we need to autoplug */
963 GstPad *pad = gst_element_get_pad (element, templ_name);
966 GST_DEBUG_OBJECT (dbin, "got the pad for always template %s",
968 /* here is the pad, we need to autoplug it */
969 to_connect = g_list_prepend (to_connect, pad);
971 /* strange, pad is marked as always but it's not
972 * there. Fix the element */
973 GST_WARNING_OBJECT (dbin,
974 "could not get the pad for always template %s", templ_name);
978 case GST_PAD_SOMETIMES:
980 /* try to get the pad to see if it is already created or
982 GstPad *pad = gst_element_get_pad (element, templ_name);
985 GST_DEBUG_OBJECT (dbin, "got the pad for sometimes template %s",
987 /* the pad is created, we need to autoplug it */
988 to_connect = g_list_prepend (to_connect, pad);
990 GST_DEBUG_OBJECT (dbin,
991 "did not get the sometimes pad of template %s", templ_name);
992 /* we have an element that will create dynamic pads */
997 case GST_PAD_REQUEST:
998 /* ignore request pads */
999 GST_DEBUG_OBJECT (dbin, "ignoring request padtemplate %s", templ_name);
1004 /* 2. if there are more potential pads, connect to relevent signals */
1007 GST_LOG ("Adding signals to element %s in group %p",
1008 GST_ELEMENT_NAME (element), group);
1009 GROUP_MUTEX_LOCK (group);
1011 GST_LOG ("Group %p has now %d dynamic elements", group, group->nbdynamic);
1012 GROUP_MUTEX_UNLOCK (group);
1013 g_signal_connect (G_OBJECT (element), "pad-added",
1014 G_CALLBACK (pad_added_group_cb), group);
1015 g_signal_connect (G_OBJECT (element), "pad-removed",
1016 G_CALLBACK (pad_removed_group_cb), group);
1017 g_signal_connect (G_OBJECT (element), "no-more-pads",
1018 G_CALLBACK (no_more_pads_group_cb), group);
1020 /* This is a non-grouped element, the handlers are different */
1021 g_signal_connect (G_OBJECT (element), "pad-added",
1022 G_CALLBACK (pad_added_cb), dbin);
1023 g_signal_connect (G_OBJECT (element), "pad-removed",
1024 G_CALLBACK (pad_removed_cb), dbin);
1025 g_signal_connect (G_OBJECT (element), "no-more-pads",
1026 G_CALLBACK (no_more_pads_cb), dbin);
1030 /* 3. for every available pad, connect it */
1031 for (pads = to_connect; pads; pads = g_list_next (pads)) {
1032 GstPad *pad = GST_PAD_CAST (pads->data);
1035 caps = gst_pad_get_caps (pad);
1036 analyze_new_pad (dbin, element, pad, caps, group);
1038 gst_caps_unref (caps);
1040 gst_object_unref (pad);
1042 g_list_free (to_connect);
1049 * Expose the given pad on the group as a decoded pad.
1050 * If group is NULL, a GstDecodeGroup will be created and setup properly.
1053 expose_pad (GstDecodeBin * dbin, GstElement * src, GstPad * pad,
1054 GstDecodeGroup * group)
1056 gboolean newgroup = FALSE;
1059 GST_DEBUG_OBJECT (dbin, "pad %s:%s, group:%p",
1060 GST_DEBUG_PAD_NAME (pad), group);
1063 if (!(group = get_current_group (dbin))) {
1064 group = gst_decode_group_new (dbin);
1065 DECODE_BIN_LOCK (dbin);
1066 dbin->groups = g_list_append (dbin->groups, group);
1067 DECODE_BIN_UNLOCK (dbin);
1071 isdemux = is_demuxer_element (src);
1073 if (isdemux || newgroup) {
1076 GST_LOG_OBJECT (src, "is a demuxer, connecting the pad through multiqueue");
1078 if (!(mqpad = gst_decode_group_control_demuxer_pad (group, pad)))
1083 gst_decode_group_control_source_pad (group, pad);
1085 if (newgroup && !isdemux) {
1086 /* If we have discovered a raw pad and it doesn't belong to any group,
1087 * that means there wasn't any demuxer. In that case, we consider the
1088 * group as being complete. */
1089 gst_decode_group_set_complete (group);
1096 type_found (GstElement * typefind, guint probability,
1097 GstCaps * caps, GstDecodeBin * decode_bin)
1101 GST_STATE_LOCK (decode_bin);
1103 GST_DEBUG_OBJECT (decode_bin, "typefind found caps %" GST_PTR_FORMAT, caps);
1105 pad = gst_element_get_pad (typefind, "src");
1107 analyze_new_pad (decode_bin, typefind, pad, caps, NULL);
1109 gst_object_unref (pad);
1111 GST_STATE_UNLOCK (decode_bin);
1116 pad_added_group_cb (GstElement * element, GstPad * pad, GstDecodeGroup * group)
1119 gboolean expose = FALSE;
1121 GST_LOG_OBJECT (pad, "pad added, group:%p", group);
1123 caps = gst_pad_get_caps (pad);
1124 analyze_new_pad (group->dbin, element, pad, caps, group);
1126 gst_caps_unref (caps);
1128 GROUP_MUTEX_LOCK (group);
1130 GST_LOG ("Group %p has now %d dynamic objects", group, group->nbdynamic);
1131 if (group->nbdynamic == 0)
1133 GROUP_MUTEX_UNLOCK (group);
1136 ("That was the last dynamic object, now attempting to expose the group");
1137 DECODE_BIN_LOCK (group->dbin);
1138 gst_decode_group_expose (group);
1139 DECODE_BIN_UNLOCK (group->dbin);
1144 pad_removed_group_cb (GstElement * element, GstPad * pad,
1145 GstDecodeGroup * group)
1147 GST_LOG_OBJECT (pad, "pad removed, group:%p", group);
1149 /* In fact, we don't have to do anything here, the active group will be
1150 * removed when the group's multiqueue is drained */
1154 no_more_pads_group_cb (GstElement * element, GstDecodeGroup * group)
1156 GST_LOG_OBJECT (element, "no more pads, setting group %p to complete", group);
1158 /* FIXME : FILLME */
1159 gst_decode_group_set_complete (group);
1163 pad_added_cb (GstElement * element, GstPad * pad, GstDecodeBin * dbin)
1167 GST_LOG_OBJECT (pad, "Pad added to non-grouped element");
1169 caps = gst_pad_get_caps (pad);
1170 analyze_new_pad (dbin, element, pad, caps, NULL);
1172 gst_caps_unref (caps);
1176 pad_removed_cb (GstElement * element, GstPad * pad, GstDecodeBin * dbin)
1178 GST_LOG_OBJECT (pad, "Pad removed from non-grouped element");
1182 no_more_pads_cb (GstElement * element, GstDecodeBin * dbin)
1184 GstDecodeGroup *group;
1186 GST_LOG_OBJECT (element, "No more pads, setting current group to complete");
1188 /* Find the non-complete group, there should only be one */
1189 if (!(group = get_current_group (dbin)))
1192 gst_decode_group_set_complete (group);
1197 GST_WARNING_OBJECT (dbin, "We couldn't find a non-completed group !!");
1202 /* this function runs through the element factories and returns a list
1203 * of all elements that are able to sink the given caps
1206 find_compatibles (GstDecodeBin * decode_bin, const GstCaps * caps)
1209 GList *to_try = NULL;
1211 /* loop over all the factories */
1212 for (factories = decode_bin->factories; factories;
1213 factories = g_list_next (factories)) {
1214 GstElementFactory *factory = GST_ELEMENT_FACTORY (factories->data);
1215 const GList *templates;
1218 /* get the templates from the element factory */
1219 templates = gst_element_factory_get_static_pad_templates (factory);
1220 for (walk = (GList *) templates; walk; walk = g_list_next (walk)) {
1221 GstStaticPadTemplate *templ = walk->data;
1223 /* we only care about the sink templates */
1224 if (templ->direction == GST_PAD_SINK) {
1228 /* try to intersect the caps with the caps of the template */
1229 tmpl_caps = gst_static_caps_get (&templ->static_caps);
1231 intersect = gst_caps_intersect (caps, tmpl_caps);
1232 gst_caps_unref (tmpl_caps);
1234 /* check if the intersection is empty */
1235 if (!gst_caps_is_empty (intersect)) {
1236 /* non empty intersection, we can use this element */
1237 to_try = g_list_prepend (to_try, factory);
1238 gst_caps_unref (intersect);
1241 gst_caps_unref (intersect);
1245 to_try = g_list_reverse (to_try);
1250 /* Decide whether an element is a demuxer based on the
1251 * klass and number/type of src pad templates it has */
1253 is_demuxer_element (GstElement * srcelement)
1255 GstElementFactory *srcfactory;
1256 GstElementClass *elemclass;
1257 GList *templates, *walk;
1259 gint potential_src_pads = 0;
1261 srcfactory = gst_element_get_factory (srcelement);
1262 klass = gst_element_factory_get_klass (srcfactory);
1264 /* Can't be a demuxer unless it has Demux in the klass name */
1265 if (!strstr (klass, "Demux"))
1268 /* Walk the src pad templates and count how many the element
1270 elemclass = GST_ELEMENT_GET_CLASS (srcelement);
1272 walk = templates = gst_element_class_get_pad_template_list (elemclass);
1273 while (walk != NULL) {
1274 GstPadTemplate *templ;
1276 templ = (GstPadTemplate *) walk->data;
1277 if (GST_PAD_TEMPLATE_DIRECTION (templ) == GST_PAD_SRC) {
1278 switch (GST_PAD_TEMPLATE_PRESENCE (templ)) {
1279 case GST_PAD_ALWAYS:
1280 case GST_PAD_SOMETIMES:
1281 if (strstr (GST_PAD_TEMPLATE_NAME_TEMPLATE (templ), "%"))
1282 potential_src_pads += 2; /* Might make multiple pads */
1284 potential_src_pads += 1;
1286 case GST_PAD_REQUEST:
1287 potential_src_pads += 2;
1291 walk = g_list_next (walk);
1294 if (potential_src_pads < 2)
1300 /* Returns TRUE if the caps are raw, or if they are compatible with the caps
1301 * specified in the 'caps' property
1303 * The decodebin_lock should be taken !
1306 are_raw_caps (GstDecodeBin * dbin, GstCaps * caps)
1308 GstCaps *intersection;
1311 GST_LOG_OBJECT (dbin, "Checking with caps %" GST_PTR_FORMAT, caps);
1313 intersection = gst_caps_intersect (dbin->caps, caps);
1315 res = (!(gst_caps_is_empty (intersection)));
1317 gst_caps_unref (intersection);
1319 GST_LOG_OBJECT (dbin, "Caps are %sfinal caps", res ? "" : "not ");
1326 * GstDecodeGroup functions
1330 multi_queue_overrun_cb (GstElement * queue, GstDecodeGroup * group)
1332 GST_LOG_OBJECT (group->dbin, "multiqueue is full");
1334 /* if we haven't exposed the group, do it */
1335 DECODE_BIN_LOCK (group->dbin);
1336 gst_decode_group_expose (group);
1337 DECODE_BIN_UNLOCK (group->dbin);
1341 multi_queue_underrun_cb (GstElement * queue, GstDecodeGroup * group)
1343 GstDecodeBin *dbin = group->dbin;
1345 GST_LOG_OBJECT (dbin, "multiqueue is empty for group %p", group);
1347 /* Check if we need to activate another group */
1348 DECODE_BIN_LOCK (dbin);
1349 if ((group == dbin->activegroup) && dbin->groups) {
1350 GST_DEBUG_OBJECT (dbin, "Switching to new group");
1351 /* unexpose current active */
1352 gst_decode_group_hide (group);
1354 /* expose first group of groups */
1355 gst_decode_group_expose ((GstDecodeGroup *) dbin->groups->data);
1357 DECODE_BIN_UNLOCK (dbin);
1360 /* gst_decode_group_new
1362 * Creates a new GstDecodeGroup. It is up to the caller to add it to the list
1365 static GstDecodeGroup *
1366 gst_decode_group_new (GstDecodeBin * dbin)
1368 GstDecodeGroup *group;
1371 GST_LOG_OBJECT (dbin, "Creating new group");
1373 if (!(mq = gst_element_factory_make ("multiqueue", NULL))) {
1374 GST_WARNING ("Couldn't create multiqueue element");
1378 g_object_set (G_OBJECT (mq),
1379 "max-size-bytes", 2 * 1024 * 1024,
1380 "max-size-time", 5 * GST_SECOND, "max-size-buffers", 0, NULL);
1382 group = g_new0 (GstDecodeGroup, 1);
1383 group->lock = g_mutex_new ();
1385 group->multiqueue = mq;
1386 group->exposed = FALSE;
1387 group->drained = FALSE;
1388 group->blocked = FALSE;
1389 group->complete = FALSE;
1390 group->endpads = NULL;
1392 group->overrunsig = g_signal_connect (G_OBJECT (mq), "overrun",
1393 G_CALLBACK (multi_queue_overrun_cb), group);
1394 group->underrunsig = g_signal_connect (G_OBJECT (mq), "underrun",
1395 G_CALLBACK (multi_queue_underrun_cb), group);
1397 gst_bin_add (GST_BIN (dbin), group->multiqueue);
1398 gst_element_set_state (group->multiqueue, GST_STATE_PAUSED);
1400 GST_LOG_OBJECT (dbin, "Returning new group %p", group);
1405 /** get_current_group:
1407 * Returns the current non-completed group.
1409 * Returns NULL if no groups are available, or all groups are completed.
1411 static GstDecodeGroup *
1412 get_current_group (GstDecodeBin * dbin)
1415 GstDecodeGroup *group = NULL;
1417 DECODE_BIN_LOCK (dbin);
1418 for (tmp = dbin->groups; tmp; tmp = g_list_next (tmp)) {
1419 GstDecodeGroup *this = (GstDecodeGroup *) tmp->data;
1421 GST_LOG_OBJECT (dbin, "group %p, complete:%d", this, this->complete);
1423 if (!this->complete) {
1428 DECODE_BIN_UNLOCK (dbin);
1430 GST_LOG_OBJECT (dbin, "Returning group %p", group);
1436 group_demuxer_event_probe (GstPad * pad, GstEvent * event,
1437 GstDecodeGroup * group)
1439 if (GST_EVENT_TYPE (event) == GST_EVENT_EOS) {
1440 GST_DEBUG_OBJECT (group->dbin,
1441 "Got EOS on group input pads, exposing group if it wasn't before");
1442 DECODE_BIN_LOCK (group->dbin);
1443 gst_decode_group_expose (group);
1444 DECODE_BIN_UNLOCK (group->dbin);
1449 /* gst_decode_group_control_demuxer_pad
1451 * Adds a new demuxer srcpad to the given group.
1453 * Returns the srcpad of the multiqueue corresponding the given pad.
1454 * Returns NULL if there was an error.
1457 gst_decode_group_control_demuxer_pad (GstDecodeGroup * group, GstPad * pad)
1459 GstPad *srcpad, *sinkpad;
1460 gchar *nb, *sinkname, *srcname;
1462 GST_LOG ("group:%p pad %s:%s", group, GST_DEBUG_PAD_NAME (pad));
1466 if (!(sinkpad = gst_element_get_pad (group->multiqueue, "sink%d"))) {
1467 GST_ERROR ("Couldn't get sinkpad from multiqueue");
1471 if ((gst_pad_link (pad, sinkpad) != GST_PAD_LINK_OK)) {
1472 GST_ERROR ("Couldn't link demuxer and multiqueue");
1476 sinkname = gst_pad_get_name (sinkpad);
1478 srcname = g_strdup_printf ("src%s", nb);
1481 GROUP_MUTEX_LOCK (group);
1483 if (!(srcpad = gst_element_get_pad (group->multiqueue, srcname))) {
1484 GST_ERROR ("Couldn't get srcpad %s from multiqueue", srcname);
1488 /* connect event handler on pad to intercept EOS events */
1489 gst_pad_add_event_probe (pad, G_CALLBACK (group_demuxer_event_probe), group);
1493 GROUP_MUTEX_UNLOCK (group);
1496 gst_object_unref (sinkpad);
1501 gst_decode_group_control_source_pad (GstDecodeGroup * group, GstPad * pad)
1505 g_return_val_if_fail (group != NULL, FALSE);
1507 GST_LOG ("group:%p , pad %s:%s", group, GST_DEBUG_PAD_NAME (pad));
1509 /* FIXME : check if pad is already controlled */
1511 GROUP_MUTEX_LOCK (group);
1513 /* Create GstDecodePad for the pad */
1514 dpad = gst_decode_pad_new (group, pad, TRUE);
1516 group->endpads = g_list_append (group->endpads, dpad);
1518 GROUP_MUTEX_UNLOCK (group);
1523 /* gst_decode_group_check_if_blocked:
1525 * Call this when one of the pads blocked status has changed.
1526 * If the group is complete and blocked, the group will be marked as blocked
1527 * and will ghost/expose all pads on decodebin if the group is the current one.
1529 * Call with the group lock taken ! MT safe
1532 gst_decode_group_check_if_blocked (GstDecodeGroup * group)
1535 gboolean blocked = TRUE;
1537 GST_LOG ("group : %p , ->complete:%d , ->nbdynamic:%d",
1538 group, group->complete, group->nbdynamic);
1540 /* 1. don't do anything if group is not complete */
1541 if (!group->complete || group->nbdynamic) {
1542 GST_DEBUG_OBJECT (group->dbin, "Group isn't complete yet");
1546 for (tmp = group->endpads; tmp; tmp = g_list_next (tmp)) {
1547 GstDecodePad *dpad = (GstDecodePad *) tmp->data;
1549 if (!dpad->blocked) {
1555 /* 2. Update status of group */
1556 group->blocked = blocked;
1557 GST_LOG ("group is blocked:%d", blocked);
1559 /* 3. don't do anything if not blocked completely */
1563 /* 4. if we're the current group, expose pads */
1564 DECODE_BIN_LOCK (group->dbin);
1565 if (!gst_decode_group_expose (group))
1566 GST_WARNING_OBJECT (group->dbin, "Couldn't expose group");
1567 DECODE_BIN_UNLOCK (group->dbin);
1571 gst_decode_group_check_if_drained (GstDecodeGroup * group)
1574 GstDecodeBin *dbin = group->dbin;
1575 gboolean drained = TRUE;
1577 GST_LOG ("group : %p", group);
1579 for (tmp = group->endpads; tmp; tmp = g_list_next (tmp)) {
1580 GstDecodePad *dpad = (GstDecodePad *) tmp->data;
1582 GST_LOG ("testing dpad %p", dpad);
1584 if (!dpad->drained) {
1590 group->drained = drained;
1591 GST_LOG ("group is drained");
1596 DECODE_BIN_LOCK (dbin);
1597 if ((group == dbin->activegroup) && dbin->groups) {
1598 GST_DEBUG_OBJECT (dbin, "Switching to new group");
1600 gst_decode_group_hide (group);
1602 gst_decode_group_expose ((GstDecodeGroup *) dbin->groups->data);
1604 DECODE_BIN_UNLOCK (dbin);
1608 * GCompareFunc to use with lists of GstPad.
1609 * Sorts pads by mime type.
1610 * First video (raw, then non-raw), then audio (raw, then non-raw),
1613 * Return: negative if a<b, 0 if a==b, positive if a>b
1617 sort_end_pads (GstDecodePad * da, GstDecodePad * db)
1621 GstCaps *capsa, *capsb;
1622 GstStructure *sa, *sb;
1623 const gchar *namea, *nameb;
1628 capsa = gst_pad_get_caps (a);
1629 capsb = gst_pad_get_caps (b);
1631 sa = gst_caps_get_structure ((const GstCaps *) capsa, 0);
1632 sb = gst_caps_get_structure ((const GstCaps *) capsb, 0);
1634 namea = gst_structure_get_name (sa);
1635 nameb = gst_structure_get_name (sb);
1637 if (g_strrstr (namea, "video/x-raw-"))
1639 else if (g_strrstr (namea, "video/"))
1641 else if (g_strrstr (namea, "audio/x-raw"))
1643 else if (g_strrstr (namea, "audio/"))
1648 if (g_strrstr (nameb, "video/x-raw-"))
1650 else if (g_strrstr (nameb, "video/"))
1652 else if (g_strrstr (nameb, "audio/x-raw"))
1654 else if (g_strrstr (nameb, "audio/"))
1659 gst_caps_unref (capsa);
1660 gst_caps_unref (capsb);
1665 /* gst_decode_group_expose:
1667 * Expose this group's pads.
1669 * Not MT safe, please take the group lock
1673 gst_decode_group_expose (GstDecodeGroup * group)
1678 if (group->dbin->activegroup) {
1679 GST_DEBUG_OBJECT (group->dbin, "A group is already active and exposed");
1683 if (group->dbin->activegroup == group) {
1684 GST_WARNING ("Group %p is already exposed", group);
1688 if (!group->dbin->groups
1689 || (group != (GstDecodeGroup *) group->dbin->groups->data)) {
1690 GST_WARNING ("Group %p is not the first group to expose", group);
1694 if (group->nbdynamic) {
1695 GST_WARNING ("Group %p still has %d dynamic objects, not exposing yet",
1696 group, group->nbdynamic);
1700 GST_LOG ("Exposing group %p", group);
1702 /* re-order pads : video, then audio, then others */
1703 group->endpads = g_list_sort (group->endpads, (GCompareFunc) sort_end_pads);
1707 for (tmp = group->endpads; tmp; tmp = next) {
1708 GstDecodePad *dpad = (GstDecodePad *) tmp->data;
1712 next = g_list_next (tmp);
1715 padname = g_strdup_printf ("src%d", group->dbin->nbpads);
1716 group->dbin->nbpads++;
1718 GST_LOG_OBJECT (group->dbin, "About to expose pad %s:%s",
1719 GST_DEBUG_PAD_NAME (dpad->pad));
1721 ghost = gst_ghost_pad_new (padname, dpad->pad);
1722 gst_pad_set_active (ghost, TRUE);
1723 gst_element_add_pad (GST_ELEMENT (group->dbin), ghost);
1724 group->ghosts = g_list_append (group->ghosts, ghost);
1728 /* 2. emit signal */
1729 GST_DEBUG_OBJECT (group->dbin, "emitting new-decoded-pad");
1730 g_signal_emit (G_OBJECT (group->dbin),
1731 gst_decode_bin_signals[SIGNAL_NEW_DECODED_PAD], 0, ghost,
1733 GST_DEBUG_OBJECT (group->dbin, "emitted new-decoded-pad");
1735 /* 3. Unblock internal pad */
1736 GST_DEBUG_OBJECT (dpad->pad, "unblocking");
1737 gst_pad_set_blocked_async (dpad->pad, FALSE,
1738 (GstPadBlockCallback) source_pad_blocked_cb, dpad);
1739 GST_DEBUG_OBJECT (dpad->pad, "unblocked");
1743 group->dbin->activegroup = group;
1745 /* pop off the first group */
1746 group->dbin->groups =
1747 g_list_delete_link (group->dbin->groups, group->dbin->groups);
1749 remove_fakesink (group->dbin);
1751 group->exposed = TRUE;
1753 GST_LOG_OBJECT (group->dbin, "signalling no-more-pads");
1754 gst_element_no_more_pads (GST_ELEMENT (group->dbin));
1756 GST_LOG_OBJECT (group->dbin, "Group %p exposed", group);
1761 gst_decode_group_hide (GstDecodeGroup * group)
1765 GST_LOG ("Hiding group %p", group);
1767 if (group != group->dbin->activegroup) {
1768 GST_WARNING ("This group is not the active one, aborting");
1772 GROUP_MUTEX_LOCK (group);
1774 /* Remove ghost pads */
1775 for (tmp = group->ghosts; tmp; tmp = g_list_next (tmp))
1776 gst_element_remove_pad (GST_ELEMENT (group->dbin), (GstPad *) tmp->data);
1778 g_list_free (group->ghosts);
1779 group->ghosts = NULL;
1781 group->exposed = FALSE;
1783 GROUP_MUTEX_UNLOCK (group);
1785 group->dbin->activegroup = NULL;
1786 group->dbin->oldgroups = g_list_append (group->dbin->oldgroups, group);
1790 deactivate_free_recursive (GstDecodeGroup * group, GstElement * element)
1793 GstIteratorResult res;
1796 GST_LOG ("element:%s", GST_ELEMENT_NAME (element));
1798 /* call on downstream elements */
1799 it = gst_element_iterate_src_pads (element);
1804 res = gst_iterator_next (it, &point);
1806 case GST_ITERATOR_DONE:
1808 case GST_ITERATOR_RESYNC:
1809 gst_iterator_resync (it);
1811 case GST_ITERATOR_ERROR:
1813 GST_WARNING ("Had an error while iterating source pads of element: %s",
1814 GST_ELEMENT_NAME (element));
1817 case GST_ITERATOR_OK:
1819 GstPad *pad = GST_PAD (point);
1820 GstPad *peerpad = NULL;
1822 if ((peerpad = gst_pad_get_peer (pad))) {
1823 GstObject *parent = gst_pad_get_parent (peerpad);
1825 if (parent && GST_IS_ELEMENT (parent))
1826 deactivate_free_recursive (group, GST_ELEMENT (parent));
1828 gst_object_unref (parent);
1838 gst_element_set_state (element, GST_STATE_NULL);
1839 gst_bin_remove (GST_BIN (group->dbin), element);
1842 gst_iterator_free (it);
1848 gst_decode_group_free (GstDecodeGroup * group)
1852 GST_LOG ("group %p", group);
1854 GROUP_MUTEX_LOCK (group);
1855 /* Clear all GstDecodePad */
1856 for (tmp = group->endpads; tmp; tmp = g_list_next (tmp)) {
1857 GstDecodePad *dpad = (GstDecodePad *) tmp->data;
1861 g_list_free (group->endpads);
1862 group->endpads = NULL;
1864 /* disconnect signal handlers on multiqueue */
1865 g_signal_handler_disconnect (group->multiqueue, group->underrunsig);
1866 g_signal_handler_disconnect (group->multiqueue, group->overrunsig);
1868 /* remove all elements */
1869 deactivate_free_recursive (group, group->multiqueue);
1871 GROUP_MUTEX_UNLOCK (group);
1873 g_mutex_free (group->lock);
1877 /* gst_decode_group_set_complete:
1879 * Mark the group as complete. This means no more streams will be controlled
1880 * through this group.
1885 gst_decode_group_set_complete (GstDecodeGroup * group)
1887 GST_LOG_OBJECT (group->dbin, "Setting group %p to COMPLETE", group);
1889 GROUP_MUTEX_LOCK (group);
1890 group->complete = TRUE;
1891 gst_decode_group_check_if_blocked (group);
1892 GROUP_MUTEX_UNLOCK (group);
1897 /*************************
1898 * GstDecodePad functions
1899 *************************/
1902 source_pad_blocked_cb (GstPad * pad, gboolean blocked, GstDecodePad * dpad)
1904 GST_LOG_OBJECT (pad, "blocked:%d , dpad:%p, dpad->group:%p",
1905 blocked, dpad, dpad->group);
1907 /* Update this GstDecodePad status */
1908 dpad->blocked = blocked;
1911 GROUP_MUTEX_LOCK (dpad->group);
1912 gst_decode_group_check_if_blocked (dpad->group);
1913 GROUP_MUTEX_UNLOCK (dpad->group);
1918 source_pad_event_probe (GstPad * pad, GstEvent * event, GstDecodePad * dpad)
1920 GST_LOG_OBJECT (pad, "%s dpad:%p", GST_EVENT_TYPE_NAME (event), dpad);
1922 if (GST_EVENT_TYPE (event) == GST_EVENT_EOS) {
1923 /* Set our pad as drained */
1924 dpad->drained = TRUE;
1926 /* Check if all pads are drained */
1927 gst_decode_group_check_if_drained (dpad->group);
1933 /*gst_decode_pad_new:
1935 * Creates a new GstDecodePad for the given pad.
1936 * If block is TRUE, Sets the pad blocking asynchronously
1939 static GstDecodePad *
1940 gst_decode_pad_new (GstDecodeGroup * group, GstPad * pad, gboolean block)
1944 dpad = g_new0 (GstDecodePad, 1);
1946 dpad->group = group;
1947 dpad->blocked = FALSE;
1948 dpad->drained = TRUE;
1951 gst_pad_set_blocked_async (pad, TRUE,
1952 (GstPadBlockCallback) source_pad_blocked_cb, dpad);
1953 gst_pad_add_event_probe (pad, G_CALLBACK (source_pad_event_probe), dpad);
1959 * Element add/remove
1963 * add_fakesink / remove_fakesink
1965 * We use a sink so that the parent ::change_state returns GST_STATE_CHANGE_ASYNC
1966 * when that sink is present (since it's not connected to anything it will
1967 * always return GST_STATE_CHANGE_ASYNC).
1969 * But this is an ugly way of achieving this goal.
1970 * Ideally, we shouldn't use a sink and just return GST_STATE_CHANGE_ASYNC in
1971 * our ::change_state if we have not exposed the active group.
1972 * We also need to override ::get_state to fake the asynchronous behaviour.
1973 * Once the active group is exposed, we would then post a
1974 * GST_MESSAGE_STATE_DIRTY and return GST_STATE_CHANGE_SUCCESS (which will call
1979 add_fakesink (GstDecodeBin * decode_bin)
1981 GST_DEBUG_OBJECT (decode_bin, "Adding the fakesink");
1983 if (decode_bin->fakesink)
1986 decode_bin->fakesink =
1987 gst_element_factory_make ("fakesink", "async-fakesink");
1988 if (!decode_bin->fakesink)
1991 /* hacky, remove sink flag, we don't want our decodebin to become a sink
1992 * just because we add a fakesink element to make us ASYNC */
1993 GST_OBJECT_FLAG_UNSET (decode_bin->fakesink, GST_ELEMENT_IS_SINK);
1995 if (!gst_bin_add (GST_BIN (decode_bin), decode_bin->fakesink))
2003 g_warning ("can't find fakesink element, decodebin will not work");
2008 g_warning ("Could not add fakesink to decodebin, decodebin will not work");
2009 gst_object_unref (decode_bin->fakesink);
2010 decode_bin->fakesink = NULL;
2016 remove_fakesink (GstDecodeBin * decode_bin)
2018 if (decode_bin->fakesink == NULL)
2021 GST_DEBUG_OBJECT (decode_bin, "Removing the fakesink");
2023 gst_element_set_state (decode_bin->fakesink, GST_STATE_NULL);
2024 gst_bin_remove (GST_BIN (decode_bin), decode_bin->fakesink);
2025 decode_bin->fakesink = NULL;
2027 gst_element_post_message (GST_ELEMENT_CAST (decode_bin),
2028 gst_message_new_state_dirty (GST_OBJECT_CAST (decode_bin)));
2032 * convenience functions
2037 * Returns the first sink pad of the given element, or NULL if it doesn't have
2042 find_sink_pad (GstElement * element)
2048 it = gst_element_iterate_sink_pads (element);
2050 if ((gst_iterator_next (it, &point)) == GST_ITERATOR_OK)
2051 pad = (GstPad *) point;
2053 gst_iterator_free (it);
2058 static GstStateChangeReturn
2059 gst_decode_bin_change_state (GstElement * element, GstStateChange transition)
2061 GstStateChangeReturn ret;
2062 GstDecodeBin *dbin = GST_DECODE_BIN (element);
2064 switch (transition) {
2065 case GST_STATE_CHANGE_NULL_TO_READY:
2066 if (dbin->typefind == NULL)
2067 goto missing_typefind;
2069 case GST_STATE_CHANGE_READY_TO_PAUSED:{
2070 if (!add_fakesink (dbin))
2071 goto missing_fakesink;
2078 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
2080 /* FIXME : put some cleanup functions here.. if needed */
2087 GST_ELEMENT_ERROR (dbin, CORE, MISSING_PLUGIN, (NULL), ("no typefind!"));
2088 return GST_STATE_CHANGE_FAILURE;
2092 GST_ELEMENT_ERROR (dbin, CORE, MISSING_PLUGIN, (NULL), ("no fakesink!"));
2093 return GST_STATE_CHANGE_FAILURE;
2098 plugin_init (GstPlugin * plugin)
2100 GST_DEBUG_CATEGORY_INIT (gst_decode_bin_debug, "decodebin2", 0,
2103 return gst_element_register (plugin, "decodebin2", GST_RANK_NONE,
2104 GST_TYPE_DECODE_BIN);
2107 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
2110 "decoder bin newer version", plugin_init, VERSION, GST_LICENSE,
2111 GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)