TrackPlayer.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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>)> TrackLoadedCallback;
  30. typedef std::function<size_t(uint8_t*, size_t, std::string_view)> DataCallback;
  31. typedef std::function<void()> EOFCallback;
  32. typedef std::function<bool()> isAiringCallback;
  33. TrackPlayer(std::shared_ptr<cspot::Context> ctx,
  34. std::shared_ptr<cspot::TrackQueue> trackQueue,
  35. EOFCallback eofCallback, TrackLoadedCallback loadedCallback);
  36. ~TrackPlayer();
  37. void loadTrackFromRef(TrackReference& ref, size_t playbackMs,
  38. bool startAutomatically);
  39. void setDataCallback(DataCallback callback);
  40. // CDNTrackStream::TrackInfo getCurrentTrackInfo();
  41. void seekMs(size_t ms);
  42. void resetState();
  43. // Vorbis codec callbacks
  44. size_t _vorbisRead(void* ptr, size_t size, size_t nmemb);
  45. size_t _vorbisClose();
  46. int _vorbisSeek(int64_t offset, int whence);
  47. long _vorbisTell();
  48. void stop();
  49. void start();
  50. private:
  51. std::shared_ptr<cspot::Context> ctx;
  52. std::shared_ptr<cspot::TrackQueue> trackQueue;
  53. std::shared_ptr<cspot::CDNAudioFile> currentTrackStream;
  54. std::unique_ptr<bell::WrappedSemaphore> playbackSemaphore;
  55. TrackLoadedCallback trackLoaded;
  56. DataCallback dataCallback = nullptr;
  57. EOFCallback eofCallback;
  58. // Playback control
  59. std::atomic<bool> currentSongPlaying;
  60. std::mutex playbackMutex;
  61. std::mutex dataOutMutex;
  62. // Vorbis related
  63. OggVorbis_File vorbisFile;
  64. ov_callbacks vorbisCallbacks;
  65. int currentSection;
  66. std::vector<uint8_t> pcmBuffer = std::vector<uint8_t>(1024);
  67. bool autoStart = false;
  68. std::atomic<bool> isRunning = false;
  69. std::atomic<bool> pendingReset = false;
  70. std::atomic<bool> inFuture = false;
  71. std::atomic<size_t> pendingSeekPositionMs = 0;
  72. std::mutex runningMutex;
  73. void runTask() override;
  74. };
  75. } // namespace cspot