]> code.delx.au - pulseaudio/blob - src/modules/oss-util.c
merge 'lennart' branch back into trunk.
[pulseaudio] / src / modules / oss-util.c
1 /* $Id$ */
2
3 /***
4 This file is part of PulseAudio.
5
6 Copyright 2004-2006 Lennart Poettering
7 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
8
9 PulseAudio is free software; you can redistribute it and/or modify
10 it under the terms of the GNU Lesser General Public License as published
11 by the Free Software Foundation; either version 2 of the License,
12 or (at your option) any later version.
13
14 PulseAudio is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 General Public License for more details.
18
19 You should have received a copy of the GNU Lesser General Public License
20 along with PulseAudio; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
22 USA.
23 ***/
24
25 #ifdef HAVE_CONFIG_H
26 #include <config.h>
27 #endif
28
29 #include <sys/soundcard.h>
30 #include <sys/ioctl.h>
31 #include <stdio.h>
32 #include <errno.h>
33 #include <string.h>
34 #include <unistd.h>
35 #include <sys/types.h>
36 #include <sys/stat.h>
37 #include <fcntl.h>
38
39 #include <pulse/xmalloc.h>
40 #include <pulsecore/core-error.h>
41 #include <pulsecore/core-util.h>
42 #include <pulsecore/log.h>
43 #include <pulsecore/macro.h>
44
45 #include "oss-util.h"
46
47 int pa_oss_open(const char *device, int *mode, int* pcaps) {
48 int fd = -1;
49 int caps;
50
51 pa_assert(device);
52 pa_assert(mode);
53 pa_assert(*mode == O_RDWR || *mode == O_RDONLY || *mode == O_WRONLY);
54
55 if(!pcaps)
56 pcaps = &caps;
57
58 if (*mode == O_RDWR) {
59 if ((fd = open(device, O_RDWR|O_NDELAY|O_NOCTTY)) >= 0) {
60 ioctl(fd, SNDCTL_DSP_SETDUPLEX, 0);
61
62 if (ioctl(fd, SNDCTL_DSP_GETCAPS, pcaps) < 0) {
63 pa_log("SNDCTL_DSP_GETCAPS: %s", pa_cstrerror(errno));
64 goto fail;
65 }
66
67 if (*pcaps & DSP_CAP_DUPLEX)
68 goto success;
69
70 pa_log_warn("'%s' doesn't support full duplex", device);
71
72 pa_close(fd);
73 }
74
75 if ((fd = open(device, (*mode = O_WRONLY)|O_NDELAY|O_NOCTTY)) < 0) {
76 if ((fd = open(device, (*mode = O_RDONLY)|O_NDELAY|O_NOCTTY)) < 0) {
77 pa_log("open('%s'): %s", device, pa_cstrerror(errno));
78 goto fail;
79 }
80 }
81 } else {
82 if ((fd = open(device, *mode|O_NDELAY|O_NOCTTY)) < 0) {
83 pa_log("open('%s'): %s", device, pa_cstrerror(errno));
84 goto fail;
85 }
86 }
87
88 *pcaps = 0;
89
90 if (ioctl(fd, SNDCTL_DSP_GETCAPS, pcaps) < 0) {
91 pa_log("SNDCTL_DSP_GETCAPS: %s", pa_cstrerror(errno));
92 goto fail;
93 }
94
95 success:
96
97 pa_log_debug("capabilities:%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
98 *pcaps & DSP_CAP_BATCH ? " BATCH" : "",
99 #ifdef DSP_CAP_BIND
100 *pcaps & DSP_CAP_BIND ? " BIND" : "",
101 #else
102 "",
103 #endif
104 *pcaps & DSP_CAP_COPROC ? " COPROC" : "",
105 *pcaps & DSP_CAP_DUPLEX ? " DUPLEX" : "",
106 #ifdef DSP_CAP_FREERATE
107 *pcaps & DSP_CAP_FREERATE ? " FREERATE" : "",
108 #else
109 "",
110 #endif
111 #ifdef DSP_CAP_INPUT
112 *pcaps & DSP_CAP_INPUT ? " INPUT" : "",
113 #else
114 "",
115 #endif
116 *pcaps & DSP_CAP_MMAP ? " MMAP" : "",
117 #ifdef DSP_CAP_MODEM
118 *pcaps & DSP_CAP_MODEM ? " MODEM" : "",
119 #else
120 "",
121 #endif
122 #ifdef DSP_CAP_MULTI
123 *pcaps & DSP_CAP_MULTI ? " MULTI" : "",
124 #else
125 "",
126 #endif
127 #ifdef DSP_CAP_OUTPUT
128 *pcaps & DSP_CAP_OUTPUT ? " OUTPUT" : "",
129 #else
130 "",
131 #endif
132 *pcaps & DSP_CAP_REALTIME ? " REALTIME" : "",
133 #ifdef DSP_CAP_SHADOW
134 *pcaps & DSP_CAP_SHADOW ? " SHADOW" : "",
135 #else
136 "",
137 #endif
138 #ifdef DSP_CAP_VIRTUAL
139 *pcaps & DSP_CAP_VIRTUAL ? " VIRTUAL" : "",
140 #else
141 "",
142 #endif
143 *pcaps & DSP_CAP_TRIGGER ? " TRIGGER" : "");
144
145 pa_make_fd_cloexec(fd);
146
147 return fd;
148
149 fail:
150 if (fd >= 0)
151 pa_close(fd);
152 return -1;
153 }
154
155 int pa_oss_auto_format(int fd, pa_sample_spec *ss) {
156 int format, channels, speed, reqformat;
157 pa_sample_format_t orig_format;
158
159 static const int format_trans[PA_SAMPLE_MAX] = {
160 [PA_SAMPLE_U8] = AFMT_U8,
161 [PA_SAMPLE_ALAW] = AFMT_A_LAW,
162 [PA_SAMPLE_ULAW] = AFMT_MU_LAW,
163 [PA_SAMPLE_S16LE] = AFMT_S16_LE,
164 [PA_SAMPLE_S16BE] = AFMT_S16_BE,
165 [PA_SAMPLE_FLOAT32LE] = AFMT_QUERY, /* not supported */
166 [PA_SAMPLE_FLOAT32BE] = AFMT_QUERY, /* not supported */
167 };
168
169 pa_assert(fd >= 0);
170 pa_assert(ss);
171
172 orig_format = ss->format;
173
174 reqformat = format = format_trans[ss->format];
175 if (reqformat == AFMT_QUERY || ioctl(fd, SNDCTL_DSP_SETFMT, &format) < 0 || format != reqformat) {
176 format = AFMT_S16_NE;
177 if (ioctl(fd, SNDCTL_DSP_SETFMT, &format) < 0 || format != AFMT_S16_NE) {
178 int f = AFMT_S16_NE == AFMT_S16_LE ? AFMT_S16_BE : AFMT_S16_LE;
179 format = f;
180 if (ioctl(fd, SNDCTL_DSP_SETFMT, &format) < 0 || format != f) {
181 format = AFMT_U8;
182 if (ioctl(fd, SNDCTL_DSP_SETFMT, &format) < 0 || format != AFMT_U8) {
183 pa_log("SNDCTL_DSP_SETFMT: %s", format != AFMT_U8 ? "No supported sample format" : pa_cstrerror(errno));
184 return -1;
185 } else
186 ss->format = PA_SAMPLE_U8;
187 } else
188 ss->format = f == AFMT_S16_LE ? PA_SAMPLE_S16LE : PA_SAMPLE_S16BE;
189 } else
190 ss->format = PA_SAMPLE_S16NE;
191 }
192
193 if (orig_format != ss->format)
194 pa_log_warn("device doesn't support sample format %s, changed to %s.",
195 pa_sample_format_to_string(orig_format),
196 pa_sample_format_to_string(ss->format));
197
198 channels = ss->channels;
199 if (ioctl(fd, SNDCTL_DSP_CHANNELS, &channels) < 0) {
200 pa_log("SNDCTL_DSP_CHANNELS: %s", pa_cstrerror(errno));
201 return -1;
202 }
203 pa_assert(channels > 0);
204
205 if (ss->channels != channels) {
206 pa_log_warn("device doesn't support %i channels, using %i channels.", ss->channels, channels);
207 ss->channels = channels;
208 }
209
210 speed = ss->rate;
211 if (ioctl(fd, SNDCTL_DSP_SPEED, &speed) < 0) {
212 pa_log("SNDCTL_DSP_SPEED: %s", pa_cstrerror(errno));
213 return -1;
214 }
215 pa_assert(speed > 0);
216
217 if (ss->rate != (unsigned) speed) {
218 pa_log_warn("device doesn't support %i Hz, changed to %i Hz.", ss->rate, speed);
219
220 /* If the sample rate deviates too much, we need to resample */
221 if (speed < ss->rate*.95 || speed > ss->rate*1.05)
222 ss->rate = speed;
223 }
224
225 return 0;
226 }
227
228 static int simple_log2(int v) {
229 int k = 0;
230
231 for (;;) {
232 v >>= 1;
233 if (!v) break;
234 k++;
235 }
236
237 return k;
238 }
239
240 int pa_oss_set_fragments(int fd, int nfrags, int frag_size) {
241 int arg;
242 arg = ((int) nfrags << 16) | simple_log2(frag_size);
243
244 if (ioctl(fd, SNDCTL_DSP_SETFRAGMENT, &arg) < 0) {
245 pa_log("SNDCTL_DSP_SETFRAGMENT: %s", pa_cstrerror(errno));
246 return -1;
247 }
248
249 return 0;
250 }
251
252 int pa_oss_get_volume(int fd, int mixer, const pa_sample_spec *ss, pa_cvolume *volume) {
253 char cv[PA_CVOLUME_SNPRINT_MAX];
254 unsigned vol;
255
256 pa_assert(fd >= 0);
257 pa_assert(ss);
258 pa_assert(volume);
259
260 if (ioctl(fd, mixer, &vol) < 0)
261 return -1;
262
263 pa_cvolume_reset(volume, ss->channels);
264
265 volume->values[0] = ((vol & 0xFF) * PA_VOLUME_NORM) / 100;
266
267 if (volume->channels >= 2)
268 volume->values[1] = (((vol >> 8) & 0xFF) * PA_VOLUME_NORM) / 100;
269
270 pa_log_debug("Read mixer settings: %s", pa_cvolume_snprint(cv, sizeof(cv), volume));
271 return 0;
272 }
273
274 int pa_oss_set_volume(int fd, long mixer, const pa_sample_spec *ss, const pa_cvolume *volume) {
275 char cv[PA_CVOLUME_SNPRINT_MAX];
276 unsigned vol;
277 pa_volume_t l, r;
278
279 l = volume->values[0] > PA_VOLUME_NORM ? PA_VOLUME_NORM : volume->values[0];
280
281 vol = (l*100)/PA_VOLUME_NORM;
282
283 if (ss->channels >= 2) {
284 r = volume->values[1] > PA_VOLUME_NORM ? PA_VOLUME_NORM : volume->values[1];
285 vol |= ((r*100)/PA_VOLUME_NORM) << 8;
286 }
287
288 if (ioctl(fd, mixer, &vol) < 0)
289 return -1;
290
291 pa_log_debug("Wrote mixer settings: %s", pa_cvolume_snprint(cv, sizeof(cv), volume));
292 return 0;
293 }
294
295 static int get_device_number(const char *dev) {
296 char buf[PATH_MAX];
297 const char *p, *e;
298
299 if (readlink(dev, buf, sizeof(buf)) < 0) {
300 if (errno != EINVAL && errno != ENOLINK)
301 return -1;
302
303 p = dev;
304 } else
305 p = buf;
306
307 if ((e = strrchr(p, '/')))
308 p = e+1;
309
310 if (p == 0)
311 return 0;
312
313 p = strchr(p, 0) -1;
314
315 if (*p >= '0' && *p <= '9')
316 return *p - '0';
317
318 return -1;
319 }
320
321 int pa_oss_get_hw_description(const char *dev, char *name, size_t l) {
322 FILE *f;
323 int n, r = -1;
324 int b = 0;
325
326 if ((n = get_device_number(dev)) < 0)
327 return -1;
328
329 if (!(f = fopen("/dev/sndstat", "r")) &&
330 !(f = fopen("/proc/sndstat", "r")) &&
331 !(f = fopen("/proc/asound/oss/sndstat", "r"))) {
332
333 if (errno != ENOENT)
334 pa_log_warn("failed to open OSS sndstat device: %s", pa_cstrerror(errno));
335
336 return -1;
337 }
338
339 while (!feof(f)) {
340 char line[64];
341 int device;
342
343 if (!fgets(line, sizeof(line), f))
344 break;
345
346 line[strcspn(line, "\r\n")] = 0;
347
348 if (!b) {
349 b = strcmp(line, "Audio devices:") == 0;
350 continue;
351 }
352
353 if (line[0] == 0)
354 break;
355
356 if (sscanf(line, "%i: ", &device) != 1)
357 continue;
358
359 if (device == n) {
360 char *k = strchr(line, ':');
361 pa_assert(k);
362 k++;
363 k += strspn(k, " ");
364
365 if (pa_endswith(k, " (DUPLEX)"))
366 k[strlen(k)-9] = 0;
367
368 pa_strlcpy(name, k, l);
369 r = 0;
370 break;
371 }
372 }
373
374 fclose(f);
375 return r;
376 }
377
378 static int open_mixer(const char *mixer) {
379 int fd;
380
381 if ((fd = open(mixer, O_RDWR|O_NDELAY|O_NOCTTY)) >= 0)
382 return fd;
383
384 return -1;
385 }
386
387 int pa_oss_open_mixer_for_device(const char *device) {
388 int n;
389 char *fn;
390 int fd;
391
392 if ((n = get_device_number(device)) < 0)
393 return -1;
394
395 if (n == 0)
396 if ((fd = open_mixer("/dev/mixer")) >= 0)
397 return fd;
398
399 fn = pa_sprintf_malloc("/dev/mixer%i", n);
400 fd = open_mixer(fn);
401 pa_xfree(fn);
402
403 if (fd < 0)
404 pa_log_warn("Failed to open mixer '%s': %s", device, pa_cstrerror(errno));
405
406 return fd;
407 }