From c80d1f748001eea7c9d9f3fc8363f9d3a01896e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Thu, 24 Sep 2026 14:53:34 +0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fix(audio):=20a=20device=20that?= =?UTF-8?q?=20dies=20under=20an=20open=20file=20says=20so,=20and=20unsays?= =?UTF-8?q?=20it?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The engine knows: a death sets its `audio_disabled_reason` to its lost-device word, and its own 1 s re-arm clears it again. No consumer on this side ever looked -- both `audio_notice` call sites are the *open* paths -- so a device that died mid-session played on in silence with no message at all, and the strip went on saying nothing after the sound came back. `Player::watch_audio` is the per-frame check, run from `Player::pump` (the one place every frame passes through): one read of an `Option<&str>` and one compare, with the strings built only on the change itself. `audio_change_notice` is the queue half and reuses the existing notice path -- `push_notice` for the death, and the same line taken back off the strip when the reason clears, only that line. The two open sites seed the cache (`Player::seed_audio_watch`) so a reason unchanged across the open is not announced a second time, and a line left over from the file before comes off. --- crates/app/src/main.rs | 9 ++++ crates/app/src/notices.rs | 51 ++++++++++++++++-- crates/app/src/player/library.rs | 10 ++++ crates/app/src/player/transport.rs | 55 +++++++++++++++++++ crates/app/src/tests/media.rs | 85 ++++++++++++++++++++++++++---- 5 files changed, 195 insertions(+), 15 deletions(-) diff --git a/crates/app/src/main.rs b/crates/app/src/main.rs index d5cecc3..e5441f5 100644 --- a/crates/app/src/main.rs +++ b/crates/app/src/main.rs @@ -790,6 +790,12 @@ struct Player { /// The active session's identity, for the above: bumped by every door that /// swaps `session` or `preview_session`. session_gen: u64, + /// The engine's own word on the sound device, as the last pump saw it: the + /// cache [`Player::watch_audio`] compares each frame against so a reason is + /// announced once when it *changes* and taken back when it clears. `None` + /// while the sound is up, and seeded by `Player::seed_audio_watch` at every + /// open, whose own line already carries the same fact. + audio_reason: Option, /// Which of the clip's two inks the look section's wheel and colour field /// act on ([`VizSlot`]): the clip's own colour, or the ring's dual half. /// The pick stays picked across clips, the way the dock tab's does. @@ -1119,6 +1125,9 @@ fn main() { picture_box: Rc::default(), shot_pending: None, session_gen: 0, + // Nothing heard yet: the first pump seeds it off the session + // it serves. + audio_reason: None, viz_slot: VizSlot::Main, viz_hex_edit: None, pending_viz: None, diff --git a/crates/app/src/notices.rs b/crates/app/src/notices.rs index 299d54f..b04ce06 100644 --- a/crates/app/src/notices.rs +++ b/crates/app/src/notices.rs @@ -46,9 +46,48 @@ fn is_completion(message: &str) -> bool { /// decode: it plays perfectly, in silence, and that is the one thing the window /// would otherwise never say (the engine's own word for it, verbatim). pub(crate) fn audio_notice(session: &PlaybackSession) -> Option { - session - .audio_disabled_reason() - .map(|reason| format!(" — NO AUDIO: {reason}")) + session.audio_disabled_reason().map(audio_tail) +} + +/// [`audio_notice`]'s own tail, for a reason already read -- the one place the +/// engine's word is spelled into a line, so the open path and the mid-session +/// one cannot drift apart. +pub(crate) fn audio_tail(reason: &str) -> String { + format!(" — NO AUDIO: {reason}") +} + +/// The same fact said on its own, for a device that dies *after* a file was +/// opened: [`audio_notice`] is a tail to append to "OPENED x", and a death +/// mid-session has no line to hang off -- so it leads with the state word the +/// strip reads first, and the tail is the same one. +pub(crate) fn audio_lost_line(reason: &str) -> String { + format!("AUDIO LOST{}", audio_tail(reason)) +} + +/// What a *change* in the engine's sound reason does to the notice queue, as a +/// pure step: the death line pushed when a reason arrives, the same line taken +/// back when the reason clears ([`PlaybackSession::audio_disabled_reason`]'s +/// `AUDIO_LOST` and its own re-arm). [`Player::watch_audio`] is the only caller +/// and owns the caching; this owns the queue, so a test can drive both +/// directions without a window and without a device that dies on cue. +pub(crate) fn audio_change_notice( + notices: &mut std::collections::VecDeque, + was: Option<&str>, + now: Option<&str>, +) { + match (was, now) { + (_, Some(reason)) if was != Some(reason) => { + push_notice(notices, audio_lost_line(reason).into()); + } + // The sound is back: the line goes with it, or the strip keeps saying NO + // AUDIO over a film that plays. Only that line -- whatever else is + // queued is none of this step's business. + (Some(was), None) => { + let line = audio_lost_line(was); + notices.retain(|notice| notice.as_ref() != line.as_str()); + } + _ => {} + } } /// The strip's own width is finite and its message is one line: what a @@ -121,12 +160,14 @@ mod tests { #[test] fn a_long_name_loses_its_middle_rather_than_the_state_word() { assert_eq!(ledger_line("SNAP OFF"), "SNAP OFF"); - let long = ledger_line("OPENED he_is_not_the_only_one_at_all_here.mp4 · 1 subtitle track(s)"); + let long = + ledger_line("OPENED he_is_not_the_only_one_at_all_here.mp4 · 1 subtitle track(s)"); assert!(long.starts_with("OPENED "), "{long}"); assert!(long.ends_with(" · 1 subtitle track(s)"), "{long}"); assert!(long.contains('…'), "{long}"); assert!( - long.split(' ').all(|w| w.chars().count() <= LEDGER_WORD_MAX), + long.split(' ') + .all(|w| w.chars().count() <= LEDGER_WORD_MAX), "{long}" ); } diff --git a/crates/app/src/player/library.rs b/crates/app/src/player/library.rs index b0d0290..787683a 100644 --- a/crates/app/src/player/library.rs +++ b/crates/app/src/player/library.rs @@ -1481,6 +1481,11 @@ impl Player { // Read before the session moves: a file that plays silent says // so here or nowhere. let silent = audio_notice(&session); + // The watch's own cache, seeded here rather than on the first + // pump after: this line already carried the fact, so a reason + // unchanged across the open is not announced a second time + // ([`Player::seed_audio_watch`]). + self.seed_audio_watch(&session); // A file replaces the one that was open, and track 3 of that one // is not track 3 of this. self.sub_track = 0; @@ -1795,6 +1800,11 @@ impl Player { // As at the file door above: the window watches in real time. session.drop_late_pictures(true); let silent = audio_notice(&session); + // The watch's own cache, seeded here rather than on the first + // pump after: this line already carried the fact, so a reason + // unchanged across the open is not announced a second time + // ([`Player::seed_audio_watch`]). + self.seed_audio_watch(&session); // A project is named after itself but still exports beside its // media: that is the only place an export has ever landed. self.export_path = retarget(&export_path(&session.sources()[0].path), self.format); diff --git a/crates/app/src/player/transport.rs b/crates/app/src/player/transport.rs index 2be6b48..4828731 100644 --- a/crates/app/src/player/transport.rs +++ b/crates/app/src/player/transport.rs @@ -25,12 +25,67 @@ impl Player { .map_or(self.fps, |s| s.meta().frame_rate) } + /// The engine's own word on the sound device, for the session the pump + /// serves ([`Player::active_session`]). + fn sound_reason(&self) -> Option<&str> { + self.active_session() + .and_then(PlaybackSession::audio_disabled_reason) + } + + /// The engine's word on the sound device, watched rather than asked once. + /// A file that *opens* silent says so in its own open line + /// ([`audio_notice`], `library.rs`), but a device that dies mid-session used + /// to leave the window playing on in silence with nothing said at all: the + /// engine knew (it sets its reason to its lost-device word and clears it + /// again when its own re-arm takes) and no consumer ever looked. + /// + /// Called from [`Player::pump`] because that is the one place every frame + /// passes through, and written to cost an ordinary frame nothing: one read of + /// an `Option<&str>` and one compare, with the strings built only on the + /// change itself. + pub(crate) fn watch_audio(&mut self) { + if self.sound_reason() == self.audio_reason.as_deref() { + return; + } + // Re-read rather than carry a borrow across the write: this line is only + // reached on the change, so the allocation it costs is the news itself. + let reason = self.sound_reason().map(str::to_owned); + let was = std::mem::replace(&mut self.audio_reason, reason); + audio_change_notice( + &mut self.notices, + was.as_deref(), + self.audio_reason.as_deref(), + ); + } + + /// [`Player::watch_audio`]'s cache, seeded at the moment an open's own line + /// carried the same fact: a reason unchanged across the open is not news, so + /// only the *clearing* half runs here -- a line left over from the file + /// before it comes off the strip. + pub(crate) fn seed_audio_watch(&mut self, session: &PlaybackSession) { + let was = std::mem::replace( + &mut self.audio_reason, + session.audio_disabled_reason().map(str::to_owned), + ); + if self.audio_reason.is_none() { + if let Some(was) = was { + let line = audio_lost_line(&was); + self.notices + .retain(|notice| notice.as_ref() != line.as_str()); + } + } + } + /// Catches the display up to the clock: everything already due is taken off /// the channel and only the last of them is shown, which *is* the /// drop-when-behind policy. A frame that is not due yet waits in `held`, and /// while the clock is paused *nothing* is due -- a repaint re-presents the /// frame already on screen, whatever asked for the repaint. pub(crate) fn pump(&mut self, window: &mut Window) { + // Before the drain, on the one read that does not need the session + // mutably: the sound can die at any point in a frame, and the notice it + // earns is owed to the frame it died in. + self.watch_audio(); // Where the transport was before this drain, so the crossing into // `Ended` can be recognised as the one transition it is. let was = self.transport(); diff --git a/crates/app/src/tests/media.rs b/crates/app/src/tests/media.rs index eee46f2..b5b5da8 100644 --- a/crates/app/src/tests/media.rs +++ b/crates/app/src/tests/media.rs @@ -6,11 +6,75 @@ use crate::player::library::{ auto_proxies_pref_path, load_auto_proxies_pref, save_auto_proxies_pref, }; use crate::subs::{ - SUB_SIZE_RANGE, audio_import_tail, load_subtitle_style, save_subtitle_style, sub_line_h_for, - subtitle_style_path, + audio_import_tail, load_subtitle_style, save_subtitle_style, sub_line_h_for, + subtitle_style_path, SUB_SIZE_RANGE, }; use crate::ui::preview::{bgra_to_rgba, screenshot_path}; +/// The notice queue's own half of the sound watch, and the three words it is +/// spelled with -- reached by name because the app's own glob does not carry +/// them (`notices` is a private module re-exported for the window). +use crate::notices::{audio_change_notice, audio_lost_line, audio_tail}; + +/// A sound device that dies under a file that is already open says so, and stops +/// saying so when the engine's own re-arm takes. The engine sets its +/// `audio_disabled_reason` to its lost-device word on a death and clears it on a +/// successful reopen, and nothing on this side ever looked at it: the two call +/// sites were the *open* paths, so a mid-session death played on in silence with +/// no message at all. +/// +/// Driven through the queue step itself -- [`audio_change_notice`] is the whole +/// of what a reason change does to the strip -- because a test cannot make a +/// sound card fail on cue; the caching around it is the same comparison plus the +/// source guard below. +#[test] +fn a_device_that_dies_under_an_open_file_says_so_and_unsays_it() { + let mut notices: std::collections::VecDeque = + ["OPENED take.mp4".into()].into_iter().collect(); + let lost = audio_lost_line("AUDIO_LOST"); + // The file was fine, and then the sound is gone: the reason arrives between + // two frames. + audio_change_notice(&mut notices, None, Some("AUDIO_LOST")); + assert_eq!( + notices.back().map(|notice| &***notice), + Some(lost.as_str()), + "a reason that arrives mid-session is never pushed" + ); + assert!( + lost.starts_with("AUDIO LOST") && lost.ends_with(&audio_tail("AUDIO_LOST")), + "the standing line lost its state word, or stopped sharing \ + `audio_notice`'s own tail" + ); + // The same reason read again on the next frame is not news. + audio_change_notice(&mut notices, Some("AUDIO_LOST"), Some("AUDIO_LOST")); + assert_eq!(notices.len(), 2, "one death was announced twice"); + // The re-arm takes: the line goes with it, and nothing else does. + audio_change_notice(&mut notices, Some("AUDIO_LOST"), None); + assert_eq!( + notices.iter().map(|notice| &***notice).collect::>(), + vec!["OPENED take.mp4"], + "the NO AUDIO line is still on the strip over a film that plays" + ); + // A second death later says so again. + audio_change_notice(&mut notices, None, Some("AUDIO_LOST")); + assert_eq!(notices.back().map(|notice| &***notice), Some(lost.as_str())); + // The wiring: the pump is the one place every frame passes through, and a + // watcher nothing calls is a message nobody reads. + assert!( + fn_body("pump").contains("self.watch_audio();"), + "the pump stopped watching the engine's sound reason, so a device that \ + dies mid-session is silent again" + ); + assert!( + src_text("player/library.rs") + .matches("self.seed_audio_watch(&session);") + .count() + == 2, + "an open path stopped seeding the watch, so its own NO AUDIO line comes \ + back a frame later as a second one" + ); +} + /// The toast tail an audio import earns: rate, channel count and rounded /// length, in the words the IMPORTED line appends them in /// ([`Player::take_import`], [`Player::install_media`]). A film gets none of @@ -32,8 +96,8 @@ fn an_audio_import_names_its_rate_channels_and_length() { /// stuck read and never as a frozen window. #[test] fn an_import_line_says_a_stage_has_stopped_moving_and_only_then() { - use std::sync::Arc; use std::sync::atomic::{AtomicU8, Ordering::Relaxed}; + use std::sync::Arc; let stage = Arc::new(AtomicU8::new(ImportStage::Header as u8)); let started = Instant::now() - Duration::from_secs(9); let mut import = Import { @@ -303,7 +367,10 @@ fn a_strand_slot_on_a_single_strand_clip_is_the_clip_s_own_colour() { // The clip's own slot is never redirected -- it is what the fallback // lands on. assert_eq!(VizSlot::Main.picked(one), VizSlot::Main); - assert_eq!(VizSlot::Strand.picked(with_viz_paint_strands(one, 2)), VizSlot::Strand); + assert_eq!( + VizSlot::Strand.picked(with_viz_paint_strands(one, 2)), + VizSlot::Strand + ); } /// A seek says nothing until it has stood: an ordinary one is a flicker and @@ -326,8 +393,8 @@ fn a_seek_says_so_only_once_it_has_stood() { /// fifty-one seconds on a 25 GB film. #[test] fn a_silence_card_is_up_while_its_scan_runs_and_says_where_it_has_got_to() { - use std::sync::Arc; use std::sync::atomic::Ordering::Relaxed; + use std::sync::Arc; let progress = Arc::new(engine::silence::Progress::default()); // Two hours and eight minutes, as the header claims it. progress.total.store(7680, Relaxed); @@ -757,11 +824,9 @@ fn every_codec_row_is_offered_or_says_why_not() { detail.contains("ec-vorbis"), "the row names the encoder like every other live one: {detail}" ); - assert!( - FORMATS - .into_iter() - .any(|(row, _, _, detail)| row.contains(&Format::Hevc) && detail.contains("intra")) - ); + assert!(FORMATS + .into_iter() + .any(|(row, _, _, detail)| row.contains(&Format::Hevc) && detail.contains("intra"))); // The destination follows the format and keeps the stem, mp4 included. assert_eq!( retarget(std::path::Path::new("/a/take.export.mp4"), Format::Wav),