| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
#include "asterisk.h" |
| 31 |
|
| 32 |
#include <sys/time.h> |
| 33 |
#include <sys/stat.h> |
| 34 |
#include <signal.h> |
| 35 |
|
| 36 |
#include "asterisk/paths.h" |
| 37 |
#include "asterisk/sched.h" |
| 38 |
#include "asterisk/channel.h" |
| 39 |
#include "asterisk/file.h" |
| 40 |
#include "asterisk/image.h" |
| 41 |
#include "asterisk/translate.h" |
| 42 |
#include "asterisk/cli.h" |
| 43 |
#include "asterisk/lock.h" |
| 44 |
|
| 45 |
|
| 46 |
static AST_RWLIST_HEAD_STATIC(imagers, ast_imager); |
| 47 |
|
| 48 |
int ast_image_register(struct ast_imager *img) |
| 49 |
{ |
| 50 |
AST_RWLIST_WRLOCK(&imagers); |
| 51 |
AST_RWLIST_INSERT_HEAD(&imagers, img, list); |
| 52 |
AST_RWLIST_UNLOCK(&imagers); |
| 53 |
ast_verb(5, "Registered format '%s' (%s)\n", img->name, img->desc); |
| 54 |
return 0; |
| 55 |
} |
| 56 |
|
| 57 |
void ast_image_unregister(struct ast_imager *img) |
| 58 |
{ |
| 59 |
AST_RWLIST_WRLOCK(&imagers); |
| 60 |
img = AST_RWLIST_REMOVE(&imagers, img, list); |
| 61 |
AST_RWLIST_UNLOCK(&imagers); |
| 62 |
|
| 63 |
if (img) |
| 64 |
ast_verb(5, "Unregistered format '%s' (%s)\n", img->name, img->desc); |
| 65 |
} |
| 66 |
|
| 67 |
int ast_supports_images(struct ast_channel *chan) |
| 68 |
{ |
| 69 |
if (!chan || !ast_channel_tech(chan)) |
| 70 |
return 0; |
| 71 |
if (!ast_channel_tech(chan)->send_image) |
| 72 |
return 0; |
| 73 |
return 1; |
| 74 |
} |
| 75 |
|
| 76 |
static int file_exists(char *filename) |
| 77 |
{ |
| 78 |
int res; |
| 79 |
struct stat st; |
| 80 |
res = stat(filename, &st); |
| 81 |
if (!res) |
| 82 |
return st.st_size; |
| 83 |
return 0; |
| 84 |
} |
| 85 |
|
| 86 |
static void make_filename(char *buf, int len, const char *filename, const char *preflang, char *ext) |
| 87 |
{ |
| 88 |
if (filename[0] == '/') { |
| 89 |
if (!ast_strlen_zero(preflang)) |
| 90 |
snprintf(buf, len, "%s-%s.%s", filename, preflang, ext); |
| 91 |
else |
| 92 |
snprintf(buf, len, "%s.%s", filename, ext); |
| 93 |
} else { |
| 94 |
if (!ast_strlen_zero(preflang)) |
| 95 |
snprintf(buf, len, "%s/%s/%s-%s.%s", ast_config_AST_DATA_DIR, "images", filename, preflang, ext); |
| 96 |
else |
| 97 |
snprintf(buf, len, "%s/%s/%s.%s", ast_config_AST_DATA_DIR, "images", filename, ext); |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
struct ast_frame *ast_read_image(const char *filename, const char *preflang, struct ast_format *format) |
| 102 |
{ |
| 103 |
struct ast_imager *i; |
| 104 |
char buf[256]; |
| 105 |
char tmp[80]; |
| 106 |
char *e; |
| 107 |
struct ast_imager *found = NULL; |
| 108 |
int fd; |
| 109 |
int len=0; |
| 110 |
struct ast_frame *f = NULL; |
| 111 |
|
| 112 |
AST_RWLIST_RDLOCK(&imagers); |
| 113 |
AST_RWLIST_TRAVERSE(&imagers, i, list) { |
| 114 |
|
| 115 |
if (!format || (ast_format_cmp(i->format, format) == AST_FORMAT_CMP_EQUAL)) { |
| 116 |
char *stringp=NULL; |
| 117 |
ast_copy_string(tmp, i->exts, sizeof(tmp)); |
| 118 |
stringp = tmp; |
| 119 |
e = strsep(&stringp, "|"); |
| 120 |
while (e) { |
| 121 |
make_filename(buf, sizeof(buf), filename, preflang, e); |
| 122 |
if ((len = file_exists(buf))) { |
| 123 |
found = i; |
| 124 |
break; |
| 125 |
} |
| 126 |
make_filename(buf, sizeof(buf), filename, NULL, e); |
| 127 |
if ((len = file_exists(buf))) { |
| 128 |
found = i; |
| 129 |
break; |
| 130 |
} |
| 131 |
e = strsep(&stringp, "|"); |
| 132 |
} |
| 133 |
} |
| 134 |
if (found) |
| 135 |
break; |
| 136 |
} |
| 137 |
|
| 138 |
if (found) { |
| 139 |
fd = open(buf, O_RDONLY); |
| 140 |
if (fd > -1) { |
| 141 |
if (!found->identify || found->identify(fd)) { |
| 142 |
|
| 143 |
lseek(fd, 0, SEEK_SET); |
| 144 |
f = found->read_image(fd, len); |
| 145 |
} else |
| 146 |
ast_log(LOG_WARNING, "%s does not appear to be a %s file\n", buf, found->name); |
| 147 |
close(fd); |
| 148 |
} else |
| 149 |
ast_log(LOG_WARNING, "Unable to open '%s': %s\n", buf, strerror(errno)); |
| 150 |
} else |
| 151 |
ast_log(LOG_WARNING, "Image file '%s' not found\n", filename); |
| 152 |
|
| 153 |
AST_RWLIST_UNLOCK(&imagers); |
| 154 |
|
| 155 |
return f; |
| 156 |
} |
| 157 |
|
| 158 |
int ast_send_image(struct ast_channel *chan, const char *filename) |
| 159 |
{ |
| 160 |
struct ast_frame *f; |
| 161 |
int res = -1; |
| 162 |
if (ast_channel_tech(chan)->send_image) { |
| 163 |
f = ast_read_image(filename, ast_channel_language(chan), NULL); |
| 164 |
if (f) { |
| 165 |
res = ast_channel_tech(chan)->send_image(chan, f); |
| 166 |
ast_frfree(f); |
| 167 |
} |
| 168 |
} |
| 169 |
return res; |
| 170 |
} |
| 171 |
|
| 172 |
static char *handle_core_show_image_formats(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) |
| 173 |
{ |
| 174 |
#define FORMAT "%10s %10s %50s %10s\n" |
| 175 |
#define FORMAT2 "%10s %10s %50s %10s\n" |
| 176 |
struct ast_imager *i; |
| 177 |
int count_fmt = 0; |
| 178 |
|
| 179 |
switch (cmd) { |
| 180 |
case CLI_INIT: |
| 181 |
e->command = "core show image formats"; |
| 182 |
e->usage = |
| 183 |
"Usage: core show image formats\n" |
| 184 |
" Displays currently registered image formats (if any).\n"; |
| 185 |
return NULL; |
| 186 |
case CLI_GENERATE: |
| 187 |
return NULL; |
| 188 |
} |
| 189 |
if (a->argc != 4) |
| 190 |
return CLI_SHOWUSAGE; |
| 191 |
ast_cli(a->fd, FORMAT, "Name", "Extensions", "Description", "Format"); |
| 192 |
ast_cli(a->fd, FORMAT, "----", "----------", "-----------", "------"); |
| 193 |
AST_RWLIST_RDLOCK(&imagers); |
| 194 |
AST_RWLIST_TRAVERSE(&imagers, i, list) { |
| 195 |
ast_cli(a->fd, FORMAT2, i->name, i->exts, i->desc, ast_format_get_name(i->format)); |
| 196 |
count_fmt++; |
| 197 |
} |
| 198 |
AST_RWLIST_UNLOCK(&imagers); |
| 199 |
ast_cli(a->fd, "\n%d image format%s registered.\n", count_fmt, count_fmt == 1 ? "" : "s"); |
| 200 |
return CLI_SUCCESS; |
| 201 |
} |
| 202 |
|
| 203 |
static struct ast_cli_entry cli_image[] = { |
| 204 |
AST_CLI_DEFINE(handle_core_show_image_formats, "Displays image formats") |
| 205 |
}; |
| 206 |
|
| 207 |
static void image_shutdown(void) |
| 208 |
{ |
| 209 |
ast_cli_unregister_multiple(cli_image, ARRAY_LEN(cli_image)); |
| 210 |
} |
| 211 |
|
| 212 |
int ast_image_init(void) |
| 213 |
{ |
| 214 |
ast_cli_register_multiple(cli_image, ARRAY_LEN(cli_image)); |
| 215 |
ast_register_cleanup(image_shutdown); |
| 216 |
return 0; |
| 217 |
} |