Lu Liu | 4b62001 | 2017-04-06 10:17:01 -0700 | [diff] [blame] | 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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "modules/audio_device/include/audio_device_data_observer.h" |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 12 | |
| 13 | #include "modules/audio_device/include/audio_device_defines.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 14 | #include "rtc_base/checks.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 15 | #include "rtc_base/ref_counted_object.h" |
Lu Liu | 4b62001 | 2017-04-06 10:17:01 -0700 | [diff] [blame] | 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: |
Fabian Bergmark | 575c2ad | 2020-02-18 14:23:36 +0100 | [diff] [blame] | 25 | ADMWrapper(rtc::scoped_refptr<AudioDeviceModule> impl, |
Fabian Bergmark | 9a4eb32 | 2020-02-20 14:22:48 +0100 | [diff] [blame] | 26 | AudioDeviceDataObserver* legacy_observer, |
| 27 | std::unique_ptr<AudioDeviceDataObserver> observer) |
| 28 | : impl_(impl), |
| 29 | legacy_observer_(legacy_observer), |
| 30 | observer_(std::move(observer)) { |
Fabian Bergmark | 575c2ad | 2020-02-18 14:23:36 +0100 | [diff] [blame] | 31 | is_valid_ = impl_.get() != nullptr; |
| 32 | } |
Danil Chapovalov | 1c41be6 | 2019-04-01 09:16:12 +0200 | [diff] [blame] | 33 | ADMWrapper(AudioLayer audio_layer, |
| 34 | TaskQueueFactory* task_queue_factory, |
Fabian Bergmark | 9a4eb32 | 2020-02-20 14:22:48 +0100 | [diff] [blame] | 35 | AudioDeviceDataObserver* legacy_observer, |
| 36 | std::unique_ptr<AudioDeviceDataObserver> observer) |
Fabian Bergmark | 575c2ad | 2020-02-18 14:23:36 +0100 | [diff] [blame] | 37 | : ADMWrapper(AudioDeviceModule::Create(audio_layer, task_queue_factory), |
Fabian Bergmark | 9a4eb32 | 2020-02-20 14:22:48 +0100 | [diff] [blame] | 38 | legacy_observer, |
| 39 | std::move(observer)) {} |
Mirko Bonadei | fe055c1 | 2019-01-29 22:53:28 +0100 | [diff] [blame] | 40 | ~ADMWrapper() override { |
Lu Liu | 4b62001 | 2017-04-06 10:17:01 -0700 | [diff] [blame] | 41 | audio_transport_ = nullptr; |
| 42 | observer_ = nullptr; |
| 43 | } |
| 44 | |
| 45 | // Make sure we have a valid ADM before returning it to user. |
| 46 | bool IsValid() { return is_valid_; } |
| 47 | |
Lu Liu | 4b62001 | 2017-04-06 10:17:01 -0700 | [diff] [blame] | 48 | // AudioTransport methods overrides. |
| 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 | |
| 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; |
Artem Titov | ac9365e | 2018-03-28 15:21:54 +0200 | [diff] [blame] | 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; |
Lu Liu | 4b62001 | 2017-04-06 10:17:01 -0700 | [diff] [blame] | 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 | |
Lu Liu | 4b62001 | 2017-04-06 10:17:01 -0700 | [diff] [blame] | 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). |
| 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. |
| 125 | int32_t ActiveAudioLayer(AudioLayer* audio_layer) const override { |
| 126 | return impl_->ActiveAudioLayer(audio_layer); |
| 127 | } |
Fabian Bergmark | 575c2ad | 2020-02-18 14:23:36 +0100 | [diff] [blame] | 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 | } |
Lu Liu | 4b62001 | 2017-04-06 10:17:01 -0700 | [diff] [blame] | 137 | int32_t Terminate() override { return impl_->Terminate(); } |
| 138 | bool Initialized() const override { return impl_->Initialized(); } |
| 139 | int16_t PlayoutDevices() override { return impl_->PlayoutDevices(); } |
| 140 | int16_t RecordingDevices() override { return impl_->RecordingDevices(); } |
| 141 | int32_t PlayoutDeviceName(uint16_t index, |
| 142 | char name[kAdmMaxDeviceNameSize], |
| 143 | char guid[kAdmMaxGuidSize]) override { |
| 144 | return impl_->PlayoutDeviceName(index, name, guid); |
| 145 | } |
| 146 | int32_t RecordingDeviceName(uint16_t index, |
| 147 | char name[kAdmMaxDeviceNameSize], |
| 148 | char guid[kAdmMaxGuidSize]) override { |
| 149 | return impl_->RecordingDeviceName(index, name, guid); |
| 150 | } |
| 151 | int32_t SetPlayoutDevice(uint16_t index) override { |
| 152 | return impl_->SetPlayoutDevice(index); |
| 153 | } |
| 154 | int32_t SetPlayoutDevice(WindowsDeviceType device) override { |
| 155 | return impl_->SetPlayoutDevice(device); |
| 156 | } |
| 157 | int32_t SetRecordingDevice(uint16_t index) override { |
| 158 | return impl_->SetRecordingDevice(index); |
| 159 | } |
| 160 | int32_t SetRecordingDevice(WindowsDeviceType device) override { |
| 161 | return impl_->SetRecordingDevice(device); |
| 162 | } |
| 163 | int32_t PlayoutIsAvailable(bool* available) override { |
| 164 | return impl_->PlayoutIsAvailable(available); |
| 165 | } |
| 166 | int32_t InitPlayout() override { return impl_->InitPlayout(); } |
| 167 | bool PlayoutIsInitialized() const override { |
| 168 | return impl_->PlayoutIsInitialized(); |
| 169 | } |
| 170 | int32_t RecordingIsAvailable(bool* available) override { |
| 171 | return impl_->RecordingIsAvailable(available); |
| 172 | } |
| 173 | int32_t InitRecording() override { return impl_->InitRecording(); } |
| 174 | bool RecordingIsInitialized() const override { |
| 175 | return impl_->RecordingIsInitialized(); |
| 176 | } |
| 177 | int32_t StartPlayout() override { return impl_->StartPlayout(); } |
| 178 | int32_t StopPlayout() override { return impl_->StopPlayout(); } |
| 179 | bool Playing() const override { return impl_->Playing(); } |
| 180 | int32_t StartRecording() override { return impl_->StartRecording(); } |
| 181 | int32_t StopRecording() override { return impl_->StopRecording(); } |
| 182 | bool Recording() const override { return impl_->Recording(); } |
Lu Liu | 4b62001 | 2017-04-06 10:17:01 -0700 | [diff] [blame] | 183 | int32_t InitSpeaker() override { return impl_->InitSpeaker(); } |
| 184 | bool SpeakerIsInitialized() const override { |
| 185 | return impl_->SpeakerIsInitialized(); |
| 186 | } |
| 187 | int32_t InitMicrophone() override { return impl_->InitMicrophone(); } |
| 188 | bool MicrophoneIsInitialized() const override { |
| 189 | return impl_->MicrophoneIsInitialized(); |
| 190 | } |
| 191 | int32_t SpeakerVolumeIsAvailable(bool* available) override { |
| 192 | return impl_->SpeakerVolumeIsAvailable(available); |
| 193 | } |
| 194 | int32_t SetSpeakerVolume(uint32_t volume) override { |
| 195 | return impl_->SetSpeakerVolume(volume); |
| 196 | } |
| 197 | int32_t SpeakerVolume(uint32_t* volume) const override { |
| 198 | return impl_->SpeakerVolume(volume); |
| 199 | } |
| 200 | int32_t MaxSpeakerVolume(uint32_t* max_volume) const override { |
| 201 | return impl_->MaxSpeakerVolume(max_volume); |
| 202 | } |
| 203 | int32_t MinSpeakerVolume(uint32_t* min_volume) const override { |
| 204 | return impl_->MinSpeakerVolume(min_volume); |
| 205 | } |
Lu Liu | 4b62001 | 2017-04-06 10:17:01 -0700 | [diff] [blame] | 206 | int32_t MicrophoneVolumeIsAvailable(bool* available) override { |
| 207 | return impl_->MicrophoneVolumeIsAvailable(available); |
| 208 | } |
| 209 | int32_t SetMicrophoneVolume(uint32_t volume) override { |
| 210 | return impl_->SetMicrophoneVolume(volume); |
| 211 | } |
| 212 | int32_t MicrophoneVolume(uint32_t* volume) const override { |
| 213 | return impl_->MicrophoneVolume(volume); |
| 214 | } |
| 215 | int32_t MaxMicrophoneVolume(uint32_t* max_volume) const override { |
| 216 | return impl_->MaxMicrophoneVolume(max_volume); |
| 217 | } |
| 218 | int32_t MinMicrophoneVolume(uint32_t* min_volume) const override { |
| 219 | return impl_->MinMicrophoneVolume(min_volume); |
| 220 | } |
Lu Liu | 4b62001 | 2017-04-06 10:17:01 -0700 | [diff] [blame] | 221 | int32_t SpeakerMuteIsAvailable(bool* available) override { |
| 222 | return impl_->SpeakerMuteIsAvailable(available); |
| 223 | } |
| 224 | int32_t SetSpeakerMute(bool enable) override { |
| 225 | return impl_->SetSpeakerMute(enable); |
| 226 | } |
| 227 | int32_t SpeakerMute(bool* enabled) const override { |
| 228 | return impl_->SpeakerMute(enabled); |
| 229 | } |
| 230 | int32_t MicrophoneMuteIsAvailable(bool* available) override { |
| 231 | return impl_->MicrophoneMuteIsAvailable(available); |
| 232 | } |
| 233 | int32_t SetMicrophoneMute(bool enable) override { |
| 234 | return impl_->SetMicrophoneMute(enable); |
| 235 | } |
| 236 | int32_t MicrophoneMute(bool* enabled) const override { |
| 237 | return impl_->MicrophoneMute(enabled); |
| 238 | } |
Lu Liu | 4b62001 | 2017-04-06 10:17:01 -0700 | [diff] [blame] | 239 | int32_t StereoPlayoutIsAvailable(bool* available) const override { |
| 240 | return impl_->StereoPlayoutIsAvailable(available); |
| 241 | } |
| 242 | int32_t SetStereoPlayout(bool enable) override { |
| 243 | return impl_->SetStereoPlayout(enable); |
| 244 | } |
| 245 | int32_t StereoPlayout(bool* enabled) const override { |
| 246 | return impl_->StereoPlayout(enabled); |
| 247 | } |
| 248 | int32_t StereoRecordingIsAvailable(bool* available) const override { |
| 249 | return impl_->StereoRecordingIsAvailable(available); |
| 250 | } |
| 251 | int32_t SetStereoRecording(bool enable) override { |
| 252 | return impl_->SetStereoRecording(enable); |
| 253 | } |
| 254 | int32_t StereoRecording(bool* enabled) const override { |
| 255 | return impl_->StereoRecording(enabled); |
| 256 | } |
Lu Liu | 4b62001 | 2017-04-06 10:17:01 -0700 | [diff] [blame] | 257 | int32_t PlayoutDelay(uint16_t* delay_ms) const override { |
| 258 | return impl_->PlayoutDelay(delay_ms); |
| 259 | } |
Lu Liu | 4b62001 | 2017-04-06 10:17:01 -0700 | [diff] [blame] | 260 | bool BuiltInAECIsAvailable() const override { |
| 261 | return impl_->BuiltInAECIsAvailable(); |
| 262 | } |
| 263 | bool BuiltInAGCIsAvailable() const override { |
| 264 | return impl_->BuiltInAGCIsAvailable(); |
| 265 | } |
| 266 | bool BuiltInNSIsAvailable() const override { |
| 267 | return impl_->BuiltInNSIsAvailable(); |
| 268 | } |
| 269 | int32_t EnableBuiltInAEC(bool enable) override { |
| 270 | return impl_->EnableBuiltInAEC(enable); |
| 271 | } |
| 272 | int32_t EnableBuiltInAGC(bool enable) override { |
| 273 | return impl_->EnableBuiltInAGC(enable); |
| 274 | } |
| 275 | int32_t EnableBuiltInNS(bool enable) override { |
| 276 | return impl_->EnableBuiltInNS(enable); |
| 277 | } |
Alex Narest | bbeb109 | 2019-08-16 11:49:04 +0200 | [diff] [blame] | 278 | int32_t GetPlayoutUnderrunCount() const override { |
| 279 | return impl_->GetPlayoutUnderrunCount(); |
| 280 | } |
Lu Liu | 4b62001 | 2017-04-06 10:17:01 -0700 | [diff] [blame] | 281 | // Only supported on iOS. |
| 282 | #if defined(WEBRTC_IOS) |
| 283 | int GetPlayoutAudioParameters(AudioParameters* params) const override { |
| 284 | return impl_->GetPlayoutAudioParameters(params); |
| 285 | } |
| 286 | 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_; |
Fabian Bergmark | 9a4eb32 | 2020-02-20 14:22:48 +0100 | [diff] [blame] | 293 | AudioDeviceDataObserver* legacy_observer_ = nullptr; |
| 294 | std::unique_ptr<AudioDeviceDataObserver> observer_; |
Lu Liu | 4b62001 | 2017-04-06 10:17:01 -0700 | [diff] [blame] | 295 | AudioTransport* audio_transport_ = nullptr; |
| 296 | bool is_valid_ = false; |
| 297 | }; |
| 298 | |
| 299 | } // namespace |
| 300 | |
| 301 | rtc::scoped_refptr<AudioDeviceModule> CreateAudioDeviceWithDataObserver( |
Fabian Bergmark | 575c2ad | 2020-02-18 14:23:36 +0100 | [diff] [blame] | 302 | rtc::scoped_refptr<AudioDeviceModule> impl, |
Fabian Bergmark | 9a4eb32 | 2020-02-20 14:22:48 +0100 | [diff] [blame] | 303 | std::unique_ptr<AudioDeviceDataObserver> observer) { |
Tommi | 87f7090 | 2021-04-27 14:43:08 +0200 | [diff] [blame] | 304 | auto audio_device = rtc::make_ref_counted<ADMWrapper>(impl, observer.get(), |
| 305 | std::move(observer)); |
Fabian Bergmark | 9a4eb32 | 2020-02-20 14:22:48 +0100 | [diff] [blame] | 306 | |
| 307 | if (!audio_device->IsValid()) { |
| 308 | return nullptr; |
| 309 | } |
| 310 | |
| 311 | return audio_device; |
| 312 | } |
| 313 | |
| 314 | rtc::scoped_refptr<AudioDeviceModule> CreateAudioDeviceWithDataObserver( |
| 315 | rtc::scoped_refptr<AudioDeviceModule> impl, |
| 316 | AudioDeviceDataObserver* legacy_observer) { |
Tommi | 87f7090 | 2021-04-27 14:43:08 +0200 | [diff] [blame] | 317 | auto audio_device = |
| 318 | rtc::make_ref_counted<ADMWrapper>(impl, legacy_observer, nullptr); |
Fabian Bergmark | 575c2ad | 2020-02-18 14:23:36 +0100 | [diff] [blame] | 319 | |
| 320 | if (!audio_device->IsValid()) { |
| 321 | return nullptr; |
| 322 | } |
| 323 | |
| 324 | return audio_device; |
| 325 | } |
| 326 | |
| 327 | rtc::scoped_refptr<AudioDeviceModule> CreateAudioDeviceWithDataObserver( |
Danil Chapovalov | 1c41be6 | 2019-04-01 09:16:12 +0200 | [diff] [blame] | 328 | AudioDeviceModule::AudioLayer audio_layer, |
| 329 | TaskQueueFactory* task_queue_factory, |
Fabian Bergmark | 9a4eb32 | 2020-02-20 14:22:48 +0100 | [diff] [blame] | 330 | std::unique_ptr<AudioDeviceDataObserver> observer) { |
Tommi | 87f7090 | 2021-04-27 14:43:08 +0200 | [diff] [blame] | 331 | auto audio_device = rtc::make_ref_counted<ADMWrapper>( |
| 332 | audio_layer, task_queue_factory, observer.get(), std::move(observer)); |
Fabian Bergmark | 9a4eb32 | 2020-02-20 14:22:48 +0100 | [diff] [blame] | 333 | |
| 334 | if (!audio_device->IsValid()) { |
| 335 | return nullptr; |
| 336 | } |
| 337 | |
| 338 | return audio_device; |
| 339 | } |
| 340 | |
| 341 | rtc::scoped_refptr<AudioDeviceModule> CreateAudioDeviceWithDataObserver( |
| 342 | AudioDeviceModule::AudioLayer audio_layer, |
| 343 | TaskQueueFactory* task_queue_factory, |
| 344 | AudioDeviceDataObserver* legacy_observer) { |
Tommi | 87f7090 | 2021-04-27 14:43:08 +0200 | [diff] [blame] | 345 | auto audio_device = rtc::make_ref_counted<ADMWrapper>( |
| 346 | audio_layer, task_queue_factory, legacy_observer, nullptr); |
Lu Liu | 4b62001 | 2017-04-06 10:17:01 -0700 | [diff] [blame] | 347 | |
| 348 | if (!audio_device->IsValid()) { |
| 349 | return nullptr; |
| 350 | } |
| 351 | |
| 352 | return audio_device; |
| 353 | } |
Lu Liu | 4b62001 | 2017-04-06 10:17:01 -0700 | [diff] [blame] | 354 | } // namespace webrtc |