| 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/peer_connection_interface.h" |
| 22 | #include "api/scoped_refptr.h" |
| 23 | #include "livekit/data_channel.h" |
| 24 | #include "livekit/helper.h" |
| 25 | #include "livekit/jsep.h" |
| 26 | #include "livekit/media_stream.h" |
| 27 | #include "livekit/rtc_error.h" |
| 28 | #include "livekit/rtp_receiver.h" |
| 29 | #include "livekit/rtp_sender.h" |
| 30 | #include "livekit/rtp_transceiver.h" |
| 31 | #include "livekit/webrtc.h" |
| 32 | #include "rust/cxx.h" |
| 33 | #include "webrtc-sys/src/data_channel.rs.h" |
| 34 | |
| 35 | namespace livekit_ffi { |
| 36 | class PeerConnection; |
| 37 | } // namespace livekit_ffi |
| 38 | #include "webrtc-sys/src/peer_connection.rs.h" |
| 39 | |
| 40 | namespace livekit_ffi { |
| 41 | |
| 42 | webrtc::PeerConnectionInterface::RTCConfiguration to_native_rtc_configuration( |
| 43 | RtcConfiguration config); |
| 44 | |
| 45 | class PeerConnectionObserverWrapper; |
| 46 | |
| 47 | class PeerConnection : webrtc::PeerConnectionObserver { |
| 48 | public: |
| 49 | PeerConnection( |
| 50 | std::shared_ptr<RtcRuntime> rtc_runtime, |
| 51 | webrtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> pc_factory, |
| 52 | rust::Box<PeerConnectionObserverWrapper> observer); |
| 53 | |
| 54 | ~PeerConnection(); |
| 55 | |
| 56 | bool Initialize(webrtc::PeerConnectionInterface::RTCConfiguration config); |
| 57 | |
| 58 | void set_configuration(RtcConfiguration config) const; |
| 59 | |
| 60 | void create_offer( |
| 61 | RtcOfferAnswerOptions options, |
| 62 | rust::Box<PeerContext> ctx, |
| 63 | rust::Fn<void(rust::Box<PeerContext>, |
| 64 | std::unique_ptr<SessionDescription>)> on_success, |
| 65 | rust::Fn<void(rust::Box<PeerContext>, RtcError)> on_error) const; |
| 66 | |
| 67 | void create_answer( |
| 68 | RtcOfferAnswerOptions options, |
| 69 | rust::Box<PeerContext> ctx, |
| 70 | rust::Fn<void(rust::Box<PeerContext>, |
| 71 | std::unique_ptr<SessionDescription>)> on_success, |
| 72 | rust::Fn<void(rust::Box<PeerContext>, RtcError)> on_error) const; |
| 73 | |
| 74 | void set_local_description( |
| 75 | std::unique_ptr<SessionDescription> desc, |
| 76 | rust::Box<PeerContext> ctx, |
| 77 | rust::Fn<void(rust::Box<PeerContext>, RtcError)> on_complete) const; |
| 78 | |
| 79 | void set_remote_description( |
| 80 | std::unique_ptr<SessionDescription> desc, |
| 81 | rust::Box<PeerContext> ctx, |
| 82 | rust::Fn<void(rust::Box<PeerContext>, RtcError)> on_complete) const; |
| 83 | |
| 84 | std::shared_ptr<DataChannel> create_data_channel(rust::String label, |
| 85 | DataChannelInit init) const; |
| 86 | |
| 87 | void add_ice_candidate( |
| 88 | std::shared_ptr<IceCandidate> candidate, |
| 89 | rust::Box<PeerContext> ctx, |
| 90 | rust::Fn<void(rust::Box<PeerContext>, RtcError)> on_complete) const; |
| 91 | |
| 92 | std::shared_ptr<RtpSender> add_track( |
| 93 | std::shared_ptr<MediaStreamTrack> track, |
| 94 | const rust::Vec<rust::String>& stream_ids) const; |
| 95 | |
| 96 | void remove_track(std::shared_ptr<RtpSender> sender) const; |
| 97 | |
| 98 | void get_stats( |
| 99 | rust::Box<PeerContext> ctx, |
| 100 | rust::Fn<void(rust::Box<PeerContext>, rust::String)> on_stats) const; |
| 101 | |
| 102 | void restart_ice() const; |
| 103 | |
| 104 | std::shared_ptr<RtpTransceiver> add_transceiver( |
| 105 | std::shared_ptr<MediaStreamTrack> track, |
| 106 | RtpTransceiverInit init) const; |
| 107 | |
| 108 | std::shared_ptr<RtpTransceiver> add_transceiver_for_media( |
| 109 | MediaType media_type, |
| 110 | RtpTransceiverInit init) const; |
| 111 | |
| 112 | rust::Vec<RtpSenderPtr> get_senders() const; |
| 113 | |
| 114 | rust::Vec<RtpReceiverPtr> get_receivers() const; |
| 115 | |
| 116 | rust::Vec<RtpTransceiverPtr> get_transceivers() const; |
| 117 | |
| 118 | std::unique_ptr<SessionDescription> current_local_description() const; |
| 119 | |
| 120 | std::unique_ptr<SessionDescription> current_remote_description() const; |
| 121 | |
| 122 | std::unique_ptr<SessionDescription> pending_local_description() const; |
| 123 | |
| 124 | std::unique_ptr<SessionDescription> pending_remote_description() const; |
| 125 | |
| 126 | std::unique_ptr<SessionDescription> local_description() const; |
| 127 | |
| 128 | std::unique_ptr<SessionDescription> remote_description() const; |
| 129 | |
| 130 | PeerConnectionState connection_state() const; |
| 131 | |
| 132 | SignalingState signaling_state() const; |
| 133 | |
| 134 | IceGatheringState ice_gathering_state() const; |
| 135 | |
| 136 | IceConnectionState ice_connection_state() const; |
| 137 | |
| 138 | void close() const; |
| 139 | |
| 140 | void OnSignalingChange( |
| 141 | webrtc::PeerConnectionInterface::SignalingState new_state) override; |
| 142 | |
| 143 | void OnAddStream( |
| 144 | webrtc::scoped_refptr<webrtc::MediaStreamInterface> stream) override; |
| 145 | |
| 146 | void OnRemoveStream( |
| 147 | webrtc::scoped_refptr<webrtc::MediaStreamInterface> stream) override; |
| 148 | |
| 149 | void OnDataChannel( |
| 150 | webrtc::scoped_refptr<webrtc::DataChannelInterface> data_channel) override; |
| 151 | |
| 152 | void OnRenegotiationNeeded() override; |
| 153 | |
| 154 | void OnNegotiationNeededEvent(uint32_t event_id) override; |
| 155 | |
| 156 | void OnIceConnectionChange( |
| 157 | webrtc::PeerConnectionInterface::IceConnectionState new_state) override; |
| 158 | |
| 159 | void OnStandardizedIceConnectionChange( |
| 160 | webrtc::PeerConnectionInterface::IceConnectionState new_state) override; |
| 161 | |
| 162 | void OnConnectionChange( |
| 163 | webrtc::PeerConnectionInterface::PeerConnectionState new_state) override; |
| 164 | |
| 165 | void OnIceGatheringChange( |
| 166 | webrtc::PeerConnectionInterface::IceGatheringState new_state) override; |
| 167 | |
| 168 | void OnIceCandidate(const webrtc::IceCandidate* candidate) override; |
| 169 | |
| 170 | void OnIceCandidateError(const std::string& address, |
| 171 | int port, |
| 172 | const std::string& url, |
| 173 | int error_code, |
| 174 | const std::string& error_text) override; |
| 175 | |
| 176 | void OnIceCandidateRemoved(const webrtc::IceCandidate* candidate) override; |
| 177 | |
| 178 | void OnIceConnectionReceivingChange(bool receiving) override; |
| 179 | |
| 180 | void OnIceSelectedCandidatePairChanged( |
| 181 | const webrtc::CandidatePairChangeEvent& event) override; |
| 182 | |
| 183 | void OnAddTrack( |
| 184 | webrtc::scoped_refptr<webrtc::RtpReceiverInterface> receiver, |
| 185 | const std::vector<webrtc::scoped_refptr<webrtc::MediaStreamInterface>>& |
| 186 | streams) override; |
| 187 | |
| 188 | void OnTrack( |
| 189 | webrtc::scoped_refptr<webrtc::RtpTransceiverInterface> transceiver) override; |
| 190 | |
| 191 | void OnRemoveTrack( |
| 192 | webrtc::scoped_refptr<webrtc::RtpReceiverInterface> receiver) override; |
| 193 | |
| 194 | void OnInterestingUsage(int usage_pattern) override; |
| 195 | |
| 196 | private: |
| 197 | std::shared_ptr<RtcRuntime> rtc_runtime_; |
| 198 | webrtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> pc_factory_; |
| 199 | rust::Box<PeerConnectionObserverWrapper> observer_; |
| 200 | webrtc::scoped_refptr<webrtc::PeerConnectionInterface> peer_connection_; |
| 201 | }; |
| 202 | |
| 203 | static std::shared_ptr<PeerConnection> _shared_peer_connection() { |
| 204 | return nullptr; // Ignore |
| 205 | } |
| 206 | |
| 207 | } // namespace livekit_ffi |