EncodedAudioStream.h 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #pragma once
  2. #include <stddef.h> // for size_t
  3. #include <cstdint> // for uint8_t
  4. #include <memory> // for shared_ptr, unique_ptr
  5. #include <string> // for basic_string, string
  6. #include <vector> // for vector
  7. #include "aacdec.h" // for AACFrameInfo
  8. #include "mp3dec.h" // for MP3FrameInfo
  9. namespace bell {
  10. class ByteStream;
  11. class EncodedAudioStream {
  12. public:
  13. EncodedAudioStream();
  14. ~EncodedAudioStream();
  15. // Codecs supported by this stream class
  16. enum class AudioCodec { AAC, MP3, OGG, NONE };
  17. void openWithStream(std::unique_ptr<bell::ByteStream> byteStream);
  18. size_t decodeFrame(uint8_t* dst);
  19. bool isReadable();
  20. private:
  21. std::shared_ptr<ByteStream> innerStream;
  22. std::vector<uint8_t> inputBuffer;
  23. std::vector<short> outputBuffer;
  24. std::string TAG = "EncryptedAudioStream";
  25. uint8_t* decodePtr = 0;
  26. int bytesInBuffer = 0;
  27. size_t offset = 0;
  28. size_t decodedSampleRate = 44100;
  29. AudioCodec codec = AudioCodec::NONE;
  30. void guessDataFormat();
  31. void readFully(uint8_t* dst, size_t nbytes);
  32. bool vectorStartsWith(std::vector<uint8_t>&, std::vector<uint8_t>&);
  33. std::vector<uint8_t> aacMagicBytes = {0xFF, 0xF1};
  34. std::vector<uint8_t> aacMagicBytes4 = {0xFF, 0xF9};
  35. std::vector<uint8_t> mp3MagicBytesUntagged = {0xFF, 0xFB};
  36. std::vector<uint8_t> mp3MagicBytesIdc = {0x49, 0x44, 0x33};
  37. AACFrameInfo aacFrameInfo;
  38. MP3FrameInfo mp3FrameInfo;
  39. size_t decodeFrameMp3(uint8_t* dst);
  40. size_t decodeFrameAAC(uint8_t* dst);
  41. };
  42. } // namespace bell