00001 /* 00002 * Asterisk -- An open source telephony toolkit. 00003 * 00004 * Copyright (C) 1999 - 2006, Digium, Inc. 00005 * 00006 * Mark Spencer <markster@digium.com> 00007 * 00008 * See http://www.asterisk.org for more information about 00009 * the Asterisk project. Please do not directly contact 00010 * any of the maintainers of this project for assistance; 00011 * the project provides a web site, mailing lists and IRC 00012 * channels for your use. 00013 * 00014 * This program is free software, distributed under the terms of 00015 * the GNU General Public License Version 2. See the LICENSE file 00016 * at the top of the source tree. 00017 */ 00018 00019 /*! \file 00020 * \brief Generic File Format Support. 00021 * Should be included by clients of the file handling routines. 00022 * File service providers should instead include mod_format.h 00023 */ 00024 00025 #ifndef _ASTERISK_FILE_H 00026 #define _ASTERISK_FILE_H 00027 00028 #ifdef HAVE_FCNTL_H 00029 #include <fcntl.h> 00030 #endif 00031 00032 #ifdef HAVE_MMAP 00033 #include <sys/mman.h> 00034 #endif 00035 00036 #if defined(__cplusplus) || defined(c_plusplus) 00037 extern "C" { 00038 #endif 00039 00040 struct ast_filestream; 00041 struct ast_format; 00042 00043 /*! The maximum number of formats we expect to see in a format string */ 00044 #define AST_MAX_FORMATS 10 00045 00046 /*! Convenient for waiting */ 00047 #define AST_DIGIT_NONE "" 00048 #define AST_DIGIT_ANY "0123456789#*ABCD" 00049 #define AST_DIGIT_ANYNUM "0123456789" 00050 00051 #define SEEK_FORCECUR 10 00052 00053 /*! The type of event associated with a ast_waitstream_fr_cb invocation */ 00054 enum ast_waitstream_fr_cb_values { 00055 AST_WAITSTREAM_CB_REWIND = 1, 00056 AST_WAITSTREAM_CB_FASTFORWARD, 00057 AST_WAITSTREAM_CB_START 00058 }; 00059 00060 /*! 00061 * \brief callback used during dtmf controlled file playback to indicate 00062 * location of playback in a file after rewinding or fastfowarding 00063 * a file. 00064 */ 00065 typedef void (ast_waitstream_fr_cb)(struct ast_channel *chan, long ms, enum ast_waitstream_fr_cb_values val); 00066 00067 /*! 00068 * \brief Streams a file 00069 * \param c channel to stream the file to 00070 * \param filename the name of the file you wish to stream, minus the extension 00071 * \param preflang the preferred language you wish to have the file streamed to you in 00072 * Prepares a channel for the streaming of a file. To start the stream, afterward do a ast_waitstream() on the channel 00073 * Also, it will stop any existing streams on the channel. 00074 * \retval 0 on success. 00075 * \retval -1 on failure. 00076 */ 00077 int ast_streamfile(struct ast_channel *c, const char *filename, const char *preflang); 00078 00079 /*! 00080 * \brief stream file until digit 00081 * If the file name is non-empty, try to play it. 00082 * \note If digits == "" then we can simply check for non-zero. 00083 * \return 0 if success. 00084 * \retval -1 if error. 00085 * \retval digit if interrupted by a digit. 00086 */ 00087 int ast_stream_and_wait(struct ast_channel *chan, const char *file, const char *digits); 00088 00089 /*! 00090 * \brief Stops a stream 00091 * 00092 * \param c The channel you wish to stop playback on 00093 * 00094 * Stop playback of a stream 00095 * 00096 * \retval 0 always 00097 * 00098 * \note The channel does not need to be locked before calling this function. 00099 */ 00100 int ast_stopstream(struct ast_channel *c); 00101 00102 /*! 00103 * \brief Checks for the existence of a given file 00104 * \param filename name of the file you wish to check, minus the extension 00105 * \param fmt the format you wish to check (the extension) 00106 * \param preflang (the preferred language you wisht to find the file in) 00107 * See if a given file exists in a given format. If fmt is NULL, any format is accepted. 00108 * \retval 0, false. The file does not exist 00109 * \retval 1, true. The file does exist. 00110 */ 00111 int ast_fileexists(const char *filename, const char *fmt, const char *preflang); 00112 00113 /*! 00114 * \brief Renames a file 00115 * \param oldname the name of the file you wish to act upon (minus the extension) 00116 * \param newname the name you wish to rename the file to (minus the extension) 00117 * \param fmt the format of the file 00118 * Rename a given file in a given format, or if fmt is NULL, then do so for all 00119 * \return -1 on failure 00120 */ 00121 int ast_filerename(const char *oldname, const char *newname, const char *fmt); 00122 00123 /*! 00124 * \brief Deletes a file 00125 * \param filename name of the file you wish to delete (minus the extension) 00126 * \param fmt of the file 00127 * Delete a given file in a given format, or if fmt is NULL, then do so for all 00128 */ 00129 int ast_filedelete(const char *filename, const char *fmt); 00130 00131 /*! 00132 * \brief Copies a file 00133 * \param oldname name of the file you wish to copy (minus extension) 00134 * \param newname name you wish the file to be copied to (minus extension) 00135 * \param fmt the format of the file 00136 * Copy a given file in a given format, or if fmt is NULL, then do so for all 00137 */ 00138 int ast_filecopy(const char *oldname, const char *newname, const char *fmt); 00139 00140 /*! 00141 * \brief Waits for a stream to stop or digit to be pressed 00142 * \param c channel to waitstream on 00143 * \param breakon string of DTMF digits to break upon 00144 * Begins playback of a stream... 00145 * Wait for a stream to stop or for any one of a given digit to arrive, 00146 * \retval 0 if the stream finishes 00147 * \retval the character if it was interrupted, 00148 * \retval -1 on error 00149 */ 00150 int ast_waitstream(struct ast_channel *c, const char *breakon); 00151 00152 /*! 00153 * \brief Waits for a stream to stop or digit matching a valid one digit exten to be pressed 00154 * \param c channel to waitstream on 00155 * \param context string of context to match digits to break upon 00156 * Begins playback of a stream... 00157 * Wait for a stream to stop or for any one of a valid extension digit to arrive, 00158 * \retval 0 if the stream finishes. 00159 * \retval the character if it was interrupted. 00160 * \retval -1 on error. 00161 */ 00162 int ast_waitstream_exten(struct ast_channel *c, const char *context); 00163 00164 /*! 00165 * \brief Same as waitstream but allows stream to be forwarded or rewound 00166 * \param c channel to waitstream on 00167 * \param breakon string of DTMF digits to break upon 00168 * \param forward DTMF digit to fast forward upon 00169 * \param rewind DTMF digit to rewind upon 00170 * \param ms How many miliseconds to skip forward/back 00171 * Begins playback of a stream... 00172 * Wait for a stream to stop or for any one of a given digit to arrive, 00173 * \retval 0 if the stream finishes. 00174 * \retval the character if it was interrupted. 00175 * \retval -1 on error. 00176 */ 00177 int ast_waitstream_fr(struct ast_channel *c, const char *breakon, const char *forward, const char *rewind, int ms); 00178 00179 /*! 00180 * \brief Same as waitstream_fr but allows a callback to be alerted when a user 00181 * fastforwards or rewinds the file. 00182 * \param c channel to waitstream on 00183 * \param breakon string of DTMF digits to break upon 00184 * \param forward DTMF digit to fast forward upon 00185 * \param rewind DTMF digit to rewind upon 00186 * \param ms How many milliseconds to skip forward/back 00187 * \param cb to call when rewind or fastfoward occurs. 00188 * Begins playback of a stream... 00189 * Wait for a stream to stop or for any one of a given digit to arrive, 00190 * \retval 0 if the stream finishes. 00191 * \retval the character if it was interrupted. 00192 * \retval -1 on error. 00193 */ 00194 int ast_waitstream_fr_w_cb(struct ast_channel *c, 00195 const char *breakon, 00196 const char *forward, 00197 const char *rewind, 00198 int ms, 00199 ast_waitstream_fr_cb cb); 00200 00201 /*! 00202 * Same as waitstream, but with audio output to fd and monitored fd checking. 00203 * 00204 * \return 1 if monfd is ready for reading 00205 */ 00206 int ast_waitstream_full(struct ast_channel *c, const char *breakon, int audiofd, int monfd); 00207 00208 /*! 00209 * \brief Starts reading from a file 00210 * \param filename the name of the file to read from 00211 * \param type format of file you wish to read from 00212 * \param comment comment to go with 00213 * \param flags file flags 00214 * \param check (unimplemented, hence negligible) 00215 * \param mode Open mode 00216 * Open an incoming file stream. flags are flags for the open() command, and 00217 * if check is non-zero, then it will not read a file if there are any files that 00218 * start with that name and have an extension 00219 * Please note, this is a blocking function. Program execution will not return until ast_waitstream completes it's execution. 00220 * \retval a struct ast_filestream on success. 00221 * \retval NULL on failure. 00222 */ 00223 struct ast_filestream *ast_readfile(const char *filename, const char *type, const char *comment, int flags, int check, mode_t mode); 00224 00225 /*! 00226 * \brief Starts writing a file 00227 * \param filename the name of the file to write to 00228 * \param type format of file you wish to write out to 00229 * \param comment comment to go with 00230 * \param flags output file flags 00231 * \param check (unimplemented, hence negligible) 00232 * \param mode Open mode 00233 * Create an outgoing file stream. oflags are flags for the open() command, and 00234 * if check is non-zero, then it will not write a file if there are any files that 00235 * start with that name and have an extension 00236 * Please note, this is a blocking function. Program execution will not return until ast_waitstream completes it's execution. 00237 * \retval a struct ast_filestream on success. 00238 * \retval NULL on failure. 00239 */ 00240 struct ast_filestream *ast_writefile(const char *filename, const char *type, const char *comment, int flags, int check, mode_t mode); 00241 00242 /*! 00243 * \brief Writes a frame to a stream 00244 * \param fs filestream to write to 00245 * \param f frame to write to the filestream 00246 * Send a frame to a filestream -- note: does NOT free the frame, call ast_frfree manually 00247 * \retval 0 on success. 00248 * \retval -1 on failure. 00249 */ 00250 int ast_writestream(struct ast_filestream *fs, struct ast_frame *f); 00251 00252 /*! 00253 * \brief Closes a stream 00254 * \param f filestream to close 00255 * Close a playback or recording stream 00256 * \retval 0 on success. 00257 * \retval -1 on failure. 00258 */ 00259 int ast_closestream(struct ast_filestream *f); 00260 00261 /*! 00262 * \brief Opens stream for use in seeking, playing 00263 * \param chan channel to work with 00264 * \param filename to use 00265 * \param preflang prefered language to use 00266 * \retval a ast_filestream pointer if it opens the file. 00267 * \retval NULL on error. 00268 */ 00269 struct ast_filestream *ast_openstream(struct ast_channel *chan, const char *filename, const char *preflang); 00270 00271 /*! 00272 * \brief Opens stream for use in seeking, playing 00273 * \param chan channel to work with 00274 * \param filename to use 00275 * \param preflang prefered language to use 00276 * \param asis if set, don't clear generators 00277 * \retval a ast_filestream pointer if it opens the file. 00278 * \retval NULL on error. 00279 */ 00280 struct ast_filestream *ast_openstream_full(struct ast_channel *chan, const char *filename, const char *preflang, int asis); 00281 /*! 00282 * \brief Opens stream for use in seeking, playing 00283 * \param chan channel to work with 00284 * \param filename to use 00285 * \param preflang prefered language to use 00286 * \retval a ast_filestream pointer if it opens the file. 00287 * \retval NULL on error. 00288 */ 00289 struct ast_filestream *ast_openvstream(struct ast_channel *chan, const char *filename, const char *preflang); 00290 00291 /*! 00292 * \brief Applys a open stream to a channel. 00293 * \param chan channel to work 00294 * \param s ast_filestream to apply 00295 * \retval 0 on success. 00296 * \retval -1 on failure. 00297 */ 00298 int ast_applystream(struct ast_channel *chan, struct ast_filestream *s); 00299 00300 /*! 00301 * \brief Play a open stream on a channel. 00302 * \param s filestream to play 00303 * \retval 0 on success. 00304 * \retval -1 on failure. 00305 */ 00306 int ast_playstream(struct ast_filestream *s); 00307 00308 /*! 00309 * \brief Seeks into stream 00310 * \param fs ast_filestream to perform seek on 00311 * \param sample_offset numbers of samples to seek 00312 * \param whence SEEK_SET, SEEK_CUR, SEEK_END 00313 * \retval 0 on success. 00314 * \retval -1 on failure. 00315 */ 00316 int ast_seekstream(struct ast_filestream *fs, off_t sample_offset, int whence); 00317 00318 /*! 00319 * \brief Trunc stream at current location 00320 * \param fs filestream to act on 00321 * \retval 0 on success. 00322 * \retval -1 on failure. 00323 */ 00324 int ast_truncstream(struct ast_filestream *fs); 00325 00326 /*! 00327 * \brief Fast forward stream ms 00328 * \param fs filestream to act on 00329 * \param ms milliseconds to move 00330 * \retval 0 on success. 00331 * \retval -1 on failure. 00332 */ 00333 int ast_stream_fastforward(struct ast_filestream *fs, off_t ms); 00334 00335 /*! 00336 * \brief Rewind stream ms 00337 * \param fs filestream to act on 00338 * \param ms milliseconds to move 00339 * \retval 0 on success. 00340 * \retval -1 on failure. 00341 */ 00342 int ast_stream_rewind(struct ast_filestream *fs, off_t ms); 00343 00344 /*! 00345 * \brief Tell where we are in a stream 00346 * \param fs fs to act on 00347 * \return a long as a sample offset into stream 00348 */ 00349 off_t ast_tellstream(struct ast_filestream *fs); 00350 00351 /*! 00352 * \brief Read a frame from a filestream 00353 * \param s ast_filestream to act on 00354 * \return a frame. 00355 * \retval NULL if read failed. 00356 */ 00357 struct ast_frame *ast_readframe(struct ast_filestream *s); 00358 00359 /*! Initialize file stuff */ 00360 /*! 00361 * Initializes all the various file stuff. Basically just registers the cli stuff 00362 * Returns 0 all the time 00363 */ 00364 int ast_file_init(void); 00365 00366 00367 #define AST_RESERVED_POINTERS 20 00368 00369 /*! Remove duplicate formats from a format string. */ 00370 /*! 00371 * \param fmts a format string, this string will be modified 00372 * \retval NULL error 00373 * \return a pointer to the reduced format string, this is a pointer to fmts 00374 */ 00375 char *ast_format_str_reduce(char *fmts); 00376 00377 #if defined(__cplusplus) || defined(c_plusplus) 00378 } 00379 #endif 00380 00381 #endif /* _ASTERISK_FILE_H */