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 "webrtc/common_audio/signal_processing/include/signal_processing_library.h"
12 #include "webrtc/modules/audio_device/audio_device_config.h"
13 #include "webrtc/modules/audio_device/audio_device_impl.h"
14 #include "webrtc/system_wrappers/include/ref_count.h"
15 #include "webrtc/system_wrappers/include/tick_util.h"
16
17 #include <assert.h>
18 #include <string.h>
19
20 #if defined(_WIN32)
21 #include "audio_device_wave_win.h"
22 #if defined(WEBRTC_WINDOWS_CORE_AUDIO_BUILD)
23 #include "audio_device_core_win.h"
24 #endif
25 #elif defined(WEBRTC_ANDROID)
26 #include <stdlib.h>
27 #include "webrtc/modules/audio_device/android/audio_device_template.h"
28 #include "webrtc/modules/audio_device/android/audio_manager.h"
29 #include "webrtc/modules/audio_device/android/audio_record_jni.h"
30 #include "webrtc/modules/audio_device/android/audio_track_jni.h"
31 #include "webrtc/modules/audio_device/android/opensles_player.h"
32 #elif defined(WEBRTC_LINUX)
33 #if defined(LINUX_ALSA)
34 #include "audio_device_alsa_linux.h"
35 #endif
36 #if defined(LINUX_PULSE)
37 #include "audio_device_pulse_linux.h"
38 #endif
39 #elif defined(WEBRTC_IOS)
40 #include "audio_device_ios.h"
41 #elif defined(WEBRTC_MAC)
42 #include "audio_device_mac.h"
43 #endif
44
45 #if defined(WEBRTC_DUMMY_FILE_DEVICES)
46 #include "webrtc/modules/audio_device/dummy/file_audio_device_factory.h"
47 #endif
48
49 #include "webrtc/modules/audio_device/dummy/audio_device_dummy.h"
50 #include "webrtc/modules/audio_device/dummy/file_audio_device.h"
51 #include "webrtc/system_wrappers/include/critical_section_wrapper.h"
52 #include "webrtc/system_wrappers/include/trace.h"
53
54 #define CHECK_INITIALIZED() \
55 { \
56 if (!_initialized) { \
57 return -1; \
58 }; \
59 }
60
61 #define CHECK_INITIALIZED_BOOL() \
62 { \
63 if (!_initialized) { \
64 return false; \
65 }; \
66 }
67
68 namespace webrtc
69 {
70
CreateAudioDeviceModule(int32_t id,AudioDeviceModule::AudioLayer audioLayer)71 AudioDeviceModule* CreateAudioDeviceModule(
72 int32_t id, AudioDeviceModule::AudioLayer audioLayer) {
73 return AudioDeviceModuleImpl::Create(id, audioLayer);
74 }
75
76 // ============================================================================
77 // Static methods
78 // ============================================================================
79
80 // ----------------------------------------------------------------------------
81 // AudioDeviceModule::Create()
82 // ----------------------------------------------------------------------------
83
Create(const int32_t id,const AudioLayer audioLayer)84 AudioDeviceModule* AudioDeviceModuleImpl::Create(const int32_t id,
85 const AudioLayer audioLayer)
86 {
87 // Create the generic ref counted (platform independent) implementation.
88 RefCountImpl<AudioDeviceModuleImpl>* audioDevice =
89 new RefCountImpl<AudioDeviceModuleImpl>(id, audioLayer);
90
91 // Ensure that the current platform is supported.
92 if (audioDevice->CheckPlatform() == -1)
93 {
94 delete audioDevice;
95 return NULL;
96 }
97
98 // Create the platform-dependent implementation.
99 if (audioDevice->CreatePlatformSpecificObjects() == -1)
100 {
101 delete audioDevice;
102 return NULL;
103 }
104
105 // Ensure that the generic audio buffer can communicate with the
106 // platform-specific parts.
107 if (audioDevice->AttachAudioBuffer() == -1)
108 {
109 delete audioDevice;
110 return NULL;
111 }
112
113 WebRtcSpl_Init();
114
115 return audioDevice;
116 }
117
118 // ============================================================================
119 // Construction & Destruction
120 // ============================================================================
121
122 // ----------------------------------------------------------------------------
123 // AudioDeviceModuleImpl - ctor
124 // ----------------------------------------------------------------------------
125
AudioDeviceModuleImpl(const int32_t id,const AudioLayer audioLayer)126 AudioDeviceModuleImpl::AudioDeviceModuleImpl(const int32_t id, const AudioLayer audioLayer) :
127 _critSect(*CriticalSectionWrapper::CreateCriticalSection()),
128 _critSectEventCb(*CriticalSectionWrapper::CreateCriticalSection()),
129 _critSectAudioCb(*CriticalSectionWrapper::CreateCriticalSection()),
130 _ptrCbAudioDeviceObserver(NULL),
131 _ptrAudioDevice(NULL),
132 _id(id),
133 _platformAudioLayer(audioLayer),
134 _lastProcessTime(TickTime::MillisecondTimestamp()),
135 _platformType(kPlatformNotSupported),
136 _initialized(false),
137 _lastError(kAdmErrNone)
138 {
139 WEBRTC_TRACE(kTraceMemory, kTraceAudioDevice, id, "%s created", __FUNCTION__);
140 }
141
142 // ----------------------------------------------------------------------------
143 // CheckPlatform
144 // ----------------------------------------------------------------------------
145
CheckPlatform()146 int32_t AudioDeviceModuleImpl::CheckPlatform()
147 {
148 WEBRTC_TRACE(kTraceInfo, kTraceAudioDevice, _id, "%s", __FUNCTION__);
149
150 // Ensure that the current platform is supported
151 //
152 PlatformType platform(kPlatformNotSupported);
153
154 #if defined(_WIN32)
155 platform = kPlatformWin32;
156 WEBRTC_TRACE(kTraceInfo, kTraceAudioDevice, _id, "current platform is WIN32");
157 #elif defined(WEBRTC_ANDROID)
158 platform = kPlatformAndroid;
159 WEBRTC_TRACE(kTraceInfo, kTraceAudioDevice, _id, "current platform is ANDROID");
160 #elif defined(WEBRTC_LINUX)
161 platform = kPlatformLinux;
162 WEBRTC_TRACE(kTraceInfo, kTraceAudioDevice, _id, "current platform is LINUX");
163 #elif defined(WEBRTC_IOS)
164 platform = kPlatformIOS;
165 WEBRTC_TRACE(kTraceInfo, kTraceAudioDevice, _id, "current platform is IOS");
166 #elif defined(WEBRTC_MAC)
167 platform = kPlatformMac;
168 WEBRTC_TRACE(kTraceInfo, kTraceAudioDevice, _id, "current platform is MAC");
169 #endif
170
171 if (platform == kPlatformNotSupported)
172 {
173 WEBRTC_TRACE(kTraceCritical, kTraceAudioDevice, _id, "current platform is not supported => this module will self destruct!");
174 return -1;
175 }
176
177 // Store valid output results
178 //
179 _platformType = platform;
180
181 return 0;
182 }
183
184
185 // ----------------------------------------------------------------------------
186 // CreatePlatformSpecificObjects
187 // ----------------------------------------------------------------------------
188
CreatePlatformSpecificObjects()189 int32_t AudioDeviceModuleImpl::CreatePlatformSpecificObjects()
190 {
191 WEBRTC_TRACE(kTraceInfo, kTraceAudioDevice, _id, "%s", __FUNCTION__);
192
193 AudioDeviceGeneric* ptrAudioDevice(NULL);
194
195 #if defined(WEBRTC_DUMMY_AUDIO_BUILD)
196 ptrAudioDevice = new AudioDeviceDummy(Id());
197 WEBRTC_TRACE(kTraceInfo, kTraceAudioDevice, _id, "Dummy Audio APIs will be utilized");
198 #elif defined(WEBRTC_DUMMY_FILE_DEVICES)
199 ptrAudioDevice = FileAudioDeviceFactory::CreateFileAudioDevice(Id());
200 WEBRTC_TRACE(kTraceInfo, kTraceAudioDevice, _id,
201 "Will use file-playing dummy device.");
202 #else
203 AudioLayer audioLayer(PlatformAudioLayer());
204
205 // Create the *Windows* implementation of the Audio Device
206 //
207 #if defined(_WIN32)
208 if ((audioLayer == kWindowsWaveAudio)
209 #if !defined(WEBRTC_WINDOWS_CORE_AUDIO_BUILD)
210 // Wave audio is default if Core audio is not supported in this build
211 || (audioLayer == kPlatformDefaultAudio)
212 #endif
213 )
214 {
215 // create *Windows Wave Audio* implementation
216 ptrAudioDevice = new AudioDeviceWindowsWave(Id());
217 WEBRTC_TRACE(kTraceInfo, kTraceAudioDevice, _id, "Windows Wave APIs will be utilized");
218 }
219 #if defined(WEBRTC_WINDOWS_CORE_AUDIO_BUILD)
220 if ((audioLayer == kWindowsCoreAudio) ||
221 (audioLayer == kPlatformDefaultAudio)
222 )
223 {
224 WEBRTC_TRACE(kTraceInfo, kTraceAudioDevice, _id, "attempting to use the Windows Core Audio APIs...");
225
226 if (AudioDeviceWindowsCore::CoreAudioIsSupported())
227 {
228 // create *Windows Core Audio* implementation
229 ptrAudioDevice = new AudioDeviceWindowsCore(Id());
230 WEBRTC_TRACE(kTraceInfo, kTraceAudioDevice, _id, "Windows Core Audio APIs will be utilized");
231 }
232 else
233 {
234 // create *Windows Wave Audio* implementation
235 ptrAudioDevice = new AudioDeviceWindowsWave(Id());
236 if (ptrAudioDevice != NULL)
237 {
238 // Core Audio was not supported => revert to Windows Wave instead
239 _platformAudioLayer = kWindowsWaveAudio; // modify the state set at construction
240 WEBRTC_TRACE(kTraceWarning, kTraceAudioDevice, _id, "Windows Core Audio is *not* supported => Wave APIs will be utilized instead");
241 }
242 }
243 }
244 #endif // defined(WEBRTC_WINDOWS_CORE_AUDIO_BUILD)
245 #endif // #if defined(_WIN32)
246
247 #if defined(WEBRTC_ANDROID)
248 // Create an Android audio manager.
249 _audioManagerAndroid.reset(new AudioManager());
250 // Select best possible combination of audio layers.
251 if (audioLayer == kPlatformDefaultAudio) {
252 if (_audioManagerAndroid->IsLowLatencyPlayoutSupported()) {
253 // Always use OpenSL ES for output on devices that supports the
254 // low-latency output audio path.
255 audioLayer = kAndroidJavaInputAndOpenSLESOutputAudio;
256 } else {
257 // Use Java-based audio in both directions when low-latency output
258 // is not supported.
259 audioLayer = kAndroidJavaAudio;
260 }
261 }
262 AudioManager* audio_manager = _audioManagerAndroid.get();
263 if (audioLayer == kAndroidJavaAudio) {
264 // Java audio for both input and output audio.
265 ptrAudioDevice = new AudioDeviceTemplate<AudioRecordJni, AudioTrackJni>(
266 audioLayer, audio_manager);
267 } else if (audioLayer == kAndroidJavaInputAndOpenSLESOutputAudio) {
268 // Java audio for input and OpenSL ES for output audio (i.e. mixed APIs).
269 // This combination provides low-latency output audio and at the same
270 // time support for HW AEC using the AudioRecord Java API.
271 ptrAudioDevice = new AudioDeviceTemplate<AudioRecordJni, OpenSLESPlayer>(
272 audioLayer, audio_manager);
273 } else {
274 // Invalid audio layer.
275 ptrAudioDevice = NULL;
276 }
277 // END #if defined(WEBRTC_ANDROID)
278
279 // Create the *Linux* implementation of the Audio Device
280 //
281 #elif defined(WEBRTC_LINUX)
282 if ((audioLayer == kLinuxPulseAudio) || (audioLayer == kPlatformDefaultAudio))
283 {
284 #if defined(LINUX_PULSE)
285 WEBRTC_TRACE(kTraceInfo, kTraceAudioDevice, _id, "attempting to use the Linux PulseAudio APIs...");
286
287 // create *Linux PulseAudio* implementation
288 AudioDeviceLinuxPulse* pulseDevice = new AudioDeviceLinuxPulse(Id());
289 if (pulseDevice->Init() != -1)
290 {
291 ptrAudioDevice = pulseDevice;
292 WEBRTC_TRACE(kTraceInfo, kTraceAudioDevice, _id, "Linux PulseAudio APIs will be utilized");
293 }
294 else
295 {
296 delete pulseDevice;
297 #endif
298 #if defined(LINUX_ALSA)
299 // create *Linux ALSA Audio* implementation
300 ptrAudioDevice = new AudioDeviceLinuxALSA(Id());
301 if (ptrAudioDevice != NULL)
302 {
303 // Pulse Audio was not supported => revert to ALSA instead
304 _platformAudioLayer = kLinuxAlsaAudio; // modify the state set at construction
305 WEBRTC_TRACE(kTraceWarning, kTraceAudioDevice, _id, "Linux PulseAudio is *not* supported => ALSA APIs will be utilized instead");
306 }
307 #endif
308 #if defined(LINUX_PULSE)
309 }
310 #endif
311 }
312 else if (audioLayer == kLinuxAlsaAudio)
313 {
314 #if defined(LINUX_ALSA)
315 // create *Linux ALSA Audio* implementation
316 ptrAudioDevice = new AudioDeviceLinuxALSA(Id());
317 WEBRTC_TRACE(kTraceInfo, kTraceAudioDevice, _id, "Linux ALSA APIs will be utilized");
318 #endif
319 }
320 #endif // #if defined(WEBRTC_LINUX)
321
322 // Create the *iPhone* implementation of the Audio Device
323 //
324 #if defined(WEBRTC_IOS)
325 if (audioLayer == kPlatformDefaultAudio)
326 {
327 // Create iOS Audio Device implementation.
328 ptrAudioDevice = new AudioDeviceIOS();
329 WEBRTC_TRACE(kTraceInfo, kTraceAudioDevice, _id, "iPhone Audio APIs will be utilized");
330 }
331 // END #if defined(WEBRTC_IOS)
332
333 // Create the *Mac* implementation of the Audio Device
334 //
335 #elif defined(WEBRTC_MAC)
336 if (audioLayer == kPlatformDefaultAudio)
337 {
338 // Create *Mac Audio* implementation
339 ptrAudioDevice = new AudioDeviceMac(Id());
340 WEBRTC_TRACE(kTraceInfo, kTraceAudioDevice, _id, "Mac OS X Audio APIs will be utilized");
341 }
342 #endif // WEBRTC_MAC
343
344 // Create the *Dummy* implementation of the Audio Device
345 // Available for all platforms
346 //
347 if (audioLayer == kDummyAudio)
348 {
349 // Create *Dummy Audio* implementation
350 assert(!ptrAudioDevice);
351 ptrAudioDevice = new AudioDeviceDummy(Id());
352 WEBRTC_TRACE(kTraceInfo, kTraceAudioDevice, _id, "Dummy Audio APIs will be utilized");
353 }
354 #endif // if defined(WEBRTC_DUMMY_AUDIO_BUILD)
355
356 if (ptrAudioDevice == NULL)
357 {
358 WEBRTC_TRACE(kTraceCritical, kTraceAudioDevice, _id, "unable to create the platform specific audio device implementation");
359 return -1;
360 }
361
362 // Store valid output pointers
363 //
364 _ptrAudioDevice = ptrAudioDevice;
365
366 return 0;
367 }
368
369 // ----------------------------------------------------------------------------
370 // AttachAudioBuffer
371 //
372 // Install "bridge" between the platform implemetation and the generic
373 // implementation. The "child" shall set the native sampling rate and the
374 // number of channels in this function call.
375 // ----------------------------------------------------------------------------
376
AttachAudioBuffer()377 int32_t AudioDeviceModuleImpl::AttachAudioBuffer()
378 {
379 WEBRTC_TRACE(kTraceInfo, kTraceAudioDevice, _id, "%s", __FUNCTION__);
380
381 _audioDeviceBuffer.SetId(_id);
382 _ptrAudioDevice->AttachAudioBuffer(&_audioDeviceBuffer);
383 return 0;
384 }
385
386 // ----------------------------------------------------------------------------
387 // ~AudioDeviceModuleImpl - dtor
388 // ----------------------------------------------------------------------------
389
~AudioDeviceModuleImpl()390 AudioDeviceModuleImpl::~AudioDeviceModuleImpl()
391 {
392 WEBRTC_TRACE(kTraceMemory, kTraceAudioDevice, _id, "%s destroyed", __FUNCTION__);
393
394 if (_ptrAudioDevice)
395 {
396 delete _ptrAudioDevice;
397 _ptrAudioDevice = NULL;
398 }
399
400 delete &_critSect;
401 delete &_critSectEventCb;
402 delete &_critSectAudioCb;
403 }
404
405 // ============================================================================
406 // Module
407 // ============================================================================
408
409 // ----------------------------------------------------------------------------
410 // Module::TimeUntilNextProcess
411 //
412 // Returns the number of milliseconds until the module want a worker thread
413 // to call Process().
414 // ----------------------------------------------------------------------------
415
TimeUntilNextProcess()416 int64_t AudioDeviceModuleImpl::TimeUntilNextProcess()
417 {
418 int64_t now = TickTime::MillisecondTimestamp();
419 int64_t deltaProcess = kAdmMaxIdleTimeProcess - (now - _lastProcessTime);
420 return deltaProcess;
421 }
422
423 // ----------------------------------------------------------------------------
424 // Module::Process
425 //
426 // Check for posted error and warning reports. Generate callbacks if
427 // new reports exists.
428 // ----------------------------------------------------------------------------
429
Process()430 int32_t AudioDeviceModuleImpl::Process()
431 {
432
433 _lastProcessTime = TickTime::MillisecondTimestamp();
434
435 // kPlayoutWarning
436 if (_ptrAudioDevice->PlayoutWarning())
437 {
438 CriticalSectionScoped lock(&_critSectEventCb);
439 if (_ptrCbAudioDeviceObserver)
440 {
441 WEBRTC_TRACE(kTraceWarning, kTraceAudioDevice, _id, "=> OnWarningIsReported(kPlayoutWarning)");
442 _ptrCbAudioDeviceObserver->OnWarningIsReported(AudioDeviceObserver::kPlayoutWarning);
443 }
444 _ptrAudioDevice->ClearPlayoutWarning();
445 }
446
447 // kPlayoutError
448 if (_ptrAudioDevice->PlayoutError())
449 {
450 CriticalSectionScoped lock(&_critSectEventCb);
451 if (_ptrCbAudioDeviceObserver)
452 {
453 WEBRTC_TRACE(kTraceError, kTraceAudioDevice, _id, "=> OnErrorIsReported(kPlayoutError)");
454 _ptrCbAudioDeviceObserver->OnErrorIsReported(AudioDeviceObserver::kPlayoutError);
455 }
456 _ptrAudioDevice->ClearPlayoutError();
457 }
458
459 // kRecordingWarning
460 if (_ptrAudioDevice->RecordingWarning())
461 {
462 CriticalSectionScoped lock(&_critSectEventCb);
463 if (_ptrCbAudioDeviceObserver)
464 {
465 WEBRTC_TRACE(kTraceWarning, kTraceAudioDevice, _id, "=> OnWarningIsReported(kRecordingWarning)");
466 _ptrCbAudioDeviceObserver->OnWarningIsReported(AudioDeviceObserver::kRecordingWarning);
467 }
468 _ptrAudioDevice->ClearRecordingWarning();
469 }
470
471 // kRecordingError
472 if (_ptrAudioDevice->RecordingError())
473 {
474 CriticalSectionScoped lock(&_critSectEventCb);
475 if (_ptrCbAudioDeviceObserver)
476 {
477 WEBRTC_TRACE(kTraceError, kTraceAudioDevice, _id, "=> OnErrorIsReported(kRecordingError)");
478 _ptrCbAudioDeviceObserver->OnErrorIsReported(AudioDeviceObserver::kRecordingError);
479 }
480 _ptrAudioDevice->ClearRecordingError();
481 }
482
483 return 0;
484 }
485
486 // ============================================================================
487 // Public API
488 // ============================================================================
489
490 // ----------------------------------------------------------------------------
491 // ActiveAudioLayer
492 // ----------------------------------------------------------------------------
493
ActiveAudioLayer(AudioLayer * audioLayer) const494 int32_t AudioDeviceModuleImpl::ActiveAudioLayer(AudioLayer* audioLayer) const {
495 AudioLayer activeAudio;
496 if (_ptrAudioDevice->ActiveAudioLayer(activeAudio) == -1) {
497 return -1;
498 }
499 *audioLayer = activeAudio;
500 return 0;
501 }
502
503 // ----------------------------------------------------------------------------
504 // LastError
505 // ----------------------------------------------------------------------------
506
LastError() const507 AudioDeviceModule::ErrorCode AudioDeviceModuleImpl::LastError() const
508 {
509 return _lastError;
510 }
511
512 // ----------------------------------------------------------------------------
513 // Init
514 // ----------------------------------------------------------------------------
515
Init()516 int32_t AudioDeviceModuleImpl::Init()
517 {
518
519 if (_initialized)
520 return 0;
521
522 if (!_ptrAudioDevice)
523 return -1;
524
525 if (_ptrAudioDevice->Init() == -1)
526 {
527 return -1;
528 }
529
530 _initialized = true;
531 return 0;
532 }
533
534 // ----------------------------------------------------------------------------
535 // Terminate
536 // ----------------------------------------------------------------------------
537
Terminate()538 int32_t AudioDeviceModuleImpl::Terminate()
539 {
540
541 if (!_initialized)
542 return 0;
543
544 if (_ptrAudioDevice->Terminate() == -1)
545 {
546 return -1;
547 }
548
549 _initialized = false;
550 return 0;
551 }
552
553 // ----------------------------------------------------------------------------
554 // Initialized
555 // ----------------------------------------------------------------------------
556
Initialized() const557 bool AudioDeviceModuleImpl::Initialized() const
558 {
559
560 WEBRTC_TRACE(kTraceStateInfo, kTraceAudioDevice, _id, "output: %d", _initialized);
561 return (_initialized);
562 }
563
564 // ----------------------------------------------------------------------------
565 // InitSpeaker
566 // ----------------------------------------------------------------------------
567
InitSpeaker()568 int32_t AudioDeviceModuleImpl::InitSpeaker()
569 {
570 CHECK_INITIALIZED();
571 return (_ptrAudioDevice->InitSpeaker());
572 }
573
574 // ----------------------------------------------------------------------------
575 // InitMicrophone
576 // ----------------------------------------------------------------------------
577
InitMicrophone()578 int32_t AudioDeviceModuleImpl::InitMicrophone()
579 {
580 CHECK_INITIALIZED();
581 return (_ptrAudioDevice->InitMicrophone());
582 }
583
584 // ----------------------------------------------------------------------------
585 // SpeakerVolumeIsAvailable
586 // ----------------------------------------------------------------------------
587
SpeakerVolumeIsAvailable(bool * available)588 int32_t AudioDeviceModuleImpl::SpeakerVolumeIsAvailable(bool* available)
589 {
590 CHECK_INITIALIZED();
591
592 bool isAvailable(0);
593
594 if (_ptrAudioDevice->SpeakerVolumeIsAvailable(isAvailable) == -1)
595 {
596 return -1;
597 }
598
599 *available = isAvailable;
600
601 WEBRTC_TRACE(kTraceStateInfo, kTraceAudioDevice, _id, "output: available=%d", *available);
602 return (0);
603 }
604
605 // ----------------------------------------------------------------------------
606 // SetSpeakerVolume
607 // ----------------------------------------------------------------------------
608
SetSpeakerVolume(uint32_t volume)609 int32_t AudioDeviceModuleImpl::SetSpeakerVolume(uint32_t volume)
610 {
611 CHECK_INITIALIZED();
612 return (_ptrAudioDevice->SetSpeakerVolume(volume));
613 }
614
615 // ----------------------------------------------------------------------------
616 // SpeakerVolume
617 // ----------------------------------------------------------------------------
618
SpeakerVolume(uint32_t * volume) const619 int32_t AudioDeviceModuleImpl::SpeakerVolume(uint32_t* volume) const
620 {
621 CHECK_INITIALIZED();
622
623 uint32_t level(0);
624
625 if (_ptrAudioDevice->SpeakerVolume(level) == -1)
626 {
627 return -1;
628 }
629
630 *volume = level;
631
632 WEBRTC_TRACE(kTraceStateInfo, kTraceAudioDevice, _id, "output: volume=%u", *volume);
633 return (0);
634 }
635
636 // ----------------------------------------------------------------------------
637 // SetWaveOutVolume
638 // ----------------------------------------------------------------------------
639
SetWaveOutVolume(uint16_t volumeLeft,uint16_t volumeRight)640 int32_t AudioDeviceModuleImpl::SetWaveOutVolume(uint16_t volumeLeft, uint16_t volumeRight)
641 {
642 CHECK_INITIALIZED();
643 return (_ptrAudioDevice->SetWaveOutVolume(volumeLeft, volumeRight));
644 }
645
646 // ----------------------------------------------------------------------------
647 // WaveOutVolume
648 // ----------------------------------------------------------------------------
649
WaveOutVolume(uint16_t * volumeLeft,uint16_t * volumeRight) const650 int32_t AudioDeviceModuleImpl::WaveOutVolume(uint16_t* volumeLeft, uint16_t* volumeRight) const
651 {
652 CHECK_INITIALIZED();
653
654 uint16_t volLeft(0);
655 uint16_t volRight(0);
656
657 if (_ptrAudioDevice->WaveOutVolume(volLeft, volRight) == -1)
658 {
659 return -1;
660 }
661
662 *volumeLeft = volLeft;
663 *volumeRight = volRight;
664
665 WEBRTC_TRACE(kTraceStateInfo, kTraceAudioDevice, _id, "outputs: volumeLeft=%u, volumeRight=%u",
666 *volumeLeft, *volumeRight);
667
668 return (0);
669 }
670
671 // ----------------------------------------------------------------------------
672 // SpeakerIsInitialized
673 // ----------------------------------------------------------------------------
674
SpeakerIsInitialized() const675 bool AudioDeviceModuleImpl::SpeakerIsInitialized() const
676 {
677 CHECK_INITIALIZED_BOOL();
678
679 bool isInitialized = _ptrAudioDevice->SpeakerIsInitialized();
680
681 WEBRTC_TRACE(kTraceStateInfo, kTraceAudioDevice, _id, "output: %d", isInitialized);
682 return (isInitialized);
683 }
684
685 // ----------------------------------------------------------------------------
686 // MicrophoneIsInitialized
687 // ----------------------------------------------------------------------------
688
MicrophoneIsInitialized() const689 bool AudioDeviceModuleImpl::MicrophoneIsInitialized() const
690 {
691 CHECK_INITIALIZED_BOOL();
692
693 bool isInitialized = _ptrAudioDevice->MicrophoneIsInitialized();
694
695 WEBRTC_TRACE(kTraceStateInfo, kTraceAudioDevice, _id, "output: %d", isInitialized);
696 return (isInitialized);
697 }
698
699 // ----------------------------------------------------------------------------
700 // MaxSpeakerVolume
701 // ----------------------------------------------------------------------------
702
MaxSpeakerVolume(uint32_t * maxVolume) const703 int32_t AudioDeviceModuleImpl::MaxSpeakerVolume(uint32_t* maxVolume) const
704 {
705 CHECK_INITIALIZED();
706
707 uint32_t maxVol(0);
708
709 if (_ptrAudioDevice->MaxSpeakerVolume(maxVol) == -1)
710 {
711 return -1;
712 }
713
714 *maxVolume = maxVol;
715
716 WEBRTC_TRACE(kTraceStateInfo, kTraceAudioDevice, _id, "output: maxVolume=%d", *maxVolume);
717 return (0);
718 }
719
720 // ----------------------------------------------------------------------------
721 // MinSpeakerVolume
722 // ----------------------------------------------------------------------------
723
MinSpeakerVolume(uint32_t * minVolume) const724 int32_t AudioDeviceModuleImpl::MinSpeakerVolume(uint32_t* minVolume) const
725 {
726 CHECK_INITIALIZED();
727
728 uint32_t minVol(0);
729
730 if (_ptrAudioDevice->MinSpeakerVolume(minVol) == -1)
731 {
732 return -1;
733 }
734
735 *minVolume = minVol;
736
737 WEBRTC_TRACE(kTraceStateInfo, kTraceAudioDevice, _id, "output: minVolume=%u", *minVolume);
738 return (0);
739 }
740
741 // ----------------------------------------------------------------------------
742 // SpeakerVolumeStepSize
743 // ----------------------------------------------------------------------------
744
SpeakerVolumeStepSize(uint16_t * stepSize) const745 int32_t AudioDeviceModuleImpl::SpeakerVolumeStepSize(uint16_t* stepSize) const
746 {
747 CHECK_INITIALIZED();
748
749 uint16_t delta(0);
750
751 if (_ptrAudioDevice->SpeakerVolumeStepSize(delta) == -1)
752 {
753 WEBRTC_TRACE(kTraceError, kTraceAudioDevice, _id, "failed to retrieve the speaker-volume step size");
754 return -1;
755 }
756
757 *stepSize = delta;
758
759 WEBRTC_TRACE(kTraceStateInfo, kTraceAudioDevice, _id, "output: stepSize=%u", *stepSize);
760 return (0);
761 }
762
763 // ----------------------------------------------------------------------------
764 // SpeakerMuteIsAvailable
765 // ----------------------------------------------------------------------------
766
SpeakerMuteIsAvailable(bool * available)767 int32_t AudioDeviceModuleImpl::SpeakerMuteIsAvailable(bool* available)
768 {
769 CHECK_INITIALIZED();
770
771 bool isAvailable(0);
772
773 if (_ptrAudioDevice->SpeakerMuteIsAvailable(isAvailable) == -1)
774 {
775 return -1;
776 }
777
778 *available = isAvailable;
779
780 WEBRTC_TRACE(kTraceStateInfo, kTraceAudioDevice, _id, "output: available=%d", *available);
781 return (0);
782 }
783
784 // ----------------------------------------------------------------------------
785 // SetSpeakerMute
786 // ----------------------------------------------------------------------------
787
SetSpeakerMute(bool enable)788 int32_t AudioDeviceModuleImpl::SetSpeakerMute(bool enable)
789 {
790 CHECK_INITIALIZED();
791 return (_ptrAudioDevice->SetSpeakerMute(enable));
792 }
793
794 // ----------------------------------------------------------------------------
795 // SpeakerMute
796 // ----------------------------------------------------------------------------
797
SpeakerMute(bool * enabled) const798 int32_t AudioDeviceModuleImpl::SpeakerMute(bool* enabled) const
799 {
800 CHECK_INITIALIZED();
801
802 bool muted(false);
803
804 if (_ptrAudioDevice->SpeakerMute(muted) == -1)
805 {
806 return -1;
807 }
808
809 *enabled = muted;
810
811 WEBRTC_TRACE(kTraceStateInfo, kTraceAudioDevice, _id, "output: enabled=%u", *enabled);
812 return (0);
813 }
814
815 // ----------------------------------------------------------------------------
816 // MicrophoneMuteIsAvailable
817 // ----------------------------------------------------------------------------
818
MicrophoneMuteIsAvailable(bool * available)819 int32_t AudioDeviceModuleImpl::MicrophoneMuteIsAvailable(bool* available)
820 {
821 CHECK_INITIALIZED();
822
823 bool isAvailable(0);
824
825 if (_ptrAudioDevice->MicrophoneMuteIsAvailable(isAvailable) == -1)
826 {
827 return -1;
828 }
829
830 *available = isAvailable;
831
832 WEBRTC_TRACE(kTraceStateInfo, kTraceAudioDevice, _id, "output: available=%d", *available);
833 return (0);
834 }
835
836 // ----------------------------------------------------------------------------
837 // SetMicrophoneMute
838 // ----------------------------------------------------------------------------
839
SetMicrophoneMute(bool enable)840 int32_t AudioDeviceModuleImpl::SetMicrophoneMute(bool enable)
841 {
842 CHECK_INITIALIZED();
843 return (_ptrAudioDevice->SetMicrophoneMute(enable));
844 }
845
846 // ----------------------------------------------------------------------------
847 // MicrophoneMute
848 // ----------------------------------------------------------------------------
849
MicrophoneMute(bool * enabled) const850 int32_t AudioDeviceModuleImpl::MicrophoneMute(bool* enabled) const
851 {
852 CHECK_INITIALIZED();
853
854 bool muted(false);
855
856 if (_ptrAudioDevice->MicrophoneMute(muted) == -1)
857 {
858 return -1;
859 }
860
861 *enabled = muted;
862
863 WEBRTC_TRACE(kTraceStateInfo, kTraceAudioDevice, _id, "output: enabled=%u", *enabled);
864 return (0);
865 }
866
867 // ----------------------------------------------------------------------------
868 // MicrophoneBoostIsAvailable
869 // ----------------------------------------------------------------------------
870
MicrophoneBoostIsAvailable(bool * available)871 int32_t AudioDeviceModuleImpl::MicrophoneBoostIsAvailable(bool* available)
872 {
873 CHECK_INITIALIZED();
874
875 bool isAvailable(0);
876
877 if (_ptrAudioDevice->MicrophoneBoostIsAvailable(isAvailable) == -1)
878 {
879 return -1;
880 }
881
882 *available = isAvailable;
883
884 WEBRTC_TRACE(kTraceStateInfo, kTraceAudioDevice, _id, "output: available=%d", *available);
885 return (0);
886 }
887
888 // ----------------------------------------------------------------------------
889 // SetMicrophoneBoost
890 // ----------------------------------------------------------------------------
891
SetMicrophoneBoost(bool enable)892 int32_t AudioDeviceModuleImpl::SetMicrophoneBoost(bool enable)
893 {
894 CHECK_INITIALIZED();
895 return (_ptrAudioDevice->SetMicrophoneBoost(enable));
896 }
897
898 // ----------------------------------------------------------------------------
899 // MicrophoneBoost
900 // ----------------------------------------------------------------------------
901
MicrophoneBoost(bool * enabled) const902 int32_t AudioDeviceModuleImpl::MicrophoneBoost(bool* enabled) const
903 {
904 CHECK_INITIALIZED();
905
906 bool onOff(false);
907
908 if (_ptrAudioDevice->MicrophoneBoost(onOff) == -1)
909 {
910 return -1;
911 }
912
913 *enabled = onOff;
914
915 WEBRTC_TRACE(kTraceStateInfo, kTraceAudioDevice, _id, "output: enabled=%u", *enabled);
916 return (0);
917 }
918
919 // ----------------------------------------------------------------------------
920 // MicrophoneVolumeIsAvailable
921 // ----------------------------------------------------------------------------
922
MicrophoneVolumeIsAvailable(bool * available)923 int32_t AudioDeviceModuleImpl::MicrophoneVolumeIsAvailable(bool* available)
924 {
925 CHECK_INITIALIZED();
926
927 bool isAvailable(0);
928
929 if (_ptrAudioDevice->MicrophoneVolumeIsAvailable(isAvailable) == -1)
930 {
931 return -1;
932 }
933
934 *available = isAvailable;
935
936 WEBRTC_TRACE(kTraceStateInfo, kTraceAudioDevice, _id, "output: available=%d", *available);
937 return (0);
938 }
939
940 // ----------------------------------------------------------------------------
941 // SetMicrophoneVolume
942 // ----------------------------------------------------------------------------
943
SetMicrophoneVolume(uint32_t volume)944 int32_t AudioDeviceModuleImpl::SetMicrophoneVolume(uint32_t volume)
945 {
946 CHECK_INITIALIZED();
947 return (_ptrAudioDevice->SetMicrophoneVolume(volume));
948 }
949
950 // ----------------------------------------------------------------------------
951 // MicrophoneVolume
952 // ----------------------------------------------------------------------------
953
MicrophoneVolume(uint32_t * volume) const954 int32_t AudioDeviceModuleImpl::MicrophoneVolume(uint32_t* volume) const
955 {
956 WEBRTC_TRACE(kTraceStream, kTraceAudioDevice, _id, "%s", __FUNCTION__);
957 CHECK_INITIALIZED();
958
959 uint32_t level(0);
960
961 if (_ptrAudioDevice->MicrophoneVolume(level) == -1)
962 {
963 return -1;
964 }
965
966 *volume = level;
967
968 WEBRTC_TRACE(kTraceStream, kTraceAudioDevice, _id, "output: volume=%u", *volume);
969 return (0);
970 }
971
972 // ----------------------------------------------------------------------------
973 // StereoRecordingIsAvailable
974 // ----------------------------------------------------------------------------
975
StereoRecordingIsAvailable(bool * available) const976 int32_t AudioDeviceModuleImpl::StereoRecordingIsAvailable(bool* available) const
977 {
978 CHECK_INITIALIZED();
979
980 bool isAvailable(0);
981
982 if (_ptrAudioDevice->StereoRecordingIsAvailable(isAvailable) == -1)
983 {
984 return -1;
985 }
986
987 *available = isAvailable;
988
989 WEBRTC_TRACE(kTraceStateInfo, kTraceAudioDevice, _id, "output: available=%d", *available);
990 return (0);
991 }
992
993 // ----------------------------------------------------------------------------
994 // SetStereoRecording
995 // ----------------------------------------------------------------------------
996
SetStereoRecording(bool enable)997 int32_t AudioDeviceModuleImpl::SetStereoRecording(bool enable)
998 {
999 CHECK_INITIALIZED();
1000
1001 if (_ptrAudioDevice->RecordingIsInitialized())
1002 {
1003 WEBRTC_TRACE(kTraceError, kTraceAudioDevice, _id, "recording in stereo is not supported");
1004 return -1;
1005 }
1006
1007 if (_ptrAudioDevice->SetStereoRecording(enable) == -1)
1008 {
1009 WEBRTC_TRACE(kTraceError, kTraceAudioDevice, _id, "failed to enable stereo recording");
1010 return -1;
1011 }
1012
1013 int8_t nChannels(1);
1014 if (enable)
1015 {
1016 nChannels = 2;
1017 }
1018 _audioDeviceBuffer.SetRecordingChannels(nChannels);
1019
1020 return 0;
1021 }
1022
1023 // ----------------------------------------------------------------------------
1024 // StereoRecording
1025 // ----------------------------------------------------------------------------
1026
StereoRecording(bool * enabled) const1027 int32_t AudioDeviceModuleImpl::StereoRecording(bool* enabled) const
1028 {
1029 CHECK_INITIALIZED();
1030
1031 bool stereo(false);
1032
1033 if (_ptrAudioDevice->StereoRecording(stereo) == -1)
1034 {
1035 return -1;
1036 }
1037
1038 *enabled = stereo;
1039
1040 WEBRTC_TRACE(kTraceStateInfo, kTraceAudioDevice, _id, "output: enabled=%u", *enabled);
1041 return (0);
1042 }
1043
1044 // ----------------------------------------------------------------------------
1045 // SetRecordingChannel
1046 // ----------------------------------------------------------------------------
1047
SetRecordingChannel(const ChannelType channel)1048 int32_t AudioDeviceModuleImpl::SetRecordingChannel(const ChannelType channel)
1049 {
1050 if (channel == kChannelBoth)
1051 {
1052 }
1053 else if (channel == kChannelLeft)
1054 {
1055 }
1056 else
1057 {
1058 }
1059 CHECK_INITIALIZED();
1060
1061 bool stereo(false);
1062
1063 if (_ptrAudioDevice->StereoRecording(stereo) == -1)
1064 {
1065 WEBRTC_TRACE(kTraceError, kTraceAudioDevice, _id, "recording in stereo is not supported");
1066 return -1;
1067 }
1068
1069 return (_audioDeviceBuffer.SetRecordingChannel(channel));
1070 }
1071
1072 // ----------------------------------------------------------------------------
1073 // RecordingChannel
1074 // ----------------------------------------------------------------------------
1075
RecordingChannel(ChannelType * channel) const1076 int32_t AudioDeviceModuleImpl::RecordingChannel(ChannelType* channel) const
1077 {
1078 CHECK_INITIALIZED();
1079
1080 ChannelType chType;
1081
1082 if (_audioDeviceBuffer.RecordingChannel(chType) == -1)
1083 {
1084 return -1;
1085 }
1086
1087 *channel = chType;
1088
1089 if (*channel == kChannelBoth)
1090 {
1091 }
1092 else if (*channel == kChannelLeft)
1093 {
1094 }
1095 else
1096 {
1097 }
1098
1099 return (0);
1100 }
1101
1102 // ----------------------------------------------------------------------------
1103 // StereoPlayoutIsAvailable
1104 // ----------------------------------------------------------------------------
1105
StereoPlayoutIsAvailable(bool * available) const1106 int32_t AudioDeviceModuleImpl::StereoPlayoutIsAvailable(bool* available) const
1107 {
1108 CHECK_INITIALIZED();
1109
1110 bool isAvailable(0);
1111
1112 if (_ptrAudioDevice->StereoPlayoutIsAvailable(isAvailable) == -1)
1113 {
1114 return -1;
1115 }
1116
1117 *available = isAvailable;
1118
1119 WEBRTC_TRACE(kTraceStateInfo, kTraceAudioDevice, _id, "output: available=%d", *available);
1120 return (0);
1121 }
1122
1123 // ----------------------------------------------------------------------------
1124 // SetStereoPlayout
1125 // ----------------------------------------------------------------------------
1126
SetStereoPlayout(bool enable)1127 int32_t AudioDeviceModuleImpl::SetStereoPlayout(bool enable)
1128 {
1129 CHECK_INITIALIZED();
1130
1131 if (_ptrAudioDevice->PlayoutIsInitialized())
1132 {
1133 WEBRTC_TRACE(kTraceError, kTraceAudioDevice, _id, "unable to set stereo mode while playing side is initialized");
1134 return -1;
1135 }
1136
1137 if (_ptrAudioDevice->SetStereoPlayout(enable))
1138 {
1139 WEBRTC_TRACE(kTraceError, kTraceAudioDevice, _id, "stereo playout is not supported");
1140 return -1;
1141 }
1142
1143 int8_t nChannels(1);
1144 if (enable)
1145 {
1146 nChannels = 2;
1147 }
1148 _audioDeviceBuffer.SetPlayoutChannels(nChannels);
1149
1150 return 0;
1151 }
1152
1153 // ----------------------------------------------------------------------------
1154 // StereoPlayout
1155 // ----------------------------------------------------------------------------
1156
StereoPlayout(bool * enabled) const1157 int32_t AudioDeviceModuleImpl::StereoPlayout(bool* enabled) const
1158 {
1159 CHECK_INITIALIZED();
1160
1161 bool stereo(false);
1162
1163 if (_ptrAudioDevice->StereoPlayout(stereo) == -1)
1164 {
1165 return -1;
1166 }
1167
1168 *enabled = stereo;
1169
1170 WEBRTC_TRACE(kTraceStateInfo, kTraceAudioDevice, _id, "output: enabled=%u", *enabled);
1171 return (0);
1172 }
1173
1174 // ----------------------------------------------------------------------------
1175 // SetAGC
1176 // ----------------------------------------------------------------------------
1177
SetAGC(bool enable)1178 int32_t AudioDeviceModuleImpl::SetAGC(bool enable)
1179 {
1180 CHECK_INITIALIZED();
1181 return (_ptrAudioDevice->SetAGC(enable));
1182 }
1183
1184 // ----------------------------------------------------------------------------
1185 // AGC
1186 // ----------------------------------------------------------------------------
1187
AGC() const1188 bool AudioDeviceModuleImpl::AGC() const
1189 {
1190 CHECK_INITIALIZED_BOOL();
1191 return (_ptrAudioDevice->AGC());
1192 }
1193
1194 // ----------------------------------------------------------------------------
1195 // PlayoutIsAvailable
1196 // ----------------------------------------------------------------------------
1197
PlayoutIsAvailable(bool * available)1198 int32_t AudioDeviceModuleImpl::PlayoutIsAvailable(bool* available)
1199 {
1200 CHECK_INITIALIZED();
1201
1202 bool isAvailable(0);
1203
1204 if (_ptrAudioDevice->PlayoutIsAvailable(isAvailable) == -1)
1205 {
1206 return -1;
1207 }
1208
1209 *available = isAvailable;
1210
1211 WEBRTC_TRACE(kTraceStateInfo, kTraceAudioDevice, _id, "output: available=%d", *available);
1212 return (0);
1213 }
1214
1215 // ----------------------------------------------------------------------------
1216 // RecordingIsAvailable
1217 // ----------------------------------------------------------------------------
1218
RecordingIsAvailable(bool * available)1219 int32_t AudioDeviceModuleImpl::RecordingIsAvailable(bool* available)
1220 {
1221 CHECK_INITIALIZED();
1222
1223 bool isAvailable(0);
1224
1225 if (_ptrAudioDevice->RecordingIsAvailable(isAvailable) == -1)
1226 {
1227 return -1;
1228 }
1229
1230 *available = isAvailable;
1231
1232 WEBRTC_TRACE(kTraceStateInfo, kTraceAudioDevice, _id, "output: available=%d", *available);
1233 return (0);
1234 }
1235
1236 // ----------------------------------------------------------------------------
1237 // MaxMicrophoneVolume
1238 // ----------------------------------------------------------------------------
1239
MaxMicrophoneVolume(uint32_t * maxVolume) const1240 int32_t AudioDeviceModuleImpl::MaxMicrophoneVolume(uint32_t* maxVolume) const
1241 {
1242 WEBRTC_TRACE(kTraceStream, kTraceAudioDevice, _id, "%s", __FUNCTION__);
1243 CHECK_INITIALIZED();
1244
1245 uint32_t maxVol(0);
1246
1247 if (_ptrAudioDevice->MaxMicrophoneVolume(maxVol) == -1)
1248 {
1249 return -1;
1250 }
1251
1252 *maxVolume = maxVol;
1253
1254 WEBRTC_TRACE(kTraceStream, kTraceAudioDevice, _id, "output: maxVolume=%d", *maxVolume);
1255 return (0);
1256 }
1257
1258 // ----------------------------------------------------------------------------
1259 // MinMicrophoneVolume
1260 // ----------------------------------------------------------------------------
1261
MinMicrophoneVolume(uint32_t * minVolume) const1262 int32_t AudioDeviceModuleImpl::MinMicrophoneVolume(uint32_t* minVolume) const
1263 {
1264 CHECK_INITIALIZED();
1265
1266 uint32_t minVol(0);
1267
1268 if (_ptrAudioDevice->MinMicrophoneVolume(minVol) == -1)
1269 {
1270 return -1;
1271 }
1272
1273 *minVolume = minVol;
1274
1275 WEBRTC_TRACE(kTraceStateInfo, kTraceAudioDevice, _id, "output: minVolume=%u", *minVolume);
1276 return (0);
1277 }
1278
1279 // ----------------------------------------------------------------------------
1280 // MicrophoneVolumeStepSize
1281 // ----------------------------------------------------------------------------
1282
MicrophoneVolumeStepSize(uint16_t * stepSize) const1283 int32_t AudioDeviceModuleImpl::MicrophoneVolumeStepSize(uint16_t* stepSize) const
1284 {
1285 CHECK_INITIALIZED();
1286
1287 uint16_t delta(0);
1288
1289 if (_ptrAudioDevice->MicrophoneVolumeStepSize(delta) == -1)
1290 {
1291 return -1;
1292 }
1293
1294 *stepSize = delta;
1295
1296 WEBRTC_TRACE(kTraceStateInfo, kTraceAudioDevice, _id, "output: stepSize=%u", *stepSize);
1297 return (0);
1298 }
1299
1300 // ----------------------------------------------------------------------------
1301 // PlayoutDevices
1302 // ----------------------------------------------------------------------------
1303
PlayoutDevices()1304 int16_t AudioDeviceModuleImpl::PlayoutDevices()
1305 {
1306 CHECK_INITIALIZED();
1307
1308 uint16_t nPlayoutDevices = _ptrAudioDevice->PlayoutDevices();
1309
1310 WEBRTC_TRACE(kTraceStateInfo, kTraceAudioDevice, _id, "output: #playout devices=%d", nPlayoutDevices);
1311 return ((int16_t)(nPlayoutDevices));
1312 }
1313
1314 // ----------------------------------------------------------------------------
1315 // SetPlayoutDevice I (II)
1316 // ----------------------------------------------------------------------------
1317
SetPlayoutDevice(uint16_t index)1318 int32_t AudioDeviceModuleImpl::SetPlayoutDevice(uint16_t index)
1319 {
1320 CHECK_INITIALIZED();
1321 return (_ptrAudioDevice->SetPlayoutDevice(index));
1322 }
1323
1324 // ----------------------------------------------------------------------------
1325 // SetPlayoutDevice II (II)
1326 // ----------------------------------------------------------------------------
1327
SetPlayoutDevice(WindowsDeviceType device)1328 int32_t AudioDeviceModuleImpl::SetPlayoutDevice(WindowsDeviceType device)
1329 {
1330 if (device == kDefaultDevice)
1331 {
1332 }
1333 else
1334 {
1335 }
1336 CHECK_INITIALIZED();
1337
1338 return (_ptrAudioDevice->SetPlayoutDevice(device));
1339 }
1340
1341 // ----------------------------------------------------------------------------
1342 // PlayoutDeviceName
1343 // ----------------------------------------------------------------------------
1344
PlayoutDeviceName(uint16_t index,char name[kAdmMaxDeviceNameSize],char guid[kAdmMaxGuidSize])1345 int32_t AudioDeviceModuleImpl::PlayoutDeviceName(
1346 uint16_t index,
1347 char name[kAdmMaxDeviceNameSize],
1348 char guid[kAdmMaxGuidSize])
1349 {
1350 CHECK_INITIALIZED();
1351
1352 if (name == NULL)
1353 {
1354 _lastError = kAdmErrArgument;
1355 return -1;
1356 }
1357
1358 if (_ptrAudioDevice->PlayoutDeviceName(index, name, guid) == -1)
1359 {
1360 return -1;
1361 }
1362
1363 if (name != NULL)
1364 {
1365 WEBRTC_TRACE(kTraceStateInfo, kTraceAudioDevice, _id, "output: name=%s", name);
1366 }
1367 if (guid != NULL)
1368 {
1369 WEBRTC_TRACE(kTraceStateInfo, kTraceAudioDevice, _id, "output: guid=%s", guid);
1370 }
1371
1372 return (0);
1373 }
1374
1375 // ----------------------------------------------------------------------------
1376 // RecordingDeviceName
1377 // ----------------------------------------------------------------------------
1378
RecordingDeviceName(uint16_t index,char name[kAdmMaxDeviceNameSize],char guid[kAdmMaxGuidSize])1379 int32_t AudioDeviceModuleImpl::RecordingDeviceName(
1380 uint16_t index,
1381 char name[kAdmMaxDeviceNameSize],
1382 char guid[kAdmMaxGuidSize])
1383 {
1384 CHECK_INITIALIZED();
1385
1386 if (name == NULL)
1387 {
1388 _lastError = kAdmErrArgument;
1389 return -1;
1390 }
1391
1392 if (_ptrAudioDevice->RecordingDeviceName(index, name, guid) == -1)
1393 {
1394 return -1;
1395 }
1396
1397 if (name != NULL)
1398 {
1399 WEBRTC_TRACE(kTraceStateInfo, kTraceAudioDevice, _id, "output: name=%s", name);
1400 }
1401 if (guid != NULL)
1402 {
1403 WEBRTC_TRACE(kTraceStateInfo, kTraceAudioDevice, _id, "output: guid=%s", guid);
1404 }
1405
1406 return (0);
1407 }
1408
1409 // ----------------------------------------------------------------------------
1410 // RecordingDevices
1411 // ----------------------------------------------------------------------------
1412
RecordingDevices()1413 int16_t AudioDeviceModuleImpl::RecordingDevices()
1414 {
1415 CHECK_INITIALIZED();
1416
1417 uint16_t nRecordingDevices = _ptrAudioDevice->RecordingDevices();
1418
1419 WEBRTC_TRACE(kTraceStateInfo, kTraceAudioDevice, _id,
1420 "output: #recording devices=%d", nRecordingDevices);
1421 return ((int16_t)nRecordingDevices);
1422 }
1423
1424 // ----------------------------------------------------------------------------
1425 // SetRecordingDevice I (II)
1426 // ----------------------------------------------------------------------------
1427
SetRecordingDevice(uint16_t index)1428 int32_t AudioDeviceModuleImpl::SetRecordingDevice(uint16_t index)
1429 {
1430 CHECK_INITIALIZED();
1431 return (_ptrAudioDevice->SetRecordingDevice(index));
1432 }
1433
1434 // ----------------------------------------------------------------------------
1435 // SetRecordingDevice II (II)
1436 // ----------------------------------------------------------------------------
1437
SetRecordingDevice(WindowsDeviceType device)1438 int32_t AudioDeviceModuleImpl::SetRecordingDevice(WindowsDeviceType device)
1439 {
1440 if (device == kDefaultDevice)
1441 {
1442 }
1443 else
1444 {
1445 }
1446 CHECK_INITIALIZED();
1447
1448 return (_ptrAudioDevice->SetRecordingDevice(device));
1449 }
1450
1451 // ----------------------------------------------------------------------------
1452 // InitPlayout
1453 // ----------------------------------------------------------------------------
1454
InitPlayout()1455 int32_t AudioDeviceModuleImpl::InitPlayout()
1456 {
1457 CHECK_INITIALIZED();
1458 _audioDeviceBuffer.InitPlayout();
1459 return (_ptrAudioDevice->InitPlayout());
1460 }
1461
1462 // ----------------------------------------------------------------------------
1463 // InitRecording
1464 // ----------------------------------------------------------------------------
1465
InitRecording()1466 int32_t AudioDeviceModuleImpl::InitRecording()
1467 {
1468 CHECK_INITIALIZED();
1469 _audioDeviceBuffer.InitRecording();
1470 return (_ptrAudioDevice->InitRecording());
1471 }
1472
1473 // ----------------------------------------------------------------------------
1474 // PlayoutIsInitialized
1475 // ----------------------------------------------------------------------------
1476
PlayoutIsInitialized() const1477 bool AudioDeviceModuleImpl::PlayoutIsInitialized() const
1478 {
1479 CHECK_INITIALIZED_BOOL();
1480 return (_ptrAudioDevice->PlayoutIsInitialized());
1481 }
1482
1483 // ----------------------------------------------------------------------------
1484 // RecordingIsInitialized
1485 // ----------------------------------------------------------------------------
1486
RecordingIsInitialized() const1487 bool AudioDeviceModuleImpl::RecordingIsInitialized() const
1488 {
1489 CHECK_INITIALIZED_BOOL();
1490 return (_ptrAudioDevice->RecordingIsInitialized());
1491 }
1492
1493 // ----------------------------------------------------------------------------
1494 // StartPlayout
1495 // ----------------------------------------------------------------------------
1496
StartPlayout()1497 int32_t AudioDeviceModuleImpl::StartPlayout()
1498 {
1499 CHECK_INITIALIZED();
1500 return (_ptrAudioDevice->StartPlayout());
1501 }
1502
1503 // ----------------------------------------------------------------------------
1504 // StopPlayout
1505 // ----------------------------------------------------------------------------
1506
StopPlayout()1507 int32_t AudioDeviceModuleImpl::StopPlayout()
1508 {
1509 CHECK_INITIALIZED();
1510 return (_ptrAudioDevice->StopPlayout());
1511 }
1512
1513 // ----------------------------------------------------------------------------
1514 // Playing
1515 // ----------------------------------------------------------------------------
1516
Playing() const1517 bool AudioDeviceModuleImpl::Playing() const
1518 {
1519 CHECK_INITIALIZED_BOOL();
1520 return (_ptrAudioDevice->Playing());
1521 }
1522
1523 // ----------------------------------------------------------------------------
1524 // StartRecording
1525 // ----------------------------------------------------------------------------
1526
StartRecording()1527 int32_t AudioDeviceModuleImpl::StartRecording()
1528 {
1529 CHECK_INITIALIZED();
1530 return (_ptrAudioDevice->StartRecording());
1531 }
1532 // ----------------------------------------------------------------------------
1533 // StopRecording
1534 // ----------------------------------------------------------------------------
1535
StopRecording()1536 int32_t AudioDeviceModuleImpl::StopRecording()
1537 {
1538 CHECK_INITIALIZED();
1539 return (_ptrAudioDevice->StopRecording());
1540 }
1541
1542 // ----------------------------------------------------------------------------
1543 // Recording
1544 // ----------------------------------------------------------------------------
1545
Recording() const1546 bool AudioDeviceModuleImpl::Recording() const
1547 {
1548 CHECK_INITIALIZED_BOOL();
1549 return (_ptrAudioDevice->Recording());
1550 }
1551
1552 // ----------------------------------------------------------------------------
1553 // RegisterEventObserver
1554 // ----------------------------------------------------------------------------
1555
RegisterEventObserver(AudioDeviceObserver * eventCallback)1556 int32_t AudioDeviceModuleImpl::RegisterEventObserver(AudioDeviceObserver* eventCallback)
1557 {
1558
1559 CriticalSectionScoped lock(&_critSectEventCb);
1560 _ptrCbAudioDeviceObserver = eventCallback;
1561
1562 return 0;
1563 }
1564
1565 // ----------------------------------------------------------------------------
1566 // RegisterAudioCallback
1567 // ----------------------------------------------------------------------------
1568
RegisterAudioCallback(AudioTransport * audioCallback)1569 int32_t AudioDeviceModuleImpl::RegisterAudioCallback(AudioTransport* audioCallback)
1570 {
1571
1572 CriticalSectionScoped lock(&_critSectAudioCb);
1573 _audioDeviceBuffer.RegisterAudioCallback(audioCallback);
1574
1575 return 0;
1576 }
1577
1578 // ----------------------------------------------------------------------------
1579 // StartRawInputFileRecording
1580 // ----------------------------------------------------------------------------
1581
StartRawInputFileRecording(const char pcmFileNameUTF8[kAdmMaxFileNameSize])1582 int32_t AudioDeviceModuleImpl::StartRawInputFileRecording(
1583 const char pcmFileNameUTF8[kAdmMaxFileNameSize])
1584 {
1585 CHECK_INITIALIZED();
1586
1587 if (NULL == pcmFileNameUTF8)
1588 {
1589 return -1;
1590 }
1591
1592 return (_audioDeviceBuffer.StartInputFileRecording(pcmFileNameUTF8));
1593 }
1594
1595 // ----------------------------------------------------------------------------
1596 // StopRawInputFileRecording
1597 // ----------------------------------------------------------------------------
1598
StopRawInputFileRecording()1599 int32_t AudioDeviceModuleImpl::StopRawInputFileRecording()
1600 {
1601 CHECK_INITIALIZED();
1602
1603 return (_audioDeviceBuffer.StopInputFileRecording());
1604 }
1605
1606 // ----------------------------------------------------------------------------
1607 // StartRawOutputFileRecording
1608 // ----------------------------------------------------------------------------
1609
StartRawOutputFileRecording(const char pcmFileNameUTF8[kAdmMaxFileNameSize])1610 int32_t AudioDeviceModuleImpl::StartRawOutputFileRecording(
1611 const char pcmFileNameUTF8[kAdmMaxFileNameSize])
1612 {
1613 CHECK_INITIALIZED();
1614
1615 if (NULL == pcmFileNameUTF8)
1616 {
1617 return -1;
1618 }
1619
1620 return (_audioDeviceBuffer.StartOutputFileRecording(pcmFileNameUTF8));
1621 }
1622
1623 // ----------------------------------------------------------------------------
1624 // StopRawOutputFileRecording
1625 // ----------------------------------------------------------------------------
1626
StopRawOutputFileRecording()1627 int32_t AudioDeviceModuleImpl::StopRawOutputFileRecording()
1628 {
1629 CHECK_INITIALIZED();
1630
1631 return (_audioDeviceBuffer.StopOutputFileRecording());
1632 }
1633
1634 // ----------------------------------------------------------------------------
1635 // SetPlayoutBuffer
1636 // ----------------------------------------------------------------------------
1637
SetPlayoutBuffer(const BufferType type,uint16_t sizeMS)1638 int32_t AudioDeviceModuleImpl::SetPlayoutBuffer(const BufferType type, uint16_t sizeMS)
1639 {
1640 CHECK_INITIALIZED();
1641
1642 if (_ptrAudioDevice->PlayoutIsInitialized())
1643 {
1644 WEBRTC_TRACE(kTraceError, kTraceAudioDevice, _id, "unable to modify the playout buffer while playing side is initialized");
1645 return -1;
1646 }
1647
1648 int32_t ret(0);
1649
1650 if (kFixedBufferSize == type)
1651 {
1652 if (sizeMS < kAdmMinPlayoutBufferSizeMs || sizeMS > kAdmMaxPlayoutBufferSizeMs)
1653 {
1654 WEBRTC_TRACE(kTraceError, kTraceAudioDevice, _id, "size parameter is out of range");
1655 return -1;
1656 }
1657 }
1658
1659 if ((ret = _ptrAudioDevice->SetPlayoutBuffer(type, sizeMS)) == -1)
1660 {
1661 WEBRTC_TRACE(kTraceError, kTraceAudioDevice, _id, "failed to set the playout buffer (error: %d)", LastError());
1662 }
1663
1664 return ret;
1665 }
1666
1667 // ----------------------------------------------------------------------------
1668 // PlayoutBuffer
1669 // ----------------------------------------------------------------------------
1670
PlayoutBuffer(BufferType * type,uint16_t * sizeMS) const1671 int32_t AudioDeviceModuleImpl::PlayoutBuffer(BufferType* type, uint16_t* sizeMS) const
1672 {
1673 CHECK_INITIALIZED();
1674
1675 BufferType bufType;
1676 uint16_t size(0);
1677
1678 if (_ptrAudioDevice->PlayoutBuffer(bufType, size) == -1)
1679 {
1680 WEBRTC_TRACE(kTraceError, kTraceAudioDevice, _id, "failed to retrieve the buffer type and size");
1681 return -1;
1682 }
1683
1684 *type = bufType;
1685 *sizeMS = size;
1686
1687 WEBRTC_TRACE(kTraceStateInfo, kTraceAudioDevice, _id, "output: type=%u, sizeMS=%u", *type, *sizeMS);
1688 return (0);
1689 }
1690
1691 // ----------------------------------------------------------------------------
1692 // PlayoutDelay
1693 // ----------------------------------------------------------------------------
1694
PlayoutDelay(uint16_t * delayMS) const1695 int32_t AudioDeviceModuleImpl::PlayoutDelay(uint16_t* delayMS) const
1696 {
1697 WEBRTC_TRACE(kTraceStream, kTraceAudioDevice, _id, "%s", __FUNCTION__);
1698 CHECK_INITIALIZED();
1699
1700 uint16_t delay(0);
1701
1702 if (_ptrAudioDevice->PlayoutDelay(delay) == -1)
1703 {
1704 WEBRTC_TRACE(kTraceError, kTraceAudioDevice, _id, "failed to retrieve the playout delay");
1705 return -1;
1706 }
1707
1708 *delayMS = delay;
1709
1710 WEBRTC_TRACE(kTraceStream, kTraceAudioDevice, _id, "output: delayMS=%u", *delayMS);
1711 return (0);
1712 }
1713
1714 // ----------------------------------------------------------------------------
1715 // RecordingDelay
1716 // ----------------------------------------------------------------------------
1717
RecordingDelay(uint16_t * delayMS) const1718 int32_t AudioDeviceModuleImpl::RecordingDelay(uint16_t* delayMS) const
1719 {
1720 WEBRTC_TRACE(kTraceStream, kTraceAudioDevice, _id, "%s", __FUNCTION__);
1721 CHECK_INITIALIZED();
1722
1723 uint16_t delay(0);
1724
1725 if (_ptrAudioDevice->RecordingDelay(delay) == -1)
1726 {
1727 WEBRTC_TRACE(kTraceError, kTraceAudioDevice, _id, "failed to retrieve the recording delay");
1728 return -1;
1729 }
1730
1731 *delayMS = delay;
1732
1733 WEBRTC_TRACE(kTraceStream, kTraceAudioDevice, _id, "output: delayMS=%u", *delayMS);
1734 return (0);
1735 }
1736
1737 // ----------------------------------------------------------------------------
1738 // CPULoad
1739 // ----------------------------------------------------------------------------
1740
CPULoad(uint16_t * load) const1741 int32_t AudioDeviceModuleImpl::CPULoad(uint16_t* load) const
1742 {
1743 CHECK_INITIALIZED();
1744
1745 uint16_t cpuLoad(0);
1746
1747 if (_ptrAudioDevice->CPULoad(cpuLoad) == -1)
1748 {
1749 WEBRTC_TRACE(kTraceError, kTraceAudioDevice, _id, "failed to retrieve the CPU load");
1750 return -1;
1751 }
1752
1753 *load = cpuLoad;
1754
1755 WEBRTC_TRACE(kTraceStateInfo, kTraceAudioDevice, _id, "output: load=%u", *load);
1756 return (0);
1757 }
1758
1759 // ----------------------------------------------------------------------------
1760 // SetRecordingSampleRate
1761 // ----------------------------------------------------------------------------
1762
SetRecordingSampleRate(const uint32_t samplesPerSec)1763 int32_t AudioDeviceModuleImpl::SetRecordingSampleRate(const uint32_t samplesPerSec)
1764 {
1765 CHECK_INITIALIZED();
1766
1767 if (_ptrAudioDevice->SetRecordingSampleRate(samplesPerSec) != 0)
1768 {
1769 return -1;
1770 }
1771
1772 return (0);
1773 }
1774
1775 // ----------------------------------------------------------------------------
1776 // RecordingSampleRate
1777 // ----------------------------------------------------------------------------
1778
RecordingSampleRate(uint32_t * samplesPerSec) const1779 int32_t AudioDeviceModuleImpl::RecordingSampleRate(uint32_t* samplesPerSec) const
1780 {
1781 CHECK_INITIALIZED();
1782
1783 int32_t sampleRate = _audioDeviceBuffer.RecordingSampleRate();
1784
1785 if (sampleRate == -1)
1786 {
1787 WEBRTC_TRACE(kTraceError, kTraceAudioDevice, _id, "failed to retrieve the sample rate");
1788 return -1;
1789 }
1790
1791 *samplesPerSec = sampleRate;
1792
1793 WEBRTC_TRACE(kTraceStateInfo, kTraceAudioDevice, _id, "output: samplesPerSec=%u", *samplesPerSec);
1794 return (0);
1795 }
1796
1797 // ----------------------------------------------------------------------------
1798 // SetPlayoutSampleRate
1799 // ----------------------------------------------------------------------------
1800
SetPlayoutSampleRate(const uint32_t samplesPerSec)1801 int32_t AudioDeviceModuleImpl::SetPlayoutSampleRate(const uint32_t samplesPerSec)
1802 {
1803 CHECK_INITIALIZED();
1804
1805 if (_ptrAudioDevice->SetPlayoutSampleRate(samplesPerSec) != 0)
1806 {
1807 return -1;
1808 }
1809
1810 return (0);
1811 }
1812
1813 // ----------------------------------------------------------------------------
1814 // PlayoutSampleRate
1815 // ----------------------------------------------------------------------------
1816
PlayoutSampleRate(uint32_t * samplesPerSec) const1817 int32_t AudioDeviceModuleImpl::PlayoutSampleRate(uint32_t* samplesPerSec) const
1818 {
1819 CHECK_INITIALIZED();
1820
1821 int32_t sampleRate = _audioDeviceBuffer.PlayoutSampleRate();
1822
1823 if (sampleRate == -1)
1824 {
1825 WEBRTC_TRACE(kTraceError, kTraceAudioDevice, _id, "failed to retrieve the sample rate");
1826 return -1;
1827 }
1828
1829 *samplesPerSec = sampleRate;
1830
1831 WEBRTC_TRACE(kTraceStateInfo, kTraceAudioDevice, _id, "output: samplesPerSec=%u", *samplesPerSec);
1832 return (0);
1833 }
1834
1835 // ----------------------------------------------------------------------------
1836 // ResetAudioDevice
1837 // ----------------------------------------------------------------------------
1838
ResetAudioDevice()1839 int32_t AudioDeviceModuleImpl::ResetAudioDevice()
1840 {
1841 CHECK_INITIALIZED();
1842
1843
1844 if (_ptrAudioDevice->ResetAudioDevice() == -1)
1845 {
1846 return -1;
1847 }
1848
1849 return (0);
1850 }
1851
1852 // ----------------------------------------------------------------------------
1853 // SetLoudspeakerStatus
1854 // ----------------------------------------------------------------------------
1855
SetLoudspeakerStatus(bool enable)1856 int32_t AudioDeviceModuleImpl::SetLoudspeakerStatus(bool enable)
1857 {
1858 CHECK_INITIALIZED();
1859
1860 if (_ptrAudioDevice->SetLoudspeakerStatus(enable) != 0)
1861 {
1862 return -1;
1863 }
1864
1865 return 0;
1866 }
1867
1868 // ----------------------------------------------------------------------------
1869 // GetLoudspeakerStatus
1870 // ----------------------------------------------------------------------------
1871
GetLoudspeakerStatus(bool * enabled) const1872 int32_t AudioDeviceModuleImpl::GetLoudspeakerStatus(bool* enabled) const {
1873 CHECK_INITIALIZED();
1874 if (_ptrAudioDevice->GetLoudspeakerStatus(*enabled) != 0) {
1875 return -1;
1876 }
1877 return 0;
1878 }
1879
BuiltInAECIsEnabled() const1880 bool AudioDeviceModuleImpl::BuiltInAECIsEnabled() const {
1881 CHECK_INITIALIZED_BOOL();
1882 return _ptrAudioDevice->BuiltInAECIsEnabled();
1883 }
1884
BuiltInAECIsAvailable() const1885 bool AudioDeviceModuleImpl::BuiltInAECIsAvailable() const {
1886 CHECK_INITIALIZED_BOOL();
1887 return _ptrAudioDevice->BuiltInAECIsAvailable();
1888 }
1889
EnableBuiltInAEC(bool enable)1890 int32_t AudioDeviceModuleImpl::EnableBuiltInAEC(bool enable) {
1891 CHECK_INITIALIZED();
1892 return _ptrAudioDevice->EnableBuiltInAEC(enable);
1893 }
1894
BuiltInAGCIsAvailable() const1895 bool AudioDeviceModuleImpl::BuiltInAGCIsAvailable() const {
1896 CHECK_INITIALIZED_BOOL();
1897 return _ptrAudioDevice->BuiltInAGCIsAvailable();
1898 }
1899
EnableBuiltInAGC(bool enable)1900 int32_t AudioDeviceModuleImpl::EnableBuiltInAGC(bool enable) {
1901 CHECK_INITIALIZED();
1902 return _ptrAudioDevice->EnableBuiltInAGC(enable);
1903 }
1904
BuiltInNSIsAvailable() const1905 bool AudioDeviceModuleImpl::BuiltInNSIsAvailable() const {
1906 CHECK_INITIALIZED_BOOL();
1907 return _ptrAudioDevice->BuiltInNSIsAvailable();
1908 }
1909
EnableBuiltInNS(bool enable)1910 int32_t AudioDeviceModuleImpl::EnableBuiltInNS(bool enable) {
1911 CHECK_INITIALIZED();
1912 return _ptrAudioDevice->EnableBuiltInNS(enable);
1913 }
1914
GetPlayoutAudioParameters(AudioParameters * params) const1915 int AudioDeviceModuleImpl::GetPlayoutAudioParameters(
1916 AudioParameters* params) const {
1917 return _ptrAudioDevice->GetPlayoutAudioParameters(params);
1918 }
1919
GetRecordAudioParameters(AudioParameters * params) const1920 int AudioDeviceModuleImpl::GetRecordAudioParameters(
1921 AudioParameters* params) const {
1922 return _ptrAudioDevice->GetRecordAudioParameters(params);
1923 }
1924
1925 // ============================================================================
1926 // Private Methods
1927 // ============================================================================
1928
1929 // ----------------------------------------------------------------------------
1930 // Platform
1931 // ----------------------------------------------------------------------------
1932
Platform() const1933 AudioDeviceModuleImpl::PlatformType AudioDeviceModuleImpl::Platform() const
1934 {
1935 return _platformType;
1936 }
1937
1938 // ----------------------------------------------------------------------------
1939 // PlatformAudioLayer
1940 // ----------------------------------------------------------------------------
1941
PlatformAudioLayer() const1942 AudioDeviceModule::AudioLayer AudioDeviceModuleImpl::PlatformAudioLayer() const
1943 {
1944 return _platformAudioLayer;
1945 }
1946
1947 } // namespace webrtc
1948