Fri Jul 3 20:37:53 2009

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 "asterisk/astobj2.h"

Include dependency graph for file.c:

Go to the source code of this file.

Data Structures

struct  formats

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)
void ast_filestream_frame_freed (struct ast_frame *fr)
 destroy a filestream using an ast_frame as input
int ast_format_unregister (const char *name)
static int ast_fsread_audio (const void *data)
static int ast_fsread_video (const void *data)
struct ast_filestream * ast_openstream (struct ast_channel *chan, const char *filename, const char *preflang)
struct ast_filestream * ast_openstream_full (struct ast_channel *chan, const char *filename, const char *preflang, int asis)
struct ast_filestream * ast_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)
struct ast_filestream * ast_readfile (const char *filename, const char *type, const char *comment, int flags, int check, mode_t mode)
struct ast_frame * ast_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)
struct ast_filestream * ast_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 void filestream_destructor (void *arg)
static int fn_wrapper (struct ast_filestream *s, const char *comment, enum wrap_fn mode)
static struct ast_filestream * get_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 = 1
struct ast_cli_entry cli_file []
struct 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"


Enumeration Type Documentation

Enumerator:
ACTION_EXISTS 
ACTION_DELETE 
ACTION_RENAME 
ACTION_OPEN 
ACTION_COPY 

Definition at line 390 of file file.c.

00390                  {
00391    ACTION_EXISTS = 1, /* return matching format if file exists, 0 otherwise */
00392    ACTION_DELETE, /* delete file, return 0 on success, -1 on error */
00393    ACTION_RENAME, /* rename file. return 0 on success, -1 on error */
00394    ACTION_OPEN,
00395    ACTION_COPY /* copy file. return 0 on success, -1 on error */
00396 };

enum fsread_res

Enumerator:
FSREAD_FAILURE 
FSREAD_SUCCESS_SCHED 
FSREAD_SUCCESS_NOSCHED 

Definition at line 704 of file file.c.

00704                 {
00705    FSREAD_FAILURE,
00706    FSREAD_SUCCESS_SCHED,
00707    FSREAD_SUCCESS_NOSCHED,
00708 };

enum wrap_fn

Enumerator:
WRAP_OPEN 
WRAP_REWRITE 

Definition at line 361 of file file.c.

00361 { 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 69 of file file.c.

References ast_calloc, ast_log(), AST_RWLIST_INSERT_HEAD, AST_RWLIST_TRAVERSE, AST_RWLIST_UNLOCK, AST_RWLIST_WRLOCK, ast_verbose(), ast_format::buf_size, ast_format::exts, ast_format::list, LOG_WARNING, ast_format::module, ast_format::name, option_verbose, and VERBOSE_PREFIX_2.

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

int ast_applystream ( struct ast_channel *  chan,
struct ast_filestream *  s 
)

Applys a open stream to a channel.

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

Definition at line 812 of file file.c.

References ast_filestream::owner.

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

00813 {
00814    s->owner = chan;
00815    return 0;
00816 }

int ast_closestream ( struct ast_filestream *  f  ) 

Closes a stream

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

Definition at line 855 of file file.c.

References ao2_ref(), AST_FRFLAG_FROM_FILESTREAM, ast_test_flag, and ast_filestream::fr.

Referenced by __ast_play_and_record(), 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(), filestream_destructor(), gen_closestream(), handle_recordfile(), local_ast_moh_stop(), mixmonitor_thread(), moh_files_release(), record_exec(), recordthread(), and rpt().

00856 {
00857 
00858    if (ast_test_flag(&f->fr, AST_FRFLAG_FROM_FILESTREAM)) {
00859       /* If this flag is still set, it essentially means that the reference
00860        * count of f is non-zero. We can't destroy this filestream until
00861        * whatever is using the filestream's frame has finished.
00862        *
00863        * Since this was called, however, we need to remove the reference from
00864        * when this filestream was first allocated. That way, when the embedded
00865        * frame is freed, the refcount will reach 0 and we can finish destroying
00866        * this filestream properly.
00867        */
00868       ao2_ref(f, -1);
00869       return 0;
00870    }
00871    
00872    ao2_ref(f, -1);
00873    return 0;
00874 }

int ast_file_init ( void   ) 

Initialize file stuff

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

Definition at line 1368 of file file.c.

References ast_cli_register_multiple().

Referenced by main().

01369 {
01370    ast_cli_register_multiple(cli_file, sizeof(cli_file) / sizeof(struct ast_cli_entry));
01371    return 0;
01372 }

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

Copies a file

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 904 of file file.c.

References ACTION_COPY, and ast_filehelper().

Referenced by copy_plain_file(), and vm_forwardoptions().

00905 {
00906    return ast_filehelper(filename, filename2, fmt, ACTION_COPY);
00907 }

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

Deletes a file

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 894 of file file.c.

References ACTION_DELETE, and ast_filehelper().

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

00895 {
00896    return ast_filehelper(filename, NULL, fmt, ACTION_DELETE);
00897 }

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

Checks for the existence of a given file

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 880 of file file.c.

References ast_filestream::buf, and fileexists_core().

Referenced by announce_thread(), app_exec(), ast_moh_files_next(), ast_monitor_start(), ast_monitor_stop(), common_exec(), 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().

00881 {
00882    char *buf;
00883    int buflen;
00884 
00885    if (preflang == NULL)
00886       preflang = "";
00887    buflen = strlen(preflang) + strlen(filename) + 4;  /* room for everything */
00888    buf = alloca(buflen);
00889    if (buf == NULL)
00890       return 0;
00891    return fileexists_core(filename, fmt, preflang, buf, buflen);
00892 }

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 407 of file file.c.

References ACTION_COPY, ACTION_DELETE, ACTION_EXISTS, ACTION_OPEN, ACTION_RENAME, ast_closestream(), AST_FORMAT_MAX_AUDIO, ast_log(), AST_RWLIST_RDLOCK, AST_RWLIST_TRAVERSE, AST_RWLIST_UNLOCK, ast_strdupa, build_filename(), copy(), errno, ext, ast_format::exts, exts_compare(), f, ast_filestream::filename, ast_filestream::fmt, ast_format::format, free, get_filestream(), ast_filestream::lasttimeout, ast_format::list, LOG_WARNING, open_wrapper(), s, ast_channel::stream, ast_filestream::trans, ast_channel::vstream, and ast_channel::writeformat.

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

00408 {
00409    struct ast_format *f;
00410    int res = (action == ACTION_EXISTS) ? 0 : -1;
00411 
00412    AST_RWLIST_RDLOCK(&formats);
00413    /* Check for a specific format */
00414    AST_RWLIST_TRAVERSE(&formats, f, list) {
00415       char *stringp, *ext = NULL;
00416 
00417       if (fmt && !exts_compare(f->exts, fmt))
00418          continue;
00419 
00420       /* Look for a file matching the supported extensions.
00421        * The file must exist, and for OPEN, must match
00422        * one of the formats supported by the channel.
00423        */
00424       stringp = ast_strdupa(f->exts);  /* this is in the stack so does not need to be freed */
00425       while ( (ext = strsep(&stringp, "|")) ) {
00426          struct stat st;
00427          char *fn = build_filename(filename, ext);
00428 
00429          if (fn == NULL)
00430             continue;
00431 
00432          if ( stat(fn, &st) ) { /* file not existent */
00433             free(fn);
00434             continue;
00435          }
00436          /* for 'OPEN' we need to be sure that the format matches
00437           * what the channel can process
00438           */
00439          if (action == ACTION_OPEN) {
00440             struct ast_channel *chan = (struct ast_channel *)arg2;
00441             FILE *bfile;
00442             struct ast_filestream *s;
00443 
00444             if ( !(chan->writeformat & f->format) &&
00445                  !(f->format >= AST_FORMAT_MAX_AUDIO && fmt)) {
00446                free(fn);
00447                continue;   /* not a supported format */
00448             }
00449             if ( (bfile = fopen(fn, "r")) == NULL) {
00450                free(fn);
00451                continue;   /* cannot open file */
00452             }
00453             s = get_filestream(f, bfile);
00454             if (!s) {
00455                fclose(bfile);
00456                free(fn);   /* cannot allocate descriptor */
00457                continue;
00458             }
00459             if (open_wrapper(s)) {
00460                fclose(bfile);
00461                free(fn);
00462                free(s);
00463                continue;   /* cannot run open on file */
00464             }
00465             /* ok this is good for OPEN */
00466             res = 1; /* found */
00467             s->lasttimeout = -1;
00468             s->fmt = f;
00469             s->trans = NULL;
00470             s->filename = NULL;
00471             if (s->fmt->format < AST_FORMAT_MAX_AUDIO) {
00472                if (chan->stream)
00473                   ast_closestream(chan->stream);
00474                chan->stream = s;
00475             } else {
00476                if (chan->vstream)
00477                   ast_closestream(chan->vstream);
00478                chan->vstream = s;
00479             }
00480             free(fn);
00481             break;
00482          }
00483          switch (action) {
00484          case ACTION_OPEN:
00485             break;   /* will never get here */
00486 
00487          case ACTION_EXISTS:  /* return the matching format */
00488             res |= f->format;
00489             break;
00490 
00491          case ACTION_DELETE:
00492             if ( (res = unlink(fn)) )
00493                ast_log(LOG_WARNING, "unlink(%s) failed: %s\n", fn, strerror(errno));
00494             break;
00495 
00496          case ACTION_RENAME:
00497          case ACTION_COPY: {
00498             char *nfn = build_filename((const char *)arg2, ext);
00499             if (!nfn)
00500                ast_log(LOG_WARNING, "Out of memory\n");
00501             else {
00502                res = action == ACTION_COPY ? copy(fn, nfn) : rename(fn, nfn);
00503                if (res)
00504                   ast_log(LOG_WARNING, "%s(%s,%s) failed: %s\n",
00505                      action == ACTION_COPY ? "copy" : "rename",
00506                       fn, nfn, strerror(errno));
00507                free(nfn);
00508             }
00509              }
00510             break;
00511 
00512          default:
00513             ast_log(LOG_WARNING, "Unknown helper %d\n", action);
00514          }
00515          free(fn);
00516       }
00517    }
00518    AST_RWLIST_UNLOCK(&formats);
00519    return res;
00520 }

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

Renames a file

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 899 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().

00900 {
00901    return ast_filehelper(filename, filename2, fmt, ACTION_RENAME);
00902 }

void ast_filestream_frame_freed ( struct ast_frame *  fr  ) 

destroy a filestream using an ast_frame as input

This is a hack that is used also by the ast_trans_pvt and ast_dsp structures. When a structure contains an ast_frame pointer as one of its fields. It may be that the frame is still used after the outer structure is freed. This leads to invalid memory accesses. This function allows for us to hold off on destroying the ast_filestream until we are done using the ast_frame pointer that is part of it

Parameters:
fr The ast_frame that is part of an ast_filestream we wish to free.

Definition at line 1275 of file file.c.

References ao2_ref(), ast_clear_flag, and AST_FRFLAG_FROM_FILESTREAM.

Referenced by ast_frame_free().

01276 {
01277    struct ast_filestream *fs;
01278 
01279    ast_clear_flag(fr, AST_FRFLAG_FROM_FILESTREAM);
01280 
01281    fs = (struct ast_filestream *) (((char *) fr) - offsetof(struct ast_filestream, fr));
01282 
01283    ao2_ref(fs, -1);
01284 }

int ast_format_unregister ( const char *  name  ) 

Unregisters a file format

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 107 of file file.c.

References ast_log(), AST_RWLIST_REMOVE_CURRENT, AST_RWLIST_TRAVERSE_SAFE_BEGIN, AST_RWLIST_TRAVERSE_SAFE_END, AST_RWLIST_UNLOCK, AST_RWLIST_WRLOCK, ast_verbose(), free, ast_format::list, LOG_WARNING, ast_format::name, option_verbose, and VERBOSE_PREFIX_2.

Referenced by unload_module().

00108 {
00109    struct ast_format *tmp;
00110    int res = -1;
00111 
00112    AST_RWLIST_WRLOCK(&formats);
00113    AST_RWLIST_TRAVERSE_SAFE_BEGIN(&formats, tmp, list) {
00114       if (!strcasecmp(name, tmp->name)) {
00115          AST_RWLIST_REMOVE_CURRENT(&formats, list);
00116          free(tmp);
00117          res = 0;
00118       }
00119    }
00120    AST_RWLIST_TRAVERSE_SAFE_END
00121    AST_RWLIST_UNLOCK(&formats);
00122 
00123    if (!res) {
00124       if (option_verbose > 1)
00125          ast_verbose( VERBOSE_PREFIX_2 "Unregistered format %s\n", name);
00126    } else
00127       ast_log(LOG_WARNING, "Tried to unregister format %s, already unregistered\n", name);
00128 
00129    return res;
00130 }

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

Definition at line 759 of file file.c.

References ast_readaudio_callback(), and FSREAD_SUCCESS_SCHED.

Referenced by ast_readaudio_callback().

00760 {
00761    struct ast_filestream *fs = (struct ast_filestream *)data;
00762    enum fsread_res res;
00763 
00764    res = ast_readaudio_callback(fs);
00765 
00766    if (res == FSREAD_SUCCESS_SCHED)
00767       return 1;
00768    
00769    return 0;
00770 }

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

Definition at line 799 of file file.c.

References ast_readvideo_callback(), and FSREAD_SUCCESS_SCHED.

Referenced by ast_readvideo_callback().

00800 {
00801    struct ast_filestream *fs = (struct ast_filestream *)data;
00802    enum fsread_res res;
00803 
00804    res = ast_readvideo_callback(fs);
00805 
00806    if (res == FSREAD_SUCCESS_SCHED)
00807       return 1;
00808    
00809    return 0;
00810 }

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

Opens stream for use in seeking, playing

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 615 of file file.c.

References ast_openstream_full().

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

00616 {
00617    return ast_openstream_full(chan, filename, preflang, 0);
00618 }

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

Opens stream for use in seeking, playing

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 620 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(), ast_channel::generator, LOG_WARNING, ast_channel::oldwriteformat, ast_channel::stream, and ast_channel::writeformat.

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

00621 {
00622    /* 
00623     * Use fileexists_core() to find a file in a compatible
00624     * language and format, set up a suitable translator,
00625     * and open the stream.
00626     */
00627    int fmts, res, buflen;
00628    char *buf;
00629 
00630    if (!asis) {
00631       /* do this first, otherwise we detect the wrong writeformat */
00632       ast_stopstream(chan);
00633       if (chan->generator)
00634          ast_deactivate_generator(chan);
00635    }
00636    if (preflang == NULL)
00637       preflang = "";
00638    buflen = strlen(preflang) + strlen(filename) + 4;
00639    buf = alloca(buflen);
00640    if (buf == NULL)
00641       return NULL;
00642    fmts = fileexists_core(filename, NULL, preflang, buf, buflen);
00643    if (fmts > 0)
00644       fmts &= AST_FORMAT_AUDIO_MASK;
00645    if (fmts < 1) {
00646       ast_log(LOG_WARNING, "File %s does not exist in any format\n", filename);
00647       return NULL;
00648    }
00649    chan->oldwriteformat = chan->writeformat;
00650    /* Set the channel to a format we can work with */
00651    res = ast_set_write_format(chan, fmts);
00652    res = ast_filehelper(buf, chan, NULL, ACTION_OPEN);
00653    if (res >= 0)
00654       return chan->stream;
00655    return NULL;
00656 }

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

Opens stream for use in seeking, playing

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 658 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(), ast_filestream::fmt, format, LOG_WARNING, ast_channel::nativeformats, and ast_channel::vstream.

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

00659 {
00660    /* As above, but for video. But here we don't have translators
00661     * so we must enforce a format.
00662     */
00663    unsigned int format;
00664    char *buf;
00665    int buflen;
00666 
00667    if (preflang == NULL)
00668       preflang = "";
00669    buflen = strlen(preflang) + strlen(filename) + 4;
00670    buf = alloca(buflen);
00671    if (buf == NULL)
00672       return NULL;
00673 
00674    for (format = AST_FORMAT_MAX_AUDIO << 1; format <= AST_FORMAT_MAX_VIDEO; format = format << 1) {
00675       int fd;
00676       const char *fmt;
00677 
00678       if (!(chan->nativeformats & format))
00679          continue;
00680       fmt = ast_getformatname(format);
00681       if ( fileexists_core(filename, fmt, preflang, buf, buflen) < 1)   /* no valid format */
00682          continue;
00683       fd = ast_filehelper(buf, chan, fmt, ACTION_OPEN);
00684       if (fd >= 0)
00685          return chan->vstream;
00686       ast_log(LOG_WARNING, "File %s has video but couldn't be opened\n", filename);
00687    }
00688    return NULL;
00689 }

int ast_playstream ( struct ast_filestream *  s  ) 

play a open stream on a channel.

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

Definition at line 818 of file file.c.

References AST_FORMAT_MAX_AUDIO, ast_readaudio_callback(), ast_readvideo_callback(), ast_filestream::fmt, ast_format::format, and FSREAD_FAILURE.

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

00819 {
00820    enum fsread_res res;
00821 
00822    if (s->fmt->format < AST_FORMAT_MAX_AUDIO)
00823       res = ast_readaudio_callback(s);
00824    else
00825       res = ast_readvideo_callback(s);
00826 
00827    return (res == FSREAD_FAILURE) ? -1 : 0;
00828 }

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

Definition at line 712 of file file.c.

References ast_format_rate(), ast_fsread_audio(), ast_log(), ast_sched_add(), ast_settimeout(), ast_write(), ast_filestream::fmt, ast_format::format, FSREAD_FAILURE, FSREAD_SUCCESS_NOSCHED, FSREAD_SUCCESS_SCHED, ast_filestream::lasttimeout, LOG_WARNING, ast_channel::name, ast_filestream::orig_chan_name, ast_filestream::owner, ast_format::read, ast_channel::sched, ast_channel::streamid, and ast_channel::timingfd.

Referenced by ast_fsread_audio(), and ast_playstream().

00713 {
00714    int whennext = 0;
00715 
00716    while (!whennext) {
00717       struct ast_frame *fr;
00718       
00719       if (s->orig_chan_name && strcasecmp(s->owner->name, s->orig_chan_name))
00720          goto return_failure;
00721       
00722       fr = s->fmt->read(s, &whennext);
00723       if (!fr /* stream complete */ || ast_write(s->owner, fr) /* error writing */) {
00724          if (fr)
00725             ast_log(LOG_WARNING, "Failed to write frame\n");
00726          goto return_failure;
00727       }
00728    }
00729    if (whennext != s->lasttimeout) {
00730 #ifdef HAVE_DAHDI
00731       if (s->owner->timingfd > -1) {
00732          int zap_timer_samples = whennext;
00733          int rate;
00734          /* whennext is in samples, but DAHDI timers operate in 8 kHz samples. */
00735          if ((rate = ast_format_rate(s->fmt->format)) != 8000) {
00736             float factor;
00737             factor = ((float) rate) / ((float) 8000.0); 
00738             zap_timer_samples = (int) ( ((float) zap_timer_samples) / factor );
00739          }
00740          ast_settimeout(s->owner, zap_timer_samples, ast_fsread_audio, s);
00741       } else
00742 #endif      
00743          s->owner->streamid = ast_sched_add(s->owner->sched, 
00744             whennext / (ast_format_rate(s->fmt->format) / 1000), 
00745             ast_fsread_audio, s);
00746       s->lasttimeout = whennext;
00747       return FSREAD_SUCCESS_NOSCHED;
00748    }
00749    return FSREAD_SUCCESS_SCHED;
00750 
00751 return_failure:
00752    s->owner->streamid = -1;
00753 #ifdef HAVE_DAHDI
00754    ast_settimeout(s->owner, 0, NULL, NULL);
00755 #endif         
00756    return FSREAD_FAILURE;
00757 }

struct ast_filestream* ast_readfile ( const char *  filename,
const char *  type,
const char *  comment,
int  flags,
int  check,
mode_t  mode 
) [read]

Starts reading from a file

Parameters:
filename the name of the file to read from
type format of file you wish to read from
comment comment to go with
flags file flags
check (unimplemented, hence negligible)
mode Open mode Open an incoming file stream. flags are flags for the open() command, and if check is non-zero, then it will not read a file if there are any files that start with that name and have an extension Please note, this is a blocking function. Program execution will not return until ast_waitstream completes it's execution. Returns a struct ast_filestream on success, NULL on failure

Definition at line 940 of file file.c.

References ast_free, ast_log(), AST_RWLIST_RDLOCK, AST_RWLIST_TRAVERSE, AST_RWLIST_UNLOCK, build_filename(), errno, ast_format::exts, exts_compare(), f, ast_filestream::filename, ast_filestream::flags, ast_filestream::fmt, free, get_filestream(), LOG_WARNING, ast_filestream::mode, open_wrapper(), strdup, ast_filestream::trans, and ast_filestream::vfs.

Referenced by __ast_play_and_record(), cli_audio_convert(), and cli_audio_convert_deprecated().

00941 {
00942    FILE *bfile;
00943    struct ast_format *f;
00944    struct ast_filestream *fs = NULL;
00945    char *fn;
00946 
00947    AST_RWLIST_RDLOCK(&formats);
00948 
00949    AST_RWLIST_TRAVERSE(&formats, f, list) {
00950       fs = NULL;
00951       if (!exts_compare(f->exts, type))
00952          continue;
00953 
00954       fn = build_filename(filename, type);
00955       errno = 0;
00956       bfile = fopen(fn, "r");
00957       if (!bfile || (fs = get_filestream(f, bfile)) == NULL ||
00958           open_wrapper(fs) ) {
00959          ast_log(LOG_WARNING, "Unable to open %s\n", fn);
00960          if (fs)
00961             ast_free(fs);
00962          fs = NULL;
00963          if (bfile)
00964             fclose(bfile);
00965          free(fn);
00966          continue;
00967       }
00968       /* found it */
00969       fs->trans = NULL;
00970       fs->fmt = f;
00971       fs->flags = flags;
00972       fs->mode = mode;
00973       fs->filename = strdup(filename);
00974       fs->vfs = NULL;
00975       break;
00976    }
00977 
00978    AST_RWLIST_UNLOCK(&formats);
00979    if (!fs) 
00980       ast_log(LOG_WARNING, "No such format '%s'\n", type);
00981 
00982    return fs;
00983 }

struct ast_frame* ast_readframe ( struct ast_filestream *  s  )  [read]

Read a frame from a filestream

Parameters:
s ast_filestream to act on Returns a frame or NULL if read failed

Definition at line 691 of file file.c.

References ao2_ref(), AST_FRFLAG_FROM_FILESTREAM, ast_set_flag, f, ast_filestream::fmt, and ast_format::read.

Referenced by __ast_play_and_record(), cli_audio_convert(), cli_audio_convert_deprecated(), dictate_exec(), gen_readframe(), and moh_files_readframe().

00692 {
00693    struct ast_frame *f = NULL;
00694    int whennext = 0; 
00695    if (s && s->fmt)
00696       f = s->fmt->read(s, &whennext);
00697    if (f) {
00698       ast_set_flag(f, AST_FRFLAG_FROM_FILESTREAM);
00699       ao2_ref(s, +1);
00700    }
00701    return f;
00702 }

static enum fsread_res ast_readvideo_callback ( struct ast_filestream *  s  )  [static]

Definition at line 774 of file file.c.

References ast_format_rate(), ast_fsread_video(), ast_log(), ast_sched_add(), ast_write(), ast_filestream::fmt, ast_format::format, FSREAD_FAILURE, FSREAD_SUCCESS_NOSCHED, FSREAD_SUCCESS_SCHED, ast_filestream::lasttimeout, LOG_WARNING, ast_filestream::owner, ast_format::read, ast_channel::sched, and ast_channel::vstreamid.

Referenced by ast_fsread_video(), and ast_playstream().

00775 {
00776    int whennext = 0;
00777 
00778    while (!whennext) {
00779       struct ast_frame *fr = s->fmt->read(s, &whennext);
00780       if (!fr || ast_write(s->owner, fr)) { /* no stream or error, as above */
00781          if (fr)
00782             ast_log(LOG_WARNING, "Failed to write frame\n");
00783          s->owner->vstreamid = -1;
00784          return FSREAD_FAILURE;
00785       }
00786    }
00787 
00788    if (whennext != s->lasttimeout) {
00789       s->owner->vstreamid = ast_sched_add(s->owner->sched, 
00790          whennext / (ast_format_rate(s->fmt->format) / 1000), 
00791          ast_fsread_video, s);
00792       s->lasttimeout = whennext;
00793       return FSREAD_SUCCESS_NOSCHED;
00794    }
00795 
00796    return FSREAD_SUCCESS_SCHED;
00797 }

int ast_seekstream ( struct ast_filestream *  fs,
off_t  sample_offset,
int  whence 
)

Seeks into stream

Parameters:
fs ast_filestream to perform seek on
sample_offset numbers of samples to seek
whence SEEK_SET, SEEK_CUR, SEEK_END Returns 0 for success, or -1 for error

Definition at line 830 of file file.c.

References ast_filestream::fmt, and ast_format::seek.

Referenced by __ast_read(), ast_control_streamfile(), ast_moh_files_next(), ast_stream_fastforward(), ast_stream_rewind(), ast_write(), dictate_exec(), handle_getoption(), handle_recordfile(), and handle_streamfile().

00831 {
00832    return fs->fmt->seek(fs, sample_offset, whence);
00833 }

int ast_stopstream ( struct ast_channel *  c  ) 

Stops a stream.

Parameters:
c The channel you wish to stop playback on
Stop playback of a stream

Return values:
0 always
Note:
The channel does not need to be locked before calling this function.

Definition at line 132 of file file.c.

References ast_channel_lock, ast_channel_unlock, ast_closestream(), ast_log(), ast_set_write_format(), LOG_WARNING, ast_channel::oldwriteformat, ast_channel::stream, and ast_channel::vstream.

Referenced by ast_adsi_transmit_message_full(), ast_app_getdata(), ast_control_streamfile(), ast_openstream_full(), ast_play_and_wait(), ast_readstring_full(), ast_say_enumeration_full_da(), ast_say_enumeration_full_de(), ast_say_enumeration_full_en(), ast_say_enumeration_full_he(), ast_say_number_full_cz(), ast_say_number_full_da(), ast_say_number_full_de(), ast_say_number_full_en(), ast_say_number_full_en_GB(), ast_say_number_full_es(), ast_say_number_full_fr(), ast_say_number_full_ge(), ast_say_number_full_gr(), ast_say_number_full_he(), ast_say_number_full_it(), ast_say_number_full_nl(), ast_say_number_full_no(), ast_say_number_full_pt(), ast_say_number_full_ru(), ast_say_number_full_se(), ast_say_number_full_tw(), background_detect_exec(), builtin_blindtransfer(), conf_exec(), conf_run(), directory_exec(), handle_getoption(), handle_streamfile(), ices_exec(), ivr_dispatch(), leave_voicemail(), mp3_exec(), NBScat_exec(), nv_background_detect_exec(), parkandannounce_exec(), pbx_builtin_background(), pl_odtworz_plik(), play_file(), play_mailbox_owner(), playback_exec(), queue_exec(), read_exec(), record_exec(), recordthread(), rpt_tele_thread(), s_streamwait3(), say_character_str_full(), say_digit_str_full(), say_phonetic_str_full(), saycharstr(), sayfile(), saynum(), send_morse(), send_tone_telemetry(), send_waveform_to_channel(), speech_background(), vm_authenticate(), vm_execmain(), wait_for_winner(), waitstream_core(), and zapateller_exec().

00133 {
00134    ast_channel_lock(tmp);
00135 
00136    /* Stop a running stream if there is one */
00137    if (tmp->stream) {
00138       ast_closestream(tmp->stream);
00139       tmp->stream = NULL;
00140       if (tmp->oldwriteformat && ast_set_write_format(tmp, tmp->oldwriteformat))
00141          ast_log(LOG_WARNING, "Unable to restore format back to %d\n", tmp->oldwriteformat);
00142    }
00143    /* Stop the video stream too */
00144    if (tmp->vstream != NULL) {
00145       ast_closestream(tmp->vstream);
00146       tmp->vstream = NULL;
00147    }
00148 
00149    ast_channel_unlock(tmp);
00150 
00151    return 0;
00152 }

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 
)

Fast forward stream ms

Parameters:
fs filestream to act on
ms milliseconds to move Returns 0 for success, or -1 for error

Definition at line 845 of file file.c.

References ast_seekstream(), and DEFAULT_SAMPLES_PER_MS.

Referenced by waitstream_core().

00846 {
00847    return ast_seekstream(fs, ms * DEFAULT_SAMPLES_PER_MS, SEEK_CUR);
00848 }

int ast_stream_rewind ( struct ast_filestream *  fs,
off_t  ms 
)

Rewind stream ms

Parameters:
fs filestream to act on
ms milliseconds to move Returns 0 for success, or -1 for error

Definition at line 850 of file file.c.

References ast_seekstream(), and DEFAULT_SAMPLES_PER_MS.

Referenced by __ast_play_and_record(), handle_recordfile(), record_exec(), and waitstream_core().

00851 {
00852    return ast_seekstream(fs, -ms * DEFAULT_SAMPLES_PER_MS, SEEK_CUR);
00853 }

int ast_streamfile ( struct ast_channel *  c,
const char *  filename,
const char *  preflang 
)

Streams a file

Parameters:
c channel to stream the file to
filename the name of the file you wish to stream, minus the extension
preflang the preferred language you wish to have the file streamed to you in Prepares a channel for the streaming of a file. To start the stream, afterward do a ast_waitstream() on the channel Also, it will stop any existing streams on the channel. Returns 0 on success, or -1 on failure.

Definition at line 909 of file file.c.

References ast_applystream(), AST_FLAG_MASQ_NOSTREAM, ast_getformatname(), ast_getformatname_multiple(), ast_log(), ast_openstream(), ast_openvstream(), ast_playstream(), ast_strdup, ast_test_flag, ast_verbose(), errno, ast_filestream::fmt, ast_format::format, LOG_DEBUG, LOG_WARNING, ast_channel::name, ast_channel::nativeformats, option_verbose, ast_filestream::orig_chan_name, VERBOSE_PREFIX_3, and ast_filestream::vfs.

Referenced by __login_exec(), action_bridge(), agent_call(), announce_thread(), app_exec(), ast_app_getdata(), ast_app_getdata_full(), ast_control_streamfile(), ast_play_and_wait(), ast_say_date_da(), ast_say_date_de(), ast_say_date_en(), ast_say_date_fr(), ast_say_date_ge(), ast_say_date_gr(), ast_say_date_he(), ast_say_date_nl(), ast_say_date_with_format_gr(), ast_say_datetime_en(), ast_say_datetime_fr(), ast_say_datetime_from_now_en(), ast_say_datetime_from_now_fr(), ast_say_datetime_from_now_ge(), ast_say_datetime_from_now_he(), ast_say_datetime_gr(), ast_say_datetime_he(), ast_say_datetime_nl(), ast_say_datetime_pt(), ast_say_datetime_tw(), ast_say_enumeration_full_da(), ast_say_enumeration_full_de(), ast_say_enumeration_full_en(), ast_say_enumeration_full_he(), ast_say_number_full_cz(), ast_say_number_full_da(), ast_say_number_full_de(), ast_say_number_full_en(), ast_say_number_full_en_GB(), ast_say_number_full_es(), ast_say_number_full_fr(), ast_say_number_full_ge(), ast_say_number_full_gr(), ast_say_number_full_he(), ast_say_number_full_it(), ast_say_number_full_nl(), ast_say_number_full_no(), ast_say_number_full_pt(), ast_say_number_full_ru(), ast_say_number_full_se(), ast_say_number_full_tw(), ast_say_time_de(), ast_say_time_en(), ast_say_time_fr(), ast_say_time_ge(), ast_say_time_gr(), ast_say_time_nl(), ast_say_time_tw(), ast_stream_and_wait(), auth_exec(), background_detect_exec(), bridge_exec(), check_availability(), check_beep(), common_exec(), conf_exec(), conf_run(), do_directory(), forward_message(), gr_say_number_female(), handle_recordfile(), leave_voicemail(), nv_background_detect_exec(), page_exec(), park_exec(), parkandannounce_exec(), pbx_builtin_background(), pl_odtworz_plik(), play_and_wait(), play_file(), play_greeting(), playback_exec(), privacy_exec(), record_exec(), retrydial_exec(), rpt_tele_thread(), s_streamwait3(), say_character_str_full(), say_digit_str_full(), say_phonetic_str_full(), sayfile(), saywords(), ss_thread(), vm_authenticate(), wait_file(), and wait_for_winner().

00910 {
00911    struct ast_filestream *fs;
00912    struct ast_filestream *vfs=NULL;
00913    char fmt[256];
00914 
00915    fs = ast_openstream(chan, filename, preflang);
00916    if (fs)
00917       vfs = ast_openvstream(chan, filename, preflang);
00918    if (vfs)
00919       ast_log(LOG_DEBUG, "Ooh, found a video stream, too, format %s\n", ast_getformatname(vfs->fmt->format));
00920    if (fs){
00921       int res;
00922       if (ast_test_flag(chan, AST_FLAG_MASQ_NOSTREAM))
00923          fs->orig_chan_name = ast_strdup(chan->name);
00924       if (ast_applystream(chan, fs))
00925          return -1;
00926       if (vfs && ast_applystream(chan, vfs))
00927          return -1;
00928       res = ast_playstream(fs);
00929       if (!res && vfs)
00930          res = ast_playstream(vfs);
00931       if (option_verbose > 2)
00932          ast_verbose(VERBOSE_PREFIX_3 "<%s> Playing '%s' (language '%s')\n", chan->name, filename, preflang ? preflang : "default");
00933 
00934       return res;
00935    }
00936    ast_log(LOG_WARNING, "Unable to open %s (format %s): %s\n", filename, ast_getformatname_multiple(fmt, sizeof(fmt), chan->nativeformats), strerror(errno));
00937    return -1;
00938 }

off_t ast_tellstream ( struct ast_filestream *  fs  ) 

Tell where we are in a stream

Parameters:
fs fs to act on Returns a long as a sample offset into stream

Definition at line 840 of file file.c.

References ast_filestream::fmt, and ast_format::tell.

Referenced by __ast_play_and_record(), ast_control_streamfile(), handle_getoption(), handle_recordfile(), and handle_streamfile().

00841 {
00842    return fs->fmt->tell(fs);
00843 }

int ast_truncstream ( struct ast_filestream *  fs  ) 

Trunc stream at current location

Parameters:
fs filestream to act on Returns 0 for success, or -1 for error

Definition at line 835 of file file.c.

References ast_filestream::fmt, and ast_format::trunc.

Referenced by __ast_play_and_record(), handle_recordfile(), and record_exec().

00836 {
00837    return fs->fmt->trunc(fs);
00838 }

int ast_waitstream ( struct ast_channel *  c,
const char *  breakon 
)

Waits for a stream to stop or digit to be pressed

Parameters:
c channel to waitstream on
breakon string of DTMF digits to break upon Begins playback of a stream... Wait for a stream to stop or for any one of a given digit to arrive, Returns 0 if the stream finishes, the character if it was interrupted, and -1 on error

Definition at line 1253 of file file.c.

References waitstream_core().

Referenced by __login_exec(), action_bridge(), agent_call(), announce_thread(), app_exec(), ast_app_getdata(), ast_play_and_wait(), ast_say_date_da(), ast_say_date_de(), ast_say_date_en(), ast_say_date_fr(), ast_say_date_ge(), ast_say_date_gr(), ast_say_date_he(), ast_say_date_nl(), ast_say_date_with_format_gr(), ast_say_datetime_en(), ast_say_datetime_fr(), ast_say_datetime_from_now_en(), ast_say_datetime_from_now_fr(), ast_say_datetime_from_now_ge(), ast_say_datetime_from_now_he(), ast_say_datetime_gr(), ast_say_datetime_he(), ast_say_datetime_nl(), ast_say_datetime_pt(), ast_say_datetime_tw(), ast_say_enumeration_full_da(), ast_say_enumeration_full_de(), ast_say_enumeration_full_en(), ast_say_enumeration_full_he(), ast_say_number_full_cz(), ast_say_number_full_da(), ast_say_number_full_de(), ast_say_number_full_en(), ast_say_number_full_en_GB(), ast_say_number_full_es(), ast_say_number_full_fr(), ast_say_number_full_ge(), ast_say_number_full_gr(), ast_say_number_full_he(), ast_say_number_full_it(), ast_say_number_full_nl(), ast_say_number_full_no(), ast_say_number_full_pt(), ast_say_number_full_ru(), ast_say_number_full_se(), ast_say_number_full_tw(), ast_say_time_de(), ast_say_time_en(), ast_say_time_ge(), ast_say_time_gr(), ast_say_time_he(), ast_say_time_nl(), ast_say_time_tw(), ast_stream_and_wait(), auth_exec(), bridge_exec(), check_availability(), check_beep(), common_exec(), conf_exec(), conf_run(), directory_exec(), gr_say_number_female(), handle_recordfile(), leave_voicemail(), page_exec(), park_exec(), parkandannounce_exec(), pbx_builtin_background(), pl_odtworz_plik(), play_and_wait(), play_file(), play_greeting(), playback_exec(), privacy_exec(), record_exec(), retrydial_exec(), rpt_tele_thread(), s_streamwait3(), say_character_str_full(), say_digit_str_full(), say_phonetic_str_full(), saycharstr(), sayfile(), saynum(), saywords(), send_morse(), send_tone_telemetry(), ss_thread(), vm_authenticate(), and wait_file().

01254 {
01255    return waitstream_core(c, breakon, NULL, NULL, 0, -1, -1, NULL);
01256 }

int ast_waitstream_exten ( struct ast_channel *  c,
const char *  context 
)

Waits for a stream to stop or digit matching a valid one digit exten to be pressed

Parameters:
c channel to waitstream on
context string of context to match digits to break upon Begins playback of a stream... Wait for a stream to stop or for any one of a valid extension digit to arrive, Returns 0 if the stream finishes, the character if it was interrupted, and -1 on error

Definition at line 1264 of file file.c.

References ast_channel::context, and waitstream_core().

Referenced by pbx_builtin_background().

01265 {
01266    /* Waitstream, with return in the case of a valid 1 digit extension */
01267    /* in the current or specified context being pressed */
01268 
01269    if (!context)
01270       context = c->context;
01271    return waitstream_core(c, NULL, NULL, NULL, 0,
01272       -1, -1, context);
01273 }

int ast_waitstream_fr ( struct ast_channel *  c,
const char *  breakon,
const char *  forward,
const char *  rewind,
int  ms 
)

Same as waitstream but allows stream to be forwarded or rewound

Parameters:
c channel to waitstream on
breakon string of DTMF digits to break upon
forward DTMF digit to fast forward upon
rewind DTMF digit to rewind upon
ms How many miliseconds to skip forward/back Begins playback of a stream... Wait for a stream to stop or for any one of a given digit to arrive, Returns 0 if the stream finishes, the character if it was interrupted, and -1 on error

Definition at line 1247 of file file.c.

References waitstream_core().

Referenced by ast_control_streamfile().

01248 {
01249    return waitstream_core(c, breakon, forward, rewind, ms,
01250       -1 /* no audiofd */, -1 /* no cmdfd */, NULL /* no context */);
01251 }

int ast_waitstream_full ( struct ast_channel *  c,
const char *  breakon,
int  audiofd,
int  cmdfd 
)

struct ast_filestream* ast_writefile ( const char *  filename,
const char *  type,
const char *  comment,
int  flags,
int  check,
mode_t  mode 
) [read]

Starts writing a file

Parameters:
filename the name of the file to write to
type format of file you wish to write out to
comment comment to go with
flags output file flags
check (unimplemented, hence negligible)
mode Open mode Create an outgoing file stream. oflags are flags for the open() command, and if check is non-zero, then it will not write a file if there are any files that start with that name and have an extension Please note, this is a blocking function. Program execution will not return until ast_waitstream completes it's execution. Returns a struct ast_filestream on success, NULL on failure

Definition at line 985 of file file.c.

References ast_free, ast_log(), ast_opt_cache_record_files, AST_RWLIST_RDLOCK, AST_RWLIST_TRAVERSE, AST_RWLIST_UNLOCK, ast_strdupa, ast_filestream::buf, build_filename(), errno, ast_format::exts, exts_compare(), f, ast_filestream::filename, ast_filestream::flags, ast_filestream::fmt, free, get_filestream(), LOG_WARNING, ast_filestream::mode, ast_filestream::realfilename, record_cache_dir, rewrite_wrapper(), ast_format::seek, strdup, ast_filestream::trans, and ast_filestream::vfs.

Referenced by __ast_play_and_record(), ast_monitor_start(), ast_writestream(), cli_audio_convert(), cli_audio_convert_deprecated(), dictate_exec(), handle_recordfile(), mixmonitor_thread(), record_exec(), recordthread(), and rpt().

00986 {
00987    int fd, myflags = 0;
00988    /* compiler claims this variable can be used before initialization... */
00989    FILE *bfile = NULL;
00990    struct ast_format *f;
00991    struct ast_filestream *fs = NULL;
00992    char *buf = NULL;
00993    size_t size = 0;
00994    int format_found = 0;
00995 
00996    AST_RWLIST_RDLOCK(&formats);
00997 
00998    /* set the O_TRUNC flag if and only if there is no O_APPEND specified */
00999    /* We really can't use O_APPEND as it will break WAV header updates */
01000    if (flags & O_APPEND) { 
01001       flags &= ~O_APPEND;
01002    } else {
01003       myflags = O_TRUNC;
01004    }
01005    
01006    myflags |= O_WRONLY | O_CREAT;
01007 
01008    /* XXX need to fix this - we should just do the fopen,
01009     * not open followed by fdopen()
01010     */
01011    AST_RWLIST_TRAVERSE(&formats, f, list) {
01012       char *fn, *orig_fn = NULL;
01013       if (fs)
01014          break;
01015 
01016       if (!exts_compare(f->exts, type))
01017          continue;
01018       else
01019          format_found = 1;
01020 
01021       fn = build_filename(filename, type);
01022       fd = open(fn, flags | myflags, mode);
01023       if (fd > -1) {
01024          /* fdopen() the resulting file stream */
01025          bfile = fdopen(fd, ((flags | myflags) & O_RDWR) ? "w+" : "w");
01026          if (!bfile) {
01027             ast_log(LOG_WARNING, "Whoa, fdopen failed: %s!\n", strerror(errno));
01028             close(fd);
01029             fd = -1;
01030          }
01031       }
01032       
01033       if (ast_opt_cache_record_files && (fd > -1)) {
01034          char *c;
01035 
01036          fclose(bfile); /* this also closes fd */
01037          /*
01038            We touch orig_fn just as a place-holder so other things (like vmail) see the file is there.
01039            What we are really doing is writing to record_cache_dir until we are done then we will mv the file into place.
01040          */
01041          orig_fn = ast_strdupa(fn);
01042          for (c = fn; *c; c++)
01043             if (*c == '/')
01044                *c = '_';
01045 
01046          size = strlen(fn) + strlen(record_cache_dir) + 2;
01047          buf = alloca(size);
01048          strcpy(buf, record_cache_dir);
01049          strcat(buf, "/");
01050          strcat(buf, fn);
01051          free(fn);
01052          fn = buf;
01053          fd = open(fn, flags | myflags, mode);
01054          if (fd > -1) {
01055             /* fdopen() the resulting file stream */
01056             bfile = fdopen(fd, ((flags | myflags) & O_RDWR) ? "w+" : "w");
01057             if (!bfile) {
01058                ast_log(LOG_WARNING, "Whoa, fdopen failed: %s!\n", strerror(errno));
01059                close(fd);
01060                fd = -1;
01061             }
01062          }
01063       }
01064       if (fd > -1) {
01065          errno = 0;
01066          fs = get_filestream(f, bfile);
01067          if (!fs || rewrite_wrapper(fs, comment)) {
01068             ast_log(LOG_WARNING, "Unable to rewrite %s\n", fn);
01069             close(fd);
01070             if (orig_fn) {
01071                unlink(fn);
01072                unlink(orig_fn);
01073             }
01074             if (fs)
01075                ast_free(fs);
01076             fs = NULL;
01077             continue;
01078          }
01079          fs->trans = NULL;
01080          fs->fmt = f;
01081          fs->flags = flags;
01082          fs->mode = mode;
01083          if (orig_fn) {
01084             fs->realfilename = strdup(orig_fn);
01085             fs->filename = strdup(fn);
01086          } else {
01087             fs->realfilename = NULL;
01088             fs->filename = strdup(filename);
01089          }
01090          fs->vfs = NULL;
01091          /* If truncated, we'll be at the beginning; if not truncated, then append */
01092          f->seek(fs, 0, SEEK_END);
01093       } else if (errno != EEXIST) {
01094          ast_log(LOG_WARNING, "Unable to open file %s: %s\n", fn, strerror(errno));
01095          if (orig_fn)
01096             unlink(orig_fn);
01097       }
01098       /* if buf != NULL then fn is already free and pointing to it */
01099       if (!buf)
01100          free(fn);
01101    }
01102 
01103    AST_RWLIST_UNLOCK(&formats);
01104 
01105    if (!format_found)
01106       ast_log(LOG_WARNING, "No such format '%s'\n", type);
01107 
01108    return fs;
01109 }

int ast_writestream ( struct ast_filestream *  fs,
struct ast_frame *  f 
)

Writes a frame to a stream

Parameters:
fs filestream to write to
f frame to write to the filestream Send a frame to a filestream -- note: does NOT free the frame, call ast_frfree manually Returns 0 on success, -1 on failure.

Definition at line 154 of file file.c.

References AST_FORMAT_MAX_AUDIO, AST_FRAME_VIDEO, AST_FRAME_VOICE, ast_frfree, ast_getformatname(), ast_log(), ast_translate(), ast_translator_build_path(), ast_translator_free_path(), ast_writefile(), ast_writestream(), ast_filestream::filename, ast_filestream::flags, ast_filestream::fmt, ast_format::format, ast_frame::frametype, ast_filestream::lastwriteformat, LOG_DEBUG, LOG_WARNING, ast_filestream::mode, ast_format::name, ast_frame::subclass, ast_filestream::trans, type, ast_filestream::vfs, and ast_format::write.

Referenced by __ast_play_and_record(), __ast_read(), ast_write(), ast_writestream(), cli_audio_convert(), cli_audio_convert_deprecated(), dictate_exec(), handle_recordfile(), mixmonitor_thread(), record_exec(), recordthread(), and rpt().

00155 {
00156    int res = -1;
00157    int alt = 0;
00158    if (f->frametype == AST_FRAME_VIDEO) {
00159       if (fs->fmt->format < AST_FORMAT_MAX_AUDIO) {
00160          /* This is the audio portion.  Call the video one... */
00161          if (!fs->vfs && fs->filename) {
00162             const char *type = ast_getformatname(f->subclass & ~0x1);
00163             fs->vfs = ast_writefile(fs->filename, type, NULL, fs->flags, 0, fs->mode);
00164             ast_log(LOG_DEBUG, "Opened video output file\n");
00165          }
00166          if (fs->vfs)
00167             return ast_writestream(fs->vfs, f);
00168          /* else ignore */
00169          return 0;            
00170       } else {
00171          /* Might / might not have mark set */
00172          alt = 1;
00173       }
00174    } else if (f->frametype != AST_FRAME_VOICE) {
00175       ast_log(LOG_WARNING, "Tried to write non-voice frame\n");
00176       return -1;
00177    }
00178    if (((fs->fmt->format | alt) & f->subclass) == f->subclass) {
00179       res =  fs->fmt->write(fs, f);
00180       if (res < 0) 
00181          ast_log(LOG_WARNING, "Natural write failed\n");
00182       else if (res > 0)
00183          ast_log(LOG_WARNING, "Huh??\n");
00184    } else {
00185       /* XXX If they try to send us a type of frame that isn't the normal frame, and isn't
00186              the one we've setup a translator for, we do the "wrong thing" XXX */
00187       if (fs->trans && f->subclass != fs->lastwriteformat) {
00188          ast_translator_free_path(fs->trans);
00189          fs->trans = NULL;
00190       }
00191       if (!fs->trans) 
00192          fs->trans = ast_translator_build_path(fs->fmt->format, f->subclass);
00193       if (!fs->trans)
00194          ast_log(LOG_WARNING, "Unable to translate to format %s, source format %s\n",
00195             fs->fmt->name, ast_getformatname(f->subclass));
00196       else {
00197          struct ast_frame *trf;
00198          fs->lastwriteformat = f->subclass;
00199          /* Get the translated frame but don't consume the original in case they're using it on another stream */
00200          trf = ast_translate(fs->trans, f, 0);
00201          if (trf) {
00202             res = fs->fmt->write(fs, trf);
00203             ast_frfree(trf);
00204             if (res) 
00205                ast_log(LOG_WARNING, "Translated frame write failed\n");
00206          } else
00207             res = 0;
00208       }
00209    }
00210    return res;
00211 }

static char* build_filename ( const char *  filename,
const char *  ext 
) [static]

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.

Definition at line 256 of file file.c.

References asprintf, ast_config_AST_DATA_DIR, ast_log(), errno, and LOG_WARNING.

Referenced by ast_filehelper(), ast_readfile(), and ast_writefile().

00257 {
00258    char *fn = NULL;
00259 
00260    if (!strcmp(ext, "wav49"))
00261       ext = "WAV";
00262 
00263    if (filename[0] == '/') {
00264       if (asprintf(&fn, "%s.%s", filename, ext) < 0) {
00265          ast_log(LOG_WARNING, "asprintf() failed: %s\n", strerror(errno));
00266          fn = NULL;
00267       }
00268    } else {
00269       if (asprintf(&fn, "%s/sounds/%s.%s",
00270               ast_config_AST_DATA_DIR, filename, ext) < 0) {
00271          ast_log(LOG_WARNING, "asprintf() failed: %s\n", strerror(errno));
00272          fn = NULL;
00273       }
00274    }
00275    return fn;
00276 }

static int copy ( const char *  infile,
const char *  outfile 
) [static]

Definition at line 213 of file file.c.

References ast_log(), errno, len(), and LOG_WARNING.

Referenced by action_getvar(), ast_filehelper(), and iax2_register().

00214 {
00215    int ifd, ofd, len;
00216    char buf[4096];   /* XXX make it lerger. */
00217 
00218    if ((ifd = open(infile, O_RDONLY)) < 0) {
00219       ast_log(LOG_WARNING, "Unable to open %s in read-only mode\n", infile);
00220       return -1;
00221    }
00222    if ((ofd = open(outfile, O_WRONLY | O_TRUNC | O_CREAT, 0600)) < 0) {
00223       ast_log(LOG_WARNING, "Unable to open %s in write-only mode\n", outfile);
00224       close(ifd);
00225       return -1;
00226    }
00227    while ( (len = read(ifd, buf, sizeof(buf)) ) ) {
00228       int res;
00229       if (len < 0) {
00230          ast_log(LOG_WARNING, "Read failed on %s: %s\n", infile, strerror(errno));
00231          break;
00232       }
00233       /* XXX handle partial writes */
00234       res = write(ofd, buf, len);
00235       if (res != len) {
00236          ast_log(LOG_WARNING, "Write failed on %s (%d of %d): %s\n", outfile, res, len, strerror(errno));
00237          len = -1; /* error marker */
00238          break;
00239       }
00240    }
00241    close(ifd);
00242    close(ofd);
00243    if (len < 0) {
00244       unlink(outfile);
00245       return -1; /* error */
00246    }
00247    return 0;   /* success */
00248 }

static int exts_compare ( const char *  exts,
const char *  type 
) [static]

Definition at line 280 of file file.c.

References ast_copy_string(), and ext.

Referenced by ast_filehelper(), ast_readfile(), and ast_writefile().

00281 {
00282    char tmp[256];
00283    char *stringp = tmp, *ext;
00284 
00285    ast_copy_string(tmp, exts, sizeof(tmp));
00286    while ((ext = strsep(&stringp, "|"))) {
00287       if (!strcmp(ext, type))
00288          return 1;
00289    }
00290 
00291    return 0;
00292 }

static int fileexists_core ( const char *  filename,
const char *  fmt,
const char *  preflang,
char *  buf,
int  buflen 
) [static]

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.

The last parameter(s) point to a buffer of sufficient size, which on success is filled with the matching filename.

Definition at line 564 of file file.c.

References ast_strdupa, ast_strlen_zero(), DEFAULT_LANGUAGE, and fileexists_test().

Referenced by ast_fileexists(), ast_openstream_full(), and ast_openvstream().

00566 {
00567    int res = -1;
00568    char *lang = NULL;
00569 
00570    if (buf == NULL) {
00571       return -1;
00572    }
00573 
00574    /* We try languages in the following order:
00575     *    preflang (may include dialect)
00576     *    lang (preflang without dialect - if any)
00577     *    <none>
00578     *    default (unless the same as preflang or lang without dialect)
00579     */
00580 
00581    /* Try preferred language */
00582    if (!ast_strlen_zero(preflang)) {
00583       /* try the preflang exactly as it was requested */
00584       if ((res = fileexists_test(filename, fmt, preflang, buf, buflen)) > 0) {
00585          return res;
00586       } else {
00587          /* try without a dialect */
00588          char *postfix = NULL;
00589          postfix = lang = ast_strdupa(preflang);
00590 
00591          strsep(&postfix, "_");
00592          if (postfix) {
00593             if ((res = fileexists_test(filename, fmt, lang, buf, buflen)) > 0) {
00594                return res;
00595             }
00596          }
00597       }
00598    }
00599 
00600    /* Try without any language */
00601    if ((res = fileexists_test(filename, fmt, NULL, buf, buflen)) > 0) {
00602       return res;
00603    }
00604 
00605    /* Finally try the default language unless it was already tried before */
00606    if ((ast_strlen_zero(preflang) || strcmp(preflang, DEFAULT_LANGUAGE)) && (ast_strlen_zero(lang) || strcmp(lang, DEFAULT_LANGUAGE))) {
00607       if ((res = fileexists_test(filename, fmt, DEFAULT_LANGUAGE, buf, buflen)) > 0) {
00608          return res;
00609       }
00610    }
00611 
00612    return 0;
00613 }

static int fileexists_test ( const char *  filename,
const char *  fmt,
const char *  lang,
char *  buf,
int  buflen 
) [static]

Definition at line 527 of file file.c.

References ACTION_EXISTS, ast_filehelper(), is_absolute_path(), and offset.

Referenced by fileexists_core().

00529 {
00530    if (buf == NULL) {
00531       return -1;
00532    }
00533 
00534    if (ast_language_is_prefix && !is_absolute_path(filename)) { /* new layout */
00535       if (lang) {
00536          snprintf(buf, buflen, "%s/%s", lang, filename);
00537       } else {
00538          snprintf(buf, buflen, "%s", filename);
00539       }
00540    } else { /* old layout */
00541       strcpy(buf, filename);  /* first copy the full string */
00542       if (lang) {
00543          /* insert the language and suffix if needed */
00544          const char *c = strrchr(filename, '/');
00545          int offset = c ? c - filename + 1 : 0; /* points right after the last '/' */
00546          snprintf(buf + offset, buflen - offset, "%s/%s", lang, filename + offset);
00547       }
00548    }
00549 
00550    return ast_filehelper(buf, NULL, fmt, ACTION_EXISTS);
00551 }

static void filestream_destructor ( void *  arg  )  [static]

Definition at line 294 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, ast_filestream::f, f, ast_filestream::filename, ast_filestream::fmt, ast_format::format, free, ast_format::module, ast_filestream::orig_chan_name, ast_filestream::owner, ast_filestream::realfilename, ast_channel::sched, ast_channel::stream, ast_channel::streamid, ast_filestream::trans, ast_filestream::vfs, ast_channel::vstream, and ast_channel::vstreamid.

Referenced by get_filestream().

00295 {
00296    char *cmd = NULL;
00297    size_t size = 0;
00298    struct ast_filestream *f = arg;
00299 
00300    /* Stop a running stream if there is one */
00301    if (f->owner) {
00302       if (f->fmt->format < AST_FORMAT_MAX_AUDIO) {
00303          f->owner->stream = NULL;
00304          AST_SCHED_DEL(f->owner->sched, f->owner->streamid);
00305 #ifdef HAVE_DAHDI
00306          ast_settimeout(f->owner, 0, NULL, NULL);
00307 #endif         
00308       } else {
00309          f->owner->vstream = NULL;
00310          AST_SCHED_DEL(f->owner->sched, f->owner->vstreamid);
00311       }
00312    }
00313    /* destroy the translator on exit */
00314    if (f->trans)
00315       ast_translator_free_path(f->trans);
00316 
00317    if (f->realfilename && f->filename) {
00318          size = strlen(f->filename) + strlen(f->realfilename) + 15;
00319          cmd = alloca(size);
00320          memset(cmd,0,size);
00321          snprintf(cmd,size,"/bin/mv -f %s %s",f->filename,f->realfilename);
00322          ast_safe_system(cmd);
00323    }
00324 
00325    if (f->filename)
00326       free(f->filename);
00327    if (f->realfilename)
00328       free(f->realfilename);
00329    if (f->fmt->close)
00330       f->fmt->close(f);
00331    fclose(f->f);
00332    if (f->vfs)
00333       ast_closestream(f->vfs);
00334    if (f->orig_chan_name)
00335       free((void *) f->orig_chan_name);
00336    ast_module_unref(f->fmt->module);
00337 }

static int fn_wrapper ( struct ast_filestream *  s,
const char *  comment,
enum wrap_fn  mode 
) [static]

Definition at line 363 of file file.c.

References ast_log(), ast_module_ref(), f, ast_filestream::fmt, LOG_WARNING, ast_format::module, ast_format::name, ast_format::open, ast_format::rewrite, WRAP_OPEN, and WRAP_REWRITE.

Referenced by open_wrapper(), and rewrite_wrapper().

00364 {
00365    struct ast_format *f = s->fmt;
00366    int ret = -1;
00367 
00368    if (mode == WRAP_OPEN && f->open && f->open(s))
00369                 ast_log(LOG_WARNING, "Unable to open format %s\n", f->name);
00370    else if (mode == WRAP_REWRITE && f->rewrite && f->rewrite(s, comment))
00371                 ast_log(LOG_WARNING, "Unable to rewrite format %s\n", f->name);
00372    else {
00373       /* preliminary checks succeed. update usecount */
00374       ast_module_ref(f->module);
00375       ret = 0;
00376    }
00377         return ret;
00378 }

static struct ast_filestream* get_filestream ( struct ast_format *  fmt,
FILE *  bfile 
) [static, read]

Definition at line 339 of file file.c.

References ast_filestream::_private, ao2_alloc(), ast_filestream::buf, ast_format::buf_size, ast_format::desc_size, ast_filestream::f, filestream_destructor(), ast_filestream::fmt, ast_filestream::fr, ast_format::name, s, and ast_frame::src.

Referenced by ast_filehelper(), ast_readfile(), and ast_writefile().

00340 {
00341    struct ast_filestream *s;
00342 
00343    int l = sizeof(*s) + fmt->buf_size + fmt->desc_size;  /* total allocation size */
00344    if ( (s = ao2_alloc(l, filestream_destructor)) == NULL)
00345       return NULL;
00346    s->fmt = fmt;
00347    s->f = bfile;
00348 
00349    if (fmt->desc_size)
00350       s->_private = ((char *)(s+1)) + fmt->buf_size;
00351    if (fmt->buf_size)
00352       s->buf = (char *)(s+1);
00353    s->fr.src = fmt->name;
00354    return s;
00355 }

static int is_absolute_path ( const char *  filename  )  [static]

Definition at line 522 of file file.c.

Referenced by fileexists_test().

00523 {
00524    return filename[0] == '/';
00525 }

static int open_wrapper ( struct ast_filestream *  s  )  [static]

Definition at line 385 of file file.c.

References fn_wrapper(), and WRAP_OPEN.

Referenced by ast_filehelper(), and ast_readfile().

00386 {
00387    return fn_wrapper(s, NULL, WRAP_OPEN);
00388 }

static int rewrite_wrapper ( struct ast_filestream *  s,
const char *  comment 
) [static]

Definition at line 380 of file file.c.

References fn_wrapper(), and WRAP_REWRITE.

Referenced by ast_writefile().

00381 {
00382    return fn_wrapper(s, comment, WRAP_REWRITE);
00383 }

static int show_file_formats ( int  fd,
int  argc,
char *  argv[] 
) [static]

Definition at line 1303 of file file.c.

References ast_cli(), ast_getformatname(), AST_RWLIST_RDLOCK, AST_RWLIST_TRAVERSE, AST_RWLIST_UNLOCK, ast_format::exts, f, ast_format::format, FORMAT, FORMAT2, ast_format::list, ast_format::name, RESULT_SHOWUSAGE, and RESULT_SUCCESS.

01304 {
01305 #define FORMAT "%-10s %-10s %-20s\n"
01306 #define FORMAT2 "%-10s %-10s %-20s\n"
01307    struct ast_format *f;
01308    int count_fmt = 0;
01309 
01310    if (argc != 4)
01311       return RESULT_SHOWUSAGE;
01312    ast_cli(fd, FORMAT, "Format", "Name", "Extensions");
01313 
01314    AST_RWLIST_RDLOCK(&formats);
01315    AST_RWLIST_TRAVERSE(&formats, f, list) {
01316       ast_cli(fd, FORMAT2, ast_getformatname(f->format), f->name, f->exts);
01317       count_fmt++;
01318    }
01319    AST_RWLIST_UNLOCK(&formats);
01320    ast_cli(fd, "%d file formats registered.\n", count_fmt);
01321    return RESULT_SUCCESS;
01322 #undef FORMAT
01323 #undef FORMAT2
01324 }

static int show_file_formats_deprecated ( int  fd,
int  argc,
char *  argv[] 
) [static]

Definition at line 1326 of file file.c.

References ast_cli(), ast_getformatname(), AST_LIST_LOCK, AST_LIST_TRAVERSE, AST_LIST_UNLOCK, ast_log(), ast_format::exts, f, ast_format::format, FORMAT, FORMAT2, ast_format::list, LOG_WARNING, ast_format::name, RESULT_SHOWUSAGE, and RESULT_SUCCESS.

01327 {
01328 #define FORMAT "%-10s %-10s %-20s\n"
01329 #define FORMAT2 "%-10s %-10s %-20s\n"
01330    struct ast_format *f;
01331    int count_fmt = 0;
01332    
01333    if (argc != 3)
01334       return RESULT_SHOWUSAGE;
01335    ast_cli(fd, FORMAT, "Format", "Name", "Extensions");
01336    
01337    if (AST_LIST_LOCK(&formats)) {
01338       ast_log(LOG_WARNING, "Unable to lock format list\n");
01339       return -1;
01340    }
01341    
01342    AST_LIST_TRAVERSE(&formats, f, list) {
01343       ast_cli(fd, FORMAT2, ast_getformatname(f->format), f->name, f->exts);
01344       count_fmt++;
01345    }
01346    AST_LIST_UNLOCK(&formats);
01347    ast_cli(fd, "%d file formats registered.\n", count_fmt);
01348    return RESULT_SUCCESS;
01349 #undef FORMAT
01350 #undef FORMAT2
01351 }

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 
) [static]

the core of all waitstream() functions

Definition at line 1114 of file file.c.

References ast_channel::_softhangup, ast_clear_flag, AST_CONTROL_ANSWER, AST_CONTROL_BUSY, AST_CONTROL_CONGESTION, AST_CONTROL_HANGUP, AST_CONTROL_HOLD, AST_CONTROL_RINGING, AST_CONTROL_SRCUPDATE, AST_CONTROL_UNHOLD, AST_CONTROL_VIDUPDATE, ast_exists_extension(), AST_FLAG_END_DTMF_ONLY, AST_FLAG_MASQ_NOSTREAM, AST_FRAME_CONTROL, AST_FRAME_DTMF_END, AST_FRAME_VOICE, ast_frfree, ast_log(), ast_read(), ast_sched_runq(), ast_sched_wait(), ast_set_flag, ast_stopstream(), ast_strdupa, ast_stream_fastforward(), ast_stream_rewind(), ast_test_flag, ast_waitfor(), ast_waitfor_nandfds(), ast_channel::cid, ast_callerid::cid_num, ast_frame::data, ast_frame::datalen, errno, exten, ast_frame::frametype, LOG_WARNING, ast_channel::name, ast_filestream::orig_chan_name, ast_channel::sched, ast_channel::stream, and ast_frame::subclass.

Referenced by ast_waitstream(), ast_waitstream_exten(), ast_waitstream_fr(), and ast_waitstream_full().

01117 {
01118    const char *orig_chan_name = NULL;
01119    int err = 0;
01120 
01121    if (!breakon)
01122       breakon = "";
01123    if (!forward)
01124       forward = "";
01125    if (!rewind)
01126       rewind = "";
01127 
01128    /* Switch the channel to end DTMF frame only. waitstream_core doesn't care about the start of DTMF. */
01129    ast_set_flag(c, AST_FLAG_END_DTMF_ONLY);
01130 
01131    if (ast_test_flag(c, AST_FLAG_MASQ_NOSTREAM))
01132       orig_chan_name = ast_strdupa(c->name);
01133 
01134    while (c->stream) {
01135       int res;
01136       int ms;
01137 
01138       if (orig_chan_name && strcasecmp(orig_chan_name, c->name)) {
01139          ast_stopstream(c);
01140          err = 1;
01141          break;
01142       }
01143 
01144       ms = ast_sched_wait(c->sched);
01145 
01146       if (ms < 0 && !c->timingfunc) {
01147          ast_stopstream(c);
01148          break;
01149       }
01150       if (ms < 0)
01151          ms = 1000;
01152       if (cmdfd < 0) {
01153          res = ast_waitfor(c, ms);
01154          if (res < 0) {
01155             ast_log(LOG_WARNING, "Select failed (%s)\n", strerror(errno));
01156             ast_clear_flag(c, AST_FLAG_END_DTMF_ONLY);
01157             return res;
01158          }
01159       } else {
01160          int outfd;
01161          struct ast_channel *rchan = ast_waitfor_nandfds(&c, 1, &cmdfd, (cmdfd > -1) ? 1 : 0, NULL, &outfd, &ms);
01162          if (!rchan && (outfd < 0) && (ms)) {
01163             /* Continue */
01164             if (errno == EINTR)
01165                continue;
01166             ast_log(LOG_WARNING, "Wait failed (%s)\n", strerror(errno));
01167             ast_clear_flag(c, AST_FLAG_END_DTMF_ONLY);
01168             return -1;
01169          } else if (outfd > -1) { /* this requires cmdfd set */
01170             /* The FD we were watching has something waiting */
01171             ast_clear_flag(c, AST_FLAG_END_DTMF_ONLY);
01172             return 1;
01173          }
01174          /* if rchan is set, it is 'c' */
01175          res = rchan ? 1 : 0; /* map into 'res' values */
01176       }
01177       if (res > 0) {
01178          struct ast_frame *fr = ast_read(c);
01179          if (!fr) {
01180             ast_clear_flag(c, AST_FLAG_END_DTMF_ONLY);
01181             return -1;
01182          }
01183          switch(fr->frametype) {
01184          case AST_FRAME_DTMF_END:
01185             if (context) {
01186                const char exten[2] = { fr->subclass, '\0' };
01187                if (ast_exists_extension(c, context, exten, 1, c->cid.cid_num)) {
01188                   res = fr->subclass;
01189                   ast_frfree(fr);
01190                   ast_clear_flag(c, AST_FLAG_END_DTMF_ONLY);
01191                   return res;
01192                }
01193             } else {
01194                res = fr->subclass;
01195                if (strchr(forward,res)) {
01196                   ast_stream_fastforward(c->stream, skip_ms);
01197                } else if (strchr(rewind,res)) {
01198                   ast_stream_rewind(c->stream, skip_ms);
01199                } else if (strchr(breakon, res)) {
01200                   ast_frfree(fr);
01201                   ast_clear_flag(c, AST_FLAG_END_DTMF_ONLY);
01202                   return res;
01203                }              
01204             }
01205             break;
01206          case AST_FRAME_CONTROL:
01207             switch(fr->subclass) {
01208             case AST_CONTROL_HANGUP:
01209             case AST_CONTROL_BUSY:
01210             case AST_CONTROL_CONGESTION:
01211                ast_frfree(fr);
01212                ast_clear_flag(c, AST_FLAG_END_DTMF_ONLY);
01213                return -1;
01214             case AST_CONTROL_RINGING:
01215             case AST_CONTROL_ANSWER:
01216             case AST_CONTROL_VIDUPDATE:
01217             case AST_CONTROL_SRCUPDATE:
01218             case AST_CONTROL_HOLD:
01219             case AST_CONTROL_UNHOLD:
01220                /* Unimportant */
01221                break;
01222             default:
01223                ast_log(LOG_WARNING, "Unexpected control subclass '%d'\n", fr->subclass);
01224             }
01225             break;
01226          case AST_FRAME_VOICE:
01227             /* Write audio if appropriate */
01228             if (audiofd > -1) {
01229                if (write(audiofd, fr->data, fr->datalen) < 0) {
01230                   ast_log(LOG_WARNING, "write() failed: %s\n", strerror(errno));
01231                }
01232             }
01233          default:
01234             /* Ignore all others */
01235             break;
01236          }
01237          ast_frfree(fr);
01238       }
01239       ast_sched_runq(c->sched);
01240    }
01241 
01242    ast_clear_flag(c, AST_FLAG_END_DTMF_ONLY);
01243 
01244    return (err || c->_softhangup) ? -1 : 0;
01245 }


Variable Documentation

Definition at line 65 of file file.c.

Referenced by ast_readconfig().

Initial value:

 {
   { { "core", "show", "file", "formats" },
   show_file_formats, "Displays file formats",
   show_file_formats_usage, NULL, &cli_show_file_formats_deprecated },
}

Definition at line 1362 of file file.c.

Initial value:

 {
   { "show", "file", "formats" },
   show_file_formats_deprecated, NULL,
   NULL }

Definition at line 1357 of file file.c.

Initial value:

 
"Usage: core show file formats\n"
"       Displays currently registered file formats (if any)\n"

Definition at line 1353 of file file.c.


Generated on Fri Jul 3 20:37:54 2009 for Asterisk - the Open Source PBX by  doxygen 1.5.9