CUOpusCoder.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. // Copyright 2019-current Getnamo. All Rights Reserved
  2. #pragma once
  3. #define WITH_OPUS (PLATFORM_WINDOWS || PLATFORM_UNIX || PLATFORM_ANDROID)
  4. #if WITH_OPUS
  5. #include "ThirdParty/libOpus/opus-1.1/include/opus.h"
  6. //#include "ogg/ogg.h"
  7. #endif
  8. //Bare minimum struct for transferring opus bytes.
  9. struct FCUOpusMinimalStream
  10. {
  11. TArray<int16> PacketSizes; // Maximum packet size is 32768 (much larger than typical packet)
  12. TArray<uint8> CompressedBytes;
  13. };
  14. /**
  15. Symmetric coder for e.g. voip written from raw libopus due to how hidden the opus
  16. coder is in the engine (this one doesn't require an online subsystem)
  17. */
  18. class FCUOpusCoder
  19. {
  20. public:
  21. FCUOpusCoder();
  22. ~FCUOpusCoder();
  23. /** Set Encoder and Decoder Samples per sec */
  24. void SetSampleRate(int32 InSampleRate);
  25. void SetChannels(int32 Channels);
  26. void SetBitrate(int32 Bitrate);
  27. void SetFrameSizeMs(int32 FrameSizeInMs);
  28. /** Expects raw PCM data, outputs compressed raw opus data along with compressed frame sizes*/
  29. bool EncodeStream(const TArray<uint8>& InPCMBytes, FCUOpusMinimalStream& OutStream);
  30. bool DecodeStream(const FCUOpusMinimalStream& InStream, TArray<uint8>& OutPCMFrame);
  31. //Format: PacketCount, PacketSizes, CompressedOpusBytes. Expects settings set in a different stream
  32. bool SerializeMinimal(const FCUOpusMinimalStream& InStream, TArray<uint8>& OutSerializedBytes);
  33. bool DeserializeMinimal(const TArray<uint8>& InSerializedMinimalBytes, FCUOpusMinimalStream& OutStream);
  34. //Handle a single frame
  35. int32 EncodeFrame(const TArray<uint8>& InPCMFrame, TArray<uint8>& OutCompressed);
  36. int32 DecodeFrame(const TArray<uint8>& InCompressedFrame, TArray<uint8>& OutPCMFrame);
  37. //Todo: add ogg file format wrapper for raw opus bytes
  38. //Intended to be Read-only
  39. int32 Channels;
  40. int32 SampleRate;
  41. //Whether we should reset the coder per stream to keep byte size small. Costs ~0.01ms
  42. bool bResetBetweenEncoding;
  43. bool bApplicationVoip;
  44. bool bLowestPossibleLatency; //trade-off is compression size; ~3ms/sec of audio if on, ~10ms/sec if off. For ~ 33% more bytes.
  45. void ResetCoderIfInitialized();
  46. protected:
  47. //Call this if some settings need to be reflected (all setters all this)
  48. bool InitEncoderIfNeeded();
  49. bool InitDecoderIfNeeded();
  50. private:
  51. #if WITH_OPUS
  52. OpusEncoder* Encoder;
  53. OpusDecoder* Decoder;
  54. #endif
  55. int32 BitRate;
  56. int32 MaxPacketSize;
  57. int32 FrameSizeMs;
  58. int32 FrameSize;
  59. int32 MaxFrameSize;
  60. //Debug utilities
  61. void DebugLogEncoder();
  62. void DebugLogDecoder();
  63. void DebugLogFrame(const uint8* PacketData, uint32 PacketLength, uint32 SampleRate, bool bEncode);
  64. };