AudioChunk.h 2.2 KB

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