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