summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorMatt Jordan <mjordan@digium.com>2015-08-09 17:56:48 -0500
committerMatt Jordan <mjordan@digium.com>2015-08-09 17:56:48 -0500
commita0f451c35ed56b08353c4c3150bf847867f74fe7 (patch)
treecee624042cb44855fb0d94683a19411e73084976 /tests
parentc3bd7fb83583b357add3638ea1022040197ad250 (diff)
main/format: Add an API call for retrieving format attributes
Some codecs that may be a third party library to Asterisk need to have knowledge of the format attributes that were negotiated. Unfortunately, when the great format migration of Asterisk 13 occurred, that ability was lost. This patch adds an API call, ast_format_attribute_get, to the core format API, along with updates to the unit test to check the new API call. A new callback is also now available for format attribute modules, such that they can provide the format attribute values they manage. Note that the API returns a void *. This is done as the format attribute modules themselves may store format attributes in any particular manner they like. Care should be taken by consumers of the API to check the return value before casting and dereferencing. Consumers will obviously need to have a priori knowledge of the type of the format attribute as well. Change-Id: Ieec76883dfb46ecd7aff3dc81a52c81f4dc1b9e3
Diffstat (limited to 'tests')
-rw-r--r--tests/test_core_format.c105
1 files changed, 105 insertions, 0 deletions
diff --git a/tests/test_core_format.c b/tests/test_core_format.c
index a6614d6a3..a3819c6d0 100644
--- a/tests/test_core_format.c
+++ b/tests/test_core_format.c
@@ -45,6 +45,7 @@ static int test_core_format_clone(const struct ast_format *src, struct ast_forma
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 const void *test_core_format_attribute_get(const struct ast_format *format, const char *name);
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);
@@ -55,6 +56,7 @@ static struct ast_format_interface test_core_format_attr = {
.format_cmp = &test_core_format_cmp,
.format_get_joint = &test_core_format_get_joint,
.format_attribute_set = &test_core_format_attribute_set,
+ .format_attribute_get = &test_core_format_attribute_get,
.format_parse_sdp_fmtp = &test_core_format_parse_sdp_fmtp,
.format_generate_sdp_fmtp = &test_core_format_generate_sdp_fmtp,
};
@@ -202,6 +204,19 @@ static struct ast_format *test_core_format_attribute_set(const struct ast_format
return clone;
}
+/*! \brief Format attribute callback for retrieving an attribute */
+static const void *test_core_format_attribute_get(const struct ast_format *format, const char *name)
+{
+ struct test_core_format_pvt *pvt = ast_format_get_attribute_data(format);
+
+ if (!strcmp(name, "one")) {
+ return &pvt->field_one;
+ } else if (!strcmp(name, "two")) {
+ return &pvt->field_two;
+ }
+ return NULL;
+}
+
/*! \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)
{
@@ -333,6 +348,55 @@ AST_TEST_DEFINE(format_create_attr)
return AST_TEST_PASS;
}
+AST_TEST_DEFINE(format_retrieve_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 attribute retrieval unit test";
+ info->description =
+ "Test retrieval of format 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;
+ }
+
+ 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;
+ }
+
+ if (*((int *)ast_format_attribute_get(format_w_attr, "one")) != 1) {
+ ast_test_status_update(test, "Could not retrieve valid format attribute\n");
+ return AST_TEST_FAIL;
+ }
+
+ if (ast_format_attribute_get(format_w_attr, "foo") != NULL) {
+ ast_test_status_update(test, "Retrieved invalid format attribute\n");
+ return AST_TEST_FAIL;
+ }
+
+ return AST_TEST_PASS;
+}
+
AST_TEST_DEFINE(format_clone)
{
RAII_VAR(struct ast_codec *, codec, NULL, ao2_cleanup);
@@ -829,6 +893,43 @@ AST_TEST_DEFINE(format_attribute_set_without_interface)
return AST_TEST_PASS;
}
+AST_TEST_DEFINE(format_attribute_get_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 retrieval unit test";
+ info->description =
+ "Test that attribute retrieval 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_get(format, "bees") != NULL) {
+ ast_test_status_update(test, "Successfully retrieved 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);
@@ -925,6 +1026,7 @@ static int unload_module(void)
{
AST_TEST_UNREGISTER(format_create);
AST_TEST_UNREGISTER(format_create_attr);
+ AST_TEST_UNREGISTER(format_retrieve_attr);
AST_TEST_UNREGISTER(format_clone);
AST_TEST_UNREGISTER(format_cmp_same_codec);
AST_TEST_UNREGISTER(format_attr_cmp_same_codec);
@@ -934,6 +1036,7 @@ static int unload_module(void)
AST_TEST_UNREGISTER(format_joint_different_codec);
AST_TEST_UNREGISTER(format_copy);
AST_TEST_UNREGISTER(format_attribute_set_without_interface);
+ AST_TEST_UNREGISTER(format_attribute_get_without_interface);
AST_TEST_UNREGISTER(format_parse_sdp_fmtp_without_interface);
AST_TEST_UNREGISTER(format_parse_and_generate_sdp_fmtp);
@@ -955,6 +1058,7 @@ static int load_module(void)
AST_TEST_REGISTER(format_create);
AST_TEST_REGISTER(format_create_attr);
+ AST_TEST_REGISTER(format_retrieve_attr);
AST_TEST_REGISTER(format_clone);
AST_TEST_REGISTER(format_cmp_same_codec);
AST_TEST_REGISTER(format_attr_cmp_same_codec);
@@ -964,6 +1068,7 @@ static int load_module(void)
AST_TEST_REGISTER(format_joint_different_codec);
AST_TEST_REGISTER(format_copy);
AST_TEST_REGISTER(format_attribute_set_without_interface);
+ AST_TEST_REGISTER(format_attribute_get_without_interface);
AST_TEST_REGISTER(format_parse_sdp_fmtp_without_interface);
AST_TEST_REGISTER(format_parse_and_generate_sdp_fmtp);