decodebin3: fix caps leaks
[platform/upstream/gstreamer.git] / gst / playback / gstparsebin.c
1 /* GStreamer
2  * Copyright (C) <2006> Edward Hervey <edward@fluendo.com>
3  * Copyright (C) <2009> Sebastian Dröge <sebastian.droege@collabora.co.uk>
4  * Copyright (C) <2011> Hewlett-Packard Development Company, L.P.
5  *   Author: Sebastian Dröge <sebastian.droege@collabora.co.uk>, Collabora Ltd.
6  * Copyright (C) <2013> Collabora Ltd.
7  *   Author: Sebastian Dröge <sebastian.droege@collabora.co.uk>
8  * Copyright (C) <2015-2016> Centricular Ltd
9  *  @author: Edward Hervey <edward@centricular.com>
10  *  @author: Jan Schmidt <jan@centricular.com>
11  *
12  * This library is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU Library General Public
14  * License as published by the Free Software Foundation; either
15  * version 2 of the License, or (at your option) any later version.
16  *
17  * This library is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20  * Library General Public License for more details.
21  *
22  * You should have received a copy of the GNU Library General Public
23  * License along with this library; if not, write to the
24  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
25  * Boston, MA 02110-1301, USA.
26  */
27
28 /**
29  * SECTION:element-ParseBin
30  *
31  * #GstBin that auto-magically constructs a parsing pipeline
32  * using available parsers and demuxers via auto-plugging.
33  *
34  */
35
36 /* Implementation notes:
37  *
38  * The following section describes how ParseBin works internally.
39  *
40  * The first part of ParseBin is its typefind element, which tries
41  * to determine the media type of the input stream. If the type is found
42  * autoplugging starts.
43  *
44  * ParseBin internally organizes the elements it autoplugged into
45  * GstParseChains and GstParseGroups. A parse chain is a single chain
46  * of parsing, this
47  * means that if ParseBin ever autoplugs an element with two+ srcpads
48  * (e.g. a demuxer) this will end the chain and everything following this
49  * demuxer will be put into parse groups below the chain. Otherwise,
50  * if an element has a single srcpad that outputs raw data the parse chain
51  * is ended too and a GstParsePad is stored and blocked.
52  *
53  * A parse group combines a number of chains that are created by a
54  * demuxer element.
55  *
56  * This continues until the top-level parse chain is complete. A parse
57  * chain is complete if it either ends with a blocked elementary stream,
58  * if autoplugging stopped because no suitable plugins could be found
59  * or if the active group is complete. A parse group on the other hand
60  * is complete if all child chains are complete.
61  *
62  * If this happens at some point, all end pads of all active groups are exposed.
63  * For this ParseBin adds the end pads, and then unblocks them. Now playback starts.
64  *
65  * If one of the chains that end on a endpad receives EOS ParseBin checks
66  * if all chains and groups are drained. In that case everything goes into EOS.
67  * If there is a chain where the active group is drained but there exist next
68  * groups, the active group is hidden (endpads are removed) and the next group
69  * is exposed. This means that in some cases more pads may be created even
70  * after the initial no-more-pads signal. This happens for example with
71  * so-called "chained oggs", most commonly found among ogg/vorbis internet
72  * radio streams.
73  *
74  * Note 1: If we're talking about blocked endpads this really means that the
75  * *target* pads of the endpads are blocked. Pads that are exposed to the outside
76  * should never ever be blocked!
77  *
78  * Note 2: If a group is complete and the parent's chain demuxer adds new pads
79  * but never signaled no-more-pads this additional pads will be ignored!
80  *
81  */
82
83 /* FIXME 0.11: suppress warnings for deprecated API such as GValueArray
84  * with newer GLib versions (>= 2.31.0) */
85 #define GLIB_DISABLE_DEPRECATION_WARNINGS
86
87 #ifdef HAVE_CONFIG_H
88 #include "config.h"
89 #endif
90
91 #include <gst/gst-i18n-plugin.h>
92
93 #include <string.h>
94 #include <gst/gst.h>
95 #include <gst/pbutils/pbutils.h>
96
97 #include "gstplay-enum.h"
98 #include "gstplayback.h"
99
100 /* Also used by gsturidecodebin.c */
101 gint _parse_bin_compare_factories_func (gconstpointer p1, gconstpointer p2);
102
103 /* generic templates */
104 static GstStaticPadTemplate decoder_bin_sink_template =
105 GST_STATIC_PAD_TEMPLATE ("sink",
106     GST_PAD_SINK,
107     GST_PAD_ALWAYS,
108     GST_STATIC_CAPS_ANY);
109
110 static GstStaticPadTemplate decoder_bin_src_template =
111 GST_STATIC_PAD_TEMPLATE ("src_%u",
112     GST_PAD_SRC,
113     GST_PAD_SOMETIMES,
114     GST_STATIC_CAPS_ANY);
115
116 GST_DEBUG_CATEGORY_STATIC (gst_parse_bin_debug);
117 #define GST_CAT_DEFAULT gst_parse_bin_debug
118
119 typedef struct _GstPendingPad GstPendingPad;
120 typedef struct _GstParseElement GstParseElement;
121 typedef struct _GstParseChain GstParseChain;
122 typedef struct _GstParseGroup GstParseGroup;
123 typedef struct _GstParsePad GstParsePad;
124 typedef GstGhostPadClass GstParsePadClass;
125 typedef struct _GstParseBin GstParseBin;
126 typedef struct _GstParseBinClass GstParseBinClass;
127
128 #define GST_TYPE_PARSE_BIN             (gst_parse_bin_get_type())
129 #define GST_PARSE_BIN_CAST(obj)        ((GstParseBin*)(obj))
130 #define GST_PARSE_BIN(obj)             (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_PARSE_BIN,GstParseBin))
131 #define GST_PARSE_BIN_CLASS(klass)     (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_PARSE_BIN,GstParseBinClass))
132 #define GST_IS_parse_bin(obj)          (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_PARSE_BIN))
133 #define GST_IS_parse_bin_CLASS(klass)  (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_PARSE_BIN))
134
135 /**
136  *  GstParseBin:
137  *
138  *  The opaque #GstParseBin data structure
139  */
140 struct _GstParseBin
141 {
142   GstBin bin;                   /* we extend GstBin */
143
144   /* properties */
145   gchar *encoding;              /* encoding of subtitles */
146   guint64 connection_speed;
147
148   GstElement *typefind;         /* this holds the typefind object */
149
150   GMutex expose_lock;           /* Protects exposal and removal of groups */
151   GstParseChain *parse_chain;   /* Top level parse chain */
152   guint nbpads;                 /* unique identifier for source pads */
153
154   GMutex factories_lock;
155   guint32 factories_cookie;     /* Cookie from last time when factories was updated */
156   GList *factories;             /* factories we can use for selecting elements */
157
158   GMutex subtitle_lock;         /* Protects changes to subtitles and encoding */
159   GList *subtitles;             /* List of elements with subtitle-encoding,
160                                  * protected by above mutex! */
161
162   gboolean have_type;           /* if we received the have_type signal */
163   guint have_type_id;           /* signal id for have-type from typefind */
164
165   gboolean async_pending;       /* async-start has been emitted */
166
167   GMutex dyn_lock;              /* lock protecting pad blocking */
168   gboolean shutdown;            /* if we are shutting down */
169   GList *blocked_pads;          /* pads that have set to block */
170
171   gboolean expose_allstreams;   /* Whether to expose unknow type streams or not */
172
173   GList *filtered;              /* elements for which error messages are filtered */
174   GList *filtered_errors;       /* filtered error messages */
175 };
176
177 struct _GstParseBinClass
178 {
179   GstBinClass parent_class;
180
181   /* signal fired when we found a pad that we cannot decode */
182   void (*unknown_type) (GstElement * element, GstPad * pad, GstCaps * caps);
183
184   /* signal fired to know if we continue trying to decode the given caps */
185     gboolean (*autoplug_continue) (GstElement * element, GstPad * pad,
186       GstCaps * caps);
187   /* signal fired to get a list of factories to try to autoplug */
188   GValueArray *(*autoplug_factories) (GstElement * element, GstPad * pad,
189       GstCaps * caps);
190   /* signal fired to sort the factories */
191   GValueArray *(*autoplug_sort) (GstElement * element, GstPad * pad,
192       GstCaps * caps, GValueArray * factories);
193   /* signal fired to select from the proposed list of factories */
194     GstAutoplugSelectResult (*autoplug_select) (GstElement * element,
195       GstPad * pad, GstCaps * caps, GstElementFactory * factory);
196   /* signal fired when a autoplugged element that is not linked downstream
197    * or exposed wants to query something */
198     gboolean (*autoplug_query) (GstElement * element, GstPad * pad,
199       GstQuery * query);
200
201   /* fired when the last group is drained */
202   void (*drained) (GstElement * element);
203 };
204
205 /* signals */
206 enum
207 {
208   SIGNAL_UNKNOWN_TYPE,
209   SIGNAL_AUTOPLUG_CONTINUE,
210   SIGNAL_AUTOPLUG_FACTORIES,
211   SIGNAL_AUTOPLUG_SELECT,
212   SIGNAL_AUTOPLUG_SORT,
213   SIGNAL_AUTOPLUG_QUERY,
214   SIGNAL_DRAINED,
215   LAST_SIGNAL
216 };
217
218 #define DEFAULT_SUBTITLE_ENCODING NULL
219 #define DEFAULT_USE_BUFFERING     FALSE
220 #define DEFAULT_LOW_PERCENT       10
221 #define DEFAULT_HIGH_PERCENT      99
222 /* by default we use the automatic values above */
223 #define DEFAULT_EXPOSE_ALL_STREAMS  TRUE
224 #define DEFAULT_CONNECTION_SPEED    0
225
226 /* Properties */
227 enum
228 {
229   PROP_0,
230   PROP_SUBTITLE_ENCODING,
231   PROP_SINK_CAPS,
232   PROP_EXPOSE_ALL_STREAMS,
233   PROP_CONNECTION_SPEED
234 };
235
236 static GstBinClass *parent_class;
237 static guint gst_parse_bin_signals[LAST_SIGNAL] = { 0 };
238
239 static void do_async_start (GstParseBin * parsebin);
240 static void do_async_done (GstParseBin * parsebin);
241
242 static void type_found (GstElement * typefind, guint probability,
243     GstCaps * caps, GstParseBin * parse_bin);
244
245 static gboolean gst_parse_bin_autoplug_continue (GstElement * element,
246     GstPad * pad, GstCaps * caps);
247 static GValueArray *gst_parse_bin_autoplug_factories (GstElement *
248     element, GstPad * pad, GstCaps * caps);
249 static GValueArray *gst_parse_bin_autoplug_sort (GstElement * element,
250     GstPad * pad, GstCaps * caps, GValueArray * factories);
251 static GstAutoplugSelectResult gst_parse_bin_autoplug_select (GstElement *
252     element, GstPad * pad, GstCaps * caps, GstElementFactory * factory);
253 static gboolean gst_parse_bin_autoplug_query (GstElement * element,
254     GstPad * pad, GstQuery * query);
255
256 static void gst_parse_bin_set_property (GObject * object, guint prop_id,
257     const GValue * value, GParamSpec * pspec);
258 static void gst_parse_bin_get_property (GObject * object, guint prop_id,
259     GValue * value, GParamSpec * pspec);
260 static void caps_notify_cb (GstPad * pad, GParamSpec * unused,
261     GstParseChain * chain);
262
263 static GstStateChangeReturn gst_parse_bin_change_state (GstElement * element,
264     GstStateChange transition);
265 static void gst_parse_bin_handle_message (GstBin * bin, GstMessage * message);
266 static void gst_parse_pad_update_caps (GstParsePad * parsepad, GstCaps * caps);
267 static void gst_parse_pad_update_tags (GstParsePad * parsepad,
268     GstTagList * tags);
269 static GstEvent *gst_parse_pad_stream_start_event (GstParsePad * parsepad,
270     GstEvent * event);
271 static void gst_parse_pad_update_stream_collection (GstParsePad * parsepad,
272     GstStreamCollection * collection);
273
274 static GstCaps *get_pad_caps (GstPad * pad);
275
276 #define EXPOSE_LOCK(parsebin) G_STMT_START {                            \
277     GST_LOG_OBJECT (parsebin,                                           \
278                     "expose locking from thread %p",                    \
279                     g_thread_self ());                                  \
280     g_mutex_lock (&GST_PARSE_BIN_CAST(parsebin)->expose_lock);          \
281     GST_LOG_OBJECT (parsebin,                                           \
282                     "expose locked from thread %p",                     \
283                     g_thread_self ());                                  \
284 } G_STMT_END
285
286 #define EXPOSE_UNLOCK(parsebin) G_STMT_START {                          \
287     GST_LOG_OBJECT (parsebin,                                           \
288                     "expose unlocking from thread %p",                  \
289                     g_thread_self ());                                  \
290     g_mutex_unlock (&GST_PARSE_BIN_CAST(parsebin)->expose_lock);                \
291 } G_STMT_END
292
293 #define DYN_LOCK(parsebin) G_STMT_START {                       \
294     GST_LOG_OBJECT (parsebin,                                           \
295                     "dynlocking from thread %p",                        \
296                     g_thread_self ());                                  \
297     g_mutex_lock (&GST_PARSE_BIN_CAST(parsebin)->dyn_lock);                     \
298     GST_LOG_OBJECT (parsebin,                                           \
299                     "dynlocked from thread %p",                         \
300                     g_thread_self ());                                  \
301 } G_STMT_END
302
303 #define DYN_UNLOCK(parsebin) G_STMT_START {                     \
304     GST_LOG_OBJECT (parsebin,                                           \
305                     "dynunlocking from thread %p",                      \
306                     g_thread_self ());                                  \
307     g_mutex_unlock (&GST_PARSE_BIN_CAST(parsebin)->dyn_lock);           \
308 } G_STMT_END
309
310 #define SUBTITLE_LOCK(parsebin) G_STMT_START {                          \
311     GST_LOG_OBJECT (parsebin,                                           \
312                     "subtitle locking from thread %p",                  \
313                     g_thread_self ());                                  \
314     g_mutex_lock (&GST_PARSE_BIN_CAST(parsebin)->subtitle_lock);                \
315     GST_LOG_OBJECT (parsebin,                                           \
316                     "subtitle lock from thread %p",                     \
317                     g_thread_self ());                                  \
318 } G_STMT_END
319
320 #define SUBTITLE_UNLOCK(parsebin) G_STMT_START {                                \
321     GST_LOG_OBJECT (parsebin,                                           \
322                     "subtitle unlocking from thread %p",                \
323                     g_thread_self ());                                  \
324     g_mutex_unlock (&GST_PARSE_BIN_CAST(parsebin)->subtitle_lock);              \
325 } G_STMT_END
326
327 struct _GstPendingPad
328 {
329   GstPad *pad;
330   GstParseChain *chain;
331   gulong event_probe_id;
332   gulong notify_caps_id;
333 };
334
335 struct _GstParseElement
336 {
337   GstElement *element;
338   GstElement *capsfilter;       /* Optional capsfilter for Parser/Convert */
339   gulong pad_added_id;
340   gulong pad_removed_id;
341   gulong no_more_pads_id;
342 };
343
344 /* GstParseGroup
345  *
346  * Streams belonging to the same group/chain of a media file
347  *
348  * When changing something here lock the parent chain!
349  */
350 struct _GstParseGroup
351 {
352   GstParseBin *parsebin;
353   GstParseChain *parent;
354
355   gboolean no_more_pads;        /* TRUE if the demuxer signaled no-more-pads */
356   gboolean drained;             /* TRUE if the all children are drained */
357
358   GList *children;              /* List of GstParseChains in this group */
359 };
360
361 struct _GstParseChain
362 {
363   GstParseGroup *parent;
364   GstParseBin *parsebin;
365
366   GMutex lock;                  /* Protects this chain and its groups */
367
368   GstPad *pad;                  /* srcpad that caused creation of this chain */
369   GstCaps *start_caps;          /* The initial caps of this chain */
370
371   gboolean drained;             /* TRUE if the all children are drained */
372   gboolean demuxer;             /* TRUE if elements->data is a demuxer */
373   gboolean parsed;              /* TRUE if any elements are a parser */
374   GList *elements;              /* All elements in this group, first
375                                    is the latest and most downstream element */
376
377   /* Note: there are only groups if the last element of this chain
378    * is a demuxer, otherwise the chain will end with an endpad.
379    * The other way around this means, that endpad only exists if this
380    * chain doesn't end with a demuxer! */
381
382   GstParseGroup *active_group;  /* Currently active group */
383   GList *next_groups;           /* head is newest group, tail is next group.
384                                    a new group will be created only if the head
385                                    group had no-more-pads. If it's only exposed
386                                    all new pads will be ignored! */
387   GList *pending_pads;          /* Pads that have no fixed caps yet */
388
389   GstParsePad *current_pad;     /* Current ending pad of the chain that can't
390                                  * be exposed yet but would be the same as endpad
391                                  * once it can be exposed */
392   GstParsePad *endpad;          /* Pad of this chain that could be exposed */
393   gboolean deadend;             /* This chain is incomplete and can't be completed,
394                                    e.g. no suitable decoder could be found
395                                    e.g. stream got EOS without buffers
396                                  */
397   gchar *deadend_details;
398   GstCaps *endcaps;             /* Caps that were used when linking to the endpad
399                                    or that resulted in the deadend
400                                  */
401
402   /* FIXME: This should be done directly via a thread! */
403   GList *old_groups;            /* Groups that should be freed later */
404 };
405
406 static void gst_parse_chain_free (GstParseChain * chain);
407 static GstParseChain *gst_parse_chain_new (GstParseBin * parsebin,
408     GstParseGroup * group, GstPad * pad, GstCaps * start_caps);
409 static void gst_parse_group_hide (GstParseGroup * group);
410 static void gst_parse_group_free (GstParseGroup * group);
411 static GstParseGroup *gst_parse_group_new (GstParseBin * parsebin,
412     GstParseChain * chain);
413 static gboolean gst_parse_chain_is_complete (GstParseChain * chain);
414 static gboolean gst_parse_chain_expose (GstParseChain * chain,
415     GList ** endpads, gboolean * missing_plugin,
416     GString * missing_plugin_details, gboolean * last_group,
417     gboolean * uncollected_streams);
418 static void build_fallback_collection (GstParseChain * chain,
419     GstStreamCollection * collection);
420 static gboolean gst_parse_chain_is_drained (GstParseChain * chain);
421 static gboolean gst_parse_group_is_complete (GstParseGroup * group);
422 static gboolean gst_parse_group_is_drained (GstParseGroup * group);
423
424 static gboolean gst_parse_bin_expose (GstParseBin * parsebin);
425
426 #define CHAIN_MUTEX_LOCK(chain) G_STMT_START {                          \
427     GST_LOG_OBJECT (chain->parsebin,                                    \
428                     "locking chain %p from thread %p",                  \
429                     chain, g_thread_self ());                           \
430     g_mutex_lock (&chain->lock);                                                \
431     GST_LOG_OBJECT (chain->parsebin,                                    \
432                     "locked chain %p from thread %p",                   \
433                     chain, g_thread_self ());                           \
434 } G_STMT_END
435
436 #define CHAIN_MUTEX_UNLOCK(chain) G_STMT_START {                        \
437     GST_LOG_OBJECT (chain->parsebin,                                    \
438                     "unlocking chain %p from thread %p",                \
439                     chain, g_thread_self ());                           \
440     g_mutex_unlock (&chain->lock);                                      \
441 } G_STMT_END
442
443 /* GstParsePad
444  *
445  * GstPad private used for source pads of chains
446  */
447 struct _GstParsePad
448 {
449   GstGhostPad parent;
450   GstParseBin *parsebin;
451   GstParseChain *chain;
452
453   gboolean blocked;             /* the *target* pad is blocked */
454   gboolean exposed;             /* the pad is exposed */
455   gboolean drained;             /* an EOS has been seen on the pad */
456
457   gulong block_id;
458
459   gboolean in_a_fallback_collection;
460   GstStreamCollection *active_collection;
461   GstStream *active_stream;
462 };
463
464 GType gst_parse_pad_get_type (void);
465 G_DEFINE_TYPE (GstParsePad, gst_parse_pad, GST_TYPE_GHOST_PAD);
466 #define GST_TYPE_PARSE_PAD (gst_parse_pad_get_type ())
467 #define GST_PARSE_PAD(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_PARSE_PAD,GstParsePad))
468
469 static GstParsePad *gst_parse_pad_new (GstParseBin * parsebin,
470     GstParseChain * chain);
471 static void gst_parse_pad_activate (GstParsePad * parsepad,
472     GstParseChain * chain);
473 static void gst_parse_pad_unblock (GstParsePad * parsepad);
474 static void gst_parse_pad_set_blocked (GstParsePad * parsepad,
475     gboolean blocked);
476 static gboolean gst_parse_pad_query (GstPad * pad, GstObject * parent,
477     GstQuery * query);
478 static GstPadProbeReturn
479 gst_parse_pad_event (GstPad * pad, GstPadProbeInfo * info, gpointer user_data);
480
481
482 static void gst_pending_pad_free (GstPendingPad * ppad);
483 static GstPadProbeReturn pad_event_cb (GstPad * pad, GstPadProbeInfo * info,
484     gpointer data);
485
486 /********************************
487  * Standard GObject boilerplate *
488  ********************************/
489
490 static void gst_parse_bin_class_init (GstParseBinClass * klass);
491 static void gst_parse_bin_init (GstParseBin * parse_bin);
492 static void gst_parse_bin_dispose (GObject * object);
493 static void gst_parse_bin_finalize (GObject * object);
494
495 static GType
496 gst_parse_bin_get_type (void)
497 {
498   static GType gst_parse_bin_type = 0;
499
500   if (!gst_parse_bin_type) {
501     static const GTypeInfo gst_parse_bin_info = {
502       sizeof (GstParseBinClass),
503       NULL,
504       NULL,
505       (GClassInitFunc) gst_parse_bin_class_init,
506       NULL,
507       NULL,
508       sizeof (GstParseBin),
509       0,
510       (GInstanceInitFunc) gst_parse_bin_init,
511       NULL
512     };
513
514     gst_parse_bin_type =
515         g_type_register_static (GST_TYPE_BIN, "GstParseBin",
516         &gst_parse_bin_info, 0);
517   }
518
519   return gst_parse_bin_type;
520 }
521
522 static gboolean
523 _gst_boolean_accumulator (GSignalInvocationHint * ihint,
524     GValue * return_accu, const GValue * handler_return, gpointer dummy)
525 {
526   gboolean myboolean;
527
528   myboolean = g_value_get_boolean (handler_return);
529   if (!(ihint->run_type & G_SIGNAL_RUN_CLEANUP))
530     g_value_set_boolean (return_accu, myboolean);
531
532   /* stop emission if FALSE */
533   return myboolean;
534 }
535
536 static gboolean
537 _gst_boolean_or_accumulator (GSignalInvocationHint * ihint,
538     GValue * return_accu, const GValue * handler_return, gpointer dummy)
539 {
540   gboolean myboolean;
541   gboolean retboolean;
542
543   myboolean = g_value_get_boolean (handler_return);
544   retboolean = g_value_get_boolean (return_accu);
545
546   if (!(ihint->run_type & G_SIGNAL_RUN_CLEANUP))
547     g_value_set_boolean (return_accu, myboolean || retboolean);
548
549   return TRUE;
550 }
551
552 /* we collect the first result */
553 static gboolean
554 _gst_array_accumulator (GSignalInvocationHint * ihint,
555     GValue * return_accu, const GValue * handler_return, gpointer dummy)
556 {
557   gpointer array;
558
559   array = g_value_get_boxed (handler_return);
560   if (!(ihint->run_type & G_SIGNAL_RUN_CLEANUP))
561     g_value_set_boxed (return_accu, array);
562
563   return FALSE;
564 }
565
566 static gboolean
567 _gst_select_accumulator (GSignalInvocationHint * ihint,
568     GValue * return_accu, const GValue * handler_return, gpointer dummy)
569 {
570   GstAutoplugSelectResult res;
571
572   res = g_value_get_enum (handler_return);
573   if (!(ihint->run_type & G_SIGNAL_RUN_CLEANUP))
574     g_value_set_enum (return_accu, res);
575
576   /* Call the next handler in the chain (if any) when the current callback
577    * returns TRY. This makes it possible to register separate autoplug-select
578    * handlers that implement different TRY/EXPOSE/SKIP strategies.
579    */
580   if (res == GST_AUTOPLUG_SELECT_TRY)
581     return TRUE;
582
583   return FALSE;
584 }
585
586 static gboolean
587 _gst_array_hasvalue_accumulator (GSignalInvocationHint * ihint,
588     GValue * return_accu, const GValue * handler_return, gpointer dummy)
589 {
590   gpointer array;
591
592   array = g_value_get_boxed (handler_return);
593   if (!(ihint->run_type & G_SIGNAL_RUN_CLEANUP))
594     g_value_set_boxed (return_accu, array);
595
596   if (array != NULL)
597     return FALSE;
598
599   return TRUE;
600 }
601
602 static void
603 gst_parse_bin_class_init (GstParseBinClass * klass)
604 {
605   GObjectClass *gobject_klass;
606   GstElementClass *gstelement_klass;
607   GstBinClass *gstbin_klass;
608
609   gobject_klass = (GObjectClass *) klass;
610   gstelement_klass = (GstElementClass *) klass;
611   gstbin_klass = (GstBinClass *) klass;
612
613   parent_class = g_type_class_peek_parent (klass);
614
615   gobject_klass->dispose = gst_parse_bin_dispose;
616   gobject_klass->finalize = gst_parse_bin_finalize;
617   gobject_klass->set_property = gst_parse_bin_set_property;
618   gobject_klass->get_property = gst_parse_bin_get_property;
619
620   /**
621    * GstParseBin::unknown-type:
622    * @bin: The ParseBin.
623    * @pad: The new pad containing caps that cannot be resolved to a 'final'
624    *       stream type.
625    * @caps: The #GstCaps of the pad that cannot be resolved.
626    *
627    * This signal is emitted when a pad for which there is no further possible
628    * decoding is added to the ParseBin.
629    */
630   gst_parse_bin_signals[SIGNAL_UNKNOWN_TYPE] =
631       g_signal_new ("unknown-type", G_TYPE_FROM_CLASS (klass),
632       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstParseBinClass, unknown_type),
633       NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 2,
634       GST_TYPE_PAD, GST_TYPE_CAPS);
635
636   /**
637    * GstParseBin::autoplug-continue:
638    * @bin: The ParseBin.
639    * @pad: The #GstPad.
640    * @caps: The #GstCaps found.
641    *
642    * This signal is emitted whenever ParseBin finds a new stream. It is
643    * emitted before looking for any elements that can handle that stream.
644    *
645    * <note>
646    *   Invocation of signal handlers stops after the first signal handler
647    *   returns #FALSE. Signal handlers are invoked in the order they were
648    *   connected in.
649    * </note>
650    *
651    * Returns: #TRUE if you wish ParseBin to look for elements that can
652    * handle the given @caps. If #FALSE, those caps will be considered as
653    * final and the pad will be exposed as such (see 'pad-added' signal of
654    * #GstElement).
655    */
656   gst_parse_bin_signals[SIGNAL_AUTOPLUG_CONTINUE] =
657       g_signal_new ("autoplug-continue", G_TYPE_FROM_CLASS (klass),
658       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstParseBinClass, autoplug_continue),
659       _gst_boolean_accumulator, NULL, g_cclosure_marshal_generic,
660       G_TYPE_BOOLEAN, 2, GST_TYPE_PAD, GST_TYPE_CAPS);
661
662   /**
663    * GstParseBin::autoplug-factories:
664    * @bin: The ParseBin.
665    * @pad: The #GstPad.
666    * @caps: The #GstCaps found.
667    *
668    * This function is emited when an array of possible factories for @caps on
669    * @pad is needed. ParseBin will by default return an array with all
670    * compatible factories, sorted by rank.
671    *
672    * If this function returns NULL, @pad will be exposed as a final caps.
673    *
674    * If this function returns an empty array, the pad will be considered as
675    * having an unhandled type media type.
676    *
677    * <note>
678    *   Only the signal handler that is connected first will ever by invoked.
679    *   Don't connect signal handlers with the #G_CONNECT_AFTER flag to this
680    *   signal, they will never be invoked!
681    * </note>
682    *
683    * Returns: a #GValueArray* with a list of factories to try. The factories are
684    * by default tried in the returned order or based on the index returned by
685    * "autoplug-select".
686    */
687   gst_parse_bin_signals[SIGNAL_AUTOPLUG_FACTORIES] =
688       g_signal_new ("autoplug-factories", G_TYPE_FROM_CLASS (klass),
689       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstParseBinClass,
690           autoplug_factories), _gst_array_accumulator, NULL,
691       g_cclosure_marshal_generic, G_TYPE_VALUE_ARRAY, 2,
692       GST_TYPE_PAD, GST_TYPE_CAPS);
693
694   /**
695    * GstParseBin::autoplug-sort:
696    * @bin: The ParseBin.
697    * @pad: The #GstPad.
698    * @caps: The #GstCaps.
699    * @factories: A #GValueArray of possible #GstElementFactory to use.
700    *
701    * Once ParseBin has found the possible #GstElementFactory objects to try
702    * for @caps on @pad, this signal is emited. The purpose of the signal is for
703    * the application to perform additional sorting or filtering on the element
704    * factory array.
705    *
706    * The callee should copy and modify @factories or return #NULL if the
707    * order should not change.
708    *
709    * <note>
710    *   Invocation of signal handlers stops after one signal handler has
711    *   returned something else than #NULL. Signal handlers are invoked in
712    *   the order they were connected in.
713    *   Don't connect signal handlers with the #G_CONNECT_AFTER flag to this
714    *   signal, they will never be invoked!
715    * </note>
716    *
717    * Returns: A new sorted array of #GstElementFactory objects.
718    */
719   gst_parse_bin_signals[SIGNAL_AUTOPLUG_SORT] =
720       g_signal_new ("autoplug-sort", G_TYPE_FROM_CLASS (klass),
721       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstParseBinClass, autoplug_sort),
722       _gst_array_hasvalue_accumulator, NULL,
723       g_cclosure_marshal_generic, G_TYPE_VALUE_ARRAY, 3, GST_TYPE_PAD,
724       GST_TYPE_CAPS, G_TYPE_VALUE_ARRAY | G_SIGNAL_TYPE_STATIC_SCOPE);
725
726   /**
727    * GstParseBin::autoplug-select:
728    * @bin: The ParseBin.
729    * @pad: The #GstPad.
730    * @caps: The #GstCaps.
731    * @factory: A #GstElementFactory to use.
732    *
733    * This signal is emitted once ParseBin has found all the possible
734    * #GstElementFactory that can be used to handle the given @caps. For each of
735    * those factories, this signal is emitted.
736    *
737    * The signal handler should return a #GST_TYPE_AUTOPLUG_SELECT_RESULT enum
738    * value indicating what ParseBin should do next.
739    *
740    * A value of #GST_AUTOPLUG_SELECT_TRY will try to autoplug an element from
741    * @factory.
742    *
743    * A value of #GST_AUTOPLUG_SELECT_EXPOSE will expose @pad without plugging
744    * any element to it.
745    *
746    * A value of #GST_AUTOPLUG_SELECT_SKIP will skip @factory and move to the
747    * next factory.
748    *
749    * <note>
750    *   The signal handler will not be invoked if any of the previously
751    *   registered signal handlers (if any) return a value other than
752    *   GST_AUTOPLUG_SELECT_TRY. Which also means that if you return
753    *   GST_AUTOPLUG_SELECT_TRY from one signal handler, handlers that get
754    *   registered next (again, if any) can override that decision.
755    * </note>
756    *
757    * Returns: a #GST_TYPE_AUTOPLUG_SELECT_RESULT that indicates the required
758    * operation. the default handler will always return
759    * #GST_AUTOPLUG_SELECT_TRY.
760    */
761   gst_parse_bin_signals[SIGNAL_AUTOPLUG_SELECT] =
762       g_signal_new ("autoplug-select", G_TYPE_FROM_CLASS (klass),
763       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstParseBinClass, autoplug_select),
764       _gst_select_accumulator, NULL,
765       g_cclosure_marshal_generic,
766       GST_TYPE_AUTOPLUG_SELECT_RESULT, 3, GST_TYPE_PAD, GST_TYPE_CAPS,
767       GST_TYPE_ELEMENT_FACTORY);
768
769   /**
770    * GstParseBin::autoplug-query:
771    * @bin: The ParseBin.
772    * @child: The child element doing the query
773    * @pad: The #GstPad.
774    * @element: The #GstElement.
775    * @query: The #GstQuery.
776    *
777    * This signal is emitted whenever an autoplugged element that is
778    * not linked downstream yet and not exposed does a query. It can
779    * be used to tell the element about the downstream supported caps
780    * for example.
781    *
782    * Returns: #TRUE if the query was handled, #FALSE otherwise.
783    */
784   gst_parse_bin_signals[SIGNAL_AUTOPLUG_QUERY] =
785       g_signal_new ("autoplug-query", G_TYPE_FROM_CLASS (klass),
786       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstParseBinClass, autoplug_query),
787       _gst_boolean_or_accumulator, NULL, g_cclosure_marshal_generic,
788       G_TYPE_BOOLEAN, 3, GST_TYPE_PAD, GST_TYPE_ELEMENT,
789       GST_TYPE_QUERY | G_SIGNAL_TYPE_STATIC_SCOPE);
790
791   /**
792    * GstParseBin::drained
793    * @bin: The ParseBin
794    *
795    * This signal is emitted once ParseBin has finished decoding all the data.
796    */
797   gst_parse_bin_signals[SIGNAL_DRAINED] =
798       g_signal_new ("drained", G_TYPE_FROM_CLASS (klass),
799       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstParseBinClass, drained),
800       NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 0, G_TYPE_NONE);
801
802   g_object_class_install_property (gobject_klass, PROP_SUBTITLE_ENCODING,
803       g_param_spec_string ("subtitle-encoding", "subtitle encoding",
804           "Encoding to assume if input subtitles are not in UTF-8 encoding. "
805           "If not set, the GST_SUBTITLE_ENCODING environment variable will "
806           "be checked for an encoding to use. If that is not set either, "
807           "ISO-8859-15 will be assumed.", NULL,
808           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
809
810   g_object_class_install_property (gobject_klass, PROP_SINK_CAPS,
811       g_param_spec_boxed ("sink-caps", "Sink Caps",
812           "The caps of the input data. (NULL = use typefind element)",
813           GST_TYPE_CAPS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
814
815   /**
816    * GstParseBin::expose-all-streams
817    *
818    * Expose streams of unknown type.
819    *
820    * If set to %FALSE, then only the streams that can be decoded to the final
821    * caps (see 'caps' property) will have a pad exposed. Streams that do not
822    * match those caps but could have been decoded will not have decoder plugged
823    * in internally and will not have a pad exposed.
824    */
825   g_object_class_install_property (gobject_klass, PROP_EXPOSE_ALL_STREAMS,
826       g_param_spec_boolean ("expose-all-streams", "Expose All Streams",
827           "Expose all streams, including those of unknown type or that don't match the 'caps' property",
828           DEFAULT_EXPOSE_ALL_STREAMS,
829           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
830
831   /**
832    * GstParseBin2::connection-speed
833    *
834    * Network connection speed in kbps (0 = unknownw)
835    */
836   g_object_class_install_property (gobject_klass, PROP_CONNECTION_SPEED,
837       g_param_spec_uint64 ("connection-speed", "Connection Speed",
838           "Network connection speed in kbps (0 = unknown)",
839           0, G_MAXUINT64 / 1000, DEFAULT_CONNECTION_SPEED,
840           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
841
842
843
844   klass->autoplug_continue =
845       GST_DEBUG_FUNCPTR (gst_parse_bin_autoplug_continue);
846   klass->autoplug_factories =
847       GST_DEBUG_FUNCPTR (gst_parse_bin_autoplug_factories);
848   klass->autoplug_sort = GST_DEBUG_FUNCPTR (gst_parse_bin_autoplug_sort);
849   klass->autoplug_select = GST_DEBUG_FUNCPTR (gst_parse_bin_autoplug_select);
850   klass->autoplug_query = GST_DEBUG_FUNCPTR (gst_parse_bin_autoplug_query);
851
852   gst_element_class_add_pad_template (gstelement_klass,
853       gst_static_pad_template_get (&decoder_bin_sink_template));
854   gst_element_class_add_pad_template (gstelement_klass,
855       gst_static_pad_template_get (&decoder_bin_src_template));
856
857   gst_element_class_set_static_metadata (gstelement_klass,
858       "Parse Bin", "Generic/Bin/Parser",
859       "Parse and de-multiplex to elementary stream",
860       "Jan Schmidt <jan@centricular.com>, "
861       "Edward Hervey <edward@centricular.com>");
862
863   gstelement_klass->change_state =
864       GST_DEBUG_FUNCPTR (gst_parse_bin_change_state);
865
866   gstbin_klass->handle_message =
867       GST_DEBUG_FUNCPTR (gst_parse_bin_handle_message);
868
869   g_type_class_ref (GST_TYPE_PARSE_PAD);
870 }
871
872 gint
873 _parse_bin_compare_factories_func (gconstpointer p1, gconstpointer p2)
874 {
875   GstPluginFeature *f1, *f2;
876   gboolean is_parser1, is_parser2;
877
878   f1 = (GstPluginFeature *) p1;
879   f2 = (GstPluginFeature *) p2;
880
881   is_parser1 = gst_element_factory_list_is_type (GST_ELEMENT_FACTORY_CAST (f1),
882       GST_ELEMENT_FACTORY_TYPE_PARSER);
883   is_parser2 = gst_element_factory_list_is_type (GST_ELEMENT_FACTORY_CAST (f2),
884       GST_ELEMENT_FACTORY_TYPE_PARSER);
885
886
887   /* We want all parsers first as we always want to plug parsers
888    * before decoders */
889   if (is_parser1 && !is_parser2)
890     return -1;
891   else if (!is_parser1 && is_parser2)
892     return 1;
893
894   /* And if it's a both a parser we first sort by rank
895    * and then by factory name */
896   return gst_plugin_feature_rank_compare_func (p1, p2);
897 }
898
899 /* Must be called with factories lock! */
900 static void
901 gst_parse_bin_update_factories_list (GstParseBin * parsebin)
902 {
903   guint cookie;
904
905   cookie = gst_registry_get_feature_list_cookie (gst_registry_get ());
906   if (!parsebin->factories || parsebin->factories_cookie != cookie) {
907     if (parsebin->factories)
908       gst_plugin_feature_list_free (parsebin->factories);
909     parsebin->factories =
910         gst_element_factory_list_get_elements
911         (GST_ELEMENT_FACTORY_TYPE_DECODABLE, GST_RANK_MARGINAL);
912     parsebin->factories =
913         g_list_sort (parsebin->factories, _parse_bin_compare_factories_func);
914     parsebin->factories_cookie = cookie;
915   }
916 }
917
918 static void
919 gst_parse_bin_init (GstParseBin * parse_bin)
920 {
921   /* first filter out the interesting element factories */
922   g_mutex_init (&parse_bin->factories_lock);
923
924   /* we create the typefind element only once */
925   parse_bin->typefind = gst_element_factory_make ("typefind", "typefind");
926   if (!parse_bin->typefind) {
927     g_warning ("can't find typefind element, ParseBin will not work");
928   } else {
929     GstPad *pad;
930     GstPad *gpad;
931     GstPadTemplate *pad_tmpl;
932
933     /* add the typefind element */
934     if (!gst_bin_add (GST_BIN (parse_bin), parse_bin->typefind)) {
935       g_warning ("Could not add typefind element, ParseBin will not work");
936       gst_object_unref (parse_bin->typefind);
937       parse_bin->typefind = NULL;
938     }
939
940     /* get the sinkpad */
941     pad = gst_element_get_static_pad (parse_bin->typefind, "sink");
942
943     /* get the pad template */
944     pad_tmpl = gst_static_pad_template_get (&decoder_bin_sink_template);
945
946     /* ghost the sink pad to ourself */
947     gpad = gst_ghost_pad_new_from_template ("sink", pad, pad_tmpl);
948     gst_pad_set_active (gpad, TRUE);
949     gst_element_add_pad (GST_ELEMENT (parse_bin), gpad);
950
951     gst_object_unref (pad_tmpl);
952     gst_object_unref (pad);
953   }
954
955   g_mutex_init (&parse_bin->expose_lock);
956   parse_bin->parse_chain = NULL;
957
958   g_mutex_init (&parse_bin->dyn_lock);
959   parse_bin->shutdown = FALSE;
960   parse_bin->blocked_pads = NULL;
961
962   g_mutex_init (&parse_bin->subtitle_lock);
963
964   parse_bin->encoding = g_strdup (DEFAULT_SUBTITLE_ENCODING);
965
966   parse_bin->expose_allstreams = DEFAULT_EXPOSE_ALL_STREAMS;
967   parse_bin->connection_speed = DEFAULT_CONNECTION_SPEED;
968 }
969
970 static void
971 gst_parse_bin_dispose (GObject * object)
972 {
973   GstParseBin *parse_bin;
974
975   parse_bin = GST_PARSE_BIN (object);
976
977   if (parse_bin->factories)
978     gst_plugin_feature_list_free (parse_bin->factories);
979   parse_bin->factories = NULL;
980
981   if (parse_bin->parse_chain)
982     gst_parse_chain_free (parse_bin->parse_chain);
983   parse_bin->parse_chain = NULL;
984
985   g_free (parse_bin->encoding);
986   parse_bin->encoding = NULL;
987
988   g_list_free (parse_bin->subtitles);
989   parse_bin->subtitles = NULL;
990
991   G_OBJECT_CLASS (parent_class)->dispose (object);
992 }
993
994 static void
995 gst_parse_bin_finalize (GObject * object)
996 {
997   GstParseBin *parse_bin;
998
999   parse_bin = GST_PARSE_BIN (object);
1000
1001   g_mutex_clear (&parse_bin->expose_lock);
1002   g_mutex_clear (&parse_bin->dyn_lock);
1003   g_mutex_clear (&parse_bin->subtitle_lock);
1004   g_mutex_clear (&parse_bin->factories_lock);
1005
1006   G_OBJECT_CLASS (parent_class)->finalize (object);
1007 }
1008
1009 static void
1010 gst_parse_bin_set_sink_caps (GstParseBin * parsebin, GstCaps * caps)
1011 {
1012   GST_DEBUG_OBJECT (parsebin, "Setting new caps: %" GST_PTR_FORMAT, caps);
1013
1014   g_object_set (parsebin->typefind, "force-caps", caps, NULL);
1015 }
1016
1017 static GstCaps *
1018 gst_parse_bin_get_sink_caps (GstParseBin * parsebin)
1019 {
1020   GstCaps *caps;
1021
1022   GST_DEBUG_OBJECT (parsebin, "Getting currently set caps");
1023
1024   g_object_get (parsebin->typefind, "force-caps", &caps, NULL);
1025
1026   return caps;
1027 }
1028
1029 static void
1030 gst_parse_bin_set_subs_encoding (GstParseBin * parsebin, const gchar * encoding)
1031 {
1032   GList *walk;
1033
1034   GST_DEBUG_OBJECT (parsebin, "Setting new encoding: %s",
1035       GST_STR_NULL (encoding));
1036
1037   SUBTITLE_LOCK (parsebin);
1038   g_free (parsebin->encoding);
1039   parsebin->encoding = g_strdup (encoding);
1040
1041   /* set the subtitle encoding on all added elements */
1042   for (walk = parsebin->subtitles; walk; walk = g_list_next (walk)) {
1043     g_object_set (G_OBJECT (walk->data), "subtitle-encoding",
1044         parsebin->encoding, NULL);
1045   }
1046   SUBTITLE_UNLOCK (parsebin);
1047 }
1048
1049 static gchar *
1050 gst_parse_bin_get_subs_encoding (GstParseBin * parsebin)
1051 {
1052   gchar *encoding;
1053
1054   GST_DEBUG_OBJECT (parsebin, "Getting currently set encoding");
1055
1056   SUBTITLE_LOCK (parsebin);
1057   encoding = g_strdup (parsebin->encoding);
1058   SUBTITLE_UNLOCK (parsebin);
1059
1060   return encoding;
1061 }
1062
1063 static void
1064 gst_parse_bin_set_property (GObject * object, guint prop_id,
1065     const GValue * value, GParamSpec * pspec)
1066 {
1067   GstParseBin *parsebin;
1068
1069   parsebin = GST_PARSE_BIN (object);
1070
1071   switch (prop_id) {
1072     case PROP_SUBTITLE_ENCODING:
1073       gst_parse_bin_set_subs_encoding (parsebin, g_value_get_string (value));
1074       break;
1075     case PROP_SINK_CAPS:
1076       gst_parse_bin_set_sink_caps (parsebin, g_value_get_boxed (value));
1077       break;
1078     case PROP_EXPOSE_ALL_STREAMS:
1079       parsebin->expose_allstreams = g_value_get_boolean (value);
1080       break;
1081     case PROP_CONNECTION_SPEED:
1082       GST_OBJECT_LOCK (parsebin);
1083       parsebin->connection_speed = g_value_get_uint64 (value) * 1000;
1084       GST_OBJECT_UNLOCK (parsebin);
1085       break;
1086     default:
1087       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1088       break;
1089   }
1090 }
1091
1092 static void
1093 gst_parse_bin_get_property (GObject * object, guint prop_id,
1094     GValue * value, GParamSpec * pspec)
1095 {
1096   GstParseBin *parsebin;
1097
1098   parsebin = GST_PARSE_BIN (object);
1099   switch (prop_id) {
1100     case PROP_SUBTITLE_ENCODING:
1101       g_value_take_string (value, gst_parse_bin_get_subs_encoding (parsebin));
1102       break;
1103     case PROP_SINK_CAPS:
1104       g_value_take_boxed (value, gst_parse_bin_get_sink_caps (parsebin));
1105       break;
1106     case PROP_EXPOSE_ALL_STREAMS:
1107       g_value_set_boolean (value, parsebin->expose_allstreams);
1108       break;
1109     case PROP_CONNECTION_SPEED:
1110       GST_OBJECT_LOCK (parsebin);
1111       g_value_set_uint64 (value, parsebin->connection_speed / 1000);
1112       GST_OBJECT_UNLOCK (parsebin);
1113       break;
1114     default:
1115       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1116       break;
1117   }
1118 }
1119
1120
1121 /*****
1122  * Default autoplug signal handlers
1123  *****/
1124 static gboolean
1125 gst_parse_bin_autoplug_continue (GstElement * element, GstPad * pad,
1126     GstCaps * caps)
1127 {
1128   GST_DEBUG_OBJECT (element, "autoplug-continue returns TRUE");
1129
1130   /* by default we always continue */
1131   return TRUE;
1132 }
1133
1134 static GValueArray *
1135 gst_parse_bin_autoplug_factories (GstElement * element, GstPad * pad,
1136     GstCaps * caps)
1137 {
1138   GList *list, *tmp;
1139   GValueArray *result;
1140   GstParseBin *parsebin = GST_PARSE_BIN_CAST (element);
1141
1142   GST_DEBUG_OBJECT (element, "finding factories");
1143
1144   /* return all compatible factories for caps */
1145   g_mutex_lock (&parsebin->factories_lock);
1146   gst_parse_bin_update_factories_list (parsebin);
1147   list =
1148       gst_element_factory_list_filter (parsebin->factories, caps, GST_PAD_SINK,
1149       gst_caps_is_fixed (caps));
1150   g_mutex_unlock (&parsebin->factories_lock);
1151
1152   result = g_value_array_new (g_list_length (list));
1153   for (tmp = list; tmp; tmp = tmp->next) {
1154     GstElementFactory *factory = GST_ELEMENT_FACTORY_CAST (tmp->data);
1155     GValue val = { 0, };
1156
1157     g_value_init (&val, G_TYPE_OBJECT);
1158     g_value_set_object (&val, factory);
1159     g_value_array_append (result, &val);
1160     g_value_unset (&val);
1161   }
1162   gst_plugin_feature_list_free (list);
1163
1164   GST_DEBUG_OBJECT (element, "autoplug-factories returns %p", result);
1165
1166   return result;
1167 }
1168
1169 static GValueArray *
1170 gst_parse_bin_autoplug_sort (GstElement * element, GstPad * pad,
1171     GstCaps * caps, GValueArray * factories)
1172 {
1173   return NULL;
1174 }
1175
1176 static GstAutoplugSelectResult
1177 gst_parse_bin_autoplug_select (GstElement * element, GstPad * pad,
1178     GstCaps * caps, GstElementFactory * factory)
1179 {
1180   /* Try factory. */
1181   return GST_AUTOPLUG_SELECT_TRY;
1182 }
1183
1184 static gboolean
1185 gst_parse_bin_autoplug_query (GstElement * element, GstPad * pad,
1186     GstQuery * query)
1187 {
1188   /* No query handled here */
1189   return FALSE;
1190 }
1191
1192 /********
1193  * Discovery methods
1194  *****/
1195
1196 static gboolean is_demuxer_element (GstElement * srcelement);
1197
1198 static gboolean connect_pad (GstParseBin * parsebin, GstElement * src,
1199     GstParsePad * parsepad, GstPad * pad, GstCaps * caps,
1200     GValueArray * factories, GstParseChain * chain, gchar ** deadend_details);
1201 static GList *connect_element (GstParseBin * parsebin, GstParseElement * delem,
1202     GstParseChain * chain);
1203 static void expose_pad (GstParseBin * parsebin, GstElement * src,
1204     GstParsePad * parsepad, GstPad * pad, GstCaps * caps,
1205     GstParseChain * chain);
1206
1207 static void pad_added_cb (GstElement * element, GstPad * pad,
1208     GstParseChain * chain);
1209 static void pad_removed_cb (GstElement * element, GstPad * pad,
1210     GstParseChain * chain);
1211 static void no_more_pads_cb (GstElement * element, GstParseChain * chain);
1212
1213 static GstParseGroup *gst_parse_chain_get_current_group (GstParseChain * chain);
1214
1215 static gboolean
1216 clear_sticky_events (GstPad * pad, GstEvent ** event, gpointer user_data)
1217 {
1218   GST_DEBUG_OBJECT (pad, "clearing sticky event %" GST_PTR_FORMAT, *event);
1219   gst_event_unref (*event);
1220   *event = NULL;
1221   return TRUE;
1222 }
1223
1224 static gboolean
1225 copy_sticky_events (GstPad * pad, GstEvent ** eventptr, gpointer user_data)
1226 {
1227   GstParsePad *ppad = GST_PARSE_PAD (user_data);
1228   GstEvent *event = gst_event_ref (*eventptr);
1229
1230   switch (GST_EVENT_TYPE (event)) {
1231     case GST_EVENT_CAPS:{
1232       GstCaps *caps = NULL;
1233       gst_event_parse_caps (event, &caps);
1234       gst_parse_pad_update_caps (ppad, caps);
1235       break;
1236     }
1237     case GST_EVENT_STREAM_START:{
1238       event = gst_parse_pad_stream_start_event (ppad, event);
1239       break;
1240     }
1241     case GST_EVENT_STREAM_COLLECTION:{
1242       GstStreamCollection *collection = NULL;
1243       gst_event_parse_stream_collection (event, &collection);
1244       gst_parse_pad_update_stream_collection (ppad, collection);
1245       break;
1246     }
1247     default:
1248       break;
1249   }
1250
1251   GST_DEBUG_OBJECT (ppad, "store sticky event %" GST_PTR_FORMAT, event);
1252   gst_pad_store_sticky_event (GST_PAD_CAST (ppad), event);
1253   gst_event_unref (event);
1254
1255   return TRUE;
1256 }
1257
1258 static void
1259 parse_pad_set_target (GstParsePad * parsepad, GstPad * target)
1260 {
1261   GstPad *old_target = gst_ghost_pad_get_target (GST_GHOST_PAD_CAST (parsepad));
1262   if (old_target)
1263     gst_object_unref (old_target);
1264
1265   if (old_target == target)
1266     return;
1267
1268   gst_pad_sticky_events_foreach (GST_PAD_CAST (parsepad),
1269       clear_sticky_events, NULL);
1270   gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (parsepad), target);
1271
1272   if (target == NULL) {
1273     GST_LOG_OBJECT (parsepad->parsebin, "Setting pad %" GST_PTR_FORMAT
1274         " target to NULL", parsepad);
1275   } else {
1276     GST_LOG_OBJECT (parsepad->parsebin, "Setting pad %" GST_PTR_FORMAT
1277         " target to %" GST_PTR_FORMAT, parsepad, target);
1278     gst_pad_sticky_events_foreach (target, copy_sticky_events, parsepad);
1279   }
1280 }
1281
1282 /* called when a new pad is discovered. It will perform some basic actions
1283  * before trying to link something to it.
1284  *
1285  *  - Check the caps, don't do anything when there are no caps or when they have
1286  *    no good type.
1287  *  - signal AUTOPLUG_CONTINUE to check if we need to continue autoplugging this
1288  *    pad.
1289  *  - if the caps are non-fixed, setup a handler to continue autoplugging when
1290  *    the caps become fixed (connect to notify::caps).
1291  *  - get list of factories to autoplug.
1292  *  - continue autoplugging to one of the factories.
1293  */
1294 static void
1295 analyze_new_pad (GstParseBin * parsebin, GstElement * src, GstPad * pad,
1296     GstCaps * caps, GstParseChain * chain)
1297 {
1298   gboolean apcontinue = TRUE;
1299   GValueArray *factories = NULL, *result = NULL;
1300   GstParsePad *parsepad;
1301   GstElementFactory *factory;
1302   const gchar *classification;
1303   gboolean is_parser_converter = FALSE;
1304   gboolean res;
1305   gchar *deadend_details = NULL;
1306
1307   GST_DEBUG_OBJECT (parsebin, "Pad %s:%s caps:%" GST_PTR_FORMAT,
1308       GST_DEBUG_PAD_NAME (pad), caps);
1309
1310   if (chain->elements
1311       && src != ((GstParseElement *) chain->elements->data)->element
1312       && src != ((GstParseElement *) chain->elements->data)->capsfilter) {
1313     GST_ERROR_OBJECT (parsebin,
1314         "New pad from not the last element in this chain");
1315     return;
1316   }
1317
1318   if (chain->demuxer) {
1319     GstParseGroup *group;
1320     GstParseChain *oldchain = chain;
1321     GstParseElement *demux = (chain->elements ? chain->elements->data : NULL);
1322
1323     if (chain->current_pad)
1324       gst_object_unref (chain->current_pad);
1325     chain->current_pad = NULL;
1326
1327     /* we are adding a new pad for a demuxer (see is_demuxer_element(),
1328      * start a new chain for it */
1329     CHAIN_MUTEX_LOCK (oldchain);
1330     group = gst_parse_chain_get_current_group (chain);
1331     if (group && !g_list_find (group->children, chain)) {
1332       chain = gst_parse_chain_new (parsebin, group, pad, caps);
1333       group->children = g_list_prepend (group->children, chain);
1334     }
1335     CHAIN_MUTEX_UNLOCK (oldchain);
1336     if (!group) {
1337       GST_WARNING_OBJECT (parsebin, "No current group");
1338       return;
1339     }
1340
1341     /* If this is not a dynamic pad demuxer, we're no-more-pads
1342      * already before anything else happens
1343      */
1344     if (demux == NULL || !demux->no_more_pads_id)
1345       group->no_more_pads = TRUE;
1346   }
1347
1348   /* From here on we own a reference to the caps as
1349    * we might create new caps below and would need
1350    * to unref them later */
1351   if (caps)
1352     gst_caps_ref (caps);
1353
1354   if ((caps == NULL) || gst_caps_is_empty (caps))
1355     goto unknown_type;
1356
1357   if (gst_caps_is_any (caps))
1358     goto any_caps;
1359
1360   if (!chain->current_pad)
1361     chain->current_pad = gst_parse_pad_new (parsebin, chain);
1362
1363   parsepad = gst_object_ref (chain->current_pad);
1364   gst_pad_set_active (GST_PAD_CAST (parsepad), TRUE);
1365   parse_pad_set_target (parsepad, pad);
1366
1367   /* 1. Emit 'autoplug-continue' the result will tell us if this pads needs
1368    * further autoplugging. Only do this for fixed caps, for unfixed caps
1369    * we will later come here again from the notify::caps handler. The
1370    * problem with unfixed caps is that, we can't reliably tell if the output
1371    * is e.g. accepted by a sink because only parts of the possible final
1372    * caps might be accepted by the sink. */
1373   if (gst_caps_is_fixed (caps))
1374     g_signal_emit (G_OBJECT (parsebin),
1375         gst_parse_bin_signals[SIGNAL_AUTOPLUG_CONTINUE], 0, parsepad, caps,
1376         &apcontinue);
1377   else
1378     apcontinue = TRUE;
1379
1380   /* 1.a if autoplug-continue is FALSE or caps is a raw format, goto pad_is_final */
1381   if (!apcontinue)
1382     goto expose_pad;
1383
1384   /* 1.b For Parser/Converter that can output different stream formats
1385    * we insert a capsfilter with the sorted caps of all possible next
1386    * elements and continue with the capsfilter srcpad */
1387   factory = gst_element_get_factory (src);
1388   classification =
1389       gst_element_factory_get_metadata (factory, GST_ELEMENT_METADATA_KLASS);
1390   is_parser_converter = (strstr (classification, "Parser")
1391       && strstr (classification, "Converter"));
1392
1393   /* FIXME: We just need to be sure that the next element is not a parser */
1394   /* 1.c when the caps are not fixed yet, we can't be sure what element to
1395    * connect. We delay autoplugging until the caps are fixed */
1396   if (!is_parser_converter && !gst_caps_is_fixed (caps)) {
1397     goto non_fixed;
1398   } else if (!is_parser_converter) {
1399     gst_caps_unref (caps);
1400     caps = gst_pad_get_current_caps (pad);
1401     if (!caps) {
1402       GST_DEBUG_OBJECT (parsebin,
1403           "No final caps set yet, delaying autoplugging");
1404       gst_object_unref (parsepad);
1405       goto setup_caps_delay;
1406     }
1407   }
1408
1409   /* 1.d else get the factories and if there's no compatible factory goto
1410    * unknown_type */
1411   g_signal_emit (G_OBJECT (parsebin),
1412       gst_parse_bin_signals[SIGNAL_AUTOPLUG_FACTORIES], 0, parsepad, caps,
1413       &factories);
1414
1415   /* NULL means that we can expose the pad */
1416   if (factories == NULL)
1417     goto expose_pad;
1418
1419   /* if the array is empty, we have a type for which we have no decoder */
1420   if (factories->n_values == 0) {
1421     /* if not we have a unhandled type with no compatible factories */
1422     g_value_array_free (factories);
1423     gst_object_unref (parsepad);
1424     goto unknown_type;
1425   }
1426
1427   /* 1.e sort some more. */
1428   g_signal_emit (G_OBJECT (parsebin),
1429       gst_parse_bin_signals[SIGNAL_AUTOPLUG_SORT], 0, parsepad, caps, factories,
1430       &result);
1431   if (result) {
1432     g_value_array_free (factories);
1433     factories = result;
1434   }
1435
1436   /* 1.g now get the factory template caps and insert the capsfilter if this
1437    * is a parser/converter
1438    */
1439   if (is_parser_converter) {
1440     GstCaps *filter_caps;
1441     gint i;
1442     GstPad *p;
1443     GstParseElement *delem;
1444
1445     g_assert (chain->elements != NULL);
1446     delem = (GstParseElement *) chain->elements->data;
1447
1448     filter_caps = gst_caps_new_empty ();
1449     for (i = 0; i < factories->n_values; i++) {
1450       GstElementFactory *factory =
1451           g_value_get_object (g_value_array_get_nth (factories, i));
1452       GstCaps *tcaps, *intersection;
1453       const GList *tmps;
1454
1455       GST_DEBUG ("Trying factory %s",
1456           gst_plugin_feature_get_name (GST_PLUGIN_FEATURE (factory)));
1457
1458       if (gst_element_get_factory (src) == factory ||
1459           gst_element_factory_list_is_type (factory,
1460               GST_ELEMENT_FACTORY_TYPE_PARSER)) {
1461         GST_DEBUG ("Skipping factory");
1462         continue;
1463       }
1464
1465       for (tmps = gst_element_factory_get_static_pad_templates (factory); tmps;
1466           tmps = tmps->next) {
1467         GstStaticPadTemplate *st = (GstStaticPadTemplate *) tmps->data;
1468         if (st->direction != GST_PAD_SINK || st->presence != GST_PAD_ALWAYS)
1469           continue;
1470         tcaps = gst_static_pad_template_get_caps (st);
1471         intersection =
1472             gst_caps_intersect_full (tcaps, caps, GST_CAPS_INTERSECT_FIRST);
1473         filter_caps = gst_caps_merge (filter_caps, intersection);
1474         gst_caps_unref (tcaps);
1475       }
1476     }
1477
1478     /* Append the parser caps to prevent any not-negotiated errors */
1479     filter_caps = gst_caps_merge (filter_caps, gst_caps_ref (caps));
1480
1481     delem->capsfilter = gst_element_factory_make ("capsfilter", NULL);
1482     g_object_set (G_OBJECT (delem->capsfilter), "caps", filter_caps, NULL);
1483     gst_caps_unref (filter_caps);
1484     gst_element_set_state (delem->capsfilter, GST_STATE_PAUSED);
1485     gst_bin_add (GST_BIN_CAST (parsebin), gst_object_ref (delem->capsfilter));
1486
1487     parse_pad_set_target (parsepad, NULL);
1488     p = gst_element_get_static_pad (delem->capsfilter, "sink");
1489     gst_pad_link_full (pad, p, GST_PAD_LINK_CHECK_NOTHING);
1490     gst_object_unref (p);
1491     p = gst_element_get_static_pad (delem->capsfilter, "src");
1492     parse_pad_set_target (parsepad, p);
1493     pad = p;
1494
1495     gst_caps_unref (caps);
1496
1497     caps = gst_pad_get_current_caps (pad);
1498     if (!caps) {
1499       GST_DEBUG_OBJECT (parsebin,
1500           "No final caps set yet, delaying autoplugging");
1501       gst_object_unref (parsepad);
1502       g_value_array_free (factories);
1503       goto setup_caps_delay;
1504     }
1505   }
1506
1507   /* 1.h else continue autoplugging something from the list. */
1508   GST_LOG_OBJECT (pad, "Let's continue discovery on this pad");
1509   res =
1510       connect_pad (parsebin, src, parsepad, pad, caps, factories, chain,
1511       &deadend_details);
1512
1513   /* Need to unref the capsfilter srcpad here if
1514    * we inserted a capsfilter */
1515   if (is_parser_converter)
1516     gst_object_unref (pad);
1517
1518   gst_object_unref (parsepad);
1519   g_value_array_free (factories);
1520
1521   if (!res)
1522     goto unknown_type;
1523
1524   gst_caps_unref (caps);
1525
1526   return;
1527
1528 expose_pad:
1529   {
1530     GST_LOG_OBJECT (parsebin, "Pad is final. autoplug-continue:%d", apcontinue);
1531     expose_pad (parsebin, src, parsepad, pad, caps, chain);
1532     gst_object_unref (parsepad);
1533     gst_caps_unref (caps);
1534     return;
1535   }
1536
1537 unknown_type:
1538   {
1539     GST_LOG_OBJECT (pad, "Unknown type, posting message and firing signal");
1540
1541     chain->deadend_details = deadend_details;
1542     chain->deadend = TRUE;
1543     chain->endcaps = caps;
1544     gst_object_replace ((GstObject **) & chain->current_pad, NULL);
1545
1546     gst_element_post_message (GST_ELEMENT_CAST (parsebin),
1547         gst_missing_decoder_message_new (GST_ELEMENT_CAST (parsebin), caps));
1548
1549     g_signal_emit (G_OBJECT (parsebin),
1550         gst_parse_bin_signals[SIGNAL_UNKNOWN_TYPE], 0, pad, caps);
1551
1552     /* Try to expose anything */
1553     EXPOSE_LOCK (parsebin);
1554     if (parsebin->parse_chain) {
1555       if (gst_parse_chain_is_complete (parsebin->parse_chain)) {
1556         gst_parse_bin_expose (parsebin);
1557       }
1558     }
1559     EXPOSE_UNLOCK (parsebin);
1560
1561     if (src == parsebin->typefind) {
1562       if (!caps || gst_caps_is_empty (caps)) {
1563         GST_ELEMENT_ERROR (parsebin, STREAM, TYPE_NOT_FOUND,
1564             (_("Could not determine type of stream")), (NULL));
1565       }
1566       do_async_done (parsebin);
1567     }
1568     return;
1569   }
1570 #if 1
1571 non_fixed:
1572   {
1573     GST_DEBUG_OBJECT (pad, "pad has non-fixed caps delay autoplugging");
1574     gst_object_unref (parsepad);
1575     goto setup_caps_delay;
1576   }
1577 #endif
1578 any_caps:
1579   {
1580     GST_DEBUG_OBJECT (pad, "pad has ANY caps, delaying auto-plugging");
1581     goto setup_caps_delay;
1582   }
1583 setup_caps_delay:
1584   {
1585     GstPendingPad *ppad;
1586
1587     /* connect to caps notification */
1588     CHAIN_MUTEX_LOCK (chain);
1589     GST_LOG_OBJECT (parsebin, "Chain %p has now %d dynamic pads", chain,
1590         g_list_length (chain->pending_pads));
1591     ppad = g_slice_new0 (GstPendingPad);
1592     ppad->pad = gst_object_ref (pad);
1593     ppad->chain = chain;
1594     ppad->event_probe_id =
1595         gst_pad_add_probe (pad, GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM,
1596         pad_event_cb, ppad, NULL);
1597     chain->pending_pads = g_list_prepend (chain->pending_pads, ppad);
1598     ppad->notify_caps_id = g_signal_connect (pad, "notify::caps",
1599         G_CALLBACK (caps_notify_cb), chain);
1600     CHAIN_MUTEX_UNLOCK (chain);
1601
1602     /* If we're here because we have a Parser/Converter
1603      * we have to unref the pad */
1604     if (is_parser_converter)
1605       gst_object_unref (pad);
1606     if (caps)
1607       gst_caps_unref (caps);
1608
1609     return;
1610   }
1611 }
1612
1613 static void
1614 add_error_filter (GstParseBin * parsebin, GstElement * element)
1615 {
1616   GST_OBJECT_LOCK (parsebin);
1617   parsebin->filtered = g_list_prepend (parsebin->filtered, element);
1618   GST_OBJECT_UNLOCK (parsebin);
1619 }
1620
1621 static void
1622 remove_error_filter (GstParseBin * parsebin, GstElement * element,
1623     GstMessage ** error)
1624 {
1625   GList *l;
1626
1627   GST_OBJECT_LOCK (parsebin);
1628   parsebin->filtered = g_list_remove (parsebin->filtered, element);
1629
1630   if (error)
1631     *error = NULL;
1632
1633   l = parsebin->filtered_errors;
1634   while (l) {
1635     GstMessage *msg = l->data;
1636
1637     if (GST_MESSAGE_SRC (msg) == GST_OBJECT_CAST (element)) {
1638       /* Get the last error of this element, i.e. the earliest */
1639       if (error)
1640         gst_message_replace (error, msg);
1641       gst_message_unref (msg);
1642       l = parsebin->filtered_errors =
1643           g_list_delete_link (parsebin->filtered_errors, l);
1644     } else {
1645       l = l->next;
1646     }
1647   }
1648   GST_OBJECT_UNLOCK (parsebin);
1649 }
1650
1651 typedef struct
1652 {
1653   gboolean ret;
1654   GstPad *peer;
1655 } SendStickyEventsData;
1656
1657 static gboolean
1658 send_sticky_event (GstPad * pad, GstEvent ** event, gpointer user_data)
1659 {
1660   SendStickyEventsData *data = user_data;
1661   gboolean ret;
1662
1663   ret = gst_pad_send_event (data->peer, gst_event_ref (*event));
1664   if (!ret)
1665     data->ret = FALSE;
1666
1667   return data->ret;
1668 }
1669
1670 static gboolean
1671 send_sticky_events (GstParseBin * parsebin, GstPad * pad)
1672 {
1673   SendStickyEventsData data;
1674
1675   data.ret = TRUE;
1676   data.peer = gst_pad_get_peer (pad);
1677
1678   gst_pad_sticky_events_foreach (pad, send_sticky_event, &data);
1679
1680   gst_object_unref (data.peer);
1681
1682   return data.ret;
1683 }
1684
1685 static gchar *
1686 error_message_to_string (GstMessage * msg)
1687 {
1688   GError *err;
1689   gchar *debug, *message, *full_message;
1690
1691   gst_message_parse_error (msg, &err, &debug);
1692
1693   message = gst_error_get_message (err->domain, err->code);
1694
1695   if (debug)
1696     full_message = g_strdup_printf ("%s\n%s\n%s", message, err->message, debug);
1697   else
1698     full_message = g_strdup_printf ("%s\n%s", message, err->message);
1699
1700   g_free (message);
1701   g_free (debug);
1702   g_clear_error (&err);
1703
1704   return full_message;
1705 }
1706
1707 /* We consider elements as "simple demuxer" when they are a demuxer
1708  * with one and only one ALWAYS source pad.
1709  */
1710 static gboolean
1711 is_simple_demuxer_factory (GstElementFactory * factory)
1712 {
1713   if (strstr (gst_element_factory_get_metadata (factory,
1714               GST_ELEMENT_METADATA_KLASS), "Demuxer")) {
1715     const GList *tmp;
1716     gint num_alway_srcpads = 0;
1717
1718     for (tmp = gst_element_factory_get_static_pad_templates (factory);
1719         tmp; tmp = tmp->next) {
1720       GstStaticPadTemplate *template = tmp->data;
1721
1722       if (template->direction == GST_PAD_SRC) {
1723         if (template->presence == GST_PAD_ALWAYS) {
1724           if (num_alway_srcpads >= 0)
1725             num_alway_srcpads++;
1726         } else {
1727           num_alway_srcpads = -1;
1728         }
1729       }
1730
1731     }
1732
1733     if (num_alway_srcpads == 1)
1734       return TRUE;
1735   }
1736
1737   return FALSE;
1738 }
1739
1740 /* connect_pad:
1741  *
1742  * Try to connect the given pad to an element created from one of the factories,
1743  * and recursively.
1744  *
1745  * Note that parsepad is ghosting pad, and so pad is linked; be sure to unset parsepad's
1746  * target before trying to link pad.
1747  *
1748  * Returns TRUE if an element was properly created and linked
1749  */
1750 static gboolean
1751 connect_pad (GstParseBin * parsebin, GstElement * src, GstParsePad * parsepad,
1752     GstPad * pad, GstCaps * caps, GValueArray * factories,
1753     GstParseChain * chain, gchar ** deadend_details)
1754 {
1755   gboolean res = FALSE;
1756   GString *error_details = NULL;
1757
1758   g_return_val_if_fail (factories != NULL, FALSE);
1759   g_return_val_if_fail (factories->n_values > 0, FALSE);
1760
1761   GST_DEBUG_OBJECT (parsebin,
1762       "pad %s:%s , chain:%p, %d factories, caps %" GST_PTR_FORMAT,
1763       GST_DEBUG_PAD_NAME (pad), chain, factories->n_values, caps);
1764
1765   error_details = g_string_new ("");
1766
1767   /* 2. Try to create an element and link to it */
1768   while (factories->n_values > 0) {
1769     GstAutoplugSelectResult ret;
1770     GstElementFactory *factory;
1771     GstParseElement *delem;
1772     GstElement *element;
1773     GstPad *sinkpad;
1774     GParamSpec *pspec;
1775     gboolean subtitle;
1776     GList *to_connect = NULL;
1777     gboolean is_parser_converter = FALSE, is_simple_demuxer = FALSE;
1778
1779     /* Set parsepad target to pad again, it might've been unset
1780      * below but we came back here because something failed
1781      */
1782     parse_pad_set_target (parsepad, pad);
1783
1784     /* take first factory */
1785     factory = g_value_get_object (g_value_array_get_nth (factories, 0));
1786     /* Remove selected factory from the list. */
1787     g_value_array_remove (factories, 0);
1788
1789     GST_LOG_OBJECT (src, "trying factory %" GST_PTR_FORMAT, factory);
1790
1791     /* Check if the caps are really supported by the factory. The
1792      * factory list is non-empty-subset filtered while caps
1793      * are only accepted by a pad if they are a subset of the
1794      * pad caps.
1795      *
1796      * FIXME: Only do this for fixed caps here. Non-fixed caps
1797      * can happen if a Parser/Converter was autoplugged before
1798      * this. We then assume that it will be able to convert to
1799      * everything that the decoder would want.
1800      *
1801      * A subset check will fail here because the parser caps
1802      * will be generic and while the decoder will only
1803      * support a subset of the parser caps.
1804      */
1805     if (gst_caps_is_fixed (caps)) {
1806       const GList *templs;
1807       gboolean skip = FALSE;
1808
1809       templs = gst_element_factory_get_static_pad_templates (factory);
1810
1811       while (templs) {
1812         GstStaticPadTemplate *templ = (GstStaticPadTemplate *) templs->data;
1813
1814         if (templ->direction == GST_PAD_SINK) {
1815           GstCaps *templcaps = gst_static_caps_get (&templ->static_caps);
1816
1817           if (!gst_caps_is_subset (caps, templcaps)) {
1818             GST_DEBUG_OBJECT (src,
1819                 "caps %" GST_PTR_FORMAT " not subset of %" GST_PTR_FORMAT, caps,
1820                 templcaps);
1821             gst_caps_unref (templcaps);
1822             skip = TRUE;
1823             break;
1824           }
1825
1826           gst_caps_unref (templcaps);
1827         }
1828         templs = g_list_next (templs);
1829       }
1830       if (skip)
1831         continue;
1832     }
1833
1834     /* If the factory is for a parser we first check if the factory
1835      * was already used for the current chain. If it was used already
1836      * we would otherwise create an infinite loop here because the
1837      * parser apparently accepts its own output as input.
1838      * This is only done for parsers because it's perfectly valid
1839      * to have other element classes after each other because a
1840      * parser is the only one that does not change the data. A
1841      * valid example for this would be multiple id3demux in a row.
1842      */
1843     is_parser_converter = strstr (gst_element_factory_get_metadata (factory,
1844             GST_ELEMENT_METADATA_KLASS), "Parser") != NULL;
1845     is_simple_demuxer = is_simple_demuxer_factory (factory);
1846
1847     if (is_parser_converter) {
1848       gboolean skip = FALSE;
1849       GList *l;
1850
1851       CHAIN_MUTEX_LOCK (chain);
1852       for (l = chain->elements; l; l = l->next) {
1853         GstParseElement *delem = (GstParseElement *) l->data;
1854         GstElement *otherelement = delem->element;
1855
1856         if (gst_element_get_factory (otherelement) == factory) {
1857           skip = TRUE;
1858           break;
1859         }
1860       }
1861
1862       if (!skip && chain->parent && chain->parent->parent) {
1863         GstParseChain *parent_chain = chain->parent->parent;
1864         GstParseElement *pelem =
1865             parent_chain->elements ? parent_chain->elements->data : NULL;
1866
1867         if (pelem && gst_element_get_factory (pelem->element) == factory)
1868           skip = TRUE;
1869       }
1870       CHAIN_MUTEX_UNLOCK (chain);
1871       if (skip) {
1872         GST_DEBUG_OBJECT (parsebin,
1873             "Skipping factory '%s' because it was already used in this chain",
1874             gst_plugin_feature_get_name (GST_PLUGIN_FEATURE_CAST (factory)));
1875         continue;
1876       }
1877
1878     }
1879
1880     /* Expose pads if the next factory is a decoder */
1881     if (gst_element_factory_list_is_type (factory,
1882             GST_ELEMENT_FACTORY_TYPE_DECODER)) {
1883       ret = GST_AUTOPLUG_SELECT_EXPOSE;
1884     } else {
1885       /* emit autoplug-select to see what we should do with it. */
1886       g_signal_emit (G_OBJECT (parsebin),
1887           gst_parse_bin_signals[SIGNAL_AUTOPLUG_SELECT],
1888           0, parsepad, caps, factory, &ret);
1889     }
1890
1891     switch (ret) {
1892       case GST_AUTOPLUG_SELECT_TRY:
1893         GST_DEBUG_OBJECT (parsebin, "autoplug select requested try");
1894         break;
1895       case GST_AUTOPLUG_SELECT_EXPOSE:
1896         GST_DEBUG_OBJECT (parsebin, "autoplug select requested expose");
1897         /* expose the pad, we don't have the source element */
1898         expose_pad (parsebin, src, parsepad, pad, caps, chain);
1899         res = TRUE;
1900         goto beach;
1901       case GST_AUTOPLUG_SELECT_SKIP:
1902         GST_DEBUG_OBJECT (parsebin, "autoplug select requested skip");
1903         continue;
1904       default:
1905         GST_WARNING_OBJECT (parsebin, "autoplug select returned unhandled %d",
1906             ret);
1907         break;
1908     }
1909
1910     /* 2.0. Unlink pad */
1911     parse_pad_set_target (parsepad, NULL);
1912
1913     /* 2.1. Try to create an element */
1914     if ((element = gst_element_factory_create (factory, NULL)) == NULL) {
1915       GST_WARNING_OBJECT (parsebin, "Could not create an element from %s",
1916           gst_plugin_feature_get_name (GST_PLUGIN_FEATURE (factory)));
1917       g_string_append_printf (error_details,
1918           "Could not create an element from %s\n",
1919           gst_plugin_feature_get_name (GST_PLUGIN_FEATURE (factory)));
1920       continue;
1921     }
1922
1923     /* Filter errors, this will prevent the element from causing the pipeline
1924      * to error while we test it using READY state. */
1925     add_error_filter (parsebin, element);
1926
1927     /* We don't yet want the bin to control the element's state */
1928     gst_element_set_locked_state (element, TRUE);
1929
1930     /* ... add it ... */
1931     if (!(gst_bin_add (GST_BIN_CAST (parsebin), element))) {
1932       GST_WARNING_OBJECT (parsebin, "Couldn't add %s to the bin",
1933           GST_ELEMENT_NAME (element));
1934       remove_error_filter (parsebin, element, NULL);
1935       g_string_append_printf (error_details, "Couldn't add %s to the bin\n",
1936           GST_ELEMENT_NAME (element));
1937       gst_object_unref (element);
1938       continue;
1939     }
1940
1941     /* Find its sink pad. */
1942     sinkpad = NULL;
1943     GST_OBJECT_LOCK (element);
1944     if (element->sinkpads != NULL)
1945       sinkpad = gst_object_ref (element->sinkpads->data);
1946     GST_OBJECT_UNLOCK (element);
1947
1948     if (sinkpad == NULL) {
1949       GST_WARNING_OBJECT (parsebin, "Element %s doesn't have a sink pad",
1950           GST_ELEMENT_NAME (element));
1951       remove_error_filter (parsebin, element, NULL);
1952       g_string_append_printf (error_details,
1953           "Element %s doesn't have a sink pad", GST_ELEMENT_NAME (element));
1954       gst_bin_remove (GST_BIN (parsebin), element);
1955       continue;
1956     }
1957
1958     /* ... and try to link */
1959     if ((gst_pad_link_full (pad, sinkpad,
1960                 GST_PAD_LINK_CHECK_NOTHING)) != GST_PAD_LINK_OK) {
1961       GST_WARNING_OBJECT (parsebin, "Link failed on pad %s:%s",
1962           GST_DEBUG_PAD_NAME (sinkpad));
1963       remove_error_filter (parsebin, element, NULL);
1964       g_string_append_printf (error_details, "Link failed on pad %s:%s",
1965           GST_DEBUG_PAD_NAME (sinkpad));
1966       gst_object_unref (sinkpad);
1967       gst_bin_remove (GST_BIN (parsebin), element);
1968       continue;
1969     }
1970
1971     /* ... activate it ... */
1972     if ((gst_element_set_state (element,
1973                 GST_STATE_READY)) == GST_STATE_CHANGE_FAILURE) {
1974       GstMessage *error_msg;
1975
1976       GST_WARNING_OBJECT (parsebin, "Couldn't set %s to READY",
1977           GST_ELEMENT_NAME (element));
1978       remove_error_filter (parsebin, element, &error_msg);
1979
1980       if (error_msg) {
1981         gchar *error_string = error_message_to_string (error_msg);
1982         g_string_append_printf (error_details, "Couldn't set %s to READY:\n%s",
1983             GST_ELEMENT_NAME (element), error_string);
1984         gst_message_unref (error_msg);
1985         g_free (error_string);
1986       } else {
1987         g_string_append_printf (error_details, "Couldn't set %s to READY",
1988             GST_ELEMENT_NAME (element));
1989       }
1990       gst_object_unref (sinkpad);
1991       gst_bin_remove (GST_BIN (parsebin), element);
1992       continue;
1993     }
1994
1995     /* check if we still accept the caps on the pad after setting
1996      * the element to READY */
1997     if (!gst_pad_query_accept_caps (sinkpad, caps)) {
1998       GstMessage *error_msg;
1999
2000       GST_WARNING_OBJECT (parsebin, "Element %s does not accept caps",
2001           GST_ELEMENT_NAME (element));
2002
2003       remove_error_filter (parsebin, element, &error_msg);
2004
2005       if (error_msg) {
2006         gchar *error_string = error_message_to_string (error_msg);
2007         g_string_append_printf (error_details,
2008             "Element %s does not accept caps:\n%s", GST_ELEMENT_NAME (element),
2009             error_string);
2010         gst_message_unref (error_msg);
2011         g_free (error_string);
2012       } else {
2013         g_string_append_printf (error_details,
2014             "Element %s does not accept caps", GST_ELEMENT_NAME (element));
2015       }
2016
2017       gst_element_set_state (element, GST_STATE_NULL);
2018       gst_object_unref (sinkpad);
2019       gst_bin_remove (GST_BIN (parsebin), element);
2020       continue;
2021     }
2022
2023     gst_object_unref (sinkpad);
2024     GST_LOG_OBJECT (parsebin, "linked on pad %s:%s", GST_DEBUG_PAD_NAME (pad));
2025
2026     CHAIN_MUTEX_LOCK (chain);
2027     delem = g_slice_new0 (GstParseElement);
2028     delem->element = gst_object_ref (element);
2029     delem->capsfilter = NULL;
2030     chain->elements = g_list_prepend (chain->elements, delem);
2031     chain->demuxer = is_demuxer_element (element);
2032
2033     /* If we plugging a parser, mark the chain as parsed */
2034     chain->parsed |= is_parser_converter;
2035
2036     CHAIN_MUTEX_UNLOCK (chain);
2037
2038     /* Set connection-speed property if needed */
2039     if (chain->demuxer) {
2040       GParamSpec *pspec;
2041
2042       if ((pspec = g_object_class_find_property (G_OBJECT_GET_CLASS (element),
2043                   "connection-speed"))) {
2044         guint64 speed = parsebin->connection_speed / 1000;
2045         gboolean wrong_type = FALSE;
2046
2047         if (G_PARAM_SPEC_TYPE (pspec) == G_TYPE_PARAM_UINT) {
2048           GParamSpecUInt *pspecuint = G_PARAM_SPEC_UINT (pspec);
2049
2050           speed = CLAMP (speed, pspecuint->minimum, pspecuint->maximum);
2051         } else if (G_PARAM_SPEC_TYPE (pspec) == G_TYPE_PARAM_INT) {
2052           GParamSpecInt *pspecint = G_PARAM_SPEC_INT (pspec);
2053
2054           speed = CLAMP (speed, pspecint->minimum, pspecint->maximum);
2055         } else if (G_PARAM_SPEC_TYPE (pspec) == G_TYPE_PARAM_UINT64) {
2056           GParamSpecUInt64 *pspecuint = G_PARAM_SPEC_UINT64 (pspec);
2057
2058           speed = CLAMP (speed, pspecuint->minimum, pspecuint->maximum);
2059         } else if (G_PARAM_SPEC_TYPE (pspec) == G_TYPE_PARAM_INT64) {
2060           GParamSpecInt64 *pspecint = G_PARAM_SPEC_INT64 (pspec);
2061
2062           speed = CLAMP (speed, pspecint->minimum, pspecint->maximum);
2063         } else {
2064           GST_WARNING_OBJECT (parsebin,
2065               "The connection speed property %" G_GUINT64_FORMAT " of type %s"
2066               " is not usefull not setting it", speed,
2067               g_type_name (G_PARAM_SPEC_TYPE (pspec)));
2068           wrong_type = TRUE;
2069         }
2070
2071         if (!wrong_type) {
2072           GST_DEBUG_OBJECT (parsebin,
2073               "setting connection-speed=%" G_GUINT64_FORMAT
2074               " to demuxer element", speed);
2075
2076           g_object_set (element, "connection-speed", speed, NULL);
2077         }
2078       }
2079     }
2080
2081     /* try to configure the subtitle encoding property when we can */
2082     pspec = g_object_class_find_property (G_OBJECT_GET_CLASS (element),
2083         "subtitle-encoding");
2084     if (pspec && G_PARAM_SPEC_VALUE_TYPE (pspec) == G_TYPE_STRING) {
2085       SUBTITLE_LOCK (parsebin);
2086       GST_DEBUG_OBJECT (parsebin,
2087           "setting subtitle-encoding=%s to element", parsebin->encoding);
2088       g_object_set (G_OBJECT (element), "subtitle-encoding", parsebin->encoding,
2089           NULL);
2090       SUBTITLE_UNLOCK (parsebin);
2091       subtitle = TRUE;
2092     } else {
2093       subtitle = FALSE;
2094     }
2095
2096     /* link this element further */
2097     to_connect = connect_element (parsebin, delem, chain);
2098
2099     if ((is_simple_demuxer || is_parser_converter) && to_connect) {
2100       GList *l;
2101       for (l = to_connect; l; l = g_list_next (l)) {
2102         GstPad *opad = GST_PAD_CAST (l->data);
2103         GstCaps *ocaps;
2104
2105         ocaps = get_pad_caps (opad);
2106         analyze_new_pad (parsebin, delem->element, opad, ocaps, chain);
2107         if (ocaps)
2108           gst_caps_unref (ocaps);
2109
2110         gst_object_unref (opad);
2111       }
2112       g_list_free (to_connect);
2113       to_connect = NULL;
2114     }
2115
2116     /* Bring the element to the state of the parent */
2117
2118     /* First lock element's sinkpad stream lock so no data reaches
2119      * the possible new element added when caps are sent by element
2120      * while we're still sending sticky events */
2121     GST_PAD_STREAM_LOCK (sinkpad);
2122
2123     if ((gst_element_set_state (element,
2124                 GST_STATE_PAUSED)) == GST_STATE_CHANGE_FAILURE ||
2125         !send_sticky_events (parsebin, pad)) {
2126       GstParseElement *dtmp = NULL;
2127       GstElement *tmp = NULL;
2128       GstMessage *error_msg;
2129
2130       GST_PAD_STREAM_UNLOCK (sinkpad);
2131
2132       GST_WARNING_OBJECT (parsebin, "Couldn't set %s to PAUSED",
2133           GST_ELEMENT_NAME (element));
2134
2135       g_list_free_full (to_connect, (GDestroyNotify) gst_object_unref);
2136       to_connect = NULL;
2137
2138       remove_error_filter (parsebin, element, &error_msg);
2139
2140       if (error_msg) {
2141         gchar *error_string = error_message_to_string (error_msg);
2142         g_string_append_printf (error_details, "Couldn't set %s to PAUSED:\n%s",
2143             GST_ELEMENT_NAME (element), error_string);
2144         gst_message_unref (error_msg);
2145         g_free (error_string);
2146       } else {
2147         g_string_append_printf (error_details, "Couldn't set %s to PAUSED",
2148             GST_ELEMENT_NAME (element));
2149       }
2150
2151       /* Remove all elements in this chain that were just added. No
2152        * other thread could've added elements in the meantime */
2153       CHAIN_MUTEX_LOCK (chain);
2154       do {
2155         GList *l;
2156
2157         dtmp = chain->elements->data;
2158         tmp = dtmp->element;
2159
2160         /* Disconnect any signal handlers that might be connected
2161          * in connect_element() or analyze_pad() */
2162         if (dtmp->pad_added_id)
2163           g_signal_handler_disconnect (tmp, dtmp->pad_added_id);
2164         if (dtmp->pad_removed_id)
2165           g_signal_handler_disconnect (tmp, dtmp->pad_removed_id);
2166         if (dtmp->no_more_pads_id)
2167           g_signal_handler_disconnect (tmp, dtmp->no_more_pads_id);
2168
2169         for (l = chain->pending_pads; l;) {
2170           GstPendingPad *pp = l->data;
2171           GList *n;
2172
2173           if (GST_PAD_PARENT (pp->pad) != tmp) {
2174             l = l->next;
2175             continue;
2176           }
2177
2178           gst_pending_pad_free (pp);
2179
2180           /* Remove element from the list, update list head and go to the
2181            * next element in the list */
2182           n = l->next;
2183           chain->pending_pads = g_list_delete_link (chain->pending_pads, l);
2184           l = n;
2185         }
2186
2187         if (dtmp->capsfilter) {
2188           gst_bin_remove (GST_BIN (parsebin), dtmp->capsfilter);
2189           gst_element_set_state (dtmp->capsfilter, GST_STATE_NULL);
2190           gst_object_unref (dtmp->capsfilter);
2191         }
2192
2193         gst_bin_remove (GST_BIN (parsebin), tmp);
2194         gst_element_set_state (tmp, GST_STATE_NULL);
2195
2196         gst_object_unref (tmp);
2197         g_slice_free (GstParseElement, dtmp);
2198
2199         chain->elements = g_list_delete_link (chain->elements, chain->elements);
2200       } while (tmp != element);
2201       CHAIN_MUTEX_UNLOCK (chain);
2202
2203       continue;
2204     } else {
2205       /* Everything went well, the spice must flow now */
2206       GST_PAD_STREAM_UNLOCK (sinkpad);
2207     }
2208
2209     /* Remove error filter now, from now on we can't gracefully
2210      * handle errors of the element anymore */
2211     remove_error_filter (parsebin, element, NULL);
2212
2213     /* Now let the bin handle the state */
2214     gst_element_set_locked_state (element, FALSE);
2215
2216     if (subtitle) {
2217       SUBTITLE_LOCK (parsebin);
2218       /* we added the element now, add it to the list of subtitle-encoding
2219        * elements when we can set the property */
2220       parsebin->subtitles = g_list_prepend (parsebin->subtitles, element);
2221       SUBTITLE_UNLOCK (parsebin);
2222     }
2223
2224     if (to_connect) {
2225       GList *l;
2226       for (l = to_connect; l; l = g_list_next (l)) {
2227         GstPad *opad = GST_PAD_CAST (l->data);
2228         GstCaps *ocaps;
2229
2230         ocaps = get_pad_caps (opad);
2231         analyze_new_pad (parsebin, delem->element, opad, ocaps, chain);
2232         if (ocaps)
2233           gst_caps_unref (ocaps);
2234
2235         gst_object_unref (opad);
2236       }
2237       g_list_free (to_connect);
2238       to_connect = NULL;
2239     }
2240
2241     res = TRUE;
2242     break;
2243   }
2244
2245 beach:
2246   if (error_details)
2247     *deadend_details = g_string_free (error_details, (error_details->len == 0
2248             || res));
2249   else
2250     *deadend_details = NULL;
2251
2252   return res;
2253 }
2254
2255 static GstCaps *
2256 get_pad_caps (GstPad * pad)
2257 {
2258   GstCaps *caps;
2259
2260   /* first check the pad caps, if this is set, we are positively sure it is
2261    * fixed and exactly what the element will produce. */
2262   caps = gst_pad_get_current_caps (pad);
2263
2264   /* then use the getcaps function if we don't have caps. These caps might not
2265    * be fixed in some cases, in which case analyze_new_pad will set up a
2266    * notify::caps signal to continue autoplugging. */
2267   if (caps == NULL)
2268     caps = gst_pad_query_caps (pad, NULL);
2269
2270   return caps;
2271 }
2272
2273 /* Returns a list of pads that can be connected to already and
2274  * connects to pad-added and related signals */
2275 static GList *
2276 connect_element (GstParseBin * parsebin, GstParseElement * delem,
2277     GstParseChain * chain)
2278 {
2279   GstElement *element = delem->element;
2280   GList *pads;
2281   gboolean dynamic = FALSE;
2282   GList *to_connect = NULL;
2283
2284   GST_DEBUG_OBJECT (parsebin,
2285       "Attempting to connect element %s [chain:%p] further",
2286       GST_ELEMENT_NAME (element), chain);
2287
2288   /* 1. Loop over pad templates, grabbing existing pads along the way */
2289   for (pads = GST_ELEMENT_GET_CLASS (element)->padtemplates; pads;
2290       pads = g_list_next (pads)) {
2291     GstPadTemplate *templ = GST_PAD_TEMPLATE (pads->data);
2292     const gchar *templ_name;
2293
2294     /* we are only interested in source pads */
2295     if (GST_PAD_TEMPLATE_DIRECTION (templ) != GST_PAD_SRC)
2296       continue;
2297
2298     templ_name = GST_PAD_TEMPLATE_NAME_TEMPLATE (templ);
2299     GST_DEBUG_OBJECT (parsebin, "got a source pad template %s", templ_name);
2300
2301     /* figure out what kind of pad this is */
2302     switch (GST_PAD_TEMPLATE_PRESENCE (templ)) {
2303       case GST_PAD_ALWAYS:
2304       {
2305         /* get the pad that we need to autoplug */
2306         GstPad *pad = gst_element_get_static_pad (element, templ_name);
2307
2308         if (pad) {
2309           GST_DEBUG_OBJECT (parsebin, "got the pad for always template %s",
2310               templ_name);
2311           /* here is the pad, we need to autoplug it */
2312           to_connect = g_list_prepend (to_connect, pad);
2313         } else {
2314           /* strange, pad is marked as always but it's not
2315            * there. Fix the element */
2316           GST_WARNING_OBJECT (parsebin,
2317               "could not get the pad for always template %s", templ_name);
2318         }
2319         break;
2320       }
2321       case GST_PAD_SOMETIMES:
2322       {
2323         /* try to get the pad to see if it is already created or
2324          * not */
2325         GstPad *pad = gst_element_get_static_pad (element, templ_name);
2326
2327         if (pad) {
2328           GST_DEBUG_OBJECT (parsebin, "got the pad for sometimes template %s",
2329               templ_name);
2330           /* the pad is created, we need to autoplug it */
2331           to_connect = g_list_prepend (to_connect, pad);
2332         } else {
2333           GST_DEBUG_OBJECT (parsebin,
2334               "did not get the sometimes pad of template %s", templ_name);
2335           /* we have an element that will create dynamic pads */
2336           dynamic = TRUE;
2337         }
2338         break;
2339       }
2340       case GST_PAD_REQUEST:
2341         /* ignore request pads */
2342         GST_DEBUG_OBJECT (parsebin, "ignoring request padtemplate %s",
2343             templ_name);
2344         break;
2345     }
2346   }
2347
2348   /* 2. if there are more potential pads, connect to relevant signals */
2349   if (dynamic) {
2350     GST_LOG_OBJECT (parsebin, "Adding signals to element %s in chain %p",
2351         GST_ELEMENT_NAME (element), chain);
2352     delem->pad_added_id = g_signal_connect (element, "pad-added",
2353         G_CALLBACK (pad_added_cb), chain);
2354     delem->pad_removed_id = g_signal_connect (element, "pad-removed",
2355         G_CALLBACK (pad_removed_cb), chain);
2356     delem->no_more_pads_id = g_signal_connect (element, "no-more-pads",
2357         G_CALLBACK (no_more_pads_cb), chain);
2358   }
2359
2360   /* 3. return all pads that can be connected to already */
2361
2362   return to_connect;
2363 }
2364
2365 /* expose_pad:
2366  *
2367  * Expose the given pad on the chain as a decoded pad.
2368  */
2369 static void
2370 expose_pad (GstParseBin * parsebin, GstElement * src, GstParsePad * parsepad,
2371     GstPad * pad, GstCaps * caps, GstParseChain * chain)
2372 {
2373   GST_DEBUG_OBJECT (parsebin, "pad %s:%s, chain:%p",
2374       GST_DEBUG_PAD_NAME (pad), chain);
2375
2376   gst_parse_pad_activate (parsepad, chain);
2377   chain->endpad = gst_object_ref (parsepad);
2378   if (caps)
2379     chain->endcaps = gst_caps_ref (caps);
2380   else
2381     chain->endcaps = NULL;
2382 }
2383
2384 static void
2385 type_found (GstElement * typefind, guint probability,
2386     GstCaps * caps, GstParseBin * parse_bin)
2387 {
2388   GstPad *pad, *sink_pad;
2389
2390   GST_DEBUG_OBJECT (parse_bin, "typefind found caps %" GST_PTR_FORMAT, caps);
2391
2392   /* If the typefinder (but not something else) finds text/plain - i.e. that's
2393    * the top-level type of the file - then error out.
2394    */
2395   if (gst_structure_has_name (gst_caps_get_structure (caps, 0), "text/plain")) {
2396     GST_ELEMENT_ERROR (parse_bin, STREAM, WRONG_TYPE,
2397         (_("This appears to be a text file")),
2398         ("ParseBin cannot decode plain text files"));
2399     goto exit;
2400   }
2401
2402   /* FIXME: we can only deal with one type, we don't yet support dynamically changing
2403    * caps from the typefind element */
2404   if (parse_bin->have_type || parse_bin->parse_chain)
2405     goto exit;
2406
2407   parse_bin->have_type = TRUE;
2408
2409   pad = gst_element_get_static_pad (typefind, "src");
2410   sink_pad = gst_element_get_static_pad (typefind, "sink");
2411
2412   /* need some lock here to prevent race with shutdown state change
2413    * which might yank away e.g. parse_chain while building stuff here.
2414    * In typical cases, STREAM_LOCK is held and handles that, it need not
2415    * be held (if called from a proxied setcaps), so grab it anyway */
2416   GST_PAD_STREAM_LOCK (sink_pad);
2417   parse_bin->parse_chain = gst_parse_chain_new (parse_bin, NULL, pad, caps);
2418   analyze_new_pad (parse_bin, typefind, pad, caps, parse_bin->parse_chain);
2419   GST_PAD_STREAM_UNLOCK (sink_pad);
2420
2421   gst_object_unref (sink_pad);
2422   gst_object_unref (pad);
2423
2424 exit:
2425   return;
2426 }
2427
2428 static GstPadProbeReturn
2429 pad_event_cb (GstPad * pad, GstPadProbeInfo * info, gpointer data)
2430 {
2431   GstEvent *event = GST_PAD_PROBE_INFO_EVENT (info);
2432   GstPendingPad *ppad = (GstPendingPad *) data;
2433   GstParseChain *chain = ppad->chain;
2434   GstParseBin *parsebin = chain->parsebin;
2435
2436   g_assert (ppad);
2437   g_assert (chain);
2438   g_assert (parsebin);
2439   switch (GST_EVENT_TYPE (event)) {
2440     case GST_EVENT_EOS:
2441       GST_DEBUG_OBJECT (pad, "Received EOS on a non final pad, this stream "
2442           "ended too early");
2443       chain->deadend = TRUE;
2444       chain->drained = TRUE;
2445       gst_object_replace ((GstObject **) & chain->current_pad, NULL);
2446       /* we don't set the endcaps because NULL endcaps means early EOS */
2447
2448       EXPOSE_LOCK (parsebin);
2449       if (parsebin->parse_chain)
2450         if (gst_parse_chain_is_complete (parsebin->parse_chain))
2451           gst_parse_bin_expose (parsebin);
2452       EXPOSE_UNLOCK (parsebin);
2453       break;
2454     default:
2455       break;
2456   }
2457   return GST_PAD_PROBE_OK;
2458 }
2459
2460 static void
2461 pad_added_cb (GstElement * element, GstPad * pad, GstParseChain * chain)
2462 {
2463   GstCaps *caps;
2464   GstParseBin *parsebin;
2465
2466   parsebin = chain->parsebin;
2467
2468   GST_DEBUG_OBJECT (pad, "pad added, chain:%p", chain);
2469
2470   caps = get_pad_caps (pad);
2471   analyze_new_pad (parsebin, element, pad, caps, chain);
2472   if (caps)
2473     gst_caps_unref (caps);
2474
2475   EXPOSE_LOCK (parsebin);
2476   if (parsebin->parse_chain) {
2477     if (gst_parse_chain_is_complete (parsebin->parse_chain)) {
2478       GST_LOG_OBJECT (parsebin,
2479           "That was the last dynamic object, now attempting to expose the group");
2480       if (!gst_parse_bin_expose (parsebin))
2481         GST_WARNING_OBJECT (parsebin, "Couldn't expose group");
2482     }
2483   } else {
2484     GST_DEBUG_OBJECT (parsebin, "No parse chain, new pad ignored");
2485   }
2486   EXPOSE_UNLOCK (parsebin);
2487 }
2488
2489 static void
2490 pad_removed_cb (GstElement * element, GstPad * pad, GstParseChain * chain)
2491 {
2492   GList *l;
2493
2494   GST_LOG_OBJECT (pad, "pad removed, chain:%p", chain);
2495
2496   /* In fact, we don't have to do anything here, the active group will be
2497    * removed when the group's multiqueue is drained */
2498   CHAIN_MUTEX_LOCK (chain);
2499   for (l = chain->pending_pads; l; l = l->next) {
2500     GstPendingPad *ppad = l->data;
2501     GstPad *opad = ppad->pad;
2502
2503     if (pad == opad) {
2504       gst_pending_pad_free (ppad);
2505       chain->pending_pads = g_list_delete_link (chain->pending_pads, l);
2506       break;
2507     }
2508   }
2509   CHAIN_MUTEX_UNLOCK (chain);
2510 }
2511
2512 static void
2513 no_more_pads_cb (GstElement * element, GstParseChain * chain)
2514 {
2515   GstParseGroup *group = NULL;
2516
2517   GST_LOG_OBJECT (element, "got no more pads");
2518
2519   CHAIN_MUTEX_LOCK (chain);
2520   if (!chain->elements
2521       || ((GstParseElement *) chain->elements->data)->element != element) {
2522     GST_LOG_OBJECT (chain->parsebin, "no-more-pads from old chain element '%s'",
2523         GST_OBJECT_NAME (element));
2524     CHAIN_MUTEX_UNLOCK (chain);
2525     return;
2526   } else if (!chain->demuxer) {
2527     GST_LOG_OBJECT (chain->parsebin,
2528         "no-more-pads from a non-demuxer element '%s'",
2529         GST_OBJECT_NAME (element));
2530     CHAIN_MUTEX_UNLOCK (chain);
2531     return;
2532   }
2533
2534   /* when we received no_more_pads, we can complete the pads of the chain */
2535   if (!chain->next_groups && chain->active_group) {
2536     group = chain->active_group;
2537   } else if (chain->next_groups) {
2538     GList *iter;
2539     for (iter = chain->next_groups; iter; iter = g_list_next (iter)) {
2540       group = iter->data;
2541       if (!group->no_more_pads)
2542         break;
2543     }
2544   }
2545   if (!group) {
2546     GST_ERROR_OBJECT (chain->parsebin, "can't find group for element");
2547     CHAIN_MUTEX_UNLOCK (chain);
2548     return;
2549   }
2550
2551   GST_DEBUG_OBJECT (element, "Setting group %p to complete", group);
2552
2553   group->no_more_pads = TRUE;
2554   CHAIN_MUTEX_UNLOCK (chain);
2555
2556   EXPOSE_LOCK (chain->parsebin);
2557   if (chain->parsebin->parse_chain) {
2558     if (gst_parse_chain_is_complete (chain->parsebin->parse_chain)) {
2559       gst_parse_bin_expose (chain->parsebin);
2560     }
2561   }
2562   EXPOSE_UNLOCK (chain->parsebin);
2563 }
2564
2565 static void
2566 caps_notify_cb (GstPad * pad, GParamSpec * unused, GstParseChain * chain)
2567 {
2568   GstElement *element;
2569   GList *l;
2570
2571   GST_LOG_OBJECT (pad, "Notified caps for pad %s:%s", GST_DEBUG_PAD_NAME (pad));
2572
2573   /* Disconnect this; if we still need it, we'll reconnect to this in
2574    * analyze_new_pad */
2575   element = GST_ELEMENT_CAST (gst_pad_get_parent (pad));
2576
2577   CHAIN_MUTEX_LOCK (chain);
2578   for (l = chain->pending_pads; l; l = l->next) {
2579     GstPendingPad *ppad = l->data;
2580     if (ppad->pad == pad) {
2581       gst_pending_pad_free (ppad);
2582       chain->pending_pads = g_list_delete_link (chain->pending_pads, l);
2583       break;
2584     }
2585   }
2586   CHAIN_MUTEX_UNLOCK (chain);
2587
2588   pad_added_cb (element, pad, chain);
2589
2590   gst_object_unref (element);
2591 }
2592
2593 /* Decide whether an element is a demuxer based on the
2594  * klass and number/type of src pad templates it has */
2595 static gboolean
2596 is_demuxer_element (GstElement * srcelement)
2597 {
2598   GstElementFactory *srcfactory;
2599   GstElementClass *elemclass;
2600   GList *walk;
2601   const gchar *klass;
2602   gint potential_src_pads = 0;
2603
2604   srcfactory = gst_element_get_factory (srcelement);
2605   klass =
2606       gst_element_factory_get_metadata (srcfactory, GST_ELEMENT_METADATA_KLASS);
2607
2608   /* Can't be a demuxer unless it has Demux in the klass name */
2609   if (!strstr (klass, "Demux"))
2610     return FALSE;
2611
2612   /* Walk the src pad templates and count how many the element
2613    * might produce */
2614   elemclass = GST_ELEMENT_GET_CLASS (srcelement);
2615
2616   walk = gst_element_class_get_pad_template_list (elemclass);
2617   while (walk != NULL) {
2618     GstPadTemplate *templ;
2619
2620     templ = (GstPadTemplate *) walk->data;
2621     if (GST_PAD_TEMPLATE_DIRECTION (templ) == GST_PAD_SRC) {
2622       switch (GST_PAD_TEMPLATE_PRESENCE (templ)) {
2623         case GST_PAD_ALWAYS:
2624         case GST_PAD_SOMETIMES:
2625           if (strstr (GST_PAD_TEMPLATE_NAME_TEMPLATE (templ), "%"))
2626             potential_src_pads += 2;    /* Might make multiple pads */
2627           else
2628             potential_src_pads += 1;
2629           break;
2630         case GST_PAD_REQUEST:
2631           potential_src_pads += 2;
2632           break;
2633       }
2634     }
2635     walk = g_list_next (walk);
2636   }
2637
2638   if (potential_src_pads < 2)
2639     return FALSE;
2640
2641   return TRUE;
2642 }
2643
2644 /* gst_parse_chain_get_current_group:
2645  *
2646  * Returns the current group of this chain, to which
2647  * new chains should be attached or NULL if the last
2648  * group didn't have no-more-pads.
2649  *
2650  * Not MT-safe: Call with parent chain lock!
2651  */
2652 static GstParseGroup *
2653 gst_parse_chain_get_current_group (GstParseChain * chain)
2654 {
2655   GstParseGroup *group;
2656
2657   /* Now we know that we can really return something useful */
2658   if (!chain->active_group) {
2659     chain->active_group = group = gst_parse_group_new (chain->parsebin, chain);
2660   } else if (!chain->active_group->no_more_pads) {
2661     group = chain->active_group;
2662   } else {
2663     GList *iter;
2664     group = NULL;
2665     for (iter = chain->next_groups; iter; iter = g_list_next (iter)) {
2666       GstParseGroup *next_group = iter->data;
2667
2668       if (!next_group->no_more_pads) {
2669         group = next_group;
2670         break;
2671       }
2672     }
2673   }
2674   if (!group) {
2675     group = gst_parse_group_new (chain->parsebin, chain);
2676     chain->next_groups = g_list_append (chain->next_groups, group);
2677   }
2678
2679   return group;
2680 }
2681
2682 static void gst_parse_group_free_internal (GstParseGroup * group,
2683     gboolean hide);
2684
2685 static void
2686 gst_parse_chain_free_internal (GstParseChain * chain, gboolean hide)
2687 {
2688   GList *l, *set_to_null = NULL;
2689
2690   CHAIN_MUTEX_LOCK (chain);
2691
2692   GST_DEBUG_OBJECT (chain->parsebin, "%s chain %p",
2693       (hide ? "Hiding" : "Freeing"), chain);
2694
2695   if (chain->active_group) {
2696     gst_parse_group_free_internal (chain->active_group, hide);
2697     if (!hide)
2698       chain->active_group = NULL;
2699   }
2700
2701   for (l = chain->next_groups; l; l = l->next) {
2702     gst_parse_group_free_internal ((GstParseGroup *) l->data, hide);
2703     if (!hide)
2704       l->data = NULL;
2705   }
2706   if (!hide) {
2707     g_list_free (chain->next_groups);
2708     chain->next_groups = NULL;
2709   }
2710
2711   if (!hide) {
2712     for (l = chain->old_groups; l; l = l->next) {
2713       GstParseGroup *group = l->data;
2714
2715       gst_parse_group_free (group);
2716     }
2717     g_list_free (chain->old_groups);
2718     chain->old_groups = NULL;
2719   }
2720
2721   gst_object_replace ((GstObject **) & chain->current_pad, NULL);
2722
2723   for (l = chain->pending_pads; l; l = l->next) {
2724     GstPendingPad *ppad = l->data;
2725     gst_pending_pad_free (ppad);
2726     l->data = NULL;
2727   }
2728   g_list_free (chain->pending_pads);
2729   chain->pending_pads = NULL;
2730
2731   for (l = chain->elements; l; l = l->next) {
2732     GstParseElement *delem = l->data;
2733     GstElement *element = delem->element;
2734
2735     if (delem->pad_added_id)
2736       g_signal_handler_disconnect (element, delem->pad_added_id);
2737     delem->pad_added_id = 0;
2738     if (delem->pad_removed_id)
2739       g_signal_handler_disconnect (element, delem->pad_removed_id);
2740     delem->pad_removed_id = 0;
2741     if (delem->no_more_pads_id)
2742       g_signal_handler_disconnect (element, delem->no_more_pads_id);
2743     delem->no_more_pads_id = 0;
2744
2745     if (delem->capsfilter) {
2746       if (GST_OBJECT_PARENT (delem->capsfilter) ==
2747           GST_OBJECT_CAST (chain->parsebin))
2748         gst_bin_remove (GST_BIN_CAST (chain->parsebin), delem->capsfilter);
2749       if (!hide) {
2750         set_to_null =
2751             g_list_append (set_to_null, gst_object_ref (delem->capsfilter));
2752       }
2753     }
2754
2755     if (GST_OBJECT_PARENT (element) == GST_OBJECT_CAST (chain->parsebin))
2756       gst_bin_remove (GST_BIN_CAST (chain->parsebin), element);
2757     if (!hide) {
2758       set_to_null = g_list_append (set_to_null, gst_object_ref (element));
2759     }
2760
2761     SUBTITLE_LOCK (chain->parsebin);
2762     /* remove possible subtitle element */
2763     chain->parsebin->subtitles =
2764         g_list_remove (chain->parsebin->subtitles, element);
2765     SUBTITLE_UNLOCK (chain->parsebin);
2766
2767     if (!hide) {
2768       if (delem->capsfilter) {
2769         gst_object_unref (delem->capsfilter);
2770         delem->capsfilter = NULL;
2771       }
2772
2773       gst_object_unref (element);
2774       l->data = NULL;
2775
2776       g_slice_free (GstParseElement, delem);
2777     }
2778   }
2779   if (!hide) {
2780     g_list_free (chain->elements);
2781     chain->elements = NULL;
2782   }
2783
2784   if (chain->endpad) {
2785     if (chain->endpad->exposed) {
2786       GstPad *endpad = GST_PAD_CAST (chain->endpad);
2787       GST_DEBUG_OBJECT (chain->parsebin, "Removing pad %s:%s",
2788           GST_DEBUG_PAD_NAME (endpad));
2789       gst_pad_push_event (endpad, gst_event_new_eos ());
2790       gst_element_remove_pad (GST_ELEMENT_CAST (chain->parsebin), endpad);
2791     }
2792
2793     parse_pad_set_target (chain->endpad, NULL);
2794     chain->endpad->exposed = FALSE;
2795     if (!hide) {
2796       gst_object_unref (chain->endpad);
2797       chain->endpad = NULL;
2798     }
2799   }
2800
2801   if (!hide && chain->current_pad) {
2802     gst_object_unref (chain->current_pad);
2803     chain->current_pad = NULL;
2804   }
2805
2806   if (chain->pad) {
2807     gst_object_unref (chain->pad);
2808     chain->pad = NULL;
2809   }
2810   if (chain->start_caps) {
2811     gst_caps_unref (chain->start_caps);
2812     chain->start_caps = NULL;
2813   }
2814
2815   if (chain->endcaps) {
2816     gst_caps_unref (chain->endcaps);
2817     chain->endcaps = NULL;
2818   }
2819   g_free (chain->deadend_details);
2820   chain->deadend_details = NULL;
2821
2822   GST_DEBUG_OBJECT (chain->parsebin, "%s chain %p", (hide ? "Hidden" : "Freed"),
2823       chain);
2824   CHAIN_MUTEX_UNLOCK (chain);
2825
2826   while (set_to_null) {
2827     GstElement *element = set_to_null->data;
2828     set_to_null = g_list_delete_link (set_to_null, set_to_null);
2829     gst_element_set_state (element, GST_STATE_NULL);
2830     gst_object_unref (element);
2831   }
2832
2833   if (!hide) {
2834     g_mutex_clear (&chain->lock);
2835     g_slice_free (GstParseChain, chain);
2836   }
2837 }
2838
2839 /* gst_parse_chain_free:
2840  *
2841  * Completely frees and removes the chain and all
2842  * child groups from ParseBin.
2843  *
2844  * MT-safe, don't hold the chain lock or any child chain's lock
2845  * when calling this!
2846  */
2847 static void
2848 gst_parse_chain_free (GstParseChain * chain)
2849 {
2850   gst_parse_chain_free_internal (chain, FALSE);
2851 }
2852
2853 /* gst_parse_chain_new:
2854  *
2855  * Creates a new parse chain and initializes it.
2856  *
2857  * It's up to the caller to add it to the list of child chains of
2858  * a group!
2859  */
2860 static GstParseChain *
2861 gst_parse_chain_new (GstParseBin * parsebin, GstParseGroup * parent,
2862     GstPad * pad, GstCaps * start_caps)
2863 {
2864   GstParseChain *chain = g_slice_new0 (GstParseChain);
2865
2866   GST_DEBUG_OBJECT (parsebin, "Creating new chain %p with parent group %p",
2867       chain, parent);
2868
2869   chain->parsebin = parsebin;
2870   chain->parent = parent;
2871   g_mutex_init (&chain->lock);
2872   chain->pad = gst_object_ref (pad);
2873   if (start_caps)
2874     chain->start_caps = gst_caps_ref (start_caps);
2875
2876   return chain;
2877 }
2878
2879 /****
2880  * GstParseGroup functions
2881  ****/
2882
2883 static void
2884 gst_parse_group_free_internal (GstParseGroup * group, gboolean hide)
2885 {
2886   GList *l;
2887
2888   GST_DEBUG_OBJECT (group->parsebin, "%s group %p",
2889       (hide ? "Hiding" : "Freeing"), group);
2890   for (l = group->children; l; l = l->next) {
2891     GstParseChain *chain = (GstParseChain *) l->data;
2892
2893     gst_parse_chain_free_internal (chain, hide);
2894     if (!hide)
2895       l->data = NULL;
2896   }
2897   if (!hide) {
2898     g_list_free (group->children);
2899     group->children = NULL;
2900   }
2901
2902   GST_DEBUG_OBJECT (group->parsebin, "%s group %p", (hide ? "Hid" : "Freed"),
2903       group);
2904   if (!hide)
2905     g_slice_free (GstParseGroup, group);
2906 }
2907
2908 /* gst_parse_group_free:
2909  *
2910  * Completely frees and removes the parse group and all
2911  * it's children.
2912  *
2913  * Never call this from any streaming thread!
2914  *
2915  * Not MT-safe, call with parent's chain lock!
2916  */
2917 static void
2918 gst_parse_group_free (GstParseGroup * group)
2919 {
2920   gst_parse_group_free_internal (group, FALSE);
2921 }
2922
2923 /* gst_parse_group_hide:
2924  *
2925  * Hide the parse group only, this means that
2926  * all child endpads are removed from ParseBin
2927  * and all signals are unconnected.
2928  *
2929  * No element is set to NULL state and completely
2930  * unrefed here.
2931  *
2932  * Can be called from streaming threads.
2933  *
2934  * Not MT-safe, call with parent's chain lock!
2935  */
2936 static void
2937 gst_parse_group_hide (GstParseGroup * group)
2938 {
2939   gst_parse_group_free_internal (group, TRUE);
2940 }
2941
2942 /* gst_parse_chain_free_hidden_groups:
2943  *
2944  * Frees any parse groups that were hidden previously.
2945  * This allows keeping memory use from ballooning when
2946  * switching chains repeatedly.
2947  *
2948  * A new throwaway thread will be created to free the
2949  * groups, so any delay does not block the setup of a
2950  * new group.
2951  *
2952  * Not MT-safe, call with parent's chain lock!
2953  */
2954 static void
2955 gst_parse_chain_free_hidden_groups (GList * old_groups)
2956 {
2957   GList *l;
2958
2959   for (l = old_groups; l; l = l->next) {
2960     GstParseGroup *group = l->data;
2961
2962     gst_parse_group_free (group);
2963   }
2964   g_list_free (old_groups);
2965 }
2966
2967 static void
2968 gst_parse_chain_start_free_hidden_groups_thread (GstParseChain * chain)
2969 {
2970   GThread *thread;
2971   GError *error = NULL;
2972   GList *old_groups;
2973
2974   old_groups = chain->old_groups;
2975   if (!old_groups)
2976     return;
2977
2978   chain->old_groups = NULL;
2979   thread = g_thread_try_new ("free-hidden-groups",
2980       (GThreadFunc) gst_parse_chain_free_hidden_groups, old_groups, &error);
2981   if (!thread || error) {
2982     GST_ERROR ("Failed to start free-hidden-groups thread: %s",
2983         error ? error->message : "unknown reason");
2984     g_clear_error (&error);
2985     chain->old_groups = old_groups;
2986     return;
2987   }
2988   GST_DEBUG_OBJECT (chain->parsebin, "Started free-hidden-groups thread");
2989   /* We do not need to wait for it or get any results from it */
2990   g_thread_unref (thread);
2991 }
2992
2993 /* gst_parse_group_new:
2994  * @parsebin: Parent ParseBin
2995  * @parent: Parent chain or %NULL
2996  *
2997  * Creates a new GstParseGroup. It is up to the caller to add it to the list
2998  * of groups.
2999  */
3000 static GstParseGroup *
3001 gst_parse_group_new (GstParseBin * parsebin, GstParseChain * parent)
3002 {
3003   GstParseGroup *group = g_slice_new0 (GstParseGroup);
3004
3005   GST_DEBUG_OBJECT (parsebin, "Creating new group %p with parent chain %p",
3006       group, parent);
3007
3008   group->parsebin = parsebin;
3009   group->parent = parent;
3010
3011   return group;
3012 }
3013
3014 /* gst_parse_group_is_complete:
3015  *
3016  * Checks if the group is complete, this means that
3017  * a) no-more-pads happened
3018  * b) all child chains are complete
3019  *
3020  * Not MT-safe, always call with ParseBin expose lock
3021  */
3022 static gboolean
3023 gst_parse_group_is_complete (GstParseGroup * group)
3024 {
3025   GList *l;
3026   gboolean complete = TRUE;
3027
3028   if (!group->no_more_pads) {
3029     complete = FALSE;
3030     goto out;
3031   }
3032
3033   for (l = group->children; l; l = l->next) {
3034     GstParseChain *chain = l->data;
3035
3036     /* Any blocked chain requires we complete this group
3037      * since everything is synchronous, we can't proceed otherwise */
3038     if (chain->endpad && chain->endpad->blocked)
3039       goto out;
3040
3041     if (!gst_parse_chain_is_complete (chain)) {
3042       complete = FALSE;
3043       goto out;
3044     }
3045   }
3046
3047 out:
3048   GST_DEBUG_OBJECT (group->parsebin, "Group %p is complete: %d", group,
3049       complete);
3050   return complete;
3051 }
3052
3053 /* gst_parse_chain_is_complete:
3054  *
3055  * Returns TRUE if the chain is complete, this means either
3056  * a) This chain is a dead end, i.e. we have no suitable plugins
3057  * b) This chain ends in an endpad and this is blocked or exposed
3058  * c) The chain has gotten far enough to have plugged 1 parser at least.
3059  *
3060  * Not MT-safe, always call with ParseBin expose lock
3061  */
3062 static gboolean
3063 gst_parse_chain_is_complete (GstParseChain * chain)
3064 {
3065   gboolean complete = FALSE;
3066
3067   CHAIN_MUTEX_LOCK (chain);
3068   if (chain->parsebin->shutdown)
3069     goto out;
3070
3071   if (chain->deadend) {
3072     complete = TRUE;
3073     goto out;
3074   }
3075
3076   if (chain->endpad && (chain->endpad->blocked || chain->endpad->exposed)) {
3077     complete = TRUE;
3078     goto out;
3079   }
3080
3081   if (chain->demuxer) {
3082     if (chain->active_group
3083         && gst_parse_group_is_complete (chain->active_group)) {
3084       complete = TRUE;
3085       goto out;
3086     }
3087   }
3088
3089   if (chain->parsed) {
3090     complete = TRUE;
3091     goto out;
3092   }
3093
3094 out:
3095   CHAIN_MUTEX_UNLOCK (chain);
3096   GST_DEBUG_OBJECT (chain->parsebin, "Chain %p is complete: %d", chain,
3097       complete);
3098   return complete;
3099 }
3100
3101 static void
3102 chain_remove_old_groups (GstParseChain * chain)
3103 {
3104   GList *tmp;
3105
3106   /* First go in child */
3107   if (chain->active_group) {
3108     for (tmp = chain->active_group->children; tmp; tmp = tmp->next) {
3109       GstParseChain *child = (GstParseChain *) tmp->data;
3110       chain_remove_old_groups (child);
3111     }
3112   }
3113
3114   if (chain->old_groups) {
3115     gst_parse_group_hide (chain->old_groups->data);
3116     gst_parse_chain_start_free_hidden_groups_thread (chain);
3117   }
3118 }
3119
3120 static gboolean
3121 drain_and_switch_chains (GstParseChain * chain, GstParsePad * drainpad,
3122     gboolean * last_group, gboolean * drained, gboolean * switched);
3123 /* drain_and_switch_chains/groups:
3124  *
3125  * CALL WITH CHAIN LOCK (or group parent) TAKEN !
3126  *
3127  * Goes down the chains/groups until it finds the chain
3128  * to which the drainpad belongs.
3129  *
3130  * It marks that pad/chain as drained and then will figure
3131  * out which group to switch to or not.
3132  *
3133  * last_chain will be set to TRUE if the group to which the
3134  * pad belongs is the last one.
3135  *
3136  * drained will be set to TRUE if the chain/group is drained.
3137  *
3138  * Returns: TRUE if the chain contained the target pad */
3139 static gboolean
3140 drain_and_switch_group (GstParseGroup * group, GstParsePad * drainpad,
3141     gboolean * last_group, gboolean * drained, gboolean * switched)
3142 {
3143   gboolean handled = FALSE;
3144   GList *tmp;
3145
3146   GST_DEBUG ("Checking group %p (target pad %s:%s)",
3147       group, GST_DEBUG_PAD_NAME (drainpad));
3148
3149   /* Definitely can't be in drained groups */
3150   if (G_UNLIKELY (group->drained)) {
3151     goto beach;
3152   }
3153
3154   /* Figure out if all our chains are drained with the
3155    * new information */
3156   group->drained = TRUE;
3157   for (tmp = group->children; tmp; tmp = tmp->next) {
3158     GstParseChain *chain = (GstParseChain *) tmp->data;
3159     gboolean subdrained = FALSE;
3160
3161     handled |=
3162         drain_and_switch_chains (chain, drainpad, last_group, &subdrained,
3163         switched);
3164     if (!subdrained)
3165       group->drained = FALSE;
3166   }
3167
3168 beach:
3169   GST_DEBUG ("group %p (last_group:%d, drained:%d, switched:%d, handled:%d)",
3170       group, *last_group, group->drained, *switched, handled);
3171   *drained = group->drained;
3172   return handled;
3173 }
3174
3175 static gboolean
3176 drain_and_switch_chains (GstParseChain * chain, GstParsePad * drainpad,
3177     gboolean * last_group, gboolean * drained, gboolean * switched)
3178 {
3179   gboolean handled = FALSE;
3180   GstParseBin *parsebin = chain->parsebin;
3181
3182   GST_DEBUG ("Checking chain %p %s:%s (target pad %s:%s)",
3183       chain, GST_DEBUG_PAD_NAME (chain->pad), GST_DEBUG_PAD_NAME (drainpad));
3184
3185   CHAIN_MUTEX_LOCK (chain);
3186
3187   /* Definitely can't be in drained chains */
3188   if (G_UNLIKELY (chain->drained)) {
3189     goto beach;
3190   }
3191
3192   if (chain->endpad) {
3193     /* Check if we're reached the target endchain */
3194     if (drainpad != NULL && chain == drainpad->chain) {
3195       GST_DEBUG ("Found the target chain");
3196       drainpad->drained = TRUE;
3197       handled = TRUE;
3198     }
3199
3200     chain->drained = chain->endpad->drained;
3201     goto beach;
3202   }
3203
3204   /* We known there are groups to switch to */
3205   if (chain->next_groups)
3206     *last_group = FALSE;
3207
3208   /* Check the active group */
3209   if (chain->active_group) {
3210     gboolean subdrained = FALSE;
3211     handled = drain_and_switch_group (chain->active_group, drainpad,
3212         last_group, &subdrained, switched);
3213
3214     /* The group is drained, see if we can switch to another */
3215     if ((handled || drainpad == NULL) && subdrained && !*switched) {
3216       if (chain->next_groups) {
3217         /* Switch to next group, the actual removal of the current group will
3218          * be done when the next one is activated */
3219         GST_DEBUG_OBJECT (parsebin, "Moving current group %p to old groups",
3220             chain->active_group);
3221         chain->old_groups =
3222             g_list_prepend (chain->old_groups, chain->active_group);
3223         GST_DEBUG_OBJECT (parsebin, "Switching to next group %p",
3224             chain->next_groups->data);
3225         chain->active_group = chain->next_groups->data;
3226         chain->next_groups =
3227             g_list_delete_link (chain->next_groups, chain->next_groups);
3228         *switched = TRUE;
3229         chain->drained = FALSE;
3230       } else {
3231         GST_DEBUG ("Group %p was the last in chain %p", chain->active_group,
3232             chain);
3233         chain->drained = TRUE;
3234         /* We're drained ! */
3235       }
3236     } else {
3237       if (subdrained && !chain->next_groups)
3238         *drained = TRUE;
3239     }
3240   }
3241
3242 beach:
3243   CHAIN_MUTEX_UNLOCK (chain);
3244
3245   GST_DEBUG ("Chain %p (handled:%d, last_group:%d, drained:%d, switched:%d)",
3246       chain, handled, *last_group, chain->drained, *switched);
3247
3248   *drained = chain->drained;
3249
3250   if (*drained)
3251     g_signal_emit (parsebin, gst_parse_bin_signals[SIGNAL_DRAINED], 0, NULL);
3252
3253   return handled;
3254 }
3255
3256 /* check if the group is drained, meaning all pads have seen an EOS
3257  * event.  */
3258 static gboolean
3259 gst_parse_pad_handle_eos (GstParsePad * pad)
3260 {
3261   gboolean last_group = TRUE;
3262   gboolean switched = FALSE;
3263   gboolean drained = FALSE;
3264   GstParseChain *chain = pad->chain;
3265   GstParseBin *parsebin = chain->parsebin;
3266
3267   GST_LOG_OBJECT (parsebin, "pad %p", pad);
3268   EXPOSE_LOCK (parsebin);
3269   if (parsebin->parse_chain) {
3270     drain_and_switch_chains (parsebin->parse_chain, pad, &last_group, &drained,
3271         &switched);
3272
3273     if (switched) {
3274       /* If we resulted in a group switch, expose what's needed */
3275       if (gst_parse_chain_is_complete (parsebin->parse_chain))
3276         gst_parse_bin_expose (parsebin);
3277     }
3278   }
3279   EXPOSE_UNLOCK (parsebin);
3280
3281   return last_group;
3282 }
3283
3284 /* gst_parse_group_is_drained:
3285  *
3286  * Check is this group is drained and cache this result.
3287  * The group is drained if all child chains are drained.
3288  *
3289  * Not MT-safe, call with group->parent's lock */
3290 static gboolean
3291 gst_parse_group_is_drained (GstParseGroup * group)
3292 {
3293   GList *l;
3294   gboolean drained = TRUE;
3295
3296   if (group->drained) {
3297     drained = TRUE;
3298     goto out;
3299   }
3300
3301   for (l = group->children; l; l = l->next) {
3302     GstParseChain *chain = l->data;
3303
3304     CHAIN_MUTEX_LOCK (chain);
3305     if (!gst_parse_chain_is_drained (chain))
3306       drained = FALSE;
3307     CHAIN_MUTEX_UNLOCK (chain);
3308     if (!drained)
3309       goto out;
3310   }
3311   group->drained = drained;
3312
3313 out:
3314   GST_DEBUG_OBJECT (group->parsebin, "Group %p is drained: %d", group, drained);
3315   return drained;
3316 }
3317
3318 /* gst_parse_chain_is_drained:
3319  *
3320  * Check is the chain is drained, which means that
3321  * either
3322  *
3323  * a) it's endpad is drained
3324  * b) there are no pending pads, the active group is drained
3325  *    and there are no next groups
3326  *
3327  * Not MT-safe, call with chain lock
3328  */
3329 static gboolean
3330 gst_parse_chain_is_drained (GstParseChain * chain)
3331 {
3332   gboolean drained = FALSE;
3333
3334   if (chain->endpad) {
3335     drained = chain->endpad->drained;
3336     goto out;
3337   }
3338
3339   if (chain->pending_pads) {
3340     drained = FALSE;
3341     goto out;
3342   }
3343
3344   if (chain->active_group && gst_parse_group_is_drained (chain->active_group)
3345       && !chain->next_groups) {
3346     drained = TRUE;
3347     goto out;
3348   }
3349
3350 out:
3351   GST_DEBUG_OBJECT (chain->parsebin, "Chain %p is drained: %d", chain, drained);
3352   return drained;
3353 }
3354
3355 /* sort_end_pads:
3356  * GCompareFunc to use with lists of GstPad.
3357  * Sorts pads by mime type.
3358  * First video (raw, then non-raw), then audio (raw, then non-raw),
3359  * then others.
3360  *
3361  * Return: negative if a<b, 0 if a==b, positive if a>b
3362  */
3363 static gint
3364 sort_end_pads (GstParsePad * da, GstParsePad * db)
3365 {
3366   gint va, vb;
3367   GstCaps *capsa, *capsb;
3368   GstStructure *sa, *sb;
3369   const gchar *namea, *nameb;
3370   gchar *ida, *idb;
3371   gint ret;
3372
3373   capsa = get_pad_caps (GST_PAD_CAST (da));
3374   capsb = get_pad_caps (GST_PAD_CAST (db));
3375
3376   sa = gst_caps_get_structure ((const GstCaps *) capsa, 0);
3377   sb = gst_caps_get_structure ((const GstCaps *) capsb, 0);
3378
3379   namea = gst_structure_get_name (sa);
3380   nameb = gst_structure_get_name (sb);
3381
3382   if (g_strrstr (namea, "video/x-raw"))
3383     va = 0;
3384   else if (g_strrstr (namea, "video/"))
3385     va = 1;
3386   else if (g_strrstr (namea, "image/"))
3387     va = 2;
3388   else if (g_strrstr (namea, "audio/x-raw"))
3389     va = 3;
3390   else if (g_strrstr (namea, "audio/"))
3391     va = 4;
3392   else
3393     va = 5;
3394
3395   if (g_strrstr (nameb, "video/x-raw"))
3396     vb = 0;
3397   else if (g_strrstr (nameb, "video/"))
3398     vb = 1;
3399   else if (g_strrstr (nameb, "image/"))
3400     vb = 2;
3401   else if (g_strrstr (nameb, "audio/x-raw"))
3402     vb = 3;
3403   else if (g_strrstr (nameb, "audio/"))
3404     vb = 4;
3405   else
3406     vb = 5;
3407
3408   gst_caps_unref (capsa);
3409   gst_caps_unref (capsb);
3410
3411   if (va != vb)
3412     return va - vb;
3413
3414   /* if otherwise the same, sort by stream-id */
3415   ida = gst_pad_get_stream_id (GST_PAD_CAST (da));
3416   idb = gst_pad_get_stream_id (GST_PAD_CAST (db));
3417   ret = (ida) ? ((idb) ? strcmp (ida, idb) : -1) : 1;
3418   g_free (ida);
3419   g_free (idb);
3420
3421   return ret;
3422 }
3423
3424 static gboolean
3425 debug_sticky_event (GstPad * pad, GstEvent ** event, gpointer user_data)
3426 {
3427   GST_DEBUG_OBJECT (pad, "sticky event %s (%p)", GST_EVENT_TYPE_NAME (*event),
3428       *event);
3429   return TRUE;
3430 }
3431
3432 /* Must only be called if the toplevel chain is complete and blocked! */
3433 /* Not MT-safe, call with ParseBin expose lock! */
3434 static gboolean
3435 gst_parse_bin_expose (GstParseBin * parsebin)
3436 {
3437   GList *tmp, *endpads;
3438   gboolean missing_plugin;
3439   GString *missing_plugin_details;
3440   gboolean already_exposed;
3441   gboolean last_group;
3442   gboolean uncollected_streams;
3443   GstStreamCollection *fallback_collection = NULL;
3444
3445 retry:
3446   endpads = NULL;
3447   missing_plugin = FALSE;
3448   already_exposed = TRUE;
3449   last_group = TRUE;
3450
3451   missing_plugin_details = g_string_new ("");
3452
3453   GST_DEBUG_OBJECT (parsebin, "Exposing currently active chains/groups");
3454
3455   /* Don't expose if we're currently shutting down */
3456   DYN_LOCK (parsebin);
3457   if (G_UNLIKELY (parsebin->shutdown)) {
3458     GST_WARNING_OBJECT (parsebin,
3459         "Currently, shutting down, aborting exposing");
3460     DYN_UNLOCK (parsebin);
3461     return FALSE;
3462   }
3463   DYN_UNLOCK (parsebin);
3464
3465   /* Get the pads that we're going to expose and mark things as exposed */
3466   uncollected_streams = FALSE;
3467   if (!gst_parse_chain_expose (parsebin->parse_chain, &endpads, &missing_plugin,
3468           missing_plugin_details, &last_group, &uncollected_streams)) {
3469     g_list_free_full (endpads, (GDestroyNotify) gst_object_unref);
3470     g_string_free (missing_plugin_details, TRUE);
3471     GST_ERROR_OBJECT (parsebin, "Broken chain/group tree");
3472     g_return_val_if_reached (FALSE);
3473     return FALSE;
3474   }
3475   if (endpads == NULL) {
3476     if (missing_plugin) {
3477       if (missing_plugin_details->len > 0) {
3478         gchar *details = g_string_free (missing_plugin_details, FALSE);
3479         GST_ELEMENT_ERROR (parsebin, CORE, MISSING_PLUGIN, (NULL),
3480             ("no suitable plugins found:\n%s", details));
3481         g_free (details);
3482       } else {
3483         g_string_free (missing_plugin_details, TRUE);
3484         GST_ELEMENT_ERROR (parsebin, CORE, MISSING_PLUGIN, (NULL),
3485             ("no suitable plugins found"));
3486       }
3487     } else {
3488       /* in this case, the stream ended without buffers,
3489        * just post a warning */
3490       g_string_free (missing_plugin_details, TRUE);
3491
3492       GST_WARNING_OBJECT (parsebin, "All streams finished without buffers. "
3493           "Last group: %d", last_group);
3494       if (last_group) {
3495         GST_ELEMENT_ERROR (parsebin, STREAM, FAILED, (NULL),
3496             ("all streams without buffers"));
3497       } else {
3498         gboolean switched = FALSE;
3499         gboolean drained = FALSE;
3500
3501         drain_and_switch_chains (parsebin->parse_chain, NULL, &last_group,
3502             &drained, &switched);
3503         GST_ELEMENT_WARNING (parsebin, STREAM, FAILED, (NULL),
3504             ("all streams without buffers"));
3505         if (switched) {
3506           if (gst_parse_chain_is_complete (parsebin->parse_chain))
3507             goto retry;
3508           else
3509             return FALSE;
3510         }
3511       }
3512     }
3513
3514     do_async_done (parsebin);
3515     return FALSE;
3516   }
3517
3518   if (uncollected_streams) {
3519     /* FIXME: Collect and use a stream id from the top chain as
3520      * upstream ID? */
3521     fallback_collection = gst_stream_collection_new (NULL);
3522
3523     build_fallback_collection (parsebin->parse_chain, fallback_collection);
3524
3525     gst_element_post_message (GST_ELEMENT (parsebin),
3526         gst_message_new_stream_collection (GST_OBJECT (parsebin),
3527             fallback_collection));
3528   }
3529
3530   g_string_free (missing_plugin_details, TRUE);
3531
3532   /* Check if this was called when everything was exposed already,
3533    * and see if we need to post a new fallback collection */
3534   for (tmp = endpads; tmp && already_exposed; tmp = tmp->next) {
3535     GstParsePad *parsepad = tmp->data;
3536
3537     already_exposed &= parsepad->exposed;
3538   }
3539   if (already_exposed) {
3540     GST_DEBUG_OBJECT (parsebin, "Everything was exposed already!");
3541     if (fallback_collection)
3542       gst_object_unref (fallback_collection);
3543     g_list_free_full (endpads, (GDestroyNotify) gst_object_unref);
3544     return TRUE;
3545   }
3546
3547   /* Set all already exposed pads to blocked */
3548   for (tmp = endpads; tmp; tmp = tmp->next) {
3549     GstParsePad *parsepad = tmp->data;
3550
3551     if (parsepad->exposed) {
3552       GST_DEBUG_OBJECT (parsepad, "blocking exposed pad");
3553       gst_parse_pad_set_blocked (parsepad, TRUE);
3554     }
3555   }
3556
3557   /* re-order pads : video, then audio, then others */
3558   endpads = g_list_sort (endpads, (GCompareFunc) sort_end_pads);
3559
3560   /* Expose pads */
3561   for (tmp = endpads; tmp; tmp = tmp->next) {
3562     GstParsePad *parsepad = (GstParsePad *) tmp->data;
3563     gchar *padname;
3564
3565     //if (!parsepad->blocked)
3566     //continue;
3567
3568     /* 1. rewrite name */
3569     padname = g_strdup_printf ("src_%u", parsebin->nbpads);
3570     parsebin->nbpads++;
3571     GST_DEBUG_OBJECT (parsebin, "About to expose parsepad %s as %s",
3572         GST_OBJECT_NAME (parsepad), padname);
3573     gst_object_set_name (GST_OBJECT (parsepad), padname);
3574     g_free (padname);
3575
3576     gst_pad_sticky_events_foreach (GST_PAD_CAST (parsepad), debug_sticky_event,
3577         parsepad);
3578
3579     /* 2. activate and add */
3580     if (!parsepad->exposed) {
3581       parsepad->exposed = TRUE;
3582       if (!gst_element_add_pad (GST_ELEMENT (parsebin),
3583               GST_PAD_CAST (parsepad))) {
3584         /* not really fatal, we can try to add the other pads */
3585         g_warning ("error adding pad to ParseBin");
3586         parsepad->exposed = FALSE;
3587         continue;
3588       }
3589 #if 0
3590       /* HACK: Send an empty gap event to push sticky events */
3591       gst_pad_push_event (GST_PAD (parsepad),
3592           gst_event_new_gap (0, GST_CLOCK_TIME_NONE));
3593 #endif
3594     }
3595
3596     GST_INFO_OBJECT (parsepad, "added new decoded pad");
3597   }
3598
3599   /* Unblock internal pads. The application should have connected stuff now
3600    * so that streaming can continue. */
3601   for (tmp = endpads; tmp; tmp = tmp->next) {
3602     GstParsePad *parsepad = (GstParsePad *) tmp->data;
3603
3604     if (parsepad->exposed) {
3605       GST_DEBUG_OBJECT (parsepad, "unblocking");
3606       gst_parse_pad_unblock (parsepad);
3607       GST_DEBUG_OBJECT (parsepad, "unblocked");
3608     }
3609
3610     /* Send stream-collection events for any pads that don't have them,
3611      * and post a stream-collection onto the bus */
3612     if (parsepad->active_collection == NULL && fallback_collection) {
3613       gst_pad_push_event (GST_PAD (parsepad),
3614           gst_event_new_stream_collection (gst_object_ref
3615               (fallback_collection)));
3616     }
3617     gst_object_unref (parsepad);
3618   }
3619   g_list_free (endpads);
3620
3621   if (fallback_collection)
3622     gst_object_unref (fallback_collection);
3623
3624   /* Remove old groups */
3625   chain_remove_old_groups (parsebin->parse_chain);
3626
3627   do_async_done (parsebin);
3628   GST_DEBUG_OBJECT (parsebin, "Exposed everything");
3629   return TRUE;
3630 }
3631
3632 /* gst_parse_chain_expose:
3633  *
3634  * Check if the chain can be exposed and add all endpads
3635  * to the endpads list.
3636  *
3637  * Not MT-safe, call with ParseBin expose lock! *
3638  */
3639 static gboolean
3640 gst_parse_chain_expose (GstParseChain * chain, GList ** endpads,
3641     gboolean * missing_plugin, GString * missing_plugin_details,
3642     gboolean * last_group, gboolean * uncollected_streams)
3643 {
3644   GstParseGroup *group;
3645   GList *l;
3646   gboolean ret = FALSE;
3647
3648   if (chain->deadend) {
3649     if (chain->endcaps) {
3650       if (chain->deadend_details) {
3651         g_string_append (missing_plugin_details, chain->deadend_details);
3652         g_string_append_c (missing_plugin_details, '\n');
3653       } else {
3654         gchar *desc = gst_pb_utils_get_codec_description (chain->endcaps);
3655         gchar *caps_str = gst_caps_to_string (chain->endcaps);
3656         g_string_append_printf (missing_plugin_details,
3657             "Missing decoder: %s (%s)\n", desc, caps_str);
3658         g_free (caps_str);
3659         g_free (desc);
3660       }
3661       *missing_plugin = TRUE;
3662     }
3663     return TRUE;
3664   }
3665
3666   if (chain->endpad == NULL && chain->parsed && chain->pending_pads) {
3667     /* The chain has a pending pad from a parser, let's just
3668      * expose that now as the endpad */
3669     GList *cur = chain->pending_pads;
3670     GstPendingPad *ppad = (GstPendingPad *) (cur->data);
3671     GstPad *endpad = gst_object_ref (ppad->pad);
3672     GstElement *elem =
3673         GST_ELEMENT (gst_object_get_parent (GST_OBJECT (endpad)));
3674
3675     chain->pending_pads = g_list_remove (chain->pending_pads, ppad);
3676
3677     gst_pending_pad_free (ppad);
3678
3679     GST_DEBUG_OBJECT (chain->parsebin,
3680         "Exposing pad %" GST_PTR_FORMAT " with incomplete caps "
3681         "because it's parsed", endpad);
3682
3683     expose_pad (chain->parsebin, elem, chain->current_pad, endpad, NULL, chain);
3684     gst_object_unref (endpad);
3685     gst_object_unref (elem);
3686   }
3687
3688   if (chain->endpad) {
3689     GstParsePad *p = chain->endpad;
3690
3691     if (p->active_stream && p->active_collection == NULL
3692         && !p->in_a_fallback_collection)
3693       *uncollected_streams = TRUE;
3694
3695     *endpads = g_list_prepend (*endpads, gst_object_ref (p));
3696     return TRUE;
3697   }
3698
3699   if (chain->next_groups)
3700     *last_group = FALSE;
3701
3702   group = chain->active_group;
3703   if (!group) {
3704     GstParsePad *p = chain->current_pad;
3705
3706     if (p->active_stream && p->active_collection == NULL
3707         && !p->in_a_fallback_collection)
3708       *uncollected_streams = TRUE;
3709
3710     return FALSE;
3711   }
3712
3713   for (l = group->children; l; l = l->next) {
3714     GstParseChain *childchain = l->data;
3715
3716     ret |= gst_parse_chain_expose (childchain, endpads, missing_plugin,
3717         missing_plugin_details, last_group, uncollected_streams);
3718   }
3719
3720   return ret;
3721 }
3722
3723 static void
3724 build_fallback_collection (GstParseChain * chain,
3725     GstStreamCollection * collection)
3726 {
3727   GstParseGroup *group = chain->active_group;
3728   GList *l;
3729
3730   /* If it's an end pad, or a not-finished chain that's
3731    * not a group, put it in the collection */
3732   if (chain->endpad || (chain->current_pad && group == NULL)) {
3733     GstParsePad *p = chain->current_pad;
3734
3735     if (p->active_stream != NULL && p->active_collection == NULL) {
3736       GST_DEBUG_OBJECT (p, "Adding stream to fallback collection");
3737       gst_stream_collection_add_stream (collection,
3738           gst_object_ref (p->active_stream));
3739       p->in_a_fallback_collection = TRUE;
3740     }
3741     return;
3742   }
3743
3744   if (!group)
3745     return;
3746   for (l = group->children; l; l = l->next) {
3747     GstParseChain *childchain = l->data;
3748
3749     build_fallback_collection (childchain, collection);
3750   }
3751 }
3752
3753 /*************************
3754  * GstParsePad functions
3755  *************************/
3756
3757 static void gst_parse_pad_dispose (GObject * object);
3758
3759 static void
3760 gst_parse_pad_class_init (GstParsePadClass * klass)
3761 {
3762   GObjectClass *gobject_klass;
3763
3764   gobject_klass = (GObjectClass *) klass;
3765
3766   gobject_klass->dispose = gst_parse_pad_dispose;
3767 }
3768
3769 static void
3770 gst_parse_pad_init (GstParsePad * pad)
3771 {
3772   pad->chain = NULL;
3773   pad->blocked = FALSE;
3774   pad->exposed = FALSE;
3775   pad->drained = FALSE;
3776   gst_object_ref_sink (pad);
3777 }
3778
3779 static void
3780 gst_parse_pad_dispose (GObject * object)
3781 {
3782   GstParsePad *parsepad = (GstParsePad *) (object);
3783   parse_pad_set_target (parsepad, NULL);
3784
3785   gst_object_replace ((GstObject **) & parsepad->active_collection, NULL);
3786   gst_object_replace ((GstObject **) & parsepad->active_stream, NULL);
3787
3788   G_OBJECT_CLASS (gst_parse_pad_parent_class)->dispose (object);
3789 }
3790
3791 static GstPadProbeReturn
3792 source_pad_blocked_cb (GstPad * pad, GstPadProbeInfo * info, gpointer user_data)
3793 {
3794   GstParsePad *parsepad = user_data;
3795   GstParseChain *chain;
3796   GstParseBin *parsebin;
3797   GstPadProbeReturn ret = GST_PAD_PROBE_OK;
3798
3799   if (GST_PAD_PROBE_INFO_TYPE (info) & GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM) {
3800     GstEvent *event = GST_PAD_PROBE_INFO_EVENT (info);
3801
3802     GST_LOG_OBJECT (pad, "Seeing event '%s'", GST_EVENT_TYPE_NAME (event));
3803
3804     if (!GST_EVENT_IS_SERIALIZED (event)) {
3805       /* do not block on sticky or out of band events otherwise the allocation query
3806          from demuxer might block the loop thread */
3807       GST_LOG_OBJECT (pad, "Letting OOB event through");
3808       return GST_PAD_PROBE_PASS;
3809     }
3810
3811     if (GST_EVENT_IS_STICKY (event) && GST_EVENT_TYPE (event) != GST_EVENT_EOS) {
3812       GstPad *peer;
3813
3814       /* manually push sticky events to ghost pad to avoid exposing pads
3815        * that don't have the sticky events. Handle EOS separately as we
3816        * want to block the pad on it if we didn't get any buffers before
3817        * EOS and expose the pad then. */
3818       peer = gst_pad_get_peer (pad);
3819       gst_pad_send_event (peer, gst_event_ref (event));
3820       gst_object_unref (peer);
3821       GST_LOG_OBJECT (pad, "Manually pushed sticky event through");
3822       ret = GST_PAD_PROBE_HANDLED;
3823       goto done;
3824     }
3825   } else if (GST_PAD_PROBE_INFO_TYPE (info) &
3826       GST_PAD_PROBE_TYPE_QUERY_DOWNSTREAM) {
3827     GstQuery *query = GST_PAD_PROBE_INFO_QUERY (info);
3828
3829     if (!GST_QUERY_IS_SERIALIZED (query)) {
3830       /* do not block on non-serialized queries */
3831       GST_LOG_OBJECT (pad, "Letting non-serialized query through");
3832       return GST_PAD_PROBE_PASS;
3833     }
3834     if (!gst_pad_has_current_caps (pad)) {
3835       /* do not block on allocation queries before we have caps,
3836        * this would deadlock because we are doing no autoplugging
3837        * without caps.
3838        * TODO: Try to do autoplugging based on the query caps
3839        */
3840       GST_LOG_OBJECT (pad, "Letting serialized query before caps through");
3841       return GST_PAD_PROBE_PASS;
3842     }
3843   }
3844   chain = parsepad->chain;
3845   parsebin = chain->parsebin;
3846
3847   GST_LOG_OBJECT (parsepad, "blocked: parsepad->chain:%p", chain);
3848
3849   parsepad->blocked = TRUE;
3850
3851   EXPOSE_LOCK (parsebin);
3852   if (parsebin->parse_chain) {
3853     if (!gst_parse_bin_expose (parsebin))
3854       GST_WARNING_OBJECT (parsebin, "Couldn't expose group");
3855   }
3856   EXPOSE_UNLOCK (parsebin);
3857
3858 done:
3859   return ret;
3860 }
3861
3862 /* FIXME: We can probably do some cleverer things, and maybe move this into
3863  * pbutils. Ideas:
3864  *    if there are tags look if it's got an AUDIO_CODEC VIDEO_CODEC CONTAINER_FORMAT tag
3865  *    Look at the factory klass designation of parsers in the chain
3866  *    Consider demuxer pad names as well, sometimes they give the type away
3867  */
3868 static GstStreamType
3869 guess_stream_type_from_caps (GstCaps * caps)
3870 {
3871   GstStructure *s;
3872   const gchar *name;
3873
3874   if (gst_caps_get_size (caps) < 1)
3875     return GST_STREAM_TYPE_UNKNOWN;
3876
3877   s = gst_caps_get_structure (caps, 0);
3878   name = gst_structure_get_name (s);
3879
3880   if (g_str_has_prefix (name, "video/") || g_str_has_prefix (name, "image/"))
3881     return GST_STREAM_TYPE_VIDEO;
3882   if (g_str_has_prefix (name, "audio/"))
3883     return GST_STREAM_TYPE_AUDIO;
3884   if (g_str_has_prefix (name, "text/") ||
3885       g_str_has_prefix (name, "subpicture/"))
3886     return GST_STREAM_TYPE_TEXT;
3887
3888   return GST_STREAM_TYPE_UNKNOWN;
3889 }
3890
3891 static void
3892 gst_parse_pad_update_caps (GstParsePad * parsepad, GstCaps * caps)
3893 {
3894   if (caps && parsepad->active_stream) {
3895     GST_DEBUG_OBJECT (parsepad, "Storing caps %" GST_PTR_FORMAT
3896         " on stream %" GST_PTR_FORMAT, caps, parsepad->active_stream);
3897
3898     if (gst_caps_is_fixed (caps))
3899       gst_stream_set_caps (parsepad->active_stream, caps);
3900     /* intuit a type */
3901     if (gst_stream_get_stream_type (parsepad->active_stream) ==
3902         GST_STREAM_TYPE_UNKNOWN) {
3903       GstStreamType new_type = guess_stream_type_from_caps (caps);
3904       if (new_type != GST_STREAM_TYPE_UNKNOWN)
3905         gst_stream_set_stream_type (parsepad->active_stream, new_type);
3906     }
3907   }
3908 }
3909
3910 static void
3911 gst_parse_pad_update_tags (GstParsePad * parsepad, GstTagList * tags)
3912 {
3913   if (tags && parsepad->active_stream) {
3914     GST_DEBUG_OBJECT (parsepad, "Storing new tags %" GST_PTR_FORMAT
3915         " on stream %" GST_PTR_FORMAT, tags, parsepad->active_stream);
3916     gst_stream_set_tags (parsepad->active_stream, tags);
3917   }
3918 }
3919
3920 static GstEvent *
3921 gst_parse_pad_stream_start_event (GstParsePad * parsepad, GstEvent * event)
3922 {
3923   GstStream *stream = NULL;
3924   const gchar *stream_id = NULL;
3925   gboolean repeat_event = FALSE;
3926
3927   gst_event_parse_stream_start (event, &stream_id);
3928
3929   if (parsepad->active_stream != NULL &&
3930       g_str_equal (parsepad->active_stream->stream_id, stream_id))
3931     repeat_event = TRUE;
3932   else {
3933     /* A new stream requires a new collection event, or else
3934      * we'll place it in a fallback collection later */
3935     gst_object_replace ((GstObject **) & parsepad->active_collection, NULL);
3936     parsepad->in_a_fallback_collection = FALSE;
3937   }
3938
3939   gst_event_parse_stream (event, &stream);
3940   if (stream == NULL) {
3941     GstCaps *caps = gst_pad_get_current_caps (GST_PAD_CAST (parsepad));
3942     if (caps == NULL) {
3943       /* Try and get caps from the parsepad peer */
3944       GstPad *peer = gst_ghost_pad_get_target (GST_GHOST_PAD (parsepad));
3945       caps = gst_pad_get_current_caps (peer);
3946       gst_object_unref (peer);
3947     }
3948     if (caps == NULL && parsepad->chain && parsepad->chain->start_caps) {
3949       /* Still no caps, use the chain start caps */
3950       caps = gst_caps_ref (parsepad->chain->start_caps);
3951     }
3952
3953     GST_DEBUG_OBJECT (parsepad,
3954         "Saw stream_start with no GstStream. Adding one. Caps %"
3955         GST_PTR_FORMAT, caps);
3956
3957     if (repeat_event) {
3958       stream = gst_object_ref (parsepad->active_stream);
3959     } else {
3960       stream =
3961           gst_stream_new (stream_id, NULL, GST_STREAM_TYPE_UNKNOWN,
3962           GST_STREAM_FLAG_NONE);
3963       gst_object_replace ((GstObject **) & parsepad->active_stream,
3964           (GstObject *) stream);
3965     }
3966     if (caps) {
3967       gst_parse_pad_update_caps (parsepad, caps);
3968       gst_caps_unref (caps);
3969     }
3970
3971     event = gst_event_make_writable (event);
3972     gst_event_set_stream (event, stream);
3973   }
3974   gst_object_unref (stream);
3975   GST_LOG_OBJECT (parsepad, "Saw stream %s (GstStream %p)",
3976       stream->stream_id, stream);
3977
3978   return event;
3979 }
3980
3981 static void
3982 gst_parse_pad_update_stream_collection (GstParsePad * parsepad,
3983     GstStreamCollection * collection)
3984 {
3985   GST_LOG_OBJECT (parsepad, "Got new stream collection %p", collection);
3986   gst_object_replace ((GstObject **) & parsepad->active_collection,
3987       (GstObject *) collection);
3988   parsepad->in_a_fallback_collection = FALSE;
3989 }
3990
3991 static GstPadProbeReturn
3992 gst_parse_pad_event (GstPad * pad, GstPadProbeInfo * info, gpointer user_data)
3993 {
3994   GstEvent *event = GST_PAD_PROBE_INFO_EVENT (info);
3995   GstObject *parent = gst_pad_get_parent (pad);
3996   GstParsePad *parsepad = GST_PARSE_PAD (parent);
3997   gboolean forwardit = TRUE;
3998
3999   GST_LOG_OBJECT (pad, "%s parsepad:%p", GST_EVENT_TYPE_NAME (event), parsepad);
4000
4001   switch (GST_EVENT_TYPE (event)) {
4002     case GST_EVENT_CAPS:{
4003       GstCaps *caps = NULL;
4004       gst_event_parse_caps (event, &caps);
4005       gst_parse_pad_update_caps (parsepad, caps);
4006       break;
4007     }
4008     case GST_EVENT_TAG:{
4009       GstTagList *tags;
4010       gst_event_parse_tag (event, &tags);
4011       gst_parse_pad_update_tags (parsepad, tags);
4012       break;
4013     }
4014     case GST_EVENT_STREAM_START:{
4015       GST_PAD_PROBE_INFO_DATA (info) =
4016           gst_parse_pad_stream_start_event (parsepad, event);
4017       break;
4018     }
4019     case GST_EVENT_STREAM_COLLECTION:{
4020       GstStreamCollection *collection = NULL;
4021       gst_event_parse_stream_collection (event, &collection);
4022       gst_parse_pad_update_stream_collection (parsepad, collection);
4023       break;
4024     }
4025     case GST_EVENT_EOS:{
4026       GST_DEBUG_OBJECT (pad, "we received EOS");
4027
4028       /* Check if all pads are drained.
4029        * * If there is no next group, we will let the EOS go through.
4030        * * If there is a next group but the current group isn't completely
4031        *   drained, we will drop the EOS event.
4032        * * If there is a next group to expose and this was the last non-drained
4033        *   pad for that group, we will remove the ghostpad of the current group
4034        *   first, which unlinks the peer and so drops the EOS. */
4035       forwardit = gst_parse_pad_handle_eos (parsepad);
4036     }
4037     default:
4038       break;
4039   }
4040   gst_object_unref (parent);
4041   if (forwardit)
4042     return GST_PAD_PROBE_OK;
4043   else
4044     return GST_PAD_PROBE_DROP;
4045 }
4046
4047 static void
4048 gst_parse_pad_set_blocked (GstParsePad * parsepad, gboolean blocked)
4049 {
4050   GstParseBin *parsebin = parsepad->parsebin;
4051   GstPad *opad;
4052
4053   DYN_LOCK (parsebin);
4054
4055   GST_DEBUG_OBJECT (parsepad, "blocking pad: %d", blocked);
4056
4057   opad = gst_ghost_pad_get_target (GST_GHOST_PAD_CAST (parsepad));
4058   if (!opad)
4059     goto out;
4060
4061   /* do not block if shutting down.
4062    * we do not consider/expect it blocked further below, but use other trick */
4063   if (!blocked || !parsebin->shutdown) {
4064     if (blocked) {
4065       if (parsepad->block_id == 0)
4066         parsepad->block_id =
4067             gst_pad_add_probe (opad,
4068             GST_PAD_PROBE_TYPE_BLOCK_DOWNSTREAM |
4069             GST_PAD_PROBE_TYPE_QUERY_DOWNSTREAM, source_pad_blocked_cb,
4070             gst_object_ref (parsepad), (GDestroyNotify) gst_object_unref);
4071     } else {
4072       if (parsepad->block_id != 0) {
4073         gst_pad_remove_probe (opad, parsepad->block_id);
4074         parsepad->block_id = 0;
4075       }
4076       parsepad->blocked = FALSE;
4077     }
4078   }
4079
4080   if (blocked) {
4081     if (parsebin->shutdown) {
4082       /* deactivate to force flushing state to prevent NOT_LINKED errors */
4083       gst_pad_set_active (GST_PAD_CAST (parsepad), FALSE);
4084       /* note that deactivating the target pad would have no effect here,
4085        * since elements are typically connected first (and pads exposed),
4086        * and only then brought to PAUSED state (so pads activated) */
4087     } else {
4088       gst_object_ref (parsepad);
4089       parsebin->blocked_pads =
4090           g_list_prepend (parsebin->blocked_pads, parsepad);
4091     }
4092   } else {
4093     GList *l;
4094
4095     if ((l = g_list_find (parsebin->blocked_pads, parsepad))) {
4096       gst_object_unref (parsepad);
4097       parsebin->blocked_pads = g_list_delete_link (parsebin->blocked_pads, l);
4098     }
4099   }
4100   gst_object_unref (opad);
4101 out:
4102   DYN_UNLOCK (parsebin);
4103 }
4104
4105 static void
4106 gst_parse_pad_activate (GstParsePad * parsepad, GstParseChain * chain)
4107 {
4108   g_return_if_fail (chain != NULL);
4109
4110   parsepad->chain = chain;
4111   gst_pad_set_active (GST_PAD_CAST (parsepad), TRUE);
4112   gst_parse_pad_set_blocked (parsepad, TRUE);
4113 }
4114
4115 static void
4116 gst_parse_pad_unblock (GstParsePad * parsepad)
4117 {
4118   gst_parse_pad_set_blocked (parsepad, FALSE);
4119 }
4120
4121 static gboolean
4122 gst_parse_pad_query (GstPad * pad, GstObject * parent, GstQuery * query)
4123 {
4124   GstParsePad *parsepad = GST_PARSE_PAD (parent);
4125   gboolean ret = FALSE;
4126
4127   CHAIN_MUTEX_LOCK (parsepad->chain);
4128   if (!parsepad->exposed && !parsepad->parsebin->shutdown
4129       && !parsepad->chain->deadend && parsepad->chain->elements) {
4130     GstParseElement *delem = parsepad->chain->elements->data;
4131
4132     ret = FALSE;
4133     GST_DEBUG_OBJECT (parsepad->parsebin,
4134         "calling autoplug-query for %s (element %s): %" GST_PTR_FORMAT,
4135         GST_PAD_NAME (parsepad), GST_ELEMENT_NAME (delem->element), query);
4136     g_signal_emit (G_OBJECT (parsepad->parsebin),
4137         gst_parse_bin_signals[SIGNAL_AUTOPLUG_QUERY], 0, parsepad,
4138         delem->element, query, &ret);
4139
4140     if (ret)
4141       GST_DEBUG_OBJECT (parsepad->parsebin,
4142           "autoplug-query returned %d: %" GST_PTR_FORMAT, ret, query);
4143     else
4144       GST_DEBUG_OBJECT (parsepad->parsebin, "autoplug-query returned %d", ret);
4145   }
4146   CHAIN_MUTEX_UNLOCK (parsepad->chain);
4147
4148   /* If exposed or nothing handled the query use the default handler */
4149   if (!ret)
4150     ret = gst_pad_query_default (pad, parent, query);
4151
4152   return ret;
4153 }
4154
4155 /*gst_parse_pad_new:
4156  *
4157  * Creates a new GstParsePad for the given pad.
4158  */
4159 static GstParsePad *
4160 gst_parse_pad_new (GstParseBin * parsebin, GstParseChain * chain)
4161 {
4162   GstParsePad *parsepad;
4163   GstProxyPad *ppad;
4164   GstPadTemplate *pad_tmpl;
4165
4166   GST_DEBUG_OBJECT (parsebin, "making new decodepad");
4167   pad_tmpl = gst_static_pad_template_get (&decoder_bin_src_template);
4168   parsepad =
4169       g_object_new (GST_TYPE_PARSE_PAD, "direction", GST_PAD_SRC,
4170       "template", pad_tmpl, NULL);
4171   gst_ghost_pad_construct (GST_GHOST_PAD_CAST (parsepad));
4172   parsepad->chain = chain;
4173   parsepad->parsebin = parsebin;
4174   gst_object_unref (pad_tmpl);
4175
4176   ppad = gst_proxy_pad_get_internal (GST_PROXY_PAD (parsepad));
4177   gst_pad_set_query_function (GST_PAD_CAST (ppad), gst_parse_pad_query);
4178
4179   /* Add downstream event probe */
4180   GST_LOG_OBJECT (parsepad, "Adding event probe on internal pad %"
4181       GST_PTR_FORMAT, ppad);
4182   gst_pad_add_probe (GST_PAD_CAST (ppad),
4183       GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM, gst_parse_pad_event, parsepad, NULL);
4184   gst_object_unref (ppad);
4185
4186   return parsepad;
4187 }
4188
4189 static void
4190 gst_pending_pad_free (GstPendingPad * ppad)
4191 {
4192   g_assert (ppad);
4193   g_assert (ppad->pad);
4194
4195   if (ppad->event_probe_id != 0)
4196     gst_pad_remove_probe (ppad->pad, ppad->event_probe_id);
4197   if (ppad->notify_caps_id)
4198     g_signal_handler_disconnect (ppad->pad, ppad->notify_caps_id);
4199   gst_object_unref (ppad->pad);
4200   g_slice_free (GstPendingPad, ppad);
4201 }
4202
4203 /*****
4204  * Element add/remove
4205  *****/
4206
4207 static void
4208 do_async_start (GstParseBin * parsebin)
4209 {
4210   GstMessage *message;
4211
4212   parsebin->async_pending = TRUE;
4213
4214   message = gst_message_new_async_start (GST_OBJECT_CAST (parsebin));
4215   parent_class->handle_message (GST_BIN_CAST (parsebin), message);
4216 }
4217
4218 static void
4219 do_async_done (GstParseBin * parsebin)
4220 {
4221   GstMessage *message;
4222
4223   if (parsebin->async_pending) {
4224     message =
4225         gst_message_new_async_done (GST_OBJECT_CAST (parsebin),
4226         GST_CLOCK_TIME_NONE);
4227     parent_class->handle_message (GST_BIN_CAST (parsebin), message);
4228
4229     parsebin->async_pending = FALSE;
4230   }
4231 }
4232
4233 /* call with dyn_lock held */
4234 static void
4235 unblock_pads (GstParseBin * parsebin)
4236 {
4237   GList *tmp;
4238
4239   GST_LOG_OBJECT (parsebin, "unblocking pads");
4240
4241   for (tmp = parsebin->blocked_pads; tmp; tmp = tmp->next) {
4242     GstParsePad *parsepad = (GstParsePad *) tmp->data;
4243     GstPad *opad;
4244
4245     opad = gst_ghost_pad_get_target (GST_GHOST_PAD_CAST (parsepad));
4246     if (!opad)
4247       continue;
4248
4249     GST_DEBUG_OBJECT (parsepad, "unblocking");
4250     if (parsepad->block_id != 0) {
4251       gst_pad_remove_probe (opad, parsepad->block_id);
4252       parsepad->block_id = 0;
4253     }
4254     parsepad->blocked = FALSE;
4255     /* make flushing, prevent NOT_LINKED */
4256     gst_pad_set_active (GST_PAD_CAST (parsepad), FALSE);
4257     gst_object_unref (parsepad);
4258     gst_object_unref (opad);
4259     GST_DEBUG_OBJECT (parsepad, "unblocked");
4260   }
4261
4262   /* clear, no more blocked pads */
4263   g_list_free (parsebin->blocked_pads);
4264   parsebin->blocked_pads = NULL;
4265 }
4266
4267 static GstStateChangeReturn
4268 gst_parse_bin_change_state (GstElement * element, GstStateChange transition)
4269 {
4270   GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
4271   GstParseBin *parsebin = GST_PARSE_BIN (element);
4272   GstParseChain *chain_to_free = NULL;
4273
4274   switch (transition) {
4275     case GST_STATE_CHANGE_NULL_TO_READY:
4276       if (parsebin->typefind == NULL)
4277         goto missing_typefind;
4278       break;
4279     case GST_STATE_CHANGE_READY_TO_PAUSED:
4280       /* Make sure we've cleared all existing chains */
4281       EXPOSE_LOCK (parsebin);
4282       if (parsebin->parse_chain) {
4283         gst_parse_chain_free (parsebin->parse_chain);
4284         parsebin->parse_chain = NULL;
4285       }
4286       EXPOSE_UNLOCK (parsebin);
4287       DYN_LOCK (parsebin);
4288       GST_LOG_OBJECT (parsebin, "clearing shutdown flag");
4289       parsebin->shutdown = FALSE;
4290       DYN_UNLOCK (parsebin);
4291       parsebin->have_type = FALSE;
4292       ret = GST_STATE_CHANGE_ASYNC;
4293       do_async_start (parsebin);
4294
4295
4296       /* connect a signal to find out when the typefind element found
4297        * a type */
4298       parsebin->have_type_id =
4299           g_signal_connect (parsebin->typefind, "have-type",
4300           G_CALLBACK (type_found), parsebin);
4301       break;
4302     case GST_STATE_CHANGE_PAUSED_TO_READY:
4303       if (parsebin->have_type_id)
4304         g_signal_handler_disconnect (parsebin->typefind,
4305             parsebin->have_type_id);
4306       parsebin->have_type_id = 0;
4307       DYN_LOCK (parsebin);
4308       GST_LOG_OBJECT (parsebin, "setting shutdown flag");
4309       parsebin->shutdown = TRUE;
4310       unblock_pads (parsebin);
4311       DYN_UNLOCK (parsebin);
4312     default:
4313       break;
4314   }
4315
4316   {
4317     GstStateChangeReturn bret;
4318
4319     bret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
4320     if (G_UNLIKELY (bret == GST_STATE_CHANGE_FAILURE))
4321       goto activate_failed;
4322     else if (G_UNLIKELY (bret == GST_STATE_CHANGE_NO_PREROLL)) {
4323       do_async_done (parsebin);
4324       ret = bret;
4325     }
4326   }
4327   switch (transition) {
4328     case GST_STATE_CHANGE_PAUSED_TO_READY:
4329       do_async_done (parsebin);
4330       EXPOSE_LOCK (parsebin);
4331       if (parsebin->parse_chain) {
4332         chain_to_free = parsebin->parse_chain;
4333         gst_parse_chain_free_internal (parsebin->parse_chain, TRUE);
4334         parsebin->parse_chain = NULL;
4335       }
4336       EXPOSE_UNLOCK (parsebin);
4337       if (chain_to_free)
4338         gst_parse_chain_free (chain_to_free);
4339       break;
4340     case GST_STATE_CHANGE_READY_TO_NULL:
4341     default:
4342       break;
4343   }
4344
4345   return ret;
4346
4347 /* ERRORS */
4348 missing_typefind:
4349   {
4350     gst_element_post_message (element,
4351         gst_missing_element_message_new (element, "typefind"));
4352     GST_ELEMENT_ERROR (parsebin, CORE, MISSING_PLUGIN, (NULL),
4353         ("no typefind!"));
4354     return GST_STATE_CHANGE_FAILURE;
4355   }
4356 activate_failed:
4357   {
4358     GST_DEBUG_OBJECT (element,
4359         "element failed to change states -- activation problem?");
4360     do_async_done (parsebin);
4361     return GST_STATE_CHANGE_FAILURE;
4362   }
4363 }
4364
4365 static void
4366 gst_parse_bin_handle_message (GstBin * bin, GstMessage * msg)
4367 {
4368   GstParseBin *parsebin = GST_PARSE_BIN (bin);
4369   gboolean drop = FALSE;
4370
4371   switch (GST_MESSAGE_TYPE (msg)) {
4372     case GST_MESSAGE_ERROR:{
4373       GST_OBJECT_LOCK (parsebin);
4374       drop = (g_list_find (parsebin->filtered, GST_MESSAGE_SRC (msg)) != NULL);
4375       if (drop)
4376         parsebin->filtered_errors =
4377             g_list_prepend (parsebin->filtered_errors, gst_message_ref (msg));
4378       GST_OBJECT_UNLOCK (parsebin);
4379       break;
4380     }
4381     default:
4382       break;
4383   }
4384
4385   if (drop)
4386     gst_message_unref (msg);
4387   else
4388     GST_BIN_CLASS (parent_class)->handle_message (bin, msg);
4389 }
4390
4391 gboolean
4392 gst_parse_bin_plugin_init (GstPlugin * plugin)
4393 {
4394   GST_DEBUG_CATEGORY_INIT (gst_parse_bin_debug, "parsebin", 0, "parser bin");
4395
4396   return gst_element_register (plugin, "parsebin", GST_RANK_NONE,
4397       GST_TYPE_PARSE_BIN);
4398 }