59d39df8fd598bcc8e7e652a6716a455d51e0d2e
[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_FILE_VERSION(__FILE__, "$Revision$")
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         if (f->filename)
416                 free(f->filename);
417         if (f->realfilename)
418                 free(f->realfilename);
419         if (f->vfs)
420                 ast_closestream(f->vfs);
421         if (f->write_buffer) {
422                 ast_free(f->write_buffer);
423         }
424         if (f->orig_chan_name)
425                 free((void *) f->orig_chan_name);
426         ao2_cleanup(f->lastwriteformat);
427         ao2_cleanup(f->fr.subclass.format);
428         ast_module_unref(f->fmt->module);
429 }
430
431 static struct ast_filestream *get_filestream(struct ast_format_def *fmt, FILE *bfile)
432 {
433         struct ast_filestream *s;
434
435         int l = sizeof(*s) + fmt->buf_size + fmt->desc_size;    /* total allocation size */
436         if ( (s = ao2_alloc(l, filestream_destructor)) == NULL)
437                 return NULL;
438         s->fmt = fmt;
439         s->f = bfile;
440
441         if (fmt->desc_size)
442                 s->_private = ((char *)(s + 1)) + fmt->buf_size;
443         if (fmt->buf_size)
444                 s->buf = (char *)(s + 1);
445         s->fr.src = fmt->name;
446
447         if (ast_format_get_type(fmt->format) == AST_MEDIA_TYPE_AUDIO) {
448                 s->fr.frametype = AST_FRAME_VOICE;
449         } else if (ast_format_get_type(fmt->format) == AST_MEDIA_TYPE_VIDEO) {
450                 s->fr.frametype = AST_FRAME_VIDEO;
451         }
452         s->fr.mallocd = 0;
453         s->fr.subclass.format = ao2_bump(fmt->format);
454
455         return s;
456 }
457
458 /*
459  * Default implementations of open and rewrite.
460  * Only use them if you don't have expensive stuff to do.
461  */
462 enum wrap_fn { WRAP_OPEN, WRAP_REWRITE };
463
464 static int fn_wrapper(struct ast_filestream *s, const char *comment, enum wrap_fn mode)
465 {
466         struct ast_format_def *f = s->fmt;
467         int ret = -1;
468         int (*openfn)(struct ast_filestream *s);
469
470         if (mode == WRAP_OPEN && (openfn = f->open) && openfn(s))
471                 ast_log(LOG_WARNING, "Unable to open format %s\n", f->name);
472         else if (mode == WRAP_REWRITE && f->rewrite && f->rewrite(s, comment))
473                 ast_log(LOG_WARNING, "Unable to rewrite format %s\n", f->name);
474         else {
475                 /* preliminary checks succeed. update usecount */
476                 ast_module_ref(f->module);
477                 ret = 0;
478         }
479         return ret;
480 }
481
482 static int rewrite_wrapper(struct ast_filestream *s, const char *comment)
483 {
484         return fn_wrapper(s, comment, WRAP_REWRITE);
485 }
486
487 static int open_wrapper(struct ast_filestream *s)
488 {
489         return fn_wrapper(s, NULL, WRAP_OPEN);
490 }
491
492 enum file_action {
493         ACTION_EXISTS = 1, /* return matching format if file exists, 0 otherwise */
494         ACTION_DELETE,  /* delete file, return 0 on success, -1 on error */
495         ACTION_RENAME,  /* rename file. return 0 on success, -1 on error */
496         ACTION_OPEN,
497         ACTION_COPY     /* copy file. return 0 on success, -1 on error */
498 };
499
500 /*!
501  * \internal
502  * \brief perform various actions on a file. Second argument
503  * \note arg2 depends on the command:
504  *      unused for DELETE
505  *  optional ast_format_cap holding all the formats found for a file, for EXISTS.
506  *      destination file name (const char *) for COPY and RENAME
507  *      struct ast_channel * for OPEN
508  * if fmt is NULL, OPEN will return the first matching entry,
509  * whereas other functions will run on all matching entries.
510  */
511 static int filehelper(const char *filename, const void *arg2, const char *fmt, const enum file_action action)
512 {
513         struct ast_format_def *f;
514         int res = (action == ACTION_EXISTS) ? 0 : -1;
515
516         AST_RWLIST_RDLOCK(&formats);
517         /* Check for a specific format */
518         AST_RWLIST_TRAVERSE(&formats, f, list) {
519                 char *stringp, *ext = NULL;
520
521                 if (fmt && !exts_compare(f->exts, fmt))
522                         continue;
523
524                 /* Look for a file matching the supported extensions.
525                  * The file must exist, and for OPEN, must match
526                  * one of the formats supported by the channel.
527                  */
528                 stringp = ast_strdupa(f->exts); /* this is in the stack so does not need to be freed */
529                 while ( (ext = strsep(&stringp, "|")) ) {
530                         struct stat st;
531                         char *fn = build_filename(filename, ext);
532
533                         if (fn == NULL)
534                                 continue;
535
536                         if ( stat(fn, &st) ) { /* file not existent */
537                                 ast_free(fn);
538                                 continue;
539                         }
540                         /* for 'OPEN' we need to be sure that the format matches
541                          * what the channel can process
542                          */
543                         if (action == ACTION_OPEN) {
544                                 struct ast_channel *chan = (struct ast_channel *)arg2;
545                                 FILE *bfile;
546                                 struct ast_filestream *s;
547
548                                 if ((ast_format_cmp(ast_channel_writeformat(chan), f->format) == AST_FORMAT_CMP_NOT_EQUAL) &&
549                                      !(((ast_format_get_type(f->format) == AST_MEDIA_TYPE_AUDIO) && fmt) ||
550                                           ((ast_format_get_type(f->format) == AST_MEDIA_TYPE_VIDEO) && fmt))) {
551                                         ast_free(fn);
552                                         continue;       /* not a supported format */
553                                 }
554                                 if ( (bfile = fopen(fn, "r")) == NULL) {
555                                         ast_free(fn);
556                                         continue;       /* cannot open file */
557                                 }
558                                 s = get_filestream(f, bfile);
559                                 if (!s) {
560                                         fclose(bfile);
561                                         ast_free(fn);   /* cannot allocate descriptor */
562                                         continue;
563                                 }
564                                 if (open_wrapper(s)) {
565                                         ast_free(fn);
566                                         ast_closestream(s);
567                                         continue;       /* cannot run open on file */
568                                 }
569                                 if (st.st_size == 0) {
570                                         ast_log(LOG_WARNING, "File %s detected to have zero size.\n", fn);
571                                 }
572                                 /* ok this is good for OPEN */
573                                 res = 1;        /* found */
574                                 s->lasttimeout = -1;
575                                 s->fmt = f;
576                                 s->trans = NULL;
577                                 s->filename = NULL;
578                                 if (ast_format_get_type(s->fmt->format) == AST_MEDIA_TYPE_AUDIO) {
579                                         if (ast_channel_stream(chan))
580                                                 ast_closestream(ast_channel_stream(chan));
581                                         ast_channel_stream_set(chan, s);
582                                 } else {
583                                         if (ast_channel_vstream(chan))
584                                                 ast_closestream(ast_channel_vstream(chan));
585                                         ast_channel_vstream_set(chan, s);
586                                 }
587                                 ast_free(fn);
588                                 break;
589                         }
590                         switch (action) {
591                         case ACTION_OPEN:
592                                 break;  /* will never get here */
593
594                         case ACTION_EXISTS:     /* return the matching format */
595                                 /* if arg2 is present, it is a format capabilities structure.
596                                  * Add this format to the set of formats this file can be played in */
597                                 if (arg2) {
598                                         ast_format_cap_append((struct ast_format_cap *) arg2, f->format, 0);
599                                 }
600                                 res = 1; /* file does exist and format it exists in is returned in arg2 */
601                                 break;
602
603                         case ACTION_DELETE:
604                                 if ( (res = unlink(fn)) )
605                                         ast_log(LOG_WARNING, "unlink(%s) failed: %s\n", fn, strerror(errno));
606                                 break;
607
608                         case ACTION_RENAME:
609                         case ACTION_COPY: {
610                                 char *nfn = build_filename((const char *)arg2, ext);
611                                 if (!nfn)
612                                         ast_log(LOG_WARNING, "Out of memory\n");
613                                 else {
614                                         res = action == ACTION_COPY ? copy(fn, nfn) : rename(fn, nfn);
615                                         if (res)
616                                                 ast_log(LOG_WARNING, "%s(%s,%s) failed: %s\n",
617                                                         action == ACTION_COPY ? "copy" : "rename",
618                                                          fn, nfn, strerror(errno));
619                                         ast_free(nfn);
620                                 }
621                             }
622                                 break;
623
624                         default:
625                                 ast_log(LOG_WARNING, "Unknown helper %u\n", action);
626                         }
627                         ast_free(fn);
628                 }
629         }
630         AST_RWLIST_UNLOCK(&formats);
631         return res;
632 }
633
634 static int is_absolute_path(const char *filename)
635 {
636         return filename[0] == '/';
637 }
638
639 /*!
640  * \brief test if a file exists for a given format.
641  * \note result_cap is OPTIONAL
642  * \retval 1, true and result_cap represents format capabilities file exists in.
643  * \retval 0, false
644  */
645 static int fileexists_test(const char *filename, const char *fmt, const char *lang,
646                            char *buf, int buflen, struct ast_format_cap *result_cap)
647 {
648         if (buf == NULL) {
649                 return 0;
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_set_oldwriteformat(chan, ast_channel_writeformat(chan));
781         /* Set the channel to the best format that exists for the file. */
782         res = ast_set_write_format_from_cap(chan, file_fmt_cap);
783         /* don't need this anymore now that the channel's write format is set. */
784         ao2_ref(file_fmt_cap, -1);
785
786         if (res == -1) {        /* No format available that works with this channel */
787                 return NULL;
788         }
789         res = filehelper(buf, chan, NULL, ACTION_OPEN);
790         if (res >= 0)
791                 return ast_channel_stream(chan);
792         return NULL;
793 }
794
795 struct ast_filestream *ast_openvstream(struct ast_channel *chan, const char *filename, const char *preflang)
796 {
797         /* As above, but for video. But here we don't have translators
798          * so we must enforce a format.
799          */
800         struct ast_format_cap *tmp_cap;
801         char *buf;
802         int buflen;
803         int i, fd;
804
805         if (preflang == NULL) {
806                 preflang = "";
807         }
808         buflen = strlen(preflang) + strlen(filename) + 4;
809         buf = ast_alloca(buflen);
810
811         /* is the channel capable of video without translation ?*/
812         if (!ast_format_cap_has_type(ast_channel_nativeformats(chan), AST_MEDIA_TYPE_VIDEO)) {
813                 return NULL;
814         }
815         if (!(tmp_cap = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT))) {
816                 return NULL;
817         }
818         /* Video is supported, so see what video formats exist for this file */
819         if (!fileexists_core(filename, NULL, preflang, buf, buflen, tmp_cap)) {
820                 ao2_ref(tmp_cap, -1);
821                 return NULL;
822         }
823
824         /* iterate over file formats and pick the first one compatible with the channel's native formats */
825         for (i = 0; i < ast_format_cap_count(tmp_cap); ++i) {
826                 struct ast_format *format = ast_format_cap_get_format(tmp_cap, i);
827
828                 if ((ast_format_get_type(format) != AST_MEDIA_TYPE_VIDEO) ||
829                         !ast_format_cap_iscompatible(ast_channel_nativeformats(chan), tmp_cap)) {
830                         ao2_ref(format, -1);
831                         continue;
832                 }
833
834                 fd = filehelper(buf, chan, ast_format_get_name(format), ACTION_OPEN);
835                 if (fd >= 0) {
836                         ao2_ref(format, -1);
837                         ao2_ref(tmp_cap, -1);
838                         return ast_channel_vstream(chan);
839                 }
840                 ast_log(LOG_WARNING, "File %s has video but couldn't be opened\n", filename);
841                 ao2_ref(format, -1);
842         }
843         ao2_ref(tmp_cap, -1);
844
845         return NULL;
846 }
847
848 static struct ast_frame *read_frame(struct ast_filestream *s, int *whennext)
849 {
850         struct ast_frame *fr, *new_fr;
851
852         if (!s || !s->fmt) {
853                 return NULL;
854         }
855
856         if (!(fr = s->fmt->read(s, whennext))) {
857                 return NULL;
858         }
859
860         if (!(new_fr = ast_frisolate(fr))) {
861                 ast_frfree(fr);
862                 return NULL;
863         }
864
865         if (new_fr != fr) {
866                 ast_frfree(fr);
867                 fr = new_fr;
868         }
869
870         return fr;
871 }
872
873 struct ast_frame *ast_readframe(struct ast_filestream *s)
874 {
875         int whennext = 0;
876
877         return read_frame(s, &whennext);
878 }
879
880 enum fsread_res {
881         FSREAD_FAILURE,
882         FSREAD_SUCCESS_SCHED,
883         FSREAD_SUCCESS_NOSCHED,
884 };
885
886 static int ast_fsread_audio(const void *data);
887
888 static enum fsread_res ast_readaudio_callback(struct ast_filestream *s)
889 {
890         int whennext = 0;
891
892         while (!whennext) {
893                 struct ast_frame *fr;
894
895                 if (s->orig_chan_name && strcasecmp(ast_channel_name(s->owner), s->orig_chan_name)) {
896                         goto return_failure;
897                 }
898
899                 fr = read_frame(s, &whennext);
900
901                 if (!fr /* stream complete */ || ast_write(s->owner, fr) /* error writing */) {
902                         if (fr) {
903                                 ast_log(LOG_WARNING, "Failed to write frame\n");
904                                 ast_frfree(fr);
905                         }
906                         goto return_failure;
907                 }
908
909                 if (fr) {
910                         ast_frfree(fr);
911                 }
912         }
913
914         if (whennext != s->lasttimeout) {
915                 if (ast_channel_timingfd(s->owner) > -1) {
916                         float samp_rate = (float) ast_format_get_sample_rate(s->fmt->format);
917                         unsigned int rate;
918
919                         rate = (unsigned int) roundf(samp_rate / ((float) whennext));
920
921                         ast_settimeout_full(s->owner, rate, ast_fsread_audio, s, 1);
922                 } else {
923                         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));
924                 }
925                 s->lasttimeout = whennext;
926                 return FSREAD_SUCCESS_NOSCHED;
927         }
928         return FSREAD_SUCCESS_SCHED;
929
930 return_failure:
931         ast_channel_streamid_set(s->owner, -1);
932         ast_settimeout(s->owner, 0, NULL, NULL);
933         return FSREAD_FAILURE;
934 }
935
936 static int ast_fsread_audio(const void *data)
937 {
938         struct ast_filestream *fs = (struct ast_filestream *)data;
939         enum fsread_res res;
940
941         res = ast_readaudio_callback(fs);
942
943         if (res == FSREAD_SUCCESS_SCHED)
944                 return 1;
945
946         return 0;
947 }
948
949 static int ast_fsread_video(const void *data);
950
951 static enum fsread_res ast_readvideo_callback(struct ast_filestream *s)
952 {
953         int whennext = 0;
954
955         while (!whennext) {
956                 struct ast_frame *fr = read_frame(s, &whennext);
957
958                 if (!fr /* stream complete */ || ast_write(s->owner, fr) /* error writing */) {
959                         if (fr) {
960                                 ast_log(LOG_WARNING, "Failed to write frame\n");
961                                 ast_frfree(fr);
962                         }
963                         ast_channel_vstreamid_set(s->owner, -1);
964                         return FSREAD_FAILURE;
965                 }
966
967                 if (fr) {
968                         ast_frfree(fr);
969                 }
970         }
971
972         if (whennext != s->lasttimeout) {
973                 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));
974                 s->lasttimeout = whennext;
975                 return FSREAD_SUCCESS_NOSCHED;
976         }
977
978         return FSREAD_SUCCESS_SCHED;
979 }
980
981 static int ast_fsread_video(const void *data)
982 {
983         struct ast_filestream *fs = (struct ast_filestream *)data;
984         enum fsread_res res;
985
986         res = ast_readvideo_callback(fs);
987
988         if (res == FSREAD_SUCCESS_SCHED)
989                 return 1;
990
991         return 0;
992 }
993
994 int ast_applystream(struct ast_channel *chan, struct ast_filestream *s)
995 {
996         s->owner = chan;
997         return 0;
998 }
999
1000 int ast_playstream(struct ast_filestream *s)
1001 {
1002         enum fsread_res res;
1003
1004         if (ast_format_get_type(s->fmt->format) == AST_MEDIA_TYPE_AUDIO)
1005                 res = ast_readaudio_callback(s);
1006         else
1007                 res = ast_readvideo_callback(s);
1008
1009         return (res == FSREAD_FAILURE) ? -1 : 0;
1010 }
1011
1012 int ast_seekstream(struct ast_filestream *fs, off_t sample_offset, int whence)
1013 {
1014         return fs->fmt->seek(fs, sample_offset, whence);
1015 }
1016
1017 int ast_truncstream(struct ast_filestream *fs)
1018 {
1019         return fs->fmt->trunc(fs);
1020 }
1021
1022 off_t ast_tellstream(struct ast_filestream *fs)
1023 {
1024         return fs->fmt->tell(fs);
1025 }
1026
1027 int ast_stream_fastforward(struct ast_filestream *fs, off_t ms)
1028 {
1029         return ast_seekstream(fs, ms * DEFAULT_SAMPLES_PER_MS, SEEK_CUR);
1030 }
1031
1032 int ast_stream_rewind(struct ast_filestream *fs, off_t ms)
1033 {
1034         return ast_seekstream(fs, -ms * DEFAULT_SAMPLES_PER_MS, SEEK_CUR);
1035 }
1036
1037 int ast_closestream(struct ast_filestream *f)
1038 {
1039         /* This used to destroy the filestream, but it now just decrements a refcount.
1040          * We close the stream in order to quit queuing frames now, because we might
1041          * change the writeformat, which could result in a subsequent write error, if
1042          * the format is different. */
1043         if (f == NULL) {
1044                 return 0;
1045         }
1046         filestream_close(f);
1047         ao2_ref(f, -1);
1048         return 0;
1049 }
1050
1051
1052 /*
1053  * Look the various language-specific places where a file could exist.
1054  */
1055 int ast_fileexists(const char *filename, const char *fmt, const char *preflang)
1056 {
1057         char *buf;
1058         int buflen;
1059
1060         if (preflang == NULL)
1061                 preflang = "";
1062         buflen = strlen(preflang) + strlen(filename) + 4;       /* room for everything */
1063         buf = ast_alloca(buflen);
1064         return fileexists_core(filename, fmt, preflang, buf, buflen, NULL) ? 1 : 0;
1065 }
1066
1067 int ast_filedelete(const char *filename, const char *fmt)
1068 {
1069         return filehelper(filename, NULL, fmt, ACTION_DELETE);
1070 }
1071
1072 int ast_filerename(const char *filename, const char *filename2, const char *fmt)
1073 {
1074         return filehelper(filename, filename2, fmt, ACTION_RENAME);
1075 }
1076
1077 int ast_filecopy(const char *filename, const char *filename2, const char *fmt)
1078 {
1079         return filehelper(filename, filename2, fmt, ACTION_COPY);
1080 }
1081
1082 int ast_streamfile(struct ast_channel *chan, const char *filename, const char *preflang)
1083 {
1084         struct ast_filestream *fs;
1085         struct ast_filestream *vfs=NULL;
1086         off_t pos;
1087         int seekattempt;
1088         int res;
1089
1090         fs = ast_openstream(chan, filename, preflang);
1091         if (!fs) {
1092                 struct ast_str *codec_buf = ast_str_alloca(64);
1093                 ast_log(LOG_WARNING, "Unable to open %s (format %s): %s\n",
1094                         filename, ast_format_cap_get_names(ast_channel_nativeformats(chan), &codec_buf), strerror(errno));
1095                 return -1;
1096         }
1097
1098         /* check to see if there is any data present (not a zero length file),
1099          * done this way because there is no where for ast_openstream_full to
1100          * return the file had no data. */
1101         pos = ftello(fs->f);
1102         seekattempt = fseeko(fs->f, -1, SEEK_END);
1103         if (seekattempt) {
1104                 if (errno == EINVAL) {
1105                         /* Zero-length file, as opposed to a pipe */
1106                         return 0;
1107                 } else {
1108                         ast_seekstream(fs, 0, SEEK_SET);
1109                 }
1110         } else {
1111                 fseeko(fs->f, pos, SEEK_SET);
1112         }
1113
1114         vfs = ast_openvstream(chan, filename, preflang);
1115         if (vfs) {
1116                 ast_debug(1, "Ooh, found a video stream, too, format %s\n", ast_format_get_name(vfs->fmt->format));
1117         }
1118
1119         if (ast_test_flag(ast_channel_flags(chan), AST_FLAG_MASQ_NOSTREAM))
1120                 fs->orig_chan_name = ast_strdup(ast_channel_name(chan));
1121         if (ast_applystream(chan, fs))
1122                 return -1;
1123         if (vfs && ast_applystream(chan, vfs))
1124                 return -1;
1125         res = ast_playstream(fs);
1126         if (!res && vfs)
1127                 res = ast_playstream(vfs);
1128         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");
1129
1130         return res;
1131 }
1132
1133 struct ast_filestream *ast_readfile(const char *filename, const char *type, const char *comment, int flags, int check, mode_t mode)
1134 {
1135         FILE *bfile;
1136         struct ast_format_def *f;
1137         struct ast_filestream *fs = NULL;
1138         char *fn;
1139         int format_found = 0;
1140
1141         AST_RWLIST_RDLOCK(&formats);
1142
1143         AST_RWLIST_TRAVERSE(&formats, f, list) {
1144                 fs = NULL;
1145                 if (!exts_compare(f->exts, type))
1146                         continue;
1147                 else
1148                         format_found = 1;
1149
1150                 fn = build_filename(filename, type);
1151                 if (!fn) {
1152                         continue;
1153                 }
1154                 errno = 0;
1155                 bfile = fopen(fn, "r");
1156
1157                 if (!bfile || (fs = get_filestream(f, bfile)) == NULL || open_wrapper(fs) ) {
1158                         ast_log(LOG_WARNING, "Unable to open %s\n", fn);
1159                         if (fs) {
1160                                 ast_closestream(fs);
1161                         }
1162                         fs = NULL;
1163                         bfile = NULL;
1164                         ast_free(fn);
1165                         break;
1166                 }
1167                 /* found it */
1168                 fs->trans = NULL;
1169                 fs->fmt = f;
1170                 fs->flags = flags;
1171                 fs->mode = mode;
1172                 fs->filename = ast_strdup(filename);
1173                 fs->vfs = NULL;
1174                 ast_free(fn);
1175                 break;
1176         }
1177
1178         AST_RWLIST_UNLOCK(&formats);
1179         if (!format_found)
1180                 ast_log(LOG_WARNING, "No such format '%s'\n", type);
1181
1182         return fs;
1183 }
1184
1185 struct ast_filestream *ast_writefile(const char *filename, const char *type, const char *comment, int flags, int check, mode_t mode)
1186 {
1187         int fd, myflags = 0;
1188         /* compiler claims this variable can be used before initialization... */
1189         FILE *bfile = NULL;
1190         struct ast_format_def *f;
1191         struct ast_filestream *fs = NULL;
1192         char *buf = NULL;
1193         size_t size = 0;
1194         int format_found = 0;
1195
1196         AST_RWLIST_RDLOCK(&formats);
1197
1198         /* set the O_TRUNC flag if and only if there is no O_APPEND specified */
1199         /* We really can't use O_APPEND as it will break WAV header updates */
1200         if (flags & O_APPEND) {
1201                 flags &= ~O_APPEND;
1202         } else {
1203                 myflags = O_TRUNC;
1204         }
1205
1206         myflags |= O_WRONLY | O_CREAT;
1207
1208         /* XXX need to fix this - we should just do the fopen,
1209          * not open followed by fdopen()
1210          */
1211         AST_RWLIST_TRAVERSE(&formats, f, list) {
1212                 char *fn, *orig_fn = NULL;
1213                 if (fs)
1214                         break;
1215
1216                 if (!exts_compare(f->exts, type))
1217                         continue;
1218                 else
1219                         format_found = 1;
1220
1221                 fn = build_filename(filename, type);
1222                 if (!fn) {
1223                         continue;
1224                 }
1225                 fd = open(fn, flags | myflags, mode);
1226                 if (fd > -1) {
1227                         /* fdopen() the resulting file stream */
1228                         bfile = fdopen(fd, ((flags | myflags) & O_RDWR) ? "w+" : "w");
1229                         if (!bfile) {
1230                                 ast_log(LOG_WARNING, "Whoa, fdopen failed: %s!\n", strerror(errno));
1231                                 close(fd);
1232                                 fd = -1;
1233                         }
1234                 }
1235
1236                 if (ast_opt_cache_record_files && (fd > -1)) {
1237                         char *c;
1238
1239                         fclose(bfile);  /* this also closes fd */
1240                         /*
1241                           We touch orig_fn just as a place-holder so other things (like vmail) see the file is there.
1242                           What we are really doing is writing to record_cache_dir until we are done then we will mv the file into place.
1243                         */
1244                         orig_fn = ast_strdupa(fn);
1245                         for (c = fn; *c; c++)
1246                                 if (*c == '/')
1247                                         *c = '_';
1248
1249                         size = strlen(fn) + strlen(record_cache_dir) + 2;
1250                         buf = ast_alloca(size);
1251                         strcpy(buf, record_cache_dir);
1252                         strcat(buf, "/");
1253                         strcat(buf, fn);
1254                         ast_free(fn);
1255                         fn = buf;
1256                         fd = open(fn, flags | myflags, mode);
1257                         if (fd > -1) {
1258                                 /* fdopen() the resulting file stream */
1259                                 bfile = fdopen(fd, ((flags | myflags) & O_RDWR) ? "w+" : "w");
1260                                 if (!bfile) {
1261                                         ast_log(LOG_WARNING, "Whoa, fdopen failed: %s!\n", strerror(errno));
1262                                         close(fd);
1263                                         fd = -1;
1264                                 }
1265                         }
1266                 }
1267                 if (fd > -1) {
1268                         errno = 0;
1269                         fs = get_filestream(f, bfile);
1270                         if (fs) {
1271                                 if ((fs->write_buffer = ast_malloc(32768))) {
1272                                         setvbuf(fs->f, fs->write_buffer, _IOFBF, 32768);
1273                                 }
1274                         }
1275                         if (!fs || rewrite_wrapper(fs, comment)) {
1276                                 ast_log(LOG_WARNING, "Unable to rewrite %s\n", fn);
1277                                 close(fd);
1278                                 if (orig_fn) {
1279                                         unlink(fn);
1280                                         unlink(orig_fn);
1281                                 }
1282                                 if (fs) {
1283                                         ast_closestream(fs);
1284                                         fs = NULL;
1285                                 }
1286                                 if (!buf) {
1287                                         ast_free(fn);
1288                                 }
1289                                 continue;
1290                         }
1291                         fs->trans = NULL;
1292                         fs->fmt = f;
1293                         fs->flags = flags;
1294                         fs->mode = mode;
1295                         if (orig_fn) {
1296                                 fs->realfilename = ast_strdup(orig_fn);
1297                                 fs->filename = ast_strdup(fn);
1298                         } else {
1299                                 fs->realfilename = NULL;
1300                                 fs->filename = ast_strdup(filename);
1301                         }
1302                         fs->vfs = NULL;
1303                         /* If truncated, we'll be at the beginning; if not truncated, then append */
1304                         f->seek(fs, 0, SEEK_END);
1305                 } else if (errno != EEXIST) {
1306                         ast_log(LOG_WARNING, "Unable to open file %s: %s\n", fn, strerror(errno));
1307                         if (orig_fn)
1308                                 unlink(orig_fn);
1309                 }
1310                 /* if buf != NULL then fn is already free and pointing to it */
1311                 if (!buf)
1312                         ast_free(fn);
1313         }
1314
1315         AST_RWLIST_UNLOCK(&formats);
1316
1317         if (!format_found)
1318                 ast_log(LOG_WARNING, "No such format '%s'\n", type);
1319
1320         return fs;
1321 }
1322
1323 static void waitstream_control(struct ast_channel *c,
1324                 enum ast_waitstream_fr_cb_values type,
1325                 ast_waitstream_fr_cb cb,
1326                 int skip_ms)
1327 {
1328         switch (type)
1329         {
1330         case AST_WAITSTREAM_CB_FASTFORWARD:
1331                 {
1332                         int eoftest;
1333                         ast_stream_fastforward(ast_channel_stream(c), skip_ms);
1334                         eoftest = fgetc(ast_channel_stream(c)->f);
1335                         if (feof(ast_channel_stream(c)->f)) {
1336                                 ast_stream_rewind(ast_channel_stream(c), skip_ms);
1337                         } else {
1338                                 ungetc(eoftest, ast_channel_stream(c)->f);
1339                         }
1340                 }
1341                 break;
1342         case AST_WAITSTREAM_CB_REWIND:
1343                 ast_stream_rewind(ast_channel_stream(c), skip_ms);
1344                 break;
1345         default:
1346                 break;
1347         }
1348
1349         if (cb) {
1350                 long ms_len = ast_tellstream(ast_channel_stream(c)) / (ast_format_get_sample_rate(ast_channel_stream(c)->fmt->format) / 1000);
1351                 cb(c, ms_len, type);
1352         }
1353
1354         ast_test_suite_event_notify("PLAYBACK","Channel: %s\r\n"
1355                 "Control: %s\r\n"
1356                 "SkipMs: %d\r\n",
1357                 ast_channel_name(c),
1358                 (type == AST_WAITSTREAM_CB_FASTFORWARD) ? "FastForward" : "Rewind",
1359                 skip_ms);
1360 }
1361
1362 /*!
1363  * \brief the core of all waitstream() functions
1364  */
1365 static int waitstream_core(struct ast_channel *c,
1366         const char *breakon,
1367         const char *forward,
1368         const char *reverse,
1369         int skip_ms,
1370         int audiofd,
1371         int cmdfd,
1372         const char *context,
1373         ast_waitstream_fr_cb cb)
1374 {
1375         const char *orig_chan_name = NULL;
1376
1377         int err = 0;
1378
1379         if (!breakon)
1380                 breakon = "";
1381         if (!forward)
1382                 forward = "";
1383         if (!reverse)
1384                 reverse = "";
1385
1386         /* Switch the channel to end DTMF frame only. waitstream_core doesn't care about the start of DTMF. */
1387         ast_set_flag(ast_channel_flags(c), AST_FLAG_END_DTMF_ONLY);
1388
1389         if (ast_test_flag(ast_channel_flags(c), AST_FLAG_MASQ_NOSTREAM))
1390                 orig_chan_name = ast_strdupa(ast_channel_name(c));
1391
1392         if (ast_channel_stream(c) && cb) {
1393                 long ms_len = ast_tellstream(ast_channel_stream(c)) / (ast_format_get_sample_rate(ast_channel_stream(c)->fmt->format) / 1000);
1394                 cb(c, ms_len, AST_WAITSTREAM_CB_START);
1395         }
1396
1397         while (ast_channel_stream(c)) {
1398                 int res;
1399                 int ms;
1400
1401                 if (orig_chan_name && strcasecmp(orig_chan_name, ast_channel_name(c))) {
1402                         ast_stopstream(c);
1403                         err = 1;
1404                         break;
1405                 }
1406
1407                 ms = ast_sched_wait(ast_channel_sched(c));
1408
1409                 if (ms < 0 && !ast_channel_timingfunc(c)) {
1410                         ast_stopstream(c);
1411                         break;
1412                 }
1413                 if (ms < 0)
1414                         ms = 1000;
1415                 if (cmdfd < 0) {
1416                         res = ast_waitfor(c, ms);
1417                         if (res < 0) {
1418                                 ast_log(LOG_WARNING, "Select failed (%s)\n", strerror(errno));
1419                                 ast_clear_flag(ast_channel_flags(c), AST_FLAG_END_DTMF_ONLY);
1420                                 return res;
1421                         }
1422                 } else {
1423                         int outfd;
1424                         struct ast_channel *rchan = ast_waitfor_nandfds(&c, 1, &cmdfd, (cmdfd > -1) ? 1 : 0, NULL, &outfd, &ms);
1425                         if (!rchan && (outfd < 0) && (ms)) {
1426                                 /* Continue */
1427                                 if (errno == EINTR)
1428                                         continue;
1429                                 ast_log(LOG_WARNING, "Wait failed (%s)\n", strerror(errno));
1430                                 ast_clear_flag(ast_channel_flags(c), AST_FLAG_END_DTMF_ONLY);
1431                                 return -1;
1432                         } else if (outfd > -1) { /* this requires cmdfd set */
1433                                 /* The FD we were watching has something waiting */
1434                                 ast_clear_flag(ast_channel_flags(c), AST_FLAG_END_DTMF_ONLY);
1435                                 return 1;
1436                         }
1437                         /* if rchan is set, it is 'c' */
1438                         res = rchan ? 1 : 0; /* map into 'res' values */
1439                 }
1440                 if (res > 0) {
1441                         struct ast_frame *fr = ast_read(c);
1442                         if (!fr) {
1443                                 ast_clear_flag(ast_channel_flags(c), AST_FLAG_END_DTMF_ONLY);
1444                                 return -1;
1445                         }
1446                         switch (fr->frametype) {
1447                         case AST_FRAME_DTMF_END:
1448                                 if (context) {
1449                                         const char exten[2] = { fr->subclass.integer, '\0' };
1450                                         if (ast_exists_extension(c, context, exten, 1,
1451                                                 S_COR(ast_channel_caller(c)->id.number.valid, ast_channel_caller(c)->id.number.str, NULL))) {
1452                                                 res = fr->subclass.integer;
1453                                                 ast_frfree(fr);
1454                                                 ast_clear_flag(ast_channel_flags(c), AST_FLAG_END_DTMF_ONLY);
1455                                                 return res;
1456                                         }
1457                                 } else {
1458                                         res = fr->subclass.integer;
1459                                         if (strchr(forward, res)) {
1460                                                 waitstream_control(c, AST_WAITSTREAM_CB_FASTFORWARD, cb, skip_ms);
1461                                         } else if (strchr(reverse, res)) {
1462                                                 waitstream_control(c, AST_WAITSTREAM_CB_REWIND, cb, skip_ms);
1463                                         } else if (strchr(breakon, res)) {
1464                                                 ast_test_suite_event_notify("PLAYBACK","Channel: %s\r\n"
1465                                                         "Control: %s\r\n",
1466                                                         ast_channel_name(c),
1467                                                         "Break");
1468
1469                                                 ast_frfree(fr);
1470                                                 ast_clear_flag(ast_channel_flags(c), AST_FLAG_END_DTMF_ONLY);
1471                                                 return res;
1472                                         }
1473                                 }
1474                                 break;
1475                         case AST_FRAME_CONTROL:
1476                                 switch (fr->subclass.integer) {
1477                                 case AST_CONTROL_STREAM_STOP:
1478                                 case AST_CONTROL_STREAM_SUSPEND:
1479                                 case AST_CONTROL_STREAM_RESTART:
1480                                         /* Fall-through and break out */
1481                                         ast_test_suite_event_notify("PLAYBACK","Channel: %s\r\n"
1482                                                 "Control: %s\r\n",
1483                                                 ast_channel_name(c),
1484                                                 "Break");
1485                                         res = fr->subclass.integer;
1486                                         ast_frfree(fr);
1487                                         ast_clear_flag(ast_channel_flags(c), AST_FLAG_END_DTMF_ONLY);
1488                                         return res;
1489                                 case AST_CONTROL_STREAM_REVERSE:
1490                                         if (!skip_ms) {
1491                                                 skip_ms = 3000;
1492                                         }
1493                                         waitstream_control(c, AST_WAITSTREAM_CB_REWIND, cb, skip_ms);
1494                                         break;
1495                                 case AST_CONTROL_STREAM_FORWARD:
1496                                         if (!skip_ms) {
1497                                                 skip_ms = 3000;
1498                                         }
1499                                         waitstream_control(c, AST_WAITSTREAM_CB_FASTFORWARD, cb, skip_ms);
1500                                         break;
1501                                 case AST_CONTROL_HANGUP:
1502                                 case AST_CONTROL_BUSY:
1503                                 case AST_CONTROL_CONGESTION:
1504                                         ast_frfree(fr);
1505                                         ast_clear_flag(ast_channel_flags(c), AST_FLAG_END_DTMF_ONLY);
1506                                         return -1;
1507                                 case AST_CONTROL_RINGING:
1508                                 case AST_CONTROL_ANSWER:
1509                                 case AST_CONTROL_VIDUPDATE:
1510                                 case AST_CONTROL_SRCUPDATE:
1511                                 case AST_CONTROL_SRCCHANGE:
1512                                 case AST_CONTROL_HOLD:
1513                                 case AST_CONTROL_UNHOLD:
1514                                 case AST_CONTROL_CONNECTED_LINE:
1515                                 case AST_CONTROL_REDIRECTING:
1516                                 case AST_CONTROL_AOC:
1517                                 case AST_CONTROL_UPDATE_RTP_PEER:
1518                                 case AST_CONTROL_PVT_CAUSE_CODE:
1519                                 case -1:
1520                                         /* Unimportant */
1521                                         break;
1522                                 default:
1523                                         ast_log(LOG_WARNING, "Unexpected control subclass '%d'\n", fr->subclass.integer);
1524                                 }
1525                                 break;
1526                         case AST_FRAME_VOICE:
1527                                 /* Write audio if appropriate */
1528                                 if (audiofd > -1) {
1529                                         if (write(audiofd, fr->data.ptr, fr->datalen) < 0) {
1530                                                 ast_log(LOG_WARNING, "write() failed: %s\n", strerror(errno));
1531                                         }
1532                                 }
1533                         default:
1534                                 /* Ignore all others */
1535                                 break;
1536                         }
1537                         ast_frfree(fr);
1538                 }
1539                 ast_sched_runq(ast_channel_sched(c));
1540         }
1541
1542         ast_clear_flag(ast_channel_flags(c), AST_FLAG_END_DTMF_ONLY);
1543
1544         return (err || ast_channel_softhangup_internal_flag(c)) ? -1 : 0;
1545 }
1546
1547 int ast_waitstream_fr_w_cb(struct ast_channel *c,
1548         const char *breakon,
1549         const char *forward,
1550         const char *reverse,
1551         int ms,
1552         ast_waitstream_fr_cb cb)
1553 {
1554         return waitstream_core(c, breakon, forward, reverse, ms,
1555                 -1 /* no audiofd */, -1 /* no cmdfd */, NULL /* no context */, cb);
1556 }
1557
1558 int ast_waitstream_fr(struct ast_channel *c, const char *breakon, const char *forward, const char *reverse, int ms)
1559 {
1560         return waitstream_core(c, breakon, forward, reverse, ms,
1561                 -1 /* no audiofd */, -1 /* no cmdfd */, NULL /* no context */, NULL /* no callback */);
1562 }
1563
1564 /*! \internal
1565  * \brief Clean up the return value of a waitstream call
1566  *
1567  * It's possible for a control frame to come in from an external source and break the
1568  * playback. From a consumer of most ast_waitstream_* function callers, this should
1569  * appear like normal playback termination, i.e., return 0 and not the value of the
1570  * control frame.
1571  */
1572 static int sanitize_waitstream_return(int return_value)
1573 {
1574         switch (return_value) {
1575         case AST_CONTROL_STREAM_STOP:
1576         case AST_CONTROL_STREAM_SUSPEND:
1577         case AST_CONTROL_STREAM_RESTART:
1578                 /* Fall through and set return_value to 0 */
1579                 return_value = 0;
1580                 break;
1581         default:
1582                 /* Do nothing */
1583                 break;
1584         }
1585
1586         return return_value;
1587 }
1588
1589 int ast_waitstream(struct ast_channel *c, const char *breakon)
1590 {
1591         int res;
1592
1593         res = waitstream_core(c, breakon, NULL, NULL, 0, -1, -1, NULL, NULL /* no callback */);
1594
1595         return sanitize_waitstream_return(res);
1596 }
1597
1598 int ast_waitstream_full(struct ast_channel *c, const char *breakon, int audiofd, int cmdfd)
1599 {
1600         int res;
1601
1602         res = waitstream_core(c, breakon, NULL, NULL, 0,
1603                 audiofd, cmdfd, NULL /* no context */, NULL /* no callback */);
1604
1605         return sanitize_waitstream_return(res);
1606 }
1607
1608 int ast_waitstream_exten(struct ast_channel *c, const char *context)
1609 {
1610         int res;
1611
1612         /* Waitstream, with return in the case of a valid 1 digit extension */
1613         /* in the current or specified context being pressed */
1614         if (!context)
1615                 context = ast_channel_context(c);
1616         res = waitstream_core(c, NULL, NULL, NULL, 0,
1617                 -1, -1, context, NULL /* no callback */);
1618
1619         return sanitize_waitstream_return(res);
1620 }
1621
1622 /*
1623  * if the file name is non-empty, try to play it.
1624  * Return 0 if success, -1 if error, digit if interrupted by a digit.
1625  * If digits == "" then we can simply check for non-zero.
1626  */
1627 int ast_stream_and_wait(struct ast_channel *chan, const char *file, const char *digits)
1628 {
1629         int res = 0;
1630         if (!ast_strlen_zero(file)) {
1631                 ast_test_suite_event_notify("PLAYBACK", "Message: %s\r\nChannel: %s", file, ast_channel_name(chan));
1632                 res = ast_streamfile(chan, file, ast_channel_language(chan));
1633                 if (!res) {
1634                         res = ast_waitstream(chan, digits);
1635                 }
1636         }
1637         return res;
1638 }
1639
1640 char *ast_format_str_reduce(char *fmts)
1641 {
1642         struct ast_format_def *f;
1643         struct ast_format_def *fmts_ptr[AST_MAX_FORMATS];
1644         char *fmts_str[AST_MAX_FORMATS];
1645         char *stringp, *type;
1646         char *orig = fmts;
1647         int i, j, x, first, found = 0;
1648         int len = strlen(fmts) + 1;
1649         int res;
1650
1651         if (AST_RWLIST_RDLOCK(&formats)) {
1652                 ast_log(LOG_WARNING, "Unable to lock format list\n");
1653                 return NULL;
1654         }
1655
1656         stringp = ast_strdupa(fmts);
1657
1658         for (x = 0; (type = strsep(&stringp, "|")) && x < AST_MAX_FORMATS; x++) {
1659                 AST_RWLIST_TRAVERSE(&formats, f, list) {
1660                         if (exts_compare(f->exts, type)) {
1661                                 found = 1;
1662                                 break;
1663                         }
1664                 }
1665
1666                 fmts_str[x] = type;
1667                 if (found) {
1668                         fmts_ptr[x] = f;
1669                 } else {
1670                         fmts_ptr[x] = NULL;
1671                 }
1672         }
1673         AST_RWLIST_UNLOCK(&formats);
1674
1675         first = 1;
1676         for (i = 0; i < x; i++) {
1677                 /* ignore invalid entries */
1678                 if (!fmts_ptr[i]) {
1679                         ast_log(LOG_WARNING, "ignoring unknown format '%s'\n", fmts_str[i]);
1680                         continue;
1681                 }
1682
1683                 /* special handling for the first entry */
1684                 if (first) {
1685                         res = snprintf(fmts, len, "%s", fmts_str[i]);
1686                         fmts += res;
1687                         len -= res;
1688                         first = 0;
1689                         continue;
1690                 }
1691
1692                 found = 0;
1693                 for (j = 0; j < i; j++) {
1694                         /* this is a duplicate */
1695                         if (fmts_ptr[j] == fmts_ptr[i]) {
1696                                 found = 1;
1697                                 break;
1698                         }
1699                 }
1700
1701                 if (!found) {
1702                         res = snprintf(fmts, len, "|%s", fmts_str[i]);
1703                         fmts += res;
1704                         len -= res;
1705                 }
1706         }
1707
1708         if (first) {
1709                 ast_log(LOG_WARNING, "no known formats found in format list (%s)\n", orig);
1710                 return NULL;
1711         }
1712
1713         return orig;
1714 }
1715
1716 static char *handle_cli_core_show_file_formats(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1717 {
1718 #define FORMAT "%-10s %-10s %-20s\n"
1719 #define FORMAT2 "%-10s %-10s %-20s\n"
1720         struct ast_format_def *f;
1721         int count_fmt = 0;
1722
1723         switch (cmd) {
1724         case CLI_INIT:
1725                 e->command = "core show file formats";
1726                 e->usage =
1727                         "Usage: core show file formats\n"
1728                         "       Displays currently registered file formats (if any).\n";
1729                 return NULL;
1730         case CLI_GENERATE:
1731                 return NULL;
1732         }
1733
1734         if (a->argc != 4)
1735                 return CLI_SHOWUSAGE;
1736
1737         ast_cli(a->fd, FORMAT, "Format", "Name", "Extensions");
1738         ast_cli(a->fd, FORMAT, "------", "----", "----------");
1739
1740         AST_RWLIST_RDLOCK(&formats);
1741         AST_RWLIST_TRAVERSE(&formats, f, list) {
1742                 ast_cli(a->fd, FORMAT2, ast_format_get_name(f->format), f->name, f->exts);
1743                 count_fmt++;
1744         }
1745         AST_RWLIST_UNLOCK(&formats);
1746         ast_cli(a->fd, "%d file formats registered.\n", count_fmt);
1747         return CLI_SUCCESS;
1748 #undef FORMAT
1749 #undef FORMAT2
1750 }
1751
1752 struct ast_format *ast_get_format_for_file_ext(const char *file_ext)
1753 {
1754         struct ast_format_def *f;
1755         SCOPED_RDLOCK(lock, &formats.lock);
1756         AST_RWLIST_TRAVERSE(&formats, f, list) {
1757                 if (exts_compare(f->exts, file_ext)) {
1758                         return f->format;
1759                 }
1760         }
1761
1762         return NULL;
1763 }
1764
1765 static struct ast_cli_entry cli_file[] = {
1766         AST_CLI_DEFINE(handle_cli_core_show_file_formats, "Displays file formats")
1767 };
1768
1769 static void file_shutdown(void)
1770 {
1771         ast_cli_unregister_multiple(cli_file, ARRAY_LEN(cli_file));
1772         STASIS_MESSAGE_TYPE_CLEANUP(ast_format_register_type);
1773         STASIS_MESSAGE_TYPE_CLEANUP(ast_format_unregister_type);
1774 }
1775
1776 int ast_file_init(void)
1777 {
1778         STASIS_MESSAGE_TYPE_INIT(ast_format_register_type);
1779         STASIS_MESSAGE_TYPE_INIT(ast_format_unregister_type);
1780         ast_cli_register_multiple(cli_file, ARRAY_LEN(cli_file));
1781         ast_register_atexit(file_shutdown);
1782         return 0;
1783 }