Stop the pause leaking into the playback clock
Browse files at this commitbffc0348665fe06d7747f676c5af5c1b7f9861b6Parent: aae5734- fix
- stop the pause leaking into the playback clock
1 changed file +22 -1
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 | ticks | 242 | 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, |