file.h File Reference

Generic File Format Support. Should be included by clients of the file handling routines. File service providers should instead include mod_format.h. More...

Go to the source code of this file.

Defines

#define AST_DIGIT_ANY   "0123456789#*ABCD"
#define AST_DIGIT_ANYNUM   "0123456789"
#define AST_DIGIT_NONE   ""
#define AST_MAX_FORMATS   10
#define AST_RESERVED_POINTERS   20
#define SEEK_FORCECUR   10

Typedefs

typedef void( ast_waitstream_fr_cb )(struct ast_channel *chan, long ms, enum ast_waitstream_fr_cb_values val)
 callback used during dtmf controlled file playback to indicate location of playback in a file after rewinding or fastfowarding a file.

Enumerations

enum  ast_waitstream_fr_cb_values { AST_WAITSTREAM_CB_REWIND = 1, AST_WAITSTREAM_CB_FASTFORWARD, AST_WAITSTREAM_CB_START }

Functions

int ast_applystream (struct ast_channel *chan, struct ast_filestream *s)
 Applys a open stream to a channel.
int ast_closestream (struct ast_filestream *f)
 Closes a stream.
int ast_file_init (void)
int ast_filecopy (const char *oldname, const char *newname, const char *fmt)
 Copies a file.
int ast_filedelete (const char *filename, const char *fmt)
 Deletes a file.
int ast_fileexists (const char *filename, const char *fmt, const char *preflang)
 Checks for the existence of a given file.
int ast_filerename (const char *oldname, const char *newname, const char *fmt)
 Renames a file.
char * ast_format_str_reduce (char *fmts)
struct ast_formatast_get_format_for_file_ext (const char *file_ext)
 Get the ast_format associated with the given file extension.
struct ast_filestreamast_openstream (struct ast_channel *chan, const char *filename, const char *preflang)
 Opens stream for use in seeking, playing.
struct ast_filestreamast_openstream_full (struct ast_channel *chan, const char *filename, const char *preflang, int asis)
 Opens stream for use in seeking, playing.
struct ast_filestreamast_openvstream (struct ast_channel *chan, const char *filename, const char *preflang)
 Opens stream for use in seeking, playing.
int ast_playstream (struct ast_filestream *s)
 Play a open stream on a channel.
int ast_ratestream (struct ast_filestream *fs)
 Return the sample rate of the stream's format.
struct ast_filestreamast_readfile (const char *filename, const char *type, const char *comment, int flags, int check, mode_t mode)
 Starts reading from a file.
struct ast_frameast_readframe (struct ast_filestream *s)
 Read a frame from a filestream.
int ast_seekstream (struct ast_filestream *fs, off_t sample_offset, int whence)
 Seeks into stream.
int ast_stopstream (struct ast_channel *c)
 Stops a stream.
int ast_stream_and_wait (struct ast_channel *chan, const char *file, const char *digits)
 stream file until digit If the file name is non-empty, try to play it.
int ast_stream_fastforward (struct ast_filestream *fs, off_t ms)
 Fast forward stream ms.
int ast_stream_rewind (struct ast_filestream *fs, off_t ms)
 Rewind stream ms.
int ast_streamfile (struct ast_channel *c, const char *filename, const char *preflang)
 Streams a file.
off_t ast_tellstream (struct ast_filestream *fs)
 Tell where we are in a stream.
int ast_truncstream (struct ast_filestream *fs)
 Trunc stream at current location.
int ast_waitstream (struct ast_channel *c, const char *breakon)
 Waits for a stream to stop or digit to be pressed.
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.
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.
int ast_waitstream_fr_w_cb (struct ast_channel *c, const char *breakon, const char *forward, const char *rewind, int ms, ast_waitstream_fr_cb cb)
 Same as waitstream_fr but allows a callback to be alerted when a user fastforwards or rewinds the file.
int ast_waitstream_full (struct ast_channel *c, const char *breakon, int audiofd, int monfd)
struct ast_filestreamast_writefile (const char *filename, const char *type, const char *comment, int flags, int check, mode_t mode)
 Starts writing a file.
int ast_writestream (struct ast_filestream *fs, struct ast_frame *f)
 Writes a frame to a stream.


Detailed Description

Generic File Format Support. Should be included by clients of the file handling routines. File service providers should instead include mod_format.h.

Definition in file file.h.


Define Documentation

#define AST_DIGIT_ANY   "0123456789#*ABCD"

#define AST_DIGIT_ANYNUM   "0123456789"

Definition at line 49 of file file.h.

Referenced by ast_sip_add_usereqphone(), and initreqprep().

#define AST_DIGIT_NONE   ""

#define AST_MAX_FORMATS   10

The maximum number of formats we expect to see in a format string

Examples:
/tmp/asterisk-trunk/trunk/main/app.c.

Definition at line 44 of file file.h.

Referenced by __ast_play_and_record(), and ast_format_str_reduce().

#define AST_RESERVED_POINTERS   20

Definition at line 376 of file file.h.

#define SEEK_FORCECUR   10


Typedef Documentation

typedef void( ast_waitstream_fr_cb)(struct ast_channel *chan, long ms, enum ast_waitstream_fr_cb_values val)

callback used during dtmf controlled file playback to indicate location of playback in a file after rewinding or fastfowarding a file.

Examples:
/tmp/asterisk-trunk/trunk/main/app.c.

Definition at line 65 of file file.h.


Enumeration Type Documentation

The type of event associated with a ast_waitstream_fr_cb invocation

Enumerator:
AST_WAITSTREAM_CB_REWIND 
AST_WAITSTREAM_CB_FASTFORWARD 
AST_WAITSTREAM_CB_START 

Definition at line 54 of file file.h.


Function Documentation

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
Return values:
0 on success.
-1 on failure.

Definition at line 991 of file file.c.

References ast_filestream::owner.

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

00992 {
00993    s->owner = chan;
00994    return 0;
00995 }

int ast_closestream ( struct ast_filestream f  ) 

Closes a stream.

Parameters:
f filestream to close Close a playback or recording stream
Return values:
0 on success.
-1 on failure.
Examples:
/tmp/asterisk-trunk/trunk/main/app.c.

Definition at line 1039 of file file.c.

References ao2_ref, filestream_close(), and NULL.

Referenced by __ast_play_and_record(), ast_hangup(), ast_moh_files_next(), ast_monitor_start(), ast_monitor_stop(), ast_readfile(), ast_stopstream(), ast_writefile(), dictate_exec(), filehelper(), filestream_destructor(), gen_closestream(), handle_cli_file_convert(), handle_recordfile(), local_ast_moh_stop(), mixmonitor_ds_close_fs(), moh_files_release(), msg_create_from_file(), record_exec(), and recordthread().

01040 {
01041    /* This used to destroy the filestream, but it now just decrements a refcount.
01042     * We close the stream in order to quit queuing frames now, because we might
01043     * change the writeformat, which could result in a subsequent write error, if
01044     * the format is different. */
01045    if (f == NULL) {
01046       return 0;
01047    }
01048    filestream_close(f);
01049    ao2_ref(f, -1);
01050    return 0;
01051 }

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

Provided by file.c

Definition at line 1778 of file file.c.

References ARRAY_LEN, ast_cli_register_multiple(), ast_format_register_type(), ast_format_unregister_type(), ast_register_cleanup(), file_shutdown(), and STASIS_MESSAGE_TYPE_INIT.

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

References ACTION_COPY, and filehelper().

Referenced by copy_plain_file(), msg_create_from_file(), stasis_app_stored_recording_copy(), and vm_forwardoptions().

01080 {
01081    return filehelper(filename, filename2, fmt, ACTION_COPY);
01082 }

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
Examples:
/tmp/asterisk-trunk/trunk/main/app.c.

Definition at line 1069 of file file.c.

References ACTION_DELETE, filehelper(), and NULL.

Referenced by __ast_play_and_record(), announce_thread(), ast_monitor_start(), ast_monitor_stop(), conf_free(), conf_run(), confbridge_exec(), dial_exec_full(), handle_cli_file_convert(), leave_voicemail(), msg_create_from_file(), play_record_review(), record_exec(), recording_cancel(), setup_privacy_args(), and vm_delete().

01070 {
01071    return filehelper(filename, NULL, fmt, ACTION_DELETE);
01072 }

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.
Return values:
0,false. The file does not exist
1,true. The file does exist.

Definition at line 1057 of file file.c.

References ast_alloca, buf, fileexists_core(), and NULL.

Referenced by announce_thread(), app_exec(), ast_moh_files_next(), ast_monitor_start(), ast_monitor_stop(), common_exec(), conf_run(), dial_exec_full(), eivr_comm(), forward_message(), get_folder(), invent_message(), leave_voicemail(), meetme_menu_admin_extended(), minivm_delete_exec(), msg_create_from_file(), play_file(), play_message(), play_message_by_id_helper(), play_message_callerid(), readexten_exec(), record_exec(), retrydial_exec(), say_character_str_full(), say_digit_str_full(), say_phonetic_str_full(), sayname(), setup_privacy_args(), sound_file_exists(), stasis_app_control_record(), vm_intro(), vm_msg_play(), vm_newuser(), vm_options(), and vm_tempgreeting().

01058 {
01059    char *buf;
01060    int buflen;
01061 
01062    if (preflang == NULL)
01063       preflang = "";
01064    buflen = strlen(preflang) + strlen(filename) + 4;  /* room for everything */
01065    buf = ast_alloca(buflen);
01066    return fileexists_core(filename, fmt, preflang, buf, buflen, NULL) ? 1 : 0;
01067 }

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
Examples:
/tmp/asterisk-trunk/trunk/main/app.c.

Definition at line 1074 of file file.c.

References ACTION_RENAME, and filehelper().

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

01075 {
01076    return filehelper(filename, filename2, fmt, ACTION_RENAME);
01077 }

char* ast_format_str_reduce ( char *  fmts  ) 

Remove duplicate formats from a format string.

Parameters:
fmts a format string, this string will be modified
Return values:
NULL error
Returns:
a pointer to the reduced format string, this is a pointer to fmts

Definition at line 1642 of file file.c.

References ast_log, AST_MAX_FORMATS, AST_RWLIST_RDLOCK, AST_RWLIST_TRAVERSE, AST_RWLIST_UNLOCK, ast_strdupa, ast_format_def::exts, exts_compare(), f, first, len(), ast_format_def::list, LOG_WARNING, NULL, strsep(), and type.

Referenced by actual_load_config(), and AST_TEST_DEFINE().

01643 {
01644    struct ast_format_def *f;
01645    struct ast_format_def *fmts_ptr[AST_MAX_FORMATS];
01646    char *fmts_str[AST_MAX_FORMATS];
01647    char *stringp, *type;
01648    char *orig = fmts;
01649    int i, j, x, first, found = 0;
01650    int len = strlen(fmts) + 1;
01651    int res;
01652 
01653    if (AST_RWLIST_RDLOCK(&formats)) {
01654       ast_log(LOG_WARNING, "Unable to lock format list\n");
01655       return NULL;
01656    }
01657 
01658    stringp = ast_strdupa(fmts);
01659 
01660    for (x = 0; (type = strsep(&stringp, "|")) && x < AST_MAX_FORMATS; x++) {
01661       AST_RWLIST_TRAVERSE(&formats, f, list) {
01662          if (exts_compare(f->exts, type)) {
01663             found = 1;
01664             break;
01665          }
01666       }
01667 
01668       fmts_str[x] = type;
01669       if (found) {
01670          fmts_ptr[x] = f;
01671       } else {
01672          fmts_ptr[x] = NULL;
01673       }
01674    }
01675    AST_RWLIST_UNLOCK(&formats);
01676 
01677    first = 1;
01678    for (i = 0; i < x; i++) {
01679       /* ignore invalid entries */
01680       if (!fmts_ptr[i]) {
01681          ast_log(LOG_WARNING, "ignoring unknown format '%s'\n", fmts_str[i]);
01682          continue;
01683       }
01684 
01685       /* special handling for the first entry */
01686       if (first) {
01687          res = snprintf(fmts, len, "%s", fmts_str[i]);
01688          fmts += res;
01689          len -= res;
01690          first = 0;
01691          continue;
01692       }
01693 
01694       found = 0;
01695       for (j = 0; j < i; j++) {
01696          /* this is a duplicate */
01697          if (fmts_ptr[j] == fmts_ptr[i]) {
01698             found = 1;
01699             break;
01700          }
01701       }
01702 
01703       if (!found) {
01704          res = snprintf(fmts, len, "|%s", fmts_str[i]);
01705          fmts += res;
01706          len -= res;
01707       }
01708    }
01709 
01710    if (first) {
01711       ast_log(LOG_WARNING, "no known formats found in format list (%s)\n", orig);
01712       return NULL;
01713    }
01714 
01715    return orig;
01716 }

struct ast_format* ast_get_format_for_file_ext ( const char *  file_ext  )  [read]

Get the ast_format associated with the given file extension.

Since:
12
Parameters:
file_ext The file extension for which to find the format
Return values:
NULL if not found
A pointer to the ast_format associated with this file extension

Definition at line 1754 of file file.c.

References AST_RWLIST_TRAVERSE, ast_format_def::exts, exts_compare(), f, ast_format_def::format, ast_format_def::list, lock, NULL, and SCOPED_RDLOCK.

Referenced by ast_ari_bridges_record(), ast_ari_channels_record(), find_recording(), process_media_file(), and scan_file().

01755 {
01756    struct ast_format_def *f;
01757    SCOPED_RDLOCK(lock, &formats.lock);
01758    AST_RWLIST_TRAVERSE(&formats, f, list) {
01759       if (exts_compare(f->exts, file_ext)) {
01760          return f->format;
01761       }
01762    }
01763 
01764    return NULL;
01765 }

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
Return values:
a ast_filestream pointer if it opens the file.
NULL on error.

Definition at line 735 of file file.c.

References ast_openstream_full().

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

00736 {
00737    return ast_openstream_full(chan, filename, preflang, 0);
00738 }

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
Return values:
a ast_filestream pointer if it opens the file.
NULL on error.

Definition at line 740 of file file.c.

References ACTION_OPEN, ao2_ref, ast_alloca, ast_channel_generator(), ast_channel_lock, ast_channel_set_oldwriteformat(), ast_channel_stream(), ast_channel_unlock, ast_channel_writeformat(), ast_deactivate_generator(), ast_format_cap_alloc, AST_FORMAT_CAP_FLAG_DEFAULT, ast_format_cap_has_type(), ast_log, AST_MEDIA_TYPE_AUDIO, ast_set_write_format_from_cap(), ast_stopstream(), buf, fileexists_core(), filehelper(), LOG_WARNING, and NULL.

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

00741 {
00742    /*
00743     * Use fileexists_core() to find a file in a compatible
00744     * language and format, set up a suitable translator,
00745     * and open the stream.
00746     */
00747    struct ast_format_cap *file_fmt_cap;
00748    int res;
00749    int buflen;
00750    char *buf;
00751 
00752    if (!asis) {
00753       /* do this first, otherwise we detect the wrong writeformat */
00754       ast_stopstream(chan);
00755       if (ast_channel_generator(chan))
00756          ast_deactivate_generator(chan);
00757    }
00758    if (preflang == NULL)
00759       preflang = "";
00760    buflen = strlen(preflang) + strlen(filename) + 4;
00761    buf = ast_alloca(buflen);
00762 
00763    if (!(file_fmt_cap = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT))) {
00764       return NULL;
00765    }
00766    if (!fileexists_core(filename, NULL, preflang, buf, buflen, file_fmt_cap) ||
00767       !ast_format_cap_has_type(file_fmt_cap, AST_MEDIA_TYPE_AUDIO)) {
00768 
00769       ast_log(LOG_WARNING, "File %s does not exist in any format\n", filename);
00770       ao2_ref(file_fmt_cap, -1);
00771       return NULL;
00772    }
00773 
00774    /* Set the channel to a format we can work with and save off the previous format. */
00775    ast_channel_lock(chan);
00776    ast_channel_set_oldwriteformat(chan, ast_channel_writeformat(chan));
00777    /* Set the channel to the best format that exists for the file. */
00778    res = ast_set_write_format_from_cap(chan, file_fmt_cap);
00779    ast_channel_unlock(chan);
00780    /* don't need this anymore now that the channel's write format is set. */
00781    ao2_ref(file_fmt_cap, -1);
00782 
00783    if (res == -1) {  /* No format available that works with this channel */
00784       return NULL;
00785    }
00786    res = filehelper(buf, chan, NULL, ACTION_OPEN);
00787    if (res >= 0)
00788       return ast_channel_stream(chan);
00789    return NULL;
00790 }

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
Return values:
a ast_filestream pointer if it opens the file.
NULL on error.

Definition at line 792 of file file.c.

References ACTION_OPEN, ao2_ref, ast_alloca, ast_channel_nativeformats(), ast_channel_vstream(), ast_format_cap_alloc, ast_format_cap_count(), AST_FORMAT_CAP_FLAG_DEFAULT, ast_format_cap_get_format(), ast_format_cap_has_type(), ast_format_cap_iscompatible(), ast_format_get_name(), ast_format_get_type(), ast_log, AST_MEDIA_TYPE_VIDEO, buf, fileexists_core(), filehelper(), format, LOG_WARNING, and NULL.

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

00793 {
00794    /* As above, but for video. But here we don't have translators
00795     * so we must enforce a format.
00796     */
00797    struct ast_format_cap *tmp_cap;
00798    char *buf;
00799    int buflen;
00800    int i, fd;
00801 
00802    if (preflang == NULL) {
00803       preflang = "";
00804    }
00805    buflen = strlen(preflang) + strlen(filename) + 4;
00806    buf = ast_alloca(buflen);
00807 
00808    /* is the channel capable of video without translation ?*/
00809    if (!ast_format_cap_has_type(ast_channel_nativeformats(chan), AST_MEDIA_TYPE_VIDEO)) {
00810       return NULL;
00811    }
00812    if (!(tmp_cap = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT))) {
00813       return NULL;
00814    }
00815    /* Video is supported, so see what video formats exist for this file */
00816    if (!fileexists_core(filename, NULL, preflang, buf, buflen, tmp_cap)) {
00817       ao2_ref(tmp_cap, -1);
00818       return NULL;
00819    }
00820 
00821    /* iterate over file formats and pick the first one compatible with the channel's native formats */
00822    for (i = 0; i < ast_format_cap_count(tmp_cap); ++i) {
00823       struct ast_format *format = ast_format_cap_get_format(tmp_cap, i);
00824 
00825       if ((ast_format_get_type(format) != AST_MEDIA_TYPE_VIDEO) ||
00826          !ast_format_cap_iscompatible(ast_channel_nativeformats(chan), tmp_cap)) {
00827          ao2_ref(format, -1);
00828          continue;
00829       }
00830 
00831       fd = filehelper(buf, chan, ast_format_get_name(format), ACTION_OPEN);
00832       if (fd >= 0) {
00833          ao2_ref(format, -1);
00834          ao2_ref(tmp_cap, -1);
00835          return ast_channel_vstream(chan);
00836       }
00837       ast_log(LOG_WARNING, "File %s has video but couldn't be opened\n", filename);
00838       ao2_ref(format, -1);
00839    }
00840    ao2_ref(tmp_cap, -1);
00841 
00842    return NULL;
00843 }

int ast_playstream ( struct ast_filestream s  ) 

Play a open stream on a channel.

Parameters:
s filestream to play
Return values:
0 on success.
-1 on failure.

Definition at line 997 of file file.c.

References ast_format_get_type(), AST_MEDIA_TYPE_AUDIO, ast_readaudio_callback(), ast_readvideo_callback(), ast_filestream::fmt, ast_format_def::format, and FSREAD_FAILURE.

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

00998 {
00999    enum fsread_res res;
01000 
01001    if (ast_format_get_type(s->fmt->format) == AST_MEDIA_TYPE_AUDIO)
01002       res = ast_readaudio_callback(s);
01003    else
01004       res = ast_readvideo_callback(s);
01005 
01006    return (res == FSREAD_FAILURE) ? -1 : 0;
01007 }

int ast_ratestream ( struct ast_filestream fs  ) 

Return the sample rate of the stream's format.

Parameters:
fs fs to act on
Returns:
sample rate in Hz

Definition at line 1024 of file file.c.

References ast_format_get_sample_rate(), ast_filestream::fmt, and ast_format_def::format.

Referenced by msg_create_from_file().

01025 {
01026    return ast_format_get_sample_rate(fs->fmt->format);
01027 }

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.
Return values:
a struct ast_filestream on success.
NULL on failure.
Examples:
/tmp/asterisk-trunk/trunk/main/app.c.

Definition at line 1136 of file file.c.

References ast_closestream(), ast_free, ast_log, AST_RWLIST_RDLOCK, AST_RWLIST_TRAVERSE, AST_RWLIST_UNLOCK, ast_strdup, build_filename(), errno, ast_format_def::exts, exts_compare(), f, ast_filestream::filename, ast_filestream::flags, ast_filestream::fmt, get_filestream(), LOG_WARNING, ast_filestream::mode, NULL, open_wrapper(), ast_filestream::trans, and ast_filestream::vfs.

Referenced by __ast_play_and_record(), handle_cli_file_convert(), and msg_create_from_file().

01137 {
01138    FILE *bfile;
01139    struct ast_format_def *f;
01140    struct ast_filestream *fs = NULL;
01141    char *fn;
01142    int format_found = 0;
01143 
01144    AST_RWLIST_RDLOCK(&formats);
01145 
01146    AST_RWLIST_TRAVERSE(&formats, f, list) {
01147       fs = NULL;
01148       if (!exts_compare(f->exts, type))
01149          continue;
01150       else
01151          format_found = 1;
01152 
01153       fn = build_filename(filename, type);
01154       if (!fn) {
01155          continue;
01156       }
01157       errno = 0;
01158       bfile = fopen(fn, "r");
01159 
01160       if (!bfile || (fs = get_filestream(f, bfile)) == NULL || open_wrapper(fs) ) {
01161          ast_log(LOG_WARNING, "Unable to open %s\n", fn);
01162          if (fs) {
01163             ast_closestream(fs);
01164          }
01165          fs = NULL;
01166          bfile = NULL;
01167          ast_free(fn);
01168          break;
01169       }
01170       /* found it */
01171       fs->trans = NULL;
01172       fs->fmt = f;
01173       fs->flags = flags;
01174       fs->mode = mode;
01175       fs->filename = ast_strdup(filename);
01176       fs->vfs = NULL;
01177       ast_free(fn);
01178       break;
01179    }
01180 
01181    AST_RWLIST_UNLOCK(&formats);
01182    if (!format_found)
01183       ast_log(LOG_WARNING, "No such format '%s'\n", type);
01184 
01185    return fs;
01186 }

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.
Return values:
NULL if read failed.
Examples:
/tmp/asterisk-trunk/trunk/main/app.c.

Definition at line 870 of file file.c.

References read_frame().

Referenced by __ast_play_and_record(), dictate_exec(), gen_readframe(), handle_cli_file_convert(), and moh_files_readframe().

00871 {
00872    int whennext = 0;
00873 
00874    return read_frame(s, &whennext);
00875 }

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
Return values:
0 on success.
-1 on failure.
Examples:
/tmp/asterisk-trunk/trunk/main/app.c.

Definition at line 1009 of file file.c.

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

Referenced by __ast_read(), ast_moh_files_next(), ast_stream_fastforward(), ast_stream_rewind(), ast_streamfile(), ast_write(), control_streamfile(), dictate_exec(), handle_getoption(), handle_recordfile(), handle_streamfile(), msg_create_from_file(), and speech_streamfile().

01010 {
01011    return fs->fmt->seek(fs, sample_offset, whence);
01012 }

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.
Examples:
/tmp/asterisk-trunk/trunk/main/app.c.

Definition at line 188 of file file.c.

References ast_channel_lock, ast_channel_oldwriteformat(), ast_channel_stream(), ast_channel_stream_set(), ast_channel_unlock, ast_channel_vstream(), ast_channel_vstream_set(), ast_closestream(), ast_format_get_name(), ast_log, ast_set_write_format(), LOG_WARNING, and NULL.

Referenced by action_playback_and_continue(), adsi_transmit_message_full(), agent_alert(), announce_to_dial(), 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_enumeration_full_vi(), ast_say_number_full_cs(), 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_gr(), ast_say_number_full_he(), ast_say_number_full_hu(), ast_say_number_full_it(), ast_say_number_full_ja(), ast_say_number_full_ka(), 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_th(), ast_say_number_full_ur(), ast_say_number_full_vi(), ast_say_number_full_zh(), background_detect_exec(), conf_exec(), conf_run(), control_streamfile(), dial_exec_full(), directory_exec(), grab_transfer(), handle_getoption(), handle_speechrecognize(), handle_streamfile(), ices_exec(), ivr_dispatch(), leave_voicemail(), meetme_menu_admin(), meetme_menu_admin_extended(), minivm_greet_exec(), mp3_exec(), NBScat_exec(), pbx_builtin_background(), pl_odtworz_plik(), play_file(), play_files_helper(), play_mailbox_owner(), playback_exec(), queue_exec(), read_exec(), readexten_exec(), record_exec(), recordthread(), s_streamwait3(), say_character_str_full(), say_digit_str_full(), say_phonetic_str_full(), select_item_seq(), send_waveform_to_channel(), speech_background(), vm_authenticate(), vm_execmain(), wait_for_winner(), waitstream_core(), and zapateller_exec().

00189 {
00190    ast_channel_lock(tmp);
00191 
00192    /* Stop a running stream if there is one */
00193    if (ast_channel_stream(tmp)) {
00194       ast_closestream(ast_channel_stream(tmp));
00195       ast_channel_stream_set(tmp, NULL);
00196       if (ast_channel_oldwriteformat(tmp) && ast_set_write_format(tmp, ast_channel_oldwriteformat(tmp)))
00197          ast_log(LOG_WARNING, "Unable to restore format back to %s\n", ast_format_get_name(ast_channel_oldwriteformat(tmp)));
00198    }
00199    /* Stop the video stream too */
00200    if (ast_channel_vstream(tmp) != NULL) {
00201       ast_closestream(ast_channel_vstream(tmp));
00202       ast_channel_vstream_set(tmp, NULL);
00203    }
00204 
00205    ast_channel_unlock(tmp);
00206 
00207    return 0;
00208 }

int ast_stream_and_wait ( struct ast_channel chan,
const char *  file,
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
Return values:
0 on success.
-1 on failure.

Definition at line 1029 of file file.c.

References ast_seekstream(), and DEFAULT_SAMPLES_PER_MS.

Referenced by waitstream_control().

01030 {
01031    return ast_seekstream(fs, ms * DEFAULT_SAMPLES_PER_MS, SEEK_CUR);
01032 }

int ast_stream_rewind ( struct ast_filestream fs,
off_t  ms 
)

Rewind stream ms.

Parameters:
fs filestream to act on
ms milliseconds to move
Return values:
0 on success.
-1 on failure.
Examples:
/tmp/asterisk-trunk/trunk/main/app.c.

Definition at line 1034 of file file.c.

References ast_seekstream(), and DEFAULT_SAMPLES_PER_MS.

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

01035 {
01036    return ast_seekstream(fs, -ms * DEFAULT_SAMPLES_PER_MS, SEEK_CUR);
01037 }

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.
Return values:
0 on success.
-1 on failure.
Examples:
/tmp/asterisk-trunk/trunk/main/app.c.

Definition at line 1084 of file file.c.

References ast_applystream(), ast_channel_flags(), ast_channel_name(), ast_channel_nativeformats(), ast_channel_writeformat(), ast_debug, AST_FLAG_MASQ_NOSTREAM, ast_format_cap_get_names(), ast_format_get_name(), ast_log, ast_openstream(), ast_openvstream(), ast_playstream(), ast_seekstream(), ast_str_alloca, ast_strdup, ast_test_flag, ast_test_suite_event_notify, ast_verb, errno, ast_filestream::f, ast_filestream::fmt, ast_format_def::format, LOG_WARNING, NULL, ast_filestream::orig_chan_name, and ast_filestream::vfs.

Referenced by __analog_ss_thread(), action_playback_and_continue(), analog_ss_thread(), announce_thread(), announce_to_dial(), app_exec(), ast_app_getdata(), ast_app_getdata_full(), ast_play_and_wait(), ast_say_date_da(), ast_say_date_de(), ast_say_date_en(), ast_say_date_fr(), ast_say_date_gr(), ast_say_date_he(), ast_say_date_hu(), ast_say_date_ja(), ast_say_date_ka(), ast_say_date_nl(), ast_say_date_th(), 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_he(), ast_say_datetime_from_now_ka(), ast_say_datetime_gr(), ast_say_datetime_he(), ast_say_datetime_ja(), ast_say_datetime_nl(), ast_say_datetime_pt(), ast_say_datetime_th(), ast_say_datetime_zh(), ast_say_enumeration_full_da(), ast_say_enumeration_full_de(), ast_say_enumeration_full_en(), ast_say_enumeration_full_he(), ast_say_enumeration_full_vi(), ast_say_number_full_cs(), 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_gr(), ast_say_number_full_he(), ast_say_number_full_hu(), ast_say_number_full_it(), ast_say_number_full_ja(), ast_say_number_full_ka(), 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_th(), ast_say_number_full_ur(), ast_say_number_full_vi(), ast_say_number_full_zh(), ast_say_time_de(), ast_say_time_en(), ast_say_time_fr(), ast_say_time_gr(), ast_say_time_hu(), ast_say_time_ja(), ast_say_time_ka(), ast_say_time_nl(), ast_say_time_zh(), ast_stream_and_wait(), auth_exec(), background_detect_exec(), common_exec(), conf_exec(), conf_get_pin(), conf_run(), control_streamfile(), dial_exec_full(), do_directory(), find_conf_realtime(), forward_message(), gr_say_number_female(), handle_recordfile(), invent_message(), leave_voicemail(), meetme_menu_admin(), meetme_menu_admin_extended(), meetme_menu_normal(), minivm_greet_exec(), page_exec(), pbx_builtin_background(), pl_odtworz_plik(), play_and_wait(), play_file(), play_record_review(), playback_exec(), privacy_exec(), readexten_exec(), record_exec(), retrydial_exec(), s_streamwait3(), say_character_str_full(), say_digit_str_full(), say_phonetic_str_full(), select_item_menu(), setup_privacy_args(), vm_authenticate(), wait_file(), and wait_for_winner().

01085 {
01086    struct ast_filestream *fs;
01087    struct ast_filestream *vfs=NULL;
01088    off_t pos;
01089    int seekattempt;
01090    int res;
01091 
01092    fs = ast_openstream(chan, filename, preflang);
01093    if (!fs) {
01094       struct ast_str *codec_buf = ast_str_alloca(64);
01095       ast_log(LOG_WARNING, "Unable to open %s (format %s): %s\n",
01096          filename, ast_format_cap_get_names(ast_channel_nativeformats(chan), &codec_buf), strerror(errno));
01097       return -1;
01098    }
01099 
01100    /* check to see if there is any data present (not a zero length file),
01101     * done this way because there is no where for ast_openstream_full to
01102     * return the file had no data. */
01103    pos = ftello(fs->f);
01104    seekattempt = fseeko(fs->f, -1, SEEK_END);
01105    if (seekattempt) {
01106       if (errno == EINVAL) {
01107          /* Zero-length file, as opposed to a pipe */
01108          return 0;
01109       } else {
01110          ast_seekstream(fs, 0, SEEK_SET);
01111       }
01112    } else {
01113       fseeko(fs->f, pos, SEEK_SET);
01114    }
01115 
01116    vfs = ast_openvstream(chan, filename, preflang);
01117    if (vfs) {
01118       ast_debug(1, "Ooh, found a video stream, too, format %s\n", ast_format_get_name(vfs->fmt->format));
01119    }
01120 
01121    if (ast_test_flag(ast_channel_flags(chan), AST_FLAG_MASQ_NOSTREAM))
01122       fs->orig_chan_name = ast_strdup(ast_channel_name(chan));
01123    if (ast_applystream(chan, fs))
01124       return -1;
01125    if (vfs && ast_applystream(chan, vfs))
01126       return -1;
01127    ast_test_suite_event_notify("PLAYBACK", "Message: %s\r\nChannel: %s", filename, ast_channel_name(chan));
01128    res = ast_playstream(fs);
01129    if (!res && vfs)
01130       res = ast_playstream(vfs);
01131    ast_verb(3, "<%s> Playing '%s.%s' (language '%s')\n", ast_channel_name(chan), filename, ast_format_get_name(ast_channel_writeformat(chan)), preflang ? preflang : "default");
01132 
01133    return res;
01134 }

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
Examples:
/tmp/asterisk-trunk/trunk/main/app.c.

Definition at line 1019 of file file.c.

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

Referenced by __ast_play_and_record(), ast_moh_files_next(), control_streamfile(), handle_getoption(), handle_recordfile(), handle_speechrecognize(), handle_streamfile(), msg_create_from_file(), waitstream_control(), and waitstream_core().

01020 {
01021    return fs->fmt->tell(fs);
01022 }

int ast_truncstream ( struct ast_filestream fs  ) 

Trunc stream at current location.

Parameters:
fs filestream to act on
Return values:
0 on success.
-1 on failure.
Examples:
/tmp/asterisk-trunk/trunk/main/app.c.

Definition at line 1014 of file file.c.

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

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

01015 {
01016    return fs->fmt->trunc(fs);
01017 }

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,
Return values:
0 if the stream finishes
the character if it was interrupted by the channel.
-1 on error
Examples:
/tmp/asterisk-trunk/trunk/main/app.c.

Definition at line 1592 of file file.c.

References NULL, sanitize_waitstream_return(), and waitstream_core().

Referenced by __analog_ss_thread(), action_playback_and_continue(), analog_ss_thread(), announce_thread(), announce_to_dial(), app_exec(), ast_play_and_wait(), ast_say_date_da(), ast_say_date_de(), ast_say_date_en(), ast_say_date_fr(), ast_say_date_gr(), ast_say_date_he(), ast_say_date_hu(), ast_say_date_ja(), ast_say_date_ka(), ast_say_date_nl(), ast_say_date_th(), 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_he(), ast_say_datetime_from_now_ka(), ast_say_datetime_gr(), ast_say_datetime_he(), ast_say_datetime_ja(), ast_say_datetime_nl(), ast_say_datetime_pt(), ast_say_datetime_th(), ast_say_datetime_zh(), ast_say_enumeration_full_da(), ast_say_enumeration_full_de(), ast_say_enumeration_full_en(), ast_say_enumeration_full_he(), ast_say_enumeration_full_vi(), ast_say_number_full_cs(), 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_gr(), ast_say_number_full_he(), ast_say_number_full_hu(), ast_say_number_full_it(), ast_say_number_full_ja(), ast_say_number_full_ka(), 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_th(), ast_say_number_full_ur(), ast_say_number_full_vi(), ast_say_number_full_zh(), ast_say_time_de(), ast_say_time_en(), ast_say_time_gr(), ast_say_time_he(), ast_say_time_hu(), ast_say_time_ja(), ast_say_time_ka(), ast_say_time_nl(), ast_say_time_zh(), ast_stream_and_wait(), auth_exec(), common_exec(), conf_exec(), conf_get_pin(), conf_run(), directory_exec(), find_conf_realtime(), gr_say_number_female(), handle_recordfile(), invent_message(), leave_voicemail(), meetme_menu_admin(), meetme_menu_admin_extended(), meetme_menu_normal(), minivm_greet_exec(), page_exec(), pbx_builtin_background(), pl_odtworz_plik(), play_and_wait(), play_file(), play_record_review(), playback_exec(), privacy_exec(), record_exec(), retrydial_exec(), s_streamwait3(), say_character_str_full(), say_digit_str_full(), say_phonetic_str_full(), select_item_menu(), setup_privacy_args(), vm_authenticate(), and wait_file().

01593 {
01594    int res;
01595 
01596    res = waitstream_core(c, breakon, NULL, NULL, 0, -1, -1, NULL, NULL /* no callback */);
01597 
01598    return sanitize_waitstream_return(res);
01599 }

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,
Return values:
0 if the stream finishes.
the character if it was interrupted.
-1 on error.

Definition at line 1611 of file file.c.

References ast_channel_context(), NULL, sanitize_waitstream_return(), and waitstream_core().

Referenced by pbx_builtin_background().

01612 {
01613    int res;
01614 
01615    /* Waitstream, with return in the case of a valid 1 digit extension */
01616    /* in the current or specified context being pressed */
01617    if (!context)
01618       context = ast_channel_context(c);
01619    res = waitstream_core(c, NULL, NULL, NULL, 0,
01620       -1, -1, context, NULL /* no callback */);
01621 
01622    return sanitize_waitstream_return(res);
01623 }

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,
Return values:
0 if the stream finishes.
the character if it was interrupted,
the value of the control frame if it was interrupted by some other party,
-1 on error.
Examples:
/tmp/asterisk-trunk/trunk/main/app.c.

Definition at line 1561 of file file.c.

References NULL, and waitstream_core().

Referenced by control_streamfile().

01562 {
01563    return waitstream_core(c, breakon, forward, reverse, ms,
01564       -1 /* no audiofd */, -1 /* no cmdfd */, NULL /* no context */, NULL /* no callback */);
01565 }

int ast_waitstream_fr_w_cb ( struct ast_channel c,
const char *  breakon,
const char *  forward,
const char *  rewind,
int  ms,
ast_waitstream_fr_cb  cb 
)

Same as waitstream_fr but allows a callback to be alerted when a user fastforwards or rewinds the file.

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 milliseconds to skip forward/back
cb to call when rewind or fastfoward occurs. Begins playback of a stream... Wait for a stream to stop or for any one of a given digit to arrive,
Return values:
0 if the stream finishes.
the character if it was interrupted,
the value of the control frame if it was interrupted by some other party,
-1 on error.
Examples:
/tmp/asterisk-trunk/trunk/main/app.c.

Definition at line 1550 of file file.c.

References NULL, and waitstream_core().

Referenced by control_streamfile().

01556 {
01557    return waitstream_core(c, breakon, forward, reverse, ms,
01558       -1 /* no audiofd */, -1 /* no cmdfd */, NULL /* no context */, cb);
01559 }

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

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.
Return values:
a struct ast_filestream on success.
NULL on failure.
Examples:
/tmp/asterisk-trunk/trunk/main/app.c.

Definition at line 1188 of file file.c.

References ast_alloca, ast_closestream(), ast_free, ast_log, ast_malloc, ast_opt_cache_record_files, AST_RWLIST_RDLOCK, AST_RWLIST_TRAVERSE, AST_RWLIST_UNLOCK, ast_strdup, ast_strdupa, buf, build_filename(), c, errno, ast_format_def::exts, exts_compare(), ast_filestream::f, f, ast_filestream::filename, ast_filestream::flags, ast_filestream::fmt, get_filestream(), LOG_WARNING, ast_filestream::mode, NULL, ast_filestream::realfilename, record_cache_dir, rewrite_wrapper(), ast_format_def::seek, ast_filestream::trans, ast_filestream::vfs, and ast_filestream::write_buffer.

Referenced by __ast_play_and_record(), ast_monitor_start(), ast_writestream(), dictate_exec(), handle_cli_file_convert(), handle_recordfile(), mixmonitor_save_prep(), record_exec(), and recordthread().

01189 {
01190    int fd, myflags = 0;
01191    /* compiler claims this variable can be used before initialization... */
01192    FILE *bfile = NULL;
01193    struct ast_format_def *f;
01194    struct ast_filestream *fs = NULL;
01195    char *buf = NULL;
01196    size_t size = 0;
01197    int format_found = 0;
01198 
01199    AST_RWLIST_RDLOCK(&formats);
01200 
01201    /* set the O_TRUNC flag if and only if there is no O_APPEND specified */
01202    /* We really can't use O_APPEND as it will break WAV header updates */
01203    if (flags & O_APPEND) {
01204       flags &= ~O_APPEND;
01205    } else {
01206       myflags = O_TRUNC;
01207    }
01208 
01209    myflags |= O_WRONLY | O_CREAT;
01210 
01211    /* XXX need to fix this - we should just do the fopen,
01212     * not open followed by fdopen()
01213     */
01214    AST_RWLIST_TRAVERSE(&formats, f, list) {
01215       char *fn, *orig_fn = NULL;
01216       if (fs)
01217          break;
01218 
01219       if (!exts_compare(f->exts, type))
01220          continue;
01221       else
01222          format_found = 1;
01223 
01224       fn = build_filename(filename, type);
01225       if (!fn) {
01226          continue;
01227       }
01228       fd = open(fn, flags | myflags, mode);
01229       if (fd > -1) {
01230          /* fdopen() the resulting file stream */
01231          bfile = fdopen(fd, ((flags | myflags) & O_RDWR) ? "w+" : "w");
01232          if (!bfile) {
01233             ast_log(LOG_WARNING, "Whoa, fdopen failed: %s!\n", strerror(errno));
01234             close(fd);
01235             fd = -1;
01236          }
01237       }
01238 
01239       if (ast_opt_cache_record_files && (fd > -1)) {
01240          char *c;
01241 
01242          fclose(bfile); /* this also closes fd */
01243          /*
01244            We touch orig_fn just as a place-holder so other things (like vmail) see the file is there.
01245            What we are really doing is writing to record_cache_dir until we are done then we will mv the file into place.
01246          */
01247          orig_fn = ast_strdupa(fn);
01248          for (c = fn; *c; c++)
01249             if (*c == '/')
01250                *c = '_';
01251 
01252          size = strlen(fn) + strlen(record_cache_dir) + 2;
01253          buf = ast_alloca(size);
01254          strcpy(buf, record_cache_dir);
01255          strcat(buf, "/");
01256          strcat(buf, fn);
01257          ast_free(fn);
01258          fn = buf;
01259          fd = open(fn, flags | myflags, mode);
01260          if (fd > -1) {
01261             /* fdopen() the resulting file stream */
01262             bfile = fdopen(fd, ((flags | myflags) & O_RDWR) ? "w+" : "w");
01263             if (!bfile) {
01264                ast_log(LOG_WARNING, "Whoa, fdopen failed: %s!\n", strerror(errno));
01265                close(fd);
01266                fd = -1;
01267             }
01268          }
01269       }
01270       if (fd > -1) {
01271          errno = 0;
01272          fs = get_filestream(f, bfile);
01273          if (fs) {
01274             if ((fs->write_buffer = ast_malloc(32768))) {
01275                setvbuf(fs->f, fs->write_buffer, _IOFBF, 32768);
01276             }
01277          }
01278          if (!fs || rewrite_wrapper(fs, comment)) {
01279             ast_log(LOG_WARNING, "Unable to rewrite %s\n", fn);
01280             close(fd);
01281             if (orig_fn) {
01282                unlink(fn);
01283                unlink(orig_fn);
01284             }
01285             if (fs) {
01286                ast_closestream(fs);
01287                fs = NULL;
01288             }
01289             if (!buf) {
01290                ast_free(fn);
01291             }
01292             continue;
01293          }
01294          fs->trans = NULL;
01295          fs->fmt = f;
01296          fs->flags = flags;
01297          fs->mode = mode;
01298          if (orig_fn) {
01299             fs->realfilename = ast_strdup(orig_fn);
01300             fs->filename = ast_strdup(fn);
01301          } else {
01302             fs->realfilename = NULL;
01303             fs->filename = ast_strdup(filename);
01304          }
01305          fs->vfs = NULL;
01306          /* If truncated, we'll be at the beginning; if not truncated, then append */
01307          f->seek(fs, 0, SEEK_END);
01308       } else if (errno != EEXIST) {
01309          ast_log(LOG_WARNING, "Unable to open file %s: %s\n", fn, strerror(errno));
01310          if (orig_fn)
01311             unlink(orig_fn);
01312       }
01313       /* if buf != NULL then fn is already free and pointing to it */
01314       if (!buf)
01315          ast_free(fn);
01316    }
01317 
01318    AST_RWLIST_UNLOCK(&formats);
01319 
01320    if (!format_found)
01321       ast_log(LOG_WARNING, "No such format '%s'\n", type);
01322 
01323    return fs;
01324 }

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
Return values:
0 on success.
-1 on failure.
Examples:
/tmp/asterisk-trunk/trunk/main/app.c.

Definition at line 210 of file file.c.

References ao2_replace, ast_debug, ast_format_cmp(), AST_FORMAT_CMP_EQUAL, AST_FORMAT_CMP_NOT_EQUAL, ast_format_get_name(), ast_format_get_type(), AST_FRAME_VIDEO, AST_FRAME_VOICE, ast_frfree, AST_LIST_NEXT, ast_log, AST_MEDIA_TYPE_AUDIO, ast_translate(), ast_translator_build_path(), ast_translator_free_path(), ast_writefile(), ast_writestream(), ast_filestream::filename, ast_filestream::flags, ast_filestream::fmt, ast_frame_subclass::format, ast_format_def::format, ast_frame::frametype, ast_filestream::lastwriteformat, LOG_WARNING, ast_filestream::mode, ast_format_def::name, NULL, ast_frame::subclass, ast_filestream::trans, type, ast_filestream::vfs, and ast_format_def::write.

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

00211 {
00212    int res = -1;
00213    if (f->frametype == AST_FRAME_VIDEO) {
00214       if (ast_format_get_type(fs->fmt->format) == AST_MEDIA_TYPE_AUDIO) {
00215          /* This is the audio portion.  Call the video one... */
00216          if (!fs->vfs && fs->filename) {
00217             const char *type = ast_format_get_name(f->subclass.format);
00218             fs->vfs = ast_writefile(fs->filename, type, NULL, fs->flags, 0, fs->mode);
00219             ast_debug(1, "Opened video output file\n");
00220          }
00221          if (fs->vfs)
00222             return ast_writestream(fs->vfs, f);
00223          /* else ignore */
00224          return 0;
00225       }
00226    } else if (f->frametype != AST_FRAME_VOICE) {
00227       ast_log(LOG_WARNING, "Tried to write non-voice frame\n");
00228       return -1;
00229    }
00230    if (ast_format_cmp(f->subclass.format, fs->fmt->format) != AST_FORMAT_CMP_NOT_EQUAL) {
00231       res =  fs->fmt->write(fs, f);
00232       if (res < 0)
00233          ast_log(LOG_WARNING, "Natural write failed\n");
00234       else if (res > 0)
00235          ast_log(LOG_WARNING, "Huh??\n");
00236    } else {
00237       /* XXX If they try to send us a type of frame that isn't the normal frame, and isn't
00238              the one we've setup a translator for, we do the "wrong thing" XXX */
00239       if (fs->trans && (ast_format_cmp(f->subclass.format, fs->lastwriteformat) != AST_FORMAT_CMP_EQUAL)) {
00240          ast_translator_free_path(fs->trans);
00241          fs->trans = NULL;
00242       }
00243       if (!fs->trans) {
00244          fs->trans = ast_translator_build_path(fs->fmt->format, f->subclass.format);
00245       }
00246       if (!fs->trans) {
00247          ast_log(LOG_WARNING, "Unable to translate to format %s, source format %s\n",
00248             fs->fmt->name, ast_format_get_name(f->subclass.format));
00249       } else {
00250          struct ast_frame *trf;
00251          ao2_replace(fs->lastwriteformat, f->subclass.format);
00252          /* Get the translated frame but don't consume the original in case they're using it on another stream */
00253          if ((trf = ast_translate(fs->trans, f, 0))) {
00254             struct ast_frame *cur;
00255 
00256             /* the translator may have returned multiple frames, so process them */
00257             for (cur = trf; cur; cur = AST_LIST_NEXT(cur, frame_list)) {
00258                if ((res = fs->fmt->write(fs, trf))) {
00259                   ast_log(LOG_WARNING, "Translated frame write failed\n");
00260                   break;
00261                }
00262             }
00263             ast_frfree(trf);
00264          } else {
00265             res = 0;
00266          }
00267       }
00268    }
00269    return res;
00270 }


Generated on Thu Apr 16 06:31:00 2015 for Asterisk - The Open Source Telephony Project by  doxygen 1.5.6