From 2a77d09f11885bea94b253222047206efc8dc26a Mon Sep 17 00:00:00 2001 From: philippe44 Date: Wed, 18 Oct 2023 17:07:29 -0700 Subject: [PATCH] fix Spotify queue modifications - release --- components/spotify/Shim.cpp | 27 ++++++++++++------- components/spotify/cspot/src/SpircHandler.cpp | 9 +++++-- 2 files changed, 24 insertions(+), 12 deletions(-) diff --git a/components/spotify/Shim.cpp b/components/spotify/Shim.cpp index aa33d0917..89d10a0fe 100644 --- a/components/spotify/Shim.cpp +++ b/components/spotify/Shim.cpp @@ -53,6 +53,7 @@ class cspotPlayer : public bell::Task { std::atomic state; std::string credentials; bool zeroConf; + std::atomic flushed = false, notify = true; int startOffset, volume = 0, bitrate = 160; httpd_handle_t serverHandle; @@ -60,6 +61,7 @@ class cspotPlayer : public bell::Task { cspot_cmd_cb_t cmdHandler; cspot_data_cb_t dataHandler; std::string lastTrackId; + cspot::TrackInfo trackInfo; std::shared_ptr blob; std::unique_ptr spirc; @@ -206,11 +208,13 @@ void cspotPlayer::eventHandler(std::unique_ptr event trackStatus = TRACK_INIT; // memorize position for when track's beginning will be detected startOffset = std::get(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: { @@ -219,16 +223,14 @@ void cspotPlayer::eventHandler(std::unique_ptr event break; } case cspot::SpircHandler::EventType::TRACK_INFO: { - auto trackInfo = std::get(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(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; } @@ -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) { diff --git a/components/spotify/cspot/src/SpircHandler.cpp b/components/spotify/cspot/src/SpircHandler.cpp index 4f8fcfb5f..8f225b76e 100644 --- a/components/spotify/cspot/src/SpircHandler.cpp +++ b/components/spotify/cspot/src/SpircHandler.cpp @@ -204,8 +204,13 @@ void SpircHandler::handleFrame(std::vector& 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);