35 #include <sys/resource.h>
47 #define MAX_RECALC 1000
167 ofs = (
char *)(pvt + 1);
289 if (source == -1 || dest == -1) {
290 ast_log(
LOG_WARNING,
"No translator path: (%s codec is not valid)\n", source == -1 ?
"starting" :
"ending");
296 while (source != dest) {
332 struct timeval delivery;
366 for ( ; out && p ; p = p->
next) {
370 out = p->t->frameout(p);
393 if (has_timing_info) {
437 getrusage(RUSAGE_SELF, &start);
440 while (num_samples < seconds * out_rate) {
456 getrusage(RUSAGE_SELF, &end);
458 cost = ((end.ru_utime.tv_sec - start.ru_utime.tv_sec) * 1000000) + end.ru_utime.tv_usec - start.ru_utime.tv_usec;
459 cost += ((end.ru_stime.tv_sec - start.ru_stime.tv_sec) * 1000000) + end.ru_stime.tv_usec - start.ru_stime.tv_usec;
463 t->
cost = cost / seconds;
477 if (dst_ll && (src_rate == dst_rate)) {
479 }
else if (!dst_ll && (src_rate == dst_rate)) {
481 }
else if (dst_ll && (src_rate < dst_rate)) {
483 }
else if (!dst_ll && (src_rate < dst_rate)) {
485 }
else if (dst_ll && (src_rate > dst_rate)) {
487 }
else if (!dst_ll && (src_rate > dst_rate)) {
493 if (dst_ll && (src_rate == dst_rate)) {
495 }
else if (!dst_ll && (src_rate == dst_rate)) {
497 }
else if (dst_ll && (src_rate < dst_rate)) {
499 }
else if (!dst_ll && (src_rate < dst_rate)) {
501 }
else if (dst_ll && (src_rate > dst_rate)) {
503 }
else if (!dst_ll && (src_rate > dst_rate)) {
524 ast_debug(1,
"Resetting translation matrix\n");
564 int better_choice = 0;
570 if (z == x || z == y)
598 }
else if (new_rate_change <
tr_matrix[x][z].rate_change) {
604 if (!better_choice) {
651 int wordlen = strlen(word);
657 for (i = 0; i <
len; i++) {
661 if (!strncasecmp(word, format_list[i].
name, wordlen) && ++which > state) {
671 #define SHOW_TRANS 64
672 static const char *
const option1[] = {
"recalc",
"paths", NULL };
674 int curlen = 0, longest = 0, magnitude[
SHOW_TRANS] = { 0, };
678 e->
command =
"core show translation";
680 "Usage: 'core show translation' can be used in two ways.\n"
681 " 1. 'core show translation [recalc [<recalc seconds>]]\n"
682 " Displays known codec translators and the cost associated\n"
683 " with each conversion. If the argument 'recalc' is supplied along\n"
684 " with optional number of seconds to test a new test will be performed\n"
685 " as the chart is being displayed.\n"
686 " 2. 'core show translation paths [codec]'\n"
687 " This will display all the translation paths associated with a codec\n";
693 if (a->
pos == 4 && !strcasecmp(a->
argv[3], option1[1])) {
702 if (a->
argv[3] && !strcasecmp(a->
argv[3], option1[1]) && a->
argc == 5) {
712 for (i = 0; i <
len; i++) {
716 if (!strncasecmp(format_list[i].
name, a->
argv[4], strlen(format_list[i].name))) {
717 input_src = format_list[i].
bits;
722 ast_cli(a->
fd,
"Source codec \"%s\" is not found.\n", a->
argv[4]);
728 for (i = 0; i <
len; i++) {
737 dst =
powerof(format_list[i].bits);
762 }
else if (a->
argv[3] && !strcasecmp(a->
argv[3],
"recalc")) {
763 z = a->
argv[4] ? atoi(a->
argv[4]) : 1;
766 ast_cli(a->
fd,
" Recalc must be greater than 0. Defaulting to 1.\n");
774 ast_cli(a->
fd,
" Recalculating Codec Translation (number of sample seconds: %d)\n\n", z);
778 }
else if (a->
argc > 3)
783 ast_cli(a->
fd,
" Translation times between formats (in microseconds) for one second of data\n");
784 ast_cli(a->
fd,
" Source Format (Rows) Destination Format (Columns)\n\n");
791 if (curlen > longest)
819 if (y >= 0 && magnitude[y] + 1 > curlen) {
820 curlen = magnitude[y] + 1;
824 if (x >= 0 && y >= 0 &&
tr_matrix[x][y].step) {
827 }
else if (x == -1 && y >= 0) {
830 }
else if (y == -1 && x >= 0) {
833 }
else if (x >= 0 && y >= 0) {
855 static int added_cli = 0;
876 ast_log(
LOG_WARNING,
"Invalid translator path: (%s codec is not valid)\n", t->
srcfmt == -1 ?
"starting" :
"ending");
894 struct _test_align {
void *a, *b; } p;
895 int align = (
char *)&p.b - (
char *)&p.a;
905 ast_verb(2,
"Registered translator '%s' from format %s to %s, cost %d\n",
988 int besttime = INT_MAX;
989 int beststeps = INT_MAX;
990 unsigned int best_rate_change = INT_MAX;
998 if (!(cur & common)) {
1014 *srcs = *dst = best;
1019 if (! (cur & *dst)) {
1023 if (!(*srcs & cursrc) || !
tr_matrix[x][y].step) {
1032 if (
tr_matrix[x][y].rate_change < best_rate_change) {
1035 if ((
tr_matrix[x][y].rate_change <= best_rate_change) &&
1061 unsigned int res = -1;
1067 if (src == -1 || dest == -1) {
1068 ast_log(
LOG_WARNING,
"No translator path: (%s codec is not valid)\n", src == -1 ?
"starting" :
"ending");
1096 src_audio =
powerof(src_audio);
1101 src_video =
powerof(src_video);
1115 for (x = 1LL; src_audio && x > 0; x <<= 1) {
1116 if (!(x & AST_FORMAT_AUDIO_MASK)) {
1133 if (!
tr_matrix[src_audio][x_bits].step) {
1147 for (x = 1LL; src_video && x > 0; x <<= 1) {
1148 if (!(x & AST_FORMAT_VIDEO_MASK)) {
1165 if (!
tr_matrix[src_video][x_bits].step) {
union ast_frame_subclass subclass
struct ast_frame * ast_trans_frameout(struct ast_trans_pvt *pvt, int datalen, int samples)
generic frameout function
int datalen
actual space used in outbuf
#define AST_CLI_DEFINE(fn, txt,...)
static char * complete_trans_path_choice(const char *line, const char *word, int pos, int state)
Asterisk locking-related definitions:
Asterisk main include file. File version handling, generic pbx functions.
struct ast_frame *(* sample)(void)
path_samp_change
these values indicate how a translation path will affect the sample rate
static void * newpvt(struct ast_translator *t)
Allocate the descriptor, required outbuf space, and possibly desc.
void ast_module_unref(struct ast_module *)
union ast_trans_pvt::@213 outbuf
#define AST_RWLIST_HEAD_STATIC(name, type)
Defines a structure to be used to hold a read/write list of specified type, statically initialized...
enum path_samp_change rate_change
static struct ast_cli_entry cli_translate[]
Descriptor of a translator.
#define ast_set2_flag(p, value, flag)
#define ast_test_flag(p, flag)
Support for translation of data formats. translate.c.
int ast_tveq(struct timeval _a, struct timeval _b)
Returns true if the two struct timeval arguments are equal.
static enum path_samp_change get_rate_change_result(format_t src, format_t dst)
void ast_translator_activate(struct ast_translator *t)
Activate a previously deactivated translator.
#define AST_RWLIST_WRLOCK(head)
Write locks a list.
static char * handle_cli_core_show_translation(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
descriptor for a cli entry.
char * ast_str_buffer(const struct ast_str *buf)
Returns the string buffer within the ast_str buf.
#define AST_RWLIST_UNLOCK(head)
Attempts to unlock a read/write based list.
int ast_tvzero(const struct timeval t)
Returns true if the argument is 0,0.
static void calc_cost(struct ast_translator *t, int seconds)
compute the cost of a single translation step
static struct ast_frame * default_frameout(struct ast_trans_pvt *pvt)
int ast_str_append(struct ast_str **buf, ssize_t max_len, const char *fmt,...)
Append to a thread local dynamic string.
struct timeval ast_tvnow(void)
Returns current timeval. Meant to replace calls to gettimeofday().
static force_inline int ast_format_rate(format_t format)
Get the sample rate for a given format.
struct ast_frame * ast_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...
#define ast_copy_flags(dest, src, flagz)
#define ast_str_alloca(init_len)
struct ast_frame * ast_frisolate(struct ast_frame *fr)
Makes a frame independent of any static storage.
struct ast_trans_pvt * next
struct ast_translator * step
struct ast_format_list * ast_get_format_list(size_t *size)
void ast_cli(int fd, const char *fmt,...)
#define ast_verb(level,...)
static int framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
framein wrapper, deals with bound checks.
char * ast_cli_complete(const char *word, const char *const choices[], int pos)
int __ast_register_translator(struct ast_translator *t, struct ast_module *module)
Register a translator This registers a codec translator with asterisk.
struct ast_frame *(* frameout)(struct ast_trans_pvt *pvt)
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.
int ast_str_set(struct ast_str **buf, ssize_t max_len, const char *fmt,...)
Set a dynamic string using variable arguments.
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...
#define AST_RWLIST_RDLOCK(head)
Read locks a list.
struct ast_module * module
#define AST_RWLIST_INSERT_HEAD
#define ast_debug(level,...)
Log a DEBUG message.
General Asterisk PBX channel definitions.
#define AST_FRIENDLY_OFFSET
Offset into a frame's data buffer.
static force_inline int attribute_pure ast_strlen_zero(const char *s)
Asterisk internal frame definitions.
#define AST_RWLIST_TRAVERSE
Scheduler Routines (derived from cheops)
int ast_unregister_translator(struct ast_translator *t)
Unregister a translator Unregisters the given tranlator.
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 ...
#define AST_RWLIST_REMOVE_CURRENT
#define AST_RWLIST_INSERT_BEFORE_CURRENT
int buf_size
size of outbuf, in bytes. Mandatory. The wrapper code will also allocate an AST_FRIENDLY_OFFSET space...
#define AST_FORMAT_SLINEAR16
format_t ast_translate_available_formats(format_t dest, format_t src)
Mask off unavailable formats from a format bitmask.
#define AST_RWLIST_TRAVERSE_SAFE_BEGIN
char * term_color(char *outbuf, const char *inbuf, int fgcolor, int bgcolor, int maxout)
char * ast_getformatname(format_t format)
Get the name of a format.
The descriptor of a dynamic string XXX storage will be optimized later if needed We use the ts field ...
Default structure for translators, with the basic fields and buffers, all allocated as part of the sa...
int(* newpvt)(struct ast_trans_pvt *)
unsigned int ast_translate_path_steps(format_t dest, format_t src)
Returns the number of steps required to convert from 'src' to 'dest'.
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
struct timeval ast_tvadd(struct timeval a, struct timeval b)
Returns the sum of two timevals a + b.
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...
#define AST_FORMAT_VIDEO_MASK
void ast_translator_deactivate(struct ast_translator *t)
Deactivate a translator.
static void destroy(struct ast_trans_pvt *pvt)
#define AST_FORMAT_AUDIO_MASK
#define ast_clear_flag(p, flag)
void ast_str_reset(struct ast_str *buf)
Reset the content of a dynamic string. Useful before a series of ast_str_append.
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.
#define AST_FORMAT_SLINEAR
Standard Command Line Interface.
static force_inline int powerof(format_t d)
returns the index of the lowest bit set
struct timeval ast_tv(ast_time_t sec, ast_suseconds_t usec)
Returns a timeval from sec, usec.
int(* framein)(struct ast_trans_pvt *pvt, struct ast_frame *in)
int ast_cli_register_multiple(struct ast_cli_entry *e, int len)
Register multiple commands.
Data structure associated with a single frame of data.
struct ast_translator * t
Handy terminal functions for vt* terms.
struct timeval ast_tvsub(struct timeval a, struct timeval b)
Returns the difference of two timevals a - b.
enum ast_frame_type frametype
Asterisk module definitions.
union ast_frame::@172 data
struct ast_translator::@212 list
#define AST_RWLIST_TRAVERSE_SAFE_END
format_t ast_translator_best_choice(format_t *dsts, format_t *srcs)
Chooses the best translation path.
void ast_translator_free_path(struct ast_trans_pvt *tr)
Frees a translator path Frees the given translator path structure.
#define ASTERISK_FILE_VERSION(file, version)
Register/unregister a source code file with the core.
void(* destroy)(struct ast_trans_pvt *pvt)
static void rebuild_matrix(int samples)
rebuild a translation matrix.
struct ast_module * ast_module_ref(struct ast_module *)
int buffer_samples
size of outbuf, in samples. Leave it 0 if you want the framein callback deal with the frame...