2
0

ChunkedAudioStream.h 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #ifndef CHUNKEDAUDIOSTREAM_H
  2. #define CHUNKEDAUDIOSTREAM_H
  3. #include <iostream>
  4. #include <vector>
  5. #include <fstream>
  6. #include <array>
  7. #ifndef _WIN32
  8. #include <unistd.h>
  9. #endif
  10. #include <atomic>
  11. #include "ivorbisfile.h"
  12. #include "MercuryManager.h"
  13. #include "AudioSink.h"
  14. #include "AudioChunk.h"
  15. #include "platform/WrappedMutex.h"
  16. #include "ChunkedByteStream.h"
  17. #define SPOTIFY_HEADER_SIZE 167
  18. #define BUFFER_SIZE 0x20000 * 1.5
  19. typedef std::function<void(uint8_t *, size_t)> pcmDataCallback;
  20. enum class Whence
  21. {
  22. START,
  23. CURRENT,
  24. END
  25. };
  26. class ChunkedAudioStream
  27. {
  28. private:
  29. // Vorbis related
  30. OggVorbis_File vorbisFile;
  31. ov_callbacks vorbisCallbacks;
  32. int currentSection;
  33. // Audio data
  34. uint32_t duration;
  35. bool loadingChunks = false;
  36. uint16_t lastSequenceId = 0;
  37. std::shared_ptr<MercuryManager> manager;
  38. std::vector<uint8_t> fileId;
  39. uint32_t startPositionMs;
  40. public:
  41. ChunkedAudioStream(std::vector<uint8_t> fileId, std::vector<uint8_t> audioKey, uint32_t duration, std::shared_ptr<MercuryManager> manager, uint32_t startPositionMs, bool isPaused);
  42. ~ChunkedAudioStream();
  43. std::shared_ptr<ChunkedByteStream> byteStream;
  44. std::function<void()> streamFinishedCallback;
  45. std::atomic<bool> isPaused = false;
  46. std::atomic<bool> isRunning = false;
  47. std::atomic<bool> finished = false;
  48. pcmDataCallback pcmCallback;
  49. std::shared_ptr<AudioSink> audioSink;
  50. WrappedMutex seekMutex;
  51. std::vector<uint8_t> read(size_t bytes);
  52. void seekMs(uint32_t positionMs);
  53. void seek(size_t pos, Whence whence);
  54. void startPlaybackLoop(uint8_t *pcmOut, size_t pcmOut_len);
  55. };
  56. #endif