| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
#include "asterisk.h" |
| 31 |
|
| 32 |
#include <ctype.h> |
| 33 |
|
| 34 |
#include "asterisk/module.h" |
| 35 |
#include "asterisk/format.h" |
| 36 |
#include "asterisk/logger.h" |
| 37 |
#include "asterisk/strings.h" |
| 38 |
#include "asterisk/utils.h" |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
struct silk_attr { |
| 46 |
unsigned int maxbitrate; |
| 47 |
unsigned int dtx; |
| 48 |
unsigned int fec; |
| 49 |
unsigned int packetloss_percentage; |
| 50 |
}; |
| 51 |
|
| 52 |
static void silk_destroy(struct ast_format *format) |
| 53 |
{ |
| 54 |
struct silk_attr *attr = ast_format_get_attribute_data(format); |
| 55 |
|
| 56 |
ast_free(attr); |
| 57 |
} |
| 58 |
|
| 59 |
static void attr_init(struct silk_attr *attr) |
| 60 |
{ |
| 61 |
memset(attr, 0, sizeof(*attr)); |
| 62 |
} |
| 63 |
|
| 64 |
static int silk_clone(const struct ast_format *src, struct ast_format *dst) |
| 65 |
{ |
| 66 |
struct silk_attr *original = ast_format_get_attribute_data(src); |
| 67 |
struct silk_attr *attr = ast_malloc(sizeof(*attr)); |
| 68 |
|
| 69 |
if (!attr) { |
| 70 |
return -1; |
| 71 |
} |
| 72 |
|
| 73 |
if (original) { |
| 74 |
*attr = *original; |
| 75 |
} else { |
| 76 |
attr_init(attr); |
| 77 |
} |
| 78 |
|
| 79 |
ast_format_set_attribute_data(dst, attr); |
| 80 |
|
| 81 |
return 0; |
| 82 |
} |
| 83 |
|
| 84 |
static struct ast_format *silk_parse_sdp_fmtp(const struct ast_format *format, const char *attributes) |
| 85 |
{ |
| 86 |
char *attribs = ast_strdupa(attributes), *attrib; |
| 87 |
struct ast_format *cloned; |
| 88 |
struct silk_attr *attr; |
| 89 |
unsigned int val; |
| 90 |
|
| 91 |
cloned = ast_format_clone(format); |
| 92 |
if (!cloned) { |
| 93 |
return NULL; |
| 94 |
} |
| 95 |
attr = ast_format_get_attribute_data(cloned); |
| 96 |
|
| 97 |
|
| 98 |
for (attrib = attribs; *attrib; ++attrib) { |
| 99 |
*attrib = tolower(*attrib); |
| 100 |
} |
| 101 |
|
| 102 |
if (sscanf(attribs, "maxaveragebitrate=%30u", &val) == 1) { |
| 103 |
attr->maxbitrate = val; |
| 104 |
} |
| 105 |
if (sscanf(attribs, "usedtx=%30u", &val) == 1) { |
| 106 |
attr->dtx = val; |
| 107 |
} |
| 108 |
if (sscanf(attribs, "useinbandfec=%30u", &val) == 1) { |
| 109 |
attr->fec = val; |
| 110 |
} |
| 111 |
|
| 112 |
return cloned; |
| 113 |
} |
| 114 |
|
| 115 |
static void silk_generate_sdp_fmtp(const struct ast_format *format, unsigned int payload, struct ast_str **str) |
| 116 |
{ |
| 117 |
struct silk_attr *attr = ast_format_get_attribute_data(format); |
| 118 |
|
| 119 |
if (!attr) { |
| 120 |
return; |
| 121 |
} |
| 122 |
|
| 123 |
if ((attr->maxbitrate > 5000) && (attr->maxbitrate < 40000)) { |
| 124 |
ast_str_append(str, 0, "a=fmtp:%u maxaveragebitrate=%u\r\n", payload, attr->maxbitrate); |
| 125 |
} |
| 126 |
|
| 127 |
if (attr->dtx) { |
| 128 |
ast_str_append(str, 0, "a=fmtp:%u usedtx=%u\r\n", payload, attr->dtx); |
| 129 |
} |
| 130 |
if (attr->fec) { |
| 131 |
ast_str_append(str, 0, "a=fmtp:%u useinbandfec=%u\r\n", payload, attr->fec); |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
static enum ast_format_cmp_res silk_cmp(const struct ast_format *format1, const struct ast_format *format2) |
| 136 |
{ |
| 137 |
if (ast_format_get_sample_rate(format1) == ast_format_get_sample_rate(format2)) { |
| 138 |
return AST_FORMAT_CMP_EQUAL; |
| 139 |
} |
| 140 |
|
| 141 |
return AST_FORMAT_CMP_NOT_EQUAL; |
| 142 |
} |
| 143 |
|
| 144 |
static struct ast_format *silk_getjoint(const struct ast_format *format1, const struct ast_format *format2) |
| 145 |
{ |
| 146 |
struct silk_attr *attr1 = ast_format_get_attribute_data(format1); |
| 147 |
struct silk_attr *attr2 = ast_format_get_attribute_data(format2); |
| 148 |
struct ast_format *jointformat; |
| 149 |
struct silk_attr *attr_res; |
| 150 |
|
| 151 |
if (ast_format_get_sample_rate(format1) != ast_format_get_sample_rate(format2)) { |
| 152 |
return NULL; |
| 153 |
} |
| 154 |
|
| 155 |
jointformat = ast_format_clone(format1); |
| 156 |
if (!jointformat) { |
| 157 |
return NULL; |
| 158 |
} |
| 159 |
attr_res = ast_format_get_attribute_data(jointformat); |
| 160 |
|
| 161 |
if (!attr1 || !attr2) { |
| 162 |
attr_init(attr_res); |
| 163 |
} else { |
| 164 |
|
| 165 |
attr_res->maxbitrate = MIN(attr1->maxbitrate, attr2->maxbitrate); |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
attr_res->dtx = attr1->dtx && attr2->dtx ? 1 : 0; |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
attr_res->fec = attr1->fec && attr2->fec ? 1 : 0; |
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
attr_res->packetloss_percentage = MAX(attr1->packetloss_percentage, attr2->packetloss_percentage); |
| 178 |
} |
| 179 |
|
| 180 |
return jointformat; |
| 181 |
} |
| 182 |
|
| 183 |
static struct ast_format *silk_set(const struct ast_format *format, const char *name, const char *value) |
| 184 |
{ |
| 185 |
struct ast_format *cloned; |
| 186 |
struct silk_attr *attr; |
| 187 |
unsigned int val; |
| 188 |
|
| 189 |
if (sscanf(value, "%30u", &val) != 1) { |
| 190 |
ast_log(LOG_WARNING, "Unknown value '%s' for attribute type '%s'\n", |
| 191 |
value, name); |
| 192 |
return NULL; |
| 193 |
} |
| 194 |
|
| 195 |
cloned = ast_format_clone(format); |
| 196 |
if (!cloned) { |
| 197 |
return NULL; |
| 198 |
} |
| 199 |
attr = ast_format_get_attribute_data(cloned); |
| 200 |
|
| 201 |
if (!strcasecmp(name, "max_bitrate")) { |
| 202 |
attr->maxbitrate = val; |
| 203 |
} else if (!strcasecmp(name, "dtx")) { |
| 204 |
attr->dtx = val; |
| 205 |
} else if (!strcasecmp(name, "fec")) { |
| 206 |
attr->fec = val; |
| 207 |
} else if (!strcasecmp(name, "packetloss_percentage")) { |
| 208 |
attr->packetloss_percentage = val; |
| 209 |
} else { |
| 210 |
ast_log(LOG_WARNING, "unknown attribute type %s\n", name); |
| 211 |
} |
| 212 |
|
| 213 |
return cloned; |
| 214 |
} |
| 215 |
|
| 216 |
static const void *silk_get(const struct ast_format *format, const char *name) |
| 217 |
{ |
| 218 |
struct silk_attr *attr = ast_format_get_attribute_data(format); |
| 219 |
unsigned int *val; |
| 220 |
|
| 221 |
if (!strcasecmp(name, "max_bitrate")) { |
| 222 |
val = &attr->maxbitrate; |
| 223 |
} else if (!strcasecmp(name, "dtx")) { |
| 224 |
val = &attr->dtx; |
| 225 |
} else if (!strcasecmp(name, "fec")) { |
| 226 |
val = &attr->fec; |
| 227 |
} else if (!strcasecmp(name, "packetloss_percentage")) { |
| 228 |
val = &attr->packetloss_percentage; |
| 229 |
} else { |
| 230 |
ast_log(LOG_WARNING, "unknown attribute type %s\n", name); |
| 231 |
return NULL; |
| 232 |
} |
| 233 |
|
| 234 |
return val; |
| 235 |
} |
| 236 |
|
| 237 |
static struct ast_format_interface silk_interface = { |
| 238 |
.format_destroy = silk_destroy, |
| 239 |
.format_clone = silk_clone, |
| 240 |
.format_cmp = silk_cmp, |
| 241 |
.format_get_joint = silk_getjoint, |
| 242 |
.format_attribute_set = silk_set, |
| 243 |
.format_attribute_get = silk_get, |
| 244 |
.format_parse_sdp_fmtp = silk_parse_sdp_fmtp, |
| 245 |
.format_generate_sdp_fmtp = silk_generate_sdp_fmtp, |
| 246 |
}; |
| 247 |
|
| 248 |
static int load_module(void) |
| 249 |
{ |
| 250 |
if (ast_format_interface_register("silk", &silk_interface)) { |
| 251 |
return AST_MODULE_LOAD_DECLINE; |
| 252 |
} |
| 253 |
|
| 254 |
return AST_MODULE_LOAD_SUCCESS; |
| 255 |
} |
| 256 |
|
| 257 |
static int unload_module(void) |
| 258 |
{ |
| 259 |
return 0; |
| 260 |
} |
| 261 |
|
| 262 |
AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "SILK Format Attribute Module", |
| 263 |
.support_level = AST_MODULE_SUPPORT_CORE, |
| 264 |
.load = load_module, |
| 265 |
.unload = unload_module, |
| 266 |
.load_pri = AST_MODPRI_CHANNEL_DEPEND, |
| 267 |
); |