From 8d18dac6769fb060693b29d5895e1f14f370acd3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jaron=20Vi=C3=ABtor?= Date: Sat, 10 May 2014 22:06:22 +0200 Subject: [PATCH 1/3] Updated patch for Asterisk 12.2.0 Updated for 12.2.0, removed VP8 passthrough parts since this is already part of 12.2.0. --- asterisk_12.2.0_opus.diff | 1929 +++++++++++++++++++++++++++++++++++++ asterisk_opus+vp8.diff | 1199 ----------------------- 2 files changed, 1929 insertions(+), 1199 deletions(-) create mode 100644 asterisk_12.2.0_opus.diff delete mode 100644 asterisk_opus+vp8.diff diff --git a/asterisk_12.2.0_opus.diff b/asterisk_12.2.0_opus.diff new file mode 100644 index 0000000..b2788ee --- /dev/null +++ b/asterisk_12.2.0_opus.diff @@ -0,0 +1,1929 @@ +diff -crBdN asterisk-12.2.0/build_tools/menuselect-deps.in asterisk-12.2.0-opus/build_tools/menuselect-deps.in +*** asterisk-12.2.0/build_tools/menuselect-deps.in 2014-05-10 18:57:44.000000000 +0200 +--- asterisk-12.2.0-opus/build_tools/menuselect-deps.in 2014-05-10 19:06:46.000000000 +0200 +*************** +*** 42,47 **** +--- 42,48 ---- + NEON29=@PBX_NEON29@ + OGG=@PBX_OGG@ + OPENH323=@PBX_OPENH323@ ++ OPUS=@PBX_OPUS@ + OSPTK=@PBX_OSPTK@ + OSS=@PBX_OSS@ + PGSQL=@PBX_PGSQL@ +diff -crBdN asterisk-12.2.0/codecs/codec_opus.c asterisk-12.2.0-opus/codecs/codec_opus.c +*** asterisk-12.2.0/codecs/codec_opus.c 1970-01-01 01:00:00.000000000 +0100 +--- asterisk-12.2.0-opus/codecs/codec_opus.c 2014-05-10 19:06:46.000000000 +0200 +*************** +*** 0 **** +--- 1,529 ---- ++ /* ++ * Asterisk -- An open source telephony toolkit. ++ * ++ * Copyright (C) 1999 - 2005, Digium, Inc. ++ * ++ * Mark Spencer ++ * ++ * ++ * See http://www.asterisk.org for more information about ++ * the Asterisk project. Please do not directly contact ++ * any of the maintainers of this project for assistance; ++ * the project provides a web site, mailing lists and IRC ++ * channels for your use. ++ * ++ * This program is free software, distributed under the terms of ++ * the GNU General Public License Version 2. See the LICENSE file ++ * at the top of the source tree. ++ */ ++ ++ /*! \file ++ * ++ * \brief Translate between signed linear and Opus (Open Codec) ++ * ++ * \author Lorenzo Miniero ++ * ++ * \note This work was motivated by Mozilla ++ * ++ * \ingroup codecs ++ * ++ * \extref The Opus library - http://opus-codec.org ++ * ++ */ ++ ++ /*** MODULEINFO ++ opus ++ core ++ ***/ ++ ++ #include "asterisk.h" ++ ++ ASTERISK_FILE_VERSION(__FILE__, "$Revision: 328209 $") ++ ++ #include ++ ++ #include "asterisk/translate.h" ++ #include "asterisk/module.h" ++ #include "asterisk/config.h" ++ #include "asterisk/utils.h" ++ #include "asterisk/cli.h" ++ ++ ++ #define BUFFER_SAMPLES 8000 ++ #define OPUS_SAMPLES 160 ++ ++ #define USE_FEC 0 ++ ++ ++ /* Sample frame data */ ++ #include "asterisk/slin.h" ++ #include "ex_opus.h" ++ ++ /* FIXME: Test */ ++ #include "asterisk/file.h" ++ ++ ++ static int encid = 0; ++ static int decid = 0; ++ ++ static int opusdebug = 0; ++ ++ ++ /* Private structures */ ++ struct opus_coder_pvt { ++ void *opus; /* May be encoder or decoder */ ++ int sampling_rate; ++ int multiplier; ++ int fec; ++ ++ int id; ++ ++ int16_t buf[BUFFER_SAMPLES]; /* FIXME */ ++ int framesize; ++ ++ FILE *file; ++ }; ++ ++ ++ /* Helper methods */ ++ static int opus_encoder_construct(struct ast_trans_pvt *pvt, int sampling_rate) { ++ if(sampling_rate != 8000 && sampling_rate != 12000 && sampling_rate != 16000 && sampling_rate != 24000 && sampling_rate != 48000) ++ return -1; ++ struct opus_coder_pvt *opvt = pvt->pvt; ++ opvt->sampling_rate = sampling_rate; ++ opvt->multiplier = 48000/sampling_rate; ++ opvt->fec = USE_FEC; ++ int error = 0; ++ opvt->opus = opus_encoder_create(sampling_rate, 1, OPUS_APPLICATION_VOIP, &error); ++ if(error != OPUS_OK) { ++ if(opusdebug) ++ ast_verbose("[Opus] Ops! got an error creating the Opus encoder: %d (%s)\n", error, opus_strerror(error)); ++ return -1; ++ } ++ if(sampling_rate == 8000) ++ opus_encoder_ctl(opvt->opus, OPUS_SET_MAX_BANDWIDTH(OPUS_BANDWIDTH_NARROWBAND)); ++ else if(sampling_rate == 12000) ++ opus_encoder_ctl(opvt->opus, OPUS_SET_MAX_BANDWIDTH(OPUS_BANDWIDTH_MEDIUMBAND)); ++ else if(sampling_rate == 16000) ++ opus_encoder_ctl(opvt->opus, OPUS_SET_MAX_BANDWIDTH(OPUS_BANDWIDTH_WIDEBAND)); ++ else if(sampling_rate == 24000) ++ opus_encoder_ctl(opvt->opus, OPUS_SET_MAX_BANDWIDTH(OPUS_BANDWIDTH_SUPERWIDEBAND)); ++ else if(sampling_rate == 48000) ++ opus_encoder_ctl(opvt->opus, OPUS_SET_MAX_BANDWIDTH(OPUS_BANDWIDTH_FULLBAND)); ++ opus_encoder_ctl(opvt->opus, OPUS_SET_INBAND_FEC(opvt->fec)); ++ opvt->framesize = sampling_rate/50; ++ opvt->id = ++encid; ++ if(opusdebug) ++ ast_verbose("[Opus] Created encoder #%d (%d->opus)\n", opvt->id, sampling_rate); ++ ++ return 0; ++ } ++ ++ static int opus_decoder_construct(struct ast_trans_pvt *pvt, int sampling_rate) { ++ if(sampling_rate != 8000 && sampling_rate != 12000 && sampling_rate != 16000 && sampling_rate != 24000 && sampling_rate != 48000) ++ return -1; ++ struct opus_coder_pvt *opvt = pvt->pvt; ++ opvt->sampling_rate = sampling_rate; ++ opvt->multiplier = 48000/sampling_rate; ++ opvt->fec = USE_FEC; /* FIXME: should be triggered by chan_sip */ ++ int error = 0; ++ opvt->opus = opus_decoder_create(sampling_rate, 1, &error); ++ if(error != OPUS_OK) { ++ if(opusdebug) ++ ast_verbose("[Opus] Ops! got an error creating the Opus decoder: %d (%s)\n", error, opus_strerror(error)); ++ return -1; ++ } ++ opvt->id = ++decid; ++ if(opusdebug) ++ ast_verbose("[Opus] Created decoder #%d (opus->%d)\n", opvt->id, sampling_rate); ++ ++ if(opusdebug > 1) { ++ char filename[50]; ++ sprintf(filename, "/home/lminiero/opusdec-%04d-%d.raw", opvt->id, opvt->sampling_rate); ++ opvt->file = fopen(filename, "wb"); ++ } ++ ++ return 0; ++ } ++ ++ /* Translator callbacks */ ++ static int lintoopus_new(struct ast_trans_pvt *pvt) { ++ return opus_encoder_construct(pvt, 8000); ++ } ++ ++ static int lin12toopus_new(struct ast_trans_pvt *pvt) { ++ return opus_encoder_construct(pvt, 12000); ++ } ++ ++ static int lin16toopus_new(struct ast_trans_pvt *pvt) { ++ return opus_encoder_construct(pvt, 16000); ++ } ++ ++ static int lin24toopus_new(struct ast_trans_pvt *pvt) { ++ return opus_encoder_construct(pvt, 24000); ++ } ++ ++ static int lin48toopus_new(struct ast_trans_pvt *pvt) { ++ return opus_encoder_construct(pvt, 48000); ++ } ++ ++ static int opustolin_new(struct ast_trans_pvt *pvt) { ++ return opus_decoder_construct(pvt, 8000); ++ } ++ ++ static int opustolin12_new(struct ast_trans_pvt *pvt) { ++ return opus_decoder_construct(pvt, 12000); ++ } ++ ++ static int opustolin16_new(struct ast_trans_pvt *pvt) { ++ return opus_decoder_construct(pvt, 16000); ++ } ++ ++ static int opustolin24_new(struct ast_trans_pvt *pvt) { ++ return opus_decoder_construct(pvt, 24000); ++ } ++ ++ static int opustolin48_new(struct ast_trans_pvt *pvt) { ++ return opus_decoder_construct(pvt, 48000); ++ } ++ ++ static int lintoopus_framein(struct ast_trans_pvt *pvt, struct ast_frame *f) { ++ struct opus_coder_pvt *opvt = pvt->pvt; ++ ++ /* XXX We should look at how old the rest of our stream is, and if it ++ is too old, then we should overwrite it entirely, otherwise we can ++ get artifacts of earlier talk that do not belong */ ++ memcpy(opvt->buf + pvt->samples, f->data.ptr, f->datalen); ++ pvt->samples += f->samples; ++ ++ return 0; ++ } ++ ++ static struct ast_frame *lintoopus_frameout(struct ast_trans_pvt *pvt) { ++ struct opus_coder_pvt *opvt = pvt->pvt; ++ ++ /* We can't work on anything less than a frame in size */ ++ if (pvt->samples < opvt->framesize) ++ return NULL; ++ ++ int datalen = 0; /* output bytes */ ++ int samples = 0; /* output samples */ ++ ++ /* Encode 160 samples (or more if it's not narrowband) */ ++ if(opusdebug > 1) ++ ast_verbose("[Opus] [Encoder #%d (%d)] %d samples, %d bytes\n", opvt->id, opvt->sampling_rate, opvt->framesize, opvt->framesize*2); ++ datalen = opus_encode(opvt->opus, opvt->buf, opvt->framesize, pvt->outbuf.uc, BUFFER_SAMPLES); ++ if(datalen < 0) { ++ if(opusdebug) ++ ast_verbose("[Opus] Ops! got an error encoding the Opus frame: %d (%s)\n", datalen, opus_strerror(datalen)); ++ return NULL; ++ } ++ samples += opvt->framesize; ++ pvt->samples -= opvt->framesize; ++ /* Move the data at the end of the buffer to the front */ ++ if (pvt->samples) ++ memmove(opvt->buf, opvt->buf + samples, pvt->samples * 2); ++ ++ if(opusdebug > 1) ++ ast_verbose("[Opus] [Encoder #%d (%d)] >> Got %d samples, %d bytes\n", opvt->id, opvt->sampling_rate, opvt->multiplier*samples, datalen); ++ ++ if(opvt->file) ++ fwrite(opvt->buf, sizeof(int16_t), opvt->multiplier*samples, opvt->file); ++ ++ return ast_trans_frameout(pvt, datalen, opvt->multiplier*samples); ++ } ++ ++ static int opustolin_framein(struct ast_trans_pvt *pvt, struct ast_frame *f) { ++ struct opus_coder_pvt *opvt = pvt->pvt; ++ /* Decode */ ++ if(opusdebug > 1) ++ ast_verbose("[Opus] [Decoder #%d (%d)] %d samples, %d bytes\n", opvt->id, opvt->sampling_rate, f->samples, f->datalen); ++ int error = opus_decode(opvt->opus, f->data.ptr, f->datalen, pvt->outbuf.i16, BUFFER_SAMPLES, opvt->fec); ++ if(error < 0) { ++ if(opusdebug) ++ ast_verbose("[Opus] Ops! got an error decoding the Opus frame: %d (%s)\n", error, opus_strerror(error)); ++ return -1; ++ } ++ pvt->samples += error; ++ pvt->datalen += error*2; ++ ++ if(opusdebug > 1) ++ ast_verbose("[Opus] [Decoder #%d (%d)] >> Got %d samples, %d bytes\n", opvt->id, opvt->sampling_rate, pvt->samples, pvt->datalen); ++ ++ if(opvt->file) ++ fwrite(pvt->outbuf.i16, sizeof(int16_t), pvt->samples, opvt->file); ++ ++ return 0; ++ } ++ ++ static void lintoopus_destroy(struct ast_trans_pvt *arg) { ++ struct opus_coder_pvt *opvt = arg->pvt; ++ if(opvt == NULL || opvt->opus == NULL) ++ return; ++ opus_encoder_destroy(opvt->opus); ++ if(opusdebug) ++ ast_verbose("[Opus] Destroyed encoder #%d (%d->opus)\n", opvt->id, opvt->sampling_rate); ++ opvt->opus = NULL; ++ ++ if(opvt->file) ++ fclose(opvt->file); ++ opvt->file = NULL; ++ } ++ ++ static void opustolin_destroy(struct ast_trans_pvt *arg) { ++ struct opus_coder_pvt *opvt = arg->pvt; ++ if(opvt == NULL || opvt->opus == NULL) ++ return; ++ opus_decoder_destroy(opvt->opus); ++ if(opusdebug) ++ ast_verbose("[Opus] Destroyed decoder #%d (opus->%d)\n", opvt->id, opvt->sampling_rate); ++ opvt->opus = NULL; ++ ++ if(opvt->file) ++ fclose(opvt->file); ++ opvt->file = NULL; ++ } ++ ++ ++ /* Translators */ ++ static struct ast_translator lintoopus = { ++ .name = "lintoopus", ++ .newpvt = lintoopus_new, ++ .framein = lintoopus_framein, ++ .frameout = lintoopus_frameout, ++ .destroy = lintoopus_destroy, ++ .sample = slin8_sample, ++ .desc_size = sizeof(struct opus_coder_pvt), ++ .buffer_samples = BUFFER_SAMPLES, ++ .buf_size = BUFFER_SAMPLES * 2, ++ }; ++ ++ static struct ast_translator lin12toopus = { ++ .name = "lin12toopus", ++ .newpvt = lin12toopus_new, ++ .framein = lintoopus_framein, ++ .frameout = lintoopus_frameout, ++ .destroy = lintoopus_destroy, ++ .desc_size = sizeof(struct opus_coder_pvt), ++ .buffer_samples = BUFFER_SAMPLES, ++ .buf_size = BUFFER_SAMPLES * 2, ++ }; ++ ++ static struct ast_translator lin16toopus = { ++ .name = "lin16toopus", ++ .newpvt = lin16toopus_new, ++ .framein = lintoopus_framein, ++ .frameout = lintoopus_frameout, ++ .destroy = lintoopus_destroy, ++ .sample = slin16_sample, ++ .desc_size = sizeof(struct opus_coder_pvt), ++ .buffer_samples = BUFFER_SAMPLES, ++ .buf_size = BUFFER_SAMPLES * 2, ++ }; ++ ++ static struct ast_translator lin24toopus = { ++ .name = "lin24toopus", ++ .newpvt = lin24toopus_new, ++ .framein = lintoopus_framein, ++ .frameout = lintoopus_frameout, ++ .destroy = lintoopus_destroy, ++ .desc_size = sizeof(struct opus_coder_pvt), ++ .buffer_samples = BUFFER_SAMPLES, ++ .buf_size = BUFFER_SAMPLES * 2, ++ }; ++ ++ static struct ast_translator lin48toopus = { ++ .name = "lin48toopus", ++ .newpvt = lin48toopus_new, ++ .framein = lintoopus_framein, ++ .frameout = lintoopus_frameout, ++ .destroy = lintoopus_destroy, ++ .desc_size = sizeof(struct opus_coder_pvt), ++ .buffer_samples = BUFFER_SAMPLES, ++ .buf_size = BUFFER_SAMPLES * 2, ++ }; ++ ++ static struct ast_translator opustolin = { ++ .name = "opustolin", ++ .newpvt = opustolin_new, ++ .framein = opustolin_framein, ++ .destroy = opustolin_destroy, ++ .sample = opus_sample, ++ .desc_size = sizeof(struct opus_coder_pvt), ++ .buffer_samples = BUFFER_SAMPLES, ++ .buf_size = BUFFER_SAMPLES * 2, ++ .native_plc = 1, /* FIXME: needed? */ ++ }; ++ ++ static struct ast_translator opustolin12 = { ++ .name = "opustolin12", ++ .newpvt = opustolin12_new, ++ .framein = opustolin_framein, ++ .destroy = opustolin_destroy, ++ .sample = opus_sample, ++ .desc_size = sizeof(struct opus_coder_pvt), ++ .buffer_samples = BUFFER_SAMPLES, ++ .buf_size = BUFFER_SAMPLES * 2, ++ .native_plc = 1, /* FIXME: needed? */ ++ }; ++ ++ static struct ast_translator opustolin16 = { ++ .name = "opustolin16", ++ .newpvt = opustolin16_new, ++ .framein = opustolin_framein, ++ .destroy = opustolin_destroy, ++ .sample = opus_sample, ++ .desc_size = sizeof(struct opus_coder_pvt), ++ .buffer_samples = BUFFER_SAMPLES, ++ .buf_size = BUFFER_SAMPLES * 2, ++ .native_plc = 1, /* FIXME: needed? */ ++ }; ++ ++ static struct ast_translator opustolin24 = { ++ .name = "opustolin24", ++ .newpvt = opustolin24_new, ++ .framein = opustolin_framein, ++ .destroy = opustolin_destroy, ++ .sample = opus_sample, ++ .desc_size = sizeof(struct opus_coder_pvt), ++ .buffer_samples = BUFFER_SAMPLES, ++ .buf_size = BUFFER_SAMPLES * 2, ++ .native_plc = 1, /* FIXME: needed? */ ++ }; ++ ++ static struct ast_translator opustolin48 = { ++ .name = "opustolin48", ++ .newpvt = opustolin48_new, ++ .framein = opustolin_framein, ++ .destroy = opustolin_destroy, ++ .sample = opus_sample, ++ .desc_size = sizeof(struct opus_coder_pvt), ++ .buffer_samples = BUFFER_SAMPLES, ++ .buf_size = BUFFER_SAMPLES * 2, ++ .native_plc = 1, /* FIXME: needed? */ ++ }; ++ ++ ++ /* Simple CLI interface to enable/disable debugging */ ++ static char *handle_cli_opus_set_debug(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) ++ { ++ switch (cmd) { ++ case CLI_INIT: ++ e->command = "opus set debug"; ++ e->usage = ++ "Usage: opus set debug {status|none|normal|huge}\n" ++ " Enable/Disable Opus debugging: normal only debugs setup and errors, huge debugs every single packet\n"; ++ return NULL; ++ case CLI_GENERATE: ++ return NULL; ++ } ++ ++ if (a->argc != 4) ++ return CLI_SHOWUSAGE; ++ ++ if (!strncasecmp(a->argv[a->argc-1], "status", 6)) { ++ ast_cli(a->fd, "Opus debugging %s\n", opusdebug > 1 ? "huge" : opusdebug > 0 ? "normal" : "none"); ++ return CLI_SUCCESS; ++ } ++ if (!strncasecmp(a->argv[a->argc-1], "huge", 4)) ++ opusdebug = 2; ++ else if (!strncasecmp(a->argv[a->argc-1], "normal", 6)) ++ opusdebug = 1; ++ else if (!strncasecmp(a->argv[a->argc-1], "none", 4)) ++ opusdebug = 0; ++ else ++ return CLI_SHOWUSAGE; ++ ++ ast_cli(a->fd, "Opus debugging %s\n", opusdebug > 1 ? "huge" : opusdebug > 0 ? "normal" : "none"); ++ return CLI_SUCCESS; ++ } ++ ++ static struct ast_cli_entry cli_opus[] = { ++ AST_CLI_DEFINE(handle_cli_opus_set_debug, "Enable/Disable Opus debugging"), ++ }; ++ ++ ++ /* Configuration and module setup */ ++ static int parse_config(int reload) { ++ /* TODO: place stuff to negotiate/enforce here */ ++ return 0; ++ } ++ ++ static int reload(void) { ++ if(parse_config(1)) ++ return AST_MODULE_LOAD_DECLINE; ++ return AST_MODULE_LOAD_SUCCESS; ++ } ++ ++ static int unload_module(void) { ++ int res = 0; ++ ++ res |= ast_unregister_translator(&opustolin); ++ res |= ast_unregister_translator(&lintoopus); ++ res |= ast_unregister_translator(&opustolin12); ++ res |= ast_unregister_translator(&lin12toopus); ++ res |= ast_unregister_translator(&opustolin16); ++ res |= ast_unregister_translator(&lin16toopus); ++ res |= ast_unregister_translator(&opustolin24); ++ res |= ast_unregister_translator(&lin24toopus); ++ res |= ast_unregister_translator(&opustolin48); ++ res |= ast_unregister_translator(&lin48toopus); ++ ++ ast_cli_unregister_multiple(cli_opus, ARRAY_LEN(cli_opus)); ++ ++ return res; ++ } ++ ++ static int load_module(void) { ++ int res = 0; ++ ++ if(parse_config(0)) ++ return AST_MODULE_LOAD_DECLINE; ++ ++ /* 8khz (nb) */ ++ ast_format_set(&opustolin.src_format, AST_FORMAT_OPUS, 0); ++ ast_format_set(&opustolin.dst_format, AST_FORMAT_SLINEAR, 0); ++ ast_format_set(&lintoopus.src_format, AST_FORMAT_SLINEAR, 0); ++ ast_format_set(&lintoopus.dst_format, AST_FORMAT_OPUS, 0); ++ /* 12khz (mb) */ ++ ast_format_set(&opustolin12.src_format, AST_FORMAT_OPUS, 0); ++ ast_format_set(&opustolin12.dst_format, AST_FORMAT_SLINEAR12, 0); ++ ast_format_set(&lin12toopus.src_format, AST_FORMAT_SLINEAR12, 0); ++ ast_format_set(&lin12toopus.dst_format, AST_FORMAT_OPUS, 0); ++ /* 16khz (wb) */ ++ ast_format_set(&opustolin16.src_format, AST_FORMAT_OPUS, 0); ++ ast_format_set(&opustolin16.dst_format, AST_FORMAT_SLINEAR16, 0); ++ ast_format_set(&lin16toopus.src_format, AST_FORMAT_SLINEAR16, 0); ++ ast_format_set(&lin16toopus.dst_format, AST_FORMAT_OPUS, 0); ++ /* 24khz (swb) */ ++ ast_format_set(&opustolin24.src_format, AST_FORMAT_OPUS, 0); ++ ast_format_set(&opustolin24.dst_format, AST_FORMAT_SLINEAR24, 0); ++ ast_format_set(&lin24toopus.src_format, AST_FORMAT_SLINEAR24, 0); ++ ast_format_set(&lin24toopus.dst_format, AST_FORMAT_OPUS, 0); ++ /* 48khz (fb) */ ++ ast_format_set(&opustolin48.src_format, AST_FORMAT_OPUS, 0); ++ ast_format_set(&opustolin48.dst_format, AST_FORMAT_SLINEAR48, 0); ++ ast_format_set(&lin48toopus.src_format, AST_FORMAT_SLINEAR48, 0); ++ ast_format_set(&lin48toopus.dst_format, AST_FORMAT_OPUS, 0); ++ ++ res |= ast_register_translator(&opustolin); ++ res |= ast_register_translator(&lintoopus); ++ res |= ast_register_translator(&opustolin12); ++ res |= ast_register_translator(&lin12toopus); ++ res |= ast_register_translator(&opustolin16); ++ res |= ast_register_translator(&lin16toopus); ++ res |= ast_register_translator(&opustolin24); ++ res |= ast_register_translator(&lin24toopus); ++ res |= ast_register_translator(&opustolin48); ++ res |= ast_register_translator(&lin48toopus); ++ ++ ast_cli_register_multiple(cli_opus, sizeof(cli_opus) / sizeof(struct ast_cli_entry)); ++ ++ return res; ++ } ++ ++ AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Opus Coder/Decoder", ++ .load = load_module, ++ .unload = unload_module, ++ .reload = reload, ++ ); +diff -crBdN asterisk-12.2.0/codecs/ex_opus.h asterisk-12.2.0-opus/codecs/ex_opus.h +*** asterisk-12.2.0/codecs/ex_opus.h 1970-01-01 01:00:00.000000000 +0100 +--- asterisk-12.2.0-opus/codecs/ex_opus.h 2014-05-10 19:06:46.000000000 +0200 +*************** +*** 0 **** +--- 1,35 ---- ++ /*! \file ++ * \brief 8-bit data ++ * ++ * Copyright (C) 2008, Digium, Inc. ++ * ++ * Distributed under the terms of the GNU General Public License ++ * ++ */ ++ ++ /* Opus, a 20ms sample */ ++ static uint8_t ex_opus[] = { ++ 0x4b, 0x41, 0x25, 0x0b, 0xe4, 0x55, 0xc6, 0x74, ++ 0xda, 0xbb, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, ++ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, ++ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, ++ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, ++ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, ++ }; ++ ++ static struct ast_frame *opus_sample(void) ++ { ++ static struct ast_frame f = { ++ .frametype = AST_FRAME_VOICE, ++ .datalen = sizeof(ex_opus), ++ .samples = 960, //ARRAY_LEN(ex_opus), ++ .mallocd = 0, ++ .offset = 0, ++ .src = __PRETTY_FUNCTION__, ++ .data.ptr = ex_opus, ++ }; ++ ++ ast_format_set(&f.subclass.format, AST_FORMAT_OPUS, 0); ++ ++ return &f; ++ } +diff -crBdN asterisk-12.2.0/configure.ac asterisk-12.2.0-opus/configure.ac +*** asterisk-12.2.0/configure.ac 2014-05-10 18:57:50.000000000 +0200 +--- asterisk-12.2.0-opus/configure.ac 2014-05-10 19:17:14.000000000 +0200 +*************** +*** 428,433 **** +--- 428,434 ---- + AST_EXT_LIB_SETUP([NEWT], [newt], [newt]) + AST_EXT_LIB_SETUP([OGG], [OGG], [ogg]) + AST_EXT_LIB_SETUP([OPENR2], [MFR2], [openr2]) ++ AST_EXT_LIB_SETUP([OPUS], [Opus], [opus]) + AST_EXT_LIB_SETUP([OSPTK], [OSP Toolkit], [osptk]) + AST_EXT_LIB_SETUP([OSS], [Open Sound System], [oss]) + AST_EXT_LIB_SETUP([PGSQL], [PostgreSQL], [postgres]) +*************** +*** 2183,2188 **** +--- 2184,2191 ---- + + AC_SUBST(PBX_SPEEX_PREPROCESS) + ++ AST_EXT_LIB_CHECK([OPUS], [opus], [opus_encoder_create], [opus/opus.h]) ++ + AST_EXT_LIB_CHECK([SQLITE], [sqlite], [sqlite_exec], [sqlite.h]) + + AST_EXT_LIB_CHECK([SQLITE3], [sqlite3], [sqlite3_open], [sqlite3.h], [${PTHREAD_LIBS}], [${PTHREAD_CFLAGS}]) +diff -crBdN asterisk-12.2.0/main/frame.c asterisk-12.2.0-opus/main/frame.c +*** asterisk-12.2.0/main/frame.c 2014-05-10 18:57:35.000000000 +0200 +--- asterisk-12.2.0-opus/main/frame.c 2014-05-10 19:10:24.000000000 +0200 +*************** +*** 1029,1034 **** +--- 1029,1068 ---- + return cnt; + } + ++ /* Opus: copied from opus_decoder.c */ ++ static int opus_samples(unsigned char *data, int len) { ++ /* Do opus_packet_get_nb_frames first */ ++ int count, frames; ++ if (len<1) { ++ return 0; /* FIXME OPUS_BAD_ARG */ ++ } else { ++ count = data[0]&0x3; ++ if (count==0) ++ frames = 1; ++ else if (count!=3) ++ frames = 2; ++ else if (len<2) ++ return 0; /* FIXME OPUS_INVALID_PACKET */ ++ else ++ frames = data[1]&0x3F; ++ } ++ /* The, do a opus_packet_get_samples_per_frame */ ++ int audiosize, Fs = 48000; ++ if (data[0]&0x80) { ++ audiosize = ((data[0]>>3)&0x3); ++ audiosize = (Fs<>3)&0x3); ++ if (audiosize == 3) ++ audiosize = Fs*60/1000; ++ else ++ audiosize = (Fs<subclass.format) / 50; + break; + case AST_FORMAT_OPUS: +! /* TODO This assumes 20ms delivery right now, which is incorrect */ +! samples = 960; + break; + default: + ast_log(LOG_WARNING, "Unable to calculate samples for format %s\n", ast_getformatname(&f->subclass.format)); +--- 1145,1151 ---- + samples = ast_format_rate(&f->subclass.format) / 50; + break; + case AST_FORMAT_OPUS: +! samples = opus_samples(f->data.ptr, f->datalen); + break; + default: + ast_log(LOG_WARNING, "Unable to calculate samples for format %s\n", ast_getformatname(&f->subclass.format)); +diff -crBdN asterisk-12.2.0/main/frame.c.orig asterisk-12.2.0-opus/main/frame.c.orig +*** asterisk-12.2.0/main/frame.c.orig 1970-01-01 01:00:00.000000000 +0100 +--- asterisk-12.2.0-opus/main/frame.c.orig 2014-05-10 19:10:07.000000000 +0200 +*************** +*** 0 **** +--- 1,1231 ---- ++ /* ++ * Asterisk -- An open source telephony toolkit. ++ * ++ * Copyright (C) 1999 - 2005, Digium, Inc. ++ * ++ * Mark Spencer ++ * ++ * See http://www.asterisk.org for more information about ++ * the Asterisk project. Please do not directly contact ++ * any of the maintainers of this project for assistance; ++ * the project provides a web site, mailing lists and IRC ++ * channels for your use. ++ * ++ * This program is free software, distributed under the terms of ++ * the GNU General Public License Version 2. See the LICENSE file ++ * at the top of the source tree. ++ */ ++ ++ /*! \file ++ * ++ * \brief Frame and codec manipulation routines ++ * ++ * \author Mark Spencer ++ */ ++ ++ /*** MODULEINFO ++ core ++ ***/ ++ ++ #include "asterisk.h" ++ ++ ASTERISK_FILE_VERSION(__FILE__, "$Revision: 413589 $") ++ ++ #include "asterisk/_private.h" ++ #include "asterisk/lock.h" ++ #include "asterisk/frame.h" ++ #include "asterisk/channel.h" ++ #include "asterisk/cli.h" ++ #include "asterisk/term.h" ++ #include "asterisk/utils.h" ++ #include "asterisk/threadstorage.h" ++ #include "asterisk/linkedlists.h" ++ #include "asterisk/translate.h" ++ #include "asterisk/dsp.h" ++ #include "asterisk/file.h" ++ ++ #if !defined(LOW_MEMORY) ++ static void frame_cache_cleanup(void *data); ++ ++ /*! \brief A per-thread cache of frame headers */ ++ AST_THREADSTORAGE_CUSTOM(frame_cache, NULL, frame_cache_cleanup); ++ ++ /*! ++ * \brief Maximum ast_frame cache size ++ * ++ * In most cases where the frame header cache will be useful, the size ++ * of the cache will stay very small. However, it is not always the case that ++ * the same thread that allocates the frame will be the one freeing them, so ++ * sometimes a thread will never have any frames in its cache, or the cache ++ * will never be pulled from. For the latter case, we limit the maximum size. ++ */ ++ #define FRAME_CACHE_MAX_SIZE 10 ++ ++ /*! \brief This is just so ast_frames, a list head struct for holding a list of ++ * ast_frame structures, is defined. */ ++ AST_LIST_HEAD_NOLOCK(ast_frames, ast_frame); ++ ++ struct ast_frame_cache { ++ struct ast_frames list; ++ size_t size; ++ }; ++ #endif ++ ++ #define SMOOTHER_SIZE 8000 ++ ++ enum frame_type { ++ TYPE_HIGH, /* 0x0 */ ++ TYPE_LOW, /* 0x1 */ ++ TYPE_SILENCE, /* 0x2 */ ++ TYPE_DONTSEND /* 0x3 */ ++ }; ++ ++ #define TYPE_MASK 0x3 ++ ++ struct ast_smoother { ++ int size; ++ struct ast_format format; ++ int flags; ++ float samplesperbyte; ++ unsigned int opt_needs_swap:1; ++ struct ast_frame f; ++ struct timeval delivery; ++ char data[SMOOTHER_SIZE]; ++ char framedata[SMOOTHER_SIZE + AST_FRIENDLY_OFFSET]; ++ struct ast_frame *opt; ++ int len; ++ }; ++ ++ struct ast_frame ast_null_frame = { AST_FRAME_NULL, }; ++ ++ static int smoother_frame_feed(struct ast_smoother *s, struct ast_frame *f, int swap) ++ { ++ if (s->flags & AST_SMOOTHER_FLAG_G729) { ++ if (s->len % 10) { ++ ast_log(LOG_NOTICE, "Dropping extra frame of G.729 since we already have a VAD frame at the end\n"); ++ return 0; ++ } ++ } ++ if (swap) { ++ ast_swapcopy_samples(s->data + s->len, f->data.ptr, f->samples); ++ } else { ++ memcpy(s->data + s->len, f->data.ptr, f->datalen); ++ } ++ /* If either side is empty, reset the delivery time */ ++ if (!s->len || ast_tvzero(f->delivery) || ast_tvzero(s->delivery)) { /* XXX really ? */ ++ s->delivery = f->delivery; ++ } ++ s->len += f->datalen; ++ ++ return 0; ++ } ++ ++ void ast_smoother_reset(struct ast_smoother *s, int bytes) ++ { ++ memset(s, 0, sizeof(*s)); ++ s->size = bytes; ++ } ++ ++ void ast_smoother_reconfigure(struct ast_smoother *s, int bytes) ++ { ++ /* if there is no change, then nothing to do */ ++ if (s->size == bytes) { ++ return; ++ } ++ /* set the new desired output size */ ++ s->size = bytes; ++ /* if there is no 'optimized' frame in the smoother, ++ * then there is nothing left to do ++ */ ++ if (!s->opt) { ++ return; ++ } ++ /* there is an 'optimized' frame here at the old size, ++ * but it must now be put into the buffer so the data ++ * can be extracted at the new size ++ */ ++ smoother_frame_feed(s, s->opt, s->opt_needs_swap); ++ s->opt = NULL; ++ } ++ ++ struct ast_smoother *ast_smoother_new(int size) ++ { ++ struct ast_smoother *s; ++ if (size < 1) ++ return NULL; ++ if ((s = ast_malloc(sizeof(*s)))) ++ ast_smoother_reset(s, size); ++ return s; ++ } ++ ++ int ast_smoother_get_flags(struct ast_smoother *s) ++ { ++ return s->flags; ++ } ++ ++ void ast_smoother_set_flags(struct ast_smoother *s, int flags) ++ { ++ s->flags = flags; ++ } ++ ++ int ast_smoother_test_flag(struct ast_smoother *s, int flag) ++ { ++ return (s->flags & flag); ++ } ++ ++ int __ast_smoother_feed(struct ast_smoother *s, struct ast_frame *f, int swap) ++ { ++ if (f->frametype != AST_FRAME_VOICE) { ++ ast_log(LOG_WARNING, "Huh? Can't smooth a non-voice frame!\n"); ++ return -1; ++ } ++ if (!s->format.id) { ++ ast_format_copy(&s->format, &f->subclass.format); ++ s->samplesperbyte = (float)f->samples / (float)f->datalen; ++ } else if (ast_format_cmp(&s->format, &f->subclass.format) == AST_FORMAT_CMP_NOT_EQUAL) { ++ ast_log(LOG_WARNING, "Smoother was working on %s format frames, now trying to feed %s?\n", ++ ast_getformatname(&s->format), ast_getformatname(&f->subclass.format)); ++ return -1; ++ } ++ if (s->len + f->datalen > SMOOTHER_SIZE) { ++ ast_log(LOG_WARNING, "Out of smoother space\n"); ++ return -1; ++ } ++ if (((f->datalen == s->size) || ++ ((f->datalen < 10) && (s->flags & AST_SMOOTHER_FLAG_G729))) && ++ !s->opt && ++ !s->len && ++ (f->offset >= AST_MIN_OFFSET)) { ++ /* Optimize by sending the frame we just got ++ on the next read, thus eliminating the douple ++ copy */ ++ if (swap) ++ ast_swapcopy_samples(f->data.ptr, f->data.ptr, f->samples); ++ s->opt = f; ++ s->opt_needs_swap = swap ? 1 : 0; ++ return 0; ++ } ++ ++ return smoother_frame_feed(s, f, swap); ++ } ++ ++ struct ast_frame *ast_smoother_read(struct ast_smoother *s) ++ { ++ struct ast_frame *opt; ++ int len; ++ ++ /* IF we have an optimization frame, send it */ ++ if (s->opt) { ++ if (s->opt->offset < AST_FRIENDLY_OFFSET) ++ ast_log(LOG_WARNING, "Returning a frame of inappropriate offset (%d).\n", ++ s->opt->offset); ++ opt = s->opt; ++ s->opt = NULL; ++ return opt; ++ } ++ ++ /* Make sure we have enough data */ ++ if (s->len < s->size) { ++ /* Or, if this is a G.729 frame with VAD on it, send it immediately anyway */ ++ if (!((s->flags & AST_SMOOTHER_FLAG_G729) && (s->len % 10))) ++ return NULL; ++ } ++ len = s->size; ++ if (len > s->len) ++ len = s->len; ++ /* Make frame */ ++ s->f.frametype = AST_FRAME_VOICE; ++ ast_format_copy(&s->f.subclass.format, &s->format); ++ s->f.data.ptr = s->framedata + AST_FRIENDLY_OFFSET; ++ s->f.offset = AST_FRIENDLY_OFFSET; ++ s->f.datalen = len; ++ /* Samples will be improper given VAD, but with VAD the concept really doesn't even exist */ ++ s->f.samples = len * s->samplesperbyte; /* XXX rounding */ ++ s->f.delivery = s->delivery; ++ /* Fill Data */ ++ memcpy(s->f.data.ptr, s->data, len); ++ s->len -= len; ++ /* Move remaining data to the front if applicable */ ++ if (s->len) { ++ /* In principle this should all be fine because if we are sending ++ G.729 VAD, the next timestamp will take over anyawy */ ++ memmove(s->data, s->data + len, s->len); ++ if (!ast_tvzero(s->delivery)) { ++ /* If we have delivery time, increment it, otherwise, leave it at 0 */ ++ s->delivery = ast_tvadd(s->delivery, ast_samp2tv(s->f.samples, ast_format_rate(&s->format))); ++ } ++ } ++ /* Return frame */ ++ return &s->f; ++ } ++ ++ void ast_smoother_free(struct ast_smoother *s) ++ { ++ ast_free(s); ++ } ++ ++ static struct ast_frame *ast_frame_header_new(void) ++ { ++ struct ast_frame *f; ++ ++ #if !defined(LOW_MEMORY) ++ struct ast_frame_cache *frames; ++ ++ if ((frames = ast_threadstorage_get(&frame_cache, sizeof(*frames)))) { ++ if ((f = AST_LIST_REMOVE_HEAD(&frames->list, frame_list))) { ++ size_t mallocd_len = f->mallocd_hdr_len; ++ memset(f, 0, sizeof(*f)); ++ f->mallocd_hdr_len = mallocd_len; ++ f->mallocd = AST_MALLOCD_HDR; ++ frames->size--; ++ return f; ++ } ++ } ++ if (!(f = ast_calloc_cache(1, sizeof(*f)))) ++ return NULL; ++ #else ++ if (!(f = ast_calloc(1, sizeof(*f)))) ++ return NULL; ++ #endif ++ ++ f->mallocd_hdr_len = sizeof(*f); ++ ++ return f; ++ } ++ ++ #if !defined(LOW_MEMORY) ++ static void frame_cache_cleanup(void *data) ++ { ++ struct ast_frame_cache *frames = data; ++ struct ast_frame *f; ++ ++ while ((f = AST_LIST_REMOVE_HEAD(&frames->list, frame_list))) ++ ast_free(f); ++ ++ ast_free(frames); ++ } ++ #endif ++ ++ static void __frame_free(struct ast_frame *fr, int cache) ++ { ++ if (!fr->mallocd) ++ return; ++ ++ #if !defined(LOW_MEMORY) ++ if (cache && fr->mallocd == AST_MALLOCD_HDR) { ++ /* Cool, only the header is malloc'd, let's just cache those for now ++ * to keep things simple... */ ++ struct ast_frame_cache *frames; ++ ++ if ((frames = ast_threadstorage_get(&frame_cache, sizeof(*frames))) && ++ (frames->size < FRAME_CACHE_MAX_SIZE)) { ++ AST_LIST_INSERT_HEAD(&frames->list, fr, frame_list); ++ frames->size++; ++ return; ++ } ++ } ++ #endif ++ ++ if (fr->mallocd & AST_MALLOCD_DATA) { ++ if (fr->data.ptr) ++ ast_free(fr->data.ptr - fr->offset); ++ } ++ if (fr->mallocd & AST_MALLOCD_SRC) { ++ if (fr->src) ++ ast_free((void *) fr->src); ++ } ++ if (fr->mallocd & AST_MALLOCD_HDR) { ++ ast_free(fr); ++ } ++ } ++ ++ ++ void ast_frame_free(struct ast_frame *frame, int cache) ++ { ++ struct ast_frame *next; ++ ++ for (next = AST_LIST_NEXT(frame, frame_list); ++ frame; ++ frame = next, next = frame ? AST_LIST_NEXT(frame, frame_list) : NULL) { ++ __frame_free(frame, cache); ++ } ++ } ++ ++ void ast_frame_dtor(struct ast_frame *f) ++ { ++ if (f) { ++ ast_frfree(f); ++ } ++ } ++ ++ /*! ++ * \brief 'isolates' a frame by duplicating non-malloc'ed components ++ * (header, src, data). ++ * On return all components are malloc'ed ++ */ ++ struct ast_frame *ast_frisolate(struct ast_frame *fr) ++ { ++ struct ast_frame *out; ++ void *newdata; ++ ++ /* if none of the existing frame is malloc'd, let ast_frdup() do it ++ since it is more efficient ++ */ ++ if (fr->mallocd == 0) { ++ return ast_frdup(fr); ++ } ++ ++ /* if everything is already malloc'd, we are done */ ++ if ((fr->mallocd & (AST_MALLOCD_HDR | AST_MALLOCD_SRC | AST_MALLOCD_DATA)) == ++ (AST_MALLOCD_HDR | AST_MALLOCD_SRC | AST_MALLOCD_DATA)) { ++ return fr; ++ } ++ ++ if (!(fr->mallocd & AST_MALLOCD_HDR)) { ++ /* Allocate a new header if needed */ ++ if (!(out = ast_frame_header_new())) { ++ return NULL; ++ } ++ out->frametype = fr->frametype; ++ ast_format_copy(&out->subclass.format, &fr->subclass.format); ++ out->datalen = fr->datalen; ++ out->samples = fr->samples; ++ out->offset = fr->offset; ++ /* Copy the timing data */ ++ ast_copy_flags(out, fr, AST_FLAGS_ALL); ++ if (ast_test_flag(fr, AST_FRFLAG_HAS_TIMING_INFO)) { ++ out->ts = fr->ts; ++ out->len = fr->len; ++ out->seqno = fr->seqno; ++ } ++ } else { ++ out = fr; ++ } ++ ++ if (!(fr->mallocd & AST_MALLOCD_SRC) && fr->src) { ++ if (!(out->src = ast_strdup(fr->src))) { ++ if (out != fr) { ++ ast_free(out); ++ } ++ return NULL; ++ } ++ } else { ++ out->src = fr->src; ++ fr->src = NULL; ++ fr->mallocd &= ~AST_MALLOCD_SRC; ++ } ++ ++ if (!(fr->mallocd & AST_MALLOCD_DATA)) { ++ if (!fr->datalen) { ++ out->data.uint32 = fr->data.uint32; ++ out->mallocd = AST_MALLOCD_HDR | AST_MALLOCD_SRC; ++ return out; ++ } ++ if (!(newdata = ast_malloc(fr->datalen + AST_FRIENDLY_OFFSET))) { ++ if (out->src != fr->src) { ++ ast_free((void *) out->src); ++ } ++ if (out != fr) { ++ ast_free(out); ++ } ++ return NULL; ++ } ++ newdata += AST_FRIENDLY_OFFSET; ++ out->offset = AST_FRIENDLY_OFFSET; ++ out->datalen = fr->datalen; ++ memcpy(newdata, fr->data.ptr, fr->datalen); ++ out->data.ptr = newdata; ++ } else { ++ out->data = fr->data; ++ memset(&fr->data, 0, sizeof(fr->data)); ++ fr->mallocd &= ~AST_MALLOCD_DATA; ++ } ++ ++ out->mallocd = AST_MALLOCD_HDR | AST_MALLOCD_SRC | AST_MALLOCD_DATA; ++ ++ return out; ++ } ++ ++ struct ast_frame *ast_frdup(const struct ast_frame *f) ++ { ++ struct ast_frame *out = NULL; ++ int len, srclen = 0; ++ void *buf = NULL; ++ ++ #if !defined(LOW_MEMORY) ++ struct ast_frame_cache *frames; ++ #endif ++ ++ /* Start with standard stuff */ ++ len = sizeof(*out) + AST_FRIENDLY_OFFSET + f->datalen; ++ /* If we have a source, add space for it */ ++ /* ++ * XXX Watch out here - if we receive a src which is not terminated ++ * properly, we can be easily attacked. Should limit the size we deal with. ++ */ ++ if (f->src) ++ srclen = strlen(f->src); ++ if (srclen > 0) ++ len += srclen + 1; ++ ++ #if !defined(LOW_MEMORY) ++ if ((frames = ast_threadstorage_get(&frame_cache, sizeof(*frames)))) { ++ AST_LIST_TRAVERSE_SAFE_BEGIN(&frames->list, out, frame_list) { ++ if (out->mallocd_hdr_len >= len) { ++ size_t mallocd_len = out->mallocd_hdr_len; ++ ++ AST_LIST_REMOVE_CURRENT(frame_list); ++ memset(out, 0, sizeof(*out)); ++ out->mallocd_hdr_len = mallocd_len; ++ buf = out; ++ frames->size--; ++ break; ++ } ++ } ++ AST_LIST_TRAVERSE_SAFE_END; ++ } ++ #endif ++ ++ if (!buf) { ++ if (!(buf = ast_calloc_cache(1, len))) ++ return NULL; ++ out = buf; ++ out->mallocd_hdr_len = len; ++ } ++ ++ out->frametype = f->frametype; ++ ast_format_copy(&out->subclass.format, &f->subclass.format); ++ out->datalen = f->datalen; ++ out->samples = f->samples; ++ out->delivery = f->delivery; ++ /* Even though this new frame was allocated from the heap, we can't mark it ++ * with AST_MALLOCD_HDR, AST_MALLOCD_DATA and AST_MALLOCD_SRC, because that ++ * would cause ast_frfree() to attempt to individually free each of those ++ * under the assumption that they were separately allocated. Since this frame ++ * was allocated in a single allocation, we'll only mark it as if the header ++ * was heap-allocated; this will result in the entire frame being properly freed. ++ */ ++ out->mallocd = AST_MALLOCD_HDR; ++ out->offset = AST_FRIENDLY_OFFSET; ++ if (out->datalen) { ++ out->data.ptr = buf + sizeof(*out) + AST_FRIENDLY_OFFSET; ++ memcpy(out->data.ptr, f->data.ptr, out->datalen); ++ } else { ++ out->data.uint32 = f->data.uint32; ++ } ++ if (srclen > 0) { ++ /* This may seem a little strange, but it's to avoid a gcc (4.2.4) compiler warning */ ++ char *src; ++ out->src = buf + sizeof(*out) + AST_FRIENDLY_OFFSET + f->datalen; ++ src = (char *) out->src; ++ /* Must have space since we allocated for it */ ++ strcpy(src, f->src); ++ } ++ ast_copy_flags(out, f, AST_FLAGS_ALL); ++ out->ts = f->ts; ++ out->len = f->len; ++ out->seqno = f->seqno; ++ return out; ++ } ++ ++ void ast_swapcopy_samples(void *dst, const void *src, int samples) ++ { ++ int i; ++ unsigned short *dst_s = dst; ++ const unsigned short *src_s = src; ++ ++ for (i = 0; i < samples; i++) ++ dst_s[i] = (src_s[i]<<8) | (src_s[i]>>8); ++ } ++ ++ void ast_frame_subclass2str(struct ast_frame *f, char *subclass, size_t slen, char *moreinfo, size_t mlen) ++ { ++ switch(f->frametype) { ++ case AST_FRAME_DTMF_BEGIN: ++ if (slen > 1) { ++ subclass[0] = f->subclass.integer; ++ subclass[1] = '\0'; ++ } ++ break; ++ case AST_FRAME_DTMF_END: ++ if (slen > 1) { ++ subclass[0] = f->subclass.integer; ++ subclass[1] = '\0'; ++ } ++ break; ++ case AST_FRAME_CONTROL: ++ switch (f->subclass.integer) { ++ case AST_CONTROL_HANGUP: ++ ast_copy_string(subclass, "Hangup", slen); ++ break; ++ case AST_CONTROL_RING: ++ ast_copy_string(subclass, "Ring", slen); ++ break; ++ case AST_CONTROL_RINGING: ++ ast_copy_string(subclass, "Ringing", slen); ++ break; ++ case AST_CONTROL_ANSWER: ++ ast_copy_string(subclass, "Answer", slen); ++ break; ++ case AST_CONTROL_BUSY: ++ ast_copy_string(subclass, "Busy", slen); ++ break; ++ case AST_CONTROL_TAKEOFFHOOK: ++ ast_copy_string(subclass, "Take Off Hook", slen); ++ break; ++ case AST_CONTROL_OFFHOOK: ++ ast_copy_string(subclass, "Line Off Hook", slen); ++ break; ++ case AST_CONTROL_CONGESTION: ++ ast_copy_string(subclass, "Congestion", slen); ++ break; ++ case AST_CONTROL_FLASH: ++ ast_copy_string(subclass, "Flash", slen); ++ break; ++ case AST_CONTROL_WINK: ++ ast_copy_string(subclass, "Wink", slen); ++ break; ++ case AST_CONTROL_OPTION: ++ ast_copy_string(subclass, "Option", slen); ++ break; ++ case AST_CONTROL_RADIO_KEY: ++ ast_copy_string(subclass, "Key Radio", slen); ++ break; ++ case AST_CONTROL_RADIO_UNKEY: ++ ast_copy_string(subclass, "Unkey Radio", slen); ++ break; ++ case AST_CONTROL_HOLD: ++ ast_copy_string(subclass, "Hold", slen); ++ break; ++ case AST_CONTROL_UNHOLD: ++ ast_copy_string(subclass, "Unhold", slen); ++ break; ++ case AST_CONTROL_T38_PARAMETERS: { ++ char *message = "Unknown"; ++ if (f->datalen != sizeof(struct ast_control_t38_parameters)) { ++ message = "Invalid"; ++ } else { ++ struct ast_control_t38_parameters *parameters = f->data.ptr; ++ enum ast_control_t38 state = parameters->request_response; ++ if (state == AST_T38_REQUEST_NEGOTIATE) ++ message = "Negotiation Requested"; ++ else if (state == AST_T38_REQUEST_TERMINATE) ++ message = "Negotiation Request Terminated"; ++ else if (state == AST_T38_NEGOTIATED) ++ message = "Negotiated"; ++ else if (state == AST_T38_TERMINATED) ++ message = "Terminated"; ++ else if (state == AST_T38_REFUSED) ++ message = "Refused"; ++ } ++ snprintf(subclass, slen, "T38_Parameters/%s", message); ++ break; ++ } ++ case -1: ++ ast_copy_string(subclass, "Stop generators", slen); ++ break; ++ default: ++ snprintf(subclass, slen, "Unknown control '%d'", f->subclass.integer); ++ } ++ break; ++ case AST_FRAME_NULL: ++ ast_copy_string(subclass, "N/A", slen); ++ break; ++ case AST_FRAME_IAX: ++ /* Should never happen */ ++ snprintf(subclass, slen, "IAX Frametype %d", f->subclass.integer); ++ break; ++ case AST_FRAME_BRIDGE_ACTION: ++ /* Should never happen */ ++ snprintf(subclass, slen, "Bridge Frametype %d", f->subclass.integer); ++ break; ++ case AST_FRAME_BRIDGE_ACTION_SYNC: ++ /* Should never happen */ ++ snprintf(subclass, slen, "Synchronous Bridge Frametype %d", f->subclass.integer); ++ break; ++ case AST_FRAME_TEXT: ++ ast_copy_string(subclass, "N/A", slen); ++ if (moreinfo) { ++ ast_copy_string(moreinfo, f->data.ptr, mlen); ++ } ++ break; ++ case AST_FRAME_IMAGE: ++ snprintf(subclass, slen, "Image format %s\n", ast_getformatname(&f->subclass.format)); ++ break; ++ case AST_FRAME_HTML: ++ switch (f->subclass.integer) { ++ case AST_HTML_URL: ++ ast_copy_string(subclass, "URL", slen); ++ if (moreinfo) { ++ ast_copy_string(moreinfo, f->data.ptr, mlen); ++ } ++ break; ++ case AST_HTML_DATA: ++ ast_copy_string(subclass, "Data", slen); ++ break; ++ case AST_HTML_BEGIN: ++ ast_copy_string(subclass, "Begin", slen); ++ break; ++ case AST_HTML_END: ++ ast_copy_string(subclass, "End", slen); ++ break; ++ case AST_HTML_LDCOMPLETE: ++ ast_copy_string(subclass, "Load Complete", slen); ++ break; ++ case AST_HTML_NOSUPPORT: ++ ast_copy_string(subclass, "No Support", slen); ++ break; ++ case AST_HTML_LINKURL: ++ ast_copy_string(subclass, "Link URL", slen); ++ if (moreinfo) { ++ ast_copy_string(moreinfo, f->data.ptr, mlen); ++ } ++ break; ++ case AST_HTML_UNLINK: ++ ast_copy_string(subclass, "Unlink", slen); ++ break; ++ case AST_HTML_LINKREJECT: ++ ast_copy_string(subclass, "Link Reject", slen); ++ break; ++ default: ++ snprintf(subclass, slen, "Unknown HTML frame '%d'\n", f->subclass.integer); ++ break; ++ } ++ break; ++ case AST_FRAME_MODEM: ++ switch (f->subclass.integer) { ++ case AST_MODEM_T38: ++ ast_copy_string(subclass, "T.38", slen); ++ break; ++ case AST_MODEM_V150: ++ ast_copy_string(subclass, "V.150", slen); ++ break; ++ default: ++ snprintf(subclass, slen, "Unknown MODEM frame '%d'\n", f->subclass.integer); ++ break; ++ } ++ break; ++ default: ++ ast_copy_string(subclass, "Unknown Subclass", slen); ++ break; ++ } ++ } ++ ++ void ast_frame_type2str(enum ast_frame_type frame_type, char *ftype, size_t len) ++ { ++ switch (frame_type) { ++ case AST_FRAME_DTMF_BEGIN: ++ ast_copy_string(ftype, "DTMF Begin", len); ++ break; ++ case AST_FRAME_DTMF_END: ++ ast_copy_string(ftype, "DTMF End", len); ++ break; ++ case AST_FRAME_CONTROL: ++ ast_copy_string(ftype, "Control", len); ++ break; ++ case AST_FRAME_NULL: ++ ast_copy_string(ftype, "Null Frame", len); ++ break; ++ case AST_FRAME_IAX: ++ /* Should never happen */ ++ ast_copy_string(ftype, "IAX Specific", len); ++ break; ++ case AST_FRAME_BRIDGE_ACTION: ++ /* Should never happen */ ++ ast_copy_string(ftype, "Bridge Specific", len); ++ break; ++ case AST_FRAME_BRIDGE_ACTION_SYNC: ++ /* Should never happen */ ++ ast_copy_string(ftype, "Bridge Specific", len); ++ break; ++ case AST_FRAME_TEXT: ++ ast_copy_string(ftype, "Text", len); ++ break; ++ case AST_FRAME_IMAGE: ++ ast_copy_string(ftype, "Image", len); ++ break; ++ case AST_FRAME_HTML: ++ ast_copy_string(ftype, "HTML", len); ++ break; ++ case AST_FRAME_MODEM: ++ ast_copy_string(ftype, "Modem", len); ++ break; ++ case AST_FRAME_VOICE: ++ ast_copy_string(ftype, "Voice", len); ++ break; ++ case AST_FRAME_VIDEO: ++ ast_copy_string(ftype, "Video", len); ++ break; ++ default: ++ snprintf(ftype, len, "Unknown Frametype '%u'", frame_type); ++ break; ++ } ++ } ++ ++ /*! Dump a frame for debugging purposes */ ++ void ast_frame_dump(const char *name, struct ast_frame *f, char *prefix) ++ { ++ const char noname[] = "unknown"; ++ char ftype[40] = "Unknown Frametype"; ++ char cft[80]; ++ char subclass[40] = "Unknown Subclass"; ++ char csub[80]; ++ char moreinfo[40] = ""; ++ char cn[60]; ++ char cp[40]; ++ char cmn[40]; ++ ++ if (!name) { ++ name = noname; ++ } ++ ++ if (!f) { ++ ast_verb(-1, "%s [ %s (NULL) ] [%s]\n", ++ term_color(cp, prefix, COLOR_BRMAGENTA, COLOR_BLACK, sizeof(cp)), ++ term_color(cft, "HANGUP", COLOR_BRRED, COLOR_BLACK, sizeof(cft)), ++ term_color(cn, name, COLOR_YELLOW, COLOR_BLACK, sizeof(cn))); ++ return; ++ } ++ /* XXX We should probably print one each of voice and video when the format changes XXX */ ++ if (f->frametype == AST_FRAME_VOICE) { ++ return; ++ } ++ if (f->frametype == AST_FRAME_VIDEO) { ++ return; ++ } ++ ++ ast_frame_type2str(f->frametype, ftype, sizeof(ftype)); ++ ast_frame_subclass2str(f, subclass, sizeof(subclass), moreinfo, sizeof(moreinfo)); ++ ++ if (!ast_strlen_zero(moreinfo)) ++ ast_verb(-1, "%s [ TYPE: %s (%u) SUBCLASS: %s (%d) '%s' ] [%s]\n", ++ term_color(cp, prefix, COLOR_BRMAGENTA, COLOR_BLACK, sizeof(cp)), ++ term_color(cft, ftype, COLOR_BRRED, COLOR_BLACK, sizeof(cft)), ++ f->frametype, ++ term_color(csub, subclass, COLOR_BRCYAN, COLOR_BLACK, sizeof(csub)), ++ f->subclass.integer, ++ term_color(cmn, moreinfo, COLOR_BRGREEN, COLOR_BLACK, sizeof(cmn)), ++ term_color(cn, name, COLOR_YELLOW, COLOR_BLACK, sizeof(cn))); ++ else ++ ast_verb(-1, "%s [ TYPE: %s (%u) SUBCLASS: %s (%d) ] [%s]\n", ++ term_color(cp, prefix, COLOR_BRMAGENTA, COLOR_BLACK, sizeof(cp)), ++ term_color(cft, ftype, COLOR_BRRED, COLOR_BLACK, sizeof(cft)), ++ f->frametype, ++ term_color(csub, subclass, COLOR_BRCYAN, COLOR_BLACK, sizeof(csub)), ++ f->subclass.integer, ++ term_color(cn, name, COLOR_YELLOW, COLOR_BLACK, sizeof(cn))); ++ } ++ ++ int ast_parse_allow_disallow(struct ast_codec_pref *pref, struct ast_format_cap *cap, const char *list, int allowing) ++ { ++ int errors = 0, framems = 0, all = 0, iter_allowing; ++ char *parse = NULL, *this = NULL, *psize = NULL; ++ struct ast_format format; ++ ++ parse = ast_strdupa(list); ++ while ((this = strsep(&parse, ","))) { ++ iter_allowing = allowing; ++ framems = 0; ++ if (*this == '!') { ++ this++; ++ iter_allowing = !allowing; ++ } ++ if ((psize = strrchr(this, ':'))) { ++ *psize++ = '\0'; ++ ast_debug(1, "Packetization for codec: %s is %s\n", this, psize); ++ framems = atoi(psize); ++ if (framems < 0) { ++ framems = 0; ++ errors++; ++ ast_log(LOG_WARNING, "Bad packetization value for codec %s\n", this); ++ } ++ } ++ all = strcasecmp(this, "all") ? 0 : 1; ++ ++ if (!all && !ast_getformatbyname(this, &format)) { ++ ast_log(LOG_WARNING, "Cannot %s unknown format '%s'\n", iter_allowing ? "allow" : "disallow", this); ++ errors++; ++ continue; ++ } ++ ++ if (cap) { ++ if (iter_allowing) { ++ if (all) { ++ ast_format_cap_add_all(cap); ++ } else { ++ ast_format_cap_add(cap, &format); ++ } ++ } else { ++ if (all) { ++ ast_format_cap_remove_all(cap); ++ } else { ++ ast_format_cap_remove(cap, &format); ++ } ++ } ++ } ++ ++ if (pref) { ++ if (!all) { ++ if (iter_allowing) { ++ ast_codec_pref_append(pref, &format); ++ ast_codec_pref_setsize(pref, &format, framems); ++ } else { ++ ast_codec_pref_remove(pref, &format); ++ } ++ } else if (!iter_allowing) { ++ memset(pref, 0, sizeof(*pref)); ++ } else { ++ ast_codec_pref_append_all(pref); ++ } ++ } ++ } ++ return errors; ++ } ++ ++ static int g723_len(unsigned char buf) ++ { ++ enum frame_type type = buf & TYPE_MASK; ++ ++ switch(type) { ++ case TYPE_DONTSEND: ++ return 0; ++ break; ++ case TYPE_SILENCE: ++ return 4; ++ break; ++ case TYPE_HIGH: ++ return 24; ++ break; ++ case TYPE_LOW: ++ return 20; ++ break; ++ default: ++ ast_log(LOG_WARNING, "Badly encoded frame (%u)\n", type); ++ } ++ return -1; ++ } ++ ++ static int g723_samples(unsigned char *buf, int maxlen) ++ { ++ int pos = 0; ++ int samples = 0; ++ int res; ++ while(pos < maxlen) { ++ res = g723_len(buf[pos]); ++ if (res <= 0) ++ break; ++ samples += 240; ++ pos += res; ++ } ++ return samples; ++ } ++ ++ static unsigned char get_n_bits_at(unsigned char *data, int n, int bit) ++ { ++ int byte = bit / 8; /* byte containing first bit */ ++ int rem = 8 - (bit % 8); /* remaining bits in first byte */ ++ unsigned char ret = 0; ++ ++ if (n <= 0 || n > 8) ++ return 0; ++ ++ if (rem < n) { ++ ret = (data[byte] << (n - rem)); ++ ret |= (data[byte + 1] >> (8 - n + rem)); ++ } else { ++ ret = (data[byte] >> (rem - n)); ++ } ++ ++ return (ret & (0xff >> (8 - n))); ++ } ++ ++ static int speex_get_wb_sz_at(unsigned char *data, int len, int bit) ++ { ++ static const int SpeexWBSubModeSz[] = { ++ 4, 36, 112, 192, ++ 352, 0, 0, 0 }; ++ int off = bit; ++ unsigned char c; ++ ++ /* skip up to two wideband frames */ ++ if (((len * 8 - off) >= 5) && ++ get_n_bits_at(data, 1, off)) { ++ c = get_n_bits_at(data, 3, off + 1); ++ off += SpeexWBSubModeSz[c]; ++ ++ if (((len * 8 - off) >= 5) && ++ get_n_bits_at(data, 1, off)) { ++ c = get_n_bits_at(data, 3, off + 1); ++ off += SpeexWBSubModeSz[c]; ++ ++ if (((len * 8 - off) >= 5) && ++ get_n_bits_at(data, 1, off)) { ++ ast_log(LOG_WARNING, "Encountered corrupt speex frame; too many wideband frames in a row.\n"); ++ return -1; ++ } ++ } ++ ++ } ++ return off - bit; ++ } ++ ++ static int speex_samples(unsigned char *data, int len) ++ { ++ static const int SpeexSubModeSz[] = { ++ 5, 43, 119, 160, ++ 220, 300, 364, 492, ++ 79, 0, 0, 0, ++ 0, 0, 0, 0 }; ++ static const int SpeexInBandSz[] = { ++ 1, 1, 4, 4, ++ 4, 4, 4, 4, ++ 8, 8, 16, 16, ++ 32, 32, 64, 64 }; ++ int bit = 0; ++ int cnt = 0; ++ int off; ++ unsigned char c; ++ ++ while ((len * 8 - bit) >= 5) { ++ /* skip wideband frames */ ++ off = speex_get_wb_sz_at(data, len, bit); ++ if (off < 0) { ++ ast_log(LOG_WARNING, "Had error while reading wideband frames for speex samples\n"); ++ break; ++ } ++ bit += off; ++ ++ if ((len * 8 - bit) < 5) ++ break; ++ ++ /* get control bits */ ++ c = get_n_bits_at(data, 5, bit); ++ bit += 5; ++ ++ if (c == 15) { ++ /* terminator */ ++ break; ++ } else if (c == 14) { ++ /* in-band signal; next 4 bits contain signal id */ ++ c = get_n_bits_at(data, 4, bit); ++ bit += 4; ++ bit += SpeexInBandSz[c]; ++ } else if (c == 13) { ++ /* user in-band; next 4 bits contain msg len */ ++ c = get_n_bits_at(data, 4, bit); ++ bit += 4; ++ /* after which it's 5-bit signal id + c bytes of data */ ++ bit += 5 + c * 8; ++ } else if (c > 8) { ++ /* unknown */ ++ ast_log(LOG_WARNING, "Unknown speex control frame %d\n", c); ++ break; ++ } else { ++ /* skip number bits for submode (less the 5 control bits) */ ++ bit += SpeexSubModeSz[c] - 5; ++ cnt += 160; /* new frame */ ++ } ++ } ++ return cnt; ++ } ++ ++ int ast_codec_get_samples(struct ast_frame *f) ++ { ++ int samples = 0; ++ ++ switch (f->subclass.format.id) { ++ case AST_FORMAT_SPEEX: ++ samples = speex_samples(f->data.ptr, f->datalen); ++ break; ++ case AST_FORMAT_SPEEX16: ++ samples = 2 * speex_samples(f->data.ptr, f->datalen); ++ break; ++ case AST_FORMAT_SPEEX32: ++ samples = 4 * speex_samples(f->data.ptr, f->datalen); ++ break; ++ case AST_FORMAT_G723_1: ++ samples = g723_samples(f->data.ptr, f->datalen); ++ break; ++ case AST_FORMAT_ILBC: ++ samples = 240 * (f->datalen / 50); ++ break; ++ case AST_FORMAT_GSM: ++ samples = 160 * (f->datalen / 33); ++ break; ++ case AST_FORMAT_G729A: ++ samples = f->datalen * 8; ++ break; ++ case AST_FORMAT_SLINEAR: ++ case AST_FORMAT_SLINEAR16: ++ samples = f->datalen / 2; ++ break; ++ case AST_FORMAT_LPC10: ++ /* assumes that the RTP packet contains one LPC10 frame */ ++ samples = 22 * 8; ++ samples += (((char *)(f->data.ptr))[7] & 0x1) * 8; ++ break; ++ case AST_FORMAT_ULAW: ++ case AST_FORMAT_ALAW: ++ case AST_FORMAT_TESTLAW: ++ samples = f->datalen; ++ break; ++ case AST_FORMAT_G722: ++ case AST_FORMAT_ADPCM: ++ case AST_FORMAT_G726: ++ case AST_FORMAT_G726_AAL2: ++ samples = f->datalen * 2; ++ break; ++ case AST_FORMAT_SIREN7: ++ /* 16,000 samples per second at 32kbps is 4,000 bytes per second */ ++ samples = f->datalen * (16000 / 4000); ++ break; ++ case AST_FORMAT_SIREN14: ++ /* 32,000 samples per second at 48kbps is 6,000 bytes per second */ ++ samples = (int) f->datalen * ((float) 32000 / 6000); ++ break; ++ case AST_FORMAT_G719: ++ /* 48,000 samples per second at 64kbps is 8,000 bytes per second */ ++ samples = (int) f->datalen * ((float) 48000 / 8000); ++ break; ++ case AST_FORMAT_SILK: ++ if (!(ast_format_isset(&f->subclass.format, ++ SILK_ATTR_KEY_SAMP_RATE, ++ SILK_ATTR_VAL_SAMP_24KHZ, ++ AST_FORMAT_ATTR_END))) { ++ return 480; ++ } else if (!(ast_format_isset(&f->subclass.format, ++ SILK_ATTR_KEY_SAMP_RATE, ++ SILK_ATTR_VAL_SAMP_16KHZ, ++ AST_FORMAT_ATTR_END))) { ++ return 320; ++ } else if (!(ast_format_isset(&f->subclass.format, ++ SILK_ATTR_KEY_SAMP_RATE, ++ SILK_ATTR_VAL_SAMP_12KHZ, ++ AST_FORMAT_ATTR_END))) { ++ return 240; ++ } else { ++ return 160; ++ } ++ case AST_FORMAT_CELT: ++ /* TODO This assumes 20ms delivery right now, which is incorrect */ ++ samples = ast_format_rate(&f->subclass.format) / 50; ++ break; ++ case AST_FORMAT_OPUS: ++ samples = opus_samples(f->data.ptr, f->datalen); ++ break; ++ default: ++ ast_log(LOG_WARNING, "Unable to calculate samples for format %s\n", ast_getformatname(&f->subclass.format)); ++ } ++ return samples; ++ } ++ ++ int ast_codec_get_len(struct ast_format *format, int samples) ++ { ++ int len = 0; ++ ++ /* XXX Still need speex, and lpc10 XXX */ ++ switch(format->id) { ++ case AST_FORMAT_G723_1: ++ len = (samples / 240) * 20; ++ break; ++ case AST_FORMAT_ILBC: ++ len = (samples / 240) * 50; ++ break; ++ case AST_FORMAT_GSM: ++ len = (samples / 160) * 33; ++ break; ++ case AST_FORMAT_G729A: ++ len = samples / 8; ++ break; ++ case AST_FORMAT_SLINEAR: ++ case AST_FORMAT_SLINEAR16: ++ len = samples * 2; ++ break; ++ case AST_FORMAT_ULAW: ++ case AST_FORMAT_ALAW: ++ case AST_FORMAT_TESTLAW: ++ len = samples; ++ break; ++ case AST_FORMAT_G722: ++ case AST_FORMAT_ADPCM: ++ case AST_FORMAT_G726: ++ case AST_FORMAT_G726_AAL2: ++ len = samples / 2; ++ break; ++ case AST_FORMAT_SIREN7: ++ /* 16,000 samples per second at 32kbps is 4,000 bytes per second */ ++ len = samples / (16000 / 4000); ++ break; ++ case AST_FORMAT_SIREN14: ++ /* 32,000 samples per second at 48kbps is 6,000 bytes per second */ ++ len = (int) samples / ((float) 32000 / 6000); ++ break; ++ case AST_FORMAT_G719: ++ /* 48,000 samples per second at 64kbps is 8,000 bytes per second */ ++ len = (int) samples / ((float) 48000 / 8000); ++ break; ++ default: ++ ast_log(LOG_WARNING, "Unable to calculate sample length for format %s\n", ast_getformatname(format)); ++ } ++ ++ return len; ++ } ++ ++ int ast_frame_adjust_volume(struct ast_frame *f, int adjustment) ++ { ++ int count; ++ short *fdata = f->data.ptr; ++ short adjust_value = abs(adjustment); ++ ++ if ((f->frametype != AST_FRAME_VOICE) || !(ast_format_is_slinear(&f->subclass.format))) { ++ return -1; ++ } ++ ++ if (!adjustment) { ++ return 0; ++ } ++ ++ for (count = 0; count < f->samples; count++) { ++ if (adjustment > 0) { ++ ast_slinear_saturated_multiply(&fdata[count], &adjust_value); ++ } else if (adjustment < 0) { ++ ast_slinear_saturated_divide(&fdata[count], &adjust_value); ++ } ++ } ++ ++ return 0; ++ } ++ ++ int ast_frame_slinear_sum(struct ast_frame *f1, struct ast_frame *f2) ++ { ++ int count; ++ short *data1, *data2; ++ ++ if ((f1->frametype != AST_FRAME_VOICE) || (f1->subclass.format.id != AST_FORMAT_SLINEAR)) ++ return -1; ++ ++ if ((f2->frametype != AST_FRAME_VOICE) || (f2->subclass.format.id != AST_FORMAT_SLINEAR)) ++ return -1; ++ ++ if (f1->samples != f2->samples) ++ return -1; ++ ++ for (count = 0, data1 = f1->data.ptr, data2 = f2->data.ptr; ++ count < f1->samples; ++ count++, data1++, data2++) ++ ast_slinear_saturated_add(data1, data2); ++ ++ return 0; ++ } ++ ++ int ast_frame_clear(struct ast_frame *frame) ++ { ++ struct ast_frame *next; ++ ++ for (next = AST_LIST_NEXT(frame, frame_list); ++ frame; ++ frame = next, next = frame ? AST_LIST_NEXT(frame, frame_list) : NULL) { ++ memset(frame->data.ptr, 0, frame->datalen); ++ } ++ return 0; ++ } +diff -crBdN asterisk-12.2.0/makeopts.in asterisk-12.2.0-opus/makeopts.in +*** asterisk-12.2.0/makeopts.in 2014-05-10 18:57:50.000000000 +0200 +--- asterisk-12.2.0-opus/makeopts.in 2014-05-10 21:41:46.000000000 +0200 +*************** +*** 272,277 **** +--- 272,280 ---- + SPEEXDSP_INCLUDE=@SPEEXDSP_INCLUDE@ + SPEEXDSP_LIB=@SPEEXDSP_LIB@ + ++ OPUS_INCLUDE=@OPUS_INCLUDE@ ++ OPUS_LIB=@OPUS_LIB@ ++ + SQLITE_INCLUDE=@SQLITE_INCLUDE@ + SQLITE_LIB=@SQLITE_LIB@ + diff --git a/asterisk_opus+vp8.diff b/asterisk_opus+vp8.diff deleted file mode 100644 index fc29839..0000000 --- a/asterisk_opus+vp8.diff +++ /dev/null @@ -1,1199 +0,0 @@ -diff -urN asterisk-11.1.2/build_tools/menuselect-deps.in asterisk-11.1.2-opus/build_tools/menuselect-deps.in ---- asterisk-11.1.2/build_tools/menuselect-deps.in 2012-07-25 14:21:54.000000000 +0200 -+++ asterisk-11.1.2-opus/build_tools/menuselect-deps.in 2013-05-24 16:22:58.320556018 +0200 -@@ -40,6 +40,7 @@ - NEON29=@PBX_NEON29@ - OGG=@PBX_OGG@ - OPENH323=@PBX_OPENH323@ -+OPUS=@PBX_OPUS@ - OSPTK=@PBX_OSPTK@ - OSS=@PBX_OSS@ - PGSQL=@PBX_PGSQL@ -diff -urN asterisk-11.1.2/channels/chan_sip.c asterisk-11.1.2-opus/channels/chan_sip.c ---- asterisk-11.1.2/channels/chan_sip.c 2013-01-02 20:23:44.000000000 +0100 -+++ asterisk-11.1.2-opus/channels/chan_sip.c 2013-05-27 12:16:44.907975566 +0200 -@@ -7724,8 +7724,25 @@ - break; - case AST_CONTROL_VIDUPDATE: /* Request a video frame update */ - if (p->vrtp && !p->novideo) { -- transmit_info_with_vidupdate(p); -- /* ast_rtcp_send_h261fur(p->vrtp); */ -+ /* Only use this for WebRTC users */ -+ struct ast_format_cap *fcap = ast_channel_nativeformats(ast); -+ struct ast_format vp8; -+ ast_format_set(&vp8, AST_FORMAT_VP8, 0); -+ if(ast_format_cap_iscompatible(fcap, &vp8)) { -+ sip_pvt_lock(p); -+ if (p->vrtp) { -+ ast_log(LOG_WARNING, "chan_sip, sending RTCP FIR to WebRTC user\n"); -+ /* FIXME Fake RTP write, this will be sent as an RTCP packet */ -+ struct ast_frame fr; -+ fr.frametype = AST_FRAME_CONTROL; -+ fr.subclass.integer = AST_CONTROL_VIDUPDATE; -+ res = ast_rtp_instance_write(p->vrtp, &fr); -+ } -+ sip_pvt_unlock(p); -+ } else { -+ transmit_info_with_vidupdate(p); -+ /* ast_rtcp_send_h261fur(p->vrtp); */ -+ } - } else - res = -1; - break; -@@ -10982,7 +10999,7 @@ - struct ast_format *format; - - if ((format = ast_rtp_codecs_get_payload_format(newaudiortp, codec))) { -- unsigned int bit_rate; -+ unsigned int bit_rate, value; - - if (!ast_format_sdp_parse(format, fmtp_string)) { - found = TRUE; -@@ -11021,6 +11038,53 @@ - } - } - break; -+ /* Opus SDP fmtp parameters (draft-ietf-payload-rtp-opus-00) */ -+ case AST_FORMAT_OPUS: -+ if (sscanf(fmtp_string, "maxplaybackrate=%30u", &value) == 1) { -+ ast_log(LOG_WARNING, "Got Opus maxplaybackrate=%d\n", value); -+ /* TODO: actually handle this */ -+ found = TRUE; -+ } -+ if (sscanf(fmtp_string, "sprop-maxcapturerate=%30u", &value) == 1) { -+ ast_log(LOG_WARNING, "Got Opus sprop-maxcapturerate=%d\n", value); -+ /* TODO: actually handle this */ -+ found = TRUE; -+ } -+ if (sscanf(fmtp_string, "minptime=%30u", &value) == 1) { -+ ast_log(LOG_WARNING, "Got Opus minptime=%d\n", value); -+ /* TODO: actually handle this */ -+ found = TRUE; -+ } -+ if (sscanf(fmtp_string, "maxaveragebitrate=%30u", &value) == 1) { -+ ast_log(LOG_WARNING, "Got Opus maxaveragebitrate=%d\n", value); -+ /* TODO: actually handle this */ -+ found = TRUE; -+ } -+ if (sscanf(fmtp_string, "stereo=%30u", &value) == 1) { -+ ast_log(LOG_WARNING, "Got Opus stereo=%d\n", value); -+ /* TODO: actually handle this */ -+ found = TRUE; -+ } -+ if (sscanf(fmtp_string, "sprop-stereo=%30u", &value) == 1) { -+ ast_log(LOG_WARNING, "Got Opus sprop-stereo=%d\n", value); -+ /* TODO: actually handle this */ -+ found = TRUE; -+ } -+ if (sscanf(fmtp_string, "cbr=%30u", &value) == 1) { -+ ast_log(LOG_WARNING, "Got Opus cbr=%d\n", value); -+ /* TODO: actually handle this */ -+ found = TRUE; -+ } -+ if (sscanf(fmtp_string, "useinbandfec=%30u", &value) == 1) { -+ ast_log(LOG_WARNING, "Got Opus useinbandfec=%d\n", value); -+ /* TODO: actually handle this */ -+ found = TRUE; -+ } -+ if (sscanf(fmtp_string, "usedtx=%30u", &value) == 1) { -+ ast_log(LOG_WARNING, "Got Opus usedtx=%d\n", value); -+ /* TODO: actually handle this */ -+ found = TRUE; -+ } - } - } - } -@@ -11041,7 +11105,9 @@ - /* We have a rtpmap to handle */ - if (*last_rtpmap_codec < SDP_MAX_RTPMAP_CODECS) { - /* Note: should really look at the '#chans' params too */ -- if (!strncasecmp(mimeSubtype, "H26", 3) || !strncasecmp(mimeSubtype, "MP4", 3)) { -+ if (!strncasecmp(mimeSubtype, "H26", 3) || !strncasecmp(mimeSubtype, "MP4", 3) -+ /* VP8 */ -+ || !strncasecmp(mimeSubtype, "VP8", 3)) { - if (!(ast_rtp_codecs_payloads_set_rtpmap_type_rate(newvideortp, NULL, codec, "video", mimeSubtype, 0, sample_rate))) { - if (debug) - ast_verbose("Found video description format %s for ID %d\n", mimeSubtype, codec); -@@ -12583,7 +12649,11 @@ - } else /* I don't see how you couldn't have p->rtp, but good to check for and error out if not there like earlier code */ - return; - ast_str_append(m_buf, 0, " %d", rtp_code); -- ast_str_append(a_buf, 0, "a=rtpmap:%d %s/%d\r\n", rtp_code, mime, rate); -+ /* Opus mandates 2 channels in rtpmap */ -+ if((int) format->id == AST_FORMAT_OPUS) -+ ast_str_append(a_buf, 0, "a=rtpmap:%d %s/%d/2\r\n", rtp_code, mime, rate); -+ else -+ ast_str_append(a_buf, 0, "a=rtpmap:%d %s/%d\r\n", rtp_code, mime, rate); - - ast_format_sdp_generate(format, rtp_code, a_buf); - -@@ -12612,6 +12682,17 @@ - /* Indicate that we only expect 64Kbps */ - ast_str_append(a_buf, 0, "a=fmtp:%d bitrate=64000\r\n", rtp_code); - break; -+ /* Opus, pass parameters we care about (FIXME could this be 'fb' and not 'wb'?) */ -+ case AST_FORMAT_OPUS: -+ ast_str_append(a_buf, 0, "a=maxptime:%d\r\n", 60); /* FIXME */ -+ ast_str_append(a_buf, 0, "a=fmtp:%d maxplaybackrate=%d; stereo=%d; sprop-stereo=%d; useinbandfec=%d\r\n", -+ rtp_code, -+ 16000, /* maxplaybackrate */ -+ 0, /* stereo */ -+ 0, /* sprop-stereo */ -+ 0 /* useinbandfec FIXME */ -+ ); -+ break; - } - - if (fmt.cur_ms && (fmt.cur_ms < *min_packet_size)) -diff -urN asterisk-11.1.2/codecs/codec_opus.c asterisk-11.1.2-opus/codecs/codec_opus.c ---- asterisk-11.1.2/codecs/codec_opus.c 1970-01-01 01:00:00.000000000 +0100 -+++ asterisk-11.1.2-opus/codecs/codec_opus.c 2013-05-24 17:04:17.660896284 +0200 -@@ -0,0 +1,529 @@ -+/* -+ * Asterisk -- An open source telephony toolkit. -+ * -+ * Copyright (C) 1999 - 2005, Digium, Inc. -+ * -+ * Mark Spencer -+ * -+ * -+ * See http://www.asterisk.org for more information about -+ * the Asterisk project. Please do not directly contact -+ * any of the maintainers of this project for assistance; -+ * the project provides a web site, mailing lists and IRC -+ * channels for your use. -+ * -+ * This program is free software, distributed under the terms of -+ * the GNU General Public License Version 2. See the LICENSE file -+ * at the top of the source tree. -+ */ -+ -+/*! \file -+ * -+ * \brief Translate between signed linear and Opus (Open Codec) -+ * -+ * \author Lorenzo Miniero -+ * -+ * \note This work was motivated by Mozilla -+ * -+ * \ingroup codecs -+ * -+ * \extref The Opus library - http://opus-codec.org -+ * -+ */ -+ -+/*** MODULEINFO -+ opus -+ core -+ ***/ -+ -+#include "asterisk.h" -+ -+ASTERISK_FILE_VERSION(__FILE__, "$Revision: 328209 $") -+ -+#include -+ -+#include "asterisk/translate.h" -+#include "asterisk/module.h" -+#include "asterisk/config.h" -+#include "asterisk/utils.h" -+#include "asterisk/cli.h" -+ -+ -+#define BUFFER_SAMPLES 8000 -+#define OPUS_SAMPLES 160 -+ -+#define USE_FEC 0 -+ -+ -+/* Sample frame data */ -+#include "asterisk/slin.h" -+#include "ex_opus.h" -+ -+/* FIXME: Test */ -+#include "asterisk/file.h" -+ -+ -+static int encid = 0; -+static int decid = 0; -+ -+static int opusdebug = 0; -+ -+ -+/* Private structures */ -+struct opus_coder_pvt { -+ void *opus; /* May be encoder or decoder */ -+ int sampling_rate; -+ int multiplier; -+ int fec; -+ -+ int id; -+ -+ int16_t buf[BUFFER_SAMPLES]; /* FIXME */ -+ int framesize; -+ -+ FILE *file; -+}; -+ -+ -+/* Helper methods */ -+static int opus_encoder_construct(struct ast_trans_pvt *pvt, int sampling_rate) { -+ if(sampling_rate != 8000 && sampling_rate != 12000 && sampling_rate != 16000 && sampling_rate != 24000 && sampling_rate != 48000) -+ return -1; -+ struct opus_coder_pvt *opvt = pvt->pvt; -+ opvt->sampling_rate = sampling_rate; -+ opvt->multiplier = 48000/sampling_rate; -+ opvt->fec = USE_FEC; -+ int error = 0; -+ opvt->opus = opus_encoder_create(sampling_rate, 1, OPUS_APPLICATION_VOIP, &error); -+ if(error != OPUS_OK) { -+ if(opusdebug) -+ ast_verbose("[Opus] Ops! got an error creating the Opus encoder: %d (%s)\n", error, opus_strerror(error)); -+ return -1; -+ } -+ if(sampling_rate == 8000) -+ opus_encoder_ctl(opvt->opus, OPUS_SET_MAX_BANDWIDTH(OPUS_BANDWIDTH_NARROWBAND)); -+ else if(sampling_rate == 12000) -+ opus_encoder_ctl(opvt->opus, OPUS_SET_MAX_BANDWIDTH(OPUS_BANDWIDTH_MEDIUMBAND)); -+ else if(sampling_rate == 16000) -+ opus_encoder_ctl(opvt->opus, OPUS_SET_MAX_BANDWIDTH(OPUS_BANDWIDTH_WIDEBAND)); -+ else if(sampling_rate == 24000) -+ opus_encoder_ctl(opvt->opus, OPUS_SET_MAX_BANDWIDTH(OPUS_BANDWIDTH_SUPERWIDEBAND)); -+ else if(sampling_rate == 48000) -+ opus_encoder_ctl(opvt->opus, OPUS_SET_MAX_BANDWIDTH(OPUS_BANDWIDTH_FULLBAND)); -+ opus_encoder_ctl(opvt->opus, OPUS_SET_INBAND_FEC(opvt->fec)); -+ opvt->framesize = sampling_rate/50; -+ opvt->id = ++encid; -+ if(opusdebug) -+ ast_verbose("[Opus] Created encoder #%d (%d->opus)\n", opvt->id, sampling_rate); -+ -+ return 0; -+} -+ -+static int opus_decoder_construct(struct ast_trans_pvt *pvt, int sampling_rate) { -+ if(sampling_rate != 8000 && sampling_rate != 12000 && sampling_rate != 16000 && sampling_rate != 24000 && sampling_rate != 48000) -+ return -1; -+ struct opus_coder_pvt *opvt = pvt->pvt; -+ opvt->sampling_rate = sampling_rate; -+ opvt->multiplier = 48000/sampling_rate; -+ opvt->fec = USE_FEC; /* FIXME: should be triggered by chan_sip */ -+ int error = 0; -+ opvt->opus = opus_decoder_create(sampling_rate, 1, &error); -+ if(error != OPUS_OK) { -+ if(opusdebug) -+ ast_verbose("[Opus] Ops! got an error creating the Opus decoder: %d (%s)\n", error, opus_strerror(error)); -+ return -1; -+ } -+ opvt->id = ++decid; -+ if(opusdebug) -+ ast_verbose("[Opus] Created decoder #%d (opus->%d)\n", opvt->id, sampling_rate); -+ -+ if(opusdebug > 1) { -+ char filename[50]; -+ sprintf(filename, "/home/lminiero/opusdec-%04d-%d.raw", opvt->id, opvt->sampling_rate); -+ opvt->file = fopen(filename, "wb"); -+ } -+ -+ return 0; -+} -+ -+/* Translator callbacks */ -+static int lintoopus_new(struct ast_trans_pvt *pvt) { -+ return opus_encoder_construct(pvt, 8000); -+} -+ -+static int lin12toopus_new(struct ast_trans_pvt *pvt) { -+ return opus_encoder_construct(pvt, 12000); -+} -+ -+static int lin16toopus_new(struct ast_trans_pvt *pvt) { -+ return opus_encoder_construct(pvt, 16000); -+} -+ -+static int lin24toopus_new(struct ast_trans_pvt *pvt) { -+ return opus_encoder_construct(pvt, 24000); -+} -+ -+static int lin48toopus_new(struct ast_trans_pvt *pvt) { -+ return opus_encoder_construct(pvt, 48000); -+} -+ -+static int opustolin_new(struct ast_trans_pvt *pvt) { -+ return opus_decoder_construct(pvt, 8000); -+} -+ -+static int opustolin12_new(struct ast_trans_pvt *pvt) { -+ return opus_decoder_construct(pvt, 12000); -+} -+ -+static int opustolin16_new(struct ast_trans_pvt *pvt) { -+ return opus_decoder_construct(pvt, 16000); -+} -+ -+static int opustolin24_new(struct ast_trans_pvt *pvt) { -+ return opus_decoder_construct(pvt, 24000); -+} -+ -+static int opustolin48_new(struct ast_trans_pvt *pvt) { -+ return opus_decoder_construct(pvt, 48000); -+} -+ -+static int lintoopus_framein(struct ast_trans_pvt *pvt, struct ast_frame *f) { -+ struct opus_coder_pvt *opvt = pvt->pvt; -+ -+ /* XXX We should look at how old the rest of our stream is, and if it -+ is too old, then we should overwrite it entirely, otherwise we can -+ get artifacts of earlier talk that do not belong */ -+ memcpy(opvt->buf + pvt->samples, f->data.ptr, f->datalen); -+ pvt->samples += f->samples; -+ -+ return 0; -+} -+ -+static struct ast_frame *lintoopus_frameout(struct ast_trans_pvt *pvt) { -+ struct opus_coder_pvt *opvt = pvt->pvt; -+ -+ /* We can't work on anything less than a frame in size */ -+ if (pvt->samples < opvt->framesize) -+ return NULL; -+ -+ int datalen = 0; /* output bytes */ -+ int samples = 0; /* output samples */ -+ -+ /* Encode 160 samples (or more if it's not narrowband) */ -+ if(opusdebug > 1) -+ ast_verbose("[Opus] [Encoder #%d (%d)] %d samples, %d bytes\n", opvt->id, opvt->sampling_rate, opvt->framesize, opvt->framesize*2); -+ datalen = opus_encode(opvt->opus, opvt->buf, opvt->framesize, pvt->outbuf.uc, BUFFER_SAMPLES); -+ if(datalen < 0) { -+ if(opusdebug) -+ ast_verbose("[Opus] Ops! got an error encoding the Opus frame: %d (%s)\n", datalen, opus_strerror(datalen)); -+ return NULL; -+ } -+ samples += opvt->framesize; -+ pvt->samples -= opvt->framesize; -+ /* Move the data at the end of the buffer to the front */ -+ if (pvt->samples) -+ memmove(opvt->buf, opvt->buf + samples, pvt->samples * 2); -+ -+ if(opusdebug > 1) -+ ast_verbose("[Opus] [Encoder #%d (%d)] >> Got %d samples, %d bytes\n", opvt->id, opvt->sampling_rate, opvt->multiplier*samples, datalen); -+ -+ if(opvt->file) -+ fwrite(opvt->buf, sizeof(int16_t), opvt->multiplier*samples, opvt->file); -+ -+ return ast_trans_frameout(pvt, datalen, opvt->multiplier*samples); -+} -+ -+static int opustolin_framein(struct ast_trans_pvt *pvt, struct ast_frame *f) { -+ struct opus_coder_pvt *opvt = pvt->pvt; -+ /* Decode */ -+ if(opusdebug > 1) -+ ast_verbose("[Opus] [Decoder #%d (%d)] %d samples, %d bytes\n", opvt->id, opvt->sampling_rate, f->samples, f->datalen); -+ int error = opus_decode(opvt->opus, f->data.ptr, f->datalen, pvt->outbuf.i16, BUFFER_SAMPLES, opvt->fec); -+ if(error < 0) { -+ if(opusdebug) -+ ast_verbose("[Opus] Ops! got an error decoding the Opus frame: %d (%s)\n", error, opus_strerror(error)); -+ return -1; -+ } -+ pvt->samples += error; -+ pvt->datalen += error*2; -+ -+ if(opusdebug > 1) -+ ast_verbose("[Opus] [Decoder #%d (%d)] >> Got %d samples, %d bytes\n", opvt->id, opvt->sampling_rate, pvt->samples, pvt->datalen); -+ -+ if(opvt->file) -+ fwrite(pvt->outbuf.i16, sizeof(int16_t), pvt->samples, opvt->file); -+ -+ return 0; -+} -+ -+static void lintoopus_destroy(struct ast_trans_pvt *arg) { -+ struct opus_coder_pvt *opvt = arg->pvt; -+ if(opvt == NULL || opvt->opus == NULL) -+ return; -+ opus_encoder_destroy(opvt->opus); -+ if(opusdebug) -+ ast_verbose("[Opus] Destroyed encoder #%d (%d->opus)\n", opvt->id, opvt->sampling_rate); -+ opvt->opus = NULL; -+ -+ if(opvt->file) -+ fclose(opvt->file); -+ opvt->file = NULL; -+} -+ -+static void opustolin_destroy(struct ast_trans_pvt *arg) { -+ struct opus_coder_pvt *opvt = arg->pvt; -+ if(opvt == NULL || opvt->opus == NULL) -+ return; -+ opus_decoder_destroy(opvt->opus); -+ if(opusdebug) -+ ast_verbose("[Opus] Destroyed decoder #%d (opus->%d)\n", opvt->id, opvt->sampling_rate); -+ opvt->opus = NULL; -+ -+ if(opvt->file) -+ fclose(opvt->file); -+ opvt->file = NULL; -+} -+ -+ -+/* Translators */ -+static struct ast_translator lintoopus = { -+ .name = "lintoopus", -+ .newpvt = lintoopus_new, -+ .framein = lintoopus_framein, -+ .frameout = lintoopus_frameout, -+ .destroy = lintoopus_destroy, -+ .sample = slin8_sample, -+ .desc_size = sizeof(struct opus_coder_pvt), -+ .buffer_samples = BUFFER_SAMPLES, -+ .buf_size = BUFFER_SAMPLES * 2, -+}; -+ -+static struct ast_translator lin12toopus = { -+ .name = "lin12toopus", -+ .newpvt = lin12toopus_new, -+ .framein = lintoopus_framein, -+ .frameout = lintoopus_frameout, -+ .destroy = lintoopus_destroy, -+ .desc_size = sizeof(struct opus_coder_pvt), -+ .buffer_samples = BUFFER_SAMPLES, -+ .buf_size = BUFFER_SAMPLES * 2, -+}; -+ -+static struct ast_translator lin16toopus = { -+ .name = "lin16toopus", -+ .newpvt = lin16toopus_new, -+ .framein = lintoopus_framein, -+ .frameout = lintoopus_frameout, -+ .destroy = lintoopus_destroy, -+ .sample = slin16_sample, -+ .desc_size = sizeof(struct opus_coder_pvt), -+ .buffer_samples = BUFFER_SAMPLES, -+ .buf_size = BUFFER_SAMPLES * 2, -+}; -+ -+static struct ast_translator lin24toopus = { -+ .name = "lin24toopus", -+ .newpvt = lin24toopus_new, -+ .framein = lintoopus_framein, -+ .frameout = lintoopus_frameout, -+ .destroy = lintoopus_destroy, -+ .desc_size = sizeof(struct opus_coder_pvt), -+ .buffer_samples = BUFFER_SAMPLES, -+ .buf_size = BUFFER_SAMPLES * 2, -+}; -+ -+static struct ast_translator lin48toopus = { -+ .name = "lin48toopus", -+ .newpvt = lin48toopus_new, -+ .framein = lintoopus_framein, -+ .frameout = lintoopus_frameout, -+ .destroy = lintoopus_destroy, -+ .desc_size = sizeof(struct opus_coder_pvt), -+ .buffer_samples = BUFFER_SAMPLES, -+ .buf_size = BUFFER_SAMPLES * 2, -+}; -+ -+static struct ast_translator opustolin = { -+ .name = "opustolin", -+ .newpvt = opustolin_new, -+ .framein = opustolin_framein, -+ .destroy = opustolin_destroy, -+ .sample = opus_sample, -+ .desc_size = sizeof(struct opus_coder_pvt), -+ .buffer_samples = BUFFER_SAMPLES, -+ .buf_size = BUFFER_SAMPLES * 2, -+ .native_plc = 1, /* FIXME: needed? */ -+}; -+ -+static struct ast_translator opustolin12 = { -+ .name = "opustolin12", -+ .newpvt = opustolin12_new, -+ .framein = opustolin_framein, -+ .destroy = opustolin_destroy, -+ .sample = opus_sample, -+ .desc_size = sizeof(struct opus_coder_pvt), -+ .buffer_samples = BUFFER_SAMPLES, -+ .buf_size = BUFFER_SAMPLES * 2, -+ .native_plc = 1, /* FIXME: needed? */ -+}; -+ -+static struct ast_translator opustolin16 = { -+ .name = "opustolin16", -+ .newpvt = opustolin16_new, -+ .framein = opustolin_framein, -+ .destroy = opustolin_destroy, -+ .sample = opus_sample, -+ .desc_size = sizeof(struct opus_coder_pvt), -+ .buffer_samples = BUFFER_SAMPLES, -+ .buf_size = BUFFER_SAMPLES * 2, -+ .native_plc = 1, /* FIXME: needed? */ -+}; -+ -+static struct ast_translator opustolin24 = { -+ .name = "opustolin24", -+ .newpvt = opustolin24_new, -+ .framein = opustolin_framein, -+ .destroy = opustolin_destroy, -+ .sample = opus_sample, -+ .desc_size = sizeof(struct opus_coder_pvt), -+ .buffer_samples = BUFFER_SAMPLES, -+ .buf_size = BUFFER_SAMPLES * 2, -+ .native_plc = 1, /* FIXME: needed? */ -+}; -+ -+static struct ast_translator opustolin48 = { -+ .name = "opustolin48", -+ .newpvt = opustolin48_new, -+ .framein = opustolin_framein, -+ .destroy = opustolin_destroy, -+ .sample = opus_sample, -+ .desc_size = sizeof(struct opus_coder_pvt), -+ .buffer_samples = BUFFER_SAMPLES, -+ .buf_size = BUFFER_SAMPLES * 2, -+ .native_plc = 1, /* FIXME: needed? */ -+}; -+ -+ -+/* Simple CLI interface to enable/disable debugging */ -+static char *handle_cli_opus_set_debug(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) -+{ -+ switch (cmd) { -+ case CLI_INIT: -+ e->command = "opus set debug"; -+ e->usage = -+ "Usage: opus set debug {status|none|normal|huge}\n" -+ " Enable/Disable Opus debugging: normal only debugs setup and errors, huge debugs every single packet\n"; -+ return NULL; -+ case CLI_GENERATE: -+ return NULL; -+ } -+ -+ if (a->argc != 4) -+ return CLI_SHOWUSAGE; -+ -+ if (!strncasecmp(a->argv[a->argc-1], "status", 6)) { -+ ast_cli(a->fd, "Opus debugging %s\n", opusdebug > 1 ? "huge" : opusdebug > 0 ? "normal" : "none"); -+ return CLI_SUCCESS; -+ } -+ if (!strncasecmp(a->argv[a->argc-1], "huge", 4)) -+ opusdebug = 2; -+ else if (!strncasecmp(a->argv[a->argc-1], "normal", 6)) -+ opusdebug = 1; -+ else if (!strncasecmp(a->argv[a->argc-1], "none", 4)) -+ opusdebug = 0; -+ else -+ return CLI_SHOWUSAGE; -+ -+ ast_cli(a->fd, "Opus debugging %s\n", opusdebug > 1 ? "huge" : opusdebug > 0 ? "normal" : "none"); -+ return CLI_SUCCESS; -+} -+ -+static struct ast_cli_entry cli_opus[] = { -+ AST_CLI_DEFINE(handle_cli_opus_set_debug, "Enable/Disable Opus debugging"), -+}; -+ -+ -+/* Configuration and module setup */ -+static int parse_config(int reload) { -+ /* TODO: place stuff to negotiate/enforce here */ -+ return 0; -+} -+ -+static int reload(void) { -+ if(parse_config(1)) -+ return AST_MODULE_LOAD_DECLINE; -+ return AST_MODULE_LOAD_SUCCESS; -+} -+ -+static int unload_module(void) { -+ int res = 0; -+ -+ res |= ast_unregister_translator(&opustolin); -+ res |= ast_unregister_translator(&lintoopus); -+ res |= ast_unregister_translator(&opustolin12); -+ res |= ast_unregister_translator(&lin12toopus); -+ res |= ast_unregister_translator(&opustolin16); -+ res |= ast_unregister_translator(&lin16toopus); -+ res |= ast_unregister_translator(&opustolin24); -+ res |= ast_unregister_translator(&lin24toopus); -+ res |= ast_unregister_translator(&opustolin48); -+ res |= ast_unregister_translator(&lin48toopus); -+ -+ ast_cli_unregister_multiple(cli_opus, ARRAY_LEN(cli_opus)); -+ -+ return res; -+} -+ -+static int load_module(void) { -+ int res = 0; -+ -+ if(parse_config(0)) -+ return AST_MODULE_LOAD_DECLINE; -+ -+ /* 8khz (nb) */ -+ ast_format_set(&opustolin.src_format, AST_FORMAT_OPUS, 0); -+ ast_format_set(&opustolin.dst_format, AST_FORMAT_SLINEAR, 0); -+ ast_format_set(&lintoopus.src_format, AST_FORMAT_SLINEAR, 0); -+ ast_format_set(&lintoopus.dst_format, AST_FORMAT_OPUS, 0); -+ /* 12khz (mb) */ -+ ast_format_set(&opustolin12.src_format, AST_FORMAT_OPUS, 0); -+ ast_format_set(&opustolin12.dst_format, AST_FORMAT_SLINEAR12, 0); -+ ast_format_set(&lin12toopus.src_format, AST_FORMAT_SLINEAR12, 0); -+ ast_format_set(&lin12toopus.dst_format, AST_FORMAT_OPUS, 0); -+ /* 16khz (wb) */ -+ ast_format_set(&opustolin16.src_format, AST_FORMAT_OPUS, 0); -+ ast_format_set(&opustolin16.dst_format, AST_FORMAT_SLINEAR16, 0); -+ ast_format_set(&lin16toopus.src_format, AST_FORMAT_SLINEAR16, 0); -+ ast_format_set(&lin16toopus.dst_format, AST_FORMAT_OPUS, 0); -+ /* 24khz (swb) */ -+ ast_format_set(&opustolin24.src_format, AST_FORMAT_OPUS, 0); -+ ast_format_set(&opustolin24.dst_format, AST_FORMAT_SLINEAR24, 0); -+ ast_format_set(&lin24toopus.src_format, AST_FORMAT_SLINEAR24, 0); -+ ast_format_set(&lin24toopus.dst_format, AST_FORMAT_OPUS, 0); -+ /* 48khz (fb) */ -+ ast_format_set(&opustolin48.src_format, AST_FORMAT_OPUS, 0); -+ ast_format_set(&opustolin48.dst_format, AST_FORMAT_SLINEAR48, 0); -+ ast_format_set(&lin48toopus.src_format, AST_FORMAT_SLINEAR48, 0); -+ ast_format_set(&lin48toopus.dst_format, AST_FORMAT_OPUS, 0); -+ -+ res |= ast_register_translator(&opustolin); -+ res |= ast_register_translator(&lintoopus); -+ res |= ast_register_translator(&opustolin12); -+ res |= ast_register_translator(&lin12toopus); -+ res |= ast_register_translator(&opustolin16); -+ res |= ast_register_translator(&lin16toopus); -+ res |= ast_register_translator(&opustolin24); -+ res |= ast_register_translator(&lin24toopus); -+ res |= ast_register_translator(&opustolin48); -+ res |= ast_register_translator(&lin48toopus); -+ -+ ast_cli_register_multiple(cli_opus, sizeof(cli_opus) / sizeof(struct ast_cli_entry)); -+ -+ return res; -+} -+ -+AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Opus Coder/Decoder", -+ .load = load_module, -+ .unload = unload_module, -+ .reload = reload, -+ ); -diff -urN asterisk-11.1.2/codecs/ex_opus.h asterisk-11.1.2-opus/codecs/ex_opus.h ---- asterisk-11.1.2/codecs/ex_opus.h 1970-01-01 01:00:00.000000000 +0100 -+++ asterisk-11.1.2-opus/codecs/ex_opus.h 2013-05-24 17:06:09.976176036 +0200 -@@ -0,0 +1,35 @@ -+/*! \file -+ * \brief 8-bit data -+ * -+ * Copyright (C) 2008, Digium, Inc. -+ * -+ * Distributed under the terms of the GNU General Public License -+ * -+ */ -+ -+/* Opus, a 20ms sample */ -+static uint8_t ex_opus[] = { -+ 0x4b, 0x41, 0x25, 0x0b, 0xe4, 0x55, 0xc6, 0x74, -+ 0xda, 0xbb, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, -+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, -+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, -+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, -+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, -+}; -+ -+static struct ast_frame *opus_sample(void) -+{ -+ static struct ast_frame f = { -+ .frametype = AST_FRAME_VOICE, -+ .datalen = sizeof(ex_opus), -+ .samples = 960, //ARRAY_LEN(ex_opus), -+ .mallocd = 0, -+ .offset = 0, -+ .src = __PRETTY_FUNCTION__, -+ .data.ptr = ex_opus, -+ }; -+ -+ ast_format_set(&f.subclass.format, AST_FORMAT_OPUS, 0); -+ -+ return &f; -+} -diff -urN asterisk-11.1.2/configure.ac asterisk-11.1.2-opus/configure.ac ---- asterisk-11.1.2/configure.ac 2012-10-18 22:02:02.000000000 +0200 -+++ asterisk-11.1.2-opus/configure.ac 2013-05-24 16:25:25.766936899 +0200 -@@ -422,6 +422,7 @@ - AST_EXT_LIB_SETUP([NEWT], [newt], [newt]) - AST_EXT_LIB_SETUP([OGG], [OGG], [ogg]) - AST_EXT_LIB_SETUP([OPENR2], [MFR2], [openr2]) -+AST_EXT_LIB_SETUP([OPUS], [Opus], [opus]) - AST_EXT_LIB_SETUP([OSPTK], [OSP Toolkit], [osptk]) - AST_EXT_LIB_SETUP([OSS], [Open Sound System], [oss]) - AST_EXT_LIB_SETUP([PGSQL], [PostgreSQL], [postgres]) -@@ -2086,6 +2087,8 @@ - - AC_SUBST(PBX_SPEEX_PREPROCESS) - -+AST_EXT_LIB_CHECK([OPUS], [opus], [opus_encoder_create], [opus/opus.h]) -+ - AST_EXT_LIB_CHECK([SQLITE], [sqlite], [sqlite_exec], [sqlite.h]) - - AST_EXT_LIB_CHECK([SQLITE3], [sqlite3], [sqlite3_open], [sqlite3.h], [${PTHREAD_LIBS}], [${PTHREAD_CFLAGS}]) -diff -urN asterisk-11.1.2/formats/format_vp8.c asterisk-11.1.2-opus/formats/format_vp8.c ---- asterisk-11.1.2/formats/format_vp8.c 1970-01-01 01:00:00.000000000 +0100 -+++ asterisk-11.1.2-opus/formats/format_vp8.c 2013-05-24 17:01:31.070470091 +0200 -@@ -0,0 +1,195 @@ -+/* -+ * Asterisk -- An open source telephony toolkit. -+ * -+ * Copyright (C) 1999 - 2005, Digium, Inc. -+ * -+ * Mark Spencer -+ * -+ * See http://www.asterisk.org for more information about -+ * the Asterisk project. Please do not directly contact -+ * any of the maintainers of this project for assistance; -+ * the project provides a web site, mailing lists and IRC -+ * channels for your use. -+ * -+ * This program is free software, distributed under the terms of -+ * the GNU General Public License Version 2. See the LICENSE file -+ * at the top of the source tree. -+ */ -+ -+/*! \file -+ * -+ * \brief Save to raw, headerless VP8 data. -+ * -+ * \author Lorenzo Miniero -+ * -+ * \note Basically a "clone" of the H.264 passthrough format -+ * -+ * \arg File name extension: VP8 -+ * \ingroup formats -+ * \arg See \ref AstVideo -+ */ -+ -+/*** MODULEINFO -+ core -+ ***/ -+ -+#include "asterisk.h" -+ -+ASTERISK_FILE_VERSION(__FILE__, "$Revision: 328209 $") -+ -+#include "asterisk/mod_format.h" -+#include "asterisk/module.h" -+#include "asterisk/endian.h" -+ -+/* VP8 passthrough */ -+ -+#define BUF_SIZE 4096 -+struct vp8_desc { -+ unsigned int lastts; -+}; -+ -+static int vp8_open(struct ast_filestream *s) -+{ -+ unsigned int ts; -+ if (fread(&ts, 1, sizeof(ts), s->f) < sizeof(ts)) { -+ ast_log(LOG_WARNING, "Empty file!\n"); -+ return -1; -+ } -+ return 0; -+} -+ -+static struct ast_frame *vp8_read(struct ast_filestream *s, int *whennext) -+{ -+ int res; -+ int mark = 0; -+ unsigned short len; -+ unsigned int ts; -+ struct vp8_desc *fs = (struct vp8_desc *)s->_private; -+ -+ /* Send a frame from the file to the appropriate channel */ -+ if ((res = fread(&len, 1, sizeof(len), s->f)) < 1) -+ return NULL; -+ len = ntohs(len); -+ mark = (len & 0x8000) ? 1 : 0; -+ len &= 0x7fff; -+ if (len > BUF_SIZE) { -+ ast_log(LOG_WARNING, "Length %d is too long\n", len); -+ len = BUF_SIZE; /* XXX truncate */ -+ } -+ s->fr.frametype = AST_FRAME_VIDEO; -+ ast_format_set(&s->fr.subclass.format, AST_FORMAT_VP8, 0); -+ s->fr.mallocd = 0; -+ AST_FRAME_SET_BUFFER(&s->fr, s->buf, AST_FRIENDLY_OFFSET, len); -+ if ((res = fread(s->fr.data.ptr, 1, s->fr.datalen, s->f)) != s->fr.datalen) { -+ if (res) -+ ast_log(LOG_WARNING, "Short read (%d of %d) (%s)!\n", res, len, strerror(errno)); -+ return NULL; -+ } -+ s->fr.samples = fs->lastts; -+ s->fr.datalen = len; -+ if (mark) { -+ ast_format_set_video_mark(&s->fr.subclass.format); -+ } -+ s->fr.delivery.tv_sec = 0; -+ s->fr.delivery.tv_usec = 0; -+ if ((res = fread(&ts, 1, sizeof(ts), s->f)) == sizeof(ts)) { -+ fs->lastts = ntohl(ts); -+ *whennext = fs->lastts * 4/45; -+ } else -+ *whennext = 0; -+ return &s->fr; -+} -+ -+static int vp8_write(struct ast_filestream *s, struct ast_frame *f) -+{ -+ int res; -+ unsigned int ts; -+ unsigned short len; -+ int mark; -+ -+ if (f->frametype != AST_FRAME_VIDEO) { -+ ast_log(LOG_WARNING, "Asked to write non-video frame!\n"); -+ return -1; -+ } -+ mark = ast_format_get_video_mark(&f->subclass.format) ? 0x8000 : 0; -+ if (f->subclass.format.id != AST_FORMAT_VP8) { -+ ast_log(LOG_WARNING, "Asked to write non-VP8 frame (%s)!\n", ast_getformatname(&f->subclass.format)); -+ return -1; -+ } -+ ts = htonl(f->samples); -+ if ((res = fwrite(&ts, 1, sizeof(ts), s->f)) != sizeof(ts)) { -+ ast_log(LOG_WARNING, "Bad write (%d/4): %s\n", res, strerror(errno)); -+ return -1; -+ } -+ len = htons(f->datalen | mark); -+ if ((res = fwrite(&len, 1, sizeof(len), s->f)) != sizeof(len)) { -+ ast_log(LOG_WARNING, "Bad write (%d/2): %s\n", res, strerror(errno)); -+ return -1; -+ } -+ if ((res = fwrite(f->data.ptr, 1, f->datalen, s->f)) != f->datalen) { -+ ast_log(LOG_WARNING, "Bad write (%d/%d): %s\n", res, f->datalen, strerror(errno)); -+ return -1; -+ } -+ return 0; -+} -+ -+static int vp8_seek(struct ast_filestream *fs, off_t sample_offset, int whence) -+{ -+ /* No way Jose */ -+ return -1; -+} -+ -+static int vp8_trunc(struct ast_filestream *fs) -+{ -+ int fd; -+ off_t cur; -+ -+ if ((fd = fileno(fs->f)) < 0) { -+ ast_log(AST_LOG_WARNING, "Unable to determine file descriptor for VP8 filestream %p: %s\n", fs, strerror(errno)); -+ return -1; -+ } -+ if ((cur = ftello(fs->f)) < 0) { -+ ast_log(AST_LOG_WARNING, "Unable to determine current position in VP8 filestream %p: %s\n", fs, strerror(errno)); -+ return -1; -+ } -+ /* Truncate file to current length */ -+ return ftruncate(fd, cur); -+} -+ -+static off_t vp8_tell(struct ast_filestream *fs) -+{ -+ off_t offset = ftell(fs->f); -+ return offset; /* XXX totally bogus, needs fixing */ -+} -+ -+static struct ast_format_def vp8_f = { -+ .name = "VP8", -+ .exts = "vp8", -+ .open = vp8_open, -+ .write = vp8_write, -+ .seek = vp8_seek, -+ .trunc = vp8_trunc, -+ .tell = vp8_tell, -+ .read = vp8_read, -+ .buf_size = BUF_SIZE + AST_FRIENDLY_OFFSET, -+ .desc_size = sizeof(struct vp8_desc), -+}; -+ -+static int load_module(void) -+{ -+ ast_format_set(&vp8_f.format, AST_FORMAT_VP8, 0); -+ if (ast_format_def_register(&vp8_f)) -+ return AST_MODULE_LOAD_FAILURE; -+ return AST_MODULE_LOAD_SUCCESS; -+} -+ -+static int unload_module(void) -+{ -+ return ast_format_def_unregister(vp8_f.name); -+} -+ -+AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Raw VP8 data", -+ .load = load_module, -+ .unload = unload_module, -+ .load_pri = AST_MODPRI_APP_DEPEND -+); -diff -urN asterisk-11.1.2/include/asterisk/format.h asterisk-11.1.2-opus/include/asterisk/format.h ---- asterisk-11.1.2/include/asterisk/format.h 2012-07-13 20:41:07.000000000 +0200 -+++ asterisk-11.1.2-opus/include/asterisk/format.h 2013-05-24 16:42:59.833669803 +0200 -@@ -101,6 +101,8 @@ - AST_FORMAT_SLINEAR192 = 27 + AST_FORMAT_TYPE_AUDIO, - AST_FORMAT_SPEEX32 = 28 + AST_FORMAT_TYPE_AUDIO, - AST_FORMAT_CELT = 29 + AST_FORMAT_TYPE_AUDIO, -+ /*! Opus */ -+ AST_FORMAT_OPUS = 30 + AST_FORMAT_TYPE_AUDIO, - - /*! H.261 Video */ - AST_FORMAT_H261 = 1 + AST_FORMAT_TYPE_VIDEO, -@@ -112,6 +114,8 @@ - AST_FORMAT_H264 = 4 + AST_FORMAT_TYPE_VIDEO, - /*! MPEG4 Video */ - AST_FORMAT_MP4_VIDEO = 5 + AST_FORMAT_TYPE_VIDEO, -+ /*! VP8 */ -+ AST_FORMAT_VP8 = 6 + AST_FORMAT_TYPE_VIDEO, - - /*! JPEG Images */ - AST_FORMAT_JPEG = 1 + AST_FORMAT_TYPE_IMAGE, -diff -urN asterisk-11.1.2/main/channel.c asterisk-11.1.2-opus/main/channel.c ---- asterisk-11.1.2/main/channel.c 2013-01-02 20:23:44.000000000 +0100 -+++ asterisk-11.1.2-opus/main/channel.c 2013-05-24 16:41:27.279429674 +0200 -@@ -909,6 +909,8 @@ - AST_FORMAT_SPEEX32, - AST_FORMAT_SPEEX16, - AST_FORMAT_SPEEX, -+ /*! Opus */ -+ AST_FORMAT_OPUS, - /*! SILK is pretty awesome. */ - AST_FORMAT_SILK, - /*! CELT supports crazy high sample rates */ -diff -urN asterisk-11.1.2/main/format.c asterisk-11.1.2-opus/main/format.c ---- asterisk-11.1.2/main/format.c 2012-10-02 03:27:19.000000000 +0200 -+++ asterisk-11.1.2-opus/main/format.c 2013-06-04 17:20:35.935378513 +0200 -@@ -430,6 +430,9 @@ - /*! SpeeX Wideband (16kHz) Free Compression */ - case AST_FORMAT_SPEEX16: - return (1ULL << 33); -+ /*! Opus audio (8kHz, 16kHz, 24kHz, 48Khz) */ -+ case AST_FORMAT_OPUS: -+ return (1ULL << 34); - /*! Raw mu-law data (G.711) */ - case AST_FORMAT_TESTLAW: - return (1ULL << 47); -@@ -449,6 +452,9 @@ - /*! MPEG4 Video */ - case AST_FORMAT_MP4_VIDEO: - return (1ULL << 22); -+ /*! VP8 Video */ -+ case AST_FORMAT_VP8: -+ return (1ULL << 23); - - /*! JPEG Images */ - case AST_FORMAT_JPEG: -@@ -532,6 +538,9 @@ - /*! SpeeX Wideband (16kHz) Free Compression */ - case (1ULL << 33): - return ast_format_set(dst, AST_FORMAT_SPEEX16, 0); -+ /*! Opus audio (8kHz, 16kHz, 24kHz, 48Khz) */ -+ case (1ULL << 34): -+ return ast_format_set(dst, AST_FORMAT_OPUS, 0); - /*! Raw mu-law data (G.711) */ - case (1ULL << 47): - return ast_format_set(dst, AST_FORMAT_TESTLAW, 0); -@@ -551,6 +560,9 @@ - /*! MPEG4 Video */ - case (1ULL << 22): - return ast_format_set(dst, AST_FORMAT_MP4_VIDEO, 0); -+ /*! VP8 Video */ -+ case (1ULL << 23): -+ return ast_format_set(dst, AST_FORMAT_VP8, 0); - - /*! JPEG Images */ - case (1ULL << 16): -@@ -782,6 +794,9 @@ - return samplerate; - } - } -+ /* Opus */ -+ case AST_FORMAT_OPUS: -+ return 48000; - default: - return 8000; - } -@@ -1072,6 +1087,10 @@ - format_list_add_static(ast_format_set(&tmpfmt, AST_FORMAT_SLINEAR48, 0), "slin48", 48000, "16 bit Signed Linear PCM (48kHz)", 960, 10, 70, 10, 20, AST_SMOOTHER_FLAG_BE, 0);/*!< Signed linear (48kHz) */ - format_list_add_static(ast_format_set(&tmpfmt, AST_FORMAT_SLINEAR96, 0), "slin96", 96000, "16 bit Signed Linear PCM (96kHz)", 1920, 10, 70, 10, 20, AST_SMOOTHER_FLAG_BE, 0);/*!< Signed linear (96kHz) */ - format_list_add_static(ast_format_set(&tmpfmt, AST_FORMAT_SLINEAR192, 0), "slin192", 192000, "16 bit Signed Linear PCM (192kHz)", 3840, 10, 70, 10, 20, AST_SMOOTHER_FLAG_BE, 0);/*!< Signed linear (192kHz) */ -+ /* Opus (FIXME: real min is 3/5/10, real max is 120...) */ -+ format_list_add_static(ast_format_set(&tmpfmt, AST_FORMAT_OPUS, 0), "opus", 48000, "Opus Codec", 10, 20, 60, 20, 20, 0, 0); /*!< codec_opus.c */ -+ /* VP8 (passthrough) */ -+ format_list_add_static(ast_format_set(&tmpfmt, AST_FORMAT_VP8, 0), "vp8", 0, "VP8 Video", 0, 0, 0, 0 ,0 ,0, 0); /*!< Passthrough support, see format_h263.c */ - - return 0; - } -diff -urN asterisk-11.1.2/main/frame.c asterisk-11.1.2-opus/main/frame.c ---- asterisk-11.1.2/main/frame.c 2012-07-24 18:54:26.000000000 +0200 -+++ asterisk-11.1.2-opus/main/frame.c 2013-05-24 16:34:23.407329930 +0200 -@@ -1002,6 +1002,40 @@ - return cnt; - } - -+/* Opus: copied from opus_decoder.c */ -+static int opus_samples(unsigned char *data, int len) { -+ /* Do opus_packet_get_nb_frames first */ -+ int count, frames; -+ if (len<1) { -+ return 0; /* FIXME OPUS_BAD_ARG */ -+ } else { -+ count = data[0]&0x3; -+ if (count==0) -+ frames = 1; -+ else if (count!=3) -+ frames = 2; -+ else if (len<2) -+ return 0; /* FIXME OPUS_INVALID_PACKET */ -+ else -+ frames = data[1]&0x3F; -+ } -+ /* The, do a opus_packet_get_samples_per_frame */ -+ int audiosize, Fs = 48000; -+ if (data[0]&0x80) { -+ audiosize = ((data[0]>>3)&0x3); -+ audiosize = (Fs<>3)&0x3); -+ if (audiosize == 3) -+ audiosize = Fs*60/1000; -+ else -+ audiosize = (Fs<subclass.format) / 50; - break; -+ /* Opus */ -+ case AST_FORMAT_OPUS: -+ samples = opus_samples(f->data.ptr, f->datalen); -+ break; - default: - ast_log(LOG_WARNING, "Unable to calculate samples for format %s\n", ast_getformatname(&f->subclass.format)); - } -diff -urN asterisk-11.1.2/main/rtp_engine.c asterisk-11.1.2-opus/main/rtp_engine.c ---- asterisk-11.1.2/main/rtp_engine.c 2012-09-20 20:18:47.000000000 +0200 -+++ asterisk-11.1.2-opus/main/rtp_engine.c 2013-05-24 16:32:34.867048313 +0200 -@@ -2268,6 +2268,9 @@ - set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_SIREN7, 0), 0, "audio", "G7221", 16000); - set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_SIREN14, 0), 0, "audio", "G7221", 32000); - set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_G719, 0), 0, "audio", "G719", 48000); -+ /* Opus and VP8 */ -+ set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_OPUS, 0), 0, "audio", "opus", 48000); -+ set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_VP8, 0), 0, "video", "VP8", 90000); - - /* Define the static rtp payload mappings */ - add_static_payload(0, ast_format_set(&tmpfmt, AST_FORMAT_ULAW, 0), 0); -@@ -2309,6 +2312,9 @@ - add_static_payload(118, ast_format_set(&tmpfmt, AST_FORMAT_SLINEAR16, 0), 0); /* 16 Khz signed linear */ - add_static_payload(119, ast_format_set(&tmpfmt, AST_FORMAT_SPEEX32, 0), 0); - add_static_payload(121, NULL, AST_RTP_CISCO_DTMF); /* Must be type 121 */ -+ /* Opus and VP8 */ -+ add_static_payload(100, ast_format_set(&tmpfmt, AST_FORMAT_VP8, 0), 0); -+ add_static_payload(107, ast_format_set(&tmpfmt, AST_FORMAT_OPUS, 0), 0); - - return 0; - } -diff -urN asterisk-11.1.2/makeopts.in asterisk-11.1.2-opus/makeopts.in ---- asterisk-11.1.2/makeopts.in 2012-10-18 22:02:02.000000000 +0200 -+++ asterisk-11.1.2-opus/makeopts.in 2013-05-24 16:23:57.912709903 +0200 -@@ -261,6 +261,9 @@ - SPEEXDSP_INCLUDE=@SPEEXDSP_INCLUDE@ - SPEEXDSP_LIB=@SPEEXDSP_LIB@ - -+OPUS_INCLUDE=@OPUS_INCLUDE@ -+OPUS_LIB=@OPUS_LIB@ -+ - SQLITE_INCLUDE=@SQLITE_INCLUDE@ - SQLITE_LIB=@SQLITE_LIB@ - -diff -urN asterisk-11.1.2/res/res_rtp_asterisk.c asterisk-11.1.2-opus/res/res_rtp_asterisk.c ---- asterisk-11.1.2/res/res_rtp_asterisk.c 2012-10-11 18:04:19.000000000 +0200 -+++ asterisk-11.1.2-opus/res/res_rtp_asterisk.c 2013-05-24 16:57:10.012799120 +0200 -@@ -91,6 +91,8 @@ - #define RTCP_PT_SDES 202 - #define RTCP_PT_BYE 203 - #define RTCP_PT_APP 204 -+/* VP8: RTCP Feedback */ -+#define RTCP_PT_PSFB 206 - - #define RTP_MTU 1200 - -@@ -341,6 +343,9 @@ - double normdevrtt; - double stdevrtt; - unsigned int rtt_count; -+ -+ /* VP8: sequence number for the RTCP FIR FCI */ -+ int firseq; - }; - - struct rtp_red { -@@ -2599,6 +2604,41 @@ - return 0; - } - -+ /* VP8: is this a request to send a RTCP FIR? */ -+ if(frame->frametype == AST_FRAME_CONTROL && frame->subclass.integer == AST_CONTROL_VIDUPDATE) { -+ ast_log(LOG_WARNING, "res_rtp_asterisk, requested to send a RTCP FIR packet to the peer\n"); -+ struct ast_rtp *rtp = ast_rtp_instance_get_data(instance); -+ if (!rtp || !rtp->rtcp) -+ return 0; -+ unsigned int *rtcpheader; -+ char bdata[1024]; -+ if (ast_sockaddr_isnull(&rtp->rtcp->them)) { -+ /* -+ * RTCP was stopped. -+ */ -+ return 0; -+ } -+ /* Prepare RTCP FIR (PT=206, FMT=4) */ -+ rtp->rtcp->firseq++; -+ if(rtp->rtcp->firseq == 256) -+ rtp->rtcp->firseq = 0; -+ int len = 20; -+ int ice; -+ rtcpheader = (unsigned int *)bdata; -+ rtcpheader[0] = htonl((2 << 30) | (4 << 24) | (RTCP_PT_PSFB << 16) | ((len/4)-1)); -+ rtcpheader[1] = htonl(rtp->ssrc); -+ rtcpheader[2] = htonl(rtp->themssrc); -+ rtcpheader[3] = htonl(rtp->themssrc); /* FCI: SSRC */ -+ rtcpheader[4] = htonl(rtp->rtcp->firseq << 24); /* FCI: Sequence number */ -+ int res = rtcp_sendto(instance, (unsigned int *)rtcpheader, len, 0, &rtp->rtcp->them, &ice); -+ if (res < 0) { -+ ast_log(LOG_ERROR, "RTCP FIR transmission error: %s\n",strerror(errno)); -+ return 0; -+ } -+ ast_log(LOG_WARNING, " >> RTCP FIR packet sent to the peer!\n"); -+ return 0; -+ } -+ - /* If there is no data length we can't very well send the packet */ - if (!frame->datalen) { - ast_debug(1, "Received frame with no data for RTP instance '%p' so dropping frame\n", instance); -@@ -2650,6 +2690,8 @@ - case AST_FORMAT_SIREN7: - case AST_FORMAT_SIREN14: - case AST_FORMAT_G719: -+ /* Opus */ -+ case AST_FORMAT_OPUS: - /* these are all frame-based codecs and cannot be safely run through - a smoother */ - break; From 1a491760224b6cf443897403d9cbeed2e4576898 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jaron=20Vi=C3=ABtor?= Date: Sat, 10 May 2014 22:12:07 +0200 Subject: [PATCH 2/3] Updated readme for 12.2.0 version --- README.md | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 50c7d9f..0859d86 100644 --- a/README.md +++ b/README.md @@ -1,14 +1,14 @@ #Asterisk Opus/VP8 patch ======================= -Since Opus and VP8 cannot, as of now, be integrated in the Asterisk repositories (learn why in this [thread](http://lists.digium.com/pipermail/asterisk-dev/2013-May/060356.html)), we prepared a patch that adds support for both codecs (Opus transcoding, VP8 passthrough) to [Asterisk 11.1.2](http://downloads.asterisk.org/pub/telephony/asterisk/releases/). +Since Opus cannot, as of now, be integrated in the Asterisk repositories (learn why in this [thread](http://lists.digium.com/pipermail/asterisk-dev/2013-May/060356.html)), we prepared a patch that adds support for Opus transcoding to [Asterisk 12.2.0](http://downloads.asterisk.org/pub/telephony/asterisk/releases/). ##Installing the patch -To support Opus, you'll need to install [libopus](http://www.opus-codec.org/downloads/) first. No library is needed for VP8, as its support is passthrough only. +To support Opus, you'll need to install [libopus](http://www.opus-codec.org/downloads/) first. -The patch was built on top of Asterisk 11.1.2: applying it on different versions may or may not work out of the box, but solving conflicts shouldn't be too hard anyway. Copy it in the Asterisk source folder and apply it: +The patch was built on top of Asterisk 12.2.0: applying it on different versions may or may not work out of the box, but solving conflicts shouldn't be too hard anyway. Copy it in the Asterisk source folder and apply it: - patch -p1 -u < asterisk_opus+vp8.diff + patch -p1 -u < asterisk_12.2.0_opus.diff Run the bootstrap script to regenerate the configure: @@ -18,7 +18,7 @@ Configure the patched Asterisk. ./configure --prefix=/usr -Make sure that codec\_opus and format\_vp8 are enabled in menuselect before going on. Besides, for better results, install the slin16 versions of the Asterisk sounds, which are not enabled by default. +Make sure that codec\_opus is enabled in menuselect before going on. make menuselect @@ -33,10 +33,8 @@ You can test Opus using the free softphone [PhonerLite](http://phonerlite.de/dow Usage: opus set debug {status|none|normal|huge} Enable/Disable Opus debugging: normal only debugs setup and errors, huge debugs every single packet -For VP8 you can make use of the open source softphone [Linphone](http://www.linphone.org/eng/linphone/news/linphone-3.5.0-released-for-desktop.html), which added support for VP8 in version 3.5.0. - ##What is missing -SDP fmtp parameters related to Opus and defined in [draft-ietf-payload-rtp-opus](http://tools.ietf.org/html/draft-ietf-payload-rtp-opus-00) are parsed but currently ignored: this means that there's no interaction between chan\_sip and codec\_opus in that sense. Besides, there still is no ad-hoc Opus configuration file for codec defaults. VP8, as anticipated, is passthrough only: besides, there's currently no way to read VP8 files for Playback. +SDP fmtp parameters related to Opus and defined in [draft-ietf-payload-rtp-opus](http://tools.ietf.org/html/draft-ietf-payload-rtp-opus-00) are parsed but currently ignored: this means that there's no interaction between chan\_sip and codec\_opus in that sense. Besides, there still is no ad-hoc Opus configuration file for codec defaults. ##Help us improve the support! Found an issue? Solved one? Added something that was missing? Help us make it better! From 2a33f723d6dda3a014f30cc5072858013be0fe58 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jaron=20Vi=C3=ABtor?= Date: Sat, 10 May 2014 22:12:27 +0200 Subject: [PATCH 3/3] Updated readme some more. --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 0859d86..9b9bedd 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -#Asterisk Opus/VP8 patch +#Asterisk Opus patch ======================= Since Opus cannot, as of now, be integrated in the Asterisk repositories (learn why in this [thread](http://lists.digium.com/pipermail/asterisk-dev/2013-May/060356.html)), we prepared a patch that adds support for Opus transcoding to [Asterisk 12.2.0](http://downloads.asterisk.org/pub/telephony/asterisk/releases/).