diff --git a/Plugin~/WebRTCPlugin/Context.cpp b/Plugin~/WebRTCPlugin/Context.cpp index 5ed9572c45..7b5b0d2b44 100644 --- a/Plugin~/WebRTCPlugin/Context.cpp +++ b/Plugin~/WebRTCPlugin/Context.cpp @@ -244,11 +244,10 @@ namespace webrtc rtc::scoped_refptr Context::CreateAudioSource() { - // avoid optimization specially for voice cricket::AudioOptions audioOptions; - audioOptions.auto_gain_control = false; - audioOptions.noise_suppression = false; + audioOptions.noise_suppression = true; audioOptions.highpass_filter = false; + audioOptions.auto_gain_control = true; return UnityAudioTrackSource::Create(audioOptions); } diff --git a/Plugin~/WebRTCPlugin/UnityAudioTrackSource.cpp b/Plugin~/WebRTCPlugin/UnityAudioTrackSource.cpp index 926def49d8..70aa88fd3d 100644 --- a/Plugin~/WebRTCPlugin/UnityAudioTrackSource.cpp +++ b/Plugin~/WebRTCPlugin/UnityAudioTrackSource.cpp @@ -1,6 +1,7 @@ #include "pch.h" #include +#include #include #include "UnityAudioTrackSource.h" @@ -68,6 +69,16 @@ namespace webrtc while (_convertedAudioData.size() >= nNumSamplesFor10ms) { + if (_audioProcessing) + { + StreamConfig streamConfig(nSampleRate, nNumChannels); + _audioProcessing->ProcessStream( + _convertedAudioData.data(), + streamConfig, + streamConfig, + _convertedAudioData.data()); + } + for (auto sink : _arrSink) sink->OnData(_convertedAudioData.data(), nBitPerSample, nSampleRate, nNumChannels, nNumFramesFor10ms); _convertedAudioData.erase(_convertedAudioData.begin(), _convertedAudioData.begin() + nNumSamplesFor10ms); @@ -78,6 +89,30 @@ namespace webrtc UnityAudioTrackSource::UnityAudioTrackSource(const cricket::AudioOptions& audio_options) : _options(audio_options) { + // Build and configure the APM so that the options are actually applied. + // cricket::AudioOptions are stored as metadata by LocalAudioSource but are + // never wired into any real processing — PushAudioData() bypasses the WebRTC + // voice engine entirely, so we must drive the APM ourselves here. + AudioProcessing::Config apmConfig; + + apmConfig.noise_suppression.enabled = + audio_options.noise_suppression.value_or(true); + apmConfig.noise_suppression.level = + AudioProcessing::Config::NoiseSuppression::kHigh; + + apmConfig.gain_controller1.enabled = + audio_options.auto_gain_control.value_or(true); + apmConfig.gain_controller1.mode = + AudioProcessing::Config::GainController1::kAdaptiveDigital; + + apmConfig.high_pass_filter.enabled = + audio_options.highpass_filter.value_or(false); + + apmConfig.echo_canceller.enabled = + audio_options.echo_cancellation.value_or(false); + + _audioProcessing = AudioProcessingBuilder().Create(); + _audioProcessing->ApplyConfig(apmConfig); } UnityAudioTrackSource::~UnityAudioTrackSource() { } diff --git a/Plugin~/WebRTCPlugin/UnityAudioTrackSource.h b/Plugin~/WebRTCPlugin/UnityAudioTrackSource.h index 7fc4a874eb..803118b092 100644 --- a/Plugin~/WebRTCPlugin/UnityAudioTrackSource.h +++ b/Plugin~/WebRTCPlugin/UnityAudioTrackSource.h @@ -3,6 +3,7 @@ #include #include +#include #include namespace unity @@ -34,6 +35,7 @@ namespace webrtc std::vector _arrSink; std::mutex _mutex; cricket::AudioOptions _options; + rtc::scoped_refptr _audioProcessing; int _sampleRate = 0; size_t _numChannels = 0; size_t _numFrames = 0;