TrackPlayer.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #pragma once
  2. #include <atomic> // for atomic
  3. #include <cstdint> // for uint8_t, int64_t
  4. #include <ctime> // for size_t, time
  5. #include <functional> // for function
  6. #include <memory> // for shared_ptr, unique_ptr
  7. #include <mutex> // for mutex
  8. #include <string_view> // for string_view
  9. #include <vector> // for vector
  10. #include "BellTask.h" // for Task
  11. #include "CDNAudioFile.h"
  12. #include "TrackQueue.h"
  13. namespace bell {
  14. class WrappedSemaphore;
  15. } // namespace bell
  16. #ifdef BELL_VORBIS_FLOAT
  17. #include "vorbis/vorbisfile.h"
  18. #else
  19. #include "ivorbisfile.h" // for OggVorbis_File, ov_callbacks
  20. #endif
  21. namespace cspot {
  22. class TrackProvider;
  23. class TrackQueue;
  24. struct Context;
  25. struct TrackReference;
  26. class TrackPlayer : bell::Task {
  27. public:
  28. // Callback types
  29. typedef std::function<void(std::shared_ptr<QueuedTrack>, bool)>
  30. TrackLoadedCallback;
  31. typedef std::function<size_t(uint8_t*, size_t, std::string_view)>
  32. DataCallback;
  33. typedef std::function<void()> EOFCallback;
  34. typedef std::function<bool()> isAiringCallback;
  35. TrackPlayer(std::shared_ptr<cspot::Context> ctx,
  36. std::shared_ptr<cspot::TrackQueue> trackQueue,
  37. EOFCallback eofCallback, TrackLoadedCallback loadedCallback);
  38. ~TrackPlayer();
  39. void loadTrackFromRef(TrackReference& ref, size_t playbackMs,
  40. bool startAutomatically);
  41. void setDataCallback(DataCallback callback);
  42. // CDNTrackStream::TrackInfo getCurrentTrackInfo();
  43. void seekMs(size_t ms);
  44. void resetState(bool paused = false);
  45. // Vorbis codec callbacks
  46. size_t _vorbisRead(void* ptr, size_t size, size_t nmemb);
  47. size_t _vorbisClose();
  48. int _vorbisSeek(int64_t offset, int whence);
  49. long _vorbisTell();
  50. void stop();
  51. void start();
  52. private:
  53. std::shared_ptr<cspot::Context> ctx;
  54. std::shared_ptr<cspot::TrackQueue> trackQueue;
  55. std::shared_ptr<cspot::CDNAudioFile> currentTrackStream;
  56. std::unique_ptr<bell::WrappedSemaphore> playbackSemaphore;
  57. TrackLoadedCallback trackLoaded;
  58. DataCallback dataCallback = nullptr;
  59. EOFCallback eofCallback;
  60. // Playback control
  61. std::atomic<bool> currentSongPlaying;
  62. std::mutex playbackMutex;
  63. std::mutex dataOutMutex;
  64. // Vorbis related
  65. OggVorbis_File vorbisFile;
  66. ov_callbacks vorbisCallbacks;
  67. int currentSection;
  68. std::vector<uint8_t> pcmBuffer = std::vector<uint8_t>(1024);
  69. bool autoStart = false;
  70. std::atomic<bool> isRunning = false;
  71. std::atomic<bool> pendingReset = false;
  72. std::atomic<bool> inFuture = false;
  73. std::atomic<size_t> pendingSeekPositionMs = 0;
  74. std::atomic<bool> startPaused = false;
  75. std::mutex runningMutex;
  76. void runTask() override;
  77. };
  78. } // namespace cspot