fork/rust-sdks
git clone https://git.julia.blue/fork/rust-sdks
ee5076b
livekit/livekit-ffi@0.13.0
1/* 2* Copyright 2023 LiveKit 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 26namespace livekit { 27 28std::string serialize_sdp_error (webrtc::SdpParseError error ) { 29 std::stringstream ss ; 30ss << std::hex << std::setfill ('0' ); 31ss << std::setw (8 ) << (uint32_t )error .line .length (); 32ss << std::dec << std::setw (1 ) <<error .line ; 33ss << std::dec << std::setw (1 ) <<error .description ; 34return ss .str (); 35} 36 37IceCandidate ::IceCandidate ( 38 std::unique_ptr < webrtc::IceCandidateInterface > ice_candidate ) 39 :ice_candidate_ (std::move (ice_candidate )) {} 40 41rust::String IceCandidate ::sdp_mid ()const { 42return ice_candidate_ -> sdp_mid (); 43} 44 45int IceCandidate ::sdp_mline_index ()const { 46return ice_candidate_ -> sdp_mline_index (); 47} 48 49rust::String IceCandidate ::candidate ()const { 50return stringify (); 51} 52 53rust::String IceCandidate ::stringify ()const { 54 std::string str ; 55ice_candidate_ -> ToString (& str ); 56return rust::String ::lossy (str ); 57} 58 59std::unique_ptr < webrtc::IceCandidateInterface > IceCandidate ::release () { 60return std::move (ice_candidate_ ); 61} 62 63std::shared_ptr < IceCandidate > create_ice_candidate (rust::String sdp_mid , 64int sdp_mline_index , 65 rust::String sdp ) { 66 webrtc::SdpParseError error ; 67auto ice_rtc = webrtc::CreateIceCandidate (sdp_mid .c_str (),sdp_mline_index , 68sdp .c_str (),& error ); 69if (!ice_rtc ) { 70throw std::runtime_error (serialize_sdp_error (error )); 71 } 72 73return std::make_shared < IceCandidate > ( 74 std::unique_ptr < webrtc::IceCandidateInterface > (ice_rtc )); 75} 76 77SessionDescription ::SessionDescription ( 78 std::unique_ptr < webrtc::SessionDescriptionInterface > session_description ) 79 :session_description_ (std::move (session_description )) {} 80 81SdpType SessionDescription ::sdp_type ()const { 82return static_cast < SdpType > (session_description_ -> GetType ()); 83} 84 85rust::String SessionDescription ::stringify ()const { 86 std::string str ; 87session_description_ -> ToString (& str ); 88return rust::String ::lossy (str ); 89} 90 91std::unique_ptr < SessionDescription > SessionDescription ::clone ()const { 92return std::make_unique < SessionDescription > (session_description_ -> Clone ()); 93} 94 95std::unique_ptr < webrtc::SessionDescriptionInterface > 96SessionDescription ::release () { 97return std::move (session_description_ ); 98} 99 100std::unique_ptr < SessionDescription > create_session_description ( 101SdpType type , 102 rust::String sdp ) { 103 webrtc::SdpParseError error ; 104auto rtc_sdp = webrtc::CreateSessionDescription ( 105static_cast < webrtc::SdpType > (type ),sdp .c_str (),& error ); 106if (!rtc_sdp ) { 107throw std::runtime_error (serialize_sdp_error (error )); 108 } 109 110return std::make_unique < SessionDescription > (std::move (rtc_sdp )); 111} 112 113NativeCreateSdpObserver ::NativeCreateSdpObserver ( 114 rust::Box < PeerContext > ctx , 115 rust::Fn < void (rust::Box < PeerContext > , std::unique_ptr < SessionDescription > )> 116on_success , 117 rust::Fn < void (rust::Box < PeerContext > ,RtcError )> on_error ) 118 :ctx_ (std::move (ctx )),on_success_ (on_success ),on_error_ (on_error ) {} 119 120void NativeCreateSdpObserver ::OnSuccess ( 121 webrtc::SessionDescriptionInterface * desc ) { 122// We have ownership of desc 123on_success_ (std::move (ctx_ ), 124 std::make_unique < SessionDescription > ( 125 std::unique_ptr < webrtc::SessionDescriptionInterface > (desc ))); 126} 127 128void NativeCreateSdpObserver ::OnFailure (webrtc::RTCError error ) { 129on_error_ (std::move (ctx_ ),to_error (error )); 130} 131 132NativeSetLocalSdpObserver ::NativeSetLocalSdpObserver ( 133 rust::Box < PeerContext > ctx , 134 rust::Fn < void (rust::Box < PeerContext > ,RtcError )> on_complete ) 135 :ctx_ (std::move (ctx )),on_complete_ (on_complete ) {} 136 137void NativeSetLocalSdpObserver ::OnSetLocalDescriptionComplete ( 138 webrtc::RTCError error ) { 139on_complete_ (std::move (ctx_ ),to_error (error )); 140} 141 142NativeSetRemoteSdpObserver ::NativeSetRemoteSdpObserver ( 143 rust::Box < PeerContext > ctx , 144 rust::Fn < void (rust::Box < PeerContext > ,RtcError )> on_complete ) 145 :ctx_ (std::move (ctx )),on_complete_ (on_complete ) {} 146 147void NativeSetRemoteSdpObserver ::OnSetRemoteDescriptionComplete ( 148 webrtc::RTCError error ) { 149on_complete_ (std::move (ctx_ ),to_error (error )); 150} 151}// namespace livekit