]> code.delx.au - pulseaudio/blob - src/oss-util.c
esound protocol
[pulseaudio] / src / oss-util.c
1 #include <assert.h>
2 #include <sys/soundcard.h>
3 #include <sys/ioctl.h>
4 #include <stdio.h>
5 #include <errno.h>
6 #include <string.h>
7
8 #include "oss-util.h"
9
10 int oss_auto_format(int fd, struct pa_sample_spec *ss) {
11 int format, channels, speed;
12
13 assert(fd >= 0 && ss);
14
15 format = AFMT_S16_NE;
16 if (ioctl(fd, SNDCTL_DSP_SETFMT, &format) < 0 || format != AFMT_S16_NE) {
17 int f = AFMT_S16_NE == AFMT_S16_LE ? AFMT_S16_BE : AFMT_S16_LE;
18 format = f;
19 if (ioctl(fd, SNDCTL_DSP_SETFMT, &format) < 0 || format != f) {
20 format = AFMT_U8;
21 if (ioctl(fd, SNDCTL_DSP_SETFMT, &format) < 0 || format != AFMT_U8) {
22 fprintf(stderr, "SNDCTL_DSP_SETFMT: %s\n", format != AFMT_U8 ? "No supported sample format" : strerror(errno));
23 return -1;
24 } else
25 ss->format = PA_SAMPLE_U8;
26 } else
27 ss->format = f == AFMT_S16_LE ? PA_SAMPLE_S16LE : PA_SAMPLE_S16BE;
28 } else
29 ss->format = PA_SAMPLE_S16NE;
30
31 channels = 2;
32 if (ioctl(fd, SNDCTL_DSP_CHANNELS, &channels) < 0) {
33 fprintf(stderr, "SNDCTL_DSP_CHANNELS: %s\n", strerror(errno));
34 return -1;
35 }
36 assert(channels);
37 ss->channels = channels;
38
39 speed = 44100;
40 if (ioctl(fd, SNDCTL_DSP_SPEED, &speed) < 0) {
41 fprintf(stderr, "SNDCTL_DSP_SPEED: %s\n", strerror(errno));
42 return -1;
43 }
44 assert(speed);
45 ss->rate = speed;
46
47 return 0;
48 }