Sun May 20 06:33:54 2012

Asterisk developer's documentation


image.c

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 1999 - 2006, Digium, Inc.
00005  *
00006  * Mark Spencer <markster@digium.com>
00007  *
00008  * See http://www.asterisk.org for more information about
00009  * the Asterisk project. Please do not directly contact
00010  * any of the maintainers of this project for assistance;
00011  * the project provides a web site, mailing lists and IRC
00012  * channels for your use.
00013  *
00014  * This program is free software, distributed under the terms of
00015  * the GNU General Public License Version 2. See the LICENSE file
00016  * at the top of the source tree.
00017  */
00018 
00019 /*! \file
00020  *
00021  * \brief Image Management
00022  *
00023  * \author Mark Spencer <markster@digium.com>
00024  */
00025 
00026 #include "asterisk.h"
00027 
00028 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 360190 $")
00029 
00030 #include <sys/time.h>
00031 #include <sys/stat.h>
00032 #include <signal.h>
00033 
00034 #include "asterisk/paths.h"   /* use ast_config_AST_DATA_DIR */
00035 #include "asterisk/sched.h"
00036 #include "asterisk/channel.h"
00037 #include "asterisk/file.h"
00038 #include "asterisk/image.h"
00039 #include "asterisk/translate.h"
00040 #include "asterisk/cli.h"
00041 #include "asterisk/lock.h"
00042 
00043 /* XXX Why don't we just use the formats struct for this? */
00044 static AST_RWLIST_HEAD_STATIC(imagers, ast_imager);
00045 
00046 int ast_image_register(struct ast_imager *img)
00047 {
00048    AST_RWLIST_WRLOCK(&imagers);
00049    AST_RWLIST_INSERT_HEAD(&imagers, img, list);
00050    AST_RWLIST_UNLOCK(&imagers);
00051    ast_verb(2, "Registered format '%s' (%s)\n", img->name, img->desc);
00052    return 0;
00053 }
00054 
00055 void ast_image_unregister(struct ast_imager *img)
00056 {
00057    AST_RWLIST_WRLOCK(&imagers);
00058    img = AST_RWLIST_REMOVE(&imagers, img, list);
00059    AST_RWLIST_UNLOCK(&imagers);
00060 
00061    if (img)
00062       ast_verb(2, "Unregistered format '%s' (%s)\n", img->name, img->desc);
00063 }
00064 
00065 int ast_supports_images(struct ast_channel *chan)
00066 {
00067    if (!chan || !ast_channel_tech(chan))
00068       return 0;
00069    if (!ast_channel_tech(chan)->send_image)
00070       return 0;
00071    return 1;
00072 }
00073 
00074 static int file_exists(char *filename)
00075 {
00076    int res;
00077    struct stat st;
00078    res = stat(filename, &st);
00079    if (!res)
00080       return st.st_size;
00081    return 0;
00082 }
00083 
00084 static void make_filename(char *buf, int len, const char *filename, const char *preflang, char *ext)
00085 {
00086    if (filename[0] == '/') {
00087       if (!ast_strlen_zero(preflang))
00088          snprintf(buf, len, "%s-%s.%s", filename, preflang, ext);
00089       else
00090          snprintf(buf, len, "%s.%s", filename, ext);
00091    } else {
00092       if (!ast_strlen_zero(preflang))
00093          snprintf(buf, len, "%s/%s/%s-%s.%s", ast_config_AST_DATA_DIR, "images", filename, preflang, ext);
00094       else
00095          snprintf(buf, len, "%s/%s/%s.%s", ast_config_AST_DATA_DIR, "images", filename, ext);
00096    }
00097 }
00098 
00099 struct ast_frame *ast_read_image(const char *filename, const char *preflang, struct ast_format *format)
00100 {
00101    struct ast_imager *i;
00102    char buf[256];
00103    char tmp[80];
00104    char *e;
00105    struct ast_imager *found = NULL;
00106    int fd;
00107    int len=0;
00108    struct ast_frame *f = NULL;
00109 
00110    AST_RWLIST_RDLOCK(&imagers);
00111    AST_RWLIST_TRAVERSE(&imagers, i, list) {
00112       /* if NULL image format, just pick the first one, otherwise match it. */
00113       if (!format || (ast_format_cmp(&i->format, format) == AST_FORMAT_CMP_EQUAL)) {
00114          char *stringp=NULL;
00115          ast_copy_string(tmp, i->exts, sizeof(tmp));
00116          stringp = tmp;
00117          e = strsep(&stringp, "|");
00118          while (e) {
00119             make_filename(buf, sizeof(buf), filename, preflang, e);
00120             if ((len = file_exists(buf))) {
00121                found = i;
00122                break;
00123             }
00124             make_filename(buf, sizeof(buf), filename, NULL, e);
00125             if ((len = file_exists(buf))) {
00126                found = i;
00127                break;
00128             }
00129             e = strsep(&stringp, "|");
00130          }
00131       }
00132       if (found)
00133          break;
00134    }
00135 
00136    if (found) {
00137       fd = open(buf, O_RDONLY);
00138       if (fd > -1) {
00139          if (!found->identify || found->identify(fd)) {
00140             /* Reset file pointer */
00141             lseek(fd, 0, SEEK_SET);
00142             f = found->read_image(fd, len);
00143          } else
00144             ast_log(LOG_WARNING, "%s does not appear to be a %s file\n", buf, found->name);
00145          close(fd);
00146       } else
00147          ast_log(LOG_WARNING, "Unable to open '%s': %s\n", buf, strerror(errno));
00148    } else
00149       ast_log(LOG_WARNING, "Image file '%s' not found\n", filename);
00150 
00151    AST_RWLIST_UNLOCK(&imagers);
00152 
00153    return f;
00154 }
00155 
00156 int ast_send_image(struct ast_channel *chan, const char *filename)
00157 {
00158    struct ast_frame *f;
00159    int res = -1;
00160    if (ast_channel_tech(chan)->send_image) {
00161       f = ast_read_image(filename, ast_channel_language(chan), NULL);
00162       if (f) {
00163          res = ast_channel_tech(chan)->send_image(chan, f);
00164          ast_frfree(f);
00165       }
00166    }
00167    return res;
00168 }
00169 
00170 static char *handle_core_show_image_formats(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
00171 {
00172 #define FORMAT "%10s %10s %50s %10s\n"
00173 #define FORMAT2 "%10s %10s %50s %10s\n"
00174    struct ast_imager *i;
00175    int count_fmt = 0;
00176 
00177    switch (cmd) {
00178    case CLI_INIT:
00179       e->command = "core show image formats";
00180       e->usage =
00181          "Usage: core show image formats\n"
00182          "       Displays currently registered image formats (if any).\n";
00183       return NULL;
00184    case CLI_GENERATE:
00185       return NULL;
00186    }
00187    if (a->argc != 4)
00188       return CLI_SHOWUSAGE;
00189    ast_cli(a->fd, FORMAT, "Name", "Extensions", "Description", "Format");
00190    ast_cli(a->fd, FORMAT, "----", "----------", "-----------", "------");
00191    AST_RWLIST_RDLOCK(&imagers);
00192    AST_RWLIST_TRAVERSE(&imagers, i, list) {
00193       ast_cli(a->fd, FORMAT2, i->name, i->exts, i->desc, ast_getformatname(&i->format));
00194       count_fmt++;
00195    }
00196    AST_RWLIST_UNLOCK(&imagers);
00197    ast_cli(a->fd, "\n%d image format%s registered.\n", count_fmt, count_fmt == 1 ? "" : "s");
00198    return CLI_SUCCESS;
00199 }
00200 
00201 static struct ast_cli_entry cli_image[] = {
00202    AST_CLI_DEFINE(handle_core_show_image_formats, "Displays image formats")
00203 };
00204 
00205 int ast_image_init(void)
00206 {
00207    ast_cli_register_multiple(cli_image, ARRAY_LEN(cli_image));
00208    return 0;
00209 }

Generated on Sun May 20 06:33:54 2012 for Asterisk - The Open Source Telephony Project by  doxygen 1.5.6