mpd/src/player/Control.cxx

275 lines
5.1 KiB
C++
Raw Normal View History

/*
2015-01-01 19:48:13 +01:00
* Copyright (C) 2003-2015 The Music Player Daemon Project
* http://www.musicpd.org
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "config.h"
2015-08-15 15:55:46 +02:00
#include "Control.hxx"
2013-01-09 08:36:52 +01:00
#include "Idle.hxx"
#include "DetachedSong.hxx"
2013-11-28 11:50:54 +01:00
#include <algorithm>
#include <assert.h>
PlayerControl::PlayerControl(PlayerListener &_listener,
MultipleOutputs &_outputs,
unsigned _buffer_chunks,
unsigned _buffered_before_play)
:listener(_listener), outputs(_outputs),
buffer_chunks(_buffer_chunks),
buffered_before_play(_buffered_before_play),
command(PlayerCommand::NONE),
state(PlayerState::STOP),
error_type(PlayerError::NONE),
tagged_song(nullptr),
next_song(nullptr),
total_play_time(0),
border_pause(false)
2008-08-26 08:45:14 +02:00
{
}
PlayerControl::~PlayerControl()
{
delete next_song;
delete tagged_song;
}
void
PlayerControl::Play(DetachedSong *song)
{
2013-10-19 18:19:03 +02:00
assert(song != nullptr);
2015-11-11 17:41:48 +01:00
const ScopeLock protect(mutex);
if (state != PlayerState::STOP)
SynchronousCommand(PlayerCommand::STOP);
assert(next_song == nullptr);
EnqueueSongLocked(song);
assert(next_song == nullptr);
}
void
PlayerControl::LockCancel()
{
LockSynchronousCommand(PlayerCommand::CANCEL);
2013-10-19 18:19:03 +02:00
assert(next_song == nullptr);
}
2009-10-08 21:12:57 +02:00
void
PlayerControl::LockStop()
{
LockSynchronousCommand(PlayerCommand::CLOSE_AUDIO);
assert(next_song == nullptr);
idle_add(IDLE_PLAYER);
}
void
PlayerControl::LockUpdateAudio()
{
LockSynchronousCommand(PlayerCommand::UPDATE_AUDIO);
}
2009-10-08 21:12:57 +02:00
void
PlayerControl::Kill()
{
assert(thread.IsDefined());
LockSynchronousCommand(PlayerCommand::EXIT);
thread.Join();
idle_add(IDLE_PLAYER);
}
2009-10-08 21:12:57 +02:00
void
PlayerControl::PauseLocked()
{
if (state != PlayerState::STOP) {
SynchronousCommand(PlayerCommand::PAUSE);
idle_add(IDLE_PLAYER);
}
}
void
PlayerControl::LockPause()
{
2015-11-11 17:41:48 +01:00
const ScopeLock protect(mutex);
PauseLocked();
}
2009-10-08 21:12:57 +02:00
void
PlayerControl::LockSetPause(bool pause_flag)
{
2015-11-11 17:41:48 +01:00
const ScopeLock protect(mutex);
switch (state) {
case PlayerState::STOP:
break;
case PlayerState::PLAY:
if (pause_flag)
PauseLocked();
break;
2009-10-08 21:12:57 +02:00
case PlayerState::PAUSE:
if (!pause_flag)
PauseLocked();
break;
}
}
void
PlayerControl::LockSetBorderPause(bool _border_pause)
{
2015-11-11 17:41:48 +01:00
const ScopeLock protect(mutex);
border_pause = _border_pause;
}
player_status
PlayerControl::LockGetStatus()
{
player_status status;
2015-11-11 17:41:48 +01:00
const ScopeLock protect(mutex);
SynchronousCommand(PlayerCommand::REFRESH);
status.state = state;
if (state != PlayerState::STOP) {
status.bit_rate = bit_rate;
status.audio_format = audio_format;
status.total_time = total_time;
status.elapsed_time = elapsed_time;
}
return status;
}
2009-10-08 21:12:57 +02:00
void
PlayerControl::SetError(PlayerError type, Error &&_error)
{
assert(type != PlayerError::NONE);
assert(_error.IsDefined());
error_type = type;
error = std::move(_error);
}
void
PlayerControl::LockClearError()
{
2015-11-11 17:41:48 +01:00
const ScopeLock protect(mutex);
ClearError();
}
void
PlayerControl::LockSetTaggedSong(const DetachedSong &song)
{
2015-11-11 17:41:48 +01:00
const ScopeLock protect(mutex);
delete tagged_song;
tagged_song = new DetachedSong(song);
}
void
PlayerControl::ClearTaggedSong()
{
delete tagged_song;
tagged_song = nullptr;
}
void
PlayerControl::LockEnqueueSong(DetachedSong *song)
{
2013-10-19 18:19:03 +02:00
assert(song != nullptr);
2015-11-11 17:41:48 +01:00
const ScopeLock protect(mutex);
EnqueueSongLocked(song);
}
bool
PlayerControl::SeekLocked(DetachedSong *song, SongTime t, Error &error_r)
{
2013-10-19 18:19:03 +02:00
assert(song != nullptr);
if (next_song != nullptr)
SynchronousCommand(PlayerCommand::CANCEL);
assert(next_song == nullptr);
ClearError();
next_song = song;
seek_time = t;
SynchronousCommand(PlayerCommand::SEEK);
assert(next_song == nullptr);
if (error_type != PlayerError::NONE) {
assert(error.IsDefined());
error_r.Set(error);
return false;
}
assert(!error.IsDefined());
return true;
}
bool
PlayerControl::LockSeek(DetachedSong *song, SongTime t, Error &error_r)
{
assert(song != nullptr);
2015-11-11 17:41:48 +01:00
{
const ScopeLock protect(mutex);
if (!SeekLocked(song, t, error_r))
return false;
2015-11-11 17:41:48 +01:00
}
idle_add(IDLE_PLAYER);
return true;
}
2009-10-08 21:12:57 +02:00
void
PlayerControl::SetCrossFade(float _cross_fade_seconds)
{
if (_cross_fade_seconds < 0)
_cross_fade_seconds = 0;
cross_fade.duration = _cross_fade_seconds;
idle_add(IDLE_OPTIONS);
}
void
PlayerControl::SetMixRampDb(float _mixramp_db)
{
cross_fade.mixramp_db = _mixramp_db;
idle_add(IDLE_OPTIONS);
}
void
PlayerControl::SetMixRampDelay(float _mixramp_delay_seconds)
{
cross_fade.mixramp_delay = _mixramp_delay_seconds;
idle_add(IDLE_OPTIONS);
}