| 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 "asterisk/logger.h" |
| 33 |
#include "asterisk/astobj2.h" |
| 34 |
#include "asterisk/codec.h" |
| 35 |
#include "asterisk/format.h" |
| 36 |
#include "asterisk/format_compatibility.h" |
| 37 |
#include "asterisk/format_cache.h" |
| 38 |
#include "asterisk/format_cap.h" |
| 39 |
#include "asterisk/utils.h" |
| 40 |
|
| 41 |
#include "include/codec_pref.h" |
| 42 |
#include "include/format_compatibility.h" |
| 43 |
|
| 44 |
void iax2_codec_pref_convert(struct iax2_codec_pref *pref, char *buf, size_t size, int right) |
| 45 |
{ |
| 46 |
static int differential = (int) 'A'; |
| 47 |
int x; |
| 48 |
|
| 49 |
if (right) { |
| 50 |
--size; |
| 51 |
for (x = 0; x < ARRAY_LEN(pref->order) && x < size; ++x) { |
| 52 |
if (!pref->order[x]) { |
| 53 |
break; |
| 54 |
} |
| 55 |
|
| 56 |
buf[x] = pref->order[x] + differential; |
| 57 |
} |
| 58 |
|
| 59 |
buf[x] = '\0'; |
| 60 |
} else { |
| 61 |
for (x = 0; x < ARRAY_LEN(pref->order) && x < size; ++x) { |
| 62 |
if (buf[x] == '\0') { |
| 63 |
break; |
| 64 |
} |
| 65 |
|
| 66 |
pref->order[x] = buf[x] - differential; |
| 67 |
pref->framing[x] = 0; |
| 68 |
} |
| 69 |
|
| 70 |
if (x < ARRAY_LEN(pref->order)) { |
| 71 |
pref->order[x] = 0; |
| 72 |
pref->framing[x] = 0; |
| 73 |
} |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
struct ast_format *iax2_codec_pref_index(struct iax2_codec_pref *pref, int idx, struct ast_format **result) |
| 78 |
{ |
| 79 |
if (0 <= idx && idx < ARRAY_LEN(pref->order) && pref->order[idx]) { |
| 80 |
uint64_t pref_bitfield; |
| 81 |
|
| 82 |
pref_bitfield = iax2_codec_pref_order_value_to_format_bitfield(pref->order[idx]); |
| 83 |
*result = ast_format_compatibility_bitfield2format(pref_bitfield); |
| 84 |
} else { |
| 85 |
*result = NULL; |
| 86 |
} |
| 87 |
|
| 88 |
return *result; |
| 89 |
} |
| 90 |
|
| 91 |
int iax2_codec_pref_to_cap(struct iax2_codec_pref *pref, struct ast_format_cap *cap) |
| 92 |
{ |
| 93 |
int idx; |
| 94 |
|
| 95 |
for (idx = 0; idx < ARRAY_LEN(pref->order); ++idx) { |
| 96 |
uint64_t pref_bitfield; |
| 97 |
struct ast_format *pref_format; |
| 98 |
|
| 99 |
pref_bitfield = iax2_codec_pref_order_value_to_format_bitfield(pref->order[idx]); |
| 100 |
if (!pref_bitfield) { |
| 101 |
break; |
| 102 |
} |
| 103 |
|
| 104 |
pref_format = ast_format_compatibility_bitfield2format(pref_bitfield); |
| 105 |
if (pref_format && ast_format_cap_append(cap, pref_format, pref->framing[idx])) { |
| 106 |
return -1; |
| 107 |
} |
| 108 |
} |
| 109 |
return 0; |
| 110 |
} |
| 111 |
|
| 112 |
int iax2_codec_pref_best_bitfield2cap(uint64_t bitfield, struct iax2_codec_pref *prefs, struct ast_format_cap *cap) |
| 113 |
{ |
| 114 |
uint64_t best_bitfield; |
| 115 |
struct ast_format *format; |
| 116 |
|
| 117 |
|
| 118 |
if (prefs) { |
| 119 |
int idx; |
| 120 |
|
| 121 |
for (idx = 0; bitfield && idx < ARRAY_LEN(prefs->order); ++idx) { |
| 122 |
best_bitfield = iax2_codec_pref_order_value_to_format_bitfield(prefs->order[idx]); |
| 123 |
if (!best_bitfield) { |
| 124 |
break; |
| 125 |
} |
| 126 |
|
| 127 |
if (best_bitfield & bitfield) { |
| 128 |
format = ast_format_compatibility_bitfield2format(best_bitfield); |
| 129 |
if (format && ast_format_cap_append(cap, format, prefs->framing[idx])) { |
| 130 |
return -1; |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
bitfield &= ~best_bitfield; |
| 135 |
} |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
while (bitfield) { |
| 141 |
best_bitfield = iax2_format_compatibility_best(bitfield); |
| 142 |
if (!best_bitfield) { |
| 143 |
|
| 144 |
break; |
| 145 |
} |
| 146 |
|
| 147 |
format = ast_format_compatibility_bitfield2format(best_bitfield); |
| 148 |
|
| 149 |
ast_assert(format != NULL); |
| 150 |
|
| 151 |
if (ast_format_cap_append(cap, format, 0)) { |
| 152 |
return -1; |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
bitfield &= ~best_bitfield; |
| 157 |
} |
| 158 |
|
| 159 |
|
| 160 |
if (bitfield) { |
| 161 |
int bit; |
| 162 |
|
| 163 |
for (bit = 0; bit < 64; ++bit) { |
| 164 |
uint64_t mask = (1ULL << bit); |
| 165 |
|
| 166 |
if (mask & bitfield) { |
| 167 |
format = ast_format_compatibility_bitfield2format(mask); |
| 168 |
if (format && ast_format_cap_append(cap, format, 0)) { |
| 169 |
return -1; |
| 170 |
} |
| 171 |
} |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
return 0; |
| 176 |
} |
| 177 |
|
| 178 |
int iax2_codec_pref_string(struct iax2_codec_pref *pref, char *buf, size_t size) |
| 179 |
{ |
| 180 |
int x; |
| 181 |
struct ast_format_cap *cap; |
| 182 |
size_t total_len; |
| 183 |
char *cur; |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
if (size < 6) { |
| 188 |
return -1; |
| 189 |
} |
| 190 |
|
| 191 |
|
| 192 |
cap = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT); |
| 193 |
if (!cap || iax2_codec_pref_to_cap(pref, cap)) { |
| 194 |
strcpy(buf, "(...)"); |
| 195 |
ao2_cleanup(cap); |
| 196 |
return -1; |
| 197 |
} |
| 198 |
|
| 199 |
|
| 200 |
total_len = size - 3; |
| 201 |
|
| 202 |
|
| 203 |
buf[0] = '('; |
| 204 |
cur = buf + 1; |
| 205 |
|
| 206 |
|
| 207 |
for (x = 0; x < ast_format_cap_count(cap); x++) { |
| 208 |
size_t name_len; |
| 209 |
struct ast_format *fmt = ast_format_cap_get_format(cap, x); |
| 210 |
const char *name = ast_format_get_name(fmt); |
| 211 |
|
| 212 |
name_len = strlen(name); |
| 213 |
|
| 214 |
|
| 215 |
if (x) { |
| 216 |
name_len++; |
| 217 |
} |
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
if (((x == ast_format_cap_count(cap) - 1) && (total_len < name_len)) || |
| 223 |
((x < ast_format_cap_count(cap) - 1) && (total_len < name_len + 3))) { |
| 224 |
strcpy(cur, "..."); |
| 225 |
cur += 3; |
| 226 |
total_len -= 3; |
| 227 |
ao2_ref(fmt, -1); |
| 228 |
break; |
| 229 |
} |
| 230 |
|
| 231 |
sprintf(cur, "%s%s", x ? "|" : "", name); |
| 232 |
cur += name_len; |
| 233 |
total_len -= name_len; |
| 234 |
|
| 235 |
ao2_ref(fmt, -1); |
| 236 |
} |
| 237 |
ao2_ref(cap, -1); |
| 238 |
|
| 239 |
|
| 240 |
cur[0] = ')'; |
| 241 |
cur[1] = '\0'; |
| 242 |
|
| 243 |
return size - total_len; |
| 244 |
} |
| 245 |
|
| 246 |
static void codec_pref_remove_index(struct iax2_codec_pref *pref, int codec_pref_index) |
| 247 |
{ |
| 248 |
int idx; |
| 249 |
|
| 250 |
idx = codec_pref_index; |
| 251 |
if (idx == ARRAY_LEN(pref->order) - 1) { |
| 252 |
|
| 253 |
pref->order[idx] = 0; |
| 254 |
pref->framing[idx] = 0; |
| 255 |
return; |
| 256 |
} |
| 257 |
|
| 258 |
for (; idx < ARRAY_LEN(pref->order); ++idx) { |
| 259 |
pref->order[idx] = pref->order[idx + 1]; |
| 260 |
pref->framing[idx] = pref->framing[idx + 1]; |
| 261 |
if (!pref->order[idx]) { |
| 262 |
return; |
| 263 |
} |
| 264 |
} |
| 265 |
} |
| 266 |
|
| 267 |
|
| 268 |
static void codec_pref_remove(struct iax2_codec_pref *pref, int format_index) |
| 269 |
{ |
| 270 |
int x; |
| 271 |
|
| 272 |
if (!pref->order[0]) { |
| 273 |
return; |
| 274 |
} |
| 275 |
|
| 276 |
for (x = 0; x < ARRAY_LEN(pref->order); ++x) { |
| 277 |
if (!pref->order[x]) { |
| 278 |
break; |
| 279 |
} |
| 280 |
|
| 281 |
if (pref->order[x] == format_index) { |
| 282 |
codec_pref_remove_index(pref, x); |
| 283 |
break; |
| 284 |
} |
| 285 |
} |
| 286 |
} |
| 287 |
|
| 288 |
void iax2_codec_pref_remove_missing(struct iax2_codec_pref *pref, uint64_t bitfield) |
| 289 |
{ |
| 290 |
int idx; |
| 291 |
|
| 292 |
if (!pref->order[0]) { |
| 293 |
return; |
| 294 |
} |
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
for (idx = ARRAY_LEN(pref->order); idx--;) { |
| 301 |
uint64_t pref_bitfield; |
| 302 |
|
| 303 |
pref_bitfield = iax2_codec_pref_order_value_to_format_bitfield(pref->order[idx]); |
| 304 |
if (!pref_bitfield) { |
| 305 |
continue; |
| 306 |
} |
| 307 |
|
| 308 |
|
| 309 |
if (!(pref_bitfield & bitfield)) { |
| 310 |
codec_pref_remove_index(pref, idx); |
| 311 |
} |
| 312 |
} |
| 313 |
} |
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
static const uint64_t iax2_supported_formats[] = { |
| 325 |
AST_FORMAT_G723, |
| 326 |
AST_FORMAT_GSM, |
| 327 |
AST_FORMAT_ULAW, |
| 328 |
AST_FORMAT_ALAW, |
| 329 |
AST_FORMAT_G726, |
| 330 |
AST_FORMAT_ADPCM, |
| 331 |
AST_FORMAT_SLIN, |
| 332 |
AST_FORMAT_LPC10, |
| 333 |
AST_FORMAT_G729, |
| 334 |
AST_FORMAT_SPEEX, |
| 335 |
AST_FORMAT_SPEEX16, |
| 336 |
AST_FORMAT_ILBC, |
| 337 |
AST_FORMAT_G726_AAL2, |
| 338 |
AST_FORMAT_G722, |
| 339 |
AST_FORMAT_SLIN16, |
| 340 |
AST_FORMAT_JPEG, |
| 341 |
AST_FORMAT_PNG, |
| 342 |
AST_FORMAT_H261, |
| 343 |
AST_FORMAT_H263, |
| 344 |
AST_FORMAT_H263P, |
| 345 |
AST_FORMAT_H264, |
| 346 |
AST_FORMAT_MP4, |
| 347 |
AST_FORMAT_T140_RED, |
| 348 |
AST_FORMAT_T140, |
| 349 |
AST_FORMAT_SIREN7, |
| 350 |
AST_FORMAT_SIREN14, |
| 351 |
0, |
| 352 |
AST_FORMAT_G719, |
| 353 |
0, |
| 354 |
0, |
| 355 |
0, |
| 356 |
0, |
| 357 |
0, |
| 358 |
0, |
| 359 |
0, |
| 360 |
0, |
| 361 |
AST_FORMAT_OPUS, |
| 362 |
AST_FORMAT_VP8, |
| 363 |
|
| 364 |
|
| 365 |
}; |
| 366 |
|
| 367 |
uint64_t iax2_codec_pref_order_value_to_format_bitfield(int order_value) |
| 368 |
{ |
| 369 |
if (order_value < 1 || ARRAY_LEN(iax2_supported_formats) < order_value) { |
| 370 |
return 0; |
| 371 |
} |
| 372 |
|
| 373 |
return iax2_supported_formats[order_value - 1]; |
| 374 |
} |
| 375 |
|
| 376 |
int iax2_codec_pref_format_bitfield_to_order_value(uint64_t bitfield) |
| 377 |
{ |
| 378 |
int idx; |
| 379 |
|
| 380 |
if (bitfield) { |
| 381 |
for (idx = 0; idx < ARRAY_LEN(iax2_supported_formats); ++idx) { |
| 382 |
if (iax2_supported_formats[idx] == bitfield) { |
| 383 |
return idx + 1; |
| 384 |
} |
| 385 |
} |
| 386 |
} |
| 387 |
return 0; |
| 388 |
} |
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
static void iax2_codec_pref_append_bitfield(struct iax2_codec_pref *pref, uint64_t bitfield, unsigned int framing) |
| 400 |
{ |
| 401 |
int format_index; |
| 402 |
int x; |
| 403 |
|
| 404 |
format_index = iax2_codec_pref_format_bitfield_to_order_value(bitfield); |
| 405 |
if (!format_index) { |
| 406 |
return; |
| 407 |
} |
| 408 |
|
| 409 |
codec_pref_remove(pref, format_index); |
| 410 |
|
| 411 |
for (x = 0; x < ARRAY_LEN(pref->order); ++x) { |
| 412 |
if (!pref->order[x]) { |
| 413 |
pref->order[x] = format_index; |
| 414 |
pref->framing[x] = framing; |
| 415 |
break; |
| 416 |
} |
| 417 |
} |
| 418 |
} |
| 419 |
|
| 420 |
void iax2_codec_pref_append(struct iax2_codec_pref *pref, struct ast_format *format, unsigned int framing) |
| 421 |
{ |
| 422 |
uint64_t bitfield; |
| 423 |
|
| 424 |
bitfield = ast_format_compatibility_format2bitfield(format); |
| 425 |
if (!bitfield) { |
| 426 |
return; |
| 427 |
} |
| 428 |
|
| 429 |
iax2_codec_pref_append_bitfield(pref, bitfield, framing); |
| 430 |
} |
| 431 |
|
| 432 |
void iax2_codec_pref_prepend(struct iax2_codec_pref *pref, struct ast_format *format, unsigned int framing, |
| 433 |
int only_if_existing) |
| 434 |
{ |
| 435 |
uint64_t bitfield; |
| 436 |
int format_index; |
| 437 |
int x; |
| 438 |
|
| 439 |
bitfield = ast_format_compatibility_format2bitfield(format); |
| 440 |
if (!bitfield) { |
| 441 |
return; |
| 442 |
} |
| 443 |
format_index = iax2_codec_pref_format_bitfield_to_order_value(bitfield); |
| 444 |
if (!format_index) { |
| 445 |
return; |
| 446 |
} |
| 447 |
|
| 448 |
|
| 449 |
for (x = 0; x < ARRAY_LEN(pref->order); ++x) { |
| 450 |
if (!pref->order[x] || pref->order[x] == format_index) |
| 451 |
break; |
| 452 |
} |
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
ast_assert(x < ARRAY_LEN(pref->order)); |
| 459 |
|
| 460 |
|
| 461 |
if (ARRAY_LEN(pref->order) <= x) { |
| 462 |
x = ARRAY_LEN(pref->order) - 1; |
| 463 |
} |
| 464 |
|
| 465 |
if (only_if_existing && !pref->order[x]) { |
| 466 |
return; |
| 467 |
} |
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
for (; x > 0; --x) { |
| 472 |
pref->order[x] = pref->order[x - 1]; |
| 473 |
pref->framing[x] = pref->framing[x - 1]; |
| 474 |
} |
| 475 |
|
| 476 |
|
| 477 |
pref->order[0] = format_index; |
| 478 |
pref->framing[0] = framing; |
| 479 |
} |
| 480 |
|
| 481 |
uint64_t iax2_codec_pref_from_bitfield(struct iax2_codec_pref *pref, uint64_t bitfield) |
| 482 |
{ |
| 483 |
int bit; |
| 484 |
uint64_t working_bitfield; |
| 485 |
uint64_t best_bitfield; |
| 486 |
struct ast_format *format; |
| 487 |
|
| 488 |
|
| 489 |
memset(pref, 0, sizeof(*pref)); |
| 490 |
|
| 491 |
working_bitfield = bitfield; |
| 492 |
|
| 493 |
|
| 494 |
while (working_bitfield) { |
| 495 |
best_bitfield = iax2_format_compatibility_best(working_bitfield); |
| 496 |
if (!best_bitfield) { |
| 497 |
|
| 498 |
break; |
| 499 |
} |
| 500 |
|
| 501 |
|
| 502 |
working_bitfield &= ~best_bitfield; |
| 503 |
|
| 504 |
format = ast_format_compatibility_bitfield2format(best_bitfield); |
| 505 |
|
| 506 |
ast_assert(format != NULL); |
| 507 |
|
| 508 |
iax2_codec_pref_append_bitfield(pref, best_bitfield, 0); |
| 509 |
} |
| 510 |
|
| 511 |
|
| 512 |
if (working_bitfield) { |
| 513 |
for (bit = 0; bit < 64; ++bit) { |
| 514 |
uint64_t mask = (1ULL << bit); |
| 515 |
|
| 516 |
if (mask & working_bitfield) { |
| 517 |
format = ast_format_compatibility_bitfield2format(mask); |
| 518 |
if (!format) { |
| 519 |
|
| 520 |
bitfield &= ~mask; |
| 521 |
continue; |
| 522 |
} |
| 523 |
|
| 524 |
iax2_codec_pref_append_bitfield(pref, mask, 0); |
| 525 |
} |
| 526 |
} |
| 527 |
} |
| 528 |
|
| 529 |
return bitfield; |
| 530 |
} |