decoder_api.c 7.59 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/* the Music Player Daemon (MPD)
 * Copyright (C) 2003-2007 by Warren Dukes (warren.dukes@gmail.com)
 * Copyright (C) 2008 Max Kellermann <max@duempel.org>
 * This project's homepage is: 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.
 * 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

20
#include "decoder_api.h"
21
#include "decoder_internal.h"
22
#include "decoder_control.h"
23
#include "player_control.h"
24
#include "audio.h"
25
#include "song.h"
26

Max Kellermann's avatar
Max Kellermann committed
27
#include "normalize.h"
28
#include "pipe.h"
29

30
#include <glib.h>
31

32 33 34
#include <assert.h>
#include <stdlib.h>

35
void decoder_initialized(G_GNUC_UNUSED struct decoder * decoder,
36
			 const struct audio_format *audio_format,
37
			 bool seekable, float total_time)
38 39
{
	assert(dc.state == DECODE_STATE_START);
40
	assert(decoder != NULL);
41 42
	assert(decoder->stream_tag == NULL);
	assert(decoder->decoder_tag == NULL);
43
	assert(!decoder->seeking);
44
	assert(audio_format != NULL);
45
	assert(audio_format_defined(audio_format));
46
	assert(audio_format_valid(audio_format));
47

48 49
	dc.in_audio_format = *audio_format;
	getOutputAudioFormat(audio_format, &dc.out_audio_format);
50

51
	dc.seekable = seekable;
Max Kellermann's avatar
Max Kellermann committed
52
	dc.total_time = total_time;
53

54 55 56
	dc.state = DECODE_STATE_DECODE;
	notify_signal(&pc.notify);
}
Max Kellermann's avatar
Max Kellermann committed
57

58
char *decoder_get_uri(G_GNUC_UNUSED struct decoder *decoder)
Max Kellermann's avatar
Max Kellermann committed
59
{
60
	return song_get_uri(dc.current_song);
Max Kellermann's avatar
Max Kellermann committed
61 62
}

63
enum decoder_command decoder_get_command(G_GNUC_UNUSED struct decoder * decoder)
64 65 66 67
{
	return dc.command;
}

68
void decoder_command_finished(G_GNUC_UNUSED struct decoder * decoder)
69 70
{
	assert(dc.command != DECODE_COMMAND_NONE);
71
	assert(dc.command != DECODE_COMMAND_SEEK ||
Max Kellermann's avatar
Max Kellermann committed
72
	       dc.seek_error || decoder->seeking);
73

74 75
	if (dc.command == DECODE_COMMAND_SEEK)
		/* delete frames from the old song position */
76
		music_pipe_clear();
77

78 79 80 81
	dc.command = DECODE_COMMAND_NONE;
	notify_signal(&pc.notify);
}

82
double decoder_seek_where(G_GNUC_UNUSED struct decoder * decoder)
83 84 85
{
	assert(dc.command == DECODE_COMMAND_SEEK);

86
	decoder->seeking = true;
87

Max Kellermann's avatar
Max Kellermann committed
88
	return dc.seek_where;
89 90 91 92 93 94
}

void decoder_seek_error(struct decoder * decoder)
{
	assert(dc.command == DECODE_COMMAND_SEEK);

Max Kellermann's avatar
Max Kellermann committed
95
	dc.seek_error = true;
96 97 98
	decoder_command_finished(decoder);
}

Max Kellermann's avatar
Max Kellermann committed
99
size_t decoder_read(struct decoder *decoder,
100
		    struct input_stream *is,
Max Kellermann's avatar
Max Kellermann committed
101 102 103 104
		    void *buffer, size_t length)
{
	size_t nbytes;

105 106
	assert(decoder == NULL ||
	       dc.state == DECODE_STATE_START ||
107
	       dc.state == DECODE_STATE_DECODE);
108
	assert(is != NULL);
Max Kellermann's avatar
Max Kellermann committed
109 110
	assert(buffer != NULL);

111 112 113
	if (length == 0)
		return 0;

114
	while (true) {
Max Kellermann's avatar
Max Kellermann committed
115
		/* XXX don't allow decoder==NULL */
116
		if (decoder != NULL &&
117 118 119
		    /* ignore the SEEK command during initialization,
		       the plugin should handle that after it has
		       initialized successfully */
120
		    (dc.command != DECODE_COMMAND_SEEK ||
121
		     (dc.state != DECODE_STATE_START && !decoder->seeking)) &&
122
		    dc.command != DECODE_COMMAND_NONE)
Max Kellermann's avatar
Max Kellermann committed
123 124
			return 0;

125 126
		nbytes = input_stream_read(is, buffer, length);
		if (nbytes > 0 || input_stream_eof(is))
Max Kellermann's avatar
Max Kellermann committed
127 128 129 130
			return nbytes;

		/* sleep for a fraction of a second! */
		/* XXX don't sleep, wait for an event instead */
131
		g_usleep(10000);
Max Kellermann's avatar
Max Kellermann committed
132 133 134
	}
}

Max Kellermann's avatar
Max Kellermann committed
135 136 137 138
/**
 * All chunks are full of decoded data; wait for the player to free
 * one.
 */
139
static enum decoder_command
140
need_chunks(struct input_stream *is, bool do_wait)
Max Kellermann's avatar
Max Kellermann committed
141
{
142 143 144
	if (dc.command == DECODE_COMMAND_STOP ||
	    dc.command == DECODE_COMMAND_SEEK)
		return dc.command;
Max Kellermann's avatar
Max Kellermann committed
145

146
	if ((is == NULL || input_stream_buffer(is) <= 0) && do_wait) {
Max Kellermann's avatar
Max Kellermann committed
147 148
		notify_wait(&dc.notify);
		notify_signal(&pc.notify);
149

150
		return dc.command;
Max Kellermann's avatar
Max Kellermann committed
151 152
	}

153
	return DECODE_COMMAND_NONE;
Max Kellermann's avatar
Max Kellermann committed
154 155
}

156 157 158 159 160 161 162 163 164 165 166 167
static enum decoder_command
do_send_tag(struct input_stream *is, const struct tag *tag)
{
	while (!music_pipe_tag(tag)) {
		enum decoder_command cmd = need_chunks(is, true);
		if (cmd != DECODE_COMMAND_NONE)
			return cmd;
	}

	return DECODE_COMMAND_NONE;
}

168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
static bool
update_stream_tag(struct decoder *decoder, struct input_stream *is)
{
	struct tag *tag;

	if (is == NULL)
		return false;

	tag = input_stream_tag(is);
	if (tag == NULL)
		return false;

	if (decoder->stream_tag != NULL)
		tag_free(decoder->stream_tag);

	decoder->stream_tag = tag;
	return true;
}

187
enum decoder_command
188
decoder_data(struct decoder *decoder,
189
	     struct input_stream *is,
190
	     const void *_data, size_t length,
191
	     float data_time, uint16_t bitRate,
192
	     struct replay_gain_info *replay_gain_info)
Max Kellermann's avatar
Max Kellermann committed
193
{
194
	const char *data = _data;
Max Kellermann's avatar
Max Kellermann committed
195

196
	assert(dc.state == DECODE_STATE_DECODE);
197
	assert(length % audio_format_frame_size(&dc.in_audio_format) == 0);
198

199
	if (dc.command == DECODE_COMMAND_STOP ||
200 201
	    dc.command == DECODE_COMMAND_SEEK ||
	    length == 0)
202 203
		return dc.command;

204 205 206 207
	/* send stream tags */

	if (update_stream_tag(decoder, is)) {
		enum decoder_command cmd;
208

209 210 211 212 213 214 215 216 217 218 219 220 221 222
		if (decoder->decoder_tag != NULL) {
			/* merge with tag from decoder plugin */
			struct tag *tag;

			tag = tag_merge(decoder->stream_tag,
					decoder->decoder_tag);
			cmd = do_send_tag(is, tag);
			tag_free(tag);
		} else
			/* send only the stream tag */
			cmd = do_send_tag(is, decoder->stream_tag);

		if (cmd != DECODE_COMMAND_NONE)
			return cmd;
223 224
	}

225 226 227 228
	if (!audio_format_equals(&dc.in_audio_format, &dc.out_audio_format)) {
		data = pcm_convert(&decoder->conv_state,
				   &dc.in_audio_format, data, length,
				   &dc.out_audio_format, &length);
229 230 231 232 233

		/* under certain circumstances, pcm_convert() may
		   return an empty buffer - this condition should be
		   investigated further, but for now, do this check as
		   a workaround: */
234
		if (data == NULL)
235
			return DECODE_COMMAND_NONE;
Max Kellermann's avatar
Max Kellermann committed
236 237
	}

238
	while (length > 0) {
239 240 241 242 243 244 245
		size_t nbytes;
		char *dest = music_pipe_write(&dc.out_audio_format,
					      data_time, bitRate,
					      &nbytes);
		if (dest == NULL) {
			/* the music pipe is full: wait for more
			   room */
246
			enum decoder_command cmd = need_chunks(is, true);
247 248
			if (cmd != DECODE_COMMAND_NONE)
				return cmd;
249
			continue;
Max Kellermann's avatar
Max Kellermann committed
250
		}
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275

		assert(nbytes > 0);

		if (nbytes > length)
			nbytes = length;

		/* copy the buffer */

		memcpy(dest, data, nbytes);

		/* apply replay gain or normalization */

		if (replay_gain_info != NULL &&
		    replay_gain_mode != REPLAY_GAIN_OFF)
			replay_gain_apply(replay_gain_info, dest, nbytes,
					  &dc.out_audio_format);
		else if (normalizationEnabled)
			normalizeData(dest, nbytes, &dc.out_audio_format);

		/* expand the music pipe chunk */

		music_pipe_expand(&dc.out_audio_format, nbytes);

		data += nbytes;
		length -= nbytes;
Max Kellermann's avatar
Max Kellermann committed
276 277
	}

278
	return DECODE_COMMAND_NONE;
Max Kellermann's avatar
Max Kellermann committed
279
}
280 281

enum decoder_command
282
decoder_tag(G_GNUC_UNUSED struct decoder *decoder, struct input_stream *is,
283 284
	    const struct tag *tag)
{
285
	enum decoder_command cmd;
286

287
	assert(dc.state == DECODE_STATE_DECODE);
288 289 290 291 292 293 294 295 296
	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 */
297

298
	update_stream_tag(decoder, is);
299

300
	/* send tag to music pipe */
301

302 303 304
	if (decoder->stream_tag != NULL) {
		/* merge with tag from input stream */
		struct tag *merged;
305

306 307 308 309 310 311
		merged = tag_merge(decoder->stream_tag, decoder->decoder_tag);
		cmd = do_send_tag(is, merged);
		tag_free(merged);
	} else
		/* send only the decoder tag */
		cmd = do_send_tag(is, tag);
312

313
	return cmd;
314
}