| 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 | |
| 21 | using SourceList = webrtc::DesktopCapturer::SourceList; |
| 22 | |
| 23 | namespace livekit_ffi { |
| 24 | |
| 25 | std::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 | |
| 77 | DesktopCapturer::~DesktopCapturer() { |
| 78 | if (auto* controller = picker_controller()) { |
| 79 | controller->Observe(nullptr); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | webrtc::DelegatedSourceListController* DesktopCapturer::picker_controller() |
| 84 | const { |
| 85 | if (!capturer) { |
| 86 | return nullptr; |
| 87 | } |
| 88 | return capturer->GetDelegatedSourceListController(); |
| 89 | } |
| 90 | |
| 91 | bool DesktopCapturer::has_delegated_picker() const { |
| 92 | return picker_controller() != nullptr; |
| 93 | } |
| 94 | |
| 95 | void DesktopCapturer::observe_picker( |
| 96 | rust::Box<PortalPickerCallbackWrapper> callback) { |
| 97 | picker = std::move(callback); |
| 98 | if (auto* controller = picker_controller()) { |
| 99 | controller->Observe(this); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | void DesktopCapturer::ensure_picker_visible() { |
| 104 | if (auto* controller = picker_controller()) { |
| 105 | controller->EnsureVisible(); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | void DesktopCapturer::ensure_picker_hidden() { |
| 110 | if (auto* controller = picker_controller()) { |
| 111 | controller->EnsureHidden(); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | void DesktopCapturer::OnSelection() { |
| 116 | if (picker) { |
| 117 | (*picker)->on_selection(); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | void DesktopCapturer::OnCancelled() { |
| 122 | if (picker) { |
| 123 | (*picker)->on_cancelled(); |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | void DesktopCapturer::OnError() { |
| 128 | if (picker) { |
| 129 | (*picker)->on_error(); |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | void DesktopCapturer::start( |
| 134 | rust::Box<DesktopCapturerCallbackWrapper> callback) { |
| 135 | this->callback = std::move(callback); |
| 136 | capturer->Start(this); |
| 137 | } |
| 138 | |
| 139 | void DesktopCapturer::OnCaptureResult( |
| 140 | webrtc::DesktopCapturer::Result result, |
| 141 | std::unique_ptr<webrtc::DesktopFrame> frame) { |
| 142 | CaptureResult ret_result = CaptureResult::ErrorPermanent; |
| 143 | switch (result) { |
| 144 | case webrtc::DesktopCapturer::Result::SUCCESS: |
| 145 | ret_result = CaptureResult::Success; |
| 146 | break; |
| 147 | case webrtc::DesktopCapturer::Result::ERROR_PERMANENT: |
| 148 | ret_result = CaptureResult::ErrorPermanent; |
| 149 | break; |
| 150 | case webrtc::DesktopCapturer::Result::ERROR_TEMPORARY: |
| 151 | ret_result = CaptureResult::ErrorTemporary; |
| 152 | break; |
| 153 | default: |
| 154 | break; |
| 155 | } |
| 156 | if (callback) { |
| 157 | (*callback)->on_capture_result( |
| 158 | ret_result, std::make_unique<DesktopFrame>(std::move(frame))); |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | rust::Vec<Source> DesktopCapturer::get_source_list() const { |
| 163 | SourceList list{}; |
| 164 | bool res = capturer->GetSourceList(&list); |
| 165 | rust::Vec<Source> source_list{}; |
| 166 | if (res) { |
| 167 | for (auto& source : list) { |
| 168 | source_list.push_back(Source{static_cast<uint64_t>(source.id), |
| 169 | source.title, source.display_id}); |
| 170 | } |
| 171 | } |
| 172 | return source_list; |
| 173 | } |
| 174 | } // namespace livekit_ffi |