res_format_attr_opus.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2013, Digium, Inc.
  5. *
  6. * Lorenzo Miniero <lorenzo@meetecho.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 Opus format attribute interface
  21. *
  22. * \author Lorenzo Miniero <lorenzo@meetecho.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. #include "asterisk/logger.h"
  32. #include "asterisk/strings.h"
  33. #include "asterisk/utils.h"
  34. #include "asterisk/opus.h"
  35. /*!
  36. * \brief Opus attribute structure.
  37. *
  38. * \note http://tools.ietf.org/html/rfc7587#section-6
  39. */
  40. struct opus_attr {
  41. int maxbitrate;
  42. int maxplayrate;
  43. int ptime;
  44. int stereo;
  45. int cbr;
  46. int fec;
  47. int dtx;
  48. int spropmaxcapturerate;
  49. int spropstereo;
  50. int maxptime;
  51. /* Note data is expected to be an ao2_object type */
  52. void *data;
  53. };
  54. static struct opus_attr default_opus_attr = {
  55. .maxbitrate = CODEC_OPUS_DEFAULT_BITRATE,
  56. .maxplayrate = CODEC_OPUS_DEFAULT_SAMPLE_RATE,
  57. .ptime = CODEC_OPUS_DEFAULT_PTIME,
  58. .stereo = CODEC_OPUS_DEFAULT_STEREO,
  59. .cbr = CODEC_OPUS_DEFAULT_CBR,
  60. .fec = CODEC_OPUS_DEFAULT_FEC,
  61. .dtx = CODEC_OPUS_DEFAULT_DTX,
  62. .spropmaxcapturerate = CODEC_OPUS_DEFAULT_SAMPLE_RATE,
  63. .spropstereo = CODEC_OPUS_DEFAULT_STEREO,
  64. .maxptime = CODEC_OPUS_DEFAULT_MAX_PTIME
  65. };
  66. static void opus_destroy(struct ast_format *format)
  67. {
  68. struct opus_attr *attr = ast_format_get_attribute_data(format);
  69. if (!attr) {
  70. return;
  71. }
  72. ao2_cleanup(attr->data);
  73. ast_free(attr);
  74. }
  75. static int opus_clone(const struct ast_format *src, struct ast_format *dst)
  76. {
  77. struct opus_attr *original = ast_format_get_attribute_data(src);
  78. struct opus_attr *attr = ast_malloc(sizeof(*attr));
  79. if (!attr) {
  80. return -1;
  81. }
  82. *attr = original ? *original : default_opus_attr;
  83. ao2_bump(attr->data);
  84. ast_format_set_attribute_data(dst, attr);
  85. return 0;
  86. }
  87. static void sdp_fmtp_get(const char *attributes, const char *name, int *attr)
  88. {
  89. const char *kvp = attributes;
  90. int val;
  91. if (ast_strlen_zero(attributes)) {
  92. return;
  93. }
  94. /* This logic goes through each attribute in the fmtp line looking for the
  95. * requested named attribute.
  96. */
  97. while (*kvp) {
  98. /* Skip any preceeding blanks as some implementations separate attributes using spaces too */
  99. kvp = ast_skip_blanks(kvp);
  100. /* If we are at the requested attribute get its value and return */
  101. if (!strncmp(kvp, name, strlen(name)) && kvp[strlen(name)] == '=') {
  102. if (sscanf(kvp, "%*[^=]=%30d", &val) == 1) {
  103. *attr = val;
  104. break;
  105. }
  106. }
  107. /* Move on to the next attribute if possible */
  108. kvp = strchr(kvp, ';');
  109. if (!kvp) {
  110. break;
  111. }
  112. kvp++;
  113. }
  114. }
  115. static struct ast_format *opus_parse_sdp_fmtp(const struct ast_format *format, const char *attributes)
  116. {
  117. struct ast_format *cloned;
  118. struct opus_attr *attr;
  119. cloned = ast_format_clone(format);
  120. if (!cloned) {
  121. return NULL;
  122. }
  123. attr = ast_format_get_attribute_data(cloned);
  124. sdp_fmtp_get(attributes, CODEC_OPUS_ATTR_MAX_PLAYBACK_RATE, &attr->maxplayrate);
  125. sdp_fmtp_get(attributes, CODEC_OPUS_ATTR_MAX_CODED_AUDIO_BANDWIDTH,
  126. &attr->maxplayrate);
  127. sdp_fmtp_get(attributes, CODEC_OPUS_ATTR_SPROP_MAX_CAPTURE_RATE,
  128. &attr->spropmaxcapturerate);
  129. sdp_fmtp_get(attributes, CODEC_OPUS_ATTR_MAX_PTIME, &attr->maxptime);
  130. sdp_fmtp_get(attributes, CODEC_OPUS_ATTR_PTIME, &attr->ptime);
  131. sdp_fmtp_get(attributes, CODEC_OPUS_ATTR_MAX_AVERAGE_BITRATE, &attr->maxbitrate);
  132. sdp_fmtp_get(attributes, CODEC_OPUS_ATTR_STEREO, &attr->stereo);
  133. sdp_fmtp_get(attributes, CODEC_OPUS_ATTR_SPROP_STEREO, &attr->spropstereo);
  134. sdp_fmtp_get(attributes, CODEC_OPUS_ATTR_CBR, &attr->cbr);
  135. sdp_fmtp_get(attributes, CODEC_OPUS_ATTR_FEC, &attr->fec);
  136. sdp_fmtp_get(attributes, CODEC_OPUS_ATTR_DTX, &attr->dtx);
  137. return cloned;
  138. }
  139. static void opus_generate_sdp_fmtp(const struct ast_format *format, unsigned int payload, struct ast_str **str)
  140. {
  141. struct opus_attr *attr = ast_format_get_attribute_data(format);
  142. int base_fmtp_size;
  143. int original_size;
  144. if (!attr) {
  145. /*
  146. * (Only) cached formats do not have attribute data assigned because
  147. * they were created before this attribute module was registered.
  148. * Therefore, we assume the default attribute values here.
  149. */
  150. attr = &default_opus_attr;
  151. }
  152. original_size = ast_str_strlen(*str);
  153. base_fmtp_size = ast_str_append(str, 0, "a=fmtp:%u ", payload);
  154. if (CODEC_OPUS_DEFAULT_SAMPLE_RATE != attr->maxplayrate) {
  155. ast_str_append(str, 0, "%s=%d;",
  156. CODEC_OPUS_ATTR_MAX_PLAYBACK_RATE, attr->maxplayrate);
  157. }
  158. if (CODEC_OPUS_DEFAULT_SAMPLE_RATE != attr->spropmaxcapturerate) {
  159. ast_str_append(str, 0, "%s=%d;",
  160. CODEC_OPUS_ATTR_SPROP_MAX_CAPTURE_RATE, attr->spropmaxcapturerate);
  161. }
  162. if (CODEC_OPUS_DEFAULT_BITRATE != attr->maxbitrate || attr->maxbitrate > 0) {
  163. ast_str_append(str, 0, "%s=%d;",
  164. CODEC_OPUS_ATTR_MAX_AVERAGE_BITRATE, attr->maxbitrate);
  165. }
  166. if (CODEC_OPUS_DEFAULT_STEREO != attr->stereo) {
  167. ast_str_append(str, 0, "%s=%d;",
  168. CODEC_OPUS_ATTR_STEREO, attr->stereo);
  169. }
  170. if (CODEC_OPUS_DEFAULT_STEREO != attr->spropstereo) {
  171. ast_str_append(str, 0, "%s=%d;",
  172. CODEC_OPUS_ATTR_SPROP_STEREO, attr->spropstereo);
  173. }
  174. if (CODEC_OPUS_DEFAULT_CBR != attr->cbr) {
  175. ast_str_append(str, 0, "%s=%d;",
  176. CODEC_OPUS_ATTR_CBR, attr->cbr);
  177. }
  178. if (CODEC_OPUS_DEFAULT_FEC!= attr->fec) {
  179. ast_str_append(str, 0, "%s=%d;",
  180. CODEC_OPUS_ATTR_FEC, attr->fec);
  181. }
  182. if (CODEC_OPUS_DEFAULT_DTX != attr->dtx) {
  183. ast_str_append(str, 0, "%s=%d;",
  184. CODEC_OPUS_ATTR_DTX, attr->dtx);
  185. }
  186. if (base_fmtp_size == ast_str_strlen(*str) - original_size) {
  187. ast_str_truncate(*str, original_size);
  188. } else {
  189. ast_str_truncate(*str, -1);
  190. ast_str_append(str, 0, "\r\n");
  191. }
  192. }
  193. static struct ast_format *opus_getjoint(const struct ast_format *format1, const struct ast_format *format2)
  194. {
  195. struct opus_attr *attr1 = ast_format_get_attribute_data(format1);
  196. struct opus_attr *attr2 = ast_format_get_attribute_data(format2);
  197. struct ast_format *jointformat;
  198. struct opus_attr *attr_res;
  199. if (!attr1) {
  200. attr1 = &default_opus_attr;
  201. }
  202. if (!attr2) {
  203. attr2 = &default_opus_attr;
  204. }
  205. jointformat = ast_format_clone(format1);
  206. if (!jointformat) {
  207. return NULL;
  208. }
  209. attr_res = ast_format_get_attribute_data(jointformat);
  210. attr_res->dtx = attr1->dtx || attr2->dtx ? 1 : 0;
  211. /* Only do FEC if both sides want it. If a peer specifically requests not
  212. * to receive with FEC, it may be a waste of bandwidth. */
  213. attr_res->fec = attr1->fec && attr2->fec ? 1 : 0;
  214. attr_res->cbr = attr1->cbr || attr2->cbr ? 1 : 0;
  215. attr_res->spropstereo = attr1->spropstereo || attr2->spropstereo ? 1 : 0;
  216. /* Only do stereo if both sides want it. If a peer specifically requests not
  217. * to receive stereo signals, it may be a waste of bandwidth. */
  218. attr_res->stereo = attr1->stereo && attr2->stereo ? 1 : 0;
  219. if (attr1->maxbitrate < 0) {
  220. attr_res->maxbitrate = attr2->maxbitrate;
  221. } else if (attr2->maxbitrate < 0) {
  222. attr_res->maxbitrate = attr1->maxbitrate;
  223. } else {
  224. attr_res->maxbitrate = MIN(attr1->maxbitrate, attr2->maxbitrate);
  225. }
  226. attr_res->spropmaxcapturerate = MIN(attr1->spropmaxcapturerate, attr2->spropmaxcapturerate);
  227. attr_res->maxplayrate = MIN(attr1->maxplayrate, attr2->maxplayrate);
  228. return jointformat;
  229. }
  230. static struct ast_format *opus_set(const struct ast_format *format,
  231. const char *name, const char *value)
  232. {
  233. struct ast_format *cloned;
  234. struct opus_attr *attr;
  235. int val;
  236. if (!(cloned = ast_format_clone(format))) {
  237. return NULL;
  238. }
  239. attr = ast_format_get_attribute_data(cloned);
  240. if (!strcmp(name, CODEC_OPUS_ATTR_DATA)) {
  241. ao2_cleanup(attr->data);
  242. attr->data = ao2_bump((void*)value);
  243. return cloned;
  244. }
  245. if (sscanf(value, "%30d", &val) != 1) {
  246. ast_log(LOG_WARNING, "Unknown value '%s' for attribute type '%s'\n",
  247. value, name);
  248. ao2_ref(cloned, -1);
  249. return NULL;
  250. }
  251. if (!strcasecmp(name, CODEC_OPUS_ATTR_MAX_PLAYBACK_RATE)) {
  252. attr->maxplayrate = val;
  253. } else if (!strcasecmp(name, CODEC_OPUS_ATTR_MAX_CODED_AUDIO_BANDWIDTH)) {
  254. attr->maxplayrate = val;
  255. } else if (!strcasecmp(name, CODEC_OPUS_ATTR_SPROP_MAX_CAPTURE_RATE)) {
  256. attr->spropmaxcapturerate = val;
  257. } else if (!strcasecmp(name, CODEC_OPUS_ATTR_MAX_PTIME)) {
  258. attr->maxptime = val;
  259. } else if (!strcasecmp(name, CODEC_OPUS_ATTR_PTIME)) {
  260. attr->ptime = val;
  261. } else if (!strcasecmp(name, CODEC_OPUS_ATTR_MAX_AVERAGE_BITRATE)) {
  262. attr->maxbitrate = val;
  263. } else if (!strcasecmp(name, CODEC_OPUS_ATTR_STEREO)) {
  264. attr->stereo = val;
  265. } else if (!strcasecmp(name, CODEC_OPUS_ATTR_SPROP_STEREO)) {
  266. attr->spropstereo = val;
  267. } else if (!strcasecmp(name, CODEC_OPUS_ATTR_CBR)) {
  268. attr->cbr = val;
  269. } else if (!strcasecmp(name, CODEC_OPUS_ATTR_FEC)) {
  270. attr->fec = val;
  271. } else if (!strcasecmp(name, CODEC_OPUS_ATTR_DTX)) {
  272. attr->dtx = val;
  273. } else {
  274. ast_log(LOG_WARNING, "unknown attribute type %s\n", name);
  275. }
  276. return cloned;
  277. }
  278. static const void *opus_get(const struct ast_format *format, const char *name)
  279. {
  280. struct opus_attr *attr = ast_format_get_attribute_data(format);
  281. int *val = NULL;
  282. if (!attr) {
  283. return NULL;
  284. }
  285. if (!strcasecmp(name, CODEC_OPUS_ATTR_DATA)) {
  286. return ao2_bump(attr->data);
  287. } else if (!strcasecmp(name, CODEC_OPUS_ATTR_MAX_PLAYBACK_RATE)) {
  288. val = &attr->maxplayrate;
  289. } else if (!strcasecmp(name, CODEC_OPUS_ATTR_SPROP_MAX_CAPTURE_RATE)) {
  290. val = &attr->spropmaxcapturerate;
  291. } else if (!strcasecmp(name, CODEC_OPUS_ATTR_MAX_PTIME)) {
  292. val = &attr->maxptime;
  293. } else if (!strcasecmp(name, CODEC_OPUS_ATTR_PTIME)) {
  294. val = &attr->ptime;
  295. } else if (!strcasecmp(name, CODEC_OPUS_ATTR_MAX_AVERAGE_BITRATE)) {
  296. val = &attr->maxbitrate;
  297. } else if (!strcasecmp(name, CODEC_OPUS_ATTR_STEREO)) {
  298. val = &attr->stereo;
  299. } else if (!strcasecmp(name, CODEC_OPUS_ATTR_SPROP_STEREO)) {
  300. val = &attr->spropstereo;
  301. } else if (!strcasecmp(name, CODEC_OPUS_ATTR_CBR)) {
  302. val = &attr->cbr;
  303. } else if (!strcasecmp(name, CODEC_OPUS_ATTR_FEC)) {
  304. val = &attr->fec;
  305. } else if (!strcasecmp(name, CODEC_OPUS_ATTR_DTX)) {
  306. val = &attr->dtx;
  307. } else {
  308. ast_log(LOG_WARNING, "unknown attribute type %s\n", name);
  309. }
  310. return val;
  311. }
  312. static struct ast_format_interface opus_interface = {
  313. .format_destroy = opus_destroy,
  314. .format_clone = opus_clone,
  315. .format_get_joint = opus_getjoint,
  316. .format_attribute_set = opus_set,
  317. .format_parse_sdp_fmtp = opus_parse_sdp_fmtp,
  318. .format_generate_sdp_fmtp = opus_generate_sdp_fmtp,
  319. .format_attribute_get = opus_get
  320. };
  321. static int load_module(void)
  322. {
  323. if (__ast_format_interface_register("opus", &opus_interface, ast_module_info->self)) {
  324. return AST_MODULE_LOAD_DECLINE;
  325. }
  326. return AST_MODULE_LOAD_SUCCESS;
  327. }
  328. static int unload_module(void)
  329. {
  330. return 0;
  331. }
  332. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS | AST_MODFLAG_LOAD_ORDER, "Opus Format Attribute Module",
  333. .support_level = AST_MODULE_SUPPORT_CORE,
  334. .load = load_module,
  335. .unload = unload_module,
  336. .load_pri = AST_MODPRI_REALTIME_DRIVER /* Needs to load before codec_opus */
  337. );