2 * Asterisk -- A telephony toolkit for Linux.
4 * Translate via the use of pseudo channels
6 * Copyright (C) 1999, Adtran Inc. and Linux Support Services, LLC
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/channel.h>
15 #include <asterisk/channel_pvt.h>
16 #include <asterisk/logger.h>
17 #include <asterisk/translate.h>
18 #include <asterisk/options.h>
19 #include <sys/socket.h>
27 static char *type = "Trans";
29 static pthread_mutex_t list_lock = PTHREAD_MUTEX_INITIALIZER;
30 static struct ast_translator *list = NULL;
32 struct ast_translator_dir {
33 struct ast_translator *step; /* Next step translator */
34 int cost; /* Complete cost to destination */
37 static struct ast_translator_dir tr_matrix[MAX_FORMAT][MAX_FORMAT];
39 struct ast_trans_pvt {
40 struct ast_translator *step;
41 struct ast_translator_pvt *state;
42 struct ast_trans_pvt *next;
46 static int powerof(int d)
49 for (x = 0; x < 32; x++)
52 ast_log(LOG_WARNING, "Powerof %d: No power??\n", d);
56 struct translator_pvt {
57 /* Sockets for communication */
59 struct ast_trans_pvt *system;
60 struct ast_trans_pvt *rsystem;
63 static int translator_hangup(struct ast_channel *chan)
65 ast_log(LOG_WARNING, "Explicit hangup on '%s' not recommended! Call translator_destroy() instead.\n", chan->name);
66 chan->master->trans = NULL;
67 ast_hangup(chan->master);
72 static int translator_send_digit(struct ast_channel *chan, char digit)
74 /* Pass digits right along */
75 if (chan->master->pvt->send_digit)
76 return chan->master->pvt->send_digit(chan->master, digit);
80 static int translator_call(struct ast_channel *chan, char *addr, int timeout)
82 if (chan->master->pvt->call)
83 return chan->master->pvt->call(chan->master, addr, timeout);
87 static int translator_answer(struct ast_channel *chan)
89 if (chan->master->pvt->answer)
90 return chan->master->pvt->answer(chan->master);
94 void ast_translator_free_path(struct ast_trans_pvt *p)
96 struct ast_trans_pvt *pl;
100 if (pl->state && pl->step->destroy)
101 pl->step->destroy(pl->state);
106 static void ast_translator_free(struct translator_pvt *pvt)
108 ast_translator_free_path(pvt->system);
109 ast_translator_free_path(pvt->rsystem);
110 if (pvt->comm[0] > -1)
112 if (pvt->comm[1] > -1)
117 struct ast_trans_pvt *ast_translator_build_path(int source, int dest)
119 struct ast_trans_pvt *tmpr = NULL, *tmp = NULL;
120 /* One of the hardest parts: Build a set of translators based upon
121 the given source and destination formats */
122 source = powerof(source);
123 dest = powerof(dest);
124 while(source != dest) {
125 if (tr_matrix[source][dest].step) {
127 tmp->next = malloc(sizeof(struct ast_trans_pvt));
130 tmp = malloc(sizeof(struct ast_trans_pvt));
135 tmp->step = tr_matrix[source][dest].step;
136 tmp->state = tmp->step->new();
141 /* Set the root, if it doesn't exist yet... */
144 /* Keep going if this isn't the final destination */
145 source = tmp->step->dstfmt;
147 /* XXX This could leak XXX */
148 ast_log(LOG_WARNING, "Out of memory\n");
156 static struct ast_frame *fd_read(int fd)
158 /* XXX Wrong: Not thread safe! XXX */
161 struct ast_frame *f = (struct ast_frame *)buf;
162 /* Read a frame directly from there. They're always in the
165 if (read(fd, buf, sizeof(struct ast_frame))
166 == sizeof(struct ast_frame)) {
167 /* read the frame header */
169 f->data = buf + sizeof(struct ast_frame) + AST_FRIENDLY_OFFSET;
170 f->offset = AST_FRIENDLY_OFFSET;
171 if (f->datalen > sizeof(buf) - sizeof(struct ast_frame) - AST_FRIENDLY_OFFSET) {
172 /* Really bad read */
173 ast_log(LOG_WARNING, "Strange read (%d bytes)\n", f->datalen);
177 if ((res = read(fd, f->data, f->datalen)) != f->datalen) {
179 ast_log(LOG_WARNING, "How very strange, expected %d, got %d\n", f->datalen, res);
183 return ast_frisolate(f);
184 } else if (option_debug)
185 ast_log(LOG_DEBUG, "NULL or invalid header\n");
186 /* Null if there was an error */
190 static struct ast_frame *translator_read(struct ast_channel *chan)
192 return fd_read(chan->fd);
195 static int fd_write(int fd, struct ast_frame *frame)
197 /* Write the frame exactly */
198 if (write(fd, frame, sizeof(struct ast_frame)) != sizeof(struct ast_frame)) {
199 ast_log(LOG_WARNING, "Write error\n");
202 if (write(fd, frame->data, frame->datalen) != frame->datalen) {
203 ast_log(LOG_WARNING, "Write error\n");
209 static int translator_write(struct ast_channel *chan, struct ast_frame *frame)
211 return fd_write(chan->fd, frame);
214 struct ast_frame_chain *ast_translate(struct ast_trans_pvt *path, struct ast_frame *f)
216 struct ast_trans_pvt *p;
217 struct ast_frame *out;
218 struct ast_frame_chain *outc = NULL, *prev = NULL, *cur;
220 /* Feed the first frame into the first translator */
221 p->step->framein(p->state, f);
223 /* Read all the frames from the current translator */
224 while((out = p->step->frameout(p->state))) {
226 /* Feed to next layer */
227 p->next->step->framein(p->next->state, out);
229 /* Last layer -- actually do something */
230 cur = malloc(sizeof(struct ast_frame_chain));
232 /* XXX Leak majorly on a problem XXX */
233 ast_log(LOG_WARNING, "Out of memory\n");
240 cur->fr = ast_frisolate(out);
253 /* XXX There's an experimentally derived fudge factor XXX */
256 static void translator_apply(struct ast_trans_pvt *path, struct ast_frame *f, int fd, struct ast_channel *c)
258 struct ast_trans_pvt *p;
259 struct ast_frame *out;
261 /* Feed the first frame into the first translator */
262 p->step->framein(p->state, f);
264 /* Read all the frames from the current translator */
265 while((out = p->step->frameout(p->state))) {
267 /* Feed to next layer */
268 p->next->step->framein(p->next->state, out);
280 static void *translator_thread(void *data)
282 struct ast_channel *real = data;
285 struct translator_pvt *pvt = NULL;
289 /* Read from the real, translate, write as necessary to the fake */
292 ast_log(LOG_WARNING, "No translator anymore\n");
295 pvt = real->trans->pvt->pvt;
299 CHECK_BLOCKING(real);
300 res = ast_waitfor_n_fd(fds, 2, &ms);
303 if (res == real->fd) {
307 ast_log(LOG_DEBUG, "Empty frame\n");
310 if (f->frametype == AST_FRAME_VOICE) {
312 translator_apply(pvt->system, f, fd, NULL);
314 /* If it's not voice, just pass it along */
322 ast_log(LOG_DEBUG, "Empty (hangup) frame\n");
325 if (f->frametype == AST_FRAME_VOICE) {
327 translator_apply(pvt->rsystem, f, -1, real);
334 ast_log(LOG_DEBUG, "Waitfor returned non-zero\n");
341 /* Write a bogus frame */
348 struct ast_channel *ast_translator_create(struct ast_channel *real, int format, int direction)
350 struct ast_channel *tmp;
351 struct translator_pvt *pvt;
354 ast_log(LOG_WARNING, "Translator already exists on '%s'\n", real->name);
357 if (!(pvt = malloc(sizeof(struct translator_pvt)))) {
358 ast_log(LOG_WARNING, "Unable to allocate private translator on '%s'\n", real->name);
363 if (socketpair(AF_UNIX, SOCK_DGRAM, 0, pvt->comm)) {
364 ast_log(LOG_WARNING, "Unable to create UNIX domain socket on '%s'\n", real->name);
365 ast_translator_free(pvt);
367 /* In to the system */
368 if (direction & AST_DIRECTION_IN)
369 pvt->system = ast_translator_build_path(real->format, format);
372 /* Out from the system */
373 if (direction & AST_DIRECTION_OUT)
374 pvt->rsystem = ast_translator_build_path(format, real->format);
377 if (!pvt->system && !pvt->rsystem) {
378 ast_log(LOG_WARNING, "Unable to build a translation path for %s (%d to %d)\n", real->name, real->format, format);
379 ast_translator_free(pvt);
382 if (!pvt->system && (direction & AST_DIRECTION_IN)) {
383 ast_log(LOG_WARNING, "Translation path for '%s' is one-way (reverse)\n", real->name);
384 ast_translator_free(pvt);
387 if (!pvt->rsystem && (direction & AST_DIRECTION_OUT)) {
388 ast_log(LOG_WARNING, "Translation path for '%s' is one-way (forward)\n", real->name);
389 ast_translator_free(pvt);
392 if ((tmp = ast_channel_alloc())) {
393 snprintf(tmp->name, sizeof(tmp->name), "%s/Translate:%d", real->name, format);
395 tmp->fd = pvt->comm[0];
396 tmp->format = format;
397 tmp->state = real->state;
401 tmp->pvt->send_digit = translator_send_digit;
402 tmp->pvt->call = translator_call;
403 tmp->pvt->hangup = translator_hangup;
404 tmp->pvt->answer = translator_answer;
405 tmp->pvt->read = translator_read;
406 tmp->pvt->write = translator_write;
408 if (option_verbose > 2)
409 ast_verbose(VERBOSE_PREFIX_3 "Created translator %s\n", tmp->name);
410 if (pthread_create(&t, NULL, translator_thread, real) < 0) {
411 ast_translator_destroy(tmp);
413 ast_log(LOG_WARNING, "Failed to start thread\n");
416 ast_translator_free(pvt);
417 ast_log(LOG_WARNING, "Unable to allocate channel\n");
422 static void rebuild_matrix()
424 struct ast_translator *t;
428 ast_log(LOG_DEBUG, "Reseting translation matrix\n");
429 /* Use the list of translators to build a translation matrix */
430 bzero(tr_matrix, sizeof(tr_matrix));
433 if (!tr_matrix[t->srcfmt][t->dstfmt].step ||
434 tr_matrix[t->srcfmt][t->dstfmt].cost > t->cost) {
435 tr_matrix[t->srcfmt][t->dstfmt].step = t;
436 tr_matrix[t->srcfmt][t->dstfmt].cost = t->cost;
442 /* Don't you just love O(N^3) operations? */
443 for (x=0; x< MAX_FORMAT; x++) /* For each source format */
444 for (y=0; y < MAX_FORMAT; y++) /* And each destination format */
445 if (x != y) /* Except ourselves, of course */
446 for (z=0; z < MAX_FORMAT; z++) /* And each format it might convert to */
447 if ((x!=z) && (y!=z)) /* Don't ever convert back to us */
448 if (tr_matrix[x][y].step && /* We can convert from x to y */
449 tr_matrix[y][z].step && /* And from y to z and... */
450 (!tr_matrix[x][z].step || /* Either there isn't an x->z conversion */
451 (tr_matrix[x][y].cost +
452 tr_matrix[y][z].cost < /* Or we're cheaper than the existing */
453 tr_matrix[x][z].cost) /* solution */
455 /* We can get from x to z via y with a cost that
456 is the sum of the transition from x to y and
459 tr_matrix[x][z].step = tr_matrix[x][y].step;
460 tr_matrix[x][z].cost = tr_matrix[x][y].cost +
461 tr_matrix[y][z].cost;
463 ast_log(LOG_DEBUG, "Discovered %d cost path from %d to %d, via %d\n", tr_matrix[x][z].cost, x, z, y);
470 static void calc_cost(struct ast_translator *t)
473 struct ast_translator_pvt *pvt;
474 struct ast_frame *f, *out;
475 struct timeval start, finish;
477 /* If they don't make samples, give them a terrible score */
479 ast_log(LOG_WARNING, "Translator '%s' does not produce sample frames.\n", t->name);
485 ast_log(LOG_WARNING, "Translator '%s' appears to be broken and will probably fail.\n", t->name);
489 gettimeofday(&start, NULL);
490 /* Call the encoder until we've processed one second of time */
491 while(sofar < 1000) {
494 ast_log(LOG_WARNING, "Translator '%s' failed to produce a sample frame.\n", t->name);
501 while((out = t->frameout(pvt))) {
502 sofar += out->timelen;
506 gettimeofday(&finish, NULL);
508 cost = (finish.tv_sec - start.tv_sec) * 1000 + (finish.tv_usec - start.tv_usec) / 1000;
512 int ast_register_translator(struct ast_translator *t)
514 t->srcfmt = powerof(t->srcfmt);
515 t->dstfmt = powerof(t->dstfmt);
516 if ((t->srcfmt >= MAX_FORMAT) || (t->dstfmt >= MAX_FORMAT)) {
517 ast_log(LOG_WARNING, "Format %d is larger than MAX_FORMAT\n", t->srcfmt);
521 if (option_verbose > 1)
522 ast_verbose(VERBOSE_PREFIX_2 "Registered translator '%s' from format %d to %d, cost %d\n", t->name, t->srcfmt, t->dstfmt, t->cost);
523 pthread_mutex_lock(&list_lock);
527 pthread_mutex_unlock(&list_lock);
531 int ast_unregister_translator(struct ast_translator *t)
533 struct ast_translator *u, *ul = NULL;
534 pthread_mutex_lock(&list_lock);
547 pthread_mutex_unlock(&list_lock);
551 void ast_translator_destroy(struct ast_channel *trans)
555 if (!trans->master) {
556 ast_log(LOG_WARNING, "Translator is not part of a real channel?\n");
559 if (trans->master->trans != trans) {
560 ast_log(LOG_WARNING, "Translator is not the right one!?!?\n");
563 trans->master->trans = NULL;
564 /* Write an invalid frame to kill off the main thread, which will
565 in turn acknowledge by writing an invalid frame back to us, unless
566 they're already closed. */
567 if (trans->fd > -1) {
568 if (write(trans->fd, trans, 1) == 1) {
569 /* Wait for a respnose, but no more than 1 second */
570 if (ast_waitfor_n_fd(&trans->fd, 1, &ms) == trans->fd)
571 read(trans->fd, &dummy, 1);
574 if (option_verbose > 2)
575 ast_verbose(VERBOSE_PREFIX_3 "Destroyed translator %s\n", trans->name);
577 ast_channel_free(trans);
580 int ast_translator_best_choice(int dst, int srcs)
582 /* Calculate our best source format, given costs, and a desired destination */
585 int besttime=999999999;
587 pthread_mutex_lock(&list_lock);
588 for (x=0;x<MAX_FORMAT;x++) {
589 if (tr_matrix[x][dst].step && /* There's a step */
590 (tr_matrix[x][dst].cost < besttime) && /* We're better than what exists now */
591 (srcs & (1 << x))) /* x is a valid source format */
594 besttime = tr_matrix[x][dst].cost;
598 pthread_mutex_unlock(&list_lock);