Control.hxx 10.6 KB
Newer Older
1
/*
2
 * Copyright 2003-2016 The Music Player Daemon Project
3
 * http://www.musicpd.org
Warren Dukes's avatar
Warren Dukes committed
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.
Warren Dukes's avatar
Warren Dukes committed
18 19
 */

20 21
#ifndef MPD_PLAYER_CONTROL_HXX
#define MPD_PLAYER_CONTROL_HXX
Warren Dukes's avatar
Warren Dukes committed
22

23
#include "AudioFormat.hxx"
24 25
#include "thread/Mutex.hxx"
#include "thread/Cond.hxx"
26
#include "thread/Thread.hxx"
27
#include "CrossFade.hxx"
28
#include "Chrono.hxx"
29 30
#include "ReplayGainConfig.hxx"
#include "ReplayGainMode.hxx"
31

32 33
#include <exception>

34
#include <stdint.h>
Warren Dukes's avatar
Warren Dukes committed
35

36
class PlayerListener;
37
class MultipleOutputs;
38
class DetachedSong;
39

40 41 42 43
enum class PlayerState : uint8_t {
	STOP,
	PAUSE,
	PLAY
44
};
Warren Dukes's avatar
Warren Dukes committed
45

46 47 48 49 50 51 52
enum class PlayerCommand : uint8_t {
	NONE,
	EXIT,
	STOP,
	PAUSE,
	SEEK,
	CLOSE_AUDIO,
53

54
	/**
55
	 * At least one AudioOutput.enabled flag has been modified;
56 57
	 * commit those changes to the output threads.
	 */
58
	UPDATE_AUDIO,
59

60
	/** PlayerControl.next_song has been updated */
61
	QUEUE,
62 63

	/**
64
	 * cancel pre-decoding PlayerControl.next_song; if the player
65 66 67
	 * has already started playing this song, it will completely
	 * stop
	 */
68
	CANCEL,
69 70

	/**
71
	 * Refresh status information in the #PlayerControl struct,
72 73
	 * e.g. elapsed_time.
	 */
74
	REFRESH,
75 76
};

77 78
enum class PlayerError : uint8_t {
	NONE,
79 80 81 82

	/**
	 * The decoder has failed to decode the song.
	 */
83
	DECODER,
84 85 86 87

	/**
	 * The audio output has failed.
	 */
88
	OUTPUT,
89
};
Warren Dukes's avatar
Warren Dukes committed
90

91
struct player_status {
92
	PlayerState state;
93
	uint16_t bit_rate;
94
	AudioFormat audio_format;
95
	SignedSongTime total_time;
96
	SongTime elapsed_time;
97 98
};

99
struct PlayerControl {
100 101
	PlayerListener &listener;

102 103
	MultipleOutputs &outputs;

104
	const unsigned buffer_chunks;
105

106
	const unsigned buffered_before_play;
107

108 109 110 111 112
	/**
	 * The "audio_output_format" setting.
	 */
	const AudioFormat configured_audio_format;

113 114 115 116
	/**
	 * The handle of the player thread.
	 */
	Thread thread;
117

118
	/**
119
	 * This lock protects #command, #state, #error, #tagged_song.
120
	 */
121
	mutable Mutex mutex;
122 123 124 125

	/**
	 * Trigger this object after you have modified #command.
	 */
126
	Cond cond;
127

128 129 130 131 132 133 134
	/**
	 * This object gets signalled when the player thread has
	 * finished the #command.  It wakes up the client that waits
	 * (i.e. the main thread).
	 */
	Cond client_cond;

135 136
	PlayerCommand command = PlayerCommand::NONE;
	PlayerState state = PlayerState::STOP;
137

138
	PlayerError error_type = PlayerError::NONE;
139

140 141
	/**
	 * The error that occurred in the player thread.  This
142
	 * attribute is only valid if #error_type is not
143 144
	 * #PlayerError::NONE.  The object must be freed when this
	 * object transitions back to #PlayerError::NONE.
145
	 */
146
	std::exception_ptr error;
147

148
	/**
149 150 151
	 * A copy of the current #DetachedSong after its tags have
	 * been updated by the decoder (for example, a radio stream
	 * that has sent a new tag after switching to the next song).
152 153
	 * This shall be used by PlayerListener::OnPlayerTagModified()
	 * to update the current #DetachedSong in the queue.
154 155 156 157
	 *
	 * Protected by #mutex.  Set by the PlayerThread and consumed
	 * by the main thread.
	 */
158
	DetachedSong *tagged_song = nullptr;
159

160
	uint16_t bit_rate;
161
	AudioFormat audio_format;
162
	SignedSongTime total_time;
163
	SongTime elapsed_time;
164 165 166 167 168 169 170

	/**
	 * The next queued song.
	 *
	 * This is a duplicate, and must be freed when this attribute
	 * is cleared.
	 */
171
	DetachedSong *next_song = nullptr;
172

173
	SongTime seek_time;
174 175 176

	CrossFadeSettings cross_fade;

177
	const ReplayGainConfig replay_gain_config;
178 179
	ReplayGainMode replay_gain_mode = ReplayGainMode::OFF;

180
	double total_play_time = 0;
181 182 183 184 185 186 187 188

	/**
	 * If this flag is set, then the player will be auto-paused at
	 * the end of the song, before the next song starts to play.
	 *
	 * This is a copy of the queue's "single" flag most of the
	 * time.
	 */
189
	bool border_pause = false;
Warren Dukes's avatar
Warren Dukes committed
190

191 192
	PlayerControl(PlayerListener &_listener,
		      MultipleOutputs &_outputs,
193
		      unsigned buffer_chunks,
194
		      unsigned buffered_before_play,
195
		      AudioFormat _configured_audio_format,
196
		      const ReplayGainConfig &_replay_gain_config);
197
	~PlayerControl();
198

199 200 201 202 203 204
	/**
	 * Locks the object.
	 */
	void Lock() const {
		mutex.lock();
	}
205

206 207 208 209 210 211
	/**
	 * Unlocks the object.
	 */
	void Unlock() const {
		mutex.unlock();
	}
Warren Dukes's avatar
Warren Dukes committed
212

213 214 215 216 217 218 219
	/**
	 * Signals the object.  The object should be locked prior to
	 * calling this function.
	 */
	void Signal() {
		cond.signal();
	}
220

221 222 223 224 225
	/**
	 * Signals the object.  The object is temporarily locked by
	 * this function.
	 */
	void LockSignal() {
226
		const ScopeLock protect(mutex);
227 228
		Signal();
	}
229

230 231 232 233 234 235
	/**
	 * Waits for a signal on the object.  This function is only
	 * valid in the player thread.  The object must be locked
	 * prior to calling this function.
	 */
	void Wait() {
236
		assert(thread.IsInside());
237

238 239
		cond.wait(mutex);
	}
Warren Dukes's avatar
Warren Dukes committed
240

241 242 243 244 245 246
	/**
	 * Wake up the client waiting for command completion.
	 *
	 * Caller must lock the object.
	 */
	void ClientSignal() {
247
		assert(thread.IsInside());
248 249 250 251 252 253 254 255 256 257 258

		client_cond.signal();
	}

	/**
	 * The client calls this method to wait for command
	 * completion.
	 *
	 * Caller must lock the object.
	 */
	void ClientWait() {
259
		assert(!thread.IsInside());
260 261 262 263

		client_cond.wait(mutex);
	}

264 265 266 267 268 269 270 271
	/**
	 * A command has been finished.  This method clears the
	 * command and signals the client.
	 *
	 * To be called from the player thread.  Caller must lock the
	 * object.
	 */
	void CommandFinished() {
272
		assert(command != PlayerCommand::NONE);
273

274
		command = PlayerCommand::NONE;
275 276 277
		ClientSignal();
	}

278
	void LockCommandFinished() {
279
		const ScopeLock protect(mutex);
280 281 282
		CommandFinished();
	}

283 284 285 286 287 288 289 290
private:
	/**
	 * Wait for the command to be finished by the player thread.
	 *
	 * To be called from the main thread.  Caller must lock the
	 * object.
	 */
	void WaitCommandLocked() {
291
		while (command != PlayerCommand::NONE)
292 293 294 295 296 297 298 299 300 301
			ClientWait();
	}

	/**
	 * Send a command to the player thread and synchronously wait
	 * for it to finish.
	 *
	 * To be called from the main thread.  Caller must lock the
	 * object.
	 */
302 303
	void SynchronousCommand(PlayerCommand cmd) {
		assert(command == PlayerCommand::NONE);
304 305 306 307 308 309 310 311 312 313 314 315 316

		command = cmd;
		Signal();
		WaitCommandLocked();
	}

	/**
	 * Send a command to the player thread and synchronously wait
	 * for it to finish.
	 *
	 * To be called from the main thread.  This method locks the
	 * object.
	 */
317
	void LockSynchronousCommand(PlayerCommand cmd) {
318
		const ScopeLock protect(mutex);
319 320 321 322
		SynchronousCommand(cmd);
	}

public:
323
	/**
324 325
	 * Throws std::runtime_error or #Error on error.
	 *
326 327 328
	 * @param song the song to be queued; the given instance will
	 * be owned and freed by the player
	 */
329
	void Play(DetachedSong *song);
330

331
	/**
332
	 * see PlayerCommand::CANCEL
333
	 */
334
	void LockCancel();
Warren Dukes's avatar
Warren Dukes committed
335

336
	void LockSetPause(bool pause_flag);
Warren Dukes's avatar
Warren Dukes committed
337

338 339 340
private:
	void PauseLocked();

341 342
	void ClearError() {
		error_type = PlayerError::NONE;
343
		error = std::exception_ptr();
344 345
	}

346
public:
347
	void LockPause();
Warren Dukes's avatar
Warren Dukes committed
348

349 350 351
	/**
	 * Set the player's #border_pause flag.
	 */
352
	void LockSetBorderPause(bool border_pause);
353

354 355 356 357 358 359 360 361 362 363 364
	bool ApplyBorderPause() {
		if (border_pause)
			state = PlayerState::PAUSE;
		return border_pause;
	}

	bool LockApplyBorderPause() {
		const ScopeLock lock(mutex);
		return ApplyBorderPause();
	}

365
	void Kill();
Warren Dukes's avatar
Warren Dukes committed
366

367
	gcc_pure
368
	player_status LockGetStatus();
Warren Dukes's avatar
Warren Dukes committed
369

370
	PlayerState GetState() const {
371 372
		return state;
	}
Warren Dukes's avatar
Warren Dukes committed
373

374 375 376 377 378
	/**
	 * Set the error.  Discards any previous error condition.
	 *
	 * Caller must lock the object.
	 *
379
	 * @param type the error type; must not be #PlayerError::NONE
380
	 */
381
	void SetError(PlayerError type, std::exception_ptr &&_error);
382

383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
	/**
	 * Set the error and set state to PlayerState::PAUSE.
	 */
	void SetOutputError(std::exception_ptr &&_error) {
		SetError(PlayerError::OUTPUT, std::move(_error));

		/* pause: the user may resume playback as soon as an
		   audio output becomes available */
		state = PlayerState::PAUSE;
	}

	void LockSetOutputError(std::exception_ptr &&_error) {
		const ScopeLock lock(mutex);
		SetOutputError(std::move(_error));
	}

399
	/**
400 401
	 * Checks whether an error has occurred, and if so, rethrows
	 * it.
402 403 404
	 *
	 * Caller must lock the object.
	 */
405
	void CheckRethrowError() const {
406
		if (error_type != PlayerError::NONE)
407
			std::rethrow_exception(error);
408
	}
409

410
	/**
411
	 * Like CheckRethrowError(), but locks and unlocks the object.
412
	 */
413
	void LockCheckRethrowError() const {
414
		const ScopeLock protect(mutex);
415
		CheckRethrowError();
416 417
	}

418
	void LockClearError();
Warren Dukes's avatar
Warren Dukes committed
419

420
	PlayerError GetErrorType() const {
421 422 423
		return error_type;
	}

424 425
	/**
	 * Set the #tagged_song attribute to a newly allocated copy of
426
	 * the given #DetachedSong.  Locks and unlocks the object.
427
	 */
428
	void LockSetTaggedSong(const DetachedSong &song);
429 430 431 432 433 434 435 436

	void ClearTaggedSong();

	/**
	 * Read and clear the #tagged_song attribute.
	 *
	 * Caller must lock the object.
	 */
437 438
	DetachedSong *ReadTaggedSong() {
		DetachedSong *result = tagged_song;
439 440 441 442 443 444 445
		tagged_song = nullptr;
		return result;
	}

	/**
	 * Like ReadTaggedSong(), but locks and unlocks the object.
	 */
446
	DetachedSong *LockReadTaggedSong() {
447 448
		const ScopeLock protect(mutex);
		return ReadTaggedSong();
449 450
	}

451
	void LockStop();
452

453
	void LockUpdateAudio();
454

455
private:
456
	void EnqueueSongLocked(DetachedSong *song) {
457 458 459 460
		assert(song != nullptr);
		assert(next_song == nullptr);

		next_song = song;
461
		seek_time = SongTime::zero();
462
		SynchronousCommand(PlayerCommand::QUEUE);
463 464
	}

465 466 467 468
	/**
	 * Throws std::runtime_error or #Error on error.
	 */
	void SeekLocked(DetachedSong *song, SongTime t);
469

470
public:
471 472 473 474
	/**
	 * @param song the song to be queued; the given instance will be owned
	 * and freed by the player
	 */
475
	void LockEnqueueSong(DetachedSong *song);
476 477 478 479

	/**
	 * Makes the player thread seek the specified song to a position.
	 *
480 481
	 * Throws std::runtime_error or #Error on error.
	 *
482 483 484
	 * @param song the song to be queued; the given instance will be owned
	 * and freed by the player
	 */
485
	void LockSeek(DetachedSong *song, SongTime t);
486 487 488 489

	void SetCrossFade(float cross_fade_seconds);

	float GetCrossFade() const {
490
		return cross_fade.duration;
491 492 493 494 495
	}

	void SetMixRampDb(float mixramp_db);

	float GetMixRampDb() const {
496
		return cross_fade.mixramp_db;
497 498 499 500 501
	}

	void SetMixRampDelay(float mixramp_delay_seconds);

	float GetMixRampDelay() const {
502
		return cross_fade.mixramp_delay;
503 504
	}

505
	void LockSetReplayGainMode(ReplayGainMode _mode) {
506 507 508 509
		const ScopeLock protect(mutex);
		replay_gain_mode = _mode;
	}

510 511 512 513
	double GetTotalPlayTime() const {
		return total_play_time;
	}
};
514

Warren Dukes's avatar
Warren Dukes committed
515
#endif