stdout_callback: since the function 'pa_write' may not write all the data we asked it to, we will add an index field to each element of the queue.

This commit is contained in:
ZenBooster 2023-01-23 12:46:35 +03:00 committed by Илья
parent 6324d009b3
commit 4c7748b7a0

View file

@ -77,6 +77,7 @@ static size_t partialframe_len = 0;
struct buffer_t { struct buffer_t {
void *buffer; void *buffer;
size_t length; size_t length;
size_t index;
SIMPLEQ_ENTRY(buffer_t) next; SIMPLEQ_ENTRY(buffer_t) next;
}; };
@ -267,6 +268,7 @@ static void stream_read_callback(pa_stream *s, size_t length, void *userdata) {
struct buffer_t *buf = pa_xmalloc(sizeof(struct buffer_t)); struct buffer_t *buf = pa_xmalloc(sizeof(struct buffer_t));
buf->buffer = buffer; buf->buffer = buffer;
buf->length = length; buf->length = length;
buf->index = 0;
SIMPLEQ_INSERT_TAIL(&buffer_head, buf, next); SIMPLEQ_INSERT_TAIL(&buffer_head, buf, next);
} }
@ -610,8 +612,8 @@ static void stdout_callback(pa_mainloop_api*a, pa_io_event *e, int fd, pa_io_eve
} }
struct buffer_t *buf = SIMPLEQ_FIRST(&buffer_head); struct buffer_t *buf = SIMPLEQ_FIRST(&buffer_head);
void *buffer = buf->buffer; void *buffer = buf->buffer + buf->index;
size_t length = buf->length; size_t length = buf->length - buf->index;
if ((r = pa_write(fd, (uint8_t*) buffer, length, userdata)) <= 0) { if ((r = pa_write(fd, (uint8_t*) buffer, length, userdata)) <= 0) {
pa_log(_("write() failed: %s"), strerror(errno)); pa_log(_("write() failed: %s"), strerror(errno));
@ -622,9 +624,14 @@ static void stdout_callback(pa_mainloop_api*a, pa_io_event *e, int fd, pa_io_eve
return; return;
} }
pa_xfree(buffer); buf->index += r;
SIMPLEQ_REMOVE_HEAD(&buffer_head, next);
pa_xfree(buf); if (buf->index == buf->length)
{
pa_xfree(buffer);
SIMPLEQ_REMOVE_HEAD(&buffer_head, next);
pa_xfree(buf);
}
} }
/* UNIX signal to quit received */ /* UNIX signal to quit received */