Open source Loom alternative. Beautiful, shareable screen recordings.
Rust appcapcossloommacnextjsnextjs15open-sourceossreactrecordrustscreen-capturescreen-recorderscreenshotsolidjstauritauri-apptypescriptvite
test(verify): real-capture recording oracle with stimulus helper and synthetic camera/mic feeds
Richie McIlroycab335b9988f
diff --git a/.github/workflows/verify-recording.yml b/.github/workflows/verify-recording.ymlnew file mode 100644index 000000000..ca66774a5--- /dev/null+++ b/.github/workflows/verify-recording.yml@@ -0,0 +1,99 @@+name: Verify recording (real capture)++# Records a marker stimulus through Cap's real Studio and Instant pipelines+# (ScreenCaptureKit + VideoToolbox) on hosted macOS runners and checks every+# frame and audio marker against the host-clock truth. See+# crates/verify/tests/recording.rs.++on:+ workflow_dispatch:+ inputs:+ repeat:+ description: "Runs of each case"+ default: "1"+ tier:+ description: "fast or full"+ default: "fast"+ push:+ branches:+ - ci/verify-recording+ paths:+ - "crates/verify/**"+ - "crates/recording/**"+ - ".github/workflows/verify-recording.yml"++concurrency:+ group: verify-recording-${{ github.ref_name }}+ cancel-in-progress: true++permissions:+ contents: read++jobs:+ real-capture:+ strategy:+ fail-fast: false+ matrix:+ runner: [macos-15, macos-26]+ runs-on: ${{ matrix.runner }}+ timeout-minutes: 45+ env:+ CAP_VERIFY_CAPTURE: display+ CAP_VERIFY_REQUIRE_CAPTURE: "1"+ CAP_VERIFY_TIER: ${{ inputs.tier || 'fast' }}+ CAP_VERIFY_REPEAT: ${{ inputs.repeat || '1' }}+ CAP_VERIFY_SCRATCH: ${{ github.workspace }}/verify-scratch+ CAP_VERIFY_REPORT_DIR: ${{ github.workspace }}/verify-report+ CAP_VERIFY_DUMP: ${{ github.workspace }}/verify-dump+ steps:+ - uses: actions/checkout@v4++ - uses: dtolnay/[email protected]++ - uses: ./.github/actions/setup-rust-cache+ with:+ target: aarch64-apple-darwin+ prefix-key: v1-rust-verify-recording+ save-cache: "true"++ - uses: ./.github/actions/install-desktop-deps++ - uses: actions/setup-node@v4+ with:+ node-version: 24++ - name: Native dependencies+ env:+ RUST_TARGET_TRIPLE: aarch64-apple-darwin+ run: node scripts/setup.js++ - name: Runner devices+ run: |+ sw_vers+ system_profiler SPDisplaysDataType SPAudioDataType 2>/dev/null | sed -n 1,80p+ sysctl hw.memsize hw.ncpu++ - name: Build+ run: |+ start=$(date +%s)+ cargo test --locked -p cap-verify --test recording --no-run+ echo "build seconds: $(( $(date +%s) - start ))"++ - name: Real-capture recording oracle+ run: |+ mkdir -p "$CAP_VERIFY_DUMP"+ start=$(date +%s)+ cargo test --locked -p cap-verify --test recording -- --nocapture+ echo "test seconds: $(( $(date +%s) - start ))"++ - name: Upload artifacts+ if: always()+ uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4.6.2+ with:+ name: verify-recording-${{ matrix.runner }}+ path: |+ ${{ github.workspace }}/verify-report+ ${{ github.workspace }}/verify-dump+ ${{ github.workspace }}/verify-scratch/**/stimulus.jsonl+ ${{ github.workspace }}/verify-scratch/**/recording-meta.json+ if-no-files-found: ignorediff --git a/Cargo.lock b/Cargo.lockindex c4fa7253a..b666152b0 100644--- a/Cargo.lock+++ b/Cargo.lock@@ -2164,16 +2164,21 @@ dependencies = ["cap-rendering","cap-timestamp","cidre",+ "cpal 0.15.3 (git+https://github.com/CapSoftware/cpal?rev=6013cb5f8bd3)","ffmpeg-next","flume","futures","relative-path","rustfft",+ "scap-targets","serde","serde_json",+ "softbuffer","tempfile","tokio","toml 0.8.2",+ "tracing-subscriber",+ "winit","workspace-hack",]diff --git a/crates/recording/src/feeds/camera.rs b/crates/recording/src/feeds/camera.rsindex 28d822b4b..7907c2573 100644--- a/crates/recording/src/feeds/camera.rs+++ b/crates/recording/src/feeds/camera.rs@@ -1828,6 +1828,68 @@ impl Message<Unlock> for CameraFeed {}}+/// Test-only camera feeds driven by a synthetic device instead of hardware,+/// so recording tests exercise the real feed, lock and pipeline paths.+#[cfg(feature = "test-utils")]+pub mod synthetic {+ use super::*;++ pub struct SyntheticCamera {+ feed: ActorRef<CameraFeed>,+ _done_rx: std::sync::Mutex<mpsc::Receiver<()>>,+ }++ impl SyntheticCamera {+ pub fn spawn(name: &str, video_info: VideoInfo) -> Self {+ let (done_tx, done_rx) = mpsc::sync_channel(1);+ let id = DeviceOrModelID::DeviceID(name.to_string());+ let camera_info = serde_json::from_value(serde_json::json!({+ "device_id": name,+ "model_id": null,+ "display_name": name+ }))+ .expect("synthetic camera info");+ let attached = AttachedState::new(+ id.clone(),+ InputConnected {+ generation: 0,+ id,+ done_tx,+ camera_info,+ video_info,+ settings: None,+ },+ );+ let feed = CameraFeed::spawn(CameraFeed {+ state: State::Open(OpenState {+ connecting: None,+ attached: Some(attached),+ }),+ ..CameraFeed::default()+ });+ Self {+ feed,+ _done_rx: std::sync::Mutex::new(done_rx),+ }+ }++ pub async fn lock(&self) -> anyhow::Result<CameraFeedLock> {+ self.feed+ .ask(Lock)+ .await+ .map_err(|error| anyhow::anyhow!("synthetic camera lock: {error}"))+ }++ pub async fn push_native(&self, frame: NativeCameraFrame) {+ let _ = self.feed.tell(NewNativeFrame(frame)).await;+ }++ pub async fn push(&self, frame: FFmpegVideoFrame) {+ let _ = self.feed.tell(NewFrame(frame)).await;+ }+ }+}+#[cfg(test)]mod tests {use super::*;diff --git a/crates/recording/src/feeds/microphone.rs b/crates/recording/src/feeds/microphone.rsindex 73cea2dd9..92236884f 100644--- a/crates/recording/src/feeds/microphone.rs+++ b/crates/recording/src/feeds/microphone.rs@@ -3,7 +3,7 @@ use cap_audio::estimate_input_latency;use cap_media_info::{AudioInfo, ffmpeg_sample_format_for};use cap_timestamp::Timestamp;use cpal::{- BufferSize, Device, InputCallbackInfo, SampleFormat, StreamError, SupportedStreamConfig,+ BufferSize, Device, SampleFormat, StreamError, SupportedStreamConfig,SupportedStreamConfigRange,traits::{DeviceTrait, HostTrait, StreamTrait},};@@ -159,7 +159,6 @@ pub struct MicrophoneSamples {pub format: SampleFormat,pub sample_rate: u32,pub channels: u16,- pub info: InputCallbackInfo,pub timestamp: Timestamp,}@@ -1419,7 +1418,6 @@ impl MicrophoneFeed {format: data.sample_format(),sample_rate: effective_sample_rate.sample_rate,channels: callback_channels,- info: info.clone(),timestamp: timestamp - capture_latency,};@@ -3076,6 +3074,78 @@ impl Message<Unlock> for MicrophoneFeed {}}+/// Test-only microphone feeds driven by a synthetic device instead of+/// hardware, so recording tests exercise the real feed, lock and pipeline+/// paths with samples whose capture times are known exactly.+#[cfg(feature = "test-utils")]+pub mod synthetic {+ use super::*;++ const STREAM_ID: u32 = 1;++ pub struct SyntheticMicrophone {+ feed: ActorRef<MicrophoneFeed>,+ sample_rate: u32,+ channels: u16,+ _done_rx: std::sync::Mutex<mpsc::Receiver<()>>,+ }++ impl SyntheticMicrophone {+ /// A feed attached to an f32 input device named `name`.+ pub fn spawn(name: &str, sample_rate: u32, channels: u16) -> Self {+ let (done_tx, done_rx) = mpsc::sync_channel(1);+ let (error_tx, _) = flume::unbounded();+ let mut feed = MicrophoneFeed::new(error_tx);+ feed.input_id_counter = STREAM_ID;+ feed.state = State::Open(OpenState {+ connecting: None,+ attached: Some(AttachedState {+ id: STREAM_ID,+ label: name.to_string(),+ config: SupportedStreamConfig::new(+ channels,+ cpal::SampleRate(sample_rate),+ cpal::SupportedBufferSize::Unknown,+ SampleFormat::F32,+ ),+ buffer_size_frames: None,+ done_tx,+ }),+ });+ Self {+ feed: MicrophoneFeed::spawn(feed),+ sample_rate,+ channels,+ _done_rx: std::sync::Mutex::new(done_rx),+ }+ }++ pub async fn lock(&self) -> anyhow::Result<MicrophoneFeedLock> {+ self.feed+ .ask(Lock)+ .await+ .map_err(|error| anyhow::anyhow!("synthetic microphone lock: {error}"))+ }++ /// Delivers interleaved f32 samples whose first frame was captured at+ /// `timestamp`.+ pub async fn push(&self, samples: &[f32], timestamp: Timestamp) {+ let data = samples.iter().flat_map(|s| s.to_ne_bytes()).collect();+ let _ = self+ .feed+ .tell(MicrophoneSamples {+ stream_id: STREAM_ID,+ data,+ format: SampleFormat::F32,+ sample_rate: self.sample_rate,+ channels: self.channels,+ timestamp,+ })+ .await;+ }+ }+}+#[cfg(test)]mod tests {use super::*;diff --git a/crates/verify/Cargo.toml b/crates/verify/Cargo.tomlindex b0da67135..e662cade2 100644--- a/crates/verify/Cargo.toml+++ b/crates/verify/Cargo.toml@@ -28,9 +28,22 @@ workspace-hack = { version = "0.1", path = "../workspace-hack" }[target.'cfg(target_os = "macos")'.dependencies]cap-enc-avfoundation = { path = "../enc-avfoundation" }cidre = { workspace = true }+cpal = { workspace = true }+softbuffer = "0.4"+winit = "0.30"[dev-dependencies]+cap-recording = { path = "../recording", features = ["test-utils"] }cap-editor = { path = "../editor" }cap-export = { path = "../export" }cap-rendering = { path = "../rendering" }tempfile = "3"+tracing-subscriber = { version = "0.3.19", features = ["env-filter"] }++[target.'cfg(target_os = "macos")'.dev-dependencies]+scap-targets = { path = "../scap-targets" }++[[bin]]+name = "cap-verify-stimulus"+path = "src/bin/stimulus.rs"+test = falsediff --git a/crates/verify/src/bin/stimulus.rs b/crates/verify/src/bin/stimulus.rsnew file mode 100644index 000000000..532e3def5--- /dev/null+++ b/crates/verify/src/bin/stimulus.rs@@ -0,0 +1,495 @@+//! `cap-verify-stimulus <spec json> <log path>`: see `cap_verify::stimulus`.++#[cfg(not(target_os = "macos"))]+fn main() {+ eprintln!("cap-verify-stimulus only runs on macOS");+ std::process::exit(2);+}++#[cfg(target_os = "macos")]+fn main() {+ if let Err(e) = app::run() {+ println!("error {e}");+ std::process::exit(1);+ }+}++#[cfg(target_os = "macos")]+mod app {+ use std::{+ fs::File,+ io::{BufRead, Write},+ num::NonZeroU32,+ path::PathBuf,+ sync::{+ Arc, Mutex,+ atomic::{AtomicU64, Ordering},+ mpsc,+ },+ time::{Duration, Instant},+ };++ use cap_verify::{+ audio_code::{self, AudioSource, MarkerPlacement},+ stimulus::{StimulusEvent, StimulusSpec, host_add_secs, host_now, host_secs},+ video_code::{self, FrameMarker, PixelOrder, StreamKind},+ };+ use cpal::traits::{DeviceTrait, HostTrait, StreamTrait};+ use winit::{+ application::ApplicationHandler,+ dpi::{LogicalPosition, LogicalSize},+ event::WindowEvent,+ event_loop::{ActiveEventLoop, ControlFlow, EventLoop, EventLoopProxy},+ window::{Fullscreen, Window, WindowId, WindowLevel},+ };++ #[derive(Debug)]+ enum Command {+ Go,+ Quit,+ }++ struct Log {+ file: Mutex<File>,+ }++ impl Log {+ fn write(&self, event: &StimulusEvent) {+ if let Ok(line) = serde_json::to_string(event)+ && let Ok(mut f) = self.file.lock()+ {+ let _ = writeln!(f, "{line}");+ let _ = f.flush();+ }+ }+ }++ fn say(word: &str) {+ let mut out = std::io::stdout();+ let _ = writeln!(out, "{word}");+ let _ = out.flush();+ }++ pub fn run() -> anyhow::Result<()> {+ let mut args = std::env::args().skip(1);+ let spec: StimulusSpec = serde_json::from_str(&args.next().unwrap_or_default())?;+ let log_path = PathBuf::from(args.next().unwrap_or_default());+ let log = Arc::new(Log {+ file: Mutex::new(File::create(&log_path)?),+ });++ use winit::platform::macos::{ActivationPolicy, EventLoopBuilderExtMacOS};+ let event_loop = EventLoop::<Command>::with_user_event()+ .with_activation_policy(ActivationPolicy::Regular)+ .with_activate_ignoring_other_apps(true)+ .build()?;+ let proxy = event_loop.create_proxy();+ spawn_stdin_reader(proxy);++ let epoch = Arc::new(AtomicU64::new(0));+ let audio = if spec.audio {+ match start_audio(&spec, epoch.clone(), log.clone()) {+ Ok(stream) => Some(stream),+ Err(reason) => {+ log.write(&StimulusEvent::AudioUnavailable { reason });+ None+ }+ }+ } else {+ None+ };++ let mut app = App {+ spec,+ log,+ epoch,+ go_at: None,+ window: None,+ surface: None,+ size: (0, 0),+ shown: None,+ ready: false,+ done: false,+ error: None,+ };+ event_loop.run_app(&mut app)?;+ drop(audio);+ if let Some(error) = app.error {+ anyhow::bail!(error);+ }+ Ok(())+ }++ fn spawn_stdin_reader(proxy: EventLoopProxy<Command>) {+ std::thread::spawn(move || {+ let stdin = std::io::stdin();+ for line in stdin.lock().lines() {+ let Ok(line) = line else { break };+ let command = match line.trim() {+ "go" => Command::Go,+ "quit" => Command::Quit,+ _ => continue,+ };+ if proxy.send_event(command).is_err() {+ return;+ }+ }+ let _ = proxy.send_event(Command::Quit);+ });+ }++ struct App {+ spec: StimulusSpec,+ log: Arc<Log>,+ epoch: Arc<AtomicU64>,+ go_at: Option<(Instant, u64)>,+ window: Option<Arc<Window>>,+ surface: Option<softbuffer::Surface<Arc<Window>, Arc<Window>>>,+ size: (u32, u32),+ shown: Option<u32>,+ ready: bool,+ done: bool,+ error: Option<String>,+ }++ impl App {+ /// The index that should be on screen now.+ fn desired(&self, now: Instant) -> u32 {+ let Some((go, _)) = self.go_at else {+ return 0;+ };+ let elapsed = now.saturating_duration_since(go).as_secs_f64();+ let last = self.spec.frame_count();+ let mut index = ((elapsed * self.spec.fps).floor() as u32 + 1).min(last);+ while index > 1 && self.spec.held(self.spec.due_secs(index)) {+ index -= 1;+ }+ index.max(self.shown.unwrap_or(0))+ }++ fn next_due(&self, now: Instant) -> Option<Instant> {+ let (go, _) = self.go_at?;+ let shown = self.shown.unwrap_or(0);+ let last = self.spec.frame_count();+ (shown + 1..=last)+ .map(|i| go + Duration::from_secs_f64(self.spec.due_secs(i)))+ .find(|&t| t > now)+ .or(Some(now))+ }++ fn draw(&mut self, index: u32) {+ let (w, h) = self.size;+ let Some(surface) = self.surface.as_mut() else {+ return;+ };+ if w == 0 || h == 0 {+ return;+ }+ if surface+ .resize(NonZeroU32::new(w).unwrap(), NonZeroU32::new(h).unwrap())+ .is_err()+ {+ return;+ }+ let Ok(mut buffer) = surface.buffer_mut() else {+ return;+ };+ let (w, h) = (w as usize, h as usize);+ let band = if self.spec.busy { h / 8 } else { 0 };+ {+ let bytes: &mut [u8] = unsafe {+ std::slice::from_raw_parts_mut(buffer.as_mut_ptr().cast::<u8>(), w * h * 4)+ };+ let stride = w * 4;+ let marker = FrameMarker::new(StreamKind::Screen, 0, index);+ video_code::draw_marker(+ marker,+ &mut bytes[band * stride..],+ w,+ h - 2 * band,+ stride,+ PixelOrder::Bgra,+ );+ if band > 0 {+ noise_band(&mut bytes[..band * stride], w, band, index, 0);+ noise_band(&mut bytes[(h - band) * stride..], w, band, index, 1);+ }+ }+ if buffer.present().is_ok() {+ let host = host_now();+ self.shown = Some(index);+ self.log.write(&StimulusEvent::Frame { index, host });+ }+ }+ }++ /// Achromatic 16 px blocks whose levels change every frame.+ fn noise_band(data: &mut [u8], w: usize, h: usize, index: u32, salt: u64) {+ let stride = w * 4;+ let mut seed = (u64::from(index) + 1).wrapping_mul(0x9E37_79B9_7F4A_7C15) ^ salt;+ let blocks_x = w.div_ceil(16);+ let mut levels = vec![0u8; blocks_x * h.div_ceil(16)];+ for level in &mut levels {+ seed ^= seed << 13;+ seed ^= seed >> 7;+ seed ^= seed << 17;+ *level = 40 + (seed % 176) as u8;+ }+ for y in 0..h {+ let row = &mut data[y * stride..y * stride + w * 4];+ for (x, px) in row.chunks_exact_mut(4).enumerate() {+ let v = levels[(y / 16) * blocks_x + x / 16];+ px.copy_from_slice(&[v, v, v, 255]);+ }+ }+ }++ impl ApplicationHandler<Command> for App {+ fn resumed(&mut self, event_loop: &ActiveEventLoop) {+ if self.window.is_some() {+ return;+ }+ let mut attrs = Window::default_attributes()+ .with_title("Cap Verify Stimulus")+ .with_window_level(WindowLevel::AlwaysOnTop);+ attrs = match self.spec.window {+ None => attrs.with_fullscreen(Some(Fullscreen::Borderless(+ event_loop.primary_monitor(),+ ))),+ Some((x, y, w, h)) => attrs+ .with_decorations(false)+ .with_resizable(false)+ .with_position(LogicalPosition::new(x, y))+ .with_inner_size(LogicalSize::new(w, h)),+ };+ let window = match event_loop.create_window(attrs) {+ Ok(w) => Arc::new(w),+ Err(e) => {+ self.error = Some(format!("window: {e}"));+ event_loop.exit();+ return;+ }+ };+ let size = window.inner_size();+ self.size = (size.width, size.height);+ let surface = softbuffer::Context::new(window.clone())+ .and_then(|context| softbuffer::Surface::new(&context, window.clone()));+ match surface {+ Ok(s) => self.surface = Some(s),+ Err(e) => {+ self.error = Some(format!("surface: {e}"));+ event_loop.exit();+ return;+ }+ }+ window.request_redraw();+ self.window = Some(window);+ }++ fn user_event(&mut self, event_loop: &ActiveEventLoop, command: Command) {+ match command {+ Command::Go if self.go_at.is_none() => {+ let host = host_now();+ self.go_at = Some((Instant::now(), host));+ self.epoch.store(host, Ordering::Release);+ self.log.write(&StimulusEvent::Go { host });+ if let Some(window) = &self.window {+ window.request_redraw();+ }+ }+ Command::Go => {}+ Command::Quit => event_loop.exit(),+ }+ }++ fn window_event(+ &mut self,+ event_loop: &ActiveEventLoop,+ _id: WindowId,+ event: WindowEvent,+ ) {+ match event {+ WindowEvent::CloseRequested => event_loop.exit(),+ WindowEvent::Resized(size) => {+ self.size = (size.width, size.height);+ if let Some(window) = &self.window {+ window.request_redraw();+ }+ }+ WindowEvent::RedrawRequested => {+ let index = self.desired(Instant::now());+ let resized = !self.ready;+ if self.shown != Some(index) || resized {+ self.draw(index);+ }+ if !self.ready && self.shown == Some(0) {+ self.ready = true;+ let scale = self.window.as_ref().map_or(1.0, |w| w.scale_factor());+ self.log.write(&StimulusEvent::Ready {+ host: host_now(),+ width: self.size.0,+ height: self.size.1,+ scale,+ });+ say("ready");+ }+ if !self.done+ && self.go_at.is_some()+ && self.shown == Some(self.spec.frame_count())+ {+ self.done = true;+ self.log.write(&StimulusEvent::Done { host: host_now() });+ say("done");+ }+ }+ _ => {}+ }+ }++ fn about_to_wait(&mut self, event_loop: &ActiveEventLoop) {+ if self.done || self.go_at.is_none() {+ event_loop.set_control_flow(ControlFlow::Wait);+ return;+ }+ let now = Instant::now();+ if let Some(window) = &self.window+ && self.desired(now) != self.shown.unwrap_or(0)+ {+ window.request_redraw();+ }+ match self.next_due(now) {+ Some(next) => event_loop.set_control_flow(ControlFlow::WaitUntil(next)),+ None => event_loop.set_control_flow(ControlFlow::Wait),+ }+ }+ }++ struct AudioState {+ sample_rate: u32,+ channels: usize,+ epoch: Arc<AtomicU64>,+ lead_secs: f64,+ markers: Vec<(MarkerPlacement, Vec<f32>)>,+ written: u64,+ epoch_sample: Option<u64>,+ events: mpsc::Sender<StimulusEvent>,+ }++ impl AudioState {+ fn fill(&mut self, data: &mut [f32], callback_host: u64) {+ data.fill(0.0);+ let frames = data.len() / self.channels.max(1);+ let start = self.written;+ self.written += frames as u64;+ let epoch = self.epoch.load(Ordering::Acquire);+ if epoch == 0 {+ return;+ }+ let rate = f64::from(self.sample_rate);+ let epoch_sample = *self.epoch_sample.get_or_insert_with(|| {+ let ahead = host_secs(epoch, callback_host) + self.lead_secs;+ (start as f64 + (ahead * rate).round()).max(start as f64) as u64+ });+ for (placement, wave) in &self.markers {+ let first = epoch_sample + (placement.time_secs * rate).round() as u64;+ let last = first + wave.len() as u64;+ if last <= start || first >= start + frames as u64 {+ continue;+ }+ if first >= start {+ let offset = (first - start) as f64 / rate;+ let _ = self.events.send(StimulusEvent::Audio {+ index: placement.index,+ host: host_add_secs(callback_host, offset),+ });+ }+ for i in 0..frames {+ let at = start + i as u64;+ if at < first || at >= last {+ continue;+ }+ let v = wave[(at - first) as usize] * audio_code::DEFAULT_AMPLITUDE;+ for c in 0..self.channels {+ data[i * self.channels + c] += v;+ }+ }+ }+ }+ }++ fn start_audio(+ spec: &StimulusSpec,+ epoch: Arc<AtomicU64>,+ log: Arc<Log>,+ ) -> Result<cpal::Stream, String> {+ use cpal::host::coreaudio::StreamInstantExt;++ let host = cpal::default_host();+ let device = host+ .default_output_device()+ .ok_or("no default output device")?;+ let config = device+ .default_output_config()+ .map_err(|e| format!("output config: {e}"))?;+ if config.sample_format() != cpal::SampleFormat::F32 {+ return Err(format!("unsupported format {:?}", config.sample_format()));+ }+ let sample_rate = config.sample_rate().0;+ let channels = config.channels();+ let lead_secs = 0.25;+ let markers = audio_code::standard_schedule(+ AudioSource::System,+ lead_secs,+ spec.seconds - lead_secs,+ spec.audio_period,+ )+ .into_iter()+ .map(|p| {+ let wave = audio_code::marker_waveform(AudioSource::System, p.index, sample_rate);+ (+ MarkerPlacement {+ index: p.index,+ time_secs: p.time_secs - lead_secs,+ },+ wave,+ )+ })+ .collect();+ let (tx, rx) = mpsc::channel::<StimulusEvent>();+ {+ let log = log.clone();+ std::thread::spawn(move || {+ for event in rx {+ log.write(&event);+ }+ });+ }+ let mut state = AudioState {+ sample_rate,+ channels: usize::from(channels),+ epoch,+ lead_secs,+ markers,+ written: 0,+ epoch_sample: None,+ events: tx,+ };+ let stream = device+ .build_output_stream(+ &config.config(),+ move |data: &mut [f32], info: &cpal::OutputCallbackInfo| {+ state.fill(data, info.timestamp().callback.as_host_time());+ },+ |e| eprintln!("stimulus audio error: {e}"),+ None,+ )+ .map_err(|e| format!("output stream: {e}"))?;+ stream.play().map_err(|e| format!("play: {e}"))?;+ log.write(&StimulusEvent::AudioStarted {+ device: device.name().unwrap_or_default(),+ sample_rate,+ channels,+ });+ Ok(stream)+ }+}diff --git a/crates/verify/src/fixture_avf.rs b/crates/verify/src/fixture_avf.rsindex 048d595e2..fa6bd34dd 100644--- a/crates/verify/src/fixture_avf.rs+++ b/crates/verify/src/fixture_avf.rs@@ -11,7 +11,7 @@ use cidre::{arc, cf, cm, cv};use crate::video_code::{self, FrameMarker};-fn pixel_buffer(width: usize, height: usize, rgba: &[u8]) -> anyhow::Result<arc::R<cv::PixelBuf>> {+pub fn pixel_buffer(width: usize, height: usize, rgba: &[u8]) -> anyhow::Result<arc::R<cv::PixelBuf>> {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()];diff --git a/crates/verify/src/lib.rs b/crates/verify/src/lib.rsindex b4e3bee11..cdd93c44c 100644--- a/crates/verify/src/lib.rs+++ b/crates/verify/src/lib.rs@@ -11,4 +11,5 @@ pub mod model;pub mod oracle;pub mod pipeline;pub mod report;+pub mod stimulus;pub mod video_code;diff --git a/crates/verify/src/stimulus.rs b/crates/verify/src/stimulus.rsnew file mode 100644index 000000000..66335e70f--- /dev/null+++ b/crates/verify/src/stimulus.rs@@ -0,0 +1,220 @@+//! Real-capture stimulus: a helper process (`cap-verify-stimulus`) that shows+//! a window rendering the screen marker of a monotonically increasing frame+//! index at a fixed rate and plays system-audio markers through the default+//! output, logging the host time of every presented frame and every marker.+//!+//! Protocol: the helper presents frame 0 and prints `ready`; the test starts+//! its recording and writes `go`; the helper then advances one index per+//! `1 / fps` (holding the picture inside `holds`), prints `done` after the+//! last frame and keeps the last frame on screen until `quit` (or EOF).+//! Every event is a JSON line in the log file; host times are mach absolute+//! ticks, the clock Cap stamps captured media with on macOS.++use std::path::Path;++use serde::{Deserialize, Serialize};++#[derive(Clone, Debug, Serialize, Deserialize)]+pub struct StimulusSpec {+ pub fps: f64,+ pub seconds: f64,+ /// (start, length) seconds after `go` during which the picture is held.+ pub holds: Vec<(f64, f64)>,+ /// Pseudo-random achromatic noise bands above and below the marker, new+ /// every frame, so the encoder sees dense changing content.+ pub busy: bool,+ pub audio: bool,+ pub audio_period: f64,+ /// `None` = borderless fullscreen on the primary display; otherwise a+ /// window at (x, y, width, height) in points.+ pub window: Option<(i32, i32, u32, u32)>,+}++impl StimulusSpec {+ pub fn frame_count(&self) -> u32 {+ (self.seconds * self.fps).round() as u32+ }++ /// Seconds after `go` at which frame `index` (>= 1) is due.+ pub fn due_secs(&self, index: u32) -> f64 {+ f64::from(index.saturating_sub(1)) / self.fps+ }++ pub fn held(&self, secs: f64) -> bool {+ self.holds.iter().any(|&(s, l)| secs > s && secs < s + l)+ }+}++#[derive(Clone, Debug, Serialize, Deserialize)]+#[serde(tag = "event", rename_all = "snake_case")]+pub enum StimulusEvent {+ Ready {+ host: u64,+ width: u32,+ height: u32,+ scale: f64,+ },+ Go {+ host: u64,+ },+ Frame {+ index: u32,+ /// Host time right after the frame was handed to the window server.+ host: u64,+ },+ AudioStarted {+ device: String,+ sample_rate: u32,+ channels: u16,+ },+ AudioUnavailable {+ reason: String,+ },+ /// Chirp start of a system-audio marker at the output, from the render+ /// callback's host time.+ Audio {+ index: u32,+ host: u64,+ },+ Done {+ host: u64,+ },+}++pub fn read_log(path: &Path) -> anyhow::Result<Vec<StimulusEvent>> {+ let text = std::fs::read_to_string(path)?;+ text.lines()+ .filter(|l| !l.trim().is_empty())+ .map(|l| serde_json::from_str(l).map_err(Into::into))+ .collect()+}++#[cfg(target_os = "macos")]+pub fn host_now() -> u64 {+ cidre::mach::abs_time()+}++/// Seconds between two mach host times (`a - b`).+#[cfg(target_os = "macos")]+pub fn host_secs(a: u64, b: u64) -> f64 {+ let info = cidre::mach::TimeBaseInfo::new();+ let ticks = a as f64 - b as f64;+ ticks * f64::from(info.numer) / f64::from(info.denom) / 1e9+}++#[cfg(target_os = "macos")]+pub fn host_add_secs(a: u64, secs: f64) -> u64 {+ let info = cidre::mach::TimeBaseInfo::new();+ (a as f64 + secs * 1e9 * f64::from(info.denom) / f64::from(info.numer)).round() as u64+}++#[cfg(target_os = "macos")]+pub use process::StimulusProcess;++#[cfg(target_os = "macos")]+mod process {+ use std::{+ io::{BufRead, BufReader, Write},+ path::{Path, PathBuf},+ process::{Child, ChildStdin, Command, Stdio},+ sync::mpsc,+ time::Duration,+ };++ use anyhow::{Context, bail};++ use super::{StimulusEvent, StimulusSpec, read_log};++ pub struct StimulusProcess {+ child: Child,+ stdin: Option<ChildStdin>,+ lines: mpsc::Receiver<String>,+ log: PathBuf,+ }++ impl StimulusProcess {+ pub fn spawn(binary: &Path, spec: &StimulusSpec, log: &Path) -> anyhow::Result<Self> {+ let mut child = Command::new(binary)+ .arg(serde_json::to_string(spec)?)+ .arg(log)+ .stdin(Stdio::piped())+ .stdout(Stdio::piped())+ .stderr(Stdio::inherit())+ .spawn()+ .with_context(|| format!("spawn {}", binary.display()))?;+ let stdout = child.stdout.take().context("stimulus stdout")?;+ let (tx, lines) = mpsc::channel();+ std::thread::spawn(move || {+ for line in BufReader::new(stdout).lines().map_while(Result::ok) {+ if tx.send(line).is_err() {+ break;+ }+ }+ });+ let stdin = child.stdin.take();+ Ok(Self {+ child,+ stdin,+ lines,+ log: log.to_path_buf(),+ })+ }++ pub fn pid(&self) -> u32 {+ self.child.id()+ }++ pub fn wait_for(&mut self, word: &str, timeout: Duration) -> anyhow::Result<()> {+ let deadline = std::time::Instant::now() + timeout;+ loop {+ let left = deadline.saturating_duration_since(std::time::Instant::now());+ match self.lines.recv_timeout(left) {+ Ok(line) if line.trim() == word => return Ok(()),+ Ok(line) if line.starts_with("error") => bail!("stimulus: {line}"),+ Ok(_) => {}+ Err(mpsc::RecvTimeoutError::Timeout) => {+ bail!("stimulus did not report `{word}` within {timeout:?}")+ }+ Err(mpsc::RecvTimeoutError::Disconnected) => {+ bail!("stimulus exited before `{word}`")+ }+ }+ }+ }++ pub fn send(&mut self, word: &str) -> anyhow::Result<()> {+ let stdin = self.stdin.as_mut().context("stimulus stdin closed")?;+ writeln!(stdin, "{word}")?;+ stdin.flush()?;+ Ok(())+ }++ /// Closes the window and returns the event log.+ pub fn finish(mut self) -> anyhow::Result<Vec<StimulusEvent>> {+ let _ = self.send("quit");+ self.stdin = None;+ let deadline = std::time::Instant::now() + Duration::from_secs(10);+ loop {+ if self.child.try_wait()?.is_some() {+ break;+ }+ if std::time::Instant::now() > deadline {+ let _ = self.child.kill();+ let _ = self.child.wait();+ break;+ }+ std::thread::sleep(Duration::from_millis(20));+ }+ read_log(&self.log)+ }+ }++ impl Drop for StimulusProcess {+ fn drop(&mut self) {+ if matches!(self.child.try_wait(), Ok(None)) {+ let _ = self.child.kill();+ let _ = self.child.wait();+ }+ }+ }+}diff --git a/crates/verify/tests/recording.rs b/crates/verify/tests/recording.rsnew file mode 100644index 000000000..0a75772e3--- /dev/null+++ b/crates/verify/tests/recording.rs@@ -0,0 +1,1317 @@+//! Real-capture recording oracle (macOS).+//!+//! A helper process shows the screen marker of a monotonically increasing+//! frame index at a fixed rate and plays system-audio markers; synthetic+//! camera and microphone feeds deliver their own markers with exact capture+//! times. Cap's real Studio and Instant pipelines record it all through+//! ScreenCaptureKit and VideoToolbox, stop and finalize exactly like the app+//! and the CLI, and every output frame and audio marker is then checked+//! against the host-clock truth logged by the sources.+//!+//! Capture mode (`CAP_VERIFY_CAPTURE`): `display` records the primary display+//! with a fullscreen stimulus (CI); `window` records only the stimulus window+//! (default on a developer Mac, so other screen activity cannot leak in);+//! `off` skips. Without Screen Recording permission the cases skip unless+//! `CAP_VERIFY_REQUIRE_CAPTURE=1`.++#![cfg(target_os = "macos")]++use std::{+ collections::BTreeMap,+ path::{Path, PathBuf},+ sync::{+ Arc,+ atomic::{AtomicBool, AtomicU64, Ordering},+ },+ time::Duration,+};++use cap_media_info::{RawVideoFormat, VideoInfo};+use cap_project::{InstantRecordingMeta, RecordingMeta, RecordingMetaInner, StudioRecordingMeta};+use cap_recording::{+ NativeCameraFrame, RecordingDefaults, SendableShareableContent, StudioQuality,+ feeds::{camera::synthetic::SyntheticCamera, microphone::synthetic::SyntheticMicrophone},+ instant_recording,+ recovery::RecoveryManager,+ screen_capture::ScreenCaptureTarget,+ studio_recording,+};+use cap_timestamp::{MachAbsoluteTimestamp, Timestamp};+use cap_verify::{+ audio_code::{self, AudioSource},+ fixture_avf, media,+ report::{self, Case, Tier},+ stimulus::{+ StimulusEvent, StimulusProcess, StimulusSpec, host_add_secs, host_now, host_secs,+ },+ video_code::{self, FrameMarker, Readout, StreamKind},+};++const MIC_RATE: u32 = 48_000;+const MIC_CHUNK: usize = 480;+const CAMERA_FPS: u32 = 30;+const CAMERA_SIZE: (usize, usize) = (640, 360);+const AUDIO_PERIOD: f64 = 0.5;+const WINDOW_TITLE: &str = "Cap Verify Stimulus";+const SYSTEM_AUDIO_STARTUP: f64 = 1.0;++#[derive(Clone, Copy, Debug, PartialEq, Eq)]+enum Mode {+ Studio { fragmented: bool, camera: bool },+ Instant,+}++#[derive(Clone, Debug)]+struct Spec {+ id: &'static str,+ tier: Tier,+ mode: Mode,+ seconds: f64,+ stimulus_fps: f64,+ holds: Vec<(f64, f64)>,+ /// Pairs of consecutive camera frames dropped this many seconds after+ /// the recording started (the start-up drops #2573 fixed).+ camera_drops_at: Vec<f64>,+}++fn specs() -> Vec<Spec> {+ vec![+ Spec {+ id: "recording/studio-screen/fragmented",+ tier: Tier::Fast,+ mode: Mode::Studio {+ fragmented: true,+ camera: false,+ },+ seconds: 8.0,+ stimulus_fps: 30.0,+ holds: vec![],+ camera_drops_at: vec![],+ },+ Spec {+ id: "recording/studio-camera/startup-drops",+ tier: Tier::Fast,+ mode: Mode::Studio {+ fragmented: true,+ camera: true,+ },+ seconds: 8.0,+ stimulus_fps: 20.0,+ holds: vec![],+ camera_drops_at: vec![0.3, 1.1],+ },+ Spec {+ id: "recording/instant/screen-mic-system",+ tier: Tier::Fast,+ mode: Mode::Instant,+ seconds: 8.0,+ stimulus_fps: 20.0,+ holds: vec![],+ camera_drops_at: vec![],+ },+ Spec {+ id: "recording/studio-screen/mp4-holds",+ tier: Tier::Full,+ mode: Mode::Studio {+ fragmented: false,+ camera: false,+ },+ seconds: 30.0,+ stimulus_fps: 30.0,+ holds: vec![(5.0, 2.0), (12.0, 0.5), (20.0, 4.0)],+ camera_drops_at: vec![],+ },+ Spec {+ id: "recording/studio-camera/long",+ tier: Tier::Full,+ mode: Mode::Studio {+ fragmented: true,+ camera: true,+ },+ seconds: 30.0,+ stimulus_fps: 20.0,+ holds: vec![(10.0, 3.0)],+ camera_drops_at: vec![0.5, 1.5, 12.0],+ },+ Spec {+ id: "recording/instant/long-holds",+ tier: Tier::Full,+ mode: Mode::Instant,+ seconds: 30.0,+ stimulus_fps: 20.0,+ holds: vec![(6.0, 2.0), (18.0, 5.0)],+ camera_drops_at: vec![],+ },+ ]+}++#[derive(Clone, Copy, Debug, PartialEq, Eq)]+enum CaptureMode {+ Display,+ Window,+}++fn capture_mode() -> Option<CaptureMode> {+ match std::env::var("CAP_VERIFY_CAPTURE").as_deref() {+ Ok("display") => Some(CaptureMode::Display),+ Ok("window") => Some(CaptureMode::Window),+ Ok("off") => None,+ _ if std::env::var_os("CI").is_some() => Some(CaptureMode::Display),+ _ => Some(CaptureMode::Window),+ }+}++fn env_flag(name: &str) -> bool {+ matches!(std::env::var(name).as_deref(), Ok("1") | Ok("true"))+}++fn runtime() -> tokio::runtime::Runtime {+ tokio::runtime::Builder::new_multi_thread()+ .worker_threads(4)+ .thread_stack_size(16 * 1024 * 1024)+ .enable_all()+ .build()+ .expect("runtime")+}++#[test]+fn real_capture() {+ media::init();+ if std::env::var_os("RUST_LOG").is_some() {+ let _ = tracing_subscriber::fmt()+ .with_env_filter(tracing_subscriber::EnvFilter::from_default_env())+ .with_writer(std::io::stderr)+ .try_init();+ }+ let Some(capture) = capture_mode() else {+ eprintln!("[cap-verify] recording: CAP_VERIFY_CAPTURE=off, skipping");+ return;+ };+ let rt = runtime();+ let shareable = match rt.block_on(cidre::sc::ShareableContent::current()) {+ Ok(content) => SendableShareableContent::from(content),+ Err(e) => {+ let message = format!("ScreenCaptureKit unavailable ({e:?}); grant Screen Recording");+ if env_flag("CAP_VERIFY_REQUIRE_CAPTURE") {+ panic!("{message}");+ }+ eprintln!("[cap-verify] recording: {message}, skipping");+ return;+ }+ };+ let tier = report::tier();+ let repeat: usize = std::env::var("CAP_VERIFY_REPEAT")+ .ok()+ .and_then(|v| v.parse().ok())+ .unwrap_or(1);+ let mut failures = Vec::new();+ for spec in specs() {+ if !report::selected(spec.id) || (spec.tier == Tier::Full && tier == Tier::Fast) {+ continue;+ }+ for attempt in 0..repeat {+ let mut case = Case::new(spec.id);+ if repeat > 1 {+ case.note(format!("repeat {}/{repeat}", attempt + 1));+ }+ if let Err(e) = rt.block_on(run_case(&spec, capture, &shareable, &mut case)) {+ case.problem(format!("{e:#}"));+ }+ for note in &case.notes {+ eprintln!(" note: {note}");+ }+ if let Err(e) = report::settle(case) {+ failures.push(e);+ }+ }+ }+ assert!(failures.is_empty(), "{}", failures.join("\n"));+}++/// Host-clock truth for one source: marker index -> host time (seconds from+/// the case reference).+type Truth = BTreeMap<u32, f64>;++struct Feeds {+ mic: Arc<SyntheticMicrophone>,+ camera: Option<Arc<SyntheticCamera>>,+ stop: Arc<AtomicBool>,+ started: Arc<AtomicU64>,+ mic_task: tokio::task::JoinHandle<Truth>,+ camera_task: Option<tokio::task::JoinHandle<(Truth, Vec<u32>)>>,+}++async fn sleep_until_host(host: u64) {+ let secs = host_secs(host, host_now());+ if secs > 0.0 {+ tokio::time::sleep(Duration::from_secs_f64(secs)).await;+ }+}++fn start_feeds(reference: u64, max_secs: f64, camera: bool, drops_at: Vec<f64>) -> Feeds {+ let stop = Arc::new(AtomicBool::new(false));+ let started = Arc::new(AtomicU64::new(0));+ let mic = Arc::new(SyntheticMicrophone::spawn(+ "Cap Verify Synthetic Microphone",+ MIC_RATE,+ 1,+ ));+ let mic_task = {+ let mic = mic.clone();+ let stop = stop.clone();+ tokio::spawn(async move {+ let origin = host_now();+ let schedule =+ audio_code::standard_schedule(AudioSource::Mic, 0.3, max_secs - 0.3, AUDIO_PERIOD);+ let mut signal = vec![0.0f32; (max_secs * f64::from(MIC_RATE)) as usize];+ audio_code::render_markers(+ &mut signal,+ MIC_RATE,+ AudioSource::Mic,+ &schedule,+ audio_code::DEFAULT_AMPLITUDE,+ );+ let base = host_secs(origin, reference);+ let truth: Truth = schedule+ .iter()+ .map(|p| (p.index, base + p.time_secs))+ .collect();+ for (n, chunk) in signal.chunks(MIC_CHUNK).enumerate() {+ if stop.load(Ordering::Acquire) {+ break;+ }+ let first = n * MIC_CHUNK;+ let capture = host_add_secs(origin, first as f64 / f64::from(MIC_RATE));+ let delivered =+ host_add_secs(origin, (first + chunk.len()) as f64 / f64::from(MIC_RATE));+ sleep_until_host(delivered).await;+ mic.push(+ chunk,+ Timestamp::MachAbsoluteTime(MachAbsoluteTimestamp::new(capture)),+ )+ .await;+ }+ truth+ })+ };+ let (camera, camera_task) = if camera {+ let info = VideoInfo::from_raw(+ RawVideoFormat::Nv12,+ CAMERA_SIZE.0 as u32,+ CAMERA_SIZE.1 as u32,+ CAMERA_FPS,+ );+ let feed = Arc::new(SyntheticCamera::spawn("Cap Verify Synthetic Camera", info));+ let task = {+ let feed = feed.clone();+ let stop = stop.clone();+ let started = started.clone();+ tokio::spawn(async move {+ let origin = host_now();+ let base = host_secs(origin, reference);+ let mut truth = Truth::new();+ let mut dropped = Vec::new();+ let mut drop_slots: Option<Vec<u32>> = None;+ for slot in 0..(max_secs * f64::from(CAMERA_FPS)) as u32 {+ if stop.load(Ordering::Acquire) {+ break;+ }+ let secs = f64::from(slot) / f64::from(CAMERA_FPS);+ let capture = host_add_secs(origin, secs);+ sleep_until_host(capture).await;+ let start = started.load(Ordering::Acquire);+ if drop_slots.is_none() && start != 0 {+ let since = host_secs(start, origin);+ drop_slots = Some(+ drops_at+ .iter()+ .flat_map(|at| {+ let s = ((since + at) * f64::from(CAMERA_FPS)).ceil() as u32;+ [s, s + 1]+ })+ .collect(),+ );+ }+ if drop_slots.as_ref().is_some_and(|d| d.contains(&slot)) {+ dropped.push(slot);+ continue;+ }+ let Some(frame) = camera_frame(slot, capture) else {+ continue;+ };+ feed.push_native(frame).await;+ truth.insert(slot, base + secs);+ }+ (truth, dropped)+ })+ };+ (Some(feed), Some(task))+ } else {+ (None, None)+ };+ Feeds {+ mic,+ camera,+ stop,+ started,+ mic_task,+ camera_task,+ }+}++fn camera_frame(slot: u32, capture: u64) -> Option<NativeCameraFrame> {+ let (w, h) = CAMERA_SIZE;+ let rgba = video_code::marker_rgba(FrameMarker::new(StreamKind::Camera, 0, slot), w, h);+ let sample_buf = camera_sample(&rgba, w, h, capture).ok()?;+ Some(NativeCameraFrame {+ sample_buf,+ timestamp: Timestamp::MachAbsoluteTime(MachAbsoluteTimestamp::new(capture)),+ })+}++fn camera_sample(+ rgba: &[u8],+ w: usize,+ h: usize,+ host: u64,+) -> anyhow::Result<cidre::arc::R<cidre::cm::SampleBuf>> {+ use cidre::cm;+ let buf = fixture_avf::pixel_buffer(w, h, rgba)?;+ let format_desc = cm::VideoFormatDesc::with_image_buf(&buf)+ .map_err(|e| anyhow::anyhow!("format description: {e:?}"))?;+ let nanos = (host_secs(host, 0) * 1e9).round() as i64;+ let timing = cm::SampleTimingInfo {+ duration: cm::Time::new(1, CAMERA_FPS as i32),+ pts: cm::Time::new(nanos, 1_000_000_000),+ dts: cm::Time::invalid(),+ };+ cm::SampleBuf::with_image_buf(&buf, true, None, std::ptr::null(), &format_desc, &timing)+ .map_err(|e| anyhow::anyhow!("sample buffer: {e:?}"))+}++enum Handle {+ Studio(studio_recording::ActorHandle),+ Instant(instant_recording::ActorHandle),+}++fn stimulus_binary() -> PathBuf {+ std::env::var_os("CAP_VERIFY_STIMULUS_BIN")+ .map(PathBuf::from)+ .unwrap_or_else(|| PathBuf::from(env!("CARGO_BIN_EXE_cap-verify-stimulus")))+}++fn find_window(title: &str) -> Option<scap_targets::WindowId> {+ scap_targets::Window::list()+ .into_iter()+ .find(|w| w.name().as_deref() == Some(title))+ .map(|w| w.id())+}++struct Recorded {+ project: PathBuf,+ started: f64,+ stopped: f64,+ stimulus: Vec<StimulusEvent>,+ mic_truth: Truth,+ camera: Option<(Truth, Vec<u32>)>,+ max_fps: u32,+}++async fn run_case(+ spec: &Spec,+ capture: CaptureMode,+ shareable: &SendableShareableContent,+ case: &mut Case,+) -> anyhow::Result<()> {+ let scratch = scratch_root().join(spec.id.replace('/', "-"));+ let _ = std::fs::remove_dir_all(&scratch);+ std::fs::create_dir_all(&scratch)?;+ let reference = host_now();+ let recorded = record(spec, capture, shareable, &scratch, reference, case).await?;+ analyze(spec, &recorded, reference, case)?;+ if !case.failed() && !env_flag("CAP_VERIFY_KEEP") {+ let _ = std::fs::remove_dir_all(&scratch);+ } else {+ case.note(format!("artifacts kept at {}", scratch.display()));+ }+ Ok(())+}++fn scratch_root() -> PathBuf {+ std::env::var_os("CAP_VERIFY_SCRATCH")+ .map(PathBuf::from)+ .unwrap_or_else(|| std::env::temp_dir().join("cap-verify-recording"))+}++async fn record(+ spec: &Spec,+ capture: CaptureMode,+ _preflight: &SendableShareableContent,+ scratch: &Path,+ reference: u64,+ case: &mut Case,+) -> anyhow::Result<Recorded> {+ let stimulus_spec = StimulusSpec {+ fps: spec.stimulus_fps,+ seconds: spec.seconds,+ holds: spec.holds.clone(),+ busy: true,+ audio: true,+ audio_period: AUDIO_PERIOD,+ window: match capture {+ CaptureMode::Display => None,+ CaptureMode::Window => Some((80, 120, 640, 400)),+ },+ };+ let mut stimulus = tokio::task::block_in_place(|| {+ StimulusProcess::spawn(&stimulus_binary(), &stimulus_spec, &scratch.join("stimulus.jsonl"))+ })?;+ tokio::task::block_in_place(|| stimulus.wait_for("ready", Duration::from_secs(30)))?;+ // A fullscreen window lands in its own Space; let the transition finish+ // so the first captured frames already show the stimulus.+ tokio::time::sleep(Duration::from_millis(match capture {+ CaptureMode::Display => 1500,+ CaptureMode::Window => 300,+ }))+ .await;+ // Only the stimulus may reach the recording: other windows (system+ // prompts, the developer's apps) are excluded from the capture filter.+ let stimulus_window = find_window(WINDOW_TITLE);+ let excluded: Vec<scap_targets::WindowId> = scap_targets::Window::list()+ .into_iter()+ .map(|w| w.id())+ .filter(|id| Some(id) != stimulus_window.as_ref())+ .collect();+ let shareable: SendableShareableContent = cidre::sc::ShareableContent::current()+ .await+ .map_err(|e| anyhow::anyhow!("shareable content: {e:?}"))?+ .into();+ let shareable = &shareable;+ let target = match capture {+ CaptureMode::Display => ScreenCaptureTarget::Display {+ id: scap_targets::Display::primary().id(),+ },+ CaptureMode::Window => ScreenCaptureTarget::Window {+ id: stimulus_window+ .clone()+ .ok_or_else(|| anyhow::anyhow!("stimulus window not found"))?,+ },+ };++ let camera = matches!(spec.mode, Mode::Studio { camera: true, .. });+ let feeds = start_feeds(+ reference,+ spec.seconds + 30.0,+ camera,+ spec.camera_drops_at.clone(),+ );+ let mic_lock = Arc::new(feeds.mic.lock().await?);+ let camera_lock = match &feeds.camera {+ Some(camera) => Some(Arc::new(camera.lock().await?)),+ None => None,+ };+ // Real cameras and microphones are already streaming when a recording+ // starts.+ tokio::time::sleep(Duration::from_millis(300)).await;++ let project = scratch.join("recording.cap");+ let defaults = RecordingDefaults::default();+ let (handle, max_fps) = match spec.mode {+ Mode::Studio { fragmented, .. } => {+ let mut builder = defaults.apply_to_studio_builder(+ studio_recording::Actor::builder(project.clone(), target).with_system_audio(true),+ camera,+ None,+ );+ builder = builder+ .with_excluded_windows(excluded.clone())+ .with_keyboard_capture(false)+ .with_fragmented(fragmented)+ .with_quality(StudioQuality::Balanced)+ .with_mic_feed(mic_lock.clone());+ if let Some(lock) = &camera_lock {+ builder = builder.with_camera_feed(lock.clone());+ }+ let max_fps = defaults.studio_max_fps(camera, None);+ (+ Handle::Studio(builder.build(Some(shareable.clone())).await?),+ max_fps,+ )+ }+ Mode::Instant => {+ let builder = instant_recording::Actor::builder(project.clone(), target)+ .with_system_audio(true)+ .with_max_output_size(defaults.instant_mode_max_resolution)+ .with_excluded_windows(excluded.clone())+ .with_mic_feed(mic_lock.clone());+ (+ Handle::Instant(builder.build(Some(shareable.clone())).await?),+ cap_recording::DEFAULT_INSTANT_MODE_FPS,+ )+ }+ };+ let started_host = host_now();+ feeds.started.store(started_host, Ordering::Release);+ tokio::task::block_in_place(|| stimulus.send("go"))?;+ let waited = tokio::task::block_in_place(|| {+ stimulus.wait_for("done", Duration::from_secs_f64(spec.seconds + 20.0))+ });+ tokio::time::sleep(Duration::from_millis(400)).await;+ let stopped_host = host_now();+ let stop_started = std::time::Instant::now();+ let project_path = match handle {+ Handle::Studio(handle) => {+ let completed = handle.stop().await?;+ let path = completed.project_path.clone();+ let finalize = std::time::Instant::now();+ let remux_path = path.clone();+ tokio::task::spawn_blocking(move || RecoveryManager::remux_if_needed(&remux_path))+ .await??;+ case.note(format!(+ "stop {:.2}s, remux {:.2}s",+ (finalize - stop_started).as_secs_f64(),+ finalize.elapsed().as_secs_f64()+ ));+ path+ }+ Handle::Instant(handle) => {+ let mut completed = handle.stop().await?;+ let finalize = std::time::Instant::now();+ finalize_instant(&mut completed).await?;+ case.note(format!(+ "stop {:.2}s, finalize {:.2}s",+ (finalize - stop_started).as_secs_f64(),+ finalize.elapsed().as_secs_f64()+ ));+ completed.project_path+ }+ };+ feeds.stop.store(true, Ordering::Release);+ drop(mic_lock);+ drop(camera_lock);+ let mic_truth = feeds.mic_task.await?;+ let camera_truth = match feeds.camera_task {+ Some(task) => Some(task.await?),+ None => None,+ };+ drop(feeds.camera);+ let events = tokio::task::block_in_place(|| stimulus.finish())?;+ waited?;+ Ok(Recorded {+ project: project_path,+ started: host_secs(started_host, reference),+ stopped: host_secs(stopped_host, reference),+ stimulus: events,+ mic_truth,+ camera: camera_truth,+ max_fps,+ })+}++async fn finalize_instant(+ recording: &mut instant_recording::CompletedRecording,+) -> anyhow::Result<()> {+ let completion = recording.clean_completion.take();+ let project = recording.project_path.clone();+ let output = project.join("content/output.mp4");+ let audio_dir = project.join("content/audio");+ let display_dir = project.join("content/display");+ let already = std::fs::metadata(&output).is_ok_and(|m| m.len() > 0) && !audio_dir.exists();+ if !already {+ tokio::task::spawn_blocking(move || match completion {+ Some(completion) => RecoveryManager::finalize_completed_instant_output(+ &display_dir,+ &audio_dir,+ &output,+ completion,+ ),+ None => RecoveryManager::finalize_instant_output(&display_dir, &audio_dir, &output),+ })+ .await??;+ }+ let meta = match &recording.meta {+ InstantRecordingMeta::InProgress { .. } => InstantRecordingMeta::Failed {+ error: "instant recording stopped before completion".to_string(),+ },+ other => other.clone(),+ };+ RecordingMeta {+ platform: Some(cap_project::Platform::default()),+ project_path: project.clone(),+ pretty_name: "Cap Verify".to_string(),+ sharing: None,+ inner: RecordingMetaInner::Instant(meta),+ upload: None,+ }+ .save_for_project()+ .map_err(|e| anyhow::anyhow!("save meta: {e}"))?;+ Ok(())+}++/// One recorded track located through recording-meta.json.+struct Track {+ path: PathBuf,+ /// `start_time` from the meta: the editor aligns tracks by it.+ start: f64,+}++struct Layout {+ display: Track,+ camera: Option<Track>,+ mic: Option<Track>,+ system: Option<Track>,+ /// Instant mode muxes every source into the display file.+ muxed_audio: bool,+}++fn layout(project: &Path, case: &mut Case) -> anyhow::Result<Layout> {+ let meta = RecordingMeta::load_for_project(project)+ .map_err(|e| anyhow::anyhow!("recording-meta.json: {e}"))?;+ match &meta.inner {+ RecordingMetaInner::Instant(instant) => {+ if !matches!(instant, InstantRecordingMeta::Complete { .. }) {+ case.problem(format!("instant meta is not complete: {instant:?}"));+ }+ Ok(Layout {+ display: Track {+ path: project.join("content/output.mp4"),+ start: 0.0,+ },+ camera: None,+ mic: None,+ system: None,+ muxed_audio: true,+ })+ }+ RecordingMetaInner::Studio(studio) => {+ let StudioRecordingMeta::MultipleSegments { inner } = &**studio else {+ anyhow::bail!("expected a multi-segment studio recording");+ };+ if !matches!(+ studio.status(),+ cap_project::StudioRecordingStatus::Complete+ ) {+ case.problem(format!("studio status after finalize: {:?}", studio.status()));+ }+ if inner.segments.len() != 1 {+ case.problem(format!("{} segments, expected 1", inner.segments.len()));+ }+ let segment = inner+ .segments+ .first()+ .ok_or_else(|| anyhow::anyhow!("no segments"))?;+ let mut display = meta.path(&segment.display.path);+ if display.is_dir() {+ case.problem(format!(+ "display path still points at fragments after finalize: {}",+ display.display()+ ));+ display = display.with_extension("mp4");+ }+ let track = |path: &relative_path::RelativePathBuf, start: Option<f64>| Track {+ path: meta.path(path),+ start: start.unwrap_or(0.0),+ };+ Ok(Layout {+ display: track(&segment.display.path, segment.display.start_time),+ camera: segment+ .camera+ .as_ref()+ .map(|c| track(&c.path, c.start_time)),+ mic: segment.mic.as_ref().map(|m| track(&m.path, m.start_time)),+ system: segment+ .system_audio+ .as_ref()+ .map(|s| track(&s.path, s.start_time)),+ muxed_audio: false,+ })+ .map(|mut l| {+ l.display.path = display;+ l+ })+ }+ }+}++/// Offsets of every observed marker: recorded (editor) time minus host truth.+#[derive(Default, Clone)]+struct Offsets(Vec<(f64, f64)>);++impl Offsets {+ fn median(&self) -> Option<f64> {+ let mut v: Vec<f64> = self.0.iter().map(|(_, d)| *d).collect();+ if v.is_empty() {+ return None;+ }+ v.sort_by(f64::total_cmp);+ Some(v[v.len() / 2])+ }++ fn spread(&self) -> f64 {+ let v: Vec<f64> = self.0.iter().map(|(_, d)| *d).collect();+ let max = v.iter().copied().fold(f64::MIN, f64::max);+ let min = v.iter().copied().fold(f64::MAX, f64::min);+ if v.is_empty() { 0.0 } else { max - min }+ }++ /// Least-squares slope of offset over truth time, in ms per minute.+ fn drift_ms_per_min(&self) -> f64 {+ let n = self.0.len() as f64;+ if n < 3.0 {+ return 0.0;+ }+ let mx = self.0.iter().map(|(t, _)| t).sum::<f64>() / n;+ let my = self.0.iter().map(|(_, d)| d).sum::<f64>() / n;+ let sxy: f64 = self.0.iter().map(|(t, d)| (t - mx) * (d - my)).sum();+ let sxx: f64 = self.0.iter().map(|(t, _)| (t - mx).powi(2)).sum();+ if sxx == 0.0 { 0.0 } else { sxy / sxx * 60_000.0 }+ }++ fn describe(&self) -> String {+ format!(+ "n={} median {:+.1} ms, spread {:.1} ms, drift {:+.2} ms/min",+ self.0.len(),+ self.median().unwrap_or(f64::NAN) * 1000.0,+ self.spread() * 1000.0,+ self.drift_ms_per_min()+ )+ }+}++fn first_presented(events: &[StimulusEvent], reference: u64) -> Truth {+ let mut truth = Truth::new();+ for event in events {+ if let StimulusEvent::Frame { index, host } = event {+ truth.entry(*index).or_insert(host_secs(*host, reference));+ }+ }+ truth+}++struct VideoCheck<'a> {+ name: &'a str,+ kind: StreamKind,+ truth: &'a Truth,+ /// Indices that were never delivered to the recorder.+ not_sent: &'a [u32],+ /// Indices the source showed too briefly for the capture rate to+ /// guarantee a sample (screen only).+ may_skip: Box<dyn Fn(u32) -> bool + 'a>,+ allow_duplicates: bool,+}++fn check_video(case: &mut Case, track: &Track, check: VideoCheck) -> anyhow::Result<Offsets> {+ let mut frames: Vec<(f64, Readout)> = Vec::new();+ media::decode_video(&track.path, |picture| {+ let readout = video_code::read_stream(&picture.view(), check.kind);+ frames.push((picture.pts_secs, readout));+ })?;+ let name = check.name;+ if frames.is_empty() {+ case.problem(format!("{name}: no frames"));+ return Ok(Offsets::default());+ }+ let mut bad = Vec::new();+ let mut indices = Vec::new();+ for (i, (pts, readout)) in frames.iter().enumerate() {+ match readout {+ Readout::Marker { marker, .. } if marker.kind == check.kind && marker.segment == 0 => {+ indices.push((*pts, marker.index));+ }+ other => bad.push(format!("frame {i} at {pts:.3}s: {other:?}")),+ }+ }+ if !bad.is_empty() {+ case.problem(format!(+ "{name}: {} of {} frames carry no valid marker, first: {}",+ bad.len(),+ frames.len(),+ bad.iter().take(3).cloned().collect::<Vec<_>>().join("; ")+ ));+ }+ let mut backwards = Vec::new();+ let mut duplicates = 0;+ let mut pts_problems = Vec::new();+ for w in indices.windows(2) {+ let ((pa, a), (pb, b)) = (w[0], w[1]);+ if pb <= pa {+ pts_problems.push(format!("{pa:.4}->{pb:.4}"));+ }+ if b < a {+ backwards.push(format!("{a}->{b} at {pb:.3}s"));+ } else if b == a {+ duplicates += 1;+ }+ }+ if !pts_problems.is_empty() {+ case.problem(format!(+ "{name}: non-increasing pts x{}: {}",+ pts_problems.len(),+ pts_problems.iter().take(3).cloned().collect::<Vec<_>>().join(", ")+ ));+ }+ if !backwards.is_empty() {+ case.problem(format!(+ "{name}: frame index went backwards x{}: {}",+ backwards.len(),+ backwards.iter().take(3).cloned().collect::<Vec<_>>().join(", ")+ ));+ }+ if duplicates > 0 && !check.allow_duplicates {+ case.problem(format!("{name}: {duplicates} duplicated frames"));+ }+ let mut first_seen: BTreeMap<u32, f64> = BTreeMap::new();+ for &(pts, index) in &indices {+ first_seen.entry(index).or_insert(pts);+ }+ let unknown: Vec<u32> = first_seen+ .keys()+ .filter(|i| !check.truth.contains_key(i))+ .copied()+ .collect();+ if !unknown.is_empty() {+ case.problem(format!(+ "{name}: {} recorded indices were never produced: {:?}",+ unknown.len(),+ &unknown[..unknown.len().min(8)]+ ));+ }+ let (Some(&lo), Some(&hi)) = (first_seen.keys().next(), first_seen.keys().next_back()) else {+ return Ok(Offsets::default());+ };+ let mut skipped = Vec::new();+ let mut explained = 0;+ for &index in check.truth.keys().filter(|&&i| i > lo && i < hi) {+ if first_seen.contains_key(&index) || check.not_sent.contains(&index) {+ continue;+ }+ if (check.may_skip)(index) {+ explained += 1;+ } else {+ skipped.push(index);+ }+ }+ if !skipped.is_empty() {+ case.problem(format!(+ "{name}: {} produced frames missing from the recording: {:?}",+ skipped.len(),+ &skipped[..skipped.len().min(12)]+ ));+ }+ if let Some(dir) = std::env::var_os("CAP_VERIFY_DUMP") {+ let rows: Vec<String> = indices+ .iter()+ .map(|(pts, i)| {+ let truth = check.truth.get(i).copied().unwrap_or(f64::NAN);+ format!("{pts:.6},{i},{truth:.6},{:.6}", pts + track.start - truth)+ })+ .collect();+ let _ = std::fs::write(+ Path::new(&dir).join(format!("{}-{name}.csv", case.id.replace('/', "-"))),+ format!("pts,index,truth,offset\n{}\n", rows.join("\n")),+ );+ }+ let mut offsets = Offsets::default();+ let mut snapped = Vec::new();+ for (&index, &pts) in &first_seen {+ if index == 0 {+ continue;+ }+ let Some(&truth) = check.truth.get(&index) else {+ continue;+ };+ // The documented cadence rule (#2573): after a source gap the first+ // frame may stay on the skipped slot, so it can land up to the gap+ // early. It is reported, and excluded from the steady-state offsets.+ let gap = index.checked_sub(1).map_or(0, |prev| {+ (0..prev)+ .rev()+ .take_while(|i| check.not_sent.contains(i))+ .count()+ });+ if gap > 0 {+ snapped.push((index, pts + track.start - truth));+ continue;+ }+ offsets.0.push((truth, pts + track.start - truth));+ }+ if let Some(median) = offsets.median() {+ let period = 1.0 / f64::from(CAMERA_FPS);+ for &(index, offset) in &snapped {+ let early = median - offset;+ let gap = (0..index)+ .rev()+ .take_while(|i| check.not_sent.contains(i))+ .count() as f64;+ if early < -0.004 || early > gap * period + 0.004 {+ case.problem(format!(+ "{name}: frame {index} after a {gap}-frame source gap is {:+.1} ms off the steady offset (allowed 0..{:.1} early)",+ -early * 1000.0,+ gap * period * 1000.0+ ));+ }+ }+ if !snapped.is_empty() {+ case.note(format!(+ "{name}: first frames after source gaps: {}",+ snapped+ .iter()+ .map(|(i, o)| format!("{i} {:+.1} ms", (o - median) * 1000.0))+ .collect::<Vec<_>>()+ .join(", ")+ ));+ }+ }+ case.note(format!(+ "{name}: {} frames, indices {lo}..={hi}, {} distinct, {duplicates} repeats, {} skips explained by display timing, offset {}",+ frames.len(),+ first_seen.len(),+ explained,+ offsets.describe()+ ));+ Ok(offsets)+}++struct AudioCheck<'a> {+ name: &'a str,+ source: AudioSource,+ truth: &'a Truth,+ /// Truth window inside which every marker must be present.+ window: (f64, f64),+}++fn check_audio(+ case: &mut Case,+ decoded: &media::DecodedAudio,+ start: f64,+ check: AudioCheck,+) -> Offsets {+ let name = check.name;+ let mono = decoded.mono();+ let located = audio_code::locate_markers(&mono, decoded.sample_rate, check.source);+ let mut found: BTreeMap<u32, f64> = BTreeMap::new();+ let mut damaged = 0;+ let mut repeated = Vec::new();+ for marker in &located {+ let at = marker.time_secs + decoded.start_secs + start;+ let Some(index) = marker.index else {+ let track_secs = mono.len() as f64 / f64::from(decoded.sample_rate);+ if marker.time_secs > 0.15 && marker.time_secs + audio_code::MARKER_SECS < track_secs - 0.15 {+ damaged += 1;+ }+ continue;+ };+ if found.insert(index, at).is_some() {+ repeated.push(index);+ }+ }+ if damaged > 0 {+ case.problem(format!(+ "{name}: {damaged} markers inside the track located but undecodable"+ ));+ }+ if !repeated.is_empty() {+ case.problem(format!("{name}: markers repeated {repeated:?}"));+ }+ let unknown: Vec<u32> = found+ .keys()+ .filter(|i| !check.truth.contains_key(i))+ .copied()+ .collect();+ if !unknown.is_empty() {+ case.problem(format!("{name}: unexpected marker indices {unknown:?}"));+ }+ let missing: Vec<u32> = check+ .truth+ .iter()+ .filter(|(i, t)| **t > check.window.0 && **t < check.window.1 && !found.contains_key(i))+ .map(|(i, _)| *i)+ .collect();+ if !missing.is_empty() {+ case.problem(format!(+ "{name}: {} markers inside the recording window are missing: {:?}",+ missing.len(),+ &missing[..missing.len().min(12)]+ ));+ }+ let mut order = found.iter().collect::<Vec<_>>();+ order.sort_by(|a, b| a.1.total_cmp(b.1));+ if order.windows(2).any(|w| w[1].0 < w[0].0) {+ case.problem(format!("{name}: markers out of order"));+ }+ if let Some(dir) = std::env::var_os("CAP_VERIFY_DUMP") {+ let rows: Vec<String> = located+ .iter()+ .map(|m| {+ let at = m.time_secs + decoded.start_secs + start;+ let truth = m+ .index+ .and_then(|i| check.truth.get(&i))+ .copied()+ .unwrap_or(f64::NAN);+ format!("{at:.6},{:?},{truth:.6},{:.3}", m.index, m.correlation)+ })+ .chain(+ check+ .truth+ .iter()+ .map(|(i, t)| format!("truth,{i},{t:.6},")),+ )+ .collect();+ let _ = std::fs::write(+ Path::new(&dir).join(format!("{}-{name}.csv", case.id.replace('/', "-"))),+ format!(+ "at,index,truth,correlation (start {start:.6}, file start {:.6}, window {:.3}..{:.3})\n{}\n",+ decoded.start_secs,+ check.window.0,+ check.window.1,+ rows.join("\n")+ ),+ );+ }+ let mut offsets = Offsets::default();+ for (index, at) in &found {+ if let Some(&truth) = check.truth.get(index) {+ offsets.0.push((truth, at - truth));+ }+ }+ case.note(format!(+ "{name}: {} markers ({} expected in window), offset {}",+ found.len(),+ check+ .truth+ .values()+ .filter(|t| **t > check.window.0 && **t < check.window.1)+ .count(),+ offsets.describe()+ ));+ offsets+}++fn analyze(spec: &Spec, rec: &Recorded, reference: u64, case: &mut Case) -> anyhow::Result<()> {+ let layout = layout(&rec.project, case)?;+ let screen_truth = first_presented(&rec.stimulus, reference);+ let presented: Vec<(u32, f64)> = screen_truth.iter().map(|(i, t)| (*i, *t)).collect();+ let capture_interval = 1.0 / f64::from(rec.max_fps);+ // SCK delivers a changed display at most once per minimum frame+ // interval. A picture is guaranteed to be sampled only when it stays on+ // screen for a full interval plus one display refresh of phase.+ let refresh = 1.0 / 60.0;+ let on_screen: BTreeMap<u32, f64> = presented+ .windows(2)+ .map(|w| (w[0].0, w[1].1 - w[0].1))+ .collect();+ let screen = check_video(+ case,+ &layout.display,+ VideoCheck {+ name: "screen",+ kind: StreamKind::Screen,+ truth: &screen_truth,+ not_sent: &[],+ may_skip: Box::new(|index| {+ on_screen+ .get(&index)+ .is_some_and(|d| *d < capture_interval + refresh)+ }),+ allow_duplicates: true,+ },+ )?;+ let camera = match (&layout.camera, &rec.camera) {+ (Some(track), Some((truth, dropped))) => {+ case.note(format!("camera: dropped at source {dropped:?}"));+ Some(check_video(+ case,+ track,+ VideoCheck {+ name: "camera",+ kind: StreamKind::Camera,+ truth,+ not_sent: dropped,+ may_skip: Box::new(|_| false),+ allow_duplicates: false,+ },+ )?)+ }+ (None, Some(_)) => {+ case.problem("camera feed attached but no camera track recorded");+ None+ }+ _ => None,+ };+ let window = (rec.started + 0.3, rec.stopped - 0.3);+ // ScreenCaptureKit starts delivering system audio some time after the+ // stream starts (measured 0.2-0.6 s); Cap fills that span with silence.+ let system_window = (rec.started + SYSTEM_AUDIO_STARTUP, window.1);+ let system_truth: Truth = rec+ .stimulus+ .iter()+ .filter_map(|e| match e {+ StimulusEvent::Audio { index, host } => Some((*index, host_secs(*host, reference))),+ _ => None,+ })+ .collect();+ let audio_unavailable = rec.stimulus.iter().find_map(|e| match e {+ StimulusEvent::AudioUnavailable { reason } => Some(reason.clone()),+ _ => None,+ });+ if let Some(reason) = &audio_unavailable {+ case.note(format!("system audio stimulus unavailable: {reason}"));+ if env_flag("CAP_VERIFY_REQUIRE_SYSTEM_AUDIO") {+ case.problem(format!("system audio required but unavailable: {reason}"));+ }+ }+ let (mic, system) = if layout.muxed_audio {+ let decoded = media::decode_audio(&layout.display.path, Some(48_000))?;+ let mic = check_audio(+ case,+ &decoded,+ 0.0,+ AudioCheck {+ name: "mic",+ source: AudioSource::Mic,+ truth: &rec.mic_truth,+ window,+ },+ );+ let system = (!system_truth.is_empty()).then(|| {+ check_audio(+ case,+ &decoded,+ 0.0,+ AudioCheck {+ name: "system",+ source: AudioSource::System,+ truth: &system_truth,+ window: system_window,+ },+ )+ });+ (Some(mic), system)+ } else {+ let mic = match &layout.mic {+ Some(track) => {+ let decoded = media::decode_audio(&track.path, Some(48_000))?;+ Some(check_audio(+ case,+ &decoded,+ track.start,+ AudioCheck {+ name: "mic",+ source: AudioSource::Mic,+ truth: &rec.mic_truth,+ window,+ },+ ))+ }+ None => {+ case.problem("no mic track recorded");+ None+ }+ };+ let system = match (&layout.system, system_truth.is_empty()) {+ (Some(track), false) => {+ let decoded = media::decode_audio(&track.path, Some(48_000))?;+ Some(check_audio(+ case,+ &decoded,+ track.start,+ AudioCheck {+ name: "system",+ source: AudioSource::System,+ truth: &system_truth,+ window: system_window,+ },+ ))+ }+ (None, false) => {+ case.problem("system audio was playing but no system audio track was recorded");+ None+ }+ _ => None,+ };+ (mic, system)+ };+ sync_checks(spec, case, &screen, camera.as_ref(), mic.as_ref(), system.as_ref());+ Ok(())+}++/// Tolerances, from first principles:+/// - Synthetic camera and microphone carry exact capture times, so their+/// offsets must be constant (spread within one audio chunk / encoder+/// rounding) and must agree with each other to within one camera frame+/// period's rounding.+/// - The screen truth is the time the stimulus handed a frame to the window+/// server; the display shows it at a later refresh, so the screen offset may+/// exceed the exact sources' by up to a few refresh intervals, never less.+fn sync_checks(+ _spec: &Spec,+ case: &mut Case,+ screen: &Offsets,+ camera: Option<&Offsets>,+ mic: Option<&Offsets>,+ system: Option<&Offsets>,+) {+ const EXACT_SPREAD: f64 = 0.004;+ const CAMERA_SPREAD: f64 = 0.012;+ const CAMERA_VS_MIC: f64 = 0.020;+ const SCREEN_LATENCY: (f64, f64) = (-0.025, 0.060);+ const SYSTEM_VS_SCREEN: f64 = 0.080;++ let ms = |s: f64| s * 1000.0;+ if let Some(mic) = mic+ && mic.spread() > EXACT_SPREAD+ {+ case.problem(format!(+ "mic: marker offsets vary by {:.1} ms (limit {:.1}): audio timing is not continuous",+ ms(mic.spread()),+ ms(EXACT_SPREAD)+ ));+ }+ if let Some(camera) = camera+ && camera.spread() > CAMERA_SPREAD+ {+ case.problem(format!(+ "camera: frame offsets vary by {:.1} ms (limit {:.1})",+ ms(camera.spread()),+ ms(CAMERA_SPREAD)+ ));+ }+ let (Some(mic_m), screen_m) = (mic.and_then(Offsets::median), screen.median()) else {+ return;+ };+ if let Some(camera_m) = camera.and_then(Offsets::median) {+ let d = camera_m - mic_m;+ case.note(format!("sync camera-mic {:+.1} ms", ms(d)));+ if d.abs() > CAMERA_VS_MIC {+ case.problem(format!(+ "camera vs mic out of sync: {:+.1} ms (limit +-{:.0})",+ ms(d),+ ms(CAMERA_VS_MIC)+ ));+ }+ }+ if let Some(screen_m) = screen_m {+ let d = screen_m - mic_m;+ case.note(format!("sync screen-mic {:+.1} ms", ms(d)));+ if d < SCREEN_LATENCY.0 || d > SCREEN_LATENCY.1 {+ case.problem(format!(+ "screen vs mic out of sync: {:+.1} ms (allowed {:+.0}..{:+.0}: display latency)",+ ms(d),+ ms(SCREEN_LATENCY.0),+ ms(SCREEN_LATENCY.1)+ ));+ }+ if let Some(system_m) = system.and_then(Offsets::median) {+ let d = system_m - screen_m;+ case.note(format!("sync system-screen {:+.1} ms", ms(d)));+ if d.abs() > SYSTEM_VS_SCREEN {+ case.problem(format!(+ "system audio vs screen out of sync: {:+.1} ms (limit +-{:.0})",+ ms(d),+ ms(SYSTEM_VS_SCREEN)+ ));+ }+ }+ }+}