AudioChunkManager.h 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #ifndef AUDIOCHUNKMANAGER_H
  2. #define AUDIOCHUNKMANAGER_H
  3. #include <memory>
  4. #include <atomic>
  5. #include <algorithm>
  6. #include <mutex>
  7. #include "Utils.h"
  8. #include "AudioChunk.h"
  9. #include "Queue.h"
  10. #include "Task.h"
  11. #define DATA_SIZE_HEADER 24
  12. #define DATA_SIZE_FOOTER 2
  13. class AudioChunkManager : public bell::Task {
  14. std::vector<std::shared_ptr<AudioChunk>> chunks;
  15. bell::Queue<std::pair<std::vector<uint8_t>, bool>> audioChunkDataQueue;
  16. void runTask();
  17. public:
  18. AudioChunkManager();
  19. std::atomic<bool> isRunning = false;
  20. std::mutex runningMutex;
  21. /**
  22. * @brief Registers a new audio chunk request.
  23. *
  24. * Registering an audiochunk will trigger a request to spotify servers.
  25. * All the incoming data will be redirected to this given audiochunk.
  26. *
  27. * @param seqId sequence identifier of given audio chunk.
  28. * @param audioKey audio key of given file, used for decryption.
  29. * @param startPos start position of audio chunk
  30. * @param endPos end position of audio chunk. end - pos % 4 must be 0.
  31. * @return std::shared_ptr<AudioChunk> registered audio chunk. Does not contain the data yet.
  32. */
  33. std::shared_ptr<AudioChunk> registerNewChunk(uint16_t seqId, std::vector<uint8_t> &audioKey, uint32_t startPos, uint32_t endPos);
  34. /**
  35. * @brief Pushes binary data from spotify's servers containing audio chunks.
  36. *
  37. * This method pushes received data to a queue that is then received by manager's thread.
  38. * That thread parses the data and passes it to a matching audio chunk.
  39. *
  40. * @param data binary data received from spotify's servers
  41. * @param failed whenever given chunk request failed
  42. */
  43. void handleChunkData(std::vector<uint8_t>& data, bool failed = false);
  44. /**
  45. * @brief Fails all requested chunks, used for reconnection.
  46. */
  47. void failAllChunks();
  48. void close();
  49. };
  50. #endif