6cd8572bb36446bf351c2705c59e08ea1e68afd3
[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         ast_debug(1, "Preparing detect of '%s', sil=%d,min=%d,max=%d\n", tmp, sil, min, max);
107         if (chan->_state != AST_STATE_UP) {
108                 /* Otherwise answer unless we're supposed to send this while on-hook */
109                 res = ast_answer(chan);
110         }
111         if (!res) {
112                 origrformat = chan->readformat;
113                 if ((res = ast_set_read_format(chan, AST_FORMAT_SLINEAR))) 
114                         ast_log(LOG_WARNING, "Unable to set read format to linear!\n");
115         }
116         if (!(dsp = ast_dsp_new())) {
117                 ast_log(LOG_WARNING, "Unable to allocate DSP!\n");
118                 res = -1;
119         }
120         if (!res) {
121                 ast_stopstream(chan);
122                 res = ast_streamfile(chan, tmp, chan->language);
123                 if (!res) {
124                         while(chan->stream) {
125                                 res = ast_sched_wait(chan->sched);
126                                 if ((res < 0) && !chan->timingfunc) {
127                                         res = 0;
128                                         break;
129                                 }
130                                 if (res < 0)
131                                         res = 1000;
132                                 res = ast_waitfor(chan, res);
133                                 if (res < 0) {
134                                         ast_log(LOG_WARNING, "Waitfor failed on %s\n", chan->name);
135                                         break;
136                                 } else if (res > 0) {
137                                         fr = ast_read(chan);
138                                         if (!fr) {
139                                                 res = -1;
140                                                 break;
141                                         } else if (fr->frametype == AST_FRAME_DTMF) {
142                                                 char t[2];
143                                                 t[0] = fr->subclass;
144                                                 t[1] = '\0';
145                                                 if (ast_canmatch_extension(chan, chan->context, t, 1, chan->cid.cid_num)) {
146                                                         /* They entered a valid  extension, or might be anyhow */
147                                                         res = fr->subclass;
148                                                         ast_frfree(fr);
149                                                         break;
150                                                 }
151                                         } else if ((fr->frametype == AST_FRAME_VOICE) && (fr->subclass == AST_FORMAT_SLINEAR)) {
152                                                 int totalsilence;
153                                                 int ms;
154                                                 res = ast_dsp_silence(dsp, fr, &totalsilence);
155                                                 if (res && (totalsilence > sil)) {
156                                                         /* We've been quiet a little while */
157                                                         if (notsilent) {
158                                                                 /* We had heard some talking */
159                                                                 ms = ast_tvdiff_ms(ast_tvnow(), start);
160                                                                 ms -= sil;
161                                                                 if (ms < 0)
162                                                                         ms = 0;
163                                                                 if ((ms > min) && ((max < 0) || (ms < max))) {
164                                                                         char ms_str[10];
165                                                                         ast_debug(1, "Found qualified token of %d ms\n", ms);
166
167                                                                         /* Save detected talk time (in milliseconds) */ 
168                                                                         sprintf(ms_str, "%d", ms );     
169                                                                         pbx_builtin_setvar_helper(chan, "TALK_DETECTED", ms_str);
170                                                                         
171                                                                         ast_goto_if_exists(chan, chan->context, "talk", 1);
172                                                                         res = 0;
173                                                                         ast_frfree(fr);
174                                                                         break;
175                                                                 } else {
176                                                                         ast_debug(1, "Found unqualified token of %d ms\n", ms);
177                                                                 }
178                                                                 notsilent = 0;
179                                                         }
180                                                 } else {
181                                                         if (!notsilent) {
182                                                                 /* Heard some audio, mark the begining of the token */
183                                                                 start = ast_tvnow();
184                                                                 ast_debug(1, "Start of voice token!\n");
185                                                                 notsilent = 1;
186                                                         }
187                                                 }
188                                                 
189                                         }
190                                         ast_frfree(fr);
191                                 }
192                                 ast_sched_runq(chan->sched);
193                         }
194                         ast_stopstream(chan);
195                 } else {
196                         ast_log(LOG_WARNING, "ast_streamfile failed on %s for %s\n", chan->name, (char *)data);
197                         res = 0;
198                 }
199         }
200         if (res > -1) {
201                 if (origrformat && ast_set_read_format(chan, origrformat)) {
202                         ast_log(LOG_WARNING, "Failed to restore read format for %s to %s\n", 
203                                 chan->name, ast_getformatname(origrformat));
204                 }
205         }
206         if (dsp)
207                 ast_dsp_free(dsp);
208         ast_module_user_remove(u);
209         return res;
210 }
211
212 static int unload_module(void)
213 {
214         return ast_unregister_application(app);
215 }
216
217 static int load_module(void)
218 {
219         return ast_register_application(app, background_detect_exec, synopsis, descrip);
220 }
221
222 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Playback with Talk Detection");