forge
Explore
Log in

Don't you dare go Hollow

edith

Clone
★ 0

0 watching0 forksRust18.3 MiBef5832fTahinli3 months ago597 commits

audio_decode.rs590 lines, 24.5 KiBHistoryRawDownload
1use std::path::PathBuf;
2use std::time::{Duration, Instant};
3
4use engine::audio::AudioSession;
5use engine::scratch::Scratch;
6
7const RATE: u32 = 44100;
8const SECONDS: u32 = 5;
9/// One AAC-LC packet; the container's tail padding is not trimmed, so the
10/// decoded length may run up to a frame past the nominal duration.
11const FRAME: i64 = 1024;
12
13fn asset(name: &str) -> PathBuf {
14 PathBuf::from(env!("CARGO_MANIFEST_DIR"))
15 .join("../../assets")
16 .join(name)
17}
18
19/// Sign changes ignoring exact zeros, so the 1 Hz volume pulse touching zero
20/// does not register as a crossing.
21fn zero_crossings(samples: &[f32]) -> usize {
22 let mut crossings = 0;
23 let mut last = 0f32;
24 for &s in samples {
25 if s == 0.0 {
26 continue;
27 }
28 if last != 0.0 && (s > 0.0) != (last > 0.0) {
29 crossings += 1;
30 }
31 last = s;
32 }
33 crossings
34}
35
36#[test]
37fn decodes_av_mp4_audio() {
38 let start = Instant::now();
39 let (meta, rx) = AudioSession::open(asset("test_av.mp4"))
40 .expect("open")
41 .expect("test_av.mp4 has an audio track");
42 assert_eq!((meta.sample_rate, meta.channels), (RATE, 2));
43
44 let mut left = Vec::new();
45 let mut right = Vec::new();
46 let mut next_sample = 0u64;
47 let mut chunks = 0usize;
48 for chunk in rx {
49 // d. no gaps, no overlaps.
50 assert_eq!(chunk.start_sample, next_sample, "chunk {chunks} start");
51 assert!(!chunk.samples.is_empty(), "empty chunk {chunks}");
52 assert_eq!(
53 chunk.samples.len() % 2,
54 0,
55 "partial frame in chunk {chunks}"
56 );
57 next_sample += (chunk.samples.len() / 2) as u64;
58 left.extend(chunk.samples.iter().step_by(2));
59 right.extend(chunk.samples[1..].iter().step_by(2));
60 chunks += 1;
61 }
62 let elapsed = start.elapsed();
63 let frames = left.len();
64 assert!(frames > 0, "no audio decoded");
65
66 // b. ~5 s of audio, priming trimmed, at most a frame of tail padding over.
67 let want = (RATE * SECONDS) as i64;
68 let slack = frames as i64 - want;
69 assert!(
70 (0..=FRAME).contains(&slack),
71 "{frames} frames per channel, want {want} (+0..={FRAME})"
72 );
73 assert_eq!(right.len(), frames, "channel lengths differ");
74
75 // c. channel identity: L is 440 Hz, R is 880 Hz. A swap or a bad interleave
76 // shows up as the wrong crossing count.
77 let secs = frames as f64 / RATE as f64;
78 for (name, samples, hz) in [("left", &left, 440.0), ("right", &right, 880.0)] {
79 let want = 2.0 * hz * secs;
80 let got = zero_crossings(samples) as f64;
81 assert!(
82 (got - want).abs() <= 0.05 * want,
83 "{name}: {got} zero crossings, want {want} +/-5%"
84 );
85 }
86
87 // Priming trimmed: the sine starts at a rising zero crossing, and audio is
88 // present immediately rather than after a silent or garbage run-in.
89 assert!(
90 left[0].abs() < 0.05,
91 "first sample {} not near zero",
92 left[0]
93 );
94 assert!(left[1] > 0.0, "first sine quarter is not rising");
95 let head_peak = left[..FRAME as usize]
96 .iter()
97 .fold(0f32, |m, s| m.max(s.abs()));
98 assert!(head_peak > 0.01, "first frame is silent: peak {head_peak}");
99
100 eprintln!(
101 "{frames} frames/ch in {chunks} chunks, {elapsed:?} ({:?}/packet)",
102 elapsed / chunks as u32
103 );
104}
105
106#[test]
107fn video_only_file_has_no_audio() {
108 assert!(
109 AudioSession::open(asset("test_baseline.mp4"))
110 .expect("open")
111 .is_none()
112 );
113 assert!(
114 AudioSession::unsupported(asset("test_baseline.mp4"))
115 .expect("probe")
116 .is_none(),
117 "a file with no audio track is not a complaint"
118 );
119}
120
121/// Both ends of the AC-3 path decode: the 44.1 kHz **mono** fixture stays the
122/// mono source it is, and the 48 kHz **5.1** one — the shape a BluRay remux has
123/// — arrives as stereo, downmixed by the decoder itself (A/52 §7.8). Both play
124/// their two seconds, at a level that is neither silence nor clipping, and
125/// neither owes the session an excuse.
126#[test]
127fn an_ac3_track_decodes_and_51_comes_down_to_stereo() {
128 for (name, rate, channels) in [("test_ac3.mp4", 44100, 1), ("test_ac3_51.mp4", 48000, 2)] {
129 let path = asset(name);
130 let (meta, rx) = AudioSession::open(&path)
131 .expect("open")
132 .expect("AC-3 decodes now");
133 assert_eq!((meta.sample_rate, meta.channels), (rate, channels), "{name}");
134 let samples: Vec<f32> = rx.into_iter().flat_map(|c| c.samples).collect();
135 let secs = (samples.len() / channels as usize) as f64 / f64::from(rate);
136 assert!(
137 (1.9..2.1).contains(&secs),
138 "{name} is two seconds, decoded {secs:.3}s"
139 );
140 // The 440 Hz sine survives at a sane level. The vetting caveat was an
141 // LFE passthrough running hot, which the built-in downmix is what
142 // avoids; this is that level check, and it is also what catches a
143 // downmix that comes out silent.
144 let rms = (samples.iter().map(|s| f64::from(*s) * f64::from(*s)).sum::<f64>()
145 / samples.len() as f64)
146 .sqrt();
147 assert!(
148 (0.005..0.75).contains(&rms),
149 "{name}: RMS {rms:.6} is not a sine at a sane level"
150 );
151 assert!(
152 samples.iter().all(|s| s.abs() <= 1.0),
153 "{name}: the decode must not leave the device's range"
154 );
155 let session = engine::PlaybackSession::open(&path).expect("open for playback");
156 assert_eq!(
157 session.audio_disabled_reason(),
158 None,
159 "{name}: a track that decodes owes no excuse"
160 );
161 }
162}
163
164/// Opus, which used to be the codec the refusal string was *for*: a standalone
165/// `.opus`, the `.webm` sound off the web, and the 5.1 in an `.mka` that a film
166/// soundtrack is -- four Opus streams with a channel mapping table, decoded by
167/// `ruopus` and folded to stereo by the same fold the AC-3 and AAC paths use.
168///
169/// The 5.1 fixture is the channel-order check as well: its tone is in FL and BR
170/// and silent in between, so both output channels carry sound only if the
171/// decoder's Vorbis order (FL, FC, FR, BL, BR, LFE) was put into the film order
172/// the fold reads (FL, FR, FC, LFE, BL, BR). Skipping that permutation folds BR
173/// as if it were BL and the right channel comes out silent.
174#[test]
175fn opus_decodes_from_every_container_and_51_comes_down_in_order() {
176 for (name, secs, channels) in [("test_tone.opus", 3.0, 2), ("test_vp9.webm", 2.0, 1)] {
177 let path = asset(name);
178 let (meta, rx) = AudioSession::open(&path)
179 .expect("open")
180 .expect("Opus decodes now");
181 assert_eq!((meta.sample_rate, meta.channels), (48000, channels), "{name}");
182 let samples: Vec<f32> = rx.into_iter().flat_map(|c| c.samples).collect();
183 let decoded = (samples.len() / channels as usize) as f64 / 48000.0;
184 assert!(
185 (secs - decoded).abs() < 0.1,
186 "{name} is {secs}s, decoded {decoded:.3}s"
187 );
188 let rms = (samples.iter().map(|s| f64::from(*s) * f64::from(*s)).sum::<f64>()
189 / samples.len() as f64)
190 .sqrt();
191 assert!(
192 (0.005..0.75).contains(&rms),
193 "{name}: RMS {rms:.6} is not a sine at a sane level"
194 );
195 assert!(
196 samples.iter().all(|s| s.abs() <= 1.0),
197 "{name}: the decode must not leave the device's range"
198 );
199 // ...and the same file as a session, where the picture allows it: the
200 // webm's is VP9, which is the VA-API plugin's and not this test's.
201 if !name.ends_with(".webm") {
202 let session = engine::PlaybackSession::open(&path).expect("open for playback");
203 assert_eq!(
204 session.audio_disabled_reason(),
205 None,
206 "{name}: a track that decodes owes no excuse"
207 );
208 }
209 }
210
211 let path = asset("test_opus_51.mka");
212 let (meta, rx) = AudioSession::open(&path)
213 .expect("open")
214 .expect("5.1 Opus decodes now");
215 assert_eq!((meta.sample_rate, meta.channels), (48000, 2));
216 let samples: Vec<f32> = rx.into_iter().flat_map(|c| c.samples).collect();
217 let rms = |channel: usize| {
218 let side: Vec<f64> = samples[channel..]
219 .iter()
220 .step_by(2)
221 .map(|s| f64::from(*s) * f64::from(*s))
222 .collect();
223 (side.iter().sum::<f64>() / side.len() as f64).sqrt()
224 };
225 let (left, right) = (rms(0), rms(1));
226 assert!(left > 0.02, "the 5.1 fold came out silent on the left: {left}");
227 assert!(
228 right > 0.3 * left,
229 "BR was folded as if it were BL: left {left:.4}, right {right:.4}"
230 );
231 // ...and it is the *tone* on each side, not noise at the right level. This is
232 // the assert that bites: a multistream decoder that mis-slices the packet
233 // (all but the last stream of an Opus 5.1 packet are self-delimited) hands
234 // back full-scale hash whose RMS passes every level check above -- measured
235 // -13 dBFS of it on a film, where the film is silent.
236 let secs = (samples.len() / 2) as f64 / 48000.0;
237 for (name, channel, hz) in [("left", 0, 440.0), ("right", 1, 880.0)] {
238 let side: Vec<f32> = samples[channel..].iter().step_by(2).copied().collect();
239 let want = 2.0 * hz * secs;
240 let got = zero_crossings(&side) as f64;
241 assert!(
242 (got - want).abs() <= 0.05 * want,
243 "{name}: {got} zero crossings, want {want} +/-5% -- a decode this far off is noise"
244 );
245 }
246}
247
248/// The amplitude of an `hz` sine inside `samples`, by Goertzel. The window is a
249/// whole second at 48 kHz, so every frequency this asks about is an exact bin
250/// and there is no leakage to correct for; a real sine of amplitude A puts
251/// `A * N / 2` in its bin.
252fn amplitude(samples: &[f32], hz: f64) -> f64 {
253 let n = samples.len() as f64;
254 let w = 2.0 * std::f64::consts::PI * hz / 48_000.0;
255 let coeff = 2.0 * w.cos();
256 let (mut s1, mut s2) = (0.0, 0.0);
257 for &sample in samples {
258 let s0 = f64::from(sample) + coeff * s1 - s2;
259 s2 = s1;
260 s1 = s0;
261 }
262 2.0 * (s1 * s1 + s2 * s2 - coeff * s1 * s2).sqrt() / n
263}
264
265/// **7.1 Opus**, the widest layout the fold has a table for and the one the
266/// largest film carries: five Opus streams, three coupled, arriving on the
267/// timeline as an ordinary stereo source instead of the import refusal it used
268/// to be ("unsupported channel layout: 8 channels (max stereo)").
269///
270/// The fixture puts one frequency in each of four channels and silence in the
271/// rest (`gen_fixtures.sh`), so the whole downmix is one measurement: 440 in FL
272/// is left only, 880 in FC is both sides at -3 dB, 1320 in the LFE must be gone
273/// entirely, and 1760 in SR is right only. Every coefficient and the whole
274/// Vorbis-to-film permutation at this width fail visibly here.
275#[test]
276fn seven_one_opus_folds_to_the_stereo_the_timeline_carries() {
277 let path = asset("test_opus_71.mka");
278 let streams = AudioSession::probe_streams(&path).expect("streams");
279 assert_eq!(streams.len(), 1);
280 assert!(streams[0].decodable, "a 7.1 row is not greyed out any more");
281 assert_eq!(streams[0].channels, 2, "what reaches the timeline is a pair");
282
283 let (meta, rx) = AudioSession::open(&path)
284 .expect("open")
285 .expect("7.1 Opus decodes now");
286 assert_eq!((meta.sample_rate, meta.channels), (48000, 2));
287 let samples: Vec<f32> = rx.into_iter().flat_map(|c| c.samples).collect();
288 assert!(
289 samples.iter().all(|s| s.abs() <= 1.0),
290 "the fold must not leave the device's range"
291 );
292 // One whole second out of the middle, past any codec fade-in.
293 let second = |channel: usize| -> Vec<f32> {
294 samples[channel..]
295 .iter()
296 .step_by(2)
297 .skip(48_000)
298 .take(48_000)
299 .copied()
300 .collect()
301 };
302 // 1 + 3 * (-3 dB): FL/FR keep their side, FC and both surround pairs are the
303 // three that join it. Nothing may come out above full scale, which is what
304 // dividing by the coefficient sum buys.
305 let norm = 1.0 + 3.0 * f64::from(std::f32::consts::FRAC_1_SQRT_2);
306 let c = f64::from(std::f32::consts::FRAC_1_SQRT_2);
307 let (left, right) = (second(0), second(1));
308 // FL is the one channel that keeps its side at unity, so what it comes out
309 // as names the tone the fixture was written with -- `sine` peaks at 0.125 in
310 // ffmpeg, and reading that off the fold rather than hard-coding it keeps the
311 // coefficients below the claim instead of the generator's level. A fold that
312 // lost FL, or scaled the pair, leaves this band.
313 let source = amplitude(&left, 440.0) * norm;
314 assert!(
315 (0.10..0.15).contains(&source),
316 "FL came out as a {source:.4} tone; the fixture's is 0.125"
317 );
318 for (side, cut, want) in [
319 ("left", &left, [(440.0, 1.0), (880.0, c), (1320.0, 0.0), (1760.0, 0.0)]),
320 ("right", &right, [(440.0, 0.0), (880.0, c), (1320.0, 0.0), (1760.0, c)]),
321 ] {
322 for (hz, coeff) in want {
323 let got = amplitude(cut, hz);
324 let expect = source * coeff / norm;
325 match coeff {
326 // The LFE, and the channels this side has none of: what is left
327 // is codec noise and bleed, an order below the quietest tone.
328 0.0 => assert!(
329 got < 0.1 * source / norm,
330 "{side}: {hz} Hz came through at {got:.4}, and nothing there should"
331 ),
332 _ => assert!(
333 (got - expect).abs() < 0.15 * expect,
334 "{side}: {hz} Hz at {got:.4}, want {expect:.4} +/-15%"
335 ),
336 }
337 }
338 }
339}
340
341/// A real 7.1 remux, which is the file this whole change exists for: 12.9 GB of
342/// 2160p AV1 with an 8-channel Opus track that used to come back "IMPORT FAILED:
343/// unsupported channel layout: 8 channels (max stereo)" and take the picture
344/// with it.
345///
346/// Skipped, not failed, on a machine that does not have the film: a fixture
347/// cannot stand in for the claim (the refusal was measured on *this* file) and a
348/// test that cannot make it must say nothing rather than something false.
349#[test]
350fn his_seven_one_remux_imports_and_plays() {
351 let Some(film) = engine::real_library::film("av1_opus_71") else {
352 return;
353 };
354 // The import gate itself: this is the call whose `Err` the app printed.
355 let probe = AudioSession::probe(&film, 0)
356 .expect("the 7.1 track no longer refuses the import")
357 .expect("the film has audio");
358 assert_eq!(
359 (probe.sample_rate, probe.channels),
360 (48_000, 2),
361 "7.1 reaches the timeline as a pair"
362 );
363 // ...and every one of its three tracks is offered, none greyed out.
364 let streams = AudioSession::probe_streams(&film).expect("streams");
365 assert_eq!(streams.len(), 3, "7.1 plus two 5.1 commentary tracks");
366 assert!(
367 streams.iter().all(|s| s.decodable && s.channels == 2),
368 "{streams:?}"
369 );
370
371 // Ten seconds out of the middle of the film, through the very worker
372 // playback and a WAV export both feed from -- with sound on *both* sides,
373 // which a fold that dropped half the layout would not have.
374 let (meta, rx) = AudioSession::open_segments(&film, &[(600.0, 610.0)])
375 .expect("open")
376 .expect("the film has audio");
377 assert_eq!((meta.sample_rate, meta.channels), (48_000, 2));
378 let samples: Vec<f32> = rx.into_iter().flat_map(|c| c.samples).collect();
379 let secs = (samples.len() / 2) as f64 / 48_000.0;
380 assert!((9.9..10.1).contains(&secs), "{secs:.3} s decoded, want 10");
381 assert!(
382 samples.iter().all(|s| s.abs() <= 1.0),
383 "the fold must not leave the device's range"
384 );
385 for (side, channel) in [("left", 0), ("right", 1)] {
386 let one: Vec<f64> = samples[channel..].iter().step_by(2).map(|s| f64::from(*s)).collect();
387 let rms = (one.iter().map(|s| s * s).sum::<f64>() / one.len() as f64).sqrt();
388 assert!(rms > 0.001, "{side} came out silent: RMS {rms:.6}");
389 eprintln!("{side}: RMS {rms:.6} over {secs:.2}s from 600s");
390 }
391
392 // ...and the whole way out: the file on a timeline, trimmed to ten seconds,
393 // written as a WAV by the export path and read back through this engine's
394 // own reader. A session open is what the app's Import does, so a picture
395 // this size failing to open would fail here rather than in use.
396 let mut session = engine::PlaybackSession::open(&film).expect("the film opens as a timeline");
397 assert_eq!(
398 session.audio_disabled_reason(),
399 None,
400 "a track that decodes owes no excuse"
401 );
402 let frames = (10.0 * session.meta().frame_rate).round() as u32;
403 for lane in session.lanes() {
404 session.trim_clip(lane, 0, engine::project::Edge::End, frames);
405 }
406 let out = Scratch::file("ve_seven_one", "wav");
407 let handle = session.export_to_with(
408 &out,
409 &engine::export::ExportSettings {
410 format: engine::export::Format::Wav,
411 ..Default::default()
412 },
413 );
414 let started = Instant::now();
415 while !handle.is_finished() {
416 assert!(started.elapsed() < Duration::from_secs(300), "export hung");
417 std::thread::sleep(Duration::from_millis(50));
418 }
419 handle.result().expect("outcome").expect("the WAV is written");
420 let (wav, rx) = AudioSession::open(&out)
421 .expect("the WAV reopens")
422 .expect("it has sound");
423 let written: Vec<f32> = rx.into_iter().flat_map(|c| c.samples).collect();
424 let channels = usize::from(wav.channels);
425 assert_eq!(channels, 2, "a stereo WAV out of a 7.1 source");
426 for (side, channel) in [("left", 0), ("right", 1)] {
427 let one: Vec<f64> = written[channel..]
428 .iter()
429 .step_by(channels)
430 .map(|s| f64::from(*s))
431 .collect();
432 let rms = (one.iter().map(|s| s * s).sum::<f64>() / one.len() as f64).sqrt();
433 assert!(rms > 0.001, "the exported {side} is silent: RMS {rms:.6}");
434 eprintln!(
435 "exported {side}: RMS {rms:.6} over {:.2}s at {} Hz",
436 one.len() as f64 / f64::from(wav.sample_rate),
437 wav.sample_rate
438 );
439 }
440}
441
442/// The same two ends of the AC-3 path, in **Matroska**: a stereo AC-3 track and
443/// a 5.1 E-AC-3 one -- the codec a remux carries and the one the "(AAC only)"
444/// refusal used to fire on -- both come out of the blocks, both arrive as the
445/// stereo the §7.8 downmix hands the timeline, and neither owes the session an
446/// excuse any more.
447#[test]
448fn matroska_ac3_and_eac3_decode_out_of_the_blocks() {
449 for (name, codec) in [("test_ac3.mkv", "ac-3"), ("test_eac3.mkv", "e-ac-3")] {
450 let path = asset(name);
451 let (meta, rx) = AudioSession::open(&path)
452 .expect("open")
453 .expect("Matroska AC-3 decodes now");
454 assert_eq!((meta.sample_rate, meta.channels), (48000, 2), "{name}");
455 let samples: Vec<f32> = rx.into_iter().flat_map(|c| c.samples).collect();
456 let secs = (samples.len() / 2) as f64 / 48000.0;
457 assert!(
458 (1.9..2.1).contains(&secs),
459 "{name} is two seconds, decoded {secs:.3}s"
460 );
461 let rms = (samples.iter().map(|s| f64::from(*s) * f64::from(*s)).sum::<f64>()
462 / samples.len() as f64)
463 .sqrt();
464 assert!(
465 (0.005..0.75).contains(&rms),
466 "{name}: RMS {rms:.6} is not a sine at a sane level"
467 );
468 assert!(
469 samples.iter().all(|s| s.abs() <= 1.0),
470 "{name}: the decode must not leave the device's range"
471 );
472 // The user-facing half: the session comes up with sound and no notice,
473 // and the file's one audio stream is offered by the name it is in.
474 let session = engine::PlaybackSession::open(&path).expect("open for playback");
475 assert_eq!(
476 session.audio_disabled_reason(),
477 None,
478 "{name}: a track that decodes owes no excuse"
479 );
480 let streams = AudioSession::probe_streams(&path).expect("streams");
481 assert_eq!(streams.len(), 1, "{name}: one audio track, one row");
482 assert_eq!(streams[0].codec, codec, "{name}");
483 assert!(streams[0].decodable, "{name}");
484 assert_eq!(
485 AudioSession::unsupported(&path).expect("unsupported"),
486 None,
487 "{name}: nothing to excuse when it plays"
488 );
489 }
490 // The invariant, stated as an assert: the AAC track of a Matroska file is
491 // still symphonia's, listed and decoded exactly as it was.
492 let streams = AudioSession::probe_streams(asset("test_av1.mkv")).expect("streams");
493 assert_eq!(streams.len(), 1);
494 assert_eq!(streams[0].codec, "aac", "AAC in an mkv is untouched by this");
495 assert!(
496 AudioSession::open(asset("test_av1.mkv"))
497 .expect("open")
498 .is_some(),
499 "AAC in an mkv still decodes"
500 );
501}
502
503/// A seek into a Matroska AC-3 track: Matroska indexes no samples, so the block
504/// a decode starts on is found by the blocks' own timestamps -- and getting that
505/// wrong is a tail that is the wrong length or lands in the wrong place. It is
506/// the same check `audio_seek` makes of the mp4 path, on the container that has
507/// no sample table.
508#[test]
509fn a_seek_into_a_matroska_ac3_track_is_the_tail_of_a_full_run() {
510 let path = asset("test_eac3.mkv");
511 let full: Vec<f32> = AudioSession::open(&path)
512 .expect("open")
513 .expect("audio")
514 .1
515 .into_iter()
516 .flat_map(|c| c.samples)
517 .collect();
518 let start = 48_000u64; // one second in, in frames per channel
519 let mut next = start;
520 let mut tail = Vec::new();
521 for chunk in AudioSession::open_at(&path, 1.0).expect("open").expect("audio").1 {
522 assert_eq!(chunk.start_sample, next, "a seeked run numbers from the seek");
523 next += (chunk.samples.len() / 2) as u64;
524 tail.extend(chunk.samples);
525 }
526 let want = &full[start as usize * 2..];
527 assert_eq!(tail.len(), want.len(), "the seeked run is a different length");
528 let diff = tail
529 .iter()
530 .zip(want)
531 .map(|(a, b)| (a - b).abs())
532 .fold(0.0f32, f32::max);
533 // One syncframe of pre-roll is what the 256-sample overlap-add needs, so
534 // this is exact in practice; the tolerance is for a decoder that carries
535 // more state than the frame before, not for a wrong block.
536 assert!(diff < 1e-3, "max abs diff {diff} after the seek");
537}
538
539/// An AC-3 syncframe is not something an `mp4a` sample table holds, so the
540/// *copy* path names it and hands the export on to the re-encode (which decodes
541/// this very source: the downmix above is what it encodes). Named either way --
542/// never a silent export.
543#[test]
544fn an_ac3_source_refuses_an_mp4_audio_copy() {
545 let Err(err) = AudioSession::copy_segments(asset("test_ac3.mp4"), &[(0.0, 1.0)]) else {
546 panic!("AC-3 cannot become an mp4a track");
547 };
548 let err = err.to_string();
549 assert!(
550 err.contains("needs AAC in an mp4") && err.contains("AC-3"),
551 "unhelpful refusal: {err}"
552 );
553}
554
555/// The two things a Matroska file with no *readable* sound can be, told apart:
556/// a file with no audio track is the silent source it says it is, and a file
557/// whose audio will not open at all is an error. They used to be the same
558/// answer -- silence -- so a broken track played and exported as a hole with
559/// nothing but an `eprintln` to say why.
560#[test]
561fn a_matroska_with_no_track_is_silent_and_a_broken_one_is_an_error() {
562 // No audio track at all: a picture and two subtitle tracks. Silent, and the
563 // stream picker offers nothing rather than refusing the file.
564 let subs = asset("test_subs.mkv");
565 assert!(
566 AudioSession::probe(&subs, 0)
567 .expect("no audio is not an error")
568 .is_none(),
569 "an mkv with no audio track is a silent source"
570 );
571 assert!(AudioSession::probe_streams(&subs).expect("list").is_empty());
572 assert!(
573 AudioSession::open(&subs).expect("open").is_none(),
574 "no sound"
575 );
576
577 // ...and a file that will not parse at all, under the same extension: an
578 // `Err` a front-end can show, not a silent import.
579 let broken = Scratch::file("ve_broken", "mkv");
580 std::fs::write(&broken, b"\x1a\x45\xdf\xa3not a matroska file at all").expect("write");
581 let err = AudioSession::probe(&broken, 0)
582 .expect_err("a broken file must not pass for a silent one")
583 .to_string();
584 assert!(!err.is_empty(), "the refusal says something");
585 assert!(
586 AudioSession::probe_streams(&broken).is_err(),
587 "and so does the listing"
588 );
589 std::fs::remove_file(&broken).unwrap();
590}