basesink: Don't forget to unref the cached ClockId
[platform/upstream/gstreamer.git] / libs / gst / base / gstbasesink.c
index 13e4944..7a3fb74 100644 (file)
@@ -236,7 +236,7 @@ struct _GstBaseSinkPrivate
   gboolean have_latency;
 
   /* the last buffer we prerolled or rendered. Useful for making snapshots */
-  gboolean enable_last_buffer;
+  gint enable_last_buffer;      /* atomic */
   GstBuffer *last_buffer;
 
   /* caps for pull based scheduling */
@@ -256,6 +256,9 @@ struct _GstBaseSinkPrivate
   /* we have a pending and a current step operation */
   GstStepInfo current_step;
   GstStepInfo pending_step;
+
+  /* Cached GstClockID */
+  GstClockID cached_clock_id;
 };
 
 #define DO_RUNNING_AVG(avg,val,size) (((val) + ((size)-1) * (avg)) / (size))
@@ -269,6 +272,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 */
@@ -340,6 +356,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);
@@ -382,7 +399,7 @@ static gboolean gst_base_sink_is_too_late (GstBaseSink * basesink,
     GstMiniObject * obj, GstClockTime start, GstClockTime stop,
     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)
@@ -511,6 +528,8 @@ gst_base_sink_class_init (GstBaseSinkClass * klass)
       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);
@@ -667,7 +686,8 @@ gst_base_sink_init (GstBaseSink * basesink, gpointer g_class)
   priv->ts_offset = DEFAULT_TS_OFFSET;
   priv->render_delay = DEFAULT_RENDER_DELAY;
   priv->blocksize = DEFAULT_BLOCKSIZE;
-  priv->enable_last_buffer = DEFAULT_ENABLE_LAST_BUFFER;
+  priv->cached_clock_id = NULL;
+  g_atomic_int_set (&priv->enable_last_buffer, DEFAULT_ENABLE_LAST_BUFFER);
 
   GST_OBJECT_FLAG_SET (basesink, GST_ELEMENT_IS_SINK);
 }
@@ -840,7 +860,7 @@ 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;
+  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);
 }
@@ -864,9 +884,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;
 }
@@ -975,13 +993,11 @@ gst_base_sink_set_last_buffer_unlocked (GstBaseSink * sink, GstBuffer * buffer)
 static void
 gst_base_sink_set_last_buffer (GstBaseSink * sink, GstBuffer * buffer)
 {
-  GST_OBJECT_LOCK (sink);
-  if (sink->priv->enable_last_buffer == FALSE)
-    goto out;
+  if (!g_atomic_int_get (&sink->priv->enable_last_buffer))
+    return;
 
+  GST_OBJECT_LOCK (sink);
   gst_base_sink_set_last_buffer_unlocked (sink, buffer);
-
-out:
   GST_OBJECT_UNLOCK (sink);
 }
 
@@ -1000,13 +1016,13 @@ gst_base_sink_set_last_buffer_enabled (GstBaseSink * sink, gboolean enabled)
 {
   g_return_if_fail (GST_IS_BASE_SINK (sink));
 
-  GST_OBJECT_LOCK (sink);
-  if (enabled != sink->priv->enable_last_buffer) {
-    sink->priv->enable_last_buffer = enabled;
-    if (!enabled)
-      gst_base_sink_set_last_buffer_unlocked (sink, NULL);
+  /* 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_OBJECT_UNLOCK (sink);
 }
 
 /**
@@ -1023,15 +1039,9 @@ gst_base_sink_set_last_buffer_enabled (GstBaseSink * sink, gboolean enabled)
 gboolean
 gst_base_sink_is_last_buffer_enabled (GstBaseSink * sink)
 {
-  gboolean res;
-
   g_return_val_if_fail (GST_IS_BASE_SINK (sink), FALSE);
 
-  GST_OBJECT_LOCK (sink);
-  res = sink->priv->enable_last_buffer;
-  GST_OBJECT_UNLOCK (sink);
-
-  return res;
+  return g_atomic_int_get (&sink->priv->enable_last_buffer);
 }
 
 /**
@@ -1278,7 +1288,7 @@ gst_base_sink_set_property (GObject * object, guint 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);
+      g_atomic_int_set (&sink->preroll_queue_max_len, g_value_get_uint (value));
       GST_PAD_PREROLL_UNLOCK (sink->sinkpad);
       break;
     case PROP_SYNC:
@@ -1319,9 +1329,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));
@@ -1828,7 +1836,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;
@@ -1845,7 +1853,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)) {
@@ -1890,6 +1898,7 @@ gst_base_sink_get_sync_times (GstBaseSink * basesink, GstMiniObject * obj,
 
   eos = FALSE;
 
+again:
   /* else do buffer sync code */
   buffer = GST_BUFFER_CAST (obj);
 
@@ -1979,6 +1988,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 */
@@ -2002,7 +2017,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)
 {
@@ -2025,6 +2041,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;
 }
 
@@ -2058,7 +2080,6 @@ GstClockReturn
 gst_base_sink_wait_clock (GstBaseSink * sink, GstClockTime time,
     GstClockTimeDiff * jitter)
 {
-  GstClockID id;
   GstClockReturn ret;
   GstClock *clock;
   GstClockTime base_time;
@@ -2081,21 +2102,28 @@ gst_base_sink_wait_clock (GstBaseSink * sink, GstClockTime time,
   /* add base_time to running_time to get the time against the clock */
   time += base_time;
 
-  id = gst_clock_new_single_shot_id (clock, 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);
 
-  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);
   sink->clock_id = NULL;
 
   return ret;
@@ -2194,9 +2222,17 @@ gst_base_sink_do_preroll (GstBaseSink * sink, GstMiniObject * obj)
   GstFlowReturn ret;
 
   while (G_UNLIKELY (sink->need_preroll)) {
+    guint8 obj_type = _PR_IS_NOTHING;
     GST_DEBUG_OBJECT (sink, "prerolling object %p", obj);
 
-    ret = gst_base_sink_preroll_object (sink, FALSE, obj);
+    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;
+
+    ret = gst_base_sink_preroll_object (sink, obj_type, obj);
     if (ret != GST_FLOW_OK)
       goto preroll_failed;
 
@@ -2252,7 +2288,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;
@@ -2320,7 +2356,7 @@ 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 = 0;
   gboolean syncable;
@@ -2348,7 +2384,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;
@@ -2410,8 +2446,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, jitter %" GST_TIME_FORMAT,
-      status, GST_TIME_ARGS (jitter));
+  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)
@@ -2756,7 +2792,7 @@ 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;
@@ -2766,7 +2802,7 @@ gst_base_sink_render_object (GstBaseSink * basesink, GstPad * pad,
 
   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
@@ -2783,20 +2819,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 */
@@ -2808,7 +2846,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
@@ -2837,7 +2875,7 @@ again:
 
       priv->rendered++;
     }
-  } else if (G_LIKELY (GST_IS_EVENT (obj))) {
+  } else if (G_LIKELY (OBJ_IS_EVENT (obj_type))) {
     GstEvent *event = GST_EVENT_CAST (obj);
     gboolean event_res = TRUE;
     GstEventType type;
@@ -2976,7 +3014,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;
@@ -2984,12 +3022,12 @@ 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) {
+    if (OBJ_IS_BUFFERLIST (obj_type)) {
       buf = gst_buffer_list_get (GST_BUFFER_LIST_CAST (obj), 0, 0);
       g_assert (NULL != buf);
     } else {
@@ -3006,7 +3044,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);
     }
 
@@ -3050,7 +3088,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;
@@ -3066,7 +3104,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;
     }
@@ -3089,13 +3127,15 @@ gst_base_sink_queue_object_unlocked (GstBaseSink * basesink, GstPad * pad,
     GST_DEBUG_OBJECT (basesink, "rendering queued object %p", 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,
+        GST_IS_BUFFER (o) ? _PR_IS_BUFFER : _PR_IS_EVENT, 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;
@@ -3131,6 +3171,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,
@@ -3146,7 +3188,7 @@ 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);
 
@@ -3189,7 +3231,6 @@ gst_base_sink_flush_start (GstBaseSink * basesink, GstPad * pad)
     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);
@@ -3230,8 +3271,8 @@ gst_base_sink_event (GstPad * pad, GstEvent * event)
 
   bclass = GST_BASE_SINK_GET_CLASS (basesink);
 
-  GST_DEBUG_OBJECT (basesink, "event %p (%s)", event,
-      GST_EVENT_TYPE_NAME (event));
+  GST_DEBUG_OBJECT (basesink, "reveived event %p %" GST_PTR_FORMAT, event,
+      event);
 
   switch (GST_EVENT_TYPE (event)) {
     case GST_EVENT_EOS:
@@ -3254,7 +3295,7 @@ 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;
       }
@@ -3288,7 +3329,7 @@ gst_base_sink_event (GstPad * pad, GstEvent * event)
 
         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 {
@@ -3399,7 +3440,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;
@@ -3413,7 +3454,7 @@ gst_base_sink_chain_unlocked (GstBaseSink * basesink, GstPad * pad,
   if (G_UNLIKELY (basesink->priv->received_eos))
     goto was_eos;
 
-  if (is_list) {
+  if (OBJ_IS_BUFFERLIST (obj_type)) {
     time_buf = gst_buffer_list_get (GST_BUFFER_LIST_CAST (obj), 0, 0);
     g_assert (NULL != time_buf);
   } else {
@@ -3470,7 +3511,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 */
@@ -3499,7 +3540,7 @@ 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;
 
@@ -3507,7 +3548,7 @@ gst_base_sink_chain_main (GstBaseSink * basesink, GstPad * pad,
     goto wrong_mode;
 
   GST_PAD_PREROLL_LOCK (pad);
-  result = gst_base_sink_chain_unlocked (basesink, pad, is_list, obj);
+  result = gst_base_sink_chain_unlocked (basesink, pad, obj_type, obj);
   GST_PAD_PREROLL_UNLOCK (pad);
 
 done:
@@ -3536,7 +3577,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
@@ -3550,7 +3591,7 @@ 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;
@@ -3568,13 +3609,14 @@ gst_base_sink_chain_list (GstPad * pad, GstBufferList * list)
         } else {
           GST_CAT_INFO_OBJECT (GST_CAT_SCHEDULING, pad, "chaining group");
         }
-        result = gst_base_sink_chain_main (basesink, pad, FALSE, group);
+        result = gst_base_sink_chain_main (basesink, pad, _PR_IS_BUFFER, 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 ());
+          gst_base_sink_chain_main (basesink, pad, _PR_IS_BUFFER,
+          gst_buffer_new ());
     }
     gst_buffer_list_iterator_free (it);
     gst_buffer_list_unref (list);
@@ -3917,7 +3959,7 @@ gst_base_sink_loop (GstPad * pad)
   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);
+  result = gst_base_sink_chain_unlocked (basesink, pad, _PR_IS_BUFFER, buf);
   GST_PAD_PREROLL_UNLOCK (pad);
   if (G_UNLIKELY (result != GST_FLOW_OK))
     goto paused;
@@ -3930,25 +3972,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;
   }
@@ -4277,6 +4320,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:
     {
@@ -4323,231 +4369,203 @@ gst_base_sink_send_event (GstElement * element, GstEvent * event)
   return result;
 }
 
-/* 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;
+  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 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;
+
+  /* our intermediate time format */
+  tformat = GST_FORMAT_TIME;
+  /* get the format in the segment */
+  oformat = segment->format;
 
-  /* we need to sync on the clock. */
+  /* 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;
 
-  /* this function might release the LOCK */
-  gst_base_sink_get_position_last (basesink, format, &last, upstream);
+    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;
+  }
+
+  /* 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;
-  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 (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;
   }
 
@@ -4556,23 +4574,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 */
@@ -4582,24 +4590,12 @@ 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;
   }
 }
 
@@ -4636,6 +4632,20 @@ gst_base_sink_get_duration (GstBaseSink * basesink, GstFormat format,
   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)
 {
@@ -4741,10 +4751,13 @@ 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);
-      res = TRUE;
+      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:
@@ -4926,6 +4939,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);