]> code.delx.au - pulseaudio/blob - src/modules/oss-util.c
fac39e7bc6df624ff9c8870c2eeebfcab7314595
[pulseaudio] / src / modules / oss-util.c
1 /* $Id$ */
2
3 /***
4 This file is part of polypaudio.
5
6 polypaudio 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 polypaudio 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 polypaudio; 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 <polypcore/util.h>
38 #include <polypcore/log.h>
39
40 #include "oss-util.h"
41
42 int pa_oss_open(const char *device, int *mode, int* pcaps) {
43 int fd = -1;
44 assert(device && mode && (*mode == O_RDWR || *mode == O_RDONLY || *mode == O_WRONLY));
45
46 if (*mode == O_RDWR) {
47 if ((fd = open(device, O_RDWR|O_NDELAY)) >= 0) {
48 int dcaps, *tcaps;
49 ioctl(fd, SNDCTL_DSP_SETDUPLEX, 0);
50
51 tcaps = pcaps ? pcaps : &dcaps;
52
53 if (ioctl(fd, SNDCTL_DSP_GETCAPS, tcaps) < 0) {
54 pa_log(__FILE__": SNDCTL_DSP_GETCAPS: %s", strerror(errno));
55 goto fail;
56 }
57
58 if (*tcaps & DSP_CAP_DUPLEX)
59 return fd;
60
61 goto fail;
62 }
63
64 if ((fd = open(device, (*mode = O_WRONLY)|O_NDELAY)) < 0) {
65 if ((fd = open(device, (*mode = O_RDONLY)|O_NDELAY)) < 0) {
66 pa_log(__FILE__": open('%s'): %s", device, strerror(errno));
67 goto fail;
68 }
69 }
70 } else {
71 if ((fd = open(device, *mode|O_NDELAY)) < 0) {
72 pa_log(__FILE__": open('%s'): %s", device, strerror(errno));
73 goto fail;
74 }
75 }
76
77 if (pcaps) {
78 if (ioctl(fd, SNDCTL_DSP_GETCAPS, pcaps) < 0) {
79 pa_log(__FILE__": SNDCTL_DSP_GETCAPS: %s", strerror(errno));
80 goto fail;
81 }
82 }
83
84 pa_fd_set_cloexec(fd, 1);
85
86 return fd;
87
88 fail:
89 if (fd >= 0)
90 close(fd);
91 return -1;
92 }
93
94 int pa_oss_auto_format(int fd, pa_sample_spec *ss) {
95 int format, channels, speed, reqformat;
96 static const int format_trans[PA_SAMPLE_MAX] = {
97 [PA_SAMPLE_U8] = AFMT_U8,
98 [PA_SAMPLE_ALAW] = AFMT_A_LAW,
99 [PA_SAMPLE_ULAW] = AFMT_MU_LAW,
100 [PA_SAMPLE_S16LE] = AFMT_S16_LE,
101 [PA_SAMPLE_S16BE] = AFMT_S16_BE,
102 [PA_SAMPLE_FLOAT32LE] = AFMT_QUERY, /* not supported */
103 [PA_SAMPLE_FLOAT32BE] = AFMT_QUERY, /* not supported */
104 };
105
106 assert(fd >= 0 && ss);
107
108 reqformat = format = format_trans[ss->format];
109 if (reqformat == AFMT_QUERY || ioctl(fd, SNDCTL_DSP_SETFMT, &format) < 0 || format != reqformat) {
110 format = AFMT_S16_NE;
111 if (ioctl(fd, SNDCTL_DSP_SETFMT, &format) < 0 || format != AFMT_S16_NE) {
112 int f = AFMT_S16_NE == AFMT_S16_LE ? AFMT_S16_BE : AFMT_S16_LE;
113 format = f;
114 if (ioctl(fd, SNDCTL_DSP_SETFMT, &format) < 0 || format != f) {
115 format = AFMT_U8;
116 if (ioctl(fd, SNDCTL_DSP_SETFMT, &format) < 0 || format != AFMT_U8) {
117 pa_log(__FILE__": SNDCTL_DSP_SETFMT: %s", format != AFMT_U8 ? "No supported sample format" : strerror(errno));
118 return -1;
119 } else
120 ss->format = PA_SAMPLE_U8;
121 } else
122 ss->format = f == AFMT_S16_LE ? PA_SAMPLE_S16LE : PA_SAMPLE_S16BE;
123 } else
124 ss->format = PA_SAMPLE_S16NE;
125 }
126
127 channels = ss->channels;
128 if (ioctl(fd, SNDCTL_DSP_CHANNELS, &channels) < 0) {
129 pa_log(__FILE__": SNDCTL_DSP_CHANNELS: %s", strerror(errno));
130 return -1;
131 }
132 assert(channels);
133 ss->channels = channels;
134
135 speed = ss->rate;
136 if (ioctl(fd, SNDCTL_DSP_SPEED, &speed) < 0) {
137 pa_log(__FILE__": SNDCTL_DSP_SPEED: %s", strerror(errno));
138 return -1;
139 }
140 assert(speed);
141 ss->rate = speed;
142
143 return 0;
144 }
145
146 static int simple_log2(int v) {
147 int k = 0;
148
149 for (;;) {
150 v >>= 1;
151 if (!v) break;
152 k++;
153 }
154
155 return k;
156 }
157
158 int pa_oss_set_fragments(int fd, int nfrags, int frag_size) {
159 int arg;
160 arg = ((int) nfrags << 16) | simple_log2(frag_size);
161
162 if (ioctl(fd, SNDCTL_DSP_SETFRAGMENT, &arg) < 0) {
163 pa_log(__FILE__": SNDCTL_DSP_SETFRAGMENT: %s", strerror(errno));
164 return -1;
165 }
166
167 return 0;
168 }
169
170 static int pa_oss_get_volume(int fd, int mixer, const pa_sample_spec *ss, pa_cvolume *volume) {
171 char cv[PA_CVOLUME_SNPRINT_MAX];
172 unsigned vol;
173
174 assert(fd >= 0);
175 assert(ss);
176 assert(volume);
177
178 if (ioctl(fd, mixer, &vol) < 0)
179 return -1;
180
181 volume->values[0] = ((vol & 0xFF) * PA_VOLUME_NORM) / 100;
182
183 if ((volume->channels = ss->channels) >= 2)
184 volume->values[1] = (((vol >> 8) & 0xFF) * PA_VOLUME_NORM) / 100;
185
186 pa_log_debug(__FILE__": Read mixer settings: %s", pa_cvolume_snprint(cv, sizeof(cv), volume));
187 return 0;
188 }
189
190 static int pa_oss_set_volume(int fd, int mixer, const pa_sample_spec *ss, const pa_cvolume *volume) {
191 char cv[PA_CVOLUME_SNPRINT_MAX];
192 unsigned vol;
193 pa_volume_t l, r;
194
195 l = volume->values[0] > PA_VOLUME_NORM ? PA_VOLUME_NORM : volume->values[0];
196
197 vol = (l*100)/PA_VOLUME_NORM;
198
199 if (ss->channels >= 2) {
200 r = volume->values[1] > PA_VOLUME_NORM ? PA_VOLUME_NORM : volume->values[1];
201 vol |= ((r*100)/PA_VOLUME_NORM) << 8;
202 }
203
204 if (ioctl(fd, mixer, &vol) < 0)
205 return -1;
206
207 pa_log_debug(__FILE__": Wrote mixer settings: %s", pa_cvolume_snprint(cv, sizeof(cv), volume));
208 return 0;
209 }
210
211 int pa_oss_get_pcm_volume(int fd, const pa_sample_spec *ss, pa_cvolume *volume) {
212 return pa_oss_get_volume(fd, SOUND_MIXER_READ_PCM, ss, volume);
213 }
214
215 int pa_oss_set_pcm_volume(int fd, const pa_sample_spec *ss, const pa_cvolume *volume) {
216 return pa_oss_set_volume(fd, SOUND_MIXER_WRITE_PCM, ss, volume);
217 }
218
219 int pa_oss_get_input_volume(int fd, const pa_sample_spec *ss, pa_cvolume *volume) {
220 return pa_oss_get_volume(fd, SOUND_MIXER_READ_IGAIN, ss, volume);
221 }
222
223 int pa_oss_set_input_volume(int fd, const pa_sample_spec *ss, const pa_cvolume *volume) {
224 return pa_oss_set_volume(fd, SOUND_MIXER_WRITE_IGAIN, ss, volume);
225 }
226
227 int pa_oss_get_hw_description(const char *dev, char *name, size_t l) {
228 FILE *f;
229 const char *e = NULL;
230 int n, r = -1;
231 int b = 0;
232
233 if (strncmp(dev, "/dev/dsp", 8) == 0)
234 e = dev+8;
235 else if (strncmp(dev, "/dev/adsp", 9) == 0)
236 e = dev+9;
237 else
238 return -1;
239
240 if (*e == 0)
241 n = 0;
242 else if (*e >= '0' && *e <= '9' && *(e+1) == 0)
243 n = *e - '0';
244 else
245 return -1;
246
247 if (!(f = fopen("/dev/sndstat", "r")) &&
248 !(f = fopen("/proc/sndstat", "r")) &&
249 !(f = fopen("/proc/asound/oss/sndstat", "r"))) {
250
251 if (errno != ENOENT)
252 pa_log_warn(__FILE__": failed to open OSS sndstat device: %s", strerror(errno));
253
254 return -1;
255 }
256
257 while (!feof(f)) {
258 char line[64];
259 int device;
260
261 if (!fgets(line, sizeof(line), f))
262 break;
263
264 line[strcspn(line, "\r\n")] = 0;
265
266 if (!b) {
267 b = strcmp(line, "Audio devices:") == 0;
268 continue;
269 }
270
271 if (line[0] == 0)
272 break;
273
274 if (sscanf(line, "%i: ", &device) != 1)
275 continue;
276
277 if (device == n) {
278 char *k = strchr(line, ':');
279 assert(k);
280 k++;
281 k += strspn(k, " ");
282
283 if (pa_endswith(k, " (DUPLEX)"))
284 k[strlen(k)-9] = 0;
285
286 pa_strlcpy(name, k, l);
287 r = 0;
288 break;
289 }
290 }
291
292 fclose(f);
293 return r;
294 }