Inject network thread to Call.

This will allow for transitioning PacketReceiver callbacks and
network related callbacks from being posted over to the worker thread
and instead can stay on the network thread along with related state.

Bug: webrtc:11993
Change-Id: I38df462d4dee064015c490f2b8f809cb47f23cf1
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/202039
Reviewed-by: Mirko Bonadei <mbonadei@webrtc.org>
Commit-Queue: Tommi <tommi@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#33116}
diff --git a/call/call.cc b/call/call.cc
index dd2a126..f20f4b5 100644
--- a/call/call.cc
+++ b/call/call.cc
@@ -325,7 +325,7 @@
   Clock* const clock_;
   TaskQueueFactory* const task_queue_factory_;
   TaskQueueBase* const worker_thread_;
-  RTC_NO_UNIQUE_ADDRESS SequenceChecker network_thread_;
+  TaskQueueBase* const network_thread_;
 
   const int num_cpu_cores_;
   const rtc::scoped_refptr<SharedModuleThread> module_process_thread_;
@@ -602,6 +602,10 @@
     : clock_(clock),
       task_queue_factory_(task_queue_factory),
       worker_thread_(GetCurrentTaskQueueOrThread()),
+      // If |network_task_queue_| was set to nullptr, network related calls
+      // must be made on |worker_thread_| (i.e. they're one and the same).
+      network_thread_(config.network_task_queue_ ? config.network_task_queue_
+                                                 : worker_thread_),
       num_cpu_cores_(CpuInfo::DetectNumberOfCores()),
       module_process_thread_(std::move(module_process_thread)),
       call_stats_(new CallStats(clock_, worker_thread_)),
@@ -628,10 +632,9 @@
       transport_send_(std::move(transport_send)) {
   RTC_DCHECK(config.event_log != nullptr);
   RTC_DCHECK(config.trials != nullptr);
+  RTC_DCHECK(network_thread_);
   RTC_DCHECK(worker_thread_->IsCurrent());
 
-  network_thread_.Detach();
-
   // Do not remove this call; it is here to convince the compiler that the
   // WebRTC source timestamp string needs to be in the final binary.
   LoadWebRTCVersionInRegister();
@@ -768,7 +771,6 @@
 }
 
 PacketReceiver* Call::Receiver() {
-  RTC_DCHECK_RUN_ON(worker_thread_);
   return this;
 }
 
@@ -1429,7 +1431,7 @@
                               rtc::CopyOnWriteBuffer packet,
                               int64_t packet_time_us,
                               PacketCallback callback) {
-  RTC_DCHECK_RUN_ON(&network_thread_);
+  RTC_DCHECK_RUN_ON(network_thread_);
 
   TaskQueueBase* network_thread = rtc::Thread::Current();
   RTC_DCHECK(network_thread);
diff --git a/call/call_config.cc b/call/call_config.cc
index b149c88..8b3c912 100644
--- a/call/call_config.cc
+++ b/call/call_config.cc
@@ -14,7 +14,9 @@
 
 namespace webrtc {
 
-CallConfig::CallConfig(RtcEventLog* event_log) : event_log(event_log) {
+CallConfig::CallConfig(RtcEventLog* event_log,
+                       TaskQueueBase* network_task_queue /* = nullptr*/)
+    : event_log(event_log), network_task_queue_(network_task_queue) {
   RTC_DCHECK(event_log);
 }
 
diff --git a/call/call_config.h b/call/call_config.h
index 205f7a4..95dad36 100644
--- a/call/call_config.h
+++ b/call/call_config.h
@@ -26,7 +26,11 @@
 class RtcEventLog;
 
 struct CallConfig {
-  explicit CallConfig(RtcEventLog* event_log);
+  // If |network_task_queue| is set to nullptr, Call will assume that network
+  // related callbacks will be made on the same TQ as the Call instance was
+  // constructed on.
+  explicit CallConfig(RtcEventLog* event_log,
+                      TaskQueueBase* network_task_queue = nullptr);
   CallConfig(const CallConfig&);
   ~CallConfig();
 
@@ -42,7 +46,7 @@
 
   // RtcEventLog to use for this call. Required.
   // Use webrtc::RtcEventLog::CreateNull() for a null implementation.
-  RtcEventLog* event_log = nullptr;
+  RtcEventLog* const event_log = nullptr;
 
   // FecController to use for this call.
   FecControllerFactoryInterface* fec_controller_factory = nullptr;
@@ -63,6 +67,8 @@
   // Key-value mapping of internal configurations to apply,
   // e.g. field trials.
   const WebRtcKeyValueConfig* trials = nullptr;
+
+  TaskQueueBase* const network_task_queue_ = nullptr;
 };
 
 }  // namespace webrtc
diff --git a/pc/peer_connection_factory.cc b/pc/peer_connection_factory.cc
index 0048992..c65b2f5 100644
--- a/pc/peer_connection_factory.cc
+++ b/pc/peer_connection_factory.cc
@@ -313,7 +313,7 @@
     RtcEventLog* event_log) {
   RTC_DCHECK_RUN_ON(worker_thread());
 
-  webrtc::Call::Config call_config(event_log);
+  webrtc::Call::Config call_config(event_log, network_thread());
   if (!channel_manager()->media_engine() || !context_->call_factory()) {
     return nullptr;
   }