Sat Nov 1 06:29:04 2008

Asterisk developer's documentation


file.c File Reference

Generic File Format Support. More...

#include "asterisk.h"
#include <sys/types.h>
#include <errno.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <fcntl.h>
#include <dirent.h>
#include <sys/stat.h>
#include "asterisk/frame.h"
#include "asterisk/file.h"
#include "asterisk/cli.h"
#include "asterisk/logger.h"
#include "asterisk/channel.h"
#include "asterisk/sched.h"
#include "asterisk/options.h"
#include "asterisk/translate.h"
#include "asterisk/utils.h"
#include "asterisk/lock.h"
#include "asterisk/app.h"
#include "asterisk/pbx.h"
#include "asterisk/linkedlists.h"
#include "asterisk/module.h"

Include dependency graph for file.c:

Go to the source code of this file.

Defines

#define FORMAT   "%-10s %-10s %-20s\n"
#define FORMAT   "%-10s %-10s %-20s\n"
#define FORMAT2   "%-10s %-10s %-20s\n"
#define FORMAT2   "%-10s %-10s %-20s\n"

Enumerations

enum  file_action {
  ACTION_EXISTS = 1, ACTION_DELETE, ACTION_RENAME, ACTION_OPEN,
  ACTION_COPY
}
enum  fsread_res { FSREAD_FAILURE, FSREAD_SUCCESS_SCHED, FSREAD_SUCCESS_NOSCHED }
enum  wrap_fn { WRAP_OPEN, WRAP_REWRITE }

Functions

int __ast_format_register (const struct ast_format *f, struct ast_module *mod)
int ast_applystream (struct ast_channel *chan, struct ast_filestream *s)
int ast_closestream (struct ast_filestream *f)
int ast_file_init (void)
int ast_filecopy (const char *filename, const char *filename2, const char *fmt)
int ast_filedelete (const char *filename, const char *fmt)
int ast_fileexists (const char *filename, const char *fmt, const char *preflang)
static int ast_filehelper (const char *filename, const void *arg2, const char *fmt, const enum file_action action)
 perform various actions on a file. Second argument arg2 depends on the command: unused for EXISTS and DELETE destination file name (const char *) for COPY and RENAME struct ast_channel * for OPEN if fmt is NULL, OPEN will return the first matching entry, whereas other functions will run on all matching entries.
int ast_filerename (const char *filename, const char *filename2, const char *fmt)
int ast_format_unregister (const char *name)
static int ast_fsread_audio (const void *data)
static int ast_fsread_video (const void *data)
static AST_LIST_HEAD_STATIC (formats, ast_format)
ast_filestreamast_openstream (struct ast_channel *chan, const char *filename, const char *preflang)
ast_filestreamast_openstream_full (struct ast_channel *chan, const char *filename, const char *preflang, int asis)
ast_filestreamast_openvstream (struct ast_channel *chan, const char *filename, const char *preflang)
int ast_playstream (struct ast_filestream *s)
static enum fsread_res ast_readaudio_callback (struct ast_filestream *s)
ast_filestreamast_readfile (const char *filename, const char *type, const char *comment, int flags, int check, mode_t mode)
ast_frameast_readframe (struct ast_filestream *s)
static enum fsread_res ast_readvideo_callback (struct ast_filestream *s)
int ast_seekstream (struct ast_filestream *fs, off_t sample_offset, int whence)
int ast_stopstream (struct ast_channel *tmp)
 Stops a stream.
int ast_stream_and_wait (struct ast_channel *chan, const char *file, const char *language, const char *digits)
int ast_stream_fastforward (struct ast_filestream *fs, off_t ms)
int ast_stream_rewind (struct ast_filestream *fs, off_t ms)
int ast_streamfile (struct ast_channel *chan, const char *filename, const char *preflang)
off_t ast_tellstream (struct ast_filestream *fs)
int ast_truncstream (struct ast_filestream *fs)
int ast_waitstream (struct ast_channel *c, const char *breakon)
int ast_waitstream_exten (struct ast_channel *c, const char *context)
int ast_waitstream_fr (struct ast_channel *c, const char *breakon, const char *forward, const char *rewind, int ms)
int ast_waitstream_full (struct ast_channel *c, const char *breakon, int audiofd, int cmdfd)
ast_filestreamast_writefile (const char *filename, const char *type, const char *comment, int flags, int check, mode_t mode)
int ast_writestream (struct ast_filestream *fs, struct ast_frame *f)
static char * build_filename (const char *filename, const char *ext)
 construct a filename. Absolute pathnames are preserved, relative names are prefixed by the sounds/ directory. The wav49 suffix is replaced by 'WAV'. Returns a malloc'ed string to be freed by the caller.
static int copy (const char *infile, const char *outfile)
static int exts_compare (const char *exts, const char *type)
static int fileexists_core (const char *filename, const char *fmt, const char *preflang, char *buf, int buflen)
 helper routine to locate a file with a given format and language preference. Try preflang, preflang with stripped '_' suffix, or NULL. In the standard asterisk, language goes just before the last component. In an alternative configuration, the language should be a prefix to the actual filename.
static int fileexists_test (const char *filename, const char *fmt, const char *lang, char *buf, int buflen)
static int fn_wrapper (struct ast_filestream *s, const char *comment, enum wrap_fn mode)
static struct ast_filestreamget_filestream (struct ast_format *fmt, FILE *bfile)
static int is_absolute_path (const char *filename)
static int open_wrapper (struct ast_filestream *s)
static int rewrite_wrapper (struct ast_filestream *s, const char *comment)
static int show_file_formats (int fd, int argc, char *argv[])
static int show_file_formats_deprecated (int fd, int argc, char *argv[])
static int waitstream_core (struct ast_channel *c, const char *breakon, const char *forward, const char *rewind, int skip_ms, int audiofd, int cmdfd, const char *context)
 the core of all waitstream() functions

Variables

int ast_language_is_prefix
ast_cli_entry cli_file []
ast_cli_entry cli_show_file_formats_deprecated
char show_file_formats_usage []


Detailed Description

Generic File Format Support.

Author:
Mark Spencer <markster@digium.com>

Definition in file file.c.


Define Documentation

#define FORMAT   "%-10s %-10s %-20s\n"

#define FORMAT   "%-10s %-10s %-20s\n"

#define FORMAT2   "%-10s %-10s %-20s\n"

#define FORMAT2   "%-10s %-10s %-20s\n"

Referenced by __iax2_show_peers(), __sip_show_channels(), _sip_show_peers(), dahdi_show_channels(), dahdi_show_status(), dundi_show_mappings(), dundi_show_peers(), dundi_show_precache(), dundi_show_requests(), dundi_show_trans(), iax2_show_channels(), iax2_show_firmware(), iax2_show_registry(), iax2_show_users(), show_file_formats(), show_file_formats_deprecated(), show_image_formats(), show_image_formats_deprecated(), sip_show_inuse(), and sip_show_registry().


Enumeration Type Documentation

enum file_action

Enumerator:
ACTION_EXISTS 
ACTION_DELETE 
ACTION_RENAME 
ACTION_OPEN 
ACTION_COPY 

Definition at line 343 of file file.c.

00343                  {
00344    ACTION_EXISTS = 1, /* return matching format if file exists, 0 otherwise */
00345    ACTION_DELETE, /* delete file, return 0 on success, -1 on error */
00346    ACTION_RENAME, /* rename file. return 0 on success, -1 on error */
00347    ACTION_OPEN,
00348    ACTION_COPY /* copy file. return 0 on success, -1 on error */
00349 };

enum fsread_res

Enumerator:
FSREAD_FAILURE 
FSREAD_SUCCESS_SCHED 
FSREAD_SUCCESS_NOSCHED 

Definition at line 656 of file file.c.

00656                 {
00657    FSREAD_FAILURE,
00658    FSREAD_SUCCESS_SCHED,
00659    FSREAD_SUCCESS_NOSCHED,
00660 };

enum wrap_fn

Enumerator:
WRAP_OPEN 
WRAP_REWRITE 

Definition at line 314 of file file.c.

00314 { WRAP_OPEN, WRAP_REWRITE };


Function Documentation

int __ast_format_register ( const struct ast_format f,
struct ast_module mod 
)

Register a new file format capability Adds a format to Asterisk's format abilities. returns 0 on success, -1 on failure

Definition at line 68 of file file.c.

References ast_calloc, AST_LIST_INSERT_HEAD, AST_LIST_LOCK, AST_LIST_TRAVERSE, AST_LIST_UNLOCK, ast_log(), ast_verbose(), ast_format::buf_size, ast_format::exts, f, LOG_WARNING, ast_format::module, ast_format::name, option_verbose, and VERBOSE_PREFIX_2.

00069 {
00070    struct ast_format *tmp;
00071 
00072    if (AST_LIST_LOCK(&formats)) {
00073       ast_log(LOG_WARNING, "Unable to lock format list\n");
00074       return -1;
00075    }
00076    AST_LIST_TRAVERSE(&formats, tmp, list) {
00077       if (!strcasecmp(f->name, tmp->name)) {
00078          AST_LIST_UNLOCK(&formats);
00079          ast_log(LOG_WARNING, "Tried to register '%s' format, already registered\n", f->name);
00080          return -1;
00081       }
00082    }
00083    if (!(tmp = ast_calloc(1, sizeof(*tmp)))) {
00084       AST_LIST_UNLOCK(&formats);
00085       return -1;
00086    }
00087    *tmp = *f;
00088    tmp->module = mod;
00089    if (tmp->buf_size) {
00090       /*
00091        * Align buf_size properly, rounding up to the machine-specific
00092        * alignment for pointers.
00093        */
00094       struct _test_align { void *a, *b; } p;
00095       int align = (char *)&p.b - (char *)&p.a;
00096       tmp->buf_size = ((f->buf_size + align - 1)/align)*align;
00097    }
00098    
00099    memset(&tmp->list, 0, sizeof(tmp->list));
00100 
00101    AST_LIST_INSERT_HEAD(&formats, tmp, list);
00102    AST_LIST_UNLOCK(&formats);
00103    if (option_verbose > 1)
00104       ast_verbose( VERBOSE_PREFIX_2 "Registered file format %s, extension(s) %s\n", f->name, f->exts);
00105 
00106    return 0;
00107 }

int ast_applystream ( struct ast_channel chan,
struct ast_filestream s 
)

Parameters:
chan channel to work
s ast_filestream to apply Returns 0 for success, -1 on failure

Definition at line 764 of file file.c.

References s.

Referenced by ast_streamfile(), handle_getoption(), handle_recordfile(), handle_streamfile(), and speech_streamfile().

00765 {
00766    s->owner = chan;
00767    return 0;
00768 }

int ast_closestream ( struct ast_filestream f  ) 

Parameters:
f filestream to close Close a playback or recording stream Returns 0 on success, -1 on failure

Definition at line 807 of file file.c.

References ast_closestream(), AST_FORMAT_MAX_AUDIO, ast_module_unref(), ast_safe_system(), AST_SCHED_DEL, ast_settimeout(), ast_translator_free_path(), ast_format::close, f, ast_format::format, free, and ast_format::module.

Referenced by __ast_play_and_record(), ast_closestream(), ast_filehelper(), ast_hangup(), ast_moh_files_next(), ast_monitor_start(), ast_monitor_stop(), ast_stopstream(), cli_audio_convert(), cli_audio_convert_deprecated(), dictate_exec(), gen_closestream(), handle_recordfile(), local_ast_moh_stop(), mixmonitor_thread(), moh_files_release(), and rpt().

00808 {
00809    char *cmd = NULL;
00810    size_t size = 0;
00811    /* Stop a running stream if there is one */
00812    if (f->owner) {
00813       if (f->fmt->format < AST_FORMAT_MAX_AUDIO) {
00814          f->owner->stream = NULL;
00815          AST_SCHED_DEL(f->owner->sched, f->owner->streamid);
00816 #ifdef HAVE_DAHDI
00817          ast_settimeout(f->owner, 0, NULL, NULL);
00818 #endif         
00819       } else {
00820          f->owner->vstream = NULL;
00821          AST_SCHED_DEL(f->owner->sched, f->owner->vstreamid);
00822       }
00823    }
00824    /* destroy the translator on exit */
00825    if (f->trans)
00826       ast_translator_free_path(f->trans);
00827 
00828    if (f->realfilename && f->filename) {
00829          size = strlen(f->filename) + strlen(f->realfilename) + 15;
00830          cmd = alloca(size);
00831          memset(cmd,0,size);
00832          snprintf(cmd,size,"/bin/mv -f %s %s",f->filename,f->realfilename);
00833          ast_safe_system(cmd);
00834    }
00835 
00836    if (f->filename)
00837       free(f->filename);
00838    if (f->realfilename)
00839       free(f->realfilename);
00840    if (f->fmt->close)
00841       f->fmt->close(f);
00842    fclose(f->f);
00843    if (f->vfs)
00844       ast_closestream(f->vfs);
00845    if (f->orig_chan_name)
00846       free((void *) f->orig_chan_name);
00847    ast_module_unref(f->fmt->module);
00848    free(f);
00849    return 0;
00850 }

int ast_file_init ( void   ) 

Initializes all the various file stuff. Basically just registers the cli stuff Returns 0 all the time

Definition at line 1340 of file file.c.

References ast_cli_register_multiple(), and cli_file.

Referenced by main().

01341 {
01342    ast_cli_register_multiple(cli_file, sizeof(cli_file) / sizeof(struct ast_cli_entry));
01343    return 0;
01344 }

int ast_filecopy ( const char *  oldname,
const char *  newname,
const char *  fmt 
)

Parameters:
oldname name of the file you wish to copy (minus extension)
newname name you wish the file to be copied to (minus extension)
fmt the format of the file Copy a given file in a given format, or if fmt is NULL, then do so for all

Definition at line 880 of file file.c.

References ast_filehelper().

Referenced by copy_plain_file(), and vm_forwardoptions().

00881 {
00882    return ast_filehelper(filename, filename2, fmt, ACTION_COPY);
00883 }

int ast_filedelete ( const char *  filename,
const char *  fmt 
)

Parameters:
filename name of the file you wish to delete (minus the extension)
fmt of the file Delete a given file in a given format, or if fmt is NULL, then do so for all

Definition at line 870 of file file.c.

References ACTION_DELETE, and ast_filehelper().

Referenced by __ast_play_and_record(), ast_monitor_start(), ast_monitor_stop(), cli_audio_convert(), cli_audio_convert_deprecated(), leave_voicemail(), play_mailbox_owner(), play_record_review(), vm_delete(), and vm_forwardoptions().

00871 {
00872    return ast_filehelper(filename, NULL, fmt, ACTION_DELETE);
00873 }

int ast_fileexists ( const char *  filename,
const char *  fmt,
const char *  preflang 
)

Parameters:
filename name of the file you wish to check, minus the extension
fmt the format you wish to check (the extension)
preflang (the preferred language you wisht to find the file in) See if a given file exists in a given format. If fmt is NULL, any format is accepted. Returns -1 if file does not exist, non-zero positive otherwise.

Definition at line 856 of file file.c.

References ast_filestream::buf, and fileexists_core().

Referenced by app_exec(), ast_moh_files_next(), ast_monitor_start(), ast_monitor_stop(), common_exec(), conf_run(), last_message_index(), leave_voicemail(), play_greeting(), play_mailbox_owner(), play_message_callerid(), record_exec(), retrydial_exec(), say_character_str_full(), say_digit_str_full(), say_phonetic_str_full(), vm_intro(), vm_newuser(), vm_options(), and vm_tempgreeting().

00857 {
00858    char *buf;
00859    int buflen;
00860 
00861    if (preflang == NULL)
00862       preflang = "";
00863    buflen = strlen(preflang) + strlen(filename) + 4;  /* room for everything */
00864    buf = alloca(buflen);
00865    if (buf == NULL)
00866       return 0;
00867    return fileexists_core(filename, fmt, preflang, buf, buflen);
00868 }

static int ast_filehelper ( const char *  filename,
const void *  arg2,
const char *  fmt,
const enum file_action  action 
) [static]

perform various actions on a file. Second argument arg2 depends on the command: unused for EXISTS and DELETE destination file name (const char *) for COPY and RENAME struct ast_channel * for OPEN if fmt is NULL, OPEN will return the first matching entry, whereas other functions will run on all matching entries.

Definition at line 360 of file file.c.

References ACTION_DELETE, ACTION_EXISTS, ACTION_OPEN, ACTION_RENAME, ast_closestream(), AST_FORMAT_MAX_AUDIO, AST_LIST_LOCK, AST_LIST_TRAVERSE, AST_LIST_UNLOCK, ast_log(), ast_strdupa, build_filename(), copy(), errno, ext, ast_format::exts, exts_compare(), f, ast_format::format, free, get_filestream(), LOG_WARNING, open_wrapper(), s, ast_channel::stream, strsep(), ast_channel::vstream, and ast_channel::writeformat.

Referenced by ast_filecopy(), ast_filedelete(), ast_filerename(), ast_openstream_full(), ast_openvstream(), and fileexists_test().

00361 {
00362    struct ast_format *f;
00363    int res = (action == ACTION_EXISTS) ? 0 : -1;
00364 
00365    if (AST_LIST_LOCK(&formats)) {
00366       ast_log(LOG_WARNING, "Unable to lock format list\n");
00367       return res;
00368    }
00369    /* Check for a specific format */
00370    AST_LIST_TRAVERSE(&formats, f, list) {
00371       char *stringp, *ext = NULL;
00372 
00373       if (fmt && !exts_compare(f->exts, fmt))
00374          continue;
00375 
00376       /* Look for a file matching the supported extensions.
00377        * The file must exist, and for OPEN, must match
00378        * one of the formats supported by the channel.
00379        */
00380       stringp = ast_strdupa(f->exts);  /* this is in the stack so does not need to be freed */
00381       while ( (ext = strsep(&stringp, "|")) ) {
00382          struct stat st;
00383          char *fn = build_filename(filename, ext);
00384 
00385          if (fn == NULL)
00386             continue;
00387 
00388          if ( stat(fn, &st) ) { /* file not existent */
00389             free(fn);
00390             continue;
00391          }
00392          /* for 'OPEN' we need to be sure that the format matches
00393           * what the channel can process
00394           */
00395          if (action == ACTION_OPEN) {
00396             struct ast_channel *chan = (struct ast_channel *)arg2;
00397             FILE *bfile;
00398             struct ast_filestream *s;
00399 
00400             if ( !(chan->writeformat & f->format) &&
00401                  !(f->format >= AST_FORMAT_MAX_AUDIO && fmt)) {
00402                free(fn);
00403                continue;   /* not a supported format */
00404             }
00405             if ( (bfile = fopen(fn, "r")) == NULL) {
00406                free(fn);
00407                continue;   /* cannot open file */
00408             }
00409             s = get_filestream(f, bfile);
00410             if (!s) {
00411                fclose(bfile);
00412                free(fn);   /* cannot allocate descriptor */
00413                continue;
00414             }
00415             if (open_wrapper(s)) {
00416                fclose(bfile);
00417                free(fn);
00418                free(s);
00419                continue;   /* cannot run open on file */
00420             }
00421             /* ok this is good for OPEN */
00422             res = 1; /* found */
00423             s->lasttimeout = -1;
00424             s->fmt = f;
00425             s->trans = NULL;
00426             s->filename = NULL;
00427             if (s->fmt->format < AST_FORMAT_MAX_AUDIO) {
00428                if (chan->stream)
00429                   ast_closestream(chan->stream);
00430                chan->stream = s;
00431             } else {
00432                if (chan->vstream)
00433                   ast_closestream(chan->vstream);
00434                chan->vstream = s;
00435             }
00436             free(fn);
00437             break;
00438          }
00439          switch (action) {
00440          case ACTION_OPEN:
00441             break;   /* will never get here */
00442 
00443          case ACTION_EXISTS:  /* return the matching format */
00444             res |= f->format;
00445             break;
00446 
00447          case ACTION_DELETE:
00448             if ( (res = unlink(fn)) )
00449                ast_log(LOG_WARNING, "unlink(%s) failed: %s\n", fn, strerror(errno));
00450             break;
00451 
00452          case ACTION_RENAME:
00453          case ACTION_COPY: {
00454             char *nfn = build_filename((const char *)arg2, ext);
00455             if (!nfn)
00456                ast_log(LOG_WARNING, "Out of memory\n");
00457             else {
00458                res = action == ACTION_COPY ? copy(fn, nfn) : rename(fn, nfn);
00459                if (res)
00460                   ast_log(LOG_WARNING, "%s(%s,%s) failed: %s\n",
00461                      action == ACTION_COPY ? "copy" : "rename",
00462                       fn, nfn, strerror(errno));
00463                free(nfn);
00464             }
00465              }
00466             break;
00467 
00468          default:
00469             ast_log(LOG_WARNING, "Unknown helper %d\n", action);
00470          }
00471          free(fn);
00472       }
00473    }
00474    AST_LIST_UNLOCK(&formats);
00475    return res;
00476 }

int ast_filerename ( const char *  oldname,
const char *  newname,
const char *  fmt 
)

Parameters:
oldname the name of the file you wish to act upon (minus the extension)
newname the name you wish to rename the file to (minus the extension)
fmt the format of the file Rename a given file in a given format, or if fmt is NULL, then do so for all Returns -1 on failure

Definition at line 875 of file file.c.

References ACTION_RENAME, and ast_filehelper().

Referenced by __ast_play_and_record(), ast_monitor_stop(), leave_voicemail(), play_record_review(), and rename_file().

00876 {
00877    return ast_filehelper(filename, filename2, fmt, ACTION_RENAME);
00878 }

int ast_format_unregister ( const char *  name  ) 

Parameters:
name the name of the format you wish to unregister Unregisters a format based on the name of the format. Returns 0 on success, -1 on failure to unregister

Definition at line 109 of file file.c.

References AST_LIST_LOCK, AST_LIST_REMOVE_CURRENT, AST_LIST_TRAVERSE_SAFE_BEGIN, AST_LIST_TRAVERSE_SAFE_END, AST_LIST_UNLOCK, ast_log(), ast_verbose(), free, LOG_WARNING, ast_format::name, option_verbose, and VERBOSE_PREFIX_2.

Referenced by unload_module().

00110 {
00111    struct ast_format *tmp;
00112    int res = -1;
00113 
00114    if (AST_LIST_LOCK(&formats)) {
00115       ast_log(LOG_WARNING, "Unable to lock format list\n");
00116       return -1;
00117    }
00118    AST_LIST_TRAVERSE_SAFE_BEGIN(&formats, tmp, list) {
00119       if (!strcasecmp(name, tmp->name)) {
00120          AST_LIST_REMOVE_CURRENT(&formats, list);
00121          free(tmp);
00122          res = 0;
00123       }
00124    }
00125    AST_LIST_TRAVERSE_SAFE_END
00126    AST_LIST_UNLOCK(&formats);
00127 
00128    if (!res) {
00129       if (option_verbose > 1)
00130          ast_verbose( VERBOSE_PREFIX_2 "Unregistered format %s\n", name);
00131    } else
00132       ast_log(LOG_WARNING, "Tried to unregister format %s, already unregistered\n", name);
00133 
00134    return res;
00135 }

static int ast_fsread_audio ( const void *  data  )  [static]

Definition at line 711 of file file.c.

References ast_readaudio_callback(), and FSREAD_SUCCESS_SCHED.

Referenced by ast_readaudio_callback().

00712 {
00713    struct ast_filestream *fs = (struct ast_filestream *)data;
00714    enum fsread_res res;
00715 
00716    res = ast_readaudio_callback(fs);
00717 
00718    if (res == FSREAD_SUCCESS_SCHED)
00719       return 1;
00720    
00721    return 0;
00722 }

static int ast_fsread_video ( const void *  data  )  [static]

Definition at line 751 of file file.c.

References ast_readvideo_callback(), and FSREAD_SUCCESS_SCHED.

Referenced by ast_readvideo_callback().

00752 {
00753    struct ast_filestream *fs = (struct ast_filestream *)data;
00754    enum fsread_res res;
00755 
00756    res = ast_readvideo_callback(fs);
00757 
00758    if (res == FSREAD_SUCCESS_SCHED)
00759       return 1;
00760    
00761    return 0;
00762 }

static AST_LIST_HEAD_STATIC ( formats  ,
ast_format   
) [static]

struct ast_filestream* ast_openstream ( struct ast_channel chan,
const char *  filename,
const char *  preflang 
)

Parameters:
chan channel to work with
filename to use
preflang prefered language to use Returns a ast_filestream pointer if it opens the file, NULL on error

Definition at line 571 of file file.c.

References ast_openstream_full().

Referenced by ast_streamfile(), dictate_exec(), handle_getoption(), handle_streamfile(), and speech_streamfile().

00572 {
00573    return ast_openstream_full(chan, filename, preflang, 0);
00574 }

struct ast_filestream* ast_openstream_full ( struct ast_channel chan,
const char *  filename,
const char *  preflang,
int  asis 
)

Parameters:
chan channel to work with
filename to use
preflang prefered language to use
asis if set, don't clear generators Returns a ast_filestream pointer if it opens the file, NULL on error

Definition at line 576 of file file.c.

References ACTION_OPEN, ast_deactivate_generator(), ast_filehelper(), AST_FORMAT_AUDIO_MASK, ast_log(), ast_set_write_format(), ast_stopstream(), ast_filestream::buf, fileexists_core(), LOG_WARNING, ast_channel::oldwriteformat, ast_channel::stream, and ast_channel::writeformat.

Referenced by ast_moh_files_next(), ast_openstream(), and gen_nextfile().

00577 {
00578    /* 
00579     * Use fileexists_core() to find a file in a compatible
00580     * language and format, set up a suitable translator,
00581     * and open the stream.
00582     */
00583    int fmts, res, buflen;
00584    char *buf;
00585 
00586    if (!asis) {
00587       /* do this first, otherwise we detect the wrong writeformat */
00588       ast_stopstream(chan);
00589       if (chan->generator)
00590          ast_deactivate_generator(chan);
00591    }
00592    if (preflang == NULL)
00593       preflang = "";
00594    buflen = strlen(preflang) + strlen(filename) + 4;
00595    buf = alloca(buflen);
00596    if (buf == NULL)
00597       return NULL;
00598    fmts = fileexists_core(filename, NULL, preflang, buf, buflen);
00599    if (fmts > 0)
00600       fmts &= AST_FORMAT_AUDIO_MASK;
00601    if (fmts < 1) {
00602       ast_log(LOG_WARNING, "File %s does not exist in any format\n", filename);
00603       return NULL;
00604    }
00605    chan->oldwriteformat = chan->writeformat;
00606    /* Set the channel to a format we can work with */
00607    res = ast_set_write_format(chan, fmts);
00608    res = ast_filehelper(buf, chan, NULL, ACTION_OPEN);
00609    if (res >= 0)
00610       return chan->stream;
00611    return NULL;
00612 }

struct ast_filestream* ast_openvstream ( struct ast_channel chan,
const char *  filename,
const char *  preflang 
)

Parameters:
chan channel to work with
filename to use
preflang prefered language to use Returns a ast_filestream pointer if it opens the file, NULL on error

Definition at line 614 of file file.c.

References ACTION_OPEN, ast_filehelper(), AST_FORMAT_MAX_AUDIO, AST_FORMAT_MAX_VIDEO, ast_getformatname(), ast_log(), ast_filestream::buf, fileexists_core(), fmt, format, LOG_WARNING, ast_channel::nativeformats, and ast_channel::vstream.

Referenced by ast_streamfile(), handle_getoption(), and handle_streamfile().

00615 {
00616    /* As above, but for video. But here we don't have translators
00617     * so we must enforce a format.
00618     */
00619    unsigned int format;
00620    char *buf;
00621    int buflen;
00622 
00623    if (preflang == NULL)
00624       preflang = "";
00625    buflen = strlen(preflang) + strlen(filename) + 4;
00626    buf = alloca(buflen);
00627    if (buf == NULL)
00628       return NULL;
00629 
00630    for (format = AST_FORMAT_MAX_AUDIO << 1; format <= AST_FORMAT_MAX_VIDEO; format = format << 1) {
00631       int fd;
00632       const char *fmt;
00633 
00634       if (!(chan->nativeformats & format))
00635          continue;
00636       fmt = ast_getformatname(format);
00637       if ( fileexists_core(filename, fmt, preflang, buf, buflen) < 1)   /* no valid format */
00638          continue;
00639       fd = ast_filehelper(buf, chan, fmt, ACTION_OPEN);
00640       if (fd >= 0)
00641          return chan->vstream;
00642       ast_log(LOG_WARNING, "File %s has video but couldn't be opened\n", filename);
00643    }
00644    return NULL;
00645 }

int ast_playstream ( struct ast_filestream s  ) 

Parameters:
s filestream to play Returns 0 for success, -1 on failure

Definition at line 770 of file file.c.

References AST_FORMAT_MAX_AUDIO, ast_readaudio_callback(), ast_readvideo_callback(), FSREAD_FAILURE, and s.

Referenced by ast_streamfile(), handle_getoption(), handle_streamfile(), and speech_streamfile().

00771 {
00772    enum fsread_res res;
00773 
00774    if (s->fmt->format < AST_FORMAT_MAX_AUDIO)
00775       res = ast_readaudio_callback(s);
00776    else
00777       res = ast_readvideo_callback(s);
00778 
00779    return (res == FSREAD_FAILURE) ? -1 : 0;
00780 }

static enum fsread_res ast_readaudio_callback ( struct ast_filestream s  )  [static]

Definition at line 664 of file file.c.

References ast_format_rate(), ast_fsread_audio(), ast_log(), ast_sched_add(), ast_settimeout(), ast_write(), FSREAD_FAILURE, FSREAD_SUCCESS_NOSCHED, FSREAD_SUCCESS_SCHED, LOG_WARNING, and s.

Referenced by ast_fsread_audio(), and ast_playstream().

00665 {
00666    int whennext = 0;
00667 
00668    while (!whennext) {
00669       struct ast_frame *fr;
00670       
00671       if (s->orig_chan_name && strcasecmp(s->owner->name, s->orig_chan_name))
00672          goto return_failure;
00673       
00674       fr = s->fmt->read(s, &whennext);
00675       if (!fr /* stream complete */ || ast_write(s->owner, fr) /* error writing */) {
00676          if (fr)
00677             ast_log(LOG_WARNING, "Failed to write frame\n");
00678          goto return_failure;
00679       }
00680    }
00681    if (whennext != s->lasttimeout) {
00682 #ifdef HAVE_DAHDI
00683       if (s->owner->timingfd > -1) {
00684          int zap_timer_samples = whennext;
00685          int rate;
00686          /* whennext is in samples, but DAHDI timers operate in 8 kHz samples. */
00687          if ((rate = ast_format_rate(s->fmt->format)) != 8000) {
00688             float factor;
00689             factor = ((float) rate) / ((float) 8000.0); 
00690             zap_timer_samples = (int) ( ((float) zap_timer_samples) / factor );
00691          }
00692          ast_settimeout(s->owner, zap_timer_samples, ast_fsread_audio, s);
00693       } else