Fix a few things I missed to ensure zt_chan_conf structure is not modified in mkintf
[asterisk-bristuff.git] / apps / app_talkdetect.c
blob79cbbd5d0e742e65e824c2400eeea6f85890236f
1 /*
2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2005, Digium, Inc.
6 * Mark Spencer <markster@digium.com>
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.
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.
19 /*! \file
21 * \brief Playback a file with audio detect
23 * \author Mark Spencer <markster@digium.com>
25 * \ingroup applications
28 #include "asterisk.h"
30 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
32 #include <stdlib.h>
33 #include <stdio.h>
34 #include <string.h>
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"
46 static char *app = "BackgroundDetect";
48 static char *synopsis = "Background a file with talk detect";
50 static char *descrip =
51 " BackgroundDetect(filename[|sil[|min|[max]]]): Plays back a given\n"
52 "filename, waiting for interruption from a given digit (the digit must\n"
53 "start the beginning of a valid extension, or it will be ignored).\n"
54 "During the playback of the file, audio is monitored in the receive\n"
55 "direction, and if a period of non-silence which is greater than 'min' ms\n"
56 "yet less than 'max' ms is followed by silence for at least 'sil' ms then\n"
57 "the audio playback is aborted and processing jumps to the 'talk' extension\n"
58 "if available. If unspecified, sil, min, and max default to 1000, 100, and\n"
59 "infinity respectively.\n";
62 static int background_detect_exec(struct ast_channel *chan, void *data)
64 int res = 0;
65 struct ast_module_user *u;
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;
79 if (ast_strlen_zero(data)) {
80 ast_log(LOG_WARNING, "BackgroundDetect requires an argument (filename)\n");
81 return -1;
84 u = ast_module_user_add(chan);
86 tmp = ast_strdupa(data);
88 stringp=tmp;
89 strsep(&stringp, "|");
90 options = strsep(&stringp, "|");
91 if (options) {
92 if ((sscanf(options, "%d", &x) == 1) && (x > 0))
93 sil = x;
94 options = strsep(&stringp, "|");
95 if (options) {
96 if ((sscanf(options, "%d", &x) == 1) && (x > 0))
97 min = x;
98 options = strsep(&stringp, "|");
99 if (options) {
100 if ((sscanf(options, "%d", &x) == 1) && (x > 0))
101 max = x;
105 ast_log(LOG_DEBUG, "Preparing detect of '%s', sil=%d,min=%d,max=%d\n",
106 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);
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");
116 if (!(dsp = ast_dsp_new())) {
117 ast_log(LOG_WARNING, "Unable to allocate DSP!\n");
118 res = -1;
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;
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;
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_log(LOG_DEBUG, "Found qualified token of %d ms\n", ms);
167 /* Save detected talk time (in milliseconds) */
168 sprintf(ms_str, "%d", ms );
169 pbx_builtin_setvar_helper(chan, "TALK_DETECTED", ms_str);
171 ast_goto_if_exists(chan, chan->context, "talk", 1);
172 res = 0;
173 ast_frfree(fr);
174 break;
175 } else
176 ast_log(LOG_DEBUG, "Found unqualified token of %d ms\n", ms);
177 notsilent = 0;
179 } else {
180 if (!notsilent) {
181 /* Heard some audio, mark the begining of the token */
182 start = ast_tvnow();
183 ast_log(LOG_DEBUG, "Start of voice token!\n");
184 notsilent = 1;
189 ast_frfree(fr);
191 ast_sched_runq(chan->sched);
193 ast_stopstream(chan);
194 } else {
195 ast_log(LOG_WARNING, "ast_streamfile failed on %s for %s\n", chan->name, (char *)data);
196 res = 0;
199 if (res > -1) {
200 if (origrformat && ast_set_read_format(chan, origrformat)) {
201 ast_log(LOG_WARNING, "Failed to restore read format for %s to %s\n",
202 chan->name, ast_getformatname(origrformat));
205 if (dsp)
206 ast_dsp_free(dsp);
207 ast_module_user_remove(u);
208 return res;
211 static int unload_module(void)
213 int res;
215 res = ast_unregister_application(app);
217 ast_module_user_hangup_all();
219 return res;
222 static int load_module(void)
224 return ast_register_application(app, background_detect_exec, synopsis, descrip);
227 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Playback with Talk Detection");