audio_device_impl.cc (27593B)
1 /* 2 * Copyright (c) 2012 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/audio_device_impl.h" 12 13 #include <cstdint> 14 #include <memory> 15 #include <utility> 16 17 #include "absl/base/nullability.h" 18 #include "api/audio/audio_device.h" 19 #include "api/audio/audio_device_defines.h" 20 #include "api/environment/environment.h" 21 #include "api/make_ref_counted.h" 22 #include "api/scoped_refptr.h" 23 #include "modules/audio_device/audio_device_generic.h" 24 #include "rtc_base/checks.h" 25 #include "rtc_base/logging.h" 26 #include "system_wrappers/include/metrics.h" 27 28 #if defined(WEBRTC_WIN) 29 #include "modules/audio_device/win/audio_device_core_win.h" 30 #elif defined(WEBRTC_LINUX) 31 #if defined(WEBRTC_ENABLE_LINUX_ALSA) 32 #include "modules/audio_device/linux/audio_device_alsa_linux.h" 33 #endif 34 #if defined(WEBRTC_ENABLE_LINUX_PULSE) 35 #include "modules/audio_device/linux/audio_device_pulse_linux.h" 36 #endif 37 #elif defined(WEBRTC_IOS) 38 #include "sdk/objc/native/src/audio/audio_device_ios.h" 39 #elif defined(WEBRTC_MAC) 40 #include "modules/audio_device/mac/audio_device_mac.h" 41 #endif 42 #if defined(WEBRTC_DUMMY_FILE_DEVICES) 43 #include "modules/audio_device/dummy/file_audio_device.h" 44 #include "modules/audio_device/dummy/file_audio_device_factory.h" 45 #endif 46 #include "modules/audio_device/dummy/audio_device_dummy.h" 47 48 #define CHECKinitialized_() \ 49 { \ 50 if (!initialized_) { \ 51 return -1; \ 52 } \ 53 } 54 55 #define CHECKinitialized__BOOL() \ 56 { \ 57 if (!initialized_) { \ 58 return false; \ 59 } \ 60 } 61 62 namespace webrtc { 63 64 absl_nullable scoped_refptr<AudioDeviceModuleImpl> 65 AudioDeviceModuleImpl::Create(const Environment& env, AudioLayer audio_layer) { 66 RTC_DLOG(LS_INFO) << __FUNCTION__; 67 68 // The "AudioDeviceModule::kWindowsCoreAudio2" audio layer has its own 69 // dedicated factory method which should be used instead. 70 if (audio_layer == AudioDeviceModule::kWindowsCoreAudio2) { 71 RTC_LOG(LS_ERROR) << "Use the CreateWindowsCoreAudioAudioDeviceModule() " 72 "factory method instead for this option."; 73 return nullptr; 74 } else if (audio_layer == AudioDeviceModule::kAndroidJavaAudio || 75 audio_layer == AudioDeviceModule::kAndroidOpenSLESAudio || 76 audio_layer == 77 AudioDeviceModule::kAndroidJavaInputAndOpenSLESOutputAudio || 78 audio_layer == kAndroidAAudioAudio || 79 audio_layer == kAndroidJavaInputAndAAudioOutputAudio) { 80 RTC_LOG(LS_ERROR) << "Use the CreateAndroidAudioDeviceModule() " 81 "factory method instead for this option."; 82 return nullptr; 83 } 84 85 // Create the generic reference counted (platform independent) implementation. 86 auto audio_device = make_ref_counted<AudioDeviceModuleImpl>(env, audio_layer); 87 88 // Ensure that the current platform is supported. 89 if (audio_device->CheckPlatform() == -1) { 90 return nullptr; 91 } 92 93 // Create the platform-dependent implementation. 94 if (audio_device->CreatePlatformSpecificObjects(env) == -1) { 95 return nullptr; 96 } 97 98 // Ensure that the generic audio buffer can communicate with the platform 99 // specific parts. 100 if (audio_device->AttachAudioBuffer() == -1) { 101 return nullptr; 102 } 103 104 return audio_device; 105 } 106 107 AudioDeviceModuleImpl::AudioDeviceModuleImpl(const Environment& env, 108 AudioLayer audio_layer) 109 : audio_layer_(audio_layer), audio_device_buffer_(env) { 110 RTC_DLOG(LS_INFO) << __FUNCTION__; 111 } 112 113 AudioDeviceModuleImpl::AudioDeviceModuleImpl( 114 const Environment& env, 115 AudioLayer audio_layer, 116 std::unique_ptr<AudioDeviceGeneric> audio_device, 117 bool create_detached) 118 : audio_layer_(audio_layer), 119 audio_device_buffer_(env, create_detached), 120 audio_device_(std::move(audio_device)) { 121 RTC_DLOG(LS_INFO) << __FUNCTION__; 122 } 123 124 int32_t AudioDeviceModuleImpl::CheckPlatform() { 125 RTC_DLOG(LS_INFO) << __FUNCTION__; 126 // Ensure that the current platform is supported 127 PlatformType platform(kPlatformNotSupported); 128 #if defined(_WIN32) 129 platform = kPlatformWin32; 130 RTC_LOG(LS_INFO) << "current platform is Win32"; 131 #elif defined(WEBRTC_ANDROID) 132 platform = kPlatformAndroid; 133 RTC_LOG(LS_INFO) << "current platform is Android"; 134 #elif defined(WEBRTC_LINUX) 135 platform = kPlatformLinux; 136 RTC_LOG(LS_INFO) << "current platform is Linux"; 137 #elif defined(WEBRTC_IOS) 138 platform = kPlatformIOS; 139 RTC_LOG(LS_INFO) << "current platform is IOS"; 140 #elif defined(WEBRTC_MAC) 141 platform = kPlatformMac; 142 RTC_LOG(LS_INFO) << "current platform is Mac"; 143 #elif defined(WEBRTC_FUCHSIA) 144 platform = kPlatformFuchsia; 145 RTC_LOG(LS_INFO) << "current platform is Fuchsia"; 146 #endif 147 if (platform == kPlatformNotSupported) { 148 RTC_LOG(LS_ERROR) 149 << "current platform is not supported => this module will self " 150 "destruct!"; 151 return -1; 152 } 153 platform_type_ = platform; 154 return 0; 155 } 156 157 int32_t AudioDeviceModuleImpl::CreatePlatformSpecificObjects( 158 [[maybe_unused]] const Environment& env) { 159 RTC_LOG(LS_INFO) << __FUNCTION__; 160 if (audio_device_ != nullptr) { 161 RTC_LOG(LS_INFO) << "Reusing provided audio device"; 162 return 0; 163 } 164 // Dummy ADM implementations if build flags are set. 165 #if defined(WEBRTC_DUMMY_AUDIO_BUILD) 166 audio_device_.reset(new AudioDeviceDummy()); 167 RTC_LOG(LS_INFO) << "Dummy Audio APIs will be utilized"; 168 #elif defined(WEBRTC_DUMMY_FILE_DEVICES) 169 audio_device_.reset(FileAudioDeviceFactory::CreateFileAudioDevice()); 170 if (audio_device_) { 171 RTC_LOG(LS_INFO) << "Will use file-playing dummy device."; 172 } else { 173 // Create a dummy device instead. 174 audio_device_.reset(new AudioDeviceDummy()); 175 RTC_LOG(LS_INFO) << "Dummy Audio APIs will be utilized"; 176 } 177 178 // Real (non-dummy) ADM implementations. 179 #else 180 AudioLayer audio_layer(PlatformAudioLayer()); 181 #if defined(WEBRTC_WIN) 182 // Windows ADM implementation. 183 if ((audio_layer == kWindowsCoreAudio) || 184 (audio_layer == kPlatformDefaultAudio)) { 185 RTC_LOG(LS_INFO) << "Attempting to use the Windows Core Audio APIs..."; 186 if (AudioDeviceWindowsCore::CoreAudioIsSupported()) { 187 audio_device_.reset(new AudioDeviceWindowsCore()); 188 RTC_LOG(LS_INFO) << "Windows Core Audio APIs will be utilized"; 189 } 190 } 191 #endif 192 193 // Linux ADM implementation. 194 // Note that, WEBRTC_ENABLE_LINUX_ALSA is always defined by default when 195 // WEBRTC_LINUX is defined. WEBRTC_ENABLE_LINUX_PULSE depends on the 196 // 'rtc_include_pulse_audio' build flag. 197 // TODO(bugs.webrtc.org/9127): improve support and make it more clear that 198 // PulseAudio is the default selection. 199 #if !defined(WEBRTC_ANDROID) && defined(WEBRTC_LINUX) 200 #if !defined(WEBRTC_ENABLE_LINUX_PULSE) 201 // Build flag 'rtc_include_pulse_audio' is set to false. In this mode: 202 // - kPlatformDefaultAudio => ALSA, and 203 // - kLinuxAlsaAudio => ALSA, and 204 // - kLinuxPulseAudio => Invalid selection. 205 RTC_LOG(LS_WARNING) << "PulseAudio is disabled using build flag."; 206 if ((audio_layer == kLinuxAlsaAudio) || 207 (audio_layer == kPlatformDefaultAudio)) { 208 audio_device_.reset(new AudioDeviceLinuxALSA()); 209 RTC_LOG(LS_INFO) << "Linux ALSA APIs will be utilized."; 210 } 211 #else 212 // Build flag 'rtc_include_pulse_audio' is set to true (default). In this 213 // mode: 214 // - kPlatformDefaultAudio => PulseAudio, and 215 // - kLinuxPulseAudio => PulseAudio, and 216 // - kLinuxAlsaAudio => ALSA (supported but not default). 217 RTC_LOG(LS_INFO) << "PulseAudio support is enabled."; 218 if ((audio_layer == kLinuxPulseAudio) || 219 (audio_layer == kPlatformDefaultAudio)) { 220 // Linux PulseAudio implementation is default. 221 audio_device_.reset(new AudioDeviceLinuxPulse()); 222 RTC_LOG(LS_INFO) << "Linux PulseAudio APIs will be utilized"; 223 } else if (audio_layer == kLinuxAlsaAudio) { 224 audio_device_.reset(new AudioDeviceLinuxALSA()); 225 RTC_LOG(LS_WARNING) << "Linux ALSA APIs will be utilized."; 226 } 227 #endif // #if !defined(WEBRTC_ENABLE_LINUX_PULSE) 228 #endif // #if defined(WEBRTC_LINUX) 229 230 // iOS ADM implementation. 231 #if defined(WEBRTC_IOS) 232 if (audio_layer == kPlatformDefaultAudio) { 233 audio_device_ = std::make_unique<ios_adm::AudioDeviceIOS>( 234 env, 235 /*bypass_voice_processing=*/false, 236 /*muted_speech_event_handler=*/nullptr, 237 /*render_error_handler=*/nullptr); 238 RTC_LOG(LS_INFO) << "iPhone Audio APIs will be utilized."; 239 } 240 // END #if defined(WEBRTC_IOS) 241 242 // Mac OS X ADM implementation. 243 #elif defined(WEBRTC_MAC) 244 if (audio_layer == kPlatformDefaultAudio) { 245 audio_device_.reset(new AudioDeviceMac()); 246 RTC_LOG(LS_INFO) << "Mac OS X Audio APIs will be utilized."; 247 } 248 #endif // WEBRTC_MAC 249 250 // Dummy ADM implementation. 251 if (audio_layer == kDummyAudio) { 252 audio_device_.reset(new AudioDeviceDummy()); 253 RTC_LOG(LS_INFO) << "Dummy Audio APIs will be utilized."; 254 } 255 #endif // if defined(WEBRTC_DUMMY_AUDIO_BUILD) 256 257 if (!audio_device_) { 258 RTC_LOG(LS_ERROR) 259 << "Failed to create the platform specific ADM implementation."; 260 return -1; 261 } 262 return 0; 263 } 264 265 int32_t AudioDeviceModuleImpl::AttachAudioBuffer() { 266 RTC_LOG(LS_INFO) << __FUNCTION__; 267 audio_device_->AttachAudioBuffer(&audio_device_buffer_); 268 return 0; 269 } 270 271 AudioDeviceModuleImpl::~AudioDeviceModuleImpl() { 272 RTC_LOG(LS_INFO) << __FUNCTION__; 273 } 274 275 int32_t AudioDeviceModuleImpl::ActiveAudioLayer(AudioLayer* audioLayer) const { 276 RTC_LOG(LS_INFO) << __FUNCTION__; 277 AudioLayer activeAudio; 278 if (audio_device_->ActiveAudioLayer(activeAudio) == -1) { 279 return -1; 280 } 281 *audioLayer = activeAudio; 282 return 0; 283 } 284 285 int32_t AudioDeviceModuleImpl::Init() { 286 RTC_LOG(LS_INFO) << __FUNCTION__; 287 if (initialized_) 288 return 0; 289 RTC_CHECK(audio_device_); 290 AudioDeviceGeneric::InitStatus status = audio_device_->Init(); 291 RTC_HISTOGRAM_ENUMERATION( 292 "WebRTC.Audio.InitializationResult", static_cast<int>(status), 293 static_cast<int>(AudioDeviceGeneric::InitStatus::NUM_STATUSES)); 294 if (status != AudioDeviceGeneric::InitStatus::OK) { 295 RTC_LOG(LS_ERROR) << "Audio device initialization failed."; 296 return -1; 297 } 298 initialized_ = true; 299 return 0; 300 } 301 302 int32_t AudioDeviceModuleImpl::Terminate() { 303 RTC_LOG(LS_INFO) << __FUNCTION__; 304 if (!initialized_) 305 return 0; 306 if (audio_device_->Terminate() == -1) { 307 return -1; 308 } 309 initialized_ = false; 310 return 0; 311 } 312 313 bool AudioDeviceModuleImpl::Initialized() const { 314 RTC_LOG(LS_INFO) << __FUNCTION__ << ": " << initialized_; 315 return initialized_; 316 } 317 318 int32_t AudioDeviceModuleImpl::InitSpeaker() { 319 RTC_LOG(LS_INFO) << __FUNCTION__; 320 CHECKinitialized_(); 321 return audio_device_->InitSpeaker(); 322 } 323 324 int32_t AudioDeviceModuleImpl::InitMicrophone() { 325 RTC_LOG(LS_INFO) << __FUNCTION__; 326 CHECKinitialized_(); 327 return audio_device_->InitMicrophone(); 328 } 329 330 int32_t AudioDeviceModuleImpl::SpeakerVolumeIsAvailable(bool* available) { 331 RTC_LOG(LS_INFO) << __FUNCTION__; 332 CHECKinitialized_(); 333 bool isAvailable = false; 334 if (audio_device_->SpeakerVolumeIsAvailable(isAvailable) == -1) { 335 return -1; 336 } 337 *available = isAvailable; 338 RTC_LOG(LS_INFO) << "output: " << isAvailable; 339 return 0; 340 } 341 342 int32_t AudioDeviceModuleImpl::SetSpeakerVolume(uint32_t volume) { 343 RTC_LOG(LS_INFO) << __FUNCTION__ << "(" << volume << ")"; 344 CHECKinitialized_(); 345 return audio_device_->SetSpeakerVolume(volume); 346 } 347 348 int32_t AudioDeviceModuleImpl::SpeakerVolume(uint32_t* volume) const { 349 RTC_LOG(LS_INFO) << __FUNCTION__; 350 CHECKinitialized_(); 351 uint32_t level = 0; 352 if (audio_device_->SpeakerVolume(level) == -1) { 353 return -1; 354 } 355 *volume = level; 356 RTC_LOG(LS_INFO) << "output: " << *volume; 357 return 0; 358 } 359 360 bool AudioDeviceModuleImpl::SpeakerIsInitialized() const { 361 RTC_LOG(LS_INFO) << __FUNCTION__; 362 CHECKinitialized__BOOL(); 363 bool isInitialized = audio_device_->SpeakerIsInitialized(); 364 RTC_LOG(LS_INFO) << "output: " << isInitialized; 365 return isInitialized; 366 } 367 368 bool AudioDeviceModuleImpl::MicrophoneIsInitialized() const { 369 RTC_LOG(LS_INFO) << __FUNCTION__; 370 CHECKinitialized__BOOL(); 371 bool isInitialized = audio_device_->MicrophoneIsInitialized(); 372 RTC_LOG(LS_INFO) << "output: " << isInitialized; 373 return isInitialized; 374 } 375 376 int32_t AudioDeviceModuleImpl::MaxSpeakerVolume(uint32_t* maxVolume) const { 377 CHECKinitialized_(); 378 uint32_t maxVol = 0; 379 if (audio_device_->MaxSpeakerVolume(maxVol) == -1) { 380 return -1; 381 } 382 *maxVolume = maxVol; 383 return 0; 384 } 385 386 int32_t AudioDeviceModuleImpl::MinSpeakerVolume(uint32_t* minVolume) const { 387 CHECKinitialized_(); 388 uint32_t minVol = 0; 389 if (audio_device_->MinSpeakerVolume(minVol) == -1) { 390 return -1; 391 } 392 *minVolume = minVol; 393 return 0; 394 } 395 396 int32_t AudioDeviceModuleImpl::SpeakerMuteIsAvailable(bool* available) { 397 RTC_LOG(LS_INFO) << __FUNCTION__; 398 CHECKinitialized_(); 399 bool isAvailable = false; 400 if (audio_device_->SpeakerMuteIsAvailable(isAvailable) == -1) { 401 return -1; 402 } 403 *available = isAvailable; 404 RTC_LOG(LS_INFO) << "output: " << isAvailable; 405 return 0; 406 } 407 408 int32_t AudioDeviceModuleImpl::SetSpeakerMute(bool enable) { 409 RTC_LOG(LS_INFO) << __FUNCTION__ << "(" << enable << ")"; 410 CHECKinitialized_(); 411 return audio_device_->SetSpeakerMute(enable); 412 } 413 414 int32_t AudioDeviceModuleImpl::SpeakerMute(bool* enabled) const { 415 RTC_LOG(LS_INFO) << __FUNCTION__; 416 CHECKinitialized_(); 417 bool muted = false; 418 if (audio_device_->SpeakerMute(muted) == -1) { 419 return -1; 420 } 421 *enabled = muted; 422 RTC_LOG(LS_INFO) << "output: " << muted; 423 return 0; 424 } 425 426 int32_t AudioDeviceModuleImpl::MicrophoneMuteIsAvailable(bool* available) { 427 RTC_LOG(LS_INFO) << __FUNCTION__; 428 CHECKinitialized_(); 429 bool isAvailable = false; 430 if (audio_device_->MicrophoneMuteIsAvailable(isAvailable) == -1) { 431 return -1; 432 } 433 *available = isAvailable; 434 RTC_LOG(LS_INFO) << "output: " << isAvailable; 435 return 0; 436 } 437 438 int32_t AudioDeviceModuleImpl::SetMicrophoneMute(bool enable) { 439 RTC_LOG(LS_INFO) << __FUNCTION__ << "(" << enable << ")"; 440 CHECKinitialized_(); 441 return (audio_device_->SetMicrophoneMute(enable)); 442 } 443 444 int32_t AudioDeviceModuleImpl::MicrophoneMute(bool* enabled) const { 445 RTC_LOG(LS_INFO) << __FUNCTION__; 446 CHECKinitialized_(); 447 bool muted = false; 448 if (audio_device_->MicrophoneMute(muted) == -1) { 449 return -1; 450 } 451 *enabled = muted; 452 RTC_LOG(LS_INFO) << "output: " << muted; 453 return 0; 454 } 455 456 int32_t AudioDeviceModuleImpl::MicrophoneVolumeIsAvailable(bool* available) { 457 RTC_LOG(LS_INFO) << __FUNCTION__; 458 CHECKinitialized_(); 459 bool isAvailable = false; 460 if (audio_device_->MicrophoneVolumeIsAvailable(isAvailable) == -1) { 461 return -1; 462 } 463 *available = isAvailable; 464 RTC_LOG(LS_INFO) << "output: " << isAvailable; 465 return 0; 466 } 467 468 int32_t AudioDeviceModuleImpl::SetMicrophoneVolume(uint32_t volume) { 469 RTC_LOG(LS_INFO) << __FUNCTION__ << "(" << volume << ")"; 470 CHECKinitialized_(); 471 return (audio_device_->SetMicrophoneVolume(volume)); 472 } 473 474 int32_t AudioDeviceModuleImpl::MicrophoneVolume(uint32_t* volume) const { 475 RTC_LOG(LS_INFO) << __FUNCTION__; 476 CHECKinitialized_(); 477 uint32_t level = 0; 478 if (audio_device_->MicrophoneVolume(level) == -1) { 479 return -1; 480 } 481 *volume = level; 482 RTC_LOG(LS_INFO) << "output: " << *volume; 483 return 0; 484 } 485 486 int32_t AudioDeviceModuleImpl::StereoRecordingIsAvailable( 487 bool* available) const { 488 RTC_LOG(LS_INFO) << __FUNCTION__; 489 CHECKinitialized_(); 490 bool isAvailable = false; 491 if (audio_device_->StereoRecordingIsAvailable(isAvailable) == -1) { 492 return -1; 493 } 494 *available = isAvailable; 495 RTC_LOG(LS_INFO) << "output: " << isAvailable; 496 return 0; 497 } 498 499 int32_t AudioDeviceModuleImpl::SetStereoRecording(bool enable) { 500 RTC_LOG(LS_INFO) << __FUNCTION__ << "(" << enable << ")"; 501 CHECKinitialized_(); 502 if (audio_device_->RecordingIsInitialized()) { 503 RTC_LOG(LS_ERROR) 504 << "unable to set stereo mode after recording is initialized"; 505 return -1; 506 } 507 if (audio_device_->SetStereoRecording(enable) == -1) { 508 if (enable) { 509 RTC_LOG(LS_WARNING) << "failed to enable stereo recording"; 510 } 511 return -1; 512 } 513 int8_t nChannels(1); 514 if (enable) { 515 nChannels = 2; 516 } 517 audio_device_buffer_.SetRecordingChannels(nChannels); 518 return 0; 519 } 520 521 int32_t AudioDeviceModuleImpl::StereoRecording(bool* enabled) const { 522 RTC_LOG(LS_INFO) << __FUNCTION__; 523 CHECKinitialized_(); 524 bool stereo = false; 525 if (audio_device_->StereoRecording(stereo) == -1) { 526 return -1; 527 } 528 *enabled = stereo; 529 RTC_LOG(LS_INFO) << "output: " << stereo; 530 return 0; 531 } 532 533 int32_t AudioDeviceModuleImpl::StereoPlayoutIsAvailable(bool* available) const { 534 RTC_LOG(LS_INFO) << __FUNCTION__; 535 CHECKinitialized_(); 536 bool isAvailable = false; 537 if (audio_device_->StereoPlayoutIsAvailable(isAvailable) == -1) { 538 return -1; 539 } 540 *available = isAvailable; 541 RTC_LOG(LS_INFO) << "output: " << isAvailable; 542 return 0; 543 } 544 545 int32_t AudioDeviceModuleImpl::SetStereoPlayout(bool enable) { 546 RTC_LOG(LS_INFO) << __FUNCTION__ << "(" << enable << ")"; 547 CHECKinitialized_(); 548 if (audio_device_->PlayoutIsInitialized()) { 549 RTC_LOG(LS_ERROR) 550 << "unable to set stereo mode while playing side is initialized"; 551 return -1; 552 } 553 if (audio_device_->SetStereoPlayout(enable)) { 554 RTC_LOG(LS_WARNING) << "stereo playout is not supported"; 555 return -1; 556 } 557 int8_t nChannels(1); 558 if (enable) { 559 nChannels = 2; 560 } 561 audio_device_buffer_.SetPlayoutChannels(nChannels); 562 return 0; 563 } 564 565 int32_t AudioDeviceModuleImpl::StereoPlayout(bool* enabled) const { 566 RTC_LOG(LS_INFO) << __FUNCTION__; 567 CHECKinitialized_(); 568 bool stereo = false; 569 if (audio_device_->StereoPlayout(stereo) == -1) { 570 return -1; 571 } 572 *enabled = stereo; 573 RTC_LOG(LS_INFO) << "output: " << stereo; 574 return 0; 575 } 576 577 int32_t AudioDeviceModuleImpl::PlayoutIsAvailable(bool* available) { 578 RTC_LOG(LS_INFO) << __FUNCTION__; 579 CHECKinitialized_(); 580 bool isAvailable = false; 581 if (audio_device_->PlayoutIsAvailable(isAvailable) == -1) { 582 return -1; 583 } 584 *available = isAvailable; 585 RTC_LOG(LS_INFO) << "output: " << isAvailable; 586 return 0; 587 } 588 589 int32_t AudioDeviceModuleImpl::RecordingIsAvailable(bool* available) { 590 RTC_LOG(LS_INFO) << __FUNCTION__; 591 CHECKinitialized_(); 592 bool isAvailable = false; 593 if (audio_device_->RecordingIsAvailable(isAvailable) == -1) { 594 return -1; 595 } 596 *available = isAvailable; 597 RTC_LOG(LS_INFO) << "output: " << isAvailable; 598 return 0; 599 } 600 601 int32_t AudioDeviceModuleImpl::MaxMicrophoneVolume(uint32_t* maxVolume) const { 602 CHECKinitialized_(); 603 uint32_t maxVol(0); 604 if (audio_device_->MaxMicrophoneVolume(maxVol) == -1) { 605 return -1; 606 } 607 *maxVolume = maxVol; 608 return 0; 609 } 610 611 int32_t AudioDeviceModuleImpl::MinMicrophoneVolume(uint32_t* minVolume) const { 612 CHECKinitialized_(); 613 uint32_t minVol(0); 614 if (audio_device_->MinMicrophoneVolume(minVol) == -1) { 615 return -1; 616 } 617 *minVolume = minVol; 618 return 0; 619 } 620 621 int16_t AudioDeviceModuleImpl::PlayoutDevices() { 622 RTC_LOG(LS_INFO) << __FUNCTION__; 623 CHECKinitialized_(); 624 uint16_t nPlayoutDevices = audio_device_->PlayoutDevices(); 625 RTC_LOG(LS_INFO) << "output: " << nPlayoutDevices; 626 return (int16_t)(nPlayoutDevices); 627 } 628 629 int32_t AudioDeviceModuleImpl::SetPlayoutDevice(uint16_t index) { 630 RTC_LOG(LS_INFO) << __FUNCTION__ << "(" << index << ")"; 631 CHECKinitialized_(); 632 return audio_device_->SetPlayoutDevice(index); 633 } 634 635 int32_t AudioDeviceModuleImpl::SetPlayoutDevice(WindowsDeviceType device) { 636 RTC_LOG(LS_INFO) << __FUNCTION__; 637 CHECKinitialized_(); 638 return audio_device_->SetPlayoutDevice(device); 639 } 640 641 int32_t AudioDeviceModuleImpl::PlayoutDeviceName( 642 uint16_t index, 643 char name[kAdmMaxDeviceNameSize], 644 char guid[kAdmMaxGuidSize]) { 645 RTC_LOG(LS_INFO) << __FUNCTION__ << "(" << index << ", ...)"; 646 CHECKinitialized_(); 647 if (name == nullptr) { 648 return -1; 649 } 650 if (audio_device_->PlayoutDeviceName(index, name, guid) == -1) { 651 return -1; 652 } 653 if (name != nullptr) { 654 RTC_LOG(LS_INFO) << "output: name = " << name; 655 } 656 if (guid != nullptr) { 657 RTC_LOG(LS_INFO) << "output: guid = " << guid; 658 } 659 return 0; 660 } 661 662 int32_t AudioDeviceModuleImpl::RecordingDeviceName( 663 uint16_t index, 664 char name[kAdmMaxDeviceNameSize], 665 char guid[kAdmMaxGuidSize]) { 666 RTC_LOG(LS_INFO) << __FUNCTION__ << "(" << index << ", ...)"; 667 CHECKinitialized_(); 668 if (name == nullptr) { 669 return -1; 670 } 671 if (audio_device_->RecordingDeviceName(index, name, guid) == -1) { 672 return -1; 673 } 674 if (name != nullptr) { 675 RTC_LOG(LS_INFO) << "output: name = " << name; 676 } 677 if (guid != nullptr) { 678 RTC_LOG(LS_INFO) << "output: guid = " << guid; 679 } 680 return 0; 681 } 682 683 int16_t AudioDeviceModuleImpl::RecordingDevices() { 684 RTC_LOG(LS_INFO) << __FUNCTION__; 685 CHECKinitialized_(); 686 uint16_t nRecordingDevices = audio_device_->RecordingDevices(); 687 RTC_LOG(LS_INFO) << "output: " << nRecordingDevices; 688 return (int16_t)nRecordingDevices; 689 } 690 691 int32_t AudioDeviceModuleImpl::SetRecordingDevice(uint16_t index) { 692 RTC_LOG(LS_INFO) << __FUNCTION__ << "(" << index << ")"; 693 CHECKinitialized_(); 694 return audio_device_->SetRecordingDevice(index); 695 } 696 697 int32_t AudioDeviceModuleImpl::SetRecordingDevice(WindowsDeviceType device) { 698 RTC_LOG(LS_INFO) << __FUNCTION__; 699 CHECKinitialized_(); 700 return audio_device_->SetRecordingDevice(device); 701 } 702 703 int32_t AudioDeviceModuleImpl::InitPlayout() { 704 RTC_LOG(LS_INFO) << __FUNCTION__; 705 CHECKinitialized_(); 706 if (PlayoutIsInitialized()) { 707 return 0; 708 } 709 int32_t result = audio_device_->InitPlayout(); 710 RTC_LOG(LS_INFO) << "output: " << result; 711 RTC_HISTOGRAM_BOOLEAN("WebRTC.Audio.InitPlayoutSuccess", 712 static_cast<int>(result == 0)); 713 return result; 714 } 715 716 int32_t AudioDeviceModuleImpl::InitRecording() { 717 RTC_LOG(LS_INFO) << __FUNCTION__; 718 CHECKinitialized_(); 719 if (RecordingIsInitialized()) { 720 return 0; 721 } 722 int32_t result = audio_device_->InitRecording(); 723 RTC_LOG(LS_INFO) << "output: " << result; 724 RTC_HISTOGRAM_BOOLEAN("WebRTC.Audio.InitRecordingSuccess", 725 static_cast<int>(result == 0)); 726 return result; 727 } 728 729 bool AudioDeviceModuleImpl::PlayoutIsInitialized() const { 730 RTC_LOG(LS_INFO) << __FUNCTION__; 731 CHECKinitialized__BOOL(); 732 return audio_device_->PlayoutIsInitialized(); 733 } 734 735 bool AudioDeviceModuleImpl::RecordingIsInitialized() const { 736 RTC_LOG(LS_INFO) << __FUNCTION__; 737 CHECKinitialized__BOOL(); 738 return audio_device_->RecordingIsInitialized(); 739 } 740 741 int32_t AudioDeviceModuleImpl::StartPlayout() { 742 RTC_LOG(LS_INFO) << __FUNCTION__; 743 CHECKinitialized_(); 744 if (Playing()) { 745 return 0; 746 } 747 audio_device_buffer_.StartPlayout(); 748 int32_t result = audio_device_->StartPlayout(); 749 RTC_LOG(LS_INFO) << "output: " << result; 750 RTC_HISTOGRAM_BOOLEAN("WebRTC.Audio.StartPlayoutSuccess", 751 static_cast<int>(result == 0)); 752 return result; 753 } 754 755 int32_t AudioDeviceModuleImpl::StopPlayout() { 756 RTC_LOG(LS_INFO) << __FUNCTION__; 757 CHECKinitialized_(); 758 int32_t result = audio_device_->StopPlayout(); 759 audio_device_buffer_.StopPlayout(); 760 RTC_LOG(LS_INFO) << "output: " << result; 761 RTC_HISTOGRAM_BOOLEAN("WebRTC.Audio.StopPlayoutSuccess", 762 static_cast<int>(result == 0)); 763 return result; 764 } 765 766 bool AudioDeviceModuleImpl::Playing() const { 767 RTC_LOG(LS_INFO) << __FUNCTION__; 768 CHECKinitialized__BOOL(); 769 return audio_device_->Playing(); 770 } 771 772 int32_t AudioDeviceModuleImpl::StartRecording() { 773 RTC_LOG(LS_INFO) << __FUNCTION__; 774 CHECKinitialized_(); 775 if (Recording()) { 776 return 0; 777 } 778 audio_device_buffer_.StartRecording(); 779 int32_t result = audio_device_->StartRecording(); 780 RTC_LOG(LS_INFO) << "output: " << result; 781 RTC_HISTOGRAM_BOOLEAN("WebRTC.Audio.StartRecordingSuccess", 782 static_cast<int>(result == 0)); 783 return result; 784 } 785 786 int32_t AudioDeviceModuleImpl::StopRecording() { 787 RTC_LOG(LS_INFO) << __FUNCTION__; 788 CHECKinitialized_(); 789 int32_t result = audio_device_->StopRecording(); 790 audio_device_buffer_.StopRecording(); 791 RTC_LOG(LS_INFO) << "output: " << result; 792 RTC_HISTOGRAM_BOOLEAN("WebRTC.Audio.StopRecordingSuccess", 793 static_cast<int>(result == 0)); 794 return result; 795 } 796 797 bool AudioDeviceModuleImpl::Recording() const { 798 RTC_LOG(LS_INFO) << __FUNCTION__; 799 CHECKinitialized__BOOL(); 800 return audio_device_->Recording(); 801 } 802 803 int32_t AudioDeviceModuleImpl::RegisterAudioCallback( 804 AudioTransport* audioCallback) { 805 RTC_LOG(LS_INFO) << __FUNCTION__; 806 return audio_device_buffer_.RegisterAudioCallback(audioCallback); 807 } 808 809 int32_t AudioDeviceModuleImpl::PlayoutDelay(uint16_t* delayMS) const { 810 CHECKinitialized_(); 811 uint16_t delay = 0; 812 if (audio_device_->PlayoutDelay(delay) == -1) { 813 RTC_LOG(LS_ERROR) << "failed to retrieve the playout delay"; 814 return -1; 815 } 816 *delayMS = delay; 817 return 0; 818 } 819 820 bool AudioDeviceModuleImpl::BuiltInAECIsAvailable() const { 821 RTC_LOG(LS_INFO) << __FUNCTION__; 822 CHECKinitialized__BOOL(); 823 bool isAvailable = audio_device_->BuiltInAECIsAvailable(); 824 RTC_LOG(LS_INFO) << "output: " << isAvailable; 825 return isAvailable; 826 } 827 828 int32_t AudioDeviceModuleImpl::EnableBuiltInAEC(bool enable) { 829 RTC_LOG(LS_INFO) << __FUNCTION__ << "(" << enable << ")"; 830 CHECKinitialized_(); 831 int32_t ok = audio_device_->EnableBuiltInAEC(enable); 832 RTC_LOG(LS_INFO) << "output: " << ok; 833 return ok; 834 } 835 836 bool AudioDeviceModuleImpl::BuiltInAGCIsAvailable() const { 837 RTC_LOG(LS_INFO) << __FUNCTION__; 838 CHECKinitialized__BOOL(); 839 bool isAvailable = audio_device_->BuiltInAGCIsAvailable(); 840 RTC_LOG(LS_INFO) << "output: " << isAvailable; 841 return isAvailable; 842 } 843 844 int32_t AudioDeviceModuleImpl::EnableBuiltInAGC(bool enable) { 845 RTC_LOG(LS_INFO) << __FUNCTION__ << "(" << enable << ")"; 846 CHECKinitialized_(); 847 int32_t ok = audio_device_->EnableBuiltInAGC(enable); 848 RTC_LOG(LS_INFO) << "output: " << ok; 849 return ok; 850 } 851 852 bool AudioDeviceModuleImpl::BuiltInNSIsAvailable() const { 853 RTC_LOG(LS_INFO) << __FUNCTION__; 854 CHECKinitialized__BOOL(); 855 bool isAvailable = audio_device_->BuiltInNSIsAvailable(); 856 RTC_LOG(LS_INFO) << "output: " << isAvailable; 857 return isAvailable; 858 } 859 860 int32_t AudioDeviceModuleImpl::EnableBuiltInNS(bool enable) { 861 RTC_LOG(LS_INFO) << __FUNCTION__ << "(" << enable << ")"; 862 CHECKinitialized_(); 863 int32_t ok = audio_device_->EnableBuiltInNS(enable); 864 RTC_LOG(LS_INFO) << "output: " << ok; 865 return ok; 866 } 867 868 int32_t AudioDeviceModuleImpl::GetPlayoutUnderrunCount() const { 869 CHECKinitialized_(); 870 int32_t underrunCount = audio_device_->GetPlayoutUnderrunCount(); 871 return underrunCount; 872 } 873 874 #if defined(WEBRTC_IOS) 875 int AudioDeviceModuleImpl::GetPlayoutAudioParameters( 876 AudioParameters* params) const { 877 RTC_LOG(LS_INFO) << __FUNCTION__; 878 int r = audio_device_->GetPlayoutAudioParameters(params); 879 RTC_LOG(LS_INFO) << "output: " << r; 880 return r; 881 } 882 883 int AudioDeviceModuleImpl::GetRecordAudioParameters( 884 AudioParameters* params) const { 885 RTC_LOG(LS_INFO) << __FUNCTION__; 886 int r = audio_device_->GetRecordAudioParameters(params); 887 RTC_LOG(LS_INFO) << "output: " << r; 888 return r; 889 } 890 #endif // WEBRTC_IOS 891 892 AudioDeviceModuleImpl::PlatformType AudioDeviceModuleImpl::Platform() const { 893 RTC_LOG(LS_INFO) << __FUNCTION__; 894 return platform_type_; 895 } 896 897 AudioDeviceModule::AudioLayer AudioDeviceModuleImpl::PlatformAudioLayer() 898 const { 899 RTC_LOG(LS_INFO) << __FUNCTION__; 900 return audio_layer_; 901 } 902 903 } // namespace webrtc