OutputThread.cxx 13 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 "Internal.hxx"
22
#include "OutputAPI.hxx"
23
#include "Domain.hxx"
Max Kellermann's avatar
Max Kellermann committed
24
#include "pcm/PcmMix.hxx"
Max Kellermann's avatar
Max Kellermann committed
25
#include "notify.hxx"
26 27 28
#include "filter/FilterInternal.hxx"
#include "filter/plugins/ConvertFilterPlugin.hxx"
#include "filter/plugins/ReplayGainFilterPlugin.hxx"
29
#include "PlayerControl.hxx"
30 31
#include "MusicPipe.hxx"
#include "MusicChunk.hxx"
32
#include "thread/Util.hxx"
33
#include "thread/Slack.hxx"
34
#include "thread/Name.hxx"
35
#include "system/FatalError.hxx"
36
#include "util/Error.hxx"
37
#include "util/ConstBuffer.hxx"
38
#include "Log.hxx"
39
#include "Compiler.h"
40

41
#include <assert.h>
Max Kellermann's avatar
Max Kellermann committed
42
#include <string.h>
43

44 45
void
AudioOutput::CommandFinished()
46
{
47 48
	assert(command != AO_COMMAND_NONE);
	command = AO_COMMAND_NONE;
49

50
	mutex.unlock();
Max Kellermann's avatar
Max Kellermann committed
51
	audio_output_client_notify.Signal();
52
	mutex.lock();
53 54
}

55 56
inline bool
AudioOutput::Enable()
57
{
58
	if (really_enabled)
59 60
		return true;

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

72
	really_enabled = true;
73 74 75
	return true;
}

76 77
inline void
AudioOutput::Disable()
78
{
79 80
	if (open)
		Close(false);
81

82 83
	if (really_enabled) {
		really_enabled = false;
84

85 86 87
		mutex.unlock();
		ao_plugin_disable(this);
		mutex.lock();
88 89 90
	}
}

91 92
inline AudioFormat
AudioOutput::OpenFilter(AudioFormat &format, Error &error_r)
93
{
94
	assert(format.IsValid());
95

96
	/* the replay_gain filter cannot fail here */
97 98
	if (replay_gain_filter != nullptr &&
	    !replay_gain_filter->Open(format, error_r).IsDefined())
99 100
		return AudioFormat::Undefined();

101 102 103 104
	if (other_replay_gain_filter != nullptr &&
	    !other_replay_gain_filter->Open(format, error_r).IsDefined()) {
		if (replay_gain_filter != nullptr)
			replay_gain_filter->Close();
105 106
		return AudioFormat::Undefined();
	}
107

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

	return af;
117 118
}

119 120
void
AudioOutput::CloseFilter()
121
{
122 123 124 125
	if (replay_gain_filter != nullptr)
		replay_gain_filter->Close();
	if (other_replay_gain_filter != nullptr)
		other_replay_gain_filter->Close();
126

127
	filter->Close();
128 129
}

130 131
inline void
AudioOutput::Open()
132 133
{
	bool success;
134
	Error error;
135
	struct audio_format_string af_string;
136

137 138 139 140
	assert(!open);
	assert(pipe != nullptr);
	assert(current_chunk == nullptr);
	assert(in_audio_format.IsValid());
141

142
	fail_timer.Reset();
143

144 145
	/* enable the device (just in case the last enable has failed) */

146
	if (!Enable())
147 148 149
		/* still no luck */
		return;

150 151
	/* open the filter */

152
	const AudioFormat filter_audio_format =
153
		OpenFilter(in_audio_format, error);
154
	if (!filter_audio_format.IsDefined()) {
155
		FormatError(error, "Failed to open filter for \"%s\" [%s]",
156
			    name, plugin.name);
157

158
		fail_timer.Update();
159 160 161
		return;
	}

162
	assert(filter_audio_format.IsValid());
163

164 165
	out_audio_format = filter_audio_format;
	out_audio_format.ApplyMask(config_audio_format);
166

167 168 169
	mutex.unlock();
	success = ao_plugin_open(this, out_audio_format, error);
	mutex.lock();
170

171
	assert(!open);
172 173

	if (!success) {
174
		FormatError(error, "Failed to open \"%s\" [%s]",
175
			    name, plugin.name);
176

177 178
		CloseFilter();
		fail_timer.Update();
179 180 181
		return;
	}

182
	if (!convert_filter_set(convert_filter, out_audio_format,
183 184
				error)) {
		FormatError(error, "Failed to convert for \"%s\" [%s]",
185
			    name, plugin.name);
186

187 188
		CloseFilter();
		fail_timer.Update();
189 190
		return;
	}
191

192
	open = true;
193

194 195
	FormatDebug(output_domain,
		    "opened plugin=%s name=\"%s\" audio_format=%s",
196 197
		    plugin.name, name,
		    audio_format_to_string(out_audio_format, &af_string));
198

199
	if (in_audio_format != out_audio_format)
200
		FormatDebug(output_domain, "converting from %s",
201
			    audio_format_to_string(in_audio_format,
202
						   &af_string));
203 204
}

205 206
void
AudioOutput::Close(bool drain)
207
{
208
	assert(open);
209

210
	pipe = nullptr;
211

212 213
	current_chunk = nullptr;
	open = false;
214

215
	mutex.unlock();
216

217
	if (drain)
218
		ao_plugin_drain(this);
219
	else
220
		ao_plugin_cancel(this);
221

222 223
	ao_plugin_close(this);
	CloseFilter();
224

225
	mutex.lock();
226

227
	FormatDebug(output_domain, "closed plugin=%s name=\"%s\"",
228
		    plugin.name, name);
229 230
}

231 232
void
AudioOutput::ReopenFilter()
233
{
234
	Error error;
235

236
	CloseFilter();
237
	const AudioFormat filter_audio_format =
238
		OpenFilter(in_audio_format, error);
239
	if (!filter_audio_format.IsDefined() ||
240
	    !convert_filter_set(convert_filter, out_audio_format,
241
				error)) {
242 243
		FormatError(error,
			    "Failed to open filter for \"%s\" [%s]",
244
			    name, plugin.name);
245

246
		/* this is a little code duplication from Close(),
247
		   but we cannot call this function because we must
248
		   not call filter_close(filter) again */
249

250
		pipe = nullptr;
251

252 253 254
		current_chunk = nullptr;
		open = false;
		fail_timer.Update();
255

256 257 258
		mutex.unlock();
		ao_plugin_close(this);
		mutex.lock();
259 260 261 262 263

		return;
	}
}

264 265
void
AudioOutput::Reopen()
266
{
267 268 269 270 271
	if (!config_audio_format.IsFullyDefined()) {
		if (open) {
			const MusicPipe *mp = pipe;
			Close(true);
			pipe = mp;
272 273 274 275 276
		}

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

281
	if (open)
282 283
		/* the audio format has changed, and all filters have
		   to be reconfigured */
284
		ReopenFilter();
285
	else
286
		Open();
287 288
}

289 290 291 292 293 294
/**
 * Wait until the output's delay reaches zero.
 *
 * @return true if playback should be continued, false if a command
 * was issued
 */
295 296
inline bool
AudioOutput::WaitForDelay()
297 298
{
	while (true) {
299
		unsigned delay = ao_plugin_delay(this);
300 301 302
		if (delay == 0)
			return true;

303
		(void)cond.timed_wait(mutex, delay);
304

305
		if (command != AO_COMMAND_NONE)
306 307 308 309
			return false;
	}
}

310
static ConstBuffer<void>
311
ao_chunk_data(AudioOutput *ao, const MusicChunk *chunk,
312
	      Filter *replay_gain_filter,
313
	      unsigned *replay_gain_serial_p)
314
{
315
	assert(chunk != nullptr);
316 317
	assert(!chunk->IsEmpty());
	assert(chunk->CheckFormat(ao->in_audio_format));
318

319
	ConstBuffer<void> data(chunk->data, chunk->length);
320 321 322

	(void)ao;

323
	assert(data.size % ao->in_audio_format.GetFrameSize() == 0);
324

325
	if (!data.IsEmpty() && replay_gain_filter != nullptr) {
326 327 328 329
		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
330
						    : nullptr);
331 332 333
			*replay_gain_serial_p = chunk->replay_gain_serial;
		}

334
		Error error;
335 336
		data = replay_gain_filter->FilterPCM(data, error);
		if (data.IsNull())
337
			FormatError(error, "\"%s\" [%s] failed to filter",
338
				    ao->name, ao->plugin.name);
339 340
	}

341 342 343
	return data;
}

344 345
static ConstBuffer<void>
ao_filter_chunk(AudioOutput *ao, const MusicChunk *chunk)
346
{
347 348 349 350
	ConstBuffer<void> data =
		ao_chunk_data(ao, chunk, ao->replay_gain_filter,
			      &ao->replay_gain_serial);
	if (data.IsEmpty())
351
		return data;
352

353 354
	/* cross-fade */

355
	if (chunk->other != nullptr) {
356
		ConstBuffer<void> other_data =
357 358
			ao_chunk_data(ao, chunk->other,
				      ao->other_replay_gain_filter,
359 360
				      &ao->other_replay_gain_serial);
		if (other_data.IsNull())
361
			return nullptr;
362

363
		if (other_data.IsEmpty())
364 365 366 367 368 369 370
			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 */

371 372
		if (data.size > other_data.size)
			data.size = other_data.size;
373

374 375 376
		void *dest = ao->cross_fade_buffer.Get(other_data.size);
		memcpy(dest, other_data.data, other_data.size);
		if (!pcm_mix(ao->cross_fade_dither, dest, data.data, data.size,
377
			     ao->in_audio_format.format,
378
			     1.0 - chunk->mix_ratio)) {
379 380 381
			FormatError(output_domain,
				    "Cannot cross-fade format %s",
				    sample_format_to_string(ao->in_audio_format.format));
382
			return nullptr;
383
		}
384

385 386
		data.data = dest;
		data.size = other_data.size;
387 388
	}

389
	/* apply filter chain */
390

391
	Error error;
392 393
	data = ao->filter->FilterPCM(data, error);
	if (data.IsNull()) {
394
		FormatError(error, "\"%s\" [%s] failed to filter",
395
			    ao->name, ao->plugin.name);
396
		return nullptr;
397 398 399 400 401
	}

	return data;
}

402
inline bool
403
AudioOutput::PlayChunk(const MusicChunk *chunk)
404
{
405
	assert(filter != nullptr);
406

407 408 409 410
	if (tags && gcc_unlikely(chunk->tag != nullptr)) {
		mutex.unlock();
		ao_plugin_send_tag(this, chunk->tag);
		mutex.lock();
411 412
	}

413 414
	auto data = ConstBuffer<char>::FromVoid(ao_filter_chunk(this, chunk));
	if (data.IsNull()) {
415
		Close(false);
416 417 418

		/* don't automatically reopen this device for 10
		   seconds */
419
		fail_timer.Update();
420
		return false;
421 422
	}

423 424
	Error error;

425
	while (!data.IsEmpty() && command == AO_COMMAND_NONE) {
426
		if (!WaitForDelay())
427 428
			break;

429
		mutex.unlock();
430 431
		size_t nbytes = ao_plugin_play(this, data.data, data.size,
					       error);
432
		mutex.lock();
433 434
		if (nbytes == 0) {
			/* play()==0 means failure */
435
			FormatError(error, "\"%s\" [%s] failed to play",
436
				    name, plugin.name);
437

438
			Close(false);
439 440 441

			/* don't automatically reopen this device for
			   10 seconds */
442 443
			assert(!fail_timer.IsDefined());
			fail_timer.Update();
444

445
			return false;
446 447
		}

448
		assert(nbytes <= data.size);
449
		assert(nbytes % out_audio_format.GetFrameSize() == 0);
450

451 452
		data.data += nbytes;
		data.size -= nbytes;
453 454
	}

455 456 457
	return true;
}

458
inline const MusicChunk *
459
AudioOutput::GetNextChunk() const
460
{
461
	return current_chunk != nullptr
462
		/* continue the previous play() call */
463
		? current_chunk->next
464
		/* get the first chunk from the pipe */
465
		: pipe->Peek();
466 467
}

468 469
inline bool
AudioOutput::Play()
470
{
471
	assert(pipe != nullptr);
472

473
	const MusicChunk *chunk = GetNextChunk();
474
	if (chunk == nullptr)
475 476
		/* no chunk available */
		return false;
477

478
	current_chunk_finished = false;
479

480 481
	assert(!in_playback_loop);
	in_playback_loop = true;
482

483 484
	while (chunk != nullptr && command == AO_COMMAND_NONE) {
		assert(!current_chunk_finished);
485

486
		current_chunk = chunk;
487

488 489
		if (!PlayChunk(chunk)) {
			assert(current_chunk == nullptr);
490 491 492
			break;
		}

493
		assert(current_chunk == chunk);
494 495 496
		chunk = chunk->next;
	}

497 498
	assert(in_playback_loop);
	in_playback_loop = false;
499

500
	current_chunk_finished = true;
501

502 503 504
	mutex.unlock();
	player_control->LockSignal();
	mutex.lock();
505 506

	return true;
507 508
}

509 510
inline void
AudioOutput::Pause()
511
{
512 513 514
	mutex.unlock();
	ao_plugin_cancel(this);
	mutex.lock();
515

516 517
	pause = true;
	CommandFinished();
518 519

	do {
520
		if (!WaitForDelay())
521 522
			break;

523 524 525
		mutex.unlock();
		bool success = ao_plugin_pause(this);
		mutex.lock();
526

527 528
		if (!success) {
			Close(false);
529 530
			break;
		}
531
	} while (command == AO_COMMAND_NONE);
532

533
	pause = false;
534 535
}

536 537
inline void
AudioOutput::Task()
538
{
539
	FormatThreadName("output:%s", name);
540

541
	SetThreadRealtime();
542
	SetThreadTimerSlackUS(100);
543

544
	mutex.lock();
545

546
	while (1) {
547
		switch (command) {
548 549 550
		case AO_COMMAND_NONE:
			break;

551
		case AO_COMMAND_ENABLE:
552 553
			Enable();
			CommandFinished();
554 555 556
			break;

		case AO_COMMAND_DISABLE:
557 558
			Disable();
			CommandFinished();
559 560
			break;

561
		case AO_COMMAND_OPEN:
562 563
			Open();
			CommandFinished();
564 565
			break;

566
		case AO_COMMAND_REOPEN:
567 568
			Reopen();
			CommandFinished();
569 570
			break;

571
		case AO_COMMAND_CLOSE:
572 573
			assert(open);
			assert(pipe != nullptr);
574

575 576
			Close(false);
			CommandFinished();
577 578
			break;

579
		case AO_COMMAND_PAUSE:
580
			if (!open) {
581 582 583 584
				/* the output has failed after
				   audio_output_all_pause() has
				   submitted the PAUSE command; bail
				   out */
585
				CommandFinished();
586 587 588
				break;
			}

589
			Pause();
590
			/* don't "break" here: this might cause
591
			   Play() to be called when command==CLOSE
592 593 594
			   ends the paused state - "continue" checks
			   the new command first */
			continue;
595

596
		case AO_COMMAND_DRAIN:
597 598 599
			if (open) {
				assert(current_chunk == nullptr);
				assert(pipe->Peek() == nullptr);
600

601 602 603
				mutex.unlock();
				ao_plugin_drain(this);
				mutex.lock();
604 605
			}

606
			CommandFinished();
607 608
			continue;

609
		case AO_COMMAND_CANCEL:
610
			current_chunk = nullptr;
611

612 613 614 615
			if (open) {
				mutex.unlock();
				ao_plugin_cancel(this);
				mutex.lock();
616 617
			}

618
			CommandFinished();
619
			continue;
620 621

		case AO_COMMAND_KILL:
622 623 624
			current_chunk = nullptr;
			CommandFinished();
			mutex.unlock();
625
			return;
626 627
		}

628
		if (open && allow_play && Play())
629 630 631
			/* don't wait for an event if there are more
			   chunks in the pipe */
			continue;
632

633 634 635
		if (command == AO_COMMAND_NONE) {
			woken_for_play = false;
			cond.wait(mutex);
636
		}
637 638 639
	}
}

640 641 642 643 644 645 646
void
AudioOutput::Task(void *arg)
{
	AudioOutput *ao = (AudioOutput *)arg;
	ao->Task();
}

647 648
void
AudioOutput::StartThread()
649
{
650
	assert(command == AO_COMMAND_NONE);
651

652
	Error error;
653
	if (!thread.Start(Task, this, error))
654
		FatalError(error);
655
}