PlaybackState.h 3.4 KB

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