forge
Explore
Log in

Don't you dare go Hollow

A device that dies under an open file says so, and unsays it

Browse files at this commit

c80d1f748001eea7c9d9f3fc8363f9d3a01896e5Ahmet Kaan GümüşParent: ceffb92
  • fix
    • a device that dies under an open file says so, and unsays it
Show full message
🐛 fix(audio): a device that dies under an open file says so, and unsays it

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.rsmodified+9 -0
@@ -790,6 +790,12 @@ struct Player {
790790 /// The active session's identity, for the above: bumped by every door that
791791 /// swaps `session` or `preview_session`.
792792 session_gen: u64,
793+ /// The engine's own word on the sound device, as the last pump saw it: the
794+ /// cache [`Player::watch_audio`] compares each frame against so a reason is
795+ /// announced once when it *changes* and taken back when it clears. `None`
796+ /// while the sound is up, and seeded by `Player::seed_audio_watch` at every
797+ /// open, whose own line already carries the same fact.
798+ audio_reason: Option<String>,
793799 /// Which of the clip's two inks the look section's wheel and colour field
794800 /// act on ([`VizSlot`]): the clip's own colour, or the ring's dual half.
795801 /// The pick stays picked across clips, the way the dock tab's does.
@@ -1119,6 +1125,9 @@ fn main() {
11191125 picture_box: Rc::default(),
11201126 shot_pending: None,
11211127 session_gen: 0,
1128+ // Nothing heard yet: the first pump seeds it off the session
1129+ // it serves.
1130+ audio_reason: None,
11221131 viz_slot: VizSlot::Main,
11231132 viz_hex_edit: None,
11241133 pending_viz: None,
crates/app/src/notices.rsmodified+46 -5
@@ -46,9 +46,48 @@ fn is_completion(message: &str) -> bool {
4646 /// decode: it plays perfectly, in silence, and that is the one thing the window
4747 /// would otherwise never say (the engine's own word for it, verbatim).
4848 pub(crate) fn audio_notice(session: &PlaybackSession) -> Option<String> {
49- session
50- .audio_disabled_reason()
51- .map(|reason| format!(" — NO AUDIO: {reason}"))
49+ session.audio_disabled_reason().map(audio_tail)
50+}
51+
52+/// [`audio_notice`]'s own tail, for a reason already read -- the one place the
53+/// engine's word is spelled into a line, so the open path and the mid-session
54+/// one cannot drift apart.
55+pub(crate) fn audio_tail(reason: &str) -> String {
56+ format!(" — NO AUDIO: {reason}")
57+}
58+
59+/// The same fact said on its own, for a device that dies *after* a file was
60+/// opened: [`audio_notice`] is a tail to append to "OPENED x", and a death
61+/// mid-session has no line to hang off -- so it leads with the state word the
62+/// strip reads first, and the tail is the same one.
63+pub(crate) fn audio_lost_line(reason: &str) -> String {
64+ format!("AUDIO LOST{}", audio_tail(reason))
65+}
66+
67+/// What a *change* in the engine's sound reason does to the notice queue, as a
68+/// pure step: the death line pushed when a reason arrives, the same line taken
69+/// back when the reason clears ([`PlaybackSession::audio_disabled_reason`]'s
70+/// `AUDIO_LOST` and its own re-arm). [`Player::watch_audio`] is the only caller
71+/// and owns the caching; this owns the queue, so a test can drive both
72+/// directions without a window and without a device that dies on cue.
73+pub(crate) fn audio_change_notice(
74+ notices: &mut std::collections::VecDeque<SharedString>,
75+ was: Option<&str>,
76+ now: Option<&str>,
77+) {
78+ match (was, now) {
79+ (_, Some(reason)) if was != Some(reason) => {
80+ push_notice(notices, audio_lost_line(reason).into());
81+ }
82+ // The sound is back: the line goes with it, or the strip keeps saying NO
83+ // AUDIO over a film that plays. Only that line -- whatever else is
84+ // queued is none of this step's business.
85+ (Some(was), None) => {
86+ let line = audio_lost_line(was);
87+ notices.retain(|notice| notice.as_ref() != line.as_str());
88+ }
89+ _ => {}
90+ }
5291 }
5392
5493 /// The strip's own width is finite and its message is one line: what a
@@ -121,12 +160,14 @@ mod tests {
121160 #[test]
122161 fn a_long_name_loses_its_middle_rather_than_the_state_word() {
123162 assert_eq!(ledger_line("SNAP OFF"), "SNAP OFF");
124- let long = ledger_line("OPENED he_is_not_the_only_one_at_all_here.mp4 · 1 subtitle track(s)");
163+ let long =
164+ ledger_line("OPENED he_is_not_the_only_one_at_all_here.mp4 · 1 subtitle track(s)");
125165 assert!(long.starts_with("OPENED "), "{long}");
126166 assert!(long.ends_with(" · 1 subtitle track(s)"), "{long}");
127167 assert!(long.contains('…'), "{long}");
128168 assert!(
129- long.split(' ').all(|w| w.chars().count() <= LEDGER_WORD_MAX),
169+ long.split(' ')
170+ .all(|w| w.chars().count() <= LEDGER_WORD_MAX),
130171 "{long}"
131172 );
132173 }
crates/app/src/player/library.rsmodified+10 -0
@@ -1481,6 +1481,11 @@ impl Player {
14811481 // Read before the session moves: a file that plays silent says
14821482 // so here or nowhere.
14831483 let silent = audio_notice(&session);
1484+ // The watch's own cache, seeded here rather than on the first
1485+ // pump after: this line already carried the fact, so a reason
1486+ // unchanged across the open is not announced a second time
1487+ // ([`Player::seed_audio_watch`]).
1488+ self.seed_audio_watch(&session);
14841489 // A file replaces the one that was open, and track 3 of that one
14851490 // is not track 3 of this.
14861491 self.sub_track = 0;
@@ -1795,6 +1800,11 @@ impl Player {
17951800 // As at the file door above: the window watches in real time.
17961801 session.drop_late_pictures(true);
17971802 let silent = audio_notice(&session);
1803+ // The watch's own cache, seeded here rather than on the first
1804+ // pump after: this line already carried the fact, so a reason
1805+ // unchanged across the open is not announced a second time
1806+ // ([`Player::seed_audio_watch`]).
1807+ self.seed_audio_watch(&session);
17981808 // A project is named after itself but still exports beside its
17991809 // media: that is the only place an export has ever landed.
18001810 self.export_path = retarget(&export_path(&session.sources()[0].path), self.format);
crates/app/src/player/transport.rsmodified+55 -0
@@ -25,12 +25,67 @@ impl Player {
2525 .map_or(self.fps, |s| s.meta().frame_rate)
2626 }
2727
28+ /// The engine's own word on the sound device, for the session the pump
29+ /// serves ([`Player::active_session`]).
30+ fn sound_reason(&self) -> Option<&str> {
31+ self.active_session()
32+ .and_then(PlaybackSession::audio_disabled_reason)
33+ }
34+
35+ /// The engine's word on the sound device, watched rather than asked once.
36+ /// A file that *opens* silent says so in its own open line
37+ /// ([`audio_notice`], `library.rs`), but a device that dies mid-session used
38+ /// to leave the window playing on in silence with nothing said at all: the
39+ /// engine knew (it sets its reason to its lost-device word and clears it
40+ /// again when its own re-arm takes) and no consumer ever looked.
41+ ///
42+ /// Called from [`Player::pump`] because that is the one place every frame
43+ /// passes through, and written to cost an ordinary frame nothing: one read of
44+ /// an `Option<&str>` and one compare, with the strings built only on the
45+ /// change itself.
46+ pub(crate) fn watch_audio(&mut self) {
47+ if self.sound_reason() == self.audio_reason.as_deref() {
48+ return;
49+ }
50+ // Re-read rather than carry a borrow across the write: this line is only
51+ // reached on the change, so the allocation it costs is the news itself.
52+ let reason = self.sound_reason().map(str::to_owned);
53+ let was = std::mem::replace(&mut self.audio_reason, reason);
54+ audio_change_notice(
55+ &mut self.notices,
56+ was.as_deref(),
57+ self.audio_reason.as_deref(),
58+ );
59+ }
60+
61+ /// [`Player::watch_audio`]'s cache, seeded at the moment an open's own line
62+ /// carried the same fact: a reason unchanged across the open is not news, so
63+ /// only the *clearing* half runs here -- a line left over from the file
64+ /// before it comes off the strip.
65+ pub(crate) fn seed_audio_watch(&mut self, session: &PlaybackSession) {
66+ let was = std::mem::replace(
67+ &mut self.audio_reason,
68+ session.audio_disabled_reason().map(str::to_owned),
69+ );
70+ if self.audio_reason.is_none() {
71+ if let Some(was) = was {
72+ let line = audio_lost_line(&was);
73+ self.notices
74+ .retain(|notice| notice.as_ref() != line.as_str());
75+ }
76+ }
77+ }
78+
2879 /// Catches the display up to the clock: everything already due is taken off
2980 /// the channel and only the last of them is shown, which *is* the
3081 /// drop-when-behind policy. A frame that is not due yet waits in `held`, and
3182 /// while the clock is paused *nothing* is due -- a repaint re-presents the
3283 /// frame already on screen, whatever asked for the repaint.
3384 pub(crate) fn pump(&mut self, window: &mut Window) {
85+ // Before the drain, on the one read that does not need the session
86+ // mutably: the sound can die at any point in a frame, and the notice it
87+ // earns is owed to the frame it died in.
88+ self.watch_audio();
3489 // Where the transport was before this drain, so the crossing into
3590 // `Ended` can be recognised as the one transition it is.
3691 let was = self.transport();
crates/app/src/tests/media.rsmodified+75 -10
@@ -6,11 +6,75 @@ use crate::player::library::{
66 auto_proxies_pref_path, load_auto_proxies_pref, save_auto_proxies_pref,
77 };
88 use crate::subs::{
9- SUB_SIZE_RANGE, audio_import_tail, load_subtitle_style, save_subtitle_style, sub_line_h_for,
10- subtitle_style_path,
9+ audio_import_tail, load_subtitle_style, save_subtitle_style, sub_line_h_for,
10+ subtitle_style_path, SUB_SIZE_RANGE,
1111 };
1212 use crate::ui::preview::{bgra_to_rgba, screenshot_path};
1313
14+/// The notice queue's own half of the sound watch, and the three words it is
15+/// spelled with -- reached by name because the app's own glob does not carry
16+/// them (`notices` is a private module re-exported for the window).
17+use crate::notices::{audio_change_notice, audio_lost_line, audio_tail};
18+
19+/// A sound device that dies under a file that is already open says so, and stops
20+/// saying so when the engine's own re-arm takes. The engine sets its
21+/// `audio_disabled_reason` to its lost-device word on a death and clears it on a
22+/// successful reopen, and nothing on this side ever looked at it: the two call
23+/// sites were the *open* paths, so a mid-session death played on in silence with
24+/// no message at all.
25+///
26+/// Driven through the queue step itself -- [`audio_change_notice`] is the whole
27+/// of what a reason change does to the strip -- because a test cannot make a
28+/// sound card fail on cue; the caching around it is the same comparison plus the
29+/// source guard below.
30+#[test]
31+fn a_device_that_dies_under_an_open_file_says_so_and_unsays_it() {
32+ let mut notices: std::collections::VecDeque<gpui::SharedString> =
33+ ["OPENED take.mp4".into()].into_iter().collect();
34+ let lost = audio_lost_line("AUDIO_LOST");
35+ // The file was fine, and then the sound is gone: the reason arrives between
36+ // two frames.
37+ audio_change_notice(&mut notices, None, Some("AUDIO_LOST"));
38+ assert_eq!(
39+ notices.back().map(|notice| &***notice),
40+ Some(lost.as_str()),
41+ "a reason that arrives mid-session is never pushed"
42+ );
43+ assert!(
44+ lost.starts_with("AUDIO LOST") && lost.ends_with(&audio_tail("AUDIO_LOST")),
45+ "the standing line lost its state word, or stopped sharing \
46+ `audio_notice`'s own tail"
47+ );
48+ // The same reason read again on the next frame is not news.
49+ audio_change_notice(&mut notices, Some("AUDIO_LOST"), Some("AUDIO_LOST"));
50+ assert_eq!(notices.len(), 2, "one death was announced twice");
51+ // The re-arm takes: the line goes with it, and nothing else does.
52+ audio_change_notice(&mut notices, Some("AUDIO_LOST"), None);
53+ assert_eq!(
54+ notices.iter().map(|notice| &***notice).collect::<Vec<_>>(),
55+ vec!["OPENED take.mp4"],
56+ "the NO AUDIO line is still on the strip over a film that plays"
57+ );
58+ // A second death later says so again.
59+ audio_change_notice(&mut notices, None, Some("AUDIO_LOST"));
60+ assert_eq!(notices.back().map(|notice| &***notice), Some(lost.as_str()));
61+ // The wiring: the pump is the one place every frame passes through, and a
62+ // watcher nothing calls is a message nobody reads.
63+ assert!(
64+ fn_body("pump").contains("self.watch_audio();"),
65+ "the pump stopped watching the engine's sound reason, so a device that \
66+ dies mid-session is silent again"
67+ );
68+ assert!(
69+ src_text("player/library.rs")
70+ .matches("self.seed_audio_watch(&session);")
71+ .count()
72+ == 2,
73+ "an open path stopped seeding the watch, so its own NO AUDIO line comes \
74+ back a frame later as a second one"
75+ );
76+}
77+
1478 /// The toast tail an audio import earns: rate, channel count and rounded
1579 /// length, in the words the IMPORTED line appends them in
1680 /// ([`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() {
3296 /// stuck read and never as a frozen window.
3397 #[test]
3498 fn an_import_line_says_a_stage_has_stopped_moving_and_only_then() {
35- use std::sync::Arc;
3699 use std::sync::atomic::{AtomicU8, Ordering::Relaxed};
100+ use std::sync::Arc;
37101 let stage = Arc::new(AtomicU8::new(ImportStage::Header as u8));
38102 let started = Instant::now() - Duration::from_secs(9);
39103 let mut import = Import {
@@ -303,7 +367,10 @@ fn a_strand_slot_on_a_single_strand_clip_is_the_clip_s_own_colour() {
303367 // The clip's own slot is never redirected -- it is what the fallback
304368 // lands on.
305369 assert_eq!(VizSlot::Main.picked(one), VizSlot::Main);
306- assert_eq!(VizSlot::Strand.picked(with_viz_paint_strands(one, 2)), VizSlot::Strand);
370+ assert_eq!(
371+ VizSlot::Strand.picked(with_viz_paint_strands(one, 2)),
372+ VizSlot::Strand
373+ );
307374 }
308375
309376 /// 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() {
326393 /// fifty-one seconds on a 25 GB film.
327394 #[test]
328395 fn a_silence_card_is_up_while_its_scan_runs_and_says_where_it_has_got_to() {
329- use std::sync::Arc;
330396 use std::sync::atomic::Ordering::Relaxed;
397+ use std::sync::Arc;
331398 let progress = Arc::new(engine::silence::Progress::default());
332399 // Two hours and eight minutes, as the header claims it.
333400 progress.total.store(7680, Relaxed);
@@ -757,11 +824,9 @@ fn every_codec_row_is_offered_or_says_why_not() {
757824 detail.contains("ec-vorbis"),
758825 "the row names the encoder like every other live one: {detail}"
759826 );
760- assert!(
761- FORMATS
762- .into_iter()
763- .any(|(row, _, _, detail)| row.contains(&Format::Hevc) && detail.contains("intra"))
764- );
827+ assert!(FORMATS
828+ .into_iter()
829+ .any(|(row, _, _, detail)| row.contains(&Format::Hevc) && detail.contains("intra")));
765830 // The destination follows the format and keeps the stem, mp4 included.
766831 assert_eq!(
767832 retarget(std::path::Path::new("/a/take.export.mp4"), Format::Wav),