[vlc-devel] [PATCH] coreaudio: fix calculation when the clock sources are different

Thomas Guillem thomas at gllm.fr
Mon Mar 30 14:40:51 CEST 2020


As MONOTONIC is defined by POSIX to be the one that ticks during sleep while
mach absolute time does not.

For now (and for 3.0) vlc_tick_now() has the same source than the mach one.
---
 modules/audio_output/coreaudio_common.c | 18 ++++++++++++------
 1 file changed, 12 insertions(+), 6 deletions(-)

diff --git a/modules/audio_output/coreaudio_common.c b/modules/audio_output/coreaudio_common.c
index 900616a8aa..5f8a633bee 100644
--- a/modules/audio_output/coreaudio_common.c
+++ b/modules/audio_output/coreaudio_common.c
@@ -243,9 +243,10 @@ ca_TimeGet(audio_output_t *p_aout, vlc_tick_t *delay)
         return -1;
     }
 
-    const vlc_tick_t i_render_time_us =
-        HostTimeToTick(p_sys, p_sys->i_render_host_time);
-    const vlc_tick_t i_render_delay = i_render_time_us - vlc_tick_now();
+    uint64_t i_render_delay_host_time = p_sys->i_render_host_time
+                                      - mach_absolute_time();
+    const vlc_tick_t i_render_delay =
+        HostTimeToTick(p_sys, i_render_delay_host_time);
 
     *delay = ca_GetLatencyLocked(p_aout) + i_render_delay;
     lock_unlock(p_sys);
@@ -307,9 +308,14 @@ ca_Play(audio_output_t * p_aout, block_t * p_block, vlc_tick_t date)
          * first (non-silence/zero) frame is rendered by the render callback.
          * Once the rendering is truly started, the date can be ignored. */
 
-        const vlc_tick_t first_render_time = date - ca_GetLatencyLocked(p_aout);
-        p_sys->i_first_render_host_time =
-            TickToHostTime(p_sys, first_render_time);
+        /* We can't convert date to host time directly since the clock source
+         * may be different (MONOTONIC vs continue during sleep). The solution
+         * is to convert it to a relative time and then add it to
+         * mach_absolute_time() */
+        const vlc_tick_t first_render_delay = date - vlc_tick_now()
+                                            - ca_GetLatencyLocked(p_aout);
+        p_sys->i_first_render_host_time
+            = mach_absolute_time() + TickToHostTime(p_sys, first_render_delay);
     }
 
     do
-- 
2.20.1



More information about the vlc-devel mailing list