mpd/src/player/Control.cxx

305 lines
6.3 KiB
C++
Raw Normal View History

/*
2017-01-03 20:48:59 +01:00
* Copyright 2003-2017 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"
#include "Outputs.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,
PlayerOutputs &_outputs,
unsigned _buffer_chunks,
unsigned _buffered_before_play,
AudioFormat _configured_audio_format,
2017-11-26 12:02:08 +01:00
const ReplayGainConfig &_replay_gain_config) noexcept
:listener(_listener), outputs(_outputs),
buffer_chunks(_buffer_chunks),
buffered_before_play(_buffered_before_play),
configured_audio_format(_configured_audio_format),
2017-02-10 22:41:11 +01:00
thread(BIND_THIS_METHOD(RunThread)),
replay_gain_config(_replay_gain_config)
2008-08-26 08:45:14 +02:00
{
}
2017-11-26 12:02:08 +01:00
PlayerControl::~PlayerControl() noexcept
{
assert(!occupied);
}
bool
2017-11-26 12:02:08 +01:00
PlayerControl::WaitOutputConsumed(unsigned threshold) noexcept
{
bool result = outputs.CheckPipe() < threshold;
if (!result && command == PlayerCommand::NONE) {
Wait();
result = outputs.CheckPipe() < threshold;
}
return result;
}
void
PlayerControl::Play(std::unique_ptr<DetachedSong> song)
{
2013-10-19 18:19:03 +02:00
assert(song != nullptr);
const std::lock_guard<Mutex> protect(mutex);
SeekLocked(std::move(song), SongTime::zero());
if (state == PlayerState::PAUSE)
/* if the player was paused previously, we need to
unpause it */
PauseLocked();
}
void
2017-11-26 12:02:08 +01:00
PlayerControl::LockCancel() noexcept
{
LockSynchronousCommand(PlayerCommand::CANCEL);
2013-10-19 18:19:03 +02:00
assert(next_song == nullptr);
}
2009-10-08 21:12:57 +02:00
void
2017-11-26 12:02:08 +01:00
PlayerControl::LockStop() noexcept
{
LockSynchronousCommand(PlayerCommand::CLOSE_AUDIO);
assert(next_song == nullptr);
idle_add(IDLE_PLAYER);
}
void
2017-11-26 12:02:08 +01:00
PlayerControl::LockUpdateAudio() noexcept
{
LockSynchronousCommand(PlayerCommand::UPDATE_AUDIO);
}
2009-10-08 21:12:57 +02:00
void
2017-11-26 12:02:08 +01:00
PlayerControl::Kill() noexcept
{
assert(thread.IsDefined());
LockSynchronousCommand(PlayerCommand::EXIT);
thread.Join();
idle_add(IDLE_PLAYER);
}
2009-10-08 21:12:57 +02:00
void
2017-11-26 12:02:08 +01:00
PlayerControl::PauseLocked() noexcept
{
if (state != PlayerState::STOP) {
SynchronousCommand(PlayerCommand::PAUSE);
idle_add(IDLE_PLAYER);
}
}
void
2017-11-26 12:02:08 +01:00
PlayerControl::LockPause() noexcept
{
const std::lock_guard<Mutex> protect(mutex);
PauseLocked();
}
2009-10-08 21:12:57 +02:00
void
2017-11-26 12:02:08 +01:00
PlayerControl::LockSetPause(bool pause_flag) noexcept
{
const std::lock_guard<Mutex> 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
2017-11-26 12:02:08 +01:00
PlayerControl::LockSetBorderPause(bool _border_pause) noexcept
{
const std::lock_guard<Mutex> protect(mutex);
border_pause = _border_pause;
}
player_status
PlayerControl::LockGetStatus() noexcept
{
player_status status;
const std::lock_guard<Mutex> protect(mutex);
if (!occupied)
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
2017-11-26 12:02:08 +01:00
PlayerControl::SetError(PlayerError type, std::exception_ptr &&_error) noexcept
{
assert(type != PlayerError::NONE);
assert(_error);
error_type = type;
error = std::move(_error);
}
void
2017-11-26 12:02:08 +01:00
PlayerControl::LockClearError() noexcept
{
const std::lock_guard<Mutex> protect(mutex);
ClearError();
}
void
2017-11-26 12:02:08 +01:00
PlayerControl::LockSetTaggedSong(const DetachedSong &song) noexcept
{
const std::lock_guard<Mutex> protect(mutex);
tagged_song.reset();
tagged_song = std::make_unique<DetachedSong>(song);
}
void
2017-11-26 12:02:08 +01:00
PlayerControl::ClearTaggedSong() noexcept
{
tagged_song.reset();
}
std::unique_ptr<DetachedSong>
PlayerControl::ReadTaggedSong() noexcept
{
return std::exchange(tagged_song, nullptr);
}
std::unique_ptr<DetachedSong>
PlayerControl::LockReadTaggedSong() noexcept
{
const std::lock_guard<Mutex> protect(mutex);
return ReadTaggedSong();
}
void
PlayerControl::LockEnqueueSong(std::unique_ptr<DetachedSong> song) noexcept
{
2013-10-19 18:19:03 +02:00
assert(song != nullptr);
const std::lock_guard<Mutex> protect(mutex);
EnqueueSongLocked(std::move(song));
}
void
PlayerControl::EnqueueSongLocked(std::unique_ptr<DetachedSong> song) noexcept
{
assert(song != nullptr);
assert(next_song == nullptr);
next_song = std::move(song);
seek_time = SongTime::zero();
SynchronousCommand(PlayerCommand::QUEUE);
}
void
PlayerControl::SeekLocked(std::unique_ptr<DetachedSong> song, SongTime t)
{
2013-10-19 18:19:03 +02:00
assert(song != nullptr);
2015-11-13 16:01:53 +01:00
/* to issue the SEEK command below, we need to clear the
"next_song" attribute with the CANCEL command */
/* optimization TODO: if the decoder happens to decode that
song already, don't cancel that */
if (next_song != nullptr)
SynchronousCommand(PlayerCommand::CANCEL);
assert(next_song == nullptr);
ClearError();
next_song = std::move(song);
seek_time = t;
SynchronousCommand(PlayerCommand::SEEK);
assert(next_song == nullptr);
if (error_type != PlayerError::NONE) {
assert(error);
std::rethrow_exception(error);
}
assert(!error);
}
void
PlayerControl::LockSeek(std::unique_ptr<DetachedSong> song, SongTime t)
{
assert(song != nullptr);
2015-11-11 17:41:48 +01:00
{
const std::lock_guard<Mutex> protect(mutex);
SeekLocked(std::move(song), t);
2015-11-11 17:41:48 +01:00
}
idle_add(IDLE_PLAYER);
}
2009-10-08 21:12:57 +02:00
void
2017-11-26 12:02:08 +01:00
PlayerControl::SetCrossFade(float _cross_fade_seconds) noexcept
{
if (_cross_fade_seconds < 0)
_cross_fade_seconds = 0;
cross_fade.duration = _cross_fade_seconds;
idle_add(IDLE_OPTIONS);
}
void
2017-11-26 12:02:08 +01:00
PlayerControl::SetMixRampDb(float _mixramp_db) noexcept
{
cross_fade.mixramp_db = _mixramp_db;
idle_add(IDLE_OPTIONS);
}
void
2017-11-26 12:02:08 +01:00
PlayerControl::SetMixRampDelay(float _mixramp_delay_seconds) noexcept
{
cross_fade.mixramp_delay = _mixramp_delay_seconds;
idle_add(IDLE_OPTIONS);
}