Asterisk - The Open Source Telephony Project GIT-master-8924258
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Macros Modules Pages
app_waitforsilence.c
Go to the documentation of this file.
1/*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (C) 1999 - 2005, Digium, Inc.
5 *
6 * WaitForSilence Application by David C. Troy <dave@popvox.com>
7 * Version 1.11 2006-06-29
8 *
9 * Mark Spencer <markster@digium.com>
10 *
11 * See http://www.asterisk.org for more information about
12 * the Asterisk project. Please do not directly contact
13 * any of the maintainers of this project for assistance;
14 * the project provides a web site, mailing lists and IRC
15 * channels for your use.
16 *
17 * This program is free software, distributed under the terms of
18 * the GNU General Public License Version 2. See the LICENSE file
19 * at the top of the source tree.
20 */
21
22/*! \file
23 *
24 * \brief Wait for Silence
25 * - Waits for up to 'x' milliseconds of silence, 'y' times \n
26 *
27 * \author David C. Troy <dave@popvox.com>
28 *
29 * \brief Wait For Noise
30 * The same as Wait For Silence but listenes noise on the chennel that is above \n
31 * the pre-configured silence threshold from dsp.conf
32 *
33 * \author Philipp Skadorov <skadorov@yahoo.com>
34 *
35 * \ingroup applications
36 */
37
38/*** MODULEINFO
39 <support_level>extended</support_level>
40 ***/
41
42#include "asterisk.h"
43
44#include "asterisk/app.h"
45#include "asterisk/file.h"
46#include "asterisk/channel.h"
47#include "asterisk/pbx.h"
48#include "asterisk/dsp.h"
49#include "asterisk/module.h"
51
52/*** DOCUMENTATION
53 <application name="WaitForSilence" language="en_US">
54 <since>
55 <version>1.2.0</version>
56 </since>
57 <synopsis>
58 Waits for a specified amount of silence.
59 </synopsis>
60 <syntax>
61 <parameter name="silencerequired">
62 <para>If not specified, defaults to <literal>1000</literal> milliseconds.</para>
63 </parameter>
64 <parameter name="iterations">
65 <para>If not specified, defaults to <literal>1</literal>.</para>
66 </parameter>
67 <parameter name="timeout">
68 <para>Is specified only to avoid an infinite loop in cases where silence is never achieved.</para>
69 </parameter>
70 </syntax>
71 <description>
72 <para>Waits for up to <replaceable>silencerequired</replaceable> milliseconds of silence,
73 <replaceable>iterations</replaceable> times. An optional <replaceable>timeout</replaceable>
74 specified the number of seconds to return after, even if we do not receive the specified amount of silence.
75 Use <replaceable>timeout</replaceable> with caution, as it may defeat the purpose of this application, which
76 is to wait indefinitely until silence is detected on the line. This is particularly useful for reverse-911-type
77 call broadcast applications where you need to wait for an answering machine to complete its spiel before
78 playing a message.</para>
79 <para>Typically you will want to include two or more calls to WaitForSilence when dealing with an answering
80 machine; first waiting for the spiel to finish, then waiting for the beep, etc.</para>
81 <example title="Wait for half a second of silence, twice">
82 same => n,WaitForSilence(500,2)
83 </example>
84 <example title="Wait for one second of silence, once">
85 same => n,WaitForSilence(1000)
86 </example>
87 <example title="Wait for 300 ms of silence, 3 times, and returns after 10 seconds, even if no silence detected">
88 same => n,WaitForSilence(300,3,10)
89 </example>
90 <para>Sets the channel variable <variable>WAITSTATUS</variable> to one of these values:</para>
91 <variablelist>
92 <variable name="WAITSTATUS">
93 <value name="SILENCE">
94 if exited with silence detected.
95 </value>
96 <value name="TIMEOUT">
97 if exited without silence detected after timeout.
98 </value>
99 </variable>
100 </variablelist>
101 </description>
102 <see-also>
103 <ref type="application">WaitForNoise</ref>
104 </see-also>
105 </application>
106 <application name="WaitForNoise" language="en_US">
107 <since>
108 <version>1.6.1.0</version>
109 </since>
110 <synopsis>
111 Waits for a specified amount of noise.
112 </synopsis>
113 <syntax>
114 <parameter name="noiserequired">
115 <para>If not specified, defaults to <literal>1000</literal> milliseconds.</para>
116 </parameter>
117 <parameter name="iterations">
118 <para>If not specified, defaults to <literal>1</literal>.</para>
119 </parameter>
120 <parameter name="timeout">
121 <para>Is specified only to avoid an infinite loop in cases where silence is never achieved.</para>
122 </parameter>
123 </syntax>
124 <description>
125 <para>Waits for up to <replaceable>noiserequired</replaceable> milliseconds of noise,
126 <replaceable>iterations</replaceable> times. An optional <replaceable>timeout</replaceable>
127 specified the number of seconds to return after, even if we do not receive the specified amount of noise.
128 Use <replaceable>timeout</replaceable> with caution, as it may defeat the purpose of this application, which
129 is to wait indefinitely until noise is detected on the line.</para>
130 </description>
131 <see-also>
132 <ref type="application">WaitForSilence</ref>
133 </see-also>
134 </application>
135 ***/
136
137static char *app_silence = "WaitForSilence";
138static char *app_noise = "WaitForNoise";
139
140struct wait_type {
141 const char *name;
142 const char *status;
144 int (*func)(struct ast_dsp *, struct ast_frame *, int *);
145};
146
147static const struct wait_type wait_for_silence = {
148 .name = "silence",
149 .status = "SILENCE",
150 .stop_on_frame_timeout = 1,
151 .func = ast_dsp_silence,
152};
153
154static const struct wait_type wait_for_noise = {
155 .name = "noise",
156 .status = "NOISE",
157 .stop_on_frame_timeout = 0,
158 .func = ast_dsp_noise,
159};
160
161static int do_waiting(struct ast_channel *chan, int timereqd, time_t waitstart, int timeout, const struct wait_type *wait_for)
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}
234
235static int waitfor_exec(struct ast_channel *chan, const char *data, const struct wait_type *wait_for)
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}
298
299static int waitforsilence_exec(struct ast_channel *chan, const char *data)
300{
301 return waitfor_exec(chan, data, &wait_for_silence);
302}
303
304static int waitfornoise_exec(struct ast_channel *chan, const char *data)
305{
306 return waitfor_exec(chan, data, &wait_for_noise);
307}
308
309static int unload_module(void)
310{
311 int res;
314
315 return res;
316}
317
318static int load_module(void)
319{
320 int res;
321
324 return res;
325}
326
AST_MODULE_INFO_STANDARD_EXTENDED(ASTERISK_GPL_KEY, "Wait For Silence/Noise")
static char * app_silence
static int waitfornoise_exec(struct ast_channel *chan, const char *data)
static const struct wait_type wait_for_silence
static int do_waiting(struct ast_channel *chan, int timereqd, time_t waitstart, int timeout, const struct wait_type *wait_for)
static int waitforsilence_exec(struct ast_channel *chan, const char *data)
static int load_module(void)
static char * app_noise
static int unload_module(void)
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
Asterisk main include file. File version handling, generic pbx functions.
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition: astmm.h:298
#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
General Asterisk PBX channel definitions.
const char * ast_channel_name(const struct ast_channel *chan)
struct ast_silence_generator * ast_channel_start_silence_generator(struct ast_channel *chan)
Starts a silence generator on the given channel.
Definition: channel.c:8190
int ast_waitfor(struct ast_channel *chan, int ms)
Wait for input on a channel.
Definition: channel.c:3190
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:8236
struct ast_frame * ast_read(struct ast_channel *chan)
Reads a frame.
Definition: channel.c:4274
int ast_set_read_format(struct ast_channel *chan, struct ast_format *format)
Sets read format on channel chan.
Definition: channel.c:5779
int ast_answer(struct ast_channel *chan)
Answer a channel.
Definition: channel.c:2834
struct ast_format * ast_channel_readformat(struct ast_channel *chan)
ast_channel_state
ast_channel states
Definition: channelstate.h:35
@ AST_STATE_UP
Definition: channelstate.h:42
Convenient Signal Processing routines.
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_silence(struct ast_dsp *dsp, struct ast_frame *f, int *totalsilence)
Process the audio frame for silence.
Definition: dsp.c:1488
int ast_dsp_get_threshold_from_settings(enum threshold which)
Get silence threshold from dsp.conf.
Definition: dsp.c:2009
int ast_dsp_noise(struct ast_dsp *dsp, struct ast_frame *f, int *totalnoise)
Process the audio frame for noise.
Definition: dsp.c:1493
struct ast_dsp * ast_dsp_new(void)
Allocates a new dsp, assumes 8khz for internal sample rate.
Definition: dsp.c:1758
Generic File Format Support. Should be included by clients of the file handling routines....
const char * ast_format_get_name(const struct ast_format *format)
Get the name associated with a format.
Definition: format.c:334
Media Format Cache API.
struct ast_format * ast_format_slin
Built-in cached signed linear 8kHz format.
Definition: format_cache.c:41
Application convenience functions, designed to give consistent look and feel to Asterisk apps.
#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 ast_frfree(fr)
@ AST_FRAME_VOICE
#define ast_debug(level,...)
Log a DEBUG message.
#define LOG_ERROR
#define ast_verb(level,...)
#define LOG_WARNING
Asterisk module definitions.
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:46
int ast_unregister_application(const char *app)
Unregister an application.
Definition: pbx_app.c:392
#define ast_register_application_xml(app, execute)
Register an application using XML documentation.
Definition: module.h:640
#define ast_opt_transmit_silence
Definition: options.h:124
Core PBX routines and definitions.
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
static force_inline int attribute_pure ast_strlen_zero(const char *s)
Definition: strings.h:65
Main Channel structure associated with a channel.
Definition: dsp.c:407
Definition of a media format.
Definition: format.c:43
Data structure associated with a single frame of data.
union ast_frame::@228 data
enum ast_frame_type frametype
int(* func)(struct ast_dsp *, struct ast_frame *, int *)
const char * name
const char * status
const char * args
#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