2
0

SpircController.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #ifndef SPIRCCONTROLLER_H
  2. #define SPIRCCONTROLLER_H
  3. #include <vector>
  4. #include <string>
  5. #include <functional>
  6. #include "Utils.h"
  7. #include "MercuryManager.h"
  8. #include "Session.h"
  9. #include "PlayerState.h"
  10. #include "SpotifyTrack.h"
  11. #include "ConstantParameters.h"
  12. #include "Player.h"
  13. #include "ConfigJSON.h"
  14. #include <cassert>
  15. #include <variant>
  16. enum class CSpotEventType {
  17. PLAY_PAUSE,
  18. VOLUME,
  19. TRACK_INFO,
  20. DISC,
  21. NEXT,
  22. PREV,
  23. SEEK,
  24. LOAD,
  25. };
  26. struct CSpotEvent {
  27. CSpotEventType eventType;
  28. std::variant<TrackInfo, int, bool> data;
  29. };
  30. typedef std::function<void(CSpotEvent&)> cspotEventHandler;
  31. class SpircController {
  32. private:
  33. std::shared_ptr<MercuryManager> manager;
  34. std::string username;
  35. bool firstFrame = true;
  36. std::unique_ptr<Player> player;
  37. std::unique_ptr<PlayerState> state;
  38. std::shared_ptr<AudioSink> audioSink;
  39. std::shared_ptr<ConfigJSON> config;
  40. cspotEventHandler eventHandler;
  41. void sendCmd(MessageType typ);
  42. void notify();
  43. void sendEvent(CSpotEventType eventType, std::variant<TrackInfo, int, bool> data = 0);
  44. void handleFrame(std::vector<uint8_t> &data);
  45. void loadTrack(uint32_t position_ms = 0, bool isPaused = 0);
  46. public:
  47. SpircController(std::shared_ptr<MercuryManager> manager, std::string username, std::shared_ptr<AudioSink> audioSink);
  48. void subscribe();
  49. /**
  50. * @brief Pauses / Plays current song
  51. *
  52. * Calling this function will pause or resume playback, setting the
  53. * necessary state value and notifying spotify SPIRC.
  54. *
  55. * @param pause if true pause content, play otherwise
  56. */
  57. void setPause(bool pause, bool notifyPlayer = true);
  58. /**
  59. * @brief Toggle Play/Pause
  60. */
  61. void playToggle();
  62. /**
  63. * @brief Notifies spotify servers about volume change
  64. *
  65. * @param volume int between 0 and `MAX_VOLUME`
  66. */
  67. void setRemoteVolume(int volume);
  68. /**
  69. * @brief Set device volume and notifies spotify
  70. *
  71. * @param volume int between 0 and `MAX_VOLUME`
  72. */
  73. void setVolume(int volume);
  74. /**
  75. * @brief change volume by a given value
  76. *
  77. * @param volume int between 0 and `MAX_VOLUME`
  78. */
  79. void adjustVolume(int by);
  80. /**
  81. * @brief Goes back to previous track and notfies spotify SPIRC
  82. */
  83. void prevSong();
  84. /**
  85. * @brief Skips to next track and notfies spotify SPIRC
  86. */
  87. void nextSong();
  88. void setEventHandler(cspotEventHandler handler);
  89. void stopPlayer();
  90. /**
  91. * @brief Disconnect players and notify
  92. */
  93. void disconnect();
  94. };
  95. #endif