Mon Oct 8 12:38:57 2012

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 /*** MODULEINFO
00042    <support_level>extended</support_level>
00043  ***/
00044 
00045 #include "asterisk.h"
00046 
00047 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 328209 $")
00048 
00049 #include "asterisk/file.h"
00050 #include "asterisk/channel.h"
00051 #include "asterisk/pbx.h"
00052 #include "asterisk/dsp.h"
00053 #include "asterisk/module.h"
00054 
00055 /*** DOCUMENTATION
00056    <application name="WaitForSilence" language="en_US">
00057       <synopsis>
00058          Waits for a specified amount of silence.
00059       </synopsis>
00060       <syntax>
00061          <parameter name="silencerequired" required="true" />
00062          <parameter name="iterations">
00063             <para>If not specified, defaults to <literal>1</literal>.</para>
00064          </parameter>
00065          <parameter name="timeout">
00066             <para>Is specified only to avoid an infinite loop in cases where silence is never achieved.</para>
00067          </parameter>
00068       </syntax>
00069       <description>
00070          <para>Waits for up to <replaceable>silencerequired</replaceable> milliseconds of silence,
00071          <replaceable>iterations</replaceable> times. An optional <replaceable>timeout</replaceable>
00072          specified the number of seconds to return after, even if we do not receive the specified amount of silence.
00073          Use <replaceable>timeout</replaceable> with caution, as it may defeat the purpose of this application, which
00074          is to wait indefinitely until silence is detected on the line. This is particularly useful for reverse-911-type
00075          call broadcast applications where you need to wait for an answering machine to complete its spiel before
00076          playing a message.</para>
00077          <para>Typically you will want to include two or more calls to WaitForSilence when dealing with an answering
00078          machine; first waiting for the spiel to finish, then waiting for the beep, etc.</para>
00079          <para>Examples:</para>
00080          <para>WaitForSilence(500,2) will wait for 1/2 second of silence, twice</para>
00081          <para>WaitForSilence(1000) will wait for 1 second of silence, once</para>
00082          <para>WaitForSilence(300,3,10) will wait for 300ms silence, 3 times, and returns after 10 sec, even if silence
00083          is not detected</para>
00084          <para>Sets the channel variable <variable>WAITSTATUS</variable> to one of these values:</para>
00085          <variablelist>
00086             <variable name="WAITSTATUS">
00087                <value name="SILENCE">
00088                   if exited with silence detected.
00089                </value>
00090                <value name="TIMEOUT">
00091                   if exited without silence detected after timeout.
00092                </value>
00093             </variable>
00094          </variablelist>
00095       </description>
00096       <see-also>
00097          <ref type="application">WaitForNoise</ref>
00098       </see-also>
00099    </application>
00100    <application name="WaitForNoise" language="en_US">
00101       <synopsis>
00102          Waits for a specified amount of noise.
00103       </synopsis>
00104       <syntax>
00105          <parameter name="noiserequired" required="true" />
00106          <parameter name="iterations">
00107             <para>If not specified, defaults to <literal>1</literal>.</para>
00108          </parameter>
00109          <parameter name="timeout">
00110             <para>Is specified only to avoid an infinite loop in cases where silence is never achieved.</para>
00111          </parameter>
00112       </syntax>
00113       <description>
00114          <para>Waits for up to <replaceable>noiserequired</replaceable> milliseconds of noise,
00115          <replaceable>iterations</replaceable> times. An optional <replaceable>timeout</replaceable>
00116          specified the number of seconds to return after, even if we do not receive the specified amount of noise.
00117          Use <replaceable>timeout</replaceable> with caution, as it may defeat the purpose of this application, which
00118          is to wait indefinitely until noise is detected on the line.</para>
00119       </description>
00120       <see-also>
00121          <ref type="application">WaitForSilence</ref>
00122       </see-also>
00123    </application>
00124  ***/
00125 
00126 static char *app_silence = "WaitForSilence";
00127 static char *app_noise = "WaitForNoise";
00128 
00129 static int do_waiting(struct ast_channel *chan, int timereqd, time_t waitstart, int timeout, int wait_for_silence) {
00130    struct ast_frame *f = NULL;
00131    int dsptime = 0;
00132    int rfmt = 0;
00133    int res = 0;
00134    struct ast_dsp *sildet;  /* silence detector dsp */
00135    time_t now;
00136 
00137    /*Either silence or noise calc depending on wait_for_silence flag*/
00138    int (*ast_dsp_func)(struct ast_dsp*, struct ast_frame*, int*) =
00139             wait_for_silence ? ast_dsp_silence : ast_dsp_noise;
00140 
00141    rfmt = chan->readformat; /* Set to linear mode */
00142    if ((res = ast_set_read_format(chan, AST_FORMAT_SLINEAR)) < 0) {
00143       ast_log(LOG_WARNING, "Unable to set channel to linear mode, giving up\n");
00144       return -1;
00145    }
00146 
00147    /* Create the silence detector */
00148    if (!(sildet = ast_dsp_new())) {
00149       ast_log(LOG_WARNING, "Unable to create silence detector :(\n");
00150       return -1;
00151    }
00152    ast_dsp_set_threshold(sildet, ast_dsp_get_threshold_from_settings(THRESHOLD_SILENCE));
00153 
00154    /* Await silence... */
00155    for (;;) {
00156       /* Start with no silence received */
00157       dsptime = 0;
00158 
00159       res = ast_waitfor(chan, timereqd);
00160 
00161       /* Must have gotten a hangup; let's exit */
00162       if (res < 0) {
00163          pbx_builtin_setvar_helper(chan, "WAITSTATUS", "HANGUP");
00164          break;
00165       }
00166       
00167       /* We waited and got no frame; sounds like digital silence or a muted digital channel */
00168       if (res == 0) {
00169          dsptime = timereqd;
00170       } else {
00171          /* Looks like we did get a frame, so let's check it out */
00172          if (!(f = ast_read(chan))) {
00173             pbx_builtin_setvar_helper(chan, "WAITSTATUS", "HANGUP");
00174             break;
00175          }
00176          if (f->frametype == AST_FRAME_VOICE) {
00177             ast_dsp_func(sildet, f, &dsptime);
00178          }
00179          ast_frfree(f);
00180       }
00181 
00182       ast_verb(6, "Got %dms %s < %dms required\n", dsptime, wait_for_silence ? "silence" : "noise", timereqd);
00183 
00184       if (dsptime >= timereqd) {
00185          ast_verb(3, "Exiting with %dms %s >= %dms required\n", dsptime, wait_for_silence ? "silence" : "noise", timereqd);
00186          /* Ended happily with silence */
00187          res = 1;
00188          pbx_builtin_setvar_helper(chan, "WAITSTATUS", wait_for_silence ? "SILENCE" : "NOISE");
00189          ast_debug(1, "WAITSTATUS was set to %s\n", wait_for_silence ? "SILENCE" : "NOISE");
00190          break;
00191       }
00192 
00193       if (timeout && (difftime(time(&now), waitstart) >= timeout)) {
00194          pbx_builtin_setvar_helper(chan, "WAITSTATUS", "TIMEOUT");
00195          ast_debug(1, "WAITSTATUS was set to TIMEOUT\n");
00196          res = 0;
00197          break;
00198       }
00199    }
00200 
00201 
00202    if (rfmt && ast_set_read_format(chan, rfmt)) {
00203       ast_log(LOG_WARNING, "Unable to restore format %s to channel '%s'\n", ast_getformatname(rfmt), chan->name);
00204    }
00205    ast_dsp_free(sildet);
00206    return res;
00207 }
00208 
00209 static int waitfor_exec(struct ast_channel *chan, const char *data, int wait_for_silence)
00210 {
00211    int res = 1;
00212    int timereqd = 1000;
00213    int timeout = 0;
00214    int iterations = 1, i;
00215    time_t waitstart;
00216    struct ast_silence_generator *silgen = NULL;
00217 
00218    if (chan->_state != AST_STATE_UP) {
00219       res = ast_answer(chan); /* Answer the channel */
00220    }
00221 
00222    if (!data || ( (sscanf(data, "%30d,%30d,%30d", &timereqd, &iterations, &timeout) != 3) &&
00223       (sscanf(data, "%30d,%30d", &timereqd, &iterations) != 2) &&
00224       (sscanf(data, "%30d", &timereqd) != 1) ) ) {
00225       ast_log(LOG_WARNING, "Using default value of 1000ms, 1 iteration, no timeout\n");
00226    }
00227 
00228    ast_verb(3, "Waiting %d time(s) for %d ms silence with %d timeout\n", iterations, timereqd, timeout);
00229 
00230    if (ast_opt_transmit_silence) {
00231       silgen = ast_channel_start_silence_generator(chan);
00232    }
00233    time(&waitstart);
00234    res = 1;
00235    for (i=0; (i<iterations) && (res == 1); i++) {
00236       res = do_waiting(chan, timereqd, waitstart, timeout, wait_for_silence);
00237    }
00238    if (silgen) {
00239       ast_channel_stop_silence_generator(chan, silgen);
00240    }
00241 
00242 
00243    if (res > 0)
00244       res = 0;
00245    return res;
00246 }
00247 
00248 static int waitforsilence_exec(struct ast_channel *chan, const char *data)
00249 {
00250    return waitfor_exec(chan, data, 1);
00251 }
00252 
00253 static int waitfornoise_exec(struct ast_channel *chan, const char *data)
00254 {
00255    return waitfor_exec(chan, data, 0);
00256 }
00257 
00258 static int unload_module(void)
00259 {
00260    int res;
00261    res = ast_unregister_application(app_silence);
00262    res |= ast_unregister_application(app_noise);
00263 
00264    return res;
00265 }
00266 
00267 static int load_module(void)
00268 {
00269    int res;
00270 
00271    res = ast_register_application_xml(app_silence, waitforsilence_exec);
00272    res |= ast_register_application_xml(app_noise, waitfornoise_exec);
00273    return res;
00274 }
00275 
00276 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Wait For Silence");
00277 

Generated on Mon Oct 8 12:38:57 2012 for Asterisk - The Open Source Telephony Project by  doxygen 1.4.7