[svn-commits] mjordan: branch group/media_formats-reviewed-trunk r418435 - in /team/group/m...

SVN commits to the Digium repositories svn-commits at lists.digium.com
Fri Jul 11 21:02:16 CDT 2014


Author: mjordan
Date: Fri Jul 11 21:02:05 2014
New Revision: 418435

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=418435
Log:
media-formats: Merge updated unit tests, add ast_format_none

This patch does the following:

* It updates a few of the unit tests for some of the API changes. In
  particular, it focuses on adding some tests for formats with attributes and
  their expected behaviour. A few other non-format related unit tests were
  updated as well to handle off nominals detected during testing.

* It adds an 'ast_format_none' format. This format is a dummy format that can
  be used instead of a NULL pointer to prevent having to put NULL dereference
  checks into every place in the codebase. Channels are assigned this format
  immediately upon creation, and their default capabilities are set to have
  it. As this format's codec has no translation (nor a representation in the
  RTP engine), it _shouldn't_ cause harm.

* A few NULL checks were put in anyway into key areas in a few modules. These
  were ones that were hit hard by the unit tests and prone to crashing if
  presented a NULL format.

Review: https://reviewboard.asterisk.org/r/3734/

Modified:
    team/group/media_formats-reviewed-trunk/include/asterisk/codec.h
    team/group/media_formats-reviewed-trunk/include/asterisk/format_cache.h
    team/group/media_formats-reviewed-trunk/main/bridge_channel.c
    team/group/media_formats-reviewed-trunk/main/channel.c
    team/group/media_formats-reviewed-trunk/main/codec.c
    team/group/media_formats-reviewed-trunk/main/codec_builtin.c
    team/group/media_formats-reviewed-trunk/main/format_cache.c
    team/group/media_formats-reviewed-trunk/main/format_cap.c
    team/group/media_formats-reviewed-trunk/tests/test_cel.c
    team/group/media_formats-reviewed-trunk/tests/test_core_format.c
    team/group/media_formats-reviewed-trunk/tests/test_format_cache.c
    team/group/media_formats-reviewed-trunk/tests/test_format_cap.c

Modified: team/group/media_formats-reviewed-trunk/include/asterisk/codec.h
URL: http://svnview.digium.com/svn/asterisk/team/group/media_formats-reviewed-trunk/include/asterisk/codec.h?view=diff&rev=418435&r1=418434&r2=418435
==============================================================================
--- team/group/media_formats-reviewed-trunk/include/asterisk/codec.h (original)
+++ team/group/media_formats-reviewed-trunk/include/asterisk/codec.h Fri Jul 11 21:02:05 2014
@@ -34,6 +34,8 @@
 	AST_MEDIA_TYPE_IMAGE,
 	AST_MEDIA_TYPE_TEXT,
 };
+
+struct ast_module;
 
 /*! \brief Represents a media codec within Asterisk. */
 struct ast_codec {
@@ -73,6 +75,8 @@
 	int (*get_length)(unsigned int samples);
 	/*! \brief Whether the media can be smoothed or not */
 	unsigned int smooth;
+	/*! \brief The module that registered this codec */
+	struct ast_module *mod;
 };
 
 /*!

Modified: team/group/media_formats-reviewed-trunk/include/asterisk/format_cache.h
URL: http://svnview.digium.com/svn/asterisk/team/group/media_formats-reviewed-trunk/include/asterisk/format_cache.h?view=diff&rev=418435&r1=418434&r2=418435
==============================================================================
--- team/group/media_formats-reviewed-trunk/include/asterisk/format_cache.h (original)
+++ team/group/media_formats-reviewed-trunk/include/asterisk/format_cache.h Fri Jul 11 21:02:05 2014
@@ -224,6 +224,11 @@
 extern struct ast_format *ast_format_vp8;
 
 /*!
+ * \brief Built-in "null" format.
+ */
+extern struct ast_format *ast_format_none;
+
+/*!
  * \brief Initialize format cache support within the core.
  *
  * \retval 0 success
@@ -234,13 +239,12 @@
 /*!
  * \brief Set a named format cache entry.
  *
- * \param name Name of the cached format
  * \param format A pointer to the format to cache
  *
  * \retval 0 success
  * \retval -1 failure
  */
-int ast_format_cache_set(const char *name, struct ast_format *format);
+int ast_format_cache_set(struct ast_format *format);
 
 /*!
  * \brief Retrieve a named format from the cache.

Modified: team/group/media_formats-reviewed-trunk/main/bridge_channel.c
URL: http://svnview.digium.com/svn/asterisk/team/group/media_formats-reviewed-trunk/main/bridge_channel.c?view=diff&rev=418435&r1=418434&r2=418435
==============================================================================
--- team/group/media_formats-reviewed-trunk/main/bridge_channel.c (original)
+++ team/group/media_formats-reviewed-trunk/main/bridge_channel.c Fri Jul 11 21:02:05 2014
@@ -320,6 +320,9 @@
 
 void ast_bridge_channel_restore_formats(struct ast_bridge_channel *bridge_channel)
 {
+	ast_assert(bridge_channel->read_format != NULL);
+	ast_assert(bridge_channel->write_format != NULL);
+
 	/* Restore original formats of the channel as they came in */
 	if (ast_format_cmp(ast_channel_readformat(bridge_channel->chan), bridge_channel->read_format) == AST_FORMAT_CMP_NOT_EQUAL) {
 		ast_debug(1, "Bridge is returning %p(%s) to read format %s\n",

Modified: team/group/media_formats-reviewed-trunk/main/channel.c
URL: http://svnview.digium.com/svn/asterisk/team/group/media_formats-reviewed-trunk/main/channel.c?view=diff&rev=418435&r1=418434&r2=418435
==============================================================================
--- team/group/media_formats-reviewed-trunk/main/channel.c (original)
+++ team/group/media_formats-reviewed-trunk/main/channel.c Fri Jul 11 21:02:05 2014
@@ -815,8 +815,14 @@
 		/* format capabilities structure allocation failure */
 		return ast_channel_unref(tmp);
 	}
+	ast_format_cap_append(nativeformats, ast_format_none, 0);
 	ast_channel_nativeformats_set(tmp, nativeformats);
 	ao2_ref(nativeformats, -1);
+
+	ast_channel_set_rawwriteformat(tmp, ast_format_none);
+	ast_channel_set_rawreadformat(tmp, ast_format_none);
+	ast_channel_set_writeformat(tmp, ast_format_none);
+	ast_channel_set_readformat(tmp, ast_format_none);
 
 	/*
 	 * Init file descriptors to unopened state so
@@ -2935,8 +2941,9 @@
 		generate = ast_channel_generator(chan)->generate;
 	ast_channel_unlock(chan);
 
-	if (!tmp || !generate)
+	if (!tmp || !generate) {
 		return 0;
+	}
 
 	res = generate(chan, tmp, 0, ast_format_get_sample_rate(ast_channel_writeformat(chan)) / 50);
 
@@ -5247,6 +5254,9 @@
 	RAII_VAR(struct ast_format *, best_native_fmt, NULL, ao2_cleanup);
 	int res;
 
+	ast_assert(format != NULL);
+	ast_assert(rawformat != NULL);
+
 	/* See if the underlying channel driver is capable of performing transcoding for us */
 	if (!ast_channel_setoption(chan, direction ? AST_OPTION_FORMAT_WRITE : AST_OPTION_FORMAT_READ, &best_set_fmt, sizeof(best_set_fmt), 0)) {
 		ast_debug(1, "Channel driver natively set channel %s to %s format %s\n", ast_channel_name(chan),
@@ -5367,6 +5377,8 @@
 	struct ast_format_cap *cap = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
 	int res;
 
+	ast_assert(format != NULL);
+
 	if (!cap) {
 		return -1;
 	}
@@ -5397,6 +5409,8 @@
 {
 	struct ast_format_cap *cap = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
 	int res;
+
+	ast_assert(format != NULL);
 
 	if (!cap) {
 		return -1;

Modified: team/group/media_formats-reviewed-trunk/main/codec.c
URL: http://svnview.digium.com/svn/asterisk/team/group/media_formats-reviewed-trunk/main/codec.c?view=diff&rev=418435&r1=418434&r2=418435
==============================================================================
--- team/group/media_formats-reviewed-trunk/main/codec.c (original)
+++ team/group/media_formats-reviewed-trunk/main/codec.c Fri Jul 11 21:02:05 2014
@@ -250,6 +250,15 @@
 	return 0;
 }
 
+static void codec_dtor(void *obj)
+{
+	struct ast_codec *codec;
+
+	codec = obj;
+
+	ast_module_unref(codec->mod);
+}
+
 int __ast_codec_register(struct ast_codec *codec, struct ast_module *mod)
 {
 	SCOPED_AO2WRLOCK(lock, codecs);
@@ -275,7 +284,7 @@
 		return -1;
 	}
 
-	codec_new = ao2_alloc_options(sizeof(*codec_new), NULL, AO2_ALLOC_OPT_LOCK_NOLOCK);
+	codec_new = ao2_alloc_options(sizeof(*codec_new), codec_dtor, AO2_ALLOC_OPT_LOCK_NOLOCK);
 	if (!codec_new) {
 		ast_log(LOG_ERROR, "Could not allocate a codec with name '%s' of type '%s' and sample rate '%u'\n",
 			codec->name, ast_codec_media_type2str(codec->type), codec->sample_rate);

Modified: team/group/media_formats-reviewed-trunk/main/codec_builtin.c
URL: http://svnview.digium.com/svn/asterisk/team/group/media_formats-reviewed-trunk/main/codec_builtin.c?view=diff&rev=418435&r1=418434&r2=418435
==============================================================================
--- team/group/media_formats-reviewed-trunk/main/codec_builtin.c (original)
+++ team/group/media_formats-reviewed-trunk/main/codec_builtin.c Fri Jul 11 21:02:05 2014
@@ -101,6 +101,27 @@
 	.default_ms = 30,
 	.samples_count = g723_samples,
 	.get_length = g723_length,
+};
+
+static int none_samples(struct ast_frame *frame)
+{
+	return frame->datalen;
+}
+
+static int none_length(unsigned int samples) {
+	return samples;
+}
+
+static struct ast_codec none = {
+	.name = "none",
+	.description = "<Null> codec",
+	.type = AST_MEDIA_TYPE_AUDIO,
+	.sample_rate = 8000, /* This must have some sample rate to prevent divide by 0 */
+	.minimum_ms = 10,
+	.maximum_ms = 150,
+	.default_ms = 20,
+	.samples_count = none_samples,
+	.get_length = none_length,
 };
 
 static int ulaw_samples(struct ast_frame *frame)
@@ -727,7 +748,7 @@
 		res |= __ast_codec_register(&(codec), NULL); \
 		__codec_ ## __LINE__ = ast_codec_get((codec).name, (codec).type, (codec).sample_rate); \
 		__fmt_ ## __LINE__ = ast_format_create(__codec_ ## __LINE__); \
-		res |= ast_format_cache_set((codec).name, __fmt_ ## __LINE__); \
+		res |= ast_format_cache_set(__fmt_ ## __LINE__); \
 		ao2_ref(__fmt_ ## __LINE__, -1); \
 		ao2_ref(__codec_ ## __LINE__, -1); \
 		__res_ ## __LINE__; \
@@ -741,7 +762,7 @@
 		res |= __ast_codec_register(&(codec), NULL); \
 		__codec_ ## __LINE__ = ast_codec_get((codec).name, (codec).type, (codec).sample_rate); \
 		__fmt_ ## __LINE__ = ast_format_create_named((format_name), __codec_ ## __LINE__); \
-		res |= ast_format_cache_set((format_name), __fmt_ ## __LINE__); \
+		res |= ast_format_cache_set(__fmt_ ## __LINE__); \
 		ao2_ref(__fmt_ ## __LINE__, -1); \
 		ao2_ref(__codec_ ## __LINE__, -1); \
 		__res_ ## __LINE__; \
@@ -789,6 +810,7 @@
 	res |= CODEC_REGISTER_AND_CACHE(vp8);
 	res |= CODEC_REGISTER_AND_CACHE(t140red);
 	res |= CODEC_REGISTER_AND_CACHE(t140);
+	res |= CODEC_REGISTER_AND_CACHE(none);
 
 	return res;
 }

Modified: team/group/media_formats-reviewed-trunk/main/format_cache.c
URL: http://svnview.digium.com/svn/asterisk/team/group/media_formats-reviewed-trunk/main/format_cache.c?view=diff&rev=418435&r1=418434&r2=418435
==============================================================================
--- team/group/media_formats-reviewed-trunk/main/format_cache.c (original)
+++ team/group/media_formats-reviewed-trunk/main/format_cache.c Fri Jul 11 21:02:05 2014
@@ -231,6 +231,11 @@
  * \brief Built-in cached vp8 format.
  */
 struct ast_format *ast_format_vp8;
+
+/*!
+ * \brief Built-in "null" format.
+ */
+struct ast_format *ast_format_none;
 
 /*! \brief Number of buckets to use for the media format cache (should be prime for performance reasons) */
 #define CACHE_BUCKETS 53
@@ -330,6 +335,7 @@
 	ao2_replace(ast_format_vp8, NULL);
 	ao2_replace(ast_format_t140_red, NULL);
 	ao2_replace(ast_format_t140, NULL);
+	ao2_replace(ast_format_none, NULL);
 }
 
 int ast_format_cache_init(void)
@@ -423,34 +429,33 @@
 		ao2_replace(ast_format_t140_red, format);
 	} else if (!strcmp(name, "t140")) {
 		ao2_replace(ast_format_t140, format);
-	}
-}
-
-int ast_format_cache_set(const char *name, struct ast_format *format)
+	} else if (!strcmp(name, "none")) {
+		ao2_replace(ast_format_none, format);
+	}
+}
+
+int ast_format_cache_set(struct ast_format *format)
 {
 	SCOPED_AO2WRLOCK(lock, formats);
 	struct ast_format *old_format;
 
-	if (ast_strlen_zero(name)) {
+	ast_assert(format != NULL);
+
+	if (ast_strlen_zero(ast_format_get_name(format))) {
 		return -1;
 	}
 
-	old_format = ao2_find(formats, name, OBJ_SEARCH_KEY | OBJ_NOLOCK);
+	old_format = ao2_find(formats, ast_format_get_name(format), OBJ_SEARCH_KEY | OBJ_NOLOCK);
 	if (old_format) {
 		ao2_unlink_flags(formats, old_format, OBJ_NOLOCK);
-	} else if (!format) {
-		/* No old format and no new format provided */
-		return 0;
-	}
-
-	if (format) {
-		ao2_link_flags(formats, format, OBJ_NOLOCK);
-	}
-
-	set_cached_format(name, format);
+	}
+	ao2_link_flags(formats, format, OBJ_NOLOCK);
+
+	set_cached_format(ast_format_get_name(format), format);
 
 	ast_verb(2, "%s cached format with name '%s'\n",
-		old_format ? (format ? "Updated" : "Removed") : "Created", name);
+		old_format ? "Updated" : "Created",
+		ast_format_get_name(format));
 
 	ao2_cleanup(old_format);
 

Modified: team/group/media_formats-reviewed-trunk/main/format_cap.c
URL: http://svnview.digium.com/svn/asterisk/team/group/media_formats-reviewed-trunk/main/format_cap.c?view=diff&rev=418435&r1=418434&r2=418435
==============================================================================
--- team/group/media_formats-reviewed-trunk/main/format_cap.c (original)
+++ team/group/media_formats-reviewed-trunk/main/format_cap.c Fri Jul 11 21:02:05 2014
@@ -191,6 +191,8 @@
 {
 	struct format_cap_framed *framed;
 
+	ast_assert(format != NULL);
+
 	if (format_in_format_cap(cap, format)) {
 		return 0;
 	}
@@ -207,6 +209,8 @@
 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;
@@ -347,6 +351,7 @@
 
 	framed = AST_VECTOR_GET(&cap->preference_order, position);
 
+	ast_assert(framed->format != ast_format_none);
 	ao2_ref(framed->format, +1);
 	return framed->format;
 }
@@ -364,6 +369,7 @@
 
 		if (ast_format_get_type(framed->format) == type) {
 			ao2_ref(framed->format, +1);
+			ast_assert(framed->format != ast_format_none);
 			return framed->format;
 		}
 	}
@@ -434,6 +440,8 @@
 {
 	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;
@@ -485,6 +493,8 @@
 	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;
 	}
@@ -518,6 +528,8 @@
 	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;

Modified: team/group/media_formats-reviewed-trunk/tests/test_cel.c
URL: http://svnview.digium.com/svn/asterisk/team/group/media_formats-reviewed-trunk/tests/test_cel.c?view=diff&rev=418435&r1=418434&r2=418435
==============================================================================
--- team/group/media_formats-reviewed-trunk/tests/test_cel.c (original)
+++ team/group/media_formats-reviewed-trunk/tests/test_cel.c Fri Jul 11 21:02:05 2014
@@ -38,6 +38,7 @@
 #include "asterisk/test.h"
 #include "asterisk/cel.h"
 #include "asterisk/channel.h"
+#include "asterisk/format_cache.h"
 #include "asterisk/linkedlists.h"
 #include "asterisk/chanvars.h"
 #include "asterisk/utils.h"
@@ -1317,7 +1318,10 @@
 	do_sleep();
 
 	/* Perform attended transfer */
-	ast_bridge_transfer_attended(chan_alice, chan_david);
+	if (ast_bridge_transfer_attended(chan_alice, chan_david)) {
+		ast_test_status_update(test, "Attended transfer failed!\n");
+		return AST_TEST_FAIL;
+	}
 	do_sleep();
 	BRIDGE_ENTER_EVENT_PEER(chan_bob, bridge2, "CELTestChannel/David,CELTestChannel/Charlie");
 
@@ -1397,7 +1401,10 @@
 	BRIDGE_ENTER(chan_david, bridge2);
 
 	/* Perform attended transfer */
-	ast_bridge_transfer_attended(chan_alice, chan_david);
+	if (ast_bridge_transfer_attended(chan_alice, chan_david)) {
+		ast_test_status_update(test, "Attended transfer failed!\n");
+		return AST_TEST_FAIL;
+	}
 	do_sleep();
 	BRIDGE_EXIT_EVENT_PEER(chan_charlie, bridge2, "CELTestChannel/David");
 	BRIDGE_ENTER_EVENT_PEER(chan_charlie, bridge1, "CELTestChannel/Bob,CELTestChannel/Alice");

Modified: team/group/media_formats-reviewed-trunk/tests/test_core_format.c
URL: http://svnview.digium.com/svn/asterisk/team/group/media_formats-reviewed-trunk/tests/test_core_format.c?view=diff&rev=418435&r1=418434&r2=418435
==============================================================================
--- team/group/media_formats-reviewed-trunk/tests/test_core_format.c (original)
+++ team/group/media_formats-reviewed-trunk/tests/test_core_format.c Fri Jul 11 21:02:05 2014
@@ -38,6 +38,205 @@
 #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);
@@ -45,9 +244,9 @@
 
 	switch (cmd) {
 	case TEST_INIT:
-		info->name = "format_create";
-		info->category = "/main/core_format/";
-		info->summary = "format creation unit test";
+		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;
@@ -70,6 +269,149 @@
 		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;
 }
 
@@ -78,12 +420,13 @@
 	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);
-
-	switch (cmd) {
-	case TEST_INIT:
-		info->name = "format_cmp_same_codec";
-		info->category = "/main/core_format/";
-		info->summary = "format comparison unit test";
+	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;
@@ -109,7 +452,18 @@
 		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;
 	}
@@ -126,9 +480,9 @@
 
 	switch (cmd) {
 	case TEST_INIT:
-		info->name = "format_cmp_different_codec";
-		info->category = "/main/core_format/";
-		info->summary = "format comparison unit test";
+		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;
@@ -168,6 +522,68 @@
 	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);
@@ -177,9 +593,9 @@
 
 	switch (cmd) {
 	case TEST_INIT:
-		info->name = "format_joint_same_codec";
-		info->category = "/main/core_format/";
-		info->summary = "joint format unit test";
+		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;
@@ -213,6 +629,70 @@
 		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;
 }
@@ -227,9 +707,9 @@
 
 	switch (cmd) {
 	case TEST_INIT:
-		info->name = "format_joint_different_codec";
-		info->category = "/main/core_format/";
-		info->summary = "joint format unit test";
+		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;
@@ -278,9 +758,9 @@
 
 	switch (cmd) {
 	case TEST_INIT:
-		info->name = "format_copy";
-		info->category = "/main/core_format/";
-		info->summary = "format copying unit test";
+		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;
@@ -319,9 +799,9 @@
 
 	switch (cmd) {
 	case TEST_INIT:
-		info->name = "format_attribute_set_without_interface";
-		info->category = "/main/core_format/";
-		info->summary = "format attribute setting unit test";
+		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;
@@ -353,12 +833,13 @@
 {
 	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 = "format_parse_sdp_fmtp_without_interface";
-		info->category = "/main/core_format/";
-		info->summary = "format sdp parse unit test";
+	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;
@@ -378,7 +859,8 @@
 		return AST_TEST_FAIL;
 	}
 
-	if (!ast_format_parse_sdp_fmtp(format, "tacos")) {
+	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;
 	}
@@ -386,29 +868,107 @@
 	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) {

[... 636 lines stripped ...]



More information about the svn-commits mailing list