app_talkdetect.c

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 1999 - 2005, Digium, Inc.
00005  *
00006  * Mark Spencer <markster@digium.com>
00007  *
00008  * See http://www.asterisk.org for more information about
00009  * the Asterisk project. Please do not directly contact
00010  * any of the maintainers of this project for assistance;
00011  * the project provides a web site, mailing lists and IRC
00012  * channels for your use.
00013  *
00014  * This program is free software, distributed under the terms of
00015  * the GNU General Public License Version 2. See the LICENSE file
00016  * at the top of the source tree.
00017  */
00018 
00019 /*! \file
00020  *
00021  * \brief Playback a file with audio detect
00022  *
00023  * \author Mark Spencer <markster@digium.com>
00024  * 
00025  * \ingroup applications
00026  */
00027 
00028 /*** MODULEINFO
00029    <support_level>extended</support_level>
00030  ***/
00031 
00032 #include "asterisk.h"
00033 
00034 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 419592 $")
00035 
00036 #include "asterisk/lock.h"
00037 #include "asterisk/file.h"
00038 #include "asterisk/channel.h"
00039 #include "asterisk/pbx.h"
00040 #include "asterisk/module.h"
00041 #include "asterisk/translate.h"
00042 #include "asterisk/utils.h"
00043 #include "asterisk/dsp.h"
00044 #include "asterisk/app.h"
00045 #include "asterisk/format.h"
00046 #include "asterisk/format_cache.h"
00047 
00048 /*** DOCUMENTATION
00049    <application name="BackgroundDetect" language="en_US">
00050       <synopsis>
00051          Background a file with talk detect.
00052       </synopsis>
00053       <syntax>
00054          <parameter name="filename" required="true" />
00055          <parameter name="sil">
00056             <para>If not specified, defaults to <literal>1000</literal>.</para>
00057          </parameter>
00058          <parameter name="min">
00059             <para>If not specified, defaults to <literal>100</literal>.</para>
00060          </parameter>
00061          <parameter name="max">
00062             <para>If not specified, defaults to <literal>infinity</literal>.</para>
00063          </parameter>
00064          <parameter name="analysistime">
00065             <para>If not specified, defaults to <literal>infinity</literal>.</para>
00066          </parameter>
00067       </syntax>
00068       <description>
00069          <para>Plays back <replaceable>filename</replaceable>, waiting for interruption from a given digit (the digit
00070          must start the beginning of a valid extension, or it will be ignored). During
00071          the playback of the file, audio is monitored in the receive direction, and if
00072          a period of non-silence which is greater than <replaceable>min</replaceable> ms yet less than
00073          <replaceable>max</replaceable> ms is followed by silence for at least <replaceable>sil</replaceable> ms,
00074          which occurs during the first <replaceable>analysistime</replaceable> ms, then the audio playback is
00075          aborted and processing jumps to the <replaceable>talk</replaceable> extension, if available.</para>
00076       </description>
00077    </application>
00078  ***/
00079 
00080 static char *app = "BackgroundDetect";
00081 
00082 static int background_detect_exec(struct ast_channel *chan, const char *data)
00083 {
00084    int res = 0;
00085    char *tmp;
00086    struct ast_frame *fr;
00087    int notsilent = 0;
00088    struct timeval start = { 0, 0 };
00089    struct timeval detection_start = { 0, 0 };
00090    int sil = 1000;
00091    int min = 100;
00092    int max = -1;
00093    int analysistime = -1;
00094    int continue_analysis = 1;
00095    int x;
00096    RAII_VAR(struct ast_format *, origrformat, NULL, ao2_cleanup);
00097    struct ast_dsp *dsp = NULL;
00098    AST_DECLARE_APP_ARGS(args,
00099       AST_APP_ARG(filename);
00100       AST_APP_ARG(silence);
00101       AST_APP_ARG(min);
00102       AST_APP_ARG(max);
00103       AST_APP_ARG(analysistime);
00104    );
00105 
00106    if (ast_strlen_zero(data)) {
00107       ast_log(LOG_WARNING, "BackgroundDetect requires an argument (filename)\n");
00108       return -1;
00109    }
00110 
00111    tmp = ast_strdupa(data);
00112    AST_STANDARD_APP_ARGS(args, tmp);
00113 
00114    if (!ast_strlen_zero(args.silence) && (sscanf(args.silence, "%30d", &x) == 1) && (x > 0)) {
00115       sil = x;
00116    }
00117    if (!ast_strlen_zero(args.min) && (sscanf(args.min, "%30d", &x) == 1) && (x > 0)) {
00118       min = x;
00119    }
00120    if (!ast_strlen_zero(args.max) && (sscanf(args.max, "%30d", &x) == 1) && (x > 0)) {
00121       max = x;
00122    }
00123    if (!ast_strlen_zero(args.analysistime) && (sscanf(args.analysistime, "%30d", &x) == 1) && (x > 0)) {
00124       analysistime = x;
00125    }
00126 
00127    ast_debug(1, "Preparing detect of '%s', sil=%d, min=%d, max=%d, analysistime=%d\n", args.filename, sil, min, max, analysistime);
00128    do {
00129       if (ast_channel_state(chan) != AST_STATE_UP) {
00130          if ((res = ast_answer(chan))) {
00131             break;
00132          }
00133       }
00134 
00135       origrformat = ao2_bump(ast_channel_readformat(chan));
00136       if ((ast_set_read_format(chan, ast_format_slin))) {
00137          ast_log(LOG_WARNING, "Unable to set read format to linear!\n");
00138          res = -1;
00139          break;
00140       }
00141 
00142       if (!(dsp = ast_dsp_new())) {
00143          ast_log(LOG_WARNING, "Unable to allocate DSP!\n");
00144          res = -1;
00145          break;
00146       }
00147       ast_stopstream(chan);
00148       if (ast_streamfile(chan, tmp, ast_channel_language(chan))) {
00149          ast_log(LOG_WARNING, "ast_streamfile failed on %s for %s\n", ast_channel_name(chan), (char *)data);
00150          break;
00151       }
00152       detection_start = ast_tvnow();
00153       while (ast_channel_stream(chan)) {
00154          res = ast_sched_wait(ast_channel_sched(chan));
00155          if ((res < 0) && !ast_channel_timingfunc(chan)) {
00156             res = 0;
00157             break;
00158          }
00159          if (res < 0) {
00160             res = 1000;
00161          }
00162          res = ast_waitfor(chan, res);
00163          if (res < 0) {
00164             ast_log(LOG_WARNING, "Waitfor failed on %s\n", ast_channel_name(chan));
00165             break;
00166          } else if (res > 0) {
00167             fr = ast_read(chan);
00168             if (continue_analysis && analysistime >= 0) {
00169                /* If we have a limit for the time to analyze voice
00170                 * frames and the time has not expired */
00171                if (ast_tvdiff_ms(ast_tvnow(), detection_start) >= analysistime) {
00172                   continue_analysis = 0;
00173                   ast_verb(3, "BackgroundDetect: Talk analysis time complete on %s.\n", ast_channel_name(chan));
00174                }
00175             }
00176             
00177             if (!fr) {
00178                res = -1;
00179                break;
00180             } else if (fr->frametype == AST_FRAME_DTMF) {
00181                char t[2];
00182                t[0] = fr->subclass.integer;
00183                t[1] = '\0';
00184                if (ast_canmatch_extension(chan, ast_channel_context(chan), t, 1,
00185                   S_COR(ast_channel_caller(chan)->id.number.valid, ast_channel_caller(chan)->id.number.str, NULL))) {
00186                   /* They entered a valid  extension, or might be anyhow */
00187                   res = fr->subclass.integer;
00188                   ast_frfree(fr);
00189                   break;
00190                }
00191             } else if ((fr->frametype == AST_FRAME_VOICE) &&
00192             (ast_format_cmp(fr->subclass.format, ast_format_slin) == AST_FORMAT_CMP_EQUAL) && continue_analysis) {
00193                int totalsilence;
00194                int ms;
00195                res = ast_dsp_silence(dsp, fr, &totalsilence);
00196                if (res && (totalsilence > sil)) {
00197                   /* We've been quiet a little while */
00198                   if (notsilent) {
00199                      /* We had heard some talking */
00200                      ms = ast_tvdiff_ms(ast_tvnow(), start);
00201                      ms -= sil;
00202                      if (ms < 0)
00203                         ms = 0;
00204                      if ((ms > min) && ((max < 0) || (ms < max))) {
00205                         char ms_str[12];
00206                         ast_debug(1, "Found qualified token of %d ms\n", ms);
00207 
00208                         /* Save detected talk time (in milliseconds) */ 
00209                         snprintf(ms_str, sizeof(ms_str), "%d", ms);  
00210                         pbx_builtin_setvar_helper(chan, "TALK_DETECTED", ms_str);
00211 
00212                         ast_goto_if_exists(chan, ast_channel_context(chan), "talk", 1);
00213                         res = 0;
00214                         ast_frfree(fr);
00215                         break;
00216                      } else {
00217                         ast_debug(1, "Found unqualified token of %d ms\n", ms);
00218                      }
00219                      notsilent = 0;
00220                   }
00221                } else {
00222                   if (!notsilent) {
00223                      /* Heard some audio, mark the begining of the token */
00224                      start = ast_tvnow();
00225                      ast_debug(1, "Start of voice token!\n");
00226                      notsilent = 1;
00227                   }
00228                }
00229             }
00230             ast_frfree(fr);
00231          }
00232          ast_sched_runq(ast_channel_sched(chan));
00233       }
00234       ast_stopstream(chan);
00235    } while (0);
00236 
00237    if (res > -1) {
00238       if (origrformat && ast_set_read_format(chan, origrformat)) {
00239          ast_log(LOG_WARNING, "Failed to restore read format for %s to %s\n", 
00240             ast_channel_name(chan), ast_format_get_name(origrformat));
00241       }
00242    }
00243    if (dsp) {
00244       ast_dsp_free(dsp);
00245    }
00246    return res;
00247 }
00248 
00249 static int unload_module(void)
00250 {
00251    return ast_unregister_application(app);
00252 }
00253 
00254 static int load_module(void)
00255 {
00256    return ast_register_application_xml(app, background_detect_exec);
00257 }
00258 
00259 AST_MODULE_INFO_STANDARD_EXTENDED(ASTERISK_GPL_KEY, "Playback with Talk Detection");
00260 

Generated on Thu Apr 16 06:27:12 2015 for Asterisk - The Open Source Telephony Project by  doxygen 1.5.6