Wed May 16 06:33:33 2012

Asterisk developer's documentation


format.h

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 2010, Digium, Inc.
00005  *
00006  * David Vossel <dvossel@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 /*!
00020  * \file
00021  * \brief Format API
00022  *
00023  * \author David Vossel <dvossel@digium.com>
00024  */
00025 
00026 #ifndef _AST_FORMAT_H_
00027 #define _AST_FORMAT_H_
00028 
00029 #include "asterisk/astobj2.h"
00030 #include "asterisk/silk.h"
00031 #include "asterisk/celt.h"
00032 #define AST_FORMAT_ATTR_SIZE 128
00033 #define AST_FORMAT_INC 100000
00034 
00035 /*! This is the value that ends a var list of format attribute
00036  * key value pairs. */
00037 #define AST_FORMAT_ATTR_END -1
00038 
00039 /* \brief Format Categories*/
00040 enum ast_format_type {
00041    AST_FORMAT_TYPE_AUDIO = 1 * AST_FORMAT_INC,
00042    AST_FORMAT_TYPE_VIDEO = 2 * AST_FORMAT_INC,
00043    AST_FORMAT_TYPE_IMAGE = 3 * AST_FORMAT_INC,
00044    AST_FORMAT_TYPE_TEXT  = 4 * AST_FORMAT_INC,
00045 };
00046 
00047 enum ast_format_id {
00048    /*! G.723.1 compression */
00049    AST_FORMAT_G723_1           = 1 + AST_FORMAT_TYPE_AUDIO,
00050    /*! GSM compression */
00051    AST_FORMAT_GSM              = 2 + AST_FORMAT_TYPE_AUDIO,
00052    /*! Raw mu-law data (G.711) */
00053    AST_FORMAT_ULAW             = 3 + AST_FORMAT_TYPE_AUDIO,
00054    /*! Raw A-law data (G.711) */
00055    AST_FORMAT_ALAW             = 4 + AST_FORMAT_TYPE_AUDIO,
00056    /*! ADPCM (G.726, 32kbps, AAL2 codeword packing) */
00057    AST_FORMAT_G726_AAL2        = 5 + AST_FORMAT_TYPE_AUDIO,
00058    /*! ADPCM (IMA) */
00059    AST_FORMAT_ADPCM            = 6 + AST_FORMAT_TYPE_AUDIO,
00060    /*! LPC10, 180 samples/frame */
00061    AST_FORMAT_LPC10            = 7 + AST_FORMAT_TYPE_AUDIO,
00062    /*! G.729A audio */
00063    AST_FORMAT_G729A            = 8 + AST_FORMAT_TYPE_AUDIO,
00064    /*! SpeeX Free Compression */
00065    AST_FORMAT_SPEEX            = 9 + AST_FORMAT_TYPE_AUDIO,
00066    /*! iLBC Free Compression */
00067    AST_FORMAT_ILBC             = 10 + AST_FORMAT_TYPE_AUDIO,
00068    /*! ADPCM (G.726, 32kbps, RFC3551 codeword packing) */
00069    AST_FORMAT_G726             = 11 + AST_FORMAT_TYPE_AUDIO,
00070    /*! G.722 */
00071    AST_FORMAT_G722             = 12 + AST_FORMAT_TYPE_AUDIO,
00072    /*! G.722.1 (also known as Siren7, 32kbps assumed) */
00073    AST_FORMAT_SIREN7           = 13 + AST_FORMAT_TYPE_AUDIO,
00074    /*! G.722.1 Annex C (also known as Siren14, 48kbps assumed) */
00075    AST_FORMAT_SIREN14          = 14 + AST_FORMAT_TYPE_AUDIO,
00076    /*! G.719 (64 kbps assumed) */
00077    AST_FORMAT_G719             = 15 + AST_FORMAT_TYPE_AUDIO,
00078    /*! SpeeX Wideband (16kHz) Free Compression */
00079    AST_FORMAT_SPEEX16          = 16 + AST_FORMAT_TYPE_AUDIO,
00080    /*! Raw mu-law data (G.711) */
00081    AST_FORMAT_TESTLAW          = 17 + AST_FORMAT_TYPE_AUDIO,
00082    /*! SILK format */
00083    AST_FORMAT_SILK             = 18 + AST_FORMAT_TYPE_AUDIO,
00084    /*! Raw 16-bit Signed Linear (8000 Hz) PCM */
00085    AST_FORMAT_SLINEAR          = 19 + AST_FORMAT_TYPE_AUDIO,
00086    /*! Raw 16-bit Signed Linear (12000 Hz) PCM */
00087    AST_FORMAT_SLINEAR12        = 20 + AST_FORMAT_TYPE_AUDIO,
00088    /*! Raw 16-bit Signed Linear (16000 Hz) PCM */
00089    AST_FORMAT_SLINEAR16        = 21 + AST_FORMAT_TYPE_AUDIO,
00090    /*! Raw 16-bit Signed Linear (24000 Hz) PCM */
00091    AST_FORMAT_SLINEAR24        = 22 + AST_FORMAT_TYPE_AUDIO,
00092    /*! Raw 16-bit Signed Linear (32000 Hz) PCM */
00093    AST_FORMAT_SLINEAR32        = 23 + AST_FORMAT_TYPE_AUDIO,
00094    /*! Raw 16-bit Signed Linear (44100 Hz) PCM just because we can. */
00095    AST_FORMAT_SLINEAR44        = 24 + AST_FORMAT_TYPE_AUDIO,
00096    /*! Raw 16-bit Signed Linear (48000 Hz) PCM */
00097    AST_FORMAT_SLINEAR48        = 25 + AST_FORMAT_TYPE_AUDIO,
00098    /*! Raw 16-bit Signed Linear (96000 Hz) PCM */
00099    AST_FORMAT_SLINEAR96        = 26 + AST_FORMAT_TYPE_AUDIO,
00100    /*! Raw 16-bit Signed Linear (192000 Hz) PCM.  maybe we're taking this too far. */
00101    AST_FORMAT_SLINEAR192       = 27 + AST_FORMAT_TYPE_AUDIO,
00102    AST_FORMAT_SPEEX32          = 28 + AST_FORMAT_TYPE_AUDIO,
00103    AST_FORMAT_CELT             = 29 + AST_FORMAT_TYPE_AUDIO,
00104 
00105    /*! H.261 Video */
00106    AST_FORMAT_H261             = 1 + AST_FORMAT_TYPE_VIDEO,
00107    /*! H.263 Video */
00108    AST_FORMAT_H263             = 2 + AST_FORMAT_TYPE_VIDEO,
00109    /*! H.263+ Video */
00110    AST_FORMAT_H263_PLUS        = 3 + AST_FORMAT_TYPE_VIDEO,
00111    /*! H.264 Video */
00112    AST_FORMAT_H264             = 4 + AST_FORMAT_TYPE_VIDEO,
00113    /*! MPEG4 Video */
00114    AST_FORMAT_MP4_VIDEO        = 5 + AST_FORMAT_TYPE_VIDEO,
00115 
00116    /*! JPEG Images */
00117    AST_FORMAT_JPEG             = 1 + AST_FORMAT_TYPE_IMAGE,
00118    /*! PNG Images */
00119    AST_FORMAT_PNG              = 2 + AST_FORMAT_TYPE_IMAGE,
00120 
00121    /*! T.140 RED Text format RFC 4103 */
00122    AST_FORMAT_T140RED          = 1 + AST_FORMAT_TYPE_TEXT,
00123    /*! T.140 Text format - ITU T.140, RFC 4103 */
00124    AST_FORMAT_T140             = 2 + AST_FORMAT_TYPE_TEXT,
00125 };
00126 
00127 /*! Determine what type of media a ast_format_id is. */
00128 #define AST_FORMAT_GET_TYPE(id) (((int) (id / AST_FORMAT_INC)) * AST_FORMAT_INC)
00129 
00130 
00131 /*! \brief This structure contains the buffer used for format attributes */
00132 struct ast_format_attr {
00133    /*! The buffer formats can use to represent attributes */
00134    uint8_t format_attr[AST_FORMAT_ATTR_SIZE];
00135    /*! If a format's payload needs to pass through that a new marker is required
00136     * for RTP, this variable will be set. */
00137    uint8_t rtp_marker_bit;
00138 };
00139 
00140 /*! \brief Represents a media format within Asterisk. */
00141 struct ast_format {
00142    /*! The unique id representing this format from all the other formats. */
00143    enum ast_format_id id;
00144    /*!  Attribute structure used to associate attributes with a format. */
00145    struct ast_format_attr fattr;
00146 };
00147 
00148 enum ast_format_cmp_res {
00149    /*! structure 1 is identical to structure 2. */
00150    AST_FORMAT_CMP_EQUAL = 0,
00151    /*! structure 1 contains elements not in structure 2. */
00152    AST_FORMAT_CMP_NOT_EQUAL,
00153    /*! structure 1 is a proper subset of the elements in structure 2.*/
00154    AST_FORMAT_CMP_SUBSET,
00155 };
00156 
00157 /*! \brief Definition of supported media formats (codecs) */
00158 struct ast_format_list {
00159    struct ast_format format; /*!< The unique format. */
00160    char name[64]; /*!< short name */
00161    unsigned int samplespersecond; /*!< Number of samples per second (8000/16000) */
00162    char desc[128];   /*!< Description */
00163    int fr_len; /*!< Single frame length in bytes */
00164    int min_ms; /*!< Min value */
00165    int max_ms; /*!< Max value */
00166    int inc_ms; /*!< Increment */
00167    int def_ms; /*!< Default value */
00168    unsigned int flags;  /*!< Smoother flags */
00169    int cur_ms; /*!< Current value */
00170    int custom_entry;
00171 };
00172 
00173 /*! \brief A format must register an attribute interface if it requires the use of the format attributes void pointer */
00174 struct ast_format_attr_interface {
00175    /*! format type */
00176    enum ast_format_id id;
00177 
00178    /*! \brief Determine if format_attr 1 is a subset of format_attr 2.
00179     *
00180     * \retval ast_format_cmp_res representing the result of comparing fattr1 and fattr2.
00181     */
00182    enum ast_format_cmp_res (* const format_attr_cmp)(const struct ast_format_attr *fattr1, const struct ast_format_attr *fattr2);
00183 
00184    /*! \brief Get joint attributes of same format type if they exist.
00185     *
00186     * \retval 0 if joint attributes exist
00187     * \retval -1 if no joint attributes are present
00188     */
00189    int (* const format_attr_get_joint)(const struct ast_format_attr *fattr1, const struct ast_format_attr *fattr2, struct ast_format_attr *result);
00190 
00191    /*! \brief Set format capabilities from a list of key value pairs ending with AST_FORMAT_ATTR_END.
00192     * \note This function does not need to call va_end of the va_list. */
00193    void (* const format_attr_set)(struct ast_format_attr *format_attr, va_list ap);
00194 
00195    /*!
00196     * \brief Find out if format capabilities in va_list are in format.
00197     * \note This function does not need to call va_end of the va_list.
00198     *
00199     * \note This function is optional.  In many cases the format_attr_cmp
00200     * function can be used to derive these results.  If it is possible
00201     * that some format attributes have no bearing on the equality of two formats, this
00202     * function must exist.
00203     *
00204     * \retval 0 if all attributes exist
00205     * \retval -1 if any of the attributes not present
00206     */
00207    int (* const format_attr_isset)(const struct ast_format_attr *format_attr, va_list ap);
00208 
00209    /*
00210     * \brief Return a value for a specific format key.   Return that value in the void pointer.
00211     *
00212     * \note It is not expected that all key value pairs can be returned, but those that can should
00213     * be documented as such.
00214     *
00215     * \note This function is optional if key value pairs are not allowed to be accessed.  This
00216     * will result in -1 always being returned.
00217     *
00218     * \retval 0 Success, value was found and copied into void pointer.
00219     * \retval -1 failure, Value was either not found, or not allowed to be accessed.
00220     */
00221    int (* const format_attr_get_val)(const struct ast_format_attr *format_attr, int key, void *val);
00222 };
00223 
00224 /*!
00225  * \brief This function is used to set an ast_format object to represent a media format
00226  * with optional format attributes represented by format specific key value pairs.
00227  *
00228  * \param format to set
00229  * \param id, format id to set on format
00230  * \param set_attributes, are there attributes to set on this format. 0 == false, 1 == True.
00231  * \param var list of attribute key value pairs, must end with AST_FORMAT_ATTR_END;
00232  *
00233  * \details Example usage.
00234  * ast_format_set(format, AST_FORMAT_ULAW, 0); // no capability attributes are needed for ULAW
00235  *
00236  * ast_format_set(format, AST_FORMAT_SILK, 1, // SILK has capability attributes.
00237  *   AST_FORMAT_SILK_ATTR_RATE, 24000,
00238  *   AST_FORMAT_SILK_ATTR_RATE, 16000,
00239  *   AST_FORMAT_SILK_ATTR_RATE, 12000,
00240  *   AST_FORMAT_SILK_ATTR_RATE, 8000,
00241  *   AST_FORMAT_ATTR_END);
00242  *
00243  * \note This function will initialize the ast_format structure.
00244  *
00245  * \return Pointer to ast_format object, same pointer that is passed in
00246  * by the first argument.
00247  */
00248 struct ast_format *ast_format_set(struct ast_format *format, enum ast_format_id id, int set_attributes, ... );
00249 
00250 /*!
00251  * \brief After ast_format_set has been used on a function, this function can be used to
00252  * set additional format attributes to the structure.
00253  *
00254  * \param format to set
00255  * \param var list of attribute key value pairs, must end with AST_FORMAT_ATTR_END;
00256  *
00257  * \details Example usage.
00258  * ast_format_set(format, AST_FORMAT_SILK, 0);
00259  * ast_format_append(format, // SILK has capability attributes.
00260  *   AST_FORMAT_SILK_ATTR_RATE, 24000,
00261  *   AST_FORMAT_SILK_ATTR_RATE, 16000,
00262  *   AST_FORMAT_SILK_ATTR_RATE, 12000,
00263  *   AST_FORMAT_SILK_ATTR_RATE, 8000,
00264  *   AST_FORMAT_ATTR_END);
00265  *
00266  * \return Pointer to ast_format object, same pointer that is passed in
00267  * by the first argument.
00268  */
00269 struct ast_format *ast_format_append(struct ast_format *format, ... );
00270 
00271 /*!
00272  * \brief Clears the format stucture.
00273  */
00274 void ast_format_clear(struct ast_format *format);
00275 
00276 /*!
00277  * \brief This function is used to set an ast_format object to represent a media format
00278  * with optional capability attributes represented by format specific key value pairs.
00279  *
00280  * \details Example usage. Is this SILK format capable of 8khz
00281  * is_8khz = ast_format_isset(format, AST_FORMAT_SILK_CAP_RATE, 8000);
00282  *
00283  * \return 0, The format key value pairs are within the capabilities defined in this structure.
00284  * \return -1, The format key value pairs are _NOT_ within the capabilities of this structure.
00285  */
00286 int ast_format_isset(const struct ast_format *format, ... );
00287 
00288 /*!
00289  * \brief Get a value from a format containing attributes.
00290  * \note The key represents the format attribute to be retrieved, and the void pointer
00291  * is to the structure that value will be stored in.  It must be known what structure a
00292  * key represents.
00293  *
00294  * \retval 0, success
00295  * \retval -1, failure
00296  */
00297 int ast_format_get_value(const struct ast_format *format, int key, void *value);
00298 
00299 /*!
00300  * \brief Compare ast_formats structures
00301  *
00302  * \retval ast_format_cmp_res representing the result of comparing format1 and format2.
00303  */
00304 enum ast_format_cmp_res ast_format_cmp(const struct ast_format *format1, const struct ast_format *format2);
00305 
00306 /*!
00307  * \brief Find joint format attributes of two ast_format
00308  * structures containing the same uid and return the intersection in the
00309  * result structure.
00310  *
00311  * retval 0, joint attribute capabilities exist.
00312  * retval -1, no joint attribute capabilities exist.
00313  */
00314 int ast_format_joint(const struct ast_format *format1, const struct ast_format *format2, struct ast_format *result);
00315 
00316 /*!
00317  * \brief copy format src into format dst.
00318  */
00319 void ast_format_copy(struct ast_format *dst, const struct ast_format *src);
00320 
00321 /*!
00322  * \brief Set the rtp mark value on the format to indicate to the interface
00323  * writing this format's payload that a new RTP marker is necessary.
00324  */
00325 void ast_format_set_video_mark(struct ast_format *format);
00326 
00327 /*!
00328  * \brief Determine of the marker bit is set or not on this format.
00329  *
00330  * \retval 1, true
00331  * \retval 0, false
00332  */
00333 int ast_format_get_video_mark(const struct ast_format *format);
00334 
00335 /*!
00336  * \brief ast_format to old bitfield format represenatation
00337  *
00338  * \note This is only to be used for IAX2 compatibility 
00339  *
00340  * \retval iax2 representation of ast_format
00341  * \retval 0, if no representation existis for iax2
00342  */
00343 uint64_t ast_format_to_old_bitfield(const struct ast_format *format);
00344 
00345 /*!
00346  * \brief ast_format_id to old bitfield format represenatation
00347  *
00348  */
00349 uint64_t ast_format_id_to_old_bitfield(enum ast_format_id id);
00350 
00351 /*!
00352  * \brief convert old bitfield format to ast_format represenatation
00353  * \note This is only to be used for IAX2 compatibility 
00354  *
00355  * \retval on success, pointer to the dst format in the input parameters
00356  * \retval on failure, NULL
00357  */
00358 struct ast_format *ast_format_from_old_bitfield(struct ast_format *dst, uint64_t src);
00359 
00360 /*!
00361  * \brief convert old bitfield format to ast_format_id value
00362  */
00363 enum ast_format_id ast_format_id_from_old_bitfield(uint64_t src);
00364 
00365 /*!
00366  * \brief Retrieve the global format list in a read only array.
00367  * \note ast_format_list_destroy must be called on every format
00368  * list retrieved from this function.
00369  */
00370 const struct ast_format_list *ast_format_list_get(size_t *size);
00371 
00372 /*!
00373  * \brief Destroy an ast_format_list gotten from ast_format_list_get()
00374  */
00375 const struct ast_format_list *ast_format_list_destroy(const struct ast_format_list *list);
00376 
00377 /*! \brief Get the name of a format
00378  * \param format id of format
00379  * \return A static string containing the name of the format or "unknown" if unknown.
00380  */
00381 const char* ast_getformatname(const struct ast_format *format);
00382 
00383 /*! \brief Returns a string containing all formats pertaining to an format id.
00384  * \param buf a buffer for the output string
00385  * \param size size of buf (bytes)
00386  * \param format id.
00387  * \return The return value is buf.
00388  */
00389 char* ast_getformatname_multiple_byid(char *buf, size_t size, enum ast_format_id id);
00390 
00391 /*!
00392  * \brief Gets a format from a name.
00393  * \param name string of format
00394  * \param format structure to return the format in.
00395  * \return This returns the format pointer given to it on success and NULL on failure
00396  */
00397 struct ast_format *ast_getformatbyname(const char *name, struct ast_format *format);
00398 
00399 /*!
00400  * \brief Get a name from a format 
00401  * \param format to get name of
00402  * \return This returns a static string identifying the format on success, 0 on error.
00403  */
00404 const char *ast_codec2str(struct ast_format *format);
00405 
00406 /*!
00407  * \brief Get the sample rate for a given format.
00408  */
00409 int ast_format_rate(const struct ast_format *format);
00410 
00411 /*!
00412  * \brief register ast_format_attr_interface with core.
00413  *
00414  * \retval 0 success
00415  * \retval -1 failure
00416  */
00417 int ast_format_attr_reg_interface(const struct ast_format_attr_interface *interface);
00418 
00419 /*!
00420  * \brief unregister format_attr interface with core.
00421  *
00422  * \retval 0 success
00423  * \retval -1 failure
00424  */
00425 int ast_format_attr_unreg_interface(const struct ast_format_attr_interface *interface);
00426 
00427 /*!
00428  * \brief Determine if a format is 16bit signed linear of any sample rate. 
00429  */
00430 int ast_format_is_slinear(const struct ast_format *format);
00431 
00432 /*!
00433  * \brief Get the best slinear format id for a given sample rate
00434  */
00435 enum ast_format_id ast_format_slin_by_rate(unsigned int rate);
00436 #endif /* _AST_FORMAT_H */

Generated on Wed May 16 06:33:33 2012 for Asterisk - The Open Source Telephony Project by  doxygen 1.5.6