Make TaskQueueFactory required construction parameter for Call

Bug: webrtc:10284
Change-Id: I573ee0087c035e26918260c21b8b0213ddfe7ebc
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/143791
Commit-Queue: Danil Chapovalov <danilchap@webrtc.org>
Reviewed-by: Niels Moller <nisse@webrtc.org>
Reviewed-by: Karl Wiberg <kwiberg@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#28467}
diff --git a/call/BUILD.gn b/call/BUILD.gn
index 694a2eb..b8c9d23 100644
--- a/call/BUILD.gn
+++ b/call/BUILD.gn
@@ -227,7 +227,6 @@
     "../api:rtp_headers",
     "../api:simulated_network_api",
     "../api:transport_api",
-    "../api/task_queue:global_task_queue_factory",
     "../api/transport:network_control",
     "../api/units:time_delta",
     "../api/video_codecs:video_codecs_api",
diff --git a/call/call.cc b/call/call.cc
index fe922e4..ca96888 100644
--- a/call/call.cc
+++ b/call/call.cc
@@ -18,7 +18,6 @@
 
 #include "absl/memory/memory.h"
 #include "absl/types/optional.h"
-#include "api/task_queue/global_task_queue_factory.h"
 #include "api/transport/network_control.h"
 #include "audio/audio_receive_stream.h"
 #include "audio/audio_send_stream.h"
@@ -430,18 +429,14 @@
                    Clock* clock,
                    std::unique_ptr<ProcessThread> call_thread,
                    std::unique_ptr<ProcessThread> pacer_thread) {
-  // TODO(bugs.webrtc.org/10284): DCHECK task_queue_factory dependency is
-  // always provided in the config.
-  TaskQueueFactory* task_queue_factory = config.task_queue_factory
-                                             ? config.task_queue_factory
-                                             : &GlobalTaskQueueFactory();
+  RTC_DCHECK(config.task_queue_factory);
   return new internal::Call(
       clock, config,
       absl::make_unique<RtpTransportControllerSend>(
           clock, config.event_log, config.network_state_predictor_factory,
           config.network_controller_factory, config.bitrate_config,
-          std::move(pacer_thread), task_queue_factory),
-      std::move(call_thread), task_queue_factory);
+          std::move(pacer_thread), config.task_queue_factory),
+      std::move(call_thread), config.task_queue_factory);
 }
 
 // This method here to avoid subclasses has to implement this method.
diff --git a/call/call_config.h b/call/call_config.h
index d111b53..a6f7a96 100644
--- a/call/call_config.h
+++ b/call/call_config.h
@@ -47,7 +47,7 @@
   // FecController to use for this call.
   FecControllerFactoryInterface* fec_controller_factory = nullptr;
 
-  // Task Queue Factory to be used in this call.
+  // Task Queue Factory to be used in this call. Required.
   TaskQueueFactory* task_queue_factory = nullptr;
 
   // NetworkStatePredictor to use for this call.
diff --git a/call/call_unittest.cc b/call/call_unittest.cc
index 4c78413..a3d4938 100644
--- a/call/call_unittest.cc
+++ b/call/call_unittest.cc
@@ -15,6 +15,7 @@
 
 #include "absl/memory/memory.h"
 #include "api/audio_codecs/builtin_audio_decoder_factory.h"
+#include "api/task_queue/default_task_queue_factory.h"
 #include "api/test/fake_media_transport.h"
 #include "api/test/mock_audio_mixer.h"
 #include "audio/audio_receive_stream.h"
@@ -35,6 +36,7 @@
 
 struct CallHelper {
   CallHelper() {
+    task_queue_factory_ = webrtc::CreateDefaultTaskQueueFactory();
     webrtc::AudioState::Config audio_state_config;
     audio_state_config.audio_mixer =
         new rtc::RefCountedObject<webrtc::test::MockAudioMixer>();
@@ -44,6 +46,7 @@
         new rtc::RefCountedObject<webrtc::test::MockAudioDeviceModule>();
     webrtc::Call::Config config(&event_log_);
     config.audio_state = webrtc::AudioState::Create(audio_state_config);
+    config.task_queue_factory = task_queue_factory_.get();
     call_.reset(webrtc::Call::Create(config));
   }
 
@@ -51,6 +54,7 @@
 
  private:
   webrtc::RtcEventLogNullImpl event_log_;
+  std::unique_ptr<webrtc::TaskQueueFactory> task_queue_factory_;
   std::unique_ptr<webrtc::Call> call_;
 };
 }  // namespace
diff --git a/media/engine/webrtc_video_engine_unittest.cc b/media/engine/webrtc_video_engine_unittest.cc
index ff5f9e5..1c2cda8 100644
--- a/media/engine/webrtc_video_engine_unittest.cc
+++ b/media/engine/webrtc_video_engine_unittest.cc
@@ -19,6 +19,7 @@
 #include "absl/strings/match.h"
 #include "api/media_transport_config.h"
 #include "api/rtp_parameters.h"
+#include "api/task_queue/default_task_queue_factory.h"
 #include "api/test/fake_media_transport.h"
 #include "api/test/mock_video_bitrate_allocator.h"
 #include "api/test/mock_video_bitrate_allocator_factory.h"
@@ -226,7 +227,12 @@
                 ? nullptr
                 : absl::make_unique<webrtc::test::ScopedFieldTrials>(
                       field_trials)),
-        call_(webrtc::Call::Create(webrtc::Call::Config(&event_log_))),
+        task_queue_factory_(webrtc::CreateDefaultTaskQueueFactory()),
+        call_(webrtc::Call::Create([&] {
+          webrtc::Call::Config call_config(&event_log_);
+          call_config.task_queue_factory = task_queue_factory_.get();
+          return call_config;
+        }())),
         encoder_factory_(new cricket::FakeWebRtcVideoEncoderFactory),
         decoder_factory_(new cricket::FakeWebRtcVideoDecoderFactory),
         video_bitrate_allocator_factory_(
@@ -264,6 +270,7 @@
   rtc::ScopedFakeClock fake_clock_;
   std::unique_ptr<webrtc::test::ScopedFieldTrials> override_field_trials_;
   webrtc::RtcEventLogNullImpl event_log_;
+  std::unique_ptr<webrtc::TaskQueueFactory> task_queue_factory_;
   // Used in WebRtcVideoEngineVoiceTest, but defined here so it's properly
   // initialized when the constructor is called.
   std::unique_ptr<webrtc::Call> call_;
@@ -1137,8 +1144,10 @@
 
   // Create a call.
   webrtc::RtcEventLogNullImpl event_log;
-  std::unique_ptr<webrtc::Call> call(
-      webrtc::Call::Create(webrtc::Call::Config(&event_log)));
+  auto task_queue_factory = webrtc::CreateDefaultTaskQueueFactory();
+  webrtc::Call::Config call_config(&event_log);
+  call_config.task_queue_factory = task_queue_factory.get();
+  const auto call = absl::WrapUnique(webrtc::Call::Create(call_config));
 
   // Create send channel.
   const int send_ssrc = 123;
@@ -1205,8 +1214,10 @@
 
   // Create a call.
   webrtc::RtcEventLogNullImpl event_log;
-  std::unique_ptr<webrtc::Call> call(
-      webrtc::Call::Create(webrtc::Call::Config(&event_log)));
+  auto task_queue_factory = webrtc::CreateDefaultTaskQueueFactory();
+  webrtc::Call::Config call_config(&event_log);
+  call_config.task_queue_factory = task_queue_factory.get();
+  const auto call = absl::WrapUnique(webrtc::Call::Create(call_config));
 
   // Create recv channel.
   const int recv_ssrc = 321;
@@ -1286,7 +1297,8 @@
 class WebRtcVideoChannelBaseTest : public ::testing::Test {
  protected:
   WebRtcVideoChannelBaseTest()
-      : video_bitrate_allocator_factory_(
+      : task_queue_factory_(webrtc::CreateDefaultTaskQueueFactory()),
+        video_bitrate_allocator_factory_(
             webrtc::CreateBuiltinVideoBitrateAllocatorFactory()),
         engine_(webrtc::CreateBuiltinVideoEncoderFactory(),
                 webrtc::CreateBuiltinVideoDecoderFactory()) {}
@@ -1294,7 +1306,9 @@
   virtual void SetUp() {
     // One testcase calls SetUp in a loop, only create call_ once.
     if (!call_) {
-      call_.reset(webrtc::Call::Create(webrtc::Call::Config(&event_log_)));
+      webrtc::Call::Config call_config(&event_log_);
+      call_config.task_queue_factory = task_queue_factory_.get();
+      call_.reset(webrtc::Call::Create(call_config));
     }
     cricket::MediaConfig media_config;
     // Disabling cpu overuse detection actually disables quality scaling too; it
@@ -1475,6 +1489,7 @@
   }
 
   webrtc::RtcEventLogNullImpl event_log_;
+  std::unique_ptr<webrtc::TaskQueueFactory> task_queue_factory_;
   std::unique_ptr<webrtc::Call> call_;
   std::unique_ptr<webrtc::VideoBitrateAllocatorFactory>
       video_bitrate_allocator_factory_;
diff --git a/media/engine/webrtc_voice_engine_unittest.cc b/media/engine/webrtc_voice_engine_unittest.cc
index 42d6145..d292038 100644
--- a/media/engine/webrtc_voice_engine_unittest.cc
+++ b/media/engine/webrtc_voice_engine_unittest.cc
@@ -11,6 +11,7 @@
 #include <memory>
 #include <utility>
 
+#include "absl/memory/memory.h"
 #include "absl/strings/match.h"
 #include "api/audio_codecs/builtin_audio_decoder_factory.h"
 #include "api/audio_codecs/builtin_audio_encoder_factory.h"
@@ -3457,8 +3458,9 @@
       webrtc::MockAudioDecoderFactory::CreateUnusedFactory(), nullptr, apm);
   engine.Init();
   webrtc::RtcEventLogNullImpl event_log;
-  std::unique_ptr<webrtc::Call> call(
-      webrtc::Call::Create(webrtc::Call::Config(&event_log)));
+  webrtc::Call::Config call_config(&event_log);
+  call_config.task_queue_factory = task_queue_factory.get();
+  auto call = absl::WrapUnique(webrtc::Call::Create(call_config));
   cricket::VoiceMediaChannel* channel = engine.CreateMediaChannel(
       call.get(), cricket::MediaConfig(), cricket::AudioOptions(),
       webrtc::CryptoOptions());
@@ -3484,8 +3486,9 @@
         webrtc::MockAudioDecoderFactory::CreateUnusedFactory(), nullptr, apm);
     engine.Init();
     webrtc::RtcEventLogNullImpl event_log;
-    std::unique_ptr<webrtc::Call> call(
-        webrtc::Call::Create(webrtc::Call::Config(&event_log)));
+    webrtc::Call::Config call_config(&event_log);
+    call_config.task_queue_factory = task_queue_factory.get();
+    auto call = absl::WrapUnique(webrtc::Call::Create(call_config));
     cricket::VoiceMediaChannel* channel = engine.CreateMediaChannel(
         call.get(), cricket::MediaConfig(), cricket::AudioOptions(),
         webrtc::CryptoOptions());
@@ -3557,8 +3560,9 @@
       webrtc::MockAudioDecoderFactory::CreateUnusedFactory(), nullptr, apm);
   engine.Init();
   webrtc::RtcEventLogNullImpl event_log;
-  std::unique_ptr<webrtc::Call> call(
-      webrtc::Call::Create(webrtc::Call::Config(&event_log)));
+  webrtc::Call::Config call_config(&event_log);
+  call_config.task_queue_factory = task_queue_factory.get();
+  auto call = absl::WrapUnique(webrtc::Call::Create(call_config));
 
   cricket::VoiceMediaChannel* channels[32];
   size_t num_channels = 0;
@@ -3599,8 +3603,9 @@
       webrtc::CreateBuiltinAudioDecoderFactory(), nullptr, apm);
   engine.Init();
   webrtc::RtcEventLogNullImpl event_log;
-  std::unique_ptr<webrtc::Call> call(
-      webrtc::Call::Create(webrtc::Call::Config(&event_log)));
+  webrtc::Call::Config call_config(&event_log);
+  call_config.task_queue_factory = task_queue_factory.get();
+  auto call = absl::WrapUnique(webrtc::Call::Create(call_config));
   cricket::WebRtcVoiceMediaChannel channel(&engine, cricket::MediaConfig(),
                                            cricket::AudioOptions(),
                                            webrtc::CryptoOptions(), call.get());
diff --git a/pc/peer_connection_data_channel_unittest.cc b/pc/peer_connection_data_channel_unittest.cc
index 435d1eb..787e5ba 100644
--- a/pc/peer_connection_data_channel_unittest.cc
+++ b/pc/peer_connection_data_channel_unittest.cc
@@ -22,6 +22,7 @@
 #include "api/peer_connection_interface.h"
 #include "api/peer_connection_proxy.h"
 #include "api/scoped_refptr.h"
+#include "api/task_queue/default_task_queue_factory.h"
 #include "api/test/fake_media_transport.h"
 #include "media/base/codec.h"
 #include "media/base/fake_media_engine.h"
@@ -71,6 +72,7 @@
   deps.network_thread = network_thread;
   deps.worker_thread = worker_thread;
   deps.signaling_thread = signaling_thread;
+  deps.task_queue_factory = CreateDefaultTaskQueueFactory();
   deps.media_engine = std::move(media_engine);
   deps.call_factory = std::move(call_factory);
   deps.media_transport_factory = std::move(media_transport_factory);
diff --git a/pc/peer_connection_histogram_unittest.cc b/pc/peer_connection_histogram_unittest.cc
index 54c251a..2f997ae 100644
--- a/pc/peer_connection_histogram_unittest.cc
+++ b/pc/peer_connection_histogram_unittest.cc
@@ -22,6 +22,7 @@
 #include "api/peer_connection_proxy.h"
 #include "api/rtc_error.h"
 #include "api/scoped_refptr.h"
+#include "api/task_queue/default_task_queue_factory.h"
 #include "media/base/fake_media_engine.h"
 #include "p2p/base/mock_async_resolver.h"
 #include "p2p/base/port_allocator.h"
@@ -74,6 +75,7 @@
           dependencies.network_thread = rtc::Thread::Current();
           dependencies.worker_thread = rtc::Thread::Current();
           dependencies.signaling_thread = rtc::Thread::Current();
+          dependencies.task_queue_factory = CreateDefaultTaskQueueFactory();
           dependencies.media_engine =
               absl::make_unique<cricket::FakeMediaEngine>();
           dependencies.call_factory = CreateCallFactory();
diff --git a/test/call_test.cc b/test/call_test.cc
index 5d551a5..f7f85f6 100644
--- a/test/call_test.cc
+++ b/test/call_test.cc
@@ -217,6 +217,7 @@
 
 void CallTest::CreateSenderCall(const Call::Config& config) {
   auto sender_config = config;
+  sender_config.task_queue_factory = task_queue_factory_.get();
   sender_config.network_state_predictor_factory =
       network_state_predictor_factory_.get();
   sender_config.network_controller_factory = network_controller_factory_.get();
@@ -224,7 +225,9 @@
 }
 
 void CallTest::CreateReceiverCall(const Call::Config& config) {
-  receiver_call_.reset(Call::Create(config));
+  auto receiver_config = config;
+  receiver_config.task_queue_factory = task_queue_factory_.get();
+  receiver_call_.reset(Call::Create(receiver_config));
 }
 
 void CallTest::DestroyCalls() {
diff --git a/video/end_to_end_tests/multi_stream_tester.cc b/video/end_to_end_tests/multi_stream_tester.cc
index 58d7bc9..ff25c70 100644
--- a/video/end_to_end_tests/multi_stream_tester.cc
+++ b/video/end_to_end_tests/multi_stream_tester.cc
@@ -48,6 +48,7 @@
   webrtc::RtcEventLogNullImpl event_log;
   auto task_queue_factory = CreateDefaultTaskQueueFactory();
   Call::Config config(&event_log);
+  config.task_queue_factory = task_queue_factory.get();
   std::unique_ptr<Call> sender_call;
   std::unique_ptr<Call> receiver_call;
   std::unique_ptr<test::DirectTransport> sender_transport;