diff --git a/crates/engine/src/audio.rs b/crates/engine/src/audio.rs index e14ef0c..bb2cc16 100644 --- a/crates/engine/src/audio.rs +++ b/crates/engine/src/audio.rs @@ -581,11 +581,10 @@ impl AudioSession { limiter: Limiter, live: Option<&Arc>, ) -> crate::Result)>> { - let [first, rest @ ..] = lanes else { + let [first, ..] = lanes else { return Ok(None); }; - let mastered = limiter.is_active() || gains.iter().any(|&g| g != 1.0); - if rest.is_empty() && !mastered { + if !Self::is_mixed(lanes.len(), gains, limiter) { let flat = Vec::new(); let plain = Vec::new(); return Self::open_multi_streams_speed( @@ -640,6 +639,19 @@ impl AudioSession { Ok(Some((meta, rx))) } + /// Whether these settings put the timeline through the **mixer**: more than + /// one lane to sum, a lane off unity, or a limiter over the sum. Anything + /// else is the single-lane flat path, which is `open_multi_streams` + /// verbatim and has no mixer thread to talk to. + /// + /// Public to the crate because [`crate::PlaybackSession`] has to know the + /// answer *before* the open now that it happens on another thread, and the + /// answer must not be spelled out twice: what it decides is whether a fader + /// is handed to a running mixer or rebuilds the stream. + pub(crate) fn is_mixed(lanes: usize, gains: &[f32], limiter: Limiter) -> bool { + lanes > 1 || limiter.is_active() || gains.iter().any(|&g| g != 1.0) + } + /// Why [`open`](Self::open) came back silent for a file that *has* sound: /// `Some(reason)` when there is an audio track and it is not the AAC-LC /// this decodes (an AC-3 remux, say). `None` when the file is simply silent diff --git a/crates/engine/src/playback.rs b/crates/engine/src/playback.rs index 4c4f3cc..0e2c246 100644 --- a/crates/engine/src/playback.rs +++ b/crates/engine/src/playback.rs @@ -149,12 +149,42 @@ impl Audio { /// Starts a feeder for the current epoch, draining `rx` into the device. /// `false` if the thread would not start, i.e. nothing will ever be fed. fn spawn_feeder(&self, rx: Receiver) -> bool { + self.spawn_feeder_deferred(move || Some(rx)) + } + + /// As [`spawn_feeder`](Self::spawn_feeder), but the *stream* is opened on + /// the feeder too: `open` runs on that thread, so nothing here touches the + /// disk and the caller pays a thread spawn instead of a demux -- seconds off + /// a cold cache on a big film, which is what a seek used to cost the UI + /// thread ([`crate::DecodeSession::open_worker_deferred`] is the picture's + /// half of the same move). + /// + /// The price is the same one: there is no [`crate::audio::AudioMeta`] to + /// hand back and no synchronous answer to "will anything play". `None` from + /// `open` -- a silent timeline, a source that will not open -- is a feeder + /// with nothing to feed, which ends exactly as a played-out stream does and + /// hands the clock to wall time ([`tick`](PlaybackSession::tick)). + /// + /// The epoch is captured *here*, at the seek, not when the open returns: a + /// scrub abandons opens by the dozen and every one of them must still be + /// the stale stream it was started as, however late it lands. + fn spawn_feeder_deferred( + &self, + open: impl FnOnce() -> Option> + Send + 'static, + ) -> bool { let me = self.clone(); let epoch = self.epoch.load(Ordering::Acquire); thread::Builder::new() .name("audio-feed".into()) .spawn(move || { - feed(rx, &me, epoch); + // Superseded before this thread was even scheduled: do not open + // anything at all. A scrub leaves all but the last one here. + if me.epoch.load(Ordering::Acquire) != epoch { + return; + } + if let Some(rx) = open() { + feed(rx, &me, epoch); + } // Only the current feeder gets to declare the end of the audio: // a superseded one is finished, the stream is not. if me.epoch.load(Ordering::Acquire) == epoch { @@ -2252,6 +2282,20 @@ impl PlaybackSession { /// Puts the device back where it was: playing if it was playing, and with /// the clock held until the new stream is really audible (see /// [`Audio::content_at`]). + /// + /// ponytail: the device is made active here, before the new stream has been + /// opened ([`start_audio`](Self::start_audio) opens on the feeder now), so a + /// cold open leaves it playing its own silence with an empty ring -- audibly + /// the same silence as before, and counted: a scripted cold play-seek-scrub + /// session goes from 5 to 1721 `engine_audio: N underruns`, one per starved + /// quantum, while a warm one is unchanged (46 -> 53 over the suite's 11 + /// sessions). Ceiling: the count stops meaning "the decoder was late" during + /// a cold seek. Upgrade path is a `wants_active` flag written here and read + /// by the feeder under the device lock beside the [`Audio::content_at`] + /// stamp, so the device is started by the first real sample rather than by + /// the intent to play -- it has to be the lock and the epoch check, or a + /// pause during the open would be overtaken by a late activation and the + /// timeline would play while it says it is paused. fn resume(&mut self, was_playing: bool) { self.priming = true; if was_playing && let Some(audio) = &self.audio { @@ -2260,7 +2304,21 @@ impl PlaybackSession { } /// Rebuilds the **sound** from `target` -- the second half of a restart -- - /// and says whether anything will be fed. + /// and says whether a feeder took the job. Not whether it will find + /// anything to play: the stream is opened on that feeder + /// ([`Audio::spawn_feeder_deferred`]), so a silent timeline is heard of + /// there, not here. Nothing on this thread reads the file. + /// + /// The clock is unaffected by the wait, which is the reason it may be a + /// long one: it holds where the seek put it until the *first real sample* + /// is queued ([`Audio::content_at`], [`tick`](Self::tick)), whether that is + /// ten milliseconds later or ten seconds. + /// + /// `true` is therefore *optimistic*, and the callers' `switch_to_audio` with + /// it: a timeline that turns out to have nothing to play ends its feeder at + /// once, which sets `fed_all` and hands the clock back to wall time at the + /// next tick, from where it stood ([`PlaybackClock::switch_to_wall`] is + /// continuous, so nothing jumps). fn start_audio(&mut self, target: u32) -> bool { let fps = self.meta.frame_rate; let mut audio_running = false; @@ -2306,21 +2364,39 @@ impl PlaybackSession { let gains = self.project.audio_gains(); let limiter = self.project.limiter(); let controls = crate::audio::MixControls::new(gains.clone(), limiter); - audio_running = match AudioSession::open_mixed_streams_live( - &sources, - &segs, - &eqs, - &speeds, - &gains, - limiter, - Some(&controls), - ) { - Ok(Some((_, rx))) => audio.spawn_feeder(rx), - _ => false, - }; // Only a mixed timeline has a mixer to talk to; the single-lane - // flat path is the bit-exact one and has none. - live = (audio_running && controls.is_live()).then_some(controls); + // flat path is the bit-exact one and has none. Decided here from + // the very lists the open is about to be handed + // ([`AudioSession::is_mixed`]) rather than read off the handle + // afterwards, because there *is* no afterwards on this thread any + // more -- the open runs on the feeder. + let mixed = crate::audio::AudioSession::is_mixed(segs.len(), &gains, limiter); + let worker_controls = Arc::clone(&controls); + // ...and the open itself -- every track a segment names, its packet + // table, its priming -- happens there too. That is the whole point: + // one `pread` on a cold 25 GB film is seconds, and this is called + // from a ruler drag ([`Audio::spawn_feeder_deferred`]). + audio_running = audio.spawn_feeder_deferred(move || { + match AudioSession::open_mixed_streams_live( + &sources, + &segs, + &eqs, + &speeds, + &gains, + limiter, + Some(&worker_controls), + ) { + Ok(Some((_, rx))) => Some(rx), + // A silent timeline, and a source that will not open: both + // feed nothing, which the feeder itself reports by ending. + Ok(None) => None, + Err(e) => { + eprintln!("timeline audio open failed: {e}"); + None + } + } + }); + live = (audio_running && mixed).then_some(controls); if !audio_running { // Nothing will ever be fed again; let `tick` fall to wall time // instead of waiting on a device that has no more work coming. @@ -2706,6 +2782,17 @@ fn feed(rx: Receiver, audio: &Audio, epoch: u64) { if accepted > 0 && audio.content_at.load(Ordering::Acquire) < 0 { let at = ao.position().unwrap_or(0).max(0); audio.content_at.store(at, Ordering::Release); + // ...and the device position this stream's samples are counted + // from ([`Audio::played_out`]). Rebased *here* rather than at + // the seek, because the open runs on this thread now: every + // millisecond it took is device time nobody fed, and counting + // it would make the stream read as played out that long before + // it really is -- an early hand-off to wall time at the end of + // the timeline. The seek's own rebase still stands for the + // stream that never queues a sample at all. + audio + .fed + .store(at as u64 * audio.channels, Ordering::Relaxed); } accepted }; diff --git a/crates/engine/tests/playback.rs b/crates/engine/tests/playback.rs index 77487f5..f6ada77 100644 --- a/crates/engine/tests/playback.rs +++ b/crates/engine/tests/playback.rs @@ -7,6 +7,8 @@ //! cargo test -p engine --release --test playback -- --nocapture //! ``` +use std::fs::File; +use std::os::fd::AsRawFd; use std::path::{Path, PathBuf}; use std::thread::sleep; use std::time::{Duration, Instant}; @@ -375,6 +377,88 @@ fn seek_keeps_the_audio_clock() { ); } +/// Drops `path` out of the page cache, so the next read of it is a real disk +/// read. `posix_fadvise(POSIX_FADV_DONTNEED)`, glibc's own and declared here +/// rather than pulled in as a dependency for one call. +fn evict(path: &Path) { + unsafe extern "C" { + fn posix_fadvise(fd: i32, offset: i64, len: i64, advice: i32) -> i32; + } + let file = File::open(path).expect("open to evict"); + // 0 length is "to the end of the file"; 4 is POSIX_FADV_DONTNEED on Linux. + let rc = unsafe { posix_fadvise(file.as_raw_fd(), 0, 0, 4) }; + assert_eq!(rc, 0, "could not evict {}", path.display()); +} + +/// What a seek costs the *caller* when the sound has to be opened again off a +/// cold page cache -- the audio half of the move +/// [`a_storm_of_seeks_stays_bounded_and_the_last_one_wins`] measures for the +/// picture. That open is a `pread` per track (21 s on a cold 25 GB film, +/// measured at the seat) and it used to run on whoever called `seek`, which in +/// the editor is the thread that paints: one ruler click froze the window for +/// the whole of it. On the feeder it is a thread spawn, whatever the file. +/// +/// And the clock still anchors on the first real sample, which is the invariant +/// the wait threatens: the timeline holds where the seek put it for however +/// long the open takes, and starts counting from *there* rather than from the +/// silence the device played meanwhile. +/// +/// `assets/test_av.mp4` by default, which makes this a regression guard; +/// `EDITH_COLD_FILM=` makes it the measurement. +#[test] +#[ignore = "needs a running PipeWire daemon"] +fn a_cold_audio_open_does_not_block_the_seek() { + let path = std::env::var_os("EDITH_COLD_FILM") + .map(PathBuf::from) + .unwrap_or_else(|| asset("test_av.mp4")); + let mut session = open(&path); + let target = session.timeline_duration() * 0.5; + let mut last_index = None; + session.play(); + run_for(&mut session, &mut last_index, Duration::from_millis(300)); + + // The seek's own reads and nothing else: the open above warmed every byte + // of this file that anything here touches. + evict(&path); + let issued = Instant::now(); + session.seek(target); + let blocked = issued.elapsed(); + eprintln!( + "cold seek on {}: caller blocked {blocked:?}", + path.display() + ); + assert!( + blocked < Duration::from_millis(10), + "the caller opened the file itself: seek blocked {blocked:?}" + ); + assert!( + (session.now() - target).abs() < 0.05, + "the seek did not land: clock at {}", + session.now() + ); + + // The sound arrives when it arrives, and the clock does not move until it + // does. Long deadline: that is the point of the test. + let waited = Instant::now(); + let deadline = waited + Duration::from_secs(120); + while session.now() <= target + 0.001 { + session.tick(); + while session.try_frame().is_some() {} + assert!( + Instant::now() < deadline, + "the clock never moved after a cold seek" + ); + sleep(Duration::from_millis(8)); + } + let (moved, waited) = (session.now(), waited.elapsed().as_secs_f64()); + eprintln!("clock moved to {moved:.3}s (target {target:.3}s) after {waited:.3}s of open"); + assert!( + moved - target < 0.5, + "the open leaked into the timeline: {waited:.3}s of it put the clock at \ + {moved:.3}s instead of {target:.3}s" + ); +} + /// The edit list end to end: two cuts, drop the middle clip, play the result. /// What comes out is one contiguous timeline whose *pictures* skip the hole -- /// no audio needed, so this runs anywhere.