format_compatibility.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2014, Digium, Inc.
  5. *
  6. * Joshua Colp <jcolp@digium.com>
  7. *
  8. * See http://www.asterisk.org for more information about
  9. * the Asterisk project. Please do not directly contact
  10. * any of the maintainers of this project for assistance;
  11. * the project provides a web site, mailing lists and IRC
  12. * channels for your use.
  13. *
  14. * This program is free software, distributed under the terms of
  15. * the GNU General Public License Version 2. See the LICENSE file
  16. * at the top of the source tree.
  17. */
  18. /*! \file
  19. *
  20. * \brief Media Format Bitfield Compatibility API
  21. *
  22. * \author Joshua Colp <jcolp@digium.com>
  23. */
  24. /*** MODULEINFO
  25. <support_level>core</support_level>
  26. ***/
  27. #include "asterisk.h"
  28. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  29. #include "asterisk/logger.h"
  30. #include "asterisk/astobj2.h"
  31. #include "asterisk/codec.h"
  32. #include "asterisk/format.h"
  33. #include "asterisk/format_compatibility.h"
  34. #include "asterisk/format_cache.h"
  35. #include "asterisk/format_cap.h"
  36. #include "asterisk/utils.h"
  37. #include "include/format_compatibility.h"
  38. uint64_t iax2_format_compatibility_cap2bitfield(const struct ast_format_cap *cap)
  39. {
  40. uint64_t bitfield = 0;
  41. int x;
  42. for (x = 0; x < ast_format_cap_count(cap); x++) {
  43. struct ast_format *format = ast_format_cap_get_format(cap, x);
  44. bitfield |= ast_format_compatibility_format2bitfield(format);
  45. ao2_ref(format, -1);
  46. }
  47. return bitfield;
  48. }
  49. int iax2_format_compatibility_bitfield2cap(uint64_t bitfield, struct ast_format_cap *cap)
  50. {
  51. int bit;
  52. for (bit = 0; bit < 64; ++bit) {
  53. uint64_t mask = (1ULL << bit);
  54. if (mask & bitfield) {
  55. struct ast_format *format;
  56. format = ast_format_compatibility_bitfield2format(mask);
  57. if (format && ast_format_cap_append(cap, format, 0)) {
  58. return -1;
  59. }
  60. }
  61. }
  62. return 0;
  63. }
  64. uint64_t iax2_format_compatibility_best(uint64_t formats)
  65. {
  66. /*
  67. * This just our opinion, expressed in code. We are
  68. * asked to choose the best codec to use, given no
  69. * information.
  70. */
  71. static const uint64_t best[] = {
  72. /*! Okay, ulaw is used by all telephony equipment, so start with it */
  73. AST_FORMAT_ULAW,
  74. /*! Unless of course, you're a silly European, so then prefer ALAW */
  75. AST_FORMAT_ALAW,
  76. AST_FORMAT_G719,
  77. AST_FORMAT_SIREN14,
  78. AST_FORMAT_SIREN7,
  79. AST_FORMAT_TESTLAW,
  80. /*! G.722 is better then all below, but not as common as the above... so give ulaw and alaw priority */
  81. AST_FORMAT_G722,
  82. /*! Okay, well, signed linear is easy to translate into other stuff */
  83. AST_FORMAT_SLIN16,
  84. AST_FORMAT_SLIN,
  85. /*! G.726 is standard ADPCM, in RFC3551 packing order */
  86. AST_FORMAT_G726,
  87. /*! G.726 is standard ADPCM, in AAL2 packing order */
  88. AST_FORMAT_G726_AAL2,
  89. /*! ADPCM has great sound quality and is still pretty easy to translate */
  90. AST_FORMAT_ADPCM,
  91. /*! Okay, we're down to vocoders now, so pick GSM because it's small and easier to
  92. translate and sounds pretty good */
  93. AST_FORMAT_GSM,
  94. /*! iLBC is not too bad */
  95. AST_FORMAT_ILBC,
  96. /*! Speex is free, but computationally more expensive than GSM */
  97. AST_FORMAT_SPEEX16,
  98. AST_FORMAT_SPEEX,
  99. /*! Opus */
  100. AST_FORMAT_OPUS,
  101. /*! Ick, LPC10 sounds terrible, but at least we have code for it, if you're tacky enough
  102. to use it */
  103. AST_FORMAT_LPC10,
  104. /*! G.729a is faster than 723 and slightly less expensive */
  105. AST_FORMAT_G729,
  106. /*! Down to G.723.1 which is proprietary but at least designed for voice */
  107. AST_FORMAT_G723,
  108. };
  109. int idx;
  110. /* Find the first preferred codec in the format given */
  111. for (idx = 0; idx < ARRAY_LEN(best); ++idx) {
  112. if (formats & best[idx]) {
  113. return best[idx];
  114. }
  115. }
  116. return 0;
  117. }