Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix(verify): Windows stimulus audio uses the WASAPI timestamp, snapshots survive fragment renames, required system audio fails without an output device

Richie McIlroyefdf0251cda7
diff --git a/crates/verify-stimulus/src/main.rs b/crates/verify-stimulus/src/main.rs
index 8faf665aa..44ff9124d 100644
--- a/crates/verify-stimulus/src/main.rs
+++ b/crates/verify-stimulus/src/main.rs
@@ -929,12 +929,13 @@ mod app {
move |data: &mut [f32], info: &cpal::OutputCallbackInfo| {
#[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.
+ // WASAPI stream instants are QueryPerformanceCounter
+ // readings in 100 ns units.
#[cfg(windows)]
let callback_host = {
- let _ = info;
- host_now()
+ use cpal::host::wasapi::StreamInstantExt;
+ let units = i128::from(info.timestamp().callback.as_performance_counter());
+ (units * i128::from(cap_verify::sched::qpc_frequency()) / 10_000_000) as u64
};
state.fill(data, callback_host);
},
diff --git a/crates/verify/src/sched_windows.rs b/crates/verify/src/sched_windows.rs
index f7cb87480..945967c81 100644
--- a/crates/verify/src/sched_windows.rs
+++ b/crates/verify/src/sched_windows.rs
@@ -9,7 +9,7 @@
use serde::{Deserialize, Serialize};
-/// The requested wake period and work budget, in milliseconds.
+/// Milliseconds.
#[derive(Clone, Copy, Debug, PartialEq, Serialize, Deserialize)]
pub struct Constraint {
pub period_ms: f64,
@@ -32,7 +32,6 @@ pub struct ThreadState {
/// `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,
@@ -47,7 +46,6 @@ pub struct Realtime {
}
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!(
@@ -59,7 +57,6 @@ impl Realtime {
}
}
-/// `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(()),
@@ -117,19 +114,16 @@ 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(|| {
@@ -170,7 +164,6 @@ pub fn request_realtime(period_secs: f64, computation_secs: f64) -> Realtime {
}
}
-/// 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 {
@@ -190,15 +183,25 @@ pub fn thread_state(thread: u32) -> Option<ThreadState> {
})
}
+struct Timer(isize);
+
+impl Drop for Timer {
+ fn drop(&mut self) {
+ if self.0 != 0 {
+ unsafe { CloseHandle(self.0) };
+ }
+ }
+}
+
thread_local! {
- static TIMER: isize = unsafe {
+ static TIMER: Timer = Timer(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.
@@ -212,7 +215,7 @@ pub fn wait_until(deadline: u64) {
let remaining = |now: u64| (deadline as f64 - now as f64) / frequency;
let left = remaining(qpc_now());
if left > SPIN_SECS {
- TIMER.with(|&timer| {
+ TIMER.with(|&Timer(timer)| {
if timer != 0 {
// Negative: relative, in 100 ns intervals.
let due = -(((left - SPIN_SECS) * 1e7) as i64).max(1);
diff --git a/crates/verify/tests/recording.rs b/crates/verify/tests/recording.rs
index 8aade6cd4..cf1f25da4 100644
--- a/crates/verify/tests/recording.rs
+++ b/crates/verify/tests/recording.rs
@@ -2054,14 +2054,19 @@ async fn record(
reference: u64,
case: &mut Case,
) -> anyhow::Result<Recorded> {
+ let system_output = platform::system_audio_output();
+ if spec.system_audio && require_system_audio() && !system_output {
+ case.problem(
+ "missing",
+ "system audio required but this host has no audio output device",
+ );
+ }
let stimulus_spec = StimulusSpec {
fps: spec.stimulus_fps,
seconds: spec.seconds,
holds: spec.holds.clone(),
busy: true,
- audio: spec.system_audio
- && stimulus_plays_system_audio()
- && platform::system_audio_output(),
+ audio: spec.system_audio && stimulus_plays_system_audio() && system_output,
warmup: match capture {
CaptureMode::Display => 2.0,
CaptureMode::Window => 2.0,
@@ -2309,16 +2314,40 @@ fn copy_project(from: &Path, to: &Path) -> anyhow::Result<()> {
{
fn copy_dir(from: &Path, to: &Path) -> std::io::Result<()> {
std::fs::create_dir_all(to)?;
- for entry in std::fs::read_dir(from)? {
- let entry = entry?;
- let target = to.join(entry.file_name());
- if entry.file_type()?.is_dir() {
- copy_dir(&entry.path(), &target)?;
- } else {
- std::fs::copy(entry.path(), &target)?;
+ let mut copied = std::collections::HashSet::new();
+ // The recorder renames `.tmp` fragments as it finishes them: a
+ // file listed can be gone by the time it is opened, under a name
+ // the next listing has.
+ for _ in 0..5 {
+ let mut vanished = false;
+ for entry in std::fs::read_dir(from)? {
+ let entry = entry?;
+ let name = entry.file_name();
+ if copied.contains(&name) {
+ continue;
+ }
+ let target = to.join(&name);
+ let result = if entry.file_type()?.is_dir() {
+ copy_dir(&entry.path(), &target)
+ } else {
+ std::fs::copy(entry.path(), &target).map(|_| ())
+ };
+ match result {
+ Ok(()) => {
+ copied.insert(name);
+ }
+ Err(e) if e.kind() == std::io::ErrorKind::NotFound => vanished = true,
+ Err(e) => return Err(e),
+ }
+ }
+ if !vanished {
+ return Ok(());
}
}
- Ok(())
+ Err(std::io::Error::other(format!(
+ "files in {} kept disappearing while it was copied",
+ from.display()
+ )))
}
copy_dir(from, to).map_err(Into::into)
}