| 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 | |
| 19 | #include <memory> |
| 20 | |
| 21 | #include "api/media_stream_interface.h" |
| 22 | #include "api/rtp_receiver_interface.h" |
| 23 | #include "api/rtp_sender_interface.h" |
| 24 | #include "livekit/helper.h" |
| 25 | #include "rtc_base/logging.h" |
| 26 | #include "rtc_base/physical_socket_server.h" |
| 27 | #include "rtc_base/ssl_adapter.h" |
| 28 | #include "rtc_base/thread.h" |
| 29 | #include "rust/cxx.h" |
| 30 | |
| 31 | #ifdef WEBRTC_WIN |
| 32 | #include "rtc_base/win32_socket_init.h" |
| 33 | #endif |
| 34 | |
| 35 | namespace livekit_ffi { |
| 36 | class RtcRuntime; |
| 37 | class LogSink; |
| 38 | } // namespace livekit_ffi |
| 39 | #include "webrtc-sys/src/webrtc.rs.h" |
| 40 | |
| 41 | namespace livekit_ffi { |
| 42 | |
| 43 | class MediaStreamTrack; |
| 44 | class RtpReceiver; |
| 45 | class RtpSender; |
| 46 | |
| 47 | // Using a shared_ptr in RtcRuntime allows us to keep a strong reference to it |
| 48 | // on resources that depend on it. (e.g: AudioTrack, VideoTrack). |
| 49 | class RtcRuntime : public std::enable_shared_from_this<RtcRuntime> { |
| 50 | public: |
| 51 | [[nodiscard]] static std::shared_ptr<RtcRuntime> create() { |
| 52 | return std::shared_ptr<RtcRuntime>(new RtcRuntime()); |
| 53 | } |
| 54 | |
| 55 | RtcRuntime(const RtcRuntime&) = delete; |
| 56 | RtcRuntime& operator=(const RtcRuntime&) = delete; |
| 57 | ~RtcRuntime(); |
| 58 | |
| 59 | webrtc::Thread* network_thread() const; |
| 60 | webrtc::Thread* worker_thread() const; |
| 61 | webrtc::Thread* signaling_thread() const; |
| 62 | |
| 63 | std::shared_ptr<MediaStreamTrack> get_or_create_media_stream_track( |
| 64 | webrtc::scoped_refptr<webrtc::MediaStreamTrackInterface> track); |
| 65 | |
| 66 | std::shared_ptr<AudioTrack> get_or_create_audio_track( |
| 67 | webrtc::scoped_refptr<webrtc::AudioTrackInterface> track); |
| 68 | |
| 69 | std::shared_ptr<VideoTrack> get_or_create_video_track( |
| 70 | webrtc::scoped_refptr<webrtc::VideoTrackInterface> track); |
| 71 | |
| 72 | private: |
| 73 | RtcRuntime(); |
| 74 | |
| 75 | std::unique_ptr<webrtc::Thread> network_thread_; |
| 76 | std::unique_ptr<webrtc::Thread> worker_thread_; |
| 77 | std::unique_ptr<webrtc::Thread> signaling_thread_; |
| 78 | |
| 79 | // Lists used to make sure we don't create multiple wrappers for one |
| 80 | // underlying webrtc object. (e.g: webrtc::VideoTrackInterface should only |
| 81 | // have one livekit_ffi::VideoTrack associated with it). |
| 82 | // The only reason we to do that is to allow to add states inside our |
| 83 | // wrappers (e.g: the sinks_ member inside AudioTrack) |
| 84 | // DataChannel and the PeerConnectionFactory don't need to do this (There's no |
| 85 | // way to retrieve them after creation) |
| 86 | webrtc::Mutex mutex_; |
| 87 | std::vector<std::weak_ptr<MediaStreamTrack>> media_stream_tracks_; |
| 88 | // We don't have additonal state in RtpReceiver and RtpSender atm.. |
| 89 | // std::vector<std::weak_ptr<RtpReceiver>> rtp_receivers_; |
| 90 | // std::vector<std::weak_ptr<RtpSender>> rtp_senders_; |
| 91 | |
| 92 | #ifdef WEBRTC_WIN |
| 93 | // webrtc::WinsockInitializer winsock_; |
| 94 | // webrtc::PhysicalSocketServer ss_; |
| 95 | // webrtc::AutoSocketServerThread main_thread_{&ss_}; |
| 96 | #endif |
| 97 | }; |
| 98 | |
| 99 | class LogSink : public webrtc::LogSink { |
| 100 | public: |
| 101 | LogSink(rust::Fn<void(rust::String message, LoggingSeverity severity)> fnc); |
| 102 | ~LogSink(); |
| 103 | |
| 104 | void OnLogMessage(const std::string& message, |
| 105 | webrtc::LoggingSeverity severity) override; |
| 106 | |
| 107 | void OnLogMessage(const std::string& message) override {} |
| 108 | |
| 109 | private: |
| 110 | rust::Fn<void(rust::String message, LoggingSeverity severity)> fnc_; |
| 111 | }; |
| 112 | |
| 113 | std::unique_ptr<LogSink> new_log_sink( |
| 114 | rust::Fn<void(rust::String, LoggingSeverity)> fnc); |
| 115 | |
| 116 | rust::String create_random_uuid(); |
| 117 | |
| 118 | rust::Vec<VideoEncoderBackend> video_encoder_backend_list(); |
| 119 | |
| 120 | void tune_windows_camera(rust::Str target); |
| 121 | |
| 122 | } // namespace livekit_ffi |