Skip to content

Commit

Permalink
Adapt to WebRTC r6818
Browse files Browse the repository at this point in the history
Voice detection and level estimation support have been removed in
WebRTC audio processing.

Voice detection is disabled by macro ENABLE_WEBRTC_VOICEDETECTION.

Level estimation is disabled by macro ENABLE_WEBRTC_LEVELESTIMATION.
  • Loading branch information
bear101 committed Nov 7, 2024
1 parent d7389d8 commit ed67fcd
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 21 deletions.
4 changes: 2 additions & 2 deletions Library/TeamTalkLib/avstream/MediaPlayback.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -199,12 +199,12 @@ bool MediaPlayback::SetupSpeexPreprocess(bool enableagc, const SpeexAGC& agc,
bool MediaPlayback::SetupWebRTCPreprocess(const webrtc::AudioProcessing::Config& webrtc)
{
if (!m_apm)
m_apm.reset(webrtc::AudioProcessingBuilder().Create());
m_apm = webrtc::AudioProcessingBuilder().Create();

m_apm->ApplyConfig(webrtc);
if (m_apm->Initialize() != webrtc::AudioProcessing::kNoError)
{
m_apm.reset();
m_apm.release();
return false;
}
return true;
Expand Down
2 changes: 1 addition & 1 deletion Library/TeamTalkLib/avstream/MediaPlayback.h
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ class MediaPlayback : public soundsystem::StreamPlayer
std::shared_ptr<SpeexPreprocess> m_preprocess_left, m_preprocess_right;
#endif
#if defined(ENABLE_WEBRTC)
std::unique_ptr<webrtc::AudioProcessing> m_apm;
rtc::scoped_refptr<webrtc::AudioProcessing> m_apm;
#endif
bool m_last_callback = false;
ACE_Future<bool> m_drained;
Expand Down
6 changes: 3 additions & 3 deletions Library/TeamTalkLib/avstream/SoundLoopback.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ bool SoundLoopback::StartTest(int inputdevid, int outputdevid,
#if defined(ENABLE_WEBRTC)
if (IsEnabled(apm_cfg))
{
m_apm.reset(webrtc::AudioProcessingBuilder().Create());
m_apm = webrtc::AudioProcessingBuilder().Create();
if (!m_apm)
{
StopTest();
Expand Down Expand Up @@ -214,7 +214,7 @@ bool SoundLoopback::StartDuplexTest(int inputdevid, int outputdevid,
#if defined(ENABLE_WEBRTC)
if (IsEnabled(apm_cfg))
{
m_apm.reset(webrtc::AudioProcessingBuilder().Create());
m_apm = webrtc::AudioProcessingBuilder().Create();
if (!m_apm)
{
StopTest();
Expand Down Expand Up @@ -254,7 +254,7 @@ bool SoundLoopback::StopTest()
#endif

#if defined(ENABLE_WEBRTC)
m_apm.reset();
m_apm.release();
#endif
m_preprocess_buffer_left.clear();
m_preprocess_buffer_right.clear();
Expand Down
2 changes: 1 addition & 1 deletion Library/TeamTalkLib/avstream/SoundLoopback.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ class SoundLoopback
SpeexPreprocess m_preprocess_left, m_preprocess_right;
#endif
#if defined(ENABLE_WEBRTC)
std::unique_ptr<webrtc::AudioProcessing> m_apm;
rtc::scoped_refptr<webrtc::AudioProcessing> m_apm;
std::vector<short> m_prev_buffer;
#endif
std::vector<short> m_preprocess_buffer_left, m_preprocess_buffer_right;
Expand Down
9 changes: 7 additions & 2 deletions Library/TeamTalkLib/avstream/WebRTCPreprocess.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@
// webrtc::GainControlImpl queries this feature. Field trials is
// excluded by passing rtc_exclude_field_trial_default=true to GN.
namespace webrtc { namespace field_trial {
std::string FindFullName(const std::string& trial)
std::string FindFullName(absl::string_view trial_)
{
std::string trial(trial_);;
#if defined(UNICODE)
ACE_TString str = LocalToUnicode(trial.c_str());
#else
Expand Down Expand Up @@ -82,7 +83,7 @@ int WebRTCPreprocess(webrtc::AudioProcessing& apm, const media::AudioFrame& infr
}

// AudioProcessingStats
int output_rms_dbfs = 0;
int output_rms_dbfs = 0;
bool voice_detected = false;

int in_index = 0, out_index = 0, n = 0;
Expand Down Expand Up @@ -116,9 +117,11 @@ int WebRTCPreprocess(webrtc::AudioProcessing& apm, const media::AudioFrame& infr
if (stats)
{
auto wstats = apm.GetStatistics();
#if defined(ENABLE_WEBRTC_VOICEDETECTION)
output_rms_dbfs += wstats.output_rms_dbfs.value_or(0);
assert(!wstats.output_rms_dbfs.has_value() || wstats.output_rms_dbfs.value() <= 127);
assert(!wstats.output_rms_dbfs.has_value() || wstats.output_rms_dbfs.value() >= 0);
#endif
voice_detected |= wstats.voice_detected.value_or(false);
}

Expand All @@ -129,7 +132,9 @@ int WebRTCPreprocess(webrtc::AudioProcessing& apm, const media::AudioFrame& infr

if (stats && n > 0)
{
#if defined(ENABLE_WEBRTC_VOICEDETECTION)
stats->output_rms_dbfs = output_rms_dbfs / n;
#endif
stats->voice_detected = voice_detected;
}

Expand Down
2 changes: 1 addition & 1 deletion Library/TeamTalkLib/avstream/WebRTCPreprocess.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@
#define WEBRTCPREPROCESS_H

#include <mystd/MyStd.h>
#include <codec/MediaUtil.h>

#include <audio_processing.h>

#include <codec/MediaUtil.h>

bool IsEnabled(const webrtc::AudioProcessing::Config& cfg);

Expand Down
16 changes: 14 additions & 2 deletions Library/TeamTalkLib/bin/dll/Convert.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1067,17 +1067,23 @@ void Convert(const WebRTCAudioPreprocessor& webrtc, webrtc::AudioProcessing::Con
break;
}

#if defined(ENABLE_WEBRTC_VOICEDETECTION)
result.voice_detection.enabled = webrtc.voicedetection.bEnable;

#endif

result.gain_controller2.enabled = webrtc.gaincontroller2.bEnable;
result.gain_controller2.fixed_digital.gain_db = webrtc.gaincontroller2.fixeddigital.fGainDB;
result.gain_controller2.adaptive_digital.enabled = webrtc.gaincontroller2.adaptivedigital.bEnable;
#if defined(ENABLE_WEBRTC_GAINCONTROLLER2)
result.gain_controller2.adaptive_digital.initial_saturation_margin_db = webrtc.gaincontroller2.adaptivedigital.fInitialSaturationMarginDB;
result.gain_controller2.adaptive_digital.extra_saturation_margin_db = webrtc.gaincontroller2.adaptivedigital.fExtraSaturationMarginDB;
#endif
result.gain_controller2.adaptive_digital.max_gain_change_db_per_second = webrtc.gaincontroller2.adaptivedigital.fMaxGainChangeDBPerSecond;
result.gain_controller2.adaptive_digital.max_output_noise_level_dbfs = webrtc.gaincontroller2.adaptivedigital.fMaxOutputNoiseLevelDBFS;

#if defined(ENABLE_WEBRTC_LEVELESTIMATION)
result.level_estimation.enabled = webrtc.levelestimation.bEnable;
#endif
}

void Convert(const webrtc::AudioProcessing::Config& cfg, WebRTCAudioPreprocessor& result)
Expand All @@ -1086,21 +1092,27 @@ void Convert(const webrtc::AudioProcessing::Config& cfg, WebRTCAudioPreprocessor
result.preamplifier.fFixedGainFactor = cfg.pre_amplifier.fixed_gain_factor;

result.echocanceller.bEnable = cfg.echo_canceller.enabled;

result.noisesuppression.bEnable = cfg.noise_suppression.enabled;
result.noisesuppression.nLevel = cfg.noise_suppression.level;

#if defined(ENABLE_WEBRTC_VOICEDETECTION)
result.voicedetection.bEnable = cfg.voice_detection.enabled;
#endif

result.gaincontroller2.bEnable = cfg.gain_controller2.enabled;
result.gaincontroller2.fixeddigital.fGainDB = cfg.gain_controller2.fixed_digital.gain_db;
result.gaincontroller2.adaptivedigital.bEnable = cfg.gain_controller2.adaptive_digital.enabled;
#if defined(ENABLE_WEBRTC_GAINCONTROLLER2)
result.gaincontroller2.adaptivedigital.fInitialSaturationMarginDB = cfg.gain_controller2.adaptive_digital.initial_saturation_margin_db;
result.gaincontroller2.adaptivedigital.fExtraSaturationMarginDB = cfg.gain_controller2.adaptive_digital.extra_saturation_margin_db;
#endif
result.gaincontroller2.adaptivedigital.fMaxGainChangeDBPerSecond = cfg.gain_controller2.adaptive_digital.max_gain_change_db_per_second;
result.gaincontroller2.adaptivedigital.fMaxOutputNoiseLevelDBFS = cfg.gain_controller2.adaptive_digital.max_output_noise_level_dbfs;

#if defined(ENABLE_WEBRTC_LEVELESTIMATION)
result.levelestimation.bEnable = cfg.level_estimation.enabled;
#endif
}
#endif

Expand Down
17 changes: 9 additions & 8 deletions Library/TeamTalkLib/teamtalk/client/AudioThread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ void AudioThread::StopEncoder()
#endif

#if defined(ENABLE_WEBRTC)
m_apm.reset();
m_apm.release();
m_aps.reset();
#endif

Expand Down Expand Up @@ -235,7 +235,7 @@ bool AudioThread::UpdatePreprocessor(const teamtalk::AudioPreprocessor& preproce
#if defined(ENABLE_WEBRTC)
if (preprocess.preprocessor != AUDIOPREPROCESSOR_WEBRTC)
{
m_apm.reset();
m_apm.release();
m_aps.reset();
}
#endif
Expand Down Expand Up @@ -267,11 +267,11 @@ bool AudioThread::UpdatePreprocessor(const teamtalk::AudioPreprocessor& preproce
}

if (!m_apm)
m_apm.reset(webrtc::AudioProcessingBuilder().Create());
m_apm = webrtc::AudioProcessingBuilder().Create();
m_apm->ApplyConfig(preprocess.webrtc);
if (m_apm->Initialize() != webrtc::AudioProcessing::kNoError)
{
m_apm.reset();
m_apm.release();
MYTRACE(ACE_TEXT("Failed to initialize WebRTC audio preprocessor\n"));
return false;
}
Expand Down Expand Up @@ -424,7 +424,7 @@ void AudioThread::QueueAudio(ACE_Message_Block* mb_audio)

bool AudioThread::IsVoiceActive()
{
#if defined(ENABLE_WEBRTC)
#if defined(ENABLE_WEBRTC_VOICEDETECTION)
std::unique_lock<std::recursive_mutex> g(m_preprocess_lock);

if (m_apm && m_apm->GetConfig().voice_detection.enabled)
Expand All @@ -440,14 +440,13 @@ bool AudioThread::IsVoiceActive()

int AudioThread::GetCurrentVoiceLevel()
{
#if defined(ENABLE_WEBRTC)
#if defined(ENABLE_WEBRTC_LEVELESTIMATION)
std::unique_lock<std::recursive_mutex> g(m_preprocess_lock);

if (m_apm)
{
assert(m_aps);
auto cfg = m_apm->GetConfig();
if (cfg.level_estimation.enabled)
if (m_apm->GetConfig().level_estimation.enabled)
{
// WebRTC's maximum value for dB from digital full scale
float value = 127.f - m_aps->output_rms_dbfs.value_or(0);
Expand Down Expand Up @@ -693,13 +692,15 @@ void AudioThread::PreprocessWebRTC(media::AudioFrame& audblock, bool& vad)
MYTRACE(ACE_TEXT("WebRTC failed to process audio\n"));
}

#if defined(ENABLE_WEBRTC_VOICEDETECTION)
vad = m_apm->GetConfig().voice_detection.enabled;
if (vad)
{
assert(m_aps);
if (m_aps->voice_detected.value_or(false))
m_lastActive = ACE_OS::gettimeofday();
}
#endif
}
#endif

Expand Down
2 changes: 1 addition & 1 deletion Library/TeamTalkLib/teamtalk/client/AudioThread.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ class AudioThread : protected ACE_Task<ACE_MT_SYNCH>
std::unique_ptr<SpeexPreprocess> m_preprocess_left, m_preprocess_right;
#endif
#if defined(ENABLE_WEBRTC)
std::unique_ptr<webrtc::AudioProcessing> m_apm;
rtc::scoped_refptr<webrtc::AudioProcessing> m_apm;
std::unique_ptr<webrtc::AudioProcessingStats> m_aps;
#endif
#if defined(ENABLE_SPEEX)
Expand Down

0 comments on commit ed67fcd

Please sign in to comment.