2009-03-13 18:43:16 +01:00
|
|
|
/*
|
2013-01-04 08:41:16 +01:00
|
|
|
* Copyright (C) 2003-2013 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-01-30 21:47:12 +01:00
|
|
|
#include "AudioConfig.hxx"
|
2013-01-07 09:38:02 +01:00
|
|
|
#include "replay_gain_config.h"
|
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"
|
2013-07-28 13:25:12 +02:00
|
|
|
#include "Song.hxx"
|
2013-01-24 19:14:40 +01:00
|
|
|
#include "InputStream.hxx"
|
2013-08-10 18:02:44 +02:00
|
|
|
#include "util/Error.hxx"
|
2008-08-26 08:27:04 +02:00
|
|
|
|
2008-11-24 14:32:53 +01:00
|
|
|
#include <glib.h>
|
2008-10-08 10:49:29 +02:00
|
|
|
|
2009-01-03 14:51:28 +01:00
|
|
|
#include <assert.h>
|
|
|
|
#include <stdlib.h>
|
2013-07-30 20:11:57 +02:00
|
|
|
#include <string.h>
|
2009-01-03 14:51:28 +01:00
|
|
|
|
2009-03-01 10:31:47 +01:00
|
|
|
#undef G_LOG_DOMAIN
|
|
|
|
#define G_LOG_DOMAIN "decoder"
|
|
|
|
|
2009-10-31 19:22:56 +01:00
|
|
|
void
|
|
|
|
decoder_initialized(struct decoder *decoder,
|
2013-08-03 21:00:50 +02:00
|
|
|
const AudioFormat audio_format,
|
2009-10-31 19:22:56 +01:00
|
|
|
bool seekable, float total_time)
|
2008-08-26 08:27:04 +02:00
|
|
|
{
|
2009-10-31 19:22:56 +01:00
|
|
|
struct decoder_control *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
|
|
|
|
|
|
|
assert(dc->state == DECODE_STATE_START);
|
|
|
|
assert(dc->pipe != NULL);
|
2008-11-10 15:02:26 +01:00
|
|
|
assert(decoder != NULL);
|
2009-01-03 23:29:45 +01:00
|
|
|
assert(decoder->stream_tag == NULL);
|
|
|
|
assert(decoder->decoder_tag == NULL);
|
2008-11-10 15:02:26 +01:00
|
|
|
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-08-03 21:00:50 +02:00
|
|
|
dc->in_audio_format = audio_format;
|
|
|
|
dc->out_audio_format = getOutputAudioFormat(audio_format);
|
2008-08-26 08:27:05 +02:00
|
|
|
|
2009-10-31 19:22:56 +01:00
|
|
|
dc->seekable = seekable;
|
|
|
|
dc->total_time = total_time;
|
2008-08-26 08:27:05 +02:00
|
|
|
|
2013-01-21 10:13:29 +01:00
|
|
|
dc->Lock();
|
2009-10-31 19:22:56 +01:00
|
|
|
dc->state = DECODE_STATE_DECODE;
|
2013-01-27 17:20:50 +01:00
|
|
|
dc->client_cond.signal();
|
2013-01-21 10:13:29 +01:00
|
|
|
dc->Unlock();
|
2009-08-13 23:33:46 +02:00
|
|
|
|
2009-11-10 17:57:14 +01:00
|
|
|
g_debug("audio_format=%s, seekable=%s",
|
2013-08-03 21:00:50 +02:00
|
|
|
audio_format_to_string(dc->in_audio_format, &af_string),
|
2009-03-01 10:31:47 +01:00
|
|
|
seekable ? "true" : "false");
|
|
|
|
|
2013-08-03 21:00:50 +02:00
|
|
|
if (dc->in_audio_format != dc->out_audio_format)
|
2009-11-10 17:57:14 +01:00
|
|
|
g_debug("converting to %s",
|
2013-08-03 21:00:50 +02:00
|
|
|
audio_format_to_string(dc->out_audio_format,
|
2009-11-10 17:57:14 +01:00
|
|
|
&af_string));
|
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
|
|
|
|
decoder_prepare_initial_seek(struct decoder *decoder)
|
2008-08-26 08:27:07 +02:00
|
|
|
{
|
2009-10-31 19:22:56 +01:00
|
|
|
const struct decoder_control *dc = decoder->dc;
|
|
|
|
assert(dc->pipe != NULL);
|
2009-04-25 15:07:22 +02:00
|
|
|
|
2012-02-13 18:27:43 +01:00
|
|
|
if (dc->state != DECODE_STATE_DECODE)
|
|
|
|
/* wait until the decoder has finished initialisation
|
|
|
|
(reading file headers etc.) before emitting the
|
|
|
|
virtual "SEEK" command */
|
|
|
|
return false;
|
|
|
|
|
2011-09-21 23:17:34 +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
|
|
|
|
|
|
|
if (decoder->initial_seek_pending) {
|
2011-11-27 19:19:43 +01:00
|
|
|
if (!dc->seekable) {
|
|
|
|
/* seeking is not possible */
|
|
|
|
decoder->initial_seek_pending = false;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-09-21 23:17:34 +02:00
|
|
|
if (dc->command == DECODE_COMMAND_NONE) {
|
2011-10-06 00:25:44 +02:00
|
|
|
/* begin initial seek */
|
|
|
|
|
2011-09-21 23:17:34 +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) */
|
|
|
|
|
2011-09-21 23:17:34 +02:00
|
|
|
decoder->initial_seek_pending = false;
|
|
|
|
}
|
|
|
|
|
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
|
2011-10-06 00:25:44 +02:00
|
|
|
static enum decoder_command
|
|
|
|
decoder_get_virtual_command(struct decoder *decoder)
|
|
|
|
{
|
|
|
|
const struct decoder_control *dc = decoder->dc;
|
|
|
|
assert(dc->pipe != NULL);
|
|
|
|
|
|
|
|
if (decoder_prepare_initial_seek(decoder))
|
|
|
|
return DECODE_COMMAND_SEEK;
|
|
|
|
|
2009-10-31 19:22:56 +01:00
|
|
|
return dc->command;
|
2008-08-26 08:27:07 +02:00
|
|
|
}
|
|
|
|
|
2011-09-21 23:17:34 +02:00
|
|
|
enum decoder_command
|
|
|
|
decoder_get_command(struct decoder *decoder)
|
|
|
|
{
|
|
|
|
return decoder_get_virtual_command(decoder);
|
|
|
|
}
|
|
|
|
|
2009-12-25 19:47:33 +01:00
|
|
|
void
|
|
|
|
decoder_command_finished(struct decoder *decoder)
|
2008-08-26 08:27:07 +02:00
|
|
|
{
|
2009-10-31 19:22:56 +01:00
|
|
|
struct decoder_control *dc = decoder->dc;
|
2009-08-13 23:33:46 +02:00
|
|
|
|
2013-01-21 10:13:29 +01:00
|
|
|
dc->Lock();
|
2008-08-26 08:27:07 +02:00
|
|
|
|
2011-09-21 23:17:34 +02:00
|
|
|
assert(dc->command != DECODE_COMMAND_NONE ||
|
|
|
|
decoder->initial_seek_running);
|
2009-10-31 19:22:56 +01:00
|
|
|
assert(dc->command != DECODE_COMMAND_SEEK ||
|
2011-09-21 23:17:34 +02:00
|
|
|
decoder->initial_seek_running ||
|
2009-10-31 19:22:56 +01:00
|
|
|
dc->seek_error || decoder->seeking);
|
|
|
|
assert(dc->pipe != NULL);
|
|
|
|
|
2011-09-21 23:17:34 +02:00
|
|
|
if (decoder->initial_seek_running) {
|
|
|
|
assert(!decoder->seeking);
|
|
|
|
assert(decoder->chunk == NULL);
|
2013-09-26 21:51:45 +02:00
|
|
|
assert(dc->pipe->IsEmpty());
|
2011-09-21 23:17:34 +02:00
|
|
|
|
|
|
|
decoder->initial_seek_running = false;
|
2011-10-05 22:37:59 +02:00
|
|
|
decoder->timestamp = dc->start_ms / 1000.;
|
2013-01-21 10:13:29 +01:00
|
|
|
dc->Unlock();
|
2011-09-21 23:17:34 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-12-26 01:10:23 +01:00
|
|
|
if (decoder->seeking) {
|
|
|
|
decoder->seeking = false;
|
|
|
|
|
2008-10-29 17:28:47 +01:00
|
|
|
/* delete frames from the old song position */
|
2009-03-06 00:42:01 +01:00
|
|
|
|
|
|
|
if (decoder->chunk != NULL) {
|
2013-09-26 22:09:42 +02:00
|
|
|
dc->buffer->Return(decoder->chunk);
|
2009-03-06 00:42:01 +01:00
|
|
|
decoder->chunk = NULL;
|
|
|
|
}
|
|
|
|
|
2013-09-26 22:09:42 +02:00
|
|
|
dc->pipe->Clear(*dc->buffer);
|
2009-12-25 19:47:33 +01:00
|
|
|
|
|
|
|
decoder->timestamp = dc->seek_where;
|
2009-03-06 00:42:01 +01:00
|
|
|
}
|
2008-10-29 17:28:47 +01:00
|
|
|
|
2009-10-31 19:22:56 +01:00
|
|
|
dc->command = DECODE_COMMAND_NONE;
|
2013-01-27 17:20:50 +01:00
|
|
|
dc->client_cond.signal();
|
2013-01-21 10:13:29 +01:00
|
|
|
dc->Unlock();
|
2008-08-26 08:27:07 +02:00
|
|
|
}
|
|
|
|
|
2013-08-04 23:48:01 +02:00
|
|
|
double decoder_seek_where(gcc_unused struct decoder * decoder)
|
2008-08-26 08:27:07 +02:00
|
|
|
{
|
2009-10-31 19:22:56 +01:00
|
|
|
const struct decoder_control *dc = decoder->dc;
|
|
|
|
|
|
|
|
assert(dc->pipe != NULL);
|
2008-08-26 08:27:07 +02:00
|
|
|
|
2011-09-21 23:17:34 +02:00
|
|
|
if (decoder->initial_seek_running)
|
2011-10-05 22:37:59 +02:00
|
|
|
return dc->start_ms / 1000.;
|
2011-09-21 23:17:34 +02:00
|
|
|
|
|
|
|
assert(dc->command == DECODE_COMMAND_SEEK);
|
|
|
|
|
2008-10-08 11:03:39 +02:00
|
|
|
decoder->seeking = true;
|
2008-08-26 08:27:14 +02:00
|
|
|
|
2009-10-31 19:22:56 +01:00
|
|
|
return dc->seek_where;
|
2008-08-26 08:27:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void decoder_seek_error(struct decoder * decoder)
|
|
|
|
{
|
2009-10-31 19:22:56 +01:00
|
|
|
struct decoder_control *dc = decoder->dc;
|
|
|
|
|
|
|
|
assert(dc->pipe != NULL);
|
2008-08-26 08:27:07 +02:00
|
|
|
|
2011-10-04 22:07:01 +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. */
|
2011-10-04 22:07:01 +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
|
|
|
|
|
|
|
assert(dc->command == DECODE_COMMAND_SEEK);
|
|
|
|
|
2009-10-31 19:22:56 +01:00
|
|
|
dc->seek_error = true;
|
2009-12-26 01:10:23 +01:00
|
|
|
decoder->seeking = false;
|
|
|
|
|
2008-08-26 08:27:07 +02:00
|
|
|
decoder_command_finished(decoder);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
decoder_check_cancel_read(const struct decoder *decoder)
|
|
|
|
{
|
|
|
|
if (decoder == NULL)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
const struct decoder_control *dc = decoder->dc;
|
|
|
|
if (dc->command == DECODE_COMMAND_NONE)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/* ignore the SEEK command during initialization, the plugin
|
|
|
|
should handle that after it has initialized successfully */
|
|
|
|
if (dc->command == DECODE_COMMAND_SEEK &&
|
|
|
|
(dc->state == DECODE_STATE_START || decoder->seeking))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2008-08-26 08:27:14 +02:00
|
|
|
size_t decoder_read(struct decoder *decoder,
|
2008-11-03 17:56:41 +01:00
|
|
|
struct input_stream *is,
|
2008-08-26 08:27:14 +02:00
|
|
|
void *buffer, size_t length)
|
|
|
|
{
|
2011-09-16 07:28:27 +02:00
|
|
|
/* XXX don't allow decoder==NULL */
|
2008-08-26 08:27:14 +02:00
|
|
|
|
2008-11-11 15:55:28 +01:00
|
|
|
assert(decoder == NULL ||
|
2011-09-16 07:28:27 +02:00
|
|
|
decoder->dc->state == DECODE_STATE_START ||
|
|
|
|
decoder->dc->state == DECODE_STATE_DECODE);
|
2008-11-03 17:56:41 +01:00
|
|
|
assert(is != NULL);
|
2008-08-26 08:27:14 +02:00
|
|
|
assert(buffer != NULL);
|
|
|
|
|
2008-11-15 19:27:30 +01:00
|
|
|
if (length == 0)
|
|
|
|
return 0;
|
|
|
|
|
2013-09-05 00:06:31 +02:00
|
|
|
is->Lock();
|
2011-09-14 21:46:41 +02:00
|
|
|
|
|
|
|
while (true) {
|
|
|
|
if (decoder_check_cancel_read(decoder)) {
|
2013-09-05 00:06:31 +02:00
|
|
|
is->Unlock();
|
2011-09-14 21:46:41 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-09-05 00:06:31 +02:00
|
|
|
if (is->IsAvailable())
|
2011-09-14 21:46:41 +02:00
|
|
|
break;
|
|
|
|
|
2013-01-28 23:35:01 +01: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-09-05 00:06:31 +02:00
|
|
|
size_t nbytes = is->Read(buffer, length, error);
|
2013-08-10 18:02:44 +02:00
|
|
|
assert(nbytes == 0 || !error.IsDefined());
|
2013-09-05 00:06:31 +02:00
|
|
|
assert(nbytes > 0 || error.IsDefined() || is->IsEOF());
|
2008-08-26 08:27:14 +02:00
|
|
|
|
2013-08-10 18:02:44 +02:00
|
|
|
if (gcc_unlikely(nbytes == 0 && error.IsDefined()))
|
|
|
|
g_warning("%s", error.GetMessage());
|
2011-09-16 08:04:02 +02:00
|
|
|
|
2013-09-05 00:06:31 +02:00
|
|
|
is->Unlock();
|
2011-09-14 21:46:41 +02:00
|
|
|
|
2011-09-16 08:04:02 +02:00
|
|
|
return nbytes;
|
2008-08-26 08:27:14 +02:00
|
|
|
}
|
|
|
|
|
2009-12-25 19:47:33 +01:00
|
|
|
void
|
|
|
|
decoder_timestamp(struct decoder *decoder, double t)
|
|
|
|
{
|
|
|
|
assert(decoder != NULL);
|
|
|
|
assert(t >= 0);
|
|
|
|
|
|
|
|
decoder->timestamp = t;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
*/
|
2008-08-26 08:27:07 +02:00
|
|
|
static enum decoder_command
|
2013-07-30 20:11:57 +02:00
|
|
|
do_send_tag(struct decoder *decoder, const Tag &tag)
|
2008-08-26 08:27:05 +02:00
|
|
|
{
|
2009-03-06 00:42:01 +01:00
|
|
|
struct music_chunk *chunk;
|
2008-08-26 08:27:05 +02:00
|
|
|
|
2009-03-06 00:42:01 +01:00
|
|
|
if (decoder->chunk != NULL) {
|
|
|
|
/* there is a partial chunk - flush it, we want the
|
|
|
|
tag in a new chunk */
|
2009-03-06 00:42:03 +01:00
|
|
|
decoder_flush_chunk(decoder);
|
2013-01-27 17:20:50 +01:00
|
|
|
decoder->dc->client_cond.signal();
|
2009-01-03 23:28:51 +01:00
|
|
|
}
|
|
|
|
|
2009-03-06 00:42:01 +01:00
|
|
|
assert(decoder->chunk == NULL);
|
|
|
|
|
2011-09-14 09:37:52 +02:00
|
|
|
chunk = decoder_get_chunk(decoder);
|
2009-03-06 00:42:03 +01:00
|
|
|
if (chunk == NULL) {
|
2009-10-31 19:22:56 +01:00
|
|
|
assert(decoder->dc->command != DECODE_COMMAND_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);
|
2009-01-03 23:28:51 +01:00
|
|
|
return DECODE_COMMAND_NONE;
|
|
|
|
}
|
|
|
|
|
2009-01-03 23:29:45 +01:00
|
|
|
static bool
|
|
|
|
update_stream_tag(struct decoder *decoder, struct input_stream *is)
|
|
|
|
{
|
2013-07-30 20:11:57 +02:00
|
|
|
Tag *tag;
|
2009-01-03 23:29:45 +01:00
|
|
|
|
2009-04-13 19:25:53 +02:00
|
|
|
tag = is != NULL
|
2013-09-05 00:06:31 +02:00
|
|
|
? is->LockReadTag()
|
2009-04-13 19:25:53 +02:00
|
|
|
: NULL;
|
|
|
|
if (tag == NULL) {
|
|
|
|
tag = decoder->song_tag;
|
|
|
|
if (tag == NULL)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/* no stream tag present - submit the song tag
|
|
|
|
instead */
|
|
|
|
decoder->song_tag = NULL;
|
|
|
|
}
|
2009-01-03 23:29:45 +01:00
|
|
|
|
2013-07-30 20:11:57 +02:00
|
|
|
delete decoder->stream_tag;
|
2009-01-03 23:29:45 +01:00
|
|
|
decoder->stream_tag = tag;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2008-08-26 08:27:07 +02:00
|
|
|
enum decoder_command
|
2008-10-17 17:53:48 +02:00
|
|
|
decoder_data(struct decoder *decoder,
|
2008-11-03 17:56:41 +01:00
|
|
|
struct input_stream *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
|
|
|
{
|
2009-10-31 19:22:56 +01:00
|
|
|
struct decoder_control *dc = decoder->dc;
|
2009-08-13 23:33:46 +02:00
|
|
|
enum decoder_command cmd;
|
2008-08-26 08:27:05 +02:00
|
|
|
|
2009-10-31 19:22:56 +01:00
|
|
|
assert(dc->state == DECODE_STATE_DECODE);
|
|
|
|
assert(dc->pipe != NULL);
|
2013-08-03 21:00:50 +02:00
|
|
|
assert(length % dc->in_audio_format.GetFrameSize() == 0);
|
2008-11-10 15:02:26 +01:00
|
|
|
|
2013-01-21 10:13:29 +01:00
|
|
|
dc->Lock();
|
2011-09-21 23:17:34 +02:00
|
|
|
cmd = decoder_get_virtual_command(decoder);
|
2013-01-21 10:13:29 +01:00
|
|
|
dc->Unlock();
|
2009-08-13 23:33:46 +02:00
|
|
|
|
|
|
|
if (cmd == DECODE_COMMAND_STOP || cmd == DECODE_COMMAND_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
|
|
|
|
2009-01-03 23:29:45 +01:00
|
|
|
/* send stream tags */
|
|
|
|
|
|
|
|
if (update_stream_tag(decoder, is)) {
|
|
|
|
if (decoder->decoder_tag != NULL) {
|
|
|
|
/* merge with tag from decoder plugin */
|
2013-07-30 20:11:57 +02:00
|
|
|
Tag *tag = Tag::Merge(*decoder->decoder_tag,
|
|
|
|
*decoder->stream_tag);
|
|
|
|
cmd = do_send_tag(decoder, *tag);
|
|
|
|
delete tag;
|
2009-01-03 23:29:45 +01:00
|
|
|
} else
|
|
|
|
/* send only the stream tag */
|
2013-07-30 20:11:57 +02:00
|
|
|
cmd = do_send_tag(decoder, *decoder->stream_tag);
|
2009-01-03 23:29:45 +01:00
|
|
|
|
|
|
|
if (cmd != DECODE_COMMAND_NONE)
|
|
|
|
return cmd;
|
2008-11-03 18:24:01 +01:00
|
|
|
}
|
|
|
|
|
2013-08-03 21:00:50 +02:00
|
|
|
if (dc->in_audio_format != dc->out_audio_format) {
|
2013-08-10 18:02:44 +02:00
|
|
|
Error error;
|
2013-08-03 21:00:50 +02:00
|
|
|
data = decoder->conv_state.Convert(dc->in_audio_format,
|
2013-01-30 23:40:56 +01:00
|
|
|
data, length,
|
2013-08-03 21:00:50 +02:00
|
|
|
dc->out_audio_format,
|
2013-01-30 23:40:56 +01:00
|
|
|
&length,
|
2013-08-10 18:02:44 +02:00
|
|
|
error);
|
2009-07-23 12:01:03 +02:00
|
|
|
if (data == NULL) {
|
|
|
|
/* the PCM conversion has failed - stop
|
|
|
|
playback, since we have no better way to
|
|
|
|
bail out */
|
2013-08-10 18:02:44 +02:00
|
|
|
g_warning("%s", error.GetMessage());
|
2009-07-23 12:01:03 +02:00
|
|
|
return DECODE_COMMAND_STOP;
|
|
|
|
}
|
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
|
|
|
struct music_chunk *chunk;
|
2009-01-17 13:11:10 +01:00
|
|
|
size_t nbytes;
|
2009-03-06 00:42:01 +01:00
|
|
|
bool full;
|
|
|
|
|
2011-09-14 09:37:52 +02:00
|
|
|
chunk = decoder_get_chunk(decoder);
|
2009-03-06 00:42:03 +01:00
|
|
|
if (chunk == NULL) {
|
2009-10-31 19:22:56 +01:00
|
|
|
assert(dc->command != DECODE_COMMAND_NONE);
|
|
|
|
return dc->command;
|
2009-03-06 00:42:03 +01:00
|
|
|
}
|
|
|
|
|
2013-01-04 21:38:46 +01:00
|
|
|
void *dest = chunk->Write(dc->out_audio_format,
|
|
|
|
decoder->timestamp -
|
|
|
|
dc->song->start_ms / 1000.0,
|
|
|
|
kbit_rate, &nbytes);
|
2009-01-17 13:11:10 +01:00
|
|
|
if (dest == NULL) {
|
2009-03-06 00:42:01 +01:00
|
|
|
/* the chunk is full, flush it */
|
2009-03-06 00:42:03 +01:00
|
|
|
decoder_flush_chunk(decoder);
|
2013-01-27 17:20:50 +01:00
|
|
|
dc->client_cond.signal();
|
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
|
|
|
|
|
|
|
assert(nbytes > 0);
|
|
|
|
|
|
|
|
if (nbytes > length)
|
|
|
|
nbytes = length;
|
|
|
|
|
|
|
|
/* copy the buffer */
|
|
|
|
|
|
|
|
memcpy(dest, data, nbytes);
|
|
|
|
|
|
|
|
/* expand the music pipe chunk */
|
|
|
|
|
2013-01-04 21:38:46 +01: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 */
|
2009-03-06 00:42:03 +01:00
|
|
|
decoder_flush_chunk(decoder);
|
2013-01-27 17:20:50 +01:00
|
|
|
dc->client_cond.signal();
|
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
|
|
|
|
|
|
|
decoder->timestamp += (double)nbytes /
|
2013-08-03 21:00:50 +02:00
|
|
|
dc->out_audio_format.GetTimeToSize();
|
2009-12-25 23:12:10 +01:00
|
|
|
|
2011-10-05 22:37:59 +02:00
|
|
|
if (dc->end_ms > 0 &&
|
|
|
|
decoder->timestamp >= dc->end_ms / 1000.0)
|
2009-12-25 23:12:10 +01:00
|
|
|
/* the end of this range has been reached:
|
|
|
|
stop decoding */
|
|
|
|
return DECODE_COMMAND_STOP;
|
2008-08-26 08:27:05 +02:00
|
|
|
}
|
|
|
|
|
2008-08-26 08:27:07 +02:00
|
|
|
return DECODE_COMMAND_NONE;
|
2008-08-26 08:27:05 +02:00
|
|
|
}
|
2008-11-02 17:02:28 +01:00
|
|
|
|
|
|
|
enum decoder_command
|
2013-08-04 23:48:01 +02:00
|
|
|
decoder_tag(gcc_unused struct decoder *decoder, struct input_stream *is,
|
2013-07-31 00:34:22 +02:00
|
|
|
Tag &&tag)
|
2008-11-02 17:02:28 +01:00
|
|
|
{
|
2013-08-04 23:48:01 +02:00
|
|
|
gcc_unused const struct decoder_control *dc = decoder->dc;
|
2009-01-03 23:28:51 +01:00
|
|
|
enum decoder_command cmd;
|
2008-11-03 18:24:01 +01:00
|
|
|
|
2009-10-31 19:22:56 +01:00
|
|
|
assert(dc->state == DECODE_STATE_DECODE);
|
|
|
|
assert(dc->pipe != NULL);
|
2009-01-03 23:29:45 +01:00
|
|
|
|
|
|
|
/* save the tag */
|
|
|
|
|
2013-07-30 20:11:57 +02:00
|
|
|
delete decoder->decoder_tag;
|
2013-07-31 00:34:22 +02:00
|
|
|
decoder->decoder_tag = new Tag(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 */
|
|
|
|
return DECODE_COMMAND_SEEK;
|
|
|
|
|
2009-01-03 23:29:45 +01:00
|
|
|
/* send tag to music pipe */
|
2008-11-02 17:02:28 +01:00
|
|
|
|
2009-01-03 23:29:45 +01:00
|
|
|
if (decoder->stream_tag != NULL) {
|
|
|
|
/* 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-07-30 20:11:57 +02:00
|
|
|
merged = Tag::Merge(*decoder->stream_tag,
|
|
|
|
*decoder->decoder_tag);
|
|
|
|
cmd = do_send_tag(decoder, *merged);
|
|
|
|
delete merged;
|
2009-01-03 23:29:45 +01:00
|
|
|
} else
|
|
|
|
/* send only the decoder tag */
|
2013-07-31 00:34:22 +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
|
2010-01-03 22:44:23 +01:00
|
|
|
decoder_replay_gain(struct decoder *decoder,
|
|
|
|
const struct replay_gain_info *replay_gain_info)
|
|
|
|
{
|
|
|
|
assert(decoder != NULL);
|
|
|
|
|
2010-02-14 17:04:39 +01:00
|
|
|
if (replay_gain_info != NULL) {
|
|
|
|
static unsigned serial;
|
|
|
|
if (++serial == 0)
|
|
|
|
serial = 1;
|
|
|
|
|
2010-05-08 09:19:44 +02:00
|
|
|
if (REPLAY_GAIN_OFF != replay_gain_mode) {
|
2013-01-05 02:07:44 +01:00
|
|
|
enum replay_gain_mode rgm = replay_gain_mode;
|
|
|
|
if (rgm != REPLAY_GAIN_ALBUM)
|
|
|
|
rgm = REPLAY_GAIN_TRACK;
|
|
|
|
|
2013-01-05 02:05:50 +01:00
|
|
|
decoder->dc->replay_gain_db = 20.0 * log10f(
|
2010-05-08 09:19:44 +02:00
|
|
|
replay_gain_tuple_scale(
|
2013-01-05 02:07:44 +01:00
|
|
|
&replay_gain_info->tuples[rgm],
|
2010-05-30 17:05:43 +02:00
|
|
|
replay_gain_preamp, replay_gain_missing_preamp,
|
|
|
|
replay_gain_limit));
|
2010-05-08 09:19:44 +02:00
|
|
|
}
|
|
|
|
|
2010-02-14 17:04:39 +01:00
|
|
|
decoder->replay_gain_info = *replay_gain_info;
|
|
|
|
decoder->replay_gain_serial = serial;
|
|
|
|
|
|
|
|
if (decoder->chunk != NULL) {
|
|
|
|
/* flush the current chunk because the new
|
|
|
|
replay gain values affect the following
|
|
|
|
samples */
|
|
|
|
decoder_flush_chunk(decoder);
|
2013-01-27 17:20:50 +01:00
|
|
|
decoder->dc->client_cond.signal();
|
2010-02-14 17:04:39 +01:00
|
|
|
}
|
|
|
|
} else
|
|
|
|
decoder->replay_gain_serial = 0;
|
2010-01-03 22:44:23 +01:00
|
|
|
}
|
2010-03-21 18:21:47 +01:00
|
|
|
|
|
|
|
void
|
2013-01-05 02:05:50 +01:00
|
|
|
decoder_mixramp(struct decoder *decoder,
|
2010-03-21 18:21:47 +01:00
|
|
|
char *mixramp_start, char *mixramp_end)
|
|
|
|
{
|
|
|
|
assert(decoder != NULL);
|
|
|
|
struct decoder_control *dc = decoder->dc;
|
|
|
|
assert(dc != NULL);
|
|
|
|
|
2013-01-21 10:13:29 +01:00
|
|
|
dc->MixRampStart(mixramp_start);
|
|
|
|
dc->MixRampEnd(mixramp_end);
|
2010-03-21 18:21:47 +01:00
|
|
|
}
|