| 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 | #include "livekit/jsep.h" |
| 18 | |
| 19 | #include <iomanip> |
| 20 | #include <memory> |
| 21 | |
| 22 | #include "livekit/rtc_error.h" |
| 23 | #include "rtc_base/ref_counted_object.h" |
| 24 | #include "rust/cxx.h" |
| 25 | |
| 26 | namespace livekit_ffi { |
| 27 | |
| 28 | std::string serialize_sdp_error(webrtc::SdpParseError error) { |
| 29 | std::stringstream ss; |
| 30 | ss << std::hex << std::setfill('0'); |
| 31 | ss << std::setw(8) << (uint32_t)error.line.length(); |
| 32 | ss << std::dec << std::setw(1) << error.line; |
| 33 | ss << std::dec << std::setw(1) << error.description; |
| 34 | return ss.str(); |
| 35 | } |
| 36 | |
| 37 | IceCandidate::IceCandidate( |
| 38 | std::unique_ptr<webrtc::IceCandidateInterface> ice_candidate) |
| 39 | : ice_candidate_(std::move(ice_candidate)) {} |
| 40 | |
| 41 | rust::String IceCandidate::sdp_mid() const { |
| 42 | return ice_candidate_->sdp_mid(); |
| 43 | } |
| 44 | |
| 45 | int IceCandidate::sdp_mline_index() const { |
| 46 | return ice_candidate_->sdp_mline_index(); |
| 47 | } |
| 48 | |
| 49 | rust::String IceCandidate::candidate() const { |
| 50 | return stringify(); |
| 51 | } |
| 52 | |
| 53 | rust::String IceCandidate::stringify() const { |
| 54 | std::string str; |
| 55 | ice_candidate_->ToString(&str); |
| 56 | return rust::String::lossy(str); |
| 57 | } |
| 58 | |
| 59 | std::unique_ptr<webrtc::IceCandidateInterface> IceCandidate::release() { |
| 60 | return std::move(ice_candidate_); |
| 61 | } |
| 62 | |
| 63 | std::shared_ptr<IceCandidate> create_ice_candidate(rust::String sdp_mid, |
| 64 | int sdp_mline_index, |
| 65 | rust::String sdp) { |
| 66 | webrtc::SdpParseError error; |
| 67 | auto ice_rtc = webrtc::CreateIceCandidate(sdp_mid.c_str(), sdp_mline_index, |
| 68 | sdp.c_str(), &error); |
| 69 | if (!ice_rtc) { |
| 70 | throw std::runtime_error(serialize_sdp_error(error)); |
| 71 | } |
| 72 | |
| 73 | return std::make_shared<IceCandidate>( |
| 74 | std::unique_ptr<webrtc::IceCandidateInterface>(ice_rtc)); |
| 75 | } |
| 76 | |
| 77 | SessionDescription::SessionDescription( |
| 78 | std::unique_ptr<webrtc::SessionDescriptionInterface> session_description) |
| 79 | : session_description_(std::move(session_description)) {} |
| 80 | |
| 81 | SdpType SessionDescription::sdp_type() const { |
| 82 | return static_cast<SdpType>(session_description_->GetType()); |
| 83 | } |
| 84 | |
| 85 | rust::String SessionDescription::stringify() const { |
| 86 | std::string str; |
| 87 | session_description_->ToString(&str); |
| 88 | return rust::String::lossy(str); |
| 89 | } |
| 90 | |
| 91 | std::unique_ptr<SessionDescription> SessionDescription::clone() const { |
| 92 | return std::make_unique<SessionDescription>(session_description_->Clone()); |
| 93 | } |
| 94 | |
| 95 | std::unique_ptr<webrtc::SessionDescriptionInterface> |
| 96 | SessionDescription::release() { |
| 97 | return std::move(session_description_); |
| 98 | } |
| 99 | |
| 100 | std::unique_ptr<SessionDescription> create_session_description( |
| 101 | SdpType type, |
| 102 | rust::String sdp) { |
| 103 | webrtc::SdpParseError error; |
| 104 | auto rtc_sdp = webrtc::CreateSessionDescription( |
| 105 | static_cast<webrtc::SdpType>(type), sdp.c_str(), &error); |
| 106 | if (!rtc_sdp) { |
| 107 | throw std::runtime_error(serialize_sdp_error(error)); |
| 108 | } |
| 109 | |
| 110 | return std::make_unique<SessionDescription>(std::move(rtc_sdp)); |
| 111 | } |
| 112 | |
| 113 | #ifdef LIVEKIT_TEST |
| 114 | rust::String serialize_sdp_parse_error_for_test() { |
| 115 | webrtc::SdpParseError error; |
| 116 | auto rtc_sdp = webrtc::CreateSessionDescription( |
| 117 | webrtc::SdpType::kOffer, |
| 118 | "v=0\n" |
| 119 | "o=- 6549709950142776241 2 IN IP4 127.0.0.1\n" |
| 120 | "s=-\n" |
| 121 | "t=0 0\n" |
| 122 | "======================== ERROR HERE\n" |
| 123 | "a=group:BUNDLE 0\n" |
| 124 | "a=extmap-allow-mixed\n" |
| 125 | "a=msid-semantic: WMS\n" |
| 126 | "m=application 9 UDP/DTLS/SCTP webrtc-datachannel\n" |
| 127 | "c=IN IP4 0.0.0.0\n" |
| 128 | "a=ice-ufrag:Tw7h\n" |
| 129 | "a=ice-pwd:6XOVUD6HpcB4c1M8EB8jXJE9\n" |
| 130 | "a=ice-options:trickle\n" |
| 131 | "a=fingerprint:sha-256 " |
| 132 | "4F:EC:23:59:5D:A5:E6:3E:3E:5D:8A:09:B6:FA:04:AA:19:99:49:67:BD:65:93:06:BB:EE:AC:D5:21:0F:57:D6\n" |
| 133 | "a=setup:actpass\n" |
| 134 | "a=mid:0\n" |
| 135 | "a=sctp-port:5000\n" |
| 136 | "a=max-message-size:262144\n", |
| 137 | &error); |
| 138 | if (rtc_sdp) { |
| 139 | return rust::String(""); |
| 140 | } |
| 141 | return rust::String::lossy(serialize_sdp_error(error)); |
| 142 | } |
| 143 | #endif |
| 144 | |
| 145 | NativeCreateSdpObserver::NativeCreateSdpObserver( |
| 146 | rust::Box<PeerContext> ctx, |
| 147 | rust::Fn<void(rust::Box<PeerContext>, std::unique_ptr<SessionDescription>)> |
| 148 | on_success, |
| 149 | rust::Fn<void(rust::Box<PeerContext>, RtcError)> on_error) |
| 150 | : ctx_(std::move(ctx)), on_success_(on_success), on_error_(on_error) {} |
| 151 | |
| 152 | void NativeCreateSdpObserver::OnSuccess( |
| 153 | webrtc::SessionDescriptionInterface* desc) { |
| 154 | // We have ownership of desc |
| 155 | on_success_(std::move(ctx_), |
| 156 | std::make_unique<SessionDescription>( |
| 157 | std::unique_ptr<webrtc::SessionDescriptionInterface>(desc))); |
| 158 | } |
| 159 | |
| 160 | void NativeCreateSdpObserver::OnFailure(webrtc::RTCError error) { |
| 161 | on_error_(std::move(ctx_), to_error(error)); |
| 162 | } |
| 163 | |
| 164 | NativeSetLocalSdpObserver::NativeSetLocalSdpObserver( |
| 165 | rust::Box<PeerContext> ctx, |
| 166 | rust::Fn<void(rust::Box<PeerContext>, RtcError)> on_complete) |
| 167 | : ctx_(std::move(ctx)), on_complete_(on_complete) {} |
| 168 | |
| 169 | void NativeSetLocalSdpObserver::OnSetLocalDescriptionComplete( |
| 170 | webrtc::RTCError error) { |
| 171 | on_complete_(std::move(ctx_), to_error(error)); |
| 172 | } |
| 173 | |
| 174 | NativeSetRemoteSdpObserver::NativeSetRemoteSdpObserver( |
| 175 | rust::Box<PeerContext> ctx, |
| 176 | rust::Fn<void(rust::Box<PeerContext>, RtcError)> on_complete) |
| 177 | : ctx_(std::move(ctx)), on_complete_(on_complete) {} |
| 178 | |
| 179 | void NativeSetRemoteSdpObserver::OnSetRemoteDescriptionComplete( |
| 180 | webrtc::RTCError error) { |
| 181 | on_complete_(std::move(ctx_), to_error(error)); |
| 182 | } |
| 183 | } // namespace livekit_ffi |