diff --git a/crates/app/src/main.rs b/crates/app/src/main.rs index 0656641..f059632 100644 --- a/crates/app/src/main.rs +++ b/crates/app/src/main.rs @@ -68,11 +68,11 @@ use engine::transform::TransformParams; use engine::{Clip, Codec, ExportHandle, Frame, MediaBitrate, PlaybackSession}; use gpui::{ AnyElement, App, Application, Bounds, ClickEvent, Context, Corners, CursorStyle, Div, - DragMoveEvent, FocusHandle, KeyDownEvent, MouseButton, MouseDownEvent, - MouseExitEvent, MouseMoveEvent, MouseUpEvent, PathBuilder, Pixels, Point, RenderImage, - ScrollDelta, ScrollHandle, ScrollWheelEvent, SharedString, Size, Stateful, TextAlign, - TitlebarOptions, Window, WindowBounds, WindowOptions, canvas, div, point, prelude::*, px, - relative, rgb, rgba, size, + DragMoveEvent, FocusHandle, KeyDownEvent, MouseButton, MouseDownEvent, MouseExitEvent, + MouseMoveEvent, MouseUpEvent, PathBuilder, Pixels, Point, RenderImage, ScrollDelta, + ScrollHandle, ScrollWheelEvent, SharedString, Size, Stateful, TextAlign, TitlebarOptions, + Window, WindowBounds, WindowOptions, canvas, div, point, prelude::*, px, relative, rgb, rgba, + size, }; struct Player { @@ -796,6 +796,13 @@ struct Player { /// while the sound is up; seeded by [`Player::session_swapped`], which every /// door that installs or drops a session already calls. audio_reason: Option, + /// The engine's last word on the *picture*, cached so a frame can tell a + /// reason that changed from one that was merely read again. Keyed to + /// `session_gen` for the same reason `audio_watch_gen` is: `pump` serves + /// the preview while one is up and the timeline otherwise, and an unkeyed + /// cache would read the other session's reason. + decode_reason_cache: Option, + decode_watch_gen: u64, /// Which session generation (`session_gen`) the cached reason above describes: /// the watch is keyed to the session it is a statement about, so a preview /// opening or closing seeds the cache instead of announcing the *other* @@ -1134,6 +1141,8 @@ fn main() { // it serves. audio_reason: None, audio_watch_gen: 0, + decode_reason_cache: None, + decode_watch_gen: 0, 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 29e38a3..525b669 100644 --- a/crates/app/src/notices.rs +++ b/crates/app/src/notices.rs @@ -64,11 +64,30 @@ pub(crate) fn audio_lost_line(reason: &str) -> String { format!("AUDIO LOST{}", audio_tail(reason)) } -/// What a frame of the sound watch decides, as a pure value: the cache's own +/// A decode failure that arrives *after* a file was opened, said on its own +/// line: the same shape as [`audio_lost_line`] and for the same reason -- a +/// death mid-session has no open line to hang a tail off, so it leads with the +/// state word the strip reads first. +/// +/// The picture twin of the sound line above, and it exists because a decode +/// failure used to reach nobody at all: the worker printed it to stderr and +/// dropped the channel, and a closed channel is what a finished film looks like +/// from the app's side. A file with a damaged middle froze on its last good +/// picture and the transport said ENDED. The reason is the engine's own words, +/// verbatim -- the app spells none of them. +pub(crate) fn decode_failed_line(reason: &str) -> String { + format!("DECODE FAILED — {reason}") +} + +/// What a frame of a reason watch decides, as a pure value: the cache's own /// session generation against the frame's says whether the two are even talking /// about the same session, and only then what the reason did. +/// +/// One type for every reason the engine states, not one per source: the sound +/// device and the decode worker both write a string the app has to notice +/// changed, and the thing that decides is the same comparison in both cases. #[derive(Clone, Copy, PartialEq, Eq, Debug)] -pub(crate) enum AudioStep { +pub(crate) enum ReasonStep { /// The cache describes the session *before* this one /// (`Player::session_swapped`): it is seeded and nothing is announced -- a /// preview that opens or closes is not a device that came back. @@ -81,7 +100,8 @@ pub(crate) enum AudioStep { Nothing, } -/// [`Player::watch_audio`]'s whole decision, pure: `cached_gen` is the session +/// [`Player::watch_audio`]'s and [`Player::watch_decode`]'s whole decision, +/// pure: `cached_gen` is the session /// generation the cache describes, `session_gen` the one the frame is serving, and /// two reasons are what the engine says the sound is (`None` for sound that is /// up). @@ -91,20 +111,20 @@ pub(crate) enum AudioStep { /// so a flip reads a reason that was never about the cached session at all -- a /// live device on one side and a dead one on the other is not a change, it is /// two different questions. -pub(crate) fn audio_step( +pub(crate) fn reason_step( cached_gen: u64, session_gen: u64, was: Option<&str>, now: Option<&str>, -) -> AudioStep { +) -> ReasonStep { if cached_gen != session_gen { - return AudioStep::Seed; + return ReasonStep::Seed; } match (was, now) { - (Some(was), Some(now)) if was == now => AudioStep::Nothing, - (_, Some(_)) => AudioStep::Push, - (Some(_), None) => AudioStep::Clear, - (None, None) => AudioStep::Nothing, + (Some(was), Some(now)) if was == now => ReasonStep::Nothing, + (_, Some(_)) => ReasonStep::Push, + (Some(_), None) => ReasonStep::Clear, + (None, None) => ReasonStep::Nothing, } } @@ -140,6 +160,32 @@ pub(crate) fn audio_change_notice( } } +/// What a *change* in the engine's **decode** reason does to the notice queue: +/// the line pushed when a reason arrives, the same line taken back when the +/// reason clears. [`audio_change_notice`]'s twin, step for step, because the +/// thing that changed is the same kind of thing -- a reason the engine started +/// or stopped stating between two frames. +/// +/// The retraction is what keeps this from becoming noise: a decode that fails, +/// is seeked away from and succeeds has to take its line back, or the strip +/// keeps saying DECODE FAILED over a film that plays. +pub(crate) fn decode_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, decode_failed_line(reason).into()); + } + (Some(was), None) => { + let line = decode_failed_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 /// too-long line must never do is lose its *state* word, which sits at the /// front, or stop mid-word with nothing saying it stopped (user report, diff --git a/crates/app/src/player/transport.rs b/crates/app/src/player/transport.rs index 1e51a14..4bef94e 100644 --- a/crates/app/src/player/transport.rs +++ b/crates/app/src/player/transport.rs @@ -50,19 +50,19 @@ impl Player { /// without the key a preview opening or closing would read the *other* /// session's reason and announce a device loss that never happened. pub(crate) fn watch_audio(&mut self) { - let step = audio_step( + let step = reason_step( self.audio_watch_gen, self.session_gen, self.audio_reason.as_deref(), self.sound_reason(), ); - if step == AudioStep::Nothing { + if step == ReasonStep::Nothing { return; } let was = self.audio_reason.take(); self.audio_reason = self.sound_reason().map(str::to_owned); self.audio_watch_gen = self.session_gen; - if step == AudioStep::Seed { + if step == ReasonStep::Seed { // A session swap is not a change: the new session's own open line // carries its reason, so only a line left over from the session // before is taken back -- and only when this one's sound is up. @@ -82,6 +82,61 @@ impl Player { ); } + /// The engine's word on the *picture*, watched rather than asked once -- + /// [`Self::watch_audio`]'s twin, and the reason a damaged file stops being + /// indistinguishable from a finished one. + /// + /// `PlaybackSession::video_failure_reason` is the writer's side: a decode + /// worker that gave up on a broken packet used to print it to stderr and + /// drop the channel, and a closed channel is what a film reaching its own + /// end looks like from here -- so the transport said ENDED, the picture + /// froze on the last good frame, and nothing in the window said why. + /// + /// Keyed to the session exactly as the sound watch is, for the same reason: + /// `pump` serves the preview while one is up and the timeline otherwise, so + /// an unkeyed cache would read the other session's reason and announce a + /// failure that never happened. + pub(crate) fn watch_decode(&mut self) { + let now = self.decode_reason(); + let step = reason_step( + self.decode_watch_gen, + self.session_gen, + self.decode_reason_cache.as_deref(), + now.as_deref(), + ); + if step == ReasonStep::Nothing { + return; + } + let was = self.decode_reason_cache.take(); + self.decode_reason_cache = now; + self.decode_watch_gen = self.session_gen; + if step == ReasonStep::Seed { + // A session swap is not a change: the new session's own open line + // carries its reason, so only a line left over from the session + // before is taken back -- and only when this one is healthy. + if self.decode_reason_cache.is_none() + && let Some(was) = was + { + let line = decode_failed_line(&was); + self.notices + .retain(|notice| notice.as_ref() != line.as_str()); + } + return; + } + decode_change_notice( + &mut self.notices, + was.as_deref(), + self.decode_reason_cache.as_deref(), + ); + } + + /// The engine's word on the picture for the session the pump serves, read + /// off that session and no other -- the same rule as [`Self::sound_reason`]. + fn decode_reason(&self) -> Option { + self.active_session() + .and_then(PlaybackSession::video_failure_reason) + } + /// 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 @@ -90,8 +145,10 @@ impl Player { 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. + // earns is owed to the frame it died in. The picture can fail for the + // same reason and earns the same courtesy. self.watch_audio(); + self.watch_decode(); // 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 e70b2b3..cd6b8fd 100644 --- a/crates/app/src/tests/media.rs +++ b/crates/app/src/tests/media.rs @@ -6,15 +6,84 @@ use crate::player::library::{ auto_proxies_pref_path, load_auto_proxies_pref, save_auto_proxies_pref, }; use crate::subs::{ - audio_import_tail, load_subtitle_style, save_subtitle_style, sub_line_h_for, - subtitle_style_path, SUB_SIZE_RANGE, + SUB_SIZE_RANGE, audio_import_tail, load_subtitle_style, save_subtitle_style, sub_line_h_for, + subtitle_style_path, }; 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}; +use crate::notices::{ + audio_change_notice, audio_lost_line, audio_tail, decode_change_notice, decode_failed_line, +}; + +/// The picture twin of the sound watch above, and the same shape for the same +/// reason: a decode failure that arrives between two frames used to reach no +/// consumer at all, because the worker printed it to stderr and dropped the +/// channel -- and a closed channel is what a finished film looks like from the +/// app's side, so the transport said ENDED over a frozen frame with nothing in +/// the window saying why. +/// +/// Driven through the queue step, because a test cannot make a decoder fail on +/// cue; the engine's writer is the decode worker's, and +/// `engine::tests::decode_failure` observes it end to end on the engine side. +#[test] +fn a_decode_that_fails_under_an_open_file_says_so_and_unsays_it() { + let mut notices: std::collections::VecDeque = + ["OPENED take.mp4".into()].into_iter().collect(); + let failed = decode_failed_line("decode error at access unit 130"); + assert!( + failed.starts_with("DECODE FAILED"), + "the line lost the state word the strip reads first: {failed}" + ); + // The file was fine, and then the picture stopped: the reason arrives + // between two frames. + decode_change_notice(&mut notices, None, Some("decode error at access unit 130")); + assert_eq!( + notices.back().map(|notice| &***notice), + Some(failed.as_str()), + "a decode failure that arrives mid-session is never pushed" + ); + // The same reason read again on the next frame is not news. + decode_change_notice( + &mut notices, + Some("decode error at access unit 130"), + Some("decode error at access unit 130"), + ); + assert_eq!(notices.len(), 2, "one failure was announced twice"); + // Seeked away from, and it decodes again: the line goes with it, or the + // strip keeps saying DECODE FAILED over a film that plays. + decode_change_notice(&mut notices, Some("decode error at access unit 130"), None); + assert_eq!( + notices.iter().map(|notice| &***notice).collect::>(), + vec!["OPENED take.mp4"], + "the DECODE FAILED line is still on the strip over a film that plays" + ); + // The sound line is a different line and is none of this step's business. + audio_change_notice(&mut notices, None, Some("AUDIO_LOST")); + decode_change_notice(&mut notices, None, Some("decode error at access unit 7")); + decode_change_notice(&mut notices, Some("decode error at access unit 7"), None); + assert_eq!( + notices.iter().map(|notice| &***notice).collect::>(), + vec!["OPENED take.mp4", audio_lost_line("AUDIO_LOST").as_str()], + "one reason's retraction took the other one's line with it" + ); + // 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_decode();"), + "the pump stopped watching the engine's decode reason, so a corrupt \ + file is a silent freeze again" + ); + // ...and it reads the reason off the session the pump is serving, the same + // rule the sound watch follows. + assert!( + fn_body("decode_reason").contains("self.active_session()"), + "the decode watch reads a reason off a session the pump is not \ + serving, so a preview flip announces the other session's failure" + ); +} /// A reason that changes under a session that is already open says so, and stops /// saying so when it clears. This side is the *reader*: the writer of a reason is @@ -72,7 +141,7 @@ fn a_device_that_dies_under_an_open_file_says_so_and_unsays_it() { // cannot be answered as if it were the cached session's own change. let watch = fn_body("watch_audio"); let args = watch - .split_once("audio_step(") + .split_once("reason_step(") .expect( "the watch decides for itself instead of through the pure step the flip test drives", ) @@ -106,29 +175,41 @@ fn a_device_that_dies_under_an_open_file_says_so_and_unsays_it() { /// speaks. #[test] fn a_preview_flip_is_not_a_device_that_came_back() { - use crate::notices::{audio_step, AudioStep}; + use crate::notices::{ReasonStep, reason_step}; // A swap, every way round: seeded, never announced. - assert_eq!(audio_step(7, 8, None, Some("AUDIO_LOST")), AudioStep::Seed); - assert_eq!(audio_step(7, 8, Some("AUDIO_LOST"), None), AudioStep::Seed); assert_eq!( - audio_step(7, 8, Some("AUDIO_LOST"), Some("AUDIO_LOST")), - AudioStep::Seed, + reason_step(7, 8, None, Some("AUDIO_LOST")), + ReasonStep::Seed + ); + assert_eq!( + reason_step(7, 8, Some("AUDIO_LOST"), None), + ReasonStep::Seed + ); + assert_eq!( + reason_step(7, 8, Some("AUDIO_LOST"), Some("AUDIO_LOST")), + ReasonStep::Seed, "a flip whose reason reads the same on both sides is still a flip" ); // The same session, the reason arriving, changing, and going. - assert_eq!(audio_step(7, 7, None, Some("AUDIO_LOST")), AudioStep::Push); assert_eq!( - audio_step(7, 7, Some("AUDIO_LOST"), Some("device gone")), - AudioStep::Push + reason_step(7, 7, None, Some("AUDIO_LOST")), + ReasonStep::Push + ); + assert_eq!( + reason_step(7, 7, Some("AUDIO_LOST"), Some("device gone")), + ReasonStep::Push + ); + assert_eq!( + reason_step(7, 7, Some("AUDIO_LOST"), None), + ReasonStep::Clear ); - assert_eq!(audio_step(7, 7, Some("AUDIO_LOST"), None), AudioStep::Clear); assert_eq!( - audio_step(7, 7, Some("AUDIO_LOST"), Some("AUDIO_LOST")), - AudioStep::Nothing, + reason_step(7, 7, Some("AUDIO_LOST"), Some("AUDIO_LOST")), + ReasonStep::Nothing, "the same reason read again on the next frame is not news" ); - assert_eq!(audio_step(7, 7, None, None), AudioStep::Nothing); - assert_eq!(audio_step(0, 0, None, None), AudioStep::Nothing); + assert_eq!(reason_step(7, 7, None, None), ReasonStep::Nothing); + assert_eq!(reason_step(0, 0, None, None), ReasonStep::Nothing); } /// The toast tail an audio import earns: rate, channel count and rounded @@ -152,8 +233,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::atomic::{AtomicU8, Ordering::Relaxed}; use std::sync::Arc; + use std::sync::atomic::{AtomicU8, Ordering::Relaxed}; let stage = Arc::new(AtomicU8::new(ImportStage::Header as u8)); let started = Instant::now() - Duration::from_secs(9); let mut import = Import { @@ -364,8 +445,8 @@ fn the_wheel_draws_the_sample_a_drag_is_holding() { #[test] fn the_strand_slot_starts_at_the_complement_and_the_write_chooses_it() { use engine::decode::{ - viz_hue_ui, viz_sat_ui, viz_strand_hue_ui, viz_strand_sat_ui, viz_strand_set, - with_viz_paint_hue, with_viz_paint_strands, VIZ_PAINT_AZURE, + VIZ_PAINT_AZURE, viz_hue_ui, viz_sat_ui, viz_strand_hue_ui, viz_strand_sat_ui, + viz_strand_set, with_viz_paint_hue, with_viz_paint_strands, }; let paint = with_viz_paint_strands(VIZ_PAINT_AZURE, 2); @@ -407,8 +488,8 @@ fn the_strand_slot_starts_at_the_complement_and_the_write_chooses_it() { #[test] fn a_strand_slot_on_a_single_strand_clip_is_the_clip_s_own_colour() { use engine::decode::{ - viz_hue_ui, viz_sat_ui, viz_strand_set, viz_strands_of, with_viz_paint_hue, - with_viz_paint_sat, with_viz_paint_strands, VIZ_PAINT_AZURE, + VIZ_PAINT_AZURE, viz_hue_ui, viz_sat_ui, viz_strand_set, viz_strands_of, + with_viz_paint_hue, with_viz_paint_sat, with_viz_paint_strands, }; let one = with_viz_paint_strands( @@ -449,8 +530,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::atomic::Ordering::Relaxed; use std::sync::Arc; + use std::sync::atomic::Ordering::Relaxed; let progress = Arc::new(engine::silence::Progress::default()); // Two hours and eight minutes, as the header claims it. progress.total.store(7680, Relaxed); @@ -880,9 +961,11 @@ 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),