Harald Alvestrand | a39689c | 2020-10-15 08:34:31 +0000 | [diff] [blame] | 1 | /* |
| 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 Alvestrand | ffd5dc7 | 2020-10-20 15:35:31 +0000 | [diff] [blame] | 13 | #include <string> |
| 14 | #include <type_traits> |
Harald Alvestrand | a39689c | 2020-10-15 08:34:31 +0000 | [diff] [blame] | 15 | #include <utility> |
| 16 | |
| 17 | #include "api/transport/field_trial_based_config.h" |
| 18 | #include "media/base/rtp_data_engine.h" |
Harald Alvestrand | ffd5dc7 | 2020-10-20 15:35:31 +0000 | [diff] [blame] | 19 | #include "rtc_base/helpers.h" |
| 20 | #include "rtc_base/ref_counted_object.h" |
| 21 | #include "rtc_base/time_utils.h" |
Harald Alvestrand | a39689c | 2020-10-15 08:34:31 +0000 | [diff] [blame] | 22 | |
| 23 | namespace webrtc { |
| 24 | |
| 25 | namespace { |
| 26 | |
| 27 | rtc::Thread* MaybeStartThread(rtc::Thread* old_thread, |
| 28 | const std::string& thread_name, |
| 29 | bool with_socket_server, |
Harald Alvestrand | 4244b5f | 2020-10-15 12:57:05 +0000 | [diff] [blame] | 30 | std::unique_ptr<rtc::Thread>& thread_holder) { |
Harald Alvestrand | a39689c | 2020-10-15 08:34:31 +0000 | [diff] [blame] | 31 | if (old_thread) { |
| 32 | return old_thread; |
| 33 | } |
| 34 | if (with_socket_server) { |
Harald Alvestrand | 4244b5f | 2020-10-15 12:57:05 +0000 | [diff] [blame] | 35 | thread_holder = rtc::Thread::CreateWithSocketServer(); |
Harald Alvestrand | a39689c | 2020-10-15 08:34:31 +0000 | [diff] [blame] | 36 | } else { |
Harald Alvestrand | 4244b5f | 2020-10-15 12:57:05 +0000 | [diff] [blame] | 37 | thread_holder = rtc::Thread::Create(); |
Harald Alvestrand | a39689c | 2020-10-15 08:34:31 +0000 | [diff] [blame] | 38 | } |
Harald Alvestrand | 4244b5f | 2020-10-15 12:57:05 +0000 | [diff] [blame] | 39 | thread_holder->SetName(thread_name, nullptr); |
| 40 | thread_holder->Start(); |
| 41 | return thread_holder.get(); |
Harald Alvestrand | a39689c | 2020-10-15 08:34:31 +0000 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | rtc::Thread* MaybeWrapThread(rtc::Thread* signaling_thread, |
Harald Alvestrand | 4244b5f | 2020-10-15 12:57:05 +0000 | [diff] [blame] | 45 | bool& wraps_current_thread) { |
| 46 | wraps_current_thread = false; |
Harald Alvestrand | a39689c | 2020-10-15 08:34:31 +0000 | [diff] [blame] | 47 | 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 Alvestrand | 4244b5f | 2020-10-15 12:57:05 +0000 | [diff] [blame] | 55 | wraps_current_thread = true; |
Harald Alvestrand | a39689c | 2020-10-15 08:34:31 +0000 | [diff] [blame] | 56 | } |
| 57 | return this_thread; |
| 58 | } |
| 59 | |
Harald Alvestrand | 4244b5f | 2020-10-15 12:57:05 +0000 | [diff] [blame] | 60 | std::unique_ptr<SctpTransportFactoryInterface> MaybeCreateSctpFactory( |
| 61 | std::unique_ptr<SctpTransportFactoryInterface> factory, |
| 62 | rtc::Thread* network_thread) { |
| 63 | if (factory) { |
| 64 | return factory; |
| 65 | } |
Mirko Bonadei | 5eb43b4 | 2021-01-18 13:24:40 +0100 | [diff] [blame^] | 66 | #ifdef WEBRTC_HAVE_SCTP |
Harald Alvestrand | 4244b5f | 2020-10-15 12:57:05 +0000 | [diff] [blame] | 67 | return std::make_unique<cricket::SctpTransportFactory>(network_thread); |
| 68 | #else |
| 69 | return nullptr; |
| 70 | #endif |
| 71 | } |
| 72 | |
Harald Alvestrand | a39689c | 2020-10-15 08:34:31 +0000 | [diff] [blame] | 73 | } // namespace |
| 74 | |
Harald Alvestrand | ffd5dc7 | 2020-10-20 15:35:31 +0000 | [diff] [blame] | 75 | // Static |
| 76 | rtc::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 Alvestrand | a39689c | 2020-10-15 08:34:31 +0000 | [diff] [blame] | 85 | ConnectionContext::ConnectionContext( |
Harald Alvestrand | ffd5dc7 | 2020-10-20 15:35:31 +0000 | [diff] [blame] | 86 | PeerConnectionFactoryDependencies* dependencies) |
| 87 | : network_thread_(MaybeStartThread(dependencies->network_thread, |
Harald Alvestrand | a39689c | 2020-10-15 08:34:31 +0000 | [diff] [blame] | 88 | "pc_network_thread", |
| 89 | true, |
Harald Alvestrand | 4244b5f | 2020-10-15 12:57:05 +0000 | [diff] [blame] | 90 | owned_network_thread_)), |
Harald Alvestrand | ffd5dc7 | 2020-10-20 15:35:31 +0000 | [diff] [blame] | 91 | worker_thread_(MaybeStartThread(dependencies->worker_thread, |
Harald Alvestrand | a39689c | 2020-10-15 08:34:31 +0000 | [diff] [blame] | 92 | "pc_worker_thread", |
| 93 | false, |
Harald Alvestrand | 4244b5f | 2020-10-15 12:57:05 +0000 | [diff] [blame] | 94 | owned_worker_thread_)), |
Harald Alvestrand | ffd5dc7 | 2020-10-20 15:35:31 +0000 | [diff] [blame] | 95 | signaling_thread_(MaybeWrapThread(dependencies->signaling_thread, |
Harald Alvestrand | 4244b5f | 2020-10-15 12:57:05 +0000 | [diff] [blame] | 96 | wraps_current_thread_)), |
Harald Alvestrand | ffd5dc7 | 2020-10-20 15:35:31 +0000 | [diff] [blame] | 97 | 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 Alvestrand | a39689c | 2020-10-15 08:34:31 +0000 | [diff] [blame] | 107 | signaling_thread_->AllowInvokesToThread(worker_thread_); |
| 108 | signaling_thread_->AllowInvokesToThread(network_thread_); |
| 109 | worker_thread_->AllowInvokesToThread(network_thread_); |
| 110 | network_thread_->DisallowAllInvokes(); |
Harald Alvestrand | ffd5dc7 | 2020-10-20 15:35:31 +0000 | [diff] [blame] | 111 | |
| 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 Alvestrand | a39689c | 2020-10-15 08:34:31 +0000 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | ConnectionContext::~ConnectionContext() { |
Harald Alvestrand | 4244b5f | 2020-10-15 12:57:05 +0000 | [diff] [blame] | 131 | RTC_DCHECK_RUN_ON(signaling_thread_); |
Harald Alvestrand | a39689c | 2020-10-15 08:34:31 +0000 | [diff] [blame] | 132 | 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 Alvestrand | a39689c | 2020-10-15 08:34:31 +0000 | [diff] [blame] | 143 | cricket::ChannelManager* ConnectionContext::channel_manager() const { |
| 144 | return channel_manager_.get(); |
| 145 | } |
| 146 | |
| 147 | } // namespace webrtc |