2017-02-02 19:24:40 +03: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.
|
|
|
|
//
|
|
|
|
|
|
|
|
#ifndef LIBTGVOIP_OPUSENCODER_H
|
|
|
|
#define LIBTGVOIP_OPUSENCODER_H
|
|
|
|
|
|
|
|
|
|
|
|
#include "MediaStreamItf.h"
|
|
|
|
#include "threading.h"
|
|
|
|
#include "BlockingQueue.h"
|
2018-06-04 22:37:43 +03:00
|
|
|
#include "Buffers.h"
|
2017-02-02 19:24:40 +03:00
|
|
|
#include "EchoCanceller.h"
|
2018-11-09 18:44:01 +03:00
|
|
|
#include "utils.h"
|
2017-02-02 19:24:40 +03:00
|
|
|
|
|
|
|
#include <stdint.h>
|
2019-04-15 02:43:10 +03:00
|
|
|
#include <atomic>
|
2017-02-02 19:24:40 +03:00
|
|
|
|
2018-07-17 19:48:21 +03:00
|
|
|
struct OpusEncoder;
|
|
|
|
|
2017-04-28 14:17:56 +03:00
|
|
|
namespace tgvoip{
|
2018-06-04 22:37:43 +03:00
|
|
|
class OpusEncoder{
|
2017-02-02 19:24:40 +03:00
|
|
|
public:
|
2018-11-09 18:44:01 +03:00
|
|
|
TGVOIP_DISALLOW_COPY_AND_ASSIGN(OpusEncoder);
|
2018-06-04 22:37:43 +03:00
|
|
|
OpusEncoder(MediaStreamItf* source, bool needSecondary);
|
2017-04-28 14:17:56 +03:00
|
|
|
virtual ~OpusEncoder();
|
2017-02-02 19:24:40 +03:00
|
|
|
virtual void Start();
|
|
|
|
virtual void Stop();
|
|
|
|
void SetBitrate(uint32_t bitrate);
|
2017-04-28 14:17:56 +03:00
|
|
|
void SetEchoCanceller(EchoCanceller* aec);
|
2017-02-02 19:24:40 +03:00
|
|
|
void SetOutputFrameDuration(uint32_t duration);
|
|
|
|
void SetPacketLoss(int percent);
|
2017-03-30 17:06:59 +03:00
|
|
|
int GetPacketLoss();
|
2017-02-02 19:24:40 +03:00
|
|
|
uint32_t GetBitrate();
|
2018-05-15 21:23:46 +03:00
|
|
|
void SetDTX(bool enable);
|
|
|
|
void SetLevelMeter(AudioLevelMeter* levelMeter);
|
2019-04-15 02:43:10 +03:00
|
|
|
void SetCallback(std::function <void(unsigned char*, size_t, unsigned char*, size_t)> callback);
|
2018-06-04 22:37:43 +03:00
|
|
|
void SetSecondaryEncoderEnabled(bool enabled);
|
2018-11-30 15:39:31 +03:00
|
|
|
void SetVadMode(bool vad);
|
2018-12-30 02:24:55 +03:00
|
|
|
void AddAudioEffect(effects::AudioEffect* effect);
|
|
|
|
void RemoveAudioEffect(effects::AudioEffect* effect);
|
2019-02-11 19:01:45 +03:00
|
|
|
int GetComplexity(){
|
|
|
|
return complexity;
|
|
|
|
}
|
2017-02-02 19:24:40 +03:00
|
|
|
|
|
|
|
private:
|
|
|
|
static size_t Callback(unsigned char* data, size_t len, void* param);
|
2018-11-09 18:44:01 +03:00
|
|
|
void RunThread();
|
2018-11-21 21:22:31 +03:00
|
|
|
void Encode(int16_t* data, size_t len);
|
2018-06-04 22:37:43 +03:00
|
|
|
void InvokeCallback(unsigned char* data, size_t length, unsigned char* secondaryData, size_t secondaryLength);
|
2017-04-28 14:17:56 +03:00
|
|
|
MediaStreamItf* source;
|
|
|
|
::OpusEncoder* enc;
|
2018-06-04 22:37:43 +03:00
|
|
|
::OpusEncoder* secondaryEncoder;
|
2017-02-02 19:24:40 +03:00
|
|
|
unsigned char buffer[4096];
|
2019-04-15 02:43:10 +03:00
|
|
|
std::atomic<uint32_t> requestedBitrate;
|
2017-02-02 19:24:40 +03:00
|
|
|
uint32_t currentBitrate;
|
2018-05-15 21:23:46 +03:00
|
|
|
Thread* thread;
|
2019-04-15 02:43:10 +03:00
|
|
|
BlockingQueue<Buffer> queue;
|
|
|
|
BufferPool<960*2, 10> bufferPool;
|
2017-04-28 14:17:56 +03:00
|
|
|
EchoCanceller* echoCanceller;
|
2019-04-15 02:43:10 +03:00
|
|
|
std::atomic<int> complexity;
|
|
|
|
std::atomic<bool> running;
|
2017-02-02 19:24:40 +03:00
|
|
|
uint32_t frameDuration;
|
2017-03-30 17:06:59 +03:00
|
|
|
int packetLossPercent;
|
2018-05-15 21:23:46 +03:00
|
|
|
AudioLevelMeter* levelMeter;
|
2020-03-05 16:31:57 +04:00
|
|
|
std::atomic<bool> secondaryEncoderEnabled;
|
2018-11-30 15:39:31 +03:00
|
|
|
bool vadMode=false;
|
|
|
|
uint32_t vadNoVoiceBitrate;
|
2018-12-30 02:24:55 +03:00
|
|
|
std::vector<effects::AudioEffect*> postProcEffects;
|
2019-02-20 01:48:36 +04:00
|
|
|
int secondaryEnabledBandwidth;
|
|
|
|
int vadModeVoiceBandwidth;
|
|
|
|
int vadModeNoVoiceBandwidth;
|
|
|
|
|
|
|
|
bool wasSecondaryEncoderEnabled=false;
|
2018-06-04 22:37:43 +03:00
|
|
|
|
2019-04-15 02:43:10 +03:00
|
|
|
std::function <void(unsigned char*, size_t, unsigned char*, size_t)> callback;
|
2017-02-02 19:24:40 +03:00
|
|
|
};
|
2017-04-28 14:17:56 +03:00
|
|
|
}
|
2017-02-02 19:24:40 +03:00
|
|
|
|
|
|
|
#endif //LIBTGVOIP_OPUSENCODER_H
|