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 #include "asterisk.h"
00029
00030 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 211580 $")
00031
00032 #include "asterisk/lock.h"
00033 #include "asterisk/file.h"
00034 #include "asterisk/channel.h"
00035 #include "asterisk/pbx.h"
00036 #include "asterisk/module.h"
00037 #include "asterisk/translate.h"
00038 #include "asterisk/utils.h"
00039 #include "asterisk/dsp.h"
00040 #include "asterisk/app.h"
00041
00042
00043
00044
00045
00046
00047
00048
00049
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 static char *app = "BackgroundDetect";
00075
00076 static int background_detect_exec(struct ast_channel *chan, void *data)
00077 {
00078 int res = 0;
00079 char *tmp;
00080 struct ast_frame *fr;
00081 int notsilent = 0;
00082 struct timeval start = { 0, 0 };
00083 struct timeval detection_start = { 0, 0 };
00084 int sil = 1000;
00085 int min = 100;
00086 int max = -1;
00087 int analysistime = -1;
00088 int continue_analysis = 1;
00089 int x;
00090 int origrformat = 0;
00091 struct ast_dsp *dsp = NULL;
00092 AST_DECLARE_APP_ARGS(args,
00093 AST_APP_ARG(filename);
00094 AST_APP_ARG(silence);
00095 AST_APP_ARG(min);
00096 AST_APP_ARG(max);
00097 AST_APP_ARG(analysistime);
00098 );
00099
00100 if (ast_strlen_zero(data)) {
00101 ast_log(LOG_WARNING, "BackgroundDetect requires an argument (filename)\n");
00102 return -1;
00103 }
00104
00105 tmp = ast_strdupa(data);
00106 AST_STANDARD_APP_ARGS(args, tmp);
00107
00108 if (!ast_strlen_zero(args.silence) && (sscanf(args.silence, "%30d", &x) == 1) && (x > 0)) {
00109 sil = x;
00110 }
00111 if (!ast_strlen_zero(args.min) && (sscanf(args.min, "%30d", &x) == 1) && (x > 0)) {
00112 min = x;
00113 }
00114 if (!ast_strlen_zero(args.max) && (sscanf(args.max, "%30d", &x) == 1) && (x > 0)) {
00115 max = x;
00116 }
00117 if (!ast_strlen_zero(args.analysistime) && (sscanf(args.analysistime, "%30d", &x) == 1) && (x > 0)) {
00118 analysistime = x;
00119 }
00120
00121 ast_debug(1, "Preparing detect of '%s', sil=%d, min=%d, max=%d, analysistime=%d\n", args.filename, sil, min, max, analysistime);
00122 do {
00123 if (chan->_state != AST_STATE_UP) {
00124 if ((res = ast_answer(chan))) {
00125 break;
00126 }
00127 }
00128
00129 origrformat = chan->readformat;
00130 if ((ast_set_read_format(chan, AST_FORMAT_SLINEAR))) {
00131 ast_log(LOG_WARNING, "Unable to set read format to linear!\n");
00132 res = -1;
00133 break;
00134 }
00135
00136 if (!(dsp = ast_dsp_new())) {
00137 ast_log(LOG_WARNING, "Unable to allocate DSP!\n");
00138 res = -1;
00139 break;
00140 }
00141 ast_stopstream(chan);
00142 if (ast_streamfile(chan, tmp, chan->language)) {
00143 ast_log(LOG_WARNING, "ast_streamfile failed on %s for %s\n", chan->name, (char *)data);
00144 break;
00145 }
00146 detection_start = ast_tvnow();
00147 while (chan->stream) {
00148 res = ast_sched_wait(chan->sched);
00149 if ((res < 0) && !chan->timingfunc) {
00150 res = 0;
00151 break;
00152 }
00153 if (res < 0) {
00154 res = 1000;
00155 }
00156 res = ast_waitfor(chan, res);
00157 if (res < 0) {
00158 ast_log(LOG_WARNING, "Waitfor failed on %s\n", chan->name);
00159 break;
00160 } else if (res > 0) {
00161 fr = ast_read(chan);
00162 if (continue_analysis && analysistime >= 0) {
00163
00164
00165 if (ast_tvdiff_ms(ast_tvnow(), detection_start) >= analysistime) {
00166 continue_analysis = 0;
00167 ast_verb(3, "BackgroundDetect: Talk analysis time complete on %s.\n", chan->name);
00168 }
00169 }
00170
00171 if (!fr) {
00172 res = -1;
00173 break;
00174 } else if (fr->frametype == AST_FRAME_DTMF) {
00175 char t[2];
00176 t[0] = fr->subclass;
00177 t[1] = '\0';
00178 if (ast_canmatch_extension(chan, chan->context, t, 1, chan->cid.cid_num)) {
00179
00180 res = fr->subclass;
00181 ast_frfree(fr);
00182 break;
00183 }
00184 } else if ((fr->frametype == AST_FRAME_VOICE) && (fr->subclass == AST_FORMAT_SLINEAR) && continue_analysis) {
00185 int totalsilence;
00186 int ms;
00187 res = ast_dsp_silence(dsp, fr, &totalsilence);
00188 if (res && (totalsilence > sil)) {
00189
00190 if (notsilent) {
00191
00192 ms = ast_tvdiff_ms(ast_tvnow(), start);
00193 ms -= sil;
00194 if (ms < 0)
00195 ms = 0;
00196 if ((ms > min) && ((max < 0) || (ms < max))) {
00197 char ms_str[12];
00198 ast_debug(1, "Found qualified token of %d ms\n", ms);
00199
00200
00201 snprintf(ms_str, sizeof(ms_str), "%d", ms);
00202 pbx_builtin_setvar_helper(chan, "TALK_DETECTED", ms_str);
00203
00204 ast_goto_if_exists(chan, chan->context, "talk", 1);
00205 res = 0;
00206 ast_frfree(fr);
00207 break;
00208 } else {
00209 ast_debug(1, "Found unqualified token of %d ms\n", ms);
00210 }
00211 notsilent = 0;
00212 }
00213 } else {
00214 if (!notsilent) {
00215
00216 start = ast_tvnow();
00217 ast_debug(1, "Start of voice token!\n");
00218 notsilent = 1;
00219 }
00220 }
00221 }
00222 ast_frfree(fr);
00223 }
00224 ast_sched_runq(chan->sched);
00225 }
00226 ast_stopstream(chan);
00227 } while (0);
00228
00229 if (res > -1) {
00230 if (origrformat && ast_set_read_format(chan, origrformat)) {
00231 ast_log(LOG_WARNING, "Failed to restore read format for %s to %s\n",
00232 chan->name, ast_getformatname(origrformat));
00233 }
00234 }
00235 if (dsp) {
00236 ast_dsp_free(dsp);
00237 }
00238 return res;
00239 }
00240
00241 static int unload_module(void)
00242 {
00243 return ast_unregister_application(app);
00244 }
00245
00246 static int load_module(void)
00247 {
00248 return ast_register_application_xml(app, background_detect_exec);
00249 }
00250
00251 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Playback with Talk Detection");