Let EmulatedNetworkManagerInterface own and expose a PacketSocketFactory

So that applications don't need to construct it from the exposed
network_thread.

The EmulatedNetworkManagerInterface::network_thread() accessor is currently
used as a way to get to emulation's SocketServer, and should be deleted
when applications of the emulation framework have migrated away from
that usage.

Bug: webrtc:13145
Change-Id: I3efa55d117cad8ac601c48a9d2d2aa62a121f9c9
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/231649
Reviewed-by: Artem Titov <titovartem@webrtc.org>
Reviewed-by: Harald Alvestrand <hta@webrtc.org>
Commit-Queue: Niels Moller <nisse@webrtc.org>
Cr-Commit-Position: refs/heads/main@{#34964}
diff --git a/api/BUILD.gn b/api/BUILD.gn
index 89f2b15..376c83f 100644
--- a/api/BUILD.gn
+++ b/api/BUILD.gn
@@ -623,6 +623,7 @@
   ]
   deps = [
     ":array_view",
+    ":packet_socket_factory",
     ":simulated_network_api",
     ":time_controller",
     "../call:simulated_network",
diff --git a/api/test/network_emulation_manager.h b/api/test/network_emulation_manager.h
index 9fe4ad5..61973eb 100644
--- a/api/test/network_emulation_manager.h
+++ b/api/test/network_emulation_manager.h
@@ -17,6 +17,7 @@
 #include <vector>
 
 #include "api/array_view.h"
+#include "api/packet_socket_factory.h"
 #include "api/test/network_emulation/cross_traffic.h"
 #include "api/test/network_emulation/network_emulation_interfaces.h"
 #include "api/test/simulated_network.h"
@@ -125,6 +126,10 @@
   // WebRTC to properly setup network emulation. Returned manager is owned by
   // EmulatedNetworkManagerInterface implementation.
   virtual rtc::NetworkManager* network_manager() = 0;
+  // Returns non-null pointer to packet socket factory that have to be injected
+  // into WebRTC to properly setup network emulation. Returned factory is owned
+  // by EmulatedNetworkManagerInterface implementation.
+  virtual rtc::PacketSocketFactory* packet_socket_factory() = 0;
   // Returns list of endpoints that are associated with this instance. Pointers
   // are guaranteed to be non-null and are owned by NetworkEmulationManager.
   virtual std::vector<EmulatedEndpoint*> endpoints() const = 0;
diff --git a/test/DEPS b/test/DEPS
index 0e51f00..2d45003 100644
--- a/test/DEPS
+++ b/test/DEPS
@@ -18,6 +18,7 @@
   "+modules/utility",
   "+modules/video_capture",
   "+modules/video_coding",
+  "+p2p/base/basic_packet_socket_factory.h",
   "+sdk",
   "+system_wrappers",
   "+third_party/libyuv",
diff --git a/test/network/BUILD.gn b/test/network/BUILD.gn
index 3836cc1..fb0bc55 100644
--- a/test/network/BUILD.gn
+++ b/test/network/BUILD.gn
@@ -54,6 +54,7 @@
     "../../api/units:timestamp",
     "../../call:simulated_network",
     "../../p2p:p2p_server_utils",
+    "../../p2p:rtc_p2p",
     "../../rtc_base",
     "../../rtc_base:ip_address",
     "../../rtc_base:network_constants",
diff --git a/test/network/emulated_network_manager.cc b/test/network/emulated_network_manager.cc
index ec8b2b3..7b43a79 100644
--- a/test/network/emulated_network_manager.cc
+++ b/test/network/emulated_network_manager.cc
@@ -14,6 +14,7 @@
 #include <utility>
 
 #include "absl/memory/memory.h"
+#include "p2p/base/basic_packet_socket_factory.h"
 #include "test/network/fake_network_socket_server.h"
 
 namespace webrtc {
@@ -25,11 +26,17 @@
     EndpointsContainer* endpoints_container)
     : task_queue_(task_queue),
       endpoints_container_(endpoints_container),
-      network_thread_(time_controller->CreateThread(
-          "net_thread",
-          std::make_unique<FakeNetworkSocketServer>(endpoints_container))),
       sent_first_update_(false),
-      start_count_(0) {}
+      start_count_(0) {
+  auto socket_server =
+      std::make_unique<FakeNetworkSocketServer>(endpoints_container);
+  packet_socket_factory_ =
+      std::make_unique<rtc::BasicPacketSocketFactory>(socket_server.get());
+  // Since we pass ownership of the socket server to `network_thread_`, we must
+  // arrange that it outlives `packet_socket_factory_` which refers to it.
+  network_thread_ =
+      time_controller->CreateThread("net_thread", std::move(socket_server));
+}
 
 void EmulatedNetworkManager::EnableEndpoint(EmulatedEndpointImpl* endpoint) {
   RTC_CHECK(endpoints_container_->HasEndpoint(endpoint))
diff --git a/test/network/emulated_network_manager.h b/test/network/emulated_network_manager.h
index fd2bb5b..4c679c3 100644
--- a/test/network/emulated_network_manager.h
+++ b/test/network/emulated_network_manager.h
@@ -50,6 +50,9 @@
   // EmulatedNetworkManagerInterface API
   rtc::Thread* network_thread() override { return network_thread_.get(); }
   rtc::NetworkManager* network_manager() override { return this; }
+  rtc::PacketSocketFactory* packet_socket_factory() override {
+    return packet_socket_factory_.get();
+  }
   std::vector<EmulatedEndpoint*> endpoints() const override {
     return endpoints_container_->GetEndpoints();
   }
@@ -62,8 +65,12 @@
 
   TaskQueueForTest* const task_queue_;
   const EndpointsContainer* const endpoints_container_;
+  // The `network_thread_` must outlive `packet_socket_factory_`, because they
+  // both refer to a socket server that is owned by `network_thread_`. Both
+  // pointers are assigned only in the constructor, but the way they are
+  // initialized unfortunately doesn't work with const std::unique_ptr<...>.
   std::unique_ptr<rtc::Thread> network_thread_;
-
+  std::unique_ptr<rtc::PacketSocketFactory> packet_socket_factory_;
   bool sent_first_update_ RTC_GUARDED_BY(network_thread_);
   int start_count_ RTC_GUARDED_BY(network_thread_);
 };