res_fax.c: crash on framehook with no dsp in fax detect
[asterisk/asterisk.git] / apps / app_waitforsilence.c
index 948c8c7..f792472 100644 (file)
@@ -4,7 +4,7 @@
  * Copyright (C) 1999 - 2005, Digium, Inc.
  *
  * WaitForSilence Application by David C. Troy <dave@popvox.com>
- * Version 1.00 2004-01-29
+ * Version 1.11 2006-06-29
  *
  * Mark Spencer <markster@digium.com>
  *
  *   - Waits for up to 'x' milliseconds of silence, 'y' times \n
  *   - WaitForSilence(500,2) will wait for 1/2 second of silence, twice \n
  *   - WaitForSilence(1000,1) will wait for 1 second of silence, once \n
+ *   - WaitForSilence(300,3,10) will wait for 300ms of silence, 3 times, and return after 10sec \n
  *
  * \author David C. Troy <dave@popvox.com>
  *
+ * \brief Wait For Noise
+ * The same as Wait For Silence but listenes noise on the chennel that is above \n
+ * the pre-configured silence threshold from dsp.conf
+ *
+ * \author Philipp Skadorov <skadorov@yahoo.com>
+ *
  * \ingroup applications
  */
 
-#include <stdlib.h>
-#include <stdio.h>
-#include <string.h>
-#include <unistd.h>
+/*** MODULEINFO
+       <support_level>extended</support_level>
+ ***/
 
 #include "asterisk.h"
 
 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
 
 #include "asterisk/file.h"
-#include "asterisk/logger.h"
 #include "asterisk/channel.h"
 #include "asterisk/pbx.h"
 #include "asterisk/dsp.h"
 #include "asterisk/module.h"
-#include "asterisk/options.h"
-
-static char *tdesc = "Wait For Silence";
-static char *app = "WaitForSilence";
-static char *synopsis = "Waits for a specified amount of silence";
-static char *descrip = 
-"  WaitForSilence(x[|y]) Wait for Silence: Waits for up to 'x' \n"
-"milliseconds of silence, 'y' times or 1 if omitted\n"
-"Set the channel variable WAITSTATUS with to one of these values:"
-"SILENCE - if silence of x ms was detected"
-"TIMEOUT - if silence of x ms was not detected."
-"Examples:\n"
-"  - WaitForSilence(500|2) will wait for 1/2 second of silence, twice\n"
-"  - WaitForSilence(1000) will wait for 1 second of silence, once\n";
-
-STANDARD_LOCAL_USER;
-
-LOCAL_USER_DECL;
-
-static int do_waiting(struct ast_channel *chan, int maxsilence) {
-
-       struct ast_frame *f;
-       int totalsilence = 0;
-       int dspsilence = 0;
-       int gotsilence = 0; 
-       static int silencethreshold = 64;
-       int rfmt = 0;
+
+/*** DOCUMENTATION
+       <application name="WaitForSilence" language="en_US">
+               <synopsis>
+                       Waits for a specified amount of silence.
+               </synopsis>
+               <syntax>
+                       <parameter name="silencerequired" required="true" />
+                       <parameter name="iterations">
+                               <para>If not specified, defaults to <literal>1</literal>.</para>
+                       </parameter>
+                       <parameter name="timeout">
+                               <para>Is specified only to avoid an infinite loop in cases where silence is never achieved.</para>
+                       </parameter>
+               </syntax>
+               <description>
+                       <para>Waits for up to <replaceable>silencerequired</replaceable> milliseconds of silence,
+                       <replaceable>iterations</replaceable> times. An optional <replaceable>timeout</replaceable>
+                       specified the number of seconds to return after, even if we do not receive the specified amount of silence.
+                       Use <replaceable>timeout</replaceable> with caution, as it may defeat the purpose of this application, which
+                       is to wait indefinitely until silence is detected on the line. This is particularly useful for reverse-911-type
+                       call broadcast applications where you need to wait for an answering machine to complete its spiel before
+                       playing a message.</para>
+                       <para>Typically you will want to include two or more calls to WaitForSilence when dealing with an answering
+                       machine; first waiting for the spiel to finish, then waiting for the beep, etc.</para>
+                       <para>Examples:</para>
+                       <para>WaitForSilence(500,2) will wait for 1/2 second of silence, twice</para>
+                       <para>WaitForSilence(1000) will wait for 1 second of silence, once</para>
+                       <para>WaitForSilence(300,3,10) will wait for 300ms silence, 3 times, and returns after 10 sec, even if silence
+                       is not detected</para>
+                       <para>Sets the channel variable <variable>WAITSTATUS</variable> to one of these values:</para>
+                       <variablelist>
+                               <variable name="WAITSTATUS">
+                                       <value name="SILENCE">
+                                               if exited with silence detected.
+                                       </value>
+                                       <value name="TIMEOUT">
+                                               if exited without silence detected after timeout.
+                                       </value>
+                               </variable>
+                       </variablelist>
+               </description>
+               <see-also>
+                       <ref type="application">WaitForNoise</ref>
+               </see-also>
+       </application>
+       <application name="WaitForNoise" language="en_US">
+               <synopsis>
+                       Waits for a specified amount of noise.
+               </synopsis>
+               <syntax>
+                       <parameter name="noiserequired" required="true" />
+                       <parameter name="iterations">
+                               <para>If not specified, defaults to <literal>1</literal>.</para>
+                       </parameter>
+                       <parameter name="timeout">
+                               <para>Is specified only to avoid an infinite loop in cases where silence is never achieved.</para>
+                       </parameter>
+               </syntax>
+               <description>
+                       <para>Waits for up to <replaceable>noiserequired</replaceable> milliseconds of noise,
+                       <replaceable>iterations</replaceable> times. An optional <replaceable>timeout</replaceable>
+                       specified the number of seconds to return after, even if we do not receive the specified amount of noise.
+                       Use <replaceable>timeout</replaceable> with caution, as it may defeat the purpose of this application, which
+                       is to wait indefinitely until noise is detected on the line.</para>
+               </description>
+               <see-also>
+                       <ref type="application">WaitForSilence</ref>
+               </see-also>
+       </application>
+ ***/
+
+static char *app_silence = "WaitForSilence";
+static char *app_noise = "WaitForNoise";
+
+static int do_waiting(struct ast_channel *chan, int timereqd, time_t waitstart, int timeout, int wait_for_silence) {
+       struct ast_frame *f = NULL;
+       int dsptime = 0;
+       struct ast_format rfmt;
        int res = 0;
        struct ast_dsp *sildet;  /* silence detector dsp */
-       time_t start, now;
-       time(&start);
+       time_t now;
 
-       rfmt = chan->readformat; /* Set to linear mode */
-       res = ast_set_read_format(chan, AST_FORMAT_SLINEAR);
-       if (res < 0) {
-               ast_log(LOG_WARNING, "Unable to set to linear mode, giving up\n");
+       /*Either silence or noise calc depending on wait_for_silence flag*/
+       int (*ast_dsp_func)(struct ast_dsp*, struct ast_frame*, int*) =
+                               wait_for_silence ? ast_dsp_silence : ast_dsp_noise;
+
+       ast_format_copy(&rfmt, ast_channel_readformat(chan)); /* Set to linear mode */
+       if ((res = ast_set_read_format_by_id(chan, AST_FORMAT_SLINEAR)) < 0) {
+               ast_log(LOG_WARNING, "Unable to set channel to linear mode, giving up\n");
                return -1;
        }
 
-       sildet = ast_dsp_new(); /* Create the silence detector */
-       if (!sildet) {
+       /* Create the silence detector */
+       if (!(sildet = ast_dsp_new())) {
                ast_log(LOG_WARNING, "Unable to create silence detector :(\n");
                return -1;
        }
-       ast_dsp_set_threshold(sildet, silencethreshold);
+       ast_dsp_set_threshold(sildet, ast_dsp_get_threshold_from_settings(THRESHOLD_SILENCE));
 
        /* Await silence... */
-       f = NULL;
-       for(;;) {
-               res = ast_waitfor(chan, 2000);
-               if (!res) {
-                       ast_log(LOG_WARNING, "One waitfor failed, trying another\n");
-                       /* Try one more time in case of masq */
-                       res = ast_waitfor(chan, 2000);
-                       if (!res) {
-                               ast_log(LOG_WARNING, "No audio available on %s??\n", chan->name);
-                               res = -1;
-                       }
-               }
+       for (;;) {
+               /* Start with no silence received */
+               dsptime = 0;
 
+               res = ast_waitfor(chan, timereqd);
+
+               /* Must have gotten a hangup; let's exit */
                if (res < 0) {
-                       f = NULL;
+                       pbx_builtin_setvar_helper(chan, "WAITSTATUS", "HANGUP");
                        break;
                }
-               f = ast_read(chan);
-               if (!f)
-                       break;
-               if (f->frametype == AST_FRAME_VOICE) {
-                       dspsilence = 0;
-                       ast_dsp_silence(sildet, f, &dspsilence);
-                       if (dspsilence) {
-                               totalsilence = dspsilence;
-                               time(&start);
-                       } else {
-                               totalsilence = 0;
-                       }
-
-                       if (totalsilence >= maxsilence) {
-                               if (option_verbose > 2)
-                                       ast_verbose(VERBOSE_PREFIX_3 "Exiting with %dms silence > %dms required\n", totalsilence, maxsilence);
-                               /* Ended happily with silence */
-                               gotsilence = 1;
-                               pbx_builtin_setvar_helper(chan, "WAITSTATUS", "SILENCE");
-                               ast_log(LOG_DEBUG, "WAITSTATUS was set to SILENCE\n");
-                               ast_frfree(f);
-                               break;
-                       } else if ( difftime(time(&now),start) >= maxsilence/1000 ) {
-                               pbx_builtin_setvar_helper(chan, "WAITSTATUS", "TIMEOUT");
-                               ast_log(LOG_DEBUG, "WAITSTATUS was set to TIMEOUT\n");
-                               ast_frfree(f);
+               
+               /* We waited and got no frame; sounds like digital silence or a muted digital channel */
+               if (res == 0) {
+                       dsptime = timereqd;
+               } else {
+                       /* Looks like we did get a frame, so let's check it out */
+                       if (!(f = ast_read(chan))) {
+                               pbx_builtin_setvar_helper(chan, "WAITSTATUS", "HANGUP");
                                break;
                        }
+                       if (f->frametype == AST_FRAME_VOICE) {
+                               ast_dsp_func(sildet, f, &dsptime);
+                       }
+                       ast_frfree(f);
+               }
+
+               ast_verb(6, "Got %dms %s < %dms required\n", dsptime, wait_for_silence ? "silence" : "noise", timereqd);
+
+               if (dsptime >= timereqd) {
+                       ast_verb(3, "Exiting with %dms %s >= %dms required\n", dsptime, wait_for_silence ? "silence" : "noise", timereqd);
+                       /* Ended happily with silence */
+                       res = 1;
+                       pbx_builtin_setvar_helper(chan, "WAITSTATUS", wait_for_silence ? "SILENCE" : "NOISE");
+                       ast_debug(1, "WAITSTATUS was set to %s\n", wait_for_silence ? "SILENCE" : "NOISE");
+                       break;
+               }
+
+               if (timeout && (difftime(time(&now), waitstart) >= timeout)) {
+                       pbx_builtin_setvar_helper(chan, "WAITSTATUS", "TIMEOUT");
+                       ast_debug(1, "WAITSTATUS was set to TIMEOUT\n");
+                       res = 0;
+                       break;
                }
-               ast_frfree(f);
        }
-       if (rfmt && ast_set_read_format(chan, rfmt)) {
-               ast_log(LOG_WARNING, "Unable to restore format %s to channel '%s'\n", ast_getformatname(rfmt), chan->name);
+
+
+       if (rfmt.id && ast_set_read_format(chan, &rfmt)) {
+               ast_log(LOG_WARNING, "Unable to restore format %s to channel '%s'\n", ast_getformatname(&rfmt), ast_channel_name(chan));
        }
        ast_dsp_free(sildet);
-       return gotsilence;
+       return res;
 }
 
-static int waitforsilence_exec(struct ast_channel *chan, void *data)
+static int waitfor_exec(struct ast_channel *chan, const char *data, int wait_for_silence)
 {
        int res = 1;
-       struct localuser *u;
-       int maxsilence = 1000;
+       int timereqd = 1000;
+       int timeout = 0;
        int iterations = 1, i;
+       time_t waitstart;
+       struct ast_silence_generator *silgen = NULL;
 
-       LOCAL_USER_ADD(u);
-       
-       res = ast_answer(chan); /* Answer the channel */
+       if (ast_channel_state(chan) != AST_STATE_UP) {
+               res = ast_answer(chan); /* Answer the channel */
+       }
 
-       if (!data || ((sscanf(data, "%d|%d", &maxsilence, &iterations) != 2) &&
-               (sscanf(data, "%d", &maxsilence) != 1))) {
-               ast_log(LOG_WARNING, "Using default value of 1000ms, 1 iteration\n");
+       if (!data || ( (sscanf(data, "%30d,%30d,%30d", &timereqd, &iterations, &timeout) != 3) &&
+               (sscanf(data, "%30d,%30d", &timereqd, &iterations) != 2) &&
+               (sscanf(data, "%30d", &timereqd) != 1) ) ) {
+               ast_log(LOG_WARNING, "Using default value of 1000ms, 1 iteration, no timeout\n");
        }
 
-       if (option_verbose > 2)
-               ast_verbose(VERBOSE_PREFIX_3 "Waiting %d time(s) for %d ms silence\n", iterations, maxsilence);
-       
+       ast_verb(3, "Waiting %d time(s) for %d ms silence with %d timeout\n", iterations, timereqd, timeout);
+
+       if (ast_opt_transmit_silence) {
+               silgen = ast_channel_start_silence_generator(chan);
+       }
+       time(&waitstart);
        res = 1;
        for (i=0; (i<iterations) && (res == 1); i++) {
-               res = do_waiting(chan, maxsilence);
+               res = do_waiting(chan, timereqd, waitstart, timeout, wait_for_silence);
+       }
+       if (silgen) {
+               ast_channel_stop_silence_generator(chan, silgen);
        }
-       LOCAL_USER_REMOVE(u);
+
+
        if (res > 0)
                res = 0;
        return res;
 }
 
-int unload_module(void)
+static int waitforsilence_exec(struct ast_channel *chan, const char *data)
 {
-       int res;
-
-       res = ast_unregister_application(app);
-       
-       STANDARD_HANGUP_LOCALUSERS;
-
-       return res;
+       return waitfor_exec(chan, data, 1);
 }
 
-int load_module(void)
+static int waitfornoise_exec(struct ast_channel *chan, const char *data)
 {
-       return ast_register_application(app, waitforsilence_exec, synopsis, descrip);
+       return waitfor_exec(chan, data, 0);
 }
 
-char *description(void)
+static int unload_module(void)
 {
-       return tdesc;
+       int res;
+       res = ast_unregister_application(app_silence);
+       res |= ast_unregister_application(app_noise);
+
+       return res;
 }
 
-int usecount(void)
+static int load_module(void)
 {
        int res;
-       STANDARD_USECOUNT(res);
+
+       res = ast_register_application_xml(app_silence, waitforsilence_exec);
+       res |= ast_register_application_xml(app_noise, waitfornoise_exec);
        return res;
 }
 
-char *key()
-{
-       return ASTERISK_GPL_KEY;
-}
+AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Wait For Silence");