Skip to content

Instantly share code, notes, and snippets.

@sreimers
Created June 4, 2024 10:31
Show Gist options
  • Save sreimers/8c25a58626b814c9fbb6fc3ec8600a13 to your computer and use it in GitHub Desktop.
Save sreimers/8c25a58626b814c9fbb6fc3ec8600a13 to your computer and use it in GitHub Desktop.
#include <stdlib.h>
#include <re.h>
#include <baresip.h>
#define REALTIME 1
static uint64_t next_value = 0;
/**
* Check if a 16-bit sequence wraps around
*
* @param seq_new seq of the current packet
* @param seq_old seq of the previous packet
*
* @return Integer describing the wrap-around
* @retval 0 no wrap-around
* @retval 1 forward wrap-around
*/
static int seq_wrap(uint16_t seq_new, uint16_t seq_old)
{
int16_t delta;
if (seq_new < seq_old) {
delta = (int16_t)seq_new - (int16_t)seq_old;
if (delta > 0)
return 1;
}
return 0;
}
static uint64_t next_play(const struct jbuf *jb)
{
(void)jb;
return next_value;
}
int main(void)
{
struct jbuf *jb;
libre_init();
re_trace_init("re_trace.json");
int err = jbuf_alloc(&jb, 20, 200, 50);
if (err)
return err;
jbuf_set_srate(jb, 48000);
if (!REALTIME)
jbuf_set_next_play_h(jb, next_play);
jbuf_set_type(jb, JBUF_ADAPTIVE);
FILE *fp = fopen("data.csv", "r");
if (!fp) {
warning("data.csv not found!\n");
return EINVAL;
}
char line[100];
int cnt = 1;
uint32_t delta = 960;
uint32_t deltao = 0;
uint64_t ts_arrive = 0;
uint16_t seq_last = 0;
uint32_t seq_ext = 0;
uint32_t seq_offset = 0;
uint32_t seq_wraps = 0;
bool startup = true;
while (fgets(line, 100, fp) != NULL) {
if (cnt++ < 2)
continue;
char *v = strtok(line, ","); /* Packet */
if (!v)
continue;
v = strtok(NULL, ","); /* Sequence */
if (!v)
continue;
uint16_t seq = atoi(v);
if (!startup && seq_wrap(seq, seq_last))
seq_wraps++;
seq_ext = seq + (seq_wraps * UINT16_MAX);
if (!seq_offset)
seq_offset = seq_ext - 1;
seq_last = seq;
startup = false;
v = strtok(NULL, ","); /* Delta (ms) */
if (!v)
continue;
if (REALTIME) {
deltao = atoi(v);
sys_msleep(deltao);
ts_arrive = tmr_jiffies() * 48000 / 1000;
}
else {
deltao = atoi(v);
delta += (deltao * 48000 / 1000);
ts_arrive = delta + 100000;
}
struct rtp_header hdr = {.seq = seq,
.ts = (seq_ext - seq_offset) * 960,
.ts_arrive = ts_arrive};
void *mem = NULL;
if (!REALTIME)
next_value = ts_arrive;
/* next_value = (seq_ext - seq_offset) * 960; */
re_printf("put seq_ext: %u seq: %u ts: %u arrive: %llu "
"buffer: %u next: %llu delta: %u ms next_play: %d\n",
seq_ext - seq_offset, hdr.seq, hdr.ts, hdr.ts_arrive,
jbuf_packets(jb), next_value, deltao, jbuf_next_play(jb));
err = jbuf_put(jb, &hdr, NULL);
if (err == ETIMEDOUT)
warning("timedout\n");
while (1) {
struct rtp_header hdr_get;
err = jbuf_get(jb, &hdr_get, &mem);
if (err != EAGAIN)
break;
}
}
mem_deref(jb);
fclose(fp);
libre_close();
re_trace_close();
mem_debug();
return err;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment