Add rtc:SocketFactory as member of jni OwnedFactoryAndThreads

Bug: webrtc:13145
Change-Id: Iff1b59d291b1a36d474cf3fb6fafa4e9ff007aa0
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/232060
Reviewed-by: Jonas Oreland <jonaso@webrtc.org>
Reviewed-by: Xavier Lepaul‎ <xalep@webrtc.org>
Commit-Queue: Niels Moller <nisse@webrtc.org>
Cr-Commit-Position: refs/heads/main@{#35051}
diff --git a/sdk/android/BUILD.gn b/sdk/android/BUILD.gn
index 2e20822..20bee2b 100644
--- a/sdk/android/BUILD.gn
+++ b/sdk/android/BUILD.gn
@@ -1569,6 +1569,7 @@
       "../../rtc_base:checks",
       "../../rtc_base:ip_address",
       "../../rtc_base:rtc_base",
+      "../../rtc_base:threading",
       "../../rtc_base/synchronization:mutex",
       "../../rtc_base/system:inline",
       "../../system_wrappers",
diff --git a/sdk/android/native_api/peerconnection/peer_connection_factory.cc b/sdk/android/native_api/peerconnection/peer_connection_factory.cc
index 4e742d1..87ab991 100644
--- a/sdk/android/native_api/peerconnection/peer_connection_factory.cc
+++ b/sdk/android/native_api/peerconnection/peer_connection_factory.cc
@@ -21,12 +21,13 @@
 jobject NativeToJavaPeerConnectionFactory(
     JNIEnv* jni,
     rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> pcf,
+    std::unique_ptr<rtc::SocketFactory> socket_factory,
     std::unique_ptr<rtc::Thread> network_thread,
     std::unique_ptr<rtc::Thread> worker_thread,
     std::unique_ptr<rtc::Thread> signaling_thread) {
   return webrtc::jni::NativeToJavaPeerConnectionFactory(
-      jni, pcf, std::move(network_thread), std::move(worker_thread),
-      std::move(signaling_thread));
+      jni, pcf, std::move(socket_factory), std::move(network_thread),
+      std::move(worker_thread), std::move(signaling_thread));
 }
 
 }  // namespace webrtc
diff --git a/sdk/android/native_api/peerconnection/peer_connection_factory.h b/sdk/android/native_api/peerconnection/peer_connection_factory.h
index 6f046c5..959eb79 100644
--- a/sdk/android/native_api/peerconnection/peer_connection_factory.h
+++ b/sdk/android/native_api/peerconnection/peer_connection_factory.h
@@ -24,6 +24,7 @@
 jobject NativeToJavaPeerConnectionFactory(
     JNIEnv* jni,
     rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> pcf,
+    std::unique_ptr<rtc::SocketFactory> socket_factory,
     std::unique_ptr<rtc::Thread> network_thread,
     std::unique_ptr<rtc::Thread> worker_thread,
     std::unique_ptr<rtc::Thread> signaling_thread);
diff --git a/sdk/android/native_unittests/peerconnection/peer_connection_factory_unittest.cc b/sdk/android/native_unittests/peerconnection/peer_connection_factory_unittest.cc
index 75535d0..4cd62bc 100644
--- a/sdk/android/native_unittests/peerconnection/peer_connection_factory_unittest.cc
+++ b/sdk/android/native_unittests/peerconnection/peer_connection_factory_unittest.cc
@@ -19,6 +19,8 @@
 #include "media/engine/webrtc_media_engine.h"
 #include "media/engine/webrtc_media_engine_defaults.h"
 #include "rtc_base/logging.h"
+#include "rtc_base/physical_socket_server.h"
+#include "rtc_base/thread.h"
 #include "sdk/android/generated_native_unittests_jni/PeerConnectionFactoryInitializationHelper_jni.h"
 #include "sdk/android/native_api/audio_device_module/audio_device_android.h"
 #include "sdk/android/native_api/jni/jvm.h"
@@ -80,9 +82,10 @@
       jni);
   RTC_LOG(INFO) << "Java peer connection factory initialized.";
 
+  auto socket_server = std::make_unique<rtc::PhysicalSocketServer>();
+
   // Create threads.
-  std::unique_ptr<rtc::Thread> network_thread =
-      rtc::Thread::CreateWithSocketServer();
+  auto network_thread = std::make_unique<rtc::Thread>(socket_server.get());
   network_thread->SetName("network_thread", nullptr);
   RTC_CHECK(network_thread->Start()) << "Failed to start thread";
 
@@ -99,8 +102,8 @@
                     signaling_thread.get());
 
   jobject java_factory = NativeToJavaPeerConnectionFactory(
-      jni, factory, std::move(network_thread), std::move(worker_thread),
-      std::move(signaling_thread));
+      jni, factory, std::move(socket_server), std::move(network_thread),
+      std::move(worker_thread), std::move(signaling_thread));
 
   RTC_LOG(INFO) << java_factory;
 
diff --git a/sdk/android/src/jni/pc/owned_factory_and_threads.cc b/sdk/android/src/jni/pc/owned_factory_and_threads.cc
index 5e00ece..d595c48 100644
--- a/sdk/android/src/jni/pc/owned_factory_and_threads.cc
+++ b/sdk/android/src/jni/pc/owned_factory_and_threads.cc
@@ -16,11 +16,13 @@
 namespace jni {
 
 OwnedFactoryAndThreads::OwnedFactoryAndThreads(
+    std::unique_ptr<rtc::SocketFactory> socket_factory,
     std::unique_ptr<rtc::Thread> network_thread,
     std::unique_ptr<rtc::Thread> worker_thread,
     std::unique_ptr<rtc::Thread> signaling_thread,
     const rtc::scoped_refptr<PeerConnectionFactoryInterface>& factory)
-    : network_thread_(std::move(network_thread)),
+    : socket_factory_(std::move(socket_factory)),
+      network_thread_(std::move(network_thread)),
       worker_thread_(std::move(worker_thread)),
       signaling_thread_(std::move(signaling_thread)),
       factory_(factory) {}
diff --git a/sdk/android/src/jni/pc/owned_factory_and_threads.h b/sdk/android/src/jni/pc/owned_factory_and_threads.h
index e87879c..7dc9443 100644
--- a/sdk/android/src/jni/pc/owned_factory_and_threads.h
+++ b/sdk/android/src/jni/pc/owned_factory_and_threads.h
@@ -30,6 +30,7 @@
 class OwnedFactoryAndThreads {
  public:
   OwnedFactoryAndThreads(
+      std::unique_ptr<rtc::SocketFactory> socket_factory,
       std::unique_ptr<rtc::Thread> network_thread,
       std::unique_ptr<rtc::Thread> worker_thread,
       std::unique_ptr<rtc::Thread> signaling_thread,
@@ -38,11 +39,15 @@
   ~OwnedFactoryAndThreads() = default;
 
   PeerConnectionFactoryInterface* factory() { return factory_.get(); }
+  rtc::SocketFactory* socket_factory() { return socket_factory_.get(); }
   rtc::Thread* network_thread() { return network_thread_.get(); }
   rtc::Thread* signaling_thread() { return signaling_thread_.get(); }
   rtc::Thread* worker_thread() { return worker_thread_.get(); }
 
  private:
+  // Usually implemented by the SocketServer associated with the network thread,
+  // so needs to outlive the network thread.
+  const std::unique_ptr<rtc::SocketFactory> socket_factory_;
   const std::unique_ptr<rtc::Thread> network_thread_;
   const std::unique_ptr<rtc::Thread> worker_thread_;
   const std::unique_ptr<rtc::Thread> signaling_thread_;
diff --git a/sdk/android/src/jni/pc/peer_connection_factory.cc b/sdk/android/src/jni/pc/peer_connection_factory.cc
index a12d5c1..5330cbd 100644
--- a/sdk/android/src/jni/pc/peer_connection_factory.cc
+++ b/sdk/android/src/jni/pc/peer_connection_factory.cc
@@ -30,6 +30,7 @@
 #include "modules/audio_device/include/audio_device.h"
 #include "modules/audio_processing/include/audio_processing.h"
 #include "rtc_base/event_tracer.h"
+#include "rtc_base/physical_socket_server.h"
 #include "rtc_base/system/thread_registry.h"
 #include "rtc_base/thread.h"
 #include "sdk/android/generated_peerconnection_jni/PeerConnectionFactory_jni.h"
@@ -136,12 +137,13 @@
 ScopedJavaLocalRef<jobject> NativeToScopedJavaPeerConnectionFactory(
     JNIEnv* env,
     rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> pcf,
+    std::unique_ptr<rtc::SocketFactory> socket_factory,
     std::unique_ptr<rtc::Thread> network_thread,
     std::unique_ptr<rtc::Thread> worker_thread,
     std::unique_ptr<rtc::Thread> signaling_thread) {
   OwnedFactoryAndThreads* owned_factory = new OwnedFactoryAndThreads(
-      std::move(network_thread), std::move(worker_thread),
-      std::move(signaling_thread), pcf);
+      std::move(socket_factory), std::move(network_thread),
+      std::move(worker_thread), std::move(signaling_thread), pcf);
 
   ScopedJavaLocalRef<jobject> j_pcf = Java_PeerConnectionFactory_Constructor(
       env, NativeToJavaPointer(owned_factory));
@@ -174,12 +176,13 @@
 jobject NativeToJavaPeerConnectionFactory(
     JNIEnv* jni,
     rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> pcf,
+    std::unique_ptr<rtc::SocketFactory> socket_factory,
     std::unique_ptr<rtc::Thread> network_thread,
     std::unique_ptr<rtc::Thread> worker_thread,
     std::unique_ptr<rtc::Thread> signaling_thread) {
   return NativeToScopedJavaPeerConnectionFactory(
-             jni, pcf, std::move(network_thread), std::move(worker_thread),
-             std::move(signaling_thread))
+             jni, pcf, std::move(socket_factory), std::move(network_thread),
+             std::move(worker_thread), std::move(signaling_thread))
       .Release();
 }
 
@@ -268,8 +271,8 @@
   // think about ramifications of auto-wrapping there.
   rtc::ThreadManager::Instance()->WrapCurrentThread();
 
-  std::unique_ptr<rtc::Thread> network_thread =
-      rtc::Thread::CreateWithSocketServer();
+  auto socket_server = std::make_unique<rtc::PhysicalSocketServer>();
+  auto network_thread = std::make_unique<rtc::Thread>(socket_server.get());
   network_thread->SetName("network_thread", nullptr);
   RTC_CHECK(network_thread->Start()) << "Failed to start thread";
 
@@ -285,6 +288,8 @@
       JavaToNativePeerConnectionFactoryOptions(jni, joptions);
 
   PeerConnectionFactoryDependencies dependencies;
+  // TODO(bugs.webrtc.org/13145): Also add socket_server.get() to the
+  // dependencies.
   dependencies.network_thread = network_thread.get();
   dependencies.worker_thread = worker_thread.get();
   dependencies.signaling_thread = signaling_thread.get();
@@ -327,8 +332,8 @@
     factory->SetOptions(*options);
 
   return NativeToScopedJavaPeerConnectionFactory(
-      jni, factory, std::move(network_thread), std::move(worker_thread),
-      std::move(signaling_thread));
+      jni, factory, std::move(socket_server), std::move(network_thread),
+      std::move(worker_thread), std::move(signaling_thread));
 }
 
 static ScopedJavaLocalRef<jobject>
diff --git a/sdk/android/src/jni/pc/peer_connection_factory.h b/sdk/android/src/jni/pc/peer_connection_factory.h
index 33cb978..b5d5e5d 100644
--- a/sdk/android/src/jni/pc/peer_connection_factory.h
+++ b/sdk/android/src/jni/pc/peer_connection_factory.h
@@ -22,6 +22,7 @@
 jobject NativeToJavaPeerConnectionFactory(
     JNIEnv* jni,
     rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> pcf,
+    std::unique_ptr<rtc::SocketFactory> socket_factory,
     std::unique_ptr<rtc::Thread> network_thread,
     std::unique_ptr<rtc::Thread> worker_thread,
     std::unique_ptr<rtc::Thread> signaling_thread);