SpircController.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. ~SpircController();
  49. void subscribe();
  50. /**
  51. * @brief Pauses / Plays current song
  52. *
  53. * Calling this function will pause or resume playback, setting the
  54. * necessary state value and notifying spotify SPIRC.
  55. *
  56. * @param pause if true pause content, play otherwise
  57. */
  58. void setPause(bool pause, bool notifyPlayer = true);
  59. /**
  60. * @brief Toggle Play/Pause
  61. */
  62. void playToggle();
  63. /**
  64. * @brief Notifies spotify servers about volume change
  65. *
  66. * @param volume int between 0 and `MAX_VOLUME`
  67. */
  68. void setRemoteVolume(int volume);
  69. /**
  70. * @brief Set device volume and notifies spotify
  71. *
  72. * @param volume int between 0 and `MAX_VOLUME`
  73. */
  74. void setVolume(int volume);
  75. /**
  76. * @brief change volume by a given value
  77. *
  78. * @param volume int between 0 and `MAX_VOLUME`
  79. */
  80. void adjustVolume(int by);
  81. /**
  82. * @brief Goes back to previous track and notfies spotify SPIRC
  83. */
  84. void prevSong();
  85. /**
  86. * @brief Skips to next track and notfies spotify SPIRC
  87. */
  88. void nextSong();
  89. void setEventHandler(cspotEventHandler handler);
  90. void stopPlayer();
  91. /**
  92. * @brief Disconnect players and notify
  93. */
  94. void disconnect();
  95. };
  96. #endif