2 * Asterisk -- A telephony toolkit for Linux.
4 * Translate via the use of pseudo channels
6 * Copyright (C) 1999, Mark Spencer
8 * Mark Spencer <markster@linux-support.net>
10 * This program is free software, distributed under the terms of
11 * the GNU General Public License
14 #include <asterisk/lock.h>
15 #include <asterisk/channel.h>
16 #include <asterisk/channel_pvt.h>
17 #include <asterisk/logger.h>
18 #include <asterisk/translate.h>
19 #include <asterisk/options.h>
20 #include <asterisk/frame.h>
21 #include <asterisk/sched.h>
22 #include <asterisk/cli.h>
23 #include <asterisk/term.h>
24 #include <sys/socket.h>
32 /* This could all be done more efficiently *IF* we chained packets together
33 by default, but it would also complicate virtually every application. */
35 static ast_mutex_t list_lock = AST_MUTEX_INITIALIZER;
36 static struct ast_translator *list = NULL;
38 struct ast_translator_dir {
39 struct ast_translator *step; /* Next step translator */
40 int cost; /* Complete cost to destination */
43 struct ast_frame_delivery {
45 struct ast_channel *chan;
47 struct translator_pvt *owner;
48 struct ast_frame_delivery *prev;
49 struct ast_frame_delivery *next;
52 static struct ast_translator_dir tr_matrix[MAX_FORMAT][MAX_FORMAT];
54 struct ast_trans_pvt {
55 struct ast_translator *step;
56 struct ast_translator_pvt *state;
57 struct ast_trans_pvt *next;
61 static int powerof(int d)
64 for (x = 0; x < 32; x++)
67 ast_log(LOG_WARNING, "Powerof %d: No power??\n", d);
71 void ast_translator_free_path(struct ast_trans_pvt *p)
73 struct ast_trans_pvt *pl;
77 if (pl->state && pl->step->destroy)
78 pl->step->destroy(pl->state);
83 struct ast_trans_pvt *ast_translator_build_path(int dest, int source)
85 struct ast_trans_pvt *tmpr = NULL, *tmp = NULL;
86 /* One of the hardest parts: Build a set of translators based upon
87 the given source and destination formats */
88 source = powerof(source);
90 while(source != dest) {
91 if (tr_matrix[source][dest].step) {
93 tmp->next = malloc(sizeof(struct ast_trans_pvt));
96 tmp = malloc(sizeof(struct ast_trans_pvt));
101 tmp->step = tr_matrix[source][dest].step;
102 tmp->state = tmp->step->new();
104 ast_log(LOG_WARNING, "Failed to build translator step from %d to %d\n", source, dest);
109 /* Set the root, if it doesn't exist yet... */
112 /* Keep going if this isn't the final destination */
113 source = tmp->step->dstfmt;
115 /* XXX This could leak XXX */
116 ast_log(LOG_WARNING, "Out of memory\n");
120 /* We shouldn't have allocated any memory */
121 ast_log(LOG_WARNING, "No translator path from %s to %s\n",
122 ast_getformatname(source), ast_getformatname(dest));
129 struct ast_frame *ast_translate(struct ast_trans_pvt *path, struct ast_frame *f, int consume)
131 struct ast_trans_pvt *p;
132 struct ast_frame *out;
134 /* Feed the first frame into the first translator */
135 p->step->framein(p->state, f);
139 out = p->step->frameout(p->state);
140 /* If we get nothing out, return NULL */
143 /* If there is a next state, feed it in there. If not,
146 p->next->step->framein(p->next->state, out);
151 ast_log(LOG_WARNING, "I should never get here...\n");
155 static void rebuild_matrix(void)
157 struct ast_translator *t;
161 ast_log(LOG_DEBUG, "Reseting translation matrix\n");
162 /* Use the list of translators to build a translation matrix */
163 bzero(tr_matrix, sizeof(tr_matrix));
166 if (!tr_matrix[t->srcfmt][t->dstfmt].step ||
167 tr_matrix[t->srcfmt][t->dstfmt].cost > t->cost) {
168 tr_matrix[t->srcfmt][t->dstfmt].step = t;
169 tr_matrix[t->srcfmt][t->dstfmt].cost = t->cost;
175 /* Don't you just love O(N^3) operations? */
176 for (x=0; x< MAX_FORMAT; x++) /* For each source format */
177 for (y=0; y < MAX_FORMAT; y++) /* And each destination format */
178 if (x != y) /* Except ourselves, of course */
179 for (z=0; z < MAX_FORMAT; z++) /* And each format it might convert to */
180 if ((x!=z) && (y!=z)) /* Don't ever convert back to us */
181 if (tr_matrix[x][y].step && /* We can convert from x to y */
182 tr_matrix[y][z].step && /* And from y to z and... */
183 (!tr_matrix[x][z].step || /* Either there isn't an x->z conversion */
184 (tr_matrix[x][y].cost +
185 tr_matrix[y][z].cost < /* Or we're cheaper than the existing */
186 tr_matrix[x][z].cost) /* solution */
188 /* We can get from x to z via y with a cost that
189 is the sum of the transition from x to y and
192 tr_matrix[x][z].step = tr_matrix[x][y].step;
193 tr_matrix[x][z].cost = tr_matrix[x][y].cost +
194 tr_matrix[y][z].cost;
196 ast_log(LOG_DEBUG, "Discovered %d cost path from %s to %s, via %d\n", tr_matrix[x][z].cost, ast_getformatname(x), ast_getformatname(z), y);
203 static void calc_cost(struct ast_translator *t)
206 struct ast_translator_pvt *pvt;
207 struct ast_frame *f, *out;
208 struct timeval start, finish;
210 /* If they don't make samples, give them a terrible score */
212 ast_log(LOG_WARNING, "Translator '%s' does not produce sample frames.\n", t->name);
218 ast_log(LOG_WARNING, "Translator '%s' appears to be broken and will probably fail.\n", t->name);
222 gettimeofday(&start, NULL);
223 /* Call the encoder until we've processed one second of time */
224 while(sofar < 8000) {
227 ast_log(LOG_WARNING, "Translator '%s' failed to produce a sample frame.\n", t->name);
234 while((out = t->frameout(pvt))) {
235 sofar += out->samples;
239 gettimeofday(&finish, NULL);
241 cost = (finish.tv_sec - start.tv_sec) * 1000 + (finish.tv_usec - start.tv_usec) / 1000;
247 static int show_translation(int fd, int argc, char *argv[])
249 #define SHOW_TRANS 11
253 return RESULT_SHOWUSAGE;
254 ast_cli(fd, " Translation times between formats (in milliseconds)\n");
255 ast_cli(fd, " Source Format (Rows) Destination Format(Columns)\n\n");
256 ast_mutex_lock(&list_lock);
257 for (x=-1;x<SHOW_TRANS; x++) {
259 for (y=-1;y<SHOW_TRANS;y++) {
260 if (x >= 0 && y >= 0 && tr_matrix[x][y].step)
261 snprintf(line + strlen(line), sizeof(line) - strlen(line), " %5d", tr_matrix[x][y].cost >= 99999 ? tr_matrix[x][y].cost-99999 : tr_matrix[x][y].cost);
263 if (((x == -1 && y >= 0) || (y == -1 && x >= 0))) {
264 snprintf(line + strlen(line), sizeof(line) - strlen(line),
265 " %5s", ast_getformatname(1<<(x+y+1)) );
266 } else if (x != -1 && y != -1) {
267 snprintf(line + strlen(line), sizeof(line) - strlen(line), " -");
269 snprintf(line + strlen(line), sizeof(line) - strlen(line), " ");
272 snprintf(line + strlen(line), sizeof(line) - strlen(line), "\n");
275 ast_mutex_unlock(&list_lock);
276 return RESULT_SUCCESS;
279 static int added_cli = 0;
281 static char show_trans_usage[] =
282 "Usage: show translation\n"
283 " Displays known codec translators and the cost associated\n"
284 "with each conversion.\n";
286 static struct ast_cli_entry show_trans =
287 { { "show", "translation", NULL }, show_translation, "Display translation matrix", show_trans_usage };
289 int ast_register_translator(struct ast_translator *t)
292 t->srcfmt = powerof(t->srcfmt);
293 t->dstfmt = powerof(t->dstfmt);
294 if ((t->srcfmt >= MAX_FORMAT) || (t->dstfmt >= MAX_FORMAT)) {
295 ast_log(LOG_WARNING, "Format %s is larger than MAX_FORMAT\n", ast_getformatname(t->srcfmt));
299 if (option_verbose > 1)
300 ast_verbose(VERBOSE_PREFIX_2 "Registered translator '%s' from format %s to %s, cost %d\n", term_color(tmp, t->name, COLOR_MAGENTA, COLOR_BLACK, sizeof(tmp)), ast_getformatname(1 << t->srcfmt), ast_getformatname(1 << t->dstfmt), t->cost);
301 ast_mutex_lock(&list_lock);
303 ast_cli_register(&show_trans);
309 ast_mutex_unlock(&list_lock);
313 int ast_unregister_translator(struct ast_translator *t)
315 struct ast_translator *u, *ul = NULL;
316 ast_mutex_lock(&list_lock);
330 ast_mutex_unlock(&list_lock);
334 int ast_translator_best_choice(int *dst, int *srcs)
336 /* Calculate our best source format, given costs, and a desired destination */
341 int besttime=999999999;
342 ast_mutex_lock(&list_lock);
343 for (y=0;y<MAX_FORMAT;y++) {
344 if ((cur & *dst) && (cur & *srcs)) {
345 /* This is a common format to both. Pick it if we don't have one already */
352 for (x=0;x<MAX_FORMAT;x++) {
353 if (tr_matrix[x][y].step && /* There's a step */
354 (tr_matrix[x][y].cost < besttime) && /* We're better than what exists now */
355 (*srcs & (1 << x))) /* x is a valid source format */
359 besttime = tr_matrix[x][y].cost;
369 ast_mutex_unlock(&list_lock);