[svn-commits] rmudgett: branch rmudgett/iax_fracks r419881 - in /team/rmudgett/iax_fracks/c...

SVN commits to the Digium repositories svn-commits at lists.digium.com
Thu Jul 31 19:22:07 CDT 2014


Author: rmudgett
Date: Thu Jul 31 19:22:03 2014
New Revision: 419881

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=419881
Log:
chan_iax2: Set channel native formats in the "best" order.

* Setup the requested initial incoming/outgoing channel native formats in
the "best" codec order since we don't have any other preferred order.

* Renamed iax2_best_codec() to iax2_format_compatibility_bitfield2best()
and moved it to iax2/formatt_compatibility.c.

Modified:
    team/rmudgett/iax_fracks/channels/chan_iax2.c
    team/rmudgett/iax_fracks/channels/iax2/format_compatibility.c
    team/rmudgett/iax_fracks/channels/iax2/include/format_compatibility.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=419881&r1=419880&r2=419881
==============================================================================
--- team/rmudgett/iax_fracks/channels/chan_iax2.c (original)
+++ team/rmudgett/iax_fracks/channels/chan_iax2.c Thu Jul 31 19:22:03 2014
@@ -1867,61 +1867,6 @@
 	return format;
 }
 
-static iax2_format iax2_best_codec(iax2_format formats)
-{
-	/* This just our opinion, expressed in code.  We are asked to choose
-	   the best codec to use, given no information */
-	static const iax2_format best[] =
-	{
-		/*! Okay, ulaw is used by all telephony equipment, so start with it */
-		AST_FORMAT_ULAW,
-		/*! Unless of course, you're a silly European, so then prefer ALAW */
-		AST_FORMAT_ALAW,
-		AST_FORMAT_G719,
-		AST_FORMAT_SIREN14,
-		AST_FORMAT_SIREN7,
-		AST_FORMAT_TESTLAW,
-		/*! G.722 is better then all below, but not as common as the above... so give ulaw and alaw priority */
-		AST_FORMAT_G722,
-		/*! Okay, well, signed linear is easy to translate into other stuff */
-		AST_FORMAT_SLIN16,
-		AST_FORMAT_SLIN,
-		/*! G.726 is standard ADPCM, in RFC3551 packing order */
-		AST_FORMAT_G726,
-		/*! G.726 is standard ADPCM, in AAL2 packing order */
-		AST_FORMAT_G726_AAL2,
-		/*! ADPCM has great sound quality and is still pretty easy to translate */
-		AST_FORMAT_ADPCM,
-		/*! Okay, we're down to vocoders now, so pick GSM because it's small and easier to
-		    translate and sounds pretty good */
-		AST_FORMAT_GSM,
-		/*! iLBC is not too bad */
-		AST_FORMAT_ILBC,
-		/*! Speex is free, but computationally more expensive than GSM */
-		AST_FORMAT_SPEEX16,
-		AST_FORMAT_SPEEX,
-		/*! Opus */
-		AST_FORMAT_OPUS,
-		/*! Ick, LPC10 sounds terrible, but at least we have code for it, if you're tacky enough
-		    to use it */
-		AST_FORMAT_LPC10,
-		/*! G.729a is faster than 723 and slightly less expensive */
-		AST_FORMAT_G729,
-		/*! Down to G.723.1 which is proprietary but at least designed for voice */
-		AST_FORMAT_G723,
-	};
-	int idx;
-
-	/* Find the first preferred codec in the format given */
-	for (idx = 0; idx < ARRAY_LEN(best); ++idx) {
-		if (formats & best[idx]) {
-			return best[idx];
-		}
-	}
-
-	return 0;
-}
-
 const char *iax2_getformatname(iax2_format format)
 {
 	struct ast_format *tmpfmt;
@@ -5906,7 +5851,7 @@
 	if (!native) {
 		return NULL;
 	}
-	if (iax2_format_compatibility_bitfield2cap(capability, native)
+	if (iax2_format_compatibility_bitfield2cap_best(capability, native)
 		|| !ast_format_cap_count(native)) {
 		ast_log(LOG_WARNING, "No requested formats available for call to: IAX2/%s-%d\n",
 			i->host, i->callno);
@@ -10914,7 +10859,7 @@
 									if(ast_test_flag64(iaxs[fr->callno], IAX_CODEC_NOPREFS)) {
 										using_prefs = ast_test_flag64(iaxs[fr->callno], IAX_CODEC_NOCAP) ? "reqonly" : "disabled";
 										memset(&pref, 0, sizeof(pref));
-										format = iax2_best_codec(iaxs[fr->callno]->peercapability & iaxs[fr->callno]->capability);
+										format = iax2_format_compatibility_bitfield2best(iaxs[fr->callno]->peercapability & iaxs[fr->callno]->capability);
 										strcpy(caller_pref_buf,"disabled");
 										strcpy(host_pref_buf,"disabled");
 									} else {
@@ -10930,7 +10875,7 @@
 											}
 											format = iax2_codec_choose(&pref, iaxs[fr->callno]->peercapability & iaxs[fr->callno]->capability);
 										} else /* if no codec_prefs IE do it the old way */
-											format = iax2_best_codec(iaxs[fr->callno]->peercapability & iaxs[fr->callno]->capability);
+											format = iax2_format_compatibility_bitfield2best(iaxs[fr->callno]->peercapability & iaxs[fr->callno]->capability);
 									}
 								}
 
@@ -11132,7 +11077,7 @@
 						struct ast_str *cap_buf = ast_str_alloca(64);
 
 						/* Switch us to use a compatible format */
-						iax2_format_compatibility_bitfield2cap(iaxs[fr->callno]->peerformat, native);
+						iax2_format_compatibility_bitfield2cap_best(iaxs[fr->callno]->peerformat, native);
 						ast_channel_nativeformats_set(iaxs[fr->callno]->owner, native);
 						ast_verb(3, "Format for call is %s\n", ast_format_cap_get_names(ast_channel_nativeformats(iaxs[fr->callno]->owner), &cap_buf));
 
@@ -11372,8 +11317,9 @@
 								if(ast_test_flag64(iaxs[fr->callno], IAX_CODEC_NOPREFS)) {
 									using_prefs = ast_test_flag64(iaxs[fr->callno], IAX_CODEC_NOCAP) ? "reqonly" : "disabled";
 									memset(&pref, 0, sizeof(pref));
-									format = ast_test_flag64(iaxs[fr->callno], IAX_CODEC_NOCAP) ?
-										iaxs[fr->callno]->peerformat : iax2_best_codec(iaxs[fr->callno]->peercapability & iaxs[fr->callno]->capability);
+									format = ast_test_flag64(iaxs[fr->callno], IAX_CODEC_NOCAP)
+										? iaxs[fr->callno]->peerformat
+										: iax2_format_compatibility_bitfield2best(iaxs[fr->callno]->peercapability & iaxs[fr->callno]->capability);
 									strcpy(caller_pref_buf,"disabled");
 									strcpy(host_pref_buf,"disabled");
 								} else {
@@ -11389,7 +11335,7 @@
 										}
 										format = iax2_codec_choose(&pref, iaxs[fr->callno]->peercapability & iaxs[fr->callno]->capability);
 									} else /* if no codec_prefs IE do it the old way */
-										format = iax2_best_codec(iaxs[fr->callno]->peercapability & iaxs[fr->callno]->capability);
+										format = iax2_format_compatibility_bitfield2best(iaxs[fr->callno]->peercapability & iaxs[fr->callno]->capability);
 								}
 							}
 							if (!format) {

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=419881&r1=419880&r2=419881
==============================================================================
--- team/rmudgett/iax_fracks/channels/iax2/format_compatibility.c (original)
+++ team/rmudgett/iax_fracks/channels/iax2/format_compatibility.c Thu Jul 31 19:22:03 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/format_compatibility.h"
 
@@ -57,12 +58,108 @@
 	return bitfield;
 }
 
+uint64_t iax2_format_compatibility_bitfield2best(uint64_t formats)
+{
+	/*
+	 * This just our opinion, expressed in code.  We are
+	 * asked to choose the best codec to use, given no
+	 * information.
+	 */
+	static const uint64_t best[] =
+	{
+		/*! Okay, ulaw is used by all telephony equipment, so start with it */
+		AST_FORMAT_ULAW,
+		/*! Unless of course, you're a silly European, so then prefer ALAW */
+		AST_FORMAT_ALAW,
+		AST_FORMAT_G719,
+		AST_FORMAT_SIREN14,
+		AST_FORMAT_SIREN7,
+		AST_FORMAT_TESTLAW,
+		/*! G.722 is better then all below, but not as common as the above... so give ulaw and alaw priority */
+		AST_FORMAT_G722,
+		/*! Okay, well, signed linear is easy to translate into other stuff */
+		AST_FORMAT_SLIN16,
+		AST_FORMAT_SLIN,
+		/*! G.726 is standard ADPCM, in RFC3551 packing order */
+		AST_FORMAT_G726,
+		/*! G.726 is standard ADPCM, in AAL2 packing order */
+		AST_FORMAT_G726_AAL2,
+		/*! ADPCM has great sound quality and is still pretty easy to translate */
+		AST_FORMAT_ADPCM,
+		/*! Okay, we're down to vocoders now, so pick GSM because it's small and easier to
+		    translate and sounds pretty good */
+		AST_FORMAT_GSM,
+		/*! iLBC is not too bad */
+		AST_FORMAT_ILBC,
+		/*! Speex is free, but computationally more expensive than GSM */
+		AST_FORMAT_SPEEX16,
+		AST_FORMAT_SPEEX,
+		/*! Opus */
+		AST_FORMAT_OPUS,
+		/*! Ick, LPC10 sounds terrible, but at least we have code for it, if you're tacky enough
+		    to use it */
+		AST_FORMAT_LPC10,
+		/*! G.729a is faster than 723 and slightly less expensive */
+		AST_FORMAT_G729,
+		/*! Down to G.723.1 which is proprietary but at least designed for voice */
+		AST_FORMAT_G723,
+	};
+	int idx;
+
+	/* Find the first preferred codec in the format given */
+	for (idx = 0; idx < ARRAY_LEN(best); ++idx) {
+		if (formats & best[idx]) {
+			return best[idx];
+		}
+	}
+
+	return 0;
+}
+
+int iax2_format_compatibility_bitfield2cap_best(uint64_t bitfield, struct ast_format_cap *cap)
+{
+	int bit;
+	uint64_t best_bitfield;
+	struct ast_format *format;
+
+	/* Add the "best" codecs first. */
+	while (bitfield) {
+		best_bitfield = iax2_format_compatibility_bitfield2best(bitfield);
+		if (!best_bitfield) {
+			/* No more codecs considered best. */
+			break;
+		}
+
+		format = ast_format_compatibility_bitfield2format(best_bitfield);
+		if (ast_format_cap_append(cap, format, 0)) {
+			return -1;
+		}
+
+		/* Remove just added "best" codec to find the next "best". */
+		bitfield &= ~best_bitfield;
+	}
+
+	/* Add any remaining codecs. */
+	if (bitfield) {
+		for (bit = 0; bit < 64; ++bit) {
+			uint64_t mask = (1ULL << bit);
+
+			if (mask & bitfield) {
+				format = ast_format_compatibility_bitfield2format(mask);
+				if (ast_format_cap_append(cap, format, 0)) {
+					return -1;
+				}
+			}
+		}
+	}
+
+	return 0;
+}
+
 int iax2_format_compatibility_bitfield2cap(uint64_t bitfield, struct ast_format_cap *cap)
 {
 	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/format_compatibility.h
URL: http://svnview.digium.com/svn/asterisk/team/rmudgett/iax_fracks/channels/iax2/include/format_compatibility.h?view=diff&rev=419881&r1=419880&r2=419881
==============================================================================
--- team/rmudgett/iax_fracks/channels/iax2/include/format_compatibility.h (original)
+++ team/rmudgett/iax_fracks/channels/iax2/include/format_compatibility.h Thu Jul 31 19:22:03 2014
@@ -52,4 +52,27 @@
  */
 int iax2_format_compatibility_bitfield2cap(uint64_t bitfield, struct ast_format_cap *cap);
 
+/*!
+ * \brief Convert a bitfield to a format capabilities structure in the "best" order.
+ *
+ * \param bitfield The bitfield for the media formats
+ * \param cap Capabilities structure to place formats into
+ *
+ * \retval 0 on success.
+ * \retval -1 on error.
+ *
+ * \note If failure occurs the capabilities structure may contain a partial set of formats
+ */
+int iax2_format_compatibility_bitfield2cap_best(uint64_t bitfield, struct ast_format_cap *cap);
+
+/*!
+ * \brief Pick the best format from the given bitfield formats.
+ *
+ * \param formats The bitfield for the media formats
+ *
+ * \retval non-zero Best format out of the given formats.
+ * \retval zero No formats present or no formats considered best.
+ */
+uint64_t iax2_format_compatibility_bitfield2best(uint64_t formats);
+
 #endif /* _IAX2_FORMAT_COMPATIBILITY_H */




More information about the svn-commits mailing list