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