Skip to content

Commit

Permalink
jbuf: improve average frame/packet ratio calculation
Browse files Browse the repository at this point in the history
use rolling average frame/packet ratio calculation
  • Loading branch information
sreimers committed Sep 24, 2023
1 parent 863fb69 commit feb8a7f
Showing 1 changed file with 39 additions and 6 deletions.
45 changes: 39 additions & 6 deletions src/jbuf/jbuf.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,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,6 +79,9 @@ 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 frame_seq; /**< Last frame sequence number */
float avg_ratio; /**< Rolling average frame/packet 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 */
Expand Down Expand Up @@ -250,6 +257,24 @@ static void wish_down(void *arg)
}


static void avg_frame_packet_ratio(struct jbuf *jb, uint32_t packets)
{
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 +291,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 @@ -492,15 +517,23 @@ 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->frame_seq)
jb->frame_seq = 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;
if (jb->seq_get > jb->frame_seq)
avg_frame_packet_ratio(
jb, jb->seq_get - jb->frame_seq);
jb->frame_seq = jb->seq_get;
}
}
else {
--jb->nf;
Expand Down

0 comments on commit feb8a7f

Please sign in to comment.