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>
26 #include <sys/types.h>
34 #include <sys/types.h>
39 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
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"
55 #include "asterisk/module.h" /* ast_update_use_count() */
58 * The following variable controls the layout of localized sound files.
59 * If 0, use the historical layout with prefix just before the filename
60 * (i.e. digits/en/1.gsm , digits/it/1.gsm or default to digits/1.gsm),
61 * if 1 put the prefix at the beginning of the filename
62 * (i.e. en/digits/1.gsm, it/digits/1.gsm or default to digits/1.gsm).
63 * The latter permits a language to be entirely in one directory.
65 int ast_language_is_prefix;
67 static AST_LIST_HEAD_STATIC(formats, ast_format);
69 int ast_format_register(const struct ast_format *f)
71 struct ast_format *tmp;
73 if (f->module == NULL) {
74 ast_log(LOG_WARNING, "Missing module pointer, you need to supply one\n");
77 if (AST_LIST_LOCK(&formats)) {
78 ast_log(LOG_WARNING, "Unable to lock format list\n");
81 AST_LIST_TRAVERSE(&formats, tmp, list) {
82 if (!strcasecmp(f->name, tmp->name)) {
83 AST_LIST_UNLOCK(&formats);
84 ast_log(LOG_WARNING, "Tried to register '%s' format, already registered\n", f->name);
88 tmp = ast_calloc(1, sizeof(struct ast_format));
90 AST_LIST_UNLOCK(&formats);
96 * Align buf_size properly, rounding up to the machine-specific
97 * alignment for pointers.
99 struct _test_align { void *a, *b; } p;
100 int align = (char *)&p.b - (char *)&p.a;
101 tmp->buf_size = ((f->buf_size + align - 1)/align)*align;
104 memset(&tmp->list, 0, sizeof(tmp->list));
106 AST_LIST_INSERT_HEAD(&formats, tmp, list);
107 AST_LIST_UNLOCK(&formats);
108 if (option_verbose > 1)
109 ast_verbose( VERBOSE_PREFIX_2 "Registered file format %s, extension(s) %s\n", f->name, f->exts);
113 int ast_format_unregister(const char *name)
115 struct ast_format *tmp;
118 if (AST_LIST_LOCK(&formats)) {
119 ast_log(LOG_WARNING, "Unable to lock format list\n");
122 AST_LIST_TRAVERSE_SAFE_BEGIN(&formats, tmp, list) {
123 if (!strcasecmp(name, tmp->name)) {
124 AST_LIST_REMOVE_CURRENT(&formats, list);
129 AST_LIST_TRAVERSE_SAFE_END
130 AST_LIST_UNLOCK(&formats);
133 if (option_verbose > 1)
134 ast_verbose( VERBOSE_PREFIX_2 "Unregistered format %s\n", name);
136 ast_log(LOG_WARNING, "Tried to unregister format %s, already unregistered\n", name);
141 int ast_stopstream(struct ast_channel *tmp)
143 /* Stop a running stream if there is one */
145 ast_closestream(tmp->stream);
146 if (tmp->oldwriteformat && ast_set_write_format(tmp, tmp->oldwriteformat))
147 ast_log(LOG_WARNING, "Unable to restore format back to %d\n", tmp->oldwriteformat);
152 int ast_writestream(struct ast_filestream *fs, struct ast_frame *f)
156 if (f->frametype == AST_FRAME_VIDEO) {
157 if (fs->fmt->format < AST_FORMAT_MAX_AUDIO) {
158 /* This is the audio portion. Call the video one... */
159 if (!fs->vfs && fs->filename) {
160 const char *type = ast_getformatname(f->subclass & ~0x1);
161 fs->vfs = ast_writefile(fs->filename, type, NULL, fs->flags, 0, fs->mode);
162 ast_log(LOG_DEBUG, "Opened video output file\n");
165 return ast_writestream(fs->vfs, f);
169 /* Might / might not have mark set */
172 } else if (f->frametype != AST_FRAME_VOICE) {
173 ast_log(LOG_WARNING, "Tried to write non-voice frame\n");
176 if (((fs->fmt->format | alt) & f->subclass) == f->subclass) {
177 res = fs->fmt->write(fs, f);
179 ast_log(LOG_WARNING, "Natural write failed\n");
181 ast_log(LOG_WARNING, "Huh??\n");
183 /* XXX If they try to send us a type of frame that isn't the normal frame, and isn't
184 the one we've setup a translator for, we do the "wrong thing" XXX */
185 if (fs->trans && f->subclass != fs->lastwriteformat) {
186 ast_translator_free_path(fs->trans);
190 fs->trans = ast_translator_build_path(fs->fmt->format, f->subclass);
192 ast_log(LOG_WARNING, "Unable to translate to format %s, source format %s\n",
193 fs->fmt->name, ast_getformatname(f->subclass));
195 struct ast_frame *trf;
196 fs->lastwriteformat = f->subclass;
197 /* Get the translated frame but don't consume the original in case they're using it on another stream */
198 trf = ast_translate(fs->trans, f, 0);
200 res = fs->fmt->write(fs, trf);
202 ast_log(LOG_WARNING, "Translated frame write failed\n");
210 static int copy(const char *infile, const char *outfile)
213 char buf[4096]; /* XXX make it lerger. */
215 if ((ifd = open(infile, O_RDONLY)) < 0) {
216 ast_log(LOG_WARNING, "Unable to open %s in read-only mode\n", infile);
219 if ((ofd = open(outfile, O_WRONLY | O_TRUNC | O_CREAT, 0600)) < 0) {
220 ast_log(LOG_WARNING, "Unable to open %s in write-only mode\n", outfile);
224 while ( (len = read(ifd, buf, sizeof(buf)) ) ) {
227 ast_log(LOG_WARNING, "Read failed on %s: %s\n", infile, strerror(errno));
230 /* XXX handle partial writes */
231 res = write(ofd, buf, len);
233 ast_log(LOG_WARNING, "Write failed on %s (%d of %d): %s\n", outfile, res, len, strerror(errno));
234 len = -1; /* error marker */
242 return -1; /* error */
244 return 0; /* success */
248 * \brief construct a filename. Absolute pathnames are preserved,
249 * relative names are prefixed by the sounds/ directory.
250 * The wav49 suffix is replaced by 'WAV'.
251 * Returns a malloc'ed string to be freed by the caller.
253 static char *build_filename(const char *filename, const char *ext)
257 if (!strcmp(ext, "wav49"))
260 if (filename[0] == '/')
261 asprintf(&fn, "%s.%s", filename, ext);
263 asprintf(&fn, "%s/sounds/%s.%s",
264 ast_config_AST_DATA_DIR, filename, ext);
268 /* compare type against the list 'exts' */
269 /* XXX need a better algorithm */
270 static int exts_compare(const char *exts, const char *type)
273 char *stringp = tmp, *ext;
275 ast_copy_string(tmp, exts, sizeof(tmp));
276 while ((ext = strsep(&stringp, "|"))) {
277 if (!strcmp(ext, type))
284 static struct ast_filestream *get_filestream(struct ast_format *fmt, FILE *bfile)
286 struct ast_filestream *s;
288 int l = sizeof(*s) + fmt->buf_size + fmt->desc_size; /* total allocation size */
289 if ( (s = ast_calloc(1, l)) == NULL)
295 s->private = ((char *)(s+1)) + fmt->buf_size;
297 s->buf = (char *)(s+1);
298 s->fr.src = fmt->name;
303 * Default implementations of open and rewrite.
304 * Only use them if you don't have expensive stuff to do.
306 enum wrap_fn { WRAP_OPEN, WRAP_REWRITE };
308 static int fn_wrapper(struct ast_filestream *s, const char *comment, enum wrap_fn mode)
310 struct ast_format *f = s->fmt;
313 if (mode == WRAP_OPEN && f->open && f->open(s))
314 ast_log(LOG_WARNING, "Unable to open format %s\n", f->name);
315 else if (mode == WRAP_REWRITE && f->rewrite && f->rewrite(s, comment))
316 ast_log(LOG_WARNING, "Unable to rewrite format %s\n", f->name);
318 /* preliminary checks succeed. update usecount */
319 ast_atomic_fetchadd_int(&f->module->usecnt, +1);
321 ast_update_use_count();
326 static int rewrite_wrapper(struct ast_filestream *s, const char *comment)
328 return fn_wrapper(s, comment, WRAP_REWRITE);
331 static int open_wrapper(struct ast_filestream *s)
333 return fn_wrapper(s, NULL, WRAP_OPEN);
337 ACTION_EXISTS = 1, /* return matching format if file exists, 0 otherwise */
338 ACTION_DELETE, /* delete file, return 0 on success, -1 on error */
339 ACTION_RENAME, /* rename file. return 0 on success, -1 on error */
341 ACTION_COPY /* copy file. return 0 on success, -1 on error */
345 * \brief perform various actions on a file. Second argument
346 * arg2 depends on the command:
347 * unused for EXISTS and DELETE
348 * destination file name (const char *) for COPY and RENAME
349 * struct ast_channel * for OPEN
350 * if fmt is NULL, OPEN will return the first matching entry,
351 * whereas other functions will run on all matching entries.
353 static int ast_filehelper(const char *filename, const void *arg2, const char *fmt, const enum file_action action)
355 struct ast_format *f;
356 int res = (action == ACTION_EXISTS) ? 0 : -1;
358 if (AST_LIST_LOCK(&formats)) {
359 ast_log(LOG_WARNING, "Unable to lock format list\n");
362 /* Check for a specific format */
363 AST_LIST_TRAVERSE(&formats, f, list) {
364 char *stringp, *ext = NULL;
366 if (fmt && !exts_compare(f->exts, fmt))
369 /* Look for a file matching the supported extensions.
370 * The file must exist, and for OPEN, must match
371 * one of the formats supported by the channel.
373 stringp = ast_strdupa(f->exts); /* this is in the stack so does not need to be freed */
374 while ( (ext = strsep(&stringp, "|")) ) {
376 char *fn = build_filename(filename, ext);
381 if ( stat(fn, &st) ) { /* file not existent */
385 /* for 'OPEN' we need to be sure that the format matches
386 * what the channel can process
388 if (action == ACTION_OPEN) {
389 struct ast_channel *chan = (struct ast_channel *)arg2;
391 struct ast_filestream *s;
393 if ( !(chan->writeformat & f->format) &&
394 !(f->format >= AST_FORMAT_MAX_AUDIO && fmt)) {
396 continue; /* not a supported format */
398 if ( (bfile = fopen(fn, "r")) == NULL) {
400 continue; /* cannot open file */
402 s = get_filestream(f, bfile);
405 free(fn); /* cannot allocate descriptor */
408 if (open_wrapper(s)) {
412 continue; /* cannot run open on file */
414 /* ok this is good for OPEN */
420 if (s->fmt->format < AST_FORMAT_MAX_AUDIO)
428 break; /* will never get here */
430 case ACTION_EXISTS: /* return the matching format */
435 if ( (res = unlink(fn)) )
436 ast_log(LOG_WARNING, "unlink(%s) failed: %s\n", fn, strerror(errno));
441 char *nfn = build_filename((const char *)arg2, ext);
443 ast_log(LOG_WARNING, "Out of memory\n");
445 res = action == ACTION_COPY ? copy(fn, nfn) : rename(fn, nfn);
447 ast_log(LOG_WARNING, "%s(%s,%s) failed: %s\n",
448 action == ACTION_COPY ? "copy" : "rename",
449 fn, nfn, strerror(errno));
456 ast_log(LOG_WARNING, "Unknown helper %d\n", action);
461 AST_LIST_UNLOCK(&formats);
466 * \brief helper routine to locate a file with a given format
467 * and language preference.
468 * Try preflang, preflang with stripped '_' suffix, or NULL.
469 * In the standard asterisk, language goes just before the last component.
470 * In an alternative configuration, the language should be a prefix
471 * to the actual filename.
473 * The last parameter(s) point to a buffer of sufficient size,
474 * which on success is filled with the matching filename.
476 static int fileexists_core(const char *filename, const char *fmt, const char *preflang,
477 char *buf, int buflen)
480 int langlen; /* length of language string */
481 const char *c = strrchr(filename, '/');
482 int offset = c ? c - filename + 1 : 0; /* points right after the last '/' */
484 if (preflang == NULL)
486 langlen = strlen(preflang);
488 if (buflen < langlen + strlen(filename) + 2) {
489 ast_log(LOG_WARNING, "buffer too small\n");
490 buf[0] = '\0'; /* set to empty */
491 buf = alloca(langlen + strlen(filename) + 2); /* room for everything */
497 if (ast_language_is_prefix) { /* new layout */
499 strcpy(buf, preflang);
501 strcpy(buf + langlen + 1, filename);
503 strcpy(buf, filename); /* first copy the full string */
504 } else { /* old layout */
505 strcpy(buf, filename); /* first copy the full string */
507 /* insert the language and suffix if needed */
508 strcpy(buf + offset, preflang);
509 sprintf(buf + offset + langlen, "/%s", filename + offset);
512 res = ast_filehelper(buf, NULL, fmt, ACTION_EXISTS);
513 if (res > 0) /* found format */
515 if (langlen == 0) /* no more formats */
517 if (preflang[langlen] == '_') /* we are on the local suffix */
518 langlen = 0; /* try again with no language */
520 langlen = (c = strchr(preflang, '_')) ? c - preflang : 0;
525 struct ast_filestream *ast_openstream(struct ast_channel *chan, const char *filename, const char *preflang)
527 return ast_openstream_full(chan, filename, preflang, 0);
530 struct ast_filestream *ast_openstream_full(struct ast_channel *chan, const char *filename, const char *preflang, int asis)
533 * Use fileexists_core() to find a file in a compatible
534 * language and format, set up a suitable translator,
535 * and open the stream.
537 int fmts, res, buflen;
541 /* do this first, otherwise we detect the wrong writeformat */
542 ast_stopstream(chan);
544 ast_deactivate_generator(chan);
546 if (preflang == NULL)
548 buflen = strlen(preflang) + strlen(filename) + 2;
549 buf = alloca(buflen);
552 fmts = fileexists_core(filename, NULL, preflang, buf, buflen);
554 fmts &= AST_FORMAT_AUDIO_MASK;
556 ast_log(LOG_WARNING, "File %s does not exist in any format\n", filename);
559 chan->oldwriteformat = chan->writeformat;
560 /* Set the channel to a format we can work with */
561 res = ast_set_write_format(chan, fmts);
562 res = ast_filehelper(buf, chan, NULL, ACTION_OPEN);
568 struct ast_filestream *ast_openvstream(struct ast_channel *chan, const char *filename, const char *preflang)
570 /* As above, but for video. But here we don't have translators
571 * so we must enforce a format.
577 if (preflang == NULL)
579 buflen = strlen(preflang) + strlen(filename) + 2;
580 buf = alloca(buflen);
584 for (format = AST_FORMAT_MAX_AUDIO << 1; format <= AST_FORMAT_MAX_VIDEO; format = format << 1) {
588 if (!(chan->nativeformats & format))
590 fmt = ast_getformatname(format);
591 if ( fileexists_core(filename, fmt, preflang, buf, buflen) < 1) /* no valid format */
593 fd = ast_filehelper(buf, chan, fmt, ACTION_OPEN);
595 return chan->vstream;
596 ast_log(LOG_WARNING, "File %s has video but couldn't be opened\n", filename);
601 struct ast_frame *ast_readframe(struct ast_filestream *s)
603 struct ast_frame *f = NULL;
606 f = s->fmt->read(s, &whennext);
610 static int ast_readaudio_callback(void *data)
612 struct ast_filestream *s = data;
616 struct ast_frame *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");
620 s->owner->streamid = -1;
621 #ifdef ZAPTEL_OPTIMIZATIONS
622 ast_settimeout(s->owner, 0, NULL, NULL);
627 if (whennext != s->lasttimeout) {
628 #ifdef ZAPTEL_OPTIMIZATIONS
629 if (s->owner->timingfd > -1)
630 ast_settimeout(s->owner, whennext, ast_readaudio_callback, s);
633 s->owner->streamid = ast_sched_add(s->owner->sched, whennext/8, ast_readaudio_callback, s);
634 s->lasttimeout = whennext;
640 static int ast_readvideo_callback(void *data)
642 struct ast_filestream *s = data;
646 struct ast_frame *fr = s->fmt->read(s, &whennext);
647 if (!fr || ast_write(s->owner, fr)) { /* no stream or error, as above */
649 ast_log(LOG_WARNING, "Failed to write frame\n");
650 s->owner->vstreamid = -1;
654 if (whennext != s->lasttimeout) {
655 s->owner->vstreamid = ast_sched_add(s->owner->sched, whennext/8, ast_readvideo_callback, s);
656 s->lasttimeout = whennext;
662 int ast_applystream(struct ast_channel *chan, struct ast_filestream *s)
668 int ast_playstream(struct ast_filestream *s)
670 if (s->fmt->format < AST_FORMAT_MAX_AUDIO)
671 ast_readaudio_callback(s);
673 ast_readvideo_callback(s);
677 int ast_seekstream(struct ast_filestream *fs, off_t sample_offset, int whence)
679 return fs->fmt->seek(fs, sample_offset, whence);
682 int ast_truncstream(struct ast_filestream *fs)
684 return fs->fmt->trunc(fs);
687 off_t ast_tellstream(struct ast_filestream *fs)
689 return fs->fmt->tell(fs);
692 int ast_stream_fastforward(struct ast_filestream *fs, off_t ms)
694 return ast_seekstream(fs, ms * DEFAULT_SAMPLES_PER_MS, SEEK_CUR);
697 int ast_stream_rewind(struct ast_filestream *fs, off_t ms)
699 return ast_seekstream(fs, -ms * DEFAULT_SAMPLES_PER_MS, SEEK_CUR);
702 int ast_closestream(struct ast_filestream *f)
706 /* Stop a running stream if there is one */
708 if (f->fmt->format < AST_FORMAT_MAX_AUDIO) {
709 f->owner->stream = NULL;
710 if (f->owner->streamid > -1)
711 ast_sched_del(f->owner->sched, f->owner->streamid);
712 f->owner->streamid = -1;
713 #ifdef ZAPTEL_OPTIMIZATIONS
714 ast_settimeout(f->owner, 0, NULL, NULL);
717 f->owner->vstream = NULL;
718 if (f->owner->vstreamid > -1)
719 ast_sched_del(f->owner->sched, f->owner->vstreamid);
720 f->owner->vstreamid = -1;
723 /* destroy the translator on exit */
725 ast_translator_free_path(f->trans);
727 if (f->realfilename && f->filename) {
728 size = strlen(f->filename) + strlen(f->realfilename) + 15;
731 snprintf(cmd,size,"/bin/mv -f %s %s",f->filename,f->realfilename);
732 ast_safe_system(cmd);
738 free(f->realfilename);
743 ast_closestream(f->vfs);
744 ast_atomic_fetchadd_int(&f->fmt->module->usecnt, -1);
745 ast_update_use_count();
752 * Look the various language-specific places where a file could exist.
754 int ast_fileexists(const char *filename, const char *fmt, const char *preflang)
759 if (preflang == NULL)
761 buflen = strlen(preflang) + strlen(filename) + 2; /* room for everything */
762 buf = alloca(buflen);
765 return fileexists_core(filename, fmt, preflang, buf, buflen);
768 int ast_filedelete(const char *filename, const char *fmt)
770 return ast_filehelper(filename, NULL, fmt, ACTION_DELETE);
773 int ast_filerename(const char *filename, const char *filename2, const char *fmt)
775 return ast_filehelper(filename, filename2, fmt, ACTION_RENAME);
778 int ast_filecopy(const char *filename, const char *filename2, const char *fmt)
780 return ast_filehelper(filename, filename2, fmt, ACTION_COPY);
783 int ast_streamfile(struct ast_channel *chan, const char *filename, const char *preflang)
785 struct ast_filestream *fs;
786 struct ast_filestream *vfs=NULL;
789 fs = ast_openstream(chan, filename, preflang);
791 vfs = ast_openvstream(chan, filename, preflang);
793 ast_log(LOG_DEBUG, "Ooh, found a video stream, too, format %s\n", ast_getformatname(vfs->fmt->format));
795 if (ast_applystream(chan, fs))
797 if (vfs && ast_applystream(chan, vfs))
799 if (ast_playstream(fs))
801 if (vfs && ast_playstream(vfs))
804 if (option_verbose > 2)
805 ast_verbose(VERBOSE_PREFIX_3 "Playing '%s' (language '%s')\n", filename, preflang ? preflang : "default");
809 ast_log(LOG_WARNING, "Unable to open %s (format %s): %s\n", filename, ast_getformatname_multiple(fmt, sizeof(fmt), chan->nativeformats), strerror(errno));
813 struct ast_filestream *ast_readfile(const char *filename, const char *type, const char *comment, int flags, int check, mode_t mode)
816 struct ast_format *f;
817 struct ast_filestream *fs = NULL;
820 if (AST_LIST_LOCK(&formats)) {
821 ast_log(LOG_WARNING, "Unable to lock format list\n");
825 AST_LIST_TRAVERSE(&formats, f, list) {
827 if (!exts_compare(f->exts, type))
830 fn = build_filename(filename, type);
832 bfile = fopen(fn, "r");
833 if (!bfile || (fs = get_filestream(f, bfile)) == NULL ||
835 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;
869 if (AST_LIST_LOCK(&formats)) {
870 ast_log(LOG_WARNING, "Unable to lock format list\n");
874 /* set the O_TRUNC flag if and only if there is no O_APPEND specified */
875 /* We really can't use O_APPEND as it will break WAV header updates */
876 if (flags & O_APPEND) {
882 myflags |= O_WRONLY | O_CREAT;
884 /* XXX need to fix this - we should just do the fopen,
885 * not open followed by fdopen()
887 AST_LIST_TRAVERSE(&formats, f, list) {
888 char *fn, *orig_fn = NULL;
892 if (!exts_compare(f->exts, type))
895 fn = build_filename(filename, type);
896 fd = open(fn, flags | myflags, mode);
898 /* fdopen() the resulting file stream */
899 bfile = fdopen(fd, ((flags | myflags) & O_RDWR) ? "w+" : "w");
901 ast_log(LOG_WARNING, "Whoa, fdopen failed: %s!\n", strerror(errno));
907 if (ast_opt_cache_record_files && (fd > -1)) {
910 fclose(bfile); /* this also closes fd */
912 We touch orig_fn just as a place-holder so other things (like vmail) see the file is there.
913 What we are really doing is writing to record_cache_dir until we are done then we will mv the file into place.
915 orig_fn = ast_strdupa(fn);
916 for (c = fn; *c; c++)
920 size = strlen(fn) + strlen(record_cache_dir) + 2;
922 strcpy(buf, record_cache_dir);
927 fd = open(fn, flags | myflags, mode);
929 /* fdopen() the resulting file stream */
930 bfile = fdopen(fd, ((flags | myflags) & O_RDWR) ? "w+" : "w");
932 ast_log(LOG_WARNING, "Whoa, fdopen failed: %s!\n", strerror(errno));
940 fs = get_filestream(f, bfile);
941 if (!fs || rewrite_wrapper(fs, comment)) {
942 ast_log(LOG_WARNING, "Unable to rewrite %s\n", fn);
956 fs->realfilename = strdup(orig_fn);
957 fs->filename = strdup(fn);
959 fs->realfilename = NULL;
960 fs->filename = strdup(filename);
963 /* If truncated, we'll be at the beginning; if not truncated, then append */
964 f->seek(fs, 0, SEEK_END);
965 } else if (errno != EEXIST) {
966 ast_log(LOG_WARNING, "Unable to open file %s: %s\n", fn, strerror(errno));
970 /* if buf != NULL then fn is already free and pointing to it */
975 AST_LIST_UNLOCK(&formats);
977 ast_log(LOG_WARNING, "No such format '%s'\n", type);
983 * \brief the core of all waitstream() functions
985 static int waitstream_core(struct ast_channel *c, const char *breakon,
986 const char *forward, const char *rewind, int skip_ms,
987 int audiofd, int cmdfd, const char *context)
998 int ms = ast_sched_wait(c->sched);
999 if (ms < 0 && !c->timingfunc) {
1006 res = ast_waitfor(c, ms);
1008 ast_log(LOG_WARNING, "Select failed (%s)\n", strerror(errno));
1013 struct ast_channel *rchan = ast_waitfor_nandfds(&c, 1, &cmdfd, (cmdfd > -1) ? 1 : 0, NULL, &outfd, &ms);
1014 if (!rchan && (outfd < 0) && (ms)) {
1018 ast_log(LOG_WARNING, "Wait failed (%s)\n", strerror(errno));
1020 } else if (outfd > -1) { /* this requires cmdfd set */
1021 /* The FD we were watching has something waiting */
1024 /* if rchan is set, it is 'c' */
1025 res = rchan ? 1 : 0; /* map into 'res' values */
1028 struct ast_frame *fr = ast_read(c);
1031 switch(fr->frametype) {
1032 case AST_FRAME_DTMF:
1034 const char exten[2] = { fr->subclass, '\0' };
1035 if (ast_exists_extension(c, context, exten, 1, c->cid.cid_num)) {
1041 if (strchr(forward,res)) {
1042 ast_stream_fastforward(c->stream, skip_ms);
1043 } else if (strchr(rewind,res)) {
1044 ast_stream_rewind(c->stream, skip_ms);
1045 } else if (strchr(breakon, res)) {
1051 case AST_FRAME_CONTROL:
1052 switch(fr->subclass) {
1053 case AST_CONTROL_HANGUP:
1056 case AST_CONTROL_RINGING:
1057 case AST_CONTROL_ANSWER:
1061 ast_log(LOG_WARNING, "Unexpected control subclass '%d'\n", fr->subclass);
1063 case AST_FRAME_VOICE:
1064 /* Write audio if appropriate */
1066 write(audiofd, fr->data, fr->datalen);
1068 /* Ignore all others */
1071 ast_sched_runq(c->sched);
1073 return (c->_softhangup ? -1 : 0);
1076 int ast_waitstream_fr(struct ast_channel *c, const char *breakon, const char *forward, const char *rewind, int ms)
1078 return waitstream_core(c, breakon, forward, rewind, ms,
1079 -1 /* no audiofd */, -1 /* no cmdfd */, NULL /* no context */);
1082 int ast_waitstream(struct ast_channel *c, const char *breakon)
1084 return waitstream_core(c, breakon, NULL, NULL, 0, -1, -1, NULL);
1087 int ast_waitstream_full(struct ast_channel *c, const char *breakon, int audiofd, int cmdfd)
1089 return waitstream_core(c, breakon, NULL, NULL, 0,
1090 audiofd, cmdfd, NULL /* no context */);
1093 int ast_waitstream_exten(struct ast_channel *c, const char *context)
1095 /* Waitstream, with return in the case of a valid 1 digit extension */
1096 /* in the current or specified context being pressed */
1099 context = c->context;
1100 return waitstream_core(c, NULL, NULL, NULL, 0,
1104 static int show_file_formats(int fd, int argc, char *argv[])
1106 #define FORMAT "%-10s %-10s %-20s\n"
1107 #define FORMAT2 "%-10s %-10s %-20s\n"
1108 struct ast_format *f;
1112 return RESULT_SHOWUSAGE;
1113 ast_cli(fd, FORMAT, "Format", "Name", "Extensions");
1115 if (AST_LIST_LOCK(&formats)) {
1116 ast_log(LOG_WARNING, "Unable to lock format list\n");
1120 AST_LIST_TRAVERSE(&formats, f, list) {
1121 ast_cli(fd, FORMAT2, ast_getformatname(f->format), f->name, f->exts);
1124 AST_LIST_UNLOCK(&formats);
1125 ast_cli(fd, "%d file formats registered.\n", count_fmt);
1126 return RESULT_SUCCESS;
1131 struct ast_cli_entry show_file =
1133 { "show", "file", "formats" },
1135 "Displays file formats",
1136 "Usage: show file formats\n"
1137 " displays currently registered file formats (if any)\n"
1140 int ast_file_init(void)
1142 ast_cli_register(&show_file);