2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2005, Digium, Inc.
6 * Mark Spencer <markster@digium.com>
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
20 * \brief Generic File Format Support.
23 #ifndef _ASTERISK_FILE_H
24 #define _ASTERISK_FILE_H
27 #error You must include stdio.h before file.h!
30 #include "asterisk/channel.h"
31 #include "asterisk/frame.h"
34 #if defined(__cplusplus) || defined(c_plusplus)
39 /*! Convenient for waiting */
40 #define AST_DIGIT_ANY "0123456789#*ABCD"
41 #define AST_DIGIT_ANYNUM "0123456789"
43 /*! structure used for lock and refcount of format handlers.
44 * Should not be here, but this is a temporary workaround
45 * until we implement a more general mechanism.
46 * The format handler should include a pointer to
48 * As a trick, if usecnt is initialized with -1,
49 * ast_format_register will init the mutex for you.
51 struct ast_format_lock {
53 int usecnt; /* number of active clients */
57 * Each supported file format is described by the following fields.
58 * Not all are necessary, the support routine implement default
59 * values for some of them.
60 * A handler typically fills a structure initializing the desired
61 * fields, and then calls ast_format_register() with the (readonly)
62 * structure as an argument.
65 char name[80]; /*! Name of format */
66 char exts[80]; /*! Extensions (separated by | if more than one)
67 this format can read. First is assumed for writing (e.g. .mp3) */
68 int format; /*! Format of frames it uses/provides (one only) */
69 /*! Prepare an input stream for playback. Return 0 on success, -1 on error.
70 * The FILE is already open (in s->f) so this function only needs to perform
71 * any applicable validity checks on the file. If none is required, the
72 * function can be omitted.
74 int (*open)(struct ast_filestream *s);
75 /*! Prepare a stream for output, and comment it appropriately if applicable.
76 * Return 0 on success, -1 on error. Same as the open, the FILE is already
77 * open so the function just needs to prepare any header and other fields,
78 * if any. The function can be omitted if nothing is needed.
80 int (*rewrite)(struct ast_filestream *s, const char *comment);
81 /*! Write a frame to a channel */
82 int (*write)(struct ast_filestream *, struct ast_frame *);
83 /*! seek num samples into file, whence - like a normal seek but with offset in samples */
84 int (*seek)(struct ast_filestream *, off_t, int);
85 int (*trunc)(struct ast_filestream *fs); /*! trunc file to current position */
86 off_t (*tell)(struct ast_filestream *fs); /*! tell current position */
87 /*! Read the next frame from the filestream (if available) and report
88 * when to get next frame (in samples)
90 struct ast_frame * (*read)(struct ast_filestream *, int *whennext);
91 /*! Do any closing actions, if any. The descriptor and structure are closed
92 * and destroyed by the generic routines, so they must not be done here. */
93 void (*close)(struct ast_filestream *);
94 char * (*getcomment)(struct ast_filestream *); /*! Retrieve file comment */
96 AST_LIST_ENTRY(ast_format) list; /*! Link */
99 * If the handler needs a buffer (for read, typically)
100 * and/or a private descriptor, put here the
101 * required size (in bytes) and the support routine will allocate them
102 * for you, pointed by s->buf and s->private, respectively.
103 * When allocating a buffer, remember to leave AST_FRIENDLY_OFFSET
104 * spare bytes at the bginning.
106 int buf_size; /*! size of frame buffer, if any, aligned to 8 bytes. */
107 int desc_size; /*! size of private descriptor, if any */
109 struct module_symbols *module;
113 * This structure is allocated by file.c in one chunk,
114 * together with buf_size and desc_size bytes of memory
115 * to be used for private purposes (e.g. buffers etc.)
117 struct ast_filestream {
118 /*! Everybody reserves a block of AST_RESERVED_POINTERS pointers for us */
119 struct ast_format *fmt; /* need to write to the lock and usecnt */
124 /*! Video file stream */
125 struct ast_filestream *vfs;
126 /*! Transparently translate from another format -- just once */
127 struct ast_trans_pvt *trans;
128 struct ast_tranlator_pvt *tr;
131 struct ast_channel *owner;
133 struct ast_frame fr; /* frame produced by read, typically */
134 char *buf; /* buffer pointed to by ast_frame; */
135 void *private; /* pointer to private buffer */
138 #define SEEK_FORCECUR 10
140 /*! Register a new file format capability
141 * Adds a format to asterisk's format abilities.
142 * returns 0 on success, -1 on failure
144 int ast_format_register(const struct ast_format *f);
146 /*! Unregisters a file format */
148 * \param name the name of the format you wish to unregister
149 * Unregisters a format based on the name of the format.
150 * Returns 0 on success, -1 on failure to unregister
152 int ast_format_unregister(const char *name);
154 /*! Streams a file */
156 * \param c channel to stream the file to
157 * \param filename the name of the file you wish to stream, minus the extension
158 * \param preflang the preferred language you wish to have the file streamed to you in
159 * Prepares a channel for the streaming of a file. To start the stream, afterward do a ast_waitstream() on the channel
160 * Also, it will stop any existing streams on the channel.
161 * Returns 0 on success, or -1 on failure.
163 int ast_streamfile(struct ast_channel *c, const char *filename, const char *preflang);
165 /*! Stops a stream */
167 * \param c The channel you wish to stop playback on
168 * Stop playback of a stream
169 * Returns 0 regardless
171 int ast_stopstream(struct ast_channel *c);
173 /*! Checks for the existence of a given file */
175 * \param filename name of the file you wish to check, minus the extension
176 * \param fmt the format you wish to check (the extension)
177 * \param preflang (the preferred language you wisht to find the file in)
178 * See if a given file exists in a given format. If fmt is NULL, any format is accepted.
179 * Returns -1 if file does not exist, non-zero positive otherwise.
181 int ast_fileexists(const char *filename, const char *fmt, const char *preflang);
183 /*! Renames a file */
185 * \param oldname the name of the file you wish to act upon (minus the extension)
186 * \param newname the name you wish to rename the file to (minus the extension)
187 * \param fmt the format of the file
188 * Rename a given file in a given format, or if fmt is NULL, then do so for all
189 * Returns -1 on failure
191 int ast_filerename(const char *oldname, const char *newname, const char *fmt);
193 /*! Deletes a file */
195 * \param filename name of the file you wish to delete (minus the extension)
196 * \param fmt of the file
197 * Delete a given file in a given format, or if fmt is NULL, then do so for all
199 int ast_filedelete(const char *filename, const char *fmt);
203 * \param oldname name of the file you wish to copy (minus extension)
204 * \param newname name you wish the file to be copied to (minus extension)
205 * \param fmt the format of the file
206 * Copy a given file in a given format, or if fmt is NULL, then do so for all
208 int ast_filecopy(const char *oldname, const char *newname, const char *fmt);
210 /*! Waits for a stream to stop or digit to be pressed */
212 * \param c channel to waitstram on
213 * \param breakon string of DTMF digits to break upon
214 * Begins playback of a stream...
215 * Wait for a stream to stop or for any one of a given digit to arrive, Returns 0
216 * if the stream finishes, the character if it was interrupted, and -1 on error
218 int ast_waitstream(struct ast_channel *c, const char *breakon);
220 /*! Waits for a stream to stop or digit matching a valid one digit exten to be pressed */
222 * \param c channel to waitstram on
223 * \param context string of context to match digits to break upon
224 * Begins playback of a stream...
225 * Wait for a stream to stop or for any one of a valid extension digit to arrive, Returns 0
226 * if the stream finishes, the character if it was interrupted, and -1 on error
228 int ast_waitstream_exten(struct ast_channel *c, const char *context);
230 /*! Same as waitstream but allows stream to be forwarded or rewound */
232 * \param c channel to waitstram on
233 * \param breakon string of DTMF digits to break upon
234 * \param forward DTMF digit to fast forward upon
235 * \param rewind DTMF digit to rewind upon
236 * \param ms How many miliseconds to skip forward/back
237 * Begins playback of a stream...
238 * Wait for a stream to stop or for any one of a given digit to arrive, Returns 0
239 * if the stream finishes, the character if it was interrupted, and -1 on error
241 int ast_waitstream_fr(struct ast_channel *c, const char *breakon, const char *forward, const char *rewind, int ms);
243 /* Same as waitstream, but with audio output to fd and monitored fd checking. Returns
244 1 if monfd is ready for reading */
245 int ast_waitstream_full(struct ast_channel *c, const char *breakon, int audiofd, int monfd);
247 /*! Starts reading from a file */
249 * \param filename the name of the file to read from
250 * \param type format of file you wish to read from
251 * \param comment comment to go with
252 * \param flags file flags
253 * \param check (unimplemented, hence negligible)
254 * \param mode Open mode
255 * Open an incoming file stream. flags are flags for the open() command, and
256 * if check is non-zero, then it will not read a file if there are any files that
257 * start with that name and have an extension
258 * Please note, this is a blocking function. Program execution will not return until ast_waitstream completes it's execution.
259 * Returns a struct ast_filestream on success, NULL on failure
261 struct ast_filestream *ast_readfile(const char *filename, const char *type, const char *comment, int flags, int check, mode_t mode);
263 /*! Starts writing a file */
265 * \param filename the name of the file to write to
266 * \param type format of file you wish to write out to
267 * \param comment comment to go with
268 * \param flags output file flags
269 * \param check (unimplemented, hence negligible)
270 * \param mode Open mode
271 * Create an outgoing file stream. oflags are flags for the open() command, and
272 * if check is non-zero, then it will not write a file if there are any files that
273 * start with that name and have an extension
274 * Please note, this is a blocking function. Program execution will not return until ast_waitstream completes it's execution.
275 * Returns a struct ast_filestream on success, NULL on failure
277 struct ast_filestream *ast_writefile(const char *filename, const char *type, const char *comment, int flags, int check, mode_t mode);
279 /*! Writes a frame to a stream */
281 * \param fs filestream to write to
282 * \param f frame to write to the filestream
283 * Send a frame to a filestream -- note: does NOT free the frame, call ast_frfree manually
284 * Returns 0 on success, -1 on failure.
286 int ast_writestream(struct ast_filestream *fs, struct ast_frame *f);
288 /*! Closes a stream */
290 * \param f filestream to close
291 * Close a playback or recording stream
292 * Returns 0 on success, -1 on failure
294 int ast_closestream(struct ast_filestream *f);
296 /*! Opens stream for use in seeking, playing */
298 * \param chan channel to work with
299 * \param filename to use
300 * \param preflang prefered language to use
301 * Returns a ast_filestream pointer if it opens the file, NULL on error
303 struct ast_filestream *ast_openstream(struct ast_channel *chan, const char *filename, const char *preflang);
305 /*! Opens stream for use in seeking, playing */
307 * \param chan channel to work with
308 * \param filename to use
309 * \param preflang prefered language to use
310 * \param asis if set, don't clear generators
311 * Returns a ast_filestream pointer if it opens the file, NULL on error
313 struct ast_filestream *ast_openstream_full(struct ast_channel *chan, const char *filename, const char *preflang, int asis);
314 /*! Opens stream for use in seeking, playing */
316 * \param chan channel to work with
317 * \param filename to use
318 * \param preflang prefered language to use
319 * Returns a ast_filestream pointer if it opens the file, NULL on error
321 struct ast_filestream *ast_openvstream(struct ast_channel *chan, const char *filename, const char *preflang);
323 /*! Applys a open stream to a channel. */
325 * \param chan channel to work
326 * \param s ast_filestream to apply
327 * Returns 0 for success, -1 on failure
329 int ast_applystream(struct ast_channel *chan, struct ast_filestream *s);
331 /*! play a open stream on a channel. */
333 * \param s filestream to play
334 * Returns 0 for success, -1 on failure
336 int ast_playstream(struct ast_filestream *s);
338 /*! Seeks into stream */
340 * \param fs ast_filestream to perform seek on
341 * \param sample_offset numbers of samples to seek
342 * \param whence SEEK_SET, SEEK_CUR, SEEK_END
343 * Returns 0 for success, or -1 for error
345 int ast_seekstream(struct ast_filestream *fs, off_t sample_offset, int whence);
347 /*! Trunc stream at current location */
349 * \param fs filestream to act on
350 * Returns 0 for success, or -1 for error
352 int ast_truncstream(struct ast_filestream *fs);
354 /*! Fast forward stream ms */
356 * \param fs filestream to act on
357 * \param ms milliseconds to move
358 * Returns 0 for success, or -1 for error
360 int ast_stream_fastforward(struct ast_filestream *fs, off_t ms);
362 /*! Rewind stream ms */
364 * \param fs filestream to act on
365 * \param ms milliseconds to move
366 * Returns 0 for success, or -1 for error
368 int ast_stream_rewind(struct ast_filestream *fs, off_t ms);
370 /*! Tell where we are in a stream */
372 * \param fs fs to act on
373 * Returns a long as a sample offset into stream
375 off_t ast_tellstream(struct ast_filestream *fs);
377 /*! Read a frame from a filestream */
379 * \param s ast_filestream to act on
380 * Returns a frame or NULL if read failed
382 struct ast_frame *ast_readframe(struct ast_filestream *s);
384 /*! Initialize file stuff */
386 * Initializes all the various file stuff. Basically just registers the cli stuff
387 * Returns 0 all the time
389 extern int ast_file_init(void);
392 #define AST_RESERVED_POINTERS 20
394 #if defined(__cplusplus) || defined(c_plusplus)
398 #endif /* _ASTERISK_FILE_H */