Merge branch 'master' into 0.11
[platform/upstream/gstreamer.git] / libs / gst / base / gstbasesink.c
index 0caed2d..470ef37 100644 (file)
@@ -22,7 +22,7 @@
 /**
  * SECTION:gstbasesink
  * @short_description: Base class for sink elements
- * @see_also: #GstBaseTransform, #GstBaseSource
+ * @see_also: #GstBaseTransform, #GstBaseSrc
  *
  * #GstBaseSink is the base class for sink elements in GStreamer, such as
  * xvimagesink or filesink. It is a layer on top of #GstElement that provides a
@@ -37,7 +37,7 @@
  * #GstBaseSink provides support for exactly one sink pad, which should be
  * named "sink". A sink implementation (subclass of #GstBaseSink) should
  * install a pad template in its base_init function, like so:
- * <programlisting>
+ * |[
  * static void
  * my_element_base_init (gpointer g_class)
  * {
  *   // see #GstElementDetails
  *   gst_element_class_set_details (gstelement_class, &amp;details);
  * }
- * </programlisting>
+ * ]|
  *
  * #GstBaseSink will handle the prerolling correctly. This means that it will
  * return #GST_STATE_CHANGE_ASYNC from a state change to PAUSED until the first
  * buffer arrives in this element. The base class will call the
- * #GstBaseSink::preroll vmethod with this preroll buffer and will then commit
- * the state change to the next asynchronously pending state.
+ * #GstBaseSinkClass.preroll() vmethod with this preroll buffer and will then
+ * commit the state change to the next asynchronously pending state.
  *
  * When the element is set to PLAYING, #GstBaseSink will synchronise on the
- * clock using the times returned from ::get_times. If this function returns
- * #GST_CLOCK_TIME_NONE for the start time, no synchronisation will be done.
- * Synchronisation can be disabled entirely by setting the object "sync"
- * property to %FALSE.
+ * clock using the times returned from #GstBaseSinkClass.get_times(). If this
+ * function returns #GST_CLOCK_TIME_NONE for the start time, no synchronisation
+ * will be done. Synchronisation can be disabled entirely by setting the object
+ * #GstBaseSink:sync property to %FALSE.
  *
- * After synchronisation the virtual method #GstBaseSink::render will be called.
- * Subclasses should minimally implement this method.
+ * After synchronisation the virtual method #GstBaseSinkClass.render() will be
+ * called. Subclasses should minimally implement this method.
  *
- * Since 0.10.3 subclasses that synchronise on the clock in the ::render method
- * are supported as well. These classes typically receive a buffer in the render
- * method and can then potentially block on the clock while rendering. A typical
- * example is an audiosink. Since 0.10.11 these subclasses can use
- * gst_base_sink_wait_preroll() to perform the blocking wait.
+ * Since 0.10.3 subclasses that synchronise on the clock in the
+ * #GstBaseSinkClass.render() method are supported as well. These classes
+ * typically receive a buffer in the render method and can then potentially
+ * block on the clock while rendering. A typical example is an audiosink.
+ * Since 0.10.11 these subclasses can use gst_base_sink_wait_preroll() to
+ * perform the blocking wait.
  *
  * Upon receiving the EOS event in the PLAYING state, #GstBaseSink will wait
  * for the clock to reach the time indicated by the stop time of the last
- * ::get_times call before posting an EOS message. When the element receives
- * EOS in PAUSED, preroll completes, the event is queued and an EOS message is
- * posted when going to PLAYING.
+ * #GstBaseSinkClass.get_times() call before posting an EOS message. When the
+ * element receives EOS in PAUSED, preroll completes, the event is queued and an
+ * EOS message is posted when going to PLAYING.
  *
  * #GstBaseSink will internally use the #GST_EVENT_NEWSEGMENT events to schedule
  * synchronisation and clipping of buffers. Buffers that fall completely outside
  * If no clock has been set on the element, the query will be forwarded
  * upstream.
  *
- * The ::set_caps function will be called when the subclass should configure
- * itself to process a specific media type.
+ * The #GstBaseSinkClass.set_caps() function will be called when the subclass
+ * should configure itself to process a specific media type.
  *
- * The ::start and ::stop virtual methods will be called when resources should
- * be allocated. Any ::preroll, ::render  and ::set_caps function will be
- * called between the ::start and ::stop calls.
+ * The #GstBaseSinkClass.start() and #GstBaseSinkClass.stop() virtual methods
+ * will be called when resources should be allocated. Any 
+ * #GstBaseSinkClass.preroll(), #GstBaseSinkClass.render() and
+ * #GstBaseSinkClass.set_caps() function will be called between the
+ * #GstBaseSinkClass.start() and #GstBaseSinkClass.stop() calls.
  *
- * The ::event virtual method will be called when an event is received by
- * #GstBaseSink. Normally this method should only be overriden by very specific
- * elements (such as file sinks) which need to handle the newsegment event
- * specially.
+ * The #GstBaseSinkClass.event() virtual method will be called when an event is
+ * received by #GstBaseSink. Normally this method should only be overriden by
+ * very specific elements (such as file sinks) which need to handle the
+ * newsegment event specially.
  *
- * #GstBaseSink provides an overridable ::buffer_alloc function that can be
- * used by sinks that want to do reverse negotiation or to provide
- * custom buffers (hardware buffers for example) to upstream elements.
+ * #GstBaseSink provides an overridable #GstBaseSinkClass.buffer_alloc()
+ * function that can be used by sinks that want to do reverse negotiation or to
+ * provide custom buffers (hardware buffers for example) to upstream elements.
  *
- * The ::unlock method is called when the elements should unblock any blocking
- * operations they perform in the ::render method. This is mostly useful when
- * the ::render method performs a blocking write on a file descriptor, for
- * example.
+ * The #GstBaseSinkClass.unlock() method is called when the elements should
+ * unblock any blocking operations they perform in the
+ * #GstBaseSinkClass.render() method. This is mostly useful when the
+ * #GstBaseSinkClass.render() method performs a blocking write on a file
+ * descriptor, for example.
  *
- * The max-lateness property affects how the sink deals with buffers that
- * arrive too late in the sink. A buffer arrives too late in the sink when
- * the presentation time (as a combination of the last segment, buffer
+ * The #GstBaseSink:max-lateness property affects how the sink deals with
+ * buffers that arrive too late in the sink. A buffer arrives too late in the
+ * sink when the presentation time (as a combination of the last segment, buffer
  * timestamp and element base_time) plus the duration is before the current
  * time of the clock.
  * If the frame is later than max-lateness, the sink will drop the buffer
  * without calling the render method.
- * This feature is disabled if sync is disabled, the ::get-times method does
- * not return a valid start time or max-lateness is set to -1 (the default).
+ * This feature is disabled if sync is disabled, the
+ * #GstBaseSinkClass.get_times() method does not return a valid start time or
+ * max-lateness is set to -1 (the default).
  * Subclasses can use gst_base_sink_set_max_lateness() to configure the
  * max-lateness value.
  *
- * The qos property will enable the quality-of-service features of the basesink
- * which gather statistics about the real-time performance of the clock
- * synchronisation. For each buffer received in the sink, statistics are
+ * The #GstBaseSink:qos property will enable the quality-of-service features of
+ * the basesink which gather statistics about the real-time performance of the
+ * clock synchronisation. For each buffer received in the sink, statistics are
  * gathered and a QOS event is sent upstream with these numbers. This
  * information can then be used by upstream elements to reduce their processing
  * rate, for example.
  *
- * Since 0.10.15 the async property can be used to instruct the sink to never
- * perform an ASYNC state change. This feature is mostly usable when dealing
- * with non-synchronized streams or sparse streams.
+ * Since 0.10.15 the #GstBaseSink:async property can be used to instruct the
+ * sink to never perform an ASYNC state change. This feature is mostly usable
+ * when dealing with non-synchronized streams or sparse streams.
  *
  * Last reviewed on 2007-08-29 (0.10.15)
  */
 #  include "config.h"
 #endif
 
+#include <gst/gst_private.h>
+
 #include "gstbasesink.h"
 #include <gst/gstmarshal.h>
-#include <gst/gst_private.h>
 #include <gst/gst-i18n-lib.h>
 
 GST_DEBUG_CATEGORY_STATIC (gst_base_sink_debug);
@@ -190,12 +196,14 @@ struct _GstBaseSinkPrivate
   GstClockTime current_rstart;
   GstClockTime current_rstop;
   GstClockTimeDiff current_jitter;
+  /* the running time of the previous buffer */
+  GstClockTime prev_rstart;
 
   /* EOS sync time in running time */
   GstClockTime eos_rtime;
 
   /* last buffer that arrived in time, running time */
-  GstClockTime last_in_time;
+  GstClockTime last_render_time;
   /* when the last buffer left the sink, running time */
   GstClockTime last_left;
 
@@ -203,6 +211,7 @@ struct _GstBaseSinkPrivate
   GstClockTime avg_pt;
   GstClockTime avg_duration;
   gdouble avg_rate;
+  GstClockTime avg_in_diff;
 
   /* these are done on system time. avg_jitter and avg_render are
    * compared to eachother to see if the rendering time takes a
@@ -230,6 +239,7 @@ struct _GstBaseSinkPrivate
   gboolean have_latency;
 
   /* the last buffer we prerolled or rendered. Useful for making snapshots */
+  gint enable_last_buffer;      /* atomic */
   GstBuffer *last_buffer;
 
   /* caps for pull based scheduling */
@@ -249,6 +259,13 @@ struct _GstBaseSinkPrivate
   /* we have a pending and a current step operation */
   GstStepInfo current_step;
   GstStepInfo pending_step;
+
+  /* Cached GstClockID */
+  GstClockID cached_clock_id;
+
+  /* for throttling and QoS */
+  GstClockTime earliest_in_time;
+  GstClockTime throttle_time;
 };
 
 #define DO_RUNNING_AVG(avg,val,size) (((val) + ((size)-1) * (avg)) / (size))
@@ -262,6 +279,19 @@ struct _GstBaseSinkPrivate
 #define UPDATE_RUNNING_AVG_P(avg,val) DO_RUNNING_AVG(avg,val,16)
 #define UPDATE_RUNNING_AVG_N(avg,val) DO_RUNNING_AVG(avg,val,4)
 
+enum
+{
+  _PR_IS_NOTHING = 1 << 0,
+  _PR_IS_BUFFER = 1 << 1,
+  _PR_IS_BUFFERLIST = 1 << 2,
+  _PR_IS_EVENT = 1 << 3
+} PrivateObjectType;
+
+#define OBJ_IS_BUFFER(a) ((a) & _PR_IS_BUFFER)
+#define OBJ_IS_BUFFERLIST(a) ((a) & _PR_IS_BUFFERLIST)
+#define OBJ_IS_EVENT(a) ((a) & _PR_IS_EVENT)
+#define OBJ_IS_BUFFERFULL(a) ((a) & (_PR_IS_BUFFER | _PR_IS_BUFFERLIST))
+
 /* BaseSink properties */
 
 #define DEFAULT_CAN_ACTIVATE_PULL FALSE /* fixme: enable me */
@@ -275,6 +305,8 @@ struct _GstBaseSinkPrivate
 #define DEFAULT_TS_OFFSET           0
 #define DEFAULT_BLOCKSIZE           4096
 #define DEFAULT_RENDER_DELAY        0
+#define DEFAULT_ENABLE_LAST_BUFFER  TRUE
+#define DEFAULT_THROTTLE_TIME       0
 
 enum
 {
@@ -285,9 +317,11 @@ enum
   PROP_QOS,
   PROP_ASYNC,
   PROP_TS_OFFSET,
+  PROP_ENABLE_LAST_BUFFER,
   PROP_LAST_BUFFER,
   PROP_BLOCKSIZE,
   PROP_RENDER_DELAY,
+  PROP_THROTTLE_TIME,
   PROP_LAST
 };
 
@@ -331,6 +365,7 @@ static void gst_base_sink_get_property (GObject * object, guint prop_id,
 static gboolean gst_base_sink_send_event (GstElement * element,
     GstEvent * event);
 static gboolean gst_base_sink_query (GstElement * element, GstQuery * query);
+static const GstQueryType *gst_base_sink_get_query_types (GstElement * element);
 
 static GstCaps *gst_base_sink_get_caps (GstBaseSink * sink);
 static gboolean gst_base_sink_set_caps (GstBaseSink * sink, GstCaps * caps);
@@ -359,16 +394,21 @@ static gboolean gst_base_sink_pad_activate (GstPad * pad);
 static gboolean gst_base_sink_pad_activate_push (GstPad * pad, gboolean active);
 static gboolean gst_base_sink_pad_activate_pull (GstPad * pad, gboolean active);
 static gboolean gst_base_sink_event (GstPad * pad, GstEvent * event);
-static gboolean gst_base_sink_peer_query (GstBaseSink * sink, GstQuery * query);
 
 static gboolean gst_base_sink_negotiate_pull (GstBaseSink * basesink);
+static GstCaps *gst_base_sink_pad_getcaps (GstPad * pad);
+static gboolean gst_base_sink_pad_setcaps (GstPad * pad, GstCaps * caps);
+static void gst_base_sink_pad_fixate (GstPad * pad, GstCaps * caps);
+static GstFlowReturn gst_base_sink_pad_buffer_alloc (GstPad * pad,
+    guint64 offset, guint size, GstCaps * caps, GstBuffer ** buf);
+
 
 /* check if an object was too late */
 static gboolean gst_base_sink_is_too_late (GstBaseSink * basesink,
-    GstMiniObject * obj, GstClockTime start, GstClockTime stop,
+    GstMiniObject * obj, GstClockTime rstart, GstClockTime rstop,
     GstClockReturn status, GstClockTimeDiff jitter);
 static GstFlowReturn gst_base_sink_preroll_object (GstBaseSink * basesink,
-    gboolean is_list, GstMiniObject * obj);
+    guint8 obj_type, GstMiniObject * obj);
 
 static void
 gst_base_sink_class_init (GstBaseSinkClass * klass)
@@ -386,9 +426,9 @@ gst_base_sink_class_init (GstBaseSinkClass * klass)
 
   parent_class = g_type_class_peek_parent (klass);
 
-  gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_base_sink_finalize);
-  gobject_class->set_property = GST_DEBUG_FUNCPTR (gst_base_sink_set_property);
-  gobject_class->get_property = GST_DEBUG_FUNCPTR (gst_base_sink_get_property);
+  gobject_class->finalize = gst_base_sink_finalize;
+  gobject_class->set_property = gst_base_sink_set_property;
+  gobject_class->get_property = gst_base_sink_get_property;
 
   /* FIXME, this next value should be configured using an event from the
    * upstream element, ie, the BUFFER_SIZE event. */
@@ -439,6 +479,22 @@ gst_base_sink_class_init (GstBaseSinkClass * klass)
       g_param_spec_int64 ("ts-offset", "TS Offset",
           "Timestamp offset in nanoseconds", G_MININT64, G_MAXINT64,
           DEFAULT_TS_OFFSET, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
+
+  /**
+   * GstBaseSink:enable-last-buffer
+   *
+   * Enable the last-buffer property. If FALSE, basesink doesn't keep a
+   * reference to the last buffer arrived and the last-buffer property is always
+   * set to NULL. This can be useful if you need buffers to be released as soon
+   * as possible, eg. if you're using a buffer pool.
+   *
+   * Since: 0.10.30
+   */
+  g_object_class_install_property (gobject_class, PROP_ENABLE_LAST_BUFFER,
+      g_param_spec_boolean ("enable-last-buffer", "Enable Last Buffer",
+          "Enable the last-buffer property", DEFAULT_ENABLE_LAST_BUFFER,
+          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
+
   /**
    * GstBaseSink:last-buffer
    *
@@ -449,7 +505,7 @@ gst_base_sink_class_init (GstBaseSinkClass * klass)
    * Since: 0.10.15
    */
   g_object_class_install_property (gobject_class, PROP_LAST_BUFFER,
-      gst_param_spec_mini_object ("last-buffer", "Last Buffer",
+      g_param_spec_boxed ("last-buffer", "Last Buffer",
           "The last buffer received in the sink", GST_TYPE_BUFFER,
           G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
   /**
@@ -459,6 +515,7 @@ gst_base_sink_class_init (GstBaseSinkClass * klass)
    *
    * Since: 0.10.22
    */
+  /* FIXME 0.11: blocksize property should be int, otherwise min>max.. */
   g_object_class_install_property (gobject_class, PROP_BLOCKSIZE,
       g_param_spec_uint ("blocksize", "Block size",
           "Size in bytes to pull per buffer (0 = default)", 0, G_MAXUINT,
@@ -476,11 +533,26 @@ gst_base_sink_class_init (GstBaseSinkClass * klass)
       g_param_spec_uint64 ("render-delay", "Render Delay",
           "Additional render delay of the sink in nanoseconds", 0, G_MAXUINT64,
           DEFAULT_RENDER_DELAY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
+  /**
+   * GstBaseSink:throttle-time
+   *
+   * The time to insert between buffers. This property can be used to control
+   * the maximum amount of buffers per second to render. Setting this property
+   * to a value bigger than 0 will make the sink create THROTTLE QoS events.
+   *
+   * Since: 0.10.33
+   */
+  g_object_class_install_property (gobject_class, PROP_THROTTLE_TIME,
+      g_param_spec_uint64 ("throttle-time", "Throttle time",
+          "The time to keep between rendered buffers (unused)", 0, G_MAXUINT64,
+          DEFAULT_THROTTLE_TIME, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
 
   gstelement_class->change_state =
       GST_DEBUG_FUNCPTR (gst_base_sink_change_state);
   gstelement_class->send_event = GST_DEBUG_FUNCPTR (gst_base_sink_send_event);
   gstelement_class->query = GST_DEBUG_FUNCPTR (gst_base_sink_query);
+  gstelement_class->get_query_types =
+      GST_DEBUG_FUNCPTR (gst_base_sink_get_query_types);
 
   klass->get_caps = GST_DEBUG_FUNCPTR (gst_base_sink_get_caps);
   klass->set_caps = GST_DEBUG_FUNCPTR (gst_base_sink_set_caps);
@@ -488,6 +560,18 @@ gst_base_sink_class_init (GstBaseSinkClass * klass)
   klass->get_times = GST_DEBUG_FUNCPTR (gst_base_sink_get_times);
   klass->activate_pull =
       GST_DEBUG_FUNCPTR (gst_base_sink_default_activate_pull);
+
+  /* Registering debug symbols for function pointers */
+  GST_DEBUG_REGISTER_FUNCPTR (gst_base_sink_pad_getcaps);
+  GST_DEBUG_REGISTER_FUNCPTR (gst_base_sink_pad_setcaps);
+  GST_DEBUG_REGISTER_FUNCPTR (gst_base_sink_pad_fixate);
+  GST_DEBUG_REGISTER_FUNCPTR (gst_base_sink_pad_buffer_alloc);
+  GST_DEBUG_REGISTER_FUNCPTR (gst_base_sink_pad_activate);
+  GST_DEBUG_REGISTER_FUNCPTR (gst_base_sink_pad_activate_push);
+  GST_DEBUG_REGISTER_FUNCPTR (gst_base_sink_pad_activate_pull);
+  GST_DEBUG_REGISTER_FUNCPTR (gst_base_sink_event);
+  GST_DEBUG_REGISTER_FUNCPTR (gst_base_sink_chain);
+  GST_DEBUG_REGISTER_FUNCPTR (gst_base_sink_chain_list);
 }
 
 static GstCaps *
@@ -569,6 +653,8 @@ gst_base_sink_pad_buffer_alloc (GstPad * pad, guint64 offset, guint size,
   GstFlowReturn result = GST_FLOW_OK;
 
   bsink = GST_BASE_SINK (gst_pad_get_parent (pad));
+  if (G_UNLIKELY (bsink == NULL))
+    return GST_FLOW_WRONG_STATE;
   bclass = GST_BASE_SINK_GET_CLASS (bsink);
 
   if (bclass->buffer_alloc)
@@ -595,43 +681,41 @@ gst_base_sink_init (GstBaseSink * basesink, gpointer g_class)
 
   basesink->sinkpad = gst_pad_new_from_template (pad_template, "sink");
 
-  gst_pad_set_getcaps_function (basesink->sinkpad,
-      GST_DEBUG_FUNCPTR (gst_base_sink_pad_getcaps));
-  gst_pad_set_setcaps_function (basesink->sinkpad,
-      GST_DEBUG_FUNCPTR (gst_base_sink_pad_setcaps));
-  gst_pad_set_fixatecaps_function (basesink->sinkpad,
-      GST_DEBUG_FUNCPTR (gst_base_sink_pad_fixate));
+  gst_pad_set_getcaps_function (basesink->sinkpad, gst_base_sink_pad_getcaps);
+  gst_pad_set_setcaps_function (basesink->sinkpad, gst_base_sink_pad_setcaps);
+  gst_pad_set_fixatecaps_function (basesink->sinkpad, gst_base_sink_pad_fixate);
   gst_pad_set_bufferalloc_function (basesink->sinkpad,
-      GST_DEBUG_FUNCPTR (gst_base_sink_pad_buffer_alloc));
-  gst_pad_set_activate_function (basesink->sinkpad,
-      GST_DEBUG_FUNCPTR (gst_base_sink_pad_activate));
+      gst_base_sink_pad_buffer_alloc);
+  gst_pad_set_activate_function (basesink->sinkpad, gst_base_sink_pad_activate);
   gst_pad_set_activatepush_function (basesink->sinkpad,
-      GST_DEBUG_FUNCPTR (gst_base_sink_pad_activate_push));
+      gst_base_sink_pad_activate_push);
   gst_pad_set_activatepull_function (basesink->sinkpad,
-      GST_DEBUG_FUNCPTR (gst_base_sink_pad_activate_pull));
-  gst_pad_set_event_function (basesink->sinkpad,
-      GST_DEBUG_FUNCPTR (gst_base_sink_event));
-  gst_pad_set_chain_function (basesink->sinkpad,
-      GST_DEBUG_FUNCPTR (gst_base_sink_chain));
-  gst_pad_set_chain_list_function (basesink->sinkpad,
-      GST_DEBUG_FUNCPTR (gst_base_sink_chain_list));
+      gst_base_sink_pad_activate_pull);
+  gst_pad_set_event_function (basesink->sinkpad, gst_base_sink_event);
+  gst_pad_set_chain_function (basesink->sinkpad, gst_base_sink_chain);
+  gst_pad_set_chain_list_function (basesink->sinkpad, gst_base_sink_chain_list);
   gst_element_add_pad (GST_ELEMENT_CAST (basesink), basesink->sinkpad);
 
   basesink->pad_mode = GST_ACTIVATE_NONE;
+  basesink->preroll_lock = g_mutex_new ();
+  basesink->preroll_cond = g_cond_new ();
   basesink->preroll_queue = g_queue_new ();
-  basesink->abidata.ABI.clip_segment = gst_segment_new ();
+  basesink->clip_segment = gst_segment_new ();
   priv->have_latency = FALSE;
 
   basesink->can_activate_push = DEFAULT_CAN_ACTIVATE_PUSH;
   basesink->can_activate_pull = DEFAULT_CAN_ACTIVATE_PULL;
 
   basesink->sync = DEFAULT_SYNC;
-  basesink->abidata.ABI.max_lateness = DEFAULT_MAX_LATENESS;
+  basesink->max_lateness = DEFAULT_MAX_LATENESS;
   g_atomic_int_set (&priv->qos_enabled, DEFAULT_QOS);
   priv->async_enabled = DEFAULT_ASYNC;
   priv->ts_offset = DEFAULT_TS_OFFSET;
   priv->render_delay = DEFAULT_RENDER_DELAY;
   priv->blocksize = DEFAULT_BLOCKSIZE;
+  priv->cached_clock_id = NULL;
+  g_atomic_int_set (&priv->enable_last_buffer, DEFAULT_ENABLE_LAST_BUFFER);
+  priv->throttle_time = DEFAULT_THROTTLE_TIME;
 
   GST_OBJECT_FLAG_SET (basesink, GST_ELEMENT_IS_SINK);
 }
@@ -643,8 +727,10 @@ gst_base_sink_finalize (GObject * object)
 
   basesink = GST_BASE_SINK (object);
 
+  g_mutex_free (basesink->preroll_lock);
+  g_cond_free (basesink->preroll_cond);
   g_queue_free (basesink->preroll_queue);
-  gst_segment_free (basesink->abidata.ABI.clip_segment);
+  gst_segment_free (basesink->clip_segment);
 
   G_OBJECT_CLASS (parent_class)->finalize (object);
 }
@@ -715,7 +801,7 @@ gst_base_sink_set_max_lateness (GstBaseSink * sink, gint64 max_lateness)
   g_return_if_fail (GST_IS_BASE_SINK (sink));
 
   GST_OBJECT_LOCK (sink);
-  sink->abidata.ABI.max_lateness = max_lateness;
+  sink->max_lateness = max_lateness;
   GST_OBJECT_UNLOCK (sink);
 }
 
@@ -740,7 +826,7 @@ gst_base_sink_get_max_lateness (GstBaseSink * sink)
   g_return_val_if_fail (GST_IS_BASE_SINK (sink), -1);
 
   GST_OBJECT_LOCK (sink);
-  res = sink->abidata.ABI.max_lateness;
+  res = sink->max_lateness;
   GST_OBJECT_UNLOCK (sink);
 
   return res;
@@ -803,10 +889,10 @@ gst_base_sink_set_async_enabled (GstBaseSink * sink, gboolean enabled)
 {
   g_return_if_fail (GST_IS_BASE_SINK (sink));
 
-  GST_PAD_PREROLL_LOCK (sink->sinkpad);
-  sink->priv->async_enabled = enabled;
+  GST_BASE_SINK_PREROLL_LOCK (sink);
+  g_atomic_int_set (&sink->priv->async_enabled, enabled);
   GST_LOG_OBJECT (sink, "set async enabled to %d", enabled);
-  GST_PAD_PREROLL_UNLOCK (sink->sinkpad);
+  GST_BASE_SINK_PREROLL_UNLOCK (sink);
 }
 
 /**
@@ -828,9 +914,7 @@ gst_base_sink_is_async_enabled (GstBaseSink * sink)
 
   g_return_val_if_fail (GST_IS_BASE_SINK (sink), FALSE);
 
-  GST_PAD_PREROLL_LOCK (sink->sinkpad);
-  res = sink->priv->async_enabled;
-  GST_PAD_PREROLL_UNLOCK (sink->sinkpad);
+  res = g_atomic_int_get (&sink->priv->async_enabled);
 
   return res;
 }
@@ -891,9 +975,11 @@ gst_base_sink_get_ts_offset (GstBaseSink * sink)
  *
  * The #GstCaps on the buffer can be used to determine the type of the buffer.
  *
- * Returns: a #GstBuffer. gst_buffer_unref() after usage. This function returns
- * NULL when no buffer has arrived in the sink yet or when the sink is not in
- * PAUSED or PLAYING.
+ * Free-function: gst_buffer_unref
+ *
+ * Returns: (transfer full): a #GstBuffer. gst_buffer_unref() after usage.
+ *     This function returns NULL when no buffer has arrived in the sink yet
+ *     or when the sink is not in PAUSED or PLAYING.
  *
  * Since: 0.10.15
  */
@@ -912,12 +998,12 @@ gst_base_sink_get_last_buffer (GstBaseSink * sink)
   return res;
 }
 
+/* with OBJECT_LOCK */
 static void
-gst_base_sink_set_last_buffer (GstBaseSink * sink, GstBuffer * buffer)
+gst_base_sink_set_last_buffer_unlocked (GstBaseSink * sink, GstBuffer * buffer)
 {
   GstBuffer *old;
 
-  GST_OBJECT_LOCK (sink);
   old = sink->priv->last_buffer;
   if (G_LIKELY (old != buffer)) {
     GST_DEBUG_OBJECT (sink, "setting last buffer to %p", buffer);
@@ -927,12 +1013,67 @@ gst_base_sink_set_last_buffer (GstBaseSink * sink, GstBuffer * buffer)
   } else {
     old = NULL;
   }
-  GST_OBJECT_UNLOCK (sink);
-
   /* avoid unreffing with the lock because cleanup code might want to take the
    * lock too */
-  if (G_LIKELY (old))
+  if (G_LIKELY (old)) {
+    GST_OBJECT_UNLOCK (sink);
     gst_buffer_unref (old);
+    GST_OBJECT_LOCK (sink);
+  }
+}
+
+static void
+gst_base_sink_set_last_buffer (GstBaseSink * sink, GstBuffer * buffer)
+{
+  if (!g_atomic_int_get (&sink->priv->enable_last_buffer))
+    return;
+
+  GST_OBJECT_LOCK (sink);
+  gst_base_sink_set_last_buffer_unlocked (sink, buffer);
+  GST_OBJECT_UNLOCK (sink);
+}
+
+/**
+ * gst_base_sink_set_last_buffer_enabled:
+ * @sink: the sink
+ * @enabled: the new enable-last-buffer value.
+ *
+ * Configures @sink to store the last received buffer in the last-buffer
+ * property.
+ *
+ * Since: 0.10.30
+ */
+void
+gst_base_sink_set_last_buffer_enabled (GstBaseSink * sink, gboolean enabled)
+{
+  g_return_if_fail (GST_IS_BASE_SINK (sink));
+
+  /* Only take lock if we change the value */
+  if (g_atomic_int_compare_and_exchange (&sink->priv->enable_last_buffer,
+          !enabled, enabled) && !enabled) {
+    GST_OBJECT_LOCK (sink);
+    gst_base_sink_set_last_buffer_unlocked (sink, NULL);
+    GST_OBJECT_UNLOCK (sink);
+  }
+}
+
+/**
+ * gst_base_sink_is_last_buffer_enabled:
+ * @sink: the sink
+ *
+ * Checks if @sink is currently configured to store the last received buffer in
+ * the last-buffer property.
+ *
+ * Returns: TRUE if the sink is configured to store the last received buffer.
+ *
+ * Since: 0.10.30
+ */
+gboolean
+gst_base_sink_is_last_buffer_enabled (GstBaseSink * sink)
+{
+  g_return_val_if_fail (GST_IS_BASE_SINK (sink), FALSE);
+
+  return g_atomic_int_get (&sink->priv->enable_last_buffer);
 }
 
 /**
@@ -960,10 +1101,10 @@ gst_base_sink_get_latency (GstBaseSink * sink)
 /**
  * gst_base_sink_query_latency:
  * @sink: the sink
- * @live: if the sink is live
- * @upstream_live: if an upstream element is live
- * @min_latency: the min latency of the upstream elements
- * @max_latency: the max latency of the upstream elements
+ * @live: (out) (allow-none): if the sink is live
+ * @upstream_live: (out) (allow-none): if an upstream element is live
+ * @min_latency: (out) (allow-none): the min latency of the upstream elements
+ * @max_latency: (out) (allow-none): the max latency of the upstream elements
  *
  * Query the sink for the latency parameters. The latency will be queried from
  * the upstream elements. @live will be TRUE if @sink is configured to
@@ -1009,7 +1150,7 @@ gst_base_sink_query_latency (GstBaseSink * sink, gboolean * live,
     query = gst_query_new_latency ();
 
     /* ask the peer for the latency */
-    if ((res = gst_base_sink_peer_query (sink, query))) {
+    if ((res = gst_pad_peer_query (sink->sinkpad, query))) {
       /* get upstream min and max latency */
       gst_query_parse_latency (query, &us_live, &us_min, &us_max);
 
@@ -1133,6 +1274,7 @@ gst_base_sink_get_render_delay (GstBaseSink * sink)
  *
  * Since: 0.10.22
  */
+/* FIXME 0.11: blocksize property should be int, otherwise min>max.. */
 void
 gst_base_sink_set_blocksize (GstBaseSink * sink, guint blocksize)
 {
@@ -1155,6 +1297,7 @@ gst_base_sink_set_blocksize (GstBaseSink * sink, guint blocksize)
  *
  * Since: 0.10.22
  */
+/* FIXME 0.11: blocksize property should be int, otherwise min>max.. */
 guint
 gst_base_sink_get_blocksize (GstBaseSink * sink)
 {
@@ -1169,6 +1312,53 @@ gst_base_sink_get_blocksize (GstBaseSink * sink)
   return res;
 }
 
+/**
+ * gst_base_sink_set_throttle_time:
+ * @sink: a #GstBaseSink
+ * @throttle: the throttle time in nanoseconds
+ *
+ * Set the time that will be inserted between rendered buffers. This
+ * can be used to control the maximum buffers per second that the sink
+ * will render. 
+ *
+ * Since: 0.10.33
+ */
+void
+gst_base_sink_set_throttle_time (GstBaseSink * sink, guint64 throttle)
+{
+  g_return_if_fail (GST_IS_BASE_SINK (sink));
+
+  GST_OBJECT_LOCK (sink);
+  sink->priv->throttle_time = throttle;
+  GST_LOG_OBJECT (sink, "set throttle_time to %" G_GUINT64_FORMAT, throttle);
+  GST_OBJECT_UNLOCK (sink);
+}
+
+/**
+ * gst_base_sink_get_throttle_time:
+ * @sink: a #GstBaseSink
+ *
+ * Get the time that will be inserted between frames to control the 
+ * maximum buffers per second.
+ *
+ * Returns: the number of nanoseconds @sink will put between frames.
+ *
+ * Since: 0.10.33
+ */
+guint64
+gst_base_sink_get_throttle_time (GstBaseSink * sink)
+{
+  guint64 res;
+
+  g_return_val_if_fail (GST_IS_BASE_SINK (sink), 0);
+
+  GST_OBJECT_LOCK (sink);
+  res = sink->priv->throttle_time;
+  GST_OBJECT_UNLOCK (sink);
+
+  return res;
+}
+
 static void
 gst_base_sink_set_property (GObject * object, guint prop_id,
     const GValue * value, GParamSpec * pspec)
@@ -1178,9 +1368,9 @@ gst_base_sink_set_property (GObject * object, guint prop_id,
   switch (prop_id) {
     case PROP_PREROLL_QUEUE_LEN:
       /* preroll lock necessary to serialize with finish_preroll */
-      GST_PAD_PREROLL_LOCK (sink->sinkpad);
-      sink->preroll_queue_max_len = g_value_get_uint (value);
-      GST_PAD_PREROLL_UNLOCK (sink->sinkpad);
+      GST_BASE_SINK_PREROLL_LOCK (sink);
+      g_atomic_int_set (&sink->preroll_queue_max_len, g_value_get_uint (value));
+      GST_BASE_SINK_PREROLL_UNLOCK (sink);
       break;
     case PROP_SYNC:
       gst_base_sink_set_sync (sink, g_value_get_boolean (value));
@@ -1203,6 +1393,12 @@ gst_base_sink_set_property (GObject * object, guint prop_id,
     case PROP_RENDER_DELAY:
       gst_base_sink_set_render_delay (sink, g_value_get_uint64 (value));
       break;
+    case PROP_ENABLE_LAST_BUFFER:
+      gst_base_sink_set_last_buffer_enabled (sink, g_value_get_boolean (value));
+      break;
+    case PROP_THROTTLE_TIME:
+      gst_base_sink_set_throttle_time (sink, g_value_get_uint64 (value));
+      break;
     default:
       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
       break;
@@ -1217,9 +1413,7 @@ gst_base_sink_get_property (GObject * object, guint prop_id, GValue * value,
 
   switch (prop_id) {
     case PROP_PREROLL_QUEUE_LEN:
-      GST_PAD_PREROLL_LOCK (sink->sinkpad);
-      g_value_set_uint (value, sink->preroll_queue_max_len);
-      GST_PAD_PREROLL_UNLOCK (sink->sinkpad);
+      g_value_set_uint (value, g_atomic_int_get (&sink->preroll_queue_max_len));
       break;
     case PROP_SYNC:
       g_value_set_boolean (value, gst_base_sink_get_sync (sink));
@@ -1239,12 +1433,18 @@ gst_base_sink_get_property (GObject * object, guint prop_id, GValue * value,
     case PROP_LAST_BUFFER:
       gst_value_take_buffer (value, gst_base_sink_get_last_buffer (sink));
       break;
+    case PROP_ENABLE_LAST_BUFFER:
+      g_value_set_boolean (value, gst_base_sink_is_last_buffer_enabled (sink));
+      break;
     case PROP_BLOCKSIZE:
       g_value_set_uint (value, gst_base_sink_get_blocksize (sink));
       break;
     case PROP_RENDER_DELAY:
       g_value_set_uint64 (value, gst_base_sink_get_render_delay (sink));
       break;
+    case PROP_THROTTLE_TIME:
+      g_value_set_uint64 (value, gst_base_sink_get_throttle_time (sink));
+      break;
     default:
       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
       break;
@@ -1299,7 +1499,7 @@ gst_base_sink_preroll_queue_flush (GstBaseSink * basesink, GstPad * pad)
     GST_OBJECT_UNLOCK (basesink);
   }
   /* and signal any waiters now */
-  GST_PAD_PREROLL_SIGNAL (pad);
+  GST_BASE_SINK_PREROLL_SIGNAL (basesink);
 }
 
 /* with STREAM_LOCK, configures given segment with the event information. */
@@ -1370,7 +1570,6 @@ gst_base_sink_commit_state (GstBaseSink * basesink)
     case GST_STATE_PLAYING:
     {
       GstBaseSinkClass *bclass;
-      GstStateChangeReturn ret;
 
       bclass = GST_BASE_SINK_GET_CLASS (basesink);
 
@@ -1384,14 +1583,6 @@ gst_base_sink_commit_state (GstBaseSink * basesink)
       if (current == GST_STATE_READY) {
         post_paused = TRUE;
       }
-
-      /* make sure we notify the subclass of async playing */
-      if (bclass->async_play) {
-        GST_WARNING_OBJECT (basesink, "deprecated async_play");
-        ret = bclass->async_play (basesink);
-        if (ret == GST_STATE_CHANGE_FAILURE)
-          goto async_failed;
-      }
       break;
     }
     case GST_STATE_PAUSED:
@@ -1476,13 +1667,6 @@ stopping:
     GST_OBJECT_UNLOCK (basesink);
     return FALSE;
   }
-async_failed:
-  {
-    GST_DEBUG_OBJECT (basesink, "async commit failed");
-    GST_STATE_RETURN (basesink) = GST_STATE_CHANGE_FAILURE;
-    GST_OBJECT_UNLOCK (basesink);
-    return FALSE;
-  }
 }
 
 static void
@@ -1609,7 +1793,7 @@ stop_stepping (GstBaseSink * sink, GstSegment * segment,
     segment->start = current->start_start;
 
   /* the clip segment is used for position report in paused... */
-  memcpy (sink->abidata.ABI.clip_segment, segment, sizeof (GstSegment));
+  memcpy (sink->clip_segment, segment, sizeof (GstSegment));
 
   /* post the step done when we know the stepped duration in TIME */
   message =
@@ -1641,9 +1825,15 @@ handle_stepping (GstBaseSink * sink, GstSegment * segment,
       gint64 first, last;
 
       if (segment->rate > 0.0) {
+        if (segment->stop == *cstop)
+          *rstop = *rstart + current->amount;
+
         first = *rstart;
         last = *rstop;
       } else {
+        if (segment->start == *cstart)
+          *rstart = *rstop + current->amount;
+
         first = *rstop;
         last = *rstart;
       }
@@ -1717,7 +1907,7 @@ gst_base_sink_get_sync_times (GstBaseSink * basesink, GstMiniObject * obj,
     GstClockTime * rsstart, GstClockTime * rsstop,
     GstClockTime * rrstart, GstClockTime * rrstop, gboolean * do_sync,
     gboolean * stepped, GstSegment * segment, GstStepInfo * step,
-    gboolean * step_end)
+    gboolean * step_end, guint8 obj_type)
 {
   GstBaseSinkClass *bclass;
   GstBuffer *buffer;
@@ -1734,7 +1924,7 @@ gst_base_sink_get_sync_times (GstBaseSink * basesink, GstMiniObject * obj,
   /* start with nothing */
   start = stop = GST_CLOCK_TIME_NONE;
 
-  if (G_UNLIKELY (GST_IS_EVENT (obj))) {
+  if (G_UNLIKELY (OBJ_IS_EVENT (obj_type))) {
     GstEvent *event = GST_EVENT_CAST (obj);
 
     switch (GST_EVENT_TYPE (event)) {
@@ -1779,6 +1969,7 @@ gst_base_sink_get_sync_times (GstBaseSink * basesink, GstMiniObject * obj,
 
   eos = FALSE;
 
+again:
   /* else do buffer sync code */
   buffer = GST_BUFFER_CAST (obj);
 
@@ -1840,7 +2031,7 @@ gst_base_sink_get_sync_times (GstBaseSink * basesink, GstMiniObject * obj,
   }
 
   /* set last stop position */
-  if (G_LIKELY (cstop != GST_CLOCK_TIME_NONE))
+  if (G_LIKELY (stop != GST_CLOCK_TIME_NONE && cstop != GST_CLOCK_TIME_NONE))
     gst_segment_set_last_stop (segment, GST_FORMAT_TIME, cstop);
   else
     gst_segment_set_last_stop (segment, GST_FORMAT_TIME, cstart);
@@ -1854,6 +2045,7 @@ do_times:
                 &rstart, &rstop))) {
       /* step is still busy, we discard data when we are flushing */
       *stepped = step->flush;
+      GST_DEBUG_OBJECT (basesink, "stepping busy");
     }
   }
   /* this can produce wrong values if we accumulated non-TIME segments. If this happens,
@@ -1867,6 +2059,12 @@ eos_done:
     GST_DEBUG_OBJECT (basesink, "flushing step ended");
     stop_stepping (basesink, segment, step, rstart, rstop, eos);
     *step_end = FALSE;
+    /* re-determine running start times for adjusted segment
+     * (which has a flushed amount of running/accumulated time removed) */
+    if (!GST_IS_EVENT (obj)) {
+      GST_DEBUG_OBJECT (basesink, "refresh sync times");
+      goto again;
+    }
   }
 
   /* save times */
@@ -1890,7 +2088,8 @@ out_of_segment:
 }
 
 /* with STREAM_LOCK, PREROLL_LOCK, LOCK
- * adjust a timestamp with the latency and timestamp offset */
+ * adjust a timestamp with the latency and timestamp offset. This function does
+ * not adjust for the render delay. */
 static GstClockTime
 gst_base_sink_adjust_time (GstBaseSink * basesink, GstClockTime time)
 {
@@ -1913,6 +2112,12 @@ gst_base_sink_adjust_time (GstBaseSink * basesink, GstClockTime time)
   } else
     time += ts_offset;
 
+  /* subtract the render delay again, which was included in the latency */
+  if (time > basesink->priv->render_delay)
+    time -= basesink->priv->render_delay;
+  else
+    time = 0;
+
   return time;
 }
 
@@ -1920,7 +2125,7 @@ gst_base_sink_adjust_time (GstBaseSink * basesink, GstClockTime time)
  * gst_base_sink_wait_clock:
  * @sink: the sink
  * @time: the running_time to be reached
- * @jitter: the jitter to be filled with time diff (can be NULL)
+ * @jitter: (out) (allow-none): the jitter to be filled with time diff, or NULL
  *
  * This function will block until @time is reached. It is usually called by
  * subclasses that use their own internal synchronisation.
@@ -1930,14 +2135,15 @@ gst_base_sink_adjust_time (GstBaseSink * basesink, GstClockTime time)
  * is no clock, no synchronisation is done and #GST_CLOCK_BADTIME is returned.
  *
  * This function should only be called with the PREROLL_LOCK held, like when
- * receiving an EOS event in the ::event vmethod or when receiving a buffer in
- * the ::render vmethod.
+ * receiving an EOS event in the #GstBaseSinkClass.event() vmethod or when
+ * receiving a buffer in
+ * the #GstBaseSinkClass.render() vmethod.
  *
  * The @time argument should be the running_time of when this method should
  * return and is not adjusted with any latency or offset configured in the
  * sink.
  *
- * Since 0.10.20
+ * Since: 0.10.20
  *
  * Returns: #GstClockReturn
  */
@@ -1945,9 +2151,9 @@ GstClockReturn
 gst_base_sink_wait_clock (GstBaseSink * sink, GstClockTime time,
     GstClockTimeDiff * jitter)
 {
-  GstClockID id;
   GstClockReturn ret;
   GstClock *clock;
+  GstClockTime base_time;
 
   if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (time)))
     goto invalid_time;
@@ -1959,24 +2165,36 @@ gst_base_sink_wait_clock (GstBaseSink * sink, GstClockTime time,
   if (G_UNLIKELY ((clock = GST_ELEMENT_CLOCK (sink)) == NULL))
     goto no_clock;
 
-  /* add base_time to running_time to get the time against the clock */
-  time += GST_ELEMENT_CAST (sink)->base_time;
+  base_time = GST_ELEMENT_CAST (sink)->base_time;
+  GST_LOG_OBJECT (sink,
+      "time %" GST_TIME_FORMAT ", base_time %" GST_TIME_FORMAT,
+      GST_TIME_ARGS (time), GST_TIME_ARGS (base_time));
 
-  id = gst_clock_new_single_shot_id (clock, time);
+  /* add base_time to running_time to get the time against the clock */
+  time += base_time;
+
+  /* Re-use existing clockid if available */
+  if (G_LIKELY (sink->priv->cached_clock_id != NULL)) {
+    if (!gst_clock_single_shot_id_reinit (clock, sink->priv->cached_clock_id,
+            time)) {
+      gst_clock_id_unref (sink->priv->cached_clock_id);
+      sink->priv->cached_clock_id = gst_clock_new_single_shot_id (clock, time);
+    }
+  } else
+    sink->priv->cached_clock_id = gst_clock_new_single_shot_id (clock, time);
   GST_OBJECT_UNLOCK (sink);
 
   /* A blocking wait is performed on the clock. We save the ClockID
    * so we can unlock the entry at any time. While we are blocking, we
    * release the PREROLL_LOCK so that other threads can interrupt the
    * entry. */
-  sink->clock_id = id;
+  sink->clock_id = sink->priv->cached_clock_id;
   /* release the preroll lock while waiting */
-  GST_PAD_PREROLL_UNLOCK (sink->sinkpad);
+  GST_BASE_SINK_PREROLL_UNLOCK (sink);
 
-  ret = gst_clock_id_wait (id, jitter);
+  ret = gst_clock_id_wait (sink->priv->cached_clock_id, jitter);
 
-  GST_PAD_PREROLL_LOCK (sink->sinkpad);
-  gst_clock_id_unref (id);
+  GST_BASE_SINK_PREROLL_LOCK (sink);
   sink->clock_id = NULL;
 
   return ret;
@@ -2005,9 +2223,9 @@ no_clock:
  * gst_base_sink_wait_preroll:
  * @sink: the sink
  *
- * If the #GstBaseSinkClass::render 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 render the remaining data.
+ * If the #GstBaseSinkClass.render() 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 render 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
@@ -2017,10 +2235,10 @@ no_clock:
  * This function should only be called with the PREROLL_LOCK held, like in the
  * render function.
  *
- * Since: 0.10.11
- *
  * Returns: #GST_FLOW_OK if the preroll completed and processing can
  * continue. Any other return value should be returned from the render vmethod.
+ *
+ * Since: 0.10.11
  */
 GstFlowReturn
 gst_base_sink_wait_preroll (GstBaseSink * sink)
@@ -2028,7 +2246,7 @@ gst_base_sink_wait_preroll (GstBaseSink * sink)
   sink->have_preroll = TRUE;
   GST_DEBUG_OBJECT (sink, "waiting in preroll for flush or PLAYING");
   /* block until the state changes, or we get a flush, or something */
-  GST_PAD_PREROLL_WAIT (sink->sinkpad);
+  GST_BASE_SINK_PREROLL_WAIT (sink);
   sink->have_preroll = FALSE;
   if (G_UNLIKELY (sink->flushing))
     goto stopping;
@@ -2052,10 +2270,27 @@ step_unlocked:
   }
 }
 
+static inline guint8
+get_object_type (GstMiniObject * obj)
+{
+  guint8 obj_type;
+
+  if (G_LIKELY (GST_IS_BUFFER (obj)))
+    obj_type = _PR_IS_BUFFER;
+  else if (GST_IS_EVENT (obj))
+    obj_type = _PR_IS_EVENT;
+  else if (GST_IS_BUFFER_LIST (obj))
+    obj_type = _PR_IS_BUFFERLIST;
+  else
+    obj_type = _PR_IS_NOTHING;
+
+  return obj_type;
+}
+
 /**
  * gst_base_sink_do_preroll:
  * @sink: the sink
- * @obj: the object that caused the preroll
+ * @obj: (transfer none): the mini object that caused the preroll
  *
  * If the @sink spawns its own thread for pulling buffers from upstream it
  * should call this method after it has pulled a buffer. If the element needed
@@ -2064,10 +2299,10 @@ step_unlocked:
  *
  * This function should be called with the PREROLL_LOCK held.
  *
- * Since 0.10.22
- *
  * Returns: #GST_FLOW_OK if the preroll completed and processing can
  * continue. Any other return value should be returned from the render vmethod.
+ *
+ * Since: 0.10.22
  */
 GstFlowReturn
 gst_base_sink_do_preroll (GstBaseSink * sink, GstMiniObject * obj)
@@ -2075,9 +2310,12 @@ gst_base_sink_do_preroll (GstBaseSink * sink, GstMiniObject * obj)
   GstFlowReturn ret;
 
   while (G_UNLIKELY (sink->need_preroll)) {
+    guint8 obj_type;
     GST_DEBUG_OBJECT (sink, "prerolling object %p", obj);
 
-    ret = gst_base_sink_preroll_object (sink, FALSE, obj);
+    obj_type = get_object_type (obj);
+
+    ret = gst_base_sink_preroll_object (sink, obj_type, obj);
     if (ret != GST_FLOW_OK)
       goto preroll_failed;
 
@@ -2095,7 +2333,7 @@ gst_base_sink_do_preroll (GstBaseSink * sink, GstMiniObject * obj)
   /* ERRORS */
 preroll_failed:
   {
-    GST_DEBUG_OBJECT (sink, "preroll failed %d", ret);
+    GST_DEBUG_OBJECT (sink, "preroll failed: %s", gst_flow_get_name (ret));
     return ret;
   }
 }
@@ -2104,7 +2342,7 @@ preroll_failed:
  * gst_base_sink_wait_eos:
  * @sink: the sink
  * @time: the running_time to be reached
- * @jitter: the jitter to be filled with time diff (can be NULL)
+ * @jitter: (out) (allow-none): the jitter to be filled with time diff, or NULL
  *
  * This function will block until @time is reached. It is usually called by
  * subclasses that use their own internal synchronisation but want to let the
@@ -2116,9 +2354,9 @@ preroll_failed:
  * The @time argument should be the running_time of when the EOS should happen
  * and will be adjusted with any latency and offset configured in the sink.
  *
- * Since 0.10.15
- *
  * Returns: #GstFlowReturn
+ *
+ * Since: 0.10.15
  */
 GstFlowReturn
 gst_base_sink_wait_eos (GstBaseSink * sink, GstClockTime time,
@@ -2133,7 +2371,7 @@ gst_base_sink_wait_eos (GstBaseSink * sink, GstClockTime time,
     GST_DEBUG_OBJECT (sink, "checking preroll");
 
     /* first wait for the playing state before we can continue */
-    if (G_UNLIKELY (sink->need_preroll)) {
+    while (G_UNLIKELY (sink->need_preroll)) {
       ret = gst_base_sink_wait_preroll (sink);
       if ((ret != GST_FLOW_OK) && (ret != GST_FLOW_STEP))
         goto flushing;
@@ -2201,9 +2439,9 @@ flushing:
  */
 static GstFlowReturn
 gst_base_sink_do_sync (GstBaseSink * basesink, GstPad * pad,
-    GstMiniObject * obj, gboolean * late, gboolean * step_end)
+    GstMiniObject * obj, gboolean * late, gboolean * step_end, guint8 obj_type)
 {
-  GstClockTimeDiff jitter;
+  GstClockTimeDiff jitter = 0;
   gboolean syncable;
   GstClockReturn status = GST_CLOCK_OK;
   GstClockTime rstart, rstop, sstart, sstop, stime;
@@ -2229,7 +2467,7 @@ do_step:
   /* get timing information for this object against the render segment */
   syncable = gst_base_sink_get_sync_times (basesink, obj,
       &sstart, &sstop, &rstart, &rstop, &do_sync, &stepped, &basesink->segment,
-      current, step_end);
+      current, step_end, obj_type);
 
   if (G_UNLIKELY (stepped))
     goto step_skipped;
@@ -2246,6 +2484,27 @@ do_step:
   /* save sync time for eos when the previous object needed sync */
   priv->eos_rtime = (do_sync ? priv->current_rstop : GST_CLOCK_TIME_NONE);
 
+  /* calculate inter frame spacing */
+  if (G_UNLIKELY (priv->prev_rstart != -1 && priv->prev_rstart < rstart)) {
+    GstClockTime in_diff;
+
+    in_diff = rstart - priv->prev_rstart;
+
+    if (priv->avg_in_diff == -1)
+      priv->avg_in_diff = in_diff;
+    else
+      priv->avg_in_diff = UPDATE_RUNNING_AVG (priv->avg_in_diff, in_diff);
+
+    GST_LOG_OBJECT (basesink, "avg frame diff %" GST_TIME_FORMAT,
+        GST_TIME_ARGS (priv->avg_in_diff));
+
+  }
+  priv->prev_rstart = rstart;
+
+  if (G_UNLIKELY (priv->earliest_in_time != -1
+          && rstart < priv->earliest_in_time))
+    goto qos_dropped;
+
 again:
   /* first do preroll, this makes sure we commit our state
    * to PAUSED and can continue to PLAYING. We cannot perform
@@ -2254,13 +2513,6 @@ again:
   if (G_UNLIKELY (ret != GST_FLOW_OK))
     goto preroll_failed;
 
-  /* After rendering we store the position of the last buffer so that we can use
-   * it to report the position. We need to take the lock here. */
-  GST_OBJECT_LOCK (basesink);
-  priv->current_sstart = sstart;
-  priv->current_sstop = (GST_CLOCK_TIME_IS_VALID (sstop) ? sstop : sstart);
-  GST_OBJECT_UNLOCK (basesink);
-
   /* update the segment with a pending step if the current one is invalid and we
    * have a new pending one. We only accept new step updates after a preroll */
   if (G_UNLIKELY (pending->valid && !current->valid)) {
@@ -2268,6 +2520,13 @@ again:
     goto do_step;
   }
 
+  /* After rendering we store the position of the last buffer so that we can use
+   * it to report the position. We need to take the lock here. */
+  GST_OBJECT_LOCK (basesink);
+  priv->current_sstart = sstart;
+  priv->current_sstop = (GST_CLOCK_TIME_IS_VALID (sstop) ? sstop : sstart);
+  GST_OBJECT_UNLOCK (basesink);
+
   if (!do_sync)
     goto done;
 
@@ -2291,7 +2550,8 @@ again:
    * or sync is disabled with GST_CLOCK_BADTIME. */
   status = gst_base_sink_wait_clock (basesink, stime, &jitter);
 
-  GST_DEBUG_OBJECT (basesink, "clock returned %d", status);
+  GST_DEBUG_OBJECT (basesink, "clock returned %d, jitter %c%" GST_TIME_FORMAT,
+      status, (jitter < 0 ? '-' : ' '), GST_TIME_ARGS (ABS (jitter)));
 
   /* invalid time, no clock or sync disabled, just render */
   if (status == GST_CLOCK_BADTIME)
@@ -2331,6 +2591,12 @@ not_syncable:
     GST_DEBUG_OBJECT (basesink, "non syncable object %p", obj);
     return GST_FLOW_OK;
   }
+qos_dropped:
+  {
+    GST_DEBUG_OBJECT (basesink, "dropped because of QoS %p", obj);
+    *late = TRUE;
+    return GST_FLOW_OK;
+  }
 flushing:
   {
     GST_DEBUG_OBJECT (basesink, "we are flushing");
@@ -2345,7 +2611,7 @@ preroll_failed:
 }
 
 static gboolean
-gst_base_sink_send_qos (GstBaseSink * basesink,
+gst_base_sink_send_qos (GstBaseSink * basesink, GstQOSType type,
     gdouble proportion, GstClockTime time, GstClockTimeDiff diff)
 {
   GstEvent *event;
@@ -2353,10 +2619,10 @@ gst_base_sink_send_qos (GstBaseSink * basesink,
 
   /* generate Quality-of-Service event */
   GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, basesink,
-      "qos: proportion: %lf, diff %" G_GINT64_FORMAT ", timestamp %"
-      GST_TIME_FORMAT, proportion, diff, GST_TIME_ARGS (time));
+      "qos: type %d, proportion: %lf, diff %" G_GINT64_FORMAT ", timestamp %"
+      GST_TIME_FORMAT, type, proportion, diff, GST_TIME_ARGS (time));
 
-  event = gst_event_new_qos (proportion, diff, time);
+  event = gst_event_new_qos_full (type, proportion, diff, time);
 
   /* send upstream */
   res = gst_pad_push_event (basesink->sinkpad, event);
@@ -2404,10 +2670,10 @@ gst_base_sink_perform_qos (GstBaseSink * sink, gboolean dropped)
   }
 
   /* calculate duration of the buffer */
-  if (GST_CLOCK_TIME_IS_VALID (stop))
+  if (GST_CLOCK_TIME_IS_VALID (stop) && stop != start)
     duration = stop - start;
   else
-    duration = GST_CLOCK_TIME_NONE;
+    duration = priv->avg_in_diff;
 
   /* if we have the time when the last buffer left us, calculate
    * processing time */
@@ -2422,11 +2688,11 @@ gst_base_sink_perform_qos (GstBaseSink * sink, gboolean dropped)
   }
 
   GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, sink, "start: %" GST_TIME_FORMAT
-      ", entered %" GST_TIME_FORMAT ", left %" GST_TIME_FORMAT ", pt: %"
-      GST_TIME_FORMAT ", duration %" GST_TIME_FORMAT ",jitter %"
-      G_GINT64_FORMAT, GST_TIME_ARGS (start), GST_TIME_ARGS (entered),
-      GST_TIME_ARGS (left), GST_TIME_ARGS (pt), GST_TIME_ARGS (duration),
-      jitter);
+      ", stop %" GST_TIME_FORMAT ", entered %" GST_TIME_FORMAT ", left %"
+      GST_TIME_FORMAT ", pt: %" GST_TIME_FORMAT ", duration %" GST_TIME_FORMAT
+      ",jitter %" G_GINT64_FORMAT, GST_TIME_ARGS (start), GST_TIME_ARGS (stop),
+      GST_TIME_ARGS (entered), GST_TIME_ARGS (left), GST_TIME_ARGS (pt),
+      GST_TIME_ARGS (duration), jitter);
 
   GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, sink, "avg_duration: %" GST_TIME_FORMAT
       ", avg_pt: %" GST_TIME_FORMAT ", avg_rate: %g",
@@ -2450,7 +2716,7 @@ gst_base_sink_perform_qos (GstBaseSink * sink, gboolean dropped)
         gst_guint64_to_gdouble (priv->avg_pt) /
         gst_guint64_to_gdouble (priv->avg_duration);
   else
-    rate = 0.0;
+    rate = 1.0;
 
   if (GST_CLOCK_TIME_IS_VALID (priv->last_left)) {
     if (dropped || priv->avg_rate < 0.0) {
@@ -2470,6 +2736,9 @@ gst_base_sink_perform_qos (GstBaseSink * sink, gboolean dropped)
 
 
   if (priv->avg_rate >= 0.0) {
+    GstQOSType type;
+    GstClockTimeDiff diff;
+
     /* if we have a valid rate, start sending QoS messages */
     if (priv->current_jitter < 0) {
       /* make sure we never go below 0 when adding the jitter to the
@@ -2477,8 +2746,20 @@ gst_base_sink_perform_qos (GstBaseSink * sink, gboolean dropped)
       if (priv->current_rstart < -priv->current_jitter)
         priv->current_jitter = -priv->current_rstart;
     }
-    gst_base_sink_send_qos (sink, priv->avg_rate, priv->current_rstart,
-        priv->current_jitter);
+
+    if (priv->throttle_time > 0) {
+      diff = priv->throttle_time;
+      type = GST_QOS_TYPE_THROTTLE;
+    } else {
+      diff = priv->current_jitter;
+      if (diff <= 0)
+        type = GST_QOS_TYPE_OVERFLOW;
+      else
+        type = GST_QOS_TYPE_UNDERFLOW;
+    }
+
+    gst_base_sink_send_qos (sink, type, priv->avg_rate, priv->current_rstart,
+        diff);
   }
 
   /* record when this buffer will leave us */
@@ -2493,12 +2774,15 @@ gst_base_sink_reset_qos (GstBaseSink * sink)
 
   priv = sink->priv;
 
-  priv->last_in_time = GST_CLOCK_TIME_NONE;
+  priv->last_render_time = GST_CLOCK_TIME_NONE;
+  priv->prev_rstart = GST_CLOCK_TIME_NONE;
+  priv->earliest_in_time = GST_CLOCK_TIME_NONE;
   priv->last_left = GST_CLOCK_TIME_NONE;
   priv->avg_duration = GST_CLOCK_TIME_NONE;
   priv->avg_pt = GST_CLOCK_TIME_NONE;
   priv->avg_rate = -1.0;
   priv->avg_render = GST_CLOCK_TIME_NONE;
+  priv->avg_in_diff = GST_CLOCK_TIME_NONE;
   priv->rendered = 0;
   priv->dropped = 0;
 
@@ -2506,7 +2790,7 @@ gst_base_sink_reset_qos (GstBaseSink * sink)
 
 /* Checks if the object was scheduled too late.
  *
- * start/stop contain the raw timestamp start and stop values
+ * rstart/rstop contain the running_time start and stop values
  * of the object.
  *
  * status and jitter contain the return values from the clock wait.
@@ -2515,7 +2799,7 @@ gst_base_sink_reset_qos (GstBaseSink * sink)
  */
 static gboolean
 gst_base_sink_is_too_late (GstBaseSink * basesink, GstMiniObject * obj,
-    GstClockTime start, GstClockTime stop,
+    GstClockTime rstart, GstClockTime rstop,
     GstClockReturn status, GstClockTimeDiff jitter)
 {
   gboolean late;
@@ -2530,7 +2814,7 @@ gst_base_sink_is_too_late (GstBaseSink * basesink, GstMiniObject * obj,
   if (G_LIKELY (status != GST_CLOCK_EARLY))
     goto in_time;
 
-  max_lateness = basesink->abidata.ABI.max_lateness;
+  max_lateness = basesink->max_lateness;
 
   /* check if frame dropping is enabled */
   if (max_lateness == -1)
@@ -2541,38 +2825,45 @@ gst_base_sink_is_too_late (GstBaseSink * basesink, GstMiniObject * obj,
     goto not_buffer;
 
   /* can't do check if we don't have a timestamp */
-  if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (start)))
+  if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (rstart)))
     goto no_timestamp;
 
   /* we can add a valid stop time */
-  if (GST_CLOCK_TIME_IS_VALID (stop))
-    max_lateness += stop;
-  else
-    max_lateness += start;
+  if (GST_CLOCK_TIME_IS_VALID (rstop))
+    max_lateness += rstop;
+  else {
+    max_lateness += rstart;
+    /* no stop time, use avg frame diff */
+    if (priv->avg_in_diff != -1)
+      max_lateness += priv->avg_in_diff;
+  }
 
   /* if the jitter bigger than duration and lateness we are too late */
-  if ((late = start + jitter > max_lateness)) {
+  if ((late = rstart + jitter > max_lateness)) {
     GST_CAT_DEBUG_OBJECT (GST_CAT_PERFORMANCE, basesink,
         "buffer is too late %" GST_TIME_FORMAT
-        " > %" GST_TIME_FORMAT, GST_TIME_ARGS (start + jitter),
+        " > %" GST_TIME_FORMAT, GST_TIME_ARGS (rstart + jitter),
         GST_TIME_ARGS (max_lateness));
     /* !!emergency!!, if we did not receive anything valid for more than a
      * second, render it anyway so the user sees something */
-    if (GST_CLOCK_TIME_IS_VALID (priv->last_in_time) &&
-        start - priv->last_in_time > GST_SECOND) {
+    if (GST_CLOCK_TIME_IS_VALID (priv->last_render_time) &&
+        rstart - priv->last_render_time > GST_SECOND) {
       late = FALSE;
       GST_ELEMENT_WARNING (basesink, CORE, CLOCK,
           (_("A lot of buffers are being dropped.")),
           ("There may be a timestamping problem, or this computer is too slow."));
       GST_CAT_DEBUG_OBJECT (GST_CAT_PERFORMANCE, basesink,
           "**emergency** last buffer at %" GST_TIME_FORMAT " > GST_SECOND",
-          GST_TIME_ARGS (priv->last_in_time));
+          GST_TIME_ARGS (priv->last_render_time));
     }
   }
 
 done:
-  if (!late) {
-    priv->last_in_time = start;
+  if (!late || !GST_CLOCK_TIME_IS_VALID (priv->last_render_time)) {
+    priv->last_render_time = rstart;
+    /* the next allowed input timestamp */
+    if (priv->throttle_time > 0)
+      priv->earliest_in_time = rstart + priv->throttle_time;
   }
   return late;
 
@@ -2636,23 +2927,22 @@ gst_base_sink_do_render_stats (GstBaseSink * basesink, gboolean start)
  */
 static GstFlowReturn
 gst_base_sink_render_object (GstBaseSink * basesink, GstPad * pad,
-    gboolean is_list, gpointer obj)
+    guint8 obj_type, gpointer obj)
 {
   GstFlowReturn ret;
   GstBaseSinkClass *bclass;
   gboolean late, step_end;
   gpointer sync_obj;
-
   GstBaseSinkPrivate *priv;
 
   priv = basesink->priv;
 
-  if (is_list) {
+  if (OBJ_IS_BUFFERLIST (obj_type)) {
     /*
      * If buffer list, use the first group buffer within the list
      * for syncing
      */
-    sync_obj = gst_buffer_list_get (GST_BUFFER_LIST_CAST (obj), 0, 0);
+    sync_obj = gst_buffer_list_get (GST_BUFFER_LIST_CAST (obj), 0);
     g_assert (NULL != sync_obj);
   } else {
     sync_obj = obj;
@@ -2664,20 +2954,22 @@ again:
 
   /* synchronize this object, non syncable objects return OK
    * immediatly. */
-  ret = gst_base_sink_do_sync (basesink, pad, sync_obj, &late, &step_end);
+  ret =
+      gst_base_sink_do_sync (basesink, pad, sync_obj, &late, &step_end,
+      obj_type);
   if (G_UNLIKELY (ret != GST_FLOW_OK))
     goto sync_failed;
 
   /* and now render, event or buffer/buffer list. */
-  if (G_LIKELY (is_list || GST_IS_BUFFER (obj))) {
+  if (G_LIKELY (OBJ_IS_BUFFERFULL (obj_type))) {
     /* drop late buffers unconditionally, let's hope it's unlikely */
     if (G_UNLIKELY (late))
       goto dropped;
 
     bclass = GST_BASE_SINK_GET_CLASS (basesink);
 
-    if (G_LIKELY ((is_list && bclass->render_list) ||
-            (!is_list && bclass->render))) {
+    if (G_LIKELY ((OBJ_IS_BUFFERLIST (obj_type) && bclass->render_list) ||
+            (!OBJ_IS_BUFFERLIST (obj_type) && bclass->render))) {
       gint do_qos;
 
       /* read once, to get same value before and after */
@@ -2689,7 +2981,7 @@ again:
       if (do_qos)
         gst_base_sink_do_render_stats (basesink, TRUE);
 
-      if (!is_list) {
+      if (!OBJ_IS_BUFFERLIST (obj_type)) {
         GstBuffer *buf;
 
         /* For buffer lists do not set last buffer. Creating buffer
@@ -2718,7 +3010,7 @@ again:
 
       priv->rendered++;
     }
-  } else {
+  } else if (G_LIKELY (OBJ_IS_EVENT (obj_type))) {
     GstEvent *event = GST_EVENT_CAST (obj);
     gboolean event_res = TRUE;
     GstEventType type;
@@ -2770,10 +3062,20 @@ again:
           gst_base_sink_configure_segment (basesink, pad, event,
               &basesink->segment);
           break;
+        case GST_EVENT_SINK_MESSAGE:{
+          GstMessage *msg = NULL;
+
+          gst_event_parse_sink_message (event, &msg);
+
+          if (msg)
+            gst_element_post_message (GST_ELEMENT_CAST (basesink), msg);
+        }
         default:
           break;
       }
     }
+  } else {
+    g_return_val_if_reached (GST_FLOW_ERROR);
   }
 
 done:
@@ -2801,6 +3103,33 @@ dropped:
   {
     priv->dropped++;
     GST_DEBUG_OBJECT (basesink, "buffer late, dropping");
+
+    if (g_atomic_int_get (&priv->qos_enabled)) {
+      GstMessage *qos_msg;
+      GstClockTime timestamp, duration;
+
+      timestamp = GST_BUFFER_TIMESTAMP (GST_BUFFER_CAST (sync_obj));
+      duration = GST_BUFFER_DURATION (GST_BUFFER_CAST (sync_obj));
+
+      GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, basesink,
+          "qos: dropped buffer rt %" GST_TIME_FORMAT ", st %" GST_TIME_FORMAT
+          ", ts %" GST_TIME_FORMAT ", dur %" GST_TIME_FORMAT,
+          GST_TIME_ARGS (priv->current_rstart),
+          GST_TIME_ARGS (priv->current_sstart), GST_TIME_ARGS (timestamp),
+          GST_TIME_ARGS (duration));
+      GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, basesink,
+          "qos: rendered %" G_GUINT64_FORMAT ", dropped %" G_GUINT64_FORMAT,
+          priv->rendered, priv->dropped);
+
+      qos_msg =
+          gst_message_new_qos (GST_OBJECT_CAST (basesink), basesink->sync,
+          priv->current_rstart, priv->current_sstart, timestamp, duration);
+      gst_message_set_qos_values (qos_msg, priv->current_jitter, priv->avg_rate,
+          1000000);
+      gst_message_set_qos_stats (qos_msg, GST_FORMAT_BUFFERS, priv->rendered,
+          priv->dropped);
+      gst_element_post_message (GST_ELEMENT_CAST (basesink), qos_msg);
+    }
     goto done;
   }
 flushing:
@@ -2820,7 +3149,7 @@ flushing:
  * function does not take ownership of obj.
  */
 static GstFlowReturn
-gst_base_sink_preroll_object (GstBaseSink * basesink, gboolean is_list,
+gst_base_sink_preroll_object (GstBaseSink * basesink, guint8 obj_type,
     GstMiniObject * obj)
 {
   GstFlowReturn ret;
@@ -2828,13 +3157,13 @@ gst_base_sink_preroll_object (GstBaseSink * basesink, gboolean is_list,
   GST_DEBUG_OBJECT (basesink, "prerolling object %p", obj);
 
   /* if it's a buffer, we need to call the preroll method */
-  if (G_LIKELY (is_list || GST_IS_BUFFER (obj)) && basesink->priv->call_preroll) {
+  if (G_LIKELY (OBJ_IS_BUFFERFULL (obj_type) && basesink->priv->call_preroll)) {
     GstBaseSinkClass *bclass;
     GstBuffer *buf;
     GstClockTime timestamp;
 
-    if (is_list) {
-      buf = gst_buffer_list_get (GST_BUFFER_LIST_CAST (obj), 0, 0);
+    if (OBJ_IS_BUFFERLIST (obj_type)) {
+      buf = gst_buffer_list_get (GST_BUFFER_LIST_CAST (obj), 0);
       g_assert (NULL != buf);
     } else {
       buf = GST_BUFFER_CAST (obj);
@@ -2850,7 +3179,7 @@ gst_base_sink_preroll_object (GstBaseSink * basesink, gboolean is_list,
      * with meaningful data can be done only with memcpy which will
      * significantly affect performance
      */
-    if (!is_list) {
+    if (!OBJ_IS_BUFFERLIST (obj_type)) {
       gst_base_sink_set_last_buffer (basesink, buf);
     }
 
@@ -2894,7 +3223,7 @@ stopping:
  */
 static GstFlowReturn
 gst_base_sink_queue_object_unlocked (GstBaseSink * basesink, GstPad * pad,
-    gboolean is_list, gpointer obj, gboolean prerollable)
+    guint8 obj_type, gpointer obj, gboolean prerollable)
 {
   GstFlowReturn ret = GST_FLOW_OK;
   gint length;
@@ -2910,7 +3239,7 @@ gst_base_sink_queue_object_unlocked (GstBaseSink * basesink, GstPad * pad,
 
     /* first prerollable item needs to finish the preroll */
     if (length == 1) {
-      ret = gst_base_sink_preroll_object (basesink, is_list, obj);
+      ret = gst_base_sink_preroll_object (basesink, obj_type, obj);
       if (G_UNLIKELY (ret != GST_FLOW_OK))
         goto preroll_failed;
     }
@@ -2928,18 +3257,21 @@ gst_base_sink_queue_object_unlocked (GstBaseSink * basesink, GstPad * pad,
   q = basesink->preroll_queue;
   while (G_UNLIKELY (!g_queue_is_empty (q))) {
     GstMiniObject *o;
+    guint8 ot;
 
     o = g_queue_pop_head (q);
     GST_DEBUG_OBJECT (basesink, "rendering queued object %p", o);
 
+    ot = get_object_type (o);
+
     /* do something with the return value */
-    ret = gst_base_sink_render_object (basesink, pad, FALSE, o);
+    ret = gst_base_sink_render_object (basesink, pad, ot, o);
     if (ret != GST_FLOW_OK)
       goto dequeue_failed;
   }
 
   /* now render the object */
-  ret = gst_base_sink_render_object (basesink, pad, is_list, obj);
+  ret = gst_base_sink_render_object (basesink, pad, obj_type, obj);
   basesink->preroll_queued = 0;
 
   return ret;
@@ -2975,6 +3307,8 @@ dequeue_failed:
  * the queue.
  *
  * This function takes ownership of obj.
+ *
+ * Note: Only GstEvent seem to be passed to this private method
  */
 static GstFlowReturn
 gst_base_sink_queue_object (GstBaseSink * basesink, GstPad * pad,
@@ -2982,7 +3316,7 @@ gst_base_sink_queue_object (GstBaseSink * basesink, GstPad * pad,
 {
   GstFlowReturn ret;
 
-  GST_PAD_PREROLL_LOCK (pad);
+  GST_BASE_SINK_PREROLL_LOCK (basesink);
   if (G_UNLIKELY (basesink->flushing))
     goto flushing;
 
@@ -2990,9 +3324,9 @@ gst_base_sink_queue_object (GstBaseSink * basesink, GstPad * pad,
     goto was_eos;
 
   ret =
-      gst_base_sink_queue_object_unlocked (basesink, pad, FALSE, obj,
+      gst_base_sink_queue_object_unlocked (basesink, pad, _PR_IS_EVENT, obj,
       prerollable);
-  GST_PAD_PREROLL_UNLOCK (pad);
+  GST_BASE_SINK_PREROLL_UNLOCK (basesink);
 
   return ret;
 
@@ -3000,7 +3334,7 @@ gst_base_sink_queue_object (GstBaseSink * basesink, GstPad * pad,
 flushing:
   {
     GST_DEBUG_OBJECT (basesink, "sink is flushing");
-    GST_PAD_PREROLL_UNLOCK (pad);
+    GST_BASE_SINK_PREROLL_UNLOCK (basesink);
     gst_mini_object_unref (obj);
     return GST_FLOW_WRONG_STATE;
   }
@@ -3008,7 +3342,7 @@ was_eos:
   {
     GST_DEBUG_OBJECT (basesink,
         "we are EOS, dropping object, return UNEXPECTED");
-    GST_PAD_PREROLL_UNLOCK (pad);
+    GST_BASE_SINK_PREROLL_UNLOCK (basesink);
     gst_mini_object_unref (obj);
     return GST_FLOW_UNEXPECTED;
   }
@@ -3026,14 +3360,13 @@ gst_base_sink_flush_start (GstBaseSink * basesink, GstPad * pad)
    * anymore */
   GST_PAD_STREAM_LOCK (pad);
   gst_base_sink_reset_qos (basesink);
+  /* and we need to commit our state again on the next
+   * prerolled buffer */
+  basesink->playing_async = TRUE;
   if (basesink->priv->async_enabled) {
-    /* and we need to commit our state again on the next
-     * prerolled buffer */
-    basesink->playing_async = TRUE;
     gst_element_lost_state (GST_ELEMENT_CAST (basesink));
   } else {
     basesink->priv->have_latency = TRUE;
-    basesink->need_preroll = FALSE;
   }
   gst_base_sink_set_last_buffer (basesink, NULL);
   GST_PAD_STREAM_UNLOCK (pad);
@@ -3058,7 +3391,7 @@ gst_base_sink_flush_stop (GstBaseSink * basesink, GstPad * pad)
     /* we need new segment info after the flush. */
     basesink->have_newsegment = FALSE;
     gst_segment_init (&basesink->segment, GST_FORMAT_UNDEFINED);
-    gst_segment_init (basesink->abidata.ABI.clip_segment, GST_FORMAT_UNDEFINED);
+    gst_segment_init (basesink->clip_segment, GST_FORMAT_UNDEFINED);
   }
   GST_OBJECT_UNLOCK (basesink);
 }
@@ -3071,18 +3404,22 @@ gst_base_sink_event (GstPad * pad, GstEvent * event)
   GstBaseSinkClass *bclass;
 
   basesink = GST_BASE_SINK (gst_pad_get_parent (pad));
+  if (G_UNLIKELY (basesink == NULL)) {
+    gst_event_unref (event);
+    return FALSE;
+  }
 
   bclass = GST_BASE_SINK_GET_CLASS (basesink);
 
-  GST_DEBUG_OBJECT (basesink, "event %p (%s)", event,
-      GST_EVENT_TYPE_NAME (event));
+  GST_DEBUG_OBJECT (basesink, "received event %p %" GST_PTR_FORMAT, event,
+      event);
 
   switch (GST_EVENT_TYPE (event)) {
     case GST_EVENT_EOS:
     {
       GstFlowReturn ret;
 
-      GST_PAD_PREROLL_LOCK (pad);
+      GST_BASE_SINK_PREROLL_LOCK (basesink);
       if (G_UNLIKELY (basesink->flushing))
         goto flushing;
 
@@ -3098,24 +3435,28 @@ gst_base_sink_event (GstPad * pad, GstEvent * event)
         /* EOS is a prerollable object, we call the unlocked version because it
          * does not check the received_eos flag. */
         ret = gst_base_sink_queue_object_unlocked (basesink, pad,
-            FALSE, GST_MINI_OBJECT_CAST (event), TRUE);
+            _PR_IS_EVENT, GST_MINI_OBJECT_CAST (event), TRUE);
         if (G_UNLIKELY (ret != GST_FLOW_OK))
           result = FALSE;
       }
-      GST_PAD_PREROLL_UNLOCK (pad);
+      GST_BASE_SINK_PREROLL_UNLOCK (basesink);
       break;
     }
     case GST_EVENT_NEWSEGMENT:
     {
       GstFlowReturn ret;
+      gboolean update;
 
       GST_DEBUG_OBJECT (basesink, "newsegment %p", event);
 
-      GST_PAD_PREROLL_LOCK (pad);
+      GST_BASE_SINK_PREROLL_LOCK (basesink);
       if (G_UNLIKELY (basesink->flushing))
         goto flushing;
 
-      if (G_UNLIKELY (basesink->priv->received_eos)) {
+      gst_event_parse_new_segment_full (event, &update, NULL, NULL, NULL, NULL,
+          NULL, NULL);
+
+      if (G_UNLIKELY (basesink->priv->received_eos && !update)) {
         /* we can't accept anything when we are EOS */
         result = FALSE;
         gst_event_unref (event);
@@ -3124,11 +3465,11 @@ gst_base_sink_event (GstPad * pad, GstEvent * event)
          * we need to configure the current clipping segment and insert the event
          * in the queue to serialize it with the buffers for rendering. */
         gst_base_sink_configure_segment (basesink, pad, event,
-            basesink->abidata.ABI.clip_segment);
+            basesink->clip_segment);
 
         ret =
             gst_base_sink_queue_object_unlocked (basesink, pad,
-            FALSE, GST_MINI_OBJECT_CAST (event), FALSE);
+            _PR_IS_EVENT, GST_MINI_OBJECT_CAST (event), FALSE);
         if (G_UNLIKELY (ret != GST_FLOW_OK))
           result = FALSE;
         else {
@@ -3137,7 +3478,7 @@ gst_base_sink_event (GstPad * pad, GstEvent * event)
           GST_OBJECT_UNLOCK (basesink);
         }
       }
-      GST_PAD_PREROLL_UNLOCK (pad);
+      GST_BASE_SINK_PREROLL_UNLOCK (basesink);
       break;
     }
     case GST_EVENT_FLUSH_START:
@@ -3182,7 +3523,7 @@ done:
 flushing:
   {
     GST_DEBUG_OBJECT (basesink, "we are flushing");
-    GST_PAD_PREROLL_UNLOCK (pad);
+    GST_BASE_SINK_PREROLL_UNLOCK (basesink);
     result = FALSE;
     gst_event_unref (event);
     goto done;
@@ -3239,7 +3580,7 @@ gst_base_sink_needs_preroll (GstBaseSink * basesink)
  */
 static GstFlowReturn
 gst_base_sink_chain_unlocked (GstBaseSink * basesink, GstPad * pad,
-    gboolean is_list, gpointer obj)
+    guint8 obj_type, gpointer obj)
 {
   GstBaseSinkClass *bclass;
   GstFlowReturn result;
@@ -3253,15 +3594,15 @@ gst_base_sink_chain_unlocked (GstBaseSink * basesink, GstPad * pad,
   if (G_UNLIKELY (basesink->priv->received_eos))
     goto was_eos;
 
-  if (is_list) {
-    time_buf = gst_buffer_list_get (GST_BUFFER_LIST_CAST (obj), 0, 0);
+  if (OBJ_IS_BUFFERLIST (obj_type)) {
+    time_buf = gst_buffer_list_get (GST_BUFFER_LIST_CAST (obj), 0);
     g_assert (NULL != time_buf);
   } else {
     time_buf = GST_BUFFER_CAST (obj);
   }
 
   /* for code clarity */
-  clip_segment = basesink->abidata.ABI.clip_segment;
+  clip_segment = basesink->clip_segment;
 
   if (G_UNLIKELY (!basesink->have_newsegment)) {
     gboolean sync;
@@ -3276,9 +3617,9 @@ gst_base_sink_chain_unlocked (GstBaseSink * basesink, GstPad * pad,
     /* this means this sink will assume timestamps start from 0 */
     GST_OBJECT_LOCK (basesink);
     clip_segment->start = 0;
-    clip_segment->stop = GST_CLOCK_TIME_NONE;
+    clip_segment->stop = -1;
     basesink->segment.start = 0;
-    basesink->segment.stop = GST_CLOCK_TIME_NONE;
+    basesink->segment.stop = -1;
     basesink->have_newsegment = TRUE;
     GST_OBJECT_UNLOCK (basesink);
   }
@@ -3310,7 +3651,7 @@ gst_base_sink_chain_unlocked (GstBaseSink * basesink, GstPad * pad,
   /* now we can process the buffer in the queue, this function takes ownership
    * of the buffer */
   result = gst_base_sink_queue_object_unlocked (basesink, pad,
-      is_list, obj, TRUE);
+      obj_type, obj, TRUE);
   return result;
 
   /* ERRORS */
@@ -3339,16 +3680,16 @@ out_of_segment:
  */
 static GstFlowReturn
 gst_base_sink_chain_main (GstBaseSink * basesink, GstPad * pad,
-    gboolean is_list, gpointer obj)
+    guint8 obj_type, gpointer obj)
 {
   GstFlowReturn result;
 
   if (G_UNLIKELY (basesink->pad_mode != GST_ACTIVATE_PUSH))
     goto wrong_mode;
 
-  GST_PAD_PREROLL_LOCK (pad);
-  result = gst_base_sink_chain_unlocked (basesink, pad, is_list, obj);
-  GST_PAD_PREROLL_UNLOCK (pad);
+  GST_BASE_SINK_PREROLL_LOCK (basesink);
+  result = gst_base_sink_chain_unlocked (basesink, pad, obj_type, obj);
+  GST_BASE_SINK_PREROLL_UNLOCK (basesink);
 
 done:
   return result;
@@ -3376,7 +3717,7 @@ gst_base_sink_chain (GstPad * pad, GstBuffer * buf)
 
   basesink = GST_BASE_SINK (GST_OBJECT_PARENT (pad));
 
-  return gst_base_sink_chain_main (basesink, pad, FALSE, buf);
+  return gst_base_sink_chain_main (basesink, pad, _PR_IS_BUFFER, buf);
 }
 
 static GstFlowReturn
@@ -3390,33 +3731,23 @@ gst_base_sink_chain_list (GstPad * pad, GstBufferList * list)
   bclass = GST_BASE_SINK_GET_CLASS (basesink);
 
   if (G_LIKELY (bclass->render_list)) {
-    result = gst_base_sink_chain_main (basesink, pad, TRUE, list);
+    result = gst_base_sink_chain_main (basesink, pad, _PR_IS_BUFFERLIST, list);
   } else {
-    GstBufferListIterator *it;
-    GstBuffer *group;
+    guint i, len;
+    GstBuffer *buffer;
 
     GST_INFO_OBJECT (pad, "chaining each group in list as a merged buffer");
 
-    it = gst_buffer_list_iterate (list);
+    len = gst_buffer_list_len (list);
 
-    if (gst_buffer_list_iterator_next_group (it)) {
-      do {
-        group = gst_buffer_list_iterator_merge_group (it);
-        if (group == NULL) {
-          group = gst_buffer_new ();
-          GST_CAT_INFO_OBJECT (GST_CAT_SCHEDULING, pad, "chaining empty group");
-        } else {
-          GST_CAT_INFO_OBJECT (GST_CAT_SCHEDULING, pad, "chaining group");
-        }
-        result = gst_base_sink_chain_main (basesink, pad, FALSE, group);
-      } while (result == GST_FLOW_OK
-          && gst_buffer_list_iterator_next_group (it));
-    } else {
-      GST_CAT_INFO_OBJECT (GST_CAT_SCHEDULING, pad, "chaining empty group");
-      result =
-          gst_base_sink_chain_main (basesink, pad, FALSE, gst_buffer_new ());
+    result = GST_FLOW_OK;
+    for (i = 0; i < len; i++) {
+      buffer = gst_buffer_list_get (list, 0);
+      result = gst_base_sink_chain_main (basesink, pad, _PR_IS_BUFFER,
+          gst_buffer_ref (buffer));
+      if (result != GST_FLOW_OK)
+        break;
     }
-    gst_buffer_list_iterator_free (it);
     gst_buffer_list_unref (list);
   }
   return result;
@@ -3585,7 +3916,7 @@ gst_base_sink_perform_seek (GstBaseSink * sink, GstPad * pad, GstEvent * event)
     GST_DEBUG_OBJECT (sink, "stop flushing upstream");
     gst_pad_push_event (pad, gst_event_new_flush_stop ());
     gst_base_sink_flush_stop (sink, pad);
-  } else if (res && sink->abidata.ABI.running) {
+  } else if (res && sink->running) {
     /* we are running the current segment and doing a non-flushing seek,
      * close the segment first based on the last_stop. */
     GST_DEBUG_OBJECT (sink, "closing running segment %" G_GINT64_FORMAT
@@ -3613,7 +3944,7 @@ gst_base_sink_perform_seek (GstBaseSink * sink, GstPad * pad, GstEvent * event)
   }
 
   sink->priv->discont = TRUE;
-  sink->abidata.ABI.running = TRUE;
+  sink->running = TRUE;
 
   GST_PAD_STREAM_UNLOCK (pad);
 
@@ -3676,7 +4007,7 @@ gst_base_sink_perform_step (GstBaseSink * sink, GstPad * pad, GstEvent * event)
     if (bclass->unlock)
       bclass->unlock (sink);
 
-    GST_PAD_PREROLL_LOCK (sink->sinkpad);
+    GST_BASE_SINK_PREROLL_LOCK (sink);
     /* now that we have the PREROLL lock, clear our unlock request */
     if (bclass->unlock_stop)
       bclass->unlock_stop (sink);
@@ -3710,9 +4041,9 @@ gst_base_sink_perform_step (GstBaseSink * sink, GstPad * pad, GstEvent * event)
     if (sink->have_preroll) {
       GST_DEBUG_OBJECT (sink, "signal waiter");
       priv->step_unlock = TRUE;
-      GST_PAD_PREROLL_SIGNAL (sink->sinkpad);
+      GST_BASE_SINK_PREROLL_SIGNAL (sink);
     }
-    GST_PAD_PREROLL_UNLOCK (sink->sinkpad);
+    GST_BASE_SINK_PREROLL_UNLOCK (sink);
   } else {
     /* update the stepinfo and make it valid */
     set_step_info (sink, current, pending, seqnum, format, amount, rate, flush,
@@ -3752,13 +4083,13 @@ gst_base_sink_loop (GstPad * pad)
   if (G_UNLIKELY (buf == NULL))
     goto no_buffer;
 
-  offset += GST_BUFFER_SIZE (buf);
+  offset += gst_buffer_get_size (buf);
 
   gst_segment_set_last_stop (&basesink->segment, GST_FORMAT_BYTES, offset);
 
-  GST_PAD_PREROLL_LOCK (pad);
-  result = gst_base_sink_chain_unlocked (basesink, pad, FALSE, buf);
-  GST_PAD_PREROLL_UNLOCK (pad);
+  GST_BASE_SINK_PREROLL_LOCK (basesink);
+  result = gst_base_sink_chain_unlocked (basesink, pad, _PR_IS_BUFFER, buf);
+  GST_BASE_SINK_PREROLL_UNLOCK (basesink);
   if (G_UNLIKELY (result != GST_FLOW_OK))
     goto paused;
 
@@ -3770,25 +4101,26 @@ paused:
     GST_LOG_OBJECT (basesink, "pausing task, reason %s",
         gst_flow_get_name (result));
     gst_pad_pause_task (pad);
-    /* fatal errors and NOT_LINKED cause EOS */
-    if (GST_FLOW_IS_FATAL (result) || result == GST_FLOW_NOT_LINKED) {
-      if (result == GST_FLOW_UNEXPECTED) {
-        /* perform EOS logic */
-        if (basesink->segment.flags & GST_SEEK_FLAG_SEGMENT) {
-          gst_element_post_message (GST_ELEMENT_CAST (basesink),
-              gst_message_new_segment_done (GST_OBJECT_CAST (basesink),
-                  basesink->segment.format, basesink->segment.last_stop));
-        } else {
-          gst_base_sink_event (pad, gst_event_new_eos ());
-        }
+    if (result == GST_FLOW_UNEXPECTED) {
+      /* perform EOS logic */
+      if (basesink->segment.flags & GST_SEEK_FLAG_SEGMENT) {
+        gst_element_post_message (GST_ELEMENT_CAST (basesink),
+            gst_message_new_segment_done (GST_OBJECT_CAST (basesink),
+                basesink->segment.format, basesink->segment.last_stop));
       } else {
-        /* for fatal errors we post an error message, post the error
-         * first so the app knows about the error first. */
-        GST_ELEMENT_ERROR (basesink, STREAM, FAILED,
-            (_("Internal data stream error.")),
-            ("stream stopped, reason %s", gst_flow_get_name (result)));
         gst_base_sink_event (pad, gst_event_new_eos ());
       }
+    } else if (result == GST_FLOW_NOT_LINKED || result <= GST_FLOW_UNEXPECTED) {
+      /* for fatal errors we post an error message, post the error
+       * first so the app knows about the error first. 
+       * wrong-state is not a fatal error because it happens due to
+       * flushing and posting an error message in that case is the
+       * wrong thing to do, e.g. when basesrc is doing a flushing
+       * seek. */
+      GST_ELEMENT_ERROR (basesink, STREAM, FAILED,
+          (_("Internal data stream error.")),
+          ("stream stopped, reason %s", gst_flow_get_name (result)));
+      gst_base_sink_event (pad, gst_event_new_eos ());
     }
     return;
   }
@@ -3817,7 +4149,7 @@ gst_base_sink_set_flushing (GstBaseSink * basesink, GstPad * pad,
       bclass->unlock (basesink);
   }
 
-  GST_PAD_PREROLL_LOCK (pad);
+  GST_BASE_SINK_PREROLL_LOCK (basesink);
   basesink->flushing = flushing;
   if (flushing) {
     /* step 1, now that we have the PREROLL lock, clear our unlock request */
@@ -3839,7 +4171,7 @@ gst_base_sink_set_flushing (GstBaseSink * basesink, GstPad * pad,
         "flushing out data thread, need preroll to TRUE");
     gst_base_sink_preroll_queue_flush (basesink, pad);
   }
-  GST_PAD_PREROLL_UNLOCK (pad);
+  GST_BASE_SINK_PREROLL_UNLOCK (basesink);
 
   return TRUE;
 }
@@ -4044,7 +4376,7 @@ gst_base_sink_pad_activate_pull (GstPad * pad, gboolean active)
     format = GST_FORMAT_BYTES;
 
     gst_segment_init (&basesink->segment, format);
-    gst_segment_init (basesink->abidata.ABI.clip_segment, format);
+    gst_segment_init (basesink->clip_segment, format);
     GST_OBJECT_LOCK (basesink);
     basesink->have_newsegment = TRUE;
     GST_OBJECT_UNLOCK (basesink);
@@ -4054,8 +4386,7 @@ gst_base_sink_pad_activate_pull (GstPad * pad, gboolean active)
     if (result) {
       GST_DEBUG_OBJECT (basesink,
           "setting duration in bytes to %" G_GINT64_FORMAT, duration);
-      gst_segment_set_duration (basesink->abidata.ABI.clip_segment, format,
-          duration);
+      gst_segment_set_duration (basesink->clip_segment, format, duration);
       gst_segment_set_duration (&basesink->segment, format, duration);
     } else {
       GST_DEBUG_OBJECT (basesink, "unknown duration");
@@ -4117,6 +4448,9 @@ gst_base_sink_send_event (GstElement * element, GstEvent * event)
   /* only push UPSTREAM events upstream */
   forward = GST_EVENT_IS_UPSTREAM (event);
 
+  GST_DEBUG_OBJECT (basesink, "handling event %p %" GST_PTR_FORMAT, event,
+      event);
+
   switch (GST_EVENT_TYPE (event)) {
     case GST_EVENT_LATENCY:
     {
@@ -4164,241 +4498,202 @@ gst_base_sink_send_event (GstElement * element, GstEvent * event)
 }
 
 static gboolean
-gst_base_sink_peer_query (GstBaseSink * sink, GstQuery * query)
-{
-  GstPad *peer;
-  gboolean res = FALSE;
-
-  if ((peer = gst_pad_get_peer (sink->sinkpad))) {
-    res = gst_pad_query (peer, query);
-    gst_object_unref (peer);
-  }
-  return res;
-}
-
-/* get the end position of the last seen object, this is used
- * for EOS and for making sure that we don't report a position we
- * have not reached yet. With LOCK. */
-static gboolean
-gst_base_sink_get_position_last (GstBaseSink * basesink, GstFormat format,
-    gint64 * cur, gboolean * upstream)
-{
-  GstFormat oformat;
-  GstSegment *segment;
-  gboolean ret = TRUE;
-
-  segment = &basesink->segment;
-  oformat = segment->format;
-
-  if (oformat == GST_FORMAT_TIME) {
-    /* return last observed stream time, we keep the stream time around in the
-     * time format. */
-    *cur = basesink->priv->current_sstop;
-  } else {
-    /* convert last stop to stream time */
-    *cur = gst_segment_to_stream_time (segment, oformat, segment->last_stop);
-  }
-
-  if (*cur != -1 && oformat != format) {
-    GST_OBJECT_UNLOCK (basesink);
-    /* convert to the target format if we need to, release lock first */
-    ret =
-        gst_pad_query_convert (basesink->sinkpad, oformat, *cur, &format, cur);
-    if (!ret) {
-      *cur = -1;
-      *upstream = TRUE;
-    }
-    GST_OBJECT_LOCK (basesink);
-  }
-
-  GST_DEBUG_OBJECT (basesink, "POSITION: %" GST_TIME_FORMAT,
-      GST_TIME_ARGS (*cur));
-
-  return ret;
-}
-
-/* get the position when we are PAUSED, this is the stream time of the buffer
- * that prerolled. If no buffer is prerolled (we are still flushing), this
- * value will be -1. With LOCK. */
-static gboolean
-gst_base_sink_get_position_paused (GstBaseSink * basesink, GstFormat format,
-    gint64 * cur, gboolean * upstream)
-{
-  gboolean res;
-  gint64 time;
-  GstSegment *segment;
-  GstFormat oformat;
-
-  /* we don't use the clip segment in pull mode, when seeking we update the
-   * main segment directly with the new segment values without it having to be
-   * activated by the rendering after preroll */
-  if (basesink->pad_mode == GST_ACTIVATE_PUSH)
-    segment = basesink->abidata.ABI.clip_segment;
-  else
-    segment = &basesink->segment;
-  oformat = segment->format;
-
-  if (oformat == GST_FORMAT_TIME) {
-    *cur = basesink->priv->current_sstart;
-    if (segment->rate < 0.0 &&
-        GST_CLOCK_TIME_IS_VALID (basesink->priv->current_sstop)) {
-      /* for reverse playback we prefer the stream time stop position if we have
-       * one */
-      *cur = basesink->priv->current_sstop;
-    }
-  } else {
-    *cur = gst_segment_to_stream_time (segment, oformat, segment->last_stop);
-  }
-
-  time = segment->time;
-
-  if (*cur != -1) {
-    *cur = MAX (*cur, time);
-    GST_DEBUG_OBJECT (basesink, "POSITION as max: %" GST_TIME_FORMAT
-        ", time %" GST_TIME_FORMAT, GST_TIME_ARGS (*cur), GST_TIME_ARGS (time));
-  } else {
-    /* we have no buffer, use the segment times. */
-    if (segment->rate >= 0.0) {
-      /* forward, next position is always the time of the segment */
-      *cur = time;
-      GST_DEBUG_OBJECT (basesink, "POSITION as time: %" GST_TIME_FORMAT,
-          GST_TIME_ARGS (*cur));
-    } else {
-      /* reverse, next expected timestamp is segment->stop. We use the function
-       * to get things right for negative applied_rates. */
-      *cur = gst_segment_to_stream_time (segment, oformat, segment->stop);
-      GST_DEBUG_OBJECT (basesink, "reverse POSITION: %" GST_TIME_FORMAT,
-          GST_TIME_ARGS (*cur));
-    }
-  }
-
-  res = (*cur != -1);
-  if (res && oformat != format) {
-    GST_OBJECT_UNLOCK (basesink);
-    res =
-        gst_pad_query_convert (basesink->sinkpad, oformat, *cur, &format, cur);
-    if (!res) {
-      *cur = -1;
-      *upstream = TRUE;
-    }
-    GST_OBJECT_LOCK (basesink);
-  }
-
-  return res;
-}
-
-static gboolean
 gst_base_sink_get_position (GstBaseSink * basesink, GstFormat format,
     gint64 * cur, gboolean * upstream)
 {
-  GstClock *clock;
+  GstClock *clock = NULL;
   gboolean res = FALSE;
   GstFormat oformat, tformat;
-  GstClockTime now, base, latency;
+  GstSegment *segment;
+  GstClockTime now, latency;
+  GstClockTimeDiff base;
   gint64 time, accum, duration;
   gdouble rate;
   gint64 last;
+  gboolean last_seen, with_clock, in_paused;
 
   GST_OBJECT_LOCK (basesink);
-  /* our intermediate time format */
-  tformat = GST_FORMAT_TIME;
-  /* get the format in the segment */
-  oformat = basesink->segment.format;
-
-  /* can only give answer based on the clock if not EOS */
-  if (G_UNLIKELY (basesink->eos))
-    goto in_eos;
-
   /* we can only get the segment when we are not NULL or READY */
   if (!basesink->have_newsegment)
     goto wrong_state;
 
+  in_paused = FALSE;
   /* when not in PLAYING or when we're busy with a state change, we
    * cannot read from the clock so we report time based on the
    * last seen timestamp. */
   if (GST_STATE (basesink) != GST_STATE_PLAYING ||
-      GST_STATE_PENDING (basesink) != GST_STATE_VOID_PENDING)
-    goto in_pause;
+      GST_STATE_PENDING (basesink) != GST_STATE_VOID_PENDING) {
+    in_paused = TRUE;
+  }
 
-  /* we need to sync on the clock. */
+  /* we don't use the clip segment in pull mode, when seeking we update the
+   * main segment directly with the new segment values without it having to be
+   * activated by the rendering after preroll */
+  if (basesink->pad_mode == GST_ACTIVATE_PUSH)
+    segment = basesink->clip_segment;
+  else
+    segment = &basesink->segment;
+
+  /* our intermediate time format */
+  tformat = GST_FORMAT_TIME;
+  /* get the format in the segment */
+  oformat = segment->format;
+
+  /* report with last seen position when EOS */
+  last_seen = basesink->eos;
+
+  /* assume we will use the clock for getting the current position */
+  with_clock = TRUE;
   if (basesink->sync == FALSE)
-    goto no_sync;
+    with_clock = FALSE;
 
   /* and we need a clock */
   if (G_UNLIKELY ((clock = GST_ELEMENT_CLOCK (basesink)) == NULL))
-    goto no_sync;
+    with_clock = FALSE;
+  else
+    gst_object_ref (clock);
 
   /* collect all data we need holding the lock */
-  if (GST_CLOCK_TIME_IS_VALID (basesink->segment.time))
-    time = basesink->segment.time;
+  if (GST_CLOCK_TIME_IS_VALID (segment->time))
+    time = segment->time;
   else
     time = 0;
 
-  if (GST_CLOCK_TIME_IS_VALID (basesink->segment.stop))
-    duration = basesink->segment.stop - basesink->segment.start;
+  if (GST_CLOCK_TIME_IS_VALID (segment->stop))
+    duration = segment->stop - segment->start;
   else
     duration = 0;
 
-  base = GST_ELEMENT_CAST (basesink)->base_time;
-  accum = basesink->segment.accum;
-  rate = basesink->segment.rate * basesink->segment.applied_rate;
+  accum = segment->accum;
+  rate = segment->rate * segment->applied_rate;
   latency = basesink->priv->latency;
 
-  gst_object_ref (clock);
+  if (oformat == GST_FORMAT_TIME) {
+    gint64 start, stop;
+
+    start = basesink->priv->current_sstart;
+    stop = basesink->priv->current_sstop;
+
+    if (in_paused) {
+      /* in paused we use the last position as a lower bound */
+      if (stop == -1 || segment->rate > 0.0)
+        last = start;
+      else
+        last = stop;
+    } else {
+      /* in playing, use last stop time as upper bound */
+      if (start == -1 || segment->rate > 0.0)
+        last = stop;
+      else
+        last = start;
+    }
+  } else {
+    /* convert last stop to stream time */
+    last = gst_segment_to_stream_time (segment, oformat, segment->last_stop);
+  }
+
+  if (in_paused) {
+    /* in paused, use start_time */
+    base = GST_ELEMENT_START_TIME (basesink);
+    GST_DEBUG_OBJECT (basesink, "in paused, using start time %" GST_TIME_FORMAT,
+        GST_TIME_ARGS (base));
+  } else if (with_clock) {
+    /* else use clock when needed */
+    base = GST_ELEMENT_CAST (basesink)->base_time;
+    GST_DEBUG_OBJECT (basesink, "using clock and base time %" GST_TIME_FORMAT,
+        GST_TIME_ARGS (base));
+  } else {
+    /* else, no sync or clock -> no base time */
+    GST_DEBUG_OBJECT (basesink, "no sync or no clock");
+    base = -1;
+  }
 
-  /* this function might release the LOCK */
-  gst_base_sink_get_position_last (basesink, format, &last, upstream);
+  /* no base, we can't calculate running_time, use last seem timestamp to report
+   * time */
+  if (base == -1)
+    last_seen = TRUE;
 
   /* need to release the object lock before we can get the time,
    * a clock might take the LOCK of the provider, which could be
    * a basesink subclass. */
   GST_OBJECT_UNLOCK (basesink);
 
-  now = gst_clock_get_time (clock);
-
-  if (oformat != tformat) {
-    /* convert accum, time and duration to time */
-    if (!gst_pad_query_convert (basesink->sinkpad, oformat, accum, &tformat,
-            &accum))
-      goto convert_failed;
-    if (!gst_pad_query_convert (basesink->sinkpad, oformat, duration, &tformat,
-            &duration))
-      goto convert_failed;
-    if (!gst_pad_query_convert (basesink->sinkpad, oformat, time, &tformat,
-            &time))
-      goto convert_failed;
-  }
-
-  /* subtract base time and accumulated time from the clock time.
-   * Make sure we don't go negative. This is the current time in
-   * the segment which we need to scale with the combined
-   * rate and applied rate. */
-  base += accum;
-  base += latency;
-  base = MIN (now, base);
-
-  /* for negative rates we need to count back from from the segment
-   * duration. */
-  if (rate < 0.0)
-    time += duration;
-
-  *cur = time + gst_guint64_to_gdouble (now - base) * rate;
+  if (last_seen) {
+    /* in EOS or when no valid stream_time, report the value of last seen
+     * timestamp */
+    if (last == -1) {
+      /* no timestamp, we need to ask upstream */
+      GST_DEBUG_OBJECT (basesink, "no last seen timestamp, asking upstream");
+      res = FALSE;
+      *upstream = TRUE;
+      goto done;
+    }
+    GST_DEBUG_OBJECT (basesink, "using last seen timestamp %" GST_TIME_FORMAT,
+        GST_TIME_ARGS (last));
+    *cur = last;
+  } else {
+    if (oformat != tformat) {
+      /* convert accum, time and duration to time */
+      if (!gst_pad_query_convert (basesink->sinkpad, oformat, accum, &tformat,
+              &accum))
+        goto convert_failed;
+      if (!gst_pad_query_convert (basesink->sinkpad, oformat, duration,
+              &tformat, &duration))
+        goto convert_failed;
+      if (!gst_pad_query_convert (basesink->sinkpad, oformat, time, &tformat,
+              &time))
+        goto convert_failed;
+      if (!gst_pad_query_convert (basesink->sinkpad, oformat, last, &tformat,
+              &last))
+        goto convert_failed;
+
+      /* assume time format from now on */
+      oformat = tformat;
+    }
 
-  /* never report more than last seen position */
-  if (last != -1)
-    *cur = MIN (last, *cur);
+    if (!in_paused && with_clock) {
+      now = gst_clock_get_time (clock);
+    } else {
+      now = base;
+      base = 0;
+    }
 
-  gst_object_unref (clock);
+    /* subtract base time and accumulated time from the clock time.
+     * Make sure we don't go negative. This is the current time in
+     * the segment which we need to scale with the combined
+     * rate and applied rate. */
+    base += accum;
+    base += latency;
+    if (GST_CLOCK_DIFF (base, now) < 0)
+      base = now;
+
+    /* for negative rates we need to count back from the segment
+     * duration. */
+    if (rate < 0.0)
+      time += duration;
+
+    *cur = time + gst_guint64_to_gdouble (now - base) * rate;
+
+    if (in_paused) {
+      /* never report less than segment values in paused */
+      if (last != -1)
+        *cur = MAX (last, *cur);
+    } else {
+      /* never report more than last seen position in playing */
+      if (last != -1)
+        *cur = MIN (last, *cur);
+    }
 
-  GST_DEBUG_OBJECT (basesink,
-      "now %" GST_TIME_FORMAT " - base %" GST_TIME_FORMAT " - accum %"
-      GST_TIME_FORMAT " + time %" GST_TIME_FORMAT,
-      GST_TIME_ARGS (now), GST_TIME_ARGS (base),
-      GST_TIME_ARGS (accum), GST_TIME_ARGS (time));
+    GST_DEBUG_OBJECT (basesink,
+        "now %" GST_TIME_FORMAT " - base %" GST_TIME_FORMAT " - accum %"
+        GST_TIME_FORMAT " + time %" GST_TIME_FORMAT "  last %" GST_TIME_FORMAT,
+        GST_TIME_ARGS (now), GST_TIME_ARGS (base), GST_TIME_ARGS (accum),
+        GST_TIME_ARGS (time), GST_TIME_ARGS (last));
+  }
 
   if (oformat != format) {
-    /* convert time to final format */
-    if (!gst_pad_query_convert (basesink->sinkpad, tformat, *cur, &format, cur))
+    /* convert to final format */
+    if (!gst_pad_query_convert (basesink->sinkpad, oformat, *cur, &format, cur))
       goto convert_failed;
   }
 
@@ -4407,23 +4702,13 @@ gst_base_sink_get_position (GstBaseSink * basesink, GstFormat format,
 done:
   GST_DEBUG_OBJECT (basesink, "res: %d, POSITION: %" GST_TIME_FORMAT,
       res, GST_TIME_ARGS (*cur));
+
+  if (clock)
+    gst_object_unref (clock);
+
   return res;
 
   /* special cases */
-in_eos:
-  {
-    GST_DEBUG_OBJECT (basesink, "position in EOS");
-    res = gst_base_sink_get_position_last (basesink, format, cur, upstream);
-    GST_OBJECT_UNLOCK (basesink);
-    goto done;
-  }
-in_pause:
-  {
-    GST_DEBUG_OBJECT (basesink, "position in PAUSED");
-    res = gst_base_sink_get_position_paused (basesink, format, cur, upstream);
-    GST_OBJECT_UNLOCK (basesink);
-    goto done;
-  }
 wrong_state:
   {
     /* in NULL or READY we always return FALSE and -1 */
@@ -4433,28 +4718,63 @@ wrong_state:
     GST_OBJECT_UNLOCK (basesink);
     goto done;
   }
-no_sync:
-  {
-    /* report last seen timestamp if any, else ask upstream to answer */
-    if ((*cur = basesink->priv->current_sstart) != -1)
-      res = TRUE;
-    else
-      *upstream = TRUE;
-
-    GST_DEBUG_OBJECT (basesink, "no sync, res %d, POSITION %" GST_TIME_FORMAT,
-        res, GST_TIME_ARGS (*cur));
-    GST_OBJECT_UNLOCK (basesink);
-    return res;
-  }
 convert_failed:
   {
     GST_DEBUG_OBJECT (basesink, "convert failed, try upstream");
     *upstream = TRUE;
-    return FALSE;
+    res = FALSE;
+    goto done;
   }
 }
 
 static gboolean
+gst_base_sink_get_duration (GstBaseSink * basesink, GstFormat format,
+    gint64 * dur, gboolean * upstream)
+{
+  gboolean res = FALSE;
+
+  if (basesink->pad_mode == GST_ACTIVATE_PULL) {
+    GstFormat uformat = GST_FORMAT_BYTES;
+    gint64 uduration;
+
+    /* get the duration in bytes, in pull mode that's all we are sure to
+     * know. We have to explicitly get this value from upstream instead of
+     * using our cached value because it might change. Duration caching
+     * should be done at a higher level. */
+    res = gst_pad_query_peer_duration (basesink->sinkpad, &uformat, &uduration);
+    if (res) {
+      gst_segment_set_duration (&basesink->segment, uformat, uduration);
+      if (format != uformat) {
+        /* convert to the requested format */
+        res = gst_pad_query_convert (basesink->sinkpad, uformat, uduration,
+            &format, dur);
+      } else {
+        *dur = uduration;
+      }
+    }
+    *upstream = FALSE;
+  } else {
+    *upstream = TRUE;
+  }
+
+  return res;
+}
+
+static const GstQueryType *
+gst_base_sink_get_query_types (GstElement * element)
+{
+  static const GstQueryType query_types[] = {
+    GST_QUERY_DURATION,
+    GST_QUERY_POSITION,
+    GST_QUERY_SEGMENT,
+    GST_QUERY_LATENCY,
+    0
+  };
+
+  return query_types;
+}
+
+static gboolean
 gst_base_sink_query (GstElement * element, GstQuery * query)
 {
   gboolean res = FALSE;
@@ -4470,7 +4790,8 @@ gst_base_sink_query (GstElement * element, GstQuery * query)
 
       gst_query_parse_position (query, &format, NULL);
 
-      GST_DEBUG_OBJECT (basesink, "position format %d", format);
+      GST_DEBUG_OBJECT (basesink, "position query in format %s",
+          gst_format_get_name (format));
 
       /* first try to get the position based on the clock */
       if ((res =
@@ -4478,46 +4799,64 @@ gst_base_sink_query (GstElement * element, GstQuery * query)
         gst_query_set_position (query, format, cur);
       } else if (upstream) {
         /* fallback to peer query */
-        res = gst_base_sink_peer_query (basesink, query);
+        res = gst_pad_peer_query (basesink->sinkpad, query);
+      }
+      if (!res) {
+        /* we can handle a few things if upstream failed */
+        if (format == GST_FORMAT_PERCENT) {
+          gint64 dur = 0;
+          GstFormat uformat = GST_FORMAT_TIME;
+
+          res = gst_base_sink_get_position (basesink, GST_FORMAT_TIME, &cur,
+              &upstream);
+          if (!res && upstream) {
+            res = gst_pad_query_peer_position (basesink->sinkpad, &uformat,
+                &cur);
+          }
+          if (res) {
+            res = gst_base_sink_get_duration (basesink, GST_FORMAT_TIME, &dur,
+                &upstream);
+            if (!res && upstream) {
+              res = gst_pad_query_peer_duration (basesink->sinkpad, &uformat,
+                  &dur);
+            }
+          }
+          if (res) {
+            gint64 pos;
+
+            pos = gst_util_uint64_scale (100 * GST_FORMAT_PERCENT_SCALE, cur,
+                dur);
+            gst_query_set_position (query, GST_FORMAT_PERCENT, pos);
+          }
+        }
       }
       break;
     }
     case GST_QUERY_DURATION:
     {
-      GstFormat format, uformat;
-      gint64 duration, uduration;
+      gint64 dur = 0;
+      GstFormat format;
+      gboolean upstream = FALSE;
 
       gst_query_parse_duration (query, &format, NULL);
 
       GST_DEBUG_OBJECT (basesink, "duration query in format %s",
           gst_format_get_name (format));
 
-      if (basesink->pad_mode == GST_ACTIVATE_PULL) {
-        uformat = GST_FORMAT_BYTES;
-
-        /* get the duration in bytes, in pull mode that's all we are sure to
-         * know. We have to explicitly get this value from upstream instead of
-         * using our cached value because it might change. Duration caching
-         * should be done at a higher level. */
-        res = gst_pad_query_peer_duration (basesink->sinkpad, &uformat,
-            &uduration);
-        if (res) {
-          gst_segment_set_duration (&basesink->segment, uformat, uduration);
-          if (format != uformat) {
-            /* convert to the requested format */
-            res = gst_pad_query_convert (basesink->sinkpad, uformat, uduration,
-                &format, &duration);
-          } else {
-            duration = uduration;
-          }
-          if (res) {
-            /* set the result */
-            gst_query_set_duration (query, format, duration);
-          }
+      if ((res =
+              gst_base_sink_get_duration (basesink, format, &dur, &upstream))) {
+        gst_query_set_duration (query, format, dur);
+      } else if (upstream) {
+        /* fallback to peer query */
+        res = gst_pad_peer_query (basesink->sinkpad, query);
+      }
+      if (!res) {
+        /* we can handle a few things if upstream failed */
+        if (format == GST_FORMAT_PERCENT) {
+          gst_query_set_duration (query, GST_FORMAT_PERCENT,
+              GST_FORMAT_PERCENT_MAX);
+          res = TRUE;
         }
-      } else {
-        /* in push mode we simply forward upstream */
-        res = gst_base_sink_peer_query (basesink, query);
       }
       break;
     }
@@ -4540,18 +4879,24 @@ gst_base_sink_query (GstElement * element, GstQuery * query)
       break;
     case GST_QUERY_SEGMENT:
     {
-      /* FIXME, bring start/stop to stream time */
-      gst_query_set_segment (query, basesink->segment.rate,
-          GST_FORMAT_TIME, basesink->segment.start, basesink->segment.stop);
+      if (basesink->pad_mode == GST_ACTIVATE_PULL) {
+        gst_query_set_segment (query, basesink->segment.rate,
+            GST_FORMAT_TIME, basesink->segment.start, basesink->segment.stop);
+        res = TRUE;
+      } else {
+        res = gst_pad_peer_query (basesink->sinkpad, query);
+      }
       break;
     }
     case GST_QUERY_SEEKING:
     case GST_QUERY_CONVERT:
     case GST_QUERY_FORMATS:
     default:
-      res = gst_base_sink_peer_query (basesink, query);
+      res = gst_pad_peer_query (basesink->sinkpad, query);
       break;
   }
+  GST_DEBUG_OBJECT (basesink, "query %s returns %d",
+      GST_QUERY_TYPE_NAME (query), res);
   return res;
 }
 
@@ -4576,12 +4921,11 @@ gst_base_sink_change_state (GstElement * element, GstStateChange transition)
     case GST_STATE_CHANGE_READY_TO_PAUSED:
       /* need to complete preroll before this state change completes, there
        * is no data flow in READY so we can safely assume we need to preroll. */
-      GST_PAD_PREROLL_LOCK (basesink->sinkpad);
+      GST_BASE_SINK_PREROLL_LOCK (basesink);
       GST_DEBUG_OBJECT (basesink, "READY to PAUSED");
       basesink->have_newsegment = FALSE;
       gst_segment_init (&basesink->segment, GST_FORMAT_UNDEFINED);
-      gst_segment_init (basesink->abidata.ABI.clip_segment,
-          GST_FORMAT_UNDEFINED);
+      gst_segment_init (basesink->clip_segment, GST_FORMAT_UNDEFINED);
       basesink->offset = 0;
       basesink->have_preroll = FALSE;
       priv->step_unlock = FALSE;
@@ -4608,10 +4952,10 @@ gst_base_sink_change_state (GstElement * element, GstStateChange transition)
       } else {
         priv->have_latency = TRUE;
       }
-      GST_PAD_PREROLL_UNLOCK (basesink->sinkpad);
+      GST_BASE_SINK_PREROLL_UNLOCK (basesink);
       break;
     case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
-      GST_PAD_PREROLL_LOCK (basesink->sinkpad);
+      GST_BASE_SINK_PREROLL_LOCK (basesink);
       if (!gst_base_sink_needs_preroll (basesink)) {
         GST_DEBUG_OBJECT (basesink, "PAUSED to PLAYING, don't need preroll");
         /* no preroll needed anymore now. */
@@ -4627,7 +4971,7 @@ gst_base_sink_change_state (GstElement * element, GstStateChange transition)
           gst_element_post_message (GST_ELEMENT_CAST (basesink), message);
         } else {
           GST_DEBUG_OBJECT (basesink, "signal preroll");
-          GST_PAD_PREROLL_SIGNAL (basesink->sinkpad);
+          GST_BASE_SINK_PREROLL_SIGNAL (basesink);
         }
       } else {
         GST_DEBUG_OBJECT (basesink, "PAUSED to PLAYING, we are not prerolled");
@@ -4642,7 +4986,7 @@ gst_base_sink_change_state (GstElement * element, GstStateChange transition)
               gst_message_new_async_start (GST_OBJECT_CAST (basesink), FALSE));
         }
       }
-      GST_PAD_PREROLL_UNLOCK (basesink->sinkpad);
+      GST_BASE_SINK_PREROLL_UNLOCK (basesink);
       break;
     default:
       break;
@@ -4666,7 +5010,8 @@ gst_base_sink_change_state (GstElement * element, GstStateChange transition)
       if (bclass->unlock)
         bclass->unlock (basesink);
 
-      GST_PAD_PREROLL_LOCK (basesink->sinkpad);
+      GST_BASE_SINK_PREROLL_LOCK (basesink);
+      GST_DEBUG_OBJECT (basesink, "got preroll lock");
       /* now that we have the PREROLL lock, clear our unlock request */
       if (bclass->unlock_stop)
         bclass->unlock_stop (basesink);
@@ -4677,6 +5022,7 @@ gst_base_sink_change_state (GstElement * element, GstStateChange transition)
       basesink->need_preroll = TRUE;
 
       if (basesink->clock_id) {
+        GST_DEBUG_OBJECT (basesink, "unschedule clock");
         gst_clock_id_unschedule (basesink->clock_id);
       }
 
@@ -4687,6 +5033,7 @@ gst_base_sink_change_state (GstElement * element, GstStateChange transition)
         basesink->playing_async = FALSE;
       } else {
         if (GST_STATE_TARGET (GST_ELEMENT (basesink)) <= GST_STATE_READY) {
+          GST_DEBUG_OBJECT (basesink, "element is <= READY");
           ret = GST_STATE_CHANGE_SUCCESS;
         } else {
           GST_DEBUG_OBJECT (basesink,
@@ -4707,10 +5054,10 @@ gst_base_sink_change_state (GstElement * element, GstStateChange transition)
           ", dropped: %" G_GUINT64_FORMAT, priv->rendered, priv->dropped);
 
       gst_base_sink_reset_qos (basesink);
-      GST_PAD_PREROLL_UNLOCK (basesink->sinkpad);
+      GST_BASE_SINK_PREROLL_UNLOCK (basesink);
       break;
     case GST_STATE_CHANGE_PAUSED_TO_READY:
-      GST_PAD_PREROLL_LOCK (basesink->sinkpad);
+      GST_BASE_SINK_PREROLL_LOCK (basesink);
       /* start by reseting our position state with the object lock so that the
        * position query gets the right idea. We do this before we post the
        * messages so that the message handlers pick this up. */
@@ -4719,6 +5066,10 @@ gst_base_sink_change_state (GstElement * element, GstStateChange transition)
       priv->current_sstart = GST_CLOCK_TIME_NONE;
       priv->current_sstop = GST_CLOCK_TIME_NONE;
       priv->have_latency = FALSE;
+      if (priv->cached_clock_id) {
+        gst_clock_id_unref (priv->cached_clock_id);
+        priv->cached_clock_id = NULL;
+      }
       GST_OBJECT_UNLOCK (basesink);
 
       gst_base_sink_set_last_buffer (basesink, NULL);
@@ -4739,7 +5090,7 @@ gst_base_sink_change_state (GstElement * element, GstStateChange transition)
       } else {
         GST_DEBUG_OBJECT (basesink, "PAUSED to READY, don't need_preroll");
       }
-      GST_PAD_PREROLL_UNLOCK (basesink->sinkpad);
+      GST_BASE_SINK_PREROLL_UNLOCK (basesink);
       break;
     case GST_STATE_CHANGE_READY_TO_NULL:
       if (bclass->stop) {