Asterisk - The Open Source Telephony Project  GIT-master-a24979a
Data Structures | Functions | Variables
app_waitforsilence.c File Reference

Wait for Silence. More...

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

Data Structures

struct  wait_type
 

Functions

 AST_MODULE_INFO_STANDARD_EXTENDED (ASTERISK_GPL_KEY, "Wait For Silence/Noise")
 
static int do_waiting (struct ast_channel *chan, int timereqd, time_t waitstart, int timeout, const struct wait_type *wait_for)
 
static int load_module (void)
 
static int unload_module (void)
 
static int waitfor_exec (struct ast_channel *chan, const char *data, const struct wait_type *wait_for)
 
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"
 
static const struct wait_type wait_for_noise
 
static const struct wait_type wait_for_silence
 

Detailed Description

Wait for Silence.

Author
Philipp Skadorov skado.nosp@m.rov@.nosp@m.yahoo.nosp@m..com

Definition in file app_waitforsilence.c.

Function Documentation

◆ AST_MODULE_INFO_STANDARD_EXTENDED()

AST_MODULE_INFO_STANDARD_EXTENDED ( ASTERISK_GPL_KEY  ,
"Wait For Silence/Noise"   
)

◆ do_waiting()

static int do_waiting ( struct ast_channel chan,
int  timereqd,
time_t  waitstart,
int  timeout,
const struct wait_type wait_for 
)
static

Definition at line 155 of file app_waitforsilence.c.

156 {
157  RAII_VAR(struct ast_format *, rfmt, NULL, ao2_cleanup);
158  int res;
159  struct ast_dsp *sildet;
160 
161  rfmt = ao2_bump(ast_channel_readformat(chan));
162  if ((res = ast_set_read_format(chan, ast_format_slin)) < 0) {
163  ast_log(LOG_WARNING, "Unable to set channel to linear mode, giving up\n");
164  return -1;
165  }
166 
167  /* Create the silence detector */
168  if (!(sildet = ast_dsp_new())) {
169  ast_log(LOG_WARNING, "Unable to create silence detector\n");
170  return -1;
171  }
173 
174  for (;;) {
175  int dsptime = 0;
176 
177  res = ast_waitfor(chan, timereqd);
178 
179  /* Must have gotten a hangup; let's exit */
180  if (res < 0) {
181  pbx_builtin_setvar_helper(chan, "WAITSTATUS", "HANGUP");
182  break;
183  }
184 
185  /* We waited and got no frame; sounds like digital silence or a muted digital channel */
186  if (res == 0) {
187  if (wait_for->stop_on_frame_timeout) {
188  dsptime = timereqd;
189  }
190  } else {
191  /* Looks like we did get a frame, so let's check it out */
192  struct ast_frame *f = ast_read(chan);
193  if (!f) {
194  pbx_builtin_setvar_helper(chan, "WAITSTATUS", "HANGUP");
195  break;
196  }
197  if (f->frametype == AST_FRAME_VOICE) {
198  wait_for->func(sildet, f, &dsptime);
199  }
200  ast_frfree(f);
201  }
202 
203  ast_debug(1, "Got %dms of %s < %dms required\n", dsptime, wait_for->name, timereqd);
204 
205  if (dsptime >= timereqd) {
206  ast_verb(3, "Exiting with %dms of %s >= %dms required\n", dsptime, wait_for->name, timereqd);
207  pbx_builtin_setvar_helper(chan, "WAITSTATUS", wait_for->status);
208  ast_debug(1, "WAITSTATUS was set to %s\n", wait_for->status);
209  res = 1;
210  break;
211  }
212 
213  if (timeout && difftime(time(NULL), waitstart) >= timeout) {
214  pbx_builtin_setvar_helper(chan, "WAITSTATUS", "TIMEOUT");
215  ast_debug(1, "WAITSTATUS was set to TIMEOUT\n");
216  res = 0;
217  break;
218  }
219  }
220 
221  if (rfmt && ast_set_read_format(chan, rfmt)) {
222  ast_log(LOG_WARNING, "Unable to restore format %s to channel '%s'\n", ast_format_get_name(rfmt), ast_channel_name(chan));
223  }
224 
225  ast_dsp_free(sildet);
226  return res;
227 }
#define ast_log
Definition: astobj2.c:42
#define ao2_cleanup(obj)
Definition: astobj2.h:1934
#define ao2_bump(obj)
Bump refcount on an AO2 object by one, returning the object.
Definition: astobj2.h:480
int ast_waitfor(struct ast_channel *chan, int ms)
Wait for input on a channel.
Definition: channel.c:3163
const char * ast_channel_name(const struct ast_channel *chan)
struct ast_frame * ast_read(struct ast_channel *chan)
Reads a frame.
Definition: channel.c:4292
int ast_set_read_format(struct ast_channel *chan, struct ast_format *format)
Sets read format on channel chan.
Definition: channel.c:5839
struct ast_format * ast_channel_readformat(struct ast_channel *chan)
void ast_dsp_set_threshold(struct ast_dsp *dsp, int threshold)
Set the minimum average magnitude threshold to determine talking by the DSP.
Definition: dsp.c:1778
void ast_dsp_free(struct ast_dsp *dsp)
Definition: dsp.c:1773
struct ast_dsp * ast_dsp_new(void)
Allocates a new dsp, assumes 8khz for internal sample rate.
Definition: dsp.c:1748
@ THRESHOLD_SILENCE
Definition: dsp.h:73
int ast_dsp_get_threshold_from_settings(enum threshold which)
Get silence threshold from dsp.conf.
Definition: dsp.c:1999
const char * ast_format_get_name(const struct ast_format *format)
Get the name associated with a format.
Definition: format.c:334
struct ast_format * ast_format_slin
Built-in cached signed linear 8kHz format.
Definition: format_cache.c:41
#define ast_frfree(fr)
@ AST_FRAME_VOICE
#define ast_debug(level,...)
Log a DEBUG message.
#define ast_verb(level,...)
#define LOG_WARNING
int pbx_builtin_setvar_helper(struct ast_channel *chan, const char *name, const char *value)
Add a variable to the channel variable stack, removing the most recently set value for the same name.
#define NULL
Definition: resample.c:96
Definition: dsp.c:407
Definition of a media format.
Definition: format.c:43
Data structure associated with a single frame of data.
enum ast_frame_type frametype
int(* func)(struct ast_dsp *, struct ast_frame *, int *)
const char * name
const char * status
#define RAII_VAR(vartype, varname, initval, dtor)
Declare a variable that will call a destructor function when it goes out of scope.
Definition: utils.h:936

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_set_threshold(), ast_format_get_name(), ast_format_slin, AST_FRAME_VOICE, ast_frfree, ast_log, ast_read(), ast_set_read_format(), ast_verb, ast_waitfor(), ast_frame::frametype, wait_type::func, LOG_WARNING, wait_type::name, NULL, pbx_builtin_setvar_helper(), RAII_VAR, wait_type::status, wait_type::stop_on_frame_timeout, and THRESHOLD_SILENCE.

Referenced by waitfor_exec().

◆ load_module()

static int load_module ( void  )
static

Definition at line 312 of file app_waitforsilence.c.

313 {
314  int res;
315 
318  return res;
319 }
static char * app_silence
static int waitfornoise_exec(struct ast_channel *chan, const char *data)
static int waitforsilence_exec(struct ast_channel *chan, const char *data)
static char * app_noise
#define ast_register_application_xml(app, execute)
Register an application using XML documentation.
Definition: module.h:626

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

◆ unload_module()

static int unload_module ( void  )
static

Definition at line 303 of file app_waitforsilence.c.

304 {
305  int res;
308 
309  return res;
310 }
int ast_unregister_application(const char *app)
Unregister an application.
Definition: pbx_app.c:392

References app_noise, app_silence, and ast_unregister_application().

◆ waitfor_exec()

static int waitfor_exec ( struct ast_channel chan,
const char *  data,
const struct wait_type wait_for 
)
static

Definition at line 229 of file app_waitforsilence.c.

230 {
231  int res = 1;
232  int timereqd = 1000;
233  int timeout = 0;
234  int iterations = 1, i;
235  time_t waitstart;
236  char *parse;
237  struct ast_silence_generator *silgen = NULL;
238 
240  AST_APP_ARG(timereqd);
241  AST_APP_ARG(iterations);
242  AST_APP_ARG(timeout);
243  );
244 
245  parse = ast_strdupa(data);
247 
248  if (!ast_strlen_zero(args.timereqd)) {
249  if (sscanf(args.timereqd, "%30d", &timereqd) != 1 || timereqd < 0) {
250  ast_log(LOG_ERROR, "Argument '%srequired' must be an integer greater than or equal to zero.\n",
251  wait_for->name);
252  return -1;
253  }
254  }
255 
256  if (!ast_strlen_zero(args.iterations)) {
257  if (sscanf(args.iterations, "%30d", &iterations) != 1 || iterations < 1) {
258  ast_log(LOG_ERROR, "Argument 'iterations' must be an integer greater than 0.\n");
259  return -1;
260  }
261  }
262 
263  if (!ast_strlen_zero(args.timeout)) {
264  if (sscanf(args.timeout, "%30d", &timeout) != 1 || timeout < 0) {
265  ast_log(LOG_ERROR, "Argument 'timeout' must be an integer greater than or equal to zero.\n");
266  return -1;
267  }
268  }
269 
270  if (ast_channel_state(chan) != AST_STATE_UP) {
271  ast_answer(chan); /* Answer the channel */
272  }
273 
274  ast_verb(3, "Waiting %d time(s) for %dms of %s with %ds timeout\n",
275  iterations, timereqd, wait_for->name, timeout);
276 
279  }
280 
281  time(&waitstart);
282  for (i = 0; i < iterations && res == 1; i++) {
283  res = do_waiting(chan, timereqd, waitstart, timeout, wait_for);
284  }
285 
286  if (silgen) {
288  }
289 
290  return res > 0 ? 0 : res;
291 }
static int do_waiting(struct ast_channel *chan, int timereqd, time_t waitstart, int timeout, const struct wait_type *wait_for)
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition: astmm.h:298
static void parse(struct mgcp_request *req)
Definition: chan_mgcp.c:1844
struct ast_silence_generator * ast_channel_start_silence_generator(struct ast_channel *chan)
Starts a silence generator on the given channel.
Definition: channel.c:8247
void ast_channel_stop_silence_generator(struct ast_channel *chan, struct ast_silence_generator *state)
Stops a previously-started silence generator on the given channel.
Definition: channel.c:8293
int ast_answer(struct ast_channel *chan)
Answer a channel.
Definition: channel.c:2806
ast_channel_state
ast_channel states
Definition: channelstate.h:35
@ AST_STATE_UP
Definition: channelstate.h:42
#define AST_APP_ARG(name)
Define an application argument.
#define AST_DECLARE_APP_ARGS(name, arglist)
Declare a structure to hold an application's arguments.
#define AST_STANDARD_APP_ARGS(args, parse)
Performs the 'standard' argument separation process for an application.
#define LOG_ERROR
#define ast_opt_transmit_silence
Definition: options.h:124
static force_inline int attribute_pure ast_strlen_zero(const char *s)
Definition: strings.h:65
const char * args

References args, ast_answer(), AST_APP_ARG, ast_channel_start_silence_generator(), ast_channel_stop_silence_generator(), AST_DECLARE_APP_ARGS, ast_log, ast_opt_transmit_silence, AST_STANDARD_APP_ARGS, AST_STATE_UP, ast_strdupa, ast_strlen_zero(), ast_verb, do_waiting(), LOG_ERROR, wait_type::name, NULL, and parse().

Referenced by waitfornoise_exec(), and waitforsilence_exec().

◆ waitfornoise_exec()

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

Definition at line 298 of file app_waitforsilence.c.

299 {
300  return waitfor_exec(chan, data, &wait_for_noise);
301 }
static int waitfor_exec(struct ast_channel *chan, const char *data, const struct wait_type *wait_for)
static const struct wait_type wait_for_noise

References wait_for_noise, and waitfor_exec().

Referenced by load_module().

◆ waitforsilence_exec()

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

Definition at line 293 of file app_waitforsilence.c.

294 {
295  return waitfor_exec(chan, data, &wait_for_silence);
296 }
static const struct wait_type wait_for_silence

References wait_for_silence, and waitfor_exec().

Referenced by load_module().

Variable Documentation

◆ app_noise

char* app_noise = "WaitForNoise"
static

Definition at line 132 of file app_waitforsilence.c.

Referenced by load_module(), and unload_module().

◆ app_silence

char* app_silence = "WaitForSilence"
static

Definition at line 131 of file app_waitforsilence.c.

Referenced by load_module(), and unload_module().

◆ wait_for_noise

const struct wait_type wait_for_noise
static

Definition at line 132 of file app_waitforsilence.c.

Referenced by waitfornoise_exec().

◆ wait_for_silence

const struct wait_type wait_for_silence
static

Definition at line 132 of file app_waitforsilence.c.

Referenced by waitforsilence_exec().