]> code.delx.au - pulseaudio/blob - src/pulsecore/core-format.c
799b2dae5b3f2e2b6f7b6bd51950224b1604b04f
[pulseaudio] / src / pulsecore / core-format.c
1 /***
2 This file is part of PulseAudio.
3
4 PulseAudio is free software; you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as published
6 by the Free Software Foundation; either version 2.1 of the License,
7 or (at your option) any later version.
8
9 PulseAudio is distributed in the hope that it will be useful, but
10 WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public License
15 along with PulseAudio; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
17 USA.
18 ***/
19
20 #ifdef HAVE_CONFIG_H
21 #include <config.h>
22 #endif
23
24 #include "core-format.h"
25
26 #include <pulse/def.h>
27 #include <pulse/xmalloc.h>
28
29 #include <pulsecore/macro.h>
30
31 int pa_format_info_get_sample_format(pa_format_info *f, pa_sample_format_t *sf) {
32 int r;
33 char *sf_str;
34 pa_sample_format_t sf_local;
35
36 pa_assert(f);
37 pa_assert(sf);
38
39 r = pa_format_info_get_prop_string(f, PA_PROP_FORMAT_SAMPLE_FORMAT, &sf_str);
40 if (r < 0)
41 return r;
42
43 sf_local = pa_parse_sample_format(sf_str);
44 pa_xfree(sf_str);
45
46 if (!pa_sample_format_valid(sf_local)) {
47 pa_log_debug("Invalid sample format.");
48 return -PA_ERR_INVALID;
49 }
50
51 *sf = sf_local;
52
53 return 0;
54 }
55
56 int pa_format_info_get_rate(pa_format_info *f, uint32_t *rate) {
57 int r;
58 int rate_local;
59
60 pa_assert(f);
61 pa_assert(rate);
62
63 r = pa_format_info_get_prop_int(f, PA_PROP_FORMAT_RATE, &rate_local);
64 if (r < 0)
65 return r;
66
67 if (!pa_sample_rate_valid(rate_local)) {
68 pa_log_debug("Invalid sample rate: %i", rate_local);
69 return -PA_ERR_INVALID;
70 }
71
72 *rate = rate_local;
73
74 return 0;
75 }
76
77 int pa_format_info_get_channels(pa_format_info *f, uint8_t *channels) {
78 int r;
79 int channels_local;
80
81 pa_assert(f);
82 pa_assert(channels);
83
84 r = pa_format_info_get_prop_int(f, PA_PROP_FORMAT_CHANNELS, &channels_local);
85 if (r < 0)
86 return r;
87
88 if (!pa_channels_valid(channels_local)) {
89 pa_log_debug("Invalid channel count: %i", channels_local);
90 return -PA_ERR_INVALID;
91 }
92
93 *channels = channels_local;
94
95 return 0;
96 }
97
98 int pa_format_info_get_channel_map(pa_format_info *f, pa_channel_map *map) {
99 int r;
100 char *map_str;
101
102 pa_assert(f);
103 pa_assert(map);
104
105 r = pa_format_info_get_prop_string(f, PA_PROP_FORMAT_CHANNEL_MAP, &map_str);
106 if (r < 0)
107 return r;
108
109 map = pa_channel_map_parse(map, map_str);
110 pa_xfree(map_str);
111
112 if (!map) {
113 pa_log_debug("Failed to parse channel map.");
114 return -PA_ERR_INVALID;
115 }
116
117 return 0;
118 }
119
120 int pa_format_info_to_sample_spec_fake(pa_format_info *f, pa_sample_spec *ss, pa_channel_map *map) {
121 int rate;
122
123 pa_assert(f);
124 pa_assert(ss);
125
126 /* Note: When we add support for non-IEC61937 encapsulated compressed
127 * formats, this function should return a non-zero values for these. */
128
129 ss->format = PA_SAMPLE_S16LE;
130 ss->channels = 2;
131
132 if (map)
133 pa_channel_map_init_stereo(map);
134
135 pa_return_val_if_fail(pa_format_info_get_prop_int(f, PA_PROP_FORMAT_RATE, &rate) == 0, -PA_ERR_INVALID);
136 ss->rate = (uint32_t) rate;
137
138 if (f->encoding == PA_ENCODING_EAC3_IEC61937)
139 ss->rate *= 4;
140
141 return 0;
142 }