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;
133 struct timeval delivery;
135 /* Feed the first frame into the first translator */
136 p->step->framein(p->state, f);
137 delivery.tv_sec = f->delivery.tv_sec;
138 delivery.tv_usec = f->delivery.tv_usec;
142 out = p->step->frameout(p->state);
143 /* If we get nothing out, return NULL */
146 /* If there is a next state, feed it in there. If not,
149 p->next->step->framein(p->next->state, out);
151 out->delivery.tv_sec = delivery.tv_sec;
152 out->delivery.tv_usec = delivery.tv_usec;
157 ast_log(LOG_WARNING, "I should never get here...\n");
161 static void rebuild_matrix(void)
163 struct ast_translator *t;
167 ast_log(LOG_DEBUG, "Reseting translation matrix\n");
168 /* Use the list of translators to build a translation matrix */
169 bzero(tr_matrix, sizeof(tr_matrix));
172 if (!tr_matrix[t->srcfmt][t->dstfmt].step ||
173 tr_matrix[t->srcfmt][t->dstfmt].cost > t->cost) {
174 tr_matrix[t->srcfmt][t->dstfmt].step = t;
175 tr_matrix[t->srcfmt][t->dstfmt].cost = t->cost;
181 /* Don't you just love O(N^3) operations? */
182 for (x=0; x< MAX_FORMAT; x++) /* For each source format */
183 for (y=0; y < MAX_FORMAT; y++) /* And each destination format */
184 if (x != y) /* Except ourselves, of course */
185 for (z=0; z < MAX_FORMAT; z++) /* And each format it might convert to */
186 if ((x!=z) && (y!=z)) /* Don't ever convert back to us */
187 if (tr_matrix[x][y].step && /* We can convert from x to y */
188 tr_matrix[y][z].step && /* And from y to z and... */
189 (!tr_matrix[x][z].step || /* Either there isn't an x->z conversion */
190 (tr_matrix[x][y].cost +
191 tr_matrix[y][z].cost < /* Or we're cheaper than the existing */
192 tr_matrix[x][z].cost) /* solution */
194 /* We can get from x to z via y with a cost that
195 is the sum of the transition from x to y and
198 tr_matrix[x][z].step = tr_matrix[x][y].step;
199 tr_matrix[x][z].cost = tr_matrix[x][y].cost +
200 tr_matrix[y][z].cost;
202 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);
209 static void calc_cost(struct ast_translator *t)
212 struct ast_translator_pvt *pvt;
213 struct ast_frame *f, *out;
214 struct timeval start, finish;
216 /* If they don't make samples, give them a terrible score */
218 ast_log(LOG_WARNING, "Translator '%s' does not produce sample frames.\n", t->name);
224 ast_log(LOG_WARNING, "Translator '%s' appears to be broken and will probably fail.\n", t->name);
228 gettimeofday(&start, NULL);
229 /* Call the encoder until we've processed one second of time */
230 while(sofar < 8000) {
233 ast_log(LOG_WARNING, "Translator '%s' failed to produce a sample frame.\n", t->name);
240 while((out = t->frameout(pvt))) {
241 sofar += out->samples;
245 gettimeofday(&finish, NULL);
247 cost = (finish.tv_sec - start.tv_sec) * 1000 + (finish.tv_usec - start.tv_usec) / 1000;
253 static int show_translation(int fd, int argc, char *argv[])
255 #define SHOW_TRANS 11
259 return RESULT_SHOWUSAGE;
260 ast_cli(fd, " Translation times between formats (in milliseconds)\n");
261 ast_cli(fd, " Source Format (Rows) Destination Format(Columns)\n\n");
262 ast_mutex_lock(&list_lock);
263 for (x=-1;x<SHOW_TRANS; x++) {
265 for (y=-1;y<SHOW_TRANS;y++) {
266 if (x >= 0 && y >= 0 && tr_matrix[x][y].step)
267 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);
269 if (((x == -1 && y >= 0) || (y == -1 && x >= 0))) {
270 snprintf(line + strlen(line), sizeof(line) - strlen(line),
271 " %5s", ast_getformatname(1<<(x+y+1)) );
272 } else if (x != -1 && y != -1) {
273 snprintf(line + strlen(line), sizeof(line) - strlen(line), " -");
275 snprintf(line + strlen(line), sizeof(line) - strlen(line), " ");
278 snprintf(line + strlen(line), sizeof(line) - strlen(line), "\n");
281 ast_mutex_unlock(&list_lock);
282 return RESULT_SUCCESS;
285 static int added_cli = 0;
287 static char show_trans_usage[] =
288 "Usage: show translation\n"
289 " Displays known codec translators and the cost associated\n"
290 "with each conversion.\n";
292 static struct ast_cli_entry show_trans =
293 { { "show", "translation", NULL }, show_translation, "Display translation matrix", show_trans_usage };
295 int ast_register_translator(struct ast_translator *t)
298 t->srcfmt = powerof(t->srcfmt);
299 t->dstfmt = powerof(t->dstfmt);
300 if ((t->srcfmt >= MAX_FORMAT) || (t->dstfmt >= MAX_FORMAT)) {
301 ast_log(LOG_WARNING, "Format %s is larger than MAX_FORMAT\n", ast_getformatname(t->srcfmt));
305 if (option_verbose > 1)
306 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);
307 ast_mutex_lock(&list_lock);
309 ast_cli_register(&show_trans);
315 ast_mutex_unlock(&list_lock);
319 int ast_unregister_translator(struct ast_translator *t)
321 struct ast_translator *u, *ul = NULL;
322 ast_mutex_lock(&list_lock);
336 ast_mutex_unlock(&list_lock);
340 int ast_translator_best_choice(int *dst, int *srcs)
342 /* Calculate our best source format, given costs, and a desired destination */
347 int besttime=999999999;
348 ast_mutex_lock(&list_lock);
349 for (y=0;y<MAX_FORMAT;y++) {
350 if ((cur & *dst) && (cur & *srcs)) {
351 /* This is a common format to both. Pick it if we don't have one already */
358 for (x=0;x<MAX_FORMAT;x++) {
359 if (tr_matrix[x][y].step && /* There's a step */
360 (tr_matrix[x][y].cost < besttime) && /* We're better than what exists now */
361 (*srcs & (1 << x))) /* x is a valid source format */
365 besttime = tr_matrix[x][y].cost;
375 ast_mutex_unlock(&list_lock);