blob: c7312bf4b0a062bbd0152454df4ce9eb55236f64 [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
leozwang@webrtc.org28f39132012-03-01 18:01:48 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
niklase@google.com470e71d2011-07-07 08:21:25 +00003 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11#ifndef WEBRTC_AUDIO_DEVICE_AUDIO_DEVICE_IMPL_H
12#define WEBRTC_AUDIO_DEVICE_AUDIO_DEVICE_IMPL_H
13
Tommi931e6582015-05-20 09:44:38 +020014#if defined(WEBRTC_INCLUDE_INTERNAL_AUDIO_DEVICE)
15
kwibergf01633e2016-02-24 05:00:36 -080016#include <memory>
17
henrikab2619892015-05-18 16:49:16 +020018#include "webrtc/base/checks.h"
pbos@webrtc.org811269d2013-07-11 13:24:38 +000019#include "webrtc/modules/audio_device/audio_device_buffer.h"
20#include "webrtc/modules/audio_device/include/audio_device.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000021
henrikaba35d052015-07-14 17:04:08 +020022namespace webrtc {
niklase@google.com470e71d2011-07-07 08:21:25 +000023
24class AudioDeviceGeneric;
henrikab2619892015-05-18 16:49:16 +020025class AudioManager;
niklase@google.com470e71d2011-07-07 08:21:25 +000026class CriticalSectionWrapper;
27
henrikaba35d052015-07-14 17:04:08 +020028class AudioDeviceModuleImpl : public AudioDeviceModule {
29 public:
30 enum PlatformType {
31 kPlatformNotSupported = 0,
32 kPlatformWin32 = 1,
33 kPlatformWinCe = 2,
34 kPlatformLinux = 3,
35 kPlatformMac = 4,
36 kPlatformAndroid = 5,
37 kPlatformIOS = 6
38 };
niklase@google.com470e71d2011-07-07 08:21:25 +000039
henrikaba35d052015-07-14 17:04:08 +020040 int32_t CheckPlatform();
41 int32_t CreatePlatformSpecificObjects();
42 int32_t AttachAudioBuffer();
niklase@google.com470e71d2011-07-07 08:21:25 +000043
henrikaba35d052015-07-14 17:04:08 +020044 AudioDeviceModuleImpl(const int32_t id, const AudioLayer audioLayer);
45 virtual ~AudioDeviceModuleImpl();
niklase@google.com470e71d2011-07-07 08:21:25 +000046
henrikaba35d052015-07-14 17:04:08 +020047 int64_t TimeUntilNextProcess() override;
48 int32_t Process() override;
niklase@google.com470e71d2011-07-07 08:21:25 +000049
henrikaba35d052015-07-14 17:04:08 +020050 // Factory methods (resource allocation/deallocation)
51 static AudioDeviceModule* Create(
52 const int32_t id,
53 const AudioLayer audioLayer = kPlatformDefaultAudio);
niklase@google.com470e71d2011-07-07 08:21:25 +000054
henrikaba35d052015-07-14 17:04:08 +020055 // Retrieve the currently utilized audio layer
56 int32_t ActiveAudioLayer(AudioLayer* audioLayer) const override;
niklase@google.com470e71d2011-07-07 08:21:25 +000057
henrikaba35d052015-07-14 17:04:08 +020058 // Error handling
59 ErrorCode LastError() const override;
60 int32_t RegisterEventObserver(AudioDeviceObserver* eventCallback) override;
niklase@google.com470e71d2011-07-07 08:21:25 +000061
henrikaba35d052015-07-14 17:04:08 +020062 // Full-duplex transportation of PCM audio
63 int32_t RegisterAudioCallback(AudioTransport* audioCallback) override;
niklase@google.com470e71d2011-07-07 08:21:25 +000064
henrikaba35d052015-07-14 17:04:08 +020065 // Main initializaton and termination
66 int32_t Init() override;
67 int32_t Terminate() override;
68 bool Initialized() const override;
niklase@google.com470e71d2011-07-07 08:21:25 +000069
henrikaba35d052015-07-14 17:04:08 +020070 // Device enumeration
71 int16_t PlayoutDevices() override;
72 int16_t RecordingDevices() override;
73 int32_t PlayoutDeviceName(uint16_t index,
74 char name[kAdmMaxDeviceNameSize],
75 char guid[kAdmMaxGuidSize]) override;
76 int32_t RecordingDeviceName(uint16_t index,
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000077 char name[kAdmMaxDeviceNameSize],
78 char guid[kAdmMaxGuidSize]) override;
niklase@google.com470e71d2011-07-07 08:21:25 +000079
henrikaba35d052015-07-14 17:04:08 +020080 // Device selection
81 int32_t SetPlayoutDevice(uint16_t index) override;
82 int32_t SetPlayoutDevice(WindowsDeviceType device) override;
83 int32_t SetRecordingDevice(uint16_t index) override;
84 int32_t SetRecordingDevice(WindowsDeviceType device) override;
niklase@google.com470e71d2011-07-07 08:21:25 +000085
henrikaba35d052015-07-14 17:04:08 +020086 // Audio transport initialization
87 int32_t PlayoutIsAvailable(bool* available) override;
88 int32_t InitPlayout() override;
89 bool PlayoutIsInitialized() const override;
90 int32_t RecordingIsAvailable(bool* available) override;
91 int32_t InitRecording() override;
92 bool RecordingIsInitialized() const override;
niklase@google.com470e71d2011-07-07 08:21:25 +000093
henrikaba35d052015-07-14 17:04:08 +020094 // Audio transport control
95 int32_t StartPlayout() override;
96 int32_t StopPlayout() override;
97 bool Playing() const override;
98 int32_t StartRecording() override;
99 int32_t StopRecording() override;
100 bool Recording() const override;
niklase@google.com470e71d2011-07-07 08:21:25 +0000101
henrikaba35d052015-07-14 17:04:08 +0200102 // Microphone Automatic Gain Control (AGC)
103 int32_t SetAGC(bool enable) override;
104 bool AGC() const override;
niklase@google.com470e71d2011-07-07 08:21:25 +0000105
henrikaba35d052015-07-14 17:04:08 +0200106 // Volume control based on the Windows Wave API (Windows only)
107 int32_t SetWaveOutVolume(uint16_t volumeLeft, uint16_t volumeRight) override;
108 int32_t WaveOutVolume(uint16_t* volumeLeft,
109 uint16_t* volumeRight) const override;
niklase@google.com470e71d2011-07-07 08:21:25 +0000110
henrikaba35d052015-07-14 17:04:08 +0200111 // Audio mixer initialization
112 int32_t InitSpeaker() override;
113 bool SpeakerIsInitialized() const override;
114 int32_t InitMicrophone() override;
115 bool MicrophoneIsInitialized() const override;
niklase@google.com470e71d2011-07-07 08:21:25 +0000116
henrikaba35d052015-07-14 17:04:08 +0200117 // Speaker volume controls
118 int32_t SpeakerVolumeIsAvailable(bool* available) override;
119 int32_t SetSpeakerVolume(uint32_t volume) override;
120 int32_t SpeakerVolume(uint32_t* volume) const override;
121 int32_t MaxSpeakerVolume(uint32_t* maxVolume) const override;
122 int32_t MinSpeakerVolume(uint32_t* minVolume) const override;
123 int32_t SpeakerVolumeStepSize(uint16_t* stepSize) const override;
niklase@google.com470e71d2011-07-07 08:21:25 +0000124
henrikaba35d052015-07-14 17:04:08 +0200125 // Microphone volume controls
126 int32_t MicrophoneVolumeIsAvailable(bool* available) override;
127 int32_t SetMicrophoneVolume(uint32_t volume) override;
128 int32_t MicrophoneVolume(uint32_t* volume) const override;
129 int32_t MaxMicrophoneVolume(uint32_t* maxVolume) const override;
130 int32_t MinMicrophoneVolume(uint32_t* minVolume) const override;
131 int32_t MicrophoneVolumeStepSize(uint16_t* stepSize) const override;
niklase@google.com470e71d2011-07-07 08:21:25 +0000132
henrikaba35d052015-07-14 17:04:08 +0200133 // Speaker mute control
134 int32_t SpeakerMuteIsAvailable(bool* available) override;
135 int32_t SetSpeakerMute(bool enable) override;
136 int32_t SpeakerMute(bool* enabled) const override;
niklase@google.com470e71d2011-07-07 08:21:25 +0000137
henrikaba35d052015-07-14 17:04:08 +0200138 // Microphone mute control
139 int32_t MicrophoneMuteIsAvailable(bool* available) override;
140 int32_t SetMicrophoneMute(bool enable) override;
141 int32_t MicrophoneMute(bool* enabled) const override;
niklase@google.com470e71d2011-07-07 08:21:25 +0000142
henrikaba35d052015-07-14 17:04:08 +0200143 // Microphone boost control
144 int32_t MicrophoneBoostIsAvailable(bool* available) override;
145 int32_t SetMicrophoneBoost(bool enable) override;
146 int32_t MicrophoneBoost(bool* enabled) const override;
niklase@google.com470e71d2011-07-07 08:21:25 +0000147
henrikaba35d052015-07-14 17:04:08 +0200148 // Stereo support
149 int32_t StereoPlayoutIsAvailable(bool* available) const override;
150 int32_t SetStereoPlayout(bool enable) override;
151 int32_t StereoPlayout(bool* enabled) const override;
152 int32_t StereoRecordingIsAvailable(bool* available) const override;
153 int32_t SetStereoRecording(bool enable) override;
154 int32_t StereoRecording(bool* enabled) const override;
155 int32_t SetRecordingChannel(const ChannelType channel) override;
156 int32_t RecordingChannel(ChannelType* channel) const override;
niklase@google.com470e71d2011-07-07 08:21:25 +0000157
henrikaba35d052015-07-14 17:04:08 +0200158 // Delay information and control
159 int32_t SetPlayoutBuffer(const BufferType type, uint16_t sizeMS = 0) override;
160 int32_t PlayoutBuffer(BufferType* type, uint16_t* sizeMS) const override;
161 int32_t PlayoutDelay(uint16_t* delayMS) const override;
162 int32_t RecordingDelay(uint16_t* delayMS) const override;
niklase@google.com470e71d2011-07-07 08:21:25 +0000163
henrikaba35d052015-07-14 17:04:08 +0200164 // CPU load
165 int32_t CPULoad(uint16_t* load) const override;
niklase@google.com470e71d2011-07-07 08:21:25 +0000166
henrikaba35d052015-07-14 17:04:08 +0200167 // Recording of raw PCM data
168 int32_t StartRawOutputFileRecording(
169 const char pcmFileNameUTF8[kAdmMaxFileNameSize]) override;
170 int32_t StopRawOutputFileRecording() override;
171 int32_t StartRawInputFileRecording(
172 const char pcmFileNameUTF8[kAdmMaxFileNameSize]) override;
173 int32_t StopRawInputFileRecording() override;
niklase@google.com470e71d2011-07-07 08:21:25 +0000174
henrikaba35d052015-07-14 17:04:08 +0200175 // Native sample rate controls (samples/sec)
176 int32_t SetRecordingSampleRate(const uint32_t samplesPerSec) override;
177 int32_t RecordingSampleRate(uint32_t* samplesPerSec) const override;
178 int32_t SetPlayoutSampleRate(const uint32_t samplesPerSec) override;
179 int32_t PlayoutSampleRate(uint32_t* samplesPerSec) const override;
niklase@google.com470e71d2011-07-07 08:21:25 +0000180
henrikaba35d052015-07-14 17:04:08 +0200181 // Mobile device specific functions
182 int32_t ResetAudioDevice() override;
183 int32_t SetLoudspeakerStatus(bool enable) override;
184 int32_t GetLoudspeakerStatus(bool* enabled) const override;
niklase@google.com470e71d2011-07-07 08:21:25 +0000185
henrikaba35d052015-07-14 17:04:08 +0200186 bool BuiltInAECIsEnabled() const override;
henrikac14f5ff2015-09-23 14:08:33 +0200187 bool BuiltInAECIsAvailable() const override;
188 int32_t EnableBuiltInAEC(bool enable) override;
189 bool BuiltInAGCIsAvailable() const override;
190 int32_t EnableBuiltInAGC(bool enable) override;
191 bool BuiltInNSIsAvailable() const override;
192 int32_t EnableBuiltInNS(bool enable) override;
andrew@webrtc.orga3c6d612011-09-13 17:17:49 +0000193
henrikaba35d052015-07-14 17:04:08 +0200194 int GetPlayoutAudioParameters(AudioParameters* params) const override;
195 int GetRecordAudioParameters(AudioParameters* params) const override;
196
197 int32_t Id() { return _id; }
Tommi931e6582015-05-20 09:44:38 +0200198#if defined(WEBRTC_ANDROID)
henrikaba35d052015-07-14 17:04:08 +0200199 // Only use this acccessor for test purposes on Android.
200 AudioManager* GetAndroidAudioManagerForTest() {
201 return _audioManagerAndroid.get();
202 }
henrika24e56e32015-05-19 11:48:51 +0200203#endif
henrikaba35d052015-07-14 17:04:08 +0200204 AudioDeviceBuffer* GetAudioDeviceBuffer() { return &_audioDeviceBuffer; }
henrika@webrtc.org474d1eb2015-03-09 12:39:53 +0000205
henrikaba35d052015-07-14 17:04:08 +0200206 private:
207 PlatformType Platform() const;
208 AudioLayer PlatformAudioLayer() const;
niklase@google.com470e71d2011-07-07 08:21:25 +0000209
henrikaba35d052015-07-14 17:04:08 +0200210 CriticalSectionWrapper& _critSect;
211 CriticalSectionWrapper& _critSectEventCb;
212 CriticalSectionWrapper& _critSectAudioCb;
niklase@google.com470e71d2011-07-07 08:21:25 +0000213
henrikaba35d052015-07-14 17:04:08 +0200214 AudioDeviceObserver* _ptrCbAudioDeviceObserver;
niklase@google.com470e71d2011-07-07 08:21:25 +0000215
henrikaba35d052015-07-14 17:04:08 +0200216 AudioDeviceGeneric* _ptrAudioDevice;
niklase@google.com470e71d2011-07-07 08:21:25 +0000217
henrikaba35d052015-07-14 17:04:08 +0200218 AudioDeviceBuffer _audioDeviceBuffer;
Tommi931e6582015-05-20 09:44:38 +0200219#if defined(WEBRTC_ANDROID)
kwibergf01633e2016-02-24 05:00:36 -0800220 std::unique_ptr<AudioManager> _audioManagerAndroid;
henrikab2619892015-05-18 16:49:16 +0200221#endif
henrikaba35d052015-07-14 17:04:08 +0200222 int32_t _id;
223 AudioLayer _platformAudioLayer;
224 int64_t _lastProcessTime;
225 PlatformType _platformType;
226 bool _initialized;
227 mutable ErrorCode _lastError;
niklase@google.com470e71d2011-07-07 08:21:25 +0000228};
229
230} // namespace webrtc
231
Tommi931e6582015-05-20 09:44:38 +0200232#endif // defined(WEBRTC_INCLUDE_INTERNAL_AUDIO_DEVICE)
233
niklase@google.com470e71d2011-07-07 08:21:25 +0000234#endif // WEBRTC_MODULES_INTERFACE_AUDIO_DEVICE_IMPL_H_