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

31 32
#include <glib.h>

33 34 35 36 37 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);
	assert(!decoder->stream_tag_sent);
	assert(input_stream != NULL);
	assert(input_stream->ready);
	assert(dc.state == DECODE_STATE_START);

46 47 48
	/* rewind the stream, so each plugin gets a fresh start */
	input_stream_seek(input_stream, 0, SEEK_SET);

49
	plugin->stream_decode(decoder, input_stream);
50

51 52
	assert(dc.state == DECODE_STATE_START ||
	       dc.state == DECODE_STATE_DECODE);
53

54
	return dc.state != DECODE_STATE_START;
55 56 57 58 59 60 61
}

static bool
decoder_file_decode(const struct decoder_plugin *plugin,
		    struct decoder *decoder, const char *path)
{
	assert(plugin != NULL);
62
	assert(plugin->file_decode != NULL);
63 64 65 66 67 68
	assert(decoder != NULL);
	assert(!decoder->stream_tag_sent);
	assert(path != NULL);
	assert(path[0] == '/');
	assert(dc.state == DECODE_STATE_START);

69
	plugin->file_decode(decoder, path);
70

71 72
	assert(dc.state == DECODE_STATE_START ||
	       dc.state == DECODE_STATE_DECODE);
73

74
	return dc.state != DECODE_STATE_START;
75 76
}

Max Kellermann's avatar
Max Kellermann committed
77
static void decoder_run(void)
Avuton Olrich's avatar
Avuton Olrich committed
78
{
79
	struct song *song = dc.next_song;
80 81
	char buffer[MPD_PATH_MAX];
	const char *uri;
Max Kellermann's avatar
Max Kellermann committed
82
	struct decoder decoder;
83
	int ret;
84
	bool close_instream = true;
Max Kellermann's avatar
Max Kellermann committed
85
	struct input_stream input_stream;
86
	const struct decoder_plugin *plugin;
87

88
	if (song_is_file(song))
89
		uri = map_song_fs(song, buffer);
90
	else
91 92
		uri = song_get_url(song, buffer);
	if (uri == NULL) {
93
		dc.state = DECODE_STATE_ERROR;
94 95
		return;
	}
Warren Dukes's avatar
Warren Dukes committed
96

97
	dc.current_song = dc.next_song; /* NEED LOCK */
Max Kellermann's avatar
Max Kellermann committed
98
	if (!input_stream_open(&input_stream, uri)) {
99
		dc.state = DECODE_STATE_ERROR;
100
		return;
101
	}
Warren Dukes's avatar
Warren Dukes committed
102

103
	decoder.seeking = false;
104
	decoder.stream_tag_sent = false;
105

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

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

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

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

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

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 = getSuffix(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 = getSuffix(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
	music_pipe_flush();
198

199
	if (close_instream)
Max Kellermann's avatar
Max Kellermann committed
200
		input_stream_close(&input_stream);
201 202

	dc.state = ret ? DECODE_STATE_STOP : DECODE_STATE_ERROR;
203 204
}

205
static void * decoder_task(G_GNUC_UNUSED void *arg)
Avuton Olrich's avatar
Avuton Olrich committed
206
{
207
	while (1) {
208 209
		assert(dc.state == DECODE_STATE_STOP ||
		       dc.state == DECODE_STATE_ERROR);
210

211 212 213
		switch (dc.command) {
		case DECODE_COMMAND_START:
		case DECODE_COMMAND_SEEK:
Max Kellermann's avatar
Max Kellermann committed
214
			decoder_run();
215 216

			dc.command = DECODE_COMMAND_NONE;
217
			notify_signal(&pc.notify);
218 219 220
			break;

		case DECODE_COMMAND_STOP:
221 222
			dc.command = DECODE_COMMAND_NONE;
			notify_signal(&pc.notify);
223 224 225
			break;

		case DECODE_COMMAND_NONE:
226
			notify_wait(&dc.notify);
227
			break;
Warren Dukes's avatar
Warren Dukes committed
228 229
		}
	}
230 231

	return NULL;
232
}
Warren Dukes's avatar
Warren Dukes committed
233

234
void decoder_thread_start(void)
235 236 237 238 239 240
{
	pthread_attr_t attr;
	pthread_t decoder_thread;

	pthread_attr_init(&attr);
	pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
241
	if (pthread_create(&decoder_thread, &attr, decoder_task, NULL))
242
		FATAL("Failed to spawn decoder task: %s\n", strerror(errno));
Warren Dukes's avatar
Warren Dukes committed
243
}