AudioChunkManager.h 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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 "BellTask.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. std::mutex chunkMutex;
  22. /**
  23. * @brief Registers a new audio chunk request.
  24. *
  25. * Registering an audiochunk will trigger a request to spotify servers.
  26. * All the incoming data will be redirected to this given audiochunk.
  27. *
  28. * @param seqId sequence identifier of given audio chunk.
  29. * @param audioKey audio key of given file, used for decryption.
  30. * @param startPos start position of audio chunk
  31. * @param endPos end position of audio chunk. end - pos % 4 must be 0.
  32. * @return std::shared_ptr<AudioChunk> registered audio chunk. Does not contain the data yet.
  33. */
  34. std::shared_ptr<AudioChunk> registerNewChunk(uint16_t seqId, std::vector<uint8_t> &audioKey, uint32_t startPos, uint32_t endPos);
  35. /**
  36. * @brief Pushes binary data from spotify's servers containing audio chunks.
  37. *
  38. * This method pushes received data to a queue that is then received by manager's thread.
  39. * That thread parses the data and passes it to a matching audio chunk.
  40. *
  41. * @param data binary data received from spotify's servers
  42. * @param failed whenever given chunk request failed
  43. */
  44. void handleChunkData(std::vector<uint8_t>& data, bool failed = false);
  45. /**
  46. * @brief Fails all requested chunks, used for reconnection.
  47. */
  48. void failAllChunks();
  49. void close();
  50. };
  51. #endif