| 6 |
| 7 | #ifndef TGVOIP_NO_DSP |
| 8 | #include "modules/audio_processing/include/audio_processing.h" |
| 9 | #include "api/audio/audio_frame.h" |
| 10 | #endif |
| 11 |
| 12 | #include "EchoCanceller.h" |
| 13 | #include "audio/AudioOutput.h" |
| 14 | #include "audio/AudioInput.h" |
| 15 | #include "logging.h" |
| 16 | #include "VoIPServerConfig.h" |
| 17 | #include <string.h> |
| 18 | #include <stdio.h> |
| 19 | #include <math.h> |
| 20 |
| 21 | using namespace tgvoip; |
| 22 |
| 23 | EchoCanceller::EchoCanceller(bool enableAEC, bool enableNS, bool enableAGC){ |
| 24 | #ifndef TGVOIP_NO_DSP |
| 25 | this->enableAEC=enableAEC; |
| 26 | this->enableAGC=enableAGC; |
| 27 | this->enableNS=enableNS; |
| 28 | isOn=true; |
| 29 |
| 30 | webrtc::Config extraConfig; |
| 31 |
| 32 | apm=webrtc::AudioProcessingBuilder().Create(extraConfig); |
| 33 |
| 34 | webrtc::AudioProcessing::Config config; |
| 35 | config.echo_canceller.enabled = enableAEC; |
| 36 | #ifndef TGVOIP_USE_DESKTOP_DSP |
| 37 | config.echo_canceller.mobile_mode = true; |
| 38 | #else |
| 39 | config.echo_canceller.mobile_mode = false; |
| 40 | #endif |
| 41 | config.high_pass_filter.enabled = enableAEC; |
| 42 | config.gain_controller2.enabled = enableAGC; |
| 43 |
| 44 | using Level = webrtc::AudioProcessing::Config::NoiseSuppression::Level; |
| 45 | Level nsLevel; |
| 46 | #ifdef __APPLE__ |
| 47 | switch(ServerConfig::GetSharedInstance()->GetInt("webrtc_ns_level_vpio", 0)){ |
| 48 | #else |
| 49 | switch (ServerConfig::GetSharedInstance()->GetInt("webrtc_ns_level", 2)) { |
| 50 | #endif |
| 51 | case 0: |
| 52 | nsLevel=Level::kLow; |
| 53 | break; |
| 54 | case 1: |
| 55 | nsLevel=Level::kModerate; |
| 56 | break; |
| 57 | case 3: |
| 58 | nsLevel=Level::kVeryHigh; |
| 59 | break; |
| 60 | case 2: |
| 61 | default: |
| 62 | nsLevel=Level::kHigh; |
| 63 | break; |
| 64 | } |
| 65 | config.noise_suppression.level = nsLevel; |
| 66 | config.noise_suppression.enabled = enableNS; |
| 67 | if (enableAGC) { |
| 68 | config.gain_controller1.mode = webrtc::AudioProcessing::Config::GainController1::kAdaptiveDigital; |
| 69 | config.gain_controller1.target_level_dbfs = ServerConfig::GetSharedInstance()->GetInt("webrtc_agc_target_level", 9); |
| 70 | config.gain_controller1.enable_limiter = ServerConfig::GetSharedInstance()->GetBoolean("webrtc_agc_enable_limiter", true); |
| 71 | config.gain_controller1.compression_gain_db = ServerConfig::GetSharedInstance()->GetInt("webrtc_agc_compression_gain", 20); |
| 72 | } |
| 73 | config.voice_detection.enabled = true; |
| 74 |
| 75 | apm->ApplyConfig(config); |
| 76 |
| 77 | audioFrame = new webrtc::AudioFrame(); |
| 78 | audioFrame->samples_per_channel_ = 480; |
| 79 | audioFrame->sample_rate_hz_ = 48000; |
| 80 | audioFrame->num_channels_ = 1; |
| 81 |
| 82 | farendQueue = new BlockingQueue<int16_t *>(11); |
| 83 | farendBufferPool = new BufferPool(960 * 2, 10); |
| 84 | running = true; |
| 85 | bufferFarendThread = new Thread(std::bind(&EchoCanceller::RunBufferFarendThread, this)); |
| 86 | bufferFarendThread->Start(); |
| 87 |
| 88 | #else |
| 89 | this->enableAEC=this->enableAGC=enableAGC=this->enableNS=enableNS=false; |
| 90 | isOn=true; |
| 91 | #endif |
| 92 | } |
| 93 |
| 94 | EchoCanceller::~EchoCanceller() { |
| 95 | #ifndef TGVOIP_NO_DSP |
| 96 | delete apm; |
| 97 | delete audioFrame; |
| 98 | delete farendBufferPool; |
| 99 | #endif |
| 100 | } |
| 101 |
| 102 | void EchoCanceller::Start() { |
| 103 |
| 104 | } |
| 105 |
| 106 | void EchoCanceller::Stop() { |
| 107 |
| 108 | } |
| 109 |
| 110 | void EchoCanceller::SpeakerOutCallback(unsigned char* data, size_t len) { |
| 111 | if (len != 960 * 2 || !enableAEC || !isOn) |
| 112 | return; |
| 113 | #ifndef TGVOIP_NO_DSP |
| 114 | int16_t *buf = (int16_t *) farendBufferPool->Get(); |
| 115 | if (buf) { |
| 116 | memcpy(buf, data, 960 * 2); |
| 117 | farendQueue->Put(buf); |
| 118 | } |
| 119 | #endif |
| 120 | } |
| 121 |
| 122 | #ifndef TGVOIP_NO_DSP |
| 123 | void EchoCanceller::RunBufferFarendThread() { |
| 124 | webrtc::AudioFrame frame; |
| 125 | frame.num_channels_ = 1; |
| 126 | frame.sample_rate_hz_ = 48000; |
| 127 | frame.samples_per_channel_ = 480; |
| 128 |
| 129 | webrtc::StreamConfig input_config(frame.sample_rate_hz_, frame.num_channels_, |
| 130 | /*has_keyboard=*/false); |
| 131 | webrtc::StreamConfig output_config(frame.sample_rate_hz_, frame.num_channels_, |
| 132 | /*has_keyboard=*/false); |
| 133 |
| 134 | while (running) { |
| 135 | int16_t *samplesIn = farendQueue->GetBlocking(); |
| 136 | if (samplesIn) { |
| 137 | memcpy(frame.mutable_data(), samplesIn, 480 * 2); |
| 138 | apm->ProcessReverseStream(frame.data(), input_config, |
| 139 | output_config, frame.mutable_data()); |
| 140 | memcpy(frame.mutable_data(), samplesIn + 480, 480 * 2); |
| 141 | apm->ProcessReverseStream(frame.data(), input_config, |
| 142 | output_config, frame.mutable_data()); |
| 143 | didBufferFarend = true; |
| 144 | farendBufferPool->Reuse(reinterpret_cast<unsigned char *>(samplesIn)); |
| 145 | } |
| 146 | } |
| 147 | } |
| 148 | #endif |
| 149 |
| 150 | void EchoCanceller::Enable(bool enabled) { |
| 151 | isOn = enabled; |
| 152 | } |
| 153 |
| 154 | void EchoCanceller::ProcessInput(int16_t* inOut, size_t numSamples, bool& hasVoice) { |
| 155 | #ifndef TGVOIP_NO_DSP |
| 156 | if (!isOn || (!enableAEC && !enableAGC && !enableNS)) { |
| 157 | return; |
| 158 | } |
| 159 | int delay = audio::AudioInput::GetEstimatedDelay() + audio::AudioOutput::GetEstimatedDelay(); |
| 160 | assert(numSamples == 960); |
| 161 |
| 162 | webrtc::StreamConfig input_config(audioFrame->sample_rate_hz_, audioFrame->num_channels_, |
| 163 | /*has_keyboard=*/false); |
| 164 | webrtc::StreamConfig output_config(audioFrame->sample_rate_hz_, audioFrame->num_channels_, |
| 165 | /*has_keyboard=*/false); |
| 166 |
| 167 | memcpy(audioFrame->mutable_data(), inOut, 480 * 2); |
| 168 | if (enableAEC) |
| 169 | apm->set_stream_delay_ms(delay); |
| 170 | apm->ProcessStream(audioFrame->data(), input_config, |
| 171 | output_config, audioFrame->mutable_data()); |
| 172 | if (enableVAD) |
| 173 | hasVoice= apm->GetStatistics().voice_detected.value_or(false); |
| 174 | memcpy(inOut, audioFrame->data(), 480 * 2); |
| 175 | memcpy(audioFrame->mutable_data(), inOut + 480, 480 * 2); |
| 176 | if (enableAEC) |
| 177 | apm->set_stream_delay_ms(delay); |
| 178 | apm->ProcessStream(audioFrame->data(), input_config, |
| 179 | output_config, audioFrame->mutable_data()); |
| 180 | if (enableVAD) { |
| 181 | hasVoice=hasVoice || apm->GetStatistics().voice_detected.value_or(false); |
| 182 | } |
| 183 | memcpy(inOut + 480, audioFrame->data(), 480 * 2); |
| 184 | #endif |
| 185 | } |
| 186 |
| 187 | void EchoCanceller::SetAECStrength(int strength) { |
| 188 | #ifndef TGVOIP_NO_DSP |
| 189 | /*if(aec){ |
| 190 | #ifndef TGVOIP_USE_DESKTOP_DSP |
| 191 | AecmConfig cfg; |
| 192 | cfg.cngMode=AecmFalse; |
| 193 | cfg.echoMode=(int16_t) strength; |
| 194 | WebRtcAecm_set_config(aec, cfg); |
| 195 | #endif |
| 196 | }*/ |
| 197 | #endif |
| 198 | } |
| 199 |
| 200 | void EchoCanceller::SetVoiceDetectionEnabled(bool enabled) { |
| 201 | enableVAD = enabled; |
| 202 | #ifndef TGVOIP_NO_DSP |
| 203 | auto config = apm->GetConfig(); |
| 204 | config.voice_detection.enabled = enabled; |
| 205 | apm->ApplyConfig(config); |
| 206 | #endif |
| 207 | } |
| 208 |
| 209 | using namespace tgvoip::effects; |
| 210 |
| 211 | AudioEffect::~AudioEffect() { |
| 212 |
| 213 | } |
| 214 |
| 215 | void AudioEffect::SetPassThrough(bool passThrough) { |
| 216 | this->passThrough = passThrough; |
| 217 | } |
| 218 |
| 219 | Volume::Volume() { |
| 220 |
| 221 | } |
| 222 |
| 223 | Volume::~Volume() { |
| 224 |
| 225 | } |
| 226 |
| 227 | void Volume::Process(int16_t* inOut, size_t numSamples) { |
| 228 | if (level == 1.0f || passThrough) { |
| 229 | return; |
| 230 | } |
| 231 | for (size_t i = 0; i < numSamples; i++) { |
| 232 | float sample = (float) inOut[i] * multiplier; |
| 233 | if (sample > 32767.0f) |
| 234 | inOut[i] = INT16_MAX; |
| 235 | else if (sample < -32768.0f) |
| 236 | inOut[i] = INT16_MIN; |
| 237 | else |
| 238 | inOut[i] = (int16_t) sample; |
| 239 | } |
| 240 | } |
| 241 |
| 242 | void Volume::SetLevel(float level) { |
| 243 | this->level = level; |
| 244 | float db; |
| 245 | if (level < 1.0f) |
| 246 | db = -50.0f * (1.0f - level); |
| 247 | else if (level > 1.0f && level <= 2.0f) |
| 248 | db = 10.0f * (level - 1.0f); |
| 249 | else |
| 250 | db = 0.0f; |
| 251 | multiplier = expf(db / 20.0f * logf(10.0f)); |
| 252 | } |
| 253 |
| 254 | float Volume::GetLevel() { |
| 255 | return level; |
| 256 | } |
| 257 |