| 6 |
| 7 | #include "logging.h" |
| 8 | #include "EchoCanceller.h" |
| 9 | #include "VoIPServerConfig.h" |
| 10 | #include "audio/AudioInput.h" |
| 11 | #include "audio/AudioOutput.h" |
| 12 |
| 13 | #ifndef TGVOIP_NO_DSP |
| 14 | #include "webrtc_dsp/api/audio/audio_frame.h" |
| 15 | #include "webrtc_dsp/modules/audio_processing/include/audio_processing.h" |
| 16 | #endif |
| 17 |
| 18 | #include <cmath> |
| 19 | #include <cstdio> |
| 20 | #include <cstring> |
| 21 | #include <limits> |
| 22 |
| 23 | using namespace tgvoip; |
| 24 |
| 25 | EchoCanceller::EchoCanceller(bool enableAEC, bool enableNS, bool enableAGC) |
| 26 | : m_enableAEC(enableAEC) |
| 27 | , m_enableAGC(enableAGC) |
| 28 | , m_enableNS(enableNS) |
| 29 | , m_isOn(true) |
| 30 | { |
| 31 | #ifndef TGVOIP_NO_DSP |
| 32 | webrtc::Config extraConfig; |
| 33 | #ifdef TGVOIP_USE_DESKTOP_DSP |
| 34 | extraConfig.Set(new webrtc::DelayAgnostic(true)); |
| 35 | #endif |
| 36 |
| 37 | m_apm = webrtc::AudioProcessingBuilder().Create(extraConfig); |
| 38 |
| 39 | webrtc::AudioProcessing::Config config; |
| 40 | config.echo_canceller.enabled = enableAEC; |
| 41 | #ifndef TGVOIP_USE_DESKTOP_DSP |
| 42 | config.echo_canceller.mobile_mode = true; |
| 43 | #else |
| 44 | config.echo_canceller.mobile_mode = false; |
| 45 | #endif |
| 46 | config.high_pass_filter.enabled = enableAEC; |
| 47 | config.gain_controller2.enabled = enableAGC; |
| 48 | m_apm->ApplyConfig(config); |
| 49 |
| 50 | webrtc::NoiseSuppression::Level nsLevel; |
| 51 | #ifdef __APPLE__ |
| 52 | switch (ServerConfig::GetSharedInstance()->GetInt("webrtc_ns_level_vpio", 0)) |
| 53 | { |
| 54 | #else |
| 55 | switch (ServerConfig::GetSharedInstance()->GetInt("webrtc_ns_level", 2)) |
| 56 | { |
| 57 | #endif |
| 58 | case 0: |
| 59 | nsLevel = webrtc::NoiseSuppression::Level::kLow; |
| 60 | break; |
| 61 | case 1: |
| 62 | nsLevel = webrtc::NoiseSuppression::Level::kModerate; |
| 63 | break; |
| 64 | case 3: |
| 65 | nsLevel = webrtc::NoiseSuppression::Level::kVeryHigh; |
| 66 | break; |
| 67 | case 2: |
| 68 | default: |
| 69 | nsLevel = webrtc::NoiseSuppression::Level::kHigh; |
| 70 | break; |
| 71 | } |
| 72 | m_apm->noise_suppression()->set_level(nsLevel); |
| 73 | m_apm->noise_suppression()->Enable(enableNS); |
| 74 | if (enableAGC) |
| 75 | { |
| 76 | m_apm->gain_control()->set_mode(webrtc::GainControl::Mode::kAdaptiveDigital); |
| 77 | m_apm->gain_control()->set_target_level_dbfs(ServerConfig::GetSharedInstance()->GetInt("webrtc_agc_target_level", 9)); |
| 78 | m_apm->gain_control()->enable_limiter(ServerConfig::GetSharedInstance()->GetBoolean("webrtc_agc_enable_limiter", true)); |
| 79 | m_apm->gain_control()->set_compression_gain_db(ServerConfig::GetSharedInstance()->GetInt("webrtc_agc_compression_gain", 20)); |
| 80 | } |
| 81 | m_apm->voice_detection()->set_likelihood(webrtc::VoiceDetection::Likelihood::kVeryLowLikelihood); |
| 82 |
| 83 | m_audioFrame = new webrtc::AudioFrame(); |
| 84 | m_audioFrame->samples_per_channel_ = 480; |
| 85 | m_audioFrame->sample_rate_hz_ = 48000; |
| 86 | m_audioFrame->num_channels_ = 1; |
| 87 |
| 88 | m_farendQueue = new BlockingQueue<Buffer>(11); |
| 89 | m_running = true; |
| 90 | m_bufferFarendThread = new Thread(std::bind(&EchoCanceller::RunBufferFarendThread, this)); |
| 91 | m_bufferFarendThread->SetName("VoipECBufferFarEnd"); |
| 92 | m_bufferFarendThread->Start(); |
| 93 |
| 94 | #else |
| 95 | this->enableAEC = this->enableAGC = enableAGC = this->enableNS = enableNS = false; |
| 96 | isOn = true; |
| 97 | #endif |
| 98 | } |
| 99 |
| 100 | EchoCanceller::~EchoCanceller() |
| 101 | { |
| 102 | #ifndef TGVOIP_NO_DSP |
| 103 | m_farendQueue->Put(Buffer()); |
| 104 | m_bufferFarendThread->Join(); |
| 105 | delete m_bufferFarendThread; |
| 106 | delete m_farendQueue; |
| 107 | delete m_audioFrame; |
| 108 | delete m_apm; |
| 109 | #endif |
| 110 | } |
| 111 |
| 112 | void EchoCanceller::Start() |
| 113 | { |
| 114 | } |
| 115 |
| 116 | void EchoCanceller::Stop() |
| 117 | { |
| 118 | } |
| 119 |
| 120 | void EchoCanceller::SpeakerOutCallback(std::uint8_t* data, std::size_t len) |
| 121 | { |
| 122 | if (len != 960 * 2 || !m_enableAEC || !m_isOn) |
| 123 | return; |
| 124 | #ifndef TGVOIP_NO_DSP |
| 125 | try |
| 126 | { |
| 127 | Buffer buf = m_farendBufferPool.Get(); |
| 128 | buf.CopyFrom(data, 0, 960 * 2); |
| 129 | m_farendQueue->Put(std::move(buf)); |
| 130 | } |
| 131 | catch (const std::bad_alloc& exception) |
| 132 | { |
| 133 | LOGW("Echo canceller can't keep up with real time.\nwhat():\n%s", exception.what()); |
| 134 | } |
| 135 | #endif |
| 136 | } |
| 137 |
| 138 | #ifndef TGVOIP_NO_DSP |
| 139 | void EchoCanceller::RunBufferFarendThread() |
| 140 | { |
| 141 | webrtc::AudioFrame frame; |
| 142 | frame.num_channels_ = 1; |
| 143 | frame.sample_rate_hz_ = 48000; |
| 144 | frame.samples_per_channel_ = 480; |
| 145 | while (m_running) |
| 146 | { |
| 147 | Buffer buf = m_farendQueue->GetBlocking(); |
| 148 | if (buf.IsEmpty()) |
| 149 | { |
| 150 | LOGI("Echo canceller buffer farend thread exiting"); |
| 151 | return; |
| 152 | } |
| 153 | std::int16_t* samplesIn = reinterpret_cast<std::int16_t*>(*buf); |
| 154 | std::memcpy(frame.mutable_data(), samplesIn, 480 * 2); |
| 155 | m_apm->ProcessReverseStream(&frame); |
| 156 | std::memcpy(frame.mutable_data(), samplesIn + 480, 480 * 2); |
| 157 | m_apm->ProcessReverseStream(&frame); |
| 158 | m_didBufferFarend = true; |
| 159 | } |
| 160 | } |
| 161 | #endif |
| 162 |
| 163 | void EchoCanceller::Enable(bool enabled) |
| 164 | { |
| 165 | m_isOn = enabled; |
| 166 | } |
| 167 |
| 168 | void EchoCanceller::ProcessInput(std::int16_t* inOut, std::size_t numSamples, bool& hasVoice) |
| 169 | { |
| 170 | #ifndef TGVOIP_NO_DSP |
| 171 | if (!m_isOn || (!m_enableAEC && !m_enableAGC && !m_enableNS)) |
| 172 | return; |
| 173 |
| 174 | int delay = audio::AudioInput::GetEstimatedDelay() + audio::AudioOutput::GetEstimatedDelay(); |
| 175 | assert(numSamples == 960); |
| 176 |
| 177 | std::memcpy(m_audioFrame->mutable_data(), inOut, 480 * 2); |
| 178 | if (m_enableAEC) |
| 179 | m_apm->set_stream_delay_ms(delay); |
| 180 |
| 181 | m_apm->ProcessStream(m_audioFrame); |
| 182 |
| 183 | if (m_enableVAD) |
| 184 | hasVoice = m_apm->voice_detection()->stream_has_voice(); |
| 185 | std::memcpy(inOut, m_audioFrame->data(), 480 * 2); |
| 186 | std::memcpy(m_audioFrame->mutable_data(), inOut + 480, 480 * 2); |
| 187 | if (m_enableAEC) |
| 188 | m_apm->set_stream_delay_ms(delay); |
| 189 |
| 190 | m_apm->ProcessStream(m_audioFrame); |
| 191 |
| 192 | if (m_enableVAD) |
| 193 | hasVoice = hasVoice || m_apm->voice_detection()->stream_has_voice(); |
| 194 | std::memcpy(inOut + 480, m_audioFrame->data(), 480 * 2); |
| 195 | #endif |
| 196 | } |
| 197 |
| 198 | void EchoCanceller::SetAECStrength(int strength) |
| 199 | { |
| 200 | #ifndef TGVOIP_NO_DSP |
| 201 | #endif |
| 202 | } |
| 203 |
| 204 | void EchoCanceller::SetVoiceDetectionEnabled(bool enabled) |
| 205 | { |
| 206 | m_enableVAD = enabled; |
| 207 | #ifndef TGVOIP_NO_DSP |
| 208 | m_apm->voice_detection()->Enable(enabled); |
| 209 | #endif |
| 210 | } |
| 211 |
| 212 | using namespace tgvoip::effects; |
| 213 |
| 214 | AudioEffect::~AudioEffect() = default; |
| 215 |
| 216 | void AudioEffect::SetPassThrough(bool passThrough) |
| 217 | { |
| 218 | m_passThrough = passThrough; |
| 219 | } |
| 220 |
| 221 | bool AudioEffect::GetPassThrough() const |
| 222 | { |
| 223 | return m_passThrough; |
| 224 | } |
| 225 |
| 226 | Volume::Volume() = default; |
| 227 |
| 228 | Volume::~Volume() = default; |
| 229 |
| 230 | void Volume::Process(std::int16_t* inOut, std::size_t numSamples) const |
| 231 | { |
| 232 | if (m_level == 1.0f || GetPassThrough()) |
| 233 | { |
| 234 | return; |
| 235 | } |
| 236 | for (std::size_t i = 0; i < numSamples; ++i) |
| 237 | { |
| 238 | float sample = inOut[i] * m_multiplier; |
| 239 | if (sample > 32767.0f) |
| 240 | inOut[i] = std::numeric_limits<std::int16_t>::max(); |
| 241 | else if (sample < -32768.0f) |
| 242 | inOut[i] = std::numeric_limits<std::int16_t>::min(); |
| 243 | else |
| 244 | inOut[i] = static_cast<std::int16_t>(sample); |
| 245 | } |
| 246 | } |
| 247 |
| 248 | void Volume::SetLevel(float level) |
| 249 | { |
| 250 | m_level = level; |
| 251 | float db; |
| 252 | if (level < 1.0f) |
| 253 | db = -50.0f * (1.0f - level); |
| 254 | else if (level > 1.0f && level <= 2.0f) |
| 255 | db = 10.0f * (level - 1.0f); |
| 256 | else |
| 257 | db = 0.0f; |
| 258 | m_multiplier = expf(db / 20.0f * logf(10.0f)); |
| 259 | } |
| 260 |
| 261 | float Volume::GetLevel() const |
| 262 | { |
| 263 | return m_level; |
| 264 | } |
| 265 |