Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update libtgvoip for latest version #61

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion libtgvoip/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,33 @@ bld/
[Bb]in/
[Oo]bj/
DerivedData/
*.o

# Visual Studio 2015 cache/options directory
.vs/

xcuserdata/
autom4te.cache/

# autoconf build files
/Makefile
/Makefile.in
/aclocal.m4
/autom4te.cache/
/compile
/config.*
/configure
/depcomp
/install-sh
/libtool
/ltmain.sh
/m4/
/missing
/stamp-h?
.deps/
.dirstamp
.libs/
*.l[ao]
*~

# generated pkg-config file
/tgvoip.pc
3 changes: 3 additions & 0 deletions libtgvoip/.gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "cmake"]
path = cmake
url = https://github.com/desktop-app/cmake_helpers.git
1 change: 1 addition & 0 deletions libtgvoip/Android.mk
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ LOCAL_SRC_FILES := \
./audio/AudioIO.cpp \
./video/VideoRenderer.cpp \
./video/VideoSource.cpp \
./video/ScreamCongestionController.cpp \
./os/android/VideoSourceAndroid.cpp \
./os/android/VideoRendererAndroid.cpp \
./client/android/tg_voip_jni.cpp
Expand Down
4 changes: 2 additions & 2 deletions libtgvoip/BlockingQueue.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
#include "threading.h"
#include "utils.h"

using namespace std;

namespace tgvoip{

using namespace std;

template<typename T>
class BlockingQueue{
public:
Expand Down
4 changes: 4 additions & 0 deletions libtgvoip/Buffers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@ void BufferInputStream::EnsureEnoughRemaining(size_t need){

BufferOutputStream::BufferOutputStream(size_t size){
buffer=(unsigned char*) malloc(size);
if(!buffer)
throw std::bad_alloc();
offset=0;
this->size=size;
bufferProvided=false;
Expand Down Expand Up @@ -217,6 +219,8 @@ void BufferOutputStream::ExpandBufferIfNeeded(size_t need){
buffer=(unsigned char *) realloc(buffer, size+need);
size+=need;
}
if(!buffer)
throw std::bad_alloc();
}
}

Expand Down
6 changes: 5 additions & 1 deletion libtgvoip/Buffers.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ namespace tgvoip{
size_t GetLength();
void Reset();
void Rewind(size_t numBytes);

BufferOutputStream& operator=(BufferOutputStream&& other){
if(this!=&other){
if(!bufferProvided && buffer)
Expand Down Expand Up @@ -265,6 +265,10 @@ namespace tgvoip{
_i=size+_i;
return data[_i];
}

size_t Size(){
return size;
}
private:
std::array<T, size> data;
ptrdiff_t offset=0;
Expand Down
13 changes: 10 additions & 3 deletions libtgvoip/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ add_library(libtgvoip STATIC
OpusEncoder.cpp
OpusEncoder.h
threading.h
TgVoip.cpp
TgVoip.h
VoIPController.cpp
VoIPGroupController.cpp
VoIPController.h
Expand All @@ -46,6 +48,8 @@ add_library(libtgvoip STATIC
MessageThread.h
audio/AudioIO.cpp
audio/AudioIO.h
video/ScreamCongestionController.cpp
video/ScreamCongestionController.h
video/VideoSource.cpp
video/VideoSource.h
video/VideoRenderer.cpp
Expand Down Expand Up @@ -648,7 +652,8 @@ add_library(libtgvoip STATIC
audio/SoftwareAudioOutput.h
audio/SoftwareAudioOutput.cpp)

set_property(TARGET libtgvoip PROPERTY CXX_STANDARD 11)
# FIXME: last libtgvoip looks adapted to C++20
set_property(TARGET libtgvoip PROPERTY CXX_STANDARD 14)

target_include_directories(libtgvoip PRIVATE
webrtc_dsp
Expand All @@ -666,8 +671,10 @@ target_compile_definitions(libtgvoip PRIVATE

target_compile_definitions(libtgvoip PUBLIC
# Chose one of NO_DSP / DESKTOP_DSP
# TGVOIP_NO_DSP
TGVOIP_USE_DESKTOP_DSP
# FIXME: desktop DSP cannot be used since last libtgvoip update
# since libtgvoip codebase is tighten to external webrtc
TGVOIP_NO_DSP
# TGVOIP_USE_DESKTOP_DSP
TGVOIP_USE_SOFTWARE_AUDIO)

if (${spdlog_FOUND})
Expand Down
78 changes: 58 additions & 20 deletions libtgvoip/EchoCanceller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
//

#ifndef TGVOIP_NO_DSP
#include "webrtc_dsp/modules/audio_processing/include/audio_processing.h"
#include "webrtc_dsp/api/audio/audio_frame.h"
#include "modules/audio_processing/include/audio_processing.h"
#include "modules/audio_processing/include/audio_frame_proxies.h"
#include "api/audio/audio_frame.h"
#endif

#include "EchoCanceller.h"
Expand All @@ -27,12 +28,14 @@ EchoCanceller::EchoCanceller(bool enableAEC, bool enableNS, bool enableAGC){
this->enableNS=enableNS;
isOn=true;

#ifdef TGVOIP_USE_DESKTOP_DSP_BUNDLED
webrtc::Config extraConfig;
#ifdef TGVOIP_USE_DESKTOP_DSP
extraConfig.Set(new webrtc::DelayAgnostic(true));
#endif

apm=webrtc::AudioProcessingBuilder().Create(extraConfig);
#else
apm=webrtc::AudioProcessingBuilder().Create();
#endif

webrtc::AudioProcessing::Config config;
config.echo_canceller.enabled = enableAEC;
Expand All @@ -43,28 +46,35 @@ EchoCanceller::EchoCanceller(bool enableAEC, bool enableNS, bool enableAGC){
#endif
config.high_pass_filter.enabled = enableAEC;
config.gain_controller2.enabled = enableAGC;

#ifdef TGVOIP_USE_DESKTOP_DSP_BUNDLED
apm->ApplyConfig(config);

webrtc::NoiseSuppression::Level nsLevel;

using Level = webrtc::NoiseSuppression::Level;
#else
using Level = webrtc::AudioProcessing::Config::NoiseSuppression::Level;
#endif
Level nsLevel;
#ifdef __APPLE__
switch(ServerConfig::GetSharedInstance()->GetInt("webrtc_ns_level_vpio", 0)){
#else
switch(ServerConfig::GetSharedInstance()->GetInt("webrtc_ns_level", 2)){
#endif
case 0:
nsLevel=webrtc::NoiseSuppression::Level::kLow;
nsLevel=Level::kLow;
break;
case 1:
nsLevel=webrtc::NoiseSuppression::Level::kModerate;
nsLevel=Level::kModerate;
break;
case 3:
nsLevel=webrtc::NoiseSuppression::Level::kVeryHigh;
nsLevel=Level::kVeryHigh;
break;
case 2:
default:
nsLevel=webrtc::NoiseSuppression::Level::kHigh;
nsLevel=Level::kHigh;
break;
}
#ifdef TGVOIP_USE_DESKTOP_DSP_BUNDLED
apm->noise_suppression()->set_level(nsLevel);
apm->noise_suppression()->Enable(enableNS);
if(enableAGC){
Expand All @@ -74,6 +84,19 @@ EchoCanceller::EchoCanceller(bool enableAEC, bool enableNS, bool enableAGC){
apm->gain_control()->set_compression_gain_db(ServerConfig::GetSharedInstance()->GetInt("webrtc_agc_compression_gain", 20));
}
apm->voice_detection()->set_likelihood(webrtc::VoiceDetection::Likelihood::kVeryLowLikelihood);
#else
config.noise_suppression.level = nsLevel;
config.noise_suppression.enabled = enableNS;
if(enableAGC){
config.gain_controller1.mode = webrtc::AudioProcessing::Config::GainController1::kAdaptiveDigital;
config.gain_controller1.target_level_dbfs = ServerConfig::GetSharedInstance()->GetInt("webrtc_agc_target_level", 9);
config.gain_controller1.enable_limiter = ServerConfig::GetSharedInstance()->GetBoolean("webrtc_agc_enable_limiter", true);
config.gain_controller1.compression_gain_db = ServerConfig::GetSharedInstance()->GetInt("webrtc_agc_compression_gain", 20);
}
config.voice_detection.enabled = true;

apm->ApplyConfig(config);
#endif

audioFrame=new webrtc::AudioFrame();
audioFrame->samples_per_channel_=480;
Expand All @@ -94,8 +117,9 @@ EchoCanceller::EchoCanceller(bool enableAEC, bool enableNS, bool enableAGC){

EchoCanceller::~EchoCanceller(){
#ifndef TGVOIP_NO_DSP
delete apm;
apm = nullptr;
delete audioFrame;
delete farendBufferPool;
#endif
}

Expand All @@ -109,7 +133,7 @@ void EchoCanceller::Stop(){


void EchoCanceller::SpeakerOutCallback(unsigned char* data, size_t len){
if(len!=960*2 || !enableAEC || !isOn)
if(len!=960*2 || !enableAEC || !isOn)
return;
#ifndef TGVOIP_NO_DSP
int16_t* buf=(int16_t*)farendBufferPool->Get();
Expand All @@ -130,9 +154,9 @@ void EchoCanceller::RunBufferFarendThread(){
int16_t* samplesIn=farendQueue->GetBlocking();
if(samplesIn){
memcpy(frame.mutable_data(), samplesIn, 480*2);
apm->ProcessReverseStream(&frame);
webrtc::ProcessReverseAudioFrame(apm, &frame);
memcpy(frame.mutable_data(), samplesIn+480, 480*2);
apm->ProcessReverseStream(&frame);
webrtc::ProcessReverseAudioFrame(apm, &frame);
didBufferFarend=true;
farendBufferPool->Reuse(reinterpret_cast<unsigned char*>(samplesIn));
}
Expand All @@ -154,17 +178,25 @@ void EchoCanceller::ProcessInput(int16_t* inOut, size_t numSamples, bool& hasVoi

memcpy(audioFrame->mutable_data(), inOut, 480*2);
if(enableAEC)
apm->set_stream_delay_ms(delay);
apm->ProcessStream(audioFrame);
apm->set_stream_delay_ms(delay);
webrtc::ProcessAudioFrame(apm, audioFrame);
if(enableVAD)
hasVoice=apm->voice_detection()->stream_has_voice();
#ifdef TGVOIP_USE_DESKTOP_DSP_BUNDLED
hasVoice=apm->voice_detection()->stream_has_voice();
#else
hasVoice= apm->GetStatistics().voice_detected.value_or(false);
#endif
memcpy(inOut, audioFrame->data(), 480*2);
memcpy(audioFrame->mutable_data(), inOut+480, 480*2);
if(enableAEC)
apm->set_stream_delay_ms(delay);
apm->ProcessStream(audioFrame);
apm->set_stream_delay_ms(delay);
webrtc::ProcessAudioFrame(apm, audioFrame);
if(enableVAD){
hasVoice=hasVoice || apm->voice_detection()->stream_has_voice();
#ifdef TGVOIP_USE_DESKTOP_DSP_BUNDLED
hasVoice=hasVoice || apm->voice_detection()->stream_has_voice();
#else
hasVoice=hasVoice || apm->GetStatistics().voice_detected.value_or(false);
#endif
}
memcpy(inOut+480, audioFrame->data(), 480*2);
#endif
Expand All @@ -186,7 +218,13 @@ void EchoCanceller::SetAECStrength(int strength){
void EchoCanceller::SetVoiceDetectionEnabled(bool enabled){
enableVAD=enabled;
#ifndef TGVOIP_NO_DSP
#ifdef TGVOIP_USE_DESKTOP_DSP_BUNDLED
apm->voice_detection()->Enable(enabled);
#else
auto config = apm->GetConfig();
config.voice_detection.enabled = enabled;
apm->ApplyConfig(config);
#endif
#endif
}

Expand Down
6 changes: 5 additions & 1 deletion libtgvoip/EchoCanceller.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
#include "MediaStreamItf.h"
#include "utils.h"

#ifndef TGVOIP_NO_DSP
#include "api/scoped_refptr.h"
#endif // TGVOIP_NO_DSP

namespace webrtc{
class AudioProcessing;
class AudioFrame;
Expand Down Expand Up @@ -40,7 +44,7 @@ class EchoCanceller{
bool enableVAD=false;
bool isOn;
#ifndef TGVOIP_NO_DSP
webrtc::AudioProcessing* apm=NULL;
rtc::scoped_refptr<webrtc::AudioProcessing> apm;
webrtc::AudioFrame* audioFrame=NULL;
void RunBufferFarendThread();
bool didBufferFarend;
Expand Down
3 changes: 1 addition & 2 deletions libtgvoip/JitterBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ JitterBuffer::JitterBuffer(MediaStreamItf *out, uint32_t step):bufferPool(JITTER
if(out)
out->SetCallback(JitterBuffer::CallbackOut, this);
this->step=step;
memset(slots, 0, sizeof(jitter_packet_t)*JITTER_SLOT_COUNT);
if(step<30){
minMinDelay=(uint32_t) ServerConfig::GetSharedInstance()->GetInt("jitter_min_delay_20", 6);
maxMinDelay=(uint32_t) ServerConfig::GetSharedInstance()->GetInt("jitter_max_delay_20", 25);
Expand Down Expand Up @@ -246,7 +245,7 @@ void JitterBuffer::PutInternal(jitter_packet_t* pkt, bool overwriteExisting){
first=true;
LOGI("jitter: resyncing, next timestamp = %lld (step=%d, minDelay=%f)", (long long int)nextTimestamp, step, minDelay);
}

for(i=0;i<JITTER_SLOT_COUNT;i++){
if(slots[i].buffer!=NULL){
if(slots[i].timestamp<nextTimestamp-1){
Expand Down
10 changes: 5 additions & 5 deletions libtgvoip/JitterBuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ class JitterBuffer{

private:
struct jitter_packet_t{
unsigned char* buffer=NULL;
size_t size;
uint32_t timestamp;
bool isEC;
double recvTimeDiff;
unsigned char* buffer = nullptr;
size_t size = 0;
uint32_t timestamp = 0;
bool isEC = false;
double recvTimeDiff = 0.;
};
static size_t CallbackIn(unsigned char* data, size_t len, void* param);
static size_t CallbackOut(unsigned char* data, size_t len, void* param);
Expand Down
Loading