| 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 |
#include "asterisk.h" |
| 33 |
|
| 34 |
#include "asterisk/lock.h" |
| 35 |
#include "asterisk/file.h" |
| 36 |
#include "asterisk/channel.h" |
| 37 |
#include "asterisk/pbx.h" |
| 38 |
#include "asterisk/module.h" |
| 39 |
#include "asterisk/translate.h" |
| 40 |
#include "asterisk/utils.h" |
| 41 |
#include "asterisk/dsp.h" |
| 42 |
#include "asterisk/app.h" |
| 43 |
#include "asterisk/format.h" |
| 44 |
#include "asterisk/format_cache.h" |
| 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 |
static char *app = "BackgroundDetect"; |
| 82 |
|
| 83 |
static int background_detect_exec(struct ast_channel *chan, const char *data) |
| 84 |
{ |
| 85 |
int res = 0; |
| 86 |
char *tmp; |
| 87 |
struct ast_frame *fr; |
| 88 |
int notsilent = 0; |
| 89 |
struct timeval start = { 0, 0 }; |
| 90 |
struct timeval detection_start = { 0, 0 }; |
| 91 |
int sil = 1000; |
| 92 |
int min = 100; |
| 93 |
int max = -1; |
| 94 |
int analysistime = -1; |
| 95 |
int continue_analysis = 1; |
| 96 |
int x; |
| 97 |
RAII_VAR(struct ast_format *, origrformat, NULL, ao2_cleanup); |
| 98 |
struct ast_dsp *dsp = NULL; |
| 99 |
AST_DECLARE_APP_ARGS(args, |
| 100 |
AST_APP_ARG(filename); |
| 101 |
AST_APP_ARG(silence); |
| 102 |
AST_APP_ARG(min); |
| 103 |
AST_APP_ARG(max); |
| 104 |
AST_APP_ARG(analysistime); |
| 105 |
); |
| 106 |
|
| 107 |
if (ast_strlen_zero(data)) { |
| 108 |
ast_log(LOG_WARNING, "BackgroundDetect requires an argument (filename)\n"); |
| 109 |
return -1; |
| 110 |
} |
| 111 |
|
| 112 |
tmp = ast_strdupa(data); |
| 113 |
AST_STANDARD_APP_ARGS(args, tmp); |
| 114 |
|
| 115 |
if (!ast_strlen_zero(args.silence) && (sscanf(args.silence, "%30d", &x) == 1) && (x > 0)) { |
| 116 |
sil = x; |
| 117 |
} |
| 118 |
if (!ast_strlen_zero(args.min) && (sscanf(args.min, "%30d", &x) == 1) && (x > 0)) { |
| 119 |
min = x; |
| 120 |
} |
| 121 |
if (!ast_strlen_zero(args.max) && (sscanf(args.max, "%30d", &x) == 1) && (x > 0)) { |
| 122 |
max = x; |
| 123 |
} |
| 124 |
if (!ast_strlen_zero(args.analysistime) && (sscanf(args.analysistime, "%30d", &x) == 1) && (x > 0)) { |
| 125 |
analysistime = x; |
| 126 |
} |
| 127 |
|
| 128 |
ast_debug(1, "Preparing detect of '%s', sil=%d, min=%d, max=%d, analysistime=%d\n", args.filename, sil, min, max, analysistime); |
| 129 |
do { |
| 130 |
if (ast_channel_state(chan) != AST_STATE_UP) { |
| 131 |
if ((res = ast_answer(chan))) { |
| 132 |
break; |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
origrformat = ao2_bump(ast_channel_readformat(chan)); |
| 137 |
if ((ast_set_read_format(chan, ast_format_slin))) { |
| 138 |
ast_log(LOG_WARNING, "Unable to set read format to linear!\n"); |
| 139 |
res = -1; |
| 140 |
break; |
| 141 |
} |
| 142 |
|
| 143 |
if (!(dsp = ast_dsp_new())) { |
| 144 |
ast_log(LOG_WARNING, "Unable to allocate DSP!\n"); |
| 145 |
res = -1; |
| 146 |
break; |
| 147 |
} |
| 148 |
ast_stopstream(chan); |
| 149 |
if (ast_streamfile(chan, tmp, ast_channel_language(chan))) { |
| 150 |
ast_log(LOG_WARNING, "ast_streamfile failed on %s for %s\n", ast_channel_name(chan), (char *)data); |
| 151 |
break; |
| 152 |
} |
| 153 |
detection_start = ast_tvnow(); |
| 154 |
while (ast_channel_stream(chan)) { |
| 155 |
res = ast_sched_wait(ast_channel_sched(chan)); |
| 156 |
if ((res < 0) && !ast_channel_timingfunc(chan)) { |
| 157 |
res = 0; |
| 158 |
break; |
| 159 |
} |
| 160 |
if (res < 0) { |
| 161 |
res = 1000; |
| 162 |
} |
| 163 |
res = ast_waitfor(chan, res); |
| 164 |
if (res < 0) { |
| 165 |
ast_log(LOG_WARNING, "Waitfor failed on %s\n", ast_channel_name(chan)); |
| 166 |
break; |
| 167 |
} else if (res > 0) { |
| 168 |
fr = ast_read(chan); |
| 169 |
if (continue_analysis && analysistime >= 0) { |
| 170 |
|
| 171 |
|
| 172 |
if (ast_tvdiff_ms(ast_tvnow(), detection_start) >= analysistime) { |
| 173 |
continue_analysis = 0; |
| 174 |
ast_verb(3, "BackgroundDetect: Talk analysis time complete on %s.\n", ast_channel_name(chan)); |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
if (!fr) { |
| 179 |
res = -1; |
| 180 |
break; |
| 181 |
} else if (fr->frametype == AST_FRAME_DTMF) { |
| 182 |
char t[2]; |
| 183 |
t[0] = fr->subclass.integer; |
| 184 |
t[1] = '\0'; |
| 185 |
if (ast_canmatch_extension(chan, ast_channel_context(chan), t, 1, |
| 186 |
S_COR(ast_channel_caller(chan)->id.number.valid, ast_channel_caller(chan)->id.number.str, NULL))) { |
| 187 |
|
| 188 |
res = fr->subclass.integer; |
| 189 |
ast_frfree(fr); |
| 190 |
break; |
| 191 |
} |
| 192 |
} else if ((fr->frametype == AST_FRAME_VOICE) && |
| 193 |
(ast_format_cmp(fr->subclass.format, ast_format_slin) == AST_FORMAT_CMP_EQUAL) && continue_analysis) { |
| 194 |
int totalsilence; |
| 195 |
int ms; |
| 196 |
res = ast_dsp_silence(dsp, fr, &totalsilence); |
| 197 |
if (res && (totalsilence > sil)) { |
| 198 |
|
| 199 |
if (notsilent) { |
| 200 |
|
| 201 |
ms = ast_tvdiff_ms(ast_tvnow(), start); |
| 202 |
ms -= sil; |
| 203 |
if (ms < 0) |
| 204 |
ms = 0; |
| 205 |
if ((ms > min) && ((max < 0) || (ms < max))) { |
| 206 |
char ms_str[12]; |
| 207 |
ast_debug(1, "Found qualified token of %d ms\n", ms); |
| 208 |
|
| 209 |
|
| 210 |
snprintf(ms_str, sizeof(ms_str), "%d", ms); |
| 211 |
pbx_builtin_setvar_helper(chan, "TALK_DETECTED", ms_str); |
| 212 |
|
| 213 |
ast_goto_if_exists(chan, ast_channel_context(chan), "talk", 1); |
| 214 |
res = 0; |
| 215 |
ast_frfree(fr); |
| 216 |
break; |
| 217 |
} else { |
| 218 |
ast_debug(1, "Found unqualified token of %d ms\n", ms); |
| 219 |
} |
| 220 |
notsilent = 0; |
| 221 |
} |
| 222 |
} else { |
| 223 |
if (!notsilent) { |
| 224 |
|
| 225 |
start = ast_tvnow(); |
| 226 |
ast_debug(1, "Start of voice token!\n"); |
| 227 |
notsilent = 1; |
| 228 |
} |
| 229 |
} |
| 230 |
} |
| 231 |
ast_frfree(fr); |
| 232 |
} |
| 233 |
ast_sched_runq(ast_channel_sched(chan)); |
| 234 |
} |
| 235 |
ast_stopstream(chan); |
| 236 |
} while (0); |
| 237 |
|
| 238 |
if (res > -1) { |
| 239 |
if (origrformat && ast_set_read_format(chan, origrformat)) { |
| 240 |
ast_log(LOG_WARNING, "Failed to restore read format for %s to %s\n", |
| 241 |
ast_channel_name(chan), ast_format_get_name(origrformat)); |
| 242 |
} |
| 243 |
} |
| 244 |
if (dsp) { |
| 245 |
ast_dsp_free(dsp); |
| 246 |
} |
| 247 |
return res; |
| 248 |
} |
| 249 |
|
| 250 |
static int unload_module(void) |
| 251 |
{ |
| 252 |
return ast_unregister_application(app); |
| 253 |
} |
| 254 |
|
| 255 |
static int load_module(void) |
| 256 |
{ |
| 257 |
return ast_register_application_xml(app, background_detect_exec); |
| 258 |
} |
| 259 |
|
| 260 |
AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Playback with Talk Detection"); |