Mon Jun 27 16:51:02 2011

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

static void __reg_module (void)
static void __unreg_module (void)
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 struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "Wait For Silence" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = "8586c2a7d357cb591cc3a6607a8f62d1" , .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_DEFAULT, }
static char * app_noise = "WaitForNoise"
static char * app_silence = "WaitForSilence"
static struct ast_module_infoast_module_info = &__mod_info


Detailed Description

Wait for Silence

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

Philipp Skadorov <skadorov@yahoo.com>

Definition in file app_waitforsilence.c.


Function Documentation

static void __reg_module ( void   )  [static]

Definition at line 272 of file app_waitforsilence.c.

static void __unreg_module ( void   )  [static]

Definition at line 272 of file app_waitforsilence.c.

static int do_waiting ( struct ast_channel chan,
int  timereqd,
time_t  waitstart,
int  timeout,
int  wait_for_silence 
) [static]

Definition at line 125 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, LOG_WARNING, ast_channel::name, pbx_builtin_setvar_helper(), ast_channel::readformat, and THRESHOLD_SILENCE.

Referenced by waitfor_exec().

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

static int load_module ( void   )  [static]

Definition at line 263 of file app_waitforsilence.c.

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

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 }

static int unload_module ( void   )  [static]

Definition at line 254 of file app_waitforsilence.c.

References ast_unregister_application().

00255 {
00256    int res;
00257    res = ast_unregister_application(app_silence);
00258    res |= ast_unregister_application(app_noise);
00259 
00260    return res;
00261 }

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

Definition at line 205 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().

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 }

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

Definition at line 249 of file app_waitforsilence.c.

References waitfor_exec().

Referenced by load_module().

00250 {
00251    return waitfor_exec(chan, data, 0);
00252 }

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

Definition at line 244 of file app_waitforsilence.c.

References waitfor_exec().

Referenced by load_module().

00245 {
00246    return waitfor_exec(chan, data, 1);
00247 }


Variable Documentation

struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "Wait For Silence" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = "8586c2a7d357cb591cc3a6607a8f62d1" , .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_DEFAULT, } [static]

Definition at line 272 of file app_waitforsilence.c.

char* app_noise = "WaitForNoise" [static]

Definition at line 123 of file app_waitforsilence.c.

char* app_silence = "WaitForSilence" [static]

Definition at line 122 of file app_waitforsilence.c.

struct ast_module_info* ast_module_info = &__mod_info [static]

Definition at line 272 of file app_waitforsilence.c.


Generated on Mon Jun 27 16:51:02 2011 for Asterisk - The Open Source Telephony Project by  doxygen 1.4.7