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/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 <asterisk/frame.h>
20 #include <asterisk/sched.h>
21 #include <asterisk/cli.h>
22 #include <sys/socket.h>
30 /* Uncomment the EXPERIMENTAL_TRANSLATION to enable a more complicated, but probably more
31 correct way of handling full duplex translation */
34 #define EXPERIMENTAL_TRANSLATION
37 /* This could all be done more efficiently *IF* we chained packets together
38 by default, but it would also complicate virtually every application. */
40 static char *type = "Trans";
42 static pthread_mutex_t list_lock = PTHREAD_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 struct translator_pvt {
79 /* Sockets for communication */
81 struct ast_trans_pvt *system;
82 struct ast_trans_pvt *rsystem;
83 struct timeval lastpass;
84 #ifdef EXPERIMENTAL_TRANSLATION
85 struct ast_frame_delivery *head;
86 struct ast_frame_delivery *tail;
87 struct sched_context *sched;
93 #ifdef EXPERIMENTAL_TRANSLATION
94 static int deliver(void *data)
96 struct ast_frame_delivery *del = data;
97 ast_log(LOG_DEBUG, "Delivering a packet\n");
100 ast_write(del->chan, del->f);
102 ast_fr_fdwrite(del->fd, del->f);
105 /* Take us out of the list */
107 del->prev->next = del->next;
109 del->owner->head = del->next;
111 del->next->prev = del->prev;
113 del->owner->tail = del->prev;
114 /* Free used memory */
116 /* Never run again */
120 /* Schedule the delivery of a packet in the near future, using the given context */
121 static int schedule_delivery(struct sched_context *sched, struct translator_pvt *p, struct ast_channel *c,
122 int fd, struct ast_frame *f, int ms)
124 struct ast_frame_delivery *del;
125 ast_log(LOG_DEBUG, "Scheduling a packet delivery\n");
126 del = malloc(sizeof(struct ast_frame_delivery));
128 del->f = ast_frdup(f);
137 p->head = p->tail = del;
141 ast_sched_add(sched, ms, deliver, del);
147 static int translator_hangup(struct ast_channel *chan)
149 ast_log(LOG_WARNING, "Explicit hangup on '%s' not recommended! Call translator_destroy() instead.\n", chan->name);
150 chan->master->trans = NULL;
151 ast_hangup(chan->master);
156 static int translator_send_digit(struct ast_channel *chan, char digit)
158 /* Pass digits right along */
159 if (chan->master->pvt->send_digit)
160 return chan->master->pvt->send_digit(chan->master, digit);
164 static int translator_call(struct ast_channel *chan, char *addr, int timeout)
166 if (chan->master->pvt->call)
167 return chan->master->pvt->call(chan->master, addr, timeout);
171 static int translator_answer(struct ast_channel *chan)
173 if (chan->master->pvt->answer)
174 return chan->master->pvt->answer(chan->master);
178 void ast_translator_free_path(struct ast_trans_pvt *p)
180 struct ast_trans_pvt *pl;
184 if (pl->state && pl->step->destroy)
185 pl->step->destroy(pl->state);
190 static void ast_translator_free(struct translator_pvt *pvt)
192 #ifdef EXPERIMENTAL_TRANSLATION
193 struct ast_frame_delivery *d, *dl;
195 sched_context_destroy(pvt->sched);
204 ast_translator_free_path(pvt->system);
205 ast_translator_free_path(pvt->rsystem);
206 if (pvt->comm[0] > -1)
208 if (pvt->comm[1] > -1)
213 struct ast_trans_pvt *ast_translator_build_path(int source, int dest)
215 struct ast_trans_pvt *tmpr = NULL, *tmp = NULL;
216 /* One of the hardest parts: Build a set of translators based upon
217 the given source and destination formats */
218 source = powerof(source);
219 dest = powerof(dest);
220 while(source != dest) {
221 if (tr_matrix[source][dest].step) {
223 tmp->next = malloc(sizeof(struct ast_trans_pvt));
226 tmp = malloc(sizeof(struct ast_trans_pvt));
231 tmp->step = tr_matrix[source][dest].step;
232 tmp->state = tmp->step->new();
237 /* Set the root, if it doesn't exist yet... */
240 /* Keep going if this isn't the final destination */
241 source = tmp->step->dstfmt;
243 /* XXX This could leak XXX */
244 ast_log(LOG_WARNING, "Out of memory\n");
248 /* We shouldn't have allocated any memory */
249 ast_log(LOG_WARNING, "No translator path from %d to %d\n", source, dest);
256 static struct ast_frame *translator_read(struct ast_channel *chan)
258 return ast_fr_fdread(chan->fd);
261 static int translator_write(struct ast_channel *chan, struct ast_frame *frame)
263 return ast_fr_fdwrite(chan->fd, frame);
266 struct ast_frame_chain *ast_translate(struct ast_trans_pvt *path, struct ast_frame *f)
268 struct ast_trans_pvt *p;
269 struct ast_frame *out;
270 struct ast_frame_chain *outc = NULL, *prev = NULL, *cur;
272 /* Feed the first frame into the first translator */
273 p->step->framein(p->state, f);
275 /* Read all the frames from the current translator */
276 while((out = p->step->frameout(p->state))) {
278 /* Feed to next layer */
279 p->next->step->framein(p->next->state, out);
281 /* Last layer -- actually do something */
282 cur = malloc(sizeof(struct ast_frame_chain));
284 /* XXX Leak majorly on a problem XXX */
285 ast_log(LOG_WARNING, "Out of memory\n");
292 cur->fr = ast_frisolate(out);
307 static void translator_apply(struct translator_pvt *pvt, struct ast_trans_pvt *path, struct ast_frame *f, int fd, struct ast_channel *c, struct timeval *last)
309 struct ast_trans_pvt *p;
310 struct ast_frame *out;
314 /* Feed the first frame into the first translator */
315 p->step->framein(p->state, f);
317 /* Read all the frames from the current translator */
318 while((out = p->step->frameout(p->state))) {
320 /* Feed to next layer */
321 p->next->step->framein(p->next->state, out);
323 /* Delay if needed */
324 if (last->tv_sec || last->tv_usec) {
325 gettimeofday(&tv, NULL);
326 ms = 1000 * (tv.tv_sec - last->tv_sec) +
327 (tv.tv_usec - last->tv_usec) / 1000;
328 #ifdef EXPERIMENTAL_TRANSLATION
329 if (ms + FUDGE < out->timelen)
330 schedule_delivery(pvt->sched, pvt,
336 ast_fr_fdwrite(fd, out);
338 last->tv_sec = tv.tv_sec;
339 last->tv_usec = tv.tv_usec;
340 /* Schedule this packet to be delivered at the
344 /* XXX Not correct in the full duplex case XXX */
345 if (ms + FUDGE < out->timelen)
346 usleep((out->timelen - ms - FUDGE) * 1000);
347 last->tv_sec = tv.tv_sec;
348 last->tv_usec = tv.tv_usec;
354 ast_fr_fdwrite(fd, out);
362 static void *translator_thread(void *data)
364 struct ast_channel *real = data;
367 struct translator_pvt *pvt = NULL;
371 /* Read from the real, translate, write as necessary to the fake */
373 /* Break here if need be */
374 pthread_testcancel();
376 ast_log(LOG_WARNING, "No translator anymore\n");
379 pvt = real->trans->pvt->pvt;
383 CHECK_BLOCKING(real);
384 res = ast_waitfor_n_fd(fds, 2, &ms);
386 /* Or we can die here, that's fine too */
387 pthread_testcancel();
389 if (res == real->fd) {
393 ast_log(LOG_DEBUG, "Empty frame\n");
396 if (f->frametype == AST_FRAME_VOICE) {
398 translator_apply(pvt, pvt->system, f, fd, NULL, &pvt->lastpass);
400 /* If it's not voice, just pass it along */
401 ast_fr_fdwrite(fd, f);
405 f = ast_fr_fdread(res);
408 ast_log(LOG_DEBUG, "Empty (hangup) frame\n");
412 if (f->frametype == AST_FRAME_VOICE) {
414 translator_apply(pvt, pvt->rsystem, f, -1, real, &pvt->lastpass);
421 ast_log(LOG_DEBUG, "Waitfor returned non-zero\n");
428 /* Write a bogus frame */
435 struct ast_channel *ast_translator_create(struct ast_channel *real, int format, int direction)
437 struct ast_channel *tmp;
438 struct translator_pvt *pvt;
440 ast_log(LOG_WARNING, "Translator already exists on '%s'\n", real->name);
443 if (!(pvt = malloc(sizeof(struct translator_pvt)))) {
444 ast_log(LOG_WARNING, "Unable to allocate private translator on '%s'\n", real->name);
449 pvt->lastpass.tv_usec = 0;
450 pvt->lastpass.tv_sec = 0;
452 #ifdef EXPERIMENTAL_TRANSLATION
455 pvt->sched = sched_context_create();
457 ast_log(LOG_WARNING, "Out of memory\n");
458 ast_translator_free(pvt);
462 if (socketpair(AF_UNIX, SOCK_DGRAM, 0, pvt->comm)) {
463 ast_log(LOG_WARNING, "Unable to create UNIX domain socket on '%s'\n", real->name);
464 ast_translator_free(pvt);
467 /* In to the system */
468 if (direction & AST_DIRECTION_IN)
469 pvt->system = ast_translator_build_path(real->format, format);
472 /* Out from the system */
473 if (direction & AST_DIRECTION_OUT)
474 pvt->rsystem = ast_translator_build_path(format, real->format);
477 if (!pvt->system && !pvt->rsystem) {
478 ast_log(LOG_WARNING, "Unable to build a translation path for %s (%d to %d)\n", real->name, real->format, format);
479 ast_translator_free(pvt);
482 if (!pvt->system && (direction & AST_DIRECTION_IN)) {
483 ast_log(LOG_WARNING, "Translation path for '%s' is one-way (reverse)\n", real->name);
484 ast_translator_free(pvt);
487 if (!pvt->rsystem && (direction & AST_DIRECTION_OUT)) {
488 ast_log(LOG_WARNING, "Translation path for '%s' is one-way (forward)\n", real->name);
489 ast_translator_free(pvt);
492 if ((tmp = ast_channel_alloc())) {
493 snprintf(tmp->name, sizeof(tmp->name), "%s/Translate:%d", real->name, format);
495 tmp->fd = pvt->comm[0];
496 tmp->format = format;
497 tmp->state = real->state;
501 tmp->pvt->send_digit = translator_send_digit;
502 tmp->pvt->call = translator_call;
503 tmp->pvt->hangup = translator_hangup;
504 tmp->pvt->answer = translator_answer;
505 tmp->pvt->read = translator_read;
506 tmp->pvt->write = translator_write;
509 if (option_verbose > 2)
510 ast_verbose(VERBOSE_PREFIX_3 "Created translator %s\n", tmp->name);
511 if (pthread_create(&pvt->threadid, NULL, translator_thread, real) < 0) {
512 ast_translator_destroy(tmp);
514 ast_log(LOG_WARNING, "Failed to start thread\n");
517 ast_translator_free(pvt);
518 ast_log(LOG_WARNING, "Unable to allocate channel\n");
523 static void rebuild_matrix()
525 struct ast_translator *t;
529 ast_log(LOG_DEBUG, "Reseting translation matrix\n");
530 /* Use the list of translators to build a translation matrix */
531 bzero(tr_matrix, sizeof(tr_matrix));
534 if (!tr_matrix[t->srcfmt][t->dstfmt].step ||
535 tr_matrix[t->srcfmt][t->dstfmt].cost > t->cost) {
536 tr_matrix[t->srcfmt][t->dstfmt].step = t;
537 tr_matrix[t->srcfmt][t->dstfmt].cost = t->cost;
543 /* Don't you just love O(N^3) operations? */
544 for (x=0; x< MAX_FORMAT; x++) /* For each source format */
545 for (y=0; y < MAX_FORMAT; y++) /* And each destination format */
546 if (x != y) /* Except ourselves, of course */
547 for (z=0; z < MAX_FORMAT; z++) /* And each format it might convert to */
548 if ((x!=z) && (y!=z)) /* Don't ever convert back to us */
549 if (tr_matrix[x][y].step && /* We can convert from x to y */
550 tr_matrix[y][z].step && /* And from y to z and... */
551 (!tr_matrix[x][z].step || /* Either there isn't an x->z conversion */
552 (tr_matrix[x][y].cost +
553 tr_matrix[y][z].cost < /* Or we're cheaper than the existing */
554 tr_matrix[x][z].cost) /* solution */
556 /* We can get from x to z via y with a cost that
557 is the sum of the transition from x to y and
560 tr_matrix[x][z].step = tr_matrix[x][y].step;
561 tr_matrix[x][z].cost = tr_matrix[x][y].cost +
562 tr_matrix[y][z].cost;
564 ast_log(LOG_DEBUG, "Discovered %d cost path from %d to %d, via %d\n", tr_matrix[x][z].cost, x, z, y);
571 static void calc_cost(struct ast_translator *t)
574 struct ast_translator_pvt *pvt;
575 struct ast_frame *f, *out;
576 struct timeval start, finish;
578 /* If they don't make samples, give them a terrible score */
580 ast_log(LOG_WARNING, "Translator '%s' does not produce sample frames.\n", t->name);
586 ast_log(LOG_WARNING, "Translator '%s' appears to be broken and will probably fail.\n", t->name);
590 gettimeofday(&start, NULL);
591 /* Call the encoder until we've processed one second of time */
592 while(sofar < 1000) {
595 ast_log(LOG_WARNING, "Translator '%s' failed to produce a sample frame.\n", t->name);
602 while((out = t->frameout(pvt))) {
603 sofar += out->timelen;
607 gettimeofday(&finish, NULL);
609 cost = (finish.tv_sec - start.tv_sec) * 1000 + (finish.tv_usec - start.tv_usec) / 1000;
613 static int show_translation(int fd, int argc, char *argv[])
615 #define SHOW_TRANS 14
619 return RESULT_SHOWUSAGE;
620 ast_cli(fd, " Translation times between formats (in milliseconds)\n");
621 ast_cli(fd, " Destination Format\n");
622 pthread_mutex_lock(&list_lock);
623 for (x=0;x<SHOW_TRANS; x++) {
625 strcpy(line, " Src ");
627 strcpy(line, " Fmt ");
630 for (y=0;y<SHOW_TRANS;y++) {
631 if (tr_matrix[x][y].step)
632 snprintf(line + strlen(line), sizeof(line) - strlen(line), " %4d", tr_matrix[x][y].cost);
634 snprintf(line + strlen(line), sizeof(line) - strlen(line), " n/a");
636 snprintf(line + strlen(line), sizeof(line) - strlen(line), "\n");
639 pthread_mutex_unlock(&list_lock);
640 return RESULT_SUCCESS;
643 static int added_cli = 0;
645 static char show_trans_usage[] =
646 "Usage: show translation\n"
647 " Displays known codec translators and the cost associated\n"
648 "with each conversion.\n";
650 static struct ast_cli_entry show_trans =
651 { { "show", "translation", NULL }, show_translation, "Display translation matrix", show_trans_usage };
653 int ast_register_translator(struct ast_translator *t)
655 t->srcfmt = powerof(t->srcfmt);
656 t->dstfmt = powerof(t->dstfmt);
657 if ((t->srcfmt >= MAX_FORMAT) || (t->dstfmt >= MAX_FORMAT)) {
658 ast_log(LOG_WARNING, "Format %d is larger than MAX_FORMAT\n", t->srcfmt);
662 if (option_verbose > 1)
663 ast_verbose(VERBOSE_PREFIX_2 "Registered translator '%s' from format %d to %d, cost %d\n", t->name, t->srcfmt, t->dstfmt, t->cost);
664 pthread_mutex_lock(&list_lock);
666 ast_cli_register(&show_trans);
672 pthread_mutex_unlock(&list_lock);
676 int ast_unregister_translator(struct ast_translator *t)
678 struct ast_translator *u, *ul = NULL;
679 pthread_mutex_lock(&list_lock);
693 pthread_mutex_unlock(&list_lock);
697 void ast_translator_destroy(struct ast_channel *trans)
699 struct translator_pvt *pvt;
700 if (!trans->master) {
701 ast_log(LOG_WARNING, "Translator is not part of a real channel?\n");
704 if (trans->master->trans != trans) {
705 ast_log(LOG_WARNING, "Translator is not the right one!?!?\n");
708 trans->master->trans = NULL;
709 pvt = trans->pvt->pvt;
710 /* Cancel the running translator thread */
711 pthread_cancel(pvt->threadid);
712 pthread_join(pvt->threadid, NULL);
713 ast_translator_free(pvt);
714 trans->pvt->pvt = NULL;
715 if (option_verbose > 2)
716 ast_verbose(VERBOSE_PREFIX_3 "Destroyed translator %s\n", trans->name);
717 ast_channel_free(trans);
720 int ast_translator_best_choice(int dst, int srcs)
722 /* Calculate our best source format, given costs, and a desired destination */
726 int besttime=999999999;
727 pthread_mutex_lock(&list_lock);
728 for (y=0;y<MAX_FORMAT;y++) {
730 for (x=0;x<MAX_FORMAT;x++) {
731 if (tr_matrix[x][y].step && /* There's a step */
732 (tr_matrix[x][y].cost < besttime) && /* We're better than what exists now */
733 (srcs & (1 << x))) /* x is a valid source format */
736 besttime = tr_matrix[x][dst].cost;
741 pthread_mutex_unlock(&list_lock);