OutputThread.cxx 14.6 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2014 The Music Player Daemon Project
3
 * http://www.musicpd.org
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.
18 19
 */

20
#include "config.h"
21
#include "OutputThread.hxx"
Max Kellermann's avatar
Max Kellermann committed
22
#include "OutputInternal.hxx"
23
#include "OutputAPI.hxx"
24
#include "OutputError.hxx"
Max Kellermann's avatar
Max Kellermann committed
25
#include "pcm/PcmMix.hxx"
Max Kellermann's avatar
Max Kellermann committed
26
#include "notify.hxx"
27
#include "FilterInternal.hxx"
28
#include "filter/ConvertFilterPlugin.hxx"
29
#include "filter/ReplayGainFilterPlugin.hxx"
30
#include "PlayerControl.hxx"
31 32
#include "MusicPipe.hxx"
#include "MusicChunk.hxx"
33
#include "thread/Util.hxx"
34
#include "system/FatalError.hxx"
35
#include "util/Error.hxx"
36
#include "Log.hxx"
37
#include "Compiler.h"
38

39
#include <assert.h>
Max Kellermann's avatar
Max Kellermann committed
40
#include <string.h>
41

42 43 44 45
static void ao_command_finished(struct audio_output *ao)
{
	assert(ao->command != AO_COMMAND_NONE);
	ao->command = AO_COMMAND_NONE;
46

47
	ao->mutex.unlock();
Max Kellermann's avatar
Max Kellermann committed
48
	audio_output_client_notify.Signal();
49
	ao->mutex.lock();
50 51
}

52 53 54
static bool
ao_enable(struct audio_output *ao)
{
55
	Error error;
56
	bool success;
57 58 59 60

	if (ao->really_enabled)
		return true;

61
	ao->mutex.unlock();
62
	success = ao_plugin_enable(ao, error);
63
	ao->mutex.lock();
64
	if (!success) {
65 66 67
		FormatError(error,
			    "Failed to enable \"%s\" [%s]",
			    ao->name, ao->plugin->name);
68 69 70 71 72 73 74 75
		return false;
	}

	ao->really_enabled = true;
	return true;
}

static void
76
ao_close(struct audio_output *ao, bool drain);
77 78 79 80 81

static void
ao_disable(struct audio_output *ao)
{
	if (ao->open)
82
		ao_close(ao, false);
83 84 85

	if (ao->really_enabled) {
		ao->really_enabled = false;
86

87
		ao->mutex.unlock();
88
		ao_plugin_disable(ao);
89
		ao->mutex.lock();
90 91 92
	}
}

93 94
static AudioFormat
ao_filter_open(struct audio_output *ao, AudioFormat &format,
95
	       Error &error_r)
96
{
97
	assert(format.IsValid());
98

99
	/* the replay_gain filter cannot fail here */
100 101 102 103 104 105 106 107 108 109
	if (ao->replay_gain_filter != nullptr &&
	    !ao->replay_gain_filter->Open(format, error_r).IsDefined())
		return AudioFormat::Undefined();

	if (ao->other_replay_gain_filter != nullptr &&
	    !ao->other_replay_gain_filter->Open(format, error_r).IsDefined()) {
		if (ao->replay_gain_filter != nullptr)
			ao->replay_gain_filter->Close();
		return AudioFormat::Undefined();
	}
110

111 112
	const AudioFormat af = ao->filter->Open(format, error_r);
	if (!af.IsDefined()) {
113
		if (ao->replay_gain_filter != nullptr)
114
			ao->replay_gain_filter->Close();
115
		if (ao->other_replay_gain_filter != nullptr)
116
			ao->other_replay_gain_filter->Close();
117 118 119
	}

	return af;
120 121 122 123 124
}

static void
ao_filter_close(struct audio_output *ao)
{
125
	if (ao->replay_gain_filter != nullptr)
126
		ao->replay_gain_filter->Close();
127
	if (ao->other_replay_gain_filter != nullptr)
128
		ao->other_replay_gain_filter->Close();
129

130
	ao->filter->Close();
131 132
}

133 134 135 136
static void
ao_open(struct audio_output *ao)
{
	bool success;
137
	Error error;
138
	struct audio_format_string af_string;
139 140

	assert(!ao->open);
141 142
	assert(ao->pipe != nullptr);
	assert(ao->chunk == nullptr);
143
	assert(ao->in_audio_format.IsValid());
144

145
	ao->fail_timer.Reset();
146

147 148 149 150 151 152
	/* enable the device (just in case the last enable has failed) */

	if (!ao_enable(ao))
		/* still no luck */
		return;

153 154
	/* open the filter */

155
	const AudioFormat filter_audio_format =
156
		ao_filter_open(ao, ao->in_audio_format, error);
157
	if (!filter_audio_format.IsDefined()) {
158 159
		FormatError(error, "Failed to open filter for \"%s\" [%s]",
			    ao->name, ao->plugin->name);
160

161
		ao->fail_timer.Update();
162 163 164
		return;
	}

165
	assert(filter_audio_format.IsValid());
166

167 168
	ao->out_audio_format = filter_audio_format;
	ao->out_audio_format.ApplyMask(ao->config_audio_format);
169

170
	ao->mutex.unlock();
171
	success = ao_plugin_open(ao, ao->out_audio_format, error);
172
	ao->mutex.lock();
173 174 175 176

	assert(!ao->open);

	if (!success) {
177 178
		FormatError(error, "Failed to open \"%s\" [%s]",
			    ao->name, ao->plugin->name);
179

180
		ao_filter_close(ao);
181
		ao->fail_timer.Update();
182 183 184
		return;
	}

185 186 187 188 189 190
	if (!convert_filter_set(ao->convert_filter, ao->out_audio_format,
				error)) {
		FormatError(error, "Failed to convert for \"%s\" [%s]",
			    ao->name, ao->plugin->name);

		ao_filter_close(ao);
191
		ao->fail_timer.Update();
192 193
		return;
	}
194 195 196

	ao->open = true;

197 198 199 200
	FormatDebug(output_domain,
		    "opened plugin=%s name=\"%s\" audio_format=%s",
		    ao->plugin->name, ao->name,
		    audio_format_to_string(ao->out_audio_format, &af_string));
201

202
	if (ao->in_audio_format != ao->out_audio_format)
203 204 205
		FormatDebug(output_domain, "converting from %s",
			    audio_format_to_string(ao->in_audio_format,
						   &af_string));
206 207
}

208
static void
209
ao_close(struct audio_output *ao, bool drain)
210 211 212
{
	assert(ao->open);

213
	ao->pipe = nullptr;
214

215
	ao->chunk = nullptr;
216
	ao->open = false;
217

218
	ao->mutex.unlock();
219

220
	if (drain)
221
		ao_plugin_drain(ao);
222
	else
223
		ao_plugin_cancel(ao);
224

225
	ao_plugin_close(ao);
226
	ao_filter_close(ao);
227

228
	ao->mutex.lock();
229

230 231
	FormatDebug(output_domain, "closed plugin=%s name=\"%s\"",
		    ao->plugin->name, ao->name);
232 233
}

234 235 236
static void
ao_reopen_filter(struct audio_output *ao)
{
237
	Error error;
238

239
	ao_filter_close(ao);
240
	const AudioFormat filter_audio_format =
241
		ao_filter_open(ao, ao->in_audio_format, error);
242 243 244
	if (!filter_audio_format.IsDefined() ||
	    !convert_filter_set(ao->convert_filter, ao->out_audio_format,
				error)) {
245 246 247
		FormatError(error,
			    "Failed to open filter for \"%s\" [%s]",
			    ao->name, ao->plugin->name);
248 249 250 251 252

		/* this is a little code duplication fro ao_close(),
		   but we cannot call this function because we must
		   not call filter_close(ao->filter) again */

253
		ao->pipe = nullptr;
254

255
		ao->chunk = nullptr;
256
		ao->open = false;
257
		ao->fail_timer.Update();
258

259
		ao->mutex.unlock();
260
		ao_plugin_close(ao);
261
		ao->mutex.lock();
262 263 264 265 266

		return;
	}
}

267 268 269
static void
ao_reopen(struct audio_output *ao)
{
270
	if (!ao->config_audio_format.IsFullyDefined()) {
271
		if (ao->open) {
272
			const MusicPipe *mp = ao->pipe;
273
			ao_close(ao, true);
274 275 276 277 278 279 280
			ao->pipe = mp;
		}

		/* no audio format is configured: copy in->out, let
		   the output's open() method determine the effective
		   out_audio_format */
		ao->out_audio_format = ao->in_audio_format;
281
		ao->out_audio_format.ApplyMask(ao->config_audio_format);
282 283
	}

284 285 286 287 288
	if (ao->open)
		/* the audio format has changed, and all filters have
		   to be reconfigured */
		ao_reopen_filter(ao);
	else
289 290 291
		ao_open(ao);
}

292 293 294 295 296 297 298 299 300 301
/**
 * Wait until the output's delay reaches zero.
 *
 * @return true if playback should be continued, false if a command
 * was issued
 */
static bool
ao_wait(struct audio_output *ao)
{
	while (true) {
302
		unsigned delay = ao_plugin_delay(ao);
303 304 305
		if (delay == 0)
			return true;

306
		(void)ao->cond.timed_wait(ao->mutex, delay);
307 308 309 310 311 312

		if (ao->command != AO_COMMAND_NONE)
			return false;
	}
}

313
static const void *
314
ao_chunk_data(struct audio_output *ao, const struct music_chunk *chunk,
315
	      Filter *replay_gain_filter,
316
	      unsigned *replay_gain_serial_p,
317
	      size_t *length_r)
318
{
319
	assert(chunk != nullptr);
320 321
	assert(!chunk->IsEmpty());
	assert(chunk->CheckFormat(ao->in_audio_format));
322

323
	const void *data = chunk->data;
324 325 326 327
	size_t length = chunk->length;

	(void)ao;

328
	assert(length % ao->in_audio_format.GetFrameSize() == 0);
329

330
	if (length > 0 && replay_gain_filter != nullptr) {
331 332 333 334
		if (chunk->replay_gain_serial != *replay_gain_serial_p) {
			replay_gain_filter_set_info(replay_gain_filter,
						    chunk->replay_gain_serial != 0
						    ? &chunk->replay_gain_info
335
						    : nullptr);
336 337 338
			*replay_gain_serial_p = chunk->replay_gain_serial;
		}

339
		Error error;
340
		data = replay_gain_filter->FilterPCM(data, length,
341
						     &length, error);
342
		if (data == nullptr) {
343 344
			FormatError(error, "\"%s\" [%s] failed to filter",
				    ao->name, ao->plugin->name);
345
			return nullptr;
346 347 348
		}
	}

349 350 351 352
	*length_r = length;
	return data;
}

353
static const void *
354 355 356 357
ao_filter_chunk(struct audio_output *ao, const struct music_chunk *chunk,
		size_t *length_r)
{
	size_t length;
358
	const void *data = ao_chunk_data(ao, chunk, ao->replay_gain_filter,
359
					 &ao->replay_gain_serial, &length);
360 361
	if (data == nullptr)
		return nullptr;
362

363 364 365 366
	if (length == 0) {
		/* empty chunk, nothing to do */
		*length_r = 0;
		return data;
367
	}
368

369 370
	/* cross-fade */

371
	if (chunk->other != nullptr) {
372
		size_t other_length;
373
		const void *other_data =
374 375 376 377
			ao_chunk_data(ao, chunk->other,
				      ao->other_replay_gain_filter,
				      &ao->other_replay_gain_serial,
				      &other_length);
378 379
		if (other_data == nullptr)
			return nullptr;
380

381 382 383 384 385 386 387 388 389 390 391 392 393
		if (other_length == 0) {
			*length_r = 0;
			return data;
		}

		/* if the "other" chunk is longer, then that trailer
		   is used as-is, without mixing; it is part of the
		   "next" song being faded in, and if there's a rest,
		   it means cross-fading ends here */

		if (length > other_length)
			length = other_length;

394
		void *dest = ao->cross_fade_buffer.Get(other_length);
395
		memcpy(dest, other_data, other_length);
396
		if (!pcm_mix(ao->cross_fade_dither, dest, data, length,
397
			     ao->in_audio_format.format,
398
			     1.0 - chunk->mix_ratio)) {
399 400 401
			FormatError(output_domain,
				    "Cannot cross-fade format %s",
				    sample_format_to_string(ao->in_audio_format.format));
402
			return nullptr;
403
		}
404 405 406 407 408

		data = dest;
		length = other_length;
	}

409
	/* apply filter chain */
410

411 412
	Error error;
	data = ao->filter->FilterPCM(data, length, &length, error);
413
	if (data == nullptr) {
414 415
		FormatError(error, "\"%s\" [%s] failed to filter",
			    ao->name, ao->plugin->name);
416
		return nullptr;
417 418 419 420 421 422 423 424 425
	}

	*length_r = length;
	return data;
}

static bool
ao_play_chunk(struct audio_output *ao, const struct music_chunk *chunk)
{
426 427
	assert(ao != nullptr);
	assert(ao->filter != nullptr);
428

429
	if (ao->tags && gcc_unlikely(chunk->tag != nullptr)) {
430
		ao->mutex.unlock();
431
		ao_plugin_send_tag(ao, chunk->tag);
432
		ao->mutex.lock();
433 434 435
	}

	size_t size;
436 437 438 439
#if GCC_CHECK_VERSION(4,7)
	/* workaround -Wmaybe-uninitialized false positive */
	size = 0;
#endif
440
	const char *data = (const char *)ao_filter_chunk(ao, chunk, &size);
441
	if (data == nullptr) {
442
		ao_close(ao, false);
443 444 445

		/* don't automatically reopen this device for 10
		   seconds */
446
		ao->fail_timer.Update();
447
		return false;
448 449
	}

450 451
	Error error;

452
	while (size > 0 && ao->command == AO_COMMAND_NONE) {
453 454
		size_t nbytes;

455 456 457
		if (!ao_wait(ao))
			break;

458
		ao->mutex.unlock();
459
		nbytes = ao_plugin_play(ao, data, size, error);
460
		ao->mutex.lock();
461 462
		if (nbytes == 0) {
			/* play()==0 means failure */
463 464
			FormatError(error, "\"%s\" [%s] failed to play",
				    ao->name, ao->plugin->name);
465

466
			ao_close(ao, false);
467 468 469

			/* don't automatically reopen this device for
			   10 seconds */
470 471
			assert(!ao->fail_timer.IsDefined());
			ao->fail_timer.Update();
472

473
			return false;
474 475 476
		}

		assert(nbytes <= size);
477
		assert(nbytes % ao->out_audio_format.GetFrameSize() == 0);
478 479 480

		data += nbytes;
		size -= nbytes;
481 482
	}

483 484 485
	return true;
}

486 487 488
static const struct music_chunk *
ao_next_chunk(struct audio_output *ao)
{
489
	return ao->chunk != nullptr
490 491 492
		/* continue the previous play() call */
		? ao->chunk->next
		/* get the first chunk from the pipe */
493
		: ao->pipe->Peek();
494 495
}

496 497 498 499 500 501 502 503 504 505
/**
 * Plays all remaining chunks, until the tail of the pipe has been
 * reached (and no more chunks are queued), or until a command is
 * received.
 *
 * @return true if at least one chunk has been available, false if the
 * tail of the pipe was already reached
 */
static bool
ao_play(struct audio_output *ao)
506 507 508 509
{
	bool success;
	const struct music_chunk *chunk;

510
	assert(ao->pipe != nullptr);
511

512
	chunk = ao_next_chunk(ao);
513
	if (chunk == nullptr)
514 515
		/* no chunk available */
		return false;
516

517 518
	ao->chunk_finished = false;

519 520 521
	assert(!ao->in_playback_loop);
	ao->in_playback_loop = true;

522
	while (chunk != nullptr && ao->command == AO_COMMAND_NONE) {
523 524 525 526 527 528
		assert(!ao->chunk_finished);

		ao->chunk = chunk;

		success = ao_play_chunk(ao, chunk);
		if (!success) {
529
			assert(ao->chunk == nullptr);
530 531 532 533 534 535 536
			break;
		}

		assert(ao->chunk == chunk);
		chunk = chunk->next;
	}

537 538 539
	assert(ao->in_playback_loop);
	ao->in_playback_loop = false;

540
	ao->chunk_finished = true;
541

542
	ao->mutex.unlock();
543
	ao->player_control->LockSignal();
544
	ao->mutex.lock();
545 546

	return true;
547 548
}

549 550
static void ao_pause(struct audio_output *ao)
{
551 552
	bool ret;

553
	ao->mutex.unlock();
554
	ao_plugin_cancel(ao);
555
	ao->mutex.lock();
556

557
	ao->pause = true;
558 559 560
	ao_command_finished(ao);

	do {
561 562 563
		if (!ao_wait(ao))
			break;

564
		ao->mutex.unlock();
565
		ret = ao_plugin_pause(ao);
566
		ao->mutex.lock();
567

568
		if (!ret) {
569
			ao_close(ao, false);
570 571 572
			break;
		}
	} while (ao->command == AO_COMMAND_NONE);
573 574

	ao->pause = false;
575 576
}

577 578
static void
audio_output_task(void *arg)
579
{
580
	struct audio_output *ao = (struct audio_output *)arg;
581

582 583
	SetThreadRealtime();

584
	ao->mutex.lock();
585

586 587 588 589 590
	while (1) {
		switch (ao->command) {
		case AO_COMMAND_NONE:
			break;

591 592 593 594 595 596 597 598 599 600
		case AO_COMMAND_ENABLE:
			ao_enable(ao);
			ao_command_finished(ao);
			break;

		case AO_COMMAND_DISABLE:
			ao_disable(ao);
			ao_command_finished(ao);
			break;

601
		case AO_COMMAND_OPEN:
602
			ao_open(ao);
603 604 605
			ao_command_finished(ao);
			break;

606 607 608 609 610
		case AO_COMMAND_REOPEN:
			ao_reopen(ao);
			ao_command_finished(ao);
			break;

611 612
		case AO_COMMAND_CLOSE:
			assert(ao->open);
613
			assert(ao->pipe != nullptr);
614

615
			ao_close(ao, false);
616 617 618
			ao_command_finished(ao);
			break;

619
		case AO_COMMAND_PAUSE:
620 621 622 623 624 625 626 627 628
			if (!ao->open) {
				/* the output has failed after
				   audio_output_all_pause() has
				   submitted the PAUSE command; bail
				   out */
				ao_command_finished(ao);
				break;
			}

629
			ao_pause(ao);
630 631 632 633 634
			/* don't "break" here: this might cause
			   ao_play() to be called when command==CLOSE
			   ends the paused state - "continue" checks
			   the new command first */
			continue;
635

636 637
		case AO_COMMAND_DRAIN:
			if (ao->open) {
638
				assert(ao->chunk == nullptr);
639
				assert(ao->pipe->Peek() == nullptr);
640

641
				ao->mutex.unlock();
642
				ao_plugin_drain(ao);
643
				ao->mutex.lock();
644 645 646 647 648
			}

			ao_command_finished(ao);
			continue;

649
		case AO_COMMAND_CANCEL:
650
			ao->chunk = nullptr;
651 652

			if (ao->open) {
653
				ao->mutex.unlock();
654
				ao_plugin_cancel(ao);
655
				ao->mutex.lock();
656 657
			}

658
			ao_command_finished(ao);
659
			continue;
660 661

		case AO_COMMAND_KILL:
662
			ao->chunk = nullptr;
663
			ao_command_finished(ao);
664
			ao->mutex.unlock();
665
			return;
666 667
		}

668
		if (ao->open && ao->allow_play && ao_play(ao))
669 670 671
			/* don't wait for an event if there are more
			   chunks in the pipe */
			continue;
672

673 674
		if (ao->command == AO_COMMAND_NONE) {
			ao->woken_for_play = false;
675
			ao->cond.wait(ao->mutex);
676
		}
677 678 679 680 681 682 683
	}
}

void audio_output_thread_start(struct audio_output *ao)
{
	assert(ao->command == AO_COMMAND_NONE);

684 685 686
	Error error;
	if (!ao->thread.Start(audio_output_task, ao, error))
		FatalError(error);
687
}