| 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 cxx::UniquePtr; |
| 16 | use webrtc_sys::desktop_capturer::{self as sys_dc, ffi::new_desktop_capturer}; |
| 17 | |
| 18 | pub(crate) use sys_dc::PickerEvent; |
| 19 | |
| 20 | #[derive(Debug, Copy, Clone, PartialEq)] |
| 21 | pub(crate) enum SourceType { |
| 22 | Screen, |
| 23 | Window, |
| 24 | #[cfg(any(target_os = "macos", target_os = "linux"))] |
| 25 | Generic, |
| 26 | } |
| 27 | |
| 28 | #[derive(Copy, Clone, Debug)] |
| 29 | pub(crate) struct DesktopCapturerOptions { |
| 30 | source_type: SourceType, |
| 31 | include_cursor: bool, |
| 32 | #[cfg(target_os = "macos")] |
| 33 | allow_sck_system_picker: bool, |
| 34 | } |
| 35 | |
| 36 | impl Default for DesktopCapturerOptions { |
| 37 | fn default() -> Self { |
| 38 | Self { |
| 39 | source_type: SourceType::Screen, |
| 40 | include_cursor: false, |
| 41 | #[cfg(target_os = "macos")] |
| 42 | allow_sck_system_picker: true, |
| 43 | } |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | impl DesktopCapturerOptions { |
| 48 | pub(crate) fn new(source_type: SourceType) -> Self { |
| 49 | Self { source_type, ..Default::default() } |
| 50 | } |
| 51 | |
| 52 | pub(crate) fn with_cursor(mut self, include: bool) -> Self { |
| 53 | self.include_cursor = include; |
| 54 | self |
| 55 | } |
| 56 | |
| 57 | #[cfg(target_os = "macos")] |
| 58 | pub(crate) fn with_sck_system_picker(mut self, allow_sck_system_picker: bool) -> Self { |
| 59 | self.allow_sck_system_picker = allow_sck_system_picker; |
| 60 | self |
| 61 | } |
| 62 | |
| 63 | pub(crate) fn to_sys_handle(&self) -> sys_dc::ffi::DesktopCapturerOptions { |
| 64 | let source_type = match self.source_type { |
| 65 | SourceType::Screen => sys_dc::ffi::SourceType::Screen, |
| 66 | SourceType::Window => sys_dc::ffi::SourceType::Window, |
| 67 | #[cfg(any(target_os = "macos", target_os = "linux"))] |
| 68 | SourceType::Generic => sys_dc::ffi::SourceType::Generic, |
| 69 | }; |
| 70 | let sys_handle = sys_dc::ffi::DesktopCapturerOptions { |
| 71 | source_type, |
| 72 | include_cursor: self.include_cursor, |
| 73 | allow_sck_system_picker: { |
| 74 | #[cfg(target_os = "macos")] |
| 75 | { |
| 76 | self.allow_sck_system_picker |
| 77 | } |
| 78 | #[cfg(not(target_os = "macos"))] |
| 79 | { |
| 80 | false |
| 81 | } |
| 82 | }, |
| 83 | }; |
| 84 | |
| 85 | sys_handle |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | #[cfg(all(any(target_os = "linux", target_os = "freebsd"), feature = "glib-main-loop"))] |
| 90 | mod shared_glib { |
| 91 | use std::sync::OnceLock; |
| 92 | |
| 93 | static LOOP: OnceLock<glib::MainLoop> = OnceLock::new(); |
| 94 | |
| 95 | pub(super) fn ensure() { |
| 96 | LOOP.get_or_init(|| { |
| 97 | let main_loop = glib::MainLoop::new(None, false); |
| 98 | let running = main_loop.clone(); |
| 99 | std::thread::Builder::new() |
| 100 | .name("glib-portal".into()) |
| 101 | .spawn(move || running.run()) |
| 102 | .expect("glib portal loop"); |
| 103 | main_loop |
| 104 | }); |
| 105 | } |
| 106 | |
| 107 | pub(super) fn id() -> usize { |
| 108 | ensure(); |
| 109 | LOOP.get().map(|main_loop| main_loop as *const glib::MainLoop as usize).unwrap_or(0) |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | pub(crate) fn ensure_shared_glib_loop() { |
| 114 | #[cfg(all(any(target_os = "linux", target_os = "freebsd"), feature = "glib-main-loop"))] |
| 115 | shared_glib::ensure(); |
| 116 | } |
| 117 | |
| 118 | pub(crate) fn shared_glib_loop_id() -> usize { |
| 119 | #[cfg(all(any(target_os = "linux", target_os = "freebsd"), feature = "glib-main-loop"))] |
| 120 | { |
| 121 | return shared_glib::id(); |
| 122 | } |
| 123 | #[cfg(not(all(any(target_os = "linux", target_os = "freebsd"), feature = "glib-main-loop")))] |
| 124 | { |
| 125 | 0 |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | pub(crate) struct DesktopCapturer { |
| 130 | sys_handle: UniquePtr<sys_dc::ffi::DesktopCapturer>, |
| 131 | } |
| 132 | |
| 133 | impl DesktopCapturer { |
| 134 | pub(crate) fn new(options: DesktopCapturerOptions) -> Option<Self> { |
| 135 | let sys_handle = new_desktop_capturer(options.to_sys_handle()); |
| 136 | if sys_handle.is_null() { |
| 137 | None |
| 138 | } else { |
| 139 | Some(Self { sys_handle }) |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | pub(crate) fn capture_frame(&self) { |
| 144 | self.sys_handle.capture_frame(); |
| 145 | } |
| 146 | |
| 147 | pub(crate) fn set_max_frame_rate(&self, max_fps: u32) { |
| 148 | self.sys_handle.set_max_frame_rate(max_fps); |
| 149 | } |
| 150 | |
| 151 | pub(crate) fn start<T>(&mut self, callback: T) |
| 152 | where |
| 153 | T: FnMut(Result<DesktopFrame, CaptureError>) + Send + 'static, |
| 154 | { |
| 155 | #[cfg(all(any(target_os = "linux", target_os = "freebsd"), feature = "glib-main-loop"))] |
| 156 | if std::env::var("WAYLAND_DISPLAY").is_ok() { |
| 157 | ensure_shared_glib_loop(); |
| 158 | } |
| 159 | let pin_handle = self.sys_handle.pin_mut(); |
| 160 | let callback = DesktopCallback::new(callback); |
| 161 | let callback_wrapper = sys_dc::DesktopCapturerCallbackWrapper::new(Box::new(callback)); |
| 162 | pin_handle.start(Box::new(callback_wrapper)); |
| 163 | } |
| 164 | |
| 165 | pub(crate) fn select_source(&self, id: u64) -> bool { |
| 166 | self.sys_handle.select_source(id) |
| 167 | } |
| 168 | |
| 169 | pub(crate) fn get_source_list(&self) -> Vec<CaptureSource> { |
| 170 | let mut sources = Vec::new(); |
| 171 | let source_list = self.sys_handle.get_source_list(); |
| 172 | for source in source_list.iter() { |
| 173 | sources.push(CaptureSource { sys_handle: source.clone() }); |
| 174 | } |
| 175 | sources |
| 176 | } |
| 177 | |
| 178 | pub(crate) fn has_delegated_picker(&self) -> bool { |
| 179 | self.sys_handle.has_delegated_picker() |
| 180 | } |
| 181 | |
| 182 | pub(crate) fn observe_picker(&mut self, callback: impl FnMut(sys_dc::PickerEvent) + Send + 'static) { |
| 183 | let wrapper = sys_dc::PortalPickerCallbackWrapper::new(Box::new(callback)); |
| 184 | self.sys_handle.pin_mut().observe_picker(Box::new(wrapper)); |
| 185 | } |
| 186 | |
| 187 | pub(crate) fn ensure_picker_visible(&mut self) { |
| 188 | #[cfg(all(any(target_os = "linux", target_os = "freebsd"), feature = "glib-main-loop"))] |
| 189 | if std::env::var("WAYLAND_DISPLAY").is_ok() { |
| 190 | ensure_shared_glib_loop(); |
| 191 | } |
| 192 | self.sys_handle.pin_mut().ensure_picker_visible(); |
| 193 | } |
| 194 | |
| 195 | pub(crate) fn ensure_picker_hidden(&mut self) { |
| 196 | self.sys_handle.pin_mut().ensure_picker_hidden(); |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | pub(crate) struct DesktopFrame { |
| 201 | sys_handle: UniquePtr<sys_dc::ffi::DesktopFrame>, |
| 202 | } |
| 203 | |
| 204 | impl DesktopFrame { |
| 205 | fn new(sys_handle: UniquePtr<sys_dc::ffi::DesktopFrame>) -> Self { |
| 206 | Self { sys_handle } |
| 207 | } |
| 208 | |
| 209 | pub(crate) fn width(&self) -> i32 { |
| 210 | self.sys_handle.width() |
| 211 | } |
| 212 | |
| 213 | pub(crate) fn height(&self) -> i32 { |
| 214 | self.sys_handle.height() |
| 215 | } |
| 216 | |
| 217 | pub(crate) fn stride(&self) -> u32 { |
| 218 | self.sys_handle.stride() as u32 |
| 219 | } |
| 220 | |
| 221 | pub(crate) fn left(&self) -> i32 { |
| 222 | self.sys_handle.left() |
| 223 | } |
| 224 | |
| 225 | pub(crate) fn top(&self) -> i32 { |
| 226 | self.sys_handle.top() |
| 227 | } |
| 228 | |
| 229 | pub(crate) fn data(&self) -> &[u8] { |
| 230 | let data = self.sys_handle.data(); |
| 231 | unsafe { std::slice::from_raw_parts(data, self.stride() as usize * self.height() as usize) } |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | struct DesktopCallback<T: FnMut(Result<DesktopFrame, CaptureError>) + Send> { |
| 236 | callback: T, |
| 237 | } |
| 238 | |
| 239 | impl<T> DesktopCallback<T> |
| 240 | where |
| 241 | T: FnMut(Result<DesktopFrame, CaptureError>) + Send, |
| 242 | { |
| 243 | fn new(callback: T) -> Self { |
| 244 | Self { callback } |
| 245 | } |
| 246 | |
| 247 | fn capture_result_from_sys( |
| 248 | result: Result<UniquePtr<sys_dc::ffi::DesktopFrame>, sys_dc::CaptureError>, |
| 249 | ) -> Result<DesktopFrame, CaptureError> { |
| 250 | match result { |
| 251 | Ok(frame) => Ok(DesktopFrame::new(frame)), |
| 252 | Err(error) => Err(match error { |
| 253 | sys_dc::CaptureError::Temporary => CaptureError::Temporary, |
| 254 | sys_dc::CaptureError::Permanent => CaptureError::Permanent, |
| 255 | sys_dc::CaptureError::StartFailed => CaptureError::StartFailed, |
| 256 | sys_dc::CaptureError::StoppedByUser => CaptureError::StoppedByUser, |
| 257 | sys_dc::CaptureError::SourceGone => CaptureError::SourceGone, |
| 258 | sys_dc::CaptureError::Interrupted => CaptureError::Interrupted, |
| 259 | }), |
| 260 | } |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | impl<T> sys_dc::DesktopCapturerCallback for DesktopCallback<T> |
| 265 | where |
| 266 | T: FnMut(Result<DesktopFrame, CaptureError>) + Send, |
| 267 | { |
| 268 | fn on_capture_result( |
| 269 | &mut self, |
| 270 | result: Result<UniquePtr<sys_dc::ffi::DesktopFrame>, sys_dc::CaptureError>, |
| 271 | ) { |
| 272 | (self.callback)(DesktopCallback::<T>::capture_result_from_sys(result)); |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | #[derive(Clone)] |
| 277 | pub(crate) struct CaptureSource { |
| 278 | sys_handle: sys_dc::ffi::Source, |
| 279 | } |
| 280 | |
| 281 | impl CaptureSource { |
| 282 | pub(crate) fn id(&self) -> u64 { |
| 283 | self.sys_handle.id |
| 284 | } |
| 285 | |
| 286 | pub(crate) fn title(&self) -> String { |
| 287 | self.sys_handle.title.clone() |
| 288 | } |
| 289 | |
| 290 | pub(crate) fn display_id(&self) -> i64 { |
| 291 | self.sys_handle.display_id |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | pub(crate) enum CaptureError { |
| 296 | Temporary, |
| 297 | Permanent, |
| 298 | StartFailed, |
| 299 | StoppedByUser, |
| 300 | SourceGone, |
| 301 | Interrupted, |
| 302 | } |