From aa64784128fff2cafea72d156f63827b16f64d34 Mon Sep 17 00:00:00 2001 From: casey langen Date: Sun, 5 Feb 2017 21:19:41 -0800 Subject: [PATCH] Updated IPlaybackService (and impl) to allow the ability to set the play queue from plugins. --- src/core/audio/PlaybackService.cpp | 51 +++++++++++++++++++++++ src/core/audio/PlaybackService.h | 5 ++- src/core/sdk/IPlaybackService.h | 10 ++++- src/musikbox/app/window/TrackListView.cpp | 4 +- 4 files changed, 66 insertions(+), 4 deletions(-) diff --git a/src/core/audio/PlaybackService.cpp b/src/core/audio/PlaybackService.cpp index 11b25f07c..872732a58 100755 --- a/src/core/audio/PlaybackService.cpp +++ b/src/core/audio/PlaybackService.cpp @@ -433,6 +433,29 @@ void PlaybackService::Play(TrackList& tracks, size_t index) { } } +void PlaybackService::Play(musik::core::sdk::ITrackList* source, size_t index) { + if (source) { + /* see if we have a TrackList -- if we do we can optimize the copy */ + TrackList* sourceTrackList = dynamic_cast(source); + + if (sourceTrackList) { + this->Play(*sourceTrackList, index); + return; + } + + /* otherwise use slower impl to be compatible with SDK */ + { + std::unique_lock lock(this->playlistMutex); + this->CopyFrom(source); + this->unshuffled.Clear(); + } + + if (index <= source->Count()) { + this->Play(index); + } + } +} + void PlaybackService::CopyTo(TrackList& target) { std::unique_lock lock(this->playlistMutex); target.CopyFrom(this->playlist); @@ -451,6 +474,34 @@ void PlaybackService::CopyFrom(TrackList& source) { } } +void PlaybackService::CopyFrom(musik::core::sdk::ITrackList* source) { + if (source) { + /* see if we have a TrackList -- if we do we can optimize the copy */ + TrackList* sourceTrackList = dynamic_cast(source); + + if (sourceTrackList) { + this->CopyFrom(*sourceTrackList); + return; + } + + /* otherwise we gotta do it one at a time */ + std::unique_lock lock(this->playlistMutex); + + this->playlist.Clear(); + for (size_t i = 0; i < source->Count(); i++) { + this->playlist.Add(source->GetId(i)); + } + + this->index = NO_POSITION; + this->nextIndex = NO_POSITION; + + if (this->playingTrack) { + this->index = playlist.IndexOf(this->playingTrack->GetId()); + POST(this, MESSAGE_PREPARE_NEXT_TRACK, NO_POSITION, 0); + } + } +} + void PlaybackService::Play(size_t index) { std::string uri = this->UriAtIndex(index); diff --git a/src/core/audio/PlaybackService.h b/src/core/audio/PlaybackService.h index cee91228c..692636a81 100755 --- a/src/core/audio/PlaybackService.h +++ b/src/core/audio/PlaybackService.h @@ -99,8 +99,11 @@ namespace musik { namespace core { namespace audio { virtual double GetDuration(); virtual musik::core::sdk::IRetainedTrack* GetTrack(size_t index); virtual musik::core::sdk::IRetainedTrack* GetPlayingTrack(); + virtual void CopyFrom(musik::core::sdk::ITrackList* source); + virtual void Play(musik::core::sdk::ITrackList* source, size_t index); - /* app-specific implementation */ + /* app-specific implementation. similar to some SDK methods, but use + concrete data types with known optimizations */ musik::core::audio::ITransport& GetTransport() { return this->transport; } void Play(musik::core::TrackList& tracks, size_t index); void CopyTo(musik::core::TrackList& target); diff --git a/src/core/sdk/IPlaybackService.h b/src/core/sdk/IPlaybackService.h index a89b6bd07..e4c950dd4 100644 --- a/src/core/sdk/IPlaybackService.h +++ b/src/core/sdk/IPlaybackService.h @@ -36,11 +36,13 @@ #include "constants.h" #include "IRetainedTrack.h" +#include "ITrackList.h" namespace musik { namespace core { namespace sdk { class IPlaybackService { public: + /* sdk v1 */ virtual void Play(size_t index) = 0; virtual bool Next() = 0; virtual bool Previous() = 0; @@ -70,7 +72,13 @@ namespace musik { namespace core { namespace sdk { virtual size_t Count() = 0; virtual IRetainedTrack* GetTrack(size_t index) = 0; - virtual IRetainedTrack* GetPlayingTrack() = 0; /* sdk v2 */ + + /* sdk v2 */ + virtual IRetainedTrack* GetPlayingTrack() = 0; + + /* sdk v3*/ + virtual void CopyFrom(ITrackList* trackList) = 0; + virtual void Play(ITrackList* source, size_t index) = 0; }; } } } diff --git a/src/musikbox/app/window/TrackListView.cpp b/src/musikbox/app/window/TrackListView.cpp index 6bb03cd23..0e3549ba7 100755 --- a/src/musikbox/app/window/TrackListView.cpp +++ b/src/musikbox/app/window/TrackListView.cpp @@ -61,9 +61,9 @@ using namespace cursespp; using namespace std::chrono; -/* if the user hasn't changed the selected index in 30 seconds +/* if the user hasn't changed the selected index in 60 seconds we assume he's not paying attention, and will automatically scroll -the view to the next track if it's invisible. */ +the view to the playing track if it's invisible. */ static const milliseconds AUTO_SCROLL_COOLDOWN = milliseconds(60000LL); static inline milliseconds now() {