From 8a3b5f3baf227afdf8cd5a7e246396d87b7c133e Mon Sep 17 00:00:00 2001 From: Richie McIlroy <33632126+richiemcilroy@users.noreply.github.com> Date: Fri, 13 Nov 2026 23:36:44 -0500 Subject: [PATCH 01/10] fix(enc-mediafoundation): keep frame sizes Intel's H264 transform cannot take away from it --- crates/enc-mediafoundation/src/mft.rs | 37 ++++++++++ crates/enc-mediafoundation/src/video/h264.rs | 27 ++++++- crates/media-info/src/lib.rs | 77 ++++++++++++++++++++ 3 files changed, 137 insertions(+), 4 deletions(-) diff --git a/crates/enc-mediafoundation/src/mft.rs b/crates/enc-mediafoundation/src/mft.rs index 530acccab1c..7be3bd569e2 100644 --- a/crates/enc-mediafoundation/src/mft.rs +++ b/crates/enc-mediafoundation/src/mft.rs @@ -82,6 +82,43 @@ impl EncoderDevice { } } +/// Friendly name of the hardware H264 encoder transform that FFmpeg's +/// `h264_mf` opens: the first hardware encoder Media Foundation lists for NV12 +/// input, as FFmpeg enumerates them. +pub fn first_hardware_h264_encoder_name() -> Option { + use windows::Win32::Media::MediaFoundation::{ + MF_VERSION, MFMediaType_Video, MFSTARTUP_LITE, MFShutdown, MFStartup, MFVideoFormat_H264, + MFVideoFormat_NV12, + }; + + let input = MFT_REGISTER_TYPE_INFO { + guidMajorType: MFMediaType_Video, + guidSubtype: MFVideoFormat_NV12, + }; + let output = MFT_REGISTER_TYPE_INFO { + guidMajorType: MFMediaType_Video, + guidSubtype: MFVideoFormat_H264, + }; + let started = unsafe { MFStartup(MF_VERSION, MFSTARTUP_LITE) }.is_ok(); + let name = enumerate_mfts( + &MFT_CATEGORY_VIDEO_ENCODER, + MFT_ENUM_FLAG_HARDWARE | MFT_ENUM_FLAG_SORTANDFILTER, + Some(&input), + Some(&output), + ) + .ok() + .and_then(|encoders| encoders.into_iter().next()) + .and_then(|encoder| { + get_string_attribute(&encoder.cast().ok()?, &MFT_FRIENDLY_NAME_Attribute) + .ok() + .flatten() + }); + if started { + let _ = unsafe { MFShutdown() }; + } + name +} + fn enumerate_mfts( category: &GUID, flags: MFT_ENUM_FLAG, diff --git a/crates/enc-mediafoundation/src/video/h264.rs b/crates/enc-mediafoundation/src/video/h264.rs index 2e496e47f64..2b7911eb5a4 100644 --- a/crates/enc-mediafoundation/src/video/h264.rs +++ b/crates/enc-mediafoundation/src/video/h264.rs @@ -183,6 +183,12 @@ pub enum NewVideoEncoderError { OutputType(windows::core::Error), #[error("InputType: {0}")] InputType(windows::core::Error), + #[error("{encoder} does not take {width}x{height} frames")] + UnsupportedFrameSize { + encoder: String, + width: i32, + height: i32, + }, } #[derive(Clone, Debug, thiserror::Error)] @@ -252,14 +258,27 @@ impl H264Encoder { bitrate_multipler, ); - let transform = + let device = EncoderDevice::enumerate_with_flags(MFMediaType_Video, MFVideoFormat_H264, flags) .map_err(|_| NewVideoEncoderError::NoVideoEncoderDevice)? .first() .cloned() - .ok_or(NewVideoEncoderError::NoVideoEncoderDevice)? - .create_transform() - .map_err(NewVideoEncoderError::EncoderTransform)?; + .ok_or(NewVideoEncoderError::NoVideoEncoderDevice)?; + if cap_media_info::is_intel_h264_mft(device.display_name()) + && !cap_media_info::intel_h264_mft_accepts_frame_size( + output_resolution.Width.max(0) as u32, + output_resolution.Height.max(0) as u32, + ) + { + return Err(NewVideoEncoderError::UnsupportedFrameSize { + encoder: device.display_name().to_owned(), + width: output_resolution.Width, + height: output_resolution.Height, + }); + } + let transform = device + .create_transform() + .map_err(NewVideoEncoderError::EncoderTransform)?; let video_processor = VideoProcessor::new( d3d_device.clone(), diff --git a/crates/media-info/src/lib.rs b/crates/media-info/src/lib.rs index 67ba2b2e4ec..e2ec679f09d 100644 --- a/crates/media-info/src/lib.rs +++ b/crates/media-info/src/lib.rs @@ -411,6 +411,29 @@ pub fn ensure_even(value: u32) -> u32 { if adjusted == 0 { 2 } else { adjusted } } +/// Intel's Media Foundation H264 encoder (`mfx_mft_h264ve_64.dll`) is +/// documented to take widths in multiples of 16 and faults with an access +/// violation instead of failing when a recording's odd capture width reaches +/// it (#4016, #3928). Its frames are also bounded by H.264 level 5.2 (36864 +/// macroblocks) and Intel's 4096 pixel encode limit. Heights such as 1080 are +/// padded inside the encoder and stay allowed. +pub fn intel_h264_mft_accepts_frame_size(width: u32, height: u32) -> bool { + const MAX_DIMENSION: u32 = 4096; + const MAX_MACROBLOCKS: u32 = 36_864; + width > 0 + && height > 0 + && width.is_multiple_of(16) + && width <= MAX_DIMENSION + && height <= MAX_DIMENSION + && (width / 16) * height.div_ceil(16) <= MAX_MACROBLOCKS +} + +/// Whether a Media Foundation encoder transform's friendly name is Intel's, +/// e.g. "IntelĀ® Quick Sync Video H.264 Encoder MFT". +pub fn is_intel_h264_mft(friendly_name: &str) -> bool { + friendly_name.to_ascii_lowercase().contains("intel") +} + pub fn ffmpeg_sample_format_for(sample_format: SampleFormat) -> Option { match sample_format { SampleFormat::U8 => Some(Sample::U8(Type::Packed)), @@ -427,6 +450,60 @@ pub fn ffmpeg_sample_format_for(sample_format: SampleFormat) -> Option { mod tests { use super::*; + #[test] + fn intel_h264_mft_frame_sizes() { + for (width, height) in [ + (1280, 720), + (1920, 1080), + (2560, 1440), + (3840, 2160), + (1440, 900), + (1920, 1200), + (1072, 1920), + (16, 16), + (4096, 2304), + (2304, 4096), + (1920, 812), + (1920, 2), + ] { + assert!( + intel_h264_mft_accepts_frame_size(width, height), + "{width}x{height}" + ); + } + for (width, height) in [ + (1438, 812), + (1436, 810), + (1366, 768), + (1080, 1920), + (1914, 1080), + (1922, 1080), + (2, 2), + (0, 1080), + (1920, 0), + (5120, 2880), + (4112, 2160), + (1920, 4112), + (4096, 4096), + ] { + assert!( + !intel_h264_mft_accepts_frame_size(width, height), + "{width}x{height}" + ); + } + } + + #[test] + fn intel_h264_mft_names() { + assert!(is_intel_h264_mft( + "IntelĀ® Quick Sync Video H.264 Encoder MFT" + )); + assert!(is_intel_h264_mft("INTEL HEVC Encoder")); + assert!(!is_intel_h264_mft("NVIDIA H.264 Encoder MFT")); + assert!(!is_intel_h264_mft("AMDh264Encoder")); + assert!(!is_intel_h264_mft("H264 Encoder MFT")); + } + mod audio_info { use super::*; From fa6c43014d6f391258f17339ba6441c06529e991 Mon Sep 17 00:00:00 2001 From: Richie McIlroy <33632126+richiemcilroy@users.noreply.github.com> Date: Fri, 13 Nov 2026 23:36:44 -0500 Subject: [PATCH 02/10] fix(enc-ffmpeg): self-test Media Foundation H264 in a child process on Windows --- apps/cli/Cargo.toml | 1 + apps/cli/src/main.rs | 2 + apps/desktop-gpui/src/main.rs | 4 + crates/enc-ffmpeg/Cargo.toml | 6 +- crates/enc-ffmpeg/src/video/h264.rs | 199 ++++++++- .../src/video/isolated_self_test.rs | 393 ++++++++++++++++++ crates/enc-ffmpeg/src/video/mod.rs | 1 + 7 files changed, 595 insertions(+), 11 deletions(-) create mode 100644 crates/enc-ffmpeg/src/video/isolated_self_test.rs diff --git a/apps/cli/Cargo.toml b/apps/cli/Cargo.toml index 0a660f81c52..ab966828c77 100644 --- a/apps/cli/Cargo.toml +++ b/apps/cli/Cargo.toml @@ -11,6 +11,7 @@ cap-recording = { path = "../../crates/recording" } cap-rendering = { path = "../../crates/rendering" } cap-editor = { path = "../../crates/editor" } cap-export = { path = "../../crates/export" } +cap-enc-ffmpeg = { path = "../../crates/enc-ffmpeg" } cap-media-info = { path = "../../crates/media-info" } cap-timestamp = { path = "../../crates/timestamp" } cap-utils = { path = "../../crates/utils" } diff --git a/apps/cli/src/main.rs b/apps/cli/src/main.rs index 2944d5656ea..b786226c238 100644 --- a/apps/cli/src/main.rs +++ b/apps/cli/src/main.rs @@ -453,6 +453,8 @@ struct CompletionsArgs { } fn main() { + cap_enc_ffmpeg::isolated_self_test::run_child_or_register_host(); + #[cfg(target_os = "linux")] if let Some(threads) = cap_utils::linux_runtime::llvmpipe_thread_count() { // Mesa counts host CPUs inside containers; configure it before creating the CLI runtime. diff --git a/apps/desktop-gpui/src/main.rs b/apps/desktop-gpui/src/main.rs index acdd53da302..c21fe52bc60 100644 --- a/apps/desktop-gpui/src/main.rs +++ b/apps/desktop-gpui/src/main.rs @@ -293,6 +293,10 @@ fn app_runtime() -> tokio::runtime::Runtime { } fn main() { + // A copy of Cap started to self-test a hardware H264 encoder runs only + // that test and exits, before logging, crash capture or single-instance. + cap_enc_ffmpeg::isolated_self_test::run_child_or_register_host(); + #[cfg(target_os = "linux")] if let Some(threads) = cap_utils::linux_runtime::llvmpipe_thread_count() { // Mesa counts host CPUs inside containers; apply the process limit before logging starts threads. diff --git a/crates/enc-ffmpeg/Cargo.toml b/crates/enc-ffmpeg/Cargo.toml index 3cadf132da2..2197340c32b 100644 --- a/crates/enc-ffmpeg/Cargo.toml +++ b/crates/enc-ffmpeg/Cargo.toml @@ -15,8 +15,12 @@ tracing.workspace = true workspace-hack = { version = "0.1", path = "../workspace-hack" } [target.'cfg(target_os = "windows")'.dependencies] +cap-enc-mediafoundation = { path = "../enc-mediafoundation" } cap-frame-converter = { path = "../frame-converter" } -windows = { workspace = true, features = ["Win32_Storage_FileSystem"] } +windows = { workspace = true, features = [ + "Win32_Storage_FileSystem", + "Win32_System_Diagnostics_Debug", +] } [dev-dependencies] tempfile = "3.20.0" diff --git a/crates/enc-ffmpeg/src/video/h264.rs b/crates/enc-ffmpeg/src/video/h264.rs index fe2b112afb7..fdb9a697f4a 100644 --- a/crates/enc-ffmpeg/src/video/h264.rs +++ b/crates/enc-ffmpeg/src/video/h264.rs @@ -15,6 +15,7 @@ use crate::base::{EncoderBase, PacketSink}; use crate::video::HwUpload; use crate::video::h264_packet::H264PacketEncoder; use crate::video::hardware_crash::{self, HardwareEncoderSession}; +use crate::video::isolated_self_test; #[cfg(target_os = "macos")] use crate::video::videotoolbox_hw::VideoToolboxHwFrames; use std::sync::Arc; @@ -217,6 +218,16 @@ impl H264EncoderBuilder { ); } + if let Some(reason) = unsupported_frame_size(&codec_name, output_width, output_height) { + info!( + encoder = %codec_name, + %reason, + "Skipping hardware H264 encoder that cannot take this frame size" + ); + last_error = Some(H264EncoderError::SelfTest(reason)); + continue; + } + let hardware_session = is_hardware_h264(&codec_name).then(|| HardwareEncoderSession::begin(&codec_name)); if hardware_session.is_some() @@ -613,6 +624,16 @@ impl H264EncoderBuilder { for (codec, encoder_options) in candidates { let codec_name = codec.name().to_string(); + if let Some(reason) = unsupported_frame_size(&codec_name, output_width, output_height) { + info!( + encoder = %codec_name, + %reason, + "Skipping hardware H264 encoder that cannot take this frame size" + ); + last_error = Some(H264EncoderError::SelfTest(reason)); + continue; + } + let hardware_session = is_hardware_h264(&codec_name).then(|| HardwareEncoderSession::begin(&codec_name)); if hardware_session.is_some() @@ -1540,9 +1561,35 @@ fn get_encoder_priority_with_override( return &["libx264"]; } + #[cfg(debug_assertions)] + if let Some(order) = debug_encoder_order() { + return order; + } + override_priority.unwrap_or_else(|| get_default_encoder_priority(config)) } +/// Debug builds only: `CAP_DEBUG_H264_ENCODER_ORDER=h264_mf,libx264` puts an +/// encoder first on a machine that would not pick it, so tests can drive the +/// Windows fallback paths on runners without a GPU. +#[cfg(debug_assertions)] +fn debug_encoder_order() -> Option<&'static [&'static str]> { + use std::sync::OnceLock; + static ORDER: OnceLock>> = OnceLock::new(); + ORDER + .get_or_init(|| { + let raw = std::env::var("CAP_DEBUG_H264_ENCODER_ORDER").ok()?; + let order: Vec<&'static str> = raw + .split(',') + .map(str::trim) + .filter(|name| !name.is_empty()) + .map(|name| &*Box::leak(name.to_owned().into_boxed_str())) + .collect(); + (!order.is_empty()).then_some(order) + }) + .as_deref() +} + fn force_software_encoder() -> bool { std::env::var("CAP_EXPORT_FORCE_SOFTWARE_ENCODER").is_ok_and(|value| { matches!( @@ -1817,21 +1864,38 @@ fn cached_hardware_self_test( } let started = std::time::Instant::now(); - let result = hardware_encoder_self_test( - codec, - clone_options(encoder_options), - input_config, - output_width, - output_height, - bpp, - crf, - ); + let isolated = self_test_runs_isolated(codec.name()) + .then(|| { + isolated_self_test::run(&isolated_self_test::Request::new( + codec.name(), + encoder_options, + input_config, + output_width, + output_height, + bpp, + crf, + )) + }) + .flatten(); + let is_isolated = isolated.is_some(); + let result = isolated.unwrap_or_else(|| { + hardware_encoder_self_test( + codec, + clone_options(encoder_options), + input_config, + output_width, + output_height, + bpp, + crf, + ) + }); debug!( encoder = %codec.name(), width = output_width, height = output_height, elapsed_ms = started.elapsed().as_millis(), ok = result.is_ok(), + isolated = is_isolated, "Hardware H264 encoder self-test finished" ); cache @@ -1841,6 +1905,40 @@ fn cached_hardware_self_test( result } +/// FFmpeg's `h264_mf` loads whichever vendor's Media Foundation transform is +/// installed into this process, and Intel's faults instead of failing on +/// frame sizes it does not support (#4016, #3928), so its self-test runs in a +/// child process where a fault only fails the test. The other Windows +/// encoders keep testing in process: no report implicates them, and a child +/// process would add its start-up time to every recording start on their GPUs. +fn self_test_runs_isolated(codec_name: &str) -> bool { + cfg!(target_os = "windows") && codec_name == "h264_mf" +} + +/// FFmpeg's `h264_mf` hands the transform a tightly packed system memory +/// frame at the exact output size, which Intel's transform reads past at +/// widths it does not support. FFmpeg's `h264_qsv` pads its surfaces itself. +fn unsupported_frame_size(codec_name: &str, width: u32, height: u32) -> Option { + if codec_name != "h264_mf" { + return None; + } + #[cfg(target_os = "windows")] + { + use std::sync::OnceLock; + static FIRST_TRANSFORM: OnceLock> = OnceLock::new(); + let name = FIRST_TRANSFORM + .get_or_init(cap_enc_mediafoundation::mft::first_hardware_h264_encoder_name) + .as_deref()?; + if cap_media_info::is_intel_h264_mft(name) + && !cap_media_info::intel_h264_mft_accepts_frame_size(width, height) + { + return Some(format!("{name} does not take {width}x{height} frames")); + } + } + let _ = (width, height); + None +} + fn clone_options(options: &Dictionary<'static>) -> Dictionary<'static> { let mut copy = Dictionary::new(); for (k, v) in options.iter() { @@ -1849,7 +1947,7 @@ fn clone_options(options: &Dictionary<'static>) -> Dictionary<'static> { copy } -fn hardware_encoder_self_test( +pub(crate) fn hardware_encoder_self_test( codec: Codec, encoder_options: Dictionary<'static>, input_config: &VideoInfo, @@ -1858,6 +1956,9 @@ fn hardware_encoder_self_test( bpp: f32, crf: Option, ) -> Result<(), String> { + #[cfg(debug_assertions)] + isolated_self_test::inject_fault()?; + // A throwaway encoder with production settings; frames are built directly // in the negotiated output format, so no converter is needed. let opened = open_video_encoder_inner( @@ -2112,6 +2213,84 @@ mod self_test_tests { /// The order a Windows machine with an NVIDIA primary GPU uses, after /// Intel's Media Foundation encoder crashed the app. + #[test] + fn only_media_foundation_self_tests_in_a_child_process() { + assert_eq!( + self_test_runs_isolated("h264_mf"), + cfg!(target_os = "windows") + ); + for name in [ + "h264_nvenc", + "h264_amf", + "h264_qsv", + "h264_videotoolbox", + "h264_vaapi", + "libx264", + ] { + assert!(!self_test_runs_isolated(name), "{name}"); + } + } + + #[test] + fn only_media_foundation_is_skipped_for_its_frame_size() { + for name in ["h264_nvenc", "h264_amf", "h264_qsv", "libx264"] { + assert_eq!(unsupported_frame_size(name, 1438, 812), None, "{name}"); + } + if !cfg!(target_os = "windows") { + assert_eq!(unsupported_frame_size("h264_mf", 1438, 812), None); + } + } + + /// Media Foundation's self-test faulting (set `CAP_H264_SELF_TEST_FAULT` + /// to `access_violation` or `abort`) must leave this process running and + /// the encoder order moving on to libx264, at the odd sizes that reach + /// Intel's transform. Without the fault variable the runner's missing + /// hardware transform fails the self-test instead. + #[cfg(target_os = "windows")] + #[test] + fn media_foundation_self_test_fault_falls_back_to_libx264_at_odd_sizes() { + crate::video::isolated_self_test::tests::register_test_host(); + for (width, height) in [(1438, 812), (1366, 768), (1448, 814)] { + let config = VideoInfo { + pixel_format: format::Pixel::NV12, + width, + height, + time_base: ffmpeg::Rational(1, 1_000_000), + frame_rate: ffmpeg::Rational(30, 1), + }; + let mut encoder = H264EncoderBuilder::new(config) + .with_encoder_priority_override(&["h264_mf", "libx264"]) + .build_standalone() + .unwrap_or_else(|e| panic!("{width}x{height}: {e}")); + assert_eq!(encoder.codec_name(), "libx264", "{width}x{height}"); + assert_eq!( + (encoder.output_width(), encoder.output_height()), + (width, height) + ); + + let mut packets = 0usize; + for index in 0..10u64 { + let mut frame = frame::Video::new(format::Pixel::NV12, width, height); + for plane in 0..frame.planes() { + frame.data_mut(plane).fill(128); + } + encoder + .encode_frame(frame, Duration::from_millis(index * 33), |_| { + packets += 1; + Ok(()) + }) + .unwrap(); + } + encoder + .flush(|_| { + packets += 1; + Ok(()) + }) + .unwrap(); + assert!(packets >= 10, "{width}x{height}: {packets} packets"); + } + } + #[test] fn a_crashed_hardware_encoder_falls_through_to_the_next_candidate() { let nvidia = ["h264_nvenc", "h264_mf", "h264_qsv", "h264_amf", "libx264"]; diff --git a/crates/enc-ffmpeg/src/video/isolated_self_test.rs b/crates/enc-ffmpeg/src/video/isolated_self_test.rs new file mode 100644 index 00000000000..176aa45eb52 --- /dev/null +++ b/crates/enc-ffmpeg/src/video/isolated_self_test.rs @@ -0,0 +1,393 @@ +//! Intel's Media Foundation H264 encoder (`mfx_mft_h264ve_64.dll`) faults with +//! an access violation on some frame sizes instead of returning an error +//! (#4016 on export, #3928 on recording start). FFmpeg loads it into the +//! process that opens the encoder, so the pre-flight self-test used to take +//! the whole app down before any fallback could run. On Windows the self-test +//! for the encoders that load Intel's driver code runs in a child copy of the +//! host executable instead: a fault ends only the child, the parent reads that +//! as a failed self-test and encoder selection moves to the next candidate. + +use std::{ + ffi::OsString, + io::Read, + path::PathBuf, + process::{Command, ExitStatus, Stdio}, + sync::OnceLock, + time::{Duration, Instant}, +}; + +use cap_media_info::VideoInfo; +use ffmpeg::{Dictionary, codec::encoder, format}; +use serde::{Deserialize, Serialize}; + +const REQUEST_ENV: &str = "CAP_H264_SELF_TEST_REQUEST"; +/// Debug builds only: makes every hardware self-test, isolated or not, fault +/// the way the Intel encoder does, so tests can prove a fault no longer takes +/// the recording or export down. +#[cfg(debug_assertions)] +const FAULT_ENV: &str = "CAP_H264_SELF_TEST_FAULT"; +const VERDICT_PREFIX: &str = "cap-h264-self-test:"; +const FAIL_EXIT_CODE: i32 = 3; +const CHILD_TIMEOUT: Duration = Duration::from_secs(20); + +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)] +pub(crate) struct Request { + pub codec: String, + pub options: Vec<(String, String)>, + pub pixel_format: String, + pub width: u32, + pub height: u32, + pub time_base: (i32, i32), + pub frame_rate: (i32, i32), + pub output_width: u32, + pub output_height: u32, + pub bpp: f32, + pub crf: Option, +} + +impl Request { + pub(crate) fn new( + codec: &str, + options: &Dictionary<'static>, + input_config: &VideoInfo, + output_width: u32, + output_height: u32, + bpp: f32, + crf: Option, + ) -> Self { + Self { + codec: codec.to_owned(), + options: options + .iter() + .map(|(k, v)| (k.to_owned(), v.to_owned())) + .collect(), + pixel_format: input_config.pixel_format.name().to_owned(), + width: input_config.width, + height: input_config.height, + time_base: (input_config.time_base.0, input_config.time_base.1), + frame_rate: (input_config.frame_rate.0, input_config.frame_rate.1), + output_width, + output_height, + bpp, + crf, + } + } +} + +struct Host { + program: PathBuf, + args: Vec, +} + +static HOST: OnceLock = OnceLock::new(); + +/// Call first in `main` of every executable that records or exports. In a +/// self-test child this runs the test and exits; otherwise it registers the +/// executable as the host the self-test children are started from. +pub fn run_child_or_register_host() { + run_child_if_requested(); + if let Ok(program) = std::env::current_exe() { + register_host(program, Vec::new()); + } +} + +/// For executables whose `main` is not their own (test harnesses): `args` +/// must reach code that calls [`run_child_if_requested`] first. +pub fn register_host(program: PathBuf, args: Vec) { + let _ = HOST.set(Host { program, args }); +} + +/// Runs the requested self-test and exits when this process is a self-test +/// child; returns immediately otherwise. +pub fn run_child_if_requested() { + let Some(raw) = std::env::var_os(REQUEST_ENV) else { + return; + }; + #[cfg(windows)] + suppress_crash_dialogs(); + + let verdict = serve(&raw.to_string_lossy()); + use std::io::Write; + let mut stdout = std::io::stdout().lock(); + let code = match verdict { + Ok(()) => { + let _ = writeln!(stdout, "{VERDICT_PREFIX}ok"); + 0 + } + Err(reason) => { + let reason = reason.replace(['\r', '\n'], " "); + let _ = writeln!(stdout, "{VERDICT_PREFIX}fail {reason}"); + FAIL_EXIT_CODE + } + }; + let _ = stdout.flush(); + std::process::exit(code); +} + +/// A crashing child must not raise Windows Error Reporting's "Cap stopped +/// working" dialog: the parent already handles the failure. +#[cfg(windows)] +fn suppress_crash_dialogs() { + use windows::Win32::System::Diagnostics::Debug::{ + SEM_FAILCRITICALERRORS, SEM_NOGPFAULTERRORBOX, SetErrorMode, + }; + unsafe { + SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOGPFAULTERRORBOX); + } +} + +fn serve(raw: &str) -> Result<(), String> { + let request: Request = + serde_json::from_str(raw).map_err(|e| format!("unreadable self-test request: {e}"))?; + ffmpeg::init().map_err(|e| format!("ffmpeg init: {e}"))?; + + let codec = encoder::find_by_name(&request.codec) + .ok_or_else(|| format!("encoder {} not found", request.codec))?; + let mut options = Dictionary::new(); + for (k, v) in &request.options { + options.set(k, v); + } + let pixel_format: format::Pixel = request + .pixel_format + .parse() + .map_err(|_| format!("unknown pixel format {}", request.pixel_format))?; + let input_config = VideoInfo { + pixel_format, + width: request.width, + height: request.height, + time_base: ffmpeg::Rational(request.time_base.0, request.time_base.1), + frame_rate: ffmpeg::Rational(request.frame_rate.0, request.frame_rate.1), + }; + super::h264::hardware_encoder_self_test( + codec, + options, + &input_config, + request.output_width, + request.output_height, + request.bpp, + request.crf, + ) +} + +#[cfg(debug_assertions)] +pub(crate) fn inject_fault() -> Result<(), String> { + match std::env::var(FAULT_ENV).as_deref() { + Ok("access_violation") => unsafe { + std::ptr::without_provenance_mut::(0x10).write_volatile(1); + unreachable!() + }, + Ok("abort") => std::process::abort(), + Ok("hang") => loop { + std::thread::sleep(Duration::from_secs(60)); + }, + Ok("fail") => Err("injected self-test failure".to_string()), + _ => Ok(()), + } +} + +/// Runs the self-test in a child process. `None` when this executable never +/// registered as a host, so the caller tests in process as before. +pub(crate) fn run(request: &Request) -> Option> { + let host = HOST.get()?; + Some(run_with(host, request, CHILD_TIMEOUT, &[])) +} + +fn run_with( + host: &Host, + request: &Request, + timeout: Duration, + extra_env: &[(&str, &str)], +) -> Result<(), String> { + let payload = + serde_json::to_string(request).map_err(|e| format!("encode self-test request: {e}"))?; + let mut command = Command::new(&host.program); + command + .args(&host.args) + .env(REQUEST_ENV, payload) + .envs(extra_env.iter().copied()) + .stdin(Stdio::null()) + .stdout(Stdio::piped()) + .stderr(Stdio::null()); + #[cfg(windows)] + { + use std::os::windows::process::CommandExt; + const CREATE_NO_WINDOW: u32 = 0x0800_0000; + command.creation_flags(CREATE_NO_WINDOW); + } + let mut child = command + .spawn() + .map_err(|e| format!("start self-test process: {e}"))?; + + let mut stdout = child.stdout.take(); + let reader = std::thread::spawn(move || { + let mut text = String::new(); + if let Some(stdout) = stdout.as_mut() { + let _ = stdout.read_to_string(&mut text); + } + text + }); + + let started = Instant::now(); + let status = loop { + match child.try_wait() { + Ok(Some(status)) => break Some(status), + Ok(None) if started.elapsed() >= timeout => { + let _ = child.kill(); + let _ = child.wait(); + break None; + } + Ok(None) => std::thread::sleep(Duration::from_millis(10)), + Err(e) => { + let _ = child.kill(); + let _ = child.wait(); + return Err(format!("wait for self-test process: {e}")); + } + } + }; + let output = reader.join().unwrap_or_default(); + match status { + Some(status) => verdict(status, &output), + None => Err(format!( + "self-test process did not finish within {} s", + timeout.as_secs() + )), + } +} + +fn verdict(status: ExitStatus, output: &str) -> Result<(), String> { + let line = output + .lines() + .rev() + .find_map(|line| line.trim().strip_prefix(VERDICT_PREFIX)); + match (status.code(), line) { + (Some(0), Some("ok")) => Ok(()), + (Some(FAIL_EXIT_CODE), Some(line)) => { + Err(line.strip_prefix("fail ").unwrap_or(line).trim().to_owned()) + } + (code, _) => Err(format!( + "encoder crashed its isolated self-test process ({})", + describe_exit(status, code) + )), + } +} + +fn describe_exit(status: ExitStatus, code: Option) -> String { + match code { + Some(code) if cfg!(windows) => format!("exit code {:#010x}", code as u32), + Some(code) => format!("exit code {code}"), + None => status.to_string(), + } +} + +#[cfg(test)] +pub(crate) mod tests { + use super::*; + + const CHILD_ENTRY: &str = "video::isolated_self_test::tests::self_test_child_entry"; + + /// The entry a self-test child of this test binary runs; without the + /// request variable it is a no-op. + #[test] + fn self_test_child_entry() { + run_child_if_requested(); + } + + fn test_host() -> Host { + Host { + program: std::env::current_exe().unwrap(), + args: vec![ + CHILD_ENTRY.into(), + "--exact".into(), + "--nocapture".into(), + "--test-threads=1".into(), + ], + } + } + + pub(crate) fn register_test_host() { + let host = test_host(); + register_host(host.program, host.args); + } + + fn gray_request(codec: &str, width: u32, height: u32) -> Request { + let config = VideoInfo { + pixel_format: format::Pixel::NV12, + width, + height, + time_base: ffmpeg::Rational(1, 1_000_000), + frame_rate: ffmpeg::Rational(30, 1), + }; + let mut options = Dictionary::new(); + options.set("preset", "ultrafast"); + Request::new(codec, &options, &config, width, height, 0.3, None) + } + + fn run_child(request: &Request, fault: Option<&str>, timeout: Duration) -> Result<(), String> { + let env: Vec<(&str, &str)> = fault.map(|fault| (FAULT_ENV, fault)).into_iter().collect(); + run_with(&test_host(), request, timeout, &env) + } + + #[test] + fn request_survives_the_trip_to_the_child() { + let request = gray_request("h264_mf", 1438, 812); + let decoded: Request = + serde_json::from_str(&serde_json::to_string(&request).unwrap()).unwrap(); + assert_eq!(decoded, request); + assert_eq!( + decoded.pixel_format.parse::().unwrap(), + format::Pixel::NV12 + ); + } + + #[test] + fn isolated_self_test_outcomes() { + let odd = gray_request("libx264", 1438, 812); + assert_eq!(run_child(&odd, None, CHILD_TIMEOUT), Ok(())); + + let crashed = run_child(&odd, Some("access_violation"), CHILD_TIMEOUT).unwrap_err(); + assert!( + crashed.starts_with("encoder crashed its isolated self-test process"), + "{crashed}" + ); + + let aborted = run_child(&odd, Some("abort"), CHILD_TIMEOUT).unwrap_err(); + assert!( + aborted.starts_with("encoder crashed its isolated self-test process"), + "{aborted}" + ); + + assert_eq!( + run_child(&odd, Some("fail"), CHILD_TIMEOUT), + Err("injected self-test failure".to_string()) + ); + + let started = Instant::now(); + let hung = run_child(&odd, Some("hang"), Duration::from_secs(2)).unwrap_err(); + assert!(hung.contains("did not finish"), "{hung}"); + assert!(started.elapsed() < Duration::from_secs(10)); + + let missing = run_child( + &gray_request("h264_not_an_encoder", 64, 64), + None, + CHILD_TIMEOUT, + ); + assert_eq!( + missing, + Err("encoder h264_not_an_encoder not found".to_string()) + ); + } + + #[test] + fn a_child_that_prints_no_verdict_counts_as_a_crash() { + let host = Host { + program: std::env::current_exe().unwrap(), + args: vec!["--list".into()], + }; + let error = + run_with(&host, &gray_request("libx264", 64, 64), CHILD_TIMEOUT, &[]).unwrap_err(); + assert!( + error.starts_with("encoder crashed its isolated self-test process"), + "{error}" + ); + } +} diff --git a/crates/enc-ffmpeg/src/video/mod.rs b/crates/enc-ffmpeg/src/video/mod.rs index 3d40290cb4b..3fee7f1c222 100644 --- a/crates/enc-ffmpeg/src/video/mod.rs +++ b/crates/enc-ffmpeg/src/video/mod.rs @@ -5,6 +5,7 @@ pub mod h264; pub mod h264_packet; pub mod hardware_crash; pub mod hevc; +pub mod isolated_self_test; mod packed_yuv; pub mod prores; mod rgb_to_yuv; From 7c6c52dba2363bf51ef25be306f0dea3f07c492c Mon Sep 17 00:00:00 2001 From: Richie McIlroy <33632126+richiemcilroy@users.noreply.github.com> Date: Fri, 13 Nov 2026 23:36:44 -0500 Subject: [PATCH 03/10] test: odd size Windows export survives a faulting Media Foundation self-test --- .../workflows/windows-recording-encoder.yml | 31 +++- .../export/tests/windows_odd_size_export.rs | 134 ++++++++++++++++++ 2 files changed, 164 insertions(+), 1 deletion(-) create mode 100644 crates/export/tests/windows_odd_size_export.rs diff --git a/.github/workflows/windows-recording-encoder.yml b/.github/workflows/windows-recording-encoder.yml index dbfa6d79466..35cd4eec448 100644 --- a/.github/workflows/windows-recording-encoder.yml +++ b/.github/workflows/windows-recording-encoder.yml @@ -1,11 +1,16 @@ name: Windows recording encoder # The segmented recording encoder's tests on Windows, the only platform where -# a hardware encoder that fails mid-recording is replaced with libx264. +# a hardware encoder that fails mid-recording is replaced with libx264, and the +# Media Foundation self-test that runs in a child process because Intel's +# transform faults on odd frame sizes. on: pull_request: paths: - "crates/enc-ffmpeg/**" + - "crates/enc-mediafoundation/**" + - "crates/media-info/**" + - "crates/export/tests/windows_odd_size_export.rs" - "crates/recording/src/output_pipeline/win_fragmented_m4s.rs" - "Cargo.lock" - ".github/workflows/windows-recording-encoder.yml" @@ -13,6 +18,9 @@ on: branches: [main] paths: - "crates/enc-ffmpeg/**" + - "crates/enc-mediafoundation/**" + - "crates/media-info/**" + - "crates/export/tests/windows_odd_size_export.rs" - "crates/recording/src/output_pipeline/win_fragmented_m4s.rs" - "Cargo.lock" - ".github/workflows/windows-recording-encoder.yml" @@ -55,3 +63,24 @@ jobs: - name: Segmented encoder tests run: cargo test --locked -p cap-enc-ffmpeg --lib mux::segmented_stream -- --nocapture + + - name: Encoder selection and isolated self-test tests + run: cargo test --locked -p cap-media-info --lib intel_h264_mft && cargo test --locked -p cap-enc-ffmpeg --lib self_test -- --nocapture + + # The fault runs wherever the self-test runs: in a child it fails only + # the child, in this process it would end the test with 0xc0000005. + - name: Media Foundation self-test fault falls back (access violation) + env: + CAP_H264_SELF_TEST_FAULT: access_violation + run: cargo test --locked -p cap-enc-ffmpeg --lib media_foundation_self_test_fault_falls_back_to_libx264_at_odd_sizes -- --exact --nocapture + + - name: Media Foundation self-test fault falls back (fast fail 0xc0000409) + env: + CAP_H264_SELF_TEST_FAULT: abort + run: cargo test --locked -p cap-enc-ffmpeg --lib media_foundation_self_test_fault_falls_back_to_libx264_at_odd_sizes -- --exact --nocapture + + - name: Odd size export with a faulting Media Foundation self-test + env: + CAP_DEBUG_H264_ENCODER_ORDER: h264_mf,libx264 + CAP_H264_SELF_TEST_FAULT: access_violation + run: cargo test --locked -p cap-export --test windows_odd_size_export -- --nocapture --test-threads=1 diff --git a/crates/export/tests/windows_odd_size_export.rs b/crates/export/tests/windows_odd_size_export.rs new file mode 100644 index 00000000000..6b65740b003 --- /dev/null +++ b/crates/export/tests/windows_odd_size_export.rs @@ -0,0 +1,134 @@ +//! MP4 export at the odd frame sizes a recorded area produces, through the +//! Windows encoder order. CI runners have no hardware encoder, so the +//! workflow puts Media Foundation first with `CAP_DEBUG_H264_ENCODER_ORDER` +//! and makes its self-test fault with `CAP_H264_SELF_TEST_FAULT`, the way +//! Intel's transform does (#4016). The export must still finish, with +//! libx264, at the requested size. +#![cfg(windows)] + +use cap_export::{ + ExporterBase, + mp4::{ExportCompression, Mp4ExportSettings}, +}; +use cap_project::{ProjectConfiguration, XY}; +use std::{fs, path::Path, process::Command}; + +const CHILD_ENTRY: &str = "self_test_child_entry"; + +/// Where the export's encoder self-test children start; a no-op otherwise. +#[test] +fn self_test_child_entry() { + cap_enc_ffmpeg::isolated_self_test::run_child_if_requested(); +} + +fn create_project(path: &Path, width: u32, height: u32) { + fs::create_dir_all(path).unwrap(); + let video = Command::new("ffmpeg") + .args(["-v", "error", "-f", "lavfi", "-i"]) + .arg(format!("testsrc2=size={width}x{height}:rate=30:duration=1")) + .args([ + "-c:v", + "libx264", + "-preset", + "ultrafast", + "-pix_fmt", + "yuv420p", + ]) + .arg(path.join("display.mp4")) + .output() + .expect("ffmpeg on PATH"); + assert!( + video.status.success(), + "{}", + String::from_utf8_lossy(&video.stderr) + ); + fs::write( + path.join("recording-meta.json"), + serde_json::to_vec(&serde_json::json!({ + "pretty_name": "Odd size export", + "display": { "path": "display.mp4", "fps": 30 } + })) + .unwrap(), + ) + .unwrap(); +} + +fn probe(path: &Path) -> (u32, u32, u32) { + let output = Command::new("ffprobe") + .args([ + "-v", + "error", + "-select_streams", + "v:0", + "-count_frames", + "-show_entries", + "stream=width,height,nb_read_frames", + "-of", + "csv=p=0", + ]) + .arg(path) + .output() + .expect("ffprobe on PATH"); + assert!( + output.status.success(), + "{}", + String::from_utf8_lossy(&output.stderr) + ); + let text = String::from_utf8_lossy(&output.stdout); + let fields: Vec = text + .trim() + .split(',') + .map(|field| field.trim().parse().unwrap()) + .collect(); + (fields[0], fields[1], fields[2]) +} + +#[tokio::test(flavor = "multi_thread")] +async fn odd_size_export_survives_a_crashing_media_foundation_self_test() { + cap_enc_ffmpeg::isolated_self_test::register_host( + std::env::current_exe().unwrap(), + vec![ + CHILD_ENTRY.into(), + "--exact".into(), + "--nocapture".into(), + "--test-threads=1".into(), + ], + ); + + for (width, height) in [(1446, 812), (1366, 770), (1914, 1078)] { + let directory = tempfile::tempdir().unwrap(); + let project = directory.path().join("project.cap"); + create_project(&project, width, height); + + let base = Box::pin( + ExporterBase::builder(project.clone()) + .with_config(ProjectConfiguration::default()) + .with_output_path(directory.path().join("export.mp4")) + .build(), + ) + .await + .unwrap(); + let written = Box::pin( + Mp4ExportSettings { + fps: 30, + resolution_base: XY::new(width, height), + compression: ExportCompression::Maximum, + custom_bpp: None, + force_ffmpeg_decoder: false, + optimize_filesize: false, + } + .export(base, |_| true), + ) + .await + .unwrap_or_else(|e| panic!("{width}x{height}: {e}")); + + let (out_width, out_height, frames) = probe(&written); + eprintln!("{width}x{height} exported as {out_width}x{out_height}, {frames} frames"); + assert!( + !out_width.is_multiple_of(16), + "{width}x{height} exported at {out_width}x{out_height}, which Intel's transform takes" + ); + assert!(out_width.abs_diff(width) <= 4 && out_height.abs_diff(height) <= 2); + assert!(frames >= 29, "{width}x{height}: {frames} frames"); + } +} From eeb98ceab422683bdc92bbd6eb28047fd5876e8c Mon Sep 17 00:00:00 2001 From: Richie McIlroy <33632126+richiemcilroy@users.noreply.github.com> Date: Fri, 13 Nov 2026 23:55:50 -0500 Subject: [PATCH 04/10] chore: lock the new encoder crate dependencies --- Cargo.lock | 2 ++ apps/desktop-gpui/Cargo.lock | 1 + 2 files changed, 3 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index d2896ce862a..c53cfcaba8f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1087,6 +1087,7 @@ dependencies = [ "cap-camera", "cap-cli-install", "cap-editor", + "cap-enc-ffmpeg", "cap-export", "cap-media-info", "cap-project", @@ -1386,6 +1387,7 @@ dependencies = [ name = "cap-enc-ffmpeg" version = "0.1.0" dependencies = [ + "cap-enc-mediafoundation", "cap-frame-converter", "cap-media-info", "ffmpeg-next", diff --git a/apps/desktop-gpui/Cargo.lock b/apps/desktop-gpui/Cargo.lock index e67b4e0d3dd..b73b46a89ff 100644 --- a/apps/desktop-gpui/Cargo.lock +++ b/apps/desktop-gpui/Cargo.lock @@ -1670,6 +1670,7 @@ dependencies = [ name = "cap-enc-ffmpeg" version = "0.1.0" dependencies = [ + "cap-enc-mediafoundation", "cap-frame-converter", "cap-media-info", "ffmpeg-next", From e142c671b5a5b6fb7058906857bb4ef646c64aef Mon Sep 17 00:00:00 2001 From: Richie McIlroy <33632126+richiemcilroy@users.noreply.github.com> Date: Sat, 14 Nov 2026 00:02:31 -0500 Subject: [PATCH 05/10] fix(enc-ffmpeg): name the self-test pixel format through its descriptor --- crates/enc-ffmpeg/src/video/isolated_self_test.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/crates/enc-ffmpeg/src/video/isolated_self_test.rs b/crates/enc-ffmpeg/src/video/isolated_self_test.rs index 176aa45eb52..0b15ef544c5 100644 --- a/crates/enc-ffmpeg/src/video/isolated_self_test.rs +++ b/crates/enc-ffmpeg/src/video/isolated_self_test.rs @@ -61,7 +61,11 @@ impl Request { .iter() .map(|(k, v)| (k.to_owned(), v.to_owned())) .collect(), - pixel_format: input_config.pixel_format.name().to_owned(), + pixel_format: input_config + .pixel_format + .descriptor() + .map_or("none", |descriptor| descriptor.name()) + .to_owned(), width: input_config.width, height: input_config.height, time_base: (input_config.time_base.0, input_config.time_base.1), From a095be7a134943b7cc09232aeb4b36dc8c3b742d Mon Sep 17 00:00:00 2001 From: Richie McIlroy <33632126+richiemcilroy@users.noreply.github.com> Date: Sat, 14 Nov 2026 00:07:36 -0500 Subject: [PATCH 06/10] test(enc-ffmpeg): let an injected self-test fault target one encoder --- crates/enc-ffmpeg/src/video/h264.rs | 2 +- .../src/video/isolated_self_test.rs | 28 +++++++++++++++---- 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/crates/enc-ffmpeg/src/video/h264.rs b/crates/enc-ffmpeg/src/video/h264.rs index fdb9a697f4a..09448689741 100644 --- a/crates/enc-ffmpeg/src/video/h264.rs +++ b/crates/enc-ffmpeg/src/video/h264.rs @@ -1957,7 +1957,7 @@ pub(crate) fn hardware_encoder_self_test( crf: Option, ) -> Result<(), String> { #[cfg(debug_assertions)] - isolated_self_test::inject_fault()?; + isolated_self_test::inject_fault(codec.name())?; // A throwaway encoder with production settings; frames are built directly // in the negotiated output format, so no converter is needed. diff --git a/crates/enc-ffmpeg/src/video/isolated_self_test.rs b/crates/enc-ffmpeg/src/video/isolated_self_test.rs index 0b15ef544c5..01e7d1f2893 100644 --- a/crates/enc-ffmpeg/src/video/isolated_self_test.rs +++ b/crates/enc-ffmpeg/src/video/isolated_self_test.rs @@ -173,18 +173,26 @@ fn serve(raw: &str) -> Result<(), String> { ) } +/// `kind` faults every hardware self-test, `kind:codec` only that codec's. #[cfg(debug_assertions)] -pub(crate) fn inject_fault() -> Result<(), String> { - match std::env::var(FAULT_ENV).as_deref() { - Ok("access_violation") => unsafe { +pub(crate) fn inject_fault(codec: &str) -> Result<(), String> { + let Ok(spec) = std::env::var(FAULT_ENV) else { + return Ok(()); + }; + let (kind, only) = spec.split_once(':').unwrap_or((spec.as_str(), codec)); + if only != codec { + return Ok(()); + } + match kind { + "access_violation" => unsafe { std::ptr::without_provenance_mut::(0x10).write_volatile(1); unreachable!() }, - Ok("abort") => std::process::abort(), - Ok("hang") => loop { + "abort" => std::process::abort(), + "hang" => loop { std::thread::sleep(Duration::from_secs(60)); }, - Ok("fail") => Err("injected self-test failure".to_string()), + "fail" => Err("injected self-test failure".to_string()), _ => Ok(()), } } @@ -364,6 +372,14 @@ pub(crate) mod tests { run_child(&odd, Some("fail"), CHILD_TIMEOUT), Err("injected self-test failure".to_string()) ); + assert_eq!( + run_child(&odd, Some("access_violation:h264_mf"), CHILD_TIMEOUT), + Ok(()) + ); + assert_eq!( + run_child(&odd, Some("fail:libx264"), CHILD_TIMEOUT), + Err("injected self-test failure".to_string()) + ); let started = Instant::now(); let hung = run_child(&odd, Some("hang"), Duration::from_secs(2)).unwrap_err(); From 307f17d9432a472befd428a683dda60955dc48ec Mon Sep 17 00:00:00 2001 From: Richie McIlroy <33632126+richiemcilroy@users.noreply.github.com> Date: Sat, 14 Nov 2026 01:37:45 -0500 Subject: [PATCH 07/10] fix(enc-ffmpeg): read a self-test verdict that shares a line with harness output --- .../src/video/isolated_self_test.rs | 29 ++++++++++++------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/crates/enc-ffmpeg/src/video/isolated_self_test.rs b/crates/enc-ffmpeg/src/video/isolated_self_test.rs index 01e7d1f2893..5866f3f0b0a 100644 --- a/crates/enc-ffmpeg/src/video/isolated_self_test.rs +++ b/crates/enc-ffmpeg/src/video/isolated_self_test.rs @@ -115,12 +115,12 @@ pub fn run_child_if_requested() { let mut stdout = std::io::stdout().lock(); let code = match verdict { Ok(()) => { - let _ = writeln!(stdout, "{VERDICT_PREFIX}ok"); + let _ = writeln!(stdout, "\n{VERDICT_PREFIX}ok"); 0 } Err(reason) => { let reason = reason.replace(['\r', '\n'], " "); - let _ = writeln!(stdout, "{VERDICT_PREFIX}fail {reason}"); + let _ = writeln!(stdout, "\n{VERDICT_PREFIX}fail {reason}"); FAIL_EXIT_CODE } }; @@ -267,19 +267,28 @@ fn run_with( } fn verdict(status: ExitStatus, output: &str) -> Result<(), String> { - let line = output - .lines() - .rev() - .find_map(|line| line.trim().strip_prefix(VERDICT_PREFIX)); + // A test harness child prints its test's name without a newline before + // the test runs, so the verdict can share a line with it. + let line = output.lines().rev().find_map(|line| { + line.find(VERDICT_PREFIX) + .map(|start| line[start + VERDICT_PREFIX.len()..].trim()) + }); match (status.code(), line) { (Some(0), Some("ok")) => Ok(()), (Some(FAIL_EXIT_CODE), Some(line)) => { Err(line.strip_prefix("fail ").unwrap_or(line).trim().to_owned()) } - (code, _) => Err(format!( - "encoder crashed its isolated self-test process ({})", - describe_exit(status, code) - )), + (code, _) => { + let mut reason = format!( + "encoder crashed its isolated self-test process ({})", + describe_exit(status, code) + ); + if let Some(last) = output.lines().rev().map(str::trim).find(|l| !l.is_empty()) { + reason.push_str(": "); + reason.extend(last.chars().take(200)); + } + Err(reason) + } } } From e16bdc3992073511a644ad3e799c52249a54e3d6 Mon Sep 17 00:00:00 2001 From: Richie McIlroy <33632126+richiemcilroy@users.noreply.github.com> Date: Sat, 14 Nov 2026 01:56:26 -0500 Subject: [PATCH 08/10] test: run the Windows fault steps by full test path and assert the child crashed --- .../workflows/windows-recording-encoder.yml | 4 ++-- crates/enc-ffmpeg/src/video/h264.rs | 20 +++++++++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/.github/workflows/windows-recording-encoder.yml b/.github/workflows/windows-recording-encoder.yml index 35cd4eec448..e12e993a5fe 100644 --- a/.github/workflows/windows-recording-encoder.yml +++ b/.github/workflows/windows-recording-encoder.yml @@ -72,12 +72,12 @@ jobs: - name: Media Foundation self-test fault falls back (access violation) env: CAP_H264_SELF_TEST_FAULT: access_violation - run: cargo test --locked -p cap-enc-ffmpeg --lib media_foundation_self_test_fault_falls_back_to_libx264_at_odd_sizes -- --exact --nocapture + run: cargo test --locked -p cap-enc-ffmpeg --lib video::h264::self_test_tests::media_foundation_self_test_fault_falls_back_to_libx264_at_odd_sizes -- --exact --nocapture - name: Media Foundation self-test fault falls back (fast fail 0xc0000409) env: CAP_H264_SELF_TEST_FAULT: abort - run: cargo test --locked -p cap-enc-ffmpeg --lib media_foundation_self_test_fault_falls_back_to_libx264_at_odd_sizes -- --exact --nocapture + run: cargo test --locked -p cap-enc-ffmpeg --lib video::h264::self_test_tests::media_foundation_self_test_fault_falls_back_to_libx264_at_odd_sizes -- --exact --nocapture - name: Odd size export with a faulting Media Foundation self-test env: diff --git a/crates/enc-ffmpeg/src/video/h264.rs b/crates/enc-ffmpeg/src/video/h264.rs index 09448689741..8845afdb11f 100644 --- a/crates/enc-ffmpeg/src/video/h264.rs +++ b/crates/enc-ffmpeg/src/video/h264.rs @@ -2288,6 +2288,26 @@ mod self_test_tests { }) .unwrap(); assert!(packets >= 10, "{width}x{height}: {packets} packets"); + + if std::env::var_os("CAP_H264_SELF_TEST_FAULT").is_some() { + let (codec, options) = get_codec_and_options( + &config, + H264Preset::Ultrafast, + Some(&["h264_mf"]), + false, + None, + ) + .into_iter() + .next() + .expect("FFmpeg build has h264_mf"); + let reason = + cached_hardware_self_test(codec, &options, &config, width, height, 0.3, None) + .unwrap_err(); + assert!( + reason.starts_with("encoder crashed its isolated self-test process"), + "{width}x{height}: {reason}" + ); + } } } From 1ca5dbad9bd742904ff7022810f4331eaaf5e092 Mon Sep 17 00:00:00 2001 From: Richie McIlroy <33632126+richiemcilroy@users.noreply.github.com> Date: Sat, 14 Nov 2026 02:24:21 -0500 Subject: [PATCH 09/10] test(enc-ffmpeg): keep fault injection tests to debug builds --- .../src/video/isolated_self_test.rs | 57 ++++++++++--------- 1 file changed, 29 insertions(+), 28 deletions(-) diff --git a/crates/enc-ffmpeg/src/video/isolated_self_test.rs b/crates/enc-ffmpeg/src/video/isolated_self_test.rs index 5866f3f0b0a..8c194ea7876 100644 --- a/crates/enc-ffmpeg/src/video/isolated_self_test.rs +++ b/crates/enc-ffmpeg/src/video/isolated_self_test.rs @@ -343,9 +343,13 @@ pub(crate) mod tests { Request::new(codec, &options, &config, width, height, 0.3, None) } - fn run_child(request: &Request, fault: Option<&str>, timeout: Duration) -> Result<(), String> { - let env: Vec<(&str, &str)> = fault.map(|fault| (FAULT_ENV, fault)).into_iter().collect(); - run_with(&test_host(), request, timeout, &env) + fn run_child(request: &Request, timeout: Duration) -> Result<(), String> { + run_with(&test_host(), request, timeout, &[]) + } + + #[cfg(debug_assertions)] + fn run_faulting_child(request: &Request, fault: &str, timeout: Duration) -> Result<(), String> { + run_with(&test_host(), request, timeout, &[(FAULT_ENV, fault)]) } #[test] @@ -363,47 +367,44 @@ pub(crate) mod tests { #[test] fn isolated_self_test_outcomes() { let odd = gray_request("libx264", 1438, 812); - assert_eq!(run_child(&odd, None, CHILD_TIMEOUT), Ok(())); - - let crashed = run_child(&odd, Some("access_violation"), CHILD_TIMEOUT).unwrap_err(); - assert!( - crashed.starts_with("encoder crashed its isolated self-test process"), - "{crashed}" - ); + assert_eq!(run_child(&odd, CHILD_TIMEOUT), Ok(())); - let aborted = run_child(&odd, Some("abort"), CHILD_TIMEOUT).unwrap_err(); - assert!( - aborted.starts_with("encoder crashed its isolated self-test process"), - "{aborted}" + let missing = run_child(&gray_request("h264_not_an_encoder", 64, 64), CHILD_TIMEOUT); + assert_eq!( + missing, + Err("encoder h264_not_an_encoder not found".to_string()) ); + } + /// Fault injection only exists in debug builds. + #[cfg(debug_assertions)] + #[test] + fn isolated_self_test_survives_injected_faults() { + let odd = gray_request("libx264", 1438, 812); + for fault in ["access_violation", "abort"] { + let crashed = run_faulting_child(&odd, fault, CHILD_TIMEOUT).unwrap_err(); + assert!( + crashed.starts_with("encoder crashed its isolated self-test process"), + "{fault}: {crashed}" + ); + } assert_eq!( - run_child(&odd, Some("fail"), CHILD_TIMEOUT), + run_faulting_child(&odd, "fail", CHILD_TIMEOUT), Err("injected self-test failure".to_string()) ); assert_eq!( - run_child(&odd, Some("access_violation:h264_mf"), CHILD_TIMEOUT), + run_faulting_child(&odd, "access_violation:h264_mf", CHILD_TIMEOUT), Ok(()) ); assert_eq!( - run_child(&odd, Some("fail:libx264"), CHILD_TIMEOUT), + run_faulting_child(&odd, "fail:libx264", CHILD_TIMEOUT), Err("injected self-test failure".to_string()) ); let started = Instant::now(); - let hung = run_child(&odd, Some("hang"), Duration::from_secs(2)).unwrap_err(); + let hung = run_faulting_child(&odd, "hang", Duration::from_secs(2)).unwrap_err(); assert!(hung.contains("did not finish"), "{hung}"); assert!(started.elapsed() < Duration::from_secs(10)); - - let missing = run_child( - &gray_request("h264_not_an_encoder", 64, 64), - None, - CHILD_TIMEOUT, - ); - assert_eq!( - missing, - Err("encoder h264_not_an_encoder not found".to_string()) - ); } #[test] From 713fa6e341abacb327c6d8b86031c6957f450722 Mon Sep 17 00:00:00 2001 From: Richie McIlroy <33632126+richiemcilroy@users.noreply.github.com> Date: Sat, 14 Nov 2026 02:30:42 -0500 Subject: [PATCH 10/10] test(enc-ffmpeg): build the Windows test host helper only on Windows --- crates/enc-ffmpeg/src/video/isolated_self_test.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/crates/enc-ffmpeg/src/video/isolated_self_test.rs b/crates/enc-ffmpeg/src/video/isolated_self_test.rs index 8c194ea7876..b899345ea2d 100644 --- a/crates/enc-ffmpeg/src/video/isolated_self_test.rs +++ b/crates/enc-ffmpeg/src/video/isolated_self_test.rs @@ -325,6 +325,7 @@ pub(crate) mod tests { } } + #[cfg(target_os = "windows")] pub(crate) fn register_test_host() { let host = test_host(); register_host(host.program, host.args);