Add copyright notices to all relevant files. (based on svn log)
[profile/ivi/pulseaudio.git] / 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 <assert.h>
30 #include <sys/soundcard.h>
31 #include <sys/ioctl.h>
32 #include <stdio.h>
33 #include <errno.h>
34 #include <string.h>
35 #include <unistd.h>
36 #include <sys/types.h>
37 #include <sys/stat.h>
38 #include <fcntl.h>
39
40 #include <pulsecore/core-error.h>
41 #include <pulsecore/core-util.h>
42 #include <pulsecore/log.h>
43
44 #include "oss-util.h"
45
46 int pa_oss_open(const char *device, int *mode, int* pcaps) {
47     int fd = -1;
48     int caps;
49
50     assert(device && mode && (*mode == O_RDWR || *mode == O_RDONLY || *mode == O_WRONLY));
51
52     if(!pcaps)
53         pcaps = &caps;
54
55     if (*mode == O_RDWR) {
56         if ((fd = open(device, O_RDWR|O_NDELAY)) >= 0) {
57             int dcaps, *tcaps;
58             ioctl(fd, SNDCTL_DSP_SETDUPLEX, 0);
59
60             tcaps = pcaps ? pcaps : &dcaps;
61
62             if (ioctl(fd, SNDCTL_DSP_GETCAPS, tcaps) < 0) {
63                 pa_log("SNDCTL_DSP_GETCAPS: %s", pa_cstrerror(errno));
64                 goto fail;
65             }
66
67             if (*tcaps & DSP_CAP_DUPLEX)
68                 goto success;
69
70             pa_log_warn("'%s' doesn't support full duplex", device);
71
72             close(fd);
73         }
74
75         if ((fd = open(device, (*mode = O_WRONLY)|O_NDELAY)) < 0) {
76             if ((fd = open(device, (*mode = O_RDONLY)|O_NDELAY)) < 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)) < 0) {
83             pa_log("open('%s'): %s", device, pa_cstrerror(errno));
84             goto fail;
85         }
86     }
87
88 success:
89
90     *pcaps = 0;
91
92     if (ioctl(fd, SNDCTL_DSP_GETCAPS, pcaps) < 0) {
93         pa_log("SNDCTL_DSP_GETCAPS: %s", pa_cstrerror(errno));
94         goto fail;
95     }
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_fd_set_cloexec(fd, 1);
146
147     return fd;
148
149 fail:
150     if (fd >= 0)
151         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     assert(fd >= 0 && ss);
170
171     orig_format = ss->format;
172
173     reqformat = format = format_trans[ss->format];
174     if (reqformat == AFMT_QUERY || ioctl(fd, SNDCTL_DSP_SETFMT, &format) < 0 || format != reqformat) {
175         format = AFMT_S16_NE;
176         if (ioctl(fd, SNDCTL_DSP_SETFMT, &format) < 0 || format != AFMT_S16_NE) {
177             int f = AFMT_S16_NE == AFMT_S16_LE ? AFMT_S16_BE : AFMT_S16_LE;
178             format = f;
179             if (ioctl(fd, SNDCTL_DSP_SETFMT, &format) < 0 || format != f) {
180                 format = AFMT_U8;
181                 if (ioctl(fd, SNDCTL_DSP_SETFMT, &format) < 0 || format != AFMT_U8) {
182                     pa_log("SNDCTL_DSP_SETFMT: %s", format != AFMT_U8 ? "No supported sample format" : pa_cstrerror(errno));
183                     return -1;
184                 } else
185                     ss->format = PA_SAMPLE_U8;
186             } else
187                 ss->format = f == AFMT_S16_LE ? PA_SAMPLE_S16LE : PA_SAMPLE_S16BE;
188         } else
189             ss->format = PA_SAMPLE_S16NE;
190     }
191
192     if (orig_format != ss->format)
193         pa_log_warn("device doesn't support sample format %s, changed to %s.",
194                pa_sample_format_to_string(orig_format),
195                pa_sample_format_to_string(ss->format));
196
197     channels = ss->channels;
198     if (ioctl(fd, SNDCTL_DSP_CHANNELS, &channels) < 0) {
199         pa_log("SNDCTL_DSP_CHANNELS: %s", pa_cstrerror(errno));
200         return -1;
201     }
202     assert(channels > 0);
203
204     if (ss->channels != channels) {
205         pa_log_warn("device doesn't support %i channels, using %i channels.", ss->channels, channels);
206         ss->channels = channels;
207     }
208
209     speed = ss->rate;
210     if (ioctl(fd, SNDCTL_DSP_SPEED, &speed) < 0) {
211         pa_log("SNDCTL_DSP_SPEED: %s", pa_cstrerror(errno));
212         return -1;
213     }
214     assert(speed > 0);
215
216     if (ss->rate != (unsigned) speed) {
217         pa_log_warn("device doesn't support %i Hz, changed to %i Hz.", ss->rate, speed);
218
219         /* If the sample rate deviates too much, we need to resample */
220         if (speed < ss->rate*.95 || speed > ss->rate*1.05)
221             ss->rate = speed;
222     }
223
224     return 0;
225 }
226
227 static int simple_log2(int v) {
228     int k = 0;
229
230     for (;;) {
231         v >>= 1;
232         if (!v) break;
233         k++;
234     }
235
236     return k;
237 }
238
239 int pa_oss_set_fragments(int fd, int nfrags, int frag_size) {
240     int arg;
241     arg = ((int) nfrags << 16) | simple_log2(frag_size);
242
243     if (ioctl(fd, SNDCTL_DSP_SETFRAGMENT, &arg) < 0) {
244         pa_log("SNDCTL_DSP_SETFRAGMENT: %s", pa_cstrerror(errno));
245         return -1;
246     }
247
248     return 0;
249 }
250
251 static int pa_oss_get_volume(int fd, int mixer, const pa_sample_spec *ss, pa_cvolume *volume) {
252     char cv[PA_CVOLUME_SNPRINT_MAX];
253     unsigned vol;
254
255     assert(fd >= 0);
256     assert(ss);
257     assert(volume);
258
259     if (ioctl(fd, mixer, &vol) < 0)
260         return -1;
261
262     volume->values[0] = ((vol & 0xFF) * PA_VOLUME_NORM) / 100;
263
264     if ((volume->channels = ss->channels) >= 2)
265         volume->values[1] = (((vol >> 8) & 0xFF) * PA_VOLUME_NORM) / 100;
266
267     pa_log_debug("Read mixer settings: %s", pa_cvolume_snprint(cv, sizeof(cv), volume));
268     return 0;
269 }
270
271 static int pa_oss_set_volume(int fd, int mixer, const pa_sample_spec *ss, const pa_cvolume *volume) {
272     char cv[PA_CVOLUME_SNPRINT_MAX];
273     unsigned vol;
274     pa_volume_t l, r;
275
276     l = volume->values[0] > PA_VOLUME_NORM ? PA_VOLUME_NORM : volume->values[0];
277
278     vol = (l*100)/PA_VOLUME_NORM;
279
280     if (ss->channels >= 2) {
281         r = volume->values[1] > PA_VOLUME_NORM ? PA_VOLUME_NORM : volume->values[1];
282         vol |= ((r*100)/PA_VOLUME_NORM) << 8;
283     }
284
285     if (ioctl(fd, mixer, &vol) < 0)
286         return -1;
287
288     pa_log_debug("Wrote mixer settings: %s", pa_cvolume_snprint(cv, sizeof(cv), volume));
289     return 0;
290 }
291
292 int pa_oss_get_pcm_volume(int fd, const pa_sample_spec *ss, pa_cvolume *volume) {
293     return pa_oss_get_volume(fd, SOUND_MIXER_READ_PCM, ss, volume);
294 }
295
296 int pa_oss_set_pcm_volume(int fd, const pa_sample_spec *ss, const pa_cvolume *volume) {
297     return pa_oss_set_volume(fd, SOUND_MIXER_WRITE_PCM, ss, volume);
298 }
299
300 int pa_oss_get_input_volume(int fd, const pa_sample_spec *ss, pa_cvolume *volume) {
301     return pa_oss_get_volume(fd, SOUND_MIXER_READ_IGAIN, ss, volume);
302 }
303
304 int pa_oss_set_input_volume(int fd, const pa_sample_spec *ss, const pa_cvolume *volume) {
305     return pa_oss_set_volume(fd, SOUND_MIXER_WRITE_IGAIN, ss, volume);
306 }
307
308 int pa_oss_get_hw_description(const char *dev, char *name, size_t l) {
309     FILE *f;
310     const char *e = NULL;
311     int n, r = -1;
312     int b = 0;
313
314     if (strncmp(dev, "/dev/dsp", 8) == 0)
315         e = dev+8;
316     else if (strncmp(dev, "/dev/adsp", 9) == 0)
317         e = dev+9;
318     else
319         return -1;
320
321     if (*e == 0)
322         n = 0;
323     else if (*e >= '0' && *e <= '9' && *(e+1) == 0)
324         n = *e - '0';
325     else
326         return -1;
327
328     if (!(f = fopen("/dev/sndstat", "r")) &&
329         !(f = fopen("/proc/sndstat", "r")) &&
330         !(f = fopen("/proc/asound/oss/sndstat", "r"))) {
331
332         if (errno != ENOENT)
333             pa_log_warn("failed to open OSS sndstat device: %s", pa_cstrerror(errno));
334
335         return -1;
336     }
337
338     while (!feof(f)) {
339         char line[64];
340         int device;
341
342         if (!fgets(line, sizeof(line), f))
343             break;
344
345         line[strcspn(line, "\r\n")] = 0;
346
347         if (!b) {
348             b = strcmp(line, "Audio devices:") == 0;
349             continue;
350         }
351
352         if (line[0] == 0)
353             break;
354
355         if (sscanf(line, "%i: ", &device) != 1)
356             continue;
357
358         if (device == n) {
359             char *k = strchr(line, ':');
360             assert(k);
361             k++;
362             k += strspn(k, " ");
363
364             if (pa_endswith(k, " (DUPLEX)"))
365                 k[strlen(k)-9] = 0;
366
367             pa_strlcpy(name, k, l);
368             r = 0;
369             break;
370         }
371     }
372
373     fclose(f);
374     return r;
375 }