Asterisk.org: Developer Documentation (28 Oct 2009)

Wed Oct 28 15:48:19 2009

## Asterisk developer's documentation

# app_waitforsilence.c File Reference

Wait for Silence

- Waits for up to 'x' milliseconds of silence, 'y' times
- WaitForSilence(500,2) will wait for 1/2 second of silence, twice
- WaitForSilence(1000,1) will wait for 1 second of silence, once
  .

More...

`#include <stdlib.h>`
`#include <stdio.h>`
`#include <string.h>`
`#include <unistd.h>`
`#include "asterisk.h"`
`#include "asterisk/file.h"`
`#include "asterisk/logger.h"`
`#include "asterisk/channel.h"`
`#include "asterisk/pbx.h"`
`#include "asterisk/dsp.h"`
`#include "asterisk/module.h"`
`#include "asterisk/options.h"`

Include dependency graph for app_waitforsilence.c:

Go to the source code of this file.

## Functions

char * | description (void)
| Provides a description of the module.
static int | do_waiting (struct ast_channel *chan, int maxsilence)
char * | key ()
| Returns the ASTERISK_GPL_KEY.
int | load_module (void)
| Initialize the module.
int | unload_module (void)
| Cleanup all module structures, sockets, etc.
int | usecount (void)
| Provides a usecount.
static int | waitforsilence_exec (struct ast_channel *chan, void *data)

## Variables

static char * | app = "WaitForSilence"
static char * | descrip
| LOCAL_USER_DECL
| STANDARD_LOCAL_USER
static char * | synopsis = "Waits for a specified amount of silence"
static char * | tdesc = "Wait For Silence"

## Detailed Description

Wait for Silence

- Waits for up to 'x' milliseconds of silence, 'y' times
- WaitForSilence(500,2) will wait for 1/2 second of silence, twice
- WaitForSilence(1000,1) will wait for 1 second of silence, once
  .

Definition in file app_waitforsilence.c.

## Function Documentation

char* description | ( | void |  | ) |

Provides a description of the module.

Returns:
  a short description of your module

Definition at line 194 of file app_waitforsilence.c.

    00195 {
    00196    return tdesc;
    00197 }

static int do_waiting | ( | struct ast_channel * | chan,
|  | int | maxsilence |
| ) |  |  | `[static]`

Definition at line 66 of file app_waitforsilence.c.

References ast_dsp_free(), ast_dsp_new(), ast_dsp_set_threshold(), ast_dsp_silence(), AST_FORMAT_SLINEAR, AST_FRAME_VOICE, ast_frfree(), ast_getformatname(), ast_log(), ast_read(), ast_set_read_format(), ast_verbose(), ast_waitfor(), ast_frame::frametype, LOG_DEBUG, LOG_WARNING, ast_channel::name, option_verbose, pbx_builtin_setvar_helper(), ast_channel::readformat, silencethreshold, and VERBOSE_PREFIX_3.

Referenced by waitforsilence_exec().

    00066                                                                 {
    00067
    00068    struct ast_frame *f;
    00069    int totalsilence = 0;
    00070    int dspsilence = 0;
    00071    int gotsilence = 0;
    00072    static int silencethreshold = 128;
    00073    int rfmt = 0;
    00074    int res = 0;
    00075    struct ast_dsp *sildet;  /* silence detector dsp */
    00076    time_t start, now;
    00077    time(&start);
    00078
    00079    rfmt = chan->readformat; /* Set to linear mode */
    00080    res = ast_set_read_format(chan, AST_FORMAT_SLINEAR);
    00081    if (res < 0) {
    00082       ast_log(LOG_WARNING, "Unable to set to linear mode, giving up\n");
    00083       return -1;
    00084    }
    00085
    00086    sildet = ast_dsp_new(); /* Create the silence detector */
    00087    if (!sildet) {
    00088       ast_log(LOG_WARNING, "Unable to create silence detector :(\n");
    00089       return -1;
    00090    }
    00091    ast_dsp_set_threshold(sildet, silencethreshold);
    00092
    00093    /* Await silence... */
    00094    f = NULL;
    00095    for(;;) {
    00096       res = ast_waitfor(chan, 2000);
    00097       if (!res) {
    00098          ast_log(LOG_WARNING, "One waitfor failed, trying another\n");
    00099          /* Try one more time in case of masq */
    00100          res = ast_waitfor(chan, 2000);
    00101          if (!res) {
    00102             ast_log(LOG_WARNING, "No audio available on %s??\n", chan->name);
    00103             res = -1;
    00104          }
    00105       }
    00106
    00107       if (res < 0) {
    00108          f = NULL;
    00109          break;
    00110       }
    00111       f = ast_read(chan);
    00112       if (!f)
    00113          break;
    00114       if (f->frametype == AST_FRAME_VOICE) {
    00115          dspsilence = 0;
    00116          ast_dsp_silence(sildet, f, &dspsilence);
    00117          if (dspsilence) {
    00118             totalsilence = dspsilence;
    00119             time(&start);
    00120          } else {
    00121             totalsilence = 0;
    00122          }
    00123
    00124          if (totalsilence >= maxsilence) {
    00125             if (option_verbose > 2)
    00126                ast_verbose(VERBOSE_PREFIX_3 "Exiting with %dms silence > %dms required\n", totalsilence, maxsilence);
    00127             /* Ended happily with silence */
    00128             gotsilence = 1;
    00129             pbx_builtin_setvar_helper(chan, "WAITSTATUS", "SILENCE");
    00130             ast_log(LOG_DEBUG, "WAITSTATUS was set to SILENCE\n");
    00131             ast_frfree(f);
    00132             break;
    00133          } else if ( difftime(time(&now),start) >= maxsilence/1000 ) {
    00134             pbx_builtin_setvar_helper(chan, "WAITSTATUS", "TIMEOUT");
    00135             ast_log(LOG_DEBUG, "WAITSTATUS was set to TIMEOUT\n");
    00136             ast_frfree(f);
    00137             break;
    00138          }
    00139       }
    00140       ast_frfree(f);
    00141    }
    00142    if (rfmt && ast_set_read_format(chan, rfmt)) {
    00143       ast_log(LOG_WARNING, "Unable to restore format %s to channel '%s'\n", ast_getformatname(rfmt), chan->name);
    00144    }
    00145    ast_dsp_free(sildet);
    00146    return gotsilence;
    00147 }

char* key | ( | void |  | ) |

Returns the ASTERISK_GPL_KEY.

This returns the ASTERISK_GPL_KEY, signifiying that you agree to the terms of the GPL stated in the ASTERISK_GPL_KEY. Your module will not load if it does not return the EXACT message:

     char *key(void) {
             return ASTERISK_GPL_KEY;
     }

Returns:
  ASTERISK_GPL_KEY

Definition at line 206 of file app_waitforsilence.c.

References ASTERISK_GPL_KEY.

    00207 {
    00208    return ASTERISK_GPL_KEY;
    00209 }

int load_module | ( | void |  | ) |

Initialize the module.

This function is called at module load time. Put all code in here that needs to set up your module's hardware, software, registrations, etc.

Returns:
  This function should return 0 on success and non-zero on failure. If the module is not loaded successfully, Asterisk will call its unload_module() function.

Initialize the Agents module. This function is being called by Asterisk when loading the module. Among other thing it registers applications, cli commands and reads the cofiguration file.

Returns:
  int Always 0.

TE STUFF END

Definition at line 189 of file app_waitforsilence.c.

References ast_register_application(), and waitforsilence_exec().

    00190 {
    00191    return ast_register_application(app, waitforsilence_exec, synopsis, descrip);
    00192 }

int unload_module | ( | void |  | ) |

Cleanup all module structures, sockets, etc.

This is called at exit. Any registrations and memory allocations need to be unregistered and free'd here. Nothing else will do these for you (until exit).

Returns:
  Zero on success, or non-zero on error.

Definition at line 178 of file app_waitforsilence.c.

References ast_unregister_application(), and STANDARD_HANGUP_LOCALUSERS.

    00179 {
    00180    int res;
    00181
    00182    res = ast_unregister_application(app);
    00183
    00184    STANDARD_HANGUP_LOCALUSERS;
    00185
    00186    return res;
    00187 }

int usecount | ( | void |  | ) |

Provides a usecount.

This function will be called by various parts of asterisk. Basically, all it has to do is to return a usecount when called. You will need to maintain your usecount within the module somewhere. The usecount should be how many channels provided by this module are in use.

Returns:
  The module's usecount.

Definition at line 199 of file app_waitforsilence.c.

References STANDARD_USECOUNT.

    00200 {
    00201    int res;
    00202    STANDARD_USECOUNT(res);
    00203    return res;
    00204 }

static int waitforsilence_exec | ( | struct ast_channel * | chan,
|  | void * | data |
| ) |  |  | `[static]`

Definition at line 149 of file app_waitforsilence.c.

References ast_answer(), ast_log(), ast_verbose(), do_waiting(), LOCAL_USER_ADD, LOCAL_USER_REMOVE, LOG_WARNING, maxsilence, option_verbose, and VERBOSE_PREFIX_3.

Referenced by load_module().

    00150 {
    00151    int res = 1;
    00152    struct localuser *u;
    00153    int maxsilence = 1000;
    00154    int iterations = 1, i;
    00155
    00156    LOCAL_USER_ADD(u);
    00157
    00158    res = ast_answer(chan); /* Answer the channel */
    00159
    00160    if (!data || ((sscanf(data, "%30d|%30d", &maxsilence, &iterations) != 2) &&
    00161       (sscanf(data, "%30d", &maxsilence) != 1))) {
    00162       ast_log(LOG_WARNING, "Using default value of 1000ms, 1 iteration\n");
    00163    }
    00164
    00165    if (option_verbose > 2)
    00166       ast_verbose(VERBOSE_PREFIX_3 "Waiting %d time(s) for %d ms silence\n", iterations, maxsilence);
    00167
    00168    res = 1;
    00169    for (i=0; (i<iterations) && (res == 1); i++) {
    00170       res = do_waiting(chan, maxsilence);
    00171    }
    00172    LOCAL_USER_REMOVE(u);
    00173    if (res > 0)
    00174       res = 0;
    00175    return res;
    00176 }

## Variable Documentation

char* app = "WaitForSilence"`[static]`

Definition at line 50 of file app_waitforsilence.c.

char* descrip`[static]`

Definition at line 52 of file app_waitforsilence.c.

LOCAL_USER_DECL

Definition at line 64 of file app_waitforsilence.c.

STANDARD_LOCAL_USER

Definition at line 62 of file app_waitforsilence.c.

char* synopsis = "Waits for a specified amount of silence"`[static]`

Definition at line 51 of file app_waitforsilence.c.

char* tdesc = "Wait For Silence"`[static]`

Definition at line 49 of file app_waitforsilence.c.