audiorate: Update next_offset per rate change
authorSeungha Yang <seungha.yang@navercorp.com>
Thu, 7 Nov 2019 13:00:03 +0000 (22:00 +0900)
committerTim-Philipp Müller <tim@centricular.com>
Mon, 18 Nov 2019 12:02:18 +0000 (12:02 +0000)
To support runtime audio samplerate change, re-calculate next target offset
per caps. Calculating the next buffer offset using the previous
offset seems to be tricky and rounding error prone.

Fixes: https://gitlab.freedesktop.org/gstreamer/gst-plugins-base/issues/693
gst/audiorate/gstaudiorate.c

index eceeed2adcd2ebd5ab1fb5024434b7e1a39d1500..30a5d6ffd21d2f62b3e0c2dcfc837b4abf5eafb8 100644 (file)
@@ -216,12 +216,24 @@ static gboolean
 gst_audio_rate_setcaps (GstAudioRate * audiorate, GstCaps * caps)
 {
   GstAudioInfo info;
+  gint prev_rate = 0;
 
   if (!gst_audio_info_from_caps (&info, caps))
     goto wrong_caps;
 
+  prev_rate = audiorate->info.rate;
   audiorate->info = info;
 
+  if (audiorate->next_offset >= 0 && prev_rate > 0 && prev_rate != info.rate) {
+    GST_DEBUG_OBJECT (audiorate,
+        "rate changed from %d to %d", prev_rate, info.rate);
+
+    /* calculate next_offset based on new rate value */
+    audiorate->next_offset =
+        gst_util_uint64_scale_int_round (audiorate->next_ts,
+        info.rate, GST_SECOND);
+  }
+
   return TRUE;
 
   /* ERRORS */