Fri Jun 19 12:10:35 2009

Asterisk developer's documentation


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...

#include <fcntl.h>
#include <sys/mman.h>

Go to the source code of this file.

Defines

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

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.
void ast_filestream_frame_freed (struct ast_frame *fr)
 destroy a filestream using an ast_frame as input
ast_filestreamast_openstream (struct ast_channel *chan, const char *filename, const char *preflang)
 Opens stream for use in seeking, playing.
ast_filestreamast_openstream_full (struct ast_channel *chan, const char *filename, const char *preflang, int asis)
 Opens stream for use in seeking, playing.
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.
ast_filestreamast_readfile (const char *filename, const char *type, const char *comment, int flags, int check, mode_t mode)
 Starts reading from a file.
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_full (struct ast_channel *c, const char *breakon, int audiofd, int monfd)
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"

Convenient for waiting

Definition at line 44 of file file.h.

Referenced by ast_ivr_menu_run_internal(), ast_play_and_wait(), ast_readstring_full(), ast_record_review(), bridge_playfile(), builtin_atxfer(), builtin_blindtransfer(), conf_exec(), conf_run(), dial_exec_full(), dictate_exec(), directory_exec(), festival_exec(), get_folder(), ivr_dispatch(), pbx_builtin_background(), play_file(), play_mailbox_owner(), play_message(), play_message_callerid(), play_message_datetime(), play_message_duration(), play_record_review(), retrydial_exec(), say_and_wait(), say_position(), sayname(), sayunixtime_exec(), select_item_menu(), select_item_seq(), try_calling(), vm_intro_gr(), vm_intro_he(), vm_intro_multilang(), vm_intro_pt(), vm_intro_pt_BR(), and wait_file2().

#define AST_DIGIT_ANYNUM   "0123456789"

Definition at line 45 of file file.h.

Referenced by initreqprep().

#define AST_RESERVED_POINTERS   20

Definition at line 341 of file file.h.

#define SEEK_FORCECUR   10

Definition at line 47 of file file.h.

Referenced by __ast_read(), ast_write(), au_seek(), g729_seek(), gsm_seek(), ilbc_seek(), pcm_seek(), slinear_seek(), vox_seek(), and wav_seek().


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

References chan, and s.

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

00819 {
00820    s->owner = chan;
00821    return 0;
00822 }

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.

Definition at line 861 of file file.c.

References ao2_ref, AST_FRFLAG_FROM_FILESTREAM, ast_test_flag, and f.

Referenced by __ast_play_and_record(), ast_filehelper(), ast_hangup(), ast_moh_files_next(), ast_monitor_start(), ast_monitor_stop(), ast_readfile(), ast_stopstream(), ast_writefile(), dictate_exec(), filestream_destructor(), gen_closestream(), handle_cli_file_convert(), handle_recordfile(), local_ast_moh_stop(), mixmonitor_thread(), moh_files_release(), and rpt().

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

int ast_file_init ( void   ) 

Provided by file.c

Definition at line 1369 of file file.c.

References ast_cli_register_multiple(), and cli_file.

Referenced by main().

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

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

References ast_filehelper().

Referenced by copy_plain_file(), and vm_forwardoptions().

00910 {
00911    return ast_filehelper(filename, filename2, fmt, ACTION_COPY);
00912 }

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

References ACTION_DELETE, and ast_filehelper().

Referenced by __ast_play_and_record(), announce_thread(), ast_monitor_start(), ast_monitor_stop(), conf_free(), conf_run(), handle_cli_file_convert(), leave_voicemail(), play_record_review(), setup_privacy_args(), vm_delete(), and vm_forwardoptions().

00900 {
00901    return ast_filehelper(filename, NULL, fmt, ACTION_DELETE);
00902 }

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

References buf, and fileexists_core().

Referenced by announce_thread(), app_exec(), ast_moh_files_next(), ast_monitor_start(), ast_monitor_stop(), common_exec(), eivr_comm(), forward_message(), function_playback(), invent_message(), leave_voicemail(), minivm_delete_exec(), play_message(), play_message_callerid(), record_exec(), retrydial_exec(), rpt_tele_thread(), say_character_str_full(), say_digit_str_full(), say_phonetic_str_full(), sayname(), saynode(), setup_privacy_args(), vm_intro(), vm_newuser(), vm_options(), and vm_tempgreeting().

00886 {
00887    char *buf;
00888    int buflen;
00889 
00890    if (preflang == NULL)
00891       preflang = "";
00892    buflen = strlen(preflang) + strlen(filename) + 4;  /* room for everything */
00893    buf = alloca(buflen);
00894    if (buf == NULL)
00895       return 0;
00896    return fileexists_core(filename, fmt, preflang, buf, buflen);
00897 }

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

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

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

References ao2_ref, ast_clear_flag, AST_FRFLAG_FROM_FILESTREAM, and ast_filestream::fr.

Referenced by ast_frame_free().

01302 {
01303    struct ast_filestream *fs;
01304 
01305    ast_clear_flag(fr, AST_FRFLAG_FROM_FILESTREAM);
01306 
01307    fs = (struct ast_filestream *) (((char *) fr) - offsetof(struct ast_filestream, fr));
01308 
01309    ao2_ref(fs, -1);
01310 }

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

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

References ast_openstream_full(), and chan.

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

00611 {
00612    return ast_openstream_full(chan, filename, preflang, 0);
00613 }

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

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

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

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

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

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

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

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

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

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

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

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

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

00825 {
00826    enum fsread_res res;
00827 
00828    if (s->fmt->format & AST_FORMAT_AUDIO_MASK)
00829       res = ast_readaudio_callback(s);
00830    else
00831       res = ast_readvideo_callback(s);
00832 
00833    return (res == FSREAD_FAILURE) ? -1 : 0;
00834 }

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

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.

Definition at line 956 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::exts, exts_compare(), f, ast_filestream::filename, ast_filestream::flags, ast_filestream::fmt, get_filestream(), LOG_WARNING, ast_filestream::mode, open_wrapper(), ast_filestream::trans, and ast_filestream::vfs.

Referenced by __ast_play_and_record(), and handle_cli_file_convert().

00957 {
00958    FILE *bfile;
00959    struct ast_format *f;
00960    struct ast_filestream *fs = NULL;
00961    char *fn;
00962    int format_found = 0;   
00963 
00964    AST_RWLIST_RDLOCK(&formats);
00965 
00966    AST_RWLIST_TRAVERSE(&formats, f, list) {
00967       fs = NULL;
00968       if (!exts_compare(f->exts, type))
00969          continue;
00970       else 
00971          format_found = 1;
00972 
00973       fn = build_filename(filename, type);
00974       errno = 0;
00975       bfile = fopen(fn, "r");
00976 
00977       if (!bfile || (fs = get_filestream(f, bfile)) == NULL || open_wrapper(fs) ) {
00978          ast_log(LOG_WARNING, "Unable to open %s\n", fn);
00979          if (fs) {
00980             ast_closestream(fs);
00981          }
00982          fs = NULL;
00983          bfile = NULL;
00984          ast_free(fn);
00985          break;            
00986       }
00987       /* found it */
00988       fs->trans = NULL;
00989       fs->fmt = f;
00990       fs->flags = flags;
00991       fs->mode = mode;
00992       fs->filename = ast_strdup(filename);
00993       fs->vfs = NULL;
00994       break;
00995    }
00996 
00997    AST_RWLIST_UNLOCK(&formats);
00998    if (!format_found)
00999       ast_log(LOG_WARNING, "No such format '%s'\n", type);
01000 
01001    return fs;
01002 }

struct ast_frame* ast_readframe ( struct ast_filestream s  ) 

Read a frame from a filestream.

Parameters:
s ast_filestream to act on
Returns:
a frame.
Return values:
NULL if read failed.

Definition at line 686 of file file.c.

References ao2_ref, AST_FRFLAG_FROM_FILESTREAM, ast_set_flag, f, and s.

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

00687 {
00688    struct ast_frame *f = NULL;
00689    int whennext = 0; 
00690    if (s && s->fmt)
00691       f = s->fmt->read(s, &whennext);
00692    if (f) {
00693       ast_set_flag(f, AST_FRFLAG_FROM_FILESTREAM);
00694       ao2_ref(s, +1);
00695    }
00696    return f;
00697 }

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.

Definition at line 836 of file file.c.

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

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

00837 {
00838    return fs->fmt->seek(fs, sample_offset, whence);
00839 }

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 122 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_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_hu(), 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_th(), ast_say_number_full_tw(), background_detect_exec(), builtin_blindtransfer(), conf_exec(), conf_run(), directory_exec(), handle_getoption(), handle_speechrecognize(), handle_streamfile(), ices_exec(), ivr_dispatch(), leave_voicemail(), minivm_greet_exec(), mp3_exec(), NBScat_exec(), parkandannounce_exec(), pbx_builtin_background(), pl_odtworz_plik(), play_file(), play_mailbox_owner(), playback_exec(), read_exec(), readexten_exec(), recordthread(), rpt_tele_thread(), s_streamwait3(), say_character_str_full(), say_digit_str_full(), say_phonetic_str_full(), saycharstr(), sayfile(), saynum(), select_item_seq(), send_morse(), send_tone_telemetry(), send_waveform_to_channel(), speech_background(), vm_authenticate(), vm_execmain(), wait_for_winner(), waitstream_core(), and zapateller_exec().

00123 {
00124    ast_channel_lock(tmp);
00125 
00126    /* Stop a running stream if there is one */
00127    if (tmp->stream) {
00128       ast_closestream(tmp->stream);
00129       tmp->stream = NULL;
00130       if (tmp->oldwriteformat && ast_set_write_format(tmp, tmp->oldwriteformat))
00131          ast_log(LOG_WARNING, "Unable to restore format back to %d\n", tmp->oldwriteformat);
00132    }
00133    /* Stop the video stream too */
00134    if (tmp->vstream != NULL) {
00135       ast_closestream(tmp->vstream);
00136       tmp->vstream = NULL;
00137    }
00138 
00139    ast_channel_unlock(tmp);
00140 
00141    return 0;
00142 }

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.

Note:
If digits == "" then we can simply check for non-zero.
Returns:
0 if success.
Return values:
-1 if error.
digit if interrupted by a digit.

Definition at line 1317 of file file.c.

References ast_streamfile(), ast_strlen_zero(), ast_waitstream(), chan, and ast_channel::language.

Referenced by __ast_play_and_record(), app_exec(), ast_record_review(), bridge_playfile(), builtin_atxfer(), builtin_automixmonitor(), builtin_blindtransfer(), directory_exec(), forward_message(), invent_message(), ivr_dispatch(), leave_voicemail(), masq_park_call(), park_exec_full(), play_mailbox_owner(), play_message_callerid(), play_message_in_bridged_call(), play_record_review(), sayname(), select_item_seq(), and wait_file2().

01318 {
01319    int res = 0;
01320    if (!ast_strlen_zero(file)) {
01321       res = ast_streamfile(chan, file, chan->language);
01322       if (!res) {
01323          res = ast_waitstream(chan, digits);
01324       }
01325    }
01326    return res;
01327 } 

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

References ast_seekstream(), and DEFAULT_SAMPLES_PER_MS.

Referenced by waitstream_core().

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

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.

Definition at line 856 of file file.c.

References ast_seekstream(), and DEFAULT_SAMPLES_PER_MS.

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

00857 {
00858    return ast_seekstream(fs, -ms * DEFAULT_SAMPLES_PER_MS, SEEK_CUR);
00859 }

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.

Definition at line 914 of file file.c.

References ast_applystream(), ast_debug, AST_FLAG_MASQ_NOSTREAM, ast_getformatname(), ast_getformatname_multiple(), ast_log(), ast_openstream(), ast_openvstream(), ast_playstream(), ast_seekstream(), ast_strdup, ast_test_flag, ast_verb, chan, errno, ast_filestream::f, ast_filestream::fmt, ast_format::format, LOG_WARNING, ast_channel::name, ast_channel::nativeformats, ast_filestream::vfs, and ast_channel::writeformat.

Referenced by 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_hu(), 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_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_th(), 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_hu(), 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_th(), 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_hu(), ast_say_time_nl(), ast_say_time_tw(), ast_stream_and_wait(), background_detect_exec(), bridge_exec(), check_availability(), check_beep(), common_exec(), conf_exec(), conf_run(), dial_exec_full(), do_directory(), find_conf_realtime(), forward_message(), gr_say_number_female(), handle_recordfile(), invent_message(), leave_voicemail(), login_exec(), minivm_greet_exec(), page_exec(), park_exec_full(), parkandannounce_exec(), pbx_builtin_background(), pl_odtworz_plik(), play_and_wait(), play_file(), play_record_review(), playback_exec(), privacy_exec(), readexten_exec(), retrydial_exec(), rpt_tele_thread(), s_streamwait3(), say_character_str_full(), say_digit_str_full(), say_phonetic_str_full(), sayfile(), select_item_menu(), setup_privacy_args(), ss_thread(), vm_authenticate(), wait_file(), and wait_for_winner().

00915 {
00916    struct ast_filestream *fs;
00917    struct ast_filestream *vfs=NULL;
00918    char fmt[256];
00919    int seekattempt;
00920    int res;
00921 
00922    fs = ast_openstream(chan, filename, preflang);
00923    if (!fs) {
00924       ast_log(LOG_WARNING, "Unable to open %s (format %s): %s\n", filename, ast_getformatname_multiple(fmt, sizeof(fmt), chan->nativeformats), strerror(errno));
00925       return -1;
00926    }
00927 
00928    /* check to see if there is any data present (not a zero length file),
00929     * done this way because there is no where for ast_openstream_full to
00930     * return the file had no data. */
00931    seekattempt = fseek(fs->f, -1, SEEK_END);
00932    if (!seekattempt)
00933       ast_seekstream(fs, 0, SEEK_SET);
00934    else
00935       return 0;
00936 
00937    vfs = ast_openvstream(chan, filename, preflang);
00938    if (vfs) {
00939       ast_debug(1, "Ooh, found a video stream, too, format %s\n", ast_getformatname(vfs->fmt->format));
00940    }
00941 
00942    if (ast_test_flag(chan, AST_FLAG_MASQ_NOSTREAM))
00943       fs->orig_chan_name = ast_strdup(chan->name);
00944    if (ast_applystream(chan, fs))
00945       return -1;
00946    if (vfs && ast_applystream(chan, vfs))
00947       return -1;
00948    res = ast_playstream(fs);
00949    if (!res && vfs)
00950       res = ast_playstream(vfs);
00951    ast_verb(3, "<%s> Playing '%s.%s' (language '%s')\n", chan->name, filename, ast_getformatname(chan->writeformat), preflang ? preflang : "default");
00952 
00953    return res;
00954 }

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 846 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(), handle_speechrecognize(), and handle_streamfile().

00847 {
00848    return fs->fmt->tell(fs);
00849 }

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.

Definition at line 841 of file file.c.

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

Referenced by __ast_play_and_record(), and handle_recordfile().

00842 {
00843    return fs->fmt->trunc(fs);
00844 }

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,
-1 on error

Definition at line 1279 of file file.c.

References waitstream_core().

Referenced by action_bridge(), agent_call(), announce_thread(), 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_ge(), ast_say_date_gr(), ast_say_date_he(), ast_say_date_hu(), 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_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_th(), 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_hu(), 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_th(), 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_hu(), ast_say_time_nl(), ast_say_time_tw(), ast_stream_and_wait(), bridge_exec(), check_availability(), check_beep(), common_exec(), conf_exec(), conf_run(), dial_exec_full(), directory_exec(), find_conf_realtime(), gr_say_number_female(), handle_recordfile(), invent_message(), leave_voicemail(), login_exec(), minivm_greet_exec(), page_exec(), park_exec_full(), parkandannounce_exec(), pbx_builtin_background(), pl_odtworz_plik(), play_and_wait(), play_file(), play_record_review(), playback_exec(), privacy_exec(), retrydial_exec(), rpt_tele_thread(), s_streamwait3(), say_character_str_full(), say_digit_str_full(), say_phonetic_str_full(), saycharstr(), sayfile(), saynum(), select_item_menu(), send_morse(), send_tone_telemetry(), setup_privacy_args(), ss_thread(), vm_authenticate(), and wait_file().

01280 {
01281    return waitstream_core(c, breakon, NULL, NULL, 0, -1, -1, NULL);
01282 }

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

References ast_channel::context, and waitstream_core().

Referenced by pbx_builtin_background().

01291 {
01292    /* Waitstream, with return in the case of a valid 1 digit extension */
01293    /* in the current or specified context being pressed */
01294 
01295    if (!context)
01296       context = c->context;
01297    return waitstream_core(c, NULL, NULL, NULL, 0,
01298       -1, -1, context);
01299 }

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.
-1 on error.

Definition at line 1273 of file file.c.

References waitstream_core().

Referenced by ast_control_streamfile().

01274 {
01275    return waitstream_core(c, breakon, forward, reverse, ms,
01276       -1 /* no audiofd */, -1 /* no cmdfd */, NULL /* no context */);
01277 }

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

Same as waitstream, but with audio output to fd and monitored fd checking.

Returns:
1 if monfd is ready for reading

Definition at line 1284 of file file.c.

References waitstream_core().

Referenced by 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_hu(), 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_th(), ast_say_number_full_tw(), handle_getoption(), handle_streamfile(), pl_odtworz_plik(), s_streamwait3(), say_character_str_full(), say_digit_str_full(), and say_phonetic_str_full().

01285 {
01286    return waitstream_core(c, breakon, NULL, NULL, 0,
01287       audiofd, cmdfd, NULL /* no context */);
01288 }

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

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.

Definition at line 1004 of file file.c.

References 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(), errno, ast_format::exts, exts_compare(), ast_filestream::f, f, ast_filestream::filename, ast_filestream::flags, ast_filestream::fmt, get_filestream(), LOG_WARNING, ast_filestream::mode, ast_filestream::realfilename, record_cache_dir, rewrite_wrapper(), ast_format::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_thread(), recordthread(), and rpt().

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

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.

Definition at line 144 of file file.c.

References ast_debug, AST_FORMAT_AUDIO_MASK, 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(), f, ast_filestream::filename, ast_filestream::flags, ast_filestream::fmt, ast_format::format, ast_filestream::lastwriteformat, LOG_WARNING, ast_filestream::mode, ast_format::name, ast_filestream::trans, type, ast_filestream::vfs, and ast_format::write.

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

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


Generated on Fri Jun 19 12:10:35 2009 for Asterisk - the Open Source PBX by  doxygen 1.4.7