[asterisk-commits] kharwell: branch group/media_formats-reviewed r414875 - in /team/group/media_...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Thu May 29 14:37:52 CDT 2014


Author: kharwell
Date: Thu May 29 14:37:45 2014
New Revision: 414875

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=414875
Log:
media_formats: convert translation core

This moves translate.c over to the media formats API.

(closes issue SWP-6725)
Reported by: Matt Jordan
Review: https://reviewboard.asterisk.org/r/3512/

Modified:
    team/group/media_formats-reviewed/include/asterisk/format.h
    team/group/media_formats-reviewed/main/translate.c

Modified: team/group/media_formats-reviewed/include/asterisk/format.h
URL: http://svnview.digium.com/svn/asterisk/team/group/media_formats-reviewed/include/asterisk/format.h?view=diff&rev=414875&r1=414874&r2=414875
==============================================================================
--- team/group/media_formats-reviewed/include/asterisk/format.h (original)
+++ team/group/media_formats-reviewed/include/asterisk/format.h Thu May 29 14:37:45 2014
@@ -137,7 +137,7 @@
  */
 enum ast_format_cmp_res ast_format_cmp(const struct ast_format *format1, const struct ast_format *format2);
 
-/*1
+/*!
  * \brief Get a common joint capability between two formats
  *
  * \retval non-NULL if joint capability exists

Modified: team/group/media_formats-reviewed/main/translate.c
URL: http://svnview.digium.com/svn/asterisk/team/group/media_formats-reviewed/main/translate.c?view=diff&rev=414875&r1=414874&r2=414875
==============================================================================
--- team/group/media_formats-reviewed/main/translate.c (original)
+++ team/group/media_formats-reviewed/main/translate.c Thu May 29 14:37:45 2014
@@ -43,6 +43,7 @@
 #include "asterisk/sched.h"
 #include "asterisk/cli.h"
 #include "asterisk/term.h"
+#include "asterisk/format.h"
 
 /*! \todo
  * TODO: sample frames for each supported input format.
@@ -76,11 +77,11 @@
 static struct translator_path **__matrix;
 
 /*!
- * \brief table for converting index to format id values.
+ * \brief table for converting index to format values.
  *
  * \note this table is protected by the table_lock.
  */
-static int *__indextable;
+static unsigned int *__indextable;
 
 /*! protects the __indextable for resizing */
 static ast_rwlock_t tablelock;
@@ -99,9 +100,9 @@
 
 /*!
  * \internal
- * \brief converts format id to index value.
- */
-static int format2index(enum ast_format_id id)
+ * \brief converts codec id to index value.
+ */
+static int codec_to_index(unsigned int id)
 {
 	int x;
 
@@ -119,16 +120,34 @@
 
 /*!
  * \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 codec to index value.
+ */
+static int codec2index(struct ast_codec *codec)
+{
+	return codec_to_index(codec->id);
+}
+
+/*!
+ * \internal
+ * \brief converts format to codec index value.
+ */
+static int format2index(struct ast_format *format)
+{
+	return codec_to_index(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(enum ast_format_id id)
-{
-	if (format2index(id) != -1) {
+static int add_codec2index(struct ast_codec *codec)
+{
+	if (codec2index(codec) != -1) {
 		/* format is already already indexed */
 		return 0;
 	}
@@ -138,7 +157,7 @@
 		ast_rwlock_unlock(&tablelock);
 		return -1; /* hit max length */
 	}
-	__indextable[cur_max_index] = id;
+	__indextable[cur_max_index] = codec->id;
 	cur_max_index++;
 	ast_rwlock_unlock(&tablelock);
 
@@ -147,20 +166,20 @@
 
 /*!
  * \internal
- * \brief converts index value back to format id
- */
-static enum ast_format_id index2format(int index)
-{
-	enum ast_format_id format_id;
+ * \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_id = __indextable[index];
+	codec = ast_codec_get_by_id(__indextable[index]);
 	ast_rwlock_unlock(&tablelock);
 
-	return format_id;
+	return codec;
 }
 
 /*!
@@ -176,7 +195,7 @@
 static int matrix_resize(int init)
 {
 	struct translator_path **tmp_matrix = NULL;
-	int *tmp_table = NULL;
+	unsigned int *tmp_table = NULL;
 	int old_index;
 	int x;
 
@@ -202,7 +221,7 @@
 	}
 
 	/* make new index table */
-	if (!(tmp_table = ast_calloc(1, sizeof(int) * index_size))) {
+	if (!(tmp_table = ast_calloc(1, sizeof(unsigned int) * index_size))) {
 		goto resize_cleanup;
 	}
 
@@ -213,7 +232,7 @@
 		}
 		ast_free(__matrix);
 
-		memcpy(tmp_table, __indextable, sizeof(int) * old_index);
+		memcpy(tmp_table, __indextable, sizeof(unsigned int) * old_index);
 		ast_free(__indextable);
 	}
 
@@ -277,9 +296,10 @@
  * \brief Allocate the descriptor, required outbuf space,
  * and possibly desc.
  */
-static void *newpvt(struct ast_translator *t, const struct ast_format *explicit_dst)
+static void *newpvt(struct ast_translator *t, struct ast_format *explicit_dst)
 {
 	struct ast_trans_pvt *pvt;
+	struct ast_format *dst_format = NULL;
 	int len;
 	char *ofs;
 
@@ -306,13 +326,38 @@
 	/* if a explicit destination format is provided, set that on the pvt so the
 	 * translator will process it. */
 	if (explicit_dst) {
-		ast_format_copy(&pvt->explicit_dst, explicit_dst);
+		ao2_cleanup(pvt->explicit_dst);
+		pvt->explicit_dst = ast_format_copy(explicit_dst);
 	}
 	/* call local init routine, if present */
 	if (t->newpvt && t->newpvt(pvt)) {
 		ast_free(pvt);
 		return NULL;
 	}
+
+	if (!ast_strlen_zero(pvt->t->format)) {
+		dst_format = ast_format_cache_get(pvt->t->format);
+	}
+
+	if (!dst_format) {
+		struct ast_codec *codec = ast_codec_get(t->dst_codec.name,
+			t->dst_codec.type, t->dst_codec.sample_rate);
+		if (!codec) {
+			ast_log(LOG_ERROR, "Unable to get destination codec\n");
+			ast_free(pvt);
+			return NULL;
+		}
+		dst_format = ast_format_create(codec);
+		ao2_ref(codec, -1);
+	}
+
+	pvt->f.subclass.format = dst_format;
+	pvt->f.frametype = AST_FRAME_VOICE;
+	pvt->f.mallocd = 0;
+	pvt->f.offset = AST_FRIENDLY_OFFSET;
+	pvt->f.src = pvt->t->name;
+	pvt->f.data.ptr = pvt->outbuf.c;
+
 	ast_module_ref(t->module);
 	return pvt;
 }
@@ -324,6 +369,7 @@
 	if (t->destroy)
 		t->destroy(pvt);
 	ao2_cleanup(pvt->f.subclass.format);
+	ao2_cleanup(pvt->explicit_dst);
 	ast_free(pvt);
 	ast_module_unref(t->module);
 }
@@ -378,8 +424,9 @@
 	if (samples) {
 		f->samples = samples;
 	} else {
-		if (pvt->samples == 0)
+		if (pvt->samples == 0) {
 			return NULL;
+		}
 		f->samples = pvt->samples;
 		pvt->samples = 0;
 	}
@@ -390,13 +437,6 @@
 		pvt->datalen = 0;
 	}
 
-	f->frametype = AST_FRAME_VOICE;
-	ast_format_copy(&f->subclass.format, &pvt->t->dst_format);
-	f->mallocd = 0;
-	f->offset = AST_FRIENDLY_OFFSET;
-	f->src = pvt->t->name;
-	f->data.ptr = pvt->outbuf.c;
-
 	return ast_frisolate(f);
 }
 
@@ -421,11 +461,9 @@
 {
 	struct ast_trans_pvt *head = NULL, *tail = NULL;
 	int src_index, dst_index;
-	struct ast_format tmp_fmt1;
-	struct ast_format tmp_fmt2;
-
-	src_index = format2index(src->id);
-	dst_index = format2index(dst->id);
+
+	src_index = format2index(src);
+	dst_index = format2index(dst);
 
 	if (src_index == -1 || dst_index == -1) {
 		ast_log(LOG_WARNING, "No translator path: (%s codec is not valid)\n", src_index == -1 ? "starting" : "ending");
@@ -439,11 +477,8 @@
 		struct ast_format *explicit_dst = NULL;
 		struct ast_translator *t = matrix_get(src_index, dst_index)->step;
 		if (!t) {
-			int src_id = index2format(src_index);
-			int dst_id = index2format(dst_index);
 			ast_log(LOG_WARNING, "No translator path from %s to %s\n",
-				ast_getformatname(ast_format_set(&tmp_fmt1, src_id, 0)),
-				ast_getformatname(ast_format_set(&tmp_fmt2, dst_id, 0)));
+				ast_format_get_name(src), ast_format_get_name(dst));
 			AST_RWLIST_UNLOCK(&translators);
 			return NULL;
 		}
@@ -451,11 +486,8 @@
 			explicit_dst = dst;
 		}
 		if (!(cur = newpvt(t, explicit_dst))) {
-			int src_id = index2format(src_index);
-			int dst_id = index2format(dst_index);
 			ast_log(LOG_WARNING, "Failed to build translator step from %s to %s\n",
-				ast_getformatname(ast_format_set(&tmp_fmt1, src_id, 0)),
-				ast_getformatname(ast_format_set(&tmp_fmt2, dst_id, 0)));
+				ast_format_get_name(src), ast_format_get_name(dst));
 			if (head) {
 				ast_translator_free_path(head);
 			}
@@ -512,7 +544,8 @@
 			path->nextout = f->delivery;
 		}
 		/* Predict next incoming sample */
-		path->nextin = ast_tvadd(path->nextin, ast_samp2tv(f->samples, ast_format_rate(&f->subclass.format)));
+		path->nextin = ast_tvadd(path->nextin, ast_samp2tv(
+			 f->samples, ast_format_get_sample_rate(f->subclass.format)));
 	}
 	delivery = f->delivery;
 	for (out = f; out && p ; p = p->next) {
@@ -535,7 +568,8 @@
 
 			/* Predict next outgoing timestamp from samples in this
 			   frame. */
-			path->nextout = ast_tvadd(path->nextout, ast_samp2tv(out->samples, ast_format_rate(&out->subclass.format)));
+			path->nextout = ast_tvadd(path->nextout, ast_samp2tv(
+				 out->samples, ast_format_get_sample_rate(out->subclass.format)));
 			if (f->samples != out->samples && ast_test_flag(out, AST_FRFLAG_HAS_TIMING_INFO)) {
 				ast_debug(4, "Sample size different %u vs %u\n", f->samples, out->samples);
 				ast_clear_flag(out, AST_FRFLAG_HAS_TIMING_INFO);
@@ -576,7 +610,7 @@
 	struct rusage start;
 	struct rusage end;
 	int cost;
-	int out_rate = ast_format_rate(&t->dst_format);
+	int out_rate = t->dst_codec.sample_rate;
 
 	if (!seconds) {
 		seconds = 1;
@@ -647,21 +681,23 @@
  * \retval Table Cost value greater than 0.
  * \retval 0 on error.
  */
-static int generate_table_cost(struct ast_format *src, struct ast_format *dst)
-{
-	int src_rate = ast_format_rate(src);
+static int generate_table_cost(struct ast_codec *src, struct ast_codec *dst)
+{
+	int src_rate = src->sample_rate;
 	int src_ll = 0;
-	int dst_rate = ast_format_rate(dst);
+	int dst_rate = dst->sample_rate;
 	int dst_ll = 0;
 
-	if ((AST_FORMAT_GET_TYPE(src->id) != AST_FORMAT_TYPE_AUDIO) || (AST_FORMAT_GET_TYPE(dst->id) != AST_FORMAT_TYPE_AUDIO)) {
+	if ((src->type != AST_MEDIA_TYPE_AUDIO) ||
+	    (dst->type != AST_MEDIA_TYPE_AUDIO)) {
 		/* This method of generating table cost is limited to audio.
 		 * Translators for media other than audio must manually set their
 		 * table cost. */
 		return 0;
 	}
-	src_ll = ast_format_is_slinear(src);
-	dst_ll = ast_format_is_slinear(dst);
+
+	src_ll = !strcmp(src->name, "slin");
+	dst_ll = !strcmp(dst->name, "slin");
 	if (src_ll) {
 		if (dst_ll && (src_rate == dst_rate)) {
 			return AST_TRANS_COST_LL_LL_ORIGSAMP;
@@ -767,18 +803,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_format tmpx;
-						struct ast_format tmpy;
-						struct ast_format tmpz;
+						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_getformatname(ast_format_set(&tmpx, index2format(x), 0)),
-							ast_getformatname(ast_format_set(&tmpy, index2format(z), 0)),
-							ast_getformatname(ast_format_set(&tmpz, index2format(y), 0)));
+							  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);
 					}
 				}
 			}
@@ -789,20 +827,26 @@
 	}
 }
 
+static void codec_append_name(const struct ast_codec *codec, struct ast_str **buf)
+{
+	if (codec) {
+		ast_str_append(buf, 0, "(%s@%d)", codec->name, codec->sample_rate);
+	} else {
+		ast_str_append(buf, 0, "(nothing)");
+	}
+}
+
 const char *ast_translate_path_to_str(struct ast_trans_pvt *p, struct ast_str **str)
 {
-	struct ast_trans_pvt *pn = p;
-	char tmp[256];
-
 	if (!p || !p->t) {
 		return "";
 	}
 
-	ast_str_set(str, 0, "%s", ast_getformatname_multiple_byid(tmp, sizeof(tmp), p->t->src_format.id));
-
-	while ( (p = pn) ) {
-		pn = p->next;
-		ast_str_append(str, 0, "->%s", ast_getformatname_multiple_byid(tmp, sizeof(tmp), p->t->dst_format.id));
+	codec_append_name(&p->t->src_codec, str);
+	while (p) {
+		ast_str_append(str, 0, "->");
+		codec_append_name(&p->t->dst_codec, str);
+		p = p->next;
 	}
 
 	return ast_str_buffer(*str);
@@ -810,24 +854,24 @@
 
 static char *complete_trans_path_choice(const char *line, const char *word, int pos, int state)
 {
-	int which = 0;
+	int i = 1, which = 0;
 	int wordlen = strlen(word);
-	int i;
-	char *ret = NULL;
-	size_t len = 0;
-	const struct ast_format_list *format_list = ast_format_list_get(&len);
-
-	for (i = 0; i < len; i++) {
-		if (AST_FORMAT_GET_TYPE(format_list[i].format.id) != AST_FORMAT_TYPE_AUDIO) {
+	struct ast_codec *codec;
+
+	while ((codec = ast_codec_get_by_id(i))) {
+		++i;
+		if (codec->type != AST_MEDIA_TYPE_AUDIO) {
+			ao2_ref(codec, -1);
 			continue;
 		}
-		if (!strncasecmp(word, format_list[i].name, wordlen) && ++which > state) {
-			ret = ast_strdup(format_list[i].name);
-			break;
-		}
-	}
-	ast_format_list_destroy(format_list);
-	return ret;
+		if (!strncasecmp(word, codec->name, wordlen) && ++which > state) {
+			char *res = ast_strdup(codec->name);
+			ao2_ref(codec, -1);
+			return res;
+		}
+		ao2_ref(codec, -1);
+	}
+	return NULL;
 }
 
 static void handle_cli_recalc(struct ast_cli_args *a)
@@ -851,63 +895,53 @@
 
 static char *handle_show_translation_table(struct ast_cli_args *a)
 {
-	int x;
-	int y;
-	int i;
-	int k;
-	int curlen = 0;
-	int longest = 0;
-	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 = 0, num_codecs = 0, 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;
+		}
+		curlen = strlen(codec->name);
+		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");
 
-	/* Get the length of the longest (usable?) codec name, so we know how wide the left side should be */
-	for (i = 0; i < f_len; i++) {
-		/* translation only applies to audio right now. */
-		if (AST_FORMAT_GET_TYPE(f_list[i].format.id) != AST_FORMAT_TYPE_AUDIO)
+	for (i = 0; i < num_codecs; i++) {
+		struct ast_codec *row = i ? ast_codec_get_by_id(i) : NULL;
+
+		x = -1;
+		if ((i > 0) && (row->type != AST_MEDIA_TYPE_AUDIO) &&
+		    (x = codec2index(row)) == -1) {
+			ao2_ref(row, -1);
 			continue;
-		curlen = strlen(ast_getformatname(&f_list[i].format));
-		if (curlen > longest) {
-			longest = curlen;
-		}
-	}
-
-	for (i = -1; i < f_len; i++) {
-		x = -1;
-		if ((i >= 0) && ((x = format2index(f_list[i].format.id)) == -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;
+
+			if (k > 0) {
+				curlen = strlen(col->name);
 			}
-			/*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 (curlen < 5) {
 				curlen = 5;
 			}
@@ -915,12 +949,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, "-");
@@ -928,62 +962,60 @@
 				/* 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);
-	ast_format_list_destroy(f_list);
 	return CLI_SUCCESS;
 }
 
 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_MEDIA_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 (step) {
+			codec_append_name(&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, "->");
+				codec_append_name(&step->src_codec, &str);
 				src = step->dst_fmt_index;
 			}
 		}
@@ -991,11 +1023,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], dst_codec->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;
 }
 
@@ -1050,13 +1083,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) {
@@ -1068,15 +1101,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) {
@@ -1084,12 +1117,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;
 	}
 
@@ -1110,9 +1143,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);
 
@@ -1129,7 +1162,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);
@@ -1153,10 +1186,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;
 		}
@@ -1191,86 +1223,111 @@
 /*! \brief Calculate our best translator source format, given costs, and a desired destination */
 int ast_translator_best_choice(struct ast_format_cap *dst_cap,
 	struct ast_format_cap *src_cap,
-	struct ast_format *dst_fmt_out,
-	struct ast_format *src_fmt_out)
+	struct ast_format **dst_fmt_out,
+	struct ast_format **src_fmt_out)
 {
 	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 = NULL;
+	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_sample_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;
-				}
+			if (!matrix_get(x, y) || !(matrix_get(x, y)->step)) {
+				ao2_ref(src, -1);
+				continue;
 			}
-			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)->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(bestdst);
+	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)
 {
 	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");
@@ -1287,25 +1344,74 @@
 	return res;
 }
 
+static void check_translation_path(
+	struct ast_format_cap *dest, struct ast_format_cap *src,
+	struct ast_format_cap *result, struct ast_format *src_fmt,
+	enum ast_media_type type)
+{
+	int index, src_index = format2index(src_fmt);
+	/* For a given source format, traverse the list of
+	   known formats to determine whether there exists
+	   a translation path from the source format to the
+	   destination format. */
+	for (index = 0; (src_index >= 0) && index < cur_max_index; index++) {
+		struct ast_codec *codec = index2codec(index);
+		RAII_VAR(struct ast_format *, fmt,
+			 ast_format_create(codec), ao2_cleanup);
+		ao2_ref(codec, -1);
+
+		if (ast_format_get_type(fmt) != type) {
+			continue;
+		}
+
+		/* if this is not a desired format, nothing to do */
+		if (ast_format_cap_iscompatible_format(
+			    dest, fmt) == AST_FORMAT_CMP_NOT_EQUAL) {
+			continue;
+		}
+
+		/* if the source is supplying this format, then
+		   we can leave it in the result */
+		if (ast_format_cap_iscompatible_format(
+			    src, fmt) == AST_FORMAT_CMP_EQUAL) {
+			continue;
+		}
+
+		/* if we don't have a translation path from the src
+		   to this format, remove it from the result */
+		if (!matrix_get(src_index, index)->step) {
+			ast_format_cap_remove(result, fmt);
+			continue;
+		}
+
+		/* now check the opposite direction */
+		if (!matrix_get(index, src_index)->step) {
+			ast_format_cap_remove(result, fmt);
+		}
+	}
+
+}
+
 void ast_translate_available_formats(struct ast_format_cap *dest, struct ast_format_cap *src, struct ast_format_cap *result)
 {
-	struct ast_format tmp_fmt;
-	struct ast_format cur_dest, cur_src;
-	int src_audio = 0;
-	int src_video = 0;
+	struct ast_format *cur_dest, *cur_src;
 	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(dest, 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(src, cur_dest))) {
+				ast_format_cap_add(result, cur_src, 0);
+			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, cur_dest, 0);
+		}
+		ao2_ref(cur_dest, -1);
+	}
 
 	/* if we don't have a source format, we just have to try all
 	   possible destination formats */
@@ -1313,91 +1419,19 @@
 		return;
 	}
 
-	ast_format_cap_iter_start(src);
-	while (!ast_format_cap_iter_next(src, &cur_src)) {
-		/* If we have a source audio format, get its format index */
-		if (AST_FORMAT_GET_TYPE(cur_src.id) == AST_FORMAT_TYPE_AUDIO) {
-			src_audio = format2index(cur_src.id);
-		}
-
-		/* If we have a source video format, get its format index */
-		if (AST_FORMAT_GET_TYPE(cur_src.id) == AST_FORMAT_TYPE_VIDEO) {
-			src_video = format2index(cur_src.id);
+	for (index = 0; index < ast_format_cap_count(src); ++index) {
+		if (!(cur_src = ast_format_cap_get_format(src, index))) {
+			continue;
 		}
 
 		AST_RWLIST_RDLOCK(&translators);
-
-		/* For a given source audio format, traverse the list of
-		   known audio formats to determine whether there exists
-		   a translation path from the source format to the
-		   destination format. */
-		for (index = 0; (src_audio >= 0) && index < cur_max_index; index++) {
-			ast_format_set(&tmp_fmt, index2format(index), 0);
-
-			if (AST_FORMAT_GET_TYPE(tmp_fmt.id) != AST_FORMAT_TYPE_AUDIO) {
-				continue;
-			}
-
-			/* if this is not a desired format, nothing to do */
-			if (!ast_format_cap_iscompatible(dest, &tmp_fmt)) {
-				continue;
-			}
-
-			/* if the source is supplying this format, then
-			   we can leave it in the result */
-			if (ast_format_cap_iscompatible(src, &tmp_fmt)) {
-				continue;
-			}
-
-			/* if we don't have a translation path from the src
-			   to this format, remove it from the result */
-			if (!matrix_get(src_audio, index)->step) {
-				ast_format_cap_remove_byid(result, tmp_fmt.id);
-				continue;
-			}
-
-			/* now check the opposite direction */
-			if (!matrix_get(index, src_audio)->step) {
-				ast_format_cap_remove_byid(result, tmp_fmt.id);
-			}
-		}
-
-		/* For a given source video format, traverse the list of
-		   known video formats to determine whether there exists
-		   a translation path from the source format to the
-		   destination format. */
-		for (index = 0; (src_video >= 0) && index < cur_max_index; index++) {
-			ast_format_set(&tmp_fmt, index2format(index), 0);
-			if (AST_FORMAT_GET_TYPE(tmp_fmt.id) != AST_FORMAT_TYPE_VIDEO) {
-				continue;
-			}
-
-			/* if this is not a desired format, nothing to do */
-			if (!ast_format_cap_iscompatible(dest, &tmp_fmt)) {
-				continue;
-			}
-
-			/* if the source is supplying this format, then
-			   we can leave it in the result */
-			if (ast_format_cap_iscompatible(src, &tmp_fmt)) {
-				continue;
-			}
-
-			/* if we don't have a translation path from the src
-			   to this format, remove it from the result */
-			if (!matrix_get(src_video, index)->step) {
-				ast_format_cap_remove_byid(result, tmp_fmt.id);
-				continue;
-			}
-
-			/* now check the opposite direction */
-			if (!matrix_get(index, src_video)->step) {
-				ast_format_cap_remove_byid(result, tmp_fmt.id);
-			}
-		}
+		check_translation_path(dest, src, result,
+				       cur_src, AST_MEDIA_TYPE_AUDIO);
+		check_translation_path(dest, src, result,
+				       cur_src, AST_MEDIA_TYPE_VIDEO);
 		AST_RWLIST_UNLOCK(&translators);
-	}
-	ast_format_cap_iter_end(src);
+		ao2_ref(cur_src, -1);
+	}
 }
 
 static void translate_shutdown(void)




More information about the asterisk-commits mailing list