Add an option, specifying maximum analysis time for talk detection.
[asterisk/asterisk.git] / apps / app_talkdetect.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999 - 2005, Digium, Inc.
5  *
6  * Mark Spencer <markster@digium.com>
7  *
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.
13  *
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.
17  */
18
19 /*! \file
20  *
21  * \brief Playback a file with audio detect
22  *
23  * \author Mark Spencer <markster@digium.com>
24  * 
25  * \ingroup applications
26  */
27  
28 #include "asterisk.h"
29
30 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
31
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"
41
42 static char *app = "BackgroundDetect";
43
44 static char *synopsis = "Background a file with talk detect";
45
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";
56
57
58 static int background_detect_exec(struct ast_channel *chan, void *data)
59 {
60         int res = 0;
61         char *tmp;
62         struct ast_frame *fr;
63         int notsilent = 0;
64         struct timeval start = { 0, 0 };
65         struct timeval detection_start = { 0, 0 };
66         int sil = 1000;
67         int min = 100;
68         int max = -1;
69         int analysistime = -1;
70         int continue_analysis = 1;
71         int x;
72         int origrformat = 0;
73         struct ast_dsp *dsp = NULL;
74         AST_DECLARE_APP_ARGS(args,
75                 AST_APP_ARG(filename);
76                 AST_APP_ARG(silence);
77                 AST_APP_ARG(min);
78                 AST_APP_ARG(max);
79                 AST_APP_ARG(analysistime);
80         );
81         
82         if (ast_strlen_zero(data)) {
83                 ast_log(LOG_WARNING, "BackgroundDetect requires an argument (filename)\n");
84                 return -1;
85         }
86
87         tmp = ast_strdupa(data);
88         AST_STANDARD_APP_ARGS(args, tmp);
89
90         if (!ast_strlen_zero(args.silence) && (sscanf(args.silence, "%d", &x) == 1) && (x > 0)) {
91                 sil = x;
92         }
93         if (!ast_strlen_zero(args.min) && (sscanf(args.min, "%d", &x) == 1) && (x > 0)) {
94                 min = x;
95         }
96         if (!ast_strlen_zero(args.max) && (sscanf(args.max, "%d", &x) == 1) && (x > 0)) {
97                 max = x;
98         }
99         if (!ast_strlen_zero(args.analysistime) && (sscanf(args.analysistime, "%d", &x) == 1) && (x > 0)) {
100                 analysistime = x;
101         }
102
103         ast_debug(1, "Preparing detect of '%s', sil=%d, min=%d, max=%d, analysistime=%d\n", args.filename, sil, min, max, analysistime);
104         do {
105                 if (chan->_state != AST_STATE_UP) {
106                         if ((res = ast_answer(chan))) {
107                                 break;
108                         }
109                 }
110
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");
114                         res = -1;
115                         break;
116                 }
117
118                 if (!(dsp = ast_dsp_new())) {
119                         ast_log(LOG_WARNING, "Unable to allocate DSP!\n");
120                         res = -1;
121                         break;
122                 }
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);
126                         break;
127                 }
128                 detection_start = ast_tvnow();
129                 while (chan->stream) {
130                         res = ast_sched_wait(chan->sched);
131                         if ((res < 0) && !chan->timingfunc) {
132                                 res = 0;
133                                 break;
134                         }
135                         if (res < 0) {
136                                 res = 1000;
137                         }
138                         res = ast_waitfor(chan, res);
139                         if (res < 0) {
140                                 ast_log(LOG_WARNING, "Waitfor failed on %s\n", chan->name);
141                                 break;
142                         } else if (res > 0) {
143                                 fr = ast_read(chan);
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);
150                                         }
151                                 }
152                                 
153                                 if (!fr) {
154                                         res = -1;
155                                         break;
156                                 } else if (fr->frametype == AST_FRAME_DTMF) {
157                                         char t[2];
158                                         t[0] = fr->subclass;
159                                         t[1] = '\0';
160                                         if (ast_canmatch_extension(chan, chan->context, t, 1, chan->cid.cid_num)) {
161                                                 /* They entered a valid  extension, or might be anyhow */
162                                                 res = fr->subclass;
163                                                 ast_frfree(fr);
164                                                 break;
165                                         }
166                                 } else if ((fr->frametype == AST_FRAME_VOICE) && (fr->subclass == AST_FORMAT_SLINEAR) && continue_analysis) {
167                                         int totalsilence;
168                                         int ms;
169                                         res = ast_dsp_silence(dsp, fr, &totalsilence);
170                                         if (res && (totalsilence > sil)) {
171                                                 /* We've been quiet a little while */
172                                                 if (notsilent) {
173                                                         /* We had heard some talking */
174                                                         ms = ast_tvdiff_ms(ast_tvnow(), start);
175                                                         ms -= sil;
176                                                         if (ms < 0)
177                                                                 ms = 0;
178                                                         if ((ms > min) && ((max < 0) || (ms < max))) {
179                                                                 char ms_str[12];
180                                                                 ast_debug(1, "Found qualified token of %d ms\n", ms);
181
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);
185
186                                                                 ast_goto_if_exists(chan, chan->context, "talk", 1);
187                                                                 res = 0;
188                                                                 ast_frfree(fr);
189                                                                 break;
190                                                         } else {
191                                                                 ast_debug(1, "Found unqualified token of %d ms\n", ms);
192                                                         }
193                                                         notsilent = 0;
194                                                 }
195                                         } else {
196                                                 if (!notsilent) {
197                                                         /* Heard some audio, mark the begining of the token */
198                                                         start = ast_tvnow();
199                                                         ast_debug(1, "Start of voice token!\n");
200                                                         notsilent = 1;
201                                                 }
202                                         }
203                                 }
204                                 ast_frfree(fr);
205                         }
206                         ast_sched_runq(chan->sched);
207                 }
208                 ast_stopstream(chan);
209         } while (0);
210
211         if (res > -1) {
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));
215                 }
216         }
217         if (dsp) {
218                 ast_dsp_free(dsp);
219         }
220         return res;
221 }
222
223 static int unload_module(void)
224 {
225         return ast_unregister_application(app);
226 }
227
228 static int load_module(void)
229 {
230         return ast_register_application(app, background_detect_exec, synopsis, descrip);
231 }
232
233 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Playback with Talk Detection");