diff --git a/crates/engine/src/audio.rs b/crates/engine/src/audio.rs index 3e9f342..ab4f99e 100644 --- a/crates/engine/src/audio.rs +++ b/crates/engine/src/audio.rs @@ -640,7 +640,11 @@ impl AudioSession { decodable: matches!(track.channels, 1 | 2), }]); } - let Ok(track) = SymTrack::open(path) else { + // No audio track lists as the silent source it is; a track that + // will not open at all is an `Err` here, as it is in + // [`Track::open`] -- a listing that quietly came back empty for a + // broken file is how a lane ends up silent with nothing to go on. + let Some(track) = SymTrack::open(path)? else { return Ok(Vec::new()); }; return Ok(vec![StreamInfo { @@ -653,7 +657,7 @@ impl AudioSession { }]); } if crate::is_audio(path) { - let track = SymTrack::open(path)?; + let track = SymTrack::open_required(path)?; return Ok(vec![StreamInfo { index: 0, codec: track.codec.into(), @@ -1025,8 +1029,8 @@ impl Track { // decoder straight out of the blocks ([`MkvAc3Track`]). Sound neither // one can decode (Opus, DTS) leaves the source the silent one it always // was, which `AudioSession::unsupported` puts into words, rather than - // failing the import of a file whose picture is fine -- and so does an - // AC-3 track that will not open, a laced block say. + // failing the import of a file whose picture is fine. A track that will + // not open at all is *not* in that group: see below. if crate::demux::is_matroska(path) { if stream > 0 { return Err(format!("{}: audio stream {stream} of 1 stream", path.display()).into()); @@ -1034,24 +1038,27 @@ impl Track { match MkvAc3Track::open(path) { Ok(Some(track)) => return Ok(Some(Self::Mkv(track))), Ok(None) => {} - // Named on the way past for the same reason symphonia's failure - // is, below: an AC-3 track that broke is a bug, not a codec we - // never had. + // Named on the way past rather than returned: symphonia gets the + // same file next and may read the track perfectly well (a laced + // block is [`MkvAc3Track`]'s limitation, not the file's), and + // if it cannot either, *its* `Err` is the one that comes back. Err(e) => eprintln!("matroska audio: {}: {e}", path.display()), } - // Logged on the way past: "silent" here covers both a codec nothing - // decodes and a track that genuinely broke, and only the second is a - // bug -- with no trace at all it reaches the user as a lane that - // drew no waveform and nothing else to go on. - return Ok(SymTrack::open(path) - .inspect_err(|e| eprintln!("matroska audio: {}: {e}", path.display())) - .ok() - .filter(|t| { - t.decoder() - .inspect_err(|e| eprintln!("matroska audio: {}: {e}", path.display())) - .is_ok() - }) - .map(Self::Sym)); + // Two things are silent here and no more: a file with no audio + // track at all, and a track whose codec nothing in this project + // decodes (Opus, DTS -- [`AudioSession::unsupported`] is what puts + // that into words). A track that will not *open* is an `Err`, the + // same answer the mp4 arm gives: turned into silence it would play + // and export as a hole with only an eprintln to say why, which is a + // bug wearing a feature's clothes. + let Some(track) = SymTrack::open(path)? else { + return Ok(None); + }; + if let Err(e) = track.decoder() { + eprintln!("matroska audio: {}: {e}", path.display()); + return Ok(None); + } + return Ok(Some(Self::Sym(track))); } let audio_file = crate::is_audio(path); // Ahead of the AAC reader, because that one answers "not AAC" for a @@ -1072,7 +1079,7 @@ impl Track { if stream > 0 { return Err(format!("{}: audio stream {stream} of 1 stream", path.display()).into()); } - SymTrack::open(path).map(|track| Some(Self::Sym(track))) + SymTrack::open_required(path).map(|track| Some(Self::Sym(track))) } fn sample_rate(&self) -> u32 { @@ -1197,7 +1204,13 @@ struct SymTrack { } impl SymTrack { - fn open(path: &Path) -> crate::Result { + /// `Ok(None)` for a container that opened and holds no audio track at all -- + /// a silent source, which is a thing a file is allowed to be. Everything + /// else that goes wrong here is an `Err`: a file that will not open, will + /// not parse, or carries a track it cannot describe is *broken*, and the + /// difference matters because silence is played and exported without a word + /// while an `Err` is shown ([`Track::open`]). + fn open(path: &Path) -> crate::Result> { let mss = MediaSourceStream::new(Box::new(File::open(path)?), Default::default()); // The extension is a hint, not a decision: the probe reads the magic and // is free to disagree, which is what makes a mislabelled file work. @@ -1211,9 +1224,9 @@ impl SymTrack { FormatOptions::default(), MetadataOptions::default(), )?; - let track = reader - .default_track(SymKind::Audio) - .ok_or_else(|| format!("{} has no audio track", path.display()))?; + let Some(track) = reader.default_track(SymKind::Audio) else { + return Ok(None); + }; let (track_id, num_frames, delay) = (track.id, track.num_frames, track.delay); let time_base = track .time_base @@ -1234,7 +1247,7 @@ impl SymTrack { let codec = symphonia::default::get_codecs() .get_audio_decoder(params.codec) .map_or("an unsupported format", |d| d.codec.info.short_name); - Ok(Self { + Ok(Some(Self { reader, track_id, sample_rate, @@ -1248,7 +1261,14 @@ impl SymTrack { time_base, params, codec, - }) + })) + } + + /// [`open`](Self::open) where having no audio track is itself the error: + /// what a standalone audio file is held to, since a song with no sound in + /// it is a broken file and not a silent source. + fn open_required(path: &Path) -> crate::Result { + Self::open(path)?.ok_or_else(|| format!("{} has no audio track", path.display()).into()) } /// A fresh decoder: seeking mid-stream leaves state that belongs to the diff --git a/crates/engine/tests/audio_decode.rs b/crates/engine/tests/audio_decode.rs index e02ea9a..80e9199 100644 --- a/crates/engine/tests/audio_decode.rs +++ b/crates/engine/tests/audio_decode.rs @@ -272,3 +272,40 @@ fn an_ac3_source_refuses_an_mp4_audio_copy() { "unhelpful refusal: {err}" ); } + +/// The two things a Matroska file with no *readable* sound can be, told apart: +/// a file with no audio track is the silent source it says it is, and a file +/// whose audio will not open at all is an error. They used to be the same +/// answer -- silence -- so a broken track played and exported as a hole with +/// nothing but an `eprintln` to say why. +#[test] +fn a_matroska_with_no_track_is_silent_and_a_broken_one_is_an_error() { + // No audio track at all: a picture and two subtitle tracks. Silent, and the + // stream picker offers nothing rather than refusing the file. + let subs = asset("test_subs.mkv"); + assert!( + AudioSession::probe(&subs, 0) + .expect("no audio is not an error") + .is_none(), + "an mkv with no audio track is a silent source" + ); + assert!(AudioSession::probe_streams(&subs).expect("list").is_empty()); + assert!( + AudioSession::open(&subs).expect("open").is_none(), + "no sound" + ); + + // ...and a file that will not parse at all, under the same extension: an + // `Err` a front-end can show, not a silent import. + let broken = std::env::temp_dir().join(format!("ve_broken_{}.mkv", std::process::id())); + std::fs::write(&broken, b"\x1a\x45\xdf\xa3not a matroska file at all").expect("write"); + let err = AudioSession::probe(&broken, 0) + .expect_err("a broken file must not pass for a silent one") + .to_string(); + assert!(!err.is_empty(), "the refusal says something"); + assert!( + AudioSession::probe_streams(&broken).is_err(), + "and so does the listing" + ); + std::fs::remove_file(&broken).unwrap(); +} diff --git a/crates/engine/tests/lanes.rs b/crates/engine/tests/lanes.rs index fe2f5fa..46ccf6f 100644 --- a/crates/engine/tests/lanes.rs +++ b/crates/engine/tests/lanes.rs @@ -52,8 +52,13 @@ fn out_path(name: &str, ext: &str) -> PathBuf { /// A hand-written v4 project: `V1` the whole of `test_av`, `V2` the middle /// third playing `test_av2` from its own frame 0, `A1` the whole of `test_av`. /// Exactly the file a user would write by hand, parsed by the real loader. -fn three_lane_file() -> PathBuf { - let path = out_path("project", "edith"); +/// +/// `name` is the caller's own, because [`out_path`] is unique per *run* and not +/// per test: two tests sharing one name run in parallel over one file, and the +/// first to finish deletes it out from under the second -- which then fails on +/// a file that is simply not there any more. +fn three_lane_file(name: &str) -> PathBuf { + let path = out_path(name, "edith"); // `source