BlueGit

ict rusty-teams

Files

rusty-teams / vendor / webrtc-sys / src / webrtc.cpp

webrtc.cpp

webrtc.cpp 5.6 KiB Download Raw
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/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/logging.h"
30#include "rtc_base/crypto_random.h"
31#include "rtc_base/synchronization/mutex.h"
32
33#ifdef WEBRTC_WIN
34#include "rtc_base/win32.h"
35#endif
36
37namespace livekit_ffi {
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() {
45 RTC_LOG(LS_VERBOSE) << "RtcRuntime()";
46
47 {
48 // Not the best way to do it...
49 webrtc::MutexLock lock(&g_mutex);
50 if (g_release_counter == 0) {
51 RTC_CHECK(webrtc::InitializeSSL()) << "Failed to InitializeSSL()";
52
53#ifdef WEBRTC_WIN
54 WSADATA data;
55 WSAStartup(MAKEWORD(1, 0), &data);
56#endif
57 }
58 g_release_counter++;
59 }
60
61 network_thread_ = webrtc::Thread::CreateWithSocketServer();
62 network_thread_->SetName("network_thread", &network_thread_);
63 network_thread_->Start();
64 worker_thread_ = webrtc::Thread::Create();
65 worker_thread_->SetName("worker_thread", &worker_thread_);
66 worker_thread_->Start();
67 signaling_thread_ = webrtc::Thread::Create();
68 signaling_thread_->SetName("signaling_thread", &signaling_thread_);
69 signaling_thread_->Start();
70}
71
72RtcRuntime::~RtcRuntime() {
73 RTC_LOG(LS_VERBOSE) << "~RtcRuntime()";
74
75 worker_thread_->Stop();
76 signaling_thread_->Stop();
77 network_thread_->Stop();
78
79 {
80 webrtc::MutexLock lock(&g_mutex);
81 g_release_counter--;
82 if (g_release_counter == 0) {
83 RTC_CHECK(webrtc::CleanupSSL()) << "Failed to CleanupSSL()";
84
85#ifdef WEBRTC_WIN
86 WSACleanup();
87#endif
88 }
89 }
90}
91
92webrtc::Thread* RtcRuntime::network_thread() const {
93 return network_thread_.get();
94}
95
96webrtc::Thread* RtcRuntime::worker_thread() const {
97 return worker_thread_.get();
98}
99
100webrtc::Thread* RtcRuntime::signaling_thread() const {
101 return signaling_thread_.get();
102}
103
104std::shared_ptr<MediaStreamTrack> RtcRuntime::get_or_create_media_stream_track(
105 webrtc::scoped_refptr<webrtc::MediaStreamTrackInterface> rtc_track) {
106 webrtc::MutexLock lock(&mutex_);
107 for (std::weak_ptr<MediaStreamTrack> weak_existing_track :
108 media_stream_tracks_) {
109 if (std::shared_ptr<MediaStreamTrack> existing_track =
110 weak_existing_track.lock()) {
111 if (existing_track->rtc_track() == rtc_track) {
112 return existing_track;
113 }
114 }
115 }
116
117 if (rtc_track->kind() == webrtc::MediaStreamTrackInterface::kVideoKind) {
118 std::shared_ptr<VideoTrack> video_track =
119 std::shared_ptr<VideoTrack>(new VideoTrack(
120 shared_from_this(),
121 webrtc::scoped_refptr<webrtc::VideoTrackInterface>(
122 static_cast<webrtc::VideoTrackInterface*>(rtc_track.get()))));
123
124 media_stream_tracks_.push_back(
125 std::static_pointer_cast<MediaStreamTrack>(video_track));
126 return video_track;
127 } else {
128 std::shared_ptr<AudioTrack> audio_track =
129 std::shared_ptr<AudioTrack>(new AudioTrack(
130 shared_from_this(),
131 webrtc::scoped_refptr<webrtc::AudioTrackInterface>(
132 static_cast<webrtc::AudioTrackInterface*>(rtc_track.get()))));
133
134 media_stream_tracks_.push_back(
135 std::static_pointer_cast<MediaStreamTrack>(audio_track));
136 return audio_track;
137 }
138}
139
140std::shared_ptr<AudioTrack> RtcRuntime::get_or_create_audio_track(
141 webrtc::scoped_refptr<webrtc::AudioTrackInterface> track) {
142 return std::static_pointer_cast<AudioTrack>(
143 get_or_create_media_stream_track(track));
144}
145
146std::shared_ptr<VideoTrack> RtcRuntime::get_or_create_video_track(
147 webrtc::scoped_refptr<webrtc::VideoTrackInterface> track) {
148 return std::static_pointer_cast<VideoTrack>(
149 get_or_create_media_stream_track(track));
150}
151
152LogSink::LogSink(
153 rust::Fn<void(rust::String message, LoggingSeverity severity)> fnc)
154 : fnc_(fnc) {
155 // m150 replaced the old "release builds don't log" default with LoggingConfig,
156 // which defaults to log_to_stderr=true / debug_severity=LS_INFO and installs
157 // itself on the first log call. That writes straight to stderr, bypassing this
158 // sink and therefore the Rust log facade, so nothing downstream can filter it.
159 // Route everything through the sink only.
160 webrtc::LogMessage::SetLogToStderr(false);
161 webrtc::LogMessage::LogToDebug(webrtc::LoggingSeverity::LS_NONE);
162
163 webrtc::LogMessage::AddLogToStream(this, webrtc::LoggingSeverity::LS_VERBOSE);
164}
165
166LogSink::~LogSink() {
167 webrtc::LogMessage::RemoveLogToStream(this);
168}
169
170void LogSink::OnLogMessage(const std::string& message,
171 webrtc::LoggingSeverity severity) {
172 fnc_(rust::String::lossy(message), static_cast<LoggingSeverity>(severity));
173}
174
175std::unique_ptr<LogSink> new_log_sink(
176 rust::Fn<void(rust::String, LoggingSeverity)> fnc) {
177 return std::make_unique<LogSink>(fnc);
178}
179
180rust::String create_random_uuid() {
181 return webrtc::CreateRandomUuid();
182}
183
184} // namespace livekit_ffi