From bffc0348665fe06d7747f676c5af5c1b7f9861b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Sun, 9 Aug 2026 18:09:23 +0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fix(audio):=20stop=20the=20pause?= =?UTF-8?q?=20leaking=20into=20the=20playback=20clock?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The graph clock keeps ticking while our stream is inactive, so the first process callback after a resume reported a position a whole pause ahead of what the device had played -- and the master clock jumped by exactly that much. Measured on test_av.mp4: a 300 ms pause moved the timeline +0.32 s, a 1000 ms pause +1.045 s, linear, with the audio itself carrying on from where it stopped. Video then raced to catch a clock the ear could not follow. Fold the inactive stretch into a skew instead of the position, so ao_position keeps meaning "samples actually played" across a pause -- the comparison `played_out` already assumed. This is what tests/playback.rs:88 audio_video_plays_at_real_speed has been red on: a true positive, not a load-sensitive margin. It fails 2/3 at --test-threads=1 as well; whether it lands under the 0.7 s bound was decided by how much the audio clock lagged in the first 2 s (1.79 s -> pass, 1.98 s -> fail), which offsets the constant jump. --- crates/engine-audio/src/lib.rs | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/crates/engine-audio/src/lib.rs b/crates/engine-audio/src/lib.rs index 779d2ca..5b20979 100644 --- a/crates/engine-audio/src/lib.rs +++ b/crates/engine-audio/src/lib.rs @@ -128,6 +128,10 @@ struct Shared { dead: AtomicBool, /// Asked for by `ao_flush`, honoured by the next process callback. flush: AtomicBool, + /// Set by `ao_set_active(true)`, cleared by the next process callback: the + /// graph clock keeps ticking while the stream is inactive, and that time + /// was never played. + resumed: AtomicBool, underruns: AtomicU64, } @@ -191,6 +195,9 @@ fn run( .process({ let shared = shared.clone(); let stride = 4 * channels; + // Graph ticks that elapsed while the stream was inactive, and the + // last raw reading they are measured against. + let (mut skew, mut last_raw) = (0i64, 0i64); move |stream, ()| { let Some(mut buffer) = stream.dequeue_buffer() else { return; @@ -234,7 +241,17 @@ fn run( } else { ticks }; - shared.position.store(played.max(0), Ordering::Relaxed); + // The graph clock runs whether or not this stream does, so a + // pause shows up here as a step. Nothing was played during + // it: fold it into the skew instead of the position, or the + // pause leaks into the caller's timeline. + if shared.resumed.swap(false, Ordering::Relaxed) { + skew += played - last_raw; + } + last_raw = played; + shared + .position + .store((played - skew).max(0), Ordering::Relaxed); } } }) @@ -352,6 +369,7 @@ pub extern "C" fn ao_open(sample_rate: u32, channels: u32) -> *mut c_void { ready: AtomicBool::new(false), dead: AtomicBool::new(false), flush: AtomicBool::new(false), + resumed: AtomicBool::new(false), underruns: AtomicU64::new(0), }); let (ready_tx, ready_rx) = mpsc::channel(); @@ -435,6 +453,9 @@ pub unsafe extern "C" fn ao_set_active(session: *mut c_void, active: u32) -> i32 } // SAFETY: caller-guaranteed live session. let session = unsafe { &*(session as *const Session) }; + if active != 0 { + session.shared.resumed.store(true, Ordering::Relaxed); + } match session.ctl.send(Ctl::Active(active != 0)) { Ok(()) => 0, Err(_) => -1,