Cleaned up new IPlaybackRemote and IPlaybackService interfaces. These will

be used for creating plugins that can interact with playback.
This commit is contained in:
casey langen 2016-11-18 00:39:12 -08:00
parent 7bfb7657f6
commit f97f6ca86b
4 changed files with 79 additions and 7 deletions

View File

@ -38,7 +38,7 @@
#include "IPlaybackService.h"
#include "ITrack.h"
namespace musik { namespace core {
namespace musik { namespace core { namespace sdk {
class IPlaybackRemote {
public:
@ -47,5 +47,5 @@ namespace musik { namespace core {
virtual void OnTrackChanged(ITrack* track) = 0;
};
} }
} } }

View File

@ -36,7 +36,7 @@
#include "config.h"
namespace musik { namespace core {
namespace musik { namespace core { namespace sdk {
class IPlaybackService {
public:
@ -53,13 +53,18 @@ namespace musik { namespace core {
virtual RepeatMode GetRepeatMode() = 0;
virtual void SetRepeatMode(RepeatMode mode) = 0;
virtual void ToggleRepeatMode() = 0;
virtual bool IsShuffled() = 0;
virtual void ToggleShuffle() = 0;
virtual void PauseOrResume() = 0;
virtual double GetVolume() = 0;
virtual void SetVolume(double volume) = 0;
virtual size_t GetIndex() = 0;
virtual size_t Count() = 0;
};
} }
} } }

View File

@ -35,11 +35,14 @@
#include <stdafx.h>
#include "PlaybackService.h"
#include <app/util/Playback.h>
#include <cursespp/MessageQueue.h>
#include <cursespp/Message.h>
#include <core/audio/ITransport.h>
#include <core/library/LocalLibraryConstants.h>
#include <core/plugin/PluginFactory.h>
using musik::core::TrackPtr;
using musik::core::LibraryPtr;
@ -49,6 +52,8 @@ using cursespp::IMessageTarget;
using cursespp::IMessage;
using namespace musik::core::library::constants;
using namespace musik::core;
using namespace musik::core::sdk;
using namespace musik::box;
#define NO_POSITION (size_t) -1
@ -93,10 +98,29 @@ PlaybackService::PlaybackService(LibraryPtr library, ITransport& transport)
transport.PlaybackEvent.connect(this, &PlaybackService::OnPlaybackEvent);
this->index = NO_POSITION;
this->nextIndex = NO_POSITION;
this->InitRemotes();
}
PlaybackService::~PlaybackService() {
this->Stop();
this->ResetRemotes();
}
void PlaybackService::InitRemotes() {
typedef PluginFactory::DestroyDeleter<IPlaybackRemote> Deleter;
this->remotes = PluginFactory::Instance()
.QueryInterface<IPlaybackRemote, Deleter>("GetPlaybackRemote");
for (auto it = remotes.begin(); it != remotes.end(); it++) {
(*it)->SetPlaybackService(this);
}
}
void PlaybackService::ResetRemotes() {
for (auto it = remotes.begin(); it != remotes.end(); it++) {
(*it)->SetPlaybackService(nullptr);
}
}
void PlaybackService::PrepareNextTrack() {
@ -196,7 +220,7 @@ void PlaybackService::ProcessMessage(IMessage &message) {
track = this->playlist.Get(this->index);
}
this->TrackChanged(this->index, track);
this->OnTrackChanged(this->index, track);
}
this->PrepareNextTrack();
@ -206,7 +230,7 @@ void PlaybackService::ProcessMessage(IMessage &message) {
int64 eventType = message.UserData1();
if (eventType == ITransport::PlaybackStopped) {
this->TrackChanged(NO_POSITION, TrackPtr());
this->OnTrackChanged(NO_POSITION, TrackPtr());
}
}
else if (message.Type() == MESSAGE_PREPARE_NEXT_TRACK) {
@ -216,6 +240,16 @@ void PlaybackService::ProcessMessage(IMessage &message) {
}
}
void PlaybackService::OnTrackChanged(size_t pos, TrackPtr track) {
this->TrackChanged(this->index, track);
if (track) {
for (auto it = remotes.begin(); it != remotes.end(); it++) {
(*it)->OnTrackChanged(track.get());
}
}
}
bool PlaybackService::Next() {
if (transport.GetPlaybackState() == ITransport::PlaybackStopped) {
return false;
@ -267,6 +301,10 @@ size_t PlaybackService::Count() {
return this->playlist.Count();
}
void PlaybackService::ToggleRepeatMode() {
playback::ToggleRepeatMode(*this);
}
void PlaybackService::Play(TrackList& tracks, size_t index) {
/* do the copy outside of the critical section, then swap. */
TrackList temp(this->library);
@ -298,6 +336,25 @@ size_t PlaybackService::GetIndex() {
return this->index;
}
double PlaybackService::GetVolume() {
return transport.Volume();
}
void PlaybackService::PauseOrResume() {
if (transport.GetPlaybackState() == ITransport::PlaybackStopped) {
if (this->Count()) {
this->Play(0);
}
}
else {
playback::PauseOrResume(this->transport);
}
}
void PlaybackService::SetVolume(double vol) {
transport.SetVolume(vol);
}
TrackPtr PlaybackService::GetTrackAtIndex(size_t index) {
boost::recursive_mutex::scoped_lock lock(this->playlistMutex);
return this->playlist.Get(index);

View File

@ -41,6 +41,7 @@
#include <cursespp/IMessageTarget.h>
#include <core/sdk/IPlaybackService.h>
#include <core/sdk/IPlaybackRemote.h>
#include <core/library/track/Track.h>
#include <core/library/ILibrary.h>
#include <core/audio/ITransport.h>
@ -50,7 +51,7 @@
namespace musik {
namespace box {
class PlaybackService :
public musik::core::IPlaybackService,
public musik::core::sdk::IPlaybackService,
public cursespp::IMessageTarget,
public sigslot::has_slots<>
{
@ -75,10 +76,14 @@ namespace musik {
virtual void Stop() { transport.Stop(); }
virtual RepeatMode GetRepeatMode() { return this->repeatMode; }
virtual void SetRepeatMode(RepeatMode mode);
virtual void ToggleRepeatMode();
virtual bool IsShuffled();
virtual void ToggleShuffle();
virtual size_t GetIndex();
virtual size_t Count();
virtual double GetVolume();
virtual void SetVolume(double vol);
virtual void PauseOrResume();
/* app-specific implementation */
musik::core::audio::ITransport& GetTransport() { return this->transport; }
@ -89,12 +94,17 @@ namespace musik {
private:
void OnStreamEvent(int eventType, std::string uri);
void OnPlaybackEvent(int eventType);
void OnTrackChanged(size_t pos, musik::core::TrackPtr track);
void PrepareNextTrack();
void InitRemotes();
void ResetRemotes();
TrackList playlist;
TrackList unshuffled;
boost::recursive_mutex playlistMutex;
std::vector<std::shared_ptr<musik::core::sdk::IPlaybackRemote > > remotes;
musik::core::LibraryPtr library;
musik::core::audio::ITransport& transport;
size_t index, nextIndex;