[Asterisk-code-review] res format attr ilbc.c: Add format attribute handler for iLBC (asterisk[13])

Sean Bright asteriskteam at digium.com
Tue Dec 11 08:01:29 CST 2018


Sean Bright has uploaded this change for review. ( https://gerrit.asterisk.org/10781


Change subject: res_format_attr_ilbc.c: Add format attribute handler for iLBC
......................................................................

res_format_attr_ilbc.c: Add format attribute handler for iLBC

ASTERISK-28199 #close

Change-Id: I5c5d37984826c6738706eae6b9edb284d564ce2b
---
A res/res_format_attr_ilbc.c
1 file changed, 241 insertions(+), 0 deletions(-)



  git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/81/10781/1

diff --git a/res/res_format_attr_ilbc.c b/res/res_format_attr_ilbc.c
new file mode 100644
index 0000000..542b7e3
--- /dev/null
+++ b/res/res_format_attr_ilbc.c
@@ -0,0 +1,241 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 2018, Digium, Inc.
+ *
+ * Sean Bright <sean.bright at gmail.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 Module
+ *
+ * \author\verbatim Sean Bright <sean.bright at gmail.com> \endverbatim
+ *
+ * This is a format attribute module for the iLBC codec.
+ * \ingroup codecs
+ */
+
+/*** MODULEINFO
+	 <support_level>core</support_level>
+***/
+
+#include "asterisk.h"
+
+ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
+
+#include "asterisk/module.h"
+#include "asterisk/format.h"
+
+struct ilbc_attr {
+	unsigned char mode;
+};
+
+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_calloc(1, sizeof(*attr));
+
+	if (!attr) {
+		return -1;
+	}
+
+	if (original) {
+		*attr = *original;
+	}
+
+	ast_format_set_attribute_data(dst, attr);
+
+	return 0;
+}
+
+static enum ast_format_cmp_res ilbc_cmp(const struct ast_format *format1, const struct ast_format *format2)
+{
+	struct ilbc_attr *attr1 = ast_format_get_attribute_data(format1);
+	struct ilbc_attr *attr2 = ast_format_get_attribute_data(format2);
+
+	if (!attr1 || !attr2 || attr1->mode == attr2->mode) {
+		return AST_FORMAT_CMP_EQUAL;
+	}
+
+	return AST_FORMAT_CMP_NOT_EQUAL;
+}
+
+static struct ast_format *ilbc_get_joint(const struct ast_format *format1, const struct ast_format *format2)
+{
+	struct ast_format *cloned;
+	struct ilbc_attr *attr, *attr1, *attr2;
+
+	cloned = ast_format_clone(format1);
+	if (!cloned) {
+		return NULL;
+	}
+
+	attr = ast_format_get_attribute_data(cloned);
+
+	attr1 = ast_format_get_attribute_data(format1);
+	attr2 = ast_format_get_attribute_data(format2);
+
+	/*
+	 * From RFC 3952:
+	 *
+	 * The offer contains the preferred mode of the offerer.  The answerer
+	 * may agree to that mode by including the same mode in the answer, or
+	 * may include a different mode.  The resulting mode used by both
+	 * parties SHALL be the lower of the bandwidth modes in the offer and
+	 * answer.
+	 *
+	 * That is, an offer of "mode=20" receiving an answer of "mode=30" will
+	 * result in "mode=30" being used by both participants.  Similarly, an
+	 * offer of "mode=30" and an answer of "mode=20" will result in
+	 * "mode=30" being used by both participants.
+	 */
+
+	attr->mode = attr1->mode > attr2->mode ? attr1->mode : attr2->mode;
+
+	return cloned;
+}
+
+static struct ast_format *ilbc_parse_sdp_fmtp(const struct ast_format *format, const char *attributes)
+{
+	char *attribs = ast_strdupa(attributes), *attrib;
+	struct ast_format *cloned;
+	struct ilbc_attr *attr;
+
+	cloned = ast_format_clone(format);
+	if (!cloned) {
+		return NULL;
+	}
+
+	attr = ast_format_get_attribute_data(cloned);
+
+	while ((attrib = strsep(&attribs, ";"))) {
+		unsigned char val;
+
+		attrib = ast_strip(attrib);
+
+		if (sscanf(attrib, "mode=%hhu", &val) == 1) {
+			if (val == 20 || val == 30) {
+				attr->mode = val;
+			}
+		}
+	}
+
+	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) {
+		return;
+	}
+
+	if (attr->mode) {
+		ast_str_append(str, 0, "a=fmtp:%u mode=%hhu\r\n", payload, attr->mode);
+	}
+
+	return;
+}
+
+static const void *ilbc_attribute_get(const struct ast_format *format, const char *name)
+{
+	struct ilbc_attr *attr = ast_format_get_attribute_data(format);
+	unsigned char *val = NULL;
+
+	if (!attr) {
+		return NULL;
+	}
+
+	if (!strcasecmp(name, "mode")) {
+		val = &attr->mode;
+	} else {
+		ast_log(LOG_WARNING, "Unknown attribute type: %s\n", name);
+	}
+
+	return val;
+}
+
+static struct ast_format *ilbc_attribute_set(const struct ast_format *format,
+	const char *name, const char *value)
+{
+	struct ast_format *cloned;
+	struct ilbc_attr *attr;
+	unsigned char val;
+
+	cloned = ast_format_clone(format);
+	if (!cloned) {
+		return NULL;
+	}
+
+	attr = ast_format_get_attribute_data(cloned);
+
+	if (sscanf(value, "%hhu", &val) != 1) {
+		ast_log(LOG_WARNING, "Unknown value '%s' for attribute type '%s'\n",
+			value, name);
+		return NULL;
+	}
+
+	if (!strcasecmp(name, "mode")) {
+		if (val == 20 || val == 30) {
+			attr->mode = val;
+		} else {
+			ast_log(LOG_NOTICE, "Invalid '%s' specified: %hhu (expected 20 or 30)\n",
+				name, val);
+		}
+	} else {
+		ast_log(LOG_WARNING, "Unknown attribute type: %s\n", name);
+	}
+
+	return cloned;
+}
+
+static struct ast_format_interface ilbc_interface = {
+	.format_destroy = ilbc_destroy,
+	.format_clone = ilbc_clone,
+	.format_cmp = ilbc_cmp,
+	.format_get_joint = ilbc_get_joint,
+	.format_parse_sdp_fmtp = ilbc_parse_sdp_fmtp,
+	.format_generate_sdp_fmtp = ilbc_generate_sdp_fmtp,
+	.format_attribute_get = ilbc_attribute_get,
+	.format_attribute_set = ilbc_attribute_set,
+};
+
+static int unload_module(void)
+{
+	return 0;
+}
+
+static int load_module(void)
+{
+	if (ast_format_interface_register("iLBC", &ilbc_interface)) {
+		return AST_MODULE_LOAD_DECLINE;
+	}
+
+	return AST_MODULE_LOAD_SUCCESS;
+}
+
+AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "iLBC Format Attribute Module",
+	.support_level = AST_MODULE_SUPPORT_CORE,
+	.load = load_module,
+	.unload = unload_module,
+);

-- 
To view, visit https://gerrit.asterisk.org/10781
To unsubscribe, or for help writing mail filters, visit https://gerrit.asterisk.org/settings

Gerrit-Project: asterisk
Gerrit-Branch: 13
Gerrit-MessageType: newchange
Gerrit-Change-Id: I5c5d37984826c6738706eae6b9edb284d564ce2b
Gerrit-Change-Number: 10781
Gerrit-PatchSet: 1
Gerrit-Owner: Sean Bright <sean.bright at gmail.com>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.digium.com/pipermail/asterisk-code-review/attachments/20181211/4d4afa44/attachment-0001.html>


More information about the asterisk-code-review mailing list