Tue Nov 16 17:07:29 2010

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  * \ingroup applications
00033  */
00034 
00035 #include "asterisk.h"
00036 
00037 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 211528 $")
00038 
00039 #include <stdlib.h>
00040 #include <stdio.h>
00041 #include <string.h>
00042 #include <unistd.h>
00043 
00044 #include "asterisk/file.h"
00045 #include "asterisk/logger.h"
00046 #include "asterisk/channel.h"
00047 #include "asterisk/pbx.h"
00048 #include "asterisk/dsp.h"
00049 #include "asterisk/module.h"
00050 #include "asterisk/options.h"
00051 
00052 static char *app = "WaitForSilence";
00053 static char *synopsis = "Waits for a specified amount of silence";
00054 static char *descrip =
00055 "  WaitForSilence(silencerequired[|iterations][|timeout]) \n"
00056 "Wait for Silence: Waits for up to 'silencerequired' \n"
00057 "milliseconds of silence, 'iterations' times or once if omitted.\n"
00058 "An optional timeout specified the number of seconds to return\n"
00059 "after, even if we do not receive the specified amount of silence.\n"
00060 "Use 'timeout' with caution, as it may defeat the purpose of this\n"
00061 "application, which is to wait indefinitely until silence is detected\n"
00062 "on the line.  This is particularly useful for reverse-911-type\n"
00063 "call broadcast applications where you need to wait for an answering\n"
00064 "machine to complete its spiel before playing a message.\n"
00065 "The timeout parameter is specified only to avoid an infinite loop in\n"
00066 "cases where silence is never achieved.  Typically you will want to\n"
00067 "include two or more calls to WaitForSilence when dealing with an answering\n"
00068 "machine; first waiting for the spiel to finish, then waiting for the beep, etc.\n\n"
00069   "Examples:\n"
00070 "  - WaitForSilence(500|2) will wait for 1/2 second of silence, twice\n"
00071 "  - WaitForSilence(1000) will wait for 1 second of silence, once\n"
00072 "  - WaitForSilence(300|3|10) will wait for 300ms silence, 3 times,\n"
00073 "     and returns after 10 sec, even if silence is not detected\n\n"
00074 "Sets the channel variable WAITSTATUS with to one of these values:\n"
00075 "SILENCE - if exited with silence detected\n"
00076 "TIMEOUT - if exited without silence detected after timeout\n";
00077 
00078 static int do_waiting(struct ast_channel *chan, int silencereqd, time_t waitstart, int timeout) {
00079    struct ast_frame *f;
00080    int dspsilence = 0;
00081    static int silencethreshold = 128;
00082    int rfmt = 0;
00083    int res = 0;
00084    struct ast_dsp *sildet;  /* silence detector dsp */
00085    time_t now;
00086 
00087    rfmt = chan->readformat; /* Set to linear mode */
00088    res = ast_set_read_format(chan, AST_FORMAT_SLINEAR);
00089    if (res < 0) {
00090       ast_log(LOG_WARNING, "Unable to set channel to linear mode, giving up\n");
00091       return -1;
00092    }
00093 
00094    sildet = ast_dsp_new(); /* Create the silence detector */
00095    if (!sildet) {
00096       ast_log(LOG_WARNING, "Unable to create silence detector :(\n");
00097       return -1;
00098    }
00099    ast_dsp_set_threshold(sildet, silencethreshold);
00100 
00101    /* Await silence... */
00102    f = NULL;
00103    for(;;) {
00104       /* Start with no silence received */
00105       dspsilence = 0;
00106 
00107       res = ast_waitfor(chan, silencereqd);
00108 
00109       /* Must have gotten a hangup; let's exit */
00110       if (res < 0) {
00111          f = NULL;
00112          break;
00113       }
00114       
00115       /* We waited and got no frame; sounds like digital silence or a muted digital channel */
00116       if (res == 0) {
00117          dspsilence = silencereqd;
00118       } else {
00119          /* Looks like we did get a frame, so let's check it out */
00120          f = ast_read(chan);
00121          if (!f)
00122             break;
00123          if (f && f->frametype == AST_FRAME_VOICE) {
00124             ast_dsp_silence(sildet, f, &dspsilence);
00125          }
00126          if (f) {
00127             ast_frfree(f);
00128          }
00129       }
00130 
00131       if (option_verbose > 6)
00132          ast_verbose(VERBOSE_PREFIX_3 "Got %dms silence< %dms required\n", dspsilence, silencereqd);
00133 
00134       if (dspsilence >= silencereqd) {
00135          if (option_verbose > 2)
00136             ast_verbose(VERBOSE_PREFIX_3 "Exiting with %dms silence >= %dms required\n", dspsilence, silencereqd);
00137          /* Ended happily with silence */
00138          res = 1;
00139          pbx_builtin_setvar_helper(chan, "WAITSTATUS", "SILENCE");
00140          ast_log(LOG_DEBUG, "WAITSTATUS was set to SILENCE\n");
00141          break;
00142       }
00143 
00144       if ( timeout && (difftime(time(&now),waitstart) >= timeout) ) {
00145          pbx_builtin_setvar_helper(chan, "WAITSTATUS", "TIMEOUT");
00146          ast_log(LOG_DEBUG, "WAITSTATUS was set to TIMEOUT\n");
00147          res = 0;
00148          break;
00149       }
00150    }
00151 
00152 
00153    if (rfmt && ast_set_read_format(chan, rfmt)) {
00154       ast_log(LOG_WARNING, "Unable to restore format %s to channel '%s'\n", ast_getformatname(rfmt), chan->name);
00155    }
00156    ast_dsp_free(sildet);
00157    return res;
00158 }
00159 
00160 static int waitforsilence_exec(struct ast_channel *chan, void *data)
00161 {
00162    int res = 1;
00163    int silencereqd = 1000;
00164    int timeout = 0;
00165    int iterations = 1, i;
00166    time_t waitstart;
00167 
00168    res = ast_answer(chan); /* Answer the channel */
00169 
00170    if (!data || ( (sscanf(data, "%30d|%30d|%30d", &silencereqd, &iterations, &timeout) != 3) &&
00171       (sscanf(data, "%30d|%30d", &silencereqd, &iterations) != 2) &&
00172       (sscanf(data, "%30d", &silencereqd) != 1) ) ) {
00173       ast_log(LOG_WARNING, "Using default value of 1000ms, 1 iteration, no timeout\n");
00174    }
00175 
00176    if (option_verbose > 2)
00177       ast_verbose(VERBOSE_PREFIX_3 "Waiting %d time(s) for %d ms silence with %d timeout\n", iterations, silencereqd, timeout);
00178 
00179    time(&waitstart);
00180    res = 1;
00181    for (i=0; (i<iterations) && (res == 1); i++) {
00182       res = do_waiting(chan, silencereqd, waitstart, timeout);
00183    }
00184    if (res > 0)
00185       res = 0;
00186    return res;
00187 }
00188 
00189 
00190 static int unload_module(void)
00191 {
00192    int res;
00193 
00194    res = ast_unregister_application(app);
00195    
00196    ast_module_user_hangup_all();
00197 
00198    return res;
00199 }
00200 
00201 static int load_module(void)
00202 {
00203    return ast_register_application(app, waitforsilence_exec, synopsis, descrip);
00204 }
00205 
00206 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Wait For Silence");
00207 

Generated on Tue Nov 16 17:07:29 2010 for Asterisk - the Open Source PBX by  doxygen 1.4.7