Merge the dialplan_aesthetics branch. Most of this patch simply converts applications
[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 #include "asterisk/app.h"
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
64 static int background_detect_exec(struct ast_channel *chan, void *data)
65 {
66         int res = 0;
67         char *tmp;
68         struct ast_frame *fr;
69         int notsilent = 0;
70         struct timeval start = { 0, 0};
71         int sil = 1000;
72         int min = 100;
73         int max = -1;
74         int x;
75         int origrformat=0;
76         struct ast_dsp *dsp = NULL;
77         AST_DECLARE_APP_ARGS(args,
78                 AST_APP_ARG(filename);
79                 AST_APP_ARG(silence);
80                 AST_APP_ARG(min);
81                 AST_APP_ARG(max);
82         );
83         
84         if (ast_strlen_zero(data)) {
85                 ast_log(LOG_WARNING, "BackgroundDetect requires an argument (filename)\n");
86                 return -1;
87         }
88
89         tmp = ast_strdupa(data);
90         AST_STANDARD_APP_ARGS(args, tmp);
91
92         if ((sscanf(args.silence, "%d", &x) == 1) && (x > 0))
93                 sil = x;
94         if ((sscanf(args.min, "%d", &x) == 1) && (x > 0))
95                 min = x;
96         if ((sscanf(args.max, "%d", &x) == 1) && (x > 0))
97                 max = x;
98
99         ast_debug(1, "Preparing detect of '%s', sil=%d, min=%d, max=%d\n", args.filename, sil, min, max);
100         do {
101                 if (chan->_state != AST_STATE_UP) {
102                         if ((res = ast_answer(chan)))
103                                 break;
104                 }
105
106                 origrformat = chan->readformat;
107                 if ((ast_set_read_format(chan, AST_FORMAT_SLINEAR))) {
108                         ast_log(LOG_WARNING, "Unable to set read format to linear!\n");
109                         res = -1;
110                         break;
111                 }
112
113                 if (!(dsp = ast_dsp_new())) {
114                         ast_log(LOG_WARNING, "Unable to allocate DSP!\n");
115                         res = -1;
116                         break;
117                 }
118                 ast_stopstream(chan);
119                 if (ast_streamfile(chan, tmp, chan->language)) {
120                         ast_log(LOG_WARNING, "ast_streamfile failed on %s for %s\n", chan->name, (char *)data);
121                         break;
122                 }
123
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                                 ast_frfree(fr);
190                         }
191                         ast_sched_runq(chan->sched);
192                 }
193                 ast_stopstream(chan);
194         } while (0);
195
196         if (res > -1) {
197                 if (origrformat && ast_set_read_format(chan, origrformat)) {
198                         ast_log(LOG_WARNING, "Failed to restore read format for %s to %s\n", 
199                                 chan->name, ast_getformatname(origrformat));
200                 }
201         }
202         if (dsp)
203                 ast_dsp_free(dsp);
204         return res;
205 }
206
207 static int unload_module(void)
208 {
209         return ast_unregister_application(app);
210 }
211
212 static int load_module(void)
213 {
214         return ast_register_application(app, background_detect_exec, synopsis, descrip);
215 }
216
217 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Playback with Talk Detection");