format.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2010, Digium, Inc.
  5. *
  6. * David Vossel <dvossel@digium.com>
  7. * Mark Spencer <markster@digium.com>
  8. *
  9. * See http://www.asterisk.org for more information about
  10. * the Asterisk project. Please do not directly contact
  11. * any of the maintainers of this project for assistance;
  12. * the project provides a web site, mailing lists and IRC
  13. * channels for your use.
  14. *
  15. * This program is free software, distributed under the terms of
  16. * the GNU General Public License Version 2. See the LICENSE file
  17. * at the top of the source tree.
  18. */
  19. /*!
  20. * \file
  21. * \brief Format API
  22. *
  23. * \author David Vossel <dvossel@digium.com>
  24. * \author Mark Spencer <markster@digium.com>
  25. */
  26. /*** MODULEINFO
  27. <support_level>core</support_level>
  28. ***/
  29. #include "asterisk.h"
  30. ASTERISK_FILE_VERSION(__FILE__, "$Revision$");
  31. #include "asterisk/_private.h"
  32. #include "asterisk/format.h"
  33. #include "asterisk/astobj2.h"
  34. #include "asterisk/lock.h"
  35. #include "asterisk/frame.h"
  36. #include "asterisk/utils.h"
  37. #include "asterisk/cli.h"
  38. #include "asterisk/rtp_engine.h"
  39. #include "asterisk/config.h"
  40. #define FORMAT_CONFIG "codecs.conf"
  41. /*!
  42. * \brief Container for all the format attribute interfaces.
  43. * \note This container uses RWLOCKs instead of MUTEX locks. .
  44. * \note An ao2 container was chosen for fast lookup.
  45. */
  46. static struct ao2_container *interfaces;
  47. /*! a wrapper is used put interfaces into the ao2 container. */
  48. struct interface_ao2_wrapper {
  49. enum ast_format_id id;
  50. const struct ast_format_attr_interface *interface;
  51. };
  52. /*! \brief Format List container, This container is never directly accessed outside
  53. * of this file, and It only exists for building the format_list_array. */
  54. static struct ao2_container *format_list;
  55. /*! \brief Format List array is a read only array protected by a read write lock.
  56. * This array may be used outside this file with the use of reference counting to
  57. * guarantee safety for access by multiple threads. */
  58. static struct ast_format_list *format_list_array;
  59. static size_t format_list_array_len = 0;
  60. /*! \brief Locks the format list array so a reference can be taken safely. */
  61. static ast_rwlock_t format_list_array_lock;
  62. static int interface_cmp_cb(void *obj, void *arg, int flags)
  63. {
  64. struct interface_ao2_wrapper *wrapper1 = obj;
  65. struct interface_ao2_wrapper *wrapper2 = arg;
  66. return (wrapper2->id == wrapper1->id) ? CMP_MATCH | CMP_STOP : 0;
  67. }
  68. static int interface_hash_cb(const void *obj, const int flags)
  69. {
  70. const struct interface_ao2_wrapper *wrapper = obj;
  71. return wrapper->id;
  72. }
  73. void ast_format_copy(struct ast_format *dst, const struct ast_format *src)
  74. {
  75. *dst = *src;
  76. }
  77. void ast_format_set_video_mark(struct ast_format *format)
  78. {
  79. format->fattr.rtp_marker_bit = 1;
  80. }
  81. int ast_format_get_video_mark(const struct ast_format *format)
  82. {
  83. return format->fattr.rtp_marker_bit;
  84. }
  85. static struct interface_ao2_wrapper *find_interface(const struct ast_format *format)
  86. {
  87. struct interface_ao2_wrapper tmp_wrapper = {
  88. .id = format->id,
  89. };
  90. return ao2_find(interfaces, &tmp_wrapper, OBJ_POINTER);
  91. }
  92. static int has_interface(const struct ast_format *format)
  93. {
  94. struct interface_ao2_wrapper *wrapper;
  95. wrapper = find_interface(format);
  96. if (!wrapper) {
  97. return 0;
  98. }
  99. ao2_ref(wrapper, -1);
  100. return 1;
  101. }
  102. int ast_format_sdp_parse(struct ast_format *format, const char *attributes)
  103. {
  104. struct interface_ao2_wrapper *wrapper;
  105. int res;
  106. if (!(wrapper = find_interface(format))) {
  107. return 0;
  108. }
  109. ao2_rdlock(wrapper);
  110. if (!wrapper->interface || !wrapper->interface->format_attr_sdp_parse) {
  111. ao2_unlock(wrapper);
  112. ao2_ref(wrapper, -1);
  113. return 0;
  114. }
  115. res = wrapper->interface->format_attr_sdp_parse(&format->fattr, attributes);
  116. ao2_unlock(wrapper);
  117. ao2_ref(wrapper, -1);
  118. return res;
  119. }
  120. void ast_format_sdp_generate(const struct ast_format *format, unsigned int payload, struct ast_str **str)
  121. {
  122. struct interface_ao2_wrapper *wrapper;
  123. if (!(wrapper = find_interface(format))) {
  124. return;
  125. }
  126. ao2_rdlock(wrapper);
  127. if (!wrapper->interface || !wrapper->interface->format_attr_sdp_generate) {
  128. ao2_unlock(wrapper);
  129. ao2_ref(wrapper, -1);
  130. return;
  131. }
  132. wrapper->interface->format_attr_sdp_generate(&format->fattr, payload, str);
  133. ao2_unlock(wrapper);
  134. ao2_ref(wrapper, -1);
  135. }
  136. /*! \internal
  137. * \brief set format attributes using an interface
  138. */
  139. static int format_set_helper(struct ast_format *format, va_list ap)
  140. {
  141. struct interface_ao2_wrapper *wrapper;
  142. if (!(wrapper = find_interface(format))) {
  143. ast_log(LOG_WARNING, "Could not find format interface to set.\n");
  144. return -1;
  145. }
  146. ao2_rdlock(wrapper);
  147. if (!wrapper->interface || !wrapper->interface->format_attr_set) {
  148. ao2_unlock(wrapper);
  149. ao2_ref(wrapper, -1);
  150. return -1;
  151. }
  152. wrapper->interface->format_attr_set(&format->fattr, ap);
  153. ao2_unlock(wrapper);
  154. ao2_ref(wrapper, -1);
  155. return 0;
  156. }
  157. struct ast_format *ast_format_append(struct ast_format *format, ... )
  158. {
  159. va_list ap;
  160. va_start(ap, format);
  161. format_set_helper(format, ap);
  162. va_end(ap);
  163. return format;
  164. }
  165. struct ast_format *ast_format_set(struct ast_format *format, enum ast_format_id id, int set_attributes, ... )
  166. {
  167. /* initialize the structure before setting it. */
  168. ast_format_clear(format);
  169. format->id = id;
  170. if (set_attributes) {
  171. va_list ap;
  172. va_start(ap, set_attributes);
  173. format_set_helper(format, ap);
  174. va_end(ap);
  175. }
  176. return format;
  177. }
  178. void ast_format_clear(struct ast_format *format)
  179. {
  180. format->id = 0;
  181. memset(&format->fattr, 0, sizeof(format->fattr));
  182. }
  183. /*! \internal
  184. * \brief determine if a list of attribute key value pairs are set on a format
  185. */
  186. static int format_isset_helper(const struct ast_format *format, va_list ap)
  187. {
  188. int res;
  189. struct interface_ao2_wrapper *wrapper;
  190. struct ast_format tmp = {
  191. .id = format->id,
  192. .fattr = { { 0, }, },
  193. };
  194. if (!(wrapper = find_interface(format))) {
  195. return -1;
  196. }
  197. ao2_rdlock(wrapper);
  198. if (!wrapper->interface ||
  199. !wrapper->interface->format_attr_set ||
  200. !wrapper->interface->format_attr_cmp) {
  201. ao2_unlock(wrapper);
  202. ao2_ref(wrapper, -1);
  203. return -1;
  204. }
  205. /* if isset is present, use that function, else just build a new
  206. * format and use the cmp function */
  207. if (wrapper->interface->format_attr_isset) {
  208. res = wrapper->interface->format_attr_isset(&format->fattr, ap);
  209. } else {
  210. wrapper->interface->format_attr_set(&tmp.fattr, ap);
  211. /* use our tmp structure to tell if the attributes are set or not */
  212. res = wrapper->interface->format_attr_cmp(&tmp.fattr, &format->fattr);
  213. res = (res == AST_FORMAT_CMP_NOT_EQUAL) ? -1 : 0;
  214. }
  215. ao2_unlock(wrapper);
  216. ao2_ref(wrapper, -1);
  217. return res;
  218. }
  219. int ast_format_isset(const struct ast_format *format, ... )
  220. {
  221. va_list ap;
  222. int res;
  223. va_start(ap, format);
  224. res = format_isset_helper(format, ap);
  225. va_end(ap);
  226. return res;
  227. }
  228. int ast_format_get_value(const struct ast_format *format, int key, void *value)
  229. {
  230. int res = 0;
  231. struct interface_ao2_wrapper *wrapper;
  232. if (!(wrapper = find_interface(format))) {
  233. return -1;
  234. }
  235. ao2_rdlock(wrapper);
  236. if (!wrapper->interface ||
  237. !wrapper->interface->format_attr_get_val) {
  238. ao2_unlock(wrapper);
  239. ao2_ref(wrapper, -1);
  240. return -1;
  241. }
  242. res = wrapper->interface->format_attr_get_val(&format->fattr, key, value);
  243. ao2_unlock(wrapper);
  244. ao2_ref(wrapper, -1);
  245. return res;
  246. }
  247. /*! \internal
  248. * \brief cmp format attributes using an interface
  249. */
  250. static enum ast_format_cmp_res format_cmp_helper(const struct ast_format *format1, const struct ast_format *format2)
  251. {
  252. enum ast_format_cmp_res res = AST_FORMAT_CMP_EQUAL;
  253. struct interface_ao2_wrapper *wrapper;
  254. if (!(wrapper = find_interface(format1))) {
  255. return res;
  256. }
  257. ao2_rdlock(wrapper);
  258. if (!wrapper->interface || !wrapper->interface->format_attr_cmp) {
  259. ao2_unlock(wrapper);
  260. ao2_ref(wrapper, -1);
  261. return res;
  262. }
  263. res = wrapper->interface->format_attr_cmp(&format1->fattr, &format2->fattr);
  264. ao2_unlock(wrapper);
  265. ao2_ref(wrapper, -1);
  266. return res;
  267. }
  268. enum ast_format_cmp_res ast_format_cmp(const struct ast_format *format1, const struct ast_format *format2)
  269. {
  270. if (format1->id != format2->id) {
  271. return AST_FORMAT_CMP_NOT_EQUAL;
  272. }
  273. return format_cmp_helper(format1, format2);
  274. }
  275. /*! \internal
  276. * \brief get joint format attributes using an interface
  277. */
  278. static int format_joint_helper(const struct ast_format *format1, const struct ast_format *format2, struct ast_format *result)
  279. {
  280. int res = 0;
  281. struct interface_ao2_wrapper *wrapper;
  282. if (!(wrapper = find_interface(format1))) {
  283. /* if no interface is present, we assume formats are joint by id alone */
  284. return res;
  285. }
  286. ao2_rdlock(wrapper);
  287. if (wrapper->interface && wrapper->interface->format_attr_get_joint) {
  288. res = wrapper->interface->format_attr_get_joint(&format1->fattr, &format2->fattr, &result->fattr);
  289. }
  290. ao2_unlock(wrapper);
  291. ao2_ref(wrapper, -1);
  292. return res;
  293. }
  294. int ast_format_joint(const struct ast_format *format1, const struct ast_format *format2, struct ast_format *result)
  295. {
  296. if (format1->id != format2->id) {
  297. return -1;
  298. }
  299. result->id = format1->id;
  300. return format_joint_helper(format1, format2, result);
  301. }
  302. uint64_t ast_format_id_to_old_bitfield(enum ast_format_id id)
  303. {
  304. switch (id) {
  305. /*! G.723.1 compression */
  306. case AST_FORMAT_G723_1:
  307. return (1ULL << 0);
  308. /*! GSM compression */
  309. case AST_FORMAT_GSM:
  310. return (1ULL << 1);
  311. /*! Raw mu-law data (G.711) */
  312. case AST_FORMAT_ULAW:
  313. return (1ULL << 2);
  314. /*! Raw A-law data (G.711) */
  315. case AST_FORMAT_ALAW:
  316. return (1ULL << 3);
  317. /*! ADPCM (G.726, 32kbps, AAL2 codeword packing) */
  318. case AST_FORMAT_G726_AAL2:
  319. return (1ULL << 4);
  320. /*! ADPCM (IMA) */
  321. case AST_FORMAT_ADPCM:
  322. return (1ULL << 5);
  323. /*! Raw 16-bit Signed Linear (8000 Hz) PCM */
  324. case AST_FORMAT_SLINEAR:
  325. return (1ULL << 6);
  326. /*! LPC10, 180 samples/frame */
  327. case AST_FORMAT_LPC10:
  328. return (1ULL << 7);
  329. /*! G.729A audio */
  330. case AST_FORMAT_G729A:
  331. return (1ULL << 8);
  332. /*! SpeeX Free Compression */
  333. case AST_FORMAT_SPEEX:
  334. return (1ULL << 9);
  335. /*! iLBC Free Compression */
  336. case AST_FORMAT_ILBC:
  337. return (1ULL << 10);
  338. /*! ADPCM (G.726, 32kbps, RFC3551 codeword packing) */
  339. case AST_FORMAT_G726:
  340. return (1ULL << 11);
  341. /*! G.722 */
  342. case AST_FORMAT_G722:
  343. return (1ULL << 12);
  344. /*! G.722.1 (also known as Siren7, 32kbps assumed) */
  345. case AST_FORMAT_SIREN7:
  346. return (1ULL << 13);
  347. /*! G.722.1 Annex C (also known as Siren14, 48kbps assumed) */
  348. case AST_FORMAT_SIREN14:
  349. return (1ULL << 14);
  350. /*! Raw 16-bit Signed Linear (16000 Hz) PCM */
  351. case AST_FORMAT_SLINEAR16:
  352. return (1ULL << 15);
  353. /*! G.719 (64 kbps assumed) */
  354. case AST_FORMAT_G719:
  355. return (1ULL << 32);
  356. /*! SpeeX Wideband (16kHz) Free Compression */
  357. case AST_FORMAT_SPEEX16:
  358. return (1ULL << 33);
  359. /*! Raw mu-law data (G.711) */
  360. case AST_FORMAT_TESTLAW:
  361. return (1ULL << 47);
  362. /*! H.261 Video */
  363. case AST_FORMAT_H261:
  364. return (1ULL << 18);
  365. /*! H.263 Video */
  366. case AST_FORMAT_H263:
  367. return (1ULL << 19);
  368. /*! H.263+ Video */
  369. case AST_FORMAT_H263_PLUS:
  370. return (1ULL << 20);
  371. /*! H.264 Video */
  372. case AST_FORMAT_H264:
  373. return (1ULL << 21);
  374. /*! MPEG4 Video */
  375. case AST_FORMAT_MP4_VIDEO:
  376. return (1ULL << 22);
  377. /*! JPEG Images */
  378. case AST_FORMAT_JPEG:
  379. return (1ULL << 16);
  380. /*! PNG Images */
  381. case AST_FORMAT_PNG:
  382. return (1ULL << 17);
  383. /*! T.140 RED Text format RFC 4103 */
  384. case AST_FORMAT_T140RED:
  385. return (1ULL << 26);
  386. /*! T.140 Text format - ITU T.140, RFC 4103 */
  387. case AST_FORMAT_T140:
  388. return (1ULL << 27);
  389. default:
  390. return 0; /* not supported by old bitfield. */
  391. }
  392. return 0;
  393. }
  394. uint64_t ast_format_to_old_bitfield(const struct ast_format *format)
  395. {
  396. return ast_format_id_to_old_bitfield(format->id);
  397. }
  398. struct ast_format *ast_format_from_old_bitfield(struct ast_format *dst, uint64_t src)
  399. {
  400. switch (src) {
  401. /*! G.723.1 compression */
  402. case (1ULL << 0):
  403. return ast_format_set(dst, AST_FORMAT_G723_1, 0);
  404. /*! GSM compression */
  405. case (1ULL << 1):
  406. return ast_format_set(dst, AST_FORMAT_GSM, 0);
  407. /*! Raw mu-law data (G.711) */
  408. case (1ULL << 2):
  409. return ast_format_set(dst, AST_FORMAT_ULAW, 0);
  410. /*! Raw A-law data (G.711) */
  411. case (1ULL << 3):
  412. return ast_format_set(dst, AST_FORMAT_ALAW, 0);
  413. /*! ADPCM (G.726, 32kbps, AAL2 codeword packing) */
  414. case (1ULL << 4):
  415. return ast_format_set(dst, AST_FORMAT_G726_AAL2, 0);
  416. /*! ADPCM (IMA) */
  417. case (1ULL << 5):
  418. return ast_format_set(dst, AST_FORMAT_ADPCM, 0);
  419. /*! Raw 16-bit Signed Linear (8000 Hz) PCM */
  420. case (1ULL << 6):
  421. return ast_format_set(dst, AST_FORMAT_SLINEAR, 0);
  422. /*! LPC10, 180 samples/frame */
  423. case (1ULL << 7):
  424. return ast_format_set(dst, AST_FORMAT_LPC10, 0);
  425. /*! G.729A audio */
  426. case (1ULL << 8):
  427. return ast_format_set(dst, AST_FORMAT_G729A, 0);
  428. /*! SpeeX Free Compression */
  429. case (1ULL << 9):
  430. return ast_format_set(dst, AST_FORMAT_SPEEX, 0);
  431. /*! iLBC Free Compression */
  432. case (1ULL << 10):
  433. return ast_format_set(dst, AST_FORMAT_ILBC, 0);
  434. /*! ADPCM (G.726, 32kbps, RFC3551 codeword packing) */
  435. case (1ULL << 11):
  436. return ast_format_set(dst, AST_FORMAT_G726, 0);
  437. /*! G.722 */
  438. case (1ULL << 12):
  439. return ast_format_set(dst, AST_FORMAT_G722, 0);
  440. /*! G.722.1 (also known as Siren7, 32kbps assumed) */
  441. case (1ULL << 13):
  442. return ast_format_set(dst, AST_FORMAT_SIREN7, 0);
  443. /*! G.722.1 Annex C (also known as Siren14, 48kbps assumed) */
  444. case (1ULL << 14):
  445. return ast_format_set(dst, AST_FORMAT_SIREN14, 0);
  446. /*! Raw 16-bit Signed Linear (16000 Hz) PCM */
  447. case (1ULL << 15):
  448. return ast_format_set(dst, AST_FORMAT_SLINEAR16, 0);
  449. /*! G.719 (64 kbps assumed) */
  450. case (1ULL << 32):
  451. return ast_format_set(dst, AST_FORMAT_G719, 0);
  452. /*! SpeeX Wideband (16kHz) Free Compression */
  453. case (1ULL << 33):
  454. return ast_format_set(dst, AST_FORMAT_SPEEX16, 0);
  455. /*! Raw mu-law data (G.711) */
  456. case (1ULL << 47):
  457. return ast_format_set(dst, AST_FORMAT_TESTLAW, 0);
  458. /*! H.261 Video */
  459. case (1ULL << 18):
  460. return ast_format_set(dst, AST_FORMAT_H261, 0);
  461. /*! H.263 Video */
  462. case (1ULL << 19):
  463. return ast_format_set(dst, AST_FORMAT_H263, 0);
  464. /*! H.263+ Video */
  465. case (1ULL << 20):
  466. return ast_format_set(dst, AST_FORMAT_H263_PLUS, 0);
  467. /*! H.264 Video */
  468. case (1ULL << 21):
  469. return ast_format_set(dst, AST_FORMAT_H264, 0);
  470. /*! MPEG4 Video */
  471. case (1ULL << 22):
  472. return ast_format_set(dst, AST_FORMAT_MP4_VIDEO, 0);
  473. /*! JPEG Images */
  474. case (1ULL << 16):
  475. return ast_format_set(dst, AST_FORMAT_JPEG, 0);
  476. /*! PNG Images */
  477. case (1ULL << 17):
  478. return ast_format_set(dst, AST_FORMAT_PNG, 0);
  479. /*! T.140 RED Text format RFC 4103 */
  480. case (1ULL << 26):
  481. return ast_format_set(dst, AST_FORMAT_T140RED, 0);
  482. /*! T.140 Text format - ITU T.140, RFC 4103 */
  483. case (1ULL << 27):
  484. return ast_format_set(dst, AST_FORMAT_T140, 0);
  485. }
  486. ast_format_clear(dst);
  487. return NULL;
  488. }
  489. enum ast_format_id ast_format_id_from_old_bitfield(uint64_t src)
  490. {
  491. struct ast_format dst;
  492. if (ast_format_from_old_bitfield(&dst, src)) {
  493. return dst.id;
  494. }
  495. return 0;
  496. }
  497. int ast_format_is_slinear(const struct ast_format *format)
  498. {
  499. if (format->id == AST_FORMAT_SLINEAR ||
  500. format->id == AST_FORMAT_SLINEAR12 ||
  501. format->id == AST_FORMAT_SLINEAR16 ||
  502. format->id == AST_FORMAT_SLINEAR24 ||
  503. format->id == AST_FORMAT_SLINEAR32 ||
  504. format->id == AST_FORMAT_SLINEAR44 ||
  505. format->id == AST_FORMAT_SLINEAR48 ||
  506. format->id == AST_FORMAT_SLINEAR96 ||
  507. format->id == AST_FORMAT_SLINEAR192) {
  508. return 1;
  509. }
  510. return 0;
  511. }
  512. enum ast_format_id ast_format_slin_by_rate(unsigned int rate)
  513. {
  514. if (rate >= 192000) {
  515. return AST_FORMAT_SLINEAR192;
  516. } else if (rate >= 96000) {
  517. return AST_FORMAT_SLINEAR96;
  518. } else if (rate >= 48000) {
  519. return AST_FORMAT_SLINEAR48;
  520. } else if (rate >= 44100) {
  521. return AST_FORMAT_SLINEAR44;
  522. } else if (rate >= 32000) {
  523. return AST_FORMAT_SLINEAR32;
  524. } else if (rate >= 24000) {
  525. return AST_FORMAT_SLINEAR24;
  526. } else if (rate >= 16000) {
  527. return AST_FORMAT_SLINEAR16;
  528. } else if (rate >= 12000) {
  529. return AST_FORMAT_SLINEAR12;
  530. }
  531. return AST_FORMAT_SLINEAR;
  532. }
  533. const char* ast_getformatname(const struct ast_format *format)
  534. {
  535. int x;
  536. const char *ret = "unknown";
  537. size_t f_len;
  538. const struct ast_format_list *f_list = ast_format_list_get(&f_len);
  539. for (x = 0; x < f_len; x++) {
  540. if (ast_format_cmp(&f_list[x].format, format) == AST_FORMAT_CMP_EQUAL) {
  541. ret = f_list[x].name;
  542. break;
  543. }
  544. }
  545. f_list = ast_format_list_destroy(f_list);
  546. return ret;
  547. }
  548. char *ast_getformatname_multiple_byid(char *buf, size_t size, enum ast_format_id id)
  549. {
  550. int x;
  551. unsigned len;
  552. char *start, *end = buf;
  553. size_t f_len;
  554. const struct ast_format_list *f_list = ast_format_list_get(&f_len);
  555. if (!size) {
  556. f_list = ast_format_list_destroy(f_list);
  557. return buf;
  558. }
  559. snprintf(end, size, "(");
  560. len = strlen(end);
  561. end += len;
  562. size -= len;
  563. start = end;
  564. for (x = 0; x < f_len; x++) {
  565. if (f_list[x].format.id == id) {
  566. snprintf(end, size, "%s|", f_list[x].name);
  567. len = strlen(end);
  568. end += len;
  569. size -= len;
  570. }
  571. }
  572. if (start == end) {
  573. ast_copy_string(start, "nothing)", size);
  574. } else if (size > 1) {
  575. *(end - 1) = ')';
  576. }
  577. f_list = ast_format_list_destroy(f_list);
  578. return buf;
  579. }
  580. static struct ast_codec_alias_table {
  581. const char *alias;
  582. const char *realname;
  583. } ast_codec_alias_table[] = {
  584. { "slinear", "slin"},
  585. { "slinear16", "slin16"},
  586. { "g723.1", "g723"},
  587. { "g722.1", "siren7"},
  588. { "g722.1c", "siren14"},
  589. };
  590. static const char *ast_expand_codec_alias(const char *in)
  591. {
  592. int x;
  593. for (x = 0; x < ARRAY_LEN(ast_codec_alias_table); x++) {
  594. if (!strcmp(in,ast_codec_alias_table[x].alias))
  595. return ast_codec_alias_table[x].realname;
  596. }
  597. return in;
  598. }
  599. struct ast_format *ast_getformatbyname(const char *name, struct ast_format *result)
  600. {
  601. int x;
  602. size_t f_len;
  603. const struct ast_format_list *f_list = ast_format_list_get(&f_len);
  604. for (x = 0; x < f_len; x++) {
  605. if (!strcasecmp(f_list[x].name, name) ||
  606. !strcasecmp(f_list[x].name, ast_expand_codec_alias(name))) {
  607. ast_format_copy(result, &f_list[x].format);
  608. f_list = ast_format_list_destroy(f_list);
  609. return result;
  610. }
  611. }
  612. f_list = ast_format_list_destroy(f_list);
  613. return NULL;
  614. }
  615. const char *ast_codec2str(struct ast_format *format)
  616. {
  617. int x;
  618. const char *ret = "unknown";
  619. size_t f_len;
  620. const struct ast_format_list *f_list = ast_format_list_get(&f_len);
  621. for (x = 0; x < f_len; x++) {
  622. if (ast_format_cmp(&f_list[x].format, format) == AST_FORMAT_CMP_EQUAL) {
  623. ret = f_list[x].desc;
  624. break;
  625. }
  626. }
  627. f_list = ast_format_list_destroy(f_list);
  628. return ret;
  629. }
  630. int ast_format_rate(const struct ast_format *format)
  631. {
  632. switch (format->id) {
  633. case AST_FORMAT_SLINEAR12:
  634. return 12000;
  635. case AST_FORMAT_SLINEAR24:
  636. return 24000;
  637. case AST_FORMAT_SLINEAR32:
  638. return 32000;
  639. case AST_FORMAT_SLINEAR44:
  640. return 44100;
  641. case AST_FORMAT_SLINEAR48:
  642. return 48000;
  643. case AST_FORMAT_SLINEAR96:
  644. return 96000;
  645. case AST_FORMAT_SLINEAR192:
  646. return 192000;
  647. case AST_FORMAT_G722:
  648. case AST_FORMAT_SLINEAR16:
  649. case AST_FORMAT_SIREN7:
  650. case AST_FORMAT_SPEEX16:
  651. return 16000;
  652. case AST_FORMAT_SIREN14:
  653. case AST_FORMAT_SPEEX32:
  654. return 32000;
  655. case AST_FORMAT_G719:
  656. return 48000;
  657. case AST_FORMAT_SILK:
  658. if (!(ast_format_isset(format,
  659. SILK_ATTR_KEY_SAMP_RATE,
  660. SILK_ATTR_VAL_SAMP_24KHZ,
  661. AST_FORMAT_ATTR_END))) {
  662. return 24000;
  663. } else if (!(ast_format_isset(format,
  664. SILK_ATTR_KEY_SAMP_RATE,
  665. SILK_ATTR_VAL_SAMP_16KHZ,
  666. AST_FORMAT_ATTR_END))) {
  667. return 16000;
  668. } else if (!(ast_format_isset(format,
  669. SILK_ATTR_KEY_SAMP_RATE,
  670. SILK_ATTR_VAL_SAMP_12KHZ,
  671. AST_FORMAT_ATTR_END))) {
  672. return 12000;
  673. } else {
  674. return 8000;
  675. }
  676. case AST_FORMAT_CELT:
  677. {
  678. int samplerate;
  679. if (!(ast_format_get_value(format,
  680. CELT_ATTR_KEY_SAMP_RATE,
  681. &samplerate))) {
  682. return samplerate;
  683. }
  684. }
  685. default:
  686. return 8000;
  687. }
  688. }
  689. static char *show_codecs(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  690. {
  691. int x, found=0;
  692. size_t f_len;
  693. const struct ast_format_list *f_list;
  694. switch (cmd) {
  695. case CLI_INIT:
  696. e->command = "core show codecs [audio|video|image|text]";
  697. e->usage =
  698. "Usage: core show codecs [audio|video|image|text]\n"
  699. " Displays codec mapping\n";
  700. return NULL;
  701. case CLI_GENERATE:
  702. return NULL;
  703. }
  704. if ((a->argc < 3) || (a->argc > 4)) {
  705. return CLI_SHOWUSAGE;
  706. }
  707. f_list = ast_format_list_get(&f_len);
  708. if (!ast_opt_dont_warn) {
  709. ast_cli(a->fd, "Disclaimer: this command is for informational purposes only.\n"
  710. "\tIt does not indicate anything about your configuration.\n");
  711. }
  712. ast_cli(a->fd, "%8s %5s %8s %s\n","ID","TYPE","NAME","DESCRIPTION");
  713. ast_cli(a->fd, "-----------------------------------------------------------------------------------\n");
  714. for (x = 0; x < f_len; x++) {
  715. if (a->argc == 4) {
  716. if (!strcasecmp(a->argv[3], "audio")) {
  717. if (AST_FORMAT_GET_TYPE(f_list[x].format.id) != AST_FORMAT_TYPE_AUDIO) {
  718. continue;
  719. }
  720. } else if (!strcasecmp(a->argv[3], "video")) {
  721. if (AST_FORMAT_GET_TYPE(f_list[x].format.id) != AST_FORMAT_TYPE_VIDEO) {
  722. continue;
  723. }
  724. } else if (!strcasecmp(a->argv[3], "image")) {
  725. if (AST_FORMAT_GET_TYPE(f_list[x].format.id) != AST_FORMAT_TYPE_IMAGE) {
  726. continue;
  727. }
  728. } else if (!strcasecmp(a->argv[3], "text")) {
  729. if (AST_FORMAT_GET_TYPE(f_list[x].format.id) != AST_FORMAT_TYPE_TEXT) {
  730. continue;
  731. }
  732. } else {
  733. continue;
  734. }
  735. }
  736. ast_cli(a->fd, "%8u %5s %8s (%s)\n",
  737. f_list[x].format.id,
  738. (AST_FORMAT_GET_TYPE(f_list[x].format.id) == AST_FORMAT_TYPE_AUDIO) ? "audio" :
  739. (AST_FORMAT_GET_TYPE(f_list[x].format.id) == AST_FORMAT_TYPE_IMAGE) ? "image" :
  740. (AST_FORMAT_GET_TYPE(f_list[x].format.id) == AST_FORMAT_TYPE_VIDEO) ? "video" :
  741. (AST_FORMAT_GET_TYPE(f_list[x].format.id) == AST_FORMAT_TYPE_TEXT) ? "text" :
  742. "(unk)",
  743. f_list[x].name,
  744. f_list[x].desc);
  745. found = 1;
  746. }
  747. f_list = ast_format_list_destroy(f_list);
  748. if (!found) {
  749. return CLI_SHOWUSAGE;
  750. } else {
  751. return CLI_SUCCESS;
  752. }
  753. }
  754. static char *show_codec_n(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  755. {
  756. enum ast_format_id format_id;
  757. int x, found = 0;
  758. int type_punned_codec;
  759. size_t f_len;
  760. const struct ast_format_list *f_list;
  761. switch (cmd) {
  762. case CLI_INIT:
  763. e->command = "core show codec";
  764. e->usage =
  765. "Usage: core show codec <number>\n"
  766. " Displays codec mapping\n";
  767. return NULL;
  768. case CLI_GENERATE:
  769. return NULL;
  770. }
  771. if (a->argc != 4) {
  772. return CLI_SHOWUSAGE;
  773. }
  774. if (sscanf(a->argv[3], "%30d", &type_punned_codec) != 1) {
  775. return CLI_SHOWUSAGE;
  776. }
  777. format_id = type_punned_codec;
  778. f_list = ast_format_list_get(&f_len);
  779. for (x = 0; x < f_len; x++) {
  780. if (f_list[x].format.id == format_id) {
  781. found = 1;
  782. ast_cli(a->fd, "%11u %s\n", (unsigned int) format_id, f_list[x].desc);
  783. }
  784. }
  785. if (!found) {
  786. ast_cli(a->fd, "Codec %u not found\n", format_id);
  787. }
  788. f_list = ast_format_list_destroy(f_list);
  789. return CLI_SUCCESS;
  790. }
  791. /* Builtin Asterisk CLI-commands for debugging */
  792. static struct ast_cli_entry my_clis[] = {
  793. AST_CLI_DEFINE(show_codecs, "Displays a list of codecs"),
  794. AST_CLI_DEFINE(show_codec_n, "Shows a specific codec"),
  795. };
  796. int init_framer(void)
  797. {
  798. return 0;
  799. }
  800. static int format_list_add_custom(struct ast_format_list *new)
  801. {
  802. RAII_VAR(struct ast_format_list *, entry, NULL, ao2_cleanup);
  803. if (!(entry = ao2_alloc(sizeof(*entry), NULL))) {
  804. return -1;
  805. }
  806. memcpy(entry, new, sizeof(struct ast_format_list));
  807. entry->custom_entry = 1;
  808. ao2_link(format_list, entry);
  809. return 0;
  810. }
  811. static int format_list_add_static(
  812. const struct ast_format *format,
  813. const char *name,
  814. int samplespersecond,
  815. const char *description,
  816. int fr_len,
  817. int min_ms,
  818. int max_ms,
  819. int inc_ms,
  820. int def_ms,
  821. unsigned int flags,
  822. int cur_ms)
  823. {
  824. struct ast_format_list *entry;
  825. if (!(entry = ao2_alloc(sizeof(*entry), NULL))) {
  826. return -1;
  827. }
  828. ast_format_copy(&entry->format, format);
  829. ast_copy_string(entry->name, name, sizeof(entry->name));
  830. ast_copy_string(entry->desc, description, sizeof(entry->desc));
  831. entry->samplespersecond = samplespersecond;
  832. entry->fr_len = fr_len;
  833. entry->min_ms = min_ms;
  834. entry->max_ms = max_ms;
  835. entry->inc_ms = inc_ms;
  836. entry->def_ms = def_ms;
  837. entry->flags = flags;
  838. entry->cur_ms = cur_ms;
  839. entry->custom_entry = 0;
  840. ao2_link(format_list, entry);
  841. ao2_ref(entry, -1);
  842. return 0;
  843. }
  844. static int list_all_custom(void *obj, void *arg, int flag)
  845. {
  846. struct ast_format_list *entry = obj;
  847. return entry->custom_entry ? CMP_MATCH : 0;
  848. }
  849. static int list_cmp_cb(void *obj, void *arg, int flags)
  850. {
  851. struct ast_format_list *entry1 = obj;
  852. struct ast_format_list *entry2 = arg;
  853. return (ast_format_cmp(&entry1->format, &entry2->format) == AST_FORMAT_CMP_EQUAL) ? CMP_MATCH | CMP_STOP : 0;
  854. }
  855. const struct ast_format_list *ast_format_list_get(size_t *size)
  856. {
  857. struct ast_format_list *list;
  858. ast_rwlock_rdlock(&format_list_array_lock);
  859. ao2_ref(format_list_array, 1);
  860. list = format_list_array;
  861. *size = format_list_array_len;
  862. ast_rwlock_unlock(&format_list_array_lock);
  863. return list;
  864. }
  865. const struct ast_format_list *ast_format_list_destroy(const struct ast_format_list *list)
  866. {
  867. ao2_ref((void *) list, -1);
  868. return NULL;
  869. }
  870. static int build_format_list_array(void)
  871. {
  872. struct ast_format_list *tmp;
  873. size_t arraysize = sizeof(struct ast_format_list) * ao2_container_count(format_list);
  874. int i = 0;
  875. struct ao2_iterator it;
  876. ast_rwlock_wrlock(&format_list_array_lock);
  877. tmp = format_list_array;
  878. if (!(format_list_array = ao2_alloc(arraysize, NULL))) {
  879. format_list_array = tmp;
  880. ast_rwlock_unlock(&format_list_array_lock);
  881. return -1;
  882. }
  883. format_list_array_len = ao2_container_count(format_list);
  884. if (tmp) {
  885. ao2_ref(tmp, -1);
  886. }
  887. /* walk through the container adding elements to the static array */
  888. it = ao2_iterator_init(format_list, 0);
  889. while ((tmp = ao2_iterator_next(&it)) && (i < format_list_array_len)) {
  890. memcpy(&format_list_array[i], tmp, sizeof(struct ast_format_list));
  891. ao2_ref(tmp, -1);
  892. i++;
  893. }
  894. ao2_iterator_destroy(&it);
  895. ast_rwlock_unlock(&format_list_array_lock);
  896. return 0;
  897. }
  898. static int format_list_init(void)
  899. {
  900. struct ast_format tmpfmt;
  901. if (!(format_list = ao2_container_alloc(1, NULL, list_cmp_cb))) {
  902. return -1;
  903. }
  904. /*
  905. * initiate static entries XXX DO NOT CHANGE THIS ORDER!
  906. *
  907. * Reason:
  908. * The order is requried by chan_iax2 to send out the IAX_IE_CODEC_PREFS
  909. * list over the wire. The following order is historical to how v1.8
  910. * and earlier listed the formats in format.c:AST_FORMAT_LIST[]. These
  911. * formats have format compatibility bits assigned which makes chan_iax2
  912. * in particular and other legacy modules aware of them.
  913. */
  914. format_list_add_static(ast_format_set(&tmpfmt, AST_FORMAT_G723_1, 0), "g723", 8000, "G.723.1", 20, 30, 300, 30, 30, 0, 0); /*!< G723.1 */
  915. format_list_add_static(ast_format_set(&tmpfmt, AST_FORMAT_GSM, 0), "gsm", 8000, "GSM", 33, 20, 300, 20, 20, 0, 0); /*!< codec_gsm.c */
  916. format_list_add_static(ast_format_set(&tmpfmt, AST_FORMAT_ULAW, 0), "ulaw", 8000, "G.711 u-law", 80, 10, 150, 10, 20, 0, 0); /*!< codec_ulaw.c */
  917. format_list_add_static(ast_format_set(&tmpfmt, AST_FORMAT_ALAW, 0), "alaw", 8000, "G.711 A-law", 80, 10, 150, 10, 20, 0, 0); /*!< codec_alaw.c */
  918. format_list_add_static(ast_format_set(&tmpfmt, AST_FORMAT_G726, 0), "g726", 8000, "G.726 RFC3551", 40, 10, 300, 10, 20, 0, 0); /*!< codec_g726.c */
  919. format_list_add_static(ast_format_set(&tmpfmt, AST_FORMAT_ADPCM, 0), "adpcm" , 8000, "ADPCM", 40, 10, 300, 10, 20, 0, 0); /*!< codec_adpcm.c */
  920. format_list_add_static(ast_format_set(&tmpfmt, AST_FORMAT_SLINEAR, 0), "slin", 8000, "16 bit Signed Linear PCM", 160, 10, 70, 10, 20, AST_SMOOTHER_FLAG_BE, 0); /*!< Signed linear */
  921. format_list_add_static(ast_format_set(&tmpfmt, AST_FORMAT_LPC10, 0), "lpc10", 8000, "LPC10", 7, 20, 20, 20, 20, 0, 0); /*!< codec_lpc10.c */
  922. format_list_add_static(ast_format_set(&tmpfmt, AST_FORMAT_G729A, 0), "g729", 8000, "G.729A", 10, 10, 230, 10, 20, AST_SMOOTHER_FLAG_G729, 0); /*!< Binary commercial distribution */
  923. format_list_add_static(ast_format_set(&tmpfmt, AST_FORMAT_SPEEX, 0), "speex", 8000, "SpeeX", 10, 10, 60, 10, 20, 0, 0); /*!< codec_speex.c */
  924. format_list_add_static(ast_format_set(&tmpfmt, AST_FORMAT_SPEEX16, 0), "speex16", 16000, "SpeeX 16khz", 10, 10, 60, 10, 20, 0, 0); /*!< codec_speex.c */
  925. format_list_add_static(ast_format_set(&tmpfmt, AST_FORMAT_ILBC, 0), "ilbc", 8000, "iLBC", 50, 30, 30, 30, 30, 0, 0); /*!< codec_ilbc.c */ /* inc=30ms - workaround */
  926. format_list_add_static(ast_format_set(&tmpfmt, AST_FORMAT_G726_AAL2, 0), "g726aal2", 8000, "G.726 AAL2", 40, 10, 300, 10, 20, 0, 0); /*!< codec_g726.c */
  927. format_list_add_static(ast_format_set(&tmpfmt, AST_FORMAT_G722, 0), "g722", 16000, "G722", 80, 10, 150, 10, 20, 0, 0); /*!< codec_g722.c */
  928. format_list_add_static(ast_format_set(&tmpfmt, AST_FORMAT_SLINEAR16, 0), "slin16", 16000, "16 bit Signed Linear PCM (16kHz)", 320, 10, 70, 10, 20, AST_SMOOTHER_FLAG_BE, 0);/*!< Signed linear (16kHz) */
  929. format_list_add_static(ast_format_set(&tmpfmt, AST_FORMAT_JPEG, 0), "jpeg", 0, "JPEG image", 0, 0, 0, 0 ,0 ,0 ,0); /*!< See format_jpeg.c */
  930. format_list_add_static(ast_format_set(&tmpfmt, AST_FORMAT_PNG, 0), "png", 0, "PNG image", 0, 0, 0, 0 ,0 ,0 ,0); /*!< PNG Image format */
  931. format_list_add_static(ast_format_set(&tmpfmt, AST_FORMAT_H261, 0), "h261", 0, "H.261 Video", 0, 0, 0, 0 ,0 ,0 ,0); /*!< H.261 Video Passthrough */
  932. format_list_add_static(ast_format_set(&tmpfmt, AST_FORMAT_H263, 0), "h263", 0, "H.263 Video", 0, 0, 0, 0 ,0 ,0 ,0); /*!< H.263 Passthrough support, see format_h263.c */
  933. format_list_add_static(ast_format_set(&tmpfmt, AST_FORMAT_H263_PLUS, 0), "h263p", 0, "H.263+ Video", 0, 0, 0,0 ,0 ,0, 0); /*!< H.263plus passthrough support See format_h263.c */
  934. format_list_add_static(ast_format_set(&tmpfmt, AST_FORMAT_H264, 0), "h264", 0, "H.264 Video", 0, 0, 0, 0 ,0 ,0, 0); /*!< Passthrough support, see format_h263.c */
  935. format_list_add_static(ast_format_set(&tmpfmt, AST_FORMAT_MP4_VIDEO, 0), "mpeg4", 0, "MPEG4 Video", 0, 0, 0, 0, 0 ,0, 0); /*!< Passthrough support for MPEG4 */
  936. format_list_add_static(ast_format_set(&tmpfmt, AST_FORMAT_T140RED, 0), "red", 1, "T.140 Realtime Text with redundancy", 0, 0, 0,0 ,0 ,0, 0); /*!< Redundant T.140 Realtime Text */
  937. format_list_add_static(ast_format_set(&tmpfmt, AST_FORMAT_T140, 0), "t140", 0, "Passthrough T.140 Realtime Text", 0, 0, 0, 0 ,0 ,0, 0); /*!< Passthrough support for T.140 Realtime Text */
  938. format_list_add_static(ast_format_set(&tmpfmt, AST_FORMAT_SIREN7, 0), "siren7", 16000, "ITU G.722.1 (Siren7, licensed from Polycom)", 80, 20, 80, 20, 20, 0, 0); /*!< Binary commercial distribution */
  939. format_list_add_static(ast_format_set(&tmpfmt, AST_FORMAT_SIREN14, 0), "siren14", 32000, "ITU G.722.1 Annex C, (Siren14, licensed from Polycom)", 120, 20, 80, 20, 20, 0, 0); /*!< Binary commercial distribution */
  940. format_list_add_static(ast_format_set(&tmpfmt, AST_FORMAT_TESTLAW, 0), "testlaw", 8000, "G.711 test-law", 80, 10, 150, 10, 20, 0, 0); /*!< codec_ulaw.c */
  941. format_list_add_static(ast_format_set(&tmpfmt, AST_FORMAT_G719, 0), "g719", 48000, "ITU G.719", 160, 20, 80, 20, 20, 0, 0);
  942. /*
  943. * ORDER MAY CHANGE AFTER THIS POINT IN THE LIST
  944. *
  945. * Reason:
  946. * These formats and any that follow do NOT have format compatibility
  947. * bits assigned so chan_iax2 in particular and other legacy modules
  948. * that use compatibility bits will not be aware of them.
  949. */
  950. format_list_add_static(ast_format_set(&tmpfmt, AST_FORMAT_SPEEX32, 0), "speex32", 32000, "SpeeX 32khz", 10, 10, 60, 10, 20, 0, 0); /*!< codec_speex.c */
  951. format_list_add_static(ast_format_set(&tmpfmt, AST_FORMAT_SLINEAR12, 0), "slin12", 12000, "16 bit Signed Linear PCM (12kHz)", 240, 10, 70, 10, 20, AST_SMOOTHER_FLAG_BE, 0);/*!< Signed linear (12kHz) */
  952. format_list_add_static(ast_format_set(&tmpfmt, AST_FORMAT_SLINEAR24, 0), "slin24", 24000, "16 bit Signed Linear PCM (24kHz)", 480, 10, 70, 10, 20, AST_SMOOTHER_FLAG_BE, 0);/*!< Signed linear (24kHz) */
  953. format_list_add_static(ast_format_set(&tmpfmt, AST_FORMAT_SLINEAR32, 0), "slin32", 32000, "16 bit Signed Linear PCM (32kHz)", 640, 10, 70, 10, 20, AST_SMOOTHER_FLAG_BE, 0);/*!< Signed linear (32kHz) */
  954. format_list_add_static(ast_format_set(&tmpfmt, AST_FORMAT_SLINEAR44, 0), "slin44", 44100, "16 bit Signed Linear PCM (44kHz)", 882, 10, 70, 10, 20, AST_SMOOTHER_FLAG_BE, 0);/*!< Signed linear (44.1kHz) */
  955. format_list_add_static(ast_format_set(&tmpfmt, AST_FORMAT_SLINEAR48, 0), "slin48", 48000, "16 bit Signed Linear PCM (48kHz)", 960, 10, 70, 10, 20, AST_SMOOTHER_FLAG_BE, 0);/*!< Signed linear (48kHz) */
  956. format_list_add_static(ast_format_set(&tmpfmt, AST_FORMAT_SLINEAR96, 0), "slin96", 96000, "16 bit Signed Linear PCM (96kHz)", 1920, 10, 70, 10, 20, AST_SMOOTHER_FLAG_BE, 0);/*!< Signed linear (96kHz) */
  957. format_list_add_static(ast_format_set(&tmpfmt, AST_FORMAT_SLINEAR192, 0), "slin192", 192000, "16 bit Signed Linear PCM (192kHz)", 3840, 10, 70, 10, 20, AST_SMOOTHER_FLAG_BE, 0);/*!< Signed linear (192kHz) */
  958. return 0;
  959. }
  960. /*! \internal \brief Clean up resources on Asterisk shutdown */
  961. static void format_list_shutdown(void)
  962. {
  963. ast_rwlock_destroy(&format_list_array_lock);
  964. if (format_list) {
  965. ao2_t_ref(format_list, -1, "Unref format_list container in shutdown");
  966. format_list = NULL;
  967. }
  968. if (format_list_array) {
  969. ao2_t_ref(format_list_array, -1, "Unref format_list_array in shutdown");
  970. format_list_array = NULL;
  971. }
  972. }
  973. int ast_format_list_init(void)
  974. {
  975. if (ast_rwlock_init(&format_list_array_lock)) {
  976. return -1;
  977. }
  978. if (format_list_init()) {
  979. goto init_list_cleanup;
  980. }
  981. if (build_format_list_array()) {
  982. goto init_list_cleanup;
  983. }
  984. ast_register_cleanup(format_list_shutdown);
  985. return 0;
  986. init_list_cleanup:
  987. format_list_shutdown();
  988. return -1;
  989. }
  990. /*! \internal \brief Clean up resources on Asterisk shutdown */
  991. static void format_attr_shutdown(void)
  992. {
  993. ast_cli_unregister_multiple(my_clis, ARRAY_LEN(my_clis));
  994. if (interfaces) {
  995. ao2_ref(interfaces, -1);
  996. interfaces = NULL;
  997. }
  998. }
  999. int ast_format_attr_init(void)
  1000. {
  1001. interfaces = ao2_container_alloc_options(AO2_ALLOC_OPT_LOCK_RWLOCK,
  1002. 283, interface_hash_cb, interface_cmp_cb);
  1003. if (!interfaces) {
  1004. return -1;
  1005. }
  1006. ast_cli_register_multiple(my_clis, ARRAY_LEN(my_clis));
  1007. ast_register_cleanup(format_attr_shutdown);
  1008. return 0;
  1009. }
  1010. static int custom_celt_format(struct ast_format_list *entry, unsigned int maxbitrate, unsigned int framesize)
  1011. {
  1012. if (!entry->samplespersecond) {
  1013. ast_log(LOG_WARNING, "Custom CELT format definition '%s' requires sample rate to be defined.\n", entry->name);
  1014. }
  1015. ast_format_set(&entry->format, AST_FORMAT_CELT, 0);
  1016. if (!has_interface(&entry->format)) {
  1017. return -1;
  1018. }
  1019. snprintf(entry->desc, sizeof(entry->desc), "CELT Custom Format %ukhz", entry->samplespersecond/1000);
  1020. ast_format_append(&entry->format,
  1021. CELT_ATTR_KEY_SAMP_RATE, entry->samplespersecond,
  1022. CELT_ATTR_KEY_MAX_BITRATE, maxbitrate,
  1023. CELT_ATTR_KEY_FRAME_SIZE, framesize,
  1024. AST_FORMAT_ATTR_END);
  1025. entry->fr_len = 80;
  1026. entry->min_ms = 20;
  1027. entry->max_ms = 20;
  1028. entry->inc_ms = 20;
  1029. entry->def_ms = 20;
  1030. return 0;
  1031. }
  1032. static int custom_silk_format(struct ast_format_list *entry, unsigned int maxbitrate, int usedtx, int usefec, int packetloss_percentage)
  1033. {
  1034. if (!entry->samplespersecond) {
  1035. ast_log(LOG_WARNING, "Custom SILK format definition '%s' requires sample rate to be defined.\n", entry->name);
  1036. }
  1037. ast_format_set(&entry->format, AST_FORMAT_SILK, 0);
  1038. if (!has_interface(&entry->format)) {
  1039. return -1;
  1040. }
  1041. switch (entry->samplespersecond) {
  1042. case 8000:
  1043. ast_copy_string(entry->desc, "SILK Custom Format 8khz", sizeof(entry->desc));
  1044. ast_format_append(&entry->format,
  1045. SILK_ATTR_KEY_SAMP_RATE, SILK_ATTR_VAL_SAMP_8KHZ,
  1046. AST_FORMAT_ATTR_END);
  1047. break;
  1048. case 12000:
  1049. ast_copy_string(entry->desc, "SILK Custom Format 12khz", sizeof(entry->desc));
  1050. ast_format_append(&entry->format,
  1051. SILK_ATTR_KEY_SAMP_RATE, SILK_ATTR_VAL_SAMP_12KHZ,
  1052. AST_FORMAT_ATTR_END);
  1053. break;
  1054. case 16000:
  1055. ast_copy_string(entry->desc, "SILK Custom Format 16khz", sizeof(entry->desc));
  1056. ast_format_append(&entry->format,
  1057. SILK_ATTR_KEY_SAMP_RATE, SILK_ATTR_VAL_SAMP_16KHZ,
  1058. AST_FORMAT_ATTR_END);
  1059. break;
  1060. case 24000:
  1061. ast_copy_string(entry->desc, "SILK Custom Format 24khz", sizeof(entry->desc));
  1062. ast_format_append(&entry->format,
  1063. SILK_ATTR_KEY_SAMP_RATE, SILK_ATTR_VAL_SAMP_24KHZ,
  1064. AST_FORMAT_ATTR_END);
  1065. break;
  1066. default:
  1067. ast_log(LOG_WARNING, "Custom SILK format definition '%s' can not support sample rate %u\n", entry->name, entry->samplespersecond);
  1068. return -1;
  1069. }
  1070. ast_format_append(&entry->format,
  1071. SILK_ATTR_KEY_MAX_BITRATE, maxbitrate,
  1072. SILK_ATTR_KEY_DTX, usedtx ? 1 : 0,
  1073. SILK_ATTR_KEY_FEC, usefec ? 1 : 0,
  1074. SILK_ATTR_KEY_PACKETLOSS_PERCENTAGE, packetloss_percentage,
  1075. AST_FORMAT_ATTR_END);
  1076. entry->fr_len = 80;
  1077. entry->min_ms = 20;
  1078. entry->max_ms = 20;
  1079. entry->inc_ms = 20;
  1080. entry->def_ms = 20;
  1081. return 0;
  1082. }
  1083. static int conf_process_format_name(const char *name, enum ast_format_id *id)
  1084. {
  1085. if (!strcasecmp(name, "silk")) {
  1086. *id = AST_FORMAT_SILK;
  1087. } else if (!strcasecmp(name, "celt")) {
  1088. *id = AST_FORMAT_CELT;
  1089. } else {
  1090. *id = 0;
  1091. return -1;
  1092. }
  1093. return 0;
  1094. }
  1095. static int conf_process_sample_rate(const char *rate, unsigned int *result)
  1096. {
  1097. if (!strcasecmp(rate, "8000")) {
  1098. *result = 8000;
  1099. } else if (!strcasecmp(rate, "12000")) {
  1100. *result = 12000;
  1101. } else if (!strcasecmp(rate, "16000")) {
  1102. *result = 16000;
  1103. } else if (!strcasecmp(rate, "24000")) {
  1104. *result = 24000;
  1105. } else if (!strcasecmp(rate, "32000")) {
  1106. *result = 32000;
  1107. } else if (!strcasecmp(rate, "44100")) {
  1108. *result = 44100;
  1109. } else if (!strcasecmp(rate, "48000")) {
  1110. *result = 48000;
  1111. } else if (!strcasecmp(rate, "96000")) {
  1112. *result = 96000;
  1113. } else if (!strcasecmp(rate, "192000")) {
  1114. *result = 192000;
  1115. } else {
  1116. *result = 0;
  1117. return -1;
  1118. }
  1119. return 0;
  1120. }
  1121. static int load_format_config(void)
  1122. {
  1123. struct ast_flags config_flags = { 0, };
  1124. struct ast_config *cfg = ast_config_load(FORMAT_CONFIG, config_flags);
  1125. struct ast_format_list entry;
  1126. struct ast_variable *var;
  1127. char *cat = NULL;
  1128. int add_it = 0;
  1129. struct {
  1130. enum ast_format_id id;
  1131. unsigned int maxbitrate;
  1132. unsigned int framesize;
  1133. unsigned int packetloss_percentage;
  1134. int usefec;
  1135. int usedtx;
  1136. } settings;
  1137. if (cfg == CONFIG_STATUS_FILEMISSING || cfg == CONFIG_STATUS_FILEINVALID) {
  1138. return 0;
  1139. }
  1140. /* remove all custom formats from the AO2 Container. Note, this has no affect on the
  1141. * global format list until the list is rebuild. That is why this is okay to do while
  1142. * reloading the config. */
  1143. ao2_callback(format_list, OBJ_NODATA | OBJ_UNLINK | OBJ_MULTIPLE, list_all_custom, NULL);
  1144. while ((cat = ast_category_browse(cfg, cat))) {
  1145. memset(&entry, 0, sizeof(entry));
  1146. memset(&settings, 0, sizeof(settings));
  1147. add_it = 0;
  1148. if (!(ast_variable_retrieve(cfg, cat, "type"))) {
  1149. continue;
  1150. }
  1151. ast_copy_string(entry.name, cat, sizeof(entry.name));
  1152. var = ast_variable_browse(cfg, cat);
  1153. for (var = ast_variable_browse(cfg, cat); var; var = var->next) {
  1154. if (!strcasecmp(var->name, "type") && conf_process_format_name(var->value, &settings.id)) {
  1155. ast_log(LOG_WARNING, "Can not make custom format type for '%s' at line %d of %s\n",
  1156. var->value, var->lineno, FORMAT_CONFIG);
  1157. continue;
  1158. } else if (!strcasecmp(var->name, "samprate") && conf_process_sample_rate(var->value, &entry.samplespersecond)) {
  1159. ast_log(LOG_WARNING, "Sample rate '%s' at line %d of %s is not supported.\n",
  1160. var->value, var->lineno, FORMAT_CONFIG);
  1161. } else if (!strcasecmp(var->name, "maxbitrate")) {
  1162. if (sscanf(var->value, "%30u", &settings.maxbitrate) != 1) {
  1163. ast_log(LOG_WARNING, "maxbitrate '%s' at line %d of %s is not supported.\n",
  1164. var->value, var->lineno, FORMAT_CONFIG);
  1165. }
  1166. } else if (!strcasecmp(var->name, "framesize")) {
  1167. if (sscanf(var->value, "%30u", &settings.framesize) != 1) {
  1168. ast_log(LOG_WARNING, "framesize '%s' at line %d of %s is not supported.\n",
  1169. var->value, var->lineno, FORMAT_CONFIG);
  1170. }
  1171. } else if (!strcasecmp(var->name, "dtx")) {
  1172. settings.usedtx = ast_true(var->value) ? 1 : 0;
  1173. } else if (!strcasecmp(var->name, "fec")) {
  1174. settings.usefec = ast_true(var->value) ? 1 : 0;
  1175. } else if (!strcasecmp(var->name, "packetloss_percentage")) {
  1176. if ((sscanf(var->value, "%30u", &settings.packetloss_percentage) != 1) || (settings.packetloss_percentage > 100)) {
  1177. ast_log(LOG_WARNING, "packetloss_percentage '%s' at line %d of %s is not supported.\n",
  1178. var->value, var->lineno, FORMAT_CONFIG);
  1179. }
  1180. }
  1181. }
  1182. switch (settings.id) {
  1183. case AST_FORMAT_SILK:
  1184. if (!(custom_silk_format(&entry, settings.maxbitrate, settings.usedtx, settings.usefec, settings.packetloss_percentage))) {
  1185. add_it = 1;
  1186. }
  1187. break;
  1188. case AST_FORMAT_CELT:
  1189. if (!(custom_celt_format(&entry, settings.maxbitrate, settings.framesize))) {
  1190. add_it = 1;
  1191. }
  1192. break;
  1193. default:
  1194. ast_log(LOG_WARNING, "Can not create custom format %s\n", entry.name);
  1195. }
  1196. if (add_it) {
  1197. format_list_add_custom(&entry);
  1198. }
  1199. }
  1200. ast_config_destroy(cfg);
  1201. build_format_list_array();
  1202. return 0;
  1203. }
  1204. int ast_format_attr_reg_interface(const struct ast_format_attr_interface *interface)
  1205. {
  1206. int x;
  1207. size_t f_len;
  1208. const struct ast_format_list *f_list;
  1209. struct interface_ao2_wrapper *wrapper;
  1210. struct interface_ao2_wrapper tmp_wrapper = {
  1211. .id = interface->id,
  1212. };
  1213. /*
  1214. * Grab the write lock before checking for duplicates in
  1215. * anticipation of adding a new interface and to prevent a
  1216. * duplicate from sneaking in between the check and add.
  1217. */
  1218. ao2_wrlock(interfaces);
  1219. /* check for duplicates first*/
  1220. if ((wrapper = ao2_find(interfaces, &tmp_wrapper, (OBJ_POINTER | OBJ_NOLOCK)))) {
  1221. ao2_unlock(interfaces);
  1222. ast_log(LOG_WARNING, "Can not register attribute interface for format id %u, interface already exists.\n", interface->id);
  1223. ao2_ref(wrapper, -1);
  1224. return -1;
  1225. }
  1226. wrapper = ao2_alloc_options(sizeof(*wrapper), NULL, AO2_ALLOC_OPT_LOCK_RWLOCK);
  1227. if (!wrapper) {
  1228. ao2_unlock(interfaces);
  1229. return -1;
  1230. }
  1231. wrapper->interface = interface;
  1232. wrapper->id = interface->id;
  1233. /* The write lock is already held. */
  1234. ao2_link_flags(interfaces, wrapper, OBJ_NOLOCK);
  1235. ao2_unlock(interfaces);
  1236. ao2_ref(wrapper, -1);
  1237. /* This will find all custom formats in codecs.conf for this new registered interface */
  1238. load_format_config();
  1239. /* update the RTP engine to all custom formats created for this interface */
  1240. f_list = ast_format_list_get(&f_len);
  1241. for (x = 0; x < f_len; x++) {
  1242. if (f_list[x].format.id == tmp_wrapper.id) {
  1243. ast_rtp_engine_load_format(&f_list[x].format);
  1244. }
  1245. }
  1246. f_list = ast_format_list_destroy(f_list);
  1247. return 0;
  1248. }
  1249. int ast_format_attr_unreg_interface(const struct ast_format_attr_interface *interface)
  1250. {
  1251. int x;
  1252. size_t f_len;
  1253. const struct ast_format_list *f_list;
  1254. struct interface_ao2_wrapper *wrapper;
  1255. struct interface_ao2_wrapper tmp_wrapper = {
  1256. .id = interface->id,
  1257. };
  1258. if (!(wrapper = ao2_find(interfaces, &tmp_wrapper, (OBJ_POINTER | OBJ_UNLINK)))) {
  1259. return -1;
  1260. }
  1261. ao2_wrlock(wrapper);
  1262. wrapper->interface = NULL;
  1263. ao2_unlock(wrapper);
  1264. ao2_ref(wrapper, -1);
  1265. /* update the RTP engine to remove all custom formats created for this interface */
  1266. f_list = ast_format_list_get(&f_len);
  1267. for (x = 0; x < f_len; x++) {
  1268. if (f_list[x].format.id == tmp_wrapper.id) {
  1269. ast_rtp_engine_unload_format(&f_list[x].format);
  1270. }
  1271. }
  1272. /* This will remove all custom formats previously created for this interface */
  1273. load_format_config();
  1274. f_list = ast_format_list_destroy(f_list);
  1275. return 0;
  1276. }