Asterisk - The Open Source Telephony Project  GIT-master-a24979a
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 <markster@digium.com>
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 fastforwarding
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  * \retval 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 The file does not exist
109  * \retval 1 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  * \retval -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 same as mkstemp, but return a FILE
142  * \param template The template for the unique file name to generate. Modified in place to return the file name.
143  * \param mode The mode for file permissions
144  *
145  * \return FILE handle to the temporary file on success or NULL if creation failed
146  */
147 FILE *ast_file_mkftemp(char *template, mode_t mode);
148 
149 /*!
150  * \brief Create a temporary file located at path
151  *
152  * \note The directory containing path will be created if it does not exist
153  * \note This function assumes path does not end with a '/'
154  *
155  * \param path The directory path to create the file in
156  * \param filename Function allocates memory and stores full filename (including path) here
157  * \param template_name mkstemp template to use. Must end with XXXXXX.
158  *
159  * \note filename will need to be freed with ast_free if this function succeeds
160  *
161  * \retval -1 on failure
162  * \return file descriptor on success
163  */
164 int ast_file_fdtemp(const char *path, char **filename, const char *template_name);
165 
166 /*!
167  * \brief Callback called for each file found when reading directories
168  * \param dir_name the name of the directory
169  * \param filename the name of the file
170  * \param obj user data object
171  * \retval non-zero to stop reading, otherwise zero to continue
172  *
173  * \note dir_name is not processed by realpath or other functions,
174  * symbolic links are not resolved. This ensures dir_name
175  * always starts with the exact string originally passed to
176  * \ref ast_file_read_dir or \ref ast_file_read_dirs.
177  */
178 typedef int (*ast_file_on_file)(const char *dir_name, const char *filename, void *obj);
179 
180 /*!
181  * \brief Recursively iterate through files and directories up to max_depth
182  * \param dir_name the name of the directory to search
183  * \param on_file callback called on each file
184  * \param obj user data object
185  * \param max_depth re-curse into sub-directories up to a given maximum (-1 = infinite)
186  * \retval -1 on failure
187  * \retval errno on failure
188  * \retval 0 otherwise
189  */
190 int ast_file_read_dirs(const char *dir_name, ast_file_on_file on_file, void *obj, int max_depth);
191 
192 /*!
193  * \brief Iterate over each file in a given directory
194  * \param dir_name the name of the directory to search
195  * \param on_file callback called on each file
196  * \param obj user data object
197  * \return -1
198  * \retval errno on failure
199  * \retval 0 otherwise
200  */
201 #define ast_file_read_dir(dir_name, on_file, obj) ast_file_read_dirs(dir_name, on_file, obj, 1)
202 
203 /*!
204  * \brief Waits for a stream to stop or digit to be pressed
205  * \param c channel to waitstream on
206  * \param breakon string of DTMF digits to break upon
207  * Begins playback of a stream...
208  * Wait for a stream to stop or for any one of a given digit to arrive,
209  * \retval 0 if the stream finishes
210  * \retval character if it was interrupted by the channel.
211  * \retval -1 on error
212  */
213 int ast_waitstream(struct ast_channel *c, const char *breakon);
214 
215 /*!
216  * \brief Waits for a stream to stop or digit matching a valid one digit exten to be pressed
217  * \param c channel to waitstream on
218  * \param context string of context to match digits to break upon
219  * Begins playback of a stream...
220  * Wait for a stream to stop or for any one of a valid extension digit to arrive,
221  * \retval 0 if the stream finishes.
222  * \retval character if it was interrupted.
223  * \retval -1 on error.
224  */
225 int ast_waitstream_exten(struct ast_channel *c, const char *context);
226 
227 /*!
228  * \brief Same as waitstream but allows stream to be forwarded or rewound
229  * \param c channel to waitstream on
230  * \param breakon string of DTMF digits to break upon
231  * \param forward DTMF digit to fast forward upon
232  * \param rewind DTMF digit to rewind upon
233  * \param ms How many miliseconds to skip forward/back
234  * Begins playback of a stream...
235  * Wait for a stream to stop or for any one of a given digit to arrive,
236  * \retval 0 if the stream finishes.
237  * \retval character if it was interrupted,
238  * \return the value of the control frame if it was interrupted by some other party,
239  * \retval -1 on error.
240  */
241 int ast_waitstream_fr(struct ast_channel *c, const char *breakon, const char *forward, const char *rewind, int ms);
242 
243 /*!
244  * \brief Same as waitstream_fr but allows a callback to be alerted when a user
245  * fastforwards or rewinds the file.
246  * \param c channel to waitstream on
247  * \param breakon string of DTMF digits to break upon
248  * \param forward DTMF digit to fast forward upon
249  * \param rewind DTMF digit to rewind upon
250  * \param ms How many milliseconds to skip forward/back
251  * \param cb to call when rewind or fastforward occurs.
252  * Begins playback of a stream...
253  * Wait for a stream to stop or for any one of a given digit to arrive,
254  * \retval 0 if the stream finishes.
255  * \retval character if it was interrupted,
256  * \return the value of the control frame if it was interrupted by some other party,
257  * \retval -1 on error.
258  */
260  const char *breakon,
261  const char *forward,
262  const char *rewind,
263  int ms,
265 
266 /*!
267  * Same as waitstream, but with audio output to fd and monitored fd checking.
268  *
269  * \retval 1 if monfd is ready for reading
270  */
271 int ast_waitstream_full(struct ast_channel *c, const char *breakon, int audiofd, int monfd);
272 
273 /*!
274  * \brief Starts reading from a file
275  * \param filename the name of the file to read from
276  * \param type format of file you wish to read from
277  * \param comment comment to go with
278  * \param flags file flags
279  * \param check (unimplemented, hence negligible)
280  * \param mode Open mode
281  * Open an incoming file stream. flags are flags for the open() command, and
282  * if check is non-zero, then it will not read a file if there are any files that
283  * start with that name and have an extension
284  * Please note, this is a blocking function. Program execution will not return until ast_waitstream completes it's execution.
285  * \return a struct ast_filestream on success.
286  * \retval NULL on failure.
287  */
288 struct ast_filestream *ast_readfile(const char *filename, const char *type, const char *comment, int flags, int check, mode_t mode);
289 
290 /*!
291  * \brief Starts writing a file
292  * \param filename the name of the file to write to
293  * \param type format of file you wish to write out to
294  * \param comment comment to go with
295  * \param flags output file flags
296  * \param check (unimplemented, hence negligible)
297  * \param mode Open mode
298  * Create an outgoing file stream. oflags are flags for the open() command, and
299  * if check is non-zero, then it will not write a file if there are any files that
300  * start with that name and have an extension
301  * Please note, this is a blocking function. Program execution will not return until ast_waitstream completes it's execution.
302  * \return a struct ast_filestream on success.
303  * \retval NULL on failure.
304  */
305 struct ast_filestream *ast_writefile(const char *filename, const char *type, const char *comment, int flags, int check, mode_t mode);
306 
307 /*!
308  * \brief Writes a frame to a stream
309  * \param fs filestream to write to
310  * \param f frame to write to the filestream
311  * Send a frame to a filestream -- note: does NOT free the frame, call ast_frfree manually
312  * \retval 0 on success.
313  * \retval -1 on failure.
314  */
315 int ast_writestream(struct ast_filestream *fs, struct ast_frame *f);
316 
317 /*!
318  * \brief Closes a stream
319  * \param f filestream to close
320  * Close a playback or recording stream
321  * \retval 0 on success.
322  * \retval -1 on failure.
323  */
324 int ast_closestream(struct ast_filestream *f);
325 
326 /*!
327  * \brief Opens stream for use in seeking, playing
328  * \param chan channel to work with
329  * \param filename to use
330  * \param preflang prefered language to use
331  * \return a ast_filestream pointer if it opens the file.
332  * \retval NULL on error.
333  */
334 struct ast_filestream *ast_openstream(struct ast_channel *chan, const char *filename, const char *preflang);
335 
336 /*!
337  * \brief Opens stream for use in seeking, playing
338  * \param chan channel to work with
339  * \param filename to use
340  * \param preflang prefered language to use
341  * \param asis if set, don't clear generators
342  * \retval a ast_filestream pointer if it opens the file.
343  * \retval NULL on error.
344  */
345 struct ast_filestream *ast_openstream_full(struct ast_channel *chan, const char *filename, const char *preflang, int asis);
346 /*!
347  * \brief Opens stream for use in seeking, playing
348  * \param chan channel to work with
349  * \param filename to use
350  * \param preflang prefered language to use
351  * \return a ast_filestream pointer if it opens the file.
352  * \retval NULL on error.
353  */
354 struct ast_filestream *ast_openvstream(struct ast_channel *chan, const char *filename, const char *preflang);
355 
356 /*!
357  * \brief Applies a open stream to a channel.
358  * \param chan channel to work
359  * \param s ast_filestream to apply
360  * \retval 0 on success.
361  * \retval -1 on failure.
362  */
363 int ast_applystream(struct ast_channel *chan, struct ast_filestream *s);
364 
365 /*!
366  * \brief Play a open stream on a channel.
367  * \param s filestream to play
368  * \retval 0 on success.
369  * \retval -1 on failure.
370  */
371 int ast_playstream(struct ast_filestream *s);
372 
373 /*!
374  * \brief Seeks into stream
375  * \param fs ast_filestream to perform seek on
376  * \param sample_offset numbers of samples to seek
377  * \param whence SEEK_SET, SEEK_CUR, SEEK_END
378  * \retval 0 on success.
379  * \retval -1 on failure.
380  */
381 int ast_seekstream(struct ast_filestream *fs, off_t sample_offset, int whence);
382 
383 /*!
384  * \brief Trunc stream at current location
385  * \param fs filestream to act on
386  * \retval 0 on success.
387  * \retval -1 on failure.
388  */
389 int ast_truncstream(struct ast_filestream *fs);
390 
391 /*!
392  * \brief Fast forward stream ms
393  * \param fs filestream to act on
394  * \param ms milliseconds to move
395  * \retval 0 on success.
396  * \retval -1 on failure.
397  */
398 int ast_stream_fastforward(struct ast_filestream *fs, off_t ms);
399 
400 /*!
401  * \brief Rewind stream ms
402  * \param fs filestream to act on
403  * \param ms milliseconds to move
404  * \retval 0 on success.
405  * \retval -1 on failure.
406  */
407 int ast_stream_rewind(struct ast_filestream *fs, off_t ms);
408 
409 /*!
410  * \brief Tell where we are in a stream
411  * \param fs fs to act on
412  * \return a long as a sample offset into stream
413  */
414 off_t ast_tellstream(struct ast_filestream *fs);
415 
416 /*!
417  * \brief Return the sample rate of the stream's format
418  * \param fs fs to act on
419  * \return sample rate in Hz
420  */
421 int ast_ratestream(struct ast_filestream *fs);
422 
423 /*!
424  * \brief Read a frame from a filestream
425  * \param s ast_filestream to act on
426  * \return a frame.
427  * \retval NULL if read failed.
428  */
429 struct ast_frame *ast_readframe(struct ast_filestream *s);
430 
431 /*! Initialize file stuff */
432 /*!
433  * Initializes all the various file stuff. Basically just registers the cli stuff
434  * Returns 0 all the time
435  */
436 int ast_file_init(void);
437 
438 
439 #define AST_RESERVED_POINTERS 20
440 
441 /*! Remove duplicate formats from a format string. */
442 /*!
443  * \param fmts a format string, this string will be modified
444  * \retval NULL error
445  * \return a pointer to the reduced format string, this is a pointer to fmts
446  */
447 char *ast_format_str_reduce(char *fmts);
448 
449 /*!
450  * \brief Get the ast_format associated with the given file extension
451  * \since 12
452  *
453  * \param file_ext The file extension for which to find the format
454  *
455  * \retval NULL if not found
456  * \return A pointer to the ast_format associated with this file extension
457  */
458 struct ast_format *ast_get_format_for_file_ext(const char *file_ext);
459 
460 /*!
461  * \brief Get a suitable filename extension for the given MIME type
462  *
463  * \param mime_type The MIME type for which to find extensions
464  * \param buffer A pointer to a buffer to receive the extension
465  * \param capacity The size of 'buffer' in bytes
466  *
467  * \retval 1 if an extension was found for the provided MIME type
468  * \retval 0 if the MIME type was not found
469  */
470 int ast_get_extension_for_mime_type(const char *mime_type, char *buffer, size_t capacity);
471 
472 #if defined(__cplusplus) || defined(c_plusplus)
473 }
474 #endif
475 
476 #endif /* _ASTERISK_FILE_H */
#define comment
Definition: ael_lex.c:976
static char context[AST_MAX_CONTEXT]
Definition: chan_alsa.c:120
static const char type[]
Definition: chan_ooh323.c:109
off_t ast_tellstream(struct ast_filestream *fs)
Tell where we are in a stream.
Definition: file.c:1083
ast_waitstream_fr_cb_values
Definition: file.h:54
@ AST_WAITSTREAM_CB_FASTFORWARD
Definition: file.h:56
@ AST_WAITSTREAM_CB_REWIND
Definition: file.h:55
@ AST_WAITSTREAM_CB_START
Definition: file.h:57
int ast_file_fdtemp(const char *path, char **filename, const char *template_name)
Create a temporary file located at path.
Definition: file.c:202
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:1775
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
int ast_waitstream_full(struct ast_channel *c, const char *breakon, int audiofd, int monfd)
Definition: file.c:1826
int ast_stopstream(struct ast_channel *c)
Stops a stream.
Definition: file.c:222
int ast_writestream(struct ast_filestream *fs, struct ast_frame *f)
Writes a frame to a stream.
Definition: file.c:244
int ast_seekstream(struct ast_filestream *fs, off_t sample_offset, int whence)
Seeks into stream.
Definition: file.c:1073
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:1979
int ast_stream_rewind(struct ast_filestream *fs, off_t ms)
Rewind stream ms.
Definition: file.c:1098
int ast_filerename(const char *oldname, const char *newname, const char *fmt)
Renames a file.
Definition: file.c:1144
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:1786
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:178
int ast_applystream(struct ast_channel *chan, struct ast_filestream *s)
Applies a open stream to a channel.
Definition: file.c:1055
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:1272
int ast_streamfile(struct ast_channel *c, const char *filename, const char *preflang)
Streams a file.
Definition: file.c:1291
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:1855
struct ast_frame * ast_readframe(struct ast_filestream *s)
Read a frame from a filestream.
Definition: file.c:934
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:1402
char * ast_format_str_reduce(char *fmts)
Definition: file.c:1867
int ast_truncstream(struct ast_filestream *fs)
Trunc stream at current location.
Definition: file.c:1078
int ast_filecopy(const char *oldname, const char *newname, const char *fmt)
Copies a file.
Definition: file.c:1149
int ast_ratestream(struct ast_filestream *fs)
Return the sample rate of the stream's format.
Definition: file.c:1088
int ast_closestream(struct ast_filestream *f)
Closes a stream.
Definition: file.c:1109
int ast_fileexists(const char *filename, const char *fmt, const char *preflang)
Checks for the existence of a given file.
Definition: file.c:1127
int ast_stream_fastforward(struct ast_filestream *fs, off_t ms)
Fast forward stream ms.
Definition: file.c:1093
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:1992
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:1350
int ast_playstream(struct ast_filestream *s)
Play a open stream on a channel.
Definition: file.c:1061
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:790
int ast_filedelete(const char *filename, const char *fmt)
Deletes a file.
Definition: file.c:1139
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:795
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:1836
int ast_file_init(void)
Definition: file.c:2024
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:847
FILE * ast_file_mkftemp(char *template, mode_t mode)
same as mkstemp, but return a FILE
Definition: file.c:187
int ast_waitstream(struct ast_channel *c, const char *breakon)
Waits for a stream to stop or digit to be pressed.
Definition: file.c:1817
Main Channel structure associated with a channel.
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
char * filename
Definition: mod_format.h:107
Definition of a media format.
Definition: format.c:43
Data structure associated with a single frame of data.
Definition: ast_expr2.c:325
static struct test_val c