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 "modules/audio_device/include/audio_device_defines.h"
14 #include "rtc_base/checks.h"
15 #include "rtc_base/ref_counted_object.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
48 // AudioTransport methods overrides.
RecordedDataIsAvailable(const void * audioSamples,const size_t nSamples,const size_t nBytesPerSample,const size_t nChannels,const uint32_t samples_per_sec,const uint32_t total_delay_ms,const int32_t clockDrift,const uint32_t currentMicLevel,const bool keyPressed,uint32_t & newMicLevel)49 int32_t RecordedDataIsAvailable(const void* audioSamples,
50 const size_t nSamples,
51 const size_t nBytesPerSample,
52 const size_t nChannels,
53 const uint32_t samples_per_sec,
54 const uint32_t total_delay_ms,
55 const int32_t clockDrift,
56 const uint32_t currentMicLevel,
57 const bool keyPressed,
58 uint32_t& newMicLevel) override {
59 int32_t res = 0;
60 // Capture PCM data of locally captured audio.
61 if (observer_) {
62 observer_->OnCaptureData(audioSamples, nSamples, nBytesPerSample,
63 nChannels, samples_per_sec);
64 }
65
66 // Send to the actual audio transport.
67 if (audio_transport_) {
68 res = audio_transport_->RecordedDataIsAvailable(
69 audioSamples, nSamples, nBytesPerSample, nChannels, samples_per_sec,
70 total_delay_ms, clockDrift, currentMicLevel, keyPressed, newMicLevel);
71 }
72
73 return res;
74 }
75
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)76 int32_t NeedMorePlayData(const size_t nSamples,
77 const size_t nBytesPerSample,
78 const size_t nChannels,
79 const uint32_t samples_per_sec,
80 void* audioSamples,
81 size_t& nSamplesOut,
82 int64_t* elapsed_time_ms,
83 int64_t* ntp_time_ms) override {
84 int32_t res = 0;
85 // Set out parameters to safe values to be sure not to return corrupted
86 // data.
87 nSamplesOut = 0;
88 *elapsed_time_ms = -1;
89 *ntp_time_ms = -1;
90 // Request data from audio transport.
91 if (audio_transport_) {
92 res = audio_transport_->NeedMorePlayData(
93 nSamples, nBytesPerSample, nChannels, samples_per_sec, audioSamples,
94 nSamplesOut, elapsed_time_ms, ntp_time_ms);
95 }
96
97 // Capture rendered data.
98 if (observer_) {
99 observer_->OnRenderData(audioSamples, nSamples, nBytesPerSample,
100 nChannels, samples_per_sec);
101 }
102
103 return res;
104 }
105
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)106 void PullRenderData(int bits_per_sample,
107 int sample_rate,
108 size_t number_of_channels,
109 size_t number_of_frames,
110 void* audio_data,
111 int64_t* elapsed_time_ms,
112 int64_t* ntp_time_ms) override {
113 RTC_NOTREACHED();
114 }
115
116 // Override AudioDeviceModule's RegisterAudioCallback method to remember the
117 // actual audio transport (e.g.: voice engine).
RegisterAudioCallback(AudioTransport * audio_callback)118 int32_t RegisterAudioCallback(AudioTransport* audio_callback) override {
119 // Remember the audio callback to forward PCM data
120 audio_transport_ = audio_callback;
121 return 0;
122 }
123
124 // AudioDeviceModule pass through method overrides.
ActiveAudioLayer(AudioLayer * audio_layer) const125 int32_t ActiveAudioLayer(AudioLayer* audio_layer) const override {
126 return impl_->ActiveAudioLayer(audio_layer);
127 }
Init()128 int32_t Init() override {
129 int res = impl_->Init();
130 if (res != 0) {
131 return res;
132 }
133 // Register self as the audio transport callback for underlying ADM impl.
134 impl_->RegisterAudioCallback(this);
135 return res;
136 }
Terminate()137 int32_t Terminate() override { return impl_->Terminate(); }
Initialized() const138 bool Initialized() const override { return impl_->Initialized(); }
PlayoutDevices()139 int16_t PlayoutDevices() override { return impl_->PlayoutDevices(); }
RecordingDevices()140 int16_t RecordingDevices() override { return impl_->RecordingDevices(); }
PlayoutDeviceName(uint16_t index,char name[kAdmMaxDeviceNameSize],char guid[kAdmMaxGuidSize])141 int32_t PlayoutDeviceName(uint16_t index,
142 char name[kAdmMaxDeviceNameSize],
143 char guid[kAdmMaxGuidSize]) override {
144 return impl_->PlayoutDeviceName(index, name, guid);
145 }
RecordingDeviceName(uint16_t index,char name[kAdmMaxDeviceNameSize],char guid[kAdmMaxGuidSize])146 int32_t RecordingDeviceName(uint16_t index,
147 char name[kAdmMaxDeviceNameSize],
148 char guid[kAdmMaxGuidSize]) override {
149 return impl_->RecordingDeviceName(index, name, guid);
150 }
SetPlayoutDevice(uint16_t index)151 int32_t SetPlayoutDevice(uint16_t index) override {
152 return impl_->SetPlayoutDevice(index);
153 }
SetPlayoutDevice(WindowsDeviceType device)154 int32_t SetPlayoutDevice(WindowsDeviceType device) override {
155 return impl_->SetPlayoutDevice(device);
156 }
SetRecordingDevice(uint16_t index)157 int32_t SetRecordingDevice(uint16_t index) override {
158 return impl_->SetRecordingDevice(index);
159 }
SetRecordingDevice(WindowsDeviceType device)160 int32_t SetRecordingDevice(WindowsDeviceType device) override {
161 return impl_->SetRecordingDevice(device);
162 }
PlayoutIsAvailable(bool * available)163 int32_t PlayoutIsAvailable(bool* available) override {
164 return impl_->PlayoutIsAvailable(available);
165 }
InitPlayout()166 int32_t InitPlayout() override { return impl_->InitPlayout(); }
PlayoutIsInitialized() const167 bool PlayoutIsInitialized() const override {
168 return impl_->PlayoutIsInitialized();
169 }
RecordingIsAvailable(bool * available)170 int32_t RecordingIsAvailable(bool* available) override {
171 return impl_->RecordingIsAvailable(available);
172 }
InitRecording()173 int32_t InitRecording() override { return impl_->InitRecording(); }
RecordingIsInitialized() const174 bool RecordingIsInitialized() const override {
175 return impl_->RecordingIsInitialized();
176 }
StartPlayout()177 int32_t StartPlayout() override { return impl_->StartPlayout(); }
StopPlayout()178 int32_t StopPlayout() override { return impl_->StopPlayout(); }
Playing() const179 bool Playing() const override { return impl_->Playing(); }
StartRecording()180 int32_t StartRecording() override { return impl_->StartRecording(); }
StopRecording()181 int32_t StopRecording() override { return impl_->StopRecording(); }
Recording() const182 bool Recording() const override { return impl_->Recording(); }
InitSpeaker()183 int32_t InitSpeaker() override { return impl_->InitSpeaker(); }
SpeakerIsInitialized() const184 bool SpeakerIsInitialized() const override {
185 return impl_->SpeakerIsInitialized();
186 }
InitMicrophone()187 int32_t InitMicrophone() override { return impl_->InitMicrophone(); }
MicrophoneIsInitialized() const188 bool MicrophoneIsInitialized() const override {
189 return impl_->MicrophoneIsInitialized();
190 }
SpeakerVolumeIsAvailable(bool * available)191 int32_t SpeakerVolumeIsAvailable(bool* available) override {
192 return impl_->SpeakerVolumeIsAvailable(available);
193 }
SetSpeakerVolume(uint32_t volume)194 int32_t SetSpeakerVolume(uint32_t volume) override {
195 return impl_->SetSpeakerVolume(volume);
196 }
SpeakerVolume(uint32_t * volume) const197 int32_t SpeakerVolume(uint32_t* volume) const override {
198 return impl_->SpeakerVolume(volume);
199 }
MaxSpeakerVolume(uint32_t * max_volume) const200 int32_t MaxSpeakerVolume(uint32_t* max_volume) const override {
201 return impl_->MaxSpeakerVolume(max_volume);
202 }
MinSpeakerVolume(uint32_t * min_volume) const203 int32_t MinSpeakerVolume(uint32_t* min_volume) const override {
204 return impl_->MinSpeakerVolume(min_volume);
205 }
MicrophoneVolumeIsAvailable(bool * available)206 int32_t MicrophoneVolumeIsAvailable(bool* available) override {
207 return impl_->MicrophoneVolumeIsAvailable(available);
208 }
SetMicrophoneVolume(uint32_t volume)209 int32_t SetMicrophoneVolume(uint32_t volume) override {
210 return impl_->SetMicrophoneVolume(volume);
211 }
MicrophoneVolume(uint32_t * volume) const212 int32_t MicrophoneVolume(uint32_t* volume) const override {
213 return impl_->MicrophoneVolume(volume);
214 }
MaxMicrophoneVolume(uint32_t * max_volume) const215 int32_t MaxMicrophoneVolume(uint32_t* max_volume) const override {
216 return impl_->MaxMicrophoneVolume(max_volume);
217 }
MinMicrophoneVolume(uint32_t * min_volume) const218 int32_t MinMicrophoneVolume(uint32_t* min_volume) const override {
219 return impl_->MinMicrophoneVolume(min_volume);
220 }
SpeakerMuteIsAvailable(bool * available)221 int32_t SpeakerMuteIsAvailable(bool* available) override {
222 return impl_->SpeakerMuteIsAvailable(available);
223 }
SetSpeakerMute(bool enable)224 int32_t SetSpeakerMute(bool enable) override {
225 return impl_->SetSpeakerMute(enable);
226 }
SpeakerMute(bool * enabled) const227 int32_t SpeakerMute(bool* enabled) const override {
228 return impl_->SpeakerMute(enabled);
229 }
MicrophoneMuteIsAvailable(bool * available)230 int32_t MicrophoneMuteIsAvailable(bool* available) override {
231 return impl_->MicrophoneMuteIsAvailable(available);
232 }
SetMicrophoneMute(bool enable)233 int32_t SetMicrophoneMute(bool enable) override {
234 return impl_->SetMicrophoneMute(enable);
235 }
MicrophoneMute(bool * enabled) const236 int32_t MicrophoneMute(bool* enabled) const override {
237 return impl_->MicrophoneMute(enabled);
238 }
StereoPlayoutIsAvailable(bool * available) const239 int32_t StereoPlayoutIsAvailable(bool* available) const override {
240 return impl_->StereoPlayoutIsAvailable(available);
241 }
SetStereoPlayout(bool enable)242 int32_t SetStereoPlayout(bool enable) override {
243 return impl_->SetStereoPlayout(enable);
244 }
StereoPlayout(bool * enabled) const245 int32_t StereoPlayout(bool* enabled) const override {
246 return impl_->StereoPlayout(enabled);
247 }
StereoRecordingIsAvailable(bool * available) const248 int32_t StereoRecordingIsAvailable(bool* available) const override {
249 return impl_->StereoRecordingIsAvailable(available);
250 }
SetStereoRecording(bool enable)251 int32_t SetStereoRecording(bool enable) override {
252 return impl_->SetStereoRecording(enable);
253 }
StereoRecording(bool * enabled) const254 int32_t StereoRecording(bool* enabled) const override {
255 return impl_->StereoRecording(enabled);
256 }
PlayoutDelay(uint16_t * delay_ms) const257 int32_t PlayoutDelay(uint16_t* delay_ms) const override {
258 return impl_->PlayoutDelay(delay_ms);
259 }
BuiltInAECIsAvailable() const260 bool BuiltInAECIsAvailable() const override {
261 return impl_->BuiltInAECIsAvailable();
262 }
BuiltInAGCIsAvailable() const263 bool BuiltInAGCIsAvailable() const override {
264 return impl_->BuiltInAGCIsAvailable();
265 }
BuiltInNSIsAvailable() const266 bool BuiltInNSIsAvailable() const override {
267 return impl_->BuiltInNSIsAvailable();
268 }
EnableBuiltInAEC(bool enable)269 int32_t EnableBuiltInAEC(bool enable) override {
270 return impl_->EnableBuiltInAEC(enable);
271 }
EnableBuiltInAGC(bool enable)272 int32_t EnableBuiltInAGC(bool enable) override {
273 return impl_->EnableBuiltInAGC(enable);
274 }
EnableBuiltInNS(bool enable)275 int32_t EnableBuiltInNS(bool enable) override {
276 return impl_->EnableBuiltInNS(enable);
277 }
GetPlayoutUnderrunCount() const278 int32_t GetPlayoutUnderrunCount() const override {
279 return impl_->GetPlayoutUnderrunCount();
280 }
281 // Only supported on iOS.
282 #if defined(WEBRTC_IOS)
GetPlayoutAudioParameters(AudioParameters * params) const283 int GetPlayoutAudioParameters(AudioParameters* params) const override {
284 return impl_->GetPlayoutAudioParameters(params);
285 }
GetRecordAudioParameters(AudioParameters * params) const286 int GetRecordAudioParameters(AudioParameters* params) const override {
287 return impl_->GetRecordAudioParameters(params);
288 }
289 #endif // WEBRTC_IOS
290
291 protected:
292 rtc::scoped_refptr<AudioDeviceModule> impl_;
293 AudioDeviceDataObserver* legacy_observer_ = nullptr;
294 std::unique_ptr<AudioDeviceDataObserver> observer_;
295 AudioTransport* audio_transport_ = nullptr;
296 bool is_valid_ = false;
297 };
298
299 } // namespace
300
CreateAudioDeviceWithDataObserver(rtc::scoped_refptr<AudioDeviceModule> impl,std::unique_ptr<AudioDeviceDataObserver> observer)301 rtc::scoped_refptr<AudioDeviceModule> CreateAudioDeviceWithDataObserver(
302 rtc::scoped_refptr<AudioDeviceModule> impl,
303 std::unique_ptr<AudioDeviceDataObserver> observer) {
304 rtc::scoped_refptr<ADMWrapper> audio_device(
305 new rtc::RefCountedObject<ADMWrapper>(impl, observer.get(),
306 std::move(observer)));
307
308 if (!audio_device->IsValid()) {
309 return nullptr;
310 }
311
312 return audio_device;
313 }
314
CreateAudioDeviceWithDataObserver(rtc::scoped_refptr<AudioDeviceModule> impl,AudioDeviceDataObserver * legacy_observer)315 rtc::scoped_refptr<AudioDeviceModule> CreateAudioDeviceWithDataObserver(
316 rtc::scoped_refptr<AudioDeviceModule> impl,
317 AudioDeviceDataObserver* legacy_observer) {
318 rtc::scoped_refptr<ADMWrapper> audio_device(
319 new rtc::RefCountedObject<ADMWrapper>(impl, legacy_observer, nullptr));
320
321 if (!audio_device->IsValid()) {
322 return nullptr;
323 }
324
325 return audio_device;
326 }
327
CreateAudioDeviceWithDataObserver(AudioDeviceModule::AudioLayer audio_layer,TaskQueueFactory * task_queue_factory,std::unique_ptr<AudioDeviceDataObserver> observer)328 rtc::scoped_refptr<AudioDeviceModule> CreateAudioDeviceWithDataObserver(
329 AudioDeviceModule::AudioLayer audio_layer,
330 TaskQueueFactory* task_queue_factory,
331 std::unique_ptr<AudioDeviceDataObserver> observer) {
332 rtc::scoped_refptr<ADMWrapper> audio_device(
333 new rtc::RefCountedObject<ADMWrapper>(audio_layer, task_queue_factory,
334 observer.get(),
335 std::move(observer)));
336
337 if (!audio_device->IsValid()) {
338 return nullptr;
339 }
340
341 return audio_device;
342 }
343
CreateAudioDeviceWithDataObserver(AudioDeviceModule::AudioLayer audio_layer,TaskQueueFactory * task_queue_factory,AudioDeviceDataObserver * legacy_observer)344 rtc::scoped_refptr<AudioDeviceModule> CreateAudioDeviceWithDataObserver(
345 AudioDeviceModule::AudioLayer audio_layer,
346 TaskQueueFactory* task_queue_factory,
347 AudioDeviceDataObserver* legacy_observer) {
348 rtc::scoped_refptr<ADMWrapper> audio_device(
349 new rtc::RefCountedObject<ADMWrapper>(audio_layer, task_queue_factory,
350 legacy_observer, nullptr));
351
352 if (!audio_device->IsValid()) {
353 return nullptr;
354 }
355
356 return audio_device;
357 }
358 } // namespace webrtc
359