1 //
2 // libtgvoip is free and unencumbered public domain software.
3 // For more information, see http://unlicense.org or the UNLICENSE file
4 // you should have received with this source code distribution.
5 //
6 
7 #ifndef LIBTGVOIP_OPUSENCODER_H
8 #define LIBTGVOIP_OPUSENCODER_H
9 
10 
11 #include "MediaStreamItf.h"
12 #include "threading.h"
13 #include "BlockingQueue.h"
14 #include "Buffers.h"
15 #include "EchoCanceller.h"
16 #include "utils.h"
17 
18 #include <stdint.h>
19 
20 struct OpusEncoder;
21 
22 namespace tgvoip{
23 class OpusEncoder{
24 public:
25 	TGVOIP_DISALLOW_COPY_AND_ASSIGN(OpusEncoder);
26 	OpusEncoder(MediaStreamItf* source, bool needSecondary);
27 	virtual ~OpusEncoder();
28 	virtual void Start();
29 	virtual void Stop();
30 	void SetBitrate(uint32_t bitrate);
31 	void SetEchoCanceller(EchoCanceller* aec);
32 	void SetOutputFrameDuration(uint32_t duration);
33 	void SetPacketLoss(int percent);
34 	int GetPacketLoss();
35 	uint32_t GetBitrate();
36 	void SetDTX(bool enable);
37 	void SetLevelMeter(AudioLevelMeter* levelMeter);
38 	void SetCallback(void (*f)(unsigned char*, size_t, unsigned char*, size_t, void*), void* param);
39 	void SetSecondaryEncoderEnabled(bool enabled);
40 	void SetVadMode(bool vad);
41 	void AddAudioEffect(effects::AudioEffect* effect);
42 	void RemoveAudioEffect(effects::AudioEffect* effect);
GetComplexity()43 	int GetComplexity(){
44 		return complexity;
45 	}
46 
47 private:
48 	static size_t Callback(unsigned char* data, size_t len, void* param);
49 	void RunThread();
50 	void Encode(int16_t* data, size_t len);
51 	void InvokeCallback(unsigned char* data, size_t length, unsigned char* secondaryData, size_t secondaryLength);
52 	MediaStreamItf* source;
53 	::OpusEncoder* enc;
54 	::OpusEncoder* secondaryEncoder;
55 	unsigned char buffer[4096];
56 	uint32_t requestedBitrate;
57 	uint32_t currentBitrate;
58 	Thread* thread;
59 	BlockingQueue<unsigned char*> queue;
60 	BufferPool bufferPool;
61 	EchoCanceller* echoCanceller;
62 	int complexity;
63 	bool running;
64 	uint32_t frameDuration;
65 	int packetLossPercent;
66 	AudioLevelMeter* levelMeter;
67 	bool secondaryEncoderEnabled;
68 	bool vadMode=false;
69 	uint32_t vadNoVoiceBitrate;
70 	std::vector<effects::AudioEffect*> postProcEffects;
71 	int secondaryEnabledBandwidth;
72 	int vadModeVoiceBandwidth;
73 	int vadModeNoVoiceBandwidth;
74 
75 	bool wasSecondaryEncoderEnabled=false;
76 
77 	void (*callback)(unsigned char*, size_t, unsigned char*, size_t, void*);
78 	void* callbackParam;
79 };
80 }
81 
82 #endif //LIBTGVOIP_OPUSENCODER_H
83