| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
#include "asterisk.h" |
| 32 |
|
| 33 |
#include "asterisk/mod_format.h" |
| 34 |
#include "asterisk/module.h" |
| 35 |
#include "asterisk/endian.h" |
| 36 |
#include "asterisk/format_cache.h" |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
#define BUF_SIZE 32768 |
| 49 |
|
| 50 |
#define FRAME_ENDED 0x8000 |
| 51 |
|
| 52 |
struct h263_desc { |
| 53 |
unsigned int lastts; |
| 54 |
}; |
| 55 |
|
| 56 |
|
| 57 |
static int h263_open(struct ast_filestream *s) |
| 58 |
{ |
| 59 |
unsigned int ts; |
| 60 |
|
| 61 |
if (fread(&ts, 1, sizeof(ts), s->f) != sizeof(ts)) { |
| 62 |
ast_log(LOG_WARNING, "Empty file!\n"); |
| 63 |
return -1; |
| 64 |
} |
| 65 |
return 0; |
| 66 |
} |
| 67 |
|
| 68 |
static struct ast_frame *h263_read(struct ast_filestream *s, int *whennext) |
| 69 |
{ |
| 70 |
size_t res; |
| 71 |
uint32_t mark; |
| 72 |
unsigned short len; |
| 73 |
unsigned int ts; |
| 74 |
struct h263_desc *fs = (struct h263_desc *)s->_private; |
| 75 |
|
| 76 |
|
| 77 |
if ((res = fread(&len, 1, sizeof(len), s->f)) != sizeof(len)) |
| 78 |
return NULL; |
| 79 |
len = ntohs(len); |
| 80 |
mark = (len & FRAME_ENDED) ? 1 : 0; |
| 81 |
len &= 0x7fff; |
| 82 |
if (len > BUF_SIZE) { |
| 83 |
ast_log(LOG_WARNING, "Length %d is too long\n", len); |
| 84 |
return NULL; |
| 85 |
} |
| 86 |
AST_FRAME_SET_BUFFER(&s->fr, s->buf, AST_FRIENDLY_OFFSET, len); |
| 87 |
if ((res = fread(s->fr.data.ptr, 1, s->fr.datalen, s->f)) != s->fr.datalen) { |
| 88 |
if (res) { |
| 89 |
ast_log(LOG_WARNING, "Short read of %s data (expected %d bytes, read %zu): %s\n", |
| 90 |
ast_format_get_name(s->fr.subclass.format), s->fr.datalen, res, |
| 91 |
strerror(errno)); |
| 92 |
} |
| 93 |
return NULL; |
| 94 |
} |
| 95 |
s->fr.samples = fs->lastts; |
| 96 |
s->fr.datalen = len; |
| 97 |
s->fr.subclass.frame_ending = mark; |
| 98 |
if ((res = fread(&ts, 1, sizeof(ts), s->f)) == sizeof(ts)) { |
| 99 |
fs->lastts = ntohl(ts); |
| 100 |
*whennext = fs->lastts * 4/45; |
| 101 |
} else |
| 102 |
*whennext = 0; |
| 103 |
return &s->fr; |
| 104 |
} |
| 105 |
|
| 106 |
static int h263_write(struct ast_filestream *fs, struct ast_frame *f) |
| 107 |
{ |
| 108 |
int res; |
| 109 |
unsigned int ts; |
| 110 |
unsigned short len; |
| 111 |
uint32_t mark = 0; |
| 112 |
mark = f->subclass.frame_ending ? FRAME_ENDED : 0; |
| 113 |
ts = htonl(f->samples); |
| 114 |
if ((res = fwrite(&ts, 1, sizeof(ts), fs->f)) != sizeof(ts)) { |
| 115 |
ast_log(LOG_WARNING, "Bad write (%d/4): %s\n", res, strerror(errno)); |
| 116 |
return -1; |
| 117 |
} |
| 118 |
len = htons(f->datalen | mark); |
| 119 |
if ((res = fwrite(&len, 1, sizeof(len), fs->f)) != sizeof(len)) { |
| 120 |
ast_log(LOG_WARNING, "Bad write (%d/2): %s\n", res, strerror(errno)); |
| 121 |
return -1; |
| 122 |
} |
| 123 |
if ((res = fwrite(f->data.ptr, 1, f->datalen, fs->f)) != f->datalen) { |
| 124 |
ast_log(LOG_WARNING, "Bad write (%d/%d): %s\n", res, f->datalen, strerror(errno)); |
| 125 |
return -1; |
| 126 |
} |
| 127 |
return 0; |
| 128 |
} |
| 129 |
|
| 130 |
static int h263_seek(struct ast_filestream *fs, off_t sample_offset, int whence) |
| 131 |
{ |
| 132 |
|
| 133 |
return -1; |
| 134 |
} |
| 135 |
|
| 136 |
static int h263_trunc(struct ast_filestream *fs) |
| 137 |
{ |
| 138 |
int fd; |
| 139 |
off_t cur; |
| 140 |
|
| 141 |
if ((fd = fileno(fs->f)) < 0) { |
| 142 |
ast_log(AST_LOG_WARNING, "Unable to determine file descriptor for h263 filestream %p: %s\n", fs, strerror(errno)); |
| 143 |
return -1; |
| 144 |
} |
| 145 |
if ((cur = ftello(fs->f)) < 0) { |
| 146 |
ast_log(AST_LOG_WARNING, "Unable to determine current position in h263 filestream %p: %s\n", fs, strerror(errno)); |
| 147 |
return -1; |
| 148 |
} |
| 149 |
|
| 150 |
return ftruncate(fd, cur); |
| 151 |
} |
| 152 |
|
| 153 |
static off_t h263_tell(struct ast_filestream *fs) |
| 154 |
{ |
| 155 |
off_t offset = ftello(fs->f); |
| 156 |
return offset; |
| 157 |
} |
| 158 |
|
| 159 |
static struct ast_format_def h263_f = { |
| 160 |
.name = "h263", |
| 161 |
.exts = "h263", |
| 162 |
.open = h263_open, |
| 163 |
.write = h263_write, |
| 164 |
.seek = h263_seek, |
| 165 |
.trunc = h263_trunc, |
| 166 |
.tell = h263_tell, |
| 167 |
.read = h263_read, |
| 168 |
.buf_size = BUF_SIZE + AST_FRIENDLY_OFFSET, |
| 169 |
.desc_size = sizeof(struct h263_desc), |
| 170 |
}; |
| 171 |
|
| 172 |
static int load_module(void) |
| 173 |
{ |
| 174 |
h263_f.format = ast_format_h263; |
| 175 |
if (ast_format_def_register(&h263_f)) |
| 176 |
return AST_MODULE_LOAD_DECLINE; |
| 177 |
return AST_MODULE_LOAD_SUCCESS; |
| 178 |
} |
| 179 |
|
| 180 |
static int unload_module(void) |
| 181 |
{ |
| 182 |
return ast_format_def_unregister(h263_f.name); |
| 183 |
} |
| 184 |
|
| 185 |
AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Raw H.263 data", |
| 186 |
.support_level = AST_MODULE_SUPPORT_CORE, |
| 187 |
.load = load_module, |
| 188 |
.unload = unload_module, |
| 189 |
.load_pri = AST_MODPRI_APP_DEPEND |
| 190 |
); |