format.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  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. /*!
  19. * \file
  20. * \brief Media Format API
  21. *
  22. * \author Joshua Colp <jcolp@digium.com>
  23. */
  24. #include "asterisk/codec.h"
  25. #ifndef _AST_FORMAT_H_
  26. #define _AST_FORMAT_H_
  27. struct ast_format;
  28. /*! \brief Format comparison results */
  29. enum ast_format_cmp_res {
  30. /*! Both formats are equivalent to each other */
  31. AST_FORMAT_CMP_EQUAL = 0,
  32. /*! Both formats are completely different and not the same in any way */
  33. AST_FORMAT_CMP_NOT_EQUAL,
  34. /*! Both formats are similar but not equivalent */
  35. AST_FORMAT_CMP_SUBSET,
  36. };
  37. /*! \brief Optional format interface to extend format operations */
  38. struct ast_format_interface {
  39. /*!
  40. * \brief Callback for when the format is destroyed, used to release attribute resources
  41. *
  42. * \param format The format structure to destroy
  43. */
  44. void (*const format_destroy)(struct ast_format *format);
  45. /*!
  46. * \brief Callback for when the format is cloned, used to clone attributes
  47. *
  48. * \param src Source format of attributes
  49. * \param dst Destination format for attributes
  50. *
  51. * \retval 0 success
  52. * \retval -1 failure
  53. */
  54. int (*const format_clone)(const struct ast_format *src, struct ast_format *dst);
  55. /*!
  56. * \brief Determine if format 1 is a subset of format 2.
  57. *
  58. * \param format1 First format to compare
  59. * \param format2 Second format which the first is compared against
  60. *
  61. * \retval ast_format_cmp_res representing the result of comparing format1 and format2.
  62. */
  63. enum ast_format_cmp_res (* const format_cmp)(const struct ast_format *format1,
  64. const struct ast_format *format2);
  65. /*!
  66. * \brief Get a format with the joint compatible attributes of both provided formats.
  67. *
  68. * \param format1 The first format
  69. * \param format2 The second format
  70. *
  71. * \retval non-NULL if joint format
  72. * \retval NULL if no joint format
  73. *
  74. * \note The returned format has its reference count incremented and must be released using
  75. * ao2_ref or ao2_cleanup.
  76. */
  77. struct ast_format *(* const format_get_joint)(const struct ast_format *format1,
  78. const struct ast_format *format2);
  79. /*!
  80. * \brief Set an attribute on a format
  81. *
  82. * \param name The name of the attribute
  83. * \param value The value of the attribute
  84. *
  85. * \retval non-NULL success
  86. * \retval NULL failure
  87. */
  88. struct ast_format *(* const format_attribute_set)(const struct ast_format *format,
  89. const char *name, const char *value);
  90. /*!
  91. * \brief Parse SDP attribute information, interpret it, and store it in the format structure.
  92. *
  93. * \param format Format to set attributes on
  94. * \param attributes A string containing only the attributes from the fmtp line
  95. *
  96. * \retval non-NULL Success, values were valid
  97. * \retval NULL Failure, some values were not acceptable
  98. */
  99. struct ast_format *(* const format_parse_sdp_fmtp)(const struct ast_format *format, const char *attributes);
  100. /*!
  101. * \brief Generate SDP attribute information from an ast_format structure.
  102. *
  103. * \param format The format containing attributes
  104. * \param payload The payload number to place into the fmtp line
  105. * \param str The generated fmtp line
  106. *
  107. * \note This callback should generate a full fmtp line using the provided payload number.
  108. */
  109. void (* const format_generate_sdp_fmtp)(const struct ast_format *format, unsigned int payload,
  110. struct ast_str **str);
  111. /*!
  112. * \since 13.6.0
  113. * \brief Retrieve a particular format attribute setting
  114. *
  115. * \param format The format containing attributes
  116. * \param name The name of the attribute to retrieve
  117. *
  118. * \retval NULL if the parameter is not set on the format
  119. * \retval non-NULL the format attribute value
  120. */
  121. const void *(* const format_attribute_get)(const struct ast_format *format, const char *name);
  122. };
  123. /*!
  124. * \brief Initialize media format support
  125. *
  126. * \retval 0 success
  127. * \retval -1 failure
  128. */
  129. int ast_format_init(void);
  130. /*!
  131. * \brief Create a new media format
  132. *
  133. * \param codec The codec to use
  134. *
  135. * \retval non-NULL success
  136. * \retval NULL failure
  137. *
  138. * \note The format is returned with reference count incremented. It must be released using
  139. * ao2_ref or ao2_cleanup.
  140. */
  141. struct ast_format *ast_format_create(struct ast_codec *codec);
  142. /*!
  143. * \brief Create a new media format with a specific name
  144. *
  145. * \param format_name The name to use for the format
  146. * \param codec The codec to use
  147. *
  148. * \note This creation function should be used when the name of the \c codec
  149. * cannot be explicitly used for the name of the format. This is the case for
  150. * codecs with multiple sample rates
  151. *
  152. * \note The format is returned with reference count incremented. It must be released using
  153. * ao2_ref or ao2_cleanup.
  154. *
  155. * \retval non-NULL success
  156. * \retval NULL failure
  157. */
  158. struct ast_format *ast_format_create_named(const char *format_name, struct ast_codec *codec);
  159. /*!
  160. * \brief Clone an existing media format so it can be modified
  161. *
  162. * \param format The existing media format
  163. *
  164. * \note The returned format is a new ao2 object. It must be released using ao2_cleanup.
  165. *
  166. * \retval non-NULL success
  167. * \retval NULL failure
  168. */
  169. struct ast_format *ast_format_clone(const struct ast_format *format);
  170. /*!
  171. * \brief Compare two formats
  172. *
  173. * \retval ast_format_cmp_res representing the result of comparing format1 and format2.
  174. */
  175. enum ast_format_cmp_res ast_format_cmp(const struct ast_format *format1, const struct ast_format *format2);
  176. /*!
  177. * \brief Get a common joint capability between two formats
  178. *
  179. * \retval non-NULL if joint capability exists
  180. * \retval NULL if no joint capability exists
  181. *
  182. * \note The returned format must be treated as immutable.
  183. */
  184. struct ast_format *ast_format_joint(const struct ast_format *format1, const struct ast_format *format2);
  185. /*!
  186. * \brief Set an attribute on a format to a specific value
  187. *
  188. * \param format The format to set the attribute on
  189. * \param name Attribute name
  190. * \param value Attribute value
  191. *
  192. * \retval non-NULL success
  193. * \retval NULL failure
  194. */
  195. struct ast_format *ast_format_attribute_set(const struct ast_format *format, const char *name,
  196. const char *value);
  197. /*!
  198. * \since 13.6.0
  199. *
  200. * \param format The format to retrieve the attribute from
  201. * \param name Attribute name
  202. *
  203. * \retval non-NULL the attribute value
  204. * \retval NULL the attribute does not exist or is unset
  205. */
  206. const void *ast_format_attribute_get(const struct ast_format *format, const char *name);
  207. /*!
  208. * \brief This function is used to have a media format aware module parse and interpret
  209. * SDP attribute information. Once interpreted this information is stored on the format
  210. * itself using Asterisk format attributes.
  211. *
  212. * \param format to set
  213. * \param attributes string containing the fmtp line from the SDP
  214. *
  215. * \retval non-NULL success, attribute values were valid
  216. * \retval NULL failure, values were not acceptable
  217. */
  218. struct ast_format *ast_format_parse_sdp_fmtp(const struct ast_format *format, const char *attributes);
  219. /*!
  220. * \brief This function is used to produce an fmtp SDP line for an Asterisk format. The
  221. * attributes present on the Asterisk format are translated into the SDP equivalent.
  222. *
  223. * \param format to generate an fmtp line for
  224. * \param payload numerical payload for the fmtp line
  225. * \param str structure that the fmtp line will be appended to
  226. */
  227. void ast_format_generate_sdp_fmtp(const struct ast_format *format, unsigned int payload, struct ast_str **str);
  228. /*!
  229. * \brief Register a format interface for use with the provided codec
  230. *
  231. * \param codec The name of codec the interface is applicable to
  232. * \param interface A pointer to the interface implementation
  233. * \param mod The module this format interface is provided by
  234. *
  235. * \retval 0 success
  236. * \retval -1 failure
  237. */
  238. int __ast_format_interface_register(const char *codec, const struct ast_format_interface *interface, struct ast_module *mod);
  239. /*!
  240. * \brief Register a format interface for use with the provided codec
  241. *
  242. * \param codec The name of codec the interface is applicable to
  243. * \param interface A pointer to the interface implementation
  244. *
  245. * \retval 0 success
  246. * \retval -1 failure
  247. */
  248. #define ast_format_interface_register(codec, interface) __ast_format_interface_register(codec, interface, ast_module_info->self)
  249. /*!
  250. * \brief Get the attribute data on a format
  251. *
  252. * \param format The media format
  253. *
  254. * \return Currently set attribute data
  255. */
  256. void *ast_format_get_attribute_data(const struct ast_format *format);
  257. /*!
  258. * \brief Set the attribute data on a format
  259. *
  260. * \param format The media format
  261. * \param attribute_data The attribute data
  262. */
  263. void ast_format_set_attribute_data(struct ast_format *format, void *attribute_data);
  264. /*!
  265. * \brief Get the name associated with a format
  266. *
  267. * \param format The media format
  268. *
  269. * \return The name of the format
  270. */
  271. const char *ast_format_get_name(const struct ast_format *format);
  272. /*!
  273. * \brief Get the codec associated with a format
  274. *
  275. * \param format The media format
  276. *
  277. * \return The codec
  278. *
  279. * \note The reference count of the returned codec is increased by 1 and must be decremented
  280. */
  281. struct ast_codec *ast_format_get_codec(const struct ast_format *format);
  282. /*!
  283. * \brief Get the codec identifier associated with a format
  284. *
  285. * \param format The media format
  286. *
  287. * \return codec identifier
  288. */
  289. unsigned int ast_format_get_codec_id(const struct ast_format *format);
  290. /*!
  291. * \brief Get the codec name associated with a format
  292. *
  293. * \param format The media format
  294. *
  295. * \return The codec name
  296. */
  297. const char *ast_format_get_codec_name(const struct ast_format *format);
  298. /*!
  299. * \brief Get whether or not the format can be smoothed
  300. *
  301. * \param format The media format
  302. *
  303. * \retval 0 the format cannot be smoothed
  304. * \retval 1 the format can be smoothed
  305. */
  306. int ast_format_can_be_smoothed(const struct ast_format *format);
  307. /*!
  308. * \since 13.17.0
  309. *
  310. * \brief Get smoother flags for this format
  311. *
  312. * \param format The media format
  313. *
  314. * \return smoother flags for the provided format
  315. */
  316. int ast_format_get_smoother_flags(const struct ast_format *format);
  317. /*!
  318. * \brief Get the media type of a format
  319. *
  320. * \param format The media format
  321. *
  322. * \return the media type
  323. */
  324. enum ast_media_type ast_format_get_type(const struct ast_format *format);
  325. /*!
  326. * \brief Get the default framing size (in milliseconds) for a format
  327. *
  328. * \param format The media format
  329. *
  330. * \return default framing size in milliseconds
  331. */
  332. unsigned int ast_format_get_default_ms(const struct ast_format *format);
  333. /*!
  334. * \brief Get the minimum amount of media carried in this format
  335. *
  336. * \param format The media format
  337. *
  338. * \return minimum framing size in milliseconds
  339. */
  340. unsigned int ast_format_get_minimum_ms(const struct ast_format *format);
  341. /*!
  342. * \brief Get the maximum amount of media carried in this format
  343. *
  344. * \param format The media format
  345. *
  346. * \return maximum framing size in milliseconds
  347. */
  348. unsigned int ast_format_get_maximum_ms(const struct ast_format *format);
  349. /*!
  350. * \brief Get the minimum number of bytes expected in a frame for this format
  351. *
  352. * \param format The media format
  353. *
  354. * \return minimum expected bytes in a frame for this format
  355. */
  356. unsigned int ast_format_get_minimum_bytes(const struct ast_format *format);
  357. /*!
  358. * \brief Get the sample rate of a media format
  359. *
  360. * \param format The media format
  361. *
  362. * \return sample rate
  363. */
  364. unsigned int ast_format_get_sample_rate(const struct ast_format *format);
  365. /*!
  366. * \brief Get the length (in milliseconds) for the format with a given number of samples
  367. *
  368. * \param format The media format
  369. * \param samples The number of samples
  370. *
  371. * \return length of media (in milliseconds)
  372. */
  373. unsigned int ast_format_determine_length(const struct ast_format *format, unsigned int samples);
  374. /*!
  375. * \since 12
  376. * \brief Get the message type used for signaling a format registration
  377. *
  378. * \retval Stasis message type for format registration
  379. * \retval NULL on error
  380. */
  381. struct stasis_message_type *ast_format_register_type(void);
  382. /*!
  383. * \since 12
  384. * \brief Get the message type used for signaling a format unregistration
  385. *
  386. * \retval Stasis message type for format unregistration
  387. * \retval NULL on error
  388. */
  389. struct stasis_message_type *ast_format_unregister_type(void);
  390. #endif /* _AST_FORMAT_H */