Wed Jan 8 2020 09:50:13

Asterisk developer's documentation


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"

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, int 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 } , }
 

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

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

Function Documentation

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 213 of file image.c.

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

Referenced by main().

214 {
217  return 0;
218 }
static void image_shutdown(void)
Definition: image.c:208
#define ARRAY_LEN(a)
Definition: isdn_lib.c:42
static struct ast_cli_entry cli_image[]
Definition: image.c:204
int ast_register_atexit(void(*func)(void))
Register a function to be executed before Asterisk exits.
Definition: asterisk.c:998
int ast_cli_register_multiple(struct ast_cli_entry *e, int len)
Register multiple commands.
Definition: cli.c:2167
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 50 of file image.c.

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

Referenced by load_module().

51 {
53  AST_RWLIST_INSERT_HEAD(&imagers, img, list);
55  ast_verb(2, "Registered format '%s' (%s)\n", img->name, img->desc);
56  return 0;
57 }
#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:243
#define AST_RWLIST_INSERT_HEAD
Definition: linkedlists.h:703
char * desc
Definition: image.h:29
char * name
Definition: image.h:28
Definition: image.c:48
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 59 of file image.c.

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

Referenced by unload_module().

60 {
62  img = AST_RWLIST_REMOVE(&imagers, img, list);
64 
65  if (img)
66  ast_verb(2, "Unregistered format '%s' (%s)\n", img->name, img->desc);
67 }
#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:243
char * desc
Definition: image.h:29
char * name
Definition: image.h:28
Definition: image.c:48
#define AST_RWLIST_REMOVE
Definition: linkedlists.h:870
struct ast_frame* ast_read_image ( const char *  filename,
const char *  preflang,
int  format 
)

Make an image.

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

Definition at line 103 of file image.c.

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

Referenced by ast_send_image().

104 {
105  struct ast_imager *i;
106  char buf[256];
107  char tmp[80];
108  char *e;
109  struct ast_imager *found = NULL;
110  int fd;
111  int len=0;
112  struct ast_frame *f = NULL;
113 
115  AST_RWLIST_TRAVERSE(&imagers, i, list) {
116  if (i->format & format) {
117  char *stringp=NULL;
118  ast_copy_string(tmp, i->exts, sizeof(tmp));
119  stringp = tmp;
120  e = strsep(&stringp, "|");
121  while (e) {
122  make_filename(buf, sizeof(buf), filename, preflang, e);
123  if ((len = file_exists(buf))) {
124  found = i;
125  break;
126  }
127  make_filename(buf, sizeof(buf), filename, NULL, e);
128  if ((len = file_exists(buf))) {
129  found = i;
130  break;
131  }
132  e = strsep(&stringp, "|");
133  }
134  }
135  if (found)
136  break;
137  }
138 
139  if (found) {
140  fd = open(buf, O_RDONLY);
141  if (fd > -1) {
142  if (!found->identify || found->identify(fd)) {
143  /* Reset file pointer */
144  lseek(fd, 0, SEEK_SET);
145  f = found->read_image(fd, len);
146  } else
147  ast_log(LOG_WARNING, "%s does not appear to be a %s file\n", buf, found->name);
148  close(fd);
149  } else
150  ast_log(LOG_WARNING, "Unable to open '%s': %s\n", buf, strerror(errno));
151  } else
152  ast_log(LOG_WARNING, "Image file '%s' not found\n", filename);
153 
155 
156  return f;
157 }
structure associated with registering an image format
Definition: image.h:27
static int file_exists(char *filename)
Definition: image.c:78
char * strsep(char **str, const char *delims)
int(* identify)(int fd)
Definition: image.h:33
#define LOG_WARNING
Definition: logger.h:144
#define AST_RWLIST_UNLOCK(head)
Attempts to unlock a read/write based list.
Definition: linkedlists.h:150
static void make_filename(char *buf, int len, const char *filename, const char *preflang, char *ext)
Definition: image.c:88
int format
Definition: image.h:31
#define AST_RWLIST_RDLOCK(head)
Read locks a list.
Definition: linkedlists.h:77
#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)
void ast_log(int level, const char *file, int line, const char *function, const char *fmt,...)
Used for sending a log message This is the standard logger function. Probably the only way you will i...
Definition: logger.c:1207
struct ast_frame *(* read_image)(int fd, int len)
Definition: image.h:32
int errno
static struct ast_format f[]
Definition: format_g726.c:181
void ast_copy_string(char *dst, const char *src, size_t size)
Size-limited null-terminating string copy.
Definition: strings.h:223
Data structure associated with a single frame of data.
Definition: frame.h:142
char * name
Definition: image.h:28
Definition: image.c:48
static snd_pcm_format_t format
Definition: chan_alsa.c:93
char * exts
Definition: image.h:30
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 159 of file image.c.

References ast_frfree, ast_read_image(), f, ast_channel::language, ast_channel_tech::send_image, and ast_channel::tech.

Referenced by handle_sendimage(), and sendimage_exec().

160 {
161  struct ast_frame *f;
162  int res = -1;
163  if (chan->tech->send_image) {
164  f = ast_read_image(filename, chan->language, -1);
165  if (f) {
166  res = chan->tech->send_image(chan, f);
167  ast_frfree(f);
168  }
169  }
170  return res;
171 }
int(*const send_image)(struct ast_channel *chan, struct ast_frame *frame)
Display or send an image.
Definition: channel.h:560
struct ast_frame * ast_read_image(const char *filename, const char *preflang, int format)
Make an image.
Definition: image.c:103
static struct ast_format f[]
Definition: format_g726.c:181
Data structure associated with a single frame of data.
Definition: frame.h:142
#define ast_frfree(fr)
Definition: frame.h:583
struct ast_channel_tech * tech
Definition: channel.h:743
const ast_string_field language
Definition: channel.h:787
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 69 of file image.c.

References ast_channel_tech::send_image, and ast_channel::tech.

Referenced by sendimage_exec().

70 {
71  if (!chan || !chan->tech)
72  return 0;
73  if (!chan->tech->send_image)
74  return 0;
75  return 1;
76 }
int(*const send_image)(struct ast_channel *chan, struct ast_frame *frame)
Display or send an image.
Definition: channel.h:560
struct ast_channel_tech * tech
Definition: channel.h:743
static int file_exists ( char *  filename)
static

Definition at line 78 of file image.c.

Referenced by ast_read_image().

79 {
80  int res;
81  struct stat st;
82  res = stat(filename, &st);
83  if (!res)
84  return st.st_size;
85  return 0;
86 }
static char* handle_core_show_image_formats ( struct ast_cli_entry e,
int  cmd,
struct ast_cli_args a 
)
static

Definition at line 173 of file image.c.

References ast_cli_args::argc, ast_cli(), ast_getformatname(), 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, and ast_cli_entry::usage.

174 {
175 #define FORMAT "%10s %10s %50s %10s\n"
176 #define FORMAT2 "%10s %10s %50s %10s\n"
177  struct ast_imager *i;
178  int count_fmt = 0;
179 
180  switch (cmd) {
181  case CLI_INIT:
182  e->command = "core show image formats";
183  e->usage =
184  "Usage: core show image formats\n"
185  " Displays currently registered image formats (if any).\n";
186  return NULL;
187  case CLI_GENERATE:
188  return NULL;
189  }
190  if (a->argc != 4)
191  return CLI_SHOWUSAGE;
192  ast_cli(a->fd, FORMAT, "Name", "Extensions", "Description", "Format");
193  ast_cli(a->fd, FORMAT, "----", "----------", "-----------", "------");
195  AST_RWLIST_TRAVERSE(&imagers, i, list) {
196  ast_cli(a->fd, FORMAT2, i->name, i->exts, i->desc, ast_getformatname(i->format));
197  count_fmt++;
198  }
200  ast_cli(a->fd, "\n%d image format%s registered.\n", count_fmt, count_fmt == 1 ? "" : "s");
201  return CLI_SUCCESS;
202 }
structure associated with registering an image format
Definition: image.h:27
#define FORMAT2
const int argc
Definition: cli.h:154
#define AST_RWLIST_UNLOCK(head)
Attempts to unlock a read/write based list.
Definition: linkedlists.h:150
Definition: cli.h:146
void ast_cli(int fd, const char *fmt,...)
Definition: cli.c:105
int format
Definition: image.h:31
#define AST_RWLIST_RDLOCK(head)
Read locks a list.
Definition: linkedlists.h:77
const int fd
Definition: cli.h:153
#define AST_RWLIST_TRAVERSE
Definition: linkedlists.h:493
#define FORMAT
char * ast_getformatname(format_t format)
Get the name of a format.
Definition: frame.c:578
#define CLI_SHOWUSAGE
Definition: cli.h:44
char * command
Definition: cli.h:180
const char * usage
Definition: cli.h:171
#define CLI_SUCCESS
Definition: cli.h:43
char * desc
Definition: image.h:29
char * name
Definition: image.h:28
Definition: image.c:48
char * exts
Definition: image.h:30
static void image_shutdown ( void  )
static

Definition at line 208 of file image.c.

References ARRAY_LEN, and ast_cli_unregister_multiple().

Referenced by ast_image_init().

209 {
211 }
#define ARRAY_LEN(a)
Definition: isdn_lib.c:42
static struct ast_cli_entry cli_image[]
Definition: image.c:204
int ast_cli_unregister_multiple(struct ast_cli_entry *e, int len)
Unregister multiple commands.
Definition: cli.c:2177
static void make_filename ( char *  buf,
int  len,
const char *  filename,
const char *  preflang,
char *  ext 
)
static

Definition at line 88 of file image.c.

References ast_config_AST_DATA_DIR, and ast_strlen_zero().

Referenced by ast_read_image().

89 {
90  if (filename[0] == '/') {
91  if (!ast_strlen_zero(preflang))
92  snprintf(buf, len, "%s-%s.%s", filename, preflang, ext);
93  else
94  snprintf(buf, len, "%s.%s", filename, ext);
95  } else {
96  if (!ast_strlen_zero(preflang))
97  snprintf(buf, len, "%s/%s/%s-%s.%s", ast_config_AST_DATA_DIR, "images", filename, preflang, ext);
98  else
99  snprintf(buf, len, "%s/%s/%s.%s", ast_config_AST_DATA_DIR, "images", filename, ext);
100  }
101 }
const char * ext
Definition: http.c:112
static force_inline int attribute_pure ast_strlen_zero(const char *s)
Definition: strings.h:63
const char * ast_config_AST_DATA_DIR
Definition: asterisk.c:262
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)

Variable Documentation

struct ast_cli_entry cli_image[]
static
Initial value:
= {
}
#define AST_CLI_DEFINE(fn, txt,...)
Definition: cli.h:191
static char * handle_core_show_image_formats(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
Definition: image.c:173

Definition at line 204 of file image.c.

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