| 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_cap.h" |
| 35 |
#include "asterisk/format_cache.h" |
| 36 |
#include "asterisk/codec.h" |
| 37 |
#include "asterisk/astobj2.h" |
| 38 |
#include "asterisk/strings.h" |
| 39 |
#include "asterisk/vector.h" |
| 40 |
#include "asterisk/linkedlists.h" |
| 41 |
#include "asterisk/utils.h" |
| 42 |
|
| 43 |
|
| 44 |
struct format_cap_framed { |
| 45 |
|
| 46 |
struct ast_format *format; |
| 47 |
|
| 48 |
unsigned int framing; |
| 49 |
|
| 50 |
AST_LIST_ENTRY(format_cap_framed) entry; |
| 51 |
}; |
| 52 |
|
| 53 |
|
| 54 |
struct ast_format_cap { |
| 55 |
|
| 56 |
AST_VECTOR(, struct format_cap_framed_list) formats; |
| 57 |
|
| 58 |
AST_VECTOR(, struct format_cap_framed *) preference_order; |
| 59 |
|
| 60 |
unsigned int framing; |
| 61 |
}; |
| 62 |
|
| 63 |
|
| 64 |
AST_LIST_HEAD_NOLOCK(format_cap_framed_list, format_cap_framed); |
| 65 |
|
| 66 |
|
| 67 |
static const struct format_cap_framed_list format_cap_framed_list_empty = AST_LIST_HEAD_NOLOCK_INIT_VALUE; |
| 68 |
|
| 69 |
|
| 70 |
static void format_cap_destroy(void *obj) |
| 71 |
{ |
| 72 |
struct ast_format_cap *cap = obj; |
| 73 |
int idx; |
| 74 |
|
| 75 |
for (idx = 0; idx < AST_VECTOR_SIZE(&cap->formats); idx++) { |
| 76 |
struct format_cap_framed_list *list = AST_VECTOR_GET_ADDR(&cap->formats, idx); |
| 77 |
struct format_cap_framed *framed; |
| 78 |
|
| 79 |
while ((framed = AST_LIST_REMOVE_HEAD(list, entry))) { |
| 80 |
ao2_ref(framed, -1); |
| 81 |
} |
| 82 |
} |
| 83 |
AST_VECTOR_FREE(&cap->formats); |
| 84 |
|
| 85 |
for (idx = 0; idx < AST_VECTOR_SIZE(&cap->preference_order); idx++) { |
| 86 |
struct format_cap_framed *framed = AST_VECTOR_GET(&cap->preference_order, idx); |
| 87 |
|
| 88 |
|
| 89 |
ao2_ref(framed, -1); |
| 90 |
} |
| 91 |
AST_VECTOR_FREE(&cap->preference_order); |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
static inline int format_cap_init(struct ast_format_cap *cap, enum ast_format_cap_flags flags) |
| 103 |
{ |
| 104 |
if (AST_VECTOR_INIT(&cap->formats, 0)) { |
| 105 |
return -1; |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
if (AST_VECTOR_INIT(&cap->preference_order, 5)) { |
| 110 |
return -1; |
| 111 |
} |
| 112 |
|
| 113 |
cap->framing = UINT_MAX; |
| 114 |
return 0; |
| 115 |
} |
| 116 |
|
| 117 |
struct ast_format_cap *__ast_format_cap_alloc(enum ast_format_cap_flags flags, |
| 118 |
const char *tag, const char *file, int line, const char *func) |
| 119 |
{ |
| 120 |
struct ast_format_cap *cap; |
| 121 |
|
| 122 |
cap = __ao2_alloc(sizeof(*cap), format_cap_destroy, AO2_ALLOC_OPT_LOCK_NOLOCK, |
| 123 |
tag, file, line, func); |
| 124 |
if (!cap) { |
| 125 |
return NULL; |
| 126 |
} |
| 127 |
|
| 128 |
if (format_cap_init(cap, flags)) { |
| 129 |
ao2_ref(cap, -1); |
| 130 |
return NULL; |
| 131 |
} |
| 132 |
|
| 133 |
return cap; |
| 134 |
} |
| 135 |
|
| 136 |
void ast_format_cap_set_framing(struct ast_format_cap *cap, unsigned int framing) |
| 137 |
{ |
| 138 |
cap->framing = framing; |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
static void format_cap_framed_destroy(void *obj) |
| 143 |
{ |
| 144 |
struct format_cap_framed *framed = obj; |
| 145 |
|
| 146 |
ao2_cleanup(framed->format); |
| 147 |
} |
| 148 |
|
| 149 |
static inline int format_cap_framed_init(struct format_cap_framed *framed, struct ast_format_cap *cap, struct ast_format *format, unsigned int framing) |
| 150 |
{ |
| 151 |
struct format_cap_framed_list *list; |
| 152 |
|
| 153 |
framed->framing = framing; |
| 154 |
|
| 155 |
if (ast_format_get_codec_id(format) >= AST_VECTOR_SIZE(&cap->formats)) { |
| 156 |
if (AST_VECTOR_REPLACE(&cap->formats, ast_format_get_codec_id(format), format_cap_framed_list_empty)) { |
| 157 |
ao2_ref(framed, -1); |
| 158 |
return -1; |
| 159 |
} |
| 160 |
} |
| 161 |
list = AST_VECTOR_GET_ADDR(&cap->formats, ast_format_get_codec_id(format)); |
| 162 |
|
| 163 |
|
| 164 |
if (AST_VECTOR_APPEND(&cap->preference_order, framed)) { |
| 165 |
ao2_ref(framed, -1); |
| 166 |
return -1; |
| 167 |
} |
| 168 |
|
| 169 |
|
| 170 |
ao2_ref(framed, +1); |
| 171 |
AST_LIST_INSERT_HEAD(list, framed, entry); |
| 172 |
|
| 173 |
cap->framing = MIN(cap->framing, framing ? framing : ast_format_get_default_ms(format)); |
| 174 |
|
| 175 |
return 0; |
| 176 |
} |
| 177 |
|
| 178 |
|
| 179 |
static int format_in_format_cap(struct ast_format_cap *cap, struct ast_format *format) |
| 180 |
{ |
| 181 |
struct format_cap_framed *framed; |
| 182 |
int i; |
| 183 |
|
| 184 |
for (i = 0; i < AST_VECTOR_SIZE(&cap->preference_order); i++) { |
| 185 |
framed = AST_VECTOR_GET(&cap->preference_order, i); |
| 186 |
|
| 187 |
if (ast_format_get_codec_id(format) == ast_format_get_codec_id(framed->format)) { |
| 188 |
return 1; |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
return 0; |
| 193 |
} |
| 194 |
|
| 195 |
int __ast_format_cap_append(struct ast_format_cap *cap, struct ast_format *format, unsigned int framing, const char *tag, const char *file, int line, const char *func) |
| 196 |
{ |
| 197 |
struct format_cap_framed *framed; |
| 198 |
|
| 199 |
ast_assert(format != NULL); |
| 200 |
|
| 201 |
if (format_in_format_cap(cap, format)) { |
| 202 |
return 0; |
| 203 |
} |
| 204 |
|
| 205 |
framed = ao2_alloc_options(sizeof(*framed), format_cap_framed_destroy, AO2_ALLOC_OPT_LOCK_NOLOCK); |
| 206 |
if (!framed) { |
| 207 |
return -1; |
| 208 |
} |
| 209 |
|
| 210 |
__ao2_ref(format, +1, tag, file, line, func); |
| 211 |
framed->format = format; |
| 212 |
|
| 213 |
return format_cap_framed_init(framed, cap, format, framing); |
| 214 |
} |
| 215 |
|
| 216 |
int ast_format_cap_append_by_type(struct ast_format_cap *cap, enum ast_media_type type) |
| 217 |
{ |
| 218 |
int id; |
| 219 |
|
| 220 |
for (id = 1; id < ast_codec_get_max(); ++id) { |
| 221 |
struct ast_codec *codec = ast_codec_get_by_id(id); |
| 222 |
struct ast_codec *codec2 = NULL; |
| 223 |
struct ast_format *format; |
| 224 |
int res; |
| 225 |
|
| 226 |
if (!codec) { |
| 227 |
continue; |
| 228 |
} |
| 229 |
|
| 230 |
if ((type != AST_MEDIA_TYPE_UNKNOWN) && codec->type != type) { |
| 231 |
ao2_ref(codec, -1); |
| 232 |
continue; |
| 233 |
} |
| 234 |
|
| 235 |
format = ast_format_cache_get_by_codec(codec); |
| 236 |
|
| 237 |
if (format == ast_format_none) { |
| 238 |
ao2_ref(format, -1); |
| 239 |
ao2_ref(codec, -1); |
| 240 |
continue; |
| 241 |
} |
| 242 |
|
| 243 |
if (format) { |
| 244 |
codec2 = ast_format_get_codec(format); |
| 245 |
} |
| 246 |
if (codec != codec2) { |
| 247 |
ao2_cleanup(format); |
| 248 |
format = ast_format_create(codec); |
| 249 |
} |
| 250 |
ao2_cleanup(codec2); |
| 251 |
ao2_ref(codec, -1); |
| 252 |
|
| 253 |
if (!format) { |
| 254 |
return -1; |
| 255 |
} |
| 256 |
|
| 257 |
|
| 258 |
res = ast_format_cap_append(cap, format, 0); |
| 259 |
ao2_ref(format, -1); |
| 260 |
|
| 261 |
if (res) { |
| 262 |
return -1; |
| 263 |
} |
| 264 |
} |
| 265 |
|
| 266 |
return 0; |
| 267 |
} |
| 268 |
|
| 269 |
int ast_format_cap_append_from_cap(struct ast_format_cap *dst, const struct ast_format_cap *src, |
| 270 |
enum ast_media_type type) |
| 271 |
{ |
| 272 |
int idx, res = 0; |
| 273 |
|
| 274 |
|
| 275 |
for (idx = 0; (idx < AST_VECTOR_SIZE(&src->preference_order)) && !res; ++idx) { |
| 276 |
struct format_cap_framed *framed = AST_VECTOR_GET(&src->preference_order, idx); |
| 277 |
|
| 278 |
if (type == AST_MEDIA_TYPE_UNKNOWN || ast_format_get_type(framed->format) == type) { |
| 279 |
res = ast_format_cap_append(dst, framed->format, framed->framing); |
| 280 |
} |
| 281 |
} |
| 282 |
|
| 283 |
return res; |
| 284 |
} |
| 285 |
|
| 286 |
static int format_cap_replace(struct ast_format_cap *cap, struct ast_format *format, unsigned int framing) |
| 287 |
{ |
| 288 |
struct format_cap_framed *framed; |
| 289 |
int i; |
| 290 |
|
| 291 |
ast_assert(format != NULL); |
| 292 |
|
| 293 |
for (i = 0; i < AST_VECTOR_SIZE(&cap->preference_order); i++) { |
| 294 |
framed = AST_VECTOR_GET(&cap->preference_order, i); |
| 295 |
|
| 296 |
if (ast_format_get_codec_id(format) == ast_format_get_codec_id(framed->format)) { |
| 297 |
ao2_t_replace(framed->format, format, "replacing with new format"); |
| 298 |
framed->framing = framing; |
| 299 |
return 0; |
| 300 |
} |
| 301 |
} |
| 302 |
|
| 303 |
return -1; |
| 304 |
} |
| 305 |
|
| 306 |
void ast_format_cap_replace_from_cap(struct ast_format_cap *dst, const struct ast_format_cap *src, |
| 307 |
enum ast_media_type type) |
| 308 |
{ |
| 309 |
int idx; |
| 310 |
|
| 311 |
for (idx = 0; (idx < AST_VECTOR_SIZE(&src->preference_order)); ++idx) { |
| 312 |
struct format_cap_framed *framed = AST_VECTOR_GET(&src->preference_order, idx); |
| 313 |
|
| 314 |
if (type == AST_MEDIA_TYPE_UNKNOWN || ast_format_get_type(framed->format) == type) { |
| 315 |
format_cap_replace(dst, framed->format, framed->framing); |
| 316 |
} |
| 317 |
} |
| 318 |
} |
| 319 |
|
| 320 |
int ast_format_cap_update_by_allow_disallow(struct ast_format_cap *cap, const char *list, int allowing) |
| 321 |
{ |
| 322 |
int res = 0, all = 0, iter_allowing; |
| 323 |
char *parse = NULL, *this = NULL, *psize = NULL; |
| 324 |
|
| 325 |
if (!allowing && ast_strlen_zero(list)) { |
| 326 |
return 0; |
| 327 |
} |
| 328 |
|
| 329 |
parse = ast_strdupa(list); |
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
if (parse[0] == '(' && parse[strlen(parse) - 1] == ')') { |
| 336 |
parse++; |
| 337 |
parse[strlen(parse) - 1] = '\0'; |
| 338 |
|
| 339 |
if (allowing) { |
| 340 |
ast_format_cap_remove_by_type(cap, AST_MEDIA_TYPE_UNKNOWN); |
| 341 |
} else { |
| 342 |
ast_format_cap_append_by_type(cap, AST_MEDIA_TYPE_UNKNOWN); |
| 343 |
} |
| 344 |
} |
| 345 |
|
| 346 |
|
| 347 |
while ((this = ast_strip(strsep(&parse, ",|")))) { |
| 348 |
int framems = 0; |
| 349 |
struct ast_format *format = NULL; |
| 350 |
|
| 351 |
iter_allowing = allowing; |
| 352 |
if (*this == '!') { |
| 353 |
this++; |
| 354 |
iter_allowing = !allowing; |
| 355 |
} |
| 356 |
if ((psize = strrchr(this, ':'))) { |
| 357 |
*psize++ = '\0'; |
| 358 |
ast_debug(1, "Packetization for codec: %s is %s\n", this, psize); |
| 359 |
if (!sscanf(psize, "%30d", &framems) || (framems < 0)) { |
| 360 |
framems = 0; |
| 361 |
res = -1; |
| 362 |
ast_log(LOG_WARNING, "Bad packetization value for codec %s\n", this); |
| 363 |
continue; |
| 364 |
} |
| 365 |
} |
| 366 |
all = strcasecmp(this, "all") ? 0 : 1; |
| 367 |
|
| 368 |
if (!all && !(format = ast_format_cache_get(this))) { |
| 369 |
ast_log(LOG_WARNING, "Cannot %s unknown format '%s'\n", iter_allowing ? "allow" : "disallow", this); |
| 370 |
res = -1; |
| 371 |
continue; |
| 372 |
} |
| 373 |
|
| 374 |
if (cap) { |
| 375 |
if (iter_allowing) { |
| 376 |
if (all) { |
| 377 |
ast_format_cap_append_by_type(cap, AST_MEDIA_TYPE_UNKNOWN); |
| 378 |
} else { |
| 379 |
ast_format_cap_append(cap, format, framems); |
| 380 |
} |
| 381 |
} else { |
| 382 |
if (all) { |
| 383 |
ast_format_cap_remove_by_type(cap, AST_MEDIA_TYPE_UNKNOWN); |
| 384 |
} else { |
| 385 |
ast_format_cap_remove(cap, format); |
| 386 |
} |
| 387 |
} |
| 388 |
} |
| 389 |
|
| 390 |
ao2_cleanup(format); |
| 391 |
} |
| 392 |
return res; |
| 393 |
} |
| 394 |
|
| 395 |
size_t ast_format_cap_count(const struct ast_format_cap *cap) |
| 396 |
{ |
| 397 |
return AST_VECTOR_SIZE(&cap->preference_order); |
| 398 |
} |
| 399 |
|
| 400 |
struct ast_format *ast_format_cap_get_format(const struct ast_format_cap *cap, int position) |
| 401 |
{ |
| 402 |
struct format_cap_framed *framed; |
| 403 |
|
| 404 |
ast_assert(position < AST_VECTOR_SIZE(&cap->preference_order)); |
| 405 |
|
| 406 |
if (position >= AST_VECTOR_SIZE(&cap->preference_order)) { |
| 407 |
return NULL; |
| 408 |
} |
| 409 |
|
| 410 |
framed = AST_VECTOR_GET(&cap->preference_order, position); |
| 411 |
|
| 412 |
ast_assert(framed->format != ast_format_none); |
| 413 |
ao2_ref(framed->format, +1); |
| 414 |
return framed->format; |
| 415 |
} |
| 416 |
|
| 417 |
struct ast_format *ast_format_cap_get_best_by_type(const struct ast_format_cap *cap, enum ast_media_type type) |
| 418 |
{ |
| 419 |
int i; |
| 420 |
|
| 421 |
if (type == AST_MEDIA_TYPE_UNKNOWN) { |
| 422 |
return ast_format_cap_get_format(cap, 0); |
| 423 |
} |
| 424 |
|
| 425 |
for (i = 0; i < AST_VECTOR_SIZE(&cap->preference_order); i++) { |
| 426 |
struct format_cap_framed *framed = AST_VECTOR_GET(&cap->preference_order, i); |
| 427 |
|
| 428 |
if (ast_format_get_type(framed->format) == type) { |
| 429 |
ao2_ref(framed->format, +1); |
| 430 |
ast_assert(framed->format != ast_format_none); |
| 431 |
return framed->format; |
| 432 |
} |
| 433 |
} |
| 434 |
|
| 435 |
return NULL; |
| 436 |
} |
| 437 |
|
| 438 |
unsigned int ast_format_cap_get_framing(const struct ast_format_cap *cap) |
| 439 |
{ |
| 440 |
return (cap->framing != UINT_MAX) ? cap->framing : 0; |
| 441 |
} |
| 442 |
|
| 443 |
unsigned int ast_format_cap_get_format_framing(const struct ast_format_cap *cap, const struct ast_format *format) |
| 444 |
{ |
| 445 |
unsigned int framing; |
| 446 |
struct format_cap_framed_list *list; |
| 447 |
struct format_cap_framed *framed, *result = NULL; |
| 448 |
|
| 449 |
if (ast_format_get_codec_id(format) >= AST_VECTOR_SIZE(&cap->formats)) { |
| 450 |
return 0; |
| 451 |
} |
| 452 |
|
| 453 |
framing = cap->framing != UINT_MAX ? cap->framing : ast_format_get_default_ms(format); |
| 454 |
list = AST_VECTOR_GET_ADDR(&cap->formats, ast_format_get_codec_id(format)); |
| 455 |
|
| 456 |
AST_LIST_TRAVERSE(list, framed, entry) { |
| 457 |
enum ast_format_cmp_res res = ast_format_cmp(format, framed->format); |
| 458 |
|
| 459 |
if (res == AST_FORMAT_CMP_NOT_EQUAL) { |
| 460 |
continue; |
| 461 |
} |
| 462 |
|
| 463 |
result = framed; |
| 464 |
|
| 465 |
if (res == AST_FORMAT_CMP_EQUAL) { |
| 466 |
break; |
| 467 |
} |
| 468 |
} |
| 469 |
|
| 470 |
if (result && result->framing) { |
| 471 |
framing = result->framing; |
| 472 |
} |
| 473 |
|
| 474 |
return framing; |
| 475 |
} |
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
#define FORMAT_CAP_FRAMED_ELEM_CMP(elem, value) ((elem)->format == (value)) |
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
#define FORMAT_CAP_FRAMED_ELEM_CLEANUP(elem) ao2_cleanup((elem)) |
| 494 |
|
| 495 |
int ast_format_cap_remove(struct ast_format_cap *cap, struct ast_format *format) |
| 496 |
{ |
| 497 |
struct format_cap_framed_list *list; |
| 498 |
struct format_cap_framed *framed; |
| 499 |
|
| 500 |
ast_assert(format != NULL); |
| 501 |
|
| 502 |
if (ast_format_get_codec_id(format) >= AST_VECTOR_SIZE(&cap->formats)) { |
| 503 |
return -1; |
| 504 |
} |
| 505 |
|
| 506 |
list = AST_VECTOR_GET_ADDR(&cap->formats, ast_format_get_codec_id(format)); |
| 507 |
|
| 508 |
AST_LIST_TRAVERSE_SAFE_BEGIN(list, framed, entry) { |
| 509 |
if (!FORMAT_CAP_FRAMED_ELEM_CMP(framed, format)) { |
| 510 |
continue; |
| 511 |
} |
| 512 |
|
| 513 |
AST_LIST_REMOVE_CURRENT(entry); |
| 514 |
FORMAT_CAP_FRAMED_ELEM_CLEANUP(framed); |
| 515 |
break; |
| 516 |
} |
| 517 |
AST_LIST_TRAVERSE_SAFE_END; |
| 518 |
|
| 519 |
return AST_VECTOR_REMOVE_CMP_ORDERED(&cap->preference_order, format, |
| 520 |
FORMAT_CAP_FRAMED_ELEM_CMP, FORMAT_CAP_FRAMED_ELEM_CLEANUP); |
| 521 |
} |
| 522 |
|
| 523 |
void ast_format_cap_remove_by_type(struct ast_format_cap *cap, enum ast_media_type type) |
| 524 |
{ |
| 525 |
int idx; |
| 526 |
|
| 527 |
for (idx = 0; idx < AST_VECTOR_SIZE(&cap->formats); ++idx) { |
| 528 |
struct format_cap_framed_list *list = AST_VECTOR_GET_ADDR(&cap->formats, idx); |
| 529 |
struct format_cap_framed *framed; |
| 530 |
|
| 531 |
AST_LIST_TRAVERSE_SAFE_BEGIN(list, framed, entry) { |
| 532 |
if ((type != AST_MEDIA_TYPE_UNKNOWN) && |
| 533 |
ast_format_get_type(framed->format) != type) { |
| 534 |
continue; |
| 535 |
} |
| 536 |
|
| 537 |
AST_LIST_REMOVE_CURRENT(entry); |
| 538 |
AST_VECTOR_REMOVE_CMP_ORDERED(&cap->preference_order, framed->format, |
| 539 |
FORMAT_CAP_FRAMED_ELEM_CMP, FORMAT_CAP_FRAMED_ELEM_CLEANUP); |
| 540 |
ao2_ref(framed, -1); |
| 541 |
} |
| 542 |
AST_LIST_TRAVERSE_SAFE_END; |
| 543 |
} |
| 544 |
} |
| 545 |
|
| 546 |
struct ast_format *ast_format_cap_get_compatible_format(const struct ast_format_cap *cap, const struct ast_format *format) |
| 547 |
{ |
| 548 |
struct format_cap_framed_list *list; |
| 549 |
struct format_cap_framed *framed; |
| 550 |
struct ast_format *result = NULL; |
| 551 |
|
| 552 |
ast_assert(format != NULL); |
| 553 |
|
| 554 |
if (ast_format_get_codec_id(format) >= AST_VECTOR_SIZE(&cap->formats)) { |
| 555 |
return NULL; |
| 556 |
} |
| 557 |
|
| 558 |
list = AST_VECTOR_GET_ADDR(&cap->formats, ast_format_get_codec_id(format)); |
| 559 |
|
| 560 |
AST_LIST_TRAVERSE(list, framed, entry) { |
| 561 |
enum ast_format_cmp_res res = ast_format_cmp(format, framed->format); |
| 562 |
|
| 563 |
if (res == AST_FORMAT_CMP_NOT_EQUAL) { |
| 564 |
continue; |
| 565 |
} |
| 566 |
|
| 567 |
|
| 568 |
ao2_cleanup(result); |
| 569 |
|
| 570 |
result = ast_format_joint(format, framed->format); |
| 571 |
|
| 572 |
|
| 573 |
if (res == AST_FORMAT_CMP_EQUAL) { |
| 574 |
break; |
| 575 |
} |
| 576 |
} |
| 577 |
|
| 578 |
return result; |
| 579 |
} |
| 580 |
|
| 581 |
enum ast_format_cmp_res ast_format_cap_iscompatible_format(const struct ast_format_cap *cap, |
| 582 |
const struct ast_format *format) |
| 583 |
{ |
| 584 |
enum ast_format_cmp_res res = AST_FORMAT_CMP_NOT_EQUAL; |
| 585 |
struct format_cap_framed_list *list; |
| 586 |
struct format_cap_framed *framed; |
| 587 |
|
| 588 |
ast_assert(format != NULL); |
| 589 |
|
| 590 |
if (ast_format_get_codec_id(format) >= AST_VECTOR_SIZE(&cap->formats)) { |
| 591 |
return AST_FORMAT_CMP_NOT_EQUAL; |
| 592 |
} |
| 593 |
|
| 594 |
list = AST_VECTOR_GET_ADDR(&cap->formats, ast_format_get_codec_id(format)); |
| 595 |
|
| 596 |
AST_LIST_TRAVERSE(list, framed, entry) { |
| 597 |
enum ast_format_cmp_res cmp = ast_format_cmp(format, framed->format); |
| 598 |
|
| 599 |
if (cmp == AST_FORMAT_CMP_NOT_EQUAL) { |
| 600 |
continue; |
| 601 |
} |
| 602 |
|
| 603 |
res = cmp; |
| 604 |
|
| 605 |
if (res == AST_FORMAT_CMP_EQUAL) { |
| 606 |
break; |
| 607 |
} |
| 608 |
} |
| 609 |
|
| 610 |
return res; |
| 611 |
} |
| 612 |
|
| 613 |
int ast_format_cap_has_type(const struct ast_format_cap *cap, enum ast_media_type type) |
| 614 |
{ |
| 615 |
int idx; |
| 616 |
|
| 617 |
for (idx = 0; idx < AST_VECTOR_SIZE(&cap->preference_order); ++idx) { |
| 618 |
struct format_cap_framed *framed = AST_VECTOR_GET(&cap->preference_order, idx); |
| 619 |
|
| 620 |
if (ast_format_get_type(framed->format) == type) { |
| 621 |
return 1; |
| 622 |
} |
| 623 |
} |
| 624 |
|
| 625 |
return 0; |
| 626 |
} |
| 627 |
|
| 628 |
int ast_format_cap_get_compatible(const struct ast_format_cap *cap1, const struct ast_format_cap *cap2, |
| 629 |
struct ast_format_cap *result) |
| 630 |
{ |
| 631 |
int idx, res = 0; |
| 632 |
|
| 633 |
for (idx = 0; idx < AST_VECTOR_SIZE(&cap1->preference_order); ++idx) { |
| 634 |
struct format_cap_framed *framed = AST_VECTOR_GET(&cap1->preference_order, idx); |
| 635 |
struct ast_format *format; |
| 636 |
|
| 637 |
format = ast_format_cap_get_compatible_format(cap2, framed->format); |
| 638 |
if (!format) { |
| 639 |
continue; |
| 640 |
} |
| 641 |
|
| 642 |
res = ast_format_cap_append(result, format, framed->framing); |
| 643 |
ao2_ref(format, -1); |
| 644 |
|
| 645 |
if (res) { |
| 646 |
break; |
| 647 |
} |
| 648 |
} |
| 649 |
|
| 650 |
return res; |
| 651 |
} |
| 652 |
|
| 653 |
int ast_format_cap_iscompatible(const struct ast_format_cap *cap1, const struct ast_format_cap *cap2) |
| 654 |
{ |
| 655 |
int idx; |
| 656 |
|
| 657 |
for (idx = 0; idx < AST_VECTOR_SIZE(&cap1->preference_order); ++idx) { |
| 658 |
struct format_cap_framed *framed = AST_VECTOR_GET(&cap1->preference_order, idx); |
| 659 |
|
| 660 |
if (ast_format_cap_iscompatible_format(cap2, framed->format) != AST_FORMAT_CMP_NOT_EQUAL) { |
| 661 |
return 1; |
| 662 |
} |
| 663 |
} |
| 664 |
|
| 665 |
return 0; |
| 666 |
} |
| 667 |
|
| 668 |
static int internal_format_cap_identical(const struct ast_format_cap *cap1, const struct ast_format_cap *cap2) |
| 669 |
{ |
| 670 |
int idx; |
| 671 |
struct ast_format *tmp; |
| 672 |
|
| 673 |
for (idx = 0; idx < AST_VECTOR_SIZE(&cap1->preference_order); ++idx) { |
| 674 |
tmp = ast_format_cap_get_format(cap1, idx); |
| 675 |
|
| 676 |
if (ast_format_cap_iscompatible_format(cap2, tmp) != AST_FORMAT_CMP_EQUAL) { |
| 677 |
ao2_ref(tmp, -1); |
| 678 |
return 0; |
| 679 |
} |
| 680 |
|
| 681 |
ao2_ref(tmp, -1); |
| 682 |
} |
| 683 |
|
| 684 |
return 1; |
| 685 |
} |
| 686 |
|
| 687 |
int ast_format_cap_identical(const struct ast_format_cap *cap1, const struct ast_format_cap *cap2) |
| 688 |
{ |
| 689 |
if (AST_VECTOR_SIZE(&cap1->preference_order) != AST_VECTOR_SIZE(&cap2->preference_order)) { |
| 690 |
return 0; |
| 691 |
} |
| 692 |
|
| 693 |
if (!internal_format_cap_identical(cap1, cap2)) { |
| 694 |
return 0; |
| 695 |
} |
| 696 |
|
| 697 |
return internal_format_cap_identical(cap2, cap1); |
| 698 |
} |
| 699 |
|
| 700 |
static const char *__ast_format_cap_get_names(const struct ast_format_cap *cap, struct ast_str **buf, int append) |
| 701 |
{ |
| 702 |
int i; |
| 703 |
|
| 704 |
if (!buf || !*buf) { |
| 705 |
return ""; |
| 706 |
} |
| 707 |
|
| 708 |
if (append) { |
| 709 |
ast_str_append(buf, 0, "("); |
| 710 |
} else { |
| 711 |
ast_str_set(buf, 0, "("); |
| 712 |
} |
| 713 |
|
| 714 |
if (!cap || !AST_VECTOR_SIZE(&cap->preference_order)) { |
| 715 |
ast_str_append(buf, 0, "nothing)"); |
| 716 |
return ast_str_buffer(*buf); |
| 717 |
} |
| 718 |
|
| 719 |
for (i = 0; i < AST_VECTOR_SIZE(&cap->preference_order); ++i) { |
| 720 |
int res; |
| 721 |
struct format_cap_framed *framed = AST_VECTOR_GET(&cap->preference_order, i); |
| 722 |
|
| 723 |
res = ast_str_append(buf, 0, "%s%s", ast_format_get_name(framed->format), |
| 724 |
i < AST_VECTOR_SIZE(&cap->preference_order) - 1 ? "|" : ""); |
| 725 |
if (res < 0) { |
| 726 |
break; |
| 727 |
} |
| 728 |
} |
| 729 |
ast_str_append(buf, 0, ")"); |
| 730 |
|
| 731 |
return ast_str_buffer(*buf); |
| 732 |
} |
| 733 |
|
| 734 |
const char *ast_format_cap_get_names(const struct ast_format_cap *cap, struct ast_str **buf) |
| 735 |
{ |
| 736 |
return __ast_format_cap_get_names(cap, buf, 0); |
| 737 |
} |
| 738 |
|
| 739 |
const char *ast_format_cap_append_names(const struct ast_format_cap *cap, struct ast_str **buf) |
| 740 |
{ |
| 741 |
return __ast_format_cap_get_names(cap, buf, 1); |
| 742 |
} |
| 743 |
|
| 744 |
int ast_format_cap_empty(const struct ast_format_cap *cap) |
| 745 |
{ |
| 746 |
int count = ast_format_cap_count(cap); |
| 747 |
|
| 748 |
if (count > 1) { |
| 749 |
return 0; |
| 750 |
} |
| 751 |
|
| 752 |
if (count == 0 || AST_VECTOR_GET(&cap->preference_order, 0)->format == ast_format_none) { |
| 753 |
return 1; |
| 754 |
} |
| 755 |
|
| 756 |
return 0; |
| 757 |
} |