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

aubuf: set auframe fields correct in read_auframe loop #750

Merged
merged 3 commits into from
Apr 2, 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
14 changes: 9 additions & 5 deletions rem/aubuf/aubuf.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ static void read_auframe(struct aubuf *ab, struct auframe *af)
size_t sample_size = aufmt_sample_size(af->fmt);
size_t sz = auframe_size(af);
uint8_t *p = af->sampv;
bool first = true;

while (le) {
struct frame *f = le->data;
Expand All @@ -85,11 +86,13 @@ static void read_auframe(struct aubuf *ab, struct auframe *af)
(void)mbuf_read_mem(f->mb, p, n);
ab->cur_sz -= n;

af->id = f->af.id;
af->srate = f->af.srate;
af->ch = f->af.ch;
af->timestamp = f->af.timestamp;
af->fmt = f->af.fmt;
if (first) {
af->id = f->af.id;
af->srate = f->af.srate;
af->ch = f->af.ch;
af->timestamp = f->af.timestamp;
af->fmt = f->af.fmt;
}

if (!mbuf_get_left(f->mb)) {
mem_deref(f);
Expand All @@ -105,6 +108,7 @@ static void read_auframe(struct aubuf *ab, struct auframe *af)

p += n;
sz -= n;
first = false;
}
}

Expand Down
19 changes: 10 additions & 9 deletions test/aubuf.c
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ static int test_aubuf_auframe(void)
float sampv_out[3 * FRAMES + (FRAMES / 2)];
uint64_t dt;

struct auframe af;
struct auframe af_in;
struct auframe af_out;
int err;
Expand All @@ -120,14 +121,15 @@ static int test_aubuf_auframe(void)
TEST_EQUALS(0, aubuf_cur_size(ab));

/* write first frame (filling with wish_sz) */
auframe_init(&af_in, AUFMT_FLOAT, sampv_in, FRAMES, 48000, 2);
af_in.timestamp = 0;
auframe_init(&af, AUFMT_FLOAT, sampv_in, FRAMES, 48000, 2);
af_in = af;

dt = FRAMES * AUDIO_TIMEBASE / (af_in.srate * af_in.ch);

err = aubuf_write_auframe(ab, &af_in);
TEST_ERR(err);
TEST_EQUALS(FRAMES * sizeof(float), aubuf_cur_size(ab));
TEST_EQUALS(auframe_size(&af), aubuf_cur_size(ab));

/* first read after filling should start aubuf */
af_out.fmt = AUFMT_FLOAT;
Expand All @@ -144,15 +146,15 @@ static int test_aubuf_auframe(void)

err = aubuf_write_auframe(ab, &af_in);
TEST_ERR(err);
TEST_EQUALS(FRAMES * sizeof(float), aubuf_cur_size(ab));
TEST_EQUALS(auframe_size(&af), aubuf_cur_size(ab));

/* read half frame */
af_out.sampc = FRAMES / 2;
af_out.sampv = &sampv_out[FRAMES];
aubuf_read_auframe(ab, &af_out);

/* the first read drops old data: 80 - 40 = 40 */
TEST_EQUALS((FRAMES / 2) * sizeof(float), aubuf_cur_size(ab));
TEST_EQUALS(auframe_size(&af)/2, aubuf_cur_size(ab));
TEST_EQUALS(dt, af_out.timestamp);

/* write one frame */
Expand All @@ -162,8 +164,7 @@ static int test_aubuf_auframe(void)

err = aubuf_write_auframe(ab, &af_in);
TEST_ERR(err);
TEST_EQUALS((FRAMES + (FRAMES / 2)) * sizeof(float),
aubuf_cur_size(ab));
TEST_EQUALS(auframe_size(&af) * 3 / 2, aubuf_cur_size(ab));

/* write half frame */
af_in.sampv = &sampv_in[3 * FRAMES];
Expand All @@ -172,13 +173,13 @@ static int test_aubuf_auframe(void)

err = aubuf_write_auframe(ab, &af_in);
TEST_ERR(err);
TEST_EQUALS(2 * FRAMES * sizeof(float), aubuf_cur_size(ab));
TEST_EQUALS(auframe_size(&af) * 2, aubuf_cur_size(ab));

/* read half frame */
af_out.sampv = &sampv_out[(FRAMES + (FRAMES / 2))];
af_out.sampc = FRAMES / 2;
aubuf_read_auframe(ab, &af_out);
TEST_EQUALS((FRAMES + FRAMES / 2) * sizeof(float), aubuf_cur_size(ab));
TEST_EQUALS(auframe_size(&af) * 3 / 2, aubuf_cur_size(ab));
TEST_EQUALS(3 * (dt / 2) + 1, af_out.timestamp);

/* read one and a half frame */
Expand All @@ -188,7 +189,7 @@ static int test_aubuf_auframe(void)

TEST_EQUALS(2, af_out.ch);
TEST_EQUALS(48000, af_out.srate);
TEST_EQUALS(3 * dt, af_out.timestamp);
TEST_EQUALS(2 * dt, af_out.timestamp);

TEST_MEMCMP(sampv_in, sizeof(sampv_in), sampv_out, sizeof(sampv_out));
TEST_EQUALS(0, aubuf_cur_size(ab));
Expand Down