Asterisk - The Open Source Telephony Project GIT-master-8f1982c
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Macros Modules Pages
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 161 of file app_waitforsilence.c.

162{
163 RAII_VAR(struct ast_format *, rfmt, NULL, ao2_cleanup);
164 int res;
165 struct ast_dsp *sildet;
166
167 rfmt = ao2_bump(ast_channel_readformat(chan));
168 if ((res = ast_set_read_format(chan, ast_format_slin)) < 0) {
169 ast_log(LOG_WARNING, "Unable to set channel to linear mode, giving up\n");
170 return -1;
171 }
172
173 /* Create the silence detector */
174 if (!(sildet = ast_dsp_new())) {
175 ast_log(LOG_WARNING, "Unable to create silence detector\n");
176 return -1;
177 }
179
180 for (;;) {
181 int dsptime = 0;
182
183 res = ast_waitfor(chan, timereqd);
184
185 /* Must have gotten a hangup; let's exit */
186 if (res < 0) {
187 pbx_builtin_setvar_helper(chan, "WAITSTATUS", "HANGUP");
188 break;
189 }
190
191 /* We waited and got no frame; sounds like digital silence or a muted digital channel */
192 if (res == 0) {
193 if (wait_for->stop_on_frame_timeout) {
194 dsptime = timereqd;
195 }
196 } else {
197 /* Looks like we did get a frame, so let's check it out */
198 struct ast_frame *f = ast_read(chan);
199 if (!f) {
200 pbx_builtin_setvar_helper(chan, "WAITSTATUS", "HANGUP");
201 break;
202 }
203 if (f->frametype == AST_FRAME_VOICE) {
204 wait_for->func(sildet, f, &dsptime);
205 }
206 ast_frfree(f);
207 }
208
209 ast_debug(1, "Got %dms of %s < %dms required\n", dsptime, wait_for->name, timereqd);
210
211 if (dsptime >= timereqd) {
212 ast_verb(3, "Exiting with %dms of %s >= %dms required\n", dsptime, wait_for->name, timereqd);
213 pbx_builtin_setvar_helper(chan, "WAITSTATUS", wait_for->status);
214 ast_debug(1, "WAITSTATUS was set to %s\n", wait_for->status);
215 res = 1;
216 break;
217 }
218
219 if (timeout && difftime(time(NULL), waitstart) >= timeout) {
220 pbx_builtin_setvar_helper(chan, "WAITSTATUS", "TIMEOUT");
221 ast_debug(1, "WAITSTATUS was set to TIMEOUT\n");
222 res = 0;
223 break;
224 }
225 }
226
227 if (rfmt && ast_set_read_format(chan, rfmt)) {
228 ast_log(LOG_WARNING, "Unable to restore format %s to channel '%s'\n", ast_format_get_name(rfmt), ast_channel_name(chan));
229 }
230
231 ast_dsp_free(sildet);
232 return res;
233}
#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
const char * ast_channel_name(const struct ast_channel *chan)
int ast_waitfor(struct ast_channel *chan, int ms)
Wait for input on a channel.
Definition: channel.c:3130
struct ast_frame * ast_read(struct ast_channel *chan)
Reads a frame.
Definition: channel.c:4214
int ast_set_read_format(struct ast_channel *chan, struct ast_format *format)
Sets read format on channel chan.
Definition: channel.c:5721
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:1788
void ast_dsp_free(struct ast_dsp *dsp)
Definition: dsp.c:1783
@ 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:2009
struct ast_dsp * ast_dsp_new(void)
Allocates a new dsp, assumes 8khz for internal sample rate.
Definition: dsp.c:1758
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:941

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

319{
320 int res;
321
324 return res;
325}
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:640

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

310{
311 int res;
314
315 return res;
316}
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 235 of file app_waitforsilence.c.

236{
237 int res = 1;
238 int timereqd = 1000;
239 int timeout = 0;
240 int iterations = 1, i;
241 time_t waitstart;
242 char *parse;
243 struct ast_silence_generator *silgen = NULL;
244
246 AST_APP_ARG(timereqd);
247 AST_APP_ARG(iterations);
248 AST_APP_ARG(timeout);
249 );
250
251 parse = ast_strdupa(data);
253
254 if (!ast_strlen_zero(args.timereqd)) {
255 if (sscanf(args.timereqd, "%30d", &timereqd) != 1 || timereqd < 0) {
256 ast_log(LOG_ERROR, "Argument '%srequired' must be an integer greater than or equal to zero.\n",
257 wait_for->name);
258 return -1;
259 }
260 }
261
262 if (!ast_strlen_zero(args.iterations)) {
263 if (sscanf(args.iterations, "%30d", &iterations) != 1 || iterations < 1) {
264 ast_log(LOG_ERROR, "Argument 'iterations' must be an integer greater than 0.\n");
265 return -1;
266 }
267 }
268
269 if (!ast_strlen_zero(args.timeout)) {
270 if (sscanf(args.timeout, "%30d", &timeout) != 1 || timeout < 0) {
271 ast_log(LOG_ERROR, "Argument 'timeout' must be an integer greater than or equal to zero.\n");
272 return -1;
273 }
274 }
275
276 if (ast_channel_state(chan) != AST_STATE_UP) {
277 ast_answer(chan); /* Answer the channel */
278 }
279
280 ast_verb(3, "Waiting %d time(s) for %dms of %s with %ds timeout\n",
281 iterations, timereqd, wait_for->name, timeout);
282
285 }
286
287 time(&waitstart);
288 for (i = 0; i < iterations && res == 1; i++) {
289 res = do_waiting(chan, timereqd, waitstart, timeout, wait_for);
290 }
291
292 if (silgen) {
294 }
295
296 return res > 0 ? 0 : res;
297}
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
struct ast_silence_generator * ast_channel_start_silence_generator(struct ast_channel *chan)
Starts a silence generator on the given channel.
Definition: channel.c:8169
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:8215
int ast_answer(struct ast_channel *chan)
Answer a channel.
Definition: channel.c:2774
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:125
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, and NULL.

Referenced by waitfornoise_exec(), and waitforsilence_exec().

◆ waitfornoise_exec()

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

Definition at line 304 of file app_waitforsilence.c.

305{
306 return waitfor_exec(chan, data, &wait_for_noise);
307}
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 299 of file app_waitforsilence.c.

300{
301 return waitfor_exec(chan, data, &wait_for_silence);
302}
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 138 of file app_waitforsilence.c.

Referenced by load_module(), and unload_module().

◆ app_silence

char* app_silence = "WaitForSilence"
static

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

Referenced by waitfornoise_exec().

◆ wait_for_silence

const struct wait_type wait_for_silence
static

Definition at line 147 of file app_waitforsilence.c.

Referenced by waitforsilence_exec().