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.
|
|
|
|
//
|
|
|
|
|
|
|
|
#ifndef LIBTGVOIP_OPUSDECODER_H
|
|
|
|
#define LIBTGVOIP_OPUSDECODER_H
|
|
|
|
|
|
|
|
|
|
|
|
#include "MediaStreamItf.h"
|
|
|
|
#include "threading.h"
|
|
|
|
#include "BlockingQueue.h"
|
2018-06-04 21:37:43 +02:00
|
|
|
#include "Buffers.h"
|
2017-02-02 17:24:40 +01:00
|
|
|
#include "EchoCanceller.h"
|
|
|
|
#include "JitterBuffer.h"
|
2018-11-09 16:44:01 +01:00
|
|
|
#include "utils.h"
|
2017-02-02 17:24:40 +01:00
|
|
|
#include <stdio.h>
|
2017-09-07 07:39:33 +02:00
|
|
|
#include <vector>
|
2018-06-04 21:37:43 +02:00
|
|
|
#include <memory>
|
2019-04-15 01:43:10 +02:00
|
|
|
#include <atomic>
|
2017-02-02 17:24:40 +01:00
|
|
|
|
2018-07-17 18:48:21 +02:00
|
|
|
struct OpusDecoder;
|
|
|
|
|
2017-04-28 13:17:56 +02:00
|
|
|
namespace tgvoip{
|
|
|
|
class OpusDecoder {
|
2017-02-02 17:24:40 +01:00
|
|
|
public:
|
2018-11-09 16:44:01 +01:00
|
|
|
TGVOIP_DISALLOW_COPY_AND_ASSIGN(OpusDecoder);
|
2017-02-02 17:24:40 +01:00
|
|
|
virtual void Start();
|
|
|
|
|
|
|
|
virtual void Stop();
|
|
|
|
|
2018-06-04 21:37:43 +02:00
|
|
|
OpusDecoder(const std::shared_ptr<MediaStreamItf>& dst, bool isAsync, bool needEC);
|
|
|
|
OpusDecoder(const std::unique_ptr<MediaStreamItf>& dst, bool isAsync, bool needEC);
|
2018-07-17 18:48:21 +02:00
|
|
|
OpusDecoder(MediaStreamItf* dst, bool isAsync, bool needEC);
|
2017-04-28 13:17:56 +02:00
|
|
|
virtual ~OpusDecoder();
|
2018-05-15 20:23:46 +02:00
|
|
|
size_t HandleCallback(unsigned char* data, size_t len);
|
2017-04-28 13:17:56 +02:00
|
|
|
void SetEchoCanceller(EchoCanceller* canceller);
|
2017-02-02 17:24:40 +01:00
|
|
|
void SetFrameDuration(uint32_t duration);
|
2018-06-04 21:37:43 +02:00
|
|
|
void SetJitterBuffer(std::shared_ptr<JitterBuffer> jitterBuffer);
|
2018-05-15 20:23:46 +02:00
|
|
|
void SetDTX(bool enable);
|
|
|
|
void SetLevelMeter(AudioLevelMeter* levelMeter);
|
2018-12-30 00:24:55 +01:00
|
|
|
void AddAudioEffect(effects::AudioEffect* effect);
|
|
|
|
void RemoveAudioEffect(effects::AudioEffect* effect);
|
2017-02-02 17:24:40 +01:00
|
|
|
|
|
|
|
private:
|
2018-06-04 21:37:43 +02:00
|
|
|
void Initialize(bool isAsync, bool needEC);
|
2017-02-02 17:24:40 +01:00
|
|
|
static size_t Callback(unsigned char* data, size_t len, void* param);
|
2018-11-09 16:44:01 +01:00
|
|
|
void RunThread();
|
2018-05-15 20:23:46 +02:00
|
|
|
int DecodeNextFrame();
|
2017-04-28 13:17:56 +02:00
|
|
|
::OpusDecoder* dec;
|
2018-06-04 21:37:43 +02:00
|
|
|
::OpusDecoder* ecDec;
|
2019-04-15 01:43:10 +02:00
|
|
|
BlockingQueue<Buffer>* decodedQueue;
|
|
|
|
BufferPool<960*2, 32> bufferPool;
|
2017-02-02 17:24:40 +01:00
|
|
|
unsigned char* buffer;
|
|
|
|
unsigned char* lastDecoded;
|
2020-03-05 13:31:57 +01:00
|
|
|
unsigned char* processedBuffer;
|
2017-02-02 17:24:40 +01:00
|
|
|
size_t outputBufferSize;
|
2019-04-15 01:43:10 +02:00
|
|
|
std::atomic<bool> running;
|
2018-05-15 20:23:46 +02:00
|
|
|
Thread* thread;
|
|
|
|
Semaphore* semaphore;
|
2017-02-02 17:24:40 +01:00
|
|
|
uint32_t frameDuration;
|
2017-04-28 13:17:56 +02:00
|
|
|
EchoCanceller* echoCanceller;
|
2018-06-04 21:37:43 +02:00
|
|
|
std::shared_ptr<JitterBuffer> jitterBuffer;
|
2018-05-15 20:23:46 +02:00
|
|
|
AudioLevelMeter* levelMeter;
|
|
|
|
int consecutiveLostPackets;
|
|
|
|
bool enableDTX;
|
|
|
|
size_t silentPacketCount;
|
2018-12-30 00:24:55 +01:00
|
|
|
std::vector<effects::AudioEffect*> postProcEffects;
|
2020-03-05 13:31:57 +01:00
|
|
|
std::atomic<bool> async;
|
|
|
|
alignas(2) unsigned char nextBuffer[8192];
|
|
|
|
alignas(2) unsigned char decodeBuffer[8192];
|
2018-05-15 20:23:46 +02:00
|
|
|
size_t nextLen;
|
|
|
|
unsigned int packetsPerFrame;
|
2018-05-24 18:42:28 +02:00
|
|
|
ptrdiff_t remainingDataLen;
|
2018-06-04 21:37:43 +02:00
|
|
|
bool prevWasEC;
|
|
|
|
int16_t prevLastSample;
|
2017-02-02 17:24:40 +01:00
|
|
|
};
|
2017-04-28 13:17:56 +02:00
|
|
|
}
|
2017-02-02 17:24:40 +01:00
|
|
|
|
|
|
|
#endif //LIBTGVOIP_OPUSDECODER_H
|