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 199 200
	/**
	 * Signals the object.  This function is only valid in the
	 * player thread.  The object should be locked prior to
	 * calling this function.
	 */
201
	void Signal() noexcept {
202
		cond.notify_one();
203 204 205
	}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

	gcc_pure
310 311 312 313
	bool IsSeekableCurrentSong(const DetachedSong &_song) const noexcept {
		return seekable && IsCurrentSong(_song);
	}

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

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

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

353
	void LockAsynchronousCommand(DecoderCommand cmd) noexcept {
354
		const std::lock_guard<Mutex> protect(mutex);
355 356 357 358 359
		command = cmd;
		Signal();
	}

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

		command = DecoderCommand::NONE;
371
		client_cond.notify_one();
372 373
	}

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

392 393 394
	/**
	 * Caller must lock the object.
	 */
395
	void Stop(std::unique_lock<Mutex> &lock) noexcept;
396

397 398
	/**
	 * Throws #std::runtime_error on error.
399 400
	 *
	 * Caller must lock the object.
401
	 */
402
	void Seek(std::unique_lock<Mutex> &lock, SongTime t);
403

404
	void Quit() noexcept;
405

406
	const char *GetMixRampStart() const noexcept {
407
		return mix_ramp.GetStart();
408 409
	}

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

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

418
	void SetMixRamp(MixRampInfo &&new_value) noexcept {
419 420
		mix_ramp = std::move(new_value);
	}
421 422 423 424 425

	/**
	 * Move mixramp_end to mixramp_prev_end and clear
	 * mixramp_start/mixramp_end.
	 */
426
	void CycleMixRamp() noexcept;
427 428

private:
429
	void RunThread() noexcept;
430 431 432

	/* virtual methods from class InputStreamHandler */
	void OnInputStreamReady() noexcept override {
433
		cond.notify_one();
434 435 436
	}

	void OnInputStreamAvailable() noexcept override {
437
		cond.notify_one();
438
	}
439
};
440

Warren Dukes's avatar
Warren Dukes committed
441
#endif