decoder_internal.c 2.98 KB
Newer Older
1
/*
2
 * Copyright (C) 2003-2010 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 25
#include "decoder_internal.h"
#include "decoder_control.h"
#include "player_control.h"
#include "pipe.h"
#include "input_stream.h"
26
#include "buffer.h"
27
#include "chunk.h"
28 29 30

#include <assert.h>

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

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

50
	decoder_lock(dc);
51

52
	return ret > 0;
53 54
}

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

66
	if ((is == NULL || !decoder_input_buffer(dc, is)) && do_wait) {
67
		decoder_wait(dc);
68
		player_signal();
69

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

	return DECODE_COMMAND_NONE;
}

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

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

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

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

96
			return decoder->chunk;
97
		}
98

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

	return NULL;
105 106
}

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

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

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

120
	decoder->chunk = NULL;
121
}