#include "asterisk.h"
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "asterisk/lock.h"
#include "asterisk/channel.h"
#include "asterisk/logger.h"
#include "asterisk/translate.h"
#include "asterisk/module.h"
#include "asterisk/options.h"
#include "asterisk/frame.h"
#include "asterisk/sched.h"
#include "asterisk/cli.h"
#include "asterisk/term.h"
Go to the source code of this file.
Data Structures | |
struct | translator_path |
Defines | |
#define | MAX_RECALC 200 |
#define | SHOW_TRANS 13 |
Functions | |
int | __ast_register_translator (struct ast_translator *t, struct ast_module *mod) |
Register a translator This registers a codec translator with asterisk. | |
static | AST_LIST_HEAD_STATIC (translators, ast_translator) |
the list of translators | |
ast_frame * | ast_trans_frameout (struct ast_trans_pvt *pvt, int datalen, int samples) |
generic frameout function | |
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 | |
unsigned int | ast_translate_available_formats (unsigned int dest, unsigned int src) |
Mask off unavailable formats from a format bitmask. | |
void | ast_translate_frame_freed (struct ast_frame *fr) |
Hint that a frame from a translator has been freed. | |
unsigned int | ast_translate_path_steps (unsigned int dest, unsigned int src) |
Returns the number of steps required to convert from 'src' to 'dest'. | |
void | ast_translator_activate (struct ast_translator *t) |
Activate a previously deactivated translator. | |
int | ast_translator_best_choice (int *dst, int *srcs) |
Chooses the best translation path. | |
ast_trans_pvt * | ast_translator_build_path (int dest, int 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 *p) |
Frees a translator path Frees the given translator path structure. | |
int | ast_unregister_translator (struct ast_translator *t) |
Unregister a translator Unregisters the given tranlator. | |
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) |
static void | destroy (struct ast_trans_pvt *pvt) |
static int | framein (struct ast_trans_pvt *pvt, struct ast_frame *f) |
framein wrapper, deals with plc and bound checks. | |
static void * | newpvt (struct ast_translator *t) |
Allocate the descriptor, required outbuf space, and possibly also plc and desc. | |
static force_inline int | powerof (unsigned int d) |
returns the index of the lowest bit set | |
static void | rebuild_matrix (int samples) |
rebuild a translation matrix. | |
static int | show_translation (int fd, int argc, char *argv[]) |
static int | show_translation_deprecated (int fd, int argc, char *argv[]) |
CLI "show translation" command handler. | |
Variables | |
static struct ast_cli_entry | cli_show_translation_deprecated |
static struct ast_cli_entry | cli_translate [] |
static char | show_trans_usage [] |
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 first step. The full path can be reconstricted iterating on the matrix until step->dstfmt == desired_format. |
Definition in file translate.c.
#define MAX_RECALC 200 |
Definition at line 49 of file translate.c.
Referenced by show_translation(), and show_translation_deprecated().
#define SHOW_TRANS 13 |
Referenced by show_translation(), and show_translation_deprecated().
int __ast_register_translator | ( | struct ast_translator * | t, | |
struct ast_module * | module | |||
) |
Register a translator This registers a codec translator with asterisk.
t | populated ast_translator structure | |
module | handle to the module that owns this translator |
Definition at line 681 of file translate.c.
References ast_cli_register_multiple(), AST_FORMAT_SLINEAR, ast_getformatname(), AST_LIST_INSERT_BEFORE_CURRENT, AST_LIST_INSERT_HEAD, AST_LIST_LOCK, AST_LIST_TRAVERSE_SAFE_BEGIN, AST_LIST_TRAVERSE_SAFE_END, AST_LIST_UNLOCK, ast_log(), ast_verbose(), calc_cost(), cli_translate, COLOR_BLACK, COLOR_MAGENTA, ast_translator::cost, default_frameout(), ast_translator::dstfmt, LOG_WARNING, MAX_FORMAT, option_verbose, powerof(), rebuild_matrix(), ast_translator::srcfmt, t, term_color(), and VERBOSE_PREFIX_2.
00682 { 00683 static int added_cli = 0; 00684 struct ast_translator *u; 00685 00686 if (!mod) { 00687 ast_log(LOG_WARNING, "Missing module pointer, you need to supply one\n"); 00688 return -1; 00689 } 00690 00691 if (!t->buf_size) { 00692 ast_log(LOG_WARNING, "empty buf size, you need to supply one\n"); 00693 return -1; 00694 } 00695 00696 t->module = mod; 00697 00698 t->srcfmt = powerof(t->srcfmt); 00699 t->dstfmt = powerof(t->dstfmt); 00700 t->active = 1; 00701 00702 if (t->srcfmt == -1 || t->dstfmt == -1) { 00703 ast_log(LOG_WARNING, "Invalid translator path: (%s codec is not valid)\n", t->srcfmt == -1 ? "starting" : "ending"); 00704 return -1; 00705 } 00706 if (t->plc_samples) { 00707 if (t->buffer_samples < t->plc_samples) { 00708 ast_log(LOG_WARNING, "plc_samples %d buffer_samples %d\n", 00709 t->plc_samples, t->buffer_samples); 00710 return -1; 00711 } 00712 if (t->dstfmt != powerof(AST_FORMAT_SLINEAR)) 00713 ast_log(LOG_WARNING, "plc_samples %d format %x\n", 00714 t->plc_samples, t->dstfmt); 00715 } 00716 if (t->srcfmt >= MAX_FORMAT) { 00717 ast_log(LOG_WARNING, "Source format %s is larger than MAX_FORMAT\n", ast_getformatname(t->srcfmt)); 00718 return -1; 00719 } 00720 00721 if (t->dstfmt >= MAX_FORMAT) { 00722 ast_log(LOG_WARNING, "Destination format %s is larger than MAX_FORMAT\n", ast_getformatname(t->dstfmt)); 00723 return -1; 00724 } 00725 00726 if (t->buf_size) { 00727 /* 00728 * Align buf_size properly, rounding up to the machine-specific 00729 * alignment for pointers. 00730 */ 00731 struct _test_align { void *a, *b; } p; 00732 int align = (char *)&p.b - (char *)&p.a; 00733 00734 t->buf_size = ((t->buf_size + align - 1) / align) * align; 00735 } 00736 00737 if (t->frameout == NULL) 00738 t->frameout = default_frameout; 00739 00740 calc_cost(t, 1); 00741 00742 if (option_verbose > 1) { 00743 char tmp[80]; 00744 00745 ast_verbose(VERBOSE_PREFIX_2 "Registered translator '%s' from format %s to %s, cost %d\n", 00746 term_color(tmp, t->name, COLOR_MAGENTA, COLOR_BLACK, sizeof(tmp)), 00747 ast_getformatname(1 << t->srcfmt), ast_getformatname(1 << t->dstfmt), t->cost); 00748 } 00749 00750 if (!added_cli) { 00751 ast_cli_register_multiple(cli_translate, sizeof(cli_translate) / sizeof(struct ast_cli_entry)); 00752 added_cli++; 00753 } 00754 00755 AST_LIST_LOCK(&translators); 00756 00757 /* find any existing translators that provide this same srcfmt/dstfmt, 00758 and put this one in order based on cost */ 00759 AST_LIST_TRAVERSE_SAFE_BEGIN(&translators, u, list) { 00760 if ((u->srcfmt == t->srcfmt) && 00761 (u->dstfmt == t->dstfmt) && 00762 (u->cost > t->cost)) { 00763 AST_LIST_INSERT_BEFORE_CURRENT(&translators, t, list); 00764 t = NULL; 00765 } 00766 } 00767 AST_LIST_TRAVERSE_SAFE_END; 00768 00769 /* if no existing translator was found for this format combination, 00770 add it to the beginning of the list */ 00771 if (t) 00772 AST_LIST_INSERT_HEAD(&translators, t, list); 00773 00774 rebuild_matrix(0); 00775 00776 AST_LIST_UNLOCK(&translators); 00777 00778 return 0; 00779 }
static AST_LIST_HEAD_STATIC | ( | translators | , | |
ast_translator | ||||
) | [static] |
the list of translators
struct ast_frame* ast_trans_frameout | ( | struct ast_trans_pvt * | pvt, | |
int | datalen, | |||
int | samples | |||
) |
generic frameout function
Definition at line 221 of file translate.c.
References AST_FRAME_VOICE, AST_FRFLAG_FROM_TRANSLATOR, AST_FRIENDLY_OFFSET, ast_set_flag, ast_trans_pvt::datalen, ast_translator::dstfmt, f, ast_trans_pvt::f, ast_translator::name, ast_trans_pvt::outbuf, ast_trans_pvt::samples, and ast_trans_pvt::t.
Referenced by default_frameout(), lintoadpcm_frameout(), lintogsm_frameout(), lintoilbc_frameout(), and lintolpc10_frameout().
00223 { 00224 struct ast_frame *f = &pvt->f; 00225 00226 if (samples) 00227 f->samples = samples; 00228 else { 00229 if (pvt->samples == 0) 00230 return NULL; 00231 f->samples = pvt->samples; 00232 pvt->samples = 0; 00233 } 00234 if (datalen) 00235 f->datalen = datalen; 00236 else { 00237 f->datalen = pvt->datalen; 00238 pvt->datalen = 0; 00239 } 00240 00241 f->frametype = AST_FRAME_VOICE; 00242 f->subclass = 1 << (pvt->t->dstfmt); 00243 f->mallocd = 0; 00244 f->offset = AST_FRIENDLY_OFFSET; 00245 f->src = pvt->t->name; 00246 f->data = pvt->outbuf; 00247 00248 ast_set_flag(f, AST_FRFLAG_FROM_TRANSLATOR); 00249 00250 return f; 00251 }
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 frames. Consume determines whether the original frame should be freed
tr | translator structure to use for translation | |
f | frame to translate | |
consume | Whether or not to free the original frame |
Definition at line 315 of file translate.c.
References ast_format_rate(), AST_FRAME_CNG, AST_FRFLAG_HAS_TIMING_INFO, ast_frfree, ast_set2_flag, ast_test_flag, ast_tvadd(), ast_tvsub(), ast_frame::delivery, f, framein(), ast_frame::frametype, len, ast_frame::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(), conf_run(), and process_ast_dsp().
00316 { 00317 struct ast_trans_pvt *p = path; 00318 struct ast_frame *out = f; 00319 struct timeval delivery; 00320 int has_timing_info; 00321 long ts; 00322 long len; 00323 int seqno; 00324 00325 has_timing_info = ast_test_flag(f, AST_FRFLAG_HAS_TIMING_INFO); 00326 ts = f->ts; 00327 len = f->len; 00328 seqno = f->seqno; 00329 00330 /* XXX hmmm... check this below */ 00331 if (!ast_tvzero(f->delivery)) { 00332 if (!ast_tvzero(path->nextin)) { 00333 /* Make sure this is in line with what we were expecting */ 00334 if (!ast_tveq(path->nextin, f->delivery)) { 00335 /* The time has changed between what we expected and this 00336 most recent time on the new packet. If we have a 00337 valid prediction adjust our output time appropriately */ 00338 if (!ast_tvzero(path->nextout)) { 00339 path->nextout = ast_tvadd(path->nextout, 00340 ast_tvsub(f->delivery, path->nextin)); 00341 } 00342 path->nextin = f->delivery; 00343 } 00344 } else { 00345 /* This is our first pass. Make sure the timing looks good */ 00346 path->nextin = f->delivery; 00347 path->nextout = f->delivery; 00348 } 00349 /* Predict next incoming sample */ 00350 path->nextin = ast_tvadd(path->nextin, ast_samp2tv(f->samples, ast_format_rate(f->subclass))); 00351 } 00352 delivery = f->delivery; 00353 for ( ; out && p ; p = p->next) { 00354 framein(p, out); 00355 if (out != f) 00356 ast_frfree(out); 00357 out = p->t->frameout(p); 00358 } 00359 if (consume) 00360 ast_frfree(f); 00361 if (out == NULL) 00362 return NULL; 00363 /* we have a frame, play with times */ 00364 if (!ast_tvzero(delivery)) { 00365 /* Regenerate prediction after a discontinuity */ 00366 if (ast_tvzero(path->nextout)) 00367 path->nextout = ast_tvnow(); 00368 00369 /* Use next predicted outgoing timestamp */ 00370 out->delivery = path->nextout; 00371 00372 /* Predict next outgoing timestamp from samples in this 00373 frame. */ 00374 path->nextout = ast_tvadd(path->nextout, ast_samp2tv(out->samples, ast_format_rate(out->subclass))); 00375 } else { 00376 out->delivery = ast_tv(0, 0); 00377 ast_set2_flag(out, has_timing_info, AST_FRFLAG_HAS_TIMING_INFO); 00378 if (has_timing_info) { 00379 out->ts = ts; 00380 out->len = len; 00381 out->seqno = seqno; 00382 } 00383 } 00384 /* Invalidate prediction if we're entering a silence period */ 00385 if (out->frametype == AST_FRAME_CNG) 00386 path->nextout = ast_tv(0, 0); 00387 return out; 00388 }
unsigned int ast_translate_available_formats | ( | unsigned int | dest, | |
unsigned int | src | |||
) |
Mask off unavailable formats from a format bitmask.
dest | possible destination formats | |
src | source formats |
Note that 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 894 of file translate.c.
References AST_FORMAT_AUDIO_MASK, AST_FORMAT_MAX_AUDIO, AST_FORMAT_MAX_VIDEO, AST_FORMAT_VIDEO_MASK, AST_LIST_LOCK, AST_LIST_UNLOCK, powerof(), and tr_matrix.
Referenced by sip_call().
00895 { 00896 unsigned int res = dest; 00897 unsigned int x; 00898 unsigned int src_audio = src & AST_FORMAT_AUDIO_MASK; 00899 unsigned int src_video = src & AST_FORMAT_VIDEO_MASK; 00900 00901 /* if we don't have a source format, we just have to try all 00902 possible destination formats */ 00903 if (!src) 00904 return dest; 00905 00906 /* If we have a source audio format, get its format index */ 00907 if (src_audio) 00908 src_audio = powerof(src_audio); 00909 00910 /* If we have a source video format, get its format index */ 00911 if (src_video) 00912 src_video = powerof(src_video); 00913 00914 AST_LIST_LOCK(&translators); 00915 00916 /* For a given source audio format, traverse the list of 00917 known audio formats to determine whether there exists 00918 a translation path from the source format to the 00919 destination format. */ 00920 for (x = 1; src_audio && x < AST_FORMAT_MAX_AUDIO; x <<= 1) { 00921 /* if this is not a desired format, nothing to do */ 00922 if (!dest & x) 00923 continue; 00924 00925 /* if the source is supplying this format, then 00926 we can leave it in the result */ 00927 if (src & x) 00928 continue; 00929 00930 /* if we don't have a translation path from the src 00931 to this format, remove it from the result */ 00932 if (!tr_matrix[src_audio][powerof(x)].step) { 00933 res &= ~x; 00934 continue; 00935 } 00936 00937 /* now check the opposite direction */ 00938 if (!tr_matrix[powerof(x)][src_audio].step) 00939 res &= ~x; 00940 } 00941 00942 /* For a given source video format, traverse the list of 00943 known video formats to determine whether there exists 00944 a translation path from the source format to the 00945 destination format. */ 00946 for (; src_video && x < AST_FORMAT_MAX_VIDEO; x <<= 1) { 00947 /* if this is not a desired format, nothing to do */ 00948 if (!dest & x) 00949 continue; 00950 00951 /* if the source is supplying this format, then 00952 we can leave it in the result */ 00953 if (src & x) 00954 continue; 00955 00956 /* if we don't have a translation path from the src 00957 to this format, remove it from the result */ 00958 if (!tr_matrix[src_video][powerof(x)].step) { 00959 res &= ~x; 00960 continue; 00961 } 00962 00963 /* now check the opposite direction */ 00964 if (!tr_matrix[powerof(x)][src_video].step) 00965 res &= ~x; 00966 } 00967 00968 AST_LIST_UNLOCK(&translators); 00969 00970 return res; 00971 }
void ast_translate_frame_freed | ( | struct ast_frame * | fr | ) |
Hint that a frame from a translator has been freed.
This is sort of a hack. This function gets called when ast_frame_free() gets called on a frame that has the AST_FRFLAG_FROM_TRANSLATOR flag set. This is because it is possible for a translation path to be destroyed while a frame from a translator is still in use. Specifically, this happens if a masquerade happens after a call to ast_read() but before the frame is done being processed, since the frame processing is generally done without the channel lock held.
Definition at line 973 of file translate.c.
References ast_clear_flag, AST_FRFLAG_FROM_TRANSLATOR, destroy(), f, and ast_trans_pvt::pvt.
Referenced by ast_frame_free().
00974 { 00975 struct ast_trans_pvt *pvt; 00976 00977 ast_clear_flag(fr, AST_FRFLAG_FROM_TRANSLATOR); 00978 00979 pvt = (struct ast_trans_pvt *) (((char *) fr) - offsetof(struct ast_trans_pvt, f)); 00980 00981 if (pvt->datalen != -1) 00982 return; 00983 00984 destroy(pvt); 00985 }
unsigned int ast_translate_path_steps | ( | unsigned int | dest, | |
unsigned int | src | |||
) |
Returns the number of steps required to convert from 'src' to 'dest'.
dest | destination format | |
src | source format |
Definition at line 872 of file translate.c.
References AST_LIST_LOCK, AST_LIST_UNLOCK, ast_log(), LOG_WARNING, translator_path::multistep, powerof(), and tr_matrix.
Referenced by ast_channel_make_compatible().
00873 { 00874 unsigned int res = -1; 00875 00876 /* convert bitwise format numbers into array indices */ 00877 src = powerof(src); 00878 dest = powerof(dest); 00879 00880 if (src == -1 || dest == -1) { 00881 ast_log(LOG_WARNING, "No translator path: (%s codec is not valid)\n", src == -1 ? "starting" : "ending"); 00882 return -1; 00883 } 00884 AST_LIST_LOCK(&translators); 00885 00886 if (tr_matrix[src][dest].step) 00887 res = tr_matrix[src][dest].multistep + 1; 00888 00889 AST_LIST_UNLOCK(&translators); 00890 00891 return res; 00892 }
void ast_translator_activate | ( | struct ast_translator * | t | ) |
Activate a previously deactivated translator.
t | translator to activate |
Definition at line 808 of file translate.c.
References AST_LIST_LOCK, AST_LIST_UNLOCK, rebuild_matrix(), and t.
00809 { 00810 AST_LIST_LOCK(&translators); 00811 t->active = 1; 00812 rebuild_matrix(0); 00813 AST_LIST_UNLOCK(&translators); 00814 }
int ast_translator_best_choice | ( | int * | dsts, | |
int * | srcs | |||
) |
Chooses the best translation path.
Given a list of sources, and a designed destination format, which should I choose?
Definition at line 825 of file translate.c.
References AST_FORMAT_AUDIO_MASK, AST_LIST_LOCK, AST_LIST_UNLOCK, ast_translator::cost, translator_path::cost, MAX_AUDIO_FORMAT, translator_path::multistep, and tr_matrix.
Referenced by ast_channel_make_compatible(), ast_request(), iax2_request(), and set_format().
00826 { 00827 int x,y; 00828 int best = -1; 00829 int bestdst = 0; 00830 int cur, cursrc; 00831 int besttime = INT_MAX; 00832 int beststeps = INT_MAX; 00833 int common = ((*dst) & (*srcs)) & AST_FORMAT_AUDIO_MASK; /* are there common formats ? */ 00834 00835 if (common) { /* yes, pick one and return */ 00836 for (cur = 1, y = 0; y <= MAX_AUDIO_FORMAT; cur <<= 1, y++) { 00837 if (cur & common) /* guaranteed to find one */ 00838 break; 00839 } 00840 /* We are done, this is a common format to both. */ 00841 *srcs = *dst = cur; 00842 return 0; 00843 } else { /* No, we will need to translate */ 00844 AST_LIST_LOCK(&translators); 00845 for (cur = 1, y = 0; y <= MAX_AUDIO_FORMAT; cur <<= 1, y++) { 00846 if (! (cur & *dst)) 00847 continue; 00848 for (cursrc = 1, x = 0; x <= MAX_AUDIO_FORMAT; cursrc <<= 1, x++) { 00849 if (!(*srcs & cursrc) || !tr_matrix[x][y].step || 00850 tr_matrix[x][y].cost > besttime) 00851 continue; /* not existing or no better */ 00852 if (tr_matrix[x][y].cost < besttime || 00853 tr_matrix[x][y].multistep < beststeps) { 00854 /* better than what we have so far */ 00855 best = cursrc; 00856 bestdst = cur; 00857 besttime = tr_matrix[x][y].cost; 00858 beststeps = tr_matrix[x][y].multistep; 00859 } 00860 } 00861 } 00862 AST_LIST_UNLOCK(&translators); 00863 if (best > -1) { 00864 *srcs = best; 00865 *dst = bestdst; 00866 best = 0; 00867 } 00868 return best; 00869 } 00870 }
struct ast_trans_pvt* ast_translator_build_path | ( | int | dest, | |
int | source | |||
) |
Builds a translator path Build a path (possibly NULL) from source to dest.
dest | destination format | |
source | source format |
Definition at line 270 of file translate.c.
References ast_getformatname(), AST_LIST_LOCK, AST_LIST_UNLOCK, ast_log(), ast_translator_free_path(), ast_translator::dstfmt, LOG_WARNING, newpvt(), ast_trans_pvt::next, ast_trans_pvt::nextin, ast_trans_pvt::nextout, powerof(), translator_path::step, t, ast_trans_pvt::t, and tr_matrix.
Referenced by ast_audiohook_read_frame(), ast_slinfactory_feed(), ast_writestream(), audio_audiohook_write_list(), conf_run(), misdn_set_opt_exec(), read_config(), and set_format().
00271 { 00272 struct ast_trans_pvt *head = NULL, *tail = NULL; 00273 00274 source = powerof(source); 00275 dest = powerof(dest); 00276 00277 if (source == -1 || dest == -1) { 00278 ast_log(LOG_WARNING, "No translator path: (%s codec is not valid)\n", source == -1 ? "starting" : "ending"); 00279 return NULL; 00280 } 00281 00282 AST_LIST_LOCK(&translators); 00283 00284 while (source != dest) { 00285 struct ast_trans_pvt *cur; 00286 struct ast_translator *t = tr_matrix[source][dest].step; 00287 if (!t) { 00288 ast_log(LOG_WARNING, "No translator path from %s to %s\n", 00289 ast_getformatname(source), ast_getformatname(dest)); 00290 AST_LIST_UNLOCK(&translators); 00291 return NULL; 00292 } 00293 if (!(cur = newpvt(t))) { 00294 ast_log(LOG_WARNING, "Failed to build translator step from %d to %d\n", source, dest); 00295 if (head) 00296 ast_translator_free_path(head); 00297 AST_LIST_UNLOCK(&translators); 00298 return NULL; 00299 } 00300 if (!head) 00301 head = cur; 00302 else 00303 tail->next = cur; 00304 tail = cur; 00305 cur->nextin = cur->nextout = ast_tv(0, 0); 00306 /* Keep going if this isn't the final destination */ 00307 source = cur->t->dstfmt; 00308 } 00309 00310 AST_LIST_UNLOCK(&translators); 00311 return head; 00312 }
void ast_translator_deactivate | ( | struct ast_translator * | t | ) |
Deactivate a translator.
t | translator to deactivate |
Definition at line 816 of file translate.c.
References AST_LIST_LOCK, AST_LIST_UNLOCK, rebuild_matrix(), and t.
00817 { 00818 AST_LIST_LOCK(&translators); 00819 t->active = 0; 00820 rebuild_matrix(0); 00821 AST_LIST_UNLOCK(&translators); 00822 }
void ast_translator_free_path | ( | struct ast_trans_pvt * | tr | ) |
Frees a translator path Frees the given translator path structure.
tr | translator path to get rid of |
Definition at line 260 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_free(), ast_closestream(), ast_slinfactory_destroy(), ast_slinfactory_feed(), ast_slinfactory_flush(), ast_translator_build_path(), ast_writestream(), audio_audiohook_write_list(), cl_dequeue_chan(), conf_free(), free_translation(), and set_format().
00261 { 00262 struct ast_trans_pvt *pn = p; 00263 while ( (p = pn) ) { 00264 pn = p->next; 00265 destroy(p); 00266 } 00267 }
int ast_unregister_translator | ( | struct ast_translator * | t | ) |
Unregister a translator Unregisters the given tranlator.
t | translator to unregister |
Definition at line 782 of file translate.c.
References ast_getformatname(), AST_LIST_LOCK, AST_LIST_REMOVE_CURRENT, AST_LIST_TRAVERSE_SAFE_BEGIN, AST_LIST_TRAVERSE_SAFE_END, AST_LIST_UNLOCK, ast_verbose(), COLOR_BLACK, COLOR_MAGENTA, option_verbose, rebuild_matrix(), t, term_color(), and VERBOSE_PREFIX_2.
Referenced by drop_translator(), load_module(), unload_module(), and unregister_translators().
00783 { 00784 char tmp[80]; 00785 struct ast_translator *u; 00786 int found = 0; 00787 00788 AST_LIST_LOCK(&translators); 00789 AST_LIST_TRAVERSE_SAFE_BEGIN(&translators, u, list) { 00790 if (u == t) { 00791 AST_LIST_REMOVE_CURRENT(&translators, list); 00792 if (option_verbose > 1) 00793 ast_verbose(VERBOSE_PREFIX_2 "Unregistered translator '%s' from format %s to %s\n", term_color(tmp, t->name, COLOR_MAGENTA, COLOR_BLACK, sizeof(tmp)), ast_getformatname(1 << t->srcfmt), ast_getformatname(1 << t->dstfmt)); 00794 found = 1; 00795 break; 00796 } 00797 } 00798 AST_LIST_TRAVERSE_SAFE_END; 00799 00800 if (found) 00801 rebuild_matrix(0); 00802 00803 AST_LIST_UNLOCK(&translators); 00804 00805 return (u ? 0 : -1); 00806 }
static void calc_cost | ( | struct ast_translator * | t, | |
int | seconds | |||
) | [static] |
compute the cost of a single translation step
Definition at line 391 of file translate.c.
References ast_format_rate(), ast_frfree, ast_log(), destroy(), f, framein(), LOG_WARNING, newpvt(), and t.
Referenced by __ast_register_translator(), and rebuild_matrix().
00392 { 00393 int num_samples = 0; 00394 struct ast_trans_pvt *pvt; 00395 struct timeval start; 00396 int cost; 00397 int out_rate = ast_format_rate(t->dstfmt); 00398 00399 if (!seconds) 00400 seconds = 1; 00401 00402 /* If they don't make samples, give them a terrible score */ 00403 if (!t->sample) { 00404 ast_log(LOG_WARNING, "Translator '%s' does not produce sample frames.\n", t->name); 00405 t->cost = 99999; 00406 return; 00407 } 00408 00409 pvt = newpvt(t); 00410 if (!pvt) { 00411 ast_log(LOG_WARNING, "Translator '%s' appears to be broken and will probably fail.\n", t->name); 00412 t->cost = 99999; 00413 return; 00414 } 00415 00416 start = ast_tvnow(); 00417 00418 /* Call the encoder until we've processed the required number of samples */ 00419 while (num_samples < seconds * out_rate) { 00420 struct ast_frame *f = t->sample(); 00421 if (!f) { 00422 ast_log(LOG_WARNING, "Translator '%s' failed to produce a sample frame.\n", t->name); 00423 destroy(pvt); 00424 t->cost = 99999; 00425 return; 00426 } 00427 framein(pvt, f); 00428 ast_frfree(f); 00429 while ((f = t->frameout(pvt))) { 00430 num_samples += f->samples; 00431 ast_frfree(f); 00432 } 00433 } 00434 00435 cost = ast_tvdiff_ms(ast_tvnow(), start); 00436 00437 destroy(pvt); 00438 00439 t->cost = cost / seconds; 00440 00441 if (!t->cost) 00442 t->cost = 1; 00443 }
static struct ast_frame* default_frameout | ( | struct ast_trans_pvt * | pvt | ) | [static] |
Definition at line 253 of file translate.c.
References ast_trans_frameout().
Referenced by __ast_register_translator().
00254 { 00255 return ast_trans_frameout(pvt, 0, 0); 00256 }
static void destroy | ( | struct ast_trans_pvt * | pvt | ) | [static] |
Definition at line 139 of file translate.c.
References AST_FRFLAG_FROM_TRANSLATOR, ast_module_unref(), ast_test_flag, ast_trans_pvt::datalen, ast_trans_pvt::f, free, ast_trans_pvt::t, and t.
Referenced by ast_translate_frame_freed(), ast_translator_free_path(), and calc_cost().
00140 { 00141 struct ast_translator *t = pvt->t; 00142 00143 if (ast_test_flag(&pvt->f, AST_FRFLAG_FROM_TRANSLATOR)) { 00144 /* If this flag is still set, that means that the translation path has 00145 * been torn down, while we still have a frame out there being used. 00146 * When ast_frfree() gets called on that frame, this ast_trans_pvt 00147 * will get destroyed, too. */ 00148 00149 /* Set the magic hint that this has been requested to be destroyed. */ 00150 pvt->datalen = -1; 00151 00152 return; 00153 } 00154 00155 if (t->destroy) 00156 t->destroy(pvt); 00157 free(pvt); 00158 ast_module_unref(t->module); 00159 }
static int framein | ( | struct ast_trans_pvt * | pvt, | |
struct ast_frame * | f | |||
) | [static] |
framein wrapper, deals with plc and bound checks.
Definition at line 162 of file translate.c.
References ast_copy_flags, AST_FRFLAG_HAS_TIMING_INFO, ast_log(), ast_translator::buffer_samples, ast_trans_pvt::datalen, f, ast_trans_pvt::f, ast_translator::framein, ast_frame::len, LOG_WARNING, ast_translator::name, ast_translator::native_plc, ast_trans_pvt::outbuf, ast_trans_pvt::plc, plc_fillin(), plc_rx(), ast_translator::plc_samples, ast_trans_pvt::samples, ast_frame::seqno, ast_trans_pvt::t, and ast_frame::ts.
Referenced by ast_translate(), and calc_cost().
00163 { 00164 int16_t *dst = (int16_t *)pvt->outbuf; 00165 int ret; 00166 int samples = pvt->samples; /* initial value */ 00167 00168 /* Copy the last in jb timing info to the pvt */ 00169 ast_copy_flags(&pvt->f, f, AST_FRFLAG_HAS_TIMING_INFO); 00170 pvt->f.ts = f->ts; 00171 pvt->f.len = f->len; 00172 pvt->f.seqno = f->seqno; 00173 00174 if (f->samples == 0) { 00175 ast_log(LOG_WARNING, "no samples for %s\n", pvt->t->name); 00176 } 00177 if (pvt->t->buffer_samples) { /* do not pass empty frames to callback */ 00178 if (f->datalen == 0) { /* perform PLC with nominal framesize of 20ms/160 samples */ 00179 if (pvt->plc) { 00180 int l = pvt->t->plc_samples; 00181 if (pvt->samples + l > pvt->t->buffer_samples) { 00182 ast_log(LOG_WARNING, "Out of buffer space\n"); 00183 return -1; 00184 } 00185 l = plc_fillin(pvt->plc, dst + pvt->samples, l); 00186 pvt->samples += l; 00187 pvt->datalen = pvt->samples * 2; /* SLIN has 2bytes for 1sample */ 00188 } 00189 /* We don't want generic PLC. If the codec has native PLC, then do that */ 00190 if (!pvt->t->native_plc) 00191 return 0; 00192 } 00193 if (pvt->samples + f->samples > pvt->t->buffer_samples) { 00194 ast_log(LOG_WARNING, "Out of buffer space\n"); 00195 return -1; 00196 } 00197 } 00198 /* we require a framein routine, wouldn't know how to do 00199 * it otherwise. 00200 */ 00201 ret = pvt->t->framein(pvt, f); 00202 /* possibly store data for plc */ 00203 if (!ret && pvt->plc) { 00204 int l = pvt->t->plc_samples; 00205 if (pvt->samples < l) 00206 l = pvt->samples; 00207 plc_rx(pvt->plc, dst + pvt->samples - l, l); 00208 } 00209 /* diagnostic ... */ 00210 if (pvt->samples == samples) 00211 ast_log(LOG_WARNING, "%s did not update samples %d\n", 00212 pvt->t->name, pvt->samples); 00213 return ret; 00214 }
static void* newpvt | ( | struct ast_translator * | t | ) | [static] |
Allocate the descriptor, required outbuf space, and possibly also plc and desc.
Definition at line 99 of file translate.c.
References ast_calloc, AST_FRIENDLY_OFFSET, ast_module_ref(), free, len, ast_trans_pvt::pvt, and t.
Referenced by ast_translator_build_path(), and calc_cost().
00100 { 00101 struct ast_trans_pvt *pvt; 00102 int len; 00103 int useplc = t->plc_samples > 0 && t->useplc; /* cache, because it can change on the fly */ 00104 char *ofs; 00105 00106 /* 00107 * compute the required size adding private descriptor, 00108 * plc, buffer, AST_FRIENDLY_OFFSET. 00109 */ 00110 len = sizeof(*pvt) + t->desc_size; 00111 if (useplc) 00112 len += sizeof(plc_state_t); 00113 if (t->buf_size) 00114 len += AST_FRIENDLY_OFFSET + t->buf_size; 00115 pvt = ast_calloc(1, len); 00116 if (!pvt) 00117 return NULL; 00118 pvt->t = t; 00119 ofs = (char *)(pvt + 1); /* pointer to data space */ 00120 if (t->desc_size) { /* first comes the descriptor */ 00121 pvt->pvt = ofs; 00122 ofs += t->desc_size; 00123 } 00124 if (useplc) { /* then plc state */ 00125 pvt->plc = (plc_state_t *)ofs; 00126 ofs += sizeof(plc_state_t); 00127 } 00128 if (t->buf_size) /* finally buffer and header */ 00129 pvt->outbuf = ofs + AST_FRIENDLY_OFFSET; 00130 /* call local init routine, if present */ 00131 if (t->newpvt && t->newpvt(pvt)) { 00132 free(pvt); 00133 return NULL; 00134 } 00135 ast_module_ref(t->module); 00136 return pvt; 00137 }
static force_inline int powerof | ( | unsigned int | d | ) | [static] |
returns the index of the lowest bit set
Definition at line 79 of file translate.c.
References ast_log(), and LOG_WARNING.
Referenced by __ast_register_translator(), agents_show(), ast_translate_available_formats(), ast_translate_path_steps(), and ast_translator_build_path().
00080 { 00081 int x = ffs(d); 00082 00083 if (x) 00084 return x - 1; 00085 00086 ast_log(LOG_WARNING, "No bits set? %d\n", d); 00087 00088 return -1; 00089 }
static void rebuild_matrix | ( | int | samples | ) | [static] |
rebuild a translation matrix.
Definition at line 449 of file translate.c.
References ast_getformatname(), AST_LIST_TRAVERSE, ast_log(), calc_cost(), ast_translator::cost, LOG_DEBUG, MAX_FORMAT, option_debug, t, and tr_matrix.
Referenced by __ast_register_translator(), ast_translator_activate(), ast_translator_deactivate(), ast_unregister_translator(), show_translation(), and show_translation_deprecated().
00450 { 00451 struct ast_translator *t; 00452 int x; /* source format index */ 00453 int y; /* intermediate format index */ 00454 int z; /* destination format index */ 00455 00456 if (option_debug) 00457 ast_log(LOG_DEBUG, "Resetting translation matrix\n"); 00458 00459 bzero(tr_matrix, sizeof(tr_matrix)); 00460 00461 /* first, compute all direct costs */ 00462 AST_LIST_TRAVERSE(&translators, t, list) { 00463 if (!t->active) 00464 continue; 00465 00466 x = t->srcfmt; 00467 z = t->dstfmt; 00468 00469 if (samples) 00470 calc_cost(t, samples); 00471 00472 if (!tr_matrix[x][z].step || t->cost < tr_matrix[x][z].cost) { 00473 tr_matrix[x][z].step = t; 00474 tr_matrix[x][z].cost = t->cost; 00475 } 00476 } 00477 00478 /* 00479 * For each triple x, y, z of distinct formats, check if there is 00480 * a path from x to z through y which is cheaper than what is 00481 * currently known, and in case, update the matrix. 00482 * Repeat until the matrix is stable. 00483 */ 00484 for (;;) { 00485 int changed = 0; 00486 for (x = 0; x < MAX_FORMAT; x++) { /* source format */ 00487 for (y=0; y < MAX_FORMAT; y++) { /* intermediate format */ 00488 if (x == y) /* skip ourselves */ 00489 continue; 00490 00491 for (z=0; z<MAX_FORMAT; z++) { /* dst format */ 00492 int newcost; 00493 00494 if (z == x || z == y) /* skip null conversions */ 00495 continue; 00496 if (!tr_matrix[x][y].step) /* no path from x to y */ 00497 continue; 00498 if (!tr_matrix[y][z].step) /* no path from y to z */ 00499 continue; 00500 newcost = tr_matrix[x][y].cost + tr_matrix[y][z].cost; 00501 if (tr_matrix[x][z].step && newcost >= tr_matrix[x][z].cost) 00502 continue; /* x->y->z is more expensive than 00503 * the existing path */ 00504 /* ok, we can get from x to z via y with a cost that 00505 is the sum of the transition from x to y and 00506 from y to z */ 00507 00508 tr_matrix[x][z].step = tr_matrix[x][y].step; 00509 tr_matrix[x][z].cost = newcost; 00510 tr_matrix[x][z].multistep = 1; 00511 if (option_debug) 00512 ast_log(LOG_DEBUG, "Discovered %d cost path from %s to %s, via %d\n", tr_matrix[x][z].cost, ast_getformatname(x), ast_getformatname(z), y); 00513 changed++; 00514 } 00515 } 00516 } 00517 if (!changed) 00518 break; 00519 } 00520 }
static int show_translation | ( | int | fd, | |
int | argc, | |||
char * | argv[] | |||
) | [static] |
Definition at line 593 of file translate.c.
References ast_build_string(), ast_cli(), ast_getformatname(), AST_LIST_LOCK, AST_LIST_UNLOCK, ast_translator::cost, MAX_RECALC, rebuild_matrix(), RESULT_SHOWUSAGE, RESULT_SUCCESS, SHOW_TRANS, and tr_matrix.
00594 { 00595 int x, y, z; 00596 int curlen = 0, longest = 0; 00597 00598 if (argc > 5) 00599 return RESULT_SHOWUSAGE; 00600 00601 AST_LIST_LOCK(&translators); 00602 00603 if (argv[3] && !strcasecmp(argv[3], "recalc")) { 00604 z = argv[4] ? atoi(argv[4]) : 1; 00605 00606 if (z <= 0) { 00607 ast_cli(fd, " C'mon let's be serious here... defaulting to 1.\n"); 00608 z = 1; 00609 } 00610 00611 if (z > MAX_RECALC) { 00612 ast_cli(fd, " Maximum limit of recalc exceeded by %d, truncating value to %d\n", z - MAX_RECALC, MAX_RECALC); 00613 z = MAX_RECALC; 00614 } 00615 ast_cli(fd, " Recalculating Codec Translation (number of sample seconds: %d)\n\n", z); 00616 rebuild_matrix(z); 00617 } 00618 00619 ast_cli(fd, " Translation times between formats (in milliseconds) for one second of data\n"); 00620 ast_cli(fd, " Source Format (Rows) Destination Format (Columns)\n\n"); 00621 /* Get the length of the longest (usable?) codec name, so we know how wide the left side should be */ 00622 for (x = 0; x < SHOW_TRANS; x++) { 00623 curlen = strlen(ast_getformatname(1 << (x))); 00624 if (curlen > longest) 00625 longest = curlen; 00626 } 00627 for (x = -1; x < SHOW_TRANS; x++) { 00628 char line[120]; 00629 char *buf = line; 00630 size_t left = sizeof(line) - 1; /* one initial space */ 00631 /* next 2 lines run faster than using ast_build_string() */ 00632 *buf++ = ' '; 00633 *buf = '\0'; 00634 for (y = -1; y < SHOW_TRANS; y++) { 00635 if (y >= 0) 00636 curlen = strlen(ast_getformatname(1 << (y))); 00637 00638 if (x >= 0 && y >= 0 && tr_matrix[x][y].step) { 00639 /* XXX 999 is a little hackish 00640 We don't want this number being larger than the shortest (or current) codec 00641 For now, that is "gsm" */ 00642 ast_build_string(&buf, &left, "%*d", curlen + 1, tr_matrix[x][y].cost > 999 ? 0 : tr_matrix[x][y].cost); 00643 } else if (x == -1 && y >= 0) { 00644 /* Top row - use a dynamic size */ 00645 ast_build_string(&buf, &left, "%*s", curlen + 1, ast_getformatname(1 << (y)) ); 00646 } else if (y == -1 && x >= 0) { 00647 /* Left column - use a static size. */ 00648 ast_build_string(&buf, &left, "%*s", longest, ast_getformatname(1 << (x)) ); 00649 } else if (x >= 0 && y >= 0) { 00650 ast_build_string(&buf, &left, "%*s", curlen + 1, "-"); 00651 } else { 00652 ast_build_string(&buf, &left, "%*s", longest, ""); 00653 } 00654 } 00655 ast_build_string(&buf, &left, "\n"); 00656 ast_cli(fd, line); 00657 } 00658 AST_LIST_UNLOCK(&translators); 00659 return RESULT_SUCCESS; 00660 }
static int show_translation_deprecated | ( | int | fd, | |
int | argc, | |||
char * | argv[] | |||
) | [static] |
CLI "show translation" command handler.
Definition at line 523 of file translate.c.
References ast_build_string(), ast_cli(), ast_getformatname(), AST_LIST_LOCK, AST_LIST_UNLOCK, ast_translator::cost, MAX_RECALC, rebuild_matrix(), RESULT_SHOWUSAGE, RESULT_SUCCESS, SHOW_TRANS, and tr_matrix.
00524 { 00525 #define SHOW_TRANS 13 00526 int x, y, z; 00527 int curlen = 0, longest = 0; 00528 00529 if (argc > 4) 00530 return RESULT_SHOWUSAGE; 00531 00532 AST_LIST_LOCK(&translators); 00533 00534 if (argv[2] && !strcasecmp(argv[2], "recalc")) { 00535 z = argv[3] ? atoi(argv[3]) : 1; 00536 00537 if (z <= 0) { 00538 ast_cli(fd, " C'mon let's be serious here... defaulting to 1.\n"); 00539 z = 1; 00540 } 00541 00542 if (z > MAX_RECALC) { 00543 ast_cli(fd, " Maximum limit of recalc exceeded by %d, truncating value to %d\n", z - MAX_RECALC, MAX_RECALC); 00544 z = MAX_RECALC; 00545 } 00546 ast_cli(fd, " Recalculating Codec Translation (number of sample seconds: %d)\n\n", z); 00547 rebuild_matrix(z); 00548 } 00549 00550 ast_cli(fd, " Translation times between formats (in milliseconds) for one second of data\n"); 00551 ast_cli(fd, " Source Format (Rows) Destination Format (Columns)\n\n"); 00552 /* Get the length of the longest (usable?) codec name, so we know how wide the left side should be */ 00553 for (x = 0; x < SHOW_TRANS; x++) { 00554 curlen = strlen(ast_getformatname(1 << (x))); 00555 if (curlen > longest) 00556 longest = curlen; 00557 } 00558 for (x = -1; x < SHOW_TRANS; x++) { 00559 char line[120]; 00560 char *buf = line; 00561 size_t left = sizeof(line) - 1; /* one initial space */ 00562 /* next 2 lines run faster than using ast_build_string() */ 00563 *buf++ = ' '; 00564 *buf = '\0'; 00565 for (y = -1; y < SHOW_TRANS; y++) { 00566 if (y >= 0) 00567 curlen = strlen(ast_getformatname(1 << (y))); 00568 00569 if (x >= 0 && y >= 0 && tr_matrix[x][y].step) { 00570 /* XXX 999 is a little hackish 00571 We don't want this number being larger than the shortest (or current) codec 00572 For now, that is "gsm" */ 00573 ast_build_string(&buf, &left, "%*d", curlen + 1, tr_matrix[x][y].cost > 999 ? 0 : tr_matrix[x][y].cost); 00574 } else if (x == -1 && y >= 0) { 00575 /* Top row - use a dynamic size */ 00576 ast_build_string(&buf, &left, "%*s", curlen + 1, ast_getformatname(1 << (y)) ); 00577 } else if (y == -1 && x >= 0) { 00578 /* Left column - use a static size. */ 00579 ast_build_string(&buf, &left, "%*s", longest, ast_getformatname(1 << (x)) ); 00580 } else if (x >= 0 && y >= 0) { 00581 ast_build_string(&buf, &left, "%*s", curlen + 1, "-"); 00582 } else { 00583 ast_build_string(&buf, &left, "%*s", longest, ""); 00584 } 00585 } 00586 ast_build_string(&buf, &left, "\n"); 00587 ast_cli(fd, line); 00588 } 00589 AST_LIST_UNLOCK(&translators); 00590 return RESULT_SUCCESS; 00591 }
struct ast_cli_entry cli_show_translation_deprecated [static] |
Initial value:
{ { "show", "translation", NULL }, show_translation_deprecated, NULL, NULL }
Definition at line 669 of file translate.c.
struct ast_cli_entry cli_translate[] [static] |
Initial value:
{ { { "core", "show", "translation", NULL }, show_translation, "Display translation matrix", show_trans_usage, NULL, &cli_show_translation_deprecated }, }
Definition at line 674 of file translate.c.
Referenced by __ast_register_translator().
char show_trans_usage[] [static] |
Definition at line 662 of file translate.c.
struct translator_path tr_matrix[MAX_FORMAT][MAX_FORMAT] [static] |
a matrix that, for any pair of supported formats, indicates the total cost of translation and the first step. The full path can be reconstricted iterating on the matrix until step->dstfmt == desired_format.
Array indexes are 'src' and 'dest', in that order.
Note: the lock in the 'translators' list is also used to protect this structure.
Definition at line 70 of file translate.c.
Referenced by ast_translate_available_formats(), ast_translate_path_steps(), ast_translator_best_choice(), ast_translator_build_path(), rebuild_matrix(), show_translation(), and show_translation_deprecated().