| 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 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
#include "asterisk.h" |
| 37 |
|
| 38 |
#if defined(ASTERISK_REGISTER_FILE) |
| 39 |
ASTERISK_REGISTER_FILE() |
| 40 |
#elif defined(ASTERISK_FILE_VERSION) |
| 41 |
ASTERISK_FILE_VERSION(__FILE__, "$Revision: $") |
| 42 |
#endif |
| 43 |
|
| 44 |
#include <netinet/in.h> |
| 45 |
#include <sys/time.h> |
| 46 |
|
| 47 |
#include "asterisk/format_cache.h" |
| 48 |
#include "asterisk/frame.h" |
| 49 |
#include "asterisk/logger.h" |
| 50 |
#include "asterisk/mod_format.h" |
| 51 |
#include "asterisk/module.h" |
| 52 |
|
| 53 |
|
| 54 |
#define FRAME_ENDED 0x8000 |
| 55 |
|
| 56 |
#define BUF_SIZE 4096 |
| 57 |
struct vp8_desc { |
| 58 |
unsigned int lastts; |
| 59 |
}; |
| 60 |
|
| 61 |
static int vp8_open(struct ast_filestream *s) |
| 62 |
{ |
| 63 |
unsigned int ts; |
| 64 |
|
| 65 |
if (fread(&ts, 1, sizeof(ts), s->f) < sizeof(ts)) { |
| 66 |
ast_log(LOG_WARNING, "Empty file!\n"); |
| 67 |
return -1; |
| 68 |
} |
| 69 |
|
| 70 |
return 0; |
| 71 |
} |
| 72 |
|
| 73 |
static struct ast_frame *vp8_read(struct ast_filestream *s, int *whennext) |
| 74 |
{ |
| 75 |
int res; |
| 76 |
int mark = 0; |
| 77 |
unsigned short len; |
| 78 |
unsigned int ts; |
| 79 |
struct vp8_desc *fs = (struct vp8_desc *) s->_private; |
| 80 |
|
| 81 |
|
| 82 |
if ((res = fread(&len, 1, sizeof(len), s->f)) < 1) { |
| 83 |
return NULL; |
| 84 |
} |
| 85 |
|
| 86 |
len = ntohs(len); |
| 87 |
mark = (len & FRAME_ENDED) ? 1 : 0; |
| 88 |
len &= 0x7fff; |
| 89 |
if (len > BUF_SIZE) { |
| 90 |
ast_log(LOG_WARNING, "Length %d is too long\n", len); |
| 91 |
len = BUF_SIZE; |
| 92 |
} |
| 93 |
s->fr.mallocd = 0; |
| 94 |
AST_FRAME_SET_BUFFER(&s->fr, s->buf, AST_FRIENDLY_OFFSET, len) |
| 95 |
if ((res = fread(s->fr.data.ptr, 1, s->fr.datalen, s->f)) != s->fr.datalen) { |
| 96 |
if (res) { |
| 97 |
ast_log(LOG_WARNING, "Short read (%d of %d) (%s)!\n", res, len, strerror(errno)); |
| 98 |
} |
| 99 |
return NULL; |
| 100 |
} |
| 101 |
s->fr.samples = fs->lastts; |
| 102 |
s->fr.datalen = len; |
| 103 |
s->fr.subclass.frame_ending = mark; |
| 104 |
s->fr.delivery.tv_sec = 0; |
| 105 |
s->fr.delivery.tv_usec = 0; |
| 106 |
if ((res = fread(&ts, 1, sizeof(ts), s->f)) == sizeof(ts)) { |
| 107 |
fs->lastts = ntohl(ts); |
| 108 |
*whennext = fs->lastts * 4 / 45; |
| 109 |
} else { |
| 110 |
*whennext = 0; |
| 111 |
} |
| 112 |
return &s->fr; |
| 113 |
} |
| 114 |
|
| 115 |
static int vp8_write(struct ast_filestream *s, struct ast_frame *f) |
| 116 |
{ |
| 117 |
int res; |
| 118 |
unsigned int ts; |
| 119 |
unsigned short len; |
| 120 |
int mark; |
| 121 |
|
| 122 |
if (f->frametype != AST_FRAME_VIDEO) { |
| 123 |
ast_log(LOG_WARNING, "Asked to write non-video frame!\n"); |
| 124 |
return -1; |
| 125 |
} |
| 126 |
|
| 127 |
mark = f->subclass.frame_ending ? FRAME_ENDED : 0; |
| 128 |
ts = htonl(f->samples); |
| 129 |
if ((res = fwrite(&ts, 1, sizeof(ts), s->f)) != sizeof(ts)) { |
| 130 |
ast_log(LOG_WARNING, "Bad write (%d/4): %s\n", res, strerror(errno)); |
| 131 |
return -1; |
| 132 |
} |
| 133 |
|
| 134 |
len = htons(f->datalen | mark); |
| 135 |
if ((res = fwrite(&len, 1, sizeof(len), s->f)) != sizeof(len)) { |
| 136 |
ast_log(LOG_WARNING, "Bad write (%d/2): %s\n", res, strerror(errno)); |
| 137 |
return -1; |
| 138 |
} |
| 139 |
|
| 140 |
if ((res = fwrite(f->data.ptr, 1, f->datalen, s->f)) != f->datalen) { |
| 141 |
ast_log(LOG_WARNING, "Bad write (%d/%d): %s\n", res, f->datalen, strerror(errno)); |
| 142 |
return -1; |
| 143 |
} |
| 144 |
|
| 145 |
return 0; |
| 146 |
} |
| 147 |
|
| 148 |
static int vp8_seek(struct ast_filestream *fs, off_t sample_offset, int whence) |
| 149 |
{ |
| 150 |
|
| 151 |
return -1; |
| 152 |
} |
| 153 |
|
| 154 |
static int vp8_trunc(struct ast_filestream *fs) |
| 155 |
{ |
| 156 |
int fd; |
| 157 |
off_t cur; |
| 158 |
|
| 159 |
if ((fd = fileno(fs->f)) < 0) { |
| 160 |
ast_log(LOG_WARNING, "Unable to determine file descriptor for VP8 filestream %p: %s\n", fs, strerror(errno)); |
| 161 |
return -1; |
| 162 |
} |
| 163 |
|
| 164 |
if ((cur = ftello(fs->f)) < 0) { |
| 165 |
ast_log(LOG_WARNING, "Unable to determine current position in VP8 filestream %p: %s\n", fs, strerror(errno)); |
| 166 |
return -1; |
| 167 |
} |
| 168 |
|
| 169 |
|
| 170 |
return ftruncate(fd, cur); |
| 171 |
} |
| 172 |
|
| 173 |
static off_t vp8_tell(struct ast_filestream *fs) |
| 174 |
{ |
| 175 |
off_t offset = ftell(fs->f); |
| 176 |
return offset; |
| 177 |
} |
| 178 |
|
| 179 |
static struct ast_format_def vp8_f = { |
| 180 |
.name = "VP8", |
| 181 |
.exts = "vp8", |
| 182 |
.open = vp8_open, |
| 183 |
.write = vp8_write, |
| 184 |
.seek = vp8_seek, |
| 185 |
.trunc = vp8_trunc, |
| 186 |
.tell = vp8_tell, |
| 187 |
.read = vp8_read, |
| 188 |
.buf_size = BUF_SIZE + AST_FRIENDLY_OFFSET, |
| 189 |
.desc_size = sizeof(struct vp8_desc), |
| 190 |
}; |
| 191 |
|
| 192 |
static int load_module(void) |
| 193 |
{ |
| 194 |
vp8_f.format = ast_format_vp8; |
| 195 |
if (ast_format_def_register(&vp8_f)) { |
| 196 |
return AST_MODULE_LOAD_FAILURE; |
| 197 |
} |
| 198 |
|
| 199 |
return AST_MODULE_LOAD_SUCCESS; |
| 200 |
} |
| 201 |
|
| 202 |
static int unload_module(void) |
| 203 |
{ |
| 204 |
return ast_format_def_unregister(vp8_f.name); |
| 205 |
} |
| 206 |
|
| 207 |
AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Raw VP8 data", |
| 208 |
.load = load_module, |
| 209 |
.unload = unload_module, |
| 210 |
.load_pri = AST_MODPRI_APP_DEPEND |
| 211 |
); |