Add an rtc::SocketFactory* member to PeerConnectionFactoryDependencies

Bug: webrtc:13145
Change-Id: I0267013fdda42e09dc23551a73a6151e0fb9b72e
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/249950
Reviewed-by: Harald Alvestrand <hta@webrtc.org>
Commit-Queue: Niels Moller <nisse@webrtc.org>
Cr-Commit-Position: refs/heads/main@{#35909}
diff --git a/api/create_peerconnection_factory.cc b/api/create_peerconnection_factory.cc
index 008fce3..c41b6d6 100644
--- a/api/create_peerconnection_factory.cc
+++ b/api/create_peerconnection_factory.cc
@@ -49,6 +49,10 @@
       dependencies.task_queue_factory.get());
   dependencies.trials = std::make_unique<webrtc::FieldTrialBasedConfig>();
 
+  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 ed801a7..1a6e782 100644
--- a/api/peer_connection_interface.h
+++ b/api/peer_connection_interface.h
@@ -1421,6 +1421,7 @@
   rtc::Thread* network_thread = nullptr;
   rtc::Thread* worker_thread = nullptr;
   rtc::Thread* signaling_thread = nullptr;
+  rtc::SocketFactory* socket_factory = nullptr;
   std::unique_ptr<TaskQueueFactory> task_queue_factory;
   std::unique_ptr<cricket::MediaEngineInterface> media_engine;
   std::unique_ptr<CallFactoryInterface> call_factory;
diff --git a/pc/connection_context.cc b/pc/connection_context.cc
index 558702a..6e53133 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 {
 
-rtc::Thread* MaybeStartThread(rtc::Thread* old_thread,
-                              const std::string& thread_name,
-                              bool with_socket_server,
-                              std::unique_ptr<rtc::Thread>& thread_holder) {
+rtc::Thread* MaybeStartNetworkThread(
+    rtc::Thread* old_thread,
+    std::unique_ptr<rtc::SocketFactory>& socket_factory_holder,
+    std::unique_ptr<rtc::Thread>& 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<rtc::SocketServer> socket_server =
+      rtc::CreateDefaultSocketServer();
+  thread_holder = std::make_unique<rtc::Thread>(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<rtc::Thread>& 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 @@
 
 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 @@
   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<rtc::BasicNetworkManager>(
-      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<rtc::BasicPacketSocketFactory>(
-      network_thread()->socketserver());
+  default_socket_factory_ =
+      std::make_unique<rtc::BasicPacketSocketFactory>(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 8fad13c..5e81407 100644
--- a/pc/connection_context.h
+++ b/pc/connection_context.h
@@ -103,6 +103,7 @@
   // 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<rtc::SocketFactory> owned_socket_factory_;
   std::unique_ptr<rtc::Thread> owned_network_thread_
       RTC_GUARDED_BY(signaling_thread_);
   std::unique_ptr<rtc::Thread> owned_worker_thread_