Control.hxx 10.4 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2020 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 <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:
Max Kellermann's avatar
Max Kellermann committed
114
	bool seek_error;
115
	bool seekable;
116
	SongTime seek_time;
117

118
private:
119 120 121 122 123
	/**
	 * The "audio_output_format" setting.
	 */
	const AudioFormat configured_audio_format;

124
public:
125
	/** the format of the song file */
126
	AudioFormat in_audio_format;
127 128

	/** the format being sent to the music pipe */
129
	AudioFormat out_audio_format;
130

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

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

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

154
	SignedSongTime total_time;
155

156 157 158
	/** the #MusicChunk allocator */
	MusicBuffer *buffer;

159 160 161 162
	/**
	 * The destination pipe for decoded chunks.  The caller thread
	 * owns this object, and is responsible for freeing it.
	 */
163
	std::shared_ptr<MusicPipe> pipe;
164

165
	const ReplayGainConfig replay_gain_config;
166 167
	ReplayGainMode replay_gain_mode = ReplayGainMode::OFF;

168 169
	float replay_gain_db = 0;
	float replay_gain_prev_db = 0;
170

171
private:
172
	MixRampInfo mix_ramp, previous_mix_ramp;
173

174
public:
175 176 177 178
	/**
	 * @param _mutex see #mutex
	 * @param _client_cond see #client_cond
	 */
179
	DecoderControl(Mutex &_mutex, Cond &_client_cond,
180
		       InputCacheManager *_input_cache,
181
		       const AudioFormat _configured_audio_format,
182 183
		       const ReplayGainConfig &_replay_gain_config) noexcept;
	~DecoderControl() noexcept;
184

185 186 187 188 189 190 191 192
	/**
	 * Throws on error.
	 */
	void StartThread() {
		quit = false;
		thread.Start();
	}

193 194 195 196 197
	/**
	 * Signals the object.  This function is only valid in the
	 * player thread.  The object should be locked prior to
	 * calling this function.
	 */
198
	void Signal() noexcept {
199
		cond.notify_one();
200 201 202
	}

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

211 212 213 214
	/**
	 * 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.
215 216
	 *
	 * Caller must hold the lock.
217
	 */
218 219 220
	void WaitForDecoder(std::unique_lock<Mutex> &lock) noexcept {
		client_cond.wait(lock);
	}
221

222
	bool IsIdle() const noexcept {
223 224
		return state == DecoderState::STOP ||
			state == DecoderState::ERROR;
225 226
	}

227
	gcc_pure
228
	bool LockIsIdle() const noexcept {
229
		const std::lock_guard<Mutex> protect(mutex);
230
		return IsIdle();
231
	}
232

233
	bool IsStarting() const noexcept {
234
		return state == DecoderState::START;
235
	}
236

237
	gcc_pure
238
	bool LockIsStarting() const noexcept {
239
		const std::lock_guard<Mutex> protect(mutex);
240
		return IsStarting();
241
	}
242

243
	bool HasFailed() const noexcept {
244
		assert(command == DecoderCommand::NONE);
245

246
		return state == DecoderState::ERROR;
247
	}
248

249
	gcc_pure
250
	bool LockHasFailed() const noexcept {
251
		const std::lock_guard<Mutex> protect(mutex);
252
		return HasFailed();
253
	}
254

255 256 257 258 259 260 261
	/**
	 * Transition this obejct from DecoderState::START to
	 * DecoderState::DECODE.
	 *
	 * Caller must lock the object.
	 */
	void SetReady(const AudioFormat audio_format,
262
		      bool _seekable, SignedSongTime _duration) noexcept;
263

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

274
		if (state == DecoderState::ERROR)
275
			std::rethrow_exception(error);
276
	}
277

278
	/**
279
	 * Like CheckRethrowError(), but locks and unlocks the object.
280
	 */
281
	void LockCheckRethrowError() const {
282
		const std::lock_guard<Mutex> protect(mutex);
283
		CheckRethrowError();
284
	}
285

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

298 299 300 301 302 303 304 305
	/**
	 * 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
306
	bool IsCurrentSong(const DetachedSong &_song) const noexcept;
307

308 309 310 311 312
	gcc_pure
	bool IsUnseekableCurrentSong(const DetachedSong &_song) const noexcept {
		return !seekable && IsCurrentSong(_song);
	}

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

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

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

	/**
	 * 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.
	 */
351
	void LockSynchronousCommand(DecoderCommand cmd) noexcept {
352
		std::unique_lock<Mutex> lock(mutex);
353
		ClearError();
354
		SynchronousCommandLocked(lock, cmd);
355 356
	}

357
	void LockAsynchronousCommand(DecoderCommand cmd) noexcept {
358
		const std::lock_guard<Mutex> protect(mutex);
359 360 361 362 363
		command = cmd;
		Signal();
	}

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

		command = DecoderCommand::NONE;
375
		client_cond.notify_one();
376 377
	}

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

396 397 398
	/**
	 * Caller must lock the object.
	 */
399
	void Stop(std::unique_lock<Mutex> &lock) noexcept;
400

401 402
	/**
	 * Throws #std::runtime_error on error.
403 404
	 *
	 * Caller must lock the object.
405
	 */
406
	void Seek(std::unique_lock<Mutex> &lock, SongTime t);
407

408
	void Quit() noexcept;
409

410
	const char *GetMixRampStart() const noexcept {
411
		return mix_ramp.GetStart();
412 413
	}

414
	const char *GetMixRampEnd() const noexcept {
415
		return mix_ramp.GetEnd();
416 417
	}

418
	const char *GetMixRampPreviousEnd() const noexcept {
419
		return previous_mix_ramp.GetEnd();
420 421
	}

422
	void SetMixRamp(MixRampInfo &&new_value) noexcept {
423 424
		mix_ramp = std::move(new_value);
	}
425 426 427 428 429

	/**
	 * Move mixramp_end to mixramp_prev_end and clear
	 * mixramp_start/mixramp_end.
	 */
430
	void CycleMixRamp() noexcept;
431 432

private:
433
	void RunThread() noexcept;
434 435 436

	/* virtual methods from class InputStreamHandler */
	void OnInputStreamReady() noexcept override {
437
		cond.notify_one();
438 439 440
	}

	void OnInputStreamAvailable() noexcept override {
441
		cond.notify_one();
442
	}
443
};
444

Warren Dukes's avatar
Warren Dukes committed
445
#endif