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 = core_video::image_buffer::CVImageBuffer;
21
22/// Metadata for a given screen capture source
23#[derive(Clone)]
24pub struct SourceMetadata {
25    /// Opaque identifier of this screen.
26    pub id: u64,
27    /// Human-readable label for this source.
28    pub label: Option<SharedString>,
29    /// Whether this source is the main display.
30    pub is_main: Option<bool>,
31    /// Video resolution of this source.
32    pub resolution: Size<DevicePixels>,
33}
34
35/// A source of on-screen video content that can be captured.
36pub trait ScreenCaptureSource {
37    /// Returns metadata for this source.
38    fn metadata(&self) -> Result<SourceMetadata>;
39
40    /// Start capture video from this source, invoking the given callback
41    /// with each frame.
42    fn stream(
43        &self,
44        foreground_executor: &ForegroundExecutor,
45        frame_callback: Box<dyn Fn(ScreenCaptureFrame) + Send>,
46    ) -> oneshot::Receiver<Result<Box<dyn ScreenCaptureStream>>>;
47}
48
49/// A video stream captured from a screen.
50pub trait ScreenCaptureStream {
51    /// Returns metadata for this source.
52    fn metadata(&self) -> Result<SourceMetadata>;
53}
54
55/// A frame of video captured from a screen.
56pub struct ScreenCaptureFrame(pub PlatformScreenCaptureFrame);