Move ADM initialization into WebRtcVoiceEngine

Bug: webrtc:4690
Change-Id: I3b8950fdb13835964c5bf41162731eff5048bf1a
Reviewed-on: https://webrtc-review.googlesource.com/23820
Commit-Queue: Fredrik Solenberg <solenberg@webrtc.org>
Reviewed-by: Henrik Andreassson <henrika@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#20823}
diff --git a/media/engine/webrtcvoiceengine.cc b/media/engine/webrtcvoiceengine.cc
index 19619a3..9630bc4 100644
--- a/media/engine/webrtcvoiceengine.cc
+++ b/media/engine/webrtcvoiceengine.cc
@@ -28,6 +28,7 @@
 #include "media/engine/payload_type_mapper.h"
 #include "media/engine/webrtcmediaengine.h"
 #include "media/engine/webrtcvoe.h"
+#include "modules/audio_device/audio_device_impl.h"
 #include "modules/audio_mixer/audio_mixer_impl.h"
 #include "modules/audio_processing/aec_dump/aec_dump_factory.h"
 #include "modules/audio_processing/include/audio_processing.h"
@@ -251,6 +252,12 @@
   if (initialized_) {
     StopAecDump();
     voe_wrapper_->base()->Terminate();
+
+    // Stop AudioDevice.
+    adm()->StopPlayout();
+    adm()->StopRecording();
+    adm()->RegisterAudioCallback(nullptr);
+    adm()->Terminate();
   }
 }
 
@@ -283,15 +290,17 @@
 
   channel_config_.enable_voice_pacing = true;
 
-  RTC_CHECK_EQ(0,
-               voe_wrapper_->base()->Init(adm_.get(), apm(), decoder_factory_));
-
-  // No ADM supplied? Get the default one from VoE.
+#if defined(WEBRTC_INCLUDE_INTERNAL_AUDIO_DEVICE)
+  // No ADM supplied? Create a default one.
   if (!adm_) {
-    adm_ = voe_wrapper_->base()->audio_device_module();
+    adm_ = webrtc::AudioDeviceModule::Create(
+        webrtc::AudioDeviceModule::kPlatformDefaultAudio);
   }
-  RTC_DCHECK(adm_);
+#endif  // WEBRTC_INCLUDE_INTERNAL_AUDIO_DEVICE
+  RTC_CHECK(adm());
+  webrtc::adm_helpers::Init(adm());
 
+  RTC_CHECK_EQ(0, voe_wrapper_->base()->Init(adm(), apm(), decoder_factory_));
   transmit_mixer_ = voe_wrapper_->base()->transmit_mixer();
   RTC_DCHECK(transmit_mixer_);
 
@@ -324,15 +333,16 @@
 
   // Set default audio devices.
 #if !defined(WEBRTC_IOS)
-  webrtc::adm_helpers::SetRecordingDevice(adm_);
   apm()->Initialize();
-  webrtc::adm_helpers::SetPlayoutDevice(adm_);
 #endif  // !WEBRTC_IOS
 
   // May be null for VoE injected for testing.
   if (voe()->engine()) {
     audio_state_ = webrtc::AudioState::Create(
         MakeAudioStateConfig(voe(), audio_mixer_, apm_));
+
+    // Connect the ADM to our audio path.
+    adm()->RegisterAudioCallback(audio_state_->audio_transport());
   }
 
   initialized_ = true;
@@ -708,7 +718,7 @@
 webrtc::AudioDeviceModule* WebRtcVoiceEngine::adm() {
   RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
   RTC_DCHECK(adm_);
-  return adm_;
+  return adm_.get();
 }
 
 webrtc::AudioProcessing* WebRtcVoiceEngine::apm() const {