Fixed a couple state management bugs in Player and CrossfadeTransport.

This commit is contained in:
casey langen 2016-12-27 10:14:11 -08:00
parent c803d2b671
commit b4b534a87b
3 changed files with 20 additions and 9 deletions

View File

@ -358,6 +358,11 @@ void CrossfadeTransport::PlayerContext::Reset(
Crossfader::FadeOut,
CROSSFADE_DURATION_MS);
}
else {
/* if we're being started with a new URL and we can't fade,
drain the current instance! */
player->Destroy(url.size() ? Player::NoDrain : Player::Drain);
}
}
this->canFade = this->started = false;

View File

@ -109,16 +109,16 @@ namespace musik {
Player* Player::Create(
const std::string &url,
std::shared_ptr<IOutput> output,
FinishMode finishMode,
DestroyMode destroyMode,
EventListener *listener)
{
return new Player(url, output, finishMode, listener);
return new Player(url, output, destroyMode, listener);
}
Player::Player(
const std::string &url,
std::shared_ptr<IOutput> output,
FinishMode finishMode,
DestroyMode destroyMode,
EventListener *listener)
: state(Player::Precache)
, url(url)
@ -126,7 +126,7 @@ Player::Player(
, output(output)
, notifiedStarted(false)
, setPosition(-1)
, finishMode(finishMode)
, destroyMode(destroyMode)
, fftContext(nullptr) {
musik::debug::info(TAG, "new instance created");
@ -174,6 +174,11 @@ void Player::Destroy() {
}
}
void Player::Destroy(DestroyMode mode) {
this->destroyMode = mode;
this->Destroy();
}
void Player::Detach(EventListener* listener) {
if (listener) {
std::unique_lock<std::mutex> lock(this->listenerMutex);
@ -373,7 +378,7 @@ void musik::core::audio::playerThreadLoop(Player* player) {
}
/* buffers have been written, wait for the output to play them all */
if (player->finishMode == Player::Drain) {
if (player->destroyMode == Player::Drain) {
player->output->Drain();
}

View File

@ -52,7 +52,7 @@ namespace musik { namespace core { namespace audio {
class Player : public musik::core::sdk::IBufferProvider {
public:
enum FinishMode { Drain, NoDrain };
enum DestroyMode { Drain, NoDrain };
struct EventListener {
virtual void OnPlayerPrepared(Player *player) { }
@ -67,7 +67,7 @@ namespace musik { namespace core { namespace audio {
static Player* Create(
const std::string &url,
std::shared_ptr<musik::core::sdk::IOutput> output,
FinishMode finishMode,
DestroyMode destroyMode,
EventListener *listener);
virtual void OnBufferProcessed(musik::core::sdk::IBuffer *buffer);
@ -77,6 +77,7 @@ namespace musik { namespace core { namespace audio {
void Play();
void Destroy();
void Destroy(DestroyMode mode);
double GetPosition();
void SetPosition(double seconds);
@ -92,7 +93,7 @@ namespace musik { namespace core { namespace audio {
Player(
const std::string &url,
std::shared_ptr<musik::core::sdk::IOutput> output,
FinishMode finishMode,
DestroyMode finishMode,
EventListener *listener);
virtual ~Player();
@ -148,7 +149,7 @@ namespace musik { namespace core { namespace audio {
bool notifiedStarted;
float* spectrum;
uint64 samplesWritten;
FinishMode finishMode;
DestroyMode destroyMode;
FftContext* fftContext;
};