file.h

Go to the documentation of this file.
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 by the channel.
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 the value of the control frame if it was interrupted by some other party,
00176  * \retval -1 on error.
00177  */
00178 int ast_waitstream_fr(struct ast_channel *c, const char *breakon, const char *forward, const char *rewind, int ms);
00179 
00180 /*!
00181  * \brief Same as waitstream_fr but allows a callback to be alerted when a user
00182  * fastforwards or rewinds the file.
00183  * \param c channel to waitstream on
00184  * \param breakon string of DTMF digits to break upon
00185  * \param forward DTMF digit to fast forward upon
00186  * \param rewind DTMF digit to rewind upon
00187  * \param ms How many milliseconds to skip forward/back
00188  * \param cb to call when rewind or fastfoward occurs.
00189  * Begins playback of a stream...
00190  * Wait for a stream to stop or for any one of a given digit to arrive,
00191  * \retval 0 if the stream finishes.
00192  * \retval the character if it was interrupted,
00193  * \retval the value of the control frame if it was interrupted by some other party,
00194  * \retval -1 on error.
00195  */
00196 int ast_waitstream_fr_w_cb(struct ast_channel *c,
00197    const char *breakon,
00198    const char *forward,
00199    const char *rewind,
00200    int ms,
00201    ast_waitstream_fr_cb cb);
00202 
00203 /*!
00204  * Same as waitstream, but with audio output to fd and monitored fd checking.  
00205  *
00206  * \return 1 if monfd is ready for reading 
00207  */
00208 int ast_waitstream_full(struct ast_channel *c, const char *breakon, int audiofd, int monfd);
00209 
00210 /*! 
00211  * \brief Starts reading from a file
00212  * \param filename the name of the file to read from
00213  * \param type format of file you wish to read from
00214  * \param comment comment to go with
00215  * \param flags file flags
00216  * \param check (unimplemented, hence negligible)
00217  * \param mode Open mode
00218  * Open an incoming file stream.  flags are flags for the open() command, and 
00219  * if check is non-zero, then it will not read a file if there are any files that 
00220  * start with that name and have an extension
00221  * Please note, this is a blocking function.  Program execution will not return until ast_waitstream completes it's execution.
00222  * \retval a struct ast_filestream on success.
00223  * \retval NULL on failure.
00224  */
00225 struct ast_filestream *ast_readfile(const char *filename, const char *type, const char *comment, int flags, int check, mode_t mode);
00226 
00227 /*! 
00228  * \brief Starts writing a file 
00229  * \param filename the name of the file to write to
00230  * \param type format of file you wish to write out to
00231  * \param comment comment to go with
00232  * \param flags output file flags
00233  * \param check (unimplemented, hence negligible)
00234  * \param mode Open mode
00235  * Create an outgoing file stream.  oflags are flags for the open() command, and 
00236  * if check is non-zero, then it will not write a file if there are any files that 
00237  * start with that name and have an extension
00238  * Please note, this is a blocking function.  Program execution will not return until ast_waitstream completes it's execution.
00239  * \retval a struct ast_filestream on success.
00240  * \retval NULL on failure.
00241  */
00242 struct ast_filestream *ast_writefile(const char *filename, const char *type, const char *comment, int flags, int check, mode_t mode);
00243 
00244 /*! 
00245  * \brief Writes a frame to a stream 
00246  * \param fs filestream to write to
00247  * \param f frame to write to the filestream
00248  * Send a frame to a filestream -- note: does NOT free the frame, call ast_frfree manually
00249  * \retval 0 on success.
00250  * \retval -1 on failure.
00251  */
00252 int ast_writestream(struct ast_filestream *fs, struct ast_frame *f);
00253 
00254 /*! 
00255  * \brief Closes a stream 
00256  * \param f filestream to close
00257  * Close a playback or recording stream
00258  * \retval 0 on success.
00259  * \retval -1 on failure.
00260  */
00261 int ast_closestream(struct ast_filestream *f);
00262 
00263 /*! 
00264  * \brief Opens stream for use in seeking, playing 
00265  * \param chan channel to work with
00266  * \param filename to use
00267  * \param preflang prefered language to use
00268  * \retval a ast_filestream pointer if it opens the file.
00269  * \retval NULL on error.
00270  */
00271 struct ast_filestream *ast_openstream(struct ast_channel *chan, const char *filename, const char *preflang);
00272 
00273 /*! 
00274  * \brief Opens stream for use in seeking, playing 
00275  * \param chan channel to work with
00276  * \param filename to use
00277  * \param preflang prefered language to use
00278  * \param asis if set, don't clear generators
00279  * \retval a ast_filestream pointer if it opens the file.
00280  * \retval NULL on error.
00281  */
00282 struct ast_filestream *ast_openstream_full(struct ast_channel *chan, const char *filename, const char *preflang, int asis);
00283 /*! 
00284  * \brief Opens stream for use in seeking, playing 
00285  * \param chan channel to work with
00286  * \param filename to use
00287  * \param preflang prefered language to use
00288  * \retval a ast_filestream pointer if it opens the file.
00289  * \retval NULL on error.
00290  */
00291 struct ast_filestream *ast_openvstream(struct ast_channel *chan, const char *filename, const char *preflang);
00292 
00293 /*! 
00294  * \brief Applys a open stream to a channel. 
00295  * \param chan channel to work
00296  * \param s ast_filestream to apply
00297  * \retval 0 on success.
00298  * \retval -1 on failure.
00299  */
00300 int ast_applystream(struct ast_channel *chan, struct ast_filestream *s);
00301 
00302 /*! 
00303  * \brief Play a open stream on a channel. 
00304  * \param s filestream to play
00305  * \retval 0 on success.
00306  * \retval -1 on failure.
00307  */
00308 int ast_playstream(struct ast_filestream *s);
00309 
00310 /*! 
00311  * \brief Seeks into stream 
00312  * \param fs ast_filestream to perform seek on
00313  * \param sample_offset numbers of samples to seek
00314  * \param whence SEEK_SET, SEEK_CUR, SEEK_END 
00315  * \retval 0 on success.
00316  * \retval -1 on failure.
00317  */
00318 int ast_seekstream(struct ast_filestream *fs, off_t sample_offset, int whence);
00319 
00320 /*! 
00321  * \brief Trunc stream at current location 
00322  * \param fs filestream to act on
00323  * \retval 0 on success.
00324  * \retval -1 on failure.
00325  */
00326 int ast_truncstream(struct ast_filestream *fs);
00327 
00328 /*! 
00329  * \brief Fast forward stream ms 
00330  * \param fs filestream to act on
00331  * \param ms milliseconds to move
00332  * \retval 0 on success.
00333  * \retval -1 on failure.
00334  */
00335 int ast_stream_fastforward(struct ast_filestream *fs, off_t ms);
00336 
00337 /*! 
00338  * \brief Rewind stream ms 
00339  * \param fs filestream to act on
00340  * \param ms milliseconds to move
00341  * \retval 0 on success.
00342  * \retval -1 on failure.
00343  */
00344 int ast_stream_rewind(struct ast_filestream *fs, off_t ms);
00345 
00346 /*! 
00347  * \brief Tell where we are in a stream 
00348  * \param fs fs to act on
00349  * \return a long as a sample offset into stream
00350  */
00351 off_t ast_tellstream(struct ast_filestream *fs);
00352 
00353 /*!
00354  * \brief Return the sample rate of the stream's format
00355  * \param fs fs to act on
00356  * \return sample rate in Hz
00357  */
00358 int ast_ratestream(struct ast_filestream *fs);
00359 
00360 /*! 
00361  * \brief Read a frame from a filestream 
00362  * \param s ast_filestream to act on
00363  * \return a frame.
00364  * \retval NULL if read failed.
00365  */ 
00366 struct ast_frame *ast_readframe(struct ast_filestream *s);
00367 
00368 /*! Initialize file stuff */
00369 /*!
00370  * Initializes all the various file stuff.  Basically just registers the cli stuff
00371  * Returns 0 all the time
00372  */
00373 int ast_file_init(void);
00374 
00375 
00376 #define AST_RESERVED_POINTERS 20
00377 
00378 /*! Remove duplicate formats from a format string. */
00379 /*!
00380  * \param fmts a format string, this string will be modified
00381  * \retval NULL error
00382  * \return a pointer to the reduced format string, this is a pointer to fmts
00383  */
00384 char *ast_format_str_reduce(char *fmts);
00385 
00386 /*!
00387  * \brief Get the ast_format associated with the given file extension
00388  * \since 12
00389  *
00390  * \param file_ext The file extension for which to find the format
00391  *
00392  * \retval NULL if not found
00393  * \retval A pointer to the ast_format associated with this file extension
00394  */
00395 struct ast_format *ast_get_format_for_file_ext(const char *file_ext);
00396 
00397 #if defined(__cplusplus) || defined(c_plusplus)
00398 }
00399 #endif
00400 
00401 #endif /* _ASTERISK_FILE_H */

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