2009-03-06 00:42:01 +01:00
|
|
|
/*
|
2011-01-29 10:13:54 +01:00
|
|
|
* Copyright (C) 2003-2011 The Music Player Daemon Project
|
2009-03-06 00:42:01 +01:00
|
|
|
* http://www.musicpd.org
|
|
|
|
*
|
|
|
|
* 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.
|
2009-03-06 00:42:01 +01:00
|
|
|
*/
|
|
|
|
|
2009-11-12 09:12:38 +01:00
|
|
|
#include "config.h"
|
2009-03-06 00:42:01 +01:00
|
|
|
#include "decoder_internal.h"
|
|
|
|
#include "decoder_control.h"
|
|
|
|
#include "pipe.h"
|
|
|
|
#include "input_stream.h"
|
2009-03-06 00:42:03 +01:00
|
|
|
#include "buffer.h"
|
2009-03-07 21:41:23 +01:00
|
|
|
#include "chunk.h"
|
2009-03-06 00:42:01 +01:00
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
|
2009-08-13 23:33:46 +02:00
|
|
|
/**
|
|
|
|
* This is a wrapper for input_stream_buffer(). It assumes that the
|
|
|
|
* decoder is currently locked, and temporarily unlocks it while
|
|
|
|
* calling input_stream_buffer(). We shouldn't hold the lock during a
|
|
|
|
* potentially blocking operation.
|
|
|
|
*/
|
2009-12-15 22:24:00 +01:00
|
|
|
static bool
|
2009-10-31 19:22:56 +01:00
|
|
|
decoder_input_buffer(struct decoder_control *dc, struct input_stream *is)
|
2009-08-13 23:33:46 +02:00
|
|
|
{
|
2009-11-14 23:53:04 +01:00
|
|
|
GError *error = NULL;
|
2009-08-13 23:33:46 +02:00
|
|
|
int ret;
|
|
|
|
|
2009-10-31 19:22:56 +01:00
|
|
|
decoder_unlock(dc);
|
2009-11-14 23:53:04 +01:00
|
|
|
ret = input_stream_buffer(is, &error);
|
|
|
|
if (ret < 0) {
|
|
|
|
g_warning("%s", error->message);
|
|
|
|
g_error_free(error);
|
|
|
|
}
|
|
|
|
|
2009-10-31 19:22:56 +01:00
|
|
|
decoder_lock(dc);
|
2009-08-13 23:33:46 +02:00
|
|
|
|
2009-12-15 22:24:00 +01:00
|
|
|
return ret > 0;
|
2009-08-13 23:33:46 +02:00
|
|
|
}
|
|
|
|
|
2009-03-06 00:42:01 +01:00
|
|
|
/**
|
|
|
|
* All chunks are full of decoded data; wait for the player to free
|
|
|
|
* one.
|
|
|
|
*/
|
|
|
|
static enum decoder_command
|
2009-10-31 19:22:56 +01:00
|
|
|
need_chunks(struct decoder_control *dc, struct input_stream *is, bool do_wait)
|
2009-03-06 00:42:01 +01:00
|
|
|
{
|
2009-10-31 19:22:56 +01:00
|
|
|
if (dc->command == DECODE_COMMAND_STOP ||
|
|
|
|
dc->command == DECODE_COMMAND_SEEK)
|
|
|
|
return dc->command;
|
2009-03-06 00:42:01 +01:00
|
|
|
|
2009-12-15 22:24:00 +01:00
|
|
|
if ((is == NULL || !decoder_input_buffer(dc, is)) && do_wait) {
|
2009-10-31 19:22:56 +01:00
|
|
|
decoder_wait(dc);
|
2011-01-10 21:27:43 +01:00
|
|
|
g_cond_signal(dc->client_cond);
|
2009-03-06 00:42:01 +01:00
|
|
|
|
2009-10-31 19:22:56 +01:00
|
|
|
return dc->command;
|
2009-03-06 00:42:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return DECODE_COMMAND_NONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct music_chunk *
|
2009-03-06 00:42:03 +01:00
|
|
|
decoder_get_chunk(struct decoder *decoder, struct input_stream *is)
|
2009-03-06 00:42:01 +01:00
|
|
|
{
|
2009-10-31 19:22:56 +01:00
|
|
|
struct decoder_control *dc = decoder->dc;
|
2009-03-06 00:42:03 +01:00
|
|
|
enum decoder_command cmd;
|
|
|
|
|
2009-03-06 00:42:01 +01:00
|
|
|
assert(decoder != NULL);
|
|
|
|
|
|
|
|
if (decoder->chunk != NULL)
|
|
|
|
return decoder->chunk;
|
|
|
|
|
2009-03-06 00:42:03 +01:00
|
|
|
do {
|
2009-10-31 19:22:56 +01:00
|
|
|
decoder->chunk = music_buffer_allocate(dc->buffer);
|
2010-02-14 17:04:39 +01:00
|
|
|
if (decoder->chunk != NULL) {
|
|
|
|
decoder->chunk->replay_gain_serial =
|
|
|
|
decoder->replay_gain_serial;
|
|
|
|
if (decoder->replay_gain_serial != 0)
|
|
|
|
decoder->chunk->replay_gain_info =
|
|
|
|
decoder->replay_gain_info;
|
|
|
|
|
2009-03-06 00:42:03 +01:00
|
|
|
return decoder->chunk;
|
2010-02-14 17:04:39 +01:00
|
|
|
}
|
2009-03-06 00:42:03 +01:00
|
|
|
|
2009-10-31 19:22:56 +01:00
|
|
|
decoder_lock(dc);
|
|
|
|
cmd = need_chunks(dc, is, true);
|
|
|
|
decoder_unlock(dc);
|
2009-03-06 00:42:03 +01:00
|
|
|
} while (cmd == DECODE_COMMAND_NONE);
|
|
|
|
|
|
|
|
return NULL;
|
2009-03-06 00:42:01 +01:00
|
|
|
}
|
|
|
|
|
2009-03-06 00:42:03 +01:00
|
|
|
void
|
|
|
|
decoder_flush_chunk(struct decoder *decoder)
|
2009-03-06 00:42:01 +01:00
|
|
|
{
|
2009-10-31 19:22:56 +01:00
|
|
|
struct decoder_control *dc = decoder->dc;
|
|
|
|
|
2009-03-06 00:42:01 +01:00
|
|
|
assert(decoder != NULL);
|
|
|
|
assert(decoder->chunk != NULL);
|
|
|
|
|
2009-03-07 21:41:23 +01:00
|
|
|
if (music_chunk_is_empty(decoder->chunk))
|
2009-10-31 19:22:56 +01:00
|
|
|
music_buffer_return(dc->buffer, decoder->chunk);
|
2009-03-07 21:41:23 +01:00
|
|
|
else
|
2009-10-31 19:22:56 +01:00
|
|
|
music_pipe_push(dc->pipe, decoder->chunk);
|
2009-03-07 21:41:23 +01:00
|
|
|
|
2009-03-06 00:42:03 +01:00
|
|
|
decoder->chunk = NULL;
|
2009-03-06 00:42:01 +01:00
|
|
|
}
|