This reverts commit 8eb37a39e79fe1098d3503dcb8c8c2d196203fed. Chrome now have its own implementation of TaskQueues that is based on Chrome threads.

cl was originally reviewed here:
https://codereview.webrtc.org/2060403002/

- Add task queue to Call with the intent of replacing the use of one of the process threads.

- Split VideoSendStream in two. VideoSendStreamInternal is created and used on the new task queue.

- BitrateAllocator is now created on libjingle's worker thread but always used on the new task queue instead of both encoder threads and the process thread.

- VideoEncoderConfig and VideoSendStream::Config support move semantics.

- The encoder thread is moved from VideoSendStream to ViEEncoder. Frames are forwarded directly to ViEEncoder which is responsible for timestamping ? and encoding the frames.

TBR=mflodman@webrtc.org
BUG=webrtc:5687

Review-Url: https://codereview.webrtc.org/2250123002
Cr-Commit-Position: refs/heads/master@{#14014}
diff --git a/webrtc/audio/audio_send_stream.cc b/webrtc/audio/audio_send_stream.cc
index 17979d5..417720c 100644
--- a/webrtc/audio/audio_send_stream.cc
+++ b/webrtc/audio/audio_send_stream.cc
@@ -16,7 +16,9 @@
 #include "webrtc/audio/conversion.h"
 #include "webrtc/audio/scoped_voe_interface.h"
 #include "webrtc/base/checks.h"
+#include "webrtc/base/event.h"
 #include "webrtc/base/logging.h"
+#include "webrtc/base/task_queue.h"
 #include "webrtc/modules/congestion_controller/include/congestion_controller.h"
 #include "webrtc/modules/pacing/paced_sender.h"
 #include "webrtc/modules/rtp_rtcp/include/rtp_rtcp_defines.h"
@@ -59,9 +61,11 @@
 AudioSendStream::AudioSendStream(
     const webrtc::AudioSendStream::Config& config,
     const rtc::scoped_refptr<webrtc::AudioState>& audio_state,
+    rtc::TaskQueue* worker_queue,
     CongestionController* congestion_controller,
     BitrateAllocator* bitrate_allocator)
-    : config_(config),
+    : worker_queue_(worker_queue),
+      config_(config),
       audio_state_(audio_state),
       bitrate_allocator_(bitrate_allocator) {
   LOG(LS_INFO) << "AudioSendStream: " << config_.ToString();
@@ -109,8 +113,13 @@
   RTC_DCHECK(thread_checker_.CalledOnValidThread());
   if (config_.min_bitrate_kbps != -1 && config_.max_bitrate_kbps != -1) {
     RTC_DCHECK_GE(config_.max_bitrate_kbps, config_.min_bitrate_kbps);
-    bitrate_allocator_->AddObserver(this, config_.min_bitrate_kbps * 1000,
-                                    config_.max_bitrate_kbps * 1000, 0, true);
+    rtc::Event thread_sync_event(false /* manual_reset */, false);
+    worker_queue_->PostTask([this, &thread_sync_event] {
+      bitrate_allocator_->AddObserver(this, config_.min_bitrate_kbps * 1000,
+                                      config_.max_bitrate_kbps * 1000, 0, true);
+      thread_sync_event.Set();
+    });
+    thread_sync_event.Wait(rtc::Event::kForever);
   }
 
   ScopedVoEInterface<VoEBase> base(voice_engine());
@@ -122,7 +131,13 @@
 
 void AudioSendStream::Stop() {
   RTC_DCHECK(thread_checker_.CalledOnValidThread());
-  bitrate_allocator_->RemoveObserver(this);
+  rtc::Event thread_sync_event(false /* manual_reset */, false);
+  worker_queue_->PostTask([this, &thread_sync_event] {
+    bitrate_allocator_->RemoveObserver(this);
+    thread_sync_event.Set();
+  });
+  thread_sync_event.Wait(rtc::Event::kForever);
+
   ScopedVoEInterface<VoEBase> base(voice_engine());
   int error = base->StopSend(config_.voe_channel_id);
   if (error != 0) {
diff --git a/webrtc/audio/audio_send_stream.h b/webrtc/audio/audio_send_stream.h
index ec2a4db..e92c326 100644
--- a/webrtc/audio/audio_send_stream.h
+++ b/webrtc/audio/audio_send_stream.h
@@ -33,6 +33,7 @@
  public:
   AudioSendStream(const webrtc::AudioSendStream::Config& config,
                   const rtc::scoped_refptr<webrtc::AudioState>& audio_state,
+                  rtc::TaskQueue* worker_queue,
                   CongestionController* congestion_controller,
                   BitrateAllocator* bitrate_allocator);
   ~AudioSendStream() override;
@@ -59,6 +60,7 @@
   VoiceEngine* voice_engine() const;
 
   rtc::ThreadChecker thread_checker_;
+  rtc::TaskQueue* worker_queue_;
   const webrtc::AudioSendStream::Config config_;
   rtc::scoped_refptr<webrtc::AudioState> audio_state_;
   std::unique_ptr<voe::ChannelProxy> channel_proxy_;
diff --git a/webrtc/audio/audio_send_stream_unittest.cc b/webrtc/audio/audio_send_stream_unittest.cc
index 7f940fc..9172064 100644
--- a/webrtc/audio/audio_send_stream_unittest.cc
+++ b/webrtc/audio/audio_send_stream_unittest.cc
@@ -16,6 +16,7 @@
 #include "webrtc/audio/audio_send_stream.h"
 #include "webrtc/audio/audio_state.h"
 #include "webrtc/audio/conversion.h"
+#include "webrtc/base/task_queue.h"
 #include "webrtc/modules/congestion_controller/include/mock/mock_congestion_controller.h"
 #include "webrtc/call/mock/mock_rtc_event_log.h"
 #include "webrtc/modules/congestion_controller/include/congestion_controller.h"
@@ -65,7 +66,8 @@
                                &bitrate_observer_,
                                &remote_bitrate_observer_,
                                &event_log_),
-        bitrate_allocator_(&limit_observer_) {
+        bitrate_allocator_(&limit_observer_),
+        worker_queue_("ConfigHelper_worker_queue") {
     using testing::Invoke;
     using testing::StrEq;
 
@@ -125,6 +127,7 @@
     return &congestion_controller_;
   }
   BitrateAllocator* bitrate_allocator() { return &bitrate_allocator_; }
+  rtc::TaskQueue* worker_queue() { return &worker_queue_; }
 
   void SetupMockForSendTelephoneEvent() {
     EXPECT_TRUE(channel_proxy_);
@@ -181,6 +184,9 @@
   MockRtcEventLog event_log_;
   testing::NiceMock<MockLimitObserver> limit_observer_;
   BitrateAllocator bitrate_allocator_;
+  // |worker_queue| is defined last to ensure all pending tasks are cancelled
+  // and deleted before any other members.
+  rtc::TaskQueue worker_queue_;
 };
 }  // namespace
 
@@ -202,16 +208,16 @@
 
 TEST(AudioSendStreamTest, ConstructDestruct) {
   ConfigHelper helper;
-  internal::AudioSendStream send_stream(helper.config(), helper.audio_state(),
-                                        helper.congestion_controller(),
-                                        helper.bitrate_allocator());
+  internal::AudioSendStream send_stream(
+      helper.config(), helper.audio_state(), helper.worker_queue(),
+      helper.congestion_controller(), helper.bitrate_allocator());
 }
 
 TEST(AudioSendStreamTest, SendTelephoneEvent) {
   ConfigHelper helper;
-  internal::AudioSendStream send_stream(helper.config(), helper.audio_state(),
-                                        helper.congestion_controller(),
-                                        helper.bitrate_allocator());
+  internal::AudioSendStream send_stream(
+      helper.config(), helper.audio_state(), helper.worker_queue(),
+      helper.congestion_controller(), helper.bitrate_allocator());
   helper.SetupMockForSendTelephoneEvent();
   EXPECT_TRUE(send_stream.SendTelephoneEvent(kTelephoneEventPayloadType,
       kTelephoneEventCode, kTelephoneEventDuration));
@@ -219,18 +225,18 @@
 
 TEST(AudioSendStreamTest, SetMuted) {
   ConfigHelper helper;
-  internal::AudioSendStream send_stream(helper.config(), helper.audio_state(),
-                                        helper.congestion_controller(),
-                                        helper.bitrate_allocator());
+  internal::AudioSendStream send_stream(
+      helper.config(), helper.audio_state(), helper.worker_queue(),
+      helper.congestion_controller(), helper.bitrate_allocator());
   EXPECT_CALL(*helper.channel_proxy(), SetInputMute(true));
   send_stream.SetMuted(true);
 }
 
 TEST(AudioSendStreamTest, GetStats) {
   ConfigHelper helper;
-  internal::AudioSendStream send_stream(helper.config(), helper.audio_state(),
-                                        helper.congestion_controller(),
-                                        helper.bitrate_allocator());
+  internal::AudioSendStream send_stream(
+      helper.config(), helper.audio_state(), helper.worker_queue(),
+      helper.congestion_controller(), helper.bitrate_allocator());
   helper.SetupMockForGetStats();
   AudioSendStream::Stats stats = send_stream.GetStats();
   EXPECT_EQ(kSsrc, stats.local_ssrc);
@@ -257,9 +263,9 @@
 
 TEST(AudioSendStreamTest, GetStatsTypingNoiseDetected) {
   ConfigHelper helper;
-  internal::AudioSendStream send_stream(helper.config(), helper.audio_state(),
-                                        helper.congestion_controller(),
-                                        helper.bitrate_allocator());
+  internal::AudioSendStream send_stream(
+      helper.config(), helper.audio_state(), helper.worker_queue(),
+      helper.congestion_controller(), helper.bitrate_allocator());
   helper.SetupMockForGetStats();
   EXPECT_FALSE(send_stream.GetStats().typing_noise_detected);