codec_pref.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2014, Digium, Inc.
  5. *
  6. * Joshua Colp <jcolp@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. /*! \file
  19. *
  20. * \brief Media Format Bitfield Compatibility API
  21. *
  22. * \author Joshua Colp <jcolp@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/logger.h"
  30. #include "asterisk/astobj2.h"
  31. #include "asterisk/codec.h"
  32. #include "asterisk/format.h"
  33. #include "asterisk/format_compatibility.h"
  34. #include "asterisk/format_cache.h"
  35. #include "asterisk/format_cap.h"
  36. #include "asterisk/utils.h"
  37. #include "include/codec_pref.h"
  38. #include "include/format_compatibility.h"
  39. void iax2_codec_pref_convert(struct iax2_codec_pref *pref, char *buf, size_t size, int right)
  40. {
  41. static int differential = (int) 'A';
  42. int x;
  43. if (right) {
  44. --size;/* Save room for the nul string terminator. */
  45. for (x = 0; x < ARRAY_LEN(pref->order) && x < size; ++x) {
  46. if (!pref->order[x]) {
  47. break;
  48. }
  49. buf[x] = pref->order[x] + differential;
  50. }
  51. buf[x] = '\0';
  52. } else {
  53. for (x = 0; x < ARRAY_LEN(pref->order) && x < size; ++x) {
  54. if (buf[x] == '\0') {
  55. break;
  56. }
  57. pref->order[x] = buf[x] - differential;
  58. pref->framing[x] = 0;
  59. }
  60. if (x < ARRAY_LEN(pref->order)) {
  61. pref->order[x] = 0;
  62. pref->framing[x] = 0;
  63. }
  64. }
  65. }
  66. struct ast_format *iax2_codec_pref_index(struct iax2_codec_pref *pref, int idx, struct ast_format **result)
  67. {
  68. if (0 <= idx && idx < ARRAY_LEN(pref->order) && pref->order[idx]) {
  69. uint64_t pref_bitfield;
  70. pref_bitfield = iax2_codec_pref_order_value_to_format_bitfield(pref->order[idx]);
  71. *result = ast_format_compatibility_bitfield2format(pref_bitfield);
  72. } else {
  73. *result = NULL;
  74. }
  75. return *result;
  76. }
  77. int iax2_codec_pref_to_cap(struct iax2_codec_pref *pref, struct ast_format_cap *cap)
  78. {
  79. int idx;
  80. for (idx = 0; idx < ARRAY_LEN(pref->order); ++idx) {
  81. uint64_t pref_bitfield;
  82. struct ast_format *pref_format;
  83. pref_bitfield = iax2_codec_pref_order_value_to_format_bitfield(pref->order[idx]);
  84. if (!pref_bitfield) {
  85. break;
  86. }
  87. pref_format = ast_format_compatibility_bitfield2format(pref_bitfield);
  88. if (pref_format && ast_format_cap_append(cap, pref_format, pref->framing[idx])) {
  89. return -1;
  90. }
  91. }
  92. return 0;
  93. }
  94. int iax2_codec_pref_best_bitfield2cap(uint64_t bitfield, struct iax2_codec_pref *prefs, struct ast_format_cap *cap)
  95. {
  96. uint64_t best_bitfield;
  97. struct ast_format *format;
  98. /* Add any user preferred codecs first. */
  99. if (prefs) {
  100. int idx;
  101. for (idx = 0; bitfield && idx < ARRAY_LEN(prefs->order); ++idx) {
  102. best_bitfield = iax2_codec_pref_order_value_to_format_bitfield(prefs->order[idx]);
  103. if (!best_bitfield) {
  104. break;
  105. }
  106. if (best_bitfield & bitfield) {
  107. format = ast_format_compatibility_bitfield2format(best_bitfield);
  108. if (format && ast_format_cap_append(cap, format, prefs->framing[idx])) {
  109. return -1;
  110. }
  111. /* Remove just added codec. */
  112. bitfield &= ~best_bitfield;
  113. }
  114. }
  115. }
  116. /* Add the hard coded "best" codecs. */
  117. while (bitfield) {
  118. best_bitfield = iax2_format_compatibility_best(bitfield);
  119. if (!best_bitfield) {
  120. /* No more codecs considered best. */
  121. break;
  122. }
  123. format = ast_format_compatibility_bitfield2format(best_bitfield);
  124. /* The best_bitfield should always be convertible to a format. */
  125. ast_assert(format != NULL);
  126. if (ast_format_cap_append(cap, format, 0)) {
  127. return -1;
  128. }
  129. /* Remove just added "best" codec to find the next "best". */
  130. bitfield &= ~best_bitfield;
  131. }
  132. /* Add any remaining codecs. */
  133. if (bitfield) {
  134. int bit;
  135. for (bit = 0; bit < 64; ++bit) {
  136. uint64_t mask = (1ULL << bit);
  137. if (mask & bitfield) {
  138. format = ast_format_compatibility_bitfield2format(mask);
  139. if (format && ast_format_cap_append(cap, format, 0)) {
  140. return -1;
  141. }
  142. }
  143. }
  144. }
  145. return 0;
  146. }
  147. int iax2_codec_pref_string(struct iax2_codec_pref *pref, char *buf, size_t size)
  148. {
  149. int x;
  150. struct ast_format_cap *cap;
  151. size_t total_len;
  152. char *cur;
  153. /* This function is useless if you have less than a 6 character buffer.
  154. * '(...)' is six characters. */
  155. if (size < 6) {
  156. return -1;
  157. }
  158. /* Convert the preferences into a format cap so that we can read the format names */
  159. cap = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
  160. if (!cap || iax2_codec_pref_to_cap(pref, cap)) {
  161. strcpy(buf, "(...)"); /* Safe */
  162. ao2_cleanup(cap);
  163. return -1;
  164. }
  165. /* We know that at a minimum, 3 characters are used - (, ), and \0 */
  166. total_len = size - 3;
  167. /* This character has already been accounted for total_len purposes */
  168. buf[0] = '(';
  169. cur = buf + 1;
  170. /* Loop through the formats and write as many into the buffer as we can */
  171. for (x = 0; x < ast_format_cap_count(cap); x++) {
  172. size_t name_len;
  173. struct ast_format *fmt = ast_format_cap_get_format(cap, x);
  174. const char *name = ast_format_get_name(fmt);
  175. name_len = strlen(name);
  176. /* all entries after the first need a delimiter character */
  177. if (x) {
  178. name_len++;
  179. }
  180. /* Terminate the list early if we don't have room for the entry.
  181. * If it's not the last entry in the list, save enough room to write '...'.
  182. */
  183. if (((x == ast_format_cap_count(cap) - 1) && (total_len < name_len)) ||
  184. ((x < ast_format_cap_count(cap) - 1) && (total_len < name_len + 3))) {
  185. strcpy(cur, "...");
  186. cur += 3;
  187. total_len -= 3;
  188. ao2_ref(fmt, -1);
  189. break;
  190. }
  191. sprintf(cur, "%s%s", x ? "|" : "", name);
  192. cur += name_len;
  193. total_len -= name_len;
  194. ao2_ref(fmt, -1);
  195. }
  196. ao2_ref(cap, -1);
  197. /* These two characters have already been accounted for total_len purposes */
  198. cur[0] = ')';
  199. cur[1] = '\0';
  200. return size - total_len;
  201. }
  202. static void codec_pref_remove_index(struct iax2_codec_pref *pref, int codec_pref_index)
  203. {
  204. int idx;
  205. idx = codec_pref_index;
  206. if (idx == ARRAY_LEN(pref->order) - 1) {
  207. /* Remove from last array entry. */
  208. pref->order[idx] = 0;
  209. pref->framing[idx] = 0;
  210. return;
  211. }
  212. for (; idx < ARRAY_LEN(pref->order); ++idx) {
  213. pref->order[idx] = pref->order[idx + 1];
  214. pref->framing[idx] = pref->framing[idx + 1];
  215. if (!pref->order[idx]) {
  216. return;
  217. }
  218. }
  219. }
  220. /*! \brief Remove codec from pref list */
  221. static void codec_pref_remove(struct iax2_codec_pref *pref, int format_index)
  222. {
  223. int x;
  224. if (!pref->order[0]) {
  225. return;
  226. }
  227. for (x = 0; x < ARRAY_LEN(pref->order); ++x) {
  228. if (!pref->order[x]) {
  229. break;
  230. }
  231. if (pref->order[x] == format_index) {
  232. codec_pref_remove_index(pref, x);
  233. break;
  234. }
  235. }
  236. }
  237. void iax2_codec_pref_remove_missing(struct iax2_codec_pref *pref, uint64_t bitfield)
  238. {
  239. int idx;
  240. if (!pref->order[0]) {
  241. return;
  242. }
  243. /*
  244. * Work from the end of the list so we always deal with
  245. * unmodified entries in case we have to remove a pref.
  246. */
  247. for (idx = ARRAY_LEN(pref->order); idx--;) {
  248. uint64_t pref_bitfield;
  249. pref_bitfield = iax2_codec_pref_order_value_to_format_bitfield(pref->order[idx]);
  250. if (!pref_bitfield) {
  251. continue;
  252. }
  253. /* If this format isn't in the bitfield, remove it from the prefs. */
  254. if (!(pref_bitfield & bitfield)) {
  255. codec_pref_remove_index(pref, idx);
  256. }
  257. }
  258. }
  259. /*!
  260. * \brief Formats supported by IAX2.
  261. *
  262. * \note All AST_FORMAT_xxx compatibility bit defines must be
  263. * represented here.
  264. *
  265. * \note The order is important because the array index+1 values
  266. * go out over the wire.
  267. */
  268. static const uint64_t iax2_supported_formats[] = {
  269. AST_FORMAT_G723,
  270. AST_FORMAT_GSM,
  271. AST_FORMAT_ULAW,
  272. AST_FORMAT_ALAW,
  273. AST_FORMAT_G726,
  274. AST_FORMAT_ADPCM,
  275. AST_FORMAT_SLIN,
  276. AST_FORMAT_LPC10,
  277. AST_FORMAT_G729,
  278. AST_FORMAT_SPEEX,
  279. AST_FORMAT_SPEEX16,
  280. AST_FORMAT_ILBC,
  281. AST_FORMAT_G726_AAL2,
  282. AST_FORMAT_G722,
  283. AST_FORMAT_SLIN16,
  284. AST_FORMAT_JPEG,
  285. AST_FORMAT_PNG,
  286. AST_FORMAT_H261,
  287. AST_FORMAT_H263,
  288. AST_FORMAT_H263P,
  289. AST_FORMAT_H264,
  290. AST_FORMAT_MP4,
  291. AST_FORMAT_T140_RED,
  292. AST_FORMAT_T140,
  293. AST_FORMAT_SIREN7,
  294. AST_FORMAT_SIREN14,
  295. AST_FORMAT_TESTLAW,
  296. AST_FORMAT_G719,
  297. 0, /* Place holder */
  298. 0, /* Place holder */
  299. 0, /* Place holder */
  300. 0, /* Place holder */
  301. 0, /* Place holder */
  302. 0, /* Place holder */
  303. 0, /* Place holder */
  304. 0, /* Place holder */
  305. AST_FORMAT_OPUS,
  306. AST_FORMAT_VP8,
  307. /* ONLY ADD TO THE END OF THIS LIST */
  308. /* XXX Use up the place holder slots first. */
  309. };
  310. uint64_t iax2_codec_pref_order_value_to_format_bitfield(int order_value)
  311. {
  312. if (order_value < 1 || ARRAY_LEN(iax2_supported_formats) < order_value) {
  313. return 0;
  314. }
  315. return iax2_supported_formats[order_value - 1];
  316. }
  317. int iax2_codec_pref_format_bitfield_to_order_value(uint64_t bitfield)
  318. {
  319. int idx;
  320. if (bitfield) {
  321. for (idx = 0; idx < ARRAY_LEN(iax2_supported_formats); ++idx) {
  322. if (iax2_supported_formats[idx] == bitfield) {
  323. return idx + 1;
  324. }
  325. }
  326. }
  327. return 0;
  328. }
  329. /*!
  330. * \internal
  331. * \brief Append the bitfield format to the codec preference list.
  332. * \since 13.0.0
  333. *
  334. * \param pref Codec preference list to append the given bitfield.
  335. * \param bitfield Format bitfield to append.
  336. * \param framing Framing size of the codec.
  337. *
  338. * \return Nothing
  339. */
  340. static void iax2_codec_pref_append_bitfield(struct iax2_codec_pref *pref, uint64_t bitfield, unsigned int framing)
  341. {
  342. int format_index;
  343. int x;
  344. format_index = iax2_codec_pref_format_bitfield_to_order_value(bitfield);
  345. if (!format_index) {
  346. return;
  347. }
  348. codec_pref_remove(pref, format_index);
  349. for (x = 0; x < ARRAY_LEN(pref->order); ++x) {
  350. if (!pref->order[x]) {
  351. pref->order[x] = format_index;
  352. pref->framing[x] = framing;
  353. break;
  354. }
  355. }
  356. }
  357. void iax2_codec_pref_append(struct iax2_codec_pref *pref, struct ast_format *format, unsigned int framing)
  358. {
  359. uint64_t bitfield;
  360. bitfield = ast_format_compatibility_format2bitfield(format);
  361. if (!bitfield) {
  362. return;
  363. }
  364. iax2_codec_pref_append_bitfield(pref, bitfield, framing);
  365. }
  366. void iax2_codec_pref_prepend(struct iax2_codec_pref *pref, struct ast_format *format, unsigned int framing,
  367. int only_if_existing)
  368. {
  369. uint64_t bitfield;
  370. int format_index;
  371. int x;
  372. bitfield = ast_format_compatibility_format2bitfield(format);
  373. if (!bitfield) {
  374. return;
  375. }
  376. format_index = iax2_codec_pref_format_bitfield_to_order_value(bitfield);
  377. if (!format_index) {
  378. return;
  379. }
  380. /* Now find any existing occurrence, or the end */
  381. for (x = 0; x < ARRAY_LEN(pref->order); ++x) {
  382. if (!pref->order[x] || pref->order[x] == format_index)
  383. break;
  384. }
  385. /*
  386. * The array can never be full without format_index
  387. * also being in the array.
  388. */
  389. ast_assert(x < ARRAY_LEN(pref->order));
  390. /* If we failed to find any occurrence, set to the end for safety. */
  391. if (ARRAY_LEN(pref->order) <= x) {
  392. x = ARRAY_LEN(pref->order) - 1;
  393. }
  394. if (only_if_existing && !pref->order[x]) {
  395. return;
  396. }
  397. /* Move down to make space to insert - either all the way to the end,
  398. or as far as the existing location (which will be overwritten) */
  399. for (; x > 0; --x) {
  400. pref->order[x] = pref->order[x - 1];
  401. pref->framing[x] = pref->framing[x - 1];
  402. }
  403. /* And insert the new entry */
  404. pref->order[0] = format_index;
  405. pref->framing[0] = framing;
  406. }
  407. uint64_t iax2_codec_pref_from_bitfield(struct iax2_codec_pref *pref, uint64_t bitfield)
  408. {
  409. int bit;
  410. uint64_t working_bitfield;
  411. uint64_t best_bitfield;
  412. struct ast_format *format;
  413. /* Init the preference list. */
  414. memset(pref, 0, sizeof(*pref));
  415. working_bitfield = bitfield;
  416. /* Add the "best" codecs first. */
  417. while (working_bitfield) {
  418. best_bitfield = iax2_format_compatibility_best(working_bitfield);
  419. if (!best_bitfield) {
  420. /* No more codecs considered best. */
  421. break;
  422. }
  423. /* Remove current "best" codec to find the next "best". */
  424. working_bitfield &= ~best_bitfield;
  425. format = ast_format_compatibility_bitfield2format(best_bitfield);
  426. /* The best_bitfield should always be convertible to a format. */
  427. ast_assert(format != NULL);
  428. iax2_codec_pref_append_bitfield(pref, best_bitfield, 0);
  429. }
  430. /* Add any remaining codecs. */
  431. if (working_bitfield) {
  432. for (bit = 0; bit < 64; ++bit) {
  433. uint64_t mask = (1ULL << bit);
  434. if (mask & working_bitfield) {
  435. format = ast_format_compatibility_bitfield2format(mask);
  436. if (!format) {
  437. /* The bit is not associated with any format. */
  438. bitfield &= ~mask;
  439. continue;
  440. }
  441. iax2_codec_pref_append_bitfield(pref, mask, 0);
  442. }
  443. }
  444. }
  445. return bitfield;
  446. }