| 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 |
#include "asterisk.h" |
| 33 |
|
| 34 |
#include "asterisk/mod_format.h" |
| 35 |
#include "asterisk/module.h" |
| 36 |
#include "asterisk/endian.h" |
| 37 |
#include "asterisk/format_cache.h" |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
#define ILBC_BUF_SIZE 50 |
| 44 |
#define ILBC_SAMPLES 240 |
| 45 |
|
| 46 |
static struct ast_frame *ilbc_read(struct ast_filestream *s, int *whennext) |
| 47 |
{ |
| 48 |
size_t res; |
| 49 |
|
| 50 |
|
| 51 |
AST_FRAME_SET_BUFFER(&s->fr, s->buf, AST_FRIENDLY_OFFSET, ILBC_BUF_SIZE); |
| 52 |
if ((res = fread(s->fr.data.ptr, 1, s->fr.datalen, s->f)) != s->fr.datalen) { |
| 53 |
if (res) { |
| 54 |
ast_log(LOG_WARNING, "Short read of %s data (expected %d bytes, read %zu): %s\n", |
| 55 |
ast_format_get_name(s->fr.subclass.format), s->fr.datalen, res, |
| 56 |
strerror(errno)); |
| 57 |
} |
| 58 |
return NULL; |
| 59 |
} |
| 60 |
*whennext = s->fr.samples = ILBC_SAMPLES; |
| 61 |
return &s->fr; |
| 62 |
} |
| 63 |
|
| 64 |
static int ilbc_write(struct ast_filestream *fs, struct ast_frame *f) |
| 65 |
{ |
| 66 |
int res; |
| 67 |
if (f->datalen % 50) { |
| 68 |
ast_log(LOG_WARNING, "Invalid data length, %d, should be multiple of 50\n", f->datalen); |
| 69 |
return -1; |
| 70 |
} |
| 71 |
if ((res = fwrite(f->data.ptr, 1, f->datalen, fs->f)) != f->datalen) { |
| 72 |
ast_log(LOG_WARNING, "Bad write (%d/50): %s\n", res, strerror(errno)); |
| 73 |
return -1; |
| 74 |
} |
| 75 |
return 0; |
| 76 |
} |
| 77 |
|
| 78 |
static int ilbc_seek(struct ast_filestream *fs, off_t sample_offset, int whence) |
| 79 |
{ |
| 80 |
long bytes; |
| 81 |
off_t min,cur,max,offset=0; |
| 82 |
min = 0; |
| 83 |
cur = ftello(fs->f); |
| 84 |
fseeko(fs->f, 0, SEEK_END); |
| 85 |
max = ftello(fs->f); |
| 86 |
|
| 87 |
bytes = ILBC_BUF_SIZE * (sample_offset / ILBC_SAMPLES); |
| 88 |
if (whence == SEEK_SET) |
| 89 |
offset = bytes; |
| 90 |
else if (whence == SEEK_CUR || whence == SEEK_FORCECUR) |
| 91 |
offset = cur + bytes; |
| 92 |
else if (whence == SEEK_END) |
| 93 |
offset = max - bytes; |
| 94 |
if (whence != SEEK_FORCECUR) { |
| 95 |
offset = (offset > max)?max:offset; |
| 96 |
} |
| 97 |
|
| 98 |
offset = (offset < min)?min:offset; |
| 99 |
if (fseeko(fs->f, offset, SEEK_SET) < 0) |
| 100 |
return -1; |
| 101 |
return 0; |
| 102 |
} |
| 103 |
|
| 104 |
static int ilbc_trunc(struct ast_filestream *fs) |
| 105 |
{ |
| 106 |
int fd; |
| 107 |
off_t cur; |
| 108 |
|
| 109 |
if ((fd = fileno(fs->f)) < 0) { |
| 110 |
ast_log(AST_LOG_WARNING, "Unable to determine file descriptor for iLBC filestream %p: %s\n", fs, strerror(errno)); |
| 111 |
return -1; |
| 112 |
} |
| 113 |
if ((cur = ftello(fs->f)) < 0) { |
| 114 |
ast_log(AST_LOG_WARNING, "Unable to determine current position in iLBC filestream %p: %s\n", fs, strerror(errno)); |
| 115 |
return -1; |
| 116 |
} |
| 117 |
|
| 118 |
return ftruncate(fd, cur); |
| 119 |
} |
| 120 |
|
| 121 |
static off_t ilbc_tell(struct ast_filestream *fs) |
| 122 |
{ |
| 123 |
off_t offset = ftello(fs->f); |
| 124 |
return (offset/ILBC_BUF_SIZE)*ILBC_SAMPLES; |
| 125 |
} |
| 126 |
|
| 127 |
static struct ast_format_def ilbc_f = { |
| 128 |
.name = "iLBC", |
| 129 |
.exts = "ilbc", |
| 130 |
.write = ilbc_write, |
| 131 |
.seek = ilbc_seek, |
| 132 |
.trunc = ilbc_trunc, |
| 133 |
.tell = ilbc_tell, |
| 134 |
.read = ilbc_read, |
| 135 |
.buf_size = ILBC_BUF_SIZE + AST_FRIENDLY_OFFSET, |
| 136 |
}; |
| 137 |
|
| 138 |
static int load_module(void) |
| 139 |
{ |
| 140 |
ilbc_f.format = ast_format_ilbc; |
| 141 |
if (ast_format_def_register(&ilbc_f)) |
| 142 |
return AST_MODULE_LOAD_DECLINE; |
| 143 |
return AST_MODULE_LOAD_SUCCESS; |
| 144 |
} |
| 145 |
|
| 146 |
static int unload_module(void) |
| 147 |
{ |
| 148 |
return ast_format_def_unregister(ilbc_f.name); |
| 149 |
} |
| 150 |
|
| 151 |
AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Raw iLBC data", |
| 152 |
.support_level = AST_MODULE_SUPPORT_CORE, |
| 153 |
.load = load_module, |
| 154 |
.unload = unload_module, |
| 155 |
.load_pri = AST_MODPRI_APP_DEPEND |
| 156 |
); |