]> code.delx.au - pulseaudio/blob - src/modules/oss-util.c
e29f0eda1a0c64adcafce7884152d791a05f468f
[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 [PA_SAMPLE_S32LE] = AFMT_QUERY, /* not supported */
168 [PA_SAMPLE_S32BE] = AFMT_QUERY, /* not supported */
169 };
170
171 pa_assert(fd >= 0);
172 pa_assert(ss);
173
174 orig_format = ss->format;
175
176 reqformat = format = format_trans[ss->format];
177 if (reqformat == AFMT_QUERY || ioctl(fd, SNDCTL_DSP_SETFMT, &format) < 0 || format != reqformat) {
178 format = AFMT_S16_NE;
179 if (ioctl(fd, SNDCTL_DSP_SETFMT, &format) < 0 || format != AFMT_S16_NE) {
180 int f = AFMT_S16_NE == AFMT_S16_LE ? AFMT_S16_BE : AFMT_S16_LE;
181 format = f;
182 if (ioctl(fd, SNDCTL_DSP_SETFMT, &format) < 0 || format != f) {
183 format = AFMT_U8;
184 if (ioctl(fd, SNDCTL_DSP_SETFMT, &format) < 0 || format != AFMT_U8) {
185 pa_log("SNDCTL_DSP_SETFMT: %s", format != AFMT_U8 ? "No supported sample format" : pa_cstrerror(errno));
186 return -1;
187 } else
188 ss->format = PA_SAMPLE_U8;
189 } else
190 ss->format = f == AFMT_S16_LE ? PA_SAMPLE_S16LE : PA_SAMPLE_S16BE;
191 } else
192 ss->format = PA_SAMPLE_S16NE;
193 }
194
195 if (orig_format != ss->format)
196 pa_log_warn("device doesn't support sample format %s, changed to %s.",
197 pa_sample_format_to_string(orig_format),
198 pa_sample_format_to_string(ss->format));
199
200 channels = ss->channels;
201 if (ioctl(fd, SNDCTL_DSP_CHANNELS, &channels) < 0) {
202 pa_log("SNDCTL_DSP_CHANNELS: %s", pa_cstrerror(errno));
203 return -1;
204 }
205 pa_assert(channels > 0);
206
207 if (ss->channels != channels) {
208 pa_log_warn("device doesn't support %i channels, using %i channels.", ss->channels, channels);
209 ss->channels = channels;
210 }
211
212 speed = ss->rate;
213 if (ioctl(fd, SNDCTL_DSP_SPEED, &speed) < 0) {
214 pa_log("SNDCTL_DSP_SPEED: %s", pa_cstrerror(errno));
215 return -1;
216 }
217 pa_assert(speed > 0);
218
219 if (ss->rate != (unsigned) speed) {
220 pa_log_warn("device doesn't support %i Hz, changed to %i Hz.", ss->rate, speed);
221
222 /* If the sample rate deviates too much, we need to resample */
223 if (speed < ss->rate*.95 || speed > ss->rate*1.05)
224 ss->rate = speed;
225 }
226
227 return 0;
228 }
229
230 static int simple_log2(int v) {
231 int k = 0;
232
233 for (;;) {
234 v >>= 1;
235 if (!v) break;
236 k++;
237 }
238
239 return k;
240 }
241
242 int pa_oss_set_fragments(int fd, int nfrags, int frag_size) {
243 int arg;
244 arg = ((int) nfrags << 16) | simple_log2(frag_size);
245
246 if (ioctl(fd, SNDCTL_DSP_SETFRAGMENT, &arg) < 0) {
247 pa_log("SNDCTL_DSP_SETFRAGMENT: %s", pa_cstrerror(errno));
248 return -1;
249 }
250
251 return 0;
252 }
253
254 int pa_oss_get_volume(int fd, unsigned long mixer, const pa_sample_spec *ss, pa_cvolume *volume) {
255 char cv[PA_CVOLUME_SNPRINT_MAX];
256 unsigned vol;
257
258 pa_assert(fd >= 0);
259 pa_assert(ss);
260 pa_assert(volume);
261
262 if (ioctl(fd, mixer, &vol) < 0)
263 return -1;
264
265 pa_cvolume_reset(volume, ss->channels);
266
267 volume->values[0] = ((vol & 0xFF) * PA_VOLUME_NORM) / 100;
268
269 if (volume->channels >= 2)
270 volume->values[1] = (((vol >> 8) & 0xFF) * PA_VOLUME_NORM) / 100;
271
272 pa_log_debug("Read mixer settings: %s", pa_cvolume_snprint(cv, sizeof(cv), volume));
273 return 0;
274 }
275
276 int pa_oss_set_volume(int fd, unsigned long mixer, const pa_sample_spec *ss, const pa_cvolume *volume) {
277 char cv[PA_CVOLUME_SNPRINT_MAX];
278 unsigned vol;
279 pa_volume_t l, r;
280
281 l = volume->values[0] > PA_VOLUME_NORM ? PA_VOLUME_NORM : volume->values[0];
282
283 vol = (l*100)/PA_VOLUME_NORM;
284
285 if (ss->channels >= 2) {
286 r = volume->values[1] > PA_VOLUME_NORM ? PA_VOLUME_NORM : volume->values[1];
287 vol |= ((r*100)/PA_VOLUME_NORM) << 8;
288 }
289
290 if (ioctl(fd, mixer, &vol) < 0)
291 return -1;
292
293 pa_log_debug("Wrote mixer settings: %s", pa_cvolume_snprint(cv, sizeof(cv), volume));
294 return 0;
295 }
296
297 static int get_device_number(const char *dev) {
298 const char *p, *e;
299 char *rp = NULL;
300 int r;
301
302 if (!(p = rp = pa_readlink(dev))) {
303 if (errno != EINVAL && errno != ENOLINK) {
304 r = -1;
305 goto finish;
306 }
307
308 p = dev;
309 }
310
311 if ((e = strrchr(p, '/')))
312 p = e+1;
313
314 if (p == 0) {
315 r = 0;
316 goto finish;
317 }
318
319 p = strchr(p, 0) -1;
320
321 if (*p >= '0' && *p <= '9') {
322 r = *p - '0';
323 goto finish;
324 }
325
326 r = -1;
327
328 finish:
329 pa_xfree(rp);
330 return r;
331 }
332
333 int pa_oss_get_hw_description(const char *dev, char *name, size_t l) {
334 FILE *f;
335 int n, r = -1;
336 int b = 0;
337
338 if ((n = get_device_number(dev)) < 0)
339 return -1;
340
341 if (!(f = fopen("/dev/sndstat", "r")) &&
342 !(f = fopen("/proc/sndstat", "r")) &&
343 !(f = fopen("/proc/asound/oss/sndstat", "r"))) {
344
345 if (errno != ENOENT)
346 pa_log_warn("failed to open OSS sndstat device: %s", pa_cstrerror(errno));
347
348 return -1;
349 }
350
351 while (!feof(f)) {
352 char line[64];
353 int device;
354
355 if (!fgets(line, sizeof(line), f))
356 break;
357
358 line[strcspn(line, "\r\n")] = 0;
359
360 if (!b) {
361 b = strcmp(line, "Audio devices:") == 0;
362 continue;
363 }
364
365 if (line[0] == 0)
366 break;
367
368 if (sscanf(line, "%i: ", &device) != 1)
369 continue;
370
371 if (device == n) {
372 char *k = strchr(line, ':');
373 pa_assert(k);
374 k++;
375 k += strspn(k, " ");
376
377 if (pa_endswith(k, " (DUPLEX)"))
378 k[strlen(k)-9] = 0;
379
380 pa_strlcpy(name, k, l);
381 r = 0;
382 break;
383 }
384 }
385
386 fclose(f);
387 return r;
388 }
389
390 static int open_mixer(const char *mixer) {
391 int fd;
392
393 if ((fd = open(mixer, O_RDWR|O_NDELAY|O_NOCTTY)) >= 0)
394 return fd;
395
396 return -1;
397 }
398
399 int pa_oss_open_mixer_for_device(const char *device) {
400 int n;
401 char *fn;
402 int fd;
403
404 if ((n = get_device_number(device)) < 0)
405 return -1;
406
407 if (n == 0)
408 if ((fd = open_mixer("/dev/mixer")) >= 0)
409 return fd;
410
411 fn = pa_sprintf_malloc("/dev/mixer%i", n);
412 fd = open_mixer(fn);
413 pa_xfree(fn);
414
415 if (fd < 0)
416 pa_log_warn("Failed to open mixer '%s': %s", device, pa_cstrerror(errno));
417
418 return fd;
419 }