diff --git a/include/baresip.h b/include/baresip.h index 57d07c1d43..cd7842e12c 100644 --- a/include/baresip.h +++ b/include/baresip.h @@ -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); diff --git a/modules/aufile/aufile_src.c b/modules/aufile/aufile_src.c index a0103e8bcf..92b6a0b8cb 100644 --- a/modules/aufile/aufile_src.c +++ b/modules/aufile/aufile_src.c @@ -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) { @@ -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); @@ -237,10 +229,15 @@ 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) + 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); @@ -253,7 +250,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; @@ -264,12 +261,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); diff --git a/modules/debug_cmd/debug_cmd.c b/modules/debug_cmd/debug_cmd.c index b55f2b755c..23d14244c6 100644 --- a/modules/debug_cmd/debug_cmd.c +++ b/modules/debug_cmd/debug_cmd.c @@ -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; }; @@ -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); } @@ -281,7 +247,7 @@ 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 */ + /* prm->ptime == 0 avoids start auf ausrc thread */ st = mem_zalloc(sizeof(*st), fileinfo_destruct); if (!st) { err = ENOMEM; @@ -290,22 +256,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;