| 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 ffi::CaptureResult; |
| 17 | |
| 18 | use crate::{desktop_capturer::ffi::DesktopFrame, impl_thread_safety}; |
| 19 | |
| 20 | #[cxx::bridge(namespace = "livekit_ffi")] |
| 21 | pub mod ffi { |
| 22 | #[derive(Clone)] |
| 23 | struct Source { |
| 24 | id: u64, |
| 25 | title: String, |
| 26 | display_id: i64, |
| 27 | } |
| 28 | |
| 29 | #[derive(Debug, PartialEq)] |
| 30 | enum SourceType { |
| 31 | Screen, |
| 32 | Window, |
| 33 | Generic, |
| 34 | } |
| 35 | |
| 36 | #[derive(Clone, Debug)] |
| 37 | struct DesktopCapturerOptions { |
| 38 | source_type: SourceType, |
| 39 | include_cursor: bool, |
| 40 | allow_sck_system_picker: bool, |
| 41 | } |
| 42 | |
| 43 | enum CaptureResult { |
| 44 | Success, |
| 45 | ErrorTemporary, |
| 46 | ErrorPermanent, |
| 47 | StartFailed, |
| 48 | StoppedByUser, |
| 49 | SourceGone, |
| 50 | Interrupted, |
| 51 | } |
| 52 | |
| 53 | unsafe extern "C++" { |
| 54 | include!("livekit/desktop_capturer.h"); |
| 55 | |
| 56 | type DesktopCapturer; |
| 57 | type DesktopFrame; |
| 58 | |
| 59 | fn new_desktop_capturer(options: DesktopCapturerOptions) -> UniquePtr<DesktopCapturer>; |
| 60 | fn capture_frame(self: &DesktopCapturer); |
| 61 | fn set_max_frame_rate(self: &DesktopCapturer, max_fps: u32); |
| 62 | fn get_source_list(self: &DesktopCapturer) -> Vec<Source>; |
| 63 | fn select_source(self: &DesktopCapturer, id: u64) -> bool; |
| 64 | fn start(self: Pin<&mut DesktopCapturer>, callback: Box<DesktopCapturerCallbackWrapper>); |
| 65 | fn has_delegated_picker(self: &DesktopCapturer) -> bool; |
| 66 | fn observe_picker( |
| 67 | self: Pin<&mut DesktopCapturer>, |
| 68 | callback: Box<PortalPickerCallbackWrapper>, |
| 69 | ); |
| 70 | fn ensure_picker_visible(self: Pin<&mut DesktopCapturer>); |
| 71 | fn ensure_picker_hidden(self: Pin<&mut DesktopCapturer>); |
| 72 | |
| 73 | fn width(self: &DesktopFrame) -> i32; |
| 74 | fn height(self: &DesktopFrame) -> i32; |
| 75 | fn stride(self: &DesktopFrame) -> i32; |
| 76 | fn left(self: &DesktopFrame) -> i32; |
| 77 | fn top(self: &DesktopFrame) -> i32; |
| 78 | fn data(self: &DesktopFrame) -> *const u8; |
| 79 | } |
| 80 | |
| 81 | extern "Rust" { |
| 82 | type DesktopCapturerCallbackWrapper; |
| 83 | |
| 84 | fn on_capture_result( |
| 85 | self: &mut DesktopCapturerCallbackWrapper, |
| 86 | result: CaptureResult, |
| 87 | frame: UniquePtr<DesktopFrame>, |
| 88 | ); |
| 89 | |
| 90 | type PortalPickerCallbackWrapper; |
| 91 | |
| 92 | fn on_selection(self: &mut PortalPickerCallbackWrapper); |
| 93 | fn on_cancelled(self: &mut PortalPickerCallbackWrapper); |
| 94 | fn on_error(self: &mut PortalPickerCallbackWrapper); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | impl_thread_safety!(ffi::DesktopCapturer, Send + Sync); |
| 99 | |
| 100 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 101 | pub enum CaptureError { |
| 102 | Temporary, |
| 103 | Permanent, |
| 104 | StartFailed, |
| 105 | StoppedByUser, |
| 106 | SourceGone, |
| 107 | Interrupted, |
| 108 | } |
| 109 | |
| 110 | pub trait DesktopCapturerCallback: Send { |
| 111 | fn on_capture_result(&mut self, result: Result<UniquePtr<DesktopFrame>, CaptureError>); |
| 112 | } |
| 113 | |
| 114 | pub struct DesktopCapturerCallbackWrapper { |
| 115 | callback: Box<dyn DesktopCapturerCallback>, |
| 116 | } |
| 117 | |
| 118 | impl DesktopCapturerCallbackWrapper { |
| 119 | pub fn new(callback: Box<dyn DesktopCapturerCallback>) -> Self { |
| 120 | Self { callback } |
| 121 | } |
| 122 | |
| 123 | fn on_capture_result(&mut self, result: CaptureResult, frame: UniquePtr<DesktopFrame>) { |
| 124 | match result { |
| 125 | CaptureResult::Success => self.callback.on_capture_result(Ok(frame)), |
| 126 | CaptureResult::ErrorTemporary => { |
| 127 | self.callback.on_capture_result(Err(CaptureError::Temporary)) |
| 128 | } |
| 129 | CaptureResult::ErrorPermanent => { |
| 130 | self.callback.on_capture_result(Err(CaptureError::Permanent)) |
| 131 | } |
| 132 | CaptureResult::StartFailed => { |
| 133 | self.callback.on_capture_result(Err(CaptureError::StartFailed)) |
| 134 | } |
| 135 | CaptureResult::StoppedByUser => { |
| 136 | self.callback.on_capture_result(Err(CaptureError::StoppedByUser)) |
| 137 | } |
| 138 | CaptureResult::SourceGone => { |
| 139 | self.callback.on_capture_result(Err(CaptureError::SourceGone)) |
| 140 | } |
| 141 | CaptureResult::Interrupted => { |
| 142 | self.callback.on_capture_result(Err(CaptureError::Interrupted)) |
| 143 | } |
| 144 | _ => self.callback.on_capture_result(Err(CaptureError::Permanent)), |
| 145 | } |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 150 | pub enum PickerEvent { |
| 151 | Selected, |
| 152 | Cancelled, |
| 153 | Failed, |
| 154 | } |
| 155 | |
| 156 | pub struct PortalPickerCallbackWrapper { |
| 157 | callback: Box<dyn FnMut(PickerEvent) + Send>, |
| 158 | } |
| 159 | |
| 160 | impl PortalPickerCallbackWrapper { |
| 161 | pub fn new(callback: Box<dyn FnMut(PickerEvent) + Send>) -> Self { |
| 162 | Self { callback } |
| 163 | } |
| 164 | |
| 165 | fn on_selection(&mut self) { |
| 166 | (self.callback)(PickerEvent::Selected); |
| 167 | } |
| 168 | |
| 169 | fn on_cancelled(&mut self) { |
| 170 | (self.callback)(PickerEvent::Cancelled); |
| 171 | } |
| 172 | |
| 173 | fn on_error(&mut self) { |
| 174 | (self.callback)(PickerEvent::Failed); |
| 175 | } |
| 176 | } |