BlueGit

ict rusty-teams

Files

rusty-teams / vendor / libwebrtc / src / native / desktop_capturer.rs

desktop_capturer.rs

desktop_capturer.rs 7.1 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 webrtc_sys::desktop_capturer::{self as sys_dc, ffi::new_desktop_capturer};
17
18#[derive(Debug, Copy, Clone, PartialEq)]
19pub(crate) enum SourceType {
20 Screen,
21 Window,
22 #[cfg(any(target_os = "macos", target_os = "linux"))]
23 Generic,
24}
25
26#[derive(Copy, Clone, Debug)]
27pub(crate) struct DesktopCapturerOptions {
28 source_type: SourceType,
29 include_cursor: bool,
30 #[cfg(target_os = "macos")]
31 allow_sck_system_picker: bool,
32}
33
34impl Default for DesktopCapturerOptions {
35 fn default() -> Self {
36 Self {
37 source_type: SourceType::Screen,
38 include_cursor: false,
39 #[cfg(target_os = "macos")]
40 allow_sck_system_picker: true,
41 }
42 }
43}
44
45impl DesktopCapturerOptions {
46 pub(crate) fn new(source_type: SourceType) -> Self {
47 Self { source_type, ..Default::default() }
48 }
49
50 pub(crate) fn with_cursor(mut self, include: bool) -> Self {
51 self.include_cursor = include;
52 self
53 }
54
55 #[cfg(target_os = "macos")]
56 pub(crate) fn with_sck_system_picker(mut self, allow_sck_system_picker: bool) -> Self {
57 self.allow_sck_system_picker = allow_sck_system_picker;
58 self
59 }
60
61 pub(crate) fn to_sys_handle(&self) -> sys_dc::ffi::DesktopCapturerOptions {
62 let source_type = match self.source_type {
63 SourceType::Screen => sys_dc::ffi::SourceType::Screen,
64 SourceType::Window => sys_dc::ffi::SourceType::Window,
65 #[cfg(any(target_os = "macos", target_os = "linux"))]
66 SourceType::Generic => sys_dc::ffi::SourceType::Generic,
67 };
68 let sys_handle = sys_dc::ffi::DesktopCapturerOptions {
69 source_type,
70 include_cursor: self.include_cursor,
71 allow_sck_system_picker: {
72 #[cfg(target_os = "macos")]
73 {
74 self.allow_sck_system_picker
75 }
76 #[cfg(not(target_os = "macos"))]
77 {
78 false
79 }
80 },
81 };
82
83 sys_handle
84 }
85}
86
87pub(crate) struct DesktopCapturer {
88 sys_handle: UniquePtr<sys_dc::ffi::DesktopCapturer>,
89 #[cfg(all(any(target_os = "linux", target_os = "freebsd"), feature = "glib-main-loop"))]
90 glib_loop: Option<glib::MainLoop>,
91}
92
93impl DesktopCapturer {
94 pub(crate) fn new(options: DesktopCapturerOptions) -> Option<Self> {
95 let sys_handle = new_desktop_capturer(options.to_sys_handle());
96 if sys_handle.is_null() {
97 None
98 } else {
99 Some(Self {
100 sys_handle,
101 #[cfg(all(
102 any(target_os = "linux", target_os = "freebsd"),
103 feature = "glib-main-loop"
104 ))]
105 glib_loop: None,
106 })
107 }
108 }
109
110 pub(crate) fn capture_frame(&self) {
111 self.sys_handle.capture_frame();
112 }
113
114 pub(crate) fn start<T>(&mut self, callback: T)
115 where
116 T: FnMut(Result<DesktopFrame, CaptureError>) + Send + 'static,
117 {
118 #[cfg(all(any(target_os = "linux", target_os = "freebsd"), feature = "glib-main-loop"))]
119 if std::env::var("WAYLAND_DISPLAY").is_ok() {
120 let main_loop = glib::MainLoop::new(None, false);
121 self.glib_loop = Some(main_loop.clone());
122 let _handle = std::thread::spawn(move || {
123 main_loop.run();
124 });
125 }
126 let pin_handle = self.sys_handle.pin_mut();
127 let callback = DesktopCallback::new(callback);
128 let callback_wrapper = sys_dc::DesktopCapturerCallbackWrapper::new(Box::new(callback));
129 pin_handle.start(Box::new(callback_wrapper));
130 }
131
132 pub(crate) fn select_source(&self, id: u64) -> bool {
133 self.sys_handle.select_source(id)
134 }
135
136 pub(crate) fn get_source_list(&self) -> Vec<CaptureSource> {
137 let mut sources = Vec::new();
138 let source_list = self.sys_handle.get_source_list();
139 for source in source_list.iter() {
140 sources.push(CaptureSource { sys_handle: source.clone() });
141 }
142 sources
143 }
144}
145
146#[cfg(all(any(target_os = "linux", target_os = "freebsd"), feature = "glib-main-loop"))]
147impl Drop for DesktopCapturer {
148 fn drop(&mut self) {
149 if let Some(glib_loop) = &self.glib_loop {
150 glib_loop.quit();
151 }
152 }
153}
154
155pub(crate) struct DesktopFrame {
156 sys_handle: UniquePtr<sys_dc::ffi::DesktopFrame>,
157}
158
159impl DesktopFrame {
160 fn new(sys_handle: UniquePtr<sys_dc::ffi::DesktopFrame>) -> Self {
161 Self { sys_handle }
162 }
163
164 pub(crate) fn width(&self) -> i32 {
165 self.sys_handle.width()
166 }
167
168 pub(crate) fn height(&self) -> i32 {
169 self.sys_handle.height()
170 }
171
172 pub(crate) fn stride(&self) -> u32 {
173 self.sys_handle.stride() as u32
174 }
175
176 pub(crate) fn left(&self) -> i32 {
177 self.sys_handle.left()
178 }
179
180 pub(crate) fn top(&self) -> i32 {
181 self.sys_handle.top()
182 }
183
184 pub(crate) fn data(&self) -> &[u8] {
185 let data = self.sys_handle.data();
186 unsafe { std::slice::from_raw_parts(data, self.stride() as usize * self.height() as usize) }
187 }
188}
189
190struct DesktopCallback<T: FnMut(Result<DesktopFrame, CaptureError>) + Send> {
191 callback: T,
192}
193
194impl<T> DesktopCallback<T>
195where
196 T: FnMut(Result<DesktopFrame, CaptureError>) + Send,
197{
198 fn new(callback: T) -> Self {
199 Self { callback }
200 }
201
202 fn capture_result_from_sys(
203 result: Result<UniquePtr<sys_dc::ffi::DesktopFrame>, sys_dc::CaptureError>,
204 ) -> Result<DesktopFrame, CaptureError> {
205 match result {
206 Ok(frame) => Ok(DesktopFrame::new(frame)),
207 Err(error) => Err(match error {
208 sys_dc::CaptureError::Temporary => CaptureError::Temporary,
209 sys_dc::CaptureError::Permanent => CaptureError::Permanent,
210 }),
211 }
212 }
213}
214
215impl<T> sys_dc::DesktopCapturerCallback for DesktopCallback<T>
216where
217 T: FnMut(Result<DesktopFrame, CaptureError>) + Send,
218{
219 fn on_capture_result(
220 &mut self,
221 result: Result<UniquePtr<sys_dc::ffi::DesktopFrame>, sys_dc::CaptureError>,
222 ) {
223 (self.callback)(DesktopCallback::<T>::capture_result_from_sys(result));
224 }
225}
226
227#[derive(Clone)]
228pub(crate) struct CaptureSource {
229 sys_handle: sys_dc::ffi::Source,
230}
231
232impl CaptureSource {
233 pub(crate) fn id(&self) -> u64 {
234 self.sys_handle.id
235 }
236
237 pub(crate) fn title(&self) -> String {
238 self.sys_handle.title.clone()
239 }
240
241 pub(crate) fn display_id(&self) -> i64 {
242 self.sys_handle.display_id
243 }
244}
245
246pub(crate) enum CaptureError {
247 Temporary,
248 Permanent,
249}