app_talkdetect.c File Reference

Playback a file with audio detect. More...

#include "asterisk.h"
#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"
#include "asterisk/app.h"
#include "asterisk/format.h"
#include "asterisk/format_cache.h"

Include dependency graph for app_talkdetect.c:

Go to the source code of this file.

Functions

 AST_MODULE_INFO_STANDARD_EXTENDED (ASTERISK_GPL_KEY,"Playback with Talk Detection")
static int background_detect_exec (struct ast_channel *chan, const char *data)
static int load_module (void)
static int unload_module (void)

Variables

static char * app = "BackgroundDetect"


Detailed Description

Playback a file with audio detect.

Author:
Mark Spencer <markster@digium.com>

Definition in file app_talkdetect.c.


Function Documentation

AST_MODULE_INFO_STANDARD_EXTENDED ( ASTERISK_GPL_KEY  ,
"Playback with Talk Detection"   
)

static int background_detect_exec ( struct ast_channel chan,
const char *  data 
) [static]

Definition at line 82 of file app_talkdetect.c.

References ao2_bump, ao2_cleanup, args, ast_answer(), AST_APP_ARG, ast_canmatch_extension(), ast_channel_caller(), ast_channel_context(), ast_channel_language(), ast_channel_name(), ast_channel_readformat(), ast_channel_sched(), ast_channel_stream(), ast_channel_timingfunc(), ast_debug, AST_DECLARE_APP_ARGS, ast_dsp_free(), ast_dsp_new(), ast_dsp_silence(), ast_format_cmp(), AST_FORMAT_CMP_EQUAL, ast_format_get_name(), ast_format_slin, AST_FRAME_DTMF, AST_FRAME_VOICE, ast_frfree, ast_goto_if_exists(), ast_log, ast_read(), ast_sched_runq(), ast_sched_wait(), ast_set_read_format(), AST_STANDARD_APP_ARGS, AST_STATE_UP, ast_stopstream(), ast_strdupa, ast_streamfile(), ast_strlen_zero, ast_tvdiff_ms(), ast_tvnow(), ast_verb, ast_waitfor(), ast_frame_subclass::format, ast_frame::frametype, ast_frame_subclass::integer, LOG_WARNING, max, min, NULL, pbx_builtin_setvar_helper(), RAII_VAR, S_COR, ast_frame::subclass, tmp(), and ast_dsp::totalsilence.

Referenced by load_module().

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 }

static int load_module ( void   )  [static]

Definition at line 254 of file app_talkdetect.c.

References ast_register_application_xml, and background_detect_exec().

static int unload_module ( void   )  [static]

Definition at line 249 of file app_talkdetect.c.

References ast_unregister_application().

00250 {
00251    return ast_unregister_application(app);
00252 }


Variable Documentation

char* app = "BackgroundDetect" [static]

Definition at line 80 of file app_talkdetect.c.


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