2009-03-13 18:43:16 +01:00
|
|
|
/*
|
2017-01-03 20:48:59 +01:00
|
|
|
* Copyright 2003-2017 The Music Player Daemon Project
|
2009-03-13 18:43:16 +01:00
|
|
|
* http://www.musicpd.org
|
2008-08-26 08:41:05 +02:00
|
|
|
*
|
|
|
|
* 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.
|
2009-03-13 18:43:16 +01:00
|
|
|
*
|
|
|
|
* 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.
|
2008-08-26 08:41:05 +02:00
|
|
|
*/
|
|
|
|
|
2009-11-12 09:12:38 +01:00
|
|
|
#include "config.h"
|
2015-08-15 15:55:46 +02:00
|
|
|
#include "Control.hxx"
|
2017-12-29 16:23:19 +01:00
|
|
|
#include "Outputs.hxx"
|
2013-01-09 08:36:52 +01:00
|
|
|
#include "Idle.hxx"
|
2014-01-07 21:39:47 +01:00
|
|
|
#include "DetachedSong.hxx"
|
2008-08-26 08:41:05 +02:00
|
|
|
|
2013-11-28 11:50:54 +01:00
|
|
|
#include <algorithm>
|
2013-01-07 08:53:08 +01:00
|
|
|
|
2008-12-29 17:28:32 +01:00
|
|
|
#include <assert.h>
|
2010-06-19 13:17:53 +02:00
|
|
|
|
2014-02-21 08:55:52 +01:00
|
|
|
PlayerControl::PlayerControl(PlayerListener &_listener,
|
2017-12-29 16:23:19 +01:00
|
|
|
PlayerOutputs &_outputs,
|
2014-01-27 08:20:25 +01:00
|
|
|
unsigned _buffer_chunks,
|
2016-12-03 13:07:33 +01:00
|
|
|
unsigned _buffered_before_play,
|
2016-12-03 14:12:08 +01:00
|
|
|
AudioFormat _configured_audio_format,
|
2017-11-26 12:02:08 +01:00
|
|
|
const ReplayGainConfig &_replay_gain_config) noexcept
|
2014-02-21 08:55:52 +01:00
|
|
|
:listener(_listener), outputs(_outputs),
|
2014-01-27 08:20:25 +01:00
|
|
|
buffer_chunks(_buffer_chunks),
|
2016-12-03 13:07:33 +01:00
|
|
|
buffered_before_play(_buffered_before_play),
|
2016-12-03 14:12:08 +01:00
|
|
|
configured_audio_format(_configured_audio_format),
|
2017-02-10 22:41:11 +01:00
|
|
|
thread(BIND_THIS_METHOD(RunThread)),
|
2016-12-03 13:07:33 +01:00
|
|
|
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
|
2008-09-24 07:14:11 +02:00
|
|
|
{
|
2017-12-20 20:27:19 +01:00
|
|
|
assert(!occupied);
|
2009-10-31 17:02:12 +01:00
|
|
|
}
|
|
|
|
|
2016-12-14 12:54:19 +01:00
|
|
|
bool
|
2017-11-26 12:02:08 +01:00
|
|
|
PlayerControl::WaitOutputConsumed(unsigned threshold) noexcept
|
2016-12-14 12:54:19 +01:00
|
|
|
{
|
2017-12-29 16:23:19 +01:00
|
|
|
bool result = outputs.CheckPipe() < threshold;
|
2016-12-14 12:54:19 +01:00
|
|
|
if (!result && command == PlayerCommand::NONE) {
|
|
|
|
Wait();
|
2017-12-29 16:23:19 +01:00
|
|
|
result = outputs.CheckPipe() < threshold;
|
2016-12-14 12:54:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2016-09-08 10:29:49 +02:00
|
|
|
void
|
2017-11-26 11:46:14 +01:00
|
|
|
PlayerControl::Play(std::unique_ptr<DetachedSong> song)
|
2008-08-26 08:44:38 +02:00
|
|
|
{
|
2013-10-19 18:19:03 +02:00
|
|
|
assert(song != nullptr);
|
2008-08-26 08:44:38 +02:00
|
|
|
|
2017-01-03 07:11:57 +01:00
|
|
|
const std::lock_guard<Mutex> protect(mutex);
|
2017-11-26 11:46:14 +01:00
|
|
|
SeekLocked(std::move(song), SongTime::zero());
|
2016-02-26 17:33:21 +01:00
|
|
|
|
2016-09-08 10:29:49 +02:00
|
|
|
if (state == PlayerState::PAUSE)
|
2016-02-26 17:33:21 +01:00
|
|
|
/* if the player was paused previously, we need to
|
|
|
|
unpause it */
|
|
|
|
PauseLocked();
|
2008-08-26 08:44:38 +02:00
|
|
|
}
|
|
|
|
|
2009-11-03 21:08:48 +01:00
|
|
|
void
|
2017-11-26 12:02:08 +01:00
|
|
|
PlayerControl::LockCancel() noexcept
|
2008-08-26 08:44:38 +02:00
|
|
|
{
|
2013-09-27 22:07:20 +02:00
|
|
|
LockSynchronousCommand(PlayerCommand::CANCEL);
|
2013-10-19 18:19:03 +02:00
|
|
|
assert(next_song == nullptr);
|
2008-10-12 00:07:54 +02:00
|
|
|
}
|
2008-08-26 08:44:38 +02:00
|
|
|
|
2009-10-08 21:12:57 +02:00
|
|
|
void
|
2017-11-26 12:02:08 +01:00
|
|
|
PlayerControl::LockStop() noexcept
|
2008-10-12 00:07:54 +02:00
|
|
|
{
|
2013-09-27 22:07:20 +02:00
|
|
|
LockSynchronousCommand(PlayerCommand::CLOSE_AUDIO);
|
2013-01-20 17:48:23 +01:00
|
|
|
assert(next_song == nullptr);
|
2008-10-14 22:38:14 +02:00
|
|
|
|
|
|
|
idle_add(IDLE_PLAYER);
|
2008-08-26 08:44:38 +02:00
|
|
|
}
|
|
|
|
|
2009-10-23 10:55:52 +02:00
|
|
|
void
|
2017-11-26 12:02:08 +01:00
|
|
|
PlayerControl::LockUpdateAudio() noexcept
|
2009-10-23 10:55:52 +02:00
|
|
|
{
|
2013-09-27 22:07:20 +02:00
|
|
|
LockSynchronousCommand(PlayerCommand::UPDATE_AUDIO);
|
2009-10-23 10:55:52 +02:00
|
|
|
}
|
|
|
|
|
2009-10-08 21:12:57 +02:00
|
|
|
void
|
2017-11-26 12:02:08 +01:00
|
|
|
PlayerControl::Kill() noexcept
|
2008-08-26 08:44:38 +02:00
|
|
|
{
|
2013-10-17 18:42:14 +02:00
|
|
|
assert(thread.IsDefined());
|
2009-01-25 13:44:33 +01:00
|
|
|
|
2013-09-27 22:07:20 +02:00
|
|
|
LockSynchronousCommand(PlayerCommand::EXIT);
|
2013-10-17 18:42:14 +02:00
|
|
|
thread.Join();
|
2008-10-14 22:38:14 +02:00
|
|
|
|
|
|
|
idle_add(IDLE_PLAYER);
|
2008-08-26 08:44:38 +02:00
|
|
|
}
|
|
|
|
|
2009-10-08 21:12:57 +02:00
|
|
|
void
|
2017-11-26 12:02:08 +01:00
|
|
|
PlayerControl::PauseLocked() noexcept
|
2010-06-19 13:17:53 +02:00
|
|
|
{
|
2013-09-27 22:07:20 +02:00
|
|
|
if (state != PlayerState::STOP) {
|
|
|
|
SynchronousCommand(PlayerCommand::PAUSE);
|
2010-06-19 13:17:53 +02:00
|
|
|
idle_add(IDLE_PLAYER);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-27 09:20:53 +02:00
|
|
|
void
|
2017-11-26 12:02:08 +01:00
|
|
|
PlayerControl::LockPause() noexcept
|
2008-08-26 08:44:38 +02:00
|
|
|
{
|
2017-01-03 07:11:57 +01:00
|
|
|
const std::lock_guard<Mutex> protect(mutex);
|
2013-09-27 09:20:53 +02:00
|
|
|
PauseLocked();
|
2008-08-26 08:44:38 +02:00
|
|
|
}
|
|
|
|
|
2009-10-08 21:12:57 +02:00
|
|
|
void
|
2017-11-26 12:02:08 +01:00
|
|
|
PlayerControl::LockSetPause(bool pause_flag) noexcept
|
2008-08-26 08:44:38 +02:00
|
|
|
{
|
2017-01-03 07:11:57 +01:00
|
|
|
const std::lock_guard<Mutex> protect(mutex);
|
2010-06-19 13:17:53 +02:00
|
|
|
|
2013-01-20 17:48:23 +01:00
|
|
|
switch (state) {
|
2013-09-27 22:07:20 +02:00
|
|
|
case PlayerState::STOP:
|
2008-08-26 08:44:38 +02:00
|
|
|
break;
|
|
|
|
|
2013-09-27 22:07:20 +02:00
|
|
|
case PlayerState::PLAY:
|
2008-08-26 08:44:38 +02:00
|
|
|
if (pause_flag)
|
2013-09-27 09:20:53 +02:00
|
|
|
PauseLocked();
|
2008-08-26 08:44:38 +02:00
|
|
|
break;
|
2009-10-08 21:12:57 +02:00
|
|
|
|
2013-09-27 22:07:20 +02:00
|
|
|
case PlayerState::PAUSE:
|
2008-08-26 08:44:38 +02:00
|
|
|
if (!pause_flag)
|
2013-09-27 09:20:53 +02:00
|
|
|
PauseLocked();
|
2008-08-26 08:44:38 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-25 08:44:31 +02:00
|
|
|
void
|
2017-11-26 12:02:08 +01:00
|
|
|
PlayerControl::LockSetBorderPause(bool _border_pause) noexcept
|
2012-08-25 08:44:31 +02:00
|
|
|
{
|
2017-01-03 07:11:57 +01:00
|
|
|
const std::lock_guard<Mutex> protect(mutex);
|
2013-01-20 17:48:23 +01:00
|
|
|
border_pause = _border_pause;
|
2012-08-25 08:44:31 +02:00
|
|
|
}
|
|
|
|
|
2013-01-20 17:48:23 +01:00
|
|
|
player_status
|
2017-05-08 14:44:49 +02:00
|
|
|
PlayerControl::LockGetStatus() noexcept
|
2008-08-26 08:44:38 +02:00
|
|
|
{
|
2013-01-20 17:48:23 +01:00
|
|
|
player_status status;
|
2009-10-08 22:09:25 +02:00
|
|
|
|
2017-01-03 07:11:57 +01:00
|
|
|
const std::lock_guard<Mutex> protect(mutex);
|
2017-12-20 20:27:19 +01:00
|
|
|
if (!occupied)
|
|
|
|
SynchronousCommand(PlayerCommand::REFRESH);
|
2008-08-26 08:44:38 +02:00
|
|
|
|
2013-01-20 17:48:23 +01:00
|
|
|
status.state = state;
|
|
|
|
|
2013-09-27 22:07:20 +02:00
|
|
|
if (state != PlayerState::STOP) {
|
2013-01-20 17:48:23 +01:00
|
|
|
status.bit_rate = bit_rate;
|
|
|
|
status.audio_format = audio_format;
|
|
|
|
status.total_time = total_time;
|
|
|
|
status.elapsed_time = elapsed_time;
|
2009-10-08 20:48:07 +02:00
|
|
|
}
|
2009-11-12 18:40:36 +01:00
|
|
|
|
2013-01-20 17:48:23 +01:00
|
|
|
return status;
|
2008-08-26 08:44:38 +02:00
|
|
|
}
|
|
|
|
|
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
|
2008-08-26 08:44:38 +02:00
|
|
|
{
|
2013-09-27 22:07:20 +02:00
|
|
|
assert(type != PlayerError::NONE);
|
2016-09-08 10:21:34 +02:00
|
|
|
assert(_error);
|
2012-08-08 22:18:08 +02:00
|
|
|
|
2013-01-20 17:48:23 +01:00
|
|
|
error_type = type;
|
2013-08-10 18:02:44 +02:00
|
|
|
error = std::move(_error);
|
2008-08-26 08:44:38 +02:00
|
|
|
}
|
|
|
|
|
2012-08-08 22:18:08 +02:00
|
|
|
void
|
2017-11-26 12:02:08 +01:00
|
|
|
PlayerControl::LockClearError() noexcept
|
2008-12-17 16:46:12 +01:00
|
|
|
{
|
2017-01-03 07:11:57 +01:00
|
|
|
const std::lock_guard<Mutex> protect(mutex);
|
2015-11-11 17:34:37 +01:00
|
|
|
ClearError();
|
2008-12-17 16:46:12 +01:00
|
|
|
}
|
|
|
|
|
2013-10-21 23:22:16 +02:00
|
|
|
void
|
2017-11-26 12:02:08 +01:00
|
|
|
PlayerControl::LockSetTaggedSong(const DetachedSong &song) noexcept
|
2013-10-21 23:22:16 +02:00
|
|
|
{
|
2017-01-03 07:11:57 +01:00
|
|
|
const std::lock_guard<Mutex> protect(mutex);
|
2017-11-26 11:46:14 +01:00
|
|
|
tagged_song.reset();
|
|
|
|
tagged_song = std::make_unique<DetachedSong>(song);
|
2013-10-21 23:22:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2017-11-26 12:02:08 +01:00
|
|
|
PlayerControl::ClearTaggedSong() noexcept
|
2013-10-21 23:22:16 +02:00
|
|
|
{
|
2017-11-26 11:46:14 +01:00
|
|
|
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();
|
2013-10-21 23:22:16 +02:00
|
|
|
}
|
|
|
|
|
2010-06-19 13:17:53 +02:00
|
|
|
void
|
2017-11-26 11:46:14 +01:00
|
|
|
PlayerControl::LockEnqueueSong(std::unique_ptr<DetachedSong> song) noexcept
|
2010-06-19 13:17:53 +02:00
|
|
|
{
|
2013-10-19 18:19:03 +02:00
|
|
|
assert(song != nullptr);
|
2010-06-19 13:17:53 +02:00
|
|
|
|
2017-01-03 07:11:57 +01:00
|
|
|
const std::lock_guard<Mutex> protect(mutex);
|
2017-11-26 11:46:14 +01:00
|
|
|
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);
|
2008-08-26 08:44:38 +02:00
|
|
|
}
|
|
|
|
|
2016-09-08 10:29:49 +02:00
|
|
|
void
|
2017-11-26 11:46:14 +01:00
|
|
|
PlayerControl::SeekLocked(std::unique_ptr<DetachedSong> song, SongTime t)
|
2008-08-26 08:44:38 +02:00
|
|
|
{
|
2013-10-19 18:19:03 +02:00
|
|
|
assert(song != nullptr);
|
2008-08-26 08:44:38 +02:00
|
|
|
|
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 */
|
2015-10-27 23:09:03 +01:00
|
|
|
if (next_song != nullptr)
|
|
|
|
SynchronousCommand(PlayerCommand::CANCEL);
|
|
|
|
|
|
|
|
assert(next_song == nullptr);
|
|
|
|
|
2015-11-11 17:31:32 +01:00
|
|
|
ClearError();
|
2017-11-26 11:46:14 +01:00
|
|
|
next_song = std::move(song);
|
2014-08-27 18:50:16 +02:00
|
|
|
seek_time = t;
|
2013-09-27 22:07:20 +02:00
|
|
|
SynchronousCommand(PlayerCommand::SEEK);
|
2008-08-26 08:44:38 +02:00
|
|
|
|
2013-01-20 17:48:23 +01:00
|
|
|
assert(next_song == nullptr);
|
2015-11-11 17:31:32 +01:00
|
|
|
|
|
|
|
if (error_type != PlayerError::NONE) {
|
2016-09-08 10:21:34 +02:00
|
|
|
assert(error);
|
|
|
|
std::rethrow_exception(error);
|
2015-11-11 17:31:32 +01:00
|
|
|
}
|
|
|
|
|
2016-09-08 10:21:34 +02:00
|
|
|
assert(!error);
|
2015-11-11 16:56:24 +01:00
|
|
|
}
|
|
|
|
|
2016-09-08 10:29:49 +02:00
|
|
|
void
|
2017-11-26 11:46:14 +01:00
|
|
|
PlayerControl::LockSeek(std::unique_ptr<DetachedSong> song, SongTime t)
|
2015-11-11 16:56:24 +01:00
|
|
|
{
|
|
|
|
assert(song != nullptr);
|
|
|
|
|
2015-11-11 17:41:48 +01:00
|
|
|
{
|
2017-01-03 07:11:57 +01:00
|
|
|
const std::lock_guard<Mutex> protect(mutex);
|
2017-11-26 11:46:14 +01:00
|
|
|
SeekLocked(std::move(song), t);
|
2015-11-11 17:41:48 +01:00
|
|
|
}
|
2009-05-06 18:46:59 +02:00
|
|
|
|
2009-05-06 18:46:52 +02:00
|
|
|
idle_add(IDLE_PLAYER);
|
2008-08-26 08:44:38 +02:00
|
|
|
}
|
|
|
|
|
2009-10-08 21:12:57 +02:00
|
|
|
void
|
2017-11-26 12:02:08 +01:00
|
|
|
PlayerControl::SetCrossFade(float _cross_fade_seconds) noexcept
|
2008-08-26 08:44:38 +02:00
|
|
|
{
|
2013-01-20 17:48:23 +01:00
|
|
|
if (_cross_fade_seconds < 0)
|
|
|
|
_cross_fade_seconds = 0;
|
2013-10-29 00:14:27 +01:00
|
|
|
cross_fade.duration = _cross_fade_seconds;
|
2008-10-14 22:38:14 +02:00
|
|
|
|
|
|
|
idle_add(IDLE_OPTIONS);
|
2008-08-26 08:44:38 +02:00
|
|
|
}
|
|
|
|
|
2010-03-21 18:21:47 +01:00
|
|
|
void
|
2017-11-26 12:02:08 +01:00
|
|
|
PlayerControl::SetMixRampDb(float _mixramp_db) noexcept
|
2010-03-21 18:21:47 +01:00
|
|
|
{
|
2013-10-29 00:14:27 +01:00
|
|
|
cross_fade.mixramp_db = _mixramp_db;
|
2010-03-21 18:21:47 +01:00
|
|
|
|
|
|
|
idle_add(IDLE_OPTIONS);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2017-11-26 12:02:08 +01:00
|
|
|
PlayerControl::SetMixRampDelay(float _mixramp_delay_seconds) noexcept
|
2010-03-21 18:21:47 +01:00
|
|
|
{
|
2013-10-29 00:14:27 +01:00
|
|
|
cross_fade.mixramp_delay = _mixramp_delay_seconds;
|
2010-03-21 18:21:47 +01:00
|
|
|
|
|
|
|
idle_add(IDLE_OPTIONS);
|
|
|
|
}
|