Move ownership of voe::Channel into Audio[Receive|Send]Stream.

* VoEBase contains only stub methods (until downstream code is
  updated).

* voe::Channel and ChannelProxy classes remain, but are now created
  internally to the streams. As a result,
  internal::Audio[Receive|Send]Stream can have a ChannelProxy injected
  for testing.

* Stream classes share Call::module_process_thread_ for their RtpRtcp
  modules, rather than using a separate thread shared only among audio
  streams.

* voe::Channel instances use Call::worker_queue_ for encoding packets,
  rather than having a separate queue for audio (send) streams.

Bug: webrtc:4690
Change-Id: I8059ef224ad13aa0a6ded2cafc52599c7f64d68d
Reviewed-on: https://webrtc-review.googlesource.com/34640
Commit-Queue: Fredrik Solenberg <solenberg@webrtc.org>
Reviewed-by: Oskar Sundbom <ossu@webrtc.org>
Reviewed-by: Henrik Andreassson <henrika@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#21578}
diff --git a/audio/audio_receive_stream.cc b/audio/audio_receive_stream.cc
index 3c53818..bfa125a 100644
--- a/audio/audio_receive_stream.cc
+++ b/audio/audio_receive_stream.cc
@@ -25,8 +25,6 @@
 #include "rtc_base/logging.h"
 #include "rtc_base/timeutils.h"
 #include "voice_engine/channel_proxy.h"
-#include "voice_engine/include/voe_base.h"
-#include "voice_engine/voice_engine_impl.h"
 
 namespace webrtc {
 
@@ -53,7 +51,6 @@
   ss << "{rtp: " << rtp.ToString();
   ss << ", rtcp_send_transport: "
      << (rtcp_send_transport ? "(Transport)" : "null");
-  ss << ", voe_channel_id: " << voe_channel_id;
   if (!sync_group.empty()) {
     ss << ", sync_group: " << sync_group;
   }
@@ -62,32 +59,58 @@
 }
 
 namespace internal {
+namespace {
+std::unique_ptr<voe::ChannelProxy> CreateChannelAndProxy(
+    webrtc::AudioState* audio_state,
+    ProcessThread* module_process_thread,
+    const webrtc::AudioReceiveStream::Config& config) {
+  RTC_DCHECK(audio_state);
+  internal::AudioState* internal_audio_state =
+      static_cast<internal::AudioState*>(audio_state);
+  return std::unique_ptr<voe::ChannelProxy>(new voe::ChannelProxy(
+      std::unique_ptr<voe::Channel>(new voe::Channel(
+              module_process_thread,
+              internal_audio_state->audio_device_module(),
+              config.jitter_buffer_max_packets,
+              config.jitter_buffer_fast_accelerate,
+              config.decoder_factory))));
+}
+}  // namespace
+
+AudioReceiveStream::AudioReceiveStream(
+    RtpStreamReceiverControllerInterface* receiver_controller,
+    PacketRouter* packet_router,
+    ProcessThread* module_process_thread,
+    const webrtc::AudioReceiveStream::Config& config,
+    const rtc::scoped_refptr<webrtc::AudioState>& audio_state,
+    webrtc::RtcEventLog* event_log)
+    : AudioReceiveStream(receiver_controller,
+                         packet_router,
+                         config,
+                         audio_state,
+                         event_log,
+                         CreateChannelAndProxy(audio_state.get(),
+                                               module_process_thread,
+                                               config)) {}
+
 AudioReceiveStream::AudioReceiveStream(
     RtpStreamReceiverControllerInterface* receiver_controller,
     PacketRouter* packet_router,
     const webrtc::AudioReceiveStream::Config& config,
     const rtc::scoped_refptr<webrtc::AudioState>& audio_state,
-    webrtc::RtcEventLog* event_log)
-    : audio_state_(audio_state) {
+    webrtc::RtcEventLog* event_log,
+    std::unique_ptr<voe::ChannelProxy> channel_proxy)
+    : audio_state_(audio_state),
+      channel_proxy_(std::move(channel_proxy)) {
   RTC_LOG(LS_INFO) << "AudioReceiveStream: " << config.ToString();
-  RTC_DCHECK_NE(config.voe_channel_id, -1);
-  RTC_DCHECK(audio_state_.get());
+  RTC_DCHECK(receiver_controller);
   RTC_DCHECK(packet_router);
+  RTC_DCHECK(config.decoder_factory);
+  RTC_DCHECK(audio_state_);
+  RTC_DCHECK(channel_proxy_);
 
   module_process_thread_checker_.DetachFromThread();
 
-  VoiceEngineImpl* voe_impl = static_cast<VoiceEngineImpl*>(voice_engine());
-  channel_proxy_ = voe_impl->GetChannelProxy(config.voe_channel_id);
-
-  // TODO(ossu): This is where we'd like to set the decoder factory to
-  // use. However, since it needs to be included when constructing Channel, we
-  // cannot do that until we're able to move Channel ownership into the
-  // Audio{Send,Receive}Streams.  The best we can do is check that we're not
-  // trying to use two different factories using the different interfaces.
-  RTC_CHECK(config.decoder_factory);
-  RTC_CHECK_EQ(config.decoder_factory,
-               channel_proxy_->GetAudioDecoderFactory());
-
   channel_proxy_->SetRtcEventLog(event_log);
   channel_proxy_->RegisterTransport(config.rtcp_send_transport);
 
@@ -204,9 +227,9 @@
   return channel_proxy_->GetSpeechOutputLevel();
 }
 
-void AudioReceiveStream::SetSink(std::unique_ptr<AudioSinkInterface> sink) {
+void AudioReceiveStream::SetSink(AudioSinkInterface* sink) {
   RTC_DCHECK_RUN_ON(&worker_thread_checker_);
-  channel_proxy_->SetSink(std::move(sink));
+  channel_proxy_->SetSink(sink);
 }
 
 void AudioReceiveStream::SetGain(float gain) {
@@ -278,13 +301,11 @@
 void AudioReceiveStream::AssociateSendStream(AudioSendStream* send_stream) {
   RTC_DCHECK_RUN_ON(&worker_thread_checker_);
   if (send_stream) {
-    VoiceEngineImpl* voe_impl = static_cast<VoiceEngineImpl*>(voice_engine());
-    std::unique_ptr<voe::ChannelProxy> send_channel_proxy =
-        voe_impl->GetChannelProxy(send_stream->GetConfig().voe_channel_id);
-    channel_proxy_->AssociateSendChannel(*send_channel_proxy.get());
+    channel_proxy_->AssociateSendChannel(send_stream->GetChannelProxy());
   } else {
     channel_proxy_->DisassociateSendChannel();
   }
+  associated_send_stream_ = send_stream;
 }
 
 void AudioReceiveStream::SignalNetworkState(NetworkState state) {
@@ -312,10 +333,10 @@
   return config_;
 }
 
-VoiceEngine* AudioReceiveStream::voice_engine() const {
-  auto* voice_engine = audio_state()->voice_engine();
-  RTC_DCHECK(voice_engine);
-  return voice_engine;
+const AudioSendStream*
+    AudioReceiveStream::GetAssociatedSendStreamForTesting() const {
+  RTC_DCHECK_RUN_ON(&worker_thread_checker_);
+  return associated_send_stream_;
 }
 
 internal::AudioState* AudioReceiveStream::audio_state() const {
@@ -346,8 +367,6 @@
 
   // Configuration parameters which cannot be changed.
   RTC_DCHECK(first_time ||
-             old_config.voe_channel_id == new_config.voe_channel_id);
-  RTC_DCHECK(first_time ||
              old_config.rtp.remote_ssrc == new_config.rtp.remote_ssrc);
   RTC_DCHECK(first_time ||
              old_config.rtcp_send_transport == new_config.rtcp_send_transport);