Control.hxx 10.6 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2021 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 "pcm/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 <cassert>
35
#include <cstdint>
36
#include <exception>
37
#include <memory>
38
#include <utility>
39

40 41 42 43
/* damn you, windows.h! */
#ifdef ERROR
#undef ERROR
#endif
44

45
class DetachedSong;
46
class MusicBuffer;
47
class MusicPipe;
48
class InputCacheManager;
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
	InputCacheManager *const input_cache;

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

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

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

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

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

109
private:
110
	bool quit;
111

112
public:
Max Kellermann's avatar
Max Kellermann committed
113
	bool seek_error;
114
	bool seekable;
115 116 117 118 119 120

	/**
	 * @see #DecoderBridge::initial_seek_essential
	 */
	bool initial_seek_essential;

121
	SongTime seek_time;
122

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

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

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

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

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

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

159
	SignedSongTime total_time;
160

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

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

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

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

176
private:
177
	MixRampInfo mix_ramp, previous_mix_ramp;
178

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

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

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

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

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

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

232
	[[gnu::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
	[[gnu::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
	[[gnu::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
	/**
	 * 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.
	 */
310
	[[gnu::pure]]
311
	bool IsCurrentSong(const DetachedSong &_song) const noexcept;
312

313
	[[gnu::pure]]
314 315 316 317
	bool IsUnseekableCurrentSong(const DetachedSong &_song) const noexcept {
		return !seekable && IsCurrentSong(_song);
	}

318
	[[gnu::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
	 * @param initial_seek_essential see
	 * #DecoderBridge::initial_seek_essential
394 395 396
	 * @param pipe the pipe which receives the decoded chunks (owned by
	 * the caller)
	 */
397 398
	void Start(std::unique_lock<Mutex> &lock,
		   std::unique_ptr<DetachedSong> song,
399
		   SongTime start_time, SongTime end_time,
400
		   bool initial_seek_essential,
401 402
		   MusicBuffer &buffer,
		   std::shared_ptr<MusicPipe> pipe) noexcept;
Max Kellermann's avatar
Max Kellermann committed
403

404 405 406
	/**
	 * Caller must lock the object.
	 */
407
	void Stop(std::unique_lock<Mutex> &lock) noexcept;
408

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

416
	void Quit() noexcept;
417

418
	const char *GetMixRampStart() const noexcept {
419
		return mix_ramp.GetStart();
420 421
	}

422
	const char *GetMixRampEnd() const noexcept {
423
		return mix_ramp.GetEnd();
424 425
	}

426
	const char *GetMixRampPreviousEnd() const noexcept {
427
		return previous_mix_ramp.GetEnd();
428 429
	}

430
	void SetMixRamp(MixRampInfo &&new_value) noexcept {
431 432
		mix_ramp = std::move(new_value);
	}
433 434 435 436 437

	/**
	 * Move mixramp_end to mixramp_prev_end and clear
	 * mixramp_start/mixramp_end.
	 */
438
	void CycleMixRamp() noexcept;
439 440

private:
441
	void RunThread() noexcept;
442 443 444

	/* virtual methods from class InputStreamHandler */
	void OnInputStreamReady() noexcept override {
445
		cond.notify_one();
446 447 448
	}

	void OnInputStreamAvailable() noexcept override {
449
		cond.notify_one();
450
	}
451
};
452

Warren Dukes's avatar
Warren Dukes committed
453
#endif