PlaybackState.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. #pragma once
  2. #include <stdint.h> // for uint8_t, uint32_t
  3. #include <memory> // for shared_ptr
  4. #include <string> // for string
  5. #include <vector> // for vector
  6. #include "protobuf/spirc.pb.h" // for Frame, TrackRef, CapabilityType, Mess...
  7. namespace cspot {
  8. struct Context;
  9. class PlaybackState {
  10. private:
  11. std::shared_ptr<cspot::Context> ctx;
  12. uint32_t seqNum = 0;
  13. uint8_t capabilityIndex = 0;
  14. std::vector<uint8_t> frameData;
  15. void addCapability(
  16. CapabilityType typ, int intValue = -1,
  17. std::vector<std::string> stringsValue = std::vector<std::string>());
  18. public:
  19. Frame innerFrame;
  20. Frame remoteFrame;
  21. enum class State { Playing, Stopped, Loading, Paused };
  22. /**
  23. * @brief Player state represents the current state of player.
  24. *
  25. * Responsible for keeping track of player's state. Doesn't control the playback itself.
  26. *
  27. * @param timeProvider synced time provider
  28. */
  29. PlaybackState(std::shared_ptr<cspot::Context> ctx);
  30. ~PlaybackState();
  31. /**
  32. * @brief Updates state according to current playback state.
  33. *
  34. * @param state playback state
  35. */
  36. void setPlaybackState(const PlaybackState::State state);
  37. /**
  38. * @brief Sets player activity
  39. *
  40. * @param isActive activity status
  41. */
  42. void setActive(bool isActive);
  43. /**
  44. * @brief Simple getter
  45. *
  46. * @return true player is active
  47. * @return false player is inactive
  48. */
  49. bool isActive();
  50. /**
  51. * @brief Updates local track position.
  52. *
  53. * @param position position in milliseconds
  54. */
  55. void updatePositionMs(uint32_t position);
  56. /**
  57. * @brief Sets local volume on internal state.
  58. *
  59. * @param volume volume between 0 and UINT16 max
  60. */
  61. void setVolume(uint32_t volume);
  62. /**
  63. * @brief Enables queue shuffling.
  64. *
  65. * Sets shuffle parameter on local frame, and in case shuffling is enabled,
  66. * it will randomize the entire local queue.
  67. *
  68. * @param shuffle whenever should shuffle
  69. */
  70. void setShuffle(bool shuffle);
  71. /**
  72. * @brief Enables repeat
  73. *
  74. * @param repeat should repeat param
  75. */
  76. void setRepeat(bool repeat);
  77. /**
  78. * @brief Updates local track queue from remote data.
  79. */
  80. void updateTracks();
  81. /**
  82. * @brief Changes playback to next queued track.
  83. *
  84. * Will go back to first track if current track is last track in queue.
  85. * In that case, it will pause if repeat is disabled.
  86. */
  87. bool nextTrack();
  88. /**
  89. * @brief Changes playback to previous queued track.
  90. *
  91. * Will stop if current track is the first track in queue and repeat is disabled.
  92. * If repeat is enabled, it will loop back to the last track in queue.
  93. */
  94. void prevTrack();
  95. /**
  96. * @brief Gets the current track reference.
  97. *
  98. * @return std::shared_ptr<TrackReference> pointer to track reference
  99. */
  100. TrackRef* getCurrentTrackRef();
  101. /**
  102. * @brief Gets reference to next track in queue, or nullptr if there is no next track.
  103. *
  104. * @return std::shared_ptr<TrackReference> pointer to track reference
  105. */
  106. TrackRef* getNextTrackRef();
  107. /**
  108. * @brief Encodes current frame into binary data via protobuf.
  109. *
  110. * @param typ message type to include in frame type
  111. * @return std::vector<uint8_t> binary frame data
  112. */
  113. std::vector<uint8_t> encodeCurrentFrame(MessageType typ);
  114. };
  115. } // namespace cspot