Skip to content
This repository has been archived by the owner on Feb 16, 2023. It is now read-only.

Commit

Permalink
aubuf: prevent faulty timestamps (#75)
Browse files Browse the repository at this point in the history
* aubuf: prevent faulty timestamps for partial read

There are applications where a useful timestamp is not available. E.g. if audio
files are read and decoded. Then audio frames are written to aubuf with
`timestamp == 0`. In such cases a partial read with `aubuf_read_auframe()` set
a wrong timestamp to the rest frame.

* aubuf: set correct timestamps if app provides zero

For partial read timestamps are changed for the rest frames. This works only
if the timestamps are already set correctly during writing.

This commit handles also the case correct if the application provides
timestamps and the first frame has timestamp zero and the first read is
partial.

* aubuf,auframe: move auframe_bytes_to_timestamp() to auframe

* aubuf: reset also written_sz in aubuf_flush()
  • Loading branch information
cspiel1 authored Jul 29, 2022
1 parent 9c6cdd1 commit 7eb6d16
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 4 deletions.
3 changes: 3 additions & 0 deletions include/rem_auframe.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
* Audio frame
*/

#define AUDIO_TIMEBASE 1000000U

/**
* Defines a frame of audio samples
*/
Expand Down Expand Up @@ -42,3 +44,4 @@ static inline void auframe_update(struct auframe *af, void *sampv,
size_t auframe_size(const struct auframe *af);
void auframe_mute(struct auframe *af);
double auframe_level(struct auframe *af);
size_t auframe_bytes_to_timestamp(const struct auframe *af, size_t n);
2 changes: 0 additions & 2 deletions src/aubuf/ajb.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
* Copyright (C) 2022 Commend.com - c.spielberger@commend.com
*/

#define AUDIO_TIMEBASE 1000000U

enum ajb_state {
AJB_GOOD = 0,
AJB_LOW,
Expand Down
13 changes: 11 additions & 2 deletions src/aubuf/aubuf.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ struct aubuf {
size_t max_sz;
size_t fill_sz; /**< To fill size */
size_t pkt_sz; /**< Packet size */
size_t wr_sz; /**< Written size */
bool started;
uint64_t ts;

Expand Down Expand Up @@ -91,8 +92,9 @@ static void read_auframe(struct aubuf *ab, struct auframe *af)
mem_deref(f);
}
else if (af->srate && af->ch && sample_size) {
f->af.timestamp += n * AUDIO_TIMEBASE /
(af->srate * af->ch * sample_size);

f->af.timestamp +=
auframe_bytes_to_timestamp(&f->af, n);
}

if (n == sz)
Expand Down Expand Up @@ -235,8 +237,14 @@ int aubuf_append_auframe(struct aubuf *ab, struct mbuf *mb,
if (ab->fill_sz >= ab->pkt_sz)
ab->fill_sz -= ab->pkt_sz;

if (!f->af.timestamp && f->af.srate && f->af.ch) {
f->af.timestamp =
auframe_bytes_to_timestamp(&f->af, ab->wr_sz);
}

list_insert_sorted(&ab->afl, frame_less_equal, NULL, &f->le, f);
ab->cur_sz += sz;
ab->wr_sz += sz;

if (ab->max_sz && ab->cur_sz > ab->max_sz) {
#if AUBUF_DEBUG
Expand Down Expand Up @@ -446,6 +454,7 @@ void aubuf_flush(struct aubuf *ab)
list_flush(&ab->afl);
ab->fill_sz = ab->wish_sz;
ab->cur_sz = 0;
ab->wr_sz = 0;
ab->ts = 0;

mtx_unlock(ab->lock);
Expand Down
8 changes: 8 additions & 0 deletions src/auframe/auframe.c
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,11 @@ double auframe_level(struct auframe *af)

return af->level;
}


size_t auframe_bytes_to_timestamp(const struct auframe *af, size_t n)
{
size_t sample_size = aufmt_sample_size(af->fmt);

return n * AUDIO_TIMEBASE / (af->srate * af->ch * sample_size);
}

0 comments on commit 7eb6d16

Please sign in to comment.