Commit e9e557c8 authored by Max Kellermann's avatar Max Kellermann Committed by Eric Wong

pass buffered_chunks to initOutputBuffer()

Try to make OutputBuffer self-contained, without depending on a global variable. git-svn-id: https://svn.musicpd.org/mpd/trunk@7310 09075e82-0dd4-0310-85a5-a0d7c8717e4f
parent b2819e12
...@@ -26,10 +26,13 @@ ...@@ -26,10 +26,13 @@
#include "conf.h" #include "conf.h"
#include "os_compat.h" #include "os_compat.h"
void initOutputBuffer(OutputBuffer * cb) void initOutputBuffer(OutputBuffer * cb, unsigned int size)
{ {
assert(size > 0);
memset(&cb->convState, 0, sizeof(ConvState)); memset(&cb->convState, 0, sizeof(ConvState));
cb->chunks = xmalloc(buffered_chunks * sizeof(*cb->chunks)); cb->chunks = xmalloc(size * sizeof(*cb->chunks));
cb->size = size;
cb->currentChunk = -1; cb->currentChunk = -1;
} }
...@@ -40,18 +43,18 @@ void clearOutputBuffer(OutputBuffer * cb) ...@@ -40,18 +43,18 @@ void clearOutputBuffer(OutputBuffer * cb)
} }
/** return the index of the chunk after i */ /** return the index of the chunk after i */
static inline unsigned successor(unsigned i) static inline unsigned successor(const OutputBuffer * cb, unsigned i)
{ {
assert(i <= buffered_chunks); assert(i <= cb->size);
++i; ++i;
return i == buffered_chunks ? 0 : i; return i == cb->size ? 0 : i;
} }
void flushOutputBuffer(OutputBuffer * cb) void flushOutputBuffer(OutputBuffer * cb)
{ {
if (cb->currentChunk == cb->end) { if (cb->currentChunk == cb->end) {
cb->end = successor(cb->end); cb->end = successor(cb, cb->end);
cb->currentChunk = -1; cb->currentChunk = -1;
} }
} }
...@@ -64,9 +67,9 @@ int outputBufferEmpty(const OutputBuffer * cb) ...@@ -64,9 +67,9 @@ int outputBufferEmpty(const OutputBuffer * cb)
void outputBufferShift(OutputBuffer * cb) void outputBufferShift(OutputBuffer * cb)
{ {
assert(cb->begin != cb->end); assert(cb->begin != cb->end);
assert(cb->begin < buffered_chunks); assert(cb->begin < cb->size);
cb->begin = successor(cb->begin); cb->begin = successor(cb, cb->begin);
} }
unsigned int outputBufferRelative(const OutputBuffer * cb, unsigned i) unsigned int outputBufferRelative(const OutputBuffer * cb, unsigned i)
...@@ -74,7 +77,7 @@ unsigned int outputBufferRelative(const OutputBuffer * cb, unsigned i) ...@@ -74,7 +77,7 @@ unsigned int outputBufferRelative(const OutputBuffer * cb, unsigned i)
if (i >= cb->begin) if (i >= cb->begin)
return i - cb->begin; return i - cb->begin;
else else
return i + buffered_chunks - cb->begin; return i + cb->size - cb->begin;
} }
unsigned availableOutputBuffer(const OutputBuffer * cb) unsigned availableOutputBuffer(const OutputBuffer * cb)
...@@ -88,20 +91,20 @@ int outputBufferAbsolute(const OutputBuffer * cb, unsigned relative) ...@@ -88,20 +91,20 @@ int outputBufferAbsolute(const OutputBuffer * cb, unsigned relative)
max = cb->end; max = cb->end;
if (max < cb->begin) if (max < cb->begin)
max += buffered_chunks; max += cb->size;
i = (unsigned)cb->begin + relative; i = (unsigned)cb->begin + relative;
if (i >= max) if (i >= max)
return -1; return -1;
if (i >= buffered_chunks) if (i >= cb->size)
i -= buffered_chunks; i -= cb->size;
return (int)i; return (int)i;
} }
OutputBufferChunk * outputBufferGetChunk(const OutputBuffer * cb, unsigned i) OutputBufferChunk * outputBufferGetChunk(const OutputBuffer * cb, unsigned i)
{ {
assert(i < buffered_chunks); assert(i < cb->size);
return &cb->chunks[i]; return &cb->chunks[i];
} }
...@@ -125,7 +128,7 @@ static int tailChunk(OutputBuffer * cb, InputStream * inStream, ...@@ -125,7 +128,7 @@ static int tailChunk(OutputBuffer * cb, InputStream * inStream,
if (cb->currentChunk == cb->end) if (cb->currentChunk == cb->end)
return cb->currentChunk; return cb->currentChunk;
next = successor(cb->end); next = successor(cb, cb->end);
while (cb->begin == next && !dc->stop) { while (cb->begin == next && !dc->stop) {
if (dc->seek) { if (dc->seek) {
if (seekable) { if (seekable) {
......
...@@ -46,6 +46,8 @@ typedef struct _OutputBufferChunk { ...@@ -46,6 +46,8 @@ typedef struct _OutputBufferChunk {
typedef struct _OutputBuffer { typedef struct _OutputBuffer {
OutputBufferChunk *chunks; OutputBufferChunk *chunks;
unsigned int size;
/** the index of the first decoded chunk */ /** the index of the first decoded chunk */
mpd_uint16 volatile begin; mpd_uint16 volatile begin;
...@@ -58,7 +60,7 @@ typedef struct _OutputBuffer { ...@@ -58,7 +60,7 @@ typedef struct _OutputBuffer {
ConvState convState; ConvState convState;
} OutputBuffer; } OutputBuffer;
void initOutputBuffer(OutputBuffer * cb); void initOutputBuffer(OutputBuffer * cb, unsigned int size);
void clearOutputBuffer(OutputBuffer * cb); void clearOutputBuffer(OutputBuffer * cb);
......
...@@ -75,7 +75,7 @@ void initPlayerData(void) ...@@ -75,7 +75,7 @@ void initPlayerData(void)
playerData_pd.audioDeviceStates = xmalloc(device_array_size); playerData_pd.audioDeviceStates = xmalloc(device_array_size);
initOutputBuffer(&(playerData_pd.buffer)); initOutputBuffer(&(playerData_pd.buffer), buffered_chunks);
notifyInit(&playerData_pd.playerControl.notify); notifyInit(&playerData_pd.playerControl.notify);
playerData_pd.playerControl.stop = 0; playerData_pd.playerControl.stop = 0;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment