AudioChunk.h 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #ifndef AUDIOCHUNK_H
  2. #define AUDIOCHUNK_H
  3. #include <memory>
  4. #include <vector>
  5. #include <string>
  6. #include <algorithm>
  7. #include "platform/WrappedSemaphore.h"
  8. #include "Crypto.h"
  9. #include "Utils.h"
  10. #include <mutex>
  11. class AudioChunk {
  12. private:
  13. /**
  14. * @brief Calculates a correct IV by performing bignum addition.
  15. *
  16. * @param num Number to add to IV.
  17. * @return std::vector<uint8_t>
  18. */
  19. std::vector<uint8_t> getIVSum(uint32_t num);
  20. size_t decryptedCount = 0;
  21. size_t oldStartPos;
  22. public:
  23. std::unique_ptr<Crypto> crypto;
  24. std::vector<uint8_t> decryptedData;
  25. std::vector<uint8_t> audioKey;
  26. bool keepInMemory = false;
  27. std::mutex dataAccessMutex;
  28. uint32_t startPosition;
  29. uint32_t endPosition;
  30. uint16_t seqId;
  31. size_t headerFileSize = -1;
  32. bool isLoaded = false;
  33. bool isFailed = false;
  34. /**
  35. * @brief Triggered when audiochunk is fully downloaded and decrypted.
  36. */
  37. std::unique_ptr<WrappedSemaphore> isLoadedSemaphore;
  38. /**
  39. * @brief
  40. */
  41. std::unique_ptr<WrappedSemaphore> isHeaderFileSizeLoadedSemaphore;
  42. /**
  43. * Decrypts data and writes it to the target buffer
  44. * @param target data buffer to write to
  45. * @param offset data offset
  46. * @param nbytes number of bytes to read
  47. */
  48. void readData(uint8_t *target, size_t offset, size_t nbytes);
  49. /**
  50. * @brief AudioChunk handles all audiochunk related operations.
  51. *
  52. * @param seqId Sequence id of requested chunk
  53. * @param audioKey Audio key used for decryption of audio data
  54. * @param startPosition Start position of current chunk in audio file
  55. * @param predictedEndPosition Predicted end position of given chunk. This is not final positon.
  56. */
  57. AudioChunk(uint16_t seqId, std::vector<uint8_t> &audioKey, uint32_t startPosition, uint32_t predictedEndPosition);
  58. ~AudioChunk();
  59. /**
  60. * @brief Appends incoming chunked data to local cache.
  61. *
  62. * @param data encrypted binary audio data.
  63. */
  64. void appendData(const std::vector<uint8_t> &data);
  65. /**
  66. * @brief Sets loaded status on the chunk
  67. *
  68. */
  69. void finalize();
  70. };
  71. #endif