root / audio / ossaudio.c @ cf2c1839
History | View | Annotate | Download (23.7 kB)
1 | 85571bc7 | bellard | /*
|
---|---|---|---|
2 | 1d14ffa9 | bellard | * QEMU OSS audio driver
|
3 | 1d14ffa9 | bellard | *
|
4 | 1d14ffa9 | bellard | * Copyright (c) 2003-2005 Vassili Karpov (malc)
|
5 | 1d14ffa9 | bellard | *
|
6 | 85571bc7 | bellard | * Permission is hereby granted, free of charge, to any person obtaining a copy
|
7 | 85571bc7 | bellard | * of this software and associated documentation files (the "Software"), to deal
|
8 | 85571bc7 | bellard | * in the Software without restriction, including without limitation the rights
|
9 | 85571bc7 | bellard | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
10 | 85571bc7 | bellard | * copies of the Software, and to permit persons to whom the Software is
|
11 | 85571bc7 | bellard | * furnished to do so, subject to the following conditions:
|
12 | 85571bc7 | bellard | *
|
13 | 85571bc7 | bellard | * The above copyright notice and this permission notice shall be included in
|
14 | 85571bc7 | bellard | * all copies or substantial portions of the Software.
|
15 | 85571bc7 | bellard | *
|
16 | 85571bc7 | bellard | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
17 | 85571bc7 | bellard | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
18 | 85571bc7 | bellard | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
19 | 85571bc7 | bellard | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
20 | 85571bc7 | bellard | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
21 | 85571bc7 | bellard | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
22 | 85571bc7 | bellard | * THE SOFTWARE.
|
23 | 85571bc7 | bellard | */
|
24 | f0c757e4 | ths | #include <stdlib.h> |
25 | 85571bc7 | bellard | #include <sys/mman.h> |
26 | 85571bc7 | bellard | #include <sys/types.h> |
27 | 85571bc7 | bellard | #include <sys/ioctl.h> |
28 | f0c757e4 | ths | #ifdef __OpenBSD__
|
29 | f0c757e4 | ths | #include <soundcard.h> |
30 | f0c757e4 | ths | #else
|
31 | 85571bc7 | bellard | #include <sys/soundcard.h> |
32 | f0c757e4 | ths | #endif
|
33 | 87ecb68b | pbrook | #include "qemu-common.h" |
34 | 057fa65c | malc | #include "host-utils.h" |
35 | dd8a5649 | malc | #include "qemu-char.h" |
36 | 87ecb68b | pbrook | #include "audio.h" |
37 | fb065187 | bellard | |
38 | 1d14ffa9 | bellard | #define AUDIO_CAP "oss" |
39 | 1d14ffa9 | bellard | #include "audio_int.h" |
40 | fb065187 | bellard | |
41 | 78d9356d | malc | #if defined OSS_GETVERSION && defined SNDCTL_DSP_POLICY
|
42 | 78d9356d | malc | #define USE_DSP_POLICY
|
43 | 78d9356d | malc | #endif
|
44 | 78d9356d | malc | |
45 | 1d14ffa9 | bellard | typedef struct OSSVoiceOut { |
46 | 1d14ffa9 | bellard | HWVoiceOut hw; |
47 | fb065187 | bellard | void *pcm_buf;
|
48 | fb065187 | bellard | int fd;
|
49 | 9d168976 | malc | int wpos;
|
50 | fb065187 | bellard | int nfrags;
|
51 | fb065187 | bellard | int fragsize;
|
52 | fb065187 | bellard | int mmapped;
|
53 | 9d168976 | malc | int pending;
|
54 | 1d14ffa9 | bellard | } OSSVoiceOut; |
55 | 85571bc7 | bellard | |
56 | 1d14ffa9 | bellard | typedef struct OSSVoiceIn { |
57 | 1d14ffa9 | bellard | HWVoiceIn hw; |
58 | 1d14ffa9 | bellard | void *pcm_buf;
|
59 | 1d14ffa9 | bellard | int fd;
|
60 | 1d14ffa9 | bellard | int nfrags;
|
61 | 1d14ffa9 | bellard | int fragsize;
|
62 | 1d14ffa9 | bellard | } OSSVoiceIn; |
63 | 85571bc7 | bellard | |
64 | 85571bc7 | bellard | static struct { |
65 | 85571bc7 | bellard | int try_mmap;
|
66 | 85571bc7 | bellard | int nfrags;
|
67 | 85571bc7 | bellard | int fragsize;
|
68 | 1d14ffa9 | bellard | const char *devpath_out; |
69 | 1d14ffa9 | bellard | const char *devpath_in; |
70 | 8ead62cf | bellard | int debug;
|
71 | 0b3652bc | malc | int exclusive;
|
72 | 0b3652bc | malc | int policy;
|
73 | 85571bc7 | bellard | } conf = { |
74 | 85571bc7 | bellard | .try_mmap = 0,
|
75 | 85571bc7 | bellard | .nfrags = 4,
|
76 | 85571bc7 | bellard | .fragsize = 4096,
|
77 | 1d14ffa9 | bellard | .devpath_out = "/dev/dsp",
|
78 | 8ead62cf | bellard | .devpath_in = "/dev/dsp",
|
79 | 0b3652bc | malc | .debug = 0,
|
80 | 0b3652bc | malc | .exclusive = 0,
|
81 | 0b3652bc | malc | .policy = 5
|
82 | 85571bc7 | bellard | }; |
83 | 85571bc7 | bellard | |
84 | 85571bc7 | bellard | struct oss_params {
|
85 | 85571bc7 | bellard | int freq;
|
86 | 85571bc7 | bellard | audfmt_e fmt; |
87 | 85571bc7 | bellard | int nchannels;
|
88 | 85571bc7 | bellard | int nfrags;
|
89 | 85571bc7 | bellard | int fragsize;
|
90 | 85571bc7 | bellard | }; |
91 | 85571bc7 | bellard | |
92 | 1d14ffa9 | bellard | static void GCC_FMT_ATTR (2, 3) oss_logerr (int err, const char *fmt, ...) |
93 | 85571bc7 | bellard | { |
94 | 1d14ffa9 | bellard | va_list ap; |
95 | 1d14ffa9 | bellard | |
96 | 571ec3d6 | bellard | va_start (ap, fmt); |
97 | 1d14ffa9 | bellard | AUD_vlog (AUDIO_CAP, fmt, ap); |
98 | 571ec3d6 | bellard | va_end (ap); |
99 | 1d14ffa9 | bellard | |
100 | 1d14ffa9 | bellard | AUD_log (AUDIO_CAP, "Reason: %s\n", strerror (err));
|
101 | 85571bc7 | bellard | } |
102 | 85571bc7 | bellard | |
103 | 1d14ffa9 | bellard | static void GCC_FMT_ATTR (3, 4) oss_logerr2 ( |
104 | 1d14ffa9 | bellard | int err,
|
105 | 1d14ffa9 | bellard | const char *typ, |
106 | 1d14ffa9 | bellard | const char *fmt, |
107 | 1d14ffa9 | bellard | ... |
108 | 1d14ffa9 | bellard | ) |
109 | 1d14ffa9 | bellard | { |
110 | 1d14ffa9 | bellard | va_list ap; |
111 | 1d14ffa9 | bellard | |
112 | c0fe3827 | bellard | AUD_log (AUDIO_CAP, "Could not initialize %s\n", typ);
|
113 | 1d14ffa9 | bellard | |
114 | 1d14ffa9 | bellard | va_start (ap, fmt); |
115 | 1d14ffa9 | bellard | AUD_vlog (AUDIO_CAP, fmt, ap); |
116 | 1d14ffa9 | bellard | va_end (ap); |
117 | 1d14ffa9 | bellard | |
118 | 1d14ffa9 | bellard | AUD_log (AUDIO_CAP, "Reason: %s\n", strerror (err));
|
119 | 1d14ffa9 | bellard | } |
120 | 1d14ffa9 | bellard | |
121 | 1d14ffa9 | bellard | static void oss_anal_close (int *fdp) |
122 | 1d14ffa9 | bellard | { |
123 | 6ebfda13 | malc | int err;
|
124 | 6ebfda13 | malc | |
125 | 6ebfda13 | malc | qemu_set_fd_handler (*fdp, NULL, NULL, NULL); |
126 | 6ebfda13 | malc | err = close (*fdp); |
127 | 1d14ffa9 | bellard | if (err) {
|
128 | 1d14ffa9 | bellard | oss_logerr (errno, "Failed to close file(fd=%d)\n", *fdp);
|
129 | 1d14ffa9 | bellard | } |
130 | 1d14ffa9 | bellard | *fdp = -1;
|
131 | 1d14ffa9 | bellard | } |
132 | 1d14ffa9 | bellard | |
133 | dd8a5649 | malc | static void oss_helper_poll_out (void *opaque) |
134 | dd8a5649 | malc | { |
135 | dd8a5649 | malc | (void) opaque;
|
136 | dd8a5649 | malc | audio_run ("oss_poll_out");
|
137 | dd8a5649 | malc | } |
138 | dd8a5649 | malc | |
139 | dd8a5649 | malc | static void oss_helper_poll_in (void *opaque) |
140 | dd8a5649 | malc | { |
141 | dd8a5649 | malc | (void) opaque;
|
142 | dd8a5649 | malc | audio_run ("oss_poll_in");
|
143 | dd8a5649 | malc | } |
144 | dd8a5649 | malc | |
145 | dd8a5649 | malc | static int oss_poll_out (HWVoiceOut *hw) |
146 | dd8a5649 | malc | { |
147 | dd8a5649 | malc | OSSVoiceOut *oss = (OSSVoiceOut *) hw; |
148 | dd8a5649 | malc | |
149 | dd8a5649 | malc | return qemu_set_fd_handler (oss->fd, NULL, oss_helper_poll_out, NULL); |
150 | dd8a5649 | malc | } |
151 | dd8a5649 | malc | |
152 | dd8a5649 | malc | static int oss_poll_in (HWVoiceIn *hw) |
153 | dd8a5649 | malc | { |
154 | dd8a5649 | malc | OSSVoiceIn *oss = (OSSVoiceIn *) hw; |
155 | dd8a5649 | malc | |
156 | dd8a5649 | malc | return qemu_set_fd_handler (oss->fd, oss_helper_poll_in, NULL, NULL); |
157 | dd8a5649 | malc | } |
158 | dd8a5649 | malc | |
159 | 1d14ffa9 | bellard | static int oss_write (SWVoiceOut *sw, void *buf, int len) |
160 | 1d14ffa9 | bellard | { |
161 | 1d14ffa9 | bellard | return audio_pcm_sw_write (sw, buf, len);
|
162 | 1d14ffa9 | bellard | } |
163 | 1d14ffa9 | bellard | |
164 | 1d14ffa9 | bellard | static int aud_to_ossfmt (audfmt_e fmt) |
165 | 85571bc7 | bellard | { |
166 | 85571bc7 | bellard | switch (fmt) {
|
167 | 1d14ffa9 | bellard | case AUD_FMT_S8:
|
168 | 1d14ffa9 | bellard | return AFMT_S8;
|
169 | 1d14ffa9 | bellard | |
170 | 1d14ffa9 | bellard | case AUD_FMT_U8:
|
171 | 1d14ffa9 | bellard | return AFMT_U8;
|
172 | 1d14ffa9 | bellard | |
173 | 1d14ffa9 | bellard | case AUD_FMT_S16:
|
174 | 1d14ffa9 | bellard | return AFMT_S16_LE;
|
175 | 1d14ffa9 | bellard | |
176 | 1d14ffa9 | bellard | case AUD_FMT_U16:
|
177 | 1d14ffa9 | bellard | return AFMT_U16_LE;
|
178 | 1d14ffa9 | bellard | |
179 | 85571bc7 | bellard | default:
|
180 | 1d14ffa9 | bellard | dolog ("Internal logic error: Bad audio format %d\n", fmt);
|
181 | 1d14ffa9 | bellard | #ifdef DEBUG_AUDIO
|
182 | 1d14ffa9 | bellard | abort (); |
183 | 1d14ffa9 | bellard | #endif
|
184 | 1d14ffa9 | bellard | return AFMT_U8;
|
185 | 85571bc7 | bellard | } |
186 | 85571bc7 | bellard | } |
187 | 85571bc7 | bellard | |
188 | 1d14ffa9 | bellard | static int oss_to_audfmt (int ossfmt, audfmt_e *fmt, int *endianness) |
189 | 85571bc7 | bellard | { |
190 | 1d14ffa9 | bellard | switch (ossfmt) {
|
191 | 1d14ffa9 | bellard | case AFMT_S8:
|
192 | ca9cc28c | balrog | *endianness = 0;
|
193 | 1d14ffa9 | bellard | *fmt = AUD_FMT_S8; |
194 | 1d14ffa9 | bellard | break;
|
195 | 1d14ffa9 | bellard | |
196 | 1d14ffa9 | bellard | case AFMT_U8:
|
197 | 1d14ffa9 | bellard | *endianness = 0;
|
198 | 1d14ffa9 | bellard | *fmt = AUD_FMT_U8; |
199 | 1d14ffa9 | bellard | break;
|
200 | 1d14ffa9 | bellard | |
201 | 1d14ffa9 | bellard | case AFMT_S16_LE:
|
202 | 1d14ffa9 | bellard | *endianness = 0;
|
203 | 1d14ffa9 | bellard | *fmt = AUD_FMT_S16; |
204 | 1d14ffa9 | bellard | break;
|
205 | 1d14ffa9 | bellard | |
206 | 1d14ffa9 | bellard | case AFMT_U16_LE:
|
207 | 1d14ffa9 | bellard | *endianness = 0;
|
208 | 1d14ffa9 | bellard | *fmt = AUD_FMT_U16; |
209 | 1d14ffa9 | bellard | break;
|
210 | 1d14ffa9 | bellard | |
211 | 1d14ffa9 | bellard | case AFMT_S16_BE:
|
212 | 1d14ffa9 | bellard | *endianness = 1;
|
213 | 1d14ffa9 | bellard | *fmt = AUD_FMT_S16; |
214 | 1d14ffa9 | bellard | break;
|
215 | 1d14ffa9 | bellard | |
216 | 1d14ffa9 | bellard | case AFMT_U16_BE:
|
217 | 1d14ffa9 | bellard | *endianness = 1;
|
218 | 1d14ffa9 | bellard | *fmt = AUD_FMT_U16; |
219 | 1d14ffa9 | bellard | break;
|
220 | 1d14ffa9 | bellard | |
221 | 85571bc7 | bellard | default:
|
222 | 1d14ffa9 | bellard | dolog ("Unrecognized audio format %d\n", ossfmt);
|
223 | 1d14ffa9 | bellard | return -1; |
224 | 85571bc7 | bellard | } |
225 | 1d14ffa9 | bellard | |
226 | 1d14ffa9 | bellard | return 0; |
227 | 85571bc7 | bellard | } |
228 | 85571bc7 | bellard | |
229 | c0fe3827 | bellard | #if defined DEBUG_MISMATCHES || defined DEBUG
|
230 | 1d14ffa9 | bellard | static void oss_dump_info (struct oss_params *req, struct oss_params *obt) |
231 | 85571bc7 | bellard | { |
232 | 85571bc7 | bellard | dolog ("parameter | requested value | obtained value\n");
|
233 | 85571bc7 | bellard | dolog ("format | %10d | %10d\n", req->fmt, obt->fmt);
|
234 | 1d14ffa9 | bellard | dolog ("channels | %10d | %10d\n",
|
235 | 1d14ffa9 | bellard | req->nchannels, obt->nchannels); |
236 | 85571bc7 | bellard | dolog ("frequency | %10d | %10d\n", req->freq, obt->freq);
|
237 | 85571bc7 | bellard | dolog ("nfrags | %10d | %10d\n", req->nfrags, obt->nfrags);
|
238 | 1d14ffa9 | bellard | dolog ("fragsize | %10d | %10d\n",
|
239 | 1d14ffa9 | bellard | req->fragsize, obt->fragsize); |
240 | 85571bc7 | bellard | } |
241 | 85571bc7 | bellard | #endif
|
242 | 85571bc7 | bellard | |
243 | 72ff25e4 | Juergen Lock | #ifdef USE_DSP_POLICY
|
244 | 72ff25e4 | Juergen Lock | static int oss_get_version (int fd, int *version, const char *typ) |
245 | 72ff25e4 | Juergen Lock | { |
246 | 72ff25e4 | Juergen Lock | if (ioctl (fd, OSS_GETVERSION, &version)) {
|
247 | 72ff25e4 | Juergen Lock | #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
|
248 | 72ff25e4 | Juergen Lock | /*
|
249 | 72ff25e4 | Juergen Lock | * Looks like atm (20100109) FreeBSD knows OSS_GETVERSION
|
250 | 72ff25e4 | Juergen Lock | * since 7.x, but currently only on the mixer device (or in
|
251 | 72ff25e4 | Juergen Lock | * the Linuxolator), and in the native version that part of
|
252 | 72ff25e4 | Juergen Lock | * the code is in fact never reached so the ioctl fails anyway.
|
253 | 72ff25e4 | Juergen Lock | * Until this is fixed, just check the errno and if its what
|
254 | 72ff25e4 | Juergen Lock | * FreeBSD's sound drivers return atm assume they are new enough.
|
255 | 72ff25e4 | Juergen Lock | */
|
256 | 72ff25e4 | Juergen Lock | if (errno == EINVAL) {
|
257 | 72ff25e4 | Juergen Lock | *version = 0x040000;
|
258 | 72ff25e4 | Juergen Lock | return 0; |
259 | 72ff25e4 | Juergen Lock | } |
260 | 72ff25e4 | Juergen Lock | #endif
|
261 | 72ff25e4 | Juergen Lock | oss_logerr2 (errno, typ, "Failed to get OSS version\n");
|
262 | 72ff25e4 | Juergen Lock | return -1; |
263 | 72ff25e4 | Juergen Lock | } |
264 | 72ff25e4 | Juergen Lock | return 0; |
265 | 72ff25e4 | Juergen Lock | } |
266 | 72ff25e4 | Juergen Lock | #endif
|
267 | 72ff25e4 | Juergen Lock | |
268 | 1d14ffa9 | bellard | static int oss_open (int in, struct oss_params *req, |
269 | 1d14ffa9 | bellard | struct oss_params *obt, int *pfd) |
270 | 85571bc7 | bellard | { |
271 | 85571bc7 | bellard | int fd;
|
272 | 0b3652bc | malc | int oflags = conf.exclusive ? O_EXCL : 0; |
273 | 85571bc7 | bellard | audio_buf_info abinfo; |
274 | 85571bc7 | bellard | int fmt, freq, nchannels;
|
275 | 3d709fe7 | malc | int setfragment = 1; |
276 | 1d14ffa9 | bellard | const char *dspname = in ? conf.devpath_in : conf.devpath_out; |
277 | 1d14ffa9 | bellard | const char *typ = in ? "ADC" : "DAC"; |
278 | 85571bc7 | bellard | |
279 | 2182349d | malc | /* Kludge needed to have working mmap on Linux */
|
280 | 0b3652bc | malc | oflags |= conf.try_mmap ? O_RDWR : (in ? O_RDONLY : O_WRONLY); |
281 | 0b3652bc | malc | |
282 | 2182349d | malc | fd = open (dspname, oflags | O_NONBLOCK); |
283 | 85571bc7 | bellard | if (-1 == fd) { |
284 | 1d14ffa9 | bellard | oss_logerr2 (errno, typ, "Failed to open `%s'\n", dspname);
|
285 | 85571bc7 | bellard | return -1; |
286 | 85571bc7 | bellard | } |
287 | 85571bc7 | bellard | |
288 | 85571bc7 | bellard | freq = req->freq; |
289 | 85571bc7 | bellard | nchannels = req->nchannels; |
290 | 85571bc7 | bellard | fmt = req->fmt; |
291 | 85571bc7 | bellard | |
292 | 85571bc7 | bellard | if (ioctl (fd, SNDCTL_DSP_SAMPLESIZE, &fmt)) {
|
293 | 1d14ffa9 | bellard | oss_logerr2 (errno, typ, "Failed to set sample size %d\n", req->fmt);
|
294 | 85571bc7 | bellard | goto err;
|
295 | 85571bc7 | bellard | } |
296 | 85571bc7 | bellard | |
297 | 85571bc7 | bellard | if (ioctl (fd, SNDCTL_DSP_CHANNELS, &nchannels)) {
|
298 | 1d14ffa9 | bellard | oss_logerr2 (errno, typ, "Failed to set number of channels %d\n",
|
299 | 1d14ffa9 | bellard | req->nchannels); |
300 | 85571bc7 | bellard | goto err;
|
301 | 85571bc7 | bellard | } |
302 | 85571bc7 | bellard | |
303 | 85571bc7 | bellard | if (ioctl (fd, SNDCTL_DSP_SPEED, &freq)) {
|
304 | 1d14ffa9 | bellard | oss_logerr2 (errno, typ, "Failed to set frequency %d\n", req->freq);
|
305 | 85571bc7 | bellard | goto err;
|
306 | 85571bc7 | bellard | } |
307 | 85571bc7 | bellard | |
308 | 902e2b51 | malc | if (ioctl (fd, SNDCTL_DSP_NONBLOCK, NULL)) { |
309 | 1d14ffa9 | bellard | oss_logerr2 (errno, typ, "Failed to set non-blocking mode\n");
|
310 | 85571bc7 | bellard | goto err;
|
311 | 85571bc7 | bellard | } |
312 | 85571bc7 | bellard | |
313 | 78d9356d | malc | #ifdef USE_DSP_POLICY
|
314 | 6d246526 | malc | if (conf.policy >= 0) { |
315 | 6d246526 | malc | int version;
|
316 | 0b3652bc | malc | |
317 | 72ff25e4 | Juergen Lock | if (!oss_get_version (fd, &version, typ)) {
|
318 | 3d709fe7 | malc | if (conf.debug) {
|
319 | 3d709fe7 | malc | dolog ("OSS version = %#x\n", version);
|
320 | 3d709fe7 | malc | } |
321 | 6d246526 | malc | |
322 | 3d709fe7 | malc | if (version >= 0x040000) { |
323 | 3d709fe7 | malc | int policy = conf.policy;
|
324 | 3d709fe7 | malc | if (ioctl (fd, SNDCTL_DSP_POLICY, &policy)) {
|
325 | 3d709fe7 | malc | oss_logerr2 (errno, typ, |
326 | 3d709fe7 | malc | "Failed to set timing policy to %d\n",
|
327 | 3d709fe7 | malc | conf.policy); |
328 | 3d709fe7 | malc | goto err;
|
329 | 3d709fe7 | malc | } |
330 | 3d709fe7 | malc | setfragment = 0;
|
331 | 6d246526 | malc | } |
332 | 0b3652bc | malc | } |
333 | 0b3652bc | malc | } |
334 | 0b3652bc | malc | #endif
|
335 | 3d709fe7 | malc | |
336 | 3d709fe7 | malc | if (setfragment) {
|
337 | 0b3652bc | malc | int mmmmssss = (req->nfrags << 16) | ctz32 (req->fragsize); |
338 | 0b3652bc | malc | if (ioctl (fd, SNDCTL_DSP_SETFRAGMENT, &mmmmssss)) {
|
339 | 0b3652bc | malc | oss_logerr2 (errno, typ, "Failed to set buffer length (%d, %d)\n",
|
340 | 0b3652bc | malc | req->nfrags, req->fragsize); |
341 | 0b3652bc | malc | goto err;
|
342 | 0b3652bc | malc | } |
343 | 85571bc7 | bellard | } |
344 | 85571bc7 | bellard | |
345 | 1d14ffa9 | bellard | if (ioctl (fd, in ? SNDCTL_DSP_GETISPACE : SNDCTL_DSP_GETOSPACE, &abinfo)) {
|
346 | 1d14ffa9 | bellard | oss_logerr2 (errno, typ, "Failed to get buffer length\n");
|
347 | 85571bc7 | bellard | goto err;
|
348 | 85571bc7 | bellard | } |
349 | 85571bc7 | bellard | |
350 | 29ddf27b | malc | if (!abinfo.fragstotal || !abinfo.fragsize) {
|
351 | 29ddf27b | malc | AUD_log (AUDIO_CAP, "Returned bogus buffer information(%d, %d) for %s\n",
|
352 | 29ddf27b | malc | abinfo.fragstotal, abinfo.fragsize, typ); |
353 | 29ddf27b | malc | goto err;
|
354 | 29ddf27b | malc | } |
355 | 29ddf27b | malc | |
356 | 85571bc7 | bellard | obt->fmt = fmt; |
357 | 85571bc7 | bellard | obt->nchannels = nchannels; |
358 | 85571bc7 | bellard | obt->freq = freq; |
359 | 85571bc7 | bellard | obt->nfrags = abinfo.fragstotal; |
360 | 85571bc7 | bellard | obt->fragsize = abinfo.fragsize; |
361 | 85571bc7 | bellard | *pfd = fd; |
362 | 85571bc7 | bellard | |
363 | c0fe3827 | bellard | #ifdef DEBUG_MISMATCHES
|
364 | 85571bc7 | bellard | if ((req->fmt != obt->fmt) ||
|
365 | 85571bc7 | bellard | (req->nchannels != obt->nchannels) || |
366 | 85571bc7 | bellard | (req->freq != obt->freq) || |
367 | 85571bc7 | bellard | (req->fragsize != obt->fragsize) || |
368 | 85571bc7 | bellard | (req->nfrags != obt->nfrags)) { |
369 | 85571bc7 | bellard | dolog ("Audio parameters mismatch\n");
|
370 | 1d14ffa9 | bellard | oss_dump_info (req, obt); |
371 | 85571bc7 | bellard | } |
372 | c0fe3827 | bellard | #endif
|
373 | 85571bc7 | bellard | |
374 | 1d14ffa9 | bellard | #ifdef DEBUG
|
375 | 1d14ffa9 | bellard | oss_dump_info (req, obt); |
376 | 85571bc7 | bellard | #endif
|
377 | 85571bc7 | bellard | return 0; |
378 | 85571bc7 | bellard | |
379 | 1d14ffa9 | bellard | err:
|
380 | 1d14ffa9 | bellard | oss_anal_close (&fd); |
381 | 85571bc7 | bellard | return -1; |
382 | 85571bc7 | bellard | } |
383 | 85571bc7 | bellard | |
384 | 9d168976 | malc | static void oss_write_pending (OSSVoiceOut *oss) |
385 | 9d168976 | malc | { |
386 | 9d168976 | malc | HWVoiceOut *hw = &oss->hw; |
387 | 9d168976 | malc | |
388 | 9d168976 | malc | if (oss->mmapped) {
|
389 | 9d168976 | malc | return;
|
390 | 9d168976 | malc | } |
391 | 9d168976 | malc | |
392 | 9d168976 | malc | while (oss->pending) {
|
393 | 9d168976 | malc | int samples_written;
|
394 | 9d168976 | malc | ssize_t bytes_written; |
395 | 9d168976 | malc | int samples_till_end = hw->samples - oss->wpos;
|
396 | 9d168976 | malc | int samples_to_write = audio_MIN (oss->pending, samples_till_end);
|
397 | 9d168976 | malc | int bytes_to_write = samples_to_write << hw->info.shift;
|
398 | 9d168976 | malc | void *pcm = advance (oss->pcm_buf, oss->wpos << hw->info.shift);
|
399 | 9d168976 | malc | |
400 | 9d168976 | malc | bytes_written = write (oss->fd, pcm, bytes_to_write); |
401 | 9d168976 | malc | if (bytes_written < 0) { |
402 | 9d168976 | malc | if (errno != EAGAIN) {
|
403 | 9d168976 | malc | oss_logerr (errno, "failed to write %d bytes\n",
|
404 | 9d168976 | malc | bytes_to_write); |
405 | 9d168976 | malc | } |
406 | 9d168976 | malc | break;
|
407 | 9d168976 | malc | } |
408 | 9d168976 | malc | |
409 | 9d168976 | malc | if (bytes_written & hw->info.align) {
|
410 | 9d168976 | malc | dolog ("misaligned write asked for %d, but got %zd\n",
|
411 | 9d168976 | malc | bytes_to_write, bytes_written); |
412 | 9d168976 | malc | return;
|
413 | 9d168976 | malc | } |
414 | 9d168976 | malc | |
415 | 9d168976 | malc | samples_written = bytes_written >> hw->info.shift; |
416 | 9d168976 | malc | oss->pending -= samples_written; |
417 | 9d168976 | malc | oss->wpos = (oss->wpos + samples_written) % hw->samples; |
418 | 9d168976 | malc | if (bytes_written - bytes_to_write) {
|
419 | 9d168976 | malc | break;
|
420 | 9d168976 | malc | } |
421 | 9d168976 | malc | } |
422 | 9d168976 | malc | } |
423 | 9d168976 | malc | |
424 | bdff253c | malc | static int oss_run_out (HWVoiceOut *hw, int live) |
425 | 85571bc7 | bellard | { |
426 | 1d14ffa9 | bellard | OSSVoiceOut *oss = (OSSVoiceOut *) hw; |
427 | bdff253c | malc | int err, decr;
|
428 | 85571bc7 | bellard | struct audio_buf_info abinfo;
|
429 | 85571bc7 | bellard | struct count_info cntinfo;
|
430 | c0fe3827 | bellard | int bufsize;
|
431 | 85571bc7 | bellard | |
432 | c0fe3827 | bellard | bufsize = hw->samples << hw->info.shift; |
433 | c0fe3827 | bellard | |
434 | 85571bc7 | bellard | if (oss->mmapped) {
|
435 | 54762b73 | malc | int bytes, pos;
|
436 | 85571bc7 | bellard | |
437 | 85571bc7 | bellard | err = ioctl (oss->fd, SNDCTL_DSP_GETOPTR, &cntinfo); |
438 | 85571bc7 | bellard | if (err < 0) { |
439 | 1d14ffa9 | bellard | oss_logerr (errno, "SNDCTL_DSP_GETOPTR failed\n");
|
440 | 1d14ffa9 | bellard | return 0; |
441 | 85571bc7 | bellard | } |
442 | 85571bc7 | bellard | |
443 | 54762b73 | malc | pos = hw->rpos << hw->info.shift; |
444 | 54762b73 | malc | bytes = audio_ring_dist (cntinfo.ptr, pos, bufsize); |
445 | 1d14ffa9 | bellard | decr = audio_MIN (bytes >> hw->info.shift, live); |
446 | 85571bc7 | bellard | } |
447 | 85571bc7 | bellard | else {
|
448 | 85571bc7 | bellard | err = ioctl (oss->fd, SNDCTL_DSP_GETOSPACE, &abinfo); |
449 | 85571bc7 | bellard | if (err < 0) { |
450 | 1d14ffa9 | bellard | oss_logerr (errno, "SNDCTL_DSP_GETOPTR failed\n");
|
451 | 1d14ffa9 | bellard | return 0; |
452 | 1d14ffa9 | bellard | } |
453 | 1d14ffa9 | bellard | |
454 | 8ead62cf | bellard | if (abinfo.bytes > bufsize) {
|
455 | 8ead62cf | bellard | if (conf.debug) {
|
456 | 8ead62cf | bellard | dolog ("warning: Invalid available size, size=%d bufsize=%d\n"
|
457 | 155a8ad3 | malc | "please report your OS/audio hw to av1474@comtv.ru\n",
|
458 | 8ead62cf | bellard | abinfo.bytes, bufsize); |
459 | 8ead62cf | bellard | } |
460 | 8ead62cf | bellard | abinfo.bytes = bufsize; |
461 | 8ead62cf | bellard | } |
462 | 8ead62cf | bellard | |
463 | 8ead62cf | bellard | if (abinfo.bytes < 0) { |
464 | 8ead62cf | bellard | if (conf.debug) {
|
465 | 8ead62cf | bellard | dolog ("warning: Invalid available size, size=%d bufsize=%d\n",
|
466 | 8ead62cf | bellard | abinfo.bytes, bufsize); |
467 | 8ead62cf | bellard | } |
468 | 1d14ffa9 | bellard | return 0; |
469 | 85571bc7 | bellard | } |
470 | 85571bc7 | bellard | |
471 | 1d14ffa9 | bellard | decr = audio_MIN (abinfo.bytes >> hw->info.shift, live); |
472 | 1d14ffa9 | bellard | if (!decr) {
|
473 | 1d14ffa9 | bellard | return 0; |
474 | 1d14ffa9 | bellard | } |
475 | 85571bc7 | bellard | } |
476 | 85571bc7 | bellard | |
477 | 9d168976 | malc | decr = audio_pcm_hw_clip_out (hw, oss->pcm_buf, decr, oss->pending); |
478 | 9d168976 | malc | oss->pending += decr; |
479 | 9d168976 | malc | oss_write_pending (oss); |
480 | 85571bc7 | bellard | |
481 | 1d14ffa9 | bellard | return decr;
|
482 | 85571bc7 | bellard | } |
483 | 85571bc7 | bellard | |
484 | 1d14ffa9 | bellard | static void oss_fini_out (HWVoiceOut *hw) |
485 | 85571bc7 | bellard | { |
486 | 85571bc7 | bellard | int err;
|
487 | 1d14ffa9 | bellard | OSSVoiceOut *oss = (OSSVoiceOut *) hw; |
488 | 85571bc7 | bellard | |
489 | 1d14ffa9 | bellard | ldebug ("oss_fini\n");
|
490 | 1d14ffa9 | bellard | oss_anal_close (&oss->fd); |
491 | 85571bc7 | bellard | |
492 | 85571bc7 | bellard | if (oss->pcm_buf) {
|
493 | 85571bc7 | bellard | if (oss->mmapped) {
|
494 | c0fe3827 | bellard | err = munmap (oss->pcm_buf, hw->samples << hw->info.shift); |
495 | 85571bc7 | bellard | if (err) {
|
496 | 1d14ffa9 | bellard | oss_logerr (errno, "Failed to unmap buffer %p, size %d\n",
|
497 | c0fe3827 | bellard | oss->pcm_buf, hw->samples << hw->info.shift); |
498 | 85571bc7 | bellard | } |
499 | 85571bc7 | bellard | } |
500 | 85571bc7 | bellard | else {
|
501 | 85571bc7 | bellard | qemu_free (oss->pcm_buf); |
502 | 85571bc7 | bellard | } |
503 | 85571bc7 | bellard | oss->pcm_buf = NULL;
|
504 | 85571bc7 | bellard | } |
505 | 85571bc7 | bellard | } |
506 | 85571bc7 | bellard | |
507 | 1ea879e5 | malc | static int oss_init_out (HWVoiceOut *hw, struct audsettings *as) |
508 | 85571bc7 | bellard | { |
509 | 1d14ffa9 | bellard | OSSVoiceOut *oss = (OSSVoiceOut *) hw; |
510 | 85571bc7 | bellard | struct oss_params req, obt;
|
511 | 1d14ffa9 | bellard | int endianness;
|
512 | 1d14ffa9 | bellard | int err;
|
513 | 1d14ffa9 | bellard | int fd;
|
514 | 1d14ffa9 | bellard | audfmt_e effective_fmt; |
515 | 1ea879e5 | malc | struct audsettings obt_as;
|
516 | 85571bc7 | bellard | |
517 | 571ec3d6 | bellard | oss->fd = -1;
|
518 | 571ec3d6 | bellard | |
519 | c0fe3827 | bellard | req.fmt = aud_to_ossfmt (as->fmt); |
520 | c0fe3827 | bellard | req.freq = as->freq; |
521 | c0fe3827 | bellard | req.nchannels = as->nchannels; |
522 | 85571bc7 | bellard | req.fragsize = conf.fragsize; |
523 | 85571bc7 | bellard | req.nfrags = conf.nfrags; |
524 | 85571bc7 | bellard | |
525 | 1d14ffa9 | bellard | if (oss_open (0, &req, &obt, &fd)) { |
526 | 85571bc7 | bellard | return -1; |
527 | 1d14ffa9 | bellard | } |
528 | 85571bc7 | bellard | |
529 | 1d14ffa9 | bellard | err = oss_to_audfmt (obt.fmt, &effective_fmt, &endianness); |
530 | 1d14ffa9 | bellard | if (err) {
|
531 | 1d14ffa9 | bellard | oss_anal_close (&fd); |
532 | 1d14ffa9 | bellard | return -1; |
533 | 1d14ffa9 | bellard | } |
534 | 85571bc7 | bellard | |
535 | c0fe3827 | bellard | obt_as.freq = obt.freq; |
536 | c0fe3827 | bellard | obt_as.nchannels = obt.nchannels; |
537 | c0fe3827 | bellard | obt_as.fmt = effective_fmt; |
538 | d929eba5 | bellard | obt_as.endianness = endianness; |
539 | c0fe3827 | bellard | |
540 | d929eba5 | bellard | audio_pcm_init_info (&hw->info, &obt_as); |
541 | 85571bc7 | bellard | oss->nfrags = obt.nfrags; |
542 | 85571bc7 | bellard | oss->fragsize = obt.fragsize; |
543 | c0fe3827 | bellard | |
544 | c0fe3827 | bellard | if (obt.nfrags * obt.fragsize & hw->info.align) {
|
545 | c0fe3827 | bellard | dolog ("warning: Misaligned DAC buffer, size %d, alignment %d\n",
|
546 | c0fe3827 | bellard | obt.nfrags * obt.fragsize, hw->info.align + 1);
|
547 | c0fe3827 | bellard | } |
548 | c0fe3827 | bellard | |
549 | c0fe3827 | bellard | hw->samples = (obt.nfrags * obt.fragsize) >> hw->info.shift; |
550 | 85571bc7 | bellard | |
551 | 85571bc7 | bellard | oss->mmapped = 0;
|
552 | 85571bc7 | bellard | if (conf.try_mmap) {
|
553 | c0fe3827 | bellard | oss->pcm_buf = mmap ( |
554 | 660f11be | Blue Swirl | NULL,
|
555 | c0fe3827 | bellard | hw->samples << hw->info.shift, |
556 | c0fe3827 | bellard | PROT_READ | PROT_WRITE, |
557 | c0fe3827 | bellard | MAP_SHARED, |
558 | c0fe3827 | bellard | fd, |
559 | c0fe3827 | bellard | 0
|
560 | c0fe3827 | bellard | ); |
561 | 85571bc7 | bellard | if (oss->pcm_buf == MAP_FAILED) {
|
562 | 1d14ffa9 | bellard | oss_logerr (errno, "Failed to map %d bytes of DAC\n",
|
563 | c0fe3827 | bellard | hw->samples << hw->info.shift); |
564 | 54762b73 | malc | } |
565 | 54762b73 | malc | else {
|
566 | 85571bc7 | bellard | int err;
|
567 | 85571bc7 | bellard | int trig = 0; |
568 | 1d14ffa9 | bellard | if (ioctl (fd, SNDCTL_DSP_SETTRIGGER, &trig) < 0) { |
569 | 1d14ffa9 | bellard | oss_logerr (errno, "SNDCTL_DSP_SETTRIGGER 0 failed\n");
|
570 | 85571bc7 | bellard | } |
571 | 44a095a7 | bellard | else {
|
572 | 44a095a7 | bellard | trig = PCM_ENABLE_OUTPUT; |
573 | 1d14ffa9 | bellard | if (ioctl (fd, SNDCTL_DSP_SETTRIGGER, &trig) < 0) { |
574 | 1d14ffa9 | bellard | oss_logerr ( |
575 | 1d14ffa9 | bellard | errno, |
576 | 1d14ffa9 | bellard | "SNDCTL_DSP_SETTRIGGER PCM_ENABLE_OUTPUT failed\n"
|
577 | 1d14ffa9 | bellard | ); |
578 | 44a095a7 | bellard | } |
579 | 44a095a7 | bellard | else {
|
580 | 44a095a7 | bellard | oss->mmapped = 1;
|
581 | 44a095a7 | bellard | } |
582 | 85571bc7 | bellard | } |
583 | 85571bc7 | bellard | |
584 | 44a095a7 | bellard | if (!oss->mmapped) {
|
585 | c0fe3827 | bellard | err = munmap (oss->pcm_buf, hw->samples << hw->info.shift); |
586 | 44a095a7 | bellard | if (err) {
|
587 | 1d14ffa9 | bellard | oss_logerr (errno, "Failed to unmap buffer %p size %d\n",
|
588 | c0fe3827 | bellard | oss->pcm_buf, hw->samples << hw->info.shift); |
589 | 44a095a7 | bellard | } |
590 | 85571bc7 | bellard | } |
591 | 85571bc7 | bellard | } |
592 | 85571bc7 | bellard | } |
593 | 85571bc7 | bellard | |
594 | 85571bc7 | bellard | if (!oss->mmapped) {
|
595 | c0fe3827 | bellard | oss->pcm_buf = audio_calloc ( |
596 | c0fe3827 | bellard | AUDIO_FUNC, |
597 | c0fe3827 | bellard | hw->samples, |
598 | c0fe3827 | bellard | 1 << hw->info.shift
|
599 | c0fe3827 | bellard | ); |
600 | 85571bc7 | bellard | if (!oss->pcm_buf) {
|
601 | b41cffbe | bellard | dolog ( |
602 | b41cffbe | bellard | "Could not allocate DAC buffer (%d samples, each %d bytes)\n",
|
603 | b41cffbe | bellard | hw->samples, |
604 | b41cffbe | bellard | 1 << hw->info.shift
|
605 | b41cffbe | bellard | ); |
606 | 1d14ffa9 | bellard | oss_anal_close (&fd); |
607 | 85571bc7 | bellard | return -1; |
608 | 85571bc7 | bellard | } |
609 | 85571bc7 | bellard | } |
610 | 85571bc7 | bellard | |
611 | 1d14ffa9 | bellard | oss->fd = fd; |
612 | 85571bc7 | bellard | return 0; |
613 | 85571bc7 | bellard | } |
614 | 85571bc7 | bellard | |
615 | 1d14ffa9 | bellard | static int oss_ctl_out (HWVoiceOut *hw, int cmd, ...) |
616 | 85571bc7 | bellard | { |
617 | 85571bc7 | bellard | int trig;
|
618 | 1d14ffa9 | bellard | OSSVoiceOut *oss = (OSSVoiceOut *) hw; |
619 | 85571bc7 | bellard | |
620 | 85571bc7 | bellard | switch (cmd) {
|
621 | 85571bc7 | bellard | case VOICE_ENABLE:
|
622 | 301901b5 | malc | { |
623 | 301901b5 | malc | va_list ap; |
624 | 301901b5 | malc | int poll_mode;
|
625 | dd8a5649 | malc | |
626 | 301901b5 | malc | va_start (ap, cmd); |
627 | 301901b5 | malc | poll_mode = va_arg (ap, int);
|
628 | 301901b5 | malc | va_end (ap); |
629 | dd8a5649 | malc | |
630 | 301901b5 | malc | ldebug ("enabling voice\n");
|
631 | 301901b5 | malc | if (poll_mode && oss_poll_out (hw)) {
|
632 | 301901b5 | malc | poll_mode = 0;
|
633 | 301901b5 | malc | } |
634 | 301901b5 | malc | hw->poll_mode = poll_mode; |
635 | 301901b5 | malc | |
636 | 301901b5 | malc | if (!oss->mmapped) {
|
637 | 301901b5 | malc | return 0; |
638 | 301901b5 | malc | } |
639 | 301901b5 | malc | |
640 | 301901b5 | malc | audio_pcm_info_clear_buf (&hw->info, oss->pcm_buf, hw->samples); |
641 | 301901b5 | malc | trig = PCM_ENABLE_OUTPUT; |
642 | 301901b5 | malc | if (ioctl (oss->fd, SNDCTL_DSP_SETTRIGGER, &trig) < 0) { |
643 | 301901b5 | malc | oss_logerr ( |
644 | 301901b5 | malc | errno, |
645 | 301901b5 | malc | "SNDCTL_DSP_SETTRIGGER PCM_ENABLE_OUTPUT failed\n"
|
646 | 301901b5 | malc | ); |
647 | 301901b5 | malc | return -1; |
648 | 301901b5 | malc | } |
649 | 85571bc7 | bellard | } |
650 | 85571bc7 | bellard | break;
|
651 | 85571bc7 | bellard | |
652 | 85571bc7 | bellard | case VOICE_DISABLE:
|
653 | dd8a5649 | malc | if (hw->poll_mode) {
|
654 | dd8a5649 | malc | qemu_set_fd_handler (oss->fd, NULL, NULL, NULL); |
655 | dd8a5649 | malc | hw->poll_mode = 0;
|
656 | dd8a5649 | malc | } |
657 | dd8a5649 | malc | |
658 | dd8a5649 | malc | if (!oss->mmapped) {
|
659 | dd8a5649 | malc | return 0; |
660 | dd8a5649 | malc | } |
661 | dd8a5649 | malc | |
662 | 85571bc7 | bellard | ldebug ("disabling voice\n");
|
663 | 85571bc7 | bellard | trig = 0;
|
664 | 85571bc7 | bellard | if (ioctl (oss->fd, SNDCTL_DSP_SETTRIGGER, &trig) < 0) { |
665 | 1d14ffa9 | bellard | oss_logerr (errno, "SNDCTL_DSP_SETTRIGGER 0 failed\n");
|
666 | 85571bc7 | bellard | return -1; |
667 | 85571bc7 | bellard | } |
668 | 85571bc7 | bellard | break;
|
669 | 85571bc7 | bellard | } |
670 | 85571bc7 | bellard | return 0; |
671 | 85571bc7 | bellard | } |
672 | 85571bc7 | bellard | |
673 | 1ea879e5 | malc | static int oss_init_in (HWVoiceIn *hw, struct audsettings *as) |
674 | 1d14ffa9 | bellard | { |
675 | 1d14ffa9 | bellard | OSSVoiceIn *oss = (OSSVoiceIn *) hw; |
676 | 1d14ffa9 | bellard | struct oss_params req, obt;
|
677 | 1d14ffa9 | bellard | int endianness;
|
678 | 1d14ffa9 | bellard | int err;
|
679 | 1d14ffa9 | bellard | int fd;
|
680 | 1d14ffa9 | bellard | audfmt_e effective_fmt; |
681 | 1ea879e5 | malc | struct audsettings obt_as;
|
682 | 1d14ffa9 | bellard | |
683 | 571ec3d6 | bellard | oss->fd = -1;
|
684 | 571ec3d6 | bellard | |
685 | c0fe3827 | bellard | req.fmt = aud_to_ossfmt (as->fmt); |
686 | c0fe3827 | bellard | req.freq = as->freq; |
687 | c0fe3827 | bellard | req.nchannels = as->nchannels; |
688 | 1d14ffa9 | bellard | req.fragsize = conf.fragsize; |
689 | 1d14ffa9 | bellard | req.nfrags = conf.nfrags; |
690 | 1d14ffa9 | bellard | if (oss_open (1, &req, &obt, &fd)) { |
691 | 1d14ffa9 | bellard | return -1; |
692 | 1d14ffa9 | bellard | } |
693 | 1d14ffa9 | bellard | |
694 | 1d14ffa9 | bellard | err = oss_to_audfmt (obt.fmt, &effective_fmt, &endianness); |
695 | 1d14ffa9 | bellard | if (err) {
|
696 | 1d14ffa9 | bellard | oss_anal_close (&fd); |
697 | 1d14ffa9 | bellard | return -1; |
698 | 1d14ffa9 | bellard | } |
699 | 1d14ffa9 | bellard | |
700 | c0fe3827 | bellard | obt_as.freq = obt.freq; |
701 | c0fe3827 | bellard | obt_as.nchannels = obt.nchannels; |
702 | c0fe3827 | bellard | obt_as.fmt = effective_fmt; |
703 | d929eba5 | bellard | obt_as.endianness = endianness; |
704 | c0fe3827 | bellard | |
705 | d929eba5 | bellard | audio_pcm_init_info (&hw->info, &obt_as); |
706 | 1d14ffa9 | bellard | oss->nfrags = obt.nfrags; |
707 | 1d14ffa9 | bellard | oss->fragsize = obt.fragsize; |
708 | c0fe3827 | bellard | |
709 | c0fe3827 | bellard | if (obt.nfrags * obt.fragsize & hw->info.align) {
|
710 | c0fe3827 | bellard | dolog ("warning: Misaligned ADC buffer, size %d, alignment %d\n",
|
711 | c0fe3827 | bellard | obt.nfrags * obt.fragsize, hw->info.align + 1);
|
712 | c0fe3827 | bellard | } |
713 | c0fe3827 | bellard | |
714 | c0fe3827 | bellard | hw->samples = (obt.nfrags * obt.fragsize) >> hw->info.shift; |
715 | c0fe3827 | bellard | oss->pcm_buf = audio_calloc (AUDIO_FUNC, hw->samples, 1 << hw->info.shift);
|
716 | 1d14ffa9 | bellard | if (!oss->pcm_buf) {
|
717 | b41cffbe | bellard | dolog ("Could not allocate ADC buffer (%d samples, each %d bytes)\n",
|
718 | b41cffbe | bellard | hw->samples, 1 << hw->info.shift);
|
719 | 1d14ffa9 | bellard | oss_anal_close (&fd); |
720 | 1d14ffa9 | bellard | return -1; |
721 | 1d14ffa9 | bellard | } |
722 | 1d14ffa9 | bellard | |
723 | 1d14ffa9 | bellard | oss->fd = fd; |
724 | 1d14ffa9 | bellard | return 0; |
725 | 1d14ffa9 | bellard | } |
726 | 1d14ffa9 | bellard | |
727 | 1d14ffa9 | bellard | static void oss_fini_in (HWVoiceIn *hw) |
728 | 1d14ffa9 | bellard | { |
729 | 1d14ffa9 | bellard | OSSVoiceIn *oss = (OSSVoiceIn *) hw; |
730 | 1d14ffa9 | bellard | |
731 | 1d14ffa9 | bellard | oss_anal_close (&oss->fd); |
732 | 1d14ffa9 | bellard | |
733 | 1d14ffa9 | bellard | if (oss->pcm_buf) {
|
734 | 1d14ffa9 | bellard | qemu_free (oss->pcm_buf); |
735 | 1d14ffa9 | bellard | oss->pcm_buf = NULL;
|
736 | 1d14ffa9 | bellard | } |
737 | 1d14ffa9 | bellard | } |
738 | 1d14ffa9 | bellard | |
739 | 1d14ffa9 | bellard | static int oss_run_in (HWVoiceIn *hw) |
740 | 1d14ffa9 | bellard | { |
741 | 1d14ffa9 | bellard | OSSVoiceIn *oss = (OSSVoiceIn *) hw; |
742 | 1d14ffa9 | bellard | int hwshift = hw->info.shift;
|
743 | 1d14ffa9 | bellard | int i;
|
744 | 1d14ffa9 | bellard | int live = audio_pcm_hw_get_live_in (hw);
|
745 | 1d14ffa9 | bellard | int dead = hw->samples - live;
|
746 | 1d14ffa9 | bellard | size_t read_samples = 0;
|
747 | 1d14ffa9 | bellard | struct {
|
748 | 1d14ffa9 | bellard | int add;
|
749 | 1d14ffa9 | bellard | int len;
|
750 | 1d14ffa9 | bellard | } bufs[2] = {
|
751 | 98f9f48c | malc | { .add = hw->wpos, .len = 0 },
|
752 | 98f9f48c | malc | { .add = 0, .len = 0 } |
753 | 1d14ffa9 | bellard | }; |
754 | 1d14ffa9 | bellard | |
755 | 1d14ffa9 | bellard | if (!dead) {
|
756 | 1d14ffa9 | bellard | return 0; |
757 | 1d14ffa9 | bellard | } |
758 | 1d14ffa9 | bellard | |
759 | 1d14ffa9 | bellard | if (hw->wpos + dead > hw->samples) {
|
760 | 1d14ffa9 | bellard | bufs[0].len = (hw->samples - hw->wpos) << hwshift;
|
761 | 1d14ffa9 | bellard | bufs[1].len = (dead - (hw->samples - hw->wpos)) << hwshift;
|
762 | 1d14ffa9 | bellard | } |
763 | 1d14ffa9 | bellard | else {
|
764 | 1d14ffa9 | bellard | bufs[0].len = dead << hwshift;
|
765 | 1d14ffa9 | bellard | } |
766 | 1d14ffa9 | bellard | |
767 | 1d14ffa9 | bellard | for (i = 0; i < 2; ++i) { |
768 | 1d14ffa9 | bellard | ssize_t nread; |
769 | 1d14ffa9 | bellard | |
770 | 1d14ffa9 | bellard | if (bufs[i].len) {
|
771 | 1d14ffa9 | bellard | void *p = advance (oss->pcm_buf, bufs[i].add << hwshift);
|
772 | 1d14ffa9 | bellard | nread = read (oss->fd, p, bufs[i].len); |
773 | 1d14ffa9 | bellard | |
774 | 1d14ffa9 | bellard | if (nread > 0) { |
775 | 1d14ffa9 | bellard | if (nread & hw->info.align) {
|
776 | b41cffbe | bellard | dolog ("warning: Misaligned read %zd (requested %d), "
|
777 | 1d14ffa9 | bellard | "alignment %d\n", nread, bufs[i].add << hwshift,
|
778 | 1d14ffa9 | bellard | hw->info.align + 1);
|
779 | 1d14ffa9 | bellard | } |
780 | 1d14ffa9 | bellard | read_samples += nread >> hwshift; |
781 | 1d14ffa9 | bellard | hw->conv (hw->conv_buf + bufs[i].add, p, nread >> hwshift, |
782 | 1d14ffa9 | bellard | &nominal_volume); |
783 | 1d14ffa9 | bellard | } |
784 | 1d14ffa9 | bellard | |
785 | 1d14ffa9 | bellard | if (bufs[i].len - nread) {
|
786 | 1d14ffa9 | bellard | if (nread == -1) { |
787 | 1d14ffa9 | bellard | switch (errno) {
|
788 | 1d14ffa9 | bellard | case EINTR:
|
789 | 1d14ffa9 | bellard | case EAGAIN:
|
790 | 1d14ffa9 | bellard | break;
|
791 | 1d14ffa9 | bellard | default:
|
792 | 1d14ffa9 | bellard | oss_logerr ( |
793 | 1d14ffa9 | bellard | errno, |
794 | 1d14ffa9 | bellard | "Failed to read %d bytes of audio (to %p)\n",
|
795 | 1d14ffa9 | bellard | bufs[i].len, p |
796 | 1d14ffa9 | bellard | ); |
797 | 1d14ffa9 | bellard | break;
|
798 | 1d14ffa9 | bellard | } |
799 | 1d14ffa9 | bellard | } |
800 | 1d14ffa9 | bellard | break;
|
801 | 1d14ffa9 | bellard | } |
802 | 1d14ffa9 | bellard | } |
803 | 1d14ffa9 | bellard | } |
804 | 1d14ffa9 | bellard | |
805 | 1d14ffa9 | bellard | hw->wpos = (hw->wpos + read_samples) % hw->samples; |
806 | 1d14ffa9 | bellard | return read_samples;
|
807 | 1d14ffa9 | bellard | } |
808 | 1d14ffa9 | bellard | |
809 | 1d14ffa9 | bellard | static int oss_read (SWVoiceIn *sw, void *buf, int size) |
810 | 1d14ffa9 | bellard | { |
811 | 1d14ffa9 | bellard | return audio_pcm_sw_read (sw, buf, size);
|
812 | 1d14ffa9 | bellard | } |
813 | 1d14ffa9 | bellard | |
814 | 1d14ffa9 | bellard | static int oss_ctl_in (HWVoiceIn *hw, int cmd, ...) |
815 | 1d14ffa9 | bellard | { |
816 | dd8a5649 | malc | OSSVoiceIn *oss = (OSSVoiceIn *) hw; |
817 | dd8a5649 | malc | |
818 | dd8a5649 | malc | switch (cmd) {
|
819 | dd8a5649 | malc | case VOICE_ENABLE:
|
820 | a628b869 | malc | { |
821 | a628b869 | malc | va_list ap; |
822 | a628b869 | malc | int poll_mode;
|
823 | a628b869 | malc | |
824 | a628b869 | malc | va_start (ap, cmd); |
825 | a628b869 | malc | poll_mode = va_arg (ap, int);
|
826 | a628b869 | malc | va_end (ap); |
827 | a628b869 | malc | |
828 | a628b869 | malc | if (poll_mode && oss_poll_in (hw)) {
|
829 | a628b869 | malc | poll_mode = 0;
|
830 | a628b869 | malc | } |
831 | a628b869 | malc | hw->poll_mode = poll_mode; |
832 | dd8a5649 | malc | } |
833 | dd8a5649 | malc | break;
|
834 | dd8a5649 | malc | |
835 | dd8a5649 | malc | case VOICE_DISABLE:
|
836 | dd8a5649 | malc | if (hw->poll_mode) {
|
837 | dd8a5649 | malc | hw->poll_mode = 0;
|
838 | dd8a5649 | malc | qemu_set_fd_handler (oss->fd, NULL, NULL, NULL); |
839 | dd8a5649 | malc | } |
840 | dd8a5649 | malc | break;
|
841 | dd8a5649 | malc | } |
842 | 1d14ffa9 | bellard | return 0; |
843 | 1d14ffa9 | bellard | } |
844 | 1d14ffa9 | bellard | |
845 | 85571bc7 | bellard | static void *oss_audio_init (void) |
846 | 85571bc7 | bellard | { |
847 | 85571bc7 | bellard | return &conf;
|
848 | 85571bc7 | bellard | } |
849 | 85571bc7 | bellard | |
850 | 85571bc7 | bellard | static void oss_audio_fini (void *opaque) |
851 | 85571bc7 | bellard | { |
852 | 1d14ffa9 | bellard | (void) opaque;
|
853 | 85571bc7 | bellard | } |
854 | 85571bc7 | bellard | |
855 | 1d14ffa9 | bellard | static struct audio_option oss_options[] = { |
856 | 98f9f48c | malc | { |
857 | 98f9f48c | malc | .name = "FRAGSIZE",
|
858 | 98f9f48c | malc | .tag = AUD_OPT_INT, |
859 | 98f9f48c | malc | .valp = &conf.fragsize, |
860 | 98f9f48c | malc | .descr = "Fragment size in bytes"
|
861 | 98f9f48c | malc | }, |
862 | 98f9f48c | malc | { |
863 | 98f9f48c | malc | .name = "NFRAGS",
|
864 | 98f9f48c | malc | .tag = AUD_OPT_INT, |
865 | 98f9f48c | malc | .valp = &conf.nfrags, |
866 | 98f9f48c | malc | .descr = "Number of fragments"
|
867 | 98f9f48c | malc | }, |
868 | 98f9f48c | malc | { |
869 | 98f9f48c | malc | .name = "MMAP",
|
870 | 98f9f48c | malc | .tag = AUD_OPT_BOOL, |
871 | 98f9f48c | malc | .valp = &conf.try_mmap, |
872 | 98f9f48c | malc | .descr = "Try using memory mapped access"
|
873 | 98f9f48c | malc | }, |
874 | 98f9f48c | malc | { |
875 | 98f9f48c | malc | .name = "DAC_DEV",
|
876 | 98f9f48c | malc | .tag = AUD_OPT_STR, |
877 | 98f9f48c | malc | .valp = &conf.devpath_out, |
878 | 98f9f48c | malc | .descr = "Path to DAC device"
|
879 | 98f9f48c | malc | }, |
880 | 98f9f48c | malc | { |
881 | 98f9f48c | malc | .name = "ADC_DEV",
|
882 | 98f9f48c | malc | .tag = AUD_OPT_STR, |
883 | 98f9f48c | malc | .valp = &conf.devpath_in, |
884 | 98f9f48c | malc | .descr = "Path to ADC device"
|
885 | 98f9f48c | malc | }, |
886 | 98f9f48c | malc | { |
887 | 0b3652bc | malc | .name = "EXCLUSIVE",
|
888 | 0b3652bc | malc | .tag = AUD_OPT_BOOL, |
889 | 0b3652bc | malc | .valp = &conf.exclusive, |
890 | 0b3652bc | malc | .descr = "Open device in exclusive mode (vmix wont work)"
|
891 | 0b3652bc | malc | }, |
892 | 78d9356d | malc | #ifdef USE_DSP_POLICY
|
893 | 0b3652bc | malc | { |
894 | 0b3652bc | malc | .name = "POLICY",
|
895 | 0b3652bc | malc | .tag = AUD_OPT_INT, |
896 | 0b3652bc | malc | .valp = &conf.policy, |
897 | 0b3652bc | malc | .descr = "Set the timing policy of the device, -1 to use fragment mode",
|
898 | 0b3652bc | malc | }, |
899 | 0b3652bc | malc | #endif
|
900 | 0b3652bc | malc | { |
901 | 98f9f48c | malc | .name = "DEBUG",
|
902 | 98f9f48c | malc | .tag = AUD_OPT_BOOL, |
903 | 98f9f48c | malc | .valp = &conf.debug, |
904 | 98f9f48c | malc | .descr = "Turn on some debugging messages"
|
905 | 98f9f48c | malc | }, |
906 | 2700efa3 | Juan Quintela | { /* End of list */ }
|
907 | 1d14ffa9 | bellard | }; |
908 | 1d14ffa9 | bellard | |
909 | 35f4b58c | blueswir1 | static struct audio_pcm_ops oss_pcm_ops = { |
910 | 1dd3e4d1 | Juan Quintela | .init_out = oss_init_out, |
911 | 1dd3e4d1 | Juan Quintela | .fini_out = oss_fini_out, |
912 | 1dd3e4d1 | Juan Quintela | .run_out = oss_run_out, |
913 | 1dd3e4d1 | Juan Quintela | .write = oss_write, |
914 | 1dd3e4d1 | Juan Quintela | .ctl_out = oss_ctl_out, |
915 | 1dd3e4d1 | Juan Quintela | |
916 | 1dd3e4d1 | Juan Quintela | .init_in = oss_init_in, |
917 | 1dd3e4d1 | Juan Quintela | .fini_in = oss_fini_in, |
918 | 1dd3e4d1 | Juan Quintela | .run_in = oss_run_in, |
919 | 1dd3e4d1 | Juan Quintela | .read = oss_read, |
920 | 1dd3e4d1 | Juan Quintela | .ctl_in = oss_ctl_in |
921 | 85571bc7 | bellard | }; |
922 | 85571bc7 | bellard | |
923 | 1d14ffa9 | bellard | struct audio_driver oss_audio_driver = {
|
924 | bee37f32 | Juan Quintela | .name = "oss",
|
925 | bee37f32 | Juan Quintela | .descr = "OSS http://www.opensound.com",
|
926 | bee37f32 | Juan Quintela | .options = oss_options, |
927 | bee37f32 | Juan Quintela | .init = oss_audio_init, |
928 | bee37f32 | Juan Quintela | .fini = oss_audio_fini, |
929 | bee37f32 | Juan Quintela | .pcm_ops = &oss_pcm_ops, |
930 | bee37f32 | Juan Quintela | .can_be_default = 1,
|
931 | bee37f32 | Juan Quintela | .max_voices_out = INT_MAX, |
932 | bee37f32 | Juan Quintela | .max_voices_in = INT_MAX, |
933 | bee37f32 | Juan Quintela | .voice_size_out = sizeof (OSSVoiceOut),
|
934 | bee37f32 | Juan Quintela | .voice_size_in = sizeof (OSSVoiceIn)
|
935 | 85571bc7 | bellard | }; |