forge
Explore
Log in

Don't you dare go Hollow

Stop the pause leaking into the playback clock

Browse files at this commit

bffc0348665fe06d7747f676c5af5c1b7f9861b6Ahmet Kaan GümüşParent: aae5734
  • fix
    • stop the pause leaking into the playback clock
Show full message
🐛 fix(audio): stop the pause leaking into the playback clock

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.rsmodified+22 -1
@@ -128,6 +128,10 @@ struct Shared {
128 dead: AtomicBool,128 dead: AtomicBool,
129 /// Asked for by `ao_flush`, honoured by the next process callback.129 /// Asked for by `ao_flush`, honoured by the next process callback.
130 flush: AtomicBool,130 flush: AtomicBool,
131+ /// Set by `ao_set_active(true)`, cleared by the next process callback: the
132+ /// graph clock keeps ticking while the stream is inactive, and that time
133+ /// was never played.
134+ resumed: AtomicBool,
131 underruns: AtomicU64,135 underruns: AtomicU64,
132 }136 }
133 137
@@ -191,6 +195,9 @@ fn run(
191 .process({195 .process({
192 let shared = shared.clone();196 let shared = shared.clone();
193 let stride = 4 * channels;197 let stride = 4 * channels;
198+ // Graph ticks that elapsed while the stream was inactive, and the
199+ // last raw reading they are measured against.
200+ let (mut skew, mut last_raw) = (0i64, 0i64);
194 move |stream, ()| {201 move |stream, ()| {
195 let Some(mut buffer) = stream.dequeue_buffer() else {202 let Some(mut buffer) = stream.dequeue_buffer() else {
196 return;203 return;
@@ -234,7 +241,17 @@ fn run(
234 } else {241 } else {
235 ticks242 ticks
236 };243 };
237- shared.position.store(played.max(0), Ordering::Relaxed);244+ // The graph clock runs whether or not this stream does, so a
245+ // pause shows up here as a step. Nothing was played during
246+ // it: fold it into the skew instead of the position, or the
247+ // pause leaks into the caller's timeline.
248+ if shared.resumed.swap(false, Ordering::Relaxed) {
249+ skew += played - last_raw;
250+ }
251+ last_raw = played;
252+ shared
253+ .position
254+ .store((played - skew).max(0), Ordering::Relaxed);
238 }255 }
239 }256 }
240 })257 })
@@ -352,6 +369,7 @@ pub extern "C" fn ao_open(sample_rate: u32, channels: u32) -> *mut c_void {
352 ready: AtomicBool::new(false),369 ready: AtomicBool::new(false),
353 dead: AtomicBool::new(false),370 dead: AtomicBool::new(false),
354 flush: AtomicBool::new(false),371 flush: AtomicBool::new(false),
372+ resumed: AtomicBool::new(false),
355 underruns: AtomicU64::new(0),373 underruns: AtomicU64::new(0),
356 });374 });
357 let (ready_tx, ready_rx) = mpsc::channel();375 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
435 }453 }
436 // SAFETY: caller-guaranteed live session.454 // SAFETY: caller-guaranteed live session.
437 let session = unsafe { &*(session as *const Session) };455 let session = unsafe { &*(session as *const Session) };
456+ if active != 0 {
457+ session.shared.resumed.store(true, Ordering::Relaxed);
458+ }
438 match session.ctl.send(Ctl::Active(active != 0)) {459 match session.ctl.send(Ctl::Active(active != 0)) {
439 Ok(()) => 0,460 Ok(()) => 0,
440 Err(_) => -1,461 Err(_) => -1,