Files
asterisk/main/format_cap.c
Matthew Jordan a2c912e997 media formats: re-architect handling of media for performance improvements
In the old times media formats were represented using a bit field. This was
fast but had a few limitations.
 1. Asterisk was limited in how many formats it could handle.
 2. Formats, being a bit field, could not include any attribute information.
    A format was strictly its type, e.g., "this is ulaw".
This was changed in Asterisk 10 (see
https://wiki.asterisk.org/wiki/display/AST/Media+Architecture+Proposal for
notes on that work) which led to the creation of the ast_format structure.
This structure allowed Asterisk to handle attributes and bundle information
with a format.

Additionally, ast_format_cap was created to act as a container for multiple
formats that, together, formed the capability of some entity. Another
mechanism was added to allow logic to be registered which performed format
attribute negotiation. Everywhere throughout the codebase Asterisk was
changed to use this strategy.

Unfortunately, in software, there is no free lunch. These new capabilities
came at a cost.

Performance analysis and profiling showed that we spend an inordinate
amount of time comparing, copying, and generally manipulating formats and
their related structures. Basic prototyping has shown that a reasonably
large performance improvement could be made in this area. This patch is the
result of that project, which overhauled the media format architecture
and its usage in Asterisk to improve performance.

Generally, the new philosophy for handling formats is as follows:
 * The ast_format structure is reference counted. This removed a large amount
   of the memory allocations and copying that was done in prior versions.
 * In order to prevent race conditions while keeping things performant, the
   ast_format structure is immutable by convention and lock-free. Violate this
   tenet at your peril!
 * Because formats are reference counted, codecs are also reference counted.
   The Asterisk core generally provides built-in codecs and caches the
   ast_format structures created to represent them. Generally, to prevent
   inordinate amounts of module reference bumping, codecs and formats can be
   added at run-time but cannot be removed.
 * All compatibility with the bit field representation of codecs/formats has
   been moved to a compatibility API. The primary user of this representation
   is chan_iax2, which must continue to maintain its bit-field usage of formats
   for interoperability concerns.
 * When a format is negotiated with attributes, or when a format cannot be
   represented by one of the cached formats, a new format object is created or
   cloned from an existing format. That format may have the same codec
   underlying it, but is a different format than a version of the format with
   different attributes or without attributes.
 * While formats are reference counted objects, the reference count maintained
   on the format should be manipulated with care. Formats are generally cached
   and will persist for the lifetime of Asterisk and do not explicitly need
   to have their lifetime modified. An exception to this is when the user of a
   format does not know where the format came from *and* the user may outlive
   the provider of the format. This occurs, for example, when a format is read
   from a channel: the channel may have a format with attributes (hence,
   non-cached) and the user of the format may last longer than the channel (if
   the reference to the channel is released prior to the format's reference).

For more information on this work, see the API design notes:
  https://wiki.asterisk.org/wiki/display/AST/Media+Format+Rewrite

Finally, this work was the culmination of a large number of developer's
efforts. Extra thanks goes to Corey Farrell, who took on a large amount of the
work in the Asterisk core, chan_sip, and was an invaluable resource in peer
reviews throughout this project.

There were a substantial number of patches contributed during this work; the
following issues/patch names simply reflect some of the work (and will cause
the release scripts to give attribution to the individuals who work on them).

Reviews:
 https://reviewboard.asterisk.org/r/3814
 https://reviewboard.asterisk.org/r/3808
 https://reviewboard.asterisk.org/r/3805
 https://reviewboard.asterisk.org/r/3803
 https://reviewboard.asterisk.org/r/3801
 https://reviewboard.asterisk.org/r/3798
 https://reviewboard.asterisk.org/r/3800
 https://reviewboard.asterisk.org/r/3794
 https://reviewboard.asterisk.org/r/3793
 https://reviewboard.asterisk.org/r/3792
 https://reviewboard.asterisk.org/r/3791
 https://reviewboard.asterisk.org/r/3790
 https://reviewboard.asterisk.org/r/3789
 https://reviewboard.asterisk.org/r/3788
 https://reviewboard.asterisk.org/r/3787
 https://reviewboard.asterisk.org/r/3786
 https://reviewboard.asterisk.org/r/3784
 https://reviewboard.asterisk.org/r/3783
 https://reviewboard.asterisk.org/r/3778
 https://reviewboard.asterisk.org/r/3774
 https://reviewboard.asterisk.org/r/3775
 https://reviewboard.asterisk.org/r/3772
 https://reviewboard.asterisk.org/r/3761
 https://reviewboard.asterisk.org/r/3754
 https://reviewboard.asterisk.org/r/3753
 https://reviewboard.asterisk.org/r/3751
 https://reviewboard.asterisk.org/r/3750
 https://reviewboard.asterisk.org/r/3748
 https://reviewboard.asterisk.org/r/3747
 https://reviewboard.asterisk.org/r/3746
 https://reviewboard.asterisk.org/r/3742
 https://reviewboard.asterisk.org/r/3740
 https://reviewboard.asterisk.org/r/3739
 https://reviewboard.asterisk.org/r/3738
 https://reviewboard.asterisk.org/r/3737
 https://reviewboard.asterisk.org/r/3736
 https://reviewboard.asterisk.org/r/3734
 https://reviewboard.asterisk.org/r/3722
 https://reviewboard.asterisk.org/r/3713
 https://reviewboard.asterisk.org/r/3703
 https://reviewboard.asterisk.org/r/3689
 https://reviewboard.asterisk.org/r/3687
 https://reviewboard.asterisk.org/r/3674
 https://reviewboard.asterisk.org/r/3671
 https://reviewboard.asterisk.org/r/3667
 https://reviewboard.asterisk.org/r/3665
 https://reviewboard.asterisk.org/r/3625
 https://reviewboard.asterisk.org/r/3602
 https://reviewboard.asterisk.org/r/3519
 https://reviewboard.asterisk.org/r/3518
 https://reviewboard.asterisk.org/r/3516
 https://reviewboard.asterisk.org/r/3515
 https://reviewboard.asterisk.org/r/3512
 https://reviewboard.asterisk.org/r/3506
 https://reviewboard.asterisk.org/r/3413
 https://reviewboard.asterisk.org/r/3410
 https://reviewboard.asterisk.org/r/3387
 https://reviewboard.asterisk.org/r/3388
 https://reviewboard.asterisk.org/r/3389
 https://reviewboard.asterisk.org/r/3390
 https://reviewboard.asterisk.org/r/3321
 https://reviewboard.asterisk.org/r/3320
 https://reviewboard.asterisk.org/r/3319
 https://reviewboard.asterisk.org/r/3318
 https://reviewboard.asterisk.org/r/3266
 https://reviewboard.asterisk.org/r/3265
 https://reviewboard.asterisk.org/r/3234
 https://reviewboard.asterisk.org/r/3178

ASTERISK-23114 #close
Reported by: mjordan
  media_formats_translation_core.diff uploaded by kharwell (License 6464)
  rb3506.diff uploaded by mjordan (License 6283)
  media_format_app_file.diff uploaded by kharwell (License 6464) 
  misc-2.diff uploaded by file (License 5000)
  chan_mild-3.diff uploaded by file (License 5000) 
  chan_obscure.diff uploaded by file (License 5000) 
  jingle.diff uploaded by file (License 5000) 
  funcs.diff uploaded by file (License 5000) 
  formats.diff uploaded by file (License 5000) 
  core.diff uploaded by file (License 5000) 
  bridges.diff uploaded by file (License 5000) 
  mf-codecs-2.diff uploaded by file (License 5000) 
  mf-app_fax.diff uploaded by file (License 5000) 
  mf-apps-3.diff uploaded by file (License 5000) 
  media-formats-3.diff uploaded by file (License 5000) 

ASTERISK-23715
  rb3713.patch uploaded by coreyfarrell (License 5909)
  rb3689.patch uploaded by mjordan (License 6283)
  
ASTERISK-23957
  rb3722.patch uploaded by mjordan (License 6283) 
  mf-attributes-3.diff uploaded by file (License 5000) 

ASTERISK-23958
Tested by: jrose
  rb3822.patch uploaded by coreyfarrell (License 5909) 
  rb3800.patch uploaded by jrose (License 6182)
  chan_sip.diff uploaded by mjordan (License 6283) 
  rb3747.patch uploaded by jrose (License 6182)

ASTERISK-23959 #close
Tested by: sgriepentrog, mjordan, coreyfarrell
  sip_cleanup.diff uploaded by opticron (License 6273)
  chan_sip_caps.diff uploaded by mjordan (License 6283) 
  rb3751.patch uploaded by coreyfarrell (License 5909) 
  chan_sip-3.diff uploaded by file (License 5000) 

ASTERISK-23960 #close
Tested by: opticron
  direct_media.diff uploaded by opticron (License 6273) 
  pjsip-direct-media.diff uploaded by file (License 5000) 
  format_cap_remove.diff uploaded by opticron (License 6273) 
  media_format_fixes.diff uploaded by opticron (License 6273) 
  chan_pjsip-2.diff uploaded by file (License 5000) 

ASTERISK-23966 #close
Tested by: rmudgett
  rb3803.patch uploaded by rmudgetti (License 5621)
  chan_dahdi.diff uploaded by file (License 5000) 
  
ASTERISK-24064 #close
Tested by: coreyfarrell, mjordan, opticron, file, rmudgett, sgriepentrog, jrose
  rb3814.patch uploaded by rmudgett (License 5621) 
  moh_cleanup.diff uploaded by opticron (License 6273) 
  bridge_leak.diff uploaded by opticron (License 6273) 
  translate.diff uploaded by file (License 5000) 
  rb3795.patch uploaded by rmudgett (License 5621) 
  tls_fix.diff uploaded by mjordan (License 6283) 
  fax-mf-fix-2.diff uploaded by file (License 5000) 
  rtp_transfer_stuff uploaded by mjordan (License 6283) 
  rb3787.patch uploaded by rmudgett (License 5621) 
  media-formats-explicit-translate-format-3.diff uploaded by file (License 5000) 
  format_cache_case_fix.diff uploaded by opticron (License 6273) 
  rb3774.patch uploaded by rmudgett (License 5621) 
  rb3775.patch uploaded by rmudgett (License 5621) 
  rtp_engine_fix.diff uploaded by opticron (License 6273) 
  rtp_crash_fix.diff uploaded by opticron (License 6273) 
  rb3753.patch uploaded by mjordan (License 6283) 
  rb3750.patch uploaded by mjordan (License 6283) 
  rb3748.patch uploaded by rmudgett (License 5621) 
  media_format_fixes.diff uploaded by opticron (License 6273) 
  rb3740.patch uploaded by mjordan (License 6283) 
  rb3739.patch uploaded by mjordan (License 6283) 
  rb3734.patch uploaded by mjordan (License 6283) 
  rb3689.patch uploaded by mjordan (License 6283) 
  rb3674.patch uploaded by coreyfarrell (License 5909) 
  rb3671.patch uploaded by coreyfarrell (License 5909) 
  rb3667.patch uploaded by coreyfarrell (License 5909) 
  rb3665.patch uploaded by mjordan (License 6283) 
  rb3625.patch uploaded by coreyfarrell (License 5909) 
  rb3602.patch uploaded by coreyfarrell (License 5909) 
  format_compatibility-2.diff uploaded by file (License 5000) 
  core.diff uploaded by file (License 5000) 
  


git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@419044 65c4cc65-6c06-0410-ace0-fbb531ad65f3
2014-07-20 22:06:33 +00:00

703 lines
18 KiB
C

/*
* Asterisk -- An open source telephony toolkit.
*
* Copyright (C) 2014, Digium, Inc.
*
* Joshua Colp <jcolp@digium.com>
*
* See http://www.asterisk.org for more information about
* the Asterisk project. Please do not directly contact
* any of the maintainers of this project for assistance;
* the project provides a web site, mailing lists and IRC
* channels for your use.
*
* This program is free software, distributed under the terms of
* the GNU General Public License Version 2. See the LICENSE file
* at the top of the source tree.
*/
/*! \file
*
* \brief Format Capabilities API
*
* \author Joshua Colp <jcolp@digium.com>
*/
/*** MODULEINFO
<support_level>core</support_level>
***/
#include "asterisk.h"
ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
#include "asterisk/logger.h"
#include "asterisk/format.h"
#include "asterisk/format_cap.h"
#include "asterisk/format_cache.h"
#include "asterisk/codec.h"
#include "asterisk/astobj2.h"
#include "asterisk/strings.h"
#include "asterisk/vector.h"
#include "asterisk/linkedlists.h"
#include "asterisk/utils.h"
/*! \brief Structure used for capability formats, adds framing */
struct format_cap_framed {
/*! \brief A pointer to the format */
struct ast_format *format;
/*! \brief The format framing size */
unsigned int framing;
/*! \brief Linked list information */
AST_LIST_ENTRY(format_cap_framed) entry;
};
/*! \brief Format capabilities structure, holds formats + preference order + etc */
struct ast_format_cap {
/*! \brief Vector of formats, indexed using the codec identifier */
AST_VECTOR(, struct format_cap_framed_list) formats;
/*! \brief Vector of formats, added in preference order */
AST_VECTOR(, struct format_cap_framed *) preference_order;
/*! \brief Global framing size, applies to all formats if no framing present on format */
unsigned int framing;
};
/*! \brief Linked list for formats */
AST_LIST_HEAD_NOLOCK(format_cap_framed_list, format_cap_framed);
/*! \brief Dummy empty list for when we are inserting a new list */
static const struct format_cap_framed_list format_cap_framed_list_empty = AST_LIST_HEAD_NOLOCK_INIT_VALUE;
/*! \brief Destructor for format capabilities structure */
static void format_cap_destroy(void *obj)
{
struct ast_format_cap *cap = obj;
int idx;
for (idx = 0; idx < AST_VECTOR_SIZE(&cap->formats); idx++) {
struct format_cap_framed_list *list = AST_VECTOR_GET_ADDR(&cap->formats, idx);
struct format_cap_framed *framed;
while ((framed = AST_LIST_REMOVE_HEAD(list, entry))) {
ao2_ref(framed, -1);
}
}
AST_VECTOR_FREE(&cap->formats);
for (idx = 0; idx < AST_VECTOR_SIZE(&cap->preference_order); idx++) {
struct format_cap_framed *framed = AST_VECTOR_GET(&cap->preference_order, idx);
/* This will always be non-null, unlike formats */
ao2_ref(framed, -1);
}
AST_VECTOR_FREE(&cap->preference_order);
}
static inline void format_cap_init(struct ast_format_cap *cap, enum ast_format_cap_flags flags)
{
AST_VECTOR_INIT(&cap->formats, 0);
/* TODO: Look at common usage of this and determine a good starting point */
AST_VECTOR_INIT(&cap->preference_order, 5);
cap->framing = UINT_MAX;
}
struct ast_format_cap *__ast_format_cap_alloc(enum ast_format_cap_flags flags)
{
struct ast_format_cap *cap;
cap = ao2_alloc_options(sizeof(*cap), format_cap_destroy, AO2_ALLOC_OPT_LOCK_NOLOCK);
if (!cap) {
return NULL;
}
format_cap_init(cap, flags);
return cap;
}
struct ast_format_cap *__ast_format_cap_alloc_debug(enum ast_format_cap_flags flags, const char *tag, const char *file, int line, const char *func)
{
struct ast_format_cap *cap;
cap = __ao2_alloc_debug(sizeof(*cap), format_cap_destroy, AO2_ALLOC_OPT_LOCK_NOLOCK, S_OR(tag, "ast_format_cap_alloc"), file, line, func, 1);
if (!cap) {
return NULL;
}
format_cap_init(cap, flags);
return cap;
}
void ast_format_cap_set_framing(struct ast_format_cap *cap, unsigned int framing)
{
cap->framing = framing;
}
/*! \brief Destructor for format capabilities framed structure */
static void format_cap_framed_destroy(void *obj)
{
struct format_cap_framed *framed = obj;
ao2_cleanup(framed->format);
}
static inline int format_cap_framed_init(struct format_cap_framed *framed, struct ast_format_cap *cap, struct ast_format *format, unsigned int framing)
{
struct format_cap_framed_list *list;
framed->framing = framing;
if (ast_format_get_codec_id(format) >= AST_VECTOR_SIZE(&cap->formats)) {
if (AST_VECTOR_INSERT(&cap->formats, ast_format_get_codec_id(format), format_cap_framed_list_empty)) {
ao2_ref(framed, -1);
return -1;
}
}
list = AST_VECTOR_GET_ADDR(&cap->formats, ast_format_get_codec_id(format));
/* Order doesn't matter for formats, so insert at the head for performance reasons */
ao2_ref(framed, +1);
AST_LIST_INSERT_HEAD(list, framed, entry);
/* This takes the allocation reference */
AST_VECTOR_APPEND(&cap->preference_order, framed);
cap->framing = MIN(cap->framing, framing ? framing : ast_format_get_default_ms(format));
return 0;
}
/*! \internal \brief Determine if \c format is in \c cap */
static int format_in_format_cap(struct ast_format_cap *cap, struct ast_format *format)
{
struct format_cap_framed *framed;
int i;
for (i = 0; i < AST_VECTOR_SIZE(&cap->preference_order); i++) {
framed = AST_VECTOR_GET(&cap->preference_order, i);
if (ast_format_get_codec_id(format) == ast_format_get_codec_id(framed->format)) {
return 1;
}
}
return 0;
}
int __ast_format_cap_append(struct ast_format_cap *cap, struct ast_format *format, unsigned int framing)
{
struct format_cap_framed *framed;
ast_assert(format != NULL);
if (format_in_format_cap(cap, format)) {
return 0;
}
framed = ao2_alloc_options(sizeof(*framed), format_cap_framed_destroy, AO2_ALLOC_OPT_LOCK_NOLOCK);
if (!framed) {
return -1;
}
framed->format = ao2_bump(format);
return format_cap_framed_init(framed, cap, format, framing);
}
int __ast_format_cap_append_debug(struct ast_format_cap *cap, struct ast_format *format, unsigned int framing, const char *tag, const char *file, int line, const char *func)
{
struct format_cap_framed *framed;
ast_assert(format != NULL);
if (format_in_format_cap(cap, format)) {
return 0;
}
framed = ao2_alloc_options(sizeof(*framed), format_cap_framed_destroy, AO2_ALLOC_OPT_LOCK_NOLOCK);
if (!framed) {
return -1;
}
__ao2_ref_debug(format, +1, S_OR(tag, "ast_format_cap_append"), file, line, func);
framed->format = format;
return format_cap_framed_init(framed, cap, format, framing);
}
int ast_format_cap_append_by_type(struct ast_format_cap *cap, enum ast_media_type type)
{
int id;
for (id = 1; id < ast_codec_get_max(); ++id) {
struct ast_codec *codec = ast_codec_get_by_id(id);
struct ast_format *format;
int res;
if (!codec) {
continue;
}
if ((type != AST_MEDIA_TYPE_UNKNOWN) && codec->type != type) {
ao2_ref(codec, -1);
continue;
}
format = ast_format_create(codec);
ao2_ref(codec, -1);
if (!format) {
return -1;
}
/* Use the global framing or default framing of the codec */
res = ast_format_cap_append(cap, format, 0);
ao2_ref(format, -1);
if (res) {
return -1;
}
}
return 0;
}
int ast_format_cap_append_from_cap(struct ast_format_cap *dst, const struct ast_format_cap *src,
enum ast_media_type type)
{
int idx, res = 0;
for (idx = 0; (idx < AST_VECTOR_SIZE(&src->preference_order)) && !res; ++idx) {
struct format_cap_framed *framed = AST_VECTOR_GET(&src->preference_order, idx);
if (type == AST_MEDIA_TYPE_UNKNOWN || ast_format_get_type(framed->format) == type) {
res = ast_format_cap_append(dst, framed->format, framed->framing);
}
}
return res;
}
static int format_cap_replace(struct ast_format_cap *cap, struct ast_format *format, unsigned int framing)
{
struct format_cap_framed *framed;
int i;
ast_assert(format != NULL);
for (i = 0; i < AST_VECTOR_SIZE(&cap->preference_order); i++) {
framed = AST_VECTOR_GET(&cap->preference_order, i);
if (ast_format_get_codec_id(format) == ast_format_get_codec_id(framed->format)) {
ao2_t_replace(framed->format, format, "replacing with new format");
framed->framing = framing;
return 0;
}
}
return -1;
}
void ast_format_cap_replace_from_cap(struct ast_format_cap *dst, const struct ast_format_cap *src,
enum ast_media_type type)
{
int idx;
for (idx = 0; (idx < AST_VECTOR_SIZE(&src->preference_order)); ++idx) {
struct format_cap_framed *framed = AST_VECTOR_GET(&src->preference_order, idx);
if (type == AST_MEDIA_TYPE_UNKNOWN || ast_format_get_type(framed->format) == type) {
format_cap_replace(dst, framed->format, framed->framing);
}
}
}
int ast_format_cap_update_by_allow_disallow(struct ast_format_cap *cap, const char *list, int allowing)
{
int res = 0, all = 0, iter_allowing;
char *parse = NULL, *this = NULL, *psize = NULL;
parse = ast_strdupa(list);
while ((this = strsep(&parse, ","))) {
int framems = 0;
struct ast_format *format = NULL;
iter_allowing = allowing;
if (*this == '!') {
this++;
iter_allowing = !allowing;
}
if ((psize = strrchr(this, ':'))) {
*psize++ = '\0';
ast_debug(1, "Packetization for codec: %s is %s\n", this, psize);
if (!sscanf(psize, "%30d", &framems) || (framems < 0)) {
framems = 0;
res = -1;
ast_log(LOG_WARNING, "Bad packetization value for codec %s\n", this);
continue;
}
}
all = strcasecmp(this, "all") ? 0 : 1;
if (!all && !(format = ast_format_cache_get(this))) {
ast_log(LOG_WARNING, "Cannot %s unknown format '%s'\n", iter_allowing ? "allow" : "disallow", this);
res = -1;
continue;
}
if (cap) {
if (iter_allowing) {
if (all) {
ast_format_cap_append_by_type(cap, AST_MEDIA_TYPE_UNKNOWN);
} else {
ast_format_cap_append(cap, format, framems);
}
} else {
if (all) {
ast_format_cap_remove_by_type(cap, AST_MEDIA_TYPE_UNKNOWN);
} else {
ast_format_cap_remove(cap, format);
}
}
}
ao2_cleanup(format);
}
return res;
}
size_t ast_format_cap_count(const struct ast_format_cap *cap)
{
return AST_VECTOR_SIZE(&cap->preference_order);
}
struct ast_format *ast_format_cap_get_format(const struct ast_format_cap *cap, int position)
{
struct format_cap_framed *framed;
ast_assert(position < AST_VECTOR_SIZE(&cap->preference_order));
if (position >= AST_VECTOR_SIZE(&cap->preference_order)) {
return NULL;
}
framed = AST_VECTOR_GET(&cap->preference_order, position);
ast_assert(framed->format != ast_format_none);
ao2_ref(framed->format, +1);
return framed->format;
}
struct ast_format *ast_format_cap_get_best_by_type(const struct ast_format_cap *cap, enum ast_media_type type)
{
int i;
if (type == AST_MEDIA_TYPE_UNKNOWN) {
return ast_format_cap_get_format(cap, 0);
}
for (i = 0; i < AST_VECTOR_SIZE(&cap->preference_order); i++) {
struct format_cap_framed *framed = AST_VECTOR_GET(&cap->preference_order, i);
if (ast_format_get_type(framed->format) == type) {
ao2_ref(framed->format, +1);
ast_assert(framed->format != ast_format_none);
return framed->format;
}
}
return NULL;
}
unsigned int ast_format_cap_get_framing(const struct ast_format_cap *cap)
{
return (cap->framing != UINT_MAX) ? cap->framing : 0;
}
unsigned int ast_format_cap_get_format_framing(const struct ast_format_cap *cap, const struct ast_format *format)
{
unsigned int framing;
struct format_cap_framed_list *list;
struct format_cap_framed *framed, *result = NULL;
if (ast_format_get_codec_id(format) >= AST_VECTOR_SIZE(&cap->formats)) {
return 0;
}
framing = cap->framing != UINT_MAX ? cap->framing : ast_format_get_default_ms(format);
list = AST_VECTOR_GET_ADDR(&cap->formats, ast_format_get_codec_id(format));
AST_LIST_TRAVERSE(list, framed, entry) {
enum ast_format_cmp_res res = ast_format_cmp(format, framed->format);
if (res == AST_FORMAT_CMP_NOT_EQUAL) {
continue;
}
result = framed;
if (res == AST_FORMAT_CMP_EQUAL) {
break;
}
}
if (result && result->framing) {
framing = result->framing;
}
return framing;
}
/*!
* \brief format_cap_framed comparator for AST_VECTOR_REMOVE_CMP_ORDERED()
*
* \param elem Element to compare against
* \param value Value to compare with the vector element.
*
* \return 0 if element does not match.
* \return Non-zero if element matches.
*/
#define FORMAT_CAP_FRAMED_ELEM_CMP(elem, value) ((elem)->format == (value))
/*!
* \brief format_cap_framed vector element cleanup.
*
* \param elem Element to cleanup
*
* \return Nothing
*/
#define FORMAT_CAP_FRAMED_ELEM_CLEANUP(elem) ao2_cleanup((elem))
int ast_format_cap_remove(struct ast_format_cap *cap, struct ast_format *format)
{
struct format_cap_framed_list *list;
struct format_cap_framed *framed;
ast_assert(format != NULL);
if (ast_format_get_codec_id(format) >= AST_VECTOR_SIZE(&cap->formats)) {
return -1;
}
list = AST_VECTOR_GET_ADDR(&cap->formats, ast_format_get_codec_id(format));
AST_LIST_TRAVERSE_SAFE_BEGIN(list, framed, entry) {
if (!FORMAT_CAP_FRAMED_ELEM_CMP(framed, format)) {
continue;
}
AST_LIST_REMOVE_CURRENT(entry);
FORMAT_CAP_FRAMED_ELEM_CLEANUP(framed);
break;
}
AST_LIST_TRAVERSE_SAFE_END;
return AST_VECTOR_REMOVE_CMP_ORDERED(&cap->preference_order, format,
FORMAT_CAP_FRAMED_ELEM_CMP, FORMAT_CAP_FRAMED_ELEM_CLEANUP);
}
void ast_format_cap_remove_by_type(struct ast_format_cap *cap, enum ast_media_type type)
{
int idx;
for (idx = 0; idx < AST_VECTOR_SIZE(&cap->formats); ++idx) {
struct format_cap_framed_list *list = AST_VECTOR_GET_ADDR(&cap->formats, idx);
struct format_cap_framed *framed;
AST_LIST_TRAVERSE_SAFE_BEGIN(list, framed, entry) {
if ((type != AST_MEDIA_TYPE_UNKNOWN) &&
ast_format_get_type(framed->format) != type) {
continue;
}
AST_LIST_REMOVE_CURRENT(entry);
AST_VECTOR_REMOVE_CMP_ORDERED(&cap->preference_order, framed->format,
FORMAT_CAP_FRAMED_ELEM_CMP, FORMAT_CAP_FRAMED_ELEM_CLEANUP);
ao2_ref(framed, -1);
}
AST_LIST_TRAVERSE_SAFE_END;
}
}
struct ast_format *ast_format_cap_get_compatible_format(const struct ast_format_cap *cap, const struct ast_format *format)
{
struct format_cap_framed_list *list;
struct format_cap_framed *framed;
struct ast_format *result = NULL;
ast_assert(format != NULL);
if (ast_format_get_codec_id(format) >= AST_VECTOR_SIZE(&cap->formats)) {
return NULL;
}
list = AST_VECTOR_GET_ADDR(&cap->formats, ast_format_get_codec_id(format));
AST_LIST_TRAVERSE(list, framed, entry) {
enum ast_format_cmp_res res = ast_format_cmp(format, framed->format);
if (res == AST_FORMAT_CMP_NOT_EQUAL) {
continue;
}
/* Replace any current result, this one will also be a subset OR an exact match */
ao2_cleanup(result);
result = ast_format_joint(format, framed->format);
/* If it's a match we can do no better so return asap */
if (res == AST_FORMAT_CMP_EQUAL) {
break;
}
}
return result;
}
enum ast_format_cmp_res ast_format_cap_iscompatible_format(const struct ast_format_cap *cap,
const struct ast_format *format)
{
enum ast_format_cmp_res res = AST_FORMAT_CMP_NOT_EQUAL;
struct format_cap_framed_list *list;
struct format_cap_framed *framed;
ast_assert(format != NULL);
if (ast_format_get_codec_id(format) >= AST_VECTOR_SIZE(&cap->formats)) {
return AST_FORMAT_CMP_NOT_EQUAL;
}
list = AST_VECTOR_GET_ADDR(&cap->formats, ast_format_get_codec_id(format));
AST_LIST_TRAVERSE(list, framed, entry) {
enum ast_format_cmp_res cmp = ast_format_cmp(format, framed->format);
if (cmp == AST_FORMAT_CMP_NOT_EQUAL) {
continue;
}
res = cmp;
if (res == AST_FORMAT_CMP_EQUAL) {
break;
}
}
return res;
}
int ast_format_cap_has_type(const struct ast_format_cap *cap, enum ast_media_type type)
{
int idx;
for (idx = 0; idx < AST_VECTOR_SIZE(&cap->preference_order); ++idx) {
struct format_cap_framed *framed = AST_VECTOR_GET(&cap->preference_order, idx);
if (ast_format_get_type(framed->format) == type) {
return 1;
}
}
return 0;
}
int ast_format_cap_get_compatible(const struct ast_format_cap *cap1, const struct ast_format_cap *cap2,
struct ast_format_cap *result)
{
int idx, res = 0;
for (idx = 0; idx < AST_VECTOR_SIZE(&cap1->preference_order); ++idx) {
struct format_cap_framed *framed = AST_VECTOR_GET(&cap1->preference_order, idx);
struct ast_format *format;
format = ast_format_cap_get_compatible_format(cap2, framed->format);
if (!format) {
continue;
}
res = ast_format_cap_append(result, format, framed->framing);
ao2_ref(format, -1);
if (res) {
break;
}
}
return res;
}
int ast_format_cap_iscompatible(const struct ast_format_cap *cap1, const struct ast_format_cap *cap2)
{
int idx;
for (idx = 0; idx < AST_VECTOR_SIZE(&cap1->preference_order); ++idx) {
struct format_cap_framed *framed = AST_VECTOR_GET(&cap1->preference_order, idx);
if (ast_format_cap_iscompatible_format(cap2, framed->format) != AST_FORMAT_CMP_NOT_EQUAL) {
return 1;
}
}
return 0;
}
static int internal_format_cap_identical(const struct ast_format_cap *cap1, const struct ast_format_cap *cap2)
{
int idx;
struct ast_format *tmp;
for (idx = 0; idx < AST_VECTOR_SIZE(&cap1->preference_order); ++idx) {
tmp = ast_format_cap_get_format(cap1, idx);
if (ast_format_cap_iscompatible_format(cap2, tmp) != AST_FORMAT_CMP_EQUAL) {
ao2_ref(tmp, -1);
return 0;
}
ao2_ref(tmp, -1);
}
return 1;
}
int ast_format_cap_identical(const struct ast_format_cap *cap1, const struct ast_format_cap *cap2)
{
if (AST_VECTOR_SIZE(&cap1->preference_order) != AST_VECTOR_SIZE(&cap2->preference_order)) {
return 0; /* if they are not the same size, they are not identical */
}
if (!internal_format_cap_identical(cap1, cap2)) {
return 0;
}
return internal_format_cap_identical(cap2, cap1);
}
const char *ast_format_cap_get_names(struct ast_format_cap *cap, struct ast_str **buf)
{
int i;
ast_str_set(buf, 0, "(");
if (!AST_VECTOR_SIZE(&cap->preference_order)) {
ast_str_append(buf, 0, "nothing)");
return ast_str_buffer(*buf);
}
for (i = 0; i < AST_VECTOR_SIZE(&cap->preference_order); ++i) {
int res;
struct format_cap_framed *framed = AST_VECTOR_GET(&cap->preference_order, i);
res = ast_str_append(buf, 0, "%s%s", ast_format_get_name(framed->format),
i < AST_VECTOR_SIZE(&cap->preference_order) - 1 ? "|" : "");
if (res < 0) {
break;
}
}
ast_str_append(buf, 0, ")");
return ast_str_buffer(*buf);
}