format.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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 API
  21. *
  22. * \author Joshua Colp <jcolp@digium.com>
  23. */
  24. /*** MODULEINFO
  25. <support_level>core</support_level>
  26. ***/
  27. #include "asterisk.h"
  28. #include "asterisk/logger.h"
  29. #include "asterisk/codec.h"
  30. #include "asterisk/format.h"
  31. #include "asterisk/astobj2.h"
  32. #include "asterisk/strings.h"
  33. #include "asterisk/module.h"
  34. /*! \brief Number of buckets to use for format interfaces (should be prime for performance reasons) */
  35. #define FORMAT_INTERFACE_BUCKETS 53
  36. /*! \brief Definition of a media format */
  37. struct ast_format {
  38. /*! Name of the format */
  39. const char *name;
  40. /*! \brief Pointer to the codec in use for this format */
  41. struct ast_codec *codec;
  42. /*! \brief Attribute specific data, implementation specific */
  43. void *attribute_data;
  44. /*! \brief Pointer to the optional format interface */
  45. const struct ast_format_interface *interface;
  46. /*! \brief The number if audio channels used, if more than one an interleaved format is required */
  47. unsigned int channel_count;
  48. };
  49. /*! \brief Structure used when registering a format interface */
  50. struct format_interface {
  51. /*! \brief Pointer to the format interface itself */
  52. const struct ast_format_interface *interface;
  53. /*! \brief Name of the codec the interface is for */
  54. char codec[0];
  55. };
  56. /*! \brief Container for registered format interfaces */
  57. static struct ao2_container *interfaces;
  58. AO2_STRING_FIELD_HASH_FN(format_interface, codec)
  59. AO2_STRING_FIELD_CMP_FN(format_interface, codec)
  60. /*! \brief Function called when the process is shutting down */
  61. static void format_shutdown(void)
  62. {
  63. ao2_cleanup(interfaces);
  64. interfaces = NULL;
  65. }
  66. int ast_format_init(void)
  67. {
  68. interfaces = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_RWLOCK, 0,
  69. FORMAT_INTERFACE_BUCKETS, format_interface_hash_fn, NULL, format_interface_cmp_fn);
  70. if (!interfaces) {
  71. return -1;
  72. }
  73. ast_register_cleanup(format_shutdown);
  74. return 0;
  75. }
  76. int __ast_format_interface_register(const char *codec, const struct ast_format_interface *interface, struct ast_module *mod)
  77. {
  78. SCOPED_AO2WRLOCK(lock, interfaces);
  79. struct format_interface *format_interface;
  80. if (!interface->format_clone || !interface->format_destroy) {
  81. ast_log(LOG_ERROR, "Format interface for codec '%s' does not implement required callbacks\n", codec);
  82. return -1;
  83. }
  84. format_interface = ao2_find(interfaces, codec, OBJ_SEARCH_KEY | OBJ_NOLOCK);
  85. if (format_interface) {
  86. ast_log(LOG_ERROR, "A format interface is already present for codec '%s'\n", codec);
  87. ao2_ref(format_interface, -1);
  88. return -1;
  89. }
  90. format_interface = ao2_alloc_options(sizeof(*format_interface) + strlen(codec) + 1,
  91. NULL, AO2_ALLOC_OPT_LOCK_NOLOCK);
  92. if (!format_interface) {
  93. return -1;
  94. }
  95. format_interface->interface = interface;
  96. strcpy(format_interface->codec, codec); /* Safe */
  97. /* Once registered a format interface cannot be unregistered. */
  98. ast_module_shutdown_ref(mod);
  99. ao2_link_flags(interfaces, format_interface, OBJ_NOLOCK);
  100. ao2_ref(format_interface, -1);
  101. ast_verb(5, "Registered format interface for codec '%s'\n", codec);
  102. return 0;
  103. }
  104. void *ast_format_get_attribute_data(const struct ast_format *format)
  105. {
  106. return format->attribute_data;
  107. }
  108. void ast_format_set_attribute_data(struct ast_format *format, void *attribute_data)
  109. {
  110. format->attribute_data = attribute_data;
  111. }
  112. unsigned int ast_format_get_channel_count(const struct ast_format *format)
  113. {
  114. return format->channel_count;
  115. }
  116. void ast_format_set_channel_count(struct ast_format *format, unsigned int channel_count)
  117. {
  118. format->channel_count = channel_count;
  119. }
  120. /*! \brief Destructor for media formats */
  121. static void format_destroy(void *obj)
  122. {
  123. struct ast_format *format = obj;
  124. if (format->interface) {
  125. format->interface->format_destroy(format);
  126. }
  127. ao2_cleanup(format->codec);
  128. }
  129. struct ast_format *ast_format_create_named(const char *format_name, struct ast_codec *codec)
  130. {
  131. struct ast_format *format;
  132. struct format_interface *format_interface;
  133. format = ao2_t_alloc_options(sizeof(*format), format_destroy,
  134. AO2_ALLOC_OPT_LOCK_NOLOCK, S_OR(codec->description, ""));
  135. if (!format) {
  136. return NULL;
  137. }
  138. format->name = format_name;
  139. format->codec = ao2_bump(codec);
  140. format->channel_count = 1;
  141. format_interface = ao2_find(interfaces, codec->name, OBJ_SEARCH_KEY);
  142. if (format_interface) {
  143. format->interface = format_interface->interface;
  144. ao2_ref(format_interface, -1);
  145. }
  146. return format;
  147. }
  148. struct ast_format *ast_format_clone(const struct ast_format *format)
  149. {
  150. struct ast_format *cloned = ast_format_create_named(format->name, format->codec);
  151. if (!cloned) {
  152. return NULL;
  153. }
  154. if (cloned->interface && cloned->interface->format_clone(format, cloned)) {
  155. ao2_ref(cloned, -1);
  156. return NULL;
  157. }
  158. return cloned;
  159. }
  160. struct ast_format *ast_format_create(struct ast_codec *codec)
  161. {
  162. return ast_format_create_named(codec->name, codec);
  163. }
  164. enum ast_format_cmp_res ast_format_cmp(const struct ast_format *format1, const struct ast_format *format2)
  165. {
  166. const struct ast_format_interface *interface;
  167. if (format1 == NULL || format2 == NULL) {
  168. return AST_FORMAT_CMP_NOT_EQUAL;
  169. }
  170. if (format1 == format2) {
  171. return AST_FORMAT_CMP_EQUAL;
  172. }
  173. if (format1->codec != format2->codec) {
  174. return AST_FORMAT_CMP_NOT_EQUAL;
  175. }
  176. interface = format1->interface ? format1->interface : format2->interface;
  177. if (interface && interface->format_cmp) {
  178. return interface->format_cmp(format1, format2);
  179. }
  180. return AST_FORMAT_CMP_EQUAL;
  181. }
  182. struct ast_format *ast_format_joint(const struct ast_format *format1, const struct ast_format *format2)
  183. {
  184. const struct ast_format_interface *interface;
  185. if (format1->codec != format2->codec) {
  186. return NULL;
  187. }
  188. /* If the two formats are the same structure OR if the codec is the same and no attributes
  189. * exist we can immediately return a format with reference count bumped up, since they are
  190. * the same.
  191. */
  192. if ((ast_format_cmp(format1, format2) == AST_FORMAT_CMP_EQUAL && !format1->attribute_data && !format2->attribute_data)) {
  193. return ao2_bump((struct ast_format*)format1);
  194. }
  195. interface = format1->interface ? format1->interface : format2->interface;
  196. /* If there is attribute data on either there has to be an interface */
  197. return interface->format_get_joint(format1, format2);
  198. }
  199. struct ast_format *ast_format_attribute_set(const struct ast_format *format, const char *name, const char *value)
  200. {
  201. const struct ast_format_interface *interface = format->interface;
  202. if (!interface) {
  203. struct format_interface *format_interface = ao2_find(interfaces, format->codec->name, OBJ_SEARCH_KEY);
  204. if (format_interface) {
  205. interface = format_interface->interface;
  206. ao2_ref(format_interface, -1);
  207. }
  208. }
  209. if (!interface || !interface->format_attribute_set) {
  210. return ao2_bump((struct ast_format*)format);
  211. }
  212. return interface->format_attribute_set(format, name, value);
  213. }
  214. const void *ast_format_attribute_get(const struct ast_format *format, const char *name)
  215. {
  216. const struct ast_format_interface *interface = format->interface;
  217. if (!interface) {
  218. struct format_interface *format_interface = ao2_find(interfaces, format->codec->name, OBJ_SEARCH_KEY);
  219. if (format_interface) {
  220. interface = format_interface->interface;
  221. ao2_ref(format_interface, -1);
  222. }
  223. }
  224. if (!interface || !interface->format_attribute_get) {
  225. return NULL;
  226. }
  227. return interface->format_attribute_get(format, name);
  228. }
  229. struct ast_format *ast_format_parse_sdp_fmtp(const struct ast_format *format, const char *attributes)
  230. {
  231. const struct ast_format_interface *interface = format->interface;
  232. if (!interface) {
  233. struct format_interface *format_interface = ao2_find(interfaces, format->codec->name, OBJ_SEARCH_KEY);
  234. if (format_interface) {
  235. interface = format_interface->interface;
  236. ao2_ref(format_interface, -1);
  237. }
  238. }
  239. if (!interface || !interface->format_parse_sdp_fmtp) {
  240. return ao2_bump((struct ast_format*)format);
  241. }
  242. return interface->format_parse_sdp_fmtp(format, attributes);
  243. }
  244. void ast_format_generate_sdp_fmtp(const struct ast_format *format, unsigned int payload, struct ast_str **str)
  245. {
  246. const struct ast_format_interface *interface = format->interface;
  247. if (!interface) {
  248. struct format_interface *format_interface = ao2_find(interfaces, format->codec->name, OBJ_SEARCH_KEY);
  249. if (format_interface) {
  250. interface = format_interface->interface;
  251. ao2_ref(format_interface, -1);
  252. }
  253. }
  254. if (!interface || !interface->format_generate_sdp_fmtp) {
  255. return;
  256. }
  257. interface->format_generate_sdp_fmtp(format, payload, str);
  258. }
  259. struct ast_codec *ast_format_get_codec(const struct ast_format *format)
  260. {
  261. return ao2_bump(format->codec);
  262. }
  263. unsigned int ast_format_get_codec_id(const struct ast_format *format)
  264. {
  265. return format->codec->id;
  266. }
  267. const char *ast_format_get_name(const struct ast_format *format)
  268. {
  269. return format->name;
  270. }
  271. const char *ast_format_get_codec_name(const struct ast_format *format)
  272. {
  273. return format->codec->name;
  274. }
  275. int ast_format_can_be_smoothed(const struct ast_format *format)
  276. {
  277. return format->codec->smooth;
  278. }
  279. int ast_format_get_smoother_flags(const struct ast_format *format)
  280. {
  281. return format->codec->smoother_flags;
  282. }
  283. enum ast_media_type ast_format_get_type(const struct ast_format *format)
  284. {
  285. return format->codec->type;
  286. }
  287. unsigned int ast_format_get_default_ms(const struct ast_format *format)
  288. {
  289. return format->codec->default_ms;
  290. }
  291. unsigned int ast_format_get_minimum_ms(const struct ast_format *format)
  292. {
  293. return format->codec->minimum_ms;
  294. }
  295. unsigned int ast_format_get_maximum_ms(const struct ast_format *format)
  296. {
  297. return format->codec->maximum_ms;
  298. }
  299. unsigned int ast_format_get_minimum_bytes(const struct ast_format *format)
  300. {
  301. return format->codec->minimum_bytes;
  302. }
  303. unsigned int ast_format_get_sample_rate(const struct ast_format *format)
  304. {
  305. return format->codec->sample_rate ?: 8000;
  306. }
  307. unsigned int ast_format_determine_length(const struct ast_format *format, unsigned int samples)
  308. {
  309. return ast_codec_determine_length(format->codec, samples);
  310. }