DecoderThread.cxx 11.1 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 23
#include "DecoderControl.hxx"
#include "DecoderInternal.hxx"
24 25
#include "DecoderError.hxx"
#include "DecoderPlugin.hxx"
26
#include "Song.hxx"
27
#include "system/FatalError.hxx"
Max Kellermann's avatar
Max Kellermann committed
28
#include "Mapper.hxx"
29
#include "fs/Path.hxx"
30
#include "DecoderAPI.hxx"
Max Kellermann's avatar
Max Kellermann committed
31
#include "Tag.hxx"
32
#include "InputStream.hxx"
33
#include "DecoderList.hxx"
Max Kellermann's avatar
Max Kellermann committed
34
#include "util/UriUtil.hxx"
Max Kellermann's avatar
Max Kellermann committed
35
#include "ApeReplayGain.hxx"
Warren Dukes's avatar
Warren Dukes committed
36

37 38
#include <glib.h>

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

42 43 44
#undef G_LOG_DOMAIN
#define G_LOG_DOMAIN "decoder_thread"

45 46 47 48 49 50 51 52 53 54 55 56 57
/**
 * 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;

58
	dc->client_cond.signal();
59 60
}

61 62 63 64 65 66 67 68
/**
 * 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.
 *
69 70
 * @return an input_stream on success or if #DECODE_COMMAND_STOP is
 * received, NULL on error
71
 */
72 73
static struct input_stream *
decoder_input_stream_open(struct decoder_control *dc, const char *uri)
74
{
75
	GError *error = NULL;
76
	struct input_stream *is;
77

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

85
		return NULL;
86
	}
87 88 89 90

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

91
	dc->Lock();
92 93

	input_stream_update(is);
94
	while (!is->ready &&
95
	       dc->command != DECODE_COMMAND_STOP) {
96
		dc->Wait();
97

98
		input_stream_update(is);
99 100
	}

101
	if (!input_stream_check(is, &error)) {
102
		dc->Unlock();
103 104 105 106 107 108 109

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

		return NULL;
	}

110
	dc->Unlock();
111

112
	return is;
113 114
}

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

129 130
	g_debug("probing plugin %s", plugin->name);

131 132 133
	if (decoder->dc->command == DECODE_COMMAND_STOP)
		return true;

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

137
	decoder->dc->Unlock();
138

139
	decoder_plugin_stream_decode(plugin, decoder, input_stream);
140

141
	decoder->dc->Lock();
142

143 144
	assert(decoder->dc->state == DECODE_STATE_START ||
	       decoder->dc->state == DECODE_STATE_DECODE);
145

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

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

162 163
	g_debug("probing plugin %s", plugin->name);

164 165 166
	if (decoder->dc->command == DECODE_COMMAND_STOP)
		return true;

167
	decoder->dc->Unlock();
168

169
	decoder_plugin_file_decode(plugin, decoder, path);
170

171
	decoder->dc->Lock();
172

173 174
	assert(decoder->dc->state == DECODE_STATE_START ||
	       decoder->dc->state == DECODE_STATE_DECODE);
175

176
	return decoder->dc->state != DECODE_STATE_START;
177 178
}

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

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

199 200 201
	const struct decoder_plugin *plugin;
	unsigned int next = 0;

202
	if (is->mime.empty())
203 204
		return false;

205 206
	while ((plugin = decoder_plugin_from_mime_type(is->mime.c_str(),
						       next++))) {
207 208 209 210 211 212 213 214
		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))
215 216
			return true;

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

220 221 222 223 224 225
	return false;
}

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

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

	if (suffix == NULL)
		return false;

241 242 243 244 245 246 247 248 249
	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))
250 251
			return true;

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

255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
	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
275
decoder_run_stream(struct decoder *decoder, const char *uri)
276
{
277
	struct decoder_control *dc = decoder->dc;
278
	struct input_stream *input_stream;
279 280
	bool success;

281
	dc->Unlock();
282

283 284
	input_stream = decoder_input_stream_open(dc, uri);
	if (input_stream == NULL) {
285
		dc->Lock();
286 287 288
		return false;
	}

289
	dc->Lock();
290

291 292
	GSList *tried = NULL;

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

304 305
	g_slist_free(tried);

306
	dc->Unlock();
307
	input_stream_close(input_stream);
308
	dc->Lock();
309 310

	return success;
311 312
}

313 314 315 316 317 318 319 320 321 322 323 324
/**
 * 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);
}

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

	if (suffix == NULL)
		return false;

338
	dc->Unlock();
339

340 341
	decoder_load_replay_gain(decoder, path_fs);

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

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

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

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

358
			dc->Lock();
359

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

363
			dc->Unlock();
364

365
			input_stream_close(input_stream);
366 367

			if (success) {
368
				dc->Lock();
369 370
				return true;
			}
371 372 373
		}
	}

374
	dc->Lock();
375 376 377
	return false;
}

378 379
static void
decoder_run_song(struct decoder_control *dc,
380
		 const Song *song, const char *uri)
Avuton Olrich's avatar
Avuton Olrich committed
381
{
382
	decoder decoder(dc, dc->start_ms > 0,
383
			song->tag != NULL && song->IsFile()
Max Kellermann's avatar
Max Kellermann committed
384
			? new Tag(*song->tag) : nullptr);
385
	int ret;
386

387
	dc->state = DECODE_STATE_START;
388

389
	decoder_command_finished_locked(dc);
390

391
	ret = song->IsFile()
392 393
		? decoder_run_file(&decoder, uri)
		: decoder_run_stream(&decoder, uri);
Warren Dukes's avatar
Warren Dukes committed
394

395
	dc->Unlock();
396

397
	/* flush the last chunk */
398

399 400
	if (decoder.chunk != NULL)
		decoder_flush_chunk(&decoder);
401

402
	dc->Lock();
403

404 405 406 407 408 409 410 411 412 413 414 415 416 417
	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);
	}
418

419
	dc->client_cond.signal();
420 421
}

422 423
static void
decoder_run(struct decoder_control *dc)
424
{
425
	dc->ClearError();
426

427
	const Song *song = dc->song;
428 429
	char *uri;

430 431
	assert(song != NULL);

432
	if (song->IsFile())
433
		uri = map_song_fs(song).Steal();
434
	else
435
		uri = song->GetURI();
436 437

	if (uri == NULL) {
438
		dc->state = DECODE_STATE_ERROR;
439 440 441
		dc->error = g_error_new(decoder_quark(), 0,
					"Failed to map song");

442
		decoder_command_finished_locked(dc);
443 444 445
		return;
	}

446
	decoder_run_song(dc, song, uri);
447 448 449 450
	g_free(uri);

}

451 452
static gpointer
decoder_task(gpointer arg)
Avuton Olrich's avatar
Avuton Olrich committed
453
{
454
	struct decoder_control *dc = (struct decoder_control *)arg;
455

456
	dc->Lock();
457

458
	do {
459 460
		assert(dc->state == DECODE_STATE_STOP ||
		       dc->state == DECODE_STATE_ERROR);
461

462
		switch (dc->command) {
463
		case DECODE_COMMAND_START:
464 465
			dc->MixRampStart(nullptr);
			dc->MixRampPrevEnd(dc->mixramp_end);
466
			dc->mixramp_end = NULL; /* Don't free, it's copied above. */
467 468
			dc->replay_gain_prev_db = dc->replay_gain_db;
			dc->replay_gain_db = 0;
469 470 471

                        /* fall through */

472
		case DECODE_COMMAND_SEEK:
473
			decoder_run(dc);
474 475 476
			break;

		case DECODE_COMMAND_STOP:
477
			decoder_command_finished_locked(dc);
478 479 480
			break;

		case DECODE_COMMAND_NONE:
481
			dc->Wait();
482
			break;
Warren Dukes's avatar
Warren Dukes committed
483
		}
484
	} while (dc->command != DECODE_COMMAND_NONE || !dc->quit);
485

486
	dc->Unlock();
487

488
	return NULL;
489
}
Warren Dukes's avatar
Warren Dukes committed
490

491 492
void
decoder_thread_start(struct decoder_control *dc)
493
{
494
	assert(dc->thread == NULL);
495

496 497
	dc->quit = false;

498 499 500 501
#if GLIB_CHECK_VERSION(2,32,0)
	dc->thread = g_thread_new("thread", decoder_task, dc);
#else
	GError *e = NULL;
502 503
	dc->thread = g_thread_create(decoder_task, dc, true, &e);
	if (dc->thread == NULL)
504
		FatalError("Failed to spawn decoder task", e);
505
#endif
Warren Dukes's avatar
Warren Dukes committed
506
}