Codename Pineapple

Home page | Mailing list | Docs

Last updated: Sat Feb 3 05:00:48 2007

Asterisk developer's documentation :: Codename Pineapple


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  */
00022 
00023 #ifndef _ASTERISK_FILE_H
00024 #define _ASTERISK_FILE_H
00025 
00026 #ifndef stdin
00027 #error You must include stdio.h before file.h!
00028 #endif /* !stdin */
00029 
00030 #include "asterisk/channel.h"
00031 #include "asterisk/frame.h"
00032 #include <fcntl.h>
00033 
00034 #if defined(__cplusplus) || defined(c_plusplus)
00035 extern "C" {
00036 #endif
00037 
00038 
00039 /*! Convenient for waiting */
00040 #define AST_DIGIT_ANY "0123456789#*ABCD"
00041 #define AST_DIGIT_ANYNUM "0123456789"
00042 
00043 /*! structure used for lock and refcount of format handlers.
00044  * Should not be here, but this is a temporary workaround
00045  * until we implement a more general mechanism.
00046  * The format handler should include a pointer to
00047  * this structure.
00048  * As a trick, if usecnt is initialized with -1,
00049  * ast_format_register will init the mutex for you.
00050  */
00051 struct ast_format_lock {
00052    ast_mutex_t lock;
00053    int usecnt; /* number of active clients */
00054 };
00055 
00056 /*!
00057  * Each supported file format is described by the following fields.
00058  * Not all are necessary, the support routine implement default
00059  * values for some of them.
00060  * A handler typically fills a structure initializing the desired
00061  * fields, and then calls ast_format_register() with the (readonly)
00062  * structure as an argument.
00063  */
00064 struct ast_format {
00065    char name[80];    /*! Name of format */
00066    char exts[80];    /*! Extensions (separated by | if more than one) 
00067                this format can read.  First is assumed for writing (e.g. .mp3) */
00068    int format;    /*! Format of frames it uses/provides (one only) */
00069    /*! Prepare an input stream for playback. Return 0 on success, -1 on error.
00070     * The FILE is already open (in s->f) so this function only needs to perform
00071     * any applicable validity checks on the file. If none is required, the
00072     * function can be omitted.
00073     */
00074    int (*open)(struct ast_filestream *s);
00075    /*! Prepare a stream for output, and comment it appropriately if applicable.
00076     *  Return 0 on success, -1 on error. Same as the open, the FILE is already
00077     * open so the function just needs to prepare any header and other fields,
00078     * if any. The function can be omitted if nothing is needed.
00079     */
00080    int (*rewrite)(struct ast_filestream *s, const char *comment);
00081    /*! Write a frame to a channel */
00082    int (*write)(struct ast_filestream *, struct ast_frame *);
00083    /*! seek num samples into file, whence - like a normal seek but with offset in samples */
00084    int (*seek)(struct ast_filestream *, off_t, int);
00085    int (*trunc)(struct ast_filestream *fs);  /*! trunc file to current position */
00086    off_t (*tell)(struct ast_filestream *fs); /*! tell current position */
00087    /*! Read the next frame from the filestream (if available) and report
00088     * when to get next frame (in samples)
00089     */
00090    struct ast_frame * (*read)(struct ast_filestream *, int *whennext);
00091    /*! Do any closing actions, if any. The descriptor and structure are closed
00092     * and destroyed by the generic routines, so they must not be done here. */
00093    void (*close)(struct ast_filestream *);
00094    char * (*getcomment)(struct ast_filestream *);     /*! Retrieve file comment */
00095 
00096    AST_LIST_ENTRY(ast_format) list;       /*! Link */
00097 
00098    /*!
00099     * If the handler needs a buffer (for read, typically)
00100     * and/or a private descriptor, put here the
00101     * required size (in bytes) and the support routine will allocate them
00102     * for you, pointed by s->buf and s->private, respectively.
00103     * When allocating a buffer, remember to leave AST_FRIENDLY_OFFSET
00104     * spare bytes at the bginning.
00105     */
00106    int buf_size;        /*! size of frame buffer, if any, aligned to 8 bytes. */
00107    int desc_size;       /*! size of private descriptor, if any */
00108 
00109    struct ast_module *module;
00110 };
00111 
00112 /*
00113  * This structure is allocated by file.c in one chunk,
00114  * together with buf_size and desc_size bytes of memory
00115  * to be used for private purposes (e.g. buffers etc.)
00116  */
00117 struct ast_filestream {
00118    /*! Everybody reserves a block of AST_RESERVED_POINTERS pointers for us */
00119    struct ast_format *fmt; /* need to write to the lock and usecnt */
00120    int flags;
00121    mode_t mode;
00122    char *filename;
00123    char *realfilename;
00124    /*! Video file stream */
00125    struct ast_filestream *vfs;
00126    /*! Transparently translate from another format -- just once */
00127    struct ast_trans_pvt *trans;
00128    struct ast_tranlator_pvt *tr;
00129    int lastwriteformat;
00130    int lasttimeout;
00131    struct ast_channel *owner;
00132    FILE *f;
00133    struct ast_frame fr; /* frame produced by read, typically */
00134    char *buf;     /* buffer pointed to by ast_frame; */
00135    void *private; /* pointer to private buffer */
00136 };
00137 
00138 #define SEEK_FORCECUR   10
00139    
00140 /*! Register a new file format capability
00141  * Adds a format to Asterisk's format abilities.
00142  * returns 0 on success, -1 on failure
00143  */
00144 int __ast_format_register(const struct ast_format *f, struct ast_module *mod);
00145 #define ast_format_register(f) __ast_format_register(f, ast_module_info->self)
00146 
00147 /*! Unregisters a file format */
00148 /*!
00149  * \param name the name of the format you wish to unregister
00150  * Unregisters a format based on the name of the format.
00151  * Returns 0 on success, -1 on failure to unregister
00152  */
00153 int ast_format_unregister(const char *name);
00154 
00155 /*! Streams a file */
00156 /*!
00157  * \param c channel to stream the file to
00158  * \param filename the name of the file you wish to stream, minus the extension
00159  * \param preflang the preferred language you wish to have the file streamed to you in
00160  * Prepares a channel for the streaming of a file.  To start the stream, afterward do a ast_waitstream() on the channel
00161  * Also, it will stop any existing streams on the channel.
00162  * Returns 0 on success, or -1 on failure.
00163  */
00164 int ast_streamfile(struct ast_channel *c, const char *filename, const char *preflang);
00165 
00166 /*
00167  * if the file name is non-empty, try to play it.
00168  * Return 0 if success, -1 if error, digit if interrupted by a digit.
00169  * If digits == "" then we can simply check for non-zero.
00170  */
00171 int ast_stream_and_wait(struct ast_channel *chan, const char *file, const char *digits);
00172 
00173 /*! Stops a stream */
00174 /*!
00175  * \param c The channel you wish to stop playback on
00176  * Stop playback of a stream 
00177  * Returns 0 regardless
00178  */
00179 int ast_stopstream(struct ast_channel *c);
00180 
00181 /*! Checks for the existence of a given file */
00182 /*!
00183  * \param filename name of the file you wish to check, minus the extension
00184  * \param fmt the format you wish to check (the extension)
00185  * \param preflang (the preferred language you wisht to find the file in)
00186  * See if a given file exists in a given format.  If fmt is NULL,  any format is accepted.
00187  * Returns -1 if file does not exist, non-zero positive otherwise.
00188  */
00189 int ast_fileexists(const char *filename, const char *fmt, const char *preflang);
00190 
00191 /*! Renames a file */
00192 /*! 
00193  * \param oldname the name of the file you wish to act upon (minus the extension)
00194  * \param newname the name you wish to rename the file to (minus the extension)
00195  * \param fmt the format of the file
00196  * Rename a given file in a given format, or if fmt is NULL, then do so for all 
00197  * Returns -1 on failure
00198  */
00199 int ast_filerename(const char *oldname, const char *newname, const char *fmt);
00200 
00201 /*! Deletes a file */
00202 /*! 
00203  * \param filename name of the file you wish to delete (minus the extension)
00204  * \param fmt of the file
00205  * Delete a given file in a given format, or if fmt is NULL, then do so for all 
00206  */
00207 int ast_filedelete(const char *filename, const char *fmt);
00208 
00209 /*! Copies a file */
00210 /*!
00211  * \param oldname name of the file you wish to copy (minus extension)
00212  * \param newname name you wish the file to be copied to (minus extension)
00213  * \param fmt the format of the file
00214  * Copy a given file in a given format, or if fmt is NULL, then do so for all 
00215  */
00216 int ast_filecopy(const char *oldname, const char *newname, const char *fmt);
00217 
00218 /*! Waits for a stream to stop or digit to be pressed */
00219 /*!
00220  * \param c channel to waitstream on
00221  * \param breakon string of DTMF digits to break upon
00222  * Begins playback of a stream...
00223  * Wait for a stream to stop or for any one of a given digit to arrive,  Returns 0 
00224  * if the stream finishes, the character if it was interrupted, and -1 on error 
00225  */
00226 int ast_waitstream(struct ast_channel *c, const char *breakon);
00227 
00228 /*! Waits for a stream to stop or digit matching a valid one digit exten to be pressed */
00229 /*!
00230  * \param c channel to waitstream on
00231  * \param context string of context to match digits to break upon
00232  * Begins playback of a stream...
00233  * Wait for a stream to stop or for any one of a valid extension digit to arrive,  Returns 0 
00234  * if the stream finishes, the character if it was interrupted, and -1 on error 
00235  */
00236 int ast_waitstream_exten(struct ast_channel *c, const char *context);
00237 
00238 /*! Same as waitstream but allows stream to be forwarded or rewound */
00239 /*!
00240  * \param c channel to waitstream on
00241  * \param breakon string of DTMF digits to break upon
00242  * \param forward DTMF digit to fast forward upon
00243  * \param rewind DTMF digit to rewind upon
00244  * \param ms How many miliseconds to skip forward/back
00245  * Begins playback of a stream...
00246  * Wait for a stream to stop or for any one of a given digit to arrive,  Returns 0 
00247  * if the stream finishes, the character if it was interrupted, and -1 on error 
00248  */
00249 int ast_waitstream_fr(struct ast_channel *c, const char *breakon, const char *forward, const char *rewind, int ms);
00250 
00251 /* Same as waitstream, but with audio output to fd and monitored fd checking.  Returns
00252    1 if monfd is ready for reading */
00253 int ast_waitstream_full(struct ast_channel *c, const char *breakon, int audiofd, int monfd);
00254 
00255 /*! Starts reading from a file */
00256 /*!
00257  * \param filename the name of the file to read from
00258  * \param type format of file you wish to read from
00259  * \param comment comment to go with
00260  * \param flags file flags
00261  * \param check (unimplemented, hence negligible)
00262  * \param mode Open mode
00263  * Open an incoming file stream.  flags are flags for the open() command, and 
00264  * if check is non-zero, then it will not read a file if there are any files that 
00265  * start with that name and have an extension
00266  * Please note, this is a blocking function.  Program execution will not return until ast_waitstream completes it's execution.
00267  * Returns a struct ast_filestream on success, NULL on failure
00268  */
00269 struct ast_filestream *ast_readfile(const char *filename, const char *type, const char *comment, int flags, int check, mode_t mode);
00270 
00271 /*! Starts writing a file */
00272 /*!
00273  * \param filename the name of the file to write to
00274  * \param type format of file you wish to write out to
00275  * \param comment comment to go with
00276  * \param flags output file flags
00277  * \param check (unimplemented, hence negligible)
00278  * \param mode Open mode
00279  * Create an outgoing file stream.  oflags are flags for the open() command, and 
00280  * if check is non-zero, then it will not write a file if there are any files that 
00281  * start with that name and have an extension
00282  * Please note, this is a blocking function.  Program execution will not return until ast_waitstream completes it's execution.
00283  * Returns a struct ast_filestream on success, NULL on failure
00284  */
00285 struct ast_filestream *ast_writefile(const char *filename, const char *type, const char *comment, int flags, int check, mode_t mode);
00286 
00287 /*! Writes a frame to a stream */
00288 /*! 
00289  * \param fs filestream to write to
00290  * \param f frame to write to the filestream
00291  * Send a frame to a filestream -- note: does NOT free the frame, call ast_frfree manually
00292  * Returns 0 on success, -1 on failure.
00293  */
00294 int ast_writestream(struct ast_filestream *fs, struct ast_frame *f);
00295 
00296 /*! Closes a stream */
00297 /*!
00298  * \param f filestream to close
00299  * Close a playback or recording stream
00300  * Returns 0 on success, -1 on failure
00301  */
00302 int ast_closestream(struct ast_filestream *f);
00303 
00304 /*! Opens stream for use in seeking, playing */
00305 /*!
00306  * \param chan channel to work with
00307  * \param filename to use
00308  * \param preflang prefered language to use
00309  * Returns a ast_filestream pointer if it opens the file, NULL on error
00310  */
00311 struct ast_filestream *ast_openstream(struct ast_channel *chan, const char *filename, const char *preflang);
00312 
00313 /*! Opens stream for use in seeking, playing */
00314 /*!
00315  * \param chan channel to work with
00316  * \param filename to use
00317  * \param preflang prefered language to use
00318  * \param asis if set, don't clear generators
00319  * Returns a ast_filestream pointer if it opens the file, NULL on error
00320  */
00321 struct ast_filestream *ast_openstream_full(struct ast_channel *chan, const char *filename, const char *preflang, int asis);
00322 /*! Opens stream for use in seeking, playing */
00323 /*!
00324  * \param chan channel to work with
00325  * \param filename to use
00326  * \param preflang prefered language to use
00327  * Returns a ast_filestream pointer if it opens the file, NULL on error
00328  */
00329 struct ast_filestream *ast_openvstream(struct ast_channel *chan, const char *filename, const char *preflang);
00330 
00331 /*! Applys a open stream to a channel. */
00332 /*!
00333  * \param chan channel to work
00334  * \param s ast_filestream to apply
00335  * Returns 0 for success, -1 on failure
00336  */
00337 int ast_applystream(struct ast_channel *chan, struct ast_filestream *s);
00338 
00339 /*! play a open stream on a channel. */
00340 /*!
00341  * \param s filestream to play
00342  * Returns 0 for success, -1 on failure
00343  */
00344 int ast_playstream(struct ast_filestream *s);
00345 
00346 /*! Seeks into stream */
00347 /*!
00348  * \param fs ast_filestream to perform seek on
00349  * \param sample_offset numbers of samples to seek
00350  * \param whence SEEK_SET, SEEK_CUR, SEEK_END 
00351  * Returns 0 for success, or -1 for error
00352  */
00353 int ast_seekstream(struct ast_filestream *fs, off_t sample_offset, int whence);
00354 
00355 /*! Trunc stream at current location */
00356 /*!
00357  * \param fs filestream to act on
00358  * Returns 0 for success, or -1 for error
00359  */
00360 int ast_truncstream(struct ast_filestream *fs);
00361 
00362 /*! Fast forward stream ms */
00363 /*!
00364  * \param fs filestream to act on
00365  * \param ms milliseconds to move
00366  * Returns 0 for success, or -1 for error
00367  */
00368 int ast_stream_fastforward(struct ast_filestream *fs, off_t ms);
00369 
00370 /*! Rewind stream ms */
00371 /*!
00372  * \param fs filestream to act on
00373  * \param ms milliseconds to move
00374  * Returns 0 for success, or -1 for error
00375  */
00376 int ast_stream_rewind(struct ast_filestream *fs, off_t ms);
00377 
00378 /*! Tell where we are in a stream */
00379 /*!
00380  * \param fs fs to act on
00381  * Returns a long as a sample offset into stream
00382  */
00383 off_t ast_tellstream(struct ast_filestream *fs);
00384 
00385 /*! Read a frame from a filestream */
00386 /*!
00387  * \param s ast_filestream to act on
00388  * Returns a frame or NULL if read failed
00389  */ 
00390 struct ast_frame *ast_readframe(struct ast_filestream *s);
00391 
00392 /*! Initialize file stuff */
00393 /*!
00394  * Initializes all the various file stuff.  Basically just registers the cli stuff
00395  * Returns 0 all the time
00396  */
00397 int ast_file_init(void);
00398 
00399 
00400 #define AST_RESERVED_POINTERS 20
00401 
00402 #if defined(__cplusplus) || defined(c_plusplus)
00403 }
00404 #endif
00405 
00406 #endif /* _ASTERISK_FILE_H */

Asterisk is a trademark for Digium, inc.. | Edvina.net | Asterisk.org | This documentation was generated with Doxygen