1 /*
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
11 #include "modules/audio_device/include/audio_device_data_observer.h"
12
13 #include "api/make_ref_counted.h"
14 #include "modules/audio_device/include/audio_device_defines.h"
15 #include "rtc_base/checks.h"
16
17 namespace webrtc {
18
19 namespace {
20
21 // A wrapper over AudioDeviceModule that registers itself as AudioTransport
22 // callback and redirects the PCM data to AudioDeviceDataObserver callback.
23 class ADMWrapper : public AudioDeviceModule, public AudioTransport {
24 public:
ADMWrapper(rtc::scoped_refptr<AudioDeviceModule> impl,AudioDeviceDataObserver * legacy_observer,std::unique_ptr<AudioDeviceDataObserver> observer)25 ADMWrapper(rtc::scoped_refptr<AudioDeviceModule> impl,
26 AudioDeviceDataObserver* legacy_observer,
27 std::unique_ptr<AudioDeviceDataObserver> observer)
28 : impl_(impl),
29 legacy_observer_(legacy_observer),
30 observer_(std::move(observer)) {
31 is_valid_ = impl_.get() != nullptr;
32 }
ADMWrapper(AudioLayer audio_layer,TaskQueueFactory * task_queue_factory,AudioDeviceDataObserver * legacy_observer,std::unique_ptr<AudioDeviceDataObserver> observer)33 ADMWrapper(AudioLayer audio_layer,
34 TaskQueueFactory* task_queue_factory,
35 AudioDeviceDataObserver* legacy_observer,
36 std::unique_ptr<AudioDeviceDataObserver> observer)
37 : ADMWrapper(AudioDeviceModule::Create(audio_layer, task_queue_factory),
38 legacy_observer,
39 std::move(observer)) {}
~ADMWrapper()40 ~ADMWrapper() override {
41 audio_transport_ = nullptr;
42 observer_ = nullptr;
43 }
44
45 // Make sure we have a valid ADM before returning it to user.
IsValid()46 bool IsValid() { return is_valid_; }
47
RecordedDataIsAvailable(const void * audioSamples,size_t nSamples,size_t nBytesPerSample,size_t nChannels,uint32_t samples_per_sec,uint32_t total_delay_ms,int32_t clockDrift,uint32_t currentMicLevel,bool keyPressed,uint32_t & newMicLevel)48 int32_t RecordedDataIsAvailable(const void* audioSamples,
49 size_t nSamples,
50 size_t nBytesPerSample,
51 size_t nChannels,
52 uint32_t samples_per_sec,
53 uint32_t total_delay_ms,
54 int32_t clockDrift,
55 uint32_t currentMicLevel,
56 bool keyPressed,
57 uint32_t& newMicLevel) override {
58 return RecordedDataIsAvailable(audioSamples, nSamples, nBytesPerSample,
59 nChannels, samples_per_sec, total_delay_ms,
60 clockDrift, currentMicLevel, keyPressed,
61 newMicLevel, /*capture_timestamp_ns*/ 0);
62 }
63
64 // AudioTransport methods overrides.
RecordedDataIsAvailable(const void * audioSamples,size_t nSamples,size_t nBytesPerSample,size_t nChannels,uint32_t samples_per_sec,uint32_t total_delay_ms,int32_t clockDrift,uint32_t currentMicLevel,bool keyPressed,uint32_t & newMicLevel,int64_t capture_timestamp_ns)65 int32_t RecordedDataIsAvailable(const void* audioSamples,
66 size_t nSamples,
67 size_t nBytesPerSample,
68 size_t nChannels,
69 uint32_t samples_per_sec,
70 uint32_t total_delay_ms,
71 int32_t clockDrift,
72 uint32_t currentMicLevel,
73 bool keyPressed,
74 uint32_t& newMicLevel,
75 int64_t capture_timestamp_ns) override {
76 int32_t res = 0;
77 // Capture PCM data of locally captured audio.
78 if (observer_) {
79 observer_->OnCaptureData(audioSamples, nSamples, nBytesPerSample,
80 nChannels, samples_per_sec);
81 }
82
83 // Send to the actual audio transport.
84 if (audio_transport_) {
85 res = audio_transport_->RecordedDataIsAvailable(
86 audioSamples, nSamples, nBytesPerSample, nChannels, samples_per_sec,
87 total_delay_ms, clockDrift, currentMicLevel, keyPressed, newMicLevel,
88 capture_timestamp_ns);
89 }
90
91 return res;
92 }
93
NeedMorePlayData(const size_t nSamples,const size_t nBytesPerSample,const size_t nChannels,const uint32_t samples_per_sec,void * audioSamples,size_t & nSamplesOut,int64_t * elapsed_time_ms,int64_t * ntp_time_ms)94 int32_t NeedMorePlayData(const size_t nSamples,
95 const size_t nBytesPerSample,
96 const size_t nChannels,
97 const uint32_t samples_per_sec,
98 void* audioSamples,
99 size_t& nSamplesOut,
100 int64_t* elapsed_time_ms,
101 int64_t* ntp_time_ms) override {
102 int32_t res = 0;
103 // Set out parameters to safe values to be sure not to return corrupted
104 // data.
105 nSamplesOut = 0;
106 *elapsed_time_ms = -1;
107 *ntp_time_ms = -1;
108 // Request data from audio transport.
109 if (audio_transport_) {
110 res = audio_transport_->NeedMorePlayData(
111 nSamples, nBytesPerSample, nChannels, samples_per_sec, audioSamples,
112 nSamplesOut, elapsed_time_ms, ntp_time_ms);
113 }
114
115 // Capture rendered data.
116 if (observer_) {
117 observer_->OnRenderData(audioSamples, nSamples, nBytesPerSample,
118 nChannels, samples_per_sec);
119 }
120
121 return res;
122 }
123
PullRenderData(int bits_per_sample,int sample_rate,size_t number_of_channels,size_t number_of_frames,void * audio_data,int64_t * elapsed_time_ms,int64_t * ntp_time_ms)124 void PullRenderData(int bits_per_sample,
125 int sample_rate,
126 size_t number_of_channels,
127 size_t number_of_frames,
128 void* audio_data,
129 int64_t* elapsed_time_ms,
130 int64_t* ntp_time_ms) override {
131 RTC_DCHECK_NOTREACHED();
132 }
133
134 // Override AudioDeviceModule's RegisterAudioCallback method to remember the
135 // actual audio transport (e.g.: voice engine).
RegisterAudioCallback(AudioTransport * audio_callback)136 int32_t RegisterAudioCallback(AudioTransport* audio_callback) override {
137 // Remember the audio callback to forward PCM data
138 audio_transport_ = audio_callback;
139 return 0;
140 }
141
142 // AudioDeviceModule pass through method overrides.
ActiveAudioLayer(AudioLayer * audio_layer) const143 int32_t ActiveAudioLayer(AudioLayer* audio_layer) const override {
144 return impl_->ActiveAudioLayer(audio_layer);
145 }
Init()146 int32_t Init() override {
147 int res = impl_->Init();
148 if (res != 0) {
149 return res;
150 }
151 // Register self as the audio transport callback for underlying ADM impl.
152 impl_->RegisterAudioCallback(this);
153 return res;
154 }
Terminate()155 int32_t Terminate() override { return impl_->Terminate(); }
Initialized() const156 bool Initialized() const override { return impl_->Initialized(); }
PlayoutDevices()157 int16_t PlayoutDevices() override { return impl_->PlayoutDevices(); }
RecordingDevices()158 int16_t RecordingDevices() override { return impl_->RecordingDevices(); }
PlayoutDeviceName(uint16_t index,char name[kAdmMaxDeviceNameSize],char guid[kAdmMaxGuidSize])159 int32_t PlayoutDeviceName(uint16_t index,
160 char name[kAdmMaxDeviceNameSize],
161 char guid[kAdmMaxGuidSize]) override {
162 return impl_->PlayoutDeviceName(index, name, guid);
163 }
RecordingDeviceName(uint16_t index,char name[kAdmMaxDeviceNameSize],char guid[kAdmMaxGuidSize])164 int32_t RecordingDeviceName(uint16_t index,
165 char name[kAdmMaxDeviceNameSize],
166 char guid[kAdmMaxGuidSize]) override {
167 return impl_->RecordingDeviceName(index, name, guid);
168 }
SetPlayoutDevice(uint16_t index)169 int32_t SetPlayoutDevice(uint16_t index) override {
170 return impl_->SetPlayoutDevice(index);
171 }
SetPlayoutDevice(WindowsDeviceType device)172 int32_t SetPlayoutDevice(WindowsDeviceType device) override {
173 return impl_->SetPlayoutDevice(device);
174 }
SetRecordingDevice(uint16_t index)175 int32_t SetRecordingDevice(uint16_t index) override {
176 return impl_->SetRecordingDevice(index);
177 }
SetRecordingDevice(WindowsDeviceType device)178 int32_t SetRecordingDevice(WindowsDeviceType device) override {
179 return impl_->SetRecordingDevice(device);
180 }
PlayoutIsAvailable(bool * available)181 int32_t PlayoutIsAvailable(bool* available) override {
182 return impl_->PlayoutIsAvailable(available);
183 }
InitPlayout()184 int32_t InitPlayout() override { return impl_->InitPlayout(); }
PlayoutIsInitialized() const185 bool PlayoutIsInitialized() const override {
186 return impl_->PlayoutIsInitialized();
187 }
RecordingIsAvailable(bool * available)188 int32_t RecordingIsAvailable(bool* available) override {
189 return impl_->RecordingIsAvailable(available);
190 }
InitRecording()191 int32_t InitRecording() override { return impl_->InitRecording(); }
RecordingIsInitialized() const192 bool RecordingIsInitialized() const override {
193 return impl_->RecordingIsInitialized();
194 }
StartPlayout()195 int32_t StartPlayout() override { return impl_->StartPlayout(); }
StopPlayout()196 int32_t StopPlayout() override { return impl_->StopPlayout(); }
Playing() const197 bool Playing() const override { return impl_->Playing(); }
StartRecording()198 int32_t StartRecording() override { return impl_->StartRecording(); }
StopRecording()199 int32_t StopRecording() override { return impl_->StopRecording(); }
Recording() const200 bool Recording() const override { return impl_->Recording(); }
InitSpeaker()201 int32_t InitSpeaker() override { return impl_->InitSpeaker(); }
SpeakerIsInitialized() const202 bool SpeakerIsInitialized() const override {
203 return impl_->SpeakerIsInitialized();
204 }
InitMicrophone()205 int32_t InitMicrophone() override { return impl_->InitMicrophone(); }
MicrophoneIsInitialized() const206 bool MicrophoneIsInitialized() const override {
207 return impl_->MicrophoneIsInitialized();
208 }
SpeakerVolumeIsAvailable(bool * available)209 int32_t SpeakerVolumeIsAvailable(bool* available) override {
210 return impl_->SpeakerVolumeIsAvailable(available);
211 }
SetSpeakerVolume(uint32_t volume)212 int32_t SetSpeakerVolume(uint32_t volume) override {
213 return impl_->SetSpeakerVolume(volume);
214 }
SpeakerVolume(uint32_t * volume) const215 int32_t SpeakerVolume(uint32_t* volume) const override {
216 return impl_->SpeakerVolume(volume);
217 }
MaxSpeakerVolume(uint32_t * max_volume) const218 int32_t MaxSpeakerVolume(uint32_t* max_volume) const override {
219 return impl_->MaxSpeakerVolume(max_volume);
220 }
MinSpeakerVolume(uint32_t * min_volume) const221 int32_t MinSpeakerVolume(uint32_t* min_volume) const override {
222 return impl_->MinSpeakerVolume(min_volume);
223 }
MicrophoneVolumeIsAvailable(bool * available)224 int32_t MicrophoneVolumeIsAvailable(bool* available) override {
225 return impl_->MicrophoneVolumeIsAvailable(available);
226 }
SetMicrophoneVolume(uint32_t volume)227 int32_t SetMicrophoneVolume(uint32_t volume) override {
228 return impl_->SetMicrophoneVolume(volume);
229 }
MicrophoneVolume(uint32_t * volume) const230 int32_t MicrophoneVolume(uint32_t* volume) const override {
231 return impl_->MicrophoneVolume(volume);
232 }
MaxMicrophoneVolume(uint32_t * max_volume) const233 int32_t MaxMicrophoneVolume(uint32_t* max_volume) const override {
234 return impl_->MaxMicrophoneVolume(max_volume);
235 }
MinMicrophoneVolume(uint32_t * min_volume) const236 int32_t MinMicrophoneVolume(uint32_t* min_volume) const override {
237 return impl_->MinMicrophoneVolume(min_volume);
238 }
SpeakerMuteIsAvailable(bool * available)239 int32_t SpeakerMuteIsAvailable(bool* available) override {
240 return impl_->SpeakerMuteIsAvailable(available);
241 }
SetSpeakerMute(bool enable)242 int32_t SetSpeakerMute(bool enable) override {
243 return impl_->SetSpeakerMute(enable);
244 }
SpeakerMute(bool * enabled) const245 int32_t SpeakerMute(bool* enabled) const override {
246 return impl_->SpeakerMute(enabled);
247 }
MicrophoneMuteIsAvailable(bool * available)248 int32_t MicrophoneMuteIsAvailable(bool* available) override {
249 return impl_->MicrophoneMuteIsAvailable(available);
250 }
SetMicrophoneMute(bool enable)251 int32_t SetMicrophoneMute(bool enable) override {
252 return impl_->SetMicrophoneMute(enable);
253 }
MicrophoneMute(bool * enabled) const254 int32_t MicrophoneMute(bool* enabled) const override {
255 return impl_->MicrophoneMute(enabled);
256 }
StereoPlayoutIsAvailable(bool * available) const257 int32_t StereoPlayoutIsAvailable(bool* available) const override {
258 return impl_->StereoPlayoutIsAvailable(available);
259 }
SetStereoPlayout(bool enable)260 int32_t SetStereoPlayout(bool enable) override {
261 return impl_->SetStereoPlayout(enable);
262 }
StereoPlayout(bool * enabled) const263 int32_t StereoPlayout(bool* enabled) const override {
264 return impl_->StereoPlayout(enabled);
265 }
StereoRecordingIsAvailable(bool * available) const266 int32_t StereoRecordingIsAvailable(bool* available) const override {
267 return impl_->StereoRecordingIsAvailable(available);
268 }
SetStereoRecording(bool enable)269 int32_t SetStereoRecording(bool enable) override {
270 return impl_->SetStereoRecording(enable);
271 }
StereoRecording(bool * enabled) const272 int32_t StereoRecording(bool* enabled) const override {
273 return impl_->StereoRecording(enabled);
274 }
PlayoutDelay(uint16_t * delay_ms) const275 int32_t PlayoutDelay(uint16_t* delay_ms) const override {
276 return impl_->PlayoutDelay(delay_ms);
277 }
BuiltInAECIsAvailable() const278 bool BuiltInAECIsAvailable() const override {
279 return impl_->BuiltInAECIsAvailable();
280 }
BuiltInAGCIsAvailable() const281 bool BuiltInAGCIsAvailable() const override {
282 return impl_->BuiltInAGCIsAvailable();
283 }
BuiltInNSIsAvailable() const284 bool BuiltInNSIsAvailable() const override {
285 return impl_->BuiltInNSIsAvailable();
286 }
EnableBuiltInAEC(bool enable)287 int32_t EnableBuiltInAEC(bool enable) override {
288 return impl_->EnableBuiltInAEC(enable);
289 }
EnableBuiltInAGC(bool enable)290 int32_t EnableBuiltInAGC(bool enable) override {
291 return impl_->EnableBuiltInAGC(enable);
292 }
EnableBuiltInNS(bool enable)293 int32_t EnableBuiltInNS(bool enable) override {
294 return impl_->EnableBuiltInNS(enable);
295 }
GetPlayoutUnderrunCount() const296 int32_t GetPlayoutUnderrunCount() const override {
297 return impl_->GetPlayoutUnderrunCount();
298 }
299 // Only supported on iOS.
300 #if defined(WEBRTC_IOS)
GetPlayoutAudioParameters(AudioParameters * params) const301 int GetPlayoutAudioParameters(AudioParameters* params) const override {
302 return impl_->GetPlayoutAudioParameters(params);
303 }
GetRecordAudioParameters(AudioParameters * params) const304 int GetRecordAudioParameters(AudioParameters* params) const override {
305 return impl_->GetRecordAudioParameters(params);
306 }
307 #endif // WEBRTC_IOS
308
309 protected:
310 rtc::scoped_refptr<AudioDeviceModule> impl_;
311 AudioDeviceDataObserver* legacy_observer_ = nullptr;
312 std::unique_ptr<AudioDeviceDataObserver> observer_;
313 AudioTransport* audio_transport_ = nullptr;
314 bool is_valid_ = false;
315 };
316
317 } // namespace
318
CreateAudioDeviceWithDataObserver(rtc::scoped_refptr<AudioDeviceModule> impl,std::unique_ptr<AudioDeviceDataObserver> observer)319 rtc::scoped_refptr<AudioDeviceModule> CreateAudioDeviceWithDataObserver(
320 rtc::scoped_refptr<AudioDeviceModule> impl,
321 std::unique_ptr<AudioDeviceDataObserver> observer) {
322 auto audio_device = rtc::make_ref_counted<ADMWrapper>(impl, observer.get(),
323 std::move(observer));
324
325 if (!audio_device->IsValid()) {
326 return nullptr;
327 }
328
329 return audio_device;
330 }
331
CreateAudioDeviceWithDataObserver(rtc::scoped_refptr<AudioDeviceModule> impl,AudioDeviceDataObserver * legacy_observer)332 rtc::scoped_refptr<AudioDeviceModule> CreateAudioDeviceWithDataObserver(
333 rtc::scoped_refptr<AudioDeviceModule> impl,
334 AudioDeviceDataObserver* legacy_observer) {
335 auto audio_device =
336 rtc::make_ref_counted<ADMWrapper>(impl, legacy_observer, nullptr);
337
338 if (!audio_device->IsValid()) {
339 return nullptr;
340 }
341
342 return audio_device;
343 }
344
CreateAudioDeviceWithDataObserver(AudioDeviceModule::AudioLayer audio_layer,TaskQueueFactory * task_queue_factory,std::unique_ptr<AudioDeviceDataObserver> observer)345 rtc::scoped_refptr<AudioDeviceModule> CreateAudioDeviceWithDataObserver(
346 AudioDeviceModule::AudioLayer audio_layer,
347 TaskQueueFactory* task_queue_factory,
348 std::unique_ptr<AudioDeviceDataObserver> observer) {
349 auto audio_device = rtc::make_ref_counted<ADMWrapper>(
350 audio_layer, task_queue_factory, observer.get(), std::move(observer));
351
352 if (!audio_device->IsValid()) {
353 return nullptr;
354 }
355
356 return audio_device;
357 }
358
CreateAudioDeviceWithDataObserver(AudioDeviceModule::AudioLayer audio_layer,TaskQueueFactory * task_queue_factory,AudioDeviceDataObserver * legacy_observer)359 rtc::scoped_refptr<AudioDeviceModule> CreateAudioDeviceWithDataObserver(
360 AudioDeviceModule::AudioLayer audio_layer,
361 TaskQueueFactory* task_queue_factory,
362 AudioDeviceDataObserver* legacy_observer) {
363 auto audio_device = rtc::make_ref_counted<ADMWrapper>(
364 audio_layer, task_queue_factory, legacy_observer, nullptr);
365
366 if (!audio_device->IsValid()) {
367 return nullptr;
368 }
369
370 return audio_device;
371 }
372 } // namespace webrtc
373