• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
3  *
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 
11 #include "modules/audio_device/audio_device_impl.h"
12 
13 #include <stddef.h>
14 
15 #include "api/scoped_refptr.h"
16 #include "modules/audio_device/audio_device_config.h"  // IWYU pragma: keep
17 #include "modules/audio_device/audio_device_generic.h"
18 #include "rtc_base/checks.h"
19 #include "rtc_base/logging.h"
20 #include "rtc_base/ref_counted_object.h"
21 #include "system_wrappers/include/metrics.h"
22 
23 #if defined(_WIN32)
24 #if defined(WEBRTC_WINDOWS_CORE_AUDIO_BUILD)
25 #include "modules/audio_device/win/audio_device_core_win.h"
26 #endif
27 #elif defined(WEBRTC_ANDROID)
28 #include <stdlib.h>
29 #if defined(WEBRTC_AUDIO_DEVICE_INCLUDE_ANDROID_AAUDIO)
30 #include "modules/audio_device/android/aaudio_player.h"
31 #include "modules/audio_device/android/aaudio_recorder.h"
32 #endif
33 #include "modules/audio_device/android/audio_device_template.h"
34 #include "modules/audio_device/android/audio_manager.h"
35 #include "modules/audio_device/android/audio_record_jni.h"
36 #include "modules/audio_device/android/audio_track_jni.h"
37 #include "modules/audio_device/android/opensles_player.h"
38 #include "modules/audio_device/android/opensles_recorder.h"
39 #elif defined(WEBRTC_LINUX)
40 #if defined(WEBRTC_ENABLE_LINUX_ALSA)
41 #include "modules/audio_device/linux/audio_device_alsa_linux.h"
42 #endif
43 #if defined(WEBRTC_ENABLE_LINUX_PULSE)
44 #include "modules/audio_device/linux/audio_device_pulse_linux.h"
45 #endif
46 #elif defined(WEBRTC_IOS)
47 #include "sdk/objc/native/src/audio/audio_device_ios.h"
48 #elif defined(WEBRTC_MAC)
49 #include "modules/audio_device/mac/audio_device_mac.h"
50 #endif
51 #if defined(WEBRTC_DUMMY_FILE_DEVICES)
52 #include "modules/audio_device/dummy/file_audio_device.h"
53 #include "modules/audio_device/dummy/file_audio_device_factory.h"
54 #endif
55 #include "modules/audio_device/dummy/audio_device_dummy.h"
56 
57 #define CHECKinitialized_() \
58   {                         \
59     if (!initialized_) {    \
60       return -1;            \
61     }                       \
62   }
63 
64 #define CHECKinitialized__BOOL() \
65   {                              \
66     if (!initialized_) {         \
67       return false;              \
68     }                            \
69   }
70 
71 namespace webrtc {
72 
Create(AudioLayer audio_layer,TaskQueueFactory * task_queue_factory)73 rtc::scoped_refptr<AudioDeviceModule> AudioDeviceModule::Create(
74     AudioLayer audio_layer,
75     TaskQueueFactory* task_queue_factory) {
76   RTC_LOG(INFO) << __FUNCTION__;
77   return AudioDeviceModule::CreateForTest(audio_layer, task_queue_factory);
78 }
79 
80 // static
CreateForTest(AudioLayer audio_layer,TaskQueueFactory * task_queue_factory)81 rtc::scoped_refptr<AudioDeviceModuleForTest> AudioDeviceModule::CreateForTest(
82     AudioLayer audio_layer,
83     TaskQueueFactory* task_queue_factory) {
84   RTC_LOG(INFO) << __FUNCTION__;
85 
86   // The "AudioDeviceModule::kWindowsCoreAudio2" audio layer has its own
87   // dedicated factory method which should be used instead.
88   if (audio_layer == AudioDeviceModule::kWindowsCoreAudio2) {
89     RTC_LOG(LS_ERROR) << "Use the CreateWindowsCoreAudioAudioDeviceModule() "
90                          "factory method instead for this option.";
91     return nullptr;
92   }
93 
94   // Create the generic reference counted (platform independent) implementation.
95   rtc::scoped_refptr<AudioDeviceModuleImpl> audioDevice(
96       new rtc::RefCountedObject<AudioDeviceModuleImpl>(audio_layer,
97                                                        task_queue_factory));
98 
99   // Ensure that the current platform is supported.
100   if (audioDevice->CheckPlatform() == -1) {
101     return nullptr;
102   }
103 
104   // Create the platform-dependent implementation.
105   if (audioDevice->CreatePlatformSpecificObjects() == -1) {
106     return nullptr;
107   }
108 
109   // Ensure that the generic audio buffer can communicate with the platform
110   // specific parts.
111   if (audioDevice->AttachAudioBuffer() == -1) {
112     return nullptr;
113   }
114 
115   return audioDevice;
116 }
117 
AudioDeviceModuleImpl(AudioLayer audio_layer,TaskQueueFactory * task_queue_factory)118 AudioDeviceModuleImpl::AudioDeviceModuleImpl(
119     AudioLayer audio_layer,
120     TaskQueueFactory* task_queue_factory)
121     : audio_layer_(audio_layer), audio_device_buffer_(task_queue_factory) {
122   RTC_LOG(INFO) << __FUNCTION__;
123 }
124 
CheckPlatform()125 int32_t AudioDeviceModuleImpl::CheckPlatform() {
126   RTC_LOG(INFO) << __FUNCTION__;
127   // Ensure that the current platform is supported
128   PlatformType platform(kPlatformNotSupported);
129 #if defined(_WIN32)
130   platform = kPlatformWin32;
131   RTC_LOG(INFO) << "current platform is Win32";
132 #elif defined(WEBRTC_ANDROID)
133   platform = kPlatformAndroid;
134   RTC_LOG(INFO) << "current platform is Android";
135 #elif defined(WEBRTC_LINUX)
136   platform = kPlatformLinux;
137   RTC_LOG(INFO) << "current platform is Linux";
138 #elif defined(WEBRTC_IOS)
139   platform = kPlatformIOS;
140   RTC_LOG(INFO) << "current platform is IOS";
141 #elif defined(WEBRTC_MAC)
142   platform = kPlatformMac;
143   RTC_LOG(INFO) << "current platform is Mac";
144 #endif
145   if (platform == kPlatformNotSupported) {
146     RTC_LOG(LERROR)
147         << "current platform is not supported => this module will self "
148            "destruct!";
149     return -1;
150   }
151   platform_type_ = platform;
152   return 0;
153 }
154 
CreatePlatformSpecificObjects()155 int32_t AudioDeviceModuleImpl::CreatePlatformSpecificObjects() {
156   RTC_LOG(INFO) << __FUNCTION__;
157 // Dummy ADM implementations if build flags are set.
158 #if defined(WEBRTC_DUMMY_AUDIO_BUILD)
159   audio_device_.reset(new AudioDeviceDummy());
160   RTC_LOG(INFO) << "Dummy Audio APIs will be utilized";
161 #elif defined(WEBRTC_DUMMY_FILE_DEVICES)
162   audio_device_.reset(FileAudioDeviceFactory::CreateFileAudioDevice());
163   if (audio_device_) {
164     RTC_LOG(INFO) << "Will use file-playing dummy device.";
165   } else {
166     // Create a dummy device instead.
167     audio_device_.reset(new AudioDeviceDummy());
168     RTC_LOG(INFO) << "Dummy Audio APIs will be utilized";
169   }
170 
171 // Real (non-dummy) ADM implementations.
172 #else
173   AudioLayer audio_layer(PlatformAudioLayer());
174 // Windows ADM implementation.
175 #if defined(WEBRTC_WINDOWS_CORE_AUDIO_BUILD)
176   if ((audio_layer == kWindowsCoreAudio) ||
177       (audio_layer == kPlatformDefaultAudio)) {
178     RTC_LOG(INFO) << "Attempting to use the Windows Core Audio APIs...";
179     if (AudioDeviceWindowsCore::CoreAudioIsSupported()) {
180       audio_device_.reset(new AudioDeviceWindowsCore());
181       RTC_LOG(INFO) << "Windows Core Audio APIs will be utilized";
182     }
183   }
184 #endif  // defined(WEBRTC_WINDOWS_CORE_AUDIO_BUILD)
185 
186 #if defined(WEBRTC_ANDROID)
187   // Create an Android audio manager.
188   audio_manager_android_.reset(new AudioManager());
189   // Select best possible combination of audio layers.
190   if (audio_layer == kPlatformDefaultAudio) {
191     if (audio_manager_android_->IsAAudioSupported()) {
192       // Use of AAudio for both playout and recording has highest priority.
193       audio_layer = kAndroidAAudioAudio;
194     } else if (audio_manager_android_->IsLowLatencyPlayoutSupported() &&
195                audio_manager_android_->IsLowLatencyRecordSupported()) {
196       // Use OpenSL ES for both playout and recording.
197       audio_layer = kAndroidOpenSLESAudio;
198     } else if (audio_manager_android_->IsLowLatencyPlayoutSupported() &&
199                !audio_manager_android_->IsLowLatencyRecordSupported()) {
200       // Use OpenSL ES for output on devices that only supports the
201       // low-latency output audio path.
202       audio_layer = kAndroidJavaInputAndOpenSLESOutputAudio;
203     } else {
204       // Use Java-based audio in both directions when low-latency output is
205       // not supported.
206       audio_layer = kAndroidJavaAudio;
207     }
208   }
209   AudioManager* audio_manager = audio_manager_android_.get();
210   if (audio_layer == kAndroidJavaAudio) {
211     // Java audio for both input and output audio.
212     audio_device_.reset(new AudioDeviceTemplate<AudioRecordJni, AudioTrackJni>(
213         audio_layer, audio_manager));
214   } else if (audio_layer == kAndroidOpenSLESAudio) {
215     // OpenSL ES based audio for both input and output audio.
216     audio_device_.reset(
217         new AudioDeviceTemplate<OpenSLESRecorder, OpenSLESPlayer>(
218             audio_layer, audio_manager));
219   } else if (audio_layer == kAndroidJavaInputAndOpenSLESOutputAudio) {
220     // Java audio for input and OpenSL ES for output audio (i.e. mixed APIs).
221     // This combination provides low-latency output audio and at the same
222     // time support for HW AEC using the AudioRecord Java API.
223     audio_device_.reset(new AudioDeviceTemplate<AudioRecordJni, OpenSLESPlayer>(
224         audio_layer, audio_manager));
225   } else if (audio_layer == kAndroidAAudioAudio) {
226 #if defined(WEBRTC_AUDIO_DEVICE_INCLUDE_ANDROID_AAUDIO)
227     // AAudio based audio for both input and output.
228     audio_device_.reset(new AudioDeviceTemplate<AAudioRecorder, AAudioPlayer>(
229         audio_layer, audio_manager));
230 #endif
231   } else if (audio_layer == kAndroidJavaInputAndAAudioOutputAudio) {
232 #if defined(WEBRTC_AUDIO_DEVICE_INCLUDE_ANDROID_AAUDIO)
233     // Java audio for input and AAudio for output audio (i.e. mixed APIs).
234     audio_device_.reset(new AudioDeviceTemplate<AudioRecordJni, AAudioPlayer>(
235         audio_layer, audio_manager));
236 #endif
237   } else {
238     RTC_LOG(LS_ERROR) << "The requested audio layer is not supported";
239     audio_device_.reset(nullptr);
240   }
241 // END #if defined(WEBRTC_ANDROID)
242 
243 // Linux ADM implementation.
244 // Note that, WEBRTC_ENABLE_LINUX_ALSA is always defined by default when
245 // WEBRTC_LINUX is defined. WEBRTC_ENABLE_LINUX_PULSE depends on the
246 // 'rtc_include_pulse_audio' build flag.
247 // TODO(bugs.webrtc.org/9127): improve support and make it more clear that
248 // PulseAudio is the default selection.
249 #elif defined(WEBRTC_LINUX)
250 #if !defined(WEBRTC_ENABLE_LINUX_PULSE)
251   // Build flag 'rtc_include_pulse_audio' is set to false. In this mode:
252   // - kPlatformDefaultAudio => ALSA, and
253   // - kLinuxAlsaAudio => ALSA, and
254   // - kLinuxPulseAudio => Invalid selection.
255   RTC_LOG(WARNING) << "PulseAudio is disabled using build flag.";
256   if ((audio_layer == kLinuxAlsaAudio) ||
257       (audio_layer == kPlatformDefaultAudio)) {
258     audio_device_.reset(new AudioDeviceLinuxALSA());
259     RTC_LOG(INFO) << "Linux ALSA APIs will be utilized.";
260   }
261 #else
262   // Build flag 'rtc_include_pulse_audio' is set to true (default). In this
263   // mode:
264   // - kPlatformDefaultAudio => PulseAudio, and
265   // - kLinuxPulseAudio => PulseAudio, and
266   // - kLinuxAlsaAudio => ALSA (supported but not default).
267   RTC_LOG(INFO) << "PulseAudio support is enabled.";
268   if ((audio_layer == kLinuxPulseAudio) ||
269       (audio_layer == kPlatformDefaultAudio)) {
270     // Linux PulseAudio implementation is default.
271     audio_device_.reset(new AudioDeviceLinuxPulse());
272     RTC_LOG(INFO) << "Linux PulseAudio APIs will be utilized";
273   } else if (audio_layer == kLinuxAlsaAudio) {
274     audio_device_.reset(new AudioDeviceLinuxALSA());
275     RTC_LOG(WARNING) << "Linux ALSA APIs will be utilized.";
276   }
277 #endif  // #if !defined(WEBRTC_ENABLE_LINUX_PULSE)
278 #endif  // #if defined(WEBRTC_LINUX)
279 
280 // iOS ADM implementation.
281 #if defined(WEBRTC_IOS)
282   if (audio_layer == kPlatformDefaultAudio) {
283     audio_device_.reset(new ios_adm::AudioDeviceIOS());
284     RTC_LOG(INFO) << "iPhone Audio APIs will be utilized.";
285   }
286 // END #if defined(WEBRTC_IOS)
287 
288 // Mac OS X ADM implementation.
289 #elif defined(WEBRTC_MAC)
290   if (audio_layer == kPlatformDefaultAudio) {
291     audio_device_.reset(new AudioDeviceMac());
292     RTC_LOG(INFO) << "Mac OS X Audio APIs will be utilized.";
293   }
294 #endif  // WEBRTC_MAC
295 
296   // Dummy ADM implementation.
297   if (audio_layer == kDummyAudio) {
298     audio_device_.reset(new AudioDeviceDummy());
299     RTC_LOG(INFO) << "Dummy Audio APIs will be utilized.";
300   }
301 #endif  // if defined(WEBRTC_DUMMY_AUDIO_BUILD)
302 
303   if (!audio_device_) {
304     RTC_LOG(LS_ERROR)
305         << "Failed to create the platform specific ADM implementation.";
306     return -1;
307   }
308   return 0;
309 }
310 
AttachAudioBuffer()311 int32_t AudioDeviceModuleImpl::AttachAudioBuffer() {
312   RTC_LOG(INFO) << __FUNCTION__;
313   audio_device_->AttachAudioBuffer(&audio_device_buffer_);
314   return 0;
315 }
316 
~AudioDeviceModuleImpl()317 AudioDeviceModuleImpl::~AudioDeviceModuleImpl() {
318   RTC_LOG(INFO) << __FUNCTION__;
319 }
320 
ActiveAudioLayer(AudioLayer * audioLayer) const321 int32_t AudioDeviceModuleImpl::ActiveAudioLayer(AudioLayer* audioLayer) const {
322   RTC_LOG(INFO) << __FUNCTION__;
323   AudioLayer activeAudio;
324   if (audio_device_->ActiveAudioLayer(activeAudio) == -1) {
325     return -1;
326   }
327   *audioLayer = activeAudio;
328   return 0;
329 }
330 
Init()331 int32_t AudioDeviceModuleImpl::Init() {
332   RTC_LOG(INFO) << __FUNCTION__;
333   if (initialized_)
334     return 0;
335   RTC_CHECK(audio_device_);
336   AudioDeviceGeneric::InitStatus status = audio_device_->Init();
337   RTC_HISTOGRAM_ENUMERATION(
338       "WebRTC.Audio.InitializationResult", static_cast<int>(status),
339       static_cast<int>(AudioDeviceGeneric::InitStatus::NUM_STATUSES));
340   if (status != AudioDeviceGeneric::InitStatus::OK) {
341     RTC_LOG(LS_ERROR) << "Audio device initialization failed.";
342     return -1;
343   }
344   initialized_ = true;
345   return 0;
346 }
347 
Terminate()348 int32_t AudioDeviceModuleImpl::Terminate() {
349   RTC_LOG(INFO) << __FUNCTION__;
350   if (!initialized_)
351     return 0;
352   if (audio_device_->Terminate() == -1) {
353     return -1;
354   }
355   initialized_ = false;
356   return 0;
357 }
358 
Initialized() const359 bool AudioDeviceModuleImpl::Initialized() const {
360   RTC_LOG(INFO) << __FUNCTION__ << ": " << initialized_;
361   return initialized_;
362 }
363 
InitSpeaker()364 int32_t AudioDeviceModuleImpl::InitSpeaker() {
365   RTC_LOG(INFO) << __FUNCTION__;
366   CHECKinitialized_();
367   return audio_device_->InitSpeaker();
368 }
369 
InitMicrophone()370 int32_t AudioDeviceModuleImpl::InitMicrophone() {
371   RTC_LOG(INFO) << __FUNCTION__;
372   CHECKinitialized_();
373   return audio_device_->InitMicrophone();
374 }
375 
SpeakerVolumeIsAvailable(bool * available)376 int32_t AudioDeviceModuleImpl::SpeakerVolumeIsAvailable(bool* available) {
377   RTC_LOG(INFO) << __FUNCTION__;
378   CHECKinitialized_();
379   bool isAvailable = false;
380   if (audio_device_->SpeakerVolumeIsAvailable(isAvailable) == -1) {
381     return -1;
382   }
383   *available = isAvailable;
384   RTC_LOG(INFO) << "output: " << isAvailable;
385   return 0;
386 }
387 
SetSpeakerVolume(uint32_t volume)388 int32_t AudioDeviceModuleImpl::SetSpeakerVolume(uint32_t volume) {
389   RTC_LOG(INFO) << __FUNCTION__ << "(" << volume << ")";
390   CHECKinitialized_();
391   return audio_device_->SetSpeakerVolume(volume);
392 }
393 
SpeakerVolume(uint32_t * volume) const394 int32_t AudioDeviceModuleImpl::SpeakerVolume(uint32_t* volume) const {
395   RTC_LOG(INFO) << __FUNCTION__;
396   CHECKinitialized_();
397   uint32_t level = 0;
398   if (audio_device_->SpeakerVolume(level) == -1) {
399     return -1;
400   }
401   *volume = level;
402   RTC_LOG(INFO) << "output: " << *volume;
403   return 0;
404 }
405 
SpeakerIsInitialized() const406 bool AudioDeviceModuleImpl::SpeakerIsInitialized() const {
407   RTC_LOG(INFO) << __FUNCTION__;
408   CHECKinitialized__BOOL();
409   bool isInitialized = audio_device_->SpeakerIsInitialized();
410   RTC_LOG(INFO) << "output: " << isInitialized;
411   return isInitialized;
412 }
413 
MicrophoneIsInitialized() const414 bool AudioDeviceModuleImpl::MicrophoneIsInitialized() const {
415   RTC_LOG(INFO) << __FUNCTION__;
416   CHECKinitialized__BOOL();
417   bool isInitialized = audio_device_->MicrophoneIsInitialized();
418   RTC_LOG(INFO) << "output: " << isInitialized;
419   return isInitialized;
420 }
421 
MaxSpeakerVolume(uint32_t * maxVolume) const422 int32_t AudioDeviceModuleImpl::MaxSpeakerVolume(uint32_t* maxVolume) const {
423   CHECKinitialized_();
424   uint32_t maxVol = 0;
425   if (audio_device_->MaxSpeakerVolume(maxVol) == -1) {
426     return -1;
427   }
428   *maxVolume = maxVol;
429   return 0;
430 }
431 
MinSpeakerVolume(uint32_t * minVolume) const432 int32_t AudioDeviceModuleImpl::MinSpeakerVolume(uint32_t* minVolume) const {
433   CHECKinitialized_();
434   uint32_t minVol = 0;
435   if (audio_device_->MinSpeakerVolume(minVol) == -1) {
436     return -1;
437   }
438   *minVolume = minVol;
439   return 0;
440 }
441 
SpeakerMuteIsAvailable(bool * available)442 int32_t AudioDeviceModuleImpl::SpeakerMuteIsAvailable(bool* available) {
443   RTC_LOG(INFO) << __FUNCTION__;
444   CHECKinitialized_();
445   bool isAvailable = false;
446   if (audio_device_->SpeakerMuteIsAvailable(isAvailable) == -1) {
447     return -1;
448   }
449   *available = isAvailable;
450   RTC_LOG(INFO) << "output: " << isAvailable;
451   return 0;
452 }
453 
SetSpeakerMute(bool enable)454 int32_t AudioDeviceModuleImpl::SetSpeakerMute(bool enable) {
455   RTC_LOG(INFO) << __FUNCTION__ << "(" << enable << ")";
456   CHECKinitialized_();
457   return audio_device_->SetSpeakerMute(enable);
458 }
459 
SpeakerMute(bool * enabled) const460 int32_t AudioDeviceModuleImpl::SpeakerMute(bool* enabled) const {
461   RTC_LOG(INFO) << __FUNCTION__;
462   CHECKinitialized_();
463   bool muted = false;
464   if (audio_device_->SpeakerMute(muted) == -1) {
465     return -1;
466   }
467   *enabled = muted;
468   RTC_LOG(INFO) << "output: " << muted;
469   return 0;
470 }
471 
MicrophoneMuteIsAvailable(bool * available)472 int32_t AudioDeviceModuleImpl::MicrophoneMuteIsAvailable(bool* available) {
473   RTC_LOG(INFO) << __FUNCTION__;
474   CHECKinitialized_();
475   bool isAvailable = false;
476   if (audio_device_->MicrophoneMuteIsAvailable(isAvailable) == -1) {
477     return -1;
478   }
479   *available = isAvailable;
480   RTC_LOG(INFO) << "output: " << isAvailable;
481   return 0;
482 }
483 
SetMicrophoneMute(bool enable)484 int32_t AudioDeviceModuleImpl::SetMicrophoneMute(bool enable) {
485   RTC_LOG(INFO) << __FUNCTION__ << "(" << enable << ")";
486   CHECKinitialized_();
487   return (audio_device_->SetMicrophoneMute(enable));
488 }
489 
MicrophoneMute(bool * enabled) const490 int32_t AudioDeviceModuleImpl::MicrophoneMute(bool* enabled) const {
491   RTC_LOG(INFO) << __FUNCTION__;
492   CHECKinitialized_();
493   bool muted = false;
494   if (audio_device_->MicrophoneMute(muted) == -1) {
495     return -1;
496   }
497   *enabled = muted;
498   RTC_LOG(INFO) << "output: " << muted;
499   return 0;
500 }
501 
MicrophoneVolumeIsAvailable(bool * available)502 int32_t AudioDeviceModuleImpl::MicrophoneVolumeIsAvailable(bool* available) {
503   RTC_LOG(INFO) << __FUNCTION__;
504   CHECKinitialized_();
505   bool isAvailable = false;
506   if (audio_device_->MicrophoneVolumeIsAvailable(isAvailable) == -1) {
507     return -1;
508   }
509   *available = isAvailable;
510   RTC_LOG(INFO) << "output: " << isAvailable;
511   return 0;
512 }
513 
SetMicrophoneVolume(uint32_t volume)514 int32_t AudioDeviceModuleImpl::SetMicrophoneVolume(uint32_t volume) {
515   RTC_LOG(INFO) << __FUNCTION__ << "(" << volume << ")";
516   CHECKinitialized_();
517   return (audio_device_->SetMicrophoneVolume(volume));
518 }
519 
MicrophoneVolume(uint32_t * volume) const520 int32_t AudioDeviceModuleImpl::MicrophoneVolume(uint32_t* volume) const {
521   RTC_LOG(INFO) << __FUNCTION__;
522   CHECKinitialized_();
523   uint32_t level = 0;
524   if (audio_device_->MicrophoneVolume(level) == -1) {
525     return -1;
526   }
527   *volume = level;
528   RTC_LOG(INFO) << "output: " << *volume;
529   return 0;
530 }
531 
StereoRecordingIsAvailable(bool * available) const532 int32_t AudioDeviceModuleImpl::StereoRecordingIsAvailable(
533     bool* available) const {
534   RTC_LOG(INFO) << __FUNCTION__;
535   CHECKinitialized_();
536   bool isAvailable = false;
537   if (audio_device_->StereoRecordingIsAvailable(isAvailable) == -1) {
538     return -1;
539   }
540   *available = isAvailable;
541   RTC_LOG(INFO) << "output: " << isAvailable;
542   return 0;
543 }
544 
SetStereoRecording(bool enable)545 int32_t AudioDeviceModuleImpl::SetStereoRecording(bool enable) {
546   RTC_LOG(INFO) << __FUNCTION__ << "(" << enable << ")";
547   CHECKinitialized_();
548   if (audio_device_->RecordingIsInitialized()) {
549     RTC_LOG(LERROR)
550         << "unable to set stereo mode after recording is initialized";
551     return -1;
552   }
553   if (audio_device_->SetStereoRecording(enable) == -1) {
554     if (enable) {
555       RTC_LOG(WARNING) << "failed to enable stereo recording";
556     }
557     return -1;
558   }
559   int8_t nChannels(1);
560   if (enable) {
561     nChannels = 2;
562   }
563   audio_device_buffer_.SetRecordingChannels(nChannels);
564   return 0;
565 }
566 
StereoRecording(bool * enabled) const567 int32_t AudioDeviceModuleImpl::StereoRecording(bool* enabled) const {
568   RTC_LOG(INFO) << __FUNCTION__;
569   CHECKinitialized_();
570   bool stereo = false;
571   if (audio_device_->StereoRecording(stereo) == -1) {
572     return -1;
573   }
574   *enabled = stereo;
575   RTC_LOG(INFO) << "output: " << stereo;
576   return 0;
577 }
578 
StereoPlayoutIsAvailable(bool * available) const579 int32_t AudioDeviceModuleImpl::StereoPlayoutIsAvailable(bool* available) const {
580   RTC_LOG(INFO) << __FUNCTION__;
581   CHECKinitialized_();
582   bool isAvailable = false;
583   if (audio_device_->StereoPlayoutIsAvailable(isAvailable) == -1) {
584     return -1;
585   }
586   *available = isAvailable;
587   RTC_LOG(INFO) << "output: " << isAvailable;
588   return 0;
589 }
590 
SetStereoPlayout(bool enable)591 int32_t AudioDeviceModuleImpl::SetStereoPlayout(bool enable) {
592   RTC_LOG(INFO) << __FUNCTION__ << "(" << enable << ")";
593   CHECKinitialized_();
594   if (audio_device_->PlayoutIsInitialized()) {
595     RTC_LOG(LERROR)
596         << "unable to set stereo mode while playing side is initialized";
597     return -1;
598   }
599   if (audio_device_->SetStereoPlayout(enable)) {
600     RTC_LOG(WARNING) << "stereo playout is not supported";
601     return -1;
602   }
603   int8_t nChannels(1);
604   if (enable) {
605     nChannels = 2;
606   }
607   audio_device_buffer_.SetPlayoutChannels(nChannels);
608   return 0;
609 }
610 
StereoPlayout(bool * enabled) const611 int32_t AudioDeviceModuleImpl::StereoPlayout(bool* enabled) const {
612   RTC_LOG(INFO) << __FUNCTION__;
613   CHECKinitialized_();
614   bool stereo = false;
615   if (audio_device_->StereoPlayout(stereo) == -1) {
616     return -1;
617   }
618   *enabled = stereo;
619   RTC_LOG(INFO) << "output: " << stereo;
620   return 0;
621 }
622 
PlayoutIsAvailable(bool * available)623 int32_t AudioDeviceModuleImpl::PlayoutIsAvailable(bool* available) {
624   RTC_LOG(INFO) << __FUNCTION__;
625   CHECKinitialized_();
626   bool isAvailable = false;
627   if (audio_device_->PlayoutIsAvailable(isAvailable) == -1) {
628     return -1;
629   }
630   *available = isAvailable;
631   RTC_LOG(INFO) << "output: " << isAvailable;
632   return 0;
633 }
634 
RecordingIsAvailable(bool * available)635 int32_t AudioDeviceModuleImpl::RecordingIsAvailable(bool* available) {
636   RTC_LOG(INFO) << __FUNCTION__;
637   CHECKinitialized_();
638   bool isAvailable = false;
639   if (audio_device_->RecordingIsAvailable(isAvailable) == -1) {
640     return -1;
641   }
642   *available = isAvailable;
643   RTC_LOG(INFO) << "output: " << isAvailable;
644   return 0;
645 }
646 
MaxMicrophoneVolume(uint32_t * maxVolume) const647 int32_t AudioDeviceModuleImpl::MaxMicrophoneVolume(uint32_t* maxVolume) const {
648   CHECKinitialized_();
649   uint32_t maxVol(0);
650   if (audio_device_->MaxMicrophoneVolume(maxVol) == -1) {
651     return -1;
652   }
653   *maxVolume = maxVol;
654   return 0;
655 }
656 
MinMicrophoneVolume(uint32_t * minVolume) const657 int32_t AudioDeviceModuleImpl::MinMicrophoneVolume(uint32_t* minVolume) const {
658   CHECKinitialized_();
659   uint32_t minVol(0);
660   if (audio_device_->MinMicrophoneVolume(minVol) == -1) {
661     return -1;
662   }
663   *minVolume = minVol;
664   return 0;
665 }
666 
PlayoutDevices()667 int16_t AudioDeviceModuleImpl::PlayoutDevices() {
668   RTC_LOG(INFO) << __FUNCTION__;
669   CHECKinitialized_();
670   uint16_t nPlayoutDevices = audio_device_->PlayoutDevices();
671   RTC_LOG(INFO) << "output: " << nPlayoutDevices;
672   return (int16_t)(nPlayoutDevices);
673 }
674 
SetPlayoutDevice(uint16_t index)675 int32_t AudioDeviceModuleImpl::SetPlayoutDevice(uint16_t index) {
676   RTC_LOG(INFO) << __FUNCTION__ << "(" << index << ")";
677   CHECKinitialized_();
678   return audio_device_->SetPlayoutDevice(index);
679 }
680 
SetPlayoutDevice(WindowsDeviceType device)681 int32_t AudioDeviceModuleImpl::SetPlayoutDevice(WindowsDeviceType device) {
682   RTC_LOG(INFO) << __FUNCTION__;
683   CHECKinitialized_();
684   return audio_device_->SetPlayoutDevice(device);
685 }
686 
PlayoutDeviceName(uint16_t index,char name[kAdmMaxDeviceNameSize],char guid[kAdmMaxGuidSize])687 int32_t AudioDeviceModuleImpl::PlayoutDeviceName(
688     uint16_t index,
689     char name[kAdmMaxDeviceNameSize],
690     char guid[kAdmMaxGuidSize]) {
691   RTC_LOG(INFO) << __FUNCTION__ << "(" << index << ", ...)";
692   CHECKinitialized_();
693   if (name == NULL) {
694     return -1;
695   }
696   if (audio_device_->PlayoutDeviceName(index, name, guid) == -1) {
697     return -1;
698   }
699   if (name != NULL) {
700     RTC_LOG(INFO) << "output: name = " << name;
701   }
702   if (guid != NULL) {
703     RTC_LOG(INFO) << "output: guid = " << guid;
704   }
705   return 0;
706 }
707 
RecordingDeviceName(uint16_t index,char name[kAdmMaxDeviceNameSize],char guid[kAdmMaxGuidSize])708 int32_t AudioDeviceModuleImpl::RecordingDeviceName(
709     uint16_t index,
710     char name[kAdmMaxDeviceNameSize],
711     char guid[kAdmMaxGuidSize]) {
712   RTC_LOG(INFO) << __FUNCTION__ << "(" << index << ", ...)";
713   CHECKinitialized_();
714   if (name == NULL) {
715     return -1;
716   }
717   if (audio_device_->RecordingDeviceName(index, name, guid) == -1) {
718     return -1;
719   }
720   if (name != NULL) {
721     RTC_LOG(INFO) << "output: name = " << name;
722   }
723   if (guid != NULL) {
724     RTC_LOG(INFO) << "output: guid = " << guid;
725   }
726   return 0;
727 }
728 
RecordingDevices()729 int16_t AudioDeviceModuleImpl::RecordingDevices() {
730   RTC_LOG(INFO) << __FUNCTION__;
731   CHECKinitialized_();
732   uint16_t nRecordingDevices = audio_device_->RecordingDevices();
733   RTC_LOG(INFO) << "output: " << nRecordingDevices;
734   return (int16_t)nRecordingDevices;
735 }
736 
SetRecordingDevice(uint16_t index)737 int32_t AudioDeviceModuleImpl::SetRecordingDevice(uint16_t index) {
738   RTC_LOG(INFO) << __FUNCTION__ << "(" << index << ")";
739   CHECKinitialized_();
740   return audio_device_->SetRecordingDevice(index);
741 }
742 
SetRecordingDevice(WindowsDeviceType device)743 int32_t AudioDeviceModuleImpl::SetRecordingDevice(WindowsDeviceType device) {
744   RTC_LOG(INFO) << __FUNCTION__;
745   CHECKinitialized_();
746   return audio_device_->SetRecordingDevice(device);
747 }
748 
InitPlayout()749 int32_t AudioDeviceModuleImpl::InitPlayout() {
750   RTC_LOG(INFO) << __FUNCTION__;
751   CHECKinitialized_();
752   if (PlayoutIsInitialized()) {
753     return 0;
754   }
755   int32_t result = audio_device_->InitPlayout();
756   RTC_LOG(INFO) << "output: " << result;
757   RTC_HISTOGRAM_BOOLEAN("WebRTC.Audio.InitPlayoutSuccess",
758                         static_cast<int>(result == 0));
759   return result;
760 }
761 
InitRecording()762 int32_t AudioDeviceModuleImpl::InitRecording() {
763   RTC_LOG(INFO) << __FUNCTION__;
764   CHECKinitialized_();
765   if (RecordingIsInitialized()) {
766     return 0;
767   }
768   int32_t result = audio_device_->InitRecording();
769   RTC_LOG(INFO) << "output: " << result;
770   RTC_HISTOGRAM_BOOLEAN("WebRTC.Audio.InitRecordingSuccess",
771                         static_cast<int>(result == 0));
772   return result;
773 }
774 
PlayoutIsInitialized() const775 bool AudioDeviceModuleImpl::PlayoutIsInitialized() const {
776   RTC_LOG(INFO) << __FUNCTION__;
777   CHECKinitialized__BOOL();
778   return audio_device_->PlayoutIsInitialized();
779 }
780 
RecordingIsInitialized() const781 bool AudioDeviceModuleImpl::RecordingIsInitialized() const {
782   RTC_LOG(INFO) << __FUNCTION__;
783   CHECKinitialized__BOOL();
784   return audio_device_->RecordingIsInitialized();
785 }
786 
StartPlayout()787 int32_t AudioDeviceModuleImpl::StartPlayout() {
788   RTC_LOG(INFO) << __FUNCTION__;
789   CHECKinitialized_();
790   if (Playing()) {
791     return 0;
792   }
793   audio_device_buffer_.StartPlayout();
794   int32_t result = audio_device_->StartPlayout();
795   RTC_LOG(INFO) << "output: " << result;
796   RTC_HISTOGRAM_BOOLEAN("WebRTC.Audio.StartPlayoutSuccess",
797                         static_cast<int>(result == 0));
798   return result;
799 }
800 
StopPlayout()801 int32_t AudioDeviceModuleImpl::StopPlayout() {
802   RTC_LOG(INFO) << __FUNCTION__;
803   CHECKinitialized_();
804   int32_t result = audio_device_->StopPlayout();
805   audio_device_buffer_.StopPlayout();
806   RTC_LOG(INFO) << "output: " << result;
807   RTC_HISTOGRAM_BOOLEAN("WebRTC.Audio.StopPlayoutSuccess",
808                         static_cast<int>(result == 0));
809   return result;
810 }
811 
Playing() const812 bool AudioDeviceModuleImpl::Playing() const {
813   RTC_LOG(INFO) << __FUNCTION__;
814   CHECKinitialized__BOOL();
815   return audio_device_->Playing();
816 }
817 
StartRecording()818 int32_t AudioDeviceModuleImpl::StartRecording() {
819   RTC_LOG(INFO) << __FUNCTION__;
820   CHECKinitialized_();
821   if (Recording()) {
822     return 0;
823   }
824   audio_device_buffer_.StartRecording();
825   int32_t result = audio_device_->StartRecording();
826   RTC_LOG(INFO) << "output: " << result;
827   RTC_HISTOGRAM_BOOLEAN("WebRTC.Audio.StartRecordingSuccess",
828                         static_cast<int>(result == 0));
829   return result;
830 }
831 
StopRecording()832 int32_t AudioDeviceModuleImpl::StopRecording() {
833   RTC_LOG(INFO) << __FUNCTION__;
834   CHECKinitialized_();
835   int32_t result = audio_device_->StopRecording();
836   audio_device_buffer_.StopRecording();
837   RTC_LOG(INFO) << "output: " << result;
838   RTC_HISTOGRAM_BOOLEAN("WebRTC.Audio.StopRecordingSuccess",
839                         static_cast<int>(result == 0));
840   return result;
841 }
842 
Recording() const843 bool AudioDeviceModuleImpl::Recording() const {
844   RTC_LOG(INFO) << __FUNCTION__;
845   CHECKinitialized__BOOL();
846   return audio_device_->Recording();
847 }
848 
RegisterAudioCallback(AudioTransport * audioCallback)849 int32_t AudioDeviceModuleImpl::RegisterAudioCallback(
850     AudioTransport* audioCallback) {
851   RTC_LOG(INFO) << __FUNCTION__;
852   return audio_device_buffer_.RegisterAudioCallback(audioCallback);
853 }
854 
PlayoutDelay(uint16_t * delayMS) const855 int32_t AudioDeviceModuleImpl::PlayoutDelay(uint16_t* delayMS) const {
856   CHECKinitialized_();
857   uint16_t delay = 0;
858   if (audio_device_->PlayoutDelay(delay) == -1) {
859     RTC_LOG(LERROR) << "failed to retrieve the playout delay";
860     return -1;
861   }
862   *delayMS = delay;
863   return 0;
864 }
865 
BuiltInAECIsAvailable() const866 bool AudioDeviceModuleImpl::BuiltInAECIsAvailable() const {
867   RTC_LOG(INFO) << __FUNCTION__;
868   CHECKinitialized__BOOL();
869   bool isAvailable = audio_device_->BuiltInAECIsAvailable();
870   RTC_LOG(INFO) << "output: " << isAvailable;
871   return isAvailable;
872 }
873 
EnableBuiltInAEC(bool enable)874 int32_t AudioDeviceModuleImpl::EnableBuiltInAEC(bool enable) {
875   RTC_LOG(INFO) << __FUNCTION__ << "(" << enable << ")";
876   CHECKinitialized_();
877   int32_t ok = audio_device_->EnableBuiltInAEC(enable);
878   RTC_LOG(INFO) << "output: " << ok;
879   return ok;
880 }
881 
BuiltInAGCIsAvailable() const882 bool AudioDeviceModuleImpl::BuiltInAGCIsAvailable() const {
883   RTC_LOG(INFO) << __FUNCTION__;
884   CHECKinitialized__BOOL();
885   bool isAvailable = audio_device_->BuiltInAGCIsAvailable();
886   RTC_LOG(INFO) << "output: " << isAvailable;
887   return isAvailable;
888 }
889 
EnableBuiltInAGC(bool enable)890 int32_t AudioDeviceModuleImpl::EnableBuiltInAGC(bool enable) {
891   RTC_LOG(INFO) << __FUNCTION__ << "(" << enable << ")";
892   CHECKinitialized_();
893   int32_t ok = audio_device_->EnableBuiltInAGC(enable);
894   RTC_LOG(INFO) << "output: " << ok;
895   return ok;
896 }
897 
BuiltInNSIsAvailable() const898 bool AudioDeviceModuleImpl::BuiltInNSIsAvailable() const {
899   RTC_LOG(INFO) << __FUNCTION__;
900   CHECKinitialized__BOOL();
901   bool isAvailable = audio_device_->BuiltInNSIsAvailable();
902   RTC_LOG(INFO) << "output: " << isAvailable;
903   return isAvailable;
904 }
905 
EnableBuiltInNS(bool enable)906 int32_t AudioDeviceModuleImpl::EnableBuiltInNS(bool enable) {
907   RTC_LOG(INFO) << __FUNCTION__ << "(" << enable << ")";
908   CHECKinitialized_();
909   int32_t ok = audio_device_->EnableBuiltInNS(enable);
910   RTC_LOG(INFO) << "output: " << ok;
911   return ok;
912 }
913 
GetPlayoutUnderrunCount() const914 int32_t AudioDeviceModuleImpl::GetPlayoutUnderrunCount() const {
915   RTC_LOG(INFO) << __FUNCTION__;
916   CHECKinitialized_();
917   int32_t underrunCount = audio_device_->GetPlayoutUnderrunCount();
918   RTC_LOG(INFO) << "output: " << underrunCount;
919   return underrunCount;
920 }
921 
922 #if defined(WEBRTC_IOS)
GetPlayoutAudioParameters(AudioParameters * params) const923 int AudioDeviceModuleImpl::GetPlayoutAudioParameters(
924     AudioParameters* params) const {
925   RTC_LOG(INFO) << __FUNCTION__;
926   int r = audio_device_->GetPlayoutAudioParameters(params);
927   RTC_LOG(INFO) << "output: " << r;
928   return r;
929 }
930 
GetRecordAudioParameters(AudioParameters * params) const931 int AudioDeviceModuleImpl::GetRecordAudioParameters(
932     AudioParameters* params) const {
933   RTC_LOG(INFO) << __FUNCTION__;
934   int r = audio_device_->GetRecordAudioParameters(params);
935   RTC_LOG(INFO) << "output: " << r;
936   return r;
937 }
938 #endif  // WEBRTC_IOS
939 
Platform() const940 AudioDeviceModuleImpl::PlatformType AudioDeviceModuleImpl::Platform() const {
941   RTC_LOG(INFO) << __FUNCTION__;
942   return platform_type_;
943 }
944 
PlatformAudioLayer() const945 AudioDeviceModule::AudioLayer AudioDeviceModuleImpl::PlatformAudioLayer()
946     const {
947   RTC_LOG(INFO) << __FUNCTION__;
948   return audio_layer_;
949 }
950 
951 }  // namespace webrtc
952