Asterisk - The Open Source Telephony Project  18.5.0
Data Structures | Macros | Functions | Variables
image.c File Reference

Image Management. More...

#include "asterisk.h"
#include <sys/time.h>
#include <sys/stat.h>
#include <signal.h>
#include "asterisk/paths.h"
#include "asterisk/sched.h"
#include "asterisk/channel.h"
#include "asterisk/file.h"
#include "asterisk/image.h"
#include "asterisk/translate.h"
#include "asterisk/cli.h"
#include "asterisk/lock.h"
Include dependency graph for image.c:

Go to the source code of this file.

Data Structures

struct  imagers
 

Macros

#define FORMAT   "%10s %10s %50s %10s\n"
 
#define FORMAT2   "%10s %10s %50s %10s\n"
 

Functions

int ast_image_init (void)
 Initialize image stuff Initializes all the various image stuff. Basically just registers the cli stuff. More...
 
int ast_image_register (struct ast_imager *img)
 Register image format. More...
 
void ast_image_unregister (struct ast_imager *img)
 Unregister an image format. More...
 
struct ast_frameast_read_image (const char *filename, const char *preflang, struct ast_format *format)
 Make an image. More...
 
int ast_send_image (struct ast_channel *chan, const char *filename)
 Sends an image. More...
 
int ast_supports_images (struct ast_channel *chan)
 Check for image support on a channel. More...
 
static int file_exists (char *filename)
 
static char * handle_core_show_image_formats (struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
 
static void image_shutdown (void)
 
static void make_filename (char *buf, int len, const char *filename, const char *preflang, char *ext)
 

Variables

static struct ast_cli_entry cli_image []
 
static struct imagers imagers = { .first = NULL, .last = NULL, .lock = { PTHREAD_RWLOCK_INITIALIZER , NULL, {1, 0} } , }
 

Detailed Description

Image Management.

Author
Mark Spencer marks.nosp@m.ter@.nosp@m.digiu.nosp@m.m.co.nosp@m.m

Definition in file image.c.

Macro Definition Documentation

◆ FORMAT

#define FORMAT   "%10s %10s %50s %10s\n"

◆ FORMAT2

#define FORMAT2   "%10s %10s %50s %10s\n"

Function Documentation

◆ ast_image_init()

int ast_image_init ( void  )

Initialize image stuff Initializes all the various image stuff. Basically just registers the cli stuff.

Returns
0 all the time

Definition at line 212 of file image.c.

References ARRAY_LEN, ast_cli_register_multiple, ast_register_cleanup(), and image_shutdown().

Referenced by asterisk_daemon().

213 {
216  return 0;
217 }
static void image_shutdown(void)
Definition: image.c:207
#define ARRAY_LEN(a)
Definition: isdn_lib.c:42
static struct ast_cli_entry cli_image[]
Definition: image.c:203
#define ast_cli_register_multiple(e, len)
Register multiple commands.
Definition: cli.h:265
int ast_register_cleanup(void(*func)(void))
Register a function to be executed before Asterisk gracefully exits.
Definition: clicompat.c:19

◆ ast_image_register()

int ast_image_register ( struct ast_imager imgdrv)

Register image format.

Parameters
imgdrvPopulated ast_imager structure with info to register Registers an image format
Returns
0 regardless

Definition at line 48 of file image.c.

References AST_RWLIST_INSERT_HEAD, AST_RWLIST_UNLOCK, AST_RWLIST_WRLOCK, ast_verb, ast_imager::desc, and ast_imager::name.

49 {
51  AST_RWLIST_INSERT_HEAD(&imagers, img, list);
53  ast_verb(2, "Registered format '%s' (%s)\n", img->name, img->desc);
54  return 0;
55 }
#define AST_RWLIST_WRLOCK(head)
Write locks a list.
Definition: linkedlists.h:51
#define AST_RWLIST_UNLOCK(head)
Attempts to unlock a read/write based list.
Definition: linkedlists.h:150
#define ast_verb(level,...)
Definition: logger.h:463
#define AST_RWLIST_INSERT_HEAD
Definition: linkedlists.h:717
char * desc
Definition: image.h:29
char * name
Definition: image.h:28
Definition: image.c:46

◆ ast_image_unregister()

void ast_image_unregister ( struct ast_imager imgdrv)

Unregister an image format.

Parameters
imgdrvpointer to the ast_imager structure you wish to unregister Unregisters the image format passed in. Returns nothing

Definition at line 57 of file image.c.

References AST_RWLIST_REMOVE, AST_RWLIST_UNLOCK, AST_RWLIST_WRLOCK, ast_verb, ast_imager::desc, and ast_imager::name.

58 {
60  img = AST_RWLIST_REMOVE(&imagers, img, list);
62 
63  if (img)
64  ast_verb(2, "Unregistered format '%s' (%s)\n", img->name, img->desc);
65 }
#define AST_RWLIST_WRLOCK(head)
Write locks a list.
Definition: linkedlists.h:51
#define AST_RWLIST_UNLOCK(head)
Attempts to unlock a read/write based list.
Definition: linkedlists.h:150
#define ast_verb(level,...)
Definition: logger.h:463
char * desc
Definition: image.h:29
char * name
Definition: image.h:28
Definition: image.c:46
#define AST_RWLIST_REMOVE
Definition: linkedlists.h:884

◆ ast_read_image()

struct ast_frame* ast_read_image ( const char *  filename,
const char *  preflang,
struct ast_format format 
)

Make an image.

Parameters
filenamefilename of image to prepare
preflangpreferred language to get the image...?
formatthe format of the file, NULL for any image format Make an image from a filename ??? No estoy positivo
Return values
anast_frame on success
NULLon failure

Definition at line 101 of file image.c.

References ast_copy_string(), ast_format_cmp(), AST_FORMAT_CMP_EQUAL, ast_log, AST_RWLIST_RDLOCK, AST_RWLIST_TRAVERSE, AST_RWLIST_UNLOCK, buf, errno, ast_imager::exts, file_exists(), ast_imager::format, ast_imager::identify, len(), LOG_WARNING, make_filename(), ast_imager::name, NULL, ast_imager::read_image, strsep(), and tmp().

Referenced by ast_send_image().

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 
113  AST_RWLIST_TRAVERSE(&imagers, i, list) {
114  /* if NULL image format, just pick the first one, otherwise match it. */
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  /* Reset file pointer */
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 
154 
155  return f;
156 }
structure associated with registering an image format
Definition: image.h:27
static int file_exists(char *filename)
Definition: image.c:76
char buf[BUFSIZE]
Definition: eagi_proxy.c:66
#define LOG_WARNING
Definition: logger.h:274
static int tmp()
Definition: bt_open.c:389
#define AST_RWLIST_UNLOCK(head)
Attempts to unlock a read/write based list.
Definition: linkedlists.h:150
#define NULL
Definition: resample.c:96
static void make_filename(char *buf, int len, const char *filename, const char *preflang, char *ext)
Definition: image.c:86
struct ast_frame *(* read_image)(int fd, int len)
Definition: image.h:32
#define AST_RWLIST_RDLOCK(head)
Read locks a list.
Definition: linkedlists.h:77
#define ast_log
Definition: astobj2.c:42
enum ast_format_cmp_res ast_format_cmp(const struct ast_format *format1, const struct ast_format *format2)
Compare two formats.
Definition: format.c:201
#define AST_RWLIST_TRAVERSE
Definition: linkedlists.h:493
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
int errno
int(* identify)(int fd)
Definition: image.h:33
char * strsep(char **str, const char *delims)
void ast_copy_string(char *dst, const char *src, size_t size)
Size-limited null-terminating string copy.
Definition: strings.h:401
Data structure associated with a single frame of data.
char * name
Definition: image.h:28
Definition: image.c:46
struct ast_format * format
Definition: image.h:31
char * exts
Definition: image.h:30

◆ ast_send_image()

int ast_send_image ( struct ast_channel chan,
const char *  filename 
)

Sends an image.

Parameters
chanchannel to send image on
filenamefilename of image to send (minus extension) Sends an image on the given channel.
Return values
0on success
-1on error

Definition at line 158 of file image.c.

References ast_channel_language(), ast_channel_tech(), ast_frfree, ast_read_image(), NULL, and ast_channel_tech::send_image.

Referenced by handle_sendimage(), and sendimage_exec().

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 }
int(*const send_image)(struct ast_channel *chan, struct ast_frame *frame)
Display or send an image.
Definition: channel.h:760
#define NULL
Definition: resample.c:96
struct ast_frame * ast_read_image(const char *filename, const char *preflang, struct ast_format *format)
Make an image.
Definition: image.c:101
#define ast_frfree(fr)
Data structure associated with a single frame of data.
const char * ast_channel_language(const struct ast_channel *chan)
const struct ast_channel_tech * ast_channel_tech(const struct ast_channel *chan)

◆ ast_supports_images()

int ast_supports_images ( struct ast_channel chan)

Check for image support on a channel.

Parameters
chanchannel to check Checks the channel to see if it supports the transmission of images
Returns
non-zero if image transmission is supported

Definition at line 67 of file image.c.

References ast_channel_tech().

Referenced by sendimage_exec().

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 }
const struct ast_channel_tech * ast_channel_tech(const struct ast_channel *chan)

◆ file_exists()

static int file_exists ( char *  filename)
static

Definition at line 76 of file image.c.

Referenced by ast_read_image().

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 }

◆ handle_core_show_image_formats()

static char* handle_core_show_image_formats ( struct ast_cli_entry e,
int  cmd,
struct ast_cli_args a 
)
static

Definition at line 172 of file image.c.

References ast_cli_args::argc, ast_cli(), ast_format_get_name(), AST_RWLIST_RDLOCK, AST_RWLIST_TRAVERSE, AST_RWLIST_UNLOCK, CLI_GENERATE, CLI_INIT, CLI_SHOWUSAGE, CLI_SUCCESS, ast_cli_entry::command, ast_imager::desc, ast_imager::exts, ast_cli_args::fd, ast_imager::format, FORMAT, FORMAT2, ast_imager::name, NULL, and ast_cli_entry::usage.

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, "----", "----------", "-----------", "------");
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  }
199  ast_cli(a->fd, "\n%d image format%s registered.\n", count_fmt, count_fmt == 1 ? "" : "s");
200  return CLI_SUCCESS;
201 }
structure associated with registering an image format
Definition: image.h:27
#define FORMAT2
const int argc
Definition: cli.h:160
#define AST_RWLIST_UNLOCK(head)
Attempts to unlock a read/write based list.
Definition: linkedlists.h:150
Definition: cli.h:152
const char * ast_format_get_name(const struct ast_format *format)
Get the name associated with a format.
Definition: format.c:334
#define NULL
Definition: resample.c:96
void ast_cli(int fd, const char *fmt,...)
Definition: clicompat.c:6
#define AST_RWLIST_RDLOCK(head)
Read locks a list.
Definition: linkedlists.h:77
const int fd
Definition: cli.h:159
#define AST_RWLIST_TRAVERSE
Definition: linkedlists.h:493
#define FORMAT
#define CLI_SHOWUSAGE
Definition: cli.h:45
char * command
Definition: cli.h:186
const char * usage
Definition: cli.h:177
#define CLI_SUCCESS
Definition: cli.h:44
char * desc
Definition: image.h:29
char * name
Definition: image.h:28
Definition: image.c:46
struct ast_format * format
Definition: image.h:31
char * exts
Definition: image.h:30

◆ image_shutdown()

static void image_shutdown ( void  )
static

Definition at line 207 of file image.c.

References ARRAY_LEN, and ast_cli_unregister_multiple().

Referenced by ast_image_init().

208 {
210 }
#define ARRAY_LEN(a)
Definition: isdn_lib.c:42
static struct ast_cli_entry cli_image[]
Definition: image.c:203
int ast_cli_unregister_multiple(struct ast_cli_entry *e, int len)
Unregister multiple commands.
Definition: clicompat.c:30

◆ make_filename()

static void make_filename ( char *  buf,
int  len,
const char *  filename,
const char *  preflang,
char *  ext 
)
static

Definition at line 86 of file image.c.

References ast_config_AST_DATA_DIR, and ast_strlen_zero.

Referenced by ast_read_image().

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 }
char buf[BUFSIZE]
Definition: eagi_proxy.c:66
const char * ext
Definition: http.c:147
#define ast_strlen_zero(foo)
Definition: strings.h:52
const char * ast_config_AST_DATA_DIR
Definition: options.c:158
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)

Variable Documentation

◆ cli_image

struct ast_cli_entry cli_image[]
static
Initial value:
= {
{ .handler = handle_core_show_image_formats , .summary = "Displays image formats" ,}
}
static char * handle_core_show_image_formats(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
Definition: image.c:172

Definition at line 203 of file image.c.

◆ imagers

struct imagers imagers = { .first = NULL, .last = NULL, .lock = { PTHREAD_RWLOCK_INITIALIZER , NULL, {1, 0} } , }
static