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