Files
asterisk/include/asterisk/translate.h
Matthew Jordan a2c912e997 media formats: re-architect handling of media for performance improvements
In the old times media formats were represented using a bit field. This was
fast but had a few limitations.
 1. Asterisk was limited in how many formats it could handle.
 2. Formats, being a bit field, could not include any attribute information.
    A format was strictly its type, e.g., "this is ulaw".
This was changed in Asterisk 10 (see
https://wiki.asterisk.org/wiki/display/AST/Media+Architecture+Proposal for
notes on that work) which led to the creation of the ast_format structure.
This structure allowed Asterisk to handle attributes and bundle information
with a format.

Additionally, ast_format_cap was created to act as a container for multiple
formats that, together, formed the capability of some entity. Another
mechanism was added to allow logic to be registered which performed format
attribute negotiation. Everywhere throughout the codebase Asterisk was
changed to use this strategy.

Unfortunately, in software, there is no free lunch. These new capabilities
came at a cost.

Performance analysis and profiling showed that we spend an inordinate
amount of time comparing, copying, and generally manipulating formats and
their related structures. Basic prototyping has shown that a reasonably
large performance improvement could be made in this area. This patch is the
result of that project, which overhauled the media format architecture
and its usage in Asterisk to improve performance.

Generally, the new philosophy for handling formats is as follows:
 * The ast_format structure is reference counted. This removed a large amount
   of the memory allocations and copying that was done in prior versions.
 * In order to prevent race conditions while keeping things performant, the
   ast_format structure is immutable by convention and lock-free. Violate this
   tenet at your peril!
 * Because formats are reference counted, codecs are also reference counted.
   The Asterisk core generally provides built-in codecs and caches the
   ast_format structures created to represent them. Generally, to prevent
   inordinate amounts of module reference bumping, codecs and formats can be
   added at run-time but cannot be removed.
 * All compatibility with the bit field representation of codecs/formats has
   been moved to a compatibility API. The primary user of this representation
   is chan_iax2, which must continue to maintain its bit-field usage of formats
   for interoperability concerns.
 * When a format is negotiated with attributes, or when a format cannot be
   represented by one of the cached formats, a new format object is created or
   cloned from an existing format. That format may have the same codec
   underlying it, but is a different format than a version of the format with
   different attributes or without attributes.
 * While formats are reference counted objects, the reference count maintained
   on the format should be manipulated with care. Formats are generally cached
   and will persist for the lifetime of Asterisk and do not explicitly need
   to have their lifetime modified. An exception to this is when the user of a
   format does not know where the format came from *and* the user may outlive
   the provider of the format. This occurs, for example, when a format is read
   from a channel: the channel may have a format with attributes (hence,
   non-cached) and the user of the format may last longer than the channel (if
   the reference to the channel is released prior to the format's reference).

For more information on this work, see the API design notes:
  https://wiki.asterisk.org/wiki/display/AST/Media+Format+Rewrite

Finally, this work was the culmination of a large number of developer's
efforts. Extra thanks goes to Corey Farrell, who took on a large amount of the
work in the Asterisk core, chan_sip, and was an invaluable resource in peer
reviews throughout this project.

There were a substantial number of patches contributed during this work; the
following issues/patch names simply reflect some of the work (and will cause
the release scripts to give attribution to the individuals who work on them).

Reviews:
 https://reviewboard.asterisk.org/r/3814
 https://reviewboard.asterisk.org/r/3808
 https://reviewboard.asterisk.org/r/3805
 https://reviewboard.asterisk.org/r/3803
 https://reviewboard.asterisk.org/r/3801
 https://reviewboard.asterisk.org/r/3798
 https://reviewboard.asterisk.org/r/3800
 https://reviewboard.asterisk.org/r/3794
 https://reviewboard.asterisk.org/r/3793
 https://reviewboard.asterisk.org/r/3792
 https://reviewboard.asterisk.org/r/3791
 https://reviewboard.asterisk.org/r/3790
 https://reviewboard.asterisk.org/r/3789
 https://reviewboard.asterisk.org/r/3788
 https://reviewboard.asterisk.org/r/3787
 https://reviewboard.asterisk.org/r/3786
 https://reviewboard.asterisk.org/r/3784
 https://reviewboard.asterisk.org/r/3783
 https://reviewboard.asterisk.org/r/3778
 https://reviewboard.asterisk.org/r/3774
 https://reviewboard.asterisk.org/r/3775
 https://reviewboard.asterisk.org/r/3772
 https://reviewboard.asterisk.org/r/3761
 https://reviewboard.asterisk.org/r/3754
 https://reviewboard.asterisk.org/r/3753
 https://reviewboard.asterisk.org/r/3751
 https://reviewboard.asterisk.org/r/3750
 https://reviewboard.asterisk.org/r/3748
 https://reviewboard.asterisk.org/r/3747
 https://reviewboard.asterisk.org/r/3746
 https://reviewboard.asterisk.org/r/3742
 https://reviewboard.asterisk.org/r/3740
 https://reviewboard.asterisk.org/r/3739
 https://reviewboard.asterisk.org/r/3738
 https://reviewboard.asterisk.org/r/3737
 https://reviewboard.asterisk.org/r/3736
 https://reviewboard.asterisk.org/r/3734
 https://reviewboard.asterisk.org/r/3722
 https://reviewboard.asterisk.org/r/3713
 https://reviewboard.asterisk.org/r/3703
 https://reviewboard.asterisk.org/r/3689
 https://reviewboard.asterisk.org/r/3687
 https://reviewboard.asterisk.org/r/3674
 https://reviewboard.asterisk.org/r/3671
 https://reviewboard.asterisk.org/r/3667
 https://reviewboard.asterisk.org/r/3665
 https://reviewboard.asterisk.org/r/3625
 https://reviewboard.asterisk.org/r/3602
 https://reviewboard.asterisk.org/r/3519
 https://reviewboard.asterisk.org/r/3518
 https://reviewboard.asterisk.org/r/3516
 https://reviewboard.asterisk.org/r/3515
 https://reviewboard.asterisk.org/r/3512
 https://reviewboard.asterisk.org/r/3506
 https://reviewboard.asterisk.org/r/3413
 https://reviewboard.asterisk.org/r/3410
 https://reviewboard.asterisk.org/r/3387
 https://reviewboard.asterisk.org/r/3388
 https://reviewboard.asterisk.org/r/3389
 https://reviewboard.asterisk.org/r/3390
 https://reviewboard.asterisk.org/r/3321
 https://reviewboard.asterisk.org/r/3320
 https://reviewboard.asterisk.org/r/3319
 https://reviewboard.asterisk.org/r/3318
 https://reviewboard.asterisk.org/r/3266
 https://reviewboard.asterisk.org/r/3265
 https://reviewboard.asterisk.org/r/3234
 https://reviewboard.asterisk.org/r/3178

ASTERISK-23114 #close
Reported by: mjordan
  media_formats_translation_core.diff uploaded by kharwell (License 6464)
  rb3506.diff uploaded by mjordan (License 6283)
  media_format_app_file.diff uploaded by kharwell (License 6464) 
  misc-2.diff uploaded by file (License 5000)
  chan_mild-3.diff uploaded by file (License 5000) 
  chan_obscure.diff uploaded by file (License 5000) 
  jingle.diff uploaded by file (License 5000) 
  funcs.diff uploaded by file (License 5000) 
  formats.diff uploaded by file (License 5000) 
  core.diff uploaded by file (License 5000) 
  bridges.diff uploaded by file (License 5000) 
  mf-codecs-2.diff uploaded by file (License 5000) 
  mf-app_fax.diff uploaded by file (License 5000) 
  mf-apps-3.diff uploaded by file (License 5000) 
  media-formats-3.diff uploaded by file (License 5000) 

ASTERISK-23715
  rb3713.patch uploaded by coreyfarrell (License 5909)
  rb3689.patch uploaded by mjordan (License 6283)
  
ASTERISK-23957
  rb3722.patch uploaded by mjordan (License 6283) 
  mf-attributes-3.diff uploaded by file (License 5000) 

ASTERISK-23958
Tested by: jrose
  rb3822.patch uploaded by coreyfarrell (License 5909) 
  rb3800.patch uploaded by jrose (License 6182)
  chan_sip.diff uploaded by mjordan (License 6283) 
  rb3747.patch uploaded by jrose (License 6182)

ASTERISK-23959 #close
Tested by: sgriepentrog, mjordan, coreyfarrell
  sip_cleanup.diff uploaded by opticron (License 6273)
  chan_sip_caps.diff uploaded by mjordan (License 6283) 
  rb3751.patch uploaded by coreyfarrell (License 5909) 
  chan_sip-3.diff uploaded by file (License 5000) 

ASTERISK-23960 #close
Tested by: opticron
  direct_media.diff uploaded by opticron (License 6273) 
  pjsip-direct-media.diff uploaded by file (License 5000) 
  format_cap_remove.diff uploaded by opticron (License 6273) 
  media_format_fixes.diff uploaded by opticron (License 6273) 
  chan_pjsip-2.diff uploaded by file (License 5000) 

ASTERISK-23966 #close
Tested by: rmudgett
  rb3803.patch uploaded by rmudgetti (License 5621)
  chan_dahdi.diff uploaded by file (License 5000) 
  
ASTERISK-24064 #close
Tested by: coreyfarrell, mjordan, opticron, file, rmudgett, sgriepentrog, jrose
  rb3814.patch uploaded by rmudgett (License 5621) 
  moh_cleanup.diff uploaded by opticron (License 6273) 
  bridge_leak.diff uploaded by opticron (License 6273) 
  translate.diff uploaded by file (License 5000) 
  rb3795.patch uploaded by rmudgett (License 5621) 
  tls_fix.diff uploaded by mjordan (License 6283) 
  fax-mf-fix-2.diff uploaded by file (License 5000) 
  rtp_transfer_stuff uploaded by mjordan (License 6283) 
  rb3787.patch uploaded by rmudgett (License 5621) 
  media-formats-explicit-translate-format-3.diff uploaded by file (License 5000) 
  format_cache_case_fix.diff uploaded by opticron (License 6273) 
  rb3774.patch uploaded by rmudgett (License 5621) 
  rb3775.patch uploaded by rmudgett (License 5621) 
  rtp_engine_fix.diff uploaded by opticron (License 6273) 
  rtp_crash_fix.diff uploaded by opticron (License 6273) 
  rb3753.patch uploaded by mjordan (License 6283) 
  rb3750.patch uploaded by mjordan (License 6283) 
  rb3748.patch uploaded by rmudgett (License 5621) 
  media_format_fixes.diff uploaded by opticron (License 6273) 
  rb3740.patch uploaded by mjordan (License 6283) 
  rb3739.patch uploaded by mjordan (License 6283) 
  rb3734.patch uploaded by mjordan (License 6283) 
  rb3689.patch uploaded by mjordan (License 6283) 
  rb3674.patch uploaded by coreyfarrell (License 5909) 
  rb3671.patch uploaded by coreyfarrell (License 5909) 
  rb3667.patch uploaded by coreyfarrell (License 5909) 
  rb3665.patch uploaded by mjordan (License 6283) 
  rb3625.patch uploaded by coreyfarrell (License 5909) 
  rb3602.patch uploaded by coreyfarrell (License 5909) 
  format_compatibility-2.diff uploaded by file (License 5000) 
  core.diff uploaded by file (License 5000) 
  


git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@419044 65c4cc65-6c06-0410-ace0-fbb531ad65f3
2014-07-20 22:06:33 +00:00

362 lines
13 KiB
C

/*
* Asterisk -- An open source telephony toolkit.
*
* Copyright (C) 1999 - 2006, Digium, Inc.
*
* Mark Spencer <markster@digium.com>
*
* See http://www.asterisk.org for more information about
* the Asterisk project. Please do not directly contact
* any of the maintainers of this project for assistance;
* the project provides a web site, mailing lists and IRC
* channels for your use.
*
* This program is free software, distributed under the terms of
* the GNU General Public License Version 2. See the LICENSE file
* at the top of the source tree.
*/
/*! \file
* \brief Support for translation of data formats.
* \ref translate.c
*/
#ifndef _ASTERISK_TRANSLATE_H
#define _ASTERISK_TRANSLATE_H
#if defined(__cplusplus) || defined(c_plusplus)
extern "C" {
#endif
#if 1 /* need lots of stuff... */
#include "asterisk/frame.h"
#include "asterisk/plc.h"
#include "asterisk/linkedlists.h"
#include "asterisk/format_cap.h"
#include "asterisk/format_cache.h"
#endif
struct ast_trans_pvt; /* declared below */
/*!
* \brief Translator Cost Table definition.
*
* \note The defined values in this table must be used to set
* the translator's table_cost value.
*
* \note 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.
*
* \details 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.
*/
enum ast_trans_cost_table {
/* Lossless Source Translation Costs */
/*! [lossless -> lossless] original sampling */
AST_TRANS_COST_LL_LL_ORIGSAMP = 400000,
/*! [lossless -> lossy] original sampling */
AST_TRANS_COST_LL_LY_ORIGSAMP = 600000,
/*! [lossless -> lossless] up sample */
AST_TRANS_COST_LL_LL_UPSAMP = 800000,
/*! [lossless -> lossy] up sample */
AST_TRANS_COST_LL_LY_UPSAMP = 825000,
/*! [lossless -> lossless] down sample */
AST_TRANS_COST_LL_LL_DOWNSAMP = 850000,
/*! [lossless -> lossy] down sample */
AST_TRANS_COST_LL_LY_DOWNSAMP = 875000,
/*! [lossless -> unknown] unknown.
* This value is for a lossless source translation
* with an unknown destination and or sample rate conversion. */
AST_TRANS_COST_LL_UNKNOWN = 885000,
/* Lossy Source Translation Costs */
/*! [lossy -> lossless] original sampling */
AST_TRANS_COST_LY_LL_ORIGSAMP = 900000,
/*! [lossy -> lossy] original sampling */
AST_TRANS_COST_LY_LY_ORIGSAMP = 915000,
/*! [lossy -> lossless] up sample */
AST_TRANS_COST_LY_LL_UPSAMP = 930000,
/*! [lossy -> lossy] up sample */
AST_TRANS_COST_LY_LY_UPSAMP = 945000,
/*! [lossy -> lossless] down sample */
AST_TRANS_COST_LY_LL_DOWNSAMP = 960000,
/*! [lossy -> lossy] down sample */
AST_TRANS_COST_LY_LY_DOWNSAMP = 975000,
/*! [lossy -> unknown] unknown.
* This value is for a lossy source translation
* with an unknown destination and or sample rate conversion. */
AST_TRANS_COST_LY_UNKNOWN = 985000,
};
/*! \brief
* Descriptor of a translator.
*
* Name, callbacks, and various options
* related to run-time operation (size of buffers, auxiliary
* descriptors, etc).
*
* A codec registers itself by filling the relevant fields
* of a structure and passing it as an argument to
* ast_register_translator(). The structure should not be
* modified after a successful registration, and its address
* must be used as an argument to ast_unregister_translator().
*
* As a minimum, a translator should supply name, srcfmt and dstfmt,
* the required buf_size (in bytes) and buffer_samples (in samples),
* and a few callbacks (framein, frameout, sample).
* The outbuf is automatically prepended by AST_FRIENDLY_OFFSET
* spare bytes so generic routines can place data in there.
*
* Note, the translator is not supposed to do any memory allocation
* or deallocation, nor any locking, because all of this is done in
* the generic code.
*
* Translators using generic plc (packet loss concealment) should
* supply a non-zero plc_samples indicating the size (in samples)
* of artificially generated frames and incoming data.
* Generic plc is only available for dstfmt = SLINEAR
*/
struct ast_translator {
char name[80]; /*!< Name of translator */
struct ast_codec src_codec; /*!< Source codec */
struct ast_codec dst_codec; /*!< Destination codec */
struct ast_codec *core_src_codec; /*!< Core registered source codec */
struct ast_codec *core_dst_codec; /*!< Core registered destination codec */
const char *format; /*!< Optional name of a cached format this translator produces */
int table_cost; /*!< Cost value associated with this translator based
* on translation cost table. */
int comp_cost; /*!< Cost value associated with this translator based
* on computation time. This cost value is computed based
* on the time required to translate sample data. */
int (*newpvt)(struct ast_trans_pvt *); /*!< initialize private data
* associated with the translator */
int (*framein)(struct ast_trans_pvt *pvt, struct ast_frame *in);
/*!< Input frame callback. Store
* (and possibly convert) input frame. */
struct ast_frame * (*frameout)(struct ast_trans_pvt *pvt);
/*!< Output frame callback. Generate a frame
* with outbuf content. */
void (*destroy)(struct ast_trans_pvt *pvt);
/*!< cleanup private data, if needed
* (often unnecessary). */
struct ast_frame * (*sample)(void); /*!< Generate an example frame */
/*!\brief size of outbuf, in samples. Leave it 0 if you want the framein
* callback deal with the frame. Set it appropriately if you
* want the code to checks if the incoming frame fits the
* outbuf (this is e.g. required for plc).
*/
int buffer_samples; /*< size of outbuf, in samples */
/*! \brief size of outbuf, in bytes. Mandatory. The wrapper code will also
* allocate an AST_FRIENDLY_OFFSET space before.
*/
int buf_size;
int desc_size; /*!< size of private descriptor in pvt->pvt, if any */
int native_plc; /*!< true if the translator can do native plc */
struct ast_module *module; /*!< opaque reference to the parent module */
int active; /*!< Whether this translator should be used or not */
int src_fmt_index; /*!< index of the source format in the matrix table */
int dst_fmt_index; /*!< index of the destination format in the matrix table */
AST_LIST_ENTRY(ast_translator) list; /*!< link field */
};
/*! \brief
* 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 \ref AST_FRIENDLY_OFFSET bytes in front of the user portion.
* 'buf' points right after this space.
*
* *_framein() routines operate in two ways:
* 1. some convert on the fly and place the data directly in outbuf;
* in this case 'samples' and 'datalen' contain the number of samples
* and number of bytes available in the buffer.
* In this case we can use a generic *_frameout() routine that simply
* takes whatever is there and places it into the output frame.
* 2. others simply store the (unconverted) samples into a working
* buffer, and leave the conversion task to *_frameout().
* In this case, the intermediate buffer must be in the private
* descriptor, 'datalen' is left to 0, while 'samples' is still
* updated with the number of samples received.
*/
struct ast_trans_pvt {
struct ast_translator *t;
struct ast_frame f; /*!< used in frameout */
int samples; /*!< samples available in outbuf */
/*! \brief actual space used in outbuf */
int datalen;
void *pvt; /*!< more private data, if any */
union {
char *c; /*!< the useful portion of the buffer */
unsigned char *uc; /*!< the useful portion of the buffer */
int16_t *i16;
uint8_t *ui8;
} outbuf;
plc_state_t *plc; /*!< optional plc pointer */
struct ast_trans_pvt *next; /*!< next in translator chain */
struct timeval nextin;
struct timeval nextout;
};
/*! \brief generic frameout function */
struct ast_frame *ast_trans_frameout(struct ast_trans_pvt *pvt,
int datalen, int samples);
struct ast_trans_pvt;
/*!
* \brief Register a translator
* This registers a codec translator with asterisk
* \param t populated ast_translator structure
* \param mod module handle to the module that owns this translator
* \return 0 on success, -1 on failure
*/
int __ast_register_translator(struct ast_translator *t, struct ast_module *module);
/*! \brief See \ref __ast_register_translator() */
#define ast_register_translator(t) __ast_register_translator(t, ast_module_info->self)
/*!
* \brief Unregister a translator
* Unregisters the given tranlator
* \param t translator to unregister
* \return 0 on success, -1 on failure
*/
int ast_unregister_translator(struct ast_translator *t);
/*!
* \brief Activate a previously deactivated translator
* \param t translator to activate
* \return nothing
*
* Enables the specified translator for use.
*/
void ast_translator_activate(struct ast_translator *t);
/*!
* \brief Deactivate a translator
* \param t translator to deactivate
* \return nothing
*
* Disables the specified translator from being used.
*/
void ast_translator_deactivate(struct ast_translator *t);
/*!
* \brief Chooses the best translation path
*
* Given a list of sources, and a designed destination format, which should
* I choose?
*
* \param dst_cap destination capabilities
* \param src_cap source capabilities
* \param dst_fmt_out destination format chosen out of destination capabilities
* \param src_fmt_out source format chosen out of source capabilities
* \return Returns 0 on success, -1 if no path could be found.
*
* \note dst_cap and src_cap are not mondified.
*/
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);
/*!
* \brief Builds a translator path
* Build a path (possibly NULL) from source to dest
* \param dst dest destination format
* \param src source source format
* \return ast_trans_pvt on success, NULL on failure
* */
struct ast_trans_pvt *ast_translator_build_path(struct ast_format *dest, struct ast_format *source);
/*!
* \brief Frees a translator path
* Frees the given translator path structure
* \param tr translator path to get rid of
*/
void ast_translator_free_path(struct ast_trans_pvt *tr);
/*!
* \brief 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
* \param path tr translator structure to use for translation
* \param f frame to translate
* \param consume Whether or not to free the original frame
* \return an ast_frame of the new translation format on success, NULL on failure
*/
struct ast_frame *ast_translate(struct ast_trans_pvt *tr, struct ast_frame *f, int consume);
/*!
* \brief Returns the number of steps required to convert from 'src' to 'dest'.
* \param dest destination format
* \param src source format
* \return the number of translation steps required, or -1 if no path is available
*/
unsigned int ast_translate_path_steps(struct ast_format *dest, struct ast_format *src);
/*!
* \brief Find available formats
* \param dest possible destination formats
* \param src source formats
* \param result capabilities structure to store available formats in
*
* \return 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.
*/
void ast_translate_available_formats(struct ast_format_cap *dest, struct ast_format_cap *src, struct ast_format_cap *result);
/*!
* \brief Puts a string representation of the translation path into outbuf
* \param t translator structure containing the translation path
* \param str ast_str output buffer
* \retval on success pointer to beginning of outbuf. on failure "".
*/
const char *ast_translate_path_to_str(struct ast_trans_pvt *t, struct ast_str **str);
/*!
* \brief Initialize the translation matrix and index to format conversion table.
* \retval 0 on success
* \retval -1 on failure
*/
int ast_translate_init(void);
#if defined(__cplusplus) || defined(c_plusplus)
}
#endif
#endif /* _ASTERISK_TRANSLATE_H */