2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2006, Digium, Inc.
6 * Mark Spencer <markster@digium.com>
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
21 * \brief Image Management
23 * \author Mark Spencer <markster@digium.com>
28 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
39 #include "asterisk/sched.h"
40 #include "asterisk/options.h"
41 #include "asterisk/channel.h"
42 #include "asterisk/logger.h"
43 #include "asterisk/file.h"
44 #include "asterisk/image.h"
45 #include "asterisk/translate.h"
46 #include "asterisk/cli.h"
47 #include "asterisk/lock.h"
49 /* XXX Why don't we just use the formats struct for this? */
50 static AST_RWLIST_HEAD_STATIC(imagers, ast_imager);
52 int ast_image_register(struct ast_imager *img)
54 ast_verb(2, "Registered format '%s' (%s)\n", img->name, img->desc);
55 AST_RWLIST_WRLOCK(&imagers);
56 AST_RWLIST_INSERT_HEAD(&imagers, img, list);
57 AST_RWLIST_UNLOCK(&imagers);
61 void ast_image_unregister(struct ast_imager *img)
65 AST_RWLIST_WRLOCK(&imagers);
66 AST_RWLIST_TRAVERSE_SAFE_BEGIN(&imagers, i, list) {
68 AST_RWLIST_REMOVE_CURRENT(&imagers, list);
72 AST_RWLIST_TRAVERSE_SAFE_END
73 AST_RWLIST_UNLOCK(&imagers);
75 ast_verb(2, "Unregistered format '%s' (%s)\n", img->name, img->desc);
78 int ast_supports_images(struct ast_channel *chan)
80 if (!chan || !chan->tech)
82 if (!chan->tech->send_image)
87 static int file_exists(char *filename)
91 res = stat(filename, &st);
97 static void make_filename(char *buf, int len, char *filename, const char *preflang, char *ext)
99 if (filename[0] == '/') {
100 if (!ast_strlen_zero(preflang))
101 snprintf(buf, len, "%s-%s.%s", filename, preflang, ext);
103 snprintf(buf, len, "%s.%s", filename, ext);
105 if (!ast_strlen_zero(preflang))
106 snprintf(buf, len, "%s/%s/%s-%s.%s", ast_config_AST_DATA_DIR, "images", filename, preflang, ext);
108 snprintf(buf, len, "%s/%s/%s.%s", ast_config_AST_DATA_DIR, "images", filename, ext);
112 struct ast_frame *ast_read_image(char *filename, const char *preflang, int format)
114 struct ast_imager *i;
118 struct ast_imager *found = NULL;
121 struct ast_frame *f = NULL;
123 AST_RWLIST_RDLOCK(&imagers);
124 AST_RWLIST_TRAVERSE(&imagers, i, list) {
125 if (i->format & format) {
127 ast_copy_string(tmp, i->exts, sizeof(tmp));
129 e = strsep(&stringp, "|");
131 make_filename(buf, sizeof(buf), filename, preflang, e);
132 if ((len = file_exists(buf))) {
136 make_filename(buf, sizeof(buf), filename, NULL, e);
137 if ((len = file_exists(buf))) {
141 e = strsep(&stringp, "|");
149 fd = open(buf, O_RDONLY);
151 if (!found->identify || found->identify(fd)) {
152 /* Reset file pointer */
153 lseek(fd, 0, SEEK_SET);
154 f = found->read_image(fd,len);
156 ast_log(LOG_WARNING, "%s does not appear to be a %s file\n", buf, found->name);
159 ast_log(LOG_WARNING, "Unable to open '%s': %s\n", buf, strerror(errno));
161 ast_log(LOG_WARNING, "Image file '%s' not found\n", filename);
163 AST_RWLIST_UNLOCK(&imagers);
168 int ast_send_image(struct ast_channel *chan, char *filename)
172 if (chan->tech->send_image) {
173 f = ast_read_image(filename, chan->language, -1);
175 res = chan->tech->send_image(chan, f);
182 static char *handle_core_show_image_formats(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
184 #define FORMAT "%10s %10s %50s %10s\n"
185 #define FORMAT2 "%10s %10s %50s %10s\n"
186 struct ast_imager *i;
191 e->command = "core show image formats";
193 "Usage: core show image formats\n"
194 " Displays currently registered image formats (if any).\n";
200 return CLI_SHOWUSAGE;
201 ast_cli(a->fd, FORMAT, "Name", "Extensions", "Description", "Format");
202 ast_cli(a->fd, FORMAT, "----", "----------", "-----------", "------");
203 AST_RWLIST_RDLOCK(&imagers);
204 AST_RWLIST_TRAVERSE(&imagers, i, list) {
205 ast_cli(a->fd, FORMAT2, i->name, i->exts, i->desc, ast_getformatname(i->format));
208 AST_RWLIST_UNLOCK(&imagers);
209 ast_cli(a->fd, "\n%d image format%s registered.\n", count_fmt, count_fmt == 1 ? "" : "s");
213 struct ast_cli_entry cli_image[] = {
214 AST_CLI_DEFINE(handle_core_show_image_formats, "Displays image formats")
217 int ast_image_init(void)
219 ast_cli_register_multiple(cli_image, sizeof(cli_image) / sizeof(struct ast_cli_entry));