res_format_attr_opus.c 12 KB

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