Open source Loom alternative. Beautiful, shareable screen recordings.
Rust appcapcossloommacnextjsnextjs15open-sourceossreactrecordrustscreen-capturescreen-recorderscreenshotsolidjstauritauri-apptypescriptvite
fix(recording): capture the selected window itself on macOS
Minit Jainb3a4b48a1b3d
diff --git a/crates/recording/src/instant_recording.rs b/crates/recording/src/instant_recording.rsindex 759025f1f..100ed1717 100644--- a/crates/recording/src/instant_recording.rs+++ b/crates/recording/src/instant_recording.rs@@ -1324,6 +1324,10 @@ async fn build_instant_recording_actor(#[cfg(not(target_os = "macos"))]let max_capture_size = None;+ #[cfg(target_os = "macos")]+ let capture_window =+ macos_capture_window(&inputs.capture_target, inputs.camera_feed.is_some());+let screen_source = ScreenCaptureConfig::<ScreenCaptureMethod>::init(display,crop_bounds,@@ -1345,6 +1349,8 @@ async fn build_instant_recording_actor(.ok_or_else(|| anyhow::anyhow!("Missing shareable content"))?,#[cfg(target_os = "macos")]inputs.excluded_windows,+ #[cfg(target_os = "macos")]+ capture_window,).await.context("screen capture init")?;@@ -1554,10 +1560,34 @@ fn clamp_size(input: (u32, u32), max: (u32, u32)) -> (u32, u32) {}}+// macOS Instant mode has no camera compositing: the camera preview only reaches the+// video because the display capture records it, so a window-only capture would drop it.+#[cfg(target_os = "macos")]+fn macos_capture_window(+ target: &ScreenCaptureTarget,+ camera_attached: bool,+) -> Option<scap_targets::WindowId> {+ target.window().filter(|_| !camera_attached)+}+#[cfg(test)]mod tests {use super::*;+ #[cfg(target_os = "macos")]+ #[test]+ fn macos_window_target_captures_window_unless_camera_attached() {+ let id: scap_targets::WindowId = "247".parse().unwrap();+ let window = ScreenCaptureTarget::Window { id: id.clone() };++ assert_eq!(macos_capture_window(&window, false), Some(id));+ assert_eq!(macos_capture_window(&window, true), None);+ assert_eq!(+ macos_capture_window(&ScreenCaptureTarget::CameraOnly, false),+ None+ );+ }+#[cfg(target_os = "linux")]#[test]fn linux_presentation_builder_preserves_explicit_config_and_cli_default() {diff --git a/crates/recording/src/sources/screen_capture/macos.rs b/crates/recording/src/sources/screen_capture/macos.rsindex 66972d65e..be29055e6 100644--- a/crates/recording/src/sources/screen_capture/macos.rs+++ b/crates/recording/src/sources/screen_capture/macos.rs@@ -28,6 +28,29 @@ use tracing::{debug, error, info, warn};const MAX_CAPTURE_RESTARTS: u32 = 3;const RESTART_DELAY: Duration = Duration::from_secs(2);+fn window_content_filter(+ window_id: &WindowId,+ content: &sc::ShareableContent,+ capture_phase: &'static str,+) -> Option<arc::R<sc::ContentFilter>> {+ let Ok(native_id) = window_id.to_string().parse::<u32>() else {+ warn!(%window_id, capture_phase, "Capture window has an invalid native id");+ return None;+ };++ let windows = content.windows();+ let Some(window) = windows.iter().find(|window| window.id() == native_id) else {+ warn!(+ %window_id,+ capture_phase,+ "Capture window missing from ScreenCaptureKit shareable content, falling back to display crop"+ );+ return None;+ };++ Some(sc::ContentFilter::with_desktop_independent_window(window))+}+fn excluded_shareable_windows(excluded_windows: &[WindowId],content: &sc::ShareableContent,@@ -219,16 +242,31 @@ impl ScreenCaptureConfig<CMSampleBufferCapture> {let display = Display::from_id(&self.config.display).ok_or_else(|| SourceError::NoDisplay(self.config.display.clone()))?;- let excluded_sc_windows =- excluded_shareable_windows(&self.excluded_windows, &self.shareable_content, "initial");+ let window_filter = self+ .config+ .window+ .as_ref()+ .and_then(|id| window_content_filter(id, &self.shareable_content, "initial"));+ let captures_window = window_filter.is_some();++ let content_filter = match window_filter {+ Some(filter) => filter,+ None => {+ let excluded_sc_windows = excluded_shareable_windows(+ &self.excluded_windows,+ &self.shareable_content,+ "initial",+ );- let content_filter = display- .raw_handle()- .as_content_filter_excluding_windows(- self.shareable_content.clone(),- excluded_sc_windows,- )- .ok_or(SourceError::AsContentFilter)?;+ display+ .raw_handle()+ .as_content_filter_excluding_windows(+ self.shareable_content.clone(),+ excluded_sc_windows,+ )+ .ok_or(SourceError::AsContentFilter)?+ }+ };debug!("SCK content filter: {:?}", content_filter);@@ -257,7 +295,7 @@ impl ScreenCaptureConfig<CMSampleBufferCapture> {settings.set_pixel_format(cv::PixelFormat::_420V);settings.set_color_space_name(cg::color_space::names::srgb());- if let Some(crop_bounds) = self.config.crop_bounds {+ if !captures_window && let Some(crop_bounds) = self.config.crop_bounds {debug!("crop bounds: {:?}", crop_bounds);settings.set_src_rect(cg::Rect::new(crop_bounds.position().x(),@@ -948,13 +986,25 @@ async fn rebuild_capturer(params: &CapturerRebuildParams) -> anyhow::Result<Captlet display = Display::from_id(¶ms.display_id).ok_or_else(|| anyhow!("Display not found during restart: {:?}", params.display_id))?;- let excluded_sc_windows =- excluded_shareable_windows(¶ms.excluded_windows, &shareable_content, "restart");-- let content_filter = display- .raw_handle()- .as_content_filter_excluding_windows(shareable_content, excluded_sc_windows)- .ok_or_else(|| anyhow!("Failed to create content filter during restart"))?;+ let window_filter = params+ .config+ .window+ .as_ref()+ .and_then(|id| window_content_filter(id, &shareable_content, "restart"));+ let captures_window = window_filter.is_some();++ let content_filter = match window_filter {+ Some(filter) => filter,+ None => {+ let excluded_sc_windows =+ excluded_shareable_windows(¶ms.excluded_windows, &shareable_content, "restart");++ display+ .raw_handle()+ .as_content_filter_excluding_windows(shareable_content, excluded_sc_windows)+ .ok_or_else(|| anyhow!("Failed to create content filter during restart"))?+ }+ };let size = PhysicalSize::new(params.video_info.width as f64,@@ -977,7 +1027,7 @@ async fn rebuild_capturer(params: &CapturerRebuildParams) -> anyhow::Result<Captsettings.set_pixel_format(cv::PixelFormat::_420V);settings.set_color_space_name(cg::color_space::names::srgb());- if let Some(crop_bounds) = params.config.crop_bounds {+ 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(),diff --git a/crates/recording/src/sources/screen_capture/mod.rs b/crates/recording/src/sources/screen_capture/mod.rsindex 6a61479f0..a7e31a429 100644--- a/crates/recording/src/sources/screen_capture/mod.rs+++ b/crates/recording/src/sources/screen_capture/mod.rs@@ -378,6 +378,8 @@ pub struct Config {show_cursor: bool,#[cfg(target_os = "linux")]linux_source: LinuxCaptureSource,+ #[cfg(target_os = "macos")]+ window: Option<WindowId>,}#[cfg(target_os = "macos")]@@ -473,6 +475,7 @@ impl<TCaptureFormat: ScreenCaptureFormat> ScreenCaptureConfig<TCaptureFormat> {#[cfg(windows)] d3d_device: ::windows::Win32::Graphics::Direct3D11::ID3D11Device,#[cfg(target_os = "macos")] shareable_content: SendableShareableContent,#[cfg(target_os = "macos")] excluded_windows: Vec<WindowId>,+ #[cfg(target_os = "macos")] window: Option<WindowId>,) -> Result<Self, ScreenCaptureInitError> {cap_fail::fail!("ScreenCaptureSource::init");@@ -531,6 +534,8 @@ impl<TCaptureFormat: ScreenCaptureFormat> ScreenCaptureConfig<TCaptureFormat> {show_cursor,#[cfg(target_os = "linux")]linux_source,+ #[cfg(target_os = "macos")]+ window,},video_info: VideoInfo::from_raw_ffmpeg(TCaptureFormat::pixel_format(),diff --git a/crates/recording/src/studio_recording.rs b/crates/recording/src/studio_recording.rsindex 1ff44d06f..35861305f 100644--- a/crates/recording/src/studio_recording.rs+++ b/crates/recording/src/studio_recording.rs@@ -3281,6 +3281,8 @@ async fn create_segment_pipeline(.ok_or_else(|| anyhow!("Missing shareable content"))?,#[cfg(target_os = "macos")]base_inputs.excluded_windows.clone(),+ #[cfg(target_os = "macos")]+ capture_target.window(),).await.context("screen capture init")?;