Asterisk - The Open Source Telephony Project  18.5.0
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"   
)

Referenced by load_module().

◆ 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 154 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_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().

155 {
156  RAII_VAR(struct ast_format *, rfmt, NULL, ao2_cleanup);
157  int res;
158  struct ast_dsp *sildet;
159 
160  rfmt = ao2_bump(ast_channel_readformat(chan));
161  if ((res = ast_set_read_format(chan, ast_format_slin)) < 0) {
162  ast_log(LOG_WARNING, "Unable to set channel to linear mode, giving up\n");
163  return -1;
164  }
165 
166  /* Create the silence detector */
167  if (!(sildet = ast_dsp_new())) {
168  ast_log(LOG_WARNING, "Unable to create silence detector\n");
169  return -1;
170  }
172 
173  for (;;) {
174  int dsptime = 0;
175 
176  res = ast_waitfor(chan, timereqd);
177 
178  /* Must have gotten a hangup; let's exit */
179  if (res < 0) {
180  pbx_builtin_setvar_helper(chan, "WAITSTATUS", "HANGUP");
181  break;
182  }
183 
184  /* We waited and got no frame; sounds like digital silence or a muted digital channel */
185  if (res == 0) {
186  if (wait_for->stop_on_frame_timeout) {
187  dsptime = timereqd;
188  }
189  } else {
190  /* Looks like we did get a frame, so let's check it out */
191  struct ast_frame *f = ast_read(chan);
192  if (!f) {
193  pbx_builtin_setvar_helper(chan, "WAITSTATUS", "HANGUP");
194  break;
195  }
196  if (f->frametype == AST_FRAME_VOICE) {
197  wait_for->func(sildet, f, &dsptime);
198  }
199  ast_frfree(f);
200  }
201 
202  ast_debug(1, "Got %dms of %s < %dms required\n", dsptime, wait_for->name, timereqd);
203 
204  if (dsptime >= timereqd) {
205  ast_verb(3, "Exiting with %dms of %s >= %dms required\n", dsptime, wait_for->name, timereqd);
206  pbx_builtin_setvar_helper(chan, "WAITSTATUS", wait_for->status);
207  ast_debug(1, "WAITSTATUS was set to %s\n", wait_for->status);
208  res = 1;
209  break;
210  }
211 
212  if (timeout && difftime(time(NULL), waitstart) >= timeout) {
213  pbx_builtin_setvar_helper(chan, "WAITSTATUS", "TIMEOUT");
214  ast_debug(1, "WAITSTATUS was set to TIMEOUT\n");
215  res = 0;
216  break;
217  }
218  }
219 
220  if (rfmt && ast_set_read_format(chan, rfmt)) {
221  ast_log(LOG_WARNING, "Unable to restore format %s to channel '%s'\n", ast_format_get_name(rfmt), ast_channel_name(chan));
222  }
223 
224  ast_dsp_free(sildet);
225  return res;
226 }
const char * name
const char * status
void ast_dsp_free(struct ast_dsp *dsp)
Definition: dsp.c:1770
#define LOG_WARNING
Definition: logger.h:274
struct ast_dsp * ast_dsp_new(void)
Allocates a new dsp, assumes 8khz for internal sample rate.
Definition: dsp.c:1745
static int timeout
Definition: cdr_mysql.c:86
struct ast_frame * ast_read(struct ast_channel *chan)
Reads a frame.
Definition: channel.c:4302
Definition of a media format.
Definition: format.c:43
const char * ast_format_get_name(const struct ast_format *format)
Get the name associated with a format.
Definition: format.c:334
#define NULL
Definition: resample.c:96
#define ast_verb(level,...)
Definition: logger.h:463
struct ast_format * ast_channel_readformat(struct ast_channel *chan)
#define ao2_bump(obj)
Definition: astobj2.h:491
#define ast_debug(level,...)
Log a DEBUG message.
Definition: logger.h:452
#define ast_log
Definition: astobj2.c:42
int ast_set_read_format(struct ast_channel *chan, struct ast_format *format)
Sets read format on channel chan.
Definition: channel.c:5849
#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:911
Definition: dsp.c:405
int(* func)(struct ast_dsp *, struct ast_frame *, int *)
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:1775
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...
int ast_waitfor(struct ast_channel *chan, int ms)
Wait for input on a channel.
Definition: channel.c:3171
#define ao2_cleanup(obj)
Definition: astobj2.h:1958
const char * ast_channel_name(const struct ast_channel *chan)
#define ast_frfree(fr)
Data structure associated with a single frame of data.
enum ast_frame_type frametype
struct ast_format * ast_format_slin
Built-in cached signed linear 8kHz format.
Definition: format_cache.c:41
int ast_dsp_get_threshold_from_settings(enum threshold which)
Get silence threshold from dsp.conf.
Definition: dsp.c:1996

◆ load_module()

static int load_module ( void  )
static

Definition at line 311 of file app_waitforsilence.c.

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

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

◆ unload_module()

static int unload_module ( void  )
static

Definition at line 302 of file app_waitforsilence.c.

References app_noise, app_silence, and ast_unregister_application().

303 {
304  int res;
307 
308  return res;
309 }
int ast_unregister_application(const char *app)
Unregister an application.
Definition: pbx_app.c:392
static char * app_silence
static char * app_noise

◆ waitfor_exec()

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

Definition at line 228 of file app_waitforsilence.c.

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, parse(), and timeout.

Referenced by waitfornoise_exec(), and waitforsilence_exec().

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

◆ waitfornoise_exec()

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

Definition at line 297 of file app_waitforsilence.c.

References waitfor_exec().

Referenced by load_module().

298 {
299  return waitfor_exec(chan, data, &wait_for_noise);
300 }
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

◆ waitforsilence_exec()

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

Definition at line 292 of file app_waitforsilence.c.

References waitfor_exec().

Referenced by load_module().

293 {
294  return waitfor_exec(chan, data, &wait_for_silence);
295 }
static int waitfor_exec(struct ast_channel *chan, const char *data, const struct wait_type *wait_for)
static const struct wait_type wait_for_silence

Variable Documentation

◆ app_noise

char* app_noise = "WaitForNoise"
static

Definition at line 131 of file app_waitforsilence.c.

Referenced by load_module(), and unload_module().

◆ app_silence

char* app_silence = "WaitForSilence"
static

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

◆ wait_for_silence

const struct wait_type wait_for_silence
static

Definition at line 140 of file app_waitforsilence.c.