| 1 |
#include "asterisk.h" |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
#include <ctype.h> |
| 7 |
#include <math.h> |
| 8 |
|
| 9 |
#include "asterisk/module.h" |
| 10 |
#include "asterisk/format.h" |
| 11 |
#include "asterisk/astobj2.h" |
| 12 |
#include "asterisk/format_cache.h" |
| 13 |
#include "asterisk/logger.h" |
| 14 |
#include "asterisk/strings.h" |
| 15 |
#include "asterisk/utils.h" |
| 16 |
|
| 17 |
#include "asterisk/amr.h" |
| 18 |
|
| 19 |
|
| 20 |
static struct amr_attr default_amr_attr = { |
| 21 |
.octet_align = 0, |
| 22 |
.mode_set = 0, |
| 23 |
.mode_change_period = 0, |
| 24 |
.mode_change_capability = 0, |
| 25 |
.mode_change_neighbor = 0, |
| 26 |
.crc = 0, |
| 27 |
.robust_sorting = 0, |
| 28 |
.interleaving = 0, |
| 29 |
.max_red = -1, |
| 30 |
}; |
| 31 |
|
| 32 |
static void amr_destroy(struct ast_format *format) |
| 33 |
{ |
| 34 |
struct amr_attr *attr = ast_format_get_attribute_data(format); |
| 35 |
|
| 36 |
ast_free(attr); |
| 37 |
} |
| 38 |
|
| 39 |
static int amr_clone(const struct ast_format *src, struct ast_format *dst) |
| 40 |
{ |
| 41 |
struct amr_attr *original = ast_format_get_attribute_data(src); |
| 42 |
struct amr_attr *attr = ast_malloc(sizeof(*attr)); |
| 43 |
|
| 44 |
if (!attr) { |
| 45 |
return -1; |
| 46 |
} |
| 47 |
|
| 48 |
if (original) { |
| 49 |
*attr = *original; |
| 50 |
} else { |
| 51 |
*attr = default_amr_attr; |
| 52 |
|
| 53 |
if (16000 == ast_format_get_sample_rate(src)) { |
| 54 |
attr->mode_current = 8; |
| 55 |
attr->vad = 0; |
| 56 |
} else { |
| 57 |
attr->mode_current = 7; |
| 58 |
attr->vad = 1; |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
ast_format_set_attribute_data(dst, attr); |
| 63 |
|
| 64 |
return 0; |
| 65 |
} |
| 66 |
|
| 67 |
static struct ast_format *amr_parse_sdp_fmtp(const struct ast_format *format, const char *attrib) |
| 68 |
{ |
| 69 |
struct ast_format *cloned; |
| 70 |
struct amr_attr *attr; |
| 71 |
unsigned int val; |
| 72 |
char *attributes; |
| 73 |
char *tmp; |
| 74 |
const unsigned int size = 9; |
| 75 |
int v[size]; |
| 76 |
|
| 77 |
for (val = 0; val < size; val = val + 1) { |
| 78 |
v[val] = -1; |
| 79 |
} |
| 80 |
|
| 81 |
cloned = ast_format_clone(format); |
| 82 |
if (!cloned) { |
| 83 |
return NULL; |
| 84 |
} |
| 85 |
attr = ast_format_get_attribute_data(cloned); |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
attributes = ast_strdupa(attrib); |
| 90 |
for (tmp = attributes; *tmp; ++tmp) { |
| 91 |
*tmp = tolower(*tmp); |
| 92 |
} |
| 93 |
|
| 94 |
attr->octet_align = 0; |
| 95 |
tmp = strstr(attributes, "octet-align="); |
| 96 |
if (tmp) { |
| 97 |
if (sscanf(tmp, "octet-align=%30u", &val) == 1) { |
| 98 |
attr->octet_align = val; |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
attr->mode_set = 0; |
| 103 |
tmp = strstr(attributes, "mode-set="); |
| 104 |
if (tmp) { |
| 105 |
if (sscanf(tmp, "mode-set=%30u,%30u,%30u,%30u,%30u,%30u,%30u,%30u,%30u", |
| 106 |
&v[0], &v[1], &v[2], &v[3], &v[4], &v[5], &v[6], &v[7], &v[8]) > 0) { |
| 107 |
for (val = 0; val < size; val = val + 1) { |
| 108 |
if (0 <= v[val] && v[val] < size) { |
| 109 |
attr->mode_set = (attr->mode_set | (1 << v[val])); |
| 110 |
} |
| 111 |
} |
| 112 |
} |
| 113 |
} |
| 114 |
if (0 < attr->mode_set) { |
| 115 |
attr->mode_current = floor(log10(attr->mode_set) / log10(2)); |
| 116 |
} |
| 117 |
|
| 118 |
attr->mode_change_capability = 0; |
| 119 |
tmp = strstr(attributes, "mode-change-capability="); |
| 120 |
if (tmp) { |
| 121 |
if (sscanf(tmp, "mode-change-capability=%30u", &val) == 1) { |
| 122 |
attr->mode_change_capability = val; |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
attr->mode_change_period = 0; |
| 127 |
tmp = strstr(attributes, "mode-change-period="); |
| 128 |
if (tmp) { |
| 129 |
if (sscanf(tmp, "mode-change-period=%30u", &val) == 1) { |
| 130 |
attr->mode_change_period = val; |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
attr->mode_change_neighbor = 0; |
| 135 |
tmp = strstr(attributes, "mode-change-neighbor="); |
| 136 |
if (tmp) { |
| 137 |
if (sscanf(tmp, "mode-change-neighbor=%30u", &val) == 1) { |
| 138 |
attr->mode_change_neighbor = val; |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
attr->crc = 0; |
| 143 |
tmp = strstr(attributes, "crc="); |
| 144 |
if (tmp) { |
| 145 |
if (sscanf(tmp, "crc=%30u", &val) == 1) { |
| 146 |
attr->crc = val; |
| 147 |
if (attr->crc) { |
| 148 |
attr->octet_align = 1; |
| 149 |
} |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
attr->robust_sorting = 0; |
| 154 |
tmp = strstr(attributes, "robust-sorting="); |
| 155 |
if (tmp) { |
| 156 |
if (sscanf(tmp, "robust-sorting=%30u", &val) == 1) { |
| 157 |
attr->robust_sorting = val; |
| 158 |
if (attr->robust_sorting) { |
| 159 |
attr->octet_align = 1; |
| 160 |
} |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
attr->interleaving = 0; |
| 165 |
tmp = strstr(attributes, "interleaving="); |
| 166 |
if (tmp) { |
| 167 |
if (sscanf(tmp, "interleaving=%30u", &val) == 1) { |
| 168 |
attr->interleaving = val; |
| 169 |
if (attr->interleaving) { |
| 170 |
attr->octet_align = 1; |
| 171 |
} |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
attr->max_red = -1; |
| 176 |
tmp = strstr(attributes, "max-red="); |
| 177 |
if (tmp) { |
| 178 |
if (sscanf(tmp, "max-red=%30u", &val) == 1) { |
| 179 |
attr->max_red = val; |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
return cloned; |
| 184 |
} |
| 185 |
|
| 186 |
static void amr_generate_sdp_fmtp(const struct ast_format *format, unsigned int payload, struct ast_str **str) |
| 187 |
{ |
| 188 |
int appended = 0; |
| 189 |
int listed = 0; |
| 190 |
struct amr_attr *attr = ast_format_get_attribute_data(format); |
| 191 |
|
| 192 |
if (!attr) { |
| 193 |
attr = &default_amr_attr; |
| 194 |
} |
| 195 |
|
| 196 |
if (0 != attr->octet_align) { |
| 197 |
if (0 == appended) { |
| 198 |
ast_str_append(str, 0, "a=fmtp:%d ", payload); |
| 199 |
} else { |
| 200 |
ast_str_append(str, 0, ";"); |
| 201 |
} |
| 202 |
ast_str_append(str, 0, "octet-align=%d", attr->octet_align); |
| 203 |
appended = appended + 1; |
| 204 |
} |
| 205 |
if (0 != attr->mode_set) |
| 206 |
{ |
| 207 |
if (0 == appended) { |
| 208 |
ast_str_append(str, 0, "a=fmtp:%d ", payload); |
| 209 |
} else { |
| 210 |
ast_str_append(str, 0, ";"); |
| 211 |
} |
| 212 |
ast_str_append(str, 0, "mode-set="); |
| 213 |
if (attr->mode_set & 0x01) { |
| 214 |
if (0 == listed) { |
| 215 |
ast_str_append(str, 0, "0"); |
| 216 |
} else { |
| 217 |
ast_str_append(str, 0, ",0"); |
| 218 |
} |
| 219 |
listed = listed + 1; |
| 220 |
} |
| 221 |
if (attr->mode_set & 0x02) { |
| 222 |
if (0 == listed) { |
| 223 |
ast_str_append(str, 0, "1"); |
| 224 |
} else { |
| 225 |
ast_str_append(str, 0, ",1"); |
| 226 |
} |
| 227 |
listed = listed + 1; |
| 228 |
} |
| 229 |
if (attr->mode_set & 0x04) { |
| 230 |
if (0 == listed) { |
| 231 |
ast_str_append(str, 0, "2"); |
| 232 |
} else { |
| 233 |
ast_str_append(str, 0, ",2"); |
| 234 |
} |
| 235 |
listed = listed + 1; |
| 236 |
} |
| 237 |
if (attr->mode_set & 0x08) { |
| 238 |
if (0 == listed) { |
| 239 |
ast_str_append(str, 0, "3"); |
| 240 |
} else { |
| 241 |
ast_str_append(str, 0, ",3"); |
| 242 |
} |
| 243 |
listed = listed + 1; |
| 244 |
} |
| 245 |
if (attr->mode_set & 0x10) { |
| 246 |
if (0 == listed) { |
| 247 |
ast_str_append(str, 0, "4"); |
| 248 |
} else { |
| 249 |
ast_str_append(str, 0, ",4"); |
| 250 |
} |
| 251 |
listed = listed + 1; |
| 252 |
} |
| 253 |
if (attr->mode_set & 0x20) { |
| 254 |
if (0 == listed) { |
| 255 |
ast_str_append(str, 0, "5"); |
| 256 |
} else { |
| 257 |
ast_str_append(str, 0, ",5"); |
| 258 |
} |
| 259 |
listed = listed + 1; |
| 260 |
} |
| 261 |
if (attr->mode_set & 0x40) { |
| 262 |
if (0 == listed) { |
| 263 |
ast_str_append(str, 0, "6"); |
| 264 |
} else { |
| 265 |
ast_str_append(str, 0, ",6"); |
| 266 |
} |
| 267 |
listed = listed + 1; |
| 268 |
} |
| 269 |
if (attr->mode_set & 0x80) { |
| 270 |
if (0 == listed) { |
| 271 |
ast_str_append(str, 0, "7"); |
| 272 |
} else { |
| 273 |
ast_str_append(str, 0, ",7"); |
| 274 |
} |
| 275 |
listed = listed + 1; |
| 276 |
} |
| 277 |
if (attr->mode_set & 0x100) { |
| 278 |
if (0 == listed) { |
| 279 |
ast_str_append(str, 0, "8"); |
| 280 |
} else { |
| 281 |
ast_str_append(str, 0, ",8"); |
| 282 |
} |
| 283 |
listed = listed + 1; |
| 284 |
} |
| 285 |
appended = appended + 1; |
| 286 |
} |
| 287 |
if (0 != attr->mode_change_capability) { |
| 288 |
if (0 == appended) { |
| 289 |
ast_str_append(str, 0, "a=fmtp:%d ", payload); |
| 290 |
} else { |
| 291 |
ast_str_append(str, 0, ";"); |
| 292 |
} |
| 293 |
ast_str_append(str, 0, "mode-change-capability=%d", attr->mode_change_capability); |
| 294 |
appended = appended + 1; |
| 295 |
} |
| 296 |
if (0 != attr->mode_change_period) { |
| 297 |
if (0 == appended) { |
| 298 |
ast_str_append(str, 0, "a=fmtp:%d ", payload); |
| 299 |
} else { |
| 300 |
ast_str_append(str, 0, ";"); |
| 301 |
} |
| 302 |
ast_str_append(str, 0, "mode-change-period=%d", attr->mode_change_period); |
| 303 |
appended = appended + 1; |
| 304 |
} |
| 305 |
if (0 != attr->mode_change_neighbor) { |
| 306 |
if (0 == appended) { |
| 307 |
ast_str_append(str, 0, "a=fmtp:%d ", payload); |
| 308 |
} else { |
| 309 |
ast_str_append(str, 0, ";"); |
| 310 |
} |
| 311 |
ast_str_append(str, 0, "mode-change-neighbor=%d", attr->mode_change_neighbor); |
| 312 |
appended = appended + 1; |
| 313 |
} |
| 314 |
if (0 != attr->crc) { |
| 315 |
if (0 == appended) { |
| 316 |
ast_str_append(str, 0, "a=fmtp:%d ", payload); |
| 317 |
} else { |
| 318 |
ast_str_append(str, 0, ";"); |
| 319 |
} |
| 320 |
ast_str_append(str, 0, "crc=%d", attr->crc); |
| 321 |
appended = appended + 1; |
| 322 |
} |
| 323 |
if (0 != attr->robust_sorting) { |
| 324 |
if (0 == appended) { |
| 325 |
ast_str_append(str, 0, "a=fmtp:%d ", payload); |
| 326 |
} else { |
| 327 |
ast_str_append(str, 0, ";"); |
| 328 |
} |
| 329 |
ast_str_append(str, 0, "robust-sorting=%d", attr->robust_sorting); |
| 330 |
appended = appended + 1; |
| 331 |
} |
| 332 |
if (0 != attr->interleaving) { |
| 333 |
if (0 == appended) { |
| 334 |
ast_str_append(str, 0, "a=fmtp:%d ", payload); |
| 335 |
} else { |
| 336 |
ast_str_append(str, 0, ";"); |
| 337 |
} |
| 338 |
ast_str_append(str, 0, "interleaving=%d", attr->interleaving); |
| 339 |
appended = appended + 1; |
| 340 |
} |
| 341 |
if (0 <= attr->max_red) { |
| 342 |
if (0 == appended) { |
| 343 |
ast_str_append(str, 0, "a=fmtp:%d ", payload); |
| 344 |
} else { |
| 345 |
ast_str_append(str, 0, ";"); |
| 346 |
} |
| 347 |
ast_str_append(str, 0, "max-red=%d", attr->max_red); |
| 348 |
appended = appended + 1; |
| 349 |
} |
| 350 |
if (0 != appended) { |
| 351 |
ast_str_append(str, 0, "\r\n"); |
| 352 |
} |
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
} |
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
static struct ast_format *amr_getjoint(const struct ast_format *format1, const struct ast_format *format2) |
| 389 |
{ |
| 390 |
struct amr_attr *attr1 = ast_format_get_attribute_data(format1); |
| 391 |
struct amr_attr *attr2 = ast_format_get_attribute_data(format2); |
| 392 |
struct amr_attr *attr_res; |
| 393 |
struct ast_format *jointformat = NULL; |
| 394 |
|
| 395 |
if (!attr1) { |
| 396 |
attr1 = &default_amr_attr; |
| 397 |
} |
| 398 |
|
| 399 |
if (!attr2) { |
| 400 |
attr2 = &default_amr_attr; |
| 401 |
} |
| 402 |
|
| 403 |
if (format1 == ast_format_amrwb || format1 == ast_format_amr) { |
| 404 |
jointformat = (struct ast_format *) format2; |
| 405 |
} |
| 406 |
if (format2 == ast_format_amrwb || format2 == ast_format_amr) { |
| 407 |
jointformat = (struct ast_format *) format1; |
| 408 |
} |
| 409 |
if (format1 == format2) { |
| 410 |
if (!jointformat) { |
| 411 |
ast_debug(3, "Both formats were not cached but the same.\n"); |
| 412 |
jointformat = (struct ast_format *) format1; |
| 413 |
} else { |
| 414 |
ast_debug(3, "Both formats were cached.\n"); |
| 415 |
jointformat = NULL; |
| 416 |
} |
| 417 |
} |
| 418 |
if (!jointformat) { |
| 419 |
ast_debug(3, "Which pointer shall be returned? Let us create a new one!\n"); |
| 420 |
jointformat = ast_format_clone(format1); |
| 421 |
} else { |
| 422 |
ao2_bump(jointformat); |
| 423 |
} |
| 424 |
if (!jointformat) { |
| 425 |
return NULL; |
| 426 |
} |
| 427 |
attr_res = ast_format_get_attribute_data(jointformat); |
| 428 |
|
| 429 |
if (0 == attr1->mode_set && 0 == attr2->mode_set) { |
| 430 |
attr_res->mode_set = 0; |
| 431 |
} else if (0 != attr1->mode_set && 0 == attr2->mode_set) { |
| 432 |
attr_res->mode_set = attr1->mode_set; |
| 433 |
} else if (0 == attr1->mode_set && 0 != attr2->mode_set) { |
| 434 |
attr_res->mode_set = attr2->mode_set; |
| 435 |
} else { |
| 436 |
attr_res->mode_set = (attr1->mode_set & attr2->mode_set); |
| 437 |
if (0 == attr_res->mode_set) { |
| 438 |
|
| 439 |
ast_log(LOG_WARNING, "mode-set did not match\n"); |
| 440 |
return NULL; |
| 441 |
} |
| 442 |
} |
| 443 |
attr_res->mode_change_period = MAX(attr1->mode_change_period, attr2->mode_change_period); |
| 444 |
attr_res->mode_change_capability = MAX(attr1->mode_change_capability, attr2->mode_change_capability); |
| 445 |
attr_res->mode_change_neighbor = MAX(attr1->mode_change_neighbor, attr2->mode_change_neighbor); |
| 446 |
attr_res->crc = MAX(attr1->crc, attr2->crc); |
| 447 |
attr_res->robust_sorting = MAX(attr1->robust_sorting, attr2->robust_sorting); |
| 448 |
attr_res->interleaving = MAX(attr1->interleaving, attr2->interleaving); |
| 449 |
attr_res->max_red = MAX(attr1->max_red, attr2->max_red); |
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
if (0 < attr_res->mode_set) { |
| 454 |
attr_res->mode_current = floor(log10(attr_res->mode_set) / log10(2)); |
| 455 |
} |
| 456 |
attr_res->vad = MAX(attr1->vad, attr2->vad); |
| 457 |
|
| 458 |
return jointformat; |
| 459 |
} |
| 460 |
|
| 461 |
static struct ast_format_interface amr_interface = { |
| 462 |
.format_destroy = amr_destroy, |
| 463 |
.format_clone = amr_clone, |
| 464 |
.format_cmp = NULL, |
| 465 |
.format_get_joint = amr_getjoint, |
| 466 |
.format_attribute_set = NULL, |
| 467 |
.format_parse_sdp_fmtp = amr_parse_sdp_fmtp, |
| 468 |
.format_generate_sdp_fmtp = amr_generate_sdp_fmtp, |
| 469 |
}; |
| 470 |
|
| 471 |
static int load_module(void) |
| 472 |
{ |
| 473 |
if (ast_format_interface_register("amr", &amr_interface)) { |
| 474 |
return AST_MODULE_LOAD_DECLINE; |
| 475 |
} |
| 476 |
|
| 477 |
if (ast_format_interface_register("amrwb", &amr_interface)) { |
| 478 |
return AST_MODULE_LOAD_DECLINE; |
| 479 |
} |
| 480 |
|
| 481 |
return AST_MODULE_LOAD_SUCCESS; |
| 482 |
} |
| 483 |
|
| 484 |
static int unload_module(void) |
| 485 |
{ |
| 486 |
return 0; |
| 487 |
} |
| 488 |
|
| 489 |
AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, |
| 490 |
"AMR Format Attribute Module", |
| 491 |
.load = load_module, |
| 492 |
.unload = unload_module, |
| 493 |
.load_pri = AST_MODPRI_CHANNEL_DEPEND, |
| 494 |
); |