| 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 |
#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" |
| 50 |
#include "asterisk/format_cache.h" |
| 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 |
static char *app_silence = "WaitForSilence"; |
| 138 |
static char *app_noise = "WaitForNoise"; |
| 139 |
|
| 140 |
struct wait_type { |
| 141 |
const char *name; |
| 142 |
const char *status; |
| 143 |
int stop_on_frame_timeout; |
| 144 |
int (*func)(struct ast_dsp *, struct ast_frame *, int *); |
| 145 |
}; |
| 146 |
|
| 147 |
static 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 |
|
| 154 |
static 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 |
|
| 161 |
static int do_waiting(struct ast_channel *chan, int timereqd, struct timeval 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 |
|
| 174 |
if (!(sildet = ast_dsp_new())) { |
| 175 |
ast_log(LOG_WARNING, "Unable to create silence detector\n"); |
| 176 |
return -1; |
| 177 |
} |
| 178 |
ast_dsp_set_threshold(sildet, ast_dsp_get_threshold_from_settings(THRESHOLD_SILENCE)); |
| 179 |
|
| 180 |
for (;;) { |
| 181 |
int dsptime = 0; |
| 182 |
|
| 183 |
res = ast_waitfor(chan, timereqd); |
| 184 |
|
| 185 |
|
| 186 |
if (res < 0) { |
| 187 |
pbx_builtin_setvar_helper(chan, "WAITSTATUS", "HANGUP"); |
| 188 |
break; |
| 189 |
} |
| 190 |
|
| 191 |
|
| 192 |
if (res == 0) { |
| 193 |
if (wait_for->stop_on_frame_timeout) { |
| 194 |
dsptime = timereqd; |
| 195 |
} |
| 196 |
} else { |
| 197 |
|
| 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 && ast_tvdiff_ms(ast_tvnow(), waitstart) >= timeout * 1000) { |
| 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 |
|
| 235 |
static 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 |
struct timeval waitstart; |
| 242 |
char *parse; |
| 243 |
struct ast_silence_generator *silgen = NULL; |
| 244 |
|
| 245 |
AST_DECLARE_APP_ARGS(args, |
| 246 |
AST_APP_ARG(timereqd); |
| 247 |
AST_APP_ARG(iterations); |
| 248 |
AST_APP_ARG(timeout); |
| 249 |
); |
| 250 |
|
| 251 |
parse = ast_strdupa(data); |
| 252 |
AST_STANDARD_APP_ARGS(args, parse); |
| 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); |
| 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 |
|
| 283 |
if (ast_opt_transmit_silence) { |
| 284 |
silgen = ast_channel_start_silence_generator(chan); |
| 285 |
} |
| 286 |
|
| 287 |
waitstart = ast_tvnow(); |
| 288 |
for (i = 0; i < iterations && res == 1; i++) { |
| 289 |
res = do_waiting(chan, timereqd, waitstart, timeout, wait_for); |
| 290 |
} |
| 291 |
|
| 292 |
if (silgen) { |
| 293 |
ast_channel_stop_silence_generator(chan, silgen); |
| 294 |
} |
| 295 |
|
| 296 |
return res > 0 ? 0 : res; |
| 297 |
} |
| 298 |
|
| 299 |
static int waitforsilence_exec(struct ast_channel *chan, const char *data) |
| 300 |
{ |
| 301 |
return waitfor_exec(chan, data, &wait_for_silence); |
| 302 |
} |
| 303 |
|
| 304 |
static int waitfornoise_exec(struct ast_channel *chan, const char *data) |
| 305 |
{ |
| 306 |
return waitfor_exec(chan, data, &wait_for_noise); |
| 307 |
} |
| 308 |
|
| 309 |
static int unload_module(void) |
| 310 |
{ |
| 311 |
int res; |
| 312 |
res = ast_unregister_application(app_silence); |
| 313 |
res |= ast_unregister_application(app_noise); |
| 314 |
|
| 315 |
return res; |
| 316 |
} |
| 317 |
|
| 318 |
static int load_module(void) |
| 319 |
{ |
| 320 |
int res; |
| 321 |
|
| 322 |
res = ast_register_application_xml(app_silence, waitforsilence_exec); |
| 323 |
res |= ast_register_application_xml(app_noise, waitfornoise_exec); |
| 324 |
return res; |
| 325 |
} |
| 326 |
|
| 327 |
AST_MODULE_INFO_STANDARD_EXTENDED(ASTERISK_GPL_KEY, "Wait For Silence/Noise"); |