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