Asterisk.org: Developer Documentation (28 Oct 2009)

Wed Oct 28 15:47:54 2009

## 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 <sys/types.h>
    00028 #include <netinet/in.h>
    00029 #include <arpa/inet.h>
    00030 #include <stdlib.h>
    00031 #include <sys/time.h>
    00032 #include <stdio.h>
    00033 #include <unistd.h>
    00034 #include <errno.h>
    00035 #include <string.h>
    00036
    00037 #include "asterisk.h"
    00038
    00039 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 18436 $")
    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 char *desc = "JPEG (Joint Picture Experts Group) Image Format";
    00051
    00052
    00053 static struct ast_frame *jpeg_read_image(int fd, int len)
    00054 {
    00055    struct ast_frame fr;
    00056    int res;
    00057    char buf[65536];
    00058    if (len > sizeof(buf) || len < 0) {
    00059       ast_log(LOG_WARNING, "JPEG image too large to read\n");
    00060       return NULL;
    00061    }
    00062    res = read(fd, buf, len);
    00063    if (res < len) {
    00064       ast_log(LOG_WARNING, "Only read %d of %d bytes: %s\n", res, len, strerror(errno));
    00065    }
    00066    memset(&fr, 0, sizeof(fr));
    00067    fr.frametype = AST_FRAME_IMAGE;
    00068    fr.subclass = AST_FORMAT_JPEG;
    00069    fr.data = buf;
    00070    fr.src = "JPEG Read";
    00071    fr.datalen = len;
    00072    return ast_frisolate(&fr);
    00073 }
    00074
    00075 static int jpeg_identify(int fd)
    00076 {
    00077    char buf[10];
    00078    int res;
    00079    res = read(fd, buf, sizeof(buf));
    00080    if (res < sizeof(buf))
    00081       return 0;
    00082    if (memcmp(buf + 6, "JFIF", 4))
    00083       return 0;
    00084    return 1;
    00085 }
    00086
    00087 static int jpeg_write_image(int fd, struct ast_frame *fr)
    00088 {
    00089    int res=0;
    00090    if (fr->frametype != AST_FRAME_IMAGE) {
    00091       ast_log(LOG_WARNING, "Not an image\n");
    00092       return -1;
    00093    }
    00094    if (fr->subclass != AST_FORMAT_JPEG) {
    00095       ast_log(LOG_WARNING, "Not a jpeg image\n");
    00096       return -1;
    00097    }
    00098    if (fr->datalen) {
    00099       res = write(fd, fr->data, fr->datalen);
    00100       if (res != fr->datalen) {
    00101          ast_log(LOG_WARNING, "Only wrote %d of %d bytes: %s\n", res, fr->datalen, strerror(errno));
    00102          return -1;
    00103       }
    00104    }
    00105    return res;
    00106 }
    00107
    00108 static struct ast_imager jpeg_format = {
    00109    "jpg",
    00110    "JPEG (Joint Picture Experts Group)",
    00111    "jpg|jpeg",
    00112    AST_FORMAT_JPEG,
    00113    jpeg_read_image,
    00114    jpeg_identify,
    00115    jpeg_write_image,
    00116 };
    00117
    00118 int load_module()
    00119 {
    00120    return ast_image_register(&jpeg_format);
    00121 }
    00122
    00123 int unload_module()
    00124 {
    00125    ast_image_unregister(&jpeg_format);
    00126    return 0;
    00127 }
    00128
    00129 int usecount()
    00130 {
    00131    /* We never really have any users */
    00132    return 0;
    00133 }
    00134
    00135 char *description()
    00136 {
    00137    return desc;
    00138 }
    00139
    00140
    00141 char *key()
    00142 {
    00143    return ASTERISK_GPL_KEY;
    00144 }