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

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

47
	plugin->stream_decode(decoder, input_stream);
48

49 50
	assert(dc.state == DECODE_STATE_START ||
	       dc.state == DECODE_STATE_DECODE);
51

52
	return dc.state != DECODE_STATE_START;
53 54 55 56 57 58 59
}

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

67
	plugin->file_decode(decoder, path);
68

69 70
	assert(dc.state == DECODE_STATE_START ||
	       dc.state == DECODE_STATE_DECODE);
71

72
	return dc.state != DECODE_STATE_START;
73 74
}

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

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

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

101
	decoder.seeking = false;
102
	decoder.stream_tag_sent = false;
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_NONE) {
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
	ret = false;
133
	if (!song_is_file(song)) {
134 135 136
		unsigned int next = 0;

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

			plugin = NULL;
146 147 148
		}

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

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

195
	music_pipe_flush();
196

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

	dc.state = ret ? DECODE_STATE_STOP : DECODE_STATE_ERROR;
201 202
}

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

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

			dc.command = DECODE_COMMAND_NONE;
215
			notify_signal(&pc.notify);
216 217 218
			break;

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

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

	return NULL;
230
}
Warren Dukes's avatar
Warren Dukes committed
231

232
void decoder_thread_start(void)
233 234 235 236 237 238
{
	pthread_attr_t attr;
	pthread_t decoder_thread;

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