audiobasesink: Reset audio clock if necessary
authorArun Raghavan <arun@centricular.com>
Tue, 3 Mar 2015 17:26:37 +0000 (22:56 +0530)
committerArun Raghavan <arun@accosted.net>
Tue, 3 Mar 2015 17:56:54 +0000 (23:26 +0530)
When the ringbuffer is deactivated and then acquired, if the audio clock
provided by the sink gets reset to zero, we need to add an offset to the
clock to make sure that subsequent samples are written out at the right
times. While we need to leave this to derived classes to take care of
when they provide their own clock (since that clock may or may not be
reset to zero), we can do this ourselves if we know the provided clock
is our own (which does reset to zero on a re-acquire).

gst-libs/gst/audio/gstaudiobasesink.c
gst-libs/gst/audio/gstaudiosink.c

index ece99d4..233efcb 100644 (file)
@@ -381,6 +381,14 @@ clock_disabled:
 }
 
 static gboolean
+gst_audio_base_sink_is_self_provided_clock (GstAudioBaseSink * sink)
+{
+  return (sink->provided_clock && GST_IS_AUDIO_CLOCK (sink->provided_clock) &&
+      GST_AUDIO_CLOCK_CAST (sink->provided_clock)->func ==
+      (GstAudioClockGetTimeFunc) gst_audio_base_sink_get_time);
+}
+
+static gboolean
 gst_audio_base_sink_query_pad (GstBaseSink * bsink, GstQuery * query)
 {
   gboolean res = FALSE;
@@ -887,6 +895,11 @@ gst_audio_base_sink_setcaps (GstBaseSink * bsink, GstCaps * caps)
   if (!gst_audio_ring_buffer_acquire (sink->ringbuffer, spec))
     goto acquire_error;
 
+  /* If we use our own clock, we need to adjust the offset since it will now
+   * restart from zero */
+  if (gst_audio_base_sink_is_self_provided_clock (sink))
+    gst_audio_clock_reset (GST_AUDIO_CLOCK (sink->provided_clock), 0);
+
   /* We need to resync since the ringbuffer restarted */
   gst_audio_base_sink_reset_sync (sink);
 
@@ -2224,9 +2237,7 @@ gst_audio_base_sink_change_state (GstElement * element,
       /* Only post clock-provide messages if this is the clock that
        * we've created. If the subclass has overriden it the subclass
        * should post this messages whenever necessary */
-      if (sink->provided_clock && GST_IS_AUDIO_CLOCK (sink->provided_clock) &&
-          GST_AUDIO_CLOCK_CAST (sink->provided_clock)->func ==
-          (GstAudioClockGetTimeFunc) gst_audio_base_sink_get_time)
+      if (gst_audio_base_sink_is_self_provided_clock (sink))
         gst_element_post_message (element,
             gst_message_new_clock_provide (GST_OBJECT_CAST (element),
                 sink->provided_clock, TRUE));
@@ -2264,9 +2275,7 @@ gst_audio_base_sink_change_state (GstElement * element,
       /* Only post clock-lost messages if this is the clock that
        * we've created. If the subclass has overriden it the subclass
        * should post this messages whenever necessary */
-      if (sink->provided_clock && GST_IS_AUDIO_CLOCK (sink->provided_clock) &&
-          GST_AUDIO_CLOCK_CAST (sink->provided_clock)->func ==
-          (GstAudioClockGetTimeFunc) gst_audio_base_sink_get_time)
+      if (gst_audio_base_sink_is_self_provided_clock (sink))
         gst_element_post_message (element,
             gst_message_new_clock_lost (GST_OBJECT_CAST (element),
                 sink->provided_clock));
index f18b1c7..0604f59 100644 (file)
@@ -399,7 +399,6 @@ gst_audio_sink_ring_buffer_acquire (GstAudioRingBuffer * buf,
   GstAudioSink *sink;
   GstAudioSinkClass *csink;
   gboolean result = FALSE;
-  GstAudioClock *clock;
 
   sink = GST_AUDIO_SINK (GST_OBJECT_PARENT (buf));
   csink = GST_AUDIO_SINK_GET_CLASS (sink);
@@ -409,10 +408,6 @@ gst_audio_sink_ring_buffer_acquire (GstAudioRingBuffer * buf,
   if (!result)
     goto could_not_prepare;
 
-  /* our clock will now start from 0 again */
-  clock = GST_AUDIO_CLOCK (GST_AUDIO_BASE_SINK (sink)->provided_clock);
-  gst_audio_clock_reset (clock, 0);
-
   /* set latency to one more segment as we need some headroom */
   spec->seglatency = spec->segtotal + 1;