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
00042
00043
00044
00045 #include "asterisk.h"
00046
00047 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 328209 $")
00048
00049 #include "asterisk/file.h"
00050 #include "asterisk/channel.h"
00051 #include "asterisk/pbx.h"
00052 #include "asterisk/dsp.h"
00053 #include "asterisk/module.h"
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
00123
00124
00125
00126 static char *app_silence = "WaitForSilence";
00127 static char *app_noise = "WaitForNoise";
00128
00129 static int do_waiting(struct ast_channel *chan, int timereqd, time_t waitstart, int timeout, int wait_for_silence) {
00130 struct ast_frame *f = NULL;
00131 int dsptime = 0;
00132 int rfmt = 0;
00133 int res = 0;
00134 struct ast_dsp *sildet;
00135 time_t now;
00136
00137
00138 int (*ast_dsp_func)(struct ast_dsp*, struct ast_frame*, int*) =
00139 wait_for_silence ? ast_dsp_silence : ast_dsp_noise;
00140
00141 rfmt = chan->readformat;
00142 if ((res = ast_set_read_format(chan, AST_FORMAT_SLINEAR)) < 0) {
00143 ast_log(LOG_WARNING, "Unable to set channel to linear mode, giving up\n");
00144 return -1;
00145 }
00146
00147
00148 if (!(sildet = ast_dsp_new())) {
00149 ast_log(LOG_WARNING, "Unable to create silence detector :(\n");
00150 return -1;
00151 }
00152 ast_dsp_set_threshold(sildet, ast_dsp_get_threshold_from_settings(THRESHOLD_SILENCE));
00153
00154
00155 for (;;) {
00156
00157 dsptime = 0;
00158
00159 res = ast_waitfor(chan, timereqd);
00160
00161
00162 if (res < 0) {
00163 pbx_builtin_setvar_helper(chan, "WAITSTATUS", "HANGUP");
00164 break;
00165 }
00166
00167
00168 if (res == 0) {
00169 dsptime = timereqd;
00170 } else {
00171
00172 if (!(f = ast_read(chan))) {
00173 pbx_builtin_setvar_helper(chan, "WAITSTATUS", "HANGUP");
00174 break;
00175 }
00176 if (f->frametype == AST_FRAME_VOICE) {
00177 ast_dsp_func(sildet, f, &dsptime);
00178 }
00179 ast_frfree(f);
00180 }
00181
00182 ast_verb(6, "Got %dms %s < %dms required\n", dsptime, wait_for_silence ? "silence" : "noise", timereqd);
00183
00184 if (dsptime >= timereqd) {
00185 ast_verb(3, "Exiting with %dms %s >= %dms required\n", dsptime, wait_for_silence ? "silence" : "noise", timereqd);
00186
00187 res = 1;
00188 pbx_builtin_setvar_helper(chan, "WAITSTATUS", wait_for_silence ? "SILENCE" : "NOISE");
00189 ast_debug(1, "WAITSTATUS was set to %s\n", wait_for_silence ? "SILENCE" : "NOISE");
00190 break;
00191 }
00192
00193 if (timeout && (difftime(time(&now), waitstart) >= timeout)) {
00194 pbx_builtin_setvar_helper(chan, "WAITSTATUS", "TIMEOUT");
00195 ast_debug(1, "WAITSTATUS was set to TIMEOUT\n");
00196 res = 0;
00197 break;
00198 }
00199 }
00200
00201
00202 if (rfmt && ast_set_read_format(chan, rfmt)) {
00203 ast_log(LOG_WARNING, "Unable to restore format %s to channel '%s'\n", ast_getformatname(rfmt), chan->name);
00204 }
00205 ast_dsp_free(sildet);
00206 return res;
00207 }
00208
00209 static int waitfor_exec(struct ast_channel *chan, const char *data, int wait_for_silence)
00210 {
00211 int res = 1;
00212 int timereqd = 1000;
00213 int timeout = 0;
00214 int iterations = 1, i;
00215 time_t waitstart;
00216 struct ast_silence_generator *silgen = NULL;
00217
00218 if (chan->_state != AST_STATE_UP) {
00219 res = ast_answer(chan);
00220 }
00221
00222 if (!data || ( (sscanf(data, "%30d,%30d,%30d", &timereqd, &iterations, &timeout) != 3) &&
00223 (sscanf(data, "%30d,%30d", &timereqd, &iterations) != 2) &&
00224 (sscanf(data, "%30d", &timereqd) != 1) ) ) {
00225 ast_log(LOG_WARNING, "Using default value of 1000ms, 1 iteration, no timeout\n");
00226 }
00227
00228 ast_verb(3, "Waiting %d time(s) for %d ms silence with %d timeout\n", iterations, timereqd, timeout);
00229
00230 if (ast_opt_transmit_silence) {
00231 silgen = ast_channel_start_silence_generator(chan);
00232 }
00233 time(&waitstart);
00234 res = 1;
00235 for (i=0; (i<iterations) && (res == 1); i++) {
00236 res = do_waiting(chan, timereqd, waitstart, timeout, wait_for_silence);
00237 }
00238 if (silgen) {
00239 ast_channel_stop_silence_generator(chan, silgen);
00240 }
00241
00242
00243 if (res > 0)
00244 res = 0;
00245 return res;
00246 }
00247
00248 static int waitforsilence_exec(struct ast_channel *chan, const char *data)
00249 {
00250 return waitfor_exec(chan, data, 1);
00251 }
00252
00253 static int waitfornoise_exec(struct ast_channel *chan, const char *data)
00254 {
00255 return waitfor_exec(chan, data, 0);
00256 }
00257
00258 static int unload_module(void)
00259 {
00260 int res;
00261 res = ast_unregister_application(app_silence);
00262 res |= ast_unregister_application(app_noise);
00263
00264 return res;
00265 }
00266
00267 static int load_module(void)
00268 {
00269 int res;
00270
00271 res = ast_register_application_xml(app_silence, waitforsilence_exec);
00272 res |= ast_register_application_xml(app_noise, waitfornoise_exec);
00273 return res;
00274 }
00275
00276 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Wait For Silence");
00277