Tue Aug 20 16:34:47 2013

Asterisk developer's documentation


app_waitforsilence.c File Reference

Wait for Silence

More...

#include "asterisk.h"
#include "asterisk/file.h"
#include "asterisk/channel.h"
#include "asterisk/pbx.h"
#include "asterisk/dsp.h"
#include "asterisk/module.h"

Go to the source code of this file.

Functions

 AST_MODULE_INFO_STANDARD (ASTERISK_GPL_KEY,"Wait For Silence")
static int do_waiting (struct ast_channel *chan, int timereqd, time_t waitstart, int timeout, int wait_for_silence)
static int load_module (void)
static int unload_module (void)
static int waitfor_exec (struct ast_channel *chan, const char *data, int wait_for_silence)
static int waitfornoise_exec (struct ast_channel *chan, const char *data)
static int waitforsilence_exec (struct ast_channel *chan, const char *data)

Variables

static char * app_noise = "WaitForNoise"
static char * app_silence = "WaitForSilence"

Detailed Description

Wait for Silence

Author:
David C. Troy <dave@popvox.com>

Wait For Noise The same as Wait For Silence but listenes noise on the chennel that is above
the pre-configured silence threshold from dsp.conf

Author:
Philipp Skadorov <skadorov@yahoo.com>

Definition in file app_waitforsilence.c.


Function Documentation

AST_MODULE_INFO_STANDARD ( ASTERISK_GPL_KEY  ,
"Wait For Silence"   
)
static int do_waiting ( struct ast_channel chan,
int  timereqd,
time_t  waitstart,
int  timeout,
int  wait_for_silence 
) [static]

Definition at line 129 of file app_waitforsilence.c.

References ast_debug, ast_dsp_free(), ast_dsp_get_threshold_from_settings(), ast_dsp_new(), ast_dsp_noise(), ast_dsp_set_threshold(), ast_dsp_silence(), AST_FORMAT_SLINEAR, AST_FRAME_VOICE, ast_frfree, ast_getformatname(), ast_log(), ast_read(), ast_set_read_format(), ast_verb, ast_waitfor(), f, ast_frame::frametype, LOG_WARNING, pbx_builtin_setvar_helper(), ast_channel::readformat, and THRESHOLD_SILENCE.

Referenced by waitfor_exec().

00129                                                                                                                    {
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 }

static int load_module ( void   )  [static]

Definition at line 267 of file app_waitforsilence.c.

References ast_register_application_xml, waitfornoise_exec(), and waitforsilence_exec().

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 }

static int unload_module ( void   )  [static]

Definition at line 258 of file app_waitforsilence.c.

References ast_unregister_application().

00259 {
00260    int res;
00261    res = ast_unregister_application(app_silence);
00262    res |= ast_unregister_application(app_noise);
00263 
00264    return res;
00265 }

static int waitfor_exec ( struct ast_channel chan,
const char *  data,
int  wait_for_silence 
) [static]

Definition at line 209 of file app_waitforsilence.c.

References ast_channel::_state, ast_answer(), ast_channel_start_silence_generator(), ast_channel_stop_silence_generator(), ast_log(), ast_opt_transmit_silence, AST_STATE_UP, ast_verb, do_waiting(), and LOG_WARNING.

Referenced by waitfornoise_exec(), and waitforsilence_exec().

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 }

static int waitfornoise_exec ( struct ast_channel chan,
const char *  data 
) [static]

Definition at line 253 of file app_waitforsilence.c.

References waitfor_exec().

Referenced by load_module().

00254 {
00255    return waitfor_exec(chan, data, 0);
00256 }

static int waitforsilence_exec ( struct ast_channel chan,
const char *  data 
) [static]

Definition at line 248 of file app_waitforsilence.c.

References waitfor_exec().

Referenced by load_module().

00249 {
00250    return waitfor_exec(chan, data, 1);
00251 }


Variable Documentation

char* app_noise = "WaitForNoise" [static]

Definition at line 127 of file app_waitforsilence.c.

char* app_silence = "WaitForSilence" [static]

Definition at line 126 of file app_waitforsilence.c.


Generated on 20 Aug 2013 for Asterisk - The Open Source Telephony Project by  doxygen 1.6.1