BlueGit

ict rusty-teams

Files

rusty-teams / vendor / webrtc-sys / src / desktop_capturer.rs

desktop_capturer.rs

desktop_capturer.rs 4.5 KiB Download Raw
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
15use cxx::UniquePtr;
16use ffi::CaptureResult;
17
18use crate::{desktop_capturer::ffi::DesktopFrame, impl_thread_safety};
19
20#[cxx::bridge(namespace = "livekit_ffi")]
21pub 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 }
48
49 unsafe extern "C++" {
50 include!("livekit/desktop_capturer.h");
51
52 type DesktopCapturer;
53 type DesktopFrame;
54
55 fn new_desktop_capturer(options: DesktopCapturerOptions) -> UniquePtr<DesktopCapturer>;
56 fn capture_frame(self: &DesktopCapturer);
57 fn get_source_list(self: &DesktopCapturer) -> Vec<Source>;
58 fn select_source(self: &DesktopCapturer, id: u64) -> bool;
59 fn start(self: Pin<&mut DesktopCapturer>, callback: Box<DesktopCapturerCallbackWrapper>);
60 fn has_delegated_picker(self: &DesktopCapturer) -> bool;
61 fn observe_picker(
62 self: Pin<&mut DesktopCapturer>,
63 callback: Box<PortalPickerCallbackWrapper>,
64 );
65 fn ensure_picker_visible(self: Pin<&mut DesktopCapturer>);
66 fn ensure_picker_hidden(self: Pin<&mut DesktopCapturer>);
67
68 fn width(self: &DesktopFrame) -> i32;
69 fn height(self: &DesktopFrame) -> i32;
70 fn stride(self: &DesktopFrame) -> i32;
71 fn left(self: &DesktopFrame) -> i32;
72 fn top(self: &DesktopFrame) -> i32;
73 fn data(self: &DesktopFrame) -> *const u8;
74 }
75
76 extern "Rust" {
77 type DesktopCapturerCallbackWrapper;
78
79 fn on_capture_result(
80 self: &mut DesktopCapturerCallbackWrapper,
81 result: CaptureResult,
82 frame: UniquePtr<DesktopFrame>,
83 );
84
85 type PortalPickerCallbackWrapper;
86
87 fn on_selection(self: &mut PortalPickerCallbackWrapper);
88 fn on_cancelled(self: &mut PortalPickerCallbackWrapper);
89 fn on_error(self: &mut PortalPickerCallbackWrapper);
90 }
91}
92
93impl_thread_safety!(ffi::DesktopCapturer, Send + Sync);
94
95#[derive(Debug, PartialEq)]
96pub enum CaptureError {
97 Temporary,
98 Permanent,
99}
100
101pub trait DesktopCapturerCallback: Send {
102 fn on_capture_result(&mut self, result: Result<UniquePtr<DesktopFrame>, CaptureError>);
103}
104
105pub struct DesktopCapturerCallbackWrapper {
106 callback: Box<dyn DesktopCapturerCallback>,
107}
108
109impl DesktopCapturerCallbackWrapper {
110 pub fn new(callback: Box<dyn DesktopCapturerCallback>) -> Self {
111 Self { callback }
112 }
113
114 fn on_capture_result(&mut self, result: CaptureResult, frame: UniquePtr<DesktopFrame>) {
115 match result {
116 CaptureResult::Success => self.callback.on_capture_result(Ok(frame)),
117 CaptureResult::ErrorTemporary => {
118 self.callback.on_capture_result(Err(CaptureError::Temporary))
119 }
120 CaptureResult::ErrorPermanent => {
121 self.callback.on_capture_result(Err(CaptureError::Permanent))
122 }
123 _ => self.callback.on_capture_result(Err(CaptureError::Permanent)),
124 }
125 }
126}
127
128#[derive(Debug, Clone, Copy, PartialEq, Eq)]
129pub enum PickerEvent {
130 Selected,
131 Cancelled,
132 Failed,
133}
134
135pub struct PortalPickerCallbackWrapper {
136 callback: Box<dyn FnMut(PickerEvent) + Send>,
137}
138
139impl PortalPickerCallbackWrapper {
140 pub fn new(callback: Box<dyn FnMut(PickerEvent) + Send>) -> Self {
141 Self { callback }
142 }
143
144 fn on_selection(&mut self) {
145 (self.callback)(PickerEvent::Selected);
146 }
147
148 fn on_cancelled(&mut self) {
149 (self.callback)(PickerEvent::Cancelled);
150 }
151
152 fn on_error(&mut self) {
153 (self.callback)(PickerEvent::Failed);
154 }
155}