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