00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #include "asterisk.h"
00027
00028 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
00029
00030 #include <sys/types.h>
00031 #include <sys/socket.h>
00032 #include <sys/time.h>
00033 #include <unistd.h>
00034 #include <stdlib.h>
00035 #include <string.h>
00036 #include <stdio.h>
00037
00038 #include "asterisk/lock.h"
00039 #include "asterisk/channel.h"
00040 #include "asterisk/logger.h"
00041 #include "asterisk/translate.h"
00042 #include "asterisk/module.h"
00043 #include "asterisk/options.h"
00044 #include "asterisk/frame.h"
00045 #include "asterisk/sched.h"
00046 #include "asterisk/cli.h"
00047 #include "asterisk/term.h"
00048
00049 #define MAX_RECALC 200
00050
00051
00052 static AST_LIST_HEAD_STATIC(translators, ast_translator);
00053
00054 struct translator_path {
00055 struct ast_translator *step;
00056 unsigned int cost;
00057 unsigned int multistep;
00058 };
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070 static struct translator_path tr_matrix[MAX_FORMAT][MAX_FORMAT];
00071
00072
00073
00074
00075
00076
00077
00078
00079 static force_inline int powerof(unsigned int d)
00080 {
00081 int x = ffs(d);
00082
00083 if (x)
00084 return x - 1;
00085
00086 ast_log(LOG_WARNING, "No bits set? %d\n", d);
00087
00088 return -1;
00089 }
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099 static void *newpvt(struct ast_translator *t)
00100 {
00101 struct ast_trans_pvt *pvt;
00102 int len;
00103 int useplc = t->plc_samples > 0 && t->useplc;
00104 char *ofs;
00105
00106
00107
00108
00109
00110 len = sizeof(*pvt) + t->desc_size;
00111 if (useplc)
00112 len += sizeof(plc_state_t);
00113 if (t->buf_size)
00114 len += AST_FRIENDLY_OFFSET + t->buf_size;
00115 pvt = ast_calloc(1, len);
00116 if (!pvt)
00117 return NULL;
00118 pvt->t = t;
00119 ofs = (char *)(pvt + 1);
00120 if (t->desc_size) {
00121 pvt->pvt = ofs;
00122 ofs += t->desc_size;
00123 }
00124 if (useplc) {
00125 pvt->plc = (plc_state_t *)ofs;
00126 ofs += sizeof(plc_state_t);
00127 }
00128 if (t->buf_size)
00129 pvt->outbuf = ofs + AST_FRIENDLY_OFFSET;
00130
00131 if (t->newpvt && t->newpvt(pvt)) {
00132 free(pvt);
00133 return NULL;
00134 }
00135 ast_module_ref(t->module);
00136 return pvt;
00137 }
00138
00139 static void destroy(struct ast_trans_pvt *pvt)
00140 {
00141 struct ast_translator *t = pvt->t;
00142
00143 if (t->destroy)
00144 t->destroy(pvt);
00145 free(pvt);
00146 ast_module_unref(t->module);
00147 }
00148
00149
00150 static int framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
00151 {
00152 int16_t *dst = (int16_t *)pvt->outbuf;
00153 int ret;
00154 int samples = pvt->samples;
00155
00156
00157 pvt->f.has_timing_info = f->has_timing_info;
00158 pvt->f.ts = f->ts;
00159 pvt->f.len = f->len;
00160 pvt->f.seqno = f->seqno;
00161
00162 if (f->samples == 0) {
00163 ast_log(LOG_WARNING, "no samples for %s\n", pvt->t->name);
00164 }
00165 if (pvt->t->buffer_samples) {
00166 if (f->datalen == 0) {
00167 if (pvt->plc) {
00168 int l = pvt->t->plc_samples;
00169 if (pvt->samples + l > pvt->t->buffer_samples) {
00170 ast_log(LOG_WARNING, "Out of buffer space\n");
00171 return -1;
00172 }
00173 l = plc_fillin(pvt->plc, dst + pvt->samples, l);
00174 pvt->samples += l;
00175 pvt->datalen = pvt->samples * 2;
00176 }
00177
00178 if (!pvt->t->native_plc)
00179 return 0;
00180 }
00181 if (pvt->samples + f->samples > pvt->t->buffer_samples) {
00182 ast_log(LOG_WARNING, "Out of buffer space\n");
00183 return -1;
00184 }
00185 }
00186 if ( f->data == NULL && f->datalen > 0 ) {
00187 ast_log(LOG_ERROR, "NULL pointer in frame data for %s\n", pvt->t->name);
00188 return -1;
00189 }
00190
00191
00192
00193 ret = pvt->t->framein(pvt, f);
00194
00195 if (!ret && pvt->plc) {
00196 int l = pvt->t->plc_samples;
00197 if (pvt->samples < l)
00198 l = pvt->samples;
00199 plc_rx(pvt->plc, dst + pvt->samples - l, l);
00200 }
00201
00202 if (pvt->samples == samples)
00203 ast_log(LOG_WARNING, "%s did not update samples %d\n",
00204 pvt->t->name, pvt->samples);
00205 return ret;
00206 }
00207
00208
00209
00210
00211
00212
00213 struct ast_frame *ast_trans_frameout(struct ast_trans_pvt *pvt,
00214 int datalen, int samples)
00215 {
00216 struct ast_frame *f = &pvt->f;
00217
00218 if (samples)
00219 f->samples = samples;
00220 else {
00221 if (pvt->samples == 0)
00222 return NULL;
00223 f->samples = pvt->samples;
00224 pvt->samples = 0;
00225 }
00226 if (datalen)
00227 f->datalen = datalen;
00228 else {
00229 f->datalen = pvt->datalen;
00230 pvt->datalen = 0;
00231 }
00232
00233 f->frametype = AST_FRAME_VOICE;
00234 f->subclass = 1 << (pvt->t->dstfmt);
00235 f->mallocd = 0;
00236 f->offset = AST_FRIENDLY_OFFSET;
00237 f->src = pvt->t->name;
00238 f->data = pvt->outbuf;
00239 return f;
00240 }
00241
00242 static struct ast_frame *default_frameout(struct ast_trans_pvt *pvt)
00243 {
00244 return ast_trans_frameout(pvt, 0, 0);
00245 }
00246
00247
00248
00249 void ast_translator_free_path(struct ast_trans_pvt *p)
00250 {
00251 struct ast_trans_pvt *pn = p;
00252 while ( (p = pn) ) {
00253 pn = p->next;
00254 destroy(p);
00255 }
00256 }
00257
00258
00259 struct ast_trans_pvt *ast_translator_build_path(int dest, int source)
00260 {
00261 struct ast_trans_pvt *head = NULL, *tail = NULL;
00262
00263 source = powerof(source);
00264 dest = powerof(dest);
00265
00266 AST_LIST_LOCK(&translators);
00267
00268 while (source != dest) {
00269 struct ast_trans_pvt *cur;
00270 struct ast_translator *t = tr_matrix[source][dest].step;
00271 if (!t) {
00272 ast_log(LOG_WARNING, "No translator path from %s to %s\n",
00273 ast_getformatname(source), ast_getformatname(dest));
00274 AST_LIST_UNLOCK(&translators);
00275 return NULL;
00276 }
00277 if (!(cur = newpvt(t))) {
00278 ast_log(LOG_WARNING, "Failed to build translator step from %d to %d\n", source, dest);
00279 if (head)
00280 ast_translator_free_path(head);
00281 AST_LIST_UNLOCK(&translators);
00282 return NULL;
00283 }
00284 if (!head)
00285 head = cur;
00286 else
00287 tail->next = cur;
00288 tail = cur;
00289 cur->nextin = cur->nextout = ast_tv(0, 0);
00290
00291 source = cur->t->dstfmt;
00292 }
00293
00294 AST_LIST_UNLOCK(&translators);
00295 return head;
00296 }
00297
00298
00299 struct ast_frame *ast_translate(struct ast_trans_pvt *path, struct ast_frame *f, int consume)
00300 {
00301 struct ast_trans_pvt *p = path;
00302 struct ast_frame *out = f;
00303 struct timeval delivery;
00304 int has_timing_info;
00305 long ts;
00306 long len;
00307 int seqno;
00308
00309 has_timing_info = f->has_timing_info;
00310 ts = f->ts;
00311 len = f->len;
00312 seqno = f->seqno;
00313
00314
00315 if (!ast_tvzero(f->delivery)) {
00316 if (!ast_tvzero(path->nextin)) {
00317
00318 if (!ast_tveq(path->nextin, f->delivery)) {
00319
00320
00321
00322 if (!ast_tvzero(path->nextout)) {
00323 path->nextout = ast_tvadd(path->nextout,
00324 ast_tvsub(f->delivery, path->nextin));
00325 }
00326 path->nextin = f->delivery;
00327 }
00328 } else {
00329
00330 path->nextin = f->delivery;
00331 path->nextout = f->delivery;
00332 }
00333
00334 path->nextin = ast_tvadd(path->nextin, ast_samp2tv(f->samples, 8000));
00335 }
00336 delivery = f->delivery;
00337 for ( ; out && p ; p = p->next) {
00338 framein(p, out);
00339 out = p->t->frameout(p);
00340 }
00341 if (consume)
00342 ast_frfree(f);
00343 if (out == NULL)
00344 return NULL;
00345
00346 if (!ast_tvzero(delivery)) {
00347
00348 if (ast_tvzero(path->nextout))
00349 path->nextout = ast_tvnow();
00350
00351
00352 out->delivery = path->nextout;
00353
00354
00355
00356 path->nextout = ast_tvadd(path->nextout, ast_samp2tv( out->samples, 8000));
00357 } else {
00358 out->delivery = ast_tv(0, 0);
00359 out->has_timing_info = has_timing_info;
00360 if (has_timing_info) {
00361 out->ts = ts;
00362 out->len = len;
00363 out->seqno = seqno;
00364 }
00365 }
00366
00367 if (out->frametype == AST_FRAME_CNG)
00368 path->nextout = ast_tv(0, 0);
00369 return out;
00370 }
00371
00372
00373 static void calc_cost(struct ast_translator *t, int seconds)
00374 {
00375 int sofar=0;
00376 struct ast_trans_pvt *pvt;
00377 struct timeval start;
00378 int cost;
00379
00380 if (!seconds)
00381 seconds = 1;
00382
00383
00384 if (!t->sample) {
00385 ast_log(LOG_WARNING, "Translator '%s' does not produce sample frames.\n", t->name);
00386 t->cost = 99999;
00387 return;
00388 }
00389 pvt = newpvt(t);
00390 if (!pvt) {
00391 ast_log(LOG_WARNING, "Translator '%s' appears to be broken and will probably fail.\n", t->name);
00392 t->cost = 99999;
00393 return;
00394 }
00395 start = ast_tvnow();
00396
00397 while (sofar < seconds * 8000) {
00398 struct ast_frame *f = t->sample();
00399 if (!f) {
00400 ast_log(LOG_WARNING, "Translator '%s' failed to produce a sample frame.\n", t->name);
00401 destroy(pvt);
00402 t->cost = 99999;
00403 return;
00404 }
00405 framein(pvt, f);
00406 ast_frfree(f);
00407 while ((f = t->frameout(pvt))) {
00408 sofar += f->samples;
00409 ast_frfree(f);
00410 }
00411 }
00412 cost = ast_tvdiff_ms(ast_tvnow(), start);
00413 destroy(pvt);
00414 t->cost = cost / seconds;
00415 if (!t->cost)
00416 t->cost = 1;
00417 }
00418
00419
00420
00421
00422
00423 static void rebuild_matrix(int samples)
00424 {
00425 struct ast_translator *t;
00426 int x;
00427 int y;
00428 int z;
00429
00430 if (option_debug)
00431 ast_log(LOG_DEBUG, "Resetting translation matrix\n");
00432
00433 bzero(tr_matrix, sizeof(tr_matrix));
00434
00435
00436 AST_LIST_TRAVERSE(&translators, t, list) {
00437 if (!t->active)
00438 continue;
00439
00440 x = t->srcfmt;
00441 z = t->dstfmt;
00442
00443 if (samples)
00444 calc_cost(t, samples);
00445
00446 if (!tr_matrix[x][z].step || t->cost < tr_matrix[x][z].cost) {
00447 tr_matrix[x][z].step = t;
00448 tr_matrix[x][z].cost = t->cost;
00449 }
00450 }
00451
00452
00453
00454
00455
00456
00457
00458 for (;;) {
00459 int changed = 0;
00460 for (x = 0; x < MAX_FORMAT; x++) {
00461 for (y=0; y < MAX_FORMAT; y++) {
00462 if (x == y)
00463 continue;
00464
00465 for (z=0; z<MAX_FORMAT; z++) {
00466 int newcost;
00467
00468 if (z == x || z == y)
00469 continue;
00470 if (!tr_matrix[x][y].step)
00471 continue;
00472 if (!tr_matrix[y][z].step)
00473 continue;
00474 newcost = tr_matrix[x][y].cost + tr_matrix[y][z].cost;
00475 if (tr_matrix[x][z].step && newcost >= tr_matrix[x][z].cost)
00476 continue;
00477
00478
00479
00480
00481
00482 tr_matrix[x][z].step = tr_matrix[x][y].step;
00483 tr_matrix[x][z].cost = newcost;
00484 tr_matrix[x][z].multistep = 1;
00485 if (option_debug)
00486 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);
00487 changed++;
00488 }
00489 }
00490 }
00491 if (!changed)
00492 break;
00493 }
00494 }
00495
00496
00497 static int show_translation_deprecated(int fd, int argc, char *argv[])
00498 {
00499 #define SHOW_TRANS 13
00500 int x, y, z;
00501 int curlen = 0, longest = 0;
00502
00503 if (argc > 4)
00504 return RESULT_SHOWUSAGE;
00505
00506 AST_LIST_LOCK(&translators);
00507
00508 if (argv[2] && !strcasecmp(argv[2], "recalc")) {
00509 z = argv[3] ? atoi(argv[3]) : 1;
00510
00511 if (z <= 0) {
00512 ast_cli(fd, " C'mon let's be serious here... defaulting to 1.\n");
00513 z = 1;
00514 }
00515
00516 if (z > MAX_RECALC) {
00517 ast_cli(fd, " Maximum limit of recalc exceeded by %d, truncating value to %d\n", z - MAX_RECALC, MAX_RECALC);
00518 z = MAX_RECALC;
00519 }
00520 ast_cli(fd, " Recalculating Codec Translation (number of sample seconds: %d)\n\n", z);
00521 rebuild_matrix(z);
00522 }
00523
00524 ast_cli(fd, " Translation times between formats (in milliseconds) for one second of data\n");
00525 ast_cli(fd, " Source Format (Rows) Destination Format (Columns)\n\n");
00526
00527 for (x = 0; x < SHOW_TRANS; x++) {
00528 curlen = strlen(ast_getformatname(1 << (x + 1)));
00529 if (curlen > longest)
00530 longest = curlen;
00531 }
00532 for (x = -1; x < SHOW_TRANS; x++) {
00533 char line[120];
00534 char *buf = line;
00535 size_t left = sizeof(line) - 1;
00536
00537 *buf++ = ' ';
00538 *buf = '\0';
00539 for (y = -1; y < SHOW_TRANS; y++) {
00540 curlen = strlen(ast_getformatname(1 << (y)));
00541
00542 if (x >= 0 && y >= 0 && tr_matrix[x][y].step) {
00543
00544
00545
00546 ast_build_string(&buf, &left, "%*d", curlen + 1, tr_matrix[x][y].cost > 999 ? 0 : tr_matrix[x][y].cost);
00547 } else if (x == -1 && y >= 0) {
00548
00549 ast_build_string(&buf, &left, "%*s", curlen + 1, ast_getformatname(1 << (x + y + 1)) );
00550 } else if (y == -1 && x >= 0) {
00551
00552 ast_build_string(&buf, &left, "%*s", longest, ast_getformatname(1 << (x + y + 1)) );
00553 } else if (x >= 0 && y >= 0) {
00554 ast_build_string(&buf, &left, "%*s", curlen + 1, "-");
00555 } else {
00556 ast_build_string(&buf, &left, "%*s", longest, "");
00557 }
00558 }
00559 ast_build_string(&buf, &left, "\n");
00560 ast_cli(fd, line);
00561 }
00562 AST_LIST_UNLOCK(&translators);
00563 return RESULT_SUCCESS;
00564 }
00565
00566 static int show_translation(int fd, int argc, char *argv[])
00567 {
00568 int x, y, z;
00569 int curlen = 0, longest = 0;
00570
00571 if (argc > 5)
00572 return RESULT_SHOWUSAGE;
00573
00574 AST_LIST_LOCK(&translators);
00575
00576 if (argv[3] && !strcasecmp(argv[3], "recalc")) {
00577 z = argv[4] ? atoi(argv[4]) : 1;
00578
00579 if (z <= 0) {
00580 ast_cli(fd, " C'mon let's be serious here... defaulting to 1.\n");
00581 z = 1;
00582 }
00583
00584 if (z > MAX_RECALC) {
00585 ast_cli(fd, " Maximum limit of recalc exceeded by %d, truncating value to %d\n", z - MAX_RECALC, MAX_RECALC);
00586 z = MAX_RECALC;
00587 }
00588 ast_cli(fd, " Recalculating Codec Translation (number of sample seconds: %d)\n\n", z);
00589 rebuild_matrix(z);
00590 }
00591
00592 ast_cli(fd, " Translation times between formats (in milliseconds) for one second of data\n");
00593 ast_cli(fd, " Source Format (Rows) Destination Format (Columns)\n\n");
00594
00595 for (x = 0; x < SHOW_TRANS; x++) {
00596 curlen = strlen(ast_getformatname(1 << (x + 1)));
00597 if (curlen > longest)
00598 longest = curlen;
00599 }
00600 for (x = -1; x < SHOW_TRANS; x++) {
00601 char line[120];
00602 char *buf = line;
00603 size_t left = sizeof(line) - 1;
00604
00605 *buf++ = ' ';
00606 *buf = '\0';
00607 for (y = -1; y < SHOW_TRANS; y++) {
00608 curlen = strlen(ast_getformatname(1 << (y)));
00609
00610 if (x >= 0 && y >= 0 && tr_matrix[x][y].step) {
00611
00612
00613
00614 ast_build_string(&buf, &left, "%*d", curlen + 1, tr_matrix[x][y].cost > 999 ? 0 : tr_matrix[x][y].cost);
00615 } else if (x == -1 && y >= 0) {
00616
00617 ast_build_string(&buf, &left, "%*s", curlen + 1, ast_getformatname(1 << (x + y + 1)) );
00618 } else if (y == -1 && x >= 0) {
00619
00620 ast_build_string(&buf, &left, "%*s", longest, ast_getformatname(1 << (x + y + 1)) );
00621 } else if (x >= 0 && y >= 0) {
00622 ast_build_string(&buf, &left, "%*s", curlen + 1, "-");
00623 } else {
00624 ast_build_string(&buf, &left, "%*s", longest, "");
00625 }
00626 }
00627 ast_build_string(&buf, &left, "\n");
00628 ast_cli(fd, line);
00629 }
00630 AST_LIST_UNLOCK(&translators);
00631 return RESULT_SUCCESS;
00632 }
00633
00634 static char show_trans_usage[] =
00635 "Usage: core show translation [recalc] [<recalc seconds>]\n"
00636 " Displays known codec translators and the cost associated\n"
00637 "with each conversion. If the argument 'recalc' is supplied along\n"
00638 "with optional number of seconds to test a new test will be performed\n"
00639 "as the chart is being displayed.\n";
00640
00641 static struct ast_cli_entry cli_show_translation_deprecated = {
00642 { "show", "translation", NULL },
00643 show_translation_deprecated, NULL,
00644 NULL };
00645
00646 static struct ast_cli_entry cli_translate[] = {
00647 { { "core", "show", "translation", NULL },
00648 show_translation, "Display translation matrix",
00649 show_trans_usage, NULL, &cli_show_translation_deprecated },
00650 };
00651
00652
00653 int __ast_register_translator(struct ast_translator *t, struct ast_module *mod)
00654 {
00655 static int added_cli = 0;
00656 struct ast_translator *u;
00657
00658 if (!mod) {
00659 ast_log(LOG_WARNING, "Missing module pointer, you need to supply one\n");
00660 return -1;
00661 }
00662
00663 if (!t->buf_size) {
00664 ast_log(LOG_WARNING, "empty buf size, you need to supply one\n");
00665 return -1;
00666 }
00667
00668 t->module = mod;
00669
00670 t->srcfmt = powerof(t->srcfmt);
00671 t->dstfmt = powerof(t->dstfmt);
00672 t->active = 1;
00673
00674 if (t->plc_samples) {
00675 if (t->buffer_samples < t->plc_samples) {
00676 ast_log(LOG_WARNING, "plc_samples %d buffer_samples %d\n",
00677 t->plc_samples, t->buffer_samples);
00678 return -1;
00679 }
00680 if (t->dstfmt != powerof(AST_FORMAT_SLINEAR))
00681 ast_log(LOG_WARNING, "plc_samples %d format %x\n",
00682 t->plc_samples, t->dstfmt);
00683 }
00684 if (t->srcfmt >= MAX_FORMAT) {
00685 ast_log(LOG_WARNING, "Source format %s is larger than MAX_FORMAT\n", ast_getformatname(t->srcfmt));
00686 return -1;
00687 }
00688
00689 if (t->dstfmt >= MAX_FORMAT) {
00690 ast_log(LOG_WARNING, "Destination format %s is larger than MAX_FORMAT\n", ast_getformatname(t->dstfmt));
00691 return -1;
00692 }
00693
00694 if (t->buf_size) {
00695
00696
00697
00698
00699 struct _test_align { void *a, *b; } p;
00700 int align = (char *)&p.b - (char *)&p.a;
00701
00702 t->buf_size = ((t->buf_size + align - 1) / align) * align;
00703 }
00704
00705 if (t->frameout == NULL)
00706 t->frameout = default_frameout;
00707
00708 calc_cost(t, 1);
00709
00710 if (option_verbose > 1) {
00711 char tmp[80];
00712
00713 ast_verbose(VERBOSE_PREFIX_2 "Registered translator '%s' from format %s to %s, cost %d\n",
00714 term_color(tmp, t->name, COLOR_MAGENTA, COLOR_BLACK, sizeof(tmp)),
00715 ast_getformatname(1 << t->srcfmt), ast_getformatname(1 << t->dstfmt), t->cost);
00716 }
00717
00718 if (!added_cli) {
00719 ast_cli_register_multiple(cli_translate, sizeof(cli_translate) / sizeof(struct ast_cli_entry));
00720 added_cli++;
00721 }
00722
00723 AST_LIST_LOCK(&translators);
00724
00725
00726
00727 AST_LIST_TRAVERSE_SAFE_BEGIN(&translators, u, list) {
00728 if ((u->srcfmt == t->srcfmt) &&
00729 (u->dstfmt == t->dstfmt) &&
00730 (u->cost > t->cost)) {
00731 AST_LIST_INSERT_BEFORE_CURRENT(&translators, t, list);
00732 t = NULL;
00733 }
00734 }
00735 AST_LIST_TRAVERSE_SAFE_END;
00736
00737
00738
00739 if (t)
00740 AST_LIST_INSERT_HEAD(&translators, t, list);
00741
00742 rebuild_matrix(0);
00743
00744 AST_LIST_UNLOCK(&translators);
00745
00746 return 0;
00747 }
00748
00749
00750 int ast_unregister_translator(struct ast_translator *t)
00751 {
00752 char tmp[80];
00753 struct ast_translator *u;
00754 int found = 0;
00755
00756 AST_LIST_LOCK(&translators);
00757 AST_LIST_TRAVERSE_SAFE_BEGIN(&translators, u, list) {
00758 if (u == t) {
00759 AST_LIST_REMOVE_CURRENT(&translators, list);
00760 if (option_verbose > 1)
00761 ast_verbose(VERBOSE_PREFIX_2 "Unregistered translator '%s' from format %s to %s\n", term_color(tmp, t->name, COLOR_MAGENTA, COLOR_BLACK, sizeof(tmp)), ast_getformatname(1 << t->srcfmt), ast_getformatname(1 << t->dstfmt));
00762 found = 1;
00763 break;
00764 }
00765 }
00766 AST_LIST_TRAVERSE_SAFE_END;
00767
00768 if (found)
00769 rebuild_matrix(0);
00770
00771 AST_LIST_UNLOCK(&translators);
00772
00773 return (u ? 0 : -1);
00774 }
00775
00776 void ast_translator_activate(struct ast_translator *t)
00777 {
00778 AST_LIST_LOCK(&translators);
00779 t->active = 1;
00780 rebuild_matrix(0);
00781 AST_LIST_UNLOCK(&translators);
00782 }
00783
00784 void ast_translator_deactivate(struct ast_translator *t)
00785 {
00786 AST_LIST_LOCK(&translators);
00787 t->active = 0;
00788 rebuild_matrix(0);
00789 AST_LIST_UNLOCK(&translators);
00790 }
00791
00792
00793 int ast_translator_best_choice(int *dst, int *srcs)
00794 {
00795 int x,y;
00796 int best = -1;
00797 int bestdst = 0;
00798 int cur, cursrc;
00799 int besttime = INT_MAX;
00800 int beststeps = INT_MAX;
00801 int common = (*dst) & (*srcs);
00802
00803 if (common) {
00804 for (cur = 1, y = 0; y < MAX_FORMAT; cur <<= 1, y++) {
00805 if (cur & common)
00806 break;
00807 }
00808
00809 *srcs = *dst = cur;
00810 return 0;
00811 } else {
00812 AST_LIST_LOCK(&translators);
00813 for (cur = 1, y = 0; y < MAX_FORMAT; cur <<= 1, y++) {
00814 if (! (cur & *dst))
00815 continue;
00816 for (cursrc = 1, x = 0; x < MAX_FORMAT; cursrc <<= 1, x++) {
00817 if (!(*srcs & cursrc) || !tr_matrix[x][y].step ||
00818 tr_matrix[x][y].cost > besttime)
00819 continue;
00820 if (tr_matrix[x][y].cost < besttime ||
00821 tr_matrix[x][y].multistep < beststeps) {
00822
00823 best = cursrc;
00824 bestdst = cur;
00825 besttime = tr_matrix[x][y].cost;
00826 beststeps = tr_matrix[x][y].multistep;
00827 }
00828 }
00829 }
00830 AST_LIST_UNLOCK(&translators);
00831 if (best > -1) {
00832 *srcs = best;
00833 *dst = bestdst;
00834 best = 0;
00835 }
00836 return best;
00837 }
00838 }
00839
00840 unsigned int ast_translate_path_steps(unsigned int dest, unsigned int src)
00841 {
00842 unsigned int res = -1;
00843
00844
00845 src = powerof(src);
00846 dest = powerof(dest);
00847
00848 AST_LIST_LOCK(&translators);
00849
00850 if (tr_matrix[src][dest].step)
00851 res = tr_matrix[src][dest].multistep + 1;
00852
00853 AST_LIST_UNLOCK(&translators);
00854
00855 return res;
00856 }
00857
00858 unsigned int ast_translate_available_formats(unsigned int dest, unsigned int src)
00859 {
00860 unsigned int res = dest;
00861 unsigned int x;
00862 unsigned int src_audio = src & AST_FORMAT_AUDIO_MASK;
00863 unsigned int src_video = src & AST_FORMAT_VIDEO_MASK;
00864
00865
00866
00867 if (!src)
00868 return dest;
00869
00870
00871 if (src_audio)
00872 src_audio = powerof(src_audio);
00873
00874
00875 if (src_video)
00876 src_video = powerof(src_video);
00877
00878 AST_LIST_LOCK(&translators);
00879
00880
00881
00882
00883
00884 for (x = 1; src_audio && x < AST_FORMAT_MAX_AUDIO; x <<= 1) {
00885
00886 if (!dest & x)
00887 continue;
00888
00889
00890
00891 if (src & x)
00892 continue;
00893
00894
00895
00896 if (!tr_matrix[src_audio][powerof(x)].step) {
00897 res &= ~x;
00898 continue;
00899 }
00900
00901
00902 if (!tr_matrix[powerof(x)][src_audio].step)
00903 res &= ~x;
00904 }
00905
00906
00907
00908
00909
00910 for (; src_video && x < AST_FORMAT_MAX_VIDEO; x <<= 1) {
00911
00912 if (!dest & x)
00913 continue;
00914
00915
00916
00917 if (src & x)
00918 continue;
00919
00920
00921
00922 if (!tr_matrix[src_video][powerof(x)].step) {
00923 res &= ~x;
00924 continue;
00925 }
00926
00927
00928 if (!tr_matrix[powerof(x)][src_video].step)
00929 res &= ~x;
00930 }
00931
00932 AST_LIST_UNLOCK(&translators);
00933
00934 return res;
00935 }