From 84beaf27bc2605fa93d78ab2285aa945dbd08db0 Mon Sep 17 00:00:00 2001
From: Joshua Colp <jcolp@digium.com>
Date: Tue, 22 Jul 2014 14:36:26 +0000
Subject: [PATCH] chan_iax2: Restore previous behavior of iax2_best_codec.

The iax2_best_codec function was changed to convert the formats
into a format compatibilities structure and grab the first
format from it. The resulting order differs from the previous
order of iax2_best_codec which causes unexpected formats to
get chosen (such as g723).

This commit brings back the old behavior of iax2_best_codec by
having a specified preference list.

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


git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@419180 65c4cc65-6c06-0410-ace0-fbb531ad65f3
---
 channels/chan_iax2.c | 60 +++++++++++++++++++++++++++++++++++---------
 1 file changed, 48 insertions(+), 12 deletions(-)

diff --git a/channels/chan_iax2.c b/channels/chan_iax2.c
index d51a17b868..424f5add70 100644
--- a/channels/chan_iax2.c
+++ b/channels/chan_iax2.c
@@ -1869,21 +1869,57 @@ static iax2_format iax2_codec_choose(struct iax2_codec_pref *pref, iax2_format f
 
 static iax2_format iax2_best_codec(iax2_format formats)
 {
-	struct ast_format_cap *cap = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
-	struct ast_format *tmpfmt;
-	iax2_format format;
+	/* This just our opinion, expressed in code.  We are asked to choose
+	   the best codec to use, given no information */
+	static const iax2_format prefs[] =
+	{
+		/*! 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 x;
 
-	if (!cap) {
-		return 0;
+	/* Find the first preferred codec in the format given */
+	for (x = 0; x < ARRAY_LEN(prefs); x++) {
+		if (formats & prefs[x]) {
+			return prefs[x];
+		}
 	}
 
-	iax2_format_compatibility_bitfield2cap(formats, cap);
-	tmpfmt = ast_format_cap_get_format(cap, 0);
-	format = ast_format_compatibility_format2bitfield(tmpfmt);
-	ao2_ref(tmpfmt, -1);
-	ao2_ref(cap, -1);
-
-	return format;
+	return 0;
 }
 
 const char *iax2_getformatname(iax2_format format)
-- 
GitLab