Open source Loom alternative. Beautiful, shareable screen recordings.
Rust appcapcossloommacnextjsnextjs15open-sourceossreactrecordrustscreen-capturescreen-recorderscreenshotsolidjstauritauri-apptypescriptvite
test(verify): real-capture Focus case that must keep a window in front out of a window-only recording
Richie McIlroy727ccabc05f5
diff --git a/crates/verify-stimulus/src/main.rs b/crates/verify-stimulus/src/main.rsindex 44ff9124d..055e40ec0 100644--- a/crates/verify-stimulus/src/main.rs+++ b/crates/verify-stimulus/src/main.rs@@ -114,6 +114,7 @@ mod app {go_at: None,window: None,surface: None,+ occluder: None,size: (0, 0),shown: None,ready: false,@@ -340,6 +341,8 @@ mod app {go_at: Option<(Instant, u64)>,window: Option<Arc<Window>>,surface: Option<canvas::Canvas>,+ /// The magenta window in front of the stimulus, when the spec asks.+ occluder: Option<(Arc<Window>, canvas::Canvas)>,size: (u32, u32),shown: Option<u32>,ready: bool,@@ -663,6 +666,31 @@ mod app {}window.request_redraw();self.window = Some(window);+ if let Some((x, y, w, h)) = self.spec.occluder {+ let attrs = Window::default_attributes()+ .with_title(cap_verify::stimulus::OCCLUDER_TITLE)+ .with_window_level(WindowLevel::AlwaysOnTop)+ .with_decorations(false)+ .with_resizable(false)+ .with_position(LogicalPosition::new(x, y))+ .with_inner_size(LogicalSize::new(w, h));+ match event_loop+ .create_window(attrs)+ .map_err(|e| e.to_string())+ .and_then(|window| {+ let window = Arc::new(window);+ canvas::new(&window).map(|canvas| (window, canvas))+ }) {+ Ok((window, canvas)) => {+ window.request_redraw();+ self.occluder = Some((window, canvas));+ }+ Err(e) => {+ self.error = Some(format!("occluder: {e}"));+ event_loop.exit();+ }+ }+ }}fn user_event(&mut self, event_loop: &ActiveEventLoop, command: Command) {@@ -681,12 +709,23 @@ mod app {}}- fn window_event(- &mut self,- event_loop: &ActiveEventLoop,- _id: WindowId,- event: WindowEvent,- ) {+ fn window_event(&mut self, event_loop: &ActiveEventLoop, id: WindowId, event: WindowEvent) {+ if let Some((window, canvas)) = self+ .occluder+ .as_mut()+ .filter(|(window, _)| window.id() == id)+ {+ if let WindowEvent::RedrawRequested = event {+ let size = window.inner_size();+ // BGRA magenta: nothing the stimulus draws has colour.+ canvas::present(canvas, size.width.max(1), size.height.max(1), |bytes| {+ for pixel in bytes.chunks_exact_mut(4) {+ pixel.copy_from_slice(&[255, 0, 255, 255]);+ }+ });+ }+ return;+ }match event {WindowEvent::CloseRequested => event_loop.exit(),WindowEvent::Resized(size) => {diff --git a/crates/verify/src/stimulus.rs b/crates/verify/src/stimulus.rsindex d43e7eef6..2bcb90d38 100644--- a/crates/verify/src/stimulus.rs+++ b/crates/verify/src/stimulus.rs@@ -33,8 +33,15 @@ pub struct StimulusSpec {/// `None` = borderless fullscreen on the primary display; otherwise a/// window at (x, y, width, height) in points.pub window: Option<(i32, i32, u32, u32)>,+ /// A solid magenta window at (x, y, width, height) in points, in front+ /// of the stimulus window: what a window-only capture must leave out.+ #[serde(default)]+ pub occluder: Option<(i32, i32, u32, u32)>,}+/// The occluder's title, for the oracle to find its window.+pub const OCCLUDER_TITLE: &str = "Cap Verify Occluder";+impl StimulusSpec {pub fn frame_count(&self) -> u32 {(self.seconds * self.fps).round() as u32diff --git a/crates/verify/tests/recording.rs b/crates/verify/tests/recording.rsindex b88caa195..b00398df3 100644--- a/crates/verify/tests/recording.rs+++ b/crates/verify/tests/recording.rs@@ -52,7 +52,7 @@ use cap_verify::{SENTINEL_FILE, SentinelLog, StimulusEvent, StimulusProcess, StimulusSpec, host_add_secs,host_now, host_secs,},- video_code::{self, FrameMarker, Readout, StreamKind},+ video_code::{self, FrameMarker, PixelSource as _, Readout, StreamKind},};use platform::{Shareable, host_timestamp};@@ -198,10 +198,30 @@ struct Spec {mic_delay: f64,mic: bool,system_audio: bool,+ /// Focus: record the stimulus window by itself (window-only capture)+ /// with a magenta window in front of it, which must stay out.+ focus: bool,}fn specs() -> Vec<Spec> {let mut specs = vec![+ Spec {+ id: "recording/studio-window/focus",+ tier: Tier::Fast,+ mode: Mode::Studio {+ fragmented: true,+ camera: false,+ },+ seconds: 6.0,+ stimulus_fps: 20.0,+ holds: vec![],+ camera_drops_at: vec![],+ gated: false,+ mic_delay: 0.0,+ mic: true,+ system_audio: false,+ focus: true,+ },Spec {id: "recording/studio-screen/fragmented",tier: Tier::Fast,@@ -217,6 +237,7 @@ fn specs() -> Vec<Spec> {mic_delay: 0.0,mic: true,system_audio: true,+ focus: false,},Spec {id: "recording/studio-camera/startup-drops",@@ -233,6 +254,7 @@ fn specs() -> Vec<Spec> {mic_delay: 0.0,mic: true,system_audio: true,+ focus: false,},Spec {id: "recording/instant/screen-mic-system",@@ -246,6 +268,7 @@ fn specs() -> Vec<Spec> {mic_delay: 0.0,mic: true,system_audio: true,+ focus: false,},Spec {id: "recording/studio-screen/gated",@@ -262,6 +285,7 @@ fn specs() -> Vec<Spec> {mic_delay: 0.0,mic: true,system_audio: true,+ focus: false,},Spec {id: "recording/instant/gated",@@ -275,6 +299,7 @@ fn specs() -> Vec<Spec> {mic_delay: 0.0,mic: true,system_audio: true,+ focus: false,},Spec {id: "recording/instant/late-mic",@@ -288,6 +313,7 @@ fn specs() -> Vec<Spec> {mic_delay: 1.0,mic: true,system_audio: false,+ focus: false,},Spec {id: "recording/instant/system-only",@@ -301,6 +327,7 @@ fn specs() -> Vec<Spec> {mic_delay: 0.0,mic: false,system_audio: true,+ focus: false,},Spec {id: "recording/studio-screen/late-mic",@@ -317,6 +344,7 @@ fn specs() -> Vec<Spec> {mic_delay: 0.3,mic: true,system_audio: true,+ focus: false,},Spec {id: "recording/studio-recovery/crash",@@ -330,6 +358,7 @@ fn specs() -> Vec<Spec> {mic_delay: 0.0,mic: true,system_audio: true,+ focus: false,},Spec {id: "recording/studio-screen/mp4-holds",@@ -346,6 +375,7 @@ fn specs() -> Vec<Spec> {mic_delay: 0.0,mic: true,system_audio: true,+ focus: false,},Spec {id: "recording/studio-camera/long",@@ -362,6 +392,7 @@ fn specs() -> Vec<Spec> {mic_delay: 0.0,mic: true,system_audio: true,+ focus: false,},Spec {id: "recording/instant/long-holds",@@ -375,6 +406,7 @@ fn specs() -> Vec<Spec> {mic_delay: 0.0,mic: true,system_audio: true,+ focus: false,},];specs.extend(soak_specs());@@ -420,6 +452,7 @@ fn soak_specs() -> Vec<Spec> {mic_delay: 0.0,mic: true,system_audio: true,+ focus: false,},Spec {id: "recording/soak/instant",@@ -433,6 +466,7 @@ fn soak_specs() -> Vec<Spec> {mic_delay: 0.0,mic: true,system_audio: true,+ focus: false,},]}@@ -558,6 +592,7 @@ fn real_capture() {mic_delay: 0.0,mic: true,system_audio: true,+ focus: false,};let mut case = Case::new(warmup.id);let started = std::time::Instant::now();@@ -1300,6 +1335,8 @@ struct Recorded {/// Recovery after a crash may lose at most this many seconds before the/// crash (the in-flight fragment).loss_budget: Option<f64>,+ /// A Focus case: the screen track must be the stimulus window alone.+ focus: bool,}async fn run_case(@@ -2180,6 +2217,12 @@ async fn record(reference: u64,case: &mut Case,) -> anyhow::Result<Recorded> {+ // Focus records one window, whatever the host's default capture is.+ let capture = if spec.focus {+ CaptureMode::Window+ } else {+ capture+ };let system_output = platform::system_audio_output();if spec.system_audio && require_system_audio() && !system_output {case.problem(@@ -2203,6 +2246,9 @@ async fn record(CaptureMode::Display => None,CaptureMode::Window => Some((80, 120, 640, 400)),},+ // Over the stimulus window's lower right, where a region capture+ // would record it.+ occluder: spec.focus.then_some((480, 320, 360, 260)),};let mut stimulus = tokio::task::block_in_place(|| {StimulusProcess::spawn(@@ -2212,7 +2258,16 @@ async fn record()})?;tokio::task::block_in_place(|| stimulus.wait_for("ready", Duration::from_secs(30)))?;- let (target, excluded, shareable) = capture_setup(capture).await?;+ let (target, mut excluded, shareable) = capture_setup(capture).await?;+ if spec.focus {+ // Left in the picture on purpose: the window-only capture itself must+ // keep it out, not the exclusion list a region capture relies on.+ if let Some(occluder) = find_window(cap_verify::stimulus::OCCLUDER_TITLE) {+ excluded.retain(|id| *id != occluder);+ } else {+ case.problem("missing", "focus: the occluder window never appeared");+ }+ }let camera = matches!(spec.mode, Mode::Studio { camera: true, .. });let feeds = start_feeds(@@ -2250,7 +2305,11 @@ async fn record(camera,system_audio,);- let mut builder = builder;+ // `CAP_VERIFY_FOCUS_OFF=1` records a Focus case as a region+ // instead, which the Focus checks must catch: the control that+ // shows they can fail.+ let mut builder =+ builder.with_window_only(spec.focus && !env_flag("CAP_VERIFY_FOCUS_OFF"));if spec.mic {builder = builder.with_mic_feed(mic_lock.clone());}@@ -2414,6 +2473,7 @@ async fn record(sources,sentinel,loss_budget,+ focus: spec.focus,})}@@ -3126,6 +3186,105 @@ impl VideoCheck<'_> {/// recorded picture is the first one presented, 9 to 69 ms after the start.const START_COVERAGE_SECS: f64 = 0.3;+/// A Focus recording's screen track is the stimulus window and nothing else:+/// no magenta from the occluder in front of it (the screen key is blue, the+/// noise grey, so strong positive Cr is the occluder alone), no black column+/// or row along an edge (the window drawn smaller than its frame), and the+/// window's own size, less the rounded corners macOS trims off.+fn check_focus(case: &mut Case, track: &Track, stimulus: &[StimulusEvent]) -> anyhow::Result<()> {+ let Some((width, height, scale)) = stimulus.iter().find_map(|event| match event {+ StimulusEvent::Ready {+ width,+ height,+ scale,+ ..+ } => Some((*width, *height, *scale)),+ _ => None,+ }) else {+ case.problem("missing", "focus: the stimulus never reported its size");+ return Ok(());+ };+ #[cfg(target_os = "macos")]+ let trim =+ 2 * (cap_recording::sources::screen_capture::WINDOW_CORNER_TRIM * scale).round() as u32;+ #[cfg(not(target_os = "macos"))]+ let trim = {+ let _ = scale;+ 0+ };+ let expected = (width.saturating_sub(trim), height.saturating_sub(trim));++ let mut frames = 0usize;+ let mut sizes = BTreeMap::new();+ let mut leaks = Vec::new();+ let mut edges = Vec::new();+ media::decode_video(&track.path, |picture| {+ let view = picture.view();+ let (w, h) = (view.width(), view.height());+ *sizes.entry((w, h)).or_insert(0usize) += 1;+ frames += 1;+ let mut magenta = 0usize;+ for y in (0..h).step_by(2) {+ for x in (0..w).step_by(2) {+ let (cb, cr) = view.chroma(x, y);+ if cr > 60.0 && cb > 30.0 {+ magenta += 1;+ }+ }+ }+ if magenta > 0 && leaks.len() < 3 {+ leaks.push(format!("{:.3}s: {magenta} pixels", picture.pts_secs));+ }+ let black_column = |x: usize| (0..h).all(|y| view.luma(x, y) < 20.0);+ let black_row = |y: usize| (0..w).all(|x| view.luma(x, y) < 20.0);+ let empty = [+ ("left", black_column(0)),+ ("right", black_column(w - 1)),+ ("top", black_row(0)),+ ("bottom", black_row(h - 1)),+ ];+ if let Some((side, _)) = empty.iter().find(|(_, black)| *black)+ && edges.len() < 3+ {+ edges.push(format!("{:.3}s: {side} edge", picture.pts_secs));+ }+ })?;++ if frames == 0 {+ case.problem("missing", "focus: no screen frames");+ return Ok(());+ }+ if !leaks.is_empty() {+ case.problem(+ "focus-leak",+ format!("the window in front was recorded: {}", leaks.join("; ")),+ );+ }+ if !edges.is_empty() {+ case.problem(+ "focus-edge",+ format!("an empty strip along the frame: {}", edges.join("; ")),+ );+ }+ // Frame sizes are rounded to even dimensions for the encoder.+ let near = |(w, h): (usize, usize)| {+ (w as i64 - expected.0 as i64).abs() <= 2 && (h as i64 - expected.1 as i64).abs() <= 2+ };+ if !sizes.keys().all(|size| near(*size)) {+ case.problem(+ "focus-size",+ format!(+ "expected {}x{} (the window less {trim} px of corners), recorded {sizes:?}",+ expected.0, expected.1+ ),+ );+ }+ case.note(format!(+ "focus: {frames} frames at {sizes:?}, window {width}x{height} @{scale}x"+ ));+ Ok(())+}+fn check_video(case: &mut Case, track: &Track, mut check: VideoCheck) -> anyhow::Result<Offsets> {let mut frames: Vec<(f64, Readout)> = Vec::new();media::decode_video(&track.path, |picture| {@@ -3733,6 +3892,9 @@ fn analyze(rec: &Recorded, reference: u64, case: &mut Case) -> anyhow::Result<()observed,},)?;+ if rec.focus {+ check_focus(case, &layout.display, &rec.stimulus)?;+ }let camera = match (&layout.camera, &rec.camera) {(Some(track), Some((truth, dropped))) if track.path.is_file() => {case.note(format!("camera: dropped at source {dropped:?}"));