blob: 5f9dcb96aa1917ccb64f708f96d4ab963f11c09b [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 <string>
14#include <type_traits>
Harald Alvestranda39689c2020-10-15 08:34:31 +000015#include <utility>
16
17#include "api/transport/field_trial_based_config.h"
18#include "media/base/rtp_data_engine.h"
Harald Alvestrandffd5dc72020-10-20 15:35:31 +000019#include "rtc_base/helpers.h"
20#include "rtc_base/ref_counted_object.h"
21#include "rtc_base/time_utils.h"
Harald Alvestranda39689c2020-10-15 08:34:31 +000022
23namespace webrtc {
24
25namespace {
26
27rtc::Thread* MaybeStartThread(rtc::Thread* old_thread,
28 const std::string& thread_name,
29 bool with_socket_server,
Harald Alvestrand4244b5f2020-10-15 12:57:05 +000030 std::unique_ptr<rtc::Thread>& thread_holder) {
Harald Alvestranda39689c2020-10-15 08:34:31 +000031 if (old_thread) {
32 return old_thread;
33 }
34 if (with_socket_server) {
Harald Alvestrand4244b5f2020-10-15 12:57:05 +000035 thread_holder = rtc::Thread::CreateWithSocketServer();
Harald Alvestranda39689c2020-10-15 08:34:31 +000036 } else {
Harald Alvestrand4244b5f2020-10-15 12:57:05 +000037 thread_holder = rtc::Thread::Create();
Harald Alvestranda39689c2020-10-15 08:34:31 +000038 }
Harald Alvestrand4244b5f2020-10-15 12:57:05 +000039 thread_holder->SetName(thread_name, nullptr);
40 thread_holder->Start();
41 return thread_holder.get();
Harald Alvestranda39689c2020-10-15 08:34:31 +000042}
43
44rtc::Thread* MaybeWrapThread(rtc::Thread* signaling_thread,
Harald Alvestrand4244b5f2020-10-15 12:57:05 +000045 bool& wraps_current_thread) {
46 wraps_current_thread = false;
Harald Alvestranda39689c2020-10-15 08:34:31 +000047 if (signaling_thread) {
48 return signaling_thread;
49 }
50 auto this_thread = rtc::Thread::Current();
51 if (!this_thread) {
52 // If this thread isn't already wrapped by an rtc::Thread, create a
53 // wrapper and own it in this class.
54 this_thread = rtc::ThreadManager::Instance()->WrapCurrentThread();
Harald Alvestrand4244b5f2020-10-15 12:57:05 +000055 wraps_current_thread = true;
Harald Alvestranda39689c2020-10-15 08:34:31 +000056 }
57 return this_thread;
58}
59
Harald Alvestrand4244b5f2020-10-15 12:57:05 +000060std::unique_ptr<SctpTransportFactoryInterface> MaybeCreateSctpFactory(
61 std::unique_ptr<SctpTransportFactoryInterface> factory,
62 rtc::Thread* network_thread) {
63 if (factory) {
64 return factory;
65 }
Mirko Bonadei5eb43b42021-01-18 13:24:40 +010066#ifdef WEBRTC_HAVE_SCTP
Harald Alvestrand4244b5f2020-10-15 12:57:05 +000067 return std::make_unique<cricket::SctpTransportFactory>(network_thread);
68#else
69 return nullptr;
70#endif
71}
72
Harald Alvestranda39689c2020-10-15 08:34:31 +000073} // namespace
74
Harald Alvestrandffd5dc72020-10-20 15:35:31 +000075// Static
76rtc::scoped_refptr<ConnectionContext> ConnectionContext::Create(
77 PeerConnectionFactoryDependencies* dependencies) {
78 auto context = new rtc::RefCountedObject<ConnectionContext>(dependencies);
79 if (!context->channel_manager_->Init()) {
80 return nullptr;
81 }
82 return context;
83}
84
Harald Alvestranda39689c2020-10-15 08:34:31 +000085ConnectionContext::ConnectionContext(
Harald Alvestrandffd5dc72020-10-20 15:35:31 +000086 PeerConnectionFactoryDependencies* dependencies)
87 : network_thread_(MaybeStartThread(dependencies->network_thread,
Harald Alvestranda39689c2020-10-15 08:34:31 +000088 "pc_network_thread",
89 true,
Harald Alvestrand4244b5f2020-10-15 12:57:05 +000090 owned_network_thread_)),
Harald Alvestrandffd5dc72020-10-20 15:35:31 +000091 worker_thread_(MaybeStartThread(dependencies->worker_thread,
Harald Alvestranda39689c2020-10-15 08:34:31 +000092 "pc_worker_thread",
93 false,
Harald Alvestrand4244b5f2020-10-15 12:57:05 +000094 owned_worker_thread_)),
Harald Alvestrandffd5dc72020-10-20 15:35:31 +000095 signaling_thread_(MaybeWrapThread(dependencies->signaling_thread,
Harald Alvestrand4244b5f2020-10-15 12:57:05 +000096 wraps_current_thread_)),
Harald Alvestrandffd5dc72020-10-20 15:35:31 +000097 network_monitor_factory_(
98 std::move(dependencies->network_monitor_factory)),
99 call_factory_(std::move(dependencies->call_factory)),
100 media_engine_(std::move(dependencies->media_engine)),
101 sctp_factory_(
102 MaybeCreateSctpFactory(std::move(dependencies->sctp_factory),
103 network_thread())),
104 trials_(dependencies->trials
105 ? std::move(dependencies->trials)
106 : std::make_unique<FieldTrialBasedConfig>()) {
Harald Alvestranda39689c2020-10-15 08:34:31 +0000107 signaling_thread_->AllowInvokesToThread(worker_thread_);
108 signaling_thread_->AllowInvokesToThread(network_thread_);
109 worker_thread_->AllowInvokesToThread(network_thread_);
110 network_thread_->DisallowAllInvokes();
Harald Alvestrandffd5dc72020-10-20 15:35:31 +0000111
112 RTC_DCHECK_RUN_ON(signaling_thread_);
113 rtc::InitRandom(rtc::Time32());
114
115 // If network_monitor_factory_ is non-null, it will be used to create a
116 // network monitor while on the network thread.
117 default_network_manager_ = std::make_unique<rtc::BasicNetworkManager>(
118 network_monitor_factory_.get());
119
120 default_socket_factory_ =
121 std::make_unique<rtc::BasicPacketSocketFactory>(network_thread());
122
123 channel_manager_ = std::make_unique<cricket::ChannelManager>(
124 std::move(media_engine_), std::make_unique<cricket::RtpDataEngine>(),
125 worker_thread(), network_thread());
126
127 channel_manager_->SetVideoRtxEnabled(true);
Harald Alvestranda39689c2020-10-15 08:34:31 +0000128}
129
130ConnectionContext::~ConnectionContext() {
Harald Alvestrand4244b5f2020-10-15 12:57:05 +0000131 RTC_DCHECK_RUN_ON(signaling_thread_);
Harald Alvestranda39689c2020-10-15 08:34:31 +0000132 channel_manager_.reset(nullptr);
133
134 // Make sure |worker_thread()| and |signaling_thread()| outlive
135 // |default_socket_factory_| and |default_network_manager_|.
136 default_socket_factory_ = nullptr;
137 default_network_manager_ = nullptr;
138
139 if (wraps_current_thread_)
140 rtc::ThreadManager::Instance()->UnwrapCurrentThread();
141}
142
Harald Alvestranda39689c2020-10-15 08:34:31 +0000143cricket::ChannelManager* ConnectionContext::channel_manager() const {
144 return channel_manager_.get();
145}
146
147} // namespace webrtc