decoder_thread.c 6.44 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
#include "player_control.h"
25
#include "pipe.h"
26
#include "song.h"
27
#include "mapper.h"
Warren Dukes's avatar
Warren Dukes committed
28 29
#include "path.h"
#include "log.h"
30
#include "ls.h"
Warren Dukes's avatar
Warren Dukes committed
31

32 33
#include <glib.h>

Max Kellermann's avatar
Max Kellermann committed
34 35
#include <unistd.h>

36 37 38 39 40 41 42 43
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);
44 45
	assert(decoder->stream_tag == NULL);
	assert(decoder->decoder_tag == NULL);
46 47 48 49
	assert(input_stream != NULL);
	assert(input_stream->ready);
	assert(dc.state == DECODE_STATE_START);

50 51 52
	/* rewind the stream, so each plugin gets a fresh start */
	input_stream_seek(input_stream, 0, SEEK_SET);

53
	plugin->stream_decode(decoder, input_stream);
54

55 56
	assert(dc.state == DECODE_STATE_START ||
	       dc.state == DECODE_STATE_DECODE);
57

58
	return dc.state != DECODE_STATE_START;
59 60 61 62 63 64 65
}

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

74
	plugin->file_decode(decoder, path);
75

76 77
	assert(dc.state == DECODE_STATE_START ||
	       dc.state == DECODE_STATE_DECODE);
78

79
	return dc.state != DECODE_STATE_START;
80 81
}

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

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

95
	decoder.seeking = false;
96 97
	decoder.stream_tag = NULL;
	decoder.decoder_tag = NULL;
98

99
	dc.state = DECODE_STATE_START;
100
	dc.command = DECODE_COMMAND_NONE;
101
	notify_signal(&pc.notify);
102

Max Kellermann's avatar
Max Kellermann committed
103 104 105
	/* wait for the input stream to become ready; its metadata
	   will be available then */

Max Kellermann's avatar
Max Kellermann committed
106
	while (!input_stream.ready) {
107
		if (dc.command == DECODE_COMMAND_STOP) {
Max Kellermann's avatar
Max Kellermann committed
108
			input_stream_close(&input_stream);
109
			dc.state = DECODE_STATE_STOP;
110 111
			return;
		}
Max Kellermann's avatar
Max Kellermann committed
112

Max Kellermann's avatar
Max Kellermann committed
113
		ret = input_stream_buffer(&input_stream);
114
		if (ret < 0) {
Max Kellermann's avatar
Max Kellermann committed
115
			input_stream_close(&input_stream);
116
			dc.state = DECODE_STATE_ERROR;
117 118
			return;
		}
Max Kellermann's avatar
Max Kellermann committed
119 120
	}

121
	if (dc.command == DECODE_COMMAND_STOP) {
Max Kellermann's avatar
Max Kellermann committed
122
		input_stream_close(&input_stream);
123
		dc.state = DECODE_STATE_STOP;
124 125
		return;
	}
126

127 128
	pcm_convert_init(&decoder.conv_state);

129
	ret = false;
130
	if (!song_is_file(song)) {
131 132 133
		unsigned int next = 0;

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

			plugin = NULL;
143 144 145
		}

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

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

192 193
	pcm_convert_deinit(&decoder.conv_state);

194
	music_pipe_flush();
195

196
	if (close_instream)
Max Kellermann's avatar
Max Kellermann committed
197
		input_stream_close(&input_stream);
198

199 200 201 202 203 204
	if (decoder.stream_tag != NULL)
		tag_free(decoder.stream_tag);

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

205
	dc.state = ret ? DECODE_STATE_STOP : DECODE_STATE_ERROR;
206 207
}

208 209 210 211 212 213 214
static void decoder_run(void)
{
	struct song *song = dc.next_song;
	char *uri;

	if (song_is_file(song))
		uri = map_song_fs(song);
215 216
	else
		uri = song_get_uri(song);
217 218 219 220 221 222 223 224 225 226 227 228

	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);

}

229
static gpointer decoder_task(G_GNUC_UNUSED gpointer arg)
Avuton Olrich's avatar
Avuton Olrich committed
230
{
231
	do {
232 233
		assert(dc.state == DECODE_STATE_STOP ||
		       dc.state == DECODE_STATE_ERROR);
234

235 236 237
		switch (dc.command) {
		case DECODE_COMMAND_START:
		case DECODE_COMMAND_SEEK:
Max Kellermann's avatar
Max Kellermann committed
238
			decoder_run();
239 240

			dc.command = DECODE_COMMAND_NONE;
241
			notify_signal(&pc.notify);
242 243 244
			break;

		case DECODE_COMMAND_STOP:
245 246
			dc.command = DECODE_COMMAND_NONE;
			notify_signal(&pc.notify);
247 248 249
			break;

		case DECODE_COMMAND_NONE:
250
			notify_wait(&dc.notify);
251
			break;
Warren Dukes's avatar
Warren Dukes committed
252
		}
253
	} while (dc.command != DECODE_COMMAND_NONE || !dc.quit);
254 255

	return NULL;
256
}
Warren Dukes's avatar
Warren Dukes committed
257

258
void decoder_thread_start(void)
259
{
260
	GError *e = NULL;
261
	GThread *t;
262

263 264
	if (!(t = g_thread_create(decoder_task, NULL, FALSE, &e)))
		FATAL("Failed to spawn decoder task: %s\n", e->message);
Warren Dukes's avatar
Warren Dukes committed
265
}