Asterisk - The Open Source Telephony Project GIT-master-f45f878
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)
37extern "C" {
38#endif
39
40struct ast_filestream;
41struct 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 */
65typedef 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 */
77int 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 * \note If a failure is encountered, the stream will be closed before returning.
84 * \retval 0 if success.
85 * \retval -1 if error.
86 * \retval digit if interrupted by a digit.
87 */
88int ast_stream_and_wait(struct ast_channel *chan, const char *file, const char *digits);
89
90/*!
91 * \brief Stops a stream
92 *
93 * \param c The channel you wish to stop playback on
94 *
95 * Stop playback of a stream
96 *
97 * \retval 0 always
98 *
99 * \note The channel does not need to be locked before calling this function.
100 */
101int ast_stopstream(struct ast_channel *c);
102
103/*!
104 * \brief Checks for the existence of a given file
105 * \param filename name of the file you wish to check, minus the extension
106 * \param fmt the format you wish to check (the extension)
107 * \param preflang (the preferred language you wisht to find the file in)
108 * See if a given file exists in a given format. If fmt is NULL, any format is accepted.
109 * \retval 0 The file does not exist
110 * \retval 1 The file does exist.
111 */
112int ast_fileexists(const char *filename, const char *fmt, const char *preflang);
113
114/*!
115 * \brief Renames a file
116 * \param oldname the name of the file you wish to act upon (minus the extension)
117 * \param newname the name you wish to rename the file to (minus the extension)
118 * \param fmt the format of the file
119 * Rename a given file in a given format, or if fmt is NULL, then do so for all
120 * \retval -1 on failure
121 */
122int ast_filerename(const char *oldname, const char *newname, const char *fmt);
123
124/*!
125 * \brief Deletes a file
126 * \param filename name of the file you wish to delete (minus the extension)
127 * \param fmt of the file
128 * Delete a given file in a given format, or if fmt is NULL, then do so for all
129 */
130int ast_filedelete(const char *filename, const char *fmt);
131
132/*!
133 * \brief Copies a file
134 * \param oldname name of the file you wish to copy (minus extension)
135 * \param newname name you wish the file to be copied to (minus extension)
136 * \param fmt the format of the file
137 * Copy a given file in a given format, or if fmt is NULL, then do so for all
138 */
139int ast_filecopy(const char *oldname, const char *newname, const char *fmt);
140
141/*!
142 * \brief same as mkstemp, but return a FILE
143 * \param template_name The template for the unique file name to generate.
144 * Modified in place to return the file name.
145 * \param mode The mode for file permissions
146 *
147 * \return FILE handle to the temporary file on success or NULL if creation failed
148 */
149FILE *ast_file_mkftemp(char *template_name, mode_t mode);
150
151/*!
152 * \brief Create a temporary file located at path
153 *
154 * \note The directory containing path will be created if it does not exist
155 * \note This function assumes path does not end with a '/'
156 *
157 * \param path The directory path to create the file in
158 * \param filename Function allocates memory and stores full filename (including path) here
159 * \param template_name mkstemp template to use. Must end with XXXXXX.
160 *
161 * \note filename will need to be freed with ast_free if this function succeeds
162 *
163 * \retval -1 on failure
164 * \return file descriptor on success
165 */
166int ast_file_fdtemp(const char *path, char **filename, const char *template_name);
167
168/*!
169 * \brief Callback called for each file found when reading directories
170 * \param dir_name the name of the directory
171 * \param filename the name of the file
172 * \param obj user data object
173 * \retval non-zero to stop reading, otherwise zero to continue
174 *
175 * \note dir_name is not processed by realpath or other functions,
176 * symbolic links are not resolved. This ensures dir_name
177 * always starts with the exact string originally passed to
178 * \ref ast_file_read_dir or \ref ast_file_read_dirs.
179 */
180typedef int (*ast_file_on_file)(const char *dir_name, const char *filename, void *obj);
181
182/*!
183 * \brief Recursively iterate through files and directories up to max_depth
184 * \param dir_name the name of the directory to search
185 * \param on_file callback called on each file
186 * \param obj user data object
187 * \param max_depth re-curse into sub-directories up to a given maximum (-1 = infinite)
188 * \retval -1 on failure
189 * \retval errno on failure
190 * \retval 0 otherwise
191 */
192int ast_file_read_dirs(const char *dir_name, ast_file_on_file on_file, void *obj, int max_depth);
193
194/*!
195 * \brief Iterate over each file in a given directory
196 * \param dir_name the name of the directory to search
197 * \param on_file callback called on each file
198 * \param obj user data object
199 * \return -1
200 * \retval errno on failure
201 * \retval 0 otherwise
202 */
203#define ast_file_read_dir(dir_name, on_file, obj) ast_file_read_dirs(dir_name, on_file, obj, 1)
204
205/*!
206 * \brief Waits for a stream to stop or digit to be pressed
207 * \param c channel to waitstream on
208 * \param breakon string of DTMF digits to break upon
209 * Begins playback of a stream...
210 * Wait for a stream to stop or for any one of a given digit to arrive,
211 * \retval 0 if the stream finishes
212 * \retval character if it was interrupted by the channel.
213 * \retval -1 on error
214 */
215int ast_waitstream(struct ast_channel *c, const char *breakon);
216
217/*!
218 * \brief Waits for a stream to stop or digit matching a valid one digit exten to be pressed
219 * \param c channel to waitstream on
220 * \param context string of context to match digits to break upon
221 * Begins playback of a stream...
222 * Wait for a stream to stop or for any one of a valid extension digit to arrive,
223 * \retval 0 if the stream finishes.
224 * \retval character if it was interrupted.
225 * \retval -1 on error.
226 */
227int ast_waitstream_exten(struct ast_channel *c, const char *context);
228
229/*!
230 * \brief Same as waitstream but allows stream to be forwarded or rewound
231 * \param c channel to waitstream on
232 * \param breakon string of DTMF digits to break upon
233 * \param forward DTMF digit to fast forward upon
234 * \param rewind DTMF digit to rewind upon
235 * \param ms How many miliseconds to skip forward/back
236 * Begins playback of a stream...
237 * Wait for a stream to stop or for any one of a given digit to arrive,
238 * \retval 0 if the stream finishes.
239 * \retval character if it was interrupted,
240 * \return the value of the control frame if it was interrupted by some other party,
241 * \retval -1 on error.
242 */
243int ast_waitstream_fr(struct ast_channel *c, const char *breakon, const char *forward, const char *rewind, int ms);
244
245/*!
246 * \brief Same as waitstream_fr but allows a callback to be alerted when a user
247 * fastforwards or rewinds the file.
248 * \param c channel to waitstream on
249 * \param breakon string of DTMF digits to break upon
250 * \param forward DTMF digit to fast forward upon
251 * \param rewind DTMF digit to rewind upon
252 * \param ms How many milliseconds to skip forward/back
253 * \param cb to call when rewind or fastforward occurs.
254 * Begins playback of a stream...
255 * Wait for a stream to stop or for any one of a given digit to arrive,
256 * \retval 0 if the stream finishes.
257 * \retval character if it was interrupted,
258 * \return the value of the control frame if it was interrupted by some other party,
259 * \retval -1 on error.
260 */
262 const char *breakon,
263 const char *forward,
264 const char *rewind,
265 int ms,
267
268/*!
269 * Same as waitstream, but with audio output to fd and monitored fd checking.
270 *
271 * \retval 1 if monfd is ready for reading
272 */
273int ast_waitstream_full(struct ast_channel *c, const char *breakon, int audiofd, int monfd);
274
275/*!
276 * \brief Starts reading from a file
277 * \param filename the name of the file to read from
278 * \param type format of file you wish to read from
279 * \param comment comment to go with
280 * \param flags file flags
281 * \param check (unimplemented, hence negligible)
282 * \param mode Open mode
283 * Open an incoming file stream. flags are flags for the open() command, and
284 * if check is non-zero, then it will not read a file if there are any files that
285 * start with that name and have an extension
286 * Please note, this is a blocking function. Program execution will not return until ast_waitstream completes it's execution.
287 * \return a struct ast_filestream on success.
288 * \retval NULL on failure.
289 */
290struct ast_filestream *ast_readfile(const char *filename, const char *type, const char *comment, int flags, int check, mode_t mode);
291
292/*!
293 * \brief Starts writing a file
294 * \param filename the name of the file to write to
295 * \param type format of file you wish to write out to
296 * \param comment comment to go with
297 * \param flags output file flags
298 * \param check (unimplemented, hence negligible)
299 * \param mode Open mode
300 * Create an outgoing file stream. oflags are flags for the open() command, and
301 * if check is non-zero, then it will not write a file if there are any files that
302 * start with that name and have an extension
303 * Please note, this is a blocking function. Program execution will not return until ast_waitstream completes it's execution.
304 * \return a struct ast_filestream on success.
305 * \retval NULL on failure.
306 */
307struct ast_filestream *ast_writefile(const char *filename, const char *type, const char *comment, int flags, int check, mode_t mode);
308
309/*!
310 * \brief Writes a frame to a stream
311 * \param fs filestream to write to
312 * \param f frame to write to the filestream
313 * Send a frame to a filestream -- note: does NOT free the frame, call ast_frfree manually
314 * \retval 0 on success.
315 * \retval -1 on failure.
316 */
317int ast_writestream(struct ast_filestream *fs, struct ast_frame *f);
318
319/*!
320 * \brief Closes a stream
321 * \param f filestream to close
322 * Close a playback or recording stream
323 * \retval 0 on success.
324 * \retval -1 on failure.
325 */
326int ast_closestream(struct ast_filestream *f);
327
328/*!
329 * \brief Opens stream for use in seeking, playing
330 * \param chan channel to work with
331 * \param filename to use
332 * \param preflang prefered language to use
333 * \return a ast_filestream pointer if it opens the file.
334 * \retval NULL on error.
335 */
336struct ast_filestream *ast_openstream(struct ast_channel *chan, const char *filename, const char *preflang);
337
338/*!
339 * \brief Opens stream for use in seeking, playing
340 * \param chan channel to work with
341 * \param filename to use
342 * \param preflang prefered language to use
343 * \param asis if set, don't clear generators
344 * \retval a ast_filestream pointer if it opens the file.
345 * \retval NULL on error.
346 */
347struct ast_filestream *ast_openstream_full(struct ast_channel *chan, const char *filename, const char *preflang, int asis);
348/*!
349 * \brief Opens stream for use in seeking, playing
350 * \param chan channel to work with
351 * \param filename to use
352 * \param preflang prefered language to use
353 * \return a ast_filestream pointer if it opens the file.
354 * \retval NULL on error.
355 */
356struct ast_filestream *ast_openvstream(struct ast_channel *chan, const char *filename, const char *preflang);
357
358/*!
359 * \brief Applies a open stream to a channel.
360 * \param chan channel to work
361 * \param s ast_filestream to apply
362 * \retval 0 on success.
363 * \retval -1 on failure.
364 */
365int ast_applystream(struct ast_channel *chan, struct ast_filestream *s);
366
367/*!
368 * \brief Play a open stream on a channel.
369 * \param s filestream to play
370 * \retval 0 on success.
371 * \retval -1 on failure.
372 */
373int ast_playstream(struct ast_filestream *s);
374
375/*!
376 * \brief Seeks into stream
377 * \param fs ast_filestream to perform seek on
378 * \param sample_offset numbers of samples to seek
379 * \param whence SEEK_SET, SEEK_CUR, SEEK_END
380 * \retval 0 on success.
381 * \retval -1 on failure.
382 */
383int ast_seekstream(struct ast_filestream *fs, off_t sample_offset, int whence);
384
385/*!
386 * \brief Trunc stream at current location
387 * \param fs filestream to act on
388 * \retval 0 on success.
389 * \retval -1 on failure.
390 */
391int ast_truncstream(struct ast_filestream *fs);
392
393/*!
394 * \brief Fast forward stream ms
395 * \param fs filestream to act on
396 * \param ms milliseconds to move
397 * \retval 0 on success.
398 * \retval -1 on failure.
399 */
400int ast_stream_fastforward(struct ast_filestream *fs, off_t ms);
401
402/*!
403 * \brief Rewind stream ms
404 * \param fs filestream to act on
405 * \param ms milliseconds to move
406 * \retval 0 on success.
407 * \retval -1 on failure.
408 */
409int ast_stream_rewind(struct ast_filestream *fs, off_t ms);
410
411/*!
412 * \brief Tell where we are in a stream
413 * \param fs fs to act on
414 * \return a long as a sample offset into stream
415 */
416off_t ast_tellstream(struct ast_filestream *fs);
417
418/*!
419 * \brief Return the sample rate of the stream's format
420 * \param fs fs to act on
421 * \return sample rate in Hz
422 */
423int ast_ratestream(struct ast_filestream *fs);
424
425/*!
426 * \brief Read a frame from a filestream
427 * \param s ast_filestream to act on
428 * \return a frame.
429 * \retval NULL if read failed.
430 */
431struct ast_frame *ast_readframe(struct ast_filestream *s);
432
433/*! Initialize file stuff */
434/*!
435 * Initializes all the various file stuff. Basically just registers the cli stuff
436 * Returns 0 all the time
437 */
438int ast_file_init(void);
439
440
441#define AST_RESERVED_POINTERS 20
442
443/*! Remove duplicate formats from a format string. */
444/*!
445 * \param fmts a format string, this string will be modified
446 * \retval NULL error
447 * \return a pointer to the reduced format string, this is a pointer to fmts
448 */
449char *ast_format_str_reduce(char *fmts);
450
451/*!
452 * \brief Get the ast_format associated with the given file extension
453 * \since 12
454 *
455 * \param file_ext The file extension for which to find the format
456 *
457 * \retval NULL if not found
458 * \return A pointer to the ast_format associated with this file extension
459 */
460struct ast_format *ast_get_format_for_file_ext(const char *file_ext);
461
462/*!
463 * \brief Get a suitable filename extension for the given MIME type
464 *
465 * \param mime_type The MIME type for which to find extensions
466 * \param buffer A pointer to a buffer to receive the extension
467 * \param capacity The size of 'buffer' in bytes
468 *
469 * \retval 1 if an extension was found for the provided MIME type
470 * \retval 0 if the MIME type was not found
471 */
472int ast_get_extension_for_mime_type(const char *mime_type, char *buffer, size_t capacity);
473
474#if defined(__cplusplus) || defined(c_plusplus)
475}
476#endif
477
478#endif /* _ASTERISK_FILE_H */
#define comment
Definition: ael_lex.c:965
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:1085
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
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_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:1798
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
struct ast_frame * ast_readframe(struct ast_filestream *s)
Read a frame from a filestream.
Definition: file.c:936
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:1849
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:1075
int ast_stream_rewind(struct ast_filestream *fs, off_t ms)
Rewind stream ms.
Definition: file.c:1100
int ast_filerename(const char *oldname, const char *newname, const char *fmt)
Renames a file.
Definition: file.c:1146
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:1809
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:180
int ast_applystream(struct ast_channel *chan, struct ast_filestream *s)
Applies a open stream to a channel.
Definition: file.c:1057
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:1274
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:1371
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:848
int ast_streamfile(struct ast_channel *c, const char *filename, const char *preflang)
Streams a file.
Definition: file.c:1293
FILE * ast_file_mkftemp(char *template_name, mode_t mode)
same as mkstemp, but return a FILE
Definition: file.c:187
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:2006
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:1423
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:1878
char * ast_format_str_reduce(char *fmts)
Definition: file.c:1894
int ast_truncstream(struct ast_filestream *fs)
Trunc stream at current location.
Definition: file.c:1080
int ast_filecopy(const char *oldname, const char *newname, const char *fmt)
Copies a file.
Definition: file.c:1151
int ast_ratestream(struct ast_filestream *fs)
Return the sample rate of the stream's format.
Definition: file.c:1090
int ast_closestream(struct ast_filestream *f)
Closes a stream.
Definition: file.c:1111
int ast_fileexists(const char *filename, const char *fmt, const char *preflang)
Checks for the existence of a given file.
Definition: file.c:1129
int ast_stream_fastforward(struct ast_filestream *fs, off_t ms)
Fast forward stream ms.
Definition: file.c:1095
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:2019
int ast_playstream(struct ast_filestream *s)
Play a open stream on a channel.
Definition: file.c:1063
int ast_filedelete(const char *filename, const char *fmt)
Deletes a file.
Definition: file.c:1141
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:1859
int ast_file_init(void)
Definition: file.c:2051
int ast_waitstream(struct ast_channel *c, const char *breakon)
Waits for a stream to stop or digit to be pressed.
Definition: file.c:1840
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