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: 276347 $")
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, const char *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.integer;
00177 t[1] = '\0';
00178 if (ast_canmatch_extension(chan, chan->context, t, 1,
00179 S_COR(chan->caller.id.number.valid, chan->caller.id.number.str, NULL))) {
00180
00181 res = fr->subclass.integer;
00182 ast_frfree(fr);
00183 break;
00184 }
00185 } else if ((fr->frametype == AST_FRAME_VOICE) && (fr->subclass.codec == AST_FORMAT_SLINEAR) && continue_analysis) {
00186 int totalsilence;
00187 int ms;
00188 res = ast_dsp_silence(dsp, fr, &totalsilence);
00189 if (res && (totalsilence > sil)) {
00190
00191 if (notsilent) {
00192
00193 ms = ast_tvdiff_ms(ast_tvnow(), start);
00194 ms -= sil;
00195 if (ms < 0)
00196 ms = 0;
00197 if ((ms > min) && ((max < 0) || (ms < max))) {
00198 char ms_str[12];
00199 ast_debug(1, "Found qualified token of %d ms\n", ms);
00200
00201
00202 snprintf(ms_str, sizeof(ms_str), "%d", ms);
00203 pbx_builtin_setvar_helper(chan, "TALK_DETECTED", ms_str);
00204
00205 ast_goto_if_exists(chan, chan->context, "talk", 1);
00206 res = 0;
00207 ast_frfree(fr);
00208 break;
00209 } else {
00210 ast_debug(1, "Found unqualified token of %d ms\n", ms);
00211 }
00212 notsilent = 0;
00213 }
00214 } else {
00215 if (!notsilent) {
00216
00217 start = ast_tvnow();
00218 ast_debug(1, "Start of voice token!\n");
00219 notsilent = 1;
00220 }
00221 }
00222 }
00223 ast_frfree(fr);
00224 }
00225 ast_sched_runq(chan->sched);
00226 }
00227 ast_stopstream(chan);
00228 } while (0);
00229
00230 if (res > -1) {
00231 if (origrformat && ast_set_read_format(chan, origrformat)) {
00232 ast_log(LOG_WARNING, "Failed to restore read format for %s to %s\n",
00233 chan->name, ast_getformatname(origrformat));
00234 }
00235 }
00236 if (dsp) {
00237 ast_dsp_free(dsp);
00238 }
00239 return res;
00240 }
00241
00242 static int unload_module(void)
00243 {
00244 return ast_unregister_application(app);
00245 }
00246
00247 static int load_module(void)
00248 {
00249 return ast_register_application_xml(app, background_detect_exec);
00250 }
00251
00252 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Playback with Talk Detection");