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