format_jpeg.c
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #include "asterisk.h"
00028
00029 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 233782 $")
00030
00031 #include <sys/types.h>
00032 #include <netinet/in.h>
00033 #include <arpa/inet.h>
00034 #include <stdlib.h>
00035 #include <sys/time.h>
00036 #include <stdio.h>
00037 #include <unistd.h>
00038 #include <errno.h>
00039 #include <string.h>
00040
00041 #include "asterisk/channel.h"
00042 #include "asterisk/file.h"
00043 #include "asterisk/logger.h"
00044 #include "asterisk/sched.h"
00045 #include "asterisk/module.h"
00046 #include "asterisk/image.h"
00047 #include "asterisk/lock.h"
00048 #include "asterisk/endian.h"
00049
00050 static struct ast_frame *jpeg_read_image(int fd, int len)
00051 {
00052 struct ast_frame fr;
00053 int res;
00054 char buf[65536];
00055 if (len > sizeof(buf) || len < 0) {
00056 ast_log(LOG_WARNING, "JPEG image too large to read\n");
00057 return NULL;
00058 }
00059 res = read(fd, buf, len);
00060 if (res < len) {
00061 ast_log(LOG_WARNING, "Only read %d of %d bytes: %s\n", res, len, strerror(errno));
00062 }
00063 memset(&fr, 0, sizeof(fr));
00064 fr.frametype = AST_FRAME_IMAGE;
00065 fr.subclass = AST_FORMAT_JPEG;
00066 fr.data = buf;
00067 fr.src = "JPEG Read";
00068 fr.datalen = len;
00069 return ast_frisolate(&fr);
00070 }
00071
00072 static int jpeg_identify(int fd)
00073 {
00074 char buf[10];
00075 int res;
00076 res = read(fd, buf, sizeof(buf));
00077 if (res < sizeof(buf))
00078 return 0;
00079 if (memcmp(buf + 6, "JFIF", 4))
00080 return 0;
00081 return 1;
00082 }
00083
00084 static int jpeg_write_image(int fd, struct ast_frame *fr)
00085 {
00086 int res=0;
00087 if (fr->frametype != AST_FRAME_IMAGE) {
00088 ast_log(LOG_WARNING, "Not an image\n");
00089 return -1;
00090 }
00091 if (fr->subclass != AST_FORMAT_JPEG) {
00092 ast_log(LOG_WARNING, "Not a jpeg image\n");
00093 return -1;
00094 }
00095 if (fr->datalen) {
00096 res = write(fd, fr->data, fr->datalen);
00097 if (res != fr->datalen) {
00098 ast_log(LOG_WARNING, "Only wrote %d of %d bytes: %s\n", res, fr->datalen, strerror(errno));
00099 return -1;
00100 }
00101 }
00102 return res;
00103 }
00104
00105 static struct ast_imager jpeg_format = {
00106 "jpg",
00107 "JPEG (Joint Picture Experts Group)",
00108 "jpg|jpeg",
00109 AST_FORMAT_JPEG,
00110 jpeg_read_image,
00111 jpeg_identify,
00112 jpeg_write_image,
00113 };
00114
00115 static int load_module(void)
00116 {
00117 return ast_image_register(&jpeg_format);
00118 }
00119
00120 static int unload_module(void)
00121 {
00122 ast_image_unregister(&jpeg_format);
00123
00124 return 0;
00125 }
00126
00127 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_FIRST, "JPEG (Joint Picture Experts Group) Image Format",
00128 .load = load_module,
00129 .unload = unload_module,
00130 );