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: frame fixes #806

Merged
merged 2 commits into from
May 17, 2023
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
30 changes: 20 additions & 10 deletions src/jbuf/jbuf.c
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,7 @@ static void calc_rdiff(struct jbuf *jb, uint16_t seq)
int32_t adiff;
int32_t s; /**< EMA coefficient */
uint32_t wish;
uint32_t max = jb->max;
bool down = false;

if (jb->jbtype != JBUF_ADAPTIVE)
Expand All @@ -262,19 +263,25 @@ static void calc_rdiff(struct jbuf *jb, uint16_t seq)
if (!jb->seq_get)
return;

uint32_t fpr = jb->n / jb->nf; /* frame packet ratio */
if (!fpr)
fpr = 1;

max = max / fpr;

rdiff = (int16_t)(jb->seq_put + 1 - seq);
adiff = abs(rdiff * JBUF_RDIFF_EMA_COEFF);
s = adiff > jb->rdiff ? JBUF_RDIFF_UP_SPEED :
jb->wish > 2 ? 1 :
jb->wish > 1 ? 2 : 3;
jb->rdiff += (adiff - jb->rdiff) * s / JBUF_RDIFF_EMA_COEFF;

wish = (uint32_t) (jb->rdiff / JBUF_RDIFF_EMA_COEFF);
wish = (uint32_t) (jb->rdiff / JBUF_RDIFF_EMA_COEFF / fpr);
if (wish < jb->min)
wish = jb->min;

if (wish >= jb->max)
wish = jb->max - 1;
if (wish >= max)
wish = max - 1;

if (wish > jb->wish) {
DEBUG_INFO("wish size changed %u --> %u\n", jb->wish, wish);
Expand Down Expand Up @@ -408,16 +415,19 @@ int jbuf_put(struct jbuf *jb, const struct rtp_header *hdr, void *mem)
f->hdr = *hdr;
f->mem = mem_ref(mem);

/* Count not equal timestamp frames (e.g. video) */
if (f->le.prev) {
struct packet *pre_f = f->le.prev->data;
jb->nf = 1;
LIST_FOREACH(&jb->packetl, le)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to avoid O(n) effort for jbuf_put()?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have not found a reliable way. Out-of-order, delayed packets lead to double or missed frame counts, and incorrect frame counts have a big impact on latency and jitter calculations.

Copy link
Member Author

@sreimers sreimers May 22, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One way maybe could a separate list with frame timestamps

Edit: but this optimizes only video frame counting.

Copy link
Collaborator

@cspiel1 cspiel1 May 22, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The packets are inserted sorted by the sequence number. The sorted insert needs normally only one to a few checks.
Timestamps and sequence number should have the same order. So after the insert only one or a few checks are needed to find packets with same timestamp. Did I miss something?

Edit: I'll try a PR draft for a closer look.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Draft: #821

Copy link
Member Author

@sreimers sreimers May 22, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem is, we don't know if a sequence number is missing, because its lost or because it arrives later. This makes a simple increment unreliable. Maybe a list of missing sequence numbers can work. I plan to add RTCP Feedback NACK to trigger a resend for missing packets (video only).

{
struct packet *cur_p = le->data;
if (!le->next)
break;

struct packet *next_p = le->next->data;

if (f->hdr.ts != pre_f->hdr.ts)
/* Count not equal timestamp frames (e.g. video) */
if (cur_p->hdr.ts != next_p->hdr.ts)
++jb->nf;
}
else {
++jb->nf;
}

out:
mtx_unlock(jb->lock);
Expand Down
107 changes: 72 additions & 35 deletions test/jbuf.c
Original file line number Diff line number Diff line change
Expand Up @@ -201,41 +201,41 @@ int test_jbuf_adaptive(void)
/* Four frames */
DEBUG_INFO("test frame: Four frames\n");
jbuf_flush(jb);
hdr.seq = 320;
hdr.ts = 320;
hdr.seq = 1;
hdr.ts = 100;
err = jbuf_put(jb, &hdr, frv[0]);
TEST_ERR(err);

hdr.seq = 480;
hdr.ts = 480;
hdr.seq = 2;
hdr.ts = 200;
err = jbuf_put(jb, &hdr, frv[1]);
TEST_ERR(err);

hdr.seq = 490;
hdr.ts = 490;
hdr.seq = 3;
hdr.ts = 300;
err = jbuf_put(jb, &hdr, frv[2]);
TEST_ERR(err);

hdr.seq = 491;
hdr.ts = 491;
hdr.seq = 4;
hdr.ts = 400;
err = jbuf_put(jb, &hdr, frv[3]);
TEST_ERR(err);

err = jbuf_get(jb, &hdr2, &mem);
TEST_EQUALS(EAGAIN, err);
TEST_EQUALS(320, hdr2.seq);
TEST_EQUALS(1, hdr2.seq);
TEST_EQUALS(mem, frv[0]);
mem = mem_deref(mem);

err = jbuf_get(jb, &hdr2, &mem);
TEST_EQUALS(EAGAIN, err);
TEST_EQUALS(480, hdr2.seq);
TEST_EQUALS(2, hdr2.seq);
TEST_EQUALS(mem, frv[1]);
mem = mem_deref(mem);

err = jbuf_get(jb, &hdr2, &mem);
TEST_EQUALS(0, err);
TEST_EQUALS(490, hdr2.seq);
TEST_EQUALS(3, hdr2.seq);
TEST_EQUALS(mem, frv[2]);
mem = mem_deref(mem);

Expand All @@ -258,7 +258,7 @@ int test_jbuf_adaptive_video(void)
{
struct rtp_header hdr, hdr2;
struct jbuf *jb = NULL;
char *frv[4];
char *frv[5];
uint32_t i;
void *mem = NULL;
int err;
Expand All @@ -281,53 +281,90 @@ int test_jbuf_adaptive_video(void)
}
}

/* Four packets */
DEBUG_INFO("test: 4 packets (3 frames)\n");

/* --- Test unordered insert --- */
jbuf_flush(jb);
hdr.seq = 320;
hdr.ts = 320;

hdr.seq = 1;
hdr.ts = 100;
err = jbuf_put(jb, &hdr, frv[0]);
TEST_ERR(err);
TEST_EQUALS(1, jbuf_frames(jb));
TEST_EQUALS(1, jbuf_packets(jb));

hdr.seq = 480;
hdr.ts = 320; /* Same frame */
hdr.seq = 2;
hdr.ts = 100; /* Same frame */
err = jbuf_put(jb, &hdr, frv[1]);
TEST_ERR(err);
TEST_EQUALS(1, jbuf_frames(jb));
TEST_EQUALS(2, jbuf_packets(jb));

hdr.seq = 490;
hdr.ts = 490;
hdr.seq = 4;
hdr.ts = 200;
err = jbuf_put(jb, &hdr, frv[2]);
TEST_ERR(err);
TEST_EQUALS(2, jbuf_frames(jb));
TEST_EQUALS(3, jbuf_packets(jb));

hdr.seq = 491;
hdr.ts = 491;
hdr.seq = 3; /* unordered late packet */
hdr.ts = 200;
err = jbuf_put(jb, &hdr, frv[3]);
TEST_ERR(err);
TEST_EQUALS(2, jbuf_frames(jb));
TEST_EQUALS(4, jbuf_packets(jb));

hdr.seq = 5;
hdr.ts = 300;
err = jbuf_put(jb, &hdr, frv[4]);
TEST_ERR(err);
TEST_EQUALS(3, jbuf_frames(jb));
TEST_EQUALS(5, jbuf_packets(jb));

err = jbuf_get(jb, &hdr2, &mem);
TEST_EQUALS(EAGAIN, err);
TEST_EQUALS(320, hdr2.seq);
TEST_EQUALS(mem, frv[0]);
mem = mem_deref(mem);

/* --- Test lost get --- */
jbuf_flush(jb);

hdr.seq = 1;
hdr.ts = 100;
err = jbuf_put(jb, &hdr, frv[0]);
TEST_ERR(err);
TEST_EQUALS(1, jbuf_frames(jb));
TEST_EQUALS(1, jbuf_packets(jb));

hdr.seq = 2;
hdr.ts = 100; /* Same frame */
err = jbuf_put(jb, &hdr, frv[1]);
TEST_ERR(err);
TEST_EQUALS(1, jbuf_frames(jb));
TEST_EQUALS(2, jbuf_packets(jb));

/* LOST hdr.seq = 3; */

hdr.seq = 4;
hdr.ts = 200;
err = jbuf_put(jb, &hdr, frv[2]);
TEST_ERR(err);
TEST_EQUALS(2, jbuf_frames(jb));
TEST_EQUALS(3, jbuf_packets(jb));

hdr.seq = 5;
hdr.ts = 300;
err = jbuf_put(jb, &hdr, frv[3]);
TEST_ERR(err);
TEST_EQUALS(3, jbuf_frames(jb));
TEST_EQUALS(4, jbuf_packets(jb));

err = jbuf_get(jb, &hdr2, &mem);
TEST_EQUALS(EAGAIN, err);
TEST_EQUALS(480, hdr2.seq);
TEST_EQUALS(mem, frv[1]);
mem = mem_deref(mem);
TEST_EQUALS(3, jbuf_frames(jb));
TEST_EQUALS(3, jbuf_packets(jb));

err = jbuf_get(jb, &hdr2, &mem);
TEST_EQUALS(0, err);
TEST_EQUALS(490, hdr2.seq);
TEST_EQUALS(mem, frv[2]);
TEST_EQUALS(EAGAIN, err);
mem = mem_deref(mem);

err = jbuf_get(jb, &hdr2, &mem);
TEST_EQUALS(ENOENT, err);
TEST_EQUALS(2, jbuf_frames(jb));
TEST_EQUALS(2, jbuf_packets(jb));

err = 0;

Expand Down