Asterisk - The Open Source Telephony Project GIT-master-590b490
Loading...
Searching...
No Matches
Data Structures | Macros | Enumerations | Functions
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"
#include "asterisk/format_cap.h"
#include "asterisk/format_cache.h"
Include dependency graph for translate.h:
This graph shows which files directly or indirectly include this file:

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_SELF)
 See __ast_register_translator()
 

Enumerations

enum  ast_trans_cost_table {
  AST_TRANS_COST_LL_LL_ORIGSAMP = 400000 , AST_TRANS_COST_LL_LY_ORIGSAMP = 600000 , AST_TRANS_COST_LL_LL_UPSAMP = 800000 , AST_TRANS_COST_LL_LY_UPSAMP = 825000 ,
  AST_TRANS_COST_LL_LL_DOWNSAMP = 850000 , AST_TRANS_COST_LL_LY_DOWNSAMP = 875000 , AST_TRANS_COST_LL_UNKNOWN = 885000 , AST_TRANS_COST_LY_LL_ORIGSAMP = 900000 ,
  AST_TRANS_COST_LY_LY_ORIGSAMP = 915000 , AST_TRANS_COST_LY_LL_UPSAMP = 930000 , AST_TRANS_COST_LY_LY_UPSAMP = 945000 , AST_TRANS_COST_LY_LL_DOWNSAMP = 960000 ,
  AST_TRANS_COST_LY_LY_DOWNSAMP = 975000 , AST_TRANS_COST_LY_UNKNOWN = 985000
}
 Translator Cost Table definition. More...
 

Functions

int __ast_register_translator (struct ast_translator *t, struct ast_module *module)
 Register a translator This registers a codec translator with asterisk.
 
struct ast_frameast_trans_frameout (struct ast_trans_pvt *pvt, int datalen, int samples)
 generic frameout function
 
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. In case the frame type is AST_FRAME_RTCP, the frame is not translated but passed to the translator codecs via the feedback callback, and a pointer to ast_null_frame is returned after that.
 
void ast_translate_available_formats (struct ast_format_cap *dest, struct ast_format_cap *src, struct ast_format_cap *result)
 Find available formats.
 
int ast_translate_init (void)
 Initialize the translation matrix and index to format conversion table.
 
unsigned int ast_translate_path_steps (struct ast_format *dest, struct ast_format *src)
 Returns the number of steps required to convert from 'src' to 'dest'.
 
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.
 
void ast_translator_activate (struct ast_translator *t)
 Activate a previously deactivated translator.
 
int ast_translator_best_choice (struct ast_format_cap *dst_cap, struct ast_format_cap *src_cap, struct ast_format **dst_fmt_out, struct ast_format **src_fmt_out)
 Chooses the best translation path.
 
struct ast_trans_pvtast_translator_build_path (struct ast_format *dest, struct ast_format *source)
 Builds a translator path Build a path (possibly NULL) from source to dest.
 
void ast_translator_deactivate (struct ast_translator *t)
 Deactivate a translator.
 
void ast_translator_free_path (struct ast_trans_pvt *tr)
 Frees a translator path Frees the given translator path structure.
 
int ast_unregister_translator (struct ast_translator *t)
 Unregister a translator Unregisters the given translator.
 

Detailed Description

Support for translation of data formats. translate.c.

Definition in file translate.h.

Macro Definition Documentation

◆ ast_register_translator

#define ast_register_translator (   t)    __ast_register_translator(t, AST_MODULE_SELF)

See __ast_register_translator()

Definition at line 258 of file translate.h.

Enumeration Type Documentation

◆ ast_trans_cost_table

Translator Cost Table definition.

Note
The defined values in this table must be used to set the translator's table_cost value.
The cost value of the first two values must always add up to be greater than the largest value defined in this table. This is done to guarantee a direct translation will always have precedence over a multi step translation.

This table is built in a way that allows translation paths to be built that guarantee the best possible balance between performance and quality. With this table direct translation paths between two formats will always take precedence over multi step paths, lossless intermediate steps will always be chosen over lossy intermediate steps, and preservation of sample rate across the translation will always have precedence over a path that involves any re-sampling.

Enumerator
AST_TRANS_COST_LL_LL_ORIGSAMP 

[lossless -> lossless] original sampling

AST_TRANS_COST_LL_LY_ORIGSAMP 

[lossless -> lossy] original sampling

AST_TRANS_COST_LL_LL_UPSAMP 

[lossless -> lossless] up sample

AST_TRANS_COST_LL_LY_UPSAMP 

[lossless -> lossy] up sample

AST_TRANS_COST_LL_LL_DOWNSAMP 

[lossless -> lossless] down sample

AST_TRANS_COST_LL_LY_DOWNSAMP 

[lossless -> lossy] down sample

AST_TRANS_COST_LL_UNKNOWN 

[lossless -> unknown] unknown. This value is for a lossless source translation with an unknown destination and or sample rate conversion.

AST_TRANS_COST_LY_LL_ORIGSAMP 

[lossy -> lossless] original sampling

AST_TRANS_COST_LY_LY_ORIGSAMP 

[lossy -> lossy] original sampling

AST_TRANS_COST_LY_LL_UPSAMP 

[lossy -> lossless] up sample

AST_TRANS_COST_LY_LY_UPSAMP 

[lossy -> lossy] up sample

AST_TRANS_COST_LY_LL_DOWNSAMP 

[lossy -> lossless] down sample

AST_TRANS_COST_LY_LY_DOWNSAMP 

[lossy -> lossy] down sample

AST_TRANS_COST_LY_UNKNOWN 

[lossy -> unknown] unknown. This value is for a lossy source translation with an unknown destination and or sample rate conversion.

Definition at line 61 of file translate.h.

61 {
62
63 /* Lossless Source Translation Costs */
64
65 /*! [lossless -> lossless] original sampling */
67 /*! [lossless -> lossy] original sampling */
69
70 /*! [lossless -> lossless] up sample */
72 /*! [lossless -> lossy] up sample */
74
75 /*! [lossless -> lossless] down sample */
77 /*! [lossless -> lossy] down sample */
79
80 /*! [lossless -> unknown] unknown.
81 * This value is for a lossless source translation
82 * with an unknown destination and or sample rate conversion. */
84
85 /* Lossy Source Translation Costs */
86
87 /*! [lossy -> lossless] original sampling */
89 /*! [lossy -> lossy] original sampling */
91
92 /*! [lossy -> lossless] up sample */
94 /*! [lossy -> lossy] up sample */
96
97 /*! [lossy -> lossless] down sample */
99 /*! [lossy -> lossy] down sample */
101
102 /*! [lossy -> unknown] unknown.
103 * This value is for a lossy source translation
104 * with an unknown destination and or sample rate conversion. */
106
107};
@ AST_TRANS_COST_LY_LY_DOWNSAMP
Definition translate.h:100
@ AST_TRANS_COST_LL_LL_DOWNSAMP
Definition translate.h:76
@ AST_TRANS_COST_LY_LY_ORIGSAMP
Definition translate.h:90
@ AST_TRANS_COST_LL_LL_UPSAMP
Definition translate.h:71
@ AST_TRANS_COST_LY_LL_ORIGSAMP
Definition translate.h:88
@ AST_TRANS_COST_LL_UNKNOWN
Definition translate.h:83
@ AST_TRANS_COST_LY_LL_DOWNSAMP
Definition translate.h:98
@ AST_TRANS_COST_LL_LY_DOWNSAMP
Definition translate.h:78
@ AST_TRANS_COST_LL_LY_ORIGSAMP
Definition translate.h:68
@ AST_TRANS_COST_LY_UNKNOWN
Definition translate.h:105
@ AST_TRANS_COST_LL_LY_UPSAMP
Definition translate.h:73
@ AST_TRANS_COST_LY_LY_UPSAMP
Definition translate.h:95
@ AST_TRANS_COST_LY_LL_UPSAMP
Definition translate.h:93
@ AST_TRANS_COST_LL_LL_ORIGSAMP
Definition translate.h:66

Function Documentation

◆ __ast_register_translator()

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
modmodule handle to the module that owns this translator
Return values
0on success
-1on failure

Register a translator This registers a codec translator with asterisk.

Definition at line 1511 of file translate.c.

1512{
1513 struct ast_translator *u;
1514 char tmp[80];
1517
1519 if (!src_codec) {
1520 ast_assert(0);
1521 ast_log(LOG_WARNING, "Failed to register translator: unknown source codec %s\n", t->src_codec.name);
1522 return -1;
1523 }
1524
1526 if (!dst_codec) {
1527 ast_log(LOG_WARNING, "Failed to register translator: unknown destination codec %s\n", t->dst_codec.name);
1528 return -1;
1529 }
1530
1532 if (matrix_resize(0)) {
1533 ast_log(LOG_WARNING, "Translator matrix can not represent any more translators. Out of resources.\n");
1534 return -1;
1535 }
1538 }
1539
1540 if (!mod) {
1541 ast_log(LOG_WARNING, "Missing module pointer, you need to supply one\n");
1542 return -1;
1543 }
1544
1545 if (!t->buf_size) {
1546 ast_log(LOG_WARNING, "empty buf size, you need to supply one\n");
1547 return -1;
1548 }
1550 ast_log(LOG_WARNING, "Table cost could not be generated for %s, "
1551 "Please set table_cost variable on translator.\n", t->name);
1552 return -1;
1553 }
1554
1555 t->module = mod;
1558 t->active = 1;
1559
1560 if (t->src_fmt_index < 0 || t->dst_fmt_index < 0) {
1561 ast_log(LOG_WARNING, "Invalid translator path: (%s codec is not valid)\n", t->src_fmt_index < 0 ? "starting" : "ending");
1562 return -1;
1563 }
1564 if (t->src_fmt_index >= cur_max_index) {
1565 ast_log(LOG_WARNING, "Source codec %s is larger than cur_max_index\n", t->src_codec.name);
1566 return -1;
1567 }
1568
1569 if (t->dst_fmt_index >= cur_max_index) {
1570 ast_log(LOG_WARNING, "Destination codec %s is larger than cur_max_index\n", t->dst_codec.name);
1571 return -1;
1572 }
1573
1574 if (t->buf_size) {
1575 /*
1576 * Align buf_size properly, rounding up to the machine-specific
1577 * alignment for pointers.
1578 */
1579 struct _test_align { void *a, *b; } p;
1580 int align = (char *)&p.b - (char *)&p.a;
1581
1582 t->buf_size = ((t->buf_size + align - 1) / align) * align;
1583 }
1584
1585 if (t->frameout == NULL) {
1587 }
1588
1590
1591 ast_verb(5, "Registered translator '%s' from codec %s to %s, table cost, %d, computational cost %d\n",
1592 term_color(tmp, t->name, COLOR_MAGENTA, COLOR_BLACK, sizeof(tmp)),
1594
1596
1597 /* find any existing translators that provide this same srcfmt/dstfmt,
1598 and put this one in order based on computational cost */
1600 if ((u->src_fmt_index == t->src_fmt_index) &&
1601 (u->dst_fmt_index == t->dst_fmt_index) &&
1602 (u->comp_cost > t->comp_cost)) {
1604 t = NULL;
1605 break;
1606 }
1607 }
1609
1610 /* if no existing translator was found for this codec combination,
1611 add it to the beginning of the list */
1612 if (t) {
1614 }
1615
1616 matrix_rebuild(0);
1617
1619
1620 return 0;
1621}
#define ast_log
Definition astobj2.c:42
#define ao2_cleanup(obj)
Definition astobj2.h:1934
struct ast_codec * ast_codec_get(const char *name, enum ast_media_type type, unsigned int sample_rate)
Retrieve a codec given a name, type, and sample rate.
Definition codec.c:327
#define ast_verb(level,...)
#define LOG_WARNING
#define AST_RWLIST_TRAVERSE_SAFE_BEGIN
#define AST_RWLIST_WRLOCK(head)
Write locks a list.
Definition linkedlists.h:52
#define AST_RWLIST_UNLOCK(head)
Attempts to unlock a read/write based list.
#define AST_RWLIST_TRAVERSE_SAFE_END
#define AST_RWLIST_INSERT_HEAD
#define AST_RWLIST_INSERT_BEFORE_CURRENT
#define NULL
Definition resample.c:96
Represents a media codec within Asterisk.
Definition codec.h:42
unsigned int sample_rate
Sample rate (number of samples carried in a second)
Definition codec.h:52
enum ast_media_type type
Type of media this codec contains.
Definition codec.h:50
const char * name
Name for this codec.
Definition codec.h:46
Descriptor of a translator.
Definition translate.h:137
struct ast_module *int active
Definition translate.h:187
char name[80]
Definition translate.h:138
int buf_size
size of outbuf, in bytes. Mandatory. The wrapper code will also allocate an AST_FRIENDLY_OFFSET space...
Definition translate.h:182
struct ast_codec src_codec
Definition translate.h:139
struct ast_frame *(* frameout)(struct ast_trans_pvt *pvt)
Definition translate.h:158
struct ast_codec dst_codec
Definition translate.h:140
the list of translators
#define COLOR_MAGENTA
Definition term.h:60
#define COLOR_BLACK
Definition term.h:50
char * term_color(char *outbuf, const char *inbuf, int fgcolor, int bgcolor, int maxout)
Colorize a specified string by adding terminal color codes.
Definition term.c:235
static struct test_val b
static struct test_val a
static int cur_max_index
Definition translate.c:152
static int add_codec2index(struct ast_codec *codec)
Definition translate.c:205
static struct ast_frame * default_frameout(struct ast_trans_pvt *pvt)
Definition translate.c:526
static void matrix_rebuild(int samples)
rebuild a translation matrix.
Definition translate.c:1040
static int matrix_resize(int init)
Definition translate.c:252
static int codec2index(struct ast_codec *codec)
Definition translate.c:182
static void generate_computational_cost(struct ast_translator *t, int seconds)
Definition translate.c:812
static int generate_table_cost(struct ast_codec *src, struct ast_codec *dst)
Definition translate.c:984
#define RAII_VAR(vartype, varname, initval, dtor)
Declare a variable that will call a destructor function when it goes out of scope.
Definition utils.h:981
#define ast_assert(a)
Definition utils.h:779

References a, ast_translator::active, add_codec2index(), ao2_cleanup, ast_assert, ast_codec_get(), 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, b, ast_translator::buf_size, codec2index(), COLOR_BLACK, COLOR_MAGENTA, ast_translator::comp_cost, cur_max_index, default_frameout(), ast_translator::dst_codec, ast_translator::dst_fmt_index, ast_translator::frameout, generate_computational_cost(), generate_table_cost(), LOG_WARNING, matrix_rebuild(), matrix_resize(), ast_codec::name, ast_translator::name, NULL, RAII_VAR, ast_codec::sample_rate, ast_translator::src_codec, ast_translator::src_fmt_index, ast_translator::table_cost, term_color(), and ast_codec::type.

◆ ast_trans_frameout()

struct ast_frame * ast_trans_frameout ( struct ast_trans_pvt pvt,
int  datalen,
int  samples 
)

generic frameout function

generic frameout function

Definition at line 496 of file translate.c.

498{
499 struct ast_frame *f = &pvt->f;
500
501 if (samples) {
502 f->samples = samples;
503 } else {
504 if (pvt->samples == 0) {
505 return NULL;
506 }
507 f->samples = pvt->samples;
508 pvt->samples = 0;
509 }
510 if (datalen) {
511 f->datalen = datalen;
512 f->data.ptr = pvt->outbuf.c;
513 } else {
514 f->datalen = pvt->datalen;
515 if (!f->datalen) {
516 f->data.ptr = NULL;
517 } else {
518 f->data.ptr = pvt->outbuf.c;
519 }
520 pvt->datalen = 0;
521 }
522
523 return ast_frisolate(f);
524}
#define ast_frisolate(fr)
Makes a frame independent of any static storage.
Data structure associated with a single frame of data.
union ast_frame::@235 data
struct ast_frame f
Definition translate.h:215
union ast_trans_pvt::@304 outbuf
int datalen
actual space used in outbuf
Definition translate.h:218

References ast_frisolate, ast_trans_pvt::c, ast_frame::data, ast_frame::datalen, ast_trans_pvt::datalen, ast_trans_pvt::f, NULL, ast_trans_pvt::outbuf, ast_frame::ptr, ast_frame::samples, and ast_trans_pvt::samples.

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

◆ ast_translate()

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. In case the frame type is AST_FRAME_RTCP, the frame is not translated but passed to the translator codecs via the feedback callback, and a pointer to ast_null_frame is returned after that.

Parameters
pathtr translator 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
Return values
NULLon 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. In case the frame type is AST_FRAME_RTCP, the frame is not translated but passed to the translator codecs via the feedback callback, and a pointer to ast_null_frame is returned after that.

Definition at line 623 of file translate.c.

624{
625 struct ast_trans_pvt *p = path;
626 struct ast_frame *out;
627 struct timeval delivery;
628 int has_timing_info;
629 long ts;
630 long len;
631 int seqno;
632
633 if (f->frametype == AST_FRAME_RTCP) {
634 /* Just pass the feedback to the right callback, if it exists.
635 * This "translation" does nothing so return a null frame. */
636 struct ast_trans_pvt *tp;
637 for (tp = p; tp; tp = tp->next) {
638 if (tp->t->feedback)
639 tp->t->feedback(tp, f);
640 }
641 return &ast_null_frame;
642 }
643
644 has_timing_info = ast_test_flag(f, AST_FRFLAG_HAS_TIMING_INFO);
645 ts = f->ts;
646 len = f->len;
647 seqno = f->seqno;
648
649 if (!ast_tvzero(f->delivery)) {
650 if (!ast_tvzero(path->nextin)) {
651 /* Make sure this is in line with what we were expecting */
652 if (!ast_tveq(path->nextin, f->delivery)) {
653 /* The time has changed between what we expected and this
654 most recent time on the new packet. If we have a
655 valid prediction adjust our output time appropriately */
656 if (!ast_tvzero(path->nextout)) {
657 path->nextout = ast_tvadd(path->nextout,
658 ast_tvsub(f->delivery, path->nextin));
659 }
660 path->nextin = f->delivery;
661 }
662 } else {
663 /* This is our first pass. Make sure the timing looks good */
664 path->nextin = f->delivery;
665 path->nextout = f->delivery;
666 }
667 /* Predict next incoming sample */
668 path->nextin = ast_tvadd(path->nextin, ast_samp2tv(
670 }
671 delivery = f->delivery;
672 for (out = f; out && p ; p = p->next) {
673 struct ast_frame *current = out;
674
675 do {
676 framein(p, current);
678 } while (current);
679 if (out != f) {
681 }
682 out = p->t->frameout(p);
683 }
684
685 if (!out) {
687 }
688
689 if (out) {
690 /* we have a frame, play with times */
691 if (!ast_tvzero(delivery)) {
692 struct ast_frame *current = out;
693
694 do {
695 /* Regenerate prediction after a discontinuity */
696 if (ast_tvzero(path->nextout)) {
697 path->nextout = ast_tvnow();
698 }
699
700 /* Use next predicted outgoing timestamp */
701 current->delivery = path->nextout;
702
703 /* Invalidate prediction if we're entering a silence period */
704 if (current->frametype == AST_FRAME_CNG) {
705 path->nextout = ast_tv(0, 0);
706 /* Predict next outgoing timestamp from samples in this
707 frame. */
708 } else {
709 path->nextout = ast_tvadd(path->nextout, ast_samp2tv(
710 current->samples, ast_format_get_sample_rate(current->subclass.format)));
711 }
712
714 ast_debug(4, "Sample size different %d vs %d\n", f->samples, current->samples);
716 }
718 } while (current);
719 } else {
720 out->delivery = ast_tv(0, 0);
722 if (has_timing_info) {
723 out->ts = ts;
724 out->len = len;
725 out->seqno = seqno;
726 }
727 /* Invalidate prediction if we're entering a silence period */
728 if (out->frametype == AST_FRAME_CNG) {
729 path->nextout = ast_tv(0, 0);
730 }
731 }
732 }
733 if (consume) {
734 ast_frfree(f);
735 }
736 return out;
737}
size_t current
unsigned int ast_format_get_sample_rate(const struct ast_format *format)
Get the sample rate of a media format.
Definition format.c:379
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
#define ast_frfree(fr)
@ AST_FRFLAG_HAS_TIMING_INFO
struct ast_frame ast_null_frame
Definition main/frame.c:79
#define ast_debug(level,...)
Log a DEBUG message.
#define AST_LIST_NEXT(elm, field)
Returns the next entry in the list after the given entry.
struct ast_format * format
struct ast_frame_subclass subclass
struct timeval delivery
enum ast_frame_type frametype
Default structure for translators, with the basic fields and buffers, all allocated as part of the sa...
Definition translate.h:213
struct timeval nextout
Definition translate.h:229
struct timeval nextin
Definition translate.h:228
struct ast_trans_pvt * next
Definition translate.h:227
struct ast_translator * t
Definition translate.h:214
void(* feedback)(struct ast_trans_pvt *pvt, struct ast_frame *feedback)
Definition translate.h:162
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:282
int ast_tveq(struct timeval _a, struct timeval _b)
Returns true if the two struct timeval arguments are equal.
Definition time.h:147
int ast_tvzero(const struct timeval t)
Returns true if the argument is 0,0.
Definition time.h:117
struct timeval ast_tvadd(struct timeval a, struct timeval b)
Returns the sum of two timevals a + b.
Definition extconf.c:2280
struct timeval ast_tvsub(struct timeval a, struct timeval b)
Returns the difference of two timevals a - b.
Definition extconf.c:2295
struct timeval ast_tvnow(void)
Returns current timeval. Meant to replace calls to gettimeofday().
Definition time.h:159
struct timeval ast_tv(ast_time_t sec, ast_suseconds_t usec)
Returns a timeval from sec, usec.
Definition time.h:235
static struct ast_frame * generate_interpolated_slin(struct ast_trans_pvt *p, struct ast_frame *f)
Definition translate.c:594
static int framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
framein wrapper, deals with bound checks.
Definition translate.c:454
FILE * out
Definition utils/frame.c:33
#define ast_test_flag(p, flag)
Definition utils.h:64
#define ast_set2_flag(p, value, flag)
Definition utils.h:95
#define ast_clear_flag(p, flag)
Definition utils.h:78

References ast_clear_flag, ast_debug, ast_format_get_sample_rate(), AST_FRAME_CNG, AST_FRAME_RTCP, AST_FRFLAG_HAS_TIMING_INFO, ast_frfree, AST_LIST_NEXT, ast_null_frame, ast_samp2tv(), ast_set2_flag, ast_test_flag, ast_tv(), ast_tvadd(), ast_tveq(), ast_tvnow(), ast_tvsub(), ast_tvzero(), current, ast_frame::delivery, ast_trans_pvt::f, ast_translator::feedback, ast_frame_subclass::format, framein(), ast_translator::frameout, ast_frame::frametype, generate_interpolated_slin(), len(), ast_frame::len, ast_trans_pvt::next, ast_trans_pvt::nextin, ast_trans_pvt::nextout, out, ast_frame::samples, ast_frame::seqno, ast_frame::subclass, ast_trans_pvt::t, and ast_frame::ts.

Referenced by __ast_read(), ast_slinfactory_feed(), ast_write_stream(), ast_writestream(), audiohook_list_translate_to_native(), audiohook_list_translate_to_slin(), audiohook_read_frame_helper(), conf_run(), fax_gateway_framehook(), and softmix_process_write_audio().

◆ ast_translate_available_formats()

void ast_translate_available_formats ( struct ast_format_cap dest,
struct ast_format_cap src,
struct ast_format_cap result 
)

Find available formats.

Parameters
destpossible destination formats
srcsource formats
[out]resultcapabilities structure to store available formats in 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 1890 of file translate.c.

1891{
1892 struct ast_format *cur_dest, *cur_src;
1893 int index;
1894
1895 for (index = 0; index < ast_format_cap_count(dest); ++index) {
1896 if (!(cur_dest = ast_format_cap_get_format(dest, index))) {
1897 continue;
1898 }
1899
1900 /* We give preference to a joint format structure if possible */
1901 if ((cur_src = ast_format_cap_get_compatible_format(src, cur_dest))) {
1902 ast_format_cap_append(result, cur_src, 0);
1903 ao2_ref(cur_src, -1);
1904 } else {
1905 /* Otherwise we just use the destination format */
1906 ast_format_cap_append(result, cur_dest, 0);
1907 }
1908 ao2_ref(cur_dest, -1);
1909 }
1910
1911 /* if we don't have a source format, we just have to try all
1912 possible destination formats */
1913 if (!src) {
1914 return;
1915 }
1916
1917 for (index = 0; index < ast_format_cap_count(src); ++index) {
1918 if (!(cur_src = ast_format_cap_get_format(src, index))) {
1919 continue;
1920 }
1921
1923 check_translation_path(dest, src, result,
1924 cur_src, AST_MEDIA_TYPE_AUDIO);
1925 check_translation_path(dest, src, result,
1926 cur_src, AST_MEDIA_TYPE_VIDEO);
1928 ao2_ref(cur_src, -1);
1929 }
1930}
#define ao2_ref(o, delta)
Reference/unreference an object and return the old refcount.
Definition astobj2.h:459
static PGresult * result
Definition cel_pgsql.c:84
@ AST_MEDIA_TYPE_AUDIO
Definition codec.h:32
@ AST_MEDIA_TYPE_VIDEO
Definition codec.h:33
struct ast_format * ast_format_cap_get_format(const struct ast_format_cap *cap, int position)
Get the format at a specific index.
Definition format_cap.c:400
#define ast_format_cap_append(cap, format, framing)
Add format capability to capabilities structure.
Definition format_cap.h:99
size_t ast_format_cap_count(const struct ast_format_cap *cap)
Get the number of formats present within the capabilities structure.
Definition format_cap.c:395
struct ast_format * ast_format_cap_get_compatible_format(const struct ast_format_cap *cap, const struct ast_format *format)
Find if input ast_format is within the capabilities of the ast_format_cap object then return the comp...
Definition format_cap.c:546
#define AST_RWLIST_RDLOCK(head)
Read locks a list.
Definition linkedlists.h:78
Definition of a media format.
Definition format.c:43
static void check_translation_path(struct ast_format_cap *dest, struct ast_format_cap *src, struct ast_format_cap *result, struct ast_format *src_fmt, enum ast_media_type type)
Definition translate.c:1832

References ao2_ref, ast_format_cap_append, ast_format_cap_count(), ast_format_cap_get_compatible_format(), ast_format_cap_get_format(), AST_MEDIA_TYPE_AUDIO, AST_MEDIA_TYPE_VIDEO, AST_RWLIST_RDLOCK, AST_RWLIST_UNLOCK, check_translation_path(), and result.

Referenced by ast_rtp_instance_available_formats().

◆ ast_translate_init()

int ast_translate_init ( void  )

Initialize the translation matrix and index to format conversion table.

Return values
0on success
-1on failure

Definition at line 1949 of file translate.c.

1950{
1951 int res = 0;
1953 res = matrix_resize(1);
1956 return res;
1957}
int ast_register_cleanup(void(*func)(void))
Register a function to be executed before Asterisk gracefully exits.
Definition clicompat.c:19
#define ast_cli_register_multiple(e, len)
Register multiple commands.
Definition cli.h:265
#define ast_rwlock_init(rwlock)
wrapper for rwlock with tracking enabled
Definition lock.h:231
static ast_rwlock_t tablelock
Definition translate.c:144
static void translate_shutdown(void)
Definition translate.c:1932
static struct ast_cli_entry cli_translate[]
Definition translate.c:1505
#define ARRAY_LEN(a)
Definition utils.h:706

References ARRAY_LEN, ast_cli_register_multiple, ast_register_cleanup(), ast_rwlock_init, cli_translate, matrix_resize(), tablelock, and translate_shutdown().

Referenced by asterisk_daemon().

◆ ast_translate_path_steps()

unsigned int ast_translate_path_steps ( struct ast_format dest,
struct ast_format 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
Return values
-1if no path is available

Definition at line 1810 of file translate.c.

1811{
1812 unsigned int res = -1;
1813 /* convert bitwise format numbers into array indices */
1814 int src = format2index(src_format);
1815 int dest = format2index(dst_format);
1816
1817 if (src < 0 || dest < 0) {
1818 ast_log(LOG_WARNING, "No translator path: (%s codec is not valid)\n", src < 0 ? "starting" : "ending");
1819 return -1;
1820 }
1822
1823 if (matrix_get(src, dest)->step) {
1824 res = matrix_get(src, dest)->multistep + 1;
1825 }
1826
1828
1829 return res;
1830}
uint8_t multistep
Definition translate.c:120
static struct translator_path * matrix_get(unsigned int x, unsigned int y)
Definition translate.c:340
static int format2index(struct ast_format *format)
Definition translate.c:191

References ast_log, AST_RWLIST_RDLOCK, AST_RWLIST_UNLOCK, format2index(), LOG_WARNING, matrix_get(), and translator_path::multistep.

Referenced by ast_channel_make_compatible_helper().

◆ ast_translate_path_to_str()

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
ttranslator structure containing the translation path
strast_str output buffer
Returns
on success pointer to beginning of outbuf
Return values
""on failure

Definition at line 1145 of file translate.c.

1146{
1147 if (!p || !p->t) {
1148 return "";
1149 }
1150
1152 codec_append_name(&p->t->src_codec, str);
1153 while (p) {
1154 ast_str_append(str, 0, "->");
1155 codec_append_name(&p->t->dst_codec, str);
1156 p = p->next;
1157 }
1158
1159 return ast_str_buffer(*str);
1160}
const char * str
Definition app_jack.c:150
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:1139
void ast_str_reset(struct ast_str *buf)
Reset the content of a dynamic string. Useful before a series of ast_str_append.
Definition strings.h:693
char *attribute_pure ast_str_buffer(const struct ast_str *buf)
Returns the string buffer within the ast_str buf.
Definition strings.h:761
static void codec_append_name(const struct ast_codec *codec, struct ast_str **buf)
Definition translate.c:1136

References ast_str_append(), ast_str_buffer(), ast_str_reset(), codec_append_name(), ast_translator::dst_codec, ast_trans_pvt::next, ast_translator::src_codec, str, and ast_trans_pvt::t.

Referenced by chan_pjsip_write_stream(), generate_status(), handle_showchan(), and serialize_showchan().

◆ ast_translator_activate()

void ast_translator_activate ( struct ast_translator t)

Activate a previously deactivated translator.

Parameters
ttranslator to activate

Enables the specified translator for use.

Definition at line 1652 of file translate.c.

1653{
1655 t->active = 1;
1656 matrix_rebuild(0);
1658}

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

◆ ast_translator_best_choice()

int ast_translator_best_choice ( struct ast_format_cap dst_cap,
struct ast_format_cap src_cap,
struct ast_format **  dst_fmt_out,
struct ast_format **  src_fmt_out 
)

Chooses the best translation path.

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

Parameters
dst_capdestination capabilities
src_capsource capabilities
dst_fmt_outdestination format chosen out of destination capabilities
src_fmt_outsource format chosen out of source capabilities
Return values
0on success
-1if no path could be found.
Note
dst_cap and src_cap are not mondified.

Chooses the best translation path.

Definition at line 1676 of file translate.c.

1680{
1681 unsigned int besttablecost = INT_MAX;
1682 unsigned int beststeps = INT_MAX;
1683 struct ast_format *fmt;
1684 struct ast_format *dst;
1685 struct ast_format *src;
1686 RAII_VAR(struct ast_format *, best, NULL, ao2_cleanup);
1687 RAII_VAR(struct ast_format *, bestdst, NULL, ao2_cleanup);
1688 struct ast_format_cap *joint_cap;
1689 int i;
1690 int j;
1691
1692 if (ast_format_cap_empty(dst_cap) || ast_format_cap_empty(src_cap)) {
1693 ast_log(LOG_ERROR, "Cannot determine best translation path since one capability supports no formats\n");
1694 return -1;
1695 }
1696
1698 if (!joint_cap) {
1699 return -1;
1700 }
1701 ast_format_cap_get_compatible(dst_cap, src_cap, joint_cap);
1702
1703 for (i = 0; i < ast_format_cap_count(joint_cap); ++i, ao2_cleanup(fmt)) {
1704 fmt = ast_format_cap_get_format(joint_cap, i);
1705 if (!fmt
1707 continue;
1708 }
1709
1710 if (!best
1712 ao2_replace(best, fmt);
1713 }
1714 }
1715 ao2_ref(joint_cap, -1);
1716
1717 if (best) {
1718 ao2_replace(*dst_fmt_out, best);
1719 ao2_replace(*src_fmt_out, best);
1720 return 0;
1721 }
1722
1723 /* need to translate */
1725 for (i = 0; i < ast_format_cap_count(dst_cap); ++i, ao2_cleanup(dst)) {
1726 dst = ast_format_cap_get_format(dst_cap, i);
1727 if (!dst
1729 continue;
1730 }
1731
1732 for (j = 0; j < ast_format_cap_count(src_cap); ++j, ao2_cleanup(src)) {
1733 int x;
1734 int y;
1735
1736 src = ast_format_cap_get_format(src_cap, j);
1737 if (!src
1739 continue;
1740 }
1741
1742 x = format2index(src);
1743 y = format2index(dst);
1744 if (x < 0 || y < 0) {
1745 continue;
1746 }
1747 if (!matrix_get(x, y) || !(matrix_get(x, y)->step)) {
1748 continue;
1749 }
1750 if (matrix_get(x, y)->table_cost < besttablecost
1751 || matrix_get(x, y)->multistep < beststeps) {
1752 /* better than what we have so far */
1753 ao2_replace(best, src);
1754 ao2_replace(bestdst, dst);
1755 besttablecost = matrix_get(x, y)->table_cost;
1756 beststeps = matrix_get(x, y)->multistep;
1757 } else if (matrix_get(x, y)->table_cost == besttablecost
1758 && matrix_get(x, y)->multistep == beststeps) {
1759 int replace = 0;
1760 unsigned int gap_selected = format_sample_rate_absdiff(best, bestdst);
1761 unsigned int gap_current = format_sample_rate_absdiff(src, dst);
1762
1763 if (gap_current < gap_selected) {
1764 /* better than what we have so far */
1765 replace = 1;
1766 } else if (gap_current == gap_selected) {
1767 int src_quality, best_quality;
1768 struct ast_codec *src_codec, *best_codec;
1769
1770 src_codec = ast_format_get_codec(src);
1771 best_codec = ast_format_get_codec(best);
1772 src_quality = src_codec->quality;
1773 best_quality = best_codec->quality;
1774
1775 ao2_cleanup(src_codec);
1776 ao2_cleanup(best_codec);
1777
1778 /* We have a tie, so choose the format with the higher quality, if they differ. */
1779 if (src_quality > best_quality) {
1780 /* Better than what we had before. */
1781 replace = 1;
1782 ast_debug(2, "Tiebreaker: preferring format %s (%d) to %s (%d)\n", ast_format_get_name(src), src_quality,
1783 ast_format_get_name(best), best_quality);
1784 } else {
1785 /* This isn't necessarily indicative of a problem, but in reality this shouldn't really happen, unless
1786 * there are 2 formats that are basically the same. */
1787 ast_debug(1, "Completely ambiguous tie between formats %s and %s (quality %d): sticking with %s, but this is arbitrary\n",
1788 ast_format_get_name(src), ast_format_get_name(best), best_quality, ast_format_get_name(best));
1789 }
1790 }
1791 if (replace) {
1792 ao2_replace(best, src);
1793 ao2_replace(bestdst, dst);
1794 besttablecost = matrix_get(x, y)->table_cost;
1795 beststeps = matrix_get(x, y)->multistep;
1796 }
1797 }
1798 }
1799 }
1801
1802 if (!best) {
1803 return -1;
1804 }
1805 ao2_replace(*dst_fmt_out, bestdst);
1806 ao2_replace(*src_fmt_out, best);
1807 return 0;
1808}
#define ao2_replace(dst, src)
Replace one object reference with another cleaning up the original.
Definition astobj2.h:501
enum ast_media_type ast_format_get_type(const struct ast_format *format)
Get the media type of a format.
Definition format.c:354
struct ast_codec * ast_format_get_codec(const struct ast_format *format)
Get the codec associated with a format.
Definition format.c:324
const char * ast_format_get_name(const struct ast_format *format)
Get the name associated with a format.
Definition format.c:334
int ast_format_cap_empty(const struct ast_format_cap *cap)
Determine if a format cap has no formats in it.
Definition format_cap.c:744
int ast_format_cap_get_compatible(const struct ast_format_cap *cap1, const struct ast_format_cap *cap2, struct ast_format_cap *result)
Find the compatible formats between two capabilities structures.
Definition format_cap.c:628
@ AST_FORMAT_CAP_FLAG_DEFAULT
Definition format_cap.h:38
#define ast_format_cap_alloc(flags)
Allocate a new ast_format_cap structure.
Definition format_cap.h:49
static int replace(struct ast_channel *chan, const char *cmd, char *data, struct ast_str **buf, ssize_t len)
#define LOG_ERROR
unsigned int quality
Format quality, on scale from 0 to 150 (100 is ulaw, the reference). This allows better format to be ...
Definition codec.h:82
Format capabilities structure, holds formats + preference order + etc.
Definition format_cap.c:54
uint32_t table_cost
Definition translate.c:118
#define format_sample_rate_absdiff(fmt1, fmt2)
Definition translate.c:1669

References ao2_cleanup, ao2_ref, ao2_replace, ast_debug, ast_format_cap_alloc, ast_format_cap_count(), ast_format_cap_empty(), AST_FORMAT_CAP_FLAG_DEFAULT, ast_format_cap_get_compatible(), ast_format_cap_get_format(), ast_format_get_codec(), ast_format_get_name(), ast_format_get_sample_rate(), ast_format_get_type(), ast_log, AST_MEDIA_TYPE_AUDIO, AST_RWLIST_RDLOCK, AST_RWLIST_UNLOCK, format2index(), format_sample_rate_absdiff, LOG_ERROR, matrix_get(), translator_path::multistep, NULL, ast_codec::quality, RAII_VAR, replace(), and translator_path::table_cost.

Referenced by ast_channel_make_compatible_helper(), ast_speech_new(), iax2_request(), local_request_with_stream_topology(), request_channel(), and set_format().

◆ ast_translator_build_path()

struct ast_trans_pvt * ast_translator_build_path ( struct ast_format dst,
struct ast_format src 
)

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

Parameters
dstdest destination format
srcsource source format
Returns
ast_trans_pvt on success
Return values
NULLon failure

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

Definition at line 543 of file translate.c.

544{
545 struct ast_trans_pvt *head = NULL, *tail = NULL;
546 int src_index, dst_index;
547
548 src_index = format2index(src);
549 dst_index = format2index(dst);
550
551 if (src_index < 0 || dst_index < 0) {
552 ast_log(LOG_WARNING, "No translator path: (%s codec is not valid)\n", src_index < 0 ? "starting" : "ending");
553 return NULL;
554 }
555
557
558 while (src_index != dst_index) {
559 struct ast_trans_pvt *cur;
560 struct ast_format *explicit_dst = NULL;
561 struct ast_translator *t = matrix_get(src_index, dst_index)->step;
562 if (!t) {
563 ast_log(LOG_WARNING, "No translator path from %s to %s\n",
567 return NULL;
568 }
570 explicit_dst = dst;
571 }
572 if (!(cur = newpvt(t, explicit_dst))) {
573 ast_log(LOG_WARNING, "Failed to build translator step from %s to %s\n",
577 return NULL;
578 }
579 if (!head) {
580 head = cur;
581 } else {
582 tail->next = cur;
583 }
584 tail = cur;
585 cur->nextin = cur->nextout = ast_tv(0, 0);
586 /* Keep going if this isn't the final destination */
587 src_index = cur->t->dst_fmt_index;
588 }
589
591 return head;
592}
struct ast_translator * step
Definition translate.c:117
void ast_translator_free_path(struct ast_trans_pvt *p)
Frees a translator path Frees the given translator path structure.
Definition translate.c:533
static struct ast_trans_pvt * newpvt(struct ast_translator *t, struct ast_format *explicit_dst)
Allocate the descriptor, required outbuf space, and possibly desc.
Definition translate.c:369

References ast_format_get_name(), ast_format_get_sample_rate(), ast_format_get_type(), ast_log, AST_RWLIST_RDLOCK, AST_RWLIST_UNLOCK, ast_translator_free_path(), ast_tv(), ast_translator::dst_codec, ast_translator::dst_fmt_index, format2index(), LOG_WARNING, matrix_get(), newpvt(), ast_trans_pvt::next, ast_trans_pvt::nextin, ast_trans_pvt::nextout, NULL, ast_codec::sample_rate, translator_path::step, ast_trans_pvt::t, and ast_codec::type.

Referenced by ast_set_read_format_path(), ast_set_write_format_path(), ast_slinfactory_feed(), ast_writestream(), audiohook_list_translate_to_native(), audiohook_list_translate_to_slin(), audiohook_read_frame_helper(), conf_run(), set_format(), softmix_process_write_audio(), and softmix_translate_helper_change_rate().

◆ ast_translator_deactivate()

void ast_translator_deactivate ( struct ast_translator t)

Deactivate a translator.

Parameters
ttranslator to deactivate

Disables the specified translator from being used.

Definition at line 1660 of file translate.c.

1661{
1663 t->active = 0;
1664 matrix_rebuild(0);
1666}

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

◆ ast_translator_free_path()

void ast_translator_free_path ( struct ast_trans_pvt tr)

◆ ast_unregister_translator()

int ast_unregister_translator ( struct ast_translator t)

Unregister a translator Unregisters the given translator.

Parameters
ttranslator to unregister
Return values
0on success
-1on failure

Unregister a translator Unregisters the given translator.

Definition at line 1624 of file translate.c.

1625{
1626 char tmp[80];
1627 struct ast_translator *u;
1628 int found = 0;
1629
1632 if (u == t) {
1634 ast_verb(5, "Unregistered translator '%s' from codec %s to %s\n",
1635 term_color(tmp, t->name, COLOR_MAGENTA, COLOR_BLACK, sizeof(tmp)),
1636 t->src_codec.name, t->dst_codec.name);
1637 found = 1;
1638 break;
1639 }
1640 }
1642
1643 if (found && !ast_shutting_down()) {
1644 matrix_rebuild(0);
1645 }
1646
1648
1649 return (u ? 0 : -1);
1650}
int ast_shutting_down(void)
Definition asterisk.c:1889
#define AST_RWLIST_REMOVE_CURRENT
struct ast_translator::@303 list

References AST_RWLIST_REMOVE_CURRENT, AST_RWLIST_TRAVERSE_SAFE_BEGIN, AST_RWLIST_TRAVERSE_SAFE_END, AST_RWLIST_UNLOCK, AST_RWLIST_WRLOCK, ast_shutting_down(), ast_verb, COLOR_BLACK, COLOR_MAGENTA, ast_translator::dst_codec, ast_translator::list, matrix_rebuild(), ast_codec::name, ast_translator::name, ast_translator::src_codec, and term_color().

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