DecoderControl.hxx 8.2 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;
Max Kellermann's avatar
Max Kellermann committed
100
	bool seek_error;
101
	bool seekable;
102
	double seek_where;
103 104

	/** the format of the song file */
105
	AudioFormat in_audio_format;
106 107

	/** the format being sent to the music pipe */
108
	AudioFormat out_audio_format;
109

110 111
	/**
	 * The song currently being decoded.  This attribute is set by
112
	 * the player thread, when it sends the #DecoderCommand::START
113
	 * command.
114 115 116
	 *
	 * This is a duplicate, and must be freed when this attribute
	 * is cleared.
117
	 */
118
	Song *song;
119

120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
	/**
	 * 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;

137
	float total_time;
138 139

	/** the #music_chunk allocator */
140
	MusicBuffer *buffer;
141

142 143 144 145
	/**
	 * The destination pipe for decoded chunks.  The caller thread
	 * owns this object, and is responsible for freeing it.
	 */
146
	MusicPipe *pipe;
147

148 149
	float replay_gain_db;
	float replay_gain_prev_db;
150 151

	MixRampInfo mix_ramp, previous_mix_ramp;
152

153 154 155 156 157
	/**
	 * @param _mutex see #mutex
	 * @param _client_cond see #client_cond
	 */
	DecoderControl(Mutex &_mutex, Cond &_client_cond);
158
	~DecoderControl();
159 160 161 162 163

	/**
	 * Locks the object.
	 */
	void Lock() const {
164
		mutex.lock();
165 166 167 168 169 170
	}

	/**
	 * Unlocks the object.
	 */
	void Unlock() const {
171
		mutex.unlock();
172 173 174 175 176 177 178 179
	}

	/**
	 * Signals the object.  This function is only valid in the
	 * player thread.  The object should be locked prior to
	 * calling this function.
	 */
	void Signal() {
180
		cond.signal();
181 182 183
	}

	/**
184
	 * Waits for a signal on the #DecoderControl object.  This function
185 186 187 188
	 * is only valid in the decoder thread.  The object must be locked
	 * prior to calling this function.
	 */
	void Wait() {
189
		cond.wait(mutex);
190 191
	}

192 193 194 195 196 197
	/**
	 * 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.
	 */
	void WaitForDecoder() {
198
		client_cond.wait(mutex);
199
	}
200

201
	bool IsIdle() const {
202 203
		return state == DecoderState::STOP ||
			state == DecoderState::ERROR;
204 205
	}

206 207 208 209 210 211 212
	gcc_pure
	bool LockIsIdle() const {
		Lock();
		bool result = IsIdle();
		Unlock();
		return result;
	}
213

214
	bool IsStarting() const {
215
		return state == DecoderState::START;
216
	}
217

218 219 220 221 222 223 224
	gcc_pure
	bool LockIsStarting() const {
		Lock();
		bool result = IsStarting();
		Unlock();
		return result;
	}
225

226
	bool HasFailed() const {
227
		assert(command == DecoderCommand::NONE);
228

229
		return state == DecoderState::ERROR;
230
	}
231

232 233 234 235 236 237 238
	gcc_pure
	bool LockHasFailed() const {
		Lock();
		bool result = HasFailed();
		Unlock();
		return result;
	}
239

240
	/**
241 242
	 * Checks whether an error has occurred, and if so, returns a
	 * copy of the #Error object.
243 244 245
	 *
	 * Caller must lock the object.
	 */
246 247
	gcc_pure
	Error GetError() const {
248
		assert(command == DecoderCommand::NONE);
249
		assert(state != DecoderState::ERROR || error.IsDefined());
250

251
		Error result;
252
		if (state == DecoderState::ERROR)
253 254
			result.Set(error);
		return result;
255
	}
256

257 258 259
	/**
	 * Like dc_get_error(), but locks and unlocks the object.
	 */
260 261
	gcc_pure
	Error LockGetError() const {
262
		Lock();
263
		Error result = GetError();
264 265 266
		Unlock();
		return result;
	}
267

268
	/**
269
	 * Clear the error condition and free the #Error object (if any).
270 271 272 273
	 *
	 * Caller must lock the object.
	 */
	void ClearError() {
274
		if (state == DecoderState::ERROR) {
275
			error.Clear();
276
			state = DecoderState::STOP;
277 278
		}
	}
Max Kellermann's avatar
Max Kellermann committed
279

280 281 282 283 284 285 286 287
	/**
	 * 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
288
	bool IsCurrentSong(const Song &_song) const;
289 290

	gcc_pure
291
	bool LockIsCurrentSong(const Song &_song) const {
292 293 294 295 296
		Lock();
		const bool result = IsCurrentSong(_song);
		Unlock();
		return result;
	}
Max Kellermann's avatar
Max Kellermann committed
297

298 299 300 301 302 303 304 305
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() {
306
		while (command != DecoderCommand::NONE)
307 308 309 310 311 312 313 314 315 316
			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.
	 */
317
	void SynchronousCommandLocked(DecoderCommand cmd) {
318 319 320 321 322 323 324 325 326 327 328 329
		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.
	 */
330
	void LockSynchronousCommand(DecoderCommand cmd) {
331 332 333 334 335 336
		Lock();
		ClearError();
		SynchronousCommandLocked(cmd);
		Unlock();
	}

337
	void LockAsynchronousCommand(DecoderCommand cmd) {
338 339 340 341 342 343 344
		Lock();
		command = cmd;
		Signal();
		Unlock();
	}

public:
345 346 347 348 349
	/**
	 * Start the decoder.
	 *
	 * @param song the song to be decoded; the given instance will be
	 * owned and freed by the decoder
350 351
	 * @param start_ms see #DecoderControl
	 * @param end_ms see #DecoderControl
352 353 354
	 * @param pipe the pipe which receives the decoded chunks (owned by
	 * the caller)
	 */
355
	void Start(Song *song, unsigned start_ms, unsigned end_ms,
356
		   MusicBuffer &buffer, MusicPipe &pipe);
Max Kellermann's avatar
Max Kellermann committed
357

358
	void Stop();
359

360
	bool Seek(double where);
361

362
	void Quit();
363

364
	const char *GetMixRampStart() const {
365
		return mix_ramp.GetStart();
366 367 368
	}

	const char *GetMixRampEnd() const {
369
		return mix_ramp.GetEnd();
370 371 372
	}

	const char *GetMixRampPreviousEnd() const {
373
		return previous_mix_ramp.GetEnd();
374 375
	}

376 377 378
	void SetMixRamp(MixRampInfo &&new_value) {
		mix_ramp = std::move(new_value);
	}
379 380 381 382 383 384

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

Warren Dukes's avatar
Warren Dukes committed
387
#endif