Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions crates/recording/examples/cursor-poll-benchmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ fn main() -> anyhow::Result<()> {
crop_bounds,
display,
window: None,
frame_size: None,
},
dir.path().join("cursors"),
Default::default(),
Expand Down
23 changes: 20 additions & 3 deletions crates/recording/src/cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,26 +52,38 @@ pub struct CursorCaptureTarget {
pub crop_bounds: CursorCropBounds,
pub display: scap_targets::Display,
pub window: Option<scap_targets::WindowId>,
/// The size of the frames the screen capture writes. A window-only
/// capture keeps it when the window is resized, so the cursor is placed
/// against it rather than against the window alone.
pub frame_size: Option<(u32, u32)>,
}

/// `frame_aspect` is the shape of the recording's frame, width over height.
#[cfg(target_os = "macos")]
fn macos_window_crop(
window: &scap_targets::WindowId,
display: &scap_targets::Display,
frame_aspect: Option<f64>,
) -> Option<CursorCropBounds> {
use crate::sources::screen_capture::window_frame_bounds;
use scap_targets::bounds::{LogicalBounds, LogicalPosition};

let window_bounds = scap_targets::Window::from_id_direct(window)?
.raw_handle()
.logical_bounds()?;
let display_position = display.raw_handle().logical_position();
Some(CursorCropBounds::new_macos(LogicalBounds::new(
let bounds = LogicalBounds::new(
LogicalPosition::new(
window_bounds.position().x() - display_position.x(),
window_bounds.position().y() - display_position.y(),
),
window_bounds.size(),
)))
);
let frame = match frame_aspect {
Some(aspect) => window_frame_bounds(bounds, aspect),
None => bounds,
};
Some(CursorCropBounds::new_macos(frame))
Comment thread
patrickballard marked this conversation as resolved.
Resolved
}

#[cfg(windows)]
Expand Down Expand Up @@ -720,6 +732,11 @@ pub fn spawn_cursor_recorder(
let mut last_window_position = None;
#[cfg(any(windows, target_os = "macos"))]
let mut last_window_crop: Option<CursorCropBounds> = None;
#[cfg(target_os = "macos")]
let window_frame_aspect = target
.frame_size
.filter(|(width, height)| *width > 0 && *height > 0)
.map(|(width, height)| f64::from(width) / f64::from(height));
// device_query's `DeviceState::new` asserts Accessibility trust on
// macOS (and prompts for it). From macOS 27.3 Cap no longer needs that
// grant: cursor position and mouse buttons read fine without it, and
Expand Down Expand Up @@ -820,7 +837,7 @@ pub fn spawn_cursor_recorder(
let window_crop = target
.window
.as_ref()
.and_then(|window| macos_window_crop(window, &display));
.and_then(|window| macos_window_crop(window, &display, window_frame_aspect));
#[cfg(any(windows, target_os = "macos"))]
let position_changed =
position_changed || (window_crop.is_some() && window_crop != last_window_crop);
Expand Down
219 changes: 158 additions & 61 deletions crates/recording/src/sources/screen_capture/macos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ use tracing::{debug, error, info, warn};

const MAX_CAPTURE_RESTARTS: u32 = 3;
const RESTART_DELAY: Duration = Duration::from_secs(2);
// Each check is a WindowServer round trip that measured several percent of an
// Instant recording's CPU at 100 ms, so a still window is checked less often and
// the fast interval only runs while the window is changing.
const WINDOW_FOLLOW_INTERVAL: Duration = Duration::from_millis(100);
const WINDOW_FOLLOW_IDLE_INTERVAL: Duration = Duration::from_millis(500);
const WINDOW_FOLLOW_ACTIVE_FOR: Duration = Duration::from_secs(2);
const WINDOW_WATCH_INTERVAL: Duration = Duration::from_secs(1);
const WINDOW_GONE_CHECKS: u32 = 2;

Expand Down Expand Up @@ -102,6 +108,53 @@ fn configure_window_capture(settings: &mut sc::StreamCfg) {
}
}

fn stream_settings(
config: &Config,
video_info: &VideoInfo,
system_audio: bool,
captures_window: bool,
crop_bounds: Option<super::CropBounds>,
) -> arc::R<sc::StreamCfg> {
let max_queue_depth = get_max_queue_depth();
let queue_depth = ((config.fps as f32 / 30.0 * 5.0).ceil() as isize).clamp(3, max_queue_depth);
debug!(
queue_depth,
max_queue_depth,
?crop_bounds,
"Screen capture queue depth"
);

let mut settings = scap_screencapturekit::StreamCfgBuilder::default()
.with_width(video_info.width as usize)
.with_height(video_info.height as usize)
.with_fps(config.fps as f32)
.with_shows_cursor(config.show_cursor)
.with_captures_audio(system_audio)
.with_queue_depth(queue_depth)
.build();

settings.set_pixel_format(cv::PixelFormat::_420V);
settings.set_color_space_name(cg::color_space::names::srgb());
if captures_window {
configure_window_capture(&mut settings);
}
if let Some(crop_bounds) = crop_bounds {
settings.set_src_rect(source_rect(captures_window, crop_bounds));
}
settings
}

/// What a window-only capture of `window` reads now: the whole window, sized
/// as the recording's own crop is when it starts. The window can be resized
/// while it is recorded, and a source rect left at its first size would cut
/// off whatever grew past it.
fn current_window_crop(window: &WindowId, scale: f64) -> Option<super::CropBounds> {
let bounds = scap_targets::Window::from_id_direct(window)?
.raw_handle()
.logical_bounds()?;
Some(super::even_pixel_crop(bounds, scale))
}

/// What the stream reads. A display capture crops to `crop_bounds` on the
/// display; a window capture reads the same size from the window's own
/// top-left corner, so Focus records every pixel a screen region would.
Expand Down Expand Up @@ -434,38 +487,13 @@ impl ScreenCaptureConfig<CMSampleBufferCapture> {

debug!("SCK content filter: {:?}", content_filter);

let size = PhysicalSize::new(self.video_info.width as f64, self.video_info.height as f64);

debug!("size: {:?}", size);

let max_queue_depth = get_max_queue_depth();
let queue_depth =
((self.config.fps as f32 / 30.0 * 5.0).ceil() as isize).clamp(3, max_queue_depth);
debug!(
queue_depth = queue_depth,
max_queue_depth = max_queue_depth,
"Screen capture queue depth"
let settings = stream_settings(
&self.config,
&self.video_info,
self.system_audio,
captures_window,
self.config.crop_bounds,
);

let mut settings = scap_screencapturekit::StreamCfgBuilder::default()
.with_width(size.width() as usize)
.with_height(size.height() as usize)
.with_fps(self.config.fps as f32)
.with_shows_cursor(self.config.show_cursor)
.with_captures_audio(self.system_audio)
.with_queue_depth(queue_depth)
.build();

settings.set_pixel_format(cv::PixelFormat::_420V);
settings.set_color_space_name(cg::color_space::names::srgb());
if captures_window {
configure_window_capture(&mut settings);
}

if let Some(crop_bounds) = self.config.crop_bounds {
debug!("crop bounds: {:?}", crop_bounds);
settings.set_src_rect(source_rect(captures_window, crop_bounds));
}
cap_fail::fail_err!(
"macos::ScreenCaptureActor::new",
ns::Error::with_domain(ns::ErrorDomain::os_status(), 69420, None)
Expand Down Expand Up @@ -705,7 +733,7 @@ impl ScreenCaptureConfig<CMSampleBufferCapture> {
});

let cancel_token = CancellationToken::new();
let capturer = Capturer::new(Arc::new(builder.build()?));
let capturer = Capturer::new(Arc::new(builder.build()?), self.config.crop_bounds);

Ok((
VideoSourceConfig {
Expand Down Expand Up @@ -750,26 +778,39 @@ pub struct CaptureError(pub arc::R<ns::Error>);
struct Capturer {
started: Arc<AtomicBool>,
capturer: Arc<scap_screencapturekit::Capturer>,
/// The size of the source rect this stream reads now. A restart builds
/// a new stream for the window's size at that moment, so this is kept
/// per stream rather than per recording.
source_size: Arc<Mutex<Option<LogicalSize>>>,
}

impl Clone for Capturer {
fn clone(&self) -> Self {
Self {
started: self.started.clone(),
capturer: self.capturer.clone(),
source_size: self.source_size.clone(),
// error_rx: self.error_rx.resubscribe(),
}
}
}

impl Capturer {
fn new(capturer: Arc<scap_screencapturekit::Capturer>) -> Self {
fn new(
capturer: Arc<scap_screencapturekit::Capturer>,
crop_bounds: Option<super::CropBounds>,
) -> Self {
Self {
started: Arc::new(AtomicBool::new(false)),
capturer,
source_size: Arc::new(Mutex::new(crop_bounds.map(|bounds| bounds.size()))),
}
}

fn source_size(&self) -> Option<LogicalSize> {
self.source_size.lock().ok().and_then(|size| *size)
}

fn mark_stopped(&self) {
self.started.store(false, atomic::Ordering::Relaxed);
}
Expand Down Expand Up @@ -891,6 +932,78 @@ impl output_pipeline::VideoSource for VideoSource {
let active_capturer: Arc<Mutex<Option<Capturer>>> = Arc::new(Mutex::new(None));
let active_capturer_for_stop = active_capturer.clone();
let original_capturer = capturer.clone();

if let Some(window) = capturable_window(
&rebuild_params.config,
&rebuild_params.excluded_windows,
"follow",
)
.cloned()
{
let follow_cancel = cancel_token.clone();
let params = rebuild_params.clone();
let active_capturer = active_capturer.clone();
let original_capturer = capturer.clone();
ctx.tasks()
.spawn("screen-capture-window-follow", async move {
let mut display_scale: Option<f64> = None;
let mut last_change: Option<Instant> = None;
loop {
let interval = if last_change
.is_some_and(|at| at.elapsed() < WINDOW_FOLLOW_ACTIVE_FOR)
{
WINDOW_FOLLOW_INTERVAL
} else {
WINDOW_FOLLOW_IDLE_INTERVAL
};
select! {
_ = follow_cancel.cancelled() => break Ok(()),
_ = tokio::time::sleep(interval) => {}
}
let capturer = active_capturer
.lock()
.ok()
.and_then(|active| active.clone())
.unwrap_or_else(|| original_capturer.clone());
// A stopped stream, paused or waiting on a restart, takes
// the window's size when it is rebuilt.
if !capturer.started.load(atomic::Ordering::Relaxed) {
continue;
}
if display_scale.is_none() {
display_scale = Display::from_id(&params.display_id)
.map(|display| display.raw_handle().scale().unwrap_or(1.0));
}
let Some(scale) = display_scale else {
continue;
};
let Some(crop) = current_window_crop(&window, scale) else {
continue;
};
if capturer.source_size() == Some(crop.size()) {
continue;
}
last_change = Some(Instant::now());
let update = capturer.capturer.update_cfg(&stream_settings(
&params.config,
&params.video_info,
params.system_audio,
true,
Some(crop),
));
match update.await {
Ok(()) => {
debug!(?crop, "Window capture follows the window's new size");
if let Ok(mut size) = capturer.source_size.lock() {
*size = Some(crop.size());
}
}
Err(error) => warn!(%error, "Could not resize the window capture"),
}
}
});
}

ctx.tasks().spawn("screen-capture-monitor", async move {
let mut restart_count = 0u32;
let mut window_watch =
Expand Down Expand Up @@ -1258,14 +1371,18 @@ async fn rebuild_capturer(params: &CapturerRebuildParams) -> anyhow::Result<Capt
let display = Display::from_id(&params.display_id)
.ok_or_else(|| anyhow!("Display not found during restart: {:?}", params.display_id))?;

let window_filter = capturable_window(&params.config, &params.excluded_windows, "restart")
let window = capturable_window(&params.config, &params.excluded_windows, "restart");
let window_filter = window
.map(|id| {
window_content_filter(id, &shareable_content, "restart").ok_or_else(|| {
anyhow!("Capture window {id} is gone; not falling back to the display")
})
})
.transpose()?;
let captures_window = window_filter.is_some();
let crop_bounds = window
.and_then(|id| current_window_crop(id, display.raw_handle().scale().unwrap_or(1.0)))
.or(params.config.crop_bounds);

let content_filter = match window_filter {
Some(filter) => filter,
Expand All @@ -1280,34 +1397,14 @@ async fn rebuild_capturer(params: &CapturerRebuildParams) -> anyhow::Result<Capt
}
};

let size = PhysicalSize::new(
params.video_info.width as f64,
params.video_info.height as f64,
let settings = stream_settings(
&params.config,
&params.video_info,
params.system_audio,
captures_window,
crop_bounds,
);

let max_queue_depth = get_max_queue_depth();
let queue_depth =
((params.config.fps as f32 / 30.0 * 5.0).ceil() as isize).clamp(3, max_queue_depth);

let mut settings = scap_screencapturekit::StreamCfgBuilder::default()
.with_width(size.width() as usize)
.with_height(size.height() as usize)
.with_fps(params.config.fps as f32)
.with_shows_cursor(params.config.show_cursor)
.with_captures_audio(params.system_audio)
.with_queue_depth(queue_depth)
.build();

settings.set_pixel_format(cv::PixelFormat::_420V);
settings.set_color_space_name(cg::color_space::names::srgb());
if captures_window {
configure_window_capture(&mut settings);
}

if let Some(crop_bounds) = params.config.crop_bounds {
settings.set_src_rect(source_rect(captures_window, crop_bounds));
}

let expected_width = params.video_info.width as usize;
let expected_height = params.video_info.height as usize;

Expand Down Expand Up @@ -1506,7 +1603,7 @@ async fn rebuild_capturer(params: &CapturerRebuildParams) -> anyhow::Result<Capt
}
});

let capturer = Capturer::new(Arc::new(builder.build()?));
let capturer = Capturer::new(Arc::new(builder.build()?), crop_bounds);
capturer.start().await?;
Ok(capturer)
}
Expand Down
Loading
Loading