00001 /* 00002 * Asterisk -- An open source telephony toolkit. 00003 * 00004 * Copyright (C) 1999 - 2006, Digium, Inc. 00005 * 00006 * Mark Spencer <markster@digium.com> 00007 * 00008 * See http://www.asterisk.org for more information about 00009 * the Asterisk project. Please do not directly contact 00010 * any of the maintainers of this project for assistance; 00011 * the project provides a web site, mailing lists and IRC 00012 * channels for your use. 00013 * 00014 * This program is free software, distributed under the terms of 00015 * the GNU General Public License Version 2. See the LICENSE file 00016 * at the top of the source tree. 00017 */ 00018 00019 /*! \file 00020 * \brief Support for translation of data formats. 00021 */ 00022 00023 #ifndef _ASTERISK_TRANSLATE_H 00024 #define _ASTERISK_TRANSLATE_H 00025 00026 #define MAX_AUDIO_FORMAT 15 /* Do not include video here */ 00027 #define MAX_FORMAT 32 /* Do include video here */ 00028 00029 #if defined(__cplusplus) || defined(c_plusplus) 00030 extern "C" { 00031 #endif 00032 00033 #if 1 /* need lots of stuff... */ 00034 #include "asterisk/frame.h" 00035 #include "asterisk/plc.h" 00036 #include "asterisk/linkedlists.h" 00037 // XXX #include "asterisk/module.h" 00038 #endif 00039 00040 struct ast_trans_pvt; /* declared below */ 00041 00042 /*! \brief 00043 * Descriptor of a translator. Name, callbacks, and various options 00044 * related to run-time operation (size of buffers, auxiliary 00045 * descriptors, etc). 00046 * 00047 * A coded registers itself by filling the relevant fields 00048 * of a structure and passing it as an argument to 00049 * ast_register_translator(). The structure should not be 00050 * modified after a successful registration, and its address 00051 * must be used as an argument to ast_unregister_translator(). 00052 * 00053 * As a minimum, a translator should supply name, srcfmt and dstfmt, 00054 * the required buf_size (in bytes) and buffer_samples (in samples), 00055 * and a few callbacks (framein, frameout, sample). 00056 * The outbuf is automatically prepended by AST_FRIENDLY_OFFSET 00057 * spare bytes so generic routines can place data in there. 00058 * 00059 * Note, the translator is not supposed to do any memory allocation 00060 * or deallocation, nor any locking, because all of this is done in 00061 * the generic code. 00062 * 00063 */ 00064 struct ast_translator { 00065 const char name[80]; /*!< Name of translator */ 00066 int srcfmt; /*!< Source format (note: bit position, 00067 converted to index during registration) */ 00068 int dstfmt; /*!< Destination format (note: bit position, 00069 converted to index during registration) */ 00070 00071 int (*newpvt)(struct ast_trans_pvt *); /*!< initialize private data 00072 associated with the translator */ 00073 00074 int (*framein)(struct ast_trans_pvt *pvt, struct ast_frame *in); 00075 /*!< Input frame callback. Store 00076 (and possibly convert) input frame. */ 00077 00078 struct ast_frame * (*frameout)(struct ast_trans_pvt *pvt); 00079 /*!< Output frame callback. Generate a frame 00080 with outbuf content. */ 00081 00082 void (*destroy)(struct ast_trans_pvt *pvt); 00083 /*!< cleanup private data, if needed 00084 (often unnecessary). */ 00085 00086 struct ast_frame * (*sample)(void); /*!< Generate an example frame */ 00087 00088 /*! \brief size of outbuf, in samples. Leave it 0 if you want the framein 00089 * callback deal with the frame. Set it appropriately if you 00090 * want the code to checks if the incoming frame fits the 00091 * outbuf (this is e.g. required for plc). 00092 */ 00093 int buffer_samples; /*< size of outbuf, in samples */ 00094 00095 /*! \brief size of outbuf, in bytes. Mandatory. The wrapper code will also 00096 * allocate an AST_FRIENDLY_OFFSET space before. 00097 */ 00098 int buf_size; 00099 00100 int desc_size; /*!< size of private descriptor in pvt->pvt, if any */ 00101 int plc_samples; /* Unused. Kept for ABI purposes */ 00102 int useplc; /* Unused. Kept for ABI purposes */ 00103 int native_plc; /*!< true if the translator can do native plc */ 00104 00105 struct ast_module *module; /* opaque reference to the parent module */ 00106 00107 int cost; /*!< Cost in milliseconds for encoding/decoding 1 second of sound */ 00108 int active; /*!< Whether this translator should be used or not */ 00109 AST_LIST_ENTRY(ast_translator) list; /*!< link field */ 00110 }; 00111 00112 /*! \brief 00113 * Default structure for translators, with the basic fields and buffers, 00114 * all allocated as part of the same chunk of memory. The buffer is 00115 * preceded by AST_FRIENDLY_OFFSET bytes in front of the user portion. 00116 * 'buf' points right after this space. 00117 * 00118 * *_framein() routines operate in two ways: 00119 * 1. some convert on the fly and place the data directly in outbuf; 00120 * in this case 'samples' and 'datalen' contain the number of samples 00121 * and number of bytes available in the buffer. 00122 * In this case we can use a generic *_frameout() routine that simply 00123 * takes whatever is there and places it into the output frame. 00124 * 2. others simply store the (unconverted) samples into a working 00125 * buffer, and leave the conversion task to *_frameout(). 00126 * In this case, the intermediate buffer must be in the private 00127 * descriptor, 'datalen' is left to 0, while 'samples' is still 00128 * updated with the number of samples received. 00129 */ 00130 struct ast_trans_pvt { 00131 struct ast_translator *t; 00132 struct ast_frame f; /*!< used in frameout */ 00133 int samples; /*!< samples available in outbuf */ 00134 /*! 00135 * \brief actual space used in outbuf 00136 */ 00137 int datalen; 00138 void *pvt; /*!< more private data, if any */ 00139 char *outbuf; /*!< the useful portion of the buffer */ 00140 plc_state_t *plc; /*!< optional plc pointer */ 00141 struct ast_trans_pvt *next; /*!< next in translator chain */ 00142 struct timeval nextin; 00143 struct timeval nextout; 00144 }; 00145 00146 /*! \brief generic frameout function */ 00147 struct ast_frame *ast_trans_frameout(struct ast_trans_pvt *pvt, 00148 int datalen, int samples); 00149 00150 struct ast_trans_pvt; 00151 00152 /*! 00153 * \brief Register a translator 00154 * This registers a codec translator with asterisk 00155 * \param t populated ast_translator structure 00156 * \param module handle to the module that owns this translator 00157 * \return 0 on success, -1 on failure 00158 */ 00159 int __ast_register_translator(struct ast_translator *t, struct ast_module *module); 00160 #define ast_register_translator(t) __ast_register_translator(t, ast_module_info->self) 00161 00162 /*! 00163 * \brief Unregister a translator 00164 * Unregisters the given tranlator 00165 * \param t translator to unregister 00166 * \return 0 on success, -1 on failure 00167 */ 00168 int ast_unregister_translator(struct ast_translator *t); 00169 00170 /*! 00171 * \brief Activate a previously deactivated translator 00172 * \param t translator to activate 00173 * \return nothing 00174 * 00175 * Enables the specified translator for use. 00176 */ 00177 void ast_translator_activate(struct ast_translator *t); 00178 00179 /*! 00180 * \brief Deactivate a translator 00181 * \param t translator to deactivate 00182 * \return nothing 00183 * 00184 * Disables the specified translator from being used. 00185 */ 00186 void ast_translator_deactivate(struct ast_translator *t); 00187 00188 /*! 00189 * \brief Chooses the best translation path 00190 * 00191 * Given a list of sources, and a designed destination format, which should 00192 * I choose? 00193 * \return Returns 0 on success, -1 if no path could be found. 00194 * \note Modifies dests and srcs in place 00195 */ 00196 int ast_translator_best_choice(int *dsts, int *srcs); 00197 00198 /*! 00199 * \brief Builds a translator path 00200 * Build a path (possibly NULL) from source to dest 00201 * \param dest destination format 00202 * \param source source format 00203 * \return ast_trans_pvt on success, NULL on failure 00204 * */ 00205 struct ast_trans_pvt *ast_translator_build_path(int dest, int source); 00206 00207 /*! 00208 * \brief Frees a translator path 00209 * Frees the given translator path structure 00210 * \param tr translator path to get rid of 00211 */ 00212 void ast_translator_free_path(struct ast_trans_pvt *tr); 00213 00214 /*! 00215 * \brief translates one or more frames 00216 * Apply an input frame into the translator and receive zero or one output frames. Consume 00217 * determines whether the original frame should be freed 00218 * \param tr translator structure to use for translation 00219 * \param f frame to translate 00220 * \param consume Whether or not to free the original frame 00221 * \return an ast_frame of the new translation format on success, NULL on failure 00222 */ 00223 struct ast_frame *ast_translate(struct ast_trans_pvt *tr, struct ast_frame *f, int consume); 00224 00225 /*! 00226 * \brief Returns the number of steps required to convert from 'src' to 'dest'. 00227 * \param dest destination format 00228 * \param src source format 00229 * \return the number of translation steps required, or -1 if no path is available 00230 */ 00231 unsigned int ast_translate_path_steps(unsigned int dest, unsigned int src); 00232 00233 /*! 00234 * \brief Mask off unavailable formats from a format bitmask 00235 * \param dest possible destination formats 00236 * \param src source formats 00237 * \return the destination formats that are available in the source or translatable 00238 * 00239 * The result will include all formats from 'dest' that are either present 00240 * in 'src' or translatable from a format present in 'src'. 00241 * 00242 * Note that only a single audio format and a single video format can be 00243 * present in 'src', or the function will produce unexpected results. 00244 */ 00245 unsigned int ast_translate_available_formats(unsigned int dest, unsigned int src); 00246 00247 #if defined(__cplusplus) || defined(c_plusplus) 00248 } 00249 #endif 00250 00251 #endif /* _ASTERISK_TRANSLATE_H */