decoder_thread.c 6.68 KB
Newer Older
1 2 3
/*
 * Copyright (C) 2003-2009 The Music Player Daemon Project
 * http://www.musicpd.org
Warren Dukes's avatar
Warren Dukes committed
4 5 6 7 8 9 10 11 12 13
 *
 * 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.
Warren Dukes's avatar
Warren Dukes committed
18 19
 */

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
#include "path.h"
32
#include "uri.h"
Warren Dukes's avatar
Warren Dukes committed
33

34 35
#include <glib.h>

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

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

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

55
	decoder_plugin_stream_decode(plugin, decoder, input_stream);
56

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

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

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

76
	decoder_plugin_file_decode(plugin, decoder, path);
77

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

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

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

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

97
	decoder.seeking = false;
98 99
	decoder.song_tag = song->tag != NULL && song_is_file(song)
		? tag_dup(song->tag) : NULL;
100 101
	decoder.stream_tag = NULL;
	decoder.decoder_tag = NULL;
102
	decoder.chunk = NULL;
103

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

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

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

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

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

132 133
	pcm_convert_init(&decoder.conv_state);

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

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

			plugin = NULL;
148 149 150
		}

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

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

197 198
	pcm_convert_deinit(&decoder.conv_state);

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

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

206 207 208
	if (decoder.song_tag != NULL)
		tag_free(decoder.song_tag);

209 210 211 212 213 214
	if (decoder.stream_tag != NULL)
		tag_free(decoder.stream_tag);

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

215
	dc.state = ret ? DECODE_STATE_STOP : DECODE_STATE_ERROR;
216 217
}

218 219 220 221 222 223 224
static void decoder_run(void)
{
	struct song *song = dc.next_song;
	char *uri;

	if (song_is_file(song))
		uri = map_song_fs(song);
225 226
	else
		uri = song_get_uri(song);
227 228 229 230 231 232 233 234 235 236 237 238

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

}

239
static gpointer decoder_task(G_GNUC_UNUSED gpointer arg)
Avuton Olrich's avatar
Avuton Olrich committed
240
{
241
	do {
242 243
		assert(dc.state == DECODE_STATE_STOP ||
		       dc.state == DECODE_STATE_ERROR);
244

245 246 247
		switch (dc.command) {
		case DECODE_COMMAND_START:
		case DECODE_COMMAND_SEEK:
Max Kellermann's avatar
Max Kellermann committed
248
			decoder_run();
249 250

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

		case DECODE_COMMAND_STOP:
255 256
			dc.command = DECODE_COMMAND_NONE;
			notify_signal(&pc.notify);
257 258 259
			break;

		case DECODE_COMMAND_NONE:
260
			notify_wait(&dc.notify);
261
			break;
Warren Dukes's avatar
Warren Dukes committed
262
		}
263
	} while (dc.command != DECODE_COMMAND_NONE || !dc.quit);
264 265

	return NULL;
266
}
Warren Dukes's avatar
Warren Dukes committed
267

268
void decoder_thread_start(void)
269
{
270
	GError *e = NULL;
271

272 273 274 275
	assert(dc.thread == NULL);

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