Control.hxx 10.3 KB
Newer Older
1
/*
2
 * Copyright 2003-2018 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_DECODER_CONTROL_HXX
#define MPD_DECODER_CONTROL_HXX
Warren Dukes's avatar
Warren Dukes committed
22

23
#include "Command.hxx"
24
#include "AudioFormat.hxx"
25
#include "MixRampInfo.hxx"
26
#include "input/Handler.hxx"
27 28
#include "thread/Mutex.hxx"
#include "thread/Cond.hxx"
29
#include "thread/Thread.hxx"
30
#include "Chrono.hxx"
31 32
#include "ReplayGainConfig.hxx"
#include "ReplayGainMode.hxx"
33 34

#include <exception>
35
#include <utility>
36
#include <memory>
37

38
#include <assert.h>
39 40 41 42 43 44
#include <stdint.h>

/* damn you, windows.h! */
#ifdef ERROR
#undef ERROR
#endif
45

46
class DetachedSong;
47
class MusicBuffer;
48
class MusicPipe;
49

50 51 52 53
enum class DecoderState : uint8_t {
	STOP = 0,
	START,
	DECODE,
54 55 56 57 58 59 60

	/**
	 * The last "START" command failed, because there was an I/O
	 * error or because no decoder was able to decode the file.
	 * This state will only come after START; once the state has
	 * turned to DECODE, by definition no such error can occur.
	 */
61
	ERROR,
62
};
63

64
class DecoderControl final : public InputStreamHandler {
65 66 67 68
	/**
	 * The handle of the decoder thread.
	 */
	Thread thread;
69

70
public:
71 72
	/**
	 * This lock protects #state and #command.
73 74 75 76 77
	 *
	 * This is usually a reference to PlayerControl::mutex, so
	 * that both player thread and decoder thread share a mutex.
	 * This simplifies synchronization with #cond and
	 * #client_cond.
78
	 */
79
	Mutex &mutex;
80 81 82 83 84 85

	/**
	 * Trigger this object after you have modified #command.  This
	 * is also used by the decoder thread to notify the caller
	 * when it has finished a command.
	 */
86
	Cond cond;
87

88 89 90
	/**
	 * The trigger of this object's client.  It is signalled
	 * whenever an event occurs.
91 92
	 *
	 * This is usually a reference to PlayerControl::cond.
93
	 */
94
	Cond &client_cond;
95

96 97
	DecoderState state = DecoderState::STOP;
	DecoderCommand command = DecoderCommand::NONE;
98

99 100
	/**
	 * The error that occurred in the decoder thread.  This
101
	 * attribute is only valid if #state is #DecoderState::ERROR.
102
	 * The object must be freed when this object transitions to
103
	 * any other state (usually #DecoderState::START).
104
	 */
105
	std::exception_ptr error;
106

107
private:
108
	bool quit;
109

110
public:
111 112 113 114 115
	/**
	 * Is the client currently waiting for the DecoderThread?  If
	 * false, the DecoderThread may omit invoking Cond::signal(),
	 * reducing the number of system calls.
	 */
116
	bool client_is_waiting = false;
117

Max Kellermann's avatar
Max Kellermann committed
118
	bool seek_error;
119
	bool seekable;
120
	SongTime seek_time;
121

122
private:
123 124 125 126 127
	/**
	 * The "audio_output_format" setting.
	 */
	const AudioFormat configured_audio_format;

128
public:
129
	/** the format of the song file */
130
	AudioFormat in_audio_format;
131 132

	/** the format being sent to the music pipe */
133
	AudioFormat out_audio_format;
134

135 136
	/**
	 * The song currently being decoded.  This attribute is set by
137
	 * the player thread, when it sends the #DecoderCommand::START
138 139
	 * command.
	 */
140
	std::unique_ptr<DetachedSong> song;
141

142
	/**
143 144
	 * The initial seek position, e.g. to the start of a sub-track
	 * described by a CUE file.
145
	 *
146
	 * This attribute is set by Start().
147
	 */
148
	SongTime start_time;
149 150

	/**
151 152
	 * The decoder will stop when it reaches this position.  0
	 * means don't stop before the end of the file.
153
	 *
154
	 * This attribute is set by Start().
155
	 */
156
	SongTime end_time;
157

158
	SignedSongTime total_time;
159

160 161 162
	/** the #MusicChunk allocator */
	MusicBuffer *buffer;

163 164 165 166
	/**
	 * The destination pipe for decoded chunks.  The caller thread
	 * owns this object, and is responsible for freeing it.
	 */
167
	std::shared_ptr<MusicPipe> pipe;
168

169
	const ReplayGainConfig replay_gain_config;
170 171
	ReplayGainMode replay_gain_mode = ReplayGainMode::OFF;

172 173
	float replay_gain_db = 0;
	float replay_gain_prev_db = 0;
174

175
private:
176
	MixRampInfo mix_ramp, previous_mix_ramp;
177

178
public:
179 180 181 182
	/**
	 * @param _mutex see #mutex
	 * @param _client_cond see #client_cond
	 */
183
	DecoderControl(Mutex &_mutex, Cond &_client_cond,
184
		       const AudioFormat _configured_audio_format,
185 186
		       const ReplayGainConfig &_replay_gain_config) noexcept;
	~DecoderControl() noexcept;
187

188 189 190 191 192 193 194 195
	/**
	 * Throws on error.
	 */
	void StartThread() {
		quit = false;
		thread.Start();
	}

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

	/**
	 * Unlocks the object.
	 */
206
	void Unlock() const noexcept {
207
		mutex.unlock();
208 209 210 211 212 213 214
	}

	/**
	 * Signals the object.  This function is only valid in the
	 * player thread.  The object should be locked prior to
	 * calling this function.
	 */
215
	void Signal() noexcept {
216
		cond.signal();
217 218 219
	}

	/**
220
	 * Waits for a signal on the #DecoderControl object.  This function
221 222 223
	 * is only valid in the decoder thread.  The object must be locked
	 * prior to calling this function.
	 */
224
	void Wait() noexcept {
225
		cond.wait(mutex);
226 227
	}

228 229 230 231
	/**
	 * Waits for a signal from the decoder thread.  This object
	 * must be locked prior to calling this function.  This method
	 * is only valid in the player thread.
232 233
	 *
	 * Caller must hold the lock.
234
	 */
235
	void WaitForDecoder() noexcept;
236

237
	bool IsIdle() const noexcept {
238 239
		return state == DecoderState::STOP ||
			state == DecoderState::ERROR;
240 241
	}

242
	gcc_pure
243
	bool LockIsIdle() const noexcept {
244
		const std::lock_guard<Mutex> protect(mutex);
245
		return IsIdle();
246
	}
247

248
	bool IsStarting() const noexcept {
249
		return state == DecoderState::START;
250
	}
251

252
	gcc_pure
253
	bool LockIsStarting() const noexcept {
254
		const std::lock_guard<Mutex> protect(mutex);
255
		return IsStarting();
256
	}
257

258
	bool HasFailed() const noexcept {
259
		assert(command == DecoderCommand::NONE);
260

261
		return state == DecoderState::ERROR;
262
	}
263

264
	gcc_pure
265
	bool LockHasFailed() const noexcept {
266
		const std::lock_guard<Mutex> protect(mutex);
267
		return HasFailed();
268
	}
269

270 271 272 273 274 275 276
	/**
	 * Transition this obejct from DecoderState::START to
	 * DecoderState::DECODE.
	 *
	 * Caller must lock the object.
	 */
	void SetReady(const AudioFormat audio_format,
277
		      bool _seekable, SignedSongTime _duration) noexcept;
278

279
	/**
280 281
	 * Checks whether an error has occurred, and if so, rethrows
	 * it.
282 283 284
	 *
	 * Caller must lock the object.
	 */
285
	void CheckRethrowError() const {
286
		assert(command == DecoderCommand::NONE);
287
		assert(state != DecoderState::ERROR || error);
288

289
		if (state == DecoderState::ERROR)
290
			std::rethrow_exception(error);
291
	}
292

293
	/**
294
	 * Like CheckRethrowError(), but locks and unlocks the object.
295
	 */
296
	void LockCheckRethrowError() const {
297
		const std::lock_guard<Mutex> protect(mutex);
298
		CheckRethrowError();
299
	}
300

301
	/**
302
	 * Clear the error condition (if any).
303 304 305
	 *
	 * Caller must lock the object.
	 */
306
	void ClearError() noexcept {
307
		if (state == DecoderState::ERROR) {
308
			error = std::exception_ptr();
309
			state = DecoderState::STOP;
310 311
		}
	}
Max Kellermann's avatar
Max Kellermann committed
312

313 314 315 316 317 318 319 320
	/**
	 * Check if the specified song is currently being decoded.  If the
	 * decoder is not running currently (or being started), then this
	 * function returns false in any case.
	 *
	 * Caller must lock the object.
	 */
	gcc_pure
321
	bool IsCurrentSong(const DetachedSong &_song) const noexcept;
322

323 324 325 326 327
	gcc_pure
	bool IsUnseekableCurrentSong(const DetachedSong &_song) const noexcept {
		return !seekable && IsCurrentSong(_song);
	}

328
	gcc_pure
329 330 331 332
	bool IsSeekableCurrentSong(const DetachedSong &_song) const noexcept {
		return seekable && IsCurrentSong(_song);
	}

333 334 335 336 337 338 339
private:
	/**
	 * Wait for the command to be finished by the decoder thread.
	 *
	 * To be called from the client thread.  Caller must lock the
	 * object.
	 */
340
	void WaitCommandLocked() noexcept {
341
		while (command != DecoderCommand::NONE)
342 343 344 345 346 347 348 349 350 351
			WaitForDecoder();
	}

	/**
	 * Send a command to the decoder thread and synchronously wait
	 * for it to finish.
	 *
	 * To be called from the client thread.  Caller must lock the
	 * object.
	 */
352
	void SynchronousCommandLocked(DecoderCommand cmd) noexcept {
353 354 355 356 357 358 359 360 361 362 363 364
		command = cmd;
		Signal();
		WaitCommandLocked();
	}

	/**
	 * Send a command to the decoder thread and synchronously wait
	 * for it to finish.
	 *
	 * To be called from the client thread.  This method locks the
	 * object.
	 */
365
	void LockSynchronousCommand(DecoderCommand cmd) noexcept {
366
		const std::lock_guard<Mutex> protect(mutex);
367 368 369 370
		ClearError();
		SynchronousCommandLocked(cmd);
	}

371
	void LockAsynchronousCommand(DecoderCommand cmd) noexcept {
372
		const std::lock_guard<Mutex> protect(mutex);
373 374 375 376 377
		command = cmd;
		Signal();
	}

public:
378 379 380 381 382 383 384
	/**
	 * Marks the current command as "finished" and notifies the
	 * client (= player thread).
	 *
	 * To be called from the decoder thread.  Caller must lock the
	 * mutex.
	 */
385
	void CommandFinishedLocked() noexcept {
386 387 388 389 390 391
		assert(command != DecoderCommand::NONE);

		command = DecoderCommand::NONE;
		client_cond.signal();
	}

392 393 394
	/**
	 * Start the decoder.
	 *
395 396
	 * Caller must lock the object.
	 *
397 398
	 * @param song the song to be decoded; the given instance will be
	 * owned and freed by the decoder
399 400
	 * @param start_time see #DecoderControl
	 * @param end_time see #DecoderControl
401 402 403
	 * @param pipe the pipe which receives the decoded chunks (owned by
	 * the caller)
	 */
404 405
	void Start(std::unique_ptr<DetachedSong> song,
		   SongTime start_time, SongTime end_time,
406 407
		   MusicBuffer &buffer,
		   std::shared_ptr<MusicPipe> pipe) noexcept;
Max Kellermann's avatar
Max Kellermann committed
408

409 410 411
	/**
	 * Caller must lock the object.
	 */
412
	void Stop() noexcept;
413

414 415
	/**
	 * Throws #std::runtime_error on error.
416 417
	 *
	 * Caller must lock the object.
418 419
	 */
	void Seek(SongTime t);
420

421
	void Quit() noexcept;
422

423
	const char *GetMixRampStart() const noexcept {
424
		return mix_ramp.GetStart();
425 426
	}

427
	const char *GetMixRampEnd() const noexcept {
428
		return mix_ramp.GetEnd();
429 430
	}

431
	const char *GetMixRampPreviousEnd() const noexcept {
432
		return previous_mix_ramp.GetEnd();
433 434
	}

435
	void SetMixRamp(MixRampInfo &&new_value) noexcept {
436 437
		mix_ramp = std::move(new_value);
	}
438 439 440 441 442

	/**
	 * Move mixramp_end to mixramp_prev_end and clear
	 * mixramp_start/mixramp_end.
	 */
443
	void CycleMixRamp() noexcept;
444 445

private:
446
	void RunThread() noexcept;
447 448 449 450 451 452 453 454 455

	/* virtual methods from class InputStreamHandler */
	void OnInputStreamReady() noexcept override {
		cond.signal();
	}

	void OnInputStreamAvailable() noexcept override {
		cond.signal();
	}
456
};
457

Warren Dukes's avatar
Warren Dukes committed
458
#endif