Sat Apr 26 2014 22:01:55

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, 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 = AST_BUILDOPT_SUM, .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.

  • 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 276 of file app_waitforsilence.c.

static void __unreg_module ( void  ) [static]

Definition at line 276 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 129 of file app_waitforsilence.c.

References 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_copy(), AST_FORMAT_SLINEAR, AST_FRAME_VOICE, ast_frfree, ast_getformatname(), ast_log(), ast_read(), ast_set_read_format(), ast_set_read_format_by_id(), ast_verb, ast_waitfor(), f, ast_frame::frametype, ast_format::id, LOG_WARNING, pbx_builtin_setvar_helper(), and THRESHOLD_SILENCE.

Referenced by waitfor_exec().

                                                                                                                   {
   struct ast_frame *f = NULL;
   int dsptime = 0;
   struct ast_format rfmt;
   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;

   ast_format_copy(&rfmt, ast_channel_readformat(chan)); /* Set to linear mode */
   if ((res = ast_set_read_format_by_id(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.id && ast_set_read_format(chan, &rfmt)) {
      ast_log(LOG_WARNING, "Unable to restore format %s to channel '%s'\n", ast_getformatname(&rfmt), ast_channel_name(chan));
   }
   ast_dsp_free(sildet);
   return res;
}
static int unload_module ( void  ) [static]

Definition at line 258 of file app_waitforsilence.c.

References ast_unregister_application().

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_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 (ast_channel_state(chan) != 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,
const char *  data 
) [static]

Definition at line 253 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,
const char *  data 
) [static]

Definition at line 248 of file app_waitforsilence.c.

References waitfor_exec().

Referenced by load_module().

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

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 = AST_BUILDOPT_SUM, .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_DEFAULT, } [static]

Definition at line 276 of file app_waitforsilence.c.

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.

Definition at line 276 of file app_waitforsilence.c.