blob: ec6f21cc1394c26a4058c0c5d67fa087f79409fa [file] [log] [blame]
Harald Alvestranda39689c2020-10-15 08:34:31 +00001/*
2 * Copyright 2020 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11#include "pc/connection_context.h"
12
Harald Alvestrandffd5dc72020-10-20 15:35:31 +000013#include <type_traits>
Harald Alvestranda39689c2020-10-15 08:34:31 +000014#include <utility>
Byoungchan Lee14210412022-06-17 17:29:03 +090015#include <vector>
Harald Alvestranda39689c2020-10-15 08:34:31 +000016
17#include "api/transport/field_trial_based_config.h"
Harald Alvestrandc24a2182022-02-23 13:44:59 +000018#include "media/base/media_engine.h"
Florent Castelli6b0f19f2021-04-08 14:59:12 +020019#include "media/sctp/sctp_transport_factory.h"
Harald Alvestrandffd5dc72020-10-20 15:35:31 +000020#include "rtc_base/helpers.h"
Niels Möllerb02e1ac2022-02-04 14:29:50 +010021#include "rtc_base/internal/default_socket_server.h"
Harald Alvestrandc24a2182022-02-23 13:44:59 +000022#include "rtc_base/socket_server.h"
Harald Alvestrandffd5dc72020-10-20 15:35:31 +000023#include "rtc_base/time_utils.h"
Harald Alvestranda39689c2020-10-15 08:34:31 +000024
25namespace webrtc {
26
27namespace {
28
Niels Möllerb02e1ac2022-02-04 14:29:50 +010029rtc::Thread* MaybeStartNetworkThread(
30 rtc::Thread* old_thread,
31 std::unique_ptr<rtc::SocketFactory>& socket_factory_holder,
32 std::unique_ptr<rtc::Thread>& thread_holder) {
Harald Alvestranda39689c2020-10-15 08:34:31 +000033 if (old_thread) {
34 return old_thread;
35 }
Niels Möllerb02e1ac2022-02-04 14:29:50 +010036 std::unique_ptr<rtc::SocketServer> socket_server =
37 rtc::CreateDefaultSocketServer();
38 thread_holder = std::make_unique<rtc::Thread>(socket_server.get());
39 socket_factory_holder = std::move(socket_server);
40
41 thread_holder->SetName("pc_network_thread", nullptr);
42 thread_holder->Start();
43 return thread_holder.get();
44}
45
Harald Alvestranda39689c2020-10-15 08:34:31 +000046rtc::Thread* MaybeWrapThread(rtc::Thread* signaling_thread,
Harald Alvestrand4244b5f2020-10-15 12:57:05 +000047 bool& wraps_current_thread) {
48 wraps_current_thread = false;
Harald Alvestranda39689c2020-10-15 08:34:31 +000049 if (signaling_thread) {
50 return signaling_thread;
51 }
52 auto this_thread = rtc::Thread::Current();
53 if (!this_thread) {
54 // If this thread isn't already wrapped by an rtc::Thread, create a
55 // wrapper and own it in this class.
56 this_thread = rtc::ThreadManager::Instance()->WrapCurrentThread();
Harald Alvestrand4244b5f2020-10-15 12:57:05 +000057 wraps_current_thread = true;
Harald Alvestranda39689c2020-10-15 08:34:31 +000058 }
59 return this_thread;
60}
61
Harald Alvestrand4244b5f2020-10-15 12:57:05 +000062std::unique_ptr<SctpTransportFactoryInterface> MaybeCreateSctpFactory(
63 std::unique_ptr<SctpTransportFactoryInterface> factory,
Jonas Orelanded99dae2022-03-09 09:28:10 +010064 rtc::Thread* network_thread,
Jonas Orelande62c2f22022-03-29 11:04:48 +020065 const FieldTrialsView& field_trials) {
Harald Alvestrand4244b5f2020-10-15 12:57:05 +000066 if (factory) {
67 return factory;
68 }
Mirko Bonadei5eb43b42021-01-18 13:24:40 +010069#ifdef WEBRTC_HAVE_SCTP
Florent Castellif2599a72022-03-31 19:15:10 +020070 return std::make_unique<cricket::SctpTransportFactory>(network_thread);
Harald Alvestrand4244b5f2020-10-15 12:57:05 +000071#else
72 return nullptr;
73#endif
74}
75
Harald Alvestranda39689c2020-10-15 08:34:31 +000076} // namespace
77
Harald Alvestrandffd5dc72020-10-20 15:35:31 +000078// Static
79rtc::scoped_refptr<ConnectionContext> ConnectionContext::Create(
80 PeerConnectionFactoryDependencies* dependencies) {
Niels Möllere7cc8832022-01-04 15:20:03 +010081 return rtc::scoped_refptr<ConnectionContext>(
82 new ConnectionContext(dependencies));
Harald Alvestrandffd5dc72020-10-20 15:35:31 +000083}
84
Harald Alvestranda39689c2020-10-15 08:34:31 +000085ConnectionContext::ConnectionContext(
Harald Alvestrandffd5dc72020-10-20 15:35:31 +000086 PeerConnectionFactoryDependencies* dependencies)
Niels Möllerb02e1ac2022-02-04 14:29:50 +010087 : network_thread_(MaybeStartNetworkThread(dependencies->network_thread,
88 owned_socket_factory_,
89 owned_network_thread_)),
Harald Alvestrand00579e82022-05-03 11:37:34 +000090 worker_thread_(dependencies->worker_thread,
91 []() {
92 auto thread_holder = rtc::Thread::Create();
93 thread_holder->SetName("pc_worker_thread", nullptr);
94 thread_holder->Start();
95 return thread_holder;
96 }),
Harald Alvestrandffd5dc72020-10-20 15:35:31 +000097 signaling_thread_(MaybeWrapThread(dependencies->signaling_thread,
Harald Alvestrand4244b5f2020-10-15 12:57:05 +000098 wraps_current_thread_)),
Jonas Orelanded99dae2022-03-09 09:28:10 +010099 trials_(dependencies->trials ? std::move(dependencies->trials)
100 : std::make_unique<FieldTrialBasedConfig>()),
Harald Alvestrand0ac50b92022-05-18 07:51:34 +0000101 media_engine_(std::move(dependencies->media_engine)),
Harald Alvestrandffd5dc72020-10-20 15:35:31 +0000102 network_monitor_factory_(
103 std::move(dependencies->network_monitor_factory)),
Niels Möllerdcb5a582022-06-20 15:33:59 +0200104 default_network_manager_(std::move(dependencies->network_manager)),
Harald Alvestrandffd5dc72020-10-20 15:35:31 +0000105 call_factory_(std::move(dependencies->call_factory)),
Niels Möller573b1452022-06-21 11:37:29 +0200106 default_socket_factory_(std::move(dependencies->packet_socket_factory)),
Harald Alvestrandffd5dc72020-10-20 15:35:31 +0000107 sctp_factory_(
108 MaybeCreateSctpFactory(std::move(dependencies->sctp_factory),
Jonas Orelanded99dae2022-03-09 09:28:10 +0100109 network_thread(),
110 *trials_.get())) {
Byoungchan Lee14210412022-06-17 17:29:03 +0900111 RTC_DCHECK_RUN_ON(signaling_thread_);
Niels Möllerdcb5a582022-06-20 15:33:59 +0200112 RTC_DCHECK(!(default_network_manager_ && network_monitor_factory_))
113 << "You can't set both network_manager and network_monitor_factory.";
114
Harald Alvestrand00579e82022-05-03 11:37:34 +0000115 signaling_thread_->AllowInvokesToThread(worker_thread());
Harald Alvestranda39689c2020-10-15 08:34:31 +0000116 signaling_thread_->AllowInvokesToThread(network_thread_);
117 worker_thread_->AllowInvokesToThread(network_thread_);
Byoungchan Lee14210412022-06-17 17:29:03 +0900118 if (!network_thread_->IsCurrent()) {
119 // network_thread_->IsCurrent() == true means signaling_thread_ is
120 // network_thread_. In this case, no further action is required as
121 // signaling_thread_ can already invoke network_thread_.
Danil Chapovalova30439b2022-07-07 10:08:49 +0200122 network_thread_->PostTask(
Byoungchan Lee14210412022-06-17 17:29:03 +0900123 [thread = network_thread_, worker_thread = worker_thread_.get()] {
124 thread->DisallowBlockingCalls();
125 thread->DisallowAllInvokes();
126 if (worker_thread == thread) {
127 // In this case, worker_thread_ == network_thread_
128 thread->AllowInvokesToThread(thread);
129 }
Danil Chapovalova30439b2022-07-07 10:08:49 +0200130 });
Tomas Gunnarsson0b5ec182021-04-01 16:49:42 +0200131 }
Harald Alvestrandffd5dc72020-10-20 15:35:31 +0000132
Harald Alvestrandffd5dc72020-10-20 15:35:31 +0000133 rtc::InitRandom(rtc::Time32());
134
Niels Möllerb02e1ac2022-02-04 14:29:50 +0100135 rtc::SocketFactory* socket_factory = dependencies->socket_factory;
136 if (socket_factory == nullptr) {
137 if (owned_socket_factory_) {
138 socket_factory = owned_socket_factory_.get();
139 } else {
140 // TODO(bugs.webrtc.org/13145): This case should be deleted. Either
141 // require that a PacketSocketFactory and NetworkManager always are
142 // injected (with no need to construct these default objects), or require
143 // that if a network_thread is injected, an approprite rtc::SocketServer
144 // should be injected too.
145 socket_factory = network_thread()->socketserver();
146 }
147 }
Niels Möllerdcb5a582022-06-20 15:33:59 +0200148 if (!default_network_manager_) {
149 // If network_monitor_factory_ is non-null, it will be used to create a
150 // network monitor while on the network thread.
151 default_network_manager_ = std::make_unique<rtc::BasicNetworkManager>(
152 network_monitor_factory_.get(), socket_factory, &field_trials());
153 }
Niels Möller573b1452022-06-21 11:37:29 +0200154 if (!default_socket_factory_) {
155 default_socket_factory_ =
156 std::make_unique<rtc::BasicPacketSocketFactory>(socket_factory);
157 }
Harald Alvestrandba694422021-01-27 21:52:14 +0000158 // Set warning levels on the threads, to give warnings when response
159 // may be slower than is expected of the thread.
160 // Since some of the threads may be the same, start with the least
161 // restrictive limits and end with the least permissive ones.
162 // This will give warnings for all cases.
163 signaling_thread_->SetDispatchWarningMs(100);
164 worker_thread_->SetDispatchWarningMs(30);
165 network_thread_->SetDispatchWarningMs(10);
Harald Alvestrand485457f2022-05-23 08:46:57 +0000166
167 if (media_engine_) {
168 // TODO(tommi): Change VoiceEngine to do ctor time initialization so that
169 // this isn't necessary.
Danil Chapovalov9e09a1f2022-09-08 18:38:10 +0200170 worker_thread_->BlockingCall([&] { media_engine_->Init(); });
Harald Alvestrand485457f2022-05-23 08:46:57 +0000171 }
Harald Alvestranda39689c2020-10-15 08:34:31 +0000172}
173
174ConnectionContext::~ConnectionContext() {
Harald Alvestrand4244b5f2020-10-15 12:57:05 +0000175 RTC_DCHECK_RUN_ON(signaling_thread_);
Danil Chapovalov9e09a1f2022-09-08 18:38:10 +0200176 worker_thread_->BlockingCall([&] {
Harald Alvestrand0ac50b92022-05-18 07:51:34 +0000177 RTC_DCHECK_RUN_ON(worker_thread());
Harald Alvestrand485457f2022-05-23 08:46:57 +0000178 // While `media_engine_` is const throughout the ConnectionContext's
179 // lifetime, it requires destruction to happen on the worker thread. Instead
180 // of marking the pointer as non-const, we live with this const_cast<> in
181 // the destructor.
Harald Alvestrand0ac50b92022-05-18 07:51:34 +0000182 const_cast<std::unique_ptr<cricket::MediaEngineInterface>&>(media_engine_)
183 .reset();
184 });
Harald Alvestranda39689c2020-10-15 08:34:31 +0000185
Artem Titov880fa812021-07-30 22:30:23 +0200186 // Make sure `worker_thread()` and `signaling_thread()` outlive
187 // `default_socket_factory_` and `default_network_manager_`.
Harald Alvestranda39689c2020-10-15 08:34:31 +0000188 default_socket_factory_ = nullptr;
189 default_network_manager_ = nullptr;
190
191 if (wraps_current_thread_)
192 rtc::ThreadManager::Instance()->UnwrapCurrentThread();
193}
194
Harald Alvestranda39689c2020-10-15 08:34:31 +0000195} // namespace webrtc