From a302837f1afe988cae5d9de41703080221946ea1 Mon Sep 17 00:00:00 2001 From: Casey Langen Date: Sat, 24 Oct 2020 12:07:48 -0700 Subject: [PATCH] Backed out a previous attempt to fix multiple streams trying to cache the same file simultaenously. This was later fixed in a more robust way in LruCache using Player instanceIds, but the code removed in this commit was left around to rot and cause issues. --- src/musikcore/audio/CrossfadeTransport.cpp | 5 -- src/musikcore/audio/CrossfadeTransport.h | 1 - src/musikcore/audio/GaplessTransport.cpp | 60 +++++++++++----------- src/musikcore/audio/GaplessTransport.h | 4 +- src/musikcore/audio/ITransport.h | 1 - src/musikcore/audio/MasterTransport.cpp | 4 -- src/musikcore/audio/MasterTransport.h | 1 - src/musikcore/audio/PlaybackService.cpp | 12 +---- 8 files changed, 33 insertions(+), 55 deletions(-) diff --git a/src/musikcore/audio/CrossfadeTransport.cpp b/src/musikcore/audio/CrossfadeTransport.cpp index a45654139..5946a0df0 100644 --- a/src/musikcore/audio/CrossfadeTransport.cpp +++ b/src/musikcore/audio/CrossfadeTransport.cpp @@ -79,11 +79,6 @@ void CrossfadeTransport::PrepareNextTrack(const std::string& uri, Gain gain) { this->next.Reset(uri, this, gain, false); } -bool CrossfadeTransport::HasNextTrack() { - Lock lock(this->stateMutex); - return !!this->next.player; -} - void CrossfadeTransport::Start(const std::string& uri, Gain gain, StartMode mode) { { Lock lock(this->stateMutex); diff --git a/src/musikcore/audio/CrossfadeTransport.h b/src/musikcore/audio/CrossfadeTransport.h index 3da729f4f..36fb93c5a 100644 --- a/src/musikcore/audio/CrossfadeTransport.h +++ b/src/musikcore/audio/CrossfadeTransport.h @@ -62,7 +62,6 @@ namespace musik { namespace core { namespace audio { virtual void Start(const std::string& uri, Gain gain, StartMode mode); virtual void PrepareNextTrack(const std::string& uri, Gain gain); - virtual bool HasNextTrack(); virtual std::string Uri(); diff --git a/src/musikcore/audio/GaplessTransport.cpp b/src/musikcore/audio/GaplessTransport.cpp index 8647ddddb..dd1c40061 100644 --- a/src/musikcore/audio/GaplessTransport.cpp +++ b/src/musikcore/audio/GaplessTransport.cpp @@ -45,22 +45,6 @@ using namespace musik::core::sdk; static std::string TAG = "GaplessTransport"; -#define RESET_NEXT_PLAYER(instance) \ - if (instance->nextPlayer) { \ - instance->nextPlayer->Detach(instance); \ - instance->nextPlayer->Destroy(); \ - this->RaiseStreamEvent(StreamDestroyed, instance->nextPlayer); \ - instance->nextPlayer = nullptr; \ - } - -#define RESET_ACTIVE_PLAYER(instance) \ - if (instance->activePlayer) { \ - instance->activePlayer->Detach(instance); \ - instance->activePlayer->Destroy(); \ - this->RaiseStreamEvent(StreamDestroyed, instance->activePlayer); \ - instance->activePlayer = nullptr; \ - } - GaplessTransport::GaplessTransport() : volume(1.0) , playbackState(PlaybackStopped) @@ -73,8 +57,8 @@ GaplessTransport::GaplessTransport() GaplessTransport::~GaplessTransport() { LockT lock(this->stateMutex); - RESET_NEXT_PLAYER(this); - RESET_ACTIVE_PLAYER(this); + this->ResetNextPlayer(); + this->ResetActivePlayer(); } PlaybackState GaplessTransport::GetPlaybackState() { @@ -92,7 +76,7 @@ void GaplessTransport::PrepareNextTrack(const std::string& uri, Gain gain) { { LockT lock(this->stateMutex); - RESET_NEXT_PLAYER(this); + this->ResetNextPlayer(); if (uri.size()) { this->nextPlayer = Player::Create(uri, this->output, Player::NoDrain, this, gain); @@ -105,10 +89,6 @@ void GaplessTransport::PrepareNextTrack(const std::string& uri, Gain gain) { } } -bool GaplessTransport::HasNextTrack() { - return !!this->nextPlayer; -} - void GaplessTransport::Start(const std::string& uri, Gain gain, StartMode mode) { musik::debug::info(TAG, "starting track at " + uri); Player* newPlayer = Player::Create(uri, this->output, Player::NoDrain, this, gain); @@ -124,10 +104,10 @@ void GaplessTransport::StartWithPlayer(Player* newPlayer, StartMode mode) { playingNext = (newPlayer == nextPlayer); if (newPlayer != nextPlayer) { - RESET_NEXT_PLAYER(this); + this->ResetNextPlayer(); } - RESET_ACTIVE_PLAYER(this); + this->ResetActivePlayer(); this->nextPlayer = nullptr; this->activePlayer = newPlayer; @@ -182,9 +162,9 @@ void GaplessTransport::StopInternal( { LockT lock(this->stateMutex); - RESET_NEXT_PLAYER(this); + this->ResetNextPlayer(); if (this->activePlayer != exclude) { - RESET_ACTIVE_PLAYER(this); + this->ResetActivePlayer(); } } @@ -366,12 +346,12 @@ void GaplessTransport::OnPlayerOpenFailed(Player* player) { { LockT lock(this->stateMutex); if (player == this->activePlayer) { - RESET_ACTIVE_PLAYER(this); - RESET_NEXT_PLAYER(this); + this->ResetActivePlayer(); + this->ResetNextPlayer(); raiseEvents = true; } else if (player == this->nextPlayer) { - RESET_NEXT_PLAYER(this); + this->ResetNextPlayer(); } } if (raiseEvents) { @@ -384,7 +364,7 @@ void GaplessTransport::OnPlayerDestroying(Player *player) { LockT lock(this->stateMutex); if (player == this->activePlayer) { - RESET_ACTIVE_PLAYER(this); + this->ResetActivePlayer(); return; } } @@ -417,3 +397,21 @@ void GaplessTransport::RaiseStreamEvent(int type, Player* player) { this->StreamEvent(type, player->GetUrl()); } } + +void GaplessTransport::ResetNextPlayer() { + if (this->nextPlayer) { + this->nextPlayer->Detach(this); + this->nextPlayer->Destroy(); + this->RaiseStreamEvent(StreamDestroyed, this->nextPlayer); + this->nextPlayer = nullptr; + } +} + +void GaplessTransport::ResetActivePlayer() { + if (this->activePlayer) { + this->activePlayer->Detach(this); + this->activePlayer->Destroy(); + this->RaiseStreamEvent(StreamDestroyed, this->activePlayer); + this->activePlayer = nullptr; + } +} diff --git a/src/musikcore/audio/GaplessTransport.h b/src/musikcore/audio/GaplessTransport.h index 227104f13..a4b5a54f5 100644 --- a/src/musikcore/audio/GaplessTransport.h +++ b/src/musikcore/audio/GaplessTransport.h @@ -55,7 +55,6 @@ namespace musik { namespace core { namespace audio { virtual void Start(const std::string& uri, Gain gain, StartMode mode); virtual void PrepareNextTrack(const std::string& uri, Gain gain); - virtual bool HasNextTrack(); virtual std::string Uri(); @@ -101,6 +100,9 @@ namespace musik { namespace core { namespace audio { virtual void OnPlayerOpenFailed(Player* player); virtual void OnPlayerDestroying(Player* player); + void ResetActivePlayer(); + void ResetNextPlayer(); + musik::core::sdk::PlaybackState playbackState; musik::core::sdk::StreamState activePlayerState; std::recursive_mutex stateMutex; diff --git a/src/musikcore/audio/ITransport.h b/src/musikcore/audio/ITransport.h index 1159344de..c4b2adc34 100755 --- a/src/musikcore/audio/ITransport.h +++ b/src/musikcore/audio/ITransport.h @@ -58,7 +58,6 @@ namespace musik { namespace core { namespace audio { virtual void Start(const std::string& uri, Gain gain, StartMode mode) = 0; virtual void PrepareNextTrack(const std::string& uri, Gain gain) = 0; - virtual bool HasNextTrack() = 0; virtual std::string Uri() = 0; diff --git a/src/musikcore/audio/MasterTransport.cpp b/src/musikcore/audio/MasterTransport.cpp index 426f637aa..06bd60791 100644 --- a/src/musikcore/audio/MasterTransport.cpp +++ b/src/musikcore/audio/MasterTransport.cpp @@ -107,10 +107,6 @@ void MasterTransport::PrepareNextTrack(const std::string& uri, Gain gain) { this->transport->PrepareNextTrack(uri, gain); } -bool MasterTransport::HasNextTrack() { - return this->transport->HasNextTrack(); -} - void MasterTransport::Start(const std::string& uri, Gain gain, StartMode type) { this->transport->Start(uri, gain, type); } diff --git a/src/musikcore/audio/MasterTransport.h b/src/musikcore/audio/MasterTransport.h index 9f458c5b5..d34ad65b1 100644 --- a/src/musikcore/audio/MasterTransport.h +++ b/src/musikcore/audio/MasterTransport.h @@ -51,7 +51,6 @@ namespace musik { namespace core { namespace audio { virtual void Start(const std::string& uri, Gain gain, StartMode mode); virtual void PrepareNextTrack(const std::string& uri, Gain gain); - virtual bool HasNextTrack(); virtual std::string Uri(); diff --git a/src/musikcore/audio/PlaybackService.cpp b/src/musikcore/audio/PlaybackService.cpp index c6ce5f7b3..de29e52d0 100755 --- a/src/musikcore/audio/PlaybackService.cpp +++ b/src/musikcore/audio/PlaybackService.cpp @@ -308,17 +308,7 @@ void PlaybackService::ProcessMessage(IMessage &message) { StreamMessage* streamMessage = static_cast(&message); StreamState eventType = (StreamState) streamMessage->GetEventType(); - if (eventType == StreamDestroyed) { - /* it's possible that we tried to precache a track that was already being - buffered. in this case, it's possible that operation failed. if that's the - case, let's just load the next track now. */ - if (!this->transport->HasNextTrack()) { - std::unique_lock lock(this->playlistMutex); - this->nextIndex = NO_POSITION; - this->PrepareNextTrack(); - } - } - else if (eventType == StreamBuffering || eventType == StreamBuffered || eventType == StreamPlaying) { + if (eventType == StreamBuffering || eventType == StreamBuffered || eventType == StreamPlaying) { TrackPtr track; {