Add -Wmissing-declarations -Wmissing-prototypes to warning flags
[platform/upstream/gstreamer.git] / gst / playback / gsturidecodebin.c
index 90d031a..b19459b 100644 (file)
@@ -19,9 +19,9 @@
 
 /**
  * SECTION:element-uridecodebin
- * @short_description: decoder for an uri
  *
- * Decodes data from a URI into raw media.
+ * Decodes data from a URI into raw media. It selects a source element that can
+ * handle the given #GstURIDecodeBin:uri scheme and connects it to a decodebin2.
  */
 
 #ifdef HAVE_CONFIG_H
 
 #include <gst/gst.h>
 #include <gst/gst-i18n-plugin.h>
+#include <gst/pbutils/missing-plugins.h>
 
+#include "gstfactorylists.h"
 #include "gstplay-marshal.h"
+#include "gstplay-enum.h"
+#include "gstrawcaps.h"
 
 #define GST_TYPE_URI_DECODE_BIN \
   (gst_uri_decode_bin_get_type())
   (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_URI_DECODE_BIN))
 #define GST_IS_URI_DECODE_BIN_CLASS(klass) \
   (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_URI_DECODE_BIN))
+#define GST_URI_DECODE_BIN_CAST(obj) ((GstURIDecodeBin *) (obj))
 
 typedef struct _GstURIDecodeBin GstURIDecodeBin;
 typedef struct _GstURIDecodeBinClass GstURIDecodeBinClass;
 
+#define GST_URI_DECODE_BIN_GET_LOCK(dec) (((GstURIDecodeBin*)(dec))->lock)
+#define GST_URI_DECODE_BIN_LOCK(dec) (g_mutex_lock(GST_URI_DECODE_BIN_GET_LOCK(dec)))
+#define GST_URI_DECODE_BIN_UNLOCK(dec) (g_mutex_unlock(GST_URI_DECODE_BIN_GET_LOCK(dec)))
+
+/**
+ * GstURIDecodeBin
+ *
+ * uridecodebin element struct
+ */
 struct _GstURIDecodeBin
 {
   GstBin parent_instance;
 
+  GMutex *lock;                 /* lock for constructing */
+
+  GMutex *factories_lock;
+  guint32 factories_cookie;
+  GValueArray *factories;       /* factories we can use for selecting elements */
+
   gchar *uri;
   guint connection_speed;
   GstCaps *caps;
   gchar *encoding;
 
   gboolean is_stream;
+  gboolean is_download;
+  gboolean need_queue;
+  guint64 buffer_duration;      /* When buffering, buffer duration (ns) */
+  guint buffer_size;            /* When buffering, buffer size (bytes) */
+  gboolean download;
+  gboolean use_buffering;
+
   GstElement *source;
   GstElement *queue;
-  GSList *decoders;
+  GstElement *typefind;
+  guint have_type_id;           /* have-type signal id from typefind */
+  GSList *decodebins;
+  GSList *pending_decodebins;
   GSList *srcpads;
   gint numpads;
 
@@ -69,6 +99,8 @@ struct _GstURIDecodeBin
   guint src_np_sig_id;          /* new-pad signal id */
   guint src_nmp_sig_id;         /* no-more-pads signal id */
   gint pending;
+
+  gboolean async_pending;       /* async-start has been emited */
 };
 
 struct _GstURIDecodeBinClass
@@ -85,8 +117,8 @@ struct _GstURIDecodeBinClass
   GValueArray *(*autoplug_factories) (GstElement * element, GstPad * pad,
       GstCaps * caps);
   /* signal fired to select from the proposed list of factories */
-    gint (*autoplug_select) (GstElement * element, GstPad * pad, GstCaps * caps,
-      GValueArray * factories);
+    GstAutoplugSelectResult (*autoplug_select) (GstElement * element,
+      GstPad * pad, GstCaps * caps, GValueArray * factories);
 
   /* emited when all data is decoded */
   void (*drained) (GstElement * element);
@@ -97,6 +129,8 @@ static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src%d",
     GST_PAD_SOMETIMES,
     GST_STATIC_CAPS_ANY);
 
+static GstStaticCaps default_raw_caps = GST_STATIC_CAPS (DEFAULT_RAW_CAPS);
+
 GST_DEBUG_CATEGORY_STATIC (gst_uri_decode_bin_debug);
 #define GST_CAT_DEFAULT gst_uri_decode_bin_debug
 
@@ -118,25 +152,37 @@ enum
 };
 
 /* properties */
-#define DEFAULT_PROP_URI           NULL
+#define DEFAULT_PROP_URI            NULL
+#define DEFAULT_PROP_SOURCE         NULL
 #define DEFAULT_CONNECTION_SPEED    0
-#define DEFAULT_CAPS                NULL
+#define DEFAULT_CAPS                (gst_static_caps_get (&default_raw_caps))
 #define DEFAULT_SUBTITLE_ENCODING   NULL
+#define DEFAULT_BUFFER_DURATION     -1
+#define DEFAULT_BUFFER_SIZE         -1
+#define DEFAULT_DOWNLOAD            FALSE
+#define DEFAULT_USE_BUFFERING       FALSE
 
 enum
 {
   PROP_0,
   PROP_URI,
+  PROP_SOURCE,
   PROP_CONNECTION_SPEED,
   PROP_CAPS,
   PROP_SUBTITLE_ENCODING,
+  PROP_BUFFER_SIZE,
+  PROP_BUFFER_DURATION,
+  PROP_DOWNLOAD,
+  PROP_USE_BUFFERING,
   PROP_LAST
 };
 
 static guint gst_uri_decode_bin_signals[LAST_SIGNAL] = { 0 };
 
+GType gst_uri_decode_bin_get_type (void);
 GST_BOILERPLATE (GstURIDecodeBin, gst_uri_decode_bin, GstBin, GST_TYPE_BIN);
 
+static void remove_decoders (GstURIDecodeBin * bin, gboolean force);
 static void gst_uri_decode_bin_set_property (GObject * object, guint prop_id,
     const GValue * value, GParamSpec * pspec);
 static void gst_uri_decode_bin_get_property (GObject * object, guint prop_id,
@@ -175,6 +221,32 @@ _gst_boolean_accumulator (GSignalInvocationHint * ihint,
 }
 
 static gboolean
+_gst_array_accumulator (GSignalInvocationHint * ihint,
+    GValue * return_accu, const GValue * handler_return, gpointer dummy)
+{
+  gpointer array;
+
+  array = g_value_get_boxed (handler_return);
+  if (!(ihint->run_type & G_SIGNAL_RUN_CLEANUP))
+    g_value_set_boxed (return_accu, array);
+
+  return FALSE;
+}
+
+static gboolean
+_gst_select_accumulator (GSignalInvocationHint * ihint,
+    GValue * return_accu, const GValue * handler_return, gpointer dummy)
+{
+  GstAutoplugSelectResult res;
+
+  res = g_value_get_enum (handler_return);
+  if (!(ihint->run_type & G_SIGNAL_RUN_CLEANUP))
+    g_value_set_enum (return_accu, res);
+
+  return FALSE;
+}
+
+static gboolean
 gst_uri_decode_bin_autoplug_continue (GstElement * element, GstPad * pad,
     GstCaps * caps)
 {
@@ -182,6 +254,41 @@ gst_uri_decode_bin_autoplug_continue (GstElement * element, GstPad * pad,
   return TRUE;
 }
 
+/* Must be called with factories lock! */
+static void
+gst_uri_decode_bin_update_factories_list (GstURIDecodeBin * dec)
+{
+  if (!dec->factories ||
+      dec->factories_cookie !=
+      gst_default_registry_get_feature_list_cookie ()) {
+    if (dec->factories)
+      g_value_array_free (dec->factories);
+    dec->factories = gst_factory_list_get_elements (GST_FACTORY_LIST_DECODER);
+    dec->factories_cookie = gst_default_registry_get_feature_list_cookie ();
+  }
+}
+
+static GValueArray *
+gst_uri_decode_bin_autoplug_factories (GstElement * element, GstPad * pad,
+    GstCaps * caps)
+{
+  GValueArray *result;
+  GstURIDecodeBin *dec = GST_URI_DECODE_BIN_CAST (element);
+
+  GST_DEBUG_OBJECT (element, "finding factories");
+
+  /* return all compatible factories for caps */
+  g_mutex_lock (dec->factories_lock);
+  gst_uri_decode_bin_update_factories_list (dec);
+  result = gst_factory_list_filter (dec->factories, caps);
+  g_mutex_unlock (dec->factories_lock);
+
+  GST_DEBUG_OBJECT (element, "autoplug-factories returns %p", result);
+
+  return result;
+}
+
+
 static void
 gst_uri_decode_bin_class_init (GstURIDecodeBinClass * klass)
 {
@@ -199,28 +306,72 @@ gst_uri_decode_bin_class_init (GstURIDecodeBinClass * klass)
 
   g_object_class_install_property (gobject_class, PROP_URI,
       g_param_spec_string ("uri", "URI", "URI to decode",
-          DEFAULT_PROP_URI, G_PARAM_READWRITE));
+          DEFAULT_PROP_URI, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
+
+  g_object_class_install_property (gobject_class, PROP_SOURCE,
+      g_param_spec_object ("source", "Source", "Source object used",
+          GST_TYPE_ELEMENT, G_PARAM_READABLE));
 
   g_object_class_install_property (gobject_class, PROP_CONNECTION_SPEED,
       g_param_spec_uint ("connection-speed", "Connection Speed",
           "Network connection speed in kbps (0 = unknown)",
-          0, G_MAXUINT, DEFAULT_CONNECTION_SPEED, G_PARAM_READWRITE));
+          0, G_MAXUINT / 1000, DEFAULT_CONNECTION_SPEED,
+          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
 
   g_object_class_install_property (gobject_class, PROP_CAPS,
       g_param_spec_boxed ("caps", "Caps",
           "The caps on which to stop decoding. (NULL = default)",
-          GST_TYPE_CAPS, G_PARAM_READWRITE));
+          GST_TYPE_CAPS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
 
   g_object_class_install_property (gobject_class, PROP_SUBTITLE_ENCODING,
       g_param_spec_string ("subtitle-encoding", "subtitle encoding",
           "Encoding to assume if input subtitles are not in UTF-8 encoding. "
           "If not set, the GST_SUBTITLE_ENCODING environment variable will "
           "be checked for an encoding to use. If that is not set either, "
-          "ISO-8859-15 will be assumed.", NULL, G_PARAM_READWRITE));
+          "ISO-8859-15 will be assumed.", NULL,
+          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
+
+  g_object_class_install_property (gobject_class, PROP_BUFFER_SIZE,
+      g_param_spec_int ("buffer-size", "Buffer size (bytes)",
+          "Buffer size when buffering streams (-1 default value)",
+          -1, G_MAXINT, DEFAULT_BUFFER_SIZE,
+          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
+  g_object_class_install_property (gobject_class, PROP_BUFFER_DURATION,
+      g_param_spec_int64 ("buffer-duration", "Buffer duration (ns)",
+          "Buffer duration when buffering streams (-1 default value)",
+          -1, G_MAXINT64, DEFAULT_BUFFER_DURATION,
+          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
+
+  /**
+   * GstURIDecodeBin::download:
+   *
+   * For certain media type, enable download buffering.
+   */
+  g_object_class_install_property (gobject_class, PROP_DOWNLOAD,
+      g_param_spec_boolean ("download", "Download",
+          "Attempt download buffering when buffering network streams",
+          DEFAULT_DOWNLOAD, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
+  /**
+   * GstURIDecodeBin::use-buffering:
+   *
+   * Emit BUFFERING messages based on low-/high-percent thresholds of the
+   * demuxed or parsed data.
+   * When download buffering is activated and used for the current media
+   * type, this property does nothing. Otherwise perform buffering on the
+   * demuxed or parsed media.
+   *
+   * Since: 0.10.26
+   */
+  g_object_class_install_property (gobject_class, PROP_USE_BUFFERING,
+      g_param_spec_boolean ("use-buffering", "Use Buffering",
+          "Perform buffering on demuxed/parsed media",
+          DEFAULT_USE_BUFFERING, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
 
   /**
    * GstURIDecodeBin::unknown-type:
-   * @pad: the new pad containing caps that cannot be resolved to a 'final' stream type.
+   * @bin: The uridecodebin
+   * @pad: the new pad containing caps that cannot be resolved to a 'final'
+   * stream type.
    * @caps: the #GstCaps of the pad that cannot be resolved.
    *
    * This signal is emitted when a pad for which there is no further possible
@@ -229,11 +380,12 @@ gst_uri_decode_bin_class_init (GstURIDecodeBinClass * klass)
   gst_uri_decode_bin_signals[SIGNAL_UNKNOWN_TYPE] =
       g_signal_new ("unknown-type", G_TYPE_FROM_CLASS (klass),
       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstURIDecodeBinClass, unknown_type),
-      NULL, NULL, gst_marshal_VOID__OBJECT_OBJECT, G_TYPE_NONE, 2,
+      NULL, NULL, gst_marshal_VOID__OBJECT_BOXED, G_TYPE_NONE, 2,
       GST_TYPE_PAD, GST_TYPE_CAPS);
 
   /**
    * GstURIDecodeBin::autoplug-continue:
+   * @bin: The uridecodebin
    * @pad: The #GstPad.
    * @caps: The #GstCaps found.
    *
@@ -249,16 +401,23 @@ gst_uri_decode_bin_class_init (GstURIDecodeBinClass * klass)
       g_signal_new ("autoplug-continue", G_TYPE_FROM_CLASS (klass),
       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstURIDecodeBinClass,
           autoplug_continue), _gst_boolean_accumulator, NULL,
-      gst_play_marshal_BOOLEAN__OBJECT_OBJECT, G_TYPE_BOOLEAN, 2, GST_TYPE_PAD,
+      gst_play_marshal_BOOLEAN__OBJECT_BOXED, G_TYPE_BOOLEAN, 2, GST_TYPE_PAD,
       GST_TYPE_CAPS);
 
   /**
    * GstURIDecodeBin::autoplug-factories:
+   * @bin: The decodebin
    * @pad: The #GstPad.
    * @caps: The #GstCaps found.
    *
    * This function is emited when an array of possible factories for @caps on
-   * @pad is needed. Decodebin2 will by default return 
+   * @pad is needed. Uridecodebin will by default return an array with all
+   * compatible factories, sorted by rank.
+   *
+   * If this function returns NULL, @pad will be exposed as a final caps.
+   *
+   * If this function returns an empty array, the pad will be considered as
+   * having an unhandled type media type.
    *
    * Returns: a #GValueArray* with a list of factories to try. The factories are
    * by default tried in the returned order or based on the index returned by
@@ -267,31 +426,43 @@ gst_uri_decode_bin_class_init (GstURIDecodeBinClass * klass)
   gst_uri_decode_bin_signals[SIGNAL_AUTOPLUG_FACTORIES] =
       g_signal_new ("autoplug-factories", G_TYPE_FROM_CLASS (klass),
       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstURIDecodeBinClass,
-          autoplug_factories), NULL, NULL,
-      gst_play_marshal_BOXED__OBJECT_OBJECT, G_TYPE_VALUE_ARRAY, 2,
+          autoplug_factories), _gst_array_accumulator, NULL,
+      gst_play_marshal_BOXED__OBJECT_BOXED, G_TYPE_VALUE_ARRAY, 2,
       GST_TYPE_PAD, GST_TYPE_CAPS);
 
   /**
    * GstURIDecodeBin::autoplug-select:
    * @pad: The #GstPad.
    * @caps: The #GstCaps.
-   * @factories: A #GValueArray of possible #GstElementFactory to use, sorted by
-   * rank (higher ranks come first).
+   * @factory: A #GstElementFactory to use
    *
    * This signal is emitted once uridecodebin has found all the possible
-   * #GstElementFactory that can be used to handle the given @caps.
+   * #GstElementFactory that can be used to handle the given @caps. For each of
+   * those factories, this signal is emited.
+   *
+   * The signal handler should return a #GST_TYPE_AUTOPLUG_SELECT_RESULT enum
+   * value indicating what decodebin2 should do next.
+   *
+   * A value of #GST_AUTOPLUG_SELECT_TRY will try to autoplug an element from
+   * @factory.
+   *
+   * A value of #GST_AUTOPLUG_SELECT_EXPOSE will expose @pad without plugging
+   * any element to it.
    *
-   * Returns: A #gint indicating what factory index from the @factories array
-   * that you wish uridecodebin to use for trying to decode the given @caps.
-   * -1 to stop selection of a factory. The default handler always
-   * returns the first possible factory.
+   * A value of #GST_AUTOPLUG_SELECT_SKIP will skip @factory and move to the
+   * next factory.
+   *
+   * Returns: a #GST_TYPE_AUTOPLUG_SELECT_RESULT that indicates the required
+   * operation. The default handler will always return
+   * #GST_AUTOPLUG_SELECT_TRY.
    */
   gst_uri_decode_bin_signals[SIGNAL_AUTOPLUG_SELECT] =
       g_signal_new ("autoplug-select", G_TYPE_FROM_CLASS (klass),
       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstURIDecodeBinClass,
-          autoplug_select), NULL, NULL,
-      gst_play_marshal_INT__OBJECT_OBJECT_BOXED, G_TYPE_INT, 3, GST_TYPE_PAD,
-      GST_TYPE_CAPS, G_TYPE_VALUE_ARRAY);
+          autoplug_select), _gst_select_accumulator, NULL,
+      gst_play_marshal_ENUM__OBJECT_BOXED_OBJECT,
+      GST_TYPE_AUTOPLUG_SELECT_RESULT, 3, GST_TYPE_PAD, GST_TYPE_CAPS,
+      GST_TYPE_ELEMENT_FACTORY);
 
   /**
    * GstURIDecodeBin::drained:
@@ -312,15 +483,28 @@ gst_uri_decode_bin_class_init (GstURIDecodeBinClass * klass)
 
   klass->autoplug_continue =
       GST_DEBUG_FUNCPTR (gst_uri_decode_bin_autoplug_continue);
+  klass->autoplug_factories =
+      GST_DEBUG_FUNCPTR (gst_uri_decode_bin_autoplug_factories);
 }
 
 static void
 gst_uri_decode_bin_init (GstURIDecodeBin * dec, GstURIDecodeBinClass * klass)
 {
+  /* first filter out the interesting element factories */
+  dec->factories_lock = g_mutex_new ();
+  gst_uri_decode_bin_update_factories_list (dec);
+
+  dec->lock = g_mutex_new ();
+
   dec->uri = g_strdup (DEFAULT_PROP_URI);
   dec->connection_speed = DEFAULT_CONNECTION_SPEED;
   dec->caps = DEFAULT_CAPS;
   dec->encoding = g_strdup (DEFAULT_SUBTITLE_ENCODING);
+
+  dec->buffer_duration = DEFAULT_BUFFER_DURATION;
+  dec->buffer_size = DEFAULT_BUFFER_SIZE;
+  dec->download = DEFAULT_DOWNLOAD;
+  dec->use_buffering = DEFAULT_USE_BUFFERING;
 }
 
 static void
@@ -328,13 +512,42 @@ gst_uri_decode_bin_finalize (GObject * obj)
 {
   GstURIDecodeBin *dec = GST_URI_DECODE_BIN (obj);
 
+  remove_decoders (dec, TRUE);
+  g_mutex_free (dec->lock);
+  g_mutex_free (dec->factories_lock);
   g_free (dec->uri);
   g_free (dec->encoding);
+  if (dec->factories)
+    g_value_array_free (dec->factories);
+  if (dec->caps)
+    gst_caps_unref (dec->caps);
 
   G_OBJECT_CLASS (parent_class)->finalize (obj);
 }
 
 static void
+gst_uri_decode_bin_set_encoding (GstURIDecodeBin * dec, const gchar * encoding)
+{
+  GSList *walk;
+
+  GST_URI_DECODE_BIN_LOCK (dec);
+
+  /* set property first */
+  GST_OBJECT_LOCK (dec);
+  g_free (dec->encoding);
+  dec->encoding = g_strdup (encoding);
+  GST_OBJECT_UNLOCK (dec);
+
+  /* set the property on all decodebins now */
+  for (walk = dec->decodebins; walk; walk = g_slist_next (walk)) {
+    GObject *decodebin = G_OBJECT (walk->data);
+
+    g_object_set (decodebin, "subtitle-encoding", encoding, NULL);
+  }
+  GST_URI_DECODE_BIN_UNLOCK (dec);
+}
+
+static void
 gst_uri_decode_bin_set_property (GObject * object, guint prop_id,
     const GValue * value, GParamSpec * pspec)
 {
@@ -360,10 +573,19 @@ gst_uri_decode_bin_set_property (GObject * object, guint prop_id,
       GST_OBJECT_UNLOCK (dec);
       break;
     case PROP_SUBTITLE_ENCODING:
-      GST_OBJECT_LOCK (dec);
-      g_free (dec->encoding);
-      dec->encoding = g_value_dup_string (value);
-      GST_OBJECT_UNLOCK (dec);
+      gst_uri_decode_bin_set_encoding (dec, g_value_get_string (value));
+      break;
+    case PROP_BUFFER_SIZE:
+      dec->buffer_size = g_value_get_int (value);
+      break;
+    case PROP_BUFFER_DURATION:
+      dec->buffer_duration = g_value_get_int64 (value);
+      break;
+    case PROP_DOWNLOAD:
+      dec->download = g_value_get_boolean (value);
+      break;
+    case PROP_USE_BUFFERING:
+      dec->use_buffering = g_value_get_boolean (value);
       break;
     default:
       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
@@ -383,6 +605,11 @@ gst_uri_decode_bin_get_property (GObject * object, guint prop_id,
       g_value_set_string (value, dec->uri);
       GST_OBJECT_UNLOCK (dec);
       break;
+    case PROP_SOURCE:
+      GST_OBJECT_LOCK (dec);
+      g_value_set_object (value, dec->source);
+      GST_OBJECT_UNLOCK (dec);
+      break;
     case PROP_CONNECTION_SPEED:
       GST_OBJECT_LOCK (dec);
       g_value_set_uint (value, dec->connection_speed / 1000);
@@ -398,12 +625,53 @@ gst_uri_decode_bin_get_property (GObject * object, guint prop_id,
       g_value_set_string (value, dec->encoding);
       GST_OBJECT_UNLOCK (dec);
       break;
+    case PROP_BUFFER_SIZE:
+      GST_OBJECT_LOCK (dec);
+      g_value_set_int (value, dec->buffer_size);
+      GST_OBJECT_UNLOCK (dec);
+      break;
+    case PROP_BUFFER_DURATION:
+      GST_OBJECT_LOCK (dec);
+      g_value_set_int64 (value, dec->buffer_duration);
+      GST_OBJECT_UNLOCK (dec);
+      break;
+    case PROP_DOWNLOAD:
+      g_value_set_boolean (value, dec->download);
+      break;
+    case PROP_USE_BUFFERING:
+      g_value_set_boolean (value, dec->use_buffering);
+      break;
     default:
       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
       break;
   }
 }
 
+static void
+do_async_start (GstURIDecodeBin * dbin)
+{
+  GstMessage *message;
+
+  dbin->async_pending = TRUE;
+
+  message = gst_message_new_async_start (GST_OBJECT_CAST (dbin), FALSE);
+  parent_class->handle_message (GST_BIN_CAST (dbin), message);
+}
+
+static void
+do_async_done (GstURIDecodeBin * dbin)
+{
+  GstMessage *message;
+
+  if (dbin->async_pending) {
+    GST_DEBUG_OBJECT (dbin, "posting ASYNC_DONE");
+    message = gst_message_new_async_done (GST_OBJECT_CAST (dbin));
+    parent_class->handle_message (GST_BIN_CAST (dbin), message);
+
+    dbin->async_pending = FALSE;
+  }
+}
+
 #define DEFAULT_QUEUE_SIZE          (3 * GST_SECOND)
 #define DEFAULT_QUEUE_MIN_THRESHOLD ((DEFAULT_QUEUE_SIZE * 30) / 100)
 #define DEFAULT_QUEUE_THRESHOLD     ((DEFAULT_QUEUE_SIZE * 95) / 100)
@@ -415,7 +683,7 @@ unknown_type_cb (GstElement * element, GstPad * pad, GstCaps * caps,
   gchar *capsstr;
 
   capsstr = gst_caps_to_string (caps);
-  GST_ELEMENT_WARNING (decoder, STREAM, WRONG_TYPE,
+  GST_ELEMENT_WARNING (decoder, STREAM, CODEC_NOT_FOUND,
       (_("No decoder available for type \'%s\'."), capsstr), (NULL));
   g_free (capsstr);
 }
@@ -443,7 +711,7 @@ no_more_pads_full (GstElement * element, gboolean subs,
   /* setup phase */
   GST_DEBUG_OBJECT (element, "no more pads, %d pending", decoder->pending);
 
-  GST_OBJECT_LOCK (decoder);
+  GST_URI_DECODE_BIN_LOCK (decoder);
   final = (decoder->pending == 0);
 
   /* nothing pending, we can exit */
@@ -459,7 +727,7 @@ no_more_pads_full (GstElement * element, gboolean subs,
   final = (decoder->pending == 0);
 
 done:
-  GST_OBJECT_UNLOCK (decoder);
+  GST_URI_DECODE_BIN_UNLOCK (decoder);
 
   if (final)
     gst_element_no_more_pads (GST_ELEMENT_CAST (decoder));
@@ -479,9 +747,9 @@ source_no_more_pads (GstElement * element, GstURIDecodeBin * bin)
   GST_DEBUG_OBJECT (bin, "No more pads in source element %s.",
       GST_ELEMENT_NAME (element));
 
-  g_signal_handler_disconnect (G_OBJECT (element), bin->src_np_sig_id);
+  g_signal_handler_disconnect (element, bin->src_np_sig_id);
   bin->src_np_sig_id = 0;
-  g_signal_handler_disconnect (G_OBJECT (element), bin->src_nmp_sig_id);
+  g_signal_handler_disconnect (element, bin->src_nmp_sig_id);
   bin->src_nmp_sig_id = 0;
 
   no_more_pads_full (element, FALSE, bin);
@@ -500,10 +768,10 @@ new_decoded_pad_cb (GstElement * element, GstPad * pad, gboolean last,
   GST_DEBUG_OBJECT (element, "new decoded pad, name: <%s>. Last: %d",
       GST_PAD_NAME (pad), last);
 
-  GST_OBJECT_LOCK (decoder);
+  GST_URI_DECODE_BIN_LOCK (decoder);
   padname = g_strdup_printf ("src%d", decoder->numpads);
   decoder->numpads++;
-  GST_OBJECT_UNLOCK (decoder);
+  GST_URI_DECODE_BIN_UNLOCK (decoder);
 
   newpad = gst_ghost_pad_new (padname, pad);
   g_free (padname);
@@ -515,6 +783,34 @@ new_decoded_pad_cb (GstElement * element, GstPad * pad, gboolean last,
   gst_element_add_pad (GST_ELEMENT_CAST (decoder), newpad);
 }
 
+
+static gboolean
+source_pad_event_probe (GstPad * pad, GstEvent * event,
+    GstURIDecodeBin * decoder)
+{
+  GST_LOG_OBJECT (pad, "%s, decoder %p", GST_EVENT_TYPE_NAME (event), decoder);
+
+  if (GST_EVENT_TYPE (event) == GST_EVENT_EOS) {
+    GST_DEBUG_OBJECT (pad, "we received EOS");
+
+    g_signal_emit (decoder,
+        gst_uri_decode_bin_signals[SIGNAL_DRAINED], 0, NULL);
+  }
+  /* never drop events */
+  return TRUE;
+}
+
+/* called when we found a raw pad on the source element. We need to set up a
+ * padprobe to detect EOS before exposing the pad. */
+static void
+expose_decoded_pad (GstElement * element, GstPad * pad,
+    GstURIDecodeBin * decoder)
+{
+  gst_pad_add_event_probe (pad, G_CALLBACK (source_pad_event_probe), decoder);
+
+  new_decoded_pad_cb (element, pad, FALSE, decoder);
+}
+
 static void
 pad_removed_cb (GstElement * element, GstPad * pad, GstURIDecodeBin * decoder)
 {
@@ -523,6 +819,10 @@ pad_removed_cb (GstElement * element, GstPad * pad, GstURIDecodeBin * decoder)
   GST_DEBUG_OBJECT (element, "pad removed name: <%s:%s>",
       GST_DEBUG_PAD_NAME (pad));
 
+  /* we only care about srcpads */
+  if (!GST_PAD_IS_SRC (pad))
+    return;
+
   if (!(ghost = g_object_get_data (G_OBJECT (pad), "uridecodebin.ghostpad")))
     goto no_ghost;
 
@@ -556,12 +856,28 @@ array_has_value (const gchar * values[], const gchar * value)
   return FALSE;
 }
 
+static gboolean
+array_has_uri_value (const gchar * values[], const gchar * value)
+{
+  gint i;
+
+  for (i = 0; values[i]; i++) {
+    if (!g_ascii_strncasecmp (value, values[i], strlen (values[i])))
+      return TRUE;
+  }
+  return FALSE;
+}
+
 /* list of URIs that we consider to be streams and that need buffering.
  * We have no mechanism yet to figure this out with a query. */
 static const gchar *stream_uris[] = { "http://", "mms://", "mmsh://",
-  "mmsu://", "mmst://", NULL
+  "mmsu://", "mmst://", "fd://", "myth://", "ssh://", "ftp://", "sftp://",
+  NULL
 };
 
+/* list of URIs that need a queue because they are pretty bursty */
+static const gchar *queue_uris[] = { "cdda://", NULL };
+
 /* blacklisted URIs, we know they will always fail. */
 static const gchar *blacklisted_uris[] = { NULL };
 
@@ -573,15 +889,16 @@ static const gchar *no_media_mimes[] = {
 };
 #endif
 
-/* mime types we consider raw media */
-static const gchar *raw_mimes[] = {
-  "audio/x-raw", "video/x-raw", NULL
+/* media types we can download */
+static const gchar *download_media[] = {
+  "video/quicktime", "video/x-flv", NULL
 };
 
-#define IS_STREAM_URI(uri)          (array_has_value (stream_uris, uri))
-#define IS_BLACKLISTED_URI(uri)     (array_has_value (blacklisted_uris, uri))
+#define IS_STREAM_URI(uri)          (array_has_uri_value (stream_uris, uri))
+#define IS_QUEUE_URI(uri)           (array_has_uri_value (queue_uris, uri))
+#define IS_BLACKLISTED_URI(uri)     (array_has_uri_value (blacklisted_uris, uri))
 #define IS_NO_MEDIA_MIME(mime)      (array_has_value (no_media_mimes, mime))
-#define IS_RAW_MIME(mime)           (array_has_value (raw_mimes, mime))
+#define IS_DOWNLOAD_MEDIA(media)    (array_has_value (download_media, media))
 
 /*
  * Generate and configure a source element.
@@ -594,6 +911,8 @@ gen_source_element (GstURIDecodeBin * decoder)
   if (!decoder->uri)
     goto no_uri;
 
+  GST_LOG_OBJECT (decoder, "finding source for %s", decoder->uri);
+
   if (!gst_uri_is_valid (decoder->uri))
     goto invalid_uri;
 
@@ -604,13 +923,20 @@ gen_source_element (GstURIDecodeBin * decoder)
   if (!source)
     goto no_source;
 
+  GST_LOG_OBJECT (decoder, "found source type %s", G_OBJECT_TYPE_NAME (source));
+
   decoder->is_stream = IS_STREAM_URI (decoder->uri);
+  GST_LOG_OBJECT (decoder, "source is stream: %d", decoder->is_stream);
+
+  decoder->need_queue = IS_QUEUE_URI (decoder->uri);
+  GST_LOG_OBJECT (decoder, "source needs queue: %d", decoder->need_queue);
 
   /* make HTTP sources send extra headers so we get icecast
    * metadata in case the stream is an icecast stream */
   if (!strncmp (decoder->uri, "http://", 7) &&
       g_object_class_find_property (G_OBJECT_GET_CLASS (source),
           "iradio-mode")) {
+    GST_LOG_OBJECT (decoder, "configuring iradio-mode");
     g_object_set (source, "iradio-mode", TRUE, NULL);
   }
 
@@ -622,7 +948,12 @@ gen_source_element (GstURIDecodeBin * decoder)
     g_object_set (source, "connection-speed",
         decoder->connection_speed / 1000, NULL);
   }
-
+  if (g_object_class_find_property (G_OBJECT_GET_CLASS (source),
+          "subtitle-encoding")) {
+    GST_DEBUG_OBJECT (decoder,
+        "setting subtitle-encoding=%s to source element", decoder->encoding);
+    g_object_set (source, "subtitle-encoding", decoder->encoding, NULL);
+  }
   return source;
 
   /* ERRORS */
@@ -651,7 +982,13 @@ no_source:
     /* whoops, could not create the source element, dig a little deeper to
      * figure out what might be wrong. */
     if (prot) {
-      GST_ELEMENT_ERROR (decoder, RESOURCE, FAILED,
+      GstMessage *msg;
+
+      msg =
+          gst_missing_uri_source_message_new (GST_ELEMENT_CAST (decoder), prot);
+      gst_element_post_message (GST_ELEMENT_CAST (decoder), msg);
+
+      GST_ELEMENT_ERROR (decoder, CORE, MISSING_PLUGIN,
           (_("No URI handler implemented for \"%s\"."), prot), (NULL));
       g_free (prot);
     } else
@@ -672,35 +1009,28 @@ no_source:
  * Returns: %FALSE @pad has no caps. Else TRUE and @all_raw set t the result.
  */
 static gboolean
-has_all_raw_caps (GstPad * pad, gboolean * all_raw)
+has_all_raw_caps (GstPad * pad, GstCaps * rawcaps, gboolean * all_raw)
 {
-  GstCaps *caps;
+  GstCaps *caps, *intersection;
   gint capssize;
-  guint i, num_raw = 0;
   gboolean res = FALSE;
 
-  caps = gst_pad_get_caps (pad);
+  caps = gst_pad_get_caps_reffed (pad);
   if (caps == NULL)
     return FALSE;
 
+  GST_DEBUG_OBJECT (pad, "have caps %" GST_PTR_FORMAT, caps);
+
   capssize = gst_caps_get_size (caps);
   /* no caps, skip and move to the next pad */
   if (capssize == 0 || gst_caps_is_empty (caps) || gst_caps_is_any (caps))
     goto done;
 
-  /* count the number of raw formats in the caps */
-  for (i = 0; i < capssize; ++i) {
-    GstStructure *s;
-    const gchar *mime_type;
+  intersection = gst_caps_intersect (caps, rawcaps);
+  *all_raw = !gst_caps_is_empty (intersection)
+      && (gst_caps_get_size (intersection) == capssize);
+  gst_caps_unref (intersection);
 
-    s = gst_caps_get_structure (caps, i);
-    mime_type = gst_structure_get_name (s);
-
-    if (IS_RAW_MIME (mime_type))
-      ++num_raw;
-  }
-
-  *all_raw = (num_raw == capssize);
   res = TRUE;
 
 done:
@@ -708,12 +1038,26 @@ done:
   return res;
 }
 
+static void
+post_missing_plugin_error (GstElement * dec, const gchar * element_name)
+{
+  GstMessage *msg;
+
+  msg = gst_missing_element_message_new (dec, element_name);
+  gst_element_post_message (dec, msg);
+
+  GST_ELEMENT_ERROR (dec, CORE, MISSING_PLUGIN,
+      (_("Missing element '%s' - check your GStreamer installation."),
+          element_name), (NULL));
+}
+
 /**
  * analyse_source:
  * @decoder: a #GstURIDecodeBin
  * @is_raw: are all pads raw data
  * @have_out: does the source have output
  * @is_dynamic: is this a dynamic source
+ * @use_queue: put a queue before raw output pads
  *
  * Check the source of @decoder and collect information about it.
  *
@@ -730,20 +1074,24 @@ done:
  */
 static gboolean
 analyse_source (GstURIDecodeBin * decoder, gboolean * is_raw,
-    gboolean * have_out, gboolean * is_dynamic)
+    gboolean * have_out, gboolean * is_dynamic, gboolean use_queue)
 {
   GstIterator *pads_iter;
   gboolean done = FALSE;
   gboolean res = TRUE;
+  GstCaps *rawcaps;
+  GstPad *pad;
 
   *have_out = FALSE;
   *is_raw = FALSE;
   *is_dynamic = FALSE;
 
+  g_object_get (decoder, "caps", &rawcaps, NULL);
+  if (!rawcaps)
+    rawcaps = DEFAULT_CAPS;
+
   pads_iter = gst_element_iterate_src_pads (decoder->source);
   while (!done) {
-    GstPad *pad;
-
     switch (gst_iterator_next (pads_iter, (gpointer) & pad)) {
       case GST_ITERATOR_ERROR:
         res = FALSE;
@@ -763,16 +1111,46 @@ analyse_source (GstURIDecodeBin * decoder, gboolean * is_raw,
         *have_out = TRUE;
 
         /* if FALSE, this pad has no caps and we continue with the next pad. */
-        if (!has_all_raw_caps (pad, is_raw))
+        if (!has_all_raw_caps (pad, rawcaps, is_raw)) {
+          gst_object_unref (pad);
           break;
+        }
 
         /* caps on source pad are all raw, we can add the pad */
-        if (*is_raw)
-          new_decoded_pad_cb (decoder->source, pad, FALSE, decoder);
+        if (*is_raw) {
+          GstElement *outelem;
+
+          if (use_queue) {
+            GstPad *sinkpad;
+
+            /* insert a queue element right before the raw pad */
+            outelem = gst_element_factory_make ("queue2", NULL);
+            if (!outelem)
+              goto no_queue2;
+
+            gst_bin_add (GST_BIN_CAST (decoder), outelem);
+
+            sinkpad = gst_element_get_static_pad (outelem, "sink");
+            gst_pad_link (pad, sinkpad);
+            gst_object_unref (sinkpad);
+            gst_object_unref (pad);
+
+            /* save queue pointer so we can remove it later */
+            decoder->queue = outelem;
+
+            /* get the new raw srcpad */
+            pad = gst_element_get_static_pad (outelem, "src");
+          } else {
+            outelem = decoder->source;
+          }
+          expose_decoded_pad (outelem, pad, decoder);
+        }
+        gst_object_unref (pad);
         break;
     }
   }
   gst_iterator_free (pads_iter);
+  gst_caps_unref (rawcaps);
 
   if (!*have_out) {
     GstElementClass *elemclass;
@@ -797,22 +1175,65 @@ analyse_source (GstURIDecodeBin * decoder, gboolean * is_raw,
   }
 
   return res;
+no_queue2:
+  {
+    post_missing_plugin_error (GST_ELEMENT_CAST (decoder), "queue2");
+
+    gst_object_unref (pad);
+    gst_iterator_free (pads_iter);
+    gst_caps_unref (rawcaps);
+
+    return FALSE;
+  }
 }
 
+/* Remove all decodebin2 from ourself 
+ * If force is FALSE, then the decodebin2 instances will be stored in
+ * pending_decodebins for re-use later on.
+ * If force is TRUE, then all decodebin2 instances will be unreferenced
+ * and cleared, including the pending ones. */
 static void
-remove_decoders (GstURIDecodeBin * bin)
+remove_decoders (GstURIDecodeBin * bin, gboolean force)
 {
   GSList *walk;
 
-  for (walk = bin->decoders; walk; walk = g_slist_next (walk)) {
+  for (walk = bin->decodebins; walk; walk = g_slist_next (walk)) {
     GstElement *decoder = GST_ELEMENT_CAST (walk->data);
 
     GST_DEBUG_OBJECT (bin, "removing old decoder element");
-    gst_element_set_state (decoder, GST_STATE_NULL);
-    gst_bin_remove (GST_BIN_CAST (bin), decoder);
+    if (force) {
+      gst_element_set_state (decoder, GST_STATE_NULL);
+      gst_bin_remove (GST_BIN_CAST (bin), decoder);
+    } else {
+      GstCaps *caps;
+
+      gst_element_set_state (decoder, GST_STATE_READY);
+      /* add it to our list of pending decodebins */
+      g_object_ref (decoder);
+      gst_bin_remove (GST_BIN_CAST (bin), decoder);
+      /* restore some properties we might have changed */
+      g_object_set (decoder, "sink-caps", NULL, NULL);
+      caps = DEFAULT_CAPS;
+      g_object_set (decoder, "caps", caps, NULL);
+      gst_caps_unref (caps);
+
+      bin->pending_decodebins =
+          g_slist_prepend (bin->pending_decodebins, decoder);
+    }
+  }
+  g_slist_free (bin->decodebins);
+  bin->decodebins = NULL;
+  if (force) {
+    GSList *tmp;
+
+    for (tmp = bin->pending_decodebins; tmp; tmp = tmp->next) {
+      gst_element_set_state ((GstElement *) tmp->data, GST_STATE_NULL);
+      gst_object_unref ((GstElement *) tmp->data);
+    }
+    g_slist_free (bin->pending_decodebins);
+    bin->pending_decodebins = NULL;
+
   }
-  g_slist_free (bin->decoders);
-  bin->decoders = NULL;
 }
 
 static void
@@ -837,7 +1258,7 @@ proxy_unknown_type_signal (GstElement * element, GstPad * pad, GstCaps * caps,
 {
   GST_DEBUG_OBJECT (dec, "unknown-type signaled");
 
-  g_signal_emit (G_OBJECT (dec),
+  g_signal_emit (dec,
       gst_uri_decode_bin_signals[SIGNAL_UNKNOWN_TYPE], 0, pad, caps);
 }
 
@@ -847,7 +1268,7 @@ proxy_autoplug_continue_signal (GstElement * element, GstPad * pad,
 {
   gboolean result;
 
-  g_signal_emit (G_OBJECT (dec),
+  g_signal_emit (dec,
       gst_uri_decode_bin_signals[SIGNAL_AUTOPLUG_CONTINUE], 0, pad, caps,
       &result);
 
@@ -860,9 +1281,9 @@ static GValueArray *
 proxy_autoplug_factories_signal (GstElement * element, GstPad * pad,
     GstCaps * caps, GstURIDecodeBin * dec)
 {
-  GValueArray *result = NULL;
+  GValueArray *result;
 
-  g_signal_emit (G_OBJECT (dec),
+  g_signal_emit (dec,
       gst_uri_decode_bin_signals[SIGNAL_AUTOPLUG_FACTORIES], 0, pad, caps,
       &result);
 
@@ -871,14 +1292,14 @@ proxy_autoplug_factories_signal (GstElement * element, GstPad * pad,
   return result;
 }
 
-static gint
+static GstAutoplugSelectResult
 proxy_autoplug_select_signal (GstElement * element, GstPad * pad,
-    GstCaps * caps, GValueArray * array, GstURIDecodeBin * dec)
+    GstCaps * caps, GstElementFactory * factory, GstURIDecodeBin * dec)
 {
-  gint result;
+  GstAutoplugSelectResult result;
 
-  g_signal_emit (G_OBJECT (dec),
-      gst_uri_decode_bin_signals[SIGNAL_AUTOPLUG_SELECT], 0, pad, caps, array,
+  g_signal_emit (dec,
+      gst_uri_decode_bin_signals[SIGNAL_AUTOPLUG_SELECT], 0, pad, caps, factory,
       &result);
 
   GST_DEBUG_OBJECT (dec, "autoplug-select returned %d", result);
@@ -891,94 +1312,247 @@ proxy_drained_signal (GstElement * element, GstURIDecodeBin * dec)
 {
   GST_DEBUG_OBJECT (dec, "drained signaled");
 
-  g_signal_emit (G_OBJECT (dec),
-      gst_uri_decode_bin_signals[SIGNAL_DRAINED], 0, NULL);
+  g_signal_emit (dec, gst_uri_decode_bin_signals[SIGNAL_DRAINED], 0, NULL);
 }
 
+/* make a decodebin and connect to all the signals */
 static GstElement *
-make_decoder (GstURIDecodeBin * decoder, gboolean use_queue)
+make_decoder (GstURIDecodeBin * decoder)
 {
-  GstElement *result, *decodebin;
+  GstElement *decodebin;
+
+  /* re-use pending decodebin2 */
+  if (decoder->pending_decodebins) {
+    GSList *first = decoder->pending_decodebins;
+    GST_LOG_OBJECT (decoder, "re-using pending decodebin2");
+    decodebin = (GstElement *) first->data;
+    decoder->pending_decodebins =
+        g_slist_delete_link (decoder->pending_decodebins, first);
+  } else {
+    GST_LOG_OBJECT (decoder, "making new decodebin2");
+
+    /* now create the decoder element */
+    decodebin = gst_element_factory_make ("decodebin2", NULL);
+
+    if (!decodebin)
+      goto no_decodebin;
+    /* connect signals to proxy */
+    g_signal_connect (decodebin, "unknown-type",
+        G_CALLBACK (proxy_unknown_type_signal), decoder);
+    g_signal_connect (decodebin, "autoplug-continue",
+        G_CALLBACK (proxy_autoplug_continue_signal), decoder);
+    g_signal_connect (decodebin, "autoplug-factories",
+        G_CALLBACK (proxy_autoplug_factories_signal), decoder);
+    g_signal_connect (decodebin, "autoplug-select",
+        G_CALLBACK (proxy_autoplug_select_signal), decoder);
+    g_signal_connect (decodebin, "drained",
+        G_CALLBACK (proxy_drained_signal), decoder);
+
+    /* set up callbacks to create the links between decoded data
+     * and video/audio/subtitle rendering/output. */
+    g_signal_connect (decodebin,
+        "new-decoded-pad", G_CALLBACK (new_decoded_pad_cb), decoder);
+    g_signal_connect (decodebin,
+        "pad-removed", G_CALLBACK (pad_removed_cb), decoder);
+    g_signal_connect (decodebin, "no-more-pads",
+        G_CALLBACK (no_more_pads), decoder);
+    g_signal_connect (decodebin,
+        "unknown-type", G_CALLBACK (unknown_type_cb), decoder);
+  }
 
-  /* now create the decoder element */
-  decodebin = gst_element_factory_make ("decodebin2", NULL);
-  if (!decodebin)
+  /* configure caps if we have any */
+  if (decoder->caps)
+    g_object_set (decodebin, "caps", decoder->caps, NULL);
+
+  if (!decoder->is_stream) {
+    /* propagate the use-buffering property but only when we are not already
+     * doing stream buffering with queue2. FIXME, we might want to do stream
+     * buffering with the multiqueue buffering instead of queue2. */
+    g_object_set (decodebin, "use-buffering", decoder->use_buffering, NULL);
+
+    if (decoder->use_buffering) {
+      guint max_bytes;
+      guint64 max_time;
+
+      /* configure sizes when buffering */
+      if ((max_bytes = decoder->buffer_size) == -1)
+        max_bytes = 2 * 1024 * 1024;
+      if ((max_time = decoder->buffer_duration) == -1)
+        max_time = 2 * GST_SECOND;
+
+      g_object_set (decodebin, "max-size-bytes", max_bytes, "max-size-buffers",
+          (guint) 0, "max-size-time", max_time, NULL);
+    }
+  }
+
+  g_object_set_data (G_OBJECT (decodebin), "pending", "1");
+  g_object_set (decodebin, "subtitle-encoding", decoder->encoding, NULL);
+  decoder->pending++;
+  GST_LOG_OBJECT (decoder, "have %d pending dynamic objects", decoder->pending);
+
+  gst_bin_add (GST_BIN_CAST (decoder), decodebin);
+
+  decoder->decodebins = g_slist_prepend (decoder->decodebins, decodebin);
+
+  return decodebin;
+
+  /* ERRORS */
+no_decodebin:
+  {
+    post_missing_plugin_error (GST_ELEMENT_CAST (decoder), "decodebin2");
+    return NULL;
+  }
+}
+
+/* signaled when we have a stream and we need to configure the download
+ * buffering or regular buffering */
+static void
+type_found (GstElement * typefind, guint probability,
+    GstCaps * caps, GstURIDecodeBin * decoder)
+{
+  GstElement *dec_elem, *queue;
+  GstStructure *s;
+  const gchar *media_type;
+
+  GST_DEBUG_OBJECT (decoder, "typefind found caps %" GST_PTR_FORMAT, caps);
+
+  s = gst_caps_get_structure (caps, 0);
+  media_type = gst_structure_get_name (s);
+
+  /* remember if we need download buffering */
+  decoder->is_download = IS_DOWNLOAD_MEDIA (media_type) && decoder->download;
+
+  dec_elem = make_decoder (decoder);
+  if (!dec_elem)
     goto no_decodebin;
 
-  /* connect signals to proxy */
-  g_signal_connect (G_OBJECT (decodebin), "unknown-type",
-      G_CALLBACK (proxy_unknown_type_signal), decoder);
-  g_signal_connect (G_OBJECT (decodebin), "autoplug-continue",
-      G_CALLBACK (proxy_autoplug_continue_signal), decoder);
-  g_signal_connect (G_OBJECT (decodebin), "autoplug-factories",
-      G_CALLBACK (proxy_autoplug_factories_signal), decoder);
-  g_signal_connect (G_OBJECT (decodebin), "autoplug-select",
-      G_CALLBACK (proxy_autoplug_select_signal), decoder);
-  g_signal_connect (G_OBJECT (decodebin), "drained",
-      G_CALLBACK (proxy_drained_signal), decoder);
+  queue = gst_element_factory_make ("queue2", NULL);
+  if (!queue)
+    goto no_queue2;
 
-  if (use_queue) {
-    GstElement *queue;
-    GstPad *gpad, *pad;
+  g_object_set (queue, "use-buffering", TRUE, NULL);
 
-    queue = gst_element_factory_make ("queue2", NULL);
-    if (!queue)
-      goto no_queue2;
+  GST_DEBUG_OBJECT (decoder, "check media-type %s, %d", media_type,
+      decoder->download);
 
-    /* configure the queue as a buffering element */
-    g_object_set (G_OBJECT (queue), "use-buffering", TRUE, NULL);
+  if (decoder->is_download) {
+    gchar *temp_template, *filename;
+    const gchar *tmp_dir, *prgname;
 
-    result = gst_bin_new ("source-bin");
+    tmp_dir = g_get_tmp_dir ();
+    prgname = g_get_prgname ();
+    if (prgname == NULL)
+      prgname = "GStreamer";
 
-    gst_bin_add (GST_BIN_CAST (result), queue);
-    gst_bin_add (GST_BIN_CAST (result), decodebin);
+    filename = g_strdup_printf ("%s-XXXXXX", prgname);
 
-    gst_element_link (queue, decodebin);
+    /* build our filename */
+    temp_template = g_build_filename (tmp_dir, filename, NULL);
 
-    pad = gst_element_get_pad (queue, "sink");
-    gpad = gst_ghost_pad_new (GST_PAD_NAME (pad), pad);
-    gst_object_unref (pad);
+    GST_DEBUG_OBJECT (decoder, "enable download buffering in %s (%s, %s, %s)",
+        temp_template, tmp_dir, prgname, filename);
 
-    gst_pad_set_active (gpad, TRUE);
-    gst_element_add_pad (GST_ELEMENT_CAST (result), gpad);
-  } else {
-    result = decodebin;
+    /* configure progressive download for selected media types */
+    g_object_set (queue, "temp-template", temp_template, NULL);
+
+    g_free (filename);
+    g_free (temp_template);
   }
 
-  /* set up callbacks to create the links between decoded data
-   * and video/audio/subtitle rendering/output. */
-  g_signal_connect (G_OBJECT (decodebin),
-      "new-decoded-pad", G_CALLBACK (new_decoded_pad_cb), decoder);
-  g_signal_connect (G_OBJECT (decodebin),
-      "pad-removed", G_CALLBACK (pad_removed_cb), decoder);
-  g_signal_connect (G_OBJECT (decodebin), "no-more-pads",
-      G_CALLBACK (no_more_pads), decoder);
-  g_signal_connect (G_OBJECT (decodebin),
-      "unknown-type", G_CALLBACK (unknown_type_cb), decoder);
-  g_object_set_data (G_OBJECT (decodebin), "pending", "1");
-  decoder->pending++;
+  /* Disable max-size-buffers */
+  g_object_set (queue, "max-size-buffers", 0, NULL);
 
-  gst_bin_add (GST_BIN_CAST (decoder), result);
+  /* If buffer size or duration are set, set them on the queue2 element */
+  if (decoder->buffer_size != -1)
+    g_object_set (queue, "max-size-bytes", decoder->buffer_size, NULL);
+  if (decoder->buffer_duration != -1)
+    g_object_set (queue, "max-size-time", decoder->buffer_duration, NULL);
 
-  decoder->decoders = g_slist_prepend (decoder->decoders, result);
+  gst_bin_add (GST_BIN_CAST (decoder), queue);
 
-  return result;
+  if (!gst_element_link_pads (typefind, "src", queue, "sink"))
+    goto could_not_link;
+
+  /* to force caps on the decodebin element and avoid reparsing stuff by
+   * typefind. It also avoids a deadlock in the way typefind activates pads in
+   * the state change */
+  g_object_set (dec_elem, "sink-caps", caps, NULL);
+
+  if (!gst_element_link_pads (queue, "src", dec_elem, "sink"))
+    goto could_not_link;
+
+  gst_element_set_state (dec_elem, GST_STATE_PLAYING);
+  gst_element_set_state (queue, GST_STATE_PLAYING);
+
+  do_async_done (decoder);
+
+  return;
 
   /* ERRORS */
 no_decodebin:
   {
-    GST_ELEMENT_ERROR (decoder, CORE, MISSING_PLUGIN,
-        (_("Could not create \"decodebin2\" element.")), (NULL));
-    return NULL;
+    /* error was posted */
+    return;
+  }
+could_not_link:
+  {
+    GST_ELEMENT_ERROR (decoder, CORE, NEGOTIATION,
+        (NULL), ("Can't link typefind to decodebin2 element"));
+    return;
   }
 no_queue2:
   {
-    GST_ELEMENT_ERROR (decoder, CORE, MISSING_PLUGIN,
-        (_("Could not create \"queue2\" element.")), (NULL));
-    return NULL;
+    post_missing_plugin_error (GST_ELEMENT_CAST (decoder), "queue2");
+    return;
+  }
+}
+
+/* setup a streaming source. This will first plug a typefind element to the
+ * source. After we find the type, we decide to plug a queue2 and continue to
+ * plug a decodebin2 starting from the found caps */
+static gboolean
+setup_streaming (GstURIDecodeBin * decoder)
+{
+  GstElement *typefind;
+
+  /* now create the decoder element */
+  typefind = gst_element_factory_make ("typefind", NULL);
+  if (!typefind)
+    goto no_typefind;
+
+  gst_bin_add (GST_BIN_CAST (decoder), typefind);
+
+  if (!gst_element_link_pads (decoder->source, NULL, typefind, "sink"))
+    goto could_not_link;
+
+  decoder->typefind = typefind;
+
+  /* connect a signal to find out when the typefind element found
+   * a type */
+  decoder->have_type_id =
+      g_signal_connect (decoder->typefind, "have-type",
+      G_CALLBACK (type_found), decoder);
+
+  do_async_start (decoder);
+
+  return TRUE;
+
+  /* ERRORS */
+no_typefind:
+  {
+    post_missing_plugin_error (GST_ELEMENT_CAST (decoder), "typefind");
+    return FALSE;
+  }
+could_not_link:
+  {
+    GST_ELEMENT_ERROR (decoder, CORE, NEGOTIATION,
+        (NULL), ("Can't link source to typefind element"));
+    gst_bin_remove (GST_BIN_CAST (decoder), typefind);
+    return FALSE;
   }
 }
 
+/* remove source and all related elements */
 static void
 remove_source (GstURIDecodeBin * bin)
 {
@@ -990,15 +1564,27 @@ remove_source (GstURIDecodeBin * bin)
     gst_bin_remove (GST_BIN_CAST (bin), source);
 
     if (bin->src_np_sig_id) {
-      g_signal_handler_disconnect (G_OBJECT (source), bin->src_np_sig_id);
+      g_signal_handler_disconnect (source, bin->src_np_sig_id);
       bin->src_np_sig_id = 0;
     }
     if (bin->src_nmp_sig_id) {
-      g_signal_handler_disconnect (G_OBJECT (source), bin->src_nmp_sig_id);
+      g_signal_handler_disconnect (source, bin->src_nmp_sig_id);
       bin->src_nmp_sig_id = 0;
     }
     bin->source = NULL;
   }
+  if (bin->queue) {
+    GST_DEBUG_OBJECT (bin, "removing old queue element");
+    gst_element_set_state (bin->queue, GST_STATE_NULL);
+    gst_bin_remove (GST_BIN_CAST (bin), bin->queue);
+    bin->queue = NULL;
+  }
+  if (bin->typefind) {
+    GST_DEBUG_OBJECT (bin, "removing old typefind element");
+    gst_element_set_state (bin->typefind, GST_STATE_NULL);
+    gst_bin_remove (GST_BIN_CAST (bin), bin->typefind);
+    bin->typefind = NULL;
+  }
 }
 
 /* is called when a dynamic source element created a new pad. */
@@ -1007,29 +1593,39 @@ source_new_pad (GstElement * element, GstPad * pad, GstURIDecodeBin * bin)
 {
   GstElement *decoder;
   gboolean is_raw;
+  GstCaps *rawcaps;
 
+  GST_URI_DECODE_BIN_LOCK (bin);
   GST_DEBUG_OBJECT (bin, "Found new pad %s.%s in source element %s",
       GST_DEBUG_PAD_NAME (pad), GST_ELEMENT_NAME (element));
 
+  g_object_get (bin, "caps", &rawcaps, NULL);
+  if (!rawcaps)
+    rawcaps = DEFAULT_CAPS;
+
   /* if this is a pad with all raw caps, we can expose it */
-  if (has_all_raw_caps (pad, &is_raw) && is_raw) {
+  if (has_all_raw_caps (pad, rawcaps, &is_raw) && is_raw) {
     /* it's all raw, create output pads. */
-    new_decoded_pad_cb (element, pad, FALSE, bin);
+    GST_URI_DECODE_BIN_UNLOCK (bin);
+    gst_caps_unref (rawcaps);
+    expose_decoded_pad (element, pad, bin);
     return;
   }
+  gst_caps_unref (rawcaps);
 
   /* not raw, create decoder */
-  decoder = make_decoder (bin, FALSE);
+  decoder = make_decoder (bin);
   if (!decoder)
     goto no_decodebin;
 
   /* and link to decoder */
-  if (!gst_element_link (bin->source, decoder))
+  if (!gst_element_link_pads (bin->source, NULL, decoder, "sink"))
     goto could_not_link;
 
   GST_DEBUG_OBJECT (bin, "linked decoder to new pad");
 
   gst_element_set_state (decoder, GST_STATE_PLAYING);
+  GST_URI_DECODE_BIN_UNLOCK (bin);
 
   return;
 
@@ -1037,12 +1633,14 @@ source_new_pad (GstElement * element, GstPad * pad, GstURIDecodeBin * bin)
 no_decodebin:
   {
     /* error was posted */
+    GST_URI_DECODE_BIN_UNLOCK (bin);
     return;
   }
 could_not_link:
   {
     GST_ELEMENT_ERROR (bin, CORE, NEGOTIATION,
         (NULL), ("Can't link source to decoder element"));
+    GST_URI_DECODE_BIN_UNLOCK (bin);
     return;
   }
 }
@@ -1060,6 +1658,8 @@ setup_source (GstURIDecodeBin * decoder)
   /* delete old src */
   remove_source (decoder);
 
+  decoder->pending = 0;
+
   /* create and configure an element that can handle the uri */
   if (!(decoder->source = gen_source_element (decoder)))
     goto no_source;
@@ -1068,21 +1668,25 @@ setup_source (GstURIDecodeBin * decoder)
    * handled by the application right after. */
   gst_bin_add (GST_BIN_CAST (decoder), decoder->source);
 
+  /* notify of the new source used */
+  g_object_notify (G_OBJECT (decoder), "source");
+
   /* remove the old decoders now, if any */
-  remove_decoders (decoder);
+  remove_decoders (decoder, FALSE);
 
   /* see if the source element emits raw audio/video all by itself,
    * if so, we can create streams for the pads and be done with it.
    * Also check that is has source pads, if not, we assume it will
    * do everything itself.  */
-  if (!analyse_source (decoder, &is_raw, &have_out, &is_dynamic))
+  if (!analyse_source (decoder, &is_raw, &have_out, &is_dynamic,
+          decoder->need_queue))
     goto invalid_source;
 
   if (is_raw) {
     GST_DEBUG_OBJECT (decoder, "Source provides all raw data");
     /* source provides raw data, we added the pads and we can now signal a
      * no_more pads because we are done. */
-    /* FIXME, actually do this... */
+    gst_element_no_more_pads (GST_ELEMENT_CAST (decoder));
     return TRUE;
   }
   if (!have_out && !is_dynamic) {
@@ -1096,25 +1700,32 @@ setup_source (GstURIDecodeBin * decoder)
     GST_DEBUG_OBJECT (decoder, "Source has dynamic output pads");
     /* connect a handler for the new-pad signal */
     decoder->src_np_sig_id =
-        g_signal_connect (G_OBJECT (decoder->source), "pad-added",
+        g_signal_connect (decoder->source, "pad-added",
         G_CALLBACK (source_new_pad), decoder);
     decoder->src_nmp_sig_id =
-        g_signal_connect (G_OBJECT (decoder->source), "no-more-pads",
+        g_signal_connect (decoder->source, "no-more-pads",
         G_CALLBACK (source_no_more_pads), decoder);
     g_object_set_data (G_OBJECT (decoder->source), "pending", "1");
     decoder->pending++;
   } else {
-    GstElement *dec_elem;
-
-    GST_DEBUG_OBJECT (decoder, "Pluggin decodebin to source");
-
-    /* no dynamic source, we can link now */
-    dec_elem = make_decoder (decoder, decoder->is_stream);
-    if (!dec_elem)
-      goto no_decoder;
-
-    if (!gst_element_link (decoder->source, dec_elem))
-      goto could_not_link;
+    if (decoder->is_stream) {
+      GST_DEBUG_OBJECT (decoder, "Setting up streaming");
+      /* do the stream things here */
+      if (!setup_streaming (decoder))
+        goto streaming_failed;
+    } else {
+      GstElement *dec_elem;
+
+      /* no streaming source, we can link now */
+      GST_DEBUG_OBJECT (decoder, "Plugging decodebin to source");
+
+      dec_elem = make_decoder (decoder);
+      if (!dec_elem)
+        goto no_decoder;
+
+      if (!gst_element_link_pads (decoder->source, NULL, dec_elem, "sink"))
+        goto could_not_link;
+    }
   }
   return TRUE;
 
@@ -1135,6 +1746,11 @@ no_decoder:
     /* message was posted */
     return FALSE;
   }
+streaming_failed:
+  {
+    /* message was posted */
+    return FALSE;
+  }
 could_not_link:
   {
     GST_ELEMENT_ERROR (decoder, CORE, NEGOTIATION,
@@ -1288,6 +1904,7 @@ decoder_query_duration_fold (GstPad * item, GValue * ret, QueryFold * fold)
   gst_object_unref (item);
   return TRUE;
 }
+
 static void
 decoder_query_duration_done (GstURIDecodeBin * dec, QueryFold * fold)
 {
@@ -1319,6 +1936,7 @@ decoder_query_position_fold (GstPad * item, GValue * ret, QueryFold * fold)
   gst_object_unref (item);
   return TRUE;
 }
+
 static void
 decoder_query_position_done (GstURIDecodeBin * dec, QueryFold * fold)
 {
@@ -1361,6 +1979,7 @@ decoder_query_latency_fold (GstPad * item, GValue * ret, QueryFold * fold)
   gst_object_unref (item);
   return TRUE;
 }
+
 static void
 decoder_query_latency_done (GstURIDecodeBin * dec, QueryFold * fold)
 {
@@ -1392,6 +2011,7 @@ decoder_query_seeking_fold (GstPad * item, GValue * ret, QueryFold * fold)
 
   return TRUE;
 }
+
 static void
 decoder_query_seeking_done (GstURIDecodeBin * dec, QueryFold * fold)
 {
@@ -1519,6 +2139,11 @@ gst_uri_decode_bin_change_state (GstElement * element,
   decoder = GST_URI_DECODE_BIN (element);
 
   switch (transition) {
+    case GST_STATE_CHANGE_NULL_TO_READY:
+      g_mutex_lock (decoder->factories_lock);
+      gst_uri_decode_bin_update_factories_list (decoder);
+      g_mutex_unlock (decoder->factories_lock);
+      break;
     case GST_STATE_CHANGE_READY_TO_PAUSED:
       if (!setup_source (decoder))
         goto source_failed;
@@ -1537,7 +2162,14 @@ gst_uri_decode_bin_change_state (GstElement * element,
       break;
     case GST_STATE_CHANGE_PAUSED_TO_READY:
       GST_DEBUG ("paused to ready");
-      remove_decoders (decoder);
+      remove_decoders (decoder, FALSE);
+      remove_pads (decoder);
+      remove_source (decoder);
+      do_async_done (decoder);
+      break;
+    case GST_STATE_CHANGE_READY_TO_NULL:
+      GST_DEBUG ("ready to null");
+      remove_decoders (decoder, TRUE);
       remove_pads (decoder);
       remove_source (decoder);
       break;
@@ -1558,8 +2190,10 @@ setup_failed:
   }
 }
 
+gboolean gst_decode_bin_plugin_init (GstPlugin * plugin);
+
 static gboolean
-plugin_init (GstPlugin * plugin)
+gst_uri_decode_bin_plugin_init (GstPlugin * plugin)
 {
   GST_DEBUG_CATEGORY_INIT (gst_uri_decode_bin_debug, "uridecodebin", 0,
       "URI decoder element");
@@ -1568,12 +2202,24 @@ plugin_init (GstPlugin * plugin)
   GST_DEBUG ("binding text domain %s to locale dir %s", GETTEXT_PACKAGE,
       LOCALEDIR);
   bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
+  bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
 #endif /* ENABLE_NLS */
 
   return gst_element_register (plugin, "uridecodebin", GST_RANK_NONE,
       GST_TYPE_URI_DECODE_BIN);
 }
 
+static gboolean
+plugin_init (GstPlugin * plugin)
+{
+  if (!gst_decode_bin_plugin_init (plugin))
+    return FALSE;
+  if (!gst_uri_decode_bin_plugin_init (plugin))
+    return FALSE;
+
+  return TRUE;
+}
+
 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
     GST_VERSION_MINOR,
     "uridecodebin",