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"
|
2016-11-18 12:29:49 +01:00
|
|
|
#include "Bridge.hxx"
|
2014-01-07 21:39:47 +01:00
|
|
|
#include "DetachedSong.hxx"
|
2014-01-24 16:18:21 +01:00
|
|
|
#include "input/InputStream.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
|
2016-11-18 12:29:49 +01:00
|
|
|
DecoderBridge::Ready(const AudioFormat audio_format,
|
|
|
|
bool seekable, SignedSongTime duration)
|
2008-08-26 08:27:04 +02:00
|
|
|
{
|
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());
|
2016-11-18 07:59:01 +01:00
|
|
|
assert(convert == nullptr);
|
|
|
|
assert(stream_tag == nullptr);
|
|
|
|
assert(decoder_tag == nullptr);
|
|
|
|
assert(!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
|
|
|
|
2016-11-18 07:59:01 +01:00
|
|
|
convert = new PcmConvert();
|
2013-11-11 16:15:38 +01:00
|
|
|
|
2016-09-05 12:19:20 +02:00
|
|
|
try {
|
2016-11-18 07:59:01 +01:00
|
|
|
convert->Open(dc.in_audio_format,
|
2016-09-05 12:19:20 +02:00
|
|
|
dc.out_audio_format);
|
|
|
|
} catch (...) {
|
2016-11-18 07:59:01 +01:00
|
|
|
error = std::current_exception();
|
2016-09-05 12:19:20 +02:00
|
|
|
}
|
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
|
|
|
|
2016-11-21 21:44:57 +01:00
|
|
|
bool
|
|
|
|
DecoderBridge::PrepareInitialSeek()
|
2008-08-26 08:27:07 +02:00
|
|
|
{
|
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;
|
|
|
|
|
2016-11-21 21:44:57 +01:00
|
|
|
if (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
|
|
|
|
2016-11-21 21:44:57 +01:00
|
|
|
if (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 */
|
2016-11-21 21:44:57 +01:00
|
|
|
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 */
|
|
|
|
|
2016-11-21 21:44:57 +01:00
|
|
|
initial_seek_pending = false;
|
|
|
|
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) */
|
|
|
|
|
2016-11-21 21:44:57 +01:00
|
|
|
initial_seek_pending = false;
|
2011-09-21 23:17:34 +02:00
|
|
|
}
|
|
|
|
|
2011-10-06 00:25:44 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-11-21 21:44:57 +01:00
|
|
|
DecoderCommand
|
|
|
|
DecoderBridge::GetVirtualCommand()
|
2011-10-06 00:25:44 +02:00
|
|
|
{
|
2016-11-21 21:44:57 +01:00
|
|
|
if (error)
|
2013-11-13 20:57:09 +01:00
|
|
|
/* an error has occurred: stop the decoder plugin */
|
|
|
|
return DecoderCommand::STOP;
|
|
|
|
|
2013-10-19 18:48:38 +02:00
|
|
|
assert(dc.pipe != nullptr);
|
2011-10-06 00:25:44 +02:00
|
|
|
|
2016-11-21 21:44:57 +01:00
|
|
|
if (PrepareInitialSeek())
|
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
|
|
|
}
|
|
|
|
|
2016-11-21 21:44:57 +01:00
|
|
|
DecoderCommand
|
|
|
|
DecoderBridge::LockGetVirtualCommand()
|
2015-12-31 13:43:35 +01:00
|
|
|
{
|
2016-11-21 21:44:57 +01:00
|
|
|
const ScopeLock protect(dc.mutex);
|
|
|
|
return GetVirtualCommand();
|
2015-12-31 13:43:35 +01:00
|
|
|
}
|
|
|
|
|
2013-09-27 12:11:37 +02:00
|
|
|
DecoderCommand
|
2016-11-18 12:29:49 +01:00
|
|
|
DecoderBridge::GetCommand()
|
2011-09-21 23:17:34 +02:00
|
|
|
{
|
2016-11-21 21:44:57 +01:00
|
|
|
return LockGetVirtualCommand();
|
2011-09-21 23:17:34 +02:00
|
|
|
}
|
|
|
|
|
2009-12-25 19:47:33 +01:00
|
|
|
void
|
2016-11-18 12:29:49 +01:00
|
|
|
DecoderBridge::CommandFinished()
|
2008-08-26 08:27:07 +02:00
|
|
|
{
|
2015-12-31 13:43:35 +01:00
|
|
|
const ScopeLock protect(dc.mutex);
|
2008-08-26 08:27:07 +02:00
|
|
|
|
2016-11-18 08:15:07 +01:00
|
|
|
assert(dc.command != DecoderCommand::NONE || initial_seek_running);
|
2013-10-19 18:48:38 +02:00
|
|
|
assert(dc.command != DecoderCommand::SEEK ||
|
2016-11-18 08:15:07 +01:00
|
|
|
initial_seek_running ||
|
|
|
|
dc.seek_error || seeking);
|
2013-10-19 18:48:38 +02:00
|
|
|
assert(dc.pipe != nullptr);
|
2009-10-31 19:22:56 +01:00
|
|
|
|
2016-11-18 08:15:07 +01:00
|
|
|
if (initial_seek_running) {
|
|
|
|
assert(!seeking);
|
|
|
|
assert(current_chunk == nullptr);
|
2013-10-19 18:48:38 +02:00
|
|
|
assert(dc.pipe->IsEmpty());
|
2011-09-21 23:17:34 +02:00
|
|
|
|
2016-11-18 08:15:07 +01:00
|
|
|
initial_seek_running = false;
|
|
|
|
timestamp = dc.start_time.ToDoubleS();
|
2011-09-21 23:17:34 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-11-18 08:15:07 +01:00
|
|
|
if (seeking) {
|
|
|
|
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
|
|
|
|
2016-11-18 08:15:07 +01:00
|
|
|
if (current_chunk != nullptr) {
|
|
|
|
dc.buffer->Return(current_chunk);
|
|
|
|
current_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
|
|
|
|
2016-11-18 08:15:07 +01:00
|
|
|
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
|
2016-11-18 12:29:49 +01:00
|
|
|
DecoderBridge::GetSeekTime()
|
2008-08-26 08:27:07 +02:00
|
|
|
{
|
2013-10-19 18:48:38 +02:00
|
|
|
assert(dc.pipe != nullptr);
|
2008-08-26 08:27:07 +02:00
|
|
|
|
2016-11-18 08:15:07 +01:00
|
|
|
if (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
|
|
|
|
2016-11-18 08:15:07 +01:00
|
|
|
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
|
2016-11-18 12:29:49 +01:00
|
|
|
DecoderBridge::GetSeekFrame()
|
2014-08-26 11:27:41 +02:00
|
|
|
{
|
2016-11-18 08:15:07 +01:00
|
|
|
return GetSeekTime().ToScale<uint64_t>(dc.in_audio_format.sample_rate);
|
2014-08-26 11:27:41 +02:00
|
|
|
}
|
|
|
|
|
2016-11-18 07:13:35 +01:00
|
|
|
void
|
2016-11-18 12:29:49 +01:00
|
|
|
DecoderBridge::SeekError()
|
2008-08-26 08:27:07 +02:00
|
|
|
{
|
2013-10-19 18:48:38 +02:00
|
|
|
assert(dc.pipe != nullptr);
|
2008-08-26 08:27:07 +02:00
|
|
|
|
2016-11-18 08:15:07 +01:00
|
|
|
if (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. */
|
2016-11-18 08:15:07 +01:00
|
|
|
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;
|
2016-11-18 08:15:07 +01:00
|
|
|
seeking = false;
|
2009-12-26 01:10:23 +01:00
|
|
|
|
2016-11-18 08:15:07 +01:00
|
|
|
CommandFinished();
|
2008-08-26 08:27:07 +02:00
|
|
|
}
|
|
|
|
|
2016-02-21 08:03:32 +01:00
|
|
|
InputStreamPtr
|
2016-11-18 12:29:49 +01:00
|
|
|
DecoderBridge::OpenUri(const char *uri)
|
2014-05-22 11:10:41 +02:00
|
|
|
{
|
2016-11-18 12:34:04 +01:00
|
|
|
assert(dc.state == DecoderState::START ||
|
|
|
|
dc.state == DecoderState::DECODE);
|
2014-05-22 11:10:41 +02:00
|
|
|
|
|
|
|
Mutex &mutex = dc.mutex;
|
|
|
|
Cond &cond = dc.cond;
|
|
|
|
|
2016-09-09 15:37:06 +02:00
|
|
|
auto is = InputStream::Open(uri, mutex, cond);
|
2014-05-22 11:10:41 +02:00
|
|
|
|
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)
|
2016-11-17 22:48:18 +01:00
|
|
|
throw StopDecoder();
|
2014-05-22 11:10:41 +02:00
|
|
|
|
|
|
|
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
|
2016-11-18 12:29:49 +01:00
|
|
|
decoder_check_cancel_read(const DecoderBridge *bridge)
|
2011-09-16 07:28:27 +02:00
|
|
|
{
|
2016-11-21 22:07:03 +01:00
|
|
|
return bridge != nullptr && bridge->CheckCancelRead();
|
2011-09-16 07:28:27 +02:00
|
|
|
}
|
|
|
|
|
2013-10-21 21:12:37 +02:00
|
|
|
size_t
|
2016-11-18 07:13:35 +01:00
|
|
|
decoder_read(DecoderClient *client,
|
2013-10-23 22:08:59 +02:00
|
|
|
InputStream &is,
|
2013-10-21 21:12:37 +02:00
|
|
|
void *buffer, size_t length)
|
2016-09-16 17:34:48 +02:00
|
|
|
try {
|
2013-10-19 18:19:03 +02:00
|
|
|
/* XXX don't allow decoder==nullptr */
|
2016-11-18 12:29:49 +01:00
|
|
|
auto *bridge = (DecoderBridge *)client;
|
2008-08-26 08:27:14 +02:00
|
|
|
|
2016-11-18 12:29:49 +01:00
|
|
|
assert(bridge == nullptr ||
|
|
|
|
bridge->dc.state == DecoderState::START ||
|
|
|
|
bridge->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-11-18 12:29:49 +01:00
|
|
|
if (decoder_check_cancel_read(bridge))
|
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
|
|
|
|
2016-09-09 18:47:42 +02:00
|
|
|
size_t nbytes = is.Read(buffer, length);
|
|
|
|
assert(nbytes > 0 || is.IsEOF());
|
2011-09-16 08:04:02 +02:00
|
|
|
|
|
|
|
return nbytes;
|
2016-09-16 17:34:48 +02:00
|
|
|
} catch (const std::runtime_error &e) {
|
2016-11-18 12:29:49 +01:00
|
|
|
auto *bridge = (DecoderBridge *)client;
|
|
|
|
if (bridge != nullptr)
|
|
|
|
bridge->error = std::current_exception();
|
2016-11-17 22:52:44 +01:00
|
|
|
else
|
|
|
|
LogError(e);
|
2016-09-16 17:34:48 +02:00
|
|
|
return 0;
|
2008-08-26 08:27:14 +02:00
|
|
|
}
|
|
|
|
|
2013-12-14 12:43:06 +01:00
|
|
|
bool
|
2016-11-18 07:13:35 +01:00
|
|
|
decoder_read_full(DecoderClient *client, InputStream &is,
|
2013-12-14 12:43:06 +01:00
|
|
|
void *_buffer, size_t size)
|
|
|
|
{
|
|
|
|
uint8_t *buffer = (uint8_t *)_buffer;
|
|
|
|
|
|
|
|
while (size > 0) {
|
2016-11-18 07:13:35 +01:00
|
|
|
size_t nbytes = decoder_read(client, is, buffer, size);
|
2013-12-14 12:43:06 +01:00
|
|
|
if (nbytes == 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
buffer += nbytes;
|
|
|
|
size -= nbytes;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-12-14 12:21:23 +01:00
|
|
|
bool
|
2016-11-18 07:13:35 +01:00
|
|
|
decoder_skip(DecoderClient *client, InputStream &is, size_t size)
|
2013-12-14 12:21:23 +01:00
|
|
|
{
|
|
|
|
while (size > 0) {
|
|
|
|
char buffer[1024];
|
2016-11-18 07:13:35 +01:00
|
|
|
size_t nbytes = decoder_read(client, is, buffer,
|
2013-12-14 12:21:23 +01:00
|
|
|
std::min(sizeof(buffer), size));
|
|
|
|
if (nbytes == 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
size -= nbytes;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-12-25 19:47:33 +01:00
|
|
|
void
|
2016-11-18 12:29:49 +01:00
|
|
|
DecoderBridge::SubmitTimestamp(double t)
|
2009-12-25 19:47:33 +01:00
|
|
|
{
|
|
|
|
assert(t >= 0);
|
|
|
|
|
2016-11-18 08:27:30 +01:00
|
|
|
timestamp = t;
|
2009-12-25 19:47:33 +01:00
|
|
|
}
|
|
|
|
|
2016-11-21 21:44:57 +01:00
|
|
|
DecoderCommand
|
|
|
|
DecoderBridge::DoSendTag(const Tag &tag)
|
2008-08-26 08:27:05 +02:00
|
|
|
{
|
2016-11-21 21:44:57 +01:00
|
|
|
if (current_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 */
|
2016-11-21 21:44:57 +01:00
|
|
|
FlushChunk();
|
2009-01-03 23:28:51 +01:00
|
|
|
}
|
|
|
|
|
2016-11-21 21:44:57 +01:00
|
|
|
assert(current_chunk == nullptr);
|
2009-03-06 00:42:01 +01:00
|
|
|
|
2016-11-21 21:44:57 +01:00
|
|
|
auto *chunk = GetChunk();
|
2013-10-19 18:19:03 +02:00
|
|
|
if (chunk == nullptr) {
|
2016-11-21 21:44:57 +01:00
|
|
|
assert(dc.command != DecoderCommand::NONE);
|
|
|
|
return 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
|
|
|
}
|
|
|
|
|
2016-11-21 21:44:57 +01:00
|
|
|
bool
|
|
|
|
DecoderBridge::UpdateStreamTag(InputStream *is)
|
2009-01-03 23:29:45 +01:00
|
|
|
{
|
2016-11-18 12:29:49 +01:00
|
|
|
auto *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) {
|
2016-11-21 21:44:57 +01:00
|
|
|
tag = 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 */
|
2016-11-21 21:44:57 +01:00
|
|
|
delete song_tag;
|
2015-06-21 15:09:50 +02:00
|
|
|
|
2016-11-21 21:44:57 +01:00
|
|
|
song_tag = nullptr;
|
2009-01-03 23:29:45 +01:00
|
|
|
|
2016-11-21 21:44:57 +01:00
|
|
|
delete stream_tag;
|
|
|
|
stream_tag = tag;
|
2009-01-03 23:29:45 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-09-27 12:11:37 +02:00
|
|
|
DecoderCommand
|
2016-11-18 12:29:49 +01:00
|
|
|
DecoderBridge::SubmitData(InputStream *is,
|
|
|
|
const void *data, size_t length,
|
|
|
|
uint16_t kbit_rate)
|
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
|
|
|
|
2016-11-21 21:44:57 +01:00
|
|
|
DecoderCommand cmd = LockGetVirtualCommand();
|
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
|
|
|
|
2016-11-18 08:27:30 +01:00
|
|
|
assert(!initial_seek_pending);
|
|
|
|
assert(!initial_seek_running);
|
2014-09-07 21:50:27 +02:00
|
|
|
|
2009-01-03 23:29:45 +01:00
|
|
|
/* send stream tags */
|
|
|
|
|
2016-11-21 21:44:57 +01:00
|
|
|
if (UpdateStreamTag(is)) {
|
2016-11-18 08:27:30 +01:00
|
|
|
if (decoder_tag != nullptr) {
|
2009-01-03 23:29:45 +01:00
|
|
|
/* merge with tag from decoder plugin */
|
2016-11-18 08:27:30 +01:00
|
|
|
Tag *tag = Tag::Merge(*decoder_tag,
|
|
|
|
*stream_tag);
|
2016-11-21 21:44:57 +01:00
|
|
|
cmd = DoSendTag(*tag);
|
2013-07-30 20:11:57 +02:00
|
|
|
delete tag;
|
2009-01-03 23:29:45 +01:00
|
|
|
} else
|
|
|
|
/* send only the stream tag */
|
2016-11-21 21:44:57 +01:00
|
|
|
cmd = DoSendTag(*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
|
|
|
}
|
|
|
|
|
2016-11-18 08:27:30 +01:00
|
|
|
if (convert != nullptr) {
|
2013-11-13 19:10:43 +01:00
|
|
|
assert(dc.in_audio_format != dc.out_audio_format);
|
|
|
|
|
2016-09-05 12:19:20 +02:00
|
|
|
try {
|
2016-11-18 08:27:30 +01:00
|
|
|
auto result = convert->Convert({data, length});
|
2016-09-05 12:19:20 +02:00
|
|
|
data = result.data;
|
|
|
|
length = result.size;
|
|
|
|
} catch (const std::runtime_error &e) {
|
2009-07-23 12:01:03 +02:00
|
|
|
/* the PCM conversion has failed - stop
|
|
|
|
playback, since we have no better way to
|
|
|
|
bail out */
|
2016-11-18 08:27:30 +01:00
|
|
|
error = std::current_exception();
|
2013-09-27 12:11:37 +02:00
|
|
|
return DecoderCommand::STOP;
|
2009-07-23 12:01:03 +02:00
|
|
|
}
|
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) {
|
2009-03-06 00:42:01 +01:00
|
|
|
bool full;
|
|
|
|
|
2016-11-18 08:27:30 +01:00
|
|
|
auto *chunk = 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,
|
2016-11-18 08:27:30 +01:00
|
|
|
SongTime::FromS(timestamp) -
|
2014-08-29 13:15:33 +02:00
|
|
|
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 */
|
2016-11-18 08:27:30 +01:00
|
|
|
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 */
|
2016-11-18 08:27:30 +01:00
|
|
|
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
|
|
|
|
2016-11-18 08:27:30 +01:00
|
|
|
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() &&
|
2016-11-18 08:27:30 +01:00
|
|
|
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
|
2016-11-18 12:29:49 +01:00
|
|
|
DecoderBridge::SubmitTag(InputStream *is, Tag &&tag)
|
2008-11-02 17:02:28 +01:00
|
|
|
{
|
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 */
|
|
|
|
|
2016-11-18 08:27:30 +01:00
|
|
|
delete decoder_tag;
|
|
|
|
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
|
|
|
|
2016-11-21 21:44:57 +01:00
|
|
|
UpdateStreamTag(is);
|
2008-11-03 18:24:01 +01:00
|
|
|
|
2011-10-05 22:53:36 +02:00
|
|
|
/* check if we're seeking */
|
|
|
|
|
2016-11-21 21:44:57 +01:00
|
|
|
if (PrepareInitialSeek())
|
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
|
|
|
|
2016-11-18 08:27:30 +01:00
|
|
|
if (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
|
|
|
|
2016-11-18 08:27:30 +01:00
|
|
|
merged = Tag::Merge(*stream_tag, *decoder_tag);
|
2016-11-21 21:44:57 +01:00
|
|
|
cmd = DoSendTag(*merged);
|
2013-07-30 20:11:57 +02:00
|
|
|
delete merged;
|
2009-01-03 23:29:45 +01:00
|
|
|
} else
|
|
|
|
/* send only the decoder tag */
|
2016-11-21 21:44:57 +01:00
|
|
|
cmd = DoSendTag(*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
|
2016-11-18 12:29:49 +01:00
|
|
|
DecoderBridge::SubmitReplayGain(const ReplayGainInfo *new_replay_gain_info)
|
2010-01-03 22:44:23 +01:00
|
|
|
{
|
2016-11-18 08:27:30 +01:00
|
|
|
if (new_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;
|
|
|
|
|
2016-11-18 08:27:30 +01:00
|
|
|
const auto &tuple = new_replay_gain_info->tuples[rgm];
|
2013-10-25 19:05:49 +02:00
|
|
|
const auto scale =
|
|
|
|
tuple.CalculateScale(replay_gain_preamp,
|
|
|
|
replay_gain_missing_preamp,
|
|
|
|
replay_gain_limit);
|
2016-11-18 08:27:30 +01:00
|
|
|
dc.replay_gain_db = 20.0 * log10f(scale);
|
2010-05-08 09:19:44 +02:00
|
|
|
}
|
|
|
|
|
2016-11-18 08:27:30 +01:00
|
|
|
replay_gain_info = *new_replay_gain_info;
|
|
|
|
replay_gain_serial = serial;
|
2010-02-14 17:04:39 +01:00
|
|
|
|
2016-11-18 08:27:30 +01:00
|
|
|
if (current_chunk != nullptr) {
|
2010-02-14 17:04:39 +01:00
|
|
|
/* flush the current chunk because the new
|
|
|
|
replay gain values affect the following
|
|
|
|
samples */
|
2016-11-18 08:27:30 +01:00
|
|
|
FlushChunk();
|
2010-02-14 17:04:39 +01:00
|
|
|
}
|
|
|
|
} else
|
2016-11-18 08:27:30 +01:00
|
|
|
replay_gain_serial = 0;
|
2010-01-03 22:44:23 +01:00
|
|
|
}
|
2010-03-21 18:21:47 +01:00
|
|
|
|
|
|
|
void
|
2016-11-18 12:29:49 +01:00
|
|
|
DecoderBridge::SubmitMixRamp(MixRampInfo &&mix_ramp)
|
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
|
|
|
}
|