]> code.delx.au - pulseaudio/blob - src/modules/oss-util.c
print the device capabilities after opening the device
[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 int caps;
46
47 assert(device && mode && (*mode == O_RDWR || *mode == O_RDONLY || *mode == O_WRONLY));
48
49 if(!pcaps)
50 pcaps = &caps;
51
52 if (*mode == O_RDWR) {
53 if ((fd = open(device, O_RDWR|O_NDELAY)) >= 0) {
54 int dcaps, *tcaps;
55 ioctl(fd, SNDCTL_DSP_SETDUPLEX, 0);
56
57 tcaps = pcaps ? pcaps : &dcaps;
58
59 if (ioctl(fd, SNDCTL_DSP_GETCAPS, tcaps) < 0) {
60 pa_log(__FILE__": SNDCTL_DSP_GETCAPS: %s", pa_cstrerror(errno));
61 goto fail;
62 }
63
64 if (*tcaps & DSP_CAP_DUPLEX)
65 goto success;
66
67 pa_log_warn(__FILE__": '%s' doesn't support full duplex", device);
68
69 close(fd);
70 }
71
72 if ((fd = open(device, (*mode = O_WRONLY)|O_NDELAY)) < 0) {
73 if ((fd = open(device, (*mode = O_RDONLY)|O_NDELAY)) < 0) {
74 pa_log(__FILE__": open('%s'): %s", device, pa_cstrerror(errno));
75 goto fail;
76 }
77 }
78 } else {
79 if ((fd = open(device, *mode|O_NDELAY)) < 0) {
80 pa_log(__FILE__": open('%s'): %s", device, pa_cstrerror(errno));
81 goto fail;
82 }
83 }
84
85 success:
86
87 *pcaps = 0;
88
89 if (ioctl(fd, SNDCTL_DSP_GETCAPS, pcaps) < 0) {
90 pa_log(__FILE__": SNDCTL_DSP_GETCAPS: %s", pa_cstrerror(errno));
91 goto fail;
92 }
93
94 pa_log_debug(__FILE__": capabilities:%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
95 *pcaps & DSP_CAP_BATCH ? " BATCH" : "",
96 *pcaps & DSP_CAP_BIND ? " BIND" : "",
97 *pcaps & DSP_CAP_COPROC ? " COPROC" : "",
98 *pcaps & DSP_CAP_DUPLEX ? " DUPLEX" : "",
99 #ifdef DSP_CAP_FREERATE
100 *pcaps & DSP_CAP_FREERATE ? " FREERATE" : "",
101 #else
102 "",
103 #endif
104 #ifdef DSP_CAP_INPUT
105 *pcaps & DSP_CAP_INPUT ? " INPUT" : "",
106 #else
107 "",
108 #endif
109 *pcaps & DSP_CAP_MMAP ? " MMAP" : "",
110 #ifdef DSP_CAP_MODEM
111 *pcaps & DSP_CAP_MODEM ? " MODEM" : "",
112 #else
113 "",
114 #endif
115 *pcaps & DSP_CAP_MULTI ? " MULTI" : "",
116 #ifdef DSP_CAP_OUTPUT
117 *pcaps & DSP_CAP_OUTPUT ? " OUTPUT" : "",
118 #else
119 "",
120 #endif
121 *pcaps & DSP_CAP_REALTIME ? " REALTIME" : "",
122 #ifdef DSP_CAP_SHADOW
123 *pcaps & DSP_CAP_SHADOW ? " SHADOW" : "",
124 #else
125 "",
126 #endif
127 #ifdef DSP_CAP_VIRTUAL
128 *pcaps & DSP_CAP_VIRTUAL ? " VIRTUAL" : "",
129 #else
130 "",
131 #endif
132 *pcaps & DSP_CAP_TRIGGER ? " TRIGGER" : "");
133
134 pa_fd_set_cloexec(fd, 1);
135
136 return fd;
137
138 fail:
139 if (fd >= 0)
140 close(fd);
141 return -1;
142 }
143
144 int pa_oss_auto_format(int fd, pa_sample_spec *ss) {
145 int format, channels, speed, reqformat;
146 pa_sample_format_t orig_format;
147
148 static const int format_trans[PA_SAMPLE_MAX] = {
149 [PA_SAMPLE_U8] = AFMT_U8,
150 [PA_SAMPLE_ALAW] = AFMT_A_LAW,
151 [PA_SAMPLE_ULAW] = AFMT_MU_LAW,
152 [PA_SAMPLE_S16LE] = AFMT_S16_LE,
153 [PA_SAMPLE_S16BE] = AFMT_S16_BE,
154 [PA_SAMPLE_FLOAT32LE] = AFMT_QUERY, /* not supported */
155 [PA_SAMPLE_FLOAT32BE] = AFMT_QUERY, /* not supported */
156 };
157
158 assert(fd >= 0 && ss);
159
160 orig_format = ss->format;
161
162 reqformat = format = format_trans[ss->format];
163 if (reqformat == AFMT_QUERY || ioctl(fd, SNDCTL_DSP_SETFMT, &format) < 0 || format != reqformat) {
164 format = AFMT_S16_NE;
165 if (ioctl(fd, SNDCTL_DSP_SETFMT, &format) < 0 || format != AFMT_S16_NE) {
166 int f = AFMT_S16_NE == AFMT_S16_LE ? AFMT_S16_BE : AFMT_S16_LE;
167 format = f;
168 if (ioctl(fd, SNDCTL_DSP_SETFMT, &format) < 0 || format != f) {
169 format = AFMT_U8;
170 if (ioctl(fd, SNDCTL_DSP_SETFMT, &format) < 0 || format != AFMT_U8) {
171 pa_log(__FILE__": SNDCTL_DSP_SETFMT: %s", format != AFMT_U8 ? "No supported sample format" : pa_cstrerror(errno));
172 return -1;
173 } else
174 ss->format = PA_SAMPLE_U8;
175 } else
176 ss->format = f == AFMT_S16_LE ? PA_SAMPLE_S16LE : PA_SAMPLE_S16BE;
177 } else
178 ss->format = PA_SAMPLE_S16NE;
179 }
180
181 if (orig_format != ss->format)
182 pa_log_warn(__FILE__": device doesn't support sample format %s, changed to %s.",
183 pa_sample_format_to_string(orig_format),
184 pa_sample_format_to_string(ss->format));
185
186 channels = ss->channels;
187 if (ioctl(fd, SNDCTL_DSP_CHANNELS, &channels) < 0) {
188 pa_log(__FILE__": SNDCTL_DSP_CHANNELS: %s", pa_cstrerror(errno));
189 return -1;
190 }
191 assert(channels > 0);
192
193 if (ss->channels != channels) {
194 pa_log_warn(__FILE__": device doesn't support %i channels, using %i channels.", ss->channels, channels);
195 ss->channels = channels;
196 }
197
198 speed = ss->rate;
199 if (ioctl(fd, SNDCTL_DSP_SPEED, &speed) < 0) {
200 pa_log(__FILE__": SNDCTL_DSP_SPEED: %s", pa_cstrerror(errno));
201 return -1;
202 }
203 assert(speed > 0);
204
205 if (ss->rate != (unsigned) speed) {
206 pa_log_warn(__FILE__": device doesn't support %i Hz, changed to %i Hz.", ss->rate, speed);
207
208 /* If the sample rate deviates too much, we need to resample */
209 if (speed < ss->rate*.95 || speed > ss->rate*1.05)
210 ss->rate = speed;
211 }
212
213 return 0;
214 }
215
216 static int simple_log2(int v) {
217 int k = 0;
218
219 for (;;) {
220 v >>= 1;
221 if (!v) break;
222 k++;
223 }
224
225 return k;
226 }
227
228 int pa_oss_set_fragments(int fd, int nfrags, int frag_size) {
229 int arg;
230 arg = ((int) nfrags << 16) | simple_log2(frag_size);
231
232 if (ioctl(fd, SNDCTL_DSP_SETFRAGMENT, &arg) < 0) {
233 pa_log(__FILE__": SNDCTL_DSP_SETFRAGMENT: %s", pa_cstrerror(errno));
234 return -1;
235 }
236
237 return 0;
238 }
239
240 static int pa_oss_get_volume(int fd, int mixer, const pa_sample_spec *ss, pa_cvolume *volume) {
241 char cv[PA_CVOLUME_SNPRINT_MAX];
242 unsigned vol;
243
244 assert(fd >= 0);
245 assert(ss);
246 assert(volume);
247
248 if (ioctl(fd, mixer, &vol) < 0)
249 return -1;
250
251 volume->values[0] = ((vol & 0xFF) * PA_VOLUME_NORM) / 100;
252
253 if ((volume->channels = ss->channels) >= 2)
254 volume->values[1] = (((vol >> 8) & 0xFF) * PA_VOLUME_NORM) / 100;
255
256 pa_log_debug(__FILE__": Read mixer settings: %s", pa_cvolume_snprint(cv, sizeof(cv), volume));
257 return 0;
258 }
259
260 static int pa_oss_set_volume(int fd, int mixer, const pa_sample_spec *ss, const pa_cvolume *volume) {
261 char cv[PA_CVOLUME_SNPRINT_MAX];
262 unsigned vol;
263 pa_volume_t l, r;
264
265 l = volume->values[0] > PA_VOLUME_NORM ? PA_VOLUME_NORM : volume->values[0];
266
267 vol = (l*100)/PA_VOLUME_NORM;
268
269 if (ss->channels >= 2) {
270 r = volume->values[1] > PA_VOLUME_NORM ? PA_VOLUME_NORM : volume->values[1];
271 vol |= ((r*100)/PA_VOLUME_NORM) << 8;
272 }
273
274 if (ioctl(fd, mixer, &vol) < 0)
275 return -1;
276
277 pa_log_debug(__FILE__": Wrote mixer settings: %s", pa_cvolume_snprint(cv, sizeof(cv), volume));
278 return 0;
279 }
280
281 int pa_oss_get_pcm_volume(int fd, const pa_sample_spec *ss, pa_cvolume *volume) {
282 return pa_oss_get_volume(fd, SOUND_MIXER_READ_PCM, ss, volume);
283 }
284
285 int pa_oss_set_pcm_volume(int fd, const pa_sample_spec *ss, const pa_cvolume *volume) {
286 return pa_oss_set_volume(fd, SOUND_MIXER_WRITE_PCM, ss, volume);
287 }
288
289 int pa_oss_get_input_volume(int fd, const pa_sample_spec *ss, pa_cvolume *volume) {
290 return pa_oss_get_volume(fd, SOUND_MIXER_READ_IGAIN, ss, volume);
291 }
292
293 int pa_oss_set_input_volume(int fd, const pa_sample_spec *ss, const pa_cvolume *volume) {
294 return pa_oss_set_volume(fd, SOUND_MIXER_WRITE_IGAIN, ss, volume);
295 }
296
297 int pa_oss_get_hw_description(const char *dev, char *name, size_t l) {
298 FILE *f;
299 const char *e = NULL;
300 int n, r = -1;
301 int b = 0;
302
303 if (strncmp(dev, "/dev/dsp", 8) == 0)
304 e = dev+8;
305 else if (strncmp(dev, "/dev/adsp", 9) == 0)
306 e = dev+9;
307 else
308 return -1;
309
310 if (*e == 0)
311 n = 0;
312 else if (*e >= '0' && *e <= '9' && *(e+1) == 0)
313 n = *e - '0';
314 else
315 return -1;
316
317 if (!(f = fopen("/dev/sndstat", "r")) &&
318 !(f = fopen("/proc/sndstat", "r")) &&
319 !(f = fopen("/proc/asound/oss/sndstat", "r"))) {
320
321 if (errno != ENOENT)
322 pa_log_warn(__FILE__": failed to open OSS sndstat device: %s", pa_cstrerror(errno));
323
324 return -1;
325 }
326
327 while (!feof(f)) {
328 char line[64];
329 int device;
330
331 if (!fgets(line, sizeof(line), f))
332 break;
333
334 line[strcspn(line, "\r\n")] = 0;
335
336 if (!b) {
337 b = strcmp(line, "Audio devices:") == 0;
338 continue;
339 }
340
341 if (line[0] == 0)
342 break;
343
344 if (sscanf(line, "%i: ", &device) != 1)
345 continue;
346
347 if (device == n) {
348 char *k = strchr(line, ':');
349 assert(k);
350 k++;
351 k += strspn(k, " ");
352
353 if (pa_endswith(k, " (DUPLEX)"))
354 k[strlen(k)-9] = 0;
355
356 pa_strlcpy(name, k, l);
357 r = 0;
358 break;
359 }
360 }
361
362 fclose(f);
363 return r;
364 }