diff options
author | Matthew Jordan <mjordan@digium.com> | 2014-07-20 22:06:33 +0000 |
---|---|---|
committer | Matthew Jordan <mjordan@digium.com> | 2014-07-20 22:06:33 +0000 |
commit | a2c912e9972c91973ea66902d217746133f96026 (patch) | |
tree | 50e01d14ba62950e3f78766d5ba435ba51ca327d /tests/test_format_api.c | |
parent | b299052e203807c9a2111eb2cd919246d7589cb3 (diff) |
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
Diffstat (limited to 'tests/test_format_api.c')
-rw-r--r-- | tests/test_format_api.c | 859 |
1 files changed, 0 insertions, 859 deletions
diff --git a/tests/test_format_api.c b/tests/test_format_api.c deleted file mode 100644 index 6cc349570..000000000 --- a/tests/test_format_api.c +++ /dev/null @@ -1,859 +0,0 @@ -/* - * Asterisk -- An open source telephony toolkit. - * - * Copyright (C) 2010, Digium, Inc. - * - * David Vossel <dvossel@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 Tests for the ast_event API - * - * \author David Vossel <dvossel@digium.com> - * - * \ingroup tests - * - */ - -/*** MODULEINFO - <depend>TEST_FRAMEWORK</depend> - <support_level>core</support_level> - ***/ - -#include "asterisk.h" - -ASTERISK_FILE_VERSION(__FILE__, "$Revision$") - -#include "asterisk/module.h" -#include "asterisk/test.h" -#include "asterisk/format.h" -#include "asterisk/format_cap.h" -#include "asterisk/strings.h" - -/*! These are the keys for accessing attributes */ -enum test_attr_keys { - TEST_ATTR_KEY_SAMP_RATE, - TEST_ATTR_KEY_STRING, -}; - -/*! These are the values for the TEST_ATTR_KEY_SAMP_RATE key */ -enum test_attr_vals_samp { - TEST_ATTR_VAL_SAMP_8KHZ = (1 << 0), - TEST_ATTR_VAL_SAMP_12KHZ = (1 << 1), - TEST_ATTR_VAL_SAMP_16KHZ = (1 << 2), - TEST_ATTR_VAL_SAMP_32KHZ = (1 << 3), - TEST_ATTR_VAL_SAMP_48KHZ = (1 << 4), -}; - -/*! This is the attribute structure used for our test interface. */ -struct test_attr { - enum test_attr_vals_samp samp_flags; - char string[32]; -}; - -static enum ast_format_cmp_res test_cmp(const struct ast_format_attr *fattr1, const struct ast_format_attr *fattr2) -{ - struct test_attr *attr1 = (struct test_attr *) fattr1; - struct test_attr *attr2 = (struct test_attr *) fattr2; - - if ((attr1->samp_flags == attr2->samp_flags) && - !(strcmp(attr1->string, attr2->string))) { - return AST_FORMAT_CMP_EQUAL; - } - if ((attr1->samp_flags != (attr1->samp_flags & attr2->samp_flags)) || - (!ast_strlen_zero(attr1->string) && strcmp(attr1->string, attr2->string))) { - return AST_FORMAT_CMP_NOT_EQUAL; - } - return AST_FORMAT_CMP_SUBSET; -} - -static int test_getjoint(const struct ast_format_attr *fattr1, const struct ast_format_attr *fattr2, struct ast_format_attr *result) -{ - struct test_attr *attr1 = (struct test_attr *) fattr1; - struct test_attr *attr2 = (struct test_attr *) fattr2; - struct test_attr *attr_res = (struct test_attr *) result; - int joint = -1; - - attr_res->samp_flags = (attr1->samp_flags & attr2->samp_flags); - - if (attr_res->samp_flags) { - joint = 0; - } - - if (!strcmp(attr1->string, attr2->string)) { - ast_copy_string(attr_res->string, attr1->string, sizeof(attr_res->string)); - joint = 0; - } - - return joint; -} - -static void test_set(struct ast_format_attr *fattr, va_list ap) -{ - enum test_attr_keys key; - struct test_attr *attr = (struct test_attr *) fattr; - char *string; - - for (key = va_arg(ap, int); - key != AST_FORMAT_ATTR_END; - key = va_arg(ap, int)) - { - switch (key) { - case TEST_ATTR_KEY_SAMP_RATE: - attr->samp_flags = (va_arg(ap, int) | attr->samp_flags); - break; - case TEST_ATTR_KEY_STRING: - string = va_arg(ap, char *); - if (!ast_strlen_zero(string)) { - ast_copy_string(attr->string, string, sizeof(attr->string)); - } - break; - default: - ast_log(LOG_WARNING, "unknown attribute type %d\n", key); - } - } -} - -/*! uLaw does not actually have any attributes associated with it. - * This is just for the purpose of testing. We are guaranteed there - * will never exist a interface for uLaw already. */ -static struct ast_format_attr_interface test_interface = { - .id = AST_FORMAT_TESTLAW, - .format_attr_cmp = test_cmp, - .format_attr_get_joint = test_getjoint, - .format_attr_set = test_set -}; - -/*! - * \internal - */ -AST_TEST_DEFINE(format_test1) -{ - struct ast_format format1 = { 0, }; - struct ast_format format2 = { 0, }; - struct ast_format joint = { 0, }; - - switch (cmd) { - case TEST_INIT: - info->name = "ast_format_test1"; - info->category = "/main/format/"; - info->summary = "Test ast_format with attributes."; - info->description = - "This test exercises the Ast Format API by creating and registering " - "a custom ast_format_attr_interface and performing various function " - "calls on ast_formats using the interface. "; - return AST_TEST_NOT_RUN; - case TEST_EXECUTE: - break; - } - - if (ast_format_attr_reg_interface(&test_interface)) { - ast_test_status_update(test, "test_interface failed to register.\n"); - return AST_TEST_FAIL; - } - - /* set a format with a single attribute. */ - ast_format_set(&format1, AST_FORMAT_TESTLAW, 1, - TEST_ATTR_KEY_SAMP_RATE, TEST_ATTR_VAL_SAMP_8KHZ, - AST_FORMAT_ATTR_END); - if (ast_format_isset(&format1, TEST_ATTR_KEY_SAMP_RATE, TEST_ATTR_VAL_SAMP_8KHZ, AST_FORMAT_ATTR_END)) { - ast_test_status_update(test, "format1 did not set number attribute correctly.\n"); - return AST_TEST_FAIL; - } - if (!ast_format_isset(&format1, TEST_ATTR_KEY_SAMP_RATE, TEST_ATTR_VAL_SAMP_12KHZ, AST_FORMAT_ATTR_END)) { - ast_test_status_update(test, "format1 did not determine isset on number correctly. \n"); - return AST_TEST_FAIL; - } - - /* append the string attribute to a format with previous attributes already set */ - ast_format_append(&format1, - TEST_ATTR_KEY_STRING,"String", - AST_FORMAT_ATTR_END); - if (ast_format_isset(&format1, TEST_ATTR_KEY_STRING, "String", AST_FORMAT_ATTR_END)) { - ast_test_status_update(test, "format1 did not set string attribute correctly.\n"); - return AST_TEST_FAIL; - } - if (!ast_format_isset(&format1, TEST_ATTR_KEY_STRING, "Not a string", AST_FORMAT_ATTR_END)) { - ast_test_status_update(test, "format1 did not determine isset on string correctly. \n"); - return AST_TEST_FAIL; - } - - /* set format2 with both STRING and NUMBER at the same time */ - ast_format_set(&format2, AST_FORMAT_TESTLAW, 1, - TEST_ATTR_KEY_STRING, "MOOOoo", - TEST_ATTR_KEY_SAMP_RATE, TEST_ATTR_VAL_SAMP_8KHZ, - TEST_ATTR_KEY_SAMP_RATE, TEST_ATTR_VAL_SAMP_16KHZ, - AST_FORMAT_ATTR_END); - /* perform isset with multiple key value pairs. */ - - if (ast_format_isset(&format2, - TEST_ATTR_KEY_STRING, "MOOOoo", - TEST_ATTR_KEY_SAMP_RATE, TEST_ATTR_VAL_SAMP_16KHZ, - AST_FORMAT_ATTR_END)) { - - ast_test_status_update(test, "format2 did not set attributes correctly.\n"); - return AST_TEST_FAIL; - } - if (!ast_format_isset(&format2, - TEST_ATTR_KEY_STRING, "WRONG", - TEST_ATTR_KEY_SAMP_RATE, TEST_ATTR_VAL_SAMP_8KHZ, - TEST_ATTR_KEY_SAMP_RATE, TEST_ATTR_VAL_SAMP_16KHZ, - AST_FORMAT_ATTR_END)) { - - ast_test_status_update(test, "format2 did not deterine isset correctly.\n"); - return AST_TEST_FAIL; - } - - /* get joint attributes between format1 and format2. */ - if (ast_format_joint(&format1, &format2, &joint)) { - ast_test_status_update(test, "failed to get joint attributes.\n"); - return AST_TEST_FAIL; - } - if (ast_format_isset(&joint, TEST_ATTR_KEY_SAMP_RATE, TEST_ATTR_VAL_SAMP_8KHZ, AST_FORMAT_ATTR_END)) { - ast_test_status_update(test, "joint attribute was not what we expected.\n"); - return AST_TEST_FAIL; - } - - /* exercise compare functions */ - if (ast_format_cmp(&format1, &format2) != AST_FORMAT_CMP_NOT_EQUAL) { - ast_test_status_update(test, "cmp 1 failed.\n"); - return AST_TEST_FAIL; - } - if (ast_format_cmp(&format1, &format1) != AST_FORMAT_CMP_EQUAL) { - ast_test_status_update(test, "cmp 2 failed.\n"); - return AST_TEST_FAIL; - } - if (ast_format_cmp(&joint, &format1) != AST_FORMAT_CMP_SUBSET) { - ast_test_status_update(test, "cmp 3 failed.\n"); - return AST_TEST_FAIL; - } - - /* unregister interface */ - if (ast_format_attr_unreg_interface(&test_interface)) { - ast_test_status_update(test, "test_interface failed to unregister.\n"); - return AST_TEST_FAIL; - } - - return AST_TEST_PASS; -} - -/*! - * \internal - */ -AST_TEST_DEFINE(format_test2) -{ - struct ast_format format = { 0, }; - - switch (cmd) { - case TEST_INIT: - info->name = "ast_format_test2"; - info->category = "/main/format/"; - info->summary = "Test ast_format unique id and category system"; - info->description = - "This test exercises the Ast Format unique id and category " - "system by creating formats of various types and verifying " - "their category matches what we expect."; - return AST_TEST_NOT_RUN; - case TEST_EXECUTE: - break; - } - - ast_format_set(&format, AST_FORMAT_ULAW, 0); - if (AST_FORMAT_GET_TYPE(format.id) != AST_FORMAT_TYPE_AUDIO) { - ast_test_status_update(test, "audio type failed\n"); - return AST_TEST_FAIL; - } - - ast_format_set(&format, AST_FORMAT_H264, 0); - if (AST_FORMAT_GET_TYPE(format.id) != AST_FORMAT_TYPE_VIDEO) { - ast_test_status_update(test, "video type failed\n"); - return AST_TEST_FAIL; - } - - ast_format_set(&format, AST_FORMAT_JPEG, 0); - if (AST_FORMAT_GET_TYPE(format.id) != AST_FORMAT_TYPE_IMAGE) { - ast_test_status_update(test, "image type failed\n"); - return AST_TEST_FAIL; - } - - ast_format_set(&format, AST_FORMAT_T140, 0); - if (AST_FORMAT_GET_TYPE(format.id) != AST_FORMAT_TYPE_TEXT) { - ast_test_status_update(test, "text type failed\n"); - return AST_TEST_FAIL; - } - - return AST_TEST_PASS; -} - -static int container_test1_helper(struct ast_format_cap *cap1, struct ast_format_cap *cap2, struct ast_test *test) -{ - - int res = AST_TEST_PASS; - struct ast_format_cap *cap_joint = NULL; - struct ast_format tmpformat; - - if (ast_format_attr_reg_interface(&test_interface)) { - ast_test_status_update(test, "test_interface failed to register.\n"); - ast_format_cap_destroy(cap1); - ast_format_cap_destroy(cap2); - return AST_TEST_FAIL; - } - - ast_format_cap_add(cap1, ast_format_set(&tmpformat, AST_FORMAT_GSM, 0)); - ast_format_cap_add(cap1, ast_format_set(&tmpformat, AST_FORMAT_ULAW, 0)); - ast_format_cap_add(cap1, ast_format_set(&tmpformat, AST_FORMAT_G722, 0)); - ast_format_cap_add(cap1, ast_format_set(&tmpformat, AST_FORMAT_ALAW, 0)); - ast_format_cap_add(cap1, ast_format_set(&tmpformat, AST_FORMAT_H264, 0)); - ast_format_cap_add(cap1, ast_format_set(&tmpformat, AST_FORMAT_H263, 0)); - ast_format_cap_add(cap1, ast_format_set(&tmpformat, AST_FORMAT_T140, 0)); - ast_format_cap_add(cap1, ast_format_set(&tmpformat, AST_FORMAT_JPEG, 0)); - ast_format_cap_add(cap1, ast_format_set(&tmpformat, AST_FORMAT_TESTLAW, 1, - TEST_ATTR_KEY_STRING, "testing caps hooray", - TEST_ATTR_KEY_SAMP_RATE, TEST_ATTR_VAL_SAMP_8KHZ, - TEST_ATTR_KEY_SAMP_RATE, TEST_ATTR_VAL_SAMP_16KHZ, - TEST_ATTR_KEY_SAMP_RATE, TEST_ATTR_VAL_SAMP_32KHZ, - AST_FORMAT_ATTR_END)); - - /* Test is compatible */ - if (!ast_format_cap_iscompatible(cap1, ast_format_set(&tmpformat, AST_FORMAT_ALAW, 0)) || - !ast_format_cap_iscompatible(cap1, ast_format_set(&tmpformat, AST_FORMAT_ULAW, 0)) || - !ast_format_cap_iscompatible(cap1, ast_format_set(&tmpformat, AST_FORMAT_GSM, 0)) || - !ast_format_cap_iscompatible(cap1, ast_format_set(&tmpformat, AST_FORMAT_H264, 0)) || - !ast_format_cap_iscompatible(cap1, ast_format_set(&tmpformat, AST_FORMAT_JPEG, 0)) || - !ast_format_cap_iscompatible(cap1, ast_format_set(&tmpformat, AST_FORMAT_T140, 0))) { - ast_test_status_update(test, "ast cap1 failed to properly detect compatibility test 1.\n"); - res = AST_TEST_FAIL; - goto test3_cleanup; - } - - /* Test things that are not compatible */ - if (ast_format_cap_iscompatible(cap1, ast_format_set(&tmpformat, AST_FORMAT_SPEEX, 0)) || - ast_format_cap_iscompatible(cap1, ast_format_set(&tmpformat, AST_FORMAT_SPEEX16, 0)) || - ast_format_cap_iscompatible(cap1, ast_format_set(&tmpformat, AST_FORMAT_H261, 0))) { - ast_test_status_update(test, "ast cap1 failed to properly detect compatibility test 2.\n"); - res = AST_TEST_FAIL; - goto test3_cleanup; - } - - /* Test compatiblity with format with attributes. */ - if (!ast_format_cap_iscompatible(cap1, ast_format_set(&tmpformat, AST_FORMAT_TESTLAW, 1, - TEST_ATTR_KEY_STRING, "testing caps hooray", - TEST_ATTR_KEY_SAMP_RATE, TEST_ATTR_VAL_SAMP_8KHZ, - TEST_ATTR_KEY_SAMP_RATE, TEST_ATTR_VAL_SAMP_16KHZ, - AST_FORMAT_ATTR_END))) { - - ast_test_status_update(test, "ast cap1 failed to properly detect compatibility test 3.\n"); - res = AST_TEST_FAIL; - goto test3_cleanup; - } - if (!ast_format_cap_iscompatible(cap1, ast_format_set(&tmpformat, AST_FORMAT_TESTLAW, 1, - TEST_ATTR_KEY_SAMP_RATE, TEST_ATTR_VAL_SAMP_8KHZ, - AST_FORMAT_ATTR_END))) { - - ast_test_status_update(test, "ast cap1 failed to properly detect compatibility test 4.\n"); - res = AST_TEST_FAIL; - goto test3_cleanup; - } - if (ast_format_cap_iscompatible(cap1, ast_format_set(&tmpformat, AST_FORMAT_TESTLAW, 1, - TEST_ATTR_KEY_SAMP_RATE, TEST_ATTR_VAL_SAMP_8KHZ, - TEST_ATTR_KEY_SAMP_RATE, TEST_ATTR_VAL_SAMP_48KHZ, /* 48khz was not compatible, so this should fail iscompatible check */ - AST_FORMAT_ATTR_END))) { - - ast_test_status_update(test, "ast cap1 failed to properly detect compatibility test 5.\n"); - res = AST_TEST_FAIL; - goto test3_cleanup; - } - - /* Lets start testing the functions that compare ast_format_cap objects. - * Genreate the cap2 object to contain some similar formats as cap1 - * and some different formats as well. */ - ast_format_cap_add(cap2, ast_format_set(&tmpformat, AST_FORMAT_GSM, 0)); - ast_format_cap_add(cap2, ast_format_set(&tmpformat, AST_FORMAT_ULAW, 0)); - ast_format_cap_add(cap2, ast_format_set(&tmpformat, AST_FORMAT_SIREN7, 0)); - ast_format_cap_add(cap2, ast_format_set(&tmpformat, AST_FORMAT_H261, 0)); - ast_format_cap_add(cap2, ast_format_set(&tmpformat, AST_FORMAT_T140, 0)); - ast_format_cap_add(cap2, ast_format_set(&tmpformat, AST_FORMAT_TESTLAW, 1, - TEST_ATTR_KEY_STRING, "testing caps hooray", - TEST_ATTR_KEY_SAMP_RATE, TEST_ATTR_VAL_SAMP_8KHZ, - TEST_ATTR_KEY_SAMP_RATE, TEST_ATTR_VAL_SAMP_12KHZ, - TEST_ATTR_KEY_SAMP_RATE, TEST_ATTR_VAL_SAMP_16KHZ, - TEST_ATTR_KEY_SAMP_RATE, TEST_ATTR_VAL_SAMP_32KHZ, - TEST_ATTR_KEY_SAMP_RATE, TEST_ATTR_VAL_SAMP_48KHZ, - AST_FORMAT_ATTR_END)); - - - /* find joint formats between cap1 and cap2 */ - cap_joint = ast_format_cap_joint(cap1, cap2); - - if (!cap_joint) { - ast_test_status_update(test, "failed to create joint capabilities correctly.\n"); - res = AST_TEST_FAIL; - goto test3_cleanup; - } - /* determine if cap_joint is what we think it should be */ - if (!ast_format_cap_iscompatible(cap_joint, ast_format_set(&tmpformat, AST_FORMAT_GSM, 0)) || - !ast_format_cap_iscompatible(cap_joint, ast_format_set(&tmpformat, AST_FORMAT_ULAW, 0)) || - !ast_format_cap_iscompatible(cap_joint, ast_format_set(&tmpformat, AST_FORMAT_T140, 0)) || - !ast_format_cap_iscompatible(cap_joint, ast_format_set(&tmpformat, AST_FORMAT_TESTLAW, 1, - TEST_ATTR_KEY_STRING, "testing caps hooray", - TEST_ATTR_KEY_SAMP_RATE, TEST_ATTR_VAL_SAMP_8KHZ, - TEST_ATTR_KEY_SAMP_RATE, TEST_ATTR_VAL_SAMP_16KHZ, - AST_FORMAT_ATTR_END))) { - - ast_test_status_update(test, "ast cap_joint failed to properly detect compatibility test 1.\n"); - res = AST_TEST_FAIL; - goto test3_cleanup; - } - /* make sure joint cap does not have formats that should not be there */ - if (ast_format_cap_iscompatible(cap_joint, ast_format_set(&tmpformat, AST_FORMAT_SIREN7, 0)) || - ast_format_cap_iscompatible(cap_joint, ast_format_set(&tmpformat, AST_FORMAT_TESTLAW, 1, - TEST_ATTR_KEY_STRING, "testing caps hooray", - TEST_ATTR_KEY_SAMP_RATE, TEST_ATTR_VAL_SAMP_8KHZ, - TEST_ATTR_KEY_SAMP_RATE, TEST_ATTR_VAL_SAMP_16KHZ, - TEST_ATTR_KEY_SAMP_RATE, TEST_ATTR_VAL_SAMP_48KHZ, - AST_FORMAT_ATTR_END))) { - - ast_test_status_update(test, "ast cap_joint failed to properly detect compatibility test 1.\n"); - res = AST_TEST_FAIL; - goto test3_cleanup; - } - - /* Lets test removing a capability */ - if (ast_format_cap_remove(cap_joint, ast_format_set(&tmpformat, AST_FORMAT_T140, 0))) { - ast_test_status_update(test, "ast_format_cap_remove failed. \n"); - res = AST_TEST_FAIL; - goto test3_cleanup; - } - /* Lets make sure what we just removed does not still exist */ - if (ast_format_cap_iscompatible(cap_joint, &tmpformat)) { - ast_test_status_update(test, "ast_format_cap_remove failed 2. \n"); - res = AST_TEST_FAIL; - goto test3_cleanup; - } - /* Lets test removing a capability by id.*/ - if (ast_format_cap_remove_byid(cap_joint, AST_FORMAT_GSM)) { - ast_test_status_update(test, "ast_format_cap_remove failed 3. \n"); - res = AST_TEST_FAIL; - goto test3_cleanup; - } - /* Lets make sure what we just removed does not still exist */ - if (ast_format_cap_iscompatible(cap_joint, ast_format_set(&tmpformat, AST_FORMAT_GSM, 0))) { - ast_test_status_update(test, "ast_format_cap_remove failed 4. \n"); - res = AST_TEST_FAIL; - goto test3_cleanup; - } - - /* lets test getting joint formats by type */ - ast_format_cap_destroy(cap_joint); - if (!(cap_joint = ast_format_cap_get_type(cap1, AST_FORMAT_TYPE_VIDEO))) { - ast_test_status_update(test, "ast_format_cap_get_type failed.\n"); - res = AST_TEST_FAIL; - goto test3_cleanup; - } - /* lets make sure our joint capability structure has what we expect */ - if (!ast_format_cap_iscompatible(cap_joint, ast_format_set(&tmpformat, AST_FORMAT_H264, 0)) || - !ast_format_cap_iscompatible(cap_joint, ast_format_set(&tmpformat, AST_FORMAT_H263, 0))) { - ast_test_status_update(test, "get_type failed 2.\n"); - res = AST_TEST_FAIL; - goto test3_cleanup; - } - /* now make sure joint does not have anything but video */ - if (ast_format_cap_iscompatible(cap_joint, ast_format_set(&tmpformat, AST_FORMAT_ALAW, 0)) || - ast_format_cap_iscompatible(cap_joint, ast_format_set(&tmpformat, AST_FORMAT_ULAW, 0)) || - ast_format_cap_iscompatible(cap_joint, ast_format_set(&tmpformat, AST_FORMAT_GSM, 0)) || - ast_format_cap_iscompatible(cap_joint, ast_format_set(&tmpformat, AST_FORMAT_JPEG, 0)) || - ast_format_cap_iscompatible(cap_joint, ast_format_set(&tmpformat, AST_FORMAT_T140, 0))) { - ast_test_status_update(test, "get_type failed 3.\n"); - res = AST_TEST_FAIL; - goto test3_cleanup; - } - - /* now lets remove everythign from cap_joint */ - ast_format_cap_remove_all(cap_joint); - if (!ast_format_cap_is_empty(cap_joint)) { - ast_test_status_update(test, "failed to remove all\n"); - res = AST_TEST_FAIL; - goto test3_cleanup; - } - - /* now lets add all by type */ - ast_format_cap_add_all_by_type(cap_joint, AST_FORMAT_TYPE_AUDIO); - if (ast_format_cap_is_empty(cap_joint)) { - ast_test_status_update(test, "failed to add all by type AUDIO\n"); - res = AST_TEST_FAIL; - } - ast_format_cap_iter_start(cap_joint); - while (!(ast_format_cap_iter_next(cap_joint, &tmpformat))) { - if (AST_FORMAT_GET_TYPE(tmpformat.id) != AST_FORMAT_TYPE_AUDIO) { - ast_test_status_update(test, "failed to add all by type AUDIO\n"); - res = AST_TEST_FAIL; - ast_format_cap_iter_end(cap_joint); - goto test3_cleanup; - } - } - ast_format_cap_iter_end(cap_joint); - - /* test append */ - ast_format_cap_append(cap_joint, cap1); - ast_format_cap_iter_start(cap1); - while (!(ast_format_cap_iter_next(cap1, &tmpformat))) { - if (!ast_format_cap_iscompatible(cap_joint, &tmpformat)) { - ast_test_status_update(test, "failed to append format capabilities.\n"); - res = AST_TEST_FAIL; - ast_format_cap_iter_end(cap1); - goto test3_cleanup; - } - } - ast_format_cap_iter_end(cap1); - - /* test copy */ - cap1 = ast_format_cap_destroy(cap1); - cap1 = ast_format_cap_dup(cap_joint); - if (!ast_format_cap_identical(cap_joint, cap1)) { - ast_test_status_update(test, "failed to copy capabilities\n"); - res = AST_TEST_FAIL; - goto test3_cleanup; - } - - /* test remove by type */ - ast_format_cap_remove_bytype(cap_joint, AST_FORMAT_TYPE_AUDIO); - if (ast_format_cap_has_type(cap_joint, AST_FORMAT_TYPE_AUDIO)) { - ast_test_status_update(test, "failed to remove all by type audio\n"); - res = AST_TEST_FAIL; - goto test3_cleanup; - } - if (!ast_format_cap_has_type(cap_joint, AST_FORMAT_TYPE_TEXT)) { /* it should still have text */ - ast_test_status_update(test, "failed to remove all by type audio\n"); - res = AST_TEST_FAIL; - goto test3_cleanup; - } - ast_format_cap_iter_start(cap_joint); - while (!(ast_format_cap_iter_next(cap_joint, &tmpformat))) { - if (AST_FORMAT_GET_TYPE(tmpformat.id) == AST_FORMAT_TYPE_AUDIO) { - ast_test_status_update(test, "failed to remove all by type audio\n"); - res = AST_TEST_FAIL; - ast_format_cap_iter_end(cap_joint); - goto test3_cleanup; - } - } - ast_format_cap_iter_end(cap_joint); - - /* test add all */ - ast_format_cap_remove_all(cap_joint); - ast_format_cap_add_all(cap_joint); - { - int video = 0, audio = 0, text = 0, image = 0; - ast_format_cap_iter_start(cap_joint); - while (!(ast_format_cap_iter_next(cap_joint, &tmpformat))) { - switch (AST_FORMAT_GET_TYPE(tmpformat.id)) { - case AST_FORMAT_TYPE_AUDIO: - audio++; - break; - case AST_FORMAT_TYPE_VIDEO: - video++; - break; - case AST_FORMAT_TYPE_TEXT: - text++; - break; - case AST_FORMAT_TYPE_IMAGE: - image++; - break; - } - } - ast_format_cap_iter_end(cap_joint); - if (!video || !audio || !text || !image) { - ast_test_status_update(test, "failed to add all\n"); - res = AST_TEST_FAIL; - goto test3_cleanup; - } - } - - /* test copy2 */ - ast_format_cap_copy(cap2, cap_joint); - if (!ast_format_cap_identical(cap2, cap_joint)) { - ast_test_status_update(test, "ast_format_cap_copy failed\n"); - res = AST_TEST_FAIL; - goto test3_cleanup; - } - -test3_cleanup: - ast_format_cap_destroy(cap1); - ast_format_cap_destroy(cap2); - ast_format_cap_destroy(cap_joint); - - /* unregister interface */ - if (ast_format_attr_unreg_interface(&test_interface)) { - ast_test_status_update(test, "test_interface failed to unregister.\n"); - res = AST_TEST_FAIL; - } - - return res; -} - -/*! - * \internal - */ -AST_TEST_DEFINE(container_test1_nolock) -{ - struct ast_format_cap *cap1; - struct ast_format_cap *cap2; - - switch (cmd) { - case TEST_INIT: - info->name = "container_test_1_no_locking"; - info->category = "/main/format/"; - info->summary = "Test ast_format and ast_format_cap structures, no locking"; - info->description = - "This test exercises the Ast Format Capability API by creating " - "capability structures and performing various API calls on them."; - return AST_TEST_NOT_RUN; - case TEST_EXECUTE: - break; - } - - cap1 = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_NOLOCK); - cap2 = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_NOLOCK); - - if (!cap1 || !cap2) { - ast_test_status_update(test, "cap alloc failed.\n"); - return AST_TEST_FAIL; - } - return container_test1_helper(cap1, cap2, test); -} - - -/*! - * \internal - */ -AST_TEST_DEFINE(container_test1_withlock) -{ - struct ast_format_cap *cap1; - struct ast_format_cap *cap2; - - switch (cmd) { - case TEST_INIT: - info->name = "container_test1_with_locking"; - info->category = "/main/format/"; - info->summary = "Test ast_format and ast_format_cap structures, with locking"; - info->description = - "This test exercises the Ast Format Capability API by creating " - "capability structures and performing various API calls on them."; - return AST_TEST_NOT_RUN; - case TEST_EXECUTE: - break; - } - - cap1 = ast_format_cap_alloc(0); - cap2 = ast_format_cap_alloc(0); - - if (!cap1 || !cap2) { - ast_test_status_update(test, "cap alloc failed.\n"); - return AST_TEST_FAIL; - } - return container_test1_helper(cap1, cap2, test); -} - -static int container_test2_no_locking_helper(struct ast_format_cap *cap, struct ast_test *test) -{ - int num = 0; - struct ast_format tmpformat = { 0, }; - - ast_format_cap_add(cap, ast_format_set(&tmpformat, AST_FORMAT_GSM, 0)); - ast_format_cap_add(cap, ast_format_set(&tmpformat, AST_FORMAT_ULAW, 0)); - ast_format_cap_add(cap, ast_format_set(&tmpformat, AST_FORMAT_G722, 0)); - - ast_format_cap_iter_start(cap); - while (!ast_format_cap_iter_next(cap, &tmpformat)) { - num++; - } - ast_format_cap_iter_end(cap); - - ast_format_cap_iter_start(cap); - while (!ast_format_cap_iter_next(cap, &tmpformat)) { - num++; - } - ast_format_cap_iter_end(cap); - - ast_format_cap_destroy(cap); - ast_test_status_update(test, "%d items iterated over\n", num); - return (num == 6) ? AST_TEST_PASS : AST_TEST_FAIL; - -} - -/*! - * \internal - */ -AST_TEST_DEFINE(container_test2_no_locking) -{ - struct ast_format_cap *cap; - - switch (cmd) { - case TEST_INIT: - info->name = "container_test2_no_locking"; - info->category = "/main/format/"; - info->summary = "Test ast_format_cap iterator, no locking"; - info->description = - "This test exercises the Ast Capability API iterators."; - return AST_TEST_NOT_RUN; - case TEST_EXECUTE: - break; - } - - cap = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_NOLOCK); - if (!cap) { - ast_test_status_update(test, "alloc failed\n"); - return AST_TEST_FAIL; - } - return container_test2_no_locking_helper(cap, test); -} - -/*! - * \internal - */ -AST_TEST_DEFINE(container_test2_with_locking) -{ - struct ast_format_cap *cap; - - switch (cmd) { - case TEST_INIT: - info->name = "container_test2_with_locking"; - info->category = "/main/format/"; - info->summary = "Test ast_format_cap iterator, with locking"; - info->description = - "This test exercises the Ast Capability API iterators."; - return AST_TEST_NOT_RUN; - case TEST_EXECUTE: - break; - } - - cap = ast_format_cap_alloc(0); - if (!cap) { - ast_test_status_update(test, "alloc failed\n"); - return AST_TEST_FAIL; - } - return container_test2_no_locking_helper(cap, test); -} - - -static int container_test3_helper(int nolocking, struct ast_test *test) -{ - int x; - int res = AST_TEST_PASS; - struct ast_format_cap *cap1; - struct ast_format_cap *cap2; - struct ast_format_cap *joint; - - for (x = 0; x < 2000; x++) { - if (nolocking) { - cap1 = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_NOLOCK); - cap2 = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_NOLOCK); - joint = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_NOLOCK); - } else { - cap1 = ast_format_cap_alloc(0); - cap2 = ast_format_cap_alloc(0); - joint = ast_format_cap_alloc(0); - } - if (!cap1 || !cap2 || !joint) { - ast_test_status_update(test, "cap alloc fail\n"); - return AST_TEST_FAIL; - } - ast_format_cap_add_all(cap1); - ast_format_cap_add_all_by_type(cap2, AST_FORMAT_TYPE_AUDIO); - ast_format_cap_joint_copy(cap1, cap2, joint); - if (!(ast_format_cap_identical(cap2, joint))) { - ast_test_status_update(test, "failed identical test\n"); - res = AST_TEST_FAIL; - cap1 = ast_format_cap_destroy(cap1); - cap2 = ast_format_cap_destroy(cap2); - joint = ast_format_cap_destroy(joint); - break; - } - cap1 = ast_format_cap_destroy(cap1); - cap2 = ast_format_cap_destroy(cap2); - joint = ast_format_cap_destroy(joint); - } - return res; -} - -/*! - * \internal - */ -AST_TEST_DEFINE(container_test3_nolock) -{ - switch (cmd) { - case TEST_INIT: - info->name = "container_test3_no_locking"; - info->category = "/main/format/"; - info->summary = "Load Test ast_format_cap no locking."; - info->description = - "This test exercises the Ast Capability API and its iterators for the purpose " - "of measuring performance."; - return AST_TEST_NOT_RUN; - case TEST_EXECUTE: - break; - } - - return container_test3_helper(1, test); -} - -/*! - * \internal - */ -AST_TEST_DEFINE(container_test3_withlock) -{ - switch (cmd) { - case TEST_INIT: - info->name = "container_test3_with_locking"; - info->category = "/main/format/"; - info->summary = "Load Test ast_format_cap with locking."; - info->description = - "This test exercises the Ast Capability API and its iterators for the purpose " - "of measuring performance."; - return AST_TEST_NOT_RUN; - case TEST_EXECUTE: - break; - } - - return container_test3_helper(0, test); -} - -static int unload_module(void) -{ - AST_TEST_UNREGISTER(format_test1); - AST_TEST_UNREGISTER(format_test2); - AST_TEST_UNREGISTER(container_test1_nolock); - AST_TEST_UNREGISTER(container_test1_withlock); - AST_TEST_UNREGISTER(container_test2_no_locking); - AST_TEST_UNREGISTER(container_test2_with_locking); - AST_TEST_UNREGISTER(container_test3_nolock); - AST_TEST_UNREGISTER(container_test3_withlock); - - return 0; -} - -static int load_module(void) -{ - AST_TEST_REGISTER(format_test1); - AST_TEST_REGISTER(format_test2); - AST_TEST_REGISTER(container_test1_nolock); - AST_TEST_REGISTER(container_test1_withlock); - AST_TEST_REGISTER(container_test2_no_locking); - AST_TEST_REGISTER(container_test2_with_locking); - AST_TEST_REGISTER(container_test3_nolock); - AST_TEST_REGISTER(container_test3_withlock); - - return AST_MODULE_LOAD_SUCCESS; -} - -AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "ast_format API Tests"); |