2
0

TrackPlayer.h 2.6 KB

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