| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | use std::path::{Path, PathBuf}; |
| 11 | use std::process::Command; |
| 12 | |
| 13 | use engine::{AudioSession, PlaybackSession}; |
| 14 | |
| 15 | fn asset(name: &str) -> PathBuf { |
| 16 | PathBuf::from(env!("CARGO_MANIFEST_DIR")) |
| 17 | .join("../../assets") |
| 18 | .join(name) |
| 19 | } |
| 20 | |
| 21 | |
| 22 | fn wavpack_video(dir: &Path) -> PathBuf { |
| 23 | let out = dir.join("wavpack_sound.mkv"); |
| 24 | let run = Command::new("ffmpeg") |
| 25 | .args(["-v", "error", "-y", "-f", "lavfi", "-i", "testsrc=duration=2:size=320x240:rate=25"]) |
| 26 | .args(["-f", "lavfi", "-i", "sine=duration=2:sample_rate=48000"]) |
| 27 | .args(["-map", "0:v", "-map", "1:a", "-c:v", "libx264", "-pix_fmt", "yuv420p"]) |
| 28 | .args(["-c:a", "wavpack"]) |
| 29 | .arg(&out) |
| 30 | .output() |
| 31 | .expect("ffmpeg runs"); |
| 32 | assert!(run.status.success(), "ffmpeg: {}", String::from_utf8_lossy(&run.stderr)); |
| 33 | out |
| 34 | } |
| 35 | |
| 36 | #[test] |
| 37 | fn an_undecodable_sound_track_imports_silent_with_a_warning_naming_wavpack() { |
| 38 | let dir = PathBuf::from(env!("CARGO_TARGET_TMPDIR")).join("silent-audio"); |
| 39 | std::fs::create_dir_all(&dir).expect("the scratch directory"); |
| 40 | let file = wavpack_video(&dir); |
| 41 | |
| 42 | |
| 43 | assert!( |
| 44 | matches!(AudioSession::open(&file), Ok(None)), |
| 45 | "no decoder opens this WavPack track, so it is silent" |
| 46 | ); |
| 47 | let secs = AudioSession::stream_duration_secs(&file, 0) |
| 48 | .expect("the length reads") |
| 49 | .expect("the header states a length for the silent sound"); |
| 50 | assert!((secs - 2.0).abs() < 0.1, "the sound is held for the picture's 2 s: {secs}"); |
| 51 | |
| 52 | let mut session = PlaybackSession::open(asset("test_av.mp4")).expect("open the picture file"); |
| 53 | session.set_gain(0.0); |
| 54 | let (source, warnings) = session |
| 55 | .import_with_warnings(&file) |
| 56 | .expect("a picture whose sound nothing decodes still imports"); |
| 57 | assert_eq!(session.sources()[source].path, file); |
| 58 | |
| 59 | |
| 60 | |
| 61 | let timeline_fps = session.native_frame_rate(); |
| 62 | let frames = f64::from(session.file_frames(&file)); |
| 63 | assert!( |
| 64 | (frames - 2.0 * timeline_fps).abs() <= 2.0, |
| 65 | "the video frames are present: {frames} frames at a {timeline_fps} fps timeline" |
| 66 | ); |
| 67 | |
| 68 | |
| 69 | assert_eq!(warnings.len(), 1, "{warnings:?}"); |
| 70 | let warning = &warnings[0]; |
| 71 | assert!(warning.contains("WavPack"), "{warning}"); |
| 72 | assert!(!warning.contains("WAVPACK4"), "the raw tag is not shown: {warning}"); |
| 73 | assert!(warning.ends_with("its sound will be silent"), "{warning}"); |
| 74 | } |