forge
Explore
Log in

Don't you dare go Hollow

edith

Clone
★ 0

0 watching0 forksRust18.3 MiBff99ad6Tahinli6 days ago1055 commits

audio_gaps.rs130 lines, 5.2 KiBHistoryRawDownload
1//! The containers and codecs this build refuses by name: ASF / WMV (Windows Media) and Speex in
2//! Ogg. A refusal is an answer, not a silent import: the probe, the warning and the import each
3//! name the file's format, and never the mp4 "ftyp" error an ASF used to get.
4//!
5//! The fixtures are made here, at test time, by ffmpeg. Skipped, loudly, where ffmpeg (or the
6//! encoder a case needs) is not installed.
7
8use std::path::PathBuf;
9use std::process::Command;
10
11use engine::{AudioSession, PlaybackSession};
12
13const ASF_REFUSAL: &str = "ASF/WMV (Windows Media) files are not supported: convert to MP4 or MKV";
14const SPEEX_REFUSAL: &str = "Speex audio is not supported";
15
16fn asset(name: &str) -> PathBuf {
17 PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../../assets").join(name)
18}
19
20/// Whether this ffmpeg lists `name` among its encoders.
21fn ffmpeg_has_encoder(name: &str) -> bool {
22 Command::new("ffmpeg")
23 .args(["-hide_banner", "-encoders"])
24 .output()
25 .is_ok_and(|o| {
26 String::from_utf8_lossy(&o.stdout)
27 .lines()
28 .any(|l| l.split_whitespace().nth(1) == Some(name))
29 })
30}
31
32/// Makes `name.ext` with ffmpeg from the given output arguments; `None` (and a note) when ffmpeg
33/// cannot make it here.
34fn make(name: &str, ext: &str, args: &[&str]) -> Option<PathBuf> {
35 let dir = std::env::temp_dir().join(format!("audio-gaps-{}", std::process::id()));
36 std::fs::create_dir_all(&dir).ok()?;
37 let out = dir.join(format!("{name}.{ext}"));
38 let status = Command::new("ffmpeg")
39 .args(["-v", "error", "-y", "-f", "lavfi", "-i", "sine=frequency=440:duration=1:sample_rate=16000"])
40 .args(["-f", "lavfi", "-i", "testsrc=duration=1:size=160x120:rate=10"])
41 .args(args)
42 .arg(&out)
43 .status()
44 .ok()?;
45 assert!(status.success(), "ffmpeg makes {name}");
46 Some(out)
47}
48
49#[test]
50fn asf_and_wmv_are_refused_by_name_not_by_the_mp4_error() {
51 let Some(pic) = asset("test_av.mp4").exists().then(|| asset("test_av.mp4")) else {
52 return;
53 };
54 let mut session = PlaybackSession::open(&pic).expect("open the picture file");
55 session.set_gain(0.0);
56 for (name, ext) in [("wma_asf", "asf"), ("wma_wmv", "wmv")] {
57 let Some(file) = make(name, ext, &["-c:v", "wmv2", "-c:a", "wmav2", "-b:a", "64k"]) else {
58 continue;
59 };
60 let probe = AudioSession::probe_streams(&file).expect_err("an ASF is refused");
61 assert_eq!(probe.to_string(), ASF_REFUSAL, "{name}: probe");
62 let import = session.import_with_warnings(&file).expect_err("an ASF is refused");
63 assert_eq!(import.to_string(), ASF_REFUSAL, "{name}: import");
64 assert!(!import.to_string().contains("ftyp"), "{name}: the mp4 error leaked");
65 let why = AudioSession::unsupported(&file).expect_err("an ASF is refused");
66 assert_eq!(why.to_string(), ASF_REFUSAL, "{name}: warning path");
67 }
68}
69
70#[test]
71fn audio_only_speex_in_ogg_is_refused_by_name() {
72 if !ffmpeg_has_encoder("libspeex") {
73 eprintln!("skipped: this ffmpeg has no libspeex encoder");
74 return;
75 }
76 for (name, ext) in [("speex_ogg", "ogg"), ("speex_spx", "spx")] {
77 let Some(file) = make(name, ext, &["-vn", "-c:a", "libspeex", "-ar", "16000", "-ac", "1", "-f", "ogg"])
78 else {
79 continue;
80 };
81 assert_eq!(
82 AudioSession::probe_streams(&file).expect_err("Speex is refused").to_string(),
83 SPEEX_REFUSAL,
84 "{name}: probe"
85 );
86 assert_eq!(
87 AudioSession::unsupported(&file).expect("asks").as_deref(),
88 Some(SPEEX_REFUSAL),
89 "{name}: the reason the import warns with"
90 );
91 }
92}
93
94#[test]
95fn an_ogg_vorbis_file_is_not_taken_for_speex() {
96 // A file that is not Speex must not be refused as one: the Ogg check reads the first packet.
97 if !ffmpeg_has_encoder("libvorbis") {
98 eprintln!("skipped: this ffmpeg has no libvorbis encoder");
99 return;
100 }
101 let Some(file) = make("vorbis_ogg", "ogg", &["-vn", "-c:a", "libvorbis", "-ac", "1", "-f", "ogg"]) else {
102 return;
103 };
104 // Whatever the reader says of a Vorbis Ogg, it is never the Speex refusal.
105 let reason = AudioSession::unsupported(&file).ok().flatten();
106 assert_ne!(reason.as_deref(), Some(SPEEX_REFUSAL));
107}
108
109#[test]
110fn speex_with_video_in_matroska_imports_silent_with_a_warning() {
111 // A Speex track beside a picture is the silent path: the picture imports, the sound is silent,
112 // and the one warning names the codec.
113 if !ffmpeg_has_encoder("libspeex") {
114 eprintln!("skipped: this ffmpeg has no libspeex encoder");
115 return;
116 }
117 let Some(file) = make(
118 "speex_video_mkv",
119 "mkv",
120 &["-c:v", "mpeg4", "-c:a", "libspeex", "-ar", "16000", "-ac", "1"],
121 ) else {
122 return;
123 };
124 let mut session = PlaybackSession::open(asset("test_av.mp4")).expect("open the picture file");
125 session.set_gain(0.0);
126 let (_, warnings) = session.import_with_warnings(&file).expect("the picture imports");
127 assert_eq!(warnings.len(), 1, "{warnings:?}");
128 assert!(warnings[0].contains("Speex"), "the warning names the codec: {warnings:?}");
129 assert!(warnings[0].contains("silent"), "the warning says the sound is silent: {warnings:?}");
130}