Skip to content

Commit

Permalink
debug_cmd: command aufileinfo return answer (baresip#3030)
Browse files Browse the repository at this point in the history
  • Loading branch information
cspiel1 authored Jun 5, 2024
1 parent 0831fe1 commit c5bc699
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 66 deletions.
1 change: 1 addition & 0 deletions include/baresip.h
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,7 @@ struct ausrc_prm {
uint8_t ch; /**< Number of channels */
uint32_t ptime; /**< Wanted packet-time in [ms] */
int fmt; /**< Sample format (enum aufmt) */
size_t duration; /**< Duration in [ms], 0 for infinite */
};

typedef void (ausrc_read_h)(struct auframe *af, void *arg);
Expand Down
27 changes: 10 additions & 17 deletions modules/aufile/aufile_src.c
Original file line number Diff line number Diff line change
Expand Up @@ -195,11 +195,9 @@ int aufile_src_alloc(struct ausrc_st **stp, const struct ausrc *as,
{
struct ausrc_st *st;
struct aufile_prm fprm;
uint32_t ptime;
bool join = false;
int err;

if (!stp || !as || !prm || !rh)
if (!stp || !as || !prm)
return EINVAL;

if (prm->fmt != AUFMT_S16LE) {
Expand All @@ -219,12 +217,6 @@ int aufile_src_alloc(struct ausrc_st **stp, const struct ausrc *as,
st->arg = arg;
st->ptime = prm->ptime;

/* ptime == 0 means blocking mode */
join = st->ptime == 0;
ptime = st->ptime;
if (!ptime)
ptime = 40;

err = aufile_open(&st->aufile, &fprm, dev, AUFILE_READ);
if (err) {
warning("aufile: failed to open file '%s' (%m)\n", dev, err);
Expand All @@ -237,10 +229,17 @@ int aufile_src_alloc(struct ausrc_st **stp, const struct ausrc *as,
/* return wav format to caller */
prm->srate = fprm.srate;
prm->ch = fprm.channels;
prm->duration = aufile_get_length(st->aufile, &fprm);

if (!rh) {
mem_deref(st);
return 0;
}

st->prm = *prm;

st->fmt = fprm.fmt;
st->sampc = prm->srate * prm->ch * ptime / 1000;
st->sampc = prm->srate * prm->ch * st->ptime / 1000;

info("aufile: audio ptime=%u sampc=%zu\n", st->ptime, st->sampc);

Expand All @@ -253,7 +252,7 @@ int aufile_src_alloc(struct ausrc_st **stp, const struct ausrc *as,
if (err)
goto out;

tmr_start(&st->tmr, ptime, timeout, st);
tmr_start(&st->tmr, st->ptime, timeout, st);

re_atomic_rlx_set(&st->run, true);
st->started = true;
Expand All @@ -264,12 +263,6 @@ int aufile_src_alloc(struct ausrc_st **stp, const struct ausrc *as,
goto out;
}

if (join) {
thrd_join(st->thread, NULL);
st->started = false;
st->errh(0, NULL, st->arg);
}

out:
if (err)
mem_deref(st);
Expand Down
58 changes: 9 additions & 49 deletions modules/debug_cmd/debug_cmd.c
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,7 @@ static int cmd_play_file(struct re_printf *pf, void *arg)
struct fileinfo_st {
struct ausrc_st *ausrc;
struct ausrc_prm prm;
size_t sampc;
struct tmr tmr;
bool finished;
};


Expand All @@ -185,52 +183,20 @@ static void fileinfo_destruct(void *arg)
}


static void fileinfo_timeout(void *arg)
static void print_fileinfo(struct fileinfo_st *st)
{
struct fileinfo_st *st = arg;
double s = 0.;

if (st->prm.ch && st->prm.srate)
s = ((double) st->sampc) / st->prm.ch / st->prm.srate;
double s = ((float) st->prm.duration) / 1000;

if (st->finished) {
if (st->prm.duration) {
info("debug_cmd: length = %1.3lf seconds\n", s);
module_event("debug_cmd", "aufileinfo", NULL, NULL,
"length = %lf seconds", s);
}
else if (s > 0.) {
warning("debug_cmd: timeout, length > %1.3lf seconds\n", s);
module_event("debug_cmd", "aufileinfo", NULL, NULL,
"timeout length = %lf seconds", s);
}
else {
info("debug_cmd: timeout\n");
module_event("debug_cmd", "aufileinfo", NULL, NULL,
"timeout", s);
"length unknown");
}

mem_deref(st);
}


static void fileinfo_read_handler(struct auframe *af, void *arg)
{
struct fileinfo_st *st = arg;

if (!af || !arg)
return;

st->sampc += af->sampc;
}


static void fileinfo_err_handler(int err, const char *str, void *arg)
{
struct fileinfo_st *st = arg;
(void) str;

st->finished = err ? false : true;
tmr_start(&st->tmr, 0, fileinfo_timeout, st);
}


Expand Down Expand Up @@ -281,7 +247,6 @@ static int cmd_aufileinfo(struct re_printf *pf, void *arg)
conf_config()->audio.audio_path, file) < 0)
return ENOMEM;

/* prm->ptime == 0 means blocking mode for ausrc */
st = mem_zalloc(sizeof(*st), fileinfo_destruct);
if (!st) {
err = ENOMEM;
Expand All @@ -290,22 +255,17 @@ static int cmd_aufileinfo(struct re_printf *pf, void *arg)

err = ausrc_alloc(&st->ausrc, baresip_ausrcl(),
aumod,
&st->prm, path,
fileinfo_read_handler, fileinfo_err_handler, st);
&st->prm, path, NULL, NULL, st);
if (err) {
warning("debug_cmd: %s - ausrc %s does not support zero ptime "
"or reading source %s failed. (%m)\n",
warning("debug_cmd: %s - ausrc %s does not support empty read "
"handler or reading source %s failed. (%m)\n",
__func__, aumod, carg->prm, err);
goto out;
}

if (st->finished)
fileinfo_timeout(st);
else
tmr_start(&st->tmr, 5000, fileinfo_timeout, st);
print_fileinfo(st);
out:
if (err)
mem_deref(st);
mem_deref(st);

mem_deref(path);
return err;
Expand Down

0 comments on commit c5bc699

Please sign in to comment.