blob: 24b5982cf222605d5864142955657044fc8549f6 [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
henrikab2619892015-05-18 16:49:16 +020016#include "webrtc/base/checks.h"
17#include "webrtc/base/scoped_ptr.h"
pbos@webrtc.org811269d2013-07-11 13:24:38 +000018#include "webrtc/modules/audio_device/audio_device_buffer.h"
19#include "webrtc/modules/audio_device/include/audio_device.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000020
henrikaba35d052015-07-14 17:04:08 +020021namespace webrtc {
niklase@google.com470e71d2011-07-07 08:21:25 +000022
23class AudioDeviceGeneric;
henrikab2619892015-05-18 16:49:16 +020024class AudioManager;
niklase@google.com470e71d2011-07-07 08:21:25 +000025class CriticalSectionWrapper;
26
henrikaba35d052015-07-14 17:04:08 +020027class AudioDeviceModuleImpl : public AudioDeviceModule {
28 public:
29 enum PlatformType {
30 kPlatformNotSupported = 0,
31 kPlatformWin32 = 1,
32 kPlatformWinCe = 2,
33 kPlatformLinux = 3,
34 kPlatformMac = 4,
35 kPlatformAndroid = 5,
36 kPlatformIOS = 6
37 };
niklase@google.com470e71d2011-07-07 08:21:25 +000038
henrikaba35d052015-07-14 17:04:08 +020039 int32_t CheckPlatform();
40 int32_t CreatePlatformSpecificObjects();
41 int32_t AttachAudioBuffer();
niklase@google.com470e71d2011-07-07 08:21:25 +000042
henrikaba35d052015-07-14 17:04:08 +020043 AudioDeviceModuleImpl(const int32_t id, const AudioLayer audioLayer);
44 virtual ~AudioDeviceModuleImpl();
niklase@google.com470e71d2011-07-07 08:21:25 +000045
henrikaba35d052015-07-14 17:04:08 +020046 int64_t TimeUntilNextProcess() override;
47 int32_t Process() override;
niklase@google.com470e71d2011-07-07 08:21:25 +000048
henrikaba35d052015-07-14 17:04:08 +020049 // Factory methods (resource allocation/deallocation)
50 static AudioDeviceModule* Create(
51 const int32_t id,
52 const AudioLayer audioLayer = kPlatformDefaultAudio);
niklase@google.com470e71d2011-07-07 08:21:25 +000053
henrikaba35d052015-07-14 17:04:08 +020054 // Retrieve the currently utilized audio layer
55 int32_t ActiveAudioLayer(AudioLayer* audioLayer) const override;
niklase@google.com470e71d2011-07-07 08:21:25 +000056
henrikaba35d052015-07-14 17:04:08 +020057 // Error handling
58 ErrorCode LastError() const override;
59 int32_t RegisterEventObserver(AudioDeviceObserver* eventCallback) override;
niklase@google.com470e71d2011-07-07 08:21:25 +000060
henrikaba35d052015-07-14 17:04:08 +020061 // Full-duplex transportation of PCM audio
62 int32_t RegisterAudioCallback(AudioTransport* audioCallback) override;
niklase@google.com470e71d2011-07-07 08:21:25 +000063
henrikaba35d052015-07-14 17:04:08 +020064 // Main initializaton and termination
65 int32_t Init() override;
66 int32_t Terminate() override;
67 bool Initialized() const override;
niklase@google.com470e71d2011-07-07 08:21:25 +000068
henrikaba35d052015-07-14 17:04:08 +020069 // Device enumeration
70 int16_t PlayoutDevices() override;
71 int16_t RecordingDevices() override;
72 int32_t PlayoutDeviceName(uint16_t index,
73 char name[kAdmMaxDeviceNameSize],
74 char guid[kAdmMaxGuidSize]) override;
75 int32_t RecordingDeviceName(uint16_t index,
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000076 char name[kAdmMaxDeviceNameSize],
77 char guid[kAdmMaxGuidSize]) override;
niklase@google.com470e71d2011-07-07 08:21:25 +000078
henrikaba35d052015-07-14 17:04:08 +020079 // Device selection
80 int32_t SetPlayoutDevice(uint16_t index) override;
81 int32_t SetPlayoutDevice(WindowsDeviceType device) override;
82 int32_t SetRecordingDevice(uint16_t index) override;
83 int32_t SetRecordingDevice(WindowsDeviceType device) override;
niklase@google.com470e71d2011-07-07 08:21:25 +000084
henrikaba35d052015-07-14 17:04:08 +020085 // Audio transport initialization
86 int32_t PlayoutIsAvailable(bool* available) override;
87 int32_t InitPlayout() override;
88 bool PlayoutIsInitialized() const override;
89 int32_t RecordingIsAvailable(bool* available) override;
90 int32_t InitRecording() override;
91 bool RecordingIsInitialized() const override;
niklase@google.com470e71d2011-07-07 08:21:25 +000092
henrikaba35d052015-07-14 17:04:08 +020093 // Audio transport control
94 int32_t StartPlayout() override;
95 int32_t StopPlayout() override;
96 bool Playing() const override;
97 int32_t StartRecording() override;
98 int32_t StopRecording() override;
99 bool Recording() const override;
niklase@google.com470e71d2011-07-07 08:21:25 +0000100
henrikaba35d052015-07-14 17:04:08 +0200101 // Microphone Automatic Gain Control (AGC)
102 int32_t SetAGC(bool enable) override;
103 bool AGC() const override;
niklase@google.com470e71d2011-07-07 08:21:25 +0000104
henrikaba35d052015-07-14 17:04:08 +0200105 // Volume control based on the Windows Wave API (Windows only)
106 int32_t SetWaveOutVolume(uint16_t volumeLeft, uint16_t volumeRight) override;
107 int32_t WaveOutVolume(uint16_t* volumeLeft,
108 uint16_t* volumeRight) const override;
niklase@google.com470e71d2011-07-07 08:21:25 +0000109
henrikaba35d052015-07-14 17:04:08 +0200110 // Audio mixer initialization
111 int32_t InitSpeaker() override;
112 bool SpeakerIsInitialized() const override;
113 int32_t InitMicrophone() override;
114 bool MicrophoneIsInitialized() const override;
niklase@google.com470e71d2011-07-07 08:21:25 +0000115
henrikaba35d052015-07-14 17:04:08 +0200116 // Speaker volume controls
117 int32_t SpeakerVolumeIsAvailable(bool* available) override;
118 int32_t SetSpeakerVolume(uint32_t volume) override;
119 int32_t SpeakerVolume(uint32_t* volume) const override;
120 int32_t MaxSpeakerVolume(uint32_t* maxVolume) const override;
121 int32_t MinSpeakerVolume(uint32_t* minVolume) const override;
122 int32_t SpeakerVolumeStepSize(uint16_t* stepSize) const override;
niklase@google.com470e71d2011-07-07 08:21:25 +0000123
henrikaba35d052015-07-14 17:04:08 +0200124 // Microphone volume controls
125 int32_t MicrophoneVolumeIsAvailable(bool* available) override;
126 int32_t SetMicrophoneVolume(uint32_t volume) override;
127 int32_t MicrophoneVolume(uint32_t* volume) const override;
128 int32_t MaxMicrophoneVolume(uint32_t* maxVolume) const override;
129 int32_t MinMicrophoneVolume(uint32_t* minVolume) const override;
130 int32_t MicrophoneVolumeStepSize(uint16_t* stepSize) const override;
niklase@google.com470e71d2011-07-07 08:21:25 +0000131
henrikaba35d052015-07-14 17:04:08 +0200132 // Speaker mute control
133 int32_t SpeakerMuteIsAvailable(bool* available) override;
134 int32_t SetSpeakerMute(bool enable) override;
135 int32_t SpeakerMute(bool* enabled) const override;
niklase@google.com470e71d2011-07-07 08:21:25 +0000136
henrikaba35d052015-07-14 17:04:08 +0200137 // Microphone mute control
138 int32_t MicrophoneMuteIsAvailable(bool* available) override;
139 int32_t SetMicrophoneMute(bool enable) override;
140 int32_t MicrophoneMute(bool* enabled) const override;
niklase@google.com470e71d2011-07-07 08:21:25 +0000141
henrikaba35d052015-07-14 17:04:08 +0200142 // Microphone boost control
143 int32_t MicrophoneBoostIsAvailable(bool* available) override;
144 int32_t SetMicrophoneBoost(bool enable) override;
145 int32_t MicrophoneBoost(bool* enabled) const override;
niklase@google.com470e71d2011-07-07 08:21:25 +0000146
henrikaba35d052015-07-14 17:04:08 +0200147 // Stereo support
148 int32_t StereoPlayoutIsAvailable(bool* available) const override;
149 int32_t SetStereoPlayout(bool enable) override;
150 int32_t StereoPlayout(bool* enabled) const override;
151 int32_t StereoRecordingIsAvailable(bool* available) const override;
152 int32_t SetStereoRecording(bool enable) override;
153 int32_t StereoRecording(bool* enabled) const override;
154 int32_t SetRecordingChannel(const ChannelType channel) override;
155 int32_t RecordingChannel(ChannelType* channel) const override;
niklase@google.com470e71d2011-07-07 08:21:25 +0000156
henrikaba35d052015-07-14 17:04:08 +0200157 // Delay information and control
158 int32_t SetPlayoutBuffer(const BufferType type, uint16_t sizeMS = 0) override;
159 int32_t PlayoutBuffer(BufferType* type, uint16_t* sizeMS) const override;
160 int32_t PlayoutDelay(uint16_t* delayMS) const override;
161 int32_t RecordingDelay(uint16_t* delayMS) const override;
niklase@google.com470e71d2011-07-07 08:21:25 +0000162
henrikaba35d052015-07-14 17:04:08 +0200163 // CPU load
164 int32_t CPULoad(uint16_t* load) const override;
niklase@google.com470e71d2011-07-07 08:21:25 +0000165
henrikaba35d052015-07-14 17:04:08 +0200166 // Recording of raw PCM data
167 int32_t StartRawOutputFileRecording(
168 const char pcmFileNameUTF8[kAdmMaxFileNameSize]) override;
169 int32_t StopRawOutputFileRecording() override;
170 int32_t StartRawInputFileRecording(
171 const char pcmFileNameUTF8[kAdmMaxFileNameSize]) override;
172 int32_t StopRawInputFileRecording() override;
niklase@google.com470e71d2011-07-07 08:21:25 +0000173
henrikaba35d052015-07-14 17:04:08 +0200174 // Native sample rate controls (samples/sec)
175 int32_t SetRecordingSampleRate(const uint32_t samplesPerSec) override;
176 int32_t RecordingSampleRate(uint32_t* samplesPerSec) const override;
177 int32_t SetPlayoutSampleRate(const uint32_t samplesPerSec) override;
178 int32_t PlayoutSampleRate(uint32_t* samplesPerSec) const override;
niklase@google.com470e71d2011-07-07 08:21:25 +0000179
henrikaba35d052015-07-14 17:04:08 +0200180 // Mobile device specific functions
181 int32_t ResetAudioDevice() override;
182 int32_t SetLoudspeakerStatus(bool enable) override;
183 int32_t GetLoudspeakerStatus(bool* enabled) const override;
niklase@google.com470e71d2011-07-07 08:21:25 +0000184
henrikaba35d052015-07-14 17:04:08 +0200185 bool BuiltInAECIsAvailable() const override;
henrika@webrtc.orga954c072014-12-09 16:22:09 +0000186
henrikaba35d052015-07-14 17:04:08 +0200187 int32_t EnableBuiltInAEC(bool enable) override;
188 bool BuiltInAECIsEnabled() const override;
andrew@webrtc.orga3c6d612011-09-13 17:17:49 +0000189
henrikaba35d052015-07-14 17:04:08 +0200190 int GetPlayoutAudioParameters(AudioParameters* params) const override;
191 int GetRecordAudioParameters(AudioParameters* params) const override;
192
193 int32_t Id() { return _id; }
Tommi931e6582015-05-20 09:44:38 +0200194#if defined(WEBRTC_ANDROID)
henrikaba35d052015-07-14 17:04:08 +0200195 // Only use this acccessor for test purposes on Android.
196 AudioManager* GetAndroidAudioManagerForTest() {
197 return _audioManagerAndroid.get();
198 }
henrika24e56e32015-05-19 11:48:51 +0200199#endif
henrikaba35d052015-07-14 17:04:08 +0200200 AudioDeviceBuffer* GetAudioDeviceBuffer() { return &_audioDeviceBuffer; }
henrika@webrtc.org474d1eb2015-03-09 12:39:53 +0000201
henrikaba35d052015-07-14 17:04:08 +0200202 private:
203 PlatformType Platform() const;
204 AudioLayer PlatformAudioLayer() const;
niklase@google.com470e71d2011-07-07 08:21:25 +0000205
henrikaba35d052015-07-14 17:04:08 +0200206 CriticalSectionWrapper& _critSect;
207 CriticalSectionWrapper& _critSectEventCb;
208 CriticalSectionWrapper& _critSectAudioCb;
niklase@google.com470e71d2011-07-07 08:21:25 +0000209
henrikaba35d052015-07-14 17:04:08 +0200210 AudioDeviceObserver* _ptrCbAudioDeviceObserver;
niklase@google.com470e71d2011-07-07 08:21:25 +0000211
henrikaba35d052015-07-14 17:04:08 +0200212 AudioDeviceGeneric* _ptrAudioDevice;
niklase@google.com470e71d2011-07-07 08:21:25 +0000213
henrikaba35d052015-07-14 17:04:08 +0200214 AudioDeviceBuffer _audioDeviceBuffer;
Tommi931e6582015-05-20 09:44:38 +0200215#if defined(WEBRTC_ANDROID)
henrikaba35d052015-07-14 17:04:08 +0200216 rtc::scoped_ptr<AudioManager> _audioManagerAndroid;
henrikab2619892015-05-18 16:49:16 +0200217#endif
henrikaba35d052015-07-14 17:04:08 +0200218 int32_t _id;
219 AudioLayer _platformAudioLayer;
220 int64_t _lastProcessTime;
221 PlatformType _platformType;
222 bool _initialized;
223 mutable ErrorCode _lastError;
niklase@google.com470e71d2011-07-07 08:21:25 +0000224};
225
226} // namespace webrtc
227
Tommi931e6582015-05-20 09:44:38 +0200228#endif // defined(WEBRTC_INCLUDE_INTERNAL_AUDIO_DEVICE)
229
niklase@google.com470e71d2011-07-07 08:21:25 +0000230#endif // WEBRTC_MODULES_INTERFACE_AUDIO_DEVICE_IMPL_H_