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-01-07 09:38:02 +01:00
|
|
|
#include "decoder_api.h"
|
2013-01-04 08:41:16 +01:00
|
|
|
|
|
|
|
extern "C" {
|
2011-10-10 08:57:51 +02:00
|
|
|
#include "audio_config.h"
|
2013-01-04 08:41:16 +01:00
|
|
|
}
|
|
|
|
|
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"
|
|
|
|
#include "song.h"
|
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>
|
|
|
|
|
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,
|
|
|
|
const struct audio_format *audio_format,
|
|
|
|
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);
|
2008-10-29 17:29:03 +01:00
|
|
|
assert(audio_format != NULL);
|
2008-11-10 15:02:26 +01:00
|
|
|
assert(audio_format_defined(audio_format));
|
2008-11-21 20:27:30 +01:00
|
|
|
assert(audio_format_valid(audio_format));
|
2008-08-26 08:27:04 +02:00
|
|
|
|
2009-10-31 19:22:56 +01:00
|
|
|
dc->in_audio_format = *audio_format;
|
|
|
|
getOutputAudioFormat(audio_format, &dc->out_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;
|
2011-01-10 21:27:43 +01:00
|
|
|
g_cond_signal(dc->client_cond);
|
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",
|
|
|
|
audio_format_to_string(&dc->in_audio_format, &af_string),
|
2009-03-01 10:31:47 +01:00
|
|
|
seekable ? "true" : "false");
|
|
|
|
|
2009-10-31 19:22:56 +01:00
|
|
|
if (!audio_format_equals(&dc->in_audio_format,
|
|
|
|
&dc->out_audio_format))
|
2009-11-10 17:57:14 +01:00
|
|
|
g_debug("converting to %s",
|
|
|
|
audio_format_to_string(&dc->out_audio_format,
|
|
|
|
&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
|
|
|
*/
|
|
|
|
G_GNUC_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.
|
|
|
|
*/
|
|
|
|
G_GNUC_PURE
|
|
|
|
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);
|
|
|
|
assert(music_pipe_empty(dc->pipe));
|
|
|
|
|
|
|
|
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) {
|
2009-10-31 19:22:56 +01:00
|
|
|
music_buffer_return(dc->buffer, decoder->chunk);
|
2009-03-06 00:42:01 +01:00
|
|
|
decoder->chunk = NULL;
|
|
|
|
}
|
|
|
|
|
2009-10-31 19:22:56 +01:00
|
|
|
music_pipe_clear(dc->pipe, 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;
|
2011-01-10 21:27:43 +01:00
|
|
|
g_cond_signal(dc->client_cond);
|
2013-01-21 10:13:29 +01:00
|
|
|
dc->Unlock();
|
2008-08-26 08:27:07 +02:00
|
|
|
}
|
|
|
|
|
2008-11-24 14:32:53 +01:00
|
|
|
double decoder_seek_where(G_GNUC_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".
|
|
|
|
*/
|
|
|
|
G_GNUC_PURE
|
|
|
|
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 */
|
2009-11-14 23:53:04 +01:00
|
|
|
GError *error = NULL;
|
2008-08-26 08:27:14 +02:00
|
|
|
size_t nbytes;
|
|
|
|
|
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;
|
|
|
|
|
2011-09-14 21:46:41 +02:00
|
|
|
input_stream_lock(is);
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
if (decoder_check_cancel_read(decoder)) {
|
|
|
|
input_stream_unlock(is);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (input_stream_available(is))
|
|
|
|
break;
|
|
|
|
|
|
|
|
g_cond_wait(is->cond, is->mutex);
|
|
|
|
}
|
2009-11-14 23:53:04 +01:00
|
|
|
|
2011-09-16 08:04:02 +02:00
|
|
|
nbytes = input_stream_read(is, buffer, length, &error);
|
|
|
|
assert(nbytes == 0 || error == NULL);
|
|
|
|
assert(nbytes > 0 || error != NULL || input_stream_eof(is));
|
2008-08-26 08:27:14 +02:00
|
|
|
|
2011-09-16 08:04:02 +02:00
|
|
|
if (G_UNLIKELY(nbytes == 0 && error != NULL)) {
|
|
|
|
g_warning("%s", error->message);
|
|
|
|
g_error_free(error);
|
2008-08-26 08:27:14 +02:00
|
|
|
}
|
2011-09-16 08:04:02 +02:00
|
|
|
|
2011-09-14 21:46:41 +02:00
|
|
|
input_stream_unlock(is);
|
|
|
|
|
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
|
2011-09-14 09:37:52 +02:00
|
|
|
do_send_tag(struct decoder *decoder, const struct 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);
|
2011-01-10 21:27:43 +01:00
|
|
|
g_cond_signal(decoder->dc->client_cond);
|
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
|
|
|
}
|
|
|
|
|
2009-03-06 00:42:01 +01:00
|
|
|
chunk->tag = tag_dup(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)
|
|
|
|
{
|
|
|
|
struct tag *tag;
|
|
|
|
|
2009-04-13 19:25:53 +02:00
|
|
|
tag = is != NULL
|
2011-09-14 21:46:41 +02:00
|
|
|
? input_stream_lock_tag(is)
|
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
|
|
|
|
|
|
|
if (decoder->stream_tag != NULL)
|
|
|
|
tag_free(decoder->stream_tag);
|
|
|
|
|
|
|
|
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-07-23 12:01:03 +02:00
|
|
|
GError *error = NULL;
|
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);
|
|
|
|
assert(length % audio_format_frame_size(&dc->in_audio_format) == 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 */
|
|
|
|
struct tag *tag;
|
|
|
|
|
2009-12-07 14:52:48 +01:00
|
|
|
tag = tag_merge(decoder->decoder_tag,
|
|
|
|
decoder->stream_tag);
|
2011-09-14 09:37:52 +02:00
|
|
|
cmd = do_send_tag(decoder, tag);
|
2009-01-03 23:29:45 +01:00
|
|
|
tag_free(tag);
|
|
|
|
} else
|
|
|
|
/* send only the stream tag */
|
2011-09-14 09:37:52 +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
|
|
|
}
|
|
|
|
|
2009-10-31 19:22:56 +01:00
|
|
|
if (!audio_format_equals(&dc->in_audio_format, &dc->out_audio_format)) {
|
2009-01-17 13:11:16 +01:00
|
|
|
data = pcm_convert(&decoder->conv_state,
|
2009-10-31 19:22:56 +01:00
|
|
|
&dc->in_audio_format, data, length,
|
|
|
|
&dc->out_audio_format, &length,
|
2009-07-23 12:01:03 +02:00
|
|
|
&error);
|
|
|
|
if (data == NULL) {
|
|
|
|
/* the PCM conversion has failed - stop
|
|
|
|
playback, since we have no better way to
|
|
|
|
bail out */
|
|
|
|
g_warning("%s", error->message);
|
|
|
|
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);
|
2011-01-10 21:27:43 +01:00
|
|
|
g_cond_signal(dc->client_cond);
|
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);
|
2011-01-10 21:27:43 +01:00
|
|
|
g_cond_signal(dc->client_cond);
|
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 /
|
2010-03-17 18:26:36 +01:00
|
|
|
audio_format_time_to_size(&dc->out_audio_format);
|
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
|
2008-11-24 14:32:53 +01:00
|
|
|
decoder_tag(G_GNUC_UNUSED struct decoder *decoder, struct input_stream *is,
|
2008-11-02 17:02:28 +01:00
|
|
|
const struct tag *tag)
|
|
|
|
{
|
2009-12-14 23:22:51 +01:00
|
|
|
G_GNUC_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
|
|
|
assert(tag != NULL);
|
|
|
|
|
|
|
|
/* save the tag */
|
|
|
|
|
|
|
|
if (decoder->decoder_tag != NULL)
|
|
|
|
tag_free(decoder->decoder_tag);
|
|
|
|
decoder->decoder_tag = tag_dup(tag);
|
|
|
|
|
|
|
|
/* 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 */
|
|
|
|
struct tag *merged;
|
2008-11-03 18:24:01 +01:00
|
|
|
|
2009-01-03 23:29:45 +01:00
|
|
|
merged = tag_merge(decoder->stream_tag, decoder->decoder_tag);
|
2011-09-14 09:37:52 +02:00
|
|
|
cmd = do_send_tag(decoder, merged);
|
2009-01-03 23:29:45 +01:00
|
|
|
tag_free(merged);
|
|
|
|
} else
|
|
|
|
/* send only the decoder tag */
|
2011-09-14 09:37:52 +02:00
|
|
|
cmd = do_send_tag(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);
|
2011-01-10 21:27:43 +01:00
|
|
|
g_cond_signal(decoder->dc->client_cond);
|
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
|
|
|
}
|