-
Notifications
You must be signed in to change notification settings - Fork 2
/
app_nv_backgrounddetect.c
372 lines (333 loc) · 12.5 KB
/
app_nv_backgrounddetect.c
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
/*
* Asterisk -- A telephony toolkit for Linux.
*
* Playback a file with audio detect
*
* Copyright (C) 2004-2005, Newman Telecom, Inc. and Newman Ventures, Inc.
*
* Justin Newman <[email protected]>
*
* We would like to thank Newman Ventures, Inc. for funding this
* Asterisk project.
*
* Newman Ventures <[email protected]>
*
* Portions Copyright:
* Copyright (C) 2001, Linux Support Services, Inc.
* Copyright (C) 2004, Digium, Inc.
*
* Matthew Fredrickson <[email protected]>
* Mark Spencer <[email protected]>
*
* This program is free software, distributed under the terms of
* the GNU General Public License
*
* Updated by Andrew Matthews [email protected]
* http://www.exstatica.net. Changes: To work with Asterisk 1.6,
* and the new delimiter (',' has replaced '|')
*
*/
#include "asterisk.h"
#include <asterisk/lock.h>
#include <asterisk/file.h>
#include <asterisk/logger.h>
#include <asterisk/channel.h>
#include <asterisk/pbx.h>
#include <asterisk/module.h>
#include <asterisk/translate.h>
#include <asterisk/utils.h>
#include <asterisk/dsp.h>
#define AST_MODULE "NVBackgroundDetect"
static char *app = "NVBackgroundDetect";
static char *synopsis = "Background a file with talk and fax detect (IAX and SIP too)";
static char *descrip =
" NVBackgroundDetect(filename[|options[|sildur[|mindur|[maxdur]]]]):\n"
"Plays filename, waiting for interruption from fax tones (on IAX and SIP too),\n"
"a digit, or non-silence. Audio is monitored in the receive direction. If\n"
"digits interrupt, they must be the start of a valid extension unless the\n"
"option is included to ignore. If fax is detected, it will jump to the\n"
"'fax' extension. If a period of non-silence is greater than 'mindur' ms,\n"
"yet less than 'maxdur' ms is followed by silence at least 'sildur' ms\n"
"then the app is aborted and processing jumps to the 'talk' extension.\n"
"If all undetected, control will continue at the next priority.\n"
" options:\n"
" 'n': Attempt on-hook if unanswered (default=no)\n"
" 'x': DTMF digits terminate without extension (default=no)\n"
" 'd': Ignore DTMF digit detection (default=no)\n"
" 'f': Ignore fax detection (default=no)\n"
" 't': Ignore talk detection (default=no)\n"
" sildur: Silence ms after mindur/maxdur before aborting (default=1000)\n"
" mindur: Minimum non-silence ms needed (default=100)\n"
" maxdur: Maximum non-silence ms allowed (default=0/forever)\n"
"Returns -1 on hangup, and 0 on successful completion with no exit conditions.\n\n"
"For questions or comments, please e-mail [email protected].\n";
// Use the second one for recent Asterisk releases
#define CALLERID_FIELD id.number.str
// #define CALLERID_FIELD cid.cid_num
//#define CALLERID_FIELD callerid
static int nv_background_detect_exec(struct ast_channel *chan, const char *data)
{
int res = 0;
char tmp[256] = "\0";
char *p = NULL;
char *filename = NULL;
char *options = NULL;
char *silstr = NULL;
char *minstr = NULL;
char *maxstr = NULL;
struct ast_frame *fr = NULL;
struct ast_frame *fr2 = NULL;
int notsilent = 0;
struct timeval start = {0, 0}, end = {0, 0};
int sildur = 1000;
int mindur = 100;
int maxdur = -1;
int skipanswer = 0;
int noextneeded = 0;
int ignoredtmf = 0;
int ignorefax = 0;
int ignoretalk = 0;
int x = 0;
struct ast_format* origrformat = NULL;
int features = 0;
struct ast_dsp *dsp = NULL;
struct ast_format_cap *cap;
struct ast_format linearFormat;
/* linear format capabilities */
ast_format_set(&linearFormat, AST_FORMAT_SLINEAR, 0);
cap = ast_format_cap_alloc_nolock();
ast_format_cap_add(cap, &linearFormat);
/* done */
pbx_builtin_setvar_helper(chan, "FAX_DETECTED", "");
pbx_builtin_setvar_helper(chan, "FAXEXTEN", "");
pbx_builtin_setvar_helper(chan, "DTMF_DETECTED", "");
pbx_builtin_setvar_helper(chan, "TALK_DETECTED", "");
if (!data || ast_strlen_zero(data)) {
ast_log(LOG_WARNING, "NVBackgroundDetect requires an argument (filename)\n");
return -1;
}
strncpy(tmp, data, sizeof(tmp)-1);
p = tmp;
filename = strsep(&p, ",");
options = strsep(&p, ",");
silstr = strsep(&p, ",");
minstr = strsep(&p, ",");
maxstr = strsep(&p, ",");
if (options) {
if (strchr(options, 'n'))
skipanswer = 1;
if (strchr(options, 'x'))
noextneeded = 1;
if (strchr(options, 'd'))
ignoredtmf = 1;
if (strchr(options, 'f'))
ignorefax = 1;
if (strchr(options, 't'))
ignoretalk = 1;
}
if (silstr) {
if ((sscanf(silstr, "%d", &x) == 1) && (x > 0))
sildur = x;
}
if (minstr) {
if ((sscanf(minstr, "%d", &x) == 1) && (x > 0))
mindur = x;
}
if (maxstr) {
if ((sscanf(maxstr, "%d", &x) == 1) && (x > 0))
maxdur = x;
}
ast_log(LOG_DEBUG, "Preparing detect of '%s' (sildur=%dms, mindur=%dms, maxdur=%dms)\n", tmp, sildur, mindur, maxdur);
// LOCAL_USER_ADD(u);
if (ast_channel_state(chan) != AST_STATE_UP && !skipanswer) {
/* Otherwise answer unless we're supposed to send this while on-hook */
res = ast_answer(chan);
}
if (!res) {
origrformat = ast_channel_readformat(chan); //chan->readformat;
// if ((res = ast_set_read_format(chan, AST_FORMAT_SLINEAR)))
// ast_log(LOG_WARNING, "Unable to set read format to linear!\n");
if ((res = ast_set_read_format_from_cap(chan, cap)) ){
ast_log(LOG_WARNING, "Unable to set read format to linear!\n");
}
}
if (!(dsp = ast_dsp_new())) {
ast_log(LOG_WARNING, "Unable to allocate DSP!\n");
res = -1;
}
if (dsp) {
if (!ignoretalk)
; /* features |= DSP_FEATURE_SILENCE_SUPPRESS; */
if (!ignorefax)
features |= DSP_FEATURE_FAX_DETECT;
//if (!ignoredtmf)
features |= DSP_FEATURE_DIGIT_DETECT;
ast_dsp_set_threshold(dsp, 256);
ast_dsp_set_features(dsp, features | DSP_DIGITMODE_RELAXDTMF);
ast_dsp_set_digitmode(dsp, DSP_DIGITMODE_DTMF);
}
if (!res) {
ast_stopstream(chan);
// res = ast_streamfile(chan, tmp, chan->language);
res = ast_streamfile(chan, tmp, ast_channel_language(chan));
if (!res) {
// while(chan->stream) {
while( ast_channel_stream(chan) ) {
// res = ast_sched_wait(chan->sched);
res = ast_sched_wait( ast_channel_sched(chan) );
// if ((res < 0) && !chan->timingfunc) {
if ((res < 0) && !ast_channel_timingfunc(chan)) {
res = 0;
break;
}
if (res < 0)
res = 1000;
res = ast_waitfor(chan, res);
if (res < 0) {
// ast_log(LOG_WARNING, "Waitfor failed on %s\n", chan->name);
ast_log(LOG_WARNING, "Waitfor failed on %s\n", ast_channel_name(chan));
break;
} else if (res > 0) {
fr = ast_read(chan);
if (!fr) {
ast_log(LOG_DEBUG, "Got hangup\n");
res = -1;
break;
}
fr2 = ast_dsp_process(chan, dsp, fr);
if (!fr2) {
ast_log(LOG_WARNING, "Bad DSP received (what happened!!)\n");
fr2 = fr;
}
if (fr2->frametype == AST_FRAME_DTMF) {
// if (fr2->subclass == 'f' && !ignorefax) {
if (fr2->subclass.integer == 'f' && !ignorefax) {
/* Fax tone -- Handle and return NULL */
// ast_log(LOG_DEBUG, "Fax detected on %s\n", chan->name);
ast_log(LOG_DEBUG, "Fax detected on %s\n", ast_channel_name(chan));
// if (strcmp(chan->exten, "fax")) {
if (strcmp(ast_channel_exten(chan), "fax")) {
ast_log(LOG_NOTICE, "Redirecting %s to fax extension\n", ast_channel_name(chan));
pbx_builtin_setvar_helper(chan, "FAX_DETECTED", "1");
// pbx_builtin_setvar_helper(chan,"FAXEXTEN",chan->exten);
pbx_builtin_setvar_helper(chan,"FAXEXTEN", ast_channel_exten(chan));
// if (ast_exists_extension(chan, chan->context, "fax", 1, chan->CALLERID_FIELD)) {
if (ast_exists_extension(chan, ast_channel_context(chan), "fax", 1, ast_channel_caller(chan)->id.number.str)) {
/* Save the DID/DNIS when we transfer the fax call to a "fax" extension */
// strncpy(chan->exten, "fax", sizeof(chan->exten)-1);
// chan->priority = 0;
ast_channel_exten_set(chan, "fax");
ast_channel_priority_set(chan, 0);
} else
ast_log(LOG_WARNING, "Fax detected, but no fax extension\n");
} else
ast_log(LOG_WARNING, "Already in a fax extension, not redirecting\n");
res = 0;
ast_frfree(fr);
break;
} else if (!ignoredtmf) {
ast_log(LOG_DEBUG, "DTMF detected on %s\n", ast_channel_name(chan));
char t[2];
// t[0] = fr2->subclass;
t[0] = fr2->subclass.integer;
t[1] = '\0';
// if (noextneeded || ast_canmatch_extension(chan, chan->context, t, 1, chan->CALLERID_FIELD)) {
if (noextneeded || ast_canmatch_extension(chan, ast_channel_context(chan), t, 1, ast_channel_caller(chan)->id.number.str)) {
pbx_builtin_setvar_helper(chan, "DTMF_DETECTED", "1");
/* They entered a valid extension, or might be anyhow */
if (noextneeded) {
ast_log(LOG_NOTICE, "DTMF received (not matching to exten)\n");
res = 0;
} else {
ast_log(LOG_NOTICE, "DTMF received (matching to exten)\n");
// res = fr2->subclass;
res = fr2->subclass.integer;
}
ast_frfree(fr);
break;
} else
ast_log(LOG_DEBUG, "Valid extension requested and DTMF did not match\n");
}
// } else if ((fr->frametype == AST_FRAME_VOICE) && (fr->subclass == AST_FORMAT_SLINEAR) && !ignoretalk) {
} else if ((fr->frametype == AST_FRAME_VOICE) && ( ast_format_cap_iscompatible(cap, &fr->subclass.format)) && !ignoretalk) {
int totalsilence;
int ms;
res = ast_dsp_silence(dsp, fr, &totalsilence);
if (res && (totalsilence > sildur)) {
/* We've been quiet a little while */
if (notsilent) {
/* We had heard some talking */
gettimeofday(&end, NULL);
ms = (end.tv_sec - start.tv_sec) * 1000;
ms += (end.tv_usec - start.tv_usec) / 1000;
ms -= sildur;
if (ms < 0)
ms = 0;
if ((ms > mindur) && ((maxdur < 0) || (ms < maxdur))) {
char ms_str[10];
ast_log(LOG_DEBUG, "Found qualified token of %d ms\n", ms);
// ast_log(LOG_NOTICE, "Redirecting %s to talk extension\n", chan->name);
ast_log(LOG_NOTICE, "Redirecting %s to talk extension\n", ast_channel_name(chan));
/* Save detected talk time (in milliseconds) */
sprintf(ms_str, "%d", ms);
pbx_builtin_setvar_helper(chan, "TALK_DETECTED", ms_str);
// if (ast_exists_extension(chan, chan->context, "talk", 1, chan->CALLERID_FIELD)) {
if (ast_exists_extension(chan, ast_channel_context(chan), "talk", 1, ast_channel_caller(chan)->id.number.str)) {
// strncpy(chan->exten, "talk", sizeof(chan->exten) - 1);
// chan->priority = 0;
ast_channel_exten_set(chan, "talk");
ast_channel_priority_set(chan, 0);
} else
ast_log(LOG_WARNING, "Talk detected, but no talk extension\n");
res = 0;
ast_frfree(fr);
break;
} else
ast_log(LOG_DEBUG, "Found unqualified token of %d ms\n", ms);
notsilent = 0;
}
} else {
if (!notsilent) {
/* Heard some audio, mark the begining of the token */
gettimeofday(&start, NULL);
ast_log(LOG_DEBUG, "Start of voice token!\n");
notsilent = 1;
}
}
}
ast_frfree(fr);
}
// ast_sched_runq(chan->sched);
ast_sched_runq( ast_channel_sched(chan) );
}
ast_stopstream(chan);
} else {
// ast_log(LOG_WARNING, "ast_streamfile failed on %s for %s\n", chan->name, (char *)data);
ast_log(LOG_WARNING, "ast_streamfile failed on %s for %s\n", ast_channel_name(chan), (char *)data);
res = 0;
}
} else
// ast_log(LOG_WARNING, "Could not answer channel '%s'\n", chan->name);
ast_log(LOG_WARNING, "Could not answer channel '%s'\n", ast_channel_name(chan));
if (res > -1) {
if (origrformat && ast_set_read_format(chan, origrformat)) {
// ast_log(LOG_WARNING, "Failed to restore read format for %s to %s\n", chan->name, ast_getformatname(origrformat));
ast_log(LOG_WARNING, "Failed to restore read format for %s to %s\n", ast_channel_name(chan), ast_getformatname(origrformat));
}
}
if (dsp)
ast_dsp_free(dsp);
// LOCAL_USER_REMOVE(u);
ast_format_cap_destroy(cap);
return res;
}
static int unload_module(void)
{
// STANDARD_HANGUP_LOCALUSERS;
return ast_unregister_application(app);
}
static int load_module(void)
{
return ast_register_application(app, nv_background_detect_exec, synopsis, descrip);
}
AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Fax playing Background Music Detection Application");