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