channels/iax2/format_compatibility.c

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 2014, Digium, Inc.
00005  *
00006  * Joshua Colp <jcolp@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  *
00021  * \brief Media Format Bitfield Compatibility API
00022  *
00023  * \author Joshua Colp <jcolp@digium.com>
00024  */
00025 
00026 /*** MODULEINFO
00027    <support_level>core</support_level>
00028  ***/
00029 
00030 #include "asterisk.h"
00031 
00032 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 420364 $")
00033 
00034 #include "asterisk/logger.h"
00035 #include "asterisk/astobj2.h"
00036 #include "asterisk/codec.h"
00037 #include "asterisk/format.h"
00038 #include "asterisk/format_compatibility.h"
00039 #include "asterisk/format_cache.h"
00040 #include "asterisk/format_cap.h"
00041 #include "asterisk/utils.h"
00042 
00043 #include "include/format_compatibility.h"
00044 
00045 uint64_t iax2_format_compatibility_cap2bitfield(const struct ast_format_cap *cap)
00046 {
00047    uint64_t bitfield = 0;
00048    int x;
00049 
00050    for (x = 0; x < ast_format_cap_count(cap); x++) {
00051       struct ast_format *format = ast_format_cap_get_format(cap, x);
00052 
00053       bitfield |= ast_format_compatibility_format2bitfield(format);
00054 
00055       ao2_ref(format, -1);
00056    }
00057 
00058    return bitfield;
00059 }
00060 
00061 int iax2_format_compatibility_bitfield2cap(uint64_t bitfield, struct ast_format_cap *cap)
00062 {
00063    int bit;
00064 
00065    for (bit = 0; bit < 64; ++bit) {
00066       uint64_t mask = (1ULL << bit);
00067 
00068       if (mask & bitfield) {
00069          struct ast_format *format;
00070 
00071          format = ast_format_compatibility_bitfield2format(mask);
00072          if (format && ast_format_cap_append(cap, format, 0)) {
00073             return -1;
00074          }
00075       }
00076    }
00077 
00078    return 0;
00079 }
00080 
00081 uint64_t iax2_format_compatibility_best(uint64_t formats)
00082 {
00083    /*
00084     * This just our opinion, expressed in code.  We are
00085     * asked to choose the best codec to use, given no
00086     * information.
00087     */
00088    static const uint64_t best[] = {
00089       /*! Okay, ulaw is used by all telephony equipment, so start with it */
00090       AST_FORMAT_ULAW,
00091       /*! Unless of course, you're a silly European, so then prefer ALAW */
00092       AST_FORMAT_ALAW,
00093       AST_FORMAT_G719,
00094       AST_FORMAT_SIREN14,
00095       AST_FORMAT_SIREN7,
00096       AST_FORMAT_TESTLAW,
00097       /*! G.722 is better then all below, but not as common as the above... so give ulaw and alaw priority */
00098       AST_FORMAT_G722,
00099       /*! Okay, well, signed linear is easy to translate into other stuff */
00100       AST_FORMAT_SLIN16,
00101       AST_FORMAT_SLIN,
00102       /*! G.726 is standard ADPCM, in RFC3551 packing order */
00103       AST_FORMAT_G726,
00104       /*! G.726 is standard ADPCM, in AAL2 packing order */
00105       AST_FORMAT_G726_AAL2,
00106       /*! ADPCM has great sound quality and is still pretty easy to translate */
00107       AST_FORMAT_ADPCM,
00108       /*! Okay, we're down to vocoders now, so pick GSM because it's small and easier to
00109           translate and sounds pretty good */
00110       AST_FORMAT_GSM,
00111       /*! iLBC is not too bad */
00112       AST_FORMAT_ILBC,
00113       /*! Speex is free, but computationally more expensive than GSM */
00114       AST_FORMAT_SPEEX16,
00115       AST_FORMAT_SPEEX,
00116       /*! Opus */
00117       AST_FORMAT_OPUS,
00118       /*! Ick, LPC10 sounds terrible, but at least we have code for it, if you're tacky enough
00119           to use it */
00120       AST_FORMAT_LPC10,
00121       /*! G.729a is faster than 723 and slightly less expensive */
00122       AST_FORMAT_G729,
00123       /*! Down to G.723.1 which is proprietary but at least designed for voice */
00124       AST_FORMAT_G723,
00125    };
00126    int idx;
00127 
00128    /* Find the first preferred codec in the format given */
00129    for (idx = 0; idx < ARRAY_LEN(best); ++idx) {
00130       if (formats & best[idx]) {
00131          return best[idx];
00132       }
00133    }
00134 
00135    return 0;
00136 }

Generated on Thu Apr 16 06:27:34 2015 for Asterisk - The Open Source Telephony Project by  doxygen 1.5.6