OpusEncoder.h 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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_OPUSENCODER_H
  7. #define LIBTGVOIP_OPUSENCODER_H
  8. #include "MediaStreamItf.h"
  9. #include "threading.h"
  10. #include "BlockingQueue.h"
  11. #include "Buffers.h"
  12. #include "utils.h"
  13. #include <stdint.h>
  14. struct OpusEncoder;
  15. namespace tgvoip{
  16. class EchoCanceller;
  17. namespace effects {
  18. class AudioEffect;
  19. } // namespace effects
  20. class OpusEncoder{
  21. public:
  22. TGVOIP_DISALLOW_COPY_AND_ASSIGN(OpusEncoder);
  23. OpusEncoder(MediaStreamItf* source, bool needSecondary);
  24. virtual ~OpusEncoder();
  25. virtual void Start();
  26. virtual void Stop();
  27. void SetBitrate(uint32_t bitrate);
  28. void SetEchoCanceller(EchoCanceller* aec);
  29. void SetOutputFrameDuration(uint32_t duration);
  30. void SetPacketLoss(int percent);
  31. int GetPacketLoss();
  32. uint32_t GetBitrate();
  33. void SetDTX(bool enable);
  34. void SetLevelMeter(AudioLevelMeter* levelMeter);
  35. void SetCallback(void (*f)(unsigned char*, size_t, unsigned char*, size_t, void*), void* param);
  36. void SetSecondaryEncoderEnabled(bool enabled);
  37. void SetVadMode(bool vad);
  38. void AddAudioEffect(effects::AudioEffect* effect);
  39. void RemoveAudioEffect(effects::AudioEffect* effect);
  40. int GetComplexity(){
  41. return complexity;
  42. }
  43. private:
  44. static size_t Callback(unsigned char* data, size_t len, void* param);
  45. void RunThread();
  46. void Encode(int16_t* data, size_t len);
  47. void InvokeCallback(unsigned char* data, size_t length, unsigned char* secondaryData, size_t secondaryLength);
  48. MediaStreamItf* source;
  49. ::OpusEncoder* enc;
  50. ::OpusEncoder* secondaryEncoder;
  51. unsigned char buffer[4096];
  52. uint32_t requestedBitrate;
  53. uint32_t currentBitrate;
  54. Thread* thread;
  55. BlockingQueue<unsigned char*> queue;
  56. BufferPool bufferPool;
  57. EchoCanceller* echoCanceller;
  58. int complexity;
  59. bool running;
  60. uint32_t frameDuration;
  61. int packetLossPercent;
  62. AudioLevelMeter* levelMeter;
  63. bool secondaryEncoderEnabled;
  64. bool vadMode=false;
  65. uint32_t vadNoVoiceBitrate;
  66. std::vector<effects::AudioEffect*> postProcEffects;
  67. int secondaryEnabledBandwidth;
  68. int vadModeVoiceBandwidth;
  69. int vadModeNoVoiceBandwidth;
  70. bool wasSecondaryEncoderEnabled=false;
  71. void (*callback)(unsigned char*, size_t, unsigned char*, size_t, void*);
  72. void* callbackParam;
  73. };
  74. }
  75. #endif //LIBTGVOIP_OPUSENCODER_H