| 1 | // Copyright 2025 LiveKit, Inc. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | use crate::imp::desktop_capturer as imp_dc; |
| 16 | |
| 17 | /// Configuration options for creating a desktop capturer. |
| 18 | /// |
| 19 | /// It contains a subset of libwebrtc's DesktopCaptureOptions. |
| 20 | /// |
| 21 | /// By default, it captures the entire screen and does not include the cursor. |
| 22 | /// |
| 23 | /// # Example |
| 24 | /// ```no_run |
| 25 | /// use libwebrtc::desktop_capturer::{DesktopCapturerOptions, DesktopCaptureSourceType}; |
| 26 | /// |
| 27 | /// let mut options = DesktopCapturerOptions::new(DesktopCaptureSourceType::Screen); |
| 28 | /// options.set_include_cursor(true); |
| 29 | /// ``` |
| 30 | pub struct DesktopCapturerOptions { |
| 31 | sys_handle: imp_dc::DesktopCapturerOptions, |
| 32 | } |
| 33 | |
| 34 | /// Specifies the type of source that a desktop capturer should capture. |
| 35 | #[derive(Debug, Copy, Clone, PartialEq)] |
| 36 | pub enum DesktopCaptureSourceType { |
| 37 | Screen, |
| 38 | Window, |
| 39 | #[cfg(any(target_os = "macos", target_os = "linux"))] |
| 40 | Generic, |
| 41 | } |
| 42 | |
| 43 | impl DesktopCapturerOptions { |
| 44 | /// Creates a new `DesktopCapturerOptions` with default values. |
| 45 | /// |
| 46 | /// # Arguments |
| 47 | /// |
| 48 | /// * `source_type` - The type of source to capture (screen or window). |
| 49 | /// |
| 50 | /// # Defaults |
| 51 | /// |
| 52 | /// - Cursor is not included in captured frames (use [`set_include_cursor`](Self::set_include_cursor) to change) |
| 53 | /// - On macOS, the ScreenCaptureKit system picker is enabled (use [`set_sck_system_picker`](Self::set_sck_system_picker) to change) |
| 54 | pub fn new(source_type: DesktopCaptureSourceType) -> Self { |
| 55 | let source_type = match source_type { |
| 56 | DesktopCaptureSourceType::Screen => imp_dc::SourceType::Screen, |
| 57 | DesktopCaptureSourceType::Window => imp_dc::SourceType::Window, |
| 58 | #[cfg(any(target_os = "macos", target_os = "linux"))] |
| 59 | DesktopCaptureSourceType::Generic => imp_dc::SourceType::Generic, |
| 60 | }; |
| 61 | Self { sys_handle: imp_dc::DesktopCapturerOptions::new(source_type) } |
| 62 | } |
| 63 | |
| 64 | /// Sets whether to include the cursor in captured frames. |
| 65 | pub fn set_include_cursor(&mut self, include: bool) { |
| 66 | self.sys_handle = self.sys_handle.with_cursor(include); |
| 67 | } |
| 68 | |
| 69 | /// Sets whether to allow the ScreenCaptureKit system picker on macOS. |
| 70 | /// |
| 71 | /// This is enabled by default. |
| 72 | /// |
| 73 | /// When disabled, for capturing displays the client should get the source id |
| 74 | /// via a different way as [`DesktopCapturer::get_source_list`] returns an empty vector. |
| 75 | #[cfg(target_os = "macos")] |
| 76 | pub fn set_sck_system_picker(&mut self, allow_sck_system_picker: bool) { |
| 77 | self.sys_handle = self.sys_handle.with_sck_system_picker(allow_sck_system_picker); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | /// A desktop capturer for capturing screens or windows. |
| 82 | pub struct DesktopCapturer { |
| 83 | handle: imp_dc::DesktopCapturer, |
| 84 | } |
| 85 | |
| 86 | impl DesktopCapturer { |
| 87 | /// Creates a new `DesktopCapturer` with the specified callback and options. |
| 88 | /// |
| 89 | /// # Arguments |
| 90 | /// |
| 91 | /// * `options` - Configuration options for the capturer |
| 92 | /// |
| 93 | /// # Returns |
| 94 | /// |
| 95 | /// Returns `Some(DesktopCapturer)` if the capturer was created successfully, |
| 96 | /// or `None` if creation failed (e.g., due to platform limitations or permissions). |
| 97 | pub fn new(options: DesktopCapturerOptions) -> Option<Self> { |
| 98 | let desktop_capturer = imp_dc::DesktopCapturer::new(options.sys_handle); |
| 99 | if desktop_capturer.is_none() { |
| 100 | return None; |
| 101 | } |
| 102 | Some(Self { handle: desktop_capturer.unwrap() }) |
| 103 | } |
| 104 | |
| 105 | /// Starts capturing from the specified source. |
| 106 | /// |
| 107 | /// # Arguments |
| 108 | /// |
| 109 | /// * `source` - The capture source to use. It should be None when the capturer |
| 110 | /// is configured to use the system picker (on platforms that support it). |
| 111 | /// * `callback` - A function that will be called for each captured frame. The callback |
| 112 | /// receives a [`CaptureResult`] indicating success or error, and a [`DesktopFrame`] |
| 113 | /// containing the captured image data. |
| 114 | /// |
| 115 | /// # Note |
| 116 | /// |
| 117 | /// After calling this method, you must call [`capture_frame`](Self::capture_frame) |
| 118 | /// to actually capture frames. This method only initializes the capture session. |
| 119 | pub fn start_capture<T>(&mut self, source: Option<CaptureSource>, mut callback: T) |
| 120 | where |
| 121 | T: FnMut(Result<DesktopFrame, CaptureError>) + Send + 'static, |
| 122 | { |
| 123 | if let Some(source) = source { |
| 124 | self.handle.select_source(source.sys_handle.id()); |
| 125 | } |
| 126 | let inner_callback = move |result: Result<imp_dc::DesktopFrame, imp_dc::CaptureError>| { |
| 127 | callback(capture_result_from_sys(result)); |
| 128 | }; |
| 129 | self.handle.start(inner_callback); |
| 130 | } |
| 131 | |
| 132 | /// Captures a single frame. |
| 133 | /// |
| 134 | /// You must call [`start_capture`](Self::start_capture) before calling this method. |
| 135 | pub fn capture_frame(&mut self) { |
| 136 | self.handle.capture_frame(); |
| 137 | } |
| 138 | |
| 139 | /// Retrieves a list of available capture sources. |
| 140 | /// |
| 141 | /// Returns a list of screens or windows that can be captured, depending |
| 142 | /// on whether the capturer was configured for window or screen capture. |
| 143 | /// |
| 144 | /// # Returns |
| 145 | /// |
| 146 | /// A vector of [`CaptureSource`] objects representing available capture sources. |
| 147 | pub fn get_source_list(&self) -> Vec<CaptureSource> { |
| 148 | let source_list = self.handle.get_source_list(); |
| 149 | source_list.into_iter().map(|source| CaptureSource { sys_handle: source }).collect() |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | pub struct DesktopFrame { |
| 154 | sys_handle: imp_dc::DesktopFrame, |
| 155 | } |
| 156 | |
| 157 | impl DesktopFrame { |
| 158 | fn new(sys_handle: imp_dc::DesktopFrame) -> Self { |
| 159 | Self { sys_handle } |
| 160 | } |
| 161 | |
| 162 | pub fn width(&self) -> i32 { |
| 163 | self.sys_handle.width() as i32 |
| 164 | } |
| 165 | |
| 166 | pub fn height(&self) -> i32 { |
| 167 | self.sys_handle.height() as i32 |
| 168 | } |
| 169 | |
| 170 | pub fn stride(&self) -> u32 { |
| 171 | self.sys_handle.stride() as u32 |
| 172 | } |
| 173 | |
| 174 | pub fn left(&self) -> i32 { |
| 175 | self.sys_handle.left() |
| 176 | } |
| 177 | |
| 178 | pub fn top(&self) -> i32 { |
| 179 | self.sys_handle.top() |
| 180 | } |
| 181 | |
| 182 | pub fn data(&self) -> &[u8] { |
| 183 | self.sys_handle.data() |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | #[derive(Clone)] |
| 188 | pub struct CaptureSource { |
| 189 | sys_handle: imp_dc::CaptureSource, |
| 190 | } |
| 191 | |
| 192 | impl CaptureSource { |
| 193 | pub fn id(&self) -> u64 { |
| 194 | self.sys_handle.id() |
| 195 | } |
| 196 | pub fn title(&self) -> String { |
| 197 | self.sys_handle.title() |
| 198 | } |
| 199 | pub fn display_id(&self) -> i64 { |
| 200 | self.sys_handle.display_id() |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | impl std::fmt::Display for CaptureSource { |
| 205 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 206 | f.debug_struct("CaptureSource") |
| 207 | .field("id", &self.id()) |
| 208 | .field("title", &self.title()) |
| 209 | .field("display_id", &self.display_id()) |
| 210 | .finish() |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | #[derive(Debug, PartialEq)] |
| 215 | pub enum CaptureError { |
| 216 | Temporary, |
| 217 | Permanent, |
| 218 | } |
| 219 | |
| 220 | fn capture_result_from_sys( |
| 221 | result: Result<imp_dc::DesktopFrame, imp_dc::CaptureError>, |
| 222 | ) -> Result<DesktopFrame, CaptureError> { |
| 223 | match result { |
| 224 | Ok(frame) => Ok(DesktopFrame::new(frame)), |
| 225 | Err(error) => Err(match error { |
| 226 | imp_dc::CaptureError::Temporary => CaptureError::Temporary, |
| 227 | imp_dc::CaptureError::Permanent => CaptureError::Permanent, |
| 228 | }), |
| 229 | } |
| 230 | } |