Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix(recording): read a focused window inside its rounded corners so they don't record black

Richie McIlroyc0b11f5753c4
diff --git a/crates/recording/src/cursor.rs b/crates/recording/src/cursor.rs
index a12e0c152..a3a71e0ac 100644
--- a/crates/recording/src/cursor.rs
+++ b/crates/recording/src/cursor.rs
@@ -65,13 +65,17 @@ fn macos_window_crop(
.raw_handle()
.logical_bounds()?;
let display_position = display.raw_handle().logical_position();
- Some(CursorCropBounds::new_macos(LogicalBounds::new(
- LogicalPosition::new(
- window_bounds.position().x() - display_position.x(),
- window_bounds.position().y() - display_position.y(),
- ),
- window_bounds.size(),
- )))
+ // The video reads the window inside its rounded corners; the cursor
+ // is placed against the same trimmed frame.
+ Some(CursorCropBounds::new_macos(
+ crate::sources::screen_capture::trim_window_corners(LogicalBounds::new(
+ LogicalPosition::new(
+ window_bounds.position().x() - display_position.x(),
+ window_bounds.position().y() - display_position.y(),
+ ),
+ window_bounds.size(),
+ )),
+ ))
}
#[cfg(windows)]
diff --git a/crates/recording/src/sources/screen_capture/macos.rs b/crates/recording/src/sources/screen_capture/macos.rs
index 5933d9798..46461fc96 100644
--- a/crates/recording/src/sources/screen_capture/macos.rs
+++ b/crates/recording/src/sources/screen_capture/macos.rs
@@ -72,6 +72,23 @@ fn window_content_filter(
Some(sc::ContentFilter::with_desktop_independent_window(window))
}
+/// What the stream reads. A display capture crops to `crop_bounds`; a window
+/// capture reads the window itself, inside its rounded corners, where
+/// `crop_bounds` is the window already trimmed by `WINDOW_CORNER_TRIM`.
+fn source_rect(captures_window: bool, crop_bounds: super::CropBounds) -> cg::Rect {
+ let (x, y) = if captures_window {
+ (super::WINDOW_CORNER_TRIM, super::WINDOW_CORNER_TRIM)
+ } else {
+ (crop_bounds.position().x(), crop_bounds.position().y())
+ };
+ cg::Rect::new(
+ x,
+ y,
+ crop_bounds.size().width(),
+ crop_bounds.size().height(),
+ )
+}
+
fn excluded_shareable_windows(
excluded_windows: &[WindowId],
content: &sc::ShareableContent,
@@ -321,14 +338,9 @@ impl ScreenCaptureConfig<CMSampleBufferCapture> {
settings.set_pixel_format(cv::PixelFormat::_420V);
settings.set_color_space_name(cg::color_space::names::srgb());
- if !captures_window && let Some(crop_bounds) = self.config.crop_bounds {
+ if let Some(crop_bounds) = self.config.crop_bounds {
debug!("crop bounds: {:?}", crop_bounds);
- settings.set_src_rect(cg::Rect::new(
- crop_bounds.position().x(),
- crop_bounds.position().y(),
- crop_bounds.size().width(),
- crop_bounds.size().height(),
- ));
+ settings.set_src_rect(source_rect(captures_window, crop_bounds));
}
cap_fail::fail_err!(
"macos::ScreenCaptureActor::new",
@@ -1055,13 +1067,8 @@ async fn rebuild_capturer(params: &CapturerRebuildParams) -> anyhow::Result<Capt
settings.set_pixel_format(cv::PixelFormat::_420V);
settings.set_color_space_name(cg::color_space::names::srgb());
- if !captures_window && let Some(crop_bounds) = params.config.crop_bounds {
- settings.set_src_rect(cg::Rect::new(
- crop_bounds.position().x(),
- crop_bounds.position().y(),
- crop_bounds.size().width(),
- crop_bounds.size().height(),
- ));
+ 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;
diff --git a/crates/recording/src/sources/screen_capture/mod.rs b/crates/recording/src/sources/screen_capture/mod.rs
index 2a4b41f5d..7784f4c4d 100644
--- a/crates/recording/src/sources/screen_capture/mod.rs
+++ b/crates/recording/src/sources/screen_capture/mod.rs
@@ -390,6 +390,29 @@ pub struct Config {
#[cfg(target_os = "macos")]
pub type CropBounds = LogicalBounds;
+/// macOS hands a single window over with its rounded corners transparent,
+/// which the encoder fills with black. Window-only capture reads this many
+/// points in from every edge: a corner's curve clears 0.29 of its radius in,
+/// and this covers radii up to 27 pt.
+#[cfg(target_os = "macos")]
+pub const WINDOW_CORNER_TRIM: f64 = 8.0;
+
+/// `bounds` with [`WINDOW_CORNER_TRIM`] taken off every side, or as it was
+/// when the window is too small to trim.
+#[cfg(target_os = "macos")]
+pub fn trim_window_corners(bounds: CropBounds) -> CropBounds {
+ let size = bounds.size();
+ let trim = WINDOW_CORNER_TRIM;
+ if size.width() <= trim * 4. || size.height() <= trim * 4. {
+ return bounds;
+ }
+ let position = bounds.position();
+ LogicalBounds::new(
+ LogicalPosition::new(position.x() + trim, position.y() + trim),
+ LogicalSize::new(size.width() - 2. * trim, size.height() - 2. * trim),
+ )
+}
+
#[cfg(windows)]
pub type CropBounds = PhysicalBounds;
@@ -487,6 +510,14 @@ impl<TCaptureFormat: ScreenCaptureFormat> ScreenCaptureConfig<TCaptureFormat> {
let target_refresh = validated_refresh_rate(display.refresh_rate());
let fps = std::cmp::max(1, std::cmp::min(max_fps, target_refresh));
+ // The capture reads the window inside its corners (see
+ // `WINDOW_CORNER_TRIM`), so the output and the cursor crop match that.
+ #[cfg(target_os = "macos")]
+ let crop_bounds = match window {
+ Some(_) => crop_bounds.map(trim_window_corners),
+ None => crop_bounds,
+ };
+
let native_output_size: PhysicalSize = {
#[cfg(target_os = "macos")]
{
@@ -1116,3 +1147,30 @@ mod tests {
assert_eq!(crop.size().height(), 450.0);
}
}
+
+#[cfg(all(test, target_os = "macos"))]
+mod window_corner_tests {
+ use super::*;
+
+ #[test]
+ fn window_capture_reads_inside_the_rounded_corners() {
+ let window = LogicalBounds::new(
+ LogicalPosition::new(100., 50.),
+ LogicalSize::new(920., 436.),
+ );
+ let trimmed = trim_window_corners(window);
+ assert_eq!(trimmed.position().x(), 108.);
+ assert_eq!(trimmed.position().y(), 58.);
+ assert_eq!(trimmed.size().width(), 904.);
+ assert_eq!(trimmed.size().height(), 420.);
+ }
+
+ #[test]
+ fn a_tiny_window_is_left_whole() {
+ let window = LogicalBounds::new(LogicalPosition::new(0., 0.), LogicalSize::new(30., 200.));
+ let kept = trim_window_corners(window);
+ assert_eq!(kept.position().x(), 0.);
+ assert_eq!(kept.size().width(), 30.);
+ assert_eq!(kept.size().height(), 200.);
+ }
+}