AirPlay: no realloc, safe TCB cleanup, tools convergence

This commit is contained in:
Philippe G
2021-12-17 10:54:25 -08:00
parent a266c07114
commit 3b6299dc1a
14 changed files with 144 additions and 59 deletions

View File

@@ -73,13 +73,11 @@ void _buf_flush(struct buffer *buf) {
// adjust buffer to multiple of mod bytes so reading in multiple always wraps on frame boundary
void buf_adjust(struct buffer *buf, size_t mod) {
size_t size;
mutex_lock(buf->mutex);
size = ((unsigned)(buf->base_size / mod)) * mod;
buf->readp = buf->buf;
buf->writep = buf->buf;
buf->wrap = buf->buf + size;
buf->size = size;
buf->base_size = ((size_t)(buf->size / mod)) * mod;
buf->readp = buf->writep = buf->buf;
buf->wrap = buf->buf + buf->base_size;
buf->size = buf->base_size;
mutex_unlock(buf->mutex);
}
@@ -90,16 +88,23 @@ void _buf_resize(struct buffer *buf, size_t size) {
buf->buf = malloc(size);
if (!buf->buf) {
size = buf->size;
buf->buf= malloc(size);
if (!buf->buf) {
size = 0;
}
buf->buf = malloc(size);
if (!buf->buf) size = 0;
}
buf->readp = buf->buf;
buf->writep = buf->buf;
buf->writep = buf->readp = buf->buf;
buf->wrap = buf->buf + size;
buf->size = size;
buf->base_size = size;
buf->true_size = buf->base_size = buf->size = size;
}
size_t _buf_limit(struct buffer *buf, size_t limit) {
if (limit) {
buf->size = limit;
buf->readp = buf->writep = buf->buf;
} else {
buf->size = buf->base_size;
}
buf->wrap = buf->buf + buf->size;
return buf->base_size - buf->size;
}
void _buf_unwrap(struct buffer *buf, size_t cont) {
@@ -130,7 +135,9 @@ void _buf_unwrap(struct buffer *buf, size_t cont) {
if (len > by) {
memmove(buf->buf, buf->buf + by, len - by);
buf->writep -= by;
} else buf->writep += buf->size - by;
} else {
buf->writep += buf->size - by;
}
return;
}
@@ -157,8 +164,7 @@ void buf_init(struct buffer *buf, size_t size) {
buf->readp = buf->buf;
buf->writep = buf->buf;
buf->wrap = buf->buf + size;
buf->size = size;
buf->base_size = size;
buf->true_size = buf->base_size = buf->size = size;
mutex_create_p(buf->mutex);
}
@@ -166,8 +172,7 @@ void buf_destroy(struct buffer *buf) {
if (buf->buf) {
free(buf->buf);
buf->buf = NULL;
buf->size = 0;
buf->base_size = 0;
buf->size = buf->base_size = buf->true_size = 0;
mutex_destroy(buf->mutex);
}
}

View File

@@ -122,6 +122,7 @@ static bool bt_sink_cmd_handler(bt_sink_cmd_t cmd, va_list args)
switch(cmd) {
case BT_SINK_AUDIO_STARTED:
_buf_flush(outputbuf);
_buf_limit(outputbuf, 0);
output.next_sample_rate = output.current_sample_rate = va_arg(args, u32_t);
output.external = DECODE_BT;
output.state = OUTPUT_STOPPED;
@@ -251,15 +252,21 @@ static bool raop_sink_cmd_handler(raop_event_t event, va_list args)
break;
}
case RAOP_SETUP:
// we need a fair bit of space for RTP process
_buf_resize(outputbuf, RAOP_OUTPUT_SIZE);
case RAOP_SETUP: {
uint8_t **buffer = va_arg(args, uint8_t**);
size_t *size = va_arg(args, size_t*);
// steal buffer tail from outputbuf but do not reallocate
*size = _buf_limit(outputbuf, RAOP_OUTPUT_SIZE);
*buffer = outputbuf->writep + RAOP_OUTPUT_SIZE;
output.frames_played = 0;
output.external = DECODE_RAOP;
output.state = OUTPUT_STOPPED;
if (decode.state != DECODE_STOPPED) decode.state = DECODE_ERROR;
LOG_INFO("resizing buffer %u", outputbuf->size);
break;
}
case RAOP_STREAM:
LOG_INFO("Stream", NULL);
raop_state = event;
@@ -271,10 +278,9 @@ static bool raop_sink_cmd_handler(raop_event_t event, va_list args)
break;
case RAOP_STOP:
case RAOP_FLUSH:
if (event == RAOP_FLUSH) { LOG_INFO("Flush", NULL); }
else { LOG_INFO("Stop", NULL); }
LOG_INFO("%s", event == RAOP_FLUSH ? "Flush" : "Stop");
_buf_flush(outputbuf);
raop_state = event;
_buf_flush(outputbuf);
if (output.state > OUTPUT_STOPPED) output.state = OUTPUT_STOPPED;
abort_sink = true;
output.frames_played = 0;

View File

@@ -354,7 +354,6 @@ void output_init_common(log_level level, const char *device, unsigned output_buf
loglevel = level;
output_buf_size = output_buf_size - (output_buf_size % BYTES_PER_FRAME);
output.init_size = output_buf_size;
LOG_DEBUG("outputbuf size: %u", output_buf_size);
buf_init(outputbuf, output_buf_size);

View File

@@ -391,7 +391,7 @@ static void process_strm(u8_t *pkt, int len) {
#if EMBEDDED
if (output.external) decode_restore(output.external);
output.external = 0;
_buf_resize(outputbuf, output.init_size);
_buf_limit(outputbuf, 0);
#endif
output.threshold = strm->output_threshold;
output.next_replay_gain = unpackN(&strm->replay_gain);

View File

@@ -532,6 +532,7 @@ struct buffer {
u8_t *wrap;
size_t size;
size_t base_size;
size_t true_size;
mutex_type mutex;
};
@@ -547,6 +548,7 @@ void _buf_flush(struct buffer *buf);
void _buf_unwrap(struct buffer *buf, size_t cont);
void buf_adjust(struct buffer *buf, size_t mod);
void _buf_resize(struct buffer *buf, size_t size);
size_t _buf_limit(struct buffer *buf, size_t limit);
void buf_init(struct buffer *buf, size_t size);
void buf_destroy(struct buffer *buf);
@@ -665,7 +667,6 @@ struct outputstate {
u8_t channels;
const char *device;
int external;
u32_t init_size;
#if ALSA
unsigned buffer;
unsigned period;