fork/rust-sdks

git clone https://git.julia.blue/fork/rust-sdks

Lean Mendozafix: support non-utf8 std::string which can come e.g. from Windows message errors (#193)3e4669e

main
5.1 KiB176 linesraw
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() {
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(rtc::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_ = rtc::Thread::CreateWithSocketServer();
62  network_thread_->SetName("network_thread", &network_thread_);
63  network_thread_->Start();
64  worker_thread_ = rtc::Thread::Create();
65  worker_thread_->SetName("worker_thread", &worker_thread_);
66  worker_thread_->Start();
67  signaling_thread_ = rtc::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(rtc::CleanupSSL()) << "Failed to CleanupSSL()";
84
85#ifdef WEBRTC_WIN
86      WSACleanup();
87#endif
88    }
89  }
90}
91
92rtc::Thread* RtcRuntime::network_thread() const {
93  return network_thread_.get();
94}
95
96rtc::Thread* RtcRuntime::worker_thread() const {
97  return worker_thread_.get();
98}
99
100rtc::Thread* RtcRuntime::signaling_thread() const {
101  return 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_);
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            rtc::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            rtc::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    rtc::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    rtc::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  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) {
164  fnc_(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) {
169  return std::make_unique<LogSink>(fnc);
170}
171
172rust::String create_random_uuid() {
173  return rtc::CreateRandomUuid();
174}
175
176}  // namespace livekit