Tue Aug 24 20:34:53 2010

Asterisk developer's documentation


app_playback.c File Reference

Trivial application to playback a sound file. More...

#include "asterisk.h"
#include "asterisk/file.h"
#include "asterisk/pbx.h"
#include "asterisk/module.h"
#include "asterisk/app.h"
#include "asterisk/say.h"
#include "asterisk/cli.h"
Include dependency graph for app_playback.c:

Go to the source code of this file.

Data Structures

struct  say_args_t

Functions

static char * __say_cli_init (struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
 AST_MODULE_INFO (ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT,"Sound File Playback Application",.load=load_module,.unload=unload_module,.reload=reload,)
static int do_say (say_args_t *a, const char *s, const char *options, int depth)
static int load_module (void)
static int playback_exec (struct ast_channel *chan, void *data)
static int reload (void)
static void restore_say_mode (void *arg)
static int s_streamwait3 (const say_args_t *a, const char *fn)
static void save_say_mode (const void *arg)
static int say_date (struct ast_channel *chan, time_t t, const char *ints, const char *lang)
static int say_date_generic (struct ast_channel *chan, time_t t, const char *ints, const char *lang, const char *format, const char *timezonename, const char *prefix)
static int say_date_with_format (struct ast_channel *chan, time_t t, const char *ints, const char *lang, const char *format, const char *timezonename)
static int say_datetime (struct ast_channel *chan, time_t t, const char *ints, const char *lang)
static int say_enumeration_full (struct ast_channel *chan, int num, const char *ints, const char *lang, const char *options, int audiofd, int ctrlfd)
static int say_full (struct ast_channel *chan, const char *string, const char *ints, const char *lang, const char *options, int audiofd, int ctrlfd)
static int say_init_mode (const char *mode)
static int say_number_full (struct ast_channel *chan, int num, const char *ints, const char *lang, const char *options, int audiofd, int ctrlfd)
static int say_time (struct ast_channel *chan, time_t t, const char *ints, const char *lang)
static int unload_module (void)

Variables

static char * app = "Playback"
static struct ast_cli_entry cli_playback []
static const void * say_api_buf [40]
static struct ast_config * say_cfg = NULL
static const char * say_new = "new"
static const char * say_old = "old"

Detailed Description

Trivial application to playback a sound file.

Author:
Mark Spencer <markster@digium.com>

Definition in file app_playback.c.


Function Documentation

static char* __say_cli_init ( struct ast_cli_entry *  e,
int  cmd,
struct ast_cli_args *  a 
) [static]

Definition at line 380 of file app_playback.c.

References ast_cli_args::argc, ast_cli_entry::args, ast_cli_args::argv, ast_cli(), CLI_GENERATE, CLI_INIT, CLI_SHOWUSAGE, CLI_SUCCESS, ast_cli_entry::command, ast_cli_args::fd, say_init_mode(), and ast_cli_entry::usage.

00381 {
00382    const char *old_mode = say_api_buf[0] ? say_new : say_old;
00383    char *mode;
00384    switch (cmd) {
00385    case CLI_INIT:
00386       e->command = "say load [new|old]";
00387       e->usage = 
00388          "Usage: say load [new|old]\n"
00389          "       say load\n"
00390          "           Report status of current say mode\n"
00391          "       say load new\n"
00392          "           Set say method, configured in say.conf\n"
00393          "       say load old\n"
00394          "           Set old say method, coded in asterisk core\n";
00395       return NULL;
00396    case CLI_GENERATE:
00397       return NULL;
00398    }
00399    if (a->argc == 2) {
00400       ast_cli(a->fd, "say mode is [%s]\n", old_mode);
00401       return CLI_SUCCESS;
00402    } else if (a->argc != e->args)
00403       return CLI_SHOWUSAGE;
00404    mode = a->argv[2];
00405    if (!strcmp(mode, old_mode))
00406       ast_cli(a->fd, "say mode is %s already\n", mode);
00407    else
00408       if (say_init_mode(mode) == 0)
00409          ast_cli(a->fd, "setting say mode from %s to %s\n", old_mode, mode);
00410 
00411    return CLI_SUCCESS;
00412 }

AST_MODULE_INFO ( ASTERISK_GPL_KEY  ,
AST_MODFLAG_DEFAULT  ,
"Sound File Playback Application"  ,
.  load = load_module,
.  unload = unload_module,
.  reload = reload 
)
static int do_say ( say_args_t *  a,
const char *  s,
const char *  options,
int  depth 
) [static]

Definition at line 162 of file app_playback.c.

References ast_debug, ast_extension_match(), AST_LIST_INSERT_HEAD, ast_log(), ast_skip_blanks(), ast_strdupa, ast_trim_blanks(), ast_var_assign(), ast_var_delete(), ast_variable_browse(), say_args_t::language, LOG_WARNING, ast_variable::name, ast_variable::next, pbx_substitute_variables_varshead(), s_streamwait3(), strsep(), and ast_variable::value.

Referenced by say_date_generic(), say_enumeration_full(), say_full(), and say_number_full().

00163 {
00164    struct ast_variable *v;
00165    char *lang, *x, *rule = NULL;
00166    int ret = 0;   
00167    struct varshead head = { .first = NULL, .last = NULL };
00168    struct ast_var_t *n;
00169 
00170    ast_debug(2, "string <%s> depth <%d>\n", s, depth);
00171    if (depth++ > 10) {
00172       ast_log(LOG_WARNING, "recursion too deep, exiting\n");
00173       return -1;
00174    } else if (!say_cfg) {
00175       ast_log(LOG_WARNING, "no say.conf, cannot spell '%s'\n", s);
00176       return -1;
00177    }
00178 
00179    /* scan languages same as in file.c */
00180    if (a->language == NULL)
00181       a->language = "en";     /* default */
00182    ast_debug(2, "try <%s> in <%s>\n", s, a->language);
00183    lang = ast_strdupa(a->language);
00184    for (;;) {
00185       for (v = ast_variable_browse(say_cfg, lang); v ; v = v->next) {
00186          if (ast_extension_match(v->name, s)) {
00187             rule = ast_strdupa(v->value);
00188             break;
00189          }
00190       }
00191       if (rule)
00192          break;
00193       if ( (x = strchr(lang, '_')) )
00194          *x = '\0';      /* try without suffix */
00195       else if (strcmp(lang, "en"))
00196          lang = "en";    /* last resort, try 'en' if not done yet */
00197       else
00198          break;
00199    }
00200    if (!rule)
00201       return 0;
00202 
00203    /* skip up to two prefixes to get the value */
00204    if ( (x = strchr(s, ':')) )
00205       s = x + 1;
00206    if ( (x = strchr(s, ':')) )
00207       s = x + 1;
00208    ast_debug(2, "value is <%s>\n", s);
00209    n = ast_var_assign("SAY", s);
00210    AST_LIST_INSERT_HEAD(&head, n, entries);
00211 
00212    /* scan the body, one piece at a time */
00213    while ( !ret && (x = strsep(&rule, ",")) ) { /* exit on key */
00214       char fn[128];
00215       const char *p, *fmt, *data; /* format and data pointers */
00216 
00217       /* prepare a decent file name */
00218       x = ast_skip_blanks(x);
00219       ast_trim_blanks(x);
00220 
00221       /* replace variables */
00222       pbx_substitute_variables_varshead(&head, x, fn, sizeof(fn));
00223       ast_debug(2, "doing [%s]\n", fn);
00224 
00225       /* locate prefix and data, if any */
00226       fmt = strchr(fn, ':');
00227       if (!fmt || fmt == fn)  {  /* regular filename */
00228          ret = s_streamwait3(a, fn);
00229          continue;
00230       }
00231       fmt++;
00232       data = strchr(fmt, ':');   /* colon before data */
00233       if (!data || data == fmt) {   /* simple prefix-fmt */
00234          ret = do_say(a, fn, options, depth);
00235          continue;
00236       }
00237       /* prefix:fmt:data */
00238       for (p = fmt; p < data && ret <= 0; p++) {
00239          char fn2[sizeof(fn)];
00240          if (*p == ' ' || *p == '\t')  /* skip blanks */
00241             continue;
00242          if (*p == '\'') {/* file name - we trim them */
00243             char *y;
00244             strcpy(fn2, ast_skip_blanks(p+1));  /* make a full copy */
00245             y = strchr(fn2, '\'');
00246             if (!y) {
00247                p = data;   /* invalid. prepare to end */
00248                break;
00249             }
00250             *y = '\0';
00251             ast_trim_blanks(fn2);
00252             p = strchr(p+1, '\'');
00253             ret = s_streamwait3(a, fn2);
00254          } else {
00255             int l = fmt-fn;
00256             strcpy(fn2, fn); /* copy everything */
00257             /* after prefix, append the format */
00258             fn2[l++] = *p;
00259             strcpy(fn2 + l, data);
00260             ret = do_say(a, fn2, options, depth);
00261          }
00262          
00263          if (ret) {
00264             break;
00265          }
00266       }
00267    }
00268    ast_var_delete(n);
00269    return ret;
00270 }

static int load_module ( void   )  [static]

Definition at line 531 of file app_playback.c.

References ARRAY_LEN, ast_cli_register_multiple(), ast_config_load, ast_extension_match(), ast_register_application_xml, ast_variable_browse(), CONFIG_STATUS_FILEINVALID, ast_variable::name, ast_variable::next, playback_exec(), say_init_mode(), and ast_variable::value.

00532 {
00533    struct ast_variable *v;
00534    struct ast_flags config_flags = { 0 };
00535 
00536    say_cfg = ast_config_load("say.conf", config_flags);
00537    if (say_cfg && say_cfg != CONFIG_STATUS_FILEINVALID) {
00538       for (v = ast_variable_browse(say_cfg, "general"); v ; v = v->next) {
00539             if (ast_extension_match(v->name, "mode")) {
00540             say_init_mode(v->value);
00541             break;
00542          }
00543       }
00544    }
00545 
00546    ast_cli_register_multiple(cli_playback, ARRAY_LEN(cli_playback));
00547    return ast_register_application_xml(app, playback_exec);
00548 }

static int playback_exec ( struct ast_channel *  chan,
void *  data 
) [static]

Definition at line 418 of file app_playback.c.

References ast_channel::_state, ast_cli_entry::args, ast_answer(), AST_APP_ARG, AST_DECLARE_APP_ARGS, ast_log(), AST_STANDARD_APP_ARGS, AST_STATE_UP, ast_stopstream(), ast_strdupa, ast_streamfile(), ast_strlen_zero(), ast_waitstream(), LOG_WARNING, pbx_builtin_setvar_helper(), say_full(), strcasestr(), and strsep().

Referenced by load_module().

00419 {
00420    int res = 0;
00421    int mres = 0;
00422    char *tmp;
00423    int option_skip=0;
00424    int option_say=0;
00425    int option_noanswer = 0;
00426 
00427    AST_DECLARE_APP_ARGS(args,
00428       AST_APP_ARG(filenames);
00429       AST_APP_ARG(options);
00430    );
00431    
00432    if (ast_strlen_zero(data)) {
00433       ast_log(LOG_WARNING, "Playback requires an argument (filename)\n");
00434       return -1;
00435    }
00436 
00437    tmp = ast_strdupa(data);
00438    AST_STANDARD_APP_ARGS(args, tmp);
00439 
00440    if (args.options) {
00441       if (strcasestr(args.options, "skip"))
00442          option_skip = 1;
00443       if (strcasestr(args.options, "say"))
00444          option_say = 1;
00445       if (strcasestr(args.options, "noanswer"))
00446          option_noanswer = 1;
00447    } 
00448    if (chan->_state != AST_STATE_UP) {
00449       if (option_skip) {
00450          /* At the user's option, skip if the line is not up */
00451          goto done;
00452       } else if (!option_noanswer) {
00453          /* Otherwise answer unless we're supposed to send this while on-hook */
00454          res = ast_answer(chan);
00455       }
00456    }
00457    if (!res) {
00458       char *back = args.filenames;
00459       char *front;
00460 
00461       ast_stopstream(chan);
00462       while (!res && (front = strsep(&back, "&"))) {
00463          if (option_say)
00464             res = say_full(chan, front, "", chan->language, NULL, -1, -1);
00465          else
00466             res = ast_streamfile(chan, front, chan->language);
00467          if (!res) { 
00468             res = ast_waitstream(chan, "");  
00469             ast_stopstream(chan);
00470          } else {
00471             ast_log(LOG_WARNING, "ast_streamfile failed on %s for %s\n", chan->name, (char *)data);
00472             res = 0;
00473             mres = 1;
00474          }
00475       }
00476    }
00477 done:
00478    pbx_builtin_setvar_helper(chan, "PLAYBACKSTATUS", mres ? "FAILED" : "SUCCESS");
00479    return res;
00480 }

static int reload ( void   )  [static]

Definition at line 482 of file app_playback.c.

References ast_config_destroy(), ast_config_load, ast_extension_match(), ast_log(), ast_variable_browse(), CONFIG_FLAG_FILEUNCHANGED, CONFIG_STATUS_FILEINVALID, CONFIG_STATUS_FILEUNCHANGED, LOG_ERROR, LOG_NOTICE, ast_variable::name, ast_variable::next, say_init_mode(), and ast_variable::value.

00483 {
00484    struct ast_variable *v;
00485    struct ast_flags config_flags = { CONFIG_FLAG_FILEUNCHANGED };
00486    struct ast_config *newcfg;
00487 
00488    if ((newcfg = ast_config_load("say.conf", config_flags)) == CONFIG_STATUS_FILEUNCHANGED) {
00489       return 0;
00490    } else if (newcfg == CONFIG_STATUS_FILEINVALID) {
00491       ast_log(LOG_ERROR, "Config file say.conf is in an invalid format.  Aborting.\n");
00492       return 0;
00493    }
00494 
00495    if (say_cfg) {
00496       ast_config_destroy(say_cfg);
00497       ast_log(LOG_NOTICE, "Reloading say.conf\n");
00498       say_cfg = newcfg;
00499    }
00500 
00501    if (say_cfg) {
00502       for (v = ast_variable_browse(say_cfg, "general"); v ; v = v->next) {
00503             if (ast_extension_match(v->name, "mode")) {
00504             say_init_mode(v->value);
00505             break;
00506          }
00507       }
00508    }
00509    
00510    /*
00511     * XXX here we should sort rules according to the same order
00512     * we have in pbx.c so we have the same matching behaviour.
00513     */
00514    return 0;
00515 }

static void restore_say_mode ( void *  arg  )  [static]
static int s_streamwait3 ( const say_args_t *  a,
const char *  fn 
) [static]

Definition at line 144 of file app_playback.c.

References ast_log(), ast_stopstream(), ast_streamfile(), ast_waitstream(), ast_waitstream_full(), say_args_t::audiofd, say_args_t::chan, say_args_t::ctrlfd, say_args_t::ints, say_args_t::language, and LOG_WARNING.

Referenced by do_say().

00145 {
00146    int res = ast_streamfile(a->chan, fn, a->language);
00147    if (res) {
00148       ast_log(LOG_WARNING, "Unable to play message %s\n", fn);
00149       return res;
00150    }
00151    res = (a->audiofd  > -1 && a->ctrlfd > -1) ?
00152    ast_waitstream_full(a->chan, a->ints, a->audiofd, a->ctrlfd) :
00153    ast_waitstream(a->chan, a->ints);
00154    ast_stopstream(a->chan);
00155    return res;  
00156 }

static void save_say_mode ( const void *  arg  )  [static]
static int say_date ( struct ast_channel *  chan,
time_t  t,
const char *  ints,
const char *  lang 
) [static]

Definition at line 331 of file app_playback.c.

References say_date_generic().

Referenced by say_init_mode().

00332 {
00333    return say_date_generic(chan, t, ints, lang, "", NULL, "date");
00334 }

static int say_date_generic ( struct ast_channel *  chan,
time_t  t,
const char *  ints,
const char *  lang,
const char *  format,
const char *  timezonename,
const char *  prefix 
) [static]

Definition at line 300 of file app_playback.c.

References ast_localtime(), buf, do_say(), ast_tm::tm_hour, ast_tm::tm_mday, ast_tm::tm_min, ast_tm::tm_mon, ast_tm::tm_sec, ast_tm::tm_wday, ast_tm::tm_yday, and ast_tm::tm_year.

Referenced by say_date(), say_date_with_format(), say_datetime(), and say_time().

00302 {
00303    char buf[128];
00304    struct ast_tm tm;
00305    struct timeval when = { t, 0 };
00306    say_args_t a = { chan, ints, lang, -1, -1 };
00307    if (format == NULL)
00308       format = "";
00309 
00310    ast_localtime(&when, &tm, NULL);
00311    snprintf(buf, sizeof(buf), "%s:%s:%04d%02d%02d%02d%02d.%02d-%d-%3d",
00312       prefix,
00313       format,
00314       tm.tm_year+1900,
00315       tm.tm_mon+1,
00316       tm.tm_mday,
00317       tm.tm_hour,
00318       tm.tm_min,
00319       tm.tm_sec,
00320       tm.tm_wday,
00321       tm.tm_yday);
00322    return do_say(&a, buf, NULL, 0);
00323 }

static int say_date_with_format ( struct ast_channel *  chan,
time_t  t,
const char *  ints,
const char *  lang,
const char *  format,
const char *  timezonename 
) [static]

Definition at line 325 of file app_playback.c.

References say_date_generic().

Referenced by say_init_mode().

00327 {
00328    return say_date_generic(chan, t, ints, lang, format, timezonename, "datetime");
00329 }

static int say_datetime ( struct ast_channel *  chan,
time_t  t,
const char *  ints,
const char *  lang 
) [static]

Definition at line 341 of file app_playback.c.

References say_date_generic().

Referenced by say_init_mode().

00342 {
00343    return say_date_generic(chan, t, ints, lang, "", NULL, "datetime");
00344 }

static int say_enumeration_full ( struct ast_channel *  chan,
int  num,
const char *  ints,
const char *  lang,
const char *  options,
int  audiofd,
int  ctrlfd 
) [static]

Definition at line 290 of file app_playback.c.

References buf, and do_say().

Referenced by say_init_mode().

00293 {
00294    char buf[64];
00295    say_args_t a = { chan, ints, lang, audiofd, ctrlfd };
00296    snprintf(buf, sizeof(buf), "enum:%d", num);
00297    return do_say(&a, buf, options, 0);
00298 }

static int say_full ( struct ast_channel *  chan,
const char *  string,
const char *  ints,
const char *  lang,
const char *  options,
int  audiofd,
int  ctrlfd 
) [static]

Definition at line 272 of file app_playback.c.

References do_say().

Referenced by playback_exec().

00275 {
00276    say_args_t a = { chan, ints, lang, audiofd, ctrlfd };
00277    return do_say(&a, string, options, 0);
00278 }

static int say_init_mode ( const char *  mode  )  [static]

Definition at line 349 of file app_playback.c.

References ast_log(), ast_say_character_str_full, ast_say_date, ast_say_date_with_format, ast_say_datetime, ast_say_datetime_from_now, ast_say_digit_str_full, ast_say_digits_full(), ast_say_enumeration_full, ast_say_number_full, ast_say_phonetic_str_full, ast_say_time, LOG_ERROR, LOG_WARNING, restore_say_mode(), save_say_mode(), say_character_str_full(), say_date(), say_date_with_format(), say_datetime(), say_datetime_from_now(), say_digit_str_full(), say_enumeration_full(), say_number_full(), say_phonetic_str_full(), and say_time().

Referenced by __say_cli_init(), load_module(), and reload().

00349                                            {
00350    if (!strcmp(mode, say_new)) {
00351       if (say_cfg == NULL) {
00352          ast_log(LOG_ERROR, "There is no say.conf file to use new mode\n");
00353          return -1;
00354       }
00355       save_say_mode(say_new);
00356       ast_say_number_full = say_number_full;
00357 
00358       ast_say_enumeration_full = say_enumeration_full;
00359 #if 0
00360       ast_say_digits_full = say_digits_full;
00361       ast_say_digit_str_full = say_digit_str_full;
00362       ast_say_character_str_full = say_character_str_full;
00363       ast_say_phonetic_str_full = say_phonetic_str_full;
00364       ast_say_datetime_from_now = say_datetime_from_now;
00365 #endif
00366       ast_say_datetime = say_datetime;
00367       ast_say_time = say_time;
00368       ast_say_date = say_date;
00369       ast_say_date_with_format = say_date_with_format;
00370    } else if (!strcmp(mode, say_old) && say_api_buf[0] == say_new) {
00371       restore_say_mode(NULL);
00372    } else if (strcmp(mode, say_old)) {
00373       ast_log(LOG_WARNING, "unrecognized mode %s\n", mode);
00374       return -1;
00375    }
00376    
00377    return 0;
00378 }

static int say_number_full ( struct ast_channel *  chan,
int  num,
const char *  ints,
const char *  lang,
const char *  options,
int  audiofd,
int  ctrlfd 
) [static]

Definition at line 280 of file app_playback.c.

References buf, and do_say().

Referenced by say_init_mode().

00283 {
00284    char buf[64];
00285    say_args_t a = { chan, ints, lang, audiofd, ctrlfd };
00286    snprintf(buf, sizeof(buf), "num:%d", num);
00287    return do_say(&a, buf, options, 0);
00288 }

static int say_time ( struct ast_channel *  chan,
time_t  t,
const char *  ints,
const char *  lang 
) [static]

Definition at line 336 of file app_playback.c.

References say_date_generic().

Referenced by say_init_mode().

00337 {
00338    return say_date_generic(chan, t, ints, lang, "", NULL, "time");
00339 }

static int unload_module ( void   )  [static]

Definition at line 517 of file app_playback.c.

References ARRAY_LEN, ast_cli_unregister_multiple(), ast_config_destroy(), and ast_unregister_application().

00518 {
00519    int res;
00520 
00521    res = ast_unregister_application(app);
00522 
00523    ast_cli_unregister_multiple(cli_playback, ARRAY_LEN(cli_playback));
00524 
00525    if (say_cfg)
00526       ast_config_destroy(say_cfg);
00527 
00528    return res; 
00529 }


Variable Documentation

char* app = "Playback" [static]

Definition at line 84 of file app_playback.c.

struct ast_cli_entry cli_playback[] [static]
Initial value:
 {
   AST_CLI_DEFINE(__say_cli_init, "Set or show the say mode"),
}

Definition at line 414 of file app_playback.c.

const void* say_api_buf[40] [static]

Definition at line 92 of file app_playback.c.

struct ast_config* say_cfg = NULL [static]

Definition at line 86 of file app_playback.c.

const char* say_new = "new" [static]

Definition at line 94 of file app_playback.c.

const char* say_old = "old" [static]

Definition at line 93 of file app_playback.c.


Generated on 24 Aug 2010 for Asterisk - the Open Source PBX by  doxygen 1.6.1