A broken Matroska track is an error, not silence
Browse files at this commit941e8824cb2c76f3ac001beefc2950c1a1596ee1Parent: 685d4e6- fix
- a broken Matroska track is an error, not silence
3 changed files +93 -31
- crates/engine/src/audio.rs +47 -27
- crates/engine/tests/audio_decode.rs +37 -0
- crates/engine/tests/lanes.rs +9 -4
crates/engine/src/audio.rsmodified+47 -27
| @@ -640,7 +640,11 @@ impl AudioSession { | |||
| 640 | decodable: matches!(track.channels, 1 | 2), | 640 | decodable: matches!(track.channels, 1 | 2), |
| 641 | }]); | 641 | }]); |
| 642 | } | 642 | } |
| 643 | - let Ok(track) = SymTrack::open(path) else { | 643 | + // No audio track lists as the silent source it is; a track that |
| 644 | + // will not open at all is an `Err` here, as it is in | ||
| 645 | + // [`Track::open`] -- a listing that quietly came back empty for a | ||
| 646 | + // broken file is how a lane ends up silent with nothing to go on. | ||
| 647 | + let Some(track) = SymTrack::open(path)? else { | ||
| 644 | return Ok(Vec::new()); | 648 | return Ok(Vec::new()); |
| 645 | }; | 649 | }; |
| 646 | return Ok(vec![StreamInfo { | 650 | return Ok(vec![StreamInfo { |
| @@ -653,7 +657,7 @@ impl AudioSession { | |||
| 653 | }]); | 657 | }]); |
| 654 | } | 658 | } |
| 655 | if crate::is_audio(path) { | 659 | if crate::is_audio(path) { |
| 656 | - let track = SymTrack::open(path)?; | 660 | + let track = SymTrack::open_required(path)?; |
| 657 | return Ok(vec![StreamInfo { | 661 | return Ok(vec![StreamInfo { |
| 658 | index: 0, | 662 | index: 0, |
| 659 | codec: track.codec.into(), | 663 | codec: track.codec.into(), |
| @@ -1025,8 +1029,8 @@ impl Track { | |||
| 1025 | // decoder straight out of the blocks ([`MkvAc3Track`]). Sound neither | 1029 | // decoder straight out of the blocks ([`MkvAc3Track`]). Sound neither |
| 1026 | // one can decode (Opus, DTS) leaves the source the silent one it always | 1030 | // one can decode (Opus, DTS) leaves the source the silent one it always |
| 1027 | // was, which `AudioSession::unsupported` puts into words, rather than | 1031 | // was, which `AudioSession::unsupported` puts into words, rather than |
| 1028 | - // failing the import of a file whose picture is fine -- and so does an | 1032 | + // failing the import of a file whose picture is fine. A track that will |
| 1029 | - // AC-3 track that will not open, a laced block say. | 1033 | + // not open at all is *not* in that group: see below. |
| 1030 | if crate::demux::is_matroska(path) { | 1034 | if crate::demux::is_matroska(path) { |
| 1031 | if stream > 0 { | 1035 | if stream > 0 { |
| 1032 | return Err(format!("{}: audio stream {stream} of 1 stream", path.display()).into()); | 1036 | return Err(format!("{}: audio stream {stream} of 1 stream", path.display()).into()); |
| @@ -1034,24 +1038,27 @@ impl Track { | |||
| 1034 | match MkvAc3Track::open(path) { | 1038 | match MkvAc3Track::open(path) { |
| 1035 | Ok(Some(track)) => return Ok(Some(Self::Mkv(track))), | 1039 | Ok(Some(track)) => return Ok(Some(Self::Mkv(track))), |
| 1036 | Ok(None) => {} | 1040 | Ok(None) => {} |
| 1037 | - // Named on the way past for the same reason symphonia's failure | 1041 | + // Named on the way past rather than returned: symphonia gets the |
| 1038 | - // is, below: an AC-3 track that broke is a bug, not a codec we | 1042 | + // same file next and may read the track perfectly well (a laced |
| 1039 | - // never had. | 1043 | + // block is [`MkvAc3Track`]'s limitation, not the file's), and |
| 1044 | + // if it cannot either, *its* `Err` is the one that comes back. | ||
| 1040 | Err(e) => eprintln!("matroska audio: {}: {e}", path.display()), | 1045 | Err(e) => eprintln!("matroska audio: {}: {e}", path.display()), |
| 1041 | } | 1046 | } |
| 1042 | - // Logged on the way past: "silent" here covers both a codec nothing | 1047 | + // Two things are silent here and no more: a file with no audio |
| 1043 | - // decodes and a track that genuinely broke, and only the second is a | 1048 | + // track at all, and a track whose codec nothing in this project |
| 1044 | - // bug -- with no trace at all it reaches the user as a lane that | 1049 | + // decodes (Opus, DTS -- [`AudioSession::unsupported`] is what puts |
| 1045 | - // drew no waveform and nothing else to go on. | 1050 | + // that into words). A track that will not *open* is an `Err`, the |
| 1046 | - return Ok(SymTrack::open(path) | 1051 | + // same answer the mp4 arm gives: turned into silence it would play |
| 1047 | - .inspect_err(|e| eprintln!("matroska audio: {}: {e}", path.display())) | 1052 | + // and export as a hole with only an eprintln to say why, which is a |
| 1048 | - .ok() | 1053 | + // bug wearing a feature's clothes. |
| 1049 | - .filter(|t| { | 1054 | + let Some(track) = SymTrack::open(path)? else { |
| 1050 | - t.decoder() | 1055 | + return Ok(None); |
| 1051 | - .inspect_err(|e| eprintln!("matroska audio: {}: {e}", path.display())) | 1056 | + }; |
| 1052 | - .is_ok() | 1057 | + if let Err(e) = track.decoder() { |
| 1053 | - }) | 1058 | + eprintln!("matroska audio: {}: {e}", path.display()); |
| 1054 | - .map(Self::Sym)); | 1059 | + return Ok(None); |
| 1060 | + } | ||
| 1061 | + return Ok(Some(Self::Sym(track))); | ||
| 1055 | } | 1062 | } |
| 1056 | let audio_file = crate::is_audio(path); | 1063 | let audio_file = crate::is_audio(path); |
| 1057 | // Ahead of the AAC reader, because that one answers "not AAC" for a | 1064 | // Ahead of the AAC reader, because that one answers "not AAC" for a |
| @@ -1072,7 +1079,7 @@ impl Track { | |||
| 1072 | if stream > 0 { | 1079 | if stream > 0 { |
| 1073 | return Err(format!("{}: audio stream {stream} of 1 stream", path.display()).into()); | 1080 | return Err(format!("{}: audio stream {stream} of 1 stream", path.display()).into()); |
| 1074 | } | 1081 | } |
| 1075 | - SymTrack::open(path).map(|track| Some(Self::Sym(track))) | 1082 | + SymTrack::open_required(path).map(|track| Some(Self::Sym(track))) |
| 1076 | } | 1083 | } |
| 1077 | 1084 | ||
| 1078 | fn sample_rate(&self) -> u32 { | 1085 | fn sample_rate(&self) -> u32 { |
| @@ -1197,7 +1204,13 @@ struct SymTrack { | |||
| 1197 | } | 1204 | } |
| 1198 | 1205 | ||
| 1199 | impl SymTrack { | 1206 | impl SymTrack { |
| 1200 | - fn open(path: &Path) -> crate::Result<Self> { | 1207 | + /// `Ok(None)` for a container that opened and holds no audio track at all -- |
| 1208 | + /// a silent source, which is a thing a file is allowed to be. Everything | ||
| 1209 | + /// else that goes wrong here is an `Err`: a file that will not open, will | ||
| 1210 | + /// not parse, or carries a track it cannot describe is *broken*, and the | ||
| 1211 | + /// difference matters because silence is played and exported without a word | ||
| 1212 | + /// while an `Err` is shown ([`Track::open`]). | ||
| 1213 | + fn open(path: &Path) -> crate::Result<Option<Self>> { | ||
| 1201 | let mss = MediaSourceStream::new(Box::new(File::open(path)?), Default::default()); | 1214 | let mss = MediaSourceStream::new(Box::new(File::open(path)?), Default::default()); |
| 1202 | // The extension is a hint, not a decision: the probe reads the magic and | 1215 | // The extension is a hint, not a decision: the probe reads the magic and |
| 1203 | // is free to disagree, which is what makes a mislabelled file work. | 1216 | // is free to disagree, which is what makes a mislabelled file work. |
| @@ -1211,9 +1224,9 @@ impl SymTrack { | |||
| 1211 | FormatOptions::default(), | 1224 | FormatOptions::default(), |
| 1212 | MetadataOptions::default(), | 1225 | MetadataOptions::default(), |
| 1213 | )?; | 1226 | )?; |
| 1214 | - let track = reader | 1227 | + let Some(track) = reader.default_track(SymKind::Audio) else { |
| 1215 | - .default_track(SymKind::Audio) | 1228 | + return Ok(None); |
| 1216 | - .ok_or_else(|| format!("{} has no audio track", path.display()))?; | 1229 | + }; |
| 1217 | let (track_id, num_frames, delay) = (track.id, track.num_frames, track.delay); | 1230 | let (track_id, num_frames, delay) = (track.id, track.num_frames, track.delay); |
| 1218 | let time_base = track | 1231 | let time_base = track |
| 1219 | .time_base | 1232 | .time_base |
| @@ -1234,7 +1247,7 @@ impl SymTrack { | |||
| 1234 | let codec = symphonia::default::get_codecs() | 1247 | let codec = symphonia::default::get_codecs() |
| 1235 | .get_audio_decoder(params.codec) | 1248 | .get_audio_decoder(params.codec) |
| 1236 | .map_or("an unsupported format", |d| d.codec.info.short_name); | 1249 | .map_or("an unsupported format", |d| d.codec.info.short_name); |
| 1237 | - Ok(Self { | 1250 | + Ok(Some(Self { |
| 1238 | reader, | 1251 | reader, |
| 1239 | track_id, | 1252 | track_id, |
| 1240 | sample_rate, | 1253 | sample_rate, |
| @@ -1248,7 +1261,14 @@ impl SymTrack { | |||
| 1248 | time_base, | 1261 | time_base, |
| 1249 | params, | 1262 | params, |
| 1250 | codec, | 1263 | codec, |
| 1251 | - }) | 1264 | + })) |
| 1265 | + } | ||
| 1266 | + | ||
| 1267 | + /// [`open`](Self::open) where having no audio track is itself the error: | ||
| 1268 | + /// what a standalone audio file is held to, since a song with no sound in | ||
| 1269 | + /// it is a broken file and not a silent source. | ||
| 1270 | + fn open_required(path: &Path) -> crate::Result<Self> { | ||
| 1271 | + Self::open(path)?.ok_or_else(|| format!("{} has no audio track", path.display()).into()) | ||
| 1252 | } | 1272 | } |
| 1253 | 1273 | ||
| 1254 | /// A fresh decoder: seeking mid-stream leaves state that belongs to the | 1274 | /// A fresh decoder: seeking mid-stream leaves state that belongs to the |
crates/engine/tests/audio_decode.rsmodified+37 -0
| @@ -272,3 +272,40 @@ fn an_ac3_source_refuses_an_mp4_audio_copy() { | |||
| 272 | "unhelpful refusal: {err}" | 272 | "unhelpful refusal: {err}" |
| 273 | ); | 273 | ); |
| 274 | } | 274 | } |
| 275 | + | ||
| 276 | +/// The two things a Matroska file with no *readable* sound can be, told apart: | ||
| 277 | +/// a file with no audio track is the silent source it says it is, and a file | ||
| 278 | +/// whose audio will not open at all is an error. They used to be the same | ||
| 279 | +/// answer -- silence -- so a broken track played and exported as a hole with | ||
| 280 | +/// nothing but an `eprintln` to say why. | ||
| 281 | +#[test] | ||
| 282 | +fn a_matroska_with_no_track_is_silent_and_a_broken_one_is_an_error() { | ||
| 283 | + // No audio track at all: a picture and two subtitle tracks. Silent, and the | ||
| 284 | + // stream picker offers nothing rather than refusing the file. | ||
| 285 | + let subs = asset("test_subs.mkv"); | ||
| 286 | + assert!( | ||
| 287 | + AudioSession::probe(&subs, 0) | ||
| 288 | + .expect("no audio is not an error") | ||
| 289 | + .is_none(), | ||
| 290 | + "an mkv with no audio track is a silent source" | ||
| 291 | + ); | ||
| 292 | + assert!(AudioSession::probe_streams(&subs).expect("list").is_empty()); | ||
| 293 | + assert!( | ||
| 294 | + AudioSession::open(&subs).expect("open").is_none(), | ||
| 295 | + "no sound" | ||
| 296 | + ); | ||
| 297 | + | ||
| 298 | + // ...and a file that will not parse at all, under the same extension: an | ||
| 299 | + // `Err` a front-end can show, not a silent import. | ||
| 300 | + let broken = std::env::temp_dir().join(format!("ve_broken_{}.mkv", std::process::id())); | ||
| 301 | + std::fs::write(&broken, b"\x1a\x45\xdf\xa3not a matroska file at all").expect("write"); | ||
| 302 | + let err = AudioSession::probe(&broken, 0) | ||
| 303 | + .expect_err("a broken file must not pass for a silent one") | ||
| 304 | + .to_string(); | ||
| 305 | + assert!(!err.is_empty(), "the refusal says something"); | ||
| 306 | + assert!( | ||
| 307 | + AudioSession::probe_streams(&broken).is_err(), | ||
| 308 | + "and so does the listing" | ||
| 309 | + ); | ||
| 310 | + std::fs::remove_file(&broken).unwrap(); | ||
| 311 | +} | ||
crates/engine/tests/lanes.rsmodified+9 -4
| @@ -52,8 +52,13 @@ fn out_path(name: &str, ext: &str) -> PathBuf { | |||
| 52 | /// A hand-written v4 project: `V1` the whole of `test_av`, `V2` the middle | 52 | /// A hand-written v4 project: `V1` the whole of `test_av`, `V2` the middle |
| 53 | /// third playing `test_av2` from its own frame 0, `A1` the whole of `test_av`. | 53 | /// third playing `test_av2` from its own frame 0, `A1` the whole of `test_av`. |
| 54 | /// Exactly the file a user would write by hand, parsed by the real loader. | 54 | /// Exactly the file a user would write by hand, parsed by the real loader. |
| 55 | -fn three_lane_file() -> PathBuf { | 55 | +/// |
| 56 | - let path = out_path("project", "edith"); | 56 | +/// `name` is the caller's own, because [`out_path`] is unique per *run* and not |
| 57 | +/// per test: two tests sharing one name run in parallel over one file, and the | ||
| 58 | +/// first to finish deletes it out from under the second -- which then fails on | ||
| 59 | +/// a file that is simply not there any more. | ||
| 60 | +fn three_lane_file(name: &str) -> PathBuf { | ||
| 61 | + let path = out_path(name, "edith"); | ||
| 57 | // `source <audio stream> <path>`: the number is which audio track of the | 62 | // `source <audio stream> <path>`: the number is which audio track of the |
| 58 | // file plays, not the source index -- both of these are on their first. | 63 | // file plays, not the source index -- both of these are on their first. |
| 59 | let text = format!( | 64 | let text = format!( |
| @@ -143,7 +148,7 @@ fn frame_of(session: &mut PlaybackSession, timeline: u32) -> Vec<u8> { | |||
| 143 | #[test] | 148 | #[test] |
| 144 | fn playback_shows_the_topmost_video_lane() { | 149 | fn playback_shows_the_topmost_video_lane() { |
| 145 | pin_software(); | 150 | pin_software(); |
| 146 | - let project = three_lane_file(); | 151 | + let project = three_lane_file("playback"); |
| 147 | let mut session = PlaybackSession::open_project(&project).expect("open the project"); | 152 | let mut session = PlaybackSession::open_project(&project).expect("open the project"); |
| 148 | session.set_gain(0.0); | 153 | session.set_gain(0.0); |
| 149 | assert_eq!(session.timeline_duration(), f64::from(TOTAL) / FPS); | 154 | assert_eq!(session.timeline_duration(), f64::from(TOTAL) / FPS); |
| @@ -164,7 +169,7 @@ fn playback_shows_the_topmost_video_lane() { | |||
| 164 | #[test] | 169 | #[test] |
| 165 | fn export_writes_the_same_composite() { | 170 | fn export_writes_the_same_composite() { |
| 166 | pin_software(); | 171 | pin_software(); |
| 167 | - let file = three_lane_file(); | 172 | + let file = three_lane_file("export"); |
| 168 | let session = PlaybackSession::open_project(&file).expect("open the project"); | 173 | let session = PlaybackSession::open_project(&file).expect("open the project"); |
| 169 | let out = out_path("composite", "mp4"); | 174 | let out = out_path("composite", "mp4"); |
| 170 | 175 | ||