#include <asterisk/lock.h>
#include <asterisk/channel.h>
#include <asterisk/channel_pvt.h>
#include <asterisk/logger.h>
#include <asterisk/translate.h>
#include <asterisk/options.h>
#include <asterisk/frame.h>
#include <asterisk/sched.h>
#include <asterisk/cli.h>
#include <asterisk/term.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
#include <string.h>
#include <stdio.h>
Go to the source code of this file.
Data Structures | |
struct | ast_frame_delivery |
struct | ast_trans_pvt |
struct | ast_translator_dir |
Defines | |
#define | SHOW_TRANS 11 |
Functions | |
void | ast_translator_free_path (struct ast_trans_pvt *p) |
Frees a translator path. | |
ast_trans_pvt * | ast_translator_build_path (int dest, int source) |
Builds a translator path. | |
ast_frame * | ast_translate (struct ast_trans_pvt *path, struct ast_frame *f, int consume) |
translates one or more frames | |
int | ast_register_translator (struct ast_translator *t) |
Register a translator. | |
int | ast_unregister_translator (struct ast_translator *t) |
Unregister a translator. | |
int | ast_translator_best_choice (int *dst, int *srcs) |
Chooses the best translation path. |
|
|
|
Register a translator.
Definition at line 297 of file translate.c. References ast_cli_register(), ast_getformatname(), ast_log(), ast_mutex_lock, ast_mutex_unlock, ast_verbose(), COLOR_BLACK, COLOR_MAGENTA, ast_translator::cost, ast_translator::dstfmt, LOG_WARNING, MAX_FORMAT, ast_translator::name, ast_translator::next, option_verbose, ast_translator::srcfmt, term_color(), and VERBOSE_PREFIX_2.
00298 { 00299 char tmp[80]; 00300 t->srcfmt = powerof(t->srcfmt); 00301 t->dstfmt = powerof(t->dstfmt); 00302 if ((t->srcfmt >= MAX_FORMAT) || (t->dstfmt >= MAX_FORMAT)) { 00303 ast_log(LOG_WARNING, "Format %s is larger than MAX_FORMAT\n", ast_getformatname(t->srcfmt)); 00304 return -1; 00305 } 00306 calc_cost(t); 00307 if (option_verbose > 1) 00308 ast_verbose(VERBOSE_PREFIX_2 "Registered translator '%s' from format %s to %s, cost %d\n", term_color(tmp, t->name, COLOR_MAGENTA, COLOR_BLACK, sizeof(tmp)), ast_getformatname(1 << t->srcfmt), ast_getformatname(1 << t->dstfmt), t->cost); 00309 ast_mutex_lock(&list_lock); 00310 if (!added_cli) { 00311 ast_cli_register(&show_trans); 00312 added_cli++; 00313 } 00314 t->next = list; 00315 list = t; 00316 rebuild_matrix(); 00317 ast_mutex_unlock(&list_lock); 00318 return 0; 00319 } |
|
translates one or more frames
Definition at line 136 of file translate.c. References ast_frfree(), ast_log(), ast_translator::framein, LOG_WARNING, and ast_trans_pvt::step. Referenced by ast_read(), ast_write(), and ast_writestream().
00137 { 00138 struct ast_trans_pvt *p; 00139 struct ast_frame *out; 00140 p = path; 00141 /* Feed the first frame into the first translator */ 00142 p->step->framein(p->state, f); 00143 if (consume) 00144 ast_frfree(f); 00145 while(p) { 00146 out = p->step->frameout(p->state); 00147 /* If we get nothing out, return NULL */ 00148 if (!out) 00149 return NULL; 00150 /* If there is a next state, feed it in there. If not, 00151 return this frame */ 00152 if (p->next) 00153 p->next->step->framein(p->next->state, out); 00154 else 00155 return out; 00156 p = p->next; 00157 } 00158 ast_log(LOG_WARNING, "I should never get here...\n"); 00159 return NULL; 00160 } |
|
Chooses the best translation path. Given a list of sources, and a designed destination format, which should I choose? Returns 0 on success, -1 if no path could be found. Modifies dests and srcs in place Definition at line 342 of file translate.c. References ast_mutex_lock, ast_mutex_unlock, ast_translator_dir::cost, MAX_FORMAT, and ast_translator_dir::step. Referenced by ast_channel_make_compatible(), ast_request(), ast_set_read_format(), and ast_set_write_format().
00343 { 00344 /* Calculate our best source format, given costs, and a desired destination */ 00345 int x,y; 00346 int best=-1; 00347 int bestdst=0; 00348 int cur = 1; 00349 int besttime=999999999; 00350 ast_mutex_lock(&list_lock); 00351 for (y=0;y<MAX_FORMAT;y++) { 00352 if ((cur & *dst) && (cur & *srcs)) { 00353 /* This is a common format to both. Pick it if we don't have one already */ 00354 besttime=0; 00355 bestdst = cur; 00356 best = cur; 00357 break; 00358 } 00359 if (cur & *dst) 00360 for (x=0;x<MAX_FORMAT;x++) { 00361 if (tr_matrix[x][y].step && /* There's a step */ 00362 (tr_matrix[x][y].cost < besttime) && /* We're better than what exists now */ 00363 (*srcs & (1 << x))) /* x is a valid source format */ 00364 { 00365 best = 1 << x; 00366 bestdst = cur; 00367 besttime = tr_matrix[x][y].cost; 00368 } 00369 } 00370 cur = cur << 1; 00371 } 00372 if (best > -1) { 00373 *srcs = best; 00374 *dst = bestdst; 00375 best = 0; 00376 } 00377 ast_mutex_unlock(&list_lock); 00378 return best; 00379 } |
|
Builds a translator path.
Definition at line 90 of file translate.c. References ast_getformatname(), ast_log(), free, LOG_WARNING, malloc, ast_translator::new, and ast_translator_dir::step. Referenced by ast_set_read_format(), ast_set_write_format(), and ast_writestream().
00091 { 00092 struct ast_trans_pvt *tmpr = NULL, *tmp = NULL; 00093 /* One of the hardest parts: Build a set of translators based upon 00094 the given source and destination formats */ 00095 source = powerof(source); 00096 dest = powerof(dest); 00097 while(source != dest) { 00098 if (tr_matrix[source][dest].step) { 00099 if (tmp) { 00100 tmp->next = malloc(sizeof(struct ast_trans_pvt)); 00101 tmp = tmp->next; 00102 } else 00103 tmp = malloc(sizeof(struct ast_trans_pvt)); 00104 00105 00106 if (tmp) { 00107 tmp->next = NULL; 00108 tmp->step = tr_matrix[source][dest].step; 00109 tmp->state = tmp->step->new(); 00110 if (!tmp->state) { 00111 ast_log(LOG_WARNING, "Failed to build translator step from %d to %d\n", source, dest); 00112 free(tmp); 00113 tmp = NULL; 00114 return NULL; 00115 } 00116 /* Set the root, if it doesn't exist yet... */ 00117 if (!tmpr) 00118 tmpr = tmp; 00119 /* Keep going if this isn't the final destination */ 00120 source = tmp->step->dstfmt; 00121 } else { 00122 /* XXX This could leak XXX */ 00123 ast_log(LOG_WARNING, "Out of memory\n"); 00124 return NULL; 00125 } 00126 } else { 00127 /* We shouldn't have allocated any memory */ 00128 ast_log(LOG_WARNING, "No translator path from %s to %s\n", 00129 ast_getformatname(source), ast_getformatname(dest)); 00130 return NULL; 00131 } 00132 } 00133 return tmpr; 00134 } |
|
Frees a translator path.
Definition at line 78 of file translate.c. References ast_translator::destroy, free, ast_trans_pvt::next, and ast_trans_pvt::step. Referenced by ast_channel_free(), ast_closestream(), ast_set_read_format(), ast_set_write_format(), and ast_writestream().
00079 { 00080 struct ast_trans_pvt *pl; 00081 while(p) { 00082 pl = p; 00083 p = p->next; 00084 if (pl->state && pl->step->destroy) 00085 pl->step->destroy(pl->state); 00086 free(pl); 00087 } 00088 } |
|
Unregister a translator.
Definition at line 321 of file translate.c. References ast_mutex_lock, ast_mutex_unlock, and ast_translator::next.
00322 { 00323 struct ast_translator *u, *ul = NULL; 00324 ast_mutex_lock(&list_lock); 00325 u = list; 00326 while(u) { 00327 if (u == t) { 00328 if (ul) 00329 ul->next = u->next; 00330 else 00331 list = u->next; 00332 break; 00333 } 00334 ul = u; 00335 u = u->next; 00336 } 00337 rebuild_matrix(); 00338 ast_mutex_unlock(&list_lock); 00339 return (u ? 0 : -1); 00340 } |