Main Page | Alphabetical List | Data Structures | File List | Data Fields | Globals

translate.h File Reference

#include <asterisk/frame.h>

Go to the source code of this file.

Data Structures

struct  ast_translator
 data structure associated with a translator More...


Defines

#define MAX_FORMAT   32

Functions

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 *dsts, int *srcs)
 Chooses the best translation path.

ast_trans_pvtast_translator_build_path (int dest, int source)
 Builds a translator path.

void ast_translator_free_path (struct ast_trans_pvt *tr)
 Frees a translator path.

ast_frameast_translate (struct ast_trans_pvt *tr, struct ast_frame *f, int consume)
 translates one or more frames


Define Documentation

#define MAX_FORMAT   32
 

Definition at line 17 of file translate.h.

Referenced by ast_register_translator(), and ast_translator_best_choice().


Function Documentation

int ast_register_translator struct ast_translator t  ) 
 

Register a translator.

Parameters:
t populated ast_translator structure This registers a codec translator with asterisk Returns 0 on success, -1 on failure

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 }

struct ast_frame* ast_translate struct ast_trans_pvt tr,
struct ast_frame f,
int  consume
 

translates one or more frames

Parameters:
tr translator structure to use for translation
f frame to translate
consume Whether or not to free the original frame Apply an input frame into the translator and receive zero or one output frames. Consume determines whether the original frame should be freed Returns an ast_frame of the new translation format on success, NULL on failure

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 }

int ast_translator_best_choice int *  dsts,
int *  srcs
 

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 }

struct ast_trans_pvt* ast_translator_build_path int  dest,
int  source
 

Builds a translator path.

Parameters:
dest destination format
source source format Build a path (possibly NULL) from source to dest Returns ast_trans_pvt on success, NULL on failure

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 }

void ast_translator_free_path struct ast_trans_pvt tr  ) 
 

Frees a translator path.

Parameters:
tr translator path to get rid of Frees the given translator path structure

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 }

int ast_unregister_translator struct ast_translator t  ) 
 

Unregister a translator.

Parameters:
t translator to unregister Unregisters the given tranlator Returns 0 on success, -1 on failure

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 }


Generated on Fri Oct 31 07:05:18 2003 for Asterisk by doxygen 1.3.4