[asterisk-commits] rmudgett: branch rmudgett/iax_fracks r419879 - in /team/rmudgett/iax_fracks/c...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Thu Jul 31 17:41:41 CDT 2014


Author: rmudgett
Date: Thu Jul 31 17:41:36 2014
New Revision: 419879

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=419879
Log:
Straighten out the struct iax2_codec_pref usage.

Modified:
    team/rmudgett/iax_fracks/channels/chan_iax2.c
    team/rmudgett/iax_fracks/channels/iax2/codec_pref.c
    team/rmudgett/iax_fracks/channels/iax2/format_compatibility.c
    team/rmudgett/iax_fracks/channels/iax2/include/codec_pref.h

Modified: team/rmudgett/iax_fracks/channels/chan_iax2.c
URL: http://svnview.digium.com/svn/asterisk/team/rmudgett/iax_fracks/channels/chan_iax2.c?view=diff&rev=419879&r1=419878&r2=419879
==============================================================================
--- team/rmudgett/iax_fracks/channels/chan_iax2.c (original)
+++ team/rmudgett/iax_fracks/channels/chan_iax2.c Thu Jul 31 17:41:36 2014
@@ -1820,16 +1820,16 @@
 	int x;
 	struct ast_format *found_format = NULL;
 
-	for (x = 0; x < IAX2_CODEC_PREF_SIZE; x++) {
+	for (x = 0; x < ARRAY_LEN(pref->order); ++x) {
 		struct ast_format *pref_format;
-		uint64_t pref_as_bitfield = iax2_codec_pref_order_value_to_format_bitfield(pref->order[x]);
-
-		if (!pref_as_bitfield) {
+		uint64_t pref_bitfield;
+
+		pref_bitfield = iax2_codec_pref_order_value_to_format_bitfield(pref->order[x]);
+		if (!pref_bitfield) {
 			break;
 		}
 
-		pref_format = ast_format_compatibility_bitfield2format(pref_as_bitfield);
-
+		pref_format = ast_format_compatibility_bitfield2format(pref_bitfield);
 		found_format = ast_format_cap_get_compatible_format(cap, pref_format);
 		if (found_format) {
 			break;
@@ -1958,14 +1958,15 @@
 	}
 
 	/* We want to add the formats to the cap in the preferred order */
-	for (i = 0; i < IAX2_CODEC_PREF_SIZE; i++) {
-		uint64_t pref_as_bitfield = iax2_codec_pref_order_value_to_format_bitfield(pref->order[i]);
-
-		if (!pref_as_bitfield) {
+	for (i = 0; i < ARRAY_LEN(pref->order); ++i) {
+		uint64_t pref_bitfield;
+
+		pref_bitfield = iax2_codec_pref_order_value_to_format_bitfield(pref->order[i]);
+		if (!pref_bitfield) {
 			break;
 		}
 
-		if (iax2_format_compatibility_bitfield2cap(pref_as_bitfield, cap)) {
+		if (iax2_format_compatibility_bitfield2cap(pref_bitfield, cap)) {
 			ao2_ref(cap, -1);
 			return 1;
 		}

Modified: team/rmudgett/iax_fracks/channels/iax2/codec_pref.c
URL: http://svnview.digium.com/svn/asterisk/team/rmudgett/iax_fracks/channels/iax2/codec_pref.c?view=diff&rev=419879&r1=419878&r2=419879
==============================================================================
--- team/rmudgett/iax_fracks/channels/iax2/codec_pref.c (original)
+++ team/rmudgett/iax_fracks/channels/iax2/codec_pref.c Thu Jul 31 17:41:36 2014
@@ -38,6 +38,7 @@
 #include "asterisk/format_compatibility.h"
 #include "asterisk/format_cache.h"
 #include "asterisk/format_cap.h"
+#include "asterisk/utils.h"
 
 #include "include/codec_pref.h"
 #include "include/format_compatibility.h"
@@ -47,9 +48,9 @@
 	static int differential = (int) 'A';
 	int x;
 
-/* BUGBUG this function is totally hosed.  It has a size mismatch of the objects involved. */
 	if (right) {
-		for (x = 0; x < IAX2_CODEC_PREF_SIZE && x < size; x++) {
+		--size;/* Save room for the nul string terminator. */
+		for (x = 0; x < ARRAY_LEN(pref->order) && x < size; ++x) {
 			if (!pref->order[x]) {
 				break;
 			}
@@ -59,7 +60,7 @@
 
 		buf[x] = '\0';
 	} else {
-		for (x = 0; x < IAX2_CODEC_PREF_SIZE && x < size; x++) {
+		for (x = 0; x < ARRAY_LEN(pref->order) && x < size; ++x) {
 			if (buf[x] == '\0') {
 				break;
 			}
@@ -67,7 +68,7 @@
 			pref->order[x] = buf[x] - differential;
 		}
 
-		if (x < size) {
+		if (x < ARRAY_LEN(pref->order)) {
 			pref->order[x] = 0;
 		}
 	}
@@ -75,8 +76,11 @@
 
 struct ast_format *iax2_codec_pref_index(struct iax2_codec_pref *pref, int idx, struct ast_format **result)
 {
-	if ((idx >= 0) && (idx < sizeof(pref->order)) && pref->order[idx]) {
-		*result = ast_format_compatibility_bitfield2format(pref->order[idx]);
+	if (0 <= idx && idx < ARRAY_LEN(pref->order) && pref->order[idx]) {
+		uint64_t pref_bitfield;
+
+		pref_bitfield = iax2_codec_pref_order_value_to_format_bitfield(pref->order[idx]);
+		*result = ast_format_compatibility_bitfield2format(pref_bitfield);
 	} else {
 		*result = NULL;
 	}
@@ -88,11 +92,16 @@
 {
 	int idx;
 
-	for (idx = 0; idx < sizeof(pref->order); idx++) {
+	for (idx = 0; idx < ARRAY_LEN(pref->order); ++idx) {
+		uint64_t pref_bitfield;
+
 		if (!pref->order[idx]) {
 			break;
 		}
-		ast_format_cap_append(cap, ast_format_compatibility_bitfield2format(pref->order[idx]), pref->framing[idx]);
+		pref_bitfield = iax2_codec_pref_order_value_to_format_bitfield(pref->order[idx]);
+		ast_format_cap_append(cap,
+			ast_format_compatibility_bitfield2format(pref_bitfield),
+			pref->framing[idx]);
 	}
 }
 
@@ -114,13 +123,15 @@
 	}
 
 	/* Convert the preferences into a format cap so that we can read the format names */
-	for (x = 0; x < IAX2_CODEC_PREF_SIZE; x++) {
-		uint64_t bitfield = iax2_codec_pref_order_value_to_format_bitfield(pref->order[x]);
-		if (!bitfield) {
-			break;
-		}
-
-		iax2_format_compatibility_bitfield2cap(bitfield, cap);
+	for (x = 0; x < ARRAY_LEN(pref->order); ++x) {
+		uint64_t pref_bitfield;
+
+		pref_bitfield = iax2_codec_pref_order_value_to_format_bitfield(pref->order[x]);
+		if (!pref_bitfield) {
+			break;
+		}
+
+		iax2_format_compatibility_bitfield2cap(pref_bitfield, cap);
 	}
 
 	/* We know that at a minimum, 3 characters are used - (, ), and \0 */
@@ -176,7 +187,7 @@
 {
 	int x;
 
-	for (x = codec_pref_index; x < IAX2_CODEC_PREF_SIZE; x++) {
+	for (x = codec_pref_index; x < ARRAY_LEN(pref->order); ++x) {
 		pref->order[x] = pref->order[x + 1];
 		pref->framing[x] = pref->framing[x + 1];
 		if (!pref->order[x]) {
@@ -194,7 +205,7 @@
 		return;
 	}
 
-	for (x = 0; x < IAX2_CODEC_PREF_SIZE; x++) {
+	for (x = 0; x < ARRAY_LEN(pref->order); ++x) {
 		if (!pref->order[x]) {
 			break;
 		}
@@ -214,29 +225,31 @@
 		return;
 	}
 
-	for (x = 0; x < IAX2_CODEC_PREF_SIZE; x++) {
-		uint64_t format_as_bitfield = iax2_codec_pref_order_value_to_format_bitfield(pref->order[x]);
+	for (x = 0; x < ARRAY_LEN(pref->order); ++x) {
+		uint64_t pref_bitfield;
+
 		if (!pref->order[x]) {
 			break;
 		}
 
 		/* If this format isn't in the bitfield, remove it from the prefs. */
-		if (!(format_as_bitfield & bitfield)) {
+		pref_bitfield = iax2_codec_pref_order_value_to_format_bitfield(pref->order[x]);
+		if (!(pref_bitfield & bitfield)) {
 			codec_pref_remove_index(pref, x);
 		}
 	}
 }
 
-uint64_t iax2_codec_pref_order_value_to_format_bitfield(uint64_t order_value)
+uint64_t iax2_codec_pref_order_value_to_format_bitfield(int order_value)
 {
 	if (!order_value) {
 		return 0;
 	}
 
-	return 1 << (order_value - 1);
-}
-
-uint64_t iax2_codec_pref_format_bitfield_to_order_value(uint64_t bitfield)
+	return 1ULL << (order_value - 1);
+}
+
+int iax2_codec_pref_format_bitfield_to_order_value(uint64_t bitfield)
 {
 	int format_index = 1;
 
@@ -253,41 +266,58 @@
 }
 
 /*! \brief Append codec to list */
-int iax2_codec_pref_append(struct iax2_codec_pref *pref, struct ast_format *format, unsigned int framing)
-{
-	uint64_t bitfield = ast_format_compatibility_format2bitfield(format);
-	int format_index = iax2_codec_pref_format_bitfield_to_order_value(bitfield);
-	int x;
-
+void iax2_codec_pref_append(struct iax2_codec_pref *pref, struct ast_format *format, unsigned int framing)
+{
+	uint64_t bitfield;
+	int format_index;
+	int x;
+
+	bitfield = ast_format_compatibility_format2bitfield(format);
+	if (!bitfield) {
+		return;
+	}
+
+	format_index = iax2_codec_pref_format_bitfield_to_order_value(bitfield);
 	codec_pref_remove(pref, format_index);
 
-	for (x = 0; x < IAX2_CODEC_PREF_SIZE; x++) {
+	for (x = 0; x < ARRAY_LEN(pref->order); ++x) {
 		if (!pref->order[x]) {
 			pref->order[x] = format_index;
 			pref->framing[x] = framing;
 			break;
 		}
 	}
-
-	return x;
 }
 
 /*! \brief Prepend codec to list */
 void iax2_codec_pref_prepend(struct iax2_codec_pref *pref, struct ast_format *format, unsigned int framing,
 	int only_if_existing)
 {
-	uint64_t bitfield = ast_format_compatibility_format2bitfield(format);
-	int x;
+	uint64_t bitfield;
+	int format_index;
+	int x;
+
+	bitfield = ast_format_compatibility_format2bitfield(format);
+	if (!bitfield) {
+		return;
+	}
 
 	/* Now find any existing occurrence, or the end */
-	for (x = 0; x < IAX2_CODEC_PREF_SIZE; x++) {
-		if (!pref->order[x] || pref->order[x] == bitfield)
-			break;
-	}
-
-	/* If we failed to find any occurrence, set to the end */
-	if (x == IAX2_CODEC_PREF_SIZE) {
-		--x;
+	format_index = iax2_codec_pref_format_bitfield_to_order_value(bitfield);
+	for (x = 0; x < ARRAY_LEN(pref->order); ++x) {
+		if (!pref->order[x] || pref->order[x] == format_index)
+			break;
+	}
+
+	/*
+	 * The array can never be full without format_index
+	 * also being in the array.
+	 */
+	ast_assert(x < ARRAY_LEN(pref->order));
+
+	/* If we failed to find any occurrence, set to the end for safety. */
+	if (ARRAY_LEN(pref->order) <= x) {
+		x = ARRAY_LEN(pref->order) - 1;
 	}
 
 	if (only_if_existing && !pref->order[x]) {
@@ -296,19 +326,19 @@
 
 	/* Move down to make space to insert - either all the way to the end,
 	   or as far as the existing location (which will be overwritten) */
-	for (; x > 0; x--) {
+	for (; x > 0; --x) {
 		pref->order[x] = pref->order[x - 1];
 		pref->framing[x] = pref->framing[x - 1];
 	}
 
 	/* And insert the new entry */
-	pref->order[0] = bitfield;
+	pref->order[0] = format_index;
 	pref->framing[0] = framing;
 }
 
 unsigned int iax2_codec_pref_getsize(struct iax2_codec_pref *pref, int idx)
 {
-	if ((idx >= 0) && (idx < sizeof(pref->order)) && pref->order[idx]) {
+	if (0 <= idx && idx < ARRAY_LEN(pref->order) && pref->order[idx]) {
 		return pref->framing[idx];
 	} else {
 		return 0;
@@ -319,10 +349,14 @@
 {
 	int idx;
 
-	for (idx = 0; idx < sizeof(pref->order); idx++) {
+	for (idx = 0; idx < ARRAY_LEN(pref->order); ++idx) {
+		uint64_t pref_bitfield;
+
 		if (!pref->order[idx]) {
 			break;
-		} else if (ast_format_cmp(ast_format_compatibility_bitfield2format(pref->order[idx]),
+		}
+		pref_bitfield = iax2_codec_pref_order_value_to_format_bitfield(pref->order[idx]);
+		if (ast_format_cmp(ast_format_compatibility_bitfield2format(pref_bitfield),
 			format) != AST_FORMAT_CMP_EQUAL) {
 			continue;
 		}

Modified: team/rmudgett/iax_fracks/channels/iax2/format_compatibility.c
URL: http://svnview.digium.com/svn/asterisk/team/rmudgett/iax_fracks/channels/iax2/format_compatibility.c?view=diff&rev=419879&r1=419878&r2=419879
==============================================================================
--- team/rmudgett/iax_fracks/channels/iax2/format_compatibility.c (original)
+++ team/rmudgett/iax_fracks/channels/iax2/format_compatibility.c Thu Jul 31 17:41:36 2014
@@ -62,6 +62,7 @@
 	int bit;
 
 /* BUGBUG probably should add the codecs in the preference order using iax2_best_codec(). */
+/* BUGBUG probably should create a new function that takes the same parameters but expects bitfield to contain multiple formats. */
 	for (bit = 0; bit < 64; ++bit) {
 		uint64_t mask = (1ULL << bit);
 

Modified: team/rmudgett/iax_fracks/channels/iax2/include/codec_pref.h
URL: http://svnview.digium.com/svn/asterisk/team/rmudgett/iax_fracks/channels/iax2/include/codec_pref.h?view=diff&rev=419879&r1=419878&r2=419879
==============================================================================
--- team/rmudgett/iax_fracks/channels/iax2/include/codec_pref.h (original)
+++ team/rmudgett/iax_fracks/channels/iax2/include/codec_pref.h Thu Jul 31 17:41:36 2014
@@ -32,8 +32,8 @@
 
 #define IAX2_CODEC_PREF_SIZE 64
 struct iax2_codec_pref {
-	/*! This array is ordered by preference and contains the codec bitfield. */
-	uint64_t order[IAX2_CODEC_PREF_SIZE];
+	/*! Array is ordered by preference.  Contains the codec bitfield bit index + 1. */
+	char order[IAX2_CODEC_PREF_SIZE];
 	/*! Framing size of the codec */
 	unsigned int framing[IAX2_CODEC_PREF_SIZE];
 };
@@ -45,7 +45,7 @@
  *
  * \return the bitfield value of the order_value format
  */
-uint64_t iax2_codec_pref_order_value_to_format_bitfield(uint64_t order_value);
+uint64_t iax2_codec_pref_order_value_to_format_bitfield(int order_value);
 
 /*!
  * \brief Convert a format bitfield into an iax2_codec_pref order value
@@ -59,7 +59,7 @@
  *  It will work with multiformat bitfields, but it can only return the
  *  index of the most significant one if that is the case.
  */
-uint64_t iax2_codec_pref_format_bitfield_to_order_value(uint64_t bitfield);
+int iax2_codec_pref_format_bitfield_to_order_value(uint64_t bitfield);
 
 /*!
  * \brief Codec located at a particular place in the preference index.
@@ -94,7 +94,7 @@
 
 /*! \brief Append a audio codec to a preference list, removing it first if it was already there
 */
-int iax2_codec_pref_append(struct iax2_codec_pref *pref, struct ast_format *format, unsigned int framing);
+void iax2_codec_pref_append(struct iax2_codec_pref *pref, struct ast_format *format, unsigned int framing);
 
 /*! \brief Prepend an audio codec to a preference list, removing it first if it was already there
 */




More information about the asterisk-commits mailing list