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