[vlc-devel] [PATCH] pulse: calculate the start time from the last date played

Thomas Guillem thomas at gllm.fr
Mon Jul 2 16:31:59 CEST 2018


If I understand correctly, stream_latency_cb() is needed to re-calculate the
new derrered time (or start late in the worst case scenario) when the audio
device latency changes.

This callback called stream_start() with the first played system date. The
problem is that the delta calculated from stream_start was always late. Using
the last system date seems more logical. Indeed, the delta need to be
calculated from values using the same reference point (and vlc_pa_get_latency()
is calculated from the amount of buffers played).

This issue can be reproduced when the first played date is more than 100ms in
the future.
---
 modules/audio_output/pulse.c | 22 +++++++++-------------
 1 file changed, 9 insertions(+), 13 deletions(-)

diff --git a/modules/audio_output/pulse.c b/modules/audio_output/pulse.c
index f999eb534b..a275c9f5e6 100644
--- a/modules/audio_output/pulse.c
+++ b/modules/audio_output/pulse.c
@@ -68,8 +68,7 @@ typedef struct
     pa_threaded_mainloop *mainloop; /**< PulseAudio thread */
     pa_time_event *trigger; /**< Deferred stream trigger */
     pa_cvolume cvolume; /**< actual sink input volume */
-    vlc_tick_t first_pts; /**< Play stream timestamp of buffer start */
-    vlc_tick_t first_date; /**< Play system timestamp of buffer start */
+    vlc_tick_t last_date; /**< Play system timestamp of last buffer */
 
     pa_volume_t volume_force; /**< Forced volume (stream must be NULL) */
     pa_stream_flags_t flags_force; /**< Forced flags (stream must be NULL) */
@@ -227,7 +226,7 @@ static void stream_start(pa_stream *s, audio_output_t *aout, vlc_tick_t date)
     aout_sys_t *sys = aout->sys;
     vlc_tick_t delta;
 
-    assert (sys->first_pts != VLC_TS_INVALID);
+    assert (sys->last_date != VLC_TS_INVALID);
 
     if (sys->trigger != NULL) {
         vlc_pa_rttime_free(sys->mainloop, sys->trigger);
@@ -258,10 +257,10 @@ static void stream_latency_cb(pa_stream *s, void *userdata)
     aout_sys_t *sys = aout->sys;
 
     /* This callback is _never_ called while paused. */
-    if (sys->first_pts == VLC_TS_INVALID)
+    if (sys->last_date == VLC_TS_INVALID)
         return; /* nothing to do if buffers are (still) empty */
     if (pa_stream_is_corked(s) > 0)
-        stream_start(s, aout, sys->first_date);
+        stream_start(s, aout, sys->last_date);
 }
 
 
@@ -331,7 +330,7 @@ static void stream_overflow_cb(pa_stream *s, void *userdata)
     if (unlikely(op == NULL))
         return;
     pa_operation_unref(op);
-    sys->first_pts = VLC_TS_INVALID;
+    sys->last_date = VLC_TS_INVALID;
 }
 
 static void stream_started_cb(pa_stream *s, void *userdata)
@@ -504,10 +503,7 @@ static void Play(audio_output_t *aout, block_t *block, vlc_tick_t date)
      * will take place, and sooner or later a deadlock. */
     pa_threaded_mainloop_lock(sys->mainloop);
 
-    if (sys->first_pts == VLC_TS_INVALID) {
-        sys->first_pts = block->i_pts;
-        sys->first_date = date;
-    }
+    sys->last_date = date;
 
     if (pa_stream_is_corked(s) > 0)
         stream_start(s, aout, date);
@@ -543,7 +539,7 @@ static void Pause(audio_output_t *aout, bool paused, vlc_tick_t date)
         stream_stop(s, aout);
     } else {
         pa_stream_set_latency_update_callback(s, stream_latency_cb, aout);
-        if (likely(sys->first_pts != VLC_TS_INVALID))
+        if (likely(sys->last_date != VLC_TS_INVALID))
             stream_start_now(s, aout);
     }
 
@@ -576,7 +572,7 @@ static void Flush(audio_output_t *aout, bool wait)
         op = pa_stream_flush(s, NULL, NULL);
     if (op != NULL)
         pa_operation_unref(op);
-    sys->first_pts = VLC_TS_INVALID;
+    sys->last_date = VLC_TS_INVALID;
     stream_stop(s, aout);
 
     pa_threaded_mainloop_unlock(sys->mainloop);
@@ -807,7 +803,7 @@ static int Start(audio_output_t *aout, audio_sample_format_t *restrict fmt)
 
     sys->trigger = NULL;
     pa_cvolume_init(&sys->cvolume);
-    sys->first_pts = VLC_TS_INVALID;
+    sys->last_date = VLC_TS_INVALID;
 
     pa_format_info *formatv = pa_format_info_new();
     formatv->encoding = encoding;
-- 
2.18.0



More information about the vlc-devel mailing list