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 <unistd.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 <errno.h>
00038 #include <string.h>
00039
00040 #include "asterisk/lock.h"
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/endian.h"
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058 #define BUF_SIZE 32768
00059
00060 struct h263_desc {
00061 unsigned int lastts;
00062 };
00063
00064
00065 static int h263_open(struct ast_filestream *s)
00066 {
00067 unsigned int ts;
00068 int res;
00069
00070 if ((res = fread(&ts, 1, sizeof(ts), s->f)) < sizeof(ts)) {
00071 ast_log(LOG_WARNING, "Empty file!\n");
00072 return -1;
00073 }
00074 return 0;
00075 }
00076
00077 static struct ast_frame *h263_read(struct ast_filestream *s, int *whennext)
00078 {
00079 int res;
00080 int mark;
00081 unsigned short len;
00082 unsigned int ts;
00083 struct h263_desc *fs = (struct h263_desc *)s->_private;
00084
00085
00086 if ((res = fread(&len, 1, sizeof(len), s->f)) < 1)
00087 return NULL;
00088 len = ntohs(len);
00089 mark = (len & 0x8000) ? 1 : 0;
00090 len &= 0x7fff;
00091 if (len > BUF_SIZE) {
00092 ast_log(LOG_WARNING, "Length %d is too long\n", len);
00093 return NULL;
00094 }
00095 s->fr.frametype = AST_FRAME_VIDEO;
00096 s->fr.subclass = AST_FORMAT_H263;
00097 s->fr.mallocd = 0;
00098 AST_FRAME_SET_BUFFER(&s->fr, s->buf, AST_FRIENDLY_OFFSET, len);
00099 if ((res = fread(s->fr.data, 1, s->fr.datalen, s->f)) != s->fr.datalen) {
00100 if (res)
00101 ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
00102 return NULL;
00103 }
00104 s->fr.samples = fs->lastts;
00105 s->fr.datalen = len;
00106 s->fr.subclass |= mark;
00107 s->fr.delivery.tv_sec = 0;
00108 s->fr.delivery.tv_usec = 0;
00109 if ((res = fread(&ts, 1, sizeof(ts), s->f)) == sizeof(ts)) {
00110 fs->lastts = ntohl(ts);
00111 *whennext = fs->lastts * 4/45;
00112 } else
00113 *whennext = 0;
00114 return &s->fr;
00115 }
00116
00117 static int h263_write(struct ast_filestream *fs, struct ast_frame *f)
00118 {
00119 int res;
00120 unsigned int ts;
00121 unsigned short len;
00122 int subclass;
00123 int mark=0;
00124 if (f->frametype != AST_FRAME_VIDEO) {
00125 ast_log(LOG_WARNING, "Asked to write non-video frame!\n");
00126 return -1;
00127 }
00128 subclass = f->subclass;
00129 if (subclass & 0x1)
00130 mark=0x8000;
00131 subclass &= ~0x1;
00132 if (subclass != AST_FORMAT_H263) {
00133 ast_log(LOG_WARNING, "Asked to write non-h263 frame (%d)!\n", f->subclass);
00134 return -1;
00135 }
00136 ts = htonl(f->samples);
00137 if ((res = fwrite(&ts, 1, sizeof(ts), fs->f)) != sizeof(ts)) {
00138 ast_log(LOG_WARNING, "Bad write (%d/4): %s\n", res, strerror(errno));
00139 return -1;
00140 }
00141 len = htons(f->datalen | mark);
00142 if ((res = fwrite(&len, 1, sizeof(len), fs->f)) != sizeof(len)) {
00143 ast_log(LOG_WARNING, "Bad write (%d/2): %s\n", res, strerror(errno));
00144 return -1;
00145 }
00146 if ((res = fwrite(f->data, 1, f->datalen, fs->f)) != f->datalen) {
00147 ast_log(LOG_WARNING, "Bad write (%d/%d): %s\n", res, f->datalen, strerror(errno));
00148 return -1;
00149 }
00150 return 0;
00151 }
00152
00153 static int h263_seek(struct ast_filestream *fs, off_t sample_offset, int whence)
00154 {
00155
00156 return -1;
00157 }
00158
00159 static int h263_trunc(struct ast_filestream *fs)
00160 {
00161
00162 if (ftruncate(fileno(fs->f), ftello(fs->f)) < 0)
00163 return -1;
00164 return 0;
00165 }
00166
00167 static off_t h263_tell(struct ast_filestream *fs)
00168 {
00169 off_t offset = ftello(fs->f);
00170 return offset;
00171 }
00172
00173 static const struct ast_format h263_f = {
00174 .name = "h263",
00175 .exts = "h263",
00176 .format = AST_FORMAT_H263,
00177 .open = h263_open,
00178 .write = h263_write,
00179 .seek = h263_seek,
00180 .trunc = h263_trunc,
00181 .tell = h263_tell,
00182 .read = h263_read,
00183 .buf_size = BUF_SIZE + AST_FRIENDLY_OFFSET,
00184 .desc_size = sizeof(struct h263_desc),
00185 };
00186
00187 static int load_module(void)
00188 {
00189 return ast_format_register(&h263_f);
00190 }
00191
00192 static int unload_module(void)
00193 {
00194 return ast_format_unregister(h263_f.name);
00195 }
00196
00197 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_FIRST, "Raw H.263 data",
00198 .load = load_module,
00199 .unload = unload_module,
00200 );