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