BlueGit

ict rusty-teams

Files

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

desktop_capturer.cpp

desktop_capturer.cpp 3.8 KiB Download Raw
1/*
2 * Copyright 2025 LiveKit, Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "livekit/desktop_capturer.h"
18
19#include "modules/desktop_capture/desktop_capture_options.h"
20
21using SourceList = webrtc::DesktopCapturer::SourceList;
22
23namespace livekit_ffi {
24
25std::unique_ptr<DesktopCapturer> new_desktop_capturer(
26 DesktopCapturerOptions options) {
27 webrtc::DesktopCaptureOptions webrtc_options =
28 webrtc::DesktopCaptureOptions::CreateDefault();
29#if defined(WEBRTC_MAC) && !defined(WEBRTC_IOS)
30 webrtc_options.set_allow_sck_capturer(true);
31 webrtc_options.set_allow_sck_system_picker(options.allow_sck_system_picker);
32#endif /* defined(WEBRTC_MAC) && !defined(WEBRTC_IOS) */
33#ifdef _WIN64
34 switch (options.source_type) {
35 case SourceType::Screen:
36 webrtc_options.set_allow_wgc_screen_capturer(true);
37 break;
38 case SourceType::Window:
39 webrtc_options.set_allow_wgc_window_capturer(true);
40 // https://github.com/webrtc-sdk/webrtc/blob/m137_release/modules/desktop_capture/desktop_capture_options.h#L133-L142
41 webrtc_options.set_enumerate_current_process_windows(false);
42 break;
43 default:
44 break;
45 }
46 webrtc_options.set_allow_directx_capturer(true);
47#endif /* _WIN64 */
48#ifdef WEBRTC_USE_PIPEWIRE
49 webrtc_options.set_allow_pipewire(true);
50#endif /* WEBRTC_USE_PIPEWIRE */
51
52 // prefer_cursor_embedded indicate that the capturer should try to include the
53 // cursor in the frame
54 webrtc_options.set_prefer_cursor_embedded(options.include_cursor);
55
56 std::unique_ptr<webrtc::DesktopCapturer> capturer = nullptr;
57 switch (options.source_type) {
58 case SourceType::Window:
59 capturer = webrtc::DesktopCapturer::CreateWindowCapturer(webrtc_options);
60 break;
61 case SourceType::Screen:
62 capturer = webrtc::DesktopCapturer::CreateScreenCapturer(webrtc_options);
63 break;
64 case SourceType::Generic:
65 capturer = webrtc::DesktopCapturer::CreateGenericCapturer(webrtc_options);
66 break;
67 default:
68 return nullptr;
69 }
70
71 if (!capturer) {
72 return nullptr;
73 }
74 return std::make_unique<DesktopCapturer>(std::move(capturer));
75}
76
77void DesktopCapturer::start(
78 rust::Box<DesktopCapturerCallbackWrapper> callback) {
79 this->callback = std::move(callback);
80 capturer->Start(this);
81}
82
83void DesktopCapturer::OnCaptureResult(
84 webrtc::DesktopCapturer::Result result,
85 std::unique_ptr<webrtc::DesktopFrame> frame) {
86 CaptureResult ret_result = CaptureResult::ErrorPermanent;
87 switch (result) {
88 case webrtc::DesktopCapturer::Result::SUCCESS:
89 ret_result = CaptureResult::Success;
90 break;
91 case webrtc::DesktopCapturer::Result::ERROR_PERMANENT:
92 ret_result = CaptureResult::ErrorPermanent;
93 break;
94 case webrtc::DesktopCapturer::Result::ERROR_TEMPORARY:
95 ret_result = CaptureResult::ErrorTemporary;
96 break;
97 default:
98 break;
99 }
100 if (callback) {
101 (*callback)->on_capture_result(
102 ret_result, std::make_unique<DesktopFrame>(std::move(frame)));
103 }
104}
105
106rust::Vec<Source> DesktopCapturer::get_source_list() const {
107 SourceList list{};
108 bool res = capturer->GetSourceList(&list);
109 rust::Vec<Source> source_list{};
110 if (res) {
111 for (auto& source : list) {
112 source_list.push_back(Source{static_cast<uint64_t>(source.id),
113 source.title, source.display_id});
114 }
115 }
116 return source_list;
117}
118} // namespace livekit_ffi