docs, gst: typo fixes
[platform/upstream/gstreamer.git] / libs / gst / base / gstbasesrc.c
index ff13edc..5cd515e 100644 (file)
  *   <listitem><para>live sources</para></listitem>
  * </itemizedlist>
  *
- * <refsect2>
- * <para>
  * The source can be configured to operate in any #GstFormat with the
- * gst_base_src_set_format() method. The currently set format determines 
- * the format of the internal #GstSegment and any #GST_EVENT_NEWSEGMENT 
+ * gst_base_src_set_format() method. The currently set format determines
+ * the format of the internal #GstSegment and any #GST_EVENT_NEWSEGMENT
  * events. The default format for #GstBaseSrc is #GST_FORMAT_BYTES.
- * </para>
- * <para>
+ *
  * #GstBaseSrc always supports push mode scheduling. If the following
  * conditions are met, it also supports pull mode scheduling:
  * <itemizedlist>
  *   <listitem><para>The format is set to #GST_FORMAT_BYTES (default).</para>
  *   </listitem>
- *   <listitem><para>#GstBaseSrc::is_seekable returns %TRUE.</para>
+ *   <listitem><para>#GstBaseSrcClass.is_seekable() returns %TRUE.</para>
  *   </listitem>
  * </itemizedlist>
- * </para>
- * <para>
- * Since 0.10.9, any #GstBaseSrc can enable pull based scheduling at any 
- * time by overriding #GstBaseSrc::check_get_range so that it returns %TRUE. 
- * </para>
- * <para>
+ *
+ * Since 0.10.9, any #GstBaseSrc can enable pull based scheduling at any time
+ * by overriding #GstBaseSrcClass.check_get_range() so that it returns %TRUE.
+ *
  * If all the conditions are met for operating in pull mode, #GstBaseSrc is
- * automatically seekable in push mode as well. The following conditions must 
+ * automatically seekable in push mode as well. The following conditions must
  * be met to make the element seekable in push mode when the format is not
  * #GST_FORMAT_BYTES:
  * <itemizedlist>
  *   <listitem><para>
- *     #GstBaseSrc::is_seekable returns %TRUE.
+ *     #GstBaseSrcClass.is_seekable() returns %TRUE.
  *   </para></listitem>
  *   <listitem><para>
- *     #GstBaseSrc::query can convert all supported seek formats to the
+ *     #GstBaseSrcClass.query() can convert all supported seek formats to the
  *     internal format as set with gst_base_src_set_format().
  *   </para></listitem>
  *   <listitem><para>
- *     #GstBaseSrc::do_seek is implemented, performs the seek and returns %TRUE.
+ *     #GstBaseSrcClass.do_seek() is implemented, performs the seek and returns
+ *      %TRUE.
  *   </para></listitem>
  * </itemizedlist>
- * </para>
- * <para>
- * When the element does not meet the requirements to operate in pull mode,
- * the offset and length in the #GstBaseSrc::create method should be ignored.
+ *
+ * When the element does not meet the requirements to operate in pull mode, the
+ * offset and length in the #GstBaseSrcClass.create() method should be ignored.
  * It is recommended to subclass #GstPushSrc instead, in this situation. If the
  * element can operate in pull mode but only with specific offsets and
  * lengths, it is allowed to generate an error when the wrong values are passed
- * to the #GstBaseSrc::create function.
- * </para>
- * <para>
+ * to the #GstBaseSrcClass.create() function.
+ *
  * #GstBaseSrc has support for live sources. Live sources are sources that when
  * paused discard data, such as audio or video capture devices. A typical live
  * source also produces data at a fixed rate and thus provides a clock to publish
  * this rate.
  * Use gst_base_src_set_live() to activate the live source mode.
- * </para>
- * <para>
- * A live source does not produce data in the PAUSED state. This means that the 
- * #GstBaseSrc::create method will not be called in PAUSED but only in PLAYING.
- * To signal the pipeline that the element will not produce data, the return
- * value from the READY to PAUSED state will be #GST_STATE_CHANGE_NO_PREROLL.
- * </para>
- * <para>
- * A typical live source will timestamp the buffers it creates with the 
+ *
+ * A live source does not produce data in the PAUSED state. This means that the
+ * #GstBaseSrcClass.create() method will not be called in PAUSED but only in
+ * PLAYING. To signal the pipeline that the element will not produce data, the
+ * return value from the READY to PAUSED state will be
+ * #GST_STATE_CHANGE_NO_PREROLL.
+ *
+ * A typical live source will timestamp the buffers it creates with the
  * current running time of the pipeline. This is one reason why a live source
- * can only produce data in the PLAYING state, when the clock is actually 
- * distributed and running. 
- * </para>
- * <para>
+ * can only produce data in the PLAYING state, when the clock is actually
+ * distributed and running.
+ *
  * Live sources that synchronize and block on the clock (an audio source, for
- * example) can since 0.10.12 use gst_base_src_wait_playing() when the ::create
- * function was interrupted by a state change to PAUSED.
- * </para>
- * <para>
- * The #GstBaseSrc::get_times method can be used to implement pseudo-live 
- * sources.
- * It only makes sense to implement the ::get_times function if the source is 
- * a live source. The ::get_times function should return timestamps starting
- * from 0, as if it were a non-live source. The base class will make sure that
- * the timestamps are transformed into the current running_time.
- * The base source will then wait for the calculated running_time before pushing
- * out the buffer.
- * </para>
- * <para>
+ * example) can since 0.10.12 use gst_base_src_wait_playing() when the
+ * #GstBaseSrcClass.create() function was interrupted by a state change to
+ * PAUSED.
+ *
+ * The #GstBaseSrcClass.get_times() method can be used to implement pseudo-live
+ * sources. It only makes sense to implement the #GstBaseSrcClass.get_times()
+ * function if the source is a live source. The #GstBaseSrcClass.get_times()
+ * function should return timestamps starting from 0, as if it were a non-live
+ * source. The base class will make sure that the timestamps are transformed
+ * into the current running_time. The base source will then wait for the
+ * calculated running_time before pushing out the buffer.
+ *
  * For live sources, the base class will by default report a latency of 0.
  * For pseudo live sources, the base class will by default measure the difference
  * between the first buffer timestamp and the start time of get_times and will
- * report this value as the latency. 
+ * report this value as the latency.
  * Subclasses should override the query function when this behaviour is not
  * acceptable.
- * </para>
- * <para>
- * There is only support in #GstBaseSrc for exactly one source pad, which 
- * should be named "src". A source implementation (subclass of #GstBaseSrc) 
- * should install a pad template in its base_init function, like so:
- * </para>
- * <para>
- * <programlisting>
+ *
+ * There is only support in #GstBaseSrc for exactly one source pad, which
+ * should be named "src". A source implementation (subclass of #GstBaseSrc)
+ * should install a pad template in its class_init function, like so:
+ * |[
  * static void
- * my_element_base_init (gpointer g_class)
+ * my_element_class_init (GstMyElementClass *klass)
  * {
- *   GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);
+ *   GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
  *   // srctemplate should be a #GstStaticPadTemplate with direction
  *   // #GST_PAD_SRC and name "src"
  *   gst_element_class_add_pad_template (gstelement_class,
  *   // see #GstElementDetails
  *   gst_element_class_set_details (gstelement_class, &amp;details);
  * }
- * </programlisting>
- * </para>
+ * ]|
+ *
+ * <refsect2>
  * <title>Controlled shutdown of live sources in applications</title>
  * <para>
  * Applications that record from a live source may want to stop recording
  * event down the pipeline. The application would then wait for an
  * EOS message posted on the pipeline's bus to know when all data has
  * been processed and the pipeline can safely be stopped.
- * </para>
- * <para>
- * Since GStreamer 0.10.3 an application may simply set the source
- * element to NULL or READY state to make it send an EOS event downstream.
- * The application should lock the state of the source afterwards, so that
- * shutting down the pipeline from PLAYING doesn't temporarily start up the
- * source element for a second time:
- * <programlisting>
- * ...
- * // stop recording
- * gst_element_set_state (audio_source, #GST_STATE_NULL);
- * gst_element_set_locked_state (audio_source, %TRUE);
- * ...
- * </programlisting>
- * Now the application should wait for an EOS message
- * to be posted on the pipeline's bus. Once it has received
- * an EOS message, it may safely shut down the entire pipeline:
- * <programlisting>
- * ...
- * // everything done - shut down pipeline
- * gst_element_set_state (pipeline, #GST_STATE_NULL);
- * gst_element_set_locked_state (audio_source, %FALSE);
- * ...
- * </programlisting>
- * </para>
- * <para>
- * Note that setting the source element to NULL or READY when the 
- * pipeline is in the PAUSED state may cause a deadlock since the streaming
- * thread might be blocked in PREROLL.
- * </para>
- * <para>
- * Last reviewed on 2007-09-13 (0.10.15)
+ *
+ * Since GStreamer 0.10.16 an application may send an EOS event to a source
+ * element to make it perform the EOS logic (send EOS event downstream or post a
+ * #GST_MESSAGE_SEGMENT_DONE on the bus). This can typically be done
+ * with the gst_element_send_event() function on the element or its parent bin.
+ *
+ * After the EOS has been sent to the element, the application should wait for
+ * an EOS message to be posted on the pipeline's bus. Once this EOS message is
+ * received, it may safely shut down the entire pipeline.
+ *
+ * The old behaviour for controlled shutdown introduced since GStreamer 0.10.3
+ * is still available but deprecated as it is dangerous and less flexible.
+ *
+ * Last reviewed on 2007-12-19 (0.10.16)
  * </para>
  * </refsect2>
  */
 #include <stdlib.h>
 #include <string.h>
 
+#include <gst/gst_private.h>
+
 #include "gstbasesrc.h"
 #include "gsttypefindhelper.h"
 #include <gst/gstmarshal.h>
@@ -223,8 +195,8 @@ enum
 
 #define DEFAULT_BLOCKSIZE       4096
 #define DEFAULT_NUM_BUFFERS     -1
-#define DEFAULT_TYPEFIND       FALSE
-#define DEFAULT_DO_TIMESTAMP   FALSE
+#define DEFAULT_TYPEFIND        FALSE
+#define DEFAULT_DO_TIMESTAMP    FALSE
 
 enum
 {
@@ -243,10 +215,15 @@ struct _GstBaseSrcPrivate
   gboolean last_sent_eos;       /* last thing we did was send an EOS (we set this
                                  * to avoid the sending of two EOS in some cases) */
   gboolean discont;
+  gboolean flushing;
 
   /* two segments to be sent in the streaming thread with STREAM_LOCK */
   GstEvent *close_segment;
   GstEvent *start_segment;
+  gboolean newsegment_pending;
+
+  /* if EOS is pending (atomic) */
+  gint pending_eos;
 
   /* startup latency is the time it takes between going to PLAYING and producing
    * the first BUFFER with running_time 0. This value is included in the latency
@@ -257,6 +234,20 @@ struct _GstBaseSrcPrivate
   GstClockTimeDiff ts_offset;
 
   gboolean do_timestamp;
+  volatile gint dynamic_size;
+
+  /* stream sequence number */
+  guint32 seqnum;
+
+  /* pending events (TAG, CUSTOM_BOTH, CUSTOM_DOWNSTREAM) to be
+   * pushed in the data stream */
+  GList *pending_events;
+  volatile gint have_events;
+
+  /* QoS *//* with LOCK */
+  gboolean qos_enabled;
+  gdouble proportion;
+  GstClockTime earliest_time;
 };
 
 static GstElementClass *parent_class = NULL;
@@ -270,9 +261,10 @@ static void gst_base_src_finalize (GObject * object);
 GType
 gst_base_src_get_type (void)
 {
-  static GType base_src_type = 0;
+  static volatile gsize base_src_type = 0;
 
-  if (G_UNLIKELY (base_src_type == 0)) {
+  if (g_once_init_enter (&base_src_type)) {
+    GType _type;
     static const GTypeInfo base_src_info = {
       sizeof (GstBaseSrcClass),
       (GBaseInitFunc) gst_base_src_base_init,
@@ -285,11 +277,13 @@ gst_base_src_get_type (void)
       (GInstanceInitFunc) gst_base_src_init,
     };
 
-    base_src_type = g_type_register_static (GST_TYPE_ELEMENT,
+    _type = g_type_register_static (GST_TYPE_ELEMENT,
         "GstBaseSrc", &base_src_info, G_TYPE_FLAG_ABSTRACT);
+    g_once_init_leave (&base_src_type, _type);
   }
   return base_src_type;
 }
+
 static GstCaps *gst_base_src_getcaps (GstPad * pad);
 static gboolean gst_base_src_setcaps (GstPad * pad, GstCaps * caps);
 static void gst_base_src_fixate (GstPad * pad, GstCaps * caps);
@@ -314,8 +308,8 @@ static gboolean gst_base_src_default_query (GstBaseSrc * src, GstQuery * query);
 static gboolean gst_base_src_default_prepare_seek_segment (GstBaseSrc * src,
     GstEvent * event, GstSegment * segment);
 
-static gboolean gst_base_src_unlock (GstBaseSrc * basesrc);
-static gboolean gst_base_src_unlock_stop (GstBaseSrc * basesrc);
+static gboolean gst_base_src_set_flushing (GstBaseSrc * basesrc,
+    gboolean flushing, gboolean live_play, gboolean unlock, gboolean * playing);
 static gboolean gst_base_src_start (GstBaseSrc * basesrc);
 static gboolean gst_base_src_stop (GstBaseSrc * basesrc);
 
@@ -329,6 +323,9 @@ static GstFlowReturn gst_base_src_pad_get_range (GstPad * pad, guint64 offset,
     guint length, GstBuffer ** buf);
 static GstFlowReturn gst_base_src_get_range (GstBaseSrc * src, guint64 offset,
     guint length, GstBuffer ** buf);
+static gboolean gst_base_src_seekable (GstBaseSrc * src);
+static gboolean gst_base_src_update_length (GstBaseSrc * src, guint64 offset,
+    guint * length);
 
 static void
 gst_base_src_base_init (gpointer g_class)
@@ -349,26 +346,28 @@ gst_base_src_class_init (GstBaseSrcClass * klass)
 
   parent_class = g_type_class_peek_parent (klass);
 
-  gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_base_src_finalize);
-  gobject_class->set_property = GST_DEBUG_FUNCPTR (gst_base_src_set_property);
-  gobject_class->get_property = GST_DEBUG_FUNCPTR (gst_base_src_get_property);
+  gobject_class->finalize = gst_base_src_finalize;
+  gobject_class->set_property = gst_base_src_set_property;
+  gobject_class->get_property = gst_base_src_get_property;
 
+/* FIXME 0.11: blocksize property should be int, not ulong (min is >max here) */
   g_object_class_install_property (gobject_class, PROP_BLOCKSIZE,
       g_param_spec_ulong ("blocksize", "Block size",
-          "Size in bytes to read per buffer (0 = default)", 0, G_MAXULONG,
-          DEFAULT_BLOCKSIZE, G_PARAM_READWRITE));
+          "Size in bytes to read per buffer (-1 = default)", 0, G_MAXULONG,
+          DEFAULT_BLOCKSIZE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
   g_object_class_install_property (gobject_class, PROP_NUM_BUFFERS,
       g_param_spec_int ("num-buffers", "num-buffers",
-          "Number of buffers to output before sending EOS", -1, G_MAXINT,
-          DEFAULT_NUM_BUFFERS, G_PARAM_READWRITE));
+          "Number of buffers to output before sending EOS (-1 = unlimited)",
+          -1, G_MAXINT, DEFAULT_NUM_BUFFERS, G_PARAM_READWRITE |
+          G_PARAM_STATIC_STRINGS));
   g_object_class_install_property (gobject_class, PROP_TYPEFIND,
       g_param_spec_boolean ("typefind", "Typefind",
           "Run typefind before negotiating", DEFAULT_TYPEFIND,
-          G_PARAM_READWRITE));
+          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
   g_object_class_install_property (gobject_class, PROP_DO_TIMESTAMP,
       g_param_spec_boolean ("do-timestamp", "Do timestamp",
           "Apply current stream time to buffers", DEFAULT_DO_TIMESTAMP,
-          G_PARAM_READWRITE));
+          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
 
   gstelement_class->change_state =
       GST_DEBUG_FUNCPTR (gst_base_src_change_state);
@@ -384,6 +383,17 @@ gst_base_src_class_init (GstBaseSrcClass * klass)
       GST_DEBUG_FUNCPTR (gst_base_src_default_check_get_range);
   klass->prepare_seek_segment =
       GST_DEBUG_FUNCPTR (gst_base_src_default_prepare_seek_segment);
+
+  /* Registering debug symbols for function pointers */
+  GST_DEBUG_REGISTER_FUNCPTR (gst_base_src_activate_push);
+  GST_DEBUG_REGISTER_FUNCPTR (gst_base_src_activate_pull);
+  GST_DEBUG_REGISTER_FUNCPTR (gst_base_src_event_handler);
+  GST_DEBUG_REGISTER_FUNCPTR (gst_base_src_query);
+  GST_DEBUG_REGISTER_FUNCPTR (gst_base_src_pad_get_range);
+  GST_DEBUG_REGISTER_FUNCPTR (gst_base_src_pad_check_get_range);
+  GST_DEBUG_REGISTER_FUNCPTR (gst_base_src_getcaps);
+  GST_DEBUG_REGISTER_FUNCPTR (gst_base_src_setcaps);
+  GST_DEBUG_REGISTER_FUNCPTR (gst_base_src_fixate);
 }
 
 static void
@@ -411,21 +421,15 @@ gst_base_src_init (GstBaseSrc * basesrc, gpointer g_class)
   pad = gst_pad_new_from_template (pad_template, "src");
 
   GST_DEBUG_OBJECT (basesrc, "setting functions on src pad");
-  gst_pad_set_activatepush_function (pad,
-      GST_DEBUG_FUNCPTR (gst_base_src_activate_push));
-  gst_pad_set_activatepull_function (pad,
-      GST_DEBUG_FUNCPTR (gst_base_src_activate_pull));
-  gst_pad_set_event_function (pad,
-      GST_DEBUG_FUNCPTR (gst_base_src_event_handler));
-  gst_pad_set_query_function (pad, GST_DEBUG_FUNCPTR (gst_base_src_query));
-  gst_pad_set_checkgetrange_function (pad,
-      GST_DEBUG_FUNCPTR (gst_base_src_pad_check_get_range));
-  gst_pad_set_getrange_function (pad,
-      GST_DEBUG_FUNCPTR (gst_base_src_pad_get_range));
-  gst_pad_set_getcaps_function (pad, GST_DEBUG_FUNCPTR (gst_base_src_getcaps));
-  gst_pad_set_setcaps_function (pad, GST_DEBUG_FUNCPTR (gst_base_src_setcaps));
-  gst_pad_set_fixatecaps_function (pad,
-      GST_DEBUG_FUNCPTR (gst_base_src_fixate));
+  gst_pad_set_activatepush_function (pad, gst_base_src_activate_push);
+  gst_pad_set_activatepull_function (pad, gst_base_src_activate_pull);
+  gst_pad_set_event_function (pad, gst_base_src_event_handler);
+  gst_pad_set_query_function (pad, gst_base_src_query);
+  gst_pad_set_checkgetrange_function (pad, gst_base_src_pad_check_get_range);
+  gst_pad_set_getrange_function (pad, gst_base_src_pad_get_range);
+  gst_pad_set_getcaps_function (pad, gst_base_src_getcaps);
+  gst_pad_set_setcaps_function (pad, gst_base_src_setcaps);
+  gst_pad_set_fixatecaps_function (pad, gst_base_src_fixate);
 
   /* hold pointer to pad */
   basesrc->srcpad = pad;
@@ -438,8 +442,10 @@ gst_base_src_init (GstBaseSrc * basesrc, gpointer g_class)
   gst_base_src_set_format (basesrc, GST_FORMAT_BYTES);
   basesrc->data.ABI.typefind = DEFAULT_TYPEFIND;
   basesrc->priv->do_timestamp = DEFAULT_DO_TIMESTAMP;
+  g_atomic_int_set (&basesrc->priv->have_events, FALSE);
 
   GST_OBJECT_FLAG_UNSET (basesrc, GST_BASE_SRC_STARTED);
+  GST_OBJECT_FLAG_SET (basesrc, GST_ELEMENT_IS_SOURCE);
 
   GST_DEBUG_OBJECT (basesrc, "init done");
 }
@@ -456,7 +462,13 @@ gst_base_src_finalize (GObject * object)
   g_cond_free (basesrc->live_cond);
 
   event_p = &basesrc->data.ABI.pending_seek;
-  gst_event_replace ((GstEvent **) event_p, NULL);
+  gst_event_replace (event_p, NULL);
+
+  if (basesrc->priv->pending_events) {
+    g_list_foreach (basesrc->priv->pending_events, (GFunc) gst_event_unref,
+        NULL);
+    g_list_free (basesrc->priv->pending_events);
+  }
 
   G_OBJECT_CLASS (parent_class)->finalize (object);
 }
@@ -465,9 +477,9 @@ gst_base_src_finalize (GObject * object)
  * gst_base_src_wait_playing:
  * @src: the src
  *
- * If the #GstBaseSrcClass::create method performs its own synchronisation against
- * the clock it must unblock when going from PLAYING to the PAUSED state and call
- * this method before continuing to produce the remaining data.
+ * If the #GstBaseSrcClass.create() method performs its own synchronisation
+ * against the clock it must unblock when going from PLAYING to the PAUSED state
+ * and call this method before continuing to produce the remaining data.
  *
  * This function will block until a state change to PLAYING happens (in which
  * case this function returns #GST_FLOW_OK) or the processing must be stopped due
@@ -482,33 +494,23 @@ gst_base_src_finalize (GObject * object)
 GstFlowReturn
 gst_base_src_wait_playing (GstBaseSrc * src)
 {
-  /* block until the state changes, or we get a flush, or something */
-  GST_LIVE_LOCK (src);
-  if (src->is_live) {
-    while (G_UNLIKELY (!src->live_running)) {
-      GST_DEBUG ("live source signal waiting");
-      GST_LIVE_SIGNAL (src);
-      GST_DEBUG ("live source waiting for running state");
-      GST_LIVE_WAIT (src);
-      GST_DEBUG ("live source unlocked");
-    }
-    /* FIXME, use another variable to signal stopping so that we don't
-     * have to grab another lock. */
-    GST_OBJECT_LOCK (src->srcpad);
-    if (G_UNLIKELY (GST_PAD_IS_FLUSHING (src->srcpad)))
+  g_return_val_if_fail (GST_IS_BASE_SRC (src), GST_FLOW_ERROR);
+
+  do {
+    /* block until the state changes, or we get a flush, or something */
+    GST_DEBUG_OBJECT (src, "live source waiting for running state");
+    GST_LIVE_WAIT (src);
+    GST_DEBUG_OBJECT (src, "live source unlocked");
+    if (src->priv->flushing)
       goto flushing;
-    GST_OBJECT_UNLOCK (src->srcpad);
-  }
-  GST_LIVE_UNLOCK (src);
+  } while (G_UNLIKELY (!src->live_running));
 
   return GST_FLOW_OK;
 
   /* ERRORS */
 flushing:
   {
-    GST_DEBUG_OBJECT (src, "pad is flushing");
-    GST_OBJECT_UNLOCK (src->srcpad);
-    GST_LIVE_UNLOCK (src);
+    GST_DEBUG_OBJECT (src, "we are flushing");
     return GST_FLOW_WRONG_STATE;
   }
 }
@@ -519,20 +521,22 @@ flushing:
  * @live: new live-mode
  *
  * If the element listens to a live source, @live should
- * be set to %TRUE. 
+ * be set to %TRUE.
  *
  * A live source will not produce data in the PAUSED state and
  * will therefore not be able to participate in the PREROLL phase
- * of a pipeline. To signal this fact to the application and the 
+ * of a pipeline. To signal this fact to the application and the
  * pipeline, the state change return value of the live source will
  * be GST_STATE_CHANGE_NO_PREROLL.
  */
 void
 gst_base_src_set_live (GstBaseSrc * src, gboolean live)
 {
-  GST_LIVE_LOCK (src);
+  g_return_if_fail (GST_IS_BASE_SRC (src));
+
+  GST_OBJECT_LOCK (src);
   src->is_live = live;
-  GST_LIVE_UNLOCK (src);
+  GST_OBJECT_UNLOCK (src);
 }
 
 /**
@@ -548,9 +552,11 @@ gst_base_src_is_live (GstBaseSrc * src)
 {
   gboolean result;
 
-  GST_LIVE_LOCK (src);
+  g_return_val_if_fail (GST_IS_BASE_SRC (src), FALSE);
+
+  GST_OBJECT_LOCK (src);
   result = src->is_live;
-  GST_LIVE_UNLOCK (src);
+  GST_OBJECT_UNLOCK (src);
 
   return result;
 }
@@ -564,29 +570,55 @@ gst_base_src_is_live (GstBaseSrc * src)
  * for sending NEW_SEGMENT events and for performing seeks.
  *
  * If a format of GST_FORMAT_BYTES is set, the element will be able to
- * operate in pull mode if the #GstBaseSrc::is_seekable returns TRUE.
+ * operate in pull mode if the #GstBaseSrcClass.is_seekable() returns TRUE.
+ *
+ * This function must only be called in states < %GST_STATE_PAUSED.
  *
- * @Since: 0.10.1
+ * Since: 0.10.1
  */
 void
 gst_base_src_set_format (GstBaseSrc * src, GstFormat format)
 {
+  g_return_if_fail (GST_IS_BASE_SRC (src));
+  g_return_if_fail (GST_STATE (src) <= GST_STATE_READY);
+
+  GST_OBJECT_LOCK (src);
   gst_segment_init (&src->segment, format);
+  GST_OBJECT_UNLOCK (src);
+}
+
+/**
+ * gst_base_src_set_dynamic_size:
+ * @src: base source instance
+ * @dynamic: new dynamic size mode
+ *
+ * If not @dynamic, size is only updated when needed, such as when trying to
+ * read past current tracked size.  Otherwise, size is checked for upon each
+ * read.
+ *
+ * Since: 0.10.35
+ */
+void
+gst_base_src_set_dynamic_size (GstBaseSrc * src, gboolean dynamic)
+{
+  g_return_if_fail (GST_IS_BASE_SRC (src));
+
+  g_atomic_int_set (&src->priv->dynamic_size, dynamic);
 }
 
 /**
  * gst_base_src_query_latency:
  * @src: the source
- * @live: if the source is live
- * @min_latency: the min latency of the source
- * @max_latency: the max latency of the source
+ * @live: (out) (allow-none): if the source is live
+ * @min_latency: (out) (allow-none): the min latency of the source
+ * @max_latency: (out) (allow-none): the max latency of the source
  *
  * Query the source for the latency parameters. @live will be TRUE when @src is
  * configured as a live source. @min_latency will be set to the difference
  * between the running time and the timestamp of the first buffer.
  * @max_latency is always the undefined value of -1.
  *
- * This function is mostly used by subclasses. 
+ * This function is mostly used by subclasses.
  *
  * Returns: TRUE if the query succeeded.
  *
@@ -598,7 +630,9 @@ gst_base_src_query_latency (GstBaseSrc * src, gboolean * live,
 {
   GstClockTime min;
 
-  GST_LIVE_LOCK (src);
+  g_return_val_if_fail (GST_IS_BASE_SRC (src), FALSE);
+
+  GST_OBJECT_LOCK (src);
   if (live)
     *live = src->is_live;
 
@@ -618,12 +652,59 @@ gst_base_src_query_latency (GstBaseSrc * src, gboolean * live,
   GST_LOG_OBJECT (src, "latency: live %d, min %" GST_TIME_FORMAT
       ", max %" GST_TIME_FORMAT, src->is_live, GST_TIME_ARGS (min),
       GST_TIME_ARGS (-1));
-  GST_LIVE_UNLOCK (src);
+  GST_OBJECT_UNLOCK (src);
 
   return TRUE;
 }
 
 /**
+ * gst_base_src_set_blocksize:
+ * @src: the source
+ * @blocksize: the new blocksize in bytes
+ *
+ * Set the number of bytes that @src will push out with each buffer. When
+ * @blocksize is set to -1, a default length will be used.
+ *
+ * Since: 0.10.22
+ */
+/* FIXME 0.11: blocksize property should be int, not ulong */
+void
+gst_base_src_set_blocksize (GstBaseSrc * src, gulong blocksize)
+{
+  g_return_if_fail (GST_IS_BASE_SRC (src));
+
+  GST_OBJECT_LOCK (src);
+  src->blocksize = blocksize;
+  GST_OBJECT_UNLOCK (src);
+}
+
+/**
+ * gst_base_src_get_blocksize:
+ * @src: the source
+ *
+ * Get the number of bytes that @src will push out with each buffer.
+ *
+ * Returns: the number of bytes pushed with each buffer.
+ *
+ * Since: 0.10.22
+ */
+/* FIXME 0.11: blocksize property should be int, not ulong */
+gulong
+gst_base_src_get_blocksize (GstBaseSrc * src)
+{
+  gulong res;
+
+  g_return_val_if_fail (GST_IS_BASE_SRC (src), 0);
+
+  GST_OBJECT_LOCK (src);
+  res = src->blocksize;
+  GST_OBJECT_UNLOCK (src);
+
+  return res;
+}
+
+
+/**
  * gst_base_src_set_do_timestamp:
  * @src: the source
  * @timestamp: enable or disable timestamping
@@ -637,6 +718,8 @@ gst_base_src_query_latency (GstBaseSrc * src, gboolean * live,
 void
 gst_base_src_set_do_timestamp (GstBaseSrc * src, gboolean timestamp)
 {
+  g_return_if_fail (GST_IS_BASE_SRC (src));
+
   GST_OBJECT_LOCK (src);
   src->priv->do_timestamp = timestamp;
   GST_OBJECT_UNLOCK (src);
@@ -657,6 +740,8 @@ gst_base_src_get_do_timestamp (GstBaseSrc * src)
 {
   gboolean res;
 
+  g_return_val_if_fail (GST_IS_BASE_SRC (src), FALSE);
+
   GST_OBJECT_LOCK (src);
   res = src->priv->do_timestamp;
   GST_OBJECT_UNLOCK (src);
@@ -664,6 +749,71 @@ gst_base_src_get_do_timestamp (GstBaseSrc * src)
   return res;
 }
 
+/**
+ * gst_base_src_new_seamless_segment:
+ * @src: The source
+ * @start: The new start value for the segment
+ * @stop: Stop value for the new segment
+ * @position: The position value for the new segent
+ *
+ * Prepare a new seamless segment for emission downstream. This function must
+ * only be called by derived sub-classes, and only from the create() function,
+ * as the stream-lock needs to be held.
+ *
+ * The format for the new segment will be the current format of the source, as
+ * configured with gst_base_src_set_format()
+ *
+ * Returns: %TRUE if preparation of the seamless segment succeeded.
+ *
+ * Since: 0.10.26
+ */
+gboolean
+gst_base_src_new_seamless_segment (GstBaseSrc * src, gint64 start, gint64 stop,
+    gint64 position)
+{
+  gboolean res = TRUE;
+
+  GST_DEBUG_OBJECT (src,
+      "Starting new seamless segment. Start %" GST_TIME_FORMAT " stop %"
+      GST_TIME_FORMAT " position %" GST_TIME_FORMAT, GST_TIME_ARGS (start),
+      GST_TIME_ARGS (stop), GST_TIME_ARGS (position));
+
+  GST_OBJECT_LOCK (src);
+  if (src->data.ABI.running && !src->priv->newsegment_pending) {
+    if (src->priv->close_segment)
+      gst_event_unref (src->priv->close_segment);
+    src->priv->close_segment =
+        gst_event_new_new_segment_full (TRUE,
+        src->segment.rate, src->segment.applied_rate, src->segment.format,
+        src->segment.start, src->segment.last_stop, src->segment.time);
+  }
+
+  gst_segment_set_newsegment_full (&src->segment, FALSE, src->segment.rate,
+      src->segment.applied_rate, src->segment.format, start, stop, position);
+
+  if (src->priv->start_segment)
+    gst_event_unref (src->priv->start_segment);
+  if (src->segment.rate >= 0.0) {
+    /* forward, we send data from last_stop to stop */
+    src->priv->start_segment =
+        gst_event_new_new_segment_full (FALSE,
+        src->segment.rate, src->segment.applied_rate, src->segment.format,
+        src->segment.last_stop, stop, src->segment.time);
+  } else {
+    /* reverse, we send data from last_stop to start */
+    src->priv->start_segment =
+        gst_event_new_new_segment_full (FALSE,
+        src->segment.rate, src->segment.applied_rate, src->segment.format,
+        src->segment.start, src->segment.last_stop, src->segment.time);
+  }
+  GST_OBJECT_UNLOCK (src);
+
+  src->priv->discont = TRUE;
+  src->data.ABI.running = TRUE;
+
+  return res;
+}
+
 static gboolean
 gst_base_src_setcaps (GstPad * pad, GstCaps * caps)
 {
@@ -730,6 +880,10 @@ gst_base_src_default_query (GstBaseSrc * src, GstQuery * query)
       GstFormat format;
 
       gst_query_parse_position (query, &format, NULL);
+
+      GST_DEBUG_OBJECT (src, "position query in format %s",
+          gst_format_get_name (format));
+
       switch (format) {
         case GST_FORMAT_PERCENT:
         {
@@ -737,8 +891,10 @@ gst_base_src_default_query (GstBaseSrc * src, GstQuery * query)
           gint64 position;
           gint64 duration;
 
+          GST_OBJECT_LOCK (src);
           position = src->segment.last_stop;
           duration = src->segment.duration;
+          GST_OBJECT_UNLOCK (src);
 
           if (position != -1 && duration != -1) {
             if (position < duration)
@@ -756,13 +912,19 @@ gst_base_src_default_query (GstBaseSrc * src, GstQuery * query)
         default:
         {
           gint64 position;
+          GstFormat seg_format;
 
-          position = src->segment.last_stop;
+          GST_OBJECT_LOCK (src);
+          position =
+              gst_segment_to_stream_time (&src->segment, src->segment.format,
+              src->segment.last_stop);
+          seg_format = src->segment.format;
+          GST_OBJECT_UNLOCK (src);
 
           if (position != -1) {
             /* convert to requested format */
             res =
-                gst_pad_query_convert (src->srcpad, src->segment.format,
+                gst_pad_query_convert (src->srcpad, seg_format,
                 position, &format, &position);
           } else
             res = TRUE;
@@ -781,6 +943,7 @@ gst_base_src_default_query (GstBaseSrc * src, GstQuery * query)
 
       GST_DEBUG_OBJECT (src, "duration query in format %s",
           gst_format_get_name (format));
+
       switch (format) {
         case GST_FORMAT_PERCENT:
           gst_query_set_duration (query, GST_FORMAT_PERCENT,
@@ -790,15 +953,33 @@ gst_base_src_default_query (GstBaseSrc * src, GstQuery * query)
         default:
         {
           gint64 duration;
+          GstFormat seg_format;
+          guint length = 0;
+
+          /* may have to refresh duration */
+          if (g_atomic_int_get (&src->priv->dynamic_size))
+            gst_base_src_update_length (src, 0, &length);
 
+          /* this is the duration as configured by the subclass. */
+          GST_OBJECT_LOCK (src);
           duration = src->segment.duration;
+          seg_format = src->segment.format;
+          GST_OBJECT_UNLOCK (src);
+
+          GST_LOG_OBJECT (src, "duration %" G_GINT64_FORMAT ", format %s",
+              duration, gst_format_get_name (seg_format));
 
           if (duration != -1) {
-            /* convert to requested format */
+            /* convert to requested format, if this fails, we have a duration
+             * but we cannot answer the query, we must return FALSE. */
             res =
-                gst_pad_query_convert (src->srcpad, src->segment.format,
+                gst_pad_query_convert (src->srcpad, seg_format,
                 duration, &format, &duration);
           } else {
+            /* The subclass did not configure a duration, we assume that the
+             * media has an unknown duration then and we return TRUE to report
+             * this. Note that this is not the same as returning FALSE, which
+             * means that we cannot report the duration at all. */
             res = TRUE;
           }
           gst_query_set_duration (query, format, duration);
@@ -810,15 +991,33 @@ gst_base_src_default_query (GstBaseSrc * src, GstQuery * query)
 
     case GST_QUERY_SEEKING:
     {
-      gst_query_set_seeking (query, src->segment.format,
-          src->seekable, 0, src->segment.duration);
-      res = TRUE;
+      GstFormat format, seg_format;
+      gint64 duration;
+
+      GST_OBJECT_LOCK (src);
+      duration = src->segment.duration;
+      seg_format = src->segment.format;
+      GST_OBJECT_UNLOCK (src);
+
+      gst_query_parse_seeking (query, &format, NULL, NULL, NULL);
+      if (format == seg_format) {
+        gst_query_set_seeking (query, seg_format,
+            gst_base_src_seekable (src), 0, duration);
+        res = TRUE;
+      } else {
+        /* FIXME 0.11: return TRUE + seekable=FALSE for SEEKING query here */
+        /* Don't reply to the query to make up for demuxers which don't
+         * handle the SEEKING query yet. Players like Totem will fall back
+         * to the duration when the SEEKING query isn't answered. */
+        res = FALSE;
+      }
       break;
     }
     case GST_QUERY_SEGMENT:
     {
       gint64 start, stop;
 
+      GST_OBJECT_LOCK (src);
       /* no end segment configured, current duration then */
       if ((stop = src->segment.stop) == -1)
         stop = src->segment.duration;
@@ -830,8 +1029,10 @@ gst_base_src_default_query (GstBaseSrc * src, GstQuery * query)
         if (stop != -1)
           stop -= src->segment.time;
       }
+
       gst_query_set_segment (query, src->segment.rate, src->segment.format,
           start, stop);
+      GST_OBJECT_UNLOCK (src);
       res = TRUE;
       break;
     }
@@ -865,7 +1066,7 @@ gst_base_src_default_query (GstBaseSrc * src, GstQuery * query)
       GstClockTime min, max;
       gboolean live;
 
-      /* Subclasses should override and implement something usefull */
+      /* Subclasses should override and implement something useful */
       res = gst_base_src_query_latency (src, &live, &min, &max);
 
       GST_LOG_OBJECT (src, "report latency: live %d, min %" GST_TIME_FORMAT
@@ -877,6 +1078,50 @@ gst_base_src_default_query (GstBaseSrc * src, GstQuery * query)
     }
     case GST_QUERY_JITTER:
     case GST_QUERY_RATE:
+      res = FALSE;
+      break;
+    case GST_QUERY_BUFFERING:
+    {
+      GstFormat format, seg_format;
+      gint64 start, stop, estimated;
+
+      gst_query_parse_buffering_range (query, &format, NULL, NULL, NULL);
+
+      GST_DEBUG_OBJECT (src, "buffering query in format %s",
+          gst_format_get_name (format));
+
+      GST_OBJECT_LOCK (src);
+      if (src->random_access) {
+        estimated = 0;
+        start = 0;
+        if (format == GST_FORMAT_PERCENT)
+          stop = GST_FORMAT_PERCENT_MAX;
+        else
+          stop = src->segment.duration;
+      } else {
+        estimated = -1;
+        start = -1;
+        stop = -1;
+      }
+      seg_format = src->segment.format;
+      GST_OBJECT_UNLOCK (src);
+
+      /* convert to required format. When the conversion fails, we can't answer
+       * the query. When the value is unknown, we can don't perform conversion
+       * but report TRUE. */
+      if (format != GST_FORMAT_PERCENT && stop != -1) {
+        res = gst_pad_query_convert (src->srcpad, seg_format,
+            stop, &format, &stop);
+      } else {
+        res = TRUE;
+      }
+      if (res && format != GST_FORMAT_PERCENT && start != -1)
+        res = gst_pad_query_convert (src->srcpad, seg_format,
+            start, &format, &start);
+
+      gst_query_set_buffering_range (query, format, start, stop, estimated);
+      break;
+    }
     default:
       res = FALSE;
       break;
@@ -894,6 +1139,8 @@ gst_base_src_query (GstPad * pad, GstQuery * query)
   gboolean result = FALSE;
 
   src = GST_BASE_SRC (gst_pad_get_parent (pad));
+  if (G_UNLIKELY (src == NULL))
+    return FALSE;
 
   bclass = GST_BASE_SRC_GET_CLASS (src);
 
@@ -914,15 +1161,14 @@ gst_base_src_default_do_seek (GstBaseSrc * src, GstSegment * segment)
 
   /* update our offset if the start/stop position was updated */
   if (segment->format == GST_FORMAT_BYTES) {
-    segment->last_stop = segment->start;
     segment->time = segment->start;
   } else if (segment->start == 0) {
     /* seek to start, we can implement a default for this. */
-    segment->last_stop = 0;
     segment->time = 0;
-    res = TRUE;
-  } else
+  } else {
     res = FALSE;
+    GST_INFO_OBJECT (src, "Can't do a default seek");
+  }
 
   return res;
 }
@@ -948,7 +1194,7 @@ gst_base_src_default_prepare_seek_segment (GstBaseSrc * src, GstEvent * event,
     GstSegment * segment)
 {
   /* By default, we try one of 2 things:
-   *   - For absolute seek positions, convert the requested position to our 
+   *   - For absolute seek positions, convert the requested position to our
    *     configured processing format and place it in the output segment \
    *   - For relative seek positions, convert our current (input) values to the
    *     seek format, adjust by the relative seek offset and then convert back to
@@ -1032,7 +1278,10 @@ gst_base_src_prepare_seek_segment (GstBaseSrc * src, GstEvent * event,
  * acquire the STREAM_LOCK which is taken when we are in the
  * _loop() function or when a getrange() is called. Normally
  * we will not receive a seek if we are operating in pull mode
- * though.
+ * though. When we operate as a live source we might block on the live
+ * cond, which does not release the STREAM_LOCK. Therefore we will try
+ * to grab the LIVE_LOCK instead of the STREAM_LOCK to make sure it is
+ * safe to perform the seek.
  *
  * When we are in the loop() function, we might be in the middle
  * of pushing a buffer, which might block in a sink. To make sure
@@ -1044,13 +1293,14 @@ gst_base_src_prepare_seek_segment (GstBaseSrc * src, GstEvent * event,
  * release the STREAM_LOCK. We say eventually because when the sink
  * blocks on the sample we might wait a very long time until the sink
  * unblocks the sample. In any case we acquire the STREAM_LOCK and
- * can continue the seek. A non-flushing seek is normally done in a 
- * running pipeline to perform seamless playback.
+ * can continue the seek. A non-flushing seek is normally done in a
+ * running pipeline to perform seamless playback, this means that the sink is
+ * PLAYING and will return from its chain function.
  * In the case of a non-flushing seek we need to make sure that the
  * data we output after the seek is continuous with the previous data,
- * this is because a non-flushing seek does not reset the stream-time
+ * this is because a non-flushing seek does not reset the running-time
  * to 0. We do this by closing the currently running segment, ie. sending
- * a new_segment event with the stop position set to the last processed 
+ * a new_segment event with the stop position set to the last processed
  * position.
  *
  * After updating the segment.start/stop values, we prepare for
@@ -1062,9 +1312,9 @@ gst_base_src_prepare_seek_segment (GstBaseSrc * src, GstEvent * event,
  * when we reach the segment.stop we have to post a segment.done
  * instead of EOS when doing a segment seek.
  */
-/* FIXME (0.11), we have the unlock gboolean here because most current 
+/* FIXME (0.11), we have the unlock gboolean here because most current
  * implementations (fdsrc, -base/gst/tcp/, ...) unconditionally unlock, even when
- * the streaming thread isn't running, resulting in bogus unlocks later when it 
+ * the streaming thread isn't running, resulting in bogus unlocks later when it
  * starts. This is fixed by adding unlock_stop, but we should still avoid unlocking
  * unnecessarily for backwards compatibility. Ergo, the unlock variable stays
  * until 0.11
@@ -1072,21 +1322,25 @@ gst_base_src_prepare_seek_segment (GstBaseSrc * src, GstEvent * event,
 static gboolean
 gst_base_src_perform_seek (GstBaseSrc * src, GstEvent * event, gboolean unlock)
 {
-  gboolean res = TRUE;
+  gboolean res = TRUE, tres;
   gdouble rate;
   GstFormat seek_format, dest_format;
   GstSeekFlags flags;
   GstSeekType cur_type, stop_type;
   gint64 cur, stop;
-  gboolean flush;
+  gboolean flush, playing;
   gboolean update;
   gboolean relative_seek = FALSE;
   gboolean seekseg_configured = FALSE;
   GstSegment seeksegment;
+  guint32 seqnum;
+  GstEvent *tevent;
 
-  GST_DEBUG_OBJECT (src, "doing seek");
+  GST_DEBUG_OBJECT (src, "doing seek: %" GST_PTR_FORMAT, event);
 
+  GST_OBJECT_LOCK (src);
   dest_format = src->segment.format;
+  GST_OBJECT_UNLOCK (src);
 
   if (event) {
     gst_event_parse_seek (event, &rate, &seek_format, &flags,
@@ -1098,7 +1352,8 @@ gst_base_src_perform_seek (GstBaseSrc * src, GstEvent * event, gboolean unlock)
     if (dest_format != seek_format && !relative_seek) {
       /* If we have an ABSOLUTE position (SEEK_SET only), we can convert it
        * here before taking the stream lock, otherwise we must convert it later,
-       * once we have the stream lock and can read the current position */
+       * once we have the stream lock and can read the last configures segment
+       * start and stop positions */
       gst_segment_init (&seeksegment, dest_format);
 
       if (!gst_base_src_prepare_seek_segment (src, event, &seeksegment))
@@ -1108,37 +1363,48 @@ gst_base_src_perform_seek (GstBaseSrc * src, GstEvent * event, gboolean unlock)
     }
 
     flush = flags & GST_SEEK_FLAG_FLUSH;
+    seqnum = gst_event_get_seqnum (event);
   } else {
     flush = FALSE;
+    /* get next seqnum */
+    seqnum = gst_util_seqnum_next ();
   }
 
   /* send flush start */
-  if (flush)
-    gst_pad_push_event (src->srcpad, gst_event_new_flush_start ());
-  else
+  if (flush) {
+    tevent = gst_event_new_flush_start ();
+    gst_event_set_seqnum (tevent, seqnum);
+    gst_pad_push_event (src->srcpad, tevent);
+  } else
     gst_pad_pause_task (src->srcpad);
 
-  /* unblock streaming thread */
-  if (unlock)
-    gst_base_src_unlock (src);
+  /* unblock streaming thread. */
+  gst_base_src_set_flushing (src, TRUE, FALSE, unlock, &playing);
 
   /* grab streaming lock, this should eventually be possible, either
-   * because the task is paused or our streaming thread stopped 
-   * because our peer is flushing. */
+   * because the task is paused, our streaming thread stopped
+   * or because our peer is flushing. */
   GST_PAD_STREAM_LOCK (src->srcpad);
+  if (G_UNLIKELY (src->priv->seqnum == seqnum)) {
+    /* we have seen this event before, issue a warning for now */
+    GST_WARNING_OBJECT (src, "duplicate event found %" G_GUINT32_FORMAT,
+        seqnum);
+  } else {
+    src->priv->seqnum = seqnum;
+    GST_DEBUG_OBJECT (src, "seek with seqnum %" G_GUINT32_FORMAT, seqnum);
+  }
 
-  if (unlock)
-    gst_base_src_unlock_stop (src);
+  gst_base_src_set_flushing (src, FALSE, playing, unlock, NULL);
 
   /* If we configured the seeksegment above, don't overwrite it now. Otherwise
    * copy the current segment info into the temp segment that we can actually
-   * attempt the seek with. We only update the real segment if the seek suceeds. */
+   * attempt the seek with. We only update the real segment if the seek succeeds. */
   if (!seekseg_configured) {
     memcpy (&seeksegment, &src->segment, sizeof (GstSegment));
 
     /* now configure the final seek segment */
     if (event) {
-      if (src->segment.format != seek_format) {
+      if (seeksegment.format != seek_format) {
         /* OK, here's where we give the subclass a chance to convert the relative
          * seek into an absolute one in the processing format. We set up any
          * absolute seek above, before taking the stream lock. */
@@ -1154,7 +1420,7 @@ gst_base_src_perform_seek (GstBaseSrc * src, GstEvent * event, gboolean unlock)
             cur_type, cur, stop_type, stop, &update);
       }
     }
-    /* Else, no seek event passed, so we're just (re)starting the 
+    /* Else, no seek event passed, so we're just (re)starting the
        current segment. */
   }
 
@@ -1169,11 +1435,13 @@ gst_base_src_perform_seek (GstBaseSrc * src, GstEvent * event, gboolean unlock)
 
   /* and prepare to continue streaming */
   if (flush) {
+    tevent = gst_event_new_flush_stop ();
+    gst_event_set_seqnum (tevent, seqnum);
     /* send flush stop, peer will accept data and events again. We
      * are not yet providing data as we still have the STREAM_LOCK. */
-    gst_pad_push_event (src->srcpad, gst_event_new_flush_stop ());
+    gst_pad_push_event (src->srcpad, tevent);
   } else if (res && src->data.ABI.running) {
-    /* we are running the current segment and doing a non-flushing seek, 
+    /* we are running the current segment and doing a non-flushing seek,
      * close the segment first based on the last_stop. */
     GST_DEBUG_OBJECT (src, "closing running segment %" G_GINT64_FORMAT
         " to %" G_GINT64_FORMAT, src->segment.start, src->segment.last_stop);
@@ -1185,9 +1453,10 @@ gst_base_src_perform_seek (GstBaseSrc * src, GstEvent * event, gboolean unlock)
         gst_event_new_new_segment_full (TRUE,
         src->segment.rate, src->segment.applied_rate, src->segment.format,
         src->segment.start, src->segment.last_stop, src->segment.time);
+    gst_event_set_seqnum (src->priv->close_segment, seqnum);
   }
 
-  /* The subclass must have converted the segment to the processing format 
+  /* The subclass must have converted the segment to the processing format
    * by now */
   if (res && seeksegment.format != dest_format) {
     GST_DEBUG_OBJECT (src, "Subclass failed to prepare a seek segment "
@@ -1195,41 +1464,60 @@ gst_base_src_perform_seek (GstBaseSrc * src, GstEvent * event, gboolean unlock)
     res = FALSE;
   }
 
-  /* if successfull seek, we update our real segment and push
+  /* if the seek was successful, we update our real segment and push
    * out the new segment. */
   if (res) {
+    GST_OBJECT_LOCK (src);
     memcpy (&src->segment, &seeksegment, sizeof (GstSegment));
+    GST_OBJECT_UNLOCK (src);
+
+    if (seeksegment.flags & GST_SEEK_FLAG_SEGMENT) {
+      GstMessage *message;
+
+      message = gst_message_new_segment_start (GST_OBJECT (src),
+          seeksegment.format, seeksegment.last_stop);
+      gst_message_set_seqnum (message, seqnum);
 
-    if (src->segment.flags & GST_SEEK_FLAG_SEGMENT) {
-      gst_element_post_message (GST_ELEMENT (src),
-          gst_message_new_segment_start (GST_OBJECT (src),
-              src->segment.format, src->segment.last_stop));
+      gst_element_post_message (GST_ELEMENT (src), message);
     }
 
-    /* for deriving a stop position for the playback segment form the seek
+    /* for deriving a stop position for the playback segment from the seek
      * segment, we must take the duration when the stop is not set */
-    if ((stop = src->segment.stop) == -1)
-      stop = src->segment.duration;
+    if ((stop = seeksegment.stop) == -1)
+      stop = seeksegment.duration;
 
     GST_DEBUG_OBJECT (src, "Sending newsegment from %" G_GINT64_FORMAT
-        " to %" G_GINT64_FORMAT, src->segment.start, stop);
+        " to %" G_GINT64_FORMAT, seeksegment.start, stop);
 
     /* now replace the old segment so that we send it in the stream thread the
      * next time it is scheduled. */
     if (src->priv->start_segment)
       gst_event_unref (src->priv->start_segment);
-    src->priv->start_segment =
-        gst_event_new_new_segment_full (FALSE,
-        src->segment.rate, src->segment.applied_rate, src->segment.format,
-        src->segment.last_stop, stop, src->segment.time);
+    if (seeksegment.rate >= 0.0) {
+      /* forward, we send data from last_stop to stop */
+      src->priv->start_segment =
+          gst_event_new_new_segment_full (FALSE,
+          seeksegment.rate, seeksegment.applied_rate, seeksegment.format,
+          seeksegment.last_stop, stop, seeksegment.time);
+    } else {
+      /* reverse, we send data from last_stop to start */
+      src->priv->start_segment =
+          gst_event_new_new_segment_full (FALSE,
+          seeksegment.rate, seeksegment.applied_rate, seeksegment.format,
+          seeksegment.start, seeksegment.last_stop, seeksegment.time);
+    }
+    gst_event_set_seqnum (src->priv->start_segment, seqnum);
+    src->priv->newsegment_pending = TRUE;
   }
 
   src->priv->discont = TRUE;
   src->data.ABI.running = TRUE;
-  /* and restart the task in case it got paused explicitely or by
+  /* and restart the task in case it got paused explicitly or by
    * the FLUSH_START event we pushed out. */
-  gst_pad_start_task (src->srcpad, (GstTaskFunction) gst_base_src_loop,
+  tres = gst_pad_start_task (src->srcpad, (GstTaskFunction) gst_base_src_loop,
       src->srcpad);
+  if (res && !tres)
+    res = FALSE;
 
   /* and release the lock again so we can continue streaming */
   GST_PAD_STREAM_UNLOCK (src->srcpad);
@@ -1273,6 +1561,8 @@ gst_base_src_send_event (GstElement * element, GstEvent * event)
 
   src = GST_BASE_SRC (element);
 
+  GST_DEBUG_OBJECT (src, "handling event %p %" GST_PTR_FORMAT, event, event);
+
   switch (GST_EVENT_TYPE (event)) {
       /* bidirectional events */
     case GST_EVENT_FLUSH_START:
@@ -1283,14 +1573,60 @@ gst_base_src_send_event (GstElement * element, GstEvent * event)
 
       /* downstream serialized events */
     case GST_EVENT_EOS:
-      /* FIXME, queue EOS and make sure the task or pull function 
-       * perform the EOS actions. */
+    {
+      GstBaseSrcClass *bclass;
+
+      bclass = GST_BASE_SRC_GET_CLASS (src);
+
+      /* queue EOS and make sure the task or pull function performs the EOS
+       * actions.
+       *
+       * We have two possibilities:
+       *
+       *  - Before we are to enter the _create function, we check the pending_eos
+       *    first and do EOS instead of entering it.
+       *  - If we are in the _create function or we did not manage to set the
+       *    flag fast enough and we are about to enter the _create function,
+       *    we unlock it so that we exit with WRONG_STATE immediately. We then
+       *    check the EOS flag and do the EOS logic.
+       */
+      g_atomic_int_set (&src->priv->pending_eos, TRUE);
+      GST_DEBUG_OBJECT (src, "EOS marked, calling unlock");
+
+      /* unlock the _create function so that we can check the pending_eos flag
+       * and we can do EOS. This will eventually release the LIVE_LOCK again so
+       * that we can grab it and stop the unlock again. We don't take the stream
+       * lock so that this operation is guaranteed to never block. */
+      if (bclass->unlock)
+        bclass->unlock (src);
+
+      GST_DEBUG_OBJECT (src, "unlock called, waiting for LIVE_LOCK");
+
+      GST_LIVE_LOCK (src);
+      GST_DEBUG_OBJECT (src, "LIVE_LOCK acquired, calling unlock_stop");
+      /* now stop the unlock of the streaming thread again. Grabbing the live
+       * lock is enough because that protects the create function. */
+      if (bclass->unlock_stop)
+        bclass->unlock_stop (src);
+      GST_LIVE_UNLOCK (src);
+
+      result = TRUE;
       break;
+    }
     case GST_EVENT_NEWSEGMENT:
       /* sending random NEWSEGMENT downstream can break sync. */
       break;
     case GST_EVENT_TAG:
-      /* sending tags could be useful, FIXME insert in dataflow */
+    case GST_EVENT_CUSTOM_DOWNSTREAM:
+    case GST_EVENT_CUSTOM_BOTH:
+      /* Insert TAG, CUSTOM_DOWNSTREAM, CUSTOM_BOTH in the dataflow */
+      GST_OBJECT_LOCK (src);
+      src->priv->pending_events =
+          g_list_append (src->priv->pending_events, event);
+      g_atomic_int_set (&src->priv->have_events, TRUE);
+      GST_OBJECT_UNLOCK (src);
+      event = NULL;
+      result = TRUE;
       break;
     case GST_EVENT_BUFFERSIZE:
       /* does not seem to make much sense currently */
@@ -1311,6 +1647,7 @@ gst_base_src_send_event (GstElement * element, GstEvent * event)
       GST_OBJECT_UNLOCK (src->srcpad);
 
       if (started) {
+        GST_DEBUG_OBJECT (src, "performing seek");
         /* when we are running in push mode, we can execute the
          * seek right now, we need to unlock. */
         result = gst_base_src_perform_seek (src, event, TRUE);
@@ -1320,6 +1657,7 @@ gst_base_src_send_event (GstElement * element, GstEvent * event)
         /* else we store the event and execute the seek when we
          * get activated */
         GST_OBJECT_LOCK (src);
+        GST_DEBUG_OBJECT (src, "queueing seek");
         event_p = &src->data.ABI.pending_seek;
         gst_event_replace ((GstEvent **) event_p, event);
         GST_OBJECT_UNLOCK (src);
@@ -1340,10 +1678,6 @@ gst_base_src_send_event (GstElement * element, GstEvent * event)
     case GST_EVENT_CUSTOM_UPSTREAM:
       /* override send_event if you want this */
       break;
-    case GST_EVENT_CUSTOM_DOWNSTREAM:
-    case GST_EVENT_CUSTOM_BOTH:
-      /* FIXME, insert event in the dataflow */
-      break;
     case GST_EVENT_CUSTOM_DOWNSTREAM_OOB:
     case GST_EVENT_CUSTOM_BOTH_OOB:
       /* insert a random custom event into the pipeline */
@@ -1373,14 +1707,42 @@ wrong_mode:
 }
 
 static gboolean
+gst_base_src_seekable (GstBaseSrc * src)
+{
+  GstBaseSrcClass *bclass;
+  bclass = GST_BASE_SRC_GET_CLASS (src);
+  if (bclass->is_seekable)
+    return bclass->is_seekable (src);
+  else
+    return FALSE;
+}
+
+static void
+gst_base_src_update_qos (GstBaseSrc * src,
+    gdouble proportion, GstClockTimeDiff diff, GstClockTime timestamp)
+{
+  GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, src,
+      "qos: proportion: %lf, diff %" G_GINT64_FORMAT ", timestamp %"
+      GST_TIME_FORMAT, proportion, diff, GST_TIME_ARGS (timestamp));
+
+  GST_OBJECT_LOCK (src);
+  src->priv->proportion = proportion;
+  src->priv->earliest_time = timestamp + diff;
+  GST_OBJECT_UNLOCK (src);
+}
+
+
+static gboolean
 gst_base_src_default_event (GstBaseSrc * src, GstEvent * event)
 {
   gboolean result;
 
+  GST_DEBUG_OBJECT (src, "handle event %" GST_PTR_FORMAT, event);
+
   switch (GST_EVENT_TYPE (event)) {
     case GST_EVENT_SEEK:
       /* is normally called when in push mode */
-      if (!src->seekable)
+      if (!gst_base_src_seekable (src))
         goto not_seekable;
 
       result = gst_base_src_perform_seek (src, event, TRUE);
@@ -1388,14 +1750,25 @@ gst_base_src_default_event (GstBaseSrc * src, GstEvent * event)
     case GST_EVENT_FLUSH_START:
       /* cancel any blocking getrange, is normally called
        * when in pull mode. */
-      result = gst_base_src_unlock (src);
+      result = gst_base_src_set_flushing (src, TRUE, FALSE, TRUE, NULL);
       break;
     case GST_EVENT_FLUSH_STOP:
-      result = gst_base_src_unlock_stop (src);
+      result = gst_base_src_set_flushing (src, FALSE, TRUE, TRUE, NULL);
       break;
-    default:
+    case GST_EVENT_QOS:
+    {
+      gdouble proportion;
+      GstClockTimeDiff diff;
+      GstClockTime timestamp;
+
+      gst_event_parse_qos (event, &proportion, &diff, &timestamp);
+      gst_base_src_update_qos (src, proportion, diff, timestamp);
       result = TRUE;
       break;
+    }
+    default:
+      result = FALSE;
+      break;
   }
   return result;
 
@@ -1415,6 +1788,11 @@ gst_base_src_event_handler (GstPad * pad, GstEvent * event)
   gboolean result = FALSE;
 
   src = GST_BASE_SRC (gst_pad_get_parent (pad));
+  if (G_UNLIKELY (src == NULL)) {
+    gst_event_unref (event);
+    return FALSE;
+  }
+
   bclass = GST_BASE_SRC_GET_CLASS (src);
 
   if (bclass->event) {
@@ -1446,7 +1824,7 @@ gst_base_src_set_property (GObject * object, guint prop_id,
 
   switch (prop_id) {
     case PROP_BLOCKSIZE:
-      src->blocksize = g_value_get_ulong (value);
+      gst_base_src_set_blocksize (src, g_value_get_ulong (value));
       break;
     case PROP_NUM_BUFFERS:
       src->num_buffers = g_value_get_int (value);
@@ -1455,7 +1833,7 @@ gst_base_src_set_property (GObject * object, guint prop_id,
       src->data.ABI.typefind = g_value_get_boolean (value);
       break;
     case PROP_DO_TIMESTAMP:
-      src->priv->do_timestamp = g_value_get_boolean (value);
+      gst_base_src_set_do_timestamp (src, g_value_get_boolean (value));
       break;
     default:
       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
@@ -1473,7 +1851,7 @@ gst_base_src_get_property (GObject * object, guint prop_id, GValue * value,
 
   switch (prop_id) {
     case PROP_BLOCKSIZE:
-      g_value_set_ulong (value, src->blocksize);
+      g_value_set_ulong (value, gst_base_src_get_blocksize (src));
       break;
     case PROP_NUM_BUFFERS:
       g_value_set_int (value, src->num_buffers);
@@ -1482,7 +1860,7 @@ gst_base_src_get_property (GObject * object, guint prop_id, GValue * value,
       g_value_set_boolean (value, src->data.ABI.typefind);
       break;
     case PROP_DO_TIMESTAMP:
-      g_value_set_boolean (value, src->priv->do_timestamp);
+      g_value_set_boolean (value, gst_base_src_get_do_timestamp (src));
       break;
     default:
       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
@@ -1500,19 +1878,19 @@ gst_base_src_wait (GstBaseSrc * basesrc, GstClock * clock, GstClockTime time)
   id = gst_clock_new_single_shot_id (clock, time);
 
   basesrc->clock_id = id;
-  /* release the object lock while waiting */
-  GST_OBJECT_UNLOCK (basesrc);
+  /* release the live lock while waiting */
+  GST_LIVE_UNLOCK (basesrc);
 
   ret = gst_clock_id_wait (id, NULL);
 
-  GST_OBJECT_LOCK (basesrc);
+  GST_LIVE_LOCK (basesrc);
   gst_clock_id_unref (id);
   basesrc->clock_id = NULL;
 
   return ret;
 }
 
-/* perform synchronisation on a buffer. 
+/* perform synchronisation on a buffer.
  * with STREAM_LOCK.
  */
 static GstClockReturn
@@ -1535,7 +1913,7 @@ gst_base_src_do_sync (GstBaseSrc * basesrc, GstBuffer * buffer)
   /* get buffer timestamp */
   timestamp = GST_BUFFER_TIMESTAMP (buffer);
 
-  /* grab the lock to prepare for clocking and calculate the startup 
+  /* grab the lock to prepare for clocking and calculate the startup
    * latency. */
   GST_OBJECT_LOCK (basesrc);
 
@@ -1626,6 +2004,9 @@ gst_base_src_do_sync (GstBaseSrc * basesrc, GstBuffer * buffer)
       now = gst_clock_get_time (clock);
 
       GST_BUFFER_TIMESTAMP (buffer) = now - base_time;
+
+      GST_LOG_OBJECT (basesrc, "created timestamp: %" GST_TIME_FORMAT,
+          GST_TIME_ARGS (now - base_time));
     }
   }
 
@@ -1643,9 +2024,9 @@ gst_base_src_do_sync (GstBaseSrc * basesrc, GstBuffer * buffer)
       "waiting for clock, base time %" GST_TIME_FORMAT
       ", stream_start %" GST_TIME_FORMAT,
       GST_TIME_ARGS (base_time), GST_TIME_ARGS (start));
+  GST_OBJECT_UNLOCK (basesrc);
 
   result = gst_base_src_wait (basesrc, clock, start + base_time);
-  GST_OBJECT_UNLOCK (basesrc);
 
   GST_LOG_OBJECT (basesrc, "clock entry done: %d", result);
 
@@ -1666,49 +2047,56 @@ no_sync:
   }
 }
 
+/* Called with STREAM_LOCK and LIVE_LOCK */
 static gboolean
 gst_base_src_update_length (GstBaseSrc * src, guint64 offset, guint * length)
 {
   guint64 size, maxsize;
   GstBaseSrcClass *bclass;
+  GstFormat format;
+  gint64 stop;
+  gboolean dynamic;
 
   bclass = GST_BASE_SRC_GET_CLASS (src);
 
-  /* only operate if we are working with bytes */
-  if (src->segment.format != GST_FORMAT_BYTES)
-    return TRUE;
-
+  format = src->segment.format;
+  stop = src->segment.stop;
   /* get total file size */
   size = (guint64) src->segment.duration;
 
+  /* only operate if we are working with bytes */
+  if (format != GST_FORMAT_BYTES)
+    return TRUE;
+
   /* the max amount of bytes to read is the total size or
    * up to the segment.stop if present. */
-  if (src->segment.stop != -1)
-    maxsize = MIN (size, src->segment.stop);
+  if (stop != -1)
+    maxsize = MIN (size, stop);
   else
     maxsize = size;
 
   GST_DEBUG_OBJECT (src,
       "reading offset %" G_GUINT64_FORMAT ", length %u, size %" G_GINT64_FORMAT
       ", segment.stop %" G_GINT64_FORMAT ", maxsize %" G_GINT64_FORMAT, offset,
-      *length, size, src->segment.stop, maxsize);
+      *length, size, stop, maxsize);
+
+  dynamic = g_atomic_int_get (&src->priv->dynamic_size);
+  GST_DEBUG_OBJECT (src, "dynamic size: %d", dynamic);
 
   /* check size if we have one */
   if (maxsize != -1) {
-    /* if we run past the end, check if the file became bigger and 
+    /* if we run past the end, check if the file became bigger and
      * retry. */
-    if (G_UNLIKELY (offset + *length >= maxsize)) {
+    if (G_UNLIKELY (offset + *length >= maxsize || dynamic)) {
       /* see if length of the file changed */
       if (bclass->get_size)
         if (!bclass->get_size (src, &size))
           size = -1;
 
-      gst_segment_set_duration (&src->segment, GST_FORMAT_BYTES, size);
-
       /* make sure we don't exceed the configured segment stop
        * if it was set */
-      if (src->segment.stop != -1)
-        maxsize = MIN (size, src->segment.stop);
+      if (stop != -1)
+        maxsize = MIN (size, stop);
       else
         maxsize = size;
 
@@ -1723,9 +2111,11 @@ gst_base_src_update_length (GstBaseSrc * src, guint64 offset, guint * length)
     }
   }
 
-  /* keep track of current position. segment is in bytes, we checked 
-   * that above. */
-  gst_segment_set_last_stop (&src->segment, GST_FORMAT_BYTES, offset);
+  /* keep track of current position and update duration.
+   * segment is in bytes, we checked that above. */
+  GST_OBJECT_LOCK (src);
+  gst_segment_set_duration (&src->segment, GST_FORMAT_BYTES, size);
+  GST_OBJECT_UNLOCK (src);
 
   return TRUE;
 
@@ -1736,6 +2126,7 @@ unexpected_length:
   }
 }
 
+/* must be called with LIVE_LOCK */
 static GstFlowReturn
 gst_base_src_get_range (GstBaseSrc * src, guint64 offset, guint length,
     GstBuffer ** buf)
@@ -1746,9 +2137,14 @@ gst_base_src_get_range (GstBaseSrc * src, guint64 offset, guint length,
 
   bclass = GST_BASE_SRC_GET_CLASS (src);
 
-  ret = gst_base_src_wait_playing (src);
-  if (ret != GST_FLOW_OK)
-    goto stopped;
+again:
+  if (src->is_live) {
+    if (G_UNLIKELY (!src->live_running)) {
+      ret = gst_base_src_wait_playing (src);
+      if (ret != GST_FLOW_OK)
+        goto stopped;
+    }
+  }
 
   if (G_UNLIKELY (!GST_OBJECT_FLAG_IS_SET (src, GST_BASE_SRC_STARTED)))
     goto not_started;
@@ -1759,6 +2155,12 @@ gst_base_src_get_range (GstBaseSrc * src, guint64 offset, guint length,
   if (G_UNLIKELY (!gst_base_src_update_length (src, offset, &length)))
     goto unexpected_length;
 
+  /* track position */
+  GST_OBJECT_LOCK (src);
+  if (src->segment.format == GST_FORMAT_BYTES)
+    gst_segment_set_last_stop (&src->segment, GST_FORMAT_BYTES, offset);
+  GST_OBJECT_UNLOCK (src);
+
   /* normally we don't count buffers */
   if (G_UNLIKELY (src->num_buffers_left >= 0)) {
     if (src->num_buffers_left == 0)
@@ -1767,21 +2169,51 @@ gst_base_src_get_range (GstBaseSrc * src, guint64 offset, guint length,
       src->num_buffers_left--;
   }
 
+  /* don't enter the create function if a pending EOS event was set. For the
+   * logic of the pending_eos, check the event function of this class. */
+  if (G_UNLIKELY (g_atomic_int_get (&src->priv->pending_eos)))
+    goto eos;
+
   GST_DEBUG_OBJECT (src,
       "calling create offset %" G_GUINT64_FORMAT " length %u, time %"
       G_GINT64_FORMAT, offset, length, src->segment.time);
 
   ret = bclass->create (src, offset, length, buf);
+
+  /* The create function could be unlocked because we have a pending EOS. It's
+   * possible that we have a valid buffer from create that we need to
+   * discard when the create function returned _OK. */
+  if (G_UNLIKELY (g_atomic_int_get (&src->priv->pending_eos))) {
+    if (ret == GST_FLOW_OK) {
+      gst_buffer_unref (*buf);
+      *buf = NULL;
+    }
+    goto eos;
+  }
+
   if (G_UNLIKELY (ret != GST_FLOW_OK))
-    goto done;
+    goto not_ok;
 
   /* no timestamp set and we are at offset 0, we can timestamp with 0 */
   if (offset == 0 && src->segment.time == 0
-      && GST_BUFFER_TIMESTAMP (*buf) == -1)
+      && GST_BUFFER_TIMESTAMP (*buf) == -1 && !src->is_live) {
+    *buf = gst_buffer_make_metadata_writable (*buf);
     GST_BUFFER_TIMESTAMP (*buf) = 0;
+  }
+
+  /* set pad caps on the buffer if the buffer had no caps */
+  if (GST_BUFFER_CAPS (*buf) == NULL) {
+    *buf = gst_buffer_make_metadata_writable (*buf);
+    gst_buffer_set_caps (*buf, GST_PAD_CAPS (src->srcpad));
+  }
 
   /* now sync before pushing the buffer */
   status = gst_base_src_do_sync (src, *buf);
+
+  /* waiting for the clock could have made us flushing */
+  if (G_UNLIKELY (src->priv->flushing))
+    goto flushing;
+
   switch (status) {
     case GST_CLOCK_EARLY:
       /* the buffer is too late. We currently don't drop the buffer. */
@@ -1793,14 +2225,23 @@ gst_base_src_get_range (GstBaseSrc * src, guint64 offset, guint length,
       break;
     case GST_CLOCK_UNSCHEDULED:
       /* this case is triggered when we were waiting for the clock and
-       * it got unlocked because we did a state change. We return 
-       * WRONG_STATE in this case to stop the dataflow also get rid of the
-       * produced buffer. */
-      GST_DEBUG_OBJECT (src,
-          "clock was unscheduled (%d), returning WRONG_STATE", status);
+       * it got unlocked because we did a state change. In any case, get rid of
+       * the buffer. */
       gst_buffer_unref (*buf);
       *buf = NULL;
-      ret = GST_FLOW_WRONG_STATE;
+      if (!src->live_running) {
+        /* We return WRONG_STATE when we are not running to stop the dataflow also
+         * get rid of the produced buffer. */
+        GST_DEBUG_OBJECT (src,
+            "clock was unscheduled (%d), returning WRONG_STATE", status);
+        ret = GST_FLOW_WRONG_STATE;
+      } else {
+        /* If we are running when this happens, we quickly switched between
+         * pause and playing. We try to produce a new buffer */
+        GST_DEBUG_OBJECT (src,
+            "clock was unscheduled (%d), but we are running", status);
+        goto again;
+      }
       break;
     default:
       /* all other result values are unexpected and errors */
@@ -1812,13 +2253,19 @@ gst_base_src_get_range (GstBaseSrc * src, guint64 offset, guint length,
       ret = GST_FLOW_ERROR;
       break;
   }
-done:
   return ret;
 
   /* ERROR */
 stopped:
   {
-    GST_DEBUG_OBJECT (src, "wait_playing returned %d", ret);
+    GST_DEBUG_OBJECT (src, "wait_playing returned %d (%s)", ret,
+        gst_flow_get_name (ret));
+    return ret;
+  }
+not_ok:
+  {
+    GST_DEBUG_OBJECT (src, "create returned %d (%s)", ret,
+        gst_flow_get_name (ret));
     return ret;
   }
 not_started:
@@ -1842,6 +2289,18 @@ reached_num_buffers:
     GST_DEBUG_OBJECT (src, "sent all buffers");
     return GST_FLOW_UNEXPECTED;
   }
+flushing:
+  {
+    GST_DEBUG_OBJECT (src, "we are flushing");
+    gst_buffer_unref (*buf);
+    *buf = NULL;
+    return GST_FLOW_WRONG_STATE;
+  }
+eos:
+  {
+    GST_DEBUG_OBJECT (src, "we are EOS");
+    return GST_FLOW_UNEXPECTED;
+  }
 }
 
 static GstFlowReturn
@@ -1851,13 +2310,28 @@ gst_base_src_pad_get_range (GstPad * pad, guint64 offset, guint length,
   GstBaseSrc *src;
   GstFlowReturn res;
 
-  src = GST_BASE_SRC (gst_pad_get_parent (pad));
+  src = GST_BASE_SRC_CAST (gst_object_ref (GST_OBJECT_PARENT (pad)));
+
+  GST_LIVE_LOCK (src);
+  if (G_UNLIKELY (src->priv->flushing))
+    goto flushing;
 
   res = gst_base_src_get_range (src, offset, length, buf);
 
+done:
+  GST_LIVE_UNLOCK (src);
+
   gst_object_unref (src);
 
   return res;
+
+  /* ERRORS */
+flushing:
+  {
+    GST_DEBUG_OBJECT (src, "we are flushing");
+    res = GST_FLOW_WRONG_STATE;
+    goto done;
+  }
 }
 
 static gboolean
@@ -1910,12 +2384,10 @@ gst_base_src_pad_check_get_range (GstPad * pad)
   GstBaseSrc *src;
   gboolean res;
 
-  src = GST_BASE_SRC (gst_pad_get_parent (pad));
+  src = GST_BASE_SRC (GST_OBJECT_PARENT (pad));
 
   res = gst_base_src_check_get_range (src);
 
-  gst_object_unref (src);
-
   return res;
 }
 
@@ -1927,23 +2399,47 @@ gst_base_src_loop (GstPad * pad)
   GstFlowReturn ret;
   gint64 position;
   gboolean eos;
+  gulong blocksize;
+  GList *pending_events = NULL, *tmp;
 
   eos = FALSE;
 
-  src = GST_BASE_SRC (gst_pad_get_parent (pad));
+  src = GST_BASE_SRC (GST_OBJECT_PARENT (pad));
+
+  GST_LIVE_LOCK (src);
+
+  if (G_UNLIKELY (src->priv->flushing))
+    goto flushing;
 
   src->priv->last_sent_eos = FALSE;
 
+  blocksize = src->blocksize;
+
   /* if we operate in bytes, we can calculate an offset */
-  if (src->segment.format == GST_FORMAT_BYTES)
+  if (src->segment.format == GST_FORMAT_BYTES) {
     position = src->segment.last_stop;
-  else
+    /* for negative rates, start with subtracting the blocksize */
+    if (src->segment.rate < 0.0) {
+      /* we cannot go below segment.start */
+      if (position > src->segment.start + blocksize)
+        position -= blocksize;
+      else {
+        /* last block, remainder up to segment.start */
+        blocksize = position - src->segment.start;
+        position = src->segment.start;
+      }
+    }
+  } else
     position = -1;
 
-  ret = gst_base_src_get_range (src, position, src->blocksize, &buf);
+  GST_LOG_OBJECT (src, "next_ts %" GST_TIME_FORMAT " size %lu",
+      GST_TIME_ARGS (position), blocksize);
+
+  ret = gst_base_src_get_range (src, position, blocksize, &buf);
   if (G_UNLIKELY (ret != GST_FLOW_OK)) {
     GST_INFO_OBJECT (src, "pausing after gst_base_src_get_range() = %s",
         gst_flow_get_name (ret));
+    GST_LIVE_UNLOCK (src);
     goto pause;
   }
   /* this should not happen */
@@ -1951,20 +2447,45 @@ gst_base_src_loop (GstPad * pad)
     goto null_buffer;
 
   /* push events to close/start our segment before we push the buffer. */
-  if (src->priv->close_segment) {
+  if (G_UNLIKELY (src->priv->close_segment)) {
     gst_pad_push_event (pad, src->priv->close_segment);
     src->priv->close_segment = NULL;
   }
-  if (src->priv->start_segment) {
+  if (G_UNLIKELY (src->priv->start_segment)) {
     gst_pad_push_event (pad, src->priv->start_segment);
     src->priv->start_segment = NULL;
   }
+  src->priv->newsegment_pending = FALSE;
+
+  if (g_atomic_int_get (&src->priv->have_events)) {
+    GST_OBJECT_LOCK (src);
+    /* take the events */
+    pending_events = src->priv->pending_events;
+    src->priv->pending_events = NULL;
+    g_atomic_int_set (&src->priv->have_events, FALSE);
+    GST_OBJECT_UNLOCK (src);
+  }
+
+  /* Push out pending events if any */
+  if (G_UNLIKELY (pending_events != NULL)) {
+    for (tmp = pending_events; tmp; tmp = g_list_next (tmp)) {
+      GstEvent *ev = (GstEvent *) tmp->data;
+      gst_pad_push_event (pad, ev);
+    }
+    g_list_free (pending_events);
+  }
 
   /* figure out the new position */
   switch (src->segment.format) {
     case GST_FORMAT_BYTES:
-      position += GST_BUFFER_SIZE (buf);
+    {
+      guint bufsize = GST_BUFFER_SIZE (buf);
+
+      /* we subtracted above for negative rates */
+      if (src->segment.rate >= 0.0)
+        position += bufsize;
       break;
+    }
     case GST_FORMAT_TIME:
     {
       GstClockTime start, duration;
@@ -1977,25 +2498,48 @@ gst_base_src_loop (GstPad * pad)
       else
         position = src->segment.last_stop;
 
-      if (GST_CLOCK_TIME_IS_VALID (duration))
-        position += duration;
+      if (GST_CLOCK_TIME_IS_VALID (duration)) {
+        if (src->segment.rate >= 0.0)
+          position += duration;
+        else if (position > duration)
+          position -= duration;
+        else
+          position = 0;
+      }
       break;
     }
     case GST_FORMAT_DEFAULT:
-      position = GST_BUFFER_OFFSET_END (buf);
+      if (src->segment.rate >= 0.0)
+        position = GST_BUFFER_OFFSET_END (buf);
+      else
+        position = GST_BUFFER_OFFSET (buf);
       break;
     default:
       position = -1;
       break;
   }
   if (position != -1) {
-    if (src->segment.stop != -1) {
-      if (position >= src->segment.stop) {
+    if (src->segment.rate >= 0.0) {
+      /* positive rate, check if we reached the stop */
+      if (src->segment.stop != -1) {
+        if (position >= src->segment.stop) {
+          eos = TRUE;
+          position = src->segment.stop;
+        }
+      }
+    } else {
+      /* negative rate, check if we reached the start. start is always set to
+       * something different from -1 */
+      if (position <= src->segment.start) {
         eos = TRUE;
-        position = src->segment.stop;
+        position = src->segment.start;
       }
+      /* when going reverse, all buffers are DISCONT */
+      src->priv->discont = TRUE;
     }
+    GST_OBJECT_LOCK (src);
     gst_segment_set_last_stop (&src->segment, src->segment.format, position);
+    GST_OBJECT_UNLOCK (src);
   }
 
   if (G_UNLIKELY (src->priv->discont)) {
@@ -2003,6 +2547,7 @@ gst_base_src_loop (GstPad * pad)
     GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
     src->priv->discont = FALSE;
   }
+  GST_LIVE_UNLOCK (src);
 
   ret = gst_pad_push (pad, buf);
   if (G_UNLIKELY (ret != GST_FLOW_OK)) {
@@ -2011,44 +2556,68 @@ gst_base_src_loop (GstPad * pad)
     goto pause;
   }
 
-  if (eos) {
-    GST_INFO_OBJECT (src, "pausing after EOS");
+  if (G_UNLIKELY (eos)) {
+    GST_INFO_OBJECT (src, "pausing after end of segment");
     ret = GST_FLOW_UNEXPECTED;
     goto pause;
   }
 
 done:
-  gst_object_unref (src);
   return;
 
   /* special cases */
+flushing:
+  {
+    GST_DEBUG_OBJECT (src, "we are flushing");
+    GST_LIVE_UNLOCK (src);
+    ret = GST_FLOW_WRONG_STATE;
+    goto pause;
+  }
 pause:
   {
     const gchar *reason = gst_flow_get_name (ret);
+    GstEvent *event;
 
     GST_DEBUG_OBJECT (src, "pausing task, reason %s", reason);
     src->data.ABI.running = FALSE;
     gst_pad_pause_task (pad);
-    if (GST_FLOW_IS_FATAL (ret) || ret == GST_FLOW_NOT_LINKED) {
-      if (ret == GST_FLOW_UNEXPECTED) {
-        /* perform EOS logic */
-        if (src->segment.flags & GST_SEEK_FLAG_SEGMENT) {
-          gst_element_post_message (GST_ELEMENT_CAST (src),
-              gst_message_new_segment_done (GST_OBJECT_CAST (src),
-                  src->segment.format, src->segment.last_stop));
-        } else {
-          gst_pad_push_event (pad, gst_event_new_eos ());
-          src->priv->last_sent_eos = TRUE;
-        }
+    if (ret == GST_FLOW_UNEXPECTED) {
+      gboolean flag_segment;
+      GstFormat format;
+      gint64 last_stop;
+
+      /* perform EOS logic */
+      flag_segment = (src->segment.flags & GST_SEEK_FLAG_SEGMENT) != 0;
+      format = src->segment.format;
+      last_stop = src->segment.last_stop;
+
+      if (flag_segment) {
+        GstMessage *message;
+
+        message = gst_message_new_segment_done (GST_OBJECT_CAST (src),
+            format, last_stop);
+        gst_message_set_seqnum (message, src->priv->seqnum);
+        gst_element_post_message (GST_ELEMENT_CAST (src), message);
       } else {
-        /* for fatal errors we post an error message, post the error
-         * first so the app knows about the error first. */
-        GST_ELEMENT_ERROR (src, STREAM, FAILED,
-            (_("Internal data flow error.")),
-            ("streaming task paused, reason %s (%d)", reason, ret));
-        gst_pad_push_event (pad, gst_event_new_eos ());
+        event = gst_event_new_eos ();
+        gst_event_set_seqnum (event, src->priv->seqnum);
+        gst_pad_push_event (pad, event);
         src->priv->last_sent_eos = TRUE;
       }
+    } else if (ret == GST_FLOW_NOT_LINKED || ret <= GST_FLOW_UNEXPECTED) {
+      event = gst_event_new_eos ();
+      gst_event_set_seqnum (event, src->priv->seqnum);
+      /* for fatal errors we post an error message, post the error
+       * first so the app knows about the error first.
+       * Also don't do this for WRONG_STATE because it happens
+       * due to flushing and posting an error message because of
+       * that is the wrong thing to do, e.g. when we're doing
+       * a flushing seek. */
+      GST_ELEMENT_ERROR (src, STREAM, FAILED,
+          (_("Internal data flow error.")),
+          ("streaming task paused, reason %s (%d)", reason, ret));
+      gst_pad_push_event (pad, event);
+      src->priv->last_sent_eos = TRUE;
     }
     goto done;
   }
@@ -2056,69 +2625,15 @@ null_buffer:
   {
     GST_ELEMENT_ERROR (src, STREAM, FAILED,
         (_("Internal data flow error.")), ("element returned NULL buffer"));
-    /* we finished the segment on error */
-    src->data.ABI.running = FALSE;
-    gst_pad_pause_task (pad);
-    gst_pad_push_event (pad, gst_event_new_eos ());
-    src->priv->last_sent_eos = TRUE;
+    GST_LIVE_UNLOCK (src);
     goto done;
   }
 }
 
-/* this will always be called between start() and stop(). So you can rely on
- * resources allocated by start() and freed from stop(). This needs to be added
- * to the docs at some point. */
-static gboolean
-gst_base_src_unlock (GstBaseSrc * basesrc)
-{
-  GstBaseSrcClass *bclass;
-  gboolean result = TRUE;
-
-  GST_DEBUG ("unlock");
-  /* unblock whatever the subclass is doing */
-  bclass = GST_BASE_SRC_GET_CLASS (basesrc);
-  if (bclass->unlock)
-    result = bclass->unlock (basesrc);
-
-  GST_DEBUG ("unschedule clock");
-  /* and unblock the clock as well, if any */
-  GST_OBJECT_LOCK (basesrc);
-  if (basesrc->clock_id) {
-    gst_clock_id_unschedule (basesrc->clock_id);
-  }
-  GST_OBJECT_UNLOCK (basesrc);
-
-  GST_DEBUG ("unlock done");
-
-  return result;
-}
-
-/* this will always be called between start() and stop(). So you can rely on
- * resources allocated by start() and freed from stop(). This needs to be added
- * to the docs at some point. */
-static gboolean
-gst_base_src_unlock_stop (GstBaseSrc * basesrc)
-{
-  GstBaseSrcClass *bclass;
-  gboolean result = TRUE;
-
-  GST_DEBUG_OBJECT (basesrc, "unlock stop");
-
-  /* Finish a previous unblock request, allowing subclasses to flush command
-   * queues or whatever they need to do */
-  bclass = GST_BASE_SRC_GET_CLASS (basesrc);
-  if (bclass->unlock_stop)
-    result = bclass->unlock_stop (basesrc);
-
-  GST_DEBUG_OBJECT (basesrc, "unlock stop done");
-
-  return result;
-}
-
-/* default negotiation code. 
+/* default negotiation code.
  *
  * Take intersection between src and sink pads, take first
- * caps and fixate. 
+ * caps and fixate.
  */
 static gboolean
 gst_base_src_default_negotiate (GstBaseSrc * basesrc)
@@ -2129,34 +2644,31 @@ gst_base_src_default_negotiate (GstBaseSrc * basesrc)
   gboolean result = FALSE;
 
   /* first see what is possible on our source pad */
-  thiscaps = gst_pad_get_caps (GST_BASE_SRC_PAD (basesrc));
+  thiscaps = gst_pad_get_caps_reffed (GST_BASE_SRC_PAD (basesrc));
   GST_DEBUG_OBJECT (basesrc, "caps of src: %" GST_PTR_FORMAT, thiscaps);
   /* nothing or anything is allowed, we're done */
   if (thiscaps == NULL || gst_caps_is_any (thiscaps))
     goto no_nego_needed;
 
+  if (G_UNLIKELY (gst_caps_is_empty (thiscaps)))
+    goto no_caps;
+
   /* get the peer caps */
-  peercaps = gst_pad_peer_get_caps (GST_BASE_SRC_PAD (basesrc));
+  peercaps = gst_pad_peer_get_caps_reffed (GST_BASE_SRC_PAD (basesrc));
   GST_DEBUG_OBJECT (basesrc, "caps of peer: %" GST_PTR_FORMAT, peercaps);
   if (peercaps) {
-    GstCaps *icaps;
-
     /* get intersection */
-    icaps = gst_caps_intersect (thiscaps, peercaps);
-    GST_DEBUG_OBJECT (basesrc, "intersect: %" GST_PTR_FORMAT, icaps);
-    gst_caps_unref (thiscaps);
+    caps =
+        gst_caps_intersect_full (peercaps, thiscaps, GST_CAPS_INTERSECT_FIRST);
+    GST_DEBUG_OBJECT (basesrc, "intersect: %" GST_PTR_FORMAT, caps);
     gst_caps_unref (peercaps);
-    if (icaps) {
-      /* take first (and best, since they are sorted) possibility */
-      caps = gst_caps_copy_nth (icaps, 0);
-      gst_caps_unref (icaps);
-    }
   } else {
     /* no peer, work with our own caps then */
-    caps = thiscaps;
+    caps = gst_caps_copy (thiscaps);
   }
+  gst_caps_unref (thiscaps);
   if (caps) {
-    caps = gst_caps_make_writable (caps);
+    /* take first (and best, since they are sorted) possibility */
     gst_caps_truncate (caps);
 
     /* now fixate */
@@ -2169,12 +2681,14 @@ gst_base_src_default_negotiate (GstBaseSrc * basesrc)
          * nego is not needed */
         result = TRUE;
       } else if (gst_caps_is_fixed (caps)) {
-        /* yay, fixed caps, use those then */
-        gst_pad_set_caps (GST_BASE_SRC_PAD (basesrc), caps);
-        result = TRUE;
+        /* yay, fixed caps, use those then, it's possible that the subclass does
+         * not accept this caps after all and we have to fail. */
+        result = gst_pad_set_caps (GST_BASE_SRC_PAD (basesrc), caps);
       }
     }
     gst_caps_unref (caps);
+  } else {
+    GST_DEBUG_OBJECT (basesrc, "no common caps");
   }
   return result;
 
@@ -2185,6 +2699,15 @@ no_nego_needed:
       gst_caps_unref (thiscaps);
     return TRUE;
   }
+no_caps:
+  {
+    GST_ELEMENT_ERROR (basesrc, STREAM, FORMAT,
+        ("No supported formats found"),
+        ("This element did not produce valid caps"));
+    if (thiscaps)
+      gst_caps_unref (thiscaps);
+    return TRUE;
+  }
 }
 
 static gboolean
@@ -2207,6 +2730,8 @@ gst_base_src_start (GstBaseSrc * basesrc)
   GstBaseSrcClass *bclass;
   gboolean result;
   guint64 size;
+  gboolean seekable;
+  GstFormat format;
 
   if (GST_OBJECT_FLAG_IS_SET (basesrc, GST_BASE_SRC_STARTED))
     return TRUE;
@@ -2215,8 +2740,12 @@ gst_base_src_start (GstBaseSrc * basesrc)
 
   basesrc->num_buffers_left = basesrc->num_buffers;
 
+  GST_OBJECT_LOCK (basesrc);
   gst_segment_init (&basesrc->segment, basesrc->segment.format);
+  GST_OBJECT_UNLOCK (basesrc);
+
   basesrc->data.ABI.running = FALSE;
+  basesrc->priv->newsegment_pending = FALSE;
 
   bclass = GST_BASE_SRC_GET_CLASS (basesrc);
   if (bclass->start)
@@ -2229,8 +2758,10 @@ gst_base_src_start (GstBaseSrc * basesrc)
 
   GST_OBJECT_FLAG_SET (basesrc, GST_BASE_SRC_STARTED);
 
+  format = basesrc->segment.format;
+
   /* figure out the size */
-  if (basesrc->segment.format == GST_FORMAT_BYTES) {
+  if (format == GST_FORMAT_BYTES) {
     if (bclass->get_size) {
       if (!(result = bclass->get_size (basesrc, &size)))
         size = -1;
@@ -2241,27 +2772,23 @@ gst_base_src_start (GstBaseSrc * basesrc)
     GST_DEBUG_OBJECT (basesrc, "setting size %" G_GUINT64_FORMAT, size);
     /* only update the size when operating in bytes, subclass is supposed
      * to set duration in the start method for other formats */
+    GST_OBJECT_LOCK (basesrc);
     gst_segment_set_duration (&basesrc->segment, GST_FORMAT_BYTES, size);
+    GST_OBJECT_UNLOCK (basesrc);
   } else {
     size = -1;
   }
 
   GST_DEBUG_OBJECT (basesrc,
-      "format: %d, have size: %d, size: %" G_GUINT64_FORMAT ", duration: %"
-      G_GINT64_FORMAT, basesrc->segment.format, result, size,
+      "format: %s, have size: %d, size: %" G_GUINT64_FORMAT ", duration: %"
+      G_GINT64_FORMAT, gst_format_get_name (format), result, size,
       basesrc->segment.duration);
 
-  /* check if we can seek */
-  if (bclass->is_seekable)
-    basesrc->seekable = bclass->is_seekable (basesrc);
-  else
-    basesrc->seekable = FALSE;
-
-  GST_DEBUG_OBJECT (basesrc, "is seekable: %d", basesrc->seekable);
+  seekable = gst_base_src_seekable (basesrc);
+  GST_DEBUG_OBJECT (basesrc, "is seekable: %d", seekable);
 
   /* update for random access flag */
-  basesrc->random_access = basesrc->seekable &&
-      basesrc->segment.format == GST_FORMAT_BYTES;
+  basesrc->random_access = seekable && format == GST_FORMAT_BYTES;
 
   GST_DEBUG_OBJECT (basesrc, "is random_access: %d", basesrc->random_access);
 
@@ -2269,16 +2796,18 @@ gst_base_src_start (GstBaseSrc * basesrc)
   if (basesrc->random_access && basesrc->data.ABI.typefind && size != -1) {
     GstCaps *caps;
 
-    caps = gst_type_find_helper (basesrc->srcpad, size);
-    gst_pad_set_caps (basesrc->srcpad, caps);
+    if (!(caps = gst_type_find_helper (basesrc->srcpad, size)))
+      goto typefind_failed;
+
+    result = gst_pad_set_caps (basesrc->srcpad, caps);
     gst_caps_unref (caps);
   } else {
     /* use class or default negotiate function */
-    if (!gst_base_src_negotiate (basesrc))
+    if (!(result = gst_base_src_negotiate (basesrc)))
       goto could_not_negotiate;
   }
 
-  return TRUE;
+  return result;
 
   /* ERROR */
 could_not_start:
@@ -2296,6 +2825,14 @@ could_not_negotiate:
     gst_base_src_stop (basesrc);
     return FALSE;
   }
+typefind_failed:
+  {
+    GST_DEBUG_OBJECT (basesrc, "could not typefind, stopping");
+    GST_ELEMENT_ERROR (basesrc, STREAM, TYPE_NOT_FOUND, (NULL), (NULL));
+    /* we must call stop */
+    gst_base_src_stop (basesrc);
+    return FALSE;
+  }
 }
 
 static gboolean
@@ -2319,26 +2856,121 @@ gst_base_src_stop (GstBaseSrc * basesrc)
   return result;
 }
 
+/* start or stop flushing dataprocessing
+ */
 static gboolean
-gst_base_src_deactivate (GstBaseSrc * basesrc, GstPad * pad)
+gst_base_src_set_flushing (GstBaseSrc * basesrc,
+    gboolean flushing, gboolean live_play, gboolean unlock, gboolean * playing)
 {
-  gboolean result;
+  GstBaseSrcClass *bclass;
+
+  bclass = GST_BASE_SRC_GET_CLASS (basesrc);
+
+  if (flushing && unlock) {
+    /* unlock any subclasses, we need to do this before grabbing the
+     * LIVE_LOCK since we hold this lock before going into ::create. We pass an
+     * unlock to the params because of backwards compat (see seek handler)*/
+    if (bclass->unlock)
+      bclass->unlock (basesrc);
+  }
 
+  /* the live lock is released when we are blocked, waiting for playing or
+   * when we sync to the clock. */
   GST_LIVE_LOCK (basesrc);
-  basesrc->live_running = TRUE;
+  if (playing)
+    *playing = basesrc->live_running;
+  basesrc->priv->flushing = flushing;
+  if (flushing) {
+    /* if we are locked in the live lock, signal it to make it flush */
+    basesrc->live_running = TRUE;
+
+    /* clear pending EOS if any */
+    g_atomic_int_set (&basesrc->priv->pending_eos, FALSE);
+
+    /* step 1, now that we have the LIVE lock, clear our unlock request */
+    if (bclass->unlock_stop)
+      bclass->unlock_stop (basesrc);
+
+    /* step 2, unblock clock sync (if any) or any other blocking thing */
+    if (basesrc->clock_id)
+      gst_clock_id_unschedule (basesrc->clock_id);
+  } else {
+    /* signal the live source that it can start playing */
+    basesrc->live_running = live_play;
+
+    /* When unlocking drop all delayed events */
+    if (unlock) {
+      GST_OBJECT_LOCK (basesrc);
+      if (basesrc->priv->pending_events) {
+        g_list_foreach (basesrc->priv->pending_events, (GFunc) gst_event_unref,
+            NULL);
+        g_list_free (basesrc->priv->pending_events);
+        basesrc->priv->pending_events = NULL;
+        g_atomic_int_set (&basesrc->priv->have_events, FALSE);
+      }
+      GST_OBJECT_UNLOCK (basesrc);
+    }
+  }
   GST_LIVE_SIGNAL (basesrc);
   GST_LIVE_UNLOCK (basesrc);
 
-  /* step 1, unblock clock sync (if any) */
-  result = gst_base_src_unlock (basesrc);
+  return TRUE;
+}
 
-  /* step 2, make sure streaming finishes */
-  result &= gst_pad_stop_task (pad);
+/* the purpose of this function is to make sure that a live source blocks in the
+ * LIVE lock or leaves the LIVE lock and continues playing. */
+static gboolean
+gst_base_src_set_playing (GstBaseSrc * basesrc, gboolean live_play)
+{
+  GstBaseSrcClass *bclass;
 
-  /* step 3, clear the unblock condition */
-  result &= gst_base_src_unlock_stop (basesrc);
+  bclass = GST_BASE_SRC_GET_CLASS (basesrc);
 
-  return result;
+  /* unlock subclasses locked in ::create, we only do this when we stop playing. */
+  if (!live_play) {
+    GST_DEBUG_OBJECT (basesrc, "unlock");
+    if (bclass->unlock)
+      bclass->unlock (basesrc);
+  }
+
+  /* we are now able to grab the LIVE lock, when we get it, we can be
+   * waiting for PLAYING while blocked in the LIVE cond or we can be waiting
+   * for the clock. */
+  GST_LIVE_LOCK (basesrc);
+  GST_DEBUG_OBJECT (basesrc, "unschedule clock");
+
+  /* unblock clock sync (if any) */
+  if (basesrc->clock_id)
+    gst_clock_id_unschedule (basesrc->clock_id);
+
+  /* configure what to do when we get to the LIVE lock. */
+  GST_DEBUG_OBJECT (basesrc, "live running %d", live_play);
+  basesrc->live_running = live_play;
+
+  if (live_play) {
+    gboolean start;
+
+    /* clear our unlock request when going to PLAYING */
+    GST_DEBUG_OBJECT (basesrc, "unlock stop");
+    if (bclass->unlock_stop)
+      bclass->unlock_stop (basesrc);
+
+    /* for live sources we restart the timestamp correction */
+    basesrc->priv->latency = -1;
+    /* have to restart the task in case it stopped because of the unlock when
+     * we went to PAUSED. Only do this if we operating in push mode. */
+    GST_OBJECT_LOCK (basesrc->srcpad);
+    start = (GST_PAD_ACTIVATE_MODE (basesrc->srcpad) == GST_ACTIVATE_PUSH);
+    GST_OBJECT_UNLOCK (basesrc->srcpad);
+    if (start)
+      gst_pad_start_task (basesrc->srcpad, (GstTaskFunction) gst_base_src_loop,
+          basesrc->srcpad);
+    GST_DEBUG_OBJECT (basesrc, "signal");
+    GST_LIVE_SIGNAL (basesrc);
+  }
+  GST_LIVE_UNLOCK (basesrc);
+
+  return TRUE;
 }
 
 static gboolean
@@ -2360,6 +2992,8 @@ gst_base_src_activate_push (GstPad * pad, gboolean active)
       goto error_start;
 
     basesrc->priv->last_sent_eos = FALSE;
+    basesrc->priv->discont = TRUE;
+    gst_base_src_set_flushing (basesrc, FALSE, FALSE, FALSE, NULL);
 
     /* do initial seek, which will start the task */
     GST_OBJECT_LOCK (basesrc);
@@ -2377,10 +3011,10 @@ gst_base_src_activate_push (GstPad * pad, gboolean active)
       gst_event_unref (event);
   } else {
     GST_DEBUG_OBJECT (basesrc, "Deactivating in push mode");
-    /* call the unlock function and stop the task */
-    if (G_UNLIKELY (!gst_base_src_deactivate (basesrc, pad)))
-      goto deactivate_failed;
-
+    /* flush all */
+    gst_base_src_set_flushing (basesrc, TRUE, FALSE, TRUE, NULL);
+    /* stop the task */
+    gst_pad_stop_task (pad);
     /* now we can stop the source */
     if (G_UNLIKELY (!gst_base_src_stop (basesrc)))
       goto error_stop;
@@ -2401,16 +3035,16 @@ error_start:
 seek_failed:
   {
     GST_ERROR_OBJECT (basesrc, "Failed to perform initial seek");
+    /* flush all */
+    gst_base_src_set_flushing (basesrc, TRUE, FALSE, TRUE, NULL);
+    /* stop the task */
+    gst_pad_stop_task (pad);
+    /* Stop the basesrc */
     gst_base_src_stop (basesrc);
     if (event)
       gst_event_unref (event);
     return FALSE;
   }
-deactivate_failed:
-  {
-    GST_ERROR_OBJECT (basesrc, "Failed to deactivate in push mode");
-    return FALSE;
-  }
 error_stop:
   {
     GST_DEBUG_OBJECT (basesrc, "Failed to stop in push mode");
@@ -2434,11 +3068,14 @@ gst_base_src_activate_pull (GstPad * pad, gboolean active)
     /* if not random_access, we cannot operate in pull mode for now */
     if (G_UNLIKELY (!gst_base_src_check_get_range (basesrc)))
       goto no_get_range;
+
+    /* stop flushing now but for live sources, still block in the LIVE lock when
+     * we are not yet PLAYING */
+    gst_base_src_set_flushing (basesrc, FALSE, FALSE, FALSE, NULL);
   } else {
     GST_DEBUG_OBJECT (basesrc, "Deactivating in pull mode");
-    /* call the unlock function. We have no task to stop. */
-    if (G_UNLIKELY (!gst_base_src_deactivate (basesrc, pad)))
-      goto deactivate_failed;
+    /* flush all, there is no task to stop */
+    gst_base_src_set_flushing (basesrc, TRUE, FALSE, TRUE, NULL);
 
     /* don't send EOS when going from PAUSED => READY when in pull mode */
     basesrc->priv->last_sent_eos = TRUE;
@@ -2460,11 +3097,6 @@ no_get_range:
     gst_base_src_stop (basesrc);
     return FALSE;
   }
-deactivate_failed:
-  {
-    GST_ERROR_OBJECT (basesrc, "Failed to deactivate in pull mode");
-    return FALSE;
-  }
 error_stop:
   {
     GST_ERROR_OBJECT (basesrc, "Failed to stop in pull mode");
@@ -2485,23 +3117,14 @@ gst_base_src_change_state (GstElement * element, GstStateChange transition)
     case GST_STATE_CHANGE_NULL_TO_READY:
       break;
     case GST_STATE_CHANGE_READY_TO_PAUSED:
-      GST_LIVE_LOCK (element);
-      if (basesrc->is_live) {
-        no_preroll = TRUE;
-        basesrc->live_running = FALSE;
-      }
-      basesrc->priv->last_sent_eos = FALSE;
-      basesrc->priv->discont = TRUE;
-      GST_LIVE_UNLOCK (element);
+      no_preroll = gst_base_src_is_live (basesrc);
       break;
     case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
-      GST_LIVE_LOCK (element);
-      basesrc->priv->latency = -1;
-      if (basesrc->is_live) {
-        basesrc->live_running = TRUE;
-        GST_LIVE_SIGNAL (element);
+      GST_DEBUG_OBJECT (basesrc, "PAUSED->PLAYING");
+      if (gst_base_src_is_live (basesrc)) {
+        /* now we can start playback */
+        gst_base_src_set_playing (basesrc, TRUE);
       }
-      GST_LIVE_UNLOCK (element);
       break;
     default:
       break;
@@ -2514,25 +3137,31 @@ gst_base_src_change_state (GstElement * element, GstStateChange transition)
 
   switch (transition) {
     case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
-      GST_LIVE_LOCK (element);
-      if (basesrc->is_live) {
+      GST_DEBUG_OBJECT (basesrc, "PLAYING->PAUSED");
+      if (gst_base_src_is_live (basesrc)) {
+        /* make sure we block in the live lock in PAUSED */
+        gst_base_src_set_playing (basesrc, FALSE);
         no_preroll = TRUE;
-        basesrc->live_running = FALSE;
       }
-      GST_LIVE_UNLOCK (element);
       break;
     case GST_STATE_CHANGE_PAUSED_TO_READY:
     {
-      GstEvent **event_p;
+      GstEvent **event_p, *event;
+
+      /* we don't need to unblock anything here, the pad deactivation code
+       * already did this */
 
       /* FIXME, deprecate this behaviour, it is very dangerous.
-       * the prefered way of sending EOS downstream is by sending
+       * the preferred way of sending EOS downstream is by sending
        * the EOS event to the element */
       if (!basesrc->priv->last_sent_eos) {
         GST_DEBUG_OBJECT (basesrc, "Sending EOS event");
-        gst_pad_push_event (basesrc->srcpad, gst_event_new_eos ());
+        event = gst_event_new_eos ();
+        gst_event_set_seqnum (event, basesrc->priv->seqnum);
+        gst_pad_push_event (basesrc->srcpad, event);
         basesrc->priv->last_sent_eos = TRUE;
       }
+      g_atomic_int_set (&basesrc->priv->pending_eos, FALSE);
       event_p = &basesrc->data.ABI.pending_seek;
       gst_event_replace (event_p, NULL);
       event_p = &basesrc->priv->close_segment;