Initialize Tizen 2.3
[framework/multimedia/gst-plugins-base0.10.git] / wearable / gst / playback / gsturidecodebin.c
1 /* GStreamer
2  * Copyright (C) <2007> Wim Taymans <wim.taymans@gmail.com>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 /**
21  * SECTION:element-uridecodebin
22  *
23  * Decodes data from a URI into raw media. It selects a source element that can
24  * handle the given #GstURIDecodeBin:uri scheme and connects it to a decodebin2.
25  */
26
27 #ifdef HAVE_CONFIG_H
28 #  include "config.h"
29 #endif
30
31 #include <string.h>
32
33 #include <gst/gst.h>
34 #include <gst/gst-i18n-plugin.h>
35 #include <gst/pbutils/missing-plugins.h>
36
37 #include "gstplay-marshal.h"
38 #include "gstplay-enum.h"
39 #include "gstrawcaps.h"
40
41 #include "gst/glib-compat-private.h"
42
43 #define GST_TYPE_URI_DECODE_BIN \
44   (gst_uri_decode_bin_get_type())
45 #define GST_URI_DECODE_BIN(obj) \
46   (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_URI_DECODE_BIN,GstURIDecodeBin))
47 #define GST_URI_DECODE_BIN_CLASS(klass) \
48   (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_URI_DECODE_BIN,GstURIDecodeBinClass))
49 #define GST_IS_URI_DECODE_BIN(obj) \
50   (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_URI_DECODE_BIN))
51 #define GST_IS_URI_DECODE_BIN_CLASS(klass) \
52   (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_URI_DECODE_BIN))
53 #define GST_URI_DECODE_BIN_CAST(obj) ((GstURIDecodeBin *) (obj))
54
55 typedef struct _GstURIDecodeBin GstURIDecodeBin;
56 typedef struct _GstURIDecodeBinClass GstURIDecodeBinClass;
57
58 #define GST_URI_DECODE_BIN_GET_LOCK(dec) (((GstURIDecodeBin*)(dec))->lock)
59 #define GST_URI_DECODE_BIN_LOCK(dec) (g_mutex_lock(GST_URI_DECODE_BIN_GET_LOCK(dec)))
60 #define GST_URI_DECODE_BIN_UNLOCK(dec) (g_mutex_unlock(GST_URI_DECODE_BIN_GET_LOCK(dec)))
61
62 typedef struct _GstURIDecodeBinStream
63 {
64   gulong probe_id;
65   guint bitrate;
66 } GstURIDecodeBinStream;
67
68 /**
69  * GstURIDecodeBin
70  *
71  * uridecodebin element struct
72  */
73 struct _GstURIDecodeBin
74 {
75   GstBin parent_instance;
76
77   GMutex *lock;                 /* lock for constructing */
78
79   GMutex *factories_lock;
80   guint32 factories_cookie;
81   GList *factories;             /* factories we can use for selecting elements */
82
83   gchar *uri;
84   guint connection_speed;
85   GstCaps *caps;
86   gchar *encoding;
87
88   gboolean is_stream;
89   gboolean is_download;
90   gboolean need_queue;
91   guint64 buffer_duration;      /* When buffering, buffer duration (ns) */
92   guint buffer_size;            /* When buffering, buffer size (bytes) */
93   gboolean download;
94   gboolean use_buffering;
95
96   GstElement *source;
97   GstElement *queue;
98   GstElement *typefind;
99   guint have_type_id;           /* have-type signal id from typefind */
100   GSList *decodebins;
101   GSList *pending_decodebins;
102   GHashTable *streams;
103   gint numpads;
104
105   /* for dynamic sources */
106   guint src_np_sig_id;          /* new-pad signal id */
107   guint src_nmp_sig_id;         /* no-more-pads signal id */
108   gint pending;
109
110   gboolean async_pending;       /* async-start has been emitted */
111
112   gboolean expose_allstreams;   /* Whether to expose unknow type streams or not */
113
114   guint64 ring_buffer_max_size; /* 0 means disabled */
115 };
116
117 struct _GstURIDecodeBinClass
118 {
119   GstBinClass parent_class;
120
121   /* signal fired when we found a pad that we cannot decode */
122   void (*unknown_type) (GstElement * element, GstPad * pad, GstCaps * caps);
123
124   /* signal fired to know if we continue trying to decode the given caps */
125     gboolean (*autoplug_continue) (GstElement * element, GstPad * pad,
126       GstCaps * caps);
127   /* signal fired to get a list of factories to try to autoplug */
128   GValueArray *(*autoplug_factories) (GstElement * element, GstPad * pad,
129       GstCaps * caps);
130   /* signal fired to sort the factories */
131   GValueArray *(*autoplug_sort) (GstElement * element, GstPad * pad,
132       GstCaps * caps, GValueArray * factories);
133   /* signal fired to select from the proposed list of factories */
134     GstAutoplugSelectResult (*autoplug_select) (GstElement * element,
135       GstPad * pad, GstCaps * caps, GstElementFactory * factory);
136
137   /* emitted when all data is decoded */
138   void (*drained) (GstElement * element);
139 };
140
141 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src%d",
142     GST_PAD_SRC,
143     GST_PAD_SOMETIMES,
144     GST_STATIC_CAPS_ANY);
145
146 static GstStaticCaps default_raw_caps = GST_STATIC_CAPS (DEFAULT_RAW_CAPS);
147
148 GST_DEBUG_CATEGORY_STATIC (gst_uri_decode_bin_debug);
149 #define GST_CAT_DEFAULT gst_uri_decode_bin_debug
150
151 /* signals */
152 enum
153 {
154   SIGNAL_UNKNOWN_TYPE,
155   SIGNAL_AUTOPLUG_CONTINUE,
156   SIGNAL_AUTOPLUG_FACTORIES,
157   SIGNAL_AUTOPLUG_SELECT,
158   SIGNAL_DRAINED,
159   SIGNAL_AUTOPLUG_SORT,
160   SIGNAL_SOURCE_SETUP,
161   LAST_SIGNAL
162 };
163
164 /* properties */
165 #define DEFAULT_PROP_URI            NULL
166 #define DEFAULT_PROP_SOURCE         NULL
167 #define DEFAULT_CONNECTION_SPEED    0
168 #define DEFAULT_CAPS                (gst_static_caps_get (&default_raw_caps))
169 #define DEFAULT_SUBTITLE_ENCODING   NULL
170 #define DEFAULT_BUFFER_DURATION     -1
171 #define DEFAULT_BUFFER_SIZE         -1
172 #define DEFAULT_DOWNLOAD            FALSE
173 #define DEFAULT_USE_BUFFERING       FALSE
174 #define DEFAULT_EXPOSE_ALL_STREAMS  TRUE
175 #define DEFAULT_RING_BUFFER_MAX_SIZE 0
176
177 enum
178 {
179   PROP_0,
180   PROP_URI,
181   PROP_SOURCE,
182   PROP_CONNECTION_SPEED,
183   PROP_CAPS,
184   PROP_SUBTITLE_ENCODING,
185   PROP_BUFFER_SIZE,
186   PROP_BUFFER_DURATION,
187   PROP_DOWNLOAD,
188   PROP_USE_BUFFERING,
189   PROP_EXPOSE_ALL_STREAMS,
190   PROP_RING_BUFFER_MAX_SIZE,
191   PROP_LAST
192 };
193
194 static guint gst_uri_decode_bin_signals[LAST_SIGNAL] = { 0 };
195
196 GType gst_uri_decode_bin_get_type (void);
197 GST_BOILERPLATE (GstURIDecodeBin, gst_uri_decode_bin, GstBin, GST_TYPE_BIN);
198
199 static void remove_decoders (GstURIDecodeBin * bin, gboolean force);
200 static void gst_uri_decode_bin_set_property (GObject * object, guint prop_id,
201     const GValue * value, GParamSpec * pspec);
202 static void gst_uri_decode_bin_get_property (GObject * object, guint prop_id,
203     GValue * value, GParamSpec * pspec);
204 static void gst_uri_decode_bin_finalize (GObject * obj);
205
206 static void handle_message (GstBin * bin, GstMessage * msg);
207
208 static gboolean gst_uri_decode_bin_query (GstElement * element,
209     GstQuery * query);
210 static GstStateChangeReturn gst_uri_decode_bin_change_state (GstElement *
211     element, GstStateChange transition);
212
213 static void
214 gst_uri_decode_bin_base_init (gpointer g_class)
215 {
216   GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);
217
218   gst_element_class_add_static_pad_template (gstelement_class, &srctemplate);
219   gst_element_class_set_details_simple (gstelement_class,
220       "URI Decoder", "Generic/Bin/Decoder",
221       "Autoplug and decode an URI to raw media",
222       "Wim Taymans <wim.taymans@gmail.com>");
223 }
224
225 static gboolean
226 _gst_boolean_accumulator (GSignalInvocationHint * ihint,
227     GValue * return_accu, const GValue * handler_return, gpointer dummy)
228 {
229   gboolean myboolean;
230
231   myboolean = g_value_get_boolean (handler_return);
232   if (!(ihint->run_type & G_SIGNAL_RUN_CLEANUP))
233     g_value_set_boolean (return_accu, myboolean);
234
235   /* stop emission if FALSE */
236   return myboolean;
237 }
238
239 static gboolean
240 _gst_array_accumulator (GSignalInvocationHint * ihint,
241     GValue * return_accu, const GValue * handler_return, gpointer dummy)
242 {
243   gpointer array;
244
245   array = g_value_get_boxed (handler_return);
246   if (!(ihint->run_type & G_SIGNAL_RUN_CLEANUP))
247     g_value_set_boxed (return_accu, array);
248
249   return FALSE;
250 }
251
252 static gboolean
253 _gst_select_accumulator (GSignalInvocationHint * ihint,
254     GValue * return_accu, const GValue * handler_return, gpointer dummy)
255 {
256   GstAutoplugSelectResult res;
257
258   res = g_value_get_enum (handler_return);
259   if (!(ihint->run_type & G_SIGNAL_RUN_CLEANUP))
260     g_value_set_enum (return_accu, res);
261
262   return FALSE;
263 }
264
265 static gboolean
266 _gst_array_hasvalue_accumulator (GSignalInvocationHint * ihint,
267     GValue * return_accu, const GValue * handler_return, gpointer dummy)
268 {
269   gpointer array;
270
271   array = g_value_get_boxed (handler_return);
272   if (!(ihint->run_type & G_SIGNAL_RUN_CLEANUP))
273     g_value_set_boxed (return_accu, array);
274
275   if (array != NULL)
276     return FALSE;
277
278   return TRUE;
279 }
280
281 static gboolean
282 gst_uri_decode_bin_autoplug_continue (GstElement * element, GstPad * pad,
283     GstCaps * caps)
284 {
285   /* by default we always continue */
286   return TRUE;
287 }
288
289 /* Must be called with factories lock! */
290 static void
291 gst_uri_decode_bin_update_factories_list (GstURIDecodeBin * dec)
292 {
293   if (!dec->factories ||
294       dec->factories_cookie !=
295       gst_default_registry_get_feature_list_cookie ()) {
296     if (dec->factories)
297       gst_plugin_feature_list_free (dec->factories);
298     dec->factories =
299         gst_element_factory_list_get_elements
300         (GST_ELEMENT_FACTORY_TYPE_DECODABLE, GST_RANK_MARGINAL);
301     dec->factories_cookie = gst_default_registry_get_feature_list_cookie ();
302   }
303 }
304
305 static GValueArray *
306 gst_uri_decode_bin_autoplug_factories (GstElement * element, GstPad * pad,
307     GstCaps * caps)
308 {
309   GList *list, *tmp;
310   GValueArray *result;
311   GstURIDecodeBin *dec = GST_URI_DECODE_BIN_CAST (element);
312
313   GST_DEBUG_OBJECT (element, "finding factories");
314
315   /* return all compatible factories for caps */
316   g_mutex_lock (dec->factories_lock);
317   gst_uri_decode_bin_update_factories_list (dec);
318   list =
319       gst_element_factory_list_filter (dec->factories, caps, GST_PAD_SINK,
320       FALSE);
321   g_mutex_unlock (dec->factories_lock);
322
323   result = g_value_array_new (g_list_length (list));
324   for (tmp = list; tmp; tmp = tmp->next) {
325     GstElementFactory *factory = GST_ELEMENT_FACTORY_CAST (tmp->data);
326     GValue val = { 0, };
327
328     g_value_init (&val, G_TYPE_OBJECT);
329     g_value_set_object (&val, factory);
330     g_value_array_append (result, &val);
331     g_value_unset (&val);
332   }
333   gst_plugin_feature_list_free (list);
334
335   GST_DEBUG_OBJECT (element, "autoplug-factories returns %p", result);
336
337   return result;
338 }
339
340 static GValueArray *
341 gst_uri_decode_bin_autoplug_sort (GstElement * element, GstPad * pad,
342     GstCaps * caps, GValueArray * factories)
343 {
344   return NULL;
345 }
346
347 static GstAutoplugSelectResult
348 gst_uri_decode_bin_autoplug_select (GstElement * element, GstPad * pad,
349     GstCaps * caps, GstElementFactory * factory)
350 {
351   GST_DEBUG_OBJECT (element, "default autoplug-select returns TRY");
352
353   /* Try factory. */
354   return GST_AUTOPLUG_SELECT_TRY;
355 }
356
357 static void
358 gst_uri_decode_bin_class_init (GstURIDecodeBinClass * klass)
359 {
360   GObjectClass *gobject_class;
361   GstElementClass *gstelement_class;
362   GstBinClass *gstbin_class;
363
364   gobject_class = G_OBJECT_CLASS (klass);
365   gstelement_class = GST_ELEMENT_CLASS (klass);
366   gstbin_class = GST_BIN_CLASS (klass);
367
368   gobject_class->set_property = gst_uri_decode_bin_set_property;
369   gobject_class->get_property = gst_uri_decode_bin_get_property;
370   gobject_class->finalize = gst_uri_decode_bin_finalize;
371
372   g_object_class_install_property (gobject_class, PROP_URI,
373       g_param_spec_string ("uri", "URI", "URI to decode",
374           DEFAULT_PROP_URI, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
375
376   g_object_class_install_property (gobject_class, PROP_SOURCE,
377       g_param_spec_object ("source", "Source", "Source object used",
378           GST_TYPE_ELEMENT, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
379
380   g_object_class_install_property (gobject_class, PROP_CONNECTION_SPEED,
381       g_param_spec_uint ("connection-speed", "Connection Speed",
382           "Network connection speed in kbps (0 = unknown)",
383           0, G_MAXUINT / 1000, DEFAULT_CONNECTION_SPEED,
384           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
385
386   g_object_class_install_property (gobject_class, PROP_CAPS,
387       g_param_spec_boxed ("caps", "Caps",
388           "The caps on which to stop decoding. (NULL = default)",
389           GST_TYPE_CAPS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
390
391   g_object_class_install_property (gobject_class, PROP_SUBTITLE_ENCODING,
392       g_param_spec_string ("subtitle-encoding", "subtitle encoding",
393           "Encoding to assume if input subtitles are not in UTF-8 encoding. "
394           "If not set, the GST_SUBTITLE_ENCODING environment variable will "
395           "be checked for an encoding to use. If that is not set either, "
396           "ISO-8859-15 will be assumed.", NULL,
397           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
398
399   g_object_class_install_property (gobject_class, PROP_BUFFER_SIZE,
400       g_param_spec_int ("buffer-size", "Buffer size (bytes)",
401           "Buffer size when buffering streams (-1 default value)",
402           -1, G_MAXINT, DEFAULT_BUFFER_SIZE,
403           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
404   g_object_class_install_property (gobject_class, PROP_BUFFER_DURATION,
405       g_param_spec_int64 ("buffer-duration", "Buffer duration (ns)",
406           "Buffer duration when buffering streams (-1 default value)",
407           -1, G_MAXINT64, DEFAULT_BUFFER_DURATION,
408           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
409
410   /**
411    * GstURIDecodeBin::download:
412    *
413    * For certain media type, enable download buffering.
414    */
415   g_object_class_install_property (gobject_class, PROP_DOWNLOAD,
416       g_param_spec_boolean ("download", "Download",
417           "Attempt download buffering when buffering network streams",
418           DEFAULT_DOWNLOAD, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
419   /**
420    * GstURIDecodeBin::use-buffering:
421    *
422    * Emit BUFFERING messages based on low-/high-percent thresholds of the
423    * demuxed or parsed data.
424    * When download buffering is activated and used for the current media
425    * type, this property does nothing. Otherwise perform buffering on the
426    * demuxed or parsed media.
427    *
428    * Since: 0.10.26
429    */
430   g_object_class_install_property (gobject_class, PROP_USE_BUFFERING,
431       g_param_spec_boolean ("use-buffering", "Use Buffering",
432           "Perform buffering on demuxed/parsed media",
433           DEFAULT_USE_BUFFERING, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
434
435   /**
436    * GstURIDecodeBin::expose-all-streams
437    *
438    * Expose streams of unknown type.
439    *
440    * If set to %FALSE, then only the streams that can be decoded to the final
441    * caps (see 'caps' property) will have a pad exposed. Streams that do not
442    * match those caps but could have been decoded will not have decoder plugged
443    * in internally and will not have a pad exposed. 
444    *
445    * Since: 0.10.30
446    */
447   g_object_class_install_property (gobject_class, PROP_EXPOSE_ALL_STREAMS,
448       g_param_spec_boolean ("expose-all-streams", "Expose All Streams",
449           "Expose all streams, including those of unknown type or that don't match the 'caps' property",
450           DEFAULT_EXPOSE_ALL_STREAMS,
451           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
452
453   /**
454    * GstURIDecodeBin::ring-buffer-max-size
455    *
456    * The maximum size of the ring buffer in kilobytes. If set to 0, the ring
457    * buffer is disabled. Default is 0.
458    *
459    * Since: 0.10.31
460    */
461   g_object_class_install_property (gobject_class, PROP_RING_BUFFER_MAX_SIZE,
462       g_param_spec_uint64 ("ring-buffer-max-size",
463           "Max. ring buffer size (bytes)",
464           "Max. amount of data in the ring buffer (bytes, 0 = ring buffer disabled)",
465           0, G_MAXUINT, DEFAULT_RING_BUFFER_MAX_SIZE,
466           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
467
468   /**
469    * GstURIDecodeBin::unknown-type:
470    * @bin: The uridecodebin.
471    * @pad: the new pad containing caps that cannot be resolved to a 'final'.
472    * stream type.
473    * @caps: the #GstCaps of the pad that cannot be resolved.
474    *
475    * This signal is emitted when a pad for which there is no further possible
476    * decoding is added to the uridecodebin.
477    */
478   gst_uri_decode_bin_signals[SIGNAL_UNKNOWN_TYPE] =
479       g_signal_new ("unknown-type", G_TYPE_FROM_CLASS (klass),
480       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstURIDecodeBinClass, unknown_type),
481       NULL, NULL, gst_marshal_VOID__OBJECT_BOXED, G_TYPE_NONE, 2,
482       GST_TYPE_PAD, GST_TYPE_CAPS);
483
484   /**
485    * GstURIDecodeBin::autoplug-continue:
486    * @bin: The uridecodebin.
487    * @pad: The #GstPad.
488    * @caps: The #GstCaps found.
489    *
490    * This signal is emitted whenever uridecodebin finds a new stream. It is
491    * emitted before looking for any elements that can handle that stream.
492    *
493    * <note>
494    *   Invocation of signal handlers stops after the first signal handler
495    *   returns #FALSE. Signal handlers are invoked in the order they were
496    *   connected in.
497    * </note>
498    *
499    * Returns: #TRUE if you wish uridecodebin to look for elements that can
500    * handle the given @caps. If #FALSE, those caps will be considered as
501    * final and the pad will be exposed as such (see 'new-decoded-pad'
502    * signal).
503    */
504   gst_uri_decode_bin_signals[SIGNAL_AUTOPLUG_CONTINUE] =
505       g_signal_new ("autoplug-continue", G_TYPE_FROM_CLASS (klass),
506       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstURIDecodeBinClass,
507           autoplug_continue), _gst_boolean_accumulator, NULL,
508       gst_play_marshal_BOOLEAN__OBJECT_BOXED, G_TYPE_BOOLEAN, 2, GST_TYPE_PAD,
509       GST_TYPE_CAPS);
510
511   /**
512    * GstURIDecodeBin::autoplug-factories:
513    * @bin: The uridecodebin.
514    * @pad: The #GstPad.
515    * @caps: The #GstCaps found.
516    *
517    * This function is emitted when an array of possible factories for @caps on
518    * @pad is needed. Uridecodebin will by default return an array with all
519    * compatible factories, sorted by rank.
520    *
521    * If this function returns NULL, @pad will be exposed as a final caps.
522    *
523    * If this function returns an empty array, the pad will be considered as
524    * having an unhandled type media type.
525    *
526    * <note>
527    *   Only the signal handler that is connected first will ever by invoked.
528    *   Don't connect signal handlers with the #G_CONNECT_AFTER flag to this
529    *   signal, they will never be invoked!
530    * </note>
531    *
532    * Returns: a #GValueArray* with a list of factories to try. The factories are
533    * by default tried in the returned order or based on the index returned by
534    * "autoplug-select".
535    */
536   gst_uri_decode_bin_signals[SIGNAL_AUTOPLUG_FACTORIES] =
537       g_signal_new ("autoplug-factories", G_TYPE_FROM_CLASS (klass),
538       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstURIDecodeBinClass,
539           autoplug_factories), _gst_array_accumulator, NULL,
540       gst_play_marshal_BOXED__OBJECT_BOXED, G_TYPE_VALUE_ARRAY, 2,
541       GST_TYPE_PAD, GST_TYPE_CAPS);
542
543   /**
544    * GstURIDecodeBin::autoplug-sort:
545    * @bin: The uridecodebin.
546    * @pad: The #GstPad.
547    * @caps: The #GstCaps.
548    * @factories: A #GValueArray of possible #GstElementFactory to use.
549    *
550    * Once decodebin2 has found the possible #GstElementFactory objects to try
551    * for @caps on @pad, this signal is emitted. The purpose of the signal is for
552    * the application to perform additional sorting or filtering on the element
553    * factory array.
554    *
555    * The callee should copy and modify @factories or return #NULL if the
556    * order should not change.
557    *
558    * <note>
559    *   Invocation of signal handlers stops after one signal handler has
560    *   returned something else than #NULL. Signal handlers are invoked in
561    *   the order they were connected in.
562    *   Don't connect signal handlers with the #G_CONNECT_AFTER flag to this
563    *   signal, they will never be invoked!
564    * </note>
565    *
566    * Returns: A new sorted array of #GstElementFactory objects.
567    *
568    * Since: 0.10.33
569    */
570   gst_uri_decode_bin_signals[SIGNAL_AUTOPLUG_SORT] =
571       g_signal_new ("autoplug-sort", G_TYPE_FROM_CLASS (klass),
572       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstURIDecodeBinClass, autoplug_sort),
573       _gst_array_hasvalue_accumulator, NULL,
574       gst_play_marshal_BOXED__OBJECT_BOXED_BOXED, G_TYPE_VALUE_ARRAY, 3,
575       GST_TYPE_PAD, GST_TYPE_CAPS, G_TYPE_VALUE_ARRAY);
576
577   /**
578    * GstURIDecodeBin::autoplug-select:
579    * @bin: The uridecodebin.
580    * @pad: The #GstPad.
581    * @caps: The #GstCaps.
582    * @factory: A #GstElementFactory to use.
583    *
584    * This signal is emitted once uridecodebin has found all the possible
585    * #GstElementFactory that can be used to handle the given @caps. For each of
586    * those factories, this signal is emitted.
587    *
588    * The signal handler should return a #GST_TYPE_AUTOPLUG_SELECT_RESULT enum
589    * value indicating what decodebin2 should do next.
590    *
591    * A value of #GST_AUTOPLUG_SELECT_TRY will try to autoplug an element from
592    * @factory.
593    *
594    * A value of #GST_AUTOPLUG_SELECT_EXPOSE will expose @pad without plugging
595    * any element to it.
596    *
597    * A value of #GST_AUTOPLUG_SELECT_SKIP will skip @factory and move to the
598    * next factory.
599    *
600    * <note>
601    *   Only the signal handler that is connected first will ever by invoked.
602    *   Don't connect signal handlers with the #G_CONNECT_AFTER flag to this
603    *   signal, they will never be invoked!
604    * </note>
605    *
606    * Returns: a #GST_TYPE_AUTOPLUG_SELECT_RESULT that indicates the required
607    * operation. The default handler will always return
608    * #GST_AUTOPLUG_SELECT_TRY.
609    */
610   gst_uri_decode_bin_signals[SIGNAL_AUTOPLUG_SELECT] =
611       g_signal_new ("autoplug-select", G_TYPE_FROM_CLASS (klass),
612       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstURIDecodeBinClass,
613           autoplug_select), _gst_select_accumulator, NULL,
614       gst_play_marshal_ENUM__OBJECT_BOXED_OBJECT,
615       GST_TYPE_AUTOPLUG_SELECT_RESULT, 3, GST_TYPE_PAD, GST_TYPE_CAPS,
616       GST_TYPE_ELEMENT_FACTORY);
617
618   /**
619    * GstURIDecodeBin::drained:
620    *
621    * This signal is emitted when the data for the current uri is played.
622    */
623   gst_uri_decode_bin_signals[SIGNAL_DRAINED] =
624       g_signal_new ("drained", G_TYPE_FROM_CLASS (klass),
625       G_SIGNAL_RUN_LAST,
626       G_STRUCT_OFFSET (GstURIDecodeBinClass, drained), NULL, NULL,
627       gst_marshal_VOID__VOID, G_TYPE_NONE, 0, G_TYPE_NONE);
628
629   /**
630    * GstURIDecodeBin::source-setup:
631    * @bin: the uridecodebin.
632    * @source: source element
633    *
634    * This signal is emitted after the source element has been created, so
635    * it can be configured by setting additional properties (e.g. set a
636    * proxy server for an http source, or set the device and read speed for
637    * an audio cd source). This is functionally equivalent to connecting to
638    * the notify::source signal, but more convenient.
639    *
640    * Since: 0.10.33
641    */
642   gst_uri_decode_bin_signals[SIGNAL_SOURCE_SETUP] =
643       g_signal_new ("source-setup", G_TYPE_FROM_CLASS (klass),
644       G_SIGNAL_RUN_LAST, 0, NULL, NULL,
645       gst_marshal_VOID__OBJECT, G_TYPE_NONE, 1, GST_TYPE_ELEMENT);
646
647   gstelement_class->query = GST_DEBUG_FUNCPTR (gst_uri_decode_bin_query);
648   gstelement_class->change_state =
649       GST_DEBUG_FUNCPTR (gst_uri_decode_bin_change_state);
650
651   gstbin_class->handle_message = GST_DEBUG_FUNCPTR (handle_message);
652
653   klass->autoplug_continue =
654       GST_DEBUG_FUNCPTR (gst_uri_decode_bin_autoplug_continue);
655   klass->autoplug_factories =
656       GST_DEBUG_FUNCPTR (gst_uri_decode_bin_autoplug_factories);
657   klass->autoplug_sort = GST_DEBUG_FUNCPTR (gst_uri_decode_bin_autoplug_sort);
658   klass->autoplug_select =
659       GST_DEBUG_FUNCPTR (gst_uri_decode_bin_autoplug_select);
660 }
661
662 static void
663 gst_uri_decode_bin_init (GstURIDecodeBin * dec, GstURIDecodeBinClass * klass)
664 {
665   /* first filter out the interesting element factories */
666   dec->factories_lock = g_mutex_new ();
667
668   dec->lock = g_mutex_new ();
669
670   dec->uri = g_strdup (DEFAULT_PROP_URI);
671   dec->connection_speed = DEFAULT_CONNECTION_SPEED;
672   dec->caps = DEFAULT_CAPS;
673   dec->encoding = g_strdup (DEFAULT_SUBTITLE_ENCODING);
674
675   dec->buffer_duration = DEFAULT_BUFFER_DURATION;
676   dec->buffer_size = DEFAULT_BUFFER_SIZE;
677   dec->download = DEFAULT_DOWNLOAD;
678   dec->use_buffering = DEFAULT_USE_BUFFERING;
679   dec->expose_allstreams = DEFAULT_EXPOSE_ALL_STREAMS;
680   dec->ring_buffer_max_size = DEFAULT_RING_BUFFER_MAX_SIZE;
681
682   GST_OBJECT_FLAG_SET (dec, GST_ELEMENT_IS_SOURCE);
683 }
684
685 static void
686 gst_uri_decode_bin_finalize (GObject * obj)
687 {
688   GstURIDecodeBin *dec = GST_URI_DECODE_BIN (obj);
689
690   remove_decoders (dec, TRUE);
691   g_mutex_free (dec->lock);
692   g_mutex_free (dec->factories_lock);
693   g_free (dec->uri);
694   g_free (dec->encoding);
695   if (dec->factories)
696     gst_plugin_feature_list_free (dec->factories);
697   if (dec->caps)
698     gst_caps_unref (dec->caps);
699
700   G_OBJECT_CLASS (parent_class)->finalize (obj);
701 }
702
703 static void
704 gst_uri_decode_bin_set_encoding (GstURIDecodeBin * dec, const gchar * encoding)
705 {
706   GSList *walk;
707
708   GST_URI_DECODE_BIN_LOCK (dec);
709
710   /* set property first */
711   GST_OBJECT_LOCK (dec);
712   g_free (dec->encoding);
713   dec->encoding = g_strdup (encoding);
714   GST_OBJECT_UNLOCK (dec);
715
716   /* set the property on all decodebins now */
717   for (walk = dec->decodebins; walk; walk = g_slist_next (walk)) {
718     GObject *decodebin = G_OBJECT (walk->data);
719
720     g_object_set (decodebin, "subtitle-encoding", encoding, NULL);
721   }
722   GST_URI_DECODE_BIN_UNLOCK (dec);
723 }
724
725 static void
726 gst_uri_decode_bin_set_property (GObject * object, guint prop_id,
727     const GValue * value, GParamSpec * pspec)
728 {
729   GstURIDecodeBin *dec = GST_URI_DECODE_BIN (object);
730
731   switch (prop_id) {
732     case PROP_URI:
733       GST_OBJECT_LOCK (dec);
734       g_free (dec->uri);
735       dec->uri = g_value_dup_string (value);
736       GST_OBJECT_UNLOCK (dec);
737       break;
738     case PROP_CONNECTION_SPEED:
739       GST_OBJECT_LOCK (dec);
740       dec->connection_speed = g_value_get_uint (value) * 1000;
741       GST_OBJECT_UNLOCK (dec);
742       break;
743     case PROP_CAPS:
744       GST_OBJECT_LOCK (dec);
745       if (dec->caps)
746         gst_caps_unref (dec->caps);
747       dec->caps = g_value_dup_boxed (value);
748       GST_OBJECT_UNLOCK (dec);
749       break;
750     case PROP_SUBTITLE_ENCODING:
751       gst_uri_decode_bin_set_encoding (dec, g_value_get_string (value));
752       break;
753     case PROP_BUFFER_SIZE:
754       dec->buffer_size = g_value_get_int (value);
755       break;
756     case PROP_BUFFER_DURATION:
757       dec->buffer_duration = g_value_get_int64 (value);
758       break;
759     case PROP_DOWNLOAD:
760       dec->download = g_value_get_boolean (value);
761       break;
762     case PROP_USE_BUFFERING:
763       dec->use_buffering = g_value_get_boolean (value);
764       break;
765     case PROP_EXPOSE_ALL_STREAMS:
766       dec->expose_allstreams = g_value_get_boolean (value);
767       break;
768     case PROP_RING_BUFFER_MAX_SIZE:
769       dec->ring_buffer_max_size = g_value_get_uint64 (value);
770       break;
771     default:
772       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
773       break;
774   }
775 }
776
777 static void
778 gst_uri_decode_bin_get_property (GObject * object, guint prop_id,
779     GValue * value, GParamSpec * pspec)
780 {
781   GstURIDecodeBin *dec = GST_URI_DECODE_BIN (object);
782
783   switch (prop_id) {
784     case PROP_URI:
785       GST_OBJECT_LOCK (dec);
786       g_value_set_string (value, dec->uri);
787       GST_OBJECT_UNLOCK (dec);
788       break;
789     case PROP_SOURCE:
790       GST_OBJECT_LOCK (dec);
791       g_value_set_object (value, dec->source);
792       GST_OBJECT_UNLOCK (dec);
793       break;
794     case PROP_CONNECTION_SPEED:
795       GST_OBJECT_LOCK (dec);
796       g_value_set_uint (value, dec->connection_speed / 1000);
797       GST_OBJECT_UNLOCK (dec);
798       break;
799     case PROP_CAPS:
800       GST_OBJECT_LOCK (dec);
801       g_value_set_boxed (value, dec->caps);
802       GST_OBJECT_UNLOCK (dec);
803       break;
804     case PROP_SUBTITLE_ENCODING:
805       GST_OBJECT_LOCK (dec);
806       g_value_set_string (value, dec->encoding);
807       GST_OBJECT_UNLOCK (dec);
808       break;
809     case PROP_BUFFER_SIZE:
810       GST_OBJECT_LOCK (dec);
811       g_value_set_int (value, dec->buffer_size);
812       GST_OBJECT_UNLOCK (dec);
813       break;
814     case PROP_BUFFER_DURATION:
815       GST_OBJECT_LOCK (dec);
816       g_value_set_int64 (value, dec->buffer_duration);
817       GST_OBJECT_UNLOCK (dec);
818       break;
819     case PROP_DOWNLOAD:
820       g_value_set_boolean (value, dec->download);
821       break;
822     case PROP_USE_BUFFERING:
823       g_value_set_boolean (value, dec->use_buffering);
824       break;
825     case PROP_EXPOSE_ALL_STREAMS:
826       g_value_set_boolean (value, dec->expose_allstreams);
827       break;
828     case PROP_RING_BUFFER_MAX_SIZE:
829       g_value_set_uint64 (value, dec->ring_buffer_max_size);
830       break;
831     default:
832       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
833       break;
834   }
835 }
836
837 static void
838 do_async_start (GstURIDecodeBin * dbin)
839 {
840   GstMessage *message;
841
842   dbin->async_pending = TRUE;
843
844   message = gst_message_new_async_start (GST_OBJECT_CAST (dbin), FALSE);
845   parent_class->handle_message (GST_BIN_CAST (dbin), message);
846 }
847
848 static void
849 do_async_done (GstURIDecodeBin * dbin)
850 {
851   GstMessage *message;
852
853   if (dbin->async_pending) {
854     GST_DEBUG_OBJECT (dbin, "posting ASYNC_DONE");
855     message = gst_message_new_async_done (GST_OBJECT_CAST (dbin));
856     parent_class->handle_message (GST_BIN_CAST (dbin), message);
857
858     dbin->async_pending = FALSE;
859   }
860 }
861
862 #define DEFAULT_QUEUE_SIZE          (3 * GST_SECOND)
863 #define DEFAULT_QUEUE_MIN_THRESHOLD ((DEFAULT_QUEUE_SIZE * 30) / 100)
864 #define DEFAULT_QUEUE_THRESHOLD     ((DEFAULT_QUEUE_SIZE * 95) / 100)
865
866 static void
867 unknown_type_cb (GstElement * element, GstPad * pad, GstCaps * caps,
868     GstURIDecodeBin * decoder)
869 {
870   gchar *capsstr;
871
872   capsstr = gst_caps_to_string (caps);
873   GST_ELEMENT_WARNING (decoder, STREAM, CODEC_NOT_FOUND,
874       (_("No decoder available for type \'%s\'."), capsstr), (NULL));
875   g_free (capsstr);
876 }
877
878 /* add a streaminfo that indicates that the stream is handled by the
879  * given element. This usually means that a stream without actual data is
880  * produced but one that is sunken by an element. Examples of this are:
881  * cdaudio, a hardware decoder/sink, dvd meta bins etc...
882  */
883 static void
884 add_element_stream (GstElement * element, GstURIDecodeBin * decoder)
885 {
886   g_warning ("add element stream");
887 }
888
889 /* when the decoder element signals that no more pads will be generated, we
890  * can commit the current group.
891  */
892 static void
893 no_more_pads_full (GstElement * element, gboolean subs,
894     GstURIDecodeBin * decoder)
895 {
896   gboolean final;
897
898   /* setup phase */
899   GST_DEBUG_OBJECT (element, "no more pads, %d pending", decoder->pending);
900
901   GST_URI_DECODE_BIN_LOCK (decoder);
902   final = (decoder->pending == 0);
903
904   /* nothing pending, we can exit */
905   if (final)
906     goto done;
907
908   /* the object has no pending no_more_pads */
909   if (!g_object_get_data (G_OBJECT (element), "pending"))
910     goto done;
911   g_object_set_data (G_OBJECT (element), "pending", NULL);
912
913   decoder->pending--;
914   final = (decoder->pending == 0);
915
916 done:
917   GST_URI_DECODE_BIN_UNLOCK (decoder);
918
919   if (final)
920     gst_element_no_more_pads (GST_ELEMENT_CAST (decoder));
921
922   return;
923 }
924
925 static void
926 no_more_pads (GstElement * element, GstURIDecodeBin * decoder)
927 {
928   no_more_pads_full (element, FALSE, decoder);
929 }
930
931 static void
932 source_no_more_pads (GstElement * element, GstURIDecodeBin * bin)
933 {
934   GST_DEBUG_OBJECT (bin, "No more pads in source element %s.",
935       GST_ELEMENT_NAME (element));
936
937   g_signal_handler_disconnect (element, bin->src_np_sig_id);
938   bin->src_np_sig_id = 0;
939   g_signal_handler_disconnect (element, bin->src_nmp_sig_id);
940   bin->src_nmp_sig_id = 0;
941
942   no_more_pads_full (element, FALSE, bin);
943 }
944
945 static void
946 configure_stream_buffering (GstURIDecodeBin * decoder)
947 {
948   GstElement *queue = NULL;
949   GHashTableIter iter;
950   gpointer key, value;
951   gint bitrate = 0;
952
953   /* automatic configuration enabled ? */
954   if (decoder->buffer_size != -1)
955     return;
956
957   GST_URI_DECODE_BIN_LOCK (decoder);
958   if (decoder->queue)
959     queue = gst_object_ref (decoder->queue);
960
961   g_hash_table_iter_init (&iter, decoder->streams);
962   while (g_hash_table_iter_next (&iter, &key, &value)) {
963     GstURIDecodeBinStream *stream = value;
964
965     if (stream->bitrate && bitrate >= 0)
966       bitrate += stream->bitrate;
967     else
968       bitrate = -1;
969   }
970   GST_URI_DECODE_BIN_UNLOCK (decoder);
971
972   GST_DEBUG_OBJECT (decoder, "overall bitrate %d", bitrate);
973   if (!queue)
974     return;
975
976   if (bitrate > 0) {
977     guint64 time;
978     guint bytes;
979
980     /* all streams have a bitrate;
981      * configure queue size based on queue duration using combined bitrate */
982     g_object_get (queue, "max-size-time", &time, NULL);
983     GST_DEBUG_OBJECT (decoder, "queue buffering time %" GST_TIME_FORMAT,
984         GST_TIME_ARGS (time));
985     if (time > 0) {
986       bytes = gst_util_uint64_scale (time, bitrate, 8 * GST_SECOND);
987       GST_DEBUG_OBJECT (decoder, "corresponds to buffer size %d", bytes);
988       g_object_set (queue, "max-size-bytes", bytes, NULL);
989     }
990   }
991
992   gst_object_unref (queue);
993 }
994
995 static gboolean
996 decoded_pad_event_probe (GstPad * pad, GstEvent * event,
997     GstURIDecodeBin * decoder)
998 {
999   GST_LOG_OBJECT (pad, "%s, decoder %p", GST_EVENT_TYPE_NAME (event), decoder);
1000
1001   /* look for a bitrate tag */
1002   switch (GST_EVENT_TYPE (event)) {
1003     case GST_EVENT_TAG:
1004     {
1005       GstTagList *list;
1006       guint bitrate = 0;
1007       GstURIDecodeBinStream *stream;
1008
1009       gst_event_parse_tag (event, &list);
1010       if (!gst_tag_list_get_uint_index (list, GST_TAG_NOMINAL_BITRATE, 0,
1011               &bitrate)) {
1012         gst_tag_list_get_uint_index (list, GST_TAG_BITRATE, 0, &bitrate);
1013       }
1014       GST_DEBUG_OBJECT (pad, "found bitrate %u", bitrate);
1015       if (bitrate) {
1016         GST_URI_DECODE_BIN_LOCK (decoder);
1017         stream = g_hash_table_lookup (decoder->streams, pad);
1018         GST_URI_DECODE_BIN_UNLOCK (decoder);
1019         if (stream) {
1020           stream->bitrate = bitrate;
1021           /* no longer need this probe now */
1022           gst_pad_remove_event_probe (pad, stream->probe_id);
1023           /* configure buffer if possible */
1024           configure_stream_buffering (decoder);
1025         }
1026       }
1027       break;
1028     }
1029     default:
1030       break;
1031   }
1032
1033   /* never drop */
1034   return TRUE;
1035 }
1036
1037 /* Called by the signal handlers when a decodebin has
1038  * found a new raw pad.  
1039  */
1040 static void
1041 new_decoded_pad_cb (GstElement * element, GstPad * pad, gboolean last,
1042     GstURIDecodeBin * decoder)
1043 {
1044   GstPad *newpad;
1045   GstPadTemplate *pad_tmpl;
1046   gchar *padname;
1047   GstURIDecodeBinStream *stream;
1048
1049   GST_DEBUG_OBJECT (element, "new decoded pad, name: <%s>. Last: %d",
1050       GST_PAD_NAME (pad), last);
1051
1052   GST_URI_DECODE_BIN_LOCK (decoder);
1053   padname = g_strdup_printf ("src%d", decoder->numpads);
1054   decoder->numpads++;
1055   GST_URI_DECODE_BIN_UNLOCK (decoder);
1056
1057   pad_tmpl = gst_static_pad_template_get (&srctemplate);
1058   newpad = gst_ghost_pad_new_from_template (padname, pad, pad_tmpl);
1059   gst_object_unref (pad_tmpl);
1060   g_free (padname);
1061
1062   /* store ref to the ghostpad so we can remove it */
1063   g_object_set_data (G_OBJECT (pad), "uridecodebin.ghostpad", newpad);
1064
1065   /* add event probe to monitor tags */
1066   stream = g_slice_alloc0 (sizeof (GstURIDecodeBinStream));
1067   stream->probe_id =
1068       gst_pad_add_event_probe (pad, G_CALLBACK (decoded_pad_event_probe),
1069       decoder);
1070   GST_URI_DECODE_BIN_LOCK (decoder);
1071   g_hash_table_insert (decoder->streams, pad, stream);
1072   GST_URI_DECODE_BIN_UNLOCK (decoder);
1073
1074   gst_pad_set_active (newpad, TRUE);
1075   gst_element_add_pad (GST_ELEMENT_CAST (decoder), newpad);
1076 }
1077
1078 static gboolean
1079 source_pad_event_probe (GstPad * pad, GstEvent * event,
1080     GstURIDecodeBin * decoder)
1081 {
1082   GST_LOG_OBJECT (pad, "%s, decoder %p", GST_EVENT_TYPE_NAME (event), decoder);
1083
1084   if (GST_EVENT_TYPE (event) == GST_EVENT_EOS) {
1085     GST_DEBUG_OBJECT (pad, "we received EOS");
1086
1087     g_signal_emit (decoder,
1088         gst_uri_decode_bin_signals[SIGNAL_DRAINED], 0, NULL);
1089   }
1090   /* never drop events */
1091   return TRUE;
1092 }
1093
1094 /* called when we found a raw pad on the source element. We need to set up a
1095  * padprobe to detect EOS before exposing the pad. */
1096 static void
1097 expose_decoded_pad (GstElement * element, GstPad * pad,
1098     GstURIDecodeBin * decoder)
1099 {
1100   gst_pad_add_event_probe (pad, G_CALLBACK (source_pad_event_probe), decoder);
1101
1102   new_decoded_pad_cb (element, pad, FALSE, decoder);
1103 }
1104
1105 static void
1106 pad_removed_cb (GstElement * element, GstPad * pad, GstURIDecodeBin * decoder)
1107 {
1108   GstPad *ghost;
1109
1110   GST_DEBUG_OBJECT (element, "pad removed name: <%s:%s>",
1111       GST_DEBUG_PAD_NAME (pad));
1112
1113   /* we only care about srcpads */
1114   if (!GST_PAD_IS_SRC (pad))
1115     return;
1116
1117   if (!(ghost = g_object_get_data (G_OBJECT (pad), "uridecodebin.ghostpad")))
1118     goto no_ghost;
1119
1120   /* unghost the pad */
1121   gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (ghost), NULL);
1122
1123   /* deactivate and remove */
1124   gst_pad_set_active (pad, FALSE);
1125   gst_element_remove_pad (GST_ELEMENT_CAST (decoder), ghost);
1126
1127   return;
1128
1129   /* ERRORS */
1130 no_ghost:
1131   {
1132     GST_WARNING_OBJECT (element, "no ghost pad found");
1133     return;
1134   }
1135 }
1136
1137 /* helper function to lookup stuff in lists */
1138 static gboolean
1139 array_has_value (const gchar * values[], const gchar * value)
1140 {
1141   gint i;
1142
1143   for (i = 0; values[i]; i++) {
1144     if (g_str_has_prefix (value, values[i]))
1145       return TRUE;
1146   }
1147   return FALSE;
1148 }
1149
1150 static gboolean
1151 array_has_uri_value (const gchar * values[], const gchar * value)
1152 {
1153   gint i;
1154
1155   for (i = 0; values[i]; i++) {
1156     if (!g_ascii_strncasecmp (value, values[i], strlen (values[i])))
1157       return TRUE;
1158   }
1159   return FALSE;
1160 }
1161
1162 /* list of URIs that we consider to be streams and that need buffering.
1163  * We have no mechanism yet to figure this out with a query. */
1164 static const gchar *stream_uris[] = { "http://", "https://", "mms://",
1165   "mmsh://", "mmsu://", "mmst://", "fd://", "myth://", "ssh://",
1166   "ftp://", "sftp://",
1167   NULL
1168 };
1169
1170 /* list of URIs that need a queue because they are pretty bursty */
1171 static const gchar *queue_uris[] = { "cdda://", NULL };
1172
1173 /* blacklisted URIs, we know they will always fail. */
1174 static const gchar *blacklisted_uris[] = { NULL };
1175
1176 /* mime types that we don't consider to be media types */
1177 #if 0
1178 static const gchar *no_media_mimes[] = {
1179   "application/x-executable", "application/x-bzip", "application/x-gzip",
1180   "application/zip", "application/x-compress", NULL
1181 };
1182 #endif
1183
1184 /* media types we can download */
1185 static const gchar *download_media[] = {
1186   "video/quicktime", "video/mj2", "audio/x-m4a", "application/x-3gp",
1187   "video/x-flv", "video/x-msvideo", "video/webm", NULL
1188 };
1189
1190 #define IS_STREAM_URI(uri)          (array_has_uri_value (stream_uris, uri))
1191 #define IS_QUEUE_URI(uri)           (array_has_uri_value (queue_uris, uri))
1192 #define IS_BLACKLISTED_URI(uri)     (array_has_uri_value (blacklisted_uris, uri))
1193 #define IS_NO_MEDIA_MIME(mime)      (array_has_value (no_media_mimes, mime))
1194 #define IS_DOWNLOAD_MEDIA(media)    (array_has_value (download_media, media))
1195
1196 /*
1197  * Generate and configure a source element.
1198  */
1199 static GstElement *
1200 gen_source_element (GstURIDecodeBin * decoder)
1201 {
1202   GstElement *source;
1203
1204   if (!decoder->uri)
1205     goto no_uri;
1206
1207   GST_LOG_OBJECT (decoder, "finding source for %s", decoder->uri);
1208
1209   if (!gst_uri_is_valid (decoder->uri))
1210     goto invalid_uri;
1211
1212   if (IS_BLACKLISTED_URI (decoder->uri))
1213     goto uri_blacklisted;
1214
1215   source = gst_element_make_from_uri (GST_URI_SRC, decoder->uri, "source");
1216   if (!source)
1217     goto no_source;
1218
1219   GST_LOG_OBJECT (decoder, "found source type %s", G_OBJECT_TYPE_NAME (source));
1220
1221   decoder->is_stream = IS_STREAM_URI (decoder->uri);
1222   GST_LOG_OBJECT (decoder, "source is stream: %d", decoder->is_stream);
1223
1224   decoder->need_queue = IS_QUEUE_URI (decoder->uri);
1225   GST_LOG_OBJECT (decoder, "source needs queue: %d", decoder->need_queue);
1226
1227   /* make HTTP sources send extra headers so we get icecast
1228    * metadata in case the stream is an icecast stream */
1229   if (!strncmp (decoder->uri, "http://", 7) &&
1230       g_object_class_find_property (G_OBJECT_GET_CLASS (source),
1231           "iradio-mode")) {
1232     GST_LOG_OBJECT (decoder, "configuring iradio-mode");
1233     g_object_set (source, "iradio-mode", TRUE, NULL);
1234   }
1235
1236   if (g_object_class_find_property (G_OBJECT_GET_CLASS (source),
1237           "connection-speed")) {
1238     GST_DEBUG_OBJECT (decoder,
1239         "setting connection-speed=%d to source element",
1240         decoder->connection_speed / 1000);
1241     g_object_set (source, "connection-speed",
1242         decoder->connection_speed / 1000, NULL);
1243   }
1244   if (g_object_class_find_property (G_OBJECT_GET_CLASS (source),
1245           "subtitle-encoding")) {
1246     GST_DEBUG_OBJECT (decoder,
1247         "setting subtitle-encoding=%s to source element", decoder->encoding);
1248     g_object_set (source, "subtitle-encoding", decoder->encoding, NULL);
1249   }
1250   return source;
1251
1252   /* ERRORS */
1253 no_uri:
1254   {
1255     GST_ELEMENT_ERROR (decoder, RESOURCE, NOT_FOUND,
1256         (_("No URI specified to play from.")), (NULL));
1257     return NULL;
1258   }
1259 invalid_uri:
1260   {
1261     GST_ELEMENT_ERROR (decoder, RESOURCE, NOT_FOUND,
1262         (_("Invalid URI \"%s\"."), decoder->uri), (NULL));
1263     return NULL;
1264   }
1265 uri_blacklisted:
1266   {
1267     GST_ELEMENT_ERROR (decoder, RESOURCE, FAILED,
1268         (_("This stream type cannot be played yet.")), (NULL));
1269     return NULL;
1270   }
1271 no_source:
1272   {
1273     gchar *prot = gst_uri_get_protocol (decoder->uri);
1274
1275     /* whoops, could not create the source element, dig a little deeper to
1276      * figure out what might be wrong. */
1277     if (prot) {
1278       GstMessage *msg;
1279
1280       msg =
1281           gst_missing_uri_source_message_new (GST_ELEMENT_CAST (decoder), prot);
1282       gst_element_post_message (GST_ELEMENT_CAST (decoder), msg);
1283
1284       GST_ELEMENT_ERROR (decoder, CORE, MISSING_PLUGIN,
1285           (_("No URI handler implemented for \"%s\"."), prot), (NULL));
1286       g_free (prot);
1287     } else
1288       goto invalid_uri;
1289
1290     return NULL;
1291   }
1292 }
1293
1294 /**
1295  * has_all_raw_caps:
1296  * @pad: a #GstPad
1297  * @all_raw: pointer to hold the result
1298  *
1299  * check if the caps of the pad are all raw. The caps are all raw if
1300  * all of its structures contain audio/x-raw or video/x-raw.
1301  *
1302  * Returns: %FALSE @pad has no caps. Else TRUE and @all_raw set t the result.
1303  */
1304 static gboolean
1305 has_all_raw_caps (GstPad * pad, GstCaps * rawcaps, gboolean * all_raw)
1306 {
1307   GstCaps *caps, *intersection;
1308   gint capssize;
1309   gboolean res = FALSE;
1310
1311   caps = gst_pad_get_caps_reffed (pad);
1312   if (caps == NULL)
1313     return FALSE;
1314
1315   GST_DEBUG_OBJECT (pad, "have caps %" GST_PTR_FORMAT, caps);
1316
1317   capssize = gst_caps_get_size (caps);
1318   /* no caps, skip and move to the next pad */
1319   if (capssize == 0 || gst_caps_is_empty (caps) || gst_caps_is_any (caps))
1320     goto done;
1321
1322   intersection = gst_caps_intersect (caps, rawcaps);
1323   *all_raw = !gst_caps_is_empty (intersection)
1324       && (gst_caps_get_size (intersection) == capssize);
1325   gst_caps_unref (intersection);
1326
1327   res = TRUE;
1328
1329 done:
1330   gst_caps_unref (caps);
1331   return res;
1332 }
1333
1334 static void
1335 post_missing_plugin_error (GstElement * dec, const gchar * element_name)
1336 {
1337   GstMessage *msg;
1338
1339   msg = gst_missing_element_message_new (dec, element_name);
1340   gst_element_post_message (dec, msg);
1341
1342   GST_ELEMENT_ERROR (dec, CORE, MISSING_PLUGIN,
1343       (_("Missing element '%s' - check your GStreamer installation."),
1344           element_name), (NULL));
1345 }
1346
1347 /**
1348  * analyse_source:
1349  * @decoder: a #GstURIDecodeBin
1350  * @is_raw: are all pads raw data
1351  * @have_out: does the source have output
1352  * @is_dynamic: is this a dynamic source
1353  * @use_queue: put a queue before raw output pads
1354  *
1355  * Check the source of @decoder and collect information about it.
1356  *
1357  * @is_raw will be set to TRUE if the source only produces raw pads. When this
1358  * function returns, all of the raw pad of the source will be added
1359  * to @decoder.
1360  *
1361  * @have_out: will be set to TRUE if the source has output pads.
1362  *
1363  * @is_dynamic: TRUE if the element will create (more) pads dynamically later
1364  * on.
1365  *
1366  * Returns: FALSE if a fatal error occured while scanning.
1367  */
1368 static gboolean
1369 analyse_source (GstURIDecodeBin * decoder, gboolean * is_raw,
1370     gboolean * have_out, gboolean * is_dynamic, gboolean use_queue)
1371 {
1372   GstIterator *pads_iter;
1373   gboolean done = FALSE;
1374   gboolean res = TRUE;
1375   GstCaps *rawcaps;
1376   GstPad *pad;
1377
1378   *have_out = FALSE;
1379   *is_raw = FALSE;
1380   *is_dynamic = FALSE;
1381
1382   g_object_get (decoder, "caps", &rawcaps, NULL);
1383   if (!rawcaps)
1384     rawcaps = DEFAULT_CAPS;
1385
1386   pads_iter = gst_element_iterate_src_pads (decoder->source);
1387   while (!done) {
1388     switch (gst_iterator_next (pads_iter, (gpointer) & pad)) {
1389       case GST_ITERATOR_ERROR:
1390         res = FALSE;
1391         /* FALLTROUGH */
1392       case GST_ITERATOR_DONE:
1393         done = TRUE;
1394         break;
1395       case GST_ITERATOR_RESYNC:
1396         /* reset results and resync */
1397         *have_out = FALSE;
1398         *is_raw = FALSE;
1399         *is_dynamic = FALSE;
1400         gst_iterator_resync (pads_iter);
1401         break;
1402       case GST_ITERATOR_OK:
1403         /* we now officially have an output pad */
1404         *have_out = TRUE;
1405
1406         /* if FALSE, this pad has no caps and we continue with the next pad. */
1407         if (!has_all_raw_caps (pad, rawcaps, is_raw)) {
1408           gst_object_unref (pad);
1409           break;
1410         }
1411
1412         /* caps on source pad are all raw, we can add the pad */
1413         if (*is_raw) {
1414           GstElement *outelem;
1415
1416           if (use_queue) {
1417             GstPad *sinkpad;
1418
1419             /* insert a queue element right before the raw pad */
1420             outelem = gst_element_factory_make ("queue2", NULL);
1421             if (!outelem)
1422               goto no_queue2;
1423
1424             gst_bin_add (GST_BIN_CAST (decoder), outelem);
1425
1426             sinkpad = gst_element_get_static_pad (outelem, "sink");
1427             gst_pad_link (pad, sinkpad);
1428             gst_object_unref (sinkpad);
1429             gst_object_unref (pad);
1430
1431             /* save queue pointer so we can remove it later */
1432             decoder->queue = outelem;
1433
1434             /* get the new raw srcpad */
1435             pad = gst_element_get_static_pad (outelem, "src");
1436           } else {
1437             outelem = decoder->source;
1438           }
1439           expose_decoded_pad (outelem, pad, decoder);
1440         }
1441         gst_object_unref (pad);
1442         break;
1443     }
1444   }
1445   gst_iterator_free (pads_iter);
1446   gst_caps_unref (rawcaps);
1447
1448   if (!*have_out) {
1449     GstElementClass *elemclass;
1450     GList *walk;
1451
1452     /* element has no output pads, check for padtemplates that list SOMETIMES
1453      * pads. */
1454     elemclass = GST_ELEMENT_GET_CLASS (decoder->source);
1455
1456     walk = gst_element_class_get_pad_template_list (elemclass);
1457     while (walk != NULL) {
1458       GstPadTemplate *templ;
1459
1460       templ = (GstPadTemplate *) walk->data;
1461       if (GST_PAD_TEMPLATE_DIRECTION (templ) == GST_PAD_SRC) {
1462         if (GST_PAD_TEMPLATE_PRESENCE (templ) == GST_PAD_SOMETIMES)
1463           *is_dynamic = TRUE;
1464         break;
1465       }
1466       walk = g_list_next (walk);
1467     }
1468   }
1469
1470   return res;
1471 no_queue2:
1472   {
1473     post_missing_plugin_error (GST_ELEMENT_CAST (decoder), "queue2");
1474
1475     gst_object_unref (pad);
1476     gst_iterator_free (pads_iter);
1477     gst_caps_unref (rawcaps);
1478
1479     return FALSE;
1480   }
1481 }
1482
1483 /* Remove all decodebin2 from ourself 
1484  * If force is FALSE, then the decodebin2 instances will be stored in
1485  * pending_decodebins for re-use later on.
1486  * If force is TRUE, then all decodebin2 instances will be unreferenced
1487  * and cleared, including the pending ones. */
1488 static void
1489 remove_decoders (GstURIDecodeBin * bin, gboolean force)
1490 {
1491   GSList *walk;
1492
1493   for (walk = bin->decodebins; walk; walk = g_slist_next (walk)) {
1494     GstElement *decoder = GST_ELEMENT_CAST (walk->data);
1495
1496     GST_DEBUG_OBJECT (bin, "removing old decoder element");
1497     if (force) {
1498       gst_element_set_state (decoder, GST_STATE_NULL);
1499       gst_bin_remove (GST_BIN_CAST (bin), decoder);
1500     } else {
1501       GstCaps *caps;
1502
1503       gst_element_set_state (decoder, GST_STATE_READY);
1504       /* add it to our list of pending decodebins */
1505       g_object_ref (decoder);
1506       gst_bin_remove (GST_BIN_CAST (bin), decoder);
1507       /* restore some properties we might have changed */
1508       g_object_set (decoder, "sink-caps", NULL, NULL);
1509       caps = DEFAULT_CAPS;
1510       g_object_set (decoder, "caps", caps, NULL);
1511       gst_caps_unref (caps);
1512
1513       bin->pending_decodebins =
1514           g_slist_prepend (bin->pending_decodebins, decoder);
1515     }
1516   }
1517   g_slist_free (bin->decodebins);
1518   bin->decodebins = NULL;
1519   if (force) {
1520     GSList *tmp;
1521
1522     for (tmp = bin->pending_decodebins; tmp; tmp = tmp->next) {
1523       gst_element_set_state ((GstElement *) tmp->data, GST_STATE_NULL);
1524       gst_object_unref ((GstElement *) tmp->data);
1525     }
1526     g_slist_free (bin->pending_decodebins);
1527     bin->pending_decodebins = NULL;
1528
1529   }
1530
1531   /* Don't loose the SOURCE flag */
1532   GST_OBJECT_FLAG_SET (bin, GST_ELEMENT_IS_SOURCE);
1533 }
1534
1535 static void
1536 proxy_unknown_type_signal (GstElement * element, GstPad * pad, GstCaps * caps,
1537     GstURIDecodeBin * dec)
1538 {
1539   GST_DEBUG_OBJECT (dec, "unknown-type signaled");
1540
1541   g_signal_emit (dec,
1542       gst_uri_decode_bin_signals[SIGNAL_UNKNOWN_TYPE], 0, pad, caps);
1543 }
1544
1545 static gboolean
1546 proxy_autoplug_continue_signal (GstElement * element, GstPad * pad,
1547     GstCaps * caps, GstURIDecodeBin * dec)
1548 {
1549   gboolean result;
1550
1551   g_signal_emit (dec,
1552       gst_uri_decode_bin_signals[SIGNAL_AUTOPLUG_CONTINUE], 0, pad, caps,
1553       &result);
1554
1555   GST_DEBUG_OBJECT (dec, "autoplug-continue returned %d", result);
1556
1557   return result;
1558 }
1559
1560 static GValueArray *
1561 proxy_autoplug_factories_signal (GstElement * element, GstPad * pad,
1562     GstCaps * caps, GstURIDecodeBin * dec)
1563 {
1564   GValueArray *result;
1565
1566   g_signal_emit (dec,
1567       gst_uri_decode_bin_signals[SIGNAL_AUTOPLUG_FACTORIES], 0, pad, caps,
1568       &result);
1569
1570   GST_DEBUG_OBJECT (dec, "autoplug-factories returned %p", result);
1571
1572   return result;
1573 }
1574
1575 static GValueArray *
1576 proxy_autoplug_sort_signal (GstElement * element, GstPad * pad,
1577     GstCaps * caps, GValueArray * factories, GstURIDecodeBin * dec)
1578 {
1579   GValueArray *result;
1580
1581   g_signal_emit (dec,
1582       gst_uri_decode_bin_signals[SIGNAL_AUTOPLUG_SORT], 0, pad, caps,
1583       factories, &result);
1584
1585   GST_DEBUG_OBJECT (dec, "autoplug-sort returned %p", result);
1586
1587   return result;
1588 }
1589
1590 static GstAutoplugSelectResult
1591 proxy_autoplug_select_signal (GstElement * element, GstPad * pad,
1592     GstCaps * caps, GstElementFactory * factory, GstURIDecodeBin * dec)
1593 {
1594   GstAutoplugSelectResult result;
1595
1596   g_signal_emit (dec,
1597       gst_uri_decode_bin_signals[SIGNAL_AUTOPLUG_SELECT], 0, pad, caps, factory,
1598       &result);
1599
1600   GST_DEBUG_OBJECT (dec, "autoplug-select returned %d", result);
1601
1602   return result;
1603 }
1604
1605 static void
1606 proxy_drained_signal (GstElement * element, GstURIDecodeBin * dec)
1607 {
1608   GST_DEBUG_OBJECT (dec, "drained signaled");
1609
1610   g_signal_emit (dec, gst_uri_decode_bin_signals[SIGNAL_DRAINED], 0, NULL);
1611 }
1612
1613 /* make a decodebin and connect to all the signals */
1614 static GstElement *
1615 make_decoder (GstURIDecodeBin * decoder)
1616 {
1617   GstElement *decodebin;
1618
1619   /* re-use pending decodebin2 */
1620   if (decoder->pending_decodebins) {
1621     GSList *first = decoder->pending_decodebins;
1622     GST_LOG_OBJECT (decoder, "re-using pending decodebin2");
1623     decodebin = (GstElement *) first->data;
1624     decoder->pending_decodebins =
1625         g_slist_delete_link (decoder->pending_decodebins, first);
1626   } else {
1627     GST_LOG_OBJECT (decoder, "making new decodebin2");
1628
1629     /* now create the decoder element */
1630     decodebin = gst_element_factory_make ("decodebin2", NULL);
1631
1632     if (!decodebin)
1633       goto no_decodebin;
1634
1635     /* sanity check */
1636     if (decodebin->numsinkpads == 0)
1637       goto no_typefind;
1638
1639     /* connect signals to proxy */
1640     g_signal_connect (decodebin, "unknown-type",
1641         G_CALLBACK (proxy_unknown_type_signal), decoder);
1642     g_signal_connect (decodebin, "autoplug-continue",
1643         G_CALLBACK (proxy_autoplug_continue_signal), decoder);
1644     g_signal_connect (decodebin, "autoplug-factories",
1645         G_CALLBACK (proxy_autoplug_factories_signal), decoder);
1646     g_signal_connect (decodebin, "autoplug-sort",
1647         G_CALLBACK (proxy_autoplug_sort_signal), decoder);
1648     g_signal_connect (decodebin, "autoplug-select",
1649         G_CALLBACK (proxy_autoplug_select_signal), decoder);
1650     g_signal_connect (decodebin, "drained",
1651         G_CALLBACK (proxy_drained_signal), decoder);
1652
1653     /* set up callbacks to create the links between decoded data
1654      * and video/audio/subtitle rendering/output. */
1655     g_signal_connect (decodebin,
1656         "new-decoded-pad", G_CALLBACK (new_decoded_pad_cb), decoder);
1657     g_signal_connect (decodebin,
1658         "pad-removed", G_CALLBACK (pad_removed_cb), decoder);
1659     g_signal_connect (decodebin, "no-more-pads",
1660         G_CALLBACK (no_more_pads), decoder);
1661     g_signal_connect (decodebin,
1662         "unknown-type", G_CALLBACK (unknown_type_cb), decoder);
1663   }
1664
1665   /* configure caps if we have any */
1666   if (decoder->caps)
1667     g_object_set (decodebin, "caps", decoder->caps, NULL);
1668
1669   /* Propagate expose-all-streams property */
1670   g_object_set (decodebin, "expose-all-streams", decoder->expose_allstreams,
1671       NULL);
1672
1673   if (!decoder->is_stream) {
1674     /* propagate the use-buffering property but only when we are not already
1675      * doing stream buffering with queue2. FIXME, we might want to do stream
1676      * buffering with the multiqueue buffering instead of queue2. */
1677     g_object_set (decodebin, "use-buffering", decoder->use_buffering, NULL);
1678
1679     if (decoder->use_buffering) {
1680       guint max_bytes;
1681       guint64 max_time;
1682
1683       /* configure sizes when buffering */
1684       if ((max_bytes = decoder->buffer_size) == -1)
1685         max_bytes = 2 * 1024 * 1024;
1686       if ((max_time = decoder->buffer_duration) == -1)
1687         max_time = 2 * GST_SECOND;
1688
1689       g_object_set (decodebin, "max-size-bytes", max_bytes, "max-size-buffers",
1690           (guint) 0, "max-size-time", max_time, NULL);
1691     }
1692   }
1693
1694   g_object_set_data (G_OBJECT (decodebin), "pending", GINT_TO_POINTER (1));
1695   g_object_set (decodebin, "subtitle-encoding", decoder->encoding, NULL);
1696   decoder->pending++;
1697   GST_LOG_OBJECT (decoder, "have %d pending dynamic objects", decoder->pending);
1698
1699   gst_bin_add (GST_BIN_CAST (decoder), decodebin);
1700
1701   decoder->decodebins = g_slist_prepend (decoder->decodebins, decodebin);
1702
1703   return decodebin;
1704
1705   /* ERRORS */
1706 no_decodebin:
1707   {
1708     post_missing_plugin_error (GST_ELEMENT_CAST (decoder), "decodebin2");
1709     GST_ELEMENT_ERROR (decoder, CORE, MISSING_PLUGIN, (NULL),
1710         ("No decodebin2 element, check your installation"));
1711     return NULL;
1712   }
1713 no_typefind:
1714   {
1715     gst_object_unref (decodebin);
1716     GST_ELEMENT_ERROR (decoder, CORE, MISSING_PLUGIN, (NULL),
1717         ("No typefind element, decodebin2 is unusable, check your installation"));
1718     return NULL;
1719   }
1720 }
1721
1722 /* signaled when we have a stream and we need to configure the download
1723  * buffering or regular buffering */
1724 static void
1725 type_found (GstElement * typefind, guint probability,
1726     GstCaps * caps, GstURIDecodeBin * decoder)
1727 {
1728   GstElement *dec_elem, *queue;
1729   GstStructure *s;
1730   const gchar *media_type;
1731
1732   GST_DEBUG_OBJECT (decoder, "typefind found caps %" GST_PTR_FORMAT, caps);
1733
1734   s = gst_caps_get_structure (caps, 0);
1735   media_type = gst_structure_get_name (s);
1736
1737   /* remember if we need download buffering */
1738   decoder->is_download = IS_DOWNLOAD_MEDIA (media_type) && decoder->download;
1739   /* only enable download buffering if the upstream duration is known */
1740   if (decoder->is_download) {
1741     GstFormat fmt = GST_FORMAT_BYTES;
1742     gint64 dur;
1743
1744     decoder->is_download = (gst_element_query_duration (typefind, &fmt, &dur)
1745         && fmt == GST_FORMAT_BYTES && dur != -1);
1746   }
1747
1748   dec_elem = make_decoder (decoder);
1749   if (!dec_elem)
1750     goto no_decodebin;
1751
1752   queue = gst_element_factory_make ("queue2", NULL);
1753   if (!queue)
1754     goto no_queue2;
1755
1756   g_object_set (queue, "use-buffering", TRUE, NULL);
1757   g_object_set (queue, "ring-buffer-max-size", decoder->ring_buffer_max_size,
1758       NULL);
1759
1760   GST_DEBUG_OBJECT (decoder, "check media-type %s, %d", media_type,
1761       decoder->download);
1762
1763   if (decoder->is_download) {
1764     gchar *temp_template, *filename;
1765     const gchar *tmp_dir, *prgname;
1766
1767     tmp_dir = g_get_tmp_dir ();
1768     prgname = g_get_prgname ();
1769     if (prgname == NULL)
1770       prgname = "GStreamer";
1771
1772     filename = g_strdup_printf ("%s-XXXXXX", prgname);
1773
1774     /* build our filename */
1775     temp_template = g_build_filename (tmp_dir, filename, NULL);
1776
1777     GST_DEBUG_OBJECT (decoder, "enable download buffering in %s (%s, %s, %s)",
1778         temp_template, tmp_dir, prgname, filename);
1779
1780     /* configure progressive download for selected media types */
1781     g_object_set (queue, "temp-template", temp_template, NULL);
1782
1783     g_free (filename);
1784     g_free (temp_template);
1785   }
1786
1787   /* Disable max-size-buffers */
1788   g_object_set (queue, "max-size-buffers", 0, NULL);
1789
1790   /* If buffer size or duration are set, set them on the queue2 element */
1791   if (decoder->buffer_size != -1)
1792     g_object_set (queue, "max-size-bytes", decoder->buffer_size, NULL);
1793   if (decoder->buffer_duration != -1)
1794     g_object_set (queue, "max-size-time", decoder->buffer_duration, NULL);
1795
1796   gst_bin_add (GST_BIN_CAST (decoder), queue);
1797
1798   if (!gst_element_link_pads (typefind, "src", queue, "sink"))
1799     goto could_not_link;
1800
1801   /* to force caps on the decodebin element and avoid reparsing stuff by
1802    * typefind. It also avoids a deadlock in the way typefind activates pads in
1803    * the state change */
1804   g_object_set (dec_elem, "sink-caps", caps, NULL);
1805
1806   if (!gst_element_link_pads (queue, "src", dec_elem, "sink"))
1807     goto could_not_link;
1808
1809   /* PLAYING in one go might fail (see bug #632782) */
1810   gst_element_set_state (dec_elem, GST_STATE_PAUSED);
1811   gst_element_set_state (dec_elem, GST_STATE_PLAYING);
1812   gst_element_set_state (queue, GST_STATE_PLAYING);
1813
1814   do_async_done (decoder);
1815
1816   return;
1817
1818   /* ERRORS */
1819 no_decodebin:
1820   {
1821     /* error was posted */
1822     return;
1823   }
1824 could_not_link:
1825   {
1826     GST_ELEMENT_ERROR (decoder, CORE, NEGOTIATION,
1827         (NULL), ("Can't link typefind to decodebin2 element"));
1828     return;
1829   }
1830 no_queue2:
1831   {
1832     post_missing_plugin_error (GST_ELEMENT_CAST (decoder), "queue2");
1833     return;
1834   }
1835 }
1836
1837 /* setup a streaming source. This will first plug a typefind element to the
1838  * source. After we find the type, we decide to plug a queue2 and continue to
1839  * plug a decodebin2 starting from the found caps */
1840 static gboolean
1841 setup_streaming (GstURIDecodeBin * decoder)
1842 {
1843   GstElement *typefind;
1844
1845   /* now create the decoder element */
1846   typefind = gst_element_factory_make ("typefind", NULL);
1847   if (!typefind)
1848     goto no_typefind;
1849
1850   gst_bin_add (GST_BIN_CAST (decoder), typefind);
1851
1852   if (!gst_element_link_pads (decoder->source, NULL, typefind, "sink"))
1853     goto could_not_link;
1854
1855   decoder->typefind = typefind;
1856
1857   /* connect a signal to find out when the typefind element found
1858    * a type */
1859   decoder->have_type_id =
1860       g_signal_connect (decoder->typefind, "have-type",
1861       G_CALLBACK (type_found), decoder);
1862
1863   do_async_start (decoder);
1864
1865   return TRUE;
1866
1867   /* ERRORS */
1868 no_typefind:
1869   {
1870     post_missing_plugin_error (GST_ELEMENT_CAST (decoder), "typefind");
1871     GST_ELEMENT_ERROR (decoder, CORE, MISSING_PLUGIN, (NULL),
1872         ("No typefind element, check your installation"));
1873     return FALSE;
1874   }
1875 could_not_link:
1876   {
1877     GST_ELEMENT_ERROR (decoder, CORE, NEGOTIATION,
1878         (NULL), ("Can't link source to typefind element"));
1879     gst_bin_remove (GST_BIN_CAST (decoder), typefind);
1880     /* Don't loose the SOURCE flag */
1881     GST_OBJECT_FLAG_SET (decoder, GST_ELEMENT_IS_SOURCE);
1882     return FALSE;
1883   }
1884 }
1885
1886 static void
1887 free_stream (gpointer value)
1888 {
1889   g_slice_free (GstURIDecodeBinStream, value);
1890 }
1891
1892 /* remove source and all related elements */
1893 static void
1894 remove_source (GstURIDecodeBin * bin)
1895 {
1896   GstElement *source = bin->source;
1897
1898   if (source) {
1899     GST_DEBUG_OBJECT (bin, "removing old src element");
1900     gst_element_set_state (source, GST_STATE_NULL);
1901
1902     if (bin->src_np_sig_id) {
1903       g_signal_handler_disconnect (source, bin->src_np_sig_id);
1904       bin->src_np_sig_id = 0;
1905     }
1906     if (bin->src_nmp_sig_id) {
1907       g_signal_handler_disconnect (source, bin->src_nmp_sig_id);
1908       bin->src_nmp_sig_id = 0;
1909     }
1910     gst_bin_remove (GST_BIN_CAST (bin), source);
1911     bin->source = NULL;
1912   }
1913   if (bin->queue) {
1914     GST_DEBUG_OBJECT (bin, "removing old queue element");
1915     gst_element_set_state (bin->queue, GST_STATE_NULL);
1916     gst_bin_remove (GST_BIN_CAST (bin), bin->queue);
1917     bin->queue = NULL;
1918   }
1919   if (bin->typefind) {
1920     GST_DEBUG_OBJECT (bin, "removing old typefind element");
1921     gst_element_set_state (bin->typefind, GST_STATE_NULL);
1922     gst_bin_remove (GST_BIN_CAST (bin), bin->typefind);
1923     bin->typefind = NULL;
1924   }
1925   if (bin->streams) {
1926     g_hash_table_destroy (bin->streams);
1927     bin->streams = NULL;
1928   }
1929   /* Don't loose the SOURCE flag */
1930   GST_OBJECT_FLAG_SET (bin, GST_ELEMENT_IS_SOURCE);
1931 }
1932
1933 /* is called when a dynamic source element created a new pad. */
1934 static void
1935 source_new_pad (GstElement * element, GstPad * pad, GstURIDecodeBin * bin)
1936 {
1937   GstElement *decoder;
1938   gboolean is_raw;
1939   GstCaps *rawcaps;
1940
1941   GST_URI_DECODE_BIN_LOCK (bin);
1942   GST_DEBUG_OBJECT (bin, "Found new pad %s.%s in source element %s",
1943       GST_DEBUG_PAD_NAME (pad), GST_ELEMENT_NAME (element));
1944
1945   g_object_get (bin, "caps", &rawcaps, NULL);
1946   if (!rawcaps)
1947     rawcaps = DEFAULT_CAPS;
1948
1949   /* if this is a pad with all raw caps, we can expose it */
1950   if (has_all_raw_caps (pad, rawcaps, &is_raw) && is_raw) {
1951     /* it's all raw, create output pads. */
1952     GST_URI_DECODE_BIN_UNLOCK (bin);
1953     gst_caps_unref (rawcaps);
1954     expose_decoded_pad (element, pad, bin);
1955     return;
1956   }
1957   gst_caps_unref (rawcaps);
1958
1959   /* not raw, create decoder */
1960   decoder = make_decoder (bin);
1961   if (!decoder)
1962     goto no_decodebin;
1963
1964   /* and link to decoder */
1965   if (!gst_element_link_pads (bin->source, NULL, decoder, "sink"))
1966     goto could_not_link;
1967
1968   GST_DEBUG_OBJECT (bin, "linked decoder to new pad");
1969
1970   gst_element_set_state (decoder, GST_STATE_PLAYING);
1971   GST_URI_DECODE_BIN_UNLOCK (bin);
1972
1973   return;
1974
1975   /* ERRORS */
1976 no_decodebin:
1977   {
1978     /* error was posted */
1979     GST_URI_DECODE_BIN_UNLOCK (bin);
1980     return;
1981   }
1982 could_not_link:
1983   {
1984     GST_ELEMENT_ERROR (bin, CORE, NEGOTIATION,
1985         (NULL), ("Can't link source to decoder element"));
1986     GST_URI_DECODE_BIN_UNLOCK (bin);
1987     return;
1988   }
1989 }
1990
1991 /* construct and run the source and decoder elements until we found
1992  * all the streams or until a preroll queue has been filled.
1993 */
1994 static gboolean
1995 setup_source (GstURIDecodeBin * decoder)
1996 {
1997   gboolean is_raw, have_out, is_dynamic;
1998
1999   GST_DEBUG_OBJECT (decoder, "setup source");
2000
2001   /* delete old src */
2002   remove_source (decoder);
2003
2004   decoder->pending = 0;
2005
2006   /* create and configure an element that can handle the uri */
2007   if (!(decoder->source = gen_source_element (decoder)))
2008     goto no_source;
2009
2010   /* state will be merged later - if file is not found, error will be
2011    * handled by the application right after. */
2012   gst_bin_add (GST_BIN_CAST (decoder), decoder->source);
2013
2014   /* notify of the new source used */
2015   g_object_notify (G_OBJECT (decoder), "source");
2016
2017   g_signal_emit (decoder, gst_uri_decode_bin_signals[SIGNAL_SOURCE_SETUP],
2018       0, decoder->source);
2019
2020   /* remove the old decoders now, if any */
2021   remove_decoders (decoder, FALSE);
2022
2023   /* stream admin setup */
2024   decoder->streams = g_hash_table_new_full (NULL, NULL, NULL, free_stream);
2025
2026   /* see if the source element emits raw audio/video all by itself,
2027    * if so, we can create streams for the pads and be done with it.
2028    * Also check that is has source pads, if not, we assume it will
2029    * do everything itself.  */
2030   if (!analyse_source (decoder, &is_raw, &have_out, &is_dynamic,
2031           decoder->need_queue))
2032     goto invalid_source;
2033
2034   if (is_raw) {
2035     GST_DEBUG_OBJECT (decoder, "Source provides all raw data");
2036     /* source provides raw data, we added the pads and we can now signal a
2037      * no_more pads because we are done. */
2038     gst_element_no_more_pads (GST_ELEMENT_CAST (decoder));
2039     return TRUE;
2040   }
2041   if (!have_out && !is_dynamic) {
2042     GST_DEBUG_OBJECT (decoder, "Source has no output pads");
2043     /* create a stream to indicate that this uri is handled by a self
2044      * contained element. We are now done. */
2045     add_element_stream (decoder->source, decoder);
2046     return TRUE;
2047   }
2048   if (is_dynamic) {
2049     GST_DEBUG_OBJECT (decoder, "Source has dynamic output pads");
2050     /* connect a handler for the new-pad signal */
2051     decoder->src_np_sig_id =
2052         g_signal_connect (decoder->source, "pad-added",
2053         G_CALLBACK (source_new_pad), decoder);
2054     decoder->src_nmp_sig_id =
2055         g_signal_connect (decoder->source, "no-more-pads",
2056         G_CALLBACK (source_no_more_pads), decoder);
2057     g_object_set_data (G_OBJECT (decoder->source), "pending",
2058         GINT_TO_POINTER (1));
2059     decoder->pending++;
2060   } else {
2061     if (decoder->is_stream) {
2062       GST_DEBUG_OBJECT (decoder, "Setting up streaming");
2063       /* do the stream things here */
2064       if (!setup_streaming (decoder))
2065         goto streaming_failed;
2066     } else {
2067       GstElement *dec_elem;
2068
2069       /* no streaming source, we can link now */
2070       GST_DEBUG_OBJECT (decoder, "Plugging decodebin to source");
2071
2072       dec_elem = make_decoder (decoder);
2073       if (!dec_elem)
2074         goto no_decoder;
2075
2076       if (!gst_element_link_pads (decoder->source, NULL, dec_elem, "sink"))
2077         goto could_not_link;
2078     }
2079   }
2080   return TRUE;
2081
2082   /* ERRORS */
2083 no_source:
2084   {
2085     /* error message was already posted */
2086     return FALSE;
2087   }
2088 invalid_source:
2089   {
2090     GST_ELEMENT_ERROR (decoder, CORE, FAILED,
2091         (_("Source element is invalid.")), (NULL));
2092     return FALSE;
2093   }
2094 no_decoder:
2095   {
2096     /* message was posted */
2097     return FALSE;
2098   }
2099 streaming_failed:
2100   {
2101     /* message was posted */
2102     return FALSE;
2103   }
2104 could_not_link:
2105   {
2106     GST_ELEMENT_ERROR (decoder, CORE, NEGOTIATION,
2107         (NULL), ("Can't link source to decoder element"));
2108     return FALSE;
2109   }
2110 }
2111
2112 static void
2113 value_list_append_structure_list (GValue * list_val, GstStructure ** first,
2114     GList * structure_list)
2115 {
2116   GList *l;
2117
2118   for (l = structure_list; l != NULL; l = l->next) {
2119     GValue val = { 0, };
2120
2121     if (*first == NULL)
2122       *first = gst_structure_copy ((GstStructure *) l->data);
2123
2124     g_value_init (&val, GST_TYPE_STRUCTURE);
2125     g_value_take_boxed (&val, gst_structure_copy ((GstStructure *) l->data));
2126     gst_value_list_append_value (list_val, &val);
2127     g_value_unset (&val);
2128   }
2129 }
2130
2131 /* if it's a redirect message with multiple redirect locations we might
2132  * want to pick a different 'best' location depending on the required
2133  * bitrates and the connection speed */
2134 static GstMessage *
2135 handle_redirect_message (GstURIDecodeBin * dec, GstMessage * msg)
2136 {
2137   const GValue *locations_list, *location_val;
2138   GstMessage *new_msg;
2139   GstStructure *new_structure = NULL;
2140   GList *l_good = NULL, *l_neutral = NULL, *l_bad = NULL;
2141   GValue new_list = { 0, };
2142   guint size, i;
2143
2144   GST_DEBUG_OBJECT (dec, "redirect message: %" GST_PTR_FORMAT, msg);
2145   GST_DEBUG_OBJECT (dec, "connection speed: %u", dec->connection_speed);
2146
2147   if (dec->connection_speed == 0 || msg->structure == NULL)
2148     return msg;
2149
2150   locations_list = gst_structure_get_value (msg->structure, "locations");
2151   if (locations_list == NULL)
2152     return msg;
2153
2154   size = gst_value_list_get_size (locations_list);
2155   if (size < 2)
2156     return msg;
2157
2158   /* maintain existing order as much as possible, just sort references
2159    * with too high a bitrate to the end (the assumption being that if
2160    * bitrates are given they are given for all interesting streams and
2161    * that the you-need-at-least-version-xyz redirect has the same bitrate
2162    * as the lowest referenced redirect alternative) */
2163   for (i = 0; i < size; ++i) {
2164     const GstStructure *s;
2165     gint bitrate = 0;
2166
2167     location_val = gst_value_list_get_value (locations_list, i);
2168     s = (const GstStructure *) g_value_get_boxed (location_val);
2169     if (!gst_structure_get_int (s, "minimum-bitrate", &bitrate) || bitrate <= 0) {
2170       GST_DEBUG_OBJECT (dec, "no bitrate: %" GST_PTR_FORMAT, s);
2171       l_neutral = g_list_append (l_neutral, (gpointer) s);
2172     } else if (bitrate > dec->connection_speed) {
2173       GST_DEBUG_OBJECT (dec, "bitrate too high: %" GST_PTR_FORMAT, s);
2174       l_bad = g_list_append (l_bad, (gpointer) s);
2175     } else if (bitrate <= dec->connection_speed) {
2176       GST_DEBUG_OBJECT (dec, "bitrate OK: %" GST_PTR_FORMAT, s);
2177       l_good = g_list_append (l_good, (gpointer) s);
2178     }
2179   }
2180
2181   g_value_init (&new_list, GST_TYPE_LIST);
2182   value_list_append_structure_list (&new_list, &new_structure, l_good);
2183   value_list_append_structure_list (&new_list, &new_structure, l_neutral);
2184   value_list_append_structure_list (&new_list, &new_structure, l_bad);
2185   gst_structure_set_value (new_structure, "locations", &new_list);
2186   g_value_unset (&new_list);
2187
2188   g_list_free (l_good);
2189   g_list_free (l_neutral);
2190   g_list_free (l_bad);
2191
2192   new_msg = gst_message_new_element (msg->src, new_structure);
2193   gst_message_unref (msg);
2194
2195   GST_DEBUG_OBJECT (dec, "new redirect message: %" GST_PTR_FORMAT, new_msg);
2196   return new_msg;
2197 }
2198
2199 static void
2200 handle_message (GstBin * bin, GstMessage * msg)
2201 {
2202   if (GST_MESSAGE_TYPE (msg) == GST_MESSAGE_ELEMENT && msg->structure != NULL
2203       && gst_structure_has_name (msg->structure, "redirect")) {
2204     /* sort redirect messages based on the connection speed. This simplifies
2205      * the user of this element as it can in most cases just pick the first item
2206      * of the sorted list as a good redirection candidate. It can of course
2207      * choose something else from the list if it has a better way. */
2208     msg = handle_redirect_message (GST_URI_DECODE_BIN (bin), msg);
2209   }
2210   GST_BIN_CLASS (parent_class)->handle_message (bin, msg);
2211 }
2212
2213 /* generic struct passed to all query fold methods
2214  * FIXME, move to core.
2215  */
2216 typedef struct
2217 {
2218   GstQuery *query;
2219   gint64 min;
2220   gint64 max;
2221   gboolean seekable;
2222   gboolean live;
2223 } QueryFold;
2224
2225 typedef void (*QueryInitFunction) (GstURIDecodeBin * dec, QueryFold * fold);
2226 typedef void (*QueryDoneFunction) (GstURIDecodeBin * dec, QueryFold * fold);
2227
2228 /* for duration/position we collect all durations/positions and take
2229  * the MAX of all valid results */
2230 static void
2231 decoder_query_init (GstURIDecodeBin * dec, QueryFold * fold)
2232 {
2233   fold->min = 0;
2234   fold->max = -1;
2235   fold->seekable = TRUE;
2236   fold->live = 0;
2237 }
2238
2239 static gboolean
2240 decoder_query_duration_fold (GstPad * item, GValue * ret, QueryFold * fold)
2241 {
2242   if (gst_pad_query (item, fold->query)) {
2243     gint64 duration;
2244
2245     g_value_set_boolean (ret, TRUE);
2246
2247     gst_query_parse_duration (fold->query, NULL, &duration);
2248
2249     GST_DEBUG_OBJECT (item, "got duration %" G_GINT64_FORMAT, duration);
2250
2251     if (duration > fold->max)
2252       fold->max = duration;
2253   }
2254   gst_object_unref (item);
2255   return TRUE;
2256 }
2257
2258 static void
2259 decoder_query_duration_done (GstURIDecodeBin * dec, QueryFold * fold)
2260 {
2261   GstFormat format;
2262
2263   gst_query_parse_duration (fold->query, &format, NULL);
2264   /* store max in query result */
2265   gst_query_set_duration (fold->query, format, fold->max);
2266
2267   GST_DEBUG ("max duration %" G_GINT64_FORMAT, fold->max);
2268 }
2269
2270 static gboolean
2271 decoder_query_position_fold (GstPad * item, GValue * ret, QueryFold * fold)
2272 {
2273   if (gst_pad_query (item, fold->query)) {
2274     gint64 position;
2275
2276     g_value_set_boolean (ret, TRUE);
2277
2278     gst_query_parse_position (fold->query, NULL, &position);
2279
2280     GST_DEBUG_OBJECT (item, "got position %" G_GINT64_FORMAT, position);
2281
2282     if (position > fold->max)
2283       fold->max = position;
2284   }
2285
2286   gst_object_unref (item);
2287   return TRUE;
2288 }
2289
2290 static void
2291 decoder_query_position_done (GstURIDecodeBin * dec, QueryFold * fold)
2292 {
2293   GstFormat format;
2294
2295   gst_query_parse_position (fold->query, &format, NULL);
2296   /* store max in query result */
2297   gst_query_set_position (fold->query, format, fold->max);
2298
2299   GST_DEBUG_OBJECT (dec, "max position %" G_GINT64_FORMAT, fold->max);
2300 }
2301
2302 static gboolean
2303 decoder_query_latency_fold (GstPad * item, GValue * ret, QueryFold * fold)
2304 {
2305   if (gst_pad_query (item, fold->query)) {
2306     GstClockTime min, max;
2307     gboolean live;
2308
2309     g_value_set_boolean (ret, TRUE);
2310
2311     gst_query_parse_latency (fold->query, &live, &min, &max);
2312
2313     GST_DEBUG_OBJECT (item,
2314         "got latency min %" GST_TIME_FORMAT ", max %" GST_TIME_FORMAT
2315         ", live %d", GST_TIME_ARGS (min), GST_TIME_ARGS (max), live);
2316
2317     /* for the combined latency we collect the MAX of all min latencies and
2318      * the MIN of all max latencies */
2319     if (min > fold->min)
2320       fold->min = min;
2321     if (fold->max == -1)
2322       fold->max = max;
2323     else if (max < fold->max)
2324       fold->max = max;
2325     if (fold->live == FALSE)
2326       fold->live = live;
2327   }
2328
2329   gst_object_unref (item);
2330   return TRUE;
2331 }
2332
2333 static void
2334 decoder_query_latency_done (GstURIDecodeBin * dec, QueryFold * fold)
2335 {
2336   /* store max in query result */
2337   gst_query_set_latency (fold->query, fold->live, fold->min, fold->max);
2338
2339   GST_DEBUG_OBJECT (dec,
2340       "latency min %" GST_TIME_FORMAT ", max %" GST_TIME_FORMAT
2341       ", live %d", GST_TIME_ARGS (fold->min), GST_TIME_ARGS (fold->max),
2342       fold->live);
2343 }
2344
2345 /* we are seekable if all srcpads are seekable */
2346 static gboolean
2347 decoder_query_seeking_fold (GstPad * item, GValue * ret, QueryFold * fold)
2348 {
2349   if (gst_pad_query (item, fold->query)) {
2350     gboolean seekable;
2351
2352     g_value_set_boolean (ret, TRUE);
2353     gst_query_parse_seeking (fold->query, NULL, &seekable, NULL, NULL);
2354
2355     GST_DEBUG_OBJECT (item, "got seekable %d", seekable);
2356
2357     if (fold->seekable == TRUE)
2358       fold->seekable = seekable;
2359   }
2360   gst_object_unref (item);
2361
2362   return TRUE;
2363 }
2364
2365 static void
2366 decoder_query_seeking_done (GstURIDecodeBin * dec, QueryFold * fold)
2367 {
2368   GstFormat format;
2369
2370   gst_query_parse_seeking (fold->query, &format, NULL, NULL, NULL);
2371   gst_query_set_seeking (fold->query, format, fold->seekable, 0, -1);
2372
2373   GST_DEBUG_OBJECT (dec, "seekable %d", fold->seekable);
2374 }
2375
2376 /* generic fold, return first valid result */
2377 static gboolean
2378 decoder_query_generic_fold (GstPad * item, GValue * ret, QueryFold * fold)
2379 {
2380   gboolean res;
2381
2382   if ((res = gst_pad_query (item, fold->query))) {
2383     g_value_set_boolean (ret, TRUE);
2384     GST_DEBUG_OBJECT (item, "answered query %p", fold->query);
2385   }
2386
2387   gst_object_unref (item);
2388
2389   /* and stop as soon as we have a valid result */
2390   return !res;
2391 }
2392
2393
2394 /* we're a bin, the default query handler iterates sink elements, which we don't
2395  * have normally. We should just query all source pads.
2396  */
2397 static gboolean
2398 gst_uri_decode_bin_query (GstElement * element, GstQuery * query)
2399 {
2400   GstURIDecodeBin *decoder;
2401   gboolean res = FALSE;
2402   GstIterator *iter;
2403   GstIteratorFoldFunction fold_func;
2404   QueryInitFunction fold_init = NULL;
2405   QueryDoneFunction fold_done = NULL;
2406   QueryFold fold_data;
2407   GValue ret = { 0 };
2408
2409   decoder = GST_URI_DECODE_BIN (element);
2410
2411   switch (GST_QUERY_TYPE (query)) {
2412     case GST_QUERY_DURATION:
2413       /* iterate and collect durations */
2414       fold_func = (GstIteratorFoldFunction) decoder_query_duration_fold;
2415       fold_init = decoder_query_init;
2416       fold_done = decoder_query_duration_done;
2417       break;
2418     case GST_QUERY_POSITION:
2419       /* iterate and collect durations */
2420       fold_func = (GstIteratorFoldFunction) decoder_query_position_fold;
2421       fold_init = decoder_query_init;
2422       fold_done = decoder_query_position_done;
2423       break;
2424     case GST_QUERY_LATENCY:
2425       /* iterate and collect durations */
2426       fold_func = (GstIteratorFoldFunction) decoder_query_latency_fold;
2427       fold_init = decoder_query_init;
2428       fold_done = decoder_query_latency_done;
2429       break;
2430     case GST_QUERY_SEEKING:
2431       /* iterate and collect durations */
2432       fold_func = (GstIteratorFoldFunction) decoder_query_seeking_fold;
2433       fold_init = decoder_query_init;
2434       fold_done = decoder_query_seeking_done;
2435       break;
2436     default:
2437       fold_func = (GstIteratorFoldFunction) decoder_query_generic_fold;
2438       break;
2439   }
2440
2441   fold_data.query = query;
2442
2443   g_value_init (&ret, G_TYPE_BOOLEAN);
2444   g_value_set_boolean (&ret, FALSE);
2445
2446   iter = gst_element_iterate_src_pads (element);
2447   GST_DEBUG_OBJECT (element, "Sending query %p (type %d) to src pads",
2448       query, GST_QUERY_TYPE (query));
2449
2450   if (fold_init)
2451     fold_init (decoder, &fold_data);
2452
2453   while (TRUE) {
2454     GstIteratorResult ires;
2455
2456     ires = gst_iterator_fold (iter, fold_func, &ret, &fold_data);
2457
2458     switch (ires) {
2459       case GST_ITERATOR_RESYNC:
2460         gst_iterator_resync (iter);
2461         if (fold_init)
2462           fold_init (decoder, &fold_data);
2463         g_value_set_boolean (&ret, FALSE);
2464         break;
2465       case GST_ITERATOR_OK:
2466       case GST_ITERATOR_DONE:
2467         res = g_value_get_boolean (&ret);
2468         if (fold_done != NULL && res)
2469           fold_done (decoder, &fold_data);
2470         goto done;
2471       default:
2472         res = FALSE;
2473         goto done;
2474     }
2475   }
2476 done:
2477   gst_iterator_free (iter);
2478
2479   return res;
2480 }
2481
2482 static GstStateChangeReturn
2483 gst_uri_decode_bin_change_state (GstElement * element,
2484     GstStateChange transition)
2485 {
2486   GstStateChangeReturn ret;
2487   GstURIDecodeBin *decoder;
2488
2489   decoder = GST_URI_DECODE_BIN (element);
2490
2491   switch (transition) {
2492     case GST_STATE_CHANGE_READY_TO_PAUSED:
2493       if (!setup_source (decoder))
2494         goto source_failed;
2495       break;
2496     default:
2497       break;
2498   }
2499
2500   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
2501
2502   switch (transition) {
2503     case GST_STATE_CHANGE_READY_TO_PAUSED:
2504       GST_DEBUG ("ready to paused");
2505       if (ret == GST_STATE_CHANGE_FAILURE)
2506         goto setup_failed;
2507       break;
2508     case GST_STATE_CHANGE_PAUSED_TO_READY:
2509       GST_DEBUG ("paused to ready");
2510       remove_decoders (decoder, FALSE);
2511       remove_source (decoder);
2512       do_async_done (decoder);
2513       break;
2514     case GST_STATE_CHANGE_READY_TO_NULL:
2515       GST_DEBUG ("ready to null");
2516       remove_decoders (decoder, TRUE);
2517       remove_source (decoder);
2518       break;
2519     default:
2520       break;
2521   }
2522   return ret;
2523
2524   /* ERRORS */
2525 source_failed:
2526   {
2527     return GST_STATE_CHANGE_FAILURE;
2528   }
2529 setup_failed:
2530   {
2531     /* clean up leftover groups */
2532     return GST_STATE_CHANGE_FAILURE;
2533   }
2534 }
2535
2536 gboolean gst_decode_bin_plugin_init (GstPlugin * plugin);
2537
2538 static gboolean
2539 gst_uri_decode_bin_plugin_init (GstPlugin * plugin)
2540 {
2541   GST_DEBUG_CATEGORY_INIT (gst_uri_decode_bin_debug, "uridecodebin", 0,
2542       "URI decoder element");
2543
2544 #ifdef ENABLE_NLS
2545   GST_DEBUG ("binding text domain %s to locale dir %s", GETTEXT_PACKAGE,
2546       LOCALEDIR);
2547   bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
2548   bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
2549 #endif /* ENABLE_NLS */
2550
2551   return gst_element_register (plugin, "uridecodebin", GST_RANK_NONE,
2552       GST_TYPE_URI_DECODE_BIN);
2553 }
2554
2555 static gboolean
2556 plugin_init (GstPlugin * plugin)
2557 {
2558   if (!gst_decode_bin_plugin_init (plugin))
2559     return FALSE;
2560   if (!gst_uri_decode_bin_plugin_init (plugin))
2561     return FALSE;
2562
2563   return TRUE;
2564 }
2565
2566 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
2567     GST_VERSION_MINOR,
2568     "uridecodebin",
2569     "URI Decoder bin", plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME,
2570     GST_PACKAGE_ORIGIN)