decoder_thread.c 5.32 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

Warren Dukes's avatar
Warren Dukes committed
25 26
#include "path.h"
#include "log.h"
27
#include "ls.h"
Warren Dukes's avatar
Warren Dukes committed
28

29
static void decodeStart(void)
Avuton Olrich's avatar
Avuton Olrich committed
30
{
Max Kellermann's avatar
Max Kellermann committed
31
	struct decoder decoder;
32
	int ret;
33
	int close_instream = 1;
34
	InputStream inStream;
35
	struct decoder_plugin *plugin = NULL;
36 37
	char path_max_fs[MPD_PATH_MAX];
	char path_max_utf8[MPD_PATH_MAX];
38

39
	if (!get_song_url(path_max_utf8, dc.next_song)) {
40
		dc.error = DECODE_ERROR_FILE;
41 42 43 44 45
		goto stop_no_close;
	}
	if (!isRemoteUrl(path_max_utf8)) {
		rmp2amp_r(path_max_fs,
		          utf8_to_fs_charset(path_max_fs, path_max_utf8));
Max Kellermann's avatar
Max Kellermann committed
46 47
	} else
		pathcpy_trunc(path_max_fs, path_max_utf8);
Warren Dukes's avatar
Warren Dukes committed
48

49
	dc.current_song = dc.next_song; /* NEED LOCK */
50
	if (openInputStream(&inStream, path_max_fs) < 0) {
51
		dc.error = DECODE_ERROR_FILE;
52
		goto stop_no_close;
53
	}
Warren Dukes's avatar
Warren Dukes committed
54

55 56
	decoder.seeking = 0;

57
	dc.state = DECODE_STATE_START;
58
	dc.command = DECODE_COMMAND_NONE;
59

Max Kellermann's avatar
Max Kellermann committed
60 61 62 63 64 65 66 67 68 69 70 71
	/* wait for the input stream to become ready; its metadata
	   will be available then */

	while (!inStream.ready) {
		if (dc.command != DECODE_COMMAND_NONE)
			goto stop;

		ret = bufferInputStream(&inStream);
		if (ret < 0)
			goto stop;
	}

72
	/* for http streams, seekable is determined in bufferInputStream */
73
	dc.seekable = inStream.seekable;
Max Kellermann's avatar
Max Kellermann committed
74

75
	if (dc.command == DECODE_COMMAND_STOP)
76
		goto stop;
77

Avuton Olrich's avatar
Avuton Olrich committed
78
	ret = DECODE_ERROR_UNKTYPE;
79
	if (isRemoteUrl(path_max_utf8)) {
80 81 82
		unsigned int next = 0;

		/* first we try mime types: */
83
		while (ret && (plugin = decoder_plugin_from_mime_type(inStream.mime, next++))) {
84
			if (!plugin->stream_decode_func)
85
				continue;
86
			if (!(plugin->stream_types & INPUT_PLUGIN_STREAM_URL))
87
				continue;
88 89
			if (plugin->try_decode_func
			    && !plugin->try_decode_func(&inStream))
90
				continue;
91
			ret = plugin->stream_decode_func(&decoder, &inStream);
92 93 94 95
			break;
		}

		/* if that fails, try suffix matching the URL: */
Avuton Olrich's avatar
Avuton Olrich committed
96
		if (plugin == NULL) {
97
			const char *s = getSuffix(path_max_utf8);
98
			next = 0;
99
			while (ret && (plugin = decoder_plugin_from_suffix(s, next++))) {
100
				if (!plugin->stream_decode_func)
101
					continue;
102
				if (!(plugin->stream_types &
Avuton Olrich's avatar
Avuton Olrich committed
103
				      INPUT_PLUGIN_STREAM_URL))
104
					continue;
105 106
				if (plugin->try_decode_func &&
				    !plugin->try_decode_func(&inStream))
107
					continue;
Max Kellermann's avatar
Max Kellermann committed
108
				decoder.plugin = plugin;
109 110
				ret = plugin->stream_decode_func(&decoder,
								 &inStream);
111 112
				break;
			}
113
		}
114
		/* fallback to mp3: */
115
		/* this is needed for bastard streams that don't have a suffix
Avuton Olrich's avatar
Avuton Olrich committed
116 117
		   or set the mimeType */
		if (plugin == NULL) {
118 119
			/* we already know our mp3Plugin supports streams, no
			 * need to check for stream{Types,DecodeFunc} */
120
			if ((plugin = decoder_plugin_from_name("mp3"))) {
Max Kellermann's avatar
Max Kellermann committed
121
				decoder.plugin = plugin;
122 123
				ret = plugin->stream_decode_func(&decoder,
								 &inStream);
124
			}
125
		}
Avuton Olrich's avatar
Avuton Olrich committed
126
	} else {
127
		unsigned int next = 0;
128
		const char *s = getSuffix(path_max_utf8);
129
		while (ret && (plugin = decoder_plugin_from_suffix(s, next++))) {
130
			if (!plugin->stream_types & INPUT_PLUGIN_STREAM_FILE)
131
				continue;
132

133 134
			if (plugin->try_decode_func &&
			    !plugin->try_decode_func(&inStream))
135
				continue;
Avuton Olrich's avatar
Avuton Olrich committed
136

137
			if (plugin->file_decode_func) {
138
				closeInputStream(&inStream);
139
				close_instream = 0;
Max Kellermann's avatar
Max Kellermann committed
140
				decoder.plugin = plugin;
141 142
				ret = plugin->file_decode_func(&decoder,
							       path_max_fs);
143
				break;
144
			} else if (plugin->stream_decode_func) {
Max Kellermann's avatar
Max Kellermann committed
145
				decoder.plugin = plugin;
146 147
				ret = plugin->stream_decode_func(&decoder,
								 &inStream);
148
				break;
149 150
			}
		}
Avuton Olrich's avatar
Avuton Olrich committed
151
	}
Warren Dukes's avatar
Warren Dukes committed
152

Avuton Olrich's avatar
Avuton Olrich committed
153
	if (ret < 0 || ret == DECODE_ERROR_UNKTYPE) {
154
		if (ret != DECODE_ERROR_UNKTYPE)
155
			dc.error = DECODE_ERROR_FILE;
156
		else
157
			dc.error = DECODE_ERROR_UNKTYPE;
158
	}
159 160 161 162 163

stop:
	if (close_instream)
		closeInputStream(&inStream);
stop_no_close:
164
	dc.state = DECODE_STATE_STOP;
165
	dc.command = DECODE_COMMAND_NONE;
166 167
}

168
static void * decoder_task(mpd_unused void *arg)
Avuton Olrich's avatar
Avuton Olrich committed
169
{
170
	notify_enter(&dc.notify);
171

172
	while (1) {
173 174
		assert(dc.state == DECODE_STATE_STOP);

175 176
		if (dc.command == DECODE_COMMAND_START ||
		    dc.command == DECODE_COMMAND_SEEK) {
177
			decodeStart();
178
		} else if (dc.command == DECODE_COMMAND_STOP) {
179 180
			dc.command = DECODE_COMMAND_NONE;
			notify_signal(&pc.notify);
181
		} else {
182 183
			notify_wait(&dc.notify);
			notify_signal(&pc.notify);
Warren Dukes's avatar
Warren Dukes committed
184 185
		}
	}
186 187

	return NULL;
188
}
Warren Dukes's avatar
Warren Dukes committed
189

190
void decoder_thread_start(void)
191 192 193 194 195 196
{
	pthread_attr_t attr;
	pthread_t decoder_thread;

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