This repository has been archived by the owner on Feb 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
aubuf: preallocate mbuf frames to avoid malloc #55
Draft
sreimers
wants to merge
2
commits into
main
Choose a base branch
from
aubuf_pre_alloc
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ | |
*/ | ||
#include <string.h> | ||
#include <re.h> | ||
#include <re_atomic.h> | ||
#include <rem_au.h> | ||
#include <rem_aulevel.h> | ||
#include <rem_auframe.h> | ||
|
@@ -15,10 +16,20 @@ | |
#define AUBUF_DEBUG 0 | ||
#define AUDIO_TIMEBASE 1000000U | ||
|
||
/* Values for startup preallocation, can be tuned if needed */ | ||
enum { | ||
SRATE = 48000, /* Default sample rate in [Hz] */ | ||
CHANNELS = 2, /* Default number of channels */ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is it possible to avoid hardcoded samplerate values inside the library ? |
||
PTIME = 20, /* Default packet time in [ms] */ | ||
BYTES = 2, /* Default S16 size */ | ||
FRAMES = 10, /* Preallocate 200ms buffer */ | ||
SAMPSZ = SRATE * CHANNELS * PTIME * BYTES / 1000, | ||
}; | ||
|
||
/** Locked audio-buffer with almost zero-copy */ | ||
struct aubuf { | ||
struct list afl; | ||
struct list pfl; | ||
struct lock *lock; | ||
size_t wish_sz; | ||
size_t cur_sz; | ||
|
@@ -38,19 +49,22 @@ struct aubuf { | |
double silence; /**< Silence volume in negative [dB] */ | ||
}; | ||
|
||
|
||
struct frame { | ||
struct le le; | ||
struct le le_afl; | ||
struct le le_pfl; | ||
struct mbuf *mb; | ||
struct auframe af; | ||
size_t sz; | ||
RE_ATOMIC bool free; | ||
}; | ||
|
||
|
||
static void frame_destructor(void *arg) | ||
{ | ||
struct frame *f = arg; | ||
|
||
list_unlink(&f->le); | ||
list_unlink(&f->le_afl); | ||
list_unlink(&f->le_pfl); | ||
mem_deref(f->mb); | ||
} | ||
|
||
|
@@ -60,39 +74,42 @@ static void aubuf_destructor(void *arg) | |
struct aubuf *ab = arg; | ||
|
||
list_flush(&ab->afl); | ||
list_flush(&ab->pfl); | ||
mem_deref(ab->lock); | ||
mem_deref(ab->ajb); | ||
} | ||
|
||
|
||
static void read_auframe(struct aubuf *ab, struct auframe *af) | ||
{ | ||
struct le *le = ab->afl.head; | ||
size_t sample_size = aufmt_sample_size(af->fmt); | ||
size_t sz = auframe_size(af); | ||
uint8_t *p = af->sampv; | ||
struct le *le = ab->afl.head; | ||
size_t sz = auframe_size(af); | ||
uint8_t *p = af->sampv; | ||
size_t samp_sz = aufmt_sample_size(af->fmt); | ||
|
||
while (le) { | ||
struct frame *f = le->data; | ||
size_t n; | ||
|
||
le = le->next; | ||
|
||
n = min(mbuf_get_left(f->mb), sz); | ||
n = min(f->sz, sz); | ||
|
||
(void)mbuf_read_mem(f->mb, p, n); | ||
ab->cur_sz -= n; | ||
f->sz -= n; | ||
|
||
af->srate = f->af.srate; | ||
af->ch = f->af.ch; | ||
af->timestamp = f->af.timestamp; | ||
|
||
if (!mbuf_get_left(f->mb)) { | ||
mem_deref(f); | ||
if (!f->sz) { | ||
list_unlink(&f->le_afl); | ||
f->free = true; | ||
} | ||
else if (af->srate && af->ch && sample_size) { | ||
else if (af->srate && af->ch && samp_sz) { | ||
f->af.timestamp += n * AUDIO_TIMEBASE / | ||
(af->srate * af->ch * sample_size); | ||
(af->srate * af->ch * samp_sz); | ||
} | ||
|
||
if (n == sz) | ||
|
@@ -104,6 +121,23 @@ static void read_auframe(struct aubuf *ab, struct auframe *af) | |
} | ||
|
||
|
||
static inline struct frame *frame_alloc(struct aubuf *ab, size_t sz) | ||
{ | ||
struct frame *f = mem_zalloc(sizeof(*f), frame_destructor); | ||
if (!f) | ||
return NULL; | ||
|
||
f->mb = mbuf_alloc(sz); | ||
if (!f->mb) | ||
return NULL; | ||
|
||
f->free = true; | ||
list_append(&ab->pfl, &f->le_pfl, f); | ||
|
||
return f; | ||
} | ||
|
||
|
||
/** | ||
* Allocate a new audio buffer | ||
* | ||
|
@@ -133,6 +167,14 @@ int aubuf_alloc(struct aubuf **abp, size_t min_sz, size_t max_sz) | |
ab->max_sz = max_sz; | ||
ab->filling = true; | ||
|
||
/* Preallocate a good size for startup. mbuf can resize if necessary | ||
* and aubuf_write_auframe() would append more frames if needed */ | ||
for (size_t i = 0; i < FRAMES; i++) { | ||
struct frame *f = frame_alloc(ab, SAMPSZ); | ||
if (!f) | ||
return ENOMEM; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. instead of pre-allocating, is it possible to reuse the buffer instead ? |
||
|
||
out: | ||
if (err) | ||
mem_deref(ab); | ||
|
@@ -211,26 +253,17 @@ static bool frame_less_equal(struct le *le1, struct le *le2, void *arg) | |
* | ||
* @return 0 for success, otherwise error code | ||
*/ | ||
int aubuf_append_auframe(struct aubuf *ab, struct mbuf *mb, struct auframe *af) | ||
static int append_auframe(struct aubuf *ab, struct frame *f) | ||
{ | ||
struct frame *f; | ||
size_t max_sz; | ||
|
||
if (!ab || !mb) | ||
if (!ab || !f) | ||
return EINVAL; | ||
|
||
f = mem_zalloc(sizeof(*f), frame_destructor); | ||
if (!f) | ||
return ENOMEM; | ||
|
||
f->mb = mem_ref(mb); | ||
if (af) | ||
f->af = *af; | ||
|
||
lock_write_get(ab->lock); | ||
|
||
list_insert_sorted(&ab->afl, frame_less_equal, NULL, &f->le, f); | ||
ab->cur_sz += mbuf_get_left(mb); | ||
list_insert_sorted(&ab->afl, frame_less_equal, NULL, &f->le_afl, f); | ||
ab->cur_sz += f->sz; | ||
|
||
max_sz = ab->started ? ab->max_sz : ab->wish_sz + 1; | ||
if (ab->max_sz && ab->cur_sz > max_sz) { | ||
|
@@ -243,8 +276,9 @@ int aubuf_append_auframe(struct aubuf *ab, struct mbuf *mb, struct auframe *af) | |
#endif | ||
f = list_ledata(ab->afl.head); | ||
if (f) { | ||
ab->cur_sz -= mbuf_get_left(f->mb); | ||
mem_deref(f); | ||
ab->cur_sz -= f->sz; | ||
list_unlink(&f->le_afl); | ||
f->free = true; | ||
} | ||
} | ||
|
||
|
@@ -267,32 +301,50 @@ int aubuf_append_auframe(struct aubuf *ab, struct mbuf *mb, struct auframe *af) | |
*/ | ||
int aubuf_write_auframe(struct aubuf *ab, struct auframe *af) | ||
{ | ||
struct mbuf *mb; | ||
struct frame *f = NULL; | ||
size_t sz; | ||
size_t sample_size; | ||
struct le *le; | ||
int err; | ||
|
||
if (!ab || !af) | ||
if (!ab || !af || !af->sampv) | ||
return EINVAL; | ||
|
||
sample_size = aufmt_sample_size(af->fmt); | ||
if (sample_size) | ||
sz = af->sampc * aufmt_sample_size(af->fmt); | ||
else | ||
sz = af->sampc; | ||
|
||
mb = mbuf_alloc(sz); | ||
if (!sz) | ||
return EINVAL; | ||
|
||
if (!mb) | ||
return ENOMEM; | ||
LIST_FOREACH(&ab->pfl, le) | ||
{ | ||
struct frame *fp = le->data; | ||
|
||
(void)mbuf_write_mem(mb, af->sampv, sz); | ||
mb->pos = 0; | ||
if (!fp->free) | ||
continue; | ||
|
||
err = aubuf_append_auframe(ab, mb, af); | ||
f = fp; | ||
break; | ||
} | ||
|
||
lock_write_get(ab->lock); | ||
mem_deref(mb); | ||
lock_rel(ab->lock); | ||
if (!f) { | ||
f = frame_alloc(ab, sz); | ||
if (!f) | ||
return ENOMEM; | ||
} | ||
|
||
f->free = false; | ||
f->sz = sz; | ||
f->af = *af; | ||
|
||
f->mb->pos = 0; | ||
(void)mbuf_write_mem(f->mb, af->sampv, f->sz); | ||
f->mb->pos = 0; | ||
|
||
err = append_auframe(ab, f); | ||
|
||
if (!ab->filling && ab->ajb) | ||
ajb_calc(ab->ajb, af, ab->cur_sz); | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this API should probably be removed.
it contains no Samplerate or Channels info, which is needed now