//! The BD gate's source arming, shared with `examples/enc_probe` (lane-probe). //! //! The probe used to build its own ffmpeg command line and dropped the gate's //! `-ss` seek, so it read film B's black leader and every census taken through //! it was probe-relative (59 kB where the gate's own ladder point coded 89 kB //! of the same title). One definition now serves both: the gate's //! `clip_frames_vf` and the probe both call [`source`], so the two can only //! differ in the arguments they pass, which //! `encode::tests::the_probe_arms_the_gates_source` pins. use crate::encode::Picture; #[cfg(test)] use std::process::Command; /// `frames` pictures of `clip` from `skip` through the filter chain `vf`, as /// planar 8-bit 4:2:0 at `width`x`height` -- the BD gate's loader recipe. /// /// # Panics /// When ffmpeg cannot be run, fails, or delivers a different number of frames. #[must_use] pub fn source( clip: &str, skip: &str, vf: &str, width: usize, height: usize, frames: usize, ) -> Vec { let out = ec_testkit::ffmpeg() .args(["-v", "error", "-ss", skip, "-i", clip]) .args(["-frames:v", &frames.to_string()]) .args(["-vf", vf]) .args(["-f", "rawvideo", "-pix_fmt", "yuv420p", "-"]) .output() .expect("ffmpeg failed to run"); assert!( out.status.success(), "ffmpeg: {}", String::from_utf8_lossy(&out.stderr) ); let (luma, chroma) = (width * height, width * height / 4); let frame_len = luma + 2 * chroma; assert_eq!( out.stdout.len(), frame_len * frames, "expected {frames} 4:2:0 frames" ); (0..frames) .map(|i| { let b = &out.stdout[i * frame_len..][..frame_len]; Picture { width, height, y: b[..luma].iter().map(|&v| u16::from(v)).collect(), u: b[luma..luma + chroma] .iter() .map(|&v| u16::from(v)) .collect(), v: b[luma + chroma..].iter().map(|&v| u16::from(v)).collect(), } }) .collect() } /// Runs a test-harness child (`aomenc`, `ffmpeg`) with `input` on its stdin /// and all three pipes drained concurrently, returning the child's /// [`std::process::Output`] (status, stdout, stderr) for the caller to /// assert on. /// /// The ONE spawn path for every test that feeds a child on stdin /// (lane-av1-pipedrain). Writing the input INLINE before /// `wait_with_output()` deadlocks as soon as the child's stdout pipe buffer /// (~64 KiB) fills before the last input byte is written: the child blocks /// in write(2), the test blocks in `write_all`, both at 0% CPU until the /// harness timeout -- measured at 45 minutes on a 1.1 MB fixture decoding /// to 150 MB of raw frames (lane-t900 r10), and four aomenc gates still sat /// on the inline pattern after that fix. Here a writer thread owns stdin /// while the parent drains stdout/stderr, so a multi-MB y4m always meets a /// reader, never a blocked writer. /// /// A child that exits early (bad flags, refused input) breaks the writer's /// pipe: the EPIPE from `write_all` is swallowed on purpose, and the /// child's own exit status and stderr -- which the caller asserts on -- /// carry the real diagnosis. /// /// # Panics /// When the child cannot be spawned or reaped, when the writer thread /// panics, or when the child is still running after the deadline (below): /// it is killed and the test fails naming the command, the timeout and the /// kill -- the CFL suite lost 11h41m to a wedged aomenc child because the /// pre-pipedrain spawn had no bound (class: aomenc-stdin-pipe-deadlock). /// /// The deadline is 1800 s per child, overridable with /// `EC_AV1_CHILD_TIMEOUT_SECS`. Every legitimate child here is an /// aomenc/ffmpeg over an at-most-a-few-MB fixture: seconds, not minutes. #[cfg(test)] pub(crate) fn run_with_stdin(cmd: &mut Command, input: &[u8]) -> std::process::Output { use std::io::{Read, Write}; use std::process::Stdio; use std::time::{Duration, Instant}; let timeout = std::env::var("EC_AV1_CHILD_TIMEOUT_SECS") .ok() .and_then(|s| s.parse::().ok()) .map_or(Duration::from_secs(1800), Duration::from_secs); cmd.stdin(Stdio::piped()) .stdout(Stdio::piped()) .stderr(Stdio::piped()); let mut child = cmd.spawn().expect("encoder failed to start"); let mut stdin = child.stdin.take().expect("encoder stdin"); let mut stdout = child.stdout.take().expect("encoder stdout"); let mut stderr = child.stderr.take().expect("encoder stderr"); let program = cmd.get_program().to_string_lossy().into_owned(); let payload = input.to_vec(); let writer = std::thread::spawn(move || { let _ = stdin.write_all(&payload); }); // Both output pipes drained from the first millisecond: the child can // never wedge on a full stdout/stderr pipe while we are still writing // stdin (the pre-pipedrain inline `write_all` + `wait_with_output` // pattern that deadlocked the CFL suite's census gate). let out_reader = std::thread::spawn(move || { let mut buf = Vec::new(); let _ = stdout.read_to_end(&mut buf); buf }); let err_reader = std::thread::spawn(move || { let mut buf = Vec::new(); let _ = stderr.read_to_end(&mut buf); buf }); let deadline = Instant::now() + timeout; let status = loop { match child.try_wait().expect("encoder status") { Some(status) => break status, None if Instant::now() >= deadline => { let _ = child.kill(); let killed = child.wait().expect("encoder reap"); // The dead child closed every pipe, so all three threads // are out of their syscalls: the joins below are bounded. let _ = writer.join(); let _ = out_reader.join(); let _ = err_reader.join(); panic!( "{program}: still running {secs}s after spawn -- killed ({killed}). \ A live child here hangs the whole suite, which is how the CFL run lost \ 11h41m to aomenc wedged in anon_pipe_write \ (class: aomenc-stdin-pipe-deadlock). Check its wchan/fds before blaming \ the decoder.", secs = timeout.as_secs(), ); } None => std::thread::sleep(Duration::from_millis(50)), } }; let out = out_reader.join().expect("encoder stdout reader thread"); let err = err_reader.join().expect("encoder stderr reader thread"); writer.join().expect("encoder stdin writer thread"); std::process::Output { status, stdout: out, stderr: err, } }