From fe0c2016742312165b00e489c41dbd18687f074a Mon Sep 17 00:00:00 2001 From: Sebastian Reimers Date: Mon, 5 Aug 2024 11:55:12 +0200 Subject: [PATCH] aubuf: add AUBUF_TRACE mode with id (#1174) --- include/rem_aubuf.h | 1 + include/rem_auframe.h | 1 + rem/aubuf/aubuf.c | 43 +++++++++++++++++++++++++++++++------------ rem/auframe/auframe.c | 8 ++++++++ 4 files changed, 41 insertions(+), 12 deletions(-) diff --git a/include/rem_aubuf.h b/include/rem_aubuf.h index 0b9655deb..b4e3a61eb 100644 --- a/include/rem_aubuf.h +++ b/include/rem_aubuf.h @@ -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); diff --git a/include/rem_auframe.h b/include/rem_auframe.h index d4fc472d4..b2da0c0fa 100644 --- a/include/rem_auframe.h +++ b/include/rem_auframe.h @@ -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); diff --git a/rem/aubuf/aubuf.c b/rem/aubuf/aubuf.c index f2b300f83..1052a16a6 100644 --- a/rem/aubuf/aubuf.c +++ b/rem/aubuf/aubuf.c @@ -3,6 +3,10 @@ * * Copyright (C) 2010 Creytiv.com */ +#undef RE_TRACE_ENABLED +#if AUBUF_TRACE +#define RE_TRACE_ENABLED 1 +#endif #include #include #include @@ -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; @@ -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); } @@ -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. @@ -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); @@ -367,16 +387,13 @@ 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) @@ -384,8 +401,10 @@ void aubuf_read_auframe(struct aubuf *ab, struct auframe *af) 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; } diff --git a/rem/auframe/auframe.c b/rem/auframe/auframe.c index 60d1b2de2..46862bf1f 100644 --- a/rem/auframe/auframe.c +++ b/rem/auframe/auframe.c @@ -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); +}