2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2006, Digium, Inc.
6 * Mark Spencer <markster@digium.com>
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.
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.
21 * \brief Generic File Format Support.
23 * \author Mark Spencer <markster@digium.com>
28 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
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"
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.
55 int ast_language_is_prefix = 1;
57 static AST_RWLIST_HEAD_STATIC(formats, ast_format);
59 int __ast_format_register(const struct ast_format *f, struct ast_module *mod)
61 struct ast_format *tmp;
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);
71 if (!(tmp = ast_calloc(1, sizeof(*tmp)))) {
72 AST_RWLIST_UNLOCK(&formats);
79 * Align buf_size properly, rounding up to the machine-specific
80 * alignment for pointers.
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;
87 memset(&tmp->list, 0, sizeof(tmp->list));
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);
96 int ast_format_unregister(const char *name)
98 struct ast_format *tmp;
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);
109 AST_RWLIST_TRAVERSE_SAFE_END;
110 AST_RWLIST_UNLOCK(&formats);
113 ast_verb(2, "Unregistered format %s\n", name);
115 ast_log(LOG_WARNING, "Tried to unregister format %s, already unregistered\n", name);
120 int ast_stopstream(struct ast_channel *tmp)
122 ast_channel_lock(tmp);
124 /* Stop a running stream if there is one */
126 ast_closestream(tmp->stream);
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);
131 /* Stop the video stream too */
132 if (tmp->vstream != NULL) {
133 ast_closestream(tmp->vstream);
137 ast_channel_unlock(tmp);
142 int ast_writestream(struct ast_filestream *fs, struct ast_frame *f)
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");
155 return ast_writestream(fs->vfs, f);
159 /* Might / might not have mark set */
162 } else if (f->frametype != AST_FRAME_VOICE) {
163 ast_log(LOG_WARNING, "Tried to write non-voice frame\n");
166 if (((fs->fmt->format | alt) & f->subclass) == f->subclass) {
167 res = fs->fmt->write(fs, f);
169 ast_log(LOG_WARNING, "Natural write failed\n");
171 ast_log(LOG_WARNING, "Huh??\n");
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);
180 fs->trans = ast_translator_build_path(fs->fmt->format, f->subclass);
182 ast_log(LOG_WARNING, "Unable to translate to format %s, source format %s\n",
183 fs->fmt->name, ast_getformatname(f->subclass));
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);
190 res = fs->fmt->write(fs, trf);
193 ast_log(LOG_WARNING, "Translated frame write failed\n");
201 static int copy(const char *infile, const char *outfile)
204 char buf[4096]; /* XXX make it lerger. */
206 if ((ifd = open(infile, O_RDONLY)) < 0) {
207 ast_log(LOG_WARNING, "Unable to open %s in read-only mode\n", infile);
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);
215 while ( (len = read(ifd, buf, sizeof(buf)) ) ) {
218 ast_log(LOG_WARNING, "Read failed on %s: %s\n", infile, strerror(errno));
221 /* XXX handle partial writes */
222 res = write(ofd, buf, 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 */
233 return -1; /* error */
235 return 0; /* success */
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.
244 static char *build_filename(const char *filename, const char *ext)
248 if (!strcmp(ext, "wav49"))
251 if (filename[0] == '/')
252 asprintf(&fn, "%s.%s", filename, ext);
254 asprintf(&fn, "%s/sounds/%s.%s",
255 ast_config_AST_DATA_DIR, filename, ext);
259 /* compare type against the list 'exts' */
260 /* XXX need a better algorithm */
261 static int exts_compare(const char *exts, const char *type)
264 char *stringp = tmp, *ext;
266 ast_copy_string(tmp, exts, sizeof(tmp));
267 while ((ext = strsep(&stringp, "|"))) {
268 if (!strcmp(ext, type))
275 static struct ast_filestream *get_filestream(struct ast_format *fmt, FILE *bfile)
277 struct ast_filestream *s;
279 int l = sizeof(*s) + fmt->buf_size + fmt->desc_size; /* total allocation size */
280 if ( (s = ast_calloc(1, l)) == NULL)
286 s->_private = ((char *)(s + 1)) + fmt->buf_size;
288 s->buf = (char *)(s + 1);
289 s->fr.src = fmt->name;
294 * Default implementations of open and rewrite.
295 * Only use them if you don't have expensive stuff to do.
297 enum wrap_fn { WRAP_OPEN, WRAP_REWRITE };
299 static int fn_wrapper(struct ast_filestream *s, const char *comment, enum wrap_fn mode)
301 struct ast_format *f = s->fmt;
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);
309 /* preliminary checks succeed. update usecount */
310 ast_module_ref(f->module);
316 static int rewrite_wrapper(struct ast_filestream *s, const char *comment)
318 return fn_wrapper(s, comment, WRAP_REWRITE);
321 static int open_wrapper(struct ast_filestream *s)
323 return fn_wrapper(s, NULL, WRAP_OPEN);
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 */
331 ACTION_COPY /* copy file. return 0 on success, -1 on error */
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.
343 static int ast_filehelper(const char *filename, const void *arg2, const char *fmt, const enum file_action action)
345 struct ast_format *f;
346 int res = (action == ACTION_EXISTS) ? 0 : -1;
348 AST_RWLIST_RDLOCK(&formats);
349 /* Check for a specific format */
350 AST_RWLIST_TRAVERSE(&formats, f, list) {
351 char *stringp, *ext = NULL;
353 if (fmt && !exts_compare(f->exts, fmt))
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.
360 stringp = ast_strdupa(f->exts); /* this is in the stack so does not need to be freed */
361 while ( (ext = strsep(&stringp, "|")) ) {
363 char *fn = build_filename(filename, ext);
368 if ( stat(fn, &st) ) { /* file not existent */
372 /* for 'OPEN' we need to be sure that the format matches
373 * what the channel can process
375 if (action == ACTION_OPEN) {
376 struct ast_channel *chan = (struct ast_channel *)arg2;
378 struct ast_filestream *s;
380 if ( !(chan->writeformat & f->format) &&
381 !(f->format & AST_FORMAT_AUDIO_MASK && fmt)) {
383 continue; /* not a supported format */
385 if ( (bfile = fopen(fn, "r")) == NULL) {
387 continue; /* cannot open file */
389 s = get_filestream(f, bfile);
392 ast_free(fn); /* cannot allocate descriptor */
395 if (open_wrapper(s)) {
399 continue; /* cannot run open on file */
401 /* ok this is good for OPEN */
407 if (s->fmt->format & AST_FORMAT_AUDIO_MASK) {
409 ast_closestream(chan->stream);
413 ast_closestream(chan->vstream);
421 break; /* will never get here */
423 case ACTION_EXISTS: /* return the matching format */
428 if ( (res = unlink(fn)) )
429 ast_log(LOG_WARNING, "unlink(%s) failed: %s\n", fn, strerror(errno));
434 char *nfn = build_filename((const char *)arg2, ext);
436 ast_log(LOG_WARNING, "Out of memory\n");
438 res = action == ACTION_COPY ? copy(fn, nfn) : rename(fn, nfn);
440 ast_log(LOG_WARNING, "%s(%s,%s) failed: %s\n",
441 action == ACTION_COPY ? "copy" : "rename",
442 fn, nfn, strerror(errno));
449 ast_log(LOG_WARNING, "Unknown helper %d\n", action);
454 AST_RWLIST_UNLOCK(&formats);
458 static int is_absolute_path(const char *filename)
460 return filename[0] == '/';
463 static int fileexists_test(const char *filename, const char *fmt, const char *lang,
464 char *buf, int buflen)
470 if (ast_language_is_prefix) { /* new layout */
472 snprintf(buf, buflen, "%s/%s", lang, filename);
474 snprintf(buf, buflen, "%s", filename);
476 } else { /* old layout */
477 strcpy(buf, filename); /* first copy the full string */
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);
486 return ast_filehelper(buf, NULL, fmt, ACTION_EXISTS);
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.
497 * The last parameter(s) point to a buffer of sufficient size,
498 * which on success is filled with the matching filename.
500 static int fileexists_core(const char *filename, const char *fmt, const char *preflang,
501 char *buf, int buflen)
510 if (is_absolute_path(filename)) {
511 ast_copy_string(buf, filename, buflen);
512 return ast_filehelper(buf, NULL, fmt, ACTION_EXISTS);
515 /* We try languages in the following order:
516 * preflang (may include dialect)
517 * lang (preflang without dialect - if any)
519 * default (unless the same as preflang or lang without dialect)
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) {
528 /* try without a dialect */
529 char *postfix = NULL;
530 postfix = lang = ast_strdupa(preflang);
532 strsep(&postfix, "_");
534 if ((res = fileexists_test(filename, fmt, lang, buf, buflen)) > 0) {
541 /* Try without any language */
542 if ((res = fileexists_test(filename, fmt, NULL, buf, buflen)) > 0) {
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) {
556 struct ast_filestream *ast_openstream(struct ast_channel *chan, const char *filename, const char *preflang)
558 return ast_openstream_full(chan, filename, preflang, 0);
561 struct ast_filestream *ast_openstream_full(struct ast_channel *chan, const char *filename, const char *preflang, int asis)
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.
568 int fmts, res, buflen;
572 /* do this first, otherwise we detect the wrong writeformat */
573 ast_stopstream(chan);
575 ast_deactivate_generator(chan);
577 if (preflang == NULL)
579 buflen = strlen(preflang) + strlen(filename) + 4;
580 buf = alloca(buflen);
583 fmts = fileexists_core(filename, NULL, preflang, buf, buflen);
585 fmts &= AST_FORMAT_AUDIO_MASK;
587 ast_log(LOG_WARNING, "File %s does not exist in any format\n", filename);
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);
599 struct ast_filestream *ast_openvstream(struct ast_channel *chan, const char *filename, const char *preflang)
601 /* As above, but for video. But here we don't have translators
602 * so we must enforce a format.
608 if (preflang == NULL)
610 buflen = strlen(preflang) + strlen(filename) + 4;
611 buf = alloca(buflen);
615 for (format = AST_FORMAT_AUDIO_MASK + 1; format <= AST_FORMAT_VIDEO_MASK; format = format << 1) {
619 if (!(chan->nativeformats & format))
621 fmt = ast_getformatname(format);
622 if ( fileexists_core(filename, fmt, preflang, buf, buflen) < 1) /* no valid format */
624 fd = ast_filehelper(buf, chan, fmt, ACTION_OPEN);
626 return chan->vstream;
627 ast_log(LOG_WARNING, "File %s has video but couldn't be opened\n", filename);
632 struct ast_frame *ast_readframe(struct ast_filestream *s)
634 struct ast_frame *f = NULL;
637 f = s->fmt->read(s, &whennext);
643 FSREAD_SUCCESS_SCHED,
644 FSREAD_SUCCESS_NOSCHED,
647 static int ast_fsread_audio(const void *data);
649 static enum fsread_res ast_readaudio_callback(struct ast_filestream *s)
654 struct ast_frame *fr;
656 if (s->orig_chan_name && strcasecmp(s->owner->name, s->orig_chan_name))
659 fr = s->fmt->read(s, &whennext);
660 if (!fr /* stream complete */ || ast_write(s->owner, fr) /* error writing */) {
662 ast_log(LOG_WARNING, "Failed to write frame\n");
666 if (whennext != s->lasttimeout) {
668 if (s->owner->timingfd > -1)
669 ast_settimeout(s->owner, whennext, ast_fsread_audio, s);
672 s->owner->streamid = ast_sched_add(s->owner->sched,
673 whennext / (ast_format_rate(s->fmt->format) / 1000), ast_fsread_audio, s);
674 s->lasttimeout = whennext;
675 return FSREAD_SUCCESS_NOSCHED;
677 return FSREAD_SUCCESS_SCHED;
680 s->owner->streamid = -1;
682 ast_settimeout(s->owner, 0, NULL, NULL);
684 return FSREAD_FAILURE;
687 static int ast_fsread_audio(const void *data)
689 struct ast_filestream *fs = (struct ast_filestream *)data;
692 res = ast_readaudio_callback(fs);
694 if (res == FSREAD_SUCCESS_SCHED)
700 static int ast_fsread_video(const void *data);
702 static enum fsread_res ast_readvideo_callback(struct ast_filestream *s)
707 struct ast_frame *fr = s->fmt->read(s, &whennext);
708 if (!fr || ast_write(s->owner, fr)) { /* no stream or error, as above */
710 ast_log(LOG_WARNING, "Failed to write frame\n");
711 s->owner->vstreamid = -1;
712 return FSREAD_FAILURE;
716 if (whennext != s->lasttimeout) {
717 s->owner->vstreamid = ast_sched_add(s->owner->sched,
718 whennext / (ast_format_rate(s->fmt->format) / 1000),
719 ast_fsread_video, s);
720 s->lasttimeout = whennext;
721 return FSREAD_SUCCESS_NOSCHED;
724 return FSREAD_SUCCESS_SCHED;
727 static int ast_fsread_video(const void *data)
729 struct ast_filestream *fs = (struct ast_filestream *)data;
732 res = ast_readvideo_callback(fs);
734 if (res == FSREAD_SUCCESS_SCHED)
740 int ast_applystream(struct ast_channel *chan, struct ast_filestream *s)
746 int ast_playstream(struct ast_filestream *s)
750 if (s->fmt->format & AST_FORMAT_AUDIO_MASK)
751 res = ast_readaudio_callback(s);
753 res = ast_readvideo_callback(s);
755 return (res == FSREAD_FAILURE) ? -1 : 0;
758 int ast_seekstream(struct ast_filestream *fs, off_t sample_offset, int whence)
760 return fs->fmt->seek(fs, sample_offset, whence);
763 int ast_truncstream(struct ast_filestream *fs)
765 return fs->fmt->trunc(fs);
768 off_t ast_tellstream(struct ast_filestream *fs)
770 return fs->fmt->tell(fs);
773 int ast_stream_fastforward(struct ast_filestream *fs, off_t ms)
775 return ast_seekstream(fs, ms * DEFAULT_SAMPLES_PER_MS, SEEK_CUR);
778 int ast_stream_rewind(struct ast_filestream *fs, off_t ms)
780 return ast_seekstream(fs, -ms * DEFAULT_SAMPLES_PER_MS, SEEK_CUR);
783 int ast_closestream(struct ast_filestream *f)
787 /* Stop a running stream if there is one */
789 if (f->fmt->format & AST_FORMAT_AUDIO_MASK) {
790 f->owner->stream = NULL;
791 AST_SCHED_DEL(f->owner->sched, f->owner->streamid);
793 ast_settimeout(f->owner, 0, NULL, NULL);
796 f->owner->vstream = NULL;
797 AST_SCHED_DEL(f->owner->sched, f->owner->vstreamid);
800 /* destroy the translator on exit */
802 ast_translator_free_path(f->trans);
804 if (f->realfilename && f->filename) {
805 size = strlen(f->filename) + strlen(f->realfilename) + 15;
807 memset(cmd, 0, size);
808 snprintf(cmd, size, "/bin/mv -f %s %s", f->filename, f->realfilename);
809 ast_safe_system(cmd);
813 ast_free(f->filename);
815 ast_free(f->realfilename);
820 ast_closestream(f->vfs);
821 if (f->orig_chan_name)
822 free((void *) f->orig_chan_name);
823 ast_module_unref(f->fmt->module);
830 * Look the various language-specific places where a file could exist.
832 int ast_fileexists(const char *filename, const char *fmt, const char *preflang)
837 if (preflang == NULL)
839 buflen = strlen(preflang) + strlen(filename) + 4; /* room for everything */
840 buf = alloca(buflen);
843 return fileexists_core(filename, fmt, preflang, buf, buflen);
846 int ast_filedelete(const char *filename, const char *fmt)
848 return ast_filehelper(filename, NULL, fmt, ACTION_DELETE);
851 int ast_filerename(const char *filename, const char *filename2, const char *fmt)
853 return ast_filehelper(filename, filename2, fmt, ACTION_RENAME);
856 int ast_filecopy(const char *filename, const char *filename2, const char *fmt)
858 return ast_filehelper(filename, filename2, fmt, ACTION_COPY);
861 int ast_streamfile(struct ast_channel *chan, const char *filename, const char *preflang)
863 struct ast_filestream *fs;
864 struct ast_filestream *vfs=NULL;
867 fs = ast_openstream(chan, filename, preflang);
869 vfs = ast_openvstream(chan, filename, preflang);
871 ast_debug(1, "Ooh, found a video stream, too, format %s\n", ast_getformatname(vfs->fmt->format));
875 if (ast_test_flag(chan, AST_FLAG_MASQ_NOSTREAM))
876 fs->orig_chan_name = ast_strdup(chan->name);
877 if (ast_applystream(chan, fs))
879 if (vfs && ast_applystream(chan, vfs))
881 res = ast_playstream(fs);
883 res = ast_playstream(vfs);
884 ast_verb(3, "<%s> Playing '%s.%s' (language '%s')\n", chan->name, filename, ast_getformatname(chan->writeformat), preflang ? preflang : "default");
888 ast_log(LOG_WARNING, "Unable to open %s (format %s): %s\n", filename, ast_getformatname_multiple(fmt, sizeof(fmt), chan->nativeformats), strerror(errno));
892 struct ast_filestream *ast_readfile(const char *filename, const char *type, const char *comment, int flags, int check, mode_t mode)
895 struct ast_format *f;
896 struct ast_filestream *fs = NULL;
898 int format_found = 0;
900 AST_RWLIST_RDLOCK(&formats);
902 AST_RWLIST_TRAVERSE(&formats, f, list) {
904 if (!exts_compare(f->exts, type))
909 fn = build_filename(filename, type);
911 bfile = fopen(fn, "r");
913 if (!bfile || (fs = get_filestream(f, bfile)) == NULL || open_wrapper(fs) ) {
914 ast_log(LOG_WARNING, "Unable to open %s\n", fn);
927 fs->filename = ast_strdup(filename);
932 AST_RWLIST_UNLOCK(&formats);
934 ast_log(LOG_WARNING, "No such format '%s'\n", type);
939 struct ast_filestream *ast_writefile(const char *filename, const char *type, const char *comment, int flags, int check, mode_t mode)
942 /* compiler claims this variable can be used before initialization... */
944 struct ast_format *f;
945 struct ast_filestream *fs = NULL;
948 int format_found = 0;
950 AST_RWLIST_RDLOCK(&formats);
952 /* set the O_TRUNC flag if and only if there is no O_APPEND specified */
953 /* We really can't use O_APPEND as it will break WAV header updates */
954 if (flags & O_APPEND) {
960 myflags |= O_WRONLY | O_CREAT;
962 /* XXX need to fix this - we should just do the fopen,
963 * not open followed by fdopen()
965 AST_RWLIST_TRAVERSE(&formats, f, list) {
966 char *fn, *orig_fn = NULL;
970 if (!exts_compare(f->exts, type))
975 fn = build_filename(filename, type);
976 fd = open(fn, flags | myflags, mode);
978 /* fdopen() the resulting file stream */
979 bfile = fdopen(fd, ((flags | myflags) & O_RDWR) ? "w+" : "w");
981 ast_log(LOG_WARNING, "Whoa, fdopen failed: %s!\n", strerror(errno));
987 if (ast_opt_cache_record_files && (fd > -1)) {
990 fclose(bfile); /* this also closes fd */
992 We touch orig_fn just as a place-holder so other things (like vmail) see the file is there.
993 What we are really doing is writing to record_cache_dir until we are done then we will mv the file into place.
995 orig_fn = ast_strdupa(fn);
996 for (c = fn; *c; c++)
1000 size = strlen(fn) + strlen(record_cache_dir) + 2;
1002 strcpy(buf, record_cache_dir);
1007 fd = open(fn, flags | myflags, mode);
1009 /* fdopen() the resulting file stream */
1010 bfile = fdopen(fd, ((flags | myflags) & O_RDWR) ? "w+" : "w");
1012 ast_log(LOG_WARNING, "Whoa, fdopen failed: %s!\n", strerror(errno));
1020 fs = get_filestream(f, bfile);
1021 if (!fs || rewrite_wrapper(fs, comment)) {
1022 ast_log(LOG_WARNING, "Unable to rewrite %s\n", fn);
1038 fs->realfilename = ast_strdup(orig_fn);
1039 fs->filename = ast_strdup(fn);
1041 fs->realfilename = NULL;
1042 fs->filename = ast_strdup(filename);
1045 /* If truncated, we'll be at the beginning; if not truncated, then append */
1046 f->seek(fs, 0, SEEK_END);
1047 } else if (errno != EEXIST) {
1048 ast_log(LOG_WARNING, "Unable to open file %s: %s\n", fn, strerror(errno));
1052 /* if buf != NULL then fn is already free and pointing to it */
1057 AST_RWLIST_UNLOCK(&formats);
1060 ast_log(LOG_WARNING, "No such format '%s'\n", type);
1066 * \brief the core of all waitstream() functions
1068 static int waitstream_core(struct ast_channel *c, const char *breakon,
1069 const char *forward, const char *rewind, int skip_ms,
1070 int audiofd, int cmdfd, const char *context)
1072 const char *orig_chan_name = NULL;
1082 /* Switch the channel to end DTMF frame only. waitstream_core doesn't care about the start of DTMF. */
1083 ast_set_flag(c, AST_FLAG_END_DTMF_ONLY);
1085 if (ast_test_flag(c, AST_FLAG_MASQ_NOSTREAM))
1086 orig_chan_name = ast_strdupa(c->name);
1092 if (orig_chan_name && strcasecmp(orig_chan_name, c->name)) {
1098 ms = ast_sched_wait(c->sched);
1100 if (ms < 0 && !c->timingfunc) {
1107 res = ast_waitfor(c, ms);
1109 ast_log(LOG_WARNING, "Select failed (%s)\n", strerror(errno));
1110 ast_clear_flag(c, AST_FLAG_END_DTMF_ONLY);
1115 struct ast_channel *rchan = ast_waitfor_nandfds(&c, 1, &cmdfd, (cmdfd > -1) ? 1 : 0, NULL, &outfd, &ms);
1116 if (!rchan && (outfd < 0) && (ms)) {
1120 ast_log(LOG_WARNING, "Wait failed (%s)\n", strerror(errno));
1121 ast_clear_flag(c, AST_FLAG_END_DTMF_ONLY);
1123 } else if (outfd > -1) { /* this requires cmdfd set */
1124 /* The FD we were watching has something waiting */
1125 ast_clear_flag(c, AST_FLAG_END_DTMF_ONLY);
1128 /* if rchan is set, it is 'c' */
1129 res = rchan ? 1 : 0; /* map into 'res' values */
1132 struct ast_frame *fr = ast_read(c);
1134 ast_clear_flag(c, AST_FLAG_END_DTMF_ONLY);
1137 switch (fr->frametype) {
1138 case AST_FRAME_DTMF_END:
1140 const char exten[2] = { fr->subclass, '\0' };
1141 if (ast_exists_extension(c, context, exten, 1, c->cid.cid_num)) {
1144 ast_clear_flag(c, AST_FLAG_END_DTMF_ONLY);
1149 if (strchr(forward, res)) {
1150 ast_stream_fastforward(c->stream, skip_ms);
1151 } else if (strchr(rewind, res)) {
1152 ast_stream_rewind(c->stream, skip_ms);
1153 } else if (strchr(breakon, res)) {
1155 ast_clear_flag(c, AST_FLAG_END_DTMF_ONLY);
1160 case AST_FRAME_CONTROL:
1161 switch (fr->subclass) {
1162 case AST_CONTROL_HANGUP:
1163 case AST_CONTROL_BUSY:
1164 case AST_CONTROL_CONGESTION:
1166 ast_clear_flag(c, AST_FLAG_END_DTMF_ONLY);
1168 case AST_CONTROL_RINGING:
1169 case AST_CONTROL_ANSWER:
1170 case AST_CONTROL_VIDUPDATE:
1171 case AST_CONTROL_SRCUPDATE:
1172 case AST_CONTROL_HOLD:
1173 case AST_CONTROL_UNHOLD:
1177 ast_log(LOG_WARNING, "Unexpected control subclass '%d'\n", fr->subclass);
1180 case AST_FRAME_VOICE:
1181 /* Write audio if appropriate */
1183 write(audiofd, fr->data, fr->datalen);
1185 /* Ignore all others */
1190 ast_sched_runq(c->sched);
1193 ast_clear_flag(c, AST_FLAG_END_DTMF_ONLY);
1195 return (err || c->_softhangup) ? -1 : 0;
1198 int ast_waitstream_fr(struct ast_channel *c, const char *breakon, const char *forward, const char *rewind, int ms)
1200 return waitstream_core(c, breakon, forward, rewind, ms,
1201 -1 /* no audiofd */, -1 /* no cmdfd */, NULL /* no context */);
1204 int ast_waitstream(struct ast_channel *c, const char *breakon)
1206 return waitstream_core(c, breakon, NULL, NULL, 0, -1, -1, NULL);
1209 int ast_waitstream_full(struct ast_channel *c, const char *breakon, int audiofd, int cmdfd)
1211 return waitstream_core(c, breakon, NULL, NULL, 0,
1212 audiofd, cmdfd, NULL /* no context */);
1215 int ast_waitstream_exten(struct ast_channel *c, const char *context)
1217 /* Waitstream, with return in the case of a valid 1 digit extension */
1218 /* in the current or specified context being pressed */
1221 context = c->context;
1222 return waitstream_core(c, NULL, NULL, NULL, 0,
1227 * if the file name is non-empty, try to play it.
1228 * Return 0 if success, -1 if error, digit if interrupted by a digit.
1229 * If digits == "" then we can simply check for non-zero.
1231 int ast_stream_and_wait(struct ast_channel *chan, const char *file, const char *digits)
1234 if (!ast_strlen_zero(file)) {
1235 res = ast_streamfile(chan, file, chan->language);
1237 res = ast_waitstream(chan, digits);
1243 static char *handle_cli_core_show_file_formats(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1245 #define FORMAT "%-10s %-10s %-20s\n"
1246 #define FORMAT2 "%-10s %-10s %-20s\n"
1247 struct ast_format *f;
1252 e->command = "core show file formats";
1254 "Usage: core show file formats\n"
1255 " Displays currently registered file formats (if any).\n";
1262 return CLI_SHOWUSAGE;
1264 ast_cli(a->fd, FORMAT, "Format", "Name", "Extensions");
1265 ast_cli(a->fd, FORMAT, "------", "----", "----------");
1267 AST_RWLIST_RDLOCK(&formats);
1268 AST_RWLIST_TRAVERSE(&formats, f, list) {
1269 ast_cli(a->fd, FORMAT2, ast_getformatname(f->format), f->name, f->exts);
1272 AST_RWLIST_UNLOCK(&formats);
1273 ast_cli(a->fd, "%d file formats registered.\n", count_fmt);
1279 struct ast_cli_entry cli_file[] = {
1280 AST_CLI_DEFINE(handle_cli_core_show_file_formats, "Displays file formats")
1283 int ast_file_init(void)
1285 ast_cli_register_multiple(cli_file, sizeof(cli_file) / sizeof(struct ast_cli_entry));