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 /* Uncomment the EXPERIMENTAL_TRANSLATION to enable a more complicated, but probably more
33 correct way of handling full duplex translation */
36 #define EXPERIMENTAL_TRANSLATION
39 /* This could all be done more efficiently *IF* we chained packets together
40 by default, but it would also complicate virtually every application. */
42 static ast_mutex_t list_lock = AST_MUTEX_INITIALIZER;
43 static struct ast_translator *list = NULL;
45 struct ast_translator_dir {
46 struct ast_translator *step; /* Next step translator */
47 int cost; /* Complete cost to destination */
50 struct ast_frame_delivery {
52 struct ast_channel *chan;
54 struct translator_pvt *owner;
55 struct ast_frame_delivery *prev;
56 struct ast_frame_delivery *next;
59 static struct ast_translator_dir tr_matrix[MAX_FORMAT][MAX_FORMAT];
61 struct ast_trans_pvt {
62 struct ast_translator *step;
63 struct ast_translator_pvt *state;
64 struct ast_trans_pvt *next;
68 static int powerof(int d)
71 for (x = 0; x < 32; x++)
74 ast_log(LOG_WARNING, "Powerof %d: No power??\n", d);
78 void ast_translator_free_path(struct ast_trans_pvt *p)
80 struct ast_trans_pvt *pl;
84 if (pl->state && pl->step->destroy)
85 pl->step->destroy(pl->state);
90 struct ast_trans_pvt *ast_translator_build_path(int dest, int source)
92 struct ast_trans_pvt *tmpr = NULL, *tmp = NULL;
93 /* One of the hardest parts: Build a set of translators based upon
94 the given source and destination formats */
95 source = powerof(source);
97 while(source != dest) {
98 if (tr_matrix[source][dest].step) {
100 tmp->next = malloc(sizeof(struct ast_trans_pvt));
103 tmp = malloc(sizeof(struct ast_trans_pvt));
108 tmp->step = tr_matrix[source][dest].step;
109 tmp->state = tmp->step->new();
111 ast_log(LOG_WARNING, "Failed to build translator step from %d to %d\n", source, dest);
116 /* Set the root, if it doesn't exist yet... */
119 /* Keep going if this isn't the final destination */
120 source = tmp->step->dstfmt;
122 /* XXX This could leak XXX */
123 ast_log(LOG_WARNING, "Out of memory\n");
127 /* We shouldn't have allocated any memory */
128 ast_log(LOG_WARNING, "No translator path from %s to %s\n",
129 ast_getformatname(source), ast_getformatname(dest));
136 struct ast_frame *ast_translate(struct ast_trans_pvt *path, struct ast_frame *f, int consume)
138 struct ast_trans_pvt *p;
139 struct ast_frame *out;
141 /* Feed the first frame into the first translator */
142 p->step->framein(p->state, f);
146 out = p->step->frameout(p->state);
147 /* If we get nothing out, return NULL */
150 /* If there is a next state, feed it in there. If not,
153 p->next->step->framein(p->next->state, out);
158 ast_log(LOG_WARNING, "I should never get here...\n");
162 static void rebuild_matrix(void)
164 struct ast_translator *t;
168 ast_log(LOG_DEBUG, "Reseting translation matrix\n");
169 /* Use the list of translators to build a translation matrix */
170 bzero(tr_matrix, sizeof(tr_matrix));
173 if (!tr_matrix[t->srcfmt][t->dstfmt].step ||
174 tr_matrix[t->srcfmt][t->dstfmt].cost > t->cost) {
175 tr_matrix[t->srcfmt][t->dstfmt].step = t;
176 tr_matrix[t->srcfmt][t->dstfmt].cost = t->cost;
182 /* Don't you just love O(N^3) operations? */
183 for (x=0; x< MAX_FORMAT; x++) /* For each source format */
184 for (y=0; y < MAX_FORMAT; y++) /* And each destination format */
185 if (x != y) /* Except ourselves, of course */
186 for (z=0; z < MAX_FORMAT; z++) /* And each format it might convert to */
187 if ((x!=z) && (y!=z)) /* Don't ever convert back to us */
188 if (tr_matrix[x][y].step && /* We can convert from x to y */
189 tr_matrix[y][z].step && /* And from y to z and... */
190 (!tr_matrix[x][z].step || /* Either there isn't an x->z conversion */
191 (tr_matrix[x][y].cost +
192 tr_matrix[y][z].cost < /* Or we're cheaper than the existing */
193 tr_matrix[x][z].cost) /* solution */
195 /* We can get from x to z via y with a cost that
196 is the sum of the transition from x to y and
199 tr_matrix[x][z].step = tr_matrix[x][y].step;
200 tr_matrix[x][z].cost = tr_matrix[x][y].cost +
201 tr_matrix[y][z].cost;
203 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);
210 static void calc_cost(struct ast_translator *t)
213 struct ast_translator_pvt *pvt;
214 struct ast_frame *f, *out;
215 struct timeval start, finish;
217 /* If they don't make samples, give them a terrible score */
219 ast_log(LOG_WARNING, "Translator '%s' does not produce sample frames.\n", t->name);
225 ast_log(LOG_WARNING, "Translator '%s' appears to be broken and will probably fail.\n", t->name);
229 gettimeofday(&start, NULL);
230 /* Call the encoder until we've processed one second of time */
231 while(sofar < 8000) {
234 ast_log(LOG_WARNING, "Translator '%s' failed to produce a sample frame.\n", t->name);
241 while((out = t->frameout(pvt))) {
242 sofar += out->samples;
246 gettimeofday(&finish, NULL);
248 cost = (finish.tv_sec - start.tv_sec) * 1000 + (finish.tv_usec - start.tv_usec) / 1000;
254 static int show_translation(int fd, int argc, char *argv[])
256 #define SHOW_TRANS 11
260 return RESULT_SHOWUSAGE;
261 ast_cli(fd, " Translation times between formats (in milliseconds)\n");
262 ast_cli(fd, " Source Format (Rows) Destination Format(Columns)\n\n");
263 ast_mutex_lock(&list_lock);
264 for (x=-1;x<SHOW_TRANS; x++) {
266 for (y=-1;y<SHOW_TRANS;y++) {
267 if (x >= 0 && y >= 0 && tr_matrix[x][y].step)
268 snprintf(line + strlen(line), sizeof(line) - strlen(line), " %5d", tr_matrix[x][y].cost);
270 if (((x == -1 && y >= 0) || (y == -1 && x >= 0))) {
271 snprintf(line + strlen(line), sizeof(line) - strlen(line),
272 " %5s", ast_getformatname(1<<(x+y+1)) );
273 } else if (x != -1 && y != -1 && y != 4) {
274 snprintf(line + strlen(line), sizeof(line) - strlen(line), " -");
276 snprintf(line + strlen(line), sizeof(line) - strlen(line), " ");
279 snprintf(line + strlen(line), sizeof(line) - strlen(line), "\n");
282 ast_mutex_unlock(&list_lock);
283 return RESULT_SUCCESS;
286 static int added_cli = 0;
288 static char show_trans_usage[] =
289 "Usage: show translation\n"
290 " Displays known codec translators and the cost associated\n"
291 "with each conversion.\n";
293 static struct ast_cli_entry show_trans =
294 { { "show", "translation", NULL }, show_translation, "Display translation matrix", show_trans_usage };
296 int ast_register_translator(struct ast_translator *t)
299 t->srcfmt = powerof(t->srcfmt);
300 t->dstfmt = powerof(t->dstfmt);
301 if ((t->srcfmt >= MAX_FORMAT) || (t->dstfmt >= MAX_FORMAT)) {
302 ast_log(LOG_WARNING, "Format %s is larger than MAX_FORMAT\n", ast_getformatname(t->srcfmt));
306 if (option_verbose > 1)
307 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);
308 ast_mutex_lock(&list_lock);
310 ast_cli_register(&show_trans);
316 ast_mutex_unlock(&list_lock);
320 int ast_unregister_translator(struct ast_translator *t)
322 struct ast_translator *u, *ul = NULL;
323 ast_mutex_lock(&list_lock);
337 ast_mutex_unlock(&list_lock);
341 int ast_translator_best_choice(int *dst, int *srcs)
343 /* Calculate our best source format, given costs, and a desired destination */
348 int besttime=999999999;
349 ast_mutex_lock(&list_lock);
350 for (y=0;y<MAX_FORMAT;y++) {
351 if ((cur & *dst) && (cur & *srcs)) {
352 /* This is a common format to both. Pick it if we don't have one already */
359 for (x=0;x<MAX_FORMAT;x++) {
360 if (tr_matrix[x][y].step && /* There's a step */
361 (tr_matrix[x][y].cost < besttime) && /* We're better than what exists now */
362 (*srcs & (1 << x))) /* x is a valid source format */
366 besttime = tr_matrix[x][y].cost;
376 ast_mutex_unlock(&list_lock);