Control.hxx 10.5 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2019 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
class InputCacheManager;
50

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

	/**
	 * 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.
	 */
62
	ERROR,
63
};
64

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

71
public:
72 73
	InputCacheManager *const input_cache;

74 75
	/**
	 * This lock protects #state and #command.
76 77 78 79 80
	 *
	 * 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.
81
	 */
82
	Mutex &mutex;
83 84 85 86 87 88

	/**
	 * 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.
	 */
89
	Cond cond;
90

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

99 100
	DecoderState state = DecoderState::STOP;
	DecoderCommand command = DecoderCommand::NONE;
101

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

110
private:
111
	bool quit;
112

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

Max Kellermann's avatar
Max Kellermann committed
121
	bool seek_error;
122
	bool seekable;
123
	SongTime seek_time;
124

125
private:
126 127 128 129 130
	/**
	 * The "audio_output_format" setting.
	 */
	const AudioFormat configured_audio_format;

131
public:
132
	/** the format of the song file */
133
	AudioFormat in_audio_format;
134 135

	/** the format being sent to the music pipe */
136
	AudioFormat out_audio_format;
137

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

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

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

161
	SignedSongTime total_time;
162

163 164 165
	/** the #MusicChunk allocator */
	MusicBuffer *buffer;

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

172
	const ReplayGainConfig replay_gain_config;
173 174
	ReplayGainMode replay_gain_mode = ReplayGainMode::OFF;

175 176
	float replay_gain_db = 0;
	float replay_gain_prev_db = 0;
177

178
private:
179
	MixRampInfo mix_ramp, previous_mix_ramp;
180

181
public:
182 183 184 185
	/**
	 * @param _mutex see #mutex
	 * @param _client_cond see #client_cond
	 */
186
	DecoderControl(Mutex &_mutex, Cond &_client_cond,
187
		       InputCacheManager *_input_cache,
188
		       const AudioFormat _configured_audio_format,
189 190
		       const ReplayGainConfig &_replay_gain_config) noexcept;
	~DecoderControl() noexcept;
191

192 193 194 195 196 197 198 199
	/**
	 * Throws on error.
	 */
	void StartThread() {
		quit = false;
		thread.Start();
	}

200 201 202 203 204
	/**
	 * Signals the object.  This function is only valid in the
	 * player thread.  The object should be locked prior to
	 * calling this function.
	 */
205
	void Signal() noexcept {
206
		cond.notify_one();
207 208 209
	}

	/**
210
	 * Waits for a signal on the #DecoderControl object.  This function
211 212 213
	 * is only valid in the decoder thread.  The object must be locked
	 * prior to calling this function.
	 */
214 215
	void Wait(std::unique_lock<Mutex> &lock) noexcept {
		cond.wait(lock);
216 217
	}

218 219 220 221
	/**
	 * 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.
222 223
	 *
	 * Caller must hold the lock.
224
	 */
225
	void WaitForDecoder(std::unique_lock<Mutex> &lock) noexcept;
226

227
	bool IsIdle() const noexcept {
228 229
		return state == DecoderState::STOP ||
			state == DecoderState::ERROR;
230 231
	}

232
	gcc_pure
233
	bool LockIsIdle() const noexcept {
234
		const std::lock_guard<Mutex> protect(mutex);
235
		return IsIdle();
236
	}
237

238
	bool IsStarting() const noexcept {
239
		return state == DecoderState::START;
240
	}
241

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

248
	bool HasFailed() const noexcept {
249
		assert(command == DecoderCommand::NONE);
250

251
		return state == DecoderState::ERROR;
252
	}
253

254
	gcc_pure
255
	bool LockHasFailed() const noexcept {
256
		const std::lock_guard<Mutex> protect(mutex);
257
		return HasFailed();
258
	}
259

260 261 262 263 264 265 266
	/**
	 * Transition this obejct from DecoderState::START to
	 * DecoderState::DECODE.
	 *
	 * Caller must lock the object.
	 */
	void SetReady(const AudioFormat audio_format,
267
		      bool _seekable, SignedSongTime _duration) noexcept;
268

269
	/**
270 271
	 * Checks whether an error has occurred, and if so, rethrows
	 * it.
272 273 274
	 *
	 * Caller must lock the object.
	 */
275
	void CheckRethrowError() const {
276
		assert(command == DecoderCommand::NONE);
277
		assert(state != DecoderState::ERROR || error);
278

279
		if (state == DecoderState::ERROR)
280
			std::rethrow_exception(error);
281
	}
282

283
	/**
284
	 * Like CheckRethrowError(), but locks and unlocks the object.
285
	 */
286
	void LockCheckRethrowError() const {
287
		const std::lock_guard<Mutex> protect(mutex);
288
		CheckRethrowError();
289
	}
290

291
	/**
292
	 * Clear the error condition (if any).
293 294 295
	 *
	 * Caller must lock the object.
	 */
296
	void ClearError() noexcept {
297
		if (state == DecoderState::ERROR) {
298
			error = std::exception_ptr();
299
			state = DecoderState::STOP;
300 301
		}
	}
Max Kellermann's avatar
Max Kellermann committed
302

303 304 305 306 307 308 309 310
	/**
	 * 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
311
	bool IsCurrentSong(const DetachedSong &_song) const noexcept;
312

313 314 315 316 317
	gcc_pure
	bool IsUnseekableCurrentSong(const DetachedSong &_song) const noexcept {
		return !seekable && IsCurrentSong(_song);
	}

318
	gcc_pure
319 320 321 322
	bool IsSeekableCurrentSong(const DetachedSong &_song) const noexcept {
		return seekable && IsCurrentSong(_song);
	}

323 324 325 326 327 328 329
private:
	/**
	 * Wait for the command to be finished by the decoder thread.
	 *
	 * To be called from the client thread.  Caller must lock the
	 * object.
	 */
330
	void WaitCommandLocked(std::unique_lock<Mutex> &lock) noexcept {
331
		while (command != DecoderCommand::NONE)
332
			WaitForDecoder(lock);
333 334 335 336 337 338 339 340 341
	}

	/**
	 * 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.
	 */
342 343
	void SynchronousCommandLocked(std::unique_lock<Mutex> &lock,
				      DecoderCommand cmd) noexcept {
344 345
		command = cmd;
		Signal();
346
		WaitCommandLocked(lock);
347 348 349 350 351 352 353 354 355
	}

	/**
	 * 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.
	 */
356
	void LockSynchronousCommand(DecoderCommand cmd) noexcept {
357
		std::unique_lock<Mutex> lock(mutex);
358
		ClearError();
359
		SynchronousCommandLocked(lock, cmd);
360 361
	}

362
	void LockAsynchronousCommand(DecoderCommand cmd) noexcept {
363
		const std::lock_guard<Mutex> protect(mutex);
364 365 366 367 368
		command = cmd;
		Signal();
	}

public:
369 370 371 372 373 374 375
	/**
	 * Marks the current command as "finished" and notifies the
	 * client (= player thread).
	 *
	 * To be called from the decoder thread.  Caller must lock the
	 * mutex.
	 */
376
	void CommandFinishedLocked() noexcept {
377 378 379
		assert(command != DecoderCommand::NONE);

		command = DecoderCommand::NONE;
380
		client_cond.notify_one();
381 382
	}

383 384 385
	/**
	 * Start the decoder.
	 *
386 387
	 * Caller must lock the object.
	 *
388 389
	 * @param song the song to be decoded; the given instance will be
	 * owned and freed by the decoder
390 391
	 * @param start_time see #DecoderControl
	 * @param end_time see #DecoderControl
392 393 394
	 * @param pipe the pipe which receives the decoded chunks (owned by
	 * the caller)
	 */
395 396
	void Start(std::unique_lock<Mutex> &lock,
		   std::unique_ptr<DetachedSong> song,
397
		   SongTime start_time, SongTime end_time,
398 399
		   MusicBuffer &buffer,
		   std::shared_ptr<MusicPipe> pipe) noexcept;
Max Kellermann's avatar
Max Kellermann committed
400

401 402 403
	/**
	 * Caller must lock the object.
	 */
404
	void Stop(std::unique_lock<Mutex> &lock) noexcept;
405

406 407
	/**
	 * Throws #std::runtime_error on error.
408 409
	 *
	 * Caller must lock the object.
410
	 */
411
	void Seek(std::unique_lock<Mutex> &lock, SongTime t);
412

413
	void Quit() noexcept;
414

415
	const char *GetMixRampStart() const noexcept {
416
		return mix_ramp.GetStart();
417 418
	}

419
	const char *GetMixRampEnd() const noexcept {
420
		return mix_ramp.GetEnd();
421 422
	}

423
	const char *GetMixRampPreviousEnd() const noexcept {
424
		return previous_mix_ramp.GetEnd();
425 426
	}

427
	void SetMixRamp(MixRampInfo &&new_value) noexcept {
428 429
		mix_ramp = std::move(new_value);
	}
430 431 432 433 434

	/**
	 * Move mixramp_end to mixramp_prev_end and clear
	 * mixramp_start/mixramp_end.
	 */
435
	void CycleMixRamp() noexcept;
436 437

private:
438
	void RunThread() noexcept;
439 440 441

	/* virtual methods from class InputStreamHandler */
	void OnInputStreamReady() noexcept override {
442
		cond.notify_one();
443 444 445
	}

	void OnInputStreamAvailable() noexcept override {
446
		cond.notify_one();
447
	}
448
};
449

Warren Dukes's avatar
Warren Dukes committed
450
#endif