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>
27 <support_level>core</support_level>
32 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
39 #include "asterisk/_private.h" /* declare ast_file_init() */
40 #include "asterisk/paths.h" /* use ast_config_AST_DATA_DIR */
41 #include "asterisk/mod_format.h"
42 #include "asterisk/cli.h"
43 #include "asterisk/channel.h"
44 #include "asterisk/sched.h"
45 #include "asterisk/translate.h"
46 #include "asterisk/utils.h"
47 #include "asterisk/lock.h"
48 #include "asterisk/app.h"
49 #include "asterisk/pbx.h"
50 #include "asterisk/linkedlists.h"
51 #include "asterisk/module.h"
52 #include "asterisk/astobj2.h"
53 #include "asterisk/test.h"
54 #include "asterisk/stasis.h"
55 #include "asterisk/json.h"
56 #include "asterisk/stasis_system.h"
59 * The following variable controls the layout of localized sound files.
60 * If 0, use the historical layout with prefix just before the filename
61 * (i.e. digits/en/1.gsm , digits/it/1.gsm or default to digits/1.gsm),
62 * if 1 put the prefix at the beginning of the filename
63 * (i.e. en/digits/1.gsm, it/digits/1.gsm or default to digits/1.gsm).
64 * The latter permits a language to be entirely in one directory.
66 * This is settable in asterisk.conf.
68 int ast_language_is_prefix = 1;
70 static AST_RWLIST_HEAD_STATIC(formats, ast_format_def);
72 STASIS_MESSAGE_TYPE_DEFN(ast_format_register_type);
73 STASIS_MESSAGE_TYPE_DEFN(ast_format_unregister_type);
75 static struct ast_json *json_array_from_list(const char *list, const char *sep)
77 RAII_VAR(struct ast_json *, array, ast_json_array_create(), ast_json_unref);
78 RAII_VAR(char *, stringp, ast_strdup(list), ast_free);
81 if (!array || !stringp) {
85 while ((ext = strsep(&stringp, sep))) {
86 if (ast_json_array_append(array, ast_json_string_create(ext))) {
91 return ast_json_ref(array);
94 static int publish_format_update(const struct ast_format_def *f, struct stasis_message_type *type)
96 RAII_VAR(struct stasis_message *, msg, NULL, ao2_cleanup);
97 RAII_VAR(struct ast_json_payload *, json_payload, NULL, ao2_cleanup);
98 RAII_VAR(struct ast_json *, json_object, NULL, ast_json_unref);
100 json_object = ast_json_pack("{s: s, s: o}",
102 "extensions", json_array_from_list(f->exts, "|"));
107 json_payload = ast_json_payload_create(json_object);
112 msg = stasis_message_create(type, json_payload);
117 stasis_publish(ast_system_topic(), msg);
121 int __ast_format_def_register(const struct ast_format_def *f, struct ast_module *mod)
123 struct ast_format_def *tmp;
125 AST_RWLIST_WRLOCK(&formats);
126 AST_RWLIST_TRAVERSE(&formats, tmp, list) {
127 if (!strcasecmp(f->name, tmp->name)) {
128 AST_RWLIST_UNLOCK(&formats);
129 ast_log(LOG_WARNING, "Tried to register '%s' format, already registered\n", f->name);
133 if (!(tmp = ast_calloc(1, sizeof(*tmp)))) {
134 AST_RWLIST_UNLOCK(&formats);
141 * Align buf_size properly, rounding up to the machine-specific
142 * alignment for pointers.
144 struct _test_align { void *a, *b; } p;
145 int align = (char *)&p.b - (char *)&p.a;
146 tmp->buf_size = ((f->buf_size + align - 1) / align) * align;
149 memset(&tmp->list, 0, sizeof(tmp->list));
151 AST_RWLIST_INSERT_HEAD(&formats, tmp, list);
152 AST_RWLIST_UNLOCK(&formats);
153 ast_verb(2, "Registered file format %s, extension(s) %s\n", f->name, f->exts);
154 publish_format_update(f, ast_format_register_type());
159 int ast_format_def_unregister(const char *name)
161 struct ast_format_def *tmp;
164 AST_RWLIST_WRLOCK(&formats);
165 AST_RWLIST_TRAVERSE_SAFE_BEGIN(&formats, tmp, list) {
166 if (!strcasecmp(name, tmp->name)) {
167 AST_RWLIST_REMOVE_CURRENT(list);
168 publish_format_update(tmp, ast_format_unregister_type());
173 AST_RWLIST_TRAVERSE_SAFE_END;
174 AST_RWLIST_UNLOCK(&formats);
177 ast_verb(2, "Unregistered format %s\n", name);
179 ast_log(LOG_WARNING, "Tried to unregister format %s, already unregistered\n", name);
184 int ast_stopstream(struct ast_channel *tmp)
186 ast_channel_lock(tmp);
188 /* Stop a running stream if there is one */
189 if (ast_channel_stream(tmp)) {
190 ast_closestream(ast_channel_stream(tmp));
191 ast_channel_stream_set(tmp, NULL);
192 if (ast_channel_oldwriteformat(tmp)->id && ast_set_write_format(tmp, ast_channel_oldwriteformat(tmp)))
193 ast_log(LOG_WARNING, "Unable to restore format back to %s\n", ast_getformatname(ast_channel_oldwriteformat(tmp)));
195 /* Stop the video stream too */
196 if (ast_channel_vstream(tmp) != NULL) {
197 ast_closestream(ast_channel_vstream(tmp));
198 ast_channel_vstream_set(tmp, NULL);
201 ast_channel_unlock(tmp);
206 int ast_writestream(struct ast_filestream *fs, struct ast_frame *f)
209 if (f->frametype == AST_FRAME_VIDEO) {
210 if (AST_FORMAT_GET_TYPE(fs->fmt->format.id) == AST_FORMAT_TYPE_AUDIO) {
211 /* This is the audio portion. Call the video one... */
212 if (!fs->vfs && fs->filename) {
213 const char *type = ast_getformatname(&f->subclass.format);
214 fs->vfs = ast_writefile(fs->filename, type, NULL, fs->flags, 0, fs->mode);
215 ast_debug(1, "Opened video output file\n");
218 return ast_writestream(fs->vfs, f);
222 } else if (f->frametype != AST_FRAME_VOICE) {
223 ast_log(LOG_WARNING, "Tried to write non-voice frame\n");
226 if (ast_format_cmp(&f->subclass.format, &fs->fmt->format) != AST_FORMAT_CMP_NOT_EQUAL) {
227 res = fs->fmt->write(fs, f);
229 ast_log(LOG_WARNING, "Natural write failed\n");
231 ast_log(LOG_WARNING, "Huh??\n");
233 /* XXX If they try to send us a type of frame that isn't the normal frame, and isn't
234 the one we've setup a translator for, we do the "wrong thing" XXX */
235 if (fs->trans && (ast_format_cmp(&f->subclass.format, &fs->lastwriteformat) != AST_FORMAT_CMP_EQUAL)) {
236 ast_translator_free_path(fs->trans);
240 fs->trans = ast_translator_build_path(&fs->fmt->format, &f->subclass.format);
242 ast_log(LOG_WARNING, "Unable to translate to format %s, source format %s\n",
243 fs->fmt->name, ast_getformatname(&f->subclass.format));
245 struct ast_frame *trf;
246 ast_format_copy(&fs->lastwriteformat, &f->subclass.format);
247 /* Get the translated frame but don't consume the original in case they're using it on another stream */
248 if ((trf = ast_translate(fs->trans, f, 0))) {
249 struct ast_frame *cur;
251 /* the translator may have returned multiple frames, so process them */
252 for (cur = trf; cur; cur = AST_LIST_NEXT(cur, frame_list)) {
253 if ((res = fs->fmt->write(fs, trf))) {
254 ast_log(LOG_WARNING, "Translated frame write failed\n");
267 static int copy(const char *infile, const char *outfile)
270 char buf[4096]; /* XXX make it lerger. */
272 if ((ifd = open(infile, O_RDONLY)) < 0) {
273 ast_log(LOG_WARNING, "Unable to open %s in read-only mode\n", infile);
276 if ((ofd = open(outfile, O_WRONLY | O_TRUNC | O_CREAT, AST_FILE_MODE)) < 0) {
277 ast_log(LOG_WARNING, "Unable to open %s in write-only mode\n", outfile);
281 while ( (len = read(ifd, buf, sizeof(buf)) ) ) {
284 ast_log(LOG_WARNING, "Read failed on %s: %s\n", infile, strerror(errno));
287 /* XXX handle partial writes */
288 res = write(ofd, buf, len);
290 ast_log(LOG_WARNING, "Write failed on %s (%d of %d): %s\n", outfile, res, len, strerror(errno));
291 len = -1; /* error marker */
299 return -1; /* error */
301 return 0; /* success */
305 * \brief construct a filename. Absolute pathnames are preserved,
306 * relative names are prefixed by the sounds/ directory.
307 * The wav49 suffix is replaced by 'WAV'.
308 * Returns a malloc'ed string to be freed by the caller.
310 static char *build_filename(const char *filename, const char *ext)
314 if (!strcmp(ext, "wav49"))
317 if (filename[0] == '/') {
318 if (ast_asprintf(&fn, "%s.%s", filename, ext) < 0) {
322 if (ast_asprintf(&fn, "%s/sounds/%s.%s",
323 ast_config_AST_DATA_DIR, filename, ext) < 0) {
330 /* compare type against the list 'exts' */
331 /* XXX need a better algorithm */
332 static int exts_compare(const char *exts, const char *type)
335 char *stringp = tmp, *ext;
337 ast_copy_string(tmp, exts, sizeof(tmp));
338 while ((ext = strsep(&stringp, "|"))) {
339 if (!strcmp(ext, type))
348 * \brief Close the file stream by canceling any pending read / write callbacks
350 static void filestream_close(struct ast_filestream *f)
352 enum ast_format_type format_type = AST_FORMAT_GET_TYPE(f->fmt->format.id);
358 /* Stop a running stream if there is one */
361 case AST_FORMAT_TYPE_AUDIO:
362 ast_channel_stream_set(f->owner, NULL);
363 AST_SCHED_DEL_ACCESSOR(ast_channel_sched(f->owner), f->owner, ast_channel_streamid, ast_channel_streamid_set);
364 ast_settimeout(f->owner, 0, NULL, NULL);
366 case AST_FORMAT_TYPE_VIDEO:
367 ast_channel_vstream_set(f->owner, NULL);
368 AST_SCHED_DEL_ACCESSOR(ast_channel_sched(f->owner), f->owner, ast_channel_vstreamid, ast_channel_vstreamid_set);
371 ast_log(AST_LOG_WARNING, "Unable to schedule deletion of filestream with unsupported type %s\n", f->fmt->name);
376 static void filestream_destructor(void *arg)
378 struct ast_filestream *f = arg;
382 /* Stop a running stream if there is one */
385 /* destroy the translator on exit */
387 ast_translator_free_path(f->trans);
390 void (*closefn)(struct ast_filestream *) = f->fmt->close;
398 if (f->realfilename && f->filename) {
399 pid = ast_safe_fork(0);
401 execl("/bin/mv", "mv", "-f", f->filename, f->realfilename, SENTINEL);
405 /* Block the parent until the move is complete.*/
406 waitpid(pid, &status, 0);
413 free(f->realfilename);
415 ast_closestream(f->vfs);
416 if (f->write_buffer) {
417 ast_free(f->write_buffer);
419 if (f->orig_chan_name)
420 free((void *) f->orig_chan_name);
421 ast_module_unref(f->fmt->module);
424 static struct ast_filestream *get_filestream(struct ast_format_def *fmt, FILE *bfile)
426 struct ast_filestream *s;
428 int l = sizeof(*s) + fmt->buf_size + fmt->desc_size; /* total allocation size */
429 if ( (s = ao2_alloc(l, filestream_destructor)) == NULL)
435 s->_private = ((char *)(s + 1)) + fmt->buf_size;
437 s->buf = (char *)(s + 1);
438 s->fr.src = fmt->name;
443 * Default implementations of open and rewrite.
444 * Only use them if you don't have expensive stuff to do.
446 enum wrap_fn { WRAP_OPEN, WRAP_REWRITE };
448 static int fn_wrapper(struct ast_filestream *s, const char *comment, enum wrap_fn mode)
450 struct ast_format_def *f = s->fmt;
452 int (*openfn)(struct ast_filestream *s);
454 if (mode == WRAP_OPEN && (openfn = f->open) && openfn(s))
455 ast_log(LOG_WARNING, "Unable to open format %s\n", f->name);
456 else if (mode == WRAP_REWRITE && f->rewrite && f->rewrite(s, comment))
457 ast_log(LOG_WARNING, "Unable to rewrite format %s\n", f->name);
459 /* preliminary checks succeed. update usecount */
460 ast_module_ref(f->module);
466 static int rewrite_wrapper(struct ast_filestream *s, const char *comment)
468 return fn_wrapper(s, comment, WRAP_REWRITE);
471 static int open_wrapper(struct ast_filestream *s)
473 return fn_wrapper(s, NULL, WRAP_OPEN);
477 ACTION_EXISTS = 1, /* return matching format if file exists, 0 otherwise */
478 ACTION_DELETE, /* delete file, return 0 on success, -1 on error */
479 ACTION_RENAME, /* rename file. return 0 on success, -1 on error */
481 ACTION_COPY /* copy file. return 0 on success, -1 on error */
486 * \brief perform various actions on a file. Second argument
487 * \note arg2 depends on the command:
489 * optional ast_format_cap holding all the formats found for a file, for EXISTS.
490 * destination file name (const char *) for COPY and RENAME
491 * struct ast_channel * for OPEN
492 * if fmt is NULL, OPEN will return the first matching entry,
493 * whereas other functions will run on all matching entries.
495 static int filehelper(const char *filename, const void *arg2, const char *fmt, const enum file_action action)
497 struct ast_format_def *f;
498 int res = (action == ACTION_EXISTS) ? 0 : -1;
500 AST_RWLIST_RDLOCK(&formats);
501 /* Check for a specific format */
502 AST_RWLIST_TRAVERSE(&formats, f, list) {
503 char *stringp, *ext = NULL;
505 if (fmt && !exts_compare(f->exts, fmt))
508 /* Look for a file matching the supported extensions.
509 * The file must exist, and for OPEN, must match
510 * one of the formats supported by the channel.
512 stringp = ast_strdupa(f->exts); /* this is in the stack so does not need to be freed */
513 while ( (ext = strsep(&stringp, "|")) ) {
515 char *fn = build_filename(filename, ext);
520 if ( stat(fn, &st) ) { /* file not existent */
524 /* for 'OPEN' we need to be sure that the format matches
525 * what the channel can process
527 if (action == ACTION_OPEN) {
528 struct ast_channel *chan = (struct ast_channel *)arg2;
530 struct ast_filestream *s;
532 if ((ast_format_cmp(ast_channel_writeformat(chan), &f->format) == AST_FORMAT_CMP_NOT_EQUAL) &&
533 !(((AST_FORMAT_GET_TYPE(f->format.id) == AST_FORMAT_TYPE_AUDIO) && fmt) ||
534 ((AST_FORMAT_GET_TYPE(f->format.id) == AST_FORMAT_TYPE_VIDEO) && fmt))) {
536 continue; /* not a supported format */
538 if ( (bfile = fopen(fn, "r")) == NULL) {
540 continue; /* cannot open file */
542 s = get_filestream(f, bfile);
545 ast_free(fn); /* cannot allocate descriptor */
548 if (open_wrapper(s)) {
551 continue; /* cannot run open on file */
553 if (st.st_size == 0) {
554 ast_log(LOG_WARNING, "File %s detected to have zero size.\n", fn);
556 /* ok this is good for OPEN */
562 if (AST_FORMAT_GET_TYPE(s->fmt->format.id) == AST_FORMAT_TYPE_AUDIO) {
563 if (ast_channel_stream(chan))
564 ast_closestream(ast_channel_stream(chan));
565 ast_channel_stream_set(chan, s);
567 if (ast_channel_vstream(chan))
568 ast_closestream(ast_channel_vstream(chan));
569 ast_channel_vstream_set(chan, s);
576 break; /* will never get here */
578 case ACTION_EXISTS: /* return the matching format */
579 /* if arg2 is present, it is a format capabilities structure.
580 * Add this format to the set of formats this file can be played in */
582 ast_format_cap_add((struct ast_format_cap *) arg2, &f->format);
584 res = 1; /* file does exist and format it exists in is returned in arg2 */
588 if ( (res = unlink(fn)) )
589 ast_log(LOG_WARNING, "unlink(%s) failed: %s\n", fn, strerror(errno));
594 char *nfn = build_filename((const char *)arg2, ext);
596 ast_log(LOG_WARNING, "Out of memory\n");
598 res = action == ACTION_COPY ? copy(fn, nfn) : rename(fn, nfn);
600 ast_log(LOG_WARNING, "%s(%s,%s) failed: %s\n",
601 action == ACTION_COPY ? "copy" : "rename",
602 fn, nfn, strerror(errno));
609 ast_log(LOG_WARNING, "Unknown helper %d\n", action);
614 AST_RWLIST_UNLOCK(&formats);
618 static int is_absolute_path(const char *filename)
620 return filename[0] == '/';
624 * \brief test if a file exists for a given format.
625 * \note result_cap is OPTIONAL
626 * \retval 1, true and result_cap represents format capabilities file exists in.
629 static int fileexists_test(const char *filename, const char *fmt, const char *lang,
630 char *buf, int buflen, struct ast_format_cap *result_cap)
636 if (ast_language_is_prefix && !is_absolute_path(filename)) { /* new layout */
638 snprintf(buf, buflen, "%s/%s", lang, filename);
640 snprintf(buf, buflen, "%s", filename);
642 } else { /* old layout */
643 strcpy(buf, filename); /* first copy the full string */
645 /* insert the language and suffix if needed */
646 const char *c = strrchr(filename, '/');
647 int offset = c ? c - filename + 1 : 0; /* points right after the last '/' */
648 snprintf(buf + offset, buflen - offset, "%s/%s", lang, filename + offset);
652 return filehelper(buf, result_cap, fmt, ACTION_EXISTS);
656 * \brief helper routine to locate a file with a given format
657 * and language preference.
659 * \note Try preflang, preflang with stripped '_' suffices, or NULL.
661 * \note The last parameter(s) point to a buffer of sufficient size,
662 * which on success is filled with the matching filename.
664 * \param filename Name of the file.
665 * \param fmt Format to look for the file in. OPTIONAL
666 * \param preflang The perfered language
667 * \param buf Returns the matching filename
668 * \param buflen Size of the buf
669 * \param result_cap OPTIONAL format capabilities result structure
670 * returns what formats the file was found in.
672 * \retval 1, true. file exists and result format is set
673 * \retval 0, false. file does not exist.
675 static int fileexists_core(const char *filename, const char *fmt, const char *preflang,
676 char *buf, int buflen, struct ast_format_cap *result_cap)
684 /* We try languages in the following order:
685 * preflang (may include dialect and style codes)
686 * lang (preflang without dialect - if any)
688 * default (unless the same as preflang or lang without dialect)
691 lang = ast_strdupa(preflang);
693 /* Try preferred language, including removing any style or dialect codes */
694 while (!ast_strlen_zero(lang)) {
697 if (fileexists_test(filename, fmt, lang, buf, buflen, result_cap)) {
701 if ((end = strrchr(lang, '_')) != NULL) {
709 /* Try without any language */
710 if (fileexists_test(filename, fmt, NULL, buf, buflen, result_cap)) {
714 /* Finally try the default language unless it was already tried before */
715 if ((ast_strlen_zero(preflang) || strcmp(preflang, DEFAULT_LANGUAGE)) && (ast_strlen_zero(lang) || strcmp(lang, DEFAULT_LANGUAGE))) {
716 if ((fileexists_test(filename, fmt, DEFAULT_LANGUAGE, buf, buflen, result_cap)) > 0) {
724 struct ast_filestream *ast_openstream(struct ast_channel *chan, const char *filename, const char *preflang)
726 return ast_openstream_full(chan, filename, preflang, 0);
729 struct ast_filestream *ast_openstream_full(struct ast_channel *chan, const char *filename, const char *preflang, int asis)
732 * Use fileexists_core() to find a file in a compatible
733 * language and format, set up a suitable translator,
734 * and open the stream.
736 struct ast_format_cap *file_fmt_cap;
742 /* do this first, otherwise we detect the wrong writeformat */
743 ast_stopstream(chan);
744 if (ast_channel_generator(chan))
745 ast_deactivate_generator(chan);
747 if (preflang == NULL)
749 buflen = strlen(preflang) + strlen(filename) + 4;
750 buf = ast_alloca(buflen);
752 if (!(file_fmt_cap = ast_format_cap_alloc_nolock())) {
755 if (!fileexists_core(filename, NULL, preflang, buf, buflen, file_fmt_cap) ||
756 !ast_format_cap_has_type(file_fmt_cap, AST_FORMAT_TYPE_AUDIO)) {
758 ast_log(LOG_WARNING, "File %s does not exist in any format\n", filename);
759 file_fmt_cap = ast_format_cap_destroy(file_fmt_cap);
763 /* Set the channel to a format we can work with and save off the previous format. */
764 ast_format_copy(ast_channel_oldwriteformat(chan), ast_channel_writeformat(chan));
765 /* Set the channel to the best format that exists for the file. */
766 res = ast_set_write_format_from_cap(chan, file_fmt_cap);
767 /* don't need this anymore now that the channel's write format is set. */
768 file_fmt_cap = ast_format_cap_destroy(file_fmt_cap);
770 if (res == -1) { /* No format available that works with this channel */
773 res = filehelper(buf, chan, NULL, ACTION_OPEN);
775 return ast_channel_stream(chan);
779 struct ast_filestream *ast_openvstream(struct ast_channel *chan, const char *filename, const char *preflang)
781 /* As above, but for video. But here we don't have translators
782 * so we must enforce a format.
784 struct ast_format tmp_fmt;
785 struct ast_format_cap *tmp_cap;
791 if (preflang == NULL)
793 buflen = strlen(preflang) + strlen(filename) + 4;
794 buf = ast_alloca(buflen);
796 /* is the channel capable of video without translation ?*/
797 if (!ast_format_cap_has_type(ast_channel_nativeformats(chan), AST_FORMAT_TYPE_VIDEO)) {
800 if (!(tmp_cap = ast_format_cap_alloc_nolock())) {
803 /* Video is supported, so see what video formats exist for this file */
804 if (!fileexists_core(filename, NULL, preflang, buf, buflen, tmp_cap)) {
805 tmp_cap = ast_format_cap_destroy(tmp_cap);
809 /* iterate over file formats and pick the first one compatible with the channel's native formats */
810 ast_format_cap_iter_start(tmp_cap);
811 while (!ast_format_cap_iter_next(tmp_cap, &tmp_fmt)) {
812 fmt = ast_getformatname(&tmp_fmt);
813 if ((AST_FORMAT_GET_TYPE(tmp_fmt.id) != AST_FORMAT_TYPE_VIDEO) ||
814 !ast_format_cap_iscompatible(ast_channel_nativeformats(chan), &tmp_fmt)) {
818 fd = filehelper(buf, chan, fmt, ACTION_OPEN);
820 ast_format_cap_iter_end(tmp_cap);
821 tmp_cap = ast_format_cap_destroy(tmp_cap);
822 return ast_channel_vstream(chan);
824 ast_log(LOG_WARNING, "File %s has video but couldn't be opened\n", filename);
826 ast_format_cap_iter_end(tmp_cap);
827 tmp_cap = ast_format_cap_destroy(tmp_cap);
832 static struct ast_frame *read_frame(struct ast_filestream *s, int *whennext)
834 struct ast_frame *fr, *new_fr;
840 if (!(fr = s->fmt->read(s, whennext))) {
844 if (!(new_fr = ast_frisolate(fr))) {
857 struct ast_frame *ast_readframe(struct ast_filestream *s)
861 return read_frame(s, &whennext);
866 FSREAD_SUCCESS_SCHED,
867 FSREAD_SUCCESS_NOSCHED,
870 static int ast_fsread_audio(const void *data);
872 static enum fsread_res ast_readaudio_callback(struct ast_filestream *s)
877 struct ast_frame *fr;
879 if (s->orig_chan_name && strcasecmp(ast_channel_name(s->owner), s->orig_chan_name)) {
883 fr = read_frame(s, &whennext);
885 if (!fr /* stream complete */ || ast_write(s->owner, fr) /* error writing */) {
887 ast_log(LOG_WARNING, "Failed to write frame\n");
898 if (whennext != s->lasttimeout) {
899 if (ast_channel_timingfd(s->owner) > -1) {
900 float samp_rate = (float) ast_format_rate(&s->fmt->format);
903 rate = (unsigned int) roundf(samp_rate / ((float) whennext));
905 ast_settimeout(s->owner, rate, ast_fsread_audio, s);
907 ast_channel_streamid_set(s->owner, ast_sched_add(ast_channel_sched(s->owner), whennext / (ast_format_rate(&s->fmt->format) / 1000), ast_fsread_audio, s));
909 s->lasttimeout = whennext;
910 return FSREAD_SUCCESS_NOSCHED;
912 return FSREAD_SUCCESS_SCHED;
915 ast_channel_streamid_set(s->owner, -1);
916 ast_settimeout(s->owner, 0, NULL, NULL);
917 return FSREAD_FAILURE;
920 static int ast_fsread_audio(const void *data)
922 struct ast_filestream *fs = (struct ast_filestream *)data;
925 res = ast_readaudio_callback(fs);
927 if (res == FSREAD_SUCCESS_SCHED)
933 static int ast_fsread_video(const void *data);
935 static enum fsread_res ast_readvideo_callback(struct ast_filestream *s)
940 struct ast_frame *fr = read_frame(s, &whennext);
942 if (!fr /* stream complete */ || ast_write(s->owner, fr) /* error writing */) {
944 ast_log(LOG_WARNING, "Failed to write frame\n");
947 ast_channel_vstreamid_set(s->owner, -1);
948 return FSREAD_FAILURE;
956 if (whennext != s->lasttimeout) {
957 ast_channel_vstreamid_set(s->owner, ast_sched_add(ast_channel_sched(s->owner), whennext / (ast_format_rate(&s->fmt->format) / 1000), ast_fsread_video, s));
958 s->lasttimeout = whennext;
959 return FSREAD_SUCCESS_NOSCHED;
962 return FSREAD_SUCCESS_SCHED;
965 static int ast_fsread_video(const void *data)
967 struct ast_filestream *fs = (struct ast_filestream *)data;
970 res = ast_readvideo_callback(fs);
972 if (res == FSREAD_SUCCESS_SCHED)
978 int ast_applystream(struct ast_channel *chan, struct ast_filestream *s)
984 int ast_playstream(struct ast_filestream *s)
988 if (AST_FORMAT_GET_TYPE(s->fmt->format.id) == AST_FORMAT_TYPE_AUDIO)
989 res = ast_readaudio_callback(s);
991 res = ast_readvideo_callback(s);
993 return (res == FSREAD_FAILURE) ? -1 : 0;
996 int ast_seekstream(struct ast_filestream *fs, off_t sample_offset, int whence)
998 return fs->fmt->seek(fs, sample_offset, whence);
1001 int ast_truncstream(struct ast_filestream *fs)
1003 return fs->fmt->trunc(fs);
1006 off_t ast_tellstream(struct ast_filestream *fs)
1008 return fs->fmt->tell(fs);
1011 int ast_stream_fastforward(struct ast_filestream *fs, off_t ms)
1013 return ast_seekstream(fs, ms * DEFAULT_SAMPLES_PER_MS, SEEK_CUR);
1016 int ast_stream_rewind(struct ast_filestream *fs, off_t ms)
1018 return ast_seekstream(fs, -ms * DEFAULT_SAMPLES_PER_MS, SEEK_CUR);
1021 int ast_closestream(struct ast_filestream *f)
1023 /* This used to destroy the filestream, but it now just decrements a refcount.
1024 * We close the stream in order to quit queuing frames now, because we might
1025 * change the writeformat, which could result in a subsequent write error, if
1026 * the format is different. */
1030 filestream_close(f);
1037 * Look the various language-specific places where a file could exist.
1039 int ast_fileexists(const char *filename, const char *fmt, const char *preflang)
1044 if (preflang == NULL)
1046 buflen = strlen(preflang) + strlen(filename) + 4; /* room for everything */
1047 buf = ast_alloca(buflen);
1048 return fileexists_core(filename, fmt, preflang, buf, buflen, NULL) ? 1 : 0;
1051 int ast_filedelete(const char *filename, const char *fmt)
1053 return filehelper(filename, NULL, fmt, ACTION_DELETE);
1056 int ast_filerename(const char *filename, const char *filename2, const char *fmt)
1058 return filehelper(filename, filename2, fmt, ACTION_RENAME);
1061 int ast_filecopy(const char *filename, const char *filename2, const char *fmt)
1063 return filehelper(filename, filename2, fmt, ACTION_COPY);
1066 int ast_streamfile(struct ast_channel *chan, const char *filename, const char *preflang)
1068 struct ast_filestream *fs;
1069 struct ast_filestream *vfs=NULL;
1075 fs = ast_openstream(chan, filename, preflang);
1077 ast_log(LOG_WARNING, "Unable to open %s (format %s): %s\n", filename, ast_getformatname_multiple(fmt, sizeof(fmt), ast_channel_nativeformats(chan)), strerror(errno));
1081 /* check to see if there is any data present (not a zero length file),
1082 * done this way because there is no where for ast_openstream_full to
1083 * return the file had no data. */
1084 pos = ftello(fs->f);
1085 seekattempt = fseeko(fs->f, -1, SEEK_END);
1087 if (errno == EINVAL) {
1088 /* Zero-length file, as opposed to a pipe */
1091 ast_seekstream(fs, 0, SEEK_SET);
1094 fseeko(fs->f, pos, SEEK_SET);
1097 vfs = ast_openvstream(chan, filename, preflang);
1099 ast_debug(1, "Ooh, found a video stream, too, format %s\n", ast_getformatname(&vfs->fmt->format));
1102 if (ast_test_flag(ast_channel_flags(chan), AST_FLAG_MASQ_NOSTREAM))
1103 fs->orig_chan_name = ast_strdup(ast_channel_name(chan));
1104 if (ast_applystream(chan, fs))
1106 if (vfs && ast_applystream(chan, vfs))
1108 res = ast_playstream(fs);
1110 res = ast_playstream(vfs);
1111 ast_verb(3, "<%s> Playing '%s.%s' (language '%s')\n", ast_channel_name(chan), filename, ast_getformatname(ast_channel_writeformat(chan)), preflang ? preflang : "default");
1116 struct ast_filestream *ast_readfile(const char *filename, const char *type, const char *comment, int flags, int check, mode_t mode)
1119 struct ast_format_def *f;
1120 struct ast_filestream *fs = NULL;
1122 int format_found = 0;
1124 AST_RWLIST_RDLOCK(&formats);
1126 AST_RWLIST_TRAVERSE(&formats, f, list) {
1128 if (!exts_compare(f->exts, type))
1133 fn = build_filename(filename, type);
1138 bfile = fopen(fn, "r");
1140 if (!bfile || (fs = get_filestream(f, bfile)) == NULL || open_wrapper(fs) ) {
1141 ast_log(LOG_WARNING, "Unable to open %s\n", fn);
1143 ast_closestream(fs);
1155 fs->filename = ast_strdup(filename);
1161 AST_RWLIST_UNLOCK(&formats);
1163 ast_log(LOG_WARNING, "No such format '%s'\n", type);
1168 struct ast_filestream *ast_writefile(const char *filename, const char *type, const char *comment, int flags, int check, mode_t mode)
1170 int fd, myflags = 0;
1171 /* compiler claims this variable can be used before initialization... */
1173 struct ast_format_def *f;
1174 struct ast_filestream *fs = NULL;
1177 int format_found = 0;
1179 AST_RWLIST_RDLOCK(&formats);
1181 /* set the O_TRUNC flag if and only if there is no O_APPEND specified */
1182 /* We really can't use O_APPEND as it will break WAV header updates */
1183 if (flags & O_APPEND) {
1189 myflags |= O_WRONLY | O_CREAT;
1191 /* XXX need to fix this - we should just do the fopen,
1192 * not open followed by fdopen()
1194 AST_RWLIST_TRAVERSE(&formats, f, list) {
1195 char *fn, *orig_fn = NULL;
1199 if (!exts_compare(f->exts, type))
1204 fn = build_filename(filename, type);
1208 fd = open(fn, flags | myflags, mode);
1210 /* fdopen() the resulting file stream */
1211 bfile = fdopen(fd, ((flags | myflags) & O_RDWR) ? "w+" : "w");
1213 ast_log(LOG_WARNING, "Whoa, fdopen failed: %s!\n", strerror(errno));
1219 if (ast_opt_cache_record_files && (fd > -1)) {
1222 fclose(bfile); /* this also closes fd */
1224 We touch orig_fn just as a place-holder so other things (like vmail) see the file is there.
1225 What we are really doing is writing to record_cache_dir until we are done then we will mv the file into place.
1227 orig_fn = ast_strdupa(fn);
1228 for (c = fn; *c; c++)
1232 size = strlen(fn) + strlen(record_cache_dir) + 2;
1233 buf = ast_alloca(size);
1234 strcpy(buf, record_cache_dir);
1239 fd = open(fn, flags | myflags, mode);
1241 /* fdopen() the resulting file stream */
1242 bfile = fdopen(fd, ((flags | myflags) & O_RDWR) ? "w+" : "w");
1244 ast_log(LOG_WARNING, "Whoa, fdopen failed: %s!\n", strerror(errno));
1252 fs = get_filestream(f, bfile);
1254 if ((fs->write_buffer = ast_malloc(32768))) {
1255 setvbuf(fs->f, fs->write_buffer, _IOFBF, 32768);
1258 if (!fs || rewrite_wrapper(fs, comment)) {
1259 ast_log(LOG_WARNING, "Unable to rewrite %s\n", fn);
1266 ast_closestream(fs);
1279 fs->realfilename = ast_strdup(orig_fn);
1280 fs->filename = ast_strdup(fn);
1282 fs->realfilename = NULL;
1283 fs->filename = ast_strdup(filename);
1286 /* If truncated, we'll be at the beginning; if not truncated, then append */
1287 f->seek(fs, 0, SEEK_END);
1288 } else if (errno != EEXIST) {
1289 ast_log(LOG_WARNING, "Unable to open file %s: %s\n", fn, strerror(errno));
1293 /* if buf != NULL then fn is already free and pointing to it */
1298 AST_RWLIST_UNLOCK(&formats);
1301 ast_log(LOG_WARNING, "No such format '%s'\n", type);
1306 static void waitstream_control(struct ast_channel *c,
1307 enum ast_waitstream_fr_cb_values type,
1308 ast_waitstream_fr_cb cb,
1313 case AST_WAITSTREAM_CB_FASTFORWARD:
1316 ast_stream_fastforward(ast_channel_stream(c), skip_ms);
1317 eoftest = fgetc(ast_channel_stream(c)->f);
1318 if (feof(ast_channel_stream(c)->f)) {
1319 ast_stream_rewind(ast_channel_stream(c), skip_ms);
1321 ungetc(eoftest, ast_channel_stream(c)->f);
1325 case AST_WAITSTREAM_CB_REWIND:
1326 ast_stream_rewind(ast_channel_stream(c), skip_ms);
1333 long ms_len = ast_tellstream(ast_channel_stream(c)) / (ast_format_rate(&ast_channel_stream(c)->fmt->format) / 1000);
1334 cb(c, ms_len, type);
1337 ast_test_suite_event_notify("PLAYBACK","Channel: %s\r\n"
1340 ast_channel_name(c),
1341 (type == AST_WAITSTREAM_CB_FASTFORWARD) ? "FastForward" : "Rewind",
1346 * \brief the core of all waitstream() functions
1348 static int waitstream_core(struct ast_channel *c,
1349 const char *breakon,
1350 const char *forward,
1351 const char *reverse,
1355 const char *context,
1356 ast_waitstream_fr_cb cb)
1358 const char *orig_chan_name = NULL;
1369 /* Switch the channel to end DTMF frame only. waitstream_core doesn't care about the start of DTMF. */
1370 ast_set_flag(ast_channel_flags(c), AST_FLAG_END_DTMF_ONLY);
1372 if (ast_test_flag(ast_channel_flags(c), AST_FLAG_MASQ_NOSTREAM))
1373 orig_chan_name = ast_strdupa(ast_channel_name(c));
1375 if (ast_channel_stream(c) && cb) {
1376 long ms_len = ast_tellstream(ast_channel_stream(c)) / (ast_format_rate(&ast_channel_stream(c)->fmt->format) / 1000);
1377 cb(c, ms_len, AST_WAITSTREAM_CB_START);
1380 while (ast_channel_stream(c)) {
1384 if (orig_chan_name && strcasecmp(orig_chan_name, ast_channel_name(c))) {
1390 ms = ast_sched_wait(ast_channel_sched(c));
1392 if (ms < 0 && !ast_channel_timingfunc(c)) {
1399 res = ast_waitfor(c, ms);
1401 ast_log(LOG_WARNING, "Select failed (%s)\n", strerror(errno));
1402 ast_clear_flag(ast_channel_flags(c), AST_FLAG_END_DTMF_ONLY);
1407 struct ast_channel *rchan = ast_waitfor_nandfds(&c, 1, &cmdfd, (cmdfd > -1) ? 1 : 0, NULL, &outfd, &ms);
1408 if (!rchan && (outfd < 0) && (ms)) {
1412 ast_log(LOG_WARNING, "Wait failed (%s)\n", strerror(errno));
1413 ast_clear_flag(ast_channel_flags(c), AST_FLAG_END_DTMF_ONLY);
1415 } else if (outfd > -1) { /* this requires cmdfd set */
1416 /* The FD we were watching has something waiting */
1417 ast_clear_flag(ast_channel_flags(c), AST_FLAG_END_DTMF_ONLY);
1420 /* if rchan is set, it is 'c' */
1421 res = rchan ? 1 : 0; /* map into 'res' values */
1424 struct ast_frame *fr = ast_read(c);
1426 ast_clear_flag(ast_channel_flags(c), AST_FLAG_END_DTMF_ONLY);
1429 switch (fr->frametype) {
1430 case AST_FRAME_DTMF_END:
1432 const char exten[2] = { fr->subclass.integer, '\0' };
1433 if (ast_exists_extension(c, context, exten, 1,
1434 S_COR(ast_channel_caller(c)->id.number.valid, ast_channel_caller(c)->id.number.str, NULL))) {
1435 res = fr->subclass.integer;
1437 ast_clear_flag(ast_channel_flags(c), AST_FLAG_END_DTMF_ONLY);
1441 res = fr->subclass.integer;
1442 if (strchr(forward, res)) {
1443 waitstream_control(c, AST_WAITSTREAM_CB_FASTFORWARD, cb, skip_ms);
1444 } else if (strchr(reverse, res)) {
1445 waitstream_control(c, AST_WAITSTREAM_CB_REWIND, cb, skip_ms);
1446 } else if (strchr(breakon, res)) {
1447 ast_test_suite_event_notify("PLAYBACK","Channel: %s\r\n"
1449 ast_channel_name(c),
1453 ast_clear_flag(ast_channel_flags(c), AST_FLAG_END_DTMF_ONLY);
1458 case AST_FRAME_CONTROL:
1459 switch (fr->subclass.integer) {
1460 case AST_CONTROL_STREAM_STOP:
1461 case AST_CONTROL_STREAM_SUSPEND:
1462 case AST_CONTROL_STREAM_RESTART:
1463 /* Fall-through and break out */
1464 ast_test_suite_event_notify("PLAYBACK","Channel: %s\r\n"
1466 ast_channel_name(c),
1468 res = fr->subclass.integer;
1470 ast_clear_flag(ast_channel_flags(c), AST_FLAG_END_DTMF_ONLY);
1472 case AST_CONTROL_STREAM_REVERSE:
1476 waitstream_control(c, AST_WAITSTREAM_CB_REWIND, cb, skip_ms);
1478 case AST_CONTROL_STREAM_FORWARD:
1482 waitstream_control(c, AST_WAITSTREAM_CB_FASTFORWARD, cb, skip_ms);
1484 case AST_CONTROL_HANGUP:
1485 case AST_CONTROL_BUSY:
1486 case AST_CONTROL_CONGESTION:
1488 ast_clear_flag(ast_channel_flags(c), AST_FLAG_END_DTMF_ONLY);
1490 case AST_CONTROL_RINGING:
1491 case AST_CONTROL_ANSWER:
1492 case AST_CONTROL_VIDUPDATE:
1493 case AST_CONTROL_SRCUPDATE:
1494 case AST_CONTROL_SRCCHANGE:
1495 case AST_CONTROL_HOLD:
1496 case AST_CONTROL_UNHOLD:
1497 case AST_CONTROL_CONNECTED_LINE:
1498 case AST_CONTROL_REDIRECTING:
1499 case AST_CONTROL_AOC:
1500 case AST_CONTROL_UPDATE_RTP_PEER:
1501 case AST_CONTROL_PVT_CAUSE_CODE:
1506 ast_log(LOG_WARNING, "Unexpected control subclass '%d'\n", fr->subclass.integer);
1509 case AST_FRAME_VOICE:
1510 /* Write audio if appropriate */
1512 if (write(audiofd, fr->data.ptr, fr->datalen) < 0) {
1513 ast_log(LOG_WARNING, "write() failed: %s\n", strerror(errno));
1517 /* Ignore all others */
1522 ast_sched_runq(ast_channel_sched(c));
1525 ast_clear_flag(ast_channel_flags(c), AST_FLAG_END_DTMF_ONLY);
1527 return (err || ast_channel_softhangup_internal_flag(c)) ? -1 : 0;
1530 int ast_waitstream_fr_w_cb(struct ast_channel *c,
1531 const char *breakon,
1532 const char *forward,
1533 const char *reverse,
1535 ast_waitstream_fr_cb cb)
1537 return waitstream_core(c, breakon, forward, reverse, ms,
1538 -1 /* no audiofd */, -1 /* no cmdfd */, NULL /* no context */, cb);
1541 int ast_waitstream_fr(struct ast_channel *c, const char *breakon, const char *forward, const char *reverse, int ms)
1543 return waitstream_core(c, breakon, forward, reverse, ms,
1544 -1 /* no audiofd */, -1 /* no cmdfd */, NULL /* no context */, NULL /* no callback */);
1548 * \brief Clean up the return value of a waitstream call
1550 * It's possible for a control frame to come in from an external source and break the
1551 * playback. From a consumer of most ast_waitstream_* function callers, this should
1552 * appear like normal playback termination, i.e., return 0 and not the value of the
1555 static int sanitize_waitstream_return(int return_value)
1557 switch (return_value) {
1558 case AST_CONTROL_STREAM_STOP:
1559 case AST_CONTROL_STREAM_SUSPEND:
1560 case AST_CONTROL_STREAM_RESTART:
1561 /* Fall through and set return_value to 0 */
1569 return return_value;
1572 int ast_waitstream(struct ast_channel *c, const char *breakon)
1576 res = waitstream_core(c, breakon, NULL, NULL, 0, -1, -1, NULL, NULL /* no callback */);
1578 return sanitize_waitstream_return(res);
1581 int ast_waitstream_full(struct ast_channel *c, const char *breakon, int audiofd, int cmdfd)
1585 res = waitstream_core(c, breakon, NULL, NULL, 0,
1586 audiofd, cmdfd, NULL /* no context */, NULL /* no callback */);
1588 return sanitize_waitstream_return(res);
1591 int ast_waitstream_exten(struct ast_channel *c, const char *context)
1595 /* Waitstream, with return in the case of a valid 1 digit extension */
1596 /* in the current or specified context being pressed */
1598 context = ast_channel_context(c);
1599 res = waitstream_core(c, NULL, NULL, NULL, 0,
1600 -1, -1, context, NULL /* no callback */);
1602 return sanitize_waitstream_return(res);
1606 * if the file name is non-empty, try to play it.
1607 * Return 0 if success, -1 if error, digit if interrupted by a digit.
1608 * If digits == "" then we can simply check for non-zero.
1610 int ast_stream_and_wait(struct ast_channel *chan, const char *file, const char *digits)
1613 if (!ast_strlen_zero(file)) {
1614 ast_test_suite_event_notify("PLAYBACK", "Message: %s\r\nChannel: %s", file, ast_channel_name(chan));
1615 res = ast_streamfile(chan, file, ast_channel_language(chan));
1617 res = ast_waitstream(chan, digits);
1623 char *ast_format_str_reduce(char *fmts)
1625 struct ast_format_def *f;
1626 struct ast_format_def *fmts_ptr[AST_MAX_FORMATS];
1627 char *fmts_str[AST_MAX_FORMATS];
1628 char *stringp, *type;
1630 int i, j, x, first, found = 0;
1631 int len = strlen(fmts) + 1;
1634 if (AST_RWLIST_RDLOCK(&formats)) {
1635 ast_log(LOG_WARNING, "Unable to lock format list\n");
1639 stringp = ast_strdupa(fmts);
1641 for (x = 0; (type = strsep(&stringp, "|")) && x < AST_MAX_FORMATS; x++) {
1642 AST_RWLIST_TRAVERSE(&formats, f, list) {
1643 if (exts_compare(f->exts, type)) {
1656 AST_RWLIST_UNLOCK(&formats);
1659 for (i = 0; i < x; i++) {
1660 /* ignore invalid entries */
1662 ast_log(LOG_WARNING, "ignoring unknown format '%s'\n", fmts_str[i]);
1666 /* special handling for the first entry */
1668 res = snprintf(fmts, len, "%s", fmts_str[i]);
1676 for (j = 0; j < i; j++) {
1677 /* this is a duplicate */
1678 if (fmts_ptr[j] == fmts_ptr[i]) {
1685 res = snprintf(fmts, len, "|%s", fmts_str[i]);
1692 ast_log(LOG_WARNING, "no known formats found in format list (%s)\n", orig);
1699 static char *handle_cli_core_show_file_formats(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1701 #define FORMAT "%-10s %-10s %-20s\n"
1702 #define FORMAT2 "%-10s %-10s %-20s\n"
1703 struct ast_format_def *f;
1708 e->command = "core show file formats";
1710 "Usage: core show file formats\n"
1711 " Displays currently registered file formats (if any).\n";
1718 return CLI_SHOWUSAGE;
1720 ast_cli(a->fd, FORMAT, "Format", "Name", "Extensions");
1721 ast_cli(a->fd, FORMAT, "------", "----", "----------");
1723 AST_RWLIST_RDLOCK(&formats);
1724 AST_RWLIST_TRAVERSE(&formats, f, list) {
1725 ast_cli(a->fd, FORMAT2, ast_getformatname(&f->format), f->name, f->exts);
1728 AST_RWLIST_UNLOCK(&formats);
1729 ast_cli(a->fd, "%d file formats registered.\n", count_fmt);
1735 const struct ast_format *ast_get_format_for_file_ext(const char *file_ext)
1737 struct ast_format_def *f;
1738 SCOPED_RDLOCK(lock, &formats.lock);
1739 AST_RWLIST_TRAVERSE(&formats, f, list) {
1740 if (exts_compare(f->exts, file_ext)) {
1748 static struct ast_cli_entry cli_file[] = {
1749 AST_CLI_DEFINE(handle_cli_core_show_file_formats, "Displays file formats")
1752 static void file_shutdown(void)
1754 ast_cli_unregister_multiple(cli_file, ARRAY_LEN(cli_file));
1755 STASIS_MESSAGE_TYPE_CLEANUP(ast_format_register_type);
1756 STASIS_MESSAGE_TYPE_CLEANUP(ast_format_unregister_type);
1759 int ast_file_init(void)
1761 STASIS_MESSAGE_TYPE_INIT(ast_format_register_type);
1762 STASIS_MESSAGE_TYPE_INIT(ast_format_unregister_type);
1763 ast_cli_register_multiple(cli_file, ARRAY_LEN(cli_file));
1764 ast_register_atexit(file_shutdown);