Skip to content

Commit

Permalink
ausrc, aufile, gst: replace duration by callback handler
Browse files Browse the repository at this point in the history
Retrieving the duration of an audio file may lead to long blocking
delay. E.g. gst_element_query_duration()
Now a callback handler can be called by the application only if
needed.
  • Loading branch information
cspiel1 committed Aug 21, 2024
1 parent 1c0c130 commit 1cf37bb
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 11 deletions.
3 changes: 2 additions & 1 deletion include/baresip.h
Original file line number Diff line number Diff line change
Expand Up @@ -565,14 +565,15 @@ int message_encode_dict(struct odict *od, struct account *acc,

struct ausrc;
struct ausrc_st;
typedef size_t (ausrc_duration_h)(struct ausrc_st *st);

/** Audio Source parameters */
struct ausrc_prm {
uint32_t srate; /**< Sampling rate in [Hz] */
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 */
ausrc_duration_h *durationh; /**< Handler returns duration in [ms] */
};

typedef void (ausrc_read_h)(struct auframe *af, void *arg);
Expand Down
17 changes: 15 additions & 2 deletions modules/aufile/aufile_src.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ struct ausrc_st {
struct aubuf *aubuf;
enum aufmt fmt; /**< Wav file sample format */
struct ausrc_prm prm; /**< Audio src parameter */
struct aufile_prm fprm;
uint32_t ptime;
size_t sampc;
RE_ATOMIC bool run;
Expand Down Expand Up @@ -189,6 +190,16 @@ static int read_file(struct ausrc_st *st)
}


static size_t aufile_duration_handler(struct ausrc_st *st)
{
if (!st)
return 0;


return aufile_get_length(st->aufile, &st->fprm);
}


int aufile_src_alloc(struct ausrc_st **stp, const struct ausrc *as,
struct ausrc_prm *prm, const char *dev,
ausrc_read_h *rh, ausrc_error_h *errh, void *arg)
Expand Down Expand Up @@ -231,7 +242,6 @@ 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)
goto out;
Expand Down Expand Up @@ -266,8 +276,11 @@ int aufile_src_alloc(struct ausrc_st **stp, const struct ausrc *as,
out:
if (err)
mem_deref(st);
else
else {
*stp = st;
st->fprm = fprm;
prm->durationh = aufile_duration_handler;
}

return err;
}
7 changes: 5 additions & 2 deletions modules/debug_cmd/debug_cmd.c
Original file line number Diff line number Diff line change
Expand Up @@ -185,9 +185,12 @@ static void fileinfo_destruct(void *arg)

static void print_fileinfo(struct fileinfo_st *st)
{
double s = ((float) st->prm.duration) / 1000;
size_t duration = 0;
if (st->prm.durationh)
duration = st->prm.durationh(st->ausrc);

if (st->prm.duration) {
if (duration) {
double s = ((float) duration) / 1000;
info("debug_cmd: length = %1.3lf seconds\n", s);
module_event("debug_cmd", "aufileinfo", NULL, NULL,
"length = %lf seconds", s);
Expand Down
22 changes: 16 additions & 6 deletions modules/gst/gst.c
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,21 @@ static void timeout(void *arg)
}


static size_t gst_duration_handler(struct ausrc_st *st)
{
if (!st)
return 0;

gst_element_get_state(st->pipeline, NULL, NULL, 500*1000*1000);
gint64 duration = 0;
gst_element_query_duration(st->pipeline,
GST_FORMAT_TIME,
&duration);

return duration / 1000000;
}


static int gst_alloc(struct ausrc_st **stp, const struct ausrc *as,
struct ausrc_prm *prm, const char *device,
ausrc_read_h *rh, ausrc_error_h *errh, void *arg)
Expand Down Expand Up @@ -487,12 +502,7 @@ static int gst_alloc(struct ausrc_st **stp, const struct ausrc *as,
mem_deref(st);
else {
*stp = st;
gst_element_get_state(st->pipeline, NULL, NULL, 500*1000*1000);
gint64 duration = 0;
gst_element_query_duration(st->pipeline,
GST_FORMAT_TIME,
&duration);
prm->duration = duration / 1000000;
prm->durationh = gst_duration_handler;
}

return err;
Expand Down

0 comments on commit 1cf37bb

Please sign in to comment.