From 940ecf5345f339b9d3ec3e8029e345540358fa4c Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Tue, 26 Aug 2008 08:27:14 +0200 Subject: added decoder_read() On our way to stabilize the decoder API, we will one day remove the input stream functions. The most basic function, read() will be provided by decoder_api.h with this patch. It already contains a loop (still with manual polling), error/eof handling and decoder command checks. This kind of code used to be duplicated in all decoder plugins. --- src/decoder_api.c | 24 ++++++++++++++++++++++++ src/decoder_api.h | 9 +++++++++ src/inputPlugins/_ogg_common.c | 10 +--------- src/inputPlugins/aac_plugin.c | 8 +++----- src/inputPlugins/flac_plugin.c | 9 +-------- src/inputPlugins/mp3_plugin.c | 20 ++++++-------------- src/inputPlugins/mpc_plugin.c | 13 +------------ src/inputPlugins/oggflac_plugin.c | 9 +-------- src/inputPlugins/oggvorbis_plugin.c | 12 +++--------- src/inputPlugins/wavpack_plugin.c | 2 +- 10 files changed, 50 insertions(+), 66 deletions(-) (limited to 'src') diff --git a/src/decoder_api.c b/src/decoder_api.c index ffc3aa781..9a8b803d9 100644 --- a/src/decoder_api.c +++ b/src/decoder_api.c @@ -89,6 +89,30 @@ void decoder_seek_error(struct decoder * decoder) decoder_command_finished(decoder); } +size_t decoder_read(struct decoder *decoder, + InputStream *inStream, + void *buffer, size_t length) +{ + size_t nbytes; + + assert(inStream != NULL); + assert(buffer != NULL); + + while (1) { + /* XXX don't allow decoder==NULL */ + if (decoder != NULL && dc.command != DECODE_COMMAND_NONE) + return 0; + + nbytes = readFromInputStream(inStream, buffer, 1, length); + if (nbytes > 0 || inputStreamAtEOF(inStream)) + return nbytes; + + /* sleep for a fraction of a second! */ + /* XXX don't sleep, wait for an event instead */ + my_usleep(10000); + } +} + /** * All chunks are full of decoded data; wait for the player to free * one. diff --git a/src/decoder_api.h b/src/decoder_api.h index 5afa06e77..7a77bc93a 100644 --- a/src/decoder_api.h +++ b/src/decoder_api.h @@ -121,6 +121,15 @@ double decoder_seek_where(struct decoder * decoder); void decoder_seek_error(struct decoder * decoder); +/** + * Blocking read from the input stream. Returns the number of bytes + * read, or 0 if one of the following occurs: end of file; error; + * command (like SEEK or STOP). + */ +size_t decoder_read(struct decoder *decoder, + InputStream *inStream, + void *buffer, size_t length); + /** * This function is called by the decoder plugin when it has * successfully decoded block of input data. diff --git a/src/inputPlugins/_ogg_common.c b/src/inputPlugins/_ogg_common.c index 73facdded..535b9a9dc 100644 --- a/src/inputPlugins/_ogg_common.c +++ b/src/inputPlugins/_ogg_common.c @@ -37,15 +37,7 @@ ogg_stream_type ogg_stream_type_detect(InputStream * inStream) seekInputStream(inStream, 0, SEEK_SET); - while (1) { - r = readFromInputStream(inStream, buf, 1, sizeof(buf)); - if (inStream->error) - break; - if (!r && !inputStreamAtEOF(inStream)) - my_usleep(10000); - else - break; - } + r = decoder_read(NULL, inStream, buf, sizeof(buf)); if (r > 0) seekInputStream(inStream, 0, SEEK_SET); diff --git a/src/inputPlugins/aac_plugin.c b/src/inputPlugins/aac_plugin.c index 0d4e5bfa4..f0796bb62 100644 --- a/src/inputPlugins/aac_plugin.c +++ b/src/inputPlugins/aac_plugin.c @@ -65,11 +65,9 @@ static void fillAacBuffer(AacBuffer * b) size_t rest = FAAD_MIN_STREAMSIZE * AAC_MAX_CHANNELS - b->bytesIntoBuffer; - bread = readFromInputStream(b->inStream, - (void *)(b->buffer + - b-> - bytesIntoBuffer), - 1, rest); + bread = decoder_read(b->decoder, b->inStream, + (void *)(b->buffer + b->bytesIntoBuffer), + rest); if (bread == 0 && inputStreamAtEOF(b->inStream)) b->atEof = 1; b->bytesIntoBuffer += bread; diff --git a/src/inputPlugins/flac_plugin.c b/src/inputPlugins/flac_plugin.c index 9b1c8d85d..aeea17023 100644 --- a/src/inputPlugins/flac_plugin.c +++ b/src/inputPlugins/flac_plugin.c @@ -33,14 +33,7 @@ static flac_read_status flacRead(mpd_unused const flac_decoder * flacDec, FlacData *data = (FlacData *) fdata; size_t r; - while (1) { - r = readFromInputStream(data->inStream, (void *)buf, 1, *bytes); - if (r == 0 && !inputStreamAtEOF(data->inStream) && - decoder_get_command(data->decoder) != DECODE_COMMAND_STOP) - my_usleep(10000); - else - break; - } + r = decoder_read(data->decoder, data->inStream, (void *)buf, *bytes); *bytes = r; if (r == 0 && decoder_get_command(data->decoder) != DECODE_COMMAND_STOP) { diff --git a/src/inputPlugins/mp3_plugin.c b/src/inputPlugins/mp3_plugin.c index c2bac068d..aaf5ae55a 100644 --- a/src/inputPlugins/mp3_plugin.c +++ b/src/inputPlugins/mp3_plugin.c @@ -233,15 +233,10 @@ static int fillMp3InputBuffer(mp3DecodeData * data) if (readSize == 0) return -1; - readed = readFromInputStream(data->inStream, readStart, (size_t) 1, - readSize); - if (readed <= 0 && inputStreamAtEOF(data->inStream)) + readed = decoder_read(data->decoder, data->inStream, + readStart, readSize); + if (readed == 0) return -1; - /* sleep for a fraction of a second! */ - else if (readed <= 0) { - readed = 0; - my_usleep(10000); - } mad_stream_buffer(&data->stream, data->readBuffer, readed + remaining); (data->stream).error = 0; @@ -324,13 +319,10 @@ static void mp3_parseId3Tag(mp3DecodeData * data, size_t tagsize, while (count < tagsize) { size_t len; - len = readFromInputStream(data->inStream, - allocated + count, (size_t) 1, - tagsize - count); - if (len <= 0 && inputStreamAtEOF(data->inStream)) { + len = decoder_read(data->decoder, data->inStream, + allocated + count, tagsize - count); + if (len == 0) break; - } else if (len <= 0) - my_usleep(10000); else count += len; } diff --git a/src/inputPlugins/mpc_plugin.c b/src/inputPlugins/mpc_plugin.c index 411373283..611c4f4d6 100644 --- a/src/inputPlugins/mpc_plugin.c +++ b/src/inputPlugins/mpc_plugin.c @@ -32,20 +32,9 @@ typedef struct _MpcCallbackData { static mpc_int32_t mpc_read_cb(void *vdata, void *ptr, mpc_int32_t size) { - mpc_int32_t ret = 0; MpcCallbackData *data = (MpcCallbackData *) vdata; - while (1) { - ret = readFromInputStream(data->inStream, ptr, 1, size); - if (ret == 0 && !inputStreamAtEOF(data->inStream) && - (data->decoder && - decoder_get_command(data->decoder) != DECODE_COMMAND_STOP)) - my_usleep(10000); - else - break; - } - - return ret; + return decoder_read(data->decoder, data->inStream, ptr, size); } static mpc_bool_t mpc_seek_cb(void *vdata, mpc_int32_t offset) diff --git a/src/inputPlugins/oggflac_plugin.c b/src/inputPlugins/oggflac_plugin.c index 637e4ac5f..f56a6b5ec 100644 --- a/src/inputPlugins/oggflac_plugin.c +++ b/src/inputPlugins/oggflac_plugin.c @@ -47,14 +47,7 @@ static OggFLAC__SeekableStreamDecoderReadStatus of_read_cb(const FlacData *data = (FlacData *) fdata; size_t r; - while (1) { - r = readFromInputStream(data->inStream, (void *)buf, 1, *bytes); - if (r == 0 && !inputStreamAtEOF(data->inStream) && - decoder_get_command(data->decoder) != DECODE_COMMAND_STOP) - my_usleep(10000); - else - break; - } + r = decoder_read(data->decoder, data->inStream, (void *)buf, *bytes); *bytes = r; if (r == 0 && !inputStreamAtEOF(data->inStream) && diff --git a/src/inputPlugins/oggvorbis_plugin.c b/src/inputPlugins/oggvorbis_plugin.c index 4801cb47b..6a6ecbd98 100644 --- a/src/inputPlugins/oggvorbis_plugin.c +++ b/src/inputPlugins/oggvorbis_plugin.c @@ -58,18 +58,12 @@ static size_t ogg_read_cb(void *ptr, size_t size, size_t nmemb, void *vdata) size_t ret; OggCallbackData *data = (OggCallbackData *) vdata; - while (1) { - ret = readFromInputStream(data->inStream, ptr, size, nmemb); - if (ret == 0 && !inputStreamAtEOF(data->inStream) && - decoder_get_command(data->decoder) != DECODE_COMMAND_STOP) { - my_usleep(10000); - } else - break; - } + ret = decoder_read(data->decoder, data->inStream, ptr, size * nmemb); + errno = 0; /*if(ret<0) errno = ((InputStream *)inStream)->error; */ - return ret; + return ret / size; } static int ogg_seek_cb(void *vdata, ogg_int64_t offset, int whence) diff --git a/src/inputPlugins/wavpack_plugin.c b/src/inputPlugins/wavpack_plugin.c index 00aa09993..b641ab7dc 100644 --- a/src/inputPlugins/wavpack_plugin.c +++ b/src/inputPlugins/wavpack_plugin.c @@ -358,7 +358,7 @@ static int32_t read_bytes(void *id, void *data, int32_t bcount) --bcount; ++i; } - return i + readFromInputStream(isp->is, buf, 1, bcount); + return i + decoder_read(isp->decoder, isp->is, buf, bcount); } static uint32_t get_pos(void *id) -- cgit v1.2.3