res_format_attr_vp8.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2015, Alexander Traud
  5. *
  6. * Alexander Traud <pabstraud@compuserve.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 VP8 format attribute interface
  21. *
  22. * \author Alexander Traud <pabstraud@compuserve.com>
  23. *
  24. * \note http://tools.ietf.org/html/draft-ietf-payload-vp8
  25. */
  26. /*** MODULEINFO
  27. <support_level>core</support_level>
  28. ***/
  29. #include "asterisk.h"
  30. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  31. #include "asterisk/module.h"
  32. #include "asterisk/format.h"
  33. #include "asterisk/logger.h" /* for ast_log, LOG_WARNING */
  34. #include "asterisk/strings.h" /* for ast_str_append */
  35. #include "asterisk/utils.h" /* for MIN, ast_malloc, ast_free */
  36. struct vp8_attr {
  37. unsigned int maximum_frame_rate;
  38. unsigned int maximum_frame_size;
  39. };
  40. static struct vp8_attr default_vp8_attr = {
  41. .maximum_frame_rate = UINT_MAX,
  42. .maximum_frame_size = UINT_MAX,
  43. };
  44. static void vp8_destroy(struct ast_format *format)
  45. {
  46. struct vp8_attr *attr = ast_format_get_attribute_data(format);
  47. ast_free(attr);
  48. }
  49. static int vp8_clone(const struct ast_format *src, struct ast_format *dst)
  50. {
  51. struct vp8_attr *original = ast_format_get_attribute_data(src);
  52. struct vp8_attr *attr = ast_malloc(sizeof(*attr));
  53. if (!attr) {
  54. return -1;
  55. }
  56. if (original) {
  57. *attr = *original;
  58. } else {
  59. *attr = default_vp8_attr;
  60. }
  61. ast_format_set_attribute_data(dst, attr);
  62. return 0;
  63. }
  64. static struct ast_format *vp8_parse_sdp_fmtp(const struct ast_format *format, const char *attributes)
  65. {
  66. struct ast_format *cloned;
  67. struct vp8_attr *attr;
  68. const char *kvp;
  69. unsigned int val;
  70. cloned = ast_format_clone(format);
  71. if (!cloned) {
  72. return NULL;
  73. }
  74. attr = ast_format_get_attribute_data(cloned);
  75. if ((kvp = strstr(attributes, "max-fr")) && sscanf(kvp, "max-fr=%30u", &val) == 1) {
  76. attr->maximum_frame_rate = val;
  77. } else {
  78. attr->maximum_frame_rate = UINT_MAX;
  79. }
  80. if ((kvp = strstr(attributes, "max-fs")) && sscanf(kvp, "max-fs=%30u", &val) == 1) {
  81. attr->maximum_frame_size = val;
  82. } else {
  83. attr->maximum_frame_size = UINT_MAX;
  84. }
  85. return cloned;
  86. }
  87. static void vp8_generate_sdp_fmtp(const struct ast_format *format, unsigned int payload, struct ast_str **str)
  88. {
  89. struct vp8_attr *attr = ast_format_get_attribute_data(format);
  90. int added = 0;
  91. if (!attr) {
  92. /*
  93. * (Only) cached formats do not have attribute data assigned because
  94. * they were created before this attribute module was registered.
  95. * Therefore, we assume the default attribute values here.
  96. */
  97. attr = &default_vp8_attr;
  98. }
  99. if (UINT_MAX != attr->maximum_frame_rate) {
  100. if (added) {
  101. ast_str_append(str, 0, ";");
  102. } else if (0 < ast_str_append(str, 0, "a=fmtp:%u ", payload)) {
  103. added = 1;
  104. }
  105. ast_str_append(str, 0, "max-fr=%u", attr->maximum_frame_rate);
  106. }
  107. if (UINT_MAX != attr->maximum_frame_size) {
  108. if (added) {
  109. ast_str_append(str, 0, ";");
  110. } else if (0 < ast_str_append(str, 0, "a=fmtp:%u ", payload)) {
  111. added = 1;
  112. }
  113. ast_str_append(str, 0, "max-fs=%u", attr->maximum_frame_size);
  114. }
  115. if (added) {
  116. ast_str_append(str, 0, "\r\n");
  117. }
  118. }
  119. static struct ast_format *vp8_getjoint(const struct ast_format *format1, const struct ast_format *format2)
  120. {
  121. struct vp8_attr *attr1 = ast_format_get_attribute_data(format1);
  122. struct vp8_attr *attr2 = ast_format_get_attribute_data(format2);
  123. struct ast_format *jointformat;
  124. struct vp8_attr *attr_res;
  125. if (!attr1) {
  126. attr1 = &default_vp8_attr;
  127. }
  128. if (!attr2) {
  129. attr2 = &default_vp8_attr;
  130. }
  131. jointformat = ast_format_clone(format1);
  132. if (!jointformat) {
  133. return NULL;
  134. }
  135. attr_res = ast_format_get_attribute_data(jointformat);
  136. attr_res->maximum_frame_rate = MIN(attr1->maximum_frame_rate, attr2->maximum_frame_rate);
  137. attr_res->maximum_frame_size = MIN(attr1->maximum_frame_size, attr2->maximum_frame_size);
  138. return jointformat;
  139. }
  140. static struct ast_format *vp8_set(const struct ast_format *format, const char *name, const char *value)
  141. {
  142. struct ast_format *cloned;
  143. struct vp8_attr *attr;
  144. unsigned int val;
  145. if (sscanf(value, "%30u", &val) != 1) {
  146. ast_log(LOG_WARNING, "Unknown value '%s' for attribute type '%s'\n",
  147. value, name);
  148. return NULL;
  149. }
  150. cloned = ast_format_clone(format);
  151. if (!cloned) {
  152. return NULL;
  153. }
  154. attr = ast_format_get_attribute_data(cloned);
  155. if (!strcasecmp(name, "maximum_frame_rate")) {
  156. attr->maximum_frame_rate = val;
  157. } else if (!strcasecmp(name, "maximum_frame_size")) {
  158. attr->maximum_frame_size = val;
  159. } else {
  160. ast_log(LOG_WARNING, "unknown attribute type %s\n", name);
  161. }
  162. return cloned;
  163. }
  164. static struct ast_format_interface vp8_interface = {
  165. .format_destroy = vp8_destroy,
  166. .format_clone = vp8_clone,
  167. .format_get_joint = vp8_getjoint,
  168. .format_attribute_set = vp8_set,
  169. .format_parse_sdp_fmtp = vp8_parse_sdp_fmtp,
  170. .format_generate_sdp_fmtp = vp8_generate_sdp_fmtp,
  171. };
  172. static int load_module(void)
  173. {
  174. if (ast_format_interface_register("vp8", &vp8_interface)) {
  175. return AST_MODULE_LOAD_DECLINE;
  176. }
  177. return AST_MODULE_LOAD_SUCCESS;
  178. }
  179. static int unload_module(void)
  180. {
  181. return 0;
  182. }
  183. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "VP8 Format Attribute Module",
  184. .support_level = AST_MODULE_SUPPORT_CORE,
  185. .load = load_module,
  186. .unload = unload_module,
  187. .load_pri = AST_MODPRI_CHANNEL_DEPEND,
  188. );