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