decoder_thread.c 6.66 KB
Newer Older
Warren Dukes's avatar
Warren Dukes committed
1
/* the Music Player Daemon (MPD)
2
 * Copyright (C) 2003-2007 by Warren Dukes (warren.dukes@gmail.com)
3
 * Copyright (C) 2008 Max Kellermann <max@duempel.org>
Warren Dukes's avatar
Warren Dukes committed
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
 * 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_thread.h"
21
#include "decoder_control.h"
Max Kellermann's avatar
Max Kellermann committed
22
#include "decoder_internal.h"
Max Kellermann's avatar
Max Kellermann committed
23
#include "decoder_list.h"
24 25
#include "decoder_plugin.h"
#include "input_stream.h"
26
#include "player_control.h"
27
#include "pipe.h"
28
#include "song.h"
29
#include "tag.h"
30
#include "mapper.h"
Warren Dukes's avatar
Warren Dukes committed
31 32
#include "path.h"
#include "log.h"
33
#include "uri.h"
Warren Dukes's avatar
Warren Dukes committed
34

35 36
#include <glib.h>

Max Kellermann's avatar
Max Kellermann committed
37 38
#include <unistd.h>

39 40 41 42 43 44 45 46
static bool
decoder_stream_decode(const struct decoder_plugin *plugin,
		      struct decoder *decoder,
		      struct input_stream *input_stream)
{
	assert(plugin != NULL);
	assert(plugin->stream_decode != NULL);
	assert(decoder != NULL);
47 48
	assert(decoder->stream_tag == NULL);
	assert(decoder->decoder_tag == NULL);
49 50 51 52
	assert(input_stream != NULL);
	assert(input_stream->ready);
	assert(dc.state == DECODE_STATE_START);

53 54 55
	/* rewind the stream, so each plugin gets a fresh start */
	input_stream_seek(input_stream, 0, SEEK_SET);

56
	decoder_plugin_stream_decode(plugin, decoder, input_stream);
57

58 59
	assert(dc.state == DECODE_STATE_START ||
	       dc.state == DECODE_STATE_DECODE);
60

61
	return dc.state != DECODE_STATE_START;
62 63 64 65 66 67 68
}

static bool
decoder_file_decode(const struct decoder_plugin *plugin,
		    struct decoder *decoder, const char *path)
{
	assert(plugin != NULL);
69
	assert(plugin->file_decode != NULL);
70
	assert(decoder != NULL);
71 72
	assert(decoder->stream_tag == NULL);
	assert(decoder->decoder_tag == NULL);
73 74 75 76
	assert(path != NULL);
	assert(path[0] == '/');
	assert(dc.state == DECODE_STATE_START);

77
	decoder_plugin_file_decode(plugin, decoder, path);
78

79 80
	assert(dc.state == DECODE_STATE_START ||
	       dc.state == DECODE_STATE_DECODE);
81

82
	return dc.state != DECODE_STATE_START;
83 84
}

85
static void decoder_run_song(const struct song *song, const char *uri)
Avuton Olrich's avatar
Avuton Olrich committed
86
{
Max Kellermann's avatar
Max Kellermann committed
87
	struct decoder decoder;
88
	int ret;
89
	bool close_instream = true;
Max Kellermann's avatar
Max Kellermann committed
90
	struct input_stream input_stream;
91
	const struct decoder_plugin *plugin;
92

Max Kellermann's avatar
Max Kellermann committed
93
	if (!input_stream_open(&input_stream, uri)) {
94
		dc.state = DECODE_STATE_ERROR;
95
		return;
96
	}
Warren Dukes's avatar
Warren Dukes committed
97

98
	decoder.seeking = false;
99 100
	decoder.stream_tag = NULL;
	decoder.decoder_tag = NULL;
101
	decoder.chunk = NULL;
102

103
	dc.state = DECODE_STATE_START;
104
	dc.command = DECODE_COMMAND_NONE;
105
	notify_signal(&pc.notify);
106

Max Kellermann's avatar
Max Kellermann committed
107 108 109
	/* wait for the input stream to become ready; its metadata
	   will be available then */

Max Kellermann's avatar
Max Kellermann committed
110
	while (!input_stream.ready) {
111
		if (dc.command == DECODE_COMMAND_STOP) {
Max Kellermann's avatar
Max Kellermann committed
112
			input_stream_close(&input_stream);
113
			dc.state = DECODE_STATE_STOP;
114 115
			return;
		}
Max Kellermann's avatar
Max Kellermann committed
116

Max Kellermann's avatar
Max Kellermann committed
117
		ret = input_stream_buffer(&input_stream);
118
		if (ret < 0) {
Max Kellermann's avatar
Max Kellermann committed
119
			input_stream_close(&input_stream);
120
			dc.state = DECODE_STATE_ERROR;
121 122
			return;
		}
Max Kellermann's avatar
Max Kellermann committed
123 124
	}

125
	if (dc.command == DECODE_COMMAND_STOP) {
Max Kellermann's avatar
Max Kellermann committed
126
		input_stream_close(&input_stream);
127
		dc.state = DECODE_STATE_STOP;
128 129
		return;
	}
130

131 132
	pcm_convert_init(&decoder.conv_state);

133
	ret = false;
134
	if (!song_is_file(song)) {
135 136 137
		unsigned int next = 0;

		/* first we try mime types: */
Max Kellermann's avatar
Max Kellermann committed
138
		while ((plugin = decoder_plugin_from_mime_type(input_stream.mime, next++))) {
139
			if (plugin->stream_decode == NULL)
140
				continue;
141 142
			ret = decoder_stream_decode(plugin, &decoder,
						    &input_stream);
143 144 145 146
			if (ret)
				break;

			plugin = NULL;
147 148 149
		}

		/* if that fails, try suffix matching the URL: */
Avuton Olrich's avatar
Avuton Olrich committed
150
		if (plugin == NULL) {
151
			const char *s = uri_get_suffix(uri);
152
			next = 0;
153
			while ((plugin = decoder_plugin_from_suffix(s, next++))) {
154
				if (plugin->stream_decode == NULL)
155
					continue;
156
				ret = decoder_stream_decode(plugin, &decoder,
Max Kellermann's avatar
Max Kellermann committed
157
							    &input_stream);
158 159 160 161 162
				if (ret)
					break;

				assert(dc.state == DECODE_STATE_START);
				plugin = NULL;
163
			}
164
		}
165
		/* fallback to mp3: */
166
		/* this is needed for bastard streams that don't have a suffix
Avuton Olrich's avatar
Avuton Olrich committed
167 168
		   or set the mimeType */
		if (plugin == NULL) {
169 170
			/* we already know our mp3Plugin supports streams, no
			 * need to check for stream{Types,DecodeFunc} */
171
			if ((plugin = decoder_plugin_from_name("mp3"))) {
172
				ret = decoder_stream_decode(plugin, &decoder,
Max Kellermann's avatar
Max Kellermann committed
173
							    &input_stream);
174
			}
175
		}
Avuton Olrich's avatar
Avuton Olrich committed
176
	} else {
177
		unsigned int next = 0;
178
		const char *s = uri_get_suffix(uri);
179
		while ((plugin = decoder_plugin_from_suffix(s, next++))) {
180
			if (plugin->file_decode != NULL) {
Max Kellermann's avatar
Max Kellermann committed
181
				input_stream_close(&input_stream);
182
				close_instream = false;
183 184
				ret = decoder_file_decode(plugin,
							  &decoder, uri);
185 186
				if (ret)
					break;
187
			} else if (plugin->stream_decode != NULL) {
188
				ret = decoder_stream_decode(plugin, &decoder,
Max Kellermann's avatar
Max Kellermann committed
189
							    &input_stream);
190 191
				if (ret)
					break;
192 193
			}
		}
Avuton Olrich's avatar
Avuton Olrich committed
194
	}
Warren Dukes's avatar
Warren Dukes committed
195

196 197
	pcm_convert_deinit(&decoder.conv_state);

198
	/* flush the last chunk */
199 200
	if (decoder.chunk != NULL)
		decoder_flush_chunk(&decoder);
201

202
	if (close_instream)
Max Kellermann's avatar
Max Kellermann committed
203
		input_stream_close(&input_stream);
204

205 206 207 208 209 210
	if (decoder.stream_tag != NULL)
		tag_free(decoder.stream_tag);

	if (decoder.decoder_tag != NULL)
		tag_free(decoder.decoder_tag);

211
	dc.state = ret ? DECODE_STATE_STOP : DECODE_STATE_ERROR;
212 213
}

214 215 216 217 218 219 220
static void decoder_run(void)
{
	struct song *song = dc.next_song;
	char *uri;

	if (song_is_file(song))
		uri = map_song_fs(song);
221 222
	else
		uri = song_get_uri(song);
223 224 225 226 227 228 229 230 231 232 233 234

	if (uri == NULL) {
		dc.state = DECODE_STATE_ERROR;
		return;
	}

	dc.current_song = dc.next_song; /* NEED LOCK */
	decoder_run_song(song, uri);
	g_free(uri);

}

235
static gpointer decoder_task(G_GNUC_UNUSED gpointer arg)
Avuton Olrich's avatar
Avuton Olrich committed
236
{
237
	do {
238 239
		assert(dc.state == DECODE_STATE_STOP ||
		       dc.state == DECODE_STATE_ERROR);
240

241 242 243
		switch (dc.command) {
		case DECODE_COMMAND_START:
		case DECODE_COMMAND_SEEK:
Max Kellermann's avatar
Max Kellermann committed
244
			decoder_run();
245 246

			dc.command = DECODE_COMMAND_NONE;
247
			notify_signal(&pc.notify);
248 249 250
			break;

		case DECODE_COMMAND_STOP:
251 252
			dc.command = DECODE_COMMAND_NONE;
			notify_signal(&pc.notify);
253 254 255
			break;

		case DECODE_COMMAND_NONE:
256
			notify_wait(&dc.notify);
257
			break;
Warren Dukes's avatar
Warren Dukes committed
258
		}
259
	} while (dc.command != DECODE_COMMAND_NONE || !dc.quit);
260 261

	return NULL;
262
}
Warren Dukes's avatar
Warren Dukes committed
263

264
void decoder_thread_start(void)
265
{
266
	GError *e = NULL;
267

268 269 270 271
	assert(dc.thread == NULL);

	dc.thread = g_thread_create(decoder_task, NULL, true, &e);
	if (dc.thread == NULL)
272
		FATAL("Failed to spawn decoder task: %s\n", e->message);
Warren Dukes's avatar
Warren Dukes committed
273
}