AudioChunk.h 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. public:
  22. std::unique_ptr<Crypto> crypto;
  23. std::vector<uint8_t> decryptedData;
  24. std::vector<uint8_t> audioKey;
  25. bool keepInMemory = false;
  26. pthread_mutex_t loadingMutex;
  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. * @brief AudioChunk handles all audiochunk related operations.
  44. *
  45. * @param seqId Sequence id of requested chunk
  46. * @param audioKey Audio key used for decryption of audio data
  47. * @param startPosition Start position of current chunk in audio file
  48. * @param predictedEndPosition Predicted end position of given chunk. This is not final positon.
  49. */
  50. AudioChunk(uint16_t seqId, std::vector<uint8_t> &audioKey, uint32_t startPosition, uint32_t predictedEndPosition);
  51. ~AudioChunk();
  52. /**
  53. * @brief Appends incoming chunked data to local cache.
  54. *
  55. * @param data encrypted binary audio data.
  56. */
  57. void appendData(std::vector<uint8_t> &data);
  58. /**
  59. * @brief Performs AES CTR decryption of received data.
  60. *
  61. */
  62. void decrypt();
  63. };
  64. #endif