Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

perf(recording): let ScreenCaptureKit scale Studio captures above 4096px (#2465)

Aatman Jain (AJ)886548083357
diff --git a/crates/recording/src/sources/screen_capture/mod.rs b/crates/recording/src/sources/screen_capture/mod.rs
index 6a61479f0..15dfd7298 100644
--- a/crates/recording/src/sources/screen_capture/mod.rs
+++ b/crates/recording/src/sources/screen_capture/mod.rs
@@ -714,6 +714,30 @@ fn list_windows_inner(_include_accessory_panels: bool) -> Vec<(CaptureWindow, Wi
mod tests {
use super::*;
+ #[test]
+ fn h264_bounded_capture_matches_the_encoder_downscale() {
+ for (width, height) in [
+ (5120, 2880),
+ (6016, 3384),
+ (5120, 2160),
+ (2880, 5120),
+ (4096, 2304),
+ ] {
+ let bounded = constrain_capture_size(
+ PhysicalSize::new(width as f64, height as f64),
+ Some((crate::H264_MAX_DIMENSION, crate::H264_MAX_DIMENSION)),
+ );
+ let encoder_size =
+ crate::calculate_gpu_compatible_size(width, height, crate::H264_MAX_DIMENSION)
+ .unwrap_or((width, height));
+ assert_eq!(
+ (bounded.width() as u32, bounded.height() as u32),
+ encoder_size,
+ "{width}x{height}"
+ );
+ }
+ }
+
#[test]
#[cfg(target_os = "linux")]
fn linux_window_source_preserves_selected_window_id() {
diff --git a/crates/recording/src/studio_recording.rs b/crates/recording/src/studio_recording.rs
index 1ff44d06f..4917df973 100644
--- a/crates/recording/src/studio_recording.rs
+++ b/crates/recording/src/studio_recording.rs
@@ -72,6 +72,17 @@ fn camera_active_max_capture_size(
}
}
+fn studio_max_capture_size(
+ quality: crate::StudioQuality,
+ camera_active: bool,
+) -> Option<(u32, u32)> {
+ // ScreenCaptureKit scales on the GPU. Unbounded, a 5K or 6K display is
+ // captured at full size and every frame is scaled down to the H.264 limit
+ // on the CPU, which also rules out zero-copy VideoToolbox input.
+ camera_active_max_capture_size(quality, camera_active)
+ .or_else(|| cfg!(target_os = "macos").then_some((H264_MAX_DIMENSION, H264_MAX_DIMENSION)))
+}
+
#[allow(clippy::large_enum_variant)]
enum ActorState {
Recording {
@@ -3255,7 +3266,7 @@ async fn create_segment_pipeline(
let (display, crop) =
target_to_display_and_crop(&capture_target).context("target_display_crop")?;
let compatibility_quality = matches!(quality, crate::StudioQuality::Compatibility);
- let max_capture_size = camera_active_max_capture_size(quality, camera_active);
+ let max_capture_size = studio_max_capture_size(quality, camera_active);
let effective_max_fps = if compatibility_quality && camera_active {
max_fps.min(24)
} else {
@@ -5446,6 +5457,23 @@ mod tests {
}
}
+ #[test]
+ fn studio_capture_is_bounded_to_the_h264_limit_on_macos() {
+ for quality in [
+ crate::StudioQuality::Compatibility,
+ crate::StudioQuality::Balanced,
+ crate::StudioQuality::Ultra,
+ ] {
+ let expected =
+ cfg!(target_os = "macos").then_some((H264_MAX_DIMENSION, H264_MAX_DIMENSION));
+ assert_eq!(studio_max_capture_size(quality, false), expected);
+ }
+ assert_eq!(
+ studio_max_capture_size(crate::StudioQuality::Compatibility, true),
+ camera_active_max_capture_size(crate::StudioQuality::Compatibility, true),
+ );
+ }
+
#[test]
fn finalize_optional_track_records_stop_failure() {
let failures = Arc::new(std::sync::Mutex::new(Vec::new()));