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

Generated on Thu Apr 16 06:27:13 2015 for Asterisk - The Open Source Telephony Project by  doxygen 1.5.6