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