| 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 | #pragma once |
| 18 | #include <cstdint> |
| 19 | #include <memory> |
| 20 | |
| 21 | #include "modules/desktop_capture/desktop_capturer.h" |
| 22 | #include "rust/cxx.h" |
| 23 | #if defined(WEBRTC_MAC) && !defined(WEBRTC_IOS) |
| 24 | #include "livekit/sck_window_capturer.h" |
| 25 | #endif |
| 26 | |
| 27 | namespace livekit_ffi { |
| 28 | class DesktopFrame; |
| 29 | class DesktopCapturer; |
| 30 | class DesktopCapturerOptions; |
| 31 | class PortalPickerCallbackWrapper; |
| 32 | class Source; |
| 33 | } // namespace livekit_ffi |
| 34 | |
| 35 | #include "webrtc-sys/src/desktop_capturer.rs.h" |
| 36 | |
| 37 | namespace livekit_ffi { |
| 38 | |
| 39 | class DesktopCapturer : public webrtc::DesktopCapturer::Callback, |
| 40 | public webrtc::DelegatedSourceListController::Observer { |
| 41 | public: |
| 42 | #if defined(WEBRTC_MAC) && !defined(WEBRTC_IOS) |
| 43 | explicit DesktopCapturer(std::unique_ptr<webrtc::DesktopCapturer> capturer, |
| 44 | SckWindowCapturer* sck_window_capturer = nullptr) |
| 45 | : capturer(std::move(capturer)), |
| 46 | sck_window_capturer(sck_window_capturer), |
| 47 | callback(std::nullopt) {} |
| 48 | #else |
| 49 | explicit DesktopCapturer(std::unique_ptr<webrtc::DesktopCapturer> capturer) |
| 50 | : capturer(std::move(capturer)), callback(std::nullopt) {} |
| 51 | #endif |
| 52 | ~DesktopCapturer() override; |
| 53 | |
| 54 | void OnCaptureResult(webrtc::DesktopCapturer::Result result, |
| 55 | std::unique_ptr<webrtc::DesktopFrame> frame) final; |
| 56 | void OnSelection() override; |
| 57 | void OnCancelled() override; |
| 58 | void OnError() override; |
| 59 | |
| 60 | rust::Vec<Source> get_source_list() const; |
| 61 | bool select_source(uint64_t id) const { return capturer->SelectSource(id); } |
| 62 | void start(rust::Box<DesktopCapturerCallbackWrapper> callback); |
| 63 | void capture_frame() const { capturer->CaptureFrame(); } |
| 64 | void set_max_frame_rate(uint32_t max_fps) const { |
| 65 | capturer->SetMaxFrameRate(max_fps); |
| 66 | } |
| 67 | bool has_delegated_picker() const; |
| 68 | void observe_picker(rust::Box<PortalPickerCallbackWrapper> callback); |
| 69 | void ensure_picker_visible(); |
| 70 | void ensure_picker_hidden(); |
| 71 | |
| 72 | private: |
| 73 | webrtc::DelegatedSourceListController* picker_controller() const; |
| 74 | |
| 75 | std::unique_ptr<webrtc::DesktopCapturer> capturer; |
| 76 | #if defined(WEBRTC_MAC) && !defined(WEBRTC_IOS) |
| 77 | SckWindowCapturer* sck_window_capturer = nullptr; |
| 78 | #endif |
| 79 | std::optional<rust::Box<DesktopCapturerCallbackWrapper>> callback; |
| 80 | std::optional<rust::Box<PortalPickerCallbackWrapper>> picker; |
| 81 | }; |
| 82 | |
| 83 | class DesktopFrame { |
| 84 | public: |
| 85 | DesktopFrame(std::unique_ptr<webrtc::DesktopFrame> frame) : frame(std::move(frame)) {} |
| 86 | int32_t width() const { return frame->size().width(); } |
| 87 | |
| 88 | int32_t height() const { return frame->size().height(); } |
| 89 | |
| 90 | int32_t left() const { return frame->rect().left(); } |
| 91 | |
| 92 | int32_t top() const { return frame->rect().top(); } |
| 93 | |
| 94 | int32_t stride() const { return frame->stride(); } |
| 95 | |
| 96 | const uint8_t* data() const { return frame->data(); } |
| 97 | |
| 98 | private: |
| 99 | std::unique_ptr<webrtc::DesktopFrame> frame; |
| 100 | }; |
| 101 | |
| 102 | std::unique_ptr<DesktopCapturer> new_desktop_capturer(DesktopCapturerOptions options); |
| 103 | } // namespace livekit_ffi |