diff --git a/crates/engine/src/playback.rs b/crates/engine/src/playback.rs index be6c899..960b637 100644 --- a/crates/engine/src/playback.rs +++ b/crates/engine/src/playback.rs @@ -16,7 +16,7 @@ use std::sync::atomic::{AtomicBool, AtomicI64, AtomicU64, AtomicUsize, Ordering} use std::sync::mpsc::{Receiver, TryRecvError}; use std::sync::{Arc, Mutex}; use std::thread; -use std::time::Duration; +use std::time::{Duration, Instant}; use crate::ao::AoSession; use crate::audio::{AudioChunk, AudioSession}; @@ -52,6 +52,18 @@ const FLUSH_WAIT: Duration = Duration::from_millis(1); /// all pays this once per span rather than freezing the preview outright. const DISSOLVE_B_TIMEOUT: Duration = Duration::from_millis(200); +/// How long a dead output is left alone before another open is attempted. A +/// PipeWire daemon restart takes a moment, and the ask that makes the attempt +/// runs once per rendered frame: a second is long enough that a restart lands +/// inside it, and short enough that a play pressed after one does not wait. +const AUDIO_REARM_AFTER: Duration = Duration::from_secs(1); + +/// What a session says when its output dies ([`PlaybackSession::tick`]): the +/// one line a front-end shows for a session that has gone quiet, through the +/// same door a file's own refusal reaches it by +/// ([`PlaybackSession::audio_disabled_reason`]). +const AUDIO_LOST: &str = "output device went away -- reconnecting the timeline's sound"; + /// The timeline a file with no picture scaffolds: 1080p at 30 fps, H.264 -- /// nothing was shot on it, so it is the canvas a *later* import meets rather /// than a description of the song. H.264 because it is the one codec that @@ -149,6 +161,13 @@ struct Audio { /// at once -- which is precisely what /// [`live_workers`](PlaybackSession::live_workers) is asked about. feeders: Arc, + /// When this device was last opened *again* after dying + /// ([`PlaybackSession::rearm_audio`]). A dead output is offered one open a + /// second and no more, because the ask that makes the attempt runs once per + /// rendered frame: a daemon restart takes a moment, and a device that is + /// gone for good must not be opened sixty times a second for the rest of + /// the session. + rearm_at: Arc>>, } /// How much of the played signal the tap keeps. A power of two, because what @@ -2672,7 +2691,12 @@ impl PlaybackSession { /// ([`Self::seek`]'s own `switch_to_audio`), which is the seek a front-end /// makes as it places the file it just imported. fn arm_audio(&mut self) { - if self.audio.is_some() { + // A device that is there but *dead* is not a device: the walk below + // opens another one, which is also what clears the notice a death put up + // ([`Self::tick`]). + if let Some(audio) = &self.audio + && !audio.died.load(Ordering::Acquire) + { return; } for source in self @@ -2690,6 +2714,63 @@ impl PlaybackSession { } } + /// Opens the output again for a stream that died, at the rate and channel + /// count it carried, and clears the flag that stopped the session feeding. + /// + /// An output that goes away -- a PipeWire daemon restarting, a sink node + /// removed and put back -- leaves every stream that was open on it dead for + /// good: its position is frozen, so the clock can only fall to wall time, and + /// a session that never opens another device is silent for the rest of its + /// life, through every seek and every pause/play, with nothing said about it. + /// + /// The device *only*, never the file: the feeder is [`Self::start_audio`]'s, + /// at the seek that follows. That is what lets [`Self::tick`] call this too, + /// once a frame -- a tick is not a seek and must not reseek the timeline. The + /// clock is on wall time by then and stays there until a seek comes, which is + /// the seek whose feeder opens in step with the picture. + /// + /// `if_idle_for` is the cooldown: a tick passes [`AUDIO_REARM_AFTER`], because + /// sixty opens a second against a device that is gone for good is a spin; a + /// seek passes zero, because that one is a person asking. + /// + /// `false` when there is nothing to open, nothing came back, or the cooldown + /// has not passed -- and the flag stays standing for the next attempt. + fn rearm_audio(&mut self, if_idle_for: Duration) -> bool { + let Some(audio) = &self.audio else { + return false; + }; + if !audio.died.load(Ordering::Acquire) { + return true; + } + { + let mut at = audio.rearm_at.lock().unwrap(); + if at.is_some_and(|at| at.elapsed() < if_idle_for) { + return false; + } + *at = Some(Instant::now()); + } + let Some(ao) = AoSession::open(audio.sample_rate, audio.channels as u32) else { + return false; + }; + // A stream comes up streaming and a session starts paused: the fresh one + // is silenced until the seek that feeds it says otherwise, which is the + // same two steps [`open_audio`] takes on the way in. + ao.set_active(false); + // Replacing the session is what closes the dead stream, so it is done + // under the device lock the feeder writes through. + *audio.ao.lock().unwrap() = ao; + // The clock anchor and the played-out arithmetic both belong to the + // stream that died: the new one stamps its own first sample + // ([`Audio::content_at`]), and `start_audio` re-bases the count from the + // device's own position. + audio.content_at.store(-1, Ordering::Release); + audio.died.store(false, Ordering::Release); + if self.audio_disabled.as_deref() == Some(AUDIO_LOST) { + self.audio_disabled = None; + } + true + } + /// The rate noted for whichever source plays `path`, by /// [`file_frames`](Self::file_frames)'s rule: one file is one rate however /// many audio tracks it carries. [`Rate::REAL_TIME`] for a file this session @@ -3910,6 +3991,12 @@ impl PlaybackSession { let fps = self.meta.frame_rate; let mut audio_running = false; let mut live = None; + // A dead output gets an attempt here without waiting out the cooldown: a + // seek is a person asking, and this is the one moment the sound can come + // back into step with the picture, because the feeder opened below starts + // at the target. If it does not come back the flag stands and the guard + // under it leaves the clock on wall time. + self.rearm_audio(Duration::ZERO); if let Some(audio) = &self.audio && !audio.died.load(Ordering::Acquire) { @@ -4079,19 +4166,37 @@ impl PlaybackSession { // still be moving it -- otherwise the queue fills with pictures whose // moment passed while nobody was taking them. self.publish_playhead(); + // Before the clock-source guard below, and not after it: a dead output is + // what *puts* the clock on wall time, so this branch has to keep running + // while it is there -- the one attempt that can bring the sound back is + // in it. + if self + .audio + .as_ref() + .is_some_and(|audio| audio.died.load(Ordering::Acquire)) + { + // Output died mid-stream: its position will never reach `fed`, so the + // EOF comparison below is unreachable. Wall time takes over from + // wherever the clock last stood, and the session says so rather than + // going silent for good without a word: a front-end already has a + // line for a silent session ([`Self::audio_disabled_reason`]). + if self.audio_disabled.is_none() { + self.audio_disabled = Some(AUDIO_LOST.to_string()); + } + self.clock.switch_to_wall(); + // ...and the output is offered another open, once a second: a + // restarting daemon takes a moment, and this runs once a frame. A + // re-arm that takes is what clears the notice, and the next seek + // feeds it. + self.rearm_audio(AUDIO_REARM_AFTER); + return; + } let Some(audio) = &self.audio else { return; // wall time from the start, nothing to poll }; if self.clock.source() != ClockSource::Audio { return; } - if audio.died.load(Ordering::Acquire) { - // Output died mid-stream: its position will never reach `fed`, so - // the EOF comparison below is unreachable. Wall time takes over - // from wherever the clock last stood. - self.clock.switch_to_wall(); - return; - } // `None` until the device's first callback: the clock holds at its // anchor rather than guessing, which is why playback starts on time // instead of a quantum early. @@ -4420,6 +4525,7 @@ fn open_audio(path: &Path, stream: usize) -> (Option