Skip to main content

gpui_platform/
screen_capture.rs

1//! Screen capture vocabulary shared by `gpui` and its platform backends.
2
3use crate::ForegroundExecutor;
4use anyhow::Result;
5use futures::channel::oneshot;
6use gpui_shared_string::SharedString;
7use gpui_types::{DevicePixels, Size};
8
9#[cfg(all(
10    any(target_os = "windows", target_os = "linux"),
11    feature = "screen-capture"
12))]
13/// The platform-specific representation of a captured frame.
14pub type PlatformScreenCaptureFrame = scap::frame::Frame;
15#[cfg(not(feature = "screen-capture"))]
16/// The platform-specific representation of a captured frame.
17pub type PlatformScreenCaptureFrame = ();
18#[cfg(all(target_os = "macos", feature = "screen-capture"))]
19/// The platform-specific representation of a captured frame.
20pub type PlatformScreenCaptureFrame =
21    objc2_core_foundation::CFRetained<objc2_core_video::CVImageBuffer>;
22
23/// Metadata for a given screen capture source
24#[derive(Clone)]
25pub struct SourceMetadata {
26    /// Opaque identifier of this screen.
27    pub id: u64,
28    /// Human-readable label for this source.
29    pub label: Option<SharedString>,
30    /// Whether this source is the main display.
31    pub is_main: Option<bool>,
32    /// Video resolution of this source.
33    pub resolution: Size<DevicePixels>,
34}
35
36/// A source of on-screen video content that can be captured.
37pub trait ScreenCaptureSource {
38    /// Returns metadata for this source.
39    fn metadata(&self) -> Result<SourceMetadata>;
40
41    /// Start capture video from this source, invoking the given callback
42    /// with each frame.
43    fn stream(
44        &self,
45        foreground_executor: &ForegroundExecutor,
46        frame_callback: Box<dyn Fn(ScreenCaptureFrame) + Send>,
47    ) -> oneshot::Receiver<Result<Box<dyn ScreenCaptureStream>>>;
48}
49
50/// A video stream captured from a screen.
51pub trait ScreenCaptureStream {
52    /// Returns metadata for this source.
53    fn metadata(&self) -> Result<SourceMetadata>;
54}
55
56/// A frame of video captured from a screen.
57pub struct ScreenCaptureFrame(pub PlatformScreenCaptureFrame);