Merge "translate: Skip matrix_rebuild during shutdown."
[asterisk/asterisk.git] / main / file.c
index c2ab096..41131f9 100644 (file)
  *
  * \brief Generic File Format Support.
  *
- * \author Mark Spencer <markster@digium.com> 
+ * \author Mark Spencer <markster@digium.com>
  */
 
-#include "asterisk.h"
+/*** MODULEINFO
+       <support_level>core</support_level>
+ ***/
 
-ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
+#include "asterisk.h"
 
 #include <dirent.h>
 #include <sys/stat.h>
+#include <sys/wait.h>
 #include <math.h>
 
 #include "asterisk/_private.h" /* declare ast_file_init() */
@@ -46,6 +49,10 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
 #include "asterisk/module.h"
 #include "asterisk/astobj2.h"
 #include "asterisk/test.h"
+#include "asterisk/stasis.h"
+#include "asterisk/json.h"
+#include "asterisk/stasis_system.h"
+#include "asterisk/media_cache.h"
 
 /*! \brief
  * The following variable controls the layout of localized sound files.
@@ -59,11 +66,64 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  */
 int ast_language_is_prefix = 1;
 
-static AST_RWLIST_HEAD_STATIC(formats, ast_format);
+static AST_RWLIST_HEAD_STATIC(formats, ast_format_def);
+
+STASIS_MESSAGE_TYPE_DEFN(ast_format_register_type);
+STASIS_MESSAGE_TYPE_DEFN(ast_format_unregister_type);
+
+static struct ast_json *json_array_from_list(const char *list, const char *sep)
+{
+       RAII_VAR(struct ast_json *, array, ast_json_array_create(), ast_json_unref);
+       char *stringp, *ext;
+
+       stringp = ast_strdupa(list);    /* this is in the stack so does not need to be freed */
+       if (!array || !stringp) {
+               return NULL;
+       }
+
+       while ((ext = strsep(&stringp, sep))) {
+               if (ast_json_array_append(array, ast_json_string_create(ext))) {
+                       return NULL;
+               }
+       }
+
+       return ast_json_ref(array);
+}
+
+static int publish_format_update(const struct ast_format_def *f, struct stasis_message_type *type)
+{
+       RAII_VAR(struct stasis_message *, msg, NULL, ao2_cleanup);
+       RAII_VAR(struct ast_json_payload *, json_payload, NULL, ao2_cleanup);
+       RAII_VAR(struct ast_json *, json_object, NULL, ast_json_unref);
+
+       if (!type) {
+               return -1;
+       }
+
+       json_object = ast_json_pack("{s: s, s: o}",
+               "format", f->name,
+               "extensions", json_array_from_list(f->exts, "|"));
+       if (!json_object) {
+               return -1;
+       }
+
+       json_payload = ast_json_payload_create(json_object);
+       if (!json_payload) {
+               return -1;
+       }
 
-int __ast_format_register(const struct ast_format *f, struct ast_module *mod)
+       msg = stasis_message_create(type, json_payload);
+       if (!msg) {
+               return -1;
+       }
+
+       stasis_publish(ast_system_topic(), msg);
+       return 0;
+}
+
+int __ast_format_def_register(const struct ast_format_def *f, struct ast_module *mod)
 {
-       struct ast_format *tmp;
+       struct ast_format_def *tmp;
 
        AST_RWLIST_WRLOCK(&formats);
        AST_RWLIST_TRAVERSE(&formats, tmp, list) {
@@ -88,25 +148,27 @@ int __ast_format_register(const struct ast_format *f, struct ast_module *mod)
                int align = (char *)&p.b - (char *)&p.a;
                tmp->buf_size = ((f->buf_size + align - 1) / align) * align;
        }
-       
+
        memset(&tmp->list, 0, sizeof(tmp->list));
 
        AST_RWLIST_INSERT_HEAD(&formats, tmp, list);
        AST_RWLIST_UNLOCK(&formats);
        ast_verb(2, "Registered file format %s, extension(s) %s\n", f->name, f->exts);
+       publish_format_update(f, ast_format_register_type());
 
        return 0;
 }
 
-int ast_format_unregister(const char *name)
+int ast_format_def_unregister(const char *name)
 {
-       struct ast_format *tmp;
+       struct ast_format_def *tmp;
        int res = -1;
 
        AST_RWLIST_WRLOCK(&formats);
        AST_RWLIST_TRAVERSE_SAFE_BEGIN(&formats, tmp, list) {
                if (!strcasecmp(name, tmp->name)) {
                        AST_RWLIST_REMOVE_CURRENT(list);
+                       publish_format_update(tmp, ast_format_unregister_type());
                        ast_free(tmp);
                        res = 0;
                }
@@ -127,16 +189,16 @@ int ast_stopstream(struct ast_channel *tmp)
        ast_channel_lock(tmp);
 
        /* Stop a running stream if there is one */
-       if (tmp->stream) {
-               ast_closestream(tmp->stream);
-               tmp->stream = NULL;
-               if (tmp->oldwriteformat && ast_set_write_format(tmp, tmp->oldwriteformat))
-                       ast_log(LOG_WARNING, "Unable to restore format back to %s\n", ast_getformatname(tmp->oldwriteformat));
+       if (ast_channel_stream(tmp)) {
+               ast_closestream(ast_channel_stream(tmp));
+               ast_channel_stream_set(tmp, NULL);
+               if (ast_channel_oldwriteformat(tmp) && ast_set_write_format(tmp, ast_channel_oldwriteformat(tmp)))
+                       ast_log(LOG_WARNING, "Unable to restore format back to %s\n", ast_format_get_name(ast_channel_oldwriteformat(tmp)));
        }
        /* Stop the video stream too */
-       if (tmp->vstream != NULL) {
-               ast_closestream(tmp->vstream);
-               tmp->vstream = NULL;
+       if (ast_channel_vstream(tmp) != NULL) {
+               ast_closestream(ast_channel_vstream(tmp));
+               ast_channel_vstream_set(tmp, NULL);
        }
 
        ast_channel_unlock(tmp);
@@ -147,48 +209,45 @@ int ast_stopstream(struct ast_channel *tmp)
 int ast_writestream(struct ast_filestream *fs, struct ast_frame *f)
 {
        int res = -1;
-       int alt = 0;
        if (f->frametype == AST_FRAME_VIDEO) {
-               if (fs->fmt->format & AST_FORMAT_AUDIO_MASK) {
+               if (ast_format_get_type(fs->fmt->format) == AST_MEDIA_TYPE_AUDIO) {
                        /* This is the audio portion.  Call the video one... */
                        if (!fs->vfs && fs->filename) {
-                               const char *type = ast_getformatname(f->subclass.codec & ~0x1);
+                               const char *type = ast_format_get_name(f->subclass.format);
                                fs->vfs = ast_writefile(fs->filename, type, NULL, fs->flags, 0, fs->mode);
                                ast_debug(1, "Opened video output file\n");
                        }
                        if (fs->vfs)
                                return ast_writestream(fs->vfs, f);
                        /* else ignore */
-                       return 0;                               
-               } else {
-                       /* Might / might not have mark set */
-                       alt = 1;
+                       return 0;
                }
        } else if (f->frametype != AST_FRAME_VOICE) {
                ast_log(LOG_WARNING, "Tried to write non-voice frame\n");
                return -1;
        }
-       if (((fs->fmt->format | alt) & f->subclass.codec) == f->subclass.codec) {
+       if (ast_format_cmp(f->subclass.format, fs->fmt->format) != AST_FORMAT_CMP_NOT_EQUAL) {
                res =  fs->fmt->write(fs, f);
-               if (res < 0) 
+               if (res < 0)
                        ast_log(LOG_WARNING, "Natural write failed\n");
                else if (res > 0)
                        ast_log(LOG_WARNING, "Huh??\n");
        } else {
                /* XXX If they try to send us a type of frame that isn't the normal frame, and isn't
                       the one we've setup a translator for, we do the "wrong thing" XXX */
-               if (fs->trans && f->subclass.codec != fs->lastwriteformat) {
+               if (fs->trans && (ast_format_cmp(f->subclass.format, fs->lastwriteformat) != AST_FORMAT_CMP_EQUAL)) {
                        ast_translator_free_path(fs->trans);
                        fs->trans = NULL;
                }
-               if (!fs->trans) 
-                       fs->trans = ast_translator_build_path(fs->fmt->format, f->subclass.codec);
-               if (!fs->trans)
+               if (!fs->trans) {
+                       fs->trans = ast_translator_build_path(fs->fmt->format, f->subclass.format);
+               }
+               if (!fs->trans) {
                        ast_log(LOG_WARNING, "Unable to translate to format %s, source format %s\n",
-                               fs->fmt->name, ast_getformatname(f->subclass.codec));
-               else {
+                               fs->fmt->name, ast_format_get_name(f->subclass.format));
+               } else {
                        struct ast_frame *trf;
-                       fs->lastwriteformat = f->subclass.codec;
+                       ao2_replace(fs->lastwriteformat, f->subclass.format);
                        /* Get the translated frame but don't consume the original in case they're using it on another stream */
                        if ((trf = ast_translate(fs->trans, f, 0))) {
                                struct ast_frame *cur;
@@ -260,14 +319,12 @@ static char *build_filename(const char *filename, const char *ext)
                ext = "WAV";
 
        if (filename[0] == '/') {
-               if (asprintf(&fn, "%s.%s", filename, ext) < 0) {
-                       ast_log(LOG_WARNING, "asprintf() failed: %s\n", strerror(errno));
+               if (ast_asprintf(&fn, "%s.%s", filename, ext) < 0) {
                        fn = NULL;
                }
        } else {
-               if (asprintf(&fn, "%s/sounds/%s.%s",
+               if (ast_asprintf(&fn, "%s/sounds/%s.%s",
                             ast_config_AST_DATA_DIR, filename, ext) < 0) {
-                       ast_log(LOG_WARNING, "asprintf() failed: %s\n", strerror(errno));
                        fn = NULL;
                }
        }
@@ -290,58 +347,89 @@ static int exts_compare(const char *exts, const char *type)
        return 0;
 }
 
+/*!
+ * \internal
+ * \brief Close the file stream by canceling any pending read / write callbacks
+ */
+static void filestream_close(struct ast_filestream *f)
+{
+       enum ast_media_type format_type = ast_format_get_type(f->fmt->format);
+
+       if (!f->owner) {
+               return;
+       }
+
+       /* Stop a running stream if there is one */
+       switch (format_type)
+       {
+       case AST_MEDIA_TYPE_AUDIO:
+               ast_channel_stream_set(f->owner, NULL);
+               AST_SCHED_DEL_ACCESSOR(ast_channel_sched(f->owner), f->owner, ast_channel_streamid, ast_channel_streamid_set);
+               ast_settimeout(f->owner, 0, NULL, NULL);
+               break;
+       case AST_MEDIA_TYPE_VIDEO:
+               ast_channel_vstream_set(f->owner, NULL);
+               AST_SCHED_DEL_ACCESSOR(ast_channel_sched(f->owner), f->owner, ast_channel_vstreamid, ast_channel_vstreamid_set);
+               break;
+       default:
+               ast_log(AST_LOG_WARNING, "Unable to schedule deletion of filestream with unsupported type %s\n", f->fmt->name);
+               break;
+       }
+}
+
 static void filestream_destructor(void *arg)
 {
        struct ast_filestream *f = arg;
+       int status;
+       int pid = -1;
 
        /* Stop a running stream if there is one */
-       if (f->owner) {
-               if (f->fmt->format < AST_FORMAT_AUDIO_MASK) {
-                       f->owner->stream = NULL;
-                       AST_SCHED_DEL(f->owner->sched, f->owner->streamid);
-                       ast_settimeout(f->owner, 0, NULL, NULL);
-               } else {
-                       f->owner->vstream = NULL;
-                       AST_SCHED_DEL(f->owner->sched, f->owner->vstreamid);
-               }
-       }
+       filestream_close(f);
+
        /* destroy the translator on exit */
        if (f->trans)
                ast_translator_free_path(f->trans);
 
-       if (f->realfilename && f->filename) {
-               if (ast_safe_fork(0) == 0) {
-                       execl("/bin/mv", "mv", "-f", f->filename, f->realfilename, SENTINEL);
-               }
-       }
-
-       if (f->filename)
-               free(f->filename);
-       if (f->realfilename)
-               free(f->realfilename);
        if (f->fmt->close) {
                void (*closefn)(struct ast_filestream *) = f->fmt->close;
                closefn(f);
        }
-       if (f->f)
+
+       if (f->f) {
                fclose(f->f);
+       }
+
+       if (f->realfilename && f->filename) {
+               pid = ast_safe_fork(0);
+               if (!pid) {
+                       execl("/bin/mv", "mv", "-f", f->filename, f->realfilename, SENTINEL);
+                       _exit(1);
+               }
+               else if (pid > 0) {
+                       /* Block the parent until the move is complete.*/
+                       waitpid(pid, &status, 0);
+               }
+       }
+
+       ast_free(f->filename);
+       ast_free(f->realfilename);
        if (f->vfs)
                ast_closestream(f->vfs);
-       if (f->write_buffer) {
-               ast_free(f->write_buffer);
-       }
-       if (f->orig_chan_name)
-               free((void *) f->orig_chan_name);
+       ast_free(f->write_buffer);
+       ast_free((void *)f->orig_chan_name);
+       ao2_cleanup(f->lastwriteformat);
+       ao2_cleanup(f->fr.subclass.format);
        ast_module_unref(f->fmt->module);
 }
 
-static struct ast_filestream *get_filestream(struct ast_format *fmt, FILE *bfile)
+static struct ast_filestream *get_filestream(struct ast_format_def *fmt, FILE *bfile)
 {
        struct ast_filestream *s;
 
        int l = sizeof(*s) + fmt->buf_size + fmt->desc_size;    /* total allocation size */
        if ( (s = ao2_alloc(l, filestream_destructor)) == NULL)
                return NULL;
+       ast_module_ref(fmt->module);
        s->fmt = fmt;
        s->f = bfile;
 
@@ -350,6 +438,15 @@ static struct ast_filestream *get_filestream(struct ast_format *fmt, FILE *bfile
        if (fmt->buf_size)
                s->buf = (char *)(s + 1);
        s->fr.src = fmt->name;
+
+       if (ast_format_get_type(fmt->format) == AST_MEDIA_TYPE_AUDIO) {
+               s->fr.frametype = AST_FRAME_VOICE;
+       } else if (ast_format_get_type(fmt->format) == AST_MEDIA_TYPE_VIDEO) {
+               s->fr.frametype = AST_FRAME_VIDEO;
+       }
+       s->fr.mallocd = 0;
+       s->fr.subclass.format = ao2_bump(fmt->format);
+
        return s;
 }
 
@@ -361,7 +458,7 @@ enum wrap_fn { WRAP_OPEN, WRAP_REWRITE };
 
 static int fn_wrapper(struct ast_filestream *s, const char *comment, enum wrap_fn mode)
 {
-       struct ast_format *f = s->fmt;
+       struct ast_format_def *f = s->fmt;
        int ret = -1;
        int (*openfn)(struct ast_filestream *s);
 
@@ -370,8 +467,7 @@ static int fn_wrapper(struct ast_filestream *s, const char *comment, enum wrap_f
        else if (mode == WRAP_REWRITE && f->rewrite && f->rewrite(s, comment))
                ast_log(LOG_WARNING, "Unable to rewrite format %s\n", f->name);
        else {
-               /* preliminary checks succeed. update usecount */
-               ast_module_ref(f->module);
+               /* preliminary checks succeed. */
                ret = 0;
        }
        return ret;
@@ -396,18 +492,20 @@ enum file_action {
 };
 
 /*!
+ * \internal
  * \brief perform various actions on a file. Second argument
- * arg2 depends on the command:
- *     unused for EXISTS and DELETE
+ * \note arg2 depends on the command:
+ *     unused for DELETE
+ *  optional ast_format_cap holding all the formats found for a file, for EXISTS.
  *     destination file name (const char *) for COPY and RENAME
- *     struct ast_channel * for OPEN
+ *     struct ast_channel * for OPEN
  * if fmt is NULL, OPEN will return the first matching entry,
  * whereas other functions will run on all matching entries.
  */
-static format_t ast_filehelper(const char *filename, const void *arg2, const char *fmt, const enum file_action action)
+static int filehelper(const char *filename, const void *arg2, const char *fmt, const enum file_action action)
 {
-       struct ast_format *f;
-       format_t res = (action == ACTION_EXISTS) ? 0 : -1;
+       struct ast_format_def *f;
+       int res = (action == ACTION_EXISTS) ? 0 : -1;
 
        AST_RWLIST_RDLOCK(&formats);
        /* Check for a specific format */
@@ -441,9 +539,9 @@ static format_t ast_filehelper(const char *filename, const void *arg2, const cha
                                FILE *bfile;
                                struct ast_filestream *s;
 
-                               if ( !(chan->writeformat & f->format) &&
-                                    !((f->format & AST_FORMAT_AUDIO_MASK && fmt) ||
-                                         (f->format & AST_FORMAT_VIDEO_MASK && fmt))) {
+                               if ((ast_format_cmp(ast_channel_writeformat(chan), f->format) == AST_FORMAT_CMP_NOT_EQUAL) &&
+                                    !(((ast_format_get_type(f->format) == AST_MEDIA_TYPE_AUDIO) && fmt) ||
+                                         ((ast_format_get_type(f->format) == AST_MEDIA_TYPE_VIDEO) && fmt))) {
                                        ast_free(fn);
                                        continue;       /* not a supported format */
                                }
@@ -471,14 +569,14 @@ static format_t ast_filehelper(const char *filename, const void *arg2, const cha
                                s->fmt = f;
                                s->trans = NULL;
                                s->filename = NULL;
-                               if (s->fmt->format & AST_FORMAT_AUDIO_MASK) {
-                                       if (chan->stream)
-                                               ast_closestream(chan->stream);
-                                       chan->stream = s;
+                               if (ast_format_get_type(s->fmt->format) == AST_MEDIA_TYPE_AUDIO) {
+                                       if (ast_channel_stream(chan))
+                                               ast_closestream(ast_channel_stream(chan));
+                                       ast_channel_stream_set(chan, s);
                                } else {
-                                       if (chan->vstream)
-                                               ast_closestream(chan->vstream);
-                                       chan->vstream = s;
+                                       if (ast_channel_vstream(chan))
+                                               ast_closestream(ast_channel_vstream(chan));
+                                       ast_channel_vstream_set(chan, s);
                                }
                                ast_free(fn);
                                break;
@@ -488,7 +586,12 @@ static format_t ast_filehelper(const char *filename, const void *arg2, const cha
                                break;  /* will never get here */
 
                        case ACTION_EXISTS:     /* return the matching format */
-                               res |= f->format;
+                               /* if arg2 is present, it is a format capabilities structure.
+                                * Add this format to the set of formats this file can be played in */
+                               if (arg2) {
+                                       ast_format_cap_append((struct ast_format_cap *) arg2, f->format, 0);
+                               }
+                               res = 1; /* file does exist and format it exists in is returned in arg2 */
                                break;
 
                        case ACTION_DELETE:
@@ -513,7 +616,7 @@ static format_t ast_filehelper(const char *filename, const void *arg2, const cha
                                break;
 
                        default:
-                               ast_log(LOG_WARNING, "Unknown helper %d\n", action);
+                               ast_log(LOG_WARNING, "Unknown helper %u\n", action);
                        }
                        ast_free(fn);
                }
@@ -527,11 +630,21 @@ static int is_absolute_path(const char *filename)
        return filename[0] == '/';
 }
 
-static format_t fileexists_test(const char *filename, const char *fmt, const char *lang,
-                          char *buf, int buflen)
+/*!
+ * \brief test if a file exists for a given format.
+ * \note result_cap is OPTIONAL
+ * \retval 1, true and result_cap represents format capabilities file exists in.
+ * \retval 0, false
+ */
+static int fileexists_test(const char *filename, const char *fmt, const char *lang,
+                          char *buf, int buflen, struct ast_format_cap *result_cap)
 {
        if (buf == NULL) {
-               return -1;
+               return 0;
+       }
+
+       if (!ast_media_cache_retrieve(filename, NULL, buf, buflen)) {
+               return filehelper(buf, result_cap, NULL, ACTION_EXISTS);
        }
 
        if (ast_language_is_prefix && !is_absolute_path(filename)) { /* new layout */
@@ -550,25 +663,36 @@ static format_t fileexists_test(const char *filename, const char *fmt, const cha
                }
        }
 
-       return ast_filehelper(buf, NULL, fmt, ACTION_EXISTS);
+       return filehelper(buf, result_cap, fmt, ACTION_EXISTS);
 }
 
 /*!
  * \brief helper routine to locate a file with a given format
  * and language preference.
- * Try preflang, preflang with stripped '_' suffices, or NULL.
  *
- * The last parameter(s) point to a buffer of sufficient size,
+ * \note Try preflang, preflang with stripped '_' suffices, or NULL.
+ *
+ * \note The last parameter(s) point to a buffer of sufficient size,
  * which on success is filled with the matching filename.
+ *
+ * \param filename Name of the file.
+ * \param fmt Format to look for the file in. OPTIONAL
+ * \param preflang The perfered language
+ * \param buf Returns the matching filename
+ * \param buflen Size of the buf
+ * \param result_cap OPTIONAL format capabilities result structure
+ *        returns what formats the file was found in.
+ *
+ * \retval 1, true. file exists and result format is set
+ * \retval 0, false. file does not exist.
  */
-static format_t fileexists_core(const char *filename, const char *fmt, const char *preflang,
-                          char *buf, int buflen)
+static int fileexists_core(const char *filename, const char *fmt, const char *preflang,
+                          char *buf, int buflen, struct ast_format_cap *result_cap)
 {
-       format_t res = -1;
        char *lang;
 
        if (buf == NULL) {
-               return -1;
+               return 0;
        }
 
        /* We try languages in the following order:
@@ -584,8 +708,8 @@ static format_t fileexists_core(const char *filename, const char *fmt, const cha
        while (!ast_strlen_zero(lang)) {
                char *end;
 
-               if ((res = fileexists_test(filename, fmt, lang, buf, buflen)) > 0) {
-                       return res;
+               if (fileexists_test(filename, fmt, lang, buf, buflen, result_cap)) {
+                       return 1;
                }
 
                if ((end = strrchr(lang, '_')) != NULL) {
@@ -597,14 +721,14 @@ static format_t fileexists_core(const char *filename, const char *fmt, const cha
        }
 
        /* Try without any language */
-       if ((res = fileexists_test(filename, fmt, NULL, buf, buflen)) > 0) {
-               return res;
+       if (fileexists_test(filename, fmt, NULL, buf, buflen, result_cap)) {
+               return 1;
        }
 
        /* Finally try the default language unless it was already tried before */
        if ((ast_strlen_zero(preflang) || strcmp(preflang, DEFAULT_LANGUAGE)) && (ast_strlen_zero(lang) || strcmp(lang, DEFAULT_LANGUAGE))) {
-               if ((res = fileexists_test(filename, fmt, DEFAULT_LANGUAGE, buf, buflen)) > 0) {
-                       return res;
+               if ((fileexists_test(filename, fmt, DEFAULT_LANGUAGE, buf, buflen, result_cap)) > 0) {
+                       return 1;
                }
        }
 
@@ -618,43 +742,53 @@ struct ast_filestream *ast_openstream(struct ast_channel *chan, const char *file
 
 struct ast_filestream *ast_openstream_full(struct ast_channel *chan, const char *filename, const char *preflang, int asis)
 {
-       /* 
+       /*
         * Use fileexists_core() to find a file in a compatible
         * language and format, set up a suitable translator,
         * and open the stream.
         */
-       format_t fmts, res;
+       struct ast_format_cap *file_fmt_cap;
+       int res;
        int buflen;
        char *buf;
 
        if (!asis) {
                /* do this first, otherwise we detect the wrong writeformat */
                ast_stopstream(chan);
-               if (chan->generator)
+               if (ast_channel_generator(chan))
                        ast_deactivate_generator(chan);
        }
        if (preflang == NULL)
                preflang = "";
        buflen = strlen(preflang) + strlen(filename) + 4;
-       buf = alloca(buflen);
-       if (buf == NULL)
+       buf = ast_alloca(buflen);
+
+       if (!(file_fmt_cap = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT))) {
                return NULL;
-       fmts = fileexists_core(filename, NULL, preflang, buf, buflen);
-       if (fmts > 0)
-               fmts &= AST_FORMAT_AUDIO_MASK;
-       if (fmts < 1) {
+       }
+       if (!fileexists_core(filename, NULL, preflang, buf, buflen, file_fmt_cap) ||
+               !ast_format_cap_has_type(file_fmt_cap, AST_MEDIA_TYPE_AUDIO)) {
+
                ast_log(LOG_WARNING, "File %s does not exist in any format\n", filename);
+               ao2_ref(file_fmt_cap, -1);
                return NULL;
        }
-       chan->oldwriteformat = chan->writeformat;
-       /* Set the channel to a format we can work with */
-       res = ast_set_write_format(chan, fmts);
+
+       /* Set the channel to a format we can work with and save off the previous format. */
+       ast_channel_lock(chan);
+       ast_channel_set_oldwriteformat(chan, ast_channel_writeformat(chan));
+       /* Set the channel to the best format that exists for the file. */
+       res = ast_set_write_format_from_cap(chan, file_fmt_cap);
+       ast_channel_unlock(chan);
+       /* don't need this anymore now that the channel's write format is set. */
+       ao2_ref(file_fmt_cap, -1);
+
        if (res == -1) {        /* No format available that works with this channel */
                return NULL;
        }
-       res = ast_filehelper(buf, chan, NULL, ACTION_OPEN);
+       res = filehelper(buf, chan, NULL, ACTION_OPEN);
        if (res >= 0)
-               return chan->stream;
+               return ast_channel_stream(chan);
        return NULL;
 }
 
@@ -663,31 +797,60 @@ struct ast_filestream *ast_openvstream(struct ast_channel *chan, const char *fil
        /* As above, but for video. But here we don't have translators
         * so we must enforce a format.
         */
-       format_t format;
+       struct ast_format_cap *nativeformats, *tmp_cap;
        char *buf;
        int buflen;
+       int i, fd;
 
-       if (preflang == NULL)
+       if (preflang == NULL) {
                preflang = "";
+       }
        buflen = strlen(preflang) + strlen(filename) + 4;
-       buf = alloca(buflen);
-       if (buf == NULL)
+       buf = ast_alloca(buflen);
+
+       ast_channel_lock(chan);
+       nativeformats = ao2_bump(ast_channel_nativeformats(chan));
+       ast_channel_unlock(chan);
+
+       /* is the channel capable of video without translation ?*/
+       if (!ast_format_cap_has_type(nativeformats, AST_MEDIA_TYPE_VIDEO)) {
+               ao2_cleanup(nativeformats);
+               return NULL;
+       }
+       if (!(tmp_cap = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT))) {
+               ao2_cleanup(nativeformats);
                return NULL;
+       }
+       /* Video is supported, so see what video formats exist for this file */
+       if (!fileexists_core(filename, NULL, preflang, buf, buflen, tmp_cap)) {
+               ao2_ref(tmp_cap, -1);
+               ao2_cleanup(nativeformats);
+               return NULL;
+       }
 
-       for (format = AST_FORMAT_AUDIO_MASK + 1; format <= AST_FORMAT_VIDEO_MASK; format = format << 1) {
-               int fd;
-               const char *fmt;
+       /* iterate over file formats and pick the first one compatible with the channel's native formats */
+       for (i = 0; i < ast_format_cap_count(tmp_cap); ++i) {
+               struct ast_format *format = ast_format_cap_get_format(tmp_cap, i);
 
-               if (!(chan->nativeformats & format))
-                       continue;
-               fmt = ast_getformatname(format);
-               if ( fileexists_core(filename, fmt, preflang, buf, buflen) < 1) /* no valid format */
+               if ((ast_format_get_type(format) != AST_MEDIA_TYPE_VIDEO) ||
+                       !ast_format_cap_iscompatible(nativeformats, tmp_cap)) {
+                       ao2_ref(format, -1);
                        continue;
-               fd = ast_filehelper(buf, chan, fmt, ACTION_OPEN);
-               if (fd >= 0)
-                       return chan->vstream;
+               }
+
+               fd = filehelper(buf, chan, ast_format_get_name(format), ACTION_OPEN);
+               if (fd >= 0) {
+                       ao2_ref(format, -1);
+                       ao2_ref(tmp_cap, -1);
+                       ao2_cleanup(nativeformats);
+                       return ast_channel_vstream(chan);
+               }
                ast_log(LOG_WARNING, "File %s has video but couldn't be opened\n", filename);
+               ao2_ref(format, -1);
        }
+       ao2_ref(tmp_cap, -1);
+       ao2_cleanup(nativeformats);
+
        return NULL;
 }
 
@@ -738,7 +901,7 @@ static enum fsread_res ast_readaudio_callback(struct ast_filestream *s)
        while (!whennext) {
                struct ast_frame *fr;
 
-               if (s->orig_chan_name && strcasecmp(s->owner->name, s->orig_chan_name)) {
+               if (s->orig_chan_name && strcasecmp(ast_channel_name(s->owner), s->orig_chan_name)) {
                        goto return_failure;
                }
 
@@ -746,11 +909,11 @@ static enum fsread_res ast_readaudio_callback(struct ast_filestream *s)
 
                if (!fr /* stream complete */ || ast_write(s->owner, fr) /* error writing */) {
                        if (fr) {
-                               ast_log(LOG_WARNING, "Failed to write frame\n");
+                               ast_debug(2, "Failed to write frame\n");
                                ast_frfree(fr);
                        }
                        goto return_failure;
-               } 
+               }
 
                if (fr) {
                        ast_frfree(fr);
@@ -758,16 +921,15 @@ static enum fsread_res ast_readaudio_callback(struct ast_filestream *s)
        }
 
        if (whennext != s->lasttimeout) {
-               if (s->owner->timingfd > -1) {
-                       float samp_rate = (float) ast_format_rate(s->fmt->format);
+               if (ast_channel_timingfd(s->owner) > -1) {
+                       float samp_rate = (float) ast_format_get_sample_rate(s->fmt->format);
                        unsigned int rate;
 
                        rate = (unsigned int) roundf(samp_rate / ((float) whennext));
 
-                       ast_settimeout(s->owner, rate, ast_fsread_audio, s);
+                       ast_settimeout_full(s->owner, rate, ast_fsread_audio, s, 1);
                } else {
-                       s->owner->streamid = ast_sched_add(s->owner->sched, 
-                               whennext / (ast_format_rate(s->fmt->format) / 1000), ast_fsread_audio, s);
+                       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));
                }
                s->lasttimeout = whennext;
                return FSREAD_SUCCESS_NOSCHED;
@@ -775,7 +937,7 @@ static enum fsread_res ast_readaudio_callback(struct ast_filestream *s)
        return FSREAD_SUCCESS_SCHED;
 
 return_failure:
-       s->owner->streamid = -1;
+       ast_channel_streamid_set(s->owner, -1);
        ast_settimeout(s->owner, 0, NULL, NULL);
        return FSREAD_FAILURE;
 }
@@ -789,7 +951,7 @@ static int ast_fsread_audio(const void *data)
 
        if (res == FSREAD_SUCCESS_SCHED)
                return 1;
-       
+
        return 0;
 }
 
@@ -804,10 +966,10 @@ static enum fsread_res ast_readvideo_callback(struct ast_filestream *s)
 
                if (!fr /* stream complete */ || ast_write(s->owner, fr) /* error writing */) {
                        if (fr) {
-                               ast_log(LOG_WARNING, "Failed to write frame\n");
+                               ast_debug(2, "Failed to write frame\n");
                                ast_frfree(fr);
                        }
-                       s->owner->vstreamid = -1;
+                       ast_channel_vstreamid_set(s->owner, -1);
                        return FSREAD_FAILURE;
                }
 
@@ -817,9 +979,7 @@ static enum fsread_res ast_readvideo_callback(struct ast_filestream *s)
        }
 
        if (whennext != s->lasttimeout) {
-               s->owner->vstreamid = ast_sched_add(s->owner->sched, 
-                       whennext / (ast_format_rate(s->fmt->format) / 1000), 
-                       ast_fsread_video, s);
+               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));
                s->lasttimeout = whennext;
                return FSREAD_SUCCESS_NOSCHED;
        }
@@ -836,7 +996,7 @@ static int ast_fsread_video(const void *data)
 
        if (res == FSREAD_SUCCESS_SCHED)
                return 1;
-       
+
        return 0;
 }
 
@@ -850,7 +1010,7 @@ int ast_playstream(struct ast_filestream *s)
 {
        enum fsread_res res;
 
-       if (s->fmt->format & AST_FORMAT_AUDIO_MASK)
+       if (ast_format_get_type(s->fmt->format) == AST_MEDIA_TYPE_AUDIO)
                res = ast_readaudio_callback(s);
        else
                res = ast_readvideo_callback(s);
@@ -873,6 +1033,11 @@ off_t ast_tellstream(struct ast_filestream *fs)
        return fs->fmt->tell(fs);
 }
 
+int ast_ratestream(struct ast_filestream *fs)
+{
+       return ast_format_get_sample_rate(fs->fmt->format);
+}
+
 int ast_stream_fastforward(struct ast_filestream *fs, off_t ms)
 {
        return ast_seekstream(fs, ms * DEFAULT_SAMPLES_PER_MS, SEEK_CUR);
@@ -886,22 +1051,13 @@ int ast_stream_rewind(struct ast_filestream *fs, off_t ms)
 int ast_closestream(struct ast_filestream *f)
 {
        /* This used to destroy the filestream, but it now just decrements a refcount.
-        * We need to force the stream to quit queuing frames now, because we might
+        * We close the stream in order to quit queuing frames now, because we might
         * change the writeformat, which could result in a subsequent write error, if
         * the format is different. */
-
-       /* Stop a running stream if there is one */
-       if (f->owner) {
-               if (f->fmt->format < AST_FORMAT_AUDIO_MASK) {
-                       f->owner->stream = NULL;
-                       AST_SCHED_DEL(f->owner->sched, f->owner->streamid);
-                       ast_settimeout(f->owner, 0, NULL, NULL);
-               } else {
-                       f->owner->vstream = NULL;
-                       AST_SCHED_DEL(f->owner->sched, f->owner->vstreamid);
-               }
+       if (f == NULL) {
+               return 0;
        }
-
+       filestream_close(f);
        ao2_ref(f, -1);
        return 0;
 }
@@ -918,67 +1074,217 @@ int ast_fileexists(const char *filename, const char *fmt, const char *preflang)
        if (preflang == NULL)
                preflang = "";
        buflen = strlen(preflang) + strlen(filename) + 4;       /* room for everything */
-       buf = alloca(buflen);
-       if (buf == NULL)
-               return 0;
-       return fileexists_core(filename, fmt, preflang, buf, buflen);
+       buf = ast_alloca(buflen);
+       return fileexists_core(filename, fmt, preflang, buf, buflen, NULL) ? 1 : 0;
 }
 
 int ast_filedelete(const char *filename, const char *fmt)
 {
-       return ast_filehelper(filename, NULL, fmt, ACTION_DELETE);
+       return filehelper(filename, NULL, fmt, ACTION_DELETE);
 }
 
 int ast_filerename(const char *filename, const char *filename2, const char *fmt)
 {
-       return ast_filehelper(filename, filename2, fmt, ACTION_RENAME);
+       return filehelper(filename, filename2, fmt, ACTION_RENAME);
 }
 
 int ast_filecopy(const char *filename, const char *filename2, const char *fmt)
 {
-       return ast_filehelper(filename, filename2, fmt, ACTION_COPY);
+       return filehelper(filename, filename2, fmt, ACTION_COPY);
+}
+
+static int __ast_file_read_dirs(const char *path, ast_file_on_file on_file,
+                               void *obj, int max_depth)
+{
+       DIR *dir;
+       struct dirent *entry;
+       int res;
+
+       if (!(dir = opendir(path))) {
+               ast_log(LOG_ERROR, "Error opening directory - %s: %s\n",
+                       path, strerror(errno));
+               return -1;
+       }
+
+       --max_depth;
+
+       res = 0;
+
+       while ((entry = readdir(dir)) != NULL && !errno) {
+               int is_file = 0;
+               int is_dir = 0;
+               RAII_VAR(char *, full_path, NULL, ast_free);
+
+               if (!strcmp(entry->d_name, ".") || !strcmp(entry->d_name, "..")) {
+                       continue;
+               }
+
+/*
+ * If the dirent structure has a d_type use it to determine if we are dealing with
+ * a file or directory. Unfortunately if it doesn't have it, or if the type is
+ * unknown, or a link then we'll need to use the stat function instead.
+ */
+#ifdef _DIRENT_HAVE_D_TYPE
+               if (entry->d_type != DT_UNKNOWN && entry->d_type != DT_LNK) {
+                       is_file = entry->d_type == DT_REG;
+                       is_dir = entry->d_type == DT_DIR;
+               } else
+#endif
+               {
+                       struct stat statbuf;
+
+                       /*
+                        * Don't use alloca or we risk blowing out the stack if recursing
+                        * into subdirectories.
+                        */
+                       full_path = ast_malloc(strlen(path) + strlen(entry->d_name) + 2);
+                       if (!full_path) {
+                               return -1;
+                       }
+                       sprintf(full_path, "%s/%s", path, entry->d_name);
+
+                       if (stat(full_path, &statbuf)) {
+                               ast_log(LOG_ERROR, "Error reading path stats - %s: %s\n",
+                                       full_path, strerror(errno));
+                               /*
+                                * Output an error, but keep going. It could just be
+                                * a broken link and other files could be fine.
+                                */
+                               continue;
+                       }
+
+                       is_file = S_ISREG(statbuf.st_mode);
+                       is_dir = S_ISDIR(statbuf.st_mode);
+               }
+
+               if (is_file) {
+                       /* If the handler returns non-zero then stop */
+                       if ((res = on_file(path, entry->d_name, obj))) {
+                               break;
+                       }
+                       /* Otherwise move on to next item in directory */
+                       continue;
+               }
+
+               if (!is_dir) {
+                       ast_debug(5, "Skipping %s: not a regular file or directory\n", full_path);
+                       continue;
+               }
+
+               /* Only re-curse into sub-directories if not at the max depth */
+               if (max_depth != 0) {
+                       if (!full_path) {
+                               /* Don't use alloca.  See note above. */
+                               full_path = ast_malloc(strlen(path) + strlen(entry->d_name) + 2);
+                               if (!full_path) {
+                                       return -1;
+                               }
+                               sprintf(full_path, "%s/%s", path, entry->d_name);
+                       }
+
+                       if ((res = __ast_file_read_dirs(full_path, on_file, obj, max_depth))) {
+                               break;
+                       }
+               }
+       }
+
+       closedir(dir);
+
+       if (!res && errno) {
+               ast_log(LOG_ERROR, "Error while reading directories - %s: %s\n",
+                       path, strerror(errno));
+               res = -1;
+       }
+
+       return res;
+}
+
+#if !defined(__GLIBC__)
+/*!
+ * \brief Lock to hold when iterating over directories.
+ *
+ * Currently, 'readdir' is not required to be thread-safe. In most modern implementations
+ * it should be safe to make concurrent calls into 'readdir' that specify different directory
+ * streams (glibc would be one of these). However, since it is potentially unsafe for some
+ * implementations we'll use our own locking in order to achieve synchronization for those.
+ */
+AST_MUTEX_DEFINE_STATIC(read_dirs_lock);
+#endif
+
+int ast_file_read_dirs(const char *dir_name, ast_file_on_file on_file, void *obj, int max_depth)
+{
+       int res;
+
+       errno = 0;
+
+#if !defined(__GLIBC__)
+       ast_mutex_lock(&read_dirs_lock);
+#endif
+
+       res = __ast_file_read_dirs(dir_name, on_file, obj, max_depth);
+
+#if !defined(__GLIBC__)
+       ast_mutex_unlock(&read_dirs_lock);
+#endif
+
+       return res;
 }
 
 int ast_streamfile(struct ast_channel *chan, const char *filename, const char *preflang)
 {
        struct ast_filestream *fs;
        struct ast_filestream *vfs=NULL;
-       char fmt[256];
+       off_t pos;
        int seekattempt;
        int res;
 
        fs = ast_openstream(chan, filename, preflang);
        if (!fs) {
-               ast_log(LOG_WARNING, "Unable to open %s (format %s): %s\n", filename, ast_getformatname_multiple(fmt, sizeof(fmt), chan->nativeformats), strerror(errno));
+               struct ast_str *codec_buf = ast_str_alloca(AST_FORMAT_CAP_NAMES_LEN);
+               ast_channel_lock(chan);
+               ast_log(LOG_WARNING, "Unable to open %s (format %s): %s\n",
+                       filename, ast_format_cap_get_names(ast_channel_nativeformats(chan), &codec_buf), strerror(errno));
+               ast_channel_unlock(chan);
                return -1;
        }
 
        /* check to see if there is any data present (not a zero length file),
         * done this way because there is no where for ast_openstream_full to
         * return the file had no data. */
-       seekattempt = fseek(fs->f, -1, SEEK_END);
-       if (seekattempt && errno == EINVAL) {
-               /* Zero-length file, as opposed to a pipe */
-               return 0;
+       pos = ftello(fs->f);
+       seekattempt = fseeko(fs->f, -1, SEEK_END);
+       if (seekattempt) {
+               if (errno == EINVAL) {
+                       /* Zero-length file, as opposed to a pipe */
+                       return 0;
+               } else {
+                       ast_seekstream(fs, 0, SEEK_SET);
+               }
        } else {
-               ast_seekstream(fs, 0, SEEK_SET);
+               fseeko(fs->f, pos, SEEK_SET);
        }
 
        vfs = ast_openvstream(chan, filename, preflang);
        if (vfs) {
-               ast_debug(1, "Ooh, found a video stream, too, format %s\n", ast_getformatname(vfs->fmt->format));
+               ast_debug(1, "Ooh, found a video stream, too, format %s\n", ast_format_get_name(vfs->fmt->format));
        }
 
-       if (ast_test_flag(chan, AST_FLAG_MASQ_NOSTREAM))
-               fs->orig_chan_name = ast_strdup(chan->name);
+       if (ast_test_flag(ast_channel_flags(chan), AST_FLAG_MASQ_NOSTREAM))
+               fs->orig_chan_name = ast_strdup(ast_channel_name(chan));
        if (ast_applystream(chan, fs))
                return -1;
        if (vfs && ast_applystream(chan, vfs))
                return -1;
+       ast_test_suite_event_notify("PLAYBACK", "Message: %s\r\nChannel: %s", filename, ast_channel_name(chan));
        res = ast_playstream(fs);
        if (!res && vfs)
                res = ast_playstream(vfs);
-       ast_verb(3, "<%s> Playing '%s.%s' (language '%s')\n", chan->name, filename, ast_getformatname(chan->writeformat), preflang ? preflang : "default");
+
+       if (VERBOSITY_ATLEAST(3)) {
+               ast_channel_lock(chan);
+               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");
+               ast_channel_unlock(chan);
+       }
 
        return res;
 }
@@ -986,10 +1292,10 @@ int ast_streamfile(struct ast_channel *chan, const char *filename, const char *p
 struct ast_filestream *ast_readfile(const char *filename, const char *type, const char *comment, int flags, int check, mode_t mode)
 {
        FILE *bfile;
-       struct ast_format *f;
+       struct ast_format_def *f;
        struct ast_filestream *fs = NULL;
        char *fn;
-       int format_found = 0;   
+       int format_found = 0;
 
        AST_RWLIST_RDLOCK(&formats);
 
@@ -997,10 +1303,13 @@ struct ast_filestream *ast_readfile(const char *filename, const char *type, cons
                fs = NULL;
                if (!exts_compare(f->exts, type))
                        continue;
-               else 
+               else
                        format_found = 1;
 
                fn = build_filename(filename, type);
+               if (!fn) {
+                       continue;
+               }
                errno = 0;
                bfile = fopen(fn, "r");
 
@@ -1012,7 +1321,7 @@ struct ast_filestream *ast_readfile(const char *filename, const char *type, cons
                        fs = NULL;
                        bfile = NULL;
                        ast_free(fn);
-                       break;                          
+                       break;
                }
                /* found it */
                fs->trans = NULL;
@@ -1021,6 +1330,7 @@ struct ast_filestream *ast_readfile(const char *filename, const char *type, cons
                fs->mode = mode;
                fs->filename = ast_strdup(filename);
                fs->vfs = NULL;
+               ast_free(fn);
                break;
        }
 
@@ -1036,7 +1346,7 @@ struct ast_filestream *ast_writefile(const char *filename, const char *type, con
        int fd, myflags = 0;
        /* compiler claims this variable can be used before initialization... */
        FILE *bfile = NULL;
-       struct ast_format *f;
+       struct ast_format_def *f;
        struct ast_filestream *fs = NULL;
        char *buf = NULL;
        size_t size = 0;
@@ -1046,12 +1356,12 @@ struct ast_filestream *ast_writefile(const char *filename, const char *type, con
 
        /* set the O_TRUNC flag if and only if there is no O_APPEND specified */
        /* We really can't use O_APPEND as it will break WAV header updates */
-       if (flags & O_APPEND) { 
+       if (flags & O_APPEND) {
                flags &= ~O_APPEND;
        } else {
                myflags = O_TRUNC;
        }
-       
+
        myflags |= O_WRONLY | O_CREAT;
 
        /* XXX need to fix this - we should just do the fopen,
@@ -1068,6 +1378,9 @@ struct ast_filestream *ast_writefile(const char *filename, const char *type, con
                        format_found = 1;
 
                fn = build_filename(filename, type);
+               if (!fn) {
+                       continue;
+               }
                fd = open(fn, flags | myflags, mode);
                if (fd > -1) {
                        /* fdopen() the resulting file stream */
@@ -1078,7 +1391,7 @@ struct ast_filestream *ast_writefile(const char *filename, const char *type, con
                                fd = -1;
                        }
                }
-               
+
                if (ast_opt_cache_record_files && (fd > -1)) {
                        char *c;
 
@@ -1093,7 +1406,7 @@ struct ast_filestream *ast_writefile(const char *filename, const char *type, con
                                        *c = '_';
 
                        size = strlen(fn) + strlen(record_cache_dir) + 2;
-                       buf = alloca(size);
+                       buf = ast_alloca(size);
                        strcpy(buf, record_cache_dir);
                        strcat(buf, "/");
                        strcat(buf, fn);
@@ -1129,6 +1442,9 @@ struct ast_filestream *ast_writefile(const char *filename, const char *type, con
                                        ast_closestream(fs);
                                        fs = NULL;
                                }
+                               if (!buf) {
+                                       ast_free(fn);
+                               }
                                continue;
                        }
                        fs->trans = NULL;
@@ -1163,14 +1479,60 @@ struct ast_filestream *ast_writefile(const char *filename, const char *type, con
        return fs;
 }
 
+static void waitstream_control(struct ast_channel *c,
+               enum ast_waitstream_fr_cb_values type,
+               ast_waitstream_fr_cb cb,
+               int skip_ms)
+{
+       switch (type)
+       {
+       case AST_WAITSTREAM_CB_FASTFORWARD:
+               {
+                       int eoftest;
+                       ast_stream_fastforward(ast_channel_stream(c), skip_ms);
+                       eoftest = fgetc(ast_channel_stream(c)->f);
+                       if (feof(ast_channel_stream(c)->f)) {
+                               ast_stream_rewind(ast_channel_stream(c), skip_ms);
+                       } else {
+                               ungetc(eoftest, ast_channel_stream(c)->f);
+                       }
+               }
+               break;
+       case AST_WAITSTREAM_CB_REWIND:
+               ast_stream_rewind(ast_channel_stream(c), skip_ms);
+               break;
+       default:
+               break;
+       }
+
+       if (cb) {
+               long ms_len = ast_tellstream(ast_channel_stream(c)) / (ast_format_get_sample_rate(ast_channel_stream(c)->fmt->format) / 1000);
+               cb(c, ms_len, type);
+       }
+
+       ast_test_suite_event_notify("PLAYBACK","Channel: %s\r\n"
+               "Control: %s\r\n"
+               "SkipMs: %d\r\n",
+               ast_channel_name(c),
+               (type == AST_WAITSTREAM_CB_FASTFORWARD) ? "FastForward" : "Rewind",
+               skip_ms);
+}
+
 /*!
  * \brief the core of all waitstream() functions
  */
-static int waitstream_core(struct ast_channel *c, const char *breakon,
-       const char *forward, const char *reverse, int skip_ms,
-       int audiofd, int cmdfd,  const char *context)
+static int waitstream_core(struct ast_channel *c,
+       const char *breakon,
+       const char *forward,
+       const char *reverse,
+       int skip_ms,
+       int audiofd,
+       int cmdfd,
+       const char *context,
+       ast_waitstream_fr_cb cb)
 {
        const char *orig_chan_name = NULL;
+
        int err = 0;
 
        if (!breakon)
@@ -1181,24 +1543,29 @@ static int waitstream_core(struct ast_channel *c, const char *breakon,
                reverse = "";
 
        /* Switch the channel to end DTMF frame only. waitstream_core doesn't care about the start of DTMF. */
-       ast_set_flag(c, AST_FLAG_END_DTMF_ONLY);
+       ast_channel_set_flag(c, AST_FLAG_END_DTMF_ONLY);
 
-       if (ast_test_flag(c, AST_FLAG_MASQ_NOSTREAM))
-               orig_chan_name = ast_strdupa(c->name);
+       if (ast_test_flag(ast_channel_flags(c), AST_FLAG_MASQ_NOSTREAM))
+               orig_chan_name = ast_strdupa(ast_channel_name(c));
+
+       if (ast_channel_stream(c) && cb) {
+               long ms_len = ast_tellstream(ast_channel_stream(c)) / (ast_format_get_sample_rate(ast_channel_stream(c)->fmt->format) / 1000);
+               cb(c, ms_len, AST_WAITSTREAM_CB_START);
+       }
 
-       while (c->stream) {
+       while (ast_channel_stream(c)) {
                int res;
                int ms;
 
-               if (orig_chan_name && strcasecmp(orig_chan_name, c->name)) {
+               if (orig_chan_name && strcasecmp(orig_chan_name, ast_channel_name(c))) {
                        ast_stopstream(c);
                        err = 1;
                        break;
                }
 
-               ms = ast_sched_wait(c->sched);
+               ms = ast_sched_wait(ast_channel_sched(c));
 
-               if (ms < 0 && !c->timingfunc) {
+               if (ms < 0 && !ast_channel_timingfunc(c)) {
                        ast_stopstream(c);
                        break;
                }
@@ -1208,7 +1575,7 @@ static int waitstream_core(struct ast_channel *c, const char *breakon,
                        res = ast_waitfor(c, ms);
                        if (res < 0) {
                                ast_log(LOG_WARNING, "Select failed (%s)\n", strerror(errno));
-                               ast_clear_flag(c, AST_FLAG_END_DTMF_ONLY);
+                               ast_channel_clear_flag(c, AST_FLAG_END_DTMF_ONLY);
                                return res;
                        }
                } else {
@@ -1219,11 +1586,11 @@ static int waitstream_core(struct ast_channel *c, const char *breakon,
                                if (errno == EINTR)
                                        continue;
                                ast_log(LOG_WARNING, "Wait failed (%s)\n", strerror(errno));
-                               ast_clear_flag(c, AST_FLAG_END_DTMF_ONLY);
+                               ast_channel_clear_flag(c, AST_FLAG_END_DTMF_ONLY);
                                return -1;
                        } else if (outfd > -1) { /* this requires cmdfd set */
                                /* The FD we were watching has something waiting */
-                               ast_clear_flag(c, AST_FLAG_END_DTMF_ONLY);
+                               ast_channel_clear_flag(c, AST_FLAG_END_DTMF_ONLY);
                                return 1;
                        }
                        /* if rchan is set, it is 'c' */
@@ -1232,7 +1599,7 @@ static int waitstream_core(struct ast_channel *c, const char *breakon,
                if (res > 0) {
                        struct ast_frame *fr = ast_read(c);
                        if (!fr) {
-                               ast_clear_flag(c, AST_FLAG_END_DTMF_ONLY);
+                               ast_channel_clear_flag(c, AST_FLAG_END_DTMF_ONLY);
                                return -1;
                        }
                        switch (fr->frametype) {
@@ -1240,39 +1607,61 @@ static int waitstream_core(struct ast_channel *c, const char *breakon,
                                if (context) {
                                        const char exten[2] = { fr->subclass.integer, '\0' };
                                        if (ast_exists_extension(c, context, exten, 1,
-                                               S_COR(c->caller.id.number.valid, c->caller.id.number.str, NULL))) {
+                                               S_COR(ast_channel_caller(c)->id.number.valid, ast_channel_caller(c)->id.number.str, NULL))) {
                                                res = fr->subclass.integer;
                                                ast_frfree(fr);
-                                               ast_clear_flag(c, AST_FLAG_END_DTMF_ONLY);
+                                               ast_channel_clear_flag(c, AST_FLAG_END_DTMF_ONLY);
                                                return res;
                                        }
                                } else {
                                        res = fr->subclass.integer;
                                        if (strchr(forward, res)) {
-                                               int eoftest;
-                                               ast_stream_fastforward(c->stream, skip_ms);
-                                               eoftest = fgetc(c->stream->f);
-                                               if (feof(c->stream->f)) {
-                                                       ast_stream_rewind(c->stream, skip_ms);
-                                               } else {
-                                                       ungetc(eoftest, c->stream->f);
-                                               }
+                                               waitstream_control(c, AST_WAITSTREAM_CB_FASTFORWARD, cb, skip_ms);
                                        } else if (strchr(reverse, res)) {
-                                               ast_stream_rewind(c->stream, skip_ms);
+                                               waitstream_control(c, AST_WAITSTREAM_CB_REWIND, cb, skip_ms);
                                        } else if (strchr(breakon, res)) {
+                                               ast_test_suite_event_notify("PLAYBACK","Channel: %s\r\n"
+                                                       "Control: %s\r\n",
+                                                       ast_channel_name(c),
+                                                       "Break");
+
                                                ast_frfree(fr);
-                                               ast_clear_flag(c, AST_FLAG_END_DTMF_ONLY);
+                                               ast_channel_clear_flag(c, AST_FLAG_END_DTMF_ONLY);
                                                return res;
-                                       }                                       
+                                       }
                                }
                                break;
                        case AST_FRAME_CONTROL:
                                switch (fr->subclass.integer) {
+                               case AST_CONTROL_STREAM_STOP:
+                               case AST_CONTROL_STREAM_SUSPEND:
+                               case AST_CONTROL_STREAM_RESTART:
+                                       /* Fall-through and break out */
+                                       ast_test_suite_event_notify("PLAYBACK","Channel: %s\r\n"
+                                               "Control: %s\r\n",
+                                               ast_channel_name(c),
+                                               "Break");
+                                       res = fr->subclass.integer;
+                                       ast_frfree(fr);
+                                       ast_channel_clear_flag(c, AST_FLAG_END_DTMF_ONLY);
+                                       return res;
+                               case AST_CONTROL_STREAM_REVERSE:
+                                       if (!skip_ms) {
+                                               skip_ms = 3000;
+                                       }
+                                       waitstream_control(c, AST_WAITSTREAM_CB_REWIND, cb, skip_ms);
+                                       break;
+                               case AST_CONTROL_STREAM_FORWARD:
+                                       if (!skip_ms) {
+                                               skip_ms = 3000;
+                                       }
+                                       waitstream_control(c, AST_WAITSTREAM_CB_FASTFORWARD, cb, skip_ms);
+                                       break;
                                case AST_CONTROL_HANGUP:
                                case AST_CONTROL_BUSY:
                                case AST_CONTROL_CONGESTION:
                                        ast_frfree(fr);
-                                       ast_clear_flag(c, AST_FLAG_END_DTMF_ONLY);
+                                       ast_channel_clear_flag(c, AST_FLAG_END_DTMF_ONLY);
                                        return -1;
                                case AST_CONTROL_RINGING:
                                case AST_CONTROL_ANSWER:
@@ -1284,6 +1673,8 @@ static int waitstream_core(struct ast_channel *c, const char *breakon,
                                case AST_CONTROL_CONNECTED_LINE:
                                case AST_CONTROL_REDIRECTING:
                                case AST_CONTROL_AOC:
+                               case AST_CONTROL_UPDATE_RTP_PEER:
+                               case AST_CONTROL_PVT_CAUSE_CODE:
                                case -1:
                                        /* Unimportant */
                                        break;
@@ -1304,40 +1695,87 @@ static int waitstream_core(struct ast_channel *c, const char *breakon,
                        }
                        ast_frfree(fr);
                }
-               ast_sched_runq(c->sched);
+               ast_sched_runq(ast_channel_sched(c));
        }
 
-       ast_clear_flag(c, AST_FLAG_END_DTMF_ONLY);
+       ast_channel_clear_flag(c, AST_FLAG_END_DTMF_ONLY);
 
-       return (err || c->_softhangup) ? -1 : 0;
+       return (err || ast_channel_softhangup_internal_flag(c)) ? -1 : 0;
+}
+
+int ast_waitstream_fr_w_cb(struct ast_channel *c,
+       const char *breakon,
+       const char *forward,
+       const char *reverse,
+       int ms,
+       ast_waitstream_fr_cb cb)
+{
+       return waitstream_core(c, breakon, forward, reverse, ms,
+               -1 /* no audiofd */, -1 /* no cmdfd */, NULL /* no context */, cb);
 }
 
 int ast_waitstream_fr(struct ast_channel *c, const char *breakon, const char *forward, const char *reverse, int ms)
 {
        return waitstream_core(c, breakon, forward, reverse, ms,
-               -1 /* no audiofd */, -1 /* no cmdfd */, NULL /* no context */);
+               -1 /* no audiofd */, -1 /* no cmdfd */, NULL /* no context */, NULL /* no callback */);
+}
+
+/*! \internal
+ * \brief Clean up the return value of a waitstream call
+ *
+ * It's possible for a control frame to come in from an external source and break the
+ * playback. From a consumer of most ast_waitstream_* function callers, this should
+ * appear like normal playback termination, i.e., return 0 and not the value of the
+ * control frame.
+ */
+static int sanitize_waitstream_return(int return_value)
+{
+       switch (return_value) {
+       case AST_CONTROL_STREAM_STOP:
+       case AST_CONTROL_STREAM_SUSPEND:
+       case AST_CONTROL_STREAM_RESTART:
+               /* Fall through and set return_value to 0 */
+               return_value = 0;
+               break;
+       default:
+               /* Do nothing */
+               break;
+       }
+
+       return return_value;
 }
 
 int ast_waitstream(struct ast_channel *c, const char *breakon)
 {
-       return waitstream_core(c, breakon, NULL, NULL, 0, -1, -1, NULL);
+       int res;
+
+       res = waitstream_core(c, breakon, NULL, NULL, 0, -1, -1, NULL, NULL /* no callback */);
+
+       return sanitize_waitstream_return(res);
 }
 
 int ast_waitstream_full(struct ast_channel *c, const char *breakon, int audiofd, int cmdfd)
 {
-       return waitstream_core(c, breakon, NULL, NULL, 0,
-               audiofd, cmdfd, NULL /* no context */);
+       int res;
+
+       res = waitstream_core(c, breakon, NULL, NULL, 0,
+               audiofd, cmdfd, NULL /* no context */, NULL /* no callback */);
+
+       return sanitize_waitstream_return(res);
 }
 
 int ast_waitstream_exten(struct ast_channel *c, const char *context)
 {
+       int res;
+
        /* Waitstream, with return in the case of a valid 1 digit extension */
        /* in the current or specified context being pressed */
-
        if (!context)
-               context = c->context;
-       return waitstream_core(c, NULL, NULL, NULL, 0,
-               -1, -1, context);
+               context = ast_channel_context(c);
+       res = waitstream_core(c, NULL, NULL, NULL, 0,
+               -1, -1, context, NULL /* no callback */);
+
+       return sanitize_waitstream_return(res);
 }
 
 /*
@@ -1349,18 +1787,18 @@ int ast_stream_and_wait(struct ast_channel *chan, const char *file, const char *
 {
        int res = 0;
        if (!ast_strlen_zero(file)) {
-               res = ast_streamfile(chan, file, chan->language);
+               res = ast_streamfile(chan, file, ast_channel_language(chan));
                if (!res) {
                        res = ast_waitstream(chan, digits);
                }
        }
        return res;
-} 
+}
 
 char *ast_format_str_reduce(char *fmts)
 {
-       struct ast_format *f;
-       struct ast_format *fmts_ptr[AST_MAX_FORMATS];
+       struct ast_format_def *f;
+       struct ast_format_def *fmts_ptr[AST_MAX_FORMATS];
        char *fmts_str[AST_MAX_FORMATS];
        char *stringp, *type;
        char *orig = fmts;
@@ -1437,7 +1875,7 @@ static char *handle_cli_core_show_file_formats(struct ast_cli_entry *e, int cmd,
 {
 #define FORMAT "%-10s %-10s %-20s\n"
 #define FORMAT2 "%-10s %-10s %-20s\n"
-       struct ast_format *f;
+       struct ast_format_def *f;
        int count_fmt = 0;
 
        switch (cmd) {
@@ -1459,7 +1897,7 @@ static char *handle_cli_core_show_file_formats(struct ast_cli_entry *e, int cmd,
 
        AST_RWLIST_RDLOCK(&formats);
        AST_RWLIST_TRAVERSE(&formats, f, list) {
-               ast_cli(a->fd, FORMAT2, ast_getformatname(f->format), f->name, f->exts);
+               ast_cli(a->fd, FORMAT2, ast_format_get_name(f->format), f->name, f->exts);
                count_fmt++;
        }
        AST_RWLIST_UNLOCK(&formats);
@@ -1469,12 +1907,35 @@ static char *handle_cli_core_show_file_formats(struct ast_cli_entry *e, int cmd,
 #undef FORMAT2
 }
 
+struct ast_format *ast_get_format_for_file_ext(const char *file_ext)
+{
+       struct ast_format_def *f;
+       SCOPED_RDLOCK(lock, &formats.lock);
+       AST_RWLIST_TRAVERSE(&formats, f, list) {
+               if (exts_compare(f->exts, file_ext)) {
+                       return f->format;
+               }
+       }
+
+       return NULL;
+}
+
 static struct ast_cli_entry cli_file[] = {
        AST_CLI_DEFINE(handle_cli_core_show_file_formats, "Displays file formats")
 };
 
+static void file_shutdown(void)
+{
+       ast_cli_unregister_multiple(cli_file, ARRAY_LEN(cli_file));
+       STASIS_MESSAGE_TYPE_CLEANUP(ast_format_register_type);
+       STASIS_MESSAGE_TYPE_CLEANUP(ast_format_unregister_type);
+}
+
 int ast_file_init(void)
 {
+       STASIS_MESSAGE_TYPE_INIT(ast_format_register_type);
+       STASIS_MESSAGE_TYPE_INIT(ast_format_unregister_type);
        ast_cli_register_multiple(cli_file, ARRAY_LEN(cli_file));
+       ast_register_cleanup(file_shutdown);
        return 0;
 }