fork/rust-sdks
git clone https://git.julia.blue/fork/rust-sdks
3e4669e
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/webrtc.h" 18 19#include <algorithm> 20#include <atomic> 21#include <iostream> 22#include <memory> 23 24#include "livekit/audio_track.h" 25#include "livekit/media_stream_track.h" 26#include "livekit/rtp_receiver.h" 27#include "livekit/rtp_sender.h" 28#include "livekit/video_track.h" 29#include "rtc_base/helpers.h" 30#include "rtc_base/logging.h" 31#include "rtc_base/synchronization/mutex.h" 32 33#ifdef WEBRTC_WIN 34#include "rtc_base/win32.h" 35#endif 36 37namespace livekit { 38 39static webrtc::Mutex g_mutex {}; 40// Can't be atomic, we're using a Mutex because we need to wait for the 41// execution of the first init 42static uint32_t g_release_counter (0 ); 43 44RtcRuntime ::RtcRuntime () { 45RTC_LOG (LS_VERBOSE ) <<"RtcRuntime()" ; 46 47 { 48// Not the best way to do it... 49 webrtc::MutexLock lock (& g_mutex ); 50if (g_release_counter == 0 ) { 51RTC_CHECK (rtc::InitializeSSL ()) <<"Failed to InitializeSSL()" ; 52 53#ifdef WEBRTC_WIN 54WSADATA data ; 55WSAStartup (MAKEWORD (1 ,0 ),& data ); 56#endif 57 } 58g_release_counter ++ ; 59 } 60 61network_thread_ = rtc::Thread ::CreateWithSocketServer (); 62network_thread_ -> SetName ("network_thread" ,& network_thread_ ); 63network_thread_ -> Start (); 64worker_thread_ = rtc::Thread ::Create (); 65worker_thread_ -> SetName ("worker_thread" ,& worker_thread_ ); 66worker_thread_ -> Start (); 67signaling_thread_ = rtc::Thread ::Create (); 68signaling_thread_ -> SetName ("signaling_thread" ,& signaling_thread_ ); 69signaling_thread_ -> Start (); 70} 71 72RtcRuntime ::~RtcRuntime () { 73RTC_LOG (LS_VERBOSE ) <<"~RtcRuntime()" ; 74 75worker_thread_ -> Stop (); 76signaling_thread_ -> Stop (); 77network_thread_ -> Stop (); 78 79 { 80 webrtc::MutexLock lock (& g_mutex ); 81g_release_counter -- ; 82if (g_release_counter == 0 ) { 83RTC_CHECK (rtc::CleanupSSL ()) <<"Failed to CleanupSSL()" ; 84 85#ifdef WEBRTC_WIN 86WSACleanup (); 87#endif 88 } 89 } 90} 91 92rtc::Thread * RtcRuntime ::network_thread ()const { 93return network_thread_ .get (); 94} 95 96rtc::Thread * RtcRuntime ::worker_thread ()const { 97return worker_thread_ .get (); 98} 99 100rtc::Thread * RtcRuntime ::signaling_thread ()const { 101return signaling_thread_ .get (); 102} 103 104std::shared_ptr < MediaStreamTrack > RtcRuntime ::get_or_create_media_stream_track ( 105 rtc::scoped_refptr < webrtc::MediaStreamTrackInterface > rtc_track ) { 106 webrtc::MutexLock lock (& mutex_ ); 107for (std::weak_ptr < MediaStreamTrack > weak_existing_track : 108media_stream_tracks_ ) { 109if (std::shared_ptr < MediaStreamTrack > existing_track = 110weak_existing_track .lock ()) { 111if (existing_track -> rtc_track ()== rtc_track ) { 112return existing_track ; 113 } 114 } 115 } 116 117if (rtc_track -> kind ()== webrtc::MediaStreamTrackInterface ::kVideoKind ) { 118 std::shared_ptr < VideoTrack > video_track = 119 std::shared_ptr < VideoTrack > (new VideoTrack ( 120shared_from_this (), 121 rtc::scoped_refptr < webrtc::VideoTrackInterface > ( 122static_cast < webrtc::VideoTrackInterface *> (rtc_track .get ())))); 123 124media_stream_tracks_ .push_back ( 125 std::static_pointer_cast < MediaStreamTrack > (video_track )); 126return video_track ; 127 }else { 128 std::shared_ptr < AudioTrack > audio_track = 129 std::shared_ptr < AudioTrack > (new AudioTrack ( 130shared_from_this (), 131 rtc::scoped_refptr < webrtc::AudioTrackInterface > ( 132static_cast < webrtc::AudioTrackInterface *> (rtc_track .get ())))); 133 134media_stream_tracks_ .push_back ( 135 std::static_pointer_cast < MediaStreamTrack > (audio_track )); 136return audio_track ; 137 } 138} 139 140std::shared_ptr < AudioTrack > RtcRuntime ::get_or_create_audio_track ( 141 rtc::scoped_refptr < webrtc::AudioTrackInterface > track ) { 142return std::static_pointer_cast < AudioTrack > ( 143get_or_create_media_stream_track (track )); 144} 145 146std::shared_ptr < VideoTrack > RtcRuntime ::get_or_create_video_track ( 147 rtc::scoped_refptr < webrtc::VideoTrackInterface > track ) { 148return std::static_pointer_cast < VideoTrack > ( 149get_or_create_media_stream_track (track )); 150} 151 152LogSink ::LogSink ( 153 rust::Fn < void (rust::String message ,LoggingSeverity severity )> fnc ) 154 :fnc_ (fnc ) { 155 rtc::LogMessage ::AddLogToStream (this , rtc::LoggingSeverity ::LS_VERBOSE ); 156} 157 158LogSink ::~LogSink () { 159 rtc::LogMessage ::RemoveLogToStream (this ); 160} 161 162void LogSink ::OnLogMessage (const std::string & message , 163 rtc::LoggingSeverity severity ) { 164fnc_ (rust::String ::lossy (message ),static_cast < LoggingSeverity > (severity )); 165} 166 167std::unique_ptr < LogSink > new_log_sink ( 168 rust::Fn < void (rust::String ,LoggingSeverity )> fnc ) { 169return std::make_unique < LogSink > (fnc ); 170} 171 172rust::String create_random_uuid () { 173return rtc::CreateRandomUuid (); 174} 175 176}// namespace livekit