aboutsummaryrefslogtreecommitdiffstats
path: root/src/decode.c
blob: dd1ea9396253708348d7adc88321ff89c501e03e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
/* the Music Player Daemon (MPD)
 * Copyright (C) 2003-2007 by Warren Dukes (warren.dukes@gmail.com)
 * 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
 */

#include "decode.h"
#include "outputBuffer.h"
#include "player_error.h"
#include "playlist.h"
#include "pcm_utils.h"
#include "path.h"
#include "log.h"
#include "ls.h"
#include "condition.h"

static struct condition dc_action_cond = STATIC_COND_INITIALIZER;
static struct condition dc_halt_cond = STATIC_COND_INITIALIZER;

struct decoder_control dc; /* ugh, global for now... */

/* blocking, waits until the signaled thread has replied */
void dc_trigger_action(enum dc_action action, float seek_where)
{
	assert(!pthread_equal(pthread_self(), dc.thread));
	/* assert(pthread_equal(pthread_self(), main_thread)); */
	assert(action != DC_ACTION_NONE);

	/* DEBUG(__FILE__ ":%s %d\n", __func__,__LINE__); */
	cond_enter(&dc_action_cond);
	assert(dc.action == DC_ACTION_NONE);
	dc.seek_where = (action == DC_ACTION_SEEK) ? seek_where : 0;
	dc.action = action;
	do {
		/* DEBUG(__FILE__ ":%s %d\n", __func__,__LINE__); */
		cond_signal(&dc_halt_cond); /* blind signal w/o lock */
		/* DEBUG(__FILE__ ":%s %d\n", __func__,__LINE__); */
		cond_timedwait(&dc_action_cond, 10);
		/* DEBUG(__FILE__ ":%s %d\n", __func__,__LINE__); */
	} while (dc.action != DC_ACTION_NONE); /* spurious wakeup protection */
	/* DEBUG(__FILE__ ":%s %d\n", __func__,__LINE__); */
	cond_leave(&dc_action_cond);
	/* DEBUG(__FILE__ ":%s %d\n", __func__,__LINE__); */
}

static void take_action(void)
{
	assert(pthread_equal(pthread_self(), dc.thread));
	assert(dc.state == DC_STATE_STOP);
	/* DEBUG("%s dc.action(%d): %d\n", __func__,__LINE__, dc.action); */
	cond_enter(&dc_action_cond);
	/* DEBUG("%s dc.action(%d): %d\n", __func__,__LINE__, dc.action); */

	switch (dc.action) {
	case DC_ACTION_NONE: goto out;
	case DC_ACTION_START:
	case DC_ACTION_SEEK:
		dc.state = DC_STATE_DECODE;
		return;
	case DC_ACTION_STOP: dc.state = DC_STATE_STOP; break;
	case DC_ACTION_QUIT: dc.state = DC_STATE_QUIT;
	}
	dc.action = DC_ACTION_NONE;
	cond_signal(&dc_action_cond);
out:
	assert(dc.action == DC_ACTION_NONE);
	cond_leave(&dc_action_cond);
}

/*
 * This will grab an action, but will not signal the calling thread.
 * dc_action_end() is required to signal the calling thread
 */
void dc_action_begin(void)
{
	enum dc_action ret = dc.action;

	assert(pthread_equal(pthread_self(), dc.thread));

	if (ret != DC_ACTION_NONE) {
		/* DEBUG(__FILE__ ":%s %d\n", __func__,__LINE__); */
		cond_enter(&dc_action_cond);
		/* dc.action can't get set to NONE outside this thread */
		assert(dc.action == ret);
		if (ret == DC_ACTION_SEEK)
			ob_seek_start();
	}
}

void dc_action_end(void)
{
	assert(pthread_equal(pthread_self(), dc.thread));
	assert(dc.action != DC_ACTION_NONE);
	/* DEBUG("DONE ACTION %d\n", dc.action); */
	if (dc.action == DC_ACTION_SEEK)
		ob_seek_finish();
	dc.action = DC_ACTION_NONE;

	cond_signal(&dc_action_cond);
	cond_leave(&dc_action_cond);
}

void dc_action_seek_fail(enum seek_err_type err_type)
{
	assert(pthread_equal(pthread_self(), dc.thread));
	cond_enter(&dc_action_cond);
	assert(dc.action == DC_ACTION_SEEK);
	dc.action = DC_ACTION_NONE;
	dc.seek_where = err_type;
	cond_signal(&dc_action_cond);
	cond_leave(&dc_action_cond);
}

/* Returns true if we need to interrupt the decoding inside an inputPlugin */
int dc_intr(void)
{
	if (!pthread_equal(pthread_self(), dc.thread))
		return 0;
	switch (dc.action) {
	case DC_ACTION_NONE:
	case DC_ACTION_SEEK:
		return 0;
	default:
		/* DEBUG(__FILE__": %s %d\n", __func__, __LINE__); */
		/* DEBUG("dc.action: %d\n", (int)dc.action); */
		return 1;
	}
}

int dc_seek(void)
{
	if (pthread_equal(pthread_self(), dc.thread))
		return (dc.action == DC_ACTION_SEEK);
	return 0;
}

static void finalize_per_track_actions(void)
{
	enum dc_action action;
	/* DEBUG(":%s dc.action(%d): %d\n", __func__,__LINE__, dc.action); */
	assert(pthread_equal(pthread_self(), dc.thread));
	cond_enter(&dc_action_cond);
	dc.state = DC_STATE_STOP;
	action = dc.action;
	dc.action = DC_ACTION_NONE;

	if (action == DC_ACTION_STOP) {
		cond_signal(&dc_action_cond);
	} else if (action == DC_ACTION_SEEK) {
		dc.seek_where = DC_SEEK_MISMATCH;
		cond_signal(&dc_action_cond);
	}
	cond_leave(&dc_action_cond);
	/* DEBUG(":%s dc.action(%d): %d\n", __func__,__LINE__, dc.action); */
}

static int decode_start(void)
{
	int err = -1;
	int close_instream = 1;
	InputStream is;
	InputPlugin *plugin = NULL;
	char path_max_fs[MPD_PATH_MAX];
	assert(pthread_equal(pthread_self(), dc.thread));
	assert(dc.state == DC_STATE_DECODE);
	assert(*dc.utf8url);

	switch (dc.action) {
	case DC_ACTION_START:
		dc_action_end();
		break;
	case DC_ACTION_SEEK:
		/* DEBUG("dc.seek_where(%d): %f\n", __LINE__, dc.seek_where); */
		/* make sure dc_action_start() works inside inputPlugins: */
		cond_leave(&dc_action_cond);
		/* DEBUG("dc.action(%d) %d\n", __LINE__, dc.action); */
		break;
	default: assert("unknown action!" && 0);
	}

	if (isRemoteUrl(dc.utf8url)) {
		pathcpy_trunc(path_max_fs, dc.utf8url);
	} else {
		rmp2amp_r(path_max_fs,
		          utf8_to_fs_charset(path_max_fs, dc.utf8url));
	}

	if (openInputStream(&is, path_max_fs) < 0) {
		DEBUG("couldn't open song: %s\n", path_max_fs);
		player_seterror(PLAYER_ERROR_FILENOTFOUND, dc.utf8url);
		return err;
	}

	if (isRemoteUrl(dc.utf8url)) {
		unsigned int next = 0;

		/* first we try mime types: */
		while (err && (plugin = getInputPluginFromMimeType(is.mime, next++))) {
			if (!plugin->streamDecodeFunc)
				continue;
			if (!(plugin->streamTypes & INPUT_PLUGIN_STREAM_URL))
				continue;
			if (plugin->tryDecodeFunc
			    && !plugin->tryDecodeFunc(&is))
				continue;
			err = plugin->streamDecodeFunc(&is);
			break;
		}

		/* if that fails, try suffix matching the URL: */
		if (plugin == NULL) {
			const char *s = getSuffix(dc.utf8url);
			next = 0;
			while (err && (plugin = getInputPluginFromSuffix(s, next++))) {
				if (!plugin->streamDecodeFunc)
					continue;
				if (!(plugin->streamTypes &
				      INPUT_PLUGIN_STREAM_URL))
					continue;
				if (plugin->tryDecodeFunc &&
				    !plugin->tryDecodeFunc(&is))
					continue;
				err = plugin->streamDecodeFunc(&is);
				break;
			}
		}
		/* fallback to mp3: */
		/* this is needed for bastard streams that don't have a suffix
		   or set the mimeType */
		if (plugin == NULL) {
			/* we already know our mp3Plugin supports streams, no
			 * need to check for stream{Types,DecodeFunc} */
			if ((plugin = getInputPluginFromName("mp3")))
				err = plugin->streamDecodeFunc(&is);
		}
	} else {
		unsigned int next = 0;
		const char *s = getSuffix(dc.utf8url);
		while (err && (plugin = getInputPluginFromSuffix(s, next++))) {
			if (!plugin->streamTypes & INPUT_PLUGIN_STREAM_FILE)
				continue;

			if (plugin->tryDecodeFunc &&
			    !plugin->tryDecodeFunc(&is))
				continue;

			if (plugin->fileDecodeFunc) {
				closeInputStream(&is);
				close_instream = 0;
				err = plugin->fileDecodeFunc(path_max_fs);
				break;
			} else if (plugin->streamDecodeFunc) {
				err = plugin->streamDecodeFunc(&is);
				break;
			}
		}
	}

	if (err) {
		if (plugin)
			player_seterror(PLAYER_ERROR_SYSTEM, dc.utf8url);
		else
			player_seterror(PLAYER_ERROR_UNKTYPE, dc.utf8url);
	}
	if (player_errno)
		ERROR("player_error: %s\n", player_strerror());
	if (close_instream)
		closeInputStream(&is);
	return err;
}

static void * decoder_task(mpd_unused void *arg)
{
	assert(pthread_equal(pthread_self(), dc.thread));
	cond_enter(&dc_halt_cond);
	while (1) {
		take_action();
		switch (dc.state) {
		case DC_STATE_STOP:
			/* DEBUG(__FILE__": halted %d\n", __LINE__); */
			cond_wait(&dc_halt_cond);
			/* DEBUG(__FILE__": unhalted %d\n", __LINE__); */
			break;
		case DC_STATE_DECODE:
			/* DEBUG(__FILE__": %s %d\n", __func__, __LINE__); */
			/* DEBUG("dc.action: %d\n", (int)dc.action); */
			if (playlist_queued_url(dc.utf8url)) {
				int err;

				ob_advance_sequence();
				DEBUG("decoding song: %s\n", dc.utf8url);
				err = decode_start();
				DEBUG("DONE decoding song: %s\n", dc.utf8url);
				if (err)
					ob_trigger_action(OB_ACTION_RESET);
				else
					ob_flush();
				dc.utf8url[0] = '\0';
			}
			finalize_per_track_actions();
			playlist_queue_next();
			break;
		case DC_STATE_QUIT:
			goto out;
		}
	}
out:
	cond_leave(&dc_halt_cond);
	assert(dc.state == DC_STATE_QUIT);
	return NULL;
}

void decoder_init(void)
{
	pthread_attr_t attr;
	assert(!dc.thread);

	pthread_attr_init(&attr);
	pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
	if (pthread_create(&dc.thread, &attr, decoder_task, NULL))
		FATAL("Failed to spawn decoder task: %s\n", strerror(errno));
}

int dc_try_unhalt(void)
{
	assert(!pthread_equal(pthread_self(), dc.thread));
	return cond_signal_trysync(&dc_halt_cond);
}

void dc_halt(void)
{
	assert(pthread_equal(pthread_self(), dc.thread));
	cond_wait(&dc_halt_cond);
}