blob: f2f4996154e86ee3de23af7963bbc0b7aa63d350 [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
henrikab2619892015-05-18 16:49:16 +020014#include "webrtc/base/checks.h"
15#include "webrtc/base/scoped_ptr.h"
pbos@webrtc.org811269d2013-07-11 13:24:38 +000016#include "webrtc/modules/audio_device/audio_device_buffer.h"
17#include "webrtc/modules/audio_device/include/audio_device.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000018
19namespace webrtc
20{
21
22class AudioDeviceGeneric;
henrikab2619892015-05-18 16:49:16 +020023class AudioManager;
niklase@google.com470e71d2011-07-07 08:21:25 +000024class CriticalSectionWrapper;
25
26class AudioDeviceModuleImpl : public AudioDeviceModule
27{
28public:
29 enum PlatformType
30 {
31 kPlatformNotSupported = 0,
32 kPlatformWin32 = 1,
33 kPlatformWinCe = 2,
34 kPlatformLinux = 3,
leozwang@google.com522f42b2011-09-19 17:39:05 +000035 kPlatformMac = 4,
sjlee@webrtc.org4b425082012-09-10 17:58:21 +000036 kPlatformAndroid = 5,
37 kPlatformIOS = 6
niklase@google.com470e71d2011-07-07 08:21:25 +000038 };
39
pbos@webrtc.org25509882013-04-09 10:30:35 +000040 int32_t CheckPlatform();
41 int32_t CreatePlatformSpecificObjects();
42 int32_t AttachAudioBuffer();
niklase@google.com470e71d2011-07-07 08:21:25 +000043
pbos@webrtc.org25509882013-04-09 10:30:35 +000044 AudioDeviceModuleImpl(const int32_t id, const AudioLayer audioLayer);
niklase@google.com470e71d2011-07-07 08:21:25 +000045 virtual ~AudioDeviceModuleImpl();
46
henrika@google.com73d65512011-09-07 15:11:18 +000047public: // RefCountedModule
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000048 int64_t TimeUntilNextProcess() override;
49 int32_t Process() override;
niklase@google.com470e71d2011-07-07 08:21:25 +000050
51public:
52 // Factory methods (resource allocation/deallocation)
53 static AudioDeviceModule* Create(
pbos@webrtc.org25509882013-04-09 10:30:35 +000054 const int32_t id,
niklase@google.com470e71d2011-07-07 08:21:25 +000055 const AudioLayer audioLayer = kPlatformDefaultAudio);
niklase@google.com470e71d2011-07-07 08:21:25 +000056
57 // Retrieve the currently utilized audio layer
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000058 int32_t ActiveAudioLayer(AudioLayer* audioLayer) const override;
niklase@google.com470e71d2011-07-07 08:21:25 +000059
60 // Error handling
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000061 ErrorCode LastError() const override;
62 int32_t RegisterEventObserver(AudioDeviceObserver* eventCallback) override;
niklase@google.com470e71d2011-07-07 08:21:25 +000063
64 // Full-duplex transportation of PCM audio
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000065 int32_t RegisterAudioCallback(AudioTransport* audioCallback) override;
niklase@google.com470e71d2011-07-07 08:21:25 +000066
67 // Main initializaton and termination
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000068 int32_t Init() override;
69 int32_t Terminate() override;
70 bool Initialized() const override;
niklase@google.com470e71d2011-07-07 08:21:25 +000071
72 // Device enumeration
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000073 int16_t PlayoutDevices() override;
74 int16_t RecordingDevices() override;
75 int32_t PlayoutDeviceName(uint16_t index,
76 char name[kAdmMaxDeviceNameSize],
77 char guid[kAdmMaxGuidSize]) override;
78 int32_t RecordingDeviceName(uint16_t index,
79 char name[kAdmMaxDeviceNameSize],
80 char guid[kAdmMaxGuidSize]) override;
niklase@google.com470e71d2011-07-07 08:21:25 +000081
82 // Device selection
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000083 int32_t SetPlayoutDevice(uint16_t index) override;
84 int32_t SetPlayoutDevice(WindowsDeviceType device) override;
85 int32_t SetRecordingDevice(uint16_t index) override;
86 int32_t SetRecordingDevice(WindowsDeviceType device) override;
niklase@google.com470e71d2011-07-07 08:21:25 +000087
88 // Audio transport initialization
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000089 int32_t PlayoutIsAvailable(bool* available) override;
90 int32_t InitPlayout() override;
91 bool PlayoutIsInitialized() const override;
92 int32_t RecordingIsAvailable(bool* available) override;
93 int32_t InitRecording() override;
94 bool RecordingIsInitialized() const override;
niklase@google.com470e71d2011-07-07 08:21:25 +000095
96 // Audio transport control
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000097 int32_t StartPlayout() override;
98 int32_t StopPlayout() override;
99 bool Playing() const override;
100 int32_t StartRecording() override;
101 int32_t StopRecording() override;
102 bool Recording() const override;
niklase@google.com470e71d2011-07-07 08:21:25 +0000103
104 // Microphone Automatic Gain Control (AGC)
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000105 int32_t SetAGC(bool enable) override;
106 bool AGC() const override;
niklase@google.com470e71d2011-07-07 08:21:25 +0000107
108 // Volume control based on the Windows Wave API (Windows only)
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000109 int32_t SetWaveOutVolume(uint16_t volumeLeft,
110 uint16_t volumeRight) override;
111 int32_t WaveOutVolume(uint16_t* volumeLeft,
112 uint16_t* volumeRight) const override;
niklase@google.com470e71d2011-07-07 08:21:25 +0000113
114 // Audio mixer initialization
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000115 int32_t InitSpeaker() override;
116 bool SpeakerIsInitialized() const override;
117 int32_t InitMicrophone() override;
118 bool MicrophoneIsInitialized() const override;
niklase@google.com470e71d2011-07-07 08:21:25 +0000119
120 // Speaker volume controls
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000121 int32_t SpeakerVolumeIsAvailable(bool* available) override;
122 int32_t SetSpeakerVolume(uint32_t volume) override;
123 int32_t SpeakerVolume(uint32_t* volume) const override;
124 int32_t MaxSpeakerVolume(uint32_t* maxVolume) const override;
125 int32_t MinSpeakerVolume(uint32_t* minVolume) const override;
126 int32_t SpeakerVolumeStepSize(uint16_t* stepSize) const override;
niklase@google.com470e71d2011-07-07 08:21:25 +0000127
128 // Microphone volume controls
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000129 int32_t MicrophoneVolumeIsAvailable(bool* available) override;
130 int32_t SetMicrophoneVolume(uint32_t volume) override;
131 int32_t MicrophoneVolume(uint32_t* volume) const override;
132 int32_t MaxMicrophoneVolume(uint32_t* maxVolume) const override;
133 int32_t MinMicrophoneVolume(uint32_t* minVolume) const override;
134 int32_t MicrophoneVolumeStepSize(uint16_t* stepSize) const override;
niklase@google.com470e71d2011-07-07 08:21:25 +0000135
136 // Speaker mute control
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000137 int32_t SpeakerMuteIsAvailable(bool* available) override;
138 int32_t SetSpeakerMute(bool enable) override;
139 int32_t SpeakerMute(bool* enabled) const override;
niklase@google.com470e71d2011-07-07 08:21:25 +0000140
141 // Microphone mute control
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000142 int32_t MicrophoneMuteIsAvailable(bool* available) override;
143 int32_t SetMicrophoneMute(bool enable) override;
144 int32_t MicrophoneMute(bool* enabled) const override;
niklase@google.com470e71d2011-07-07 08:21:25 +0000145
146 // Microphone boost control
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000147 int32_t MicrophoneBoostIsAvailable(bool* available) override;
148 int32_t SetMicrophoneBoost(bool enable) override;
149 int32_t MicrophoneBoost(bool* enabled) const override;
niklase@google.com470e71d2011-07-07 08:21:25 +0000150
151 // Stereo support
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000152 int32_t StereoPlayoutIsAvailable(bool* available) const override;
153 int32_t SetStereoPlayout(bool enable) override;
154 int32_t StereoPlayout(bool* enabled) const override;
155 int32_t StereoRecordingIsAvailable(bool* available) const override;
156 int32_t SetStereoRecording(bool enable) override;
157 int32_t StereoRecording(bool* enabled) const override;
158 int32_t SetRecordingChannel(const ChannelType channel) override;
159 int32_t RecordingChannel(ChannelType* channel) const override;
niklase@google.com470e71d2011-07-07 08:21:25 +0000160
161 // Delay information and control
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000162 int32_t SetPlayoutBuffer(const BufferType type,
163 uint16_t sizeMS = 0) override;
164 int32_t PlayoutBuffer(BufferType* type, uint16_t* sizeMS) const override;
165 int32_t PlayoutDelay(uint16_t* delayMS) const override;
166 int32_t RecordingDelay(uint16_t* delayMS) const override;
niklase@google.com470e71d2011-07-07 08:21:25 +0000167
168 // CPU load
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000169 int32_t CPULoad(uint16_t* load) const override;
niklase@google.com470e71d2011-07-07 08:21:25 +0000170
171 // Recording of raw PCM data
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000172 int32_t StartRawOutputFileRecording(
173 const char pcmFileNameUTF8[kAdmMaxFileNameSize]) override;
174 int32_t StopRawOutputFileRecording() override;
175 int32_t StartRawInputFileRecording(
176 const char pcmFileNameUTF8[kAdmMaxFileNameSize]) override;
177 int32_t StopRawInputFileRecording() override;
niklase@google.com470e71d2011-07-07 08:21:25 +0000178
179 // Native sample rate controls (samples/sec)
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000180 int32_t SetRecordingSampleRate(const uint32_t samplesPerSec) override;
181 int32_t RecordingSampleRate(uint32_t* samplesPerSec) const override;
182 int32_t SetPlayoutSampleRate(const uint32_t samplesPerSec) override;
183 int32_t PlayoutSampleRate(uint32_t* samplesPerSec) const override;
niklase@google.com470e71d2011-07-07 08:21:25 +0000184
185 // Mobile device specific functions
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000186 int32_t ResetAudioDevice() override;
187 int32_t SetLoudspeakerStatus(bool enable) override;
188 int32_t GetLoudspeakerStatus(bool* enabled) const override;
niklase@google.com470e71d2011-07-07 08:21:25 +0000189
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000190 bool BuiltInAECIsAvailable() const override;
henrika@webrtc.orga954c072014-12-09 16:22:09 +0000191
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000192 int32_t EnableBuiltInAEC(bool enable) override;
193 bool BuiltInAECIsEnabled() const override;
andrew@webrtc.orga3c6d612011-09-13 17:17:49 +0000194
niklase@google.com470e71d2011-07-07 08:21:25 +0000195public:
pbos@webrtc.org25509882013-04-09 10:30:35 +0000196 int32_t Id() {return _id;}
henrika24e56e32015-05-19 11:48:51 +0200197#if defined(WEBRTC_ANDROID) && !defined(WEBRTC_CHROMIUM_BUILD)
henrikab2619892015-05-18 16:49:16 +0200198 // Only use this acccessor for test purposes on Android.
199 AudioManager* GetAndroidAudioManagerForTest() {
henrikab2619892015-05-18 16:49:16 +0200200 return _audioManagerAndroid.get();
henrikab2619892015-05-18 16:49:16 +0200201 }
henrika24e56e32015-05-19 11:48:51 +0200202#endif
henrika@webrtc.org474d1eb2015-03-09 12:39:53 +0000203 AudioDeviceBuffer* GetAudioDeviceBuffer() {
henrikab2619892015-05-18 16:49:16 +0200204 return &_audioDeviceBuffer;
henrika@webrtc.org474d1eb2015-03-09 12:39:53 +0000205 }
206
niklase@google.com470e71d2011-07-07 08:21:25 +0000207private:
208 PlatformType Platform() const;
209 AudioLayer PlatformAudioLayer() const;
210
211private:
henrikab2619892015-05-18 16:49:16 +0200212 CriticalSectionWrapper& _critSect;
213 CriticalSectionWrapper& _critSectEventCb;
214 CriticalSectionWrapper& _critSectAudioCb;
niklase@google.com470e71d2011-07-07 08:21:25 +0000215
henrikab2619892015-05-18 16:49:16 +0200216 AudioDeviceObserver* _ptrCbAudioDeviceObserver;
niklase@google.com470e71d2011-07-07 08:21:25 +0000217
henrikab2619892015-05-18 16:49:16 +0200218 AudioDeviceGeneric* _ptrAudioDevice;
niklase@google.com470e71d2011-07-07 08:21:25 +0000219
henrikab2619892015-05-18 16:49:16 +0200220 AudioDeviceBuffer _audioDeviceBuffer;
henrika24e56e32015-05-19 11:48:51 +0200221#if defined(WEBRTC_ANDROID) && !defined(WEBRTC_CHROMIUM_BUILD)
henrikab2619892015-05-18 16:49:16 +0200222 rtc::scoped_ptr<AudioManager> _audioManagerAndroid;
223#endif
224 int32_t _id;
225 AudioLayer _platformAudioLayer;
Tommi68898a22015-05-19 17:28:07 +0200226 int64_t _lastProcessTime;
henrikab2619892015-05-18 16:49:16 +0200227 PlatformType _platformType;
228 bool _initialized;
229 mutable ErrorCode _lastError;
niklase@google.com470e71d2011-07-07 08:21:25 +0000230};
231
232} // namespace webrtc
233
234#endif // WEBRTC_MODULES_INTERFACE_AUDIO_DEVICE_IMPL_H_