Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

ci(verify): bring the real-capture oracle up on hosted Windows

Richie McIlroyc79da0b2573e
diff --git a/.github/workflows/verify-recording-windows.yml b/.github/workflows/verify-recording-windows.yml
new file mode 100644
index 000000000..9c155f5c9
--- /dev/null
+++ b/.github/workflows/verify-recording-windows.yml
@@ -0,0 +1,80 @@
+name: Verify recording (Windows, bring-up)
+
+# Bring-up of the real-capture oracle (crates/verify/tests/recording.rs) on
+# GitHub-hosted Windows: its jobs run in the runner's interactive console
+# session, where Windows Graphics Capture records the desktop. Blacksmith
+# Windows VMs run jobs in session 0, which has no desktop to capture
+# (probe runs 38997642473, 38997747111).
+
+on:
+ push:
+ branches: [ci/windows-real-capture]
+ workflow_dispatch:
+ inputs:
+ tier:
+ description: "fast or full"
+ default: "fast"
+ case_filter:
+ description: "Case globs, comma-separated (empty runs all)"
+ default: ""
+
+concurrency:
+ group: verify-recording-windows-${{ github.ref_name }}
+ cancel-in-progress: true
+
+permissions:
+ contents: read
+
+jobs:
+ real-capture:
+ runs-on: windows-2022
+ timeout-minutes: 90
+ env:
+ CAP_VERIFY_CAPTURE: display
+ CAP_VERIFY_REQUIRE_CAPTURE: "1"
+ CAP_VERIFY_TIER: ${{ inputs.tier || 'fast' }}
+ CAP_VERIFY_CASE: ${{ inputs.case_filter || '' }}
+ CAP_VERIFY_SCRATCH: ${{ github.workspace }}/verify-scratch
+ CAP_VERIFY_REPORT_DIR: ${{ github.workspace }}/verify-report
+ CAP_VERIFY_DUMP: ${{ github.workspace }}/verify-dump
+ CAP_VERIFY_HOST: github-hosted
+ # No audio endpoints on hosted Windows: nothing plays or loops back
+ # system audio.
+ CAP_VERIFY_STIMULUS_AUDIO: "0"
+ steps:
+ - uses: actions/checkout@v4
+ with:
+ persist-credentials: false
+
+ - name: Setup
+ uses: ./.github/actions/engine-verify-setup
+ with:
+ target: x86_64-pc-windows-msvc
+ tier: fast
+
+ - name: Build
+ shell: bash
+ run: |
+ start=$(date +%s)
+ cargo build --locked --profile ci-verify -p cap-verify-stimulus
+ cargo test --locked --profile ci-verify -p cap-verify --test recording --no-run
+ echo "build seconds: $(( $(date +%s) - start ))"
+
+ - name: Real-capture recording oracle
+ shell: bash
+ run: cargo test --locked --profile ci-verify -p cap-verify --test recording -- --nocapture
+
+ - name: Summary
+ if: always()
+ shell: bash
+ run: python scripts/verify/summary.py --title "real capture windows-2022" "$CAP_VERIFY_REPORT_DIR" >> "$GITHUB_STEP_SUMMARY" || true
+
+ - uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4.6.2
+ if: always()
+ with:
+ name: verify-recording-windows-2022
+ path: |
+ ${{ github.workspace }}/verify-report
+ ${{ github.workspace }}/verify-dump
+ ${{ github.workspace }}/verify-scratch
+ if-no-files-found: ignore
diff --git a/Cargo.lock b/Cargo.lock
index 2e6a9faee..38bfa138d 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -2157,16 +2157,19 @@ version = "0.1.0"
dependencies = [
"anyhow",
"cap-audio",
+ "cap-camera-windows",
"cap-editor",
"cap-enc-avfoundation",
"cap-export",
"cap-media-info",
+ "cap-mediafoundation-utils",
"cap-project",
"cap-recording",
"cap-rendering",
"cap-timestamp",
"cap-verify-expect",
"cidre",
+ "cpal 0.15.3 (git+https://github.com/CapSoftware/cpal?rev=6013cb5f8bd3)",
"ffmpeg-next",
"flume",
"futures",
@@ -2178,6 +2181,7 @@ dependencies = [
"tempfile",
"tokio",
"tracing-subscriber",
+ "windows 0.60.0",
"workspace-hack",
]
diff --git a/crates/verify-stimulus/Cargo.toml b/crates/verify-stimulus/Cargo.toml
index 81905b474..8706b3942 100644
--- a/crates/verify-stimulus/Cargo.toml
+++ b/crates/verify-stimulus/Cargo.toml
@@ -26,3 +26,8 @@ objc2-foundation = "0.3.1"
scap-screencapturekit = { path = "../scap-screencapturekit" }
softbuffer = "0.4"
winit = "0.30"
+
+[target.'cfg(windows)'.dependencies]
+cpal = { workspace = true }
+softbuffer = "0.4"
+winit = "0.30"
diff --git a/crates/verify-stimulus/src/main.rs b/crates/verify-stimulus/src/main.rs
index 528d9692f..84f5e0333 100644
--- a/crates/verify-stimulus/src/main.rs
+++ b/crates/verify-stimulus/src/main.rs
@@ -1,12 +1,12 @@
//! `cap-verify-stimulus <spec json> <log path>`: see `cap_verify::stimulus`.
-#[cfg(not(target_os = "macos"))]
+#[cfg(not(any(target_os = "macos", windows)))]
fn main() {
- eprintln!("cap-verify-stimulus only runs on macOS");
+ eprintln!("cap-verify-stimulus only runs on macOS and Windows");
std::process::exit(2);
}
-#[cfg(target_os = "macos")]
+#[cfg(any(target_os = "macos", windows))]
fn main() {
if let Err(e) = app::run() {
println!("error {e}");
@@ -14,7 +14,7 @@ fn main() {
}
}
-#[cfg(target_os = "macos")]
+#[cfg(any(target_os = "macos", windows))]
mod app {
use std::{
fs::File,
@@ -36,6 +36,7 @@ mod app {
video_code::{self, FrameMarker, PixelOrder, StreamKind},
};
use cpal::traits::{DeviceTrait, HostTrait, StreamTrait};
+ #[cfg(target_os = "macos")]
use objc2_foundation::{NSActivityOptions, NSProcessInfo, NSString};
use winit::{
application::ApplicationHandler,
@@ -81,11 +82,16 @@ mod app {
});
let sentinel = Sentinel::start(log_path.with_file_name(SENTINEL_FILE));
- 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()?;
+ #[cfg(target_os = "macos")]
+ let event_loop = {
+ use winit::platform::macos::{ActivationPolicy, EventLoopBuilderExtMacOS};
+ EventLoop::<Command>::with_user_event()
+ .with_activation_policy(ActivationPolicy::Regular)
+ .with_activate_ignoring_other_apps(true)
+ .build()?
+ };
+ #[cfg(windows)]
+ let event_loop = EventLoop::<Command>::with_user_event().build()?;
let proxy = event_loop.create_proxy();
spawn_stdin_reader(proxy);
@@ -124,15 +130,21 @@ mod app {
let observer = start_observer(app.log.clone());
let (qos, realtime) = raise_priority(app.spec.fps);
app.log.write(&StimulusEvent::Priority { qos, realtime });
- let process_info = NSProcessInfo::processInfo();
- let activity = unsafe {
- process_info.beginActivityWithOptions_reason(
- NSActivityOptions::UserInitiated | NSActivityOptions::LatencyCritical,
- &NSString::from_str("Present recording verification markers on schedule"),
- )
+ #[cfg(target_os = "macos")]
+ let result = {
+ let process_info = NSProcessInfo::processInfo();
+ let activity = unsafe {
+ process_info.beginActivityWithOptions_reason(
+ NSActivityOptions::UserInitiated | NSActivityOptions::LatencyCritical,
+ &NSString::from_str("Present recording verification markers on schedule"),
+ )
+ };
+ let result = event_loop.run_app(&mut app);
+ unsafe { process_info.endActivity(&activity) };
+ result
};
+ #[cfg(windows)]
let result = event_loop.run_app(&mut app);
- unsafe { process_info.endActivity(&activity) };
result?;
drop(observer);
drop(audio);
@@ -190,10 +202,17 @@ mod app {
.thread
.join()
.map_err(|_| anyhow::anyhow!("sentinel panicked"))?;
- let info = cidre::mach::TimeBaseInfo::new();
+ // Host ticks to nanoseconds: ticks * numer / denom.
+ #[cfg(target_os = "macos")]
+ let timebase = {
+ let info = cidre::mach::TimeBaseInfo::new();
+ [u64::from(info.numer), u64::from(info.denom)]
+ };
+ #[cfg(windows)]
+ let timebase = [1_000_000_000, sched::qpc_frequency()];
let mut out = serde_json::json!({
"period_secs": SENTINEL_PERIOD_SECS,
- "timebase": [info.numer, info.denom],
+ "timebase": timebase,
"first": wakes.first(),
"realtime": realtime,
"end": end,
@@ -230,6 +249,18 @@ mod app {
/// while pictures are being presented is the window server or capture
/// service stalling (the input never reached the screen or the capture
/// service), not the recorder.
+ #[cfg(windows)]
+ fn start_observer(log: Arc<Log>) -> Observer {
+ log.write(&StimulusEvent::ObserverUnavailable {
+ reason: "no independent capture observer on Windows".into(),
+ });
+ Observer {
+ stop: Arc::new(std::sync::atomic::AtomicBool::new(false)),
+ thread: None,
+ }
+ }
+
+ #[cfg(target_os = "macos")]
fn start_observer(log: Arc<Log>) -> Observer {
use cidre::{ns, sc};
let stop = Arc::new(std::sync::atomic::AtomicBool::new(false));
@@ -694,6 +725,7 @@ mod app {
epoch: Arc<AtomicU64>,
log: Arc<Log>,
) -> Result<cpal::Stream, String> {
+ #[cfg(target_os = "macos")]
use cpal::host::coreaudio::StreamInstantExt;
let host = cpal::default_host();
@@ -760,7 +792,16 @@ mod app {
.build_output_stream(
&config.config(),
move |data: &mut [f32], info: &cpal::OutputCallbackInfo| {
- state.fill(data, info.timestamp().callback.as_host_time());
+ #[cfg(target_os = "macos")]
+ let callback_host = info.timestamp().callback.as_host_time();
+ // cpal's WASAPI instants have no host-clock accessor; the
+ // callback runs at the start of the buffer it fills.
+ #[cfg(windows)]
+ let callback_host = {
+ let _ = info;
+ host_now()
+ };
+ state.fill(data, callback_host);
},
|e| eprintln!("stimulus audio error: {e}"),
None,
diff --git a/crates/verify/Cargo.toml b/crates/verify/Cargo.toml
index 4392a72d1..453ddfbc8 100644
--- a/crates/verify/Cargo.toml
+++ b/crates/verify/Cargo.toml
@@ -40,3 +40,10 @@ tracing-subscriber = { version = "0.3", features = ["env-filter"] }
[target.'cfg(target_os = "macos")'.dev-dependencies]
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/src/lib.rs b/crates/verify/src/lib.rs
index 2460603f8..7a31487d0 100644
--- a/crates/verify/src/lib.rs
+++ b/crates/verify/src/lib.rs
@@ -15,6 +15,9 @@ pub mod pipeline;
pub mod report;
#[cfg(target_os = "macos")]
pub mod sched;
+#[cfg(windows)]
+#[path = "sched_windows.rs"]
+pub mod sched;
pub mod sentinel;
pub mod stimulus;
pub mod video_code;
diff --git a/crates/verify/src/sched_windows.rs b/crates/verify/src/sched_windows.rs
new file mode 100644
index 000000000..f7cb87480
--- /dev/null
+++ b/crates/verify/src/sched_windows.rs
@@ -0,0 +1,231 @@
+//! Real-time scheduling for the verification suite's input sources, Windows.
+//!
+//! The same interface as the macOS module. Windows has no time-constraint
+//! policy: an input thread asks for `THREAD_PRIORITY_TIME_CRITICAL` (the top
+//! of its process's priority class, where audio and capture callbacks run)
+//! and a 1 ms system timer resolution, and the priority is read back. Host
+//! ticks are QueryPerformanceCounter ticks, the clock Cap stamps Windows
+//! capture with (`Timestamp::PerformanceCounter`).
+
+use serde::{Deserialize, Serialize};
+
+/// The requested wake period and work budget, in milliseconds.
+#[derive(Clone, Copy, Debug, PartialEq, Serialize, Deserialize)]
+pub struct Constraint {
+ pub period_ms: f64,
+ pub computation_ms: f64,
+ pub constraint_ms: f64,
+ pub preemptible: bool,
+}
+
+/// A thread's scheduling state: `GetThreadPriority` in every priority field
+/// and the thread's CPU time.
+#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
+pub struct ThreadState {
+ pub policy: i32,
+ pub base_pri: i32,
+ pub cur_pri: i32,
+ pub max_pri: i32,
+ pub cpu_us: u64,
+}
+
+/// `THREAD_PRIORITY_TIME_CRITICAL`.
+pub const REALTIME_PRIORITY: i32 = 15;
+
+/// The outcome of [`request_realtime`].
+#[derive(Clone, Debug, Serialize, Deserialize)]
+pub struct Realtime {
+ pub requested: Constraint,
+ /// `timeBeginPeriod(1)` (`TIMERR_NOERROR` is 0).
+ pub qos_status: i32,
+ /// 0 when `SetThreadPriority` succeeded, else `GetLastError`.
+ pub policy_status: i32,
+ pub readback_status: i32,
+ pub readback_default: bool,
+ pub granted: Option<Constraint>,
+ pub state: Option<ThreadState>,
+}
+
+impl Realtime {
+ /// `Ok` when the thread runs at the time-critical priority.
+ pub fn obtained(&self) -> Result<(), String> {
+ if self.policy_status != 0 {
+ return Err(format!(
+ "SetThreadPriority(TIME_CRITICAL) failed: {}",
+ self.policy_status
+ ));
+ }
+ state_is_realtime(self.state)
+ }
+}
+
+/// `Ok` when the thread state is at the time-critical priority.
+pub fn state_is_realtime(state: Option<ThreadState>) -> Result<(), String> {
+ match state {
+ Some(state) if state.base_pri >= REALTIME_PRIORITY => Ok(()),
+ Some(state) => Err(format!(
+ "thread priority {} is below time-critical ({REALTIME_PRIORITY})",
+ state.base_pri
+ )),
+ None => Err("GetThreadPriority failed".into()),
+ }
+}
+
+const THREAD_QUERY_LIMITED_INFORMATION: u32 = 0x0800;
+const THREAD_PRIORITY_ERROR_RETURN: i32 = 0x7fff_ffff;
+const CREATE_WAITABLE_TIMER_HIGH_RESOLUTION: u32 = 0x0000_0002;
+const TIMER_ALL_ACCESS: u32 = 0x001F_0003;
+const INFINITE: u32 = 0xFFFF_FFFF;
+
+unsafe extern "system" {
+ fn GetCurrentThread() -> isize;
+ fn GetCurrentThreadId() -> u32;
+ fn GetLastError() -> u32;
+ fn SetThreadPriority(thread: isize, priority: i32) -> i32;
+ fn GetThreadPriority(thread: isize) -> i32;
+ fn OpenThread(access: u32, inherit: i32, id: u32) -> isize;
+ fn CloseHandle(handle: isize) -> i32;
+ fn GetThreadTimes(
+ thread: isize,
+ creation: *mut u64,
+ exit: *mut u64,
+ kernel: *mut u64,
+ user: *mut u64,
+ ) -> i32;
+ fn QueryPerformanceCounter(count: *mut i64) -> i32;
+ fn QueryPerformanceFrequency(frequency: *mut i64) -> i32;
+ fn CreateWaitableTimerExW(
+ attributes: *const core::ffi::c_void,
+ name: *const u16,
+ flags: u32,
+ access: u32,
+ ) -> isize;
+ fn SetWaitableTimer(
+ timer: isize,
+ due: *const i64,
+ period: i32,
+ completion: *const core::ffi::c_void,
+ argument: *const core::ffi::c_void,
+ resume: i32,
+ ) -> i32;
+ fn WaitForSingleObject(handle: isize, millis: u32) -> u32;
+ fn SwitchToThread() -> i32;
+}
+
+#[link(name = "winmm")]
+unsafe extern "system" {
+ fn timeBeginPeriod(period: u32) -> u32;
+}
+
+/// The calling thread's id.
+pub fn current_thread() -> u32 {
+ unsafe { GetCurrentThreadId() }
+}
+
+/// QueryPerformanceCounter ticks.
+pub fn qpc_now() -> u64 {
+ let mut count = 0i64;
+ unsafe { QueryPerformanceCounter(&mut count) };
+ count as u64
+}
+
+/// QueryPerformanceCounter ticks per second.
+pub fn qpc_frequency() -> u64 {
+ static FREQUENCY: std::sync::OnceLock<u64> = std::sync::OnceLock::new();
+ *FREQUENCY.get_or_init(|| {
+ let mut frequency = 0i64;
+ unsafe { QueryPerformanceFrequency(&mut frequency) };
+ frequency.max(1) as u64
+ })
+}
+
+/// Asks for the time-critical priority and a 1 ms timer resolution, then
+/// reads the priority back.
+pub fn request_realtime(period_secs: f64, computation_secs: f64) -> Realtime {
+ let requested = Constraint {
+ period_ms: period_secs * 1e3,
+ computation_ms: computation_secs * 1e3,
+ constraint_ms: period_secs * 1e3,
+ preemptible: true,
+ };
+ let qos_status = unsafe { timeBeginPeriod(1) } as i32;
+ let policy_status = if unsafe { SetThreadPriority(GetCurrentThread(), REALTIME_PRIORITY) } != 0
+ {
+ 0
+ } else {
+ unsafe { GetLastError() as i32 }
+ };
+ let state = thread_state(current_thread());
+ let granted = state
+ .filter(|state| state.base_pri >= REALTIME_PRIORITY)
+ .map(|_| requested);
+ Realtime {
+ requested,
+ qos_status,
+ policy_status,
+ readback_status: if state.is_some() { 0 } else { -1 },
+ readback_default: granted.is_none(),
+ granted,
+ state,
+ }
+}
+
+/// The scheduling state of the thread with id `thread`.
+pub fn thread_state(thread: u32) -> Option<ThreadState> {
+ let handle = unsafe { OpenThread(THREAD_QUERY_LIMITED_INFORMATION, 0, thread) };
+ if handle == 0 {
+ return None;
+ }
+ let priority = unsafe { GetThreadPriority(handle) };
+ let (mut creation, mut exit, mut kernel, mut user) = (0u64, 0u64, 0u64, 0u64);
+ let times = unsafe { GetThreadTimes(handle, &mut creation, &mut exit, &mut kernel, &mut user) };
+ unsafe { CloseHandle(handle) };
+ (priority != THREAD_PRIORITY_ERROR_RETURN).then_some(ThreadState {
+ policy: 0,
+ base_pri: priority,
+ cur_pri: priority,
+ max_pri: REALTIME_PRIORITY,
+ // FILETIME counts 100 ns intervals.
+ cpu_us: if times != 0 { (kernel + user) / 10 } else { 0 },
+ })
+}
+
+thread_local! {
+ static TIMER: isize = unsafe {
+ CreateWaitableTimerExW(
+ std::ptr::null(),
+ std::ptr::null(),
+ CREATE_WAITABLE_TIMER_HIGH_RESOLUTION,
+ TIMER_ALL_ACCESS,
+ )
+ };
+}
+
+/// How long before the deadline the timer wait ends and spinning starts.
+const SPIN_SECS: f64 = 0.000_25;
+
+/// Blocks until the host clock reaches `deadline` (QueryPerformanceCounter
+/// ticks): a high-resolution waitable timer for most of the wait, then a
+/// yielding spin for the last quarter millisecond.
+pub fn wait_until(deadline: u64) {
+ let frequency = qpc_frequency() as f64;
+ let remaining = |now: u64| (deadline as f64 - now as f64) / frequency;
+ let left = remaining(qpc_now());
+ if left > SPIN_SECS {
+ TIMER.with(|&timer| {
+ if timer != 0 {
+ // Negative: relative, in 100 ns intervals.
+ let due = -(((left - SPIN_SECS) * 1e7) as i64).max(1);
+ unsafe {
+ if SetWaitableTimer(timer, &due, 0, std::ptr::null(), std::ptr::null(), 0) != 0
+ {
+ WaitForSingleObject(timer, INFINITE);
+ }
+ }
+ }
+ });
+ }
+ while remaining(qpc_now()) > 0.0 {
+ unsafe { SwitchToThread() };
+ }
+}
diff --git a/crates/verify/src/stimulus.rs b/crates/verify/src/stimulus.rs
index 0820d75b2..d43e7eef6 100644
--- a/crates/verify/src/stimulus.rs
+++ b/crates/verify/src/stimulus.rs
@@ -130,7 +130,7 @@ pub const SENTINEL_PERIOD_SECS: f64 = 0.002;
/// Every wake of the stimulus's real-time sentinel thread (host ticks) and
/// the scheduling it ran under.
-#[cfg(target_os = "macos")]
+#[cfg(any(target_os = "macos", windows))]
#[derive(Clone, Debug)]
pub struct SentinelLog {
pub realtime: crate::sched::Realtime,
@@ -138,7 +138,7 @@ pub struct SentinelLog {
pub wakes: Vec<u64>,
}
-#[cfg(target_os = "macos")]
+#[cfg(any(target_os = "macos", windows))]
impl SentinelLog {
pub fn read(path: &std::path::Path) -> anyhow::Result<Self> {
#[derive(serde::Deserialize)]
@@ -190,10 +190,27 @@ pub fn host_add_secs(a: u64, secs: f64) -> u64 {
(a as f64 + secs * 1e9 * f64::from(info.denom) / f64::from(info.numer)).round() as u64
}
-#[cfg(target_os = "macos")]
+/// QueryPerformanceCounter ticks, the clock Windows capture is stamped with.
+#[cfg(windows)]
+pub fn host_now() -> u64 {
+ crate::sched::qpc_now()
+}
+
+/// Seconds between two host times (`a - b`).
+#[cfg(windows)]
+pub fn host_secs(a: u64, b: u64) -> f64 {
+ (a as f64 - b as f64) / crate::sched::qpc_frequency() as f64
+}
+
+#[cfg(windows)]
+pub fn host_add_secs(a: u64, secs: f64) -> u64 {
+ (a as f64 + secs * crate::sched::qpc_frequency() as f64).round() as u64
+}
+
+#[cfg(any(target_os = "macos", windows))]
pub use process::StimulusProcess;
-#[cfg(target_os = "macos")]
+#[cfg(any(target_os = "macos", windows))]
mod process {
use std::{
io::{BufRead, BufReader, Write},
diff --git a/crates/verify/src/video_code.rs b/crates/verify/src/video_code.rs
index 399b8ad8d..f0e5733fd 100644
--- a/crates/verify/src/video_code.rs
+++ b/crates/verify/src/video_code.rs
@@ -267,6 +267,30 @@ pub fn rgb_to_ycbcr(r: u8, g: u8, b: u8) -> (f64, f64, f64) {
)
}
+/// Tightly packed limited-range NV12 (BT.601) of an RGBA picture: the Y
+/// plane, then interleaved Cb/Cr sampled at each 2x2 block's top-left pixel.
+pub fn rgba_to_nv12(rgba: &[u8], width: usize, height: usize) -> Vec<u8> {
+ let ycc = |x: usize, y: usize| {
+ let i = (y * width + x) * 4;
+ rgb_to_ycbcr(rgba[i], rgba[i + 1], rgba[i + 2])
+ };
+ let mut out = Vec::with_capacity(width * height * 3 / 2);
+ for y in 0..height {
+ for x in 0..width {
+ let (l, _, _) = ycc(x, y);
+ out.push((16.0 + l * 219.0 / 255.0).round() as u8);
+ }
+ }
+ for y in 0..height / 2 {
+ for x in 0..width / 2 {
+ let (_, cb, cr) = ycc(x * 2, y * 2);
+ out.push((128.0 + cb * 224.0 / 255.0).round() as u8);
+ out.push((128.0 + cr * 224.0 / 255.0).round() as u8);
+ }
+ }
+ out
+}
+
/// Read access to a decoded picture in luma + centred chroma.
pub trait PixelSource {
fn width(&self) -> usize;
diff --git a/crates/verify/tests/recording.rs b/crates/verify/tests/recording.rs
index 33c4a3a16..9547a7030 100644
--- a/crates/verify/tests/recording.rs
+++ b/crates/verify/tests/recording.rs
@@ -14,7 +14,7 @@
//! `off` skips. Without Screen Recording permission the cases skip unless
//! `CAP_VERIFY_REQUIRE_CAPTURE=1`.
-#![cfg(target_os = "macos")]
+#![cfg(any(target_os = "macos", windows))]
use std::{
collections::BTreeMap,
@@ -29,7 +29,7 @@ use std::{
use cap_media_info::{RawVideoFormat, VideoInfo};
use cap_project::{InstantRecordingMeta, RecordingMeta, RecordingMetaInner, StudioRecordingMeta};
use cap_recording::{
- NativeCameraFrame, RecordingDefaults, SendableShareableContent, StudioQuality,
+ NativeCameraFrame, RecordingDefaults, StudioQuality,
feeds::{
Admission, camera::synthetic::SyntheticCamera, microphone::synthetic::SyntheticMicrophone,
},
@@ -38,10 +38,11 @@ use cap_recording::{
screen_capture::ScreenCaptureTarget,
studio_recording,
};
-use cap_timestamp::{MachAbsoluteTimestamp, Timestamp};
+#[cfg(target_os = "macos")]
+use cap_verify::fixture_avf;
use cap_verify::{
audio_code::{self, AudioSource, MarkerPlacement},
- fixture_avf, media,
+ media,
report::{self, Case, Tier},
sched,
stimulus::{
@@ -51,6 +52,89 @@ use cap_verify::{
video_code::{self, FrameMarker, Readout, StreamKind},
};
+use platform::{Shareable, host_timestamp};
+
+/// What differs between the macOS and Windows hosts: the capture permission
+/// check, the host clock the sources are stamped with and thread priority.
+#[cfg(target_os = "macos")]
+mod platform {
+ use cap_timestamp::{MachAbsoluteTimestamp, Timestamp};
+
+ pub type Shareable = cap_recording::SendableShareableContent;
+
+ /// ScreenCaptureKit's shareable content, which also checks the Screen
+ /// Recording permission.
+ pub async fn shareable() -> anyhow::Result<Shareable> {
+ cidre::sc::ShareableContent::current()
+ .await
+ .map(Shareable::from)
+ .map_err(|e| {
+ anyhow::anyhow!("ScreenCaptureKit unavailable ({e:?}); grant Screen Recording")
+ })
+ }
+
+ pub fn host_timestamp(ticks: u64) -> Timestamp {
+ Timestamp::MachAbsoluteTime(MachAbsoluteTimestamp::new(ticks))
+ }
+
+ /// Host ticks to nanoseconds: ticks * numer / denom.
+ pub fn timebase() -> [u64; 2] {
+ let info = cidre::mach::TimeBaseInfo::new();
+ [u64::from(info.numer), u64::from(info.denom)]
+ }
+
+ pub fn raise_thread_qos() {
+ const QOS_CLASS_USER_INITIATED: u32 = 0x19;
+ unsafe extern "C" {
+ fn pthread_set_qos_class_self_np(qos: u32, relative_priority: i32) -> i32;
+ }
+ unsafe { pthread_set_qos_class_self_np(QOS_CLASS_USER_INITIATED, 0) };
+ }
+
+ /// Every host has an output device for the system audio stimulus.
+ pub fn system_audio_output() -> bool {
+ true
+ }
+}
+
+#[cfg(windows)]
+mod platform {
+ use cap_timestamp::{PerformanceCounterTimestamp, Timestamp};
+
+ pub type Shareable = ();
+
+ /// Windows Graphics Capture needs no permission; recording itself fails
+ /// when the session has no desktop.
+ pub async fn shareable() -> anyhow::Result<Shareable> {
+ Ok(())
+ }
+
+ pub fn host_timestamp(ticks: u64) -> Timestamp {
+ Timestamp::PerformanceCounter(PerformanceCounterTimestamp::new(ticks as i64))
+ }
+
+ /// Host ticks to nanoseconds: ticks * numer / denom.
+ pub fn timebase() -> [u64; 2] {
+ [1_000_000_000, cap_verify::sched::qpc_frequency()]
+ }
+
+ pub fn raise_thread_qos() {
+ const THREAD_PRIORITY_ABOVE_NORMAL: i32 = 1;
+ unsafe extern "system" {
+ fn GetCurrentThread() -> isize;
+ fn SetThreadPriority(thread: isize, priority: i32) -> i32;
+ }
+ unsafe { SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_ABOVE_NORMAL) };
+ }
+
+ /// Hosted Windows runners run the audio service with no endpoints: there
+ /// is nothing to play the system audio stimulus on or to loop back.
+ pub fn system_audio_output() -> bool {
+ use cpal::traits::HostTrait;
+ cpal::default_host().default_output_device().is_some()
+ }
+}
+
static ATTEMPT: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0);
const MIC_RATE: u32 = 48_000;
@@ -342,13 +426,8 @@ fn env_flag(name: &str) -> bool {
/// high priority; at utility priority a loaded runner delays them and their
/// lateness shows up as recording timing errors. Run the runtime at the
/// priority the product itself asks for its recording threads.
-#[cfg(target_os = "macos")]
fn raise_thread_qos() {
- const QOS_CLASS_USER_INITIATED: u32 = 0x19;
- unsafe extern "C" {
- fn pthread_set_qos_class_self_np(qos: u32, relative_priority: i32) -> i32;
- }
- unsafe { pthread_set_qos_class_self_np(QOS_CLASS_USER_INITIATED, 0) };
+ platform::raise_thread_qos();
}
fn runtime() -> tokio::runtime::Runtime {
@@ -380,10 +459,10 @@ fn real_capture() {
return;
};
let rt = runtime();
- let shareable = match rt.block_on(cidre::sc::ShareableContent::current()) {
- Ok(content) => SendableShareableContent::from(content),
+ let shareable = match rt.block_on(platform::shareable()) {
+ Ok(content) => content,
Err(e) => {
- let message = format!("ScreenCaptureKit unavailable ({e:?}); grant Screen Recording");
+ let message = format!("{e:#}");
if env_flag("CAP_VERIFY_REQUIRE_CAPTURE") {
panic!("{message}");
}
@@ -653,10 +732,7 @@ fn start_feeds(
}
}
trace.handoff(n as u32, delivered, wake, pri, || {
- mic.try_push(
- chunk,
- Timestamp::MachAbsoluteTime(MachAbsoluteTimestamp::new(capture)),
- )
+ mic.try_push(chunk, host_timestamp(capture))
});
}
trace.end = sched::thread_state(thread);
@@ -929,7 +1005,6 @@ fn persist_sources(
.map(|d| PathBuf::from(d).join("sources"))
.unwrap_or_else(|| fallback.to_path_buf());
let path = dir.join(format!("{stem}.json"));
- let info = cidre::mach::TimeBaseInfo::new();
let sources: Vec<serde_json::Value> = rec
.sources
.iter()
@@ -960,7 +1035,7 @@ fn persist_sources(
.collect();
let value = serde_json::json!({
"attempt": stem,
- "timebase": [info.numer, info.denom],
+ "timebase": platform::timebase(),
"reference": reference,
"window_secs": [rec.started, rec.stopped],
"columns": ["index", "due", "wake", "built", "pushed", "admission", "pri"],
@@ -974,16 +1049,43 @@ fn persist_sources(
path
}
+#[cfg(target_os = "macos")]
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)),
+ timestamp: host_timestamp(capture),
})
}
+/// An NV12 frame in a Media Foundation memory buffer, as the Windows camera
+/// feed hands native frames to the encoder.
+#[cfg(windows)]
+fn camera_frame(slot: u32, capture: u64) -> Option<NativeCameraFrame> {
+ use cap_mediafoundation_utils::IMFMediaBufferExt;
+ use windows::Win32::Media::MediaFoundation::MFCreateMemoryBuffer;
+
+ let (w, h) = CAMERA_SIZE;
+ let rgba = video_code::marker_rgba(FrameMarker::new(StreamKind::Camera, 0, slot), w, h);
+ let nv12 = video_code::rgba_to_nv12(&rgba, w, h);
+ let buffer = unsafe { MFCreateMemoryBuffer(nv12.len() as u32) }.ok()?;
+ buffer.lock_for_write().ok()?.copy_from_slice(&nv12);
+ unsafe { buffer.SetCurrentLength(nv12.len() as u32) }.ok()?;
+ #[allow(clippy::arc_with_non_send_sync)]
+ let buffer = Arc::new(std::sync::Mutex::new(buffer));
+ Some(NativeCameraFrame {
+ buffer,
+ pixel_format: cap_camera_windows::PixelFormat::NV12,
+ width: w as u32,
+ height: h as u32,
+ is_bottom_up: false,
+ timestamp: host_timestamp(capture),
+ })
+}
+
+#[cfg(target_os = "macos")]
fn camera_sample(
rgba: &[u8],
w: usize,
@@ -1014,21 +1116,14 @@ enum Handle {
/// reach the recording.
async fn capture_setup(
capture: CaptureMode,
-) -> anyhow::Result<(
- ScreenCaptureTarget,
- Vec<scap_targets::WindowId>,
- SendableShareableContent,
-)> {
+) -> anyhow::Result<(ScreenCaptureTarget, Vec<scap_targets::WindowId>, Shareable)> {
let stimulus_window = find_window(WINDOW_TITLE);
let excluded = scap_targets::Window::list()
.into_iter()
.map(|w| w.id())
.filter(|id| Some(id) != stimulus_window.as_ref())
.collect();
- let shareable = cidre::sc::ShareableContent::current()
- .await
- .map_err(|e| anyhow::anyhow!("shareable content: {e:?}"))?
- .into();
+ let shareable = platform::shareable().await?;
let target = match capture {
CaptureMode::Display => ScreenCaptureTarget::Display {
id: scap_targets::Display::primary().id(),
@@ -1052,19 +1147,50 @@ fn studio_builder(
system_audio: bool,
) -> (studio_recording::ActorBuilder, u32) {
let defaults = RecordingDefaults::default();
- let builder = defaults
- .apply_to_studio_builder(
- studio_recording::Actor::builder(project, target).with_system_audio(system_audio),
- camera,
- None,
- )
- .with_excluded_windows(excluded)
+ let builder = defaults.apply_to_studio_builder(
+ studio_recording::Actor::builder(project, target).with_system_audio(system_audio),
+ camera,
+ None,
+ );
+ // Windows Graphics Capture records the display without per-window
+ // exclusion; the stimulus covers it full screen.
+ #[cfg(target_os = "macos")]
+ let builder = builder.with_excluded_windows(excluded);
+ #[cfg(windows)]
+ let _ = excluded;
+ let builder = builder
.with_keyboard_capture(false)
.with_fragmented(fragmented)
.with_quality(StudioQuality::Balanced);
(builder, defaults.studio_max_fps(camera, None))
}
+async fn build_studio(
+ builder: studio_recording::ActorBuilder,
+ shareable: &Shareable,
+) -> anyhow::Result<studio_recording::ActorHandle> {
+ #[cfg(target_os = "macos")]
+ return builder.build(Some(shareable.clone())).await;
+ #[cfg(windows)]
+ {
+ let () = *shareable;
+ builder.build().await
+ }
+}
+
+async fn build_instant(
+ builder: instant_recording::ActorBuilder,
+ shareable: &Shareable,
+) -> anyhow::Result<instant_recording::ActorHandle> {
+ #[cfg(target_os = "macos")]
+ return builder.build(Some(shareable.clone())).await;
+ #[cfg(windows)]
+ {
+ let () = *shareable;
+ builder.build().await
+ }
+}
+
fn stimulus_binary() -> PathBuf {
std::env::var_os("CAP_VERIFY_STIMULUS_BIN")
.map(PathBuf::from)
@@ -1076,7 +1202,7 @@ fn stimulus_binary() -> PathBuf {
.parent()
.and_then(|deps| deps.parent())
.expect("target profile directory")
- .join("cap-verify-stimulus");
+ .join(format!("cap-verify-stimulus{}", std::env::consts::EXE_SUFFIX));
assert!(
path.exists(),
"{} not found: build it first with `cargo build -p cap-verify-stimulus` (same profile), or set CAP_VERIFY_STIMULUS_BIN",
@@ -1118,7 +1244,7 @@ struct Recorded {
async fn run_case(
spec: &Spec,
capture: CaptureMode,
- shareable: &SendableShareableContent,
+ shareable: &Shareable,
case: &mut Case,
) -> anyhow::Result<()> {
let scratch = scratch_root().join(format!(
@@ -1919,7 +2045,7 @@ fn scratch_root() -> PathBuf {
async fn record(
spec: &Spec,
capture: CaptureMode,
- _preflight: &SendableShareableContent,
+ _preflight: &Shareable,
scratch: &Path,
reference: u64,
case: &mut Case,
@@ -1929,7 +2055,9 @@ async fn record(
seconds: spec.seconds,
holds: spec.holds.clone(),
busy: true,
- audio: spec.system_audio && stimulus_plays_system_audio(),
+ audio: spec.system_audio
+ && stimulus_plays_system_audio()
+ && platform::system_audio_output(),
warmup: match capture {
CaptureMode::Display => 2.0,
CaptureMode::Window => 2.0,
@@ -1972,6 +2100,10 @@ async fn record(
let project = scratch.join("recording.cap");
let defaults = RecordingDefaults::default();
+ let system_audio = spec.system_audio && platform::system_audio_output();
+ if spec.system_audio && !system_audio {
+ case.note("no audio output device: recorded without system audio");
+ }
let gate = spec.gated.then(cap_recording::RecordingStartGate::new);
let (handle, max_fps) = match spec.mode {
Mode::Studio { fragmented, .. } => {
@@ -1981,7 +2113,7 @@ async fn record(
excluded,
fragmented,
camera,
- spec.system_audio,
+ system_audio,
);
let mut builder = builder;
if spec.mic {
@@ -1994,15 +2126,16 @@ async fn record(
builder = builder.with_camera_feed(lock.clone());
}
(
- Handle::Studio(builder.build(Some(shareable.clone())).await?),
+ Handle::Studio(build_studio(builder, &shareable).await?),
max_fps,
)
}
Mode::Instant => {
let builder = instant_recording::Actor::builder(project.clone(), target)
- .with_system_audio(spec.system_audio)
- .with_max_output_size(defaults.instant_mode_max_resolution)
- .with_excluded_windows(excluded);
+ .with_system_audio(system_audio)
+ .with_max_output_size(defaults.instant_mode_max_resolution);
+ #[cfg(target_os = "macos")]
+ let builder = builder.with_excluded_windows(excluded);
let builder = if spec.mic {
builder.with_mic_feed(mic_lock.clone())
} else {
@@ -2013,22 +2146,16 @@ async fn record(
None => builder,
};
(
- Handle::Instant(builder.build(Some(shareable.clone())).await?),
+ Handle::Instant(build_instant(builder, &shareable).await?),
cap_recording::DEFAULT_INSTANT_MODE_FPS,
)
}
Mode::StudioCrashed => {
- let (builder, max_fps) = studio_builder(
- project.clone(),
- target,
- excluded,
- true,
- false,
- spec.system_audio,
- );
+ let (builder, max_fps) =
+ studio_builder(project.clone(), target, excluded, true, false, system_audio);
let builder = builder.with_mic_feed(mic_lock.clone());
(
- Handle::Studio(builder.build(Some(shareable.clone())).await?),
+ Handle::Studio(build_studio(builder, &shareable).await?),
max_fps,
)
}