| 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 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
#include "asterisk.h" |
| 40 |
|
| 41 |
#if defined(ASTERISK_REGISTER_FILE) |
| 42 |
ASTERISK_REGISTER_FILE() |
| 43 |
#elif defined(ASTERISK_FILE_VERSION) |
| 44 |
ASTERISK_FILE_VERSION(__FILE__, "$Revision: $") |
| 45 |
#endif |
| 46 |
|
| 47 |
#include "asterisk/astobj2.h" |
| 48 |
#include "asterisk/cli.h" |
| 49 |
#include "asterisk/codec.h" |
| 50 |
#include "asterisk/format.h" |
| 51 |
#include "asterisk/frame.h" |
| 52 |
#include "asterisk/linkedlists.h" |
| 53 |
#include "asterisk/lock.h" |
| 54 |
#include "asterisk/logger.h" |
| 55 |
#include "asterisk/module.h" |
| 56 |
#include "asterisk/translate.h" |
| 57 |
#include "asterisk/utils.h" |
| 58 |
|
| 59 |
#include <opus/opus.h> |
| 60 |
|
| 61 |
#include "asterisk/opus.h" |
| 62 |
|
| 63 |
#define BUFFER_SAMPLES 5760 |
| 64 |
#define MAX_CHANNELS 2 |
| 65 |
#define OPUS_SAMPLES 960 |
| 66 |
|
| 67 |
|
| 68 |
#include "asterisk/slin.h" |
| 69 |
#include "ex_opus.h" |
| 70 |
|
| 71 |
static struct codec_usage { |
| 72 |
int encoder_id; |
| 73 |
int decoder_id; |
| 74 |
int encoders; |
| 75 |
int decoders; |
| 76 |
} usage; |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
static struct ast_codec *opus_codec; |
| 84 |
static int (*opus_samples_previous)(struct ast_frame *frame); |
| 85 |
|
| 86 |
|
| 87 |
struct opus_coder_pvt { |
| 88 |
void *opus; |
| 89 |
int sampling_rate; |
| 90 |
int multiplier; |
| 91 |
int id; |
| 92 |
int16_t buf[BUFFER_SAMPLES]; |
| 93 |
int framesize; |
| 94 |
int inited; |
| 95 |
int channels; |
| 96 |
int decode_fec_incoming; |
| 97 |
int previous_lost; |
| 98 |
}; |
| 99 |
|
| 100 |
struct opus_attr { |
| 101 |
unsigned int maxbitrate; |
| 102 |
unsigned int maxplayrate; |
| 103 |
unsigned int unused; |
| 104 |
unsigned int stereo; |
| 105 |
unsigned int cbr; |
| 106 |
unsigned int fec; |
| 107 |
unsigned int dtx; |
| 108 |
unsigned int spropmaxcapturerate; |
| 109 |
unsigned int spropstereo; |
| 110 |
}; |
| 111 |
|
| 112 |
|
| 113 |
static int opus_encoder_construct(struct ast_trans_pvt *pvt, int sampling_rate) |
| 114 |
{ |
| 115 |
struct opus_coder_pvt *opvt = pvt->pvt; |
| 116 |
struct opus_attr *attr = pvt->explicit_dst ? ast_format_get_attribute_data(pvt->explicit_dst) : NULL; |
| 117 |
const opus_int32 bitrate = attr ? attr->maxbitrate : CODEC_OPUS_DEFAULT_BITRATE; |
| 118 |
const int maxplayrate = attr ? attr->maxplayrate : CODEC_OPUS_DEFAULT_MAX_PLAYBACK_RATE; |
| 119 |
const int channels = attr ? attr->stereo + 1 : CODEC_OPUS_DEFAULT_STEREO + 1; |
| 120 |
const opus_int32 vbr = attr ? !(attr->cbr) : !CODEC_OPUS_DEFAULT_CBR; |
| 121 |
const opus_int32 fec = attr ? attr->fec : CODEC_OPUS_DEFAULT_FEC; |
| 122 |
const opus_int32 dtx = attr ? attr->dtx : CODEC_OPUS_DEFAULT_DTX; |
| 123 |
const int application = OPUS_APPLICATION_VOIP; |
| 124 |
int status = 0; |
| 125 |
|
| 126 |
opvt->opus = opus_encoder_create(sampling_rate, channels, application, &status); |
| 127 |
|
| 128 |
if (status != OPUS_OK) { |
| 129 |
ast_log(LOG_ERROR, "Error creating the Opus encoder: %s\n", opus_strerror(status)); |
| 130 |
return -1; |
| 131 |
} |
| 132 |
|
| 133 |
if (sampling_rate <= 8000 || maxplayrate <= 8000) { |
| 134 |
status = opus_encoder_ctl(opvt->opus, OPUS_SET_MAX_BANDWIDTH(OPUS_BANDWIDTH_NARROWBAND)); |
| 135 |
} else if (sampling_rate <= 12000 || maxplayrate <= 12000) { |
| 136 |
status = opus_encoder_ctl(opvt->opus, OPUS_SET_MAX_BANDWIDTH(OPUS_BANDWIDTH_MEDIUMBAND)); |
| 137 |
} else if (sampling_rate <= 16000 || maxplayrate <= 16000) { |
| 138 |
status = opus_encoder_ctl(opvt->opus, OPUS_SET_MAX_BANDWIDTH(OPUS_BANDWIDTH_WIDEBAND)); |
| 139 |
} else if (sampling_rate <= 24000 || maxplayrate <= 24000) { |
| 140 |
status = opus_encoder_ctl(opvt->opus, OPUS_SET_MAX_BANDWIDTH(OPUS_BANDWIDTH_SUPERWIDEBAND)); |
| 141 |
} |
| 142 |
|
| 143 |
if (0 < bitrate && bitrate != 510000) { |
| 144 |
status = opus_encoder_ctl(opvt->opus, OPUS_SET_BITRATE(bitrate)); |
| 145 |
} |
| 146 |
status = opus_encoder_ctl(opvt->opus, OPUS_SET_VBR(vbr)); |
| 147 |
status = opus_encoder_ctl(opvt->opus, OPUS_SET_INBAND_FEC(fec)); |
| 148 |
status = opus_encoder_ctl(opvt->opus, OPUS_SET_DTX(dtx)); |
| 149 |
|
| 150 |
opvt->sampling_rate = sampling_rate; |
| 151 |
opvt->multiplier = 48000 / sampling_rate; |
| 152 |
opvt->framesize = sampling_rate / 50; |
| 153 |
opvt->id = ast_atomic_fetchadd_int(&usage.encoder_id, 1) + 1; |
| 154 |
|
| 155 |
ast_atomic_fetchadd_int(&usage.encoders, +1); |
| 156 |
|
| 157 |
ast_debug(3, "Created encoder #%d (%d -> opus)\n", opvt->id, sampling_rate); |
| 158 |
|
| 159 |
return 0; |
| 160 |
} |
| 161 |
|
| 162 |
static int opus_decoder_construct(struct ast_trans_pvt *pvt, struct ast_frame *f) |
| 163 |
{ |
| 164 |
struct opus_coder_pvt *opvt = pvt->pvt; |
| 165 |
|
| 166 |
int error = 0; |
| 167 |
|
| 168 |
opvt->sampling_rate = pvt->t->dst_codec.sample_rate; |
| 169 |
opvt->multiplier = 48000 / opvt->sampling_rate; |
| 170 |
opvt->channels = 1; |
| 171 |
|
| 172 |
opvt->opus = opus_decoder_create(opvt->sampling_rate, opvt->channels, &error); |
| 173 |
|
| 174 |
if (error != OPUS_OK) { |
| 175 |
ast_log(LOG_ERROR, "Error creating the Opus decoder: %s\n", opus_strerror(error)); |
| 176 |
return -1; |
| 177 |
} |
| 178 |
|
| 179 |
opvt->id = ast_atomic_fetchadd_int(&usage.decoder_id, 1) + 1; |
| 180 |
|
| 181 |
ast_atomic_fetchadd_int(&usage.decoders, +1); |
| 182 |
|
| 183 |
ast_debug(3, "Created decoder #%d (opus -> %d)\n", opvt->id, opvt->sampling_rate); |
| 184 |
|
| 185 |
return 0; |
| 186 |
} |
| 187 |
|
| 188 |
|
| 189 |
static int lintoopus_new(struct ast_trans_pvt *pvt) |
| 190 |
{ |
| 191 |
return opus_encoder_construct(pvt, pvt->t->src_codec.sample_rate); |
| 192 |
} |
| 193 |
|
| 194 |
static int opustolin_new(struct ast_trans_pvt *pvt) |
| 195 |
{ |
| 196 |
struct opus_coder_pvt *opvt = pvt->pvt; |
| 197 |
|
| 198 |
opvt->previous_lost = 0; |
| 199 |
opvt->inited = 0; |
| 200 |
|
| 201 |
return 0; |
| 202 |
} |
| 203 |
|
| 204 |
static int lintoopus_framein(struct ast_trans_pvt *pvt, struct ast_frame *f) |
| 205 |
{ |
| 206 |
struct opus_coder_pvt *opvt = pvt->pvt; |
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
memcpy(opvt->buf + pvt->samples, f->data.ptr, f->datalen); |
| 212 |
pvt->samples += f->samples; |
| 213 |
|
| 214 |
return 0; |
| 215 |
} |
| 216 |
|
| 217 |
static struct ast_frame *lintoopus_frameout(struct ast_trans_pvt *pvt) |
| 218 |
{ |
| 219 |
struct opus_coder_pvt *opvt = pvt->pvt; |
| 220 |
struct ast_frame *result = NULL; |
| 221 |
struct ast_frame *last = NULL; |
| 222 |
int samples = 0; |
| 223 |
|
| 224 |
while (pvt->samples >= opvt->framesize) { |
| 225 |
|
| 226 |
const int status = opus_encode(opvt->opus, |
| 227 |
opvt->buf + samples, |
| 228 |
opvt->framesize, |
| 229 |
pvt->outbuf.uc, |
| 230 |
BUFFER_SAMPLES); |
| 231 |
|
| 232 |
samples += opvt->framesize; |
| 233 |
pvt->samples -= opvt->framesize; |
| 234 |
|
| 235 |
if (status < 0) { |
| 236 |
ast_log(LOG_ERROR, "Error encoding the Opus frame: %s\n", opus_strerror(status)); |
| 237 |
} else { |
| 238 |
struct ast_frame *current = ast_trans_frameout(pvt, |
| 239 |
status, |
| 240 |
OPUS_SAMPLES); |
| 241 |
|
| 242 |
if (!current) { |
| 243 |
continue; |
| 244 |
} else if (last) { |
| 245 |
AST_LIST_NEXT(last, frame_list) = current; |
| 246 |
} else { |
| 247 |
result = current; |
| 248 |
} |
| 249 |
last = current; |
| 250 |
} |
| 251 |
} |
| 252 |
|
| 253 |
|
| 254 |
if (samples) { |
| 255 |
memmove(opvt->buf, opvt->buf + samples, pvt->samples * 2); |
| 256 |
} |
| 257 |
|
| 258 |
return result; |
| 259 |
} |
| 260 |
|
| 261 |
static int opustolin_framein(struct ast_trans_pvt *pvt, struct ast_frame *f) |
| 262 |
{ |
| 263 |
struct opus_coder_pvt *opvt = pvt->pvt; |
| 264 |
int decode_fec; |
| 265 |
int frame_size; |
| 266 |
opus_int16 *dst; |
| 267 |
opus_int32 len; |
| 268 |
unsigned char *src; |
| 269 |
int status; |
| 270 |
|
| 271 |
if (!opvt->inited && f->datalen == 0) { |
| 272 |
return 0; |
| 273 |
} else if (!opvt->inited) { |
| 274 |
status = opus_decoder_construct(pvt, f); |
| 275 |
opvt->inited = 1; |
| 276 |
if (status) { |
| 277 |
return status; |
| 278 |
} |
| 279 |
} |
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
if (f->subclass.format) { |
| 286 |
struct opus_attr *attr = ast_format_get_attribute_data(f->subclass.format); |
| 287 |
|
| 288 |
if (attr) { |
| 289 |
opvt->decode_fec_incoming = attr->fec; |
| 290 |
} |
| 291 |
} |
| 292 |
decode_fec = opvt->decode_fec_incoming; |
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
if (f->datalen == 0 && opvt->previous_lost) { |
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
decode_fec = 0; |
| 335 |
opus_decoder_ctl(opvt->opus, OPUS_GET_LAST_PACKET_DURATION(&frame_size)); |
| 336 |
dst = pvt->outbuf.i16 + (pvt->samples * opvt->channels); |
| 337 |
len = 0; |
| 338 |
src = NULL; |
| 339 |
status = opus_decode(opvt->opus, src, len, dst, frame_size, decode_fec); |
| 340 |
if (status < 0) { |
| 341 |
ast_log(LOG_ERROR, "%s\n", opus_strerror(status)); |
| 342 |
} else { |
| 343 |
pvt->samples += status; |
| 344 |
pvt->datalen += status * opvt->channels * sizeof(int16_t); |
| 345 |
} |
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
opvt->previous_lost = (f->datalen == 0 || status < 0); |
| 351 |
return 0; |
| 352 |
} |
| 353 |
|
| 354 |
|
| 355 |
if (f->datalen == 0 && !decode_fec) { |
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
decode_fec = 0; |
| 361 |
opus_decoder_ctl(opvt->opus, OPUS_GET_LAST_PACKET_DURATION(&frame_size)); |
| 362 |
dst = pvt->outbuf.i16 + (pvt->samples * opvt->channels); |
| 363 |
len = f->datalen; |
| 364 |
src = NULL; |
| 365 |
status = opus_decode(opvt->opus, src, len, dst, frame_size, decode_fec); |
| 366 |
if (status < 0) { |
| 367 |
ast_log(LOG_ERROR, "%s\n", opus_strerror(status)); |
| 368 |
} else { |
| 369 |
pvt->samples += status; |
| 370 |
pvt->datalen += status * opvt->channels * sizeof(int16_t); |
| 371 |
} |
| 372 |
opvt->previous_lost = (f->datalen == 0 || status < 0); |
| 373 |
return 0; |
| 374 |
} |
| 375 |
|
| 376 |
|
| 377 |
if (f->datalen == 0) { |
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
status = 0; |
| 387 |
if (status < 0) { |
| 388 |
ast_log(LOG_ERROR, "%s\n", opus_strerror(status)); |
| 389 |
} else { |
| 390 |
pvt->samples += status; |
| 391 |
pvt->datalen += status * opvt->channels * sizeof(int16_t); |
| 392 |
} |
| 393 |
opvt->previous_lost = (f->datalen == 0 || status < 0); |
| 394 |
return 0; |
| 395 |
} |
| 396 |
|
| 397 |
|
| 398 |
if (!opvt->previous_lost) { |
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
decode_fec = 0; |
| 404 |
frame_size = BUFFER_SAMPLES / opvt->multiplier; |
| 405 |
dst = pvt->outbuf.i16 + (pvt->samples * opvt->channels); |
| 406 |
len = f->datalen; |
| 407 |
src = f->data.ptr; |
| 408 |
status = opus_decode(opvt->opus, src, len, dst, frame_size, decode_fec); |
| 409 |
if (status < 0) { |
| 410 |
ast_log(LOG_ERROR, "%s\n", opus_strerror(status)); |
| 411 |
} else { |
| 412 |
pvt->samples += status; |
| 413 |
pvt->datalen += status * opvt->channels * sizeof(int16_t); |
| 414 |
} |
| 415 |
opvt->previous_lost = (f->datalen == 0 || status < 0); |
| 416 |
return 0; |
| 417 |
} |
| 418 |
|
| 419 |
|
| 420 |
if (!decode_fec) { |
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
decode_fec = 0; |
| 431 |
opus_decoder_ctl(opvt->opus, OPUS_GET_LAST_PACKET_DURATION(&frame_size)); |
| 432 |
dst = pvt->outbuf.i16 + (pvt->samples * opvt->channels); |
| 433 |
len = 0; |
| 434 |
src = NULL; |
| 435 |
status = opus_decode(opvt->opus, src, len, dst, frame_size, decode_fec); |
| 436 |
if (status < 0) { |
| 437 |
ast_log(LOG_ERROR, "%s\n", opus_strerror(status)); |
| 438 |
} else { |
| 439 |
pvt->samples += status; |
| 440 |
pvt->datalen += status * opvt->channels * sizeof(int16_t); |
| 441 |
} |
| 442 |
decode_fec = 0; |
| 443 |
frame_size = BUFFER_SAMPLES / opvt->multiplier; |
| 444 |
dst = pvt->outbuf.i16 + (pvt->samples * opvt->channels); |
| 445 |
len = f->datalen; |
| 446 |
src = f->data.ptr; |
| 447 |
status = opus_decode(opvt->opus, src, len, dst, frame_size, decode_fec); |
| 448 |
if (status < 0) { |
| 449 |
ast_log(LOG_ERROR, "%s\n", opus_strerror(status)); |
| 450 |
} else { |
| 451 |
pvt->samples += status; |
| 452 |
pvt->datalen += status * opvt->channels * sizeof(int16_t); |
| 453 |
} |
| 454 |
opvt->previous_lost = (f->datalen == 0 || status < 0); |
| 455 |
return 0; |
| 456 |
} |
| 457 |
|
| 458 |
|
| 459 |
{ |
| 460 |
decode_fec = 1; |
| 461 |
opus_decoder_ctl(opvt->opus, OPUS_GET_LAST_PACKET_DURATION(&frame_size)); |
| 462 |
dst = pvt->outbuf.i16 + (pvt->samples * opvt->channels); |
| 463 |
len = f->datalen; |
| 464 |
src = f->data.ptr; |
| 465 |
status = opus_decode(opvt->opus, src, len, dst, frame_size, decode_fec); |
| 466 |
if (status < 0) { |
| 467 |
ast_log(LOG_ERROR, "%s\n", opus_strerror(status)); |
| 468 |
} else { |
| 469 |
pvt->samples += status; |
| 470 |
pvt->datalen += status * opvt->channels * sizeof(int16_t); |
| 471 |
} |
| 472 |
decode_fec = 0; |
| 473 |
frame_size = BUFFER_SAMPLES / opvt->multiplier; |
| 474 |
dst = pvt->outbuf.i16 + (pvt->samples * opvt->channels); |
| 475 |
len = f->datalen; |
| 476 |
src = f->data.ptr; |
| 477 |
status = opus_decode(opvt->opus, src, len, dst, frame_size, decode_fec); |
| 478 |
if (status < 0) { |
| 479 |
ast_log(LOG_ERROR, "%s\n", opus_strerror(status)); |
| 480 |
} else { |
| 481 |
pvt->samples += status; |
| 482 |
pvt->datalen += status * opvt->channels * sizeof(int16_t); |
| 483 |
} |
| 484 |
opvt->previous_lost = (f->datalen == 0 || status < 0); |
| 485 |
return 0; |
| 486 |
} |
| 487 |
} |
| 488 |
|
| 489 |
static void lintoopus_destroy(struct ast_trans_pvt *arg) |
| 490 |
{ |
| 491 |
struct opus_coder_pvt *opvt = arg->pvt; |
| 492 |
|
| 493 |
if (!opvt || !opvt->opus) { |
| 494 |
return; |
| 495 |
} |
| 496 |
|
| 497 |
opus_encoder_destroy(opvt->opus); |
| 498 |
opvt->opus = NULL; |
| 499 |
|
| 500 |
ast_atomic_fetchadd_int(&usage.encoders, -1); |
| 501 |
|
| 502 |
ast_debug(3, "Destroyed encoder #%d (%d->opus)\n", opvt->id, opvt->sampling_rate); |
| 503 |
} |
| 504 |
|
| 505 |
static void opustolin_destroy(struct ast_trans_pvt *arg) |
| 506 |
{ |
| 507 |
struct opus_coder_pvt *opvt = arg->pvt; |
| 508 |
|
| 509 |
if (!opvt || !opvt->opus) { |
| 510 |
return; |
| 511 |
} |
| 512 |
|
| 513 |
opus_decoder_destroy(opvt->opus); |
| 514 |
opvt->opus = NULL; |
| 515 |
|
| 516 |
ast_atomic_fetchadd_int(&usage.decoders, -1); |
| 517 |
|
| 518 |
ast_debug(3, "Destroyed decoder #%d (opus->%d)\n", opvt->id, opvt->sampling_rate); |
| 519 |
} |
| 520 |
|
| 521 |
static char *handle_cli_opus_show(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) |
| 522 |
{ |
| 523 |
struct codec_usage copy; |
| 524 |
|
| 525 |
switch (cmd) { |
| 526 |
case CLI_INIT: |
| 527 |
e->command = "opus show"; |
| 528 |
e->usage = |
| 529 |
"Usage: opus show\n" |
| 530 |
" Displays Opus encoder/decoder utilization.\n"; |
| 531 |
return NULL; |
| 532 |
case CLI_GENERATE: |
| 533 |
return NULL; |
| 534 |
} |
| 535 |
|
| 536 |
if (a->argc != 2) { |
| 537 |
return CLI_SHOWUSAGE; |
| 538 |
} |
| 539 |
|
| 540 |
copy = usage; |
| 541 |
|
| 542 |
ast_cli(a->fd, "%d/%d encoders/decoders are in use.\n", copy.encoders, copy.decoders); |
| 543 |
|
| 544 |
return CLI_SUCCESS; |
| 545 |
} |
| 546 |
|
| 547 |
|
| 548 |
static struct ast_translator opustolin = { |
| 549 |
.table_cost = AST_TRANS_COST_LY_LL_ORIGSAMP, |
| 550 |
.name = "opustolin", |
| 551 |
.src_codec = { |
| 552 |
.name = "opus", |
| 553 |
.type = AST_MEDIA_TYPE_AUDIO, |
| 554 |
.sample_rate = 48000, |
| 555 |
}, |
| 556 |
.dst_codec = { |
| 557 |
.name = "slin", |
| 558 |
.type = AST_MEDIA_TYPE_AUDIO, |
| 559 |
.sample_rate = 8000, |
| 560 |
}, |
| 561 |
.format = "slin", |
| 562 |
.newpvt = opustolin_new, |
| 563 |
.framein = opustolin_framein, |
| 564 |
.destroy = opustolin_destroy, |
| 565 |
.sample = opus_sample, |
| 566 |
.desc_size = sizeof(struct opus_coder_pvt), |
| 567 |
.buffer_samples = (BUFFER_SAMPLES / (48000 / 8000)) * 2, |
| 568 |
.buf_size = (BUFFER_SAMPLES / (48000 / 8000)) * MAX_CHANNELS * sizeof(opus_int16) * 2, |
| 569 |
.native_plc = 1, |
| 570 |
}; |
| 571 |
|
| 572 |
static struct ast_translator lintoopus = { |
| 573 |
.table_cost = AST_TRANS_COST_LL_LY_ORIGSAMP, |
| 574 |
.name = "lintoopus", |
| 575 |
.src_codec = { |
| 576 |
.name = "slin", |
| 577 |
.type = AST_MEDIA_TYPE_AUDIO, |
| 578 |
.sample_rate = 8000, |
| 579 |
}, |
| 580 |
.dst_codec = { |
| 581 |
.name = "opus", |
| 582 |
.type = AST_MEDIA_TYPE_AUDIO, |
| 583 |
.sample_rate = 48000, |
| 584 |
}, |
| 585 |
.format = "opus", |
| 586 |
.newpvt = lintoopus_new, |
| 587 |
.framein = lintoopus_framein, |
| 588 |
.frameout = lintoopus_frameout, |
| 589 |
.destroy = lintoopus_destroy, |
| 590 |
.sample = slin8_sample, |
| 591 |
.desc_size = sizeof(struct opus_coder_pvt), |
| 592 |
.buffer_samples = BUFFER_SAMPLES, |
| 593 |
.buf_size = BUFFER_SAMPLES * 2, |
| 594 |
}; |
| 595 |
|
| 596 |
static struct ast_translator opustolin12 = { |
| 597 |
.table_cost = AST_TRANS_COST_LY_LL_ORIGSAMP - 1, |
| 598 |
.name = "opustolin12", |
| 599 |
.src_codec = { |
| 600 |
.name = "opus", |
| 601 |
.type = AST_MEDIA_TYPE_AUDIO, |
| 602 |
.sample_rate = 48000, |
| 603 |
}, |
| 604 |
.dst_codec = { |
| 605 |
.name = "slin", |
| 606 |
.type = AST_MEDIA_TYPE_AUDIO, |
| 607 |
.sample_rate = 12000, |
| 608 |
}, |
| 609 |
.format = "slin12", |
| 610 |
.newpvt = opustolin_new, |
| 611 |
.framein = opustolin_framein, |
| 612 |
.destroy = opustolin_destroy, |
| 613 |
.sample = opus_sample, |
| 614 |
.desc_size = sizeof(struct opus_coder_pvt), |
| 615 |
.buffer_samples = (BUFFER_SAMPLES / (48000 / 12000)) * 2, |
| 616 |
.buf_size = (BUFFER_SAMPLES / (48000 / 12000)) * MAX_CHANNELS * sizeof(opus_int16) * 2, |
| 617 |
.native_plc = 1, |
| 618 |
}; |
| 619 |
|
| 620 |
static struct ast_translator lin12toopus = { |
| 621 |
.table_cost = AST_TRANS_COST_LL_LY_ORIGSAMP - 1, |
| 622 |
.name = "lin12toopus", |
| 623 |
.src_codec = { |
| 624 |
.name = "slin", |
| 625 |
.type = AST_MEDIA_TYPE_AUDIO, |
| 626 |
.sample_rate = 12000, |
| 627 |
}, |
| 628 |
.dst_codec = { |
| 629 |
.name = "opus", |
| 630 |
.type = AST_MEDIA_TYPE_AUDIO, |
| 631 |
.sample_rate = 48000, |
| 632 |
}, |
| 633 |
.format = "opus", |
| 634 |
.newpvt = lintoopus_new, |
| 635 |
.framein = lintoopus_framein, |
| 636 |
.frameout = lintoopus_frameout, |
| 637 |
.destroy = lintoopus_destroy, |
| 638 |
.desc_size = sizeof(struct opus_coder_pvt), |
| 639 |
.buffer_samples = BUFFER_SAMPLES, |
| 640 |
.buf_size = BUFFER_SAMPLES * 2, |
| 641 |
}; |
| 642 |
|
| 643 |
static struct ast_translator opustolin16 = { |
| 644 |
.table_cost = AST_TRANS_COST_LY_LL_ORIGSAMP - 2, |
| 645 |
.name = "opustolin16", |
| 646 |
.src_codec = { |
| 647 |
.name = "opus", |
| 648 |
.type = AST_MEDIA_TYPE_AUDIO, |
| 649 |
.sample_rate = 48000, |
| 650 |
}, |
| 651 |
.dst_codec = { |
| 652 |
.name = "slin", |
| 653 |
.type = AST_MEDIA_TYPE_AUDIO, |
| 654 |
.sample_rate = 16000, |
| 655 |
}, |
| 656 |
.format = "slin16", |
| 657 |
.newpvt = opustolin_new, |
| 658 |
.framein = opustolin_framein, |
| 659 |
.destroy = opustolin_destroy, |
| 660 |
.sample = opus_sample, |
| 661 |
.desc_size = sizeof(struct opus_coder_pvt), |
| 662 |
.buffer_samples = (BUFFER_SAMPLES / (48000 / 16000)) * 2, |
| 663 |
.buf_size = (BUFFER_SAMPLES / (48000 / 16000)) * MAX_CHANNELS * sizeof(opus_int16) * 2, |
| 664 |
.native_plc = 1, |
| 665 |
}; |
| 666 |
|
| 667 |
static struct ast_translator lin16toopus = { |
| 668 |
.table_cost = AST_TRANS_COST_LL_LY_ORIGSAMP - 2, |
| 669 |
.name = "lin16toopus", |
| 670 |
.src_codec = { |
| 671 |
.name = "slin", |
| 672 |
.type = AST_MEDIA_TYPE_AUDIO, |
| 673 |
.sample_rate = 16000, |
| 674 |
}, |
| 675 |
.dst_codec = { |
| 676 |
.name = "opus", |
| 677 |
.type = AST_MEDIA_TYPE_AUDIO, |
| 678 |
.sample_rate = 48000, |
| 679 |
}, |
| 680 |
.format = "opus", |
| 681 |
.newpvt = lintoopus_new, |
| 682 |
.framein = lintoopus_framein, |
| 683 |
.frameout = lintoopus_frameout, |
| 684 |
.destroy = lintoopus_destroy, |
| 685 |
.sample = slin16_sample, |
| 686 |
.desc_size = sizeof(struct opus_coder_pvt), |
| 687 |
.buffer_samples = BUFFER_SAMPLES, |
| 688 |
.buf_size = BUFFER_SAMPLES * 2, |
| 689 |
}; |
| 690 |
|
| 691 |
static struct ast_translator opustolin24 = { |
| 692 |
.table_cost = AST_TRANS_COST_LY_LL_ORIGSAMP - 4, |
| 693 |
.name = "opustolin24", |
| 694 |
.src_codec = { |
| 695 |
.name = "opus", |
| 696 |
.type = AST_MEDIA_TYPE_AUDIO, |
| 697 |
.sample_rate = 48000, |
| 698 |
}, |
| 699 |
.dst_codec = { |
| 700 |
.name = "slin", |
| 701 |
.type = AST_MEDIA_TYPE_AUDIO, |
| 702 |
.sample_rate = 24000, |
| 703 |
}, |
| 704 |
.format = "slin24", |
| 705 |
.newpvt = opustolin_new, |
| 706 |
.framein = opustolin_framein, |
| 707 |
.destroy = opustolin_destroy, |
| 708 |
.sample = opus_sample, |
| 709 |
.desc_size = sizeof(struct opus_coder_pvt), |
| 710 |
.buffer_samples = (BUFFER_SAMPLES / (48000 / 24000)) * 2, |
| 711 |
.buf_size = (BUFFER_SAMPLES / (48000 / 24000)) * MAX_CHANNELS * sizeof(opus_int16) * 2, |
| 712 |
.native_plc = 1, |
| 713 |
}; |
| 714 |
|
| 715 |
static struct ast_translator lin24toopus = { |
| 716 |
.table_cost = AST_TRANS_COST_LL_LY_ORIGSAMP - 4, |
| 717 |
.name = "lin24toopus", |
| 718 |
.src_codec = { |
| 719 |
.name = "slin", |
| 720 |
.type = AST_MEDIA_TYPE_AUDIO, |
| 721 |
.sample_rate = 24000, |
| 722 |
}, |
| 723 |
.dst_codec = { |
| 724 |
.name = "opus", |
| 725 |
.type = AST_MEDIA_TYPE_AUDIO, |
| 726 |
.sample_rate = 48000, |
| 727 |
}, |
| 728 |
.format = "opus", |
| 729 |
.newpvt = lintoopus_new, |
| 730 |
.framein = lintoopus_framein, |
| 731 |
.frameout = lintoopus_frameout, |
| 732 |
.destroy = lintoopus_destroy, |
| 733 |
.desc_size = sizeof(struct opus_coder_pvt), |
| 734 |
.buffer_samples = BUFFER_SAMPLES, |
| 735 |
.buf_size = BUFFER_SAMPLES * 2, |
| 736 |
}; |
| 737 |
|
| 738 |
static struct ast_translator opustolin48 = { |
| 739 |
.table_cost = AST_TRANS_COST_LY_LL_ORIGSAMP - 8, |
| 740 |
.name = "opustolin48", |
| 741 |
.src_codec = { |
| 742 |
.name = "opus", |
| 743 |
.type = AST_MEDIA_TYPE_AUDIO, |
| 744 |
.sample_rate = 48000, |
| 745 |
}, |
| 746 |
.dst_codec = { |
| 747 |
.name = "slin", |
| 748 |
.type = AST_MEDIA_TYPE_AUDIO, |
| 749 |
.sample_rate = 48000, |
| 750 |
}, |
| 751 |
.format = "slin48", |
| 752 |
.newpvt = opustolin_new, |
| 753 |
.framein = opustolin_framein, |
| 754 |
.destroy = opustolin_destroy, |
| 755 |
.sample = opus_sample, |
| 756 |
.desc_size = sizeof(struct opus_coder_pvt), |
| 757 |
.buffer_samples = BUFFER_SAMPLES * 2, |
| 758 |
.buf_size = BUFFER_SAMPLES * MAX_CHANNELS * sizeof(opus_int16) * 2, |
| 759 |
.native_plc = 1, |
| 760 |
}; |
| 761 |
|
| 762 |
static struct ast_translator lin48toopus = { |
| 763 |
.table_cost = AST_TRANS_COST_LL_LY_ORIGSAMP - 8, |
| 764 |
.name = "lin48toopus", |
| 765 |
.src_codec = { |
| 766 |
.name = "slin", |
| 767 |
.type = AST_MEDIA_TYPE_AUDIO, |
| 768 |
.sample_rate = 48000, |
| 769 |
}, |
| 770 |
.dst_codec = { |
| 771 |
.name = "opus", |
| 772 |
.type = AST_MEDIA_TYPE_AUDIO, |
| 773 |
.sample_rate = 48000, |
| 774 |
}, |
| 775 |
.format = "opus", |
| 776 |
.newpvt = lintoopus_new, |
| 777 |
.framein = lintoopus_framein, |
| 778 |
.frameout = lintoopus_frameout, |
| 779 |
.destroy = lintoopus_destroy, |
| 780 |
.desc_size = sizeof(struct opus_coder_pvt), |
| 781 |
.buffer_samples = BUFFER_SAMPLES, |
| 782 |
.buf_size = BUFFER_SAMPLES * 2, |
| 783 |
}; |
| 784 |
|
| 785 |
static struct ast_cli_entry cli[] = { |
| 786 |
AST_CLI_DEFINE(handle_cli_opus_show, "Display Opus codec utilization.") |
| 787 |
}; |
| 788 |
|
| 789 |
static int opus_samples(struct ast_frame *frame) |
| 790 |
{ |
| 791 |
opus_int32 sampling_rate = 48000; |
| 792 |
|
| 793 |
return opus_packet_get_nb_samples(frame->data.ptr, frame->datalen, sampling_rate); |
| 794 |
} |
| 795 |
|
| 796 |
static int reload(void) |
| 797 |
{ |
| 798 |
|
| 799 |
return AST_MODULE_LOAD_SUCCESS; |
| 800 |
} |
| 801 |
|
| 802 |
static int unload_module(void) |
| 803 |
{ |
| 804 |
int res; |
| 805 |
|
| 806 |
opus_codec->samples_count = opus_samples_previous; |
| 807 |
ao2_ref(opus_codec, -1); |
| 808 |
|
| 809 |
res = ast_unregister_translator(&opustolin); |
| 810 |
res |= ast_unregister_translator(&lintoopus); |
| 811 |
res |= ast_unregister_translator(&opustolin12); |
| 812 |
res |= ast_unregister_translator(&lin12toopus); |
| 813 |
res |= ast_unregister_translator(&opustolin16); |
| 814 |
res |= ast_unregister_translator(&lin16toopus); |
| 815 |
res |= ast_unregister_translator(&opustolin24); |
| 816 |
res |= ast_unregister_translator(&lin24toopus); |
| 817 |
res |= ast_unregister_translator(&opustolin48); |
| 818 |
res |= ast_unregister_translator(&lin48toopus); |
| 819 |
|
| 820 |
ast_cli_unregister_multiple(cli, ARRAY_LEN(cli)); |
| 821 |
|
| 822 |
return res; |
| 823 |
} |
| 824 |
|
| 825 |
static int load_module(void) |
| 826 |
{ |
| 827 |
int res; |
| 828 |
|
| 829 |
opus_codec = ast_codec_get("opus", AST_MEDIA_TYPE_AUDIO, 48000); |
| 830 |
opus_samples_previous = opus_codec->samples_count; |
| 831 |
opus_codec->samples_count = opus_samples; |
| 832 |
|
| 833 |
res = ast_register_translator(&opustolin); |
| 834 |
res |= ast_register_translator(&lintoopus); |
| 835 |
res |= ast_register_translator(&opustolin12); |
| 836 |
res |= ast_register_translator(&lin12toopus); |
| 837 |
res |= ast_register_translator(&opustolin16); |
| 838 |
res |= ast_register_translator(&lin16toopus); |
| 839 |
res |= ast_register_translator(&opustolin24); |
| 840 |
res |= ast_register_translator(&lin24toopus); |
| 841 |
res |= ast_register_translator(&opustolin48); |
| 842 |
res |= ast_register_translator(&lin48toopus); |
| 843 |
|
| 844 |
ast_cli_register_multiple(cli, ARRAY_LEN(cli)); |
| 845 |
|
| 846 |
return res; |
| 847 |
} |
| 848 |
|
| 849 |
AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Opus Coder/Decoder", |
| 850 |
.load = load_module, |
| 851 |
.unload = unload_module, |
| 852 |
.reload = reload, |
| 853 |
); |