format.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. #ifndef fooformathfoo
  2. #define fooformathfoo
  3. /***
  4. This file is part of PulseAudio.
  5. Copyright 2011 Intel Corporation
  6. Copyright 2011 Collabora Multimedia
  7. Copyright 2011 Arun Raghavan <arun.raghavan@collabora.co.uk>
  8. PulseAudio is free software; you can redistribute it and/or modify
  9. it under the terms of the GNU Lesser General Public License as published
  10. by the Free Software Foundation; either version 2.1 of the License,
  11. or (at your option) any later version.
  12. PulseAudio is distributed in the hope that it will be useful, but
  13. WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. General Public License for more details.
  16. You should have received a copy of the GNU Lesser General Public License
  17. along with PulseAudio; if not, see <http://www.gnu.org/licenses/>.
  18. ***/
  19. #include <pulse/cdecl.h>
  20. #include <pulse/gccmacro.h>
  21. #include <pulse/proplist.h>
  22. #include <pulse/sample.h>
  23. #include <pulse/channelmap.h>
  24. /** \file
  25. * Utility functions for handling a stream or sink format. */
  26. PA_C_DECL_BEGIN
  27. /** Represents the type of encoding used in a stream or accepted by a sink. \since 1.0 */
  28. typedef enum pa_encoding {
  29. PA_ENCODING_ANY,
  30. /**< Any encoding format, PCM or compressed */
  31. PA_ENCODING_PCM,
  32. /**< Any PCM format */
  33. PA_ENCODING_AC3_IEC61937,
  34. /**< AC3 data encapsulated in IEC 61937 header/padding */
  35. PA_ENCODING_EAC3_IEC61937,
  36. /**< EAC3 data encapsulated in IEC 61937 header/padding */
  37. PA_ENCODING_MPEG_IEC61937,
  38. /**< MPEG-1 or MPEG-2 (Part 3, not AAC) data encapsulated in IEC 61937 header/padding */
  39. PA_ENCODING_DTS_IEC61937,
  40. /**< DTS data encapsulated in IEC 61937 header/padding */
  41. PA_ENCODING_MPEG2_AAC_IEC61937,
  42. /**< MPEG-2 AAC data encapsulated in IEC 61937 header/padding. \since 4.0 */
  43. PA_ENCODING_MAX,
  44. /**< Valid encoding types must be less than this value */
  45. PA_ENCODING_INVALID = -1,
  46. /**< Represents an invalid encoding */
  47. } pa_encoding_t;
  48. /** \cond fulldocs */
  49. #define PA_ENCODING_ANY PA_ENCODING_ANY
  50. #define PA_ENCODING_PCM PA_ENCODING_PCM
  51. #define PA_ENCODING_AC3_IEC61937 PA_ENCODING_AC3_IEC61937
  52. #define PA_ENCODING_EAC3_IEC61937 PA_ENCODING_EAC3_IEC61937
  53. #define PA_ENCODING_MPEG_IEC61937 PA_ENCODING_MPEG_IEC61937
  54. #define PA_ENCODING_DTS_IEC61937 PA_ENCODING_DTS_IEC61937
  55. #define PA_ENCODING_MPEG2_AAC_IEC61937 PA_ENCODING_MPEG2_AAC_IEC61937
  56. #define PA_ENCODING_MAX PA_ENCODING_MAX
  57. #define PA_ENCODING_INVALID PA_ENCODING_INVALID
  58. /** \endcond */
  59. /** Returns a printable string representing the given encoding type. \since 1.0 */
  60. const char *pa_encoding_to_string(pa_encoding_t e) PA_GCC_CONST;
  61. /** Converts a string of the form returned by \a pa_encoding_to_string() back to a \a pa_encoding_t. \since 1.0 */
  62. pa_encoding_t pa_encoding_from_string(const char *encoding);
  63. /** Represents the format of data provided in a stream or processed by a sink. \since 1.0 */
  64. typedef struct pa_format_info {
  65. pa_encoding_t encoding;
  66. /**< The encoding used for the format */
  67. pa_proplist *plist;
  68. /**< Additional encoding-specific properties such as sample rate, bitrate, etc. */
  69. } pa_format_info;
  70. /** Allocates a new \a pa_format_info structure. Clients must initialise at least the encoding field themselves. \since 1.0 */
  71. pa_format_info* pa_format_info_new(void);
  72. /** Returns a new \a pa_format_info struct and representing the same format as \a src. \since 1.0 */
  73. pa_format_info* pa_format_info_copy(const pa_format_info *src);
  74. /** Frees a \a pa_format_info structure. \since 1.0 */
  75. void pa_format_info_free(pa_format_info *f);
  76. /** Returns non-zero when the format info structure is valid. \since 1.0 */
  77. int pa_format_info_valid(const pa_format_info *f);
  78. /** Returns non-zero when the format info structure represents a PCM (i.e.\ uncompressed data) format. \since 1.0 */
  79. int pa_format_info_is_pcm(const pa_format_info *f);
  80. /** Returns non-zero if the format represented by \a first is a subset of
  81. * the format represented by \a second. This means that \a second must
  82. * have all the fields that \a first does, but the reverse need not
  83. * be true. This is typically expected to be used to check if a
  84. * stream's format is compatible with a given sink. In such a case,
  85. * \a first would be the sink's format and \a second would be the
  86. * stream's. \since 1.0 */
  87. int pa_format_info_is_compatible(const pa_format_info *first, const pa_format_info *second);
  88. /** Maximum required string length for
  89. * pa_format_info_snprint(). Please note that this value can change
  90. * with any release without warning and without being considered API
  91. * or ABI breakage. You should not use this definition anywhere where
  92. * it might become part of an ABI. \since 1.0 */
  93. #define PA_FORMAT_INFO_SNPRINT_MAX 256
  94. /** Return a human-readable string representing the given format. \since 1.0 */
  95. char *pa_format_info_snprint(char *s, size_t l, const pa_format_info *f);
  96. /** Parse a human-readable string of the form generated by
  97. * \a pa_format_info_snprint() into a pa_format_info structure. \since 1.0 */
  98. pa_format_info* pa_format_info_from_string(const char *str);
  99. /** Utility function to take a \a pa_sample_spec and generate the corresponding
  100. * \a pa_format_info.
  101. *
  102. * Note that if you want the server to choose some of the stream parameters,
  103. * for example the sample rate, so that they match the device parameters, then
  104. * you shouldn't use this function. In order to allow the server to choose
  105. * a parameter value, that parameter must be left unspecified in the
  106. * pa_format_info object, and this function always specifies all parameters. An
  107. * exception is the channel map: if you pass NULL for the channel map, then the
  108. * channel map will be left unspecified, allowing the server to choose it.
  109. *
  110. * \since 2.0 */
  111. pa_format_info* pa_format_info_from_sample_spec(const pa_sample_spec *ss, const pa_channel_map *map);
  112. /** Utility function to generate a \a pa_sample_spec and \a pa_channel_map corresponding to a given \a pa_format_info. The
  113. * conversion for PCM formats is straight-forward. For non-PCM formats, if there is a fixed size-time conversion (i.e. all
  114. * IEC61937-encapsulated formats), a "fake" sample spec whose size-time conversion corresponds to this format is provided and
  115. * the channel map argument is ignored. For formats with variable size-time conversion, this function will fail. Returns a
  116. * negative integer if conversion failed and 0 on success. \since 2.0 */
  117. int pa_format_info_to_sample_spec(const pa_format_info *f, pa_sample_spec *ss, pa_channel_map *map);
  118. /** Represents the type of value type of a property on a \ref pa_format_info. \since 2.0 */
  119. typedef enum pa_prop_type_t {
  120. PA_PROP_TYPE_INT,
  121. /**< Integer property */
  122. PA_PROP_TYPE_INT_RANGE,
  123. /**< Integer range property */
  124. PA_PROP_TYPE_INT_ARRAY,
  125. /**< Integer array property */
  126. PA_PROP_TYPE_STRING,
  127. /**< String property */
  128. PA_PROP_TYPE_STRING_ARRAY,
  129. /**< String array property */
  130. PA_PROP_TYPE_INVALID = -1,
  131. /**< Represents an invalid type */
  132. } pa_prop_type_t;
  133. /** \cond fulldocs */
  134. #define PA_PROP_TYPE_INT PA_PROP_TYPE_INT
  135. #define PA_PROP_TYPE_INT_RANGE PA_PROP_TYPE_INT_RANGE
  136. #define PA_PROP_TYPE_INT_ARRAY PA_PROP_TYPE_INT_ARRAY
  137. #define PA_PROP_TYPE_STRING PA_PROP_TYPE_STRING
  138. #define PA_PROP_TYPE_STRING_ARRAY PA_PROP_TYPE_STRING_ARRAY
  139. #define PA_PROP_TYPE_INVALID PA_PROP_TYPE_INVALID
  140. /** \endcond */
  141. /** Gets the type of property \a key in a given \ref pa_format_info. \since 2.0 */
  142. pa_prop_type_t pa_format_info_get_prop_type(const pa_format_info *f, const char *key);
  143. /** Gets an integer property from the given format info. Returns 0 on success and a negative integer on failure. \since 2.0 */
  144. int pa_format_info_get_prop_int(const pa_format_info *f, const char *key, int *v);
  145. /** Gets an integer range property from the given format info. Returns 0 on success and a negative integer on failure.
  146. * \since 2.0 */
  147. int pa_format_info_get_prop_int_range(const pa_format_info *f, const char *key, int *min, int *max);
  148. /** Gets an integer array property from the given format info. \a values contains the values and \a n_values contains the
  149. * number of elements. The caller must free \a values using \ref pa_xfree. Returns 0 on success and a negative integer on
  150. * failure. \since 2.0 */
  151. int pa_format_info_get_prop_int_array(const pa_format_info *f, const char *key, int **values, int *n_values);
  152. /** Gets a string property from the given format info. The caller must free the returned string using \ref pa_xfree. Returns
  153. * 0 on success and a negative integer on failure. \since 2.0 */
  154. int pa_format_info_get_prop_string(const pa_format_info *f, const char *key, char **v);
  155. /** Gets a string array property from the given format info. \a values contains the values and \a n_values contains
  156. * the number of elements. The caller must free \a values using \ref pa_format_info_free_string_array. Returns 0 on success and
  157. * a negative integer on failure. \since 2.0 */
  158. int pa_format_info_get_prop_string_array(const pa_format_info *f, const char *key, char ***values, int *n_values);
  159. /** Frees a string array returned by \ref pa_format_info_get_prop_string_array. \since 2.0 */
  160. void pa_format_info_free_string_array(char **values, int n_values);
  161. /** Sets an integer property on the given format info. \since 1.0 */
  162. void pa_format_info_set_prop_int(pa_format_info *f, const char *key, int value);
  163. /** Sets a property with a list of integer values on the given format info. \since 1.0 */
  164. void pa_format_info_set_prop_int_array(pa_format_info *f, const char *key, const int *values, int n_values);
  165. /** Sets a property which can have any value in a given integer range on the given format info. \since 1.0 */
  166. void pa_format_info_set_prop_int_range(pa_format_info *f, const char *key, int min, int max);
  167. /** Sets a string property on the given format info. \since 1.0 */
  168. void pa_format_info_set_prop_string(pa_format_info *f, const char *key, const char *value);
  169. /** Sets a property with a list of string values on the given format info. \since 1.0 */
  170. void pa_format_info_set_prop_string_array(pa_format_info *f, const char *key, const char **values, int n_values);
  171. /** Convenience method to set the sample format as a property on the given
  172. * format.
  173. *
  174. * Note for PCM: If the sample format is left unspecified in the pa_format_info
  175. * object, then the server will select the stream sample format. In that case
  176. * the stream sample format will most likely match the device sample format,
  177. * meaning that sample format conversion will be avoided.
  178. *
  179. * \since 1.0 */
  180. void pa_format_info_set_sample_format(pa_format_info *f, pa_sample_format_t sf);
  181. /** Convenience method to set the sampling rate as a property on the given
  182. * format.
  183. *
  184. * Note for PCM: If the sample rate is left unspecified in the pa_format_info
  185. * object, then the server will select the stream sample rate. In that case the
  186. * stream sample rate will most likely match the device sample rate, meaning
  187. * that sample rate conversion will be avoided.
  188. *
  189. * \since 1.0 */
  190. void pa_format_info_set_rate(pa_format_info *f, int rate);
  191. /** Convenience method to set the number of channels as a property on the given
  192. * format.
  193. *
  194. * Note for PCM: If the channel count is left unspecified in the pa_format_info
  195. * object, then the server will select the stream channel count. In that case
  196. * the stream channel count will most likely match the device channel count,
  197. * meaning that up/downmixing will be avoided.
  198. *
  199. * \since 1.0 */
  200. void pa_format_info_set_channels(pa_format_info *f, int channels);
  201. /** Convenience method to set the channel map as a property on the given
  202. * format.
  203. *
  204. * Note for PCM: If the channel map is left unspecified in the pa_format_info
  205. * object, then the server will select the stream channel map. In that case the
  206. * stream channel map will most likely match the device channel map, meaning
  207. * that remixing will be avoided.
  208. *
  209. * \since 1.0 */
  210. void pa_format_info_set_channel_map(pa_format_info *f, const pa_channel_map *map);
  211. PA_C_DECL_END
  212. #endif