gst-gmyth/decodebin2/gstdecodebin2.c
author renatofilho
Wed Aug 01 14:22:14 2007 +0100 (2007-08-01)
branchtrunk
changeset 789 f9cd59844f78
child 792 a6ac25bf88a7
permissions -rw-r--r--
[svn r795] - fixed bug on transcode operation;
- some code clean;
     1 /* GStreamer
     2  * Copyright (C) <2006> Edward Hervey <edward@fluendo.com>
     3  *
     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.
     8  *
     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.
    13  *
    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.
    18  */
    19 
    20 /**
    21  * SECTION:element-decodebin2
    22  * @short_description: Next-generation automatic decoding bin
    23  *
    24  * #GstBin that auto-magically constructs a decoding pipeline using available
    25  * decoders and demuxers via auto-plugging.
    26  *
    27  * At this stage, decodebin2 is considered UNSTABLE. The API provided in the
    28  * signals is expected to change in the near future. 
    29  *
    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.
    33  */
    34 
    35 #ifdef HAVE_CONFIG_H
    36 #include "config.h"
    37 #endif
    38 
    39 #include <string.h>
    40 #include <gst/gst.h>
    41 
    42 #include "gstplay-marshal.h"
    43 
    44 /* generic templates */
    45 static GstStaticPadTemplate decoder_bin_sink_template =
    46 GST_STATIC_PAD_TEMPLATE ("sink",
    47     GST_PAD_SINK,
    48     GST_PAD_ALWAYS,
    49     GST_STATIC_CAPS_ANY);
    50 
    51 static GstStaticPadTemplate decoder_bin_src_template =
    52 GST_STATIC_PAD_TEMPLATE ("src%d",
    53     GST_PAD_SRC,
    54     GST_PAD_SOMETIMES,
    55     GST_STATIC_CAPS_ANY);
    56 
    57 GST_DEBUG_CATEGORY_STATIC (gst_decode_bin_debug);
    58 #define GST_CAT_DEFAULT gst_decode_bin_debug
    59 
    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;
    65 
    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))
    72 
    73 /**
    74  *  GstDecodeBin2:
    75  *
    76  *  The opaque #DecodeBin2 data structure
    77  */
    78 struct _GstDecodeBin
    79 {
    80   GstBin bin;                   /* we extend GstBin */
    81 
    82   GstElement *typefind;         /* this holds the typefind object */
    83   GstElement *fakesink;
    84 
    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 */
    93 
    94   GList *factories;             /* factories we can use for selecting elements */
    95 };
    96 
    97 struct _GstDecodeBinClass
    98 {
    99   GstBinClass parent_class;
   100 
   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,
   111       GList ** list);
   112 };
   113 
   114 /* signals */
   115 enum
   116 {
   117   SIGNAL_NEW_DECODED_PAD,
   118   SIGNAL_REMOVED_DECODED_PAD,
   119   SIGNAL_UNKNOWN_TYPE,
   120   SIGNAL_AUTOPLUG_CONTINUE,
   121   SIGNAL_AUTOPLUG_SORT,
   122   LAST_SIGNAL
   123 };
   124 
   125 /* Properties */
   126 enum
   127 {
   128   PROP_0,
   129   PROP_CAPS,
   130 };
   131 
   132 static GstBinClass *parent_class;
   133 static guint gst_decode_bin_signals[LAST_SIGNAL] = { 0 };
   134 
   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>");
   140 
   141 
   142 static gboolean add_fakesink (GstDecodeBin * decode_bin);
   143 static void remove_fakesink (GstDecodeBin * decode_bin);
   144 
   145 static void type_found (GstElement * typefind, guint probability,
   146     GstCaps * caps, GstDecodeBin * decode_bin);
   147 
   148 static gboolean gst_decode_bin_autoplug_continue (GstElement * element,
   149     GstCaps * caps);
   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);
   158 
   159 static GstPad *find_sink_pad (GstElement * element);
   160 static GstStateChangeReturn gst_decode_bin_change_state (GstElement * element,
   161     GstStateChange transition);
   162 
   163 #define DECODE_BIN_LOCK(dbin) G_STMT_START {				\
   164     GST_LOG_OBJECT (dbin,						\
   165 		    "locking from thread %p",				\
   166 		    g_thread_self ());					\
   167     g_mutex_lock (GST_DECODE_BIN_CAST(dbin)->lock);			\
   168     GST_LOG_OBJECT (dbin,						\
   169 		    "locked from thread %p",				\
   170 		    g_thread_self ());					\
   171 } G_STMT_END
   172 
   173 #define DECODE_BIN_UNLOCK(dbin) G_STMT_START {				\
   174     GST_LOG_OBJECT (dbin,						\
   175 		    "unlocking from thread %p",				\
   176 		    g_thread_self ());					\
   177     g_mutex_unlock (GST_DECODE_BIN_CAST(dbin)->lock);			\
   178 } G_STMT_END
   179 
   180 /* GstDecodeGroup
   181  *
   182  * Streams belonging to the same group/chain of a media file
   183  *
   184  */
   185 
   186 struct _GstDecodeGroup
   187 {
   188   GstDecodeBin *dbin;
   189   GMutex *lock;
   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 
   195                                  * on this group */
   196   gulong overrunsig;
   197   gulong underrunsig;
   198   guint nbdynamic;              /* number of dynamic pads in the group. */
   199 
   200   GList *endpads;               /* List of GstDecodePad of source pads to be exposed */
   201   GList *ghosts;                /* List of GstGhostPad for the endpads */
   202 };
   203 
   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 ());				\
   212 } G_STMT_END
   213 
   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);					\
   219 } G_STMT_END
   220 
   221 
   222 static GstDecodeGroup *gst_decode_group_new (GstDecodeBin * decode_bin);
   223 static GstPad *gst_decode_group_control_demuxer_pad (GstDecodeGroup * group,
   224     GstPad * pad);
   225 static gboolean gst_decode_group_control_source_pad (GstDecodeGroup * group,
   226     GstPad * pad);
   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);
   232 
   233 /* GstDecodePad
   234  *
   235  * GstPad private used for source pads of groups
   236  */
   237 
   238 struct _GstDecodePad
   239 {
   240   GstPad *pad;
   241   GstDecodeGroup *group;
   242   gboolean blocked;
   243   gboolean drained;
   244 };
   245 
   246 static GstDecodePad *gst_decode_pad_new (GstDecodeGroup * group, GstPad * pad,
   247     gboolean block);
   248 static void source_pad_blocked_cb (GstPad * pad, gboolean blocked,
   249     GstDecodePad * dpad);
   250 
   251 /* TempPadStruct
   252  * Internal structure used for pads which have more than one structure.
   253  */
   254 typedef struct _TempPadStruct
   255 {
   256   GstDecodeBin *dbin;
   257   GstDecodeGroup *group;
   258 } TempPadStruct;
   259 
   260 /********************************
   261  * Standard GObject boilerplate *
   262  ********************************/
   263 
   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);
   268 
   269 static GType
   270 gst_decode_bin_get_type (void)
   271 {
   272   static GType gst_decode_bin_type = 0;
   273 
   274   if (!gst_decode_bin_type) {
   275     static const GTypeInfo gst_decode_bin_info = {
   276       sizeof (GstDecodeBinClass),
   277       NULL,
   278       NULL,
   279       (GClassInitFunc) gst_decode_bin_class_init,
   280       NULL,
   281       NULL,
   282       sizeof (GstDecodeBin),
   283       0,
   284       (GInstanceInitFunc) gst_decode_bin_init,
   285       NULL
   286     };
   287 
   288     gst_decode_bin_type =
   289         g_type_register_static (GST_TYPE_BIN, "GstDecodeBin2",
   290         &gst_decode_bin_info, 0);
   291   }
   292 
   293   return gst_decode_bin_type;
   294 }
   295 
   296 static gboolean
   297 _gst_boolean_accumulator (GSignalInvocationHint * ihint,
   298     GValue * return_accu, const GValue * handler_return, gpointer dummy)
   299 {
   300   gboolean myboolean;
   301 
   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);
   305 
   306   /* stop emission if FALSE */
   307   return myboolean;
   308 }
   309 
   310 static void
   311 gst_decode_bin_class_init (GstDecodeBinClass * klass)
   312 {
   313   GObjectClass *gobject_klass;
   314   GstElementClass *gstelement_klass;
   315   GstBinClass *gstbin_klass;
   316 
   317   gobject_klass = (GObjectClass *) klass;
   318   gstelement_klass = (GstElementClass *) klass;
   319   gstbin_klass = (GstBinClass *) klass;
   320 
   321   parent_class = g_type_class_peek_parent (klass);
   322 
   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);
   327 
   328   /**
   329    * GstDecodeBin2::new-decoded-pad:
   330    * @pad: the newly created pad
   331    * @islast: #TRUE if this is the last pad to be added. Deprecated.
   332    *
   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.
   335    */
   336 
   337   gst_decode_bin_signals[SIGNAL_NEW_DECODED_PAD] =
   338       g_signal_new ("new-decoded-pad", G_TYPE_FROM_CLASS (klass),
   339       G_SIGNAL_RUN_LAST,
   340       G_STRUCT_OFFSET (GstDecodeBinClass, new_decoded_pad), NULL, NULL,
   341       gst_play_marshal_VOID__OBJECT_BOOLEAN, G_TYPE_NONE, 2, GST_TYPE_PAD,
   342       G_TYPE_BOOLEAN);
   343 
   344   /**
   345    * GstDecodeBin2::removed-decoded-pad:
   346    * @pad: the pad that was removed
   347    *
   348    * This signal is emitted when a 'final' caps pad has been removed.
   349    */
   350 
   351   gst_decode_bin_signals[SIGNAL_REMOVED_DECODED_PAD] =
   352       g_signal_new ("removed-decoded-pad", G_TYPE_FROM_CLASS (klass),
   353       G_SIGNAL_RUN_LAST,
   354       G_STRUCT_OFFSET (GstDecodeBinClass, removed_decoded_pad), NULL, NULL,
   355       gst_marshal_VOID__OBJECT, G_TYPE_NONE, 1, GST_TYPE_PAD);
   356 
   357   /**
   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.
   361    *
   362    * This signal is emitted when a pad for which there is no further possible
   363    * decoding is added to the decodebin.
   364    */
   365 
   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);
   371 
   372   /**
   373    * GstDecodeBin2::autoplug-continue:
   374    * @caps: The #GstCaps found.
   375    *
   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.
   378    *
   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'
   382    * signal).
   383    */
   384 
   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);
   390 
   391   /**
   392    * GstDecodeBin2::autoplug-sort:
   393    * @caps: The #GstCaps.
   394    * @factories: A #GList of possible #GstElementFactory to use.
   395    *
   396    * This signal is emitted once decodebin2 has found all the possible
   397    * #GstElementFactory that can be used to handle the given @caps.
   398    *
   399    * UNSTABLE API. Will change soon.
   400    *
   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).
   405    */
   406 
   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);
   412 
   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));
   416 
   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);
   420 
   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));
   425 
   426   gst_element_class_set_details (gstelement_klass, &gst_decode_bin_details);
   427 
   428   gstelement_klass->change_state =
   429       GST_DEBUG_FUNCPTR (gst_decode_bin_change_state);
   430 }
   431 
   432 /* the filter function for selecting the elements we can use in
   433  * autoplugging */
   434 static gboolean
   435 gst_decode_bin_factory_filter (GstPluginFeature * feature,
   436     GstDecodeBin * decode_bin)
   437 {
   438   guint rank;
   439   const gchar *klass;
   440 
   441   /* we only care about element factories */
   442   if (!GST_IS_ELEMENT_FACTORY (feature))
   443     return FALSE;
   444 
   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) {
   449     return FALSE;
   450   }
   451 
   452   /* only select elements with autoplugging rank */
   453   rank = gst_plugin_feature_get_rank (feature);
   454   if (rank < GST_RANK_MARGINAL)
   455     return FALSE;
   456 
   457   return TRUE;
   458 }
   459 
   460 /* function used to sort element features */
   461 static gint
   462 compare_ranks (GstPluginFeature * f1, GstPluginFeature * f2)
   463 {
   464   gint diff;
   465   const gchar *rname1, *rname2;
   466 
   467   diff = gst_plugin_feature_get_rank (f2) - gst_plugin_feature_get_rank (f1);
   468   if (diff != 0)
   469     return diff;
   470 
   471   rname1 = gst_plugin_feature_get_name (f1);
   472   rname2 = gst_plugin_feature_get_name (f2);
   473 
   474   diff = strcmp (rname2, rname1);
   475 
   476   return diff;
   477 }
   478 
   479 static void
   480 print_feature (GstPluginFeature * feature)
   481 {
   482   const gchar *rname;
   483 
   484   rname = gst_plugin_feature_get_name (feature);
   485 
   486   GST_DEBUG ("%s", rname);
   487 }
   488 
   489 static void
   490 gst_decode_bin_init (GstDecodeBin * decode_bin)
   491 {
   492   GList *factories;
   493 
   494   /* first filter out the interesting element factories */
   495   factories = gst_default_registry_feature_filter (
   496       (GstPluginFeatureFilter) gst_decode_bin_factory_filter,
   497       FALSE, decode_bin);
   498 
   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);
   503 
   504 
   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");
   509   } else {
   510     GstPad *pad;
   511     GstPad *gpad;
   512 
   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;
   518     }
   519 
   520     /* get the sinkpad */
   521     pad = gst_element_get_pad (decode_bin->typefind, "sink");
   522 
   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);
   527 
   528     gst_object_unref (pad);
   529 
   530     /* connect a signal to find out when the typefind element found
   531      * a type */
   532     g_signal_connect (G_OBJECT (decode_bin->typefind), "have_type",
   533         G_CALLBACK (type_found), decode_bin);
   534   }
   535 
   536   decode_bin->lock = g_mutex_new ();
   537   decode_bin->activegroup = NULL;
   538   decode_bin->groups = NULL;
   539 
   540   decode_bin->caps =
   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");
   543 
   544   add_fakesink (decode_bin);
   545 
   546   /* FILLME */
   547 }
   548 
   549 static void
   550 gst_decode_bin_dispose (GObject * object)
   551 {
   552   GstDecodeBin *decode_bin;
   553   GList *tmp;
   554 
   555   decode_bin = GST_DECODE_BIN (object);
   556 
   557   if (decode_bin->factories)
   558     gst_plugin_feature_list_free (decode_bin->factories);
   559   decode_bin->factories = NULL;
   560 
   561   if (decode_bin->activegroup) {
   562     gst_decode_group_free (decode_bin->activegroup);
   563     decode_bin->activegroup = NULL;
   564   }
   565 
   566   /* remove groups */
   567   for (tmp = decode_bin->groups; tmp; tmp = g_list_next (tmp)) {
   568     GstDecodeGroup *group = (GstDecodeGroup *) tmp->data;
   569 
   570     gst_decode_group_free (group);
   571   }
   572   g_list_free (decode_bin->groups);
   573   decode_bin->groups = NULL;
   574 
   575   for (tmp = decode_bin->oldgroups; tmp; tmp = g_list_next (tmp)) {
   576     GstDecodeGroup *group = (GstDecodeGroup *) tmp->data;
   577 
   578     gst_decode_group_free (group);
   579   }
   580   g_list_free (decode_bin->oldgroups);
   581   decode_bin->oldgroups = NULL;
   582 
   583   if (decode_bin->caps)
   584     gst_caps_unref (decode_bin->caps);
   585   decode_bin->caps = NULL;
   586   remove_fakesink (decode_bin);
   587 
   588   G_OBJECT_CLASS (parent_class)->dispose (object);
   589 }
   590 
   591 static void
   592 gst_decode_bin_finalize (GObject * object)
   593 {
   594   GstDecodeBin *decode_bin;
   595 
   596   decode_bin = GST_DECODE_BIN (object);
   597 
   598   if (decode_bin->lock) {
   599     g_mutex_free (decode_bin->lock);
   600     decode_bin->lock = NULL;
   601   }
   602 
   603   G_OBJECT_CLASS (parent_class)->finalize (object);
   604 }
   605 
   606 static void
   607 gst_decode_bin_set_property (GObject * object, guint prop_id,
   608     const GValue * value, GParamSpec * pspec)
   609 {
   610   GstDecodeBin *dbin;
   611 
   612   dbin = GST_DECODE_BIN (object);
   613 
   614   switch (prop_id) {
   615     case PROP_CAPS:
   616       gst_decode_bin_set_caps (dbin, (GstCaps *) g_value_dup_boxed (value));
   617       break;
   618     default:
   619       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
   620       break;
   621   }
   622 }
   623 
   624 static void
   625 gst_decode_bin_get_property (GObject * object, guint prop_id,
   626     GValue * value, GParamSpec * pspec)
   627 {
   628   GstDecodeBin *dbin;
   629 
   630   dbin = GST_DECODE_BIN (object);
   631   switch (prop_id) {
   632     case PROP_CAPS:{
   633       g_value_take_boxed (value, gst_decode_bin_get_caps (dbin));
   634       break;
   635     }
   636     default:
   637       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
   638       break;
   639   }
   640 
   641 }
   642 
   643 /* _set_caps
   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
   646  * taken.
   647  * @caps can be NULL.
   648  *
   649  * MT-safe
   650  */
   651 
   652 static void
   653 gst_decode_bin_set_caps (GstDecodeBin * dbin, GstCaps * caps)
   654 {
   655   GST_DEBUG_OBJECT (dbin, "Setting new caps: %" GST_PTR_FORMAT, caps);
   656 
   657   DECODE_BIN_LOCK (dbin);
   658   if (dbin->caps)
   659     gst_caps_unref (dbin->caps);
   660   dbin->caps = caps;
   661   DECODE_BIN_UNLOCK (dbin);
   662 }
   663 
   664 /* _get_caps
   665  * Returns the currently configured caps on which decodebin will stop decoding.
   666  * The returned caps (if not NULL), will have its refcount incremented.
   667  *
   668  * MT-safe
   669  */
   670 
   671 static GstCaps *
   672 gst_decode_bin_get_caps (GstDecodeBin * dbin)
   673 {
   674   GstCaps *caps;
   675 
   676   GST_DEBUG_OBJECT (dbin, "Getting currently set caps");
   677 
   678   DECODE_BIN_LOCK (dbin);
   679   caps = dbin->caps;
   680   if (caps)
   681     gst_caps_ref (caps);
   682   DECODE_BIN_UNLOCK (dbin);
   683 
   684   return caps;
   685 }
   686 
   687 /*****
   688  * Default autoplug signal handlers
   689  *****/
   690 
   691 static gboolean
   692 gst_decode_bin_autoplug_continue (GstElement * element, GstCaps * caps)
   693 {
   694   return TRUE;
   695 }
   696 
   697 static gboolean
   698 gst_decode_bin_autoplug_sort (GstElement * element, GstCaps * caps,
   699     GList ** list)
   700 {
   701   return TRUE;
   702 }
   703 
   704 
   705 
   706 /********
   707  * Discovery methods
   708  *****/
   709 
   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);
   714 
   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);
   721 
   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);
   733 
   734 static GstDecodeGroup *get_current_group (GstDecodeBin * dbin);
   735 
   736 static void
   737 analyze_new_pad (GstDecodeBin * dbin, GstElement * src, GstPad * pad,
   738     GstCaps * caps, GstDecodeGroup * group)
   739 {
   740   gboolean apcontinue = TRUE;
   741   GList *factories = NULL;
   742   gboolean apsort = TRUE;
   743 
   744   GST_DEBUG_OBJECT (dbin, "Pad %s:%s caps:%" GST_PTR_FORMAT,
   745       GST_DEBUG_PAD_NAME (pad), caps);
   746 
   747   if ((caps == NULL) || gst_caps_is_empty (caps))
   748     goto unknown_type;
   749 
   750   if (gst_caps_is_any (caps))
   751     goto any_caps;
   752 
   753   /* 1. Emit 'autoplug-continue' */
   754   g_signal_emit (G_OBJECT (dbin),
   755       gst_decode_bin_signals[SIGNAL_AUTOPLUG_CONTINUE], 0, caps, &apcontinue);
   756 
   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))
   759     goto expose_pad;
   760 
   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);
   767     if (!apsort) {
   768       g_list_free (factories);
   769       /* User doesn't want that pad */
   770       goto pad_not_wanted;
   771     }
   772   } else {
   773     /* no compatible factories */
   774     goto unknown_type;
   775   }
   776 
   777   /* 1.c else goto pad_is_valid */
   778   GST_LOG_OBJECT (pad, "Let's continue discovery on this pad");
   779 
   780   connect_pad (dbin, src, pad, factories, group);
   781   g_list_free (factories);
   782 
   783   return;
   784 
   785 expose_pad:
   786   {
   787     GST_LOG_OBJECT (dbin, "Pad is final. autoplug-continue:%d", apcontinue);
   788     expose_pad (dbin, src, pad, group);
   789     return;
   790   }
   791 
   792 pad_not_wanted:
   793   {
   794     GST_LOG_OBJECT (pad, "User doesn't want this pad, stopping discovery");
   795     return;
   796   }
   797 
   798 unknown_type:
   799   {
   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);
   803 
   804     /* Check if there are no pending groups, if so, remove fakesink */
   805     if (dbin->groups == NULL)
   806       remove_fakesink (dbin);
   807 
   808     return;
   809   }
   810 
   811 any_caps:
   812   {
   813     GST_WARNING_OBJECT (pad,
   814         "pad has ANY caps, not able to autoplug to anything");
   815     /* FIXME : connect to caps notification */
   816     return;
   817   }
   818 }
   819 
   820 
   821 /* connect_pad:
   822  *
   823  * Try to connect the given pad to an element created from one of the factories,
   824  * and recursively.
   825  *
   826  * Returns TRUE if an element was properly created and linked
   827  */
   828 
   829 static gboolean
   830 connect_pad (GstDecodeBin * dbin, GstElement * src, GstPad * pad,
   831     GList * factories, GstDecodeGroup * group)
   832 {
   833   gboolean res = FALSE;
   834   GList *tmp;
   835 
   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);
   839 
   840   /* 1. is element demuxer or parser */
   841   if (is_demuxer_element (src)) {
   842     GstPad *mqpad;
   843 
   844     GST_LOG_OBJECT (src, "is a demuxer, connecting the pad through multiqueue");
   845 
   846     if (!group)
   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);
   852       }
   853 
   854     if (!(mqpad = gst_decode_group_control_demuxer_pad (group, pad)))
   855       goto beach;
   856     pad = mqpad;
   857   }
   858 
   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;
   862     GstElement *element;
   863     GstPad *sinkpad;
   864 
   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)));
   869       continue;
   870     }
   871 
   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);
   877       continue;
   878     }
   879 
   880     /* 2.4 add it ... */
   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);
   886       continue;
   887     }
   888 
   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);
   896       continue;
   897     }
   898 
   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);
   906       continue;
   907     }
   908 
   909     GST_LOG_OBJECT (dbin, "linked on pad %s:%s", GST_DEBUG_PAD_NAME (pad));
   910 
   911     /* link this element further */
   912     connect_element (dbin, element, group);
   913 
   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);
   922       continue;
   923     }
   924 
   925     res = TRUE;
   926     break;
   927   }
   928 
   929 beach:
   930   return res;
   931 }
   932 
   933 static gboolean
   934 connect_element (GstDecodeBin * dbin, GstElement * element,
   935     GstDecodeGroup * group)
   936 {
   937   GList *pads;
   938   gboolean res = TRUE;
   939   gboolean dynamic = FALSE;
   940   GList *to_connect = NULL;
   941 
   942   GST_DEBUG_OBJECT (dbin, "Attempting to connect element %s [group:%p] further",
   943       GST_ELEMENT_NAME (element), group);
   944 
   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;
   950 
   951     /* we are only interested in source pads */
   952     if (GST_PAD_TEMPLATE_DIRECTION (templ) != GST_PAD_SRC)
   953       continue;
   954 
   955     templ_name = GST_PAD_TEMPLATE_NAME_TEMPLATE (templ);
   956     GST_DEBUG_OBJECT (dbin, "got a source pad template %s", templ_name);
   957 
   958     /* figure out what kind of pad this is */
   959     switch (GST_PAD_TEMPLATE_PRESENCE (templ)) {
   960       case GST_PAD_ALWAYS:
   961       {
   962         /* get the pad that we need to autoplug */
   963         GstPad *pad = gst_element_get_pad (element, templ_name);
   964 
   965         if (pad) {
   966           GST_DEBUG_OBJECT (dbin, "got the pad for always template %s",
   967               templ_name);
   968           /* here is the pad, we need to autoplug it */
   969           to_connect = g_list_prepend (to_connect, pad);
   970         } else {
   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);
   975         }
   976         break;
   977       }
   978       case GST_PAD_SOMETIMES:
   979       {
   980         /* try to get the pad to see if it is already created or
   981          * not */
   982         GstPad *pad = gst_element_get_pad (element, templ_name);
   983 
   984         if (pad) {
   985           GST_DEBUG_OBJECT (dbin, "got the pad for sometimes template %s",
   986               templ_name);
   987           /* the pad is created, we need to autoplug it */
   988           to_connect = g_list_prepend (to_connect, pad);
   989         } else {
   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 */
   993           dynamic = TRUE;
   994         }
   995         break;
   996       }
   997       case GST_PAD_REQUEST:
   998         /* ignore request pads */
   999         GST_DEBUG_OBJECT (dbin, "ignoring request padtemplate %s", templ_name);
  1000         break;
  1001     }
  1002   }
  1003 
  1004   /* 2. if there are more potential pads, connect to relevent signals */
  1005   if (dynamic) {
  1006     if (group) {
  1007       GST_LOG ("Adding signals to element %s in group %p",
  1008           GST_ELEMENT_NAME (element), group);
  1009       GROUP_MUTEX_LOCK (group);
  1010       group->nbdynamic++;
  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);
  1019     } else {
  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);
  1027     }
  1028   }
  1029 
  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);
  1033     GstCaps *caps;
  1034 
  1035     caps = gst_pad_get_caps (pad);
  1036     analyze_new_pad (dbin, element, pad, caps, group);
  1037     if (caps)
  1038       gst_caps_unref (caps);
  1039 
  1040     gst_object_unref (pad);
  1041   }
  1042   g_list_free (to_connect);
  1043 
  1044   return res;
  1045 }
  1046 
  1047 /* expose_pad:
  1048  *
  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.
  1051  */
  1052 static void
  1053 expose_pad (GstDecodeBin * dbin, GstElement * src, GstPad * pad,
  1054     GstDecodeGroup * group)
  1055 {
  1056   gboolean newgroup = FALSE;
  1057   gboolean isdemux;
  1058 
  1059   GST_DEBUG_OBJECT (dbin, "pad %s:%s, group:%p",
  1060       GST_DEBUG_PAD_NAME (pad), group);
  1061 
  1062   if (!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);
  1068       newgroup = TRUE;
  1069     }
  1070 
  1071   isdemux = is_demuxer_element (src);
  1072 
  1073   if (isdemux || newgroup) {
  1074     GstPad *mqpad;
  1075 
  1076     GST_LOG_OBJECT (src, "is a demuxer, connecting the pad through multiqueue");
  1077 
  1078     if (!(mqpad = gst_decode_group_control_demuxer_pad (group, pad)))
  1079       goto beach;
  1080     pad = mqpad;
  1081   }
  1082 
  1083   gst_decode_group_control_source_pad (group, pad);
  1084 
  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);
  1090   }
  1091 beach:
  1092   return;
  1093 }
  1094 
  1095 static void
  1096 type_found (GstElement * typefind, guint probability,
  1097     GstCaps * caps, GstDecodeBin * decode_bin)
  1098 {
  1099   GstPad *pad;
  1100 
  1101   GST_STATE_LOCK (decode_bin);
  1102 
  1103   GST_DEBUG_OBJECT (decode_bin, "typefind found caps %" GST_PTR_FORMAT, caps);
  1104 
  1105   pad = gst_element_get_pad (typefind, "src");
  1106 
  1107   analyze_new_pad (decode_bin, typefind, pad, caps, NULL);
  1108 
  1109   gst_object_unref (pad);
  1110 
  1111   GST_STATE_UNLOCK (decode_bin);
  1112   return;
  1113 }
  1114 
  1115 static void
  1116 pad_added_group_cb (GstElement * element, GstPad * pad, GstDecodeGroup * group)
  1117 {
  1118   GstCaps *caps;
  1119   gboolean expose = FALSE;
  1120 
  1121   GST_LOG_OBJECT (pad, "pad added, group:%p", group);
  1122 
  1123   caps = gst_pad_get_caps (pad);
  1124   analyze_new_pad (group->dbin, element, pad, caps, group);
  1125   if (caps)
  1126     gst_caps_unref (caps);
  1127 
  1128   GROUP_MUTEX_LOCK (group);
  1129   group->nbdynamic--;
  1130   GST_LOG ("Group %p has now %d dynamic objects", group, group->nbdynamic);
  1131   if (group->nbdynamic == 0)
  1132     expose = TRUE;
  1133   GROUP_MUTEX_UNLOCK (group);
  1134   if (expose) {
  1135     GST_LOG
  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);
  1140   }
  1141 }
  1142 
  1143 static void
  1144 pad_removed_group_cb (GstElement * element, GstPad * pad,
  1145     GstDecodeGroup * group)
  1146 {
  1147   GST_LOG_OBJECT (pad, "pad removed, group:%p", group);
  1148 
  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 */
  1151 }
  1152 
  1153 static void
  1154 no_more_pads_group_cb (GstElement * element, GstDecodeGroup * group)
  1155 {
  1156   GST_LOG_OBJECT (element, "no more pads, setting group %p to complete", group);
  1157 
  1158   /* FIXME : FILLME */
  1159   gst_decode_group_set_complete (group);
  1160 }
  1161 
  1162 static void
  1163 pad_added_cb (GstElement * element, GstPad * pad, GstDecodeBin * dbin)
  1164 {
  1165   GstCaps *caps;
  1166 
  1167   GST_LOG_OBJECT (pad, "Pad added to non-grouped element");
  1168 
  1169   caps = gst_pad_get_caps (pad);
  1170   analyze_new_pad (dbin, element, pad, caps, NULL);
  1171   if (caps)
  1172     gst_caps_unref (caps);
  1173 }
  1174 
  1175 static void
  1176 pad_removed_cb (GstElement * element, GstPad * pad, GstDecodeBin * dbin)
  1177 {
  1178   GST_LOG_OBJECT (pad, "Pad removed from non-grouped element");
  1179 }
  1180 
  1181 static void
  1182 no_more_pads_cb (GstElement * element, GstDecodeBin * dbin)
  1183 {
  1184   GstDecodeGroup *group;
  1185 
  1186   GST_LOG_OBJECT (element, "No more pads, setting current group to complete");
  1187 
  1188   /* Find the non-complete group, there should only be one */
  1189   if (!(group = get_current_group (dbin)))
  1190     goto no_group;
  1191 
  1192   gst_decode_group_set_complete (group);
  1193   return;
  1194 
  1195 no_group:
  1196   {
  1197     GST_WARNING_OBJECT (dbin, "We couldn't find a non-completed group !!");
  1198     return;
  1199   }
  1200 }
  1201 
  1202 /* this function runs through the element factories and returns a list
  1203  * of all elements that are able to sink the given caps 
  1204  */
  1205 static GList *
  1206 find_compatibles (GstDecodeBin * decode_bin, const GstCaps * caps)
  1207 {
  1208   GList *factories;
  1209   GList *to_try = NULL;
  1210 
  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;
  1216     GList *walk;
  1217 
  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;
  1222 
  1223       /* we only care about the sink templates */
  1224       if (templ->direction == GST_PAD_SINK) {
  1225         GstCaps *intersect;
  1226         GstCaps *tmpl_caps;
  1227 
  1228         /* try to intersect the caps with the caps of the template */
  1229         tmpl_caps = gst_static_caps_get (&templ->static_caps);
  1230 
  1231         intersect = gst_caps_intersect (caps, tmpl_caps);
  1232         gst_caps_unref (tmpl_caps);
  1233 
  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);
  1239           break;
  1240         }
  1241         gst_caps_unref (intersect);
  1242       }
  1243     }
  1244   }
  1245   to_try = g_list_reverse (to_try);
  1246 
  1247   return to_try;
  1248 }
  1249 
  1250 /* Decide whether an element is a demuxer based on the 
  1251  * klass and number/type of src pad templates it has */
  1252 static gboolean
  1253 is_demuxer_element (GstElement * srcelement)
  1254 {
  1255   GstElementFactory *srcfactory;
  1256   GstElementClass *elemclass;
  1257   GList *templates, *walk;
  1258   const gchar *klass;
  1259   gint potential_src_pads = 0;
  1260 
  1261   srcfactory = gst_element_get_factory (srcelement);
  1262   klass = gst_element_factory_get_klass (srcfactory);
  1263 
  1264   /* Can't be a demuxer unless it has Demux in the klass name */
  1265   if (!strstr (klass, "Demux"))
  1266     return FALSE;
  1267 
  1268   /* Walk the src pad templates and count how many the element
  1269    * might produce */
  1270   elemclass = GST_ELEMENT_GET_CLASS (srcelement);
  1271 
  1272   walk = templates = gst_element_class_get_pad_template_list (elemclass);
  1273   while (walk != NULL) {
  1274     GstPadTemplate *templ;
  1275 
  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 */
  1283           else
  1284             potential_src_pads += 1;
  1285           break;
  1286         case GST_PAD_REQUEST:
  1287           potential_src_pads += 2;
  1288           break;
  1289       }
  1290     }
  1291     walk = g_list_next (walk);
  1292   }
  1293 
  1294   if (potential_src_pads < 2)
  1295     return FALSE;
  1296 
  1297   return TRUE;
  1298 }
  1299 
  1300 /* Returns TRUE if the caps are raw, or if they are compatible with the caps 
  1301  * specified in the 'caps' property 
  1302  * 
  1303  * The decodebin_lock should be taken !
  1304  */
  1305 static gboolean
  1306 are_raw_caps (GstDecodeBin * dbin, GstCaps * caps)
  1307 {
  1308   GstCaps *intersection;
  1309   gboolean res;
  1310 
  1311   GST_LOG_OBJECT (dbin, "Checking with caps %" GST_PTR_FORMAT, caps);
  1312 
  1313   intersection = gst_caps_intersect (dbin->caps, caps);
  1314 
  1315   res = (!(gst_caps_is_empty (intersection)));
  1316 
  1317   gst_caps_unref (intersection);
  1318 
  1319   GST_LOG_OBJECT (dbin, "Caps are %sfinal caps", res ? "" : "not ");
  1320 
  1321   return res;
  1322 }
  1323 
  1324 
  1325 /****
  1326  * GstDecodeGroup functions
  1327  ****/
  1328 
  1329 static void
  1330 multi_queue_overrun_cb (GstElement * queue, GstDecodeGroup * group)
  1331 {
  1332   GST_LOG_OBJECT (group->dbin, "multiqueue is full");
  1333 
  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);
  1338 }
  1339 
  1340 static void
  1341 multi_queue_underrun_cb (GstElement * queue, GstDecodeGroup * group)
  1342 {
  1343   GstDecodeBin *dbin = group->dbin;
  1344 
  1345   GST_LOG_OBJECT (dbin, "multiqueue is empty for group %p", group);
  1346 
  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);
  1353 
  1354     /* expose first group of groups */
  1355     gst_decode_group_expose ((GstDecodeGroup *) dbin->groups->data);
  1356   }
  1357   DECODE_BIN_UNLOCK (dbin);
  1358 }
  1359 
  1360 /* gst_decode_group_new
  1361  *
  1362  * Creates a new GstDecodeGroup. It is up to the caller to add it to the list
  1363  * of groups.
  1364  */
  1365 static GstDecodeGroup *
  1366 gst_decode_group_new (GstDecodeBin * dbin)
  1367 {
  1368   GstDecodeGroup *group;
  1369   GstElement *mq;
  1370 
  1371   GST_LOG_OBJECT (dbin, "Creating new group");
  1372 
  1373   if (!(mq = gst_element_factory_make ("multiqueue", NULL))) {
  1374     GST_WARNING ("Couldn't create multiqueue element");
  1375     return NULL;
  1376   }
  1377 
  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);
  1381 
  1382   group = g_new0 (GstDecodeGroup, 1);
  1383   group->lock = g_mutex_new ();
  1384   group->dbin = dbin;
  1385   group->multiqueue = mq;
  1386   group->exposed = FALSE;
  1387   group->drained = FALSE;
  1388   group->blocked = FALSE;
  1389   group->complete = FALSE;
  1390   group->endpads = NULL;
  1391 
  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);
  1396 
  1397   gst_bin_add (GST_BIN (dbin), group->multiqueue);
  1398   gst_element_set_state (group->multiqueue, GST_STATE_PAUSED);
  1399 
  1400   GST_LOG_OBJECT (dbin, "Returning new group %p", group);
  1401 
  1402   return group;
  1403 }
  1404 
  1405 /** get_current_group:
  1406  *
  1407  * Returns the current non-completed group.
  1408  *
  1409  * Returns NULL if no groups are available, or all groups are completed.
  1410  */
  1411 static GstDecodeGroup *
  1412 get_current_group (GstDecodeBin * dbin)
  1413 {
  1414   GList *tmp;
  1415   GstDecodeGroup *group = NULL;
  1416 
  1417   DECODE_BIN_LOCK (dbin);
  1418   for (tmp = dbin->groups; tmp; tmp = g_list_next (tmp)) {
  1419     GstDecodeGroup *this = (GstDecodeGroup *) tmp->data;
  1420 
  1421     GST_LOG_OBJECT (dbin, "group %p, complete:%d", this, this->complete);
  1422 
  1423     if (!this->complete) {
  1424       group = this;
  1425       break;
  1426     }
  1427   }
  1428   DECODE_BIN_UNLOCK (dbin);
  1429 
  1430   GST_LOG_OBJECT (dbin, "Returning group %p", group);
  1431 
  1432   return group;
  1433 }
  1434 
  1435 static gboolean
  1436 group_demuxer_event_probe (GstPad * pad, GstEvent * event,
  1437     GstDecodeGroup * group)
  1438 {
  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);
  1445   }
  1446   return TRUE;
  1447 }
  1448 
  1449 /* gst_decode_group_control_demuxer_pad
  1450  *
  1451  * Adds a new demuxer srcpad to the given group.
  1452  *
  1453  * Returns the srcpad of the multiqueue corresponding the given pad.
  1454  * Returns NULL if there was an error.
  1455  */
  1456 static GstPad *
  1457 gst_decode_group_control_demuxer_pad (GstDecodeGroup * group, GstPad * pad)
  1458 {
  1459   GstPad *srcpad, *sinkpad;
  1460   gchar *nb, *sinkname, *srcname;
  1461 
  1462   GST_LOG ("group:%p pad %s:%s", group, GST_DEBUG_PAD_NAME (pad));
  1463 
  1464   srcpad = NULL;
  1465 
  1466   if (!(sinkpad = gst_element_get_pad (group->multiqueue, "sink%d"))) {
  1467     GST_ERROR ("Couldn't get sinkpad from multiqueue");
  1468     return NULL;
  1469   }
  1470 
  1471   if ((gst_pad_link (pad, sinkpad) != GST_PAD_LINK_OK)) {
  1472     GST_ERROR ("Couldn't link demuxer and multiqueue");
  1473     goto beach;
  1474   }
  1475 
  1476   sinkname = gst_pad_get_name (sinkpad);
  1477   nb = sinkname + 4;
  1478   srcname = g_strdup_printf ("src%s", nb);
  1479   g_free (sinkname);
  1480 
  1481   GROUP_MUTEX_LOCK (group);
  1482 
  1483   if (!(srcpad = gst_element_get_pad (group->multiqueue, srcname))) {
  1484     GST_ERROR ("Couldn't get srcpad %s from multiqueue", srcname);
  1485     goto chiringuito;
  1486   }
  1487 
  1488   /* connect event handler on pad to intercept EOS events */
  1489   gst_pad_add_event_probe (pad, G_CALLBACK (group_demuxer_event_probe), group);
  1490 
  1491 chiringuito:
  1492   g_free (srcname);
  1493   GROUP_MUTEX_UNLOCK (group);
  1494 
  1495 beach:
  1496   gst_object_unref (sinkpad);
  1497   return srcpad;
  1498 }
  1499 
  1500 static gboolean
  1501 gst_decode_group_control_source_pad (GstDecodeGroup * group, GstPad * pad)
  1502 {
  1503   GstDecodePad *dpad;
  1504 
  1505   g_return_val_if_fail (group != NULL, FALSE);
  1506 
  1507   GST_LOG ("group:%p , pad %s:%s", group, GST_DEBUG_PAD_NAME (pad));
  1508 
  1509   /* FIXME : check if pad is already controlled */
  1510 
  1511   GROUP_MUTEX_LOCK (group);
  1512 
  1513   /* Create GstDecodePad for the pad */
  1514   dpad = gst_decode_pad_new (group, pad, TRUE);
  1515 
  1516   group->endpads = g_list_append (group->endpads, dpad);
  1517 
  1518   GROUP_MUTEX_UNLOCK (group);
  1519 
  1520   return TRUE;
  1521 }
  1522 
  1523 /* gst_decode_group_check_if_blocked:
  1524  *
  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.
  1528  *
  1529  * Call with the group lock taken ! MT safe
  1530  */
  1531 static void
  1532 gst_decode_group_check_if_blocked (GstDecodeGroup * group)
  1533 {
  1534   GList *tmp;
  1535   gboolean blocked = TRUE;
  1536 
  1537   GST_LOG ("group : %p , ->complete:%d , ->nbdynamic:%d",
  1538       group, group->complete, group->nbdynamic);
  1539 
  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");
  1543     return;
  1544   }
  1545 
  1546   for (tmp = group->endpads; tmp; tmp = g_list_next (tmp)) {
  1547     GstDecodePad *dpad = (GstDecodePad *) tmp->data;
  1548 
  1549     if (!dpad->blocked) {
  1550       blocked = FALSE;
  1551       break;
  1552     }
  1553   }
  1554 
  1555   /* 2. Update status of group */
  1556   group->blocked = blocked;
  1557   GST_LOG ("group is blocked:%d", blocked);
  1558 
  1559   /* 3. don't do anything if not blocked completely */
  1560   if (!blocked)
  1561     return;
  1562 
  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);
  1568 }
  1569 
  1570 static void
  1571 gst_decode_group_check_if_drained (GstDecodeGroup * group)
  1572 {
  1573   GList *tmp;
  1574   GstDecodeBin *dbin = group->dbin;
  1575   gboolean drained = TRUE;
  1576 
  1577   GST_LOG ("group : %p", group);
  1578 
  1579   for (tmp = group->endpads; tmp; tmp = g_list_next (tmp)) {
  1580     GstDecodePad *dpad = (GstDecodePad *) tmp->data;
  1581 
  1582     GST_LOG ("testing dpad %p", dpad);
  1583 
  1584     if (!dpad->drained) {
  1585       drained = FALSE;
  1586       break;
  1587     }
  1588   }
  1589 
  1590   group->drained = drained;
  1591   GST_LOG ("group is drained");
  1592 
  1593   if (!drained)
  1594     return;
  1595 
  1596   DECODE_BIN_LOCK (dbin);
  1597   if ((group == dbin->activegroup) && dbin->groups) {
  1598     GST_DEBUG_OBJECT (dbin, "Switching to new group");
  1599 
  1600     gst_decode_group_hide (group);
  1601 
  1602     gst_decode_group_expose ((GstDecodeGroup *) dbin->groups->data);
  1603   }
  1604   DECODE_BIN_UNLOCK (dbin);
  1605 }
  1606 
  1607 /* sort_end_pads:
  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),
  1611  * then others.
  1612  *
  1613  * Return: negative if a<b, 0 if a==b, positive if a>b
  1614  */
  1615 
  1616 static gint
  1617 sort_end_pads (GstDecodePad * da, GstDecodePad * db)
  1618 {
  1619   GstPad *a, *b;
  1620   gint va, vb;
  1621   GstCaps *capsa, *capsb;
  1622   GstStructure *sa, *sb;
  1623   const gchar *namea, *nameb;
  1624 
  1625   a = da->pad;
  1626   b = db->pad;
  1627 
  1628   capsa = gst_pad_get_caps (a);
  1629   capsb = gst_pad_get_caps (b);
  1630 
  1631   sa = gst_caps_get_structure ((const GstCaps *) capsa, 0);
  1632   sb = gst_caps_get_structure ((const GstCaps *) capsb, 0);
  1633 
  1634   namea = gst_structure_get_name (sa);
  1635   nameb = gst_structure_get_name (sb);
  1636 
  1637   if (g_strrstr (namea, "video/x-raw-"))
  1638     va = 0;
  1639   else if (g_strrstr (namea, "video/"))
  1640     va = 1;
  1641   else if (g_strrstr (namea, "audio/x-raw"))
  1642     va = 2;
  1643   else if (g_strrstr (namea, "audio/"))
  1644     va = 3;
  1645   else
  1646     va = 4;
  1647 
  1648   if (g_strrstr (nameb, "video/x-raw-"))
  1649     vb = 0;
  1650   else if (g_strrstr (nameb, "video/"))
  1651     vb = 1;
  1652   else if (g_strrstr (nameb, "audio/x-raw"))
  1653     vb = 2;
  1654   else if (g_strrstr (nameb, "audio/"))
  1655     vb = 3;
  1656   else
  1657     vb = 4;
  1658 
  1659   gst_caps_unref (capsa);
  1660   gst_caps_unref (capsb);
  1661 
  1662   return va - vb;
  1663 }
  1664 
  1665 /* gst_decode_group_expose:
  1666  *
  1667  * Expose this group's pads.
  1668  *
  1669  * Not MT safe, please take the group lock
  1670  */
  1671 
  1672 static gboolean
  1673 gst_decode_group_expose (GstDecodeGroup * group)
  1674 {
  1675   GList *tmp;
  1676   GList *next = NULL;
  1677 
  1678   if (group->dbin->activegroup) {
  1679     GST_DEBUG_OBJECT (group->dbin, "A group is already active and exposed");
  1680     return TRUE;
  1681   }
  1682 
  1683   if (group->dbin->activegroup == group) {
  1684     GST_WARNING ("Group %p is already exposed", group);
  1685     return TRUE;
  1686   }
  1687 
  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);
  1691     return FALSE;
  1692   }
  1693 
  1694   if (group->nbdynamic) {
  1695     GST_WARNING ("Group %p still has %d dynamic objects, not exposing yet",
  1696         group, group->nbdynamic);
  1697     return FALSE;
  1698   }
  1699 
  1700   GST_LOG ("Exposing group %p", group);
  1701 
  1702   /* re-order pads : video, then audio, then others */
  1703   group->endpads = g_list_sort (group->endpads, (GCompareFunc) sort_end_pads);
  1704 
  1705   /* Expose pads */
  1706 
  1707   for (tmp = group->endpads; tmp; tmp = next) {
  1708     GstDecodePad *dpad = (GstDecodePad *) tmp->data;
  1709     gchar *padname;
  1710     GstPad *ghost;
  1711 
  1712     next = g_list_next (tmp);
  1713 
  1714     /* 1. ghost pad */
  1715     padname = g_strdup_printf ("src%d", group->dbin->nbpads);
  1716     group->dbin->nbpads++;
  1717 
  1718     GST_LOG_OBJECT (group->dbin, "About to expose pad %s:%s",
  1719         GST_DEBUG_PAD_NAME (dpad->pad));
  1720 
  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);
  1725 
  1726     g_free (padname);
  1727 
  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,
  1732         (next == NULL));
  1733     GST_DEBUG_OBJECT (group->dbin, "emitted new-decoded-pad");
  1734 
  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");
  1740 
  1741   }
  1742 
  1743   group->dbin->activegroup = group;
  1744 
  1745   /* pop off the first group */
  1746   group->dbin->groups =
  1747       g_list_delete_link (group->dbin->groups, group->dbin->groups);
  1748 
  1749   remove_fakesink (group->dbin);
  1750 
  1751   group->exposed = TRUE;
  1752 
  1753   GST_LOG_OBJECT (group->dbin, "signalling no-more-pads");
  1754   gst_element_no_more_pads (GST_ELEMENT (group->dbin));
  1755 
  1756   GST_LOG_OBJECT (group->dbin, "Group %p exposed", group);
  1757   return TRUE;
  1758 }
  1759 
  1760 static void
  1761 gst_decode_group_hide (GstDecodeGroup * group)
  1762 {
  1763   GList *tmp;
  1764 
  1765   GST_LOG ("Hiding group %p", group);
  1766 
  1767   if (group != group->dbin->activegroup) {
  1768     GST_WARNING ("This group is not the active one, aborting");
  1769     return;
  1770   }
  1771 
  1772   GROUP_MUTEX_LOCK (group);
  1773 
  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);
  1777 
  1778   g_list_free (group->ghosts);
  1779   group->ghosts = NULL;
  1780 
  1781   group->exposed = FALSE;
  1782 
  1783   GROUP_MUTEX_UNLOCK (group);
  1784 
  1785   group->dbin->activegroup = NULL;
  1786   group->dbin->oldgroups = g_list_append (group->dbin->oldgroups, group);
  1787 }
  1788 
  1789 static void
  1790 deactivate_free_recursive (GstDecodeGroup * group, GstElement * element)
  1791 {
  1792   GstIterator *it;
  1793   GstIteratorResult res;
  1794   gpointer point;
  1795 
  1796   GST_LOG ("element:%s", GST_ELEMENT_NAME (element));
  1797 
  1798   /* call on downstream elements */
  1799   it = gst_element_iterate_src_pads (element);
  1800 
  1801 restart:
  1802 
  1803   while (1) {
  1804     res = gst_iterator_next (it, &point);
  1805     switch (res) {
  1806       case GST_ITERATOR_DONE:
  1807         goto done;
  1808       case GST_ITERATOR_RESYNC:
  1809         gst_iterator_resync (it);
  1810         goto restart;
  1811       case GST_ITERATOR_ERROR:
  1812       {
  1813         GST_WARNING ("Had an error while iterating source pads of element: %s",
  1814             GST_ELEMENT_NAME (element));
  1815         goto beach;
  1816       }
  1817       case GST_ITERATOR_OK:
  1818       {
  1819         GstPad *pad = GST_PAD (point);
  1820         GstPad *peerpad = NULL;
  1821 
  1822         if ((peerpad = gst_pad_get_peer (pad))) {
  1823           GstObject *parent = gst_pad_get_parent (peerpad);
  1824 
  1825           if (parent && GST_IS_ELEMENT (parent))
  1826             deactivate_free_recursive (group, GST_ELEMENT (parent));
  1827           if (parent)
  1828             gst_object_unref (parent);
  1829         }
  1830       }
  1831         break;
  1832       default:
  1833         break;
  1834     }
  1835   }
  1836 
  1837 done:
  1838   gst_element_set_state (element, GST_STATE_NULL);
  1839   gst_bin_remove (GST_BIN (group->dbin), element);
  1840 
  1841 beach:
  1842   gst_iterator_free (it);
  1843 
  1844   return;
  1845 }
  1846 
  1847 static void
  1848 gst_decode_group_free (GstDecodeGroup * group)
  1849 {
  1850   GList *tmp;
  1851 
  1852   GST_LOG ("group %p", group);
  1853 
  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;
  1858 
  1859     g_free (dpad);
  1860   }
  1861   g_list_free (group->endpads);
  1862   group->endpads = NULL;
  1863 
  1864   /* disconnect signal handlers on multiqueue */
  1865   g_signal_handler_disconnect (group->multiqueue, group->underrunsig);
  1866   g_signal_handler_disconnect (group->multiqueue, group->overrunsig);
  1867 
  1868   /* remove all elements */
  1869   deactivate_free_recursive (group, group->multiqueue);
  1870 
  1871   GROUP_MUTEX_UNLOCK (group);
  1872 
  1873   g_mutex_free (group->lock);
  1874   g_free (group);
  1875 }
  1876 
  1877 /* gst_decode_group_set_complete:
  1878  *
  1879  * Mark the group as complete. This means no more streams will be controlled
  1880  * through this group.
  1881  *
  1882  * MT safe
  1883  */
  1884 static void
  1885 gst_decode_group_set_complete (GstDecodeGroup * group)
  1886 {
  1887   GST_LOG_OBJECT (group->dbin, "Setting group %p to COMPLETE", group);
  1888 
  1889   GROUP_MUTEX_LOCK (group);
  1890   group->complete = TRUE;
  1891   gst_decode_group_check_if_blocked (group);
  1892   GROUP_MUTEX_UNLOCK (group);
  1893 }
  1894 
  1895 
  1896 
  1897 /*************************
  1898  * GstDecodePad functions
  1899  *************************/
  1900 
  1901 static void
  1902 source_pad_blocked_cb (GstPad * pad, gboolean blocked, GstDecodePad * dpad)
  1903 {
  1904   GST_LOG_OBJECT (pad, "blocked:%d , dpad:%p, dpad->group:%p",
  1905       blocked, dpad, dpad->group);
  1906 
  1907   /* Update this GstDecodePad status */
  1908   dpad->blocked = blocked;
  1909 
  1910   if (blocked) {
  1911     GROUP_MUTEX_LOCK (dpad->group);
  1912     gst_decode_group_check_if_blocked (dpad->group);
  1913     GROUP_MUTEX_UNLOCK (dpad->group);
  1914   }
  1915 }
  1916 
  1917 static gboolean
  1918 source_pad_event_probe (GstPad * pad, GstEvent * event, GstDecodePad * dpad)
  1919 {
  1920   GST_LOG_OBJECT (pad, "%s dpad:%p", GST_EVENT_TYPE_NAME (event), dpad);
  1921 
  1922   if (GST_EVENT_TYPE (event) == GST_EVENT_EOS) {
  1923     /* Set our pad as drained */
  1924     dpad->drained = TRUE;
  1925 
  1926     /* Check if all pads are drained */
  1927     gst_decode_group_check_if_drained (dpad->group);
  1928   }
  1929 
  1930   return TRUE;
  1931 }
  1932 
  1933 /*gst_decode_pad_new:
  1934  *
  1935  * Creates a new GstDecodePad for the given pad.
  1936  * If block is TRUE, Sets the pad blocking asynchronously
  1937  */
  1938 
  1939 static GstDecodePad *
  1940 gst_decode_pad_new (GstDecodeGroup * group, GstPad * pad, gboolean block)
  1941 {
  1942   GstDecodePad *dpad;
  1943 
  1944   dpad = g_new0 (GstDecodePad, 1);
  1945   dpad->pad = pad;
  1946   dpad->group = group;
  1947   dpad->blocked = FALSE;
  1948   dpad->drained = TRUE;
  1949 
  1950   if (block)
  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);
  1954   return dpad;
  1955 }
  1956 
  1957 
  1958 /*****
  1959  * Element add/remove
  1960  *****/
  1961 
  1962 /*
  1963  * add_fakesink / remove_fakesink
  1964  *
  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).
  1968  *
  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
  1975  * ::get_state .
  1976  */
  1977 
  1978 static gboolean
  1979 add_fakesink (GstDecodeBin * decode_bin)
  1980 {
  1981   GST_DEBUG_OBJECT (decode_bin, "Adding the fakesink");
  1982 
  1983   if (decode_bin->fakesink)
  1984     return TRUE;
  1985 
  1986   decode_bin->fakesink =
  1987       gst_element_factory_make ("fakesink", "async-fakesink");
  1988   if (!decode_bin->fakesink)
  1989     goto no_fakesink;
  1990 
  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);
  1994 
  1995   if (!gst_bin_add (GST_BIN (decode_bin), decode_bin->fakesink))
  1996     goto could_not_add;
  1997 
  1998   return TRUE;
  1999 
  2000   /* ERRORS */
  2001 no_fakesink:
  2002   {
  2003     g_warning ("can't find fakesink element, decodebin will not work");
  2004     return FALSE;
  2005   }
  2006 could_not_add:
  2007   {
  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;
  2011     return FALSE;
  2012   }
  2013 }
  2014 
  2015 static void
  2016 remove_fakesink (GstDecodeBin * decode_bin)
  2017 {
  2018   if (decode_bin->fakesink == NULL)
  2019     return;
  2020 
  2021   GST_DEBUG_OBJECT (decode_bin, "Removing the fakesink");
  2022 
  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;
  2026 
  2027   gst_element_post_message (GST_ELEMENT_CAST (decode_bin),
  2028       gst_message_new_state_dirty (GST_OBJECT_CAST (decode_bin)));
  2029 }
  2030 
  2031 /*****
  2032  * convenience functions
  2033  *****/
  2034 
  2035 /* find_sink_pad
  2036  *
  2037  * Returns the first sink pad of the given element, or NULL if it doesn't have
  2038  * any.
  2039  */
  2040 
  2041 static GstPad *
  2042 find_sink_pad (GstElement * element)
  2043 {
  2044   GstIterator *it;
  2045   GstPad *pad = NULL;
  2046   gpointer point;
  2047 
  2048   it = gst_element_iterate_sink_pads (element);
  2049 
  2050   if ((gst_iterator_next (it, &point)) == GST_ITERATOR_OK)
  2051     pad = (GstPad *) point;
  2052 
  2053   gst_iterator_free (it);
  2054 
  2055   return pad;
  2056 }
  2057 
  2058 static GstStateChangeReturn
  2059 gst_decode_bin_change_state (GstElement * element, GstStateChange transition)
  2060 {
  2061   GstStateChangeReturn ret;
  2062   GstDecodeBin *dbin = GST_DECODE_BIN (element);
  2063 
  2064   switch (transition) {
  2065     case GST_STATE_CHANGE_NULL_TO_READY:
  2066       if (dbin->typefind == NULL)
  2067         goto missing_typefind;
  2068       break;
  2069     case GST_STATE_CHANGE_READY_TO_PAUSED:{
  2070       if (!add_fakesink (dbin))
  2071         goto missing_fakesink;
  2072       break;
  2073     }
  2074     default:
  2075       break;
  2076   }
  2077 
  2078   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
  2079 
  2080   /* FIXME : put some cleanup functions here.. if needed */
  2081 
  2082   return ret;
  2083 
  2084 /* ERRORS */
  2085 missing_typefind:
  2086   {
  2087     GST_ELEMENT_ERROR (dbin, CORE, MISSING_PLUGIN, (NULL), ("no typefind!"));
  2088     return GST_STATE_CHANGE_FAILURE;
  2089   }
  2090 missing_fakesink:
  2091   {
  2092     GST_ELEMENT_ERROR (dbin, CORE, MISSING_PLUGIN, (NULL), ("no fakesink!"));
  2093     return GST_STATE_CHANGE_FAILURE;
  2094   }
  2095 }
  2096 
  2097 static gboolean
  2098 plugin_init (GstPlugin * plugin)
  2099 {
  2100   GST_DEBUG_CATEGORY_INIT (gst_decode_bin_debug, "decodebin2", 0,
  2101       "decoder bin");
  2102 
  2103   return gst_element_register (plugin, "decodebin2", GST_RANK_NONE,
  2104       GST_TYPE_DECODE_BIN);
  2105 }
  2106 
  2107 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
  2108     GST_VERSION_MINOR,
  2109     "decodebin2",
  2110     "decoder bin newer version", plugin_init, VERSION, GST_LICENSE,
  2111     GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)