Turn a NOTICE into a DEBUG message.
[asterisk-bristuff.git] / apps / app_waitforsilence.c
blobef0734cdc9efec4297f1c9051706eda9deb75a2a
1 /*
2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2005, Digium, Inc.
6 * WaitForSilence Application by David C. Troy <dave@popvox.com>
7 * Version 1.11 2006-06-29
9 * Mark Spencer <markster@digium.com>
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.
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.
22 /*! \file
24 * \brief Wait for Silence
25 * - Waits for up to 'x' milliseconds of silence, 'y' times \n
26 * - WaitForSilence(500,2) will wait for 1/2 second of silence, twice \n
27 * - WaitForSilence(1000,1) will wait for 1 second of silence, once \n
28 * - WaitForSilence(300,3,10) will wait for 300ms of silence, 3 times, and return after 10sec \n
30 * \author David C. Troy <dave@popvox.com>
32 * \ingroup applications
35 #include "asterisk.h"
37 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
39 #include <stdlib.h>
40 #include <stdio.h>
41 #include <string.h>
42 #include <unistd.h>
44 #include "asterisk/file.h"
45 #include "asterisk/logger.h"
46 #include "asterisk/channel.h"
47 #include "asterisk/pbx.h"
48 #include "asterisk/dsp.h"
49 #include "asterisk/module.h"
50 #include "asterisk/options.h"
52 static char *app = "WaitForSilence";
53 static char *synopsis = "Waits for a specified amount of silence";
54 static char *descrip =
55 " WaitForSilence(silencerequired[|iterations][|timeout]) \n"
56 "Wait for Silence: Waits for up to 'silencerequired' \n"
57 "milliseconds of silence, 'iterations' times or once if omitted.\n"
58 "An optional timeout specified the number of seconds to return\n"
59 "after, even if we do not receive the specified amount of silence.\n"
60 "Use 'timeout' with caution, as it may defeat the purpose of this\n"
61 "application, which is to wait indefinitely until silence is detected\n"
62 "on the line. This is particularly useful for reverse-911-type\n"
63 "call broadcast applications where you need to wait for an answering\n"
64 "machine to complete its spiel before playing a message.\n"
65 "The timeout parameter is specified only to avoid an infinite loop in\n"
66 "cases where silence is never achieved. Typically you will want to\n"
67 "include two or more calls to WaitForSilence when dealing with an answering\n"
68 "machine; first waiting for the spiel to finish, then waiting for the beep, etc.\n\n"
69 "Examples:\n"
70 " - WaitForSilence(500|2) will wait for 1/2 second of silence, twice\n"
71 " - WaitForSilence(1000) will wait for 1 second of silence, once\n"
72 " - WaitForSilence(300|3|10) will wait for 300ms silence, 3 times,\n"
73 " and returns after 10 sec, even if silence is not detected\n\n"
74 "Sets the channel variable WAITSTATUS with to one of these values:\n"
75 "SILENCE - if exited with silence detected\n"
76 "TIMEOUT - if exited without silence detected after timeout\n";
78 static int do_waiting(struct ast_channel *chan, int silencereqd, time_t waitstart, int timeout) {
79 struct ast_frame *f;
80 int dspsilence = 0;
81 static int silencethreshold = 128;
82 int rfmt = 0;
83 int res = 0;
84 struct ast_dsp *sildet; /* silence detector dsp */
85 time_t now;
87 rfmt = chan->readformat; /* Set to linear mode */
88 res = ast_set_read_format(chan, AST_FORMAT_SLINEAR);
89 if (res < 0) {
90 ast_log(LOG_WARNING, "Unable to set channel to linear mode, giving up\n");
91 return -1;
94 sildet = ast_dsp_new(); /* Create the silence detector */
95 if (!sildet) {
96 ast_log(LOG_WARNING, "Unable to create silence detector :(\n");
97 return -1;
99 ast_dsp_set_threshold(sildet, silencethreshold);
101 /* Await silence... */
102 f = NULL;
103 for(;;) {
104 /* Start with no silence received */
105 dspsilence = 0;
107 res = ast_waitfor(chan, silencereqd);
109 /* Must have gotten a hangup; let's exit */
110 if (res <= 0) {
111 f = NULL;
112 break;
115 /* We waited and got no frame; sounds like digital silence or a muted digital channel */
116 if (!res) {
117 dspsilence = silencereqd;
118 } else {
119 /* Looks like we did get a frame, so let's check it out */
120 f = ast_read(chan);
121 if (!f)
122 break;
123 if (f && f->frametype == AST_FRAME_VOICE) {
124 ast_dsp_silence(sildet, f, &dspsilence);
125 ast_frfree(f);
129 if (option_verbose > 6)
130 ast_verbose(VERBOSE_PREFIX_3 "Got %dms silence< %dms required\n", dspsilence, silencereqd);
132 if (dspsilence >= silencereqd) {
133 if (option_verbose > 2)
134 ast_verbose(VERBOSE_PREFIX_3 "Exiting with %dms silence >= %dms required\n", dspsilence, silencereqd);
135 /* Ended happily with silence */
136 res = 1;
137 pbx_builtin_setvar_helper(chan, "WAITSTATUS", "SILENCE");
138 ast_log(LOG_DEBUG, "WAITSTATUS was set to SILENCE\n");
139 break;
142 if ( timeout && (difftime(time(&now),waitstart) >= timeout) ) {
143 pbx_builtin_setvar_helper(chan, "WAITSTATUS", "TIMEOUT");
144 ast_log(LOG_DEBUG, "WAITSTATUS was set to TIMEOUT\n");
145 res = 0;
146 break;
151 if (rfmt && ast_set_read_format(chan, rfmt)) {
152 ast_log(LOG_WARNING, "Unable to restore format %s to channel '%s'\n", ast_getformatname(rfmt), chan->name);
154 ast_dsp_free(sildet);
155 return res;
158 static int waitforsilence_exec(struct ast_channel *chan, void *data)
160 int res = 1;
161 int silencereqd = 1000;
162 int timeout = 0;
163 int iterations = 1, i;
164 time_t waitstart;
166 res = ast_answer(chan); /* Answer the channel */
168 if (!data || ( (sscanf(data, "%d|%d|%d", &silencereqd, &iterations, &timeout) != 3) &&
169 (sscanf(data, "%d|%d", &silencereqd, &iterations) != 2) &&
170 (sscanf(data, "%d", &silencereqd) != 1) ) ) {
171 ast_log(LOG_WARNING, "Using default value of 1000ms, 1 iteration, no timeout\n");
174 if (option_verbose > 2)
175 ast_verbose(VERBOSE_PREFIX_3 "Waiting %d time(s) for %d ms silence with %d timeout\n", iterations, silencereqd, timeout);
177 time(&waitstart);
178 res = 1;
179 for (i=0; (i<iterations) && (res == 1); i++) {
180 res = do_waiting(chan, silencereqd, waitstart, timeout);
182 if (res > 0)
183 res = 0;
184 return res;
188 static int unload_module(void)
190 int res;
192 res = ast_unregister_application(app);
194 ast_module_user_hangup_all();
196 return res;
199 static int load_module(void)
201 return ast_register_application(app, waitforsilence_exec, synopsis, descrip);
204 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Wait For Silence");