SpircHandler.h 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #pragma once
  2. #include <stdint.h> // for uint32_t, uint8_t
  3. #include <functional> // for function
  4. #include <memory> // for shared_ptr, unique_ptr
  5. #include <string> // for string
  6. #include <variant> // for variant
  7. #include <vector> // for vector
  8. #include "CDNAudioFile.h" // for CDNTrackStream, CDNTrackStream::Track...
  9. #include "TrackQueue.h"
  10. #include "protobuf/spirc.pb.h" // for MessageType
  11. namespace cspot {
  12. class TrackPlayer;
  13. struct Context;
  14. class SpircHandler {
  15. public:
  16. SpircHandler(std::shared_ptr<cspot::Context> ctx);
  17. enum class EventType {
  18. PLAY_PAUSE,
  19. VOLUME,
  20. TRACK_INFO,
  21. DISC,
  22. NEXT,
  23. PREV,
  24. SEEK,
  25. DEPLETED,
  26. FLUSH,
  27. PLAYBACK_START
  28. };
  29. typedef std::variant<TrackInfo, int, bool> EventData;
  30. struct Event {
  31. EventType eventType;
  32. EventData data;
  33. };
  34. typedef std::function<void(std::unique_ptr<Event>)> EventHandler;
  35. void subscribeToMercury();
  36. std::shared_ptr<TrackPlayer> getTrackPlayer();
  37. void setEventHandler(EventHandler handler);
  38. void setPause(bool pause);
  39. bool previousSong();
  40. bool nextSong();
  41. void notifyAudioReachedPlayback();
  42. void notifyAudioEnded();
  43. void updatePositionMs(uint32_t position);
  44. void setRemoteVolume(int volume);
  45. void loadTrackFromURI(const std::string& uri);
  46. std::shared_ptr<cspot::TrackQueue> getTrackQueue() { return trackQueue; }
  47. void disconnect();
  48. private:
  49. std::shared_ptr<cspot::Context> ctx;
  50. std::shared_ptr<cspot::TrackPlayer> trackPlayer;
  51. std::shared_ptr<cspot::TrackQueue> trackQueue;
  52. EventHandler eventHandler = nullptr;
  53. std::shared_ptr<cspot::PlaybackState> playbackState;
  54. void sendCmd(MessageType typ);
  55. void sendEvent(EventType type);
  56. void sendEvent(EventType type, EventData data);
  57. bool skipSong(TrackQueue::SkipDirection dir);
  58. void handleFrame(std::vector<uint8_t>& data);
  59. void notify();
  60. };
  61. } // namespace cspot