前言

image.png
分为外部AudioState和内部AudioState

RefCountedObject
image.png
image.png
image.png

代码分析

WebRtcVoiceEngine::Init

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

  1. void WebRtcVoiceEngine::Init() {
  2. RTC_DCHECK_RUN_ON(&worker_thread_checker_);
  3. RTC_LOG(LS_INFO) << "WebRtcVoiceEngine::Init";
  4. // 1、 TaskQueue expects to be created/destroyed on the same thread.
  5. low_priority_worker_queue_.reset(
  6. new rtc::TaskQueue(task_queue_factory_->CreateTaskQueue(
  7. "rtc-low-prio", webrtc::TaskQueueFactory::Priority::LOW)));
  8. // 2、 收集音频编码器
  9. RTC_LOG(LS_VERBOSE) << "Supported send codecs in order of preference:";
  10. send_codecs_ = CollectCodecs(encoder_factory_->GetSupportedEncoders());
  11. for (const AudioCodec& codec : send_codecs_) {
  12. RTC_LOG(LS_VERBOSE) << ToString(codec);
  13. }
  14. // 3、 收集音频解码器
  15. RTC_LOG(LS_VERBOSE) << "Supported recv codecs in order of preference:";
  16. recv_codecs_ = CollectCodecs(decoder_factory_->GetSupportedDecoders());
  17. for (const AudioCodec& codec : recv_codecs_) {
  18. RTC_LOG(LS_VERBOSE) << ToString(codec);
  19. }
  20. // 4、创建ADM
  21. #if defined(WEBRTC_INCLUDE_INTERNAL_AUDIO_DEVICE)
  22. // No ADM supplied? Create a default one.
  23. if (!adm_) {
  24. adm_ = webrtc::AudioDeviceModule::Create(
  25. webrtc::AudioDeviceModule::kPlatformDefaultAudio, task_queue_factory_);
  26. }
  27. #endif // WEBRTC_INCLUDE_INTERNAL_AUDIO_DEVICE
  28. RTC_CHECK(adm());
  29. // 5、 初始化adm
  30. webrtc::adm_helpers::Init(adm());
  31. // 6、 Set up AudioState. 当前讲解这一步
  32. {
  33. webrtc::AudioState::Config config;
  34. if (audio_mixer_) {
  35. config.audio_mixer = audio_mixer_;
  36. } else {
  37. config.audio_mixer = webrtc::AudioMixerImpl::Create();
  38. }
  39. config.audio_processing = apm_;
  40. config.audio_device_module = adm_;
  41. if (audio_frame_processor_)
  42. config.async_audio_processing_factory =
  43. new rtc::RefCountedObject<webrtc::AsyncAudioProcessing::Factory>(
  44. *audio_frame_processor_, *task_queue_factory_);
  45. audio_state_ = webrtc::AudioState::Create(config);
  46. }
  47. // 7、 Connect the ADM to our audio path.
  48. // 注册回调,采集到音频数据后,由audio_transport()传输
  49. adm()->RegisterAudioCallback(audio_state()->audio_transport());
  50. // 8、 Set default engine options.
  51. {
  52. AudioOptions options;
  53. options.echo_cancellation = true;
  54. options.auto_gain_control = true;
  55. #if defined(WEBRTC_IOS)
  56. // On iOS, VPIO provides built-in NS.
  57. options.noise_suppression = false;
  58. options.typing_detection = false;
  59. #else
  60. options.noise_suppression = true;
  61. options.typing_detection = true;
  62. #endif
  63. options.experimental_ns = false;
  64. options.highpass_filter = true;
  65. options.stereo_swapping = false;
  66. options.audio_jitter_buffer_max_packets = 200;
  67. options.audio_jitter_buffer_fast_accelerate = false;
  68. options.audio_jitter_buffer_min_delay_ms = 0;
  69. options.audio_jitter_buffer_enable_rtx_handling = false;
  70. options.experimental_agc = false;
  71. options.residual_echo_detector = true;
  72. bool error = ApplyOptions(options);
  73. RTC_DCHECK(error);
  74. }
  75. initialized_ = true;
  76. }

—》
// 6、 Set up AudioState. 当前讲解这一步
{
webrtc::AudioState::Config config;
if (audiomixer) {
config.audiomixer = audio_mixer;
} else {
config.audiomixer = webrtc::AudioMixerImpl::Create();
}
config.audio_processing = apm
;
config.audiodevice_module = adm;
if (audioframe_processor)
config.asyncaudio_processing_factory =
new rtc::RefCountedObject(
*audio_frame_processor
, *taskqueue_factory);
audiostate = webrtc::AudioState::Create(config);
}
当前堆栈
image.png
—》

AudioState::Create

  1. rtc::scoped_refptr<AudioState> AudioState::Create(
  2. const AudioState::Config& config) {
  3. return new rtc::RefCountedObject<internal::AudioState>(config);
  4. }

—》

internal::AudioState::AudioState

  1. namespace webrtc {
  2. namespace internal {
  3. AudioState::AudioState(const AudioState::Config& config)
  4. : config_(config),
  5. audio_transport_(config_.audio_mixer,
  6. config_.audio_processing.get(),
  7. config_.async_audio_processing_factory.get()) {
  8. process_thread_checker_.Detach();
  9. RTC_DCHECK(config_.audio_mixer);
  10. RTC_DCHECK(config_.audio_device_module);
  11. }
  12. ****

image.png