OpusDecoder.h 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. #ifndef LIBTGVOIP_OPUSDECODER_H
  7. #define LIBTGVOIP_OPUSDECODER_H
  8. #include "MediaStreamItf.h"
  9. #include "threading.h"
  10. #include "BlockingQueue.h"
  11. #include "Buffers.h"
  12. #include "JitterBuffer.h"
  13. #include "utils.h"
  14. #include <stdio.h>
  15. #include <vector>
  16. #include <memory>
  17. struct OpusDecoder;
  18. namespace tgvoip{
  19. class EchoCanceller;
  20. namespace effects {
  21. class AudioEffect;
  22. } // namespace effects
  23. class OpusDecoder {
  24. public:
  25. TGVOIP_DISALLOW_COPY_AND_ASSIGN(OpusDecoder);
  26. virtual void Start();
  27. virtual void Stop();
  28. OpusDecoder(const std::shared_ptr<MediaStreamItf>& dst, bool isAsync, bool needEC);
  29. OpusDecoder(const std::unique_ptr<MediaStreamItf>& dst, bool isAsync, bool needEC);
  30. OpusDecoder(MediaStreamItf* dst, bool isAsync, bool needEC);
  31. virtual ~OpusDecoder();
  32. size_t HandleCallback(unsigned char* data, size_t len);
  33. void SetEchoCanceller(EchoCanceller* canceller);
  34. void SetFrameDuration(uint32_t duration);
  35. void SetJitterBuffer(std::shared_ptr<JitterBuffer> jitterBuffer);
  36. void SetDTX(bool enable);
  37. void SetLevelMeter(AudioLevelMeter* levelMeter);
  38. void AddAudioEffect(effects::AudioEffect* effect);
  39. void RemoveAudioEffect(effects::AudioEffect* effect);
  40. private:
  41. void Initialize(bool isAsync, bool needEC);
  42. static size_t Callback(unsigned char* data, size_t len, void* param);
  43. void RunThread();
  44. int DecodeNextFrame();
  45. ::OpusDecoder* dec;
  46. ::OpusDecoder* ecDec;
  47. BlockingQueue<unsigned char*>* decodedQueue;
  48. BufferPool* bufferPool;
  49. unsigned char* buffer;
  50. unsigned char* lastDecoded;
  51. unsigned char* processedBuffer;
  52. size_t outputBufferSize;
  53. bool running;
  54. Thread* thread;
  55. Semaphore* semaphore;
  56. uint32_t frameDuration;
  57. EchoCanceller* echoCanceller;
  58. std::shared_ptr<JitterBuffer> jitterBuffer;
  59. AudioLevelMeter* levelMeter;
  60. int consecutiveLostPackets;
  61. bool enableDTX;
  62. size_t silentPacketCount;
  63. std::vector<effects::AudioEffect*> postProcEffects;
  64. bool async;
  65. unsigned char nextBuffer[8192];
  66. unsigned char decodeBuffer[8192];
  67. size_t nextLen;
  68. unsigned int packetsPerFrame;
  69. ptrdiff_t remainingDataLen;
  70. bool prevWasEC;
  71. };
  72. }
  73. #endif //LIBTGVOIP_OPUSDECODER_H