| 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 <memory> |
| 19 | |
| 20 | #include "modules/desktop_capture/desktop_capturer.h" |
| 21 | #include "rust/cxx.h" |
| 22 | |
| 23 | namespace livekit_ffi { |
| 24 | class DesktopFrame; |
| 25 | class DesktopCapturer; |
| 26 | class DesktopCapturerOptions; |
| 27 | class Source; |
| 28 | } // namespace livekit_ffi |
| 29 | |
| 30 | #include "webrtc-sys/src/desktop_capturer.rs.h" |
| 31 | |
| 32 | namespace livekit_ffi { |
| 33 | |
| 34 | class DesktopCapturer : public webrtc::DesktopCapturer::Callback { |
| 35 | public: |
| 36 | explicit DesktopCapturer(std::unique_ptr<webrtc::DesktopCapturer> capturer) |
| 37 | : capturer(std::move(capturer)), callback(std::nullopt) {} |
| 38 | |
| 39 | void OnCaptureResult(webrtc::DesktopCapturer::Result result, |
| 40 | std::unique_ptr<webrtc::DesktopFrame> frame) final; |
| 41 | |
| 42 | rust::Vec<Source> get_source_list() const; |
| 43 | bool select_source(uint64_t id) const { return capturer->SelectSource(id); } |
| 44 | void start(rust::Box<DesktopCapturerCallbackWrapper> callback); |
| 45 | void capture_frame() const { capturer->CaptureFrame(); } |
| 46 | |
| 47 | private: |
| 48 | std::unique_ptr<webrtc::DesktopCapturer> capturer; |
| 49 | std::optional<rust::Box<DesktopCapturerCallbackWrapper>> callback; |
| 50 | }; |
| 51 | |
| 52 | class DesktopFrame { |
| 53 | public: |
| 54 | DesktopFrame(std::unique_ptr<webrtc::DesktopFrame> frame) : frame(std::move(frame)) {} |
| 55 | int32_t width() const { return frame->size().width(); } |
| 56 | |
| 57 | int32_t height() const { return frame->size().height(); } |
| 58 | |
| 59 | int32_t left() const { return frame->rect().left(); } |
| 60 | |
| 61 | int32_t top() const { return frame->rect().top(); } |
| 62 | |
| 63 | int32_t stride() const { return frame->stride(); } |
| 64 | |
| 65 | const uint8_t* data() const { return frame->data(); } |
| 66 | |
| 67 | private: |
| 68 | std::unique_ptr<webrtc::DesktopFrame> frame; |
| 69 | }; |
| 70 | |
| 71 | std::unique_ptr<DesktopCapturer> new_desktop_capturer(DesktopCapturerOptions options); |
| 72 | } // namespace livekit_ffi |