DecoderThread.cxx 11.5 KB
Newer Older
1
/*
2
 * Copyright (C) 2003-2013 The Music Player Daemon Project
3
 * 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 "config.h"
21
#include "DecoderThread.hxx"
22
#include "decoder_error.h"
23 24 25 26 27
#include "decoder_plugin.h"
#include "song.h"
#include "mpd_error.h"

extern "C" {
28
#include "decoder_control.h"
Max Kellermann's avatar
Max Kellermann committed
29
#include "decoder_internal.h"
Max Kellermann's avatar
Max Kellermann committed
30
#include "decoder_list.h"
31
#include "decoder_api.h"
32
#include "replay_gain_ape.h"
33 34
#include "input_stream.h"
#include "tag.h"
35
#include "mapper.h"
36
#include "uri.h"
37
}
Warren Dukes's avatar
Warren Dukes committed
38

39 40
#include <glib.h>

Max Kellermann's avatar
Max Kellermann committed
41
#include <unistd.h>
42
#include <stdio.h> /* for SEEK_SET */
Max Kellermann's avatar
Max Kellermann committed
43

44 45 46
#undef G_LOG_DOMAIN
#define G_LOG_DOMAIN "decoder_thread"

47 48 49 50 51 52 53 54 55 56 57 58 59
/**
 * Marks the current decoder command as "finished" and notifies the
 * player thread.
 *
 * @param dc the #decoder_control object; must be locked
 */
static void
decoder_command_finished_locked(struct decoder_control *dc)
{
	assert(dc->command != DECODE_COMMAND_NONE);

	dc->command = DECODE_COMMAND_NONE;

60
	g_cond_signal(dc->client_cond);
61 62
}

63 64 65 66 67 68 69 70
/**
 * Opens the input stream with input_stream_open(), and waits until
 * the stream gets ready.  If a decoder STOP command is received
 * during that, it cancels the operation (but does not close the
 * stream).
 *
 * Unlock the decoder before calling this function.
 *
71 72
 * @return an input_stream on success or if #DECODE_COMMAND_STOP is
 * received, NULL on error
73
 */
74 75
static struct input_stream *
decoder_input_stream_open(struct decoder_control *dc, const char *uri)
76
{
77
	GError *error = NULL;
78
	struct input_stream *is;
79

80
	is = input_stream_open(uri, dc->mutex, dc->cond, &error);
81
	if (is == NULL) {
82 83 84 85 86
		if (error != NULL) {
			g_warning("%s", error->message);
			g_error_free(error);
		}

87
		return NULL;
88
	}
89 90 91 92

	/* wait for the input stream to become ready; its metadata
	   will be available then */

93 94 95
	decoder_lock(dc);

	input_stream_update(is);
96
	while (!is->ready &&
97 98
	       dc->command != DECODE_COMMAND_STOP) {
		decoder_wait(dc);
99

100
		input_stream_update(is);
101 102
	}

103 104 105 106 107 108 109 110 111 112 113
	if (!input_stream_check(is, &error)) {
		decoder_unlock(dc);

		g_warning("%s", error->message);
		g_error_free(error);

		return NULL;
	}

	decoder_unlock(dc);

114
	return is;
115 116
}

117 118 119 120 121 122 123 124
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);
125 126
	assert(decoder->stream_tag == NULL);
	assert(decoder->decoder_tag == NULL);
127 128
	assert(input_stream != NULL);
	assert(input_stream->ready);
129
	assert(decoder->dc->state == DECODE_STATE_START);
130

131 132
	g_debug("probing plugin %s", plugin->name);

133 134 135
	if (decoder->dc->command == DECODE_COMMAND_STOP)
		return true;

136
	/* rewind the stream, so each plugin gets a fresh start */
137
	input_stream_seek(input_stream, 0, SEEK_SET, NULL);
138

139 140
	decoder_unlock(decoder->dc);

141
	decoder_plugin_stream_decode(plugin, decoder, input_stream);
142

143
	decoder_lock(decoder->dc);
144

145 146
	assert(decoder->dc->state == DECODE_STATE_START ||
	       decoder->dc->state == DECODE_STATE_DECODE);
147

148
	return decoder->dc->state != DECODE_STATE_START;
149 150 151 152 153 154 155
}

static bool
decoder_file_decode(const struct decoder_plugin *plugin,
		    struct decoder *decoder, const char *path)
{
	assert(plugin != NULL);
156
	assert(plugin->file_decode != NULL);
157
	assert(decoder != NULL);
158 159
	assert(decoder->stream_tag == NULL);
	assert(decoder->decoder_tag == NULL);
160
	assert(path != NULL);
161
	assert(g_path_is_absolute(path));
162
	assert(decoder->dc->state == DECODE_STATE_START);
163

164 165
	g_debug("probing plugin %s", plugin->name);

166 167 168
	if (decoder->dc->command == DECODE_COMMAND_STOP)
		return true;

169
	decoder_unlock(decoder->dc);
170

171
	decoder_plugin_file_decode(plugin, decoder, path);
172

173
	decoder_lock(decoder->dc);
174

175 176
	assert(decoder->dc->state == DECODE_STATE_START ||
	       decoder->dc->state == DECODE_STATE_DECODE);
177

178
	return decoder->dc->state != DECODE_STATE_START;
179 180
}

181 182 183 184 185 186
/**
 * Hack to allow tracking const decoder plugins in a GSList.
 */
static inline gpointer
deconst_plugin(const struct decoder_plugin *plugin)
{
187
	return const_cast<struct decoder_plugin *>(plugin);
188 189
}

190 191
/**
 * Try decoding a stream, using plugins matching the stream's MIME type.
192 193
 *
 * @param tried_r a list of plugins which were tried
194 195
 */
static bool
196 197
decoder_run_stream_mime_type(struct decoder *decoder, struct input_stream *is,
			     GSList **tried_r)
198
{
199 200
	assert(tried_r != NULL);

201 202 203 204 205 206
	const struct decoder_plugin *plugin;
	unsigned int next = 0;

	if (is->mime == NULL)
		return false;

207 208 209 210 211 212 213 214 215
	while ((plugin = decoder_plugin_from_mime_type(is->mime, next++))) {
		if (plugin->stream_decode == NULL)
			continue;

		if (g_slist_find(*tried_r, plugin) != NULL)
			/* don't try a plugin twice */
			continue;

		if (decoder_stream_decode(plugin, decoder, is))
216 217
			return true;

218 219 220
		*tried_r = g_slist_prepend(*tried_r, deconst_plugin(plugin));
	}

221 222 223 224 225 226
	return false;
}

/**
 * Try decoding a stream, using plugins matching the stream's URI
 * suffix.
227 228
 *
 * @param tried_r a list of plugins which were tried
229 230 231
 */
static bool
decoder_run_stream_suffix(struct decoder *decoder, struct input_stream *is,
232
			  const char *uri, GSList **tried_r)
233
{
234 235
	assert(tried_r != NULL);

236
	const char *suffix = uri_get_suffix(uri);
237
	const struct decoder_plugin *plugin = NULL;
238 239 240 241

	if (suffix == NULL)
		return false;

242 243 244 245 246 247 248 249 250
	while ((plugin = decoder_plugin_from_suffix(suffix, plugin)) != NULL) {
		if (plugin->stream_decode == NULL)
			continue;

		if (g_slist_find(*tried_r, plugin) != NULL)
			/* don't try a plugin twice */
			continue;

		if (decoder_stream_decode(plugin, decoder, is))
251 252
			return true;

253 254 255
		*tried_r = g_slist_prepend(*tried_r, deconst_plugin(plugin));
	}

256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
	return false;
}

/**
 * Try decoding a stream, using the fallback plugin.
 */
static bool
decoder_run_stream_fallback(struct decoder *decoder, struct input_stream *is)
{
	const struct decoder_plugin *plugin;

	plugin = decoder_plugin_from_name("mad");
	return plugin != NULL && plugin->stream_decode != NULL &&
		decoder_stream_decode(plugin, decoder, is);
}

/**
 * Try decoding a stream.
 */
static bool
276
decoder_run_stream(struct decoder *decoder, const char *uri)
277
{
278
	struct decoder_control *dc = decoder->dc;
279
	struct input_stream *input_stream;
280 281 282 283
	bool success;

	decoder_unlock(dc);

284 285
	input_stream = decoder_input_stream_open(dc, uri);
	if (input_stream == NULL) {
286 287 288 289 290 291
		decoder_lock(dc);
		return false;
	}

	decoder_lock(dc);

292 293
	GSList *tried = NULL;

294 295
	success = dc->command == DECODE_COMMAND_STOP ||
		/* first we try mime types: */
296
		decoder_run_stream_mime_type(decoder, input_stream, &tried) ||
297
		/* if that fails, try suffix matching the URL: */
298 299
		decoder_run_stream_suffix(decoder, input_stream, uri,
					  &tried) ||
300 301
		/* fallback to mp3: this is needed for bastard streams
		   that don't have a suffix or set the mimeType */
302 303
		(tried == NULL &&
		 decoder_run_stream_fallback(decoder, input_stream));
304

305 306
	g_slist_free(tried);

307
	decoder_unlock(dc);
308
	input_stream_close(input_stream);
309 310 311
	decoder_lock(dc);

	return success;
312 313
}

314 315 316 317 318 319 320 321 322 323 324 325
/**
 * Attempt to load replay gain data, and pass it to
 * decoder_replay_gain().
 */
static void
decoder_load_replay_gain(struct decoder *decoder, const char *path_fs)
{
	struct replay_gain_info info;
	if (replay_gain_ape_read(path_fs, &info))
		decoder_replay_gain(decoder, &info);
}

326 327 328 329
/**
 * Try decoding a file.
 */
static bool
330
decoder_run_file(struct decoder *decoder, const char *path_fs)
331
{
332
	struct decoder_control *dc = decoder->dc;
333
	const char *suffix = uri_get_suffix(path_fs);
334
	const struct decoder_plugin *plugin = NULL;
335 336 337 338

	if (suffix == NULL)
		return false;

339
	decoder_unlock(dc);
340

341 342
	decoder_load_replay_gain(decoder, path_fs);

343
	while ((plugin = decoder_plugin_from_suffix(suffix, plugin)) != NULL) {
344
		if (plugin->file_decode != NULL) {
345
			decoder_lock(dc);
346 347 348 349

			if (decoder_file_decode(plugin, decoder, path_fs))
				return true;

350
			decoder_unlock(dc);
351
		} else if (plugin->stream_decode != NULL) {
352
			struct input_stream *input_stream;
353
			bool success;
354

355 356
			input_stream = decoder_input_stream_open(dc, path_fs);
			if (input_stream == NULL)
357
				continue;
358

359
			decoder_lock(dc);
360

361
			success = decoder_stream_decode(plugin, decoder,
362
							input_stream);
363

364
			decoder_unlock(dc);
365

366
			input_stream_close(input_stream);
367 368 369 370 371

			if (success) {
				decoder_lock(dc);
				return true;
			}
372 373 374
		}
	}

375
	decoder_lock(dc);
376 377 378
	return false;
}

379 380 381
static void
decoder_run_song(struct decoder_control *dc,
		 const struct song *song, const char *uri)
Avuton Olrich's avatar
Avuton Olrich committed
382
{
383
	decoder decoder(dc, dc->start_ms > 0);
384
	int ret;
385

386
	decoder.timestamp = 0.0;
387
	decoder.seeking = false;
388 389
	decoder.song_tag = song->tag != NULL && song_is_file(song)
		? tag_dup(song->tag) : NULL;
390 391
	decoder.stream_tag = NULL;
	decoder.decoder_tag = NULL;
392
	decoder.chunk = NULL;
393

394
	dc->state = DECODE_STATE_START;
395

396
	decoder_command_finished_locked(dc);
397

398 399
	pcm_convert_init(&decoder.conv_state);

400
	ret = song_is_file(song)
401 402
		? decoder_run_file(&decoder, uri)
		: decoder_run_stream(&decoder, uri);
Warren Dukes's avatar
Warren Dukes committed
403

404
	decoder_unlock(dc);
405

406 407
	pcm_convert_deinit(&decoder.conv_state);

408
	/* flush the last chunk */
409

410 411
	if (decoder.chunk != NULL)
		decoder_flush_chunk(&decoder);
412

413 414 415
	if (decoder.song_tag != NULL)
		tag_free(decoder.song_tag);

416 417 418 419 420 421
	if (decoder.stream_tag != NULL)
		tag_free(decoder.stream_tag);

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

422
	decoder_lock(dc);
423

424 425 426 427 428 429 430 431 432 433 434 435 436 437
	if (ret)
		dc->state = DECODE_STATE_STOP;
	else {
		dc->state = DECODE_STATE_ERROR;

		const char *error_uri = song->uri;
		char *allocated = uri_remove_auth(error_uri);
		if (allocated != NULL)
			error_uri = allocated;

		dc->error = g_error_new(decoder_quark(), 0,
					"Failed to decode %s", error_uri);
		g_free(allocated);
	}
438 439
}

440 441
static void
decoder_run(struct decoder_control *dc)
442
{
443 444
	dc_clear_error(dc);

445
	const struct song *song = dc->song;
446 447
	char *uri;

448 449
	assert(song != NULL);

450 451
	if (song_is_file(song))
		uri = map_song_fs(song);
452 453
	else
		uri = song_get_uri(song);
454 455

	if (uri == NULL) {
456
		dc->state = DECODE_STATE_ERROR;
457 458 459
		dc->error = g_error_new(decoder_quark(), 0,
					"Failed to map song");

460
		decoder_command_finished_locked(dc);
461 462 463
		return;
	}

464
	decoder_run_song(dc, song, uri);
465 466 467 468
	g_free(uri);

}

469 470
static gpointer
decoder_task(gpointer arg)
Avuton Olrich's avatar
Avuton Olrich committed
471
{
472
	struct decoder_control *dc = (struct decoder_control *)arg;
473 474

	decoder_lock(dc);
475

476
	do {
477 478
		assert(dc->state == DECODE_STATE_STOP ||
		       dc->state == DECODE_STATE_ERROR);
479

480
		switch (dc->command) {
481
		case DECODE_COMMAND_START:
482 483 484
			dc_mixramp_start(dc, NULL);
			dc_mixramp_prev_end(dc, dc->mixramp_end);
			dc->mixramp_end = NULL; /* Don't free, it's copied above. */
485 486
			dc->replay_gain_prev_db = dc->replay_gain_db;
			dc->replay_gain_db = 0;
487 488 489

                        /* fall through */

490
		case DECODE_COMMAND_SEEK:
491
			decoder_run(dc);
492 493 494
			break;

		case DECODE_COMMAND_STOP:
495
			decoder_command_finished_locked(dc);
496 497 498
			break;

		case DECODE_COMMAND_NONE:
499
			decoder_wait(dc);
500
			break;
Warren Dukes's avatar
Warren Dukes committed
501
		}
502
	} while (dc->command != DECODE_COMMAND_NONE || !dc->quit);
503

504
	decoder_unlock(dc);
505

506
	return NULL;
507
}
Warren Dukes's avatar
Warren Dukes committed
508

509 510
void
decoder_thread_start(struct decoder_control *dc)
511
{
512
	GError *e = NULL;
513

514
	assert(dc->thread == NULL);
515

516 517
	dc->quit = false;

518 519
	dc->thread = g_thread_create(decoder_task, dc, true, &e);
	if (dc->thread == NULL)
520
		MPD_ERROR("Failed to spawn decoder task: %s", e->message);
Warren Dukes's avatar
Warren Dukes committed
521
}