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