Skip to content

Commit

Permalink
jbuf: replace shrink timer by wait frame counter
Browse files Browse the repository at this point in the history
  • Loading branch information
cspiel1 committed Oct 2, 2023
1 parent a2effe4 commit 16849a9
Showing 1 changed file with 10 additions and 30 deletions.
40 changes: 10 additions & 30 deletions src/jbuf.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
enum {
JBUF_PUT_TIMEOUT = 400,
JBUF_WAIT_TIMEOUT = 1000,
JBUF_WAIT_FRAMES = 10,
};


Expand Down Expand Up @@ -68,9 +69,7 @@ struct jbuf {
uint64_t tr; /**< Time of previous jbuf_put() */
int pt; /**< Payload type */
bool running; /**< Jitter buffer is running */
struct tmr tmr; /**< Timer for EAGAIN */
bool wait; /**< Wait flag for jbuf_get() */
bool again; /**< Again flag for jbuf_get() */
uint32_t wait; /**< Wait counter [# frames] */

mtx_t *lock; /**< Makes jitter buffer thread safe */
enum jbuf_type jbtype; /**< Jitter buffer type */
Expand Down Expand Up @@ -226,7 +225,6 @@ static void jbuf_destructor(void *data)
{
struct jbuf *jb = data;

tmr_cancel(&jb->tmr);
jbuf_flush(jb);

/* Free all packets in the pool list */
Expand Down Expand Up @@ -269,7 +267,6 @@ int jbuf_alloc(struct jbuf **jbp, uint32_t min, uint32_t max)
jb->min = min;
jb->max = max;
jb->packets = 0;
tmr_init(&jb->tmr);

DEBUG_INFO("alloc: delay=%u-%u [frames]\n", min, max);

Expand Down Expand Up @@ -407,25 +404,6 @@ static bool jbuf_frame_ready(struct jbuf *jb)
}


static void reset_wait(void *arg)
{
struct jbuf *jb = arg;

jb->wait = false;
jb->again = true;
}


static void eagain_later(struct jbuf *jb)
{
jb->wait = true;
if (tmr_isrunning(&jb->tmr))
return;

tmr_start(&jb->tmr, JBUF_WAIT_TIMEOUT, reset_wait, jb);
}


/**
* Put one packet into the jitter buffer
*
Expand Down Expand Up @@ -541,7 +519,9 @@ int jbuf_put(struct jbuf *jb, const struct rtp_header *hdr, void *mem)
p->mem = mem_ref(mem);
if (tail && ((struct packet *)tail->data)->hdr.ts != hdr->ts) {
++jb->nf;
jb->wait = false;

if (jb->wait)
--jb->wait;
}

/* check frame completeness */
Expand Down Expand Up @@ -614,13 +594,12 @@ int jbuf_get(struct jbuf *jb, struct rtp_header *hdr, void **mem)
packet_deref(jb, p);

if (jbuf_frame_ready(jb)) {
p = jb->packetl.head->data;
if (p->hdr.ts == hdr->ts || jb->again) {
if (p->hdr.ts == hdr->ts || !jb->wait) {
p = jb->packetl.head->data;
err = EAGAIN;
jb->again = false;
}
else
eagain_later(jb);
else if (!jb->wait)
jb->wait = JBUF_WAIT_FRAMES;
}

out:
Expand Down Expand Up @@ -700,6 +679,7 @@ void jbuf_flush(struct jbuf *jb)
jb->n = 0;
jb->nf = 0;
jb->ncf = 0;
jb->wait = 0;
jb->end = NULL;
jb->running = false;

Expand Down

0 comments on commit 16849a9

Please sign in to comment.