2009-03-13 18:43:16 +01:00
|
|
|
/*
|
2020-01-18 19:22:19 +01:00
|
|
|
* Copyright 2003-2020 The Music Player Daemon Project
|
2009-03-13 18:43:16 +01:00
|
|
|
* http://www.musicpd.org
|
2008-08-26 08:27:18 +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:27:18 +02:00
|
|
|
*/
|
|
|
|
|
2018-09-21 16:56:53 +02:00
|
|
|
#include "Control.hxx"
|
2013-01-04 10:16:16 +01:00
|
|
|
#include "MusicPipe.hxx"
|
2018-08-02 13:45:43 +02:00
|
|
|
#include "song/DetachedSong.hxx"
|
2016-11-07 09:03:17 +01:00
|
|
|
|
|
|
|
#include <stdexcept>
|
2008-08-26 08:40:47 +02:00
|
|
|
|
2008-10-08 10:49:29 +02:00
|
|
|
#include <assert.h>
|
2010-03-21 18:21:47 +01:00
|
|
|
|
2016-12-03 13:05:25 +01:00
|
|
|
DecoderControl::DecoderControl(Mutex &_mutex, Cond &_client_cond,
|
2019-05-08 18:39:00 +02:00
|
|
|
InputCacheManager *_input_cache,
|
2016-12-03 14:12:08 +01:00
|
|
|
const AudioFormat _configured_audio_format,
|
2017-11-26 12:11:29 +01:00
|
|
|
const ReplayGainConfig &_replay_gain_config) noexcept
|
2017-02-10 22:41:11 +01:00
|
|
|
:thread(BIND_THIS_METHOD(RunThread)),
|
2019-05-08 18:39:00 +02:00
|
|
|
input_cache(_input_cache),
|
2017-02-10 22:41:11 +01:00
|
|
|
mutex(_mutex), client_cond(_client_cond),
|
2016-12-03 14:12:08 +01:00
|
|
|
configured_audio_format(_configured_audio_format),
|
2016-12-03 13:05:25 +01:00
|
|
|
replay_gain_config(_replay_gain_config) {}
|
2013-01-21 10:13:29 +01:00
|
|
|
|
2017-11-26 12:11:29 +01:00
|
|
|
DecoderControl::~DecoderControl() noexcept
|
2008-08-26 08:45:14 +02:00
|
|
|
{
|
2013-01-21 10:13:29 +01:00
|
|
|
ClearError();
|
2008-09-24 07:14:11 +02:00
|
|
|
}
|
|
|
|
|
2016-12-03 14:20:51 +01:00
|
|
|
void
|
|
|
|
DecoderControl::SetReady(const AudioFormat audio_format,
|
2017-11-26 12:11:29 +01:00
|
|
|
bool _seekable, SignedSongTime _duration) noexcept
|
2016-12-03 14:20:51 +01:00
|
|
|
{
|
|
|
|
assert(state == DecoderState::START);
|
|
|
|
assert(pipe != nullptr);
|
|
|
|
assert(pipe->IsEmpty());
|
|
|
|
assert(audio_format.IsDefined());
|
|
|
|
assert(audio_format.IsValid());
|
|
|
|
|
|
|
|
in_audio_format = audio_format;
|
2016-12-13 20:55:48 +01:00
|
|
|
out_audio_format = audio_format.WithMask(configured_audio_format);
|
2016-12-03 14:20:51 +01:00
|
|
|
|
|
|
|
seekable = _seekable;
|
|
|
|
total_time = _duration;
|
|
|
|
|
|
|
|
state = DecoderState::DECODE;
|
2019-04-25 18:33:09 +02:00
|
|
|
client_cond.notify_one();
|
2016-12-03 14:20:51 +01:00
|
|
|
}
|
|
|
|
|
2012-08-15 17:49:23 +02:00
|
|
|
bool
|
2017-05-08 14:44:49 +02:00
|
|
|
DecoderControl::IsCurrentSong(const DetachedSong &_song) const noexcept
|
2012-08-15 17:49:23 +02:00
|
|
|
{
|
2013-01-21 10:13:29 +01:00
|
|
|
switch (state) {
|
2013-09-27 12:27:33 +02:00
|
|
|
case DecoderState::STOP:
|
|
|
|
case DecoderState::ERROR:
|
2012-08-15 17:49:23 +02:00
|
|
|
return false;
|
|
|
|
|
2013-09-27 12:27:33 +02:00
|
|
|
case DecoderState::START:
|
|
|
|
case DecoderState::DECODE:
|
2014-01-07 21:39:47 +01:00
|
|
|
return song->IsSame(_song);
|
2012-08-15 17:49:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
assert(false);
|
2013-08-03 21:34:17 +02:00
|
|
|
gcc_unreachable();
|
2012-08-15 17:49:23 +02:00
|
|
|
}
|
|
|
|
|
2008-10-08 10:49:11 +02:00
|
|
|
void
|
2019-04-26 18:34:16 +02:00
|
|
|
DecoderControl::Start(std::unique_lock<Mutex> &lock,
|
|
|
|
std::unique_ptr<DetachedSong> _song,
|
2014-08-28 13:04:45 +02:00
|
|
|
SongTime _start_time, SongTime _end_time,
|
2018-06-23 18:45:57 +02:00
|
|
|
MusicBuffer &_buffer,
|
|
|
|
std::shared_ptr<MusicPipe> _pipe) noexcept
|
2008-08-26 08:27:18 +02:00
|
|
|
{
|
2013-10-19 18:19:03 +02:00
|
|
|
assert(_song != nullptr);
|
2018-06-23 18:45:57 +02:00
|
|
|
assert(_pipe->IsEmpty());
|
2008-08-26 08:27:18 +02:00
|
|
|
|
2017-11-26 12:11:07 +01:00
|
|
|
song = std::move(_song);
|
2014-08-28 13:04:45 +02:00
|
|
|
start_time = _start_time;
|
|
|
|
end_time = _end_time;
|
2018-06-23 18:04:09 +02:00
|
|
|
buffer = &_buffer;
|
2018-06-23 18:45:57 +02:00
|
|
|
pipe = std::move(_pipe);
|
2012-08-09 20:55:18 +02:00
|
|
|
|
2017-02-05 13:37:20 +01:00
|
|
|
ClearError();
|
2019-04-26 18:34:16 +02:00
|
|
|
SynchronousCommandLocked(lock, DecoderCommand::START);
|
2008-08-26 08:27:18 +02:00
|
|
|
}
|
|
|
|
|
2008-10-08 10:49:16 +02:00
|
|
|
void
|
2019-04-26 18:34:16 +02:00
|
|
|
DecoderControl::Stop(std::unique_lock<Mutex> &lock) noexcept
|
2008-08-26 08:27:18 +02:00
|
|
|
{
|
2013-09-27 12:11:37 +02:00
|
|
|
if (command != DecoderCommand::NONE)
|
2009-04-25 15:19:01 +02:00
|
|
|
/* Attempt to cancel the current command. If it's too
|
|
|
|
late and the decoder thread is already executing
|
|
|
|
the old command, we'll call STOP again in this
|
|
|
|
function (see below). */
|
2019-04-26 18:34:16 +02:00
|
|
|
SynchronousCommandLocked(lock, DecoderCommand::STOP);
|
2009-04-25 15:19:01 +02:00
|
|
|
|
2013-09-27 12:27:33 +02:00
|
|
|
if (state != DecoderState::STOP && state != DecoderState::ERROR)
|
2019-04-26 18:34:16 +02:00
|
|
|
SynchronousCommandLocked(lock, DecoderCommand::STOP);
|
2008-08-26 08:27:18 +02:00
|
|
|
}
|
|
|
|
|
2016-11-07 09:03:17 +01:00
|
|
|
void
|
2019-04-26 18:34:16 +02:00
|
|
|
DecoderControl::Seek(std::unique_lock<Mutex> &lock, SongTime t)
|
2008-08-26 08:27:18 +02:00
|
|
|
{
|
2013-09-27 12:27:33 +02:00
|
|
|
assert(state != DecoderState::START);
|
2015-11-11 17:28:48 +01:00
|
|
|
assert(state != DecoderState::ERROR);
|
2008-08-26 08:27:18 +02:00
|
|
|
|
2015-11-11 17:21:26 +01:00
|
|
|
switch (state) {
|
|
|
|
case DecoderState::START:
|
2015-11-11 17:28:48 +01:00
|
|
|
case DecoderState::ERROR:
|
2015-11-11 17:21:26 +01:00
|
|
|
gcc_unreachable();
|
|
|
|
|
|
|
|
case DecoderState::STOP:
|
2015-11-11 17:20:27 +01:00
|
|
|
/* TODO: if this happens, the caller should be given a
|
|
|
|
chance to restart the decoder */
|
2016-11-07 09:03:17 +01:00
|
|
|
throw std::runtime_error("Decoder is dead");
|
2015-11-11 17:21:26 +01:00
|
|
|
|
|
|
|
case DecoderState::DECODE:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2016-11-07 09:03:17 +01:00
|
|
|
if (!seekable)
|
|
|
|
throw std::runtime_error("Not seekable");
|
2008-08-26 08:27:18 +02:00
|
|
|
|
2014-08-27 18:38:41 +02:00
|
|
|
seek_time = t;
|
2013-01-21 10:13:29 +01:00
|
|
|
seek_error = false;
|
2019-04-26 18:34:16 +02:00
|
|
|
SynchronousCommandLocked(lock, DecoderCommand::SEEK);
|
2008-08-26 08:27:18 +02:00
|
|
|
|
2019-08-20 20:01:53 +02:00
|
|
|
while (state == DecoderState::START)
|
|
|
|
/* If the decoder falls back to DecoderState::START,
|
|
|
|
this means that our SEEK command arrived too late,
|
|
|
|
and the decoder had meanwhile finished decoding and
|
|
|
|
went idle. Our SEEK command is finished, but that
|
|
|
|
means only that the decoder thread has launched the
|
|
|
|
decoder. To work around illegal states, we wait
|
|
|
|
until the decoder plugin has become ready. This is
|
|
|
|
a kludge, built on top of the "late seek" kludge.
|
|
|
|
Not exactly elegant, sorry. */
|
2019-08-21 10:52:49 +02:00
|
|
|
WaitForDecoder(lock);
|
2019-08-20 20:01:53 +02:00
|
|
|
|
2016-11-07 09:03:17 +01:00
|
|
|
if (seek_error)
|
|
|
|
throw std::runtime_error("Decoder failed to seek");
|
2008-08-26 08:27:18 +02:00
|
|
|
}
|
2008-12-28 19:48:53 +01:00
|
|
|
|
|
|
|
void
|
2017-11-26 12:11:29 +01:00
|
|
|
DecoderControl::Quit() noexcept
|
2008-12-28 19:48:53 +01:00
|
|
|
{
|
2013-10-17 18:42:14 +02:00
|
|
|
assert(thread.IsDefined());
|
2009-01-25 13:44:27 +01:00
|
|
|
|
2013-01-21 10:13:29 +01:00
|
|
|
quit = true;
|
2013-09-27 12:11:37 +02:00
|
|
|
LockAsynchronousCommand(DecoderCommand::STOP);
|
2009-01-25 13:44:27 +01:00
|
|
|
|
2013-10-17 18:42:14 +02:00
|
|
|
thread.Join();
|
2008-12-28 19:48:53 +01:00
|
|
|
}
|
2010-03-21 18:21:47 +01:00
|
|
|
|
|
|
|
void
|
2017-11-26 12:11:29 +01:00
|
|
|
DecoderControl::CycleMixRamp() noexcept
|
2010-03-21 18:21:47 +01:00
|
|
|
{
|
2013-10-26 14:19:34 +02:00
|
|
|
previous_mix_ramp = std::move(mix_ramp);
|
|
|
|
mix_ramp.Clear();
|
2010-03-21 18:21:47 +01:00
|
|
|
}
|