From 676acbdbc2008b0d4d651549e212b0632b6c58b0 Mon Sep 17 00:00:00 2001 From: Philippe G Date: Sat, 25 Dec 2021 23:05:53 -0800 Subject: [PATCH] fixed cspot crash when switching with LMS --- components/spotify/cspot/bell/include/Queue.h | 2 ++ components/spotify/cspot/include/MercuryManager.h | 1 + components/spotify/cspot/src/AudioChunkManager.cpp | 2 +- components/spotify/cspot/src/MercuryManager.cpp | 9 +++++++-- components/spotify/cspot/src/SpircController.cpp | 3 ++- 5 files changed, 13 insertions(+), 4 deletions(-) diff --git a/components/spotify/cspot/bell/include/Queue.h b/components/spotify/cspot/bell/include/Queue.h index c9ebc573..545a74fe 100644 --- a/components/spotify/cspot/bell/include/Queue.h +++ b/components/spotify/cspot/bell/include/Queue.h @@ -103,6 +103,8 @@ namespace bell //delete m_queue.front(); m_queue.pop(); } + lk.unlock(); + m_cv.notify_one(); } /// Check queue in forced exit state. bool isExit() const diff --git a/components/spotify/cspot/include/MercuryManager.h b/components/spotify/cspot/include/MercuryManager.h index afe11b6a..bf6889fb 100644 --- a/components/spotify/cspot/include/MercuryManager.h +++ b/components/spotify/cspot/include/MercuryManager.h @@ -59,6 +59,7 @@ private: std::map callbacks; std::mutex reconnectionMutex; std::mutex runningMutex; + std::mutex stopMutex; std::map subscriptions; std::unique_ptr session; std::shared_ptr lastAuthBlob; diff --git a/components/spotify/cspot/src/AudioChunkManager.cpp b/components/spotify/cspot/src/AudioChunkManager.cpp index 073497b8..733199d4 100644 --- a/components/spotify/cspot/src/AudioChunkManager.cpp +++ b/components/spotify/cspot/src/AudioChunkManager.cpp @@ -45,8 +45,8 @@ void AudioChunkManager::close() { } void AudioChunkManager::runTask() { - this->isRunning = true; std::scoped_lock lock(this->runningMutex); + this->isRunning = true; while (isRunning) { std::pair, bool> audioPair; if (this->audioChunkDataQueue.wtpop(audioPair, 100)) { diff --git a/components/spotify/cspot/src/MercuryManager.cpp b/components/spotify/cspot/src/MercuryManager.cpp index ba6564d3..f637850c 100644 --- a/components/spotify/cspot/src/MercuryManager.cpp +++ b/components/spotify/cspot/src/MercuryManager.cpp @@ -29,6 +29,8 @@ MercuryManager::MercuryManager(std::unique_ptr session): bell::Task("me bool MercuryManager::timeoutHandler() { + if (!isRunning) return true; + auto currentTimestamp = timeProvider->getSyncedTimestamp(); if (this->lastRequestTimestamp != -1 && currentTimestamp - this->lastRequestTimestamp > AUDIOCHUNK_TIMEOUT_MS) @@ -161,7 +163,7 @@ void MercuryManager::runTask() } catch (const std::runtime_error& e) { - if (!isRunning) break; + if (!isRunning) break; // Reconnection required this->reconnect(); this->reconnectedCallback(); @@ -189,10 +191,11 @@ void MercuryManager::runTask() } void MercuryManager::stop() { + std::scoped_lock stop(this->stopMutex); CSPOT_LOG(debug, "Stopping mercury manager"); isRunning = false; audioChunkManager->close(); - std::scoped_lock lock(audioChunkManager->runningMutex, this->runningMutex); + std::scoped_lock lock(this->runningMutex); CSPOT_LOG(debug, "mercury stopped"); } @@ -274,6 +277,8 @@ void MercuryManager::handleQueue() { this->updateQueue(); } + + std::scoped_lock lock(this->stopMutex); } uint64_t MercuryManager::execute(MercuryType method, std::string uri, mercuryCallback& callback, mercuryCallback& subscription, mercuryParts& payload) diff --git a/components/spotify/cspot/src/SpircController.cpp b/components/spotify/cspot/src/SpircController.cpp index 9888080b..e7978fbc 100644 --- a/components/spotify/cspot/src/SpircController.cpp +++ b/components/spotify/cspot/src/SpircController.cpp @@ -55,7 +55,8 @@ void SpircController::disconnect(void) { player->cancelCurrentTrack(); state->setActive(false); notify(); - sendEvent(CSpotEventType::DISC); + // Send the event at the end at it might be a last gasp + sendEvent(CSpotEventType::DISC); } void SpircController::playToggle() {