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