Paused seek ate 1.49s of post-seek audio
Browse files at this commite2595163db84b7f6598616269e5b2804c4da56b2Parent: 14821ab- fix
- paused seek ate 1.49s of post-seek audio
1 changed file +41 -2
crates/engine-audio/src/lib.rsmodified+41 -2
| @@ -96,6 +96,19 @@ impl Ring { | |||
| 96 | /// only the RT thread ever moves `read` -- and means a flush asked for while | 96 | /// only the RT thread ever moves `read` -- and means a flush asked for while |
| 97 | /// paused (no callbacks running) still lands before the next pop, so the stale | 97 | /// paused (no callbacks running) still lands before the next pop, so the stale |
| 98 | /// samples can never be heard. | 98 | /// samples can never be heard. |
| 99 | +/// The producer half of a flush: while the flag is pending, the ring still | ||
| 100 | +/// holds pre-flush audio waiting to be discarded, and anything accepted now | ||
| 101 | +/// would queue BEHIND that discard -- with no RT callbacks running (paused | ||
| 102 | +/// seek) the next callback would then drain the new stream's head along with | ||
| 103 | +/// the stale tail. Refuse instead; the writer's retry loop parks until the | ||
| 104 | +/// RT thread consumes the flag. | ||
| 105 | +fn accept(flush: &AtomicBool, ring: &Ring, samples: &[f32]) -> usize { | ||
| 106 | + if flush.load(Ordering::Acquire) { | ||
| 107 | + return 0; | ||
| 108 | + } | ||
| 109 | + ring.push(samples) | ||
| 110 | +} | ||
| 111 | + | ||
| 99 | fn consume(flush: &AtomicBool, ring: &Ring, dst: &mut [u8], want: usize) -> usize { | 112 | fn consume(flush: &AtomicBool, ring: &Ring, dst: &mut [u8], want: usize) -> usize { |
| 100 | if flush.swap(false, Ordering::Acquire) { | 113 | if flush.swap(false, Ordering::Acquire) { |
| 101 | ring.read | 114 | ring.read |
| @@ -387,7 +400,7 @@ pub unsafe extern "C" fn ao_write(session: *mut c_void, samples: *const f32, n: | |||
| 387 | return -1; | 400 | return -1; |
| 388 | } | 401 | } |
| 389 | let samples = unsafe { std::slice::from_raw_parts(samples, n) }; | 402 | let samples = unsafe { std::slice::from_raw_parts(samples, n) }; |
| 390 | - session.shared.ring.push(samples) as isize | 403 | + accept(&session.shared.flush, &session.shared.ring, samples) as isize |
| 391 | })) | 404 | })) |
| 392 | .unwrap_or(-1) | 405 | .unwrap_or(-1) |
| 393 | } | 406 | } |
| @@ -468,7 +481,33 @@ pub unsafe extern "C" fn ao_close(session: *mut c_void) { | |||
| 468 | 481 | ||
| 469 | #[cfg(test)] | 482 | #[cfg(test)] |
| 470 | mod tests { | 483 | mod tests { |
| 471 | - use super::{AtomicBool, Ordering, Ring, consume}; | 484 | + use super::{AtomicBool, Ordering, Ring, accept, consume}; |
| 485 | + | ||
| 486 | + /// The paused-seek ordering: a flush requested with no RT callbacks running | ||
| 487 | + /// must not let the writer queue new audio behind the pending discard. | ||
| 488 | + #[test] | ||
| 489 | + fn pending_flush_refuses_writes_until_consumed() { | ||
| 490 | + let ring = Ring::new(4); | ||
| 491 | + let flush = AtomicBool::new(false); | ||
| 492 | + let mut out = [0u8; 16]; | ||
| 493 | + | ||
| 494 | + // Stale pre-seek audio sits in the ring; a flush is requested while | ||
| 495 | + // paused (no callback runs yet). | ||
| 496 | + assert_eq!(accept(&flush, &ring, &[1.0, 2.0]), 2); | ||
| 497 | + flush.store(true, Ordering::Release); | ||
| 498 | + | ||
| 499 | + // The new stream's feeder tries to write: refused, ring untouched. | ||
| 500 | + assert_eq!(accept(&flush, &ring, &[3.0, 4.0]), 0); | ||
| 501 | + | ||
| 502 | + // First post-resume callback consumes the flag and drains the stale | ||
| 503 | + // tail -- and ONLY the stale tail, because nothing was queued behind it. | ||
| 504 | + assert_eq!(consume(&flush, &ring, &mut out, 2), 0); | ||
| 505 | + | ||
| 506 | + // Writer unparks; the new audio flows intact. | ||
| 507 | + assert_eq!(accept(&flush, &ring, &[3.0, 4.0]), 2); | ||
| 508 | + assert_eq!(consume(&flush, &ring, &mut out, 2), 2); | ||
| 509 | + assert_eq!(&out[..4], &3.0f32.to_le_bytes()); | ||
| 510 | + } | ||
| 472 | 511 | ||
| 473 | #[test] | 512 | #[test] |
| 474 | fn ring_wraps_fills_and_starves() { | 513 | fn ring_wraps_fills_and_starves() { |