Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

test(verify): add the chaos suite: hostile device data, files, projects and settings through every engine

Richie McIlroy9676170a0437
diff --git a/Cargo.lock b/Cargo.lock
index 00cb9905b..317744992 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -2159,6 +2159,7 @@ version = "0.1.0"
dependencies = [
"anyhow",
"cap-audio",
+ "cap-camera-ffmpeg",
"cap-camera-windows",
"cap-editor",
"cap-enc-avfoundation",
diff --git a/crates/recording/src/feeds/microphone.rs b/crates/recording/src/feeds/microphone.rs
index 9c955e5b2..754cb064a 100644
--- a/crates/recording/src/feeds/microphone.rs
+++ b/crates/recording/src/feeds/microphone.rs
@@ -3176,6 +3176,30 @@ pub mod synthetic {
.await;
}
+ /// Delivers one raw callback buffer as a device would, in any sample
+ /// format, rate and channel count, whatever the feed was opened with.
+ pub async fn push_raw(
+ &self,
+ data: Vec<u8>,
+ format: SampleFormat,
+ sample_rate: u32,
+ channels: u16,
+ timestamp: Timestamp,
+ ) {
+ let _ = self
+ .feed
+ .tell(MicrophoneSamples {
+ stream_id: STREAM_ID,
+ data,
+ format,
+ sample_rate,
+ channels,
+ info: zero_callback_info(),
+ timestamp,
+ })
+ .await;
+ }
+
/// Hands samples over the way the capture callback does
/// (`enqueue_microphone_samples`: `try_send`, never waiting).
pub fn try_push(&self, samples: &[f32], timestamp: Timestamp) -> crate::feeds::Admission {
diff --git a/crates/verify/Cargo.toml b/crates/verify/Cargo.toml
index 453ddfbc8..f81f0e553 100644
--- a/crates/verify/Cargo.toml
+++ b/crates/verify/Cargo.toml
@@ -35,15 +35,16 @@ cap-editor = { path = "../editor", features = ["test-hooks"] }
cap-audio = { path = "../audio" }
cap-export = { path = "../export" }
cap-rendering = { path = "../rendering" }
+cpal = { workspace = true }
tempfile = "3"
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
[target.'cfg(target_os = "macos")'.dev-dependencies]
+cap-camera-ffmpeg = { path = "../camera-ffmpeg" }
scap-targets = { path = "../scap-targets" }
[target.'cfg(windows)'.dev-dependencies]
scap-targets = { path = "../scap-targets" }
cap-camera-windows = { path = "../camera-windows" }
cap-mediafoundation-utils = { path = "../mediafoundation-utils" }
-cpal = { workspace = true }
windows = { workspace = true, features = ["Win32_Media_MediaFoundation"] }
diff --git a/crates/verify/README.md b/crates/verify/README.md
index fb01d8488..87210e0ea 100644
--- a/crates/verify/README.md
+++ b/crates/verify/README.md
@@ -19,6 +19,7 @@ The suites are listed in `scripts/verify/suites.json`:
| `seek` | random, keyframe and scrub seeks | linux, macos | all |
| `audio` | export and offline playback audio against sample-accurate markers | linux, macos, windows | all |
| `editing` | random edit sequences exported and checked frame by frame | linux | linux |
+| `chaos` | hostile device data, files, projects and settings: no panic, abort, signal, Objective-C exception, hang or wrong success | macos | macos |
| `captions` | fake-transcriber captions: segments and rendered captions on the right frames | linux | linux |
| `decoder-model-ffmpeg` | FFmpeg decoder decisions on scripted media (`cap-rendering` `decoder::model`) | linux | linux |
| `decoder-model-avf` | AVAssetReader decoder decisions on scripted media | macos | macos |
@@ -42,6 +43,44 @@ locally, the platform-independent suites run on every OS.
`CAP_VERIFY_TIER=full` (the FULL tier) uses longer fixtures and more seeds.
+## Chaos suite
+
+`crates/verify/tests/chaos.rs` tries to crash the engines. Every probe feeds one
+hostile input to a product entry point and runs in its own child process (the
+test binary re-run with `CAP_VERIFY_CHAOS_CHILD`), so an abort, a fatal signal,
+an Objective-C exception or a hang ends the child, not the run. A panic hook in
+the child records every panic on any thread. Outcomes:
+
+- `pass`: the engine returned an error, or it succeeded and its output checks
+ out (readable media with packets when the input had usable frames).
+- `panic`, `abort` (fatal signal or unexpected exit), `objc-exception`,
+ `hang` (no verdict before the probe's timeout, reproduced once more) and
+ `wrong-success` (success reported for an empty or unreadable output).
+
+Areas, one test per area (`chaos_recording`, `chaos_files`, `chaos_export`,
+`chaos_editor`, `chaos_fragments`, `chaos_fuzz`):
+
+- `chaos/record/`: synthetic frames, audio buffers and control sequences
+ through `OutputPipeline` with the Studio and Instant muxers (in-process and
+ out-of-process on macOS), the microphone feed and source, the macOS camera
+ conversion, and the Studio and Instant recording actors in camera-only mode
+ with synthetic camera and microphone feeds, finalized like the app.
+- `chaos/files/`, `chaos/export/`, `chaos/editor/`: a generated project with
+ one damaged file, metadata or configuration field, opened like the editor's
+ preflight, in `EditorInstance` (preview, playback, seeks) and exported;
+ hostile export settings; non-finite values pushed in memory.
+- `chaos/fragments/`: damaged fragmented recordings through crash recovery and
+ Instant finalization.
+- `chaos/fuzz/`: seeded fuzzing (structure-aware JSON, box-aware MP4 bytes).
+ Each seed runs in a batch child; a crash or hang is pinned to its seed, and
+ a failing seed is shrunk by dropping mutations while it fails the same way.
+ The failure prints `CAP_VERIFY_CHAOS_SEED` and `CAP_VERIFY_CHAOS_KEEP` for
+ its repro.
+
+`CAP_VERIFY_CHAOS_JOBS` sets the children run at once per test (default a
+quarter of the cores, at most 3). `CAP_VERIFY_CHAOS_KEEP_SCRATCH=1` keeps
+each probe's scratch directory, child log and stderr.
+
## Running locally
```sh
diff --git a/crates/verify/tests/chaos.rs b/crates/verify/tests/chaos.rs
new file mode 100644
index 000000000..dddbedac1
--- /dev/null
+++ b/crates/verify/tests/chaos.rs
@@ -0,0 +1,70 @@
+//! Chaos and resilience suite: hostile device data, files, projects and
+//! settings through the engines, every probe in its own child process, so
+//! panics, aborts, fatal signals, Objective-C exceptions and hangs are
+//! classified instead of ending the run (see `chaos_support/harness.rs`).
+//!
+//! Outcomes: a clean error or a checked success passes; `panic`, `abort`,
+//! `objc-exception`, `hang` and `wrong-success` (success reported for an
+//! empty or unreadable output) are failures, judged against the strict
+//! expected failures in `crates/verify/expectations.toml`.
+
+mod chaos_support;
+mod common;
+
+use chaos_support::{all, harness};
+
+#[test]
+fn chaos_child() {
+ harness::child_main(all);
+}
+
+#[test]
+fn chaos_recording() {
+ harness::run_group(all, "chaos/record/");
+}
+
+#[test]
+fn chaos_files() {
+ if std::env::var(harness::CHILD_ENV).is_err() {
+ common::init();
+ chaos_support::project::base().expect("base fixture");
+ }
+ harness::run_group(all, "chaos/files/");
+}
+
+#[test]
+fn chaos_export() {
+ if std::env::var(harness::CHILD_ENV).is_err() {
+ common::init();
+ chaos_support::project::base().expect("base fixture");
+ }
+ harness::run_group(all, "chaos/export/");
+}
+
+#[test]
+fn chaos_fragments() {
+ if std::env::var(harness::CHILD_ENV).is_err() {
+ common::init();
+ chaos_support::fragments::prepare_fixture().expect("fragment fixture");
+ }
+ harness::run_group(all, "chaos/fragments/");
+}
+
+#[test]
+fn chaos_fuzz() {
+ if std::env::var(harness::CHILD_ENV).is_err() {
+ common::init();
+ chaos_support::fragments::prepare_fixture().expect("fragment fixture");
+ chaos_support::project::base().expect("base fixture");
+ }
+ harness::run_group(all, "chaos/fuzz/");
+}
+
+#[test]
+fn chaos_editor() {
+ if std::env::var(harness::CHILD_ENV).is_err() {
+ common::init();
+ chaos_support::project::base().expect("base fixture");
+ }
+ harness::run_group(all, "chaos/editor/");
+}
diff --git a/crates/verify/tests/chaos_support/actors.rs b/crates/verify/tests/chaos_support/actors.rs
new file mode 100644
index 000000000..88de64b91
--- /dev/null
+++ b/crates/verify/tests/chaos_support/actors.rs
@@ -0,0 +1,449 @@
+//! The Studio and Instant recording actors, camera-only (the one capture
+//! target that needs no screen capture permission), fed by synthetic camera
+//! and microphone feeds and driven through hostile control sequences, then
+//! finalized like the app.
+
+use super::harness::Probe;
+
+#[cfg(target_os = "macos")]
+mod mac {
+ use std::{sync::Arc, time::Duration};
+
+ use cap_media_info::{Pixel, RawVideoFormat, VideoInfo};
+ use cap_recording::{
+ NativeCameraFrame,
+ feeds::{camera::synthetic::SyntheticCamera, microphone::synthetic::SyntheticMicrophone},
+ instant_recording,
+ recovery::RecoveryManager,
+ screen_capture::ScreenCaptureTarget,
+ studio_recording,
+ };
+ use cap_timestamp::{MachAbsoluteTimestamp, Timestamp};
+
+ use super::super::{
+ harness::{Ctx, Probe, Verdict},
+ media_check,
+ };
+
+ #[derive(Clone, Copy, Debug, PartialEq, Eq)]
+ pub enum Mode {
+ Studio,
+ Instant,
+ }
+
+ #[derive(Clone, Copy, Debug, PartialEq)]
+ pub enum Step {
+ /// Camera and mic frames for this many seconds.
+ Frames(f64),
+ Pause,
+ Resume,
+ Stop,
+ Cancel,
+ /// Stop and cancel at the same time.
+ StopAndCancel,
+ DropCamera,
+ DropMic,
+ /// The camera switches to this size and format.
+ CameraShape(u32, u32, Pixel),
+ /// Camera timestamps run backwards from here.
+ CameraBackwards,
+ }
+
+ pub struct Sequence {
+ pub name: &'static str,
+ pub camera_info: VideoInfo,
+ pub steps: Vec<Step>,
+ pub expect_media: bool,
+ }
+
+ fn nv12(w: u32, h: u32, fps: u32) -> VideoInfo {
+ VideoInfo::from_raw(RawVideoFormat::Nv12, w, h, fps)
+ }
+
+ pub fn sequences() -> Vec<Sequence> {
+ use Step::*;
+ let seq = |name, steps: Vec<Step>, expect_media| Sequence {
+ name,
+ camera_info: nv12(320, 240, 30),
+ steps,
+ expect_media,
+ };
+ vec![
+ seq("control", vec![Frames(1.0), Stop], true),
+ seq("stop-before-first-frame", vec![Stop], false),
+ seq("stop-twice", vec![Frames(0.6), Stop, Stop], true),
+ seq(
+ "pause-before-first-frame",
+ vec![Pause, Frames(0.4), Resume, Frames(0.6), Stop],
+ true,
+ ),
+ seq(
+ "pause-resume-storm",
+ vec![
+ Frames(0.3),
+ Pause,
+ Resume,
+ Pause,
+ Pause,
+ Resume,
+ Resume,
+ Pause,
+ Frames(0.2),
+ Resume,
+ Pause,
+ Stop,
+ ],
+ true,
+ ),
+ seq(
+ "resume-without-pause",
+ vec![Resume, Frames(0.6), Stop],
+ true,
+ ),
+ seq("cancel-then-stop", vec![Frames(0.5), Cancel, Stop], false),
+ seq(
+ "stop-and-cancel-together",
+ vec![Frames(0.5), StopAndCancel],
+ false,
+ ),
+ seq("pause-then-stop", vec![Frames(0.5), Pause, Stop], true),
+ seq(
+ "camera-vanishes",
+ vec![Frames(0.5), DropCamera, Frames(0.5), Stop],
+ true,
+ ),
+ seq(
+ "mic-vanishes",
+ vec![Frames(0.5), DropMic, Frames(0.5), Stop],
+ true,
+ ),
+ seq(
+ "camera-size-changes",
+ vec![
+ Frames(0.3),
+ CameraShape(640, 480, Pixel::NV12),
+ Frames(0.3),
+ CameraShape(161, 91, Pixel::NV12),
+ Frames(0.3),
+ Stop,
+ ],
+ true,
+ ),
+ seq(
+ "camera-format-changes",
+ vec![
+ Frames(0.3),
+ CameraShape(320, 240, Pixel::BGRA),
+ Frames(0.3),
+ CameraShape(320, 240, Pixel::UYVY422),
+ Frames(0.3),
+ Stop,
+ ],
+ true,
+ ),
+ seq(
+ "camera-ts-backwards",
+ vec![Frames(0.4), CameraBackwards, Frames(0.4), Stop],
+ true,
+ ),
+ Sequence {
+ name: "camera-info-zero",
+ camera_info: nv12(0, 0, 0),
+ steps: vec![Frames(0.6), Stop],
+ expect_media: false,
+ },
+ Sequence {
+ name: "camera-info-fps-1000",
+ camera_info: nv12(320, 240, 1000),
+ steps: vec![Frames(0.6), Stop],
+ expect_media: true,
+ },
+ ]
+ }
+
+ enum Handle {
+ Studio(studio_recording::ActorHandle),
+ Instant(instant_recording::ActorHandle),
+ }
+
+ impl Handle {
+ async fn pause(&self) -> anyhow::Result<()> {
+ match self {
+ Handle::Studio(h) => h.pause().await,
+ Handle::Instant(h) => h.pause().await,
+ }
+ }
+ async fn resume(&self) -> anyhow::Result<()> {
+ match self {
+ Handle::Studio(h) => h.resume().await,
+ Handle::Instant(h) => h.resume().await,
+ }
+ }
+ async fn cancel(&self) -> anyhow::Result<()> {
+ match self {
+ Handle::Studio(h) => h.cancel().await,
+ Handle::Instant(h) => h.cancel().await,
+ }
+ }
+ async fn stop(&self) -> anyhow::Result<()> {
+ match self {
+ Handle::Studio(h) => h.stop().await.map(|_| ()),
+ Handle::Instant(h) => h.stop().await.map(|_| ()),
+ }
+ }
+ }
+
+ fn host_now() -> u64 {
+ cidre::mach::abs_time()
+ }
+
+ fn camera_frame(
+ width: u32,
+ height: u32,
+ format: Pixel,
+ index: u32,
+ at: u64,
+ ) -> Option<NativeCameraFrame> {
+ let sample_buf = super::super::record::mac_sample(format, width, height, 30, index).ok()?;
+ Some(NativeCameraFrame {
+ sample_buf,
+ timestamp: Timestamp::MachAbsoluteTime(MachAbsoluteTimestamp::new(at)),
+ })
+ }
+
+ fn ticks(secs: f64) -> u64 {
+ let info = cidre::mach::TimeBaseInfo::new();
+ (secs * 1e9 * f64::from(info.denom) / f64::from(info.numer)) as u64
+ }
+
+ pub async fn run(ctx: &Ctx, mode: Mode, seq: &Sequence) -> anyhow::Result<Verdict> {
+ let project = ctx.path("project.cap");
+ std::fs::create_dir_all(&project)?;
+ let camera = Some(Arc::new(SyntheticCamera::spawn(
+ "Cap Chaos Camera",
+ seq.camera_info,
+ )));
+ let mic = Some(Arc::new(SyntheticMicrophone::spawn(
+ "Cap Chaos Microphone",
+ 48_000,
+ 1,
+ )));
+ let camera_lock = match camera.as_ref().map(|c| c.lock()) {
+ Some(lock) => match lock.await {
+ Ok(lock) => Arc::new(lock),
+ Err(e) => return Ok(Verdict::rejected(format!("camera lock: {e:#}"))),
+ },
+ None => unreachable!(),
+ };
+ let mic_lock = match mic.as_ref().map(|m| m.lock()) {
+ Some(lock) => match lock.await {
+ Ok(lock) => Arc::new(lock),
+ Err(e) => return Ok(Verdict::rejected(format!("mic lock: {e:#}"))),
+ },
+ None => unreachable!(),
+ };
+ let handle = match mode {
+ Mode::Studio => {
+ studio_recording::Actor::builder(project.clone(), ScreenCaptureTarget::CameraOnly)
+ .with_camera_feed(camera_lock.clone())
+ .with_mic_feed(mic_lock.clone())
+ .with_keyboard_capture(false)
+ .build(None)
+ .await
+ .map(Handle::Studio)
+ }
+ Mode::Instant => {
+ instant_recording::Actor::builder(project.clone(), ScreenCaptureTarget::CameraOnly)
+ .with_camera_feed(camera_lock.clone())
+ .with_mic_feed(mic_lock.clone())
+ .build(None)
+ .await
+ .map(Handle::Instant)
+ }
+ };
+ let handle = match handle {
+ Ok(h) => h,
+ Err(e) => return Ok(Verdict::rejected(format!("build: {e:#}"))),
+ };
+ let mut camera = camera;
+ let mut mic = mic;
+ let (mut width, mut height, mut format) = (
+ seq.camera_info.width.max(2),
+ seq.camera_info.height.max(2),
+ Pixel::NV12,
+ );
+ let start = host_now();
+ let mut clock = 0.0f64;
+ let mut direction = 1.0f64;
+ let mut index = 0u32;
+ let mut first_error: Option<String> = None;
+ let mut stopped = false;
+ for step in &seq.steps {
+ let result = match *step {
+ Step::Frames(secs) => {
+ let end = clock + secs;
+ while clock < end {
+ tokio::time::sleep(Duration::from_millis(33)).await;
+ let at = if direction > 0.0 {
+ start + ticks(clock)
+ } else {
+ start.saturating_sub(ticks(clock))
+ };
+ if let Some(camera) = &camera
+ && let Some(frame) = camera_frame(width, height, format, index, at)
+ {
+ let _ = camera.try_push_native(frame);
+ }
+ if let Some(mic) = &mic {
+ let samples = vec![0.1f32; 1600];
+ let _ = mic.try_push(
+ &samples,
+ Timestamp::MachAbsoluteTime(MachAbsoluteTimestamp::new(
+ start + ticks(clock),
+ )),
+ );
+ }
+ clock += 1.0 / 30.0;
+ index += 1;
+ }
+ Ok(())
+ }
+ Step::Pause => handle.pause().await,
+ Step::Resume => handle.resume().await,
+ Step::Cancel => handle.cancel().await,
+ Step::Stop => {
+ let r = handle.stop().await;
+ stopped |= r.is_ok();
+ r
+ }
+ Step::StopAndCancel => {
+ let (a, b) = tokio::join!(handle.stop(), handle.cancel());
+ stopped |= a.is_ok();
+ a.and(b)
+ }
+ Step::DropCamera => {
+ camera = None;
+ Ok(())
+ }
+ Step::DropMic => {
+ mic = None;
+ Ok(())
+ }
+ Step::CameraShape(w, h, f) => {
+ width = w;
+ height = h;
+ format = f;
+ Ok(())
+ }
+ Step::CameraBackwards => {
+ direction = -1.0;
+ Ok(())
+ }
+ };
+ if let Err(e) = result
+ && first_error.is_none()
+ {
+ first_error = Some(format!("{step:?}: {e:#}"));
+ }
+ }
+ drop(camera);
+ drop(mic);
+ if !stopped {
+ return Ok(Verdict::rejected(
+ first_error.unwrap_or_else(|| "never stopped".into()),
+ ));
+ }
+ let verdict = match mode {
+ Mode::Studio => {
+ let path = project.clone();
+ let remux =
+ tokio::task::spawn_blocking(move || RecoveryManager::remux_if_needed(&path))
+ .await?;
+ if let Err(e) = remux {
+ return Ok(Verdict::rejected(format!("finalize: {e}")));
+ }
+ match cap_project::RecordingMeta::load_for_project(&project) {
+ Err(e) => {
+ Verdict::Wrong(format!("stopped cleanly but the meta is unreadable: {e}"))
+ }
+ Ok(meta) => match meta.studio_meta() {
+ None => Verdict::Wrong("stopped cleanly but the meta is not studio".into()),
+ Some(studio) => {
+ let display = match studio {
+ cap_project::StudioRecordingMeta::SingleSegment { segment } => {
+ segment.display.path.to_path(&project)
+ }
+ cap_project::StudioRecordingMeta::MultipleSegments { inner } => {
+ match inner.segments.first() {
+ Some(s) => s.display.path.to_path(&project),
+ None => {
+ return Ok(if seq.expect_media {
+ Verdict::Wrong(
+ "stopped cleanly with no segment".into(),
+ )
+ } else {
+ Verdict::Ok("no segment".into())
+ });
+ }
+ }
+ }
+ };
+ media_check::judge_output(&display, seq.expect_media)
+ }
+ },
+ }
+ }
+ Mode::Instant => {
+ let content = project.join("content");
+ let display = content.join("display");
+ let output = content.join("output.mp4");
+ if display.is_dir() {
+ let audio = content.join("audio");
+ let out = output.clone();
+ let finalize = tokio::task::spawn_blocking(move || {
+ RecoveryManager::finalize_instant_output(&display, &audio, &out)
+ })
+ .await?;
+ if let Err(e) = finalize {
+ return Ok(Verdict::rejected(format!("finalize: {e}")));
+ }
+ }
+ media_check::judge_output(&output, seq.expect_media)
+ }
+ };
+ Ok(match (verdict, first_error) {
+ (Verdict::Ok(text), Some(err)) => Verdict::Ok(format!("{text}; {err}")),
+ (v, _) => v,
+ })
+ }
+
+ pub fn probes(out: &mut Vec<Probe>) {
+ for (mode_name, mode) in [("studio", Mode::Studio), ("instant", Mode::Instant)] {
+ for seq in sequences() {
+ let name = seq.name;
+ let seq = Arc::new(seq);
+ let probe = Probe::new(
+ format!("chaos/record/actor/{mode_name}/{name}"),
+ 90,
+ move |ctx| ctx.block_on(run(ctx, mode, &seq)),
+ );
+ let fast = mode == Mode::Studio
+ || matches!(
+ name,
+ "control"
+ | "stop-before-first-frame"
+ | "stop-twice"
+ | "stop-and-cancel-together"
+ | "camera-format-changes"
+ );
+ out.push(if fast { probe } else { probe.full() });
+ }
+ }
+ }
+}
+
+pub fn probes(out: &mut Vec<Probe>) {
+ #[cfg(target_os = "macos")]
+ mac::probes(out);
+ let _ = out;
+}
diff --git a/crates/verify/tests/chaos_support/fragments.rs b/crates/verify/tests/chaos_support/fragments.rs
new file mode 100644
index 000000000..6ef3366c5
--- /dev/null
+++ b/crates/verify/tests/chaos_support/fragments.rs
@@ -0,0 +1,554 @@
+//! Fragment chaos for crash recovery and Instant finalization: a short
+//! fragmented recording made by the product's segmented muxers, copied per
+//! probe and damaged (missing or truncated init segments and fragments,
+//! garbage manifests), then recovered or finalized like the app.
+
+use std::{path::Path, sync::Arc, time::Duration};
+
+use anyhow::{Context, anyhow};
+use cap_media_info::{AudioInfo, RawVideoFormat, Sample, Type, VideoInfo};
+use cap_recording::{
+ AudioFrame, ChannelAudioSource, ChannelAudioSourceConfig, ChannelVideoSource,
+ ChannelVideoSourceConfig, OutputPipeline,
+ ffmpeg::{
+ DashSegmentedAudioMuxer, DashSegmentedAudioMuxerConfig, FFmpegVideoFrame,
+ FragmentedAudioMuxer, FragmentedAudioMuxerConfig, SegmentedVideoMuxer,
+ SegmentedVideoMuxerConfig,
+ },
+ recovery::RecoveryManager,
+};
+use cap_timestamp::{Timestamp, Timestamps};
+use serde_json::json;
+
+use super::{
+ harness::{Ctx, Probe, Verdict},
+ project::copy_dir,
+};
+
+const SECONDS: f64 = 4.5;
+const FPS: u32 = 30;
+
+/// Records the fixture: `studio/` (an in-progress fragmented Studio
+/// recording) and `instant/` (Instant's display and audio fragments).
+async fn record(dir: &Path) -> anyhow::Result<()> {
+ let studio = dir.join("studio");
+ let segment = studio.join("content/segments/segment-0");
+ let instant = dir.join("instant/content");
+ std::fs::create_dir_all(&segment)?;
+ std::fs::create_dir_all(&instant)?;
+ let timestamps = Timestamps::now();
+ let info = VideoInfo::from_raw(RawVideoFormat::Bgra, 320, 180, FPS);
+ let (vtx, vrx) = flume::bounded::<FFmpegVideoFrame>(64);
+ let video = OutputPipeline::builder(segment.join("display"))
+ .with_video::<ChannelVideoSource<FFmpegVideoFrame>>(ChannelVideoSourceConfig::new(
+ info, vrx,
+ ))
+ .with_timestamps(timestamps)
+ .build::<SegmentedVideoMuxer>(SegmentedVideoMuxerConfig {
+ segment_duration: Duration::from_secs(2),
+ ..Default::default()
+ })
+ .await
+ .map_err(|e| anyhow!("display pipeline: {e:#}"))?;
+ let (ivtx, ivrx) = flume::bounded::<FFmpegVideoFrame>(64);
+ let instant_video = OutputPipeline::builder(instant.join("display"))
+ .with_video::<ChannelVideoSource<FFmpegVideoFrame>>(ChannelVideoSourceConfig::new(
+ info, ivrx,
+ ))
+ .with_timestamps(timestamps)
+ .build::<SegmentedVideoMuxer>(SegmentedVideoMuxerConfig {
+ segment_duration: Duration::from_secs(2),
+ ..Default::default()
+ })
+ .await
+ .map_err(|e| anyhow!("instant display pipeline: {e:#}"))?;
+ let mono = AudioInfo::new(Sample::F32(Type::Packed), 48_000, 1)?;
+ let (mut atx, arx) = futures::channel::mpsc::channel::<AudioFrame>(64);
+ let mic = OutputPipeline::builder(segment.join("audio-input.m4a"))
+ .with_audio_source::<ChannelAudioSource>(ChannelAudioSourceConfig::new(mono, arx))
+ .with_timestamps(timestamps)
+ .build::<FragmentedAudioMuxer>(FragmentedAudioMuxerConfig {
+ shared_pause_state: None,
+ })
+ .await
+ .map_err(|e| anyhow!("mic pipeline: {e:#}"))?;
+ let (mut iatx, iarx) = futures::channel::mpsc::channel::<AudioFrame>(64);
+ let instant_audio = OutputPipeline::builder(instant.join("audio"))
+ .with_audio_source::<ChannelAudioSource>(ChannelAudioSourceConfig::new(mono, iarx))
+ .with_timestamps(timestamps)
+ .build::<DashSegmentedAudioMuxer>(DashSegmentedAudioMuxerConfig::default())
+ .await
+ .map_err(|e| anyhow!("instant audio pipeline: {e:#}"))?;
+ let base = timestamps.instant();
+ let frames = (SECONDS * f64::from(FPS)) as u32;
+ for i in 0..frames {
+ let at = base + Duration::from_secs_f64(0.05 + f64::from(i) / f64::from(FPS));
+ tokio::time::sleep_until(at.into()).await;
+ for tx in [&vtx, &ivtx] {
+ let mut frame = ffmpeg::frame::Video::new(ffmpeg::format::Pixel::BGRA, 320, 180);
+ for (n, b) in frame.data_mut(0).iter_mut().enumerate() {
+ *b = (n as u32 ^ i) as u8;
+ }
+ tx.send_async(FFmpegVideoFrame {
+ inner: frame,
+ timestamp: Timestamp::Instant(at),
+ })
+ .await
+ .map_err(|_| anyhow!("video channel closed"))?;
+ }
+ use futures::SinkExt;
+ for tx in [&mut atx, &mut iatx] {
+ let samples: Vec<u8> = (0..1600)
+ .flat_map(|n| ((f64::from(n) * 0.05).sin() as f32 * 0.3).to_ne_bytes())
+ .collect();
+ tx.send(AudioFrame::new(
+ mono.wrap_frame(&samples),
+ Timestamp::Instant(at),
+ ))
+ .await
+ .map_err(|_| anyhow!("audio channel closed"))?;
+ }
+ }
+ tokio::time::sleep(Duration::from_millis(300)).await;
+ video
+ .stop()
+ .await
+ .map_err(|e| anyhow!("display stop: {e:#}"))?;
+ instant_video
+ .stop()
+ .await
+ .map_err(|e| anyhow!("instant display stop: {e:#}"))?;
+ mic.stop().await.map_err(|e| anyhow!("mic stop: {e:#}"))?;
+ instant_audio
+ .stop()
+ .await
+ .map_err(|e| anyhow!("instant audio stop: {e:#}"))?;
+ drop((vtx, ivtx, atx, iatx));
+ let meta = json!({
+ "platform": "MacOS",
+ "pretty_name": "chaos-fragments",
+ "sharing": null,
+ "segments": [{
+ "display": { "path": "content/segments/segment-0/display.mp4", "fps": FPS, "start_time": 0.0 },
+ "mic": { "path": "content/segments/segment-0/audio-input.m4a", "start_time": 0.0 }
+ }],
+ "status": { "status": "InProgress" }
+ });
+ std::fs::write(
+ studio.join("recording-meta.json"),
+ serde_json::to_vec_pretty(&meta)?,
+ )?;
+ Ok(())
+}
+
+fn fixture() -> anyhow::Result<std::path::PathBuf> {
+ let key = cap_verify::fixture::cache_key(
+ "chaos-fragments",
+ &(SECONDS, FPS, cap_verify::pipeline::product_source_hash()),
+ );
+ cap_verify::fixture::ensure_fixture_keyed(key, |dir| {
+ crate::common::runtime().block_on(record(dir))
+ })
+}
+
+/// Makes sure the fixture exists before children race to create it.
+pub fn prepare_fixture() -> anyhow::Result<()> {
+ fixture().map(|_| ())
+}
+
+type Damage = Arc<dyn Fn(&Path) -> anyhow::Result<()> + Send + Sync>;
+
+fn files_matching(dir: &Path, suffix: &str) -> Vec<std::path::PathBuf> {
+ let mut out: Vec<_> = std::fs::read_dir(dir)
+ .map(|entries| {
+ entries
+ .flatten()
+ .map(|e| e.path())
+ .filter(|p| {
+ p.file_name()
+ .and_then(|n| n.to_str())
+ .is_some_and(|n| n.ends_with(suffix))
+ })
+ .collect()
+ })
+ .unwrap_or_default();
+ out.sort();
+ out
+}
+
+fn on_each(
+ dir: &'static str,
+ suffix: &'static str,
+ pick: fn(usize) -> Vec<usize>,
+ act: fn(&Path) -> anyhow::Result<()>,
+) -> Damage {
+ Arc::new(move |root| {
+ let files = files_matching(&root.join(dir), suffix);
+ anyhow::ensure!(!files.is_empty(), "no {suffix} files in {dir}");
+ for i in pick(files.len()) {
+ if let Some(f) = files.get(i) {
+ act(f)?;
+ }
+ }
+ Ok(())
+ })
+}
+
+fn zero(path: &Path) -> anyhow::Result<()> {
+ Ok(std::fs::write(path, b"")?)
+}
+
+fn half(path: &Path) -> anyhow::Result<()> {
+ let bytes = std::fs::read(path)?;
+ Ok(std::fs::write(path, &bytes[..bytes.len() / 2])?)
+}
+
+fn head(path: &Path) -> anyhow::Result<()> {
+ let bytes = std::fs::read(path)?;
+ Ok(std::fs::write(path, &bytes[..bytes.len().min(13)])?)
+}
+
+fn garbage(path: &Path) -> anyhow::Result<()> {
+ let bytes: Vec<u8> = (0..4000).map(|i| (i * 37 % 256) as u8).collect();
+ Ok(std::fs::write(path, bytes)?)
+}
+
+fn smash(path: &Path) -> anyhow::Result<()> {
+ let mut bytes = std::fs::read(path)?;
+ let start = bytes.len() / 4;
+ for (i, b) in bytes.iter_mut().skip(start).take(512).enumerate() {
+ *b = (i as u8).wrapping_mul(13) ^ 0xa5;
+ }
+ Ok(std::fs::write(path, bytes)?)
+}
+
+fn remove(path: &Path) -> anyhow::Result<()> {
+ Ok(std::fs::remove_file(path)?)
+}
+
+fn big_box(path: &Path) -> anyhow::Result<()> {
+ let mut bytes = std::fs::read(path)?;
+ if bytes.len() >= 8 {
+ bytes[0..4].copy_from_slice(&u32::MAX.to_be_bytes());
+ }
+ Ok(std::fs::write(path, bytes)?)
+}
+
+fn zero_box(path: &Path) -> anyhow::Result<()> {
+ let mut bytes = std::fs::read(path)?;
+ if bytes.len() >= 8 {
+ bytes[0..4].copy_from_slice(&0u32.to_be_bytes());
+ }
+ Ok(std::fs::write(path, bytes)?)
+}
+
+fn rename_tmp(path: &Path) -> anyhow::Result<()> {
+ let mut tmp = path.as_os_str().to_owned();
+ tmp.push(".tmp");
+ Ok(std::fs::rename(path, tmp)?)
+}
+
+fn damages(display: &'static str, audio: &'static str) -> Vec<(&'static str, Damage)> {
+ let first = |_: usize| vec![0];
+ let last = |n: usize| vec![n.saturating_sub(1)];
+ let all = |n: usize| (0..n).collect();
+ let middle = |n: usize| vec![n / 2];
+ vec![
+ ("control", Arc::new(|_: &Path| Ok(())) as Damage),
+ ("init-missing", on_each(display, "init.mp4", first, remove)),
+ ("init-zero", on_each(display, "init.mp4", first, zero)),
+ ("init-truncated", on_each(display, "init.mp4", first, half)),
+ ("init-garbage", on_each(display, "init.mp4", first, garbage)),
+ (
+ "init-box-size-huge",
+ on_each(display, "init.mp4", first, big_box),
+ ),
+ (
+ "init-box-size-zero",
+ on_each(display, "init.mp4", first, zero_box),
+ ),
+ ("first-fragment-zero", on_each(display, ".m4s", first, zero)),
+ (
+ "last-fragment-truncated",
+ on_each(display, ".m4s", last, half),
+ ),
+ (
+ "middle-fragment-missing",
+ on_each(display, ".m4s", middle, remove),
+ ),
+ (
+ "middle-fragment-garbage",
+ on_each(display, ".m4s", middle, garbage),
+ ),
+ (
+ "middle-fragment-smashed",
+ on_each(display, ".m4s", middle, smash),
+ ),
+ (
+ "fragment-box-size-huge",
+ on_each(display, ".m4s", middle, big_box),
+ ),
+ (
+ "fragment-box-size-zero",
+ on_each(display, ".m4s", middle, zero_box),
+ ),
+ ("fragment-header-only", on_each(display, ".m4s", all, head)),
+ (
+ "all-fragments-missing",
+ on_each(display, ".m4s", all, remove),
+ ),
+ (
+ "last-fragment-pending",
+ on_each(display, ".m4s", last, rename_tmp),
+ ),
+ (
+ "fragments-renumbered",
+ Arc::new(move |root: &Path| {
+ let files = files_matching(&root.join(display), ".m4s");
+ for (i, f) in files.iter().enumerate().rev() {
+ let name = f.file_name().and_then(|n| n.to_str()).unwrap_or_default();
+ let renamed = name.replace(&format!("{i:03}"), &format!("{:03}", 999 - i));
+ std::fs::rename(f, f.with_file_name(renamed))?;
+ }
+ Ok(())
+ }),
+ ),
+ (
+ "manifest-garbage",
+ Arc::new(move |root: &Path| {
+ for f in files_matching(&root.join(display), ".json") {
+ std::fs::write(
+ f,
+ b"{\"segments\": [{\"index\": -1, \"duration\": \"x\"}, 7]",
+ )?;
+ }
+ Ok(())
+ }),
+ ),
+ (
+ "manifest-hostile-values",
+ Arc::new(move |root: &Path| {
+ for f in files_matching(&root.join(display), ".json") {
+ let mut v: serde_json::Value = serde_json::from_slice(&std::fs::read(&f)?)
+ .unwrap_or(serde_json::Value::Null);
+ fn poison(v: &mut serde_json::Value) {
+ match v {
+ serde_json::Value::Number(_) => *v = json!(1e300),
+ serde_json::Value::Array(a) => a.iter_mut().for_each(poison),
+ serde_json::Value::Object(o) => o.values_mut().for_each(poison),
+ _ => {}
+ }
+ }
+ poison(&mut v);
+ std::fs::write(f, serde_json::to_vec(&v)?)?;
+ }
+ Ok(())
+ }),
+ ),
+ (
+ "audio-init-missing",
+ on_each(audio, "init.mp4", first, remove),
+ ),
+ ("audio-fragments-zero", on_each(audio, ".m4s", all, zero)),
+ (
+ "audio-file-garbage",
+ Arc::new(move |root: &Path| {
+ let path = root.join(audio);
+ if path.is_file() {
+ garbage(&path)
+ } else {
+ for f in files_matching(&path, ".m4s") {
+ garbage(&f)?;
+ }
+ Ok(())
+ }
+ }),
+ ),
+ (
+ "audio-missing",
+ Arc::new(move |root: &Path| {
+ let path = root.join(audio);
+ if path.is_dir() {
+ std::fs::remove_dir_all(&path)?;
+ } else {
+ std::fs::remove_file(&path)?;
+ }
+ Ok(())
+ }),
+ ),
+ ]
+}
+
+fn judge_mp4(path: &Path) -> Verdict {
+ super::media_check::judge_output(path, true)
+}
+
+fn recover(dir: &Path) -> anyhow::Result<Verdict> {
+ let Some(incomplete) = RecoveryManager::inspect_recording(dir) else {
+ return Ok(Verdict::rejected("nothing recoverable"));
+ };
+ match RecoveryManager::recover(&incomplete) {
+ Err(e) => Ok(Verdict::rejected(format!("recover: {e}"))),
+ Ok(_) => {
+ let meta = match cap_project::RecordingMeta::load_for_project(dir) {
+ Ok(meta) => meta,
+ Err(e) => {
+ return Ok(Verdict::Wrong(format!(
+ "recovered but the meta is unreadable: {e}"
+ )));
+ }
+ };
+ let display = match meta.studio_meta() {
+ Some(cap_project::StudioRecordingMeta::MultipleSegments { inner }) => {
+ inner.segments.first().map(|s| s.display.path.to_path(dir))
+ }
+ Some(cap_project::StudioRecordingMeta::SingleSegment { segment }) => {
+ Some(segment.display.path.to_path(dir))
+ }
+ None => None,
+ };
+ Ok(match display {
+ Some(path) => judge_mp4(&path),
+ None => Verdict::Wrong("recovered with no display track".into()),
+ })
+ }
+ }
+}
+
+fn probes_for(out: &mut Vec<Probe>) {
+ let studio_display = "studio/content/segments/segment-0/display";
+ let studio_audio = "studio/content/segments/segment-0/audio-input.m4a";
+ for (name, damage) in damages(studio_display, studio_audio) {
+ // The Studio microphone track is one fragmented file, not a
+ // directory of fragments.
+ if matches!(name, "audio-init-missing" | "audio-fragments-zero") {
+ continue;
+ }
+ let d = damage.clone();
+ out.push(Probe::new(
+ format!("chaos/fragments/recover/{name}"),
+ 90,
+ move |ctx: &Ctx| {
+ let root = ctx.path("fx");
+ copy_dir(&fixture()?, &root)?;
+ d(&root).context("damage")?;
+ recover(&root.join("studio"))
+ },
+ ));
+ if name.starts_with("audio") {
+ continue;
+ }
+ out.push(Probe::new(
+ format!("chaos/fragments/finalize-display/{name}"),
+ 60,
+ move |ctx: &Ctx| {
+ let root = ctx.path("fx");
+ copy_dir(&fixture()?, &root)?;
+ damage(&root).context("damage")?;
+ let out = ctx.path("display.mp4");
+ match RecoveryManager::finalize_to_progressive_mp4(&root.join(studio_display), &out)
+ {
+ Err(e) => Ok(Verdict::rejected(format!("finalize: {e}"))),
+ Ok(_) => Ok(judge_mp4(&out)),
+ }
+ },
+ ));
+ }
+ for (name, damage) in damages("instant/content/display", "instant/content/audio") {
+ out.push(Probe::new(
+ format!("chaos/fragments/instant-finalize/{name}"),
+ 60,
+ move |ctx: &Ctx| {
+ let root = ctx.path("fx");
+ copy_dir(&fixture()?, &root)?;
+ damage(&root).context("damage")?;
+ let content = root.join("instant/content");
+ let out = content.join("output.mp4");
+ match RecoveryManager::finalize_instant_output(
+ &content.join("display"),
+ &content.join("audio"),
+ &out,
+ ) {
+ Err(e) => Ok(Verdict::rejected(format!("finalize: {e}"))),
+ Ok(_) => Ok(judge_mp4(&out)),
+ }
+ },
+ ));
+ }
+}
+
+pub fn probes() -> Vec<Probe> {
+ let mut out = Vec::new();
+ probes_for(&mut out);
+ out
+}
+
+/// Byte-level fuzzing of every fragment file, then recovery and Instant
+/// finalization.
+pub fn fuzz_probes() -> Vec<Probe> {
+ fn mutate_tree(ctx: &Ctx, dir: &Path, salt: u64) -> anyhow::Result<usize> {
+ let mut files = Vec::new();
+ let mut stack = vec![dir.to_path_buf()];
+ while let Some(d) = stack.pop() {
+ for entry in std::fs::read_dir(&d)? {
+ let path = entry?.path();
+ if path.is_dir() {
+ stack.push(path);
+ } else if path
+ .extension()
+ .and_then(|e| e.to_str())
+ .is_some_and(|e| matches!(e, "mp4" | "m4s" | "m4a"))
+ {
+ files.push(path);
+ }
+ }
+ }
+ files.sort();
+ anyhow::ensure!(!files.is_empty(), "no fragments under {}", dir.display());
+ let mut rng = cap_verify::fixture::Rng::new(ctx.seed ^ salt);
+ let mutations = super::fuzz::byte_mutations(ctx.seed, salt);
+ for (i, m) in mutations.iter().enumerate() {
+ let file = &files[rng.range(0, files.len() as u64 - 1) as usize];
+ if ctx.keeps(i) {
+ let mut bytes = std::fs::read(file)?;
+ m.apply(&mut bytes);
+ std::fs::write(file, bytes)?;
+ }
+ }
+ Ok(mutations.len())
+ }
+ fn tagged(v: Verdict, n: usize) -> Verdict {
+ match v {
+ Verdict::Ok(t) => Verdict::Ok(format!("mutations={n}; {t}")),
+ Verdict::Rejected(t) => Verdict::Rejected(format!("mutations={n}; {t}")),
+ Verdict::Wrong(t) => Verdict::Wrong(format!("mutations={n}; {t}")),
+ }
+ }
+ vec![
+ Probe::new("chaos/fuzz/fragments/recover", 90, |ctx: &Ctx| {
+ let root = ctx.path("fx");
+ copy_dir(&fixture()?, &root)?;
+ let studio = root.join("studio");
+ let n = mutate_tree(ctx, &studio.join("content"), 11)?;
+ Ok(tagged(recover(&studio)?, n))
+ })
+ .seeded(10, 120),
+ Probe::new("chaos/fuzz/fragments/instant-finalize", 60, |ctx: &Ctx| {
+ let root = ctx.path("fx");
+ copy_dir(&fixture()?, &root)?;
+ let content = root.join("instant/content");
+ let n = mutate_tree(ctx, &content, 12)?;
+ let out = content.join("output.mp4");
+ let v = match RecoveryManager::finalize_instant_output(
+ &content.join("display"),
+ &content.join("audio"),
+ &out,
+ ) {
+ Err(e) => Verdict::rejected(format!("finalize: {e}")),
+ Ok(_) => super::media_check::judge_output(&out, false),
+ };
+ Ok(tagged(v, n))
+ })
+ .seeded(10, 120),
+ ]
+}
diff --git a/crates/verify/tests/chaos_support/fuzz.rs b/crates/verify/tests/chaos_support/fuzz.rs
new file mode 100644
index 000000000..973df24e9
--- /dev/null
+++ b/crates/verify/tests/chaos_support/fuzz.rs
@@ -0,0 +1,478 @@
+//! Seeded, reproducible fuzzing of the engines' file inputs. Each seed
+//! derives a list of mutations of a valid input (structure-aware for JSON,
+//! box-aware for MP4 and fragments); the harness runs a batch of seeds per
+//! child process, isolates crashes and hangs per seed, and shrinks a failing
+//! seed by dropping mutations while it still fails the same way.
+
+use std::{path::Path, sync::Arc};
+
+use anyhow::Context;
+use cap_verify::fixture::Rng;
+use serde_json::{Value, json};
+
+use super::{
+ harness::{Ctx, Probe, Verdict},
+ project::{self, SEGMENT},
+};
+
+fn hostile_scalar(rng: &mut Rng) -> Value {
+ match rng.range(0, 21) {
+ 0 => json!(0),
+ 1 => json!(-1),
+ 2 => json!(1),
+ 3 => json!(0.5),
+ 4 => json!(1e300),
+ 5 => json!(-1e300),
+ 6 => json!(1e-300),
+ 7 => json!(4_294_967_295u64),
+ 8 => json!(4_294_967_296u64),
+ 9 => json!(u64::MAX),
+ 10 => json!(i64::MIN),
+ 11 => json!(2_147_483_648u64),
+ 12 => json!(""),
+ 13 => json!("\u{0}\u{202E}\u{1F4A5}"),
+ 14 => json!("x".repeat(rng.range(1, 70_000) as usize)),
+ 15 => json!(true),
+ 16 => Value::Null,
+ 17 => json!([]),
+ 18 => json!({}),
+ 19 => json!(-0.0),
+ 20 => json!(65_536),
+ _ => json!(3.0e38),
+ }
+}
+
+/// Every path to a node of `value`, as a list of keys and indices.
+fn paths(value: &Value, prefix: &mut Vec<Value>, out: &mut Vec<Vec<Value>>) {
+ out.push(prefix.clone());
+ match value {
+ Value::Object(map) => {
+ for (k, v) in map {
+ prefix.push(json!(k));
+ paths(v, prefix, out);
+ prefix.pop();
+ }
+ }
+ Value::Array(items) => {
+ for (i, v) in items.iter().enumerate() {
+ prefix.push(json!(i));
+ paths(v, prefix, out);
+ prefix.pop();
+ }
+ }
+ _ => {}
+ }
+}
+
+fn at<'a>(value: &'a mut Value, path: &[Value]) -> Option<&'a mut Value> {
+ let mut cur = value;
+ for key in path {
+ cur = match key {
+ Value::String(k) => cur.get_mut(k.as_str())?,
+ Value::Number(i) => cur.get_mut(i.as_u64()? as usize)?,
+ _ => return None,
+ };
+ }
+ Some(cur)
+}
+
+/// One JSON mutation, fully determined by its own seed.
+#[derive(Clone, Copy, Debug)]
+pub struct JsonMutation(u64);
+
+impl JsonMutation {
+ pub fn apply(&self, root: &mut Value) {
+ let mut rng = Rng::new(self.0);
+ let mut all = Vec::new();
+ paths(root, &mut Vec::new(), &mut all);
+ // Bias towards leaves and numbers, where hostile values matter.
+ let candidates: Vec<&Vec<Value>> = all.iter().filter(|p| !p.is_empty()).collect();
+ if candidates.is_empty() {
+ return;
+ }
+ let path = candidates[rng.range(0, candidates.len() as u64 - 1) as usize].clone();
+ let op = rng.range(0, 11);
+ let (parent_path, key) = path.split_at(path.len() - 1);
+ let key = key[0].clone();
+ let Some(target) = at(root, &path) else {
+ return;
+ };
+ // Most mutations keep the node's type, so they reach the engines
+ // instead of stopping at deserialization.
+ if rng.range(0, 9) < 6 {
+ match target {
+ Value::Number(_) => {
+ *target = match rng.range(0, 13) {
+ 0 => json!(0),
+ 1 => json!(-1),
+ 2 => json!(1e300),
+ 3 => json!(-1e300),
+ 4 => json!(1e-300),
+ 5 => json!(u32::MAX),
+ 6 => json!(u64::MAX),
+ 7 => json!(-0.0),
+ 8 => json!(i32::MIN),
+ 9 => json!(3.0e38),
+ 10 => json!(-3.0e38),
+ 11 => json!(-target.as_f64().unwrap_or(1.0)),
+ _ => json!(target.as_f64().unwrap_or(1.0) * 1e6),
+ };
+ return;
+ }
+ Value::String(_) => {
+ *target = match rng.range(0, 4) {
+ 0 => json!(""),
+ 1 => json!("\u{0}\u{202E}\u{1F4A5}"),
+ 2 => json!("\u{1F4A5}".repeat(rng.range(1, 20_000) as usize)),
+ _ => json!("../../../../../../dev/zero"),
+ };
+ return;
+ }
+ Value::Bool(b) => {
+ *b = !*b;
+ return;
+ }
+ _ => {}
+ }
+ }
+ match op {
+ 0..=3 => *target = hostile_scalar(&mut rng),
+ 4 => {
+ if let Some(n) = target.as_f64() {
+ *target = json!(match rng.range(0, 4) {
+ 0 => -n,
+ 1 => n * 1e6,
+ 2 => n + 1e9,
+ 3 => n - 1e9,
+ _ => n * 1e-9,
+ });
+ } else {
+ *target = hostile_scalar(&mut rng);
+ }
+ }
+ 5 => {
+ if let Value::Array(items) = target {
+ items.reverse();
+ } else {
+ *target = json!([target.clone()]);
+ }
+ }
+ 6 => {
+ if let Value::Array(items) = target
+ && let Some(first) = items.first().cloned()
+ {
+ let copies = rng.range(1, 50) as usize;
+ items.extend(std::iter::repeat_n(first, copies));
+ }
+ }
+ 7 => {
+ if let Value::Array(items) = target {
+ items.clear();
+ } else if let Value::Object(map) = target {
+ map.clear();
+ }
+ }
+ 8 => {
+ let Some(parent) = at(root, parent_path) else {
+ return;
+ };
+ match (parent, &key) {
+ (Value::Object(map), Value::String(k)) => {
+ map.remove(k);
+ }
+ (Value::Array(items), Value::Number(i)) => {
+ let i = i.as_u64().unwrap_or(0) as usize;
+ if i < items.len() {
+ items.remove(i);
+ }
+ }
+ _ => {}
+ }
+ }
+ 9 => {
+ let mut nested = hostile_scalar(&mut rng);
+ for _ in 0..rng.range(1, 200) {
+ nested = json!([nested]);
+ }
+ *target = nested;
+ }
+ _ => {
+ let Some(parent) = at(root, parent_path) else {
+ return;
+ };
+ if let Value::Array(items) = parent
+ && items.len() > 1
+ {
+ let a = rng.range(0, items.len() as u64 - 1) as usize;
+ let b = rng.range(0, items.len() as u64 - 1) as usize;
+ items.swap(a, b);
+ }
+ }
+ }
+ }
+}
+
+pub fn json_mutations(seed: u64, salt: u64) -> Vec<JsonMutation> {
+ let mut rng = Rng::new(seed.wrapping_mul(0x9E37_79B9).wrapping_add(salt));
+ let n = rng.range(1, 12);
+ (0..n).map(|_| JsonMutation(rng.next_u64())).collect()
+}
+
+/// One byte-level mutation of a media file.
+#[derive(Clone, Copy, Debug)]
+pub struct ByteMutation(u64);
+
+/// Offsets of plausible ISO BMFF box headers (size + four ASCII letters).
+fn boxes(bytes: &[u8]) -> Vec<usize> {
+ (0..bytes.len().saturating_sub(8))
+ .filter(|&i| bytes[i + 4..i + 8].iter().all(|b| b.is_ascii_lowercase()))
+ .collect()
+}
+
+impl ByteMutation {
+ pub fn apply(&self, bytes: &mut Vec<u8>) {
+ if bytes.is_empty() {
+ return;
+ }
+ let mut rng = Rng::new(self.0);
+ let len = bytes.len() as u64;
+ match rng.range(0, 8) {
+ 0 => {
+ for _ in 0..rng.range(1, 16) {
+ let i = rng.range(0, len - 1) as usize;
+ bytes[i] ^= 1 << rng.range(0, 7);
+ }
+ }
+ 1 => {
+ let i = rng.range(0, len - 1) as usize;
+ let n = rng.range(1, 256) as usize;
+ for b in bytes.iter_mut().skip(i).take(n) {
+ *b = rng.next_u64() as u8;
+ }
+ }
+ 2 => {
+ let keep = rng.range(0, len - 1) as usize;
+ bytes.truncate(keep);
+ }
+ 3 => {
+ let i = rng.range(0, len - 1) as usize;
+ let n = rng.range(1, 64) as usize;
+ let insert: Vec<u8> = (0..n).map(|_| rng.next_u64() as u8).collect();
+ bytes.splice(i..i, insert);
+ }
+ 4 => {
+ let i = rng.range(0, len - 1) as usize;
+ let n = (rng.range(1, 4096) as usize).min(bytes.len() - i);
+ let chunk = bytes[i..i + n].to_vec();
+ bytes.splice(i..i, chunk);
+ }
+ 5 => {
+ let i = rng.range(0, len - 1) as usize;
+ let n = rng.range(1, 2048) as usize;
+ for b in bytes.iter_mut().skip(i).take(n) {
+ *b = 0;
+ }
+ }
+ _ => {
+ let headers = boxes(bytes);
+ if headers.is_empty() {
+ return;
+ }
+ let at = headers[rng.range(0, headers.len() as u64 - 1) as usize];
+ let size: u32 = match rng.range(0, 6) {
+ 0 => 0,
+ 1 => 1,
+ 2 => 7,
+ 3 => 8,
+ 4 => u32::MAX,
+ 5 => len as u32 * 2,
+ _ => rng.next_u64() as u32,
+ };
+ bytes[at..at + 4].copy_from_slice(&size.to_be_bytes());
+ }
+ }
+ }
+}
+
+pub fn byte_mutations(seed: u64, salt: u64) -> Vec<ByteMutation> {
+ let mut rng = Rng::new(seed.wrapping_mul(0xD1B5_4A32).wrapping_add(salt));
+ let n = rng.range(1, 4);
+ (0..n).map(|_| ByteMutation(rng.next_u64())).collect()
+}
+
+fn mutate_json_file(ctx: &Ctx, path: &Path, salt: u64) -> anyhow::Result<usize> {
+ let mut value: Value = serde_json::from_slice(&std::fs::read(path)?)?;
+ let mutations = json_mutations(ctx.seed, salt);
+ for (i, m) in mutations.iter().enumerate() {
+ if ctx.keeps(i) {
+ m.apply(&mut value);
+ }
+ }
+ std::fs::write(path, serde_json::to_vec(&value)?)?;
+ Ok(mutations.len())
+}
+
+fn mutate_bytes_file(ctx: &Ctx, path: &Path, salt: u64) -> anyhow::Result<usize> {
+ let mut bytes = std::fs::read(path)?;
+ let mutations = byte_mutations(ctx.seed, salt);
+ for (i, m) in mutations.iter().enumerate() {
+ if ctx.keeps(i) {
+ m.apply(&mut bytes);
+ }
+ }
+ std::fs::write(path, bytes)?;
+ Ok(mutations.len())
+}
+
+fn tag(verdict: Verdict, mutations: usize) -> Verdict {
+ match verdict {
+ Verdict::Ok(t) => Verdict::Ok(format!("mutations={mutations}; {t}")),
+ Verdict::Rejected(t) => Verdict::Rejected(format!("mutations={mutations}; {t}")),
+ Verdict::Wrong(t) => Verdict::Wrong(format!("mutations={mutations}; {t}")),
+ }
+}
+
+type Target = Arc<dyn Fn(&Ctx) -> anyhow::Result<Verdict> + Send + Sync>;
+
+fn target(f: impl Fn(&Ctx) -> anyhow::Result<Verdict> + Send + Sync + 'static) -> Target {
+ Arc::new(f)
+}
+
+fn small_export(ctx: &Ctx, dir: &Path) -> anyhow::Result<Verdict> {
+ ctx.block_on(project::export(
+ dir,
+ &ctx.path("out.mp4"),
+ project::Format::Mp4 { bpp: None },
+ 5,
+ (160, 90),
+ false,
+ ))
+}
+
+pub fn probes() -> Vec<Probe> {
+ let targets: Vec<(&'static str, u64, u64, u64, Target)> = vec![
+ (
+ "project-config/open",
+ 80,
+ 800,
+ 30,
+ target(|ctx| {
+ let dir = project::prepare(ctx).context("prepare")?;
+ let n = mutate_json_file(ctx, &dir.join("project-config.json"), 1)?;
+ Ok(tag(project::open(&dir)?, n))
+ }),
+ ),
+ (
+ "project-config/export",
+ 10,
+ 60,
+ 90,
+ target(|ctx| {
+ let dir = project::prepare(ctx).context("prepare")?;
+ let n = mutate_json_file(ctx, &dir.join("project-config.json"), 2)?;
+ Ok(tag(small_export(ctx, &dir)?, n))
+ }),
+ ),
+ (
+ "project-config/editor",
+ 0,
+ 20,
+ 90,
+ target(|ctx| {
+ let dir = project::prepare(ctx).context("prepare")?;
+ let n = mutate_json_file(ctx, &dir.join("project-config.json"), 3)?;
+ Ok(tag(ctx.block_on(project::editor(&dir, false))?, n))
+ }),
+ ),
+ (
+ "recording-meta/open",
+ 60,
+ 600,
+ 30,
+ target(|ctx| {
+ let dir = project::prepare(ctx).context("prepare")?;
+ let n = mutate_json_file(ctx, &dir.join("recording-meta.json"), 4)?;
+ Ok(tag(project::open(&dir)?, n))
+ }),
+ ),
+ (
+ "recording-meta/export",
+ 8,
+ 50,
+ 90,
+ target(|ctx| {
+ let dir = project::prepare(ctx).context("prepare")?;
+ let n = mutate_json_file(ctx, &dir.join("recording-meta.json"), 5)?;
+ Ok(tag(small_export(ctx, &dir)?, n))
+ }),
+ ),
+ (
+ "cursor/export",
+ 10,
+ 60,
+ 90,
+ target(|ctx| {
+ let dir = project::prepare(ctx).context("prepare")?;
+ let n = mutate_json_file(ctx, &dir.join(SEGMENT).join("cursor.json"), 6)?;
+ Ok(tag(small_export(ctx, &dir)?, n))
+ }),
+ ),
+ (
+ "display-mp4/export",
+ 10,
+ 60,
+ 90,
+ target(|ctx| {
+ let dir = project::prepare(ctx).context("prepare")?;
+ let n = mutate_bytes_file(ctx, &dir.join(SEGMENT).join("display.mp4"), 7)?;
+ Ok(tag(small_export(ctx, &dir)?, n))
+ }),
+ ),
+ (
+ "camera-mp4/export",
+ 6,
+ 40,
+ 90,
+ target(|ctx| {
+ let dir = project::prepare(ctx).context("prepare")?;
+ let n = mutate_bytes_file(ctx, &dir.join(SEGMENT).join("camera.mp4"), 8)?;
+ Ok(tag(small_export(ctx, &dir)?, n))
+ }),
+ ),
+ (
+ "mic-ogg/export",
+ 6,
+ 40,
+ 90,
+ target(|ctx| {
+ let dir = project::prepare(ctx).context("prepare")?;
+ let n = mutate_bytes_file(ctx, &dir.join(SEGMENT).join("audio-input.ogg"), 9)?;
+ Ok(tag(small_export(ctx, &dir)?, n))
+ }),
+ ),
+ (
+ "display-mp4/open",
+ 30,
+ 300,
+ 30,
+ target(|ctx| {
+ let dir = project::prepare(ctx).context("prepare")?;
+ let n = mutate_bytes_file(ctx, &dir.join(SEGMENT).join("display.mp4"), 10)?;
+ Ok(tag(project::open(&dir)?, n))
+ }),
+ ),
+ ];
+ let mut out: Vec<Probe> = targets
+ .into_iter()
+ .map(|(name, fast, full, timeout, run)| {
+ let mut probe = Probe::new(format!("chaos/fuzz/{name}"), timeout, move |ctx| run(ctx))
+ .seeded(fast, full);
+ if fast == 0 {
+ probe = probe.full();
+ }
+ probe
+ })
+ .collect();
+ out.extend(super::fragments::fuzz_probes());
+ out
+}
diff --git a/crates/verify/tests/chaos_support/harness.rs b/crates/verify/tests/chaos_support/harness.rs
new file mode 100644
index 000000000..324e22f01
--- /dev/null
+++ b/crates/verify/tests/chaos_support/harness.rs
@@ -0,0 +1,707 @@
+//! Child-process isolation for chaos probes.
+//!
+//! The parent test re-runs its own binary as a child per probe (and per batch
+//! of fuzz seeds), so an abort, a fatal signal, an Objective-C exception or a
+//! hang ends the child, never the harness. The child logs one line per event
+//! to an append-only file that survives any death: `start`, every `panic` on
+//! any thread (through the panic hook), and the probe's `verdict`.
+
+use std::{
+ collections::BTreeMap,
+ io::Write,
+ path::{Path, PathBuf},
+ process::{Command, Stdio},
+ sync::{
+ Arc, Mutex, OnceLock,
+ atomic::{AtomicU64, Ordering},
+ },
+ time::{Duration, Instant},
+};
+
+use cap_verify::report::{self, Case, Tier};
+
+pub const CHILD_ENV: &str = "CAP_VERIFY_CHAOS_CHILD";
+const LOG_ENV: &str = "CAP_VERIFY_CHAOS_LOG";
+const SCRATCH_ENV: &str = "CAP_VERIFY_CHAOS_SCRATCH";
+const SEEDS_ENV: &str = "CAP_VERIFY_CHAOS_SEEDS";
+/// Restricts a fuzz case to one seed (the repro of a seeded failure).
+pub const SEED_ENV: &str = "CAP_VERIFY_CHAOS_SEED";
+/// Comma-separated mutation indices a fuzz seed applies (shrinking).
+pub const KEEP_ENV: &str = "CAP_VERIFY_CHAOS_KEEP";
+
+/// What an engine did with a hostile input.
+pub enum Verdict {
+ /// It succeeded and the output passed the probe's checks.
+ Ok(String),
+ /// It refused the input with an error: the resilient outcome.
+ Rejected(String),
+ /// It reported success but the output is wrong (empty, unreadable, ...).
+ Wrong(String),
+}
+
+impl Verdict {
+ pub fn rejected(error: impl std::fmt::Display) -> Self {
+ Verdict::Rejected(one_line(&error.to_string()))
+ }
+}
+
+pub struct Ctx {
+ pub scratch: PathBuf,
+ pub seed: u64,
+ /// Mutations a fuzz seed keeps (all when `None`).
+ pub keep: Option<Vec<usize>>,
+}
+
+impl Ctx {
+ pub fn path(&self, name: &str) -> PathBuf {
+ self.scratch.join(name)
+ }
+
+ /// Runs `future` on a fresh multi-threaded runtime like the app's tokio
+ /// pool. Leftover tasks are abandoned after a short grace period so a
+ /// stuck background task shows up as the probe's own hang only when the
+ /// probe awaits it.
+ pub fn block_on<F: std::future::Future>(&self, future: F) -> F::Output {
+ let rt = crate::common::runtime();
+ let out = rt.block_on(future);
+ rt.shutdown_timeout(Duration::from_secs(2));
+ out
+ }
+
+ pub fn keeps(&self, index: usize) -> bool {
+ self.keep.as_ref().is_none_or(|keep| keep.contains(&index))
+ }
+}
+
+pub type ProbeFn = Arc<dyn Fn(&Ctx) -> anyhow::Result<Verdict> + Send + Sync>;
+
+#[derive(Clone)]
+pub struct Probe {
+ pub id: String,
+ pub full_only: bool,
+ pub timeout: Duration,
+ /// Fuzz targets: seeds per tier (fast, full). `None` runs once.
+ pub seeds: Option<(u64, u64)>,
+ pub run: ProbeFn,
+}
+
+impl Probe {
+ pub fn new(
+ id: impl Into<String>,
+ timeout_secs: u64,
+ run: impl Fn(&Ctx) -> anyhow::Result<Verdict> + Send + Sync + 'static,
+ ) -> Self {
+ Self {
+ id: id.into(),
+ full_only: false,
+ timeout: Duration::from_secs(timeout_secs),
+ seeds: None,
+ run: Arc::new(run),
+ }
+ }
+
+ pub fn full(mut self) -> Self {
+ self.full_only = true;
+ self
+ }
+
+ pub fn seeded(mut self, fast: u64, full: u64) -> Self {
+ self.seeds = Some((fast, full));
+ self
+ }
+}
+
+pub fn one_line(text: &str) -> String {
+ let mut out: String = text
+ .chars()
+ .map(|c| {
+ if c == '\n' || c == '\t' || c == '\r' {
+ ' '
+ } else {
+ c
+ }
+ })
+ .collect();
+ if out.len() > 600 {
+ let mut cut = 600;
+ while !out.is_char_boundary(cut) {
+ cut -= 1;
+ }
+ out.truncate(cut);
+ out.push_str("...");
+ }
+ out
+}
+
+static LOG: OnceLock<Mutex<std::fs::File>> = OnceLock::new();
+static CURRENT_SEED: AtomicU64 = AtomicU64::new(0);
+
+fn log_line(fields: &[&str]) {
+ if let Some(file) = LOG.get() {
+ let line = format!("{}\n", fields.join("\t"));
+ let mut file = file.lock().unwrap_or_else(|e| e.into_inner());
+ let _ = file.write_all(line.as_bytes());
+ let _ = file.flush();
+ }
+}
+
+fn install_panic_hook() {
+ let default = std::panic::take_hook();
+ std::panic::set_hook(Box::new(move |info| {
+ let thread = std::thread::current();
+ let location = info
+ .location()
+ .map(|l| format!("{}:{}", l.file(), l.line()))
+ .unwrap_or_default();
+ let message = info
+ .payload()
+ .downcast_ref::<&str>()
+ .map(|s| s.to_string())
+ .or_else(|| info.payload().downcast_ref::<String>().cloned())
+ .unwrap_or_default();
+ log_line(&[
+ "panic",
+ &CURRENT_SEED.load(Ordering::SeqCst).to_string(),
+ thread.name().unwrap_or("unnamed"),
+ &location,
+ &one_line(&message),
+ ]);
+ default(info);
+ }));
+}
+
+/// The child side: runs only when `CAP_VERIFY_CHAOS_CHILD` names a probe.
+pub fn child_main(all: fn() -> Vec<Probe>) {
+ let Ok(id) = std::env::var(CHILD_ENV) else {
+ return;
+ };
+ crate::common::init();
+ let log = std::env::var(LOG_ENV).expect("chaos child log path");
+ let file = std::fs::OpenOptions::new()
+ .create(true)
+ .append(true)
+ .open(&log)
+ .expect("chaos child log");
+ let _ = LOG.set(Mutex::new(file));
+ install_panic_hook();
+ let probe = all()
+ .into_iter()
+ .find(|p| p.id == id)
+ .unwrap_or_else(|| panic!("no chaos probe {id}"));
+ let scratch = PathBuf::from(std::env::var(SCRATCH_ENV).expect("chaos scratch"));
+ let (first, end) = std::env::var(SEEDS_ENV)
+ .ok()
+ .and_then(|s| {
+ let (a, b) = s.split_once("..")?;
+ Some((a.parse().ok()?, b.parse().ok()?))
+ })
+ .unwrap_or((0u64, 1u64));
+ let keep = std::env::var(KEEP_ENV).ok().map(|s| {
+ s.split(',')
+ .filter_map(|v| v.trim().parse().ok())
+ .collect::<Vec<usize>>()
+ });
+ for seed in first..end {
+ CURRENT_SEED.store(seed, Ordering::SeqCst);
+ log_line(&["start", &seed.to_string()]);
+ let dir = scratch.join(format!("s{seed}"));
+ let _ = std::fs::remove_dir_all(&dir);
+ std::fs::create_dir_all(&dir).expect("chaos seed scratch");
+ let ctx = Ctx {
+ scratch: dir.clone(),
+ seed,
+ keep: keep.clone(),
+ };
+ let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| (probe.run)(&ctx)));
+ let (kind, text) = match result {
+ Ok(Ok(Verdict::Ok(text))) => ("ok", text),
+ Ok(Ok(Verdict::Rejected(text))) => ("rejected", text),
+ Ok(Ok(Verdict::Wrong(text))) => ("wrong", text),
+ Ok(Err(error)) => ("harness", format!("{error:#}")),
+ Err(_) => ("panicked", String::new()),
+ };
+ log_line(&["verdict", &seed.to_string(), kind, &one_line(&text)]);
+ let _ = std::fs::remove_dir_all(&dir);
+ }
+ log_line(&["end"]);
+ // Leftover engine threads must not keep the child alive (or run
+ // destructors racing a finished probe).
+ std::process::exit(0);
+}
+
+/// One seed's outcome as the parent saw it.
+#[derive(Clone, Debug)]
+pub struct SeedOutcome {
+ pub seed: u64,
+ /// `pass`, `panic`, `abort`, `objc-exception`, `hang`, `wrong-success`
+ /// or `harness`.
+ pub class: &'static str,
+ pub detail: String,
+}
+
+#[derive(Default)]
+struct ChildLog {
+ started: Vec<u64>,
+ panics: BTreeMap<u64, Vec<String>>,
+ verdicts: BTreeMap<u64, (String, String)>,
+ ended: bool,
+}
+
+fn read_log(path: &Path) -> ChildLog {
+ let mut log = ChildLog::default();
+ let Ok(text) = std::fs::read_to_string(path) else {
+ return log;
+ };
+ for line in text.lines() {
+ let fields: Vec<&str> = line.split('\t').collect();
+ match fields.as_slice() {
+ ["start", seed] => {
+ if let Ok(seed) = seed.parse() {
+ log.started.push(seed);
+ }
+ }
+ ["panic", seed, thread, location, message] => {
+ if let Ok(seed) = seed.parse() {
+ log.panics.entry(seed).or_default().push(format!(
+ "thread '{thread}' panicked at {location}: {message}"
+ ));
+ }
+ }
+ ["verdict", seed, kind, text] => {
+ if let Ok(seed) = seed.parse() {
+ log.verdicts
+ .insert(seed, (kind.to_string(), text.to_string()));
+ }
+ }
+ ["end"] => log.ended = true,
+ _ => {}
+ }
+ }
+ log
+}
+
+fn stderr_tail(path: &Path) -> String {
+ let text = std::fs::read(path).unwrap_or_default();
+ let text = String::from_utf8_lossy(&text);
+ let lines: Vec<&str> = text.lines().collect();
+ let tail = &lines[lines.len().saturating_sub(12)..];
+ one_line(&tail.join(" | "))
+}
+
+fn objc_exception(path: &Path) -> Option<String> {
+ let text = std::fs::read(path).unwrap_or_default();
+ let text = String::from_utf8_lossy(&text);
+ text.lines()
+ .find(|l| {
+ l.contains("uncaught exception")
+ || l.contains("NSException")
+ || l.contains("NSInvalidArgumentException")
+ || l.contains("NSRangeException")
+ || l.contains("NSInternalInconsistencyException")
+ })
+ .map(one_line)
+}
+
+#[cfg(unix)]
+fn signal_of(status: &std::process::ExitStatus) -> Option<i32> {
+ use std::os::unix::process::ExitStatusExt;
+ status.signal()
+}
+
+#[cfg(not(unix))]
+fn signal_of(_status: &std::process::ExitStatus) -> Option<i32> {
+ None
+}
+
+fn signal_name(signal: i32) -> String {
+ match signal {
+ 4 => "SIGILL".into(),
+ 5 => "SIGTRAP".into(),
+ 6 => "SIGABRT".into(),
+ 7 if cfg!(target_os = "linux") => "SIGBUS".into(),
+ 8 => "SIGFPE".into(),
+ 9 => "SIGKILL".into(),
+ 10 if cfg!(target_os = "macos") => "SIGBUS".into(),
+ 11 => "SIGSEGV".into(),
+ other => format!("signal {other}"),
+ }
+}
+
+pub fn scratch_root() -> PathBuf {
+ std::env::var_os("CAP_VERIFY_SCRATCH")
+ .map(PathBuf::from)
+ .unwrap_or_else(std::env::temp_dir)
+ .join("cap-verify-chaos")
+}
+
+fn case_dir(id: &str) -> PathBuf {
+ scratch_root().join(id.replace('/', "_"))
+}
+
+/// Runs seeds `first..end` of `probe` in children, one child per batch: a
+/// child that dies or hangs is replaced, starting after the seed it was on.
+pub fn run_seeds(probe: &Probe, first: u64, end: u64, keep: Option<&str>) -> Vec<SeedOutcome> {
+ let dir = case_dir(&probe.id);
+ let _ = std::fs::remove_dir_all(&dir);
+ let mut outcomes = Vec::new();
+ let mut next = first;
+ let mut attempt = 0;
+ while next < end {
+ attempt += 1;
+ let log = dir.join(format!("log-{attempt}.tsv"));
+ let stderr = dir.join(format!("stderr-{attempt}.txt"));
+ let scratch = dir.join("work");
+ if let Err(e) = std::fs::create_dir_all(&scratch) {
+ outcomes.push(SeedOutcome {
+ seed: next,
+ class: "harness",
+ detail: format!("scratch: {e}"),
+ });
+ break;
+ }
+ let spawned = (|| -> anyhow::Result<std::process::Child> {
+ let mut command = Command::new(std::env::current_exe()?);
+ command
+ .args(["chaos_child", "--exact", "--nocapture", "--test-threads=1"])
+ .env(CHILD_ENV, &probe.id)
+ .env(LOG_ENV, &log)
+ .env(SCRATCH_ENV, &scratch)
+ .env(SEEDS_ENV, format!("{next}..{end}"))
+ .env("RUST_BACKTRACE", "0")
+ .stdout(Stdio::null())
+ .stderr(std::fs::File::create(&stderr)?);
+ if let Some(keep) = keep {
+ command.env(KEEP_ENV, keep);
+ }
+ if let Ok(muxer) = cap_recording::oop_muxer::resolve_muxer_binary() {
+ command.env(cap_recording::oop_muxer::ENV_BIN_PATH, muxer);
+ }
+ Ok(command.spawn()?)
+ })();
+ let mut child = match spawned {
+ Ok(child) => child,
+ Err(e) => {
+ outcomes.push(SeedOutcome {
+ seed: next,
+ class: "harness",
+ detail: format!("spawn: {e:#}"),
+ });
+ break;
+ }
+ };
+ let mut current = next;
+ let mut seed_started = Instant::now();
+ let status = loop {
+ if let Ok(Some(status)) = child.try_wait() {
+ break Some(status);
+ }
+ let log = read_log(&log);
+ let latest = log.started.last().copied().unwrap_or(next);
+ if latest != current {
+ current = latest;
+ seed_started = Instant::now();
+ }
+ let finished = log.verdicts.contains_key(&current);
+ if !finished && seed_started.elapsed() > probe.timeout {
+ let _ = child.kill();
+ let _ = child.wait();
+ break None;
+ }
+ std::thread::sleep(Duration::from_millis(20));
+ };
+ let parsed = read_log(&log);
+ for (&seed, (kind, text)) in &parsed.verdicts {
+ let panics = parsed.panics.get(&seed).cloned().unwrap_or_default();
+ outcomes.push(if !panics.is_empty() {
+ SeedOutcome {
+ seed,
+ class: "panic",
+ detail: panics.join(" || "),
+ }
+ } else {
+ match kind.as_str() {
+ "ok" | "rejected" => SeedOutcome {
+ seed,
+ class: "pass",
+ detail: format!("{kind}: {text}"),
+ },
+ "wrong" => SeedOutcome {
+ seed,
+ class: "wrong-success",
+ detail: text.clone(),
+ },
+ _ => SeedOutcome {
+ seed,
+ class: "harness",
+ detail: format!("{kind}: {text}"),
+ },
+ }
+ });
+ }
+ let unfinished = parsed
+ .started
+ .iter()
+ .copied()
+ .find(|seed| !parsed.verdicts.contains_key(seed));
+ match (status, unfinished) {
+ (None, seed) => {
+ let seed = seed.unwrap_or(current);
+ let mut detail = format!(
+ "no verdict after {:.0} s (killed)",
+ probe.timeout.as_secs_f64()
+ );
+ if let Some(panics) = parsed.panics.get(&seed) {
+ detail.push_str(&format!("; after {}", panics.join(" || ")));
+ }
+ outcomes.push(SeedOutcome {
+ seed,
+ class: "hang",
+ detail,
+ });
+ next = seed + 1;
+ }
+ (Some(status), Some(seed)) => {
+ let tail = stderr_tail(&stderr);
+ let panics = parsed.panics.get(&seed).cloned().unwrap_or_default();
+ let (class, what) = if let Some(line) = objc_exception(&stderr) {
+ ("objc-exception", line)
+ } else if let Some(signal) = signal_of(&status) {
+ ("abort", signal_name(signal))
+ } else if !panics.is_empty() {
+ ("panic", format!("exit {status}"))
+ } else {
+ ("abort", format!("exit {status}"))
+ };
+ let mut detail = what;
+ if !panics.is_empty() {
+ detail.push_str(&format!("; {}", panics.join(" || ")));
+ }
+ detail.push_str(&format!("; stderr: {tail}"));
+ outcomes.push(SeedOutcome {
+ seed,
+ class,
+ detail,
+ });
+ next = seed + 1;
+ }
+ (Some(status), None) => {
+ if !parsed.ended || !status.success() {
+ let last = parsed.verdicts.keys().last().copied();
+ let after = last.map_or(next, |s| s + 1);
+ if after >= end {
+ // Every seed has its verdict; the child died on its
+ // way out (an engine thread outliving the probe).
+ let seed = last.unwrap_or(next);
+ let panics = parsed.panics.get(&seed).cloned().unwrap_or_default();
+ if !status.success() {
+ outcomes.push(SeedOutcome {
+ seed,
+ class: if signal_of(&status).is_some() {
+ "abort"
+ } else {
+ "panic"
+ },
+ detail: format!(
+ "child exited {status} after its last verdict; {}; stderr: {}",
+ panics.join(" || "),
+ stderr_tail(&stderr)
+ ),
+ });
+ }
+ break;
+ }
+ if parsed.started.is_empty() && attempt > 2 {
+ outcomes.push(SeedOutcome {
+ seed: next,
+ class: "harness",
+ detail: format!(
+ "child exited {status} before its first seed; stderr: {}",
+ stderr_tail(&stderr)
+ ),
+ });
+ break;
+ }
+ next = after;
+ } else {
+ break;
+ }
+ }
+ }
+ }
+ if std::env::var_os("CAP_VERIFY_CHAOS_KEEP_SCRATCH").is_none() {
+ let _ = std::fs::remove_dir_all(&dir);
+ }
+ outcomes
+}
+
+/// Removes mutations from a failing seed while it still fails the same way:
+/// one pass, each index dropped once. Returns the indices kept.
+fn shrink(probe: &Probe, seed: u64, class: &str, mutations: usize) -> Option<Vec<usize>> {
+ if mutations <= 1 || mutations > 24 {
+ return None;
+ }
+ let mut kept: Vec<usize> = (0..mutations).collect();
+ let mut index = 0;
+ while index < kept.len() {
+ let mut trial = kept.clone();
+ trial.remove(index);
+ let keep: Vec<String> = trial.iter().map(|i| i.to_string()).collect();
+ let outcome = run_seeds(probe, seed, seed + 1, Some(&keep.join(",")));
+ if outcome.first().is_some_and(|o| o.class == class) {
+ kept = trial;
+ } else {
+ index += 1;
+ }
+ }
+ Some(kept)
+}
+
+/// Mutation count of a fuzz seed, as its probe reports it in the verdict
+/// text (`mutations=N`).
+fn mutation_count(detail: &str) -> Option<usize> {
+ detail
+ .split_whitespace()
+ .find_map(|w| w.strip_prefix("mutations="))
+ .and_then(|n| n.trim_end_matches([';', ',']).parse().ok())
+}
+
+pub fn run_probe(probe: &Probe, case: &mut Case) {
+ let tier = report::tier();
+ let (first, end) = match (probe.seeds, std::env::var(SEED_ENV).ok()) {
+ (Some(_), Some(seed)) => match seed.parse::<u64>() {
+ Ok(seed) => (seed, seed + 1),
+ Err(_) => {
+ case.problem("harness", format!("{SEED_ENV}={seed} is not a seed"));
+ return;
+ }
+ },
+ (Some((fast, full)), None) => (0, if tier == Tier::Full { full } else { fast }),
+ (None, _) => (0, 1),
+ };
+ let keep = std::env::var(KEEP_ENV).ok();
+ let mut outcomes = run_seeds(probe, first, end, keep.as_deref());
+ // A hang must reproduce: a shared machine can stall a child past its
+ // timeout. The rerun's outcome stands, and a hang that does not repeat is
+ // noted.
+ for outcome in &mut outcomes {
+ if outcome.class != "hang" {
+ continue;
+ }
+ let rerun = run_seeds(probe, outcome.seed, outcome.seed + 1, keep.as_deref());
+ if let Some(again) = rerun.into_iter().next()
+ && again.class != "hang"
+ {
+ case.note(format!(
+ "seed {} hung once ({}) and did not hang on its rerun",
+ outcome.seed, outcome.detail
+ ));
+ *outcome = again;
+ }
+ }
+ let mut passed = 0;
+ let mut signatures: BTreeMap<&'static str, usize> = BTreeMap::new();
+ for outcome in &outcomes {
+ if outcome.class == "pass" {
+ passed += 1;
+ if probe.seeds.is_none() {
+ case.note(outcome.detail.clone());
+ }
+ continue;
+ }
+ *signatures.entry(outcome.class).or_default() += 1;
+ let mut text = outcome.detail.clone();
+ if probe.seeds.is_some() {
+ let shrunk = (outcome.class != "hang" && outcome.class != "harness")
+ .then(|| {
+ let probe_outcome = run_seeds(probe, outcome.seed, outcome.seed + 1, Some(""));
+ let count = probe_outcome
+ .first()
+ .and_then(|o| mutation_count(&o.detail));
+ count.and_then(|n| shrink(probe, outcome.seed, outcome.class, n))
+ })
+ .flatten();
+ let keep = shrunk
+ .map(|k| {
+ format!(
+ " {KEEP_ENV}={}",
+ k.iter()
+ .map(|i| i.to_string())
+ .collect::<Vec<_>>()
+ .join(",")
+ )
+ })
+ .unwrap_or_default();
+ text = format!(
+ "seed {} ({SEED_ENV}={}{keep}): {text}",
+ outcome.seed, outcome.seed
+ );
+ }
+ case.problem(outcome.class, text);
+ }
+ if probe.seeds.is_some() {
+ case.note(format!(
+ "seeds {first}..{end}: {passed} clean, failures {signatures:?}"
+ ));
+ }
+}
+
+/// Runs every selected probe whose id starts with `prefix` and settles each
+/// as a case.
+pub fn run_group(all: fn() -> Vec<Probe>, prefix: &str) {
+ if std::env::var(CHILD_ENV).is_ok() {
+ return;
+ }
+ crate::common::init();
+ if std::env::var_os(cap_recording::oop_muxer::ENV_BIN_PATH).is_none()
+ && let Some(bin) = std::env::current_exe()
+ .ok()
+ .and_then(|exe| Some(exe.parent()?.parent()?.join("cap-muxer")))
+ .filter(|p| p.exists())
+ {
+ let _ = cap_recording::oop_muxer::set_muxer_binary_override(bin);
+ }
+ let tier = report::tier();
+ let probes: Vec<Probe> = all()
+ .into_iter()
+ .filter(|p| p.id.starts_with(prefix) && report::selected(&p.id))
+ .filter(|p| !(p.full_only && tier == Tier::Fast))
+ .collect();
+ let jobs = std::env::var("CAP_VERIFY_CHAOS_JOBS")
+ .ok()
+ .and_then(|v| v.parse::<usize>().ok())
+ .unwrap_or_else(|| {
+ std::thread::available_parallelism()
+ .map(|n| (n.get() / 4).clamp(1, 3))
+ .unwrap_or(1)
+ })
+ .max(1);
+ let next = std::sync::atomic::AtomicUsize::new(0);
+ let failures: Mutex<Vec<(usize, String)>> = Mutex::new(Vec::new());
+ std::thread::scope(|scope| {
+ for _ in 0..jobs.min(probes.len().max(1)) {
+ scope.spawn(|| {
+ loop {
+ let index = next.fetch_add(1, Ordering::SeqCst);
+ let Some(probe) = probes.get(index) else {
+ break;
+ };
+ let mut case = Case::new(&probe.id);
+ run_probe(probe, &mut case);
+ for note in &case.notes {
+ eprintln!(" note: {note}");
+ }
+ if let Err(e) = report::settle(case) {
+ failures
+ .lock()
+ .unwrap_or_else(|e| e.into_inner())
+ .push((index, e));
+ }
+ }
+ });
+ }
+ });
+ let mut failures = failures.into_inner().unwrap_or_else(|e| e.into_inner());
+ failures.sort();
+ let failures: Vec<String> = failures.into_iter().map(|(_, e)| e).collect();
+ assert!(failures.is_empty(), "{}", failures.join("\n"));
+}
diff --git a/crates/verify/tests/chaos_support/media_check.rs b/crates/verify/tests/chaos_support/media_check.rs
new file mode 100644
index 000000000..c17682696
--- /dev/null
+++ b/crates/verify/tests/chaos_support/media_check.rs
@@ -0,0 +1,94 @@
+//! What a recording pipeline left on disk, read back with FFmpeg.
+
+use std::path::{Path, PathBuf};
+
+/// Packets FFmpeg reads from `path` (a file, or a fragment directory read as
+/// its init segment followed by its media segments).
+pub fn packets(path: &Path) -> anyhow::Result<usize> {
+ if path.is_dir() {
+ let mut total = 0;
+ let mut any = false;
+ for media in fragment_streams(path)? {
+ any = true;
+ total += file_packets(&media)?;
+ }
+ anyhow::ensure!(any, "no media in {}", path.display());
+ return Ok(total);
+ }
+ file_packets(path)
+}
+
+fn file_packets(path: &Path) -> anyhow::Result<usize> {
+ let mut input = ffmpeg::format::input(path)?;
+ let mut count = 0;
+ for (_, packet) in input.packets() {
+ if packet.size() > 0 {
+ count += 1;
+ }
+ }
+ Ok(count)
+}
+
+/// Concatenates `init.mp4` and the `*.m4s` segments of every fragment
+/// directory under `dir` into one readable file per directory.
+fn fragment_streams(dir: &Path) -> anyhow::Result<Vec<PathBuf>> {
+ let mut out = Vec::new();
+ let mut dirs = vec![dir.to_path_buf()];
+ while let Some(dir) = dirs.pop() {
+ let mut segments = Vec::new();
+ let mut init = None;
+ let mut files = Vec::new();
+ for entry in std::fs::read_dir(&dir)? {
+ let path = entry?.path();
+ if path.is_dir() {
+ dirs.push(path);
+ continue;
+ }
+ let name = path
+ .file_name()
+ .and_then(|n| n.to_str())
+ .unwrap_or_default()
+ .to_string();
+ if name == "init.mp4" {
+ init = Some(path);
+ } else if name.ends_with(".m4s") {
+ segments.push(path);
+ } else if name.ends_with(".mp4") || name.ends_with(".ogg") || name.ends_with(".m4a") {
+ files.push(path);
+ }
+ }
+ segments.sort();
+ if let Some(init) = init {
+ if segments.is_empty() {
+ continue;
+ }
+ let joined = dir.join("chaos-joined.mp4");
+ let mut bytes = std::fs::read(&init)?;
+ for segment in &segments {
+ bytes.extend(std::fs::read(segment)?);
+ }
+ std::fs::write(&joined, bytes)?;
+ out.push(joined);
+ } else {
+ out.extend(files);
+ }
+ }
+ Ok(out)
+}
+
+/// Judges a pipeline that reported success: it must have written readable
+/// media when the input carried any usable frame.
+pub fn judge_output(path: &Path, expect_media: bool) -> super::harness::Verdict {
+ use super::harness::Verdict;
+ match packets(path) {
+ Ok(n) if n > 0 => Verdict::Ok(format!("{n} packets")),
+ Ok(_) if expect_media => {
+ Verdict::Wrong("reported success but the output has no packets".into())
+ }
+ Ok(_) => Verdict::Ok("no packets (no usable input)".into()),
+ Err(e) if expect_media => Verdict::Wrong(format!(
+ "reported success but the output is unreadable: {e:#}"
+ )),
+ Err(e) => Verdict::Ok(format!("no readable output (no usable input): {e:#}")),
+ }
+}
diff --git a/crates/verify/tests/chaos_support/mod.rs b/crates/verify/tests/chaos_support/mod.rs
new file mode 100644
index 000000000..c9a3d26ec
--- /dev/null
+++ b/crates/verify/tests/chaos_support/mod.rs
@@ -0,0 +1,15 @@
+pub mod actors;
+pub mod fragments;
+pub mod fuzz;
+pub mod harness;
+pub mod media_check;
+pub mod project;
+pub mod record;
+
+pub fn all() -> Vec<harness::Probe> {
+ let mut out = record::probes();
+ out.extend(project::probes());
+ out.extend(fragments::probes());
+ out.extend(fuzz::probes());
+ out
+}
diff --git a/crates/verify/tests/chaos_support/project.rs b/crates/verify/tests/chaos_support/project.rs
new file mode 100644
index 000000000..eb4643377
--- /dev/null
+++ b/crates/verify/tests/chaos_support/project.rs
@@ -0,0 +1,1511 @@
+//! File and project chaos for the editor, playback, seek, render and export
+//! engines: a small generated Studio project, copied per probe, with one
+//! hostile change to its media, metadata, configuration or cursor data.
+
+use std::{
+ path::{Path, PathBuf},
+ sync::{
+ Arc,
+ atomic::{AtomicUsize, Ordering},
+ },
+ time::Duration,
+};
+
+use anyhow::Context;
+use cap_project::{RecordingMeta, XY};
+use cap_verify::{
+ audio_code::AudioSource,
+ fixture::{self, AudioTrackSpec, FixtureSpec, SegmentSpec, Timing, VideoTrackSpec},
+};
+use serde_json::{Value, json};
+
+use super::harness::{Ctx, Probe, Verdict};
+
+pub const SECONDS: f64 = 2.0;
+pub const SEGMENT: &str = "content/segments/segment-0";
+
+pub fn spec() -> FixtureSpec {
+ let audio = |source, channels| AudioTrackSpec {
+ source,
+ sample_rate: 48_000,
+ channels,
+ duration: SECONDS,
+ start_time: 0.0,
+ };
+ FixtureSpec {
+ name: "chaos-base".into(),
+ segments: vec![SegmentSpec {
+ display: VideoTrackSpec::new(160, 90, 30, Timing::Constant { duration: SECONDS }),
+ camera: Some(VideoTrackSpec::new(
+ 96,
+ 72,
+ 30,
+ Timing::Constant { duration: SECONDS },
+ )),
+ mic: Some(audio(AudioSource::Mic, 1)),
+ system_audio: Some(audio(AudioSource::System, 2)),
+ }],
+ }
+}
+
+/// The cached base fixture (generated on first use).
+pub fn base() -> anyhow::Result<PathBuf> {
+ fixture::ensure_fixture(&spec(), fixture::write_fixture)
+}
+
+pub fn copy_dir(from: &Path, to: &Path) -> anyhow::Result<()> {
+ std::fs::create_dir_all(to)?;
+ for entry in std::fs::read_dir(from)? {
+ let entry = entry?;
+ let target = to.join(entry.file_name());
+ if entry.file_type()?.is_dir() {
+ copy_dir(&entry.path(), &target)?;
+ } else {
+ std::fs::copy(entry.path(), &target)?;
+ }
+ }
+ Ok(())
+}
+
+pub fn cursor_json() -> Value {
+ let moves: Vec<Value> = (0..60)
+ .map(|i| {
+ json!({
+ "active_modifiers": [],
+ "cursor_id": "0",
+ "time_ms": f64::from(i) * 33.0,
+ "x": 0.2 + f64::from(i) * 0.01,
+ "y": 0.3 + f64::from(i % 7) * 0.05,
+ })
+ })
+ .collect();
+ let clicks: Vec<Value> = (0..4)
+ .map(|i| {
+ json!({
+ "active_modifiers": [],
+ "cursor_num": 0,
+ "cursor_id": "0",
+ "time_ms": 300.0 + f64::from(i) * 400.0,
+ "down": i % 2 == 0,
+ })
+ })
+ .collect();
+ json!({ "clicks": clicks, "moves": moves })
+}
+
+/// A configuration that exercises every timeline track the renderer reads.
+pub fn rich_config(base: &Value) -> Value {
+ let mut config = base.clone();
+ config["timeline"] = json!({
+ "segments": [
+ { "recordingSegment": 0, "timescale": 1.0, "start": 0.0, "end": 0.9 },
+ { "recordingSegment": 0, "timescale": 2.0, "start": 0.9, "end": 1.9 },
+ { "recordingSegment": 0, "timescale": 0.5, "start": 1.2, "end": 1.6 }
+ ],
+ "transitions": [{ "segmentIndex": 1, "type": "cross-fade", "duration": 0.2 }],
+ "zoomSegments": [
+ { "start": 0.1, "end": 0.8, "amount": 1.8, "mode": "auto" },
+ { "start": 1.0, "end": 1.4, "amount": 2.5, "mode": { "manual": { "x": 0.3, "y": 0.6 } } }
+ ],
+ "sceneSegments": [{ "start": 0.5, "end": 1.0, "mode": "splitScreen" }],
+ "maskSegments": [{
+ "start": 0.2, "end": 1.2, "track": 0, "maskType": "sensitive",
+ "center": { "x": 0.5, "y": 0.5 }, "size": { "x": 0.2, "y": 0.2 }
+ }],
+ "textSegments": [{ "start": 0.3, "end": 1.5, "track": 0, "content": "Chaos" }],
+ "captionSegments": [{ "id": "c0", "start": 0.1, "end": 1.0, "text": "hello chaos" }],
+ "keyboardSegments": [{
+ "id": "k0", "start": 0.2, "end": 1.0, "displayText": "ab",
+ "keys": [{ "key": "a", "timeOffset": 100.0 }, { "key": "b", "timeOffset": 400.0 }]
+ }],
+ "camera3dSegments": [{ "start": 0.4, "end": 0.9 }],
+ "styleSegments": [{ "start": 0.6, "end": 1.1, "overrides": { "background": { "padding": 10.0 } } }]
+ });
+ config["captions"] = json!({
+ "segments": [{ "id": "s0", "start": 0.1, "end": 1.0, "text": "hello chaos" }],
+ "settings": { "enabled": true }
+ });
+ config["keyboard"] = json!({ "settings": { "enabled": true } });
+ config["annotations"] = json!([{
+ "id": "a0", "type": "rectangle", "x": 0.1, "y": 0.1, "width": 0.2, "height": 0.2,
+ "strokeColor": "#ff0000", "strokeWidth": 2.0, "fillColor": "#00000000",
+ "opacity": 1.0, "rotation": 0.0, "text": null
+ }]);
+ config
+}
+
+/// A copy of the base fixture with a cursor track and a rich configuration.
+pub fn prepare(ctx: &Ctx) -> anyhow::Result<PathBuf> {
+ let dir = ctx.path("project.cap");
+ copy_dir(&base()?, &dir)?;
+ let _ = std::fs::remove_file(dir.join("verify-truth.json"));
+ std::fs::write(
+ dir.join(SEGMENT).join("cursor.json"),
+ serde_json::to_vec(&cursor_json())?,
+ )?;
+ let mut meta: Value = serde_json::from_slice(&std::fs::read(dir.join("recording-meta.json"))?)?;
+ meta["segments"][0]["cursor"] = json!(format!("{SEGMENT}/cursor.json"));
+ std::fs::write(
+ dir.join("recording-meta.json"),
+ serde_json::to_vec_pretty(&meta)?,
+ )?;
+ let config: Value = serde_json::from_slice(&std::fs::read(dir.join("project-config.json"))?)?;
+ std::fs::write(
+ dir.join("project-config.json"),
+ serde_json::to_vec_pretty(&rich_config(&config))?,
+ )?;
+ Ok(dir)
+}
+
+pub fn edit_json(path: &Path, edit: impl FnOnce(&mut Value)) -> anyhow::Result<()> {
+ let mut value: Value = serde_json::from_slice(&std::fs::read(path)?)?;
+ edit(&mut value);
+ std::fs::write(path, serde_json::to_vec_pretty(&value)?)?;
+ Ok(())
+}
+
+pub type Mutation = Arc<dyn Fn(&Path) -> anyhow::Result<()> + Send + Sync>;
+
+fn m(f: impl Fn(&Path) -> anyhow::Result<()> + Send + Sync + 'static) -> Mutation {
+ Arc::new(f)
+}
+
+fn seg(file: &str) -> String {
+ format!("{SEGMENT}/{file}")
+}
+
+fn zero(rel: String) -> Mutation {
+ m(move |dir| Ok(std::fs::write(dir.join(&rel), b"")?))
+}
+
+fn truncate(rel: String, keep: f64) -> Mutation {
+ m(move |dir| {
+ let path = dir.join(&rel);
+ let bytes = std::fs::read(&path)?;
+ let n = (bytes.len() as f64 * keep) as usize;
+ Ok(std::fs::write(&path, &bytes[..n.min(bytes.len())])?)
+ })
+}
+
+fn truncate_to(rel: String, n: usize) -> Mutation {
+ m(move |dir| {
+ let path = dir.join(&rel);
+ let bytes = std::fs::read(&path)?;
+ Ok(std::fs::write(&path, &bytes[..n.min(bytes.len())])?)
+ })
+}
+
+/// Overwrites `count` bytes from the middle third on with a pattern.
+fn smash(rel: String, count: usize) -> Mutation {
+ m(move |dir| {
+ let path = dir.join(&rel);
+ let mut bytes = std::fs::read(&path)?;
+ let start = bytes.len() / 3;
+ for (i, b) in bytes.iter_mut().skip(start).take(count).enumerate() {
+ *b = (i as u8).wrapping_mul(97) ^ 0x5a;
+ }
+ Ok(std::fs::write(&path, bytes)?)
+ })
+}
+
+/// Flips one byte in every `every` bytes past the first 64.
+fn flip(rel: String, every: usize) -> Mutation {
+ m(move |dir| {
+ let path = dir.join(&rel);
+ let mut bytes = std::fs::read(&path)?;
+ for i in (64..bytes.len()).step_by(every) {
+ bytes[i] ^= 0xff;
+ }
+ Ok(std::fs::write(&path, bytes)?)
+ })
+}
+
+fn garbage(rel: String, n: usize) -> Mutation {
+ m(move |dir| {
+ let bytes: Vec<u8> = (0..n).map(|i| (i * 131 % 251) as u8).collect();
+ Ok(std::fs::write(dir.join(&rel), bytes)?)
+ })
+}
+
+fn remove(rel: String) -> Mutation {
+ m(move |dir| Ok(std::fs::remove_file(dir.join(&rel))?))
+}
+
+fn replace_with_dir(rel: String) -> Mutation {
+ m(move |dir| {
+ let path = dir.join(&rel);
+ std::fs::remove_file(&path)?;
+ Ok(std::fs::create_dir_all(&path)?)
+ })
+}
+
+fn swap(a: String, b: String) -> Mutation {
+ m(move |dir| {
+ let tmp = dir.join("swap.tmp");
+ std::fs::rename(dir.join(&a), &tmp)?;
+ std::fs::rename(dir.join(&b), dir.join(&a))?;
+ Ok(std::fs::rename(&tmp, dir.join(&b))?)
+ })
+}
+
+fn meta(edit: impl Fn(&mut Value) + Send + Sync + 'static) -> Mutation {
+ m(move |dir| edit_json(&dir.join("recording-meta.json"), &edit))
+}
+
+fn config(edit: impl Fn(&mut Value) + Send + Sync + 'static) -> Mutation {
+ m(move |dir| edit_json(&dir.join("project-config.json"), &edit))
+}
+
+fn cursor(value: Value) -> Mutation {
+ m(move |dir| {
+ Ok(std::fs::write(
+ dir.join(seg("cursor.json")),
+ serde_json::to_vec(&value)?,
+ )?)
+ })
+}
+
+fn timeline_segments(segments: Value) -> Mutation {
+ config(move |c| c["timeline"]["segments"] = segments.clone())
+}
+
+fn first_segment(field: &'static str, value: Value) -> Mutation {
+ config(move |c| c["timeline"]["segments"][0][field] = value.clone())
+}
+
+/// (name, mutation, whether a usable project remains, FULL only)
+pub fn mutations() -> Vec<(&'static str, Mutation, bool, bool)> {
+ let mut out: Vec<(&'static str, Mutation, bool, bool)> = vec![
+ ("control", m(|_| Ok(())), true, false),
+ ("display-zero-bytes", zero(seg("display.mp4")), false, false),
+ (
+ "display-truncated-half",
+ truncate(seg("display.mp4"), 0.5),
+ false,
+ false,
+ ),
+ (
+ "display-truncated-1k",
+ truncate_to(seg("display.mp4"), 1024),
+ false,
+ false,
+ ),
+ (
+ "display-smashed",
+ smash(seg("display.mp4"), 4096),
+ false,
+ false,
+ ),
+ (
+ "display-bitflips",
+ flip(seg("display.mp4"), 97),
+ false,
+ false,
+ ),
+ (
+ "display-garbage",
+ garbage(seg("display.mp4"), 50_000),
+ false,
+ false,
+ ),
+ ("display-missing", remove(seg("display.mp4")), false, false),
+ (
+ "display-is-directory",
+ replace_with_dir(seg("display.mp4")),
+ false,
+ false,
+ ),
+ (
+ "display-is-audio",
+ swap(seg("display.mp4"), seg("audio-input.ogg")),
+ false,
+ false,
+ ),
+ ("camera-zero-bytes", zero(seg("camera.mp4")), true, false),
+ (
+ "camera-truncated-half",
+ truncate(seg("camera.mp4"), 0.5),
+ true,
+ false,
+ ),
+ (
+ "camera-smashed",
+ smash(seg("camera.mp4"), 4096),
+ true,
+ false,
+ ),
+ ("camera-missing", remove(seg("camera.mp4")), true, false),
+ ("mic-zero-bytes", zero(seg("audio-input.ogg")), true, false),
+ (
+ "mic-truncated-half",
+ truncate(seg("audio-input.ogg"), 0.5),
+ true,
+ false,
+ ),
+ (
+ "mic-smashed",
+ smash(seg("audio-input.ogg"), 2048),
+ true,
+ false,
+ ),
+ (
+ "mic-garbage",
+ garbage(seg("audio-input.ogg"), 9_000),
+ true,
+ false,
+ ),
+ ("mic-missing", remove(seg("audio-input.ogg")), true, false),
+ (
+ "system-audio-zero-bytes",
+ zero(seg("system_audio.ogg")),
+ true,
+ false,
+ ),
+ (
+ "system-audio-truncated",
+ truncate_to(seg("system_audio.ogg"), 300),
+ true,
+ false,
+ ),
+ ("cursor-zero-bytes", zero(seg("cursor.json")), true, false),
+ (
+ "cursor-garbage",
+ garbage(seg("cursor.json"), 3000),
+ true,
+ false,
+ ),
+ (
+ "cursor-wrong-types",
+ cursor(json!({ "clicks": "x", "moves": [1, 2, 3] })),
+ true,
+ false,
+ ),
+ (
+ "cursor-hostile-values",
+ cursor(json!({
+ "clicks": [
+ { "active_modifiers": [], "cursor_num": 255, "cursor_id": "", "time_ms": -1e300, "down": true },
+ { "active_modifiers": [], "cursor_num": 0, "cursor_id": "99999999999", "time_ms": 1e300, "down": false }
+ ],
+ "moves": [
+ { "active_modifiers": ["\u{0}"], "cursor_id": "0", "time_ms": 500.0, "x": 1e300, "y": -1e300 },
+ { "active_modifiers": [], "cursor_id": "0", "time_ms": 100.0, "x": -5.0, "y": 7.0 },
+ { "active_modifiers": [], "cursor_id": "x", "time_ms": 100.0, "x": 0.5, "y": 0.5 },
+ { "active_modifiers": [], "cursor_id": "0", "time_ms": -1e300, "x": 0.5, "y": 0.5 }
+ ]
+ })),
+ true,
+ false,
+ ),
+ (
+ "cursor-unsorted-duplicates",
+ cursor(json!({
+ "clicks": [],
+ "moves": (0..200).map(|i| json!({
+ "active_modifiers": [], "cursor_id": format!("{}", i % 3),
+ "time_ms": f64::from((i * 7919) % 2000), "x": 0.5, "y": 0.5
+ })).collect::<Vec<_>>()
+ })),
+ true,
+ false,
+ ),
+ (
+ "cursor-200k-moves",
+ cursor(json!({
+ "clicks": [],
+ "moves": (0..200_000).map(|i| json!({
+ "active_modifiers": [], "cursor_id": "0",
+ "time_ms": f64::from(i) * 0.002, "x": 0.5, "y": 0.5
+ })).collect::<Vec<_>>()
+ })),
+ true,
+ true,
+ ),
+ (
+ "meta-zero-bytes",
+ zero("recording-meta.json".into()),
+ false,
+ false,
+ ),
+ (
+ "meta-garbage",
+ garbage("recording-meta.json".into(), 400),
+ false,
+ false,
+ ),
+ (
+ "meta-missing",
+ remove("recording-meta.json".into()),
+ false,
+ false,
+ ),
+ (
+ "meta-wrong-types",
+ meta(|v| v["segments"] = json!("segments")),
+ false,
+ false,
+ ),
+ (
+ "meta-no-segments",
+ meta(|v| v["segments"] = json!([])),
+ false,
+ false,
+ ),
+ (
+ "meta-fps-0",
+ meta(|v| v["segments"][0]["display"]["fps"] = json!(0)),
+ true,
+ false,
+ ),
+ (
+ "meta-fps-huge",
+ meta(|v| v["segments"][0]["display"]["fps"] = json!(u32::MAX)),
+ true,
+ false,
+ ),
+ (
+ "meta-camera-fps-0",
+ meta(|v| v["segments"][0]["camera"]["fps"] = json!(0)),
+ true,
+ false,
+ ),
+ (
+ "meta-start-time-hostile",
+ meta(|v| {
+ v["segments"][0]["display"]["start_time"] = json!(-1e300);
+ v["segments"][0]["camera"]["start_time"] = json!(1e300);
+ v["segments"][0]["mic"]["start_time"] = json!(-3600.0);
+ v["segments"][0]["system_audio"]["start_time"] = json!(1e-300);
+ }),
+ true,
+ false,
+ ),
+ (
+ "meta-path-escapes",
+ meta(|v| v["segments"][0]["display"]["path"] = json!("../../../../../../etc/hosts")),
+ false,
+ false,
+ ),
+ (
+ "meta-path-absolute",
+ meta(|v| v["segments"][0]["camera"]["path"] = json!("/dev/zero")),
+ true,
+ false,
+ ),
+ (
+ "meta-path-unicode",
+ meta(|v| v["segments"][0]["mic"]["path"] = json!("content/\u{1F4A5}/\u{202E}mic.ogg")),
+ true,
+ false,
+ ),
+ (
+ "meta-camera-is-display",
+ meta(|v| v["segments"][0]["camera"]["path"] = json!(seg("display.mp4"))),
+ true,
+ false,
+ ),
+ (
+ "meta-segments-duplicated",
+ meta(|v| {
+ let s = v["segments"][0].clone();
+ v["segments"] = json!([s.clone(), s.clone(), s]);
+ }),
+ true,
+ false,
+ ),
+ (
+ "meta-single-segment-legacy",
+ meta(|v| {
+ let s = v["segments"][0].clone();
+ *v = json!({
+ "platform": "MacOS",
+ "pretty_name": "chaos",
+ "sharing": null,
+ "display": s["display"],
+ "camera": s["camera"],
+ "audio": s["mic"],
+ "cursor": s["cursor"],
+ });
+ }),
+ true,
+ false,
+ ),
+ (
+ "meta-single-segment-damaged-camera",
+ meta(|v| {
+ let s = v["segments"][0].clone();
+ *v = json!({
+ "platform": "MacOS",
+ "pretty_name": "chaos",
+ "sharing": null,
+ "display": s["display"],
+ "camera": { "path": "content/segments/segment-0/missing-camera.mp4", "fps": 30 },
+ "audio": s["mic"],
+ });
+ }),
+ true,
+ false,
+ ),
+ (
+ "meta-instant",
+ meta(|v| {
+ *v = json!({
+ "platform": "MacOS", "pretty_name": "chaos", "sharing": null,
+ "fps": 0, "sample_rate": 0
+ });
+ }),
+ false,
+ false,
+ ),
+ (
+ "meta-status-failed",
+ meta(|v| v["status"] = json!({ "status": "Failed", "error": "\u{0}" })),
+ false,
+ false,
+ ),
+ (
+ "config-zero-bytes",
+ zero("project-config.json".into()),
+ true,
+ false,
+ ),
+ (
+ "config-garbage",
+ garbage("project-config.json".into(), 700),
+ true,
+ false,
+ ),
+ (
+ "config-missing",
+ remove("project-config.json".into()),
+ true,
+ false,
+ ),
+ (
+ "config-wrong-types",
+ config(|c| c["timeline"] = json!([1, "two"])),
+ true,
+ false,
+ ),
+ ("timeline-empty", timeline_segments(json!([])), false, false),
+ (
+ "timeline-null",
+ config(|c| c["timeline"] = Value::Null),
+ true,
+ false,
+ ),
+ (
+ "timeline-clip-out-of-range",
+ first_segment("recordingSegment", json!(99)),
+ true,
+ false,
+ ),
+ (
+ "timeline-clip-huge",
+ first_segment("recordingSegment", json!(u32::MAX)),
+ true,
+ false,
+ ),
+ (
+ "timescale-0",
+ first_segment("timescale", json!(0.0)),
+ true,
+ false,
+ ),
+ (
+ "timescale-negative",
+ first_segment("timescale", json!(-1.0)),
+ true,
+ false,
+ ),
+ (
+ "timescale-huge",
+ first_segment("timescale", json!(1e300)),
+ true,
+ false,
+ ),
+ (
+ "timescale-tiny",
+ first_segment("timescale", json!(1e-300)),
+ true,
+ false,
+ ),
+ (
+ "segment-reversed",
+ timeline_segments(
+ json!([{ "recordingSegment": 0, "timescale": 1.0, "start": 1.5, "end": 0.5 }]),
+ ),
+ false,
+ false,
+ ),
+ (
+ "segment-zero-length",
+ timeline_segments(json!([
+ { "recordingSegment": 0, "timescale": 1.0, "start": 0.5, "end": 0.5 },
+ { "recordingSegment": 0, "timescale": 1.0, "start": 0.0, "end": 1.0 }
+ ])),
+ true,
+ false,
+ ),
+ (
+ "segment-all-zero-length",
+ timeline_segments(
+ json!([{ "recordingSegment": 0, "timescale": 1.0, "start": 1.0, "end": 1.0 }]),
+ ),
+ false,
+ false,
+ ),
+ (
+ "segment-past-media",
+ timeline_segments(
+ json!([{ "recordingSegment": 0, "timescale": 1.0, "start": 1.0, "end": 9.0 }]),
+ ),
+ true,
+ false,
+ ),
+ (
+ "segment-negative-start",
+ timeline_segments(
+ json!([{ "recordingSegment": 0, "timescale": 1.0, "start": -5.0, "end": 1.0 }]),
+ ),
+ true,
+ false,
+ ),
+ (
+ "segment-huge-end",
+ timeline_segments(
+ json!([{ "recordingSegment": 0, "timescale": 1.0, "start": 0.0, "end": 1e300 }]),
+ ),
+ true,
+ true,
+ ),
+ (
+ "segments-overlapping",
+ timeline_segments(json!([
+ { "recordingSegment": 0, "timescale": 1.0, "start": 0.0, "end": 1.5 },
+ { "recordingSegment": 0, "timescale": 1.0, "start": 0.5, "end": 1.0 },
+ { "recordingSegment": 0, "timescale": 1.0, "start": 0.2, "end": 1.9 }
+ ])),
+ true,
+ false,
+ ),
+ (
+ "transition-out-of-range",
+ config(|c| {
+ c["timeline"]["transitions"] =
+ json!([{ "segmentIndex": 99, "type": "cross-fade", "duration": 5.0 }])
+ }),
+ true,
+ false,
+ ),
+ (
+ "transition-hostile",
+ config(|c| {
+ c["timeline"]["transitions"] = json!([
+ { "segmentIndex": 0, "type": "fade-through-black", "duration": -1.0 },
+ { "segmentIndex": 1, "type": "cross-fade", "duration": 1e300 },
+ { "segmentIndex": 1, "type": "cross-fade", "duration": 0.0 }
+ ])
+ }),
+ true,
+ false,
+ ),
+ (
+ "zoom-hostile",
+ config(|c| {
+ c["timeline"]["zoomSegments"] = json!([
+ { "start": 0.0, "end": 1.0, "amount": 0.0, "mode": "auto" },
+ { "start": 1.0, "end": 0.2, "amount": -3.0, "mode": "auto" },
+ { "start": 0.3, "end": 0.3, "amount": 1e300, "mode": { "manual": { "x": 1e300, "y": -1e300 } } },
+ { "start": -1e300, "end": 1e300, "amount": 1e-300, "mode": "auto", "glideSpeed": -1.0, "edgeSnapRatio": 1e300 }
+ ])
+ }),
+ true,
+ false,
+ ),
+ (
+ "scene-hostile",
+ config(|c| {
+ c["timeline"]["sceneSegments"] = json!([
+ { "start": 1.0, "end": 0.0, "mode": "cameraOnly", "transitionIn": -1.0, "transitionOut": 1e300 },
+ { "start": 0.0, "end": 1e300, "mode": "hideCamera" }
+ ])
+ }),
+ true,
+ false,
+ ),
+ (
+ "mask-hostile",
+ config(|c| {
+ c["timeline"]["maskSegments"] = json!([{
+ "start": 0.0, "end": 2.0, "track": u32::MAX, "maskType": "sensitive",
+ "center": { "x": 1e300, "y": -1e300 }, "size": { "x": 0.0, "y": -1.0 },
+ "feather": 1e300, "opacity": -5.0, "pixelation": 0.0, "darkness": 1e300,
+ "fadeDuration": -1.0
+ }, {
+ "start": 0.0, "end": 2.0, "maskType": "highlight",
+ "center": { "x": 0.5, "y": 0.5 }, "size": { "x": 1e300, "y": 1e300 },
+ "pixelation": 1e300
+ }])
+ }),
+ true,
+ false,
+ ),
+ (
+ "text-hostile",
+ config(|c| {
+ c["timeline"]["textSegments"] = json!([
+ { "start": 0.0, "end": 2.0, "content": "\u{0}\u{202E}\u{1F4A5}".repeat(200), "fontSize": 1e30, "fontWeight": -1.0, "size": { "x": 0.0, "y": 0.0 } },
+ { "start": 0.0, "end": 2.0, "content": "", "fontSize": 0.0, "fontFamily": "\u{0}", "color": "not-a-colour" },
+ { "start": 0.0, "end": 2.0, "content": "x", "center": { "x": 1e300, "y": -1e300 }, "size": { "x": 1e300, "y": 1e300 }, "track": u32::MAX }
+ ])
+ }),
+ true,
+ false,
+ ),
+ (
+ "captions-hostile",
+ config(|c| {
+ c["captions"] = json!({
+ "segments": [
+ { "id": "", "start": -1.0, "end": 1e30, "text": "\u{1F4A5}".repeat(5000) },
+ { "id": "b", "start": 1.0, "end": 0.0, "text": "", "words": [{ "text": "w", "start": 5.0, "end": -5.0 }] }
+ ],
+ "settings": { "enabled": true, "size": 0, "fontSize": 4294967295u64, "font": "\u{0}" }
+ });
+ c["timeline"]["captionSegments"] = json!([
+ { "id": "x", "start": 1e30, "end": -1e30, "text": "\u{202E}" }
+ ]);
+ }),
+ true,
+ false,
+ ),
+ (
+ "keyboard-hostile",
+ config(|c| {
+ c["timeline"]["keyboardSegments"] = json!([{
+ "id": "k", "start": 1.0, "end": 0.0, "displayText": "\u{1F4A5}".repeat(3000),
+ "keys": [{ "key": "", "timeOffset": -1e300 }, { "key": "z", "timeOffset": 1e300 }]
+ }])
+ }),
+ true,
+ false,
+ ),
+ (
+ "clips-hostile",
+ config(|c| {
+ c["clips"] = json!([
+ { "index": 99, "offsets": { "camera": 1e300, "mic": -1e300, "system_audio": 0.0 } },
+ { "index": 0, "offsets": { "camera": -1e300, "mic": 1e300, "system_audio": -1e300 } },
+ { "index": 0, "offsets": {} }
+ ])
+ }),
+ true,
+ false,
+ ),
+ (
+ "background-hostile",
+ config(|c| {
+ let b = &mut c["background"];
+ b["padding"] = json!(1e300);
+ b["rounding"] = json!(-1e300);
+ b["blur"] = json!(1e300);
+ b["inset"] = json!(4294967295u64);
+ b["shadow"] = json!(-1.0);
+ b["crop"] = json!({ "position": { "x": 4294967295u64, "y": 0 }, "size": { "x": 0, "y": 0 } });
+ }),
+ true,
+ false,
+ ),
+ (
+ "camera-hostile",
+ config(|c| {
+ let cam = &mut c["camera"];
+ cam["size"] = json!(0.0);
+ cam["zoomSize"] = json!(1e300);
+ cam["rounding"] = json!(-1e300);
+ cam["scaleDuringZoom"] = json!(-1.0);
+ cam["manualPosition"] = json!({ "x": 1e300, "y": -1e300 });
+ }),
+ true,
+ false,
+ ),
+ (
+ "cursor-config-hostile",
+ config(|c| {
+ let cur = &mut c["cursor"];
+ cur["size"] = json!(0);
+ cur["tension"] = json!(-1e300);
+ cur["mass"] = json!(0.0);
+ cur["friction"] = json!(1e300);
+ cur["motionBlur"] = json!(1e300);
+ cur["hideWhenIdleDelay"] = json!(-1.0);
+ cur["hideWhenIdle"] = json!(true);
+ cur["rotationAmount"] = json!(1e300);
+ cur["stopMovementInLastSeconds"] = json!(-1e300);
+ }),
+ true,
+ false,
+ ),
+ (
+ "motion-hostile",
+ config(|c| {
+ c["screenMotionBlur"] = json!(1e30);
+ c["screenMovementSpring"] =
+ json!({ "stiffness": 0.0, "damping": -1e300, "mass": 0.0 });
+ }),
+ true,
+ false,
+ ),
+ (
+ "audio-config-hostile",
+ config(|c| {
+ c["audio"]["micVolumeDb"] = json!(1e30);
+ c["audio"]["systemVolumeDb"] = json!(-1e30);
+ c["timeline"]["segments"][0]["volume"] = json!(-1e300);
+ }),
+ true,
+ false,
+ ),
+ (
+ "annotations-hostile",
+ config(|c| {
+ c["annotations"] = json!([
+ { "id": "", "type": "draw", "x": 1e300, "y": -1e300, "width": -1.0, "height": 0.0,
+ "strokeColor": "", "strokeWidth": 1e300, "fillColor": "#zz", "opacity": -1.0,
+ "rotation": 1e300, "text": null, "points": [] },
+ { "id": "m", "type": "mask", "x": 0.0, "y": 0.0, "width": 1e300, "height": 1e300,
+ "strokeColor": "#000", "strokeWidth": 0.0, "fillColor": "#000", "opacity": 1.0,
+ "rotation": 0.0, "text": null, "maskType": "pixelate", "maskLevel": 0.0 }
+ ])
+ }),
+ true,
+ false,
+ ),
+ (
+ "style-image-hostile",
+ config(|c| {
+ c["timeline"]["imageSegments"] = json!([
+ { "start": 0.0, "end": 2.0, "path": "/dev/zero", "size": { "x": 1e300, "y": 0.0 }, "opacity": -1.0, "rotation": 1e30 },
+ { "start": 0.0, "end": 2.0, "path": "../../../../etc/passwd" },
+ { "start": 0.0, "end": 2.0, "path": "recording-meta.json" }
+ ]);
+ c["timeline"]["styleSegments"] = json!([
+ { "start": 1.0, "end": 0.0, "overrides": { "background": { "padding": 1e300 } } }
+ ]);
+ }),
+ true,
+ false,
+ ),
+ (
+ "camera3d-hostile",
+ config(|c| {
+ c["timeline"]["camera3dSegments"] = json!([{
+ "start": 0.0, "end": 2.0,
+ "properties": { "tiltX": 1e300, "tiltY": -1e300, "zoom": 0.0, "fov": 0.0, "panX": 1e300, "rotateX": 90.0 }
+ }])
+ }),
+ true,
+ false,
+ ),
+ (
+ "aspect-and-unicode",
+ config(|c| {
+ c["aspectRatio"] = json!("square");
+ c["timeline"]["segments"][0]["name"] = json!("\u{0}\u{202E}\u{1F4A5}".repeat(1000));
+ }),
+ true,
+ false,
+ ),
+ ];
+ let deep = m(|dir| {
+ let mut text = String::new();
+ for _ in 0..100_000 {
+ text.push('[');
+ }
+ Ok(std::fs::write(dir.join("project-config.json"), text)?)
+ });
+ out.push(("config-deeply-nested", deep, true, false));
+ out
+}
+
+/// The app's editor preflight (`editor_window::preflight`): heal, load the
+/// meta, open every track, load the configuration and walk its timeline.
+pub fn open(dir: &Path) -> anyhow::Result<Verdict> {
+ let _ = cap_recording::track_heal::heal_stretched_tracks(dir);
+ let meta = match RecordingMeta::load_for_project(dir) {
+ Ok(meta) => meta,
+ Err(e) => return Ok(Verdict::rejected(format!("meta: {e}"))),
+ };
+ let Some(studio) = meta.studio_meta() else {
+ return Ok(Verdict::rejected("not a studio recording"));
+ };
+ let recordings = match cap_rendering::ProjectRecordingsMeta::new(&dir.to_path_buf(), studio) {
+ Ok(r) => r,
+ Err(e) => return Ok(Verdict::rejected(format!("recordings: {e}"))),
+ };
+ let config = meta.project_config();
+ let duration = config
+ .timeline
+ .as_ref()
+ .map(|t| t.duration())
+ .unwrap_or_else(|| recordings.duration());
+ let mut mapped = 0;
+ if let Some(timeline) = &config.timeline {
+ for i in 0..=40 {
+ let t = duration * f64::from(i) / 40.0;
+ if timeline.get_frame_mapping(t).is_some() {
+ mapped += 1;
+ }
+ let _ = config.get_segment_time(t);
+ }
+ }
+ Ok(Verdict::Ok(format!(
+ "duration {duration:.3}, {mapped} of 41 times mapped"
+ )))
+}
+
+/// The editor: `EditorInstance` (decoders, renderer, offline audio), preview
+/// frames at hostile positions, playback, seeks, stop and dispose.
+pub async fn editor(dir: &Path, expect_frames: bool) -> anyhow::Result<Verdict> {
+ let rendered = Arc::new(AtomicUsize::new(0));
+ let counter = rendered.clone();
+ let (audio, _sink) = cap_editor::AudioOutput::new_offline();
+ let instance = match cap_editor::EditorInstance::new_with_audio_output(
+ dir.to_path_buf(),
+ |_| {},
+ Box::new(move |_, _| {
+ counter.fetch_add(1, Ordering::Relaxed);
+ }),
+ None,
+ Arc::new(audio),
+ )
+ .await
+ {
+ Ok(instance) => instance,
+ Err(e) => return Ok(Verdict::rejected(format!("editor: {e}"))),
+ };
+ let resolution = XY::new(320, 180);
+ for frame in [0, 1, 29, 59, 10_000, u32::MAX] {
+ let _ = instance.preview_tx.send(Some((frame, 30, resolution)));
+ tokio::time::sleep(Duration::from_millis(150)).await;
+ }
+ let _ = instance.preview_tx.send(Some((10, 0, XY::new(0, 0))));
+ tokio::time::sleep(Duration::from_millis(150)).await;
+ instance.start_playback(30, resolution).await;
+ tokio::time::sleep(Duration::from_millis(500)).await;
+ for frame in [15, 0, 59, 100_000, 3] {
+ instance.seek_playback(frame).await;
+ tokio::time::sleep(Duration::from_millis(80)).await;
+ }
+ instance.dispose().await;
+ let n = rendered.load(Ordering::Relaxed);
+ Ok(if n == 0 && expect_frames {
+ Verdict::Wrong("the editor opened but delivered no frame".into())
+ } else {
+ Verdict::Ok(format!("{n} frames delivered"))
+ })
+}
+
+#[derive(Clone, Copy, Debug)]
+pub enum Format {
+ Mp4 { bpp: Option<f32> },
+ Gif,
+ Mov { cursor_only: bool },
+}
+
+pub async fn export(
+ dir: &Path,
+ out: &Path,
+ format: Format,
+ fps: u32,
+ resolution: (u32, u32),
+ expect_media: bool,
+) -> anyhow::Result<Verdict> {
+ let base = match cap_export::ExporterBase::builder(dir.to_path_buf())
+ .with_output_path(out.to_path_buf())
+ .build()
+ .await
+ {
+ Ok(base) => base,
+ Err(e) => return Ok(Verdict::rejected(format!("exporter: {e}"))),
+ };
+ let resolution_base = XY::new(resolution.0, resolution.1);
+ let result = match format {
+ Format::Mp4 { bpp } => {
+ cap_export::mp4::Mp4ExportSettings {
+ fps,
+ resolution_base,
+ compression: cap_export::mp4::ExportCompression::Potato,
+ custom_bpp: bpp,
+ force_ffmpeg_decoder: false,
+ optimize_filesize: false,
+ }
+ .export(base, |_| true)
+ .await
+ }
+ Format::Gif => {
+ cap_export::gif::GifExportSettings {
+ fps,
+ resolution_base,
+ quality: None,
+ }
+ .export(base, |_| true)
+ .await
+ }
+ Format::Mov { cursor_only } => {
+ cap_export::mov::MovExportSettings {
+ fps,
+ resolution_base,
+ cursor_only,
+ }
+ .export(base, |_| true)
+ .await
+ }
+ };
+ let path = match result {
+ Ok(path) => path,
+ Err(e) => return Ok(Verdict::rejected(format!("export: {e}"))),
+ };
+ let size = std::fs::metadata(&path).map(|m| m.len()).unwrap_or(0);
+ if !expect_media {
+ return Ok(Verdict::Ok(format!("{size} bytes")));
+ }
+ if size == 0 {
+ return Ok(Verdict::Wrong(format!(
+ "export reported success with an empty file {}",
+ path.display()
+ )));
+ }
+ match format {
+ Format::Gif => Ok(Verdict::Ok(format!("{size} bytes"))),
+ _ => Ok(super::media_check::judge_output(&path, true)),
+ }
+}
+
+fn file_probes(out: &mut Vec<Probe>) {
+ for (name, mutation, usable, full) in mutations() {
+ for engine in ["open", "editor", "export"] {
+ let mutation = mutation.clone();
+ let probe = Probe::new(
+ format!("chaos/files/{name}/{engine}"),
+ if engine == "open" { 45 } else { 90 },
+ move |ctx: &Ctx| {
+ let dir = prepare(ctx).context("prepare project")?;
+ mutation(&dir).context("mutate project")?;
+ match engine {
+ "open" => open(&dir),
+ "editor" => ctx.block_on(editor(&dir, usable)),
+ _ => ctx.block_on(export(
+ &dir,
+ &ctx.path("out.mp4"),
+ Format::Mp4 { bpp: None },
+ 10,
+ (320, 180),
+ usable,
+ )),
+ }
+ },
+ );
+ let editor_full = engine == "editor" && !FAST_EDITOR.contains(&name);
+ out.push(if full || editor_full {
+ probe.full()
+ } else {
+ probe
+ });
+ }
+ }
+}
+
+/// Mutations the editor runs in the FAST tier; FULL opens every mutation.
+const FAST_EDITOR: &[&str] = &[
+ "control",
+ "display-zero-bytes",
+ "display-smashed",
+ "camera-missing",
+ "mic-garbage",
+ "cursor-hostile-values",
+ "meta-fps-0",
+ "meta-fps-huge",
+ "meta-start-time-hostile",
+ "meta-single-segment-damaged-camera",
+ "timeline-empty",
+ "timeline-clip-out-of-range",
+ "timescale-0",
+ "timescale-tiny",
+ "segment-reversed",
+ "zoom-hostile",
+ "mask-hostile",
+ "text-hostile",
+ "captions-hostile",
+ "background-hostile",
+];
+
+/// (name, format, fps, resolution, usable output expected, FULL only)
+type ExportCase = (&'static str, Format, u32, (u32, u32), bool, bool);
+
+fn export_probes(out: &mut Vec<Probe>) {
+ let cases: Vec<ExportCase> = vec![
+ (
+ "mp4/control",
+ Format::Mp4 { bpp: None },
+ 10,
+ (320, 180),
+ true,
+ false,
+ ),
+ (
+ "mp4/resolution-0x0",
+ Format::Mp4 { bpp: None },
+ 10,
+ (0, 0),
+ false,
+ false,
+ ),
+ (
+ "mp4/resolution-1x1",
+ Format::Mp4 { bpp: None },
+ 10,
+ (1, 1),
+ false,
+ false,
+ ),
+ (
+ "mp4/resolution-3x3",
+ Format::Mp4 { bpp: None },
+ 10,
+ (3, 3),
+ false,
+ false,
+ ),
+ (
+ "mp4/resolution-odd",
+ Format::Mp4 { bpp: None },
+ 10,
+ (641, 361),
+ true,
+ false,
+ ),
+ (
+ "mp4/resolution-tall",
+ Format::Mp4 { bpp: None },
+ 10,
+ (2, 4000),
+ false,
+ false,
+ ),
+ (
+ "mp4/resolution-16384",
+ Format::Mp4 { bpp: None },
+ 1,
+ (16384, 16384),
+ false,
+ true,
+ ),
+ (
+ "mp4/resolution-max",
+ Format::Mp4 { bpp: None },
+ 1,
+ (u32::MAX, u32::MAX),
+ false,
+ false,
+ ),
+ (
+ "mp4/fps-0",
+ Format::Mp4 { bpp: None },
+ 0,
+ (320, 180),
+ false,
+ false,
+ ),
+ (
+ "mp4/fps-1",
+ Format::Mp4 { bpp: None },
+ 1,
+ (320, 180),
+ true,
+ false,
+ ),
+ (
+ "mp4/fps-240",
+ Format::Mp4 { bpp: None },
+ 240,
+ (320, 180),
+ true,
+ false,
+ ),
+ (
+ "mp4/fps-max",
+ Format::Mp4 { bpp: None },
+ u32::MAX,
+ (320, 180),
+ false,
+ true,
+ ),
+ (
+ "mp4/bpp-nan",
+ Format::Mp4 {
+ bpp: Some(f32::NAN),
+ },
+ 10,
+ (320, 180),
+ true,
+ false,
+ ),
+ (
+ "mp4/bpp-negative",
+ Format::Mp4 { bpp: Some(-1.0) },
+ 10,
+ (320, 180),
+ true,
+ false,
+ ),
+ (
+ "mp4/bpp-inf",
+ Format::Mp4 {
+ bpp: Some(f32::INFINITY),
+ },
+ 10,
+ (320, 180),
+ true,
+ false,
+ ),
+ (
+ "mp4/bpp-zero",
+ Format::Mp4 { bpp: Some(0.0) },
+ 10,
+ (320, 180),
+ true,
+ false,
+ ),
+ ("gif/control", Format::Gif, 10, (320, 180), true, false),
+ ("gif/resolution-0x0", Format::Gif, 10, (0, 0), false, false),
+ ("gif/resolution-1x1", Format::Gif, 10, (1, 1), false, false),
+ ("gif/fps-0", Format::Gif, 0, (320, 180), false, false),
+ ("gif/fps-240", Format::Gif, 240, (160, 90), true, true),
+ (
+ "mov/control",
+ Format::Mov { cursor_only: false },
+ 10,
+ (320, 180),
+ true,
+ false,
+ ),
+ (
+ "mov/cursor-only",
+ Format::Mov { cursor_only: true },
+ 10,
+ (320, 180),
+ true,
+ false,
+ ),
+ (
+ "mov/resolution-0x0",
+ Format::Mov { cursor_only: false },
+ 10,
+ (0, 0),
+ false,
+ false,
+ ),
+ (
+ "mov/fps-0",
+ Format::Mov { cursor_only: true },
+ 0,
+ (320, 180),
+ false,
+ false,
+ ),
+ ];
+ for (name, format, fps, resolution, expect, full) in cases {
+ let probe = Probe::new(format!("chaos/export/{name}"), 120, move |ctx: &Ctx| {
+ let dir = prepare(ctx)?;
+ let ext = match format {
+ Format::Gif => "gif",
+ Format::Mov { .. } => "mov",
+ Format::Mp4 { .. } => "mp4",
+ };
+ ctx.block_on(export(
+ &dir,
+ &ctx.path(&format!("out.{ext}")),
+ format,
+ fps,
+ resolution,
+ expect,
+ ))
+ });
+ out.push(if full { probe.full() } else { probe });
+ }
+}
+
+type Poison = fn(&mut cap_project::ProjectConfiguration, f64);
+
+/// Non-finite values the editor's own arithmetic can hand the renderer and
+/// exporter in memory (JSON cannot carry NaN).
+fn poisons() -> Vec<(&'static str, Poison)> {
+ fn timeline(
+ c: &mut cap_project::ProjectConfiguration,
+ ) -> &mut cap_project::TimelineConfiguration {
+ c.timeline.as_mut().expect("rich config has a timeline")
+ }
+ vec![
+ ("timescale", |c, v| timeline(c).segments[0].timescale = v),
+ ("segment-start", |c, v| timeline(c).segments[1].start = v),
+ ("segment-end", |c, v| timeline(c).segments[0].end = v),
+ ("zoom-amount", |c, v| {
+ timeline(c).zoom_segments[0].amount = v
+ }),
+ ("zoom-start", |c, v| timeline(c).zoom_segments[1].start = v),
+ ("transition-duration", |c, v| {
+ timeline(c).transitions[0].duration = v
+ }),
+ ("scene-transition", |c, v| {
+ timeline(c).scene_segments[0].transition_in = v
+ }),
+ ("mask-center", |c, v| {
+ timeline(c).mask_segments[0].center.x = v
+ }),
+ ("text-font-size", |c, v| {
+ timeline(c).text_segments[0].font_size = v as f32
+ }),
+ ("caption-start", |c, v| {
+ timeline(c).caption_segments[0].start = v
+ }),
+ ("clip-offset", |c, v| c.clips[0].offsets.camera = v as f32),
+ ("background-padding", |c, v| c.background.padding = v),
+ ("background-rounding", |c, v| c.background.rounding = v),
+ ("camera-size", |c, v| c.camera.size = v as f32),
+ ("cursor-tension", |c, v| c.cursor.tension = v as f32),
+ ("cursor-motion-blur", |c, v| c.cursor.motion_blur = v as f32),
+ ("screen-motion-blur", |c, v| c.screen_motion_blur = v as f32),
+ ]
+}
+
+fn poisoned_config(
+ dir: &Path,
+ poison: Poison,
+ value: f64,
+) -> anyhow::Result<cap_project::ProjectConfiguration> {
+ let meta = RecordingMeta::load_for_project(dir).map_err(|e| anyhow::anyhow!("meta: {e}"))?;
+ let mut config = meta.project_config();
+ poison(&mut config, value);
+ Ok(config)
+}
+
+async fn export_config(
+ dir: &Path,
+ out: &Path,
+ config: cap_project::ProjectConfiguration,
+) -> anyhow::Result<Verdict> {
+ let base = match cap_export::ExporterBase::builder(dir.to_path_buf())
+ .with_config(config)
+ .with_output_path(out.to_path_buf())
+ .build()
+ .await
+ {
+ Ok(base) => base,
+ Err(e) => return Ok(Verdict::rejected(format!("exporter: {e}"))),
+ };
+ match (cap_export::mp4::Mp4ExportSettings {
+ fps: 10,
+ resolution_base: XY::new(160, 90),
+ compression: cap_export::mp4::ExportCompression::Potato,
+ custom_bpp: None,
+ force_ffmpeg_decoder: false,
+ optimize_filesize: false,
+ })
+ .export(base, |_| true)
+ .await
+ {
+ Ok(path) => Ok(Verdict::Ok(format!(
+ "{} bytes",
+ std::fs::metadata(path).map(|m| m.len()).unwrap_or(0)
+ ))),
+ Err(e) => Ok(Verdict::rejected(format!("export: {e}"))),
+ }
+}
+
+/// The editor with a poisoned configuration pushed in memory, as the
+/// editor UI does on every edit, then preview frames and playback.
+async fn editor_config(
+ dir: &Path,
+ config: cap_project::ProjectConfiguration,
+) -> anyhow::Result<Verdict> {
+ let (audio, _sink) = cap_editor::AudioOutput::new_offline();
+ let instance = match cap_editor::EditorInstance::new_with_audio_output(
+ dir.to_path_buf(),
+ |_| {},
+ Box::new(|_, _| {}),
+ None,
+ Arc::new(audio),
+ )
+ .await
+ {
+ Ok(instance) => instance,
+ Err(e) => return Ok(Verdict::rejected(format!("editor: {e}"))),
+ };
+ let _ = instance.project_config.0.send(config);
+ for frame in [0, 7, 15, 30, 45] {
+ let _ = instance
+ .preview_tx
+ .send(Some((frame, 30, XY::new(320, 180))));
+ tokio::time::sleep(Duration::from_millis(120)).await;
+ }
+ instance.start_playback(30, XY::new(320, 180)).await;
+ tokio::time::sleep(Duration::from_millis(400)).await;
+ instance.seek_playback(20).await;
+ tokio::time::sleep(Duration::from_millis(200)).await;
+ instance.dispose().await;
+ Ok(Verdict::Ok("editor survived".into()))
+}
+
+fn poison_probes(out: &mut Vec<Probe>) {
+ for (name, poison) in poisons() {
+ for (value_name, value) in [
+ ("nan", f64::NAN),
+ ("inf", f64::INFINITY),
+ ("neg-inf", f64::NEG_INFINITY),
+ ] {
+ let export = Probe::new(
+ format!("chaos/export/poison/{name}/{value_name}"),
+ 90,
+ move |ctx: &Ctx| {
+ let dir = prepare(ctx)?;
+ let config = poisoned_config(&dir, poison, value)?;
+ ctx.block_on(export_config(&dir, &ctx.path("out.mp4"), config))
+ },
+ );
+ let fast = value_name == "nan"
+ || matches!(
+ (name, value_name),
+ ("segment-end", "inf")
+ | ("segment-start", "neg-inf")
+ | ("clip-offset", "inf")
+ | ("background-padding", "inf")
+ );
+ out.push(if fast { export } else { export.full() });
+ let probe = Probe::new(
+ format!("chaos/editor/poison/{name}/{value_name}"),
+ 90,
+ move |ctx: &Ctx| {
+ let dir = prepare(ctx)?;
+ let config = poisoned_config(&dir, poison, value)?;
+ ctx.block_on(editor_config(&dir, config))
+ },
+ );
+ let fast = value_name == "nan"
+ && matches!(
+ name,
+ "timescale" | "segment-end" | "zoom-amount" | "background-padding"
+ );
+ out.push(if fast { probe } else { probe.full() });
+ }
+ }
+}
+
+pub fn probes() -> Vec<Probe> {
+ let mut out = Vec::new();
+ file_probes(&mut out);
+ export_probes(&mut out);
+ poison_probes(&mut out);
+ out
+}
diff --git a/crates/verify/tests/chaos_support/record.rs b/crates/verify/tests/chaos_support/record.rs
new file mode 100644
index 000000000..b8f6aecb8
--- /dev/null
+++ b/crates/verify/tests/chaos_support/record.rs
@@ -0,0 +1,1170 @@
+//! Device and source chaos: hostile synthetic frames, audio buffers and
+//! control sequences through the product's recording pipelines
+//! (`OutputPipeline` with the Studio and Instant muxers, the microphone and
+//! camera feeds and sources, and the Studio and Instant recording actors).
+
+use std::{
+ path::Path,
+ sync::Arc,
+ time::{Duration, Instant, SystemTime},
+};
+
+use anyhow::anyhow;
+use cap_media_info::{AudioInfo, Pixel, Sample, Type, VideoInfo};
+use cap_recording::{
+ AudioFrame, ChannelAudioSource, ChannelAudioSourceConfig, ChannelVideoSource,
+ ChannelVideoSourceConfig, OutputPipeline,
+ ffmpeg::{
+ DashSegmentedAudioMuxer, DashSegmentedAudioMuxerConfig, FFmpegVideoFrame,
+ FragmentedAudioMuxer, FragmentedAudioMuxerConfig, Mp4Muxer, OggMuxer, SegmentedVideoMuxer,
+ SegmentedVideoMuxerConfig,
+ },
+};
+use cap_timestamp::{Timestamp, Timestamps};
+
+use super::{
+ harness::{Ctx, Probe, Verdict},
+ media_check::judge_output,
+};
+
+/// Which clock a hostile frame is stamped with.
+#[derive(Clone, Copy, Debug)]
+pub enum Clock {
+ /// `Instant`, offset seconds from the pipeline epoch (may be negative).
+ Instant(f64),
+ /// `SystemTime`, offset seconds from the epoch's wall clock.
+ System(f64),
+ /// The host clock real capture devices stamp (mach ticks on macOS).
+ Host(f64),
+}
+
+fn timestamp(clock: Clock, timestamps: Timestamps) -> Timestamp {
+ fn shift_instant(base: Instant, secs: f64) -> Instant {
+ if secs >= 0.0 {
+ base.checked_add(Duration::from_secs_f64(secs))
+ .unwrap_or(base)
+ } else {
+ base.checked_sub(Duration::from_secs_f64(-secs))
+ .unwrap_or(base)
+ }
+ }
+ match clock {
+ Clock::Instant(secs) => Timestamp::Instant(shift_instant(timestamps.instant(), secs)),
+ Clock::System(secs) => {
+ let base = timestamps.system_time();
+ Timestamp::SystemTime(if secs >= 0.0 {
+ base + Duration::from_secs_f64(secs)
+ } else {
+ base.checked_sub(Duration::from_secs_f64(-secs))
+ .unwrap_or(SystemTime::UNIX_EPOCH)
+ })
+ }
+ Clock::Host(secs) => host(timestamps, secs),
+ }
+}
+
+#[cfg(target_os = "macos")]
+fn host(timestamps: Timestamps, secs: f64) -> Timestamp {
+ let base = timestamps.mach_absolute_time();
+ Timestamp::MachAbsoluteTime(if secs >= 0.0 {
+ base + Duration::from_secs_f64(secs)
+ } else {
+ base - Duration::from_secs_f64(-secs)
+ })
+}
+
+#[cfg(not(target_os = "macos"))]
+fn host(timestamps: Timestamps, secs: f64) -> Timestamp {
+ timestamp(Clock::Instant(secs), timestamps)
+}
+
+/// One hostile video frame.
+#[derive(Clone, Debug)]
+pub struct VFrame {
+ pub width: u32,
+ pub height: u32,
+ pub format: Pixel,
+ pub clock: Clock,
+ /// Seconds to wait before handing it over (bursts are 0, stalls long).
+ pub wait: f64,
+}
+
+#[derive(Clone, Debug)]
+pub struct VideoScript {
+ pub info: VideoInfo,
+ pub frames: Vec<VFrame>,
+ /// Some frame matches the declared format, so an empty output is wrong.
+ pub expect_media: bool,
+ /// Pause and resume the pipeline at these frame indices.
+ pub pause_at: Vec<usize>,
+ pub resume_at: Vec<usize>,
+ /// Close the source channel after this many frames (the device vanished).
+ pub vanish_after: Option<usize>,
+}
+
+fn steady(width: u32, height: u32, format: Pixel, fps: u32, count: usize) -> Vec<VFrame> {
+ (0..count)
+ .map(|i| VFrame {
+ width,
+ height,
+ format,
+ clock: Clock::Host(0.05 + i as f64 / f64::from(fps.max(1))),
+ wait: 1.0 / f64::from(fps.max(1)),
+ })
+ .collect()
+}
+
+fn script(info: VideoInfo, frames: Vec<VFrame>, expect_media: bool) -> VideoScript {
+ VideoScript {
+ info,
+ frames,
+ expect_media,
+ pause_at: Vec::new(),
+ resume_at: Vec::new(),
+ vanish_after: None,
+ }
+}
+
+fn info(format: Pixel, width: u32, height: u32, fps: u32) -> VideoInfo {
+ VideoInfo::from_raw_ffmpeg(format, width, height, fps)
+}
+
+/// The hostile video scripts, by name, for a source whose native format is
+/// `format` (BGRA for FFmpeg frames, NV12 for ScreenCaptureKit buffers).
+pub fn video_scripts(format: Pixel, other: Pixel) -> Vec<(&'static str, bool, VideoScript)> {
+ let w = 320;
+ let h = 180;
+ let mut out: Vec<(&'static str, bool, VideoScript)> = vec![(
+ "control",
+ false,
+ script(info(format, w, h, 30), steady(w, h, format, 30, 20), true),
+ )];
+ out.push((
+ "info-zero-size",
+ false,
+ script(
+ info(format, 0, 0, 30),
+ steady(64, 64, format, 30, 10),
+ false,
+ ),
+ ));
+ out.push((
+ "one-pixel",
+ false,
+ script(info(format, 1, 1, 30), steady(1, 1, format, 30, 10), false),
+ ));
+ out.push((
+ "odd-size",
+ false,
+ script(
+ info(format, 641, 361, 30),
+ steady(641, 361, format, 30, 10),
+ true,
+ ),
+ ));
+ out.push((
+ "info-huge",
+ false,
+ script(
+ info(format, 16384, 16384, 30),
+ steady(w, h, format, 30, 6),
+ false,
+ ),
+ ));
+ out.push((
+ "huge-frames",
+ true,
+ script(
+ info(format, 8192, 8192, 30),
+ steady(8192, 8192, format, 30, 3),
+ false,
+ ),
+ ));
+ let mut changing = steady(w, h, format, 30, 6);
+ changing.extend(steady(640, 360, format, 30, 6));
+ changing.extend(steady(160, 90, format, 30, 6));
+ changing.extend(steady(w + 1, h + 1, format, 30, 6));
+ for (i, f) in changing.iter_mut().enumerate() {
+ f.clock = Clock::Host(0.05 + i as f64 / 30.0);
+ }
+ out.push((
+ "size-changes",
+ false,
+ script(info(format, w, h, 30), changing, true),
+ ));
+ out.push((
+ "wrong-format",
+ false,
+ script(info(format, w, h, 30), steady(w, h, other, 30, 10), false),
+ ));
+ let mut backwards = steady(w, h, format, 30, 12);
+ for (i, f) in backwards.iter_mut().enumerate() {
+ f.clock = Clock::Host(1.0 - i as f64 / 30.0);
+ }
+ out.push((
+ "ts-backwards",
+ false,
+ script(info(format, w, h, 30), backwards, true),
+ ));
+ let mut repeat = steady(w, h, format, 30, 12);
+ for f in &mut repeat {
+ f.clock = Clock::Host(0.1);
+ }
+ out.push((
+ "ts-repeat",
+ false,
+ script(info(format, w, h, 30), repeat, true),
+ ));
+ let mut jump = steady(w, h, format, 30, 12);
+ for (i, f) in jump.iter_mut().enumerate().skip(6) {
+ f.clock = Clock::Host(3.0 * 3600.0 + i as f64 / 30.0);
+ }
+ out.push((
+ "ts-jump-hours",
+ false,
+ script(info(format, w, h, 30), jump, true),
+ ));
+ let mut before = steady(w, h, format, 30, 12);
+ for (i, f) in before.iter_mut().enumerate() {
+ f.clock = Clock::Host(-10.0 + i as f64 / 30.0);
+ }
+ out.push((
+ "ts-before-epoch",
+ false,
+ script(info(format, w, h, 30), before, true),
+ ));
+ let mut mixed = steady(w, h, format, 30, 12);
+ for (i, f) in mixed.iter_mut().enumerate() {
+ let t = 0.05 + i as f64 / 30.0;
+ f.clock = match i % 3 {
+ 0 => Clock::Host(t),
+ 1 => Clock::Instant(t),
+ _ => Clock::System(t),
+ };
+ }
+ out.push((
+ "ts-mixed-clocks",
+ false,
+ script(info(format, w, h, 30), mixed, true),
+ ));
+ let mut wall_past = steady(w, h, format, 30, 8);
+ for (i, f) in wall_past.iter_mut().enumerate() {
+ f.clock = Clock::System(-3600.0 + i as f64 / 30.0);
+ }
+ out.push((
+ "ts-wallclock-past",
+ false,
+ script(info(format, w, h, 30), wall_past, true),
+ ));
+ for (name, fps) in [("fps-0", 0), ("fps-1", 1), ("fps-1000", 1000)] {
+ out.push((
+ name,
+ false,
+ script(
+ info(format, w, h, fps),
+ steady(w, h, format, fps.clamp(1, 240), 10),
+ true,
+ ),
+ ));
+ }
+ let mut burst = steady(w, h, format, 30, 240);
+ for f in &mut burst {
+ f.wait = 0.0;
+ }
+ out.push(("burst", false, script(info(format, w, h, 30), burst, true)));
+ let mut stall = steady(w, h, format, 30, 6);
+ stall[3].wait = 5.0;
+ for (i, f) in stall.iter_mut().enumerate().skip(3) {
+ f.clock = Clock::Host(5.2 + i as f64 / 30.0);
+ }
+ out.push(("stall", false, script(info(format, w, h, 30), stall, true)));
+ out.push((
+ "no-frames",
+ false,
+ script(info(format, w, h, 30), Vec::new(), false),
+ ));
+ let mut paused = script(info(format, w, h, 30), steady(w, h, format, 30, 30), true);
+ paused.pause_at = vec![0, 10, 11, 20];
+ paused.resume_at = vec![5, 12, 25];
+ out.push(("pause-resume-hostile", false, paused));
+ let mut vanish = script(info(format, w, h, 30), steady(w, h, format, 30, 20), true);
+ vanish.vanish_after = Some(5);
+ out.push(("device-vanishes", false, vanish));
+ out
+}
+
+/// An FFmpeg frame of `format` and size, with a deterministic pattern.
+/// Frames FFmpeg cannot allocate are returned as allocated (empty planes),
+/// like a converter that produced nothing.
+fn ffmpeg_frame(format: Pixel, width: u32, height: u32, seed: u32) -> ffmpeg::frame::Video {
+ let mut frame = ffmpeg::frame::Video::new(format, width, height);
+ let allocated = unsafe { !(*frame.as_ptr()).data[0].is_null() };
+ if allocated {
+ for plane in 0..frame.planes() {
+ let data = frame.data_mut(plane);
+ for (i, byte) in data.iter_mut().enumerate() {
+ *byte = (i as u32).wrapping_mul(31).wrapping_add(seed) as u8;
+ }
+ }
+ }
+ frame
+}
+
+pub enum VideoMuxer {
+ Mp4,
+ Segmented,
+ #[cfg(target_os = "macos")]
+ MacFragmented,
+ #[cfg(target_os = "macos")]
+ MacOop,
+ #[cfg(target_os = "macos")]
+ MacAvfMp4,
+}
+
+impl VideoMuxer {
+ fn output(&self, dir: &Path) -> std::path::PathBuf {
+ match self {
+ VideoMuxer::Mp4 => dir.join("out.mp4"),
+ #[cfg(target_os = "macos")]
+ VideoMuxer::MacAvfMp4 => dir.join("out.mp4"),
+ _ => dir.join("display"),
+ }
+ }
+}
+
+async fn run_ffmpeg_video(
+ ctx: &Ctx,
+ muxer: &VideoMuxer,
+ script: &VideoScript,
+) -> anyhow::Result<Verdict> {
+ let timestamps = Timestamps::now();
+ let (tx, rx) = flume::bounded::<FFmpegVideoFrame>(64);
+ let output = muxer.output(&ctx.scratch);
+ let builder = OutputPipeline::builder(output.clone())
+ .with_video::<ChannelVideoSource<FFmpegVideoFrame>>(ChannelVideoSourceConfig::new(
+ script.info,
+ rx,
+ ))
+ .with_timestamps(timestamps);
+ let built = match muxer {
+ VideoMuxer::Mp4 => builder.build::<Mp4Muxer>(()).await,
+ _ => {
+ builder
+ .build::<SegmentedVideoMuxer>(SegmentedVideoMuxerConfig {
+ segment_duration: Duration::from_secs(2),
+ ..Default::default()
+ })
+ .await
+ }
+ };
+ let pipeline = match built {
+ Ok(p) => p,
+ Err(e) => return Ok(Verdict::rejected(format!("build: {e:#}"))),
+ };
+ let mut tx = Some(tx);
+ for (i, f) in script.frames.iter().enumerate() {
+ if script.pause_at.contains(&i) {
+ pipeline.pause();
+ }
+ if script.resume_at.contains(&i) {
+ pipeline.resume();
+ }
+ if script.vanish_after == Some(i) {
+ tx = None;
+ }
+ if f.wait > 0.0 {
+ tokio::time::sleep(Duration::from_secs_f64(f.wait)).await;
+ }
+ let Some(sender) = &tx else { break };
+ let frame = FFmpegVideoFrame {
+ inner: ffmpeg_frame(f.format, f.width, f.height, i as u32),
+ timestamp: timestamp(f.clock, timestamps),
+ };
+ if sender.send_async(frame).await.is_err() {
+ break;
+ }
+ }
+ tokio::time::sleep(Duration::from_millis(200)).await;
+ let stopped = pipeline.stop().await;
+ drop(tx);
+ match stopped {
+ Err(e) => Ok(Verdict::rejected(format!("stop: {e:#}"))),
+ Ok(_) => Ok(judge_output(&output, script.expect_media)),
+ }
+}
+
+#[cfg(target_os = "macos")]
+mod mac {
+ use super::*;
+ use cap_recording::{
+ MacOSFragmentedM4SMuxer, MacOSFragmentedM4SMuxerConfig, OutOfProcessFragmentedM4SMuxer,
+ OutOfProcessFragmentedM4SMuxerConfig, screen_capture,
+ };
+ use cidre::{arc, cf, cm, cv};
+
+ pub fn cv_format(format: Pixel) -> cv::PixelFormat {
+ match format {
+ Pixel::BGRA => cv::PixelFormat::_32_BGRA,
+ Pixel::UYVY422 => cv::PixelFormat::_2VUY,
+ Pixel::P010LE => cv::PixelFormat(u32::from_be_bytes(*b"x420")),
+ Pixel::YUYV422 => cv::PixelFormat(u32::from_be_bytes(*b"yuvs")),
+ Pixel::NV21 => cv::PixelFormat::_420F,
+ _ => cv::PixelFormat::_420V,
+ }
+ }
+
+ pub fn sample(
+ format: Pixel,
+ width: u32,
+ height: u32,
+ fps: u32,
+ index: u32,
+ ) -> anyhow::Result<arc::R<cm::SampleBuf>> {
+ let io_surface_properties = cf::Dictionary::new();
+ let keys: [&cf::Type; 1] = [cv::pixel_buffer::keys::io_surf_props().as_ref()];
+ let values: [&cf::Type; 1] = [io_surface_properties.as_ref()];
+ let attrs = cf::Dictionary::with_keys_values(&keys, &values)
+ .ok_or_else(|| anyhow!("pixel buffer attributes"))?;
+ let buf = cv::PixelBuf::new(
+ width as usize,
+ height as usize,
+ cv_format(format),
+ Some(&attrs),
+ )
+ .map_err(|e| anyhow!("pixel buffer {width}x{height} {format:?}: {e:?}"))?;
+ let format_desc = cm::VideoFormatDesc::with_image_buf(&buf)
+ .map_err(|e| anyhow!("format description: {e:?}"))?;
+ let timing = cm::SampleTimingInfo {
+ duration: cm::Time::new(1, fps.max(1) as i32),
+ pts: cm::Time::new(i64::from(index), fps.max(1) as i32),
+ dts: cm::Time::invalid(),
+ };
+ cm::SampleBuf::with_image_buf(&buf, true, None, std::ptr::null(), &format_desc, &timing)
+ .map_err(|e| anyhow!("sample buffer: {e:?}"))
+ }
+
+ pub async fn run_screen(
+ ctx: &Ctx,
+ muxer: &VideoMuxer,
+ script: &VideoScript,
+ ) -> anyhow::Result<Verdict> {
+ let timestamps = Timestamps::now();
+ let (tx, rx) = flume::bounded::<screen_capture::VideoFrame>(64);
+ let output = muxer.output(&ctx.scratch);
+ let builder = OutputPipeline::builder(output.clone())
+ .with_video::<ChannelVideoSource<screen_capture::VideoFrame>>(
+ ChannelVideoSourceConfig::new(script.info, rx),
+ )
+ .with_timestamps(timestamps);
+ let built = match muxer {
+ VideoMuxer::MacFragmented => {
+ builder
+ .build::<MacOSFragmentedM4SMuxer>(MacOSFragmentedM4SMuxerConfig::default())
+ .await
+ }
+ VideoMuxer::MacOop => {
+ builder
+ .build::<OutOfProcessFragmentedM4SMuxer>(
+ OutOfProcessFragmentedM4SMuxerConfig::default(),
+ )
+ .await
+ }
+ _ => {
+ builder
+ .build::<cap_recording::AVFoundationMp4Muxer>(
+ cap_recording::AVFoundationMp4MuxerConfig {
+ output_height: None,
+ instant_mode: false,
+ ultra_quality: false,
+ compatibility_quality: false,
+ },
+ )
+ .await
+ }
+ };
+ let pipeline = match built {
+ Ok(p) => p,
+ Err(e) => return Ok(Verdict::rejected(format!("build: {e:#}"))),
+ };
+ let fps = script.info.fps();
+ let mut tx = Some(tx);
+ for (i, f) in script.frames.iter().enumerate() {
+ if script.pause_at.contains(&i) {
+ pipeline.pause();
+ }
+ if script.resume_at.contains(&i) {
+ pipeline.resume();
+ }
+ if script.vanish_after == Some(i) {
+ tx = None;
+ }
+ if f.wait > 0.0 {
+ tokio::time::sleep(Duration::from_secs_f64(f.wait)).await;
+ }
+ let Some(sender) = &tx else { break };
+ // A size no pixel buffer can have never reaches the recorder.
+ let Ok(sample_buf) = sample(f.format, f.width, f.height, fps, i as u32) else {
+ continue;
+ };
+ let frame = screen_capture::VideoFrame {
+ sample_buf,
+ timestamp: timestamp(f.clock, timestamps),
+ };
+ if sender.send_async(frame).await.is_err() {
+ break;
+ }
+ }
+ tokio::time::sleep(Duration::from_millis(200)).await;
+ let stopped = pipeline.stop().await;
+ drop(tx);
+ match stopped {
+ Err(e) => Ok(Verdict::rejected(format!("stop: {e:#}"))),
+ Ok(_) => Ok(judge_output(&output, script.expect_media)),
+ }
+ }
+}
+
+/// Video scripts every muxer runs in the FAST tier; FULL runs them all.
+const FAST_VIDEO: &[&str] = &[
+ "control",
+ "info-zero-size",
+ "size-changes",
+ "wrong-format",
+ "ts-backwards",
+ "ts-jump-hours",
+ "ts-wallclock-past",
+ "fps-0",
+ "no-frames",
+ "device-vanishes",
+];
+
+/// Audio scripts every muxer but Ogg runs in the FAST tier.
+const FAST_AUDIO: &[&str] = &[
+ "control",
+ "channels-0",
+ "channels-3",
+ "format-changes",
+ "empty-buffers",
+ "odd-lengths",
+ "nan",
+ "ts-backwards",
+];
+
+type NamedMuxer = (&'static str, fn() -> VideoMuxer);
+
+fn video_probes(out: &mut Vec<Probe>) {
+ let ffmpeg_muxers: [NamedMuxer; 2] = [
+ ("mp4", || VideoMuxer::Mp4),
+ ("segmented", || VideoMuxer::Segmented),
+ ];
+ for (muxer_name, muxer) in ffmpeg_muxers {
+ for (name, full, script) in video_scripts(Pixel::BGRA, Pixel::YUV420P) {
+ let probe = Probe::new(
+ format!("chaos/record/video/{muxer_name}/{name}"),
+ 60,
+ move |ctx| {
+ let muxer = muxer();
+ ctx.block_on(run_ffmpeg_video(ctx, &muxer, &script))
+ },
+ );
+ out.push(if full || !FAST_VIDEO.contains(&name) {
+ probe.full()
+ } else {
+ probe
+ });
+ }
+ }
+ #[cfg(target_os = "macos")]
+ {
+ let mac_muxers: [NamedMuxer; 3] = [
+ ("mac-fragmented", || VideoMuxer::MacFragmented),
+ ("mac-oop", || VideoMuxer::MacOop),
+ ("mac-avf-mp4", || VideoMuxer::MacAvfMp4),
+ ];
+ for (muxer_name, muxer) in mac_muxers {
+ for (name, full, script) in video_scripts(Pixel::NV12, Pixel::BGRA) {
+ let full = full || !FAST_VIDEO.contains(&name);
+ let probe = Probe::new(
+ format!("chaos/record/video/{muxer_name}/{name}"),
+ 60,
+ move |ctx| {
+ let muxer = muxer();
+ ctx.block_on(mac::run_screen(ctx, &muxer, &script))
+ },
+ );
+ out.push(if full { probe.full() } else { probe });
+ }
+ }
+ for format in [
+ Pixel::NV12,
+ Pixel::NV21,
+ Pixel::BGRA,
+ Pixel::UYVY422,
+ Pixel::YUYV422,
+ Pixel::P010LE,
+ ] {
+ for (w, h) in [(1, 1), (2, 2), (641, 361), (3, 1080), (1920, 1)] {
+ let id = format!(
+ "chaos/record/camera-convert/{}/{w}x{h}",
+ format!("{format:?}").to_lowercase()
+ );
+ out.push(Probe::new(id, 30, move |_ctx| {
+ let Ok(sample) = mac::sample(format, w, h, 30, 0) else {
+ return Ok(Verdict::Ok("no pixel buffer of this shape".into()));
+ };
+ Ok(match cap_camera_ffmpeg::sample_buf_as_ffmpeg(&sample) {
+ Ok(frame) => Verdict::Ok(format!(
+ "{}x{} {:?}",
+ frame.width(),
+ frame.height(),
+ frame.format()
+ )),
+ Err(e) => Verdict::rejected(e),
+ })
+ }));
+ }
+ }
+ }
+}
+
+/// One hostile audio buffer.
+#[derive(Clone, Debug)]
+pub struct AFrame {
+ pub format: Sample,
+ pub rate: u32,
+ pub channels: u16,
+ pub samples: usize,
+ pub fill: Fill,
+ pub clock: Clock,
+ pub wait: f64,
+}
+
+#[derive(Clone, Copy, Debug)]
+pub enum Fill {
+ Tone,
+ Nan,
+ Inf,
+ Huge,
+}
+
+#[derive(Clone, Debug)]
+pub struct AudioScript {
+ pub info: AudioInfo,
+ pub frames: Vec<AFrame>,
+ pub expect_media: bool,
+}
+
+const F32: Sample = Sample::F32(Type::Packed);
+
+fn chunks(format: Sample, rate: u32, channels: u16, secs: f64, chunk: f64) -> Vec<AFrame> {
+ let n = (secs / chunk).round() as usize;
+ let samples = ((f64::from(rate) * chunk).round() as usize).max(1);
+ (0..n)
+ .map(|i| AFrame {
+ format,
+ rate,
+ channels,
+ samples,
+ fill: Fill::Tone,
+ clock: Clock::Host(0.05 + i as f64 * chunk),
+ wait: chunk,
+ })
+ .collect()
+}
+
+pub fn audio_scripts() -> Vec<(&'static str, AudioScript)> {
+ let ok = |name, frames| {
+ (
+ name,
+ AudioScript {
+ info: AudioInfo::new_raw(F32, 48_000, 1),
+ frames,
+ expect_media: true,
+ },
+ )
+ };
+ let mut out = vec![ok("control", chunks(F32, 48_000, 1, 1.0, 0.02))];
+ for (name, rate) in [
+ ("rate-0", 0),
+ ("rate-1", 1),
+ ("rate-8000", 8000),
+ ("rate-44101", 44_101),
+ ("rate-384000", 384_000),
+ ] {
+ out.push((
+ name,
+ AudioScript {
+ info: AudioInfo::new_raw(F32, rate, 1),
+ frames: chunks(F32, rate, 1, 1.0, 0.02),
+ expect_media: rate >= 8000,
+ },
+ ));
+ }
+ for (name, channels) in [
+ ("channels-0", 0u16),
+ ("channels-3", 3),
+ ("channels-8", 8),
+ ("channels-32", 32),
+ ] {
+ out.push((
+ name,
+ AudioScript {
+ info: AudioInfo::new_raw(F32, 48_000, channels),
+ frames: chunks(F32, 48_000, channels, 1.0, 0.02),
+ expect_media: channels > 0,
+ },
+ ));
+ }
+ let mut empty = chunks(F32, 48_000, 1, 0.4, 0.02);
+ for f in &mut empty {
+ f.samples = 0;
+ }
+ out.push(ok("empty-buffers", empty));
+ let mut huge = chunks(F32, 48_000, 1, 0.2, 0.02);
+ huge[0].samples = 48_000 * 20;
+ out.push(ok("huge-buffer", huge));
+ let mut odd = chunks(F32, 48_000, 1, 0.6, 0.02);
+ for (i, f) in odd.iter_mut().enumerate() {
+ f.samples = [1, 7, 333, 1023, 2][i % 5];
+ }
+ out.push(ok("odd-lengths", odd));
+ for (name, fill) in [
+ ("nan", Fill::Nan),
+ ("inf", Fill::Inf),
+ ("huge-values", Fill::Huge),
+ ] {
+ let mut frames = chunks(F32, 48_000, 1, 0.6, 0.02);
+ for f in frames.iter_mut().skip(5) {
+ f.fill = fill;
+ }
+ out.push(ok(name, frames));
+ }
+ let mut change = chunks(F32, 48_000, 1, 0.3, 0.02);
+ change.extend(chunks(Sample::I16(Type::Packed), 48_000, 1, 0.3, 0.02));
+ change.extend(chunks(F32, 44_100, 2, 0.3, 0.02));
+ change.extend(chunks(Sample::F32(Type::Planar), 48_000, 2, 0.3, 0.02));
+ change.extend(chunks(Sample::U8(Type::Packed), 8_000, 1, 0.3, 0.02));
+ for (i, f) in change.iter_mut().enumerate() {
+ f.clock = Clock::Host(0.05 + i as f64 * 0.02);
+ }
+ out.push(ok("format-changes", change));
+ let mut backwards = chunks(F32, 48_000, 1, 0.5, 0.02);
+ for (i, f) in backwards.iter_mut().enumerate() {
+ f.clock = Clock::Host(1.0 - i as f64 * 0.02);
+ }
+ out.push(ok("ts-backwards", backwards));
+ let mut jump = chunks(F32, 48_000, 1, 0.5, 0.02);
+ for (i, f) in jump.iter_mut().enumerate().skip(10) {
+ f.clock = Clock::Host(2.0 * 3600.0 + i as f64 * 0.02);
+ }
+ out.push(ok("ts-jump-hours", jump));
+ let mut before = chunks(F32, 48_000, 1, 0.5, 0.02);
+ for (i, f) in before.iter_mut().enumerate() {
+ f.clock = Clock::Host(-30.0 + i as f64 * 0.02);
+ }
+ out.push(ok("ts-before-epoch", before));
+ let mut wall = chunks(F32, 48_000, 1, 0.5, 0.02);
+ for (i, f) in wall.iter_mut().enumerate() {
+ f.clock = Clock::System(-60.0 + i as f64 * 0.02);
+ }
+ out.push(ok("ts-wallclock-past", wall));
+ let mut repeat = chunks(F32, 48_000, 1, 0.5, 0.02);
+ for f in &mut repeat {
+ f.clock = Clock::Host(0.1);
+ }
+ out.push(ok("ts-repeat", repeat));
+ let mut stall = chunks(F32, 48_000, 1, 0.4, 0.02);
+ stall[10].wait = 4.0;
+ for (i, f) in stall.iter_mut().enumerate().skip(10) {
+ f.clock = Clock::Host(4.2 + i as f64 * 0.02);
+ }
+ out.push(ok("stall", stall));
+ let mut burst = chunks(F32, 48_000, 1, 2.0, 0.02);
+ for f in &mut burst {
+ f.wait = 0.0;
+ }
+ out.push(ok("burst", burst));
+ out.push((
+ "no-frames",
+ AudioScript {
+ info: AudioInfo::new_raw(F32, 48_000, 1),
+ frames: Vec::new(),
+ expect_media: false,
+ },
+ ));
+ out
+}
+
+fn audio_frame(f: &AFrame, index: usize) -> ffmpeg::frame::Audio {
+ let layout = ffmpeg::util::channel_layout::ChannelLayout::default(i32::from(f.channels));
+ let mut frame = ffmpeg::frame::Audio::new(f.format, f.samples, layout);
+ frame.set_rate(f.rate);
+ let allocated = unsafe { !(*frame.as_ptr()).data[0].is_null() };
+ if !allocated {
+ return frame;
+ }
+ let value = |n: usize| -> f32 {
+ match f.fill {
+ Fill::Tone => (0.4 * ((index * f.samples + n) as f64 * 0.0577).sin()) as f32,
+ Fill::Nan => f32::NAN,
+ Fill::Inf => {
+ if n % 2 == 0 {
+ f32::INFINITY
+ } else {
+ f32::NEG_INFINITY
+ }
+ }
+ Fill::Huge => 1e30,
+ }
+ };
+ for plane in 0..frame.planes() {
+ let data = frame.data_mut(plane);
+ match f.format {
+ Sample::F32(_) => {
+ for (n, chunk) in data.chunks_exact_mut(4).enumerate() {
+ chunk.copy_from_slice(&value(n).to_ne_bytes());
+ }
+ }
+ Sample::I16(_) => {
+ for (n, chunk) in data.chunks_exact_mut(2).enumerate() {
+ let v = (value(n).clamp(-1.0, 1.0) * 32767.0) as i16;
+ chunk.copy_from_slice(&v.to_ne_bytes());
+ }
+ }
+ _ => {
+ for (n, byte) in data.iter_mut().enumerate() {
+ *byte = (n % 251) as u8;
+ }
+ }
+ }
+ }
+ frame
+}
+
+#[derive(Clone, Copy)]
+pub enum AudioMuxerKind {
+ Ogg,
+ FragmentedM4a,
+ Dash,
+ /// Two sources mixed into the Instant muxer.
+ Mixed,
+}
+
+async fn run_audio(
+ ctx: &Ctx,
+ muxer: AudioMuxerKind,
+ script: &AudioScript,
+) -> anyhow::Result<Verdict> {
+ use futures::SinkExt;
+ let timestamps = Timestamps::now();
+ let output = match muxer {
+ AudioMuxerKind::Ogg => ctx.path("audio.ogg"),
+ AudioMuxerKind::FragmentedM4a => ctx.path("audio.m4a"),
+ AudioMuxerKind::Dash | AudioMuxerKind::Mixed => ctx.path("audio"),
+ };
+ let (tx, rx) = futures::channel::mpsc::channel::<AudioFrame>(64);
+ let mut builder = OutputPipeline::builder(output.clone())
+ .with_audio_source::<ChannelAudioSource>(ChannelAudioSourceConfig::new(script.info, rx))
+ .with_timestamps(timestamps);
+ let mut second = None;
+ if matches!(muxer, AudioMuxerKind::Mixed) {
+ let (tx2, rx2) = futures::channel::mpsc::channel::<AudioFrame>(64);
+ let stereo = AudioInfo::new_raw(F32, 48_000, 2);
+ builder = builder
+ .with_audio_source::<ChannelAudioSource>(ChannelAudioSourceConfig::new(stereo, rx2));
+ second = Some(tx2);
+ }
+ let built = match muxer {
+ AudioMuxerKind::Ogg => builder.build::<OggMuxer>(()).await,
+ AudioMuxerKind::FragmentedM4a => {
+ builder
+ .build::<FragmentedAudioMuxer>(FragmentedAudioMuxerConfig {
+ shared_pause_state: None,
+ })
+ .await
+ }
+ AudioMuxerKind::Dash | AudioMuxerKind::Mixed => {
+ builder
+ .build::<DashSegmentedAudioMuxer>(DashSegmentedAudioMuxerConfig::default())
+ .await
+ }
+ };
+ let pipeline = match built {
+ Ok(p) => p,
+ Err(e) => return Ok(Verdict::rejected(format!("build: {e:#}"))),
+ };
+ let mut tx = tx;
+ for (i, f) in script.frames.iter().enumerate() {
+ if f.wait > 0.0 {
+ tokio::time::sleep(Duration::from_secs_f64(f.wait)).await;
+ }
+ let at = timestamp(f.clock, timestamps);
+ if let Some(second) = &mut second {
+ let steady = AFrame {
+ format: F32,
+ rate: 48_000,
+ channels: 2,
+ samples: 960,
+ fill: Fill::Tone,
+ clock: Clock::Host(0.05 + i as f64 * 0.02),
+ wait: 0.0,
+ };
+ let _ = second
+ .send(AudioFrame::new(
+ audio_frame(&steady, i),
+ timestamp(steady.clock, timestamps),
+ ))
+ .await;
+ }
+ if tx
+ .send(AudioFrame::new(audio_frame(f, i), at))
+ .await
+ .is_err()
+ {
+ break;
+ }
+ }
+ tokio::time::sleep(Duration::from_millis(300)).await;
+ let stopped = pipeline.stop().await;
+ drop(tx);
+ drop(second);
+ match stopped {
+ Err(e) => Ok(Verdict::rejected(format!("stop: {e:#}"))),
+ Ok(_) => Ok(judge_output(&output, script.expect_media)),
+ }
+}
+
+fn audio_probes(out: &mut Vec<Probe>) {
+ for (muxer_name, muxer) in [
+ ("ogg", AudioMuxerKind::Ogg),
+ ("fragmented-m4a", AudioMuxerKind::FragmentedM4a),
+ ("dash", AudioMuxerKind::Dash),
+ ("mixed", AudioMuxerKind::Mixed),
+ ] {
+ for (name, script) in audio_scripts() {
+ let full = !matches!(muxer, AudioMuxerKind::Ogg) && !FAST_AUDIO.contains(&name);
+ let probe = Probe::new(
+ format!("chaos/record/audio/{muxer_name}/{name}"),
+ 60,
+ move |ctx| ctx.block_on(run_audio(ctx, muxer, &script)),
+ );
+ out.push(if full { probe.full() } else { probe });
+ }
+ }
+}
+
+/// Raw microphone callbacks through the real feed and `sources::Microphone`
+/// (resampling to the recording format) into the Studio mic muxer.
+#[derive(Clone, Debug)]
+pub struct MicScript {
+ pub rate: u32,
+ pub channels: u16,
+ /// (bytes, format, rate, channels) per callback.
+ pub callbacks: Vec<MicCallback>,
+ pub drop_feed_after: Option<usize>,
+ pub expect_media: bool,
+}
+
+#[derive(Clone, Debug)]
+pub struct MicCallback {
+ pub bytes: usize,
+ pub format: cpal::SampleFormat,
+ pub rate: u32,
+ pub channels: u16,
+ pub fill: Fill,
+}
+
+fn mic_callbacks(rate: u32, channels: u16, secs: f64) -> Vec<MicCallback> {
+ let per = ((f64::from(rate) * 0.01).round() as usize).max(1);
+ (0..(secs / 0.01) as usize)
+ .map(|_| MicCallback {
+ bytes: per * usize::from(channels.max(1)) * 4,
+ format: cpal::SampleFormat::F32,
+ rate,
+ channels,
+ fill: Fill::Tone,
+ })
+ .collect()
+}
+
+pub fn mic_scripts() -> Vec<(&'static str, MicScript)> {
+ let base = |name, rate: u32, channels: u16, expect_media| {
+ (
+ name,
+ MicScript {
+ rate,
+ channels,
+ callbacks: mic_callbacks(rate, channels, 0.8),
+ drop_feed_after: None,
+ expect_media,
+ },
+ )
+ };
+ let mut out = vec![
+ base("control", 48_000, 1, true),
+ base("rate-0", 0, 1, false),
+ base("rate-1", 1, 1, false),
+ base("rate-8000", 8_000, 1, true),
+ base("rate-44101", 44_101, 2, true),
+ base("rate-384000", 384_000, 2, true),
+ base("channels-0", 48_000, 0, false),
+ base("channels-3", 48_000, 3, true),
+ base("channels-8", 48_000, 8, true),
+ base("channels-32", 48_000, 32, true),
+ ];
+ let mut odd = base("odd-byte-lengths", 48_000, 2, true);
+ for (i, c) in odd.1.callbacks.iter_mut().enumerate() {
+ c.bytes = [1, 3, 5, 7, 4, 6, 9, 0][i % 8];
+ }
+ out.push(odd);
+ let mut empty = base("empty-callbacks", 48_000, 1, false);
+ for c in &mut empty.1.callbacks {
+ c.bytes = 0;
+ }
+ out.push(empty);
+ let mut huge = base("huge-callback", 48_000, 1, true);
+ huge.1.callbacks[0].bytes = 48_000 * 4 * 30;
+ out.push(huge);
+ for (name, fill) in [("nan", Fill::Nan), ("inf", Fill::Inf)] {
+ let mut s = base(name, 48_000, 1, true);
+ for c in &mut s.1.callbacks {
+ c.fill = fill;
+ }
+ out.push(s);
+ }
+ let mut change = base("format-changes", 48_000, 1, true);
+ for (i, c) in change.1.callbacks.iter_mut().enumerate() {
+ match (i / 10) % 5 {
+ 1 => {
+ c.format = cpal::SampleFormat::I16;
+ c.bytes /= 2;
+ }
+ 2 => {
+ c.rate = 44_100;
+ c.channels = 2;
+ c.bytes = 441 * 2 * 4;
+ }
+ 3 => {
+ c.format = cpal::SampleFormat::U8;
+ c.bytes /= 4;
+ }
+ 4 => {
+ c.format = cpal::SampleFormat::I32;
+ c.channels = 6;
+ c.bytes = 480 * 6 * 4;
+ }
+ _ => {}
+ }
+ }
+ out.push(change);
+ let mut vanish = base("device-vanishes", 48_000, 1, true);
+ vanish.1.drop_feed_after = Some(20);
+ out.push(vanish);
+ out
+}
+
+fn mic_bytes(c: &MicCallback, index: usize) -> Vec<u8> {
+ let mut out = Vec::with_capacity(c.bytes);
+ let mut n = 0usize;
+ while out.len() < c.bytes {
+ let v = match c.fill {
+ Fill::Tone => (0.3 * ((index * 4096 + n) as f64 * 0.05).sin()) as f32,
+ Fill::Nan => f32::NAN,
+ Fill::Inf => f32::INFINITY,
+ Fill::Huge => 1e30,
+ };
+ match c.format {
+ cpal::SampleFormat::F32 => out.extend(v.to_ne_bytes()),
+ cpal::SampleFormat::I16 => out.extend(((v * 32767.0) as i16).to_ne_bytes()),
+ cpal::SampleFormat::I32 => out.extend(((v * 2.1e9) as i32).to_ne_bytes()),
+ _ => out.push((v * 127.0 + 128.0) as u8),
+ }
+ n += 1;
+ }
+ out.truncate(c.bytes);
+ out
+}
+
+async fn run_mic(ctx: &Ctx, script: &MicScript, fragmented: bool) -> anyhow::Result<Verdict> {
+ use cap_recording::feeds::microphone::synthetic::SyntheticMicrophone;
+ let mic = SyntheticMicrophone::spawn("Cap Chaos Microphone", script.rate, script.channels);
+ let lock = match mic.lock().await {
+ Ok(lock) => Arc::new(lock),
+ Err(e) => return Ok(Verdict::rejected(format!("lock: {e:#}"))),
+ };
+ let timestamps = Timestamps::now();
+ let output = if fragmented {
+ ctx.path("audio-input.m4a")
+ } else {
+ ctx.path("audio-input.ogg")
+ };
+ let builder = OutputPipeline::builder(output.clone())
+ .with_audio_source::<cap_recording::sources::Microphone>(lock.clone())
+ .with_timestamps(timestamps);
+ let built = if fragmented {
+ builder
+ .build::<FragmentedAudioMuxer>(FragmentedAudioMuxerConfig {
+ shared_pause_state: None,
+ })
+ .await
+ } else {
+ builder.build::<OggMuxer>(()).await
+ };
+ let pipeline = match built {
+ Ok(p) => p,
+ Err(e) => return Ok(Verdict::rejected(format!("build: {e:#}"))),
+ };
+ let mut mic = Some(mic);
+ for (i, c) in script.callbacks.iter().enumerate() {
+ if script.drop_feed_after == Some(i) {
+ mic = None;
+ }
+ let Some(feed) = &mic else { break };
+ tokio::time::sleep(Duration::from_millis(10)).await;
+ feed.push_raw(
+ mic_bytes(c, i),
+ c.format,
+ c.rate,
+ c.channels,
+ host(timestamps, 0.05 + i as f64 * 0.01),
+ )
+ .await;
+ }
+ tokio::time::sleep(Duration::from_millis(300)).await;
+ let stopped = pipeline.stop().await;
+ drop(mic);
+ drop(lock);
+ match stopped {
+ Err(e) => Ok(Verdict::rejected(format!("stop: {e:#}"))),
+ Ok(_) => Ok(judge_output(&output, script.expect_media)),
+ }
+}
+
+fn mic_probes(out: &mut Vec<Probe>) {
+ for (muxer, fragmented) in [("ogg", false), ("fragmented-m4a", true)] {
+ for (name, script) in mic_scripts() {
+ let probe = Probe::new(format!("chaos/record/mic/{muxer}/{name}"), 60, move |ctx| {
+ ctx.block_on(run_mic(ctx, &script, fragmented))
+ });
+ out.push(
+ if fragmented && name != "control" && name != "format-changes" {
+ probe.full()
+ } else {
+ probe
+ },
+ );
+ }
+ }
+}
+
+pub fn probes() -> Vec<Probe> {
+ let mut out = Vec::new();
+ video_probes(&mut out);
+ audio_probes(&mut out);
+ mic_probes(&mut out);
+ super::actors::probes(&mut out);
+ out
+}
+
+#[cfg(target_os = "macos")]
+pub use mac::sample as mac_sample;
diff --git a/scripts/verify/run.sh b/scripts/verify/run.sh
index e5145ffcb..6efcef569 100755
--- a/scripts/verify/run.sh
+++ b/scripts/verify/run.sh
@@ -104,9 +104,10 @@ if [[ "$mode" == list ]]; then
exit 0
fi
-# The crash suite spawns the out-of-process muxer; build it with the same
-# profile so it sits beside the test binaries (target/<profile>/cap-muxer).
-if [[ " ${suite_names//,/ } " == *" crash "* && "$mode" != archive ]]; then
+# The crash and chaos suites spawn the out-of-process muxer; build it with
+# the same profile so it sits beside the test binaries
+# (target/<profile>/cap-muxer).
+if [[ ( " ${suite_names//,/ } " == *" crash "* || " ${suite_names//,/ } " == *" chaos "* ) && "$mode" != archive ]]; then
cargo build --profile "$profile" -p cap-muxer ${CAP_VERIFY_LOCKED:+--locked}
fi
diff --git a/scripts/verify/suites.json b/scripts/verify/suites.json
index de61d65e3..93e4c9073 100644
--- a/scripts/verify/suites.json
+++ b/scripts/verify/suites.json
@@ -142,6 +142,27 @@
}
}
},
+ {
+ "name": "chaos",
+ "about": "chaos and resilience: hostile device data, files, projects and settings through the recording, editor, playback, export and recovery engines, each probe in a child process so panics, aborts, signals, Objective-C exceptions and hangs are classified (needs the cap-muxer binary, built by run.sh)",
+ "cargo": ["-p", "cap-verify", "--test", "chaos"],
+ "binaries": ["cap-verify::chaos"],
+ "platforms": {
+ "fast": ["macos"],
+ "full": ["macos"]
+ },
+ "roots": ["cap-verify", "cap-muxer"],
+ "own": ["crates/verify/tests/chaos.rs", "crates/verify/tests/chaos_support/**"],
+ "cases": {
+ "prefix": "chaos/",
+ "fast": {
+ "macos": 0
+ },
+ "full": {
+ "macos": 0
+ }
+ }
+ },
{
"name": "audio",
"about": "export and offline playback audio against sample-accurate markers (mic, system audio, cuts, speeds, mute, volume, A/V offset)",