| 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/format_cache.h" |
| 37 |
|
| 38 |
#define G723_MAX_SIZE 1024 |
| 39 |
|
| 40 |
static struct ast_frame *g723_read(struct ast_filestream *s, int *whennext) |
| 41 |
{ |
| 42 |
unsigned short size; |
| 43 |
size_t res; |
| 44 |
int delay; |
| 45 |
|
| 46 |
|
| 47 |
if (fread(&delay, 1, 4, s->f) == 4) |
| 48 |
delay = ntohl(delay); |
| 49 |
else |
| 50 |
delay = -1; |
| 51 |
if (fread(&size, 1, 2, s->f) != 2) { |
| 52 |
|
| 53 |
|
| 54 |
return NULL; |
| 55 |
} |
| 56 |
|
| 57 |
size = ntohs(size); |
| 58 |
if (size > G723_MAX_SIZE) { |
| 59 |
ast_log(LOG_WARNING, "Size %d is invalid\n", size); |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
return NULL; |
| 64 |
} |
| 65 |
|
| 66 |
AST_FRAME_SET_BUFFER(&s->fr, s->buf, AST_FRIENDLY_OFFSET, size); |
| 67 |
if ((res = fread(s->fr.data.ptr, 1, s->fr.datalen, s->f)) != s->fr.datalen) { |
| 68 |
if (res) { |
| 69 |
ast_log(LOG_WARNING, "Short read of %s data (expected %d bytes, read %zu): %s\n", |
| 70 |
ast_format_get_name(s->fr.subclass.format), s->fr.datalen, res, |
| 71 |
strerror(errno)); |
| 72 |
} |
| 73 |
return NULL; |
| 74 |
} |
| 75 |
*whennext = s->fr.samples = 240; |
| 76 |
return &s->fr; |
| 77 |
} |
| 78 |
|
| 79 |
static int g723_write(struct ast_filestream *s, struct ast_frame *f) |
| 80 |
{ |
| 81 |
uint32_t delay; |
| 82 |
uint16_t size; |
| 83 |
int res; |
| 84 |
|
| 85 |
delay = 0; |
| 86 |
if (f->datalen <= 0) { |
| 87 |
ast_log(LOG_WARNING, "Short frame ignored (%d bytes long?)\n", f->datalen); |
| 88 |
return 0; |
| 89 |
} |
| 90 |
if ((res = fwrite(&delay, 1, 4, s->f)) != 4) { |
| 91 |
ast_log(LOG_WARNING, "Unable to write delay: res=%d (%s)\n", res, strerror(errno)); |
| 92 |
return -1; |
| 93 |
} |
| 94 |
size = htons(f->datalen); |
| 95 |
if ((res = fwrite(&size, 1, 2, s->f)) != 2) { |
| 96 |
ast_log(LOG_WARNING, "Unable to write size: res=%d (%s)\n", res, strerror(errno)); |
| 97 |
return -1; |
| 98 |
} |
| 99 |
if ((res = fwrite(f->data.ptr, 1, f->datalen, s->f)) != f->datalen) { |
| 100 |
ast_log(LOG_WARNING, "Unable to write frame: res=%d (%s)\n", res, strerror(errno)); |
| 101 |
return -1; |
| 102 |
} |
| 103 |
return 0; |
| 104 |
} |
| 105 |
|
| 106 |
static int g723_seek(struct ast_filestream *fs, off_t sample_offset, int whence) |
| 107 |
{ |
| 108 |
return -1; |
| 109 |
} |
| 110 |
|
| 111 |
static int g723_trunc(struct ast_filestream *fs) |
| 112 |
{ |
| 113 |
int fd; |
| 114 |
off_t cur; |
| 115 |
|
| 116 |
if ((fd = fileno(fs->f)) < 0) { |
| 117 |
ast_log(AST_LOG_WARNING, "Unable to determine file descriptor for g723 filestream %p: %s\n", fs, strerror(errno)); |
| 118 |
return -1; |
| 119 |
} |
| 120 |
if ((cur = ftello(fs->f)) < 0) { |
| 121 |
ast_log(AST_LOG_WARNING, "Unable to determine current position in g723 filestream %p: %s\n", fs, strerror(errno)); |
| 122 |
return -1; |
| 123 |
} |
| 124 |
|
| 125 |
return ftruncate(fd, cur); |
| 126 |
} |
| 127 |
|
| 128 |
static off_t g723_tell(struct ast_filestream *fs) |
| 129 |
{ |
| 130 |
return -1; |
| 131 |
} |
| 132 |
|
| 133 |
static struct ast_format_def g723_1_f = { |
| 134 |
.name = "g723sf", |
| 135 |
.exts = "g723|g723sf", |
| 136 |
.write = g723_write, |
| 137 |
.seek = g723_seek, |
| 138 |
.trunc = g723_trunc, |
| 139 |
.tell = g723_tell, |
| 140 |
.read = g723_read, |
| 141 |
.buf_size = G723_MAX_SIZE + AST_FRIENDLY_OFFSET, |
| 142 |
}; |
| 143 |
|
| 144 |
static int load_module(void) |
| 145 |
{ |
| 146 |
g723_1_f.format = ast_format_g723; |
| 147 |
|
| 148 |
if (ast_format_def_register(&g723_1_f)) |
| 149 |
return AST_MODULE_LOAD_DECLINE; |
| 150 |
return AST_MODULE_LOAD_SUCCESS; |
| 151 |
} |
| 152 |
|
| 153 |
static int unload_module(void) |
| 154 |
{ |
| 155 |
return ast_format_def_unregister(g723_1_f.name); |
| 156 |
} |
| 157 |
|
| 158 |
AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "G.723.1 Simple Timestamp File Format", |
| 159 |
.support_level = AST_MODULE_SUPPORT_CORE, |
| 160 |
.load = load_module, |
| 161 |
.unload = unload_module, |
| 162 |
.load_pri = AST_MODPRI_APP_DEPEND |
| 163 |
); |