GetSimpleAudioVolume

image.png
image.png

AudioDeviceWindowsCore

image.png
GetSimpleAudioVolume 返回值赋值到 _ptrRenderSimpleVolume

源码分析

h:\webrtc-20210315\webrtc-20210315\webrtc\webrtc-checkout\src\media\engine\webrtc_voice_engine.cc

WebRtcVoiceEngine::Init

  1. void WebRtcVoiceEngine::Init() {
  2. ***
  3. RTC_CHECK(adm());
  4. webrtc::adm_helpers::Init(adm());
  5. ***
  6. }

这时候进入到
H:\webrtc-20210315\webrtc-20210315\webrtc\webrtc-checkout\src\media\engine\adm_helpers.cc
void Init(AudioDeviceModule adm) 方法
这时候进入到
H:\webrtc-20210315\webrtc-20210315\webrtc\webrtc-checkout\src\media\engine\adm_helpers.cc
void Init(AudioDeviceModule
adm) 方法

webrtc::adm_helpers::Init

  1. void Init(AudioDeviceModule* adm) {
  2. RTC_DCHECK(adm);
  3. ****
  4. // Playout device.
  5. {
  6. if (adm->SetPlayoutDevice(AUDIO_DEVICE_ID) != 0) {
  7. RTC_LOG(LS_ERROR) << "Unable to set playout device.";
  8. return;
  9. }
  10. if (adm->InitSpeaker() != 0) {
  11. RTC_LOG(LS_ERROR) << "Unable to access speaker.";
  12. }
  13. ***
  14. }

这里主要讲 adm->InitSpeaker().

AudioDeviceModuleImpl::InitSpeaker

  1. int32_t AudioDeviceModuleImpl::InitSpeaker() {
  2. RTC_LOG(INFO) << __FUNCTION__;
  3. CHECKinitialized_();
  4. return audio_device_->InitSpeaker();
  5. }

AudioDeviceWindowsCore::InitSpeaker

  1. int32_t AudioDeviceWindowsCore::InitSpeaker() {
  2. MutexLock lock(&mutex_);
  3. return InitSpeakerLocked();
  4. }

AudioDeviceWindowsCore::InitSpeakerLocked

  1. int32_t AudioDeviceWindowsCore::InitSpeakerLocked() {
  2. if (_playing) {
  3. return -1;
  4. }
  5. if (_ptrDeviceOut == NULL) {
  6. return -1;
  7. }
  8. if (_usingOutputDeviceIndex) {
  9. int16_t nDevices = PlayoutDevicesLocked();
  10. if (_outputDeviceIndex > (nDevices - 1)) {
  11. RTC_LOG(LS_ERROR) << "current device selection is invalid => unable to"
  12. " initialize";
  13. return -1;
  14. }
  15. }
  16. int32_t ret(0);
  17. SAFE_RELEASE(_ptrDeviceOut);
  18. if (_usingOutputDeviceIndex) {
  19. // Refresh the selected rendering endpoint device using current index
  20. ret = _GetListDevice(eRender, _outputDeviceIndex, &_ptrDeviceOut);
  21. } else {
  22. ERole role;
  23. (_outputDevice == AudioDeviceModule::kDefaultDevice)
  24. ? role = eConsole
  25. : role = eCommunications;
  26. // Refresh the selected rendering endpoint device using role
  27. // 里面会调用 _ptrEnumerator->GetDefaultAudioEndpoint(dir, role, ppDevice)
  28. // 刷新是必须的,避免操作时设备已经没有了。
  29. ret = _GetDefaultDevice(eRender, role, &_ptrDeviceOut);
  30. }
  31. if (ret != 0 || (_ptrDeviceOut == NULL)) {
  32. RTC_LOG(LS_ERROR) << "failed to initialize the rendering enpoint device";
  33. SAFE_RELEASE(_ptrDeviceOut);
  34. return -1;
  35. }
  36. IAudioSessionManager* pManager = NULL;
  37. ret = _ptrDeviceOut->Activate(__uuidof(IAudioSessionManager), CLSCTX_ALL,
  38. NULL, (void**)&pManager);
  39. if (ret != 0 || pManager == NULL) {
  40. RTC_LOG(LS_ERROR) << "failed to initialize the render manager";
  41. SAFE_RELEASE(pManager);
  42. return -1;
  43. }
  44. //通过上面的pManager来获取操作音频音量对象,NULL音频流使用默认session,FALSE非跨进程
  45. SAFE_RELEASE(_ptrRenderSimpleVolume);
  46. ret = pManager->GetSimpleAudioVolume(NULL, FALSE, &_ptrRenderSimpleVolume);
  47. if (ret != 0 || _ptrRenderSimpleVolume == NULL) {
  48. RTC_LOG(LS_ERROR) << "failed to initialize the render simple volume";
  49. SAFE_RELEASE(pManager);
  50. SAFE_RELEASE(_ptrRenderSimpleVolume);
  51. return -1;
  52. }
  53. SAFE_RELEASE(pManager);
  54. _speakerIsInitialized = true;
  55. return 0;
  56. }

堆栈调用

image.png