Main Page | Alphabetical List | Data Structures | File List | Data Fields | Globals

file.h File Reference

#include <asterisk/channel.h>
#include <asterisk/frame.h>
#include <fcntl.h>

Include dependency graph for file.h:

Include dependency graph

This graph shows which files directly or indirectly include this file:

Included by dependency graph

Go to the source code of this file.

Defines

#define AST_DIGIT_ANY   "0123456789#*"
 Convenient for waiting.

#define SEEK_FORCECUR   10
#define AST_RESERVED_POINTERS   20

Functions

int ast_format_register (char *name, char *exts, int format, struct ast_filestream *(*open)(int fd), struct ast_filestream *(*rewrite)(int fd, char *comment), int(*write)(struct ast_filestream *, struct ast_frame *), int(*seek)(struct ast_filestream *, long offset, int whence), int(*trunc)(struct ast_filestream *), long(*tell)(struct ast_filestream *), struct ast_frame *(*read)(struct ast_filestream *, int *timetonext), void(*close)(struct ast_filestream *), char *(*getcomment)(struct ast_filestream *))
 Registers a new file format.

int ast_format_unregister (char *name)
 Unregisters a file format.

int ast_streamfile (struct ast_channel *c, char *filename, char *preflang)
 Streams a file.

int ast_stopstream (struct ast_channel *c)
 Stops a stream.

int ast_fileexists (char *filename, char *fmt, char *preflang)
 Checks for the existence of a given file.

int ast_filerename (char *oldname, char *newname, char *fmt)
 Renames a file.

int ast_filedelete (char *filename, char *fmt)
 Deletes a file.

int ast_filecopy (char *oldname, char *newname, char *fmt)
 Copies a file.

char ast_waitstream (struct ast_channel *c, char *breakon)
 Waits for a stream to stop or digit to be pressed.

char ast_waitstream_fr (struct ast_channel *c, char *breakon, char *forward, char *rewind, int ms)
 Same as waitstream but allows stream to be forwarded or rewound.

char ast_waitstream_full (struct ast_channel *c, char *breakon, int audiofd, int monfd)
ast_filestreamast_readfile (char *filename, char *type, char *comment, int flags, int check, mode_t mode)
 Starts reading from a file.

ast_filestreamast_writefile (char *filename, char *type, 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.

int ast_closestream (struct ast_filestream *f)
 Closes a stream.

ast_filestreamast_openstream (struct ast_channel *chan, char *filename, char *preflang)
 Opens stream for use in seeking, playing.

ast_filestreamast_openvstream (struct ast_channel *chan, char *filename, char *preflang)
 Opens stream for use in seeking, playing.

int ast_applystream (struct ast_channel *chan, struct ast_filestream *s)
 Applys a open stream to a channel.

int ast_playstream (struct ast_filestream *s)
 play a open stream on a channel.

int ast_seekstream (struct ast_filestream *fs, long sample_offset, int whence)
 Seeks into stream.

int ast_truncstream (struct ast_filestream *fs)
 Trunc stream at current location.

int ast_stream_fastforward (struct ast_filestream *fs, long ms)
 Fast forward stream ms.

int ast_stream_rewind (struct ast_filestream *fs, long ms)
 Rewind stream ms.

long ast_tellstream (struct ast_filestream *fs)
 Tell where we are in a stream.

ast_frameast_readframe (struct ast_filestream *s)
 Read a frame from a filestream.


Define Documentation

#define AST_DIGIT_ANY   "0123456789#*"
 

Convenient for waiting.

Definition at line 28 of file file.h.

Referenced by ast_readstring(), and ast_readstring_full().

#define AST_RESERVED_POINTERS   20
 

Definition at line 287 of file file.h.

#define SEEK_FORCECUR   10
 

Definition at line 30 of file file.h.

Referenced by ast_read(), and ast_write().


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
ast_filestream s to apply Returns 0 for success, -1 on failure

Definition at line 596 of file file.c.

References s.

Referenced by ast_streamfile().

00597 { 00598 s->owner = chan; 00599 return 0; 00600 }

int ast_closestream struct ast_filestream f  ) 
 

Closes a stream.

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

Definition at line 641 of file file.c.

References AST_FORMAT_MAX_AUDIO, ast_sched_del(), ast_settimeout(), ast_translator_free_path(), ast_format::close, ast_filestream::filename, ast_filestream::fmt, ast_format::format, free, ast_filestream::owner, ast_channel::sched, ast_channel::stream, ast_channel::streamid, ast_filestream::trans, ast_channel::vstream, and ast_channel::vstreamid.

Referenced by ast_app_getvoice(), ast_hangup(), and ast_stopstream().

00642 { 00643 /* Stop a running stream if there is one */ 00644 if (f->owner) { 00645 if (f->fmt->format < AST_FORMAT_MAX_AUDIO) { 00646 f->owner->stream = NULL; 00647 if (f->owner->streamid > -1) 00648 ast_sched_del(f->owner->sched, f->owner->streamid); 00649 f->owner->streamid = -1; 00650 #ifdef ZAPTEL_OPTIMIZATIONS 00651 ast_settimeout(f->owner, 0, NULL, NULL); 00652 #endif 00653 } else { 00654 f->owner->vstream = NULL; 00655 if (f->owner->vstreamid > -1) 00656 ast_sched_del(f->owner->sched, f->owner->vstreamid); 00657 f->owner->vstreamid = -1; 00658 } 00659 } 00660 /* destroy the translator on exit */ 00661 if (f->trans) { 00662 ast_translator_free_path(f->trans); 00663 f->trans = NULL; 00664 } 00665 if (f->filename) 00666 free(f->filename); 00667 f->filename = NULL; 00668 f->fmt->close(f); 00669 return 0; 00670 }

int ast_filecopy char *  oldname,
char *  newname,
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 723 of file file.c.

References ACTION_COPY.

00724 { 00725 return ast_filehelper(filename, filename2, fmt, ACTION_COPY); 00726 }

int ast_filedelete char *  filename,
char *  fmt
 

Deletes a file.

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

Definition at line 713 of file file.c.

References ACTION_DELETE.

00714 { 00715 return ast_filehelper(filename, NULL, fmt, ACTION_DELETE); 00716 }

int ast_fileexists char *  filename,
char *  fmt,
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 673 of file file.c.

References ACTION_EXISTS, and MAX_LANGUAGE.

Referenced by ast_openstream(), and ast_openvstream().

00674 { 00675 char filename2[256]; 00676 char tmp[256]; 00677 char *postfix; 00678 char *prefix; 00679 char *c; 00680 char lang2[MAX_LANGUAGE]; 00681 int res = -1; 00682 if (preflang && !ast_strlen_zero(preflang)) { 00683 /* Insert the language between the last two parts of the path */ 00684 strncpy(tmp, filename, sizeof(tmp) - 1); 00685 c = strrchr(tmp, '/'); 00686 if (c) { 00687 *c = '\0'; 00688 postfix = c+1; 00689 prefix = tmp; 00690 } else { 00691 postfix = tmp; 00692 prefix=""; 00693 } 00694 snprintf(filename2, sizeof(filename2), "%s/%s/%s", prefix, preflang, postfix); 00695 res = ast_filehelper(filename2, NULL, fmt, ACTION_EXISTS); 00696 if (res < 1) { 00697 char *stringp=NULL; 00698 strncpy(lang2, preflang, sizeof(lang2)-1); 00699 stringp=lang2; 00700 strsep(&stringp, "_"); 00701 if (strcmp(lang2, preflang)) { 00702 snprintf(filename2, sizeof(filename2), "%s/%s/%s", prefix, lang2, postfix); 00703 res = ast_filehelper(filename2, NULL, fmt, ACTION_EXISTS); 00704 } 00705 } 00706 } 00707 if (res < 1) { 00708 res = ast_filehelper(filename, NULL, fmt, ACTION_EXISTS); 00709 } 00710 return res; 00711 }

int ast_filerename char *  oldname,
char *  newname,
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 718 of file file.c.

References ACTION_RENAME.

00719 { 00720 return ast_filehelper(filename, filename2, fmt, ACTION_RENAME); 00721 }

int ast_format_register char *  name,
char *  exts,
int  format,
struct ast_filestream *(*  open)(int fd),
struct ast_filestream *(*  rewrite)(int fd, char *comment),
int(*  write)(struct ast_filestream *, struct ast_frame *),
int(*  seek)(struct ast_filestream *, long offset, int whence),
int(*  trunc)(struct ast_filestream *),
long(*  tell)(struct ast_filestream *),
struct ast_frame *(*  read)(struct ast_filestream *, int *timetonext),
void(*  close)(struct ast_filestream *),
char *(*  getcomment)(struct ast_filestream *)
 

Registers a new file format.

Register a new file format capability Adds a format to asterisk's format abilities. Fill in the fields, and it will work. For examples, look at some of the various format code. returns 0 on success, -1 on failure

int ast_format_unregister char *  name  ) 
 

Unregisters a file format.

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

Definition at line 137 of file file.c.

References ast_log(), ast_mutex_lock, ast_mutex_unlock, ast_verbose(), free, LOG_WARNING, ast_format::next, option_verbose, and VERBOSE_PREFIX_2.

00138 { 00139 struct ast_format *tmp, *tmpl = NULL; 00140 if (ast_mutex_lock(&formatlock)) { 00141 ast_log(LOG_WARNING, "Unable to lock format list\n"); 00142 return -1; 00143 } 00144 tmp = formats; 00145 while(tmp) { 00146 if (!strcasecmp(name, tmp->name)) { 00147 if (tmpl) 00148 tmpl->next = tmp->next; 00149 else 00150 formats = tmp->next; 00151 free(tmp); 00152 ast_mutex_unlock(&formatlock); 00153 if (option_verbose > 1) 00154 ast_verbose( VERBOSE_PREFIX_2 "Unregistered format %s\n", name); 00155 return 0; 00156 } 00157 tmpl = tmp; 00158 tmp = tmp->next; 00159 } 00160 ast_log(LOG_WARNING, "Tried to unregister format %s, already unregistered\n", name); 00161 return -1; 00162 }

struct ast_filestream* ast_openstream struct ast_channel chan,
char *  filename,
char *  preflang
 

Opens stream for use in seeking, playing.

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

Definition at line 424 of file file.c.

References ACTION_OPEN, ast_deactivate_generator(), ast_fileexists(), ast_log(), ast_set_write_format(), ast_stopstream(), ast_channel::generator, LOG_WARNING, ast_channel::oldwriteformat, and ast_channel::writeformat.

Referenced by ast_streamfile().

00425 { 00426 /* This is a fairly complex routine. Essentially we should do 00427 the following: 00428 00429 1) Find which file handlers produce our type of format. 00430 2) Look for a filename which it can handle. 00431 3) If we find one, then great. 00432 4) If not, see what files are there 00433 5) See what we can actually support 00434 6) Choose the one with the least costly translator path and 00435 set it up. 00436 00437 */ 00438 int fd = -1; 00439 int fmts = -1; 00440 char filename2[256]=""; 00441 char filename3[256]=""; 00442 char *endpart; 00443 int res; 00444 ast_stopstream(chan); 00445 /* do this first, otherwise we detect the wrong writeformat */ 00446 if (chan->generator) 00447 ast_deactivate_generator(chan); 00448 if (preflang && !ast_strlen_zero(preflang)) { 00449 strncpy(filename3, filename, sizeof(filename3) - 1); 00450 endpart = strrchr(filename3, '/'); 00451 if (endpart) { 00452 *endpart = '\0'; 00453 endpart++; 00454 snprintf(filename2, sizeof(filename2), "%s/%s/%s", filename3, preflang, endpart); 00455 } else 00456 snprintf(filename2, sizeof(filename2), "%s/%s", preflang, filename); 00457 fmts = ast_fileexists(filename2, NULL, NULL); 00458 } 00459 if (fmts < 1) { 00460 strncpy(filename2, filename, sizeof(filename2)-1); 00461 fmts = ast_fileexists(filename2, NULL, NULL); 00462 } 00463 if (fmts < 1) { 00464 ast_log(LOG_WARNING, "File %s does not exist in any format\n", filename); 00465 return NULL; 00466 } 00467 chan->oldwriteformat = chan->writeformat; 00468 /* Set the channel to a format we can work with */ 00469 res = ast_set_write_format(chan, fmts); 00470 00471 fd = ast_filehelper(filename2, (char *)chan, NULL, ACTION_OPEN); 00472 if(fd >= 0) 00473 return chan->stream; 00474 return NULL; 00475 }

struct ast_filestream* ast_openvstream struct ast_channel chan,
char *  filename,
char *  preflang
 

Opens stream for use in seeking, playing.

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

Definition at line 477 of file file.c.

References ACTION_OPEN, ast_fileexists(), ast_log(), LOG_WARNING, and MAX_LANGUAGE.

Referenced by ast_streamfile().

00478 { 00479 /* This is a fairly complex routine. Essentially we should do 00480 the following: 00481 00482 1) Find which file handlers produce our type of format. 00483 2) Look for a filename which it can handle. 00484 3) If we find one, then great. 00485 4) If not, see what files are there 00486 5) See what we can actually support 00487 6) Choose the one with the least costly translator path and 00488 set it up. 00489 00490 */ 00491 int fd = -1; 00492 int fmts = -1; 00493 char filename2[256]; 00494 char lang2[MAX_LANGUAGE]; 00495 /* XXX H.263 only XXX */ 00496 char *fmt = "h263"; 00497 if (preflang && !ast_strlen_zero(preflang)) { 00498 snprintf(filename2, sizeof(filename2), "%s/%s", preflang, filename); 00499 fmts = ast_fileexists(filename2, fmt, NULL); 00500 if (fmts < 1) { 00501 strncpy(lang2, preflang, sizeof(lang2)-1); 00502 snprintf(filename2, sizeof(filename2), "%s/%s", lang2, filename); 00503 fmts = ast_fileexists(filename2, fmt, NULL); 00504 } 00505 } 00506 if (fmts < 1) { 00507 strncpy(filename2, filename, sizeof(filename2)-1); 00508 fmts = ast_fileexists(filename2, fmt, NULL); 00509 } 00510 if (fmts < 1) { 00511 return NULL; 00512 } 00513 fd = ast_filehelper(filename2, (char *)chan, fmt, ACTION_OPEN); 00514 if(fd >= 0) 00515 return chan->vstream; 00516 ast_log(LOG_WARNING, "File %s has video but couldn't be opened\n", filename); 00517 return NULL; 00518 }

int ast_playstream struct ast_filestream s  ) 
 

play a open stream on a channel.

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

Definition at line 602 of file file.c.

References AST_FORMAT_MAX_AUDIO, and s.

Referenced by ast_streamfile().

00603 { 00604 if (s->fmt->format < AST_FORMAT_MAX_AUDIO) 00605 ast_readaudio_callback(s); 00606 else 00607 ast_readvideo_callback(s); 00608 return 0; 00609 }

struct ast_filestream* ast_readfile char *  filename,
char *  type,
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. Returns a struct ast_filestream on success, NULL on failure

Definition at line 756 of file file.c.

References ast_log(), ast_mutex_lock, ast_mutex_unlock, free, LOG_WARNING, strdup, and type.

00757 { 00758 int fd,myflags = 0; 00759 struct ast_format *f; 00760 struct ast_filestream *fs=NULL; 00761 char *fn; 00762 char *ext; 00763 if (ast_mutex_lock(&formatlock)) { 00764 ast_log(LOG_WARNING, "Unable to lock format list\n"); 00765 return NULL; 00766 } 00767 f = formats; 00768 while(f) { 00769 if (exts_compare(f->exts, type)) { 00770 char *stringp=NULL; 00771 /* XXX Implement check XXX */ 00772 ext = strdup(f->exts); 00773 stringp=ext; 00774 ext = strsep(&stringp, "|"); 00775 fn = build_filename(filename, ext); 00776 fd = open(fn, flags | myflags); 00777 if (fd >= 0) { 00778 errno = 0; 00779 if ((fs = f->open(fd))) { 00780 fs->trans = NULL; 00781 fs->fmt = f; 00782 fs->flags = flags; 00783 fs->mode = mode; 00784 fs->filename = strdup(filename); 00785 fs->vfs = NULL; 00786 } else { 00787 ast_log(LOG_WARNING, "Unable to open %s\n", fn); 00788 close(fd); 00789 unlink(fn); 00790 } 00791 } else if (errno != EEXIST) 00792 ast_log(LOG_WARNING, "Unable to open file %s: %s\n", fn, strerror(errno)); 00793 free(fn); 00794 free(ext); 00795 break; 00796 } 00797 f = f->next; 00798 } 00799 ast_mutex_unlock(&formatlock); 00800 if (!f) 00801 ast_log(LOG_WARNING, "No such format '%s'\n", type); 00802 return fs; 00803 }

struct ast_frame* ast_readframe struct ast_filestream s  ) 
 

Read a frame from a filestream.

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

Definition at line 520 of file file.c.

References s.

00521 { 00522 struct ast_frame *f = NULL; 00523 int whennext = 0; 00524 if (s && s->fmt) 00525 f = s->fmt->read(s, &whennext); 00526 return f; 00527 }

int ast_seekstream struct ast_filestream fs,
long  sample_offset,
int  whence
 

Seeks into stream.

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

Definition at line 611 of file file.c.

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

Referenced by ast_read(), ast_stream_fastforward(), ast_stream_rewind(), and ast_write().

00612 { 00613 return fs->fmt->seek(fs, sample_offset, whence); 00614 }

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 Returns 0 regardless

Definition at line 164 of file file.c.

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

Referenced by ast_control_streamfile(), ast_openstream(), ast_readstring(), ast_readstring_full(), ast_say_character_str(), ast_say_character_str_full(), ast_say_digit_str(), ast_say_digit_str_full(), ast_say_phonetic_str(), ast_say_phonetic_str_full(), ast_waitstream(), ast_waitstream_fr(), and ast_waitstream_full().

00165 { 00166 /* Stop a running stream if there is one */ 00167 if (tmp->vstream) 00168 ast_closestream(tmp->vstream); 00169 if (tmp->stream) { 00170 ast_closestream(tmp->stream); 00171 if (tmp->oldwriteformat && ast_set_write_format(tmp, tmp->oldwriteformat)) 00172 ast_log(LOG_WARNING, "Unable to restore format back to %d\n", tmp->oldwriteformat); 00173 } 00174 return 0; 00175 }

int ast_stream_fastforward struct ast_filestream fs,
long  ms
 

Fast forward stream ms.

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

Definition at line 626 of file file.c.

References ast_seekstream().

Referenced by ast_control_streamfile(), and ast_waitstream_fr().

00627 { 00628 /* I think this is right, 8000 samples per second, 1000 ms a second so 8 00629 * samples per ms */ 00630 long samples = ms * 8; 00631 return ast_seekstream(fs, samples, SEEK_CUR); 00632 }

int ast_stream_rewind struct ast_filestream fs,
long  ms
 

Rewind stream ms.

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

Definition at line 634 of file file.c.

References ast_seekstream().

Referenced by ast_waitstream_fr().

00635 { 00636 long samples = ms * 8; 00637 samples = samples * -1; 00638 return ast_seekstream(fs, samples, SEEK_CUR); 00639 }

int ast_streamfile struct ast_channel c,
char *  filename,
char *  preflang
 

Streams a file.

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

Definition at line 728 of file file.c.

References ast_applystream(), ast_getformatname(), ast_log(), ast_openstream(), ast_openvstream(), ast_playstream(), ast_verbose(), LOG_DEBUG, LOG_WARNING, ast_channel::nativeformats, option_verbose, and VERBOSE_PREFIX_3.

Referenced by ast_app_getdata(), ast_app_getdata_full(), ast_app_getvoice(), ast_control_streamfile(), ast_say_character_str(), ast_say_character_str_full(), ast_say_digit_str(), ast_say_digit_str_full(), ast_say_phonetic_str(), and ast_say_phonetic_str_full().

00729 { 00730 struct ast_filestream *fs; 00731 struct ast_filestream *vfs; 00732 00733 fs = ast_openstream(chan, filename, preflang); 00734 vfs = ast_openvstream(chan, filename, preflang); 00735 if (vfs) 00736 ast_log(LOG_DEBUG, "Ooh, found a video stream, too\n"); 00737 if(fs){ 00738 if(ast_applystream(chan, fs)) 00739 return -1; 00740 if(vfs && ast_applystream(chan, vfs)) 00741 return -1; 00742 if(ast_playstream(fs)) 00743 return -1; 00744 if(vfs && ast_playstream(vfs)) 00745 return -1; 00746 #if 1 00747 if (option_verbose > 2) 00748 ast_verbose(VERBOSE_PREFIX_3 "Playing '%s' (language '%s')\n", filename, preflang ? preflang : "default"); 00749 #endif 00750 return 0; 00751 } 00752 ast_log(LOG_WARNING, "Unable to open %s (format %s): %s\n", filename, ast_getformatname(chan->nativeformats), strerror(errno)); 00753 return -1; 00754 }

long ast_tellstream struct ast_filestream fs  ) 
 

Tell where we are in a stream.

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

Definition at line 621 of file file.c.

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

00622 { 00623 return fs->fmt->tell(fs); 00624 }

int ast_truncstream struct ast_filestream fs  ) 
 

Trunc stream at current location.

Parameters:
ast_filestream fs Returns 0 for success, or -1 for error

Definition at line 616 of file file.c.

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

00617 { 00618 return fs->fmt->trunc(fs); 00619 }

char ast_waitstream struct ast_channel c,
char *  breakon
 

Waits for a stream to stop or digit to be pressed.

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

Definition at line 860 of file file.c.

References ast_channel::_softhangup, AST_CONTROL_ANSWER, AST_CONTROL_HANGUP, AST_CONTROL_RINGING, AST_FRAME_CONTROL, AST_FRAME_DTMF, ast_frfree(), ast_log(), ast_read(), ast_sched_runq(), ast_sched_wait(), ast_stopstream(), ast_waitfor(), LOG_DEBUG, LOG_WARNING, ast_channel::sched, ast_channel::stream, and ast_channel::timingfunc.

Referenced by ast_app_getvoice(), ast_readstring(), ast_say_character_str(), ast_say_digit_str(), and ast_say_phonetic_str().

00861 { 00862 /* XXX Maybe I should just front-end ast_waitstream_full ? XXX */ 00863 int res; 00864 struct ast_frame *fr; 00865 while(c->stream) { 00866 res = ast_sched_wait(c->sched); 00867 if ((res < 0) && !c->timingfunc) { 00868 ast_stopstream(c); 00869 break; 00870 } 00871 if (res < 0) 00872 res = 1000; 00873 res = ast_waitfor(c, res); 00874 if (res < 0) { 00875 ast_log(LOG_WARNING, "Select failed (%s)\n", strerror(errno)); 00876 return res; 00877 } else if (res > 0) { 00878 fr = ast_read(c); 00879 if (!fr) { 00880 #if 0 00881 ast_log(LOG_DEBUG, "Got hung up\n"); 00882 #endif 00883 return -1; 00884 } 00885 00886 switch(fr->frametype) { 00887 case AST_FRAME_DTMF: 00888 res = fr->subclass; 00889 if (strchr(breakon, res)) { 00890 ast_frfree(fr); 00891 return res; 00892 } 00893 break; 00894 case AST_FRAME_CONTROL: 00895 switch(fr->subclass) { 00896 case AST_CONTROL_HANGUP: 00897 ast_frfree(fr); 00898 return -1; 00899 case AST_CONTROL_RINGING: 00900 case AST_CONTROL_ANSWER: 00901 /* Unimportant */ 00902 break; 00903 default: 00904 ast_log(LOG_WARNING, "Unexpected control subclass '%d'\n", fr->subclass); 00905 } 00906 } 00907 /* Ignore */ 00908 ast_frfree(fr); 00909 } 00910 ast_sched_runq(c->sched); 00911 } 00912 return (c->_softhangup ? -1 : 0); 00913 }

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

Same as waitstream but allows stream to be forwarded or rewound.

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

Definition at line 915 of file file.c.

References ast_channel::_softhangup, AST_CONTROL_ANSWER, AST_CONTROL_HANGUP, AST_CONTROL_RINGING, AST_FRAME_CONTROL, AST_FRAME_DTMF, ast_frfree(), ast_log(), ast_read(), ast_sched_runq(), ast_sched_wait(), ast_stopstream(), ast_stream_fastforward(), ast_stream_rewind(), ast_waitfor(), LOG_DEBUG, LOG_WARNING, ast_channel::sched, ast_channel::stream, and ast_channel::timingfunc.

Referenced by ast_control_streamfile().

00916 { 00917 int res; 00918 struct ast_frame *fr; 00919 while(c->stream) { 00920 res = ast_sched_wait(c->sched); 00921 if ((res < 0) && !c->timingfunc) { 00922 ast_stopstream(c); 00923 break; 00924 } 00925 if (res < 0) 00926 res = 1000; 00927 res = ast_waitfor(c, res); 00928 if (res < 0) { 00929 ast_log(LOG_WARNING, "Select failed (%s)\n", strerror(errno)); 00930 return res; 00931 } else 00932 if (res > 0) { 00933 fr = ast_read(c); 00934 if (!fr) { 00935 #if 0 00936 ast_log(LOG_DEBUG, "Got hung up\n"); 00937 #endif 00938 return -1; 00939 } 00940 00941 switch(fr->frametype) { 00942 case AST_FRAME_DTMF: 00943 res = fr->subclass; 00944 if (strchr(forward,res)) { 00945 ast_stream_fastforward(c->stream, ms); 00946 } else if (strchr(rewind,res)) { 00947 ast_stream_rewind(c->stream, ms); 00948 } else if (strchr(breakon, res)) { 00949 ast_frfree(fr); 00950 return res; 00951 } 00952 break; 00953 case AST_FRAME_CONTROL: 00954 switch(fr->subclass) { 00955 case AST_CONTROL_HANGUP: 00956 ast_frfree(fr); 00957 return -1; 00958 case AST_CONTROL_RINGING: 00959 case AST_CONTROL_ANSWER: 00960 /* Unimportant */ 00961 break; 00962 default: 00963 ast_log(LOG_WARNING, "Unexpected control subclass '%d'\n", fr->subclass); 00964 } 00965 } 00966 /* Ignore */ 00967 ast_frfree(fr); 00968 } else 00969 ast_sched_runq(c->sched); 00970 00971 00972 } 00973 return (c->_softhangup ? -1 : 0); 00974 }

char ast_waitstream_full struct ast_channel c,
char *  breakon,
int  audiofd,
int  monfd
 

Definition at line 976 of file file.c.

References ast_channel::_softhangup, AST_CONTROL_ANSWER, AST_CONTROL_HANGUP, AST_CONTROL_RINGING, AST_FRAME_CONTROL, AST_FRAME_DTMF, AST_FRAME_VOICE, ast_frfree(), ast_log(), ast_read(), ast_sched_runq(), ast_sched_wait(), ast_stopstream(), ast_waitfor_nandfds(), LOG_DEBUG, LOG_WARNING, ast_channel::sched, ast_channel::stream, and ast_channel::timingfunc.

Referenced by ast_readstring_full(), ast_say_character_str_full(), ast_say_digit_str_full(), and ast_say_phonetic_str_full().

00977 { 00978 int res; 00979 int ms; 00980 int outfd; 00981 struct ast_frame *fr; 00982 struct ast_channel *rchan; 00983 00984 while(c->stream) { 00985 ms = ast_sched_wait(c->sched); 00986 if ((ms < 0) && !c->timingfunc) { 00987 ast_stopstream(c); 00988 break; 00989 } 00990 if (ms < 0) 00991 ms = 1000; 00992 rchan = ast_waitfor_nandfds(&c, 1, &cmdfd, (cmdfd > -1) ? 1 : 0, NULL, &outfd, &ms); 00993 if (!rchan && (outfd < 0) && (ms)) { 00994 ast_log(LOG_WARNING, "Wait failed (%s)\n", strerror(errno)); 00995 return -1; 00996 } else if (outfd > -1) { 00997 /* The FD we were watching has something waiting */ 00998 return 1; 00999 } else if (rchan) { 01000 fr = ast_read(c); 01001 if (!fr) { 01002 #if 0 01003 ast_log(LOG_DEBUG, "Got hung up\n"); 01004 #endif 01005 return -1; 01006 } 01007 01008 switch(fr->frametype) { 01009 case AST_FRAME_DTMF: 01010 res = fr->subclass; 01011 if (strchr(breakon, res)) { 01012 ast_frfree(fr); 01013 return res; 01014 } 01015 break; 01016 case AST_FRAME_CONTROL: 01017 switch(fr->subclass) { 01018 case AST_CONTROL_HANGUP: 01019 ast_frfree(fr); 01020 return -1; 01021 case AST_CONTROL_RINGING: 01022 case AST_CONTROL_ANSWER: 01023 /* Unimportant */ 01024 break; 01025 default: 01026 ast_log(LOG_WARNING, "Unexpected control subclass '%d'\n", fr->subclass); 01027 } 01028 case AST_FRAME_VOICE: 01029 /* Write audio if appropriate */ 01030 if (audiofd > -1) 01031 write(audiofd, fr->data, fr->datalen); 01032 } 01033 /* Ignore */ 01034 ast_frfree(fr); 01035 } 01036 ast_sched_runq(c->sched); 01037 01038 01039 } 01040 return (c->_softhangup ? -1 : 0); 01041 }

struct ast_filestream* ast_writefile char *  filename,
char *  type,
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
oflags output file flags
check (unimplemented, hence negligible)
mode Open mode Create an outgoing file stream. oflags are flags for the open() command, and if check is non-zero, then it will not write a file if there are any files that start with that name and have an extension Please note, this is a blocking function. Program execution will not return until ast_waitstream completes it's execution. Returns a struct ast_filestream on success, NULL on failure

Definition at line 805 of file file.c.

References ast_log(), ast_mutex_lock, ast_mutex_unlock, free, LOG_WARNING, strdup, and type.

Referenced by ast_app_getvoice(), and ast_writestream().

00806 { 00807 int fd,myflags = 0; 00808 struct ast_format *f; 00809 struct ast_filestream *fs=NULL; 00810 char *fn; 00811 char *ext; 00812 if (ast_mutex_lock(&formatlock)) { 00813 ast_log(LOG_WARNING, "Unable to lock format list\n"); 00814 return NULL; 00815 } 00816 /* set the O_TRUNC flag if and only if there is no O_APPEND specified */ 00817 if (!(flags & O_APPEND)) 00818 myflags = O_TRUNC; 00819 00820 myflags |= O_WRONLY | O_CREAT; 00821 00822 f = formats; 00823 while(f) { 00824 if (exts_compare(f->exts, type)) { 00825 char *stringp=NULL; 00826 /* XXX Implement check XXX */ 00827 ext = strdup(f->exts); 00828 stringp=ext; 00829 ext = strsep(&stringp, "|"); 00830 fn = build_filename(filename, ext); 00831 fd = open(fn, flags | myflags, mode); 00832 if (fd >= 0) { 00833 errno = 0; 00834 if ((fs = f->rewrite(fd, comment))) { 00835 fs->trans = NULL; 00836 fs->fmt = f; 00837 fs->flags = flags; 00838 fs->mode = mode; 00839 fs->filename = strdup(filename); 00840 fs->vfs = NULL; 00841 } else { 00842 ast_log(LOG_WARNING, "Unable to rewrite %s\n", fn); 00843 close(fd); 00844 unlink(fn); 00845 } 00846 } else if (errno != EEXIST) 00847 ast_log(LOG_WARNING, "Unable to open file %s: %s\n", fn, strerror(errno)); 00848 free(fn); 00849 free(ext); 00850 break; 00851 } 00852 f = f->next; 00853 } 00854 ast_mutex_unlock(&formatlock); 00855 if (!f) 00856 ast_log(LOG_WARNING, "No such format '%s'\n", type); 00857 return fs; 00858 }

int ast_writestream struct ast_filestream fs,
struct ast_frame f
 

Writes a frame to a stream.

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

Definition at line 177 of file file.c.

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

Referenced by ast_read(), and ast_write().

00178 { 00179 struct ast_frame *trf; 00180 int res = -1; 00181 int alt=0; 00182 if (f->frametype == AST_FRAME_VIDEO) { 00183 if (fs->fmt->format < AST_FORMAT_MAX_AUDIO) { 00184 /* This is the audio portion. Call the video one... */ 00185 if (!fs->vfs && fs->filename) { 00186 /* XXX Support other video formats XXX */ 00187 char *type = "h263"; 00188 fs->vfs = ast_writefile(fs->filename, type, NULL, fs->flags, 0, fs->mode); 00189 ast_log(LOG_DEBUG, "Opened video output file\n"); 00190 } 00191 if (fs->vfs) 00192 return ast_writestream(fs->vfs, f); 00193 /* Ignore */ 00194 return 0; 00195 } else { 00196 /* Might / might not have mark set */ 00197 alt = 1; 00198 } 00199 } else if (f->frametype != AST_FRAME_VOICE) { 00200 ast_log(LOG_WARNING, "Tried to write non-voice frame\n"); 00201 return -1; 00202 } 00203 if (((fs->fmt->format | alt) & f->subclass) == f->subclass) { 00204 res = fs->fmt->write(fs, f); 00205 if (res < 0) 00206 ast_log(LOG_WARNING, "Natural write failed\n"); 00207 if (res > 0) 00208 ast_log(LOG_WARNING, "Huh??\n"); 00209 return res; 00210 } else { 00211 /* XXX If they try to send us a type of frame that isn't the normal frame, and isn't 00212 the one we've setup a translator for, we do the "wrong thing" XXX */ 00213 if (fs->trans && (f->subclass != fs->lastwriteformat)) { 00214 ast_translator_free_path(fs->trans); 00215 fs->trans = NULL; 00216 } 00217 if (!fs->trans) 00218 fs->trans = ast_translator_build_path(fs->fmt->format, f->subclass); 00219 if (!fs->trans) 00220 ast_log(LOG_WARNING, "Unable to translate to format %s, source format %s\n", fs->fmt->name, ast_getformatname(f->subclass)); 00221 else { 00222 fs->lastwriteformat = f->subclass; 00223 res = 0; 00224 /* Get the translated frame but don't consume the original in case they're using it on another stream */ 00225 trf = ast_translate(fs->trans, f, 0); 00226 if (trf) { 00227 res = fs->fmt->write(fs, trf); 00228 if (res) 00229 ast_log(LOG_WARNING, "Translated frame write failed\n"); 00230 } else 00231 res = 0; 00232 } 00233 return res; 00234 } 00235 }


Generated on Sat Jun 12 16:41:33 2004 for Asterisk by doxygen 1.3.7