Internal.hxx 11.3 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2017 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
 */

Max Kellermann's avatar
Max Kellermann committed
20 21
#ifndef MPD_OUTPUT_INTERNAL_HXX
#define MPD_OUTPUT_INTERNAL_HXX
22

23
#include "Source.hxx"
24
#include "SharedPipeConsumer.hxx"
25
#include "AudioFormat.hxx"
26
#include "filter/Observer.hxx"
27 28
#include "thread/Mutex.hxx"
#include "thread/Cond.hxx"
29
#include "thread/Thread.hxx"
30
#include "system/PeriodClock.hxx"
31

32 33
#include <exception>

34
class PreparedFilter;
35
class MusicPipe;
36
class EventLoop;
37 38
class Mixer;
class MixerListener;
39
class AudioOutputClient;
40
struct MusicChunk;
41
struct ConfigBlock;
42
struct AudioOutputPlugin;
43
struct ReplayGainConfig;
44

45 46 47 48 49
struct AudioOutput {
	enum class Command {
		NONE,
		ENABLE,
		DISABLE,
50

51
		/**
52 53
		 * Open the output, or reopen it if it is already
		 * open, adjusting for input #AudioFormat changes.
54
		 */
55
		OPEN,
56

57 58
		CLOSE,
		PAUSE,
59

60 61 62 63 64
		/**
		 * Drains the internal (hardware) buffers of the device.  This
		 * operation may take a while to complete.
		 */
		DRAIN,
65

66 67 68
		CANCEL,
		KILL
	};
69

70 71 72
	/**
	 * The device's configured display name.
	 */
73 74
	const char *name;

75 76 77
	/**
	 * The plugin which implements this output device.
	 */
78
	const AudioOutputPlugin &plugin;
79

80 81
	/**
	 * The #mixer object associated with this audio output device.
82
	 * May be nullptr if none is available, or if software volume is
83 84
	 * configured.
	 */
85
	Mixer *mixer = nullptr;
86

87 88 89 90 91 92 93
	/**
	 * Will this output receive tags from the decoder?  The
	 * default is true, but it may be configured to false to
	 * suppress sending tags to the output.
	 */
	bool tags;

94 95 96 97 98 99
	/**
	 * Shall this output always play something (i.e. silence),
	 * even when playback is stopped?
	 */
	bool always_on;

100 101 102
	/**
	 * Has the user enabled this device?
	 */
103
	bool enabled = true;
104

105 106 107 108
	/**
	 * Is this device actually enabled, i.e. the "enable" method
	 * has succeeded?
	 */
109
	bool really_enabled = false;
110

111 112
	/**
	 * Is the device (already) open and functional?
113 114 115 116 117
	 *
	 * This attribute may only be modified by the output thread.
	 * It is protected with #mutex: write accesses inside the
	 * output thread and read accesses outside of it may only be
	 * performed while the lock is held.
118
	 */
119
	bool open = false;
120

121 122 123 124
	/**
	 * Is the device paused?  i.e. the output thread is in the
	 * ao_pause() loop.
	 */
125
	bool pause = false;
126

127 128 129 130 131 132 133
	/**
	 * When this flag is set, the output thread will not do any
	 * playback.  It will wait until the flag is cleared.
	 *
	 * This is used to synchronize the "clear" operation on the
	 * shared music pipe during the CANCEL command.
	 */
134
	bool allow_play = true;
135

136 137 138 139 140 141
	/**
	 * True while the OutputThread is inside ao_play().  This
	 * means the PlayerThread does not need to wake up the
	 * OutputThread when new chunks are added to the MusicPipe,
	 * because the OutputThread is already watching that.
	 */
142
	bool in_playback_loop = false;
143 144 145 146 147 148

	/**
	 * Has the OutputThread been woken up to play more chunks?
	 * This is set by audio_output_play() and reset by ao_play()
	 * to reduce the number of duplicate wakeups.
	 */
149
	bool woken_for_play = false;
150

151
	/**
152
	 * If not nullptr, the device has failed, and this timer is used
153 154
	 * to estimate how long it should stay disabled (unless
	 * explicitly reopened with "play").
155
	 */
156
	PeriodClock fail_timer;
157

158 159 160
	/**
	 * The configured audio format.
	 */
161
	AudioFormat config_audio_format;
162

163 164 165 166 167 168 169 170
	/**
	 * The #AudioFormat which is emitted by the #Filter, with
	 * #config_audio_format already applied.  This is used to
	 * decide whether this object needs to be closed and reopened
	 * upon #AudioFormat changes.
	 */
	AudioFormat filter_audio_format;

171 172
	/**
	 * The audio_format which is really sent to the device.  This
173 174
	 * is basically config_audio_format (if configured) or
	 * in_audio_format, but may have been modified by
175 176
	 * plugin->open().
	 */
177
	AudioFormat out_audio_format;
178

179 180 181 182
	/**
	 * The filter object of this audio output.  This is an
	 * instance of chain_filter_plugin.
	 */
183
	PreparedFilter *prepared_filter = nullptr;
184

185 186 187 188
	/**
	 * The #VolumeFilter instance of this audio output.  It is
	 * used by the #SoftwareMixer.
	 */
189
	FilterObserver volume_filter;
190

191 192 193 194
	/**
	 * The replay_gain_filter_plugin instance of this audio
	 * output.
	 */
195
	PreparedFilter *prepared_replay_gain_filter = nullptr;
196

197 198 199 200 201
	/**
	 * The replay_gain_filter_plugin instance of this audio
	 * output, to be applied to the second chunk during
	 * cross-fading.
	 */
202
	PreparedFilter *prepared_other_replay_gain_filter = nullptr;
203

204 205 206 207 208 209
	/**
	 * The convert_filter_plugin instance of this audio output.
	 * It is the last item in the filter chain, and is responsible
	 * for converting the input data into the appropriate format
	 * for this audio output.
	 */
210
	FilterObserver convert_filter;
211

212
	/**
213
	 * The thread handle, or nullptr if the output thread isn't
214 215
	 * running.
	 */
216
	Thread thread;
217 218 219 220

	/**
	 * The next command to be performed by the output thread.
	 */
221
	Command command = Command::NONE;
222

223 224 225 226 227 228 229 230
	/**
	 * Additional data for #command.  Protected by #mutex.
	 */
	struct Request {
		/**
		 * The #AudioFormat requested by #Command::OPEN.
		 */
		AudioFormat audio_format;
231 232 233 234 235

		/**
		 * The #MusicPipe passed to #Command::OPEN.
		 */
		const MusicPipe *pipe;
236 237
	} request;

238
	/**
239
	 * This mutex protects #open, #fail_timer, #pipe.
240
	 */
241
	mutable Mutex mutex;
242

243 244 245 246
	/**
	 * This condition object wakes up the output thread after
	 * #command has been set.
	 */
247
	Cond cond;
248

249
	/**
250
	 * The PlayerControl object which "owns" this output.  This
251 252
	 * object is needed to signal command completion.
	 */
253
	AudioOutputClient *client;
254

255
	/**
256
	 * Source of audio data.
257
	 */
258
	AudioOutputSource source;
259

260 261 262 263 264 265 266 267
	/**
	 * The error that occurred in the output thread.  It is
	 * cleared whenever the output is opened successfully.
	 *
	 * Protected by #mutex.
	 */
	std::exception_ptr last_error;

268 269 270 271 272 273
	/**
	 * Throws #std::runtime_error on error.
	 */
	AudioOutput(const AudioOutputPlugin &_plugin,
		    const ConfigBlock &block);

274
	~AudioOutput();
275

276
private:
277
	void Configure(const ConfigBlock &block);
278

279
public:
280 281 282 283 284
	void Setup(EventLoop &event_loop,
		   const ReplayGainConfig &replay_gain_config,
		   MixerListener &mixer_listener,
		   const ConfigBlock &block);

285 286 287
	void StartThread();
	void StopThread();

288 289
	void BeginDestroy();
	void FinishDestroy();
290

291 292 293 294 295 296 297 298 299 300 301 302 303 304
	const char *GetName() const {
		return name;
	}

	/**
	 * Caller must lock the mutex.
	 */
	bool IsEnabled() const {
		return enabled;
	}

	/**
	 * Caller must lock the mutex.
	 */
305 306 307 308
	bool IsOpen() const {
		return open;
	}

309 310 311
	/**
	 * Caller must lock the mutex.
	 */
312
	bool IsCommandFinished() const {
313
		return command == Command::NONE;
314 315
	}

316 317 318 319 320 321 322
	/**
	 * Caller must lock the mutex.
	 */
	const std::exception_ptr &GetLastError() const {
		return last_error;
	}

323 324 325 326 327 328 329 330 331 332 333 334
	/**
	 * Waits for command completion.
	 *
	 * Caller must lock the mutex.
	 */
	void WaitForCommand();

	/**
	 * Sends a command, but does not wait for completion.
	 *
	 * Caller must lock the mutex.
	 */
335
	void CommandAsync(Command cmd);
336 337 338 339 340 341 342

	/**
	 * Sends a command to the #AudioOutput object and waits for
	 * completion.
	 *
	 * Caller must lock the mutex.
	 */
343
	void CommandWait(Command cmd);
344 345 346 347 348

	/**
	 * Lock the #AudioOutput object and execute the command
	 * synchronously.
	 */
349
	void LockCommandWait(Command cmd);
350 351

	/**
352
	 * Enables the device, but don't wait for completion.
353 354
	 *
	 * Caller must lock the mutex.
355
	 */
356
	void EnableAsync();
357 358

	/**
359
	 * Disables the device, but don't wait for completion.
360 361
	 *
	 * Caller must lock the mutex.
362
	 */
363
	void DisableAsync();
364

365 366 367
	/**
	 * Attempt to enable or disable the device as specified by the
	 * #enabled attribute; attempt to sync it with #really_enabled
368
	 * (wrapper for EnableAsync() or DisableAsync()).
369 370 371
	 *
	 * Caller must lock the mutex.
	 */
372
	void EnableDisableAsync() {
373 374 375 376
		if (enabled == really_enabled)
			return;

		if (enabled)
377
			EnableAsync();
378
		else
379
			DisableAsync();
380 381
	}

382 383 384 385 386 387 388 389 390 391 392 393 394 395 396
	void LockPauseAsync();

	/**
	 * Same LockCloseWait(), but expects the lock to be
	 * held by the caller.
	 */
	void CloseWait();
	void LockCloseWait();

	/**
	 * Closes the audio output, but if the "always_on" flag is set, put it
	 * into pause mode instead.
	 */
	void LockRelease();

397
	void SetReplayGainMode(ReplayGainMode _mode) {
398
		source.SetReplayGainMode(_mode);
399
	}
400 401 402 403 404 405 406 407 408 409

	/**
	 * Caller must lock the mutex.
	 */
	bool Open(const AudioFormat audio_format, const MusicPipe &mp);

	/**
	 * Opens or closes the device, depending on the "enabled"
	 * flag.
	 *
410
	 * @param force true to ignore the #fail_timer
411 412 413
	 * @return true if the device is open
	 */
	bool LockUpdate(const AudioFormat audio_format,
414 415
			const MusicPipe &mp,
			bool force);
416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431

	void LockPlay();

	void LockDrainAsync();

	/**
	 * Clear the "allow_play" flag and send the "CANCEL" command
	 * asynchronously.  To finish the operation, the caller has to
	 * call LockAllowPlay().
	 */
	void LockCancelAsync();

	/**
	 * Set the "allow_play" and signal the thread.
	 */
	void LockAllowPlay();
432

433 434 435 436 437 438 439 440 441 442
	/**
	 * Did we already consumed this chunk?
	 *
	 * Caller must lock the mutex.
	 */
	gcc_pure
	bool IsChunkConsumed(const MusicChunk &chunk) const;

	gcc_pure
	bool LockIsChunkConsumed(const MusicChunk &chunk) {
443
		const std::lock_guard<Mutex> protect(mutex);
444 445 446
		return IsChunkConsumed(chunk);
	}

447
	void ClearTailChunk(const MusicChunk &chunk) {
448
		source.ClearTailChunk(chunk);
449 450
	}

451 452 453
private:
	void CommandFinished();

454 455 456 457 458
	/**
	 * Throws #std::runtime_error on error.
	 */
	void Enable();

459 460
	void Disable();

461 462 463
	/**
	 * Throws #std::runtime_error on error.
	 */
464
	void Open();
465 466 467 468 469

	/**
	 * Invoke OutputPlugin::open() and configure the
	 * #ConvertFilter.
	 *
470
	 * Throws #std::runtime_error on error.
471
	 *
472
	 * Caller must not lock the mutex.
473
	 */
474
	void OpenOutputAndConvert(AudioFormat audio_format);
475

476 477
	void Close(bool drain);

478 479 480 481 482 483 484
	/**
	 * Close the output plugin.
	 *
	 * Mutex must not be locked.
	 */
	void CloseOutput(bool drain);

485 486 487
	/**
	 * Mutex must not be locked.
	 */
488
	void CloseFilter();
489

490 491 492 493 494 495 496 497
	/**
	 * Wait until the output's delay reaches zero.
	 *
	 * @return true if playback should be continued, false if a
	 * command was issued
	 */
	bool WaitForDelay();

498 499 500
	bool FillSourceOrClose();

	bool PlayChunk();
501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517

	/**
	 * 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
	 */
	bool Play();

	void Pause();

	/**
	 * The OutputThread.
	 */
	void Task();
518 519
};

520 521 522 523
/**
 * Notify object used by the thread's client, i.e. we will send a
 * notify signal to this object, expecting the caller to wait on it.
 */
524 525
extern struct notify audio_output_client_notify;

526 527 528
/**
 * Throws #std::runtime_error on error.
 */
529
AudioOutput *
530 531 532
audio_output_new(EventLoop &event_loop,
		 const ReplayGainConfig &replay_gain_config,
		 const ConfigBlock &block,
533
		 MixerListener &mixer_listener,
534
		 AudioOutputClient &client);
535

536
void
537
audio_output_free(AudioOutput *ao);
538

539
#endif