file: Ensure nativeformats remains valid for lifetime of use.
[asterisk/asterisk.git] / main / file.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999 - 2006, Digium, Inc.
5  *
6  * Mark Spencer <markster@digium.com>
7  *
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.
13  *
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.
17  */
18
19 /*! \file
20  *
21  * \brief Generic File Format Support.
22  *
23  * \author Mark Spencer <markster@digium.com>
24  */
25
26 /*** MODULEINFO
27         <support_level>core</support_level>
28  ***/
29
30 #include "asterisk.h"
31
32 ASTERISK_REGISTER_FILE()
33
34 #include <dirent.h>
35 #include <sys/stat.h>
36 #include <sys/wait.h>
37 #include <math.h>
38
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"
57 #include "asterisk/media_cache.h"
58
59 /*! \brief
60  * The following variable controls the layout of localized sound files.
61  * If 0, use the historical layout with prefix just before the filename
62  * (i.e. digits/en/1.gsm , digits/it/1.gsm or default to digits/1.gsm),
63  * if 1 put the prefix at the beginning of the filename
64  * (i.e. en/digits/1.gsm, it/digits/1.gsm or default to digits/1.gsm).
65  * The latter permits a language to be entirely in one directory.
66  *
67  * This is settable in asterisk.conf.
68  */
69 int ast_language_is_prefix = 1;
70
71 static AST_RWLIST_HEAD_STATIC(formats, ast_format_def);
72
73 STASIS_MESSAGE_TYPE_DEFN(ast_format_register_type);
74 STASIS_MESSAGE_TYPE_DEFN(ast_format_unregister_type);
75
76 static struct ast_json *json_array_from_list(const char *list, const char *sep)
77 {
78         RAII_VAR(struct ast_json *, array, ast_json_array_create(), ast_json_unref);
79         char *stringp, *ext;
80
81         stringp = ast_strdupa(list);    /* this is in the stack so does not need to be freed */
82         if (!array || !stringp) {
83                 return NULL;
84         }
85
86         while ((ext = strsep(&stringp, sep))) {
87                 if (ast_json_array_append(array, ast_json_string_create(ext))) {
88                         return NULL;
89                 }
90         }
91
92         return ast_json_ref(array);
93 }
94
95 static int publish_format_update(const struct ast_format_def *f, struct stasis_message_type *type)
96 {
97         RAII_VAR(struct stasis_message *, msg, NULL, ao2_cleanup);
98         RAII_VAR(struct ast_json_payload *, json_payload, NULL, ao2_cleanup);
99         RAII_VAR(struct ast_json *, json_object, NULL, ast_json_unref);
100
101         if (!type) {
102                 return -1;
103         }
104
105         json_object = ast_json_pack("{s: s, s: o}",
106                 "format", f->name,
107                 "extensions", json_array_from_list(f->exts, "|"));
108         if (!json_object) {
109                 return -1;
110         }
111
112         json_payload = ast_json_payload_create(json_object);
113         if (!json_payload) {
114                 return -1;
115         }
116
117         msg = stasis_message_create(type, json_payload);
118         if (!msg) {
119                 return -1;
120         }
121
122         stasis_publish(ast_system_topic(), msg);
123         return 0;
124 }
125
126 int __ast_format_def_register(const struct ast_format_def *f, struct ast_module *mod)
127 {
128         struct ast_format_def *tmp;
129
130         AST_RWLIST_WRLOCK(&formats);
131         AST_RWLIST_TRAVERSE(&formats, tmp, list) {
132                 if (!strcasecmp(f->name, tmp->name)) {
133                         AST_RWLIST_UNLOCK(&formats);
134                         ast_log(LOG_WARNING, "Tried to register '%s' format, already registered\n", f->name);
135                         return -1;
136                 }
137         }
138         if (!(tmp = ast_calloc(1, sizeof(*tmp)))) {
139                 AST_RWLIST_UNLOCK(&formats);
140                 return -1;
141         }
142         *tmp = *f;
143         tmp->module = mod;
144         if (tmp->buf_size) {
145                 /*
146                  * Align buf_size properly, rounding up to the machine-specific
147                  * alignment for pointers.
148                  */
149                 struct _test_align { void *a, *b; } p;
150                 int align = (char *)&p.b - (char *)&p.a;
151                 tmp->buf_size = ((f->buf_size + align - 1) / align) * align;
152         }
153
154         memset(&tmp->list, 0, sizeof(tmp->list));
155
156         AST_RWLIST_INSERT_HEAD(&formats, tmp, list);
157         AST_RWLIST_UNLOCK(&formats);
158         ast_verb(2, "Registered file format %s, extension(s) %s\n", f->name, f->exts);
159         publish_format_update(f, ast_format_register_type());
160
161         return 0;
162 }
163
164 int ast_format_def_unregister(const char *name)
165 {
166         struct ast_format_def *tmp;
167         int res = -1;
168
169         AST_RWLIST_WRLOCK(&formats);
170         AST_RWLIST_TRAVERSE_SAFE_BEGIN(&formats, tmp, list) {
171                 if (!strcasecmp(name, tmp->name)) {
172                         AST_RWLIST_REMOVE_CURRENT(list);
173                         publish_format_update(tmp, ast_format_unregister_type());
174                         ast_free(tmp);
175                         res = 0;
176                 }
177         }
178         AST_RWLIST_TRAVERSE_SAFE_END;
179         AST_RWLIST_UNLOCK(&formats);
180
181         if (!res)
182                 ast_verb(2, "Unregistered format %s\n", name);
183         else
184                 ast_log(LOG_WARNING, "Tried to unregister format %s, already unregistered\n", name);
185
186         return res;
187 }
188
189 int ast_stopstream(struct ast_channel *tmp)
190 {
191         ast_channel_lock(tmp);
192
193         /* Stop a running stream if there is one */
194         if (ast_channel_stream(tmp)) {
195                 ast_closestream(ast_channel_stream(tmp));
196                 ast_channel_stream_set(tmp, NULL);
197                 if (ast_channel_oldwriteformat(tmp) && ast_set_write_format(tmp, ast_channel_oldwriteformat(tmp)))
198                         ast_log(LOG_WARNING, "Unable to restore format back to %s\n", ast_format_get_name(ast_channel_oldwriteformat(tmp)));
199         }
200         /* Stop the video stream too */
201         if (ast_channel_vstream(tmp) != NULL) {
202                 ast_closestream(ast_channel_vstream(tmp));
203                 ast_channel_vstream_set(tmp, NULL);
204         }
205
206         ast_channel_unlock(tmp);
207
208         return 0;
209 }
210
211 int ast_writestream(struct ast_filestream *fs, struct ast_frame *f)
212 {
213         int res = -1;
214         if (f->frametype == AST_FRAME_VIDEO) {
215                 if (ast_format_get_type(fs->fmt->format) == AST_MEDIA_TYPE_AUDIO) {
216                         /* This is the audio portion.  Call the video one... */
217                         if (!fs->vfs && fs->filename) {
218                                 const char *type = ast_format_get_name(f->subclass.format);
219                                 fs->vfs = ast_writefile(fs->filename, type, NULL, fs->flags, 0, fs->mode);
220                                 ast_debug(1, "Opened video output file\n");
221                         }
222                         if (fs->vfs)
223                                 return ast_writestream(fs->vfs, f);
224                         /* else ignore */
225                         return 0;
226                 }
227         } else if (f->frametype != AST_FRAME_VOICE) {
228                 ast_log(LOG_WARNING, "Tried to write non-voice frame\n");
229                 return -1;
230         }
231         if (ast_format_cmp(f->subclass.format, fs->fmt->format) != AST_FORMAT_CMP_NOT_EQUAL) {
232                 res =  fs->fmt->write(fs, f);
233                 if (res < 0)
234                         ast_log(LOG_WARNING, "Natural write failed\n");
235                 else if (res > 0)
236                         ast_log(LOG_WARNING, "Huh??\n");
237         } else {
238                 /* XXX If they try to send us a type of frame that isn't the normal frame, and isn't
239                        the one we've setup a translator for, we do the "wrong thing" XXX */
240                 if (fs->trans && (ast_format_cmp(f->subclass.format, fs->lastwriteformat) != AST_FORMAT_CMP_EQUAL)) {
241                         ast_translator_free_path(fs->trans);
242                         fs->trans = NULL;
243                 }
244                 if (!fs->trans) {
245                         fs->trans = ast_translator_build_path(fs->fmt->format, f->subclass.format);
246                 }
247                 if (!fs->trans) {
248                         ast_log(LOG_WARNING, "Unable to translate to format %s, source format %s\n",
249                                 fs->fmt->name, ast_format_get_name(f->subclass.format));
250                 } else {
251                         struct ast_frame *trf;
252                         ao2_replace(fs->lastwriteformat, f->subclass.format);
253                         /* Get the translated frame but don't consume the original in case they're using it on another stream */
254                         if ((trf = ast_translate(fs->trans, f, 0))) {
255                                 struct ast_frame *cur;
256
257                                 /* the translator may have returned multiple frames, so process them */
258                                 for (cur = trf; cur; cur = AST_LIST_NEXT(cur, frame_list)) {
259                                         if ((res = fs->fmt->write(fs, trf))) {
260                                                 ast_log(LOG_WARNING, "Translated frame write failed\n");
261                                                 break;
262                                         }
263                                 }
264                                 ast_frfree(trf);
265                         } else {
266                                 res = 0;
267                         }
268                 }
269         }
270         return res;
271 }
272
273 static int copy(const char *infile, const char *outfile)
274 {
275         int ifd, ofd, len;
276         char buf[4096]; /* XXX make it lerger. */
277
278         if ((ifd = open(infile, O_RDONLY)) < 0) {
279                 ast_log(LOG_WARNING, "Unable to open %s in read-only mode\n", infile);
280                 return -1;
281         }
282         if ((ofd = open(outfile, O_WRONLY | O_TRUNC | O_CREAT, AST_FILE_MODE)) < 0) {
283                 ast_log(LOG_WARNING, "Unable to open %s in write-only mode\n", outfile);
284                 close(ifd);
285                 return -1;
286         }
287         while ( (len = read(ifd, buf, sizeof(buf)) ) ) {
288                 int res;
289                 if (len < 0) {
290                         ast_log(LOG_WARNING, "Read failed on %s: %s\n", infile, strerror(errno));
291                         break;
292                 }
293                 /* XXX handle partial writes */
294                 res = write(ofd, buf, len);
295                 if (res != len) {
296                         ast_log(LOG_WARNING, "Write failed on %s (%d of %d): %s\n", outfile, res, len, strerror(errno));
297                         len = -1; /* error marker */
298                         break;
299                 }
300         }
301         close(ifd);
302         close(ofd);
303         if (len < 0) {
304                 unlink(outfile);
305                 return -1; /* error */
306         }
307         return 0;       /* success */
308 }
309
310 /*!
311  * \brief construct a filename. Absolute pathnames are preserved,
312  * relative names are prefixed by the sounds/ directory.
313  * The wav49 suffix is replaced by 'WAV'.
314  * Returns a malloc'ed string to be freed by the caller.
315  */
316 static char *build_filename(const char *filename, const char *ext)
317 {
318         char *fn = NULL;
319
320         if (!strcmp(ext, "wav49"))
321                 ext = "WAV";
322
323         if (filename[0] == '/') {
324                 if (ast_asprintf(&fn, "%s.%s", filename, ext) < 0) {
325                         fn = NULL;
326                 }
327         } else {
328                 if (ast_asprintf(&fn, "%s/sounds/%s.%s",
329                              ast_config_AST_DATA_DIR, filename, ext) < 0) {
330                         fn = NULL;
331                 }
332         }
333         return fn;
334 }
335
336 /* compare type against the list 'exts' */
337 /* XXX need a better algorithm */
338 static int exts_compare(const char *exts, const char *type)
339 {
340         char tmp[256];
341         char *stringp = tmp, *ext;
342
343         ast_copy_string(tmp, exts, sizeof(tmp));
344         while ((ext = strsep(&stringp, "|"))) {
345                 if (!strcmp(ext, type))
346                         return 1;
347         }
348
349         return 0;
350 }
351
352 /*!
353  * \internal
354  * \brief Close the file stream by canceling any pending read / write callbacks
355  */
356 static void filestream_close(struct ast_filestream *f)
357 {
358         enum ast_media_type format_type = ast_format_get_type(f->fmt->format);
359
360         if (!f->owner) {
361                 return;
362         }
363
364         /* Stop a running stream if there is one */
365         switch (format_type)
366         {
367         case AST_MEDIA_TYPE_AUDIO:
368                 ast_channel_stream_set(f->owner, NULL);
369                 AST_SCHED_DEL_ACCESSOR(ast_channel_sched(f->owner), f->owner, ast_channel_streamid, ast_channel_streamid_set);
370                 ast_settimeout(f->owner, 0, NULL, NULL);
371                 break;
372         case AST_MEDIA_TYPE_VIDEO:
373                 ast_channel_vstream_set(f->owner, NULL);
374                 AST_SCHED_DEL_ACCESSOR(ast_channel_sched(f->owner), f->owner, ast_channel_vstreamid, ast_channel_vstreamid_set);
375                 break;
376         default:
377                 ast_log(AST_LOG_WARNING, "Unable to schedule deletion of filestream with unsupported type %s\n", f->fmt->name);
378                 break;
379         }
380 }
381
382 static void filestream_destructor(void *arg)
383 {
384         struct ast_filestream *f = arg;
385         int status;
386         int pid = -1;
387
388         /* Stop a running stream if there is one */
389         filestream_close(f);
390
391         /* destroy the translator on exit */
392         if (f->trans)
393                 ast_translator_free_path(f->trans);
394
395         if (f->fmt->close) {
396                 void (*closefn)(struct ast_filestream *) = f->fmt->close;
397                 closefn(f);
398         }
399
400         if (f->f) {
401                 fclose(f->f);
402         }
403
404         if (f->realfilename && f->filename) {
405                 pid = ast_safe_fork(0);
406                 if (!pid) {
407                         execl("/bin/mv", "mv", "-f", f->filename, f->realfilename, SENTINEL);
408                         _exit(1);
409                 }
410                 else if (pid > 0) {
411                         /* Block the parent until the move is complete.*/
412                         waitpid(pid, &status, 0);
413                 }
414         }
415
416         ast_free(f->filename);
417         ast_free(f->realfilename);
418         if (f->vfs)
419                 ast_closestream(f->vfs);
420         ast_free(f->write_buffer);
421         ast_free((void *)f->orig_chan_name);
422         ao2_cleanup(f->lastwriteformat);
423         ao2_cleanup(f->fr.subclass.format);
424         ast_module_unref(f->fmt->module);
425 }
426
427 static struct ast_filestream *get_filestream(struct ast_format_def *fmt, FILE *bfile)
428 {
429         struct ast_filestream *s;
430
431         int l = sizeof(*s) + fmt->buf_size + fmt->desc_size;    /* total allocation size */
432         if ( (s = ao2_alloc(l, filestream_destructor)) == NULL)
433                 return NULL;
434         ast_module_ref(fmt->module);
435         s->fmt = fmt;
436         s->f = bfile;
437
438         if (fmt->desc_size)
439                 s->_private = ((char *)(s + 1)) + fmt->buf_size;
440         if (fmt->buf_size)
441                 s->buf = (char *)(s + 1);
442         s->fr.src = fmt->name;
443
444         if (ast_format_get_type(fmt->format) == AST_MEDIA_TYPE_AUDIO) {
445                 s->fr.frametype = AST_FRAME_VOICE;
446         } else if (ast_format_get_type(fmt->format) == AST_MEDIA_TYPE_VIDEO) {
447                 s->fr.frametype = AST_FRAME_VIDEO;
448         }
449         s->fr.mallocd = 0;
450         s->fr.subclass.format = ao2_bump(fmt->format);
451
452         return s;
453 }
454
455 /*
456  * Default implementations of open and rewrite.
457  * Only use them if you don't have expensive stuff to do.
458  */
459 enum wrap_fn { WRAP_OPEN, WRAP_REWRITE };
460
461 static int fn_wrapper(struct ast_filestream *s, const char *comment, enum wrap_fn mode)
462 {
463         struct ast_format_def *f = s->fmt;
464         int ret = -1;
465         int (*openfn)(struct ast_filestream *s);
466
467         if (mode == WRAP_OPEN && (openfn = f->open) && openfn(s))
468                 ast_log(LOG_WARNING, "Unable to open format %s\n", f->name);
469         else if (mode == WRAP_REWRITE && f->rewrite && f->rewrite(s, comment))
470                 ast_log(LOG_WARNING, "Unable to rewrite format %s\n", f->name);
471         else {
472                 /* preliminary checks succeed. */
473                 ret = 0;
474         }
475         return ret;
476 }
477
478 static int rewrite_wrapper(struct ast_filestream *s, const char *comment)
479 {
480         return fn_wrapper(s, comment, WRAP_REWRITE);
481 }
482
483 static int open_wrapper(struct ast_filestream *s)
484 {
485         return fn_wrapper(s, NULL, WRAP_OPEN);
486 }
487
488 enum file_action {
489         ACTION_EXISTS = 1, /* return matching format if file exists, 0 otherwise */
490         ACTION_DELETE,  /* delete file, return 0 on success, -1 on error */
491         ACTION_RENAME,  /* rename file. return 0 on success, -1 on error */
492         ACTION_OPEN,
493         ACTION_COPY     /* copy file. return 0 on success, -1 on error */
494 };
495
496 /*!
497  * \internal
498  * \brief perform various actions on a file. Second argument
499  * \note arg2 depends on the command:
500  *      unused for DELETE
501  *  optional ast_format_cap holding all the formats found for a file, for EXISTS.
502  *      destination file name (const char *) for COPY and RENAME
503  *      struct ast_channel * for OPEN
504  * if fmt is NULL, OPEN will return the first matching entry,
505  * whereas other functions will run on all matching entries.
506  */
507 static int filehelper(const char *filename, const void *arg2, const char *fmt, const enum file_action action)
508 {
509         struct ast_format_def *f;
510         int res = (action == ACTION_EXISTS) ? 0 : -1;
511
512         AST_RWLIST_RDLOCK(&formats);
513         /* Check for a specific format */
514         AST_RWLIST_TRAVERSE(&formats, f, list) {
515                 char *stringp, *ext = NULL;
516
517                 if (fmt && !exts_compare(f->exts, fmt))
518                         continue;
519
520                 /* Look for a file matching the supported extensions.
521                  * The file must exist, and for OPEN, must match
522                  * one of the formats supported by the channel.
523                  */
524                 stringp = ast_strdupa(f->exts); /* this is in the stack so does not need to be freed */
525                 while ( (ext = strsep(&stringp, "|")) ) {
526                         struct stat st;
527                         char *fn = build_filename(filename, ext);
528
529                         if (fn == NULL)
530                                 continue;
531
532                         if ( stat(fn, &st) ) { /* file not existent */
533                                 ast_free(fn);
534                                 continue;
535                         }
536                         /* for 'OPEN' we need to be sure that the format matches
537                          * what the channel can process
538                          */
539                         if (action == ACTION_OPEN) {
540                                 struct ast_channel *chan = (struct ast_channel *)arg2;
541                                 FILE *bfile;
542                                 struct ast_filestream *s;
543
544                                 if ((ast_format_cmp(ast_channel_writeformat(chan), f->format) == AST_FORMAT_CMP_NOT_EQUAL) &&
545                                      !(((ast_format_get_type(f->format) == AST_MEDIA_TYPE_AUDIO) && fmt) ||
546                                           ((ast_format_get_type(f->format) == AST_MEDIA_TYPE_VIDEO) && fmt))) {
547                                         ast_free(fn);
548                                         continue;       /* not a supported format */
549                                 }
550                                 if ( (bfile = fopen(fn, "r")) == NULL) {
551                                         ast_free(fn);
552                                         continue;       /* cannot open file */
553                                 }
554                                 s = get_filestream(f, bfile);
555                                 if (!s) {
556                                         fclose(bfile);
557                                         ast_free(fn);   /* cannot allocate descriptor */
558                                         continue;
559                                 }
560                                 if (open_wrapper(s)) {
561                                         ast_free(fn);
562                                         ast_closestream(s);
563                                         continue;       /* cannot run open on file */
564                                 }
565                                 if (st.st_size == 0) {
566                                         ast_log(LOG_WARNING, "File %s detected to have zero size.\n", fn);
567                                 }
568                                 /* ok this is good for OPEN */
569                                 res = 1;        /* found */
570                                 s->lasttimeout = -1;
571                                 s->fmt = f;
572                                 s->trans = NULL;
573                                 s->filename = NULL;
574                                 if (ast_format_get_type(s->fmt->format) == AST_MEDIA_TYPE_AUDIO) {
575                                         if (ast_channel_stream(chan))
576                                                 ast_closestream(ast_channel_stream(chan));
577                                         ast_channel_stream_set(chan, s);
578                                 } else {
579                                         if (ast_channel_vstream(chan))
580                                                 ast_closestream(ast_channel_vstream(chan));
581                                         ast_channel_vstream_set(chan, s);
582                                 }
583                                 ast_free(fn);
584                                 break;
585                         }
586                         switch (action) {
587                         case ACTION_OPEN:
588                                 break;  /* will never get here */
589
590                         case ACTION_EXISTS:     /* return the matching format */
591                                 /* if arg2 is present, it is a format capabilities structure.
592                                  * Add this format to the set of formats this file can be played in */
593                                 if (arg2) {
594                                         ast_format_cap_append((struct ast_format_cap *) arg2, f->format, 0);
595                                 }
596                                 res = 1; /* file does exist and format it exists in is returned in arg2 */
597                                 break;
598
599                         case ACTION_DELETE:
600                                 if ( (res = unlink(fn)) )
601                                         ast_log(LOG_WARNING, "unlink(%s) failed: %s\n", fn, strerror(errno));
602                                 break;
603
604                         case ACTION_RENAME:
605                         case ACTION_COPY: {
606                                 char *nfn = build_filename((const char *)arg2, ext);
607                                 if (!nfn)
608                                         ast_log(LOG_WARNING, "Out of memory\n");
609                                 else {
610                                         res = action == ACTION_COPY ? copy(fn, nfn) : rename(fn, nfn);
611                                         if (res)
612                                                 ast_log(LOG_WARNING, "%s(%s,%s) failed: %s\n",
613                                                         action == ACTION_COPY ? "copy" : "rename",
614                                                          fn, nfn, strerror(errno));
615                                         ast_free(nfn);
616                                 }
617                             }
618                                 break;
619
620                         default:
621                                 ast_log(LOG_WARNING, "Unknown helper %u\n", action);
622                         }
623                         ast_free(fn);
624                 }
625         }
626         AST_RWLIST_UNLOCK(&formats);
627         return res;
628 }
629
630 static int is_absolute_path(const char *filename)
631 {
632         return filename[0] == '/';
633 }
634
635 /*!
636  * \brief test if a file exists for a given format.
637  * \note result_cap is OPTIONAL
638  * \retval 1, true and result_cap represents format capabilities file exists in.
639  * \retval 0, false
640  */
641 static int fileexists_test(const char *filename, const char *fmt, const char *lang,
642                            char *buf, int buflen, struct ast_format_cap *result_cap)
643 {
644         if (buf == NULL) {
645                 return 0;
646         }
647
648         if (!ast_media_cache_retrieve(filename, NULL, buf, buflen)) {
649                 return filehelper(buf, result_cap, NULL, ACTION_EXISTS);
650         }
651
652         if (ast_language_is_prefix && !is_absolute_path(filename)) { /* new layout */
653                 if (lang) {
654                         snprintf(buf, buflen, "%s/%s", lang, filename);
655                 } else {
656                         snprintf(buf, buflen, "%s", filename);
657                 }
658         } else { /* old layout */
659                 strcpy(buf, filename);  /* first copy the full string */
660                 if (lang) {
661                         /* insert the language and suffix if needed */
662                         const char *c = strrchr(filename, '/');
663                         int offset = c ? c - filename + 1 : 0;  /* points right after the last '/' */
664                         snprintf(buf + offset, buflen - offset, "%s/%s", lang, filename + offset);
665                 }
666         }
667
668         return filehelper(buf, result_cap, fmt, ACTION_EXISTS);
669 }
670
671 /*!
672  * \brief helper routine to locate a file with a given format
673  * and language preference.
674  *
675  * \note Try preflang, preflang with stripped '_' suffices, or NULL.
676  *
677  * \note The last parameter(s) point to a buffer of sufficient size,
678  * which on success is filled with the matching filename.
679  *
680  * \param filename Name of the file.
681  * \param fmt Format to look for the file in. OPTIONAL
682  * \param preflang The perfered language
683  * \param buf Returns the matching filename
684  * \param buflen Size of the buf
685  * \param result_cap OPTIONAL format capabilities result structure
686  *        returns what formats the file was found in.
687  *
688  * \retval 1, true. file exists and result format is set
689  * \retval 0, false. file does not exist.
690  */
691 static int fileexists_core(const char *filename, const char *fmt, const char *preflang,
692                            char *buf, int buflen, struct ast_format_cap *result_cap)
693 {
694         char *lang;
695
696         if (buf == NULL) {
697                 return 0;
698         }
699
700         /* We try languages in the following order:
701          *    preflang (may include dialect and style codes)
702          *    lang (preflang without dialect - if any)
703          *    <none>
704          *    default (unless the same as preflang or lang without dialect)
705          */
706
707         lang = ast_strdupa(preflang);
708
709         /* Try preferred language, including removing any style or dialect codes */
710         while (!ast_strlen_zero(lang)) {
711                 char *end;
712
713                 if (fileexists_test(filename, fmt, lang, buf, buflen, result_cap)) {
714                         return 1;
715                 }
716
717                 if ((end = strrchr(lang, '_')) != NULL) {
718                         *end = '\0';
719                         continue;
720                 }
721
722                 break;
723         }
724
725         /* Try without any language */
726         if (fileexists_test(filename, fmt, NULL, buf, buflen, result_cap)) {
727                 return 1;
728         }
729
730         /* Finally try the default language unless it was already tried before */
731         if ((ast_strlen_zero(preflang) || strcmp(preflang, DEFAULT_LANGUAGE)) && (ast_strlen_zero(lang) || strcmp(lang, DEFAULT_LANGUAGE))) {
732                 if ((fileexists_test(filename, fmt, DEFAULT_LANGUAGE, buf, buflen, result_cap)) > 0) {
733                         return 1;
734                 }
735         }
736
737         return 0;
738 }
739
740 struct ast_filestream *ast_openstream(struct ast_channel *chan, const char *filename, const char *preflang)
741 {
742         return ast_openstream_full(chan, filename, preflang, 0);
743 }
744
745 struct ast_filestream *ast_openstream_full(struct ast_channel *chan, const char *filename, const char *preflang, int asis)
746 {
747         /*
748          * Use fileexists_core() to find a file in a compatible
749          * language and format, set up a suitable translator,
750          * and open the stream.
751          */
752         struct ast_format_cap *file_fmt_cap;
753         int res;
754         int buflen;
755         char *buf;
756
757         if (!asis) {
758                 /* do this first, otherwise we detect the wrong writeformat */
759                 ast_stopstream(chan);
760                 if (ast_channel_generator(chan))
761                         ast_deactivate_generator(chan);
762         }
763         if (preflang == NULL)
764                 preflang = "";
765         buflen = strlen(preflang) + strlen(filename) + 4;
766         buf = ast_alloca(buflen);
767
768         if (!(file_fmt_cap = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT))) {
769                 return NULL;
770         }
771         if (!fileexists_core(filename, NULL, preflang, buf, buflen, file_fmt_cap) ||
772                 !ast_format_cap_has_type(file_fmt_cap, AST_MEDIA_TYPE_AUDIO)) {
773
774                 ast_log(LOG_WARNING, "File %s does not exist in any format\n", filename);
775                 ao2_ref(file_fmt_cap, -1);
776                 return NULL;
777         }
778
779         /* Set the channel to a format we can work with and save off the previous format. */
780         ast_channel_lock(chan);
781         ast_channel_set_oldwriteformat(chan, ast_channel_writeformat(chan));
782         /* Set the channel to the best format that exists for the file. */
783         res = ast_set_write_format_from_cap(chan, file_fmt_cap);
784         ast_channel_unlock(chan);
785         /* don't need this anymore now that the channel's write format is set. */
786         ao2_ref(file_fmt_cap, -1);
787
788         if (res == -1) {        /* No format available that works with this channel */
789                 return NULL;
790         }
791         res = filehelper(buf, chan, NULL, ACTION_OPEN);
792         if (res >= 0)
793                 return ast_channel_stream(chan);
794         return NULL;
795 }
796
797 struct ast_filestream *ast_openvstream(struct ast_channel *chan, const char *filename, const char *preflang)
798 {
799         /* As above, but for video. But here we don't have translators
800          * so we must enforce a format.
801          */
802         struct ast_format_cap *nativeformats, *tmp_cap;
803         char *buf;
804         int buflen;
805         int i, fd;
806
807         if (preflang == NULL) {
808                 preflang = "";
809         }
810         buflen = strlen(preflang) + strlen(filename) + 4;
811         buf = ast_alloca(buflen);
812
813         ast_channel_lock(chan);
814         nativeformats = ao2_bump(ast_channel_nativeformats(chan));
815         ast_channel_unlock(chan);
816
817         /* is the channel capable of video without translation ?*/
818         if (!ast_format_cap_has_type(nativeformats, AST_MEDIA_TYPE_VIDEO)) {
819                 ao2_cleanup(nativeformats);
820                 return NULL;
821         }
822         if (!(tmp_cap = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT))) {
823                 ao2_cleanup(nativeformats);
824                 return NULL;
825         }
826         /* Video is supported, so see what video formats exist for this file */
827         if (!fileexists_core(filename, NULL, preflang, buf, buflen, tmp_cap)) {
828                 ao2_ref(tmp_cap, -1);
829                 ao2_cleanup(nativeformats);
830                 return NULL;
831         }
832
833         /* iterate over file formats and pick the first one compatible with the channel's native formats */
834         for (i = 0; i < ast_format_cap_count(tmp_cap); ++i) {
835                 struct ast_format *format = ast_format_cap_get_format(tmp_cap, i);
836
837                 if ((ast_format_get_type(format) != AST_MEDIA_TYPE_VIDEO) ||
838                         !ast_format_cap_iscompatible(nativeformats, tmp_cap)) {
839                         ao2_ref(format, -1);
840                         continue;
841                 }
842
843                 fd = filehelper(buf, chan, ast_format_get_name(format), ACTION_OPEN);
844                 if (fd >= 0) {
845                         ao2_ref(format, -1);
846                         ao2_ref(tmp_cap, -1);
847                         ao2_cleanup(nativeformats);
848                         return ast_channel_vstream(chan);
849                 }
850                 ast_log(LOG_WARNING, "File %s has video but couldn't be opened\n", filename);
851                 ao2_ref(format, -1);
852         }
853         ao2_ref(tmp_cap, -1);
854         ao2_cleanup(nativeformats);
855
856         return NULL;
857 }
858
859 static struct ast_frame *read_frame(struct ast_filestream *s, int *whennext)
860 {
861         struct ast_frame *fr, *new_fr;
862
863         if (!s || !s->fmt) {
864                 return NULL;
865         }
866
867         if (!(fr = s->fmt->read(s, whennext))) {
868                 return NULL;
869         }
870
871         if (!(new_fr = ast_frisolate(fr))) {
872                 ast_frfree(fr);
873                 return NULL;
874         }
875
876         if (new_fr != fr) {
877                 ast_frfree(fr);
878                 fr = new_fr;
879         }
880
881         return fr;
882 }
883
884 struct ast_frame *ast_readframe(struct ast_filestream *s)
885 {
886         int whennext = 0;
887
888         return read_frame(s, &whennext);
889 }
890
891 enum fsread_res {
892         FSREAD_FAILURE,
893         FSREAD_SUCCESS_SCHED,
894         FSREAD_SUCCESS_NOSCHED,
895 };
896
897 static int ast_fsread_audio(const void *data);
898
899 static enum fsread_res ast_readaudio_callback(struct ast_filestream *s)
900 {
901         int whennext = 0;
902
903         while (!whennext) {
904                 struct ast_frame *fr;
905
906                 if (s->orig_chan_name && strcasecmp(ast_channel_name(s->owner), s->orig_chan_name)) {
907                         goto return_failure;
908                 }
909
910                 fr = read_frame(s, &whennext);
911
912                 if (!fr /* stream complete */ || ast_write(s->owner, fr) /* error writing */) {
913                         if (fr) {
914                                 ast_debug(2, "Failed to write frame\n");
915                                 ast_frfree(fr);
916                         }
917                         goto return_failure;
918                 }
919
920                 if (fr) {
921                         ast_frfree(fr);
922                 }
923         }
924
925         if (whennext != s->lasttimeout) {
926                 if (ast_channel_timingfd(s->owner) > -1) {
927                         float samp_rate = (float) ast_format_get_sample_rate(s->fmt->format);
928                         unsigned int rate;
929
930                         rate = (unsigned int) roundf(samp_rate / ((float) whennext));
931
932                         ast_settimeout_full(s->owner, rate, ast_fsread_audio, s, 1);
933                 } else {
934                         ast_channel_streamid_set(s->owner, ast_sched_add(ast_channel_sched(s->owner), whennext / (ast_format_get_sample_rate(s->fmt->format) / 1000), ast_fsread_audio, s));
935                 }
936                 s->lasttimeout = whennext;
937                 return FSREAD_SUCCESS_NOSCHED;
938         }
939         return FSREAD_SUCCESS_SCHED;
940
941 return_failure:
942         ast_channel_streamid_set(s->owner, -1);
943         ast_settimeout(s->owner, 0, NULL, NULL);
944         return FSREAD_FAILURE;
945 }
946
947 static int ast_fsread_audio(const void *data)
948 {
949         struct ast_filestream *fs = (struct ast_filestream *)data;
950         enum fsread_res res;
951
952         res = ast_readaudio_callback(fs);
953
954         if (res == FSREAD_SUCCESS_SCHED)
955                 return 1;
956
957         return 0;
958 }
959
960 static int ast_fsread_video(const void *data);
961
962 static enum fsread_res ast_readvideo_callback(struct ast_filestream *s)
963 {
964         int whennext = 0;
965
966         while (!whennext) {
967                 struct ast_frame *fr = read_frame(s, &whennext);
968
969                 if (!fr /* stream complete */ || ast_write(s->owner, fr) /* error writing */) {
970                         if (fr) {
971                                 ast_debug(2, "Failed to write frame\n");
972                                 ast_frfree(fr);
973                         }
974                         ast_channel_vstreamid_set(s->owner, -1);
975                         return FSREAD_FAILURE;
976                 }
977
978                 if (fr) {
979                         ast_frfree(fr);
980                 }
981         }
982
983         if (whennext != s->lasttimeout) {
984                 ast_channel_vstreamid_set(s->owner, ast_sched_add(ast_channel_sched(s->owner), whennext / (ast_format_get_sample_rate(s->fmt->format) / 1000), ast_fsread_video, s));
985                 s->lasttimeout = whennext;
986                 return FSREAD_SUCCESS_NOSCHED;
987         }
988
989         return FSREAD_SUCCESS_SCHED;
990 }
991
992 static int ast_fsread_video(const void *data)
993 {
994         struct ast_filestream *fs = (struct ast_filestream *)data;
995         enum fsread_res res;
996
997         res = ast_readvideo_callback(fs);
998
999         if (res == FSREAD_SUCCESS_SCHED)
1000                 return 1;
1001
1002         return 0;
1003 }
1004
1005 int ast_applystream(struct ast_channel *chan, struct ast_filestream *s)
1006 {
1007         s->owner = chan;
1008         return 0;
1009 }
1010
1011 int ast_playstream(struct ast_filestream *s)
1012 {
1013         enum fsread_res res;
1014
1015         if (ast_format_get_type(s->fmt->format) == AST_MEDIA_TYPE_AUDIO)
1016                 res = ast_readaudio_callback(s);
1017         else
1018                 res = ast_readvideo_callback(s);
1019
1020         return (res == FSREAD_FAILURE) ? -1 : 0;
1021 }
1022
1023 int ast_seekstream(struct ast_filestream *fs, off_t sample_offset, int whence)
1024 {
1025         return fs->fmt->seek(fs, sample_offset, whence);
1026 }
1027
1028 int ast_truncstream(struct ast_filestream *fs)
1029 {
1030         return fs->fmt->trunc(fs);
1031 }
1032
1033 off_t ast_tellstream(struct ast_filestream *fs)
1034 {
1035         return fs->fmt->tell(fs);
1036 }
1037
1038 int ast_ratestream(struct ast_filestream *fs)
1039 {
1040         return ast_format_get_sample_rate(fs->fmt->format);
1041 }
1042
1043 int ast_stream_fastforward(struct ast_filestream *fs, off_t ms)
1044 {
1045         return ast_seekstream(fs, ms * DEFAULT_SAMPLES_PER_MS, SEEK_CUR);
1046 }
1047
1048 int ast_stream_rewind(struct ast_filestream *fs, off_t ms)
1049 {
1050         return ast_seekstream(fs, -ms * DEFAULT_SAMPLES_PER_MS, SEEK_CUR);
1051 }
1052
1053 int ast_closestream(struct ast_filestream *f)
1054 {
1055         /* This used to destroy the filestream, but it now just decrements a refcount.
1056          * We close the stream in order to quit queuing frames now, because we might
1057          * change the writeformat, which could result in a subsequent write error, if
1058          * the format is different. */
1059         if (f == NULL) {
1060                 return 0;
1061         }
1062         filestream_close(f);
1063         ao2_ref(f, -1);
1064         return 0;
1065 }
1066
1067
1068 /*
1069  * Look the various language-specific places where a file could exist.
1070  */
1071 int ast_fileexists(const char *filename, const char *fmt, const char *preflang)
1072 {
1073         char *buf;
1074         int buflen;
1075
1076         if (preflang == NULL)
1077                 preflang = "";
1078         buflen = strlen(preflang) + strlen(filename) + 4;       /* room for everything */
1079         buf = ast_alloca(buflen);
1080         return fileexists_core(filename, fmt, preflang, buf, buflen, NULL) ? 1 : 0;
1081 }
1082
1083 int ast_filedelete(const char *filename, const char *fmt)
1084 {
1085         return filehelper(filename, NULL, fmt, ACTION_DELETE);
1086 }
1087
1088 int ast_filerename(const char *filename, const char *filename2, const char *fmt)
1089 {
1090         return filehelper(filename, filename2, fmt, ACTION_RENAME);
1091 }
1092
1093 int ast_filecopy(const char *filename, const char *filename2, const char *fmt)
1094 {
1095         return filehelper(filename, filename2, fmt, ACTION_COPY);
1096 }
1097
1098 int ast_streamfile(struct ast_channel *chan, const char *filename, const char *preflang)
1099 {
1100         struct ast_filestream *fs;
1101         struct ast_filestream *vfs=NULL;
1102         off_t pos;
1103         int seekattempt;
1104         int res;
1105
1106         fs = ast_openstream(chan, filename, preflang);
1107         if (!fs) {
1108                 struct ast_str *codec_buf = ast_str_alloca(AST_FORMAT_CAP_NAMES_LEN);
1109                 ast_channel_lock(chan);
1110                 ast_log(LOG_WARNING, "Unable to open %s (format %s): %s\n",
1111                         filename, ast_format_cap_get_names(ast_channel_nativeformats(chan), &codec_buf), strerror(errno));
1112                 ast_channel_unlock(chan);
1113                 return -1;
1114         }
1115
1116         /* check to see if there is any data present (not a zero length file),
1117          * done this way because there is no where for ast_openstream_full to
1118          * return the file had no data. */
1119         pos = ftello(fs->f);
1120         seekattempt = fseeko(fs->f, -1, SEEK_END);
1121         if (seekattempt) {
1122                 if (errno == EINVAL) {
1123                         /* Zero-length file, as opposed to a pipe */
1124                         return 0;
1125                 } else {
1126                         ast_seekstream(fs, 0, SEEK_SET);
1127                 }
1128         } else {
1129                 fseeko(fs->f, pos, SEEK_SET);
1130         }
1131
1132         vfs = ast_openvstream(chan, filename, preflang);
1133         if (vfs) {
1134                 ast_debug(1, "Ooh, found a video stream, too, format %s\n", ast_format_get_name(vfs->fmt->format));
1135         }
1136
1137         if (ast_test_flag(ast_channel_flags(chan), AST_FLAG_MASQ_NOSTREAM))
1138                 fs->orig_chan_name = ast_strdup(ast_channel_name(chan));
1139         if (ast_applystream(chan, fs))
1140                 return -1;
1141         if (vfs && ast_applystream(chan, vfs))
1142                 return -1;
1143         ast_test_suite_event_notify("PLAYBACK", "Message: %s\r\nChannel: %s", filename, ast_channel_name(chan));
1144         res = ast_playstream(fs);
1145         if (!res && vfs)
1146                 res = ast_playstream(vfs);
1147
1148         if (VERBOSITY_ATLEAST(3)) {
1149                 ast_channel_lock(chan);
1150                 ast_verb(3, "<%s> Playing '%s.%s' (language '%s')\n", ast_channel_name(chan), filename, ast_format_get_name(ast_channel_writeformat(chan)), preflang ? preflang : "default");
1151                 ast_channel_unlock(chan);
1152         }
1153
1154         return res;
1155 }
1156
1157 struct ast_filestream *ast_readfile(const char *filename, const char *type, const char *comment, int flags, int check, mode_t mode)
1158 {
1159         FILE *bfile;
1160         struct ast_format_def *f;
1161         struct ast_filestream *fs = NULL;
1162         char *fn;
1163         int format_found = 0;
1164
1165         AST_RWLIST_RDLOCK(&formats);
1166
1167         AST_RWLIST_TRAVERSE(&formats, f, list) {
1168                 fs = NULL;
1169                 if (!exts_compare(f->exts, type))
1170                         continue;
1171                 else
1172                         format_found = 1;
1173
1174                 fn = build_filename(filename, type);
1175                 if (!fn) {
1176                         continue;
1177                 }
1178                 errno = 0;
1179                 bfile = fopen(fn, "r");
1180
1181                 if (!bfile || (fs = get_filestream(f, bfile)) == NULL || open_wrapper(fs) ) {
1182                         ast_log(LOG_WARNING, "Unable to open %s\n", fn);
1183                         if (fs) {
1184                                 ast_closestream(fs);
1185                         }
1186                         fs = NULL;
1187                         bfile = NULL;
1188                         ast_free(fn);
1189                         break;
1190                 }
1191                 /* found it */
1192                 fs->trans = NULL;
1193                 fs->fmt = f;
1194                 fs->flags = flags;
1195                 fs->mode = mode;
1196                 fs->filename = ast_strdup(filename);
1197                 fs->vfs = NULL;
1198                 ast_free(fn);
1199                 break;
1200         }
1201
1202         AST_RWLIST_UNLOCK(&formats);
1203         if (!format_found)
1204                 ast_log(LOG_WARNING, "No such format '%s'\n", type);
1205
1206         return fs;
1207 }
1208
1209 struct ast_filestream *ast_writefile(const char *filename, const char *type, const char *comment, int flags, int check, mode_t mode)
1210 {
1211         int fd, myflags = 0;
1212         /* compiler claims this variable can be used before initialization... */
1213         FILE *bfile = NULL;
1214         struct ast_format_def *f;
1215         struct ast_filestream *fs = NULL;
1216         char *buf = NULL;
1217         size_t size = 0;
1218         int format_found = 0;
1219
1220         AST_RWLIST_RDLOCK(&formats);
1221
1222         /* set the O_TRUNC flag if and only if there is no O_APPEND specified */
1223         /* We really can't use O_APPEND as it will break WAV header updates */
1224         if (flags & O_APPEND) {
1225                 flags &= ~O_APPEND;
1226         } else {
1227                 myflags = O_TRUNC;
1228         }
1229
1230         myflags |= O_WRONLY | O_CREAT;
1231
1232         /* XXX need to fix this - we should just do the fopen,
1233          * not open followed by fdopen()
1234          */
1235         AST_RWLIST_TRAVERSE(&formats, f, list) {
1236                 char *fn, *orig_fn = NULL;
1237                 if (fs)
1238                         break;
1239
1240                 if (!exts_compare(f->exts, type))
1241                         continue;
1242                 else
1243                         format_found = 1;
1244
1245                 fn = build_filename(filename, type);
1246                 if (!fn) {
1247                         continue;
1248                 }
1249                 fd = open(fn, flags | myflags, mode);
1250                 if (fd > -1) {
1251                         /* fdopen() the resulting file stream */
1252                         bfile = fdopen(fd, ((flags | myflags) & O_RDWR) ? "w+" : "w");
1253                         if (!bfile) {
1254                                 ast_log(LOG_WARNING, "Whoa, fdopen failed: %s!\n", strerror(errno));
1255                                 close(fd);
1256                                 fd = -1;
1257                         }
1258                 }
1259
1260                 if (ast_opt_cache_record_files && (fd > -1)) {
1261                         char *c;
1262
1263                         fclose(bfile);  /* this also closes fd */
1264                         /*
1265                           We touch orig_fn just as a place-holder so other things (like vmail) see the file is there.
1266                           What we are really doing is writing to record_cache_dir until we are done then we will mv the file into place.
1267                         */
1268                         orig_fn = ast_strdupa(fn);
1269                         for (c = fn; *c; c++)
1270                                 if (*c == '/')
1271                                         *c = '_';
1272
1273                         size = strlen(fn) + strlen(record_cache_dir) + 2;
1274                         buf = ast_alloca(size);
1275                         strcpy(buf, record_cache_dir);
1276                         strcat(buf, "/");
1277                         strcat(buf, fn);
1278                         ast_free(fn);
1279                         fn = buf;
1280                         fd = open(fn, flags | myflags, mode);
1281                         if (fd > -1) {
1282                                 /* fdopen() the resulting file stream */
1283                                 bfile = fdopen(fd, ((flags | myflags) & O_RDWR) ? "w+" : "w");
1284                                 if (!bfile) {
1285                                         ast_log(LOG_WARNING, "Whoa, fdopen failed: %s!\n", strerror(errno));
1286                                         close(fd);
1287                                         fd = -1;
1288                                 }
1289                         }
1290                 }
1291                 if (fd > -1) {
1292                         errno = 0;
1293                         fs = get_filestream(f, bfile);
1294                         if (fs) {
1295                                 if ((fs->write_buffer = ast_malloc(32768))) {
1296                                         setvbuf(fs->f, fs->write_buffer, _IOFBF, 32768);
1297                                 }
1298                         }
1299                         if (!fs || rewrite_wrapper(fs, comment)) {
1300                                 ast_log(LOG_WARNING, "Unable to rewrite %s\n", fn);
1301                                 close(fd);
1302                                 if (orig_fn) {
1303                                         unlink(fn);
1304                                         unlink(orig_fn);
1305                                 }
1306                                 if (fs) {
1307                                         ast_closestream(fs);
1308                                         fs = NULL;
1309                                 }
1310                                 if (!buf) {
1311                                         ast_free(fn);
1312                                 }
1313                                 continue;
1314                         }
1315                         fs->trans = NULL;
1316                         fs->fmt = f;
1317                         fs->flags = flags;
1318                         fs->mode = mode;
1319                         if (orig_fn) {
1320                                 fs->realfilename = ast_strdup(orig_fn);
1321                                 fs->filename = ast_strdup(fn);
1322                         } else {
1323                                 fs->realfilename = NULL;
1324                                 fs->filename = ast_strdup(filename);
1325                         }
1326                         fs->vfs = NULL;
1327                         /* If truncated, we'll be at the beginning; if not truncated, then append */
1328                         f->seek(fs, 0, SEEK_END);
1329                 } else if (errno != EEXIST) {
1330                         ast_log(LOG_WARNING, "Unable to open file %s: %s\n", fn, strerror(errno));
1331                         if (orig_fn)
1332                                 unlink(orig_fn);
1333                 }
1334                 /* if buf != NULL then fn is already free and pointing to it */
1335                 if (!buf)
1336                         ast_free(fn);
1337         }
1338
1339         AST_RWLIST_UNLOCK(&formats);
1340
1341         if (!format_found)
1342                 ast_log(LOG_WARNING, "No such format '%s'\n", type);
1343
1344         return fs;
1345 }
1346
1347 static void waitstream_control(struct ast_channel *c,
1348                 enum ast_waitstream_fr_cb_values type,
1349                 ast_waitstream_fr_cb cb,
1350                 int skip_ms)
1351 {
1352         switch (type)
1353         {
1354         case AST_WAITSTREAM_CB_FASTFORWARD:
1355                 {
1356                         int eoftest;
1357                         ast_stream_fastforward(ast_channel_stream(c), skip_ms);
1358                         eoftest = fgetc(ast_channel_stream(c)->f);
1359                         if (feof(ast_channel_stream(c)->f)) {
1360                                 ast_stream_rewind(ast_channel_stream(c), skip_ms);
1361                         } else {
1362                                 ungetc(eoftest, ast_channel_stream(c)->f);
1363                         }
1364                 }
1365                 break;
1366         case AST_WAITSTREAM_CB_REWIND:
1367                 ast_stream_rewind(ast_channel_stream(c), skip_ms);
1368                 break;
1369         default:
1370                 break;
1371         }
1372
1373         if (cb) {
1374                 long ms_len = ast_tellstream(ast_channel_stream(c)) / (ast_format_get_sample_rate(ast_channel_stream(c)->fmt->format) / 1000);
1375                 cb(c, ms_len, type);
1376         }
1377
1378         ast_test_suite_event_notify("PLAYBACK","Channel: %s\r\n"
1379                 "Control: %s\r\n"
1380                 "SkipMs: %d\r\n",
1381                 ast_channel_name(c),
1382                 (type == AST_WAITSTREAM_CB_FASTFORWARD) ? "FastForward" : "Rewind",
1383                 skip_ms);
1384 }
1385
1386 /*!
1387  * \brief the core of all waitstream() functions
1388  */
1389 static int waitstream_core(struct ast_channel *c,
1390         const char *breakon,
1391         const char *forward,
1392         const char *reverse,
1393         int skip_ms,
1394         int audiofd,
1395         int cmdfd,
1396         const char *context,
1397         ast_waitstream_fr_cb cb)
1398 {
1399         const char *orig_chan_name = NULL;
1400
1401         int err = 0;
1402
1403         if (!breakon)
1404                 breakon = "";
1405         if (!forward)
1406                 forward = "";
1407         if (!reverse)
1408                 reverse = "";
1409
1410         /* Switch the channel to end DTMF frame only. waitstream_core doesn't care about the start of DTMF. */
1411         ast_set_flag(ast_channel_flags(c), AST_FLAG_END_DTMF_ONLY);
1412
1413         if (ast_test_flag(ast_channel_flags(c), AST_FLAG_MASQ_NOSTREAM))
1414                 orig_chan_name = ast_strdupa(ast_channel_name(c));
1415
1416         if (ast_channel_stream(c) && cb) {
1417                 long ms_len = ast_tellstream(ast_channel_stream(c)) / (ast_format_get_sample_rate(ast_channel_stream(c)->fmt->format) / 1000);
1418                 cb(c, ms_len, AST_WAITSTREAM_CB_START);
1419         }
1420
1421         while (ast_channel_stream(c)) {
1422                 int res;
1423                 int ms;
1424
1425                 if (orig_chan_name && strcasecmp(orig_chan_name, ast_channel_name(c))) {
1426                         ast_stopstream(c);
1427                         err = 1;
1428                         break;
1429                 }
1430
1431                 ms = ast_sched_wait(ast_channel_sched(c));
1432
1433                 if (ms < 0 && !ast_channel_timingfunc(c)) {
1434                         ast_stopstream(c);
1435                         break;
1436                 }
1437                 if (ms < 0)
1438                         ms = 1000;
1439                 if (cmdfd < 0) {
1440                         res = ast_waitfor(c, ms);
1441                         if (res < 0) {
1442                                 ast_log(LOG_WARNING, "Select failed (%s)\n", strerror(errno));
1443                                 ast_clear_flag(ast_channel_flags(c), AST_FLAG_END_DTMF_ONLY);
1444                                 return res;
1445                         }
1446                 } else {
1447                         int outfd;
1448                         struct ast_channel *rchan = ast_waitfor_nandfds(&c, 1, &cmdfd, (cmdfd > -1) ? 1 : 0, NULL, &outfd, &ms);
1449                         if (!rchan && (outfd < 0) && (ms)) {
1450                                 /* Continue */
1451                                 if (errno == EINTR)
1452                                         continue;
1453                                 ast_log(LOG_WARNING, "Wait failed (%s)\n", strerror(errno));
1454                                 ast_clear_flag(ast_channel_flags(c), AST_FLAG_END_DTMF_ONLY);
1455                                 return -1;
1456                         } else if (outfd > -1) { /* this requires cmdfd set */
1457                                 /* The FD we were watching has something waiting */
1458                                 ast_clear_flag(ast_channel_flags(c), AST_FLAG_END_DTMF_ONLY);
1459                                 return 1;
1460                         }
1461                         /* if rchan is set, it is 'c' */
1462                         res = rchan ? 1 : 0; /* map into 'res' values */
1463                 }
1464                 if (res > 0) {
1465                         struct ast_frame *fr = ast_read(c);
1466                         if (!fr) {
1467                                 ast_clear_flag(ast_channel_flags(c), AST_FLAG_END_DTMF_ONLY);
1468                                 return -1;
1469                         }
1470                         switch (fr->frametype) {
1471                         case AST_FRAME_DTMF_END:
1472                                 if (context) {
1473                                         const char exten[2] = { fr->subclass.integer, '\0' };
1474                                         if (ast_exists_extension(c, context, exten, 1,
1475                                                 S_COR(ast_channel_caller(c)->id.number.valid, ast_channel_caller(c)->id.number.str, NULL))) {
1476                                                 res = fr->subclass.integer;
1477                                                 ast_frfree(fr);
1478                                                 ast_clear_flag(ast_channel_flags(c), AST_FLAG_END_DTMF_ONLY);
1479                                                 return res;
1480                                         }
1481                                 } else {
1482                                         res = fr->subclass.integer;
1483                                         if (strchr(forward, res)) {
1484                                                 waitstream_control(c, AST_WAITSTREAM_CB_FASTFORWARD, cb, skip_ms);
1485                                         } else if (strchr(reverse, res)) {
1486                                                 waitstream_control(c, AST_WAITSTREAM_CB_REWIND, cb, skip_ms);
1487                                         } else if (strchr(breakon, res)) {
1488                                                 ast_test_suite_event_notify("PLAYBACK","Channel: %s\r\n"
1489                                                         "Control: %s\r\n",
1490                                                         ast_channel_name(c),
1491                                                         "Break");
1492
1493                                                 ast_frfree(fr);
1494                                                 ast_clear_flag(ast_channel_flags(c), AST_FLAG_END_DTMF_ONLY);
1495                                                 return res;
1496                                         }
1497                                 }
1498                                 break;
1499                         case AST_FRAME_CONTROL:
1500                                 switch (fr->subclass.integer) {
1501                                 case AST_CONTROL_STREAM_STOP:
1502                                 case AST_CONTROL_STREAM_SUSPEND:
1503                                 case AST_CONTROL_STREAM_RESTART:
1504                                         /* Fall-through and break out */
1505                                         ast_test_suite_event_notify("PLAYBACK","Channel: %s\r\n"
1506                                                 "Control: %s\r\n",
1507                                                 ast_channel_name(c),
1508                                                 "Break");
1509                                         res = fr->subclass.integer;
1510                                         ast_frfree(fr);
1511                                         ast_clear_flag(ast_channel_flags(c), AST_FLAG_END_DTMF_ONLY);
1512                                         return res;
1513                                 case AST_CONTROL_STREAM_REVERSE:
1514                                         if (!skip_ms) {
1515                                                 skip_ms = 3000;
1516                                         }
1517                                         waitstream_control(c, AST_WAITSTREAM_CB_REWIND, cb, skip_ms);
1518                                         break;
1519                                 case AST_CONTROL_STREAM_FORWARD:
1520                                         if (!skip_ms) {
1521                                                 skip_ms = 3000;
1522                                         }
1523                                         waitstream_control(c, AST_WAITSTREAM_CB_FASTFORWARD, cb, skip_ms);
1524                                         break;
1525                                 case AST_CONTROL_HANGUP:
1526                                 case AST_CONTROL_BUSY:
1527                                 case AST_CONTROL_CONGESTION:
1528                                         ast_frfree(fr);
1529                                         ast_clear_flag(ast_channel_flags(c), AST_FLAG_END_DTMF_ONLY);
1530                                         return -1;
1531                                 case AST_CONTROL_RINGING:
1532                                 case AST_CONTROL_ANSWER:
1533                                 case AST_CONTROL_VIDUPDATE:
1534                                 case AST_CONTROL_SRCUPDATE:
1535                                 case AST_CONTROL_SRCCHANGE:
1536                                 case AST_CONTROL_HOLD:
1537                                 case AST_CONTROL_UNHOLD:
1538                                 case AST_CONTROL_CONNECTED_LINE:
1539                                 case AST_CONTROL_REDIRECTING:
1540                                 case AST_CONTROL_AOC:
1541                                 case AST_CONTROL_UPDATE_RTP_PEER:
1542                                 case AST_CONTROL_PVT_CAUSE_CODE:
1543                                 case -1:
1544                                         /* Unimportant */
1545                                         break;
1546                                 default:
1547                                         ast_log(LOG_WARNING, "Unexpected control subclass '%d'\n", fr->subclass.integer);
1548                                 }
1549                                 break;
1550                         case AST_FRAME_VOICE:
1551                                 /* Write audio if appropriate */
1552                                 if (audiofd > -1) {
1553                                         if (write(audiofd, fr->data.ptr, fr->datalen) < 0) {
1554                                                 ast_log(LOG_WARNING, "write() failed: %s\n", strerror(errno));
1555                                         }
1556                                 }
1557                         default:
1558                                 /* Ignore all others */
1559                                 break;
1560                         }
1561                         ast_frfree(fr);
1562                 }
1563                 ast_sched_runq(ast_channel_sched(c));
1564         }
1565
1566         ast_clear_flag(ast_channel_flags(c), AST_FLAG_END_DTMF_ONLY);
1567
1568         return (err || ast_channel_softhangup_internal_flag(c)) ? -1 : 0;
1569 }
1570
1571 int ast_waitstream_fr_w_cb(struct ast_channel *c,
1572         const char *breakon,
1573         const char *forward,
1574         const char *reverse,
1575         int ms,
1576         ast_waitstream_fr_cb cb)
1577 {
1578         return waitstream_core(c, breakon, forward, reverse, ms,
1579                 -1 /* no audiofd */, -1 /* no cmdfd */, NULL /* no context */, cb);
1580 }
1581
1582 int ast_waitstream_fr(struct ast_channel *c, const char *breakon, const char *forward, const char *reverse, int ms)
1583 {
1584         return waitstream_core(c, breakon, forward, reverse, ms,
1585                 -1 /* no audiofd */, -1 /* no cmdfd */, NULL /* no context */, NULL /* no callback */);
1586 }
1587
1588 /*! \internal
1589  * \brief Clean up the return value of a waitstream call
1590  *
1591  * It's possible for a control frame to come in from an external source and break the
1592  * playback. From a consumer of most ast_waitstream_* function callers, this should
1593  * appear like normal playback termination, i.e., return 0 and not the value of the
1594  * control frame.
1595  */
1596 static int sanitize_waitstream_return(int return_value)
1597 {
1598         switch (return_value) {
1599         case AST_CONTROL_STREAM_STOP:
1600         case AST_CONTROL_STREAM_SUSPEND:
1601         case AST_CONTROL_STREAM_RESTART:
1602                 /* Fall through and set return_value to 0 */
1603                 return_value = 0;
1604                 break;
1605         default:
1606                 /* Do nothing */
1607                 break;
1608         }
1609
1610         return return_value;
1611 }
1612
1613 int ast_waitstream(struct ast_channel *c, const char *breakon)
1614 {
1615         int res;
1616
1617         res = waitstream_core(c, breakon, NULL, NULL, 0, -1, -1, NULL, NULL /* no callback */);
1618
1619         return sanitize_waitstream_return(res);
1620 }
1621
1622 int ast_waitstream_full(struct ast_channel *c, const char *breakon, int audiofd, int cmdfd)
1623 {
1624         int res;
1625
1626         res = waitstream_core(c, breakon, NULL, NULL, 0,
1627                 audiofd, cmdfd, NULL /* no context */, NULL /* no callback */);
1628
1629         return sanitize_waitstream_return(res);
1630 }
1631
1632 int ast_waitstream_exten(struct ast_channel *c, const char *context)
1633 {
1634         int res;
1635
1636         /* Waitstream, with return in the case of a valid 1 digit extension */
1637         /* in the current or specified context being pressed */
1638         if (!context)
1639                 context = ast_channel_context(c);
1640         res = waitstream_core(c, NULL, NULL, NULL, 0,
1641                 -1, -1, context, NULL /* no callback */);
1642
1643         return sanitize_waitstream_return(res);
1644 }
1645
1646 /*
1647  * if the file name is non-empty, try to play it.
1648  * Return 0 if success, -1 if error, digit if interrupted by a digit.
1649  * If digits == "" then we can simply check for non-zero.
1650  */
1651 int ast_stream_and_wait(struct ast_channel *chan, const char *file, const char *digits)
1652 {
1653         int res = 0;
1654         if (!ast_strlen_zero(file)) {
1655                 res = ast_streamfile(chan, file, ast_channel_language(chan));
1656                 if (!res) {
1657                         res = ast_waitstream(chan, digits);
1658                 }
1659         }
1660         return res;
1661 }
1662
1663 char *ast_format_str_reduce(char *fmts)
1664 {
1665         struct ast_format_def *f;
1666         struct ast_format_def *fmts_ptr[AST_MAX_FORMATS];
1667         char *fmts_str[AST_MAX_FORMATS];
1668         char *stringp, *type;
1669         char *orig = fmts;
1670         int i, j, x, first, found = 0;
1671         int len = strlen(fmts) + 1;
1672         int res;
1673
1674         if (AST_RWLIST_RDLOCK(&formats)) {
1675                 ast_log(LOG_WARNING, "Unable to lock format list\n");
1676                 return NULL;
1677         }
1678
1679         stringp = ast_strdupa(fmts);
1680
1681         for (x = 0; (type = strsep(&stringp, "|")) && x < AST_MAX_FORMATS; x++) {
1682                 AST_RWLIST_TRAVERSE(&formats, f, list) {
1683                         if (exts_compare(f->exts, type)) {
1684                                 found = 1;
1685                                 break;
1686                         }
1687                 }
1688
1689                 fmts_str[x] = type;
1690                 if (found) {
1691                         fmts_ptr[x] = f;
1692                 } else {
1693                         fmts_ptr[x] = NULL;
1694                 }
1695         }
1696         AST_RWLIST_UNLOCK(&formats);
1697
1698         first = 1;
1699         for (i = 0; i < x; i++) {
1700                 /* ignore invalid entries */
1701                 if (!fmts_ptr[i]) {
1702                         ast_log(LOG_WARNING, "ignoring unknown format '%s'\n", fmts_str[i]);
1703                         continue;
1704                 }
1705
1706                 /* special handling for the first entry */
1707                 if (first) {
1708                         res = snprintf(fmts, len, "%s", fmts_str[i]);
1709                         fmts += res;
1710                         len -= res;
1711                         first = 0;
1712                         continue;
1713                 }
1714
1715                 found = 0;
1716                 for (j = 0; j < i; j++) {
1717                         /* this is a duplicate */
1718                         if (fmts_ptr[j] == fmts_ptr[i]) {
1719                                 found = 1;
1720                                 break;
1721                         }
1722                 }
1723
1724                 if (!found) {
1725                         res = snprintf(fmts, len, "|%s", fmts_str[i]);
1726                         fmts += res;
1727                         len -= res;
1728                 }
1729         }
1730
1731         if (first) {
1732                 ast_log(LOG_WARNING, "no known formats found in format list (%s)\n", orig);
1733                 return NULL;
1734         }
1735
1736         return orig;
1737 }
1738
1739 static char *handle_cli_core_show_file_formats(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1740 {
1741 #define FORMAT "%-10s %-10s %-20s\n"
1742 #define FORMAT2 "%-10s %-10s %-20s\n"
1743         struct ast_format_def *f;
1744         int count_fmt = 0;
1745
1746         switch (cmd) {
1747         case CLI_INIT:
1748                 e->command = "core show file formats";
1749                 e->usage =
1750                         "Usage: core show file formats\n"
1751                         "       Displays currently registered file formats (if any).\n";
1752                 return NULL;
1753         case CLI_GENERATE:
1754                 return NULL;
1755         }
1756
1757         if (a->argc != 4)
1758                 return CLI_SHOWUSAGE;
1759
1760         ast_cli(a->fd, FORMAT, "Format", "Name", "Extensions");
1761         ast_cli(a->fd, FORMAT, "------", "----", "----------");
1762
1763         AST_RWLIST_RDLOCK(&formats);
1764         AST_RWLIST_TRAVERSE(&formats, f, list) {
1765                 ast_cli(a->fd, FORMAT2, ast_format_get_name(f->format), f->name, f->exts);
1766                 count_fmt++;
1767         }
1768         AST_RWLIST_UNLOCK(&formats);
1769         ast_cli(a->fd, "%d file formats registered.\n", count_fmt);
1770         return CLI_SUCCESS;
1771 #undef FORMAT
1772 #undef FORMAT2
1773 }
1774
1775 struct ast_format *ast_get_format_for_file_ext(const char *file_ext)
1776 {
1777         struct ast_format_def *f;
1778         SCOPED_RDLOCK(lock, &formats.lock);
1779         AST_RWLIST_TRAVERSE(&formats, f, list) {
1780                 if (exts_compare(f->exts, file_ext)) {
1781                         return f->format;
1782                 }
1783         }
1784
1785         return NULL;
1786 }
1787
1788 static struct ast_cli_entry cli_file[] = {
1789         AST_CLI_DEFINE(handle_cli_core_show_file_formats, "Displays file formats")
1790 };
1791
1792 static void file_shutdown(void)
1793 {
1794         ast_cli_unregister_multiple(cli_file, ARRAY_LEN(cli_file));
1795         STASIS_MESSAGE_TYPE_CLEANUP(ast_format_register_type);
1796         STASIS_MESSAGE_TYPE_CLEANUP(ast_format_unregister_type);
1797 }
1798
1799 int ast_file_init(void)
1800 {
1801         STASIS_MESSAGE_TYPE_INIT(ast_format_register_type);
1802         STASIS_MESSAGE_TYPE_INIT(ast_format_unregister_type);
1803         ast_cli_register_multiple(cli_file, ARRAY_LEN(cli_file));
1804         ast_register_cleanup(file_shutdown);
1805         return 0;
1806 }