| 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 |
#define BUF_SIZE 80 |
| 39 |
#define VOX_SAMPLES 160 |
| 40 |
|
| 41 |
static struct ast_frame *vox_read(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)) < 1) { |
| 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 = res * 2; |
| 56 |
s->fr.datalen = res; |
| 57 |
return &s->fr; |
| 58 |
} |
| 59 |
|
| 60 |
static int vox_write(struct ast_filestream *s, struct ast_frame *f) |
| 61 |
{ |
| 62 |
int res; |
| 63 |
if ((res = fwrite(f->data.ptr, 1, f->datalen, s->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 vox_seek(struct ast_filestream *fs, off_t sample_offset, int whence) |
| 71 |
{ |
| 72 |
off_t offset = 0, min = 0, cur, max, distance; |
| 73 |
|
| 74 |
if ((cur = ftello(fs->f)) < 0) { |
| 75 |
ast_log(AST_LOG_WARNING, "Unable to determine current position in g719 filestream %p: %s\n", fs, strerror(errno)); |
| 76 |
return -1; |
| 77 |
} |
| 78 |
|
| 79 |
if (fseeko(fs->f, 0, SEEK_END) < 0) { |
| 80 |
ast_log(AST_LOG_WARNING, "Unable to seek to end of g719 filestream %p: %s\n", fs, strerror(errno)); |
| 81 |
return -1; |
| 82 |
} |
| 83 |
|
| 84 |
if ((max = ftello(fs->f)) < 0) { |
| 85 |
ast_log(AST_LOG_WARNING, "Unable to determine max position in g719 filestream %p: %s\n", fs, strerror(errno)); |
| 86 |
return -1; |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
distance = sample_offset/2; |
| 91 |
if (whence == SEEK_SET) { |
| 92 |
offset = distance; |
| 93 |
} else if (whence == SEEK_CUR || whence == SEEK_FORCECUR) { |
| 94 |
offset = distance + cur; |
| 95 |
} else if (whence == SEEK_END) { |
| 96 |
offset = max - distance; |
| 97 |
} |
| 98 |
if (whence != SEEK_FORCECUR) { |
| 99 |
offset = (offset > max)?max:offset; |
| 100 |
offset = (offset < min)?min:offset; |
| 101 |
} |
| 102 |
return fseeko(fs->f, offset, SEEK_SET); |
| 103 |
} |
| 104 |
|
| 105 |
static int vox_trunc(struct ast_filestream *fs) |
| 106 |
{ |
| 107 |
int fd; |
| 108 |
off_t cur; |
| 109 |
|
| 110 |
if ((fd = fileno(fs->f)) < 0) { |
| 111 |
ast_log(AST_LOG_WARNING, "Unable to determine file descriptor for vox filestream %p: %s\n", fs, strerror(errno)); |
| 112 |
return -1; |
| 113 |
} |
| 114 |
if ((cur = ftello(fs->f)) < 0) { |
| 115 |
ast_log(AST_LOG_WARNING, "Unable to determine current position in vox filestream %p: %s\n", fs, strerror(errno)); |
| 116 |
return -1; |
| 117 |
} |
| 118 |
|
| 119 |
return ftruncate(fd, cur);} |
| 120 |
|
| 121 |
static off_t vox_tell(struct ast_filestream *fs) |
| 122 |
{ |
| 123 |
off_t offset; |
| 124 |
offset = ftello(fs->f) << 1; |
| 125 |
return offset; |
| 126 |
} |
| 127 |
|
| 128 |
static struct ast_format_def vox_f = { |
| 129 |
.name = "vox", |
| 130 |
.exts = "vox", |
| 131 |
.mime_types = "audio/x-vox", |
| 132 |
.write = vox_write, |
| 133 |
.seek = vox_seek, |
| 134 |
.trunc = vox_trunc, |
| 135 |
.tell = vox_tell, |
| 136 |
.read = vox_read, |
| 137 |
.buf_size = BUF_SIZE + AST_FRIENDLY_OFFSET, |
| 138 |
}; |
| 139 |
|
| 140 |
static int load_module(void) |
| 141 |
{ |
| 142 |
vox_f.format = ast_format_adpcm; |
| 143 |
if (ast_format_def_register(&vox_f)) |
| 144 |
return AST_MODULE_LOAD_DECLINE; |
| 145 |
return AST_MODULE_LOAD_SUCCESS; |
| 146 |
} |
| 147 |
|
| 148 |
static int unload_module(void) |
| 149 |
{ |
| 150 |
return ast_format_def_unregister(vox_f.name); |
| 151 |
} |
| 152 |
|
| 153 |
AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Dialogic VOX (ADPCM) File Format", |
| 154 |
.support_level = AST_MODULE_SUPPORT_EXTENDED, |
| 155 |
.load = load_module, |
| 156 |
.unload = unload_module, |
| 157 |
.load_pri = AST_MODPRI_APP_DEPEND |
| 158 |
); |