(closes issue #12929)
[asterisk/asterisk.git] / main / file.c
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  *
21  * \brief Generic File Format Support.
22  *
23  * \author Mark Spencer <markster@digium.com> 
24  */
25
26 #include "asterisk.h"
27
28 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
29
30 #include <dirent.h>
31 #include <sys/stat.h>
32 #include <math.h>
33
34 #include "asterisk/_private.h"  /* declare ast_file_init() */
35 #include "asterisk/paths.h"     /* use ast_config_AST_DATA_DIR */
36 #include "asterisk/mod_format.h"
37 #include "asterisk/cli.h"
38 #include "asterisk/channel.h"
39 #include "asterisk/sched.h"
40 #include "asterisk/translate.h"
41 #include "asterisk/utils.h"
42 #include "asterisk/lock.h"
43 #include "asterisk/app.h"
44 #include "asterisk/pbx.h"
45 #include "asterisk/linkedlists.h"
46 #include "asterisk/module.h"
47
48 /*
49  * The following variable controls the layout of localized sound files.
50  * If 0, use the historical layout with prefix just before the filename
51  * (i.e. digits/en/1.gsm , digits/it/1.gsm or default to digits/1.gsm),
52  * if 1 put the prefix at the beginning of the filename
53  * (i.e. en/digits/1.gsm, it/digits/1.gsm or default to digits/1.gsm).
54  * The latter permits a language to be entirely in one directory.
55  */
56 int ast_language_is_prefix = 1;
57
58 static AST_RWLIST_HEAD_STATIC(formats, ast_format);
59
60 int __ast_format_register(const struct ast_format *f, struct ast_module *mod)
61 {
62         struct ast_format *tmp;
63
64         AST_RWLIST_WRLOCK(&formats);
65         AST_RWLIST_TRAVERSE(&formats, tmp, list) {
66                 if (!strcasecmp(f->name, tmp->name)) {
67                         AST_RWLIST_UNLOCK(&formats);
68                         ast_log(LOG_WARNING, "Tried to register '%s' format, already registered\n", f->name);
69                         return -1;
70                 }
71         }
72         if (!(tmp = ast_calloc(1, sizeof(*tmp)))) {
73                 AST_RWLIST_UNLOCK(&formats);
74                 return -1;
75         }
76         *tmp = *f;
77         tmp->module = mod;
78         if (tmp->buf_size) {
79                 /*
80                  * Align buf_size properly, rounding up to the machine-specific
81                  * alignment for pointers.
82                  */
83                 struct _test_align { void *a, *b; } p;
84                 int align = (char *)&p.b - (char *)&p.a;
85                 tmp->buf_size = ((f->buf_size + align - 1) / align) * align;
86         }
87         
88         memset(&tmp->list, 0, sizeof(tmp->list));
89
90         AST_RWLIST_INSERT_HEAD(&formats, tmp, list);
91         AST_RWLIST_UNLOCK(&formats);
92         ast_verb(2, "Registered file format %s, extension(s) %s\n", f->name, f->exts);
93
94         return 0;
95 }
96
97 int ast_format_unregister(const char *name)
98 {
99         struct ast_format *tmp;
100         int res = -1;
101
102         AST_RWLIST_WRLOCK(&formats);
103         AST_RWLIST_TRAVERSE_SAFE_BEGIN(&formats, tmp, list) {
104                 if (!strcasecmp(name, tmp->name)) {
105                         AST_RWLIST_REMOVE_CURRENT(list);
106                         ast_free(tmp);
107                         res = 0;
108                 }
109         }
110         AST_RWLIST_TRAVERSE_SAFE_END;
111         AST_RWLIST_UNLOCK(&formats);
112
113         if (!res)
114                 ast_verb(2, "Unregistered format %s\n", name);
115         else
116                 ast_log(LOG_WARNING, "Tried to unregister format %s, already unregistered\n", name);
117
118         return res;
119 }
120
121 int ast_stopstream(struct ast_channel *tmp)
122 {
123         ast_channel_lock(tmp);
124
125         /* Stop a running stream if there is one */
126         if (tmp->stream) {
127                 ast_closestream(tmp->stream);
128                 tmp->stream = NULL;
129                 if (tmp->oldwriteformat && ast_set_write_format(tmp, tmp->oldwriteformat))
130                         ast_log(LOG_WARNING, "Unable to restore format back to %d\n", tmp->oldwriteformat);
131         }
132         /* Stop the video stream too */
133         if (tmp->vstream != NULL) {
134                 ast_closestream(tmp->vstream);
135                 tmp->vstream = NULL;
136         }
137
138         ast_channel_unlock(tmp);
139
140         return 0;
141 }
142
143 int ast_writestream(struct ast_filestream *fs, struct ast_frame *f)
144 {
145         int res = -1;
146         int alt = 0;
147         if (f->frametype == AST_FRAME_VIDEO) {
148                 if (fs->fmt->format & AST_FORMAT_AUDIO_MASK) {
149                         /* This is the audio portion.  Call the video one... */
150                         if (!fs->vfs && fs->filename) {
151                                 const char *type = ast_getformatname(f->subclass & ~0x1);
152                                 fs->vfs = ast_writefile(fs->filename, type, NULL, fs->flags, 0, fs->mode);
153                                 ast_debug(1, "Opened video output file\n");
154                         }
155                         if (fs->vfs)
156                                 return ast_writestream(fs->vfs, f);
157                         /* else ignore */
158                         return 0;                               
159                 } else {
160                         /* Might / might not have mark set */
161                         alt = 1;
162                 }
163         } else if (f->frametype != AST_FRAME_VOICE) {
164                 ast_log(LOG_WARNING, "Tried to write non-voice frame\n");
165                 return -1;
166         }
167         if (((fs->fmt->format | alt) & f->subclass) == f->subclass) {
168                 res =  fs->fmt->write(fs, f);
169                 if (res < 0) 
170                         ast_log(LOG_WARNING, "Natural write failed\n");
171                 else if (res > 0)
172                         ast_log(LOG_WARNING, "Huh??\n");
173         } else {
174                 /* XXX If they try to send us a type of frame that isn't the normal frame, and isn't
175                        the one we've setup a translator for, we do the "wrong thing" XXX */
176                 if (fs->trans && f->subclass != fs->lastwriteformat) {
177                         ast_translator_free_path(fs->trans);
178                         fs->trans = NULL;
179                 }
180                 if (!fs->trans) 
181                         fs->trans = ast_translator_build_path(fs->fmt->format, f->subclass);
182                 if (!fs->trans)
183                         ast_log(LOG_WARNING, "Unable to translate to format %s, source format %s\n",
184                                 fs->fmt->name, ast_getformatname(f->subclass));
185                 else {
186                         struct ast_frame *trf;
187                         fs->lastwriteformat = f->subclass;
188                         /* Get the translated frame but don't consume the original in case they're using it on another stream */
189                         trf = ast_translate(fs->trans, f, 0);
190                         if (trf) {
191                                 res = fs->fmt->write(fs, trf);
192                                 ast_frfree(trf);
193                                 if (res) 
194                                         ast_log(LOG_WARNING, "Translated frame write failed\n");
195                         } else
196                                 res = 0;
197                 }
198         }
199         return res;
200 }
201
202 static int copy(const char *infile, const char *outfile)
203 {
204         int ifd, ofd, len;
205         char buf[4096]; /* XXX make it lerger. */
206
207         if ((ifd = open(infile, O_RDONLY)) < 0) {
208                 ast_log(LOG_WARNING, "Unable to open %s in read-only mode\n", infile);
209                 return -1;
210         }
211         if ((ofd = open(outfile, O_WRONLY | O_TRUNC | O_CREAT, AST_FILE_MODE)) < 0) {
212                 ast_log(LOG_WARNING, "Unable to open %s in write-only mode\n", outfile);
213                 close(ifd);
214                 return -1;
215         }
216         while ( (len = read(ifd, buf, sizeof(buf)) ) ) {
217                 int res;
218                 if (len < 0) {
219                         ast_log(LOG_WARNING, "Read failed on %s: %s\n", infile, strerror(errno));
220                         break;
221                 }
222                 /* XXX handle partial writes */
223                 res = write(ofd, buf, len);
224                 if (res != len) {
225                         ast_log(LOG_WARNING, "Write failed on %s (%d of %d): %s\n", outfile, res, len, strerror(errno));
226                         len = -1; /* error marker */
227                         break;
228                 }
229         }
230         close(ifd);
231         close(ofd);
232         if (len < 0) {
233                 unlink(outfile);
234                 return -1; /* error */
235         }
236         return 0;       /* success */
237 }
238
239 /*!
240  * \brief construct a filename. Absolute pathnames are preserved,
241  * relative names are prefixed by the sounds/ directory.
242  * The wav49 suffix is replaced by 'WAV'.
243  * Returns a malloc'ed string to be freed by the caller.
244  */
245 static char *build_filename(const char *filename, const char *ext)
246 {
247         char *fn = NULL;
248
249         if (!strcmp(ext, "wav49"))
250                 ext = "WAV";
251
252         if (filename[0] == '/') {
253                 if (asprintf(&fn, "%s.%s", filename, ext) < 0) {
254                         ast_log(LOG_WARNING, "asprintf() failed: %s\n", strerror(errno));
255                         fn = NULL;
256                 }
257         } else {
258                 if (asprintf(&fn, "%s/sounds/%s.%s",
259                              ast_config_AST_DATA_DIR, filename, ext) < 0) {
260                         ast_log(LOG_WARNING, "asprintf() failed: %s\n", strerror(errno));
261                         fn = NULL;
262                 }
263         }
264         return fn;
265 }
266
267 /* compare type against the list 'exts' */
268 /* XXX need a better algorithm */
269 static int exts_compare(const char *exts, const char *type)
270 {
271         char tmp[256];
272         char *stringp = tmp, *ext;
273
274         ast_copy_string(tmp, exts, sizeof(tmp));
275         while ((ext = strsep(&stringp, "|"))) {
276                 if (!strcmp(ext, type))
277                         return 1;
278         }
279
280         return 0;
281 }
282
283 static struct ast_filestream *get_filestream(struct ast_format *fmt, FILE *bfile)
284 {
285         struct ast_filestream *s;
286
287         int l = sizeof(*s) + fmt->buf_size + fmt->desc_size;    /* total allocation size */
288         if ( (s = ast_calloc(1, l)) == NULL)
289                 return NULL;
290         s->fmt = fmt;
291         s->f = bfile;
292
293         if (fmt->desc_size)
294                 s->_private = ((char *)(s + 1)) + fmt->buf_size;
295         if (fmt->buf_size)
296                 s->buf = (char *)(s + 1);
297         s->fr.src = fmt->name;
298         return s;
299 }
300
301 /*
302  * Default implementations of open and rewrite.
303  * Only use them if you don't have expensive stuff to do.
304  */
305 enum wrap_fn { WRAP_OPEN, WRAP_REWRITE };
306
307 static int fn_wrapper(struct ast_filestream *s, const char *comment, enum wrap_fn mode)
308 {
309         struct ast_format *f = s->fmt;
310         int ret = -1;
311
312         if (mode == WRAP_OPEN && f->open && f->open(s))
313                 ast_log(LOG_WARNING, "Unable to open format %s\n", f->name);
314         else if (mode == WRAP_REWRITE && f->rewrite && f->rewrite(s, comment))
315                 ast_log(LOG_WARNING, "Unable to rewrite format %s\n", f->name);
316         else {
317                 /* preliminary checks succeed. update usecount */
318                 ast_module_ref(f->module);
319                 ret = 0;
320         }
321         return ret;
322 }
323
324 static int rewrite_wrapper(struct ast_filestream *s, const char *comment)
325 {
326         return fn_wrapper(s, comment, WRAP_REWRITE);
327 }
328
329 static int open_wrapper(struct ast_filestream *s)
330 {
331         return fn_wrapper(s, NULL, WRAP_OPEN);
332 }
333
334 enum file_action {
335         ACTION_EXISTS = 1, /* return matching format if file exists, 0 otherwise */
336         ACTION_DELETE,  /* delete file, return 0 on success, -1 on error */
337         ACTION_RENAME,  /* rename file. return 0 on success, -1 on error */
338         ACTION_OPEN,
339         ACTION_COPY     /* copy file. return 0 on success, -1 on error */
340 };
341
342 /*!
343  * \brief perform various actions on a file. Second argument
344  * arg2 depends on the command:
345  *      unused for EXISTS and DELETE
346  *      destination file name (const char *) for COPY and RENAME
347  *      struct ast_channel * for OPEN
348  * if fmt is NULL, OPEN will return the first matching entry,
349  * whereas other functions will run on all matching entries.
350  */
351 static int ast_filehelper(const char *filename, const void *arg2, const char *fmt, const enum file_action action)
352 {
353         struct ast_format *f;
354         int res = (action == ACTION_EXISTS) ? 0 : -1;
355
356         AST_RWLIST_RDLOCK(&formats);
357         /* Check for a specific format */
358         AST_RWLIST_TRAVERSE(&formats, f, list) {
359                 char *stringp, *ext = NULL;
360
361                 if (fmt && !exts_compare(f->exts, fmt))
362                         continue;
363
364                 /* Look for a file matching the supported extensions.
365                  * The file must exist, and for OPEN, must match
366                  * one of the formats supported by the channel.
367                  */
368                 stringp = ast_strdupa(f->exts); /* this is in the stack so does not need to be freed */
369                 while ( (ext = strsep(&stringp, "|")) ) {
370                         struct stat st;
371                         char *fn = build_filename(filename, ext);
372
373                         if (fn == NULL)
374                                 continue;
375
376                         if ( stat(fn, &st) ) { /* file not existent */
377                                 ast_free(fn);
378                                 continue;
379                         }
380                         /* for 'OPEN' we need to be sure that the format matches
381                          * what the channel can process
382                          */
383                         if (action == ACTION_OPEN) {
384                                 struct ast_channel *chan = (struct ast_channel *)arg2;
385                                 FILE *bfile;
386                                 struct ast_filestream *s;
387
388                                 if ( !(chan->writeformat & f->format) &&
389                                      !((f->format & AST_FORMAT_AUDIO_MASK && fmt) ||
390                                           (f->format & AST_FORMAT_VIDEO_MASK && fmt))) {
391                                         ast_free(fn);
392                                         continue;       /* not a supported format */
393                                 }
394                                 if ( (bfile = fopen(fn, "r")) == NULL) {
395                                         ast_free(fn);
396                                         continue;       /* cannot open file */
397                                 }
398                                 s = get_filestream(f, bfile);
399                                 if (!s) {
400                                         fclose(bfile);
401                                         ast_free(fn);   /* cannot allocate descriptor */
402                                         continue;
403                                 }
404                                 if (open_wrapper(s)) {
405                                         fclose(bfile);
406                                         ast_free(fn);
407                                         ast_free(s);
408                                         continue;       /* cannot run open on file */
409                                 }
410                                 if (st.st_size == 0) {
411                                         ast_log(LOG_WARNING, "File %s detected to have zero size.\n", fn);
412                                 }
413                                 /* ok this is good for OPEN */
414                                 res = 1;        /* found */
415                                 s->lasttimeout = -1;
416                                 s->fmt = f;
417                                 s->trans = NULL;
418                                 s->filename = NULL;
419                                 if (s->fmt->format & AST_FORMAT_AUDIO_MASK) {
420                                         if (chan->stream)
421                                                 ast_closestream(chan->stream);
422                                         chan->stream = s;
423                                 } else {
424                                         if (chan->vstream)
425                                                 ast_closestream(chan->vstream);
426                                         chan->vstream = s;
427                                 }
428                                 ast_free(fn);
429                                 break;
430                         }
431                         switch (action) {
432                         case ACTION_OPEN:
433                                 break;  /* will never get here */
434
435                         case ACTION_EXISTS:     /* return the matching format */
436                                 res |= f->format;
437                                 break;
438
439                         case ACTION_DELETE:
440                                 if ( (res = unlink(fn)) )
441                                         ast_log(LOG_WARNING, "unlink(%s) failed: %s\n", fn, strerror(errno));
442                                 break;
443
444                         case ACTION_RENAME:
445                         case ACTION_COPY: {
446                                 char *nfn = build_filename((const char *)arg2, ext);
447                                 if (!nfn)
448                                         ast_log(LOG_WARNING, "Out of memory\n");
449                                 else {
450                                         res = action == ACTION_COPY ? copy(fn, nfn) : rename(fn, nfn);
451                                         if (res)
452                                                 ast_log(LOG_WARNING, "%s(%s,%s) failed: %s\n",
453                                                         action == ACTION_COPY ? "copy" : "rename",
454                                                          fn, nfn, strerror(errno));
455                                         ast_free(nfn);
456                                 }
457                             }
458                                 break;
459
460                         default:
461                                 ast_log(LOG_WARNING, "Unknown helper %d\n", action);
462                         }
463                         ast_free(fn);
464                 }
465         }
466         AST_RWLIST_UNLOCK(&formats);
467         return res;
468 }
469
470 static int is_absolute_path(const char *filename)
471 {
472         return filename[0] == '/';
473 }
474
475 static int fileexists_test(const char *filename, const char *fmt, const char *lang,
476                            char *buf, int buflen)
477 {
478         if (buf == NULL) {
479                 return -1;
480         }
481
482         if (ast_language_is_prefix && !is_absolute_path(filename)) { /* new layout */
483                 if (lang) {
484                         snprintf(buf, buflen, "%s/%s", lang, filename);
485                 } else {
486                         snprintf(buf, buflen, "%s", filename);
487                 }
488         } else { /* old layout */
489                 strcpy(buf, filename);  /* first copy the full string */
490                 if (lang) {
491                         /* insert the language and suffix if needed */
492                         const char *c = strrchr(filename, '/');
493                         int offset = c ? c - filename + 1 : 0;  /* points right after the last '/' */
494                         snprintf(buf + offset, buflen - offset, "%s/%s", lang, filename + offset);
495                 }
496         }
497
498         return ast_filehelper(buf, NULL, fmt, ACTION_EXISTS);
499 }
500
501 /*!
502  * \brief helper routine to locate a file with a given format
503  * and language preference.
504  * Try preflang, preflang with stripped '_' suffix, or NULL.
505  * In the standard asterisk, language goes just before the last component.
506  * In an alternative configuration, the language should be a prefix
507  * to the actual filename.
508  *
509  * The last parameter(s) point to a buffer of sufficient size,
510  * which on success is filled with the matching filename.
511  */
512 static int fileexists_core(const char *filename, const char *fmt, const char *preflang,
513                            char *buf, int buflen)
514 {
515         int res = -1;
516         char *lang = NULL;
517
518         if (buf == NULL) {
519                 return -1;
520         }
521
522         /* We try languages in the following order:
523          *    preflang (may include dialect)
524          *    lang (preflang without dialect - if any)
525          *    <none>
526          *    default (unless the same as preflang or lang without dialect)
527          */
528
529         /* Try preferred language */
530         if (!ast_strlen_zero(preflang)) {
531                 /* try the preflang exactly as it was requested */
532                 if ((res = fileexists_test(filename, fmt, preflang, buf, buflen)) > 0) {
533                         return res;
534                 } else {
535                         /* try without a dialect */
536                         char *postfix = NULL;
537                         postfix = lang = ast_strdupa(preflang);
538
539                         strsep(&postfix, "_");
540                         if (postfix) {
541                                 if ((res = fileexists_test(filename, fmt, lang, buf, buflen)) > 0) {
542                                         return res;
543                                 }
544                         }
545                 }
546         }
547
548         /* Try without any language */
549         if ((res = fileexists_test(filename, fmt, NULL, buf, buflen)) > 0) {
550                 return res;
551         }
552
553         /* Finally try the default language unless it was already tried before */
554         if ((ast_strlen_zero(preflang) || strcmp(preflang, DEFAULT_LANGUAGE)) && (ast_strlen_zero(lang) || strcmp(lang, DEFAULT_LANGUAGE))) {
555                 if ((res = fileexists_test(filename, fmt, DEFAULT_LANGUAGE, buf, buflen)) > 0) {
556                         return res;
557                 }
558         }
559
560         return 0;
561 }
562
563 struct ast_filestream *ast_openstream(struct ast_channel *chan, const char *filename, const char *preflang)
564 {
565         return ast_openstream_full(chan, filename, preflang, 0);
566 }
567
568 struct ast_filestream *ast_openstream_full(struct ast_channel *chan, const char *filename, const char *preflang, int asis)
569 {
570         /* 
571          * Use fileexists_core() to find a file in a compatible
572          * language and format, set up a suitable translator,
573          * and open the stream.
574          */
575         int fmts, res, buflen;
576         char *buf;
577
578         if (!asis) {
579                 /* do this first, otherwise we detect the wrong writeformat */
580                 ast_stopstream(chan);
581                 if (chan->generator)
582                         ast_deactivate_generator(chan);
583         }
584         if (preflang == NULL)
585                 preflang = "";
586         buflen = strlen(preflang) + strlen(filename) + 4;
587         buf = alloca(buflen);
588         if (buf == NULL)
589                 return NULL;
590         fmts = fileexists_core(filename, NULL, preflang, buf, buflen);
591         if (fmts > 0)
592                 fmts &= AST_FORMAT_AUDIO_MASK;
593         if (fmts < 1) {
594                 ast_log(LOG_WARNING, "File %s does not exist in any format\n", filename);
595                 return NULL;
596         }
597         chan->oldwriteformat = chan->writeformat;
598         /* Set the channel to a format we can work with */
599         res = ast_set_write_format(chan, fmts);
600         res = ast_filehelper(buf, chan, NULL, ACTION_OPEN);
601         if (res >= 0)
602                 return chan->stream;
603         return NULL;
604 }
605
606 struct ast_filestream *ast_openvstream(struct ast_channel *chan, const char *filename, const char *preflang)
607 {
608         /* As above, but for video. But here we don't have translators
609          * so we must enforce a format.
610          */
611         unsigned int format;
612         char *buf;
613         int buflen;
614
615         if (preflang == NULL)
616                 preflang = "";
617         buflen = strlen(preflang) + strlen(filename) + 4;
618         buf = alloca(buflen);
619         if (buf == NULL)
620                 return NULL;
621
622         for (format = AST_FORMAT_AUDIO_MASK + 1; format <= AST_FORMAT_VIDEO_MASK; format = format << 1) {
623                 int fd;
624                 const char *fmt;
625
626                 if (!(chan->nativeformats & format))
627                         continue;
628                 fmt = ast_getformatname(format);
629                 if ( fileexists_core(filename, fmt, preflang, buf, buflen) < 1) /* no valid format */
630                         continue;
631                 fd = ast_filehelper(buf, chan, fmt, ACTION_OPEN);
632                 if (fd >= 0)
633                         return chan->vstream;
634                 ast_log(LOG_WARNING, "File %s has video but couldn't be opened\n", filename);
635         }
636         return NULL;
637 }
638
639 struct ast_frame *ast_readframe(struct ast_filestream *s)
640 {
641         struct ast_frame *f = NULL;
642         int whennext = 0;       
643         if (s && s->fmt)
644                 f = s->fmt->read(s, &whennext);
645         return f;
646 }
647
648 enum fsread_res {
649         FSREAD_FAILURE,
650         FSREAD_SUCCESS_SCHED,
651         FSREAD_SUCCESS_NOSCHED,
652 };
653
654 static int ast_fsread_audio(const void *data);
655
656 static enum fsread_res ast_readaudio_callback(struct ast_filestream *s)
657 {
658         int whennext = 0;
659
660         while (!whennext) {
661                 struct ast_frame *fr;
662                 
663                 if (s->orig_chan_name && strcasecmp(s->owner->name, s->orig_chan_name))
664                         goto return_failure;
665                 
666                 fr = s->fmt->read(s, &whennext);
667                 if (!fr /* stream complete */ || ast_write(s->owner, fr) /* error writing */) {
668                         if (fr)
669                                 ast_log(LOG_WARNING, "Failed to write frame\n");
670                         goto return_failure;
671                 }
672         }
673         if (whennext != s->lasttimeout) {
674                 if (s->owner->timingfd > -1) {
675                         float samp_rate = (float) ast_format_rate(s->fmt->format);
676                         unsigned int rate;
677
678                         rate = (unsigned int) roundf(samp_rate / ((float) whennext));
679
680                         ast_settimeout(s->owner, rate, ast_fsread_audio, s);
681                 } else {
682                         s->owner->streamid = ast_sched_add(s->owner->sched, 
683                                 whennext / (ast_format_rate(s->fmt->format) / 1000), ast_fsread_audio, s);
684                 }
685                 s->lasttimeout = whennext;
686                 return FSREAD_SUCCESS_NOSCHED;
687         }
688         return FSREAD_SUCCESS_SCHED;
689
690 return_failure:
691         s->owner->streamid = -1;
692         ast_settimeout(s->owner, 0, NULL, NULL);
693         return FSREAD_FAILURE;
694 }
695
696 static int ast_fsread_audio(const void *data)
697 {
698         struct ast_filestream *fs = (struct ast_filestream *)data;
699         enum fsread_res res;
700
701         res = ast_readaudio_callback(fs);
702
703         if (res == FSREAD_SUCCESS_SCHED)
704                 return 1;
705         
706         return 0;
707 }
708
709 static int ast_fsread_video(const void *data);
710
711 static enum fsread_res ast_readvideo_callback(struct ast_filestream *s)
712 {
713         int whennext = 0;
714
715         while (!whennext) {
716                 struct ast_frame *fr = s->fmt->read(s, &whennext);
717                 if (!fr || ast_write(s->owner, fr)) { /* no stream or error, as above */
718                         if (fr)
719                                 ast_log(LOG_WARNING, "Failed to write frame\n");
720                         s->owner->vstreamid = -1;
721                         return FSREAD_FAILURE;
722                 }
723         }
724
725         if (whennext != s->lasttimeout) {
726                 s->owner->vstreamid = ast_sched_add(s->owner->sched, 
727                         whennext / (ast_format_rate(s->fmt->format) / 1000), 
728                         ast_fsread_video, s);
729                 s->lasttimeout = whennext;
730                 return FSREAD_SUCCESS_NOSCHED;
731         }
732
733         return FSREAD_SUCCESS_SCHED;
734 }
735
736 static int ast_fsread_video(const void *data)
737 {
738         struct ast_filestream *fs = (struct ast_filestream *)data;
739         enum fsread_res res;
740
741         res = ast_readvideo_callback(fs);
742
743         if (res == FSREAD_SUCCESS_SCHED)
744                 return 1;
745         
746         return 0;
747 }
748
749 int ast_applystream(struct ast_channel *chan, struct ast_filestream *s)
750 {
751         s->owner = chan;
752         return 0;
753 }
754
755 int ast_playstream(struct ast_filestream *s)
756 {
757         enum fsread_res res;
758
759         if (s->fmt->format & AST_FORMAT_AUDIO_MASK)
760                 res = ast_readaudio_callback(s);
761         else
762                 res = ast_readvideo_callback(s);
763
764         return (res == FSREAD_FAILURE) ? -1 : 0;
765 }
766
767 int ast_seekstream(struct ast_filestream *fs, off_t sample_offset, int whence)
768 {
769         return fs->fmt->seek(fs, sample_offset, whence);
770 }
771
772 int ast_truncstream(struct ast_filestream *fs)
773 {
774         return fs->fmt->trunc(fs);
775 }
776
777 off_t ast_tellstream(struct ast_filestream *fs)
778 {
779         return fs->fmt->tell(fs);
780 }
781
782 int ast_stream_fastforward(struct ast_filestream *fs, off_t ms)
783 {
784         return ast_seekstream(fs, ms * DEFAULT_SAMPLES_PER_MS, SEEK_CUR);
785 }
786
787 int ast_stream_rewind(struct ast_filestream *fs, off_t ms)
788 {
789         return ast_seekstream(fs, -ms * DEFAULT_SAMPLES_PER_MS, SEEK_CUR);
790 }
791
792 int ast_closestream(struct ast_filestream *f)
793 {
794         char *cmd = NULL;
795         size_t size = 0;
796         /* Stop a running stream if there is one */
797         if (f->owner) {
798                 if (f->fmt->format & AST_FORMAT_AUDIO_MASK) {
799                         f->owner->stream = NULL;
800                         AST_SCHED_DEL(f->owner->sched, f->owner->streamid);
801                         ast_settimeout(f->owner, 0, NULL, NULL);
802                 } else {
803                         f->owner->vstream = NULL;
804                         AST_SCHED_DEL(f->owner->sched, f->owner->vstreamid);
805                 }
806         }
807         /* destroy the translator on exit */
808         if (f->trans)
809                 ast_translator_free_path(f->trans);
810
811         if (f->realfilename && f->filename) {
812                         size = strlen(f->filename) + strlen(f->realfilename) + 15;
813                         cmd = alloca(size);
814                         memset(cmd, 0, size);
815                         snprintf(cmd, size, "/bin/mv -f %s %s", f->filename, f->realfilename);
816                         ast_safe_system(cmd);
817         }
818
819         if (f->fmt->close) {
820                 f->fmt->close(f);
821         }
822         if (f->filename)
823                 ast_free(f->filename);
824         if (f->realfilename)
825                 ast_free(f->realfilename);
826         fclose(f->f);
827         if (f->vfs)
828                 ast_closestream(f->vfs);
829         if (f->orig_chan_name)
830                 free((void *) f->orig_chan_name);
831         if (f->write_buffer) {
832                 ast_free(f->write_buffer);
833         }
834
835         ast_module_unref(f->fmt->module);
836         ast_free(f);
837         return 0;
838 }
839
840
841 /*
842  * Look the various language-specific places where a file could exist.
843  */
844 int ast_fileexists(const char *filename, const char *fmt, const char *preflang)
845 {
846         char *buf;
847         int buflen;
848
849         if (preflang == NULL)
850                 preflang = "";
851         buflen = strlen(preflang) + strlen(filename) + 4;       /* room for everything */
852         buf = alloca(buflen);
853         if (buf == NULL)
854                 return 0;
855         return fileexists_core(filename, fmt, preflang, buf, buflen);
856 }
857
858 int ast_filedelete(const char *filename, const char *fmt)
859 {
860         return ast_filehelper(filename, NULL, fmt, ACTION_DELETE);
861 }
862
863 int ast_filerename(const char *filename, const char *filename2, const char *fmt)
864 {
865         return ast_filehelper(filename, filename2, fmt, ACTION_RENAME);
866 }
867
868 int ast_filecopy(const char *filename, const char *filename2, const char *fmt)
869 {
870         return ast_filehelper(filename, filename2, fmt, ACTION_COPY);
871 }
872
873 int ast_streamfile(struct ast_channel *chan, const char *filename, const char *preflang)
874 {
875         struct ast_filestream *fs;
876         struct ast_filestream *vfs=NULL;
877         char fmt[256];
878         int seekattempt;
879
880         fs = ast_openstream(chan, filename, preflang);
881
882         /* check to see if there is any data present (not a zero length file),
883          * done this way because there is no where for ast_openstream_full to
884          * return the file had no data. */
885         seekattempt = fseek(fs->f, -1, SEEK_END);
886         if (!seekattempt)
887                 fseek(fs->f, 0, SEEK_SET);
888         else
889                 return 0;
890
891         if (fs)
892                 vfs = ast_openvstream(chan, filename, preflang);
893         if (vfs) {
894                 ast_debug(1, "Ooh, found a video stream, too, format %s\n", ast_getformatname(vfs->fmt->format));
895         }
896         if (fs){
897                 int res;
898                 if (ast_test_flag(chan, AST_FLAG_MASQ_NOSTREAM))
899                         fs->orig_chan_name = ast_strdup(chan->name);
900                 if (ast_applystream(chan, fs))
901                         return -1;
902                 if (vfs && ast_applystream(chan, vfs))
903                         return -1;
904                 res = ast_playstream(fs);
905                 if (!res && vfs)
906                         res = ast_playstream(vfs);
907                 ast_verb(3, "<%s> Playing '%s.%s' (language '%s')\n", chan->name, filename, ast_getformatname(chan->writeformat), preflang ? preflang : "default");
908
909                 return res;
910         }
911         ast_log(LOG_WARNING, "Unable to open %s (format %s): %s\n", filename, ast_getformatname_multiple(fmt, sizeof(fmt), chan->nativeformats), strerror(errno));
912         return -1;
913 }
914
915 struct ast_filestream *ast_readfile(const char *filename, const char *type, const char *comment, int flags, int check, mode_t mode)
916 {
917         FILE *bfile;
918         struct ast_format *f;
919         struct ast_filestream *fs = NULL;
920         char *fn;
921         int format_found = 0;   
922
923         AST_RWLIST_RDLOCK(&formats);
924
925         AST_RWLIST_TRAVERSE(&formats, f, list) {
926                 fs = NULL;
927                 if (!exts_compare(f->exts, type))
928                         continue;
929                 else 
930                         format_found = 1;
931
932                 fn = build_filename(filename, type);
933                 errno = 0;
934                 bfile = fopen(fn, "r");
935
936                 if (!bfile || (fs = get_filestream(f, bfile)) == NULL || open_wrapper(fs) ) {
937                         ast_log(LOG_WARNING, "Unable to open %s\n", fn);
938                         if (fs)
939                                 ast_free(fs);
940                         fs = NULL;
941                         if (bfile)
942                                 fclose(bfile);
943                         ast_free(fn);
944                         break;                          
945                 }
946                 /* found it */
947                 fs->trans = NULL;
948                 fs->fmt = f;
949                 fs->flags = flags;
950                 fs->mode = mode;
951                 fs->filename = ast_strdup(filename);
952                 fs->vfs = NULL;
953                 break;
954         }
955
956         AST_RWLIST_UNLOCK(&formats);
957         if (!format_found)
958                 ast_log(LOG_WARNING, "No such format '%s'\n", type);
959
960         return fs;
961 }
962
963 struct ast_filestream *ast_writefile(const char *filename, const char *type, const char *comment, int flags, int check, mode_t mode)
964 {
965         int fd, myflags = 0;
966         /* compiler claims this variable can be used before initialization... */
967         FILE *bfile = NULL;
968         struct ast_format *f;
969         struct ast_filestream *fs = NULL;
970         char *buf = NULL;
971         size_t size = 0;
972         int format_found = 0;
973
974         AST_RWLIST_RDLOCK(&formats);
975
976         /* set the O_TRUNC flag if and only if there is no O_APPEND specified */
977         /* We really can't use O_APPEND as it will break WAV header updates */
978         if (flags & O_APPEND) { 
979                 flags &= ~O_APPEND;
980         } else {
981                 myflags = O_TRUNC;
982         }
983         
984         myflags |= O_WRONLY | O_CREAT;
985
986         /* XXX need to fix this - we should just do the fopen,
987          * not open followed by fdopen()
988          */
989         AST_RWLIST_TRAVERSE(&formats, f, list) {
990                 char *fn, *orig_fn = NULL;
991                 if (fs)
992                         break;
993
994                 if (!exts_compare(f->exts, type))
995                         continue;
996                 else
997                         format_found = 1;
998
999                 fn = build_filename(filename, type);
1000                 fd = open(fn, flags | myflags, mode);
1001                 if (fd > -1) {
1002                         /* fdopen() the resulting file stream */
1003                         bfile = fdopen(fd, ((flags | myflags) & O_RDWR) ? "w+" : "w");
1004                         if (!bfile) {
1005                                 ast_log(LOG_WARNING, "Whoa, fdopen failed: %s!\n", strerror(errno));
1006                                 close(fd);
1007                                 fd = -1;
1008                         }
1009                 }
1010                 
1011                 if (ast_opt_cache_record_files && (fd > -1)) {
1012                         char *c;
1013
1014                         fclose(bfile);  /* this also closes fd */
1015                         /*
1016                           We touch orig_fn just as a place-holder so other things (like vmail) see the file is there.
1017                           What we are really doing is writing to record_cache_dir until we are done then we will mv the file into place.
1018                         */
1019                         orig_fn = ast_strdupa(fn);
1020                         for (c = fn; *c; c++)
1021                                 if (*c == '/')
1022                                         *c = '_';
1023
1024                         size = strlen(fn) + strlen(record_cache_dir) + 2;
1025                         buf = alloca(size);
1026                         strcpy(buf, record_cache_dir);
1027                         strcat(buf, "/");
1028                         strcat(buf, fn);
1029                         ast_free(fn);
1030                         fn = buf;
1031                         fd = open(fn, flags | myflags, mode);
1032                         if (fd > -1) {
1033                                 /* fdopen() the resulting file stream */
1034                                 bfile = fdopen(fd, ((flags | myflags) & O_RDWR) ? "w+" : "w");
1035                                 if (!bfile) {
1036                                         ast_log(LOG_WARNING, "Whoa, fdopen failed: %s!\n", strerror(errno));
1037                                         close(fd);
1038                                         fd = -1;
1039                                 }
1040                         }
1041                 }
1042                 if (fd > -1) {
1043                         errno = 0;
1044                         fs = get_filestream(f, bfile);
1045                         if (!fs || rewrite_wrapper(fs, comment)) {
1046                                 ast_log(LOG_WARNING, "Unable to rewrite %s\n", fn);
1047                                 close(fd);
1048                                 if (orig_fn) {
1049                                         unlink(fn);
1050                                         unlink(orig_fn);
1051                                 }
1052                                 if (fs)
1053                                         ast_free(fs);
1054                                 fs = NULL;
1055                                 continue;
1056                         }
1057                         fs->trans = NULL;
1058                         fs->fmt = f;
1059                         fs->flags = flags;
1060                         fs->mode = mode;
1061                         if (orig_fn) {
1062                                 fs->realfilename = ast_strdup(orig_fn);
1063                                 fs->filename = ast_strdup(fn);
1064                         } else {
1065                                 fs->realfilename = NULL;
1066                                 fs->filename = ast_strdup(filename);
1067                         }
1068                         fs->vfs = NULL;
1069                         /* If truncated, we'll be at the beginning; if not truncated, then append */
1070
1071                         if ((fs->write_buffer = ast_malloc(32768))){
1072                                 setvbuf(fs->f, fs->write_buffer, _IOFBF, 32768);
1073                         }
1074
1075                         f->seek(fs, 0, SEEK_END);
1076                 } else if (errno != EEXIST) {
1077                         ast_log(LOG_WARNING, "Unable to open file %s: %s\n", fn, strerror(errno));
1078                         if (orig_fn)
1079                                 unlink(orig_fn);
1080                 }
1081                 /* if buf != NULL then fn is already free and pointing to it */
1082                 if (!buf)
1083                         ast_free(fn);
1084         }
1085
1086         AST_RWLIST_UNLOCK(&formats);
1087
1088         if (!format_found)
1089                 ast_log(LOG_WARNING, "No such format '%s'\n", type);
1090
1091         return fs;
1092 }
1093
1094 /*!
1095  * \brief the core of all waitstream() functions
1096  */
1097 static int waitstream_core(struct ast_channel *c, const char *breakon,
1098         const char *forward, const char *reverse, int skip_ms,
1099         int audiofd, int cmdfd,  const char *context)
1100 {
1101         const char *orig_chan_name = NULL;
1102         int err = 0;
1103
1104         if (!breakon)
1105                 breakon = "";
1106         if (!forward)
1107                 forward = "";
1108         if (!reverse)
1109                 reverse = "";
1110
1111         /* Switch the channel to end DTMF frame only. waitstream_core doesn't care about the start of DTMF. */
1112         ast_set_flag(c, AST_FLAG_END_DTMF_ONLY);
1113
1114         if (ast_test_flag(c, AST_FLAG_MASQ_NOSTREAM))
1115                 orig_chan_name = ast_strdupa(c->name);
1116
1117         while (c->stream) {
1118                 int res;
1119                 int ms;
1120
1121                 if (orig_chan_name && strcasecmp(orig_chan_name, c->name)) {
1122                         ast_stopstream(c);
1123                         err = 1;
1124                         break;
1125                 }
1126
1127                 ms = ast_sched_wait(c->sched);
1128
1129                 if (ms < 0 && !c->timingfunc) {
1130                         ast_stopstream(c);
1131                         break;
1132                 }
1133                 if (ms < 0)
1134                         ms = 1000;
1135                 if (cmdfd < 0) {
1136                         res = ast_waitfor(c, ms);
1137                         if (res < 0) {
1138                                 ast_log(LOG_WARNING, "Select failed (%s)\n", strerror(errno));
1139                                 ast_clear_flag(c, AST_FLAG_END_DTMF_ONLY);
1140                                 return res;
1141                         }
1142                 } else {
1143                         int outfd;
1144                         struct ast_channel *rchan = ast_waitfor_nandfds(&c, 1, &cmdfd, (cmdfd > -1) ? 1 : 0, NULL, &outfd, &ms);
1145                         if (!rchan && (outfd < 0) && (ms)) {
1146                                 /* Continue */
1147                                 if (errno == EINTR)
1148                                         continue;
1149                                 ast_log(LOG_WARNING, "Wait failed (%s)\n", strerror(errno));
1150                                 ast_clear_flag(c, AST_FLAG_END_DTMF_ONLY);
1151                                 return -1;
1152                         } else if (outfd > -1) { /* this requires cmdfd set */
1153                                 /* The FD we were watching has something waiting */
1154                                 ast_clear_flag(c, AST_FLAG_END_DTMF_ONLY);
1155                                 return 1;
1156                         }
1157                         /* if rchan is set, it is 'c' */
1158                         res = rchan ? 1 : 0; /* map into 'res' values */
1159                 }
1160                 if (res > 0) {
1161                         struct ast_frame *fr = ast_read(c);
1162                         if (!fr) {
1163                                 ast_clear_flag(c, AST_FLAG_END_DTMF_ONLY);
1164                                 return -1;
1165                         }
1166                         switch (fr->frametype) {
1167                         case AST_FRAME_DTMF_END:
1168                                 if (context) {
1169                                         const char exten[2] = { fr->subclass, '\0' };
1170                                         if (ast_exists_extension(c, context, exten, 1, c->cid.cid_num)) {
1171                                                 res = fr->subclass;
1172                                                 ast_frfree(fr);
1173                                                 ast_clear_flag(c, AST_FLAG_END_DTMF_ONLY);
1174                                                 return res;
1175                                         }
1176                                 } else {
1177                                         res = fr->subclass;
1178                                         if (strchr(forward, res)) {
1179                                                 ast_stream_fastforward(c->stream, skip_ms);
1180                                         } else if (strchr(reverse, res)) {
1181                                                 ast_stream_rewind(c->stream, skip_ms);
1182                                         } else if (strchr(breakon, res)) {
1183                                                 ast_frfree(fr);
1184                                                 ast_clear_flag(c, AST_FLAG_END_DTMF_ONLY);
1185                                                 return res;
1186                                         }                                       
1187                                 }
1188                                 break;
1189                         case AST_FRAME_CONTROL:
1190                                 switch (fr->subclass) {
1191                                 case AST_CONTROL_HANGUP:
1192                                 case AST_CONTROL_BUSY:
1193                                 case AST_CONTROL_CONGESTION:
1194                                         ast_frfree(fr);
1195                                         ast_clear_flag(c, AST_FLAG_END_DTMF_ONLY);
1196                                         return -1;
1197                                 case AST_CONTROL_RINGING:
1198                                 case AST_CONTROL_ANSWER:
1199                                 case AST_CONTROL_VIDUPDATE:
1200                                 case AST_CONTROL_SRCUPDATE:
1201                                 case AST_CONTROL_HOLD:
1202                                 case AST_CONTROL_UNHOLD:
1203                                         /* Unimportant */
1204                                         break;
1205                                 default:
1206                                         ast_log(LOG_WARNING, "Unexpected control subclass '%d'\n", fr->subclass);
1207                                 }
1208                                 break;
1209                         case AST_FRAME_VOICE:
1210                                 /* Write audio if appropriate */
1211                                 if (audiofd > -1) {
1212                                         if (write(audiofd, fr->data.ptr, fr->datalen) < 0) {
1213                                                 ast_log(LOG_WARNING, "write() failed: %s\n", strerror(errno));
1214                                         }
1215                                 }
1216                         default:
1217                                 /* Ignore all others */
1218                                 break;
1219                         }
1220                         ast_frfree(fr);
1221                 }
1222                 ast_sched_runq(c->sched);
1223         }
1224
1225         ast_clear_flag(c, AST_FLAG_END_DTMF_ONLY);
1226
1227         return (err || c->_softhangup) ? -1 : 0;
1228 }
1229
1230 int ast_waitstream_fr(struct ast_channel *c, const char *breakon, const char *forward, const char *reverse, int ms)
1231 {
1232         return waitstream_core(c, breakon, forward, reverse, ms,
1233                 -1 /* no audiofd */, -1 /* no cmdfd */, NULL /* no context */);
1234 }
1235
1236 int ast_waitstream(struct ast_channel *c, const char *breakon)
1237 {
1238         return waitstream_core(c, breakon, NULL, NULL, 0, -1, -1, NULL);
1239 }
1240
1241 int ast_waitstream_full(struct ast_channel *c, const char *breakon, int audiofd, int cmdfd)
1242 {
1243         return waitstream_core(c, breakon, NULL, NULL, 0,
1244                 audiofd, cmdfd, NULL /* no context */);
1245 }
1246
1247 int ast_waitstream_exten(struct ast_channel *c, const char *context)
1248 {
1249         /* Waitstream, with return in the case of a valid 1 digit extension */
1250         /* in the current or specified context being pressed */
1251
1252         if (!context)
1253                 context = c->context;
1254         return waitstream_core(c, NULL, NULL, NULL, 0,
1255                 -1, -1, context);
1256 }
1257
1258 /*
1259  * if the file name is non-empty, try to play it.
1260  * Return 0 if success, -1 if error, digit if interrupted by a digit.
1261  * If digits == "" then we can simply check for non-zero.
1262  */
1263 int ast_stream_and_wait(struct ast_channel *chan, const char *file, const char *digits)
1264 {
1265         int res = 0;
1266         if (!ast_strlen_zero(file)) {
1267                 res = ast_streamfile(chan, file, chan->language);
1268                 if (!res) {
1269                         res = ast_waitstream(chan, digits);
1270                 }
1271         }
1272         return res;
1273
1274
1275 static char *handle_cli_core_show_file_formats(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1276 {
1277 #define FORMAT "%-10s %-10s %-20s\n"
1278 #define FORMAT2 "%-10s %-10s %-20s\n"
1279         struct ast_format *f;
1280         int count_fmt = 0;
1281
1282         switch (cmd) {
1283         case CLI_INIT:
1284                 e->command = "core show file formats";
1285                 e->usage =
1286                         "Usage: core show file formats\n"
1287                         "       Displays currently registered file formats (if any).\n";
1288                 return NULL;
1289         case CLI_GENERATE:
1290                 return NULL;
1291         }
1292
1293         if (a->argc != 4)
1294                 return CLI_SHOWUSAGE;
1295
1296         ast_cli(a->fd, FORMAT, "Format", "Name", "Extensions");
1297         ast_cli(a->fd, FORMAT, "------", "----", "----------");
1298
1299         AST_RWLIST_RDLOCK(&formats);
1300         AST_RWLIST_TRAVERSE(&formats, f, list) {
1301                 ast_cli(a->fd, FORMAT2, ast_getformatname(f->format), f->name, f->exts);
1302                 count_fmt++;
1303         }
1304         AST_RWLIST_UNLOCK(&formats);
1305         ast_cli(a->fd, "%d file formats registered.\n", count_fmt);
1306         return CLI_SUCCESS;
1307 #undef FORMAT
1308 #undef FORMAT2
1309 }
1310
1311 struct ast_cli_entry cli_file[] = {
1312         AST_CLI_DEFINE(handle_cli_core_show_file_formats, "Displays file formats")
1313 };
1314
1315 int ast_file_init(void)
1316 {
1317         ast_cli_register_multiple(cli_file, sizeof(cli_file) / sizeof(struct ast_cli_entry));
1318         return 0;
1319 }