00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041 #include "asterisk.h"
00042
00043 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 239712 $")
00044
00045 #include "asterisk/file.h"
00046 #include "asterisk/channel.h"
00047 #include "asterisk/pbx.h"
00048 #include "asterisk/dsp.h"
00049 #include "asterisk/module.h"
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122 static char *app_silence = "WaitForSilence";
00123 static char *app_noise = "WaitForNoise";
00124
00125 static int do_waiting(struct ast_channel *chan, int timereqd, time_t waitstart, int timeout, int wait_for_silence) {
00126 struct ast_frame *f = NULL;
00127 int dsptime = 0;
00128 int rfmt = 0;
00129 int res = 0;
00130 struct ast_dsp *sildet;
00131 time_t now;
00132
00133
00134 int (*ast_dsp_func)(struct ast_dsp*, struct ast_frame*, int*) =
00135 wait_for_silence ? ast_dsp_silence : ast_dsp_noise;
00136
00137 rfmt = chan->readformat;
00138 if ((res = ast_set_read_format(chan, AST_FORMAT_SLINEAR)) < 0) {
00139 ast_log(LOG_WARNING, "Unable to set channel to linear mode, giving up\n");
00140 return -1;
00141 }
00142
00143
00144 if (!(sildet = ast_dsp_new())) {
00145 ast_log(LOG_WARNING, "Unable to create silence detector :(\n");
00146 return -1;
00147 }
00148 ast_dsp_set_threshold(sildet, ast_dsp_get_threshold_from_settings(THRESHOLD_SILENCE));
00149
00150
00151 for (;;) {
00152
00153 dsptime = 0;
00154
00155 res = ast_waitfor(chan, timereqd);
00156
00157
00158 if (res < 0) {
00159 pbx_builtin_setvar_helper(chan, "WAITSTATUS", "HANGUP");
00160 break;
00161 }
00162
00163
00164 if (res == 0) {
00165 dsptime = timereqd;
00166 } else {
00167
00168 if (!(f = ast_read(chan))) {
00169 pbx_builtin_setvar_helper(chan, "WAITSTATUS", "HANGUP");
00170 break;
00171 }
00172 if (f->frametype == AST_FRAME_VOICE) {
00173 ast_dsp_func(sildet, f, &dsptime);
00174 }
00175 ast_frfree(f);
00176 }
00177
00178 ast_verb(6, "Got %dms %s < %dms required\n", dsptime, wait_for_silence ? "silence" : "noise", timereqd);
00179
00180 if (dsptime >= timereqd) {
00181 ast_verb(3, "Exiting with %dms %s >= %dms required\n", dsptime, wait_for_silence ? "silence" : "noise", timereqd);
00182
00183 res = 1;
00184 pbx_builtin_setvar_helper(chan, "WAITSTATUS", wait_for_silence ? "SILENCE" : "NOISE");
00185 ast_debug(1, "WAITSTATUS was set to %s\n", wait_for_silence ? "SILENCE" : "NOISE");
00186 break;
00187 }
00188
00189 if (timeout && (difftime(time(&now), waitstart) >= timeout)) {
00190 pbx_builtin_setvar_helper(chan, "WAITSTATUS", "TIMEOUT");
00191 ast_debug(1, "WAITSTATUS was set to TIMEOUT\n");
00192 res = 0;
00193 break;
00194 }
00195 }
00196
00197
00198 if (rfmt && ast_set_read_format(chan, rfmt)) {
00199 ast_log(LOG_WARNING, "Unable to restore format %s to channel '%s'\n", ast_getformatname(rfmt), chan->name);
00200 }
00201 ast_dsp_free(sildet);
00202 return res;
00203 }
00204
00205 static int waitfor_exec(struct ast_channel *chan, const char *data, int wait_for_silence)
00206 {
00207 int res = 1;
00208 int timereqd = 1000;
00209 int timeout = 0;
00210 int iterations = 1, i;
00211 time_t waitstart;
00212 struct ast_silence_generator *silgen = NULL;
00213
00214 if (chan->_state != AST_STATE_UP) {
00215 res = ast_answer(chan);
00216 }
00217
00218 if (!data || ( (sscanf(data, "%30d,%30d,%30d", &timereqd, &iterations, &timeout) != 3) &&
00219 (sscanf(data, "%30d,%30d", &timereqd, &iterations) != 2) &&
00220 (sscanf(data, "%30d", &timereqd) != 1) ) ) {
00221 ast_log(LOG_WARNING, "Using default value of 1000ms, 1 iteration, no timeout\n");
00222 }
00223
00224 ast_verb(3, "Waiting %d time(s) for %d ms silence with %d timeout\n", iterations, timereqd, timeout);
00225
00226 if (ast_opt_transmit_silence) {
00227 silgen = ast_channel_start_silence_generator(chan);
00228 }
00229 time(&waitstart);
00230 res = 1;
00231 for (i=0; (i<iterations) && (res == 1); i++) {
00232 res = do_waiting(chan, timereqd, waitstart, timeout, wait_for_silence);
00233 }
00234 if (silgen) {
00235 ast_channel_stop_silence_generator(chan, silgen);
00236 }
00237
00238
00239 if (res > 0)
00240 res = 0;
00241 return res;
00242 }
00243
00244 static int waitforsilence_exec(struct ast_channel *chan, const char *data)
00245 {
00246 return waitfor_exec(chan, data, 1);
00247 }
00248
00249 static int waitfornoise_exec(struct ast_channel *chan, const char *data)
00250 {
00251 return waitfor_exec(chan, data, 0);
00252 }
00253
00254 static int unload_module(void)
00255 {
00256 int res;
00257 res = ast_unregister_application(app_silence);
00258 res |= ast_unregister_application(app_noise);
00259
00260 return res;
00261 }
00262
00263 static int load_module(void)
00264 {
00265 int res;
00266
00267 res = ast_register_application_xml(app_silence, waitforsilence_exec);
00268 res |= ast_register_application_xml(app_noise, waitfornoise_exec);
00269 return res;
00270 }
00271
00272 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Wait For Silence");
00273