| 1 | use std::path::PathBuf; |
| 2 | use std::time::{Duration, Instant}; |
| 3 | |
| 4 | use engine::audio::AudioSession; |
| 5 | use engine::scratch::Scratch; |
| 6 | |
| 7 | const RATE: u32 = 44100; |
| 8 | const SECONDS: u32 = 5; |
| 9 | |
| 10 | |
| 11 | const FRAME: i64 = 1024; |
| 12 | |
| 13 | fn asset(name: &str) -> PathBuf { |
| 14 | PathBuf::from(env!("CARGO_MANIFEST_DIR")) |
| 15 | .join("../../assets") |
| 16 | .join(name) |
| 17 | } |
| 18 | |
| 19 | |
| 20 | |
| 21 | fn 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] |
| 37 | fn 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 | |
| 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 | |
| 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 | |
| 76 | |
| 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 | |
| 88 | |
| 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] |
| 107 | fn 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 | |
| 122 | |
| 123 | |
| 124 | |
| 125 | |
| 126 | #[test] |
| 127 | fn 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 | |
| 141 | |
| 142 | |
| 143 | |
| 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 | |
| 165 | |
| 166 | |
| 167 | |
| 168 | |
| 169 | |
| 170 | |
| 171 | |
| 172 | |
| 173 | |
| 174 | #[test] |
| 175 | fn 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 | |
| 200 | |
| 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 | |
| 232 | |
| 233 | |
| 234 | |
| 235 | |
| 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 | |
| 249 | |
| 250 | |
| 251 | |
| 252 | fn 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 | |
| 266 | |
| 267 | |
| 268 | |
| 269 | |
| 270 | |
| 271 | |
| 272 | |
| 273 | |
| 274 | |
| 275 | #[test] |
| 276 | fn 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 | |
| 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 | |
| 303 | |
| 304 | |
| 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 | |
| 309 | |
| 310 | |
| 311 | |
| 312 | |
| 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 | |
| 327 | |
| 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 | |
| 342 | |
| 343 | |
| 344 | |
| 345 | |
| 346 | |
| 347 | |
| 348 | |
| 349 | #[test] |
| 350 | fn his_seven_one_remux_imports_and_plays() { |
| 351 | let Some(film) = engine::real_library::film("av1_opus_71") else { |
| 352 | return; |
| 353 | }; |
| 354 | |
| 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 | |
| 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 | |
| 372 | |
| 373 | |
| 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 | |
| 393 | |
| 394 | |
| 395 | |
| 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 | |
| 443 | |
| 444 | |
| 445 | |
| 446 | |
| 447 | #[test] |
| 448 | fn 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 | |
| 473 | |
| 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 | |
| 491 | |
| 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 | |
| 504 | |
| 505 | |
| 506 | |
| 507 | |
| 508 | #[test] |
| 509 | fn 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; |
| 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 | |
| 534 | |
| 535 | |
| 536 | assert!(diff < 1e-3, "max abs diff {diff} after the seek"); |
| 537 | } |
| 538 | |
| 539 | |
| 540 | |
| 541 | |
| 542 | |
| 543 | #[test] |
| 544 | fn 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 | |
| 556 | |
| 557 | |
| 558 | |
| 559 | |
| 560 | #[test] |
| 561 | fn a_matroska_with_no_track_is_silent_and_a_broken_one_is_an_error() { |
| 562 | |
| 563 | |
| 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 | |
| 578 | |
| 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 | } |