2 * Asterisk -- A telephony toolkit for Linux.
4 * Generic File Format Support.
6 * Copyright (C) 1999, Mark Spencer
8 * Mark Spencer <markster@linux-support.net>
10 * This program is free software, distributed under the terms of
11 * the GNU General Public License
14 #ifndef _ASTERISK_FILE_H
15 #define _ASTERISK_FILE_H
17 #include "asterisk/channel.h"
18 #include "asterisk/frame.h"
22 #if defined(__cplusplus) || defined(c_plusplus)
27 /*! Convenient for waiting */
28 #define AST_DIGIT_ANY "0123456789#*ABCD"
29 #define AST_DIGIT_ANYNUM "0123456789"
31 #define SEEK_FORCECUR 10
33 /* Defined by individual formats. First item MUST be a
34 pointer for use by the stream manager */
35 struct ast_filestream;
37 /*! Registers a new file format */
38 /*! Register a new file format capability
39 * 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.
40 * returns 0 on success, -1 on failure
42 int ast_format_register(const char *name, const char *exts, int format,
43 struct ast_filestream * (*open)(int fd),
44 struct ast_filestream * (*rewrite)(int fd, const char *comment),
45 int (*write)(struct ast_filestream *, struct ast_frame *),
46 int (*seek)(struct ast_filestream *, long offset, int whence),
47 int (*trunc)(struct ast_filestream *),
48 long (*tell)(struct ast_filestream *),
49 struct ast_frame * (*read)(struct ast_filestream *, int *timetonext),
50 void (*close)(struct ast_filestream *),
51 char * (*getcomment)(struct ast_filestream *));
53 /*! Unregisters a file format */
55 * \param name the name of the format you wish to unregister
56 * Unregisters a format based on the name of the format.
57 * Returns 0 on success, -1 on failure to unregister
59 int ast_format_unregister(const char *name);
63 * \param c channel to stream the file to
64 * \param filename the name of the file you wish to stream, minus the extension
65 * \param preflang the preferred language you wish to have the file streamed to you in
66 * Prepares a channel for the streaming of a file. To start the stream, afterward do a ast_waitstream() on the channel
67 * Also, it will stop any existing streams on the channel.
68 * Returns 0 on success, or -1 on failure.
70 int ast_streamfile(struct ast_channel *c, const char *filename, const char *preflang);
74 * \param c The channel you wish to stop playback on
75 * Stop playback of a stream
76 * Returns 0 regardless
78 int ast_stopstream(struct ast_channel *c);
80 /*! Checks for the existence of a given file */
82 * \param filename name of the file you wish to check, minus the extension
83 * \param fmt the format you wish to check (the extension)
84 * \param preflang (the preferred language you wisht to find the file in)
85 * See if a given file exists in a given format. If fmt is NULL, any format is accepted.
86 * Returns -1 if file does not exist, non-zero positive otherwise.
88 int ast_fileexists(const char *filename, const char *fmt, const char *preflang);
92 * \param oldname the name of the file you wish to act upon (minus the extension)
93 * \param newname the name you wish to rename the file to (minus the extension)
94 * \param fmt the format of the file
95 * Rename a given file in a given format, or if fmt is NULL, then do so for all
96 * Returns -1 on failure
98 int ast_filerename(const char *oldname, const char *newname, const char *fmt);
100 /*! Deletes a file */
102 * \param filename name of the file you wish to delete (minus the extension)
103 * \param format of the file
104 * Delete a given file in a given format, or if fmt is NULL, then do so for all
106 int ast_filedelete(const char *filename, const char *fmt);
110 * \param oldname name of the file you wish to copy (minus extension)
111 * \param newname name you wish the file to be copied to (minus extension)
112 * \param fmt the format of the file
113 * Copy a given file in a given format, or if fmt is NULL, then do so for all
115 int ast_filecopy(const char *oldname, const char *newname, const char *fmt);
117 /*! Waits for a stream to stop or digit to be pressed */
119 * \param c channel to waitstram on
120 * \param breakon string of DTMF digits to break upon
121 * Begins playback of a stream...
122 * Wait for a stream to stop or for any one of a given digit to arrive, Returns 0
123 * if the stream finishes, the character if it was interrupted, and -1 on error
125 int ast_waitstream(struct ast_channel *c, const char *breakon);
127 /*! Waits for a stream to stop or digit matching a valid one digit exten to be pressed */
129 * \param c channel to waitstram on
130 * \param context string of context to match digits to break upon
131 * Begins playback of a stream...
132 * Wait for a stream to stop or for any one of a valid extension digit to arrive, Returns 0
133 * if the stream finishes, the character if it was interrupted, and -1 on error
135 int ast_waitstream_exten(struct ast_channel *c, const char *context);
137 /*! Same as waitstream but allows stream to be forwarded or rewound */
139 * \param c channel to waitstram on
140 * \param breakon string of DTMF digits to break upon
141 * \param forward DTMF digit to fast forward upon
142 * \param rewind DTMF digit to rewind upon
143 * \param ms How many miliseconds to skip forward/back
144 * Begins playback of a stream...
145 * Wait for a stream to stop or for any one of a given digit to arrive, Returns 0
146 * if the stream finishes, the character if it was interrupted, and -1 on error
148 int ast_waitstream_fr(struct ast_channel *c, const char *breakon, const char *forward, const char *rewind, int ms);
150 /* Same as waitstream, but with audio output to fd and monitored fd checking. Returns
151 1 if monfd is ready for reading */
152 int ast_waitstream_full(struct ast_channel *c, const char *breakon, int audiofd, int monfd);
154 /*! Starts reading from a file */
156 * \param filename the name of the file to read from
157 * \param type format of file you wish to read from
158 * \param comment comment to go with
159 * \param flags file flags
160 * \param check (unimplemented, hence negligible)
161 * \param mode Open mode
162 * Open an incoming file stream. flags are flags for the open() command, and
163 * if check is non-zero, then it will not read a file if there are any files that
164 * start with that name and have an extension
165 * Please note, this is a blocking function. Program execution will not return until ast_waitstream completes it's execution.
166 * Returns a struct ast_filestream on success, NULL on failure
168 struct ast_filestream *ast_readfile(const char *filename, const char *type, const char *comment, int flags, int check, mode_t mode);
170 /*! Starts writing a file */
172 * \param filename the name of the file to write to
173 * \param type format of file you wish to write out to
174 * \param comment comment to go with
175 * \param oflags output file flags
176 * \param check (unimplemented, hence negligible)
177 * \param mode Open mode
178 * Create an outgoing file stream. oflags are flags for the open() command, and
179 * if check is non-zero, then it will not write a file if there are any files that
180 * start with that name and have an extension
181 * Please note, this is a blocking function. Program execution will not return until ast_waitstream completes it's execution.
182 * Returns a struct ast_filestream on success, NULL on failure
184 struct ast_filestream *ast_writefile(const char *filename, const char *type, const char *comment, int flags, int check, mode_t mode);
186 /*! Writes a frame to a stream */
188 * \param fs filestream to write to
189 * \param f frame to write to the filestream
190 * Send a frame to a filestream -- note: does NOT free the frame, call ast_frfree manually
191 * Returns 0 on success, -1 on failure.
193 int ast_writestream(struct ast_filestream *fs, struct ast_frame *f);
195 /*! Closes a stream */
197 * \param f filestream to close
198 * Close a playback or recording stream
199 * Returns 0 on success, -1 on failure
201 int ast_closestream(struct ast_filestream *f);
203 /*! Opens stream for use in seeking, playing */
205 * \param chan channel to work with
206 * \param filename to use
207 * \param preflang prefered language to use
208 * Returns a ast_filestream pointer if it opens the file, NULL on error
210 struct ast_filestream *ast_openstream(struct ast_channel *chan, const char *filename, const char *preflang);
212 /*! Opens stream for use in seeking, playing */
214 * \param chan channel to work with
215 * \param filename to use
216 * \param preflang prefered language to use
217 * \param asis if set, don't clear generators
218 * Returns a ast_filestream pointer if it opens the file, NULL on error
220 struct ast_filestream *ast_openstream_full(struct ast_channel *chan, const char *filename, const char *preflang, int asis);
221 /*! Opens stream for use in seeking, playing */
223 * \param chan channel to work with
224 * \param filename to use
225 * \param preflang prefered language to use
226 * Returns a ast_filestream pointer if it opens the file, NULL on error
228 struct ast_filestream *ast_openvstream(struct ast_channel *chan, const char *filename, const char *preflang);
230 /*! Applys a open stream to a channel. */
232 * \param chan channel to work
233 * \param ast_filestream s to apply
234 * Returns 0 for success, -1 on failure
236 int ast_applystream(struct ast_channel *chan, struct ast_filestream *s);
238 /*! play a open stream on a channel. */
240 * \param ast_filestream s to play
241 * Returns 0 for success, -1 on failure
243 int ast_playstream(struct ast_filestream *s);
245 /*! Seeks into stream */
247 * \param ast_filestream to perform seek on
248 * \param sample_offset numbers of samples to seek
249 * \param whence SEEK_SET, SEEK_CUR, SEEK_END
250 * Returns 0 for success, or -1 for error
252 int ast_seekstream(struct ast_filestream *fs, long sample_offset, int whence);
254 /*! Trunc stream at current location */
256 * \param ast_filestream fs
257 * Returns 0 for success, or -1 for error
259 int ast_truncstream(struct ast_filestream *fs);
261 /*! Fast forward stream ms */
263 * \param ast_filestream fs filestream to act on
264 * \param ms milliseconds to move
265 * Returns 0 for success, or -1 for error
267 int ast_stream_fastforward(struct ast_filestream *fs, long ms);
269 /*! Rewind stream ms */
271 * \param ast_filestream fs filestream to act on
272 * \param ms milliseconds to move
273 * Returns 0 for success, or -1 for error
275 int ast_stream_rewind(struct ast_filestream *fs, long ms);
277 /*! Fast forward stream ms */
279 * \param ast_filestream fs filestream to act on
280 * \param ms milliseconds to move
281 * Returns 0 for success, or -1 for error
283 int ast_stream_fastforward(struct ast_filestream *fs, long ms);
285 /*! Rewind stream ms */
287 * \param ast_filestream fs filestream to act on
288 * \param ms milliseconds to move
289 * Returns 0 for success, or -1 for error
291 int ast_stream_rewind(struct ast_filestream *fs, long ms);
293 /*! Tell where we are in a stream */
295 * \param ast_filestream fs to act on
296 * Returns a long as a sample offset into stream
298 long ast_tellstream(struct ast_filestream *fs);
300 /*! Read a frame from a filestream */
302 * \param ast_filestream fs to act on
303 * Returns a frame or NULL if read failed
305 struct ast_frame *ast_readframe(struct ast_filestream *s);
307 /*! Initialize file stuff */
309 * Initializes all the various file stuff. Basically just registers the cli stuff
310 * Returns 0 all the time
312 extern int ast_file_init(void);
315 #define AST_RESERVED_POINTERS 20
317 #if defined(__cplusplus) || defined(c_plusplus)