Asterisk - The Open Source Telephony Project  18.5.0
file.h
Go to the documentation of this file.
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999 - 2006, Digium, Inc.
5  *
6  * Mark Spencer <[email protected]>
7  *
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.
13  *
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.
17  */
18 
19 /*! \file
20  * \brief Generic File Format Support.
21  * Should be included by clients of the file handling routines.
22  * File service providers should instead include mod_format.h
23  */
24 
25 #ifndef _ASTERISK_FILE_H
26 #define _ASTERISK_FILE_H
27 
28 #ifdef HAVE_FCNTL_H
29 #include <fcntl.h>
30 #endif
31 
32 #ifdef HAVE_MMAP
33 #include <sys/mman.h>
34 #endif
35 
36 #if defined(__cplusplus) || defined(c_plusplus)
37 extern "C" {
38 #endif
39 
40 struct ast_filestream;
41 struct ast_format;
42 
43 /*! The maximum number of formats we expect to see in a format string */
44 #define AST_MAX_FORMATS 10
45 
46 /*! Convenient for waiting */
47 #define AST_DIGIT_NONE ""
48 #define AST_DIGIT_ANY "0123456789#*ABCD"
49 #define AST_DIGIT_ANYNUM "0123456789"
50 
51 #define SEEK_FORCECUR 10
52 
53 /*! The type of event associated with a ast_waitstream_fr_cb invocation */
58 };
59 
60 /*!
61  * \brief callback used during dtmf controlled file playback to indicate
62  * location of playback in a file after rewinding or fastfowarding
63  * a file.
64  */
65 typedef void (ast_waitstream_fr_cb)(struct ast_channel *chan, long ms, enum ast_waitstream_fr_cb_values val);
66 
67 /*!
68  * \brief Streams a file
69  * \param c channel to stream the file to
70  * \param filename the name of the file you wish to stream, minus the extension
71  * \param preflang the preferred language you wish to have the file streamed to you in
72  * Prepares a channel for the streaming of a file. To start the stream, afterward do a ast_waitstream() on the channel
73  * Also, it will stop any existing streams on the channel.
74  * \retval 0 on success.
75  * \retval -1 on failure.
76  */
77 int ast_streamfile(struct ast_channel *c, const char *filename, const char *preflang);
78 
79 /*!
80  * \brief stream file until digit
81  * If the file name is non-empty, try to play it.
82  * \note If digits == "" then we can simply check for non-zero.
83  * \return 0 if success.
84  * \retval -1 if error.
85  * \retval digit if interrupted by a digit.
86  */
87 int ast_stream_and_wait(struct ast_channel *chan, const char *file, const char *digits);
88 
89 /*!
90  * \brief Stops a stream
91  *
92  * \param c The channel you wish to stop playback on
93  *
94  * Stop playback of a stream
95  *
96  * \retval 0 always
97  *
98  * \note The channel does not need to be locked before calling this function.
99  */
100 int ast_stopstream(struct ast_channel *c);
101 
102 /*!
103  * \brief Checks for the existence of a given file
104  * \param filename name of the file you wish to check, minus the extension
105  * \param fmt the format you wish to check (the extension)
106  * \param preflang (the preferred language you wisht to find the file in)
107  * See if a given file exists in a given format. If fmt is NULL, any format is accepted.
108  * \retval 0, false. The file does not exist
109  * \retval 1, true. The file does exist.
110  */
111 int ast_fileexists(const char *filename, const char *fmt, const char *preflang);
112 
113 /*!
114  * \brief Renames a file
115  * \param oldname the name of the file you wish to act upon (minus the extension)
116  * \param newname the name you wish to rename the file to (minus the extension)
117  * \param fmt the format of the file
118  * Rename a given file in a given format, or if fmt is NULL, then do so for all
119  * \return -1 on failure
120  */
121 int ast_filerename(const char *oldname, const char *newname, const char *fmt);
122 
123 /*!
124  * \brief Deletes a file
125  * \param filename name of the file you wish to delete (minus the extension)
126  * \param fmt of the file
127  * Delete a given file in a given format, or if fmt is NULL, then do so for all
128  */
129 int ast_filedelete(const char *filename, const char *fmt);
130 
131 /*!
132  * \brief Copies a file
133  * \param oldname name of the file you wish to copy (minus extension)
134  * \param newname name you wish the file to be copied to (minus extension)
135  * \param fmt the format of the file
136  * Copy a given file in a given format, or if fmt is NULL, then do so for all
137  */
138 int ast_filecopy(const char *oldname, const char *newname, const char *fmt);
139 
140 /*!
141  * \brief Callback called for each file found when reading directories
142  * \param dir_name the name of the directory
143  * \param filename the name of the file
144  * \param obj user data object
145  * \return non-zero to stop reading, otherwise zero to continue
146  *
147  * \note dir_name is not processed by realpath or other functions,
148  * symbolic links are not resolved. This ensures dir_name
149  * always starts with the exact string originally passed to
150  * \ref ast_file_read_dir or \ref ast_file_read_dirs.
151  */
152 typedef int (*ast_file_on_file)(const char *dir_name, const char *filename, void *obj);
153 
154 /*!
155  * \brief Recursively iterate through files and directories up to max_depth
156  * \param dir_name the name of the directory to search
157  * \param on_file callback called on each file
158  * \param obj user data object
159  * \param max_depth re-curse into sub-directories up to a given maximum (-1 = infinite)
160  * \return -1 or errno on failure, otherwise 0
161  */
162 int ast_file_read_dirs(const char *dir_name, ast_file_on_file on_file, void *obj, int max_depth);
163 
164 /*!
165  * \brief Iterate over each file in a given directory
166  * \param dir_name the name of the directory to search
167  * \param on_file callback called on each file
168  * \param obj user data object
169  * \return -1 or errno on failure, otherwise 0
170  */
171 #define ast_file_read_dir(dir_name, on_file, obj) ast_file_read_dirs(dir_name, on_file, obj, 1)
172 
173 /*!
174  * \brief Waits for a stream to stop or digit to be pressed
175  * \param c channel to waitstream on
176  * \param breakon string of DTMF digits to break upon
177  * Begins playback of a stream...
178  * Wait for a stream to stop or for any one of a given digit to arrive,
179  * \retval 0 if the stream finishes
180  * \retval the character if it was interrupted by the channel.
181  * \retval -1 on error
182  */
183 int ast_waitstream(struct ast_channel *c, const char *breakon);
184 
185 /*!
186  * \brief Waits for a stream to stop or digit matching a valid one digit exten to be pressed
187  * \param c channel to waitstream on
188  * \param context string of context to match digits to break upon
189  * Begins playback of a stream...
190  * Wait for a stream to stop or for any one of a valid extension digit to arrive,
191  * \retval 0 if the stream finishes.
192  * \retval the character if it was interrupted.
193  * \retval -1 on error.
194  */
195 int ast_waitstream_exten(struct ast_channel *c, const char *context);
196 
197 /*!
198  * \brief Same as waitstream but allows stream to be forwarded or rewound
199  * \param c channel to waitstream on
200  * \param breakon string of DTMF digits to break upon
201  * \param forward DTMF digit to fast forward upon
202  * \param rewind DTMF digit to rewind upon
203  * \param ms How many miliseconds to skip forward/back
204  * Begins playback of a stream...
205  * Wait for a stream to stop or for any one of a given digit to arrive,
206  * \retval 0 if the stream finishes.
207  * \retval the character if it was interrupted,
208  * \retval the value of the control frame if it was interrupted by some other party,
209  * \retval -1 on error.
210  */
211 int ast_waitstream_fr(struct ast_channel *c, const char *breakon, const char *forward, const char *rewind, int ms);
212 
213 /*!
214  * \brief Same as waitstream_fr but allows a callback to be alerted when a user
215  * fastforwards or rewinds the file.
216  * \param c channel to waitstream on
217  * \param breakon string of DTMF digits to break upon
218  * \param forward DTMF digit to fast forward upon
219  * \param rewind DTMF digit to rewind upon
220  * \param ms How many milliseconds to skip forward/back
221  * \param cb to call when rewind or fastfoward occurs.
222  * Begins playback of a stream...
223  * Wait for a stream to stop or for any one of a given digit to arrive,
224  * \retval 0 if the stream finishes.
225  * \retval the character if it was interrupted,
226  * \retval the value of the control frame if it was interrupted by some other party,
227  * \retval -1 on error.
228  */
230  const char *breakon,
231  const char *forward,
232  const char *rewind,
233  int ms,
235 
236 /*!
237  * Same as waitstream, but with audio output to fd and monitored fd checking.
238  *
239  * \return 1 if monfd is ready for reading
240  */
241 int ast_waitstream_full(struct ast_channel *c, const char *breakon, int audiofd, int monfd);
242 
243 /*!
244  * \brief Starts reading from a file
245  * \param filename the name of the file to read from
246  * \param type format of file you wish to read from
247  * \param comment comment to go with
248  * \param flags file flags
249  * \param check (unimplemented, hence negligible)
250  * \param mode Open mode
251  * Open an incoming file stream. flags are flags for the open() command, and
252  * if check is non-zero, then it will not read a file if there are any files that
253  * start with that name and have an extension
254  * Please note, this is a blocking function. Program execution will not return until ast_waitstream completes it's execution.
255  * \retval a struct ast_filestream on success.
256  * \retval NULL on failure.
257  */
258 struct ast_filestream *ast_readfile(const char *filename, const char *type, const char *comment, int flags, int check, mode_t mode);
259 
260 /*!
261  * \brief Starts writing a file
262  * \param filename the name of the file to write to
263  * \param type format of file you wish to write out to
264  * \param comment comment to go with
265  * \param flags output file flags
266  * \param check (unimplemented, hence negligible)
267  * \param mode Open mode
268  * Create an outgoing file stream. oflags are flags for the open() command, and
269  * if check is non-zero, then it will not write a file if there are any files that
270  * start with that name and have an extension
271  * Please note, this is a blocking function. Program execution will not return until ast_waitstream completes it's execution.
272  * \retval a struct ast_filestream on success.
273  * \retval NULL on failure.
274  */
275 struct ast_filestream *ast_writefile(const char *filename, const char *type, const char *comment, int flags, int check, mode_t mode);
276 
277 /*!
278  * \brief Writes a frame to a stream
279  * \param fs filestream to write to
280  * \param f frame to write to the filestream
281  * Send a frame to a filestream -- note: does NOT free the frame, call ast_frfree manually
282  * \retval 0 on success.
283  * \retval -1 on failure.
284  */
285 int ast_writestream(struct ast_filestream *fs, struct ast_frame *f);
286 
287 /*!
288  * \brief Closes a stream
289  * \param f filestream to close
290  * Close a playback or recording stream
291  * \retval 0 on success.
292  * \retval -1 on failure.
293  */
294 int ast_closestream(struct ast_filestream *f);
295 
296 /*!
297  * \brief 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  * \retval a ast_filestream pointer if it opens the file.
302  * \retval NULL on error.
303  */
304 struct ast_filestream *ast_openstream(struct ast_channel *chan, const char *filename, const char *preflang);
305 
306 /*!
307  * \brief Opens stream for use in seeking, playing
308  * \param chan channel to work with
309  * \param filename to use
310  * \param preflang prefered language to use
311  * \param asis if set, don't clear generators
312  * \retval a ast_filestream pointer if it opens the file.
313  * \retval NULL on error.
314  */
315 struct ast_filestream *ast_openstream_full(struct ast_channel *chan, const char *filename, const char *preflang, int asis);
316 /*!
317  * \brief Opens stream for use in seeking, playing
318  * \param chan channel to work with
319  * \param filename to use
320  * \param preflang prefered language to use
321  * \retval a ast_filestream pointer if it opens the file.
322  * \retval NULL on error.
323  */
324 struct ast_filestream *ast_openvstream(struct ast_channel *chan, const char *filename, const char *preflang);
325 
326 /*!
327  * \brief Applys a open stream to a channel.
328  * \param chan channel to work
329  * \param s ast_filestream to apply
330  * \retval 0 on success.
331  * \retval -1 on failure.
332  */
333 int ast_applystream(struct ast_channel *chan, struct ast_filestream *s);
334 
335 /*!
336  * \brief Play a open stream on a channel.
337  * \param s filestream to play
338  * \retval 0 on success.
339  * \retval -1 on failure.
340  */
341 int ast_playstream(struct ast_filestream *s);
342 
343 /*!
344  * \brief Seeks into stream
345  * \param fs ast_filestream to perform seek on
346  * \param sample_offset numbers of samples to seek
347  * \param whence SEEK_SET, SEEK_CUR, SEEK_END
348  * \retval 0 on success.
349  * \retval -1 on failure.
350  */
351 int ast_seekstream(struct ast_filestream *fs, off_t sample_offset, int whence);
352 
353 /*!
354  * \brief Trunc stream at current location
355  * \param fs filestream to act on
356  * \retval 0 on success.
357  * \retval -1 on failure.
358  */
359 int ast_truncstream(struct ast_filestream *fs);
360 
361 /*!
362  * \brief Fast forward stream ms
363  * \param fs filestream to act on
364  * \param ms milliseconds to move
365  * \retval 0 on success.
366  * \retval -1 on failure.
367  */
368 int ast_stream_fastforward(struct ast_filestream *fs, off_t ms);
369 
370 /*!
371  * \brief Rewind stream ms
372  * \param fs filestream to act on
373  * \param ms milliseconds to move
374  * \retval 0 on success.
375  * \retval -1 on failure.
376  */
377 int ast_stream_rewind(struct ast_filestream *fs, off_t ms);
378 
379 /*!
380  * \brief Tell where we are in a stream
381  * \param fs fs to act on
382  * \return a long as a sample offset into stream
383  */
384 off_t ast_tellstream(struct ast_filestream *fs);
385 
386 /*!
387  * \brief Return the sample rate of the stream's format
388  * \param fs fs to act on
389  * \return sample rate in Hz
390  */
391 int ast_ratestream(struct ast_filestream *fs);
392 
393 /*!
394  * \brief Read a frame from a filestream
395  * \param s ast_filestream to act on
396  * \return a frame.
397  * \retval NULL if read failed.
398  */
399 struct ast_frame *ast_readframe(struct ast_filestream *s);
400 
401 /*! Initialize file stuff */
402 /*!
403  * Initializes all the various file stuff. Basically just registers the cli stuff
404  * Returns 0 all the time
405  */
406 int ast_file_init(void);
407 
408 
409 #define AST_RESERVED_POINTERS 20
410 
411 /*! Remove duplicate formats from a format string. */
412 /*!
413  * \param fmts a format string, this string will be modified
414  * \retval NULL error
415  * \return a pointer to the reduced format string, this is a pointer to fmts
416  */
417 char *ast_format_str_reduce(char *fmts);
418 
419 /*!
420  * \brief Get the ast_format associated with the given file extension
421  * \since 12
422  *
423  * \param file_ext The file extension for which to find the format
424  *
425  * \retval NULL if not found
426  * \retval A pointer to the ast_format associated with this file extension
427  */
428 struct ast_format *ast_get_format_for_file_ext(const char *file_ext);
429 
430 /*!
431  * \brief Get a suitable filename extension for the given MIME type
432  *
433  * \param mime_type The MIME type for which to find extensions
434  * \param buffer A pointer to a buffer to receive the extension
435  * \param capacity The size of 'buffer' in bytes
436  *
437  * \retval 1 if an extension was found for the provided MIME type
438  * \retval 0 if the MIME type was not found
439  */
440 int ast_get_extension_for_mime_type(const char *mime_type, char *buffer, size_t capacity);
441 
442 #if defined(__cplusplus) || defined(c_plusplus)
443 }
444 #endif
445 
446 #endif /* _ASTERISK_FILE_H */
int ast_filecopy(const char *oldname, const char *newname, const char *fmt)
Copies a file.
Definition: file.c:1108
static const char type[]
Definition: chan_ooh323.c:109
Main Channel structure associated with a channel.
int ast_streamfile(struct ast_channel *c, const char *filename, const char *preflang)
Streams a file.
Definition: file.c:1250
Definition: ast_expr2.c:325
Definition of a media format.
Definition: format.c:43
ast_waitstream_fr_cb_values
Definition: file.h:54
struct ast_filestream * ast_openstream_full(struct ast_channel *chan, const char *filename, const char *preflang, int asis)
Opens stream for use in seeking, playing.
Definition: file.c:760
static struct test_val c
int ast_filedelete(const char *filename, const char *fmt)
Deletes a file.
Definition: file.c:1098
int ast_file_read_dirs(const char *dir_name, ast_file_on_file on_file, void *obj, int max_depth)
Recursively iterate through files and directories up to max_depth.
Definition: file.c:1231
int ast_stream_fastforward(struct ast_filestream *fs, off_t ms)
Fast forward stream ms.
Definition: file.c:1058
off_t ast_tellstream(struct ast_filestream *fs)
Tell where we are in a stream.
Definition: file.c:1048
int ast_playstream(struct ast_filestream *s)
Play a open stream on a channel.
Definition: file.c:1026
struct ast_filestream * ast_openvstream(struct ast_channel *chan, const char *filename, const char *preflang)
Opens stream for use in seeking, playing.
Definition: file.c:812
char * ast_format_str_reduce(char *fmts)
Definition: file.c:1826
int ast_waitstream_fr(struct ast_channel *c, const char *breakon, const char *forward, const char *rewind, int ms)
Same as waitstream but allows stream to be forwarded or rewound.
Definition: file.c:1745
int ast_ratestream(struct ast_filestream *fs)
Return the sample rate of the stream&#39;s format.
Definition: file.c:1053
int(* ast_file_on_file)(const char *dir_name, const char *filename, void *obj)
Callback called for each file found when reading directories.
Definition: file.h:152
int ast_applystream(struct ast_channel *chan, struct ast_filestream *s)
Applys a open stream to a channel.
Definition: file.c:1020
void() ast_waitstream_fr_cb(struct ast_channel *chan, long ms, enum ast_waitstream_fr_cb_values val)
callback used during dtmf controlled file playback to indicate location of playback in a file after r...
Definition: file.h:65
struct ast_frame * ast_readframe(struct ast_filestream *s)
Read a frame from a filestream.
Definition: file.c:899
int ast_stream_rewind(struct ast_filestream *fs, off_t ms)
Rewind stream ms.
Definition: file.c:1063
#define comment
Definition: ael_lex.c:976
struct ast_filestream * ast_writefile(const char *filename, const char *type, const char *comment, int flags, int check, mode_t mode)
Starts writing a file.
Definition: file.c:1361
int ast_seekstream(struct ast_filestream *fs, off_t sample_offset, int whence)
Seeks into stream.
Definition: file.c:1038
int ast_stream_and_wait(struct ast_channel *chan, const char *file, const char *digits)
stream file until digit If the file name is non-empty, try to play it.
Definition: file.c:1814
int ast_closestream(struct ast_filestream *f)
Closes a stream.
Definition: file.c:1068
struct ast_format * ast_get_format_for_file_ext(const char *file_ext)
Get the ast_format associated with the given file extension.
Definition: file.c:1938
int ast_truncstream(struct ast_filestream *fs)
Trunc stream at current location.
Definition: file.c:1043
int ast_waitstream_full(struct ast_channel *c, const char *breakon, int audiofd, int monfd)
Definition: file.c:1785
This structure is allocated by file.c in one chunk, together with buf_size and desc_size bytes of mem...
Definition: mod_format.h:101
int ast_writestream(struct ast_filestream *fs, struct ast_frame *f)
Writes a frame to a stream.
Definition: file.c:209
int ast_waitstream(struct ast_channel *c, const char *breakon)
Waits for a stream to stop or digit to be pressed.
Definition: file.c:1776
int ast_fileexists(const char *filename, const char *fmt, const char *preflang)
Checks for the existence of a given file.
Definition: file.c:1086
Data structure associated with a single frame of data.
struct ast_filestream * ast_readfile(const char *filename, const char *type, const char *comment, int flags, int check, mode_t mode)
Starts reading from a file.
Definition: file.c:1309
int ast_filerename(const char *oldname, const char *newname, const char *fmt)
Renames a file.
Definition: file.c:1103
static char context[AST_MAX_CONTEXT]
Definition: chan_alsa.c:116
int ast_get_extension_for_mime_type(const char *mime_type, char *buffer, size_t capacity)
Get a suitable filename extension for the given MIME type.
Definition: file.c:1951
int ast_stopstream(struct ast_channel *c)
Stops a stream.
Definition: file.c:187
int ast_file_init(void)
Definition: file.c:1983
struct ast_filestream * ast_openstream(struct ast_channel *chan, const char *filename, const char *preflang)
Opens stream for use in seeking, playing.
Definition: file.c:755
int ast_waitstream_fr_w_cb(struct ast_channel *c, const char *breakon, const char *forward, const char *rewind, int ms, ast_waitstream_fr_cb cb)
Same as waitstream_fr but allows a callback to be alerted when a user fastforwards or rewinds the fil...
Definition: file.c:1734
int ast_waitstream_exten(struct ast_channel *c, const char *context)
Waits for a stream to stop or digit matching a valid one digit exten to be pressed.
Definition: file.c:1795