Wed Jan 8 2020 09:49:47

Asterisk developer's documentation


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_ANY "0123456789#*ABCD"
48 #define AST_DIGIT_ANYNUM "0123456789"
49 
50 #define SEEK_FORCECUR 10
51 
52 /*!
53  * \brief Streams a file
54  * \param c channel to stream the file to
55  * \param filename the name of the file you wish to stream, minus the extension
56  * \param preflang the preferred language you wish to have the file streamed to you in
57  * Prepares a channel for the streaming of a file. To start the stream, afterward do a ast_waitstream() on the channel
58  * Also, it will stop any existing streams on the channel.
59  * \retval 0 on success.
60  * \retval -1 on failure.
61  */
62 int ast_streamfile(struct ast_channel *c, const char *filename, const char *preflang);
63 
64 /*!
65  * \brief stream file until digit
66  * If the file name is non-empty, try to play it.
67  * \note If digits == "" then we can simply check for non-zero.
68  * \return 0 if success.
69  * \retval -1 if error.
70  * \retval digit if interrupted by a digit.
71  */
72 int ast_stream_and_wait(struct ast_channel *chan, const char *file, const char *digits);
73 
74 /*!
75  * \brief Stops a stream
76  *
77  * \param c The channel you wish to stop playback on
78  *
79  * Stop playback of a stream
80  *
81  * \retval 0 always
82  *
83  * \note The channel does not need to be locked before calling this function.
84  */
85 int ast_stopstream(struct ast_channel *c);
86 
87 /*!
88  * \brief Checks for the existence of a given file
89  * \param filename name of the file you wish to check, minus the extension
90  * \param fmt the format you wish to check (the extension)
91  * \param preflang (the preferred language you wisht to find the file in)
92  * See if a given file exists in a given format. If fmt is NULL, any format is accepted.
93  * \return 0 if file does not exist, non-zero positive otherwise.
94  */
95 int ast_fileexists(const char *filename, const char *fmt, const char *preflang);
96 
97 /*!
98  * \brief Renames a file
99  * \param oldname the name of the file you wish to act upon (minus the extension)
100  * \param newname the name you wish to rename the file to (minus the extension)
101  * \param fmt the format of the file
102  * Rename a given file in a given format, or if fmt is NULL, then do so for all
103  * \return -1 on failure
104  */
105 int ast_filerename(const char *oldname, const char *newname, const char *fmt);
106 
107 /*!
108  * \brief Deletes a file
109  * \param filename name of the file you wish to delete (minus the extension)
110  * \param fmt of the file
111  * Delete a given file in a given format, or if fmt is NULL, then do so for all
112  */
113 int ast_filedelete(const char *filename, const char *fmt);
114 
115 /*!
116  * \brief Copies a file
117  * \param oldname name of the file you wish to copy (minus extension)
118  * \param newname name you wish the file to be copied to (minus extension)
119  * \param fmt the format of the file
120  * Copy a given file in a given format, or if fmt is NULL, then do so for all
121  */
122 int ast_filecopy(const char *oldname, const char *newname, const char *fmt);
123 
124 /*!
125  * \brief Waits for a stream to stop or digit to be pressed
126  * \param c channel to waitstream on
127  * \param breakon string of DTMF digits to break upon
128  * Begins playback of a stream...
129  * Wait for a stream to stop or for any one of a given digit to arrive,
130  * \retval 0 if the stream finishes
131  * \retval the character if it was interrupted,
132  * \retval -1 on error
133  */
134 int ast_waitstream(struct ast_channel *c, const char *breakon);
135 
136 /*!
137  * \brief Waits for a stream to stop or digit matching a valid one digit exten to be pressed
138  * \param c channel to waitstream on
139  * \param context string of context to match digits to break upon
140  * Begins playback of a stream...
141  * Wait for a stream to stop or for any one of a valid extension digit to arrive,
142  * \retval 0 if the stream finishes.
143  * \retval the character if it was interrupted.
144  * \retval -1 on error.
145  */
146 int ast_waitstream_exten(struct ast_channel *c, const char *context);
147 
148 /*!
149  * \brief Same as waitstream but allows stream to be forwarded or rewound
150  * \param c channel to waitstream on
151  * \param breakon string of DTMF digits to break upon
152  * \param forward DTMF digit to fast forward upon
153  * \param rewind DTMF digit to rewind upon
154  * \param ms How many miliseconds to skip forward/back
155  * Begins playback of a stream...
156  * Wait for a stream to stop or for any one of a given digit to arrive,
157  * \retval 0 if the stream finishes.
158  * \retval the character if it was interrupted.
159  * \retval -1 on error.
160  */
161 int ast_waitstream_fr(struct ast_channel *c, const char *breakon, const char *forward, const char *rewind, int ms);
162 
163 /*!
164  * Same as waitstream, but with audio output to fd and monitored fd checking.
165  *
166  * \return 1 if monfd is ready for reading
167  */
168 int ast_waitstream_full(struct ast_channel *c, const char *breakon, int audiofd, int monfd);
169 
170 /*!
171  * \brief Starts reading from a file
172  * \param filename the name of the file to read from
173  * \param type format of file you wish to read from
174  * \param comment comment to go with
175  * \param flags file flags
176  * \param check (unimplemented, hence negligible)
177  * \param mode Open mode
178  * Open an incoming file stream. flags are flags for the open() command, and
179  * if check is non-zero, then it will not read a file if there are any files that
180  * start with that name and have an extension
181  * Please note, this is a blocking function. Program execution will not return until ast_waitstream completes it's execution.
182  * \retval a struct ast_filestream on success.
183  * \retval NULL on failure.
184  */
185 struct ast_filestream *ast_readfile(const char *filename, const char *type, const char *comment, int flags, int check, mode_t mode);
186 
187 /*!
188  * \brief Starts writing a file
189  * \param filename the name of the file to write to
190  * \param type format of file you wish to write out to
191  * \param comment comment to go with
192  * \param flags output file flags
193  * \param check (unimplemented, hence negligible)
194  * \param mode Open mode
195  * Create an outgoing file stream. oflags are flags for the open() command, and
196  * if check is non-zero, then it will not write a file if there are any files that
197  * start with that name and have an extension
198  * Please note, this is a blocking function. Program execution will not return until ast_waitstream completes it's execution.
199  * \retval a struct ast_filestream on success.
200  * \retval NULL on failure.
201  */
202 struct ast_filestream *ast_writefile(const char *filename, const char *type, const char *comment, int flags, int check, mode_t mode);
203 
204 /*!
205  * \brief Writes a frame to a stream
206  * \param fs filestream to write to
207  * \param f frame to write to the filestream
208  * Send a frame to a filestream -- note: does NOT free the frame, call ast_frfree manually
209  * \retval 0 on success.
210  * \retval -1 on failure.
211  */
212 int ast_writestream(struct ast_filestream *fs, struct ast_frame *f);
213 
214 /*!
215  * \brief Closes a stream
216  * \param f filestream to close
217  * Close a playback or recording stream
218  * \retval 0 on success.
219  * \retval -1 on failure.
220  */
221 int ast_closestream(struct ast_filestream *f);
222 
223 /*!
224  * \brief Opens stream for use in seeking, playing
225  * \param chan channel to work with
226  * \param filename to use
227  * \param preflang prefered language to use
228  * \retval a ast_filestream pointer if it opens the file.
229  * \retval NULL on error.
230  */
231 struct ast_filestream *ast_openstream(struct ast_channel *chan, const char *filename, const char *preflang);
232 
233 /*!
234  * \brief Opens stream for use in seeking, playing
235  * \param chan channel to work with
236  * \param filename to use
237  * \param preflang prefered language to use
238  * \param asis if set, don't clear generators
239  * \retval a ast_filestream pointer if it opens the file.
240  * \retval NULL on error.
241  */
242 struct ast_filestream *ast_openstream_full(struct ast_channel *chan, const char *filename, const char *preflang, int asis);
243 /*!
244  * \brief Opens stream for use in seeking, playing
245  * \param chan channel to work with
246  * \param filename to use
247  * \param preflang prefered language to use
248  * \retval a ast_filestream pointer if it opens the file.
249  * \retval NULL on error.
250  */
251 struct ast_filestream *ast_openvstream(struct ast_channel *chan, const char *filename, const char *preflang);
252 
253 /*!
254  * \brief Applys a open stream to a channel.
255  * \param chan channel to work
256  * \param s ast_filestream to apply
257  * \retval 0 on success.
258  * \retval -1 on failure.
259  */
260 int ast_applystream(struct ast_channel *chan, struct ast_filestream *s);
261 
262 /*!
263  * \brief Play a open stream on a channel.
264  * \param s filestream to play
265  * \retval 0 on success.
266  * \retval -1 on failure.
267  */
268 int ast_playstream(struct ast_filestream *s);
269 
270 /*!
271  * \brief Seeks into stream
272  * \param fs ast_filestream to perform seek on
273  * \param sample_offset numbers of samples to seek
274  * \param whence SEEK_SET, SEEK_CUR, SEEK_END
275  * \retval 0 on success.
276  * \retval -1 on failure.
277  */
278 int ast_seekstream(struct ast_filestream *fs, off_t sample_offset, int whence);
279 
280 /*!
281  * \brief Trunc stream at current location
282  * \param fs filestream to act on
283  * \retval 0 on success.
284  * \retval -1 on failure.
285  */
286 int ast_truncstream(struct ast_filestream *fs);
287 
288 /*!
289  * \brief Fast forward stream ms
290  * \param fs filestream to act on
291  * \param ms milliseconds to move
292  * \retval 0 on success.
293  * \retval -1 on failure.
294  */
295 int ast_stream_fastforward(struct ast_filestream *fs, off_t ms);
296 
297 /*!
298  * \brief Rewind stream ms
299  * \param fs filestream to act on
300  * \param ms milliseconds to move
301  * \retval 0 on success.
302  * \retval -1 on failure.
303  */
304 int ast_stream_rewind(struct ast_filestream *fs, off_t ms);
305 
306 /*!
307  * \brief Tell where we are in a stream
308  * \param fs fs to act on
309  * \return a long as a sample offset into stream
310  */
311 off_t ast_tellstream(struct ast_filestream *fs);
312 
313 /*!
314  * \brief Read a frame from a filestream
315  * \param s ast_filestream to act on
316  * \return a frame.
317  * \retval NULL if read failed.
318  */
319 struct ast_frame *ast_readframe(struct ast_filestream *s);
320 
321 /*! Initialize file stuff */
322 /*!
323  * Initializes all the various file stuff. Basically just registers the cli stuff
324  * Returns 0 all the time
325  */
326 int ast_file_init(void);
327 
328 
329 #define AST_RESERVED_POINTERS 20
330 
331 /*! Remove duplicate formats from a format string. */
332 /*!
333  * \param fmts a format string, this string will be modified
334  * \retval NULL error
335  * \return a pointer to the reduced format string, this is a pointer to fmts
336  */
337 char *ast_format_str_reduce(char *fmts);
338 
339 #if defined(__cplusplus) || defined(c_plusplus)
340 }
341 #endif
342 
343 #endif /* _ASTERISK_FILE_H */
int ast_filecopy(const char *oldname, const char *newname, const char *fmt)
Copies a file.
Definition: file.c:941
Main Channel structure associated with a channel.
Definition: channel.h:742
int ast_file_init(void)
Definition: file.c:1503
int ast_streamfile(struct ast_channel *c, const char *filename, const char *preflang)
Streams a file.
Definition: file.c:946
Each supported file format is described by the following structure.
Definition: mod_format.h:43
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:641
int ast_filedelete(const char *filename, const char *fmt)
Deletes a file.
Definition: file.c:931
int ast_stream_fastforward(struct ast_filestream *fs, off_t ms)
Fast forward stream ms.
Definition: file.c:894
off_t ast_tellstream(struct ast_filestream *fs)
Tell where we are in a stream.
Definition: file.c:889
int ast_playstream(struct ast_filestream *s)
Play a open stream on a channel.
Definition: file.c:867
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:681
char * ast_format_str_reduce(char *fmts)
Definition: file.c:1382
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:1337
int ast_applystream(struct ast_channel *chan, struct ast_filestream *s)
Applys a open stream to a channel.
Definition: file.c:861
struct ast_frame * ast_readframe(struct ast_filestream *s)
Read a frame from a filestream.
Definition: file.c:737
int ast_stream_rewind(struct ast_filestream *fs, off_t ms)
Rewind stream ms.
Definition: file.c:899
#define comment
Definition: ael_lex.c:961
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:1049
int ast_seekstream(struct ast_filestream *fs, off_t sample_offset, int whence)
Seeks into stream.
Definition: file.c:879
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:1370
int ast_closestream(struct ast_filestream *f)
Closes a stream.
Definition: file.c:904
static struct ast_format f[]
Definition: format_g726.c:181
static const char type[]
Definition: chan_nbs.c:57
int ast_truncstream(struct ast_filestream *fs)
Trunc stream at current location.
Definition: file.c:884
int ast_waitstream_full(struct ast_channel *c, const char *breakon, int audiofd, int monfd)
Definition: file.c:1348
char * filename
Definition: mod_format.h:106
This structure is allocated by file.c in one chunk, together with buf_size and desc_size bytes of mem...
Definition: mod_format.h:100
int ast_writestream(struct ast_filestream *fs, struct ast_frame *f)
Writes a frame to a stream.
Definition: file.c:150
int ast_waitstream(struct ast_channel *c, const char *breakon)
Waits for a stream to stop or digit to be pressed.
Definition: file.c:1343
int ast_fileexists(const char *filename, const char *fmt, const char *preflang)
Checks for the existence of a given file.
Definition: file.c:919
Data structure associated with a single frame of data.
Definition: frame.h:142
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:997
int ast_filerename(const char *oldname, const char *newname, const char *fmt)
Renames a file.
Definition: file.c:936
static char context[AST_MAX_CONTEXT]
Definition: chan_alsa.c:107
int ast_stopstream(struct ast_channel *c)
Stops a stream.
Definition: file.c:128
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:636
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:1354