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

translate.c File Reference

#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_translator_dir
struct  ast_frame_delivery
struct  ast_trans_pvt

Defines

#define SHOW_TRANS   11

Functions

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

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

ast_frameast_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.


Define Documentation

#define SHOW_TRANS   11
 


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 290 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.

00291 {
00292    char tmp[80];
00293    t->srcfmt = powerof(t->srcfmt);
00294    t->dstfmt = powerof(t->dstfmt);
00295    if ((t->srcfmt >= MAX_FORMAT) || (t->dstfmt >= MAX_FORMAT)) {
00296       ast_log(LOG_WARNING, "Format %s is larger than MAX_FORMAT\n", ast_getformatname(t->srcfmt));
00297       return -1;
00298    }
00299    calc_cost(t);
00300    if (option_verbose > 1)
00301       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);
00302    ast_mutex_lock(&list_lock);
00303    if (!added_cli) {
00304       ast_cli_register(&show_trans);
00305       added_cli++;
00306    }
00307    t->next = list;
00308    list = t;
00309    rebuild_matrix();
00310    ast_mutex_unlock(&list_lock);
00311    return 0;
00312 }

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 130 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().

00131 {
00132    struct ast_trans_pvt *p;
00133    struct ast_frame *out;
00134    p = path;
00135    /* Feed the first frame into the first translator */
00136    p->step->framein(p->state, f);
00137    if (consume)
00138       ast_frfree(f);
00139    while(p) {
00140       out = p->step->frameout(p->state);
00141       /* If we get nothing out, return NULL */
00142       if (!out)
00143          return NULL;
00144       /* If there is a next state, feed it in there.  If not,
00145          return this frame  */
00146       if (p->next) 
00147          p->next->step->framein(p->next->state, out);
00148       else
00149          return out;
00150       p = p->next;
00151    }
00152    ast_log(LOG_WARNING, "I should never get here...\n");
00153    return NULL;
00154 }

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 335 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().

00336 {
00337    /* Calculate our best source format, given costs, and a desired destination */
00338    int x,y;
00339    int best=-1;
00340    int bestdst=0;
00341    int cur = 1;
00342    int besttime=999999999;
00343    ast_mutex_lock(&list_lock);
00344    for (y=0;y<MAX_FORMAT;y++) {
00345       if ((cur & *dst) && (cur & *srcs)) {
00346          /* This is a common format to both.  Pick it if we don't have one already */
00347          besttime=0;
00348          bestdst = cur;
00349          best = cur;
00350          break;
00351       }
00352       if (cur & *dst)
00353          for (x=0;x<MAX_FORMAT;x++) {
00354             if (tr_matrix[x][y].step &&   /* There's a step */
00355                 (tr_matrix[x][y].cost < besttime) && /* We're better than what exists now */
00356                (*srcs & (1 << x)))        /* x is a valid source format */
00357                {
00358                   best = 1 << x;
00359                   bestdst = cur;
00360                   besttime = tr_matrix[x][y].cost;
00361                }
00362          }
00363       cur = cur << 1;
00364    }
00365    if (best > -1) {
00366       *srcs = best;
00367       *dst = bestdst;
00368       best = 0;
00369    }
00370    ast_mutex_unlock(&list_lock);
00371    return best;
00372 }

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 84 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().

00085 {
00086    struct ast_trans_pvt *tmpr = NULL, *tmp = NULL;
00087    /* One of the hardest parts:  Build a set of translators based upon
00088       the given source and destination formats */
00089    source = powerof(source);
00090    dest = powerof(dest);
00091    while(source != dest) {
00092       if (tr_matrix[source][dest].step) {
00093          if (tmp) {
00094             tmp->next = malloc(sizeof(struct ast_trans_pvt));
00095             tmp = tmp->next;
00096          } else
00097             tmp = malloc(sizeof(struct ast_trans_pvt));
00098 
00099             
00100          if (tmp) {
00101             tmp->next = NULL;
00102             tmp->step = tr_matrix[source][dest].step;
00103             tmp->state = tmp->step->new();
00104             if (!tmp->state) {
00105                ast_log(LOG_WARNING, "Failed to build translator step from %d to %d\n", source, dest);
00106                free(tmp);
00107                tmp = NULL;
00108                return NULL;
00109             }
00110             /* Set the root, if it doesn't exist yet... */
00111             if (!tmpr)
00112                tmpr = tmp;
00113             /* Keep going if this isn't the final destination */
00114             source = tmp->step->dstfmt;
00115          } else {
00116             /* XXX This could leak XXX */
00117             ast_log(LOG_WARNING, "Out of memory\n");
00118             return NULL;
00119          }
00120       } else {
00121          /* We shouldn't have allocated any memory */
00122          ast_log(LOG_WARNING, "No translator path from %s to %s\n", 
00123             ast_getformatname(source), ast_getformatname(dest));
00124          return NULL;
00125       }
00126    }
00127    return tmpr;
00128 }

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 71 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().

00072 {
00073    struct ast_trans_pvt *pl, *pn;
00074    pn = p;
00075    while(pn) {
00076       pl = pn;
00077       pn = pn->next;
00078       if (pl->state && pl->step->destroy)
00079          pl->step->destroy(pl->state);
00080       free(pl);
00081    }
00082 }

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 314 of file translate.c.

References ast_mutex_lock, ast_mutex_unlock, and ast_translator::next.

00315 {
00316    struct ast_translator *u, *ul = NULL;
00317    ast_mutex_lock(&list_lock);
00318    u = list;
00319    while(u) {
00320       if (u == t) {
00321          if (ul)
00322             ul->next = u->next;
00323          else
00324             list = u->next;
00325          break;
00326       }
00327       ul = u;
00328       u = u->next;
00329    }
00330    rebuild_matrix();
00331    ast_mutex_unlock(&list_lock);
00332    return (u ? 0 : -1);
00333 }


Generated on Sun Apr 18 23:34:18 2004 for Asterisk by doxygen 1.3.6-20040222