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