From 034361be62697692384103298d5b39741d5ad477 Mon Sep 17 00:00:00 2001 From: Ajender Reddy Date: Wed, 11 Jun 2025 13:15:39 +0530 Subject: [PATCH 1/2] tinycompress: fix compress_read API In blocking mode: read is blocked until it reads some bytes In Non-blocking mode: read as much as available bytes, if available. --- include/tinycompress/tinycompress.h | 2 +- src/lib/compress_hw.c | 72 +++++++++++++---------------- 2 files changed, 34 insertions(+), 40 deletions(-) diff --git a/include/tinycompress/tinycompress.h b/include/tinycompress/tinycompress.h index a8b4e58..8865ef9 100644 --- a/include/tinycompress/tinycompress.h +++ b/include/tinycompress/tinycompress.h @@ -173,7 +173,7 @@ int compress_write(struct compress *compress, const void *buf, unsigned int size * compress_read: read data from the compress stream * return bytes read on success, negative on error * By default this is a blocking call and will block until - * size bytes have been written or there was a read error. + * some bytes have been read or there was a read error. * If non-blocking mode was enabled using compress_nonblock() * the behaviour will change to read only as many bytes as * are currently available (if no bytes are available it diff --git a/src/lib/compress_hw.c b/src/lib/compress_hw.c index 5aabae0..492b154 100644 --- a/src/lib/compress_hw.c +++ b/src/lib/compress_hw.c @@ -333,7 +333,7 @@ static int compress_hw_read(void *data, void *buf, size_t size) struct snd_compr_avail avail; struct pollfd fds; int to_read = 0; - int num_read, total = 0, ret; + int num_read = 0, ret; char* cbuf = buf; const unsigned int frag_size = compress->config->fragment_size; @@ -344,50 +344,44 @@ static int compress_hw_read(void *data, void *buf, size_t size) fds.fd = compress->fd; fds.events = POLLIN; - while (size) { - if (ioctl(compress->fd, SNDRV_COMPRESS_AVAIL, &avail)) - return oops(compress, errno, "cannot get avail"); + if (ioctl(compress->fd, SNDRV_COMPRESS_AVAIL, &avail)) + return oops(compress, errno, "cannot get avail"); - if ( (avail.avail < frag_size) && (avail.avail < size) ) { - /* Less than one fragment available and not at the - * end of the read, so poll - */ - if (compress->nonblocking) - return total; + if ( (avail.avail < frag_size) && (avail.avail < size) ) { + /* Less than one fragment available and not at the + * end of the read, so poll + */ + if (compress->nonblocking) + return num_read; - ret = poll(&fds, 1, compress->max_poll_wait_ms); - if (fds.revents & POLLERR) { - return oops(compress, EIO, "poll returned error!"); - } - /* A pause will cause -EBADFD or zero. - * This is not an error, just stop reading */ - if ((ret == 0) || (ret < 0 && errno == EBADFD)) - break; - if (ret < 0) - return oops(compress, errno, "poll error"); - if (fds.revents & POLLIN) { - continue; - } + ret = poll(&fds, 1, compress->max_poll_wait_ms); + if (fds.revents & POLLERR) { + return oops(compress, EIO, "poll returned error!"); } - /* read avail bytes */ - if (size > avail.avail) - to_read = avail.avail; - else - to_read = size; - num_read = read(compress->fd, cbuf, to_read); - if (num_read < 0) { - /* If play was paused the read returns -EBADFD */ - if (errno == EBADFD) - break; - return oops(compress, errno, "read failed!"); + /* A pause will cause -EBADFD or zero. + * This is not an error, just stop reading */ + if ((ret == 0) || (ret < 0 && errno == EBADFD)) + break; + if (ret < 0) + return oops(compress, errno, "poll error"); + if (fds.revents & POLLIN) { + continue; } - - size -= num_read; - cbuf += num_read; - total += num_read; + } + /* read avail bytes */ + if (size > avail.avail) + to_read = avail.avail; + else + to_read = size; + num_read = read(compress->fd, cbuf, to_read); + if (num_read < 0) { + /* If play was paused the read returns -EBADFD */ + if (errno == EBADFD) + break; + return oops(compress, errno, "read failed!"); } - return total; + return num_read; } static int compress_hw_start(void *data) From 415c67918f22bc4df932ab5038aa68ec9a201f57 Mon Sep 17 00:00:00 2001 From: Ajender Reddy Date: Wed, 11 Jun 2025 14:22:08 +0530 Subject: [PATCH 2/2] fix comments indentation --- src/lib/compress_hw.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/lib/compress_hw.c b/src/lib/compress_hw.c index 492b154..8e366aa 100644 --- a/src/lib/compress_hw.c +++ b/src/lib/compress_hw.c @@ -349,8 +349,8 @@ static int compress_hw_read(void *data, void *buf, size_t size) if ( (avail.avail < frag_size) && (avail.avail < size) ) { /* Less than one fragment available and not at the - * end of the read, so poll - */ + * end of the read, so poll + */ if (compress->nonblocking) return num_read; @@ -359,7 +359,7 @@ static int compress_hw_read(void *data, void *buf, size_t size) return oops(compress, EIO, "poll returned error!"); } /* A pause will cause -EBADFD or zero. - * This is not an error, just stop reading */ + * This is not an error, just stop reading */ if ((ret == 0) || (ret < 0 && errno == EBADFD)) break; if (ret < 0)