Thu Apr 28 2011 16:56:54

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"
Include dependency graph for app_waitforsilence.c:

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, void *data, int wait_for_silence)
static int waitfornoise_exec (struct ast_channel *chan, void *data)
static int waitforsilence_exec (struct ast_channel *chan, void *data)

Variables

static struct ast_module_info
__MODULE_INFO_SECTION 
__mod_info = { __MODULE_INFO_GLOBALS .name = AST_MODULE, .flags = AST_MODFLAG_DEFAULT , .description = "Wait For Silence" , .key = ASTERISK_GPL_KEY , .buildopt_sum = AST_BUILDOPT_SUM, .load = load_module, .unload = unload_module, }
static char * app_noise = "WaitForNoise"
static char * app_silence = "WaitForSilence"
static struct ast_module_infoast_module_info = &__mod_info

Detailed Description

Wait for Silence

  • Waits for up to 'x' milliseconds of silence, 'y' times
  • WaitForSilence(500,2) will wait for 1/2 second of silence, twice
  • WaitForSilence(1000,1) will wait for 1 second of silence, once
  • WaitForSilence(300,3,10) will wait for 300ms of silence, 3 times, and return after 10sec
    .
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

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, ast_frame::frametype, LOG_WARNING, ast_channel::name, pbx_builtin_setvar_helper(), ast_channel::readformat, and THRESHOLD_SILENCE.

Referenced by waitfor_exec().

                                                                                                                   {
   struct ast_frame *f = NULL;
   int dsptime = 0;
   int rfmt = 0;
   int res = 0;
   struct ast_dsp *sildet;  /* silence detector dsp */
   time_t now;

   /*Either silence or noise calc depending on wait_for_silence flag*/
   int (*ast_dsp_func)(struct ast_dsp*, struct ast_frame*, int*) =
            wait_for_silence ? ast_dsp_silence : ast_dsp_noise;

   rfmt = chan->readformat; /* Set to linear mode */
   if ((res = ast_set_read_format(chan, AST_FORMAT_SLINEAR)) < 0) {
      ast_log(LOG_WARNING, "Unable to set channel to linear mode, giving up\n");
      return -1;
   }

   /* Create the silence detector */
   if (!(sildet = ast_dsp_new())) {
      ast_log(LOG_WARNING, "Unable to create silence detector :(\n");
      return -1;
   }
   ast_dsp_set_threshold(sildet, ast_dsp_get_threshold_from_settings(THRESHOLD_SILENCE));

   /* Await silence... */
   for (;;) {
      /* Start with no silence received */
      dsptime = 0;

      res = ast_waitfor(chan, timereqd);

      /* Must have gotten a hangup; let's exit */
      if (res < 0) {
         pbx_builtin_setvar_helper(chan, "WAITSTATUS", "HANGUP");
         break;
      }
      
      /* We waited and got no frame; sounds like digital silence or a muted digital channel */
      if (res == 0) {
         dsptime = timereqd;
      } else {
         /* Looks like we did get a frame, so let's check it out */
         if (!(f = ast_read(chan))) {
            pbx_builtin_setvar_helper(chan, "WAITSTATUS", "HANGUP");
            break;
         }
         if (f->frametype == AST_FRAME_VOICE) {
            ast_dsp_func(sildet, f, &dsptime);
         }
         ast_frfree(f);
      }

      ast_verb(6, "Got %dms %s < %dms required\n", dsptime, wait_for_silence ? "silence" : "noise", timereqd);

      if (dsptime >= timereqd) {
         ast_verb(3, "Exiting with %dms %s >= %dms required\n", dsptime, wait_for_silence ? "silence" : "noise", timereqd);
         /* Ended happily with silence */
         res = 1;
         pbx_builtin_setvar_helper(chan, "WAITSTATUS", wait_for_silence ? "SILENCE" : "NOISE");
         ast_debug(1, "WAITSTATUS was set to %s\n", wait_for_silence ? "SILENCE" : "NOISE");
         break;
      }

      if (timeout && (difftime(time(&now), waitstart) >= timeout)) {
         pbx_builtin_setvar_helper(chan, "WAITSTATUS", "TIMEOUT");
         ast_debug(1, "WAITSTATUS was set to TIMEOUT\n");
         res = 0;
         break;
      }
   }


   if (rfmt && ast_set_read_format(chan, rfmt)) {
      ast_log(LOG_WARNING, "Unable to restore format %s to channel '%s'\n", ast_getformatname(rfmt), chan->name);
   }
   ast_dsp_free(sildet);
   return res;
}
static int load_module ( void  ) [static]
static int unload_module ( void  ) [static]

Definition at line 254 of file app_waitforsilence.c.

References ast_unregister_application().

static int waitfor_exec ( struct ast_channel chan,
void *  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().

{
   int res = 1;
   int timereqd = 1000;
   int timeout = 0;
   int iterations = 1, i;
   time_t waitstart;
   struct ast_silence_generator *silgen = NULL;

   if (chan->_state != AST_STATE_UP) {
      res = ast_answer(chan); /* Answer the channel */
   }

   if (!data || ( (sscanf(data, "%30d,%30d,%30d", &timereqd, &iterations, &timeout) != 3) &&
      (sscanf(data, "%30d,%30d", &timereqd, &iterations) != 2) &&
      (sscanf(data, "%30d", &timereqd) != 1) ) ) {
      ast_log(LOG_WARNING, "Using default value of 1000ms, 1 iteration, no timeout\n");
   }

   ast_verb(3, "Waiting %d time(s) for %d ms silence with %d timeout\n", iterations, timereqd, timeout);

   if (ast_opt_transmit_silence) {
      silgen = ast_channel_start_silence_generator(chan);
   }
   time(&waitstart);
   res = 1;
   for (i=0; (i<iterations) && (res == 1); i++) {
      res = do_waiting(chan, timereqd, waitstart, timeout, wait_for_silence);
   }
   if (silgen) {
      ast_channel_stop_silence_generator(chan, silgen);
   }


   if (res > 0)
      res = 0;
   return res;
}
static int waitfornoise_exec ( struct ast_channel chan,
void *  data 
) [static]

Definition at line 249 of file app_waitforsilence.c.

References waitfor_exec().

Referenced by load_module().

{
   return waitfor_exec(chan, data, 0);
}
static int waitforsilence_exec ( struct ast_channel chan,
void *  data 
) [static]

Definition at line 244 of file app_waitforsilence.c.

References waitfor_exec().

Referenced by load_module().

{
   return waitfor_exec(chan, data, 1);
}

Variable Documentation

struct ast_module_info __MODULE_INFO_SECTION __mod_info = { __MODULE_INFO_GLOBALS .name = AST_MODULE, .flags = AST_MODFLAG_DEFAULT , .description = "Wait For Silence" , .key = ASTERISK_GPL_KEY , .buildopt_sum = AST_BUILDOPT_SUM, .load = load_module, .unload = unload_module, } [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.

Definition at line 272 of file app_waitforsilence.c.