[asterisk-commits] codecs: Add iLBC 20. (asterisk[14])
SVN commits to the Asterisk project
asterisk-commits at lists.digium.com
Tue Jul 26 10:38:11 CDT 2016
Anonymous Coward #1000019 has submitted this change and it was merged.
Change subject: codecs: Add iLBC 20.
......................................................................
codecs: Add iLBC 20.
Asterisk already supported iLBC 30. This change adds iLBC 20. Now, Asterisk
defaults to iLBC 20 but falls back to iLBC 30, when the remote party requests
this.
ASTERISK-26218 #close
ASTERISK-26221 #close
Reported by: Aaron Meriwether
Change-Id: I07f523a3aa1338bb5217a1bf69c1eeb92adedffa
---
M CHANGES
M codecs/codec_ilbc.c
M codecs/ex_ilbc.h
A include/asterisk/ilbc.h
M main/codec_builtin.c
A res/res_format_attr_ilbc.c
6 files changed, 252 insertions(+), 36 deletions(-)
Approvals:
Kevin Harwell: Looks good to me, but someone else must approve
Mark Michelson: Looks good to me, approved
Anonymous Coward #1000019: Verified
Joshua Colp: Looks good to me, but someone else must approve
diff --git a/CHANGES b/CHANGES
index 9caa524..a0dffe6 100644
--- a/CHANGES
+++ b/CHANGES
@@ -63,6 +63,12 @@
experienced when using ChanSpy, but may introduce some delay in the audio
feed on the listening channel.
+Codecs
+------------------
+ * Added format attribute negotiation for the iLBC audio codec. Format attribute
+ negotiation is provided by the res_format_attr_ilbc module. iLBC 20 is the
+ default now. Falls back to iLBC 30, when the remote party requests this.
+
ConfBridge
------------------
* Added the ability to pass options to MixMonitor when recording is used with
diff --git a/codecs/codec_ilbc.c b/codecs/codec_ilbc.c
index 3e480e8..fc713be 100644
--- a/codecs/codec_ilbc.c
+++ b/codecs/codec_ilbc.c
@@ -34,10 +34,13 @@
ASTERISK_REGISTER_FILE()
-#include "asterisk/translate.h"
+#include "asterisk/codec.h" /* for AST_MEDIA_TYPE_AUDIO */
+#include "asterisk/format.h" /* for ast_format_get_attribute_data */
+#include "asterisk/frame.h" /* for ast_frame, etc */
+#include "asterisk/linkedlists.h" /* for AST_LIST_NEXT, etc */
+#include "asterisk/logger.h" /* for ast_log, ast_debug, etc */
#include "asterisk/module.h"
-#include "asterisk/utils.h"
-#include "asterisk/linkedlists.h"
+#include "asterisk/translate.h" /* for ast_trans_pvt, etc */
#ifdef ILBC_WEBRTC
#include <ilbc.h>
@@ -52,13 +55,10 @@
#define BUF_TYPE uc
#endif
-#define USE_ILBC_ENHANCER 0
-#define ILBC_MS 30
-/* #define ILBC_MS 20 */
+#include "asterisk/ilbc.h"
-#define ILBC_FRAME_LEN 50 /* apparently... */
-#define ILBC_SAMPLES 240 /* 30ms at 8000 hz */
-#define BUFFER_SAMPLES 8000
+#define USE_ILBC_ENHANCER 0
+#define BUFFER_SAMPLES 8000
/* Sample frame data */
#include "asterisk/slin.h"
@@ -69,13 +69,16 @@
iLBC_Dec_Inst_t dec;
/* Enough to store a full second */
int16_t buf[BUFFER_SAMPLES];
+ int16_t inited;
};
static int lintoilbc_new(struct ast_trans_pvt *pvt)
{
struct ilbc_coder_pvt *tmp = pvt->pvt;
+ struct ilbc_attr *attr = pvt->explicit_dst ? ast_format_get_attribute_data(pvt->explicit_dst) : NULL;
+ const unsigned int mode = attr ? attr->mode : 30;
- initEncode(&tmp->enc, ILBC_MS);
+ initEncode(&tmp->enc, mode);
return 0;
}
@@ -84,7 +87,7 @@
{
struct ilbc_coder_pvt *tmp = pvt->pvt;
- initDecode(&tmp->dec, ILBC_MS, USE_ILBC_ENHANCER);
+ tmp->inited = 0; /* we do not know the iLBC mode, yet */
return 0;
}
@@ -93,13 +96,19 @@
static int ilbctolin_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
{
struct ilbc_coder_pvt *tmp = pvt->pvt;
+ struct ilbc_attr *attr = ast_format_get_attribute_data(f->subclass.format);
+ const unsigned int mode = attr ? attr->mode : 30;
+ const unsigned int sample_rate = pvt->t->dst_codec.sample_rate;
+ const unsigned int samples_per_frame = mode * sample_rate / 1000;
+ const unsigned int octets_per_frame = (mode == 20) ? 38 : 50;
+
int plc_mode = 1; /* 1 = normal data, 0 = plc */
/* Assuming there's space left, decode into the current buffer at
the tail location. Read in as many frames as there are */
int x,i;
int datalen = f->datalen;
int16_t *dst = pvt->outbuf.i16;
- ilbc_block tmpf[ILBC_SAMPLES];
+ ilbc_block tmpf[samples_per_frame];
if (!f->data.ptr && datalen) {
ast_debug(1, "issue 16070, ILIB ERROR. data = NULL datalen = %d src = %s\n", datalen, f->src ? f->src : "no src set");
@@ -108,27 +117,32 @@
}
if (datalen == 0) { /* native PLC, set fake datalen and clear plc_mode */
- datalen = ILBC_FRAME_LEN;
- f->samples = ILBC_SAMPLES;
+ datalen = octets_per_frame;
+ f->samples = samples_per_frame;
plc_mode = 0; /* do native plc */
- pvt->samples += ILBC_SAMPLES;
+ pvt->samples += samples_per_frame;
}
- if (datalen % ILBC_FRAME_LEN) {
- ast_log(LOG_WARNING, "Huh? An ilbc frame that isn't a multiple of 50 bytes long from %s (%d)?\n", f->src, datalen);
+ if (datalen % octets_per_frame) {
+ ast_log(LOG_WARNING, "Huh? An ilbc frame that isn't a multiple of %u bytes long from %s (%d)?\n", octets_per_frame, f->src, datalen);
return -1;
}
- for (x=0; x < datalen ; x += ILBC_FRAME_LEN) {
- if (pvt->samples + ILBC_SAMPLES > BUFFER_SAMPLES) {
+ if (!tmp->inited) {
+ initDecode(&tmp->dec, mode, USE_ILBC_ENHANCER);
+ tmp->inited = 1;
+ }
+
+ for (x = 0; x < datalen; x += octets_per_frame) {
+ if (pvt->samples + samples_per_frame > BUFFER_SAMPLES) {
ast_log(LOG_WARNING, "Out of buffer space\n");
return -1;
}
iLBC_decode(tmpf, plc_mode ? f->data.ptr + x : NULL, &tmp->dec, plc_mode);
- for ( i=0; i < ILBC_SAMPLES; i++)
+ for (i = 0; i < samples_per_frame; i++)
dst[pvt->samples + i] = tmpf[i];
- pvt->samples += ILBC_SAMPLES;
- pvt->datalen += 2*ILBC_SAMPLES;
+ pvt->samples += samples_per_frame;
+ pvt->datalen += samples_per_frame * 2;
}
return 0;
}
@@ -155,20 +169,26 @@
struct ast_frame *last = NULL;
int samples = 0; /* output samples */
- while (pvt->samples >= ILBC_SAMPLES) {
+ struct ilbc_attr *attr = ast_format_get_attribute_data(pvt->f.subclass.format);
+ const unsigned int mode = attr ? attr->mode : 30;
+ const unsigned int sample_rate = pvt->t->dst_codec.sample_rate;
+ const unsigned int samples_per_frame = mode * sample_rate / 1000;
+ const unsigned int octets_per_frame = (mode == 20) ? 38 : 50;
+
+ while (pvt->samples >= samples_per_frame) {
struct ast_frame *current;
- ilbc_block tmpf[ILBC_SAMPLES];
+ ilbc_block tmpf[samples_per_frame];
int i;
/* Encode a frame of data */
- for (i = 0 ; i < ILBC_SAMPLES ; i++)
+ for (i = 0; i < samples_per_frame; i++)
tmpf[i] = tmp->buf[samples + i];
iLBC_encode((ilbc_bytes *) pvt->outbuf.BUF_TYPE, tmpf, &tmp->enc);
- samples += ILBC_SAMPLES;
- pvt->samples -= ILBC_SAMPLES;
+ samples += samples_per_frame;
+ pvt->samples -= samples_per_frame;
- current = ast_trans_frameout(pvt, ILBC_FRAME_LEN, ILBC_SAMPLES);
+ current = ast_trans_frameout(pvt, octets_per_frame, samples_per_frame);
if (!current) {
continue;
} else if (last) {
@@ -226,7 +246,8 @@
.frameout = lintoilbc_frameout,
.sample = slin8_sample,
.desc_size = sizeof(struct ilbc_coder_pvt),
- .buf_size = (BUFFER_SAMPLES * ILBC_FRAME_LEN + ILBC_SAMPLES - 1) / ILBC_SAMPLES,
+ /* frame len (38 bytes), frame size (160 samples), ceil (+ 160 - 1) */
+ .buf_size = (BUFFER_SAMPLES * 38 + 160 - 1) / 160,
};
static int unload_module(void)
diff --git a/codecs/ex_ilbc.h b/codecs/ex_ilbc.h
index 3a79b09..3fe2749 100644
--- a/codecs/ex_ilbc.h
+++ b/codecs/ex_ilbc.h
@@ -7,6 +7,9 @@
*
*/
+#include "asterisk/format_cache.h" /* for ast_format_ilbc */
+#include "asterisk/frame.h" /* for ast_frame, etc */
+
static uint8_t ex_ilbc[] = {
0xff, 0xa0, 0xff, 0xfa, 0x0f, 0x60, 0x12, 0x11, 0xa2, 0x47,
0x22, 0x8c, 0x00, 0x00, 0x01, 0x02, 0x80, 0x43, 0xa0, 0x40,
@@ -20,8 +23,8 @@
static struct ast_frame f = {
.frametype = AST_FRAME_VOICE,
.datalen = sizeof(ex_ilbc),
- /* All frames are 30 ms long */
- .samples = ILBC_SAMPLES,
+ /* example frames are default long (30 ms) */
+ .samples = 240,
.mallocd = 0,
.offset = 0,
.src = __PRETTY_FUNCTION__,
diff --git a/include/asterisk/ilbc.h b/include/asterisk/ilbc.h
new file mode 100644
index 0000000..2534367
--- /dev/null
+++ b/include/asterisk/ilbc.h
@@ -0,0 +1,8 @@
+#ifndef _AST_FORMAT_ILBC_H_
+#define _AST_FORMAT_ILBC_H_
+
+struct ilbc_attr {
+ unsigned int mode;
+};
+
+#endif /* _AST_FORMAT_ILBC_H */
\ No newline at end of file
diff --git a/main/codec_builtin.c b/main/codec_builtin.c
index 50fbf55..1514798 100644
--- a/main/codec_builtin.c
+++ b/main/codec_builtin.c
@@ -31,6 +31,7 @@
ASTERISK_REGISTER_FILE()
+#include "asterisk/ilbc.h"
#include "asterisk/logger.h"
#include "asterisk/astobj2.h"
#include "asterisk/codec.h"
@@ -588,7 +589,12 @@
static int ilbc_samples(struct ast_frame *frame)
{
- return 240 * (frame->datalen / 50);
+ struct ilbc_attr *attr = ast_format_get_attribute_data(frame->subclass.format);
+ const unsigned int mode = attr ? attr->mode : 30;
+ const unsigned int samples_per_frame = mode * ast_format_get_sample_rate(frame->subclass.format) / 1000;
+ const unsigned int octets_per_frame = (mode == 20) ? 38 : 50;
+
+ return samples_per_frame * frame->datalen / octets_per_frame;
}
static struct ast_codec ilbc = {
@@ -596,12 +602,12 @@
.description = "iLBC",
.type = AST_MEDIA_TYPE_AUDIO,
.sample_rate = 8000,
- .minimum_ms = 30,
+ .minimum_ms = 20,
.maximum_ms = 300,
- .default_ms = 30,
- .minimum_bytes = 50,
+ .default_ms = 20,
+ .minimum_bytes = 38,
.samples_count = ilbc_samples,
- .smooth = 1,
+ .smooth = 0,
};
static struct ast_codec g722 = {
diff --git a/res/res_format_attr_ilbc.c b/res/res_format_attr_ilbc.c
new file mode 100644
index 0000000..0fac55c
--- /dev/null
+++ b/res/res_format_attr_ilbc.c
@@ -0,0 +1,172 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 2016, Alexander Traud
+ *
+ * Alexander Traud <pabstraud at compuserve.com>
+ *
+ * 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 iLBC format attribute interface
+ *
+ * \author Alexander Traud <pabstraud at compuserve.com>
+ *
+ * \note http://tools.ietf.org/html/rfc3952
+ */
+
+/*** MODULEINFO
+ <support_level>core</support_level>
+ ***/
+
+#include "asterisk.h"
+
+#include "asterisk/module.h"
+#include "asterisk/format.h"
+#include "asterisk/strings.h" /* for ast_str_append */
+#include "asterisk/utils.h" /* for ast_calloc, ast_free */
+
+#include "asterisk/ilbc.h"
+
+static struct ilbc_attr default_ilbc_attr = {
+ .mode = 20,
+};
+
+static void ilbc_destroy(struct ast_format *format)
+{
+ struct ilbc_attr *attr = ast_format_get_attribute_data(format);
+
+ ast_free(attr);
+}
+
+static int ilbc_clone(const struct ast_format *src, struct ast_format *dst)
+{
+ struct ilbc_attr *original = ast_format_get_attribute_data(src);
+ struct ilbc_attr *attr = ast_malloc(sizeof(*attr));
+
+ if (!attr) {
+ return -1;
+ }
+
+ if (original) {
+ *attr = *original;
+ } else {
+ *attr = default_ilbc_attr;
+ }
+
+ ast_format_set_attribute_data(dst, attr);
+
+ return 0;
+}
+
+static struct ast_format *ilbc_parse_sdp_fmtp(const struct ast_format *format, const char *attributes)
+{
+ struct ast_format *cloned;
+ struct ilbc_attr *attr;
+ const char *kvp;
+ unsigned int val;
+
+ cloned = ast_format_clone(format);
+ if (!cloned) {
+ return NULL;
+ }
+ attr = ast_format_get_attribute_data(cloned);
+
+ if ((kvp = strstr(attributes, "mode")) && sscanf(kvp, "mode=%30u", &val) == 1) {
+ attr->mode = val;
+ } else {
+ attr->mode = 30; /* optional attribute; 30 is default value */
+ }
+
+ return cloned;
+}
+
+static void ilbc_generate_sdp_fmtp(const struct ast_format *format, unsigned int payload, struct ast_str **str)
+{
+ struct ilbc_attr *attr = ast_format_get_attribute_data(format);
+
+ if (!attr) {
+ attr = &default_ilbc_attr;
+ }
+
+ /* When the VoIP/SIP client Zoiper calls Asterisk and its
+ * iLBC 20 is disabled but iLBC 30 enabled, Zoiper still
+ * falls back to iLBC 20, when there is no mode=30 in the
+ * answer. Consequently, Zoiper defaults to iLBC 20. To
+ * make that client happy, Asterisk sends mode always.
+ * tested in June 2016, Zoiper Premium 1.13.2 for iPhone
+ */
+ /* if (attr->mode != 30) */ {
+ ast_str_append(str, 0, "a=fmtp:%u mode=%u\r\n", payload, attr->mode);
+ }
+}
+
+static struct ast_format *ilbc_getjoint(const struct ast_format *format1, const struct ast_format *format2)
+{
+ struct ast_format *jointformat;
+ struct ilbc_attr *attr1 = ast_format_get_attribute_data(format1);
+ struct ilbc_attr *attr2 = ast_format_get_attribute_data(format2);
+ struct ilbc_attr *attr_res;
+
+ if (!attr1) {
+ attr1 = &default_ilbc_attr;
+ }
+
+ if (!attr2) {
+ attr2 = &default_ilbc_attr;
+ }
+
+ jointformat = ast_format_clone(format1);
+ if (!jointformat) {
+ return NULL;
+ }
+ attr_res = ast_format_get_attribute_data(jointformat);
+
+ if (attr1->mode != attr2->mode) {
+ attr_res->mode = 30;
+ }
+
+ return jointformat;
+}
+
+static struct ast_format_interface ilbc_interface = {
+ .format_destroy = ilbc_destroy,
+ .format_clone = ilbc_clone,
+ .format_cmp = NULL,
+ .format_get_joint = ilbc_getjoint,
+ .format_attribute_set = NULL,
+ .format_parse_sdp_fmtp = ilbc_parse_sdp_fmtp,
+ .format_generate_sdp_fmtp = ilbc_generate_sdp_fmtp,
+};
+
+static int load_module(void)
+{
+ if (ast_format_interface_register("ilbc", &ilbc_interface)) {
+ return AST_MODULE_LOAD_DECLINE;
+ }
+
+ return AST_MODULE_LOAD_SUCCESS;
+}
+
+static int unload_module(void)
+{
+ return 0;
+}
+
+AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER,
+ "iLBC Format Attribute Module",
+ .support_level = AST_MODULE_SUPPORT_CORE,
+ .load = load_module,
+ .unload = unload_module,
+ .load_pri = AST_MODPRI_CHANNEL_DEPEND,
+);
--
To view, visit https://gerrit.asterisk.org/3304
To unsubscribe, visit https://gerrit.asterisk.org/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: I07f523a3aa1338bb5217a1bf69c1eeb92adedffa
Gerrit-PatchSet: 1
Gerrit-Project: asterisk
Gerrit-Branch: 14
Gerrit-Owner: Alexander Traud <pabstraud at compuserve.com>
Gerrit-Reviewer: Anonymous Coward #1000019
Gerrit-Reviewer: Joshua Colp <jcolp at digium.com>
Gerrit-Reviewer: Kevin Harwell <kharwell at digium.com>
Gerrit-Reviewer: Mark Michelson <mmichelson at digium.com>
More information about the asterisk-commits
mailing list