2009-03-13 18:43:16 +01:00
|
|
|
/*
|
2016-02-26 17:54:05 +01:00
|
|
|
* Copyright 2003-2016 The Music Player Daemon Project
|
2009-03-13 18:43:16 +01:00
|
|
|
* http://www.musicpd.org
|
2008-08-26 08:27:04 +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:04 +02:00
|
|
|
*/
|
|
|
|
|
2009-11-12 09:12:38 +01:00
|
|
|
#include "config.h"
|
2013-07-28 13:18:48 +02:00
|
|
|
#include "DecoderAPI.hxx"
|
2013-09-27 22:31:24 +02:00
|
|
|
#include "DecoderError.hxx"
|
2013-11-13 19:10:43 +01:00
|
|
|
#include "pcm/PcmConvert.hxx"
|
2013-01-30 21:47:12 +01:00
|
|
|
#include "AudioConfig.hxx"
|
2013-10-02 12:22:12 +02:00
|
|
|
#include "ReplayGainConfig.hxx"
|
2013-01-04 10:16:16 +01:00
|
|
|
#include "MusicChunk.hxx"
|
|
|
|
#include "MusicBuffer.hxx"
|
|
|
|
#include "MusicPipe.hxx"
|
2013-01-04 08:41:16 +01:00
|
|
|
#include "DecoderControl.hxx"
|
|
|
|
#include "DecoderInternal.hxx"
|
2014-01-07 21:39:47 +01:00
|
|
|
#include "DetachedSong.hxx"
|
2014-01-24 16:18:21 +01:00
|
|
|
#include "input/InputStream.hxx"
|
2013-08-10 18:02:44 +02:00
|
|
|
#include "util/Error.hxx"
|
2014-08-12 16:36:07 +02:00
|
|
|
#include "util/ConstBuffer.hxx"
|
2013-09-27 22:31:24 +02:00
|
|
|
#include "Log.hxx"
|
2008-10-08 10:49:29 +02:00
|
|
|
|
2009-01-03 14:51:28 +01:00
|
|
|
#include <assert.h>
|
2013-07-30 20:11:57 +02:00
|
|
|
#include <string.h>
|
2013-11-11 08:18:48 +01:00
|
|
|
#include <math.h>
|
2009-01-03 14:51:28 +01:00
|
|
|
|
2009-10-31 19:22:56 +01:00
|
|
|
void
|
2013-10-21 21:12:37 +02:00
|
|
|
decoder_initialized(Decoder &decoder,
|
2013-08-03 21:00:50 +02:00
|
|
|
const AudioFormat audio_format,
|
2014-08-29 20:52:39 +02:00
|
|
|
bool seekable, SignedSongTime duration)
|
2008-08-26 08:27:04 +02:00
|
|
|
{
|
2013-10-28 10:09:21 +01:00
|
|
|
DecoderControl &dc = decoder.dc;
|
2009-11-10 17:57:14 +01:00
|
|
|
struct audio_format_string af_string;
|
2009-10-31 19:22:56 +01:00
|
|
|
|
2013-10-19 18:48:38 +02:00
|
|
|
assert(dc.state == DecoderState::START);
|
|
|
|
assert(dc.pipe != nullptr);
|
2014-09-07 21:50:27 +02:00
|
|
|
assert(dc.pipe->IsEmpty());
|
2013-11-13 19:10:43 +01:00
|
|
|
assert(decoder.convert == nullptr);
|
2013-10-21 21:12:37 +02:00
|
|
|
assert(decoder.stream_tag == nullptr);
|
|
|
|
assert(decoder.decoder_tag == nullptr);
|
|
|
|
assert(!decoder.seeking);
|
2013-08-03 21:00:50 +02:00
|
|
|
assert(audio_format.IsDefined());
|
|
|
|
assert(audio_format.IsValid());
|
2008-08-26 08:27:04 +02:00
|
|
|
|
2013-10-19 18:48:38 +02:00
|
|
|
dc.in_audio_format = audio_format;
|
|
|
|
dc.out_audio_format = getOutputAudioFormat(audio_format);
|
2008-08-26 08:27:05 +02:00
|
|
|
|
2013-10-19 18:48:38 +02:00
|
|
|
dc.seekable = seekable;
|
2014-08-29 20:52:39 +02:00
|
|
|
dc.total_time = duration;
|
2008-08-26 08:27:05 +02:00
|
|
|
|
2013-09-27 22:31:24 +02:00
|
|
|
FormatDebug(decoder_domain, "audio_format=%s, seekable=%s",
|
2013-10-19 18:48:38 +02:00
|
|
|
audio_format_to_string(dc.in_audio_format, &af_string),
|
2013-09-27 22:31:24 +02:00
|
|
|
seekable ? "true" : "false");
|
2009-03-01 10:31:47 +01:00
|
|
|
|
2013-11-13 19:10:43 +01:00
|
|
|
if (dc.in_audio_format != dc.out_audio_format) {
|
2013-09-27 22:31:24 +02:00
|
|
|
FormatDebug(decoder_domain, "converting to %s",
|
2013-10-19 18:48:38 +02:00
|
|
|
audio_format_to_string(dc.out_audio_format,
|
2013-09-27 22:31:24 +02:00
|
|
|
&af_string));
|
2013-11-13 19:10:43 +01:00
|
|
|
|
|
|
|
decoder.convert = new PcmConvert();
|
2013-11-11 16:15:38 +01:00
|
|
|
|
|
|
|
Error error;
|
|
|
|
if (!decoder.convert->Open(dc.in_audio_format,
|
|
|
|
dc.out_audio_format,
|
|
|
|
error))
|
|
|
|
decoder.error = std::move(error);
|
2013-11-13 19:10:43 +01:00
|
|
|
}
|
|
|
|
|
2015-12-31 13:43:35 +01:00
|
|
|
const ScopeLock protect(dc.mutex);
|
2013-11-13 19:10:43 +01:00
|
|
|
dc.state = DecoderState::DECODE;
|
|
|
|
dc.client_cond.signal();
|
2008-08-26 08:27:04 +02:00
|
|
|
}
|
2008-08-26 08:27:05 +02:00
|
|
|
|
2011-09-21 23:17:34 +02:00
|
|
|
/**
|
2011-10-06 00:25:44 +02:00
|
|
|
* Checks if we need an "initial seek". If so, then the initial seek
|
|
|
|
* is prepared, and the function returns true.
|
2011-09-21 23:17:34 +02:00
|
|
|
*/
|
2013-08-04 23:48:01 +02:00
|
|
|
gcc_pure
|
2011-10-06 00:25:44 +02:00
|
|
|
static bool
|
2013-10-21 21:12:37 +02:00
|
|
|
decoder_prepare_initial_seek(Decoder &decoder)
|
2008-08-26 08:27:07 +02:00
|
|
|
{
|
2013-10-28 10:09:21 +01:00
|
|
|
const DecoderControl &dc = decoder.dc;
|
2013-10-19 18:48:38 +02:00
|
|
|
assert(dc.pipe != nullptr);
|
2009-04-25 15:07:22 +02:00
|
|
|
|
2013-10-19 18:48:38 +02:00
|
|
|
if (dc.state != DecoderState::DECODE)
|
2012-02-13 18:27:43 +01:00
|
|
|
/* wait until the decoder has finished initialisation
|
|
|
|
(reading file headers etc.) before emitting the
|
|
|
|
virtual "SEEK" command */
|
|
|
|
return false;
|
|
|
|
|
2013-10-21 21:12:37 +02:00
|
|
|
if (decoder.initial_seek_running)
|
2011-10-06 00:25:44 +02:00
|
|
|
/* initial seek has already begun - override any other
|
|
|
|
command */
|
|
|
|
return true;
|
2011-09-21 23:17:34 +02:00
|
|
|
|
2013-10-21 21:12:37 +02:00
|
|
|
if (decoder.initial_seek_pending) {
|
2013-10-19 18:48:38 +02:00
|
|
|
if (!dc.seekable) {
|
2011-11-27 19:19:43 +01:00
|
|
|
/* seeking is not possible */
|
2013-10-21 21:12:37 +02:00
|
|
|
decoder.initial_seek_pending = false;
|
2011-11-27 19:19:43 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-10-19 18:48:38 +02:00
|
|
|
if (dc.command == DecoderCommand::NONE) {
|
2011-10-06 00:25:44 +02:00
|
|
|
/* begin initial seek */
|
|
|
|
|
2013-10-21 21:12:37 +02:00
|
|
|
decoder.initial_seek_pending = false;
|
|
|
|
decoder.initial_seek_running = true;
|
2011-10-06 00:25:44 +02:00
|
|
|
return true;
|
2011-09-21 23:17:34 +02:00
|
|
|
}
|
|
|
|
|
2011-10-06 00:25:44 +02:00
|
|
|
/* skip initial seek when there's another command
|
|
|
|
(e.g. STOP) */
|
|
|
|
|
2013-10-21 21:12:37 +02:00
|
|
|
decoder.initial_seek_pending = false;
|
2011-09-21 23:17:34 +02:00
|
|
|
}
|
|
|
|
|
2011-10-06 00:25:44 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the current decoder command. May return a "virtual"
|
|
|
|
* synthesized command, e.g. to seek to the beginning of the CUE
|
|
|
|
* track.
|
|
|
|
*/
|
2013-08-04 23:48:01 +02:00
|
|
|
gcc_pure
|
2013-09-27 12:11:37 +02:00
|
|
|
static DecoderCommand
|
2013-10-21 21:12:37 +02:00
|
|
|
decoder_get_virtual_command(Decoder &decoder)
|
2011-10-06 00:25:44 +02:00
|
|
|
{
|
2013-11-13 20:57:09 +01:00
|
|
|
if (decoder.error.IsDefined())
|
|
|
|
/* an error has occurred: stop the decoder plugin */
|
|
|
|
return DecoderCommand::STOP;
|
|
|
|
|
2013-10-28 10:09:21 +01:00
|
|
|
const DecoderControl &dc = decoder.dc;
|
2013-10-19 18:48:38 +02:00
|
|
|
assert(dc.pipe != nullptr);
|
2011-10-06 00:25:44 +02:00
|
|
|
|
|
|
|
if (decoder_prepare_initial_seek(decoder))
|
2013-09-27 12:11:37 +02:00
|
|
|
return DecoderCommand::SEEK;
|
2011-10-06 00:25:44 +02:00
|
|
|
|
2013-10-19 18:48:38 +02:00
|
|
|
return dc.command;
|
2008-08-26 08:27:07 +02:00
|
|
|
}
|
|
|
|
|
2015-12-31 13:43:35 +01:00
|
|
|
gcc_pure
|
|
|
|
static DecoderCommand
|
|
|
|
decoder_lock_get_virtual_command(Decoder &decoder)
|
|
|
|
{
|
|
|
|
const ScopeLock protect(decoder.dc.mutex);
|
|
|
|
return decoder_get_virtual_command(decoder);
|
|
|
|
}
|
|
|
|
|
2013-09-27 12:11:37 +02:00
|
|
|
DecoderCommand
|
2013-10-21 21:12:37 +02:00
|
|
|
decoder_get_command(Decoder &decoder)
|
2011-09-21 23:17:34 +02:00
|
|
|
{
|
|
|
|
return decoder_get_virtual_command(decoder);
|
|
|
|
}
|
|
|
|
|
2009-12-25 19:47:33 +01:00
|
|
|
void
|
2013-10-21 21:12:37 +02:00
|
|
|
decoder_command_finished(Decoder &decoder)
|
2008-08-26 08:27:07 +02:00
|
|
|
{
|
2013-10-28 10:09:21 +01:00
|
|
|
DecoderControl &dc = decoder.dc;
|
2009-08-13 23:33:46 +02:00
|
|
|
|
2015-12-31 13:43:35 +01:00
|
|
|
const ScopeLock protect(dc.mutex);
|
2008-08-26 08:27:07 +02:00
|
|
|
|
2013-10-19 18:48:38 +02:00
|
|
|
assert(dc.command != DecoderCommand::NONE ||
|
2013-10-21 21:12:37 +02:00
|
|
|
decoder.initial_seek_running);
|
2013-10-19 18:48:38 +02:00
|
|
|
assert(dc.command != DecoderCommand::SEEK ||
|
2013-10-21 21:12:37 +02:00
|
|
|
decoder.initial_seek_running ||
|
|
|
|
dc.seek_error || decoder.seeking);
|
2013-10-19 18:48:38 +02:00
|
|
|
assert(dc.pipe != nullptr);
|
2009-10-31 19:22:56 +01:00
|
|
|
|
2013-10-21 21:12:37 +02:00
|
|
|
if (decoder.initial_seek_running) {
|
|
|
|
assert(!decoder.seeking);
|
|
|
|
assert(decoder.chunk == nullptr);
|
2013-10-19 18:48:38 +02:00
|
|
|
assert(dc.pipe->IsEmpty());
|
2011-09-21 23:17:34 +02:00
|
|
|
|
2013-10-21 21:12:37 +02:00
|
|
|
decoder.initial_seek_running = false;
|
2014-08-28 13:04:45 +02:00
|
|
|
decoder.timestamp = dc.start_time.ToDoubleS();
|
2011-09-21 23:17:34 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-10-21 21:12:37 +02:00
|
|
|
if (decoder.seeking) {
|
|
|
|
decoder.seeking = false;
|
2009-12-26 01:10:23 +01:00
|
|
|
|
2008-10-29 17:28:47 +01:00
|
|
|
/* delete frames from the old song position */
|
2009-03-06 00:42:01 +01:00
|
|
|
|
2013-10-21 21:12:37 +02:00
|
|
|
if (decoder.chunk != nullptr) {
|
|
|
|
dc.buffer->Return(decoder.chunk);
|
|
|
|
decoder.chunk = nullptr;
|
2009-03-06 00:42:01 +01:00
|
|
|
}
|
|
|
|
|
2013-10-19 18:48:38 +02:00
|
|
|
dc.pipe->Clear(*dc.buffer);
|
2009-12-25 19:47:33 +01:00
|
|
|
|
2014-08-27 18:38:41 +02:00
|
|
|
decoder.timestamp = dc.seek_time.ToDoubleS();
|
2009-03-06 00:42:01 +01:00
|
|
|
}
|
2008-10-29 17:28:47 +01:00
|
|
|
|
2013-10-19 18:48:38 +02:00
|
|
|
dc.command = DecoderCommand::NONE;
|
|
|
|
dc.client_cond.signal();
|
2008-08-26 08:27:07 +02:00
|
|
|
}
|
|
|
|
|
2014-08-26 21:52:28 +02:00
|
|
|
SongTime
|
|
|
|
decoder_seek_time(Decoder &decoder)
|
2008-08-26 08:27:07 +02:00
|
|
|
{
|
2013-10-28 10:09:21 +01:00
|
|
|
const DecoderControl &dc = decoder.dc;
|
2009-10-31 19:22:56 +01:00
|
|
|
|
2013-10-19 18:48:38 +02:00
|
|
|
assert(dc.pipe != nullptr);
|
2008-08-26 08:27:07 +02:00
|
|
|
|
2013-10-21 21:12:37 +02:00
|
|
|
if (decoder.initial_seek_running)
|
2014-08-28 13:04:45 +02:00
|
|
|
return dc.start_time;
|
2011-09-21 23:17:34 +02:00
|
|
|
|
2013-10-19 18:48:38 +02:00
|
|
|
assert(dc.command == DecoderCommand::SEEK);
|
2011-09-21 23:17:34 +02:00
|
|
|
|
2013-10-21 21:12:37 +02:00
|
|
|
decoder.seeking = true;
|
2008-08-26 08:27:14 +02:00
|
|
|
|
2014-08-27 18:38:41 +02:00
|
|
|
return dc.seek_time;
|
2014-08-26 11:02:02 +02:00
|
|
|
}
|
|
|
|
|
2014-08-26 11:27:41 +02:00
|
|
|
uint64_t
|
|
|
|
decoder_seek_where_frame(Decoder &decoder)
|
|
|
|
{
|
|
|
|
const DecoderControl &dc = decoder.dc;
|
|
|
|
|
2014-08-28 13:04:45 +02:00
|
|
|
return decoder_seek_time(decoder).ToScale<uint64_t>(dc.in_audio_format.sample_rate);
|
2014-08-26 11:27:41 +02:00
|
|
|
}
|
|
|
|
|
2013-10-21 21:12:37 +02:00
|
|
|
void decoder_seek_error(Decoder & decoder)
|
2008-08-26 08:27:07 +02:00
|
|
|
{
|
2013-10-28 10:09:21 +01:00
|
|
|
DecoderControl &dc = decoder.dc;
|
2009-10-31 19:22:56 +01:00
|
|
|
|
2013-10-19 18:48:38 +02:00
|
|
|
assert(dc.pipe != nullptr);
|
2008-08-26 08:27:07 +02:00
|
|
|
|
2013-10-21 21:12:37 +02:00
|
|
|
if (decoder.initial_seek_running) {
|
2011-09-21 23:17:34 +02:00
|
|
|
/* d'oh, we can't seek to the sub-song start position,
|
|
|
|
what now? - no idea, ignoring the problem for now. */
|
2013-10-21 21:12:37 +02:00
|
|
|
decoder.initial_seek_running = false;
|
2011-09-21 23:17:34 +02:00
|
|
|
return;
|
2011-10-04 22:07:01 +02:00
|
|
|
}
|
2011-09-21 23:17:34 +02:00
|
|
|
|
2013-10-19 18:48:38 +02:00
|
|
|
assert(dc.command == DecoderCommand::SEEK);
|
2011-09-21 23:17:34 +02:00
|
|
|
|
2013-10-19 18:48:38 +02:00
|
|
|
dc.seek_error = true;
|
2013-10-21 21:12:37 +02:00
|
|
|
decoder.seeking = false;
|
2009-12-26 01:10:23 +01:00
|
|
|
|
2008-08-26 08:27:07 +02:00
|
|
|
decoder_command_finished(decoder);
|
|
|
|
}
|
|
|
|
|
2016-02-21 08:03:32 +01:00
|
|
|
InputStreamPtr
|
2014-05-22 11:10:41 +02:00
|
|
|
decoder_open_uri(Decoder &decoder, const char *uri, Error &error)
|
|
|
|
{
|
|
|
|
assert(decoder.dc.state == DecoderState::START ||
|
|
|
|
decoder.dc.state == DecoderState::DECODE);
|
|
|
|
|
|
|
|
DecoderControl &dc = decoder.dc;
|
|
|
|
Mutex &mutex = dc.mutex;
|
|
|
|
Cond &cond = dc.cond;
|
|
|
|
|
2016-02-21 08:03:32 +01:00
|
|
|
auto is = InputStream::Open(uri, mutex, cond, error);
|
|
|
|
if (!is)
|
2014-05-22 11:10:41 +02:00
|
|
|
return nullptr;
|
|
|
|
|
2016-05-02 23:33:08 +02:00
|
|
|
const ScopeLock lock(mutex);
|
2014-05-22 11:10:41 +02:00
|
|
|
while (true) {
|
|
|
|
is->Update();
|
2016-05-02 23:33:08 +02:00
|
|
|
if (is->IsReady())
|
2014-05-22 11:10:41 +02:00
|
|
|
return is;
|
|
|
|
|
2016-05-02 23:33:08 +02:00
|
|
|
if (dc.command == DecoderCommand::STOP)
|
2014-05-22 11:10:41 +02:00
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
cond.wait(mutex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-16 07:28:27 +02:00
|
|
|
/**
|
|
|
|
* Should be read operation be cancelled? That is the case when the
|
|
|
|
* player thread has sent a command such as "STOP".
|
|
|
|
*/
|
2013-08-04 23:48:01 +02:00
|
|
|
gcc_pure
|
2011-09-16 07:28:27 +02:00
|
|
|
static inline bool
|
2013-10-21 21:12:37 +02:00
|
|
|
decoder_check_cancel_read(const Decoder *decoder)
|
2011-09-16 07:28:27 +02:00
|
|
|
{
|
2013-10-19 18:19:03 +02:00
|
|
|
if (decoder == nullptr)
|
2011-09-16 07:28:27 +02:00
|
|
|
return false;
|
|
|
|
|
2013-10-28 10:09:21 +01:00
|
|
|
const DecoderControl &dc = decoder->dc;
|
2013-10-19 18:48:38 +02:00
|
|
|
if (dc.command == DecoderCommand::NONE)
|
2011-09-16 07:28:27 +02:00
|
|
|
return false;
|
|
|
|
|
|
|
|
/* ignore the SEEK command during initialization, the plugin
|
|
|
|
should handle that after it has initialized successfully */
|
2013-10-19 18:48:38 +02:00
|
|
|
if (dc.command == DecoderCommand::SEEK &&
|
2016-07-06 15:37:11 +02:00
|
|
|
(dc.state == DecoderState::START || decoder->seeking ||
|
|
|
|
decoder->initial_seek_running))
|
2011-09-16 07:28:27 +02:00
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-10-21 21:12:37 +02:00
|
|
|
size_t
|
|
|
|
decoder_read(Decoder *decoder,
|
2013-10-23 22:08:59 +02:00
|
|
|
InputStream &is,
|
2013-10-21 21:12:37 +02:00
|
|
|
void *buffer, size_t length)
|
2008-08-26 08:27:14 +02:00
|
|
|
{
|
2013-10-19 18:19:03 +02:00
|
|
|
/* XXX don't allow decoder==nullptr */
|
2008-08-26 08:27:14 +02:00
|
|
|
|
2013-10-19 18:19:03 +02:00
|
|
|
assert(decoder == nullptr ||
|
2013-10-19 18:48:38 +02:00
|
|
|
decoder->dc.state == DecoderState::START ||
|
|
|
|
decoder->dc.state == DecoderState::DECODE);
|
2013-10-19 18:19:03 +02:00
|
|
|
assert(buffer != nullptr);
|
2008-08-26 08:27:14 +02:00
|
|
|
|
2008-11-15 19:27:30 +01:00
|
|
|
if (length == 0)
|
|
|
|
return 0;
|
|
|
|
|
2016-05-02 23:33:08 +02:00
|
|
|
ScopeLock lock(is.mutex);
|
2011-09-14 21:46:41 +02:00
|
|
|
|
|
|
|
while (true) {
|
2016-05-02 23:33:08 +02:00
|
|
|
if (decoder_check_cancel_read(decoder))
|
2011-09-14 21:46:41 +02:00
|
|
|
return 0;
|
|
|
|
|
2013-10-23 22:08:59 +02:00
|
|
|
if (is.IsAvailable())
|
2011-09-14 21:46:41 +02:00
|
|
|
break;
|
|
|
|
|
2013-10-23 22:08:59 +02:00
|
|
|
is.cond.wait(is.mutex);
|
2011-09-14 21:46:41 +02:00
|
|
|
}
|
2009-11-14 23:53:04 +01:00
|
|
|
|
2013-08-10 18:02:44 +02:00
|
|
|
Error error;
|
2013-10-23 22:08:59 +02:00
|
|
|
size_t nbytes = is.Read(buffer, length, error);
|
2013-08-10 18:02:44 +02:00
|
|
|
assert(nbytes == 0 || !error.IsDefined());
|
2013-10-23 22:08:59 +02:00
|
|
|
assert(nbytes > 0 || error.IsDefined() || is.IsEOF());
|
2008-08-26 08:27:14 +02:00
|
|
|
|
2016-05-02 23:33:08 +02:00
|
|
|
lock.Unlock();
|
2013-11-08 12:15:05 +01:00
|
|
|
|
2013-08-10 18:02:44 +02:00
|
|
|
if (gcc_unlikely(nbytes == 0 && error.IsDefined()))
|
2013-09-27 22:31:24 +02:00
|
|
|
LogError(error);
|
2011-09-16 08:04:02 +02:00
|
|
|
|
|
|
|
return nbytes;
|
2008-08-26 08:27:14 +02:00
|
|
|
}
|
|
|
|
|
2013-12-14 12:43:06 +01:00
|
|
|
bool
|
|
|
|
decoder_read_full(Decoder *decoder, InputStream &is,
|
|
|
|
void *_buffer, size_t size)
|
|
|
|
{
|
|
|
|
uint8_t *buffer = (uint8_t *)_buffer;
|
|
|
|
|
|
|
|
while (size > 0) {
|
|
|
|
size_t nbytes = decoder_read(decoder, is, buffer, size);
|
|
|
|
if (nbytes == 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
buffer += nbytes;
|
|
|
|
size -= nbytes;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-12-14 12:21:23 +01:00
|
|
|
bool
|
|
|
|
decoder_skip(Decoder *decoder, InputStream &is, size_t size)
|
|
|
|
{
|
|
|
|
while (size > 0) {
|
|
|
|
char buffer[1024];
|
|
|
|
size_t nbytes = decoder_read(decoder, is, buffer,
|
|
|
|
std::min(sizeof(buffer), size));
|
|
|
|
if (nbytes == 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
size -= nbytes;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-12-25 19:47:33 +01:00
|
|
|
void
|
2013-10-21 21:12:37 +02:00
|
|
|
decoder_timestamp(Decoder &decoder, double t)
|
2009-12-25 19:47:33 +01:00
|
|
|
{
|
|
|
|
assert(t >= 0);
|
|
|
|
|
2013-10-21 21:12:37 +02:00
|
|
|
decoder.timestamp = t;
|
2009-12-25 19:47:33 +01:00
|
|
|
}
|
|
|
|
|
2008-08-26 08:27:05 +02:00
|
|
|
/**
|
2009-03-06 00:42:01 +01:00
|
|
|
* Sends a #tag as-is to the music pipe. Flushes the current chunk
|
|
|
|
* (decoder.chunk) if there is one.
|
2008-08-26 08:27:05 +02:00
|
|
|
*/
|
2013-09-27 12:11:37 +02:00
|
|
|
static DecoderCommand
|
2013-10-21 21:12:37 +02:00
|
|
|
do_send_tag(Decoder &decoder, const Tag &tag)
|
2008-08-26 08:27:05 +02:00
|
|
|
{
|
2014-08-12 15:56:41 +02:00
|
|
|
MusicChunk *chunk;
|
2008-08-26 08:27:05 +02:00
|
|
|
|
2013-10-21 21:12:37 +02:00
|
|
|
if (decoder.chunk != nullptr) {
|
2009-03-06 00:42:01 +01:00
|
|
|
/* there is a partial chunk - flush it, we want the
|
|
|
|
tag in a new chunk */
|
2013-11-13 19:13:47 +01:00
|
|
|
decoder.FlushChunk();
|
2009-01-03 23:28:51 +01:00
|
|
|
}
|
|
|
|
|
2013-10-21 21:12:37 +02:00
|
|
|
assert(decoder.chunk == nullptr);
|
2009-03-06 00:42:01 +01:00
|
|
|
|
2013-11-13 19:13:47 +01:00
|
|
|
chunk = decoder.GetChunk();
|
2013-10-19 18:19:03 +02:00
|
|
|
if (chunk == nullptr) {
|
2013-10-21 21:12:37 +02:00
|
|
|
assert(decoder.dc.command != DecoderCommand::NONE);
|
|
|
|
return decoder.dc.command;
|
2009-03-06 00:42:03 +01:00
|
|
|
}
|
|
|
|
|
2013-07-30 20:11:57 +02:00
|
|
|
chunk->tag = new Tag(tag);
|
2013-09-27 12:11:37 +02:00
|
|
|
return DecoderCommand::NONE;
|
2009-01-03 23:28:51 +01:00
|
|
|
}
|
|
|
|
|
2009-01-03 23:29:45 +01:00
|
|
|
static bool
|
2013-10-23 22:08:59 +02:00
|
|
|
update_stream_tag(Decoder &decoder, InputStream *is)
|
2009-01-03 23:29:45 +01:00
|
|
|
{
|
2013-07-30 20:11:57 +02:00
|
|
|
Tag *tag;
|
2009-01-03 23:29:45 +01:00
|
|
|
|
2013-10-19 18:19:03 +02:00
|
|
|
tag = is != nullptr
|
2013-09-05 00:06:31 +02:00
|
|
|
? is->LockReadTag()
|
2013-10-19 18:19:03 +02:00
|
|
|
: nullptr;
|
|
|
|
if (tag == nullptr) {
|
2013-10-21 21:12:37 +02:00
|
|
|
tag = decoder.song_tag;
|
2013-10-19 18:19:03 +02:00
|
|
|
if (tag == nullptr)
|
2009-04-13 19:25:53 +02:00
|
|
|
return false;
|
|
|
|
|
|
|
|
/* no stream tag present - submit the song tag
|
|
|
|
instead */
|
2015-06-21 15:09:50 +02:00
|
|
|
} else
|
|
|
|
/* discard the song tag; we don't need it */
|
|
|
|
delete decoder.song_tag;
|
|
|
|
|
|
|
|
decoder.song_tag = nullptr;
|
2009-01-03 23:29:45 +01:00
|
|
|
|
2013-10-21 21:12:37 +02:00
|
|
|
delete decoder.stream_tag;
|
|
|
|
decoder.stream_tag = tag;
|
2009-01-03 23:29:45 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-09-27 12:11:37 +02:00
|
|
|
DecoderCommand
|
2013-10-21 21:12:37 +02:00
|
|
|
decoder_data(Decoder &decoder,
|
2013-10-23 22:08:59 +02:00
|
|
|
InputStream *is,
|
2013-01-04 08:41:16 +01:00
|
|
|
const void *data, size_t length,
|
2010-01-03 22:44:23 +01:00
|
|
|
uint16_t kbit_rate)
|
2008-08-26 08:27:05 +02:00
|
|
|
{
|
2013-10-28 10:09:21 +01:00
|
|
|
DecoderControl &dc = decoder.dc;
|
2008-08-26 08:27:05 +02:00
|
|
|
|
2013-10-19 18:48:38 +02:00
|
|
|
assert(dc.state == DecoderState::DECODE);
|
|
|
|
assert(dc.pipe != nullptr);
|
|
|
|
assert(length % dc.in_audio_format.GetFrameSize() == 0);
|
2008-11-10 15:02:26 +01:00
|
|
|
|
2015-12-31 13:43:35 +01:00
|
|
|
DecoderCommand cmd = decoder_lock_get_virtual_command(decoder);
|
2009-08-13 23:33:46 +02:00
|
|
|
|
2013-09-27 12:11:37 +02:00
|
|
|
if (cmd == DecoderCommand::STOP || cmd == DecoderCommand::SEEK ||
|
2008-11-21 20:26:57 +01:00
|
|
|
length == 0)
|
2009-08-13 23:33:46 +02:00
|
|
|
return cmd;
|
2008-11-13 02:42:40 +01:00
|
|
|
|
2014-09-07 21:50:27 +02:00
|
|
|
assert(!decoder.initial_seek_pending);
|
|
|
|
assert(!decoder.initial_seek_running);
|
|
|
|
|
2009-01-03 23:29:45 +01:00
|
|
|
/* send stream tags */
|
|
|
|
|
|
|
|
if (update_stream_tag(decoder, is)) {
|
2013-10-21 21:12:37 +02:00
|
|
|
if (decoder.decoder_tag != nullptr) {
|
2009-01-03 23:29:45 +01:00
|
|
|
/* merge with tag from decoder plugin */
|
2013-10-21 21:12:37 +02:00
|
|
|
Tag *tag = Tag::Merge(*decoder.decoder_tag,
|
|
|
|
*decoder.stream_tag);
|
2013-07-30 20:11:57 +02:00
|
|
|
cmd = do_send_tag(decoder, *tag);
|
|
|
|
delete tag;
|
2009-01-03 23:29:45 +01:00
|
|
|
} else
|
|
|
|
/* send only the stream tag */
|
2013-10-21 21:12:37 +02:00
|
|
|
cmd = do_send_tag(decoder, *decoder.stream_tag);
|
2009-01-03 23:29:45 +01:00
|
|
|
|
2013-09-27 12:11:37 +02:00
|
|
|
if (cmd != DecoderCommand::NONE)
|
2009-01-03 23:29:45 +01:00
|
|
|
return cmd;
|
2008-11-03 18:24:01 +01:00
|
|
|
}
|
|
|
|
|
2013-11-13 19:10:43 +01:00
|
|
|
if (decoder.convert != nullptr) {
|
|
|
|
assert(dc.in_audio_format != dc.out_audio_format);
|
|
|
|
|
2013-08-10 18:02:44 +02:00
|
|
|
Error error;
|
2014-08-12 16:36:07 +02:00
|
|
|
auto result = decoder.convert->Convert({data, length},
|
|
|
|
error);
|
2013-10-19 18:19:03 +02:00
|
|
|
if (data == nullptr) {
|
2009-07-23 12:01:03 +02:00
|
|
|
/* the PCM conversion has failed - stop
|
|
|
|
playback, since we have no better way to
|
|
|
|
bail out */
|
2013-09-27 22:31:24 +02:00
|
|
|
LogError(error);
|
2013-09-27 12:11:37 +02:00
|
|
|
return DecoderCommand::STOP;
|
2009-07-23 12:01:03 +02:00
|
|
|
}
|
2014-08-12 16:36:07 +02:00
|
|
|
|
|
|
|
data = result.data;
|
|
|
|
length = result.size;
|
2013-11-13 19:10:43 +01:00
|
|
|
} else {
|
|
|
|
assert(dc.in_audio_format == dc.out_audio_format);
|
2008-08-26 08:27:05 +02:00
|
|
|
}
|
|
|
|
|
2008-11-03 17:56:41 +01:00
|
|
|
while (length > 0) {
|
2014-08-12 15:56:41 +02:00
|
|
|
MusicChunk *chunk;
|
2009-03-06 00:42:01 +01:00
|
|
|
bool full;
|
|
|
|
|
2013-11-13 19:13:47 +01:00
|
|
|
chunk = decoder.GetChunk();
|
2013-10-19 18:19:03 +02:00
|
|
|
if (chunk == nullptr) {
|
2013-10-19 18:48:38 +02:00
|
|
|
assert(dc.command != DecoderCommand::NONE);
|
|
|
|
return dc.command;
|
2009-03-06 00:42:03 +01:00
|
|
|
}
|
|
|
|
|
2013-10-28 17:10:12 +01:00
|
|
|
const auto dest =
|
|
|
|
chunk->Write(dc.out_audio_format,
|
2014-08-29 13:15:33 +02:00
|
|
|
SongTime::FromS(decoder.timestamp) -
|
|
|
|
dc.song->GetStartTime(),
|
2013-10-28 17:10:12 +01:00
|
|
|
kbit_rate);
|
2014-08-31 08:26:03 +02:00
|
|
|
if (dest.IsEmpty()) {
|
2009-03-06 00:42:01 +01:00
|
|
|
/* the chunk is full, flush it */
|
2013-11-13 19:13:47 +01:00
|
|
|
decoder.FlushChunk();
|
2009-01-17 13:11:10 +01:00
|
|
|
continue;
|
2008-08-26 08:27:05 +02:00
|
|
|
}
|
2009-01-17 13:11:10 +01:00
|
|
|
|
2014-08-31 08:27:51 +02:00
|
|
|
const size_t nbytes = std::min(dest.size, length);
|
2009-01-17 13:11:10 +01:00
|
|
|
|
|
|
|
/* copy the buffer */
|
|
|
|
|
2013-10-28 17:10:12 +01:00
|
|
|
memcpy(dest.data, data, nbytes);
|
2009-01-17 13:11:10 +01:00
|
|
|
|
|
|
|
/* expand the music pipe chunk */
|
|
|
|
|
2013-10-19 18:48:38 +02:00
|
|
|
full = chunk->Expand(dc.out_audio_format, nbytes);
|
2009-03-06 00:42:01 +01:00
|
|
|
if (full) {
|
|
|
|
/* the chunk is full, flush it */
|
2013-11-13 19:13:47 +01:00
|
|
|
decoder.FlushChunk();
|
2009-03-06 00:42:01 +01:00
|
|
|
}
|
2009-01-17 13:11:10 +01:00
|
|
|
|
2013-01-04 08:41:16 +01:00
|
|
|
data = (const uint8_t *)data + nbytes;
|
2009-01-17 13:11:10 +01:00
|
|
|
length -= nbytes;
|
2009-12-25 19:47:33 +01:00
|
|
|
|
2013-10-21 21:12:37 +02:00
|
|
|
decoder.timestamp += (double)nbytes /
|
2013-10-19 18:48:38 +02:00
|
|
|
dc.out_audio_format.GetTimeToSize();
|
2009-12-25 23:12:10 +01:00
|
|
|
|
2014-08-28 13:04:45 +02:00
|
|
|
if (dc.end_time.IsPositive() &&
|
|
|
|
decoder.timestamp >= dc.end_time.ToDoubleS())
|
2009-12-25 23:12:10 +01:00
|
|
|
/* the end of this range has been reached:
|
|
|
|
stop decoding */
|
2013-09-27 12:11:37 +02:00
|
|
|
return DecoderCommand::STOP;
|
2008-08-26 08:27:05 +02:00
|
|
|
}
|
|
|
|
|
2013-09-27 12:11:37 +02:00
|
|
|
return DecoderCommand::NONE;
|
2008-08-26 08:27:05 +02:00
|
|
|
}
|
2008-11-02 17:02:28 +01:00
|
|
|
|
2013-09-27 12:11:37 +02:00
|
|
|
DecoderCommand
|
2013-10-23 22:08:59 +02:00
|
|
|
decoder_tag(Decoder &decoder, InputStream *is,
|
2013-07-31 00:34:22 +02:00
|
|
|
Tag &&tag)
|
2008-11-02 17:02:28 +01:00
|
|
|
{
|
2013-10-28 10:09:21 +01:00
|
|
|
gcc_unused const DecoderControl &dc = decoder.dc;
|
2013-09-27 12:11:37 +02:00
|
|
|
DecoderCommand cmd;
|
2008-11-03 18:24:01 +01:00
|
|
|
|
2013-10-19 18:48:38 +02:00
|
|
|
assert(dc.state == DecoderState::DECODE);
|
|
|
|
assert(dc.pipe != nullptr);
|
2009-01-03 23:29:45 +01:00
|
|
|
|
|
|
|
/* save the tag */
|
|
|
|
|
2013-10-21 21:12:37 +02:00
|
|
|
delete decoder.decoder_tag;
|
2015-06-20 16:06:01 +02:00
|
|
|
decoder.decoder_tag = new Tag(std::move(tag));
|
2009-01-03 23:29:45 +01:00
|
|
|
|
|
|
|
/* check for a new stream tag */
|
2008-11-10 15:02:26 +01:00
|
|
|
|
2009-01-03 23:29:45 +01:00
|
|
|
update_stream_tag(decoder, is);
|
2008-11-03 18:24:01 +01:00
|
|
|
|
2011-10-05 22:53:36 +02:00
|
|
|
/* check if we're seeking */
|
|
|
|
|
2011-10-06 00:35:45 +02:00
|
|
|
if (decoder_prepare_initial_seek(decoder))
|
2011-10-05 22:53:36 +02:00
|
|
|
/* during initial seek, no music chunk must be created
|
|
|
|
until seeking is finished; skip the rest of the
|
|
|
|
function here */
|
2013-09-27 12:11:37 +02:00
|
|
|
return DecoderCommand::SEEK;
|
2011-10-05 22:53:36 +02:00
|
|
|
|
2009-01-03 23:29:45 +01:00
|
|
|
/* send tag to music pipe */
|
2008-11-02 17:02:28 +01:00
|
|
|
|
2013-10-21 21:12:37 +02:00
|
|
|
if (decoder.stream_tag != nullptr) {
|
2009-01-03 23:29:45 +01:00
|
|
|
/* merge with tag from input stream */
|
2013-07-30 20:11:57 +02:00
|
|
|
Tag *merged;
|
2008-11-03 18:24:01 +01:00
|
|
|
|
2013-10-21 21:12:37 +02:00
|
|
|
merged = Tag::Merge(*decoder.stream_tag,
|
|
|
|
*decoder.decoder_tag);
|
2013-07-30 20:11:57 +02:00
|
|
|
cmd = do_send_tag(decoder, *merged);
|
|
|
|
delete merged;
|
2009-01-03 23:29:45 +01:00
|
|
|
} else
|
|
|
|
/* send only the decoder tag */
|
2013-10-21 21:12:37 +02:00
|
|
|
cmd = do_send_tag(decoder, *decoder.decoder_tag);
|
2008-11-03 18:24:01 +01:00
|
|
|
|
2009-01-03 23:28:51 +01:00
|
|
|
return cmd;
|
2008-11-02 17:02:28 +01:00
|
|
|
}
|
2010-01-03 22:44:23 +01:00
|
|
|
|
2013-01-05 02:05:50 +01:00
|
|
|
void
|
2013-10-21 21:12:37 +02:00
|
|
|
decoder_replay_gain(Decoder &decoder,
|
2013-10-25 19:09:22 +02:00
|
|
|
const ReplayGainInfo *replay_gain_info)
|
2010-01-03 22:44:23 +01:00
|
|
|
{
|
2013-10-19 18:19:03 +02:00
|
|
|
if (replay_gain_info != nullptr) {
|
2010-02-14 17:04:39 +01:00
|
|
|
static unsigned serial;
|
|
|
|
if (++serial == 0)
|
|
|
|
serial = 1;
|
|
|
|
|
2010-05-08 09:19:44 +02:00
|
|
|
if (REPLAY_GAIN_OFF != replay_gain_mode) {
|
2013-10-25 19:09:22 +02:00
|
|
|
ReplayGainMode rgm = replay_gain_mode;
|
2013-01-05 02:07:44 +01:00
|
|
|
if (rgm != REPLAY_GAIN_ALBUM)
|
|
|
|
rgm = REPLAY_GAIN_TRACK;
|
|
|
|
|
2013-10-25 19:05:49 +02:00
|
|
|
const auto &tuple = replay_gain_info->tuples[rgm];
|
|
|
|
const auto scale =
|
|
|
|
tuple.CalculateScale(replay_gain_preamp,
|
|
|
|
replay_gain_missing_preamp,
|
|
|
|
replay_gain_limit);
|
|
|
|
decoder.dc.replay_gain_db = 20.0 * log10f(scale);
|
2010-05-08 09:19:44 +02:00
|
|
|
}
|
|
|
|
|
2013-10-21 21:12:37 +02:00
|
|
|
decoder.replay_gain_info = *replay_gain_info;
|
|
|
|
decoder.replay_gain_serial = serial;
|
2010-02-14 17:04:39 +01:00
|
|
|
|
2013-10-21 21:12:37 +02:00
|
|
|
if (decoder.chunk != nullptr) {
|
2010-02-14 17:04:39 +01:00
|
|
|
/* flush the current chunk because the new
|
|
|
|
replay gain values affect the following
|
|
|
|
samples */
|
2013-11-13 19:13:47 +01:00
|
|
|
decoder.FlushChunk();
|
2010-02-14 17:04:39 +01:00
|
|
|
}
|
|
|
|
} else
|
2013-10-21 21:12:37 +02:00
|
|
|
decoder.replay_gain_serial = 0;
|
2010-01-03 22:44:23 +01:00
|
|
|
}
|
2010-03-21 18:21:47 +01:00
|
|
|
|
|
|
|
void
|
2013-10-26 14:19:34 +02:00
|
|
|
decoder_mixramp(Decoder &decoder, MixRampInfo &&mix_ramp)
|
2010-03-21 18:21:47 +01:00
|
|
|
{
|
2013-10-28 10:09:21 +01:00
|
|
|
DecoderControl &dc = decoder.dc;
|
2010-03-21 18:21:47 +01:00
|
|
|
|
2013-10-26 14:19:34 +02:00
|
|
|
dc.SetMixRamp(std::move(mix_ramp));
|
2010-03-21 18:21:47 +01:00
|
|
|
}
|