Skip to content

Commit

Permalink
fix Spotify queue modifications - release
Browse files Browse the repository at this point in the history
  • Loading branch information
philippe44 committed Oct 19, 2023
1 parent d03678e commit 2a77d09
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 12 deletions.
27 changes: 17 additions & 10 deletions components/spotify/Shim.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,15 @@ class cspotPlayer : public bell::Task {
std::atomic<states> state;
std::string credentials;
bool zeroConf;
std::atomic<bool> flushed = false, notify = true;

int startOffset, volume = 0, bitrate = 160;
httpd_handle_t serverHandle;
int serverPort;
cspot_cmd_cb_t cmdHandler;
cspot_data_cb_t dataHandler;
std::string lastTrackId;
cspot::TrackInfo trackInfo;

std::shared_ptr<cspot::LoginBlob> blob;
std::unique_ptr<cspot::SpircHandler> spirc;
Expand Down Expand Up @@ -206,11 +208,13 @@ void cspotPlayer::eventHandler(std::unique_ptr<cspot::SpircHandler::Event> event
trackStatus = TRACK_INIT;
// memorize position for when track's beginning will be detected
startOffset = std::get<int>(event->data);
notify = !flushed;
flushed = false;
// Spotify servers do not send volume at connection
spirc->setRemoteVolume(volume);

cmdHandler(CSPOT_START, 44100);
CSPOT_LOG(info, "(re)start playing");
CSPOT_LOG(info, "(re)start playing at %d", startOffset);
break;
}
case cspot::SpircHandler::EventType::PLAY_PAUSE: {
Expand All @@ -219,16 +223,14 @@ void cspotPlayer::eventHandler(std::unique_ptr<cspot::SpircHandler::Event> event
break;
}
case cspot::SpircHandler::EventType::TRACK_INFO: {
auto trackInfo = std::get<cspot::TrackInfo>(event->data);
cmdHandler(CSPOT_TRACK_INFO, trackInfo.duration, startOffset, trackInfo.artist.c_str(),
trackInfo.album.c_str(), trackInfo.name.c_str(), trackInfo.imageUrl.c_str());
spirc->updatePositionMs(startOffset);
startOffset = 0;
trackInfo = std::get<cspot::TrackInfo>(event->data);
break;
}
case cspot::SpircHandler::EventType::FLUSH:
flushed = true;
__attribute__ ((fallthrough));
case cspot::SpircHandler::EventType::NEXT:
case cspot::SpircHandler::EventType::PREV:
case cspot::SpircHandler::EventType::FLUSH: {
case cspot::SpircHandler::EventType::PREV: {
cmdHandler(CSPOT_FLUSH);
break;
}
Expand Down Expand Up @@ -411,8 +413,13 @@ void cspotPlayer::runTask() {
uint32_t started;
cmdHandler(CSPOT_QUERY_STARTED, &started);
if (started) {
CSPOT_LOG(info, "next track's audio has reached DAC");
spirc->notifyAudioReachedPlayback();
CSPOT_LOG(info, "next track's audio has reached DAC (offset %d)", startOffset);
if (notify) spirc->notifyAudioReachedPlayback();
else notify = true;
cmdHandler(CSPOT_TRACK_INFO, trackInfo.duration, startOffset, trackInfo.artist.c_str(),
trackInfo.album.c_str(), trackInfo.name.c_str(), trackInfo.imageUrl.c_str());
spirc->updatePositionMs(startOffset);
startOffset = 0;
trackStatus = TRACK_STREAM;
}
} else if (trackStatus == TRACK_END) {
Expand Down
9 changes: 7 additions & 2 deletions components/spotify/cspot/src/SpircHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -204,8 +204,13 @@ void SpircHandler::handleFrame(std::vector<uint8_t>& data) {
CSPOT_LOG(debug, "Got replace frame");
playbackState->syncWithRemote();

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

this->notify();

sendEvent(EventType::FLUSH);
Expand Down

0 comments on commit 2a77d09

Please sign in to comment.