From e06acc1ba23b7a2dd6a822d04e51a031e629aa35 Mon Sep 17 00:00:00 2001 From: Romain Vimont Date: Sat, 11 Mar 2023 10:16:43 +0100 Subject: [PATCH] Simplify bytebuf naming Rename read_available to can_read and write_available to can_write. This is more readable. --- app/src/audio_player.c | 24 ++++++++++----------- app/src/audio_player.h | 2 +- app/src/util/audiobuf.h | 8 +++---- app/src/util/bytebuf.c | 8 +++---- app/src/util/bytebuf.h | 6 +++--- app/tests/test_bytebuf.c | 46 ++++++++++++++++++++-------------------- 6 files changed, 47 insertions(+), 47 deletions(-) diff --git a/app/src/audio_player.c b/app/src/audio_player.c index 320af082..652711c6 100644 --- a/app/src/audio_player.c +++ b/app/src/audio_player.c @@ -33,7 +33,7 @@ sc_audio_player_sdl_callback(void *userdata, uint8_t *stream, int len_int) { LOGD("[Audio] SDL callback requests %" PRIu32 " samples", count); #endif - uint32_t buffered_samples = sc_audiobuf_read_available(&ap->buf); + uint32_t buffered_samples = sc_audiobuf_can_read(&ap->buf); if (!ap->played) { // Part of the buffering is handled by inserting initial silence. The // remaining (margin) last samples will be handled by compensation. @@ -126,20 +126,20 @@ sc_audio_player_frame_sink_push(struct sc_frame_sink *sink, // Since this function is the only writer, the current available space is // at least the previous available space. In practice, it should almost // always be possible to write without lock. - bool lockless_write = samples_written <= ap->previous_write_avail; + bool lockless_write = samples_written <= ap->previous_can_write; if (lockless_write) { sc_audiobuf_prepare_write(&ap->buf, swr_buf, samples_written); } SDL_LockAudioDevice(ap->device); - uint32_t buffered_samples = sc_audiobuf_read_available(&ap->buf); + uint32_t buffered_samples = sc_audiobuf_can_read(&ap->buf); if (lockless_write) { sc_audiobuf_commit_write(&ap->buf, samples_written); } else { - uint32_t write_avail = sc_audiobuf_write_available(&ap->buf); - if (samples_written > write_avail) { + uint32_t can_write = sc_audiobuf_can_write(&ap->buf); + if (samples_written > can_write) { // Entering this branch is very unlikely, the audio buffer is // allocated with a size sufficient to store 1 second more than the // target buffering. If this happens, though, we have to skip old @@ -155,9 +155,9 @@ sc_audio_player_frame_sink_push(struct sc_frame_sink *sink, samples_written = cap; } - assert(samples_written >= write_avail); - if (samples_written > write_avail) { - uint32_t skip_samples = samples_written - write_avail; + assert(samples_written >= can_write); + if (samples_written > can_write) { + uint32_t skip_samples = samples_written - can_write; assert(buffered_samples >= skip_samples); sc_audiobuf_skip(&ap->buf, skip_samples); buffered_samples -= skip_samples; @@ -169,14 +169,14 @@ sc_audio_player_frame_sink_push(struct sc_frame_sink *sink, // It should remain exactly the expected size to write the new // samples. - assert(sc_audiobuf_write_available(&ap->buf) == samples_written); + assert(sc_audiobuf_can_write(&ap->buf) == samples_written); } sc_audiobuf_write(&ap->buf, swr_buf, samples_written); } buffered_samples += samples_written; - assert(buffered_samples == sc_audiobuf_read_available(&ap->buf)); + assert(buffered_samples == sc_audiobuf_can_read(&ap->buf)); // Read with lock held, to be used after unlocking bool played = ap->played; @@ -221,7 +221,7 @@ sc_audio_player_frame_sink_push(struct sc_frame_sink *sink, } } - ap->previous_write_avail = sc_audiobuf_write_available(&ap->buf); + ap->previous_can_write = sc_audiobuf_can_write(&ap->buf); ap->received = true; SDL_UnlockAudioDevice(ap->device); @@ -349,7 +349,7 @@ sc_audio_player_frame_sink_open(struct sc_frame_sink *sink, } ap->swr_buf_alloc_size = initial_swr_buf_size; - ap->previous_write_avail = sc_audiobuf_write_available(&ap->buf); + ap->previous_can_write = sc_audiobuf_can_write(&ap->buf); // Samples are produced and consumed by blocks, so the buffering must be // smoothed to get a relatively stable value. diff --git a/app/src/audio_player.h b/app/src/audio_player.h index e82735c2..f4670939 100644 --- a/app/src/audio_player.h +++ b/app/src/audio_player.h @@ -33,7 +33,7 @@ struct sc_audio_player { // The previous empty space in the buffer (only used by the receiver // thread) - uint32_t previous_write_avail; + uint32_t previous_can_write; // Resampler (only used from the receiver thread) struct SwrContext *swr_ctx; diff --git a/app/src/util/audiobuf.h b/app/src/util/audiobuf.h index 16bf20d3..8616d539 100644 --- a/app/src/util/audiobuf.h +++ b/app/src/util/audiobuf.h @@ -69,14 +69,14 @@ sc_audiobuf_commit_write(struct sc_audiobuf *buf, uint32_t samples) { } static inline uint32_t -sc_audiobuf_read_available(struct sc_audiobuf *buf) { - size_t bytes = sc_bytebuf_read_available(&buf->buf); +sc_audiobuf_can_read(struct sc_audiobuf *buf) { + size_t bytes = sc_bytebuf_can_read(&buf->buf); return sc_audiobuf_to_samples(buf, bytes); } static inline uint32_t -sc_audiobuf_write_available(struct sc_audiobuf *buf) { - size_t bytes = sc_bytebuf_write_available(&buf->buf); +sc_audiobuf_can_write(struct sc_audiobuf *buf) { + size_t bytes = sc_bytebuf_can_write(&buf->buf); return sc_audiobuf_to_samples(buf, bytes); } diff --git a/app/src/util/bytebuf.c b/app/src/util/bytebuf.c index eac69e9c..93544d72 100644 --- a/app/src/util/bytebuf.c +++ b/app/src/util/bytebuf.c @@ -30,7 +30,7 @@ sc_bytebuf_destroy(struct sc_bytebuf *buf) { void sc_bytebuf_read(struct sc_bytebuf *buf, uint8_t *to, size_t len) { assert(len); - assert(len <= sc_bytebuf_read_available(buf)); + assert(len <= sc_bytebuf_can_read(buf)); assert(buf->tail != buf->head); // the buffer could not be empty size_t right_limit = buf->tail < buf->head ? buf->head : buf->alloc_size; @@ -50,7 +50,7 @@ sc_bytebuf_read(struct sc_bytebuf *buf, uint8_t *to, size_t len) { void sc_bytebuf_skip(struct sc_bytebuf *buf, size_t len) { assert(len); - assert(len <= sc_bytebuf_read_available(buf)); + assert(len <= sc_bytebuf_can_read(buf)); assert(buf->tail != buf->head); // the buffer could not be empty buf->tail = (buf->tail + len) % buf->alloc_size; @@ -78,7 +78,7 @@ sc_bytebuf_write_step1(struct sc_bytebuf *buf, size_t len) { void sc_bytebuf_write(struct sc_bytebuf *buf, const uint8_t *from, size_t len) { assert(len); - assert(len <= sc_bytebuf_write_available(buf)); + assert(len <= sc_bytebuf_can_write(buf)); sc_bytebuf_write_step0(buf, from, len); sc_bytebuf_write_step1(buf, len); @@ -99,6 +99,6 @@ sc_bytebuf_prepare_write(struct sc_bytebuf *buf, const uint8_t *from, void sc_bytebuf_commit_write(struct sc_bytebuf *buf, size_t len) { - assert(len <= sc_bytebuf_write_available(buf)); + assert(len <= sc_bytebuf_can_write(buf)); sc_bytebuf_write_step1(buf, len); } diff --git a/app/src/util/bytebuf.h b/app/src/util/bytebuf.h index e8279ef8..1448f752 100644 --- a/app/src/util/bytebuf.h +++ b/app/src/util/bytebuf.h @@ -86,7 +86,7 @@ sc_bytebuf_commit_write(struct sc_bytebuf *buf, size_t len); * It is an error to read more bytes than available. */ static inline size_t -sc_bytebuf_read_available(struct sc_bytebuf *buf) { +sc_bytebuf_can_read(struct sc_bytebuf *buf) { return (buf->alloc_size + buf->head - buf->tail) % buf->alloc_size; } @@ -96,12 +96,12 @@ sc_bytebuf_read_available(struct sc_bytebuf *buf) { * It is an error to write more bytes than available. */ static inline size_t -sc_bytebuf_write_available(struct sc_bytebuf *buf) { +sc_bytebuf_can_write(struct sc_bytebuf *buf) { return (buf->alloc_size + buf->tail - buf->head - 1) % buf->alloc_size; } /** - * Return the actual capacity of the buffer (read available + write available) + * Return the actual capacity of the buffer (can_read() + can_write()) */ static inline size_t sc_bytebuf_capacity(struct sc_bytebuf *buf) { diff --git a/app/tests/test_bytebuf.c b/app/tests/test_bytebuf.c index 75af3073..c85e79ec 100644 --- a/app/tests/test_bytebuf.c +++ b/app/tests/test_bytebuf.c @@ -13,23 +13,23 @@ void test_bytebuf_simple(void) { assert(ok); sc_bytebuf_write(&buf, (uint8_t *) "hello", sizeof("hello") - 1); - assert(sc_bytebuf_read_available(&buf) == 5); + assert(sc_bytebuf_can_read(&buf) == 5); sc_bytebuf_read(&buf, data, 4); assert(!strncmp((char *) data, "hell", 4)); sc_bytebuf_write(&buf, (uint8_t *) " world", sizeof(" world") - 1); - assert(sc_bytebuf_read_available(&buf) == 7); + assert(sc_bytebuf_can_read(&buf) == 7); sc_bytebuf_write(&buf, (uint8_t *) "!", 1); - assert(sc_bytebuf_read_available(&buf) == 8); + assert(sc_bytebuf_can_read(&buf) == 8); sc_bytebuf_read(&buf, &data[4], 8); - assert(sc_bytebuf_read_available(&buf) == 0); + assert(sc_bytebuf_can_read(&buf) == 0); data[12] = '\0'; assert(!strcmp((char *) data, "hello world!")); - assert(sc_bytebuf_read_available(&buf) == 0); + assert(sc_bytebuf_can_read(&buf) == 0); sc_bytebuf_destroy(&buf); } @@ -42,31 +42,31 @@ void test_bytebuf_boundaries(void) { assert(ok); sc_bytebuf_write(&buf, (uint8_t *) "hello ", sizeof("hello ") - 1); - assert(sc_bytebuf_read_available(&buf) == 6); + assert(sc_bytebuf_can_read(&buf) == 6); sc_bytebuf_write(&buf, (uint8_t *) "hello ", sizeof("hello ") - 1); - assert(sc_bytebuf_read_available(&buf) == 12); + assert(sc_bytebuf_can_read(&buf) == 12); sc_bytebuf_write(&buf, (uint8_t *) "hello ", sizeof("hello ") - 1); - assert(sc_bytebuf_read_available(&buf) == 18); + assert(sc_bytebuf_can_read(&buf) == 18); sc_bytebuf_read(&buf, data, 9); assert(!strncmp((char *) data, "hello hel", 9)); - assert(sc_bytebuf_read_available(&buf) == 9); + assert(sc_bytebuf_can_read(&buf) == 9); sc_bytebuf_write(&buf, (uint8_t *) "world", sizeof("world") - 1); - assert(sc_bytebuf_read_available(&buf) == 14); + assert(sc_bytebuf_can_read(&buf) == 14); sc_bytebuf_write(&buf, (uint8_t *) "!", 1); - assert(sc_bytebuf_read_available(&buf) == 15); + assert(sc_bytebuf_can_read(&buf) == 15); sc_bytebuf_skip(&buf, 3); - assert(sc_bytebuf_read_available(&buf) == 12); + assert(sc_bytebuf_can_read(&buf) == 12); sc_bytebuf_read(&buf, data, 12); data[12] = '\0'; assert(!strcmp((char *) data, "hello world!")); - assert(sc_bytebuf_read_available(&buf) == 0); + assert(sc_bytebuf_can_read(&buf) == 0); sc_bytebuf_destroy(&buf); } @@ -79,37 +79,37 @@ void test_bytebuf_two_steps_write(void) { assert(ok); sc_bytebuf_write(&buf, (uint8_t *) "hello ", sizeof("hello ") - 1); - assert(sc_bytebuf_read_available(&buf) == 6); + assert(sc_bytebuf_can_read(&buf) == 6); sc_bytebuf_write(&buf, (uint8_t *) "hello ", sizeof("hello ") - 1); - assert(sc_bytebuf_read_available(&buf) == 12); + assert(sc_bytebuf_can_read(&buf) == 12); sc_bytebuf_prepare_write(&buf, (uint8_t *) "hello ", sizeof("hello ") - 1); - assert(sc_bytebuf_read_available(&buf) == 12); // write not committed yet + assert(sc_bytebuf_can_read(&buf) == 12); // write not committed yet sc_bytebuf_read(&buf, data, 9); assert(!strncmp((char *) data, "hello hel", 3)); - assert(sc_bytebuf_read_available(&buf) == 3); + assert(sc_bytebuf_can_read(&buf) == 3); sc_bytebuf_commit_write(&buf, sizeof("hello ") - 1); - assert(sc_bytebuf_read_available(&buf) == 9); + assert(sc_bytebuf_can_read(&buf) == 9); sc_bytebuf_prepare_write(&buf, (uint8_t *) "world", sizeof("world") - 1); - assert(sc_bytebuf_read_available(&buf) == 9); // write not committed yet + assert(sc_bytebuf_can_read(&buf) == 9); // write not committed yet sc_bytebuf_commit_write(&buf, sizeof("world") - 1); - assert(sc_bytebuf_read_available(&buf) == 14); + assert(sc_bytebuf_can_read(&buf) == 14); sc_bytebuf_write(&buf, (uint8_t *) "!", 1); - assert(sc_bytebuf_read_available(&buf) == 15); + assert(sc_bytebuf_can_read(&buf) == 15); sc_bytebuf_skip(&buf, 3); - assert(sc_bytebuf_read_available(&buf) == 12); + assert(sc_bytebuf_can_read(&buf) == 12); sc_bytebuf_read(&buf, data, 12); data[12] = '\0'; assert(!strcmp((char *) data, "hello world!")); - assert(sc_bytebuf_read_available(&buf) == 0); + assert(sc_bytebuf_can_read(&buf) == 0); sc_bytebuf_destroy(&buf); }