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$")
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"
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.
56 int ast_language_is_prefix = 1;
58 static AST_RWLIST_HEAD_STATIC(formats, ast_format);
60 int __ast_format_register(const struct ast_format *f, struct ast_module *mod)
62 struct ast_format *tmp;
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);
72 if (!(tmp = ast_calloc(1, sizeof(*tmp)))) {
73 AST_RWLIST_UNLOCK(&formats);
80 * Align buf_size properly, rounding up to the machine-specific
81 * alignment for pointers.
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;
88 memset(&tmp->list, 0, sizeof(tmp->list));
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);
97 int ast_format_unregister(const char *name)
99 struct ast_format *tmp;
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);
110 AST_RWLIST_TRAVERSE_SAFE_END;
111 AST_RWLIST_UNLOCK(&formats);
114 ast_verb(2, "Unregistered format %s\n", name);
116 ast_log(LOG_WARNING, "Tried to unregister format %s, already unregistered\n", name);
121 int ast_stopstream(struct ast_channel *tmp)
123 /* Stop a running stream if there is one */
125 ast_closestream(tmp->stream);
127 if (tmp->oldwriteformat && ast_set_write_format(tmp, tmp->oldwriteformat))
128 ast_log(LOG_WARNING, "Unable to restore format back to %d\n", tmp->oldwriteformat);
130 /* Stop the video stream too */
131 if (tmp->vstream != NULL) {
132 ast_closestream(tmp->vstream);
138 int ast_writestream(struct ast_filestream *fs, struct ast_frame *f)
142 if (f->frametype == AST_FRAME_VIDEO) {
143 if (fs->fmt->format & AST_FORMAT_AUDIO_MASK) {
144 /* This is the audio portion. Call the video one... */
145 if (!fs->vfs && fs->filename) {
146 const char *type = ast_getformatname(f->subclass & ~0x1);
147 fs->vfs = ast_writefile(fs->filename, type, NULL, fs->flags, 0, fs->mode);
148 ast_debug(1, "Opened video output file\n");
151 return ast_writestream(fs->vfs, f);
155 /* Might / might not have mark set */
158 } else if (f->frametype != AST_FRAME_VOICE) {
159 ast_log(LOG_WARNING, "Tried to write non-voice frame\n");
162 if (((fs->fmt->format | alt) & f->subclass) == f->subclass) {
163 res = fs->fmt->write(fs, f);
165 ast_log(LOG_WARNING, "Natural write failed\n");
167 ast_log(LOG_WARNING, "Huh??\n");
169 /* XXX If they try to send us a type of frame that isn't the normal frame, and isn't
170 the one we've setup a translator for, we do the "wrong thing" XXX */
171 if (fs->trans && f->subclass != fs->lastwriteformat) {
172 ast_translator_free_path(fs->trans);
176 fs->trans = ast_translator_build_path(fs->fmt->format, f->subclass);
178 ast_log(LOG_WARNING, "Unable to translate to format %s, source format %s\n",
179 fs->fmt->name, ast_getformatname(f->subclass));
181 struct ast_frame *trf;
182 fs->lastwriteformat = f->subclass;
183 /* Get the translated frame but don't consume the original in case they're using it on another stream */
184 trf = ast_translate(fs->trans, f, 0);
186 res = fs->fmt->write(fs, trf);
188 ast_log(LOG_WARNING, "Translated frame write failed\n");
196 static int copy(const char *infile, const char *outfile)
199 char buf[4096]; /* XXX make it lerger. */
201 if ((ifd = open(infile, O_RDONLY)) < 0) {
202 ast_log(LOG_WARNING, "Unable to open %s in read-only mode\n", infile);
205 if ((ofd = open(outfile, O_WRONLY | O_TRUNC | O_CREAT, AST_FILE_MODE)) < 0) {
206 ast_log(LOG_WARNING, "Unable to open %s in write-only mode\n", outfile);
210 while ( (len = read(ifd, buf, sizeof(buf)) ) ) {
213 ast_log(LOG_WARNING, "Read failed on %s: %s\n", infile, strerror(errno));
216 /* XXX handle partial writes */
217 res = write(ofd, buf, len);
219 ast_log(LOG_WARNING, "Write failed on %s (%d of %d): %s\n", outfile, res, len, strerror(errno));
220 len = -1; /* error marker */
228 return -1; /* error */
230 return 0; /* success */
234 * \brief construct a filename. Absolute pathnames are preserved,
235 * relative names are prefixed by the sounds/ directory.
236 * The wav49 suffix is replaced by 'WAV'.
237 * Returns a malloc'ed string to be freed by the caller.
239 static char *build_filename(const char *filename, const char *ext)
243 if (!strcmp(ext, "wav49"))
246 if (filename[0] == '/')
247 asprintf(&fn, "%s.%s", filename, ext);
249 asprintf(&fn, "%s/sounds/%s.%s",
250 ast_config_AST_DATA_DIR, filename, ext);
254 /* compare type against the list 'exts' */
255 /* XXX need a better algorithm */
256 static int exts_compare(const char *exts, const char *type)
259 char *stringp = tmp, *ext;
261 ast_copy_string(tmp, exts, sizeof(tmp));
262 while ((ext = strsep(&stringp, "|"))) {
263 if (!strcmp(ext, type))
270 static struct ast_filestream *get_filestream(struct ast_format *fmt, FILE *bfile)
272 struct ast_filestream *s;
274 int l = sizeof(*s) + fmt->buf_size + fmt->desc_size; /* total allocation size */
275 if ( (s = ast_calloc(1, l)) == NULL)
281 s->private = ((char *)(s+1)) + fmt->buf_size;
283 s->buf = (char *)(s+1);
284 s->fr.src = fmt->name;
289 * Default implementations of open and rewrite.
290 * Only use them if you don't have expensive stuff to do.
292 enum wrap_fn { WRAP_OPEN, WRAP_REWRITE };
294 static int fn_wrapper(struct ast_filestream *s, const char *comment, enum wrap_fn mode)
296 struct ast_format *f = s->fmt;
299 if (mode == WRAP_OPEN && f->open && f->open(s))
300 ast_log(LOG_WARNING, "Unable to open format %s\n", f->name);
301 else if (mode == WRAP_REWRITE && f->rewrite && f->rewrite(s, comment))
302 ast_log(LOG_WARNING, "Unable to rewrite format %s\n", f->name);
304 /* preliminary checks succeed. update usecount */
305 ast_module_ref(f->module);
311 static int rewrite_wrapper(struct ast_filestream *s, const char *comment)
313 return fn_wrapper(s, comment, WRAP_REWRITE);
316 static int open_wrapper(struct ast_filestream *s)
318 return fn_wrapper(s, NULL, WRAP_OPEN);
322 ACTION_EXISTS = 1, /* return matching format if file exists, 0 otherwise */
323 ACTION_DELETE, /* delete file, return 0 on success, -1 on error */
324 ACTION_RENAME, /* rename file. return 0 on success, -1 on error */
326 ACTION_COPY /* copy file. return 0 on success, -1 on error */
330 * \brief perform various actions on a file. Second argument
331 * arg2 depends on the command:
332 * unused for EXISTS and DELETE
333 * destination file name (const char *) for COPY and RENAME
334 * struct ast_channel * for OPEN
335 * if fmt is NULL, OPEN will return the first matching entry,
336 * whereas other functions will run on all matching entries.
338 static int ast_filehelper(const char *filename, const void *arg2, const char *fmt, const enum file_action action)
340 struct ast_format *f;
341 int res = (action == ACTION_EXISTS) ? 0 : -1;
343 AST_RWLIST_RDLOCK(&formats);
344 /* Check for a specific format */
345 AST_RWLIST_TRAVERSE(&formats, f, list) {
346 char *stringp, *ext = NULL;
348 if (fmt && !exts_compare(f->exts, fmt))
351 /* Look for a file matching the supported extensions.
352 * The file must exist, and for OPEN, must match
353 * one of the formats supported by the channel.
355 stringp = ast_strdupa(f->exts); /* this is in the stack so does not need to be freed */
356 while ( (ext = strsep(&stringp, "|")) ) {
358 char *fn = build_filename(filename, ext);
363 if ( stat(fn, &st) ) { /* file not existent */
367 /* for 'OPEN' we need to be sure that the format matches
368 * what the channel can process
370 if (action == ACTION_OPEN) {
371 struct ast_channel *chan = (struct ast_channel *)arg2;
373 struct ast_filestream *s;
375 if ( !(chan->writeformat & f->format) &&
376 !(f->format & AST_FORMAT_AUDIO_MASK && fmt)) {
378 continue; /* not a supported format */
380 if ( (bfile = fopen(fn, "r")) == NULL) {
382 continue; /* cannot open file */
384 s = get_filestream(f, bfile);
387 ast_free(fn); /* cannot allocate descriptor */
390 if (open_wrapper(s)) {
394 continue; /* cannot run open on file */
396 /* ok this is good for OPEN */
402 if (s->fmt->format & AST_FORMAT_AUDIO_MASK) {
404 ast_closestream(chan->stream);
408 ast_closestream(chan->vstream);
416 break; /* will never get here */
418 case ACTION_EXISTS: /* return the matching format */
423 if ( (res = unlink(fn)) )
424 ast_log(LOG_WARNING, "unlink(%s) failed: %s\n", fn, strerror(errno));
429 char *nfn = build_filename((const char *)arg2, ext);
431 ast_log(LOG_WARNING, "Out of memory\n");
433 res = action == ACTION_COPY ? copy(fn, nfn) : rename(fn, nfn);
435 ast_log(LOG_WARNING, "%s(%s,%s) failed: %s\n",
436 action == ACTION_COPY ? "copy" : "rename",
437 fn, nfn, strerror(errno));
444 ast_log(LOG_WARNING, "Unknown helper %d\n", action);
449 AST_RWLIST_UNLOCK(&formats);
454 * \brief helper routine to locate a file with a given format
455 * and language preference.
456 * Try preflang, preflang with stripped '_' suffix, or NULL.
457 * In the standard asterisk, language goes just before the last component.
458 * In an alternative configuration, the language should be a prefix
459 * to the actual filename.
461 * The last parameter(s) point to a buffer of sufficient size,
462 * which on success is filled with the matching filename.
464 static int fileexists_core(const char *filename, const char *fmt, const char *preflang,
465 char *buf, int buflen)
468 int langlen; /* length of language string */
469 const char *c = strrchr(filename, '/');
470 int offset = c ? c - filename + 1 : 0; /* points right after the last '/' */
472 if (preflang == NULL)
474 langlen = strlen(preflang);
476 if (buflen < langlen + strlen(filename) + 2) {
477 ast_log(LOG_WARNING, "buffer too small\n");
478 buf[0] = '\0'; /* set to empty */
479 buf = alloca(langlen + strlen(filename) + 2); /* room for everything */
485 if (ast_language_is_prefix) { /* new layout */
487 strcpy(buf, preflang);
489 strcpy(buf + langlen + 1, filename);
491 strcpy(buf, filename); /* first copy the full string */
492 } else { /* old layout */
493 strcpy(buf, filename); /* first copy the full string */
495 /* insert the language and suffix if needed */
496 strcpy(buf + offset, preflang);
497 sprintf(buf + offset + langlen, "/%s", filename + offset);
500 res = ast_filehelper(buf, NULL, fmt, ACTION_EXISTS);
501 if (res > 0) /* found format */
503 if (langlen == 0) /* no more formats */
505 if (preflang[langlen] == '_') /* we are on the local suffix */
506 langlen = 0; /* try again with no language */
508 langlen = (c = strchr(preflang, '_')) ? c - preflang : 0;
513 struct ast_filestream *ast_openstream(struct ast_channel *chan, const char *filename, const char *preflang)
515 return ast_openstream_full(chan, filename, preflang, 0);
518 struct ast_filestream *ast_openstream_full(struct ast_channel *chan, const char *filename, const char *preflang, int asis)
521 * Use fileexists_core() to find a file in a compatible
522 * language and format, set up a suitable translator,
523 * and open the stream.
525 int fmts, res, buflen;
529 /* do this first, otherwise we detect the wrong writeformat */
530 ast_stopstream(chan);
532 ast_deactivate_generator(chan);
534 if (preflang == NULL)
536 buflen = strlen(preflang) + strlen(filename) + 2;
537 buf = alloca(buflen);
540 fmts = fileexists_core(filename, NULL, preflang, buf, buflen);
542 fmts &= AST_FORMAT_AUDIO_MASK;
544 ast_log(LOG_WARNING, "File %s does not exist in any format\n", filename);
547 chan->oldwriteformat = chan->writeformat;
548 /* Set the channel to a format we can work with */
549 res = ast_set_write_format(chan, fmts);
550 res = ast_filehelper(buf, chan, NULL, ACTION_OPEN);
556 struct ast_filestream *ast_openvstream(struct ast_channel *chan, const char *filename, const char *preflang)
558 /* As above, but for video. But here we don't have translators
559 * so we must enforce a format.
565 if (preflang == NULL)
567 buflen = strlen(preflang) + strlen(filename) + 2;
568 buf = alloca(buflen);
572 for (format = AST_FORMAT_AUDIO_MASK + 1; format <= AST_FORMAT_VIDEO_MASK; format = format << 1) {
576 if (!(chan->nativeformats & format))
578 fmt = ast_getformatname(format);
579 if ( fileexists_core(filename, fmt, preflang, buf, buflen) < 1) /* no valid format */
581 fd = ast_filehelper(buf, chan, fmt, ACTION_OPEN);
583 return chan->vstream;
584 ast_log(LOG_WARNING, "File %s has video but couldn't be opened\n", filename);
589 struct ast_frame *ast_readframe(struct ast_filestream *s)
591 struct ast_frame *f = NULL;
594 f = s->fmt->read(s, &whennext);
600 FSREAD_SUCCESS_SCHED,
601 FSREAD_SUCCESS_NOSCHED,
604 static int ast_fsread_audio(const void *data);
606 static enum fsread_res ast_readaudio_callback(struct ast_filestream *s)
611 struct ast_frame *fr;
613 if (s->orig_chan_name && strcasecmp(s->owner->name, s->orig_chan_name))
616 fr = s->fmt->read(s, &whennext);
617 if (!fr /* stream complete */ || ast_write(s->owner, fr) /* error writing */) {
619 ast_log(LOG_WARNING, "Failed to write frame\n");
623 if (whennext != s->lasttimeout) {
625 if (s->owner->timingfd > -1)
626 ast_settimeout(s->owner, whennext, ast_fsread_audio, s);
629 s->owner->streamid = ast_sched_add(s->owner->sched, whennext/8, ast_fsread_audio, s);
630 s->lasttimeout = whennext;
631 return FSREAD_SUCCESS_NOSCHED;
633 return FSREAD_SUCCESS_SCHED;
636 s->owner->streamid = -1;
638 ast_settimeout(s->owner, 0, NULL, NULL);
640 return FSREAD_FAILURE;
643 static int ast_fsread_audio(const void *data)
645 struct ast_filestream *fs = (struct ast_filestream *)data;
648 res = ast_readaudio_callback(fs);
650 if (res == FSREAD_SUCCESS_SCHED)
656 static int ast_fsread_video(const void *data);
658 static enum fsread_res ast_readvideo_callback(struct ast_filestream *s)
663 struct ast_frame *fr = s->fmt->read(s, &whennext);
664 if (!fr || ast_write(s->owner, fr)) { /* no stream or error, as above */
666 ast_log(LOG_WARNING, "Failed to write frame\n");
667 s->owner->vstreamid = -1;
668 return FSREAD_FAILURE;
672 if (whennext != s->lasttimeout) {
673 s->owner->vstreamid = ast_sched_add(s->owner->sched, whennext / 8,
674 ast_fsread_video, s);
675 s->lasttimeout = whennext;
676 return FSREAD_SUCCESS_NOSCHED;
679 return FSREAD_SUCCESS_SCHED;
682 static int ast_fsread_video(const void *data)
684 struct ast_filestream *fs = (struct ast_filestream *)data;
687 res = ast_readvideo_callback(fs);
689 if (res == FSREAD_SUCCESS_SCHED)
695 int ast_applystream(struct ast_channel *chan, struct ast_filestream *s)
701 int ast_playstream(struct ast_filestream *s)
705 if (s->fmt->format & AST_FORMAT_AUDIO_MASK)
706 res = ast_readaudio_callback(s);
708 res = ast_readvideo_callback(s);
710 return (res == FSREAD_FAILURE) ? -1 : 0;
713 int ast_seekstream(struct ast_filestream *fs, off_t sample_offset, int whence)
715 return fs->fmt->seek(fs, sample_offset, whence);
718 int ast_truncstream(struct ast_filestream *fs)
720 return fs->fmt->trunc(fs);
723 off_t ast_tellstream(struct ast_filestream *fs)
725 return fs->fmt->tell(fs);
728 int ast_stream_fastforward(struct ast_filestream *fs, off_t ms)
730 return ast_seekstream(fs, ms * DEFAULT_SAMPLES_PER_MS, SEEK_CUR);
733 int ast_stream_rewind(struct ast_filestream *fs, off_t ms)
735 return ast_seekstream(fs, -ms * DEFAULT_SAMPLES_PER_MS, SEEK_CUR);
738 int ast_closestream(struct ast_filestream *f)
742 /* Stop a running stream if there is one */
744 if (f->fmt->format & AST_FORMAT_AUDIO_MASK) {
745 f->owner->stream = NULL;
746 if (f->owner->streamid > -1)
747 ast_sched_del(f->owner->sched, f->owner->streamid);
748 f->owner->streamid = -1;
750 ast_settimeout(f->owner, 0, NULL, NULL);
753 f->owner->vstream = NULL;
754 if (f->owner->vstreamid > -1)
755 ast_sched_del(f->owner->sched, f->owner->vstreamid);
756 f->owner->vstreamid = -1;
759 /* destroy the translator on exit */
761 ast_translator_free_path(f->trans);
763 if (f->realfilename && f->filename) {
764 size = strlen(f->filename) + strlen(f->realfilename) + 15;
767 snprintf(cmd,size,"/bin/mv -f %s %s",f->filename,f->realfilename);
768 ast_safe_system(cmd);
772 ast_free(f->filename);
774 ast_free(f->realfilename);
779 ast_closestream(f->vfs);
780 if (f->orig_chan_name)
781 free((void *) f->orig_chan_name);
782 ast_module_unref(f->fmt->module);
789 * Look the various language-specific places where a file could exist.
791 int ast_fileexists(const char *filename, const char *fmt, const char *preflang)
796 if (preflang == NULL)
798 buflen = strlen(preflang) + strlen(filename) + 2; /* room for everything */
799 buf = alloca(buflen);
802 return fileexists_core(filename, fmt, preflang, buf, buflen);
805 int ast_filedelete(const char *filename, const char *fmt)
807 return ast_filehelper(filename, NULL, fmt, ACTION_DELETE);
810 int ast_filerename(const char *filename, const char *filename2, const char *fmt)
812 return ast_filehelper(filename, filename2, fmt, ACTION_RENAME);
815 int ast_filecopy(const char *filename, const char *filename2, const char *fmt)
817 return ast_filehelper(filename, filename2, fmt, ACTION_COPY);
820 int ast_streamfile(struct ast_channel *chan, const char *filename, const char *preflang)
822 struct ast_filestream *fs;
823 struct ast_filestream *vfs=NULL;
826 fs = ast_openstream(chan, filename, preflang);
828 vfs = ast_openvstream(chan, filename, preflang);
830 ast_debug(1, "Ooh, found a video stream, too, format %s\n", ast_getformatname(vfs->fmt->format));
834 if (ast_test_flag(chan, AST_FLAG_MASQ_NOSTREAM))
835 fs->orig_chan_name = ast_strdup(chan->name);
836 if (ast_applystream(chan, fs))
838 if (vfs && ast_applystream(chan, vfs))
840 res = ast_playstream(fs);
842 res = ast_playstream(vfs);
843 ast_verb(3, "<%s> Playing '%s.%s' (language '%s')\n", chan->name, filename, ast_getformatname(chan->writeformat), preflang ? preflang : "default");
847 ast_log(LOG_WARNING, "Unable to open %s (format %s): %s\n", filename, ast_getformatname_multiple(fmt, sizeof(fmt), chan->nativeformats), strerror(errno));
851 struct ast_filestream *ast_readfile(const char *filename, const char *type, const char *comment, int flags, int check, mode_t mode)
854 struct ast_format *f;
855 struct ast_filestream *fs = NULL;
858 AST_RWLIST_RDLOCK(&formats);
860 AST_RWLIST_TRAVERSE(&formats, f, list) {
862 if (!exts_compare(f->exts, type))
865 fn = build_filename(filename, type);
867 bfile = fopen(fn, "r");
868 if (!bfile || (fs = get_filestream(f, bfile)) == NULL ||
870 ast_log(LOG_WARNING, "Unable to open %s\n", fn);
883 fs->filename = ast_strdup(filename);
888 AST_RWLIST_UNLOCK(&formats);
890 ast_log(LOG_WARNING, "No such format '%s'\n", type);
895 struct ast_filestream *ast_writefile(const char *filename, const char *type, const char *comment, int flags, int check, mode_t mode)
898 /* compiler claims this variable can be used before initialization... */
900 struct ast_format *f;
901 struct ast_filestream *fs = NULL;
904 int format_found = 0;
906 AST_RWLIST_RDLOCK(&formats);
908 /* set the O_TRUNC flag if and only if there is no O_APPEND specified */
909 /* We really can't use O_APPEND as it will break WAV header updates */
910 if (flags & O_APPEND) {
916 myflags |= O_WRONLY | O_CREAT;
918 /* XXX need to fix this - we should just do the fopen,
919 * not open followed by fdopen()
921 AST_RWLIST_TRAVERSE(&formats, f, list) {
922 char *fn, *orig_fn = NULL;
926 if (!exts_compare(f->exts, type))
931 fn = build_filename(filename, type);
932 fd = open(fn, flags | myflags, mode);
934 /* fdopen() the resulting file stream */
935 bfile = fdopen(fd, ((flags | myflags) & O_RDWR) ? "w+" : "w");
937 ast_log(LOG_WARNING, "Whoa, fdopen failed: %s!\n", strerror(errno));
943 if (ast_opt_cache_record_files && (fd > -1)) {
946 fclose(bfile); /* this also closes fd */
948 We touch orig_fn just as a place-holder so other things (like vmail) see the file is there.
949 What we are really doing is writing to record_cache_dir until we are done then we will mv the file into place.
951 orig_fn = ast_strdupa(fn);
952 for (c = fn; *c; c++)
956 size = strlen(fn) + strlen(record_cache_dir) + 2;
958 strcpy(buf, record_cache_dir);
963 fd = open(fn, flags | myflags, mode);
965 /* fdopen() the resulting file stream */
966 bfile = fdopen(fd, ((flags | myflags) & O_RDWR) ? "w+" : "w");
968 ast_log(LOG_WARNING, "Whoa, fdopen failed: %s!\n", strerror(errno));
976 fs = get_filestream(f, bfile);
977 if (!fs || rewrite_wrapper(fs, comment)) {
978 ast_log(LOG_WARNING, "Unable to rewrite %s\n", fn);
994 fs->realfilename = ast_strdup(orig_fn);
995 fs->filename = ast_strdup(fn);
997 fs->realfilename = NULL;
998 fs->filename = ast_strdup(filename);
1001 /* If truncated, we'll be at the beginning; if not truncated, then append */
1002 f->seek(fs, 0, SEEK_END);
1003 } else if (errno != EEXIST) {
1004 ast_log(LOG_WARNING, "Unable to open file %s: %s\n", fn, strerror(errno));
1008 /* if buf != NULL then fn is already free and pointing to it */
1013 AST_RWLIST_UNLOCK(&formats);
1016 ast_log(LOG_WARNING, "No such format '%s'\n", type);
1022 * \brief the core of all waitstream() functions
1024 static int waitstream_core(struct ast_channel *c, const char *breakon,
1025 const char *forward, const char *rewind, int skip_ms,
1026 int audiofd, int cmdfd, const char *context)
1028 const char *orig_chan_name = NULL;
1038 /* Switch the channel to end DTMF frame only. waitstream_core doesn't care about the start of DTMF. */
1039 ast_set_flag(c, AST_FLAG_END_DTMF_ONLY);
1041 if (ast_test_flag(c, AST_FLAG_MASQ_NOSTREAM))
1042 orig_chan_name = ast_strdupa(c->name);
1048 if (orig_chan_name && strcasecmp(orig_chan_name, c->name)) {
1054 ms = ast_sched_wait(c->sched);
1056 if (ms < 0 && !c->timingfunc) {
1063 res = ast_waitfor(c, ms);
1065 ast_log(LOG_WARNING, "Select failed (%s)\n", strerror(errno));
1066 ast_clear_flag(c, AST_FLAG_END_DTMF_ONLY);
1071 struct ast_channel *rchan = ast_waitfor_nandfds(&c, 1, &cmdfd, (cmdfd > -1) ? 1 : 0, NULL, &outfd, &ms);
1072 if (!rchan && (outfd < 0) && (ms)) {
1076 ast_log(LOG_WARNING, "Wait failed (%s)\n", strerror(errno));
1077 ast_clear_flag(c, AST_FLAG_END_DTMF_ONLY);
1079 } else if (outfd > -1) { /* this requires cmdfd set */
1080 /* The FD we were watching has something waiting */
1081 ast_clear_flag(c, AST_FLAG_END_DTMF_ONLY);
1084 /* if rchan is set, it is 'c' */
1085 res = rchan ? 1 : 0; /* map into 'res' values */
1088 struct ast_frame *fr = ast_read(c);
1090 ast_clear_flag(c, AST_FLAG_END_DTMF_ONLY);
1093 switch (fr->frametype) {
1094 case AST_FRAME_DTMF_END:
1096 const char exten[2] = { fr->subclass, '\0' };
1097 if (ast_exists_extension(c, context, exten, 1, c->cid.cid_num)) {
1100 ast_clear_flag(c, AST_FLAG_END_DTMF_ONLY);
1105 if (strchr(forward,res)) {
1106 ast_stream_fastforward(c->stream, skip_ms);
1107 } else if (strchr(rewind,res)) {
1108 ast_stream_rewind(c->stream, skip_ms);
1109 } else if (strchr(breakon, res)) {
1111 ast_clear_flag(c, AST_FLAG_END_DTMF_ONLY);
1116 case AST_FRAME_CONTROL:
1117 switch (fr->subclass) {
1118 case AST_CONTROL_HANGUP:
1119 case AST_CONTROL_BUSY:
1120 case AST_CONTROL_CONGESTION:
1122 ast_clear_flag(c, AST_FLAG_END_DTMF_ONLY);
1124 case AST_CONTROL_RINGING:
1125 case AST_CONTROL_ANSWER:
1126 case AST_CONTROL_VIDUPDATE:
1127 case AST_CONTROL_HOLD:
1128 case AST_CONTROL_UNHOLD:
1132 ast_log(LOG_WARNING, "Unexpected control subclass '%d'\n", fr->subclass);
1135 case AST_FRAME_VOICE:
1136 /* Write audio if appropriate */
1138 write(audiofd, fr->data, fr->datalen);
1140 /* Ignore all others */
1145 ast_sched_runq(c->sched);
1148 ast_clear_flag(c, AST_FLAG_END_DTMF_ONLY);
1150 return (err || c->_softhangup) ? -1 : 0;
1153 int ast_waitstream_fr(struct ast_channel *c, const char *breakon, const char *forward, const char *rewind, int ms)
1155 return waitstream_core(c, breakon, forward, rewind, ms,
1156 -1 /* no audiofd */, -1 /* no cmdfd */, NULL /* no context */);
1159 int ast_waitstream(struct ast_channel *c, const char *breakon)
1161 return waitstream_core(c, breakon, NULL, NULL, 0, -1, -1, NULL);
1164 int ast_waitstream_full(struct ast_channel *c, const char *breakon, int audiofd, int cmdfd)
1166 return waitstream_core(c, breakon, NULL, NULL, 0,
1167 audiofd, cmdfd, NULL /* no context */);
1170 int ast_waitstream_exten(struct ast_channel *c, const char *context)
1172 /* Waitstream, with return in the case of a valid 1 digit extension */
1173 /* in the current or specified context being pressed */
1176 context = c->context;
1177 return waitstream_core(c, NULL, NULL, NULL, 0,
1182 * if the file name is non-empty, try to play it.
1183 * Return 0 if success, -1 if error, digit if interrupted by a digit.
1184 * If digits == "" then we can simply check for non-zero.
1186 int ast_stream_and_wait(struct ast_channel *chan, const char *file, const char *digits)
1189 if (!ast_strlen_zero(file)) {
1190 res = ast_streamfile(chan, file, chan->language);
1192 res = ast_waitstream(chan, digits);
1197 static char *handle_cli_core_show_file_formats(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1199 #define FORMAT "%-10s %-10s %-20s\n"
1200 #define FORMAT2 "%-10s %-10s %-20s\n"
1201 struct ast_format *f;
1206 e->command = "core show file formats";
1208 "Usage: core show file formats\n"
1209 " Displays currently registered file formats (if any).\n";
1216 return CLI_SHOWUSAGE;
1218 ast_cli(a->fd, FORMAT, "Format", "Name", "Extensions");
1219 ast_cli(a->fd, FORMAT, "------", "----", "----------");
1221 AST_RWLIST_RDLOCK(&formats);
1222 AST_RWLIST_TRAVERSE(&formats, f, list) {
1223 ast_cli(a->fd, FORMAT2, ast_getformatname(f->format), f->name, f->exts);
1226 AST_RWLIST_UNLOCK(&formats);
1227 ast_cli(a->fd, "%d file formats registered.\n", count_fmt);
1233 struct ast_cli_entry cli_file[] = {
1234 AST_CLI_DEFINE(handle_cli_core_show_file_formats, "Displays file formats")
1237 int ast_file_init(void)
1239 ast_cli_register_multiple(cli_file, sizeof(cli_file) / sizeof(struct ast_cli_entry));