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/audio/audio_receive_stream_unittest.cc b/audio/audio_receive_stream_unittest.cc
index 4fdb68c..d6c2dbe 100644
--- a/audio/audio_receive_stream_unittest.cc
+++ b/audio/audio_receive_stream_unittest.cc
@@ -75,7 +75,6 @@
         audio_mixer_(new rtc::RefCountedObject<MockAudioMixer>()) {
     using testing::Invoke;
 
-    EXPECT_CALL(voice_engine_, audio_device_module());
     EXPECT_CALL(voice_engine_, audio_transport());
 
     AudioState::Config config;
diff --git a/audio/audio_send_stream_unittest.cc b/audio/audio_send_stream_unittest.cc
index 8e42029..c67eb9b 100644
--- a/audio/audio_send_stream_unittest.cc
+++ b/audio/audio_send_stream_unittest.cc
@@ -148,7 +148,6 @@
         audio_encoder_(nullptr) {
     using testing::Invoke;
 
-    EXPECT_CALL(voice_engine_, audio_device_module());
     EXPECT_CALL(voice_engine_, audio_transport());
 
     AudioState::Config config;
diff --git a/audio/audio_state.cc b/audio/audio_state.cc
index a1dd956..5a30c53 100644
--- a/audio/audio_state.cc
+++ b/audio/audio_state.cc
@@ -29,13 +29,6 @@
                              config_.audio_mixer) {
   process_thread_checker_.DetachFromThread();
   RTC_DCHECK(config_.audio_mixer);
-
-  auto* const device = voe_base_->audio_device_module();
-  RTC_DCHECK(device);
-
-  // This is needed for the Chrome implementation of RegisterAudioCallback.
-  device->RegisterAudioCallback(nullptr);
-  device->RegisterAudioCallback(&audio_transport_proxy_);
 }
 
 AudioState::~AudioState() {
diff --git a/audio/audio_state_unittest.cc b/audio/audio_state_unittest.cc
index 86be245..28b0a71 100644
--- a/audio/audio_state_unittest.cc
+++ b/audio/audio_state_unittest.cc
@@ -26,22 +26,9 @@
 
 struct ConfigHelper {
   ConfigHelper() : audio_mixer(AudioMixerImpl::Create()) {
-    EXPECT_CALL(mock_voice_engine, audio_device_module())
-        .Times(testing::AtLeast(1));
     EXPECT_CALL(mock_voice_engine, audio_transport())
         .WillRepeatedly(testing::Return(&audio_transport));
 
-    auto device = static_cast<MockAudioDeviceModule*>(
-        voice_engine().audio_device_module());
-
-    // Populate the audio transport proxy pointer to the most recent
-    // transport connected to the Audio Device.
-    ON_CALL(*device, RegisterAudioCallback(testing::_))
-        .WillByDefault(testing::Invoke([this](AudioTransport* transport) {
-          registered_audio_transport = transport;
-          return 0;
-        }));
-
     audio_state_config.voice_engine = &mock_voice_engine;
     audio_state_config.audio_mixer = audio_mixer;
     audio_state_config.audio_processing =
@@ -51,14 +38,12 @@
   MockVoiceEngine& voice_engine() { return mock_voice_engine; }
   rtc::scoped_refptr<AudioMixer> mixer() { return audio_mixer; }
   MockAudioTransport& original_audio_transport() { return audio_transport; }
-  AudioTransport* audio_transport_proxy() { return registered_audio_transport; }
 
  private:
   testing::StrictMock<MockVoiceEngine> mock_voice_engine;
   AudioState::Config audio_state_config;
   rtc::scoped_refptr<AudioMixer> audio_mixer;
   MockAudioTransport audio_transport;
-  AudioTransport* registered_audio_transport = nullptr;
 };
 
 class FakeAudioSource : public AudioMixer::Source {
@@ -112,7 +97,7 @@
                               kNumberOfChannels, kSampleRate, 0, 0, 0, false,
                               testing::Ref(new_mic_level)));
 
-  helper.audio_transport_proxy()->RecordedDataIsAvailable(
+  audio_state->audio_transport()->RecordedDataIsAvailable(
       nullptr, kSampleRate / 100, kBytesPerSample, kNumberOfChannels,
       kSampleRate, 0, 0, 0, false, new_mic_level);
 }
@@ -141,7 +126,7 @@
   size_t n_samples_out;
   int64_t elapsed_time_ms;
   int64_t ntp_time_ms;
-  helper.audio_transport_proxy()->NeedMorePlayData(
+  audio_state->audio_transport()->NeedMorePlayData(
       kSampleRate / 100, kBytesPerSample, kNumberOfChannels, kSampleRate,
       audio_buffer, n_samples_out, &elapsed_time_ms, &ntp_time_ms);
 }
diff --git a/call/call_perf_tests.cc b/call/call_perf_tests.cc
index 98ebe8b..a6fe0b2 100644
--- a/call/call_perf_tests.cc
+++ b/call/call_perf_tests.cc
@@ -176,6 +176,7 @@
     fake_audio_device = rtc::MakeUnique<FakeAudioDevice>(
         FakeAudioDevice::CreatePulsedNoiseCapturer(256, 48000),
         FakeAudioDevice::CreateDiscardRenderer(48000), audio_rtp_speed);
+    EXPECT_EQ(0, fake_audio_device->Init());
     EXPECT_EQ(0, voe_base->Init(fake_audio_device.get(), audio_processing.get(),
                                 decoder_factory_));
     VoEBase::ChannelConfig config;
@@ -189,9 +190,11 @@
     send_audio_state_config.audio_processing = audio_processing;
     Call::Config sender_config(event_log_.get());
 
-    sender_config.audio_state = AudioState::Create(send_audio_state_config);
+    auto audio_state = AudioState::Create(send_audio_state_config);
+    fake_audio_device->RegisterAudioCallback(audio_state->audio_transport());
+    sender_config.audio_state = audio_state;
     Call::Config receiver_config(event_log_.get());
-    receiver_config.audio_state = sender_config.audio_state;
+    receiver_config.audio_state = audio_state;
     CreateCalls(sender_config, receiver_config);
 
     std::copy_if(std::begin(payload_type_map_), std::end(payload_type_map_),
diff --git a/call/call_unittest.cc b/call/call_unittest.cc
index aeba5a8..7b16271 100644
--- a/call/call_unittest.cc
+++ b/call/call_unittest.cc
@@ -41,7 +41,6 @@
     audio_state_config.voice_engine = &voice_engine_;
     audio_state_config.audio_mixer = webrtc::AudioMixerImpl::Create();
     audio_state_config.audio_processing = webrtc::AudioProcessing::Create();
-    EXPECT_CALL(voice_engine_, audio_device_module());
     EXPECT_CALL(voice_engine_, audio_transport());
     webrtc::Call::Config config(&event_log_);
     config.audio_state = webrtc::AudioState::Create(audio_state_config);
diff --git a/media/engine/adm_helpers.cc b/media/engine/adm_helpers.cc
index b973ce6..119cc64 100644
--- a/media/engine/adm_helpers.cc
+++ b/media/engine/adm_helpers.cc
@@ -32,94 +32,50 @@
 #define AUDIO_DEVICE_ID (0u)
 #endif  // defined(WEBRTC_WIN)
 
-void SetRecordingDevice(AudioDeviceModule* adm) {
+void Init(AudioDeviceModule* adm) {
   RTC_DCHECK(adm);
 
-  // Save recording status and stop recording.
-  const bool was_recording = adm->Recording();
-  if (was_recording && adm->StopRecording() != 0) {
-    RTC_LOG(LS_ERROR) << "Unable to stop recording.";
-    return;
-  }
+  RTC_CHECK_EQ(0, adm->Init()) << "Failed to initialize the ADM.";
 
-  // Set device to default.
-  if (adm->SetRecordingDevice(AUDIO_DEVICE_ID) != 0) {
-    RTC_LOG(LS_ERROR) << "Unable to set recording device.";
-    return;
-  }
-
-  // Init microphone, so user can do volume settings etc.
-  if (adm->InitMicrophone() != 0) {
-    RTC_LOG(LS_ERROR) << "Unable to access microphone.";
-  }
-
-  // Set number of channels
-  bool available = false;
-  if (adm->StereoRecordingIsAvailable(&available) != 0) {
-    RTC_LOG(LS_ERROR) << "Failed to query stereo recording.";
-  }
-  if (adm->SetStereoRecording(available) != 0) {
-    RTC_LOG(LS_ERROR) << "Failed to set stereo recording mode.";
-  }
-
-  // Restore recording if it was enabled already when calling this function.
-  if (was_recording) {
-    if (adm->InitRecording() != 0) {
-      RTC_LOG(LS_ERROR) << "Failed to initialize recording.";
+  // Playout device.
+  {
+    if (adm->SetPlayoutDevice(AUDIO_DEVICE_ID) != 0) {
+      RTC_LOG(LS_ERROR) << "Unable to set playout device.";
       return;
     }
-    if (adm->StartRecording() != 0) {
-      RTC_LOG(LS_ERROR) << "Failed to start recording.";
-      return;
+    if (adm->InitSpeaker() != 0) {
+      RTC_LOG(LS_ERROR) << "Unable to access speaker.";
+    }
+
+    // Set number of channels
+    bool available = false;
+    if (adm->StereoPlayoutIsAvailable(&available) != 0) {
+      RTC_LOG(LS_ERROR) << "Failed to query stereo playout.";
+    }
+    if (adm->SetStereoPlayout(available) != 0) {
+      RTC_LOG(LS_ERROR) << "Failed to set stereo playout mode.";
     }
   }
 
-  RTC_LOG(LS_INFO) << "Set recording device.";
+  // Recording device.
+  {
+    if (adm->SetRecordingDevice(AUDIO_DEVICE_ID) != 0) {
+      RTC_LOG(LS_ERROR) << "Unable to set recording device.";
+      return;
+    }
+    if (adm->InitMicrophone() != 0) {
+      RTC_LOG(LS_ERROR) << "Unable to access microphone.";
+    }
+
+    // Set number of channels
+    bool available = false;
+    if (adm->StereoRecordingIsAvailable(&available) != 0) {
+      RTC_LOG(LS_ERROR) << "Failed to query stereo recording.";
+    }
+    if (adm->SetStereoRecording(available) != 0) {
+      RTC_LOG(LS_ERROR) << "Failed to set stereo recording mode.";
+    }
+  }
 }
-
-void SetPlayoutDevice(AudioDeviceModule* adm) {
-  RTC_DCHECK(adm);
-
-  // Save playing status and stop playout.
-  const bool was_playing = adm->Playing();
-  if (was_playing && adm->StopPlayout() != 0) {
-    RTC_LOG(LS_ERROR) << "Unable to stop playout.";
-  }
-
-  // Set device.
-  if (adm->SetPlayoutDevice(AUDIO_DEVICE_ID) != 0) {
-    RTC_LOG(LS_ERROR) << "Unable to set playout device.";
-    return;
-  }
-
-  // Init speaker, so user can do volume settings etc.
-  if (adm->InitSpeaker() != 0) {
-    RTC_LOG(LS_ERROR) << "Unable to access speaker.";
-  }
-
-  // Set number of channels
-  bool available = false;
-  if (adm->StereoPlayoutIsAvailable(&available) != 0) {
-    RTC_LOG(LS_ERROR) << "Failed to query stereo playout.";
-  }
-  if (adm->SetStereoPlayout(available) != 0) {
-    RTC_LOG(LS_ERROR) << "Failed to set stereo playout mode.";
-  }
-
-  // Restore recording if it was enabled already when calling this function.
-  if (was_playing) {
-    if (adm->InitPlayout() != 0) {
-      RTC_LOG(LS_ERROR) << "Failed to initialize playout.";
-      return;
-    }
-    if (adm->StartPlayout() != 0) {
-      RTC_LOG(LS_ERROR) << "Failed to start playout.";
-      return;
-    }
-  }
-
-  RTC_LOG(LS_INFO) << "Set playout device.";
-}
-
 }  // namespace adm_helpers
 }  // namespace webrtc
diff --git a/media/engine/adm_helpers.h b/media/engine/adm_helpers.h
index d04db17..c6ea3a2 100644
--- a/media/engine/adm_helpers.h
+++ b/media/engine/adm_helpers.h
@@ -19,8 +19,7 @@
 
 namespace adm_helpers {
 
-void SetRecordingDevice(AudioDeviceModule* adm);
-void SetPlayoutDevice(AudioDeviceModule* adm);
+void Init(AudioDeviceModule* adm);
 
 }  // namespace adm_helpers
 }  // namespace webrtc
diff --git a/media/engine/fakewebrtcvoiceengine.h b/media/engine/fakewebrtcvoiceengine.h
index 55d3100..9c9a428 100644
--- a/media/engine/fakewebrtcvoiceengine.h
+++ b/media/engine/fakewebrtcvoiceengine.h
@@ -69,9 +69,6 @@
     inited_ = false;
     return 0;
   }
-  webrtc::AudioDeviceModule* audio_device_module() override {
-    return nullptr;
-  }
   webrtc::voe::TransmitMixer* transmit_mixer() override {
     return transmit_mixer_;
   }
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 {
diff --git a/media/engine/webrtcvoiceengine_unittest.cc b/media/engine/webrtcvoiceengine_unittest.cc
index ef56473..f178262 100644
--- a/media/engine/webrtcvoiceengine_unittest.cc
+++ b/media/engine/webrtcvoiceengine_unittest.cc
@@ -85,23 +85,10 @@
 
 void AdmSetupExpectations(webrtc::test::MockAudioDeviceModule* adm) {
   RTC_DCHECK(adm);
+
+  // Setup.
   EXPECT_CALL(*adm, AddRef()).Times(1);
-  EXPECT_CALL(*adm, Release())
-      .WillOnce(Return(rtc::RefCountReleaseStatus::kDroppedLastRef));
-#if !defined(WEBRTC_IOS)
-  EXPECT_CALL(*adm, Recording()).WillOnce(Return(false));
-#if defined(WEBRTC_WIN)
-  EXPECT_CALL(*adm, SetRecordingDevice(
-      testing::Matcher<webrtc::AudioDeviceModule::WindowsDeviceType>(
-          webrtc::AudioDeviceModule::kDefaultCommunicationDevice)))
-              .WillOnce(Return(0));
-#else
-  EXPECT_CALL(*adm, SetRecordingDevice(0)).WillOnce(Return(0));
-#endif  // #if defined(WEBRTC_WIN)
-  EXPECT_CALL(*adm, InitMicrophone()).WillOnce(Return(0));
-  EXPECT_CALL(*adm, StereoRecordingIsAvailable(testing::_)).WillOnce(Return(0));
-  EXPECT_CALL(*adm, SetStereoRecording(false)).WillOnce(Return(0));
-  EXPECT_CALL(*adm, Playing()).WillOnce(Return(false));
+  EXPECT_CALL(*adm, Init()).WillOnce(Return(0));
 #if defined(WEBRTC_WIN)
   EXPECT_CALL(*adm, SetPlayoutDevice(
       testing::Matcher<webrtc::AudioDeviceModule::WindowsDeviceType>(
@@ -113,11 +100,29 @@
   EXPECT_CALL(*adm, InitSpeaker()).WillOnce(Return(0));
   EXPECT_CALL(*adm, StereoPlayoutIsAvailable(testing::_)).WillOnce(Return(0));
   EXPECT_CALL(*adm, SetStereoPlayout(false)).WillOnce(Return(0));
-#endif  // #if !defined(WEBRTC_IOS)
+#if defined(WEBRTC_WIN)
+  EXPECT_CALL(*adm, SetRecordingDevice(
+      testing::Matcher<webrtc::AudioDeviceModule::WindowsDeviceType>(
+          webrtc::AudioDeviceModule::kDefaultCommunicationDevice)))
+              .WillOnce(Return(0));
+#else
+  EXPECT_CALL(*adm, SetRecordingDevice(0)).WillOnce(Return(0));
+#endif  // #if defined(WEBRTC_WIN)
+  EXPECT_CALL(*adm, InitMicrophone()).WillOnce(Return(0));
+  EXPECT_CALL(*adm, StereoRecordingIsAvailable(testing::_)).WillOnce(Return(0));
+  EXPECT_CALL(*adm, SetStereoRecording(false)).WillOnce(Return(0));
   EXPECT_CALL(*adm, BuiltInAECIsAvailable()).WillOnce(Return(false));
   EXPECT_CALL(*adm, BuiltInAGCIsAvailable()).WillOnce(Return(false));
   EXPECT_CALL(*adm, BuiltInNSIsAvailable()).WillOnce(Return(false));
   EXPECT_CALL(*adm, SetAGC(true)).WillOnce(Return(0));
+
+  // Teardown.
+  EXPECT_CALL(*adm, StopPlayout()).WillOnce(Return(0));
+  EXPECT_CALL(*adm, StopRecording()).WillOnce(Return(0));
+  EXPECT_CALL(*adm, RegisterAudioCallback(nullptr)).WillOnce(Return(0));
+  EXPECT_CALL(*adm, Terminate()).WillOnce(Return(0));
+  EXPECT_CALL(*adm, Release())
+      .WillOnce(Return(rtc::RefCountReleaseStatus::kDroppedLastRef));
 }
 }  // namespace
 
diff --git a/test/call_test.cc b/test/call_test.cc
index f4b3877..3210e64 100644
--- a/test/call_test.cc
+++ b/test/call_test.cc
@@ -75,6 +75,8 @@
       audio_state_config.audio_mixer = AudioMixerImpl::Create();
       audio_state_config.audio_processing = apm_send_;
       send_config.audio_state = AudioState::Create(audio_state_config);
+      fake_send_audio_device_->RegisterAudioCallback(
+          send_config.audio_state->audio_transport());
     }
     CreateSenderCall(send_config);
     if (sender_call_transport_controller_ != nullptr) {
@@ -89,7 +91,8 @@
         audio_state_config.audio_mixer = AudioMixerImpl::Create();
         audio_state_config.audio_processing = apm_recv_;
         recv_config.audio_state = AudioState::Create(audio_state_config);
-      }
+        fake_recv_audio_device_->RegisterAudioCallback(
+            recv_config.audio_state->audio_transport());      }
       CreateReceiverCall(recv_config);
     }
     test->OnCallsCreated(sender_call_.get(), receiver_call_.get());
@@ -427,6 +430,7 @@
 void CallTest::CreateVoiceEngines() {
   voe_send_.voice_engine = VoiceEngine::Create();
   voe_send_.base = VoEBase::GetInterface(voe_send_.voice_engine);
+  EXPECT_EQ(0, fake_send_audio_device_->Init());
   EXPECT_EQ(0, voe_send_.base->Init(fake_send_audio_device_.get(),
                                     apm_send_.get(), decoder_factory_));
   VoEBase::ChannelConfig config;
@@ -436,6 +440,7 @@
 
   voe_recv_.voice_engine = VoiceEngine::Create();
   voe_recv_.base = VoEBase::GetInterface(voe_recv_.voice_engine);
+  EXPECT_EQ(0, fake_recv_audio_device_->Init());
   EXPECT_EQ(0, voe_recv_.base->Init(fake_recv_audio_device_.get(),
                                     apm_recv_.get(), decoder_factory_));
   voe_recv_.channel_id = voe_recv_.base->CreateChannel();
diff --git a/test/fake_audio_device.cc b/test/fake_audio_device.cc
index fa3f1f4..eef8bfd 100644
--- a/test/fake_audio_device.cc
+++ b/test/fake_audio_device.cc
@@ -303,13 +303,9 @@
 }
 
 int32_t FakeAudioDevice::Init() {
-  // TODO(solenberg): Temporarily allow multiple init calls.
-  if (!inited_) {
-    RTC_CHECK(tick_->StartTimer(true, kFrameLengthMs / speed_));
-    thread_.Start();
-    thread_.SetPriority(rtc::kHighPriority);
-    inited_ = true;
-  }
+  RTC_CHECK(tick_->StartTimer(true, kFrameLengthMs / speed_));
+  thread_.Start();
+  thread_.SetPriority(rtc::kHighPriority);
   return 0;
 }
 
diff --git a/test/fake_audio_device.h b/test/fake_audio_device.h
index 4d90619..f71fd01 100644
--- a/test/fake_audio_device.h
+++ b/test/fake_audio_device.h
@@ -137,7 +137,6 @@
 
   std::unique_ptr<EventTimerWrapper> tick_;
   rtc::PlatformThread thread_;
-  bool inited_ = false;
 };
 }  // namespace test
 }  // namespace webrtc
diff --git a/test/mock_voice_engine.h b/test/mock_voice_engine.h
index 67bc993..7fec120 100644
--- a/test/mock_voice_engine.h
+++ b/test/mock_voice_engine.h
@@ -13,7 +13,6 @@
 
 #include <memory>
 
-#include "modules/audio_device/include/mock_audio_device.h"
 #include "modules/audio_device/include/mock_audio_transport.h"
 #include "modules/rtp_rtcp/mocks/mock_rtp_rtcp.h"
 #include "test/gmock.h"
@@ -63,8 +62,6 @@
           return proxy;
         }));
 
-    ON_CALL(*this, audio_device_module())
-        .WillByDefault(testing::Return(&mock_audio_device_));
     ON_CALL(*this, audio_transport())
         .WillByDefault(testing::Return(&mock_audio_transport_));
   }
@@ -97,7 +94,6 @@
       int(AudioDeviceModule* external_adm,
           AudioProcessing* external_apm,
           const rtc::scoped_refptr<AudioDecoderFactory>& decoder_factory));
-  MOCK_METHOD0(audio_device_module, AudioDeviceModule*());
   MOCK_METHOD0(transmit_mixer, voe::TransmitMixer*());
   MOCK_METHOD0(Terminate, int());
   MOCK_METHOD0(CreateChannel, int());
@@ -120,7 +116,6 @@
 
   std::map<int, std::unique_ptr<MockRtpRtcp>> mock_rtp_rtcps_;
 
-  MockAudioDeviceModule mock_audio_device_;
   MockAudioTransport mock_audio_transport_;
 };
 }  // namespace test
diff --git a/video/video_quality_test.cc b/video/video_quality_test.cc
index 512a9bb..e40d295 100644
--- a/video/video_quality_test.cc
+++ b/video/video_quality_test.cc
@@ -92,11 +92,13 @@
 
 void CreateVoiceEngine(
     VoiceEngineState* voe,
+    webrtc::AudioDeviceModule* adm,
     webrtc::AudioProcessing* apm,
     rtc::scoped_refptr<webrtc::AudioDecoderFactory> decoder_factory) {
   voe->voice_engine = webrtc::VoiceEngine::Create();
   voe->base = webrtc::VoEBase::GetInterface(voe->voice_engine);
-  EXPECT_EQ(0, voe->base->Init(nullptr, apm, decoder_factory));
+  EXPECT_EQ(0, adm->Init());
+  EXPECT_EQ(0, voe->base->Init(adm, apm, decoder_factory));
   webrtc::VoEBase::ChannelConfig config;
   config.enable_voice_pacing = true;
   voe->send_channel_id = voe->base->CreateChannel(config);
@@ -1968,6 +1970,7 @@
 void VideoQualityTest::RunWithRenderers(const Params& params) {
   std::unique_ptr<test::LayerFilteringTransport> send_transport;
   std::unique_ptr<test::DirectTransport> recv_transport;
+  std::unique_ptr<test::FakeAudioDevice> fake_audio_device;
   ::VoiceEngineState voe;
   std::unique_ptr<test::VideoRenderer> local_preview;
   std::vector<std::unique_ptr<test::VideoRenderer>> loopback_renderers;
@@ -1982,16 +1985,24 @@
     Call::Config call_config(event_log_.get());
     call_config.bitrate_config = params_.call.call_bitrate_config;
 
+    fake_audio_device.reset(new test::FakeAudioDevice(
+        test::FakeAudioDevice::CreatePulsedNoiseCapturer(32000, 48000),
+        test::FakeAudioDevice::CreateDiscardRenderer(48000),
+        1.f));
+
     rtc::scoped_refptr<webrtc::AudioProcessing> audio_processing(
         webrtc::AudioProcessing::Create());
 
     if (params_.audio.enabled) {
-      CreateVoiceEngine(&voe, audio_processing.get(), decoder_factory_);
+      CreateVoiceEngine(&voe, fake_audio_device.get(), audio_processing.get(),
+                        decoder_factory_);
       AudioState::Config audio_state_config;
       audio_state_config.voice_engine = voe.voice_engine;
       audio_state_config.audio_mixer = AudioMixerImpl::Create();
       audio_state_config.audio_processing = audio_processing;
       call_config.audio_state = AudioState::Create(audio_state_config);
+      fake_audio_device->RegisterAudioCallback(
+          call_config.audio_state->audio_transport());
     }
 
     CreateCalls(call_config, call_config);
diff --git a/voice_engine/include/voe_base.h b/voice_engine/include/voe_base.h
index 06a8cf3..2f74fcc 100644
--- a/voice_engine/include/voe_base.h
+++ b/voice_engine/include/voe_base.h
@@ -92,15 +92,10 @@
   // functionality in a separate (reference counted) module.
   // - The AudioProcessing module handles capture-side processing.
   // - An AudioDecoderFactory - used to create audio decoders.
-  // If NULL is passed for ADM, VoiceEngine
-  // will create its own. Returns -1 in case of an error, 0 otherwise.
   virtual int Init(
-      AudioDeviceModule* external_adm,
-      AudioProcessing* external_apm,
+      AudioDeviceModule* audio_device,
+      AudioProcessing* audio_processing,
       const rtc::scoped_refptr<AudioDecoderFactory>& decoder_factory) = 0;
-  // This method is WIP - DO NOT USE!
-  // Returns NULL before Init() is called.
-  virtual AudioDeviceModule* audio_device_module() = 0;
 
   // This method is WIP - DO NOT USE!
   // Returns NULL before Init() is called.
diff --git a/voice_engine/voe_base_impl.cc b/voice_engine/voe_base_impl.cc
index 9848557..695b06d 100644
--- a/voice_engine/voe_base_impl.cc
+++ b/voice_engine/voe_base_impl.cc
@@ -142,91 +142,17 @@
 }
 
 int VoEBaseImpl::Init(
-    AudioDeviceModule* external_adm,
+    AudioDeviceModule* audio_device,
     AudioProcessing* audio_processing,
     const rtc::scoped_refptr<AudioDecoderFactory>& decoder_factory) {
+  RTC_DCHECK(audio_device);
   RTC_DCHECK(audio_processing);
   rtc::CritScope cs(shared_->crit_sec());
-  WebRtcSpl_Init();
   if (shared_->process_thread()) {
     shared_->process_thread()->Start();
   }
 
-  // Create an internal ADM if the user has not added an external
-  // ADM implementation as input to Init().
-  if (external_adm == nullptr) {
-#if !defined(WEBRTC_INCLUDE_INTERNAL_AUDIO_DEVICE)
-    return -1;
-#else
-    // Create the internal ADM implementation.
-    shared_->set_audio_device(AudioDeviceModule::Create(
-        AudioDeviceModule::kPlatformDefaultAudio));
-    if (shared_->audio_device() == nullptr) {
-      RTC_LOG(LS_ERROR) << "Init() failed to create the ADM";
-      return -1;
-    }
-#endif  // WEBRTC_INCLUDE_INTERNAL_AUDIO_DEVICE
-  } else {
-    // Use the already existing external ADM implementation.
-    shared_->set_audio_device(external_adm);
-    RTC_LOG_F(LS_INFO)
-        << "An external ADM implementation will be used in VoiceEngine";
-  }
-
-  bool available = false;
-
-  // --------------------
-  // Reinitialize the ADM
-
-  // Register the AudioTransport implementation
-  if (shared_->audio_device()->RegisterAudioCallback(this) != 0) {
-    RTC_LOG(LS_ERROR) << "Init() failed to register audio callback for the ADM";
-  }
-
-  // ADM initialization
-  if (shared_->audio_device()->Init() != 0) {
-    RTC_LOG(LS_ERROR) << "Init() failed to initialize the ADM";
-    return -1;
-  }
-
-  // Initialize the default speaker
-  if (shared_->audio_device()->SetPlayoutDevice(
-          WEBRTC_VOICE_ENGINE_DEFAULT_DEVICE) != 0) {
-    RTC_LOG(LS_ERROR) << "Init() failed to set the default output device";
-  }
-  if (shared_->audio_device()->InitSpeaker() != 0) {
-    RTC_LOG(LS_ERROR) << "Init() failed to initialize the speaker";
-  }
-
-  // Initialize the default microphone
-  if (shared_->audio_device()->SetRecordingDevice(
-          WEBRTC_VOICE_ENGINE_DEFAULT_DEVICE) != 0) {
-    RTC_LOG(LS_ERROR) << "Init() failed to set the default input device";
-  }
-  if (shared_->audio_device()->InitMicrophone() != 0) {
-    RTC_LOG(LS_ERROR) << "Init() failed to initialize the microphone";
-  }
-
-  // Set number of channels
-  if (shared_->audio_device()->StereoPlayoutIsAvailable(&available) != 0) {
-    RTC_LOG(LS_ERROR) << "Init() failed to query stereo playout mode";
-  }
-  if (shared_->audio_device()->SetStereoPlayout(available) != 0) {
-    RTC_LOG(LS_ERROR) << "Init() failed to set mono/stereo playout mode";
-  }
-
-  // TODO(andrew): These functions don't tell us whether stereo recording
-  // is truly available. We simply set the AudioProcessing input to stereo
-  // here, because we have to wait until receiving the first frame to
-  // determine the actual number of channels anyway.
-  //
-  // These functions may be changed; tracked here:
-  // http://code.google.com/p/webrtc/issues/detail?id=204
-  shared_->audio_device()->StereoRecordingIsAvailable(&available);
-  if (shared_->audio_device()->SetStereoRecording(available) != 0) {
-    RTC_LOG(LS_ERROR) << "Init() failed to set mono/stereo recording mode";
-  }
-
+  shared_->set_audio_device(audio_device);
   shared_->set_audio_processing(audio_processing);
 
   // Configure AudioProcessing components.
@@ -518,23 +444,7 @@
     shared_->process_thread()->Stop();
   }
 
-  if (shared_->audio_device()) {
-    if (shared_->audio_device()->StopPlayout() != 0) {
-      RTC_LOG(LS_ERROR) << "TerminateInternal() failed to stop playout";
-    }
-    if (shared_->audio_device()->StopRecording() != 0) {
-      RTC_LOG(LS_ERROR) << "TerminateInternal() failed to stop recording";
-    }
-    if (shared_->audio_device()->RegisterAudioCallback(nullptr) != 0) {
-      RTC_LOG(LS_ERROR) << "TerminateInternal() failed to de-register audio "
-                           "callback for the ADM";
-    }
-    if (shared_->audio_device()->Terminate() != 0) {
-      RTC_LOG(LS_ERROR) << "TerminateInternal() failed to terminate the ADM";
-    }
-    shared_->set_audio_device(nullptr);
-  }
-
+  shared_->set_audio_device(nullptr);
   shared_->set_audio_processing(nullptr);
 
   return 0;
diff --git a/voice_engine/voe_base_impl.h b/voice_engine/voe_base_impl.h
index e647124..4cd6c86 100644
--- a/voice_engine/voe_base_impl.h
+++ b/voice_engine/voe_base_impl.h
@@ -25,12 +25,9 @@
                     public AudioTransport {
  public:
   int Init(
-      AudioDeviceModule* external_adm,
+      AudioDeviceModule* audio_device,
       AudioProcessing* audio_processing,
       const rtc::scoped_refptr<AudioDecoderFactory>& decoder_factory) override;
-  AudioDeviceModule* audio_device_module() override {
-    return shared_->audio_device();
-  }
   voe::TransmitMixer* transmit_mixer() override {
     return shared_->transmit_mixer();
   }
diff --git a/voice_engine/voice_engine_defines.h b/voice_engine/voice_engine_defines.h
index 4397662..35f9492 100644
--- a/voice_engine/voice_engine_defines.h
+++ b/voice_engine/voice_engine_defines.h
@@ -47,23 +47,4 @@
 
 }  // namespace webrtc
 
-namespace webrtc {
-
-inline int VoEId(int veId, int chId) {
-  if (chId == -1) {
-    const int dummyChannel(99);
-    return (int)((veId << 16) + dummyChannel);
-  }
-  return (int)((veId << 16) + chId);
-}
-
-}  // namespace webrtc
-
-#if defined(_WIN32)
-#define WEBRTC_VOICE_ENGINE_DEFAULT_DEVICE \
-  AudioDeviceModule::kDefaultCommunicationDevice
-#else
-#define WEBRTC_VOICE_ENGINE_DEFAULT_DEVICE 0
-#endif  // #if (defined(_WIN32)
-
 #endif  // VOICE_ENGINE_VOICE_ENGINE_DEFINES_H_