return ret;
}
-static char *vss_read_chunk(long unsigned current_chunk, ssize_t *len)
+/**
+ * read a chunk of data from the current audio file
+ *
+ * \param current_chunk the chunk number to read
+ *
+ * \return The length of the chunk on success, zero on end of file, negative on
+ * errors. Note: If the current chunk is of length zero, but the end of the
+ * file is not yet reached, this function returns -E_EMPTY_CHUNK.
+ * */
+ssize_t vss_read_chunk(void)
{
- int ret;
+ ssize_t len;
size_t pos;
-
- *len = 0;
- if (current_chunk >= mmd->chunks_total)
- return NULL;
- *len = chunk_table[current_chunk + 1] - chunk_table[current_chunk];
- if (!*len) /* nothing to send for this run */
- return inbuf;
- pos = chunk_table[current_chunk];
- if (inbuf_size < *len) {
- PARA_INFO_LOG("increasing inbuf for chunk #%lu/%lu to %zd bytes\n",
- current_chunk, mmd->chunks_total, *len);
- inbuf = para_realloc(inbuf, *len);
- inbuf_size = *len;
+ int ret;
+ long unsigned cc = mmd->current_chunk;
+
+ if (cc >= mmd->chunks_total) /* eof */
+ return 0;
+ len = chunk_table[cc + 1] - chunk_table[cc];
+ if (!len) /* nothing to send for this run */
+ return -E_EMPTY_CHUNK;
+ pos = chunk_table[cc];
+ if (inbuf_size < len) {
+ PARA_INFO_LOG("increasing inbuf for chunk #%lu/%lu to %zu bytes\n",
+ cc, mmd->chunks_total, len);
+ inbuf = para_realloc(inbuf, len);
+ inbuf_size = len;
}
-// PARA_DEBUG_LOG("reading chunk #%lu@%zd (%zd bytes)\n", current_chunk,
-// pos, *len);
- ret = fseek(audio_file, pos, SEEK_SET);
+ ret = para_fseek(audio_file, pos, SEEK_SET);
if (ret < 0)
- return NULL;
- ret = para_fread(inbuf, *len, 1, audio_file);
- if (ret != *len)
- return NULL;
- return inbuf;
+ return ret;
+ return para_fread(inbuf, len, 1, audio_file);
}
/**
{
int i;
struct audio_format_handler *af;
- char *buf;
ssize_t ret;
struct timeval now, due;
if (chk_barrier("data send", &now, &data_send_barrier,
&due, 1) < 0)
return;
- buf = vss_read_chunk(mmd->current_chunk, &ret);
+ ret= vss_read_chunk();
mmd->new_vss_status_flags &= ~VSS_REPOS;
- if (!buf) {
- if (ret < 0)
+ if (!ret || (ret < 0 && ret != -E_EMPTY_CHUNK)) {
+ if (ret < 0) {
mmd->new_vss_status_flags = VSS_NEXT;
- else
+ PARA_ERROR_LOG("%s\n", PARA_STRERROR(-ret));
+ } else
mmd->new_vss_status_flags |= VSS_NEXT;
vss_eof(af);
return;
}
+ /*
+ * We call the send function also in case of empty chunks as they
+ * might have still some data queued which can be sent in this case.
+ */
+ if (ret < 0)
+ ret = 0;
if (!mmd->chunks_sent) {
struct timeval tmp;
gettimeofday(&mmd->stream_start, NULL);
mmd->events++;
}
for (i = 0; senders[i].name; i++)
- senders[i].send(mmd->current_chunk, mmd->chunks_sent, buf, ret);
+ senders[i].send(mmd->current_chunk, mmd->chunks_sent, inbuf, ret);
mmd->new_vss_status_flags |= VSS_PLAYING;
mmd->chunks_sent++;
mmd->current_chunk++;