From e85733fc32b0238b209b4966f1dabebe06853145 Mon Sep 17 00:00:00 2001 From: Philippe G Date: Sun, 26 Dec 2021 15:48:25 -0800 Subject: [PATCH] cspot duration --- .../spotify/cspot/protocol/metadata.proto | 2 +- .../spotify/cspot/src/AudioChunkManager.cpp | 2 +- components/spotify/cspot/src/PbReader.cpp | 1 + components/spotify/cspot/src/Protobuf.cpp | 4 ++ .../spotify/cspot/src/SpircController.cpp | 50 +++++++++---------- components/spotify/cspot/src/SpotifyTrack.cpp | 1 + 6 files changed, 33 insertions(+), 27 deletions(-) diff --git a/components/spotify/cspot/protocol/metadata.proto b/components/spotify/cspot/protocol/metadata.proto index 9bb2d02d..ebe382ab 100644 --- a/components/spotify/cspot/protocol/metadata.proto +++ b/components/spotify/cspot/protocol/metadata.proto @@ -32,9 +32,9 @@ message Artist { message Track { optional bytes gid = 1; optional string name = 2; + optional sint32 duration = 0x7; optional Album album = 0x3; repeated Artist artist = 0x4; - optional sint32 duration = 0x7; repeated Restriction restriction = 0xb; repeated AudioFile file = 0xc; repeated Track alternative = 0xd; diff --git a/components/spotify/cspot/src/AudioChunkManager.cpp b/components/spotify/cspot/src/AudioChunkManager.cpp index 733199d4..f2cdcf8e 100644 --- a/components/spotify/cspot/src/AudioChunkManager.cpp +++ b/components/spotify/cspot/src/AudioChunkManager.cpp @@ -46,7 +46,7 @@ void AudioChunkManager::close() { void AudioChunkManager::runTask() { std::scoped_lock lock(this->runningMutex); - this->isRunning = true; + this->isRunning = true; while (isRunning) { std::pair, bool> audioPair; if (this->audioChunkDataQueue.wtpop(audioPair, 100)) { diff --git a/components/spotify/cspot/src/PbReader.cpp b/components/spotify/cspot/src/PbReader.cpp index 1a2f10ca..e216e870 100644 --- a/components/spotify/cspot/src/PbReader.cpp +++ b/components/spotify/cspot/src/PbReader.cpp @@ -36,6 +36,7 @@ template int64_t PbReader::decodeFixed(); template uint32_t PbReader::decodeVarInt(); template int64_t PbReader::decodeVarInt(); +template int32_t PbReader::decodeVarInt(); template bool PbReader::decodeVarInt(); void PbReader::resetMaxPosition() diff --git a/components/spotify/cspot/src/Protobuf.cpp b/components/spotify/cspot/src/Protobuf.cpp index cf6f90a9..a725e797 100644 --- a/components/spotify/cspot/src/Protobuf.cpp +++ b/components/spotify/cspot/src/Protobuf.cpp @@ -91,6 +91,10 @@ void decodeField(std::shared_ptr reader, AnyRef any) { *any.as() = reader->decodeVarInt(); } + else if (any.is()) + { + *any.as() = reader->decodeVarInt(); + } else { reader->skip(); diff --git a/components/spotify/cspot/src/SpircController.cpp b/components/spotify/cspot/src/SpircController.cpp index e7978fbc..06c53c5b 100644 --- a/components/spotify/cspot/src/SpircController.cpp +++ b/components/spotify/cspot/src/SpircController.cpp @@ -38,7 +38,7 @@ void SpircController::subscribe() { } void SpircController::setPause(bool isPaused, bool notifyPlayer) { - sendEvent(CSpotEventType::PLAY_PAUSE, isPaused); + sendEvent(CSpotEventType::PLAY_PAUSE, isPaused); if (isPaused) { CSPOT_LOG(debug, "External pause command"); if (notifyPlayer) player->pause(); @@ -55,29 +55,29 @@ void SpircController::disconnect(void) { player->cancelCurrentTrack(); state->setActive(false); notify(); - // Send the event at the end at it might be a last gasp - sendEvent(CSpotEventType::DISC); + // Send the event at the end at it might be a last gasp + sendEvent(CSpotEventType::DISC); } void SpircController::playToggle() { - if (state->innerFrame.state->status.value() == PlayStatus::kPlayStatusPause) { - setPause(false); - } else { - setPause(true); - } + if (state->innerFrame.state->status.value() == PlayStatus::kPlayStatusPause) { + setPause(false); + } else { + setPause(true); + } } void SpircController::adjustVolume(int by) { - if (state->innerFrame.device_state->volume.has_value()) { - int volume = state->innerFrame.device_state->volume.value() + by; - if (volume < 0) volume = 0; - else if (volume > MAX_VOLUME) volume = MAX_VOLUME; - setVolume(volume); - } + if (state->innerFrame.device_state->volume.has_value()) { + int volume = state->innerFrame.device_state->volume.value() + by; + if (volume < 0) volume = 0; + else if (volume > MAX_VOLUME) volume = MAX_VOLUME; + setVolume(volume); + } } void SpircController::setVolume(int volume) { - setRemoteVolume(volume); + setRemoteVolume(volume); player->setVolume(volume); configMan->save(); } @@ -111,21 +111,21 @@ void SpircController::handleFrame(std::vector &data) { // Pause the playback if another player took control if (state->isActive() && state->remoteFrame.device_state->is_active.value()) { - disconnect(); + disconnect(); } break; } case MessageType::kMessageTypeSeek: { CSPOT_LOG(debug, "Seek command"); - sendEvent(CSpotEventType::SEEK, (int) state->remoteFrame.position.value()); + sendEvent(CSpotEventType::SEEK, (int) state->remoteFrame.position.value()); state->updatePositionMs(state->remoteFrame.position.value()); this->player->seekMs(state->remoteFrame.position.value()); notify(); break; } case MessageType::kMessageTypeVolume: - sendEvent(CSpotEventType::VOLUME, (int) state->remoteFrame.volume.value()); - setVolume(state->remoteFrame.volume.value()); + sendEvent(CSpotEventType::VOLUME, (int) state->remoteFrame.volume.value()); + setVolume(state->remoteFrame.volume.value()); break; case MessageType::kMessageTypePause: setPause(true); @@ -134,11 +134,11 @@ void SpircController::handleFrame(std::vector &data) { setPause(false); break; case MessageType::kMessageTypeNext: - sendEvent(CSpotEventType::NEXT); + sendEvent(CSpotEventType::NEXT); nextSong(); break; case MessageType::kMessageTypePrev: - sendEvent(CSpotEventType::PREV); + sendEvent(CSpotEventType::PREV); prevSong(); break; case MessageType::kMessageTypeLoad: { @@ -182,7 +182,7 @@ void SpircController::handleFrame(std::vector &data) { } void SpircController::loadTrack(uint32_t position_ms, bool isPaused) { - sendEvent(CSpotEventType::LOAD, (int) position_ms); + sendEvent(CSpotEventType::LOAD, (int) position_ms); state->setPlaybackState(PlaybackState::Loading); std::function loadedLambda = [=]() { // Loading finished, notify that playback started @@ -201,7 +201,7 @@ void SpircController::sendEvent(CSpotEventType eventType, std::variantsendEvent(CSpotEventType::TRACK_INFO, info); + this->sendEvent(CSpotEventType::TRACK_INFO, info); }); } diff --git a/components/spotify/cspot/src/SpotifyTrack.cpp b/components/spotify/cspot/src/SpotifyTrack.cpp index 42b9e32d..1a3ea264 100644 --- a/components/spotify/cspot/src/SpotifyTrack.cpp +++ b/components/spotify/cspot/src/SpotifyTrack.cpp @@ -74,6 +74,7 @@ void SpotifyTrack::trackInformationCallback(std::unique_ptr res trackInfo = decodePb(response->parts[0]); CSPOT_LOG(info, "Track name: %s", trackInfo.name.value().c_str()); + CSPOT_LOG(info, "Track duration: %d", trackInfo.duration.value()); CSPOT_LOG(debug, "trackInfo.restriction.size() = %d", trackInfo.restriction.size()); int altIndex = 0;