[asterisk-commits] kharwell: branch kharwell/media_formats_translation_core r413116 - /team/khar...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Tue Apr 29 17:58:01 CDT 2014


Author: kharwell
Date: Tue Apr 29 17:57:54 2014
New Revision: 413116

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=413116
Log:
turn and face the strain

Modified:
    team/kharwell/media_formats_translation_core/main/codec.c
    team/kharwell/media_formats_translation_core/main/translate.c

Modified: team/kharwell/media_formats_translation_core/main/codec.c
URL: http://svnview.digium.com/svn/asterisk/team/kharwell/media_formats_translation_core/main/codec.c?view=diff&rev=413116&r1=413115&r2=413116
==============================================================================
--- team/kharwell/media_formats_translation_core/main/codec.c (original)
+++ team/kharwell/media_formats_translation_core/main/codec.c Tue Apr 29 17:57:54 2014
@@ -404,31 +404,3 @@
 	ao2_unlock(codecs);
 	return res;
 }
-
-static int codec_longest_size(void *obj, void *arg, int flags)
-{
-	const struct ast_codec *codec = obj;
-	int *size = arg;
-	int len = strlen(codec->name);
-
-	if (len > *size) {
-		*size = len;
-	}
-	return 0;
-}
-
-int ast_codec_get_longest_size(void)
-{
-	int size = 0;
-	struct ast_codec *codec = ao2_callback(
-		codecs, 0, codec_longest_size, &size);
-
-	if (!codec) {
-		return 0;
-	}
-
-	size = strlen(codec->name);
-	ao2_ref(codec, -1);
-	return size;
-}
-

Modified: team/kharwell/media_formats_translation_core/main/translate.c
URL: http://svnview.digium.com/svn/asterisk/team/kharwell/media_formats_translation_core/main/translate.c?view=diff&rev=413116&r1=413115&r2=413116
==============================================================================
--- team/kharwell/media_formats_translation_core/main/translate.c (original)
+++ team/kharwell/media_formats_translation_core/main/translate.c Tue Apr 29 17:57:54 2014
@@ -80,7 +80,7 @@
  *
  * \note this table is protected by the table_lock.
  */
-static struct ast_format **__indextable;
+static unsigned int *__indextable;
 
 /*! protects the __indextable for resizing */
 static ast_rwlock_t tablelock;
@@ -99,15 +99,15 @@
 
 /*!
  * \internal
- * \brief converts format to index value.
- */
-static int format2index(struct ast_format *format)
+ * \brief converts codec to index value.
+ */
+static int codec2index(struct ast_codec *codec)
 {
 	int x;
 
 	ast_rwlock_rdlock(&tablelock);
 	for (x = 0; x < cur_max_index; x++) {
-		if (__indextable[x] == format) {
+		if (__indextable[x] == codec->id) {
 			/* format already exists in index2format table */
 			ast_rwlock_unlock(&tablelock);
 			return x;
@@ -119,16 +119,25 @@
 
 /*!
  * \internal
- * \brief add a new format to the matrix and index table structures.
- *
- * \note it is perfectly safe to call this on formats already indexed.
+ * \brief converts format to codec index value.
+ */
+static int format2index(struct ast_format *format)
+{
+	return codec2index(ast_format_get_codec_id(format));
+}
+
+/*!
+ * \internal
+ * \brief add a new codec to the matrix and index table structures.
+ *
+ * \note it is perfectly safe to call this on codecs already indexed.
  *
  * \retval 0, success
  * \retval -1, matrix and index table need to be resized
  */
-static int add_format2index(struct ast_format *format)
-{
-	if (format2index(format) != -1) {
+static int add_codec2index(struct ast_codec *codec)
+{
+	if (codec2index(codec) != -1) {
 		/* format is already already indexed */
 		return 0;
 	}
@@ -138,7 +147,7 @@
 		ast_rwlock_unlock(&tablelock);
 		return -1; /* hit max length */
 	}
-	__indextable[cur_max_index] = format;
+	__indextable[cur_max_index] = codec;
 	cur_max_index++;
 	ast_rwlock_unlock(&tablelock);
 
@@ -147,20 +156,20 @@
 
 /*!
  * \internal
- * \brief converts index value back to format
- */
-static struct ast_format *format index2format(int index)
-{
-	struct ast_format *format;
+ * \brief converts index value back to codec
+ */
+static struct ast_codec *index2codec(int index)
+{
+	struct ast_codec *codec;
 
 	if (index >= cur_max_index) {
 		return 0;
 	}
 	ast_rwlock_rdlock(&tablelock);
-	format = __indextable[index];
+	codec = ast_codec_get_by_id(__indextable[index]);
 	ast_rwlock_unlock(&tablelock);
 
-	return format;
+	return codec;
 }
 
 /*!
@@ -196,13 +205,13 @@
 	}
 
 	for (x = 0; x < index_size; x++) {
-		if (!(tmp_matrix[x] = ast_calloc(1, sizeof(struct translator_path*) * (index_size)))) {
+		if (!(tmp_matrix[x] = ast_calloc(1, sizeof(struct translator_path) * (index_size)))) {
 			goto resize_cleanup;
 		}
 	}
 
 	/* make new index table */
-	if (!(tmp_table = ast_calloc(1, sizeof(struct ast_format*) * index_size))) {
+	if (!(tmp_table = ast_calloc(1, sizeof(unsigned int) * index_size))) {
 		goto resize_cleanup;
 	}
 
@@ -213,7 +222,7 @@
 		}
 		ast_free(__matrix);
 
-		memcpy(tmp_table, __indextable, sizeof(struct ast_format*) * old_index);
+		memcpy(tmp_table, __indextable, sizeof(unsigned int) * old_index);
 		ast_free(__indextable);
 	}
 
@@ -768,15 +777,20 @@
 					/* if no step already exists between x and z OR the new cost of using the intermediate
 					 * step is cheaper, use this step. */
 					if (!matrix_get(x, z)->step || (newtablecost < matrix_get(x, z)->table_cost)) {
+						struct ast_codec *x_codec = index2codec(x);
+						struct ast_codec *y_codec = index2codec(y);
+						struct ast_codec *z_codec = index2codec(z);
+
 						matrix_get(x, z)->step = matrix_get(x, y)->step;
 						matrix_get(x, z)->table_cost = newtablecost;
 						matrix_get(x, z)->multistep = 1;
 						changed++;
 						ast_debug(10, "Discovered %d cost path from %s to %s, via %s\n",
-							matrix_get(x, z)->table_cost,
-							  ast_format_get_name(index2format(x)),
-							  ast_format_get_name(index2format(z)),
-							  ast_format_get_name(index2format(y));
+							  matrix_get(x, z)->table_cost, x_codec->name,
+							  y_codec->name, z_codec->name);
+						ao2_ref(x_codec, -1);
+						ao2_ref(y_codec, -1);
+						ao2_ref(z_codec, -1);
 					}
 				}
 			}
@@ -797,7 +811,7 @@
 
 	ast_codec_get_names(p->t->src_codec, str);
 	while (p) {
-		ast_str_append(str, 0, "->%s");
+		ast_str_append(str, 0, "->");
 		ast_codec_get_names(p->t->dst_codec, str);
 		p = p->next;
 	}
@@ -831,53 +845,52 @@
 
 static char *handle_show_translation_table(struct ast_cli_args *a)
 {
-	int x;
-	int y;
-	int i;
-	int k;
-	int curlen = 0;
-	/* Get the length of the longest (usable?) codec name, so we know how wide the left side should be */
-	int longest = ast_codec_get_longest_size();
-	int f_len;
-	size_t f_size = 0;
-	const struct ast_format_list *f_list = ast_format_list_get(&f_size);
+	int x, y, i, k;
+	int longest, num_codecs, curlen = 0;
 	struct ast_str *out = ast_str_create(1024);
-
-	f_len = f_size;
+	struct ast_codec *codec;
+
+	/* Get the length of the longest (usable?) codec name,
+	   so we know how wide the left side should be */
+	for (i = 1; (codec = ast_codec_get_by_id(i)); ao2_ref(codec, -1)) {
+		++num_codecs;
+		if (codec->type != AST_MEDIA_TYPE_AUDIO) {
+			continue;
+		}
+		if (curlen > longest) {
+			longest = curlen;
+		}
+	}
+
 	AST_RWLIST_RDLOCK(&translators);
 	ast_cli(a->fd, "         Translation times between formats (in microseconds) for one second of data\n");
 	ast_cli(a->fd, "          Source Format (Rows) Destination Format (Columns)\n\n");
 
-	for (i = -1; i < f_len; i++) {
+	for (i = 0; i < num_codecs; i++) {
+		struct ast_codec *row = i ? ast_codec_get_by_id(i) : NULL
+
 		x = -1;
-		if ((i >= 0) && ((x = format2index(f_list[i].format.id)) == -1)) {
+		if ((i > 0) && (row->type != AST_MEDIA_TYPE_AUDIO) &&
+		    (x = codec2index(row)) == -1) {
+			ao2_ref(row, -1);
 			continue;
 		}
-		/* translation only applies to audio right now. */
-		if (i >= 0 && (AST_FORMAT_GET_TYPE(f_list[i].format.id) != AST_FORMAT_TYPE_AUDIO)) {
-			continue;
-		}
-		/*Go ahead and move to next iteration if dealing with an unknown codec*/
-		if (i >= 0 && !strcmp(ast_getformatname(&f_list[i].format), "unknown")) {
-			continue;
-		}
+
 		ast_str_set(&out, 0, " ");
-		for (k = -1; k < f_len; k++) {
+		for (k = 0; k < num_codecs; k++) {
+			struct ast_codec *col = k ? ast_codec_get_by_id(k) : NULL;
+
 			y = -1;
-			if ((k >= 0) && ((y = format2index(f_list[k].format.id)) == -1)) {
+			if ((k > 0) && (col->type != AST_MEDIA_TYPE_AUDIO) &&
+			    (y = codec2index(col)) == -1) {
+				ao2_ref(col, -1);
 				continue;
 			}
-			/* translation only applies to audio right now. */
-			if (k >= 0 && (AST_FORMAT_GET_TYPE(f_list[k].format.id) != AST_FORMAT_TYPE_AUDIO)) {
-				continue;
-			}
-			/*Go ahead and move to next iteration if dealing with an unknown codec*/
-			if (k >= 0 && !strcmp(ast_getformatname(&f_list[k].format), "unknown")) {
-				continue;
-			}
-			if (k >= 0) {
-				curlen = strlen(ast_getformatname(&f_list[k].format));
-			}
+
+			if (k > 0) {
+				curlen = strlen(col->name);
+			}
+
 			if (curlen < 5) {
 				curlen = 5;
 			}
@@ -885,12 +898,12 @@
 			if (x >= 0 && y >= 0 && matrix_get(x, y)->step) {
 				/* Actual codec output */
 				ast_str_append(&out, 0, "%*d", curlen + 1, (matrix_get(x, y)->table_cost/100));
-			} else if (i == -1 && k >= 0) {
+			} else if (i == 0 && k > 0) {
 				/* Top row - use a dynamic size */
-				ast_str_append(&out, 0, "%*s", curlen + 1, ast_getformatname(&f_list[k].format));
-			} else if (k == -1 && i >= 0) {
+				ast_str_append(&out, 0, "%*s", curlen + 1, col->name);
+			} else if (k == 0 && i > 0) {
 				/* Left column - use a static size. */
-				ast_str_append(&out, 0, "%*s", longest, ast_getformatname(&f_list[i].format));
+				ast_str_append(&out, 0, "%*s", longest, row->name);
 			} else if (x >= 0 && y >= 0) {
 				/* Codec not supported */
 				ast_str_append(&out, 0, "%*s", curlen + 1, "-");
@@ -898,9 +911,11 @@
 				/* Upper left hand corner */
 				ast_str_append(&out, 0, "%*s", longest, "");
 			}
+			ao2_cleanup(col);
 		}
 		ast_str_append(&out, 0, "\n");
 		ast_cli(a->fd, "%s", ast_str_buffer(out));
+		ao2_cleanup(row);
 	}
 	ast_free(out);
 	AST_RWLIST_UNLOCK(&translators);
@@ -910,50 +925,47 @@
 
 static char *handle_show_translation_path(struct ast_cli_args *a)
 {
-	struct ast_format input_src_format;
-	size_t len = 0;
-	int i;
-	const struct ast_format_list *format_list = ast_format_list_get(&len);
+	int i = 1;
 	struct ast_str *str = ast_str_alloca(1024);
 	struct ast_translator *step;
-	char tmp[256];
-
-	ast_format_clear(&input_src_format);
-	for (i = 0; i < len; i++) {
-		if (AST_FORMAT_GET_TYPE(format_list[i].format.id) != AST_FORMAT_TYPE_AUDIO) {
+	struct ast_codec *dst_codec, *src_codec = ast_codec_get(
+		a->argv[4], AST_FORMAT_TYPE_AUDIO, 0);
+
+	if (!src_codec) {
+		ast_cli(a->fd, "Source codec \"%s\" is not found.\n", a->argv[4]);
+		return CLI_FAILURE;
+	}
+
+	AST_RWLIST_RDLOCK(&translators);
+	ast_cli(a->fd, "--- Translation paths SRC Codec \"%s\" sample rate %d ---\n",
+		a->argv[4], src_codec->sample_rate);
+
+	while (dst_codec = ast_codec_get_by_id(i)) {
+		int src, dst;
+
+		++i;
+		if (src_codec == dst_codec ||
+		    dst_codec->type != AST_MEDIA_TYPE_AUDIO) {
+			ao2_ref(dst_codec, -1);
 			continue;
 		}
-		if (!strncasecmp(format_list[i].name, a->argv[4], strlen(format_list[i].name))) {
-			ast_format_copy(&input_src_format, &format_list[i].format);
-		}
-	}
-
-	if (!input_src_format.id) {
-		ast_cli(a->fd, "Source codec \"%s\" is not found.\n", a->argv[4]);
-		ast_format_list_destroy(format_list);
-		return CLI_FAILURE;
-	}
-
-	AST_RWLIST_RDLOCK(&translators);
-	ast_cli(a->fd, "--- Translation paths SRC Codec \"%s\" sample rate %d ---\n", a->argv[4], ast_format_rate(&input_src_format));
-	for (i = 0; i < len; i++) {
-		int src;
-		int dst;
-		if ((AST_FORMAT_GET_TYPE(format_list[i].format.id) != AST_FORMAT_TYPE_AUDIO) || (format_list[i].format.id == input_src_format.id)) {
-			continue;
-		}
-		dst = format2index(format_list[i].format.id);
-		src = format2index(input_src_format.id);
+
+		dst = codec2index(dst_codec);
+		src = codec2index(src_codec);
+
+		step = matrix_get(src, dst)->step;
 		ast_str_reset(str);
-		if ((len >= cur_max_index) && (src != -1) && (dst != -1) && matrix_get(src, dst)->step) {
-			ast_str_append(&str, 0, "%s", ast_getformatname_multiple_byid(tmp, sizeof(tmp), matrix_get(src, dst)->step->src_format.id));
+
+		if ((len >= cur_max_index) && (src != -1) && (dst != -1) && step) {
+			ast_codec_get_names(step->src_codec, &str);
 			while (src != dst) {
 				step = matrix_get(src, dst)->step;
 				if (!step) {
 					ast_str_reset(str);
 					break;
 				}
-				ast_str_append(&str, 0, "->%s", ast_getformatname_multiple_byid(tmp, sizeof(tmp), step->dst_format.id));
+				ast_str_append(&str, 0, "->");
+				ast_codec_get_names(step->src_codec, &str);
 				src = step->dst_fmt_index;
 			}
 		}
@@ -961,11 +973,12 @@
 		if (ast_strlen_zero(ast_str_buffer(str))) {
 			ast_str_set(&str, 0, "No Translation Path");
 		}
-		ast_cli(a->fd, "\t%-10.10s To %-10.10s: %-60.60s\n", a->argv[4], format_list[i].name, ast_str_buffer(str));
+		ast_cli(a->fd, "\t%-10.10s To %-10.10s: %-60.60s\n",
+			a->argv[4], format_list[i].name, ast_str_buffer(str));
+		ao2_ref(dst_codec, -1);
 	}
 	AST_RWLIST_UNLOCK(&translators);
-
-	ast_format_list_destroy(format_list);
+	ao2_ref(src_codec, -1);
 	return CLI_SUCCESS;
 }
 
@@ -1020,13 +1033,13 @@
 	struct ast_translator *u;
 	char tmp[80];
 
-	if (add_format2index(t->src_format.id) || add_format2index(t->dst_format.id)) {
+	if (add_codec2index(t->src_codec) || add_codec2index(t->dst_codec)) {
 		if (matrix_resize(0)) {
 			ast_log(LOG_WARNING, "Translator matrix can not represent any more translators.  Out of resources.\n");
 			return -1;
 		}
-		add_format2index(t->src_format.id);
-		add_format2index(t->dst_format.id);
+		add_codec2index(t->src_codec);
+		add_codec2index(t->dst_codec);
 	}
 
 	if (!mod) {
@@ -1038,15 +1051,15 @@
 		ast_log(LOG_WARNING, "empty buf size, you need to supply one\n");
 		return -1;
 	}
-	if (!t->table_cost && !(t->table_cost = generate_table_cost(&t->src_format, &t->dst_format))) {
+	if (!t->table_cost && !(t->table_cost = generate_table_cost(&t->src_codec, &t->dst_codec))) {
 		ast_log(LOG_WARNING, "Table cost could not be generated for %s, "
 			"Please set table_cost variable on translator.\n", t->name);
 		return -1;
 	}
 
 	t->module = mod;
-	t->src_fmt_index = format2index(t->src_format.id);
-	t->dst_fmt_index = format2index(t->dst_format.id);
+	t->src_fmt_index = codec2index(t->src_codec);
+	t->dst_fmt_index = codec2index(t->dst_codec);
 	t->active = 1;
 
 	if (t->src_fmt_index == -1 || t->dst_fmt_index == -1) {
@@ -1054,12 +1067,12 @@
 		return -1;
 	}
 	if (t->src_fmt_index >= cur_max_index) {
-		ast_log(LOG_WARNING, "Source format %s is larger than cur_max_index\n", ast_getformatname(&t->src_format));
+		ast_log(LOG_WARNING, "Source codec %s is larger than cur_max_index\n", &t->src_codec->name);
 		return -1;
 	}
 
 	if (t->dst_fmt_index >= cur_max_index) {
-		ast_log(LOG_WARNING, "Destination format %s is larger than cur_max_index\n", ast_getformatname(&t->dst_format));
+		ast_log(LOG_WARNING, "Destination codec %s is larger than cur_max_index\n", &t->dst_codec->name);
 		return -1;
 	}
 
@@ -1080,9 +1093,9 @@
 
 	generate_computational_cost(t, 1);
 
-	ast_verb(2, "Registered translator '%s' from format %s to %s, table cost, %d, computational cost %d\n",
-			    term_color(tmp, t->name, COLOR_MAGENTA, COLOR_BLACK, sizeof(tmp)),
-			    ast_getformatname(&t->src_format), ast_getformatname(&t->dst_format), t->table_cost, t->comp_cost);
+	ast_verb(2, "Registered translator '%s' from codec %s to %s, table cost, %d, computational cost %d\n",
+		 term_color(tmp, t->name, COLOR_MAGENTA, COLOR_BLACK, sizeof(tmp)),
+		 &t->src_codec->name, &t->dst_codec->name, t->table_cost, t->comp_cost);
 
 	AST_RWLIST_WRLOCK(&translators);
 
@@ -1099,7 +1112,7 @@
 	}
 	AST_RWLIST_TRAVERSE_SAFE_END;
 
-	/* if no existing translator was found for this format combination,
+	/* if no existing translator was found for this codec combination,
 	   add it to the beginning of the list */
 	if (t) {
 		AST_RWLIST_INSERT_HEAD(&translators, t, list);
@@ -1123,10 +1136,9 @@
 	AST_RWLIST_TRAVERSE_SAFE_BEGIN(&translators, u, list) {
 		if (u == t) {
 			AST_RWLIST_REMOVE_CURRENT(list);
-			ast_verb(2, "Unregistered translator '%s' from format %s to %s\n",
+			ast_verb(2, "Unregistered translator '%s' from codec %s to %s\n",
 				term_color(tmp, t->name, COLOR_MAGENTA, COLOR_BLACK, sizeof(tmp)),
-				ast_getformatname(&t->src_format),
-				ast_getformatname(&t->dst_format));
+				&t->src_codec->name), &t->dst_codec->name);
 			found = 1;
 			break;
 		}
@@ -1166,72 +1178,98 @@
 {
 	unsigned int besttablecost = INT_MAX;
 	unsigned int beststeps = INT_MAX;
-	struct ast_format best;
-	struct ast_format bestdst;
-	struct ast_format_cap *joint_cap = ast_format_cap_joint(dst_cap, src_cap);
-	ast_format_clear(&best);
-	ast_format_clear(&bestdst);
-
-	if (joint_cap) { /* yes, pick one and return */
-		struct ast_format tmp_fmt;
-		ast_format_cap_iter_start(joint_cap);
-		while (!ast_format_cap_iter_next(joint_cap, &tmp_fmt)) {
-			/* We are guaranteed to find one common format. */
-			if (!best.id) {
-				ast_format_copy(&best, &tmp_fmt);
+	struct ast_format *best = NULL;
+	struct ast_format *bestdst, *cur_dst, *cur_src;
+	struct ast_format_cap *joint_cap;
+	int i, j;
+
+	if (!(joint_cap = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT))) {
+		return -1;
+	}
+	ast_format_cap_get_compatible(dst_cap, src_cap, joint_cap);
+
+	for (i = 0; i < ast_format_cap_count(joint_cap); ++i) {
+		struct ast_format *fmt =
+			ast_format_cap_get_format(joint_cap, i);
+
+		if (!fmt) {
+			continue;
+		}
+
+		if (!best) {
+			best = ast_format_copy(fmt);
+			ao2_ref(fmt, -1);
+			continue;
+		}
+
+		if (ast_format_get_sample_rate(best) <
+		    ast_format_get_sampe_rate(fmt)) {
+			ao2_ref(best, -1);
+			best = ast_format_copy(fmt);
+		}
+		ao2_ref(fmt, -1);
+	}
+
+	if (best) {
+		dst_fmt_out = ast_format_copy(best);
+		src_fmt_out = ast_format_copy(best);
+		ao2_ref(best, -1);
+		ao2_ref(joint_cap, -1);
+		return 0;
+	}
+	/* need to translate */
+	AST_RWLIST_RDLOCK(&translators);
+
+	for (i = 0; i < ast_format_cap_count(dst_cap); ++i) {
+		struct ast_format *dst =
+			ast_format_cap_get_format(dst_cap, i);
+
+		if (!dst) {
+			continue;
+		}
+
+		for (j = 0; j < ast_format_cap_count(src_cap); ++j) {
+			struct ast_format *src =
+				ast_format_cap_get_format(src_cap, j);
+			int x, y;
+
+			if (!src) {
 				continue;
 			}
-			/* If there are multiple common formats, pick the one with the highest sample rate */
-			if (ast_format_rate(&best) < ast_format_rate(&tmp_fmt)) {
-				ast_format_copy(&best, &tmp_fmt);
+
+			x = format2index(src);
+			y = format2index(dst);
+			if (x < 0 || y < 0) {
+				ao2_ref(src, -1);
 				continue;
 			}
-
-		}
-		ast_format_cap_iter_end(joint_cap);
-
-		/* We are done, this is a common format to both. */
-		ast_format_copy(dst_fmt_out, &best);
-		ast_format_copy(src_fmt_out, &best);
-		ast_format_cap_destroy(joint_cap);
-		return 0;
-	} else {      /* No, we will need to translate */
-		struct ast_format cur_dst;
-		struct ast_format cur_src;
-		AST_RWLIST_RDLOCK(&translators);
-
-		ast_format_cap_iter_start(dst_cap);
-		while (!ast_format_cap_iter_next(dst_cap, &cur_dst)) {
-			ast_format_cap_iter_start(src_cap);
-			while (!ast_format_cap_iter_next(src_cap, &cur_src)) {
-				int x = format2index(cur_src.id);
-				int y = format2index(cur_dst.id);
-				if (x < 0 || y < 0) {
-					continue;
-				}
-				if (!matrix_get(x, y) || !(matrix_get(x, y)->step)) {
-					continue;
-				}
-				if (((matrix_get(x, y)->table_cost < besttablecost) || (matrix_get(x, y)->multistep < beststeps))) {
-					/* better than what we have so far */
-					ast_format_copy(&best, &cur_src);
-					ast_format_copy(&bestdst, &cur_dst);
-					besttablecost = matrix_get(x, y)->table_cost;
-					beststeps = matrix_get(x, y)->multistep;
-				}
-			}
-			ast_format_cap_iter_end(src_cap);
-		}
-
-		ast_format_cap_iter_end(dst_cap);
-		AST_RWLIST_UNLOCK(&translators);
-		if (best.id) {
-			ast_format_copy(dst_fmt_out, &bestdst);
-			ast_format_copy(src_fmt_out, &best);
-			return 0;
-		}
+			if (!matrix_get(x, y) || !(matrix_get(x, y)->step)) {
+				ao2_ref(src, -1);
+				continue;
+			}
+			if (((matrix_get(x, y)->table_cost < besttablecost) ||
+			     (matrix_get(x, y)->multistep < beststeps))) {
+				/* better than what we have so far */
+				ao2_cleanup(best);
+				ao2_cleanup(bestdst);
+				best = ast_format_copy(src);
+				bestdst = ast_format_copy(dst);
+				besttablecost = matrix_get(x, y)->table_cost;
+				beststeps = matrix_get(x, y)->multistep;
+			}
+			ao2_ref(src, -1);
+		}
+		ao2_ref(dst, -1);
+	}
+	AST_RWLIST_UNLOCK(&translators);
+	if (!best) {
 		return -1;
 	}
+	dst_fmt_out = ast_format_copy(best);
+	src_fmt_out = ast_format_copy(best);
+	ao2_ref(best, -1);
+	ao2_ref(bestdst, -1);
+	return 0;
 }
 
 unsigned int ast_translate_path_steps(struct ast_format *dst_format, struct ast_format *src_format)
@@ -1239,8 +1277,8 @@
 	unsigned int res = -1;
 	int src, dest;
 	/* convert bitwise format numbers into array indices */
-	src = format2index(src_format->id);
-	dest = format2index(dst_format->id);
+	int src = format2index(src_format);
+	int dest = format2index(dst_format);
 
 	if (src == -1 || dest == -1) {
 		ast_log(LOG_WARNING, "No translator path: (%s codec is not valid)\n", src == -1 ? "starting" : "ending");
@@ -1265,17 +1303,21 @@
 	int src_video = 0;
 	int index;
 
-	ast_format_cap_iter_start(dest);
-	while (!ast_format_cap_iter_next(dest, &cur_dest)) {
+	for (index = 0; index < ast_format_cap_count(dest); ++index) {
+		if ((!cur_dest = ast_format_cap_get_format(joint_cap, index))) {
+			continue;
+		}
+
 		/* We give preference to a joint format structure if possible */
-		if (ast_format_cap_get_compatible_format(src, &cur_dest, &tmp_fmt)) {
-			ast_format_cap_add(result, &tmp_fmt);
+		if ((cur_src = ast_format_cap_get_compatible_format)) {
+			ast_format_cap_add(result, cur_src);
+			ao2_ref(cur_src, -1);
 		} else {
 			/* Otherwise we just use the destination format */
-			ast_format_cap_add(result, &cur_dest);
-		}
-	}
-	ast_format_cap_iter_end(dest);
+			ast_format_cap_add(result, fmt)
+		}
+		ao2_ref(cur_dest, -1);
+	}
 
 	/* if we don't have a source format, we just have to try all
 	   possible destination formats */




More information about the asterisk-commits mailing list