| OLD | NEW |
| (Empty) | |
| 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 "webrtc/modules/audio_device/include/audio_device_data_observer.h" |
| 12 #include "webrtc/base/checks.h" |
| 13 #include "webrtc/base/refcountedobject.h" |
| 14 |
| 15 namespace webrtc { |
| 16 |
| 17 namespace { |
| 18 |
| 19 // A wrapper over AudioDeviceModule that registers itself as AudioTransport |
| 20 // callback and redirects the PCM data to AudioDeviceDataObserver callback. |
| 21 class ADMWrapper : public AudioDeviceModule, public AudioTransport { |
| 22 public: |
| 23 ADMWrapper(const int32_t id, |
| 24 const AudioLayer audio_layer, |
| 25 AudioDeviceDataObserver* observer) |
| 26 : impl_(AudioDeviceModule::Create(id, audio_layer)), observer_(observer) { |
| 27 // Register self as the audio transport callback for underlying ADM impl. |
| 28 auto res = impl_->RegisterAudioCallback(this); |
| 29 is_valid_ = (impl_.get() != nullptr) && (res == 0); |
| 30 } |
| 31 virtual ~ADMWrapper() { |
| 32 audio_transport_ = nullptr; |
| 33 observer_ = nullptr; |
| 34 } |
| 35 |
| 36 // Make sure we have a valid ADM before returning it to user. |
| 37 bool IsValid() { return is_valid_; } |
| 38 |
| 39 // RefCountedModule methods overrides. |
| 40 int64_t TimeUntilNextProcess() override { |
| 41 return impl_->TimeUntilNextProcess(); |
| 42 } |
| 43 void Process() override { return impl_->Process(); } |
| 44 |
| 45 // AudioTransport methods overrides. |
| 46 int32_t RecordedDataIsAvailable(const void* audioSamples, |
| 47 const size_t nSamples, |
| 48 const size_t nBytesPerSample, |
| 49 const size_t nChannels, |
| 50 const uint32_t samples_per_sec, |
| 51 const uint32_t total_delay_ms, |
| 52 const int32_t clockDrift, |
| 53 const uint32_t currentMicLevel, |
| 54 const bool keyPressed, |
| 55 uint32_t& newMicLevel) override { |
| 56 int32_t res = 0; |
| 57 // Capture PCM data of locally captured audio. |
| 58 if (observer_) { |
| 59 observer_->OnCaptureData(audioSamples, nSamples, nBytesPerSample, |
| 60 nChannels, samples_per_sec); |
| 61 } |
| 62 |
| 63 // Send to the actual audio transport. |
| 64 if (audio_transport_) { |
| 65 res = audio_transport_->RecordedDataIsAvailable( |
| 66 audioSamples, nSamples, nBytesPerSample, nChannels, samples_per_sec, |
| 67 total_delay_ms, clockDrift, currentMicLevel, keyPressed, newMicLevel); |
| 68 } |
| 69 |
| 70 return res; |
| 71 } |
| 72 |
| 73 int32_t NeedMorePlayData(const size_t nSamples, |
| 74 const size_t nBytesPerSample, |
| 75 const size_t nChannels, |
| 76 const uint32_t samples_per_sec, |
| 77 void* audioSamples, |
| 78 size_t& nSamplesOut, |
| 79 int64_t* elapsed_time_ms, |
| 80 int64_t* ntp_time_ms) override { |
| 81 int32_t res = 0; |
| 82 // Request data from audio transport. |
| 83 if (audio_transport_) { |
| 84 res = audio_transport_->NeedMorePlayData( |
| 85 nSamples, nBytesPerSample, nChannels, samples_per_sec, audioSamples, |
| 86 nSamplesOut, elapsed_time_ms, ntp_time_ms); |
| 87 } |
| 88 |
| 89 // Capture rendered data. |
| 90 if (observer_) { |
| 91 observer_->OnRenderData(audioSamples, nSamples, nBytesPerSample, |
| 92 nChannels, samples_per_sec); |
| 93 } |
| 94 |
| 95 return res; |
| 96 } |
| 97 |
| 98 void PushCaptureData(int voe_channel, |
| 99 const void* audio_data, |
| 100 int bits_per_sample, |
| 101 int sample_rate, |
| 102 size_t number_of_channels, |
| 103 size_t number_of_frames) override { |
| 104 RTC_NOTREACHED(); |
| 105 } |
| 106 |
| 107 void PullRenderData(int bits_per_sample, |
| 108 int sample_rate, |
| 109 size_t number_of_channels, |
| 110 size_t number_of_frames, |
| 111 void* audio_data, |
| 112 int64_t* elapsed_time_ms, |
| 113 int64_t* ntp_time_ms) override { |
| 114 RTC_NOTREACHED(); |
| 115 } |
| 116 |
| 117 // Override AudioDeviceModule's RegisterAudioCallback method to remember the |
| 118 // actual audio transport (e.g.: voice engine). |
| 119 int32_t RegisterAudioCallback(AudioTransport* audio_callback) override { |
| 120 // Remember the audio callback to forward PCM data |
| 121 audio_transport_ = audio_callback; |
| 122 return 0; |
| 123 } |
| 124 |
| 125 // AudioDeviceModule pass through method overrides. |
| 126 int32_t ActiveAudioLayer(AudioLayer* audio_layer) const override { |
| 127 return impl_->ActiveAudioLayer(audio_layer); |
| 128 } |
| 129 ErrorCode LastError() const override { return impl_->LastError(); } |
| 130 int32_t RegisterEventObserver(AudioDeviceObserver* event_callback) override { |
| 131 return impl_->RegisterEventObserver(event_callback); |
| 132 } |
| 133 int32_t Init() override { return impl_->Init(); } |
| 134 int32_t Terminate() override { return impl_->Terminate(); } |
| 135 bool Initialized() const override { return impl_->Initialized(); } |
| 136 int16_t PlayoutDevices() override { return impl_->PlayoutDevices(); } |
| 137 int16_t RecordingDevices() override { return impl_->RecordingDevices(); } |
| 138 int32_t PlayoutDeviceName(uint16_t index, |
| 139 char name[kAdmMaxDeviceNameSize], |
| 140 char guid[kAdmMaxGuidSize]) override { |
| 141 return impl_->PlayoutDeviceName(index, name, guid); |
| 142 } |
| 143 int32_t RecordingDeviceName(uint16_t index, |
| 144 char name[kAdmMaxDeviceNameSize], |
| 145 char guid[kAdmMaxGuidSize]) override { |
| 146 return impl_->RecordingDeviceName(index, name, guid); |
| 147 } |
| 148 int32_t SetPlayoutDevice(uint16_t index) override { |
| 149 return impl_->SetPlayoutDevice(index); |
| 150 } |
| 151 int32_t SetPlayoutDevice(WindowsDeviceType device) override { |
| 152 return impl_->SetPlayoutDevice(device); |
| 153 } |
| 154 int32_t SetRecordingDevice(uint16_t index) override { |
| 155 return impl_->SetRecordingDevice(index); |
| 156 } |
| 157 int32_t SetRecordingDevice(WindowsDeviceType device) override { |
| 158 return impl_->SetRecordingDevice(device); |
| 159 } |
| 160 int32_t PlayoutIsAvailable(bool* available) override { |
| 161 return impl_->PlayoutIsAvailable(available); |
| 162 } |
| 163 int32_t InitPlayout() override { return impl_->InitPlayout(); } |
| 164 bool PlayoutIsInitialized() const override { |
| 165 return impl_->PlayoutIsInitialized(); |
| 166 } |
| 167 int32_t RecordingIsAvailable(bool* available) override { |
| 168 return impl_->RecordingIsAvailable(available); |
| 169 } |
| 170 int32_t InitRecording() override { return impl_->InitRecording(); } |
| 171 bool RecordingIsInitialized() const override { |
| 172 return impl_->RecordingIsInitialized(); |
| 173 } |
| 174 int32_t StartPlayout() override { return impl_->StartPlayout(); } |
| 175 int32_t StopPlayout() override { return impl_->StopPlayout(); } |
| 176 bool Playing() const override { return impl_->Playing(); } |
| 177 int32_t StartRecording() override { return impl_->StartRecording(); } |
| 178 int32_t StopRecording() override { return impl_->StopRecording(); } |
| 179 bool Recording() const override { return impl_->Recording(); } |
| 180 int32_t SetAGC(bool enable) override { return impl_->SetAGC(enable); } |
| 181 bool AGC() const override { return impl_->AGC(); } |
| 182 int32_t SetWaveOutVolume(uint16_t volume_left, |
| 183 uint16_t volume_right) override { |
| 184 return impl_->SetWaveOutVolume(volume_left, volume_right); |
| 185 } |
| 186 int32_t WaveOutVolume(uint16_t* volume_left, |
| 187 uint16_t* volume_right) const override { |
| 188 return impl_->WaveOutVolume(volume_left, volume_right); |
| 189 } |
| 190 int32_t InitSpeaker() override { return impl_->InitSpeaker(); } |
| 191 bool SpeakerIsInitialized() const override { |
| 192 return impl_->SpeakerIsInitialized(); |
| 193 } |
| 194 int32_t InitMicrophone() override { return impl_->InitMicrophone(); } |
| 195 bool MicrophoneIsInitialized() const override { |
| 196 return impl_->MicrophoneIsInitialized(); |
| 197 } |
| 198 int32_t SpeakerVolumeIsAvailable(bool* available) override { |
| 199 return impl_->SpeakerVolumeIsAvailable(available); |
| 200 } |
| 201 int32_t SetSpeakerVolume(uint32_t volume) override { |
| 202 return impl_->SetSpeakerVolume(volume); |
| 203 } |
| 204 int32_t SpeakerVolume(uint32_t* volume) const override { |
| 205 return impl_->SpeakerVolume(volume); |
| 206 } |
| 207 int32_t MaxSpeakerVolume(uint32_t* max_volume) const override { |
| 208 return impl_->MaxSpeakerVolume(max_volume); |
| 209 } |
| 210 int32_t MinSpeakerVolume(uint32_t* min_volume) const override { |
| 211 return impl_->MinSpeakerVolume(min_volume); |
| 212 } |
| 213 int32_t SpeakerVolumeStepSize(uint16_t* step_size) const override { |
| 214 return impl_->SpeakerVolumeStepSize(step_size); |
| 215 } |
| 216 int32_t MicrophoneVolumeIsAvailable(bool* available) override { |
| 217 return impl_->MicrophoneVolumeIsAvailable(available); |
| 218 } |
| 219 int32_t SetMicrophoneVolume(uint32_t volume) override { |
| 220 return impl_->SetMicrophoneVolume(volume); |
| 221 } |
| 222 int32_t MicrophoneVolume(uint32_t* volume) const override { |
| 223 return impl_->MicrophoneVolume(volume); |
| 224 } |
| 225 int32_t MaxMicrophoneVolume(uint32_t* max_volume) const override { |
| 226 return impl_->MaxMicrophoneVolume(max_volume); |
| 227 } |
| 228 int32_t MinMicrophoneVolume(uint32_t* min_volume) const override { |
| 229 return impl_->MinMicrophoneVolume(min_volume); |
| 230 } |
| 231 int32_t MicrophoneVolumeStepSize(uint16_t* step_size) const override { |
| 232 return impl_->MicrophoneVolumeStepSize(step_size); |
| 233 } |
| 234 int32_t SpeakerMuteIsAvailable(bool* available) override { |
| 235 return impl_->SpeakerMuteIsAvailable(available); |
| 236 } |
| 237 int32_t SetSpeakerMute(bool enable) override { |
| 238 return impl_->SetSpeakerMute(enable); |
| 239 } |
| 240 int32_t SpeakerMute(bool* enabled) const override { |
| 241 return impl_->SpeakerMute(enabled); |
| 242 } |
| 243 int32_t MicrophoneMuteIsAvailable(bool* available) override { |
| 244 return impl_->MicrophoneMuteIsAvailable(available); |
| 245 } |
| 246 int32_t SetMicrophoneMute(bool enable) override { |
| 247 return impl_->SetMicrophoneMute(enable); |
| 248 } |
| 249 int32_t MicrophoneMute(bool* enabled) const override { |
| 250 return impl_->MicrophoneMute(enabled); |
| 251 } |
| 252 int32_t MicrophoneBoostIsAvailable(bool* available) override { |
| 253 return impl_->MicrophoneBoostIsAvailable(available); |
| 254 } |
| 255 int32_t SetMicrophoneBoost(bool enable) override { |
| 256 return impl_->SetMicrophoneBoost(enable); |
| 257 } |
| 258 int32_t MicrophoneBoost(bool* enabled) const override { |
| 259 return impl_->MicrophoneBoost(enabled); |
| 260 } |
| 261 int32_t StereoPlayoutIsAvailable(bool* available) const override { |
| 262 return impl_->StereoPlayoutIsAvailable(available); |
| 263 } |
| 264 int32_t SetStereoPlayout(bool enable) override { |
| 265 return impl_->SetStereoPlayout(enable); |
| 266 } |
| 267 int32_t StereoPlayout(bool* enabled) const override { |
| 268 return impl_->StereoPlayout(enabled); |
| 269 } |
| 270 int32_t StereoRecordingIsAvailable(bool* available) const override { |
| 271 return impl_->StereoRecordingIsAvailable(available); |
| 272 } |
| 273 int32_t SetStereoRecording(bool enable) override { |
| 274 return impl_->SetStereoRecording(enable); |
| 275 } |
| 276 int32_t StereoRecording(bool* enabled) const override { |
| 277 return impl_->StereoRecording(enabled); |
| 278 } |
| 279 int32_t SetRecordingChannel(const ChannelType channel) override { |
| 280 return impl_->SetRecordingChannel(channel); |
| 281 } |
| 282 int32_t RecordingChannel(ChannelType* channel) const override { |
| 283 return impl_->RecordingChannel(channel); |
| 284 } |
| 285 int32_t SetPlayoutBuffer(const BufferType type, uint16_t size_ms) override { |
| 286 return impl_->SetPlayoutBuffer(type, size_ms); |
| 287 } |
| 288 int32_t PlayoutBuffer(BufferType* type, uint16_t* size_ms) const override { |
| 289 return impl_->PlayoutBuffer(type, size_ms); |
| 290 } |
| 291 int32_t PlayoutDelay(uint16_t* delay_ms) const override { |
| 292 return impl_->PlayoutDelay(delay_ms); |
| 293 } |
| 294 int32_t RecordingDelay(uint16_t* delay_ms) const override { |
| 295 return impl_->RecordingDelay(delay_ms); |
| 296 } |
| 297 int32_t CPULoad(uint16_t* load) const override { |
| 298 return impl_->CPULoad(load); |
| 299 } |
| 300 int32_t StartRawOutputFileRecording( |
| 301 const char pcm_file_name_utf8[kAdmMaxFileNameSize]) override { |
| 302 return impl_->StartRawOutputFileRecording(pcm_file_name_utf8); |
| 303 } |
| 304 int32_t StopRawOutputFileRecording() override { |
| 305 return impl_->StopRawOutputFileRecording(); |
| 306 } |
| 307 int32_t StartRawInputFileRecording( |
| 308 const char pcm_file_name_utf8[kAdmMaxFileNameSize]) override { |
| 309 return impl_->StartRawInputFileRecording(pcm_file_name_utf8); |
| 310 } |
| 311 int32_t StopRawInputFileRecording() override { |
| 312 return impl_->StopRawInputFileRecording(); |
| 313 } |
| 314 int32_t SetRecordingSampleRate(const uint32_t samples_per_sec) override { |
| 315 return impl_->SetRecordingSampleRate(samples_per_sec); |
| 316 } |
| 317 int32_t RecordingSampleRate(uint32_t* samples_per_sec) const override { |
| 318 return impl_->RecordingSampleRate(samples_per_sec); |
| 319 } |
| 320 int32_t SetPlayoutSampleRate(const uint32_t samples_per_sec) override { |
| 321 return impl_->SetPlayoutSampleRate(samples_per_sec); |
| 322 } |
| 323 int32_t PlayoutSampleRate(uint32_t* samples_per_sec) const override { |
| 324 return impl_->PlayoutSampleRate(samples_per_sec); |
| 325 } |
| 326 int32_t ResetAudioDevice() override { return impl_->ResetAudioDevice(); } |
| 327 int32_t SetLoudspeakerStatus(bool enable) override { |
| 328 return impl_->SetLoudspeakerStatus(enable); |
| 329 } |
| 330 int32_t GetLoudspeakerStatus(bool* enabled) const override { |
| 331 return impl_->GetLoudspeakerStatus(enabled); |
| 332 } |
| 333 bool BuiltInAECIsAvailable() const override { |
| 334 return impl_->BuiltInAECIsAvailable(); |
| 335 } |
| 336 bool BuiltInAGCIsAvailable() const override { |
| 337 return impl_->BuiltInAGCIsAvailable(); |
| 338 } |
| 339 bool BuiltInNSIsAvailable() const override { |
| 340 return impl_->BuiltInNSIsAvailable(); |
| 341 } |
| 342 int32_t EnableBuiltInAEC(bool enable) override { |
| 343 return impl_->EnableBuiltInAEC(enable); |
| 344 } |
| 345 int32_t EnableBuiltInAGC(bool enable) override { |
| 346 return impl_->EnableBuiltInAGC(enable); |
| 347 } |
| 348 int32_t EnableBuiltInNS(bool enable) override { |
| 349 return impl_->EnableBuiltInNS(enable); |
| 350 } |
| 351 // Only supported on iOS. |
| 352 #if defined(WEBRTC_IOS) |
| 353 int GetPlayoutAudioParameters(AudioParameters* params) const override { |
| 354 return impl_->GetPlayoutAudioParameters(params); |
| 355 } |
| 356 int GetRecordAudioParameters(AudioParameters* params) const override { |
| 357 return impl_->GetRecordAudioParameters(params); |
| 358 } |
| 359 #endif // WEBRTC_IOS |
| 360 |
| 361 protected: |
| 362 rtc::scoped_refptr<AudioDeviceModule> impl_; |
| 363 AudioDeviceDataObserver* observer_ = nullptr; |
| 364 AudioTransport* audio_transport_ = nullptr; |
| 365 bool is_valid_ = false; |
| 366 }; |
| 367 |
| 368 } // namespace |
| 369 |
| 370 rtc::scoped_refptr<AudioDeviceModule> CreateAudioDeviceWithDataObserver( |
| 371 const int32_t id, |
| 372 const AudioDeviceModule::AudioLayer audio_layer, |
| 373 AudioDeviceDataObserver* observer) { |
| 374 rtc::scoped_refptr<ADMWrapper> audio_device( |
| 375 new rtc::RefCountedObject<ADMWrapper>(id, audio_layer, observer)); |
| 376 |
| 377 if (!audio_device->IsValid()) { |
| 378 return nullptr; |
| 379 } |
| 380 |
| 381 return audio_device; |
| 382 } |
| 383 |
| 384 } // namespace webrtc |
| OLD | NEW |