Dead audio output froze the clock forever
Browse files at this commit752c6a0bd6ada458faf72f934a89f88e52557406Parent: 63e6964- fix
- dead audio output froze the clock forever
1 changed file +21 -4
crates/engine/src/playback.rsmodified+21 -4
| @@ -40,6 +40,10 @@ struct Audio { | |||
| 40 | fed: Arc<AtomicU64>, | 40 | fed: Arc<AtomicU64>, |
| 41 | /// The decoder ran out and `fed` is final. | 41 | /// The decoder ran out and `fed` is final. |
| 42 | fed_all: Arc<AtomicBool>, | 42 | fed_all: Arc<AtomicBool>, |
| 43 | + /// The output plugin failed mid-stream. Its position is frozen from here on, | ||
| 44 | + /// so it can never satisfy [`Audio::played_out`] -- the flag is what lets | ||
| 45 | + /// [`PlaybackSession::tick`] hand the timeline to wall time instead. | ||
| 46 | + died: Arc<AtomicBool>, | ||
| 43 | } | 47 | } |
| 44 | 48 | ||
| 45 | impl Audio { | 49 | impl Audio { |
| @@ -137,6 +141,13 @@ impl PlaybackSession { | |||
| 137 | if self.clock.source() != ClockSource::Audio { | 141 | if self.clock.source() != ClockSource::Audio { |
| 138 | return; | 142 | return; |
| 139 | } | 143 | } |
| 144 | + if audio.died.load(Ordering::Acquire) { | ||
| 145 | + // Output died mid-stream: its position will never reach `fed`, so | ||
| 146 | + // the EOF comparison below is unreachable. Wall time takes over | ||
| 147 | + // from wherever the clock last stood. | ||
| 148 | + self.clock.switch_to_wall(); | ||
| 149 | + return; | ||
| 150 | + } | ||
| 140 | // `None` until the device's first callback: the clock holds at its | 151 | // `None` until the device's first callback: the clock holds at its |
| 141 | // anchor rather than guessing, which is why playback starts on time | 152 | // anchor rather than guessing, which is why playback starts on time |
| 142 | // instead of a quantum early. | 153 | // instead of a quantum early. |
| @@ -176,16 +187,18 @@ fn open_audio(path: &Path) -> Option<Audio> { | |||
| 176 | channels: u64::from(meta.channels), | 187 | channels: u64::from(meta.channels), |
| 177 | fed: Arc::new(AtomicU64::new(0)), | 188 | fed: Arc::new(AtomicU64::new(0)), |
| 178 | fed_all: Arc::new(AtomicBool::new(false)), | 189 | fed_all: Arc::new(AtomicBool::new(false)), |
| 190 | + died: Arc::new(AtomicBool::new(false)), | ||
| 179 | }; | 191 | }; |
| 180 | - let (ao, fed, fed_all) = ( | 192 | + let (ao, fed, fed_all, died) = ( |
| 181 | audio.ao.clone(), | 193 | audio.ao.clone(), |
| 182 | audio.fed.clone(), | 194 | audio.fed.clone(), |
| 183 | audio.fed_all.clone(), | 195 | audio.fed_all.clone(), |
| 196 | + audio.died.clone(), | ||
| 184 | ); | 197 | ); |
| 185 | thread::Builder::new() | 198 | thread::Builder::new() |
| 186 | .name("audio-feed".into()) | 199 | .name("audio-feed".into()) |
| 187 | .spawn(move || { | 200 | .spawn(move || { |
| 188 | - feed(rx, &ao, &fed); | 201 | + feed(rx, &ao, &fed, &died); |
| 189 | fed_all.store(true, Ordering::Release); | 202 | fed_all.store(true, Ordering::Release); |
| 190 | }) | 203 | }) |
| 191 | .ok()?; | 204 | .ok()?; |
| @@ -194,7 +207,7 @@ fn open_audio(path: &Path) -> Option<Audio> { | |||
| 194 | 207 | ||
| 195 | /// Pours decoded chunks into the device, keeping whatever the ring would not | 208 | /// Pours decoded chunks into the device, keeping whatever the ring would not |
| 196 | /// take. Ends when the decoder is done or the output dies. | 209 | /// take. Ends when the decoder is done or the output dies. |
| 197 | -fn feed(rx: Receiver<AudioChunk>, ao: &Mutex<AoSession>, fed: &AtomicU64) { | 210 | +fn feed(rx: Receiver<AudioChunk>, ao: &Mutex<AoSession>, fed: &AtomicU64, died: &AtomicBool) { |
| 198 | let mut pending: Vec<f32> = Vec::new(); | 211 | let mut pending: Vec<f32> = Vec::new(); |
| 199 | loop { | 212 | loop { |
| 200 | if pending.is_empty() { | 213 | if pending.is_empty() { |
| @@ -205,7 +218,11 @@ fn feed(rx: Receiver<AudioChunk>, ao: &Mutex<AoSession>, fed: &AtomicU64) { | |||
| 205 | } | 218 | } |
| 206 | let accepted = match ao.lock().unwrap().write(&pending) { | 219 | let accepted = match ao.lock().unwrap().write(&pending) { |
| 207 | Some(n) => n, | 220 | Some(n) => n, |
| 208 | - None => return, // output died; the clock switches to wall | 221 | + None => { |
| 222 | + // Output died; flag it so `tick` moves the clock to wall time. | ||
| 223 | + died.store(true, Ordering::Release); | ||
| 224 | + return; | ||
| 225 | + } | ||
| 209 | }; | 226 | }; |
| 210 | fed.fetch_add(accepted as u64, Ordering::Relaxed); | 227 | fed.fetch_add(accepted as u64, Ordering::Relaxed); |
| 211 | pending.drain(..accepted); | 228 | pending.drain(..accepted); |