Change function documentation to use doxygen tags. (Really, I just needed
[asterisk/asterisk.git] / include / asterisk / file.h
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  */
22
23 #ifndef _ASTERISK_FILE_H
24 #define _ASTERISK_FILE_H
25
26 #ifndef stdin
27 #error You must include stdio.h before file.h!
28 #endif /* !stdin */
29
30 #include "asterisk/channel.h"
31 #include "asterisk/frame.h"
32 #include <fcntl.h>
33
34 #if defined(__cplusplus) || defined(c_plusplus)
35 extern "C" {
36 #endif
37
38
39 /*! Convenient for waiting */
40 #define AST_DIGIT_ANY "0123456789#*ABCD"
41 #define AST_DIGIT_ANYNUM "0123456789"
42
43 /*! structure used for lock and refcount of format handlers.
44  * Should not be here, but this is a temporary workaround
45  * until we implement a more general mechanism.
46  * The format handler should include a pointer to
47  * this structure.
48  * As a trick, if usecnt is initialized with -1,
49  * ast_format_register will init the mutex for you.
50  */
51 struct ast_format_lock {
52         ast_mutex_t lock;
53         int usecnt;     /* number of active clients */
54 };
55
56 /*!
57  * Each supported file format is described by the following fields.
58  * Not all are necessary, the support routine implement default
59  * values for some of them.
60  * A handler typically fills a structure initializing the desired
61  * fields, and then calls ast_format_register() with the (readonly)
62  * structure as an argument.
63  */
64 struct ast_format {
65         char name[80];          /*!< Name of format */
66         char exts[80];          /*!< Extensions (separated by | if more than one) 
67                                 this format can read.  First is assumed for writing (e.g. .mp3) */
68         int format;             /*!< Format of frames it uses/provides (one only) */
69         /*! 
70          * \brief Prepare an input stream for playback. 
71          * \return 0 on success, -1 on error.
72          * The FILE is already open (in s->f) so this function only needs to perform
73          * any applicable validity checks on the file. If none is required, the
74          * function can be omitted.
75          */
76         int (*open)(struct ast_filestream *s);
77         /*! 
78          * \brief Prepare a stream for output, and comment it appropriately if applicable.
79          * \return 0 on success, -1 on error. 
80          * Same as the open, the FILE is already open so the function just needs to 
81          * prepare any header and other fields, if any. 
82          * The function can be omitted if nothing is needed.
83          */
84         int (*rewrite)(struct ast_filestream *s, const char *comment);
85         /*! Write a frame to a channel */
86         int (*write)(struct ast_filestream *, struct ast_frame *);
87         /*! seek num samples into file, whence - like a normal seek but with offset in samples */
88         int (*seek)(struct ast_filestream *, off_t, int);
89         int (*trunc)(struct ast_filestream *fs);        /*!< trunc file to current position */
90         off_t (*tell)(struct ast_filestream *fs);       /*!< tell current position */
91         /*! Read the next frame from the filestream (if available) and report
92          * when to get next frame (in samples)
93          */
94         struct ast_frame * (*read)(struct ast_filestream *, int *whennext);
95         /*! Do any closing actions, if any. The descriptor and structure are closed
96          * and destroyed by the generic routines, so they must not be done here. */
97         void (*close)(struct ast_filestream *);
98         char * (*getcomment)(struct ast_filestream *);          /*!< Retrieve file comment */
99
100         AST_LIST_ENTRY(ast_format) list;                        /*!< Link */
101
102         /*!
103          * If the handler needs a buffer (for read, typically)
104          * and/or a private descriptor, put here the
105          * required size (in bytes) and the support routine will allocate them
106          * for you, pointed by s->buf and s->private, respectively.
107          * When allocating a buffer, remember to leave AST_FRIENDLY_OFFSET
108          * spare bytes at the bginning.
109          */
110         int buf_size;                   /*!< size of frame buffer, if any, aligned to 8 bytes. */
111         int desc_size;                  /*!< size of private descriptor, if any */
112
113         struct ast_module *module;
114 };
115
116 /*!
117  * This structure is allocated by file.c in one chunk,
118  * together with buf_size and desc_size bytes of memory
119  * to be used for private purposes (e.g. buffers etc.)
120  */
121 struct ast_filestream {
122         /*! Everybody reserves a block of AST_RESERVED_POINTERS pointers for us */
123         struct ast_format *fmt; /* need to write to the lock and usecnt */
124         int flags;
125         mode_t mode;
126         char *filename;
127         char *realfilename;
128         /*! Video file stream */
129         struct ast_filestream *vfs;
130         /*! Transparently translate from another format -- just once */
131         struct ast_trans_pvt *trans;
132         struct ast_tranlator_pvt *tr;
133         int lastwriteformat;
134         int lasttimeout;
135         struct ast_channel *owner;
136         const char *orig_chan_name;
137         FILE *f;
138         struct ast_frame fr;    /*!< frame produced by read, typically */
139         char *buf;              /*!< buffer pointed to by ast_frame; */
140         void *private;  /*!< pointer to private buffer */
141 };
142
143 #define SEEK_FORCECUR   10
144         
145 /*! 
146  * \brief Register a new file format capability.
147  * Adds a format to Asterisk's format abilities.
148  * \retval 0 on success
149  * \retval -1 on failure
150  */
151 int __ast_format_register(const struct ast_format *f, struct ast_module *mod);
152 #define ast_format_register(f) __ast_format_register(f, ast_module_info->self)
153
154 /*! 
155  * \brief Unregisters a file format 
156  * \param name the name of the format you wish to unregister
157  * Unregisters a format based on the name of the format.
158  * \retval 0 on success
159  * \retval -1 on failure to unregister
160  */
161 int ast_format_unregister(const char *name);
162
163 /*! 
164  * \brief Streams a file 
165  * \param c channel to stream the file to
166  * \param filename the name of the file you wish to stream, minus the extension
167  * \param preflang the preferred language you wish to have the file streamed to you in
168  * Prepares a channel for the streaming of a file.  To start the stream, afterward do a ast_waitstream() on the channel
169  * Also, it will stop any existing streams on the channel.
170  * \retval 0 on success.
171  * \retval -1 on failure.
172  */
173 int ast_streamfile(struct ast_channel *c, const char *filename, const char *preflang);
174
175 /*!
176  * \brief stream file until digit
177  * If the file name is non-empty, try to play it.
178  * \note If digits == "" then we can simply check for non-zero.
179  * \return 0 if success.
180  * \retval -1 if error.
181  * \retval digit if interrupted by a digit.
182  */
183 int ast_stream_and_wait(struct ast_channel *chan, const char *file, const char *digits);
184
185 /*! 
186  * \brief Stops a stream 
187  * \param c The channel you wish to stop playback on
188  * Stop playback of a stream 
189  * \return 0 regardless
190  */
191 int ast_stopstream(struct ast_channel *c);
192
193 /*! 
194  * \brief Checks for the existence of a given file 
195  * \param filename name of the file you wish to check, minus the extension
196  * \param fmt the format you wish to check (the extension)
197  * \param preflang (the preferred language you wisht to find the file in)
198  * See if a given file exists in a given format.  If fmt is NULL,  any format is accepted.
199  * \return -1 if file does not exist, non-zero positive otherwise.
200  */
201 int ast_fileexists(const char *filename, const char *fmt, const char *preflang);
202
203 /*! 
204  * \brief Renames a file 
205  * \param oldname the name of the file you wish to act upon (minus the extension)
206  * \param newname the name you wish to rename the file to (minus the extension)
207  * \param fmt the format of the file
208  * Rename a given file in a given format, or if fmt is NULL, then do so for all 
209  * \return -1 on failure
210  */
211 int ast_filerename(const char *oldname, const char *newname, const char *fmt);
212
213 /*!
214  * \brief  Deletes a file
215  * \param filename name of the file you wish to delete (minus the extension)
216  * \param fmt of the file
217  * Delete a given file in a given format, or if fmt is NULL, then do so for all 
218  */
219 int ast_filedelete(const char *filename, const char *fmt);
220
221 /*! 
222  * \brief Copies a file 
223  * \param oldname name of the file you wish to copy (minus extension)
224  * \param newname name you wish the file to be copied to (minus extension)
225  * \param fmt the format of the file
226  * Copy a given file in a given format, or if fmt is NULL, then do so for all 
227  */
228 int ast_filecopy(const char *oldname, const char *newname, const char *fmt);
229
230 /*! 
231  * \brief Waits for a stream to stop or digit to be pressed
232  * \param c channel to waitstream on
233  * \param breakon string of DTMF digits to break upon
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 the character if it was interrupted,
238  * \retval -1 on error 
239  */
240 int ast_waitstream(struct ast_channel *c, const char *breakon);
241
242 /*! 
243  * \brief Waits for a stream to stop or digit matching a valid one digit exten to be pressed 
244  * \param c channel to waitstream on
245  * \param context string of context to match digits to break upon
246  * Begins playback of a stream...
247  * Wait for a stream to stop or for any one of a valid extension digit to arrive,  
248  * \retval 0 if the stream finishes.
249  * \retval the character if it was interrupted.
250  * \retval -1 on error.
251  */
252 int ast_waitstream_exten(struct ast_channel *c, const char *context);
253
254 /*! 
255  * \brief Same as waitstream but allows stream to be forwarded or rewound 
256  * \param c channel to waitstream on
257  * \param breakon string of DTMF digits to break upon
258  * \param forward DTMF digit to fast forward upon
259  * \param rewind DTMF digit to rewind upon
260  * \param ms How many miliseconds to skip forward/back
261  * Begins playback of a stream...
262  * Wait for a stream to stop or for any one of a given digit to arrive,  
263  * \retval 0 if the stream finishes.
264  * \retval the character if it was interrupted.
265  * \retval -1 on error.
266  */
267 int ast_waitstream_fr(struct ast_channel *c, const char *breakon, const char *forward, const char *rewind, int ms);
268
269 /*!
270  * Same as waitstream, but with audio output to fd and monitored fd checking.  
271  *
272  * \return 1 if monfd is ready for reading 
273  */
274 int ast_waitstream_full(struct ast_channel *c, const char *breakon, int audiofd, int monfd);
275
276 /*! 
277  * \brief Starts reading from a file
278  * \param filename the name of the file to read from
279  * \param type format of file you wish to read from
280  * \param comment comment to go with
281  * \param flags file flags
282  * \param check (unimplemented, hence negligible)
283  * \param mode Open mode
284  * Open an incoming file stream.  flags are flags for the open() command, and 
285  * if check is non-zero, then it will not read a file if there are any files that 
286  * start with that name and have an extension
287  * Please note, this is a blocking function.  Program execution will not return until ast_waitstream completes it's execution.
288  * \retval a struct ast_filestream on success.
289  * \retval NULL on failure.
290  */
291 struct ast_filestream *ast_readfile(const char *filename, const char *type, const char *comment, int flags, int check, mode_t mode);
292
293 /*! 
294  * \brief Starts writing a file 
295  * \param filename the name of the file to write to
296  * \param type format of file you wish to write out to
297  * \param comment comment to go with
298  * \param flags output file flags
299  * \param check (unimplemented, hence negligible)
300  * \param mode Open mode
301  * Create an outgoing file stream.  oflags are flags for the open() command, and 
302  * if check is non-zero, then it will not write a file if there are any files that 
303  * start with that name and have an extension
304  * Please note, this is a blocking function.  Program execution will not return until ast_waitstream completes it's execution.
305  * \retval a struct ast_filestream on success.
306  * \retval NULL on failure.
307  */
308 struct ast_filestream *ast_writefile(const char *filename, const char *type, const char *comment, int flags, int check, mode_t mode);
309
310 /*! 
311  * \brief Writes a frame to a stream 
312  * \param fs filestream to write to
313  * \param f frame to write to the filestream
314  * Send a frame to a filestream -- note: does NOT free the frame, call ast_frfree manually
315  * \retval 0 on success.
316  * \retval -1 on failure.
317  */
318 int ast_writestream(struct ast_filestream *fs, struct ast_frame *f);
319
320 /*! 
321  * \brief Closes a stream 
322  * \param f filestream to close
323  * Close a playback or recording stream
324  * \retval 0 on success.
325  * \retval -1 on failure.
326  */
327 int ast_closestream(struct ast_filestream *f);
328
329 /*! 
330  * \brief Opens stream for use in seeking, playing 
331  * \param chan channel to work with
332  * \param filename to use
333  * \param preflang prefered language to use
334  * \retval a ast_filestream pointer if it opens the file.
335  * \retval NULL on error.
336  */
337 struct ast_filestream *ast_openstream(struct ast_channel *chan, const char *filename, const char *preflang);
338
339 /*! 
340  * \brief Opens stream for use in seeking, playing 
341  * \param chan channel to work with
342  * \param filename to use
343  * \param preflang prefered language to use
344  * \param asis if set, don't clear generators
345  * \retval a ast_filestream pointer if it opens the file.
346  * \retval NULL on error.
347  */
348 struct ast_filestream *ast_openstream_full(struct ast_channel *chan, const char *filename, const char *preflang, int asis);
349 /*! 
350  * \brief Opens stream for use in seeking, playing 
351  * \param chan channel to work with
352  * \param filename to use
353  * \param preflang prefered language to use
354  * \retval a ast_filestream pointer if it opens the file.
355  * \retval NULL on error.
356  */
357 struct ast_filestream *ast_openvstream(struct ast_channel *chan, const char *filename, const char *preflang);
358
359 /*! 
360  * \brief Applys a open stream to a channel. 
361  * \param chan channel to work
362  * \param s ast_filestream to apply
363  * \retval 0 on success.
364  * \retval -1 on failure.
365  */
366 int ast_applystream(struct ast_channel *chan, struct ast_filestream *s);
367
368 /*! 
369  * \brief Play a open stream on a channel. 
370  * \param s filestream to play
371  * \retval 0 on success.
372  * \retval -1 on failure.
373  */
374 int ast_playstream(struct ast_filestream *s);
375
376 /*! 
377  * \brief Seeks into stream 
378  * \param fs ast_filestream to perform seek on
379  * \param sample_offset numbers of samples to seek
380  * \param whence SEEK_SET, SEEK_CUR, SEEK_END 
381  * \retval 0 on success.
382  * \retval -1 on failure.
383  */
384 int ast_seekstream(struct ast_filestream *fs, off_t sample_offset, int whence);
385
386 /*! 
387  * \brief Trunc stream at current location 
388  * \param fs filestream to act on
389  * \retval 0 on success.
390  * \retval -1 on failure.
391  */
392 int ast_truncstream(struct ast_filestream *fs);
393
394 /*! 
395  * \brief Fast forward stream ms 
396  * \param fs filestream to act on
397  * \param ms milliseconds to move
398  * \retval 0 on success.
399  * \retval -1 on failure.
400  */
401 int ast_stream_fastforward(struct ast_filestream *fs, off_t ms);
402
403 /*! 
404  * \brief Rewind stream ms 
405  * \param fs filestream to act on
406  * \param ms milliseconds to move
407  * \retval 0 on success.
408  * \retval -1 on failure.
409  */
410 int ast_stream_rewind(struct ast_filestream *fs, off_t ms);
411
412 /*! 
413  * \brief Tell where we are in a stream 
414  * \param fs fs to act on
415  * \return a long as a sample offset into stream
416  */
417 off_t ast_tellstream(struct ast_filestream *fs);
418
419 /*! 
420  * \brief Read a frame from a filestream 
421  * \param s ast_filestream to act on
422  * \return a frame.
423  * \retval NULL if read failed.
424  */ 
425 struct ast_frame *ast_readframe(struct ast_filestream *s);
426
427 /*! 
428  * \brief Initialize file stuff 
429  * Initializes all the various file stuff.  Basically just registers the cli stuff
430  * \return 0 all the time
431  */
432 int ast_file_init(void);
433
434
435 #define AST_RESERVED_POINTERS 20
436
437 #if defined(__cplusplus) || defined(c_plusplus)
438 }
439 #endif
440
441 #endif /* _ASTERISK_FILE_H */