| 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 |
#include "asterisk.h" |
| 36 |
|
| 37 |
#include "asterisk/mod_format.h" |
| 38 |
#include "asterisk/module.h" |
| 39 |
#include "asterisk/endian.h" |
| 40 |
#include "asterisk/format_cache.h" |
| 41 |
|
| 42 |
#define RATE_40 0 |
| 43 |
#define RATE_32 1 |
| 44 |
#define RATE_24 2 |
| 45 |
#define RATE_16 3 |
| 46 |
|
| 47 |
|
| 48 |
#define FRAME_TIME 10 |
| 49 |
|
| 50 |
#define BUF_SIZE (5*FRAME_TIME) |
| 51 |
|
| 52 |
static int frame_size[4] = { |
| 53 |
FRAME_TIME * 5, |
| 54 |
FRAME_TIME * 4, |
| 55 |
FRAME_TIME * 3, |
| 56 |
FRAME_TIME * 2 |
| 57 |
}; |
| 58 |
|
| 59 |
struct g726_desc { |
| 60 |
int rate; |
| 61 |
}; |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
static int g726_open(struct ast_filestream *tmp, int rate) |
| 67 |
{ |
| 68 |
struct g726_desc *s = (struct g726_desc *)tmp->_private; |
| 69 |
s->rate = rate; |
| 70 |
ast_debug(1, "Created filestream G.726-%dk.\n", 40 - s->rate * 8); |
| 71 |
return 0; |
| 72 |
} |
| 73 |
|
| 74 |
static int g726_40_open(struct ast_filestream *s) |
| 75 |
{ |
| 76 |
return g726_open(s, RATE_40); |
| 77 |
} |
| 78 |
|
| 79 |
static int g726_32_open(struct ast_filestream *s) |
| 80 |
{ |
| 81 |
return g726_open(s, RATE_32); |
| 82 |
} |
| 83 |
|
| 84 |
static int g726_24_open(struct ast_filestream *s) |
| 85 |
{ |
| 86 |
return g726_open(s, RATE_24); |
| 87 |
} |
| 88 |
|
| 89 |
static int g726_16_open(struct ast_filestream *s) |
| 90 |
{ |
| 91 |
return g726_open(s, RATE_16); |
| 92 |
} |
| 93 |
|
| 94 |
static int g726_40_rewrite(struct ast_filestream *s, const char *comment) |
| 95 |
{ |
| 96 |
return g726_open(s, RATE_40); |
| 97 |
} |
| 98 |
|
| 99 |
static int g726_32_rewrite(struct ast_filestream *s, const char *comment) |
| 100 |
{ |
| 101 |
return g726_open(s, RATE_32); |
| 102 |
} |
| 103 |
|
| 104 |
static int g726_24_rewrite(struct ast_filestream *s, const char *comment) |
| 105 |
{ |
| 106 |
return g726_open(s, RATE_24); |
| 107 |
} |
| 108 |
|
| 109 |
static int g726_16_rewrite(struct ast_filestream *s, const char *comment) |
| 110 |
{ |
| 111 |
return g726_open(s, RATE_16); |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
static struct ast_frame *g726_read(struct ast_filestream *s, int *whennext) |
| 119 |
{ |
| 120 |
size_t res; |
| 121 |
struct g726_desc *fs = (struct g726_desc *)s->_private; |
| 122 |
|
| 123 |
|
| 124 |
AST_FRAME_SET_BUFFER(&s->fr, s->buf, AST_FRIENDLY_OFFSET, frame_size[fs->rate]); |
| 125 |
s->fr.samples = 8 * FRAME_TIME; |
| 126 |
if ((res = fread(s->fr.data.ptr, 1, s->fr.datalen, s->f)) != s->fr.datalen) { |
| 127 |
if (res) { |
| 128 |
ast_log(LOG_WARNING, "Short read of %s data (expected %d bytes, read %zu): %s\n", |
| 129 |
ast_format_get_name(s->fr.subclass.format), s->fr.datalen, res, |
| 130 |
strerror(errno)); |
| 131 |
} |
| 132 |
return NULL; |
| 133 |
} |
| 134 |
*whennext = s->fr.samples; |
| 135 |
return &s->fr; |
| 136 |
} |
| 137 |
|
| 138 |
static int g726_write(struct ast_filestream *s, struct ast_frame *f) |
| 139 |
{ |
| 140 |
int res; |
| 141 |
struct g726_desc *fs = (struct g726_desc *)s->_private; |
| 142 |
|
| 143 |
if (f->datalen % frame_size[fs->rate]) { |
| 144 |
ast_log(LOG_WARNING, "Invalid data length %d, should be multiple of %d\n", |
| 145 |
f->datalen, frame_size[fs->rate]); |
| 146 |
return -1; |
| 147 |
} |
| 148 |
if ((res = fwrite(f->data.ptr, 1, f->datalen, s->f)) != f->datalen) { |
| 149 |
ast_log(LOG_WARNING, "Bad write (%d/%d): %s\n", |
| 150 |
res, frame_size[fs->rate], strerror(errno)); |
| 151 |
return -1; |
| 152 |
} |
| 153 |
return 0; |
| 154 |
} |
| 155 |
|
| 156 |
static int g726_seek(struct ast_filestream *fs, off_t sample_offset, int whence) |
| 157 |
{ |
| 158 |
off_t offset = 0, min = 0, cur, max, distance; |
| 159 |
|
| 160 |
if ((cur = ftello(fs->f)) < 0) { |
| 161 |
ast_log(AST_LOG_WARNING, "Unable to determine current position in g726 filestream %p: %s\n", fs, strerror(errno)); |
| 162 |
return -1; |
| 163 |
} |
| 164 |
|
| 165 |
if (fseeko(fs->f, 0, SEEK_END) < 0) { |
| 166 |
ast_log(AST_LOG_WARNING, "Unable to seek to end of g726 filestream %p: %s\n", fs, strerror(errno)); |
| 167 |
return -1; |
| 168 |
} |
| 169 |
|
| 170 |
if ((max = ftello(fs->f)) < 0) { |
| 171 |
ast_log(AST_LOG_WARNING, "Unable to determine max position in g726 filestream %p: %s\n", fs, strerror(errno)); |
| 172 |
return -1; |
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
distance = sample_offset / 2; |
| 177 |
if (whence == SEEK_SET) { |
| 178 |
offset = distance; |
| 179 |
} else if (whence == SEEK_CUR || whence == SEEK_FORCECUR) { |
| 180 |
offset = distance + cur; |
| 181 |
} else if (whence == SEEK_END) { |
| 182 |
offset = max - distance; |
| 183 |
} |
| 184 |
|
| 185 |
if (whence != SEEK_FORCECUR) { |
| 186 |
offset = offset > max ? max : offset; |
| 187 |
offset = offset < min ? min : offset; |
| 188 |
} |
| 189 |
return fseeko(fs->f, offset, SEEK_SET); |
| 190 |
} |
| 191 |
|
| 192 |
static int g726_trunc(struct ast_filestream *fs) |
| 193 |
{ |
| 194 |
return -1; |
| 195 |
} |
| 196 |
|
| 197 |
static off_t g726_tell(struct ast_filestream *fs) |
| 198 |
{ |
| 199 |
return ftello(fs->f) << 1; |
| 200 |
} |
| 201 |
|
| 202 |
static struct ast_format_def f_def[] = { |
| 203 |
{ |
| 204 |
.name = "g726-40", |
| 205 |
.exts = "g726-40", |
| 206 |
.open = g726_40_open, |
| 207 |
.rewrite = g726_40_rewrite, |
| 208 |
.write = g726_write, |
| 209 |
.seek = g726_seek, |
| 210 |
.trunc = g726_trunc, |
| 211 |
.tell = g726_tell, |
| 212 |
.read = g726_read, |
| 213 |
.buf_size = BUF_SIZE + AST_FRIENDLY_OFFSET, |
| 214 |
.desc_size = sizeof(struct g726_desc), |
| 215 |
}, |
| 216 |
{ |
| 217 |
.name = "g726-32", |
| 218 |
.exts = "g726-32", |
| 219 |
.open = g726_32_open, |
| 220 |
.rewrite = g726_32_rewrite, |
| 221 |
.write = g726_write, |
| 222 |
.seek = g726_seek, |
| 223 |
.trunc = g726_trunc, |
| 224 |
.tell = g726_tell, |
| 225 |
.read = g726_read, |
| 226 |
.buf_size = BUF_SIZE + AST_FRIENDLY_OFFSET, |
| 227 |
.desc_size = sizeof(struct g726_desc), |
| 228 |
}, |
| 229 |
{ |
| 230 |
.name = "g726-24", |
| 231 |
.exts = "g726-24", |
| 232 |
.open = g726_24_open, |
| 233 |
.rewrite = g726_24_rewrite, |
| 234 |
.write = g726_write, |
| 235 |
.seek = g726_seek, |
| 236 |
.trunc = g726_trunc, |
| 237 |
.tell = g726_tell, |
| 238 |
.read = g726_read, |
| 239 |
.buf_size = BUF_SIZE + AST_FRIENDLY_OFFSET, |
| 240 |
.desc_size = sizeof(struct g726_desc), |
| 241 |
}, |
| 242 |
{ |
| 243 |
.name = "g726-16", |
| 244 |
.exts = "g726-16", |
| 245 |
.open = g726_16_open, |
| 246 |
.rewrite = g726_16_rewrite, |
| 247 |
.write = g726_write, |
| 248 |
.seek = g726_seek, |
| 249 |
.trunc = g726_trunc, |
| 250 |
.tell = g726_tell, |
| 251 |
.read = g726_read, |
| 252 |
.buf_size = BUF_SIZE + AST_FRIENDLY_OFFSET, |
| 253 |
.desc_size = sizeof(struct g726_desc), |
| 254 |
}, |
| 255 |
{ .desc_size = 0 } |
| 256 |
}; |
| 257 |
|
| 258 |
static int unload_module(void) |
| 259 |
{ |
| 260 |
int i; |
| 261 |
|
| 262 |
for (i = 0; f_def[i].desc_size ; i++) { |
| 263 |
if (ast_format_def_unregister(f_def[i].name)) |
| 264 |
ast_log(LOG_WARNING, "Failed to unregister format %s.\n", f_def[i].name); |
| 265 |
} |
| 266 |
return(0); |
| 267 |
} |
| 268 |
|
| 269 |
static int load_module(void) |
| 270 |
{ |
| 271 |
int i; |
| 272 |
|
| 273 |
for (i = 0; f_def[i].desc_size ; i++) { |
| 274 |
f_def[i].format = ast_format_g726; |
| 275 |
if (ast_format_def_register(&f_def[i])) { |
| 276 |
ast_log(LOG_WARNING, "Failed to register format %s.\n", f_def[i].name); |
| 277 |
unload_module(); |
| 278 |
return AST_MODULE_LOAD_DECLINE; |
| 279 |
} |
| 280 |
} |
| 281 |
return AST_MODULE_LOAD_SUCCESS; |
| 282 |
} |
| 283 |
|
| 284 |
AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Raw G.726 (16/24/32/40kbps) data", |
| 285 |
.support_level = AST_MODULE_SUPPORT_CORE, |
| 286 |
.load = load_module, |
| 287 |
.unload = unload_module, |
| 288 |
.load_pri = AST_MODPRI_APP_DEPEND |
| 289 |
); |