Thu Apr 28 2011 17:13:29

Asterisk developer's documentation


app_waitforsilence.c

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 1999 - 2005, Digium, Inc.
00005  *
00006  * WaitForSilence Application by David C. Troy <dave@popvox.com>
00007  * Version 1.11 2006-06-29
00008  *
00009  * Mark Spencer <markster@digium.com>
00010  *
00011  * See http://www.asterisk.org for more information about
00012  * the Asterisk project. Please do not directly contact
00013  * any of the maintainers of this project for assistance;
00014  * the project provides a web site, mailing lists and IRC
00015  * channels for your use.
00016  *
00017  * This program is free software, distributed under the terms of
00018  * the GNU General Public License Version 2. See the LICENSE file
00019  * at the top of the source tree.
00020  */
00021 
00022 /*! \file
00023  *
00024  * \brief Wait for Silence
00025  *   - Waits for up to 'x' milliseconds of silence, 'y' times \n
00026  *   - WaitForSilence(500,2) will wait for 1/2 second of silence, twice \n
00027  *   - WaitForSilence(1000,1) will wait for 1 second of silence, once \n
00028  *   - WaitForSilence(300,3,10) will wait for 300ms of silence, 3 times, and return after 10sec \n
00029  *
00030  * \author David C. Troy <dave@popvox.com>
00031  *
00032  * \brief Wait For Noise
00033  * The same as Wait For Silence but listenes noise on the chennel that is above \n
00034  * the pre-configured silence threshold from dsp.conf
00035  *
00036  * \author Philipp Skadorov <skadorov@yahoo.com>
00037  *
00038  * \ingroup applications
00039  */
00040 
00041 #include "asterisk.h"
00042 
00043 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 239713 $")
00044 
00045 #include "asterisk/file.h"
00046 #include "asterisk/channel.h"
00047 #include "asterisk/pbx.h"
00048 #include "asterisk/dsp.h"
00049 #include "asterisk/module.h"
00050 
00051 /*** DOCUMENTATION
00052    <application name="WaitForSilence" language="en_US">
00053       <synopsis>
00054          Waits for a specified amount of silence.
00055       </synopsis>
00056       <syntax>
00057          <parameter name="silencerequired" required="true" />
00058          <parameter name="iterations">
00059             <para>If not specified, defaults to <literal>1</literal>.</para>
00060          </parameter>
00061          <parameter name="timeout">
00062             <para>Is specified only to avoid an infinite loop in cases where silence is never achieved.</para>
00063          </parameter>
00064       </syntax>
00065       <description>
00066          <para>Waits for up to <replaceable>silencerequired</replaceable> milliseconds of silence,
00067          <replaceable>iterations</replaceable> times. An optional <replaceable>timeout</replaceable>
00068          specified the number of seconds to return after, even if we do not receive the specified amount of silence.
00069          Use <replaceable>timeout</replaceable> with caution, as it may defeat the purpose of this application, which
00070          is to wait indefinitely until silence is detected on the line. This is particularly useful for reverse-911-type
00071          call broadcast applications where you need to wait for an answering machine to complete its spiel before
00072          playing a message.</para>
00073          <para>Typically you will want to include two or more calls to WaitForSilence when dealing with an answering
00074          machine; first waiting for the spiel to finish, then waiting for the beep, etc.</para>
00075          <para>Examples:</para>
00076          <para>WaitForSilence(500,2) will wait for 1/2 second of silence, twice</para>
00077          <para>WaitForSilence(1000) will wait for 1 second of silence, once</para>
00078          <para>WaitForSilence(300,3,10) will wait for 300ms silence, 3 times, and returns after 10 sec, even if silence
00079          is not detected</para>
00080          <para>Sets the channel variable <variable>WAITSTATUS</variable> to one of these values:</para>
00081          <variablelist>
00082             <variable name="WAITSTATUS">
00083                <value name="SILENCE">
00084                   if exited with silence detected.
00085                </value>
00086                <value name="TIMEOUT">
00087                   if exited without silence detected after timeout.
00088                </value>
00089             </variable>
00090          </variablelist>
00091       </description>
00092       <see-also>
00093          <ref type="application">WaitForNoise</ref>
00094       </see-also>
00095    </application>
00096    <application name="WaitForNoise" language="en_US">
00097       <synopsis>
00098          Waits for a specified amount of noise.
00099       </synopsis>
00100       <syntax>
00101          <parameter name="noiserequired" required="true" />
00102          <parameter name="iterations">
00103             <para>If not specified, defaults to <literal>1</literal>.</para>
00104          </parameter>
00105          <parameter name="timeout">
00106             <para>Is specified only to avoid an infinite loop in cases where silence is never achieved.</para>
00107          </parameter>
00108       </syntax>
00109       <description>
00110          <para>Waits for up to <replaceable>noiserequired</replaceable> milliseconds of noise,
00111          <replaceable>iterations</replaceable> times. An optional <replaceable>timeout</replaceable>
00112          specified the number of seconds to return after, even if we do not receive the specified amount of noise.
00113          Use <replaceable>timeout</replaceable> with caution, as it may defeat the purpose of this application, which
00114          is to wait indefinitely until noise is detected on the line.</para>
00115       </description>
00116       <see-also>
00117          <ref type="application">WaitForSilence</ref>
00118       </see-also>
00119    </application>
00120  ***/
00121 
00122 static char *app_silence = "WaitForSilence";
00123 static char *app_noise = "WaitForNoise";
00124 
00125 static int do_waiting(struct ast_channel *chan, int timereqd, time_t waitstart, int timeout, int wait_for_silence) {
00126    struct ast_frame *f = NULL;
00127    int dsptime = 0;
00128    int rfmt = 0;
00129    int res = 0;
00130    struct ast_dsp *sildet;  /* silence detector dsp */
00131    time_t now;
00132 
00133    /*Either silence or noise calc depending on wait_for_silence flag*/
00134    int (*ast_dsp_func)(struct ast_dsp*, struct ast_frame*, int*) =
00135             wait_for_silence ? ast_dsp_silence : ast_dsp_noise;
00136 
00137    rfmt = chan->readformat; /* Set to linear mode */
00138    if ((res = ast_set_read_format(chan, AST_FORMAT_SLINEAR)) < 0) {
00139       ast_log(LOG_WARNING, "Unable to set channel to linear mode, giving up\n");
00140       return -1;
00141    }
00142 
00143    /* Create the silence detector */
00144    if (!(sildet = ast_dsp_new())) {
00145       ast_log(LOG_WARNING, "Unable to create silence detector :(\n");
00146       return -1;
00147    }
00148    ast_dsp_set_threshold(sildet, ast_dsp_get_threshold_from_settings(THRESHOLD_SILENCE));
00149 
00150    /* Await silence... */
00151    for (;;) {
00152       /* Start with no silence received */
00153       dsptime = 0;
00154 
00155       res = ast_waitfor(chan, timereqd);
00156 
00157       /* Must have gotten a hangup; let's exit */
00158       if (res < 0) {
00159          pbx_builtin_setvar_helper(chan, "WAITSTATUS", "HANGUP");
00160          break;
00161       }
00162       
00163       /* We waited and got no frame; sounds like digital silence or a muted digital channel */
00164       if (res == 0) {
00165          dsptime = timereqd;
00166       } else {
00167          /* Looks like we did get a frame, so let's check it out */
00168          if (!(f = ast_read(chan))) {
00169             pbx_builtin_setvar_helper(chan, "WAITSTATUS", "HANGUP");
00170             break;
00171          }
00172          if (f->frametype == AST_FRAME_VOICE) {
00173             ast_dsp_func(sildet, f, &dsptime);
00174          }
00175          ast_frfree(f);
00176       }
00177 
00178       ast_verb(6, "Got %dms %s < %dms required\n", dsptime, wait_for_silence ? "silence" : "noise", timereqd);
00179 
00180       if (dsptime >= timereqd) {
00181          ast_verb(3, "Exiting with %dms %s >= %dms required\n", dsptime, wait_for_silence ? "silence" : "noise", timereqd);
00182          /* Ended happily with silence */
00183          res = 1;
00184          pbx_builtin_setvar_helper(chan, "WAITSTATUS", wait_for_silence ? "SILENCE" : "NOISE");
00185          ast_debug(1, "WAITSTATUS was set to %s\n", wait_for_silence ? "SILENCE" : "NOISE");
00186          break;
00187       }
00188 
00189       if (timeout && (difftime(time(&now), waitstart) >= timeout)) {
00190          pbx_builtin_setvar_helper(chan, "WAITSTATUS", "TIMEOUT");
00191          ast_debug(1, "WAITSTATUS was set to TIMEOUT\n");
00192          res = 0;
00193          break;
00194       }
00195    }
00196 
00197 
00198    if (rfmt && ast_set_read_format(chan, rfmt)) {
00199       ast_log(LOG_WARNING, "Unable to restore format %s to channel '%s'\n", ast_getformatname(rfmt), chan->name);
00200    }
00201    ast_dsp_free(sildet);
00202    return res;
00203 }
00204 
00205 static int waitfor_exec(struct ast_channel *chan, void *data, int wait_for_silence)
00206 {
00207    int res = 1;
00208    int timereqd = 1000;
00209    int timeout = 0;
00210    int iterations = 1, i;
00211    time_t waitstart;
00212    struct ast_silence_generator *silgen = NULL;
00213 
00214    if (chan->_state != AST_STATE_UP) {
00215       res = ast_answer(chan); /* Answer the channel */
00216    }
00217 
00218    if (!data || ( (sscanf(data, "%30d,%30d,%30d", &timereqd, &iterations, &timeout) != 3) &&
00219       (sscanf(data, "%30d,%30d", &timereqd, &iterations) != 2) &&
00220       (sscanf(data, "%30d", &timereqd) != 1) ) ) {
00221       ast_log(LOG_WARNING, "Using default value of 1000ms, 1 iteration, no timeout\n");
00222    }
00223 
00224    ast_verb(3, "Waiting %d time(s) for %d ms silence with %d timeout\n", iterations, timereqd, timeout);
00225 
00226    if (ast_opt_transmit_silence) {
00227       silgen = ast_channel_start_silence_generator(chan);
00228    }
00229    time(&waitstart);
00230    res = 1;
00231    for (i=0; (i<iterations) && (res == 1); i++) {
00232       res = do_waiting(chan, timereqd, waitstart, timeout, wait_for_silence);
00233    }
00234    if (silgen) {
00235       ast_channel_stop_silence_generator(chan, silgen);
00236    }
00237 
00238 
00239    if (res > 0)
00240       res = 0;
00241    return res;
00242 }
00243 
00244 static int waitforsilence_exec(struct ast_channel *chan, void *data)
00245 {
00246    return waitfor_exec(chan, data, 1);
00247 }
00248 
00249 static int waitfornoise_exec(struct ast_channel *chan, void *data)
00250 {
00251    return waitfor_exec(chan, data, 0);
00252 }
00253 
00254 static int unload_module(void)
00255 {
00256    int res;
00257    res = ast_unregister_application(app_silence);
00258    res |= ast_unregister_application(app_noise);
00259 
00260    return res;
00261 }
00262 
00263 static int load_module(void)
00264 {
00265    int res;
00266 
00267    res = ast_register_application_xml(app_silence, waitforsilence_exec);
00268    res |= ast_register_application_xml(app_noise, waitfornoise_exec);
00269    return res;
00270 }
00271 
00272 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Wait For Silence");
00273