blob: e81670ef007e4e7e71e8e802637ce3c1f6cc4a75 [file] [log] [blame]
Lu Liu4b620012017-04-06 10:17:01 -07001/*
2 * Copyright (c) 2017 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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "modules/audio_device/include/audio_device_data_observer.h"
Yves Gerey988cc082018-10-23 12:03:01 +020012
13#include "modules/audio_device/include/audio_device_defines.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020014#include "rtc_base/checks.h"
Steve Anton10542f22019-01-11 09:11:00 -080015#include "rtc_base/ref_counted_object.h"
Lu Liu4b620012017-04-06 10:17:01 -070016
17namespace webrtc {
18
19namespace {
20
21// A wrapper over AudioDeviceModule that registers itself as AudioTransport
22// callback and redirects the PCM data to AudioDeviceDataObserver callback.
23class ADMWrapper : public AudioDeviceModule, public AudioTransport {
24 public:
Danil Chapovalov1c41be62019-04-01 09:16:12 +020025 ADMWrapper(AudioLayer audio_layer,
26 TaskQueueFactory* task_queue_factory,
27 AudioDeviceDataObserver* observer)
28 : impl_(AudioDeviceModule::Create(audio_layer, task_queue_factory)),
29 observer_(observer) {
Lu Liu4b620012017-04-06 10:17:01 -070030 // Register self as the audio transport callback for underlying ADM impl.
31 auto res = impl_->RegisterAudioCallback(this);
32 is_valid_ = (impl_.get() != nullptr) && (res == 0);
33 }
Mirko Bonadeife055c12019-01-29 22:53:28 +010034 ~ADMWrapper() override {
Lu Liu4b620012017-04-06 10:17:01 -070035 audio_transport_ = nullptr;
36 observer_ = nullptr;
37 }
38
39 // Make sure we have a valid ADM before returning it to user.
40 bool IsValid() { return is_valid_; }
41
Lu Liu4b620012017-04-06 10:17:01 -070042 // AudioTransport methods overrides.
43 int32_t RecordedDataIsAvailable(const void* audioSamples,
44 const size_t nSamples,
45 const size_t nBytesPerSample,
46 const size_t nChannels,
47 const uint32_t samples_per_sec,
48 const uint32_t total_delay_ms,
49 const int32_t clockDrift,
50 const uint32_t currentMicLevel,
51 const bool keyPressed,
52 uint32_t& newMicLevel) override {
53 int32_t res = 0;
54 // Capture PCM data of locally captured audio.
55 if (observer_) {
56 observer_->OnCaptureData(audioSamples, nSamples, nBytesPerSample,
57 nChannels, samples_per_sec);
58 }
59
60 // Send to the actual audio transport.
61 if (audio_transport_) {
62 res = audio_transport_->RecordedDataIsAvailable(
63 audioSamples, nSamples, nBytesPerSample, nChannels, samples_per_sec,
64 total_delay_ms, clockDrift, currentMicLevel, keyPressed, newMicLevel);
65 }
66
67 return res;
68 }
69
70 int32_t NeedMorePlayData(const size_t nSamples,
71 const size_t nBytesPerSample,
72 const size_t nChannels,
73 const uint32_t samples_per_sec,
74 void* audioSamples,
75 size_t& nSamplesOut,
76 int64_t* elapsed_time_ms,
77 int64_t* ntp_time_ms) override {
78 int32_t res = 0;
Artem Titovac9365e2018-03-28 15:21:54 +020079 // Set out parameters to safe values to be sure not to return corrupted
80 // data.
81 nSamplesOut = 0;
82 *elapsed_time_ms = -1;
83 *ntp_time_ms = -1;
Lu Liu4b620012017-04-06 10:17:01 -070084 // Request data from audio transport.
85 if (audio_transport_) {
86 res = audio_transport_->NeedMorePlayData(
87 nSamples, nBytesPerSample, nChannels, samples_per_sec, audioSamples,
88 nSamplesOut, elapsed_time_ms, ntp_time_ms);
89 }
90
91 // Capture rendered data.
92 if (observer_) {
93 observer_->OnRenderData(audioSamples, nSamples, nBytesPerSample,
94 nChannels, samples_per_sec);
95 }
96
97 return res;
98 }
99
Lu Liu4b620012017-04-06 10:17:01 -0700100 void PullRenderData(int bits_per_sample,
101 int sample_rate,
102 size_t number_of_channels,
103 size_t number_of_frames,
104 void* audio_data,
105 int64_t* elapsed_time_ms,
106 int64_t* ntp_time_ms) override {
107 RTC_NOTREACHED();
108 }
109
110 // Override AudioDeviceModule's RegisterAudioCallback method to remember the
111 // actual audio transport (e.g.: voice engine).
112 int32_t RegisterAudioCallback(AudioTransport* audio_callback) override {
113 // Remember the audio callback to forward PCM data
114 audio_transport_ = audio_callback;
115 return 0;
116 }
117
118 // AudioDeviceModule pass through method overrides.
119 int32_t ActiveAudioLayer(AudioLayer* audio_layer) const override {
120 return impl_->ActiveAudioLayer(audio_layer);
121 }
Lu Liu4b620012017-04-06 10:17:01 -0700122 int32_t Init() override { return impl_->Init(); }
123 int32_t Terminate() override { return impl_->Terminate(); }
124 bool Initialized() const override { return impl_->Initialized(); }
125 int16_t PlayoutDevices() override { return impl_->PlayoutDevices(); }
126 int16_t RecordingDevices() override { return impl_->RecordingDevices(); }
127 int32_t PlayoutDeviceName(uint16_t index,
128 char name[kAdmMaxDeviceNameSize],
129 char guid[kAdmMaxGuidSize]) override {
130 return impl_->PlayoutDeviceName(index, name, guid);
131 }
132 int32_t RecordingDeviceName(uint16_t index,
133 char name[kAdmMaxDeviceNameSize],
134 char guid[kAdmMaxGuidSize]) override {
135 return impl_->RecordingDeviceName(index, name, guid);
136 }
137 int32_t SetPlayoutDevice(uint16_t index) override {
138 return impl_->SetPlayoutDevice(index);
139 }
140 int32_t SetPlayoutDevice(WindowsDeviceType device) override {
141 return impl_->SetPlayoutDevice(device);
142 }
143 int32_t SetRecordingDevice(uint16_t index) override {
144 return impl_->SetRecordingDevice(index);
145 }
146 int32_t SetRecordingDevice(WindowsDeviceType device) override {
147 return impl_->SetRecordingDevice(device);
148 }
149 int32_t PlayoutIsAvailable(bool* available) override {
150 return impl_->PlayoutIsAvailable(available);
151 }
152 int32_t InitPlayout() override { return impl_->InitPlayout(); }
153 bool PlayoutIsInitialized() const override {
154 return impl_->PlayoutIsInitialized();
155 }
156 int32_t RecordingIsAvailable(bool* available) override {
157 return impl_->RecordingIsAvailable(available);
158 }
159 int32_t InitRecording() override { return impl_->InitRecording(); }
160 bool RecordingIsInitialized() const override {
161 return impl_->RecordingIsInitialized();
162 }
163 int32_t StartPlayout() override { return impl_->StartPlayout(); }
164 int32_t StopPlayout() override { return impl_->StopPlayout(); }
165 bool Playing() const override { return impl_->Playing(); }
166 int32_t StartRecording() override { return impl_->StartRecording(); }
167 int32_t StopRecording() override { return impl_->StopRecording(); }
168 bool Recording() const override { return impl_->Recording(); }
Lu Liu4b620012017-04-06 10:17:01 -0700169 int32_t InitSpeaker() override { return impl_->InitSpeaker(); }
170 bool SpeakerIsInitialized() const override {
171 return impl_->SpeakerIsInitialized();
172 }
173 int32_t InitMicrophone() override { return impl_->InitMicrophone(); }
174 bool MicrophoneIsInitialized() const override {
175 return impl_->MicrophoneIsInitialized();
176 }
177 int32_t SpeakerVolumeIsAvailable(bool* available) override {
178 return impl_->SpeakerVolumeIsAvailable(available);
179 }
180 int32_t SetSpeakerVolume(uint32_t volume) override {
181 return impl_->SetSpeakerVolume(volume);
182 }
183 int32_t SpeakerVolume(uint32_t* volume) const override {
184 return impl_->SpeakerVolume(volume);
185 }
186 int32_t MaxSpeakerVolume(uint32_t* max_volume) const override {
187 return impl_->MaxSpeakerVolume(max_volume);
188 }
189 int32_t MinSpeakerVolume(uint32_t* min_volume) const override {
190 return impl_->MinSpeakerVolume(min_volume);
191 }
Lu Liu4b620012017-04-06 10:17:01 -0700192 int32_t MicrophoneVolumeIsAvailable(bool* available) override {
193 return impl_->MicrophoneVolumeIsAvailable(available);
194 }
195 int32_t SetMicrophoneVolume(uint32_t volume) override {
196 return impl_->SetMicrophoneVolume(volume);
197 }
198 int32_t MicrophoneVolume(uint32_t* volume) const override {
199 return impl_->MicrophoneVolume(volume);
200 }
201 int32_t MaxMicrophoneVolume(uint32_t* max_volume) const override {
202 return impl_->MaxMicrophoneVolume(max_volume);
203 }
204 int32_t MinMicrophoneVolume(uint32_t* min_volume) const override {
205 return impl_->MinMicrophoneVolume(min_volume);
206 }
Lu Liu4b620012017-04-06 10:17:01 -0700207 int32_t SpeakerMuteIsAvailable(bool* available) override {
208 return impl_->SpeakerMuteIsAvailable(available);
209 }
210 int32_t SetSpeakerMute(bool enable) override {
211 return impl_->SetSpeakerMute(enable);
212 }
213 int32_t SpeakerMute(bool* enabled) const override {
214 return impl_->SpeakerMute(enabled);
215 }
216 int32_t MicrophoneMuteIsAvailable(bool* available) override {
217 return impl_->MicrophoneMuteIsAvailable(available);
218 }
219 int32_t SetMicrophoneMute(bool enable) override {
220 return impl_->SetMicrophoneMute(enable);
221 }
222 int32_t MicrophoneMute(bool* enabled) const override {
223 return impl_->MicrophoneMute(enabled);
224 }
Lu Liu4b620012017-04-06 10:17:01 -0700225 int32_t StereoPlayoutIsAvailable(bool* available) const override {
226 return impl_->StereoPlayoutIsAvailable(available);
227 }
228 int32_t SetStereoPlayout(bool enable) override {
229 return impl_->SetStereoPlayout(enable);
230 }
231 int32_t StereoPlayout(bool* enabled) const override {
232 return impl_->StereoPlayout(enabled);
233 }
234 int32_t StereoRecordingIsAvailable(bool* available) const override {
235 return impl_->StereoRecordingIsAvailable(available);
236 }
237 int32_t SetStereoRecording(bool enable) override {
238 return impl_->SetStereoRecording(enable);
239 }
240 int32_t StereoRecording(bool* enabled) const override {
241 return impl_->StereoRecording(enabled);
242 }
Lu Liu4b620012017-04-06 10:17:01 -0700243 int32_t PlayoutDelay(uint16_t* delay_ms) const override {
244 return impl_->PlayoutDelay(delay_ms);
245 }
Lu Liu4b620012017-04-06 10:17:01 -0700246 bool BuiltInAECIsAvailable() const override {
247 return impl_->BuiltInAECIsAvailable();
248 }
249 bool BuiltInAGCIsAvailable() const override {
250 return impl_->BuiltInAGCIsAvailable();
251 }
252 bool BuiltInNSIsAvailable() const override {
253 return impl_->BuiltInNSIsAvailable();
254 }
255 int32_t EnableBuiltInAEC(bool enable) override {
256 return impl_->EnableBuiltInAEC(enable);
257 }
258 int32_t EnableBuiltInAGC(bool enable) override {
259 return impl_->EnableBuiltInAGC(enable);
260 }
261 int32_t EnableBuiltInNS(bool enable) override {
262 return impl_->EnableBuiltInNS(enable);
263 }
Alex Narestbbeb1092019-08-16 11:49:04 +0200264 int32_t GetPlayoutUnderrunCount() const override {
265 return impl_->GetPlayoutUnderrunCount();
266 }
Lu Liu4b620012017-04-06 10:17:01 -0700267// Only supported on iOS.
268#if defined(WEBRTC_IOS)
269 int GetPlayoutAudioParameters(AudioParameters* params) const override {
270 return impl_->GetPlayoutAudioParameters(params);
271 }
272 int GetRecordAudioParameters(AudioParameters* params) const override {
273 return impl_->GetRecordAudioParameters(params);
274 }
275#endif // WEBRTC_IOS
276
277 protected:
278 rtc::scoped_refptr<AudioDeviceModule> impl_;
279 AudioDeviceDataObserver* observer_ = nullptr;
280 AudioTransport* audio_transport_ = nullptr;
281 bool is_valid_ = false;
282};
283
284} // namespace
285
286rtc::scoped_refptr<AudioDeviceModule> CreateAudioDeviceWithDataObserver(
Danil Chapovalov1c41be62019-04-01 09:16:12 +0200287 AudioDeviceModule::AudioLayer audio_layer,
288 TaskQueueFactory* task_queue_factory,
289 AudioDeviceDataObserver* observer) {
Lu Liu4b620012017-04-06 10:17:01 -0700290 rtc::scoped_refptr<ADMWrapper> audio_device(
Danil Chapovalov1c41be62019-04-01 09:16:12 +0200291 new rtc::RefCountedObject<ADMWrapper>(audio_layer, task_queue_factory,
292 observer));
Lu Liu4b620012017-04-06 10:17:01 -0700293
294 if (!audio_device->IsValid()) {
295 return nullptr;
296 }
297
298 return audio_device;
299}
Lu Liu4b620012017-04-06 10:17:01 -0700300} // namespace webrtc