| 6 |
| 7 | #ifndef LIBTGVOIP_OPUSDECODER_H |
| 8 | #define LIBTGVOIP_OPUSDECODER_H |
| 9 |
| 10 | #include "threading.h" |
| 11 | #include "utils.h" |
| 12 | #include "BlockingQueue.h" |
| 13 | #include "Buffers.h" |
| 14 | #include "EchoCanceller.h" |
| 15 | #include "JitterBuffer.h" |
| 16 | #include "MediaStreamItf.h" |
| 17 |
| 18 | #include <atomic> |
| 19 | #include <cstdio> |
| 20 | #include <memory> |
| 21 | #include <vector> |
| 22 |
| 23 | struct OpusDecoder; |
| 24 |
| 25 | namespace tgvoip |
| 26 | { |
| 27 |
| 28 | class OpusDecoder |
| 29 | { |
| 30 | public: |
| 31 | TGVOIP_DISALLOW_COPY_AND_ASSIGN(OpusDecoder); |
| 32 | virtual void Start(); |
| 33 | virtual void Stop(); |
| 34 |
| 35 | OpusDecoder(MediaStreamItf* dst, bool isAsync, bool needEC); |
| 36 | virtual ~OpusDecoder(); |
| 37 |
| 38 | std::size_t HandleCallback(std::uint8_t* data, std::size_t len); |
| 39 | void SetEchoCanceller(EchoCanceller* canceller); |
| 40 | void SetFrameDuration(std::uint32_t duration); |
| 41 | void SetJitterBuffer(std::shared_ptr<JitterBuffer> m_jitterBuffer); |
| 42 | void SetDTX(bool enable); |
| 43 | void SetLevelMeter(AudioLevelMeter* m_levelMeter); |
| 44 | void AddAudioEffect(effects::AudioEffect* effect); |
| 45 | void RemoveAudioEffect(effects::AudioEffect* effect); |
| 46 |
| 47 | private: |
| 48 | BlockingQueue<Buffer>* m_decodedQueue; |
| 49 | BufferPool<960 * 2, 32> m_bufferPool; |
| 50 | std::vector<effects::AudioEffect*> m_postProcEffects; |
| 51 | std::uint8_t* m_buffer; |
| 52 | std::uint8_t* m_processedBuffer; |
| 53 | std::uint8_t* m_lastDecoded; |
| 54 |
| 55 | ::OpusDecoder* m_dec; |
| 56 | ::OpusDecoder* m_ecDec; |
| 57 | EchoCanceller* m_echoCanceller; |
| 58 | AudioLevelMeter* m_levelMeter; |
| 59 | std::shared_ptr<JitterBuffer> m_jitterBuffer; |
| 60 |
| 61 | Thread* m_thread; |
| 62 | Semaphore* m_semaphore; |
| 63 |
| 64 | std::size_t m_outputBufferSize; |
| 65 | std::size_t m_silentPacketCount; |
| 66 | std::size_t m_nextLen; |
| 67 | std::ptrdiff_t m_remainingDataLen; |
| 68 |
| 69 | std::uint32_t m_frameDuration; |
| 70 | unsigned int m_packetsPerFrame; |
| 71 | int m_consecutiveLostPackets; |
| 72 |
| 73 | std::int16_t m_prevLastSample; |
| 74 | alignas(2) std::uint8_t m_nextBuffer[8192]; |
| 75 | alignas(2) std::uint8_t m_decodeBuffer[8192]; |
| 76 |
| 77 | std::atomic<bool> m_running; |
| 78 | std::atomic<bool> m_async; |
| 79 | bool m_enableDTX; |
| 80 | bool m_prevWasEC; |
| 81 |
| 82 |
| 83 | void Initialize(bool isAsync, bool needEC); |
| 84 | void RunThread(); |
| 85 | int DecodeNextFrame(); |
| 86 | static std::size_t Callback(std::uint8_t* data, std::size_t len, void* param); |
| 87 | }; |
| 88 |
| 89 | } // namespace tgvoip |
| 90 |
| 91 | #endif // LIBTGVOIP_OPUSDECODER_H |
| 92 |