2004-06-11 14:06:30 +00:00
|
|
|
/*
|
2005-09-14 20:46:50 +00:00
|
|
|
* Asterisk -- An open source telephony toolkit.
|
2004-06-11 14:06:30 +00:00
|
|
|
*
|
2005-09-14 20:46:50 +00:00
|
|
|
* Copyright (C) 1999 - 2005, Digium, Inc.
|
2004-06-11 14:06:30 +00:00
|
|
|
*
|
|
|
|
* Mark Spencer <markster@digium.com>
|
|
|
|
*
|
2005-09-14 20:46:50 +00:00
|
|
|
* See http://www.asterisk.org for more information about
|
|
|
|
* the Asterisk project. Please do not directly contact
|
|
|
|
* any of the maintainers of this project for assistance;
|
|
|
|
* the project provides a web site, mailing lists and IRC
|
|
|
|
* channels for your use.
|
|
|
|
*
|
2004-06-11 14:06:30 +00:00
|
|
|
* This program is free software, distributed under the terms of
|
2005-09-14 20:46:50 +00:00
|
|
|
* the GNU General Public License Version 2. See the LICENSE file
|
|
|
|
* at the top of the source tree.
|
|
|
|
*/
|
|
|
|
|
2005-10-24 20:12:06 +00:00
|
|
|
/*! \file
|
2005-09-14 20:46:50 +00:00
|
|
|
*
|
2005-10-24 20:12:06 +00:00
|
|
|
* \brief Playback a file with audio detect
|
2005-12-30 21:18:06 +00:00
|
|
|
*
|
|
|
|
* \author Mark Spencer <markster@digium.com>
|
2005-09-14 20:46:50 +00:00
|
|
|
*
|
2005-11-06 15:09:47 +00:00
|
|
|
* \ingroup applications
|
2004-06-11 14:06:30 +00:00
|
|
|
*/
|
|
|
|
|
2005-06-06 22:39:32 +00:00
|
|
|
#include "asterisk.h"
|
|
|
|
|
|
|
|
ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
|
|
|
|
|
2005-04-21 06:02:45 +00:00
|
|
|
#include "asterisk/lock.h"
|
|
|
|
#include "asterisk/file.h"
|
|
|
|
#include "asterisk/channel.h"
|
|
|
|
#include "asterisk/pbx.h"
|
|
|
|
#include "asterisk/module.h"
|
|
|
|
#include "asterisk/translate.h"
|
|
|
|
#include "asterisk/utils.h"
|
|
|
|
#include "asterisk/dsp.h"
|
2007-07-23 19:51:41 +00:00
|
|
|
#include "asterisk/app.h"
|
2004-06-11 14:06:30 +00:00
|
|
|
|
2008-11-01 21:10:07 +00:00
|
|
|
/*** DOCUMENTATION
|
2008-11-02 04:57:02 +00:00
|
|
|
<application name="BackgroundDetect" language="en_US">
|
2008-11-01 21:10:07 +00:00
|
|
|
<synopsis>
|
|
|
|
Background a file with talk detect.
|
|
|
|
</synopsis>
|
|
|
|
<syntax>
|
|
|
|
<parameter name="filename" required="true" />
|
|
|
|
<parameter name="sil">
|
|
|
|
<para>If not specified, defaults to <literal>1000</literal>.</para>
|
|
|
|
</parameter>
|
|
|
|
<parameter name="min">
|
|
|
|
<para>If not specified, defaults to <literal>100</literal>.</para>
|
|
|
|
</parameter>
|
|
|
|
<parameter name="max">
|
|
|
|
<para>If not specified, defaults to <literal>infinity</literal>.</para>
|
|
|
|
</parameter>
|
|
|
|
<parameter name="analysistime">
|
|
|
|
<para>If not specified, defaults to <literal>infinity</literal>.</para>
|
|
|
|
</parameter>
|
|
|
|
</syntax>
|
|
|
|
<description>
|
|
|
|
<para>Plays back <replaceable>filename</replaceable>, waiting for interruption from a given digit (the digit
|
|
|
|
must start the beginning of a valid extension, or it will be ignored). During
|
|
|
|
the playback of the file, audio is monitored in the receive direction, and if
|
|
|
|
a period of non-silence which is greater than <replaceable>min</replaceable> ms yet less than
|
|
|
|
<replaceable>max</replaceable> ms is followed by silence for at least <replaceable>sil</replaceable> ms,
|
|
|
|
which occurs during the first <replaceable>analysistime</replaceable> ms, then the audio playback is
|
|
|
|
aborted and processing jumps to the <replaceable>talk</replaceable> extension, if available.</para>
|
|
|
|
</description>
|
|
|
|
</application>
|
|
|
|
***/
|
2004-06-11 14:06:30 +00:00
|
|
|
|
2008-11-01 21:10:07 +00:00
|
|
|
static char *app = "BackgroundDetect";
|
2004-06-11 14:06:30 +00:00
|
|
|
|
2009-05-21 21:13:09 +00:00
|
|
|
static int background_detect_exec(struct ast_channel *chan, const char *data)
|
2004-06-11 14:06:30 +00:00
|
|
|
{
|
|
|
|
int res = 0;
|
2005-10-19 18:19:02 +00:00
|
|
|
char *tmp;
|
2004-06-11 14:06:30 +00:00
|
|
|
struct ast_frame *fr;
|
2007-07-23 19:51:41 +00:00
|
|
|
int notsilent = 0;
|
2008-06-17 21:42:42 +00:00
|
|
|
struct timeval start = { 0, 0 };
|
|
|
|
struct timeval detection_start = { 0, 0 };
|
2004-06-11 14:06:30 +00:00
|
|
|
int sil = 1000;
|
|
|
|
int min = 100;
|
|
|
|
int max = -1;
|
2008-06-17 21:42:42 +00:00
|
|
|
int analysistime = -1;
|
|
|
|
int continue_analysis = 1;
|
2004-06-11 14:06:30 +00:00
|
|
|
int x;
|
2008-06-17 21:42:42 +00:00
|
|
|
int origrformat = 0;
|
2007-07-23 19:51:41 +00:00
|
|
|
struct ast_dsp *dsp = NULL;
|
|
|
|
AST_DECLARE_APP_ARGS(args,
|
|
|
|
AST_APP_ARG(filename);
|
|
|
|
AST_APP_ARG(silence);
|
|
|
|
AST_APP_ARG(min);
|
|
|
|
AST_APP_ARG(max);
|
2008-06-17 21:42:42 +00:00
|
|
|
AST_APP_ARG(analysistime);
|
2007-07-23 19:51:41 +00:00
|
|
|
);
|
2005-10-19 18:19:02 +00:00
|
|
|
|
2005-10-26 19:48:14 +00:00
|
|
|
if (ast_strlen_zero(data)) {
|
2004-06-11 14:06:30 +00:00
|
|
|
ast_log(LOG_WARNING, "BackgroundDetect requires an argument (filename)\n");
|
|
|
|
return -1;
|
|
|
|
}
|
2005-10-19 18:19:02 +00:00
|
|
|
|
2006-05-10 13:22:15 +00:00
|
|
|
tmp = ast_strdupa(data);
|
2007-07-23 19:51:41 +00:00
|
|
|
AST_STANDARD_APP_ARGS(args, tmp);
|
2005-10-19 18:19:02 +00:00
|
|
|
|
2009-08-10 19:20:57 +00:00
|
|
|
if (!ast_strlen_zero(args.silence) && (sscanf(args.silence, "%30d", &x) == 1) && (x > 0)) {
|
2007-07-23 19:51:41 +00:00
|
|
|
sil = x;
|
2008-06-17 21:42:42 +00:00
|
|
|
}
|
2009-08-10 19:20:57 +00:00
|
|
|
if (!ast_strlen_zero(args.min) && (sscanf(args.min, "%30d", &x) == 1) && (x > 0)) {
|
2007-07-23 19:51:41 +00:00
|
|
|
min = x;
|
2008-06-17 21:42:42 +00:00
|
|
|
}
|
2009-08-10 19:20:57 +00:00
|
|
|
if (!ast_strlen_zero(args.max) && (sscanf(args.max, "%30d", &x) == 1) && (x > 0)) {
|
2007-07-23 19:51:41 +00:00
|
|
|
max = x;
|
2008-06-17 21:42:42 +00:00
|
|
|
}
|
2009-08-10 19:20:57 +00:00
|
|
|
if (!ast_strlen_zero(args.analysistime) && (sscanf(args.analysistime, "%30d", &x) == 1) && (x > 0)) {
|
2008-06-17 21:42:42 +00:00
|
|
|
analysistime = x;
|
|
|
|
}
|
2007-07-23 19:51:41 +00:00
|
|
|
|
2008-06-17 21:42:42 +00:00
|
|
|
ast_debug(1, "Preparing detect of '%s', sil=%d, min=%d, max=%d, analysistime=%d\n", args.filename, sil, min, max, analysistime);
|
2007-07-23 19:51:41 +00:00
|
|
|
do {
|
|
|
|
if (chan->_state != AST_STATE_UP) {
|
2008-06-17 21:42:42 +00:00
|
|
|
if ((res = ast_answer(chan))) {
|
2007-07-23 19:51:41 +00:00
|
|
|
break;
|
2008-06-17 21:42:42 +00:00
|
|
|
}
|
2004-06-11 14:06:30 +00:00
|
|
|
}
|
2007-07-23 19:51:41 +00:00
|
|
|
|
2004-06-11 14:06:30 +00:00
|
|
|
origrformat = chan->readformat;
|
2007-07-23 19:51:41 +00:00
|
|
|
if ((ast_set_read_format(chan, AST_FORMAT_SLINEAR))) {
|
2004-06-11 14:06:30 +00:00
|
|
|
ast_log(LOG_WARNING, "Unable to set read format to linear!\n");
|
2007-07-23 19:51:41 +00:00
|
|
|
res = -1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(dsp = ast_dsp_new())) {
|
|
|
|
ast_log(LOG_WARNING, "Unable to allocate DSP!\n");
|
|
|
|
res = -1;
|
|
|
|
break;
|
|
|
|
}
|
2004-06-11 14:06:30 +00:00
|
|
|
ast_stopstream(chan);
|
2007-07-23 19:51:41 +00:00
|
|
|
if (ast_streamfile(chan, tmp, chan->language)) {
|
|
|
|
ast_log(LOG_WARNING, "ast_streamfile failed on %s for %s\n", chan->name, (char *)data);
|
|
|
|
break;
|
|
|
|
}
|
2008-06-17 21:42:42 +00:00
|
|
|
detection_start = ast_tvnow();
|
2007-07-23 19:51:41 +00:00
|
|
|
while (chan->stream) {
|
|
|
|
res = ast_sched_wait(chan->sched);
|
|
|
|
if ((res < 0) && !chan->timingfunc) {
|
|
|
|
res = 0;
|
|
|
|
break;
|
|
|
|
}
|
2008-06-17 21:42:42 +00:00
|
|
|
if (res < 0) {
|
2007-07-23 19:51:41 +00:00
|
|
|
res = 1000;
|
2008-06-17 21:42:42 +00:00
|
|
|
}
|
2007-07-23 19:51:41 +00:00
|
|
|
res = ast_waitfor(chan, res);
|
|
|
|
if (res < 0) {
|
|
|
|
ast_log(LOG_WARNING, "Waitfor failed on %s\n", chan->name);
|
|
|
|
break;
|
|
|
|
} else if (res > 0) {
|
|
|
|
fr = ast_read(chan);
|
2008-06-17 21:42:42 +00:00
|
|
|
if (continue_analysis && analysistime >= 0) {
|
|
|
|
/* If we have a limit for the time to analyze voice
|
|
|
|
* frames and the time has not expired */
|
|
|
|
if (ast_tvdiff_ms(ast_tvnow(), detection_start) >= analysistime) {
|
|
|
|
continue_analysis = 0;
|
|
|
|
ast_verb(3, "BackgroundDetect: Talk analysis time complete on %s.\n", chan->name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-07-23 19:51:41 +00:00
|
|
|
if (!fr) {
|
|
|
|
res = -1;
|
2004-06-11 14:06:30 +00:00
|
|
|
break;
|
2007-07-23 19:51:41 +00:00
|
|
|
} else if (fr->frametype == AST_FRAME_DTMF) {
|
|
|
|
char t[2];
|
2009-11-04 14:05:12 +00:00
|
|
|
t[0] = fr->subclass.integer;
|
2007-07-23 19:51:41 +00:00
|
|
|
t[1] = '\0';
|
2010-07-14 15:48:36 +00:00
|
|
|
if (ast_canmatch_extension(chan, chan->context, t, 1,
|
|
|
|
S_COR(chan->caller.id.number.valid, chan->caller.id.number.str, NULL))) {
|
2007-07-23 19:51:41 +00:00
|
|
|
/* They entered a valid extension, or might be anyhow */
|
2009-11-04 14:05:12 +00:00
|
|
|
res = fr->subclass.integer;
|
2007-07-23 19:51:41 +00:00
|
|
|
ast_frfree(fr);
|
2004-06-11 14:06:30 +00:00
|
|
|
break;
|
2007-07-23 19:51:41 +00:00
|
|
|
}
|
2009-11-04 14:05:12 +00:00
|
|
|
} else if ((fr->frametype == AST_FRAME_VOICE) && (fr->subclass.codec == AST_FORMAT_SLINEAR) && continue_analysis) {
|
2007-07-23 19:51:41 +00:00
|
|
|
int totalsilence;
|
|
|
|
int ms;
|
|
|
|
res = ast_dsp_silence(dsp, fr, &totalsilence);
|
|
|
|
if (res && (totalsilence > sil)) {
|
|
|
|
/* We've been quiet a little while */
|
|
|
|
if (notsilent) {
|
|
|
|
/* We had heard some talking */
|
|
|
|
ms = ast_tvdiff_ms(ast_tvnow(), start);
|
|
|
|
ms -= sil;
|
|
|
|
if (ms < 0)
|
|
|
|
ms = 0;
|
|
|
|
if ((ms > min) && ((max < 0) || (ms < max))) {
|
2008-06-17 21:42:42 +00:00
|
|
|
char ms_str[12];
|
2007-07-23 19:51:41 +00:00
|
|
|
ast_debug(1, "Found qualified token of %d ms\n", ms);
|
|
|
|
|
|
|
|
/* Save detected talk time (in milliseconds) */
|
2008-06-17 21:42:42 +00:00
|
|
|
snprintf(ms_str, sizeof(ms_str), "%d", ms);
|
2007-07-23 19:51:41 +00:00
|
|
|
pbx_builtin_setvar_helper(chan, "TALK_DETECTED", ms_str);
|
|
|
|
|
|
|
|
ast_goto_if_exists(chan, chan->context, "talk", 1);
|
|
|
|
res = 0;
|
|
|
|
ast_frfree(fr);
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
ast_debug(1, "Found unqualified token of %d ms\n", ms);
|
2004-06-11 14:06:30 +00:00
|
|
|
}
|
2007-07-23 19:51:41 +00:00
|
|
|
notsilent = 0;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (!notsilent) {
|
|
|
|
/* Heard some audio, mark the begining of the token */
|
|
|
|
start = ast_tvnow();
|
|
|
|
ast_debug(1, "Start of voice token!\n");
|
|
|
|
notsilent = 1;
|
2004-06-11 14:06:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-07-23 19:51:41 +00:00
|
|
|
ast_frfree(fr);
|
2004-06-11 14:06:30 +00:00
|
|
|
}
|
2007-07-23 19:51:41 +00:00
|
|
|
ast_sched_runq(chan->sched);
|
2004-06-11 14:06:30 +00:00
|
|
|
}
|
2007-07-23 19:51:41 +00:00
|
|
|
ast_stopstream(chan);
|
|
|
|
} while (0);
|
|
|
|
|
2004-06-11 14:06:30 +00:00
|
|
|
if (res > -1) {
|
|
|
|
if (origrformat && ast_set_read_format(chan, origrformat)) {
|
|
|
|
ast_log(LOG_WARNING, "Failed to restore read format for %s to %s\n",
|
|
|
|
chan->name, ast_getformatname(origrformat));
|
|
|
|
}
|
|
|
|
}
|
2008-06-17 21:42:42 +00:00
|
|
|
if (dsp) {
|
2004-06-11 14:06:30 +00:00
|
|
|
ast_dsp_free(dsp);
|
2008-06-17 21:42:42 +00:00
|
|
|
}
|
2004-06-11 14:06:30 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2006-08-21 02:11:39 +00:00
|
|
|
static int unload_module(void)
|
2004-06-11 14:06:30 +00:00
|
|
|
{
|
2007-07-16 13:35:20 +00:00
|
|
|
return ast_unregister_application(app);
|
2004-06-11 14:06:30 +00:00
|
|
|
}
|
|
|
|
|
2006-08-21 02:11:39 +00:00
|
|
|
static int load_module(void)
|
2004-06-11 14:06:30 +00:00
|
|
|
{
|
2008-11-01 21:10:07 +00:00
|
|
|
return ast_register_application_xml(app, background_detect_exec);
|
2004-06-11 14:06:30 +00:00
|
|
|
}
|
|
|
|
|
2006-08-21 02:11:39 +00:00
|
|
|
AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Playback with Talk Detection");
|