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.
21 * \brief Playback a file with audio detect
23 * \author Mark Spencer <markster@digium.com>
25 * \ingroup applications
30 ASTERISK_FILE_VERSION(__FILE__
, "$Revision$")
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
)
65 struct ast_module_user
*u
;
71 struct timeval start
= { 0, 0};
79 if (ast_strlen_zero(data
)) {
80 ast_log(LOG_WARNING
, "BackgroundDetect requires an argument (filename)\n");
84 u
= ast_module_user_add(chan
);
86 tmp
= ast_strdupa(data
);
89 strsep(&stringp
, "|");
90 options
= strsep(&stringp
, "|");
92 if ((sscanf(options
, "%d", &x
) == 1) && (x
> 0))
94 options
= strsep(&stringp
, "|");
96 if ((sscanf(options
, "%d", &x
) == 1) && (x
> 0))
98 options
= strsep(&stringp
, "|");
100 if ((sscanf(options
, "%d", &x
) == 1) && (x
> 0))
105 ast_log(LOG_DEBUG
, "Preparing detect of '%s', sil=%d,min=%d,max=%d\n",
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
);
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");
121 ast_stopstream(chan
);
122 res
= ast_streamfile(chan
, tmp
, chan
->language
);
124 while(chan
->stream
) {
125 res
= ast_sched_wait(chan
->sched
);
126 if ((res
< 0) && !chan
->timingfunc
) {
132 res
= ast_waitfor(chan
, res
);
134 ast_log(LOG_WARNING
, "Waitfor failed on %s\n", chan
->name
);
136 } else if (res
> 0) {
141 } else if (fr
->frametype
== AST_FRAME_DTMF
) {
145 if (ast_canmatch_extension(chan
, chan
->context
, t
, 1, chan
->cid
.cid_num
)) {
146 /* They entered a valid extension, or might be anyhow */
151 } else if ((fr
->frametype
== AST_FRAME_VOICE
) && (fr
->subclass
== AST_FORMAT_SLINEAR
)) {
154 res
= ast_dsp_silence(dsp
, fr
, &totalsilence
);
155 if (res
&& (totalsilence
> sil
)) {
156 /* We've been quiet a little while */
158 /* We had heard some talking */
159 ms
= ast_tvdiff_ms(ast_tvnow(), start
);
163 if ((ms
> min
) && ((max
< 0) || (ms
< max
))) {
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);
176 ast_log(LOG_DEBUG
, "Found unqualified token of %d ms\n", ms
);
181 /* Heard some audio, mark the begining of the token */
183 ast_log(LOG_DEBUG
, "Start of voice token!\n");
191 ast_sched_runq(chan
->sched
);
193 ast_stopstream(chan
);
195 ast_log(LOG_WARNING
, "ast_streamfile failed on %s for %s\n", chan
->name
, (char *)data
);
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
));
207 ast_module_user_remove(u
);
211 static int unload_module(void)
215 res
= ast_unregister_application(app
);
217 ast_module_user_hangup_all();
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");