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