[svn r849] Now nuvdemux package replaces gstreamer0.10-plugins-bad (see debian/control file)
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 */
454 rank = gst_plugin_feature_get_rank (feature);
455 if (rank < GST_RANK_MARGINAL)
462 /* function used to sort element features */
464 compare_ranks (GstPluginFeature * f1, GstPluginFeature * f2)
467 const gchar *rname1, *rname2;
469 diff = gst_plugin_feature_get_rank (f2) - gst_plugin_feature_get_rank (f1);
473 rname1 = gst_plugin_feature_get_name (f1);
474 rname2 = gst_plugin_feature_get_name (f2);
476 diff = strcmp (rname2, rname1);
482 print_feature (GstPluginFeature * feature)
486 rname = gst_plugin_feature_get_name (feature);
488 GST_DEBUG ("%s", rname);
492 gst_decode_bin_init (GstDecodeBin * decode_bin)
496 /* first filter out the interesting element factories */
497 factories = gst_default_registry_feature_filter (
498 (GstPluginFeatureFilter) gst_decode_bin_factory_filter,
501 /* sort them according to their ranks */
502 decode_bin->factories = g_list_sort (factories, (GCompareFunc) compare_ranks);
503 /* do some debugging */
504 g_list_foreach (decode_bin->factories, (GFunc) print_feature, NULL);
507 /* we create the typefind element only once */
508 decode_bin->typefind = gst_element_factory_make ("typefind", "typefind");
509 if (!decode_bin->typefind) {
510 g_warning ("can't find typefind element, decodebin will not work");
515 /* add the typefind element */
516 if (!gst_bin_add (GST_BIN (decode_bin), decode_bin->typefind)) {
517 g_warning ("Could not add typefind element, decodebin will not work");
518 gst_object_unref (decode_bin->typefind);
519 decode_bin->typefind = NULL;
522 /* get the sinkpad */
523 pad = gst_element_get_pad (decode_bin->typefind, "sink");
525 /* ghost the sink pad to ourself */
526 gpad = gst_ghost_pad_new ("sink", pad);
527 gst_pad_set_active (gpad, TRUE);
528 gst_element_add_pad (GST_ELEMENT (decode_bin), gpad);
530 gst_object_unref (pad);
532 /* connect a signal to find out when the typefind element found
534 g_signal_connect (G_OBJECT (decode_bin->typefind), "have_type",
535 G_CALLBACK (type_found), decode_bin);
538 decode_bin->lock = g_mutex_new ();
539 decode_bin->activegroup = NULL;
540 decode_bin->groups = NULL;
543 gst_caps_from_string ("video/x-raw-yuv;video/x-raw-rgb;video/x-raw-gray;"
544 "audio/x-raw-int;audio/x-raw-float;" "text/plain;text/x-pango-markup");
546 add_fakesink (decode_bin);
552 gst_decode_bin_dispose (GObject * object)
554 GstDecodeBin *decode_bin;
557 decode_bin = GST_DECODE_BIN (object);
559 if (decode_bin->factories)
560 gst_plugin_feature_list_free (decode_bin->factories);
561 decode_bin->factories = NULL;
563 if (decode_bin->activegroup) {
564 gst_decode_group_free (decode_bin->activegroup);
565 decode_bin->activegroup = NULL;
569 for (tmp = decode_bin->groups; tmp; tmp = g_list_next (tmp)) {
570 GstDecodeGroup *group = (GstDecodeGroup *) tmp->data;
572 gst_decode_group_free (group);
574 g_list_free (decode_bin->groups);
575 decode_bin->groups = NULL;
577 for (tmp = decode_bin->oldgroups; tmp; tmp = g_list_next (tmp)) {
578 GstDecodeGroup *group = (GstDecodeGroup *) tmp->data;
580 gst_decode_group_free (group);
582 g_list_free (decode_bin->oldgroups);
583 decode_bin->oldgroups = NULL;
585 if (decode_bin->caps)
586 gst_caps_unref (decode_bin->caps);
587 decode_bin->caps = NULL;
588 remove_fakesink (decode_bin);
590 G_OBJECT_CLASS (parent_class)->dispose (object);
594 gst_decode_bin_finalize (GObject * object)
596 GstDecodeBin *decode_bin;
598 decode_bin = GST_DECODE_BIN (object);
600 if (decode_bin->lock) {
601 g_mutex_free (decode_bin->lock);
602 decode_bin->lock = NULL;
605 G_OBJECT_CLASS (parent_class)->finalize (object);
609 gst_decode_bin_set_property (GObject * object, guint prop_id,
610 const GValue * value, GParamSpec * pspec)
614 dbin = GST_DECODE_BIN (object);
618 gst_decode_bin_set_caps (dbin, (GstCaps *) g_value_dup_boxed (value));
621 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
627 gst_decode_bin_get_property (GObject * object, guint prop_id,
628 GValue * value, GParamSpec * pspec)
632 dbin = GST_DECODE_BIN (object);
635 g_value_take_boxed (value, gst_decode_bin_get_caps (dbin));
639 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
646 * Changes the caps on which decodebin will stop decoding.
647 * Will unref the previously set one. The refcount of the given caps will be
655 gst_decode_bin_set_caps (GstDecodeBin * dbin, GstCaps * caps)
657 GST_DEBUG_OBJECT (dbin, "Setting new caps: %" GST_PTR_FORMAT, caps);
659 DECODE_BIN_LOCK (dbin);
661 gst_caps_unref (dbin->caps);
663 DECODE_BIN_UNLOCK (dbin);
667 * Returns the currently configured caps on which decodebin will stop decoding.
668 * The returned caps (if not NULL), will have its refcount incremented.
674 gst_decode_bin_get_caps (GstDecodeBin * dbin)
678 GST_DEBUG_OBJECT (dbin, "Getting currently set caps");
680 DECODE_BIN_LOCK (dbin);
684 DECODE_BIN_UNLOCK (dbin);
690 * Default autoplug signal handlers
694 gst_decode_bin_autoplug_continue (GstElement * element, GstCaps * caps)
700 gst_decode_bin_autoplug_sort (GstElement * element, GstCaps * caps,
712 static gboolean are_raw_caps (GstDecodeBin * dbin, GstCaps * caps);
713 static gboolean is_demuxer_element (GstElement * srcelement);
714 static GList *find_compatibles (GstDecodeBin * decode_bin,
715 const GstCaps * caps);
717 static gboolean connect_pad (GstDecodeBin * dbin, GstElement * src,
718 GstPad * pad, GList * factories, GstDecodeGroup * group);
719 static gboolean connect_element (GstDecodeBin * dbin, GstElement * element,
720 GstDecodeGroup * group);
721 static void expose_pad (GstDecodeBin * dbin, GstElement * src, GstPad * pad,
722 GstDecodeGroup * group);
724 static void pad_added_group_cb (GstElement * element, GstPad * pad,
725 GstDecodeGroup * group);
726 static void pad_removed_group_cb (GstElement * element, GstPad * pad,
727 GstDecodeGroup * group);
728 static void no_more_pads_group_cb (GstElement * element,
729 GstDecodeGroup * group);
730 static void pad_added_cb (GstElement * element, GstPad * pad,
731 GstDecodeBin * dbin);
732 static void pad_removed_cb (GstElement * element, GstPad * pad,
733 GstDecodeBin * dbin);
734 static void no_more_pads_cb (GstElement * element, GstDecodeBin * dbin);
736 static GstDecodeGroup *get_current_group (GstDecodeBin * dbin);
739 analyze_new_pad (GstDecodeBin * dbin, GstElement * src, GstPad * pad,
740 GstCaps * caps, GstDecodeGroup * group)
742 gboolean apcontinue = TRUE;
743 GList *factories = NULL;
744 gboolean apsort = TRUE;
746 GST_DEBUG_OBJECT (dbin, "Pad %s:%s caps:%" GST_PTR_FORMAT,
747 GST_DEBUG_PAD_NAME (pad), caps);
749 if ((caps == NULL) || gst_caps_is_empty (caps))
752 if (gst_caps_is_any (caps))
755 /* 1. Emit 'autoplug-continue' */
756 g_signal_emit (G_OBJECT (dbin),
757 gst_decode_bin_signals[SIGNAL_AUTOPLUG_CONTINUE], 0, caps, &apcontinue);
759 /* 1.a if autoplug-continue is FALSE or caps is a raw format, goto pad_is_final */
760 if ((!apcontinue) || are_raw_caps (dbin, caps))
763 /* 1.b else if there's no compatible factory or 'autoplug-sort' returned FALSE, goto pad_not_used */
764 if ((factories = find_compatibles (dbin, caps))) {
765 /* emit autoplug-sort */
766 g_signal_emit (G_OBJECT (dbin),
767 gst_decode_bin_signals[SIGNAL_AUTOPLUG_SORT],
768 0, caps, &factories, &apsort);
770 g_list_free (factories);
771 /* User doesn't want that pad */
775 /* no compatible factories */
779 /* 1.c else goto pad_is_valid */
780 GST_LOG_OBJECT (pad, "Let's continue discovery on this pad");
782 connect_pad (dbin, src, pad, factories, group);
783 g_list_free (factories);
789 GST_LOG_OBJECT (dbin, "Pad is final. autoplug-continue:%d", apcontinue);
790 expose_pad (dbin, src, pad, group);
796 GST_LOG_OBJECT (pad, "User doesn't want this pad, stopping discovery");
802 GST_LOG_OBJECT (pad, "Unknown type, firing signal");
803 g_signal_emit (G_OBJECT (dbin),
804 gst_decode_bin_signals[SIGNAL_UNKNOWN_TYPE], 0, pad, caps);
806 /* Check if there are no pending groups, if so, remove fakesink */
807 if (dbin->groups == NULL)
808 remove_fakesink (dbin);
815 GST_WARNING_OBJECT (pad,
816 "pad has ANY caps, not able to autoplug to anything");
817 /* FIXME : connect to caps notification */
825 * Try to connect the given pad to an element created from one of the factories,
828 * Returns TRUE if an element was properly created and linked
832 connect_pad (GstDecodeBin * dbin, GstElement * src, GstPad * pad,
833 GList * factories, GstDecodeGroup * group)
835 gboolean res = FALSE;
838 g_return_val_if_fail (factories != NULL, FALSE);
839 GST_DEBUG_OBJECT (dbin, "pad %s:%s , group:%p",
840 GST_DEBUG_PAD_NAME (pad), group);
842 /* 1. is element demuxer or parser */
843 if (is_demuxer_element (src)) {
846 GST_LOG_OBJECT (src, "is a demuxer, connecting the pad through multiqueue");
849 if (!(group = get_current_group (dbin))) {
850 group = gst_decode_group_new (dbin);
851 DECODE_BIN_LOCK (dbin);
852 dbin->groups = g_list_append (dbin->groups, group);
853 DECODE_BIN_UNLOCK (dbin);
856 if (!(mqpad = gst_decode_group_control_demuxer_pad (group, pad)))
861 /* 2. Try to create an element and link to it */
862 for (tmp = factories; tmp; tmp = g_list_next (tmp)) {
863 GstElementFactory *factory = (GstElementFactory *) tmp->data;
867 /* 2.1. Try to create an element */
868 if ((element = gst_element_factory_create (factory, NULL)) == NULL) {
869 GST_WARNING_OBJECT (dbin, "Could not create an element from %s",
870 gst_plugin_feature_get_name (GST_PLUGIN_FEATURE (factory)));
874 /* 2.3. Find its sink pad */
875 if (!(sinkpad = find_sink_pad (element))) {
876 GST_WARNING_OBJECT (dbin, "Element %s doesn't have a sink pad",
877 GST_ELEMENT_NAME (element));
878 gst_object_unref (element);
883 if (!(gst_bin_add (GST_BIN (dbin), element))) {
884 GST_WARNING_OBJECT (dbin, "Couldn't add %s to the bin",
885 GST_ELEMENT_NAME (element));
886 gst_object_unref (sinkpad);
887 gst_object_unref (element);
891 /* ... activate it ... */
892 if ((gst_element_set_state (element,
893 GST_STATE_READY)) == GST_STATE_CHANGE_FAILURE) {
894 GST_WARNING_OBJECT (dbin, "Couldn't set %s to READY",
895 GST_ELEMENT_NAME (element));
896 gst_object_unref (sinkpad);
897 gst_bin_remove (GST_BIN (dbin), element);
901 /* 2.5 ...and try to link */
902 if ((gst_pad_link (pad, sinkpad)) != GST_PAD_LINK_OK) {
903 GST_WARNING_OBJECT (dbin, "Link failed on pad %s:%s",
904 GST_DEBUG_PAD_NAME (sinkpad));
905 gst_element_set_state (element, GST_STATE_NULL);
906 gst_object_unref (sinkpad);
907 gst_bin_remove (GST_BIN (dbin), element);
911 GST_LOG_OBJECT (dbin, "linked on pad %s:%s", GST_DEBUG_PAD_NAME (pad));
913 /* link this element further */
914 connect_element (dbin, element, group);
916 /* Bring the element to the state of the parent */
917 if ((gst_element_set_state (element,
918 GST_STATE_PAUSED)) == GST_STATE_CHANGE_FAILURE) {
919 GST_WARNING_OBJECT (dbin, "Couldn't set %s to PAUSED",
920 GST_ELEMENT_NAME (element));
921 gst_element_set_state (element, GST_STATE_NULL);
922 gst_object_unref (sinkpad);
923 gst_bin_remove (GST_BIN (dbin), element);
936 connect_element (GstDecodeBin * dbin, GstElement * element,
937 GstDecodeGroup * group)
941 gboolean dynamic = FALSE;
942 GList *to_connect = NULL;
944 GST_DEBUG_OBJECT (dbin, "Attempting to connect element %s [group:%p] further",
945 GST_ELEMENT_NAME (element), group);
947 /* 1. Loop over pad templates, grabbing existing pads along the way */
948 for (pads = GST_ELEMENT_GET_CLASS (element)->padtemplates; pads;
949 pads = g_list_next (pads)) {
950 GstPadTemplate *templ = GST_PAD_TEMPLATE (pads->data);
951 const gchar *templ_name;
953 /* we are only interested in source pads */
954 if (GST_PAD_TEMPLATE_DIRECTION (templ) != GST_PAD_SRC)
957 templ_name = GST_PAD_TEMPLATE_NAME_TEMPLATE (templ);
958 GST_DEBUG_OBJECT (dbin, "got a source pad template %s", templ_name);
960 /* figure out what kind of pad this is */
961 switch (GST_PAD_TEMPLATE_PRESENCE (templ)) {
964 /* get the pad that we need to autoplug */
965 GstPad *pad = gst_element_get_pad (element, templ_name);
968 GST_DEBUG_OBJECT (dbin, "got the pad for always template %s",
970 /* here is the pad, we need to autoplug it */
971 to_connect = g_list_prepend (to_connect, pad);
973 /* strange, pad is marked as always but it's not
974 * there. Fix the element */
975 GST_WARNING_OBJECT (dbin,
976 "could not get the pad for always template %s", templ_name);
980 case GST_PAD_SOMETIMES:
982 /* try to get the pad to see if it is already created or
984 GstPad *pad = gst_element_get_pad (element, templ_name);
987 GST_DEBUG_OBJECT (dbin, "got the pad for sometimes template %s",
989 /* the pad is created, we need to autoplug it */
990 to_connect = g_list_prepend (to_connect, pad);
992 GST_DEBUG_OBJECT (dbin,
993 "did not get the sometimes pad of template %s", templ_name);
994 /* we have an element that will create dynamic pads */
999 case GST_PAD_REQUEST:
1000 /* ignore request pads */
1001 GST_DEBUG_OBJECT (dbin, "ignoring request padtemplate %s", templ_name);
1006 /* 2. if there are more potential pads, connect to relevent signals */
1009 GST_LOG ("Adding signals to element %s in group %p",
1010 GST_ELEMENT_NAME (element), group);
1011 GROUP_MUTEX_LOCK (group);
1013 GST_LOG ("Group %p has now %d dynamic elements", group, group->nbdynamic);
1014 GROUP_MUTEX_UNLOCK (group);
1015 g_signal_connect (G_OBJECT (element), "pad-added",
1016 G_CALLBACK (pad_added_group_cb), group);
1017 g_signal_connect (G_OBJECT (element), "pad-removed",
1018 G_CALLBACK (pad_removed_group_cb), group);
1019 g_signal_connect (G_OBJECT (element), "no-more-pads",
1020 G_CALLBACK (no_more_pads_group_cb), group);
1022 /* This is a non-grouped element, the handlers are different */
1023 g_signal_connect (G_OBJECT (element), "pad-added",
1024 G_CALLBACK (pad_added_cb), dbin);
1025 g_signal_connect (G_OBJECT (element), "pad-removed",
1026 G_CALLBACK (pad_removed_cb), dbin);
1027 g_signal_connect (G_OBJECT (element), "no-more-pads",
1028 G_CALLBACK (no_more_pads_cb), dbin);
1032 /* 3. for every available pad, connect it */
1033 for (pads = to_connect; pads; pads = g_list_next (pads)) {
1034 GstPad *pad = GST_PAD_CAST (pads->data);
1037 caps = gst_pad_get_caps (pad);
1038 analyze_new_pad (dbin, element, pad, caps, group);
1040 gst_caps_unref (caps);
1042 gst_object_unref (pad);
1044 g_list_free (to_connect);
1051 * Expose the given pad on the group as a decoded pad.
1052 * If group is NULL, a GstDecodeGroup will be created and setup properly.
1055 expose_pad (GstDecodeBin * dbin, GstElement * src, GstPad * pad,
1056 GstDecodeGroup * group)
1058 gboolean newgroup = FALSE;
1061 GST_DEBUG_OBJECT (dbin, "pad %s:%s, group:%p",
1062 GST_DEBUG_PAD_NAME (pad), group);
1065 if (!(group = get_current_group (dbin))) {
1066 group = gst_decode_group_new (dbin);
1067 DECODE_BIN_LOCK (dbin);
1068 dbin->groups = g_list_append (dbin->groups, group);
1069 DECODE_BIN_UNLOCK (dbin);
1073 isdemux = is_demuxer_element (src);
1075 if (isdemux || newgroup) {
1078 GST_LOG_OBJECT (src, "is a demuxer, connecting the pad through multiqueue");
1080 if (!(mqpad = gst_decode_group_control_demuxer_pad (group, pad)))
1085 gst_decode_group_control_source_pad (group, pad);
1087 if (newgroup && !isdemux) {
1088 /* If we have discovered a raw pad and it doesn't belong to any group,
1089 * that means there wasn't any demuxer. In that case, we consider the
1090 * group as being complete. */
1091 gst_decode_group_set_complete (group);
1098 type_found (GstElement * typefind, guint probability,
1099 GstCaps * caps, GstDecodeBin * decode_bin)
1103 GST_STATE_LOCK (decode_bin);
1105 GST_DEBUG_OBJECT (decode_bin, "typefind found caps %" GST_PTR_FORMAT, caps);
1107 pad = gst_element_get_pad (typefind, "src");
1109 analyze_new_pad (decode_bin, typefind, pad, caps, NULL);
1111 gst_object_unref (pad);
1113 GST_STATE_UNLOCK (decode_bin);
1118 pad_added_group_cb (GstElement * element, GstPad * pad, GstDecodeGroup * group)
1121 gboolean expose = FALSE;
1123 GST_LOG_OBJECT (pad, "pad added, group:%p", group);
1125 caps = gst_pad_get_caps (pad);
1126 analyze_new_pad (group->dbin, element, pad, caps, group);
1128 gst_caps_unref (caps);
1130 GROUP_MUTEX_LOCK (group);
1132 GST_LOG ("Group %p has now %d dynamic objects", group, group->nbdynamic);
1133 if (group->nbdynamic == 0)
1135 GROUP_MUTEX_UNLOCK (group);
1138 ("That was the last dynamic object, now attempting to expose the group");
1139 DECODE_BIN_LOCK (group->dbin);
1140 gst_decode_group_expose (group);
1141 DECODE_BIN_UNLOCK (group->dbin);
1146 pad_removed_group_cb (GstElement * element, GstPad * pad,
1147 GstDecodeGroup * group)
1149 GST_LOG_OBJECT (pad, "pad removed, group:%p", group);
1151 /* In fact, we don't have to do anything here, the active group will be
1152 * removed when the group's multiqueue is drained */
1156 no_more_pads_group_cb (GstElement * element, GstDecodeGroup * group)
1158 GST_LOG_OBJECT (element, "no more pads, setting group %p to complete", group);
1160 /* FIXME : FILLME */
1161 gst_decode_group_set_complete (group);
1165 pad_added_cb (GstElement * element, GstPad * pad, GstDecodeBin * dbin)
1169 GST_LOG_OBJECT (pad, "Pad added to non-grouped element");
1171 caps = gst_pad_get_caps (pad);
1172 analyze_new_pad (dbin, element, pad, caps, NULL);
1174 gst_caps_unref (caps);
1178 pad_removed_cb (GstElement * element, GstPad * pad, GstDecodeBin * dbin)
1180 GST_LOG_OBJECT (pad, "Pad removed from non-grouped element");
1184 no_more_pads_cb (GstElement * element, GstDecodeBin * dbin)
1186 GstDecodeGroup *group;
1188 GST_LOG_OBJECT (element, "No more pads, setting current group to complete");
1190 /* Find the non-complete group, there should only be one */
1191 if (!(group = get_current_group (dbin)))
1194 gst_decode_group_set_complete (group);
1199 GST_WARNING_OBJECT (dbin, "We couldn't find a non-completed group !!");
1204 /* this function runs through the element factories and returns a list
1205 * of all elements that are able to sink the given caps
1208 find_compatibles (GstDecodeBin * decode_bin, const GstCaps * caps)
1211 GList *to_try = NULL;
1213 /* loop over all the factories */
1214 for (factories = decode_bin->factories; factories;
1215 factories = g_list_next (factories)) {
1216 GstElementFactory *factory = GST_ELEMENT_FACTORY (factories->data);
1217 const GList *templates;
1220 /* get the templates from the element factory */
1221 templates = gst_element_factory_get_static_pad_templates (factory);
1222 for (walk = (GList *) templates; walk; walk = g_list_next (walk)) {
1223 GstStaticPadTemplate *templ = walk->data;
1225 /* we only care about the sink templates */
1226 if (templ->direction == GST_PAD_SINK) {
1230 /* try to intersect the caps with the caps of the template */
1231 tmpl_caps = gst_static_caps_get (&templ->static_caps);
1233 intersect = gst_caps_intersect (caps, tmpl_caps);
1234 gst_caps_unref (tmpl_caps);
1236 /* check if the intersection is empty */
1237 if (!gst_caps_is_empty (intersect)) {
1238 /* non empty intersection, we can use this element */
1239 to_try = g_list_prepend (to_try, factory);
1240 gst_caps_unref (intersect);
1243 gst_caps_unref (intersect);
1247 to_try = g_list_reverse (to_try);
1252 /* Decide whether an element is a demuxer based on the
1253 * klass and number/type of src pad templates it has */
1255 is_demuxer_element (GstElement * srcelement)
1257 GstElementFactory *srcfactory;
1258 GstElementClass *elemclass;
1259 GList *templates, *walk;
1261 gint potential_src_pads = 0;
1263 srcfactory = gst_element_get_factory (srcelement);
1264 klass = gst_element_factory_get_klass (srcfactory);
1266 /* Can't be a demuxer unless it has Demux in the klass name */
1267 if (!strstr (klass, "Demux"))
1270 /* Walk the src pad templates and count how many the element
1272 elemclass = GST_ELEMENT_GET_CLASS (srcelement);
1274 walk = templates = gst_element_class_get_pad_template_list (elemclass);
1275 while (walk != NULL) {
1276 GstPadTemplate *templ;
1278 templ = (GstPadTemplate *) walk->data;
1279 if (GST_PAD_TEMPLATE_DIRECTION (templ) == GST_PAD_SRC) {
1280 switch (GST_PAD_TEMPLATE_PRESENCE (templ)) {
1281 case GST_PAD_ALWAYS:
1282 case GST_PAD_SOMETIMES:
1283 if (strstr (GST_PAD_TEMPLATE_NAME_TEMPLATE (templ), "%"))
1284 potential_src_pads += 2; /* Might make multiple pads */
1286 potential_src_pads += 1;
1288 case GST_PAD_REQUEST:
1289 potential_src_pads += 2;
1293 walk = g_list_next (walk);
1296 if (potential_src_pads < 2)
1302 /* Returns TRUE if the caps are raw, or if they are compatible with the caps
1303 * specified in the 'caps' property
1305 * The decodebin_lock should be taken !
1308 are_raw_caps (GstDecodeBin * dbin, GstCaps * caps)
1310 GstCaps *intersection;
1313 GST_LOG_OBJECT (dbin, "Checking with caps %" GST_PTR_FORMAT, caps);
1315 intersection = gst_caps_intersect (dbin->caps, caps);
1317 res = (!(gst_caps_is_empty (intersection)));
1319 gst_caps_unref (intersection);
1321 GST_LOG_OBJECT (dbin, "Caps are %sfinal caps", res ? "" : "not ");
1328 * GstDecodeGroup functions
1332 multi_queue_overrun_cb (GstElement * queue, GstDecodeGroup * group)
1334 GST_LOG_OBJECT (group->dbin, "multiqueue is full");
1336 /* if we haven't exposed the group, do it */
1337 DECODE_BIN_LOCK (group->dbin);
1338 gst_decode_group_expose (group);
1339 DECODE_BIN_UNLOCK (group->dbin);
1343 multi_queue_underrun_cb (GstElement * queue, GstDecodeGroup * group)
1345 GstDecodeBin *dbin = group->dbin;
1347 GST_LOG_OBJECT (dbin, "multiqueue is empty for group %p", group);
1349 /* Check if we need to activate another group */
1350 DECODE_BIN_LOCK (dbin);
1351 if ((group == dbin->activegroup) && dbin->groups) {
1352 GST_DEBUG_OBJECT (dbin, "Switching to new group");
1353 /* unexpose current active */
1354 gst_decode_group_hide (group);
1356 /* expose first group of groups */
1357 gst_decode_group_expose ((GstDecodeGroup *) dbin->groups->data);
1359 DECODE_BIN_UNLOCK (dbin);
1362 /* gst_decode_group_new
1364 * Creates a new GstDecodeGroup. It is up to the caller to add it to the list
1367 static GstDecodeGroup *
1368 gst_decode_group_new (GstDecodeBin * dbin)
1370 GstDecodeGroup *group;
1373 GST_LOG_OBJECT (dbin, "Creating new group");
1375 if (!(mq = gst_element_factory_make ("multiqueue", NULL))) {
1376 GST_WARNING ("Couldn't create multiqueue element");
1380 g_object_set (G_OBJECT (mq),
1381 "max-size-bytes", 2 * 1024 * 1024,
1382 "max-size-time", 5 * GST_SECOND, "max-size-buffers", 0, NULL);
1384 group = g_new0 (GstDecodeGroup, 1);
1385 group->lock = g_mutex_new ();
1387 group->multiqueue = mq;
1388 group->exposed = FALSE;
1389 group->drained = FALSE;
1390 group->blocked = FALSE;
1391 group->complete = FALSE;
1392 group->endpads = NULL;
1394 group->overrunsig = g_signal_connect (G_OBJECT (mq), "overrun",
1395 G_CALLBACK (multi_queue_overrun_cb), group);
1396 group->underrunsig = g_signal_connect (G_OBJECT (mq), "underrun",
1397 G_CALLBACK (multi_queue_underrun_cb), group);
1399 gst_bin_add (GST_BIN (dbin), group->multiqueue);
1400 gst_element_set_state (group->multiqueue, GST_STATE_PAUSED);
1402 GST_LOG_OBJECT (dbin, "Returning new group %p", group);
1407 /** get_current_group:
1409 * Returns the current non-completed group.
1411 * Returns NULL if no groups are available, or all groups are completed.
1413 static GstDecodeGroup *
1414 get_current_group (GstDecodeBin * dbin)
1417 GstDecodeGroup *group = NULL;
1419 DECODE_BIN_LOCK (dbin);
1420 for (tmp = dbin->groups; tmp; tmp = g_list_next (tmp)) {
1421 GstDecodeGroup *this = (GstDecodeGroup *) tmp->data;
1423 GST_LOG_OBJECT (dbin, "group %p, complete:%d", this, this->complete);
1425 if (!this->complete) {
1430 DECODE_BIN_UNLOCK (dbin);
1432 GST_LOG_OBJECT (dbin, "Returning group %p", group);
1438 group_demuxer_event_probe (GstPad * pad, GstEvent * event,
1439 GstDecodeGroup * group)
1441 if (GST_EVENT_TYPE (event) == GST_EVENT_EOS) {
1442 GST_DEBUG_OBJECT (group->dbin,
1443 "Got EOS on group input pads, exposing group if it wasn't before");
1444 DECODE_BIN_LOCK (group->dbin);
1445 gst_decode_group_expose (group);
1446 DECODE_BIN_UNLOCK (group->dbin);
1451 /* gst_decode_group_control_demuxer_pad
1453 * Adds a new demuxer srcpad to the given group.
1455 * Returns the srcpad of the multiqueue corresponding the given pad.
1456 * Returns NULL if there was an error.
1459 gst_decode_group_control_demuxer_pad (GstDecodeGroup * group, GstPad * pad)
1461 GstPad *srcpad, *sinkpad;
1462 gchar *nb, *sinkname, *srcname;
1464 GST_LOG ("group:%p pad %s:%s", group, GST_DEBUG_PAD_NAME (pad));
1468 if (!(sinkpad = gst_element_get_pad (group->multiqueue, "sink%d"))) {
1469 GST_ERROR ("Couldn't get sinkpad from multiqueue");
1473 if ((gst_pad_link (pad, sinkpad) != GST_PAD_LINK_OK)) {
1474 GST_ERROR ("Couldn't link demuxer and multiqueue");
1478 sinkname = gst_pad_get_name (sinkpad);
1480 srcname = g_strdup_printf ("src%s", nb);
1483 GROUP_MUTEX_LOCK (group);
1485 if (!(srcpad = gst_element_get_pad (group->multiqueue, srcname))) {
1486 GST_ERROR ("Couldn't get srcpad %s from multiqueue", srcname);
1490 /* connect event handler on pad to intercept EOS events */
1491 gst_pad_add_event_probe (pad, G_CALLBACK (group_demuxer_event_probe), group);
1495 GROUP_MUTEX_UNLOCK (group);
1498 gst_object_unref (sinkpad);
1503 gst_decode_group_control_source_pad (GstDecodeGroup * group, GstPad * pad)
1507 g_return_val_if_fail (group != NULL, FALSE);
1509 GST_LOG ("group:%p , pad %s:%s", group, GST_DEBUG_PAD_NAME (pad));
1511 /* FIXME : check if pad is already controlled */
1513 GROUP_MUTEX_LOCK (group);
1515 /* Create GstDecodePad for the pad */
1516 dpad = gst_decode_pad_new (group, pad, TRUE);
1518 group->endpads = g_list_append (group->endpads, dpad);
1520 GROUP_MUTEX_UNLOCK (group);
1525 /* gst_decode_group_check_if_blocked:
1527 * Call this when one of the pads blocked status has changed.
1528 * If the group is complete and blocked, the group will be marked as blocked
1529 * and will ghost/expose all pads on decodebin if the group is the current one.
1531 * Call with the group lock taken ! MT safe
1534 gst_decode_group_check_if_blocked (GstDecodeGroup * group)
1537 gboolean blocked = TRUE;
1539 GST_LOG ("group : %p , ->complete:%d , ->nbdynamic:%d",
1540 group, group->complete, group->nbdynamic);
1542 /* 1. don't do anything if group is not complete */
1543 if (!group->complete || group->nbdynamic) {
1544 GST_DEBUG_OBJECT (group->dbin, "Group isn't complete yet");
1548 for (tmp = group->endpads; tmp; tmp = g_list_next (tmp)) {
1549 GstDecodePad *dpad = (GstDecodePad *) tmp->data;
1551 if (!dpad->blocked) {
1557 /* 2. Update status of group */
1558 group->blocked = blocked;
1559 GST_LOG ("group is blocked:%d", blocked);
1561 /* 3. don't do anything if not blocked completely */
1565 /* 4. if we're the current group, expose pads */
1566 DECODE_BIN_LOCK (group->dbin);
1567 if (!gst_decode_group_expose (group))
1568 GST_WARNING_OBJECT (group->dbin, "Couldn't expose group");
1569 DECODE_BIN_UNLOCK (group->dbin);
1573 gst_decode_group_check_if_drained (GstDecodeGroup * group)
1576 GstDecodeBin *dbin = group->dbin;
1577 gboolean drained = TRUE;
1579 GST_LOG ("group : %p", group);
1581 for (tmp = group->endpads; tmp; tmp = g_list_next (tmp)) {
1582 GstDecodePad *dpad = (GstDecodePad *) tmp->data;
1584 GST_LOG ("testing dpad %p", dpad);
1586 if (!dpad->drained) {
1592 group->drained = drained;
1593 GST_LOG ("group is drained");
1598 DECODE_BIN_LOCK (dbin);
1599 if ((group == dbin->activegroup) && dbin->groups) {
1600 GST_DEBUG_OBJECT (dbin, "Switching to new group");
1602 gst_decode_group_hide (group);
1604 gst_decode_group_expose ((GstDecodeGroup *) dbin->groups->data);
1606 DECODE_BIN_UNLOCK (dbin);
1610 * GCompareFunc to use with lists of GstPad.
1611 * Sorts pads by mime type.
1612 * First video (raw, then non-raw), then audio (raw, then non-raw),
1615 * Return: negative if a<b, 0 if a==b, positive if a>b
1619 sort_end_pads (GstDecodePad * da, GstDecodePad * db)
1623 GstCaps *capsa, *capsb;
1624 GstStructure *sa, *sb;
1625 const gchar *namea, *nameb;
1630 capsa = gst_pad_get_caps (a);
1631 capsb = gst_pad_get_caps (b);
1633 sa = gst_caps_get_structure ((const GstCaps *) capsa, 0);
1634 sb = gst_caps_get_structure ((const GstCaps *) capsb, 0);
1636 namea = gst_structure_get_name (sa);
1637 nameb = gst_structure_get_name (sb);
1639 if (g_strrstr (namea, "video/x-raw-"))
1641 else if (g_strrstr (namea, "video/"))
1643 else if (g_strrstr (namea, "audio/x-raw"))
1645 else if (g_strrstr (namea, "audio/"))
1650 if (g_strrstr (nameb, "video/x-raw-"))
1652 else if (g_strrstr (nameb, "video/"))
1654 else if (g_strrstr (nameb, "audio/x-raw"))
1656 else if (g_strrstr (nameb, "audio/"))
1661 gst_caps_unref (capsa);
1662 gst_caps_unref (capsb);
1667 /* gst_decode_group_expose:
1669 * Expose this group's pads.
1671 * Not MT safe, please take the group lock
1675 gst_decode_group_expose (GstDecodeGroup * group)
1680 if (group->dbin->activegroup) {
1681 GST_DEBUG_OBJECT (group->dbin, "A group is already active and exposed");
1685 if (group->dbin->activegroup == group) {
1686 GST_WARNING ("Group %p is already exposed", group);
1690 if (!group->dbin->groups
1691 || (group != (GstDecodeGroup *) group->dbin->groups->data)) {
1692 GST_WARNING ("Group %p is not the first group to expose", group);
1696 if (group->nbdynamic) {
1697 GST_WARNING ("Group %p still has %d dynamic objects, not exposing yet",
1698 group, group->nbdynamic);
1702 GST_LOG ("Exposing group %p", group);
1704 /* re-order pads : video, then audio, then others */
1705 group->endpads = g_list_sort (group->endpads, (GCompareFunc) sort_end_pads);
1709 for (tmp = group->endpads; tmp; tmp = next) {
1710 GstDecodePad *dpad = (GstDecodePad *) tmp->data;
1714 next = g_list_next (tmp);
1717 padname = g_strdup_printf ("src%d", group->dbin->nbpads);
1718 group->dbin->nbpads++;
1720 GST_LOG_OBJECT (group->dbin, "About to expose pad %s:%s",
1721 GST_DEBUG_PAD_NAME (dpad->pad));
1723 ghost = gst_ghost_pad_new (padname, dpad->pad);
1724 gst_pad_set_active (ghost, TRUE);
1725 gst_element_add_pad (GST_ELEMENT (group->dbin), ghost);
1726 group->ghosts = g_list_append (group->ghosts, ghost);
1730 /* 2. emit signal */
1731 GST_DEBUG_OBJECT (group->dbin, "emitting new-decoded-pad");
1732 g_signal_emit (G_OBJECT (group->dbin),
1733 gst_decode_bin_signals[SIGNAL_NEW_DECODED_PAD], 0, ghost,
1735 GST_DEBUG_OBJECT (group->dbin, "emitted new-decoded-pad");
1737 /* 3. Unblock internal pad */
1738 GST_DEBUG_OBJECT (dpad->pad, "unblocking");
1739 gst_pad_set_blocked_async (dpad->pad, FALSE,
1740 (GstPadBlockCallback) source_pad_blocked_cb, dpad);
1741 GST_DEBUG_OBJECT (dpad->pad, "unblocked");
1745 group->dbin->activegroup = group;
1747 /* pop off the first group */
1748 group->dbin->groups =
1749 g_list_delete_link (group->dbin->groups, group->dbin->groups);
1751 remove_fakesink (group->dbin);
1753 group->exposed = TRUE;
1755 GST_LOG_OBJECT (group->dbin, "signalling no-more-pads");
1756 gst_element_no_more_pads (GST_ELEMENT (group->dbin));
1758 GST_LOG_OBJECT (group->dbin, "Group %p exposed", group);
1763 gst_decode_group_hide (GstDecodeGroup * group)
1767 GST_LOG ("Hiding group %p", group);
1769 if (group != group->dbin->activegroup) {
1770 GST_WARNING ("This group is not the active one, aborting");
1774 GROUP_MUTEX_LOCK (group);
1776 /* Remove ghost pads */
1777 for (tmp = group->ghosts; tmp; tmp = g_list_next (tmp))
1778 gst_element_remove_pad (GST_ELEMENT (group->dbin), (GstPad *) tmp->data);
1780 g_list_free (group->ghosts);
1781 group->ghosts = NULL;
1783 group->exposed = FALSE;
1785 GROUP_MUTEX_UNLOCK (group);
1787 group->dbin->activegroup = NULL;
1788 group->dbin->oldgroups = g_list_append (group->dbin->oldgroups, group);
1792 deactivate_free_recursive (GstDecodeGroup * group, GstElement * element)
1795 GstIteratorResult res;
1798 GST_LOG ("element:%s", GST_ELEMENT_NAME (element));
1800 /* call on downstream elements */
1801 it = gst_element_iterate_src_pads (element);
1806 res = gst_iterator_next (it, &point);
1808 case GST_ITERATOR_DONE:
1810 case GST_ITERATOR_RESYNC:
1811 gst_iterator_resync (it);
1813 case GST_ITERATOR_ERROR:
1815 GST_WARNING ("Had an error while iterating source pads of element: %s",
1816 GST_ELEMENT_NAME (element));
1819 case GST_ITERATOR_OK:
1821 GstPad *pad = GST_PAD (point);
1822 GstPad *peerpad = NULL;
1824 if ((peerpad = gst_pad_get_peer (pad))) {
1825 GstObject *parent = gst_pad_get_parent (peerpad);
1827 if (parent && GST_IS_ELEMENT (parent))
1828 deactivate_free_recursive (group, GST_ELEMENT (parent));
1830 gst_object_unref (parent);
1840 gst_element_set_state (element, GST_STATE_NULL);
1841 gst_bin_remove (GST_BIN (group->dbin), element);
1844 gst_iterator_free (it);
1850 gst_decode_group_free (GstDecodeGroup * group)
1854 GST_LOG ("group %p", group);
1856 GROUP_MUTEX_LOCK (group);
1857 /* Clear all GstDecodePad */
1858 for (tmp = group->endpads; tmp; tmp = g_list_next (tmp)) {
1859 GstDecodePad *dpad = (GstDecodePad *) tmp->data;
1863 g_list_free (group->endpads);
1864 group->endpads = NULL;
1866 /* disconnect signal handlers on multiqueue */
1867 g_signal_handler_disconnect (group->multiqueue, group->underrunsig);
1868 g_signal_handler_disconnect (group->multiqueue, group->overrunsig);
1870 /* remove all elements */
1871 deactivate_free_recursive (group, group->multiqueue);
1873 GROUP_MUTEX_UNLOCK (group);
1875 g_mutex_free (group->lock);
1879 /* gst_decode_group_set_complete:
1881 * Mark the group as complete. This means no more streams will be controlled
1882 * through this group.
1887 gst_decode_group_set_complete (GstDecodeGroup * group)
1889 GST_LOG_OBJECT (group->dbin, "Setting group %p to COMPLETE", group);
1891 GROUP_MUTEX_LOCK (group);
1892 group->complete = TRUE;
1893 gst_decode_group_check_if_blocked (group);
1894 GROUP_MUTEX_UNLOCK (group);
1899 /*************************
1900 * GstDecodePad functions
1901 *************************/
1904 source_pad_blocked_cb (GstPad * pad, gboolean blocked, GstDecodePad * dpad)
1906 GST_LOG_OBJECT (pad, "blocked:%d , dpad:%p, dpad->group:%p",
1907 blocked, dpad, dpad->group);
1909 /* Update this GstDecodePad status */
1910 dpad->blocked = blocked;
1913 GROUP_MUTEX_LOCK (dpad->group);
1914 gst_decode_group_check_if_blocked (dpad->group);
1915 GROUP_MUTEX_UNLOCK (dpad->group);
1920 source_pad_event_probe (GstPad * pad, GstEvent * event, GstDecodePad * dpad)
1922 GST_LOG_OBJECT (pad, "%s dpad:%p", GST_EVENT_TYPE_NAME (event), dpad);
1924 if (GST_EVENT_TYPE (event) == GST_EVENT_EOS) {
1925 /* Set our pad as drained */
1926 dpad->drained = TRUE;
1928 /* Check if all pads are drained */
1929 gst_decode_group_check_if_drained (dpad->group);
1935 /*gst_decode_pad_new:
1937 * Creates a new GstDecodePad for the given pad.
1938 * If block is TRUE, Sets the pad blocking asynchronously
1941 static GstDecodePad *
1942 gst_decode_pad_new (GstDecodeGroup * group, GstPad * pad, gboolean block)
1946 dpad = g_new0 (GstDecodePad, 1);
1948 dpad->group = group;
1949 dpad->blocked = FALSE;
1950 dpad->drained = TRUE;
1953 gst_pad_set_blocked_async (pad, TRUE,
1954 (GstPadBlockCallback) source_pad_blocked_cb, dpad);
1955 gst_pad_add_event_probe (pad, G_CALLBACK (source_pad_event_probe), dpad);
1961 * Element add/remove
1965 * add_fakesink / remove_fakesink
1967 * We use a sink so that the parent ::change_state returns GST_STATE_CHANGE_ASYNC
1968 * when that sink is present (since it's not connected to anything it will
1969 * always return GST_STATE_CHANGE_ASYNC).
1971 * But this is an ugly way of achieving this goal.
1972 * Ideally, we shouldn't use a sink and just return GST_STATE_CHANGE_ASYNC in
1973 * our ::change_state if we have not exposed the active group.
1974 * We also need to override ::get_state to fake the asynchronous behaviour.
1975 * Once the active group is exposed, we would then post a
1976 * GST_MESSAGE_STATE_DIRTY and return GST_STATE_CHANGE_SUCCESS (which will call
1981 add_fakesink (GstDecodeBin * decode_bin)
1983 GST_DEBUG_OBJECT (decode_bin, "Adding the fakesink");
1985 if (decode_bin->fakesink)
1988 decode_bin->fakesink =
1989 gst_element_factory_make ("fakesink", "async-fakesink");
1990 if (!decode_bin->fakesink)
1993 /* hacky, remove sink flag, we don't want our decodebin to become a sink
1994 * just because we add a fakesink element to make us ASYNC */
1995 GST_OBJECT_FLAG_UNSET (decode_bin->fakesink, GST_ELEMENT_IS_SINK);
1997 if (!gst_bin_add (GST_BIN (decode_bin), decode_bin->fakesink))
2005 g_warning ("can't find fakesink element, decodebin will not work");
2010 g_warning ("Could not add fakesink to decodebin, decodebin will not work");
2011 gst_object_unref (decode_bin->fakesink);
2012 decode_bin->fakesink = NULL;
2018 remove_fakesink (GstDecodeBin * decode_bin)
2020 if (decode_bin->fakesink == NULL)
2023 GST_DEBUG_OBJECT (decode_bin, "Removing the fakesink");
2025 gst_element_set_state (decode_bin->fakesink, GST_STATE_NULL);
2026 gst_bin_remove (GST_BIN (decode_bin), decode_bin->fakesink);
2027 decode_bin->fakesink = NULL;
2029 gst_element_post_message (GST_ELEMENT_CAST (decode_bin),
2030 gst_message_new_state_dirty (GST_OBJECT_CAST (decode_bin)));
2034 * convenience functions
2039 * Returns the first sink pad of the given element, or NULL if it doesn't have
2044 find_sink_pad (GstElement * element)
2050 it = gst_element_iterate_sink_pads (element);
2052 if ((gst_iterator_next (it, &point)) == GST_ITERATOR_OK)
2053 pad = (GstPad *) point;
2055 gst_iterator_free (it);
2060 static GstStateChangeReturn
2061 gst_decode_bin_change_state (GstElement * element, GstStateChange transition)
2063 GstStateChangeReturn ret;
2064 GstDecodeBin *dbin = GST_DECODE_BIN (element);
2066 switch (transition) {
2067 case GST_STATE_CHANGE_NULL_TO_READY:
2068 if (dbin->typefind == NULL)
2069 goto missing_typefind;
2071 case GST_STATE_CHANGE_READY_TO_PAUSED:{
2072 if (!add_fakesink (dbin))
2073 goto missing_fakesink;
2080 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
2082 /* FIXME : put some cleanup functions here.. if needed */
2089 GST_ELEMENT_ERROR (dbin, CORE, MISSING_PLUGIN, (NULL), ("no typefind!"));
2090 return GST_STATE_CHANGE_FAILURE;
2094 GST_ELEMENT_ERROR (dbin, CORE, MISSING_PLUGIN, (NULL), ("no fakesink!"));
2095 return GST_STATE_CHANGE_FAILURE;
2100 plugin_init (GstPlugin * plugin)
2102 GST_DEBUG_CATEGORY_INIT (gst_decode_bin_debug, "decodebin2", 0,
2105 return gst_element_register (plugin, "decodebin2", GST_RANK_NONE,
2106 GST_TYPE_DECODE_BIN);
2109 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
2112 "decoder bin newer version", plugin_init, VERSION, GST_LICENSE,
2113 GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)