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