summaryrefslogtreecommitdiff
path: root/apps/app_waitforsilence.c
blob: 52248ac097d3bb219593c2092544334d979fe208 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/*
 * Asterisk -- An open source telephony toolkit.
 *
 * Copyright (C) 1999 - 2005, Digium, Inc.
 *
 * WaitForSilence Application by David C. Troy <dave@popvox.com>
 * Version 1.11 2006-06-29
 *
 * Mark Spencer <markster@digium.com>
 *
 * See http://www.asterisk.org for more information about
 * the Asterisk project. Please do not directly contact
 * any of the maintainers of this project for assistance;
 * the project provides a web site, mailing lists and IRC
 * channels for your use.
 *
 * This program is free software, distributed under the terms of
 * the GNU General Public License Version 2. See the LICENSE file
 * at the top of the source tree.
 */

/*! \file
 *
 * \brief Wait for Silence
 *   - Waits for up to 'x' milliseconds of silence, 'y' times \n
 *   - WaitForSilence(500,2) will wait for 1/2 second of silence, twice \n
 *   - WaitForSilence(1000,1) will wait for 1 second of silence, once \n
 *   - WaitForSilence(300,3,10) will wait for 300ms of silence, 3 times, and return after 10sec \n
 *
 * \author David C. Troy <dave@popvox.com>
 *
 * \brief Wait For Noise
 * The same as Wait For Silence but listenes noise on the chennel that is above \n
 * the pre-configured silence threshold from dsp.conf
 *
 * \author Philipp Skadorov <skadorov@yahoo.com>
 *
 * \ingroup applications
 */

/*** MODULEINFO
	<support_level>extended</support_level>
 ***/

#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"

/*** DOCUMENTATION
	<application name="WaitForSilence" language="en_US">
		<synopsis>
			Waits for a specified amount of silence.
		</synopsis>
		<syntax>
			<parameter name="silencerequired">
				<para>If not specified, defaults to <literal>1000</literal> milliseconds.</para>
			</parameter>
			<parameter name="iterations">
				<para>If not specified, defaults to <literal>1</literal>.</para>
			</parameter>
			<parameter name="timeout">
				<para>Is specified only to avoid an infinite loop in cases where silence is never achieved.</para>
			</parameter>
		</syntax>
		<description>
			<para>Waits for up to <replaceable>silencerequired</replaceable> milliseconds of silence,
			<replaceable>iterations</replaceable> times. An optional <replaceable>timeout</replaceable>
			specified the number of seconds to return after, even if we do not receive the specified amount of silence.
			Use <replaceable>timeout</replaceable> with caution, as it may defeat the purpose of this application, which
			is to wait indefinitely until silence is detected on the line. This is particularly useful for reverse-911-type
			call broadcast applications where you need to wait for an answering machine to complete its spiel before
			playing a message.</para>
			<para>Typically you will want to include two or more calls to WaitForSilence when dealing with an answering
			machine; first waiting for the spiel to finish, then waiting for the beep, etc.</para>
			<para>Examples:</para>
			<para>WaitForSilence(500,2) will wait for 1/2 second of silence, twice</para>
			<para>WaitForSilence(1000) will wait for 1 second of silence, once</para>
			<para>WaitForSilence(300,3,10) will wait for 300ms silence, 3 times, and returns after 10 sec, even if silence
			is not detected</para>
			<para>Sets the channel variable <variable>WAITSTATUS</variable> to one of these values:</para>
			<variablelist>
				<variable name="WAITSTATUS">
					<value name="SILENCE">
						if exited with silence detected.
					</value>
					<value name="TIMEOUT">
						if exited without silence detected after timeout.
					</value>
				</variable>
			</variablelist>
		</description>
		<see-also>
			<ref type="application">WaitForNoise</ref>
		</see-also>
	</application>
	<application name="WaitForNoise" language="en_US">
		<synopsis>
			Waits for a specified amount of noise.
		</synopsis>
		<syntax>
			<parameter name="noiserequired">
				<para>If not specified, defaults to <literal>1000</literal> milliseconds.</para>
			</parameter>
			<parameter name="iterations">
				<para>If not specified, defaults to <literal>1</literal>.</para>
			</parameter>
			<parameter name="timeout">
				<para>Is specified only to avoid an infinite loop in cases where silence is never achieved.</para>
			</parameter>
		</syntax>
		<description>
			<para>Waits for up to <replaceable>noiserequired</replaceable> milliseconds of noise,
			<replaceable>iterations</replaceable> times. An optional <replaceable>timeout</replaceable>
			specified the number of seconds to return after, even if we do not receive the specified amount of noise.
			Use <replaceable>timeout</replaceable> with caution, as it may defeat the purpose of this application, which
			is to wait indefinitely until noise is detected on the line.</para>
		</description>
		<see-also>
			<ref type="application">WaitForSilence</ref>
		</see-also>
	</application>
 ***/

static char *app_silence = "WaitForSilence";
static char *app_noise = "WaitForNoise";

struct wait_type {
	const char *name;
	const char *status;
	int stop_on_frame_timeout;
	int (*func)(struct ast_dsp *, struct ast_frame *, int *);
};

static const struct wait_type wait_for_silence = {
	.name = "silence",
	.status = "SILENCE",
	.stop_on_frame_timeout = 1,
	.func = ast_dsp_silence,
};

static const struct wait_type wait_for_noise = {
	.name = "noise",
	.status = "NOISE",
	.stop_on_frame_timeout = 0,
	.func = ast_dsp_noise,
};

static int do_waiting(struct ast_channel *chan, int timereqd, time_t waitstart, int timeout, const struct wait_type *wait_for)
{
	RAII_VAR(struct ast_format *, rfmt, NULL, ao2_cleanup);
	int res;
	struct ast_dsp *sildet;

	rfmt = ao2_bump(ast_channel_readformat(chan));
	if ((res = ast_set_read_format(chan, ast_format_slin)) < 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));

	for (;;) {
		int 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) {
			if (wait_for->stop_on_frame_timeout) {
				dsptime = timereqd;
			}
		} else {
			/* Looks like we did get a frame, so let's check it out */
			struct ast_frame *f = ast_read(chan);
			if (!f) {
				pbx_builtin_setvar_helper(chan, "WAITSTATUS", "HANGUP");
				break;
			}
			if (f->frametype == AST_FRAME_VOICE) {
				wait_for->func(sildet, f, &dsptime);
			}
			ast_frfree(f);
		}

		ast_debug(1, "Got %dms of %s < %dms required\n", dsptime, wait_for->name, timereqd);

		if (dsptime >= timereqd) {
			ast_verb(3, "Exiting with %dms of %s >= %dms required\n", dsptime, wait_for->name, timereqd);
			pbx_builtin_setvar_helper(chan, "WAITSTATUS", wait_for->status);
			ast_debug(1, "WAITSTATUS was set to %s\n", wait_for->status);
			res = 1;
			break;
		}

		if (timeout && difftime(time(NULL), waitstart) >= timeout) {
			pbx_builtin_setvar_helper(chan, "WAITSTATUS", "TIMEOUT");
			ast_debug(1, "WAITSTATUS was set to TIMEOUT\n");
			res = 0;
			break;
		}
	}

	if (rfmt && ast_set_read_format(chan, rfmt)) {
		ast_log(LOG_WARNING, "Unable to restore format %s to channel '%s'\n", ast_format_get_name(rfmt), ast_channel_name(chan));
	}

	ast_dsp_free(sildet);
	return res;
}

static int waitfor_exec(struct ast_channel *chan, const char *data, const struct wait_type *wait_for)
{
	int res = 1;
	int timereqd = 1000;
	int timeout = 0;
	int iterations = 1, i;
	time_t waitstart;
	char *parse;
	struct ast_silence_generator *silgen = NULL;

	AST_DECLARE_APP_ARGS(args,
		AST_APP_ARG(timereqd);
		AST_APP_ARG(iterations);
		AST_APP_ARG(timeout);
	);

	parse = ast_strdupa(data);
	AST_STANDARD_APP_ARGS(args, parse);

	if (!ast_strlen_zero(args.timereqd)) {
		if (sscanf(args.timereqd, "%30d", &timereqd) != 1 || timereqd < 0) {
			ast_log(LOG_ERROR, "Argument '%srequired' must be an integer greater than or equal to zero.\n",
					wait_for->name);
			return -1;
		}
	}

	if (!ast_strlen_zero(args.iterations)) {
		if (sscanf(args.iterations, "%30d", &iterations) != 1 || iterations < 1) {
			ast_log(LOG_ERROR, "Argument 'iterations' must be an integer greater than 0.\n");
			return -1;
		}
	}

	if (!ast_strlen_zero(args.timeout)) {
		if (sscanf(args.timeout, "%30d", &timeout) != 1 || timeout < 0) {
			ast_log(LOG_ERROR, "Argument 'timeout' must be an integer greater than or equal to zero.\n");
			return -1;
		}
	}

	if (ast_channel_state(chan) != AST_STATE_UP) {
		ast_answer(chan); /* Answer the channel */
	}

	ast_verb(3, "Waiting %d time(s) for %dms of %s with %ds timeout\n",
			 iterations, timereqd, wait_for->name, timeout);

	if (ast_opt_transmit_silence) {
		silgen = ast_channel_start_silence_generator(chan);
	}

	time(&waitstart);
	for (i = 0; i < iterations && res == 1; i++) {
		res = do_waiting(chan, timereqd, waitstart, timeout, wait_for);
	}

	if (silgen) {
		ast_channel_stop_silence_generator(chan, silgen);
	}

	return res > 0 ? 0 : res;
}

static int waitforsilence_exec(struct ast_channel *chan, const char *data)
{
	return waitfor_exec(chan, data, &wait_for_silence);
}

static int waitfornoise_exec(struct ast_channel *chan, const char *data)
{
	return waitfor_exec(chan, data, &wait_for_noise);
}

static int unload_module(void)
{
	int res;
	res = ast_unregister_application(app_silence);
	res |= ast_unregister_application(app_noise);

	return res;
}

static int load_module(void)
{
	int res;

	res = ast_register_application_xml(app_silence, waitforsilence_exec);
	res |= ast_register_application_xml(app_noise, waitfornoise_exec);
	return res;
}

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