fork/rust-sdks
git clone https://git.julia.blue/fork/rust-sdks
f162413
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/data_channel.h" 18 19#include <utility> 20 21#include "rtc_base/synchronization/mutex.h" 22#include "webrtc-sys/src/data_channel.rs.h" 23 24namespace livekit { 25 26webrtc::DataChannelInit to_native_data_channel_init (DataChannelInit init ) { 27 webrtc::DataChannelInit rtc_init {}; 28rtc_init .id = init .id ; 29rtc_init .negotiated = init .negotiated ; 30rtc_init .ordered = init .ordered ; 31rtc_init .protocol = init .protocol .c_str (); 32 33if (init .has_max_retransmit_time ) 34rtc_init .maxRetransmitTime = init .max_retransmit_time ; 35 36if (init .has_max_retransmits ) 37rtc_init .maxRetransmits = init .max_retransmits ; 38 39if (init .has_priority ) 40rtc_init .priority = static_cast < webrtc::Priority > (init .priority ); 41 42return rtc_init ; 43} 44 45DataChannel ::DataChannel ( 46 std::shared_ptr < RtcRuntime > rtc_runtime , 47 rtc::scoped_refptr < webrtc::DataChannelInterface > data_channel ) 48 :rtc_runtime_ (rtc_runtime ),data_channel_ (std::move (data_channel )) { 49RTC_LOG (LS_VERBOSE ) <<"DataChannel::DataChannel()" ; 50} 51 52DataChannel ::~DataChannel () { 53RTC_LOG (LS_VERBOSE ) <<"DataChannel::~DataChannel()" ; 54unregister_observer (); 55} 56 57void DataChannel ::register_observer ( 58 rust::Box < DataChannelObserverWrapper > observer )const { 59 webrtc::MutexLock lock (& mutex_ ); 60 61data_channel_ -> UnregisterObserver (); 62 63observer_ = 64 std::make_unique < NativeDataChannelObserver > (std::move (observer ),this ); 65data_channel_ -> RegisterObserver (observer_ .get ()); 66} 67 68void DataChannel ::unregister_observer ()const { 69 webrtc::MutexLock lock (& mutex_ ); 70data_channel_ -> UnregisterObserver (); 71observer_ = nullptr ; 72} 73 74bool DataChannel ::send (const DataBuffer & buffer )const { 75return data_channel_ -> Send (webrtc::DataBuffer { 76 rtc::CopyOnWriteBuffer (buffer .ptr ,buffer .len ),buffer .binary }); 77} 78 79int DataChannel ::id ()const { 80return data_channel_ -> id (); 81} 82 83rust::String DataChannel ::label ()const { 84return data_channel_ -> label (); 85} 86 87DataState DataChannel ::state ()const { 88return static_cast < DataState > (data_channel_ -> state ()); 89} 90 91void DataChannel ::close ()const { 92return data_channel_ -> Close (); 93} 94 95uint64_t DataChannel ::buffered_amount ()const { 96return data_channel_ -> buffered_amount (); 97} 98 99NativeDataChannelObserver ::NativeDataChannelObserver ( 100 rust::Box < DataChannelObserverWrapper > observer , 101const DataChannel * dc ) 102 :observer_ (std::move (observer )),dc_ (dc ) {} 103 104void NativeDataChannelObserver ::OnStateChange () { 105observer_ -> on_state_change (dc_ -> state ()); 106} 107 108void NativeDataChannelObserver ::OnMessage (const webrtc::DataBuffer & buffer ) { 109DataBuffer data {}; 110data .ptr = buffer .data .data (); 111data .len = buffer .data .size (); 112data .binary = buffer .binary ; 113observer_ -> on_message (data ); 114} 115 116void NativeDataChannelObserver ::OnBufferedAmountChange ( 117uint64_t sent_data_size ) { 118observer_ -> on_buffered_amount_change (sent_data_size ); 119} 120 121}// namespace livekit