res_format_attr_opus: Update to latest RFC 7587.

Beside that, the format-attribute module sends only non-default values in the
line fmtp, now. This avoids unnecessary overhead in SDP messages. Furthermore,
previously the parameter stereo was not parsed when being the first parameter.

ASTERISK-25583 #close

Change-Id: Iae85ba3e5960bfd5d51cf65bcffad00dd4875a73
changes/74/1674/5
Alexander Traud 10 years ago
parent 6fcd361540
commit 3e2178c05e

@ -33,28 +33,36 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
#include "asterisk/module.h" #include "asterisk/module.h"
#include "asterisk/format.h" #include "asterisk/format.h"
#include "asterisk/logger.h" /* for ast_log, LOG_WARNING */
#include "asterisk/strings.h" /* for ast_str_append */
#include "asterisk/utils.h" /* for MIN, ast_malloc, ast_free */
/*! /*!
* \brief Opus attribute structure. * \brief Opus attribute structure.
* *
* \note http://tools.ietf.org/html/draft-ietf-payload-rtp-opus-00. * \note http://tools.ietf.org/html/rfc7587#section-6
*/ */
struct opus_attr { struct opus_attr {
unsigned int maxbitrate; /* Default 64-128 kb/s for FB stereo music */ unsigned int maxbitrate;
unsigned int maxplayrate /* Default 48000 */; unsigned int maxplayrate;
unsigned int minptime; /* Default 3, but it's 10 in format.c */ unsigned int unused; /* was minptime, kept for binary compatibility */
unsigned int stereo; /* Default 0 */ unsigned int stereo;
unsigned int cbr; /* Default 0 */ unsigned int cbr;
unsigned int fec; /* Default 0 */ unsigned int fec;
unsigned int dtx; /* Default 0 */ unsigned int dtx;
unsigned int spropmaxcapturerate; /* Default 48000 */ unsigned int spropmaxcapturerate;
unsigned int spropstereo; /* Default 0 */ unsigned int spropstereo;
}; };
static struct opus_attr default_opus_attr = { static struct opus_attr default_opus_attr = {
.fec = 0, .maxplayrate = 48000,
.dtx = 0, .spropmaxcapturerate = 48000,
.stereo = 0, .maxbitrate = 510000,
.stereo = 0,
.spropstereo = 0,
.cbr = 0,
.fec = 1,
.dtx = 0,
}; };
static void opus_destroy(struct ast_format *format) static void opus_destroy(struct ast_format *format)
@ -67,7 +75,7 @@ static void opus_destroy(struct ast_format *format)
static int opus_clone(const struct ast_format *src, struct ast_format *dst) static int opus_clone(const struct ast_format *src, struct ast_format *dst)
{ {
struct opus_attr *original = ast_format_get_attribute_data(src); struct opus_attr *original = ast_format_get_attribute_data(src);
struct opus_attr *attr = ast_calloc(1, sizeof(*attr)); struct opus_attr *attr = ast_malloc(sizeof(*attr));
if (!attr) { if (!attr) {
return -1; return -1;
@ -75,6 +83,8 @@ static int opus_clone(const struct ast_format *src, struct ast_format *dst)
if (original) { if (original) {
*attr = *original; *attr = *original;
} else {
*attr = default_opus_attr;
} }
ast_format_set_attribute_data(dst, attr); ast_format_set_attribute_data(dst, attr);
@ -97,33 +107,54 @@ static struct ast_format *opus_parse_sdp_fmtp(const struct ast_format *format, c
if ((kvp = strstr(attributes, "maxplaybackrate")) && sscanf(kvp, "maxplaybackrate=%30u", &val) == 1) { if ((kvp = strstr(attributes, "maxplaybackrate")) && sscanf(kvp, "maxplaybackrate=%30u", &val) == 1) {
attr->maxplayrate = val; attr->maxplayrate = val;
} else {
attr->maxplayrate = 48000;
} }
if ((kvp = strstr(attributes, "sprop-maxcapturerate")) && sscanf(kvp, "sprop-maxcapturerate=%30u", &val) == 1) { if ((kvp = strstr(attributes, "sprop-maxcapturerate")) && sscanf(kvp, "sprop-maxcapturerate=%30u", &val) == 1) {
attr->spropmaxcapturerate = val; attr->spropmaxcapturerate = val;
} else {
attr->spropmaxcapturerate = 48000;
} }
if ((kvp = strstr(attributes, "minptime")) && sscanf(kvp, "minptime=%30u", &val) == 1) {
attr->minptime = val;
}
if ((kvp = strstr(attributes, "maxaveragebitrate")) && sscanf(kvp, "maxaveragebitrate=%30u", &val) == 1) { if ((kvp = strstr(attributes, "maxaveragebitrate")) && sscanf(kvp, "maxaveragebitrate=%30u", &val) == 1) {
attr->maxbitrate = val; attr->maxbitrate = val;
} else {
attr->maxbitrate = 510000;
} }
if ((kvp = strstr(attributes, " stereo")) && sscanf(kvp, " stereo=%30u", &val) == 1) {
attr->stereo = val; if (!strncmp(attributes, "stereo=1", 8)) {
} attr->stereo = 1;
if ((kvp = strstr(attributes, ";stereo")) && sscanf(kvp, ";stereo=%30u", &val) == 1) { } else if (strstr(attributes, " stereo=1")) {
attr->stereo = val; attr->stereo = 1;
} else if (strstr(attributes, ";stereo=1")) {
attr->stereo = 1;
} else {
attr->stereo = 0;
} }
if ((kvp = strstr(attributes, "sprop-stereo")) && sscanf(kvp, "sprop-stereo=%30u", &val) == 1) {
attr->spropstereo = val; if (strstr(attributes, "sprop-stereo=1")) {
attr->spropstereo = 1;
} else {
attr->spropstereo = 0;
} }
if ((kvp = strstr(attributes, "cbr")) && sscanf(kvp, "cbr=%30u", &val) == 1) {
attr->cbr = val; if (strstr(attributes, "cbr=1")) {
attr->cbr = 1;
} else {
attr->cbr = 0;
} }
if ((kvp = strstr(attributes, "useinbandfec")) && sscanf(kvp, "useinbandfec=%30u", &val) == 1) {
attr->fec = val; if (strstr(attributes, "useinbandfec=1")) {
attr->fec = 1;
} else {
attr->fec = 0;
} }
if ((kvp = strstr(attributes, "usedtx")) && sscanf(kvp, "usedtx=%30u", &val) == 1) {
attr->dtx = val; if (strstr(attributes, "usedtx=1")) {
attr->dtx = 1;
} else {
attr->dtx = 0;
} }
return cloned; return cloned;
@ -132,34 +163,92 @@ static struct ast_format *opus_parse_sdp_fmtp(const struct ast_format *format, c
static void opus_generate_sdp_fmtp(const struct ast_format *format, unsigned int payload, struct ast_str **str) static void opus_generate_sdp_fmtp(const struct ast_format *format, unsigned int payload, struct ast_str **str)
{ {
struct opus_attr *attr = ast_format_get_attribute_data(format); struct opus_attr *attr = ast_format_get_attribute_data(format);
int added = 0;
if (!attr) { if (!attr) {
return; /*
* (Only) cached formats do not have attribute data assigned because
* they were created before this attribute module was registered.
* Therefore, we assume the default attribute values here.
*/
attr = &default_opus_attr;
}
if (48000 != attr->maxplayrate) {
if (added) {
ast_str_append(str, 0, ";");
} else if (0 < ast_str_append(str, 0, "a=fmtp:%u ", payload)) {
added = 1;
}
ast_str_append(str, 0, "maxplaybackrate=%u", attr->maxplayrate);
}
if (48000 != attr->spropmaxcapturerate) {
if (added) {
ast_str_append(str, 0, ";");
} else if (0 < ast_str_append(str, 0, "a=fmtp:%u ", payload)) {
added = 1;
}
ast_str_append(str, 0, "sprop-maxcapturerate=%u", attr->spropmaxcapturerate);
}
if (510000 != attr->maxbitrate) {
if (added) {
ast_str_append(str, 0, ";");
} else if (0 < ast_str_append(str, 0, "a=fmtp:%u ", payload)) {
added = 1;
}
ast_str_append(str, 0, "maxaveragebitrate=%u", attr->maxbitrate);
}
if (0 != attr->stereo) {
if (added) {
ast_str_append(str, 0, ";");
} else if (0 < ast_str_append(str, 0, "a=fmtp:%u ", payload)) {
added = 1;
}
ast_str_append(str, 0, "stereo=%u", attr->stereo);
} }
/* FIXME should we only generate attributes that were explicitly set? */ if (0 != attr->spropstereo) {
ast_str_append(str, 0, if (added) {
"a=fmtp:%u " ast_str_append(str, 0, ";");
"maxplaybackrate=%u;" } else if (0 < ast_str_append(str, 0, "a=fmtp:%u ", payload)) {
"sprop-maxcapturerate=%u;" added = 1;
"minptime=%u;" }
"maxaveragebitrate=%u;" ast_str_append(str, 0, "sprop-stereo=%u", attr->spropstereo);
"stereo=%d;" }
"sprop-stereo=%d;"
"cbr=%d;" if (0 != attr->cbr) {
"useinbandfec=%d;" if (added) {
"usedtx=%d\r\n", ast_str_append(str, 0, ";");
payload, } else if (0 < ast_str_append(str, 0, "a=fmtp:%u ", payload)) {
attr->maxplayrate ? attr->maxplayrate : 48000, /* maxplaybackrate */ added = 1;
attr->spropmaxcapturerate ? attr->spropmaxcapturerate : 48000, /* sprop-maxcapturerate */ }
attr->minptime > 10 ? attr->minptime : 10, /* minptime */ ast_str_append(str, 0, "cbr=%u", attr->cbr);
attr->maxbitrate ? attr->maxbitrate : 20000, /* maxaveragebitrate */ }
attr->stereo ? 1 : 0, /* stereo */
attr->spropstereo ? 1 : 0, /* sprop-stereo */ if (0 != attr->fec) {
attr->cbr ? 1 : 0, /* cbr */ if (added) {
attr->fec ? 1 : 0, /* useinbandfec */ ast_str_append(str, 0, ";");
attr->dtx ? 1 : 0 /* usedtx */ } else if (0 < ast_str_append(str, 0, "a=fmtp:%u ", payload)) {
); added = 1;
}
ast_str_append(str, 0, "useinbandfec=%u", attr->fec);
}
if (0 != attr->dtx) {
if (added) {
ast_str_append(str, 0, ";");
} else if (0 < ast_str_append(str, 0, "a=fmtp:%u ", payload)) {
added = 1;
}
ast_str_append(str, 0, "usedtx=%u", attr->dtx);
}
if (added) {
ast_str_append(str, 0, "\r\n");
}
} }
static struct ast_format *opus_getjoint(const struct ast_format *format1, const struct ast_format *format2) static struct ast_format *opus_getjoint(const struct ast_format *format1, const struct ast_format *format2)
@ -183,19 +272,22 @@ static struct ast_format *opus_getjoint(const struct ast_format *format1, const
} }
attr_res = ast_format_get_attribute_data(jointformat); attr_res = ast_format_get_attribute_data(jointformat);
/* Only do dtx if both sides want it. DTX is a trade off between attr_res->dtx = attr1->dtx || attr2->dtx ? 1 : 0;
* computational complexity and bandwidth. */
attr_res->dtx = attr1->dtx && attr2->dtx ? 1 : 0;
/* Only do FEC if both sides want it. If a peer specifically requests not /* Only do FEC if both sides want it. If a peer specifically requests not
* to receive with FEC, it may be a waste of bandwidth. */ * to receive with FEC, it may be a waste of bandwidth. */
attr_res->fec = attr1->fec && attr2->fec ? 1 : 0; attr_res->fec = attr1->fec && attr2->fec ? 1 : 0;
attr_res->cbr = attr1->cbr || attr2->cbr ? 1 : 0;
attr_res->spropstereo = attr1->spropstereo || attr2->spropstereo ? 1 : 0;
/* Only do stereo if both sides want it. If a peer specifically requests not /* Only do stereo if both sides want it. If a peer specifically requests not
* to receive stereo signals, it may be a waste of bandwidth. */ * to receive stereo signals, it may be a waste of bandwidth. */
attr_res->stereo = attr1->stereo && attr2->stereo ? 1 : 0; attr_res->stereo = attr1->stereo && attr2->stereo ? 1 : 0;
/* FIXME: do we need to join other attributes as well, e.g., minptime, cbr, etc.? */ attr_res->maxbitrate = MIN(attr1->maxbitrate, attr2->maxbitrate);
attr_res->spropmaxcapturerate = MIN(attr1->spropmaxcapturerate, attr2->spropmaxcapturerate);
attr_res->maxplayrate = MIN(attr1->maxplayrate, attr2->maxplayrate);
return jointformat; return jointformat;
} }
@ -223,7 +315,7 @@ static struct ast_format *opus_set(const struct ast_format *format, const char *
} else if (!strcasecmp(name, "max_playrate")) { } else if (!strcasecmp(name, "max_playrate")) {
attr->maxplayrate = val; attr->maxplayrate = val;
} else if (!strcasecmp(name, "minptime")) { } else if (!strcasecmp(name, "minptime")) {
attr->minptime = val; attr->unused = val;
} else if (!strcasecmp(name, "stereo")) { } else if (!strcasecmp(name, "stereo")) {
attr->stereo = val; attr->stereo = val;
} else if (!strcasecmp(name, "cbr")) { } else if (!strcasecmp(name, "cbr")) {

Loading…
Cancel
Save