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