git migration: Refactor the ASTERISK_FILE_VERSION macro
[asterisk/asterisk.git] / main / file.c
index 8ff88e7..acd2cc6 100644 (file)
@@ -29,7 +29,7 @@
 
 #include "asterisk.h"
 
-ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
+ASTERISK_REGISTER_FILE()
 
 #include <dirent.h>
 #include <sys/stat.h>
@@ -51,6 +51,9 @@ 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"
 
 /*! \brief
  * The following variable controls the layout of localized sound files.
@@ -66,6 +69,59 @@ int ast_language_is_prefix = 1;
 
 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;
+       }
+
+       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_def *tmp;
@@ -99,6 +155,7 @@ int __ast_format_def_register(const struct ast_format_def *f, struct ast_module
        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;
 }
@@ -112,6 +169,7 @@ int ast_format_def_unregister(const char *name)
        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;
                }
@@ -135,8 +193,8 @@ int ast_stopstream(struct ast_channel *tmp)
        if (ast_channel_stream(tmp)) {
                ast_closestream(ast_channel_stream(tmp));
                ast_channel_stream_set(tmp, NULL);
-               if (ast_channel_oldwriteformat(tmp)->id && ast_set_write_format(tmp, ast_channel_oldwriteformat(tmp)))
-                       ast_log(LOG_WARNING, "Unable to restore format back to %s\n", ast_getformatname(ast_channel_oldwriteformat(tmp)));
+               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 (ast_channel_vstream(tmp) != NULL) {
@@ -153,10 +211,10 @@ int ast_writestream(struct ast_filestream *fs, struct ast_frame *f)
 {
        int res = -1;
        if (f->frametype == AST_FRAME_VIDEO) {
-               if (AST_FORMAT_GET_TYPE(fs->fmt->format.id) == AST_FORMAT_TYPE_AUDIO) {
+               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.format);
+                               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");
                        }
@@ -169,7 +227,7 @@ int ast_writestream(struct ast_filestream *fs, struct ast_frame *f)
                ast_log(LOG_WARNING, "Tried to write non-voice frame\n");
                return -1;
        }
-       if (ast_format_cmp(&f->subclass.format, &fs->fmt->format) != AST_FORMAT_CMP_NOT_EQUAL) {
+       if (ast_format_cmp(f->subclass.format, fs->fmt->format) != AST_FORMAT_CMP_NOT_EQUAL) {
                res =  fs->fmt->write(fs, f);
                if (res < 0)
                        ast_log(LOG_WARNING, "Natural write failed\n");
@@ -178,18 +236,19 @@ int ast_writestream(struct ast_filestream *fs, struct ast_frame *f)
        } 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 && (ast_format_cmp(&f->subclass.format, &fs->lastwriteformat) != AST_FORMAT_CMP_EQUAL)) {
+               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.format);
-               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.format));
-               else {
+                               fs->fmt->name, ast_format_get_name(f->subclass.format));
+               } else {
                        struct ast_frame *trf;
-                       ast_format_copy(&fs->lastwriteformat, &f->subclass.format);
+                       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;
@@ -289,10 +348,13 @@ 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 */
+/*!
+ * \internal
+ * \brief Close the file stream by canceling any pending read / write callbacks
+ */
 static void filestream_close(struct ast_filestream *f)
 {
-       enum ast_format_type format_type = AST_FORMAT_GET_TYPE(f->fmt->format.id);
+       enum ast_media_type format_type = ast_format_get_type(f->fmt->format);
 
        if (!f->owner) {
                return;
@@ -301,12 +363,12 @@ static void filestream_close(struct ast_filestream *f)
        /* Stop a running stream if there is one */
        switch (format_type)
        {
-       case AST_FORMAT_TYPE_AUDIO:
+       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_FORMAT_TYPE_VIDEO:
+       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;
@@ -329,6 +391,15 @@ static void filestream_destructor(void *arg)
        if (f->trans)
                ast_translator_free_path(f->trans);
 
+       if (f->fmt->close) {
+               void (*closefn)(struct ast_filestream *) = f->fmt->close;
+               closefn(f);
+       }
+
+       if (f->f) {
+               fclose(f->f);
+       }
+
        if (f->realfilename && f->filename) {
                pid = ast_safe_fork(0);
                if (!pid) {
@@ -341,23 +412,14 @@ static void filestream_destructor(void *arg)
                }
        }
 
-       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)
-               fclose(f->f);
+       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);
 }
 
@@ -368,6 +430,7 @@ static struct ast_filestream *get_filestream(struct ast_format_def *fmt, FILE *b
        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;
 
@@ -376,6 +439,15 @@ static struct ast_filestream *get_filestream(struct ast_format_def *fmt, FILE *b
        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;
 }
 
@@ -396,8 +468,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;
@@ -469,9 +540,9 @@ static int filehelper(const char *filename, const void *arg2, const char *fmt, c
                                FILE *bfile;
                                struct ast_filestream *s;
 
-                               if ((ast_format_cmp(ast_channel_writeformat(chan), &f->format) == AST_FORMAT_CMP_NOT_EQUAL) &&
-                                    !(((AST_FORMAT_GET_TYPE(f->format.id) == AST_FORMAT_TYPE_AUDIO) && fmt) ||
-                                         ((AST_FORMAT_GET_TYPE(f->format.id) == AST_FORMAT_TYPE_VIDEO) && 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 */
                                }
@@ -499,7 +570,7 @@ static int filehelper(const char *filename, const void *arg2, const char *fmt, c
                                s->fmt = f;
                                s->trans = NULL;
                                s->filename = NULL;
-                               if (AST_FORMAT_GET_TYPE(s->fmt->format.id) == AST_FORMAT_TYPE_AUDIO) {
+                               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);
@@ -519,7 +590,7 @@ static int filehelper(const char *filename, const void *arg2, const char *fmt, c
                                /* 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_add((struct ast_format_cap *) arg2, &f->format);
+                                       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;
@@ -546,7 +617,7 @@ static int filehelper(const char *filename, const void *arg2, const char *fmt, c
                                break;
 
                        default:
-                               ast_log(LOG_WARNING, "Unknown helper %d\n", action);
+                               ast_log(LOG_WARNING, "Unknown helper %u\n", action);
                        }
                        ast_free(fn);
                }
@@ -601,12 +672,12 @@ static int fileexists_test(const char *filename, const char *fmt, const char *la
  * \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
+ * \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
@@ -689,23 +760,25 @@ struct ast_filestream *ast_openstream_full(struct ast_channel *chan, const char
        buflen = strlen(preflang) + strlen(filename) + 4;
        buf = ast_alloca(buflen);
 
-       if (!(file_fmt_cap = ast_format_cap_alloc_nolock())) {
+       if (!(file_fmt_cap = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT))) {
                return NULL;
        }
        if (!fileexists_core(filename, NULL, preflang, buf, buflen, file_fmt_cap) ||
-               !ast_format_cap_has_type(file_fmt_cap, AST_FORMAT_TYPE_AUDIO)) {
+               !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);
-               file_fmt_cap = ast_format_cap_destroy(file_fmt_cap);
+               ao2_ref(file_fmt_cap, -1);
                return NULL;
        }
 
        /* Set the channel to a format we can work with and save off the previous format. */
-       ast_format_copy(ast_channel_oldwriteformat(chan), ast_channel_writeformat(chan));
+       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. */
-       file_fmt_cap = ast_format_cap_destroy(file_fmt_cap);
+       ao2_ref(file_fmt_cap, -1);
 
        if (res == -1) {        /* No format available that works with this channel */
                return NULL;
@@ -721,50 +794,50 @@ 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.
         */
-       struct ast_format tmp_fmt;
        struct ast_format_cap *tmp_cap;
        char *buf;
        int buflen;
-       const char *fmt;
-       int fd;
+       int i, fd;
 
-       if (preflang == NULL)
+       if (preflang == NULL) {
                preflang = "";
+       }
        buflen = strlen(preflang) + strlen(filename) + 4;
        buf = ast_alloca(buflen);
 
        /* is the channel capable of video without translation ?*/
-       if (!ast_format_cap_has_type(ast_channel_nativeformats(chan), AST_FORMAT_TYPE_VIDEO)) {
+       if (!ast_format_cap_has_type(ast_channel_nativeformats(chan), AST_MEDIA_TYPE_VIDEO)) {
                return NULL;
        }
-       if (!(tmp_cap = ast_format_cap_alloc_nolock())) {
+       if (!(tmp_cap = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT))) {
                return NULL;
        }
        /* Video is supported, so see what video formats exist for this file */
        if (!fileexists_core(filename, NULL, preflang, buf, buflen, tmp_cap)) {
-               tmp_cap = ast_format_cap_destroy(tmp_cap);
+               ao2_ref(tmp_cap, -1);
                return NULL;
        }
 
        /* iterate over file formats and pick the first one compatible with the channel's native formats */
-       ast_format_cap_iter_start(tmp_cap);
-       while (!ast_format_cap_iter_next(tmp_cap, &tmp_fmt)) {
-               fmt = ast_getformatname(&tmp_fmt);
-               if ((AST_FORMAT_GET_TYPE(tmp_fmt.id) != AST_FORMAT_TYPE_VIDEO) ||
-                       !ast_format_cap_iscompatible(ast_channel_nativeformats(chan), &tmp_fmt)) {
+       for (i = 0; i < ast_format_cap_count(tmp_cap); ++i) {
+               struct ast_format *format = ast_format_cap_get_format(tmp_cap, i);
+
+               if ((ast_format_get_type(format) != AST_MEDIA_TYPE_VIDEO) ||
+                       !ast_format_cap_iscompatible(ast_channel_nativeformats(chan), tmp_cap)) {
+                       ao2_ref(format, -1);
                        continue;
                }
 
-               fd = filehelper(buf, chan, fmt, ACTION_OPEN);
+               fd = filehelper(buf, chan, ast_format_get_name(format), ACTION_OPEN);
                if (fd >= 0) {
-                       ast_format_cap_iter_end(tmp_cap);
-                       tmp_cap = ast_format_cap_destroy(tmp_cap);
+                       ao2_ref(format, -1);
+                       ao2_ref(tmp_cap, -1);
                        return ast_channel_vstream(chan);
                }
                ast_log(LOG_WARNING, "File %s has video but couldn't be opened\n", filename);
+               ao2_ref(format, -1);
        }
-       ast_format_cap_iter_end(tmp_cap);
-       tmp_cap = ast_format_cap_destroy(tmp_cap);
+       ao2_ref(tmp_cap, -1);
 
        return NULL;
 }
@@ -837,14 +910,14 @@ static enum fsread_res ast_readaudio_callback(struct ast_filestream *s)
 
        if (whennext != s->lasttimeout) {
                if (ast_channel_timingfd(s->owner) > -1) {
-                       float samp_rate = (float) ast_format_rate(&s->fmt->format);
+                       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 {
-                       ast_channel_streamid_set(s->owner, ast_sched_add(ast_channel_sched(s->owner), 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;
@@ -894,7 +967,7 @@ static enum fsread_res ast_readvideo_callback(struct ast_filestream *s)
        }
 
        if (whennext != s->lasttimeout) {
-               ast_channel_vstreamid_set(s->owner, ast_sched_add(ast_channel_sched(s->owner), 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;
        }
@@ -925,7 +998,7 @@ int ast_playstream(struct ast_filestream *s)
 {
        enum fsread_res res;
 
-       if (AST_FORMAT_GET_TYPE(s->fmt->format.id) == AST_FORMAT_TYPE_AUDIO)
+       if (ast_format_get_type(s->fmt->format) == AST_MEDIA_TYPE_AUDIO)
                res = ast_readaudio_callback(s);
        else
                res = ast_readvideo_callback(s);
@@ -948,6 +1021,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);
@@ -964,6 +1042,9 @@ int ast_closestream(struct ast_filestream *f)
         * 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. */
+       if (f == NULL) {
+               return 0;
+       }
        filestream_close(f);
        ao2_ref(f, -1);
        return 0;
@@ -1004,14 +1085,15 @@ int ast_streamfile(struct ast_channel *chan, const char *filename, const char *p
 {
        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), ast_channel_nativeformats(chan)), strerror(errno));
+               struct ast_str *codec_buf = ast_str_alloca(64);
+               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));
                return -1;
        }
 
@@ -1033,7 +1115,7 @@ int ast_streamfile(struct ast_channel *chan, const char *filename, const char *p
 
        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(ast_channel_flags(chan), AST_FLAG_MASQ_NOSTREAM))
@@ -1042,10 +1124,11 @@ int ast_streamfile(struct ast_channel *chan, const char *filename, const char *p
                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", ast_channel_name(chan), filename, ast_getformatname(ast_channel_writeformat(chan)), preflang ? preflang : "default");
+       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");
 
        return res;
 }
@@ -1240,6 +1323,45 @@ 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
  */
@@ -1271,7 +1393,7 @@ static int waitstream_core(struct ast_channel *c,
                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_rate(&ast_channel_stream(c)->fmt->format) / 1000);
+               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);
        }
 
@@ -1336,34 +1458,49 @@ static int waitstream_core(struct ast_channel *c,
                                                return res;
                                        }
                                } else {
-                                       enum ast_waitstream_fr_cb_values cb_val = 0;
                                        res = fr->subclass.integer;
                                        if (strchr(forward, res)) {
-                                               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);
-                                               }
-                                               cb_val = AST_WAITSTREAM_CB_FASTFORWARD;
+                                               waitstream_control(c, AST_WAITSTREAM_CB_FASTFORWARD, cb, skip_ms);
                                        } else if (strchr(reverse, res)) {
-                                               ast_stream_rewind(ast_channel_stream(c), skip_ms);
-                                               cb_val = AST_WAITSTREAM_CB_REWIND;
+                                               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(ast_channel_flags(c), AST_FLAG_END_DTMF_ONLY);
                                                return res;
                                        }
-                                       if (cb_val && cb) {
-                                               long ms_len = ast_tellstream(ast_channel_stream(c)) / (ast_format_rate(&ast_channel_stream(c)->fmt->format) / 1000);
-                                               cb(c, ms_len, cb_val);
-                                       }
                                }
                                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_clear_flag(ast_channel_flags(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:
@@ -1427,26 +1564,62 @@ int ast_waitstream_fr(struct ast_channel *c, const char *breakon, const char *fo
                -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, NULL /* no callback */);
+       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,
+       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 = ast_channel_context(c);
-       return waitstream_core(c, NULL, NULL, NULL, 0,
+       res = waitstream_core(c, NULL, NULL, NULL, 0,
                -1, -1, context, NULL /* no callback */);
+
+       return sanitize_waitstream_return(res);
 }
 
 /*
@@ -1458,7 +1631,6 @@ int ast_stream_and_wait(struct ast_channel *chan, const char *file, const char *
 {
        int res = 0;
        if (!ast_strlen_zero(file)) {
-               ast_test_suite_event_notify("PLAYBACK", "Message: %s\r\nChannel: %s", file, ast_channel_name(chan));
                res = ast_streamfile(chan, file, ast_channel_language(chan));
                if (!res) {
                        res = ast_waitstream(chan, digits);
@@ -1569,7 +1741,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);
@@ -1579,12 +1751,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;
 }