git migration: Refactor the ASTERISK_FILE_VERSION macro
[asterisk/asterisk.git] / main / file.c
index 4783c41..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>
@@ -75,9 +75,9 @@ 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);
-       RAII_VAR(char *, stringp, ast_strdup(list), ast_free);
-       char *ext;
+       char *stringp, *ext;
 
+       stringp = ast_strdupa(list);    /* this is in the stack so does not need to be freed */
        if (!array || !stringp) {
                return NULL;
        }
@@ -97,6 +97,10 @@ static int publish_format_update(const struct ast_format_def *f, struct stasis_m
        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, "|"));
@@ -189,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) {
@@ -207,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");
                        }
@@ -223,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");
@@ -232,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;
@@ -349,7 +354,7 @@ static int exts_compare(const char *exts, const char *type)
  */
 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;
@@ -358,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;
@@ -407,17 +412,14 @@ static void filestream_destructor(void *arg)
                }
        }
 
-       if (f->filename)
-               free(f->filename);
-       if (f->realfilename)
-               free(f->realfilename);
+       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);
 }
 
@@ -428,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;
 
@@ -436,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;
 }
 
@@ -456,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;
@@ -529,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 */
                                }
@@ -559,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);
@@ -579,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;
@@ -606,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);
                }
@@ -749,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;
@@ -781,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;
 }
@@ -897,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;
@@ -954,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;
        }
@@ -985,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);
@@ -1008,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);
@@ -1067,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;
        }
 
@@ -1096,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))
@@ -1105,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;
 }
@@ -1330,7 +1350,7 @@ static void waitstream_control(struct ast_channel *c,
        }
 
        if (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, type);
        }
 
@@ -1373,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);
        }
 
@@ -1611,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);
@@ -1722,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);
@@ -1732,13 +1751,13 @@ static char *handle_cli_core_show_file_formats(struct ast_cli_entry *e, int cmd,
 #undef FORMAT2
 }
 
-const struct ast_format *ast_get_format_for_file_ext(const char *file_ext)
+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 f->format;
                }
        }
 
@@ -1761,6 +1780,6 @@ 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_atexit(file_shutdown);
+       ast_register_cleanup(file_shutdown);
        return 0;
 }