| 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 |
#define FRAME_ENDED 0x8000 |
| 44 |
|
| 45 |
#define BUF_SIZE 4096 |
| 46 |
struct h264_desc { |
| 47 |
unsigned int lastts; |
| 48 |
}; |
| 49 |
|
| 50 |
static int h264_open(struct ast_filestream *s) |
| 51 |
{ |
| 52 |
unsigned int ts; |
| 53 |
if (fread(&ts, 1, sizeof(ts), s->f) != sizeof(ts)) { |
| 54 |
ast_log(LOG_WARNING, "Empty file!\n"); |
| 55 |
return -1; |
| 56 |
} |
| 57 |
return 0; |
| 58 |
} |
| 59 |
|
| 60 |
static struct ast_frame *h264_read(struct ast_filestream *s, int *whennext) |
| 61 |
{ |
| 62 |
size_t res; |
| 63 |
int mark = 0; |
| 64 |
unsigned short len; |
| 65 |
unsigned int ts; |
| 66 |
struct h264_desc *fs = (struct h264_desc *)s->_private; |
| 67 |
|
| 68 |
|
| 69 |
if ((res = fread(&len, 1, sizeof(len), s->f)) != sizeof(len)) |
| 70 |
return NULL; |
| 71 |
len = ntohs(len); |
| 72 |
mark = (len & FRAME_ENDED) ? 1 : 0; |
| 73 |
len &= 0x7fff; |
| 74 |
if (len > BUF_SIZE) { |
| 75 |
ast_log(LOG_WARNING, "Length %d is too long\n", len); |
| 76 |
len = BUF_SIZE; |
| 77 |
} |
| 78 |
AST_FRAME_SET_BUFFER(&s->fr, s->buf, AST_FRIENDLY_OFFSET, len); |
| 79 |
if ((res = fread(s->fr.data.ptr, 1, s->fr.datalen, s->f)) != s->fr.datalen) { |
| 80 |
if (res) { |
| 81 |
ast_log(LOG_WARNING, "Short read of %s data (expected %d bytes, read %zu): %s\n", |
| 82 |
ast_format_get_name(s->fr.subclass.format), s->fr.datalen, res, |
| 83 |
strerror(errno)); |
| 84 |
} |
| 85 |
return NULL; |
| 86 |
} |
| 87 |
s->fr.samples = fs->lastts; |
| 88 |
s->fr.datalen = len; |
| 89 |
s->fr.subclass.frame_ending = mark; |
| 90 |
if ((res = fread(&ts, 1, sizeof(ts), s->f)) == sizeof(ts)) { |
| 91 |
fs->lastts = ntohl(ts); |
| 92 |
*whennext = fs->lastts * 4/45; |
| 93 |
} else |
| 94 |
*whennext = 0; |
| 95 |
return &s->fr; |
| 96 |
} |
| 97 |
|
| 98 |
static int h264_write(struct ast_filestream *s, struct ast_frame *f) |
| 99 |
{ |
| 100 |
int res; |
| 101 |
unsigned int ts; |
| 102 |
unsigned short len; |
| 103 |
int mark; |
| 104 |
|
| 105 |
mark = f->subclass.frame_ending ? FRAME_ENDED : 0; |
| 106 |
ts = htonl(f->samples); |
| 107 |
if ((res = fwrite(&ts, 1, sizeof(ts), s->f)) != sizeof(ts)) { |
| 108 |
ast_log(LOG_WARNING, "Bad write (%d/4): %s\n", res, strerror(errno)); |
| 109 |
return -1; |
| 110 |
} |
| 111 |
len = htons(f->datalen | mark); |
| 112 |
if ((res = fwrite(&len, 1, sizeof(len), s->f)) != sizeof(len)) { |
| 113 |
ast_log(LOG_WARNING, "Bad write (%d/2): %s\n", res, strerror(errno)); |
| 114 |
return -1; |
| 115 |
} |
| 116 |
if ((res = fwrite(f->data.ptr, 1, f->datalen, s->f)) != f->datalen) { |
| 117 |
ast_log(LOG_WARNING, "Bad write (%d/%d): %s\n", res, f->datalen, strerror(errno)); |
| 118 |
return -1; |
| 119 |
} |
| 120 |
return 0; |
| 121 |
} |
| 122 |
|
| 123 |
static int h264_seek(struct ast_filestream *fs, off_t sample_offset, int whence) |
| 124 |
{ |
| 125 |
|
| 126 |
return -1; |
| 127 |
} |
| 128 |
|
| 129 |
static int h264_trunc(struct ast_filestream *fs) |
| 130 |
{ |
| 131 |
int fd; |
| 132 |
off_t cur; |
| 133 |
|
| 134 |
if ((fd = fileno(fs->f)) < 0) { |
| 135 |
ast_log(AST_LOG_WARNING, "Unable to determine file descriptor for h264 filestream %p: %s\n", fs, strerror(errno)); |
| 136 |
return -1; |
| 137 |
} |
| 138 |
if ((cur = ftello(fs->f)) < 0) { |
| 139 |
ast_log(AST_LOG_WARNING, "Unable to determine current position in h264 filestream %p: %s\n", fs, strerror(errno)); |
| 140 |
return -1; |
| 141 |
} |
| 142 |
|
| 143 |
return ftruncate(fd, cur); |
| 144 |
} |
| 145 |
|
| 146 |
static off_t h264_tell(struct ast_filestream *fs) |
| 147 |
{ |
| 148 |
off_t offset = ftell(fs->f); |
| 149 |
return offset; |
| 150 |
} |
| 151 |
|
| 152 |
static struct ast_format_def h264_f = { |
| 153 |
.name = "h264", |
| 154 |
.exts = "h264", |
| 155 |
.open = h264_open, |
| 156 |
.write = h264_write, |
| 157 |
.seek = h264_seek, |
| 158 |
.trunc = h264_trunc, |
| 159 |
.tell = h264_tell, |
| 160 |
.read = h264_read, |
| 161 |
.buf_size = BUF_SIZE + AST_FRIENDLY_OFFSET, |
| 162 |
.desc_size = sizeof(struct h264_desc), |
| 163 |
}; |
| 164 |
|
| 165 |
static int load_module(void) |
| 166 |
{ |
| 167 |
h264_f.format = ast_format_h264; |
| 168 |
if (ast_format_def_register(&h264_f)) |
| 169 |
return AST_MODULE_LOAD_DECLINE; |
| 170 |
return AST_MODULE_LOAD_SUCCESS; |
| 171 |
} |
| 172 |
|
| 173 |
static int unload_module(void) |
| 174 |
{ |
| 175 |
return ast_format_def_unregister(h264_f.name); |
| 176 |
} |
| 177 |
|
| 178 |
AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Raw H.264 data", |
| 179 |
.support_level = AST_MODULE_SUPPORT_CORE, |
| 180 |
.load = load_module, |
| 181 |
.unload = unload_module, |
| 182 |
.load_pri = AST_MODPRI_APP_DEPEND |
| 183 |
); |