DecoderControl.hxx 8.4 KB
Newer Older
1
/*
2
 * Copyright (C) 2003-2013 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 "DecoderCommand.hxx"
24
#include "AudioFormat.hxx"
25
#include "MixRampInfo.hxx"
26 27
#include "thread/Mutex.hxx"
#include "thread/Cond.hxx"
28
#include "thread/Thread.hxx"
29
#include "util/Error.hxx"
30

31
#include <assert.h>
32 33 34 35 36 37
#include <stdint.h>

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

39
struct Song;
40
class MusicBuffer;
41
class MusicPipe;
42

43 44 45 46
enum class DecoderState : uint8_t {
	STOP = 0,
	START,
	DECODE,
47 48 49 50 51 52 53

	/**
	 * 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.
	 */
54
	ERROR,
55
};
56

57
struct DecoderControl {
58 59 60 61
	/**
	 * The handle of the decoder thread.
	 */
	Thread thread;
62

63 64
	/**
	 * This lock protects #state and #command.
65 66 67 68 69
	 *
	 * 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.
70
	 */
71
	Mutex &mutex;
72 73 74 75 76 77

	/**
	 * 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.
	 */
78
	Cond cond;
79

80 81 82
	/**
	 * The trigger of this object's client.  It is signalled
	 * whenever an event occurs.
83 84
	 *
	 * This is usually a reference to PlayerControl::cond.
85
	 */
86
	Cond &client_cond;
87

88
	DecoderState state;
89
	DecoderCommand command;
90

91 92
	/**
	 * The error that occurred in the decoder thread.  This
93
	 * attribute is only valid if #state is #DecoderState::ERROR.
94
	 * The object must be freed when this object transitions to
95
	 * any other state (usually #DecoderState::START).
96
	 */
97
	Error error;
98

99
	bool quit;
100 101 102 103 104 105 106 107

	/**
	 * Is the client currently waiting for the DecoderThread?  If
	 * false, the DecoderThread may omit invoking Cond::signal(),
	 * reducing the number of system calls.
	 */
	bool client_is_waiting;

Max Kellermann's avatar
Max Kellermann committed
108
	bool seek_error;
109
	bool seekable;
110
	double seek_where;
111 112

	/** the format of the song file */
113
	AudioFormat in_audio_format;
114 115

	/** the format being sent to the music pipe */
116
	AudioFormat out_audio_format;
117

118 119
	/**
	 * The song currently being decoded.  This attribute is set by
120
	 * the player thread, when it sends the #DecoderCommand::START
121
	 * command.
122 123 124
	 *
	 * This is a duplicate, and must be freed when this attribute
	 * is cleared.
125
	 */
126
	Song *song;
127

128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
	/**
	 * The initial seek position (in milliseconds), e.g. to the
	 * start of a sub-track described by a CUE file.
	 *
	 * This attribute is set by dc_start().
	 */
	unsigned start_ms;

	/**
	 * The decoder will stop when it reaches this position (in
	 * milliseconds).  0 means don't stop before the end of the
	 * file.
	 *
	 * This attribute is set by dc_start().
	 */
	unsigned end_ms;

145
	float total_time;
146 147

	/** the #music_chunk allocator */
148
	MusicBuffer *buffer;
149

150 151 152 153
	/**
	 * The destination pipe for decoded chunks.  The caller thread
	 * owns this object, and is responsible for freeing it.
	 */
154
	MusicPipe *pipe;
155

156 157
	float replay_gain_db;
	float replay_gain_prev_db;
158 159

	MixRampInfo mix_ramp, previous_mix_ramp;
160

161 162 163 164 165
	/**
	 * @param _mutex see #mutex
	 * @param _client_cond see #client_cond
	 */
	DecoderControl(Mutex &_mutex, Cond &_client_cond);
166
	~DecoderControl();
167 168 169 170 171

	/**
	 * Locks the object.
	 */
	void Lock() const {
172
		mutex.lock();
173 174 175 176 177 178
	}

	/**
	 * Unlocks the object.
	 */
	void Unlock() const {
179
		mutex.unlock();
180 181 182 183 184 185 186 187
	}

	/**
	 * Signals the object.  This function is only valid in the
	 * player thread.  The object should be locked prior to
	 * calling this function.
	 */
	void Signal() {
188
		cond.signal();
189 190 191
	}

	/**
192
	 * Waits for a signal on the #DecoderControl object.  This function
193 194 195 196
	 * is only valid in the decoder thread.  The object must be locked
	 * prior to calling this function.
	 */
	void Wait() {
197
		cond.wait(mutex);
198 199
	}

200 201 202 203
	/**
	 * 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.
204 205
	 *
	 * Caller must hold the lock.
206
	 */
207
	void WaitForDecoder();
208

209
	bool IsIdle() const {
210 211
		return state == DecoderState::STOP ||
			state == DecoderState::ERROR;
212 213
	}

214 215 216 217 218 219 220
	gcc_pure
	bool LockIsIdle() const {
		Lock();
		bool result = IsIdle();
		Unlock();
		return result;
	}
221

222
	bool IsStarting() const {
223
		return state == DecoderState::START;
224
	}
225

226 227 228 229 230 231 232
	gcc_pure
	bool LockIsStarting() const {
		Lock();
		bool result = IsStarting();
		Unlock();
		return result;
	}
233

234
	bool HasFailed() const {
235
		assert(command == DecoderCommand::NONE);
236

237
		return state == DecoderState::ERROR;
238
	}
239

240 241 242 243 244 245 246
	gcc_pure
	bool LockHasFailed() const {
		Lock();
		bool result = HasFailed();
		Unlock();
		return result;
	}
247

248
	/**
249 250
	 * Checks whether an error has occurred, and if so, returns a
	 * copy of the #Error object.
251 252 253
	 *
	 * Caller must lock the object.
	 */
254 255
	gcc_pure
	Error GetError() const {
256
		assert(command == DecoderCommand::NONE);
257
		assert(state != DecoderState::ERROR || error.IsDefined());
258

259
		Error result;
260
		if (state == DecoderState::ERROR)
261 262
			result.Set(error);
		return result;
263
	}
264

265 266 267
	/**
	 * Like dc_get_error(), but locks and unlocks the object.
	 */
268 269
	gcc_pure
	Error LockGetError() const {
270
		Lock();
271
		Error result = GetError();
272 273 274
		Unlock();
		return result;
	}
275

276
	/**
277
	 * Clear the error condition and free the #Error object (if any).
278 279 280 281
	 *
	 * Caller must lock the object.
	 */
	void ClearError() {
282
		if (state == DecoderState::ERROR) {
283
			error.Clear();
284
			state = DecoderState::STOP;
285 286
		}
	}
Max Kellermann's avatar
Max Kellermann committed
287

288 289 290 291 292 293 294 295
	/**
	 * 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
296
	bool IsCurrentSong(const Song &_song) const;
297 298

	gcc_pure
299
	bool LockIsCurrentSong(const Song &_song) const {
300 301 302 303 304
		Lock();
		const bool result = IsCurrentSong(_song);
		Unlock();
		return result;
	}
Max Kellermann's avatar
Max Kellermann committed
305

306 307 308 309 310 311 312 313
private:
	/**
	 * Wait for the command to be finished by the decoder thread.
	 *
	 * To be called from the client thread.  Caller must lock the
	 * object.
	 */
	void WaitCommandLocked() {
314
		while (command != DecoderCommand::NONE)
315 316 317 318 319 320 321 322 323 324
			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.
	 */
325
	void SynchronousCommandLocked(DecoderCommand cmd) {
326 327 328 329 330 331 332 333 334 335 336 337
		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.
	 */
338
	void LockSynchronousCommand(DecoderCommand cmd) {
339 340 341 342 343 344
		Lock();
		ClearError();
		SynchronousCommandLocked(cmd);
		Unlock();
	}

345
	void LockAsynchronousCommand(DecoderCommand cmd) {
346 347 348 349 350 351 352
		Lock();
		command = cmd;
		Signal();
		Unlock();
	}

public:
353 354 355 356 357
	/**
	 * Start the decoder.
	 *
	 * @param song the song to be decoded; the given instance will be
	 * owned and freed by the decoder
358 359
	 * @param start_ms see #DecoderControl
	 * @param end_ms see #DecoderControl
360 361 362
	 * @param pipe the pipe which receives the decoded chunks (owned by
	 * the caller)
	 */
363
	void Start(Song *song, unsigned start_ms, unsigned end_ms,
364
		   MusicBuffer &buffer, MusicPipe &pipe);
Max Kellermann's avatar
Max Kellermann committed
365

366
	void Stop();
367

368
	bool Seek(double where);
369

370
	void Quit();
371

372
	const char *GetMixRampStart() const {
373
		return mix_ramp.GetStart();
374 375 376
	}

	const char *GetMixRampEnd() const {
377
		return mix_ramp.GetEnd();
378 379 380
	}

	const char *GetMixRampPreviousEnd() const {
381
		return previous_mix_ramp.GetEnd();
382 383
	}

384 385 386
	void SetMixRamp(MixRampInfo &&new_value) {
		mix_ramp = std::move(new_value);
	}
387 388 389 390 391 392

	/**
	 * Move mixramp_end to mixramp_prev_end and clear
	 * mixramp_start/mixramp_end.
	 */
	void CycleMixRamp();
393
};
394

Warren Dukes's avatar
Warren Dukes committed
395
#endif