2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2005, Digium, Inc.
6 * Mark Spencer <markster@digium.com>
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
21 * \brief Playback a file with audio detect
23 * \author Mark Spencer <markster@digium.com>
25 * \ingroup applications
30 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
32 #include "asterisk/lock.h"
33 #include "asterisk/file.h"
34 #include "asterisk/channel.h"
35 #include "asterisk/pbx.h"
36 #include "asterisk/module.h"
37 #include "asterisk/translate.h"
38 #include "asterisk/utils.h"
39 #include "asterisk/dsp.h"
40 #include "asterisk/app.h"
42 static char *app = "BackgroundDetect";
44 static char *synopsis = "Background a file with talk detect";
46 static char *descrip =
47 " BackgroundDetect(<filename>[,<sil>[,<min>[,<max>[,<analysistime>]]]]):\n"
48 "Plays back <filename>, waiting for interruption from a given digit (the digit\n"
49 "must start the beginning of a valid extension, or it will be ignored). During\n"
50 "the playback of the file, audio is monitored in the receive direction, and if\n"
51 "a period of non-silence which is greater than <min> ms yet less than <max> ms\n"
52 "is followed by silence for at least <sil> ms, which occurs during the first\n"
53 "<analysistime> ms, then the audio playback is aborted and processing jumps to\n"
54 "the <talk> extension, if available. If unspecified, <sil>, <min>, <max>, and\n"
55 "<analysistime> default to 1000, 100, infinity, and infinity respectively.\n";
58 static int background_detect_exec(struct ast_channel *chan, void *data)
64 struct timeval start = { 0, 0 };
65 struct timeval detection_start = { 0, 0 };
69 int analysistime = -1;
70 int continue_analysis = 1;
73 struct ast_dsp *dsp = NULL;
74 AST_DECLARE_APP_ARGS(args,
75 AST_APP_ARG(filename);
79 AST_APP_ARG(analysistime);
82 if (ast_strlen_zero(data)) {
83 ast_log(LOG_WARNING, "BackgroundDetect requires an argument (filename)\n");
87 tmp = ast_strdupa(data);
88 AST_STANDARD_APP_ARGS(args, tmp);
90 if (!ast_strlen_zero(args.silence) && (sscanf(args.silence, "%d", &x) == 1) && (x > 0)) {
93 if (!ast_strlen_zero(args.min) && (sscanf(args.min, "%d", &x) == 1) && (x > 0)) {
96 if (!ast_strlen_zero(args.max) && (sscanf(args.max, "%d", &x) == 1) && (x > 0)) {
99 if (!ast_strlen_zero(args.analysistime) && (sscanf(args.analysistime, "%d", &x) == 1) && (x > 0)) {
103 ast_debug(1, "Preparing detect of '%s', sil=%d, min=%d, max=%d, analysistime=%d\n", args.filename, sil, min, max, analysistime);
105 if (chan->_state != AST_STATE_UP) {
106 if ((res = ast_answer(chan))) {
111 origrformat = chan->readformat;
112 if ((ast_set_read_format(chan, AST_FORMAT_SLINEAR))) {
113 ast_log(LOG_WARNING, "Unable to set read format to linear!\n");
118 if (!(dsp = ast_dsp_new())) {
119 ast_log(LOG_WARNING, "Unable to allocate DSP!\n");
123 ast_stopstream(chan);
124 if (ast_streamfile(chan, tmp, chan->language)) {
125 ast_log(LOG_WARNING, "ast_streamfile failed on %s for %s\n", chan->name, (char *)data);
128 detection_start = ast_tvnow();
129 while (chan->stream) {
130 res = ast_sched_wait(chan->sched);
131 if ((res < 0) && !chan->timingfunc) {
138 res = ast_waitfor(chan, res);
140 ast_log(LOG_WARNING, "Waitfor failed on %s\n", chan->name);
142 } else if (res > 0) {
144 if (continue_analysis && analysistime >= 0) {
145 /* If we have a limit for the time to analyze voice
146 * frames and the time has not expired */
147 if (ast_tvdiff_ms(ast_tvnow(), detection_start) >= analysistime) {
148 continue_analysis = 0;
149 ast_verb(3, "BackgroundDetect: Talk analysis time complete on %s.\n", chan->name);
156 } else if (fr->frametype == AST_FRAME_DTMF) {
160 if (ast_canmatch_extension(chan, chan->context, t, 1, chan->cid.cid_num)) {
161 /* They entered a valid extension, or might be anyhow */
166 } else if ((fr->frametype == AST_FRAME_VOICE) && (fr->subclass == AST_FORMAT_SLINEAR) && continue_analysis) {
169 res = ast_dsp_silence(dsp, fr, &totalsilence);
170 if (res && (totalsilence > sil)) {
171 /* We've been quiet a little while */
173 /* We had heard some talking */
174 ms = ast_tvdiff_ms(ast_tvnow(), start);
178 if ((ms > min) && ((max < 0) || (ms < max))) {
180 ast_debug(1, "Found qualified token of %d ms\n", ms);
182 /* Save detected talk time (in milliseconds) */
183 snprintf(ms_str, sizeof(ms_str), "%d", ms);
184 pbx_builtin_setvar_helper(chan, "TALK_DETECTED", ms_str);
186 ast_goto_if_exists(chan, chan->context, "talk", 1);
191 ast_debug(1, "Found unqualified token of %d ms\n", ms);
197 /* Heard some audio, mark the begining of the token */
199 ast_debug(1, "Start of voice token!\n");
206 ast_sched_runq(chan->sched);
208 ast_stopstream(chan);
212 if (origrformat && ast_set_read_format(chan, origrformat)) {
213 ast_log(LOG_WARNING, "Failed to restore read format for %s to %s\n",
214 chan->name, ast_getformatname(origrformat));
223 static int unload_module(void)
225 return ast_unregister_application(app);
228 static int load_module(void)
230 return ast_register_application(app, background_detect_exec, synopsis, descrip);
233 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Playback with Talk Detection");