Control.cxx 7.72 KB
Newer Older
1
/*
2
 * Copyright 2003-2018 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 "Control.hxx"
21
#include "Filtered.hxx"
22
#include "Domain.hxx"
Max Kellermann's avatar
Max Kellermann committed
23
#include "mixer/MixerControl.hxx"
24
#include "filter/plugins/ReplayGainFilterPlugin.hxx"
25
#include "config/Block.hxx"
26
#include "Log.hxx"
27

28 29
#include <stdexcept>

30
#include <assert.h>
31

32
/** after a failure, wait this duration before
33
    automatically reopening the device */
34
static constexpr PeriodClock::Duration REOPEN_AFTER = std::chrono::seconds(10);
35

36
AudioOutputControl::AudioOutputControl(std::unique_ptr<FilteredAudioOutput> _output,
37
				       AudioOutputClient &_client) noexcept
38
	:output(std::move(_output)), client(_client),
39
	 thread(BIND_THIS_METHOD(Task))
40 41 42
{
}

43 44
AudioOutputControl::~AudioOutputControl() noexcept
{
45 46
	BeginDestroy();

47 48
	if (thread.IsDefined())
		thread.Join();
49 50
}

51 52 53
void
AudioOutputControl::Configure(const ConfigBlock &block)
{
54 55 56
	tags = block.GetBlockValue("tags", true);
	always_on = block.GetBlockValue("always_on", false);
	enabled = block.GetBlockValue("enabled", true);
57 58
}

59
const char *
60
AudioOutputControl::GetName() const noexcept
61 62 63 64
{
	return output->GetName();
}

65 66 67 68 69 70
const char *
AudioOutputControl::GetPluginName() const noexcept
{
	return output->GetPluginName();
}

71 72 73 74 75 76
const char *
AudioOutputControl::GetLogName() const noexcept
{
	return output->GetLogName();
}

77
Mixer *
78
AudioOutputControl::GetMixer() const noexcept
79 80 81 82
{
	return output->mixer;
}

83 84 85 86 87 88 89 90 91 92 93 94
const std::map<std::string, std::string>
AudioOutputControl::GetAttributes() const noexcept
{
	return output->GetAttributes();
}

void
AudioOutputControl::SetAttribute(std::string &&name, std::string &&value)
{
	output->SetAttribute(std::move(name), std::move(value));
}

95
bool
96
AudioOutputControl::LockSetEnabled(bool new_value) noexcept
97 98 99
{
	const std::lock_guard<Mutex> protect(mutex);

100
	if (new_value == enabled)
101 102
		return false;

103
	enabled = new_value;
104 105 106 107
	return true;
}

bool
108
AudioOutputControl::LockToggleEnabled() noexcept
109 110
{
	const std::lock_guard<Mutex> protect(mutex);
111
	return enabled = !enabled;
112 113
}

114
void
115
AudioOutputControl::WaitForCommand() noexcept
116
{
117 118
	while (!IsCommandFinished())
		client_cond.wait(mutex);
119 120
}

121
void
122
AudioOutputControl::CommandAsync(Command cmd) noexcept
123
{
124 125 126
	assert(IsCommandFinished());

	command = cmd;
127
	wake_cond.notify_one();
128
}
129

130
void
131
AudioOutputControl::CommandWait(Command cmd) noexcept
132
{
133 134
	CommandAsync(cmd);
	WaitForCommand();
135 136
}

137
void
138
AudioOutputControl::LockCommandWait(Command cmd) noexcept
139
{
140
	const std::lock_guard<Mutex> protect(mutex);
141
	CommandWait(cmd);
142 143
}

144
void
145
AudioOutputControl::EnableAsync()
146
{
147
	if (!thread.IsDefined()) {
148
		if (!output->SupportsEnableDisable()) {
149 150 151
			/* don't bother to start the thread now if the
			   device doesn't even have a enable() method;
			   just assign the variable and we're done */
152
			really_enabled = true;
153 154 155
			return;
		}

156
		StartThread();
157 158
	}

159
	CommandAsync(Command::ENABLE);
160 161 162
}

void
163
AudioOutputControl::DisableAsync() noexcept
164
{
165
	if (!thread.IsDefined()) {
166
		if (!output->SupportsEnableDisable())
167
			really_enabled = false;
168 169 170
		else
			/* if there's no thread yet, the device cannot
			   be enabled */
171
			assert(!really_enabled);
172 173 174 175

		return;
	}

176
	CommandAsync(Command::DISABLE);
177 178
}

179 180 181
void
AudioOutputControl::EnableDisableAsync()
{
182
	if (enabled == really_enabled)
183
		return;
184

185
	if (enabled)
186 187 188
		EnableAsync();
	else
		DisableAsync();
189 190
}

191
inline bool
192 193
AudioOutputControl::Open(const AudioFormat audio_format,
			 const MusicPipe &mp) noexcept
194
{
195
	assert(allow_play);
196
	assert(audio_format.IsValid());
197

198
	fail_timer.Reset();
199

200
	if (open && audio_format == request.audio_format) {
201
		assert(request.pipe == &mp || (always_on && pause));
202

203
		if (!pause)
204 205 206
			/* already open, already the right parameters
			   - nothing needs to be done */
			return true;
207 208
	}

209
	request.audio_format = audio_format;
210
	request.pipe = &mp;
211

212 213 214 215 216 217 218 219
	if (!thread.IsDefined()) {
		try {
			StartThread();
		} catch (...) {
			LogError(std::current_exception());
			return false;
		}
	}
220

221
	CommandWait(Command::OPEN);
222
	const bool open2 = open;
223

224
	if (open2 && output->mixer != nullptr) {
225
		const ScopeUnlock unlock(mutex);
226
		try {
227
			mixer_open(output->mixer);
228 229 230
		} catch (...) {
			FormatError(std::current_exception(),
				    "Failed to open mixer for '%s'",
231
				    GetName());
232
		}
233
	}
234

235
	return open2;
236 237
}

238
void
239
AudioOutputControl::CloseWait() noexcept
240
{
241
	assert(allow_play);
242

243 244
	if (output->mixer != nullptr)
		mixer_auto_close(output->mixer);
245

246
	assert(!open || !fail_timer.IsDefined());
247

248
	if (open)
249
		CommandWait(Command::CLOSE);
250
	else
251
		fail_timer.Reset();
252 253
}

254
bool
255 256
AudioOutputControl::LockUpdate(const AudioFormat audio_format,
			       const MusicPipe &mp,
257
			       bool force) noexcept
258
{
259
	const std::lock_guard<Mutex> protect(mutex);
260

261
	if (enabled && really_enabled) {
262
		if (force || !fail_timer.IsDefined() ||
263
		    fail_timer.Check(REOPEN_AFTER * 1000)) {
264
			return Open(audio_format, mp);
265
		}
266 267
	} else if (IsOpen())
		CloseWait();
268 269

	return false;
270 271
}

272
bool
273
AudioOutputControl::IsChunkConsumed(const MusicChunk &chunk) const noexcept
274
{
275
	if (!open)
276 277 278
		return true;

	return source.IsChunkConsumed(chunk);
279 280
}

281 282
bool
AudioOutputControl::LockIsChunkConsumed(const MusicChunk &chunk) const noexcept
283
{
284 285
	const std::lock_guard<Mutex> protect(mutex);
	return IsChunkConsumed(chunk);
286 287
}

288
void
289
AudioOutputControl::LockPlay() noexcept
290
{
291
	const std::lock_guard<Mutex> protect(mutex);
292

293
	assert(allow_play);
294

295 296
	if (IsOpen() && !in_playback_loop && !woken_for_play) {
		woken_for_play = true;
297
		wake_cond.notify_one();
298
	}
299 300
}

301
void
302
AudioOutputControl::LockPauseAsync() noexcept
303
{
304
	if (output->mixer != nullptr && !output->SupportsPause())
305 306 307
		/* the device has no pause mode: close the mixer,
		   unless its "global" flag is set (checked by
		   mixer_auto_close()) */
308
		mixer_auto_close(output->mixer);
309

310
	const std::lock_guard<Mutex> protect(mutex);
311

312 313
	assert(allow_play);
	if (IsOpen())
314
		CommandAsync(Command::PAUSE);
315 316
}

317
void
318
AudioOutputControl::LockDrainAsync() noexcept
319
{
320
	const std::lock_guard<Mutex> protect(mutex);
321

322 323
	assert(allow_play);
	if (IsOpen())
324
		CommandAsync(Command::DRAIN);
325 326
}

327
void
328
AudioOutputControl::LockCancelAsync() noexcept
329
{
330
	const std::lock_guard<Mutex> protect(mutex);
331

332 333
	if (IsOpen()) {
		allow_play = false;
334
		CommandAsync(Command::CANCEL);
335
	}
336 337 338
}

void
339
AudioOutputControl::LockAllowPlay() noexcept
340
{
341
	const std::lock_guard<Mutex> protect(mutex);
342

343 344
	allow_play = true;
	if (IsOpen())
345
		wake_cond.notify_one();
346 347
}

348
void
349
AudioOutputControl::LockRelease() noexcept
350
{
351 352 353 354 355 356 357 358 359 360 361 362 363 364
	if (output->mixer != nullptr &&
	    (!always_on || !output->SupportsPause()))
		/* the device has no pause mode: close the mixer,
		   unless its "global" flag is set (checked by
		   mixer_auto_close()) */
		mixer_auto_close(output->mixer);

	const std::lock_guard<Mutex> protect(mutex);

	assert(!open || !fail_timer.IsDefined());
	assert(allow_play);

	if (IsOpen())
		CommandWait(Command::RELEASE);
365
	else
366
		fail_timer.Reset();
367 368
}

369
void
370
AudioOutputControl::LockCloseWait() noexcept
371
{
372
	assert(!open || !fail_timer.IsDefined());
373

374
	const std::lock_guard<Mutex> protect(mutex);
375
	CloseWait();
376 377
}

378
void
379
AudioOutputControl::BeginDestroy() noexcept
380
{
381 382
	if (thread.IsDefined()) {
		const std::lock_guard<Mutex> protect(mutex);
383 384
		if (IsCommandFinished())
			CommandAsync(Command::KILL);
385
	}
386
}