res_format_attr_silk.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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 maxbitrate;
  38. unsigned int dtx;
  39. unsigned int fec;
  40. unsigned int packetloss_percentage;
  41. };
  42. static void silk_destroy(struct ast_format *format)
  43. {
  44. struct silk_attr *attr = ast_format_get_attribute_data(format);
  45. ast_free(attr);
  46. }
  47. static void attr_init(struct silk_attr *attr)
  48. {
  49. memset(attr, 0, sizeof(*attr));
  50. }
  51. static int silk_clone(const struct ast_format *src, struct ast_format *dst)
  52. {
  53. struct silk_attr *original = ast_format_get_attribute_data(src);
  54. struct silk_attr *attr = ast_malloc(sizeof(*attr));
  55. if (!attr) {
  56. return -1;
  57. }
  58. if (original) {
  59. *attr = *original;
  60. } else {
  61. attr_init(attr);
  62. }
  63. ast_format_set_attribute_data(dst, attr);
  64. return 0;
  65. }
  66. static struct ast_format *silk_parse_sdp_fmtp(const struct ast_format *format, const char *attributes)
  67. {
  68. struct ast_format *cloned;
  69. struct silk_attr *attr;
  70. unsigned int val;
  71. cloned = ast_format_clone(format);
  72. if (!cloned) {
  73. return NULL;
  74. }
  75. attr = ast_format_get_attribute_data(cloned);
  76. if (sscanf(attributes, "maxaveragebitrate=%30u", &val) == 1) {
  77. attr->maxbitrate = val;
  78. }
  79. if (sscanf(attributes, "usedtx=%30u", &val) == 1) {
  80. attr->dtx = val;
  81. }
  82. if (sscanf(attributes, "useinbandfec=%30u", &val) == 1) {
  83. attr->fec = val;
  84. }
  85. return cloned;
  86. }
  87. static void silk_generate_sdp_fmtp(const struct ast_format *format, unsigned int payload, struct ast_str **str)
  88. {
  89. struct silk_attr *attr = ast_format_get_attribute_data(format);
  90. if (!attr) {
  91. return;
  92. }
  93. if ((attr->maxbitrate > 5000) && (attr->maxbitrate < 40000)) {
  94. ast_str_append(str, 0, "a=fmtp:%u maxaveragebitrate=%u\r\n", payload, attr->maxbitrate);
  95. }
  96. if (attr->dtx) {
  97. ast_str_append(str, 0, "a=fmtp:%u usedtx=%u\r\n", payload, attr->dtx);
  98. }
  99. if (attr->fec) {
  100. ast_str_append(str, 0, "a=fmtp:%u useinbandfec=%u\r\n", payload, attr->fec);
  101. }
  102. }
  103. static enum ast_format_cmp_res silk_cmp(const struct ast_format *format1, const struct ast_format *format2)
  104. {
  105. if (ast_format_get_sample_rate(format1) == ast_format_get_sample_rate(format2)) {
  106. return AST_FORMAT_CMP_EQUAL;
  107. }
  108. return AST_FORMAT_CMP_NOT_EQUAL;
  109. }
  110. static struct ast_format *silk_getjoint(const struct ast_format *format1, const struct ast_format *format2)
  111. {
  112. struct silk_attr *attr1 = ast_format_get_attribute_data(format1);
  113. struct silk_attr *attr2 = ast_format_get_attribute_data(format2);
  114. struct ast_format *jointformat;
  115. struct silk_attr *attr_res;
  116. if (ast_format_get_sample_rate(format1) != ast_format_get_sample_rate(format2)) {
  117. return NULL;
  118. }
  119. jointformat = ast_format_clone(format1);
  120. if (!jointformat) {
  121. return NULL;
  122. }
  123. attr_res = ast_format_get_attribute_data(jointformat);
  124. if (!attr1 || !attr2) {
  125. attr_init(attr_res);
  126. } else {
  127. /* Take the lowest max bitrate */
  128. attr_res->maxbitrate = MIN(attr1->maxbitrate, attr2->maxbitrate);
  129. /* Only do dtx if both sides want it. DTX is a trade off between
  130. * computational complexity and bandwidth. */
  131. attr_res->dtx = attr1->dtx && attr2->dtx ? 1 : 0;
  132. /* Only do FEC if both sides want it. If a peer specifically requests not
  133. * to receive with FEC, it may be a waste of bandwidth. */
  134. attr_res->fec = attr1->fec && attr2->fec ? 1 : 0;
  135. /* Use the maximum packetloss percentage between the two attributes. This affects how
  136. * much redundancy is used in the FEC. */
  137. attr_res->packetloss_percentage = MAX(attr1->packetloss_percentage, attr2->packetloss_percentage);
  138. }
  139. return jointformat;
  140. }
  141. static struct ast_format *silk_set(const struct ast_format *format, const char *name, const char *value)
  142. {
  143. struct ast_format *cloned;
  144. struct silk_attr *attr;
  145. unsigned int val;
  146. if (sscanf(value, "%30u", &val) != 1) {
  147. ast_log(LOG_WARNING, "Unknown value '%s' for attribute type '%s'\n",
  148. value, name);
  149. return NULL;
  150. }
  151. cloned = ast_format_clone(format);
  152. if (!cloned) {
  153. return NULL;
  154. }
  155. attr = ast_format_get_attribute_data(cloned);
  156. if (!strcasecmp(name, "max_bitrate")) {
  157. attr->maxbitrate = val;
  158. } else if (!strcasecmp(name, "dtx")) {
  159. attr->dtx = val;
  160. } else if (!strcasecmp(name, "fec")) {
  161. attr->fec = val;
  162. } else if (!strcasecmp(name, "packetloss_percentage")) {
  163. attr->packetloss_percentage = val;
  164. } else {
  165. ast_log(LOG_WARNING, "unknown attribute type %s\n", name);
  166. }
  167. return cloned;
  168. }
  169. static const void *silk_get(const struct ast_format *format, const char *name)
  170. {
  171. struct silk_attr *attr = ast_format_get_attribute_data(format);
  172. unsigned int *val;
  173. if (!strcasecmp(name, "max_bitrate")) {
  174. val = &attr->maxbitrate;
  175. } else if (!strcasecmp(name, "dtx")) {
  176. val = &attr->dtx;
  177. } else if (!strcasecmp(name, "fec")) {
  178. val = &attr->fec;
  179. } else if (!strcasecmp(name, "packetloss_percentage")) {
  180. val = &attr->packetloss_percentage;
  181. } else {
  182. ast_log(LOG_WARNING, "unknown attribute type %s\n", name);
  183. return NULL;
  184. }
  185. return val;
  186. }
  187. static struct ast_format_interface silk_interface = {
  188. .format_destroy = silk_destroy,
  189. .format_clone = silk_clone,
  190. .format_cmp = silk_cmp,
  191. .format_get_joint = silk_getjoint,
  192. .format_attribute_set = silk_set,
  193. .format_attribute_get = silk_get,
  194. .format_parse_sdp_fmtp = silk_parse_sdp_fmtp,
  195. .format_generate_sdp_fmtp = silk_generate_sdp_fmtp,
  196. };
  197. static int load_module(void)
  198. {
  199. if (ast_format_interface_register("silk", &silk_interface)) {
  200. return AST_MODULE_LOAD_DECLINE;
  201. }
  202. return AST_MODULE_LOAD_SUCCESS;
  203. }
  204. static int unload_module(void)
  205. {
  206. return 0;
  207. }
  208. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "SILK Format Attribute Module",
  209. .support_level = AST_MODULE_SUPPORT_CORE,
  210. .load = load_module,
  211. .unload = unload_module,
  212. .load_pri = AST_MODPRI_CHANNEL_DEPEND,
  213. );