| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | use std::path::{Path, PathBuf}; |
| 6 | use std::time::Instant; |
| 7 | |
| 8 | use engine::{MediaBitrate, probe_bitrate}; |
| 9 | |
| 10 | fn asset(name: &str) -> PathBuf { |
| 11 | Path::new(env!("CARGO_MANIFEST_DIR")) |
| 12 | .join("../../assets") |
| 13 | .join(name) |
| 14 | } |
| 15 | |
| 16 | |
| 17 | |
| 18 | |
| 19 | |
| 20 | |
| 21 | const FIXTURES: &[(&str, f64)] = &[ |
| 22 | ("test_av.mp4", 5.0), |
| 23 | ("test_av1.mkv", 2.0), |
| 24 | ("test_hevc.mkv", 2.0), |
| 25 | ("test_tone_48k.wav", 3.0), |
| 26 | ]; |
| 27 | |
| 28 | |
| 29 | |
| 30 | const CONTAINERS: &[&str] = &[ |
| 31 | "test_av.mp4", |
| 32 | "test_av1.mkv", |
| 33 | "test_hevc.mkv", |
| 34 | "test_hevc10.mkv", |
| 35 | "test_multiaudio.mp4", |
| 36 | "test_multiaudio.mkv", |
| 37 | "test_ac3.mkv", |
| 38 | "test_eac3.mkv", |
| 39 | "test_subs.mkv", |
| 40 | "test_vp9.mp4", |
| 41 | "test_h264.mkv", |
| 42 | "test_tone_48k.wav", |
| 43 | "test_short_video_long_audio.mp4", |
| 44 | ]; |
| 45 | |
| 46 | |
| 47 | |
| 48 | |
| 49 | fn no_field_is_zero(name: &str, rate: MediaBitrate) { |
| 50 | for (field, value) in [ |
| 51 | ("total", rate.total), |
| 52 | ("video", rate.video), |
| 53 | ("audio", rate.audio), |
| 54 | ] { |
| 55 | assert_ne!(value, Some(0), "{name}: {field} came back a fabricated zero"); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | #[test] |
| 60 | fn a_breakdown_adds_up() { |
| 61 | |
| 62 | |
| 63 | |
| 64 | |
| 65 | |
| 66 | |
| 67 | |
| 68 | for &name in CONTAINERS { |
| 69 | let path = asset(name); |
| 70 | let bytes = std::fs::metadata(&path).expect(name).len(); |
| 71 | let rate = probe_bitrate(&path); |
| 72 | no_field_is_zero(name, rate); |
| 73 | let total = rate.total.unwrap_or_else(|| panic!("{name}: no total")); |
| 74 | let parts = rate.video.unwrap_or(0) + rate.audio.unwrap_or(0); |
| 75 | assert!( |
| 76 | total >= parts, |
| 77 | "{name}: total {total} is under its own parts {parts}" |
| 78 | ); |
| 79 | |
| 80 | |
| 81 | |
| 82 | assert!( |
| 83 | total <= bytes * 8, |
| 84 | "{name}: total {total} exceeds the file's own {} bits", |
| 85 | bytes * 8 |
| 86 | ); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | #[test] |
| 91 | fn a_long_soundtrack_over_a_short_picture_divides_by_the_file() { |
| 92 | |
| 93 | |
| 94 | |
| 95 | |
| 96 | |
| 97 | let name = "test_short_video_long_audio.mp4"; |
| 98 | let path = asset(name); |
| 99 | let bytes = std::fs::metadata(&path).expect(name).len(); |
| 100 | let rate = probe_bitrate(&path); |
| 101 | no_field_is_zero(name, rate); |
| 102 | |
| 103 | let total = rate.total.expect("no total"); |
| 104 | let expected = bytes as f64 * 8.0 / 60.0; |
| 105 | let error = (total as f64 - expected).abs() / expected; |
| 106 | assert!( |
| 107 | error < 0.05, |
| 108 | "{name}: total {total} is {:.1} % off the file over its own 60 s ({expected:.0})", |
| 109 | error * 100.0 |
| 110 | ); |
| 111 | |
| 112 | |
| 113 | let video = rate.video.expect("no video rate"); |
| 114 | let audio = rate.audio.expect("no audio rate"); |
| 115 | assert!(video < audio, "{name}: video {video} >= audio {audio}"); |
| 116 | assert!( |
| 117 | video + audio <= total, |
| 118 | "{name}: parts {} beat the total {total}", |
| 119 | video + audio |
| 120 | ); |
| 121 | } |
| 122 | |
| 123 | #[test] |
| 124 | fn total_is_the_file_over_its_seconds() { |
| 125 | for &(name, secs) in FIXTURES { |
| 126 | let path = asset(name); |
| 127 | let bytes = std::fs::metadata(&path).expect(name).len(); |
| 128 | let expected = bytes as f64 * 8.0 / secs; |
| 129 | |
| 130 | let rate = probe_bitrate(&path); |
| 131 | no_field_is_zero(name, rate); |
| 132 | let total = rate.total.unwrap_or_else(|| panic!("{name}: no total")); |
| 133 | |
| 134 | let error = (total as f64 - expected).abs() / expected; |
| 135 | assert!( |
| 136 | error < 0.05, |
| 137 | "{name}: total {total} bps is {:.1} % off {expected:.0} bps \ |
| 138 | ({bytes} bytes over {secs} s)", |
| 139 | error * 100.0 |
| 140 | ); |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | #[test] |
| 145 | fn a_stream_the_container_states_is_stated_and_no_stream_is_invented() { |
| 146 | |
| 147 | |
| 148 | |
| 149 | |
| 150 | for &(name, secs) in FIXTURES { |
| 151 | let rate = probe_bitrate(&asset(name)); |
| 152 | let total = rate.total.unwrap_or_else(|| panic!("{name}: no total")); |
| 153 | if name.ends_with(".wav") { |
| 154 | |
| 155 | |
| 156 | assert_eq!(rate.video, None, "{name}: a wav has no picture"); |
| 157 | assert_eq!(rate.audio, Some(total), "{name}"); |
| 158 | continue; |
| 159 | } |
| 160 | let video = rate.video.unwrap_or_else(|| panic!("{name}: no video rate")); |
| 161 | assert!(video < total, "{name}: video {video} >= file {total}"); |
| 162 | |
| 163 | assert!( |
| 164 | (10_000..100_000_000).contains(&video), |
| 165 | "{name}: video {video} bps is not a plausible rate for {secs} s of 720p" |
| 166 | ); |
| 167 | if let Some(audio) = rate.audio { |
| 168 | assert!(audio < total, "{name}: audio {audio} >= file {total}"); |
| 169 | } |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | |
| 174 | |
| 175 | |
| 176 | fn ffprobe_picture_bps(name: &str) -> u64 { |
| 177 | let run = |entries: &[&str]| { |
| 178 | let out = std::process::Command::new("ffprobe") |
| 179 | .args(["-v", "error"]) |
| 180 | .args(entries) |
| 181 | .arg(asset(name)) |
| 182 | .output() |
| 183 | .expect("ffprobe runs"); |
| 184 | String::from_utf8(out.stdout).expect("ffprobe prints utf-8") |
| 185 | }; |
| 186 | let bytes: u64 = run(&["-select_streams", "v", "-show_entries", "packet=size", "-of", "csv=p=0"]) |
| 187 | .lines() |
| 188 | .filter(|l| !l.trim().is_empty()) |
| 189 | .map(|l| l.trim().parse::<u64>().expect("a packet size")) |
| 190 | .sum(); |
| 191 | let secs: f64 = run(&["-show_entries", "format=duration", "-of", "csv=p=0"]) |
| 192 | .trim() |
| 193 | .parse() |
| 194 | .expect("a duration"); |
| 195 | (bytes as f64 * 8.0 / secs).round() as u64 |
| 196 | } |
| 197 | |
| 198 | #[test] |
| 199 | fn a_matroska_states_its_sound_too_and_its_picture_is_unchanged() { |
| 200 | |
| 201 | |
| 202 | |
| 203 | |
| 204 | |
| 205 | |
| 206 | |
| 207 | |
| 208 | |
| 209 | |
| 210 | |
| 211 | |
| 212 | |
| 213 | |
| 214 | for name in ["test_av1.mkv", "test_hevc.mkv"] { |
| 215 | let rate = probe_bitrate(&asset(name)); |
| 216 | no_field_is_zero(name, rate); |
| 217 | |
| 218 | |
| 219 | let expected = ffprobe_picture_bps(name); |
| 220 | let video = rate.video.unwrap_or_else(|| panic!("{name}: no picture rate")); |
| 221 | assert!( |
| 222 | video.abs_diff(expected) * 1000 <= expected, |
| 223 | "{name}: picture moved: {video} bps, ffprobe reads {expected}" |
| 224 | ); |
| 225 | let audio = rate |
| 226 | .audio |
| 227 | .unwrap_or_else(|| panic!("{name}: no audio rate, and it has a 128k AAC track")); |
| 228 | assert!( |
| 229 | (100_000..170_000).contains(&audio), |
| 230 | "{name}: audio {audio} bps, asked for 128000" |
| 231 | ); |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | #[test] |
| 236 | fn the_mp4_rows_are_what_the_encoder_was_asked_for() { |
| 237 | |
| 238 | |
| 239 | |
| 240 | |
| 241 | |
| 242 | let rate = probe_bitrate(&asset("test_av.mp4")); |
| 243 | assert_eq!(rate.video, Some(3_119_464), "test_av.mp4: picture"); |
| 244 | let audio = rate.audio.expect("test_av.mp4: no audio rate"); |
| 245 | assert!( |
| 246 | (120_000..140_000).contains(&audio), |
| 247 | "test_av.mp4: audio {audio} bps, asked for 128000" |
| 248 | ); |
| 249 | } |
| 250 | |
| 251 | #[test] |
| 252 | fn a_dual_audio_file_names_the_track_it_would_play_and_says_there_are_others() { |
| 253 | |
| 254 | |
| 255 | |
| 256 | |
| 257 | |
| 258 | |
| 259 | |
| 260 | |
| 261 | |
| 262 | |
| 263 | let name = "test_multiaudio.mp4"; |
| 264 | let path = asset(name); |
| 265 | let streams = engine::AudioSession::probe_streams(&path).expect(name); |
| 266 | assert!( |
| 267 | streams.len() > 1, |
| 268 | "{name}: the fixture stopped being multi-audio, this test is now vacuous" |
| 269 | ); |
| 270 | let rate = probe_bitrate(&path); |
| 271 | let audio = rate.audio.expect("no audio rate"); |
| 272 | |
| 273 | |
| 274 | |
| 275 | assert_eq!(streams[0].index, 0, "{name}: stream order moved"); |
| 276 | assert_eq!(streams[0].codec, "aac", "{name}: stream 0 is not the AAC one"); |
| 277 | assert!( |
| 278 | (110_000..145_000).contains(&audio), |
| 279 | "{name}: audio {audio} bps is not stream 0's ~127 kbps AAC -- the row \ |
| 280 | and the audio row now name different tracks" |
| 281 | ); |
| 282 | |
| 283 | |
| 284 | |
| 285 | |
| 286 | assert_eq!(streams.len(), 3, "{name}"); |
| 287 | } |
| 288 | |
| 289 | #[test] |
| 290 | fn a_still_states_no_rate_at_all() { |
| 291 | |
| 292 | |
| 293 | |
| 294 | |
| 295 | let rate = probe_bitrate(&asset("test_still.png")); |
| 296 | no_field_is_zero("test_still.png", rate); |
| 297 | assert_eq!(rate, MediaBitrate::default(), "test_still.png"); |
| 298 | } |
| 299 | |
| 300 | #[test] |
| 301 | fn a_file_that_will_not_open_states_no_rate_either() { |
| 302 | |
| 303 | |
| 304 | let rate = probe_bitrate(Path::new("/nonexistent/nothing.mp4")); |
| 305 | assert_eq!(rate, MediaBitrate::default()); |
| 306 | no_field_is_zero("missing", rate); |
| 307 | } |
| 308 | |
| 309 | #[test] |
| 310 | fn no_decoder_is_opened() { |
| 311 | |
| 312 | |
| 313 | |
| 314 | |
| 315 | let path = asset("test_hevc10.mkv"); |
| 316 | let started = Instant::now(); |
| 317 | let rate = probe_bitrate(&path); |
| 318 | let elapsed = started.elapsed(); |
| 319 | no_field_is_zero("test_hevc10.mkv", rate); |
| 320 | assert!(rate.total.is_some(), "test_hevc10.mkv: no total"); |
| 321 | assert!( |
| 322 | elapsed.as_millis() < 50, |
| 323 | "probe_bitrate took {elapsed:?}, which is decode territory" |
| 324 | ); |
| 325 | } |