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

Generated on Tue Apr 28 22:50:07 2009 for Asterisk - the Open Source PBX by  doxygen 1.4.7