decoder_internal.c 2.97 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2011 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13
 * 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.
14 15 16 17
 *
 * 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.
18 19
 */

20
#include "config.h"
21 22 23 24
#include "decoder_internal.h"
#include "decoder_control.h"
#include "pipe.h"
#include "input_stream.h"
25
#include "buffer.h"
26
#include "chunk.h"
27 28 29

#include <assert.h>

30 31 32 33 34 35
/**
 * 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.
 */
36
static bool
37
decoder_input_buffer(struct decoder_control *dc, struct input_stream *is)
38
{
39
	GError *error = NULL;
40 41
	int ret;

42
	decoder_unlock(dc);
43 44 45 46 47 48
	ret = input_stream_buffer(is, &error);
	if (ret < 0) {
		g_warning("%s", error->message);
		g_error_free(error);
	}

49
	decoder_lock(dc);
50

51
	return ret > 0;
52 53
}

54 55 56 57 58
/**
 * All chunks are full of decoded data; wait for the player to free
 * one.
 */
static enum decoder_command
59
need_chunks(struct decoder_control *dc, struct input_stream *is, bool do_wait)
60
{
61 62 63
	if (dc->command == DECODE_COMMAND_STOP ||
	    dc->command == DECODE_COMMAND_SEEK)
		return dc->command;
64

65
	if ((is == NULL || !decoder_input_buffer(dc, is)) && do_wait) {
66
		decoder_wait(dc);
67
		g_cond_signal(dc->client_cond);
68

69
		return dc->command;
70 71 72 73 74 75
	}

	return DECODE_COMMAND_NONE;
}

struct music_chunk *
76
decoder_get_chunk(struct decoder *decoder, struct input_stream *is)
77
{
78
	struct decoder_control *dc = decoder->dc;
79 80
	enum decoder_command cmd;

81 82 83 84 85
	assert(decoder != NULL);

	if (decoder->chunk != NULL)
		return decoder->chunk;

86
	do {
87
		decoder->chunk = music_buffer_allocate(dc->buffer);
88 89 90 91 92 93 94
		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;

95
			return decoder->chunk;
96
		}
97

98 99 100
		decoder_lock(dc);
		cmd = need_chunks(dc, is, true);
		decoder_unlock(dc);
101 102 103
	} while (cmd == DECODE_COMMAND_NONE);

	return NULL;
104 105
}

106 107
void
decoder_flush_chunk(struct decoder *decoder)
108
{
109 110
	struct decoder_control *dc = decoder->dc;

111 112 113
	assert(decoder != NULL);
	assert(decoder->chunk != NULL);

114
	if (music_chunk_is_empty(decoder->chunk))
115
		music_buffer_return(dc->buffer, decoder->chunk);
116
	else
117
		music_pipe_push(dc->pipe, decoder->chunk);
118

119
	decoder->chunk = NULL;
120
}