| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | use std::path::{Path, PathBuf}; |
| 12 | use std::process::Command; |
| 13 | use std::time::{Duration, Instant}; |
| 14 | |
| 15 | use engine::PlaybackSession; |
| 16 | use engine::export::{EncoderSeat, ExportSettings, Format}; |
| 17 | |
| 18 | #[path = "support/seat.rs"] |
| 19 | mod seat; |
| 20 | |
| 21 | const WIDTH: u32 = 320; |
| 22 | const HEIGHT: u32 = 240; |
| 23 | const FPS: u32 = 25; |
| 24 | |
| 25 | fn scratch(name: &str) -> PathBuf { |
| 26 | let dir = std::env::temp_dir().join(format!("edith-{name}-{}", std::process::id())); |
| 27 | let _ = std::fs::remove_dir_all(&dir); |
| 28 | std::fs::create_dir_all(&dir).expect("scratch directory"); |
| 29 | dir |
| 30 | } |
| 31 | |
| 32 | fn have(tool: &str) -> bool { |
| 33 | Command::new(tool).arg("-version").output().is_ok_and(|o| o.status.success()) |
| 34 | } |
| 35 | |
| 36 | |
| 37 | const PIN_CORES: usize = 4; |
| 38 | |
| 39 | fn have_taskset() -> bool { |
| 40 | Command::new("taskset").arg("--version").output().is_ok_and(|o| o.status.success()) |
| 41 | } |
| 42 | |
| 43 | fn run(source: &Path, out: &Path, settings: &ExportSettings) -> Result<(), String> { |
| 44 | let session = PlaybackSession::open(source).unwrap_or_else(|e| panic!("open {}: {e}", source.display())); |
| 45 | let handle = session.export_to_with(out, settings); |
| 46 | let started = Instant::now(); |
| 47 | while !handle.is_finished() { |
| 48 | assert!(started.elapsed() < Duration::from_secs(600), "the export did not finish in 600 s"); |
| 49 | std::thread::sleep(Duration::from_millis(20)); |
| 50 | } |
| 51 | handle.result().expect("an outcome").map_err(|e| e.to_string()) |
| 52 | } |
| 53 | |
| 54 | |
| 55 | fn file_md5(path: &Path) -> String { |
| 56 | let out = Command::new("md5sum").arg(path).output().expect("md5sum runs"); |
| 57 | String::from_utf8_lossy(&out.stdout).split_whitespace().next().unwrap_or("").to_owned() |
| 58 | } |
| 59 | |
| 60 | #[test] |
| 61 | fn default_exports_are_byte_identical_to_before() { |
| 62 | if !have("ffmpeg") { |
| 63 | eprintln!("no ffmpeg: skipping"); |
| 64 | return; |
| 65 | } |
| 66 | |
| 67 | |
| 68 | |
| 69 | |
| 70 | if std::thread::available_parallelism().map_or(1, |n| n.get()) != PIN_CORES { |
| 71 | if std::env::var_os("EDITH_PIN_REEXEC").is_some() { |
| 72 | eprintln!("could not pin to {PIN_CORES} CPUs: skipping"); |
| 73 | return; |
| 74 | } |
| 75 | if !have_taskset() { |
| 76 | eprintln!("no taskset: skipping"); |
| 77 | return; |
| 78 | } |
| 79 | let status = Command::new("taskset") |
| 80 | .args(["-c", &format!("0-{}", PIN_CORES - 1)]) |
| 81 | .arg(std::env::current_exe().expect("this test binary")) |
| 82 | .args(["--exact", "default_exports_are_byte_identical_to_before", "--nocapture"]) |
| 83 | .env("EDITH_PIN_REEXEC", "1") |
| 84 | .status() |
| 85 | .expect("taskset runs"); |
| 86 | assert!(status.success(), "the export on {PIN_CORES} CPUs changed"); |
| 87 | return; |
| 88 | } |
| 89 | let _seat = seat::software(); |
| 90 | let dir = scratch("hevc10-defaults"); |
| 91 | let source = dir.join("plain.mp4"); |
| 92 | let ok = Command::new("ffmpeg") |
| 93 | .args(["-v", "error", "-y", "-f", "lavfi", "-i"]) |
| 94 | .arg(format!("testsrc2=size={WIDTH}x{HEIGHT}:rate={FPS}:duration=1")) |
| 95 | .args(["-c:v", "libx264", "-preset", "ultrafast", "-crf", "12", "-pix_fmt", "yuv420p", "-an"]) |
| 96 | .arg(&source) |
| 97 | .status() |
| 98 | .is_ok_and(|s| s.success()); |
| 99 | assert!(ok, "ffmpeg makes the source"); |
| 100 | let mut report = String::new(); |
| 101 | for (name, settings, ext) in [ |
| 102 | ("h264", ExportSettings { format: Format::Mp4, seat: EncoderSeat::Software, ..Default::default() }, "mp4"), |
| 103 | ("hevc-mp4", ExportSettings { format: Format::HevcMp4, seat: EncoderSeat::Software, ..Default::default() }, "mp4"), |
| 104 | ("hevc-mkv", ExportSettings { format: Format::Hevc, seat: EncoderSeat::Software, ..Default::default() }, "mkv"), |
| 105 | ("hevc10-mp4", ExportSettings { format: Format::HevcMp4, seat: EncoderSeat::Software, video_bit_depth: Some(10), ..Default::default() }, "mp4"), |
| 106 | ("hevc-intra-mkv", ExportSettings { format: Format::Hevc, seat: EncoderSeat::Software, intra_only: true, ..Default::default() }, "mkv"), |
| 107 | ] { |
| 108 | let out = dir.join(format!("{name}.{ext}")); |
| 109 | run(&source, &out, &settings).unwrap_or_else(|e| panic!("{name}: {e}")); |
| 110 | let size = std::fs::metadata(&out).expect("the file").len(); |
| 111 | let again = dir.join(format!("{name}-again.{ext}")); |
| 112 | run(&source, &again, &settings).unwrap_or_else(|e| panic!("{name}: {e}")); |
| 113 | let (a, b) = (file_md5(&out), file_md5(&again)); |
| 114 | eprintln!("PIN {name} {size} {a}"); |
| 115 | report.push_str(&format!("{name} {size} {a}\n")); |
| 116 | assert_eq!(a, b, "{name}: the export is deterministic"); |
| 117 | if let Some((_, pinned)) = PINS.iter().find(|(n, _)| *n == name) { |
| 118 | assert_eq!(format!("{size} {a}"), *pinned, "{name}: the default export changed"); |
| 119 | } |
| 120 | } |
| 121 | eprintln!("{report}"); |
| 122 | let _ = std::fs::remove_dir_all(&dir); |
| 123 | } |
| 124 | |
| 125 | |
| 126 | const PINS: &[(&str, &str)] = &[ |
| 127 | ("h264", "28816 99882583a60538757d8c2cfb16f24f95"), |
| 128 | ("hevc-mp4", "34617 ab1f3164ffc3f79cf41d19ba78a85261"), |
| 129 | ("hevc-mkv", "34207 7a400530e2d541b203a5d4963e160acc"), |
| 130 | ("hevc10-mp4", "34446 4659cc08bf7227ec1609e086c7fd9078"), |
| 131 | ("hevc-intra-mkv", "39154 1099ec487e0c165f41c7302be18ef775"), |
| 132 | ]; |