SpircController.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. PLAYBACK_START
  26. };
  27. struct CSpotEvent {
  28. CSpotEventType eventType;
  29. std::variant<TrackInfo, int, bool> data;
  30. };
  31. typedef std::function<void(CSpotEvent&)> cspotEventHandler;
  32. class SpircController {
  33. private:
  34. std::shared_ptr<MercuryManager> manager;
  35. std::string username;
  36. bool firstFrame = true;
  37. std::unique_ptr<Player> player;
  38. std::unique_ptr<PlayerState> state;
  39. std::shared_ptr<AudioSink> audioSink;
  40. std::shared_ptr<ConfigJSON> config;
  41. cspotEventHandler eventHandler;
  42. void sendCmd(MessageType typ);
  43. void notify();
  44. void sendEvent(CSpotEventType eventType, std::variant<TrackInfo, int, bool> data = 0);
  45. void handleFrame(std::vector<uint8_t> &data);
  46. void loadTrack(uint32_t position_ms = 0, bool isPaused = 0);
  47. public:
  48. SpircController(std::shared_ptr<MercuryManager> manager, std::string username, std::shared_ptr<AudioSink> audioSink);
  49. ~SpircController();
  50. void subscribe();
  51. /**
  52. * @brief Pauses / Plays current song
  53. *
  54. * Calling this function will pause or resume playback, setting the
  55. * necessary state value and notifying spotify SPIRC.
  56. *
  57. * @param pause if true pause content, play otherwise
  58. */
  59. void setPause(bool pause, bool notifyPlayer = true);
  60. /**
  61. * @brief Toggle Play/Pause
  62. */
  63. void playToggle();
  64. /**
  65. * @brief Notifies spotify servers about volume change
  66. *
  67. * @param volume int between 0 and `MAX_VOLUME`
  68. */
  69. void setRemoteVolume(int volume);
  70. /**
  71. * @brief Set device volume and notifies spotify
  72. *
  73. * @param volume int between 0 and `MAX_VOLUME`
  74. */
  75. void setVolume(int volume);
  76. /**
  77. * @brief change volume by a given value
  78. *
  79. * @param volume int between 0 and `MAX_VOLUME`
  80. */
  81. void adjustVolume(int by);
  82. /**
  83. * @brief Goes back to previous track and notfies spotify SPIRC
  84. */
  85. void prevSong();
  86. /**
  87. * @brief Skips to next track and notfies spotify SPIRC
  88. */
  89. void nextSong();
  90. void setEventHandler(cspotEventHandler handler);
  91. void stopPlayer();
  92. /**
  93. * @brief Disconnect players and notify
  94. */
  95. void disconnect();
  96. };
  97. #endif