Merge "core/frame: Fix ast_frdup() and ast_frisolate() for empty text frames"
[asterisk/asterisk.git] / main / image.c
index ff67c03..b007ae1 100644 (file)
  *
  * \brief Image Management
  *
- * \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 <stdio.h>
-#include <stdlib.h>
-#include <string.h>
 #include <sys/time.h>
 #include <sys/stat.h>
 #include <signal.h>
-#include <errno.h>
-#include <unistd.h>
 
+#include "asterisk/paths.h"    /* use ast_config_AST_DATA_DIR */
 #include "asterisk/sched.h"
-#include "asterisk/options.h"
 #include "asterisk/channel.h"
-#include "asterisk/logger.h"
 #include "asterisk/file.h"
 #include "asterisk/image.h"
 #include "asterisk/translate.h"
 #include "asterisk/cli.h"
 #include "asterisk/lock.h"
 
-static AST_LIST_HEAD_STATIC(imagers, ast_imager);
+/* XXX Why don't we just use the formats struct for this? */
+static AST_RWLIST_HEAD_STATIC(imagers, ast_imager);
 
 int ast_image_register(struct ast_imager *img)
 {
-       if (option_verbose > 1)
-               ast_verbose(VERBOSE_PREFIX_2 "Registered format '%s' (%s)\n", img->name, img->desc);
-       AST_LIST_LOCK(&imagers);
-       AST_LIST_INSERT_HEAD(&imagers, img, list);
-       AST_LIST_UNLOCK(&imagers);
+       AST_RWLIST_WRLOCK(&imagers);
+       AST_RWLIST_INSERT_HEAD(&imagers, img, list);
+       AST_RWLIST_UNLOCK(&imagers);
+       ast_verb(2, "Registered format '%s' (%s)\n", img->name, img->desc);
        return 0;
 }
 
 void ast_image_unregister(struct ast_imager *img)
 {
-       struct ast_imager *i;
-       
-       AST_LIST_LOCK(&imagers);
-       AST_LIST_TRAVERSE_SAFE_BEGIN(&imagers, i, list) {       
-               if (i == img) {
-                       AST_LIST_REMOVE_CURRENT(&imagers, list);
-                       break;
-               }
-       }
-       AST_LIST_TRAVERSE_SAFE_END
-       AST_LIST_UNLOCK(&imagers);
-       if (i && (option_verbose > 1))
-               ast_verbose(VERBOSE_PREFIX_2 "Unregistered format '%s' (%s)\n", img->name, img->desc);
+       AST_RWLIST_WRLOCK(&imagers);
+       img = AST_RWLIST_REMOVE(&imagers, img, list);
+       AST_RWLIST_UNLOCK(&imagers);
+
+       if (img)
+               ast_verb(2, "Unregistered format '%s' (%s)\n", img->name, img->desc);
 }
 
 int ast_supports_images(struct ast_channel *chan)
 {
-       if (!chan || !chan->tech)
+       if (!chan || !ast_channel_tech(chan))
                return 0;
-       if (!chan->tech->send_image)
+       if (!ast_channel_tech(chan)->send_image)
                return 0;
        return 1;
 }
@@ -94,7 +83,7 @@ static int file_exists(char *filename)
        return 0;
 }
 
-static void make_filename(char *buf, int len, char *filename, const char *preflang, char *ext)
+static void make_filename(char *buf, int len, const char *filename, const char *preflang, char *ext)
 {
        if (filename[0] == '/') {
                if (!ast_strlen_zero(preflang))
@@ -109,7 +98,7 @@ static void make_filename(char *buf, int len, char *filename, const char *prefla
        }
 }
 
-struct ast_frame *ast_read_image(char *filename, const char *preflang, int format)
+struct ast_frame *ast_read_image(const char *filename, const char *preflang, struct ast_format *format)
 {
        struct ast_imager *i;
        char buf[256];
@@ -119,15 +108,16 @@ struct ast_frame *ast_read_image(char *filename, const char *preflang, int forma
        int fd;
        int len=0;
        struct ast_frame *f = NULL;
-       
-       AST_LIST_LOCK(&imagers);
-       AST_LIST_TRAVERSE(&imagers, i, list) {
-               if (i->format & format) {
+
+       AST_RWLIST_RDLOCK(&imagers);
+       AST_RWLIST_TRAVERSE(&imagers, i, list) {
+               /* if NULL image format, just pick the first one, otherwise match it. */
+               if (!format || (ast_format_cmp(i->format, format) == AST_FORMAT_CMP_EQUAL)) {
                        char *stringp=NULL;
-                       strncpy(tmp, i->exts, sizeof(tmp)-1);
-                       stringp=tmp;
+                       ast_copy_string(tmp, i->exts, sizeof(tmp));
+                       stringp = tmp;
                        e = strsep(&stringp, "|");
-                       while(e) {
+                       while (e) {
                                make_filename(buf, sizeof(buf), filename, preflang, e);
                                if ((len = file_exists(buf))) {
                                        found = i;
@@ -142,7 +132,7 @@ struct ast_frame *ast_read_image(char *filename, const char *preflang, int forma
                        }
                }
                if (found)
-                       break;  
+                       break;
        }
 
        if (found) {
@@ -151,7 +141,7 @@ struct ast_frame *ast_read_image(char *filename, const char *preflang, int forma
                        if (!found->identify || found->identify(fd)) {
                                /* Reset file pointer */
                                lseek(fd, 0, SEEK_SET);
-                               f = found->read_image(fd,len); 
+                               f = found->read_image(fd, len);
                        } else
                                ast_log(LOG_WARNING, "%s does not appear to be a %s file\n", buf, found->name);
                        close(fd);
@@ -159,52 +149,69 @@ struct ast_frame *ast_read_image(char *filename, const char *preflang, int forma
                        ast_log(LOG_WARNING, "Unable to open '%s': %s\n", buf, strerror(errno));
        } else
                ast_log(LOG_WARNING, "Image file '%s' not found\n", filename);
-       
-       AST_LIST_UNLOCK(&imagers);
-       
+
+       AST_RWLIST_UNLOCK(&imagers);
+
        return f;
 }
 
-
-int ast_send_image(struct ast_channel *chan, char *filename)
+int ast_send_image(struct ast_channel *chan, const char *filename)
 {
        struct ast_frame *f;
        int res = -1;
-       if (chan->tech->send_image) {
-               f = ast_read_image(filename, chan->language, -1);
+       if (ast_channel_tech(chan)->send_image) {
+               f = ast_read_image(filename, ast_channel_language(chan), NULL);
                if (f) {
-                       res = chan->tech->send_image(chan, f);
+                       res = ast_channel_tech(chan)->send_image(chan, f);
                        ast_frfree(f);
                }
        }
        return res;
 }
 
-static int show_image_formats(int fd, int argc, char *argv[])
+static char *handle_core_show_image_formats(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
 {
 #define FORMAT "%10s %10s %50s %10s\n"
 #define FORMAT2 "%10s %10s %50s %10s\n"
        struct ast_imager *i;
-       if (argc != 3)
-               return RESULT_SHOWUSAGE;
-       ast_cli(fd, FORMAT, "Name", "Extensions", "Description", "Format");
-       AST_LIST_TRAVERSE(&imagers, i, list)
-               ast_cli(fd, FORMAT2, i->name, i->exts, i->desc, ast_getformatname(i->format));
-       return RESULT_SUCCESS;
+       int count_fmt = 0;
+
+       switch (cmd) {
+       case CLI_INIT:
+               e->command = "core show image formats";
+               e->usage =
+                       "Usage: core show image formats\n"
+                       "       Displays currently registered image formats (if any).\n";
+               return NULL;
+       case CLI_GENERATE:
+               return NULL;
+       }
+       if (a->argc != 4)
+               return CLI_SHOWUSAGE;
+       ast_cli(a->fd, FORMAT, "Name", "Extensions", "Description", "Format");
+       ast_cli(a->fd, FORMAT, "----", "----------", "-----------", "------");
+       AST_RWLIST_RDLOCK(&imagers);
+       AST_RWLIST_TRAVERSE(&imagers, i, list) {
+               ast_cli(a->fd, FORMAT2, i->name, i->exts, i->desc, ast_format_get_name(i->format));
+               count_fmt++;
+       }
+       AST_RWLIST_UNLOCK(&imagers);
+       ast_cli(a->fd, "\n%d image format%s registered.\n", count_fmt, count_fmt == 1 ? "" : "s");
+       return CLI_SUCCESS;
 }
 
-struct ast_cli_entry show_images =
-{
-       { "show", "image", "formats" },
-       show_image_formats,
-       "Displays image formats",
-"Usage: show image formats\n"
-"       displays currently registered image formats (if any)\n"
+static struct ast_cli_entry cli_image[] = {
+       AST_CLI_DEFINE(handle_core_show_image_formats, "Displays image formats")
 };
 
+static void image_shutdown(void)
+{
+       ast_cli_unregister_multiple(cli_image, ARRAY_LEN(cli_image));
+}
 
 int ast_image_init(void)
 {
-       return ast_cli_register(&show_images);
+       ast_cli_register_multiple(cli_image, ARRAY_LEN(cli_image));
+       ast_register_cleanup(image_shutdown);
+       return 0;
 }
-