diff --git a/api/create_peerconnection_factory.cc b/api/create_peerconnection_factory.cc index 008fce3e80..c41b6d6fb2 100644 --- a/api/create_peerconnection_factory.cc +++ b/api/create_peerconnection_factory.cc @@ -49,6 +49,10 @@ rtc::scoped_refptr CreatePeerConnectionFactory( dependencies.task_queue_factory.get()); dependencies.trials = std::make_unique(); + if (network_thread) { + // TODO(bugs.webrtc.org/13145): Add an rtc::SocketFactory* argument. + dependencies.socket_factory = network_thread->socketserver(); + } cricket::MediaEngineDependencies media_dependencies; media_dependencies.task_queue_factory = dependencies.task_queue_factory.get(); media_dependencies.adm = std::move(default_adm); diff --git a/api/peer_connection_interface.h b/api/peer_connection_interface.h index ed801a7771..1a6e782f0e 100644 --- a/api/peer_connection_interface.h +++ b/api/peer_connection_interface.h @@ -1421,6 +1421,7 @@ struct RTC_EXPORT PeerConnectionFactoryDependencies final { rtc::Thread* network_thread = nullptr; rtc::Thread* worker_thread = nullptr; rtc::Thread* signaling_thread = nullptr; + rtc::SocketFactory* socket_factory = nullptr; std::unique_ptr task_queue_factory; std::unique_ptr media_engine; std::unique_ptr call_factory; diff --git a/pc/connection_context.cc b/pc/connection_context.cc index 558702a72e..6e531339f4 100644 --- a/pc/connection_context.cc +++ b/pc/connection_context.cc @@ -17,6 +17,7 @@ #include "api/transport/field_trial_based_config.h" #include "media/sctp/sctp_transport_factory.h" #include "rtc_base/helpers.h" +#include "rtc_base/internal/default_socket_server.h" #include "rtc_base/task_utils/to_queued_task.h" #include "rtc_base/time_utils.h" @@ -24,19 +25,31 @@ namespace webrtc { namespace { -rtc::Thread* MaybeStartThread(rtc::Thread* old_thread, - const std::string& thread_name, - bool with_socket_server, - std::unique_ptr& thread_holder) { +rtc::Thread* MaybeStartNetworkThread( + rtc::Thread* old_thread, + std::unique_ptr& socket_factory_holder, + std::unique_ptr& thread_holder) { if (old_thread) { return old_thread; } - if (with_socket_server) { - thread_holder = rtc::Thread::CreateWithSocketServer(); - } else { - thread_holder = rtc::Thread::Create(); + std::unique_ptr socket_server = + rtc::CreateDefaultSocketServer(); + thread_holder = std::make_unique(socket_server.get()); + socket_factory_holder = std::move(socket_server); + + thread_holder->SetName("pc_network_thread", nullptr); + thread_holder->Start(); + return thread_holder.get(); +} + +rtc::Thread* MaybeStartWorkerThread( + rtc::Thread* old_thread, + std::unique_ptr& thread_holder) { + if (old_thread) { + return old_thread; } - thread_holder->SetName(thread_name, nullptr); + thread_holder = rtc::Thread::Create(); + thread_holder->SetName("pc_worker_thread", nullptr); thread_holder->Start(); return thread_holder.get(); } @@ -81,14 +94,11 @@ rtc::scoped_refptr ConnectionContext::Create( ConnectionContext::ConnectionContext( PeerConnectionFactoryDependencies* dependencies) - : network_thread_(MaybeStartThread(dependencies->network_thread, - "pc_network_thread", - true, - owned_network_thread_)), - worker_thread_(MaybeStartThread(dependencies->worker_thread, - "pc_worker_thread", - false, - owned_worker_thread_)), + : network_thread_(MaybeStartNetworkThread(dependencies->network_thread, + owned_socket_factory_, + owned_network_thread_)), + worker_thread_(MaybeStartWorkerThread(dependencies->worker_thread, + owned_worker_thread_)), signaling_thread_(MaybeWrapThread(dependencies->signaling_thread, wraps_current_thread_)), network_monitor_factory_( @@ -117,17 +127,26 @@ ConnectionContext::ConnectionContext( RTC_DCHECK_RUN_ON(signaling_thread_); rtc::InitRandom(rtc::Time32()); + rtc::SocketFactory* socket_factory = dependencies->socket_factory; + if (socket_factory == nullptr) { + if (owned_socket_factory_) { + socket_factory = owned_socket_factory_.get(); + } else { + // TODO(bugs.webrtc.org/13145): This case should be deleted. Either + // require that a PacketSocketFactory and NetworkManager always are + // injected (with no need to construct these default objects), or require + // that if a network_thread is injected, an approprite rtc::SocketServer + // should be injected too. + socket_factory = network_thread()->socketserver(); + } + } // If network_monitor_factory_ is non-null, it will be used to create a // network monitor while on the network thread. default_network_manager_ = std::make_unique( - network_monitor_factory_.get(), network_thread()->socketserver()); + network_monitor_factory_.get(), socket_factory); - // TODO(bugs.webrtc.org/13145): Either require that a PacketSocketFactory - // always is injected (with no need to construct this default factory), or get - // the appropriate underlying SocketFactory without going through the - // rtc::Thread::socketserver() accessor. - default_socket_factory_ = std::make_unique( - network_thread()->socketserver()); + default_socket_factory_ = + std::make_unique(socket_factory); channel_manager_ = cricket::ChannelManager::Create( std::move(dependencies->media_engine), diff --git a/pc/connection_context.h b/pc/connection_context.h index 8fad13c10c..5e814079f9 100644 --- a/pc/connection_context.h +++ b/pc/connection_context.h @@ -103,6 +103,7 @@ class ConnectionContext final // Note: Since owned_network_thread_ and owned_worker_thread_ are used // in the initialization of network_thread_ and worker_thread_, they // must be declared before them, so that they are initialized first. + std::unique_ptr owned_socket_factory_; std::unique_ptr owned_network_thread_ RTC_GUARDED_BY(signaling_thread_); std::unique_ptr owned_worker_thread_