/* * Copyright 2025 LiveKit, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #pragma once #include #include #include "modules/desktop_capture/desktop_capturer.h" #include "rust/cxx.h" #if defined(WEBRTC_MAC) && !defined(WEBRTC_IOS) #include "livekit/sck_window_capturer.h" #endif namespace livekit_ffi { class DesktopFrame; class DesktopCapturer; class DesktopCapturerOptions; class PortalPickerCallbackWrapper; class Source; } // namespace livekit_ffi #include "webrtc-sys/src/desktop_capturer.rs.h" namespace livekit_ffi { class DesktopCapturer : public webrtc::DesktopCapturer::Callback, public webrtc::DelegatedSourceListController::Observer { public: #if defined(WEBRTC_MAC) && !defined(WEBRTC_IOS) explicit DesktopCapturer(std::unique_ptr capturer, SckWindowCapturer* sck_window_capturer = nullptr) : capturer(std::move(capturer)), sck_window_capturer(sck_window_capturer), callback(std::nullopt) {} #else explicit DesktopCapturer(std::unique_ptr capturer) : capturer(std::move(capturer)), callback(std::nullopt) {} #endif ~DesktopCapturer() override; void OnCaptureResult(webrtc::DesktopCapturer::Result result, std::unique_ptr frame) final; void OnSelection() override; void OnCancelled() override; void OnError() override; rust::Vec get_source_list() const; bool select_source(uint64_t id) const { return capturer->SelectSource(id); } void start(rust::Box callback); void capture_frame() const { capturer->CaptureFrame(); } void set_max_frame_rate(uint32_t max_fps) const { capturer->SetMaxFrameRate(max_fps); } bool has_delegated_picker() const; void observe_picker(rust::Box callback); void ensure_picker_visible(); void ensure_picker_hidden(); private: webrtc::DelegatedSourceListController* picker_controller() const; std::unique_ptr capturer; #if defined(WEBRTC_MAC) && !defined(WEBRTC_IOS) SckWindowCapturer* sck_window_capturer = nullptr; #endif std::optional> callback; std::optional> picker; }; class DesktopFrame { public: DesktopFrame(std::unique_ptr frame) : frame(std::move(frame)) {} int32_t width() const { return frame->size().width(); } int32_t height() const { return frame->size().height(); } int32_t left() const { return frame->rect().left(); } int32_t top() const { return frame->rect().top(); } int32_t stride() const { return frame->stride(); } const uint8_t* data() const { return frame->data(); } private: std::unique_ptr frame; }; std::unique_ptr new_desktop_capturer(DesktopCapturerOptions options); } // namespace livekit_ffi