| 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 |
| 13 | #include <type_traits> |
| 14 | #include <utility> |
| 15 | #include <vector> |
| 16 |
| 17 | #include "api/environment/environment.h" |
| 18 | #include "media/base/media_engine.h" |
| 19 | #include "media/sctp/sctp_transport_factory.h" |
| 20 | #include "pc/media_factory.h" |
| 21 | #include "rtc_base/helpers.h" |
| 22 | #include "rtc_base/internal/default_socket_server.h" |
| 23 | #include "rtc_base/socket_server.h" |
| 24 | #include "rtc_base/time_utils.h" |
| 25 |
| 26 | namespace webrtc { |
| 27 |
| 28 | namespace { |
| 29 |
| 30 | rtc::Thread* MaybeStartNetworkThread( |
| 31 | rtc::Thread* old_thread, |
| 32 | std::unique_ptr<rtc::SocketFactory>& socket_factory_holder, |
| 33 | std::unique_ptr<rtc::Thread>& thread_holder) { |
| 34 | if (old_thread) { |
| 35 | return old_thread; |
| 36 | } |
| 37 | std::unique_ptr<rtc::SocketServer> socket_server = |
| 38 | rtc::CreateDefaultSocketServer(); |
| 39 | thread_holder = std::make_unique<rtc::Thread>(socket_server.get()); |
| 40 | socket_factory_holder = std::move(socket_server); |
| 41 |
| 42 | thread_holder->SetName("pc_network_thread", nullptr); |
| 43 | thread_holder->Start(); |
| 44 | return thread_holder.get(); |
| 45 | } |
| 46 |
| 47 | rtc::Thread* MaybeWrapThread(rtc::Thread* signaling_thread, |
| 48 | bool& wraps_current_thread) { |
| 49 | wraps_current_thread = false; |
| 50 | if (signaling_thread) { |
| 51 | return signaling_thread; |
| 52 | } |
| 53 | auto this_thread = rtc::Thread::Current(); |
| 54 | if (!this_thread) { |
| 55 | // If this thread isn't already wrapped by an rtc::Thread, create a |
| 56 | // wrapper and own it in this class. |
| 57 | this_thread = rtc::ThreadManager::Instance()->WrapCurrentThread(); |
| 58 | wraps_current_thread = true; |
| 59 | } |
| 60 | return this_thread; |
| 61 | } |
| 62 |
| 63 | std::unique_ptr<SctpTransportFactoryInterface> MaybeCreateSctpFactory( |
| 64 | std::unique_ptr<SctpTransportFactoryInterface> factory, |
| 65 | rtc::Thread* network_thread) { |
| 66 | if (factory) { |
| 67 | return factory; |
| 68 | } |
| 69 | #ifdef WEBRTC_HAVE_SCTP |
| 70 | return std::make_unique<cricket::SctpTransportFactory>(network_thread); |
| 71 | #else |
| 72 | return nullptr; |
| 73 | #endif |
| 74 | } |
| 75 |
| 76 | } // namespace |
| 77 |
| 78 | // Static |
| 79 | rtc::scoped_refptr<ConnectionContext> ConnectionContext::Create( |
| 80 | const Environment& env, |
| 81 | PeerConnectionFactoryDependencies* dependencies) { |
| 82 | return rtc::scoped_refptr<ConnectionContext>( |
| 83 | new ConnectionContext(env, dependencies)); |
| 84 | } |
| 85 |
| 86 | ConnectionContext::ConnectionContext( |
| 87 | const Environment& env, |
| 88 | PeerConnectionFactoryDependencies* dependencies) |
| 89 | : network_thread_(MaybeStartNetworkThread(dependencies->network_thread, |
| 90 | owned_socket_factory_, |
| 91 | owned_network_thread_)), |
| 92 | worker_thread_(dependencies->worker_thread, |
| 93 | []() { |
| 94 | auto thread_holder = rtc::Thread::Create(); |
| 95 | thread_holder->SetName("pc_worker_thread", nullptr); |
| 96 | thread_holder->Start(); |
| 97 | return thread_holder; |
| 98 | }), |
| 99 | signaling_thread_(MaybeWrapThread(dependencies->signaling_thread, |
| 100 | wraps_current_thread_)), |
| 101 | env_(env), |
| 102 | media_engine_( |
| 103 | dependencies->media_factory != nullptr |
| 104 | ? dependencies->media_factory->CreateMediaEngine(env_, |
| 105 | *dependencies) |
| 106 | : nullptr), |
| 107 | network_monitor_factory_( |
| 108 | std::move(dependencies->network_monitor_factory)), |
| 109 | default_network_manager_(std::move(dependencies->network_manager)), |
| 110 | call_factory_(std::move(dependencies->media_factory)), |
| 111 | default_socket_factory_(std::move(dependencies->packet_socket_factory)), |
| 112 | sctp_factory_( |
| 113 | MaybeCreateSctpFactory(std::move(dependencies->sctp_factory), |
| 114 | network_thread())), |
| 115 | use_rtx_(true) { |
| 116 | RTC_DCHECK_RUN_ON(signaling_thread_); |
| 117 | RTC_DCHECK(!(default_network_manager_ && network_monitor_factory_)) |
| 118 | << "You can't set both network_manager and network_monitor_factory."; |
| 119 |
| 120 | signaling_thread_->AllowInvokesToThread(worker_thread()); |
| 121 | signaling_thread_->AllowInvokesToThread(network_thread_); |
| 122 | worker_thread_->AllowInvokesToThread(network_thread_); |
| 123 | if (!network_thread_->IsCurrent()) { |
| 124 | // network_thread_->IsCurrent() == true means signaling_thread_ is |
| 125 | // network_thread_. In this case, no further action is required as |
| 126 | // signaling_thread_ can already invoke network_thread_. |
| 127 | network_thread_->PostTask( |
| 128 | [thread = network_thread_, worker_thread = worker_thread_.get()] { |
| 129 | thread->DisallowBlockingCalls(); |
| 130 | thread->DisallowAllInvokes(); |
| 131 | if (worker_thread == thread) { |
| 132 | // In this case, worker_thread_ == network_thread_ |
| 133 | thread->AllowInvokesToThread(thread); |
| 134 | } |
| 135 | }); |
| 136 | } |
| 137 |
| 138 | rtc::InitRandom(rtc::Time32()); |
| 139 |
| 140 | rtc::SocketFactory* socket_factory = dependencies->socket_factory; |
| 141 | if (socket_factory == nullptr) { |
| 142 | if (owned_socket_factory_) { |
| 143 | socket_factory = owned_socket_factory_.get(); |
| 144 | } else { |
| 145 | // TODO(bugs.webrtc.org/13145): This case should be deleted. Either |
| 146 | // require that a PacketSocketFactory and NetworkManager always are |
| 147 | // injected (with no need to construct these default objects), or require |
| 148 | // that if a network_thread is injected, an approprite rtc::SocketServer |
| 149 | // should be injected too. |
| 150 | socket_factory = network_thread()->socketserver(); |
| 151 | } |
| 152 | } |
| 153 | if (!default_network_manager_) { |
| 154 | // If network_monitor_factory_ is non-null, it will be used to create a |
| 155 | // network monitor while on the network thread. |
| 156 | default_network_manager_ = std::make_unique<rtc::BasicNetworkManager>( |
| 157 | network_monitor_factory_.get(), socket_factory, &env_.field_trials()); |
| 158 | } |
| 159 | if (!default_socket_factory_) { |
| 160 | default_socket_factory_ = |
| 161 | std::make_unique<rtc::BasicPacketSocketFactory>(socket_factory); |
| 162 | } |
| 163 | // Set warning levels on the threads, to give warnings when response |
| 164 | // may be slower than is expected of the thread. |
| 165 | // Since some of the threads may be the same, start with the least |
| 166 | // restrictive limits and end with the least permissive ones. |
| 167 | // This will give warnings for all cases. |
| 168 | signaling_thread_->SetDispatchWarningMs(100); |
| 169 | worker_thread_->SetDispatchWarningMs(30); |
| 170 | network_thread_->SetDispatchWarningMs(10); |
| 171 |
| 172 | if (media_engine_) { |
| 173 | // TODO(tommi): Change VoiceEngine to do ctor time initialization so that |
| 174 | // this isn't necessary. |
| 175 | worker_thread_->BlockingCall([&] { media_engine_->Init(); }); |
| 176 | } |
| 177 | } |
| 178 |
| 179 | ConnectionContext::~ConnectionContext() { |
| 180 | RTC_DCHECK_RUN_ON(signaling_thread_); |
| 181 | // `media_engine_` requires destruction to happen on the worker thread. |
| 182 | worker_thread_->PostTask([media_engine = std::move(media_engine_)] {}); |
| 183 |
| 184 | // Make sure `worker_thread()` and `signaling_thread()` outlive |
| 185 | // `default_socket_factory_` and `default_network_manager_`. |
| 186 | default_socket_factory_ = nullptr; |
| 187 | default_network_manager_ = nullptr; |
| 188 |
| 189 | if (wraps_current_thread_) |
| 190 | rtc::ThreadManager::Instance()->UnwrapCurrentThread(); |
| 191 | } |
| 192 |
| 193 | } // namespace webrtc |
| 194 |