chan_pjsip: Update media translation paths when new SDP negotiated.
[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_lock(chan);
781         ast_channel_set_oldwriteformat(chan, ast_channel_writeformat(chan));
782         /* Set the channel to the best format that exists for the file. */
783         res = ast_set_write_format_from_cap(chan, file_fmt_cap);
784         ast_channel_unlock(chan);
785         /* don't need this anymore now that the channel's write format is set. */
786         ao2_ref(file_fmt_cap, -1);
787
788         if (res == -1) {        /* No format available that works with this channel */
789                 return NULL;
790         }
791         res = filehelper(buf, chan, NULL, ACTION_OPEN);
792         if (res >= 0)
793                 return ast_channel_stream(chan);
794         return NULL;
795 }
796
797 struct ast_filestream *ast_openvstream(struct ast_channel *chan, const char *filename, const char *preflang)
798 {
799         /* As above, but for video. But here we don't have translators
800          * so we must enforce a format.
801          */
802         struct ast_format_cap *tmp_cap;
803         char *buf;
804         int buflen;
805         int i, fd;
806
807         if (preflang == NULL) {
808                 preflang = "";
809         }
810         buflen = strlen(preflang) + strlen(filename) + 4;
811         buf = ast_alloca(buflen);
812
813         /* is the channel capable of video without translation ?*/
814         if (!ast_format_cap_has_type(ast_channel_nativeformats(chan), AST_MEDIA_TYPE_VIDEO)) {
815                 return NULL;
816         }
817         if (!(tmp_cap = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT))) {
818                 return NULL;
819         }
820         /* Video is supported, so see what video formats exist for this file */
821         if (!fileexists_core(filename, NULL, preflang, buf, buflen, tmp_cap)) {
822                 ao2_ref(tmp_cap, -1);
823                 return NULL;
824         }
825
826         /* iterate over file formats and pick the first one compatible with the channel's native formats */
827         for (i = 0; i < ast_format_cap_count(tmp_cap); ++i) {
828                 struct ast_format *format = ast_format_cap_get_format(tmp_cap, i);
829
830                 if ((ast_format_get_type(format) != AST_MEDIA_TYPE_VIDEO) ||
831                         !ast_format_cap_iscompatible(ast_channel_nativeformats(chan), tmp_cap)) {
832                         ao2_ref(format, -1);
833                         continue;
834                 }
835
836                 fd = filehelper(buf, chan, ast_format_get_name(format), ACTION_OPEN);
837                 if (fd >= 0) {
838                         ao2_ref(format, -1);
839                         ao2_ref(tmp_cap, -1);
840                         return ast_channel_vstream(chan);
841                 }
842                 ast_log(LOG_WARNING, "File %s has video but couldn't be opened\n", filename);
843                 ao2_ref(format, -1);
844         }
845         ao2_ref(tmp_cap, -1);
846
847         return NULL;
848 }
849
850 static struct ast_frame *read_frame(struct ast_filestream *s, int *whennext)
851 {
852         struct ast_frame *fr, *new_fr;
853
854         if (!s || !s->fmt) {
855                 return NULL;
856         }
857
858         if (!(fr = s->fmt->read(s, whennext))) {
859                 return NULL;
860         }
861
862         if (!(new_fr = ast_frisolate(fr))) {
863                 ast_frfree(fr);
864                 return NULL;
865         }
866
867         if (new_fr != fr) {
868                 ast_frfree(fr);
869                 fr = new_fr;
870         }
871
872         return fr;
873 }
874
875 struct ast_frame *ast_readframe(struct ast_filestream *s)
876 {
877         int whennext = 0;
878
879         return read_frame(s, &whennext);
880 }
881
882 enum fsread_res {
883         FSREAD_FAILURE,
884         FSREAD_SUCCESS_SCHED,
885         FSREAD_SUCCESS_NOSCHED,
886 };
887
888 static int ast_fsread_audio(const void *data);
889
890 static enum fsread_res ast_readaudio_callback(struct ast_filestream *s)
891 {
892         int whennext = 0;
893
894         while (!whennext) {
895                 struct ast_frame *fr;
896
897                 if (s->orig_chan_name && strcasecmp(ast_channel_name(s->owner), s->orig_chan_name)) {
898                         goto return_failure;
899                 }
900
901                 fr = read_frame(s, &whennext);
902
903                 if (!fr /* stream complete */ || ast_write(s->owner, fr) /* error writing */) {
904                         if (fr) {
905                                 ast_log(LOG_WARNING, "Failed to write frame\n");
906                                 ast_frfree(fr);
907                         }
908                         goto return_failure;
909                 }
910
911                 if (fr) {
912                         ast_frfree(fr);
913                 }
914         }
915
916         if (whennext != s->lasttimeout) {
917                 if (ast_channel_timingfd(s->owner) > -1) {
918                         float samp_rate = (float) ast_format_get_sample_rate(s->fmt->format);
919                         unsigned int rate;
920
921                         rate = (unsigned int) roundf(samp_rate / ((float) whennext));
922
923                         ast_settimeout_full(s->owner, rate, ast_fsread_audio, s, 1);
924                 } else {
925                         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));
926                 }
927                 s->lasttimeout = whennext;
928                 return FSREAD_SUCCESS_NOSCHED;
929         }
930         return FSREAD_SUCCESS_SCHED;
931
932 return_failure:
933         ast_channel_streamid_set(s->owner, -1);
934         ast_settimeout(s->owner, 0, NULL, NULL);
935         return FSREAD_FAILURE;
936 }
937
938 static int ast_fsread_audio(const void *data)
939 {
940         struct ast_filestream *fs = (struct ast_filestream *)data;
941         enum fsread_res res;
942
943         res = ast_readaudio_callback(fs);
944
945         if (res == FSREAD_SUCCESS_SCHED)
946                 return 1;
947
948         return 0;
949 }
950
951 static int ast_fsread_video(const void *data);
952
953 static enum fsread_res ast_readvideo_callback(struct ast_filestream *s)
954 {
955         int whennext = 0;
956
957         while (!whennext) {
958                 struct ast_frame *fr = read_frame(s, &whennext);
959
960                 if (!fr /* stream complete */ || ast_write(s->owner, fr) /* error writing */) {
961                         if (fr) {
962                                 ast_log(LOG_WARNING, "Failed to write frame\n");
963                                 ast_frfree(fr);
964                         }
965                         ast_channel_vstreamid_set(s->owner, -1);
966                         return FSREAD_FAILURE;
967                 }
968
969                 if (fr) {
970                         ast_frfree(fr);
971                 }
972         }
973
974         if (whennext != s->lasttimeout) {
975                 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));
976                 s->lasttimeout = whennext;
977                 return FSREAD_SUCCESS_NOSCHED;
978         }
979
980         return FSREAD_SUCCESS_SCHED;
981 }
982
983 static int ast_fsread_video(const void *data)
984 {
985         struct ast_filestream *fs = (struct ast_filestream *)data;
986         enum fsread_res res;
987
988         res = ast_readvideo_callback(fs);
989
990         if (res == FSREAD_SUCCESS_SCHED)
991                 return 1;
992
993         return 0;
994 }
995
996 int ast_applystream(struct ast_channel *chan, struct ast_filestream *s)
997 {
998         s->owner = chan;
999         return 0;
1000 }
1001
1002 int ast_playstream(struct ast_filestream *s)
1003 {
1004         enum fsread_res res;
1005
1006         if (ast_format_get_type(s->fmt->format) == AST_MEDIA_TYPE_AUDIO)
1007                 res = ast_readaudio_callback(s);
1008         else
1009                 res = ast_readvideo_callback(s);
1010
1011         return (res == FSREAD_FAILURE) ? -1 : 0;
1012 }
1013
1014 int ast_seekstream(struct ast_filestream *fs, off_t sample_offset, int whence)
1015 {
1016         return fs->fmt->seek(fs, sample_offset, whence);
1017 }
1018
1019 int ast_truncstream(struct ast_filestream *fs)
1020 {
1021         return fs->fmt->trunc(fs);
1022 }
1023
1024 off_t ast_tellstream(struct ast_filestream *fs)
1025 {
1026         return fs->fmt->tell(fs);
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(64);
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_atexit(file_shutdown);
1784         return 0;
1785 }