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"
#include "asterisk/format_cache.h"

Include dependency graph for app_waitforsilence.c:

Go to the source code of this file.

Functions

 AST_MODULE_INFO_STANDARD_EXTENDED (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_EXTENDED ( 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 130 of file app_waitforsilence.c.

References ao2_bump, ao2_cleanup, ast_channel_name(), ast_channel_readformat(), 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_get_name(), ast_format_slin, AST_FRAME_VOICE, ast_frfree, ast_log, ast_read(), ast_set_read_format(), ast_verb, ast_waitfor(), f, ast_frame::frametype, LOG_WARNING, NULL, pbx_builtin_setvar_helper(), RAII_VAR, and THRESHOLD_SILENCE.

Referenced by waitfor_exec().

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

static int load_module ( void   )  [static]

Definition at line 268 of file app_waitforsilence.c.

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

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 }

static int unload_module ( void   )  [static]

Definition at line 259 of file app_waitforsilence.c.

References ast_unregister_application().

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

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

Definition at line 210 of file app_waitforsilence.c.

References 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(), LOG_WARNING, NULL, and timeout.

Referenced by waitfornoise_exec(), and waitforsilence_exec().

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 }

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

Definition at line 254 of file app_waitforsilence.c.

References waitfor_exec().

Referenced by load_module().

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

static int waitforsilence_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, 1);
00252 }


Variable Documentation

char* app_noise = "WaitForNoise" [static]

Definition at line 128 of file app_waitforsilence.c.

char* app_silence = "WaitForSilence" [static]

Definition at line 127 of file app_waitforsilence.c.


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