Sat Aug 6 00:39:28 2011

Asterisk developer's documentation


format_jpeg.c

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 1999 - 2005, Digium, Inc.
00005  *
00006  * Mark Spencer <markster@digium.com>
00007  *
00008  * See http://www.asterisk.org for more information about
00009  * the Asterisk project. Please do not directly contact
00010  * any of the maintainers of this project for assistance;
00011  * the project provides a web site, mailing lists and IRC
00012  * channels for your use.
00013  *
00014  * This program is free software, distributed under the terms of
00015  * the GNU General Public License Version 2. See the LICENSE file
00016  * at the top of the source tree.
00017  */
00018 
00019 /*! \file
00020  *
00021  * \brief JPEG File format support.
00022  * 
00023  * \arg File name extension: jpeg, jpg
00024  * \ingroup formats
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 );

Generated on Sat Aug 6 00:39:28 2011 for Asterisk - the Open Source PBX by  doxygen 1.4.7