SpircController.h 2.5 KB

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