Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

feat(recording): record a window by itself only when asked, on macOS and Windows

Richie McIlroy3a936e34d40e
diff --git a/crates/recording/src/instant_recording.rs b/crates/recording/src/instant_recording.rs
index 9bfa49cd3..6c0102a11 100644
--- a/crates/recording/src/instant_recording.rs
+++ b/crates/recording/src/instant_recording.rs
@@ -859,6 +859,7 @@ impl Actor {
pub struct ActorBuilder {
output_path: PathBuf,
capture_target: ScreenCaptureTarget,
+ window_only: bool,
system_audio: bool,
mic_feed: Option<Arc<MicrophoneFeedLock>>,
camera_feed: Option<Arc<crate::feeds::camera::CameraFeedLock>>,
@@ -884,6 +885,7 @@ impl ActorBuilder {
Self {
output_path: output,
capture_target,
+ window_only: false,
system_audio: false,
mic_feed: None,
camera_feed: None,
@@ -922,6 +924,15 @@ impl ActorBuilder {
self
}
+ /// Record only the target window, following it, on macOS and Windows.
+ /// Ignored while a camera is attached there: Instant has no camera
+ /// compositing on those platforms, so the camera bubble only reaches
+ /// the video because the screen capture records it.
+ pub fn with_window_only(mut self, window_only: bool) -> Self {
+ self.window_only = window_only;
+ self
+ }
+
pub fn with_mic_feed(mut self, mic_feed: Arc<MicrophoneFeedLock>) -> Self {
self.mic_feed = Some(mic_feed);
self
@@ -986,6 +997,7 @@ impl ActorBuilder {
self.output_path,
RecordingBaseInputs {
capture_target: self.capture_target,
+ window_only: self.window_only,
capture_system_audio: self.system_audio,
mic_feed: self.mic_feed,
camera_feed: self.camera_feed,
@@ -1337,9 +1349,12 @@ 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());
+ #[cfg(any(target_os = "macos", windows))]
+ let capture_window = instant_capture_window(
+ &inputs.capture_target,
+ inputs.window_only,
+ inputs.camera_feed.is_some(),
+ );
let screen_source = ScreenCaptureConfig::<ScreenCaptureMethod>::init(
display,
@@ -1349,8 +1364,8 @@ async fn build_instant_recording_actor(
max_capture_size,
timestamps.system_time(),
inputs.capture_system_audio,
- #[cfg(windows)]
- inputs.capture_target.window(),
+ #[cfg(any(target_os = "macos", windows))]
+ capture_window,
#[cfg(target_os = "linux")]
crate::sources::screen_capture::LinuxCaptureSource::from_target(
&inputs.capture_target,
@@ -1364,8 +1379,6 @@ 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")?;
@@ -1575,30 +1588,33 @@ 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(
+// Instant mode on macOS and Windows 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(any(target_os = "macos", windows))]
+fn instant_capture_window(
target: &ScreenCaptureTarget,
+ window_only: bool,
camera_attached: bool,
) -> Option<scap_targets::WindowId> {
- target.window().filter(|_| !camera_attached)
+ crate::window_only_target(target, window_only).filter(|_| !camera_attached)
}
#[cfg(test)]
mod tests {
use super::*;
- #[cfg(target_os = "macos")]
+ #[cfg(any(target_os = "macos", windows))]
#[test]
- fn macos_window_target_captures_window_unless_camera_attached() {
+ fn instant_captures_the_window_only_when_asked_and_without_a_camera() {
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!(instant_capture_window(&window, true, false), Some(id));
+ assert_eq!(instant_capture_window(&window, true, true), None);
+ assert_eq!(instant_capture_window(&window, false, false), None);
assert_eq!(
- macos_capture_window(&ScreenCaptureTarget::CameraOnly, false),
+ instant_capture_window(&ScreenCaptureTarget::CameraOnly, true, false),
None
);
}
diff --git a/crates/recording/src/lib.rs b/crates/recording/src/lib.rs
index a7dcbbcc4..23e2f98dd 100644
--- a/crates/recording/src/lib.rs
+++ b/crates/recording/src/lib.rs
@@ -79,9 +79,21 @@ pub struct RecordingOptions {
pub mode: RecordingMode,
}
+/// The window a capture should record by itself: only for a window target
+/// with window-only capture on.
+pub fn window_only_target(
+ target: &ScreenCaptureTarget,
+ window_only: bool,
+) -> Option<scap_targets::WindowId> {
+ target.window().filter(|_| window_only)
+}
+
#[derive(Clone)]
pub struct RecordingBaseInputs {
pub capture_target: ScreenCaptureTarget,
+ /// For a window target on macOS and Windows: record only that window,
+ /// following it, instead of the region of the screen it starts in.
+ pub window_only: bool,
pub capture_system_audio: bool,
pub mic_feed: Option<Arc<MicrophoneFeedLock>>,
pub camera_feed: Option<Arc<CameraFeedLock>>,
diff --git a/crates/recording/src/sources/screen_capture/mod.rs b/crates/recording/src/sources/screen_capture/mod.rs
index 8f6adecb0..2a4b41f5d 100644
--- a/crates/recording/src/sources/screen_capture/mod.rs
+++ b/crates/recording/src/sources/screen_capture/mod.rs
@@ -380,11 +380,10 @@ pub struct Config {
crop_bounds: Option<CropBounds>,
fps: u32,
show_cursor: bool,
- #[cfg(windows)]
- window: Option<WindowId>,
#[cfg(target_os = "linux")]
linux_source: LinuxCaptureSource,
- #[cfg(target_os = "macos")]
+ /// Capture this window itself rather than a crop of its display.
+ #[cfg(any(target_os = "macos", windows))]
window: Option<WindowId>,
}
@@ -477,12 +476,11 @@ impl<TCaptureFormat: ScreenCaptureFormat> ScreenCaptureConfig<TCaptureFormat> {
max_capture_size: Option<(u32, u32)>,
start_time: SystemTime,
system_audio: bool,
- #[cfg(windows)] window: Option<WindowId>,
+ #[cfg(any(target_os = "macos", windows))] window: Option<WindowId>,
#[cfg(target_os = "linux")] linux_source: LinuxCaptureSource,
#[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");
@@ -542,11 +540,9 @@ impl<TCaptureFormat: ScreenCaptureFormat> ScreenCaptureConfig<TCaptureFormat> {
crop_bounds,
fps,
show_cursor,
- #[cfg(windows)]
- window,
#[cfg(target_os = "linux")]
linux_source,
- #[cfg(target_os = "macos")]
+ #[cfg(any(target_os = "macos", windows))]
window,
},
video_info: VideoInfo::from_raw_ffmpeg(
diff --git a/crates/recording/src/studio_recording.rs b/crates/recording/src/studio_recording.rs
index 75650d91d..4967455f6 100644
--- a/crates/recording/src/studio_recording.rs
+++ b/crates/recording/src/studio_recording.rs
@@ -2144,6 +2144,7 @@ pub struct ActorBuilder {
lifetime: StudioLifetimeOwner,
output_path: PathBuf,
capture_target: screen_capture::ScreenCaptureTarget,
+ window_only: bool,
system_audio: bool,
mic_feed: Option<Arc<MicrophoneFeedLock>>,
camera_feed: Option<Arc<CameraFeedLock>>,
@@ -2168,6 +2169,7 @@ impl ActorBuilder {
},
output_path: output,
capture_target,
+ window_only: false,
system_audio: false,
mic_feed: None,
camera_feed: None,
@@ -2188,6 +2190,12 @@ impl ActorBuilder {
self
}
+ /// Record only the target window, following it, on macOS and Windows.
+ pub fn with_window_only(mut self, window_only: bool) -> Self {
+ self.window_only = window_only;
+ self
+ }
+
/// Prime the capture pipeline ahead of the start cue: every source, encoder
/// and muxer is live once `build` returns, but nothing is recorded until the
/// gate is armed. See [`RecordingStartGate`].
@@ -2265,6 +2273,7 @@ impl ActorBuilder {
self.output_path,
RecordingBaseInputs {
capture_target: self.capture_target,
+ window_only: self.window_only,
capture_system_audio: self.system_audio,
mic_feed: self.mic_feed,
camera_feed: self.camera_feed,
@@ -3413,8 +3422,8 @@ async fn create_segment_pipeline(
max_capture_size,
start_time.system_time(),
base_inputs.capture_system_audio,
- #[cfg(windows)]
- capture_target.window(),
+ #[cfg(any(target_os = "macos", windows))]
+ crate::window_only_target(&capture_target, base_inputs.window_only),
#[cfg(target_os = "linux")]
sources::screen_capture::LinuxCaptureSource::from_target(&capture_target),
#[cfg(windows)]
@@ -3426,8 +3435,6 @@ 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")?;
@@ -3658,8 +3665,13 @@ async fn create_segment_pipeline(
crate::cursor::CursorCaptureTarget {
crop_bounds: cursor_crop_bounds,
display: cursor_display,
- #[cfg(any(target_os = "linux", windows))]
+ #[cfg(target_os = "linux")]
window: base_inputs.capture_target.window(),
+ #[cfg(windows)]
+ window: crate::window_only_target(
+ &base_inputs.capture_target,
+ base_inputs.window_only,
+ ),
},
cursors_dir.to_path_buf(),
prev_cursors,
@@ -3991,6 +4003,7 @@ mod tests {
root.join("content/cursors"),
RecordingBaseInputs {
capture_target: screen_capture::ScreenCaptureTarget::CameraOnly,
+ window_only: false,
capture_system_audio: false,
mic_feed: None,
camera_feed: None,
@@ -4101,6 +4114,7 @@ mod tests {
path.join("content/cursors"),
RecordingBaseInputs {
capture_target: target.clone(),
+ window_only: false,
capture_system_audio: false,
mic_feed: None,
camera_feed: None,
@@ -4199,6 +4213,7 @@ mod tests {
path.join("content/cursors"),
RecordingBaseInputs {
capture_target: target.clone(),
+ window_only: false,
capture_system_audio: false,
mic_feed: None,
camera_feed: None,
@@ -4336,6 +4351,7 @@ mod tests {
path.join("content/cursors"),
RecordingBaseInputs {
capture_target: target.clone(),
+ window_only: false,
capture_system_audio: false,
mic_feed: None,
camera_feed: None,
@@ -4580,6 +4596,7 @@ mod tests {
path.join("content/cursors"),
RecordingBaseInputs {
capture_target: target.clone(),
+ window_only: false,
capture_system_audio: false,
mic_feed: None,
camera_feed: None,
@@ -6259,6 +6276,7 @@ mod tests {
recording_dir.join("content/cursors"),
RecordingBaseInputs {
capture_target: screen_capture::ScreenCaptureTarget::CameraOnly,
+ window_only: false,
capture_system_audio: false,
mic_feed: None,
camera_feed: None,
@@ -6506,6 +6524,7 @@ mod windows_cancel_tests {
PathBuf::new(),
RecordingBaseInputs {
capture_target: screen_capture::ScreenCaptureTarget::CameraOnly,
+ window_only: false,
capture_system_audio: false,
mic_feed: None,
camera_feed: None,