Add TALK_DETECTED variable (bug #2628)
[asterisk/asterisk.git] / apps / app_talkdetect.c
1 /*
2  * Asterisk -- A telephony toolkit for Linux.
3  *
4  * Playback a file with audio detect
5  * 
6  * Copyright (C) 2004, Digium, Inc.
7  *
8  * Mark Spencer <markster@digium.com>
9  *
10  * This program is free software, distributed under the terms of
11  * the GNU General Public License
12  */
13  
14 #include <asterisk/lock.h>
15 #include <asterisk/file.h>
16 #include <asterisk/logger.h>
17 #include <asterisk/channel.h>
18 #include <asterisk/pbx.h>
19 #include <asterisk/module.h>
20 #include <asterisk/translate.h>
21 #include <asterisk/utils.h>
22 #include <asterisk/dsp.h>
23 #include <string.h>
24 #include <stdlib.h>
25
26 static char *tdesc = "Playback with Talk Detection";
27
28 static char *app = "BackgroundDetect";
29
30 static char *synopsis = "Background a file with talk detect";
31
32 static char *descrip = 
33 "  BackgroundDetect(filename[|sil[|min|[max]]]):  Plays  back  a  given\n"
34 "filename, waiting for interruption from a given digit (the digit must\n"
35 "start the beginning of a valid extension, or it will be ignored).\n"
36 "During the playback of the file, audio is monitored in the receive\n"
37 "direction, and if a period of non-silence which is greater than 'min' ms\n"
38 "yet less than 'max' ms is followed by silence for at least 'sil' ms then\n"
39 "the audio playback is aborted and processing jumps to the 'talk' extension\n"
40 "if available.  If unspecified, sil, min, and max default to 1000, 100, and\n"
41 "infinity respectively.  Returns -1 on hangup, and 0 on successful playback\n"
42 "completion with no exit conditions.\n";
43
44 STANDARD_LOCAL_USER;
45
46 LOCAL_USER_DECL;
47
48 static int background_detect_exec(struct ast_channel *chan, void *data)
49 {
50         int res = 0;
51         struct localuser *u;
52         char tmp[256];
53         char *options;
54         char *stringp;
55         struct ast_frame *fr;
56         int notsilent=0;
57         struct timeval start = { 0, 0}, end = {0, 0};
58         int sil = 1000;
59         int min = 100;
60         int max = -1;
61         int x;
62         int origrformat=0;
63         struct ast_dsp *dsp;
64         if (!data || ast_strlen_zero((char *)data)) {
65                 ast_log(LOG_WARNING, "BackgroundDetect requires an argument (filename)\n");
66                 return -1;
67         }
68         strncpy(tmp, (char *)data, sizeof(tmp)-1);
69         stringp=tmp;
70         strsep(&stringp, "|");
71         options = strsep(&stringp, "|");
72         if (options) {
73                 if ((sscanf(options, "%d", &x) == 1) && (x > 0))
74                         sil = x;
75                 options = strsep(&stringp, "|");
76                 if (options) {
77                         if ((sscanf(options, "%d", &x) == 1) && (x > 0))
78                                 min = x;
79                         options = strsep(&stringp, "|");
80                         if (options) {
81                                 if ((sscanf(options, "%d", &x) == 1) && (x > 0))
82                                         max = x;
83                         }
84                 }
85         }
86         ast_log(LOG_DEBUG, "Preparing detect of '%s', sil=%d,min=%d,max=%d\n", 
87                                                 tmp, sil, min, max);
88         LOCAL_USER_ADD(u);
89         if (chan->_state != AST_STATE_UP) {
90                 /* Otherwise answer unless we're supposed to send this while on-hook */
91                 res = ast_answer(chan);
92         }
93         if (!res) {
94                 origrformat = chan->readformat;
95                 if ((res = ast_set_read_format(chan, AST_FORMAT_SLINEAR))) 
96                         ast_log(LOG_WARNING, "Unable to set read format to linear!\n");
97         }
98         if (!(dsp = ast_dsp_new())) {
99                 ast_log(LOG_WARNING, "Unable to allocate DSP!\n");
100                 res = -1;
101         }
102         if (!res) {
103                 ast_stopstream(chan);
104                 res = ast_streamfile(chan, tmp, chan->language);
105                 if (!res) {
106                         while(chan->stream) {
107                                 res = ast_sched_wait(chan->sched);
108                                 if ((res < 0) && !chan->timingfunc) {
109                                         res = 0;
110                                         break;
111                                 }
112                                 if (res < 0)
113                                         res = 1000;
114                                 res = ast_waitfor(chan, res);
115                                 if (res < 0) {
116                                         ast_log(LOG_WARNING, "Waitfor failed on %s\n", chan->name);
117                                         break;
118                                 } else if (res > 0) {
119                                         fr = ast_read(chan);
120                                         if (!fr) {
121                                                 res = -1;
122                                                 break;
123                                         } else if (fr->frametype == AST_FRAME_DTMF) {
124                                                 char t[2];
125                                                 t[0] = fr->subclass;
126                                                 t[1] = '\0';
127                                                 if (ast_canmatch_extension(chan, chan->context, t, 1, chan->cid.cid_num)) {
128                                                         /* They entered a valid  extension, or might be anyhow */
129                                                         res = fr->subclass;
130                                                         ast_frfree(fr);
131                                                         break;
132                                                 }
133                                         } else if ((fr->frametype == AST_FRAME_VOICE) && (fr->subclass == AST_FORMAT_SLINEAR)) {
134                                                 int totalsilence;
135                                                 int ms;
136                                                 res = ast_dsp_silence(dsp, fr, &totalsilence);
137                                                 if (res && (totalsilence > sil)) {
138                                                         /* We've been quiet a little while */
139                                                         if (notsilent) {
140                                                                 /* We had heard some talking */
141                                                                 gettimeofday(&end, NULL);
142                                                                 ms = (end.tv_sec - start.tv_sec) * 1000;
143                                                                 ms += (end.tv_usec - start.tv_usec) / 1000;
144                                                                 ms -= sil;
145                                                                 if (ms < 0)
146                                                                         ms = 0;
147                                                                 if ((ms > min) && ((max < 0) || (ms < max))) {
148                                                                         char ms_str[10];
149                                                                         ast_log(LOG_DEBUG, "Found qualified token of %d ms\n", ms);
150
151                                                                         /* Save detected talk time (in milliseconds) */ 
152                                                                         sprintf(ms_str, "%d", ms );     
153                                                                         pbx_builtin_setvar_helper(chan, "TALK_DETECTED", ms_str);
154                                                                         
155                                                                         if (ast_exists_extension(chan, chan->context, "talk", 1, chan->cid.cid_num)) {
156                                                                                 strncpy(chan->exten, "talk", sizeof(chan->exten) -1 );
157                                                                                 chan->priority = 0;
158                                                                         }
159                                                                         res = 0;
160                                                                         ast_frfree(fr);
161                                                                         break;
162                                                                 } else
163                                                                         ast_log(LOG_DEBUG, "Found unqualified token of %d ms\n", ms);
164                                                                 notsilent = 0;
165                                                         }
166                                                 } else {
167                                                         if (!notsilent) {
168                                                                 /* Heard some audio, mark the begining of the token */
169                                                                 gettimeofday(&start, NULL);
170                                                                 ast_log(LOG_DEBUG, "Start of voice token!\n");
171                                                                 notsilent = 1;
172                                                         }
173                                                 }
174                                                 
175                                         }
176                                         ast_frfree(fr);
177                                 }
178                                 ast_sched_runq(chan->sched);
179                         }
180                         ast_stopstream(chan);
181                 } else {
182                         ast_log(LOG_WARNING, "ast_streamfile failed on %s for %s\n", chan->name, (char *)data);
183                         res = 0;
184                 }
185         }
186         if (res > -1) {
187                 if (origrformat && ast_set_read_format(chan, origrformat)) {
188                         ast_log(LOG_WARNING, "Failed to restore read format for %s to %s\n", 
189                                 chan->name, ast_getformatname(origrformat));
190                 }
191         }
192         if (dsp)
193                 ast_dsp_free(dsp);
194         LOCAL_USER_REMOVE(u);
195         return res;
196 }
197
198 int unload_module(void)
199 {
200         STANDARD_HANGUP_LOCALUSERS;
201         return ast_unregister_application(app);
202 }
203
204 int load_module(void)
205 {
206         return ast_register_application(app, background_detect_exec, synopsis, descrip);
207 }
208
209 char *description(void)
210 {
211         return tdesc;
212 }
213
214 int usecount(void)
215 {
216         int res;
217         STANDARD_USECOUNT(res);
218         return res;
219 }
220
221 char *key()
222 {
223         return ASTERISK_GPL_KEY;
224 }