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$")
30 #include <sys/types.h>
38 #include <sys/types.h>
41 #include "asterisk/frame.h"
42 #include "asterisk/file.h"
43 #include "asterisk/cli.h"
44 #include "asterisk/logger.h"
45 #include "asterisk/channel.h"
46 #include "asterisk/sched.h"
47 #include "asterisk/options.h"
48 #include "asterisk/translate.h"
49 #include "asterisk/utils.h"
50 #include "asterisk/lock.h"
51 #include "asterisk/app.h"
52 #include "asterisk/pbx.h"
53 #include "asterisk/linkedlists.h"
54 #include "asterisk/module.h"
57 * The following variable controls the layout of localized sound files.
58 * If 0, use the historical layout with prefix just before the filename
59 * (i.e. digits/en/1.gsm , digits/it/1.gsm or default to digits/1.gsm),
60 * if 1 put the prefix at the beginning of the filename
61 * (i.e. en/digits/1.gsm, it/digits/1.gsm or default to digits/1.gsm).
62 * The latter permits a language to be entirely in one directory.
64 int ast_language_is_prefix;
66 static AST_LIST_HEAD_STATIC(formats, ast_format);
68 int __ast_format_register(const struct ast_format *f, struct ast_module *mod)
70 struct ast_format *tmp;
72 if (AST_LIST_LOCK(&formats)) {
73 ast_log(LOG_WARNING, "Unable to lock format list\n");
76 AST_LIST_TRAVERSE(&formats, tmp, list) {
77 if (!strcasecmp(f->name, tmp->name)) {
78 AST_LIST_UNLOCK(&formats);
79 ast_log(LOG_WARNING, "Tried to register '%s' format, already registered\n", f->name);
83 if (!(tmp = ast_calloc(1, sizeof(*tmp)))) {
84 AST_LIST_UNLOCK(&formats);
91 * Align buf_size properly, rounding up to the machine-specific
92 * alignment for pointers.
94 struct _test_align { void *a, *b; } p;
95 int align = (char *)&p.b - (char *)&p.a;
96 tmp->buf_size = ((f->buf_size + align - 1)/align)*align;
99 memset(&tmp->list, 0, sizeof(tmp->list));
101 AST_LIST_INSERT_HEAD(&formats, tmp, list);
102 AST_LIST_UNLOCK(&formats);
103 if (option_verbose > 1)
104 ast_verbose( VERBOSE_PREFIX_2 "Registered file format %s, extension(s) %s\n", f->name, f->exts);
109 int ast_format_unregister(const char *name)
111 struct ast_format *tmp;
114 if (AST_LIST_LOCK(&formats)) {
115 ast_log(LOG_WARNING, "Unable to lock format list\n");
118 AST_LIST_TRAVERSE_SAFE_BEGIN(&formats, tmp, list) {
119 if (!strcasecmp(name, tmp->name)) {
120 AST_LIST_REMOVE_CURRENT(&formats, list);
125 AST_LIST_TRAVERSE_SAFE_END
126 AST_LIST_UNLOCK(&formats);
129 if (option_verbose > 1)
130 ast_verbose( VERBOSE_PREFIX_2 "Unregistered format %s\n", name);
132 ast_log(LOG_WARNING, "Tried to unregister format %s, already unregistered\n", name);
137 int ast_stopstream(struct ast_channel *tmp)
139 /* Stop a running stream if there is one */
141 ast_closestream(tmp->stream);
143 if (tmp->oldwriteformat && ast_set_write_format(tmp, tmp->oldwriteformat))
144 ast_log(LOG_WARNING, "Unable to restore format back to %d\n", tmp->oldwriteformat);
149 int ast_writestream(struct ast_filestream *fs, struct ast_frame *f)
153 if (f->frametype == AST_FRAME_VIDEO) {
154 if (fs->fmt->format < AST_FORMAT_MAX_AUDIO) {
155 /* This is the audio portion. Call the video one... */
156 if (!fs->vfs && fs->filename) {
157 const char *type = ast_getformatname(f->subclass & ~0x1);
158 fs->vfs = ast_writefile(fs->filename, type, NULL, fs->flags, 0, fs->mode);
160 ast_log(LOG_DEBUG, "Opened video output file\n");
163 return ast_writestream(fs->vfs, f);
167 /* Might / might not have mark set */
170 } else if (f->frametype != AST_FRAME_VOICE) {
171 ast_log(LOG_WARNING, "Tried to write non-voice frame\n");
174 if (((fs->fmt->format | alt) & f->subclass) == f->subclass) {
175 res = fs->fmt->write(fs, f);
177 ast_log(LOG_WARNING, "Natural write failed\n");
179 ast_log(LOG_WARNING, "Huh??\n");
181 /* XXX If they try to send us a type of frame that isn't the normal frame, and isn't
182 the one we've setup a translator for, we do the "wrong thing" XXX */
183 if (fs->trans && f->subclass != fs->lastwriteformat) {
184 ast_translator_free_path(fs->trans);
188 fs->trans = ast_translator_build_path(fs->fmt->format, f->subclass);
190 ast_log(LOG_WARNING, "Unable to translate to format %s, source format %s\n",
191 fs->fmt->name, ast_getformatname(f->subclass));
193 struct ast_frame *trf;
194 fs->lastwriteformat = f->subclass;
195 /* Get the translated frame but don't consume the original in case they're using it on another stream */
196 trf = ast_translate(fs->trans, f, 0);
198 res = fs->fmt->write(fs, trf);
200 ast_log(LOG_WARNING, "Translated frame write failed\n");
208 static int copy(const char *infile, const char *outfile)
211 char buf[4096]; /* XXX make it lerger. */
213 if ((ifd = open(infile, O_RDONLY)) < 0) {
214 ast_log(LOG_WARNING, "Unable to open %s in read-only mode\n", infile);
217 if ((ofd = open(outfile, O_WRONLY | O_TRUNC | O_CREAT, 0600)) < 0) {
218 ast_log(LOG_WARNING, "Unable to open %s in write-only mode\n", outfile);
222 while ( (len = read(ifd, buf, sizeof(buf)) ) ) {
225 ast_log(LOG_WARNING, "Read failed on %s: %s\n", infile, strerror(errno));
228 /* XXX handle partial writes */
229 res = write(ofd, buf, len);
231 ast_log(LOG_WARNING, "Write failed on %s (%d of %d): %s\n", outfile, res, len, strerror(errno));
232 len = -1; /* error marker */
240 return -1; /* error */
242 return 0; /* success */
246 * \brief construct a filename. Absolute pathnames are preserved,
247 * relative names are prefixed by the sounds/ directory.
248 * The wav49 suffix is replaced by 'WAV'.
249 * Returns a malloc'ed string to be freed by the caller.
251 static char *build_filename(const char *filename, const char *ext)
255 if (!strcmp(ext, "wav49"))
258 if (filename[0] == '/')
259 asprintf(&fn, "%s.%s", filename, ext);
261 asprintf(&fn, "%s/sounds/%s.%s",
262 ast_config_AST_DATA_DIR, filename, ext);
266 /* compare type against the list 'exts' */
267 /* XXX need a better algorithm */
268 static int exts_compare(const char *exts, const char *type)
271 char *stringp = tmp, *ext;
273 ast_copy_string(tmp, exts, sizeof(tmp));
274 while ((ext = strsep(&stringp, "|"))) {
275 if (!strcmp(ext, type))
282 static struct ast_filestream *get_filestream(struct ast_format *fmt, FILE *bfile)
284 struct ast_filestream *s;
286 int l = sizeof(*s) + fmt->buf_size + fmt->desc_size; /* total allocation size */
287 if ( (s = ast_calloc(1, l)) == NULL)
293 s->private = ((char *)(s+1)) + fmt->buf_size;
295 s->buf = (char *)(s+1);
296 s->fr.src = fmt->name;
301 * Default implementations of open and rewrite.
302 * Only use them if you don't have expensive stuff to do.
304 enum wrap_fn { WRAP_OPEN, WRAP_REWRITE };
306 static int fn_wrapper(struct ast_filestream *s, const char *comment, enum wrap_fn mode)
308 struct ast_format *f = s->fmt;
311 if (mode == WRAP_OPEN && f->open && f->open(s))
312 ast_log(LOG_WARNING, "Unable to open format %s\n", f->name);
313 else if (mode == WRAP_REWRITE && f->rewrite && f->rewrite(s, comment))
314 ast_log(LOG_WARNING, "Unable to rewrite format %s\n", f->name);
316 /* preliminary checks succeed. update usecount */
317 ast_module_ref(f->module);
323 static int rewrite_wrapper(struct ast_filestream *s, const char *comment)
325 return fn_wrapper(s, comment, WRAP_REWRITE);
328 static int open_wrapper(struct ast_filestream *s)
330 return fn_wrapper(s, NULL, WRAP_OPEN);
334 ACTION_EXISTS = 1, /* return matching format if file exists, 0 otherwise */
335 ACTION_DELETE, /* delete file, return 0 on success, -1 on error */
336 ACTION_RENAME, /* rename file. return 0 on success, -1 on error */
338 ACTION_COPY /* copy file. return 0 on success, -1 on error */
342 * \brief perform various actions on a file. Second argument
343 * arg2 depends on the command:
344 * unused for EXISTS and DELETE
345 * destination file name (const char *) for COPY and RENAME
346 * struct ast_channel * for OPEN
347 * if fmt is NULL, OPEN will return the first matching entry,
348 * whereas other functions will run on all matching entries.
350 static int ast_filehelper(const char *filename, const void *arg2, const char *fmt, const enum file_action action)
352 struct ast_format *f;
353 int res = (action == ACTION_EXISTS) ? 0 : -1;
355 if (AST_LIST_LOCK(&formats)) {
356 ast_log(LOG_WARNING, "Unable to lock format list\n");
359 /* Check for a specific format */
360 AST_LIST_TRAVERSE(&formats, f, list) {
361 char *stringp, *ext = NULL;
363 if (fmt && !exts_compare(f->exts, fmt))
366 /* Look for a file matching the supported extensions.
367 * The file must exist, and for OPEN, must match
368 * one of the formats supported by the channel.
370 stringp = ast_strdupa(f->exts); /* this is in the stack so does not need to be freed */
371 while ( (ext = strsep(&stringp, "|")) ) {
373 char *fn = build_filename(filename, ext);
378 if ( stat(fn, &st) ) { /* file not existent */
382 /* for 'OPEN' we need to be sure that the format matches
383 * what the channel can process
385 if (action == ACTION_OPEN) {
386 struct ast_channel *chan = (struct ast_channel *)arg2;
388 struct ast_filestream *s;
390 if ( !(chan->writeformat & f->format) &&
391 !(f->format >= AST_FORMAT_MAX_AUDIO && fmt)) {
393 continue; /* not a supported format */
395 if ( (bfile = fopen(fn, "r")) == NULL) {
397 continue; /* cannot open file */
399 s = get_filestream(f, bfile);
402 free(fn); /* cannot allocate descriptor */
405 if (open_wrapper(s)) {
409 continue; /* cannot run open on file */
411 /* ok this is good for OPEN */
417 if (s->fmt->format < AST_FORMAT_MAX_AUDIO)
426 break; /* will never get here */
428 case ACTION_EXISTS: /* return the matching format */
433 if ( (res = unlink(fn)) )
434 ast_log(LOG_WARNING, "unlink(%s) failed: %s\n", fn, strerror(errno));
439 char *nfn = build_filename((const char *)arg2, ext);
441 ast_log(LOG_WARNING, "Out of memory\n");
443 res = action == ACTION_COPY ? copy(fn, nfn) : rename(fn, nfn);
445 ast_log(LOG_WARNING, "%s(%s,%s) failed: %s\n",
446 action == ACTION_COPY ? "copy" : "rename",
447 fn, nfn, strerror(errno));
454 ast_log(LOG_WARNING, "Unknown helper %d\n", action);
459 AST_LIST_UNLOCK(&formats);
464 * \brief helper routine to locate a file with a given format
465 * and language preference.
466 * Try preflang, preflang with stripped '_' suffix, or NULL.
467 * In the standard asterisk, language goes just before the last component.
468 * In an alternative configuration, the language should be a prefix
469 * to the actual filename.
471 * The last parameter(s) point to a buffer of sufficient size,
472 * which on success is filled with the matching filename.
474 static int fileexists_core(const char *filename, const char *fmt, const char *preflang,
475 char *buf, int buflen)
478 int langlen; /* length of language string */
479 const char *c = strrchr(filename, '/');
480 int offset = c ? c - filename + 1 : 0; /* points right after the last '/' */
482 if (preflang == NULL)
484 langlen = strlen(preflang);
486 if (buflen < langlen + strlen(filename) + 2) {
487 ast_log(LOG_WARNING, "buffer too small\n");
488 buf[0] = '\0'; /* set to empty */
489 buf = alloca(langlen + strlen(filename) + 2); /* room for everything */
495 if (ast_language_is_prefix) { /* new layout */
497 strcpy(buf, preflang);
499 strcpy(buf + langlen + 1, filename);
501 strcpy(buf, filename); /* first copy the full string */
502 } else { /* old layout */
503 strcpy(buf, filename); /* first copy the full string */
505 /* insert the language and suffix if needed */
506 strcpy(buf + offset, preflang);
507 sprintf(buf + offset + langlen, "/%s", filename + offset);
510 res = ast_filehelper(buf, NULL, fmt, ACTION_EXISTS);
511 if (res > 0) /* found format */
513 if (langlen == 0) /* no more formats */
515 if (preflang[langlen] == '_') /* we are on the local suffix */
516 langlen = 0; /* try again with no language */
518 langlen = (c = strchr(preflang, '_')) ? c - preflang : 0;
523 struct ast_filestream *ast_openstream(struct ast_channel *chan, const char *filename, const char *preflang)
525 return ast_openstream_full(chan, filename, preflang, 0);
528 struct ast_filestream *ast_openstream_full(struct ast_channel *chan, const char *filename, const char *preflang, int asis)
531 * Use fileexists_core() to find a file in a compatible
532 * language and format, set up a suitable translator,
533 * and open the stream.
535 int fmts, res, buflen;
539 /* do this first, otherwise we detect the wrong writeformat */
540 ast_stopstream(chan);
542 ast_deactivate_generator(chan);
544 if (preflang == NULL)
546 buflen = strlen(preflang) + strlen(filename) + 2;
547 buf = alloca(buflen);
550 fmts = fileexists_core(filename, NULL, preflang, buf, buflen);
552 fmts &= AST_FORMAT_AUDIO_MASK;
554 ast_log(LOG_WARNING, "File %s does not exist in any format\n", filename);
557 chan->oldwriteformat = chan->writeformat;
558 /* Set the channel to a format we can work with */
559 res = ast_set_write_format(chan, fmts);
560 res = ast_filehelper(buf, chan, NULL, ACTION_OPEN);
566 struct ast_filestream *ast_openvstream(struct ast_channel *chan, const char *filename, const char *preflang)
568 /* As above, but for video. But here we don't have translators
569 * so we must enforce a format.
575 if (preflang == NULL)
577 buflen = strlen(preflang) + strlen(filename) + 2;
578 buf = alloca(buflen);
582 for (format = AST_FORMAT_MAX_AUDIO << 1; format <= AST_FORMAT_MAX_VIDEO; format = format << 1) {
586 if (!(chan->nativeformats & format))
588 fmt = ast_getformatname(format);
589 if ( fileexists_core(filename, fmt, preflang, buf, buflen) < 1) /* no valid format */
591 fd = ast_filehelper(buf, chan, fmt, ACTION_OPEN);
593 return chan->vstream;
594 ast_log(LOG_WARNING, "File %s has video but couldn't be opened\n", filename);
599 struct ast_frame *ast_readframe(struct ast_filestream *s)
601 struct ast_frame *f = NULL;
604 f = s->fmt->read(s, &whennext);
608 static int ast_readaudio_callback(void *data)
610 struct ast_filestream *s = data;
614 struct ast_frame *fr = s->fmt->read(s, &whennext);
615 if (!fr /* stream complete */ || ast_write(s->owner, fr) /* error writing */) {
617 ast_log(LOG_WARNING, "Failed to write frame\n");
618 s->owner->streamid = -1;
620 ast_settimeout(s->owner, 0, NULL, NULL);
625 if (whennext != s->lasttimeout) {
627 if (s->owner->timingfd > -1)
628 ast_settimeout(s->owner, whennext, ast_readaudio_callback, s);
631 s->owner->streamid = ast_sched_add(s->owner->sched, whennext/8, ast_readaudio_callback, s);
632 s->lasttimeout = whennext;
638 static int ast_readvideo_callback(void *data)
640 struct ast_filestream *s = data;
644 struct ast_frame *fr = s->fmt->read(s, &whennext);
645 if (!fr || ast_write(s->owner, fr)) { /* no stream or error, as above */
647 ast_log(LOG_WARNING, "Failed to write frame\n");
648 s->owner->vstreamid = -1;
652 if (whennext != s->lasttimeout) {
653 s->owner->vstreamid = ast_sched_add(s->owner->sched, whennext/8, ast_readvideo_callback, s);
654 s->lasttimeout = whennext;
660 int ast_applystream(struct ast_channel *chan, struct ast_filestream *s)
666 int ast_playstream(struct ast_filestream *s)
668 if (s->fmt->format < AST_FORMAT_MAX_AUDIO)
669 ast_readaudio_callback(s);
671 ast_readvideo_callback(s);
675 int ast_seekstream(struct ast_filestream *fs, off_t sample_offset, int whence)
677 return fs->fmt->seek(fs, sample_offset, whence);
680 int ast_truncstream(struct ast_filestream *fs)
682 return fs->fmt->trunc(fs);
685 off_t ast_tellstream(struct ast_filestream *fs)
687 return fs->fmt->tell(fs);
690 int ast_stream_fastforward(struct ast_filestream *fs, off_t ms)
692 return ast_seekstream(fs, ms * DEFAULT_SAMPLES_PER_MS, SEEK_CUR);
695 int ast_stream_rewind(struct ast_filestream *fs, off_t ms)
697 return ast_seekstream(fs, -ms * DEFAULT_SAMPLES_PER_MS, SEEK_CUR);
700 int ast_closestream(struct ast_filestream *f)
704 /* Stop a running stream if there is one */
706 if (f->fmt->format < AST_FORMAT_MAX_AUDIO) {
707 f->owner->stream = NULL;
708 if (f->owner->streamid > -1)
709 ast_sched_del(f->owner->sched, f->owner->streamid);
710 f->owner->streamid = -1;
712 ast_settimeout(f->owner, 0, NULL, NULL);
715 f->owner->vstream = NULL;
716 if (f->owner->vstreamid > -1)
717 ast_sched_del(f->owner->sched, f->owner->vstreamid);
718 f->owner->vstreamid = -1;
721 /* destroy the translator on exit */
723 ast_translator_free_path(f->trans);
725 if (f->realfilename && f->filename) {
726 size = strlen(f->filename) + strlen(f->realfilename) + 15;
729 snprintf(cmd,size,"/bin/mv -f %s %s",f->filename,f->realfilename);
730 ast_safe_system(cmd);
736 free(f->realfilename);
741 ast_closestream(f->vfs);
742 ast_module_unref(f->fmt->module);
749 * Look the various language-specific places where a file could exist.
751 int ast_fileexists(const char *filename, const char *fmt, const char *preflang)
756 if (preflang == NULL)
758 buflen = strlen(preflang) + strlen(filename) + 2; /* room for everything */
759 buf = alloca(buflen);
762 return fileexists_core(filename, fmt, preflang, buf, buflen);
765 int ast_filedelete(const char *filename, const char *fmt)
767 return ast_filehelper(filename, NULL, fmt, ACTION_DELETE);
770 int ast_filerename(const char *filename, const char *filename2, const char *fmt)
772 return ast_filehelper(filename, filename2, fmt, ACTION_RENAME);
775 int ast_filecopy(const char *filename, const char *filename2, const char *fmt)
777 return ast_filehelper(filename, filename2, fmt, ACTION_COPY);
780 int ast_streamfile(struct ast_channel *chan, const char *filename, const char *preflang)
782 struct ast_filestream *fs;
783 struct ast_filestream *vfs=NULL;
786 fs = ast_openstream(chan, filename, preflang);
788 vfs = ast_openvstream(chan, filename, preflang);
791 ast_log(LOG_DEBUG, "Ooh, found a video stream, too, format %s\n", ast_getformatname(vfs->fmt->format));
794 if (ast_applystream(chan, fs))
796 if (vfs && ast_applystream(chan, vfs))
798 if (ast_playstream(fs))
800 if (vfs && ast_playstream(vfs))
803 if (option_verbose > 2)
804 ast_verbose(VERBOSE_PREFIX_3 "Playing '%s' (language '%s')\n", filename, preflang ? preflang : "default");
808 ast_log(LOG_WARNING, "Unable to open %s (format %s): %s\n", filename, ast_getformatname_multiple(fmt, sizeof(fmt), chan->nativeformats), strerror(errno));
812 struct ast_filestream *ast_readfile(const char *filename, const char *type, const char *comment, int flags, int check, mode_t mode)
815 struct ast_format *f;
816 struct ast_filestream *fs = NULL;
819 if (AST_LIST_LOCK(&formats)) {
820 ast_log(LOG_WARNING, "Unable to lock format list\n");
824 AST_LIST_TRAVERSE(&formats, f, list) {
826 if (!exts_compare(f->exts, type))
829 fn = build_filename(filename, type);
831 bfile = fopen(fn, "r");
832 if (!bfile || (fs = get_filestream(f, bfile)) == NULL ||
834 ast_log(LOG_WARNING, "Unable to open %s\n", fn);
847 fs->filename = strdup(filename);
852 AST_LIST_UNLOCK(&formats);
854 ast_log(LOG_WARNING, "No such format '%s'\n", type);
859 struct ast_filestream *ast_writefile(const char *filename, const char *type, const char *comment, int flags, int check, mode_t mode)
862 /* compiler claims this variable can be used before initialization... */
864 struct ast_format *f;
865 struct ast_filestream *fs = NULL;
868 int format_found = 0;
870 if (AST_LIST_LOCK(&formats)) {
871 ast_log(LOG_WARNING, "Unable to lock format list\n");
875 /* set the O_TRUNC flag if and only if there is no O_APPEND specified */
876 /* We really can't use O_APPEND as it will break WAV header updates */
877 if (flags & O_APPEND) {
883 myflags |= O_WRONLY | O_CREAT;
885 /* XXX need to fix this - we should just do the fopen,
886 * not open followed by fdopen()
888 AST_LIST_TRAVERSE(&formats, f, list) {
889 char *fn, *orig_fn = NULL;
893 if (!exts_compare(f->exts, type))
898 fn = build_filename(filename, type);
899 fd = open(fn, flags | myflags, mode);
901 /* fdopen() the resulting file stream */
902 bfile = fdopen(fd, ((flags | myflags) & O_RDWR) ? "w+" : "w");
904 ast_log(LOG_WARNING, "Whoa, fdopen failed: %s!\n", strerror(errno));
910 if (ast_opt_cache_record_files && (fd > -1)) {
913 fclose(bfile); /* this also closes fd */
915 We touch orig_fn just as a place-holder so other things (like vmail) see the file is there.
916 What we are really doing is writing to record_cache_dir until we are done then we will mv the file into place.
918 orig_fn = ast_strdupa(fn);
919 for (c = fn; *c; c++)
923 size = strlen(fn) + strlen(record_cache_dir) + 2;
925 strcpy(buf, record_cache_dir);
930 fd = open(fn, flags | myflags, mode);
932 /* fdopen() the resulting file stream */
933 bfile = fdopen(fd, ((flags | myflags) & O_RDWR) ? "w+" : "w");
935 ast_log(LOG_WARNING, "Whoa, fdopen failed: %s!\n", strerror(errno));
943 fs = get_filestream(f, bfile);
944 if (!fs || rewrite_wrapper(fs, comment)) {
945 ast_log(LOG_WARNING, "Unable to rewrite %s\n", fn);
959 fs->realfilename = strdup(orig_fn);
960 fs->filename = strdup(fn);
962 fs->realfilename = NULL;
963 fs->filename = strdup(filename);
966 /* If truncated, we'll be at the beginning; if not truncated, then append */
967 f->seek(fs, 0, SEEK_END);
968 } else if (errno != EEXIST) {
969 ast_log(LOG_WARNING, "Unable to open file %s: %s\n", fn, strerror(errno));
973 /* if buf != NULL then fn is already free and pointing to it */
978 AST_LIST_UNLOCK(&formats);
981 ast_log(LOG_WARNING, "No such format '%s'\n", type);
987 * \brief the core of all waitstream() functions
989 static int waitstream_core(struct ast_channel *c, const char *breakon,
990 const char *forward, const char *rewind, int skip_ms,
991 int audiofd, int cmdfd, const char *context)
1002 int ms = ast_sched_wait(c->sched);
1003 if (ms < 0 && !c->timingfunc) {
1010 res = ast_waitfor(c, ms);
1012 ast_log(LOG_WARNING, "Select failed (%s)\n", strerror(errno));
1017 struct ast_channel *rchan = ast_waitfor_nandfds(&c, 1, &cmdfd, (cmdfd > -1) ? 1 : 0, NULL, &outfd, &ms);
1018 if (!rchan && (outfd < 0) && (ms)) {
1022 ast_log(LOG_WARNING, "Wait failed (%s)\n", strerror(errno));
1024 } else if (outfd > -1) { /* this requires cmdfd set */
1025 /* The FD we were watching has something waiting */
1028 /* if rchan is set, it is 'c' */
1029 res = rchan ? 1 : 0; /* map into 'res' values */
1032 struct ast_frame *fr = ast_read(c);
1035 switch(fr->frametype) {
1036 case AST_FRAME_DTMF_END:
1038 const char exten[2] = { fr->subclass, '\0' };
1039 if (ast_exists_extension(c, context, exten, 1, c->cid.cid_num)) {
1045 if (strchr(forward,res)) {
1046 ast_stream_fastforward(c->stream, skip_ms);
1047 } else if (strchr(rewind,res)) {
1048 ast_stream_rewind(c->stream, skip_ms);
1049 } else if (strchr(breakon, res)) {
1055 case AST_FRAME_CONTROL:
1056 switch(fr->subclass) {
1057 case AST_CONTROL_HANGUP:
1058 case AST_CONTROL_BUSY:
1059 case AST_CONTROL_CONGESTION:
1062 case AST_CONTROL_RINGING:
1063 case AST_CONTROL_ANSWER:
1064 case AST_CONTROL_VIDUPDATE:
1065 case AST_CONTROL_HOLD:
1066 case AST_CONTROL_UNHOLD:
1070 ast_log(LOG_WARNING, "Unexpected control subclass '%d'\n", fr->subclass);
1073 case AST_FRAME_VOICE:
1074 /* Write audio if appropriate */
1076 write(audiofd, fr->data, fr->datalen);
1078 /* Ignore all others */
1083 ast_sched_runq(c->sched);
1085 return (c->_softhangup ? -1 : 0);
1088 int ast_waitstream_fr(struct ast_channel *c, const char *breakon, const char *forward, const char *rewind, int ms)
1090 return waitstream_core(c, breakon, forward, rewind, ms,
1091 -1 /* no audiofd */, -1 /* no cmdfd */, NULL /* no context */);
1094 int ast_waitstream(struct ast_channel *c, const char *breakon)
1096 return waitstream_core(c, breakon, NULL, NULL, 0, -1, -1, NULL);
1099 int ast_waitstream_full(struct ast_channel *c, const char *breakon, int audiofd, int cmdfd)
1101 return waitstream_core(c, breakon, NULL, NULL, 0,
1102 audiofd, cmdfd, NULL /* no context */);
1105 int ast_waitstream_exten(struct ast_channel *c, const char *context)
1107 /* Waitstream, with return in the case of a valid 1 digit extension */
1108 /* in the current or specified context being pressed */
1111 context = c->context;
1112 return waitstream_core(c, NULL, NULL, NULL, 0,
1117 * if the file name is non-empty, try to play it.
1118 * Return 0 if success, -1 if error, digit if interrupted by a digit.
1119 * If digits == "" then we can simply check for non-zero.
1121 int ast_stream_and_wait(struct ast_channel *chan, const char *file,
1122 const char *language, const char *digits)
1125 if (!ast_strlen_zero(file)) {
1126 res = ast_streamfile(chan, file, language);
1128 res = ast_waitstream(chan, digits);
1133 static int show_file_formats(int fd, int argc, char *argv[])
1135 #define FORMAT "%-10s %-10s %-20s\n"
1136 #define FORMAT2 "%-10s %-10s %-20s\n"
1137 struct ast_format *f;
1141 return RESULT_SHOWUSAGE;
1142 ast_cli(fd, FORMAT, "Format", "Name", "Extensions");
1144 if (AST_LIST_LOCK(&formats)) {
1145 ast_log(LOG_WARNING, "Unable to lock format list\n");
1149 AST_LIST_TRAVERSE(&formats, f, list) {
1150 ast_cli(fd, FORMAT2, ast_getformatname(f->format), f->name, f->exts);
1153 AST_LIST_UNLOCK(&formats);
1154 ast_cli(fd, "%d file formats registered.\n", count_fmt);
1155 return RESULT_SUCCESS;
1160 char show_file_formats_usage[] =
1161 "Usage: core show file formats\n"
1162 " Displays currently registered file formats (if any)\n";
1164 struct ast_cli_entry cli_file[] = {
1165 { { "core", "show", "file", "formats" },
1166 show_file_formats, "Displays file formats",
1167 show_file_formats_usage },
1170 int ast_file_init(void)
1172 ast_cli_register_multiple(cli_file, sizeof(cli_file) / sizeof(struct ast_cli_entry));