blob: ff651d4c90d1ffcde02d19c5f3c33484096a5989 [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
xians@webrtc.org20aabbb2012-02-20 09:17:41 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
niklase@google.com470e71d2011-07-07 08:21:25 +00003 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "modules/audio_device/audio_device_impl.h"
henrika4af73662017-10-11 13:16:17 +020012
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020013#include "modules/audio_device/audio_device_config.h"
14#include "modules/audio_device/audio_device_generic.h"
15#include "rtc_base/checks.h"
16#include "rtc_base/logging.h"
17#include "rtc_base/refcount.h"
Niels Möller84255bb2017-10-06 13:43:23 +020018#include "rtc_base/refcountedobject.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#include "system_wrappers/include/metrics.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000020
niklase@google.com470e71d2011-07-07 08:21:25 +000021#if defined(_WIN32)
Max Morin787eeed2016-06-23 10:42:07 +020022#if defined(WEBRTC_WINDOWS_CORE_AUDIO_BUILD)
23#include "audio_device_core_win.h"
24#endif
leozwang@google.com39f20512011-07-15 16:29:40 +000025#elif defined(WEBRTC_ANDROID)
henrikab2619892015-05-18 16:49:16 +020026#include <stdlib.h>
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020027#include "modules/audio_device/android/audio_device_template.h"
28#include "modules/audio_device/android/audio_manager.h"
29#include "modules/audio_device/android/audio_record_jni.h"
30#include "modules/audio_device/android/audio_track_jni.h"
31#include "modules/audio_device/android/opensles_player.h"
32#include "modules/audio_device/android/opensles_recorder.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000033#elif defined(WEBRTC_LINUX)
Max Morin787eeed2016-06-23 10:42:07 +020034#if defined(LINUX_ALSA)
35#include "audio_device_alsa_linux.h"
36#endif
Tommi68898a22015-05-19 17:28:07 +020037#if defined(LINUX_PULSE)
Max Morin787eeed2016-06-23 10:42:07 +020038#include "audio_device_pulse_linux.h"
Tommi68898a22015-05-19 17:28:07 +020039#endif
sjlee@webrtc.org414fa7f2012-09-11 17:25:46 +000040#elif defined(WEBRTC_IOS)
Max Morin787eeed2016-06-23 10:42:07 +020041#include "audio_device_ios.h"
andrew@webrtc.orgf3b65db2012-09-06 18:17:00 +000042#elif defined(WEBRTC_MAC)
Max Morin787eeed2016-06-23 10:42:07 +020043#include "audio_device_mac.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000044#endif
phoglund@webrtc.org8454ad12014-06-11 14:12:04 +000045#if defined(WEBRTC_DUMMY_FILE_DEVICES)
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020046#include "modules/audio_device/dummy/file_audio_device_factory.h"
phoglund@webrtc.org8454ad12014-06-11 14:12:04 +000047#endif
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020048#include "modules/audio_device/dummy/audio_device_dummy.h"
49#include "modules/audio_device/dummy/file_audio_device.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000050
henrika4af73662017-10-11 13:16:17 +020051#define CHECKinitialized_() \
Max Morin787eeed2016-06-23 10:42:07 +020052 { \
henrika4af73662017-10-11 13:16:17 +020053 if (!initialized_) { \
Max Morin787eeed2016-06-23 10:42:07 +020054 return -1; \
55 }; \
56 }
niklase@google.com470e71d2011-07-07 08:21:25 +000057
henrika4af73662017-10-11 13:16:17 +020058#define CHECKinitialized__BOOL() \
Max Morin787eeed2016-06-23 10:42:07 +020059 { \
henrika4af73662017-10-11 13:16:17 +020060 if (!initialized_) { \
Max Morin787eeed2016-06-23 10:42:07 +020061 return false; \
62 }; \
63 }
niklase@google.com470e71d2011-07-07 08:21:25 +000064
Peter Boström1d194412016-03-21 16:44:31 +010065namespace webrtc {
henrike@webrtc.org70efc322012-02-23 17:45:33 +000066
henrika4af73662017-10-11 13:16:17 +020067// static
Peter Boström4adbbcf2016-05-03 15:51:26 -040068rtc::scoped_refptr<AudioDeviceModule> AudioDeviceModule::Create(
Peter Boström4adbbcf2016-05-03 15:51:26 -040069 const AudioLayer audio_layer) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010070 RTC_LOG(INFO) << __FUNCTION__;
henrika4af73662017-10-11 13:16:17 +020071 // Create the generic reference counted (platform independent) implementation.
Max Morin787eeed2016-06-23 10:42:07 +020072 rtc::scoped_refptr<AudioDeviceModuleImpl> audioDevice(
henrika5ff64832017-10-11 15:14:51 +020073 new rtc::RefCountedObject<AudioDeviceModuleImpl>(audio_layer));
niklase@google.com470e71d2011-07-07 08:21:25 +000074
Max Morin787eeed2016-06-23 10:42:07 +020075 // Ensure that the current platform is supported.
76 if (audioDevice->CheckPlatform() == -1) {
77 return nullptr;
78 }
niklase@google.com470e71d2011-07-07 08:21:25 +000079
Max Morin787eeed2016-06-23 10:42:07 +020080 // Create the platform-dependent implementation.
81 if (audioDevice->CreatePlatformSpecificObjects() == -1) {
82 return nullptr;
83 }
niklase@google.com470e71d2011-07-07 08:21:25 +000084
henrika4af73662017-10-11 13:16:17 +020085 // Ensure that the generic audio buffer can communicate with the platform
86 // specific parts.
Max Morin787eeed2016-06-23 10:42:07 +020087 if (audioDevice->AttachAudioBuffer() == -1) {
88 return nullptr;
89 }
niklase@google.com470e71d2011-07-07 08:21:25 +000090
Max Morin787eeed2016-06-23 10:42:07 +020091 return audioDevice;
niklase@google.com470e71d2011-07-07 08:21:25 +000092}
93
henrika616e3132017-11-13 12:47:59 +010094// TODO(bugs.webrtc.org/7306): deprecated.
95rtc::scoped_refptr<AudioDeviceModule> AudioDeviceModule::Create(
96 const int32_t id,
97 const AudioLayer audio_layer) {
98 RTC_LOG(INFO) << __FUNCTION__;
99 return AudioDeviceModule::Create(audio_layer);
100}
101
henrika5ff64832017-10-11 15:14:51 +0200102AudioDeviceModuleImpl::AudioDeviceModuleImpl(const AudioLayer audioLayer)
103 : audio_layer_(audioLayer) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100104 RTC_LOG(INFO) << __FUNCTION__;
niklase@google.com470e71d2011-07-07 08:21:25 +0000105}
106
Max Morin787eeed2016-06-23 10:42:07 +0200107int32_t AudioDeviceModuleImpl::CheckPlatform() {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100108 RTC_LOG(INFO) << __FUNCTION__;
Max Morin787eeed2016-06-23 10:42:07 +0200109 // Ensure that the current platform is supported
Max Morin787eeed2016-06-23 10:42:07 +0200110 PlatformType platform(kPlatformNotSupported);
niklase@google.com470e71d2011-07-07 08:21:25 +0000111#if defined(_WIN32)
Max Morin787eeed2016-06-23 10:42:07 +0200112 platform = kPlatformWin32;
Mirko Bonadei675513b2017-11-09 11:09:25 +0100113 RTC_LOG(INFO) << "current platform is Win32";
leozwang@google.com522f42b2011-09-19 17:39:05 +0000114#elif defined(WEBRTC_ANDROID)
Max Morin787eeed2016-06-23 10:42:07 +0200115 platform = kPlatformAndroid;
Mirko Bonadei675513b2017-11-09 11:09:25 +0100116 RTC_LOG(INFO) << "current platform is Android";
niklase@google.com470e71d2011-07-07 08:21:25 +0000117#elif defined(WEBRTC_LINUX)
Max Morin787eeed2016-06-23 10:42:07 +0200118 platform = kPlatformLinux;
Mirko Bonadei675513b2017-11-09 11:09:25 +0100119 RTC_LOG(INFO) << "current platform is Linux";
sjlee@webrtc.org414fa7f2012-09-11 17:25:46 +0000120#elif defined(WEBRTC_IOS)
Max Morin787eeed2016-06-23 10:42:07 +0200121 platform = kPlatformIOS;
Mirko Bonadei675513b2017-11-09 11:09:25 +0100122 RTC_LOG(INFO) << "current platform is IOS";
andrew@webrtc.orgf3b65db2012-09-06 18:17:00 +0000123#elif defined(WEBRTC_MAC)
Max Morin787eeed2016-06-23 10:42:07 +0200124 platform = kPlatformMac;
Mirko Bonadei675513b2017-11-09 11:09:25 +0100125 RTC_LOG(INFO) << "current platform is Mac";
niklase@google.com470e71d2011-07-07 08:21:25 +0000126#endif
Max Morin787eeed2016-06-23 10:42:07 +0200127 if (platform == kPlatformNotSupported) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100128 RTC_LOG(LERROR)
129 << "current platform is not supported => this module will self "
130 "destruct!";
Max Morin787eeed2016-06-23 10:42:07 +0200131 return -1;
132 }
henrika4af73662017-10-11 13:16:17 +0200133 platform_type_ = platform;
Max Morin787eeed2016-06-23 10:42:07 +0200134 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000135}
136
Max Morin787eeed2016-06-23 10:42:07 +0200137int32_t AudioDeviceModuleImpl::CreatePlatformSpecificObjects() {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100138 RTC_LOG(INFO) << __FUNCTION__;
henrika4af73662017-10-11 13:16:17 +0200139// Dummy ADM implementations if build flags are set.
xians@google.combf5d2ba2011-08-16 07:44:19 +0000140#if defined(WEBRTC_DUMMY_AUDIO_BUILD)
henrika5ff64832017-10-11 15:14:51 +0200141 audio_device_.reset(new AudioDeviceDummy());
Mirko Bonadei675513b2017-11-09 11:09:25 +0100142 RTC_LOG(INFO) << "Dummy Audio APIs will be utilized";
phoglund@webrtc.org8454ad12014-06-11 14:12:04 +0000143#elif defined(WEBRTC_DUMMY_FILE_DEVICES)
henrika5ff64832017-10-11 15:14:51 +0200144 audio_device_.reset(FileAudioDeviceFactory::CreateFileAudioDevice());
henrika4af73662017-10-11 13:16:17 +0200145 if (audio_device_) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100146 RTC_LOG(INFO) << "Will use file-playing dummy device.";
noahric6a355902016-08-17 15:19:50 -0700147 } else {
148 // Create a dummy device instead.
henrika5ff64832017-10-11 15:14:51 +0200149 audio_device_.reset(new AudioDeviceDummy());
Mirko Bonadei675513b2017-11-09 11:09:25 +0100150 RTC_LOG(INFO) << "Dummy Audio APIs will be utilized";
noahric6a355902016-08-17 15:19:50 -0700151 }
henrika4af73662017-10-11 13:16:17 +0200152
153// Real (non-dummy) ADM implementations.
xians@google.combf5d2ba2011-08-16 07:44:19 +0000154#else
henrika4af73662017-10-11 13:16:17 +0200155 AudioLayer audio_layer(PlatformAudioLayer());
156// Windows ADM implementation.
niklase@google.com470e71d2011-07-07 08:21:25 +0000157#if defined(WEBRTC_WINDOWS_CORE_AUDIO_BUILD)
henrika4af73662017-10-11 13:16:17 +0200158 if ((audio_layer == kWindowsCoreAudio) ||
159 (audio_layer == kPlatformDefaultAudio)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100160 RTC_LOG(INFO) << "Attempting to use the Windows Core Audio APIs...";
Max Morin787eeed2016-06-23 10:42:07 +0200161 if (AudioDeviceWindowsCore::CoreAudioIsSupported()) {
henrika4af73662017-10-11 13:16:17 +0200162 audio_device_.reset(new AudioDeviceWindowsCore());
Mirko Bonadei675513b2017-11-09 11:09:25 +0100163 RTC_LOG(INFO) << "Windows Core Audio APIs will be utilized";
niklase@google.com470e71d2011-07-07 08:21:25 +0000164 }
Max Morin787eeed2016-06-23 10:42:07 +0200165 }
166#endif // defined(WEBRTC_WINDOWS_CORE_AUDIO_BUILD)
niklase@google.com470e71d2011-07-07 08:21:25 +0000167
henrike@webrtc.org9ee75e92013-12-11 21:42:44 +0000168#if defined(WEBRTC_ANDROID)
Max Morin787eeed2016-06-23 10:42:07 +0200169 // Create an Android audio manager.
henrika4af73662017-10-11 13:16:17 +0200170 audio_manager_android_.reset(new AudioManager());
Max Morin787eeed2016-06-23 10:42:07 +0200171 // Select best possible combination of audio layers.
henrika4af73662017-10-11 13:16:17 +0200172 if (audio_layer == kPlatformDefaultAudio) {
173 if (audio_manager_android_->IsLowLatencyPlayoutSupported() &&
174 audio_manager_android_->IsLowLatencyRecordSupported()) {
henrika918b5542016-09-19 15:44:09 +0200175 // Use OpenSL ES for both playout and recording.
henrika4af73662017-10-11 13:16:17 +0200176 audio_layer = kAndroidOpenSLESAudio;
177 } else if (audio_manager_android_->IsLowLatencyPlayoutSupported() &&
178 !audio_manager_android_->IsLowLatencyRecordSupported()) {
henrika918b5542016-09-19 15:44:09 +0200179 // Use OpenSL ES for output on devices that only supports the
Max Morin787eeed2016-06-23 10:42:07 +0200180 // low-latency output audio path.
henrika4af73662017-10-11 13:16:17 +0200181 audio_layer = kAndroidJavaInputAndOpenSLESOutputAudio;
henrikab2619892015-05-18 16:49:16 +0200182 } else {
henrika918b5542016-09-19 15:44:09 +0200183 // Use Java-based audio in both directions when low-latency output is
184 // not supported.
henrika4af73662017-10-11 13:16:17 +0200185 audio_layer = kAndroidJavaAudio;
niklase@google.com470e71d2011-07-07 08:21:25 +0000186 }
Max Morin787eeed2016-06-23 10:42:07 +0200187 }
henrika4af73662017-10-11 13:16:17 +0200188 AudioManager* audio_manager = audio_manager_android_.get();
189 if (audio_layer == kAndroidJavaAudio) {
Max Morin787eeed2016-06-23 10:42:07 +0200190 // Java audio for both input and output audio.
henrika4af73662017-10-11 13:16:17 +0200191 audio_device_.reset(new AudioDeviceTemplate<AudioRecordJni, AudioTrackJni>(
192 audio_layer, audio_manager));
193 } else if (audio_layer == kAndroidOpenSLESAudio) {
henrika918b5542016-09-19 15:44:09 +0200194 // OpenSL ES based audio for both input and output audio.
henrika4af73662017-10-11 13:16:17 +0200195 audio_device_.reset(
196 new AudioDeviceTemplate<OpenSLESRecorder, OpenSLESPlayer>(
197 audio_layer, audio_manager));
198 } else if (audio_layer == kAndroidJavaInputAndOpenSLESOutputAudio) {
Max Morin787eeed2016-06-23 10:42:07 +0200199 // Java audio for input and OpenSL ES for output audio (i.e. mixed APIs).
200 // This combination provides low-latency output audio and at the same
201 // time support for HW AEC using the AudioRecord Java API.
henrika4af73662017-10-11 13:16:17 +0200202 audio_device_.reset(new AudioDeviceTemplate<AudioRecordJni, OpenSLESPlayer>(
203 audio_layer, audio_manager));
Max Morin787eeed2016-06-23 10:42:07 +0200204 } else {
205 // Invalid audio layer.
henrika4af73662017-10-11 13:16:17 +0200206 audio_device_.reset(nullptr);
Max Morin787eeed2016-06-23 10:42:07 +0200207 }
208// END #if defined(WEBRTC_ANDROID)
niklase@google.com470e71d2011-07-07 08:21:25 +0000209
henrika4af73662017-10-11 13:16:17 +0200210// Linux ADM implementation.
niklase@google.com470e71d2011-07-07 08:21:25 +0000211#elif defined(WEBRTC_LINUX)
henrika4af73662017-10-11 13:16:17 +0200212 if ((audio_layer == kLinuxPulseAudio) ||
213 (audio_layer == kPlatformDefaultAudio)) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000214#if defined(LINUX_PULSE)
Mirko Bonadei675513b2017-11-09 11:09:25 +0100215 RTC_LOG(INFO) << "Attempting to use Linux PulseAudio APIs...";
henrika4af73662017-10-11 13:16:17 +0200216 // Linux PulseAudio implementation.
217 audio_device_.reset(new AudioDeviceLinuxPulse());
Mirko Bonadei675513b2017-11-09 11:09:25 +0100218 RTC_LOG(INFO) << "Linux PulseAudio APIs will be utilized";
niklase@google.com470e71d2011-07-07 08:21:25 +0000219#endif
220#if defined(LINUX_PULSE)
Max Morin787eeed2016-06-23 10:42:07 +0200221#endif
henrika4af73662017-10-11 13:16:17 +0200222 } else if (audio_layer == kLinuxAlsaAudio) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000223#if defined(LINUX_ALSA)
henrika4af73662017-10-11 13:16:17 +0200224 // Linux ALSA implementation.
225 audio_device_.reset(new AudioDeviceLinuxALSA());
Mirko Bonadei675513b2017-11-09 11:09:25 +0100226 RTC_LOG(INFO) << "Linux ALSA APIs will be utilized.";
niklase@google.com470e71d2011-07-07 08:21:25 +0000227#endif
Max Morin787eeed2016-06-23 10:42:07 +0200228 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000229#endif // #if defined(WEBRTC_LINUX)
230
henrika4af73662017-10-11 13:16:17 +0200231// iOS ADM implementation.
sjlee@webrtc.org414fa7f2012-09-11 17:25:46 +0000232#if defined(WEBRTC_IOS)
henrika4af73662017-10-11 13:16:17 +0200233 if (audio_layer == kPlatformDefaultAudio) {
234 audio_device_.reset(new AudioDeviceIOS());
Mirko Bonadei675513b2017-11-09 11:09:25 +0100235 RTC_LOG(INFO) << "iPhone Audio APIs will be utilized.";
Max Morin787eeed2016-06-23 10:42:07 +0200236 }
237// END #if defined(WEBRTC_IOS)
niklase@google.com470e71d2011-07-07 08:21:25 +0000238
henrika4af73662017-10-11 13:16:17 +0200239// Mac OS X ADM implementation.
andrew@webrtc.orgf3b65db2012-09-06 18:17:00 +0000240#elif defined(WEBRTC_MAC)
henrika4af73662017-10-11 13:16:17 +0200241 if (audio_layer == kPlatformDefaultAudio) {
242 audio_device_.reset(new AudioDeviceMac());
Mirko Bonadei675513b2017-11-09 11:09:25 +0100243 RTC_LOG(INFO) << "Mac OS X Audio APIs will be utilized.";
Max Morin787eeed2016-06-23 10:42:07 +0200244 }
andrew@webrtc.orgf3b65db2012-09-06 18:17:00 +0000245#endif // WEBRTC_MAC
niklase@google.com470e71d2011-07-07 08:21:25 +0000246
henrika4af73662017-10-11 13:16:17 +0200247 // Dummy ADM implementation.
248 if (audio_layer == kDummyAudio) {
henrika5ff64832017-10-11 15:14:51 +0200249 audio_device_.reset(new AudioDeviceDummy());
Mirko Bonadei675513b2017-11-09 11:09:25 +0100250 RTC_LOG(INFO) << "Dummy Audio APIs will be utilized.";
Max Morin787eeed2016-06-23 10:42:07 +0200251 }
xians@google.combf5d2ba2011-08-16 07:44:19 +0000252#endif // if defined(WEBRTC_DUMMY_AUDIO_BUILD)
niklase@google.com470e71d2011-07-07 08:21:25 +0000253
henrika4af73662017-10-11 13:16:17 +0200254 if (!audio_device_) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100255 RTC_LOG(LS_ERROR)
henrika4af73662017-10-11 13:16:17 +0200256 << "Failed to create the platform specific ADM implementation.";
Max Morin787eeed2016-06-23 10:42:07 +0200257 return -1;
258 }
Max Morin787eeed2016-06-23 10:42:07 +0200259 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000260}
261
Max Morin787eeed2016-06-23 10:42:07 +0200262int32_t AudioDeviceModuleImpl::AttachAudioBuffer() {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100263 RTC_LOG(INFO) << __FUNCTION__;
henrika4af73662017-10-11 13:16:17 +0200264 audio_device_->AttachAudioBuffer(&audio_device_buffer_);
Max Morin787eeed2016-06-23 10:42:07 +0200265 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000266}
267
Max Morin787eeed2016-06-23 10:42:07 +0200268AudioDeviceModuleImpl::~AudioDeviceModuleImpl() {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100269 RTC_LOG(INFO) << __FUNCTION__;
niklase@google.com470e71d2011-07-07 08:21:25 +0000270}
271
henrikab2619892015-05-18 16:49:16 +0200272int32_t AudioDeviceModuleImpl::ActiveAudioLayer(AudioLayer* audioLayer) const {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100273 RTC_LOG(INFO) << __FUNCTION__;
henrikab2619892015-05-18 16:49:16 +0200274 AudioLayer activeAudio;
henrika4af73662017-10-11 13:16:17 +0200275 if (audio_device_->ActiveAudioLayer(activeAudio) == -1) {
henrikab2619892015-05-18 16:49:16 +0200276 return -1;
277 }
278 *audioLayer = activeAudio;
279 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000280}
281
Max Morin787eeed2016-06-23 10:42:07 +0200282int32_t AudioDeviceModuleImpl::Init() {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100283 RTC_LOG(INFO) << __FUNCTION__;
henrika4af73662017-10-11 13:16:17 +0200284 if (initialized_)
niklase@google.com470e71d2011-07-07 08:21:25 +0000285 return 0;
henrika4af73662017-10-11 13:16:17 +0200286 RTC_CHECK(audio_device_);
287 AudioDeviceGeneric::InitStatus status = audio_device_->Init();
Max Morin84cab202016-07-01 13:35:19 +0200288 RTC_HISTOGRAM_ENUMERATION(
289 "WebRTC.Audio.InitializationResult", static_cast<int>(status),
290 static_cast<int>(AudioDeviceGeneric::InitStatus::NUM_STATUSES));
291 if (status != AudioDeviceGeneric::InitStatus::OK) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100292 RTC_LOG(LS_ERROR) << "Audio device initialization failed.";
Max Morin787eeed2016-06-23 10:42:07 +0200293 return -1;
294 }
henrika4af73662017-10-11 13:16:17 +0200295 initialized_ = true;
Max Morin787eeed2016-06-23 10:42:07 +0200296 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000297}
298
Max Morin787eeed2016-06-23 10:42:07 +0200299int32_t AudioDeviceModuleImpl::Terminate() {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100300 RTC_LOG(INFO) << __FUNCTION__;
henrika4af73662017-10-11 13:16:17 +0200301 if (!initialized_)
niklase@google.com470e71d2011-07-07 08:21:25 +0000302 return 0;
henrika4af73662017-10-11 13:16:17 +0200303 if (audio_device_->Terminate() == -1) {
Max Morin787eeed2016-06-23 10:42:07 +0200304 return -1;
305 }
henrika4af73662017-10-11 13:16:17 +0200306 initialized_ = false;
Max Morin787eeed2016-06-23 10:42:07 +0200307 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000308}
309
Max Morin787eeed2016-06-23 10:42:07 +0200310bool AudioDeviceModuleImpl::Initialized() const {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100311 RTC_LOG(INFO) << __FUNCTION__ << ": " << initialized_;
henrika4af73662017-10-11 13:16:17 +0200312 return initialized_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000313}
314
Max Morin787eeed2016-06-23 10:42:07 +0200315int32_t AudioDeviceModuleImpl::InitSpeaker() {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100316 RTC_LOG(INFO) << __FUNCTION__;
henrika4af73662017-10-11 13:16:17 +0200317 CHECKinitialized_();
318 return audio_device_->InitSpeaker();
niklase@google.com470e71d2011-07-07 08:21:25 +0000319}
320
Max Morin787eeed2016-06-23 10:42:07 +0200321int32_t AudioDeviceModuleImpl::InitMicrophone() {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100322 RTC_LOG(INFO) << __FUNCTION__;
henrika4af73662017-10-11 13:16:17 +0200323 CHECKinitialized_();
324 return audio_device_->InitMicrophone();
niklase@google.com470e71d2011-07-07 08:21:25 +0000325}
326
Max Morin787eeed2016-06-23 10:42:07 +0200327int32_t AudioDeviceModuleImpl::SpeakerVolumeIsAvailable(bool* available) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100328 RTC_LOG(INFO) << __FUNCTION__;
henrika4af73662017-10-11 13:16:17 +0200329 CHECKinitialized_();
330 bool isAvailable = false;
331 if (audio_device_->SpeakerVolumeIsAvailable(isAvailable) == -1) {
Max Morin787eeed2016-06-23 10:42:07 +0200332 return -1;
333 }
Max Morin787eeed2016-06-23 10:42:07 +0200334 *available = isAvailable;
Mirko Bonadei675513b2017-11-09 11:09:25 +0100335 RTC_LOG(INFO) << "output: " << isAvailable;
henrika4af73662017-10-11 13:16:17 +0200336 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000337}
338
Max Morin787eeed2016-06-23 10:42:07 +0200339int32_t AudioDeviceModuleImpl::SetSpeakerVolume(uint32_t volume) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100340 RTC_LOG(INFO) << __FUNCTION__ << "(" << volume << ")";
henrika4af73662017-10-11 13:16:17 +0200341 CHECKinitialized_();
342 return audio_device_->SetSpeakerVolume(volume);
niklase@google.com470e71d2011-07-07 08:21:25 +0000343}
344
Max Morin787eeed2016-06-23 10:42:07 +0200345int32_t AudioDeviceModuleImpl::SpeakerVolume(uint32_t* volume) const {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100346 RTC_LOG(INFO) << __FUNCTION__;
henrika4af73662017-10-11 13:16:17 +0200347 CHECKinitialized_();
348 uint32_t level = 0;
349 if (audio_device_->SpeakerVolume(level) == -1) {
Max Morin787eeed2016-06-23 10:42:07 +0200350 return -1;
351 }
Max Morin787eeed2016-06-23 10:42:07 +0200352 *volume = level;
Mirko Bonadei675513b2017-11-09 11:09:25 +0100353 RTC_LOG(INFO) << "output: " << *volume;
henrika4af73662017-10-11 13:16:17 +0200354 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000355}
356
Max Morin787eeed2016-06-23 10:42:07 +0200357bool AudioDeviceModuleImpl::SpeakerIsInitialized() const {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100358 RTC_LOG(INFO) << __FUNCTION__;
henrika4af73662017-10-11 13:16:17 +0200359 CHECKinitialized__BOOL();
360 bool isInitialized = audio_device_->SpeakerIsInitialized();
Mirko Bonadei675513b2017-11-09 11:09:25 +0100361 RTC_LOG(INFO) << "output: " << isInitialized;
henrika4af73662017-10-11 13:16:17 +0200362 return isInitialized;
niklase@google.com470e71d2011-07-07 08:21:25 +0000363}
364
Max Morin787eeed2016-06-23 10:42:07 +0200365bool AudioDeviceModuleImpl::MicrophoneIsInitialized() const {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100366 RTC_LOG(INFO) << __FUNCTION__;
henrika4af73662017-10-11 13:16:17 +0200367 CHECKinitialized__BOOL();
368 bool isInitialized = audio_device_->MicrophoneIsInitialized();
Mirko Bonadei675513b2017-11-09 11:09:25 +0100369 RTC_LOG(INFO) << "output: " << isInitialized;
henrika4af73662017-10-11 13:16:17 +0200370 return isInitialized;
niklase@google.com470e71d2011-07-07 08:21:25 +0000371}
372
Max Morin787eeed2016-06-23 10:42:07 +0200373int32_t AudioDeviceModuleImpl::MaxSpeakerVolume(uint32_t* maxVolume) const {
henrika4af73662017-10-11 13:16:17 +0200374 CHECKinitialized_();
375 uint32_t maxVol = 0;
376 if (audio_device_->MaxSpeakerVolume(maxVol) == -1) {
Max Morin787eeed2016-06-23 10:42:07 +0200377 return -1;
378 }
Max Morin787eeed2016-06-23 10:42:07 +0200379 *maxVolume = maxVol;
henrika4af73662017-10-11 13:16:17 +0200380 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000381}
382
Max Morin787eeed2016-06-23 10:42:07 +0200383int32_t AudioDeviceModuleImpl::MinSpeakerVolume(uint32_t* minVolume) const {
henrika4af73662017-10-11 13:16:17 +0200384 CHECKinitialized_();
385 uint32_t minVol = 0;
386 if (audio_device_->MinSpeakerVolume(minVol) == -1) {
Max Morin787eeed2016-06-23 10:42:07 +0200387 return -1;
388 }
Max Morin787eeed2016-06-23 10:42:07 +0200389 *minVolume = minVol;
henrika4af73662017-10-11 13:16:17 +0200390 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000391}
392
Max Morin787eeed2016-06-23 10:42:07 +0200393int32_t AudioDeviceModuleImpl::SpeakerMuteIsAvailable(bool* available) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100394 RTC_LOG(INFO) << __FUNCTION__;
henrika4af73662017-10-11 13:16:17 +0200395 CHECKinitialized_();
Mirko Bonadei72c42502017-11-09 09:33:23 +0100396 bool isAvailable = false;
henrika4af73662017-10-11 13:16:17 +0200397 if (audio_device_->SpeakerMuteIsAvailable(isAvailable) == -1) {
Max Morin787eeed2016-06-23 10:42:07 +0200398 return -1;
399 }
Max Morin787eeed2016-06-23 10:42:07 +0200400 *available = isAvailable;
Mirko Bonadei675513b2017-11-09 11:09:25 +0100401 RTC_LOG(INFO) << "output: " << isAvailable;
henrika4af73662017-10-11 13:16:17 +0200402 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000403}
404
Max Morin787eeed2016-06-23 10:42:07 +0200405int32_t AudioDeviceModuleImpl::SetSpeakerMute(bool enable) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100406 RTC_LOG(INFO) << __FUNCTION__ << "(" << enable << ")";
henrika4af73662017-10-11 13:16:17 +0200407 CHECKinitialized_();
408 return audio_device_->SetSpeakerMute(enable);
niklase@google.com470e71d2011-07-07 08:21:25 +0000409}
410
Max Morin787eeed2016-06-23 10:42:07 +0200411int32_t AudioDeviceModuleImpl::SpeakerMute(bool* enabled) const {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100412 RTC_LOG(INFO) << __FUNCTION__;
henrika4af73662017-10-11 13:16:17 +0200413 CHECKinitialized_();
414 bool muted = false;
415 if (audio_device_->SpeakerMute(muted) == -1) {
Max Morin787eeed2016-06-23 10:42:07 +0200416 return -1;
417 }
Max Morin787eeed2016-06-23 10:42:07 +0200418 *enabled = muted;
Mirko Bonadei675513b2017-11-09 11:09:25 +0100419 RTC_LOG(INFO) << "output: " << muted;
henrika4af73662017-10-11 13:16:17 +0200420 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000421}
422
Max Morin787eeed2016-06-23 10:42:07 +0200423int32_t AudioDeviceModuleImpl::MicrophoneMuteIsAvailable(bool* available) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100424 RTC_LOG(INFO) << __FUNCTION__;
henrika4af73662017-10-11 13:16:17 +0200425 CHECKinitialized_();
426 bool isAvailable = false;
427 if (audio_device_->MicrophoneMuteIsAvailable(isAvailable) == -1) {
Max Morin787eeed2016-06-23 10:42:07 +0200428 return -1;
429 }
Max Morin787eeed2016-06-23 10:42:07 +0200430 *available = isAvailable;
Mirko Bonadei675513b2017-11-09 11:09:25 +0100431 RTC_LOG(INFO) << "output: " << isAvailable;
henrika4af73662017-10-11 13:16:17 +0200432 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000433}
434
Max Morin787eeed2016-06-23 10:42:07 +0200435int32_t AudioDeviceModuleImpl::SetMicrophoneMute(bool enable) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100436 RTC_LOG(INFO) << __FUNCTION__ << "(" << enable << ")";
henrika4af73662017-10-11 13:16:17 +0200437 CHECKinitialized_();
438 return (audio_device_->SetMicrophoneMute(enable));
niklase@google.com470e71d2011-07-07 08:21:25 +0000439}
440
Max Morin787eeed2016-06-23 10:42:07 +0200441int32_t AudioDeviceModuleImpl::MicrophoneMute(bool* enabled) const {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100442 RTC_LOG(INFO) << __FUNCTION__;
henrika4af73662017-10-11 13:16:17 +0200443 CHECKinitialized_();
444 bool muted = false;
445 if (audio_device_->MicrophoneMute(muted) == -1) {
Max Morin787eeed2016-06-23 10:42:07 +0200446 return -1;
447 }
Max Morin787eeed2016-06-23 10:42:07 +0200448 *enabled = muted;
Mirko Bonadei675513b2017-11-09 11:09:25 +0100449 RTC_LOG(INFO) << "output: " << muted;
henrika4af73662017-10-11 13:16:17 +0200450 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000451}
452
Max Morin787eeed2016-06-23 10:42:07 +0200453int32_t AudioDeviceModuleImpl::MicrophoneVolumeIsAvailable(bool* available) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100454 RTC_LOG(INFO) << __FUNCTION__;
henrika4af73662017-10-11 13:16:17 +0200455 CHECKinitialized_();
456 bool isAvailable = false;
457 if (audio_device_->MicrophoneVolumeIsAvailable(isAvailable) == -1) {
Max Morin787eeed2016-06-23 10:42:07 +0200458 return -1;
459 }
Max Morin787eeed2016-06-23 10:42:07 +0200460 *available = isAvailable;
Mirko Bonadei675513b2017-11-09 11:09:25 +0100461 RTC_LOG(INFO) << "output: " << isAvailable;
henrika4af73662017-10-11 13:16:17 +0200462 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000463}
464
Max Morin787eeed2016-06-23 10:42:07 +0200465int32_t AudioDeviceModuleImpl::SetMicrophoneVolume(uint32_t volume) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100466 RTC_LOG(INFO) << __FUNCTION__ << "(" << volume << ")";
henrika4af73662017-10-11 13:16:17 +0200467 CHECKinitialized_();
468 return (audio_device_->SetMicrophoneVolume(volume));
niklase@google.com470e71d2011-07-07 08:21:25 +0000469}
470
Max Morin787eeed2016-06-23 10:42:07 +0200471int32_t AudioDeviceModuleImpl::MicrophoneVolume(uint32_t* volume) const {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100472 RTC_LOG(INFO) << __FUNCTION__;
henrika4af73662017-10-11 13:16:17 +0200473 CHECKinitialized_();
474 uint32_t level = 0;
475 if (audio_device_->MicrophoneVolume(level) == -1) {
Max Morin787eeed2016-06-23 10:42:07 +0200476 return -1;
477 }
Max Morin787eeed2016-06-23 10:42:07 +0200478 *volume = level;
Mirko Bonadei675513b2017-11-09 11:09:25 +0100479 RTC_LOG(INFO) << "output: " << *volume;
henrika4af73662017-10-11 13:16:17 +0200480 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000481}
482
Max Morin787eeed2016-06-23 10:42:07 +0200483int32_t AudioDeviceModuleImpl::StereoRecordingIsAvailable(
484 bool* available) const {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100485 RTC_LOG(INFO) << __FUNCTION__;
henrika4af73662017-10-11 13:16:17 +0200486 CHECKinitialized_();
487 bool isAvailable = false;
488 if (audio_device_->StereoRecordingIsAvailable(isAvailable) == -1) {
Max Morin787eeed2016-06-23 10:42:07 +0200489 return -1;
490 }
Max Morin787eeed2016-06-23 10:42:07 +0200491 *available = isAvailable;
Mirko Bonadei675513b2017-11-09 11:09:25 +0100492 RTC_LOG(INFO) << "output: " << isAvailable;
henrika4af73662017-10-11 13:16:17 +0200493 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000494}
495
Max Morin787eeed2016-06-23 10:42:07 +0200496int32_t AudioDeviceModuleImpl::SetStereoRecording(bool enable) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100497 RTC_LOG(INFO) << __FUNCTION__ << "(" << enable << ")";
henrika4af73662017-10-11 13:16:17 +0200498 CHECKinitialized_();
499 if (audio_device_->RecordingIsInitialized()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100500 RTC_LOG(WARNING) << "recording in stereo is not supported";
Max Morin787eeed2016-06-23 10:42:07 +0200501 return -1;
502 }
henrika4af73662017-10-11 13:16:17 +0200503 if (audio_device_->SetStereoRecording(enable) == -1) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100504 RTC_LOG(WARNING) << "failed to change stereo recording";
Max Morin787eeed2016-06-23 10:42:07 +0200505 return -1;
506 }
Max Morin787eeed2016-06-23 10:42:07 +0200507 int8_t nChannels(1);
508 if (enable) {
509 nChannels = 2;
510 }
henrika4af73662017-10-11 13:16:17 +0200511 audio_device_buffer_.SetRecordingChannels(nChannels);
Max Morin787eeed2016-06-23 10:42:07 +0200512 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000513}
514
Max Morin787eeed2016-06-23 10:42:07 +0200515int32_t AudioDeviceModuleImpl::StereoRecording(bool* enabled) const {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100516 RTC_LOG(INFO) << __FUNCTION__;
henrika4af73662017-10-11 13:16:17 +0200517 CHECKinitialized_();
518 bool stereo = false;
519 if (audio_device_->StereoRecording(stereo) == -1) {
Max Morin787eeed2016-06-23 10:42:07 +0200520 return -1;
521 }
Max Morin787eeed2016-06-23 10:42:07 +0200522 *enabled = stereo;
Mirko Bonadei675513b2017-11-09 11:09:25 +0100523 RTC_LOG(INFO) << "output: " << stereo;
henrika4af73662017-10-11 13:16:17 +0200524 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000525}
526
Max Morin787eeed2016-06-23 10:42:07 +0200527int32_t AudioDeviceModuleImpl::StereoPlayoutIsAvailable(bool* available) const {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100528 RTC_LOG(INFO) << __FUNCTION__;
henrika4af73662017-10-11 13:16:17 +0200529 CHECKinitialized_();
530 bool isAvailable = false;
531 if (audio_device_->StereoPlayoutIsAvailable(isAvailable) == -1) {
Max Morin787eeed2016-06-23 10:42:07 +0200532 return -1;
533 }
Max Morin787eeed2016-06-23 10:42:07 +0200534 *available = isAvailable;
Mirko Bonadei675513b2017-11-09 11:09:25 +0100535 RTC_LOG(INFO) << "output: " << isAvailable;
henrika4af73662017-10-11 13:16:17 +0200536 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000537}
538
Max Morin787eeed2016-06-23 10:42:07 +0200539int32_t AudioDeviceModuleImpl::SetStereoPlayout(bool enable) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100540 RTC_LOG(INFO) << __FUNCTION__ << "(" << enable << ")";
henrika4af73662017-10-11 13:16:17 +0200541 CHECKinitialized_();
542 if (audio_device_->PlayoutIsInitialized()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100543 RTC_LOG(LERROR)
Max Morin098e6c52016-06-28 09:36:25 +0200544 << "unable to set stereo mode while playing side is initialized";
Max Morin787eeed2016-06-23 10:42:07 +0200545 return -1;
546 }
henrika4af73662017-10-11 13:16:17 +0200547 if (audio_device_->SetStereoPlayout(enable)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100548 RTC_LOG(WARNING) << "stereo playout is not supported";
Max Morin787eeed2016-06-23 10:42:07 +0200549 return -1;
550 }
Max Morin787eeed2016-06-23 10:42:07 +0200551 int8_t nChannels(1);
552 if (enable) {
553 nChannels = 2;
554 }
henrika4af73662017-10-11 13:16:17 +0200555 audio_device_buffer_.SetPlayoutChannels(nChannels);
Max Morin787eeed2016-06-23 10:42:07 +0200556 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000557}
558
Max Morin787eeed2016-06-23 10:42:07 +0200559int32_t AudioDeviceModuleImpl::StereoPlayout(bool* enabled) const {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100560 RTC_LOG(INFO) << __FUNCTION__;
henrika4af73662017-10-11 13:16:17 +0200561 CHECKinitialized_();
562 bool stereo = false;
563 if (audio_device_->StereoPlayout(stereo) == -1) {
Max Morin787eeed2016-06-23 10:42:07 +0200564 return -1;
565 }
Max Morin787eeed2016-06-23 10:42:07 +0200566 *enabled = stereo;
Mirko Bonadei675513b2017-11-09 11:09:25 +0100567 RTC_LOG(INFO) << "output: " << stereo;
henrika4af73662017-10-11 13:16:17 +0200568 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000569}
570
Max Morin787eeed2016-06-23 10:42:07 +0200571int32_t AudioDeviceModuleImpl::PlayoutIsAvailable(bool* available) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100572 RTC_LOG(INFO) << __FUNCTION__;
henrika4af73662017-10-11 13:16:17 +0200573 CHECKinitialized_();
574 bool isAvailable = false;
575 if (audio_device_->PlayoutIsAvailable(isAvailable) == -1) {
Max Morin787eeed2016-06-23 10:42:07 +0200576 return -1;
577 }
Max Morin787eeed2016-06-23 10:42:07 +0200578 *available = isAvailable;
Mirko Bonadei675513b2017-11-09 11:09:25 +0100579 RTC_LOG(INFO) << "output: " << isAvailable;
henrika4af73662017-10-11 13:16:17 +0200580 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000581}
582
Max Morin787eeed2016-06-23 10:42:07 +0200583int32_t AudioDeviceModuleImpl::RecordingIsAvailable(bool* available) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100584 RTC_LOG(INFO) << __FUNCTION__;
henrika4af73662017-10-11 13:16:17 +0200585 CHECKinitialized_();
586 bool isAvailable = false;
587 if (audio_device_->RecordingIsAvailable(isAvailable) == -1) {
Max Morin787eeed2016-06-23 10:42:07 +0200588 return -1;
589 }
Max Morin787eeed2016-06-23 10:42:07 +0200590 *available = isAvailable;
Mirko Bonadei675513b2017-11-09 11:09:25 +0100591 RTC_LOG(INFO) << "output: " << isAvailable;
henrika4af73662017-10-11 13:16:17 +0200592 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000593}
594
Max Morin787eeed2016-06-23 10:42:07 +0200595int32_t AudioDeviceModuleImpl::MaxMicrophoneVolume(uint32_t* maxVolume) const {
henrika4af73662017-10-11 13:16:17 +0200596 CHECKinitialized_();
Max Morin787eeed2016-06-23 10:42:07 +0200597 uint32_t maxVol(0);
henrika4af73662017-10-11 13:16:17 +0200598 if (audio_device_->MaxMicrophoneVolume(maxVol) == -1) {
Max Morin787eeed2016-06-23 10:42:07 +0200599 return -1;
600 }
Max Morin787eeed2016-06-23 10:42:07 +0200601 *maxVolume = maxVol;
henrika4af73662017-10-11 13:16:17 +0200602 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000603}
604
Max Morin787eeed2016-06-23 10:42:07 +0200605int32_t AudioDeviceModuleImpl::MinMicrophoneVolume(uint32_t* minVolume) const {
henrika4af73662017-10-11 13:16:17 +0200606 CHECKinitialized_();
Max Morin787eeed2016-06-23 10:42:07 +0200607 uint32_t minVol(0);
henrika4af73662017-10-11 13:16:17 +0200608 if (audio_device_->MinMicrophoneVolume(minVol) == -1) {
Max Morin787eeed2016-06-23 10:42:07 +0200609 return -1;
610 }
Max Morin787eeed2016-06-23 10:42:07 +0200611 *minVolume = minVol;
henrika4af73662017-10-11 13:16:17 +0200612 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000613}
614
Max Morin787eeed2016-06-23 10:42:07 +0200615int16_t AudioDeviceModuleImpl::PlayoutDevices() {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100616 RTC_LOG(INFO) << __FUNCTION__;
henrika4af73662017-10-11 13:16:17 +0200617 CHECKinitialized_();
618 uint16_t nPlayoutDevices = audio_device_->PlayoutDevices();
Mirko Bonadei675513b2017-11-09 11:09:25 +0100619 RTC_LOG(INFO) << "output: " << nPlayoutDevices;
henrika4af73662017-10-11 13:16:17 +0200620 return (int16_t)(nPlayoutDevices);
niklase@google.com470e71d2011-07-07 08:21:25 +0000621}
622
Max Morin787eeed2016-06-23 10:42:07 +0200623int32_t AudioDeviceModuleImpl::SetPlayoutDevice(uint16_t index) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100624 RTC_LOG(INFO) << __FUNCTION__ << "(" << index << ")";
henrika4af73662017-10-11 13:16:17 +0200625 CHECKinitialized_();
626 return audio_device_->SetPlayoutDevice(index);
niklase@google.com470e71d2011-07-07 08:21:25 +0000627}
628
Max Morin787eeed2016-06-23 10:42:07 +0200629int32_t AudioDeviceModuleImpl::SetPlayoutDevice(WindowsDeviceType device) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100630 RTC_LOG(INFO) << __FUNCTION__;
henrika4af73662017-10-11 13:16:17 +0200631 CHECKinitialized_();
632 return audio_device_->SetPlayoutDevice(device);
niklase@google.com470e71d2011-07-07 08:21:25 +0000633}
634
pbos@webrtc.org25509882013-04-09 10:30:35 +0000635int32_t AudioDeviceModuleImpl::PlayoutDeviceName(
636 uint16_t index,
leozwang@webrtc.org28f39132012-03-01 18:01:48 +0000637 char name[kAdmMaxDeviceNameSize],
Max Morin787eeed2016-06-23 10:42:07 +0200638 char guid[kAdmMaxGuidSize]) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100639 RTC_LOG(INFO) << __FUNCTION__ << "(" << index << ", ...)";
henrika4af73662017-10-11 13:16:17 +0200640 CHECKinitialized_();
Max Morin787eeed2016-06-23 10:42:07 +0200641 if (name == NULL) {
Max Morin787eeed2016-06-23 10:42:07 +0200642 return -1;
643 }
henrika4af73662017-10-11 13:16:17 +0200644 if (audio_device_->PlayoutDeviceName(index, name, guid) == -1) {
Max Morin787eeed2016-06-23 10:42:07 +0200645 return -1;
646 }
Max Morin787eeed2016-06-23 10:42:07 +0200647 if (name != NULL) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100648 RTC_LOG(INFO) << "output: name = " << name;
Max Morin787eeed2016-06-23 10:42:07 +0200649 }
650 if (guid != NULL) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100651 RTC_LOG(INFO) << "output: guid = " << guid;
Max Morin787eeed2016-06-23 10:42:07 +0200652 }
henrika4af73662017-10-11 13:16:17 +0200653 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000654}
655
pbos@webrtc.org25509882013-04-09 10:30:35 +0000656int32_t AudioDeviceModuleImpl::RecordingDeviceName(
657 uint16_t index,
leozwang@webrtc.org28f39132012-03-01 18:01:48 +0000658 char name[kAdmMaxDeviceNameSize],
Max Morin787eeed2016-06-23 10:42:07 +0200659 char guid[kAdmMaxGuidSize]) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100660 RTC_LOG(INFO) << __FUNCTION__ << "(" << index << ", ...)";
henrika4af73662017-10-11 13:16:17 +0200661 CHECKinitialized_();
Max Morin787eeed2016-06-23 10:42:07 +0200662 if (name == NULL) {
Max Morin787eeed2016-06-23 10:42:07 +0200663 return -1;
664 }
henrika4af73662017-10-11 13:16:17 +0200665 if (audio_device_->RecordingDeviceName(index, name, guid) == -1) {
Max Morin787eeed2016-06-23 10:42:07 +0200666 return -1;
667 }
Max Morin787eeed2016-06-23 10:42:07 +0200668 if (name != NULL) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100669 RTC_LOG(INFO) << "output: name = " << name;
Max Morin787eeed2016-06-23 10:42:07 +0200670 }
671 if (guid != NULL) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100672 RTC_LOG(INFO) << "output: guid = " << guid;
Max Morin787eeed2016-06-23 10:42:07 +0200673 }
henrika4af73662017-10-11 13:16:17 +0200674 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000675}
676
Max Morin787eeed2016-06-23 10:42:07 +0200677int16_t AudioDeviceModuleImpl::RecordingDevices() {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100678 RTC_LOG(INFO) << __FUNCTION__;
henrika4af73662017-10-11 13:16:17 +0200679 CHECKinitialized_();
680 uint16_t nRecordingDevices = audio_device_->RecordingDevices();
Mirko Bonadei675513b2017-11-09 11:09:25 +0100681 RTC_LOG(INFO) << "output: " << nRecordingDevices;
henrika4af73662017-10-11 13:16:17 +0200682 return (int16_t)nRecordingDevices;
niklase@google.com470e71d2011-07-07 08:21:25 +0000683}
684
Max Morin787eeed2016-06-23 10:42:07 +0200685int32_t AudioDeviceModuleImpl::SetRecordingDevice(uint16_t index) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100686 RTC_LOG(INFO) << __FUNCTION__ << "(" << index << ")";
henrika4af73662017-10-11 13:16:17 +0200687 CHECKinitialized_();
688 return audio_device_->SetRecordingDevice(index);
niklase@google.com470e71d2011-07-07 08:21:25 +0000689}
690
Max Morin787eeed2016-06-23 10:42:07 +0200691int32_t AudioDeviceModuleImpl::SetRecordingDevice(WindowsDeviceType device) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100692 RTC_LOG(INFO) << __FUNCTION__;
henrika4af73662017-10-11 13:16:17 +0200693 CHECKinitialized_();
694 return audio_device_->SetRecordingDevice(device);
niklase@google.com470e71d2011-07-07 08:21:25 +0000695}
696
Max Morin787eeed2016-06-23 10:42:07 +0200697int32_t AudioDeviceModuleImpl::InitPlayout() {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100698 RTC_LOG(INFO) << __FUNCTION__;
henrika4af73662017-10-11 13:16:17 +0200699 CHECKinitialized_();
maxmorin8c695b42016-07-25 02:46:44 -0700700 if (PlayoutIsInitialized()) {
701 return 0;
702 }
henrika4af73662017-10-11 13:16:17 +0200703 int32_t result = audio_device_->InitPlayout();
Mirko Bonadei675513b2017-11-09 11:09:25 +0100704 RTC_LOG(INFO) << "output: " << result;
Max Morin84cab202016-07-01 13:35:19 +0200705 RTC_HISTOGRAM_BOOLEAN("WebRTC.Audio.InitPlayoutSuccess",
706 static_cast<int>(result == 0));
707 return result;
niklase@google.com470e71d2011-07-07 08:21:25 +0000708}
709
Max Morin787eeed2016-06-23 10:42:07 +0200710int32_t AudioDeviceModuleImpl::InitRecording() {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100711 RTC_LOG(INFO) << __FUNCTION__;
henrika4af73662017-10-11 13:16:17 +0200712 CHECKinitialized_();
maxmorin8c695b42016-07-25 02:46:44 -0700713 if (RecordingIsInitialized()) {
714 return 0;
715 }
henrika4af73662017-10-11 13:16:17 +0200716 int32_t result = audio_device_->InitRecording();
Mirko Bonadei675513b2017-11-09 11:09:25 +0100717 RTC_LOG(INFO) << "output: " << result;
Max Morin84cab202016-07-01 13:35:19 +0200718 RTC_HISTOGRAM_BOOLEAN("WebRTC.Audio.InitRecordingSuccess",
719 static_cast<int>(result == 0));
720 return result;
niklase@google.com470e71d2011-07-07 08:21:25 +0000721}
722
Max Morin787eeed2016-06-23 10:42:07 +0200723bool AudioDeviceModuleImpl::PlayoutIsInitialized() const {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100724 RTC_LOG(INFO) << __FUNCTION__;
henrika4af73662017-10-11 13:16:17 +0200725 CHECKinitialized__BOOL();
726 return audio_device_->PlayoutIsInitialized();
niklase@google.com470e71d2011-07-07 08:21:25 +0000727}
728
Max Morin787eeed2016-06-23 10:42:07 +0200729bool AudioDeviceModuleImpl::RecordingIsInitialized() const {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100730 RTC_LOG(INFO) << __FUNCTION__;
henrika4af73662017-10-11 13:16:17 +0200731 CHECKinitialized__BOOL();
732 return audio_device_->RecordingIsInitialized();
niklase@google.com470e71d2011-07-07 08:21:25 +0000733}
734
Max Morin787eeed2016-06-23 10:42:07 +0200735int32_t AudioDeviceModuleImpl::StartPlayout() {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100736 RTC_LOG(INFO) << __FUNCTION__;
henrika4af73662017-10-11 13:16:17 +0200737 CHECKinitialized_();
maxmorin8c695b42016-07-25 02:46:44 -0700738 if (Playing()) {
739 return 0;
740 }
henrika4af73662017-10-11 13:16:17 +0200741 audio_device_buffer_.StartPlayout();
742 int32_t result = audio_device_->StartPlayout();
Mirko Bonadei675513b2017-11-09 11:09:25 +0100743 RTC_LOG(INFO) << "output: " << result;
Max Morin84cab202016-07-01 13:35:19 +0200744 RTC_HISTOGRAM_BOOLEAN("WebRTC.Audio.StartPlayoutSuccess",
745 static_cast<int>(result == 0));
746 return result;
niklase@google.com470e71d2011-07-07 08:21:25 +0000747}
748
Max Morin787eeed2016-06-23 10:42:07 +0200749int32_t AudioDeviceModuleImpl::StopPlayout() {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100750 RTC_LOG(INFO) << __FUNCTION__;
henrika4af73662017-10-11 13:16:17 +0200751 CHECKinitialized_();
752 int32_t result = audio_device_->StopPlayout();
753 audio_device_buffer_.StopPlayout();
Mirko Bonadei675513b2017-11-09 11:09:25 +0100754 RTC_LOG(INFO) << "output: " << result;
Max Morin84cab202016-07-01 13:35:19 +0200755 RTC_HISTOGRAM_BOOLEAN("WebRTC.Audio.StopPlayoutSuccess",
756 static_cast<int>(result == 0));
757 return result;
niklase@google.com470e71d2011-07-07 08:21:25 +0000758}
759
Max Morin787eeed2016-06-23 10:42:07 +0200760bool AudioDeviceModuleImpl::Playing() const {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100761 RTC_LOG(INFO) << __FUNCTION__;
henrika4af73662017-10-11 13:16:17 +0200762 CHECKinitialized__BOOL();
763 return audio_device_->Playing();
niklase@google.com470e71d2011-07-07 08:21:25 +0000764}
765
Max Morin787eeed2016-06-23 10:42:07 +0200766int32_t AudioDeviceModuleImpl::StartRecording() {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100767 RTC_LOG(INFO) << __FUNCTION__;
henrika4af73662017-10-11 13:16:17 +0200768 CHECKinitialized_();
maxmorin8c695b42016-07-25 02:46:44 -0700769 if (Recording()) {
770 return 0;
771 }
henrika4af73662017-10-11 13:16:17 +0200772 audio_device_buffer_.StartRecording();
773 int32_t result = audio_device_->StartRecording();
Mirko Bonadei675513b2017-11-09 11:09:25 +0100774 RTC_LOG(INFO) << "output: " << result;
Max Morin84cab202016-07-01 13:35:19 +0200775 RTC_HISTOGRAM_BOOLEAN("WebRTC.Audio.StartRecordingSuccess",
776 static_cast<int>(result == 0));
777 return result;
niklase@google.com470e71d2011-07-07 08:21:25 +0000778}
niklase@google.com470e71d2011-07-07 08:21:25 +0000779
Max Morin787eeed2016-06-23 10:42:07 +0200780int32_t AudioDeviceModuleImpl::StopRecording() {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100781 RTC_LOG(INFO) << __FUNCTION__;
henrika4af73662017-10-11 13:16:17 +0200782 CHECKinitialized_();
783 int32_t result = audio_device_->StopRecording();
784 audio_device_buffer_.StopRecording();
Mirko Bonadei675513b2017-11-09 11:09:25 +0100785 RTC_LOG(INFO) << "output: " << result;
Max Morin84cab202016-07-01 13:35:19 +0200786 RTC_HISTOGRAM_BOOLEAN("WebRTC.Audio.StopRecordingSuccess",
787 static_cast<int>(result == 0));
788 return result;
niklase@google.com470e71d2011-07-07 08:21:25 +0000789}
790
Max Morin787eeed2016-06-23 10:42:07 +0200791bool AudioDeviceModuleImpl::Recording() const {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100792 RTC_LOG(INFO) << __FUNCTION__;
henrika4af73662017-10-11 13:16:17 +0200793 CHECKinitialized__BOOL();
794 return audio_device_->Recording();
niklase@google.com470e71d2011-07-07 08:21:25 +0000795}
796
Max Morin787eeed2016-06-23 10:42:07 +0200797int32_t AudioDeviceModuleImpl::RegisterAudioCallback(
798 AudioTransport* audioCallback) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100799 RTC_LOG(INFO) << __FUNCTION__;
henrika4af73662017-10-11 13:16:17 +0200800 return audio_device_buffer_.RegisterAudioCallback(audioCallback);
niklase@google.com470e71d2011-07-07 08:21:25 +0000801}
802
Max Morin787eeed2016-06-23 10:42:07 +0200803int32_t AudioDeviceModuleImpl::PlayoutDelay(uint16_t* delayMS) const {
henrika4af73662017-10-11 13:16:17 +0200804 CHECKinitialized_();
805 uint16_t delay = 0;
806 if (audio_device_->PlayoutDelay(delay) == -1) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100807 RTC_LOG(LERROR) << "failed to retrieve the playout delay";
Max Morin787eeed2016-06-23 10:42:07 +0200808 return -1;
809 }
Max Morin787eeed2016-06-23 10:42:07 +0200810 *delayMS = delay;
henrika4af73662017-10-11 13:16:17 +0200811 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000812}
813
henrika@webrtc.orga954c072014-12-09 16:22:09 +0000814bool AudioDeviceModuleImpl::BuiltInAECIsAvailable() const {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100815 RTC_LOG(INFO) << __FUNCTION__;
henrika4af73662017-10-11 13:16:17 +0200816 CHECKinitialized__BOOL();
817 bool isAvailable = audio_device_->BuiltInAECIsAvailable();
Mirko Bonadei675513b2017-11-09 11:09:25 +0100818 RTC_LOG(INFO) << "output: " << isAvailable;
Max Morin098e6c52016-06-28 09:36:25 +0200819 return isAvailable;
henrika@webrtc.orga954c072014-12-09 16:22:09 +0000820}
821
henrikac14f5ff2015-09-23 14:08:33 +0200822int32_t AudioDeviceModuleImpl::EnableBuiltInAEC(bool enable) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100823 RTC_LOG(INFO) << __FUNCTION__ << "(" << enable << ")";
henrika4af73662017-10-11 13:16:17 +0200824 CHECKinitialized_();
825 int32_t ok = audio_device_->EnableBuiltInAEC(enable);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100826 RTC_LOG(INFO) << "output: " << ok;
Max Morin098e6c52016-06-28 09:36:25 +0200827 return ok;
henrikac14f5ff2015-09-23 14:08:33 +0200828}
829
830bool AudioDeviceModuleImpl::BuiltInAGCIsAvailable() const {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100831 RTC_LOG(INFO) << __FUNCTION__;
henrika4af73662017-10-11 13:16:17 +0200832 CHECKinitialized__BOOL();
833 bool isAvailable = audio_device_->BuiltInAGCIsAvailable();
Mirko Bonadei675513b2017-11-09 11:09:25 +0100834 RTC_LOG(INFO) << "output: " << isAvailable;
Max Morin098e6c52016-06-28 09:36:25 +0200835 return isAvailable;
henrikac14f5ff2015-09-23 14:08:33 +0200836}
837
838int32_t AudioDeviceModuleImpl::EnableBuiltInAGC(bool enable) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100839 RTC_LOG(INFO) << __FUNCTION__ << "(" << enable << ")";
henrika4af73662017-10-11 13:16:17 +0200840 CHECKinitialized_();
841 int32_t ok = audio_device_->EnableBuiltInAGC(enable);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100842 RTC_LOG(INFO) << "output: " << ok;
Max Morin098e6c52016-06-28 09:36:25 +0200843 return ok;
henrikac14f5ff2015-09-23 14:08:33 +0200844}
845
846bool AudioDeviceModuleImpl::BuiltInNSIsAvailable() const {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100847 RTC_LOG(INFO) << __FUNCTION__;
henrika4af73662017-10-11 13:16:17 +0200848 CHECKinitialized__BOOL();
849 bool isAvailable = audio_device_->BuiltInNSIsAvailable();
Mirko Bonadei675513b2017-11-09 11:09:25 +0100850 RTC_LOG(INFO) << "output: " << isAvailable;
Max Morin098e6c52016-06-28 09:36:25 +0200851 return isAvailable;
henrikac14f5ff2015-09-23 14:08:33 +0200852}
853
854int32_t AudioDeviceModuleImpl::EnableBuiltInNS(bool enable) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100855 RTC_LOG(INFO) << __FUNCTION__ << "(" << enable << ")";
henrika4af73662017-10-11 13:16:17 +0200856 CHECKinitialized_();
857 int32_t ok = audio_device_->EnableBuiltInNS(enable);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100858 RTC_LOG(INFO) << "output: " << ok;
Max Morin098e6c52016-06-28 09:36:25 +0200859 return ok;
henrikac14f5ff2015-09-23 14:08:33 +0200860}
861
maxmorin88e31a32016-08-16 00:56:09 -0700862#if defined(WEBRTC_IOS)
henrikaba35d052015-07-14 17:04:08 +0200863int AudioDeviceModuleImpl::GetPlayoutAudioParameters(
864 AudioParameters* params) const {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100865 RTC_LOG(INFO) << __FUNCTION__;
henrika4af73662017-10-11 13:16:17 +0200866 int r = audio_device_->GetPlayoutAudioParameters(params);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100867 RTC_LOG(INFO) << "output: " << r;
Max Morin098e6c52016-06-28 09:36:25 +0200868 return r;
henrikaba35d052015-07-14 17:04:08 +0200869}
870
871int AudioDeviceModuleImpl::GetRecordAudioParameters(
872 AudioParameters* params) const {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100873 RTC_LOG(INFO) << __FUNCTION__;
henrika4af73662017-10-11 13:16:17 +0200874 int r = audio_device_->GetRecordAudioParameters(params);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100875 RTC_LOG(INFO) << "output: " << r;
Max Morin098e6c52016-06-28 09:36:25 +0200876 return r;
henrikaba35d052015-07-14 17:04:08 +0200877}
maxmorin88e31a32016-08-16 00:56:09 -0700878#endif // WEBRTC_IOS
henrikaba35d052015-07-14 17:04:08 +0200879
Max Morin787eeed2016-06-23 10:42:07 +0200880AudioDeviceModuleImpl::PlatformType AudioDeviceModuleImpl::Platform() const {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100881 RTC_LOG(INFO) << __FUNCTION__;
henrika4af73662017-10-11 13:16:17 +0200882 return platform_type_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000883}
884
Max Morin787eeed2016-06-23 10:42:07 +0200885AudioDeviceModule::AudioLayer AudioDeviceModuleImpl::PlatformAudioLayer()
886 const {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100887 RTC_LOG(INFO) << __FUNCTION__;
henrika4af73662017-10-11 13:16:17 +0200888 return audio_layer_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000889}
890
891} // namespace webrtc