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

jbuf: improve average packets per frame ratio calculation #962

Closed
wants to merge 2 commits into from
Closed
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
120 changes: 114 additions & 6 deletions src/jbuf/jbuf.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <re_thread.h>
#include <re_tmr.h>
#include <re_jbuf.h>
#include <re_trace.h>

#include <stdlib.h>

Expand All @@ -38,10 +39,14 @@
#define STAT_INC(var)
#endif

#define JBUF_NORMAL_CONVERGE 0.2f
#define JBUF_FAST_CONVERGE 0.4f

enum {
JBUF_RDIFF_EMA_COEFF = 1024,
JBUF_RDIFF_UP_SPEED = 512,
JBUF_PUT_TIMEOUT = 400,
JBUF_RDIFF_EMA_COEFF = 1024,
JBUF_RDIFF_UP_SPEED = 512,
JBUF_PUT_TIMEOUT = 400,
JBUF_FAST_CONVERGE_TR = 5,
};


Expand Down Expand Up @@ -75,15 +80,73 @@ struct jbuf {
bool running; /**< Jitter buffer is running */
int32_t rdiff; /**< Average out of order reverse diff */
struct tmr tmr; /**< Rdiff down timer */
uint32_t seq_frame; /**< Last frame sequence number */
float avg_ratio; /**< Rolling average packets per frame ratio */
uint16_t start_frame_cnt; /**< Rolling average start counter */

mtx_t *lock; /**< Makes jitter buffer thread safe */
enum jbuf_type jbtype; /**< Jitter buffer type */
#if JBUF_STAT
struct jbuf_stat stat; /**< Jitter buffer Statistics */
#endif
#ifdef RE_JBUF_TRACE
uint64_t tr00; /**< Arrival of first packet */
char buf[136]; /**< Buffer for trace */
#endif
};


#ifdef RE_JBUF_TRACE
static void plot_jbuf(struct jbuf *jb, uint64_t tr)
{
uint32_t treal;
uint32_t rdiff = (uint32_t)(jb->rdiff / (float)JBUF_RDIFF_EMA_COEFF);

if (!jb->tr00)
jb->tr00 = tr;

treal = (uint32_t) (tr - jb->tr00);
re_snprintf(jb->buf, sizeof(jb->buf),
"%s, 0x%p, %u, %u, %u, %u, %u",
__func__, /* row 1 - grep */
jb, /* row 2 - grep optional */
treal, /* row 3 - plot x-axis */
rdiff, /* row 4 - plot */
jb->wish, /* row 5 - plot */
jb->n, /* row 6 - plot */
jb->nf); /* row 7 - plot */
re_trace_event("jbuf", "plot", 'P', NULL, 0, RE_TRACE_ARG_STRING_COPY,
"line", jb->buf);
}


static void plot_jbuf_event(struct jbuf *jb, char ph)
{
uint32_t treal;
uint64_t tr;

tr = tmr_jiffies();
if (!jb->tr00)
jb->tr00 = tr;

treal = (uint32_t) (tr - jb->tr00);
re_snprintf(jb->buf, sizeof(jb->buf), "%s, 0x%p, %u, %i",
__func__, /* row 1 - grep */
jb, /* row 2 - grep optional */
treal, /* row 3 - plot x-axis */
1); /* row 4 - plot */
re_trace_event("jbuf", "plot", ph, NULL, 0, RE_TRACE_ARG_STRING_COPY,
"line", jb->buf);
}
#else
static void plot_jbuf_event(struct jbuf *jb, char ph)
{
(void)jb;
(void)ph;
}
#endif


/** Is x less than y? */
static inline bool seq_less(uint16_t x, uint16_t y)
{
Expand Down Expand Up @@ -148,6 +211,10 @@ static void jbuf_destructor(void *data)
/* Free all packets in the pool list */
list_flush(&jb->pooll);
mem_deref(jb->lock);

#ifdef RE_JBUF_TRACE
(void)re_trace_close();
#endif
}


Expand Down Expand Up @@ -209,6 +276,10 @@ int jbuf_alloc(struct jbuf **jbp, uint32_t min, uint32_t max)
DEBUG_INFO("alloc: adding to pool list %u\n", i);
}

#ifdef RE_JBUF_TRACE
(void)re_trace_init("re_jbuf.json");
#endif

out:
if (err)
mem_deref(jb);
Expand Down Expand Up @@ -250,6 +321,29 @@ static void wish_down(void *arg)
}


static void avg_packet_frame_ratio(struct jbuf *jb)
{
if (jb->seq_get <= jb->seq_frame)
return;

uint32_t packets = jb->seq_get - jb->seq_frame;

if (jb->start_frame_cnt > JBUF_FAST_CONVERGE_TR) {
jb->avg_ratio = jb->avg_ratio * (1 - JBUF_NORMAL_CONVERGE) +
packets * JBUF_NORMAL_CONVERGE;
}
else if (jb->start_frame_cnt > 0) {
jb->avg_ratio = jb->avg_ratio * (1 - JBUF_FAST_CONVERGE) +
packets * JBUF_FAST_CONVERGE;
jb->start_frame_cnt++;
}
else {
jb->avg_ratio = (float)packets;
jb->start_frame_cnt++;
}
}


static void calc_rdiff(struct jbuf *jb, uint16_t seq)
{
int32_t rdiff;
Expand All @@ -266,8 +360,8 @@ static void calc_rdiff(struct jbuf *jb, uint16_t seq)
if (!jb->seq_get)
return;

if (jb->nf) {
ratio = (float)jb->n / (float)jb->nf;
if (jb->avg_ratio) {
ratio = jb->avg_ratio;
max = (uint32_t)(max / ratio);
}

Expand Down Expand Up @@ -354,6 +448,7 @@ int jbuf_put(struct jbuf *jb, const struct rtp_header *hdr, void *mem)
/* Packet arrived too late to be put into buffer */
if (jb->seq_get && seq_less(seq, jb->seq_get + 1)) {
STAT_INC(n_late);
plot_jbuf_event(jb, 'L');
DEBUG_INFO("packet too late: seq=%u "
"(seq_put=%u seq_get=%u)\n",
seq, jb->seq_put, jb->seq_get);
Expand Down Expand Up @@ -392,6 +487,7 @@ int jbuf_put(struct jbuf *jb, const struct rtp_header *hdr, void *mem)
/* Detect duplicates */
DEBUG_INFO("duplicate: seq=%u\n", seq);
STAT_INC(n_dups);
plot_jbuf_event(jb, 'D');
list_insert_after(&jb->packetl, le, &f->le, f);
packet_deref(jb, f);
err = EALREADY;
Expand All @@ -409,6 +505,7 @@ int jbuf_put(struct jbuf *jb, const struct rtp_header *hdr, void *mem)
}

STAT_INC(n_oos);
plot_jbuf_event(jb, 'S');

success:
/* Update last sequence */
Expand All @@ -434,6 +531,9 @@ int jbuf_put(struct jbuf *jb, const struct rtp_header *hdr, void *mem)
++jb->nf;

out:
#ifdef RE_JBUF_TRACE
plot_jbuf(jb, tr);
#endif
mtx_unlock(jb->lock);
return err;
}
Expand Down Expand Up @@ -464,6 +564,7 @@ int jbuf_get(struct jbuf *jb, struct rtp_header *hdr, void **mem)
DEBUG_INFO("not enough buffer packets - wait.. "
"(n=%u wish=%u)\n", jb->n, jb->wish);
STAT_INC(n_underflow);
plot_jbuf_event(jb, 'U');
err = ENOENT;
goto out;
}
Expand Down Expand Up @@ -492,15 +593,21 @@ int jbuf_get(struct jbuf *jb, struct rtp_header *hdr, void **mem)
/* Update sequence number for 'get' */
jb->seq_get = f->hdr.seq;

if (!jb->seq_frame)
jb->seq_frame = jb->seq_get;

*hdr = f->hdr;
*mem = mem_ref(f->mem);

/* decrease not equal frames */
if (f->le.next) {
struct packet *next_f = f->le.next->data;

if (f->hdr.ts != next_f->hdr.ts)
if (f->hdr.ts != next_f->hdr.ts) {
--jb->nf;
avg_packet_frame_ratio(jb);
jb->seq_frame = jb->seq_get;
}
}
else {
--jb->nf;
Expand Down Expand Up @@ -611,6 +718,7 @@ void jbuf_flush(struct jbuf *jb)
n_flush = STAT_INC(n_flush);
memset(&jb->stat, 0, sizeof(jb->stat));
jb->stat.n_flush = n_flush;
plot_jbuf_event(jb, 'F');
#endif
mtx_unlock(jb->lock);
}
Expand Down