res_format_attr_silk.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2011, Digium, Inc.
  5. *
  6. * David Vossel <dvossel@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. /*!
  19. * \file
  20. * \brief SILK format attribute interface
  21. *
  22. * \author David Vossel <dvossel@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/module.h"
  30. #include "asterisk/format.h"
  31. /*!
  32. * \brief SILK attribute structure.
  33. *
  34. * \note The only attribute that affects compatibility here is the sample rate.
  35. */
  36. struct silk_attr {
  37. unsigned int samplerate;
  38. unsigned int maxbitrate;
  39. unsigned int dtx;
  40. unsigned int fec;
  41. unsigned int packetloss_percentage;
  42. };
  43. static void silk_destroy(struct ast_format *format)
  44. {
  45. struct silk_attr *attr = ast_format_get_attribute_data(format);
  46. ast_free(attr);
  47. }
  48. static int silk_clone(const struct ast_format *src, struct ast_format *dst)
  49. {
  50. struct silk_attr *original = ast_format_get_attribute_data(src);
  51. struct silk_attr *attr = ast_calloc(1, sizeof(*attr));
  52. if (!attr) {
  53. return -1;
  54. }
  55. if (original) {
  56. *attr = *original;
  57. }
  58. ast_format_set_attribute_data(dst, attr);
  59. return 0;
  60. }
  61. static struct ast_format *silk_parse_sdp_fmtp(const struct ast_format *format, const char *attributes)
  62. {
  63. struct ast_format *cloned;
  64. struct silk_attr *attr;
  65. unsigned int val;
  66. cloned = ast_format_clone(format);
  67. if (!cloned) {
  68. return NULL;
  69. }
  70. attr = ast_format_get_attribute_data(cloned);
  71. if (sscanf(attributes, "maxaveragebitrate=%30u", &val) == 1) {
  72. attr->maxbitrate = val;
  73. }
  74. if (sscanf(attributes, "usedtx=%30u", &val) == 1) {
  75. attr->dtx = val;
  76. }
  77. if (sscanf(attributes, "useinbandfec=%30u", &val) == 1) {
  78. attr->fec = val;
  79. }
  80. return 0;
  81. }
  82. static void silk_generate_sdp_fmtp(const struct ast_format *format, unsigned int payload, struct ast_str **str)
  83. {
  84. struct silk_attr *attr = ast_format_get_attribute_data(format);
  85. if (!attr) {
  86. return;
  87. }
  88. if ((attr->maxbitrate > 5000) && (attr->maxbitrate < 40000)) {
  89. ast_str_append(str, 0, "a=fmtp:%u maxaveragebitrate=%u\r\n", payload, attr->maxbitrate);
  90. }
  91. ast_str_append(str, 0, "a=fmtp:%u usedtx=%u\r\n", payload, attr->dtx);
  92. ast_str_append(str, 0, "a=fmtp:%u useinbandfec=%u\r\n", payload, attr->fec);
  93. }
  94. static enum ast_format_cmp_res silk_cmp(const struct ast_format *format1, const struct ast_format *format2)
  95. {
  96. struct silk_attr *attr1 = ast_format_get_attribute_data(format1);
  97. struct silk_attr *attr2 = ast_format_get_attribute_data(format2);
  98. if (((!attr1 || !attr1->samplerate) && (!attr2 || !attr2->samplerate)) ||
  99. (attr1->samplerate == attr2->samplerate)) {
  100. return AST_FORMAT_CMP_EQUAL;
  101. }
  102. return AST_FORMAT_CMP_NOT_EQUAL;
  103. }
  104. static struct ast_format *silk_getjoint(const struct ast_format *format1, const struct ast_format *format2)
  105. {
  106. struct silk_attr *attr1 = ast_format_get_attribute_data(format1);
  107. struct silk_attr *attr2 = ast_format_get_attribute_data(format2);
  108. unsigned int samplerate;
  109. struct ast_format *jointformat;
  110. struct silk_attr *attr_res;
  111. samplerate = attr1->samplerate & attr2->samplerate;
  112. /* sample rate is the only attribute that has any bearing on if joint capabilities exist or not */
  113. if (samplerate) {
  114. return NULL;
  115. }
  116. jointformat = ast_format_clone(format1);
  117. if (!jointformat) {
  118. return NULL;
  119. }
  120. attr_res = ast_format_get_attribute_data(jointformat);
  121. attr_res->samplerate = samplerate;
  122. /* Take the lowest max bitrate */
  123. attr_res->maxbitrate = MIN(attr1->maxbitrate, attr2->maxbitrate);
  124. /* Only do dtx if both sides want it. DTX is a trade off between
  125. * computational complexity and bandwidth. */
  126. attr_res->dtx = attr1->dtx && attr2->dtx ? 1 : 0;
  127. /* Only do FEC if both sides want it. If a peer specifically requests not
  128. * to receive with FEC, it may be a waste of bandwidth. */
  129. attr_res->fec = attr1->fec && attr2->fec ? 1 : 0;
  130. /* Use the maximum packetloss percentage between the two attributes. This affects how
  131. * much redundancy is used in the FEC. */
  132. attr_res->packetloss_percentage = MAX(attr1->packetloss_percentage, attr2->packetloss_percentage);
  133. return jointformat;
  134. }
  135. static struct ast_format *silk_set(const struct ast_format *format, const char *name, const char *value)
  136. {
  137. struct ast_format *cloned;
  138. struct silk_attr *attr;
  139. unsigned int val;
  140. if (sscanf(value, "%30u", &val) != 1) {
  141. ast_log(LOG_WARNING, "Unknown value '%s' for attribute type '%s'\n",
  142. value, name);
  143. return NULL;
  144. }
  145. cloned = ast_format_clone(format);
  146. if (!cloned) {
  147. return NULL;
  148. }
  149. attr = ast_format_get_attribute_data(cloned);
  150. if (!strcasecmp(name, "sample_rate")) {
  151. attr->samplerate = val;
  152. } else if (!strcasecmp(name, "max_bitrate")) {
  153. attr->maxbitrate = val;
  154. } else if (!strcasecmp(name, "dtx")) {
  155. attr->dtx = val;
  156. } else if (!strcasecmp(name, "fec")) {
  157. attr->fec = val;
  158. } else if (!strcasecmp(name, "packetloss_percentage")) {
  159. attr->packetloss_percentage = val;
  160. } else {
  161. ast_log(LOG_WARNING, "unknown attribute type %s\n", name);
  162. }
  163. return cloned;
  164. }
  165. static struct ast_format_interface silk_interface = {
  166. .format_destroy = silk_destroy,
  167. .format_clone = silk_clone,
  168. .format_cmp = silk_cmp,
  169. .format_get_joint = silk_getjoint,
  170. .format_attribute_set = silk_set,
  171. .format_parse_sdp_fmtp = silk_parse_sdp_fmtp,
  172. .format_generate_sdp_fmtp = silk_generate_sdp_fmtp,
  173. };
  174. static int load_module(void)
  175. {
  176. if (ast_format_interface_register("silk", &silk_interface)) {
  177. return AST_MODULE_LOAD_DECLINE;
  178. }
  179. return AST_MODULE_LOAD_SUCCESS;
  180. }
  181. static int unload_module(void)
  182. {
  183. return 0;
  184. }
  185. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "SILK Format Attribute Module",
  186. .support_level = AST_MODULE_SUPPORT_CORE,
  187. .load = load_module,
  188. .unload = unload_module,
  189. .load_pri = AST_MODPRI_CHANNEL_DEPEND,
  190. );