| 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/jsep.h" |
| 22 | #include "api/ref_counted_base.h" |
| 23 | #include "api/set_local_description_observer_interface.h" |
| 24 | #include "api/set_remote_description_observer_interface.h" |
| 25 | #include "api/stats/rtc_stats_collector_callback.h" |
| 26 | #include "livekit/rtc_error.h" |
| 27 | #include "rtc_base/ref_count.h" |
| 28 | #include "rust/cxx.h" |
| 29 | |
| 30 | namespace livekit_ffi { |
| 31 | class IceCandidate; |
| 32 | class SessionDescription; |
| 33 | }; // namespace livekit_ffi |
| 34 | #include "webrtc-sys/src/jsep.rs.h" |
| 35 | |
| 36 | namespace livekit_ffi { |
| 37 | |
| 38 | struct PeerContext; |
| 39 | |
| 40 | class IceCandidate { |
| 41 | public: |
| 42 | explicit IceCandidate( |
| 43 | std::unique_ptr<webrtc::IceCandidateInterface> ice_candidate); |
| 44 | |
| 45 | rust::String sdp_mid() const; |
| 46 | int sdp_mline_index() const; |
| 47 | rust::String candidate() const; // TODO(theomonnom) Return livekit_ffi::Candidate |
| 48 | // instead of rust::String |
| 49 | |
| 50 | rust::String stringify() const; |
| 51 | std::unique_ptr<webrtc::IceCandidateInterface> release(); |
| 52 | |
| 53 | private: |
| 54 | std::unique_ptr<webrtc::IceCandidateInterface> ice_candidate_; |
| 55 | }; |
| 56 | |
| 57 | std::shared_ptr<IceCandidate> create_ice_candidate(rust::String sdp_mid, |
| 58 | int sdp_mline_index, |
| 59 | rust::String sdp); |
| 60 | |
| 61 | static std::shared_ptr<IceCandidate> _shared_ice_candidate() { |
| 62 | return nullptr; // Ignore |
| 63 | } |
| 64 | |
| 65 | class SessionDescription { |
| 66 | public: |
| 67 | explicit SessionDescription( |
| 68 | std::unique_ptr<webrtc::SessionDescriptionInterface> session_description); |
| 69 | |
| 70 | SdpType sdp_type() const; |
| 71 | rust::String stringify() const; |
| 72 | std::unique_ptr<SessionDescription> clone() const; |
| 73 | std::unique_ptr<webrtc::SessionDescriptionInterface> release(); |
| 74 | |
| 75 | private: |
| 76 | std::unique_ptr<webrtc::SessionDescriptionInterface> session_description_; |
| 77 | }; |
| 78 | |
| 79 | std::unique_ptr<SessionDescription> create_session_description( |
| 80 | SdpType type, |
| 81 | rust::String sdp); |
| 82 | |
| 83 | static std::unique_ptr<SessionDescription> _unique_session_description() { |
| 84 | return nullptr; // Ignore |
| 85 | } |
| 86 | |
| 87 | #ifdef LIVEKIT_TEST |
| 88 | rust::String serialize_sdp_parse_error_for_test(); |
| 89 | #endif |
| 90 | |
| 91 | class NativeCreateSdpObserver |
| 92 | : public webrtc::CreateSessionDescriptionObserver { |
| 93 | public: |
| 94 | NativeCreateSdpObserver( |
| 95 | rust::Box<PeerContext> ctx, |
| 96 | rust::Fn<void(rust::Box<PeerContext> ctx, |
| 97 | std::unique_ptr<SessionDescription>)> on_success, |
| 98 | rust::Fn<void(rust::Box<PeerContext> ctx, RtcError)> on_error); |
| 99 | |
| 100 | void OnSuccess(webrtc::SessionDescriptionInterface* desc) override; |
| 101 | void OnFailure(webrtc::RTCError error) override; |
| 102 | |
| 103 | private: |
| 104 | rust::Box<PeerContext> ctx_; |
| 105 | rust::Fn<void(rust::Box<PeerContext>, std::unique_ptr<SessionDescription>)> |
| 106 | on_success_; |
| 107 | rust::Fn<void(rust::Box<PeerContext>, RtcError)> on_error_; |
| 108 | }; |
| 109 | |
| 110 | class NativeSetLocalSdpObserver |
| 111 | : public webrtc::SetLocalDescriptionObserverInterface { |
| 112 | public: |
| 113 | NativeSetLocalSdpObserver( |
| 114 | rust::Box<PeerContext> ctx, |
| 115 | rust::Fn<void(rust::Box<PeerContext>, RtcError)> on_complete); |
| 116 | |
| 117 | void OnSetLocalDescriptionComplete(webrtc::RTCError error) override; |
| 118 | |
| 119 | private: |
| 120 | rust::Box<PeerContext> ctx_; |
| 121 | rust::Fn<void(rust::Box<PeerContext>, RtcError)> on_complete_; |
| 122 | }; |
| 123 | |
| 124 | class NativeSetRemoteSdpObserver |
| 125 | : public webrtc::SetRemoteDescriptionObserverInterface { |
| 126 | public: |
| 127 | NativeSetRemoteSdpObserver( |
| 128 | rust::Box<PeerContext> ctx, |
| 129 | rust::Fn<void(rust::Box<PeerContext>, RtcError)> on_complete); |
| 130 | |
| 131 | void OnSetRemoteDescriptionComplete(webrtc::RTCError error) override; |
| 132 | |
| 133 | private: |
| 134 | rust::Box<PeerContext> ctx_; |
| 135 | rust::Fn<void(rust::Box<PeerContext>, RtcError)> on_complete_; |
| 136 | }; |
| 137 | |
| 138 | template <class T> // Context type |
| 139 | class NativeRtcStatsCollector : public webrtc::RTCStatsCollectorCallback { |
| 140 | public: |
| 141 | NativeRtcStatsCollector(rust::Box<T> ctx, |
| 142 | rust::Fn<void(rust::Box<T>, rust::String)> on_stats) |
| 143 | : ctx_(std::move(ctx)), on_stats_(on_stats) {} |
| 144 | |
| 145 | void OnStatsDelivered( |
| 146 | const webrtc::scoped_refptr<const webrtc::RTCStatsReport>& report) override { |
| 147 | on_stats_(std::move(ctx_), report->ToJson()); |
| 148 | } |
| 149 | |
| 150 | private: |
| 151 | rust::Box<T> ctx_; |
| 152 | rust::Fn<void(rust::Box<T>, rust::String)> on_stats_; |
| 153 | }; |
| 154 | |
| 155 | } // namespace livekit_ffi |