Thu Apr 28 2011 16:56:41

Asterisk developer's documentation


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 #include "asterisk.h"
00029 
00030 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 211580 $")
00031 
00032 #include "asterisk/lock.h"
00033 #include "asterisk/file.h"
00034 #include "asterisk/channel.h"
00035 #include "asterisk/pbx.h"
00036 #include "asterisk/module.h"
00037 #include "asterisk/translate.h"
00038 #include "asterisk/utils.h"
00039 #include "asterisk/dsp.h"
00040 #include "asterisk/app.h"
00041 
00042 /*** DOCUMENTATION
00043    <application name="BackgroundDetect" language="en_US">
00044       <synopsis>
00045          Background a file with talk detect.
00046       </synopsis>
00047       <syntax>
00048          <parameter name="filename" required="true" />
00049          <parameter name="sil">
00050             <para>If not specified, defaults to <literal>1000</literal>.</para>
00051          </parameter>
00052          <parameter name="min">
00053             <para>If not specified, defaults to <literal>100</literal>.</para>
00054          </parameter>
00055          <parameter name="max">
00056             <para>If not specified, defaults to <literal>infinity</literal>.</para>
00057          </parameter>
00058          <parameter name="analysistime">
00059             <para>If not specified, defaults to <literal>infinity</literal>.</para>
00060          </parameter>
00061       </syntax>
00062       <description>
00063          <para>Plays back <replaceable>filename</replaceable>, waiting for interruption from a given digit (the digit
00064          must start the beginning of a valid extension, or it will be ignored). During
00065          the playback of the file, audio is monitored in the receive direction, and if
00066          a period of non-silence which is greater than <replaceable>min</replaceable> ms yet less than
00067          <replaceable>max</replaceable> ms is followed by silence for at least <replaceable>sil</replaceable> ms,
00068          which occurs during the first <replaceable>analysistime</replaceable> ms, then the audio playback is
00069          aborted and processing jumps to the <replaceable>talk</replaceable> extension, if available.</para>
00070       </description>
00071    </application>
00072  ***/
00073 
00074 static char *app = "BackgroundDetect";
00075 
00076 static int background_detect_exec(struct ast_channel *chan, void *data)
00077 {
00078    int res = 0;
00079    char *tmp;
00080    struct ast_frame *fr;
00081    int notsilent = 0;
00082    struct timeval start = { 0, 0 };
00083    struct timeval detection_start = { 0, 0 };
00084    int sil = 1000;
00085    int min = 100;
00086    int max = -1;
00087    int analysistime = -1;
00088    int continue_analysis = 1;
00089    int x;
00090    int origrformat = 0;
00091    struct ast_dsp *dsp = NULL;
00092    AST_DECLARE_APP_ARGS(args,
00093       AST_APP_ARG(filename);
00094       AST_APP_ARG(silence);
00095       AST_APP_ARG(min);
00096       AST_APP_ARG(max);
00097       AST_APP_ARG(analysistime);
00098    );
00099    
00100    if (ast_strlen_zero(data)) {
00101       ast_log(LOG_WARNING, "BackgroundDetect requires an argument (filename)\n");
00102       return -1;
00103    }
00104 
00105    tmp = ast_strdupa(data);
00106    AST_STANDARD_APP_ARGS(args, tmp);
00107 
00108    if (!ast_strlen_zero(args.silence) && (sscanf(args.silence, "%30d", &x) == 1) && (x > 0)) {
00109       sil = x;
00110    }
00111    if (!ast_strlen_zero(args.min) && (sscanf(args.min, "%30d", &x) == 1) && (x > 0)) {
00112       min = x;
00113    }
00114    if (!ast_strlen_zero(args.max) && (sscanf(args.max, "%30d", &x) == 1) && (x > 0)) {
00115       max = x;
00116    }
00117    if (!ast_strlen_zero(args.analysistime) && (sscanf(args.analysistime, "%30d", &x) == 1) && (x > 0)) {
00118       analysistime = x;
00119    }
00120 
00121    ast_debug(1, "Preparing detect of '%s', sil=%d, min=%d, max=%d, analysistime=%d\n", args.filename, sil, min, max, analysistime);
00122    do {
00123       if (chan->_state != AST_STATE_UP) {
00124          if ((res = ast_answer(chan))) {
00125             break;
00126          }
00127       }
00128 
00129       origrformat = chan->readformat;
00130       if ((ast_set_read_format(chan, AST_FORMAT_SLINEAR))) {
00131          ast_log(LOG_WARNING, "Unable to set read format to linear!\n");
00132          res = -1;
00133          break;
00134       }
00135 
00136       if (!(dsp = ast_dsp_new())) {
00137          ast_log(LOG_WARNING, "Unable to allocate DSP!\n");
00138          res = -1;
00139          break;
00140       }
00141       ast_stopstream(chan);
00142       if (ast_streamfile(chan, tmp, chan->language)) {
00143          ast_log(LOG_WARNING, "ast_streamfile failed on %s for %s\n", chan->name, (char *)data);
00144          break;
00145       }
00146       detection_start = ast_tvnow();
00147       while (chan->stream) {
00148          res = ast_sched_wait(chan->sched);
00149          if ((res < 0) && !chan->timingfunc) {
00150             res = 0;
00151             break;
00152          }
00153          if (res < 0) {
00154             res = 1000;
00155          }
00156          res = ast_waitfor(chan, res);
00157          if (res < 0) {
00158             ast_log(LOG_WARNING, "Waitfor failed on %s\n", chan->name);
00159             break;
00160          } else if (res > 0) {
00161             fr = ast_read(chan);
00162             if (continue_analysis && analysistime >= 0) {
00163                /* If we have a limit for the time to analyze voice
00164                 * frames and the time has not expired */
00165                if (ast_tvdiff_ms(ast_tvnow(), detection_start) >= analysistime) {
00166                   continue_analysis = 0;
00167                   ast_verb(3, "BackgroundDetect: Talk analysis time complete on %s.\n", chan->name);
00168                }
00169             }
00170             
00171             if (!fr) {
00172                res = -1;
00173                break;
00174             } else if (fr->frametype == AST_FRAME_DTMF) {
00175                char t[2];
00176                t[0] = fr->subclass;
00177                t[1] = '\0';
00178                if (ast_canmatch_extension(chan, chan->context, t, 1, chan->cid.cid_num)) {
00179                   /* They entered a valid  extension, or might be anyhow */
00180                   res = fr->subclass;
00181                   ast_frfree(fr);
00182                   break;
00183                }
00184             } else if ((fr->frametype == AST_FRAME_VOICE) && (fr->subclass == AST_FORMAT_SLINEAR) && continue_analysis) {
00185                int totalsilence;
00186                int ms;
00187                res = ast_dsp_silence(dsp, fr, &totalsilence);
00188                if (res && (totalsilence > sil)) {
00189                   /* We've been quiet a little while */
00190                   if (notsilent) {
00191                      /* We had heard some talking */
00192                      ms = ast_tvdiff_ms(ast_tvnow(), start);
00193                      ms -= sil;
00194                      if (ms < 0)
00195                         ms = 0;
00196                      if ((ms > min) && ((max < 0) || (ms < max))) {
00197                         char ms_str[12];
00198                         ast_debug(1, "Found qualified token of %d ms\n", ms);
00199 
00200                         /* Save detected talk time (in milliseconds) */ 
00201                         snprintf(ms_str, sizeof(ms_str), "%d", ms);  
00202                         pbx_builtin_setvar_helper(chan, "TALK_DETECTED", ms_str);
00203 
00204                         ast_goto_if_exists(chan, chan->context, "talk", 1);
00205                         res = 0;
00206                         ast_frfree(fr);
00207                         break;
00208                      } else {
00209                         ast_debug(1, "Found unqualified token of %d ms\n", ms);
00210                      }
00211                      notsilent = 0;
00212                   }
00213                } else {
00214                   if (!notsilent) {
00215                      /* Heard some audio, mark the begining of the token */
00216                      start = ast_tvnow();
00217                      ast_debug(1, "Start of voice token!\n");
00218                      notsilent = 1;
00219                   }
00220                }
00221             }
00222             ast_frfree(fr);
00223          }
00224          ast_sched_runq(chan->sched);
00225       }
00226       ast_stopstream(chan);
00227    } while (0);
00228 
00229    if (res > -1) {
00230       if (origrformat && ast_set_read_format(chan, origrformat)) {
00231          ast_log(LOG_WARNING, "Failed to restore read format for %s to %s\n", 
00232             chan->name, ast_getformatname(origrformat));
00233       }
00234    }
00235    if (dsp) {
00236       ast_dsp_free(dsp);
00237    }
00238    return res;
00239 }
00240 
00241 static int unload_module(void)
00242 {
00243    return ast_unregister_application(app);
00244 }
00245 
00246 static int load_module(void)
00247 {
00248    return ast_register_application_xml(app, background_detect_exec);
00249 }
00250 
00251 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Playback with Talk Detection");