| 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/format.h" |
| 34 |
#include "asterisk/format_cache.h" |
| 35 |
#include "asterisk/astobj2.h" |
| 36 |
#include "asterisk/strings.h" |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
struct ast_format *ast_format_slin; |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
struct ast_format *ast_format_slin12; |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
struct ast_format *ast_format_slin16; |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
struct ast_format *ast_format_slin24; |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
struct ast_format *ast_format_slin32; |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
struct ast_format *ast_format_slin44; |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
struct ast_format *ast_format_slin48; |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
struct ast_format *ast_format_slin96; |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
struct ast_format *ast_format_slin192; |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
struct ast_format *ast_format_ulaw; |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
struct ast_format *ast_format_alaw; |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
struct ast_format *ast_format_gsm; |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
struct ast_format *ast_format_adpcm; |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
struct ast_format *ast_format_g722; |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
struct ast_format *ast_format_g726; |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
struct ast_format *ast_format_g726_aal2; |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
struct ast_format *ast_format_ilbc; |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
struct ast_format *ast_format_lpc10; |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
struct ast_format *ast_format_speex; |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
struct ast_format *ast_format_speex16; |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
struct ast_format *ast_format_speex32; |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
struct ast_format *ast_format_g723; |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
struct ast_format *ast_format_g729; |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
struct ast_format *ast_format_g719; |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
struct ast_format *ast_format_h261; |
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
struct ast_format *ast_format_h263; |
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
struct ast_format *ast_format_h263p; |
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
struct ast_format *ast_format_h264; |
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
struct ast_format *ast_format_h265; |
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
struct ast_format *ast_format_mp4; |
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
struct ast_format *ast_format_vp8; |
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
struct ast_format *ast_format_vp9; |
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
struct ast_format *ast_format_jpeg; |
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
struct ast_format *ast_format_png; |
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
struct ast_format *ast_format_siren14; |
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
struct ast_format *ast_format_siren7; |
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
struct ast_format *ast_format_opus; |
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
struct ast_format *ast_format_codec2; |
| 227 |
|
| 228 |
|
| •229 |
|
| 230 |
|
| 231 |
struct ast_format *ast_format_amr; |
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
struct ast_format *ast_format_amrwb; |
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
struct ast_format *ast_format_t140; |
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
struct ast_format *ast_format_t140_red; |
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
struct ast_format *ast_format_t38; |
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
struct ast_format *ast_format_none; |
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
struct ast_format *ast_format_silk8; |
| 262 |
struct ast_format *ast_format_silk12; |
| 263 |
struct ast_format *ast_format_silk16; |
| 264 |
struct ast_format *ast_format_silk24; |
| 265 |
|
| 266 |
|
| 267 |
#define CACHE_BUCKETS 53 |
| 268 |
|
| 269 |
|
| 270 |
static struct ao2_container *formats; |
| 271 |
|
| 272 |
static int format_hash_cb(const void *obj, int flags) |
| 273 |
{ |
| 274 |
const struct ast_format *format; |
| 275 |
const char *key; |
| 276 |
|
| 277 |
switch (flags & OBJ_SEARCH_MASK) { |
| 278 |
case OBJ_SEARCH_KEY: |
| 279 |
key = obj; |
| 280 |
return ast_str_case_hash(key); |
| 281 |
case OBJ_SEARCH_OBJECT: |
| 282 |
format = obj; |
| 283 |
return ast_str_case_hash(ast_format_get_name(format)); |
| 284 |
default: |
| 285 |
|
| 286 |
ast_assert(0); |
| 287 |
return 0; |
| 288 |
} |
| 289 |
} |
| 290 |
|
| 291 |
static int format_cmp_cb(void *obj, void *arg, int flags) |
| 292 |
{ |
| 293 |
const struct ast_format *left = obj; |
| 294 |
const struct ast_format *right = arg; |
| 295 |
const char *right_key = arg; |
| 296 |
int cmp; |
| 297 |
|
| 298 |
switch (flags & OBJ_SEARCH_MASK) { |
| 299 |
case OBJ_SEARCH_OBJECT: |
| 300 |
right_key = ast_format_get_name(right); |
| 301 |
|
| 302 |
case OBJ_SEARCH_KEY: |
| 303 |
cmp = strcasecmp(ast_format_get_name(left), right_key); |
| 304 |
break; |
| 305 |
case OBJ_SEARCH_PARTIAL_KEY: |
| 306 |
cmp = strncasecmp(ast_format_get_name(left), right_key, strlen(right_key)); |
| 307 |
break; |
| 308 |
default: |
| 309 |
ast_assert(0); |
| 310 |
cmp = 0; |
| 311 |
break; |
| 312 |
} |
| 313 |
if (cmp) { |
| 314 |
return 0; |
| 315 |
} |
| 316 |
|
| 317 |
return CMP_MATCH; |
| 318 |
} |
| 319 |
|
| 320 |
|
| 321 |
static void format_cache_shutdown(void) |
| 322 |
{ |
| 323 |
ao2_cleanup(formats); |
| 324 |
formats = NULL; |
| 325 |
|
| •326 |
ao2_replace(ast_format_amr, NULL); |
| 327 |
ao2_replace(ast_format_amrwb, NULL); |
| 328 |
|
| 329 |
ao2_replace(ast_format_g723, NULL); |
| 330 |
ao2_replace(ast_format_ulaw, NULL); |
| 331 |
ao2_replace(ast_format_alaw, NULL); |
| 332 |
ao2_replace(ast_format_gsm, NULL); |
| 333 |
ao2_replace(ast_format_g726, NULL); |
| 334 |
ao2_replace(ast_format_g726_aal2, NULL); |
| 335 |
ao2_replace(ast_format_adpcm, NULL); |
| 336 |
ao2_replace(ast_format_slin, NULL); |
| 337 |
ao2_replace(ast_format_slin12, NULL); |
| 338 |
ao2_replace(ast_format_slin16, NULL); |
| 339 |
ao2_replace(ast_format_slin24, NULL); |
| 340 |
ao2_replace(ast_format_slin32, NULL); |
| 341 |
ao2_replace(ast_format_slin44, NULL); |
| 342 |
ao2_replace(ast_format_slin48, NULL); |
| 343 |
ao2_replace(ast_format_slin96, NULL); |
| 344 |
ao2_replace(ast_format_slin192, NULL); |
| 345 |
ao2_replace(ast_format_lpc10, NULL); |
| 346 |
ao2_replace(ast_format_g729, NULL); |
| 347 |
ao2_replace(ast_format_speex, NULL); |
| 348 |
ao2_replace(ast_format_speex16, NULL); |
| 349 |
ao2_replace(ast_format_speex32, NULL); |
| 350 |
ao2_replace(ast_format_ilbc, NULL); |
| 351 |
ao2_replace(ast_format_g722, NULL); |
| 352 |
ao2_replace(ast_format_siren7, NULL); |
| 353 |
ao2_replace(ast_format_siren14, NULL); |
| 354 |
ao2_replace(ast_format_g719, NULL); |
| 355 |
ao2_replace(ast_format_opus, NULL); |
| 356 |
ao2_replace(ast_format_codec2, NULL); |
| 357 |
ao2_replace(ast_format_jpeg, NULL); |
| 358 |
ao2_replace(ast_format_png, NULL); |
| 359 |
ao2_replace(ast_format_h261, NULL); |
| 360 |
ao2_replace(ast_format_h263, NULL); |
| 361 |
ao2_replace(ast_format_h263p, NULL); |
| 362 |
ao2_replace(ast_format_h264, NULL); |
| 363 |
ao2_replace(ast_format_h265, NULL); |
| 364 |
ao2_replace(ast_format_mp4, NULL); |
| 365 |
ao2_replace(ast_format_vp8, NULL); |
| 366 |
ao2_replace(ast_format_vp9, NULL); |
| 367 |
ao2_replace(ast_format_t140_red, NULL); |
| 368 |
ao2_replace(ast_format_t140, NULL); |
| 369 |
ao2_replace(ast_format_t38, NULL); |
| 370 |
ao2_replace(ast_format_none, NULL); |
| 371 |
ao2_replace(ast_format_silk8, NULL); |
| 372 |
ao2_replace(ast_format_silk12, NULL); |
| 373 |
ao2_replace(ast_format_silk16, NULL); |
| 374 |
ao2_replace(ast_format_silk24, NULL); |
| 375 |
} |
| 376 |
|
| 377 |
int ast_format_cache_init(void) |
| 378 |
{ |
| 379 |
formats = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_RWLOCK, 0, CACHE_BUCKETS, |
| 380 |
format_hash_cb, NULL, format_cmp_cb); |
| 381 |
if (!formats) { |
| 382 |
return -1; |
| 383 |
} |
| 384 |
|
| 385 |
ast_register_cleanup(format_cache_shutdown); |
| 386 |
|
| 387 |
return 0; |
| 388 |
} |
| 389 |
|
| 390 |
static void set_cached_format(const char *name, struct ast_format *format) |
| 391 |
{ |
| 392 |
if (!strcmp(name, "codec2")) { |
| 393 |
ao2_replace(ast_format_codec2, format); |
| 394 |
} else if (!strcmp(name, "g723")) { |
| 395 |
ao2_replace(ast_format_g723, format); |
| 396 |
} else if (!strcmp(name, "ulaw")) { |
| 397 |
ao2_replace(ast_format_ulaw, format); |
| 398 |
} else if (!strcmp(name, "alaw")) { |
| 399 |
ao2_replace(ast_format_alaw, format); |
| 400 |
} else if (!strcmp(name, "gsm")) { |
| 401 |
ao2_replace(ast_format_gsm, format); |
| 402 |
} else if (!strcmp(name, "g726")) { |
| 403 |
ao2_replace(ast_format_g726, format); |
| 404 |
} else if (!strcmp(name, "g726aal2")) { |
| 405 |
ao2_replace(ast_format_g726_aal2, format); |
| 406 |
} else if (!strcmp(name, "adpcm")) { |
| 407 |
ao2_replace(ast_format_adpcm, format); |
| 408 |
} else if (!strcmp(name, "slin")) { |
| 409 |
ao2_replace(ast_format_slin, format); |
| 410 |
} else if (!strcmp(name, "slin12")) { |
| 411 |
ao2_replace(ast_format_slin12, format); |
| 412 |
} else if (!strcmp(name, "slin16")) { |
| 413 |
ao2_replace(ast_format_slin16, format); |
| 414 |
} else if (!strcmp(name, "slin24")) { |
| 415 |
ao2_replace(ast_format_slin24, format); |
| 416 |
} else if (!strcmp(name, "slin32")) { |
| 417 |
ao2_replace(ast_format_slin32, format); |
| 418 |
} else if (!strcmp(name, "slin44")) { |
| 419 |
ao2_replace(ast_format_slin44, format); |
| 420 |
} else if (!strcmp(name, "slin48")) { |
| 421 |
ao2_replace(ast_format_slin48, format); |
| 422 |
} else if (!strcmp(name, "slin96")) { |
| 423 |
ao2_replace(ast_format_slin96, format); |
| 424 |
} else if (!strcmp(name, "slin192")) { |
| 425 |
ao2_replace(ast_format_slin192, format); |
| 426 |
} else if (!strcmp(name, "lpc10")) { |
| 427 |
ao2_replace(ast_format_lpc10, format); |
| 428 |
} else if (!strcmp(name, "g729")) { |
| 429 |
ao2_replace(ast_format_g729, format); |
| 430 |
} else if (!strcmp(name, "speex")) { |
| 431 |
ao2_replace(ast_format_speex, format); |
| 432 |
} else if (!strcmp(name, "speex16")) { |
| 433 |
ao2_replace(ast_format_speex16, format); |
| 434 |
} else if (!strcmp(name, "speex32")) { |
| 435 |
ao2_replace(ast_format_speex32, format); |
| 436 |
} else if (!strcmp(name, "ilbc")) { |
| 437 |
ao2_replace(ast_format_ilbc, format); |
| 438 |
} else if (!strcmp(name, "g722")) { |
| 439 |
ao2_replace(ast_format_g722, format); |
| 440 |
} else if (!strcmp(name, "siren7")) { |
| 441 |
ao2_replace(ast_format_siren7, format); |
| 442 |
} else if (!strcmp(name, "siren14")) { |
| 443 |
ao2_replace(ast_format_siren14, format); |
| 444 |
} else if (!strcmp(name, "g719")) { |
| 445 |
ao2_replace(ast_format_g719, format); |
| 446 |
} else if (!strcmp(name, "opus")) { |
| 447 |
ao2_replace(ast_format_opus, format); |
| •448 |
} else if (!strcmp(name, "amr")) { |
| 449 |
ao2_replace(ast_format_amr, format); |
| 450 |
} else if (!strcmp(name, "amrwb")) { |
| 451 |
ao2_replace(ast_format_amrwb, format); |
| 452 |
} else if (!strcmp(name, "jpeg")) { |
| 453 |
ao2_replace(ast_format_jpeg, format); |
| 454 |
} else if (!strcmp(name, "png")) { |
| 455 |
ao2_replace(ast_format_png, format); |
| 456 |
} else if (!strcmp(name, "h261")) { |
| 457 |
ao2_replace(ast_format_h261, format); |
| 458 |
} else if (!strcmp(name, "h263")) { |
| 459 |
ao2_replace(ast_format_h263, format); |
| 460 |
} else if (!strcmp(name, "h263p")) { |
| 461 |
ao2_replace(ast_format_h263p, format); |
| 462 |
} else if (!strcmp(name, "h264")) { |
| 463 |
ao2_replace(ast_format_h264, format); |
| 464 |
} else if (!strcmp(name, "h265")) { |
| 465 |
ao2_replace(ast_format_h265, format); |
| 466 |
} else if (!strcmp(name, "mpeg4")) { |
| 467 |
ao2_replace(ast_format_mp4, format); |
| 468 |
} else if (!strcmp(name, "vp8")) { |
| 469 |
ao2_replace(ast_format_vp8, format); |
| 470 |
} else if (!strcmp(name, "vp9")) { |
| 471 |
ao2_replace(ast_format_vp9, format); |
| 472 |
} else if (!strcmp(name, "red")) { |
| 473 |
ao2_replace(ast_format_t140_red, format); |
| 474 |
} else if (!strcmp(name, "t140")) { |
| 475 |
ao2_replace(ast_format_t140, format); |
| 476 |
} else if (!strcmp(name, "t38")) { |
| 477 |
ao2_replace(ast_format_t38, format); |
| 478 |
} else if (!strcmp(name, "none")) { |
| 479 |
ao2_replace(ast_format_none, format); |
| 480 |
} else if (!strcmp(name, "silk8")) { |
| 481 |
ao2_replace(ast_format_silk8, format); |
| 482 |
} else if (!strcmp(name, "silk12")) { |
| 483 |
ao2_replace(ast_format_silk12, format); |
| 484 |
} else if (!strcmp(name, "silk16")) { |
| 485 |
ao2_replace(ast_format_silk16, format); |
| 486 |
} else if (!strcmp(name, "silk24")) { |
| 487 |
ao2_replace(ast_format_silk24, format); |
| 488 |
} |
| 489 |
} |
| 490 |
|
| 491 |
int ast_format_cache_set(struct ast_format *format) |
| 492 |
{ |
| 493 |
SCOPED_AO2WRLOCK(lock, formats); |
| 494 |
struct ast_format *old_format; |
| 495 |
|
| 496 |
ast_assert(format != NULL); |
| 497 |
|
| 498 |
if (ast_strlen_zero(ast_format_get_name(format))) { |
| 499 |
return -1; |
| 500 |
} |
| 501 |
|
| 502 |
old_format = ao2_find(formats, ast_format_get_name(format), OBJ_SEARCH_KEY | OBJ_NOLOCK); |
| 503 |
if (old_format) { |
| 504 |
ao2_unlink_flags(formats, old_format, OBJ_NOLOCK); |
| 505 |
} |
| 506 |
ao2_link_flags(formats, format, OBJ_NOLOCK); |
| 507 |
|
| 508 |
set_cached_format(ast_format_get_name(format), format); |
| 509 |
|
| 510 |
ast_verb(5, "%s cached format with name '%s'\n", |
| 511 |
old_format ? "Updated" : "Created", |
| 512 |
ast_format_get_name(format)); |
| 513 |
|
| 514 |
ao2_cleanup(old_format); |
| 515 |
|
| 516 |
return 0; |
| 517 |
} |
| 518 |
|
| 519 |
struct ast_format *__ast_format_cache_get(const char *name, |
| 520 |
const char *tag, const char *file, int line, const char *func) |
| 521 |
{ |
| 522 |
if (ast_strlen_zero(name)) { |
| 523 |
return NULL; |
| 524 |
} |
| 525 |
|
| 526 |
return __ao2_find(formats, name, OBJ_SEARCH_KEY, tag, file, line, func); |
| 527 |
} |
| 528 |
|
| 529 |
struct ast_format *ast_format_cache_get_slin_by_rate(unsigned int rate) |
| 530 |
{ |
| 531 |
if (rate >= 192000) { |
| 532 |
return ast_format_slin192; |
| 533 |
} else if (rate >= 96000) { |
| 534 |
return ast_format_slin96; |
| 535 |
} else if (rate >= 48000) { |
| 536 |
return ast_format_slin48; |
| 537 |
} else if (rate >= 44100) { |
| 538 |
return ast_format_slin44; |
| 539 |
} else if (rate >= 32000) { |
| 540 |
return ast_format_slin32; |
| 541 |
} else if (rate >= 24000) { |
| 542 |
return ast_format_slin24; |
| 543 |
} else if (rate >= 16000) { |
| 544 |
return ast_format_slin16; |
| 545 |
} else if (rate >= 12000) { |
| 546 |
return ast_format_slin12; |
| 547 |
} |
| 548 |
return ast_format_slin; |
| 549 |
} |
| 550 |
|
| 551 |
int ast_format_cache_is_slinear(struct ast_format *format) |
| 552 |
{ |
| 553 |
if ((ast_format_cmp(format, ast_format_slin) == AST_FORMAT_CMP_EQUAL) |
| 554 |
|| (ast_format_cmp(format, ast_format_slin12) == AST_FORMAT_CMP_EQUAL) |
| 555 |
|| (ast_format_cmp(format, ast_format_slin16) == AST_FORMAT_CMP_EQUAL) |
| 556 |
|| (ast_format_cmp(format, ast_format_slin24) == AST_FORMAT_CMP_EQUAL) |
| 557 |
|| (ast_format_cmp(format, ast_format_slin32) == AST_FORMAT_CMP_EQUAL) |
| 558 |
|| (ast_format_cmp(format, ast_format_slin44) == AST_FORMAT_CMP_EQUAL) |
| 559 |
|| (ast_format_cmp(format, ast_format_slin48) == AST_FORMAT_CMP_EQUAL) |
| 560 |
|| (ast_format_cmp(format, ast_format_slin96) == AST_FORMAT_CMP_EQUAL) |
| 561 |
|| (ast_format_cmp(format, ast_format_slin192) == AST_FORMAT_CMP_EQUAL)) { |
| 562 |
return 1; |
| 563 |
} |
| 564 |
|
| 565 |
return 0; |
| 566 |
} |
| 567 |
|
| 568 |
struct ast_format *ast_format_cache_get_by_codec(const struct ast_codec *codec) |
| 569 |
{ |
| 570 |
struct ast_format *format; |
| 571 |
struct ao2_iterator it; |
| 572 |
|
| 573 |
for (it = ao2_iterator_init(formats, 0); |
| 574 |
(format = ao2_iterator_next(&it)); |
| 575 |
ao2_ref(format, -1)) { |
| 576 |
struct ast_codec *candidate = ast_format_get_codec(format); |
| 577 |
if (codec == candidate) { |
| 578 |
ao2_cleanup(candidate); |
| 579 |
ao2_iterator_destroy(&it); |
| 580 |
return format; |
| 581 |
} |
| 582 |
ao2_cleanup(candidate); |
| 583 |
} |
| 584 |
|
| 585 |
ao2_iterator_destroy(&it); |
| 586 |
return NULL; |
| 587 |
} |