Sat Apr 26 2014 22:01:36

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 64
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    uint32_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     * \brief Parse SDP attribute information, interpret it, and store it in ast_format_attr structure.
00225     *
00226     * \retval 0 Success, values were valid
00227     * \retval -1 Failure, some values were not acceptable
00228     */
00229    int (* const format_attr_sdp_parse)(struct ast_format_attr *format_attr, const char *attributes);
00230 
00231    /*!
00232     * \brief Generate SDP attribute information from an ast_format_attr structure.
00233     *
00234     * \note This callback should generate a full fmtp line using the provided payload number.
00235     */
00236    void (* const format_attr_sdp_generate)(const struct ast_format_attr *format_attr, unsigned int payload, struct ast_str **str);
00237 };
00238 
00239 /*!
00240  * \brief This function is used to have a media format aware module parse and interpret
00241  * SDP attribute information. Once interpreted this information is stored on the format
00242  * itself using Asterisk format attributes.
00243  *
00244  * \param format to set
00245  * \param attributes string containing the fmtp line from the SDP
00246  *
00247  * \retval 0 success, attribute values were valid
00248  * \retval -1 failure, values were not acceptable
00249  */
00250 int ast_format_sdp_parse(struct ast_format *format, const char *attributes);
00251 
00252 /*!
00253  * \brief This function is used to produce an fmtp SDP line for an Asterisk format. The
00254  * attributes present on the Asterisk format are translated into the SDP equivalent.
00255  *
00256  * \param format to generate an fmtp line for
00257  * \param payload numerical payload for the fmtp line
00258  * \param str structure that the fmtp line will be appended to
00259  */
00260 void ast_format_sdp_generate(const struct ast_format *format, unsigned int payload, struct ast_str **str);
00261 
00262 /*!
00263  * \brief This function is used to set an ast_format object to represent a media format
00264  * with optional format attributes represented by format specific key value pairs.
00265  *
00266  * \param format to set
00267  * \param id, format id to set on format
00268  * \param set_attributes, are there attributes to set on this format. 0 == false, 1 == True.
00269  * \param var list of attribute key value pairs, must end with AST_FORMAT_ATTR_END;
00270  *
00271  * \details Example usage.
00272  * ast_format_set(format, AST_FORMAT_ULAW, 0); // no capability attributes are needed for ULAW
00273  *
00274  * ast_format_set(format, AST_FORMAT_SILK, 1, // SILK has capability attributes.
00275  *   AST_FORMAT_SILK_ATTR_RATE, 24000,
00276  *   AST_FORMAT_SILK_ATTR_RATE, 16000,
00277  *   AST_FORMAT_SILK_ATTR_RATE, 12000,
00278  *   AST_FORMAT_SILK_ATTR_RATE, 8000,
00279  *   AST_FORMAT_ATTR_END);
00280  *
00281  * \note This function will initialize the ast_format structure.
00282  *
00283  * \return Pointer to ast_format object, same pointer that is passed in
00284  * by the first argument.
00285  */
00286 struct ast_format *ast_format_set(struct ast_format *format, enum ast_format_id id, int set_attributes, ... );
00287 
00288 /*!
00289  * \brief After ast_format_set has been used on a function, this function can be used to
00290  * set additional format attributes to the structure.
00291  *
00292  * \param format to set
00293  * \param var list of attribute key value pairs, must end with AST_FORMAT_ATTR_END;
00294  *
00295  * \details Example usage.
00296  * ast_format_set(format, AST_FORMAT_SILK, 0);
00297  * ast_format_append(format, // SILK has capability attributes.
00298  *   AST_FORMAT_SILK_ATTR_RATE, 24000,
00299  *   AST_FORMAT_SILK_ATTR_RATE, 16000,
00300  *   AST_FORMAT_SILK_ATTR_RATE, 12000,
00301  *   AST_FORMAT_SILK_ATTR_RATE, 8000,
00302  *   AST_FORMAT_ATTR_END);
00303  *
00304  * \return Pointer to ast_format object, same pointer that is passed in
00305  * by the first argument.
00306  */
00307 struct ast_format *ast_format_append(struct ast_format *format, ... );
00308 
00309 /*!
00310  * \brief Clears the format stucture.
00311  */
00312 void ast_format_clear(struct ast_format *format);
00313 
00314 /*!
00315  * \brief This function is used to set an ast_format object to represent a media format
00316  * with optional capability attributes represented by format specific key value pairs.
00317  *
00318  * \details Example usage. Is this SILK format capable of 8khz
00319  * is_8khz = ast_format_isset(format, AST_FORMAT_SILK_CAP_RATE, 8000);
00320  *
00321  * \return 0, The format key value pairs are within the capabilities defined in this structure.
00322  * \return -1, The format key value pairs are _NOT_ within the capabilities of this structure.
00323  */
00324 int ast_format_isset(const struct ast_format *format, ... );
00325 
00326 /*!
00327  * \brief Get a value from a format containing attributes.
00328  * \note The key represents the format attribute to be retrieved, and the void pointer
00329  * is to the structure that value will be stored in.  It must be known what structure a
00330  * key represents.
00331  *
00332  * \retval 0, success
00333  * \retval -1, failure
00334  */
00335 int ast_format_get_value(const struct ast_format *format, int key, void *value);
00336 
00337 /*!
00338  * \brief Compare ast_formats structures
00339  *
00340  * \retval ast_format_cmp_res representing the result of comparing format1 and format2.
00341  */
00342 enum ast_format_cmp_res ast_format_cmp(const struct ast_format *format1, const struct ast_format *format2);
00343 
00344 /*!
00345  * \brief Find joint format attributes of two ast_format
00346  * structures containing the same uid and return the intersection in the
00347  * result structure.
00348  *
00349  * retval 0, joint attribute capabilities exist.
00350  * retval -1, no joint attribute capabilities exist.
00351  */
00352 int ast_format_joint(const struct ast_format *format1, const struct ast_format *format2, struct ast_format *result);
00353 
00354 /*!
00355  * \brief copy format src into format dst.
00356  */
00357 void ast_format_copy(struct ast_format *dst, const struct ast_format *src);
00358 
00359 /*!
00360  * \brief Set the rtp mark value on the format to indicate to the interface
00361  * writing this format's payload that a new RTP marker is necessary.
00362  */
00363 void ast_format_set_video_mark(struct ast_format *format);
00364 
00365 /*!
00366  * \brief Determine of the marker bit is set or not on this format.
00367  *
00368  * \retval 1, true
00369  * \retval 0, false
00370  */
00371 int ast_format_get_video_mark(const struct ast_format *format);
00372 
00373 /*!
00374  * \brief ast_format to old bitfield format represenatation
00375  *
00376  * \note This is only to be used for IAX2 compatibility 
00377  *
00378  * \retval iax2 representation of ast_format
00379  * \retval 0, if no representation existis for iax2
00380  */
00381 uint64_t ast_format_to_old_bitfield(const struct ast_format *format);
00382 
00383 /*!
00384  * \brief ast_format_id to old bitfield format represenatation
00385  *
00386  */
00387 uint64_t ast_format_id_to_old_bitfield(enum ast_format_id id);
00388 
00389 /*!
00390  * \brief convert old bitfield format to ast_format represenatation
00391  * \note This is only to be used for IAX2 compatibility 
00392  *
00393  * \retval on success, pointer to the dst format in the input parameters
00394  * \retval on failure, NULL
00395  */
00396 struct ast_format *ast_format_from_old_bitfield(struct ast_format *dst, uint64_t src);
00397 
00398 /*!
00399  * \brief convert old bitfield format to ast_format_id value
00400  */
00401 enum ast_format_id ast_format_id_from_old_bitfield(uint64_t src);
00402 
00403 /*!
00404  * \brief Retrieve the global format list in a read only array.
00405  * \note ast_format_list_destroy must be called on every format
00406  * list retrieved from this function.
00407  */
00408 const struct ast_format_list *ast_format_list_get(size_t *size);
00409 
00410 /*!
00411  * \brief Destroy an ast_format_list gotten from ast_format_list_get()
00412  */
00413 const struct ast_format_list *ast_format_list_destroy(const struct ast_format_list *list);
00414 
00415 /*! \brief Get the name of a format
00416  * \param format id of format
00417  * \return A static string containing the name of the format or "unknown" if unknown.
00418  */
00419 const char* ast_getformatname(const struct ast_format *format);
00420 
00421 /*! \brief Returns a string containing all formats pertaining to an format id.
00422  * \param buf a buffer for the output string
00423  * \param size size of buf (bytes)
00424  * \param format id.
00425  * \return The return value is buf.
00426  */
00427 char* ast_getformatname_multiple_byid(char *buf, size_t size, enum ast_format_id id);
00428 
00429 /*!
00430  * \brief Gets a format from a name.
00431  * \param name string of format
00432  * \param format structure to return the format in.
00433  * \return This returns the format pointer given to it on success and NULL on failure
00434  */
00435 struct ast_format *ast_getformatbyname(const char *name, struct ast_format *format);
00436 
00437 /*!
00438  * \brief Get a name from a format 
00439  * \param format to get name of
00440  * \return This returns a static string identifying the format on success, 0 on error.
00441  */
00442 const char *ast_codec2str(struct ast_format *format);
00443 
00444 /*!
00445  * \brief Get the sample rate for a given format.
00446  */
00447 int ast_format_rate(const struct ast_format *format);
00448 
00449 /*!
00450  * \brief register ast_format_attr_interface with core.
00451  *
00452  * \retval 0 success
00453  * \retval -1 failure
00454  */
00455 int ast_format_attr_reg_interface(const struct ast_format_attr_interface *interface);
00456 
00457 /*!
00458  * \brief unregister format_attr interface with core.
00459  *
00460  * \retval 0 success
00461  * \retval -1 failure
00462  */
00463 int ast_format_attr_unreg_interface(const struct ast_format_attr_interface *interface);
00464 
00465 /*!
00466  * \brief Determine if a format is 16bit signed linear of any sample rate. 
00467  */
00468 int ast_format_is_slinear(const struct ast_format *format);
00469 
00470 /*!
00471  * \brief Get the best slinear format id for a given sample rate
00472  */
00473 enum ast_format_id ast_format_slin_by_rate(unsigned int rate);
00474 #endif /* _AST_FORMAT_H */