[asterisk-commits] dvossel: branch dvossel/fixtheworld_phase1_step2 r299402 - /team/dvossel/fixt...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Tue Dec 21 22:15:33 UTC 2010


Author: dvossel
Date: Tue Dec 21 16:15:29 2010
New Revision: 299402

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=299402
Log:
clean up translator cli functions

Modified:
    team/dvossel/fixtheworld_phase1_step2/main/translate.c

Modified: team/dvossel/fixtheworld_phase1_step2/main/translate.c
URL: http://svnview.digium.com/svn/asterisk/team/dvossel/fixtheworld_phase1_step2/main/translate.c?view=diff&rev=299402&r1=299401&r2=299402
==============================================================================
--- team/dvossel/fixtheworld_phase1_step2/main/translate.c (original)
+++ team/dvossel/fixtheworld_phase1_step2/main/translate.c Tue Dec 21 16:15:29 2010
@@ -45,16 +45,16 @@
 /*! This value is used to define how large the tr_matrix and tr_index2format arrays
  *  are defined to be in memory by default.  Once Asterisk can represent more formats
  *  than this number represents with translation, increase it. */
-#define MAX_INDEX 256
+#define MAX_INDEX 128
 
 /*! \brief the list of translators */
 static AST_RWLIST_HEAD_STATIC(translators, ast_translator);
 
 struct translator_path {
 	struct ast_translator *step;       /*!< Next step translator */
-	unsigned int table_cost;           /*!< Complete table cost to destination */
-	unsigned int multistep;            /*!< Multiple conversions required for this translation */
-};
+	uint32_t table_cost;               /*!< Complete table cost to destination */
+	uint8_t multistep;                 /*!< Multiple conversions required for this translation */
+} __attribute__((packed));
 
 /*! \brief a matrix that, for any pair of supported formats,
  * indicates the total cost of translation and the first step.
@@ -93,6 +93,7 @@
 	}
 
 	if (cur_max_index == ARRAY_LEN(tr_index2format)) {
+		ast_log(LOG_WARNING, "All translator matrix index values are in use! Increase size of MAX_INDEX.\n");
 		return -1; /* hit max length. */
 	}
 	tr_index2format[cur_max_index] = id;
@@ -626,119 +627,35 @@
 	return ret;
 }
 
-static char *handle_cli_core_show_translation(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
-{
-#define SHOW_TRANS 64
-	static const char * const option1[] = { "recalc", "paths", NULL };
-	int x, y, z;
+static void handle_cli_recalc(struct ast_cli_args *a)
+{
+	int time = a->argv[4] ? atoi(a->argv[4]) : 1;
+
+	if (time <= 0) {
+		ast_cli(a->fd, "         Recalc must be greater than 0.  Defaulting to 1.\n");
+		time = 1;
+	}
+
+	if (time > MAX_RECALC) {
+		ast_cli(a->fd, "         Maximum limit of recalc exceeded by %d, truncating value to %d\n", time - MAX_RECALC, MAX_RECALC);
+		time = MAX_RECALC;
+	}
+	ast_cli(a->fd, "         Recalculating Codec Translation (number of sample seconds: %d)\n\n", time);
+	AST_RWLIST_WRLOCK(&translators);
+	rebuild_matrix(time);
+	AST_RWLIST_UNLOCK(&translators);
+}
+
+static char *handle_show_translation_table(struct ast_cli_args *a)
+{
+	int x, y;
 	int curlen = 0, longest = 0;
 
-	switch (cmd) {
-	case CLI_INIT:
-		e->command = "core show translation";
-		e->usage =
-			"Usage: 'core show translation' can be used in two ways.\n"
-			"       1. 'core show translation [recalc [<recalc seconds>]]\n"
-			"          Displays known codec translators and the cost associated\n"
-			"          with each conversion.  If the argument 'recalc' is supplied along\n"
-			"          with optional number of seconds to test a new test will be performed\n"
-			"          as the chart is being displayed.\n"
-			"       2. 'core show translation paths [codec]'\n"
-			"           This will display all the translation paths associated with a codec\n";
-		return NULL;
-	case CLI_GENERATE:
-		if (a->pos == 3) {
-			return ast_cli_complete(a->word, option1, a->n);
-		}
-		if (a->pos == 4 && !strcasecmp(a->argv[3], option1[1])) {
-			return complete_trans_path_choice(a->line, a->word, a->pos, a->n);
-		}
-		return NULL;
-	}
-
-	if (a->argc > 5)
-		return CLI_SHOWUSAGE;
-
-	if (a->argv[3] && !strcasecmp(a->argv[3], option1[1]) && a->argc == 5) {
-		format_t input_src = 0;
-		format_t src = 0;
-		size_t len = 0;
-		int dst;
-		int i;
-		const struct ast_format_list *format_list = ast_get_format_list(&len);
-		struct ast_str *str = ast_str_alloca(256);
-		struct ast_translator *step;
-
-		for (i = 0; i < len; i++) {
-			if (!(format_list[i].bits & AST_FORMAT_AUDIO_MASK)) {
-				continue;
-			}
-			if (!strncasecmp(format_list[i].name, a->argv[4], strlen(format_list[i].name))) {
-				input_src = format_list[i].bits;
-			}
-		}
-
-		if (!input_src) {
-			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], ast_format_rate(input_src));
-		for (i = 0; i < len; i++) {
-			if (!(format_list[i].bits & AST_FORMAT_AUDIO_MASK) || (format_list[i].bits == input_src)) {
-				continue;
-			}
-			dst = format2index(format_list[i].bits);
-			src = format2index(input_src);
-			ast_str_reset(str);
-			if (tr_matrix[src][dst].step) {
-				ast_str_append(&str, 0, "%s", ast_getformatname(index2format(tr_matrix[src][dst].step->srcfmt)));
-				while (src != dst) {
-					step = tr_matrix[src][dst].step;
-					if (!step) {
-						ast_str_reset(str);
-						break;
-					}
-					ast_str_append(&str, 0, "->%s", ast_getformatname(index2format(step->dstfmt)));
-					src = step->dstfmt;
-				}
-			}
-
-			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_RWLIST_UNLOCK(&translators);
-
-		return CLI_SUCCESS;
-	} else if (a->argv[3] && !strcasecmp(a->argv[3], "recalc")) {
-		z = a->argv[4] ? atoi(a->argv[4]) : 1;
-
-		if (z <= 0) {
-			ast_cli(a->fd, "         Recalc must be greater than 0.  Defaulting to 1.\n");
-			z = 1;
-		}
-
-		if (z > MAX_RECALC) {
-			ast_cli(a->fd, "         Maximum limit of recalc exceeded by %d, truncating value to %d\n", z - MAX_RECALC, MAX_RECALC);
-			z = MAX_RECALC;
-		}
-		ast_cli(a->fd, "         Recalculating Codec Translation (number of sample seconds: %d)\n\n", z);
-		AST_RWLIST_WRLOCK(&translators);
-		rebuild_matrix(z);
-		AST_RWLIST_UNLOCK(&translators);
-	} else if (a->argc > 3)
-		return CLI_SHOWUSAGE;
-
 	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 (x = 0; x < SHOW_TRANS; x++) {
+	for (x = 0; x < cur_max_index; x++) {
 		/* translation only applies to audio right now. */
 		if (!(AST_FORMAT_AUDIO_MASK & (index2format(x))))
 			continue;
@@ -747,7 +664,7 @@
 			longest = curlen;
 		}
 	}
-	for (x = -1; x < SHOW_TRANS; x++) {
+	for (x = -1; x < cur_max_index; x++) {
 		struct ast_str *out = ast_str_alloca(256);
 		/* translation only applies to audio right now. */
 		if (x >= 0 && !(AST_FORMAT_AUDIO_MASK & (index2format(x))))
@@ -756,7 +673,7 @@
 		if(x >= 0 && !strcmp(ast_getformatname(index2format(x)), "unknown"))
 			continue;
 		ast_str_set(&out, -1, " ");
-		for (y = -1; y < SHOW_TRANS; y++) {
+		for (y = -1; y < cur_max_index; y++) {
 			/* translation only applies to audio right now. */
 			if (y >= 0 && !(AST_FORMAT_AUDIO_MASK & (index2format(y))))
 				continue;
@@ -790,6 +707,104 @@
 	}
 	AST_RWLIST_UNLOCK(&translators);
 	return CLI_SUCCESS;
+}
+
+static char *handle_show_translation_path(struct ast_cli_args *a)
+{
+	format_t input_src = 0;
+	format_t src = 0;
+	size_t len = 0;
+	int dst;
+	int i;
+	const struct ast_format_list *format_list = ast_get_format_list(&len);
+	struct ast_str *str = ast_str_alloca(256);
+	struct ast_translator *step;
+
+	for (i = 0; i < len; i++) {
+		if (!(format_list[i].bits & AST_FORMAT_AUDIO_MASK)) {
+			continue;
+		}
+		if (!strncasecmp(format_list[i].name, a->argv[4], strlen(format_list[i].name))) {
+			input_src = format_list[i].bits;
+		}
+	}
+
+	if (!input_src) {
+		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], ast_format_rate(input_src));
+	for (i = 0; i < len; i++) {
+		if (!(format_list[i].bits & AST_FORMAT_AUDIO_MASK) || (format_list[i].bits == input_src)) {
+			continue;
+		}
+		dst = format2index(format_list[i].bits);
+		src = format2index(input_src);
+		ast_str_reset(str);
+		if (tr_matrix[src][dst].step) {
+			ast_str_append(&str, 0, "%s", ast_getformatname(index2format(tr_matrix[src][dst].step->srcfmt)));
+			while (src != dst) {
+				step = tr_matrix[src][dst].step;
+				if (!step) {
+					ast_str_reset(str);
+					break;
+				}
+				ast_str_append(&str, 0, "->%s", ast_getformatname(index2format(step->dstfmt)));
+				src = step->dstfmt;
+			}
+		}
+
+		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_RWLIST_UNLOCK(&translators);
+
+	return CLI_SUCCESS;
+}
+
+static char *handle_cli_core_show_translation(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
+{
+	static const char * const option[] = { "recalc", "paths", NULL };
+
+	switch (cmd) {
+	case CLI_INIT:
+		e->command = "core show translation";
+		e->usage =
+			"Usage: 'core show translation' can be used in two ways.\n"
+			"       1. 'core show translation [recalc [<recalc seconds>]]\n"
+			"          Displays known codec translators and the cost associated\n"
+			"          with each conversion.  If the argument 'recalc' is supplied along\n"
+			"          with optional number of seconds to test a new test will be performed\n"
+			"          as the chart is being displayed.\n"
+			"       2. 'core show translation paths [codec]'\n"
+			"           This will display all the translation paths associated with a codec\n";
+		return NULL;
+	case CLI_GENERATE:
+		if (a->pos == 3) {
+			return ast_cli_complete(a->word, option, a->n);
+		}
+		if (a->pos == 4 && !strcasecmp(a->argv[3], option[1])) {
+			return complete_trans_path_choice(a->line, a->word, a->pos, a->n);
+		}
+		return NULL;
+	}
+
+	if (a->argc > 5)
+		return CLI_SHOWUSAGE;
+
+	if (a->argv[3] && !strcasecmp(a->argv[3], option[1]) && a->argc == 5) { /* show paths */
+		return handle_show_translation_path(a);
+	} else if (a->argv[3] && !strcasecmp(a->argv[3], option[0])) { /* recalc and then fall through to show table */
+		handle_cli_recalc(a);
+	} else if (a->argc > 3) { /* wrong input */
+		return CLI_SHOWUSAGE;
+	}
+
+	return handle_show_translation_table(a);
 }
 
 static struct ast_cli_entry cli_translate[] = {
@@ -1102,5 +1117,3 @@
 
 	return res;
 }
-
-




More information about the asterisk-commits mailing list