]> git.ipfire.org Git - thirdparty/asterisk.git/commitdiff
app_waitforsilence.c: Use milliseconds to calculate timeout time
authorIgor Goncharovsky <igorg@iqtek.ru>
Thu, 4 Sep 2025 04:00:50 +0000 (10:00 +0600)
committerAsterisk Development Team <asteriskteam@digium.com>
Wed, 10 Sep 2025 19:55:24 +0000 (19:55 +0000)
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
(cherry picked from commit 2e95a334a59e835617f371424130eddd0957f995)

apps/app_waitforsilence.c

index 15d585162adc3c2f6b25ab1b5c8a859103bbedb8..68afdcf37510925312fd25d43a194e7fa6efdfb1 100644 (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);
        }