MultipleOutputs.cxx 8.55 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2017 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 * 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.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include "config.h"
#include "MultipleOutputs.hxx"
22 23
#include "Internal.hxx"
#include "Domain.hxx"
24 25 26
#include "MusicBuffer.hxx"
#include "MusicPipe.hxx"
#include "MusicChunk.hxx"
27
#include "config/Block.hxx"
28 29 30
#include "config/ConfigGlobal.hxx"
#include "config/ConfigOption.hxx"
#include "notify.hxx"
31
#include "util/RuntimeError.hxx"
32

33 34
#include <stdexcept>

35 36 37
#include <assert.h>
#include <string.h>

38
MultipleOutputs::MultipleOutputs(MixerListener &_mixer_listener)
39
	:mixer_listener(_mixer_listener)
40 41 42 43 44
{
}

MultipleOutputs::~MultipleOutputs()
{
45
	/* parallel destruction */
46
	for (auto i : outputs)
47 48 49
		i->BeginDestroy();
	for (auto i : outputs)
		i->FinishDestroy();
50 51
}

52
static AudioOutput *
53 54 55
LoadOutput(EventLoop &event_loop,
	   const ReplayGainConfig &replay_gain_config,
	   MixerListener &mixer_listener,
56
	   AudioOutputClient &client, const ConfigBlock &block)
57
try {
58
	return audio_output_new(event_loop, replay_gain_config, block,
59
				mixer_listener,
60
				client);
61 62
} catch (const std::runtime_error &e) {
	if (block.line > 0)
63 64
		std::throw_with_nested(FormatRuntimeError("Failed to configure output in line %i",
							  block.line));
65
	else
66
		throw;
67 68 69
}

void
70 71
MultipleOutputs::Configure(EventLoop &event_loop,
			   const ReplayGainConfig &replay_gain_config,
72
			   AudioOutputClient &client)
73
{
74
	for (const auto *param = config_get_block(ConfigBlockOption::AUDIO_OUTPUT);
75
	     param != nullptr; param = param->next) {
76 77
		auto output = LoadOutput(event_loop, replay_gain_config,
					 mixer_listener,
78
					 client, *param);
79
		if (FindByName(output->GetName()) != nullptr)
80
			throw FormatRuntimeError("output devices with identical "
81
						 "names: %s", output->GetName());
82 83 84 85 86 87

		outputs.push_back(output);
	}

	if (outputs.empty()) {
		/* auto-detect device */
88
		const ConfigBlock empty;
89 90
		auto output = LoadOutput(event_loop, replay_gain_config,
					 mixer_listener,
91
					 client, empty);
92 93 94 95
		outputs.push_back(output);
	}
}

96
AudioOutput *
97
MultipleOutputs::FindByName(const char *name) const noexcept
98 99
{
	for (auto i : outputs)
100
		if (strcmp(i->GetName(), name) == 0)
101 102 103 104 105 106 107 108
			return i;

	return nullptr;
}

void
MultipleOutputs::EnableDisable()
{
109 110 111
	/* parallel execution */

	for (auto ao : outputs) {
112
		const std::lock_guard<Mutex> lock(ao->mutex);
113 114 115
		ao->EnableDisableAsync();
	}

116
	for (auto ao : outputs) {
117
		const std::lock_guard<Mutex> lock(ao->mutex);
118
		ao->WaitForCommand();
119 120 121 122
	}
}

bool
123
MultipleOutputs::AllFinished() const noexcept
124 125
{
	for (auto ao : outputs) {
126
		const std::lock_guard<Mutex> protect(ao->mutex);
127
		if (ao->IsOpen() && !ao->IsCommandFinished())
128 129 130 131 132 133 134
			return false;
	}

	return true;
}

void
135
MultipleOutputs::WaitAll() noexcept
136 137 138 139 140 141 142 143 144
{
	while (!AllFinished())
		audio_output_client_notify.Wait();
}

void
MultipleOutputs::AllowPlay()
{
	for (auto ao : outputs)
145
		ao->LockAllowPlay();
146 147 148
}

bool
149
MultipleOutputs::Update(bool force)
150 151 152 153 154 155 156
{
	bool ret = false;

	if (!input_audio_format.IsDefined())
		return false;

	for (auto ao : outputs)
157
		ret = ao->LockUpdate(input_audio_format, *pipe, force)
158 159 160 161 162 163 164 165 166
			|| ret;

	return ret;
}

void
MultipleOutputs::SetReplayGainMode(ReplayGainMode mode)
{
	for (auto ao : outputs)
167
		ao->SetReplayGainMode(mode);
168 169
}

170 171
void
MultipleOutputs::Play(MusicChunk *chunk)
172 173 174 175 176 177
{
	assert(buffer != nullptr);
	assert(pipe != nullptr);
	assert(chunk != nullptr);
	assert(chunk->CheckFormat(input_audio_format));

178
	if (!Update(false))
179
		/* TODO: obtain real error */
180
		throw std::runtime_error("Failed to open audio output");
181 182 183 184

	pipe->Push(chunk);

	for (auto ao : outputs)
185
		ao->LockPlay();
186 187
}

188
void
189
MultipleOutputs::Open(const AudioFormat audio_format,
190
		      MusicBuffer &_buffer)
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
{
	bool ret = false, enabled = false;

	assert(buffer == nullptr || buffer == &_buffer);
	assert((pipe == nullptr) == (buffer == nullptr));

	buffer = &_buffer;

	/* the audio format must be the same as existing chunks in the
	   pipe */
	assert(pipe == nullptr || pipe->CheckFormat(audio_format));

	if (pipe == nullptr)
		pipe = new MusicPipe();
	else
		/* if the pipe hasn't been cleared, the the audio
		   format must not have changed */
		assert(pipe->IsEmpty() || audio_format == input_audio_format);

	input_audio_format = audio_format;

	EnableDisable();
213
	Update(true);
214

215 216
	std::exception_ptr first_error;

217
	for (auto ao : outputs) {
218
		const std::lock_guard<Mutex> lock(ao->mutex);
219

220
		if (ao->IsEnabled())
221 222
			enabled = true;

223
		if (ao->IsOpen())
224
			ret = true;
225 226
		else if (!first_error)
			first_error = ao->GetLastError();
227 228
	}

229
	if (!enabled) {
230 231
		/* close all devices if there was an error */
		Close();
232 233 234 235
		throw std::runtime_error("All audio outputs are disabled");
	} else if (!ret) {
		/* close all devices if there was an error */
		Close();
236 237 238 239 240 241

		if (first_error)
			/* we have details, so throw that */
			std::rethrow_exception(first_error);
		else
			throw std::runtime_error("Failed to open audio output");
242
	}
243 244 245
}

bool
246
MultipleOutputs::IsChunkConsumed(const MusicChunk *chunk) const noexcept
247
{
248 249
	for (auto ao : outputs)
		if (!ao->LockIsChunkConsumed(*chunk))
250 251 252 253 254 255
			return false;

	return true;
}

inline void
256
MultipleOutputs::ClearTailChunk(const MusicChunk *chunk,
257 258 259 260 261 262
				bool *locked)
{
	assert(chunk->next == nullptr);
	assert(pipe->Contains(chunk));

	for (unsigned i = 0, n = outputs.size(); i != n; ++i) {
263
		AudioOutput *ao = outputs[i];
264 265 266 267

		/* this mutex will be unlocked by the caller when it's
		   ready */
		ao->mutex.lock();
268
		locked[i] = ao->IsOpen();
269 270 271 272 273 274

		if (!locked[i]) {
			ao->mutex.unlock();
			continue;
		}

275
		ao->ClearTailChunk(*chunk);
276 277 278 279 280 281
	}
}

unsigned
MultipleOutputs::Check()
{
282
	const MusicChunk *chunk;
283
	bool is_tail;
284
	MusicChunk *shifted;
285 286 287 288 289 290 291 292 293 294 295 296 297
	bool locked[outputs.size()];

	assert(buffer != nullptr);
	assert(pipe != nullptr);

	while ((chunk = pipe->Peek()) != nullptr) {
		assert(!pipe->IsEmpty());

		if (!IsChunkConsumed(chunk))
			/* at least one output is not finished playing
			   this chunk */
			return pipe->GetSize();

298
		if (chunk->length > 0 && !chunk->time.IsNegative())
299 300
			/* only update elapsed_time if the chunk
			   provides a defined value */
301
			elapsed_time = chunk->time;
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

		is_tail = chunk->next == nullptr;
		if (is_tail)
			/* this is the tail of the pipe - clear the
			   chunk reference in all outputs */
			ClearTailChunk(chunk, locked);

		/* remove the chunk from the pipe */
		shifted = pipe->Shift();
		assert(shifted == chunk);

		if (is_tail)
			/* unlock all audio outputs which were locked
			   by clear_tail_chunk() */
			for (unsigned i = 0, n = outputs.size(); i != n; ++i)
				if (locked[i])
					outputs[i]->mutex.unlock();

		/* return the chunk to the buffer */
		buffer->Return(shifted);
	}

	return 0;
}

void
MultipleOutputs::Pause()
{
330
	Update(false);
331 332

	for (auto ao : outputs)
333
		ao->LockPauseAsync();
334 335 336 337 338 339 340 341

	WaitAll();
}

void
MultipleOutputs::Drain()
{
	for (auto ao : outputs)
342
		ao->LockDrainAsync();
343 344 345 346 347 348 349 350 351 352

	WaitAll();
}

void
MultipleOutputs::Cancel()
{
	/* send the cancel() command to all audio outputs */

	for (auto ao : outputs)
353
		ao->LockCancelAsync();
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368

	WaitAll();

	/* clear the music pipe and return all chunks to the buffer */

	if (pipe != nullptr)
		pipe->Clear(*buffer);

	/* the audio outputs are now waiting for a signal, to
	   synchronize the cleared music pipe */

	AllowPlay();

	/* invalidate elapsed_time */

369
	elapsed_time = SignedSongTime::Negative();
370 371 372 373 374 375
}

void
MultipleOutputs::Close()
{
	for (auto ao : outputs)
376
		ao->LockCloseWait();
377 378 379 380 381 382 383 384 385 386 387 388 389

	if (pipe != nullptr) {
		assert(buffer != nullptr);

		pipe->Clear(*buffer);
		delete pipe;
		pipe = nullptr;
	}

	buffer = nullptr;

	input_audio_format.Clear();

390
	elapsed_time = SignedSongTime::Negative();
391 392 393 394 395 396
}

void
MultipleOutputs::Release()
{
	for (auto ao : outputs)
397
		ao->LockRelease();
398 399 400 401 402 403 404 405 406 407 408 409 410

	if (pipe != nullptr) {
		assert(buffer != nullptr);

		pipe->Clear(*buffer);
		delete pipe;
		pipe = nullptr;
	}

	buffer = nullptr;

	input_audio_format.Clear();

411
	elapsed_time = SignedSongTime::Negative();
412 413 414 415 416 417 418
}

void
MultipleOutputs::SongBorder()
{
	/* clear the elapsed_time pointer at the beginning of a new
	   song */
419
	elapsed_time = SignedSongTime::zero();
420
}