codec.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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 Codec API
  21. *
  22. * \author Joshua Colp <jcolp@digium.com>
  23. */
  24. #ifndef _AST_CODEC_H_
  25. #define _AST_CODEC_H_
  26. #define AST_SMOOTHER_FLAGS_PACK(x) ((x) << 1)
  27. #define AST_SMOOTHER_FLAGS_UNPACK(x) ((x) >> 1)
  28. /*! \brief Types of media */
  29. enum ast_media_type {
  30. AST_MEDIA_TYPE_UNKNOWN = 0,
  31. AST_MEDIA_TYPE_AUDIO,
  32. AST_MEDIA_TYPE_VIDEO,
  33. AST_MEDIA_TYPE_IMAGE,
  34. AST_MEDIA_TYPE_TEXT,
  35. };
  36. struct ast_module;
  37. /*! \brief Represents a media codec within Asterisk. */
  38. struct ast_codec {
  39. /*! \brief Internal unique identifier for this codec, set at registration time (starts at 1) */
  40. unsigned int id;
  41. /*! \brief Name for this codec */
  42. const char *name;
  43. /*! \brief Brief description */
  44. const char *description;
  45. /*! \brief Type of media this codec contains */
  46. enum ast_media_type type;
  47. /*! \brief Sample rate (number of samples carried in a second) */
  48. unsigned int sample_rate;
  49. /*! \brief Minimum length of media that can be carried (in milliseconds) in a frame */
  50. unsigned int minimum_ms;
  51. /*! \brief Maximum length of media that can be carried (in milliseconds) in a frame */
  52. unsigned int maximum_ms;
  53. /*! \brief Default length of media carried (in milliseconds) in a frame */
  54. unsigned int default_ms;
  55. /*! \brief Length in bytes of the data payload of a minimum_ms frame */
  56. unsigned int minimum_bytes;
  57. /*!
  58. * \brief Retrieve the number of samples in a frame
  59. *
  60. * \param frame The frame to examine
  61. *
  62. * \return the number of samples
  63. */
  64. int (*samples_count)(struct ast_frame *frame);
  65. /*!
  66. * \brief Retrieve the length of media from number of samples
  67. *
  68. * \param samples The number of samples
  69. *
  70. * \return The length of media in milliseconds
  71. */
  72. int (*get_length)(unsigned int samples);
  73. /*! \brief Whether the media can be smoothed or not */
  74. unsigned int smooth;
  75. /*! \brief The module that registered this codec */
  76. struct ast_module *mod;
  77. };
  78. /*!
  79. * \brief Initialize codec support within the core.
  80. *
  81. * \retval 0 success
  82. * \retval -1 failure
  83. */
  84. int ast_codec_init(void);
  85. /*!
  86. * \brief Initialize built-in codecs within the core.
  87. *
  88. * \retval 0 success
  89. * \retval -1 failure
  90. */
  91. int ast_codec_builtin_init(void);
  92. /*!
  93. * \brief This function is used to register a codec with the Asterisk core. Registering
  94. * allows it to be passed through in frames and configured in channel drivers.
  95. *
  96. * \param codec to register
  97. * \param mod the module this codec is provided by
  98. *
  99. * \retval 0 success
  100. * \retval -1 failure
  101. */
  102. int __ast_codec_register(struct ast_codec *codec, struct ast_module *mod);
  103. /*!
  104. * \brief This function is used to register a codec with the Asterisk core. Registering
  105. * allows it to be passed through in frames and configured in channel drivers.
  106. *
  107. * \param codec to register
  108. *
  109. * \retval 0 success
  110. * \retval -1 failure
  111. */
  112. #define ast_codec_register(codec) __ast_codec_register(codec, ast_module_info->self)
  113. /*!
  114. * \brief Retrieve a codec given a name, type, and sample rate
  115. *
  116. * \param name The name of the codec
  117. * \param type The type of the codec
  118. * \param sample_rate Optional sample rate, may not be applicable for some types
  119. *
  120. * \retval non-NULL success
  121. * \retval NULL failure
  122. *
  123. * \note The returned codec is reference counted and ao2_ref or ao2_cleanup
  124. * must be used to release the reference.
  125. */
  126. struct ast_codec *ast_codec_get(const char *name, enum ast_media_type type, unsigned int sample_rate);
  127. /*!
  128. * \brief Retrieve a codec given the unique identifier
  129. *
  130. * \param id The unique identifier
  131. *
  132. * \retval non-NULL success
  133. * \retval NULL failure
  134. *
  135. * \note Identifiers start at 1 so if iterating don't start at 0.
  136. *
  137. * \note The returned codec is reference counted and ao2_ref or ao2_cleanup
  138. * must be used to release the reference.
  139. */
  140. struct ast_codec *ast_codec_get_by_id(int id);
  141. /*!
  142. * \brief Retrieve the current maximum identifier for codec iteration
  143. *
  144. * \return Maximum codec identifier
  145. */
  146. int ast_codec_get_max(void);
  147. /*!
  148. * \brief Conversion function to take a media type and turn it into a string
  149. *
  150. * \param type The media type
  151. *
  152. * \retval string representation of the media type
  153. */
  154. const char *ast_codec_media_type2str(enum ast_media_type type);
  155. /*!
  156. * \brief Get the number of samples contained within a frame
  157. *
  158. * \param frame The frame itself
  159. *
  160. * \retval number of samples in the frame
  161. */
  162. unsigned int ast_codec_samples_count(struct ast_frame *frame);
  163. /*!
  164. * \brief Get the length of media (in milliseconds) given a number of samples
  165. *
  166. * \param codec The codec itself
  167. * \param samples The number of samples
  168. *
  169. * \retval length of media (in milliseconds)
  170. */
  171. unsigned int ast_codec_determine_length(const struct ast_codec *codec, unsigned int samples);
  172. #endif /* _AST_CODEC_H */