TrackPlayer.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. #include "TrackPlayer.h"
  2. #include <cstddef>
  3. #include <fstream>
  4. #include <memory>
  5. #include <mutex>
  6. #include <vector>
  7. #include "CDNTrackStream.h"
  8. #include "Logger.h"
  9. #include "TrackReference.h"
  10. using namespace cspot;
  11. static size_t vorbisReadCb(void* ptr, size_t size, size_t nmemb,
  12. TrackPlayer* self) {
  13. return self->_vorbisRead(ptr, size, nmemb);
  14. }
  15. static int vorbisCloseCb(TrackPlayer* self) {
  16. return self->_vorbisClose();
  17. }
  18. static int vorbisSeekCb(TrackPlayer* self, int64_t offset, int whence) {
  19. return self->_vorbisSeek(offset, whence);
  20. }
  21. static long vorbisTellCb(TrackPlayer* self) {
  22. return self->_vorbisTell();
  23. }
  24. TrackPlayer::TrackPlayer(std::shared_ptr<cspot::Context> ctx, isAiringCallback isAiring, EOFCallback eof, TrackLoadedCallback trackLoaded)
  25. : bell::Task("cspot_player", 48 * 1024, 5, 1) {
  26. this->ctx = ctx;
  27. this->isAiring = isAiring;
  28. this->eofCallback = eof;
  29. this->trackLoaded = trackLoaded;
  30. this->trackProvider = std::make_shared<cspot::TrackProvider>(ctx);
  31. this->playbackSemaphore = std::make_unique<bell::WrappedSemaphore>(5);
  32. // Initialize vorbis callbacks
  33. vorbisFile = {};
  34. vorbisCallbacks = {
  35. (decltype(ov_callbacks::read_func))&vorbisReadCb,
  36. (decltype(ov_callbacks::seek_func))&vorbisSeekCb,
  37. (decltype(ov_callbacks::close_func))&vorbisCloseCb,
  38. (decltype(ov_callbacks::tell_func))&vorbisTellCb,
  39. };
  40. isRunning = true;
  41. startTask();
  42. }
  43. TrackPlayer::~TrackPlayer() {
  44. isRunning = false;
  45. std::scoped_lock lock(runningMutex);
  46. }
  47. void TrackPlayer::loadTrackFromRef(TrackReference& ref, size_t positionMs,
  48. bool startAutomatically) {
  49. this->playbackPosition = positionMs;
  50. this->autoStart = startAutomatically;
  51. auto nextTrack = trackProvider->loadFromTrackRef(ref);
  52. stopTrack();
  53. this->sequence++;
  54. this->currentTrackStream = nextTrack;
  55. this->playbackSemaphore->give();
  56. }
  57. void TrackPlayer::stopTrack() {
  58. this->currentSongPlaying = false;
  59. std::scoped_lock lock(playbackMutex);
  60. }
  61. void TrackPlayer::seekMs(size_t ms) {
  62. std::scoped_lock lock(seekMutex);
  63. #ifdef BELL_VORBIS_FLOAT
  64. ov_time_seek(&vorbisFile, (double)ms / 1000);
  65. #else
  66. ov_time_seek(&vorbisFile, ms);
  67. #endif
  68. }
  69. void TrackPlayer::runTask() {
  70. std::scoped_lock lock(runningMutex);
  71. while (isRunning) {
  72. this->playbackSemaphore->twait(100);
  73. if (this->currentTrackStream == nullptr) {
  74. continue;
  75. }
  76. CSPOT_LOG(info, "Player received a track, waiting for it to be ready...");
  77. // when track changed many times and very quickly, we are stuck on never-given semaphore
  78. while (this->currentTrackStream->trackReady->twait(250));
  79. CSPOT_LOG(info, "Got track");
  80. if (this->currentTrackStream->status == CDNTrackStream::Status::FAILED) {
  81. CSPOT_LOG(error, "Track failed to load, skipping it");
  82. this->currentTrackStream = nullptr;
  83. this->eofCallback();
  84. continue;
  85. }
  86. this->currentSongPlaying = true;
  87. this->trackLoaded();
  88. this->playbackMutex.lock();
  89. int32_t r = ov_open_callbacks(this, &vorbisFile, NULL, 0, vorbisCallbacks);
  90. if (playbackPosition > 0) {
  91. #ifdef BELL_VORBIS_FLOAT
  92. ov_time_seek(&vorbisFile, (double)playbackPosition / 1000);
  93. #else
  94. ov_time_seek(&vorbisFile, playbackPosition);
  95. #endif
  96. }
  97. bool eof = false;
  98. while (!eof && currentSongPlaying) {
  99. seekMutex.lock();
  100. #ifdef BELL_VORBIS_FLOAT
  101. long ret = ov_read(&vorbisFile, (char*)&pcmBuffer[0], pcmBuffer.size(),
  102. 0, 2, 1, &currentSection);
  103. #else
  104. long ret = ov_read(&vorbisFile, (char*)&pcmBuffer[0], pcmBuffer.size(),
  105. &currentSection);
  106. #endif
  107. seekMutex.unlock();
  108. if (ret == 0) {
  109. CSPOT_LOG(info, "EOF");
  110. // and done :)
  111. eof = true;
  112. } else if (ret < 0) {
  113. CSPOT_LOG(error, "An error has occured in the stream %d", ret);
  114. currentSongPlaying = false;
  115. } else {
  116. if (this->dataCallback != nullptr) {
  117. auto toWrite = ret;
  118. while (!eof && currentSongPlaying && toWrite > 0) {
  119. auto written =
  120. dataCallback(pcmBuffer.data() + (ret - toWrite), toWrite,
  121. this->currentTrackStream->trackInfo.trackId, this->sequence);
  122. if (written == 0) {
  123. BELL_SLEEP_MS(50);
  124. }
  125. toWrite -= written;
  126. }
  127. }
  128. }
  129. }
  130. ov_clear(&vorbisFile);
  131. // With very large buffers, track N+1 can be downloaded while N has not aired yet and
  132. // if we continue, the currentTrackStream will be emptied, causing a crash in
  133. // notifyAudioReachedPlayback when it will look for trackInfo. A busy loop is never
  134. // ideal, but this low impact, infrequent and more simple than yet another semaphore
  135. while (currentSongPlaying && !isAiring()) {
  136. BELL_SLEEP_MS(100);
  137. }
  138. // always move back to LOADING (ensure proper seeking after last track has been loaded)
  139. this->currentTrackStream.reset();
  140. this->playbackMutex.unlock();
  141. if (eof) {
  142. this->eofCallback();
  143. }
  144. }
  145. }
  146. size_t TrackPlayer::_vorbisRead(void* ptr, size_t size, size_t nmemb) {
  147. if (this->currentTrackStream == nullptr) {
  148. return 0;
  149. }
  150. return this->currentTrackStream->readBytes((uint8_t*)ptr, nmemb * size);
  151. }
  152. size_t TrackPlayer::_vorbisClose() {
  153. return 0;
  154. }
  155. int TrackPlayer::_vorbisSeek(int64_t offset, int whence) {
  156. if (this->currentTrackStream == nullptr) {
  157. return 0;
  158. }
  159. switch (whence) {
  160. case 0:
  161. this->currentTrackStream->seek(offset); // Spotify header offset
  162. break;
  163. case 1:
  164. this->currentTrackStream->seek(this->currentTrackStream->getPosition() +
  165. offset);
  166. break;
  167. case 2:
  168. this->currentTrackStream->seek(this->currentTrackStream->getSize() +
  169. offset);
  170. break;
  171. }
  172. return 0;
  173. }
  174. long TrackPlayer::_vorbisTell() {
  175. if (this->currentTrackStream == nullptr) {
  176. return 0;
  177. }
  178. return this->currentTrackStream->getPosition();
  179. }
  180. CDNTrackStream::TrackInfo TrackPlayer::getCurrentTrackInfo() {
  181. return this->currentTrackStream->trackInfo;
  182. }
  183. void TrackPlayer::setDataCallback(DataCallback callback) {
  184. this->dataCallback = callback;
  185. }