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