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