| 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/codec.h" |
| 38 |
#include "asterisk/frame.h" |
| 39 |
#include "asterisk/linkedlists.h" |
| 40 |
#include "asterisk/logger.h" |
| 41 |
#include "asterisk/module.h" |
| 42 |
#include "asterisk/rtp_engine.h" |
| 43 |
#include "asterisk/translate.h" |
| 44 |
|
| 45 |
#include <codec2/codec2.h> |
| 46 |
|
| 47 |
#define BUFFER_SAMPLES 8000 |
| 48 |
#define CODEC2_SAMPLES 160 |
| 49 |
#define CODEC2_FRAME_LEN 6 |
| 50 |
|
| 51 |
|
| 52 |
#include "asterisk/slin.h" |
| 53 |
#include "ex_codec2.h" |
| 54 |
|
| 55 |
struct codec2_translator_pvt { |
| 56 |
struct CODEC2 *state; |
| 57 |
int16_t buf[BUFFER_SAMPLES]; |
| 58 |
}; |
| 59 |
|
| 60 |
static int codec2_new(struct ast_trans_pvt *pvt) |
| 61 |
{ |
| 62 |
struct codec2_translator_pvt *tmp = pvt->pvt; |
| 63 |
|
| 64 |
tmp->state = codec2_create(CODEC2_MODE_2400); |
| 65 |
|
| 66 |
if (!tmp->state) { |
| 67 |
ast_log(LOG_ERROR, "Error creating Codec 2 conversion\n"); |
| 68 |
return -1; |
| 69 |
} |
| 70 |
|
| 71 |
return 0; |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
static int codec2tolin_framein(struct ast_trans_pvt *pvt, struct ast_frame *f) |
| 76 |
{ |
| 77 |
struct codec2_translator_pvt *tmp = pvt->pvt; |
| 78 |
int x; |
| 79 |
|
| 80 |
for (x = 0; x + CODEC2_FRAME_LEN <= f->datalen; x += CODEC2_FRAME_LEN) { |
| 81 |
unsigned char *src = f->data.ptr + x; |
| 82 |
int16_t *dst = pvt->outbuf.i16 + pvt->samples; |
| 83 |
|
| 84 |
codec2_decode(tmp->state, dst, src); |
| 85 |
|
| 86 |
pvt->samples += CODEC2_SAMPLES; |
| 87 |
pvt->datalen += CODEC2_SAMPLES * 2; |
| 88 |
} |
| 89 |
|
| 90 |
return 0; |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
static int lintocodec2_framein(struct ast_trans_pvt *pvt, struct ast_frame *f) |
| 95 |
{ |
| 96 |
struct codec2_translator_pvt *tmp = pvt->pvt; |
| 97 |
|
| 98 |
memcpy(tmp->buf + pvt->samples, f->data.ptr, f->datalen); |
| 99 |
pvt->samples += f->samples; |
| 100 |
|
| 101 |
return 0; |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
static struct ast_frame *lintocodec2_frameout(struct ast_trans_pvt *pvt) |
| 106 |
{ |
| 107 |
struct codec2_translator_pvt *tmp = pvt->pvt; |
| 108 |
struct ast_frame *result = NULL; |
| 109 |
struct ast_frame *last = NULL; |
| 110 |
int samples = 0; |
| 111 |
|
| 112 |
while (pvt->samples >= CODEC2_SAMPLES) { |
| 113 |
struct ast_frame *current; |
| 114 |
|
| 115 |
|
| 116 |
codec2_encode(tmp->state, pvt->outbuf.uc, tmp->buf + samples); |
| 117 |
|
| 118 |
samples += CODEC2_SAMPLES; |
| 119 |
pvt->samples -= CODEC2_SAMPLES; |
| 120 |
|
| 121 |
current = ast_trans_frameout(pvt, CODEC2_FRAME_LEN, CODEC2_SAMPLES); |
| 122 |
|
| 123 |
if (!current) { |
| 124 |
continue; |
| 125 |
} else if (last) { |
| 126 |
AST_LIST_NEXT(last, frame_list) = current; |
| 127 |
} else { |
| 128 |
result = current; |
| 129 |
} |
| 130 |
last = current; |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
if (samples) { |
| 135 |
memmove(tmp->buf, tmp->buf + samples, pvt->samples * 2); |
| 136 |
} |
| 137 |
|
| 138 |
return result; |
| 139 |
} |
| 140 |
|
| 141 |
static void codec2_destroy_stuff(struct ast_trans_pvt *pvt) |
| 142 |
{ |
| 143 |
struct codec2_translator_pvt *tmp = pvt->pvt; |
| 144 |
|
| 145 |
if (tmp->state) { |
| 146 |
codec2_destroy(tmp->state); |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
static struct ast_translator codec2tolin = { |
| 151 |
.name = "codec2tolin", |
| 152 |
.src_codec = { |
| 153 |
.name = "codec2", |
| 154 |
.type = AST_MEDIA_TYPE_AUDIO, |
| 155 |
.sample_rate = 8000, |
| 156 |
}, |
| 157 |
.dst_codec = { |
| 158 |
.name = "slin", |
| 159 |
.type = AST_MEDIA_TYPE_AUDIO, |
| 160 |
.sample_rate = 8000, |
| 161 |
}, |
| 162 |
.format = "slin", |
| 163 |
.newpvt = codec2_new, |
| 164 |
.framein = codec2tolin_framein, |
| 165 |
.destroy = codec2_destroy_stuff, |
| 166 |
.sample = codec2_sample, |
| 167 |
.desc_size = sizeof(struct codec2_translator_pvt), |
| 168 |
.buffer_samples = BUFFER_SAMPLES, |
| 169 |
.buf_size = BUFFER_SAMPLES * 2, |
| 170 |
}; |
| 171 |
|
| 172 |
static struct ast_translator lintocodec2 = { |
| 173 |
.name = "lintocodec2", |
| 174 |
.src_codec = { |
| 175 |
.name = "slin", |
| 176 |
.type = AST_MEDIA_TYPE_AUDIO, |
| 177 |
.sample_rate = 8000, |
| 178 |
}, |
| 179 |
.dst_codec = { |
| 180 |
.name = "codec2", |
| 181 |
.type = AST_MEDIA_TYPE_AUDIO, |
| 182 |
.sample_rate = 8000, |
| 183 |
}, |
| 184 |
.format = "codec2", |
| 185 |
.newpvt = codec2_new, |
| 186 |
.framein = lintocodec2_framein, |
| 187 |
.frameout = lintocodec2_frameout, |
| 188 |
.destroy = codec2_destroy_stuff, |
| 189 |
.sample = slin8_sample, |
| 190 |
.desc_size = sizeof(struct codec2_translator_pvt), |
| 191 |
.buffer_samples = BUFFER_SAMPLES, |
| 192 |
.buf_size = (BUFFER_SAMPLES * CODEC2_FRAME_LEN + CODEC2_SAMPLES - 1) / CODEC2_SAMPLES, |
| 193 |
}; |
| 194 |
|
| 195 |
static int unload_module(void) |
| 196 |
{ |
| 197 |
int res = 0; |
| 198 |
|
| 199 |
res |= ast_rtp_engine_unload_format(ast_format_codec2); |
| 200 |
res |= ast_unregister_translator(&lintocodec2); |
| 201 |
res |= ast_unregister_translator(&codec2tolin); |
| 202 |
|
| 203 |
return res; |
| 204 |
} |
| 205 |
|
| 206 |
static int load_module(void) |
| 207 |
{ |
| 208 |
int res = 0; |
| 209 |
|
| 210 |
res |= ast_register_translator(&codec2tolin); |
| 211 |
res |= ast_register_translator(&lintocodec2); |
| 212 |
res |= ast_rtp_engine_load_format(ast_format_codec2); |
| 213 |
|
| 214 |
if (res) { |
| 215 |
unload_module(); |
| 216 |
return AST_MODULE_LOAD_DECLINE; |
| 217 |
} |
| 218 |
|
| 219 |
return AST_MODULE_LOAD_SUCCESS; |
| 220 |
} |
| 221 |
|
| 222 |
AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Codec 2 Coder/Decoder"); |