]> code.delx.au - pulseaudio/blob - polyp/oss-util.c
613cda47d594fa1f3323eb0cceffc81063194e51
[pulseaudio] / polyp / 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 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 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 "oss-util.h"
38 #include "util.h"
39 #include "log.h"
40
41 int pa_oss_open(const char *device, int *mode, int* pcaps) {
42 int fd = -1;
43 assert(device && mode && (*mode == O_RDWR || *mode == O_RDONLY || *mode == O_WRONLY));
44
45 if (*mode == O_RDWR) {
46 if ((fd = open(device, O_RDWR|O_NDELAY)) >= 0) {
47 int dcaps, *tcaps;
48 ioctl(fd, SNDCTL_DSP_SETDUPLEX, 0);
49
50 tcaps = pcaps ? pcaps : &dcaps;
51
52 if (ioctl(fd, SNDCTL_DSP_GETCAPS, tcaps) < 0) {
53 pa_log(__FILE__": SNDCTL_DSP_GETCAPS: %s\n", strerror(errno));
54 goto fail;
55 }
56
57 if (*tcaps & DSP_CAP_DUPLEX)
58 return fd;
59
60 close(fd);
61 }
62
63 if ((fd = open(device, (*mode = O_WRONLY)|O_NDELAY)) < 0) {
64 if ((fd = open(device, (*mode = O_RDONLY)|O_NDELAY)) < 0) {
65 pa_log(__FILE__": open('%s'): %s\n", device, strerror(errno));
66 goto fail;
67 }
68 }
69 } else {
70 if ((fd = open(device, *mode|O_NDELAY)) < 0) {
71 pa_log(__FILE__": open('%s'): %s\n", device, strerror(errno));
72 goto fail;
73 }
74 }
75
76 if (pcaps) {
77 if (ioctl(fd, SNDCTL_DSP_GETCAPS, pcaps) < 0) {
78 pa_log(__FILE__": SNDCTL_DSP_GETCAPS: %s\n", strerror(errno));
79 goto fail;
80 }
81 }
82
83 pa_fd_set_cloexec(fd, 1);
84
85 return fd;
86
87 fail:
88 if (fd >= 0)
89 close(fd);
90 return fd;
91 }
92
93 int pa_oss_auto_format(int fd, struct pa_sample_spec *ss) {
94 int format, channels, speed, reqformat;
95 static const int format_trans[] = {
96 [PA_SAMPLE_U8] = AFMT_U8,
97 [PA_SAMPLE_ALAW] = AFMT_A_LAW,
98 [PA_SAMPLE_ULAW] = AFMT_MU_LAW,
99 [PA_SAMPLE_S16LE] = AFMT_S16_LE,
100 [PA_SAMPLE_S16BE] = AFMT_S16_BE,
101 [PA_SAMPLE_FLOAT32LE] = AFMT_QUERY, /* not supported */
102 [PA_SAMPLE_FLOAT32BE] = AFMT_QUERY, /* not supported */
103 };
104
105 assert(fd >= 0 && ss);
106
107 reqformat = format = format_trans[ss->format];
108 if (reqformat == AFMT_QUERY || ioctl(fd, SNDCTL_DSP_SETFMT, &format) < 0 || format != reqformat) {
109 format = AFMT_S16_NE;
110 if (ioctl(fd, SNDCTL_DSP_SETFMT, &format) < 0 || format != AFMT_S16_NE) {
111 int f = AFMT_S16_NE == AFMT_S16_LE ? AFMT_S16_BE : AFMT_S16_LE;
112 format = f;
113 if (ioctl(fd, SNDCTL_DSP_SETFMT, &format) < 0 || format != f) {
114 format = AFMT_U8;
115 if (ioctl(fd, SNDCTL_DSP_SETFMT, &format) < 0 || format != AFMT_U8) {
116 pa_log(__FILE__": SNDCTL_DSP_SETFMT: %s\n", format != AFMT_U8 ? "No supported sample format" : strerror(errno));
117 return -1;
118 } else
119 ss->format = PA_SAMPLE_U8;
120 } else
121 ss->format = f == AFMT_S16_LE ? PA_SAMPLE_S16LE : PA_SAMPLE_S16BE;
122 } else
123 ss->format = PA_SAMPLE_S16NE;
124 }
125
126 channels = ss->channels;
127 if (ioctl(fd, SNDCTL_DSP_CHANNELS, &channels) < 0) {
128 pa_log(__FILE__": SNDCTL_DSP_CHANNELS: %s\n", strerror(errno));
129 return -1;
130 }
131 assert(channels);
132 ss->channels = channels;
133
134 speed = ss->rate;
135 if (ioctl(fd, SNDCTL_DSP_SPEED, &speed) < 0) {
136 pa_log(__FILE__": SNDCTL_DSP_SPEED: %s\n", strerror(errno));
137 return -1;
138 }
139 assert(speed);
140 ss->rate = speed;
141
142 return 0;
143 }
144
145 static int simple_log2(int v) {
146 int k = 0;
147
148 for (;;) {
149 v >>= 1;
150 if (!v) break;
151 k++;
152 }
153
154 return k;
155 }
156
157 int pa_oss_set_fragments(int fd, int nfrags, int frag_size) {
158 int arg;
159 arg = ((int) nfrags << 16) | simple_log2(frag_size);
160
161 if (ioctl(fd, SNDCTL_DSP_SETFRAGMENT, &arg) < 0) {
162 pa_log(__FILE__": SNDCTL_DSP_SETFRAGMENT: %s\n", strerror(errno));
163 return -1;
164 }
165
166 return 0;
167 }