]> code.delx.au - pulseaudio/blob - src/modules/oss-util.c
big s/polyp/pulse/g
[pulseaudio] / src / modules / oss-util.c
1 /* $Id$ */
2
3 /***
4 This file is part of PulseAudio.
5
6 PulseAudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published
8 by the Free Software Foundation; either version 2 of the License,
9 or (at your option) any later version.
10
11 PulseAudio is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with PulseAudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19 USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <assert.h>
27 #include <sys/soundcard.h>
28 #include <sys/ioctl.h>
29 #include <stdio.h>
30 #include <errno.h>
31 #include <string.h>
32 #include <unistd.h>
33 #include <sys/types.h>
34 #include <sys/stat.h>
35 #include <fcntl.h>
36
37 #include <pulsecore/core-error.h>
38 #include <pulsecore/core-util.h>
39 #include <pulsecore/log.h>
40
41 #include "oss-util.h"
42
43 int pa_oss_open(const char *device, int *mode, int* pcaps) {
44 int fd = -1;
45 assert(device && mode && (*mode == O_RDWR || *mode == O_RDONLY || *mode == O_WRONLY));
46
47 if (*mode == O_RDWR) {
48 if ((fd = open(device, O_RDWR|O_NDELAY)) >= 0) {
49 int dcaps, *tcaps;
50 ioctl(fd, SNDCTL_DSP_SETDUPLEX, 0);
51
52 tcaps = pcaps ? pcaps : &dcaps;
53
54 if (ioctl(fd, SNDCTL_DSP_GETCAPS, tcaps) < 0) {
55 pa_log(__FILE__": SNDCTL_DSP_GETCAPS: %s", pa_cstrerror(errno));
56 goto fail;
57 }
58
59 if (*tcaps & DSP_CAP_DUPLEX)
60 goto success;
61
62 pa_log_warn(__FILE__": '%s' doesn't support full duplex", device);
63
64 close(fd);
65 }
66
67 if ((fd = open(device, (*mode = O_WRONLY)|O_NDELAY)) < 0) {
68 if ((fd = open(device, (*mode = O_RDONLY)|O_NDELAY)) < 0) {
69 pa_log(__FILE__": open('%s'): %s", device, pa_cstrerror(errno));
70 goto fail;
71 }
72 }
73 } else {
74 if ((fd = open(device, *mode|O_NDELAY)) < 0) {
75 pa_log(__FILE__": open('%s'): %s", device, pa_cstrerror(errno));
76 goto fail;
77 }
78 }
79
80 success:
81
82 if (pcaps) {
83 if (ioctl(fd, SNDCTL_DSP_GETCAPS, pcaps) < 0) {
84 pa_log(__FILE__": SNDCTL_DSP_GETCAPS: %s", pa_cstrerror(errno));
85 goto fail;
86 }
87 }
88
89 pa_fd_set_cloexec(fd, 1);
90
91 return fd;
92
93 fail:
94 if (fd >= 0)
95 close(fd);
96 return -1;
97 }
98
99 int pa_oss_auto_format(int fd, pa_sample_spec *ss) {
100 int format, channels, speed, reqformat;
101 static const int format_trans[PA_SAMPLE_MAX] = {
102 [PA_SAMPLE_U8] = AFMT_U8,
103 [PA_SAMPLE_ALAW] = AFMT_A_LAW,
104 [PA_SAMPLE_ULAW] = AFMT_MU_LAW,
105 [PA_SAMPLE_S16LE] = AFMT_S16_LE,
106 [PA_SAMPLE_S16BE] = AFMT_S16_BE,
107 [PA_SAMPLE_FLOAT32LE] = AFMT_QUERY, /* not supported */
108 [PA_SAMPLE_FLOAT32BE] = AFMT_QUERY, /* not supported */
109 };
110
111 assert(fd >= 0 && ss);
112
113 reqformat = format = format_trans[ss->format];
114 if (reqformat == AFMT_QUERY || ioctl(fd, SNDCTL_DSP_SETFMT, &format) < 0 || format != reqformat) {
115 format = AFMT_S16_NE;
116 if (ioctl(fd, SNDCTL_DSP_SETFMT, &format) < 0 || format != AFMT_S16_NE) {
117 int f = AFMT_S16_NE == AFMT_S16_LE ? AFMT_S16_BE : AFMT_S16_LE;
118 format = f;
119 if (ioctl(fd, SNDCTL_DSP_SETFMT, &format) < 0 || format != f) {
120 format = AFMT_U8;
121 if (ioctl(fd, SNDCTL_DSP_SETFMT, &format) < 0 || format != AFMT_U8) {
122 pa_log(__FILE__": SNDCTL_DSP_SETFMT: %s", format != AFMT_U8 ? "No supported sample format" : pa_cstrerror(errno));
123 return -1;
124 } else
125 ss->format = PA_SAMPLE_U8;
126 } else
127 ss->format = f == AFMT_S16_LE ? PA_SAMPLE_S16LE : PA_SAMPLE_S16BE;
128 } else
129 ss->format = PA_SAMPLE_S16NE;
130 }
131
132 channels = ss->channels;
133 if (ioctl(fd, SNDCTL_DSP_CHANNELS, &channels) < 0) {
134 pa_log(__FILE__": SNDCTL_DSP_CHANNELS: %s", pa_cstrerror(errno));
135 return -1;
136 }
137 assert(channels);
138 ss->channels = channels;
139
140 speed = ss->rate;
141 if (ioctl(fd, SNDCTL_DSP_SPEED, &speed) < 0) {
142 pa_log(__FILE__": SNDCTL_DSP_SPEED: %s", pa_cstrerror(errno));
143 return -1;
144 }
145 assert(speed);
146 ss->rate = speed;
147
148 return 0;
149 }
150
151 static int simple_log2(int v) {
152 int k = 0;
153
154 for (;;) {
155 v >>= 1;
156 if (!v) break;
157 k++;
158 }
159
160 return k;
161 }
162
163 int pa_oss_set_fragments(int fd, int nfrags, int frag_size) {
164 int arg;
165 arg = ((int) nfrags << 16) | simple_log2(frag_size);
166
167 if (ioctl(fd, SNDCTL_DSP_SETFRAGMENT, &arg) < 0) {
168 pa_log(__FILE__": SNDCTL_DSP_SETFRAGMENT: %s", pa_cstrerror(errno));
169 return -1;
170 }
171
172 return 0;
173 }
174
175 static int pa_oss_get_volume(int fd, int mixer, const pa_sample_spec *ss, pa_cvolume *volume) {
176 char cv[PA_CVOLUME_SNPRINT_MAX];
177 unsigned vol;
178
179 assert(fd >= 0);
180 assert(ss);
181 assert(volume);
182
183 if (ioctl(fd, mixer, &vol) < 0)
184 return -1;
185
186 volume->values[0] = ((vol & 0xFF) * PA_VOLUME_NORM) / 100;
187
188 if ((volume->channels = ss->channels) >= 2)
189 volume->values[1] = (((vol >> 8) & 0xFF) * PA_VOLUME_NORM) / 100;
190
191 pa_log_debug(__FILE__": Read mixer settings: %s", pa_cvolume_snprint(cv, sizeof(cv), volume));
192 return 0;
193 }
194
195 static int pa_oss_set_volume(int fd, int mixer, const pa_sample_spec *ss, const pa_cvolume *volume) {
196 char cv[PA_CVOLUME_SNPRINT_MAX];
197 unsigned vol;
198 pa_volume_t l, r;
199
200 l = volume->values[0] > PA_VOLUME_NORM ? PA_VOLUME_NORM : volume->values[0];
201
202 vol = (l*100)/PA_VOLUME_NORM;
203
204 if (ss->channels >= 2) {
205 r = volume->values[1] > PA_VOLUME_NORM ? PA_VOLUME_NORM : volume->values[1];
206 vol |= ((r*100)/PA_VOLUME_NORM) << 8;
207 }
208
209 if (ioctl(fd, mixer, &vol) < 0)
210 return -1;
211
212 pa_log_debug(__FILE__": Wrote mixer settings: %s", pa_cvolume_snprint(cv, sizeof(cv), volume));
213 return 0;
214 }
215
216 int pa_oss_get_pcm_volume(int fd, const pa_sample_spec *ss, pa_cvolume *volume) {
217 return pa_oss_get_volume(fd, SOUND_MIXER_READ_PCM, ss, volume);
218 }
219
220 int pa_oss_set_pcm_volume(int fd, const pa_sample_spec *ss, const pa_cvolume *volume) {
221 return pa_oss_set_volume(fd, SOUND_MIXER_WRITE_PCM, ss, volume);
222 }
223
224 int pa_oss_get_input_volume(int fd, const pa_sample_spec *ss, pa_cvolume *volume) {
225 return pa_oss_get_volume(fd, SOUND_MIXER_READ_IGAIN, ss, volume);
226 }
227
228 int pa_oss_set_input_volume(int fd, const pa_sample_spec *ss, const pa_cvolume *volume) {
229 return pa_oss_set_volume(fd, SOUND_MIXER_WRITE_IGAIN, ss, volume);
230 }
231
232 int pa_oss_get_hw_description(const char *dev, char *name, size_t l) {
233 FILE *f;
234 const char *e = NULL;
235 int n, r = -1;
236 int b = 0;
237
238 if (strncmp(dev, "/dev/dsp", 8) == 0)
239 e = dev+8;
240 else if (strncmp(dev, "/dev/adsp", 9) == 0)
241 e = dev+9;
242 else
243 return -1;
244
245 if (*e == 0)
246 n = 0;
247 else if (*e >= '0' && *e <= '9' && *(e+1) == 0)
248 n = *e - '0';
249 else
250 return -1;
251
252 if (!(f = fopen("/dev/sndstat", "r")) &&
253 !(f = fopen("/proc/sndstat", "r")) &&
254 !(f = fopen("/proc/asound/oss/sndstat", "r"))) {
255
256 if (errno != ENOENT)
257 pa_log_warn(__FILE__": failed to open OSS sndstat device: %s", pa_cstrerror(errno));
258
259 return -1;
260 }
261
262 while (!feof(f)) {
263 char line[64];
264 int device;
265
266 if (!fgets(line, sizeof(line), f))
267 break;
268
269 line[strcspn(line, "\r\n")] = 0;
270
271 if (!b) {
272 b = strcmp(line, "Audio devices:") == 0;
273 continue;
274 }
275
276 if (line[0] == 0)
277 break;
278
279 if (sscanf(line, "%i: ", &device) != 1)
280 continue;
281
282 if (device == n) {
283 char *k = strchr(line, ':');
284 assert(k);
285 k++;
286 k += strspn(k, " ");
287
288 if (pa_endswith(k, " (DUPLEX)"))
289 k[strlen(k)-9] = 0;
290
291 pa_strlcpy(name, k, l);
292 r = 0;
293 break;
294 }
295 }
296
297 fclose(f);
298 return r;
299 }