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_core_format.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_core_format.c')
-rw-r--r-- | tests/test_core_format.c | 975 |
1 files changed, 975 insertions, 0 deletions
diff --git a/tests/test_core_format.c b/tests/test_core_format.c new file mode 100644 index 000000000..2ae944bb3 --- /dev/null +++ b/tests/test_core_format.c @@ -0,0 +1,975 @@ +/* + * 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 Core Format API Unit Tests + * + * \author Joshua Colp <jcolp@digium.com> + * + */ + +/*** MODULEINFO + <depend>TEST_FRAMEWORK</depend> + <support_level>core</support_level> + ***/ + +#include "asterisk.h" + +ASTERISK_FILE_VERSION(__FILE__, "$Revision$") + +#include "asterisk/test.h" +#include "asterisk/module.h" +#include "asterisk/codec.h" +#include "asterisk/format.h" + +#define TEST_CATEGORY "/main/core_format/" + +static void test_core_format_destroy(struct ast_format *format); +static int test_core_format_clone(const struct ast_format *src, struct ast_format *dst); +static enum ast_format_cmp_res test_core_format_cmp(const struct ast_format *format1, const struct ast_format *format2); +static struct ast_format *test_core_format_get_joint(const struct ast_format *format1, const struct ast_format *format2); +static struct ast_format *test_core_format_attribute_set(const struct ast_format *format, const char *name, const char *value); +static struct ast_format *test_core_format_parse_sdp_fmtp(const struct ast_format *format, const char *attributes); +static void test_core_format_generate_sdp_fmtp(const struct ast_format *format, unsigned int payload, struct ast_str **str); + +/*! \brief A format attribute 'module' used by the unit tests */ +static struct ast_format_interface test_core_format_attr = { + .format_destroy = &test_core_format_destroy, + .format_clone = &test_core_format_clone, + .format_cmp = &test_core_format_cmp, + .format_get_joint = &test_core_format_get_joint, + .format_attribute_set = &test_core_format_attribute_set, + .format_parse_sdp_fmtp = &test_core_format_parse_sdp_fmtp, + .format_generate_sdp_fmtp = &test_core_format_generate_sdp_fmtp, +}; + +/*! \brief A test piece of data to associate with \ref test_core_format_attr */ +struct test_core_format_pvt { + /*! Some data field */ + int field_one; + /*! Another arbitrary data field */ + int field_two; +}; + +/*! \brief A test codec for these unit tests. Should be used with \c test_core_format */ +static struct ast_codec test_core_format_codec = { + .name = "test_core_format_codec", + .description = "Unit test codec used by test_core_format", + .type = AST_MEDIA_TYPE_AUDIO, + .sample_rate = 8000, + .minimum_ms = 10, + .maximum_ms = 150, + .default_ms = 20, +}; + +/*! \brief Tracking object used to verify format attribute callbacks */ +struct callbacks_called { + /*! Number of times \ref test_core_format_destroy was called */ + int format_destroy; + /*! Number of times \ref test_core_format_clone was called */ + int format_clone; + /*! Number of times \ref test_core_format_cmp was called */ + int format_cmp; + /*! Number of times \ref test_core_format_get_joint was called */ + int format_get_joint; + /*! Number of times \ref test_core_format_attribute_set was called */ + int format_attribute_set; + /*! Number of times \ref test_core_format_parse_sdp_fmtp was called */ + int format_parse_sdp_fmtp; + /*! Number of times \ref test_core_format_generate_sdp_fmtp was called */ + int format_generate_sdp_fmtp; +}; + +/*! \brief A global tracking object. Cleared out by the test init cb */ +static struct callbacks_called test_callbacks_called; + +/*! \brief Format attribute callback for when format attributes are to be destroyed */ +static void test_core_format_destroy(struct ast_format *format) +{ + struct test_core_format_pvt *pvt = ast_format_get_attribute_data(format); + + ast_free(pvt); + ++test_callbacks_called.format_destroy; +} + +/*! \brief Format attribute callback called during format cloning */ +static int test_core_format_clone(const struct ast_format *src, struct ast_format *dst) +{ + struct test_core_format_pvt *pvt = ast_format_get_attribute_data(src); + struct test_core_format_pvt *new_pvt; + + new_pvt = ast_calloc(1, sizeof(*new_pvt)); + if (!new_pvt) { + return -1; + } + + if (pvt) { + *new_pvt = *pvt; + } + ast_format_set_attribute_data(dst, new_pvt); + + ++test_callbacks_called.format_clone; + + return 0; +} + +/*! \brief Format attribute callback called during format comparison */ +static enum ast_format_cmp_res test_core_format_cmp(const struct ast_format *format1, const struct ast_format *format2) +{ + struct test_core_format_pvt *pvt1 = ast_format_get_attribute_data(format1); + struct test_core_format_pvt *pvt2 = ast_format_get_attribute_data(format2); + + ++test_callbacks_called.format_cmp; + if (pvt1 == pvt2) { + return AST_FORMAT_CMP_EQUAL; + } + + if ((!pvt1 && pvt2 && (pvt2->field_one != 0 || pvt2->field_two != 0)) + || (pvt1 && !pvt2 && (pvt1->field_one != 0 || pvt1->field_two != 0))) { + return AST_FORMAT_CMP_NOT_EQUAL; + } + + if (pvt1 && pvt2) { + if (!memcmp(pvt1, pvt2, sizeof(*pvt1))) { + return AST_FORMAT_CMP_EQUAL; + } else { + return AST_FORMAT_CMP_NOT_EQUAL; + } + } + + return AST_FORMAT_CMP_EQUAL; +} + +/*! + * \brief Format attribute callback called during joint format capability + * \note Our test will assume the max of attributes \c field_one and \c field_two + */ +static struct ast_format *test_core_format_get_joint(const struct ast_format *format1, const struct ast_format *format2) +{ + struct test_core_format_pvt *pvt1 = ast_format_get_attribute_data(format1); + struct test_core_format_pvt *pvt2 = ast_format_get_attribute_data(format2); + struct ast_format *joint; + struct test_core_format_pvt *joint_pvt; + + joint = ast_format_clone(format1); + if (!joint) { + return NULL; + } + joint_pvt = ast_format_get_attribute_data(joint); + + joint_pvt->field_one = MAX(pvt1 ? pvt1->field_one : 0, pvt2 ? pvt2->field_one : 0); + joint_pvt->field_two = MAX(pvt2 ? pvt2->field_two : 0, pvt2 ? pvt2->field_two : 0); + + ++test_callbacks_called.format_get_joint; + + return joint; +} + +/*! \brief Format attribute callback for setting an attribute on a format */ +static struct ast_format *test_core_format_attribute_set(const struct ast_format *format, const char *name, const char *value) +{ + struct ast_format *clone = ast_format_clone(format); + struct test_core_format_pvt *clone_pvt; + + if (!clone) { + return NULL; + } + clone_pvt = ast_format_get_attribute_data(clone); + + if (!strcmp(name, "one")) { + clone_pvt->field_one = atoi(value); + } else if (!strcmp(name, "two")) { + clone_pvt->field_two = atoi(value); + } + ++test_callbacks_called.format_attribute_set; + + return clone; +} + +/*! \brief Format attribute callback to construct a format from an SDP fmtp line */ +static struct ast_format *test_core_format_parse_sdp_fmtp(const struct ast_format *format, const char *attributes) +{ + struct ast_format *clone = ast_format_clone(format); + struct test_core_format_pvt *pvt; + + if (!clone) { + return NULL; + } + + pvt = ast_format_get_attribute_data(clone); + + if (sscanf(attributes, "one=%d;two=%d", &pvt->field_one, &pvt->field_two) != 2) { + ao2_ref(clone, -1); + return NULL; + } + + ++test_callbacks_called.format_parse_sdp_fmtp; + return clone; +} + +/*! \brief Format attribute callback to generate an SDP fmtp line from a format */ +static void test_core_format_generate_sdp_fmtp(const struct ast_format *format, unsigned int payload, struct ast_str **str) +{ + struct test_core_format_pvt *pvt = ast_format_get_attribute_data(format); + + if (!pvt) { + return; + } + + ast_str_append(str, 0, "a=fmtp:%d one=%d;two=%d\r\n", payload, pvt->field_one, pvt->field_two); + + ++test_callbacks_called.format_generate_sdp_fmtp; +} + +AST_TEST_DEFINE(format_create) +{ + RAII_VAR(struct ast_codec *, codec, NULL, ao2_cleanup); + RAII_VAR(struct ast_format *, format, NULL, ao2_cleanup); + + switch (cmd) { + case TEST_INIT: + info->name = __PRETTY_FUNCTION__; + info->category = TEST_CATEGORY; + info->summary = "Format creation unit test"; + info->description = + "Test creation of a format"; + return AST_TEST_NOT_RUN; + case TEST_EXECUTE: + break; + } + + codec = ast_codec_get("ulaw", AST_MEDIA_TYPE_AUDIO, 8000); + if (!codec) { + ast_test_status_update(test, "Could not retrieve built-in ulaw codec\n"); + return AST_TEST_FAIL; + } + + format = ast_format_create(codec); + if (!format) { + ast_test_status_update(test, "Could not create format using built-in codec\n"); + return AST_TEST_FAIL; + } else if (ast_format_get_codec_id(format) != codec->id) { + ast_test_status_update(test, "Created format does not contain provided codec\n"); + return AST_TEST_FAIL; + } + + ao2_ref(format, -1); + format = ast_format_create_named("super_ulaw", codec); + if (!format) { + ast_test_status_update(test, "Could not create format using built-in codec\n"); + return AST_TEST_FAIL; + } else if (ast_format_get_codec_id(format) != codec->id) { + ast_test_status_update(test, "Created format does not contain provided codec\n"); + return AST_TEST_FAIL; + } + + return AST_TEST_PASS; +} + +AST_TEST_DEFINE(format_create_attr) +{ + RAII_VAR(struct ast_codec *, codec, NULL, ao2_cleanup); + RAII_VAR(struct ast_format *, format, NULL, ao2_cleanup); + RAII_VAR(struct ast_format *, format_w_attr, NULL, ao2_cleanup); + + switch (cmd) { + case TEST_INIT: + info->name = __PRETTY_FUNCTION__; + info->category = TEST_CATEGORY; + info->summary = "Format creation w/ attributes unit test"; + info->description = + "Test creation of a format with attributes"; + return AST_TEST_NOT_RUN; + case TEST_EXECUTE: + break; + } + + codec = ast_codec_get("test_core_format_codec", AST_MEDIA_TYPE_AUDIO, 8000); + if (!codec) { + ast_test_status_update(test, "Could not retrieve test_core_format_codec codec\n"); + return AST_TEST_FAIL; + } + + format = ast_format_create(codec); + if (!format) { + ast_test_status_update(test, "Could not create format using test_core_format_codec codec\n"); + return AST_TEST_FAIL; + } else if (ast_format_get_codec_id(format) != codec->id) { + ast_test_status_update(test, "Created format does not contain provided codec\n"); + return AST_TEST_FAIL; + } + + format_w_attr = ast_format_attribute_set(format, "one", "1"); + if (!format_w_attr) { + ast_test_status_update(test, "Could not create format with attributes using test_core_format_codec codec\n"); + return AST_TEST_FAIL; + } else if (ast_format_get_codec_id(format_w_attr) != codec->id) { + ast_test_status_update(test, "Created format does not contain provided codec\n"); + return AST_TEST_FAIL; + } else if (ast_format_cmp(format, format_w_attr) == AST_FORMAT_CMP_EQUAL) { + ast_test_status_update(test, "Format with attributes should not be equal to format without attributes\n"); + return AST_TEST_FAIL; + } + + ast_test_validate(test, test_callbacks_called.format_attribute_set == 1); + ast_test_validate(test, test_callbacks_called.format_cmp == 1); + + return AST_TEST_PASS; +} + +AST_TEST_DEFINE(format_clone) +{ + RAII_VAR(struct ast_codec *, codec, NULL, ao2_cleanup); + RAII_VAR(struct ast_format *, format, NULL, ao2_cleanup); + RAII_VAR(struct ast_format *, format_w_attr, NULL, ao2_cleanup); + RAII_VAR(struct ast_format *, clone, NULL, ao2_cleanup); + + switch (cmd) { + case TEST_INIT: + info->name = __PRETTY_FUNCTION__; + info->category = TEST_CATEGORY; + info->summary = "Format cloning unit test"; + info->description = + "Test cloning of a format"; + return AST_TEST_NOT_RUN; + case TEST_EXECUTE: + break; + } + + codec = ast_codec_get("test_core_format_codec", AST_MEDIA_TYPE_AUDIO, 8000); + if (!codec) { + ast_test_status_update(test, "Could not retrieve test_core_format_codec codec\n"); + return AST_TEST_FAIL; + } + + format = ast_format_create(codec); + if (!format) { + ast_test_status_update(test, "Could not create format using test_core_format_codec codec\n"); + return AST_TEST_FAIL; + } else if (ast_format_get_codec_id(format) != codec->id) { + ast_test_status_update(test, "Created format does not contain provided codec\n"); + return AST_TEST_FAIL; + } + + format_w_attr = ast_format_attribute_set(format, "one", "1"); + if (!format_w_attr) { + ast_test_status_update(test, "Could not create format with attributes using test_core_format_codec codec\n"); + return AST_TEST_FAIL; + } else if (ast_format_get_codec_id(format_w_attr) != codec->id) { + ast_test_status_update(test, "Created format does not contain provided codec\n"); + return AST_TEST_FAIL; + } + + /* Test cloning a format without attributes */ + clone = ast_format_clone(format); + if (!clone) { + ast_test_status_update(test, "Could not create cloned format\n"); + return AST_TEST_FAIL; + } else if (ast_format_get_codec_id(clone) != codec->id) { + ast_test_status_update(test, "Cloned format does not contain provided codec\n"); + return AST_TEST_FAIL; + } else if (clone == format) { + ast_test_status_update(test, "Cloned format pointer is the same as original format pointer\n"); + return AST_TEST_FAIL; + } else if (ast_format_cmp(clone, format) != AST_FORMAT_CMP_EQUAL) { + ast_test_status_update(test, "Cloned format is not the same as its original format\n"); + return AST_TEST_FAIL; + } + ao2_ref(clone, -1); + + /* Test cloning a format with attributes */ + clone = ast_format_clone(format_w_attr); + if (!clone) { + ast_test_status_update(test, "Could not create cloned format\n"); + return AST_TEST_FAIL; + } else if (ast_format_get_codec_id(clone) != codec->id) { + ast_test_status_update(test, "Cloned format does not contain provided codec\n"); + return AST_TEST_FAIL; + } else if (clone == format_w_attr) { + ast_test_status_update(test, "Cloned format pointer is the same as original format pointer\n"); + return AST_TEST_FAIL; + } else if (ast_format_cmp(clone, format_w_attr) != AST_FORMAT_CMP_EQUAL) { + ast_test_status_update(test, "Cloned format is not the same as its original format\n"); + return AST_TEST_FAIL; + } + ast_test_validate(test, test_callbacks_called.format_attribute_set == 1); + ast_test_validate(test, test_callbacks_called.format_clone == 3); + ast_test_validate(test, test_callbacks_called.format_cmp == 2); + + return AST_TEST_PASS; +} + +AST_TEST_DEFINE(format_cmp_same_codec) +{ + RAII_VAR(struct ast_codec *, codec, NULL, ao2_cleanup); + RAII_VAR(struct ast_format *, first, NULL, ao2_cleanup); + RAII_VAR(struct ast_format *, second, NULL, ao2_cleanup); + RAII_VAR(struct ast_format *, named, NULL, ao2_cleanup); + + switch (cmd) { + case TEST_INIT: + info->name = __PRETTY_FUNCTION__; + info->category = TEST_CATEGORY; + info->summary = "Format comparison unit test"; + info->description = + "Test comparison of two different formats with same codec"; + return AST_TEST_NOT_RUN; + case TEST_EXECUTE: + break; + } + + codec = ast_codec_get("ulaw", AST_MEDIA_TYPE_AUDIO, 8000); + if (!codec) { + ast_test_status_update(test, "Could not retrieve built-in ulaw codec\n"); + return AST_TEST_FAIL; + } + + first = ast_format_create(codec); + if (!first) { + ast_test_status_update(test, "Could not create first format using built-in codec\n"); + return AST_TEST_FAIL; + } + + second = ast_format_create(codec); + if (!second) { + ast_test_status_update(test, "Could not create second format using built-in codec\n"); + return AST_TEST_FAIL; + } + + named = ast_format_create_named("super_ulaw", codec); + if (!named) { + ast_test_status_update(test, "Could not create named format using built-in codec\n"); + return AST_TEST_FAIL; + } + + if (ast_format_cmp(first, second) != AST_FORMAT_CMP_EQUAL) { + ast_test_status_update(test, "Two formats that are the same compared as not being equal\n"); + return AST_TEST_FAIL; + } + + if (ast_format_cmp(first, named) != AST_FORMAT_CMP_EQUAL) { + ast_test_status_update(test, "Two formats that are the same compared as not being equal\n"); + return AST_TEST_FAIL; + } + + return AST_TEST_PASS; +} + +AST_TEST_DEFINE(format_cmp_different_codec) +{ + RAII_VAR(struct ast_codec *, first_codec, NULL, ao2_cleanup); + RAII_VAR(struct ast_codec *, second_codec, NULL, ao2_cleanup); + RAII_VAR(struct ast_format *, first, NULL, ao2_cleanup); + RAII_VAR(struct ast_format *, second, NULL, ao2_cleanup); + + switch (cmd) { + case TEST_INIT: + info->name = __PRETTY_FUNCTION__; + info->category = TEST_CATEGORY; + info->summary = "Format comparison unit test"; + info->description = + "Test comparison of two different formats with different codec"; + return AST_TEST_NOT_RUN; + case TEST_EXECUTE: + break; + } + + first_codec = ast_codec_get("ulaw", AST_MEDIA_TYPE_AUDIO, 8000); + if (!first_codec) { + ast_test_status_update(test, "Could not retrieve built-in ulaw codec\n"); + return AST_TEST_FAIL; + } + + first = ast_format_create(first_codec); + if (!first) { + ast_test_status_update(test, "Could not create first format using built-in codec\n"); + return AST_TEST_FAIL; + } + + second_codec = ast_codec_get("alaw", AST_MEDIA_TYPE_AUDIO, 8000); + if (!second_codec) { + ast_test_status_update(test, "Could not retrieve built-in alaw codec\n"); + return AST_TEST_FAIL; + } + + second = ast_format_create(second_codec); + if (!second) { + ast_test_status_update(test, "Could not create second format using built-in codec\n"); + return AST_TEST_FAIL; + } + + if (ast_format_cmp(first, second) != AST_FORMAT_CMP_NOT_EQUAL) { + ast_test_status_update(test, "Two formats that have different codecs did not compare as being not equal\n"); + return AST_TEST_FAIL; + } + + return AST_TEST_PASS; +} + +AST_TEST_DEFINE(format_attr_cmp_same_codec) +{ + RAII_VAR(struct ast_codec *, codec, NULL, ao2_cleanup); + RAII_VAR(struct ast_format *, first, NULL, ao2_cleanup); + RAII_VAR(struct ast_format *, second, NULL, ao2_cleanup); + RAII_VAR(struct ast_format *, original, NULL, ao2_cleanup); + + switch (cmd) { + case TEST_INIT: + info->name = __PRETTY_FUNCTION__; + info->category = TEST_CATEGORY; + info->summary = "Format with attributes comparison unit test"; + info->description = + "Test comparison of two different formats with attributes with same codec"; + return AST_TEST_NOT_RUN; + case TEST_EXECUTE: + break; + } + + codec = ast_codec_get("test_core_format_codec", AST_MEDIA_TYPE_AUDIO, 8000); + if (!codec) { + ast_test_status_update(test, "Could not retrieve test_core_format_codec codec\n"); + return AST_TEST_FAIL; + } + + original = ast_format_create(codec); + if (!original) { + ast_test_status_update(test, "Could not create format using test_core_format_codec codec\n"); + return AST_TEST_FAIL; + } + + first = ast_format_attribute_set(original, "one", "1"); + if (!first) { + ast_test_status_update(test, "Could not create first format with attributes\n"); + return AST_TEST_FAIL; + } + + second = ast_format_attribute_set(original, "two", "1"); + if (!second) { + ast_test_status_update(test, "Could not create second format with attributes\n"); + return AST_TEST_FAIL; + } + + if (ast_format_cmp(first, second) == AST_FORMAT_CMP_EQUAL) { + ast_test_status_update(test, "Formats with different attributes were compared to be equal when they should not\n"); + return AST_TEST_FAIL; + } + + ao2_ref(second, -1); + second = ast_format_attribute_set(original, "one", "1"); + + if (ast_format_cmp(first, second) != AST_FORMAT_CMP_EQUAL) { + ast_test_status_update(test, "Formats with the same attributes should be equal\n"); + return AST_TEST_FAIL; + } + + ast_test_validate(test, test_callbacks_called.format_attribute_set == 3); + ast_test_validate(test, test_callbacks_called.format_cmp == 2); + + return AST_TEST_PASS; +} + +AST_TEST_DEFINE(format_joint_same_codec) +{ + RAII_VAR(struct ast_codec *, codec, NULL, ao2_cleanup); + RAII_VAR(struct ast_format *, first, NULL, ao2_cleanup); + RAII_VAR(struct ast_format *, second, NULL, ao2_cleanup); + RAII_VAR(struct ast_format *, joint, NULL, ao2_cleanup); + + switch (cmd) { + case TEST_INIT: + info->name = __PRETTY_FUNCTION__; + info->category = TEST_CATEGORY; + info->summary = "Joint format unit test"; + info->description = + "Test joint format creation using two different formats with same codec"; + return AST_TEST_NOT_RUN; + case TEST_EXECUTE: + break; + } + + codec = ast_codec_get("ulaw", AST_MEDIA_TYPE_AUDIO, 8000); + if (!codec) { + ast_test_status_update(test, "Could not retrieve built-in ulaw codec\n"); + return AST_TEST_FAIL; + } + + first = ast_format_create(codec); + if (!first) { + ast_test_status_update(test, "Could not create first format using built-in codec\n"); + return AST_TEST_FAIL; + } + + second = ast_format_create(codec); + if (!second) { + ast_test_status_update(test, "Could not create second format using built-in codec\n"); + return AST_TEST_FAIL; + } + + joint = ast_format_joint(first, second); + if (!joint) { + ast_test_status_update(test, "Failed to create a joint format using two formats of same codec\n"); + return AST_TEST_FAIL; + } else if (ast_format_get_codec_id(joint) != codec->id) { + ast_test_status_update(test, "Returned joint format does not contain expected codec\n"); + return AST_TEST_FAIL; + } + + return AST_TEST_PASS; +} + +AST_TEST_DEFINE(format_attr_joint_same_codec) +{ + RAII_VAR(struct ast_codec *, codec, NULL, ao2_cleanup); + RAII_VAR(struct ast_format *, original, NULL, ao2_cleanup); + RAII_VAR(struct ast_format *, first, NULL, ao2_cleanup); + RAII_VAR(struct ast_format *, second, NULL, ao2_cleanup); + RAII_VAR(struct ast_format *, joint, NULL, ao2_cleanup); + struct ast_str *fmtp = ast_str_alloca(64); + + switch (cmd) { + case TEST_INIT: + info->name = __PRETTY_FUNCTION__; + info->category = TEST_CATEGORY; + info->summary = "Joint format attribute unit test"; + info->description = + "Test joint format creation using two different formats with attributes and with same codec"; + return AST_TEST_NOT_RUN; + case TEST_EXECUTE: + break; + } + + codec = ast_codec_get("test_core_format_codec", AST_MEDIA_TYPE_AUDIO, 8000); + if (!codec) { + ast_test_status_update(test, "Could not retrieve test_core_format_codec codec\n"); + return AST_TEST_FAIL; + } + + original = ast_format_create(codec); + if (!original) { + ast_test_status_update(test, "Could not create format from test_core_format_codec codec\n"); + return AST_TEST_FAIL; + } + + first = ast_format_attribute_set(original, "one", "2"); + if (!first) { + ast_test_status_update(test, "Could not create first format using test_core_format_codec codec\n"); + return AST_TEST_FAIL; + } + + second = ast_format_attribute_set(original, "one", "5"); + if (!second) { + ast_test_status_update(test, "Could not create second format using test_core_format_codec codec\n"); + return AST_TEST_FAIL; + } + + joint = ast_format_joint(first, second); + if (!joint) { + ast_test_status_update(test, "Failed to create a joint format using two formats of same codec\n"); + return AST_TEST_FAIL; + } else if (ast_format_get_codec_id(joint) != codec->id) { + ast_test_status_update(test, "Returned joint format does not contain expected codec\n"); + return AST_TEST_FAIL; + } + + ast_format_generate_sdp_fmtp(joint, 100, &fmtp); + ast_test_validate(test, strcmp("a=fmtp:100 one=5;two=0\r\n", ast_str_buffer(fmtp)) == 0); + + ast_test_validate(test, test_callbacks_called.format_attribute_set == 2); + ast_test_validate(test, test_callbacks_called.format_get_joint == 1); + ast_test_validate(test, test_callbacks_called.format_generate_sdp_fmtp == 1); + + return AST_TEST_PASS; +} + +AST_TEST_DEFINE(format_joint_different_codec) +{ + RAII_VAR(struct ast_codec *, first_codec, NULL, ao2_cleanup); + RAII_VAR(struct ast_codec *, second_codec, NULL, ao2_cleanup); + RAII_VAR(struct ast_format *, first, NULL, ao2_cleanup); + RAII_VAR(struct ast_format *, second, NULL, ao2_cleanup); + RAII_VAR(struct ast_format *, joint, NULL, ao2_cleanup); + + switch (cmd) { + case TEST_INIT: + info->name = __PRETTY_FUNCTION__; + info->category = TEST_CATEGORY; + info->summary = "Joint format unit test"; + info->description = + "Test that there is no joint format between two different formats with different codec"; + return AST_TEST_NOT_RUN; + case TEST_EXECUTE: + break; + } + + first_codec = ast_codec_get("ulaw", AST_MEDIA_TYPE_AUDIO, 8000); + if (!first_codec) { + ast_test_status_update(test, "Could not retrieve built-in ulaw codec\n"); + return AST_TEST_FAIL; + } + + first = ast_format_create(first_codec); + if (!first) { + ast_test_status_update(test, "Could not create first format using built-in codec\n"); + return AST_TEST_FAIL; + } + + second_codec = ast_codec_get("alaw", AST_MEDIA_TYPE_AUDIO, 8000); + if (!second_codec) { + ast_test_status_update(test, "Could not retrieve built-in alaw codec\n"); + return AST_TEST_FAIL; + } + + second = ast_format_create(second_codec); + if (!second) { + ast_test_status_update(test, "Could not create second format using built-in codec\n"); + return AST_TEST_FAIL; + } + + joint = ast_format_joint(first, second); + if (joint) { + ast_test_status_update(test, "Got a joint format between two formats with different codecs\n"); + return AST_TEST_FAIL; + } + + return AST_TEST_PASS; +} + +AST_TEST_DEFINE(format_copy) +{ + RAII_VAR(struct ast_codec *, codec, NULL, ao2_cleanup); + RAII_VAR(struct ast_format *, format, NULL, ao2_cleanup); + RAII_VAR(struct ast_format *, copy, NULL, ao2_cleanup); + + switch (cmd) { + case TEST_INIT: + info->name = __PRETTY_FUNCTION__; + info->category = TEST_CATEGORY; + info->summary = "Format copying unit test"; + info->description = + "Test copying of a format"; + return AST_TEST_NOT_RUN; + case TEST_EXECUTE: + break; + } + + codec = ast_codec_get("ulaw", AST_MEDIA_TYPE_AUDIO, 8000); + if (!codec) { + ast_test_status_update(test, "Could not retrieve built-in ulaw codec\n"); + return AST_TEST_FAIL; + } + + format = ast_format_create(codec); + if (!format) { + ast_test_status_update(test, "Could not create format using built-in codec\n"); + return AST_TEST_FAIL; + } + + copy = ao2_bump(format); + if (!copy) { + ast_test_status_update(test, "Copying of a just created format failed\n"); + return AST_TEST_FAIL; + } else if (copy != format) { + ast_test_status_update(test, "Copying of a format returned a new format instead of the same one\n"); + return AST_TEST_FAIL; + } + + return AST_TEST_PASS; +} + +AST_TEST_DEFINE(format_attribute_set_without_interface) +{ + RAII_VAR(struct ast_codec *, codec, NULL, ao2_cleanup); + RAII_VAR(struct ast_format *, format, NULL, ao2_cleanup); + + switch (cmd) { + case TEST_INIT: + info->name = __PRETTY_FUNCTION__; + info->category = TEST_CATEGORY; + info->summary = "Format attribute setting unit test"; + info->description = + "Test that attribute setting on a format without an interface fails"; + return AST_TEST_NOT_RUN; + case TEST_EXECUTE: + break; + } + + codec = ast_codec_get("ulaw", AST_MEDIA_TYPE_AUDIO, 8000); + if (!codec) { + ast_test_status_update(test, "Could not retrieve built-in ulaw codec\n"); + return AST_TEST_FAIL; + } + + format = ast_format_create(codec); + if (!format) { + ast_test_status_update(test, "Could not create format using built-in codec\n"); + return AST_TEST_FAIL; + } + + if (!ast_format_attribute_set(format, "bees", "cool")) { + ast_test_status_update(test, "Successfully set an attribute on a format without an interface\n"); + return AST_TEST_FAIL; + } + + return AST_TEST_PASS; +} + +AST_TEST_DEFINE(format_parse_sdp_fmtp_without_interface) +{ + RAII_VAR(struct ast_codec *, codec, NULL, ao2_cleanup); + RAII_VAR(struct ast_format *, format, NULL, ao2_cleanup); + RAII_VAR(struct ast_format *, generated, NULL, ao2_cleanup); + + switch (cmd) { + case TEST_INIT: + info->name = __PRETTY_FUNCTION__; + info->category = TEST_CATEGORY; + info->summary = "Format sdp parse unit test"; + info->description = + "Test that sdp parsing on a format without an interface fails"; + return AST_TEST_NOT_RUN; + case TEST_EXECUTE: + break; + } + + codec = ast_codec_get("ulaw", AST_MEDIA_TYPE_AUDIO, 8000); + if (!codec) { + ast_test_status_update(test, "Could not retrieve built-in ulaw codec\n"); + return AST_TEST_FAIL; + } + + format = ast_format_create(codec); + if (!format) { + ast_test_status_update(test, "Could not create format using built-in codec\n"); + return AST_TEST_FAIL; + } + + generated = ast_format_parse_sdp_fmtp(format, "tacos"); + if (generated != format) { + ast_test_status_update(test, "Successfully parsed SDP on a format without an interface\n"); + return AST_TEST_FAIL; + } + + return AST_TEST_PASS; +} + +AST_TEST_DEFINE(format_parse_and_generate_sdp_fmtp) +{ + RAII_VAR(struct ast_codec *, codec, NULL, ao2_cleanup); + RAII_VAR(struct ast_format *, format, NULL, ao2_cleanup); + RAII_VAR(struct ast_format *, generated, NULL, ao2_cleanup); + struct ast_str *fmtp = ast_str_alloca(64); + + switch (cmd) { + case TEST_INIT: + info->name = __PRETTY_FUNCTION__; + info->category = TEST_CATEGORY; + info->summary = "Format sdp parse/generate unit test"; + info->description = + "Test that sdp parsing and generation on a format with an interface succeeds"; + return AST_TEST_NOT_RUN; + case TEST_EXECUTE: + break; + } + + codec = ast_codec_get("test_core_format_codec", AST_MEDIA_TYPE_AUDIO, 8000); + if (!codec) { + ast_test_status_update(test, "Could not retrieve test_core_format_codec codec\n"); + return AST_TEST_FAIL; + } + + format = ast_format_create(codec); + if (!format) { + ast_test_status_update(test, "Could not create format using test_core_format_codec codec\n"); + return AST_TEST_FAIL; + } + + generated = ast_format_parse_sdp_fmtp(format, "one=1000;two=256"); + if (format == generated) { + ast_test_status_update(test, "Failed to parse SDP on a format without an interface\n"); + return AST_TEST_FAIL; + } + + ast_format_generate_sdp_fmtp(generated, 8, &fmtp); + + ast_test_validate(test, strcmp("a=fmtp:8 one=1000;two=256\r\n", ast_str_buffer(fmtp)) == 0); + ast_test_validate(test, test_callbacks_called.format_parse_sdp_fmtp == 1); + ast_test_validate(test, test_callbacks_called.format_generate_sdp_fmtp == 1); + + return AST_TEST_PASS; +} + +static int test_core_format_init(struct ast_test_info *info, struct ast_test *test) +{ + memset(&test_callbacks_called, 0, sizeof(test_callbacks_called)); + + return 0; +} + +static int unload_module(void) +{ + AST_TEST_UNREGISTER(format_create); + AST_TEST_UNREGISTER(format_create_attr); + AST_TEST_UNREGISTER(format_clone); + AST_TEST_UNREGISTER(format_cmp_same_codec); + AST_TEST_UNREGISTER(format_attr_cmp_same_codec); + AST_TEST_UNREGISTER(format_cmp_different_codec); + AST_TEST_UNREGISTER(format_joint_same_codec); + AST_TEST_UNREGISTER(format_attr_joint_same_codec); + AST_TEST_UNREGISTER(format_joint_different_codec); + AST_TEST_UNREGISTER(format_copy); + AST_TEST_UNREGISTER(format_attribute_set_without_interface); + AST_TEST_UNREGISTER(format_parse_sdp_fmtp_without_interface); + AST_TEST_UNREGISTER(format_parse_and_generate_sdp_fmtp); + + return 0; +} + +static int load_module(void) +{ + /* Test codec/format interface used by this module */ + if (ast_codec_register(&test_core_format_codec)) { + ast_log(AST_LOG_ERROR, "Failed to register test_core_format_codec\n"); + return AST_MODULE_LOAD_DECLINE; + } + + if (ast_format_interface_register("test_core_format_codec", &test_core_format_attr)) { + ast_log(AST_LOG_ERROR, "Failed to register format interface for test_core_format_codec\n"); + return AST_MODULE_LOAD_DECLINE; + } + + AST_TEST_REGISTER(format_create); + AST_TEST_REGISTER(format_create_attr); + AST_TEST_REGISTER(format_clone); + AST_TEST_REGISTER(format_cmp_same_codec); + AST_TEST_REGISTER(format_attr_cmp_same_codec); + AST_TEST_REGISTER(format_cmp_different_codec); + AST_TEST_REGISTER(format_joint_same_codec); + AST_TEST_REGISTER(format_attr_joint_same_codec); + AST_TEST_REGISTER(format_joint_different_codec); + AST_TEST_REGISTER(format_copy); + AST_TEST_REGISTER(format_attribute_set_without_interface); + AST_TEST_REGISTER(format_parse_sdp_fmtp_without_interface); + AST_TEST_REGISTER(format_parse_and_generate_sdp_fmtp); + + ast_test_register_init(TEST_CATEGORY, &test_core_format_init); + + return AST_MODULE_LOAD_SUCCESS; +} + +AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Core format API test module"); |