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