Wed Jan 8 2020 09:49:47

Asterisk developer's documentation


format_jpeg.c
Go to the documentation of this file.
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999 - 2005, Digium, Inc.
5  *
6  * Mark Spencer <markster@digium.com>
7  *
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.
13  *
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.
17  */
18 
19 /*! \file
20  *
21  * \brief JPEG File format support.
22  *
23  * \arg File name extension: jpeg, jpg
24  * \ingroup formats
25  */
26 
27 /*** MODULEINFO
28  <support_level>extended</support_level>
29  ***/
30 
31 #include "asterisk.h"
32 
33 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 328209 $")
34 
35 #include "asterisk/mod_format.h"
36 #include "asterisk/module.h"
37 #include "asterisk/image.h"
38 #include "asterisk/endian.h"
39 
40 static struct ast_frame *jpeg_read_image(int fd, int len)
41 {
42  struct ast_frame fr;
43  int res;
44  char buf[65536];
45  if (len > sizeof(buf) || len < 0) {
46  ast_log(LOG_WARNING, "JPEG image too large to read\n");
47  return NULL;
48  }
49  res = read(fd, buf, len);
50  if (res < len) {
51  ast_log(LOG_WARNING, "Only read %d of %d bytes: %s\n", res, len, strerror(errno));
52  }
53  memset(&fr, 0, sizeof(fr));
56  fr.data.ptr = buf;
57  fr.src = "JPEG Read";
58  fr.datalen = len;
59  return ast_frisolate(&fr);
60 }
61 
62 static int jpeg_identify(int fd)
63 {
64  char buf[10];
65  int res;
66  res = read(fd, buf, sizeof(buf));
67  if (res < sizeof(buf))
68  return 0;
69  if (memcmp(buf + 6, "JFIF", 4))
70  return 0;
71  return 1;
72 }
73 
74 static int jpeg_write_image(int fd, struct ast_frame *fr)
75 {
76  int res=0;
77  if (fr->frametype != AST_FRAME_IMAGE) {
78  ast_log(LOG_WARNING, "Not an image\n");
79  return -1;
80  }
81  if (fr->subclass.codec != AST_FORMAT_JPEG) {
82  ast_log(LOG_WARNING, "Not a jpeg image\n");
83  return -1;
84  }
85  if (fr->datalen) {
86  res = write(fd, fr->data.ptr, fr->datalen);
87  if (res != fr->datalen) {
88  ast_log(LOG_WARNING, "Only wrote %d of %d bytes: %s\n", res, fr->datalen, strerror(errno));
89  return -1;
90  }
91  }
92  return res;
93 }
94 
95 static struct ast_imager jpeg_format = {
96  .name = "jpg",
97  .desc = "JPEG (Joint Picture Experts Group)",
98  .exts = "jpg|jpeg",
99  .format = AST_FORMAT_JPEG,
100  .read_image = jpeg_read_image,
101  .identify = jpeg_identify,
102  .write_image = jpeg_write_image,
103 };
104 
105 static int load_module(void)
106 {
107  if (ast_image_register(&jpeg_format))
110 }
111 
112 static int unload_module(void)
113 {
114  ast_image_unregister(&jpeg_format);
115 
116  return 0;
117 }
118 
119 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "jpeg (joint picture experts group) image format",
120  .load = load_module,
121  .unload = unload_module,
122  .load_pri = AST_MODPRI_APP_DEPEND
123 );
structure associated with registering an image format
Definition: image.h:27
union ast_frame_subclass subclass
Definition: frame.h:146
static struct ast_imager jpeg_format
Definition: format_jpeg.c:95
Asterisk main include file. File version handling, generic pbx functions.
static int jpeg_write_image(int fd, struct ast_frame *fr)
Definition: format_jpeg.c:74
static int unload_module(void)
Definition: format_jpeg.c:112
void * ptr
Definition: frame.h:160
static int load_module(void)
Definition: format_jpeg.c:105
#define LOG_WARNING
Definition: logger.h:144
static struct ast_frame * jpeg_read_image(int fd, int len)
Definition: format_jpeg.c:40
#define AST_MODULE_INFO(keystr, flags_to_set, desc, fields...)
Definition: module.h:374
struct ast_frame * ast_frisolate(struct ast_frame *fr)
Makes a frame independent of any static storage.
Definition: frame.c:391
format_t codec
Definition: frame.h:137
int ast_image_register(struct ast_imager *imgdrv)
Register image format.
Definition: image.c:50
General Asterisk channel definitions for image handling.
const char * src
Definition: frame.h:158
Asterisk architecture endianess compatibility definitions.
int datalen
Definition: frame.h:148
#define AST_FORMAT_JPEG
Definition: frame.h:276
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
int errno
Data structure associated with a single frame of data.
Definition: frame.h:142
char * name
Definition: image.h:28
enum ast_frame_type frametype
Definition: frame.h:144
void ast_image_unregister(struct ast_imager *imgdrv)
Unregister an image format.
Definition: image.c:59
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:38
Asterisk module definitions.
union ast_frame::@172 data
#define ASTERISK_FILE_VERSION(file, version)
Register/unregister a source code file with the core.
Definition: asterisk.h:180
static int jpeg_identify(int fd)
Definition: format_jpeg.c:62