app_waitforsilence.c: Use milliseconds to calculate timeout time

The functions WaitForNoise() and WaitForSilence() use the time()
functions to calculate elapsed time, which causes the timer to fire on
a whole second boundary, and the actual function execution time to fire
the timer may be 1 second less than expected. This fix replaces time()
with ast_tvnow().

Fixes: #1401
This commit is contained in:
Igor Goncharovsky
2025-09-04 10:00:50 +06:00
parent c291f67847
commit 2e95a334a5

View File

@@ -158,7 +158,7 @@ static const struct wait_type wait_for_noise = {
.func = ast_dsp_noise,
};
static int do_waiting(struct ast_channel *chan, int timereqd, time_t waitstart, int timeout, const struct wait_type *wait_for)
static int do_waiting(struct ast_channel *chan, int timereqd, struct timeval waitstart, int timeout, const struct wait_type *wait_for)
{
RAII_VAR(struct ast_format *, rfmt, NULL, ao2_cleanup);
int res;
@@ -216,7 +216,7 @@ static int do_waiting(struct ast_channel *chan, int timereqd, time_t waitstart,
break;
}
if (timeout && difftime(time(NULL), waitstart) >= timeout) {
if (timeout && ast_tvdiff_ms(ast_tvnow(), waitstart) >= timeout * 1000) {
pbx_builtin_setvar_helper(chan, "WAITSTATUS", "TIMEOUT");
ast_debug(1, "WAITSTATUS was set to TIMEOUT\n");
res = 0;
@@ -238,7 +238,7 @@ static int waitfor_exec(struct ast_channel *chan, const char *data, const struct
int timereqd = 1000;
int timeout = 0;
int iterations = 1, i;
time_t waitstart;
struct timeval waitstart;
char *parse;
struct ast_silence_generator *silgen = NULL;
@@ -284,7 +284,7 @@ static int waitfor_exec(struct ast_channel *chan, const char *data, const struct
silgen = ast_channel_start_silence_generator(chan);
}
time(&waitstart);
waitstart = ast_tvnow();
for (i = 0; i < iterations && res == 1; i++) {
res = do_waiting(chan, timereqd, waitstart, timeout, wait_for);
}