Thu Dec 17 15:35:39 2009

Asterisk developer's documentation


file.c

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 1999 - 2006, 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 Generic File Format Support.
00022  *
00023  * \author Mark Spencer <markster@digium.com> 
00024  */
00025 
00026 #include "asterisk.h"
00027 
00028 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 222878 $")
00029 
00030 #include <sys/types.h>
00031 #include <errno.h>
00032 #include <unistd.h>
00033 #include <stdlib.h>
00034 #include <string.h>
00035 #include <stdio.h>
00036 #include <fcntl.h>
00037 #include <dirent.h>
00038 #include <sys/types.h>
00039 #include <sys/stat.h>
00040 
00041 #include "asterisk/frame.h"
00042 #include "asterisk/file.h"
00043 #include "asterisk/cli.h"
00044 #include "asterisk/logger.h"
00045 #include "asterisk/channel.h"
00046 #include "asterisk/sched.h"
00047 #include "asterisk/options.h"
00048 #include "asterisk/translate.h"
00049 #include "asterisk/utils.h"
00050 #include "asterisk/lock.h"
00051 #include "asterisk/app.h"
00052 #include "asterisk/pbx.h"
00053 #include "asterisk/linkedlists.h"
00054 #include "asterisk/module.h"
00055 #include "asterisk/astobj2.h"
00056 
00057 /*
00058  * The following variable controls the layout of localized sound files.
00059  * If 0, use the historical layout with prefix just before the filename
00060  * (i.e. digits/en/1.gsm , digits/it/1.gsm or default to digits/1.gsm),
00061  * if 1 put the prefix at the beginning of the filename
00062  * (i.e. en/digits/1.gsm, it/digits/1.gsm or default to digits/1.gsm).
00063  * The latter permits a language to be entirely in one directory.
00064  */
00065 int ast_language_is_prefix;
00066 
00067 static AST_LIST_HEAD_STATIC(formats, ast_format);
00068 
00069 int __ast_format_register(const struct ast_format *f, struct ast_module *mod)
00070 {
00071    struct ast_format *tmp;
00072 
00073    if (AST_LIST_LOCK(&formats)) {
00074       ast_log(LOG_WARNING, "Unable to lock format list\n");
00075       return -1;
00076    }
00077    AST_LIST_TRAVERSE(&formats, tmp, list) {
00078       if (!strcasecmp(f->name, tmp->name)) {
00079          AST_LIST_UNLOCK(&formats);
00080          ast_log(LOG_WARNING, "Tried to register '%s' format, already registered\n", f->name);
00081          return -1;
00082       }
00083    }
00084    if (!(tmp = ast_calloc(1, sizeof(*tmp)))) {
00085       AST_LIST_UNLOCK(&formats);
00086       return -1;
00087    }
00088    *tmp = *f;
00089    tmp->module = mod;
00090    if (tmp->buf_size) {
00091       /*
00092        * Align buf_size properly, rounding up to the machine-specific
00093        * alignment for pointers.
00094        */
00095       struct _test_align { void *a, *b; } p;
00096       int align = (char *)&p.b - (char *)&p.a;
00097       tmp->buf_size = ((f->buf_size + align - 1)/align)*align;
00098    }
00099    
00100    memset(&tmp->list, 0, sizeof(tmp->list));
00101 
00102    AST_LIST_INSERT_HEAD(&formats, tmp, list);
00103    AST_LIST_UNLOCK(&formats);
00104    if (option_verbose > 1)
00105       ast_verbose( VERBOSE_PREFIX_2 "Registered file format %s, extension(s) %s\n", f->name, f->exts);
00106 
00107    return 0;
00108 }
00109 
00110 int ast_format_unregister(const char *name)
00111 {
00112    struct ast_format *tmp;
00113    int res = -1;
00114 
00115    if (AST_LIST_LOCK(&formats)) {
00116       ast_log(LOG_WARNING, "Unable to lock format list\n");
00117       return -1;
00118    }
00119    AST_LIST_TRAVERSE_SAFE_BEGIN(&formats, tmp, list) {
00120       if (!strcasecmp(name, tmp->name)) {
00121          AST_LIST_REMOVE_CURRENT(&formats, list);
00122          free(tmp);
00123          res = 0;
00124       }
00125    }
00126    AST_LIST_TRAVERSE_SAFE_END
00127    AST_LIST_UNLOCK(&formats);
00128 
00129    if (!res) {
00130       if (option_verbose > 1)
00131          ast_verbose( VERBOSE_PREFIX_2 "Unregistered format %s\n", name);
00132    } else
00133       ast_log(LOG_WARNING, "Tried to unregister format %s, already unregistered\n", name);
00134 
00135    return res;
00136 }
00137 
00138 int ast_stopstream(struct ast_channel *tmp)
00139 {
00140    ast_channel_lock(tmp);
00141 
00142    /* Stop a running stream if there is one */
00143    if (tmp->stream) {
00144       ast_closestream(tmp->stream);
00145       tmp->stream = NULL;
00146       if (tmp->oldwriteformat && ast_set_write_format(tmp, tmp->oldwriteformat))
00147          ast_log(LOG_WARNING, "Unable to restore format back to %d\n", tmp->oldwriteformat);
00148    }
00149    /* Stop the video stream too */
00150    if (tmp->vstream != NULL) {
00151       ast_closestream(tmp->vstream);
00152       tmp->vstream = NULL;
00153    }
00154 
00155    ast_channel_unlock(tmp);
00156 
00157    return 0;
00158 }
00159 
00160 int ast_writestream(struct ast_filestream *fs, struct ast_frame *f)
00161 {
00162    int res = -1;
00163    int alt = 0;
00164    if (f->frametype == AST_FRAME_VIDEO) {
00165       if (fs->fmt->format < AST_FORMAT_MAX_AUDIO) {
00166          /* This is the audio portion.  Call the video one... */
00167          if (!fs->vfs && fs->filename) {
00168             const char *type = ast_getformatname(f->subclass & ~0x1);
00169             fs->vfs = ast_writefile(fs->filename, type, NULL, fs->flags, 0, fs->mode);
00170             ast_log(LOG_DEBUG, "Opened video output file\n");
00171          }
00172          if (fs->vfs)
00173             return ast_writestream(fs->vfs, f);
00174          /* else ignore */
00175          return 0;            
00176       } else {
00177          /* Might / might not have mark set */
00178          alt = 1;
00179       }
00180    } else if (f->frametype != AST_FRAME_VOICE) {
00181       ast_log(LOG_WARNING, "Tried to write non-voice frame\n");
00182       return -1;
00183    }
00184    if (((fs->fmt->format | alt) & f->subclass) == f->subclass) {
00185       res =  fs->fmt->write(fs, f);
00186       if (res < 0) 
00187          ast_log(LOG_WARNING, "Natural write failed\n");
00188       else if (res > 0)
00189          ast_log(LOG_WARNING, "Huh??\n");
00190    } else {
00191       /* XXX If they try to send us a type of frame that isn't the normal frame, and isn't
00192              the one we've setup a translator for, we do the "wrong thing" XXX */
00193       if (fs->trans && f->subclass != fs->lastwriteformat) {
00194          ast_translator_free_path(fs->trans);
00195          fs->trans = NULL;
00196       }
00197       if (!fs->trans) 
00198          fs->trans = ast_translator_build_path(fs->fmt->format, f->subclass);
00199       if (!fs->trans)
00200          ast_log(LOG_WARNING, "Unable to translate to format %s, source format %s\n",
00201             fs->fmt->name, ast_getformatname(f->subclass));
00202       else {
00203          struct ast_frame *trf;
00204          fs->lastwriteformat = f->subclass;
00205          /* Get the translated frame but don't consume the original in case they're using it on another stream */
00206          if ((trf = ast_translate(fs->trans, f, 0))) {
00207             struct ast_frame *cur;
00208 
00209             /* the translator may have returned multiple frames, so process them */
00210             for (cur = trf; cur; cur = AST_LIST_NEXT(cur, frame_list)) {
00211                if ((res = fs->fmt->write(fs, trf))) {
00212                   ast_log(LOG_WARNING, "Translated frame write failed\n");
00213                   break;
00214                }
00215             }
00216             ast_frfree(trf);
00217          } else {
00218             res = 0;
00219          }
00220       }
00221    }
00222    return res;
00223 }
00224 
00225 static int copy(const char *infile, const char *outfile)
00226 {
00227    int ifd, ofd, len;
00228    char buf[4096];   /* XXX make it lerger. */
00229 
00230    if ((ifd = open(infile, O_RDONLY)) < 0) {
00231       ast_log(LOG_WARNING, "Unable to open %s in read-only mode\n", infile);
00232       return -1;
00233    }
00234    if ((ofd = open(outfile, O_WRONLY | O_TRUNC | O_CREAT, 0600)) < 0) {
00235       ast_log(LOG_WARNING, "Unable to open %s in write-only mode\n", outfile);
00236       close(ifd);
00237       return -1;
00238    }
00239    while ( (len = read(ifd, buf, sizeof(buf)) ) ) {
00240       int res;
00241       if (len < 0) {
00242          ast_log(LOG_WARNING, "Read failed on %s: %s\n", infile, strerror(errno));
00243          break;
00244       }
00245       /* XXX handle partial writes */
00246       res = write(ofd, buf, len);
00247       if (res != len) {
00248          ast_log(LOG_WARNING, "Write failed on %s (%d of %d): %s\n", outfile, res, len, strerror(errno));
00249          len = -1; /* error marker */
00250          break;
00251       }
00252    }
00253    close(ifd);
00254    close(ofd);
00255    if (len < 0) {
00256       unlink(outfile);
00257       return -1; /* error */
00258    }
00259    return 0;   /* success */
00260 }
00261 
00262 /*!
00263  * \brief construct a filename. Absolute pathnames are preserved,
00264  * relative names are prefixed by the sounds/ directory.
00265  * The wav49 suffix is replaced by 'WAV'.
00266  * Returns a malloc'ed string to be freed by the caller.
00267  */
00268 static char *build_filename(const char *filename, const char *ext)
00269 {
00270    char *fn = NULL;
00271 
00272    if (!strcmp(ext, "wav49"))
00273       ext = "WAV";
00274 
00275    if (filename[0] == '/') {
00276       if (asprintf(&fn, "%s.%s", filename, ext) < 0) {
00277          ast_log(LOG_WARNING, "asprintf() failed: %s\n", strerror(errno));
00278          fn = NULL;
00279       }
00280    } else {
00281       if (asprintf(&fn, "%s/sounds/%s.%s",
00282               ast_config_AST_DATA_DIR, filename, ext) < 0) {
00283          ast_log(LOG_WARNING, "asprintf() failed: %s\n", strerror(errno));
00284          fn = NULL;
00285       }
00286    }
00287    return fn;
00288 }
00289 
00290 /* compare type against the list 'exts' */
00291 /* XXX need a better algorithm */
00292 static int exts_compare(const char *exts, const char *type)
00293 {
00294    char tmp[256];
00295    char *stringp = tmp, *ext;
00296 
00297    ast_copy_string(tmp, exts, sizeof(tmp));
00298    while ((ext = strsep(&stringp, "|"))) {
00299       if (!strcmp(ext, type))
00300          return 1;
00301    }
00302 
00303    return 0;
00304 }
00305 
00306 static void filestream_destructor(void *arg)
00307 {
00308    char *cmd = NULL;
00309    size_t size = 0;
00310    struct ast_filestream *f = arg;
00311 
00312    /* Stop a running stream if there is one */
00313    if (f->owner) {
00314       if (f->fmt->format < AST_FORMAT_MAX_AUDIO) {
00315          f->owner->stream = NULL;
00316          AST_SCHED_DEL(f->owner->sched, f->owner->streamid);
00317 #ifdef HAVE_DAHDI
00318          ast_settimeout(f->owner, 0, NULL, NULL);
00319 #endif         
00320       } else {
00321          f->owner->vstream = NULL;
00322          AST_SCHED_DEL(f->owner->sched, f->owner->vstreamid);
00323       }
00324    }
00325    /* destroy the translator on exit */
00326    if (f->trans)
00327       ast_translator_free_path(f->trans);
00328 
00329    if (f->realfilename && f->filename) {
00330          size = strlen(f->filename) + strlen(f->realfilename) + 15;
00331          cmd = alloca(size);
00332          memset(cmd,0,size);
00333          snprintf(cmd,size,"/bin/mv -f %s %s",f->filename,f->realfilename);
00334          ast_safe_system(cmd);
00335    }
00336 
00337    if (f->filename)
00338       free(f->filename);
00339    if (f->realfilename)
00340       free(f->realfilename);
00341    if (f->fmt->close) {
00342       void (*closefn)(struct ast_filestream *) = f->fmt->close;
00343       closefn(f);
00344    }
00345    if (f->f)
00346       fclose(f->f);
00347    if (f->vfs)
00348       ast_closestream(f->vfs);
00349    if (f->orig_chan_name)
00350       free((void *) f->orig_chan_name);
00351    ast_module_unref(f->fmt->module);
00352 }
00353 
00354 static struct ast_filestream *get_filestream(struct ast_format *fmt, FILE *bfile)
00355 {
00356    struct ast_filestream *s;
00357 
00358    int l = sizeof(*s) + fmt->buf_size + fmt->desc_size;  /* total allocation size */
00359    if ( (s = ao2_alloc(l, filestream_destructor)) == NULL)
00360       return NULL;
00361    s->fmt = fmt;
00362    s->f = bfile;
00363 
00364    if (fmt->desc_size)
00365       s->_private = ((char *)(s+1)) + fmt->buf_size;
00366    if (fmt->buf_size)
00367       s->buf = (char *)(s+1);
00368    s->fr.src = fmt->name;
00369    return s;
00370 }
00371 
00372 /*
00373  * Default implementations of open and rewrite.
00374  * Only use them if you don't have expensive stuff to do.
00375  */
00376 enum wrap_fn { WRAP_OPEN, WRAP_REWRITE };
00377 
00378 static int fn_wrapper(struct ast_filestream *s, const char *comment, enum wrap_fn mode)
00379 {
00380    struct ast_format *f = s->fmt;
00381    int ret = -1;
00382    int (*openfn)(struct ast_filestream *s);
00383 
00384    if (mode == WRAP_OPEN && (openfn = f->open) && openfn(s))
00385                 ast_log(LOG_WARNING, "Unable to open format %s\n", f->name);
00386    else if (mode == WRAP_REWRITE && f->rewrite && f->rewrite(s, comment))
00387                 ast_log(LOG_WARNING, "Unable to rewrite format %s\n", f->name);
00388    else {
00389       /* preliminary checks succeed. update usecount */
00390       ast_module_ref(f->module);
00391       ret = 0;
00392    }
00393         return ret;
00394 }
00395 
00396 static int rewrite_wrapper(struct ast_filestream *s, const char *comment)
00397 {
00398    return fn_wrapper(s, comment, WRAP_REWRITE);
00399 }
00400                 
00401 static int open_wrapper(struct ast_filestream *s)
00402 {
00403    return fn_wrapper(s, NULL, WRAP_OPEN);
00404 }
00405 
00406 enum file_action {
00407    ACTION_EXISTS = 1, /* return matching format if file exists, 0 otherwise */
00408    ACTION_DELETE, /* delete file, return 0 on success, -1 on error */
00409    ACTION_RENAME, /* rename file. return 0 on success, -1 on error */
00410    ACTION_OPEN,
00411    ACTION_COPY /* copy file. return 0 on success, -1 on error */
00412 };
00413 
00414 /*!
00415  * \brief perform various actions on a file. Second argument
00416  * arg2 depends on the command:
00417  * unused for EXISTS and DELETE
00418  * destination file name (const char *) for COPY and RENAME
00419  *    struct ast_channel * for OPEN
00420  * if fmt is NULL, OPEN will return the first matching entry,
00421  * whereas other functions will run on all matching entries.
00422  */
00423 static int ast_filehelper(const char *filename, const void *arg2, const char *fmt, const enum file_action action)
00424 {
00425    struct ast_format *f;
00426    int res = (action == ACTION_EXISTS) ? 0 : -1;
00427 
00428    if (AST_LIST_LOCK(&formats)) {
00429       ast_log(LOG_WARNING, "Unable to lock format list\n");
00430       return res;
00431    }
00432    /* Check for a specific format */
00433    AST_LIST_TRAVERSE(&formats, f, list) {
00434       char *stringp, *ext = NULL;
00435 
00436       if (fmt && !exts_compare(f->exts, fmt))
00437          continue;
00438 
00439       /* Look for a file matching the supported extensions.
00440        * The file must exist, and for OPEN, must match
00441        * one of the formats supported by the channel.
00442        */
00443       stringp = ast_strdupa(f->exts);  /* this is in the stack so does not need to be freed */
00444       while ( (ext = strsep(&stringp, "|")) ) {
00445          struct stat st;
00446          char *fn = build_filename(filename, ext);
00447 
00448          if (fn == NULL)
00449             continue;
00450 
00451          if ( stat(fn, &st) ) { /* file not existent */
00452             free(fn);
00453             continue;
00454          }
00455          /* for 'OPEN' we need to be sure that the format matches
00456           * what the channel can process
00457           */
00458          if (action == ACTION_OPEN) {
00459             struct ast_channel *chan = (struct ast_channel *)arg2;
00460             FILE *bfile;
00461             struct ast_filestream *s;
00462 
00463             if ( !(chan->writeformat & f->format) &&
00464                  !(f->format >= AST_FORMAT_MAX_AUDIO && fmt)) {
00465                free(fn);
00466                continue;   /* not a supported format */
00467             }
00468             if ( (bfile = fopen(fn, "r")) == NULL) {
00469                free(fn);
00470                continue;   /* cannot open file */
00471             }
00472             s = get_filestream(f, bfile);
00473             if (!s) {
00474                fclose(bfile);
00475                free(fn);   /* cannot allocate descriptor */
00476                continue;
00477             }
00478             if (open_wrapper(s)) {
00479                free(fn);
00480                ast_closestream(s);
00481                continue;   /* cannot run open on file */
00482             }
00483             /* ok this is good for OPEN */
00484             res = 1; /* found */
00485             s->lasttimeout = -1;
00486             s->fmt = f;
00487             s->trans = NULL;
00488             s->filename = NULL;
00489             if (s->fmt->format < AST_FORMAT_MAX_AUDIO) {
00490                if (chan->stream)
00491                   ast_closestream(chan->stream);
00492                chan->stream = s;
00493             } else {
00494                if (chan->vstream)
00495                   ast_closestream(chan->vstream);
00496                chan->vstream = s;
00497             }
00498             free(fn);
00499             break;
00500          }
00501          switch (action) {
00502          case ACTION_OPEN:
00503             break;   /* will never get here */
00504 
00505          case ACTION_EXISTS:  /* return the matching format */
00506             res |= f->format;
00507             break;
00508 
00509          case ACTION_DELETE:
00510             if ( (res = unlink(fn)) )
00511                ast_log(LOG_WARNING, "unlink(%s) failed: %s\n", fn, strerror(errno));
00512             break;
00513 
00514          case ACTION_RENAME:
00515          case ACTION_COPY: {
00516             char *nfn = build_filename((const char *)arg2, ext);
00517             if (!nfn)
00518                ast_log(LOG_WARNING, "Out of memory\n");
00519             else {
00520                res = action == ACTION_COPY ? copy(fn, nfn) : rename(fn, nfn);
00521                if (res)
00522                   ast_log(LOG_WARNING, "%s(%s,%s) failed: %s\n",
00523                      action == ACTION_COPY ? "copy" : "rename",
00524                       fn, nfn, strerror(errno));
00525                free(nfn);
00526             }
00527              }
00528             break;
00529 
00530          default:
00531             ast_log(LOG_WARNING, "Unknown helper %d\n", action);
00532          }
00533          free(fn);
00534       }
00535    }
00536    AST_LIST_UNLOCK(&formats);
00537    return res;
00538 }
00539 
00540 static int is_absolute_path(const char *filename)
00541 {
00542    return filename[0] == '/';
00543 }
00544 
00545 static int fileexists_test(const char *filename, const char *fmt, const char *lang,
00546             char *buf, int buflen)
00547 {
00548    if (buf == NULL) {
00549       return -1;
00550    }
00551 
00552    if (ast_language_is_prefix && !is_absolute_path(filename)) { /* new layout */
00553       if (lang) {
00554          snprintf(buf, buflen, "%s/%s", lang, filename);
00555       } else {
00556          snprintf(buf, buflen, "%s", filename);
00557       }
00558    } else { /* old layout */
00559       strcpy(buf, filename);  /* first copy the full string */
00560       if (lang) {
00561          /* insert the language and suffix if needed */
00562          const char *c = strrchr(filename, '/');
00563          int offset = c ? c - filename + 1 : 0; /* points right after the last '/' */
00564          snprintf(buf + offset, buflen - offset, "%s/%s", lang, filename + offset);
00565       }
00566    }
00567 
00568    return ast_filehelper(buf, NULL, fmt, ACTION_EXISTS);
00569 }
00570 
00571 /*!
00572  * \brief helper routine to locate a file with a given format
00573  * and language preference.
00574  * Try preflang, preflang with stripped '_' suffix, or NULL.
00575  * In the standard asterisk, language goes just before the last component.
00576  * In an alternative configuration, the language should be a prefix
00577  * to the actual filename.
00578  *
00579  * The last parameter(s) point to a buffer of sufficient size,
00580  * which on success is filled with the matching filename.
00581  */
00582 static int fileexists_core(const char *filename, const char *fmt, const char *preflang,
00583             char *buf, int buflen)
00584 {
00585    int res = -1;
00586    char *lang = NULL;
00587 
00588    if (buf == NULL) {
00589       return -1;
00590    }
00591 
00592    /* We try languages in the following order:
00593     *    preflang (may include dialect)
00594     *    lang (preflang without dialect - if any)
00595     *    <none>
00596     *    default (unless the same as preflang or lang without dialect)
00597     */
00598 
00599    /* Try preferred language */
00600    if (!ast_strlen_zero(preflang)) {
00601       /* try the preflang exactly as it was requested */
00602       if ((res = fileexists_test(filename, fmt, preflang, buf, buflen)) > 0) {
00603          return res;
00604       } else {
00605          /* try without a dialect */
00606          char *postfix = NULL;
00607          postfix = lang = ast_strdupa(preflang);
00608 
00609          strsep(&postfix, "_");
00610          if (postfix) {
00611             if ((res = fileexists_test(filename, fmt, lang, buf, buflen)) > 0) {
00612                return res;
00613             }
00614          }
00615       }
00616    }
00617 
00618    /* Try without any language */
00619    if ((res = fileexists_test(filename, fmt, NULL, buf, buflen)) > 0) {
00620       return res;
00621    }
00622 
00623    /* Finally try the default language unless it was already tried before */
00624    if ((ast_strlen_zero(preflang) || strcmp(preflang, DEFAULT_LANGUAGE)) && (ast_strlen_zero(lang) || strcmp(lang, DEFAULT_LANGUAGE))) {
00625       if ((res = fileexists_test(filename, fmt, DEFAULT_LANGUAGE, buf, buflen)) > 0) {
00626          return res;
00627       }
00628    }
00629 
00630    return 0;
00631 }
00632 
00633 struct ast_filestream *ast_openstream(struct ast_channel *chan, const char *filename, const char *preflang)
00634 {
00635    return ast_openstream_full(chan, filename, preflang, 0);
00636 }
00637 
00638 struct ast_filestream *ast_openstream_full(struct ast_channel *chan, const char *filename, const char *preflang, int asis)
00639 {
00640    /* 
00641     * Use fileexists_core() to find a file in a compatible
00642     * language and format, set up a suitable translator,
00643     * and open the stream.
00644     */
00645    int fmts, res, buflen;
00646    char *buf;
00647 
00648    if (!asis) {
00649       /* do this first, otherwise we detect the wrong writeformat */
00650       ast_stopstream(chan);
00651       if (chan->generator)
00652          ast_deactivate_generator(chan);
00653    }
00654    if (preflang == NULL)
00655       preflang = "";
00656    buflen = strlen(preflang) + strlen(filename) + 4;
00657    buf = alloca(buflen);
00658    if (buf == NULL)
00659       return NULL;
00660    fmts = fileexists_core(filename, NULL, preflang, buf, buflen);
00661    if (fmts > 0)
00662       fmts &= AST_FORMAT_AUDIO_MASK;
00663    if (fmts < 1) {
00664       ast_log(LOG_WARNING, "File %s does not exist in any format\n", filename);
00665       return NULL;
00666    }
00667    chan->oldwriteformat = chan->writeformat;
00668    /* Set the channel to a format we can work with */
00669    res = ast_set_write_format(chan, fmts);
00670    res = ast_filehelper(buf, chan, NULL, ACTION_OPEN);
00671    if (res >= 0)
00672       return chan->stream;
00673    return NULL;
00674 }
00675 
00676 struct ast_filestream *ast_openvstream(struct ast_channel *chan, const char *filename, const char *preflang)
00677 {
00678    /* As above, but for video. But here we don't have translators
00679     * so we must enforce a format.
00680     */
00681    unsigned int format;
00682    char *buf;
00683    int buflen;
00684 
00685    if (preflang == NULL)
00686       preflang = "";
00687    buflen = strlen(preflang) + strlen(filename) + 4;
00688    buf = alloca(buflen);
00689    if (buf == NULL)
00690       return NULL;
00691 
00692    for (format = AST_FORMAT_MAX_AUDIO << 1; format <= AST_FORMAT_MAX_VIDEO; format = format << 1) {
00693       int fd;
00694       const char *fmt;
00695 
00696       if (!(chan->nativeformats & format))
00697          continue;
00698       fmt = ast_getformatname(format);
00699       if ( fileexists_core(filename, fmt, preflang, buf, buflen) < 1)   /* no valid format */
00700          continue;
00701       fd = ast_filehelper(buf, chan, fmt, ACTION_OPEN);
00702       if (fd >= 0)
00703          return chan->vstream;
00704       ast_log(LOG_WARNING, "File %s has video but couldn't be opened\n", filename);
00705    }
00706    return NULL;
00707 }
00708 
00709 static struct ast_frame *read_frame(struct ast_filestream *s, int *whennext)
00710 {
00711    struct ast_frame *fr, *new_fr;
00712 
00713    if (!s || !s->fmt) {
00714       return NULL;
00715    }
00716 
00717    if (!(fr = s->fmt->read(s, whennext))) {
00718       return NULL;
00719    }
00720 
00721    if (!(new_fr = ast_frisolate(fr))) {
00722       ast_frfree(fr);
00723       return NULL;
00724    }
00725 
00726    if (new_fr != fr) {
00727       ast_frfree(fr);
00728       fr = new_fr;
00729    }
00730 
00731    return fr;
00732 }
00733 
00734 struct ast_frame *ast_readframe(struct ast_filestream *s)
00735 {
00736    int whennext = 0;
00737 
00738    return read_frame(s, &whennext);
00739 }
00740 
00741 enum fsread_res {
00742    FSREAD_FAILURE,
00743    FSREAD_SUCCESS_SCHED,
00744    FSREAD_SUCCESS_NOSCHED,
00745 };
00746 
00747 static int ast_fsread_audio(const void *data);
00748 
00749 static enum fsread_res ast_readaudio_callback(struct ast_filestream *s)
00750 {
00751    int whennext = 0;
00752 
00753    while (!whennext) {
00754       struct ast_frame *fr;
00755 
00756       if (s->orig_chan_name && strcasecmp(s->owner->name, s->orig_chan_name)) {
00757          goto return_failure;
00758       }
00759 
00760       fr = read_frame(s, &whennext);
00761 
00762       if (!fr /* stream complete */ || ast_write(s->owner, fr) /* error writing */) {
00763          if (fr) {
00764             ast_log(LOG_WARNING, "Failed to write frame\n");
00765             ast_frfree(fr);
00766          }
00767          goto return_failure;
00768       }
00769 
00770       if (fr) {
00771          ast_frfree(fr);
00772       }
00773    }
00774 
00775    if (whennext != s->lasttimeout) {
00776 #ifdef HAVE_DAHDI
00777       if (s->owner->timingfd > -1) {
00778          int zap_timer_samples = whennext;
00779          int rate;
00780          /* whennext is in samples, but DAHDI timers operate in 8 kHz samples. */
00781          if ((rate = ast_format_rate(s->fmt->format)) != 8000) {
00782             float factor;
00783             factor = ((float) rate) / ((float) 8000.0); 
00784             zap_timer_samples = (int) ( ((float) zap_timer_samples) / factor );
00785          }
00786          ast_settimeout(s->owner, zap_timer_samples, ast_fsread_audio, s);
00787       } else
00788 #endif      
00789          s->owner->streamid = ast_sched_add(s->owner->sched, 
00790             whennext / (ast_format_rate(s->fmt->format) / 1000), 
00791             ast_fsread_audio, s);
00792       s->lasttimeout = whennext;
00793       return FSREAD_SUCCESS_NOSCHED;
00794    }
00795    return FSREAD_SUCCESS_SCHED;
00796 
00797 return_failure:
00798    s->owner->streamid = -1;
00799 #ifdef HAVE_DAHDI
00800    ast_settimeout(s->owner, 0, NULL, NULL);
00801 #endif         
00802    return FSREAD_FAILURE;
00803 }
00804 
00805 static int ast_fsread_audio(const void *data)
00806 {
00807    struct ast_filestream *fs = (struct ast_filestream *)data;
00808    enum fsread_res res;
00809 
00810    res = ast_readaudio_callback(fs);
00811 
00812    if (res == FSREAD_SUCCESS_SCHED)
00813       return 1;
00814    
00815    return 0;
00816 }
00817 
00818 static int ast_fsread_video(const void *data);
00819 
00820 static enum fsread_res ast_readvideo_callback(struct ast_filestream *s)
00821 {
00822    int whennext = 0;
00823 
00824    while (!whennext) {
00825       struct ast_frame *fr = read_frame(s, &whennext);
00826 
00827       if (!fr /* stream complete */ || ast_write(s->owner, fr) /* error writing */) {
00828          if (fr) {
00829             ast_log(LOG_WARNING, "Failed to write frame\n");
00830             ast_frfree(fr);
00831          }
00832          s->owner->vstreamid = -1;
00833          return FSREAD_FAILURE;
00834       }
00835 
00836       if (fr) {
00837          ast_frfree(fr);
00838       }
00839    }
00840 
00841    if (whennext != s->lasttimeout) {
00842       s->owner->vstreamid = ast_sched_add(s->owner->sched, 
00843          whennext / (ast_format_rate(s->fmt->format) / 1000), 
00844          ast_fsread_video, s);
00845       s->lasttimeout = whennext;
00846       return FSREAD_SUCCESS_NOSCHED;
00847    }
00848 
00849    return FSREAD_SUCCESS_SCHED;
00850 }
00851 
00852 static int ast_fsread_video(const void *data)
00853 {
00854    struct ast_filestream *fs = (struct ast_filestream *)data;
00855    enum fsread_res res;
00856 
00857    res = ast_readvideo_callback(fs);
00858 
00859    if (res == FSREAD_SUCCESS_SCHED)
00860       return 1;
00861    
00862    return 0;
00863 }
00864 
00865 int ast_applystream(struct ast_channel *chan, struct ast_filestream *s)
00866 {
00867    s->owner = chan;
00868    return 0;
00869 }
00870 
00871 int ast_playstream(struct ast_filestream *s)
00872 {
00873    enum fsread_res res;
00874 
00875    if (s->fmt->format < AST_FORMAT_MAX_AUDIO)
00876       res = ast_readaudio_callback(s);
00877    else
00878       res = ast_readvideo_callback(s);
00879 
00880    return (res == FSREAD_FAILURE) ? -1 : 0;
00881 }
00882 
00883 int ast_seekstream(struct ast_filestream *fs, off_t sample_offset, int whence)
00884 {
00885    return fs->fmt->seek(fs, sample_offset, whence);
00886 }
00887 
00888 int ast_truncstream(struct ast_filestream *fs)
00889 {
00890    return fs->fmt->trunc(fs);
00891 }
00892 
00893 off_t ast_tellstream(struct ast_filestream *fs)
00894 {
00895    return fs->fmt->tell(fs);
00896 }
00897 
00898 int ast_stream_fastforward(struct ast_filestream *fs, off_t ms)
00899 {
00900    return ast_seekstream(fs, ms * DEFAULT_SAMPLES_PER_MS, SEEK_CUR);
00901 }
00902 
00903 int ast_stream_rewind(struct ast_filestream *fs, off_t ms)
00904 {
00905    return ast_seekstream(fs, -ms * DEFAULT_SAMPLES_PER_MS, SEEK_CUR);
00906 }
00907 
00908 int ast_closestream(struct ast_filestream *f)
00909 {
00910    /* This used to destroy the filestream, but it now just decrements a refcount.
00911     * We need to force the stream to quit queuing frames now, because we might
00912     * change the writeformat, which could result in a subsequent write error, if
00913     * the format is different. */
00914 
00915    /* Stop a running stream if there is one */
00916    if (f->owner) {
00917       if (f->fmt->format < AST_FORMAT_AUDIO_MASK) {
00918          f->owner->stream = NULL;
00919          AST_SCHED_DEL(f->owner->sched, f->owner->streamid);
00920          ast_settimeout(f->owner, 0, NULL, NULL);
00921       } else {
00922          f->owner->vstream = NULL;
00923          AST_SCHED_DEL(f->owner->sched, f->owner->vstreamid);
00924       }
00925    }
00926 
00927    ao2_ref(f, -1);
00928    return 0;
00929 }
00930 
00931 
00932 /*
00933  * Look the various language-specific places where a file could exist.
00934  */
00935 int ast_fileexists(const char *filename, const char *fmt, const char *preflang)
00936 {
00937    char *buf;
00938    int buflen;
00939 
00940    if (preflang == NULL)
00941       preflang = "";
00942    buflen = strlen(preflang) + strlen(filename) + 4;  /* room for everything */
00943    buf = alloca(buflen);
00944    if (buf == NULL)
00945       return 0;
00946    return fileexists_core(filename, fmt, preflang, buf, buflen);
00947 }
00948 
00949 int ast_filedelete(const char *filename, const char *fmt)
00950 {
00951    return ast_filehelper(filename, NULL, fmt, ACTION_DELETE);
00952 }
00953 
00954 int ast_filerename(const char *filename, const char *filename2, const char *fmt)
00955 {
00956    return ast_filehelper(filename, filename2, fmt, ACTION_RENAME);
00957 }
00958 
00959 int ast_filecopy(const char *filename, const char *filename2, const char *fmt)
00960 {
00961    return ast_filehelper(filename, filename2, fmt, ACTION_COPY);
00962 }
00963 
00964 int ast_streamfile(struct ast_channel *chan, const char *filename, const char *preflang)
00965 {
00966    struct ast_filestream *fs;
00967    struct ast_filestream *vfs=NULL;
00968    char fmt[256];
00969 
00970    fs = ast_openstream(chan, filename, preflang);
00971    if (fs)
00972       vfs = ast_openvstream(chan, filename, preflang);
00973    if (vfs)
00974       ast_log(LOG_DEBUG, "Ooh, found a video stream, too, format %s\n", ast_getformatname(vfs->fmt->format));
00975    if (fs){
00976       int res;
00977       if (ast_test_flag(chan, AST_FLAG_MASQ_NOSTREAM))
00978          fs->orig_chan_name = ast_strdup(chan->name);
00979       if (ast_applystream(chan, fs))
00980          return -1;
00981       if (vfs && ast_applystream(chan, vfs))
00982          return -1;
00983       res = ast_playstream(fs);
00984       if (!res && vfs)
00985          res = ast_playstream(vfs);
00986       if (option_verbose > 2)
00987          ast_verbose(VERBOSE_PREFIX_3 "<%s> Playing '%s' (language '%s')\n", chan->name, filename, preflang ? preflang : "default");
00988 
00989       return res;
00990    }
00991    ast_log(LOG_WARNING, "Unable to open %s (format %s): %s\n", filename, ast_getformatname_multiple(fmt, sizeof(fmt), chan->nativeformats), strerror(errno));
00992    return -1;
00993 }
00994 
00995 struct ast_filestream *ast_readfile(const char *filename, const char *type, const char *comment, int flags, int check, mode_t mode)
00996 {
00997    FILE *bfile;
00998    struct ast_format *f;
00999    struct ast_filestream *fs = NULL;
01000    char *fn;
01001 
01002    if (AST_LIST_LOCK(&formats)) {
01003       ast_log(LOG_WARNING, "Unable to lock format list\n");
01004       return NULL;
01005    }
01006 
01007    AST_LIST_TRAVERSE(&formats, f, list) {
01008       fs = NULL;
01009       if (!exts_compare(f->exts, type))
01010          continue;
01011 
01012       fn = build_filename(filename, type);
01013       errno = 0;
01014       bfile = fopen(fn, "r");
01015       if (!bfile || (fs = get_filestream(f, bfile)) == NULL ||
01016           open_wrapper(fs) ) {
01017          ast_log(LOG_WARNING, "Unable to open %s\n", fn);
01018          if (fs) {
01019             ast_closestream(fs);
01020          }
01021          fs = NULL;
01022          bfile = NULL;
01023          free(fn);
01024          continue;
01025       }
01026       /* found it */
01027       fs->trans = NULL;
01028       fs->fmt = f;
01029       fs->flags = flags;
01030       fs->mode = mode;
01031       fs->filename = strdup(filename);
01032       fs->vfs = NULL;
01033       break;
01034    }
01035 
01036    AST_LIST_UNLOCK(&formats);
01037    if (!fs) 
01038       ast_log(LOG_WARNING, "No such format '%s'\n", type);
01039 
01040    return fs;
01041 }
01042 
01043 struct ast_filestream *ast_writefile(const char *filename, const char *type, const char *comment, int flags, int check, mode_t mode)
01044 {
01045    int fd, myflags = 0;
01046    /* compiler claims this variable can be used before initialization... */
01047    FILE *bfile = NULL;
01048    struct ast_format *f;
01049    struct ast_filestream *fs = NULL;
01050    char *buf = NULL;
01051    size_t size = 0;
01052    int format_found = 0;
01053 
01054    if (AST_LIST_LOCK(&formats)) {
01055       ast_log(LOG_WARNING, "Unable to lock format list\n");
01056       return NULL;
01057    }
01058 
01059    /* set the O_TRUNC flag if and only if there is no O_APPEND specified */
01060    /* We really can't use O_APPEND as it will break WAV header updates */
01061    if (flags & O_APPEND) { 
01062       flags &= ~O_APPEND;
01063    } else {
01064       myflags = O_TRUNC;
01065    }
01066    
01067    myflags |= O_WRONLY | O_CREAT;
01068 
01069    /* XXX need to fix this - we should just do the fopen,
01070     * not open followed by fdopen()
01071     */
01072    AST_LIST_TRAVERSE(&formats, f, list) {
01073       char *fn, *orig_fn = NULL;
01074       if (fs)
01075          break;
01076 
01077       if (!exts_compare(f->exts, type))
01078          continue;
01079       else
01080          format_found = 1;
01081 
01082       fn = build_filename(filename, type);
01083       fd = open(fn, flags | myflags, mode);
01084       if (fd > -1) {
01085          /* fdopen() the resulting file stream */
01086          bfile = fdopen(fd, ((flags | myflags) & O_RDWR) ? "w+" : "w");
01087          if (!bfile) {
01088             ast_log(LOG_WARNING, "Whoa, fdopen failed: %s!\n", strerror(errno));
01089             close(fd);
01090             fd = -1;
01091          }
01092       }
01093       
01094       if (ast_opt_cache_record_files && (fd > -1)) {
01095          char *c;
01096 
01097          fclose(bfile); /* this also closes fd */
01098          /*
01099            We touch orig_fn just as a place-holder so other things (like vmail) see the file is there.
01100            What we are really doing is writing to record_cache_dir until we are done then we will mv the file into place.
01101          */
01102          orig_fn = ast_strdupa(fn);
01103          for (c = fn; *c; c++)
01104             if (*c == '/')
01105                *c = '_';
01106 
01107          size = strlen(fn) + strlen(record_cache_dir) + 2;
01108          buf = alloca(size);
01109          strcpy(buf, record_cache_dir);
01110          strcat(buf, "/");
01111          strcat(buf, fn);
01112          free(fn);
01113          fn = buf;
01114          fd = open(fn, flags | myflags, mode);
01115          if (fd > -1) {
01116             /* fdopen() the resulting file stream */
01117             bfile = fdopen(fd, ((flags | myflags) & O_RDWR) ? "w+" : "w");
01118             if (!bfile) {
01119                ast_log(LOG_WARNING, "Whoa, fdopen failed: %s!\n", strerror(errno));
01120                close(fd);
01121                fd = -1;
01122             }
01123          }
01124       }
01125       if (fd > -1) {
01126          errno = 0;
01127          fs = get_filestream(f, bfile);
01128          if (!fs || rewrite_wrapper(fs, comment)) {
01129             ast_log(LOG_WARNING, "Unable to rewrite %s\n", fn);
01130             close(fd);
01131             if (orig_fn) {
01132                unlink(fn);
01133                unlink(orig_fn);
01134             }
01135             if (fs) {
01136                ast_closestream(fs);
01137                fs = NULL;
01138             }
01139             continue;
01140          }
01141          fs->trans = NULL;
01142          fs->fmt = f;
01143          fs->flags = flags;
01144          fs->mode = mode;
01145          if (orig_fn) {
01146             fs->realfilename = strdup(orig_fn);
01147             fs->filename = strdup(fn);
01148          } else {
01149             fs->realfilename = NULL;
01150             fs->filename = strdup(filename);
01151          }
01152          fs->vfs = NULL;
01153          /* If truncated, we'll be at the beginning; if not truncated, then append */
01154          f->seek(fs, 0, SEEK_END);
01155       } else if (errno != EEXIST) {
01156          ast_log(LOG_WARNING, "Unable to open file %s: %s\n", fn, strerror(errno));
01157          if (orig_fn)
01158             unlink(orig_fn);
01159       }
01160       /* if buf != NULL then fn is already free and pointing to it */
01161       if (!buf)
01162          free(fn);
01163    }
01164 
01165    AST_LIST_UNLOCK(&formats);
01166 
01167    if (!format_found)
01168       ast_log(LOG_WARNING, "No such format '%s'\n", type);
01169 
01170    return fs;
01171 }
01172 
01173 /*!
01174  * \brief the core of all waitstream() functions
01175  */
01176 static int waitstream_core(struct ast_channel *c, const char *breakon,
01177    const char *forward, const char *rewind, int skip_ms,
01178    int audiofd, int cmdfd,  const char *context)
01179 {
01180    const char *orig_chan_name = NULL;
01181    int err = 0;
01182 
01183    if (!breakon)
01184       breakon = "";
01185    if (!forward)
01186       forward = "";
01187    if (!rewind)
01188       rewind = "";
01189 
01190    /* Switch the channel to end DTMF frame only. waitstream_core doesn't care about the start of DTMF. */
01191    ast_set_flag(c, AST_FLAG_END_DTMF_ONLY);
01192 
01193    if (ast_test_flag(c, AST_FLAG_MASQ_NOSTREAM))
01194       orig_chan_name = ast_strdupa(c->name);
01195 
01196    while (c->stream) {
01197       int res;
01198       int ms;
01199 
01200       if (orig_chan_name && strcasecmp(orig_chan_name, c->name)) {
01201          ast_stopstream(c);
01202          err = 1;
01203          break;
01204       }
01205 
01206       ms = ast_sched_wait(c->sched);
01207 
01208       if (ms < 0 && !c->timingfunc) {
01209          ast_stopstream(c);
01210          break;
01211       }
01212       if (ms < 0)
01213          ms = 1000;
01214       if (cmdfd < 0) {
01215          res = ast_waitfor(c, ms);
01216          if (res < 0) {
01217             ast_log(LOG_WARNING, "Select failed (%s)\n", strerror(errno));
01218             ast_clear_flag(c, AST_FLAG_END_DTMF_ONLY);
01219             return res;
01220          }
01221       } else {
01222          int outfd;
01223          struct ast_channel *rchan = ast_waitfor_nandfds(&c, 1, &cmdfd, (cmdfd > -1) ? 1 : 0, NULL, &outfd, &ms);
01224          if (!rchan && (outfd < 0) && (ms)) {
01225             /* Continue */
01226             if (errno == EINTR)
01227                continue;
01228             ast_log(LOG_WARNING, "Wait failed (%s)\n", strerror(errno));
01229             ast_clear_flag(c, AST_FLAG_END_DTMF_ONLY);
01230             return -1;
01231          } else if (outfd > -1) { /* this requires cmdfd set */
01232             /* The FD we were watching has something waiting */
01233             ast_clear_flag(c, AST_FLAG_END_DTMF_ONLY);
01234             return 1;
01235          }
01236          /* if rchan is set, it is 'c' */
01237          res = rchan ? 1 : 0; /* map into 'res' values */
01238       }
01239       if (res > 0) {
01240          struct ast_frame *fr = ast_read(c);
01241          if (!fr) {
01242             ast_clear_flag(c, AST_FLAG_END_DTMF_ONLY);
01243             return -1;
01244          }
01245          switch(fr->frametype) {
01246          case AST_FRAME_DTMF_END:
01247             if (context) {
01248                const char exten[2] = { fr->subclass, '\0' };
01249                if (ast_exists_extension(c, context, exten, 1, c->cid.cid_num)) {
01250                   res = fr->subclass;
01251                   ast_frfree(fr);
01252                   ast_clear_flag(c, AST_FLAG_END_DTMF_ONLY);
01253                   return res;
01254                }
01255             } else {
01256                res = fr->subclass;
01257                if (strchr(forward,res)) {
01258                   int eoftest;
01259                   ast_stream_fastforward(c->stream, skip_ms);
01260                   eoftest = fgetc(c->stream->f);
01261                   if (feof(c->stream->f)) {
01262                      ast_stream_rewind(c->stream, skip_ms);
01263                   } else {
01264                      ungetc(eoftest, c->stream->f);
01265                   }
01266                } else if (strchr(rewind,res)) {
01267                   ast_stream_rewind(c->stream, skip_ms);
01268                } else if (strchr(breakon, res)) {
01269                   ast_frfree(fr);
01270                   ast_clear_flag(c, AST_FLAG_END_DTMF_ONLY);
01271                   return res;
01272                }              
01273             }
01274             break;
01275          case AST_FRAME_CONTROL:
01276             switch(fr->subclass) {
01277             case AST_CONTROL_HANGUP:
01278             case AST_CONTROL_BUSY:
01279             case AST_CONTROL_CONGESTION:
01280                ast_frfree(fr);
01281                ast_clear_flag(c, AST_FLAG_END_DTMF_ONLY);
01282                return -1;
01283             case AST_CONTROL_RINGING:
01284             case AST_CONTROL_ANSWER:
01285             case AST_CONTROL_VIDUPDATE:
01286             case AST_CONTROL_SRCUPDATE:
01287             case AST_CONTROL_HOLD:
01288             case AST_CONTROL_UNHOLD:
01289             case AST_CONTROL_PROGRESS:
01290             case AST_CONTROL_PROCEEDING:
01291                /* Unimportant */
01292                break;
01293             default:
01294                ast_log(LOG_WARNING, "Unexpected control subclass '%d'\n", fr->subclass);
01295             }
01296             break;
01297          case AST_FRAME_VOICE:
01298             /* Write audio if appropriate */
01299             if (audiofd > -1) {
01300                if (write(audiofd, fr->data, fr->datalen) < 0) {
01301                   ast_log(LOG_WARNING, "write() failed: %s\n", strerror(errno));
01302                }
01303             }
01304          default:
01305             /* Ignore all others */
01306             break;
01307          }
01308          ast_frfree(fr);
01309       }
01310       ast_sched_runq(c->sched);
01311    }
01312 
01313    ast_clear_flag(c, AST_FLAG_END_DTMF_ONLY);
01314 
01315    return (err || c->_softhangup) ? -1 : 0;
01316 }
01317 
01318 int ast_waitstream_fr(struct ast_channel *c, const char *breakon, const char *forward, const char *rewind, int ms)
01319 {
01320    return waitstream_core(c, breakon, forward, rewind, ms,
01321       -1 /* no audiofd */, -1 /* no cmdfd */, NULL /* no context */);
01322 }
01323 
01324 int ast_waitstream(struct ast_channel *c, const char *breakon)
01325 {
01326    return waitstream_core(c, breakon, NULL, NULL, 0, -1, -1, NULL);
01327 }
01328 
01329 int ast_waitstream_full(struct ast_channel *c, const char *breakon, int audiofd, int cmdfd)
01330 {
01331    return waitstream_core(c, breakon, NULL, NULL, 0,
01332       audiofd, cmdfd, NULL /* no context */);
01333 }
01334 
01335 int ast_waitstream_exten(struct ast_channel *c, const char *context)
01336 {
01337    /* Waitstream, with return in the case of a valid 1 digit extension */
01338    /* in the current or specified context being pressed */
01339 
01340    if (!context)
01341       context = c->context;
01342    return waitstream_core(c, NULL, NULL, NULL, 0,
01343       -1, -1, context);
01344 }
01345 
01346 /*
01347  * if the file name is non-empty, try to play it.
01348  * Return 0 if success, -1 if error, digit if interrupted by a digit.
01349  * If digits == "" then we can simply check for non-zero.
01350  */
01351 int ast_stream_and_wait(struct ast_channel *chan, const char *file,
01352    const char *language, const char *digits)
01353 {
01354         int res = 0;
01355         if (!ast_strlen_zero(file)) {
01356                 res =  ast_streamfile(chan, file, language);
01357                 if (!res)
01358                         res = ast_waitstream(chan, digits);
01359         }
01360         return res;
01361 } 
01362 
01363 static int show_file_formats(int fd, int argc, char *argv[])
01364 {
01365 #define FORMAT "%-10s %-10s %-20s\n"
01366 #define FORMAT2 "%-10s %-10s %-20s\n"
01367    struct ast_format *f;
01368    int count_fmt = 0;
01369 
01370    if (argc != 4)
01371       return RESULT_SHOWUSAGE;
01372    ast_cli(fd, FORMAT, "Format", "Name", "Extensions");
01373            
01374    if (AST_LIST_LOCK(&formats)) {
01375       ast_log(LOG_WARNING, "Unable to lock format list\n");
01376       return -1;
01377    }
01378 
01379    AST_LIST_TRAVERSE(&formats, f, list) {
01380       ast_cli(fd, FORMAT2, ast_getformatname(f->format), f->name, f->exts);
01381       count_fmt++;
01382    }
01383    AST_LIST_UNLOCK(&formats);
01384    ast_cli(fd, "%d file formats registered.\n", count_fmt);
01385    return RESULT_SUCCESS;
01386 #undef FORMAT
01387 #undef FORMAT2
01388 }
01389 
01390 static int show_file_formats_deprecated(int fd, int argc, char *argv[])
01391 {
01392 #define FORMAT "%-10s %-10s %-20s\n"
01393 #define FORMAT2 "%-10s %-10s %-20s\n"
01394    struct ast_format *f;
01395    int count_fmt = 0;
01396    
01397    if (argc != 3)
01398       return RESULT_SHOWUSAGE;
01399    ast_cli(fd, FORMAT, "Format", "Name", "Extensions");
01400    
01401    if (AST_LIST_LOCK(&formats)) {
01402       ast_log(LOG_WARNING, "Unable to lock format list\n");
01403       return -1;
01404    }
01405    
01406    AST_LIST_TRAVERSE(&formats, f, list) {
01407       ast_cli(fd, FORMAT2, ast_getformatname(f->format), f->name, f->exts);
01408       count_fmt++;
01409    }
01410    AST_LIST_UNLOCK(&formats);
01411    ast_cli(fd, "%d file formats registered.\n", count_fmt);
01412    return RESULT_SUCCESS;
01413 #undef FORMAT
01414 #undef FORMAT2
01415 }
01416 
01417 char show_file_formats_usage[] = 
01418 "Usage: core show file formats\n"
01419 "       Displays currently registered file formats (if any)\n";
01420 
01421 struct ast_cli_entry cli_show_file_formats_deprecated = {
01422    { "show", "file", "formats" },
01423    show_file_formats_deprecated, NULL,
01424    NULL };
01425 
01426 struct ast_cli_entry cli_file[] = {
01427    { { "core", "show", "file", "formats" },
01428    show_file_formats, "Displays file formats",
01429    show_file_formats_usage, NULL, &cli_show_file_formats_deprecated },
01430 };
01431 
01432 int ast_file_init(void)
01433 {
01434    ast_cli_register_multiple(cli_file, sizeof(cli_file) / sizeof(struct ast_cli_entry));
01435    return 0;
01436 }

Generated on Thu Dec 17 15:35:39 2009 for Asterisk - the Open Source PBX by  doxygen 1.4.7