res_format_attr_celt.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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 CELT 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 CELT attribute structure.
  33. *
  34. * \note The only attribute that affects compatibility here is the sample rate.
  35. */
  36. struct celt_attr {
  37. unsigned int samplerate;
  38. unsigned int maxbitrate;
  39. unsigned int framesize;
  40. };
  41. static enum ast_format_cmp_res celt_cmp(const struct ast_format_attr *fattr1, const struct ast_format_attr *fattr2)
  42. {
  43. struct celt_attr *attr1 = (struct celt_attr *) fattr1;
  44. struct celt_attr *attr2 = (struct celt_attr *) fattr2;
  45. if (attr1->samplerate == attr2->samplerate) {
  46. return AST_FORMAT_CMP_EQUAL;
  47. }
  48. return AST_FORMAT_CMP_NOT_EQUAL;
  49. }
  50. static int celt_get_val(const struct ast_format_attr *fattr, int key, void *result)
  51. {
  52. const struct celt_attr *attr = (struct celt_attr *) fattr;
  53. int *val = result;
  54. switch (key) {
  55. case CELT_ATTR_KEY_SAMP_RATE:
  56. *val = attr->samplerate;
  57. break;
  58. case CELT_ATTR_KEY_MAX_BITRATE:
  59. *val = attr->maxbitrate;
  60. break;
  61. case CELT_ATTR_KEY_FRAME_SIZE:
  62. *val = attr->framesize;
  63. break;
  64. default:
  65. return -1;
  66. ast_log(LOG_WARNING, "unknown attribute type %d\n", key);
  67. }
  68. return 0;
  69. }
  70. static int celt_isset(const struct ast_format_attr *fattr, va_list ap)
  71. {
  72. enum celt_attr_keys key;
  73. const struct celt_attr *attr = (struct celt_attr *) fattr;
  74. for (key = va_arg(ap, int);
  75. key != AST_FORMAT_ATTR_END;
  76. key = va_arg(ap, int))
  77. {
  78. switch (key) {
  79. case CELT_ATTR_KEY_SAMP_RATE:
  80. if (attr->samplerate != (va_arg(ap, int))) {
  81. return -1;
  82. }
  83. break;
  84. case CELT_ATTR_KEY_MAX_BITRATE:
  85. if (attr->maxbitrate != (va_arg(ap, int))) {
  86. return -1;
  87. }
  88. break;
  89. case CELT_ATTR_KEY_FRAME_SIZE:
  90. if (attr->framesize != (va_arg(ap, int))) {
  91. return -1;
  92. }
  93. break;
  94. default:
  95. return -1;
  96. ast_log(LOG_WARNING, "unknown attribute type %d\n", key);
  97. }
  98. }
  99. return 0;
  100. }
  101. static int celt_getjoint(const struct ast_format_attr *fattr1, const struct ast_format_attr *fattr2, struct ast_format_attr *result)
  102. {
  103. struct celt_attr *attr1 = (struct celt_attr *) fattr1;
  104. struct celt_attr *attr2 = (struct celt_attr *) fattr2;
  105. struct celt_attr *attr_res = (struct celt_attr *) result;
  106. /* sample rate is the only attribute that has any bearing on if joint capabilities exist or not */
  107. if (attr1->samplerate != attr2->samplerate) {
  108. return -1;
  109. }
  110. /* either would work, they are guaranteed the same at this point. */
  111. attr_res->samplerate = attr1->samplerate;
  112. /* Take the lowest max bitrate */
  113. attr_res->maxbitrate = MIN(attr1->maxbitrate, attr2->maxbitrate);
  114. attr_res->framesize = attr2->framesize; /* TODO figure out what joint framesize means */
  115. return 0;
  116. }
  117. static void celt_set(struct ast_format_attr *fattr, va_list ap)
  118. {
  119. enum celt_attr_keys key;
  120. struct celt_attr *attr = (struct celt_attr *) fattr;
  121. for (key = va_arg(ap, int);
  122. key != AST_FORMAT_ATTR_END;
  123. key = va_arg(ap, int))
  124. {
  125. switch (key) {
  126. case CELT_ATTR_KEY_SAMP_RATE:
  127. attr->samplerate = (va_arg(ap, int));
  128. break;
  129. case CELT_ATTR_KEY_MAX_BITRATE:
  130. attr->maxbitrate = (va_arg(ap, int));
  131. break;
  132. case CELT_ATTR_KEY_FRAME_SIZE:
  133. attr->framesize = (va_arg(ap, int));
  134. break;
  135. default:
  136. ast_log(LOG_WARNING, "unknown attribute type %d\n", key);
  137. }
  138. }
  139. }
  140. static struct ast_format_attr_interface celt_interface = {
  141. .id = AST_FORMAT_CELT,
  142. .format_attr_cmp = celt_cmp,
  143. .format_attr_get_joint = celt_getjoint,
  144. .format_attr_set = celt_set,
  145. .format_attr_isset = celt_isset,
  146. .format_attr_get_val = celt_get_val,
  147. };
  148. static int load_module(void)
  149. {
  150. if (ast_format_attr_reg_interface(&celt_interface)) {
  151. return AST_MODULE_LOAD_DECLINE;
  152. }
  153. return AST_MODULE_LOAD_SUCCESS;
  154. }
  155. static int unload_module(void)
  156. {
  157. ast_format_attr_unreg_interface(&celt_interface);
  158. return 0;
  159. }
  160. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "CELT Format Attribute Module",
  161. .load = load_module,
  162. .unload = unload_module,
  163. .load_pri = AST_MODPRI_CHANNEL_DEPEND,
  164. );