Skip to content

Commit

Permalink
do not reload track when changing queue (if possible) (#164)
Browse files Browse the repository at this point in the history
* do not reload track when changing queue (if possible)

* simpler solution

* replace frame of further tracks

* buffer rollback

* feat: Update bell, fix formatting

---------

Co-authored-by: Filip Grzywok <[email protected]>
  • Loading branch information
philippe44 and feelfreelinux authored Nov 20, 2023
1 parent 34f6436 commit 7e3930d
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 16 deletions.
2 changes: 1 addition & 1 deletion cspot/bell
Submodule bell updated 0 files
1 change: 0 additions & 1 deletion cspot/include/TrackPlayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ class TrackPlayer : bell::Task {
typedef std::function<size_t(uint8_t*, size_t, std::string_view)>
DataCallback;
typedef std::function<void()> EOFCallback;
typedef std::function<bool()> isAiringCallback;

TrackPlayer(std::shared_ptr<cspot::Context> ctx,
std::shared_ptr<cspot::TrackQueue> trackQueue,
Expand Down
3 changes: 2 additions & 1 deletion cspot/include/TrackQueue.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ class QueuedTrack {

uint32_t requestedPosition;
std::string identifier;
bool loading = false;

// Will return nullptr if the track is not ready
std::shared_ptr<cspot::CDNAudioFile> getAudioFile();
Expand Down Expand Up @@ -100,7 +101,7 @@ class TrackQueue : public bell::Task {
bool hasTracks();
bool isFinished();
bool skipTrack(SkipDirection dir, bool expectNotify = true);
void updateTracks(uint32_t requestedPosition = 0, bool initial = false);
bool updateTracks(uint32_t requestedPosition = 0, bool initial = false);
TrackInfo getTrackInfo(std::string_view identifier);
std::shared_ptr<QueuedTrack> consumeTrack(
std::shared_ptr<QueuedTrack> prevSong, int& offset);
Expand Down
12 changes: 8 additions & 4 deletions cspot/src/SpircHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -201,20 +201,24 @@ void SpircHandler::handleFrame(std::vector<uint8_t>& data) {
break;
}
case MessageType_kMessageTypeReplace: {
CSPOT_LOG(debug, "Got replace frame");
CSPOT_LOG(debug, "Got replace frame %d",
playbackState->remoteTracks.size());
playbackState->syncWithRemote();

// 1st track is the current one, but update the position
trackQueue->updateTracks(
bool cleared = trackQueue->updateTracks(
playbackState->remoteFrame.state.position_ms +
ctx->timeProvider->getSyncedTimestamp() -
playbackState->innerFrame.state.position_measured_at,
false);

this->notify();

sendEvent(EventType::FLUSH);
trackPlayer->resetState();
// need to re-load all if streaming track is completed
if (cleared) {
sendEvent(EventType::FLUSH);
trackPlayer->resetState();
}
break;
}
case MessageType_kMessageTypeShuffle: {
Expand Down
2 changes: 2 additions & 0 deletions cspot/src/TrackPlayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ void TrackPlayer::runTask() {
}

eof = false;
track->loading = true;

CSPOT_LOG(info, "Playing");

Expand Down Expand Up @@ -255,6 +256,7 @@ void TrackPlayer::runTask() {

// always move back to LOADING (ensure proper seeking after last track has been loaded)
currentTrackStream = nullptr;
track->loading = false;
}

if (eof) {
Expand Down
29 changes: 20 additions & 9 deletions cspot/src/TrackQueue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -587,18 +587,18 @@ bool TrackQueue::isFinished() {
return currentTracksIndex >= currentTracks.size() - 1;
}

void TrackQueue::updateTracks(uint32_t requestedPosition, bool initial) {
bool TrackQueue::updateTracks(uint32_t requestedPosition, bool initial) {
std::scoped_lock lock(tracksMutex);
bool cleared = true;

// Copy requested track list
currentTracks = playbackState->remoteTracks;
currentTracksIndex = playbackState->innerFrame.state.playing_track_index;

if (initial) {
// Clear preloaded tracks
preloadedTracks.clear();

// Copy requested track list
currentTracks = playbackState->remoteTracks;

currentTracksIndex = playbackState->innerFrame.state.playing_track_index;

if (currentTracksIndex < currentTracks.size()) {
// Push a song on the preloaded queue
queueNextTrack(0, requestedPosition);
Expand All @@ -608,14 +608,25 @@ void TrackQueue::updateTracks(uint32_t requestedPosition, bool initial) {
notifyPending = true;

playableSemaphore->give();
} else if (preloadedTracks[0]->loading) {
// try to not re-load track if we are still loading it

// remove everything except first track
preloadedTracks.erase(preloadedTracks.begin() + 1, preloadedTracks.end());

// Push a song on the preloaded queue
CSPOT_LOG(info, "Keeping current track %d", currentTracksIndex);
queueNextTrack(1);

cleared = false;
} else {
// Clear preloaded tracks
preloadedTracks.clear();

// Copy requested track list
currentTracks = playbackState->remoteTracks;

// Push a song on the preloaded queue
CSPOT_LOG(info, "Re-loading current track");
queueNextTrack(0, requestedPosition);
}

return cleared;
}
4 changes: 4 additions & 0 deletions targets/cli/CliPlayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ CliPlayer::CliPlayer(std::unique_ptr<AudioSink> sink,
case cspot::SpircHandler::EventType::DEPLETED:
this->playlistEnd = true;
break;
case cspot::SpircHandler::EventType::VOLUME: {
int volume = std::get<int>(event->data);
break;
}
default:
break;
}
Expand Down

0 comments on commit 7e3930d

Please sign in to comment.