Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

aubuf: add AUBUF_TRACE mode with id #1174

Merged
merged 1 commit into from
Aug 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions include/rem_aubuf.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ enum aubuf_mode {
};

int aubuf_alloc(struct aubuf **abp, size_t min_sz, size_t max_sz);
void aubuf_set_id(struct aubuf *ab, struct pl *id);
void aubuf_set_live(struct aubuf *ab, bool live);
void aubuf_set_mode(struct aubuf *ab, enum aubuf_mode mode);
void aubuf_set_silence(struct aubuf *ab, double silence);
Expand Down
1 change: 1 addition & 0 deletions include/rem_auframe.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,4 @@ size_t auframe_size(const struct auframe *af);
void auframe_mute(struct auframe *af);
double auframe_level(struct auframe *af);
uint64_t auframe_bytes_to_timestamp(const struct auframe *af, size_t n);
uint64_t auframe_bytes_to_ms(const struct auframe *af, size_t n);
43 changes: 31 additions & 12 deletions rem/aubuf/aubuf.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
*
* Copyright (C) 2010 Creytiv.com
*/
#undef RE_TRACE_ENABLED
#if AUBUF_TRACE
#define RE_TRACE_ENABLED 1
#endif
#include <string.h>
#include <re.h>
#include <rem_au.h>
Expand All @@ -18,6 +22,7 @@
/** Locked audio-buffer with almost zero-copy */
struct aubuf {
struct list afl;
struct pl *id; /**< Audio buffer Identifier */
mtx_t *lock;
size_t wish_sz;
size_t cur_sz;
Expand Down Expand Up @@ -63,6 +68,7 @@ static void aubuf_destructor(void *arg)
list_flush(&ab->afl);
mem_deref(ab->lock);
mem_deref(ab->ajb);
mem_deref(ab->id);
}


Expand Down Expand Up @@ -152,6 +158,23 @@ int aubuf_alloc(struct aubuf **abp, size_t min_sz, size_t max_sz)
}


/**
* Set buffer id.
*
* @param ab Audio buffer.
* @param id Identifier.
*/
void aubuf_set_id(struct aubuf *ab, struct pl *id)
{
if (!ab)
return;

mtx_lock(ab->lock);
ab->id = mem_ref(id);
mtx_unlock(ab->lock);
}


/**
* Sets the live stream flag on/off. If activated the audio buffer drops old
* frames on first read to keep the latency under `min_sz` bytes on startup.
Expand Down Expand Up @@ -272,10 +295,7 @@ int aubuf_append_auframe(struct aubuf *ab, struct mbuf *mb,

if (ab->max_sz && ab->cur_sz > ab->max_sz) {
++ab->stats.or;
#if AUBUF_DEBUG
(void)re_printf("aubuf: %p overrun (cur=%zu/%zu)\n",
ab, ab->cur_sz, ab->max_sz);
#endif
RE_TRACE_ID_INSTANT("aubuf", "overrun", ab->id);
f = list_ledata(ab->afl.head);
if (f) {
ab->cur_sz -= mbuf_get_left(f->mb);
Expand Down Expand Up @@ -367,25 +387,24 @@ void aubuf_read_auframe(struct aubuf *ab, struct auframe *af)
goto out;
}

RE_TRACE_ID_INSTANT_I("aubuf", "cur_sz_ms",
auframe_bytes_to_ms(af, ab->cur_sz), ab->id);

if (ab->fill_sz || ab->cur_sz < sz) {
if (!ab->fill_sz) {
++ab->stats.ur;
#if AUBUF_DEBUG
(void)re_printf("aubuf: %p underrun "
"(cur=%zu, sz=%zu)\n",
ab, ab->cur_sz, sz);
fflush(stdout);
plot_underrun(ab->ajb);
#endif
RE_TRACE_ID_INSTANT("aubuf", "underrun", ab->id);
}

if (!ab->fill_sz)
ajb_set_ts0(ab->ajb, 0);

filling = ab->fill_sz > 0;
memset(af->sampv, 0, sz);
if (filling)
if (filling) {
RE_TRACE_ID_INSTANT("aubuf", "filling", ab->id);
goto out;
}
else
ab->fill_sz = ab->wish_sz;
}
Expand Down
8 changes: 8 additions & 0 deletions rem/auframe/auframe.c
Original file line number Diff line number Diff line change
Expand Up @@ -114,3 +114,11 @@ uint64_t auframe_bytes_to_timestamp(const struct auframe *af, size_t n)
return ((uint64_t) n) * AUDIO_TIMEBASE /
(af->srate * af->ch * sample_size);
}


uint64_t auframe_bytes_to_ms(const struct auframe *af, size_t n)
{
size_t sample_size = aufmt_sample_size(af->fmt);

return ((uint64_t)n * 1000) / (af->srate * af->ch * sample_size);
}
Loading