outputBuffer.c 4.5 KB
Newer Older
1
/* the Music Player Daemon (MPD)
2
 * Copyright (C) 2003-2007 by Warren Dukes (warren.dukes@gmail.com)
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 * This project's homepage is: http://www.musicpd.org
 *
 * 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.
 * 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include "outputBuffer.h"

#include "utils.h"

23 24
struct output_buffer ob;

25
void ob_init(unsigned int size, Notify *notify)
26
{
27 28
	assert(size > 0);

29 30 31 32
	ob.chunks = xmalloc(size * sizeof(*ob.chunks));
	ob.size = size;
	ob.begin = 0;
	ob.end = 0;
Max Kellermann's avatar
Max Kellermann committed
33
	ob.lazy = 0;
34
	ob.notify = notify;
35
	ob.chunks[0].chunkSize = 0;
Warren Dukes's avatar
Warren Dukes committed
36 37
}

38
void ob_free(void)
39
{
40 41
	assert(ob.chunks != NULL);
	free(ob.chunks);
42 43
}

44
void ob_clear(void)
Avuton Olrich's avatar
Avuton Olrich committed
45
{
46 47
	ob.end = ob.begin;
	ob.chunks[ob.end].chunkSize = 0;
48 49
}

50
/** return the index of the chunk after i */
51
static inline unsigned successor(unsigned i)
52
{
53
	assert(i <= ob.size);
54 55

	++i;
56
	return i == ob.size ? 0 : i;
57 58
}

59 60 61 62
/**
 * Mark the tail chunk as "full" and wake up the player if is waiting
 * for the decoder.
 */
63
static void output_buffer_expand(unsigned i)
64
{
65
	int was_empty = ob.notify != NULL && (!ob.lazy || ob_is_empty());
66

67 68
	assert(i == (ob.end + 1) % ob.size);
	assert(i != ob.end);
69

70 71
	ob.end = i;
	ob.chunks[i].chunkSize = 0;
72 73 74 75
	if (was_empty)
		/* if the buffer was empty, the player thread might be
		   waiting for us; wake it up now that another decoded
		   buffer has become available. */
76
		notify_signal(ob.notify);
77 78
}

79
void ob_flush(void)
Avuton Olrich's avatar
Avuton Olrich committed
80
{
81
	ob_chunk *chunk = ob_get_chunk(ob.end);
82 83

	if (chunk->chunkSize > 0) {
84 85
		unsigned int next = successor(ob.end);
		if (next == ob.begin)
86 87 88 89 90
			/* all buffers are full; we have to wait for
			   the player to free one, so don't flush
			   right now */
			return;

91
		output_buffer_expand(next);
92
	}
93 94
}

Max Kellermann's avatar
Max Kellermann committed
95 96 97 98 99
void ob_set_lazy(int lazy)
{
	ob.lazy = lazy;
}

100
int ob_is_empty(void)
101
{
102
	return ob.begin == ob.end;
103 104
}

105
void ob_shift(void)
106
{
107 108
	assert(ob.begin != ob.end);
	assert(ob.begin < ob.size);
109

110
	ob.begin = successor(ob.begin);
111 112
}

113
unsigned int ob_relative(const unsigned i)
114
{
115 116
	if (i >= ob.begin)
		return i - ob.begin;
117
	else
118
		return i + ob.size - ob.begin;
119 120
}

121
unsigned ob_available(void)
122
{
123
	return ob_relative(ob.end);
124 125
}

126
int ob_absolute(const unsigned relative)
127 128 129
{
	unsigned i, max;

130 131 132 133
	max = ob.end;
	if (max < ob.begin)
		max += ob.size;
	i = (unsigned)ob.begin + relative;
134 135 136
	if (i >= max)
		return -1;

137 138
	if (i >= ob.size)
		i -= ob.size;
139 140 141 142

	return (int)i;
}

143
ob_chunk * ob_get_chunk(const unsigned i)
144
{
145
	assert(i < ob.size);
146

147
	return &ob.chunks[i];
148 149
}

150
/**
Max Kellermann's avatar
Max Kellermann committed
151
 * Return the tail chunk which has room for additional data.
152
 *
153
 * @return the chunk which has room for more data; NULL if there is no
Max Kellermann's avatar
Max Kellermann committed
154
 * room.
155
 */
156
static ob_chunk *tail_chunk(float data_time, mpd_uint16 bitRate)
157 158
{
	unsigned int next;
159
	ob_chunk *chunk;
160

161
	chunk = ob_get_chunk(ob.end);
162 163 164
	assert(chunk->chunkSize <= sizeof(chunk->data));
	if (chunk->chunkSize == sizeof(chunk->data)) {
		/* this chunk is full; allocate a new chunk */
165
		next = successor(ob.end);
Max Kellermann's avatar
Max Kellermann committed
166 167
		if (ob.begin == next)
			/* no chunks available */
168
			return NULL;
169

170
		output_buffer_expand(next);
171
		chunk = ob_get_chunk(next);
172
		assert(chunk->chunkSize == 0);
173 174
	}

175 176 177 178 179 180 181
	if (chunk->chunkSize == 0) {
		/* if the chunk is empty, nobody has set bitRate and
		   times yet */

		chunk->bitRate = bitRate;
		chunk->times = data_time;
	}
182

183
	return chunk;
184 185
}

Max Kellermann's avatar
Max Kellermann committed
186 187
size_t ob_append(const void *data0, size_t datalen,
		 float data_time, mpd_uint16 bitRate)
188
{
Max Kellermann's avatar
Max Kellermann committed
189 190
	const unsigned char *data = data0;
	size_t ret = 0, dataToSend;
191
	ob_chunk *chunk = NULL;
192

Avuton Olrich's avatar
Avuton Olrich committed
193
	while (datalen) {
194 195
		chunk = tail_chunk(data_time, bitRate);
		if (chunk == NULL)
Max Kellermann's avatar
Max Kellermann committed
196
			return ret;
197

198 199 200
		dataToSend = sizeof(chunk->data) - chunk->chunkSize;
		if (dataToSend > datalen)
			dataToSend = datalen;
201

202 203
		memcpy(chunk->data + chunk->chunkSize, data, dataToSend);
		chunk->chunkSize += dataToSend;
Avuton Olrich's avatar
Avuton Olrich committed
204 205
		datalen -= dataToSend;
		data += dataToSend;
Max Kellermann's avatar
Max Kellermann committed
206
		ret += dataToSend;
Avuton Olrich's avatar
Avuton Olrich committed
207
	}
208

209
	if (chunk != NULL && chunk->chunkSize == sizeof(chunk->data))
210
		ob_flush();
211

Max Kellermann's avatar
Max Kellermann committed
212
	return ret;
213
}
Warren Dukes's avatar
Warren Dukes committed
214

215
void ob_skip(unsigned num)
216
{
217
	int i = ob_absolute(num);
218
	if (i >= 0)
219
		ob.begin = i;
220
}