From 8280bc490301a6f8743896b90a8dc8328efd078c Mon Sep 17 00:00:00 2001 From: philippe44 Date: Sun, 8 Oct 2023 17:15:14 -0700 Subject: [PATCH] catchup with cspot "official" - release --- CHANGELOG | 3 + .../bell/main/utilities/include/BellLogger.h | 9 +-- .../spotify/cspot/include/TrackPlayer.h | 3 +- components/spotify/cspot/src/SpircHandler.cpp | 14 ++-- components/spotify/cspot/src/TrackQueue.cpp | 76 ++++++++++--------- 5 files changed, 56 insertions(+), 49 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 8069f55d..e297efd5 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,6 @@ +2023-10.07 + - catchup with official cspot + 2023-10-06 - fix cspot PREV on first track, NEXT on last track and normal ending - use DMA_AUTO for SPI diff --git a/components/spotify/cspot/bell/main/utilities/include/BellLogger.h b/components/spotify/cspot/bell/main/utilities/include/BellLogger.h index d53e705a..7e336886 100644 --- a/components/spotify/cspot/bell/main/utilities/include/BellLogger.h +++ b/components/spotify/cspot/bell/main/utilities/include/BellLogger.h @@ -101,11 +101,10 @@ class BellLogger : public bell::AbstractLogger { gmt_time = localtime(&now_time); std::cout << std::put_time(gmt_time, "[%H:%M:%S") << '.' << std::setfill('0') << std::setw(3) << nowMs.count() << "] "; - } - else { - gmt_time = gmtime(&now_time); - std::cout << std::put_time(gmt_time, "[%Y-%m-%d %H:%M:%S") << '.' - << std::setfill('0') << std::setw(3) << nowMs.count() << "] "; + } else { + gmt_time = gmtime(&now_time); + std::cout << std::put_time(gmt_time, "[%Y-%m-%d %H:%M:%S") << '.' + << std::setfill('0') << std::setw(3) << nowMs.count() << "] "; } } } diff --git a/components/spotify/cspot/include/TrackPlayer.h b/components/spotify/cspot/include/TrackPlayer.h index 0a409865..31f98939 100644 --- a/components/spotify/cspot/include/TrackPlayer.h +++ b/components/spotify/cspot/include/TrackPlayer.h @@ -32,7 +32,8 @@ struct TrackReference; class TrackPlayer : bell::Task { public: // Callback types - typedef std::function, bool)> TrackLoadedCallback; + typedef std::function, bool)> + TrackLoadedCallback; typedef std::function DataCallback; typedef std::function EOFCallback; diff --git a/components/spotify/cspot/src/SpircHandler.cpp b/components/spotify/cspot/src/SpircHandler.cpp index cd5da7a6..4f8fcfb5 100644 --- a/components/spotify/cspot/src/SpircHandler.cpp +++ b/components/spotify/cspot/src/SpircHandler.cpp @@ -31,8 +31,10 @@ SpircHandler::SpircHandler(std::shared_ptr ctx) { } }; - auto trackLoadedCallback = [this](std::shared_ptr track, bool paused = false) { - playbackState->setPlaybackState(paused ? PlaybackState::State::Paused : PlaybackState::State::Playing); + auto trackLoadedCallback = [this](std::shared_ptr track, + bool paused = false) { + playbackState->setPlaybackState(paused ? PlaybackState::State::Paused + : PlaybackState::State::Playing); playbackState->updatePositionMs(track->requestedPosition); this->notify(); @@ -78,9 +80,9 @@ void SpircHandler::subscribeToMercury() { void SpircHandler::loadTrackFromURI(const std::string& uri) {} void SpircHandler::notifyAudioEnded() { - playbackState->updatePositionMs(0); - notify(); - trackPlayer->resetState(true); + playbackState->updatePositionMs(0); + notify(); + trackPlayer->resetState(true); } void SpircHandler::notifyAudioReachedPlayback() { @@ -234,7 +236,7 @@ void SpircHandler::notify() { this->sendCmd(MessageType_kMessageTypeNotify); } -bool SpircHandler::skipSong(TrackQueue::SkipDirection dir) { +bool SpircHandler::skipSong(TrackQueue::SkipDirection dir) { bool skipped = trackQueue->skipTrack(dir); // Reset track state diff --git a/components/spotify/cspot/src/TrackQueue.cpp b/components/spotify/cspot/src/TrackQueue.cpp index 4369f3cf..b859539f 100644 --- a/components/spotify/cspot/src/TrackQueue.cpp +++ b/components/spotify/cspot/src/TrackQueue.cpp @@ -510,9 +510,9 @@ bool TrackQueue::queueNextTrack(int offset, uint32_t positionMs) { } // in case we re-queue current track, make sure position is updated (0) - if (offset == 0 && preloadedTracks.size() && + if (offset == 0 && preloadedTracks.size() && preloadedTracks[0]->ref == currentTracks[currentTracksIndex]) { - preloadedTracks.pop_front(); + preloadedTracks.pop_front(); } if (offset <= 0) { @@ -527,51 +527,53 @@ bool TrackQueue::queueNextTrack(int offset, uint32_t positionMs) { } bool TrackQueue::skipTrack(SkipDirection dir, bool expectNotify) { - bool skipped = true; - std::scoped_lock lock(tracksMutex); + bool skipped = true; + std::scoped_lock lock(tracksMutex); - if (dir == SkipDirection::PREV) { - uint64_t position = !playbackState->innerFrame.state.has_position_ms ? 0 : - playbackState->innerFrame.state.position_ms + - ctx->timeProvider->getSyncedTimestamp() - - playbackState->innerFrame.state.position_measured_at; + if (dir == SkipDirection::PREV) { + uint64_t position = + !playbackState->innerFrame.state.has_position_ms + ? 0 + : playbackState->innerFrame.state.position_ms + + ctx->timeProvider->getSyncedTimestamp() - + playbackState->innerFrame.state.position_measured_at; - if (currentTracksIndex > 0 && position < 3000) { - queueNextTrack(-1); + if (currentTracksIndex > 0 && position < 3000) { + queueNextTrack(-1); - if (preloadedTracks.size() > MAX_TRACKS_PRELOAD) { - preloadedTracks.pop_back(); - } - - currentTracksIndex--; - } else { - queueNextTrack(0); + if (preloadedTracks.size() > MAX_TRACKS_PRELOAD) { + preloadedTracks.pop_back(); } + + currentTracksIndex--; } else { - if (currentTracks.size() > currentTracksIndex + 1) { - preloadedTracks.pop_front(); - - if (!queueNextTrack(preloadedTracks.size() + 1)) { - CSPOT_LOG(info, "Failed to queue next track"); - } - - currentTracksIndex++; - } else { - skipped = false; - } + queueNextTrack(0); } + } else { + if (currentTracks.size() > currentTracksIndex + 1) { + preloadedTracks.pop_front(); - if (skipped) { - // Update frame data - playbackState->innerFrame.state.playing_track_index = currentTracksIndex; + if (!queueNextTrack(preloadedTracks.size() + 1)) { + CSPOT_LOG(info, "Failed to queue next track"); + } - if (expectNotify) { - // Reset position to zero - notifyPending = true; - } + currentTracksIndex++; + } else { + skipped = false; } + } - return skipped; + if (skipped) { + // Update frame data + playbackState->innerFrame.state.playing_track_index = currentTracksIndex; + + if (expectNotify) { + // Reset position to zero + notifyPending = true; + } + } + + return skipped; } bool TrackQueue::hasTracks() {