Propogate network-worker thread split to api

BUG=webrtc:5645

Review-Url: https://codereview.webrtc.org/1968393002
Cr-Commit-Position: refs/heads/master@{#12767}
diff --git a/webrtc/api/java/jni/peerconnection_jni.cc b/webrtc/api/java/jni/peerconnection_jni.cc
index 6c5ba97..165d6da 100644
--- a/webrtc/api/java/jni/peerconnection_jni.cc
+++ b/webrtc/api/java/jni/peerconnection_jni.cc
@@ -1038,14 +1038,16 @@
 // single thing for Java to hold and eventually free.
 class OwnedFactoryAndThreads {
  public:
-  OwnedFactoryAndThreads(Thread* worker_thread,
-                         Thread* signaling_thread,
+  OwnedFactoryAndThreads(std::unique_ptr<Thread> network_thread,
+                         std::unique_ptr<Thread> worker_thread,
+                         std::unique_ptr<Thread> signaling_thread,
                          WebRtcVideoEncoderFactory* encoder_factory,
                          WebRtcVideoDecoderFactory* decoder_factory,
                          rtc::NetworkMonitorFactory* network_monitor_factory,
                          PeerConnectionFactoryInterface* factory)
-      : worker_thread_(worker_thread),
-        signaling_thread_(signaling_thread),
+      : network_thread_(std::move(network_thread)),
+        worker_thread_(std::move(worker_thread)),
+        signaling_thread_(std::move(signaling_thread)),
         encoder_factory_(encoder_factory),
         decoder_factory_(decoder_factory),
         network_monitor_factory_(network_monitor_factory),
@@ -1070,6 +1072,7 @@
  private:
   void JavaCallbackOnFactoryThreads();
 
+  const std::unique_ptr<Thread> network_thread_;
   const std::unique_ptr<Thread> worker_thread_;
   const std::unique_ptr<Thread> signaling_thread_;
   WebRtcVideoEncoderFactory* encoder_factory_;
@@ -1083,11 +1086,15 @@
   ScopedLocalRefFrame local_ref_frame(jni);
   jclass j_factory_class = FindClass(jni, "org/webrtc/PeerConnectionFactory");
   jmethodID m = nullptr;
-  if (Thread::Current() == worker_thread_.get()) {
+  if (network_thread_->IsCurrent()) {
+    LOG(LS_INFO) << "Network thread JavaCallback";
+    m = GetStaticMethodID(jni, j_factory_class, "onNetworkThreadReady", "()V");
+  }
+  if (worker_thread_->IsCurrent()) {
     LOG(LS_INFO) << "Worker thread JavaCallback";
     m = GetStaticMethodID(jni, j_factory_class, "onWorkerThreadReady", "()V");
   }
-  if (Thread::Current() == signaling_thread_.get()) {
+  if (signaling_thread_->IsCurrent()) {
     LOG(LS_INFO) << "Signaling thread JavaCallback";
     m = GetStaticMethodID(
         jni, j_factory_class, "onSignalingThreadReady", "()V");
@@ -1100,10 +1107,9 @@
 
 void OwnedFactoryAndThreads::InvokeJavaCallbacksOnFactoryThreads() {
   LOG(LS_INFO) << "InvokeJavaCallbacksOnFactoryThreads.";
-  worker_thread_->Invoke<void>(
-      Bind(&OwnedFactoryAndThreads::JavaCallbackOnFactoryThreads, this));
-  signaling_thread_->Invoke<void>(
-      Bind(&OwnedFactoryAndThreads::JavaCallbackOnFactoryThreads, this));
+  network_thread_->Invoke<void>([this] { JavaCallbackOnFactoryThreads(); });
+  worker_thread_->Invoke<void>([this] { JavaCallbackOnFactoryThreads(); });
+  signaling_thread_->Invoke<void>([this] { JavaCallbackOnFactoryThreads(); });
 }
 
 PeerConnectionFactoryInterface::Options ParseOptionsFromJava(JNIEnv* jni,
@@ -1143,12 +1149,20 @@
   // about ramifications of auto-wrapping there.
   rtc::ThreadManager::Instance()->WrapCurrentThread();
   webrtc::Trace::CreateTrace();
-  Thread* worker_thread = new Thread();
-  worker_thread->SetName("worker_thread", NULL);
-  Thread* signaling_thread = new Thread();
+
+  std::unique_ptr<Thread> network_thread =
+      rtc::Thread::CreateWithSocketServer();
+  network_thread->SetName("network_thread", nullptr);
+  RTC_CHECK(network_thread->Start()) << "Failed to start thread";
+
+  std::unique_ptr<Thread> worker_thread = rtc::Thread::Create();
+  worker_thread->SetName("worker_thread", nullptr);
+  RTC_CHECK(worker_thread->Start()) << "Failed to start thread";
+
+  std::unique_ptr<Thread> signaling_thread = rtc::Thread::Create();
   signaling_thread->SetName("signaling_thread", NULL);
-  RTC_CHECK(worker_thread->Start() && signaling_thread->Start())
-      << "Failed to start threads";
+  RTC_CHECK(signaling_thread->Start()) << "Failed to start thread";
+
   WebRtcVideoEncoderFactory* encoder_factory = nullptr;
   WebRtcVideoDecoderFactory* decoder_factory = nullptr;
   rtc::NetworkMonitorFactory* network_monitor_factory = nullptr;
@@ -1171,11 +1185,9 @@
   }
 
   rtc::scoped_refptr<PeerConnectionFactoryInterface> factory(
-      webrtc::CreatePeerConnectionFactory(worker_thread,
-                                          signaling_thread,
-                                          NULL,
-                                          encoder_factory,
-                                          decoder_factory));
+      webrtc::CreatePeerConnectionFactory(
+          network_thread.get(), worker_thread.get(), signaling_thread.get(),
+          nullptr, encoder_factory, decoder_factory));
   RTC_CHECK(factory) << "Failed to create the peer connection factory; "
                      << "WebRTC/libjingle init likely failed on this device";
   // TODO(honghaiz): Maybe put the options as the argument of
@@ -1184,8 +1196,8 @@
     factory->SetOptions(options);
   }
   OwnedFactoryAndThreads* owned_factory = new OwnedFactoryAndThreads(
-      worker_thread, signaling_thread,
-      encoder_factory, decoder_factory,
+      std::move(network_thread), std::move(worker_thread),
+      std::move(signaling_thread), encoder_factory, decoder_factory,
       network_monitor_factory, factory.release());
   owned_factory->InvokeJavaCallbacksOnFactoryThreads();
   return jlongFromPointer(owned_factory);
diff --git a/webrtc/api/java/src/org/webrtc/PeerConnectionFactory.java b/webrtc/api/java/src/org/webrtc/PeerConnectionFactory.java
index fef56ba..0c1ef3c 100644
--- a/webrtc/api/java/src/org/webrtc/PeerConnectionFactory.java
+++ b/webrtc/api/java/src/org/webrtc/PeerConnectionFactory.java
@@ -24,6 +24,7 @@
 
   private static final String TAG = "PeerConnectionFactory";
   private final long nativeFactory;
+  private static Thread networkThread;
   private static Thread workerThread;
   private static Thread signalingThread;
   private EglBase localEglbase;
@@ -198,8 +199,9 @@
 
   public void dispose() {
     nativeFreeFactory(nativeFactory);
-    signalingThread = null;
+    networkThread = null;
     workerThread = null;
+    signalingThread = null;
     if (localEglbase != null)
       localEglbase.release();
     if (remoteEglbase != null)
@@ -223,10 +225,16 @@
   }
 
   public static void printStackTraces() {
+    printStackTrace(networkThread, "Network thread");
     printStackTrace(workerThread, "Worker thread");
     printStackTrace(signalingThread, "Signaling thread");
   }
 
+  private static void onNetworkThreadReady() {
+    networkThread = Thread.currentThread();
+    Logging.d(TAG, "onNetworkThreadReady");
+  }
+
   private static void onWorkerThreadReady() {
     workerThread = Thread.currentThread();
     Logging.d(TAG, "onWorkerThreadReady");
diff --git a/webrtc/api/peerconnection.cc b/webrtc/api/peerconnection.cc
index 5284db3..dda0eeb 100644
--- a/webrtc/api/peerconnection.cc
+++ b/webrtc/api/peerconnection.cc
@@ -588,8 +588,9 @@
       factory_->CreateMediaController(configuration.media_config));
 
   session_.reset(
-      new WebRtcSession(media_controller_.get(), factory_->signaling_thread(),
-                        factory_->worker_thread(), port_allocator_.get()));
+      new WebRtcSession(media_controller_.get(), factory_->network_thread(),
+                        factory_->worker_thread(), factory_->signaling_thread(),
+                        port_allocator_.get()));
   stats_.reset(new StatsCollector(this));
 
   // Initialize the WebRtcSession. It creates transport channels etc.
diff --git a/webrtc/api/peerconnection_unittest.cc b/webrtc/api/peerconnection_unittest.cc
index 1c3d073..24411ab 100644
--- a/webrtc/api/peerconnection_unittest.cc
+++ b/webrtc/api/peerconnection_unittest.cc
@@ -156,10 +156,11 @@
       const PeerConnectionFactory::Options* options,
       std::unique_ptr<webrtc::DtlsIdentityStoreInterface> dtls_identity_store,
       bool prefer_constraint_apis,
+      rtc::Thread* network_thread,
       rtc::Thread* worker_thread) {
     PeerConnectionTestClient* client(new PeerConnectionTestClient(id));
     if (!client->Init(constraints, options, std::move(dtls_identity_store),
-                      prefer_constraint_apis, worker_thread)) {
+                      prefer_constraint_apis, network_thread, worker_thread)) {
       delete client;
       return nullptr;
     }
@@ -170,27 +171,29 @@
       const std::string& id,
       const MediaConstraintsInterface* constraints,
       const PeerConnectionFactory::Options* options,
+      rtc::Thread* network_thread,
       rtc::Thread* worker_thread) {
     std::unique_ptr<FakeDtlsIdentityStore> dtls_identity_store(
         rtc::SSLStreamAdapter::HaveDtlsSrtp() ? new FakeDtlsIdentityStore()
                                               : nullptr);
 
-    return CreateClientWithDtlsIdentityStore(id, constraints, options,
-                                             std::move(dtls_identity_store),
-                                             true, worker_thread);
+    return CreateClientWithDtlsIdentityStore(
+        id, constraints, options, std::move(dtls_identity_store), true,
+        network_thread, worker_thread);
   }
 
   static PeerConnectionTestClient* CreateClientPreferNoConstraints(
       const std::string& id,
       const PeerConnectionFactory::Options* options,
+      rtc::Thread* network_thread,
       rtc::Thread* worker_thread) {
     std::unique_ptr<FakeDtlsIdentityStore> dtls_identity_store(
         rtc::SSLStreamAdapter::HaveDtlsSrtp() ? new FakeDtlsIdentityStore()
                                               : nullptr);
 
-    return CreateClientWithDtlsIdentityStore(id, nullptr, options,
-                                             std::move(dtls_identity_store),
-                                             false, worker_thread);
+    return CreateClientWithDtlsIdentityStore(
+        id, nullptr, options, std::move(dtls_identity_store), false,
+        network_thread, worker_thread);
   }
 
   ~PeerConnectionTestClient() {
@@ -806,6 +809,7 @@
       const PeerConnectionFactory::Options* options,
       std::unique_ptr<webrtc::DtlsIdentityStoreInterface> dtls_identity_store,
       bool prefer_constraint_apis,
+      rtc::Thread* network_thread,
       rtc::Thread* worker_thread) {
     EXPECT_TRUE(!peer_connection_);
     EXPECT_TRUE(!peer_connection_factory_);
@@ -815,7 +819,7 @@
     prefer_constraint_apis_ = prefer_constraint_apis;
 
     std::unique_ptr<cricket::PortAllocator> port_allocator(
-        new cricket::FakePortAllocator(worker_thread, nullptr));
+        new cricket::FakePortAllocator(network_thread, nullptr));
     fake_audio_capture_module_ = FakeAudioCaptureModule::Create();
 
     if (fake_audio_capture_module_ == nullptr) {
@@ -823,9 +827,11 @@
     }
     fake_video_decoder_factory_ = new FakeWebRtcVideoDecoderFactory();
     fake_video_encoder_factory_ = new FakeWebRtcVideoEncoderFactory();
+    rtc::Thread* const signaling_thread = rtc::Thread::Current();
     peer_connection_factory_ = webrtc::CreatePeerConnectionFactory(
-        worker_thread, rtc::Thread::Current(), fake_audio_capture_module_,
-        fake_video_encoder_factory_, fake_video_decoder_factory_);
+        network_thread, worker_thread, signaling_thread,
+        fake_audio_capture_module_, fake_video_encoder_factory_,
+        fake_video_decoder_factory_);
     if (!peer_connection_factory_) {
       return false;
     }
@@ -1022,10 +1028,13 @@
 class P2PTestConductor : public testing::Test {
  public:
   P2PTestConductor()
-      : pss_(new rtc::PhysicalSocketServer),
+      : network_thread_(rtc::Thread::CreateWithSocketServer()),
+        worker_thread_(rtc::Thread::Create()),
+        pss_(new rtc::PhysicalSocketServer),
         ss_(new rtc::VirtualSocketServer(pss_.get())),
         ss_scope_(ss_.get()) {
-    RTC_CHECK(worker_thread_.Start());
+    RTC_CHECK(network_thread_->Start());
+    RTC_CHECK(worker_thread_->Start());
   }
 
   bool SessionActive() {
@@ -1135,10 +1144,10 @@
   bool CreateTestClientsThatPreferNoConstraints() {
     initiating_client_.reset(
         PeerConnectionTestClient::CreateClientPreferNoConstraints(
-            "Caller: ", nullptr, &worker_thread_));
+            "Caller: ", nullptr, network_thread_.get(), worker_thread_.get()));
     receiving_client_.reset(
         PeerConnectionTestClient::CreateClientPreferNoConstraints(
-            "Callee: ", nullptr, &worker_thread_));
+            "Callee: ", nullptr, network_thread_.get(), worker_thread_.get()));
     if (!initiating_client_ || !receiving_client_) {
       return false;
     }
@@ -1158,9 +1167,11 @@
                          MediaConstraintsInterface* recv_constraints,
                          PeerConnectionFactory::Options* recv_options) {
     initiating_client_.reset(PeerConnectionTestClient::CreateClient(
-        "Caller: ", init_constraints, init_options, &worker_thread_));
+        "Caller: ", init_constraints, init_options, network_thread_.get(),
+        worker_thread_.get()));
     receiving_client_.reset(PeerConnectionTestClient::CreateClient(
-        "Callee: ", recv_constraints, recv_options, &worker_thread_));
+        "Callee: ", recv_constraints, recv_options, network_thread_.get(),
+        worker_thread_.get()));
     if (!initiating_client_ || !receiving_client_) {
       return false;
     }
@@ -1262,7 +1273,7 @@
     return PeerConnectionTestClient::CreateClientWithDtlsIdentityStore(
         "New Peer: ", &setup_constraints, nullptr,
         std::move(dtls_identity_store), prefer_constraint_apis_,
-        &worker_thread_);
+        network_thread_.get(), worker_thread_.get());
   }
 
   void SendRtpData(webrtc::DataChannelInterface* dc, const std::string& data) {
@@ -1304,7 +1315,8 @@
  private:
   // |worker_thread_| is used by both |initiating_client_| and
   // |receiving_client_|. Must be destroyed last.
-  rtc::Thread worker_thread_;
+  std::unique_ptr<rtc::Thread> network_thread_;
+  std::unique_ptr<rtc::Thread> worker_thread_;
   std::unique_ptr<rtc::PhysicalSocketServer> pss_;
   std::unique_ptr<rtc::VirtualSocketServer> ss_;
   rtc::SocketServerScope ss_scope_;
diff --git a/webrtc/api/peerconnectionendtoend_unittest.cc b/webrtc/api/peerconnectionendtoend_unittest.cc
index 40bb437..4ca73ba 100644
--- a/webrtc/api/peerconnectionendtoend_unittest.cc
+++ b/webrtc/api/peerconnectionendtoend_unittest.cc
@@ -50,11 +50,12 @@
       DataChannelList;
 
   PeerConnectionEndToEndTest() {
+    RTC_CHECK(network_thread_.Start());
     RTC_CHECK(worker_thread_.Start());
     caller_ = new rtc::RefCountedObject<PeerConnectionTestWrapper>(
-        "caller", &worker_thread_);
+        "caller", &network_thread_, &worker_thread_);
     callee_ = new rtc::RefCountedObject<PeerConnectionTestWrapper>(
-        "callee", &worker_thread_);
+        "callee", &network_thread_, &worker_thread_);
 #ifdef WEBRTC_ANDROID
     webrtc::InitializeAndroidObjects();
 #endif
@@ -155,6 +156,7 @@
   }
 
  protected:
+  rtc::Thread network_thread_;
   rtc::Thread worker_thread_;
   rtc::scoped_refptr<PeerConnectionTestWrapper> caller_;
   rtc::scoped_refptr<PeerConnectionTestWrapper> callee_;
diff --git a/webrtc/api/peerconnectionfactory.cc b/webrtc/api/peerconnectionfactory.cc
index d236b19..9cb5b46 100644
--- a/webrtc/api/peerconnectionfactory.cc
+++ b/webrtc/api/peerconnectionfactory.cc
@@ -74,19 +74,17 @@
                                             pc_factory);
 }
 
-rtc::scoped_refptr<PeerConnectionFactoryInterface>
-CreatePeerConnectionFactory(
+rtc::scoped_refptr<PeerConnectionFactoryInterface> CreatePeerConnectionFactory(
+    rtc::Thread* network_thread,
     rtc::Thread* worker_thread,
     rtc::Thread* signaling_thread,
     AudioDeviceModule* default_adm,
     cricket::WebRtcVideoEncoderFactory* encoder_factory,
     cricket::WebRtcVideoDecoderFactory* decoder_factory) {
   rtc::scoped_refptr<PeerConnectionFactory> pc_factory(
-      new rtc::RefCountedObject<PeerConnectionFactory>(worker_thread,
-                                                       signaling_thread,
-                                                       default_adm,
-                                                       encoder_factory,
-                                                       decoder_factory));
+      new rtc::RefCountedObject<PeerConnectionFactory>(
+          network_thread, worker_thread, signaling_thread, default_adm,
+          encoder_factory, decoder_factory));
 
   // Call Initialize synchronously but make sure its executed on
   // |signaling_thread|.
@@ -104,16 +102,19 @@
 PeerConnectionFactory::PeerConnectionFactory()
     : owns_ptrs_(true),
       wraps_current_thread_(false),
-      signaling_thread_(rtc::ThreadManager::Instance()->CurrentThread()),
-      worker_thread_(new rtc::Thread) {
+      network_thread_(rtc::Thread::CreateWithSocketServer().release()),
+      worker_thread_(rtc::Thread::Create().release()),
+      signaling_thread_(rtc::Thread::Current()) {
   if (!signaling_thread_) {
     signaling_thread_ = rtc::ThreadManager::Instance()->WrapCurrentThread();
     wraps_current_thread_ = true;
   }
+  network_thread_->Start();
   worker_thread_->Start();
 }
 
 PeerConnectionFactory::PeerConnectionFactory(
+    rtc::Thread* network_thread,
     rtc::Thread* worker_thread,
     rtc::Thread* signaling_thread,
     AudioDeviceModule* default_adm,
@@ -121,13 +122,15 @@
     cricket::WebRtcVideoDecoderFactory* video_decoder_factory)
     : owns_ptrs_(false),
       wraps_current_thread_(false),
-      signaling_thread_(signaling_thread),
+      network_thread_(network_thread),
       worker_thread_(worker_thread),
+      signaling_thread_(signaling_thread),
       default_adm_(default_adm),
       video_encoder_factory_(video_encoder_factory),
       video_decoder_factory_(video_decoder_factory) {
-  ASSERT(worker_thread != NULL);
-  ASSERT(signaling_thread != NULL);
+  RTC_DCHECK(network_thread);
+  RTC_DCHECK(worker_thread);
+  RTC_DCHECK(signaling_thread);
   // TODO: Currently there is no way creating an external adm in
   // libjingle source tree. So we can 't currently assert if this is NULL.
   // ASSERT(default_adm != NULL);
@@ -148,6 +151,7 @@
     if (wraps_current_thread_)
       rtc::ThreadManager::Instance()->UnwrapCurrentThread();
     delete worker_thread_;
+    delete network_thread_;
   }
 }
 
@@ -161,7 +165,7 @@
   }
 
   default_socket_factory_.reset(
-      new rtc::BasicPacketSocketFactory(worker_thread_));
+      new rtc::BasicPacketSocketFactory(network_thread_));
   if (!default_socket_factory_) {
     return false;
   }
@@ -172,17 +176,16 @@
       worker_thread_->Invoke<cricket::MediaEngineInterface*>(rtc::Bind(
       &PeerConnectionFactory::CreateMediaEngine_w, this));
 
-  rtc::Thread* const network_thread = worker_thread_;
   channel_manager_.reset(new cricket::ChannelManager(
-      media_engine, worker_thread_, network_thread));
+      media_engine, worker_thread_, network_thread_));
 
   channel_manager_->SetVideoRtxEnabled(true);
   if (!channel_manager_->Init()) {
     return false;
   }
 
-  dtls_identity_store_ = new RefCountedDtlsIdentityStore(
-      signaling_thread_, worker_thread_);
+  dtls_identity_store_ =
+      new RefCountedDtlsIdentityStore(signaling_thread_, network_thread_);
 
   return true;
 }
@@ -340,6 +343,10 @@
   return worker_thread_;
 }
 
+rtc::Thread* PeerConnectionFactory::network_thread() {
+  return network_thread_;
+}
+
 cricket::MediaEngineInterface* PeerConnectionFactory::CreateMediaEngine_w() {
   ASSERT(worker_thread_ == rtc::Thread::Current());
   return cricket::WebRtcMediaEngineFactory::Create(
diff --git a/webrtc/api/peerconnectionfactory.h b/webrtc/api/peerconnectionfactory.h
index ec8ea17..21165cf 100644
--- a/webrtc/api/peerconnectionfactory.h
+++ b/webrtc/api/peerconnectionfactory.h
@@ -94,11 +94,13 @@
       const cricket::MediaConfig& config) const;
   virtual rtc::Thread* signaling_thread();
   virtual rtc::Thread* worker_thread();
+  virtual rtc::Thread* network_thread();
   const Options& options() const { return options_; }
 
  protected:
   PeerConnectionFactory();
   PeerConnectionFactory(
+      rtc::Thread* network_thread,
       rtc::Thread* worker_thread,
       rtc::Thread* signaling_thread,
       AudioDeviceModule* default_adm,
@@ -111,8 +113,9 @@
 
   bool owns_ptrs_;
   bool wraps_current_thread_;
-  rtc::Thread* signaling_thread_;
+  rtc::Thread* network_thread_;
   rtc::Thread* worker_thread_;
+  rtc::Thread* signaling_thread_;
   Options options_;
   // External Audio device used for audio playback.
   rtc::scoped_refptr<AudioDeviceModule> default_adm_;
diff --git a/webrtc/api/peerconnectionfactory_unittest.cc b/webrtc/api/peerconnectionfactory_unittest.cc
index 963f1fe..de21e80 100644
--- a/webrtc/api/peerconnectionfactory_unittest.cc
+++ b/webrtc/api/peerconnectionfactory_unittest.cc
@@ -87,11 +87,9 @@
 #ifdef WEBRTC_ANDROID
     webrtc::InitializeAndroidObjects();
 #endif
-    factory_ = webrtc::CreatePeerConnectionFactory(rtc::Thread::Current(),
-                                                   rtc::Thread::Current(),
-                                                   NULL,
-                                                   NULL,
-                                                   NULL);
+    factory_ = webrtc::CreatePeerConnectionFactory(
+        rtc::Thread::Current(), rtc::Thread::Current(), rtc::Thread::Current(),
+        nullptr, nullptr, nullptr);
 
     ASSERT_TRUE(factory_.get() != NULL);
     port_allocator_.reset(
diff --git a/webrtc/api/peerconnectioninterface.h b/webrtc/api/peerconnectioninterface.h
index 68904e2..7575c7a 100644
--- a/webrtc/api/peerconnectioninterface.h
+++ b/webrtc/api/peerconnectioninterface.h
@@ -58,7 +58,6 @@
 
 #include "webrtc/api/datachannelinterface.h"
 #include "webrtc/api/dtlsidentitystore.h"
-#include "webrtc/api/dtlsidentitystore.h"
 #include "webrtc/api/dtmfsenderinterface.h"
 #include "webrtc/api/jsep.h"
 #include "webrtc/api/mediastreaminterface.h"
@@ -66,6 +65,7 @@
 #include "webrtc/api/rtpsenderinterface.h"
 #include "webrtc/api/statstypes.h"
 #include "webrtc/api/umametrics.h"
+#include "webrtc/base/deprecation.h"
 #include "webrtc/base/fileutils.h"
 #include "webrtc/base/network.h"
 #include "webrtc/base/rtccertificate.h"
@@ -679,19 +679,34 @@
 
 // Create a new instance of PeerConnectionFactoryInterface.
 //
-// |worker_thread| and |signaling_thread| are the only mandatory
-// parameters.
+// |network_thread|, |worker_thread| and |signaling_thread| are
+// the only mandatory parameters.
 //
 // If non-null, ownership of |default_adm|, |encoder_factory| and
 // |decoder_factory| are transferred to the returned factory.
-rtc::scoped_refptr<PeerConnectionFactoryInterface>
-CreatePeerConnectionFactory(
+rtc::scoped_refptr<PeerConnectionFactoryInterface> CreatePeerConnectionFactory(
+    rtc::Thread* network_thread,
     rtc::Thread* worker_thread,
     rtc::Thread* signaling_thread,
     AudioDeviceModule* default_adm,
     cricket::WebRtcVideoEncoderFactory* encoder_factory,
     cricket::WebRtcVideoDecoderFactory* decoder_factory);
 
+// Create a new instance of PeerConnectionFactoryInterface.
+// Same thread is used as worker and network thread.
+RTC_DEPRECATED
+inline rtc::scoped_refptr<PeerConnectionFactoryInterface>
+CreatePeerConnectionFactory(
+    rtc::Thread* worker_and_network_thread,
+    rtc::Thread* signaling_thread,
+    AudioDeviceModule* default_adm,
+    cricket::WebRtcVideoEncoderFactory* encoder_factory,
+    cricket::WebRtcVideoDecoderFactory* decoder_factory) {
+  return CreatePeerConnectionFactory(
+      worker_and_network_thread, worker_and_network_thread, signaling_thread,
+      default_adm, encoder_factory, decoder_factory);
+}
+
 }  // namespace webrtc
 
 #endif  // WEBRTC_API_PEERCONNECTIONINTERFACE_H_
diff --git a/webrtc/api/peerconnectioninterface_unittest.cc b/webrtc/api/peerconnectioninterface_unittest.cc
index d33a402..445f025 100644
--- a/webrtc/api/peerconnectioninterface_unittest.cc
+++ b/webrtc/api/peerconnectioninterface_unittest.cc
@@ -545,9 +545,9 @@
 
   virtual void SetUp() {
     pc_factory_ = webrtc::CreatePeerConnectionFactory(
-        rtc::Thread::Current(), rtc::Thread::Current(), NULL, NULL,
-        NULL);
-    ASSERT_TRUE(pc_factory_.get() != NULL);
+        rtc::Thread::Current(), rtc::Thread::Current(), rtc::Thread::Current(),
+        nullptr, nullptr, nullptr);
+    ASSERT_TRUE(pc_factory_);
   }
 
   void CreatePeerConnection() {
diff --git a/webrtc/api/statscollector_unittest.cc b/webrtc/api/statscollector_unittest.cc
index 9243aed..7953515 100644
--- a/webrtc/api/statscollector_unittest.cc
+++ b/webrtc/api/statscollector_unittest.cc
@@ -75,6 +75,7 @@
       : WebRtcSession(media_controller,
                       rtc::Thread::Current(),
                       rtc::Thread::Current(),
+                      rtc::Thread::Current(),
                       nullptr) {}
   MOCK_METHOD0(voice_channel, cricket::VoiceChannel*());
   MOCK_METHOD0(video_channel, cricket::VideoChannel*());
diff --git a/webrtc/api/test/peerconnectiontestwrapper.cc b/webrtc/api/test/peerconnectiontestwrapper.cc
index 1ec2b47..ed8e031 100644
--- a/webrtc/api/test/peerconnectiontestwrapper.cc
+++ b/webrtc/api/test/peerconnectiontestwrapper.cc
@@ -47,16 +47,20 @@
       caller, &PeerConnectionTestWrapper::ReceiveAnswerSdp);
 }
 
-PeerConnectionTestWrapper::PeerConnectionTestWrapper(const std::string& name,
-                                                     rtc::Thread* worker_thread)
-    : name_(name), worker_thread_(worker_thread) {}
+PeerConnectionTestWrapper::PeerConnectionTestWrapper(
+    const std::string& name,
+    rtc::Thread* network_thread,
+    rtc::Thread* worker_thread)
+    : name_(name),
+      network_thread_(network_thread),
+      worker_thread_(worker_thread) {}
 
 PeerConnectionTestWrapper::~PeerConnectionTestWrapper() {}
 
 bool PeerConnectionTestWrapper::CreatePc(
   const MediaConstraintsInterface* constraints) {
   std::unique_ptr<cricket::PortAllocator> port_allocator(
-      new cricket::FakePortAllocator(worker_thread_, nullptr));
+      new cricket::FakePortAllocator(network_thread_, nullptr));
 
   fake_audio_capture_module_ = FakeAudioCaptureModule::Create();
   if (fake_audio_capture_module_ == NULL) {
@@ -64,8 +68,8 @@
   }
 
   peer_connection_factory_ = webrtc::CreatePeerConnectionFactory(
-      worker_thread_, rtc::Thread::Current(), fake_audio_capture_module_, NULL,
-      NULL);
+      network_thread_, worker_thread_, rtc::Thread::Current(),
+      fake_audio_capture_module_, NULL, NULL);
   if (!peer_connection_factory_) {
     return false;
   }
diff --git a/webrtc/api/test/peerconnectiontestwrapper.h b/webrtc/api/test/peerconnectiontestwrapper.h
index 7a9bea4..3272366 100644
--- a/webrtc/api/test/peerconnectiontestwrapper.h
+++ b/webrtc/api/test/peerconnectiontestwrapper.h
@@ -27,8 +27,9 @@
   static void Connect(PeerConnectionTestWrapper* caller,
                       PeerConnectionTestWrapper* callee);
 
-  explicit PeerConnectionTestWrapper(const std::string& name,
-                                     rtc::Thread* worker_thread);
+  PeerConnectionTestWrapper(const std::string& name,
+                            rtc::Thread* network_thread,
+                            rtc::Thread* worker_thread);
   virtual ~PeerConnectionTestWrapper();
 
   bool CreatePc(const webrtc::MediaConstraintsInterface* constraints);
@@ -91,7 +92,8 @@
       bool video, const webrtc::FakeConstraints& video_constraints);
 
   std::string name_;
-  rtc::Thread* worker_thread_;
+  rtc::Thread* const network_thread_;
+  rtc::Thread* const worker_thread_;
   rtc::scoped_refptr<webrtc::PeerConnectionInterface> peer_connection_;
   rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface>
       peer_connection_factory_;
diff --git a/webrtc/api/webrtcsession.cc b/webrtc/api/webrtcsession.cc
index 96442c8..e7d9b14 100644
--- a/webrtc/api/webrtcsession.cc
+++ b/webrtc/api/webrtcsession.cc
@@ -454,17 +454,18 @@
 }
 
 WebRtcSession::WebRtcSession(webrtc::MediaControllerInterface* media_controller,
-                             rtc::Thread* signaling_thread,
+                             rtc::Thread* network_thread,
                              rtc::Thread* worker_thread,
+                             rtc::Thread* signaling_thread,
                              cricket::PortAllocator* port_allocator)
-    : signaling_thread_(signaling_thread),
-      worker_thread_(worker_thread),
+    : worker_thread_(worker_thread),
+      signaling_thread_(signaling_thread),
       // RFC 3264: The numeric value of the session id and version in the
       // o line MUST be representable with a "64 bit signed integer".
       // Due to this constraint session id |sid_| is max limited to LLONG_MAX.
       sid_(rtc::ToString(rtc::CreateRandomId64() & LLONG_MAX)),
       transport_controller_(new cricket::TransportController(signaling_thread,
-                                                             worker_thread,
+                                                             network_thread,
                                                              port_allocator)),
       media_controller_(media_controller),
       channel_manager_(media_controller_->channel_manager()),
diff --git a/webrtc/api/webrtcsession.h b/webrtc/api/webrtcsession.h
index d0a5cd4..a97bd73 100644
--- a/webrtc/api/webrtcsession.h
+++ b/webrtc/api/webrtcsession.h
@@ -138,14 +138,15 @@
   };
 
   WebRtcSession(webrtc::MediaControllerInterface* media_controller,
-                rtc::Thread* signaling_thread,
+                rtc::Thread* network_thread,
                 rtc::Thread* worker_thread,
+                rtc::Thread* signaling_thread,
                 cricket::PortAllocator* port_allocator);
   virtual ~WebRtcSession();
 
   // These are const to allow them to be called from const methods.
-  rtc::Thread* signaling_thread() const { return signaling_thread_; }
   rtc::Thread* worker_thread() const { return worker_thread_; }
+  rtc::Thread* signaling_thread() const { return signaling_thread_; }
 
   // The ID of this session.
   const std::string& id() const { return sid_; }
@@ -470,8 +471,8 @@
 
   void OnSentPacket_w(const rtc::SentPacket& sent_packet);
 
-  rtc::Thread* const signaling_thread_;
   rtc::Thread* const worker_thread_;
+  rtc::Thread* const signaling_thread_;
 
   State state_ = STATE_INIT;
   Error error_ = ERROR_NONE;
diff --git a/webrtc/api/webrtcsession_unittest.cc b/webrtc/api/webrtcsession_unittest.cc
index d81aece..ea8b3c5 100644
--- a/webrtc/api/webrtcsession_unittest.cc
+++ b/webrtc/api/webrtcsession_unittest.cc
@@ -208,13 +208,15 @@
 class WebRtcSessionForTest : public webrtc::WebRtcSession {
  public:
   WebRtcSessionForTest(webrtc::MediaControllerInterface* media_controller,
-                       rtc::Thread* signaling_thread,
+                       rtc::Thread* network_thread,
                        rtc::Thread* worker_thread,
+                       rtc::Thread* signaling_thread,
                        cricket::PortAllocator* port_allocator,
                        webrtc::IceObserver* ice_observer)
       : WebRtcSession(media_controller,
-                      signaling_thread,
+                      network_thread,
                       worker_thread,
+                      signaling_thread,
                       port_allocator) {
     RegisterIceObserver(ice_observer);
   }
@@ -381,7 +383,7 @@
     ASSERT_TRUE(session_.get() == NULL);
     session_.reset(new WebRtcSessionForTest(
         media_controller_.get(), rtc::Thread::Current(), rtc::Thread::Current(),
-        allocator_.get(), &observer_));
+        rtc::Thread::Current(), allocator_.get(), &observer_));
     session_->SignalDataChannelOpenMessage.connect(
         this, &WebRtcSessionTest::OnDataChannelOpenMessage);
     session_->GetOnDestroyedSignal()->connect(
diff --git a/webrtc/sdk/objc/Framework/Classes/RTCPeerConnectionFactory.mm b/webrtc/sdk/objc/Framework/Classes/RTCPeerConnectionFactory.mm
index 82d7707..2398ce5 100644
--- a/webrtc/sdk/objc/Framework/Classes/RTCPeerConnectionFactory.mm
+++ b/webrtc/sdk/objc/Framework/Classes/RTCPeerConnectionFactory.mm
@@ -23,23 +23,30 @@
 #include <memory>
 
 @implementation RTCPeerConnectionFactory {
-  std::unique_ptr<rtc::Thread> _signalingThread;
+  std::unique_ptr<rtc::Thread> _networkThread;
   std::unique_ptr<rtc::Thread> _workerThread;
+  std::unique_ptr<rtc::Thread> _signalingThread;
 }
 
 @synthesize nativeFactory = _nativeFactory;
 
 - (instancetype)init {
   if ((self = [super init])) {
-    _signalingThread.reset(new rtc::Thread());
-    BOOL result = _signalingThread->Start();
-    NSAssert(result, @"Failed to start signaling thread.");
-    _workerThread.reset(new rtc::Thread());
+    _networkThread = rtc::Thread::CreateWithSocketServer();
+    BOOL result = _networkThread->Start();
+    NSAssert(result, @"Failed to start network thread.");
+
+    _workerThread = rtc::Thread::Create();
     result = _workerThread->Start();
     NSAssert(result, @"Failed to start worker thread.");
 
+    _signalingThread = rtc::Thread::Create();
+    result = _signalingThread->Start();
+    NSAssert(result, @"Failed to start signaling thread.");
+
     _nativeFactory = webrtc::CreatePeerConnectionFactory(
-        _workerThread.get(), _signalingThread.get(), nullptr, nullptr, nullptr);
+        _networkThread.get(), _workerThread.get(), _signalingThread.get(),
+        nullptr, nullptr, nullptr);
     NSAssert(_nativeFactory, @"Failed to initialize PeerConnectionFactory!");
   }
   return self;