Accept %d/%d in m= line of SDP (bug #2625, but not their patch)
[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                                                                         ast_log(LOG_DEBUG, "Found qualified token of %d ms\n", ms);
149                                                                         if (ast_exists_extension(chan, chan->context, "talk", 1, chan->cid.cid_num)) {
150                                                                                 strncpy(chan->exten, "talk", sizeof(chan->exten) -1 );
151                                                                                 chan->priority = 0;
152                                                                         }
153                                                                         res = 0;
154                                                                         ast_frfree(fr);
155                                                                         break;
156                                                                 } else
157                                                                         ast_log(LOG_DEBUG, "Found unqualified token of %d ms\n", ms);
158                                                                 notsilent = 0;
159                                                         }
160                                                 } else {
161                                                         if (!notsilent) {
162                                                                 /* Heard some audio, mark the begining of the token */
163                                                                 gettimeofday(&start, NULL);
164                                                                 ast_log(LOG_DEBUG, "Start of voice token!\n");
165                                                                 notsilent = 1;
166                                                         }
167                                                 }
168                                                 
169                                         }
170                                         ast_frfree(fr);
171                                 }
172                                 ast_sched_runq(chan->sched);
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 }