decoder_control.h 4.96 KB
Newer Older
1 2 3
/*
 * Copyright (C) 2003-2009 The Music Player Daemon Project
 * 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_H
#define MPD_DECODER_CONTROL_H
Warren Dukes's avatar
Warren Dukes committed
22

23
#include "decoder_command.h"
24
#include "audio_format.h"
25 26

#include <glib.h>
Warren Dukes's avatar
Warren Dukes committed
27

28 29
#include <assert.h>

30 31 32
enum decoder_state {
	DECODE_STATE_STOP = 0,
	DECODE_STATE_START,
33 34 35 36 37 38 39 40 41
	DECODE_STATE_DECODE,

	/**
	 * 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.
	 */
	DECODE_STATE_ERROR,
42
};
43

44
struct decoder_control {
45 46 47 48
	/** the handle of the decoder thread, or NULL if the decoder
	    thread isn't running */
	GThread *thread;

49 50 51 52 53 54 55 56 57 58 59 60 61 62
	/**
	 * This lock protects #state and #command.
	 */
	GMutex *mutex;

	/**
	 * 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.
	 */
	GCond *cond;

	enum decoder_state state;
	enum decoder_command command;
63

64
	bool quit;
Max Kellermann's avatar
Max Kellermann committed
65
	bool seek_error;
66
	bool seekable;
67
	double seek_where;
68 69 70 71 72 73 74

	/** the format of the song file */
	struct audio_format in_audio_format;

	/** the format being sent to the music pipe */
	struct audio_format out_audio_format;

75 76 77 78 79 80 81
	/**
	 * The song currently being decoded.  This attribute is set by
	 * the player thread, when it sends the #DECODE_COMMAND_START
	 * command.
	 */
	const struct song *song;

82
	float total_time;
83 84 85 86

	/** the #music_chunk allocator */
	struct music_buffer *buffer;

87 88 89 90
	/**
	 * The destination pipe for decoded chunks.  The caller thread
	 * owns this object, and is responsible for freeing it.
	 */
91
	struct music_pipe *pipe;
92 93
};

94 95
void
dc_init(struct decoder_control *dc);
96

97 98
void
dc_deinit(struct decoder_control *dc);
99

100 101 102 103
/**
 * Locks the #decoder_control object.
 */
static inline void
104
decoder_lock(struct decoder_control *dc)
105
{
106
	g_mutex_lock(dc->mutex);
107 108 109 110 111 112
}

/**
 * Unlocks the #decoder_control object.
 */
static inline void
113
decoder_unlock(struct decoder_control *dc)
114
{
115
	g_mutex_unlock(dc->mutex);
116 117 118 119 120 121 122 123
}

/**
 * Waits for a signal on the #decoder_control object.  This function
 * is only valid in the decoder thread.  The object must be locked
 * prior to calling this function.
 */
static inline void
124
decoder_wait(struct decoder_control *dc)
125
{
126
	g_cond_wait(dc->cond, dc->mutex);
127 128 129 130 131 132 133 134
}

/**
 * Signals the #decoder_control object.  This function is only valid
 * in the player thread.  The object should be locked prior to calling
 * this function.
 */
static inline void
135
decoder_signal(struct decoder_control *dc)
136
{
137
	g_cond_signal(dc->cond);
138 139
}

140 141
static inline bool
decoder_is_idle(const struct decoder_control *dc)
142
{
143 144
	return dc->state == DECODE_STATE_STOP ||
		dc->state == DECODE_STATE_ERROR;
145 146
}

147 148
static inline bool
decoder_is_starting(const struct decoder_control *dc)
149
{
150
	return dc->state == DECODE_STATE_START;
151 152
}

153 154
static inline bool
decoder_has_failed(const struct decoder_control *dc)
155
{
156
	assert(dc->command == DECODE_COMMAND_NONE);
157

158
	return dc->state == DECODE_STATE_ERROR;
159 160
}

161 162
static inline bool
decoder_lock_is_idle(struct decoder_control *dc)
163 164 165
{
	bool ret;

166 167 168
	decoder_lock(dc);
	ret = decoder_is_idle(dc);
	decoder_unlock(dc);
169 170 171 172

	return ret;
}

173 174
static inline bool
decoder_lock_is_starting(struct decoder_control *dc)
175 176 177
{
	bool ret;

178 179 180
	decoder_lock(dc);
	ret = decoder_is_starting(dc);
	decoder_unlock(dc);
181 182 183 184

	return ret;
}

185 186
static inline bool
decoder_lock_has_failed(struct decoder_control *dc)
187 188 189
{
	bool ret;

190 191 192
	decoder_lock(dc);
	ret = decoder_has_failed(dc);
	decoder_unlock(dc);
193 194 195 196

	return ret;
}

197 198
static inline const struct song *
decoder_current_song(const struct decoder_control *dc)
199
{
200
	switch (dc->state) {
201 202
	case DECODE_STATE_STOP:
	case DECODE_STATE_ERROR:
203 204
		return NULL;

205 206
	case DECODE_STATE_START:
	case DECODE_STATE_DECODE:
207
		return dc->song;
208 209 210 211
	}

	assert(false);
	return NULL;
212 213
}

214
void
215
dc_command_wait(struct decoder_control *dc);
Max Kellermann's avatar
Max Kellermann committed
216

217 218 219 220 221 222 223 224
/**
 * Start the decoder.
 *
 * @param the decoder
 * @param song the song to be decoded
 * @param pipe the pipe which receives the decoded chunks (owned by
 * the caller)
 */
225
void
226 227
dc_start(struct decoder_control *dc, struct song *song,
	 struct music_buffer *buffer, struct music_pipe *pipe);
Max Kellermann's avatar
Max Kellermann committed
228

229
void
230
dc_stop(struct decoder_control *dc);
Max Kellermann's avatar
Max Kellermann committed
231

232
bool
233
dc_seek(struct decoder_control *dc, double where);
Max Kellermann's avatar
Max Kellermann committed
234

235
void
236
dc_quit(struct decoder_control *dc);
237

Warren Dukes's avatar
Warren Dukes committed
238
#endif