Wed Jan 8 2020 09:50:21

Asterisk developer's documentation


translate.h File Reference

Support for translation of data formats. translate.c. More...

#include "asterisk/frame.h"
#include "asterisk/plc.h"
#include "asterisk/linkedlists.h"

Go to the source code of this file.

Data Structures

struct  ast_trans_pvt
 Default structure for translators, with the basic fields and buffers, all allocated as part of the same chunk of memory. The buffer is preceded by AST_FRIENDLY_OFFSET bytes in front of the user portion. 'buf' points right after this space. More...
 
struct  ast_translator
 Descriptor of a translator. More...
 

Macros

#define ast_register_translator(t)   __ast_register_translator(t, ast_module_info->self)
 See __ast_register_translator() More...
 
#define MAX_AUDIO_FORMAT   47 /* Do not include video here */
 
#define MAX_FORMAT   64 /* Do include video here */
 

Functions

int __ast_register_translator (struct ast_translator *t, struct ast_module *module)
 Register a translator This registers a codec translator with asterisk. More...
 
struct ast_frameast_trans_frameout (struct ast_trans_pvt *pvt, int datalen, int samples)
 generic frameout function More...
 
struct ast_frameast_translate (struct ast_trans_pvt *tr, struct ast_frame *f, int consume)
 translates one or more frames Apply an input frame into the translator and receive zero or one output frames. Consume determines whether the original frame should be freed More...
 
format_t ast_translate_available_formats (format_t dest, format_t src)
 Mask off unavailable formats from a format bitmask. More...
 
unsigned int ast_translate_path_steps (format_t dest, format_t src)
 Returns the number of steps required to convert from 'src' to 'dest'. More...
 
const char * ast_translate_path_to_str (struct ast_trans_pvt *t, struct ast_str **str)
 Puts a string representation of the translation path into outbuf. More...
 
void ast_translator_activate (struct ast_translator *t)
 Activate a previously deactivated translator. More...
 
format_t ast_translator_best_choice (format_t *dsts, format_t *srcs)
 Chooses the best translation path. More...
 
struct ast_trans_pvtast_translator_build_path (format_t dest, format_t source)
 Builds a translator path Build a path (possibly NULL) from source to dest. More...
 
void ast_translator_deactivate (struct ast_translator *t)
 Deactivate a translator. More...
 
void ast_translator_free_path (struct ast_trans_pvt *tr)
 Frees a translator path Frees the given translator path structure. More...
 
int ast_unregister_translator (struct ast_translator *t)
 Unregister a translator Unregisters the given tranlator. More...
 

Detailed Description

Support for translation of data formats. translate.c.

Definition in file translate.h.

Macro Definition Documentation

#define ast_register_translator (   t)    __ast_register_translator(t, ast_module_info->self)

See __ast_register_translator()

Definition at line 170 of file translate.h.

Referenced by load_module(), and register_translator().

#define MAX_AUDIO_FORMAT   47 /* Do not include video here */

Definition at line 27 of file translate.h.

Referenced by ast_translator_best_choice().

#define MAX_FORMAT   64 /* Do include video here */

Definition at line 28 of file translate.h.

Referenced by __ast_register_translator(), and rebuild_matrix().

Function Documentation

int __ast_register_translator ( struct ast_translator t,
struct ast_module mod 
)

Register a translator This registers a codec translator with asterisk.

Parameters
tpopulated ast_translator structure
modulehandle to the module that owns this translator
Returns
0 on success, -1 on failure

Register a translator This registers a codec translator with asterisk.

Definition at line 853 of file translate.c.

References ast_translator::active, ARRAY_LEN, ast_cli_register_multiple(), ast_getformatname(), ast_log(), AST_RWLIST_INSERT_BEFORE_CURRENT, AST_RWLIST_INSERT_HEAD, AST_RWLIST_TRAVERSE_SAFE_BEGIN, AST_RWLIST_TRAVERSE_SAFE_END, AST_RWLIST_UNLOCK, AST_RWLIST_WRLOCK, ast_verb, ast_translator::buf_size, calc_cost(), COLOR_BLACK, COLOR_MAGENTA, ast_translator::cost, default_frameout(), ast_translator::dstfmt, ast_translator::frameout, LOG_WARNING, MAX_FORMAT, ast_translator::module, ast_translator::name, powerof(), rebuild_matrix(), ast_translator::srcfmt, and term_color().

854 {
855  static int added_cli = 0;
856  struct ast_translator *u;
857  char tmp[80];
858 
859  if (!mod) {
860  ast_log(LOG_WARNING, "Missing module pointer, you need to supply one\n");
861  return -1;
862  }
863 
864  if (!t->buf_size) {
865  ast_log(LOG_WARNING, "empty buf size, you need to supply one\n");
866  return -1;
867  }
868 
869  t->module = mod;
870 
871  t->srcfmt = powerof(t->srcfmt);
872  t->dstfmt = powerof(t->dstfmt);
873  t->active = 1;
874 
875  if (t->srcfmt == -1 || t->dstfmt == -1) {
876  ast_log(LOG_WARNING, "Invalid translator path: (%s codec is not valid)\n", t->srcfmt == -1 ? "starting" : "ending");
877  return -1;
878  }
879  if (t->srcfmt >= MAX_FORMAT) {
880  ast_log(LOG_WARNING, "Source format %s is larger than MAX_FORMAT\n", ast_getformatname(t->srcfmt));
881  return -1;
882  }
883 
884  if (t->dstfmt >= MAX_FORMAT) {
885  ast_log(LOG_WARNING, "Destination format %s is larger than MAX_FORMAT\n", ast_getformatname(t->dstfmt));
886  return -1;
887  }
888 
889  if (t->buf_size) {
890  /*
891  * Align buf_size properly, rounding up to the machine-specific
892  * alignment for pointers.
893  */
894  struct _test_align { void *a, *b; } p;
895  int align = (char *)&p.b - (char *)&p.a;
896 
897  t->buf_size = ((t->buf_size + align - 1) / align) * align;
898  }
899 
900  if (t->frameout == NULL)
902 
903  calc_cost(t, 1);
904 
905  ast_verb(2, "Registered translator '%s' from format %s to %s, cost %d\n",
906  term_color(tmp, t->name, COLOR_MAGENTA, COLOR_BLACK, sizeof(tmp)),
907  ast_getformatname(1LL << t->srcfmt), ast_getformatname(1LL << t->dstfmt), t->cost);
908 
909  if (!added_cli) {
911  added_cli++;
912  }
913 
915 
916  /* find any existing translators that provide this same srcfmt/dstfmt,
917  and put this one in order based on cost */
919  if ((u->srcfmt == t->srcfmt) &&
920  (u->dstfmt == t->dstfmt) &&
921  (u->cost > t->cost)) {
923  t = NULL;
924  break;
925  }
926  }
928 
929  /* if no existing translator was found for this format combination,
930  add it to the beginning of the list */
931  if (t)
933 
934  rebuild_matrix(0);
935 
937 
938  return 0;
939 }
#define ARRAY_LEN(a)
Definition: isdn_lib.c:42
static struct ast_cli_entry cli_translate[]
Definition: translate.c:848
Descriptor of a translator.
Definition: translate.h:71
#define AST_RWLIST_WRLOCK(head)
Write locks a list.
Definition: linkedlists.h:51
#define LOG_WARNING
Definition: logger.h:144
#define AST_RWLIST_UNLOCK(head)
Attempts to unlock a read/write based list.
Definition: linkedlists.h:150
static void calc_cost(struct ast_translator *t, int seconds)
compute the cost of a single translation step
Definition: translate.c:411
static struct ast_frame * default_frameout(struct ast_trans_pvt *pvt)
Definition: translate.c:265
const char name[80]
Definition: translate.h:72
#define ast_verb(level,...)
Definition: logger.h:243
struct ast_frame *(* frameout)(struct ast_trans_pvt *pvt)
Definition: translate.h:85
struct ast_module * module
Definition: translate.h:110
#define AST_RWLIST_INSERT_HEAD
Definition: linkedlists.h:703
#define AST_RWLIST_INSERT_BEFORE_CURRENT
Definition: linkedlists.h:595
int buf_size
size of outbuf, in bytes. Mandatory. The wrapper code will also allocate an AST_FRIENDLY_OFFSET space...
Definition: translate.h:105
#define AST_RWLIST_TRAVERSE_SAFE_BEGIN
Definition: linkedlists.h:542
char * term_color(char *outbuf, const char *inbuf, int fgcolor, int bgcolor, int maxout)
Definition: term.c:184
#define COLOR_BLACK
Definition: term.h:47
the list of translators
Definition: codec_dahdi.c:84
char * ast_getformatname(format_t format)
Get the name of a format.
Definition: frame.c:578
void ast_log(int level, const char *file, int line, const char *function, const char *fmt,...)
Used for sending a log message This is the standard logger function. Probably the only way you will i...
Definition: logger.c:1207
format_t dstfmt
Definition: translate.h:75
#define MAX_FORMAT
Definition: translate.h:28
static force_inline int powerof(format_t d)
returns the index of the lowest bit set
Definition: translate.c:130
int ast_cli_register_multiple(struct ast_cli_entry *e, int len)
Register multiple commands.
Definition: cli.c:2167
format_t srcfmt
Definition: translate.h:73
#define AST_RWLIST_TRAVERSE_SAFE_END
Definition: linkedlists.h:602
#define COLOR_MAGENTA
Definition: term.h:57
static void rebuild_matrix(int samples)
rebuild a translation matrix.
Definition: translate.c:515
struct ast_frame* ast_trans_frameout ( struct ast_trans_pvt pvt,
int  datalen,
int  samples 
)

generic frameout function

generic frameout function

Definition at line 235 of file translate.c.

References AST_FRAME_VOICE, AST_FRIENDLY_OFFSET, ast_frisolate(), ast_trans_pvt::c, ast_frame_subclass::codec, ast_frame::data, ast_trans_pvt::datalen, ast_frame::datalen, ast_translator::dstfmt, ast_trans_pvt::f, f, ast_frame::frametype, ast_frame::mallocd, ast_translator::name, ast_frame::offset, ast_trans_pvt::outbuf, ast_frame::ptr, ast_trans_pvt::samples, ast_frame::samples, ast_frame::src, ast_frame::subclass, and ast_trans_pvt::t.

Referenced by default_frameout(), lintoadpcm_frameout(), lintogsm_frameout(), lintoilbc_frameout(), lintolpc10_frameout(), and lintospeex_frameout().

237 {
238  struct ast_frame *f = &pvt->f;
239 
240  if (samples)
241  f->samples = samples;
242  else {
243  if (pvt->samples == 0)
244  return NULL;
245  f->samples = pvt->samples;
246  pvt->samples = 0;
247  }
248  if (datalen)
249  f->datalen = datalen;
250  else {
251  f->datalen = pvt->datalen;
252  pvt->datalen = 0;
253  }
254 
256  f->subclass.codec = 1LL << (pvt->t->dstfmt);
257  f->mallocd = 0;
259  f->src = pvt->t->name;
260  f->data.ptr = pvt->outbuf.c;
261 
262  return ast_frisolate(f);
263 }
union ast_frame_subclass subclass
Definition: frame.h:146
int datalen
actual space used in outbuf
Definition: translate.h:140
int offset
Definition: frame.h:156
union ast_trans_pvt::@213 outbuf
void * ptr
Definition: frame.h:160
struct ast_frame f
Definition: translate.h:137
const char name[80]
Definition: translate.h:72
struct ast_frame * ast_frisolate(struct ast_frame *fr)
Makes a frame independent of any static storage.
Definition: frame.c:391
format_t codec
Definition: frame.h:137
#define AST_FRIENDLY_OFFSET
Offset into a frame&#39;s data buffer.
Definition: frame.h:204
const char * src
Definition: frame.h:158
int datalen
Definition: frame.h:148
format_t dstfmt
Definition: translate.h:75
static struct ast_format f[]
Definition: format_g726.c:181
int mallocd
Definition: frame.h:152
Data structure associated with a single frame of data.
Definition: frame.h:142
struct ast_translator * t
Definition: translate.h:136
enum ast_frame_type frametype
Definition: frame.h:144
union ast_frame::@172 data
int samples
Definition: frame.h:150
struct ast_frame* ast_translate ( struct ast_trans_pvt path,
struct ast_frame f,
int  consume 
)

translates one or more frames Apply an input frame into the translator and receive zero or one output frames. Consume determines whether the original frame should be freed

Parameters
trtranslator structure to use for translation
fframe to translate
consumeWhether or not to free the original frame
Returns
an ast_frame of the new translation format on success, NULL on failure

translates one or more frames Apply an input frame into the translator and receive zero or one output frames. Consume determines whether the original frame should be freed

Definition at line 328 of file translate.c.

References ast_clear_flag, ast_debug, ast_format_rate(), AST_FRAME_CNG, AST_FRFLAG_HAS_TIMING_INFO, ast_frfree, ast_samp2tv(), ast_set2_flag, ast_test_flag, ast_tv(), ast_tvadd(), ast_tveq(), ast_tvnow(), ast_tvsub(), ast_tvzero(), ast_frame_subclass::codec, ast_frame::delivery, f, framein(), ast_frame::frametype, ast_frame::len, len(), ast_trans_pvt::next, ast_trans_pvt::nextin, ast_trans_pvt::nextout, ast_frame::samples, ast_frame::seqno, ast_frame::subclass, and ast_frame::ts.

Referenced by __ast_read(), ast_audiohook_read_frame(), ast_slinfactory_feed(), ast_write(), ast_writestream(), audio_audiohook_write_list(), and conf_run().

329 {
330  struct ast_trans_pvt *p = path;
331  struct ast_frame *out = f;
332  struct timeval delivery;
333  int has_timing_info;
334  long ts;
335  long len;
336  int seqno;
337 
338  has_timing_info = ast_test_flag(f, AST_FRFLAG_HAS_TIMING_INFO);
339  ts = f->ts;
340  len = f->len;
341  seqno = f->seqno;
342 
343  /* XXX hmmm... check this below */
344  if (!ast_tvzero(f->delivery)) {
345  if (!ast_tvzero(path->nextin)) {
346  /* Make sure this is in line with what we were expecting */
347  if (!ast_tveq(path->nextin, f->delivery)) {
348  /* The time has changed between what we expected and this
349  most recent time on the new packet. If we have a
350  valid prediction adjust our output time appropriately */
351  if (!ast_tvzero(path->nextout)) {
352  path->nextout = ast_tvadd(path->nextout,
353  ast_tvsub(f->delivery, path->nextin));
354  }
355  path->nextin = f->delivery;
356  }
357  } else {
358  /* This is our first pass. Make sure the timing looks good */
359  path->nextin = f->delivery;
360  path->nextout = f->delivery;
361  }
362  /* Predict next incoming sample */
364  }
365  delivery = f->delivery;
366  for ( ; out && p ; p = p->next) {
367  framein(p, out);
368  if (out != f)
369  ast_frfree(out);
370  out = p->t->frameout(p);
371  }
372  if (out) {
373  /* we have a frame, play with times */
374  if (!ast_tvzero(delivery)) {
375  /* Regenerate prediction after a discontinuity */
376  if (ast_tvzero(path->nextout)) {
377  path->nextout = ast_tvnow();
378  }
379 
380  /* Use next predicted outgoing timestamp */
381  out->delivery = path->nextout;
382 
383  /* Predict next outgoing timestamp from samples in this
384  frame. */
386  if (f->samples != out->samples && ast_test_flag(out, AST_FRFLAG_HAS_TIMING_INFO)) {
387  ast_debug(4, "Sample size different %d vs %d\n", f->samples, out->samples);
389  }
390  } else {
391  out->delivery = ast_tv(0, 0);
392  ast_set2_flag(out, has_timing_info, AST_FRFLAG_HAS_TIMING_INFO);
393  if (has_timing_info) {
394  out->ts = ts;
395  out->len = len;
396  out->seqno = seqno;
397  }
398  }
399  /* Invalidate prediction if we're entering a silence period */
400  if (out->frametype == AST_FRAME_CNG) {
401  path->nextout = ast_tv(0, 0);
402  }
403  }
404  if (consume) {
405  ast_frfree(f);
406  }
407  return out;
408 }
union ast_frame_subclass subclass
Definition: frame.h:146
int seqno
Definition: frame.h:172
#define ast_set2_flag(p, value, flag)
Definition: utils.h:94
#define ast_test_flag(p, flag)
Definition: utils.h:63
int ast_tveq(struct timeval _a, struct timeval _b)
Returns true if the two struct timeval arguments are equal.
Definition: time.h:130
int ast_tvzero(const struct timeval t)
Returns true if the argument is 0,0.
Definition: time.h:100
long ts
Definition: frame.h:168
struct timeval ast_tvnow(void)
Returns current timeval. Meant to replace calls to gettimeofday().
Definition: time.h:142
static force_inline int ast_format_rate(format_t format)
Get the sample rate for a given format.
Definition: frame.h:809
struct ast_trans_pvt * next
Definition: translate.h:149
format_t codec
Definition: frame.h:137
struct timeval nextin
Definition: translate.h:150
static int framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
framein wrapper, deals with bound checks.
Definition: translate.c:194
#define ast_debug(level,...)
Log a DEBUG message.
Definition: logger.h:236
struct timeval ast_samp2tv(unsigned int _nsamp, unsigned int _rate)
Returns a timeval corresponding to the duration of n samples at rate r. Useful to convert samples to ...
Definition: time.h:191
Default structure for translators, with the basic fields and buffers, all allocated as part of the sa...
Definition: translate.h:135
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
struct timeval nextout
Definition: translate.h:151
struct timeval ast_tvadd(struct timeval a, struct timeval b)
Returns the sum of two timevals a + b.
Definition: utils.c:1587
static struct ast_format f[]
Definition: format_g726.c:181
#define ast_clear_flag(p, flag)
Definition: utils.h:77
struct timeval delivery
Definition: frame.h:162
struct timeval ast_tv(ast_time_t sec, ast_suseconds_t usec)
Returns a timeval from sec, usec.
Definition: time.h:179
Data structure associated with a single frame of data.
Definition: frame.h:142
struct timeval ast_tvsub(struct timeval a, struct timeval b)
Returns the difference of two timevals a - b.
Definition: utils.c:1601
enum ast_frame_type frametype
Definition: frame.h:144
#define ast_frfree(fr)
Definition: frame.h:583
long len
Definition: frame.h:170
int samples
Definition: frame.h:150
format_t ast_translate_available_formats ( format_t  dest,
format_t  src 
)

Mask off unavailable formats from a format bitmask.

Parameters
destpossible destination formats
srcsource formats
Returns
the destination formats that are available in the source or translatable

The result will include all formats from 'dest' that are either present in 'src' or translatable from a format present in 'src'.

Note
Only a single audio format and a single video format can be present in 'src', or the function will produce unexpected results.

Definition at line 1081 of file translate.c.

References AST_FORMAT_AUDIO_MASK, AST_FORMAT_VIDEO_MASK, AST_RWLIST_RDLOCK, AST_RWLIST_UNLOCK, powerof(), and tr_matrix.

Referenced by ast_rtp_instance_available_formats().

1082 {
1083  format_t res = dest;
1084  format_t x;
1085  format_t src_audio = src & AST_FORMAT_AUDIO_MASK;
1086  format_t src_video = src & AST_FORMAT_VIDEO_MASK;
1087  format_t x_bits;
1088 
1089  /* if we don't have a source format, we just have to try all
1090  possible destination formats */
1091  if (!src)
1092  return dest;
1093 
1094  /* If we have a source audio format, get its format index */
1095  if (src_audio) {
1096  src_audio = powerof(src_audio);
1097  }
1098 
1099  /* If we have a source video format, get its format index */
1100  if (src_video) {
1101  src_video = powerof(src_video);
1102  }
1103 
1104  /* Note that src_audio and src_video are guaranteed to not be
1105  * negative at this point, as we ensured they were non-zero. It is
1106  * safe to use the return value of powerof as an index into tr_matrix.
1107  */
1108 
1110 
1111  /* For a given source audio format, traverse the list of
1112  known audio formats to determine whether there exists
1113  a translation path from the source format to the
1114  destination format. */
1115  for (x = 1LL; src_audio && x > 0; x <<= 1) {
1116  if (!(x & AST_FORMAT_AUDIO_MASK)) {
1117  continue;
1118  }
1119 
1120  /* if this is not a desired format, nothing to do */
1121  if (!(dest & x))
1122  continue;
1123 
1124  /* if the source is supplying this format, then
1125  we can leave it in the result */
1126  if (src & x)
1127  continue;
1128 
1129  /* if we don't have a translation path from the src
1130  to this format, remove it from the result. Note that x_bits
1131  cannot be less than 0 as x will always have one bit set to 1 */
1132  x_bits = powerof(x);
1133  if (!tr_matrix[src_audio][x_bits].step) {
1134  res &= ~x;
1135  continue;
1136  }
1137 
1138  /* now check the opposite direction */
1139  if (!tr_matrix[x_bits][src_audio].step)
1140  res &= ~x;
1141  }
1142 
1143  /* For a given source video format, traverse the list of
1144  known video formats to determine whether there exists
1145  a translation path from the source format to the
1146  destination format. */
1147  for (x = 1LL; src_video && x > 0; x <<= 1) {
1148  if (!(x & AST_FORMAT_VIDEO_MASK)) {
1149  continue;
1150  }
1151 
1152  /* if this is not a desired format, nothing to do */
1153  if (!(dest & x))
1154  continue;
1155 
1156  /* if the source is supplying this format, then
1157  we can leave it in the result */
1158  if (src & x)
1159  continue;
1160 
1161  /* if we don't have a translation path from the src
1162  to this format, remove it from the result. Note that x_bits
1163  cannot be less than 0 as x will always have one bit set to 1 */
1164  x_bits = powerof(x);
1165  if (!tr_matrix[src_video][x_bits].step) {
1166  res &= ~x;
1167  continue;
1168  }
1169 
1170  /* now check the opposite direction */
1171  if (!tr_matrix[x_bits][src_video].step)
1172  res &= ~x;
1173  }
1174 
1176 
1177  return res;
1178 }
#define AST_RWLIST_UNLOCK(head)
Attempts to unlock a read/write based list.
Definition: linkedlists.h:150
static struct translator_path tr_matrix[MAX_FORMAT][MAX_FORMAT]
a matrix that, for any pair of supported formats, indicates the total cost of translation and the fir...
Definition: translate.c:121
#define AST_RWLIST_RDLOCK(head)
Read locks a list.
Definition: linkedlists.h:77
the list of translators
Definition: codec_dahdi.c:84
int64_t format_t
Definition: frame_defs.h:32
#define AST_FORMAT_VIDEO_MASK
Definition: frame.h:290
#define AST_FORMAT_AUDIO_MASK
Definition: frame.h:274
static force_inline int powerof(format_t d)
returns the index of the lowest bit set
Definition: translate.c:130
unsigned int ast_translate_path_steps ( format_t  dest,
format_t  src 
)

Returns the number of steps required to convert from 'src' to 'dest'.

Parameters
destdestination format
srcsource format
Returns
the number of translation steps required, or -1 if no path is available

Definition at line 1059 of file translate.c.

References ast_log(), AST_RWLIST_RDLOCK, AST_RWLIST_UNLOCK, LOG_WARNING, translator_path::multistep, powerof(), and tr_matrix.

Referenced by ast_channel_make_compatible_helper().

1060 {
1061  unsigned int res = -1;
1062 
1063  /* convert bitwise format numbers into array indices */
1064  src = powerof(src);
1065  dest = powerof(dest);
1066 
1067  if (src == -1 || dest == -1) {
1068  ast_log(LOG_WARNING, "No translator path: (%s codec is not valid)\n", src == -1 ? "starting" : "ending");
1069  return -1;
1070  }
1072 
1073  if (tr_matrix[src][dest].step)
1074  res = tr_matrix[src][dest].multistep + 1;
1075 
1077 
1078  return res;
1079 }
unsigned int multistep
Definition: translate.c:107
#define LOG_WARNING
Definition: logger.h:144
#define AST_RWLIST_UNLOCK(head)
Attempts to unlock a read/write based list.
Definition: linkedlists.h:150
static struct translator_path tr_matrix[MAX_FORMAT][MAX_FORMAT]
a matrix that, for any pair of supported formats, indicates the total cost of translation and the fir...
Definition: translate.c:121
#define AST_RWLIST_RDLOCK(head)
Read locks a list.
Definition: linkedlists.h:77
the list of translators
Definition: codec_dahdi.c:84
void ast_log(int level, const char *file, int line, const char *function, const char *fmt,...)
Used for sending a log message This is the standard logger function. Probably the only way you will i...
Definition: logger.c:1207
static force_inline int powerof(format_t d)
returns the index of the lowest bit set
Definition: translate.c:130
const char* ast_translate_path_to_str ( struct ast_trans_pvt t,
struct ast_str **  str 
)

Puts a string representation of the translation path into outbuf.

Parameters
translatorstructure containing the translation path
ast_stroutput buffer
Return values
onsuccess pointer to beginning of outbuf. on failure "".

Definition at line 630 of file translate.c.

References ast_getformatname(), ast_str_append(), ast_str_buffer(), ast_str_set(), ast_translator::dstfmt, ast_trans_pvt::next, ast_translator::srcfmt, and ast_trans_pvt::t.

Referenced by handle_showchan(), and serialize_showchan().

631 {
632  struct ast_trans_pvt *pn = p;
633 
634  if (!p || !p->t) {
635  return "";
636  }
637 
638  ast_str_set(str, 0, "%s", ast_getformatname(1LL << p->t->srcfmt));
639 
640  while ( (p = pn) ) {
641  pn = p->next;
642  ast_str_append(str, 0, "->%s", ast_getformatname(1LL << p->t->dstfmt));
643  }
644 
645  return ast_str_buffer(*str);
646 }
char * ast_str_buffer(const struct ast_str *buf)
Returns the string buffer within the ast_str buf.
Definition: strings.h:497
int ast_str_append(struct ast_str **buf, ssize_t max_len, const char *fmt,...)
Append to a thread local dynamic string.
Definition: strings.h:900
struct ast_trans_pvt * next
Definition: translate.h:149
int ast_str_set(struct ast_str **buf, ssize_t max_len, const char *fmt,...)
Set a dynamic string using variable arguments.
Definition: strings.h:874
char * ast_getformatname(format_t format)
Get the name of a format.
Definition: frame.c:578
Default structure for translators, with the basic fields and buffers, all allocated as part of the sa...
Definition: translate.h:135
void ast_translator_activate ( struct ast_translator t)

Activate a previously deactivated translator.

Parameters
ttranslator to activate
Returns
nothing

Enables the specified translator for use.

Definition at line 967 of file translate.c.

References ast_translator::active, AST_RWLIST_UNLOCK, AST_RWLIST_WRLOCK, and rebuild_matrix().

968 {
970  t->active = 1;
971  rebuild_matrix(0);
973 }
#define AST_RWLIST_WRLOCK(head)
Write locks a list.
Definition: linkedlists.h:51
#define AST_RWLIST_UNLOCK(head)
Attempts to unlock a read/write based list.
Definition: linkedlists.h:150
the list of translators
Definition: codec_dahdi.c:84
static void rebuild_matrix(int samples)
rebuild a translation matrix.
Definition: translate.c:515
format_t ast_translator_best_choice ( format_t dst,
format_t srcs 
)

Chooses the best translation path.

Given a list of sources, and a designed destination format, which should I choose?

Returns
Returns 0 on success, -1 if no path could be found.
Note
Modifies dests and srcs in place

Chooses the best translation path.

Definition at line 984 of file translate.c.

References AST_FORMAT_AUDIO_MASK, ast_format_rate(), AST_RWLIST_RDLOCK, AST_RWLIST_UNLOCK, translator_path::cost, ast_translator::cost, MAX_AUDIO_FORMAT, translator_path::multistep, translator_path::rate_change, and tr_matrix.

Referenced by ast_channel_make_compatible_helper(), ast_request(), iax2_request(), and set_format().

985 {
986  int x,y;
987  int better = 0;
988  int besttime = INT_MAX;
989  int beststeps = INT_MAX;
990  unsigned int best_rate_change = INT_MAX;
991  format_t best = -1;
992  format_t bestdst = 0;
993  format_t cur, cursrc;
994  format_t common = ((*dst) & (*srcs)) & AST_FORMAT_AUDIO_MASK; /* are there common formats ? */
995 
996  if (common) { /* yes, pick one and return */
997  for (cur = 1, y = 0; y <= MAX_AUDIO_FORMAT; cur <<= 1, y++) {
998  if (!(cur & common)) {
999  continue;
1000  }
1001 
1002  /* We are guaranteed to find one common format. */
1003  if (best == -1) {
1004  best = cur;
1005  continue;
1006  }
1007  /* If there are multiple common formats, pick the one with the highest sample rate */
1008  if (ast_format_rate(best) < ast_format_rate(cur)) {
1009  best = cur;
1010  continue;
1011  }
1012  }
1013  /* We are done, this is a common format to both. */
1014  *srcs = *dst = best;
1015  return 0;
1016  } else { /* No, we will need to translate */
1018  for (cur = 1, y = 0; y <= MAX_AUDIO_FORMAT; cur <<= 1, y++) {
1019  if (! (cur & *dst)) {
1020  continue;
1021  }
1022  for (cursrc = 1, x = 0; x <= MAX_AUDIO_FORMAT; cursrc <<= 1, x++) {
1023  if (!(*srcs & cursrc) || !tr_matrix[x][y].step) {
1024  continue;
1025  }
1026 
1027  /* This is a better choice if any of the following are true.
1028  * 1. The sample rate conversion is better than the current pick.
1029  * 2. the sample rate conversion is no worse than the current pick and the cost or multistep is better
1030  */
1031  better = 0;
1032  if (tr_matrix[x][y].rate_change < best_rate_change) {
1033  better = 1; /* this match has a better rate conversion */
1034  }
1035  if ((tr_matrix[x][y].rate_change <= best_rate_change) &&
1036  (tr_matrix[x][y].cost < besttime || tr_matrix[x][y].multistep < beststeps)) {
1037  better = 1; /* this match has no worse rate conversion and the conversion cost is less */
1038  }
1039  if (better) {
1040  /* better than what we have so far */
1041  best = cursrc;
1042  bestdst = cur;
1043  besttime = tr_matrix[x][y].cost;
1044  beststeps = tr_matrix[x][y].multistep;
1045  best_rate_change = tr_matrix[x][y].rate_change;
1046  }
1047  }
1048  }
1050  if (best > -1) {
1051  *srcs = best;
1052  *dst = bestdst;
1053  best = 0;
1054  }
1055  return best;
1056  }
1057 }
unsigned int cost
Definition: translate.c:106
#define MAX_AUDIO_FORMAT
Definition: translate.h:27
enum path_samp_change rate_change
Definition: translate.c:108
unsigned int multistep
Definition: translate.c:107
#define AST_RWLIST_UNLOCK(head)
Attempts to unlock a read/write based list.
Definition: linkedlists.h:150
static force_inline int ast_format_rate(format_t format)
Get the sample rate for a given format.
Definition: frame.h:809
static struct translator_path tr_matrix[MAX_FORMAT][MAX_FORMAT]
a matrix that, for any pair of supported formats, indicates the total cost of translation and the fir...
Definition: translate.c:121
#define AST_RWLIST_RDLOCK(head)
Read locks a list.
Definition: linkedlists.h:77
the list of translators
Definition: codec_dahdi.c:84
int64_t format_t
Definition: frame_defs.h:32
#define AST_FORMAT_AUDIO_MASK
Definition: frame.h:274
struct ast_trans_pvt* ast_translator_build_path ( format_t  dest,
format_t  source 
)

Builds a translator path Build a path (possibly NULL) from source to dest.

Parameters
destdestination format
sourcesource format
Returns
ast_trans_pvt on success, NULL on failure

Builds a translator path Build a path (possibly NULL) from source to dest.

Definition at line 282 of file translate.c.

References ast_getformatname(), ast_log(), AST_RWLIST_RDLOCK, AST_RWLIST_UNLOCK, ast_translator_free_path(), ast_tv(), ast_translator::dstfmt, LOG_WARNING, newpvt(), ast_trans_pvt::next, ast_trans_pvt::nextin, ast_trans_pvt::nextout, powerof(), translator_path::step, ast_trans_pvt::t, and tr_matrix.

Referenced by ast_audiohook_read_frame(), ast_slinfactory_feed(), ast_writestream(), audio_audiohook_write_list(), conf_run(), and set_format().

283 {
284  struct ast_trans_pvt *head = NULL, *tail = NULL;
285 
286  source = powerof(source);
287  dest = powerof(dest);
288 
289  if (source == -1 || dest == -1) {
290  ast_log(LOG_WARNING, "No translator path: (%s codec is not valid)\n", source == -1 ? "starting" : "ending");
291  return NULL;
292  }
293 
295 
296  while (source != dest) {
297  struct ast_trans_pvt *cur;
298  struct ast_translator *t = tr_matrix[source][dest].step;
299  if (!t) {
300  ast_log(LOG_WARNING, "No translator path from %s to %s\n",
301  ast_getformatname(source), ast_getformatname(dest));
303  return NULL;
304  }
305  if (!(cur = newpvt(t))) {
306  ast_log(LOG_WARNING, "Failed to build translator step from %s to %s\n",
307  ast_getformatname(source), ast_getformatname(dest));
308  if (head)
311  return NULL;
312  }
313  if (!head)
314  head = cur;
315  else
316  tail->next = cur;
317  tail = cur;
318  cur->nextin = cur->nextout = ast_tv(0, 0);
319  /* Keep going if this isn't the final destination */
320  source = cur->t->dstfmt;
321  }
322 
324  return head;
325 }
static void * newpvt(struct ast_translator *t)
Allocate the descriptor, required outbuf space, and possibly desc.
Definition: translate.c:150
Descriptor of a translator.
Definition: translate.h:71
#define LOG_WARNING
Definition: logger.h:144
#define AST_RWLIST_UNLOCK(head)
Attempts to unlock a read/write based list.
Definition: linkedlists.h:150
struct ast_trans_pvt * next
Definition: translate.h:149
struct ast_translator * step
Definition: translate.c:105
struct timeval nextin
Definition: translate.h:150
static struct translator_path tr_matrix[MAX_FORMAT][MAX_FORMAT]
a matrix that, for any pair of supported formats, indicates the total cost of translation and the fir...
Definition: translate.c:121
#define AST_RWLIST_RDLOCK(head)
Read locks a list.
Definition: linkedlists.h:77
the list of translators
Definition: codec_dahdi.c:84
char * ast_getformatname(format_t format)
Get the name of a format.
Definition: frame.c:578
Default structure for translators, with the basic fields and buffers, all allocated as part of the sa...
Definition: translate.h:135
struct timeval nextout
Definition: translate.h:151
void ast_log(int level, const char *file, int line, const char *function, const char *fmt,...)
Used for sending a log message This is the standard logger function. Probably the only way you will i...
Definition: logger.c:1207
format_t dstfmt
Definition: translate.h:75
static force_inline int powerof(format_t d)
returns the index of the lowest bit set
Definition: translate.c:130
struct timeval ast_tv(ast_time_t sec, ast_suseconds_t usec)
Returns a timeval from sec, usec.
Definition: time.h:179
struct ast_translator * t
Definition: translate.h:136
void ast_translator_free_path(struct ast_trans_pvt *tr)
Frees a translator path Frees the given translator path structure.
Definition: translate.c:272
void ast_translator_deactivate ( struct ast_translator t)

Deactivate a translator.

Parameters
ttranslator to deactivate
Returns
nothing

Disables the specified translator from being used.

Definition at line 975 of file translate.c.

References ast_translator::active, AST_RWLIST_UNLOCK, AST_RWLIST_WRLOCK, and rebuild_matrix().

976 {
978  t->active = 0;
979  rebuild_matrix(0);
981 }
#define AST_RWLIST_WRLOCK(head)
Write locks a list.
Definition: linkedlists.h:51
#define AST_RWLIST_UNLOCK(head)
Attempts to unlock a read/write based list.
Definition: linkedlists.h:150
the list of translators
Definition: codec_dahdi.c:84
static void rebuild_matrix(int samples)
rebuild a translation matrix.
Definition: translate.c:515
void ast_translator_free_path ( struct ast_trans_pvt tr)

Frees a translator path Frees the given translator path structure.

Parameters
trtranslator path to get rid of

Definition at line 272 of file translate.c.

References destroy(), and ast_trans_pvt::next.

Referenced by ast_audiohook_destroy(), ast_audiohook_detach_list(), ast_audiohook_read_frame(), ast_channel_destructor(), ast_slinfactory_destroy(), ast_slinfactory_feed(), ast_slinfactory_flush(), ast_translator_build_path(), ast_writestream(), audio_audiohook_write_list(), conf_free(), filestream_destructor(), free_translation(), and set_format().

273 {
274  struct ast_trans_pvt *pn = p;
275  while ( (p = pn) ) {
276  pn = p->next;
277  destroy(p);
278  }
279 }
struct ast_trans_pvt * next
Definition: translate.h:149
Default structure for translators, with the basic fields and buffers, all allocated as part of the sa...
Definition: translate.h:135
static void destroy(struct ast_trans_pvt *pvt)
Definition: translate.c:183
int ast_unregister_translator ( struct ast_translator t)

Unregister a translator Unregisters the given tranlator.

Parameters
ttranslator to unregister
Returns
0 on success, -1 on failure

Unregister a translator Unregisters the given tranlator.

Definition at line 942 of file translate.c.

References ast_getformatname(), AST_RWLIST_REMOVE_CURRENT, AST_RWLIST_TRAVERSE_SAFE_BEGIN, AST_RWLIST_TRAVERSE_SAFE_END, AST_RWLIST_UNLOCK, AST_RWLIST_WRLOCK, ast_verb, COLOR_BLACK, COLOR_MAGENTA, ast_translator::dstfmt, ast_translator::list, ast_translator::name, rebuild_matrix(), ast_translator::srcfmt, and term_color().

Referenced by drop_translator(), load_module(), unload_module(), and unregister_translators().

943 {
944  char tmp[80];
945  struct ast_translator *u;
946  int found = 0;
947 
950  if (u == t) {
952  ast_verb(2, "Unregistered translator '%s' from format %s to %s\n", term_color(tmp, t->name, COLOR_MAGENTA, COLOR_BLACK, sizeof(tmp)), ast_getformatname(1LL << t->srcfmt), ast_getformatname(1LL << t->dstfmt));
953  found = 1;
954  break;
955  }
956  }
958 
959  if (found)
960  rebuild_matrix(0);
961 
963 
964  return (u ? 0 : -1);
965 }
Descriptor of a translator.
Definition: translate.h:71
#define AST_RWLIST_WRLOCK(head)
Write locks a list.
Definition: linkedlists.h:51
#define AST_RWLIST_UNLOCK(head)
Attempts to unlock a read/write based list.
Definition: linkedlists.h:150
const char name[80]
Definition: translate.h:72
#define ast_verb(level,...)
Definition: logger.h:243
#define AST_RWLIST_REMOVE_CURRENT
Definition: linkedlists.h:565
#define AST_RWLIST_TRAVERSE_SAFE_BEGIN
Definition: linkedlists.h:542
char * term_color(char *outbuf, const char *inbuf, int fgcolor, int bgcolor, int maxout)
Definition: term.c:184
#define COLOR_BLACK
Definition: term.h:47
the list of translators
Definition: codec_dahdi.c:84
char * ast_getformatname(format_t format)
Get the name of a format.
Definition: frame.c:578
format_t dstfmt
Definition: translate.h:75
format_t srcfmt
Definition: translate.h:73
struct ast_translator::@212 list
#define AST_RWLIST_TRAVERSE_SAFE_END
Definition: linkedlists.h:602
#define COLOR_MAGENTA
Definition: term.h:57
static void rebuild_matrix(int samples)
rebuild a translation matrix.
Definition: translate.c:515