summaryrefslogtreecommitdiff
path: root/src/modules/oss/oss-util.c
blob: f8e11fd270ac70f8f24326cdf30c3b45527c09f6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
/***
  This file is part of PulseAudio.

  Copyright 2004-2006 Lennart Poettering
  Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB

  PulseAudio is free software; you can redistribute it and/or modify
  it under the terms of the GNU Lesser General Public License as published
  by the Free Software Foundation; either version 2 of the License,
  or (at your option) any later version.

  PulseAudio is distributed in the hope that it will be useful, but
  WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  General Public License for more details.

  You should have received a copy of the GNU Lesser General Public License
  along with PulseAudio; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  USA.
***/

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <sys/soundcard.h>
#include <sys/ioctl.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#include <pulse/xmalloc.h>
#include <pulsecore/core-error.h>
#include <pulsecore/core-util.h>
#include <pulsecore/log.h>
#include <pulsecore/macro.h>

#include "oss-util.h"

int pa_oss_open(const char *device, int *mode, int* pcaps) {
    int fd = -1;
    int caps;

    pa_assert(device);
    pa_assert(mode);
    pa_assert(*mode == O_RDWR || *mode == O_RDONLY || *mode == O_WRONLY);

    if(!pcaps)
        pcaps = &caps;

    if (*mode == O_RDWR) {
        if ((fd = open(device, O_RDWR|O_NDELAY|O_NOCTTY)) >= 0) {
            ioctl(fd, SNDCTL_DSP_SETDUPLEX, 0);

            if (ioctl(fd, SNDCTL_DSP_GETCAPS, pcaps) < 0) {
                pa_log("SNDCTL_DSP_GETCAPS: %s", pa_cstrerror(errno));
                goto fail;
            }

            if (*pcaps & DSP_CAP_DUPLEX)
                goto success;

            pa_log_warn("'%s' doesn't support full duplex", device);

            pa_close(fd);
        }

        if ((fd = open(device, (*mode = O_WRONLY)|O_NDELAY|O_NOCTTY)) < 0) {
            if ((fd = open(device, (*mode = O_RDONLY)|O_NDELAY|O_NOCTTY)) < 0) {
                pa_log("open('%s'): %s", device, pa_cstrerror(errno));
                goto fail;
            }
        }
    } else {
        if ((fd = open(device, *mode|O_NDELAY|O_NOCTTY)) < 0) {
            pa_log("open('%s'): %s", device, pa_cstrerror(errno));
            goto fail;
        }
    }

    *pcaps = 0;

    if (ioctl(fd, SNDCTL_DSP_GETCAPS, pcaps) < 0) {
        pa_log("SNDCTL_DSP_GETCAPS: %s", pa_cstrerror(errno));
        goto fail;
    }

success:

    pa_log_debug("capabilities:%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
                 *pcaps & DSP_CAP_BATCH ? " BATCH" : "",
#ifdef DSP_CAP_BIND
                 *pcaps & DSP_CAP_BIND ? " BIND" : "",
#else
                 "",
#endif
                 *pcaps & DSP_CAP_COPROC ? " COPROC" : "",
                 *pcaps & DSP_CAP_DUPLEX ? " DUPLEX" : "",
#ifdef DSP_CAP_FREERATE
                 *pcaps & DSP_CAP_FREERATE ? " FREERATE" : "",
#else
                 "",
#endif
#ifdef DSP_CAP_INPUT
                 *pcaps & DSP_CAP_INPUT ? " INPUT" : "",
#else
                 "",
#endif
                 *pcaps & DSP_CAP_MMAP ? " MMAP" : "",
#ifdef DSP_CAP_MODEM
                 *pcaps & DSP_CAP_MODEM ? " MODEM" : "",
#else
                 "",
#endif
#ifdef DSP_CAP_MULTI
                 *pcaps & DSP_CAP_MULTI ? " MULTI" : "",
#else
                 "",
#endif
#ifdef DSP_CAP_OUTPUT
                 *pcaps & DSP_CAP_OUTPUT ? " OUTPUT" : "",
#else
                 "",
#endif
                 *pcaps & DSP_CAP_REALTIME ? " REALTIME" : "",
#ifdef DSP_CAP_SHADOW
                 *pcaps & DSP_CAP_SHADOW ? " SHADOW" : "",
#else
                 "",
#endif
#ifdef DSP_CAP_VIRTUAL
                 *pcaps & DSP_CAP_VIRTUAL ? " VIRTUAL" : "",
#else
                 "",
#endif
                 *pcaps & DSP_CAP_TRIGGER ? " TRIGGER" : "");

    pa_make_fd_cloexec(fd);

    return fd;

fail:
    if (fd >= 0)
        pa_close(fd);
    return -1;
}

int pa_oss_auto_format(int fd, pa_sample_spec *ss) {
    int format, channels, speed, reqformat;
    pa_sample_format_t orig_format;

    static const int format_trans[PA_SAMPLE_MAX] = {
        [PA_SAMPLE_U8] = AFMT_U8,
        [PA_SAMPLE_ALAW] = AFMT_A_LAW,
        [PA_SAMPLE_ULAW] = AFMT_MU_LAW,
        [PA_SAMPLE_S16LE] = AFMT_S16_LE,
        [PA_SAMPLE_S16BE] = AFMT_S16_BE,
        [PA_SAMPLE_FLOAT32LE] = AFMT_QUERY, /* not supported */
        [PA_SAMPLE_FLOAT32BE] = AFMT_QUERY, /* not supported */
        [PA_SAMPLE_S32LE] = AFMT_QUERY, /* not supported */
        [PA_SAMPLE_S32BE] = AFMT_QUERY, /* not supported */
        [PA_SAMPLE_S24LE] = AFMT_QUERY, /* not supported */
        [PA_SAMPLE_S24BE] = AFMT_QUERY, /* not supported */
        [PA_SAMPLE_S24_32LE] = AFMT_QUERY, /* not supported */
        [PA_SAMPLE_S24_32BE] = AFMT_QUERY, /* not supported */
    };

    pa_assert(fd >= 0);
    pa_assert(ss);

    orig_format = ss->format;

    reqformat = format = format_trans[ss->format];
    if (reqformat == AFMT_QUERY || ioctl(fd, SNDCTL_DSP_SETFMT, &format) < 0 || format != reqformat) {
        format = AFMT_S16_NE;
        if (ioctl(fd, SNDCTL_DSP_SETFMT, &format) < 0 || format != AFMT_S16_NE) {
            int f = AFMT_S16_NE == AFMT_S16_LE ? AFMT_S16_BE : AFMT_S16_LE;
            format = f;
            if (ioctl(fd, SNDCTL_DSP_SETFMT, &format) < 0 || format != f) {
                format = AFMT_U8;
                if (ioctl(fd, SNDCTL_DSP_SETFMT, &format) < 0 || format != AFMT_U8) {
                    pa_log("SNDCTL_DSP_SETFMT: %s", format != AFMT_U8 ? "No supported sample format" : pa_cstrerror(errno));
                    return -1;
                } else
                    ss->format = PA_SAMPLE_U8;
            } else
                ss->format = f == AFMT_S16_LE ? PA_SAMPLE_S16LE : PA_SAMPLE_S16BE;
        } else
            ss->format = PA_SAMPLE_S16NE;
    }

    if (orig_format != ss->format)
        pa_log_warn("device doesn't support sample format %s, changed to %s.",
               pa_sample_format_to_string(orig_format),
               pa_sample_format_to_string(ss->format));

    channels = ss->channels;
    if (ioctl(fd, SNDCTL_DSP_CHANNELS, &channels) < 0) {
        pa_log("SNDCTL_DSP_CHANNELS: %s", pa_cstrerror(errno));
        return -1;
    }
    pa_assert(channels > 0);

    if (ss->channels != channels) {
        pa_log_warn("device doesn't support %i channels, using %i channels.", ss->channels, channels);
        ss->channels = (uint8_t) channels;
    }

    speed = (int) ss->rate;
    if (ioctl(fd, SNDCTL_DSP_SPEED, &speed) < 0) {
        pa_log("SNDCTL_DSP_SPEED: %s", pa_cstrerror(errno));
        return -1;
    }
    pa_assert(speed > 0);

    if (ss->rate != (unsigned) speed) {
        pa_log_warn("device doesn't support %i Hz, changed to %i Hz.", ss->rate, speed);

        /* If the sample rate deviates too much, we need to resample */
        if (speed < ss->rate*.95 || speed > ss->rate*1.05)
            ss->rate = (uint32_t) speed;
    }

    return 0;
}

static int simple_log2(int v) {
    int k = 0;

    for (;;) {
        v >>= 1;
        if (!v) break;
        k++;
    }

    return k;
}

int pa_oss_set_fragments(int fd, int nfrags, int frag_size) {
    int arg;
    arg = ((int) nfrags << 16) | simple_log2(frag_size);

    if (ioctl(fd, SNDCTL_DSP_SETFRAGMENT, &arg) < 0) {
        pa_log("SNDCTL_DSP_SETFRAGMENT: %s", pa_cstrerror(errno));
        return -1;
    }

    return 0;
}

int pa_oss_get_volume(int fd, unsigned long mixer, const pa_sample_spec *ss, pa_cvolume *volume) {
    char cv[PA_CVOLUME_SNPRINT_MAX];
    unsigned vol;

    pa_assert(fd >= 0);
    pa_assert(ss);
    pa_assert(volume);

    if (ioctl(fd, mixer, &vol) < 0)
        return -1;

    pa_cvolume_reset(volume, ss->channels);

    volume->values[0] = ((vol & 0xFF) * PA_VOLUME_NORM) / 100;

    if (volume->channels >= 2)
        volume->values[1] = (((vol >> 8) & 0xFF) * PA_VOLUME_NORM) / 100;

    pa_log_debug("Read mixer settings: %s", pa_cvolume_snprint(cv, sizeof(cv), volume));
    return 0;
}

int pa_oss_set_volume(int fd, unsigned long mixer, const pa_sample_spec *ss, const pa_cvolume *volume) {
    char cv[PA_CVOLUME_SNPRINT_MAX];
    unsigned vol;
    pa_volume_t l, r;

    l = volume->values[0] > PA_VOLUME_NORM ? PA_VOLUME_NORM : volume->values[0];

    vol = (l*100)/PA_VOLUME_NORM;

    if (ss->channels >= 2) {
        r = volume->values[1] > PA_VOLUME_NORM ? PA_VOLUME_NORM : volume->values[1];
        vol |= ((r*100)/PA_VOLUME_NORM) << 8;
    }

    if (ioctl(fd, mixer, &vol) < 0)
        return -1;

    pa_log_debug("Wrote mixer settings: %s", pa_cvolume_snprint(cv, sizeof(cv), volume));
    return 0;
}

static int get_device_number(const char *dev) {
    const char *p, *e;
    char *rp = NULL;
    int r;

    if (!(p = rp = pa_readlink(dev))) {
        if (errno != EINVAL && errno != ENOLINK) {
            r = -1;
            goto finish;
        }

        p = dev;
    }

    if ((e = strrchr(p, '/')))
        p = e+1;

    if (p == 0) {
        r = 0;
        goto finish;
    }

    p = strchr(p, 0) -1;

    if (*p >= '0' && *p <= '9') {
        r = *p - '0';
        goto finish;
    }

    r = -1;

finish:
    pa_xfree(rp);
    return r;
}

int pa_oss_get_hw_description(const char *dev, char *name, size_t l) {
    FILE *f;
    int n, r = -1;
    int b = 0;

    if ((n = get_device_number(dev)) < 0)
        return -1;

    if (!(f = fopen("/dev/sndstat", "r")) &&
        !(f = fopen("/proc/sndstat", "r")) &&
        !(f = fopen("/proc/asound/oss/sndstat", "r"))) {

        if (errno != ENOENT)
            pa_log_warn("failed to open OSS sndstat device: %s", pa_cstrerror(errno));

        return -1;
    }

    while (!feof(f)) {
        char line[64];
        int device;

        if (!fgets(line, sizeof(line), f))
            break;

        line[strcspn(line, "\r\n")] = 0;

        if (!b) {
            b = strcmp(line, "Audio devices:") == 0;
            continue;
        }

        if (line[0] == 0)
            break;

        if (sscanf(line, "%i: ", &device) != 1)
            continue;

        if (device == n) {
            char *k = strchr(line, ':');
            pa_assert(k);
            k++;
            k += strspn(k, " ");

            if (pa_endswith(k, " (DUPLEX)"))
                k[strlen(k)-9] = 0;

            pa_strlcpy(name, k, l);
            r = 0;
            break;
        }
    }

    fclose(f);
    return r;
}

static int open_mixer(const char *mixer) {
    int fd;

    if ((fd = open(mixer, O_RDWR|O_NDELAY|O_NOCTTY)) >= 0)
        return fd;

    return -1;
}

int pa_oss_open_mixer_for_device(const char *device) {
    int n;
    char *fn;
    int fd;

    if ((n = get_device_number(device)) < 0)
        return -1;

    if (n == 0)
        if ((fd = open_mixer("/dev/mixer")) >= 0)
            return fd;

    fn = pa_sprintf_malloc("/dev/mixer%i", n);
    fd = open_mixer(fn);
    pa_xfree(fn);

    if (fd < 0)
        pa_log_warn("Failed to open mixer '%s': %s", device, pa_cstrerror(errno));

    return fd;
}