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