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 /*! Same as waitstream but allows stream to be forwarded or rewound */
129 * \param c channel to waitstram on
130 * \param breakon string of DTMF digits to break upon
131 * \param forward DTMF digit to fast forward upon
132 * \param rewind DTMF digit to rewind upon
133 * \param ms How many miliseconds to skip forward/back
134 * Begins playback of a stream...
135 * Wait for a stream to stop or for any one of a given digit to arrive, Returns 0
136 * if the stream finishes, the character if it was interrupted, and -1 on error
138 int ast_waitstream_fr(struct ast_channel *c, const char *breakon, const char *forward, const char *rewind, int ms);
140 /* Same as waitstream, but with audio output to fd and monitored fd checking. Returns
141 1 if monfd is ready for reading */
142 int ast_waitstream_full(struct ast_channel *c, const char *breakon, int audiofd, int monfd);
144 /*! Starts reading from a file */
146 * \param filename the name of the file to read from
147 * \param type format of file you wish to read from
148 * \param comment comment to go with
149 * \param flags file flags
150 * \param check (unimplemented, hence negligible)
151 * \param mode Open mode
152 * Open an incoming file stream. flags are flags for the open() command, and
153 * if check is non-zero, then it will not read a file if there are any files that
154 * start with that name and have an extension
155 * Please note, this is a blocking function. Program execution will not return until ast_waitstream completes it's execution.
156 * Returns a struct ast_filestream on success, NULL on failure
158 struct ast_filestream *ast_readfile(const char *filename, const char *type, const char *comment, int flags, int check, mode_t mode);
160 /*! Starts writing a file */
162 * \param filename the name of the file to write to
163 * \param type format of file you wish to write out to
164 * \param comment comment to go with
165 * \param oflags output file flags
166 * \param check (unimplemented, hence negligible)
167 * \param mode Open mode
168 * Create an outgoing file stream. oflags are flags for the open() command, and
169 * if check is non-zero, then it will not write a file if there are any files that
170 * start with that name and have an extension
171 * Please note, this is a blocking function. Program execution will not return until ast_waitstream completes it's execution.
172 * Returns a struct ast_filestream on success, NULL on failure
174 struct ast_filestream *ast_writefile(const char *filename, const char *type, const char *comment, int flags, int check, mode_t mode);
176 /*! Writes a frame to a stream */
178 * \param fs filestream to write to
179 * \param f frame to write to the filestream
180 * Send a frame to a filestream -- note: does NOT free the frame, call ast_frfree manually
181 * Returns 0 on success, -1 on failure.
183 int ast_writestream(struct ast_filestream *fs, struct ast_frame *f);
185 /*! Closes a stream */
187 * \param f filestream to close
188 * Close a playback or recording stream
189 * Returns 0 on success, -1 on failure
191 int ast_closestream(struct ast_filestream *f);
193 /*! Opens stream for use in seeking, playing */
195 * \param chan channel to work with
196 * \param filename to use
197 * \param preflang prefered language to use
198 * Returns a ast_filestream pointer if it opens the file, NULL on error
200 struct ast_filestream *ast_openstream(struct ast_channel *chan, const char *filename, const char *preflang);
202 /*! Opens stream for use in seeking, playing */
204 * \param chan channel to work with
205 * \param filename to use
206 * \param preflang prefered language to use
207 * \param asis if set, don't clear generators
208 * Returns a ast_filestream pointer if it opens the file, NULL on error
210 struct ast_filestream *ast_openstream_full(struct ast_channel *chan, const char *filename, const char *preflang, int asis);
211 /*! Opens stream for use in seeking, playing */
213 * \param chan channel to work with
214 * \param filename to use
215 * \param preflang prefered language to use
216 * Returns a ast_filestream pointer if it opens the file, NULL on error
218 struct ast_filestream *ast_openvstream(struct ast_channel *chan, const char *filename, const char *preflang);
220 /*! Applys a open stream to a channel. */
222 * \param chan channel to work
223 * \param ast_filestream s to apply
224 * Returns 0 for success, -1 on failure
226 int ast_applystream(struct ast_channel *chan, struct ast_filestream *s);
228 /*! play a open stream on a channel. */
230 * \param ast_filestream s to play
231 * Returns 0 for success, -1 on failure
233 int ast_playstream(struct ast_filestream *s);
235 /*! Seeks into stream */
237 * \param ast_filestream to perform seek on
238 * \param sample_offset numbers of samples to seek
239 * \param whence SEEK_SET, SEEK_CUR, SEEK_END
240 * Returns 0 for success, or -1 for error
242 int ast_seekstream(struct ast_filestream *fs, long sample_offset, int whence);
244 /*! Trunc stream at current location */
246 * \param ast_filestream fs
247 * Returns 0 for success, or -1 for error
249 int ast_truncstream(struct ast_filestream *fs);
251 /*! Fast forward stream ms */
253 * \param ast_filestream fs filestream to act on
254 * \param ms milliseconds to move
255 * Returns 0 for success, or -1 for error
257 int ast_stream_fastforward(struct ast_filestream *fs, long ms);
259 /*! Rewind stream ms */
261 * \param ast_filestream fs filestream to act on
262 * \param ms milliseconds to move
263 * Returns 0 for success, or -1 for error
265 int ast_stream_rewind(struct ast_filestream *fs, long ms);
267 /*! Fast forward stream ms */
269 * \param ast_filestream fs filestream to act on
270 * \param ms milliseconds to move
271 * Returns 0 for success, or -1 for error
273 int ast_stream_fastforward(struct ast_filestream *fs, long ms);
275 /*! Rewind stream ms */
277 * \param ast_filestream fs filestream to act on
278 * \param ms milliseconds to move
279 * Returns 0 for success, or -1 for error
281 int ast_stream_rewind(struct ast_filestream *fs, long ms);
283 /*! Tell where we are in a stream */
285 * \param ast_filestream fs to act on
286 * Returns a long as a sample offset into stream
288 long ast_tellstream(struct ast_filestream *fs);
290 /*! Read a frame from a filestream */
292 * \param ast_filestream fs to act on
293 * Returns a frame or NULL if read failed
295 struct ast_frame *ast_readframe(struct ast_filestream *s);
297 /*! Initialize file stuff */
299 * Initializes all the various file stuff. Basically just registers the cli stuff
300 * Returns 0 all the time
302 extern int ast_file_init(void);
305 #define AST_RESERVED_POINTERS 20
307 #if defined(__cplusplus) || defined(c_plusplus)