2017-02-02 17:24:40 +01:00
|
|
|
//
|
|
|
|
// libtgvoip is free and unencumbered public domain software.
|
|
|
|
// For more information, see http://unlicense.org or the UNLICENSE file
|
|
|
|
// you should have received with this source code distribution.
|
|
|
|
//
|
|
|
|
|
|
|
|
#include "OpusEncoder.h"
|
|
|
|
#include <assert.h>
|
|
|
|
#include "logging.h"
|
2017-03-30 16:06:59 +02:00
|
|
|
#include "VoIPServerConfig.h"
|
2018-07-17 18:48:21 +02:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include <opus/opus.h>
|
|
|
|
#else
|
|
|
|
#include "opus.h"
|
|
|
|
#endif
|
2017-02-02 17:24:40 +01:00
|
|
|
|
2018-06-04 21:37:43 +02:00
|
|
|
tgvoip::OpusEncoder::OpusEncoder(MediaStreamItf *source, bool needSecondary):queue(11), bufferPool(960*2, 10){
|
2017-02-02 17:24:40 +01:00
|
|
|
this->source=source;
|
2017-04-28 13:17:56 +02:00
|
|
|
source->SetCallback(tgvoip::OpusEncoder::Callback, this);
|
2017-02-02 17:24:40 +01:00
|
|
|
enc=opus_encoder_create(48000, 1, OPUS_APPLICATION_VOIP, NULL);
|
|
|
|
opus_encoder_ctl(enc, OPUS_SET_COMPLEXITY(10));
|
|
|
|
opus_encoder_ctl(enc, OPUS_SET_PACKET_LOSS_PERC(15));
|
|
|
|
opus_encoder_ctl(enc, OPUS_SET_INBAND_FEC(1));
|
|
|
|
opus_encoder_ctl(enc, OPUS_SET_SIGNAL(OPUS_SIGNAL_VOICE));
|
2017-03-30 16:06:59 +02:00
|
|
|
opus_encoder_ctl(enc, OPUS_SET_BANDWIDTH(OPUS_BANDWIDTH_FULLBAND));
|
2017-02-02 17:24:40 +01:00
|
|
|
requestedBitrate=32000;
|
|
|
|
currentBitrate=0;
|
|
|
|
running=false;
|
|
|
|
echoCanceller=NULL;
|
|
|
|
complexity=10;
|
|
|
|
frameDuration=20;
|
2018-05-15 20:23:46 +02:00
|
|
|
levelMeter=NULL;
|
2017-04-17 20:57:07 +02:00
|
|
|
mediumCorrectionBitrate=ServerConfig::GetSharedInstance()->GetInt("audio_medium_fec_bitrate", 10000);
|
|
|
|
strongCorrectionBitrate=ServerConfig::GetSharedInstance()->GetInt("audio_strong_fec_bitrate", 8000);
|
|
|
|
mediumCorrectionMultiplier=ServerConfig::GetSharedInstance()->GetDouble("audio_medium_fec_multiplier", 1.5);
|
|
|
|
strongCorrectionMultiplier=ServerConfig::GetSharedInstance()->GetDouble("audio_strong_fec_multiplier", 2.0);
|
2018-06-04 21:37:43 +02:00
|
|
|
secondaryEncoderEnabled=false;
|
|
|
|
|
|
|
|
if(needSecondary){
|
|
|
|
secondaryEncoder=opus_encoder_create(48000, 1, OPUS_APPLICATION_VOIP, NULL);
|
|
|
|
opus_encoder_ctl(secondaryEncoder, OPUS_SET_COMPLEXITY(10));
|
|
|
|
opus_encoder_ctl(secondaryEncoder, OPUS_SET_SIGNAL(OPUS_SIGNAL_VOICE));
|
2018-11-09 16:44:01 +01:00
|
|
|
//opus_encoder_ctl(secondaryEncoder, OPUS_SET_VBR(0));
|
2018-06-04 21:37:43 +02:00
|
|
|
opus_encoder_ctl(secondaryEncoder, OPUS_SET_BITRATE(8000));
|
|
|
|
opus_encoder_ctl(secondaryEncoder, OPUS_SET_INBAND_FEC(1));
|
|
|
|
opus_encoder_ctl(secondaryEncoder, OPUS_SET_PACKET_LOSS_PERC(15));
|
2018-11-09 16:44:01 +01:00
|
|
|
opus_encoder_ctl(secondaryEncoder, OPUS_SET_BANDWIDTH(OPUS_BANDWIDTH_FULLBAND));
|
2018-06-04 21:37:43 +02:00
|
|
|
}else{
|
|
|
|
secondaryEncoder=NULL;
|
|
|
|
}
|
2017-02-02 17:24:40 +01:00
|
|
|
}
|
|
|
|
|
2017-04-28 13:17:56 +02:00
|
|
|
tgvoip::OpusEncoder::~OpusEncoder(){
|
2017-02-02 17:24:40 +01:00
|
|
|
opus_encoder_destroy(enc);
|
2018-06-04 21:37:43 +02:00
|
|
|
if(secondaryEncoder)
|
|
|
|
opus_encoder_destroy(secondaryEncoder);
|
2017-02-02 17:24:40 +01:00
|
|
|
}
|
|
|
|
|
2017-04-28 13:17:56 +02:00
|
|
|
void tgvoip::OpusEncoder::Start(){
|
2017-02-02 17:24:40 +01:00
|
|
|
if(running)
|
|
|
|
return;
|
|
|
|
running=true;
|
2018-11-09 16:44:01 +01:00
|
|
|
thread=new Thread(std::bind(&tgvoip::OpusEncoder::RunThread, this));
|
2018-05-15 20:23:46 +02:00
|
|
|
thread->SetName("OpusEncoder");
|
|
|
|
thread->Start();
|
|
|
|
thread->SetMaxPriority();
|
2017-02-02 17:24:40 +01:00
|
|
|
}
|
|
|
|
|
2017-04-28 13:17:56 +02:00
|
|
|
void tgvoip::OpusEncoder::Stop(){
|
2017-02-02 17:24:40 +01:00
|
|
|
if(!running)
|
|
|
|
return;
|
|
|
|
running=false;
|
|
|
|
queue.Put(NULL);
|
2018-05-15 20:23:46 +02:00
|
|
|
thread->Join();
|
|
|
|
delete thread;
|
2017-02-02 17:24:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-04-28 13:17:56 +02:00
|
|
|
void tgvoip::OpusEncoder::SetBitrate(uint32_t bitrate){
|
2017-02-02 17:24:40 +01:00
|
|
|
requestedBitrate=bitrate;
|
|
|
|
}
|
|
|
|
|
2017-04-28 13:17:56 +02:00
|
|
|
void tgvoip::OpusEncoder::Encode(unsigned char *data, size_t len){
|
2017-02-02 17:24:40 +01:00
|
|
|
if(requestedBitrate!=currentBitrate){
|
|
|
|
opus_encoder_ctl(enc, OPUS_SET_BITRATE(requestedBitrate));
|
|
|
|
currentBitrate=requestedBitrate;
|
|
|
|
LOGV("opus_encoder: setting bitrate to %u", currentBitrate);
|
|
|
|
}
|
2018-05-15 20:23:46 +02:00
|
|
|
if(levelMeter)
|
|
|
|
levelMeter->Update(reinterpret_cast<int16_t *>(data), len/2);
|
2017-02-02 17:24:40 +01:00
|
|
|
int32_t r=opus_encode(enc, (int16_t*)data, len/2, buffer, 4096);
|
|
|
|
if(r<=0){
|
|
|
|
LOGE("Error encoding: %d", r);
|
|
|
|
}else if(r==1){
|
|
|
|
LOGW("DTX");
|
|
|
|
}else if(running){
|
|
|
|
//LOGV("Packet size = %d", r);
|
2018-06-04 21:37:43 +02:00
|
|
|
int32_t secondaryLen=0;
|
|
|
|
unsigned char secondaryBuffer[128];
|
|
|
|
if(secondaryEncoderEnabled && secondaryEncoder){
|
|
|
|
secondaryLen=opus_encode(secondaryEncoder, (int16_t*)data, len/2, secondaryBuffer, sizeof(secondaryBuffer));
|
|
|
|
//LOGV("secondaryLen %d", secondaryLen);
|
|
|
|
}
|
|
|
|
InvokeCallback(buffer, (size_t)r, secondaryBuffer, (size_t)secondaryLen);
|
2017-02-02 17:24:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-28 13:17:56 +02:00
|
|
|
size_t tgvoip::OpusEncoder::Callback(unsigned char *data, size_t len, void* param){
|
|
|
|
OpusEncoder* e=(OpusEncoder*)param;
|
2017-02-02 17:24:40 +01:00
|
|
|
unsigned char* buf=e->bufferPool.Get();
|
|
|
|
if(buf){
|
|
|
|
assert(len==960*2);
|
|
|
|
memcpy(buf, data, 960*2);
|
|
|
|
e->queue.Put(buf);
|
|
|
|
}else{
|
|
|
|
LOGW("opus_encoder: no buffer slots left");
|
|
|
|
if(e->complexity>1){
|
|
|
|
e->complexity--;
|
|
|
|
opus_encoder_ctl(e->enc, OPUS_SET_COMPLEXITY(e->complexity));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-04-28 13:17:56 +02:00
|
|
|
uint32_t tgvoip::OpusEncoder::GetBitrate(){
|
2017-02-02 17:24:40 +01:00
|
|
|
return requestedBitrate;
|
|
|
|
}
|
|
|
|
|
2017-04-28 13:17:56 +02:00
|
|
|
void tgvoip::OpusEncoder::SetEchoCanceller(EchoCanceller* aec){
|
2017-02-02 17:24:40 +01:00
|
|
|
echoCanceller=aec;
|
|
|
|
}
|
|
|
|
|
2018-11-09 16:44:01 +01:00
|
|
|
void tgvoip::OpusEncoder::RunThread(){
|
2017-02-02 17:24:40 +01:00
|
|
|
unsigned char buf[960*2];
|
|
|
|
uint32_t bufferedCount=0;
|
|
|
|
uint32_t packetsPerFrame=frameDuration/20;
|
2017-05-21 16:50:23 +02:00
|
|
|
LOGV("starting encoder, packets per frame=%d", packetsPerFrame);
|
2017-02-02 17:24:40 +01:00
|
|
|
unsigned char* frame;
|
|
|
|
if(packetsPerFrame>1)
|
|
|
|
frame=(unsigned char *) malloc(960*2*packetsPerFrame);
|
|
|
|
else
|
|
|
|
frame=NULL;
|
|
|
|
while(running){
|
|
|
|
unsigned char* packet=(unsigned char*)queue.GetBlocking();
|
|
|
|
if(packet){
|
|
|
|
if(echoCanceller)
|
|
|
|
echoCanceller->ProcessInput(packet, buf, 960*2);
|
|
|
|
else
|
|
|
|
memcpy(buf, packet, 960*2);
|
|
|
|
if(packetsPerFrame==1){
|
|
|
|
Encode(buf, 960*2);
|
|
|
|
}else{
|
|
|
|
memcpy(frame+(960*2*bufferedCount), buf, 960*2);
|
|
|
|
bufferedCount++;
|
|
|
|
if(bufferedCount==packetsPerFrame){
|
|
|
|
Encode(frame, 960*2*packetsPerFrame);
|
|
|
|
bufferedCount=0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
bufferPool.Reuse(packet);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(frame)
|
|
|
|
free(frame);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-04-28 13:17:56 +02:00
|
|
|
void tgvoip::OpusEncoder::SetOutputFrameDuration(uint32_t duration){
|
2017-02-02 17:24:40 +01:00
|
|
|
frameDuration=duration;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-04-28 13:17:56 +02:00
|
|
|
void tgvoip::OpusEncoder::SetPacketLoss(int percent){
|
2017-03-30 16:06:59 +02:00
|
|
|
packetLossPercent=percent;
|
|
|
|
double multiplier=1;
|
|
|
|
if(currentBitrate<=strongCorrectionBitrate)
|
|
|
|
multiplier=strongCorrectionMultiplier;
|
|
|
|
else if(currentBitrate<=mediumCorrectionBitrate)
|
|
|
|
multiplier=mediumCorrectionMultiplier;
|
|
|
|
opus_encoder_ctl(enc, OPUS_SET_PACKET_LOSS_PERC((int)(percent*multiplier)));
|
|
|
|
}
|
|
|
|
|
2017-04-28 13:17:56 +02:00
|
|
|
int tgvoip::OpusEncoder::GetPacketLoss(){
|
2017-03-30 16:06:59 +02:00
|
|
|
return packetLossPercent;
|
2017-02-02 17:24:40 +01:00
|
|
|
}
|
2018-05-15 20:23:46 +02:00
|
|
|
|
|
|
|
void tgvoip::OpusEncoder::SetDTX(bool enable){
|
|
|
|
opus_encoder_ctl(enc, OPUS_SET_DTX(enable ? 1 : 0));
|
|
|
|
}
|
|
|
|
|
|
|
|
void tgvoip::OpusEncoder::SetLevelMeter(tgvoip::AudioLevelMeter *levelMeter){
|
|
|
|
this->levelMeter=levelMeter;
|
|
|
|
}
|
2018-06-04 21:37:43 +02:00
|
|
|
|
|
|
|
void tgvoip::OpusEncoder::SetCallback(void (*f)(unsigned char *, size_t, unsigned char *, size_t, void *), void *param){
|
|
|
|
callback=f;
|
|
|
|
callbackParam=param;
|
|
|
|
}
|
|
|
|
|
|
|
|
void tgvoip::OpusEncoder::InvokeCallback(unsigned char *data, size_t length, unsigned char *secondaryData, size_t secondaryLength){
|
|
|
|
callback(data, length, secondaryData, secondaryLength, callbackParam);
|
|
|
|
}
|
|
|
|
|
|
|
|
void tgvoip::OpusEncoder::SetSecondaryEncoderEnabled(bool enabled){
|
|
|
|
secondaryEncoderEnabled=enabled;
|
|
|
|
}
|