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 "VBoxDD.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 [email protected]\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 | # ifndef VBOX /* -Wshadow */
|
---|
580 | int err;
|
---|
581 | # endif
|
---|
582 | int trig = 0;
|
---|
583 | if (ioctl (fd, SNDCTL_DSP_SETTRIGGER, &trig) < 0) {
|
---|
584 | oss_logerr (errno, "SNDCTL_DSP_SETTRIGGER 0 failed\n");
|
---|
585 | }
|
---|
586 | else {
|
---|
587 | trig = PCM_ENABLE_OUTPUT;
|
---|
588 | if (ioctl (fd, SNDCTL_DSP_SETTRIGGER, &trig) < 0) {
|
---|
589 | oss_logerr (
|
---|
590 | errno,
|
---|
591 | "SNDCTL_DSP_SETTRIGGER PCM_ENABLE_OUTPUT failed\n"
|
---|
592 | );
|
---|
593 | }
|
---|
594 | else {
|
---|
595 | oss->mmapped = 1;
|
---|
596 | }
|
---|
597 | }
|
---|
598 |
|
---|
599 | if (!oss->mmapped) {
|
---|
600 | err = munmap (oss->pcm_buf, hw->samples << hw->info.shift);
|
---|
601 | if (err) {
|
---|
602 | oss_logerr (errno, "Failed to unmap buffer %p size %d\n",
|
---|
603 | oss->pcm_buf, hw->samples << hw->info.shift);
|
---|
604 | }
|
---|
605 | }
|
---|
606 | }
|
---|
607 | }
|
---|
608 | #endif
|
---|
609 |
|
---|
610 | #ifndef RT_OS_L4
|
---|
611 | if (!oss->mmapped) {
|
---|
612 | #endif
|
---|
613 | oss->pcm_buf = audio_calloc (
|
---|
614 | AUDIO_FUNC,
|
---|
615 | hw->samples,
|
---|
616 | 1 << hw->info.shift
|
---|
617 | );
|
---|
618 | if (!oss->pcm_buf) {
|
---|
619 | dolog (
|
---|
620 | "Could not allocate DAC buffer (%d samples, each %d bytes)\n",
|
---|
621 | hw->samples,
|
---|
622 | 1 << hw->info.shift
|
---|
623 | );
|
---|
624 | oss_anal_close (&fd);
|
---|
625 | #ifdef VBOX
|
---|
626 | LogRel(("OSS: Closed %s for DAC\n", conf.devpath_out));
|
---|
627 | #endif
|
---|
628 | return -1;
|
---|
629 | }
|
---|
630 | #ifndef RT_OS_L4
|
---|
631 | }
|
---|
632 | #endif
|
---|
633 |
|
---|
634 | oss->fd = fd;
|
---|
635 | return 0;
|
---|
636 | }
|
---|
637 |
|
---|
638 | static int oss_ctl_out (HWVoiceOut *hw, int cmd, ...)
|
---|
639 | {
|
---|
640 | int trig;
|
---|
641 | OSSVoiceOut *oss = (OSSVoiceOut *) hw;
|
---|
642 |
|
---|
643 | #ifdef RT_OS_L4
|
---|
644 | return 0;
|
---|
645 | #else
|
---|
646 | if (!oss->mmapped) {
|
---|
647 | return 0;
|
---|
648 | }
|
---|
649 | #endif
|
---|
650 |
|
---|
651 | switch (cmd) {
|
---|
652 | case VOICE_ENABLE:
|
---|
653 | ldebug ("enabling voice\n");
|
---|
654 | audio_pcm_info_clear_buf (&hw->info, oss->pcm_buf, hw->samples);
|
---|
655 | trig = PCM_ENABLE_OUTPUT;
|
---|
656 | if (ioctl (oss->fd, SNDCTL_DSP_SETTRIGGER, &trig) < 0) {
|
---|
657 | oss_logerr (
|
---|
658 | errno,
|
---|
659 | "SNDCTL_DSP_SETTRIGGER PCM_ENABLE_OUTPUT failed\n"
|
---|
660 | );
|
---|
661 | return -1;
|
---|
662 | }
|
---|
663 | break;
|
---|
664 |
|
---|
665 | case VOICE_DISABLE:
|
---|
666 | ldebug ("disabling voice\n");
|
---|
667 | trig = 0;
|
---|
668 | if (ioctl (oss->fd, SNDCTL_DSP_SETTRIGGER, &trig) < 0) {
|
---|
669 | oss_logerr (errno, "SNDCTL_DSP_SETTRIGGER 0 failed\n");
|
---|
670 | return -1;
|
---|
671 | }
|
---|
672 | break;
|
---|
673 | }
|
---|
674 | return 0;
|
---|
675 | }
|
---|
676 |
|
---|
677 | static int oss_init_in (HWVoiceIn *hw, audsettings_t *as)
|
---|
678 | {
|
---|
679 | OSSVoiceIn *oss = (OSSVoiceIn *) hw;
|
---|
680 | struct oss_params req, obt;
|
---|
681 | int endianness;
|
---|
682 | int err;
|
---|
683 | int fd;
|
---|
684 | audfmt_e effective_fmt;
|
---|
685 | audsettings_t obt_as;
|
---|
686 |
|
---|
687 | oss->fd = -1;
|
---|
688 |
|
---|
689 | req.fmt = aud_to_ossfmt (as->fmt);
|
---|
690 | req.freq = as->freq;
|
---|
691 | req.nchannels = as->nchannels;
|
---|
692 | req.fragsize = conf.fragsize;
|
---|
693 | req.nfrags = conf.nfrags;
|
---|
694 | if (oss_open (1, &req, &obt, &fd)) {
|
---|
695 | return -1;
|
---|
696 | }
|
---|
697 |
|
---|
698 | err = oss_to_audfmt (obt.fmt, &effective_fmt, &endianness);
|
---|
699 | if (err) {
|
---|
700 | oss_anal_close (&fd);
|
---|
701 | #ifdef VBOX
|
---|
702 | LogRel(("OSS: Closed %s for ADC\n", conf.devpath_in));
|
---|
703 | #endif
|
---|
704 | return -1;
|
---|
705 | }
|
---|
706 |
|
---|
707 | obt_as.freq = obt.freq;
|
---|
708 | obt_as.nchannels = obt.nchannels;
|
---|
709 | obt_as.fmt = effective_fmt;
|
---|
710 | obt_as.endianness = endianness;
|
---|
711 |
|
---|
712 | audio_pcm_init_info (&hw->info, &obt_as);
|
---|
713 | oss->nfrags = obt.nfrags;
|
---|
714 | oss->fragsize = obt.fragsize;
|
---|
715 |
|
---|
716 | if (obt.nfrags * obt.fragsize & hw->info.align) {
|
---|
717 | dolog ("warning: Misaligned ADC buffer, size %d, alignment %d\n",
|
---|
718 | obt.nfrags * obt.fragsize, hw->info.align + 1);
|
---|
719 | }
|
---|
720 |
|
---|
721 | hw->samples = (obt.nfrags * obt.fragsize) >> hw->info.shift;
|
---|
722 | oss->pcm_buf = audio_calloc (AUDIO_FUNC, hw->samples, 1 << hw->info.shift);
|
---|
723 | if (!oss->pcm_buf) {
|
---|
724 | dolog ("Could not allocate ADC buffer (%d samples, each %d bytes)\n",
|
---|
725 | hw->samples, 1 << hw->info.shift);
|
---|
726 | oss_anal_close (&fd);
|
---|
727 | #ifdef VBOX
|
---|
728 | LogRel(("OSS: Closed %s for ADC\n", conf.devpath_in));
|
---|
729 | #endif
|
---|
730 | return -1;
|
---|
731 | }
|
---|
732 |
|
---|
733 | oss->fd = fd;
|
---|
734 | return 0;
|
---|
735 | }
|
---|
736 |
|
---|
737 | static void oss_fini_in (HWVoiceIn *hw)
|
---|
738 | {
|
---|
739 | OSSVoiceIn *oss = (OSSVoiceIn *) hw;
|
---|
740 |
|
---|
741 | oss_anal_close (&oss->fd);
|
---|
742 | #ifdef VBOX
|
---|
743 | LogRel(("OSS: Closed %s for ADC\n", conf.devpath_in));
|
---|
744 | #endif
|
---|
745 |
|
---|
746 | if (oss->pcm_buf) {
|
---|
747 | qemu_free (oss->pcm_buf);
|
---|
748 | oss->pcm_buf = NULL;
|
---|
749 | }
|
---|
750 | }
|
---|
751 |
|
---|
752 | static int oss_run_in (HWVoiceIn *hw)
|
---|
753 | {
|
---|
754 | OSSVoiceIn *oss = (OSSVoiceIn *) hw;
|
---|
755 | int hwshift = hw->info.shift;
|
---|
756 | int i;
|
---|
757 | int live = audio_pcm_hw_get_live_in (hw);
|
---|
758 | int dead = hw->samples - live;
|
---|
759 | size_t read_samples = 0;
|
---|
760 | struct {
|
---|
761 | int add;
|
---|
762 | int len;
|
---|
763 | } bufs[2];
|
---|
764 |
|
---|
765 | bufs[0].add = hw->wpos;
|
---|
766 | bufs[0].len = 0;
|
---|
767 | bufs[1].add = 0;
|
---|
768 | bufs[1].len = 0;
|
---|
769 |
|
---|
770 | if (!dead) {
|
---|
771 | return 0;
|
---|
772 | }
|
---|
773 |
|
---|
774 | if (hw->wpos + dead > hw->samples) {
|
---|
775 | bufs[0].len = (hw->samples - hw->wpos) << hwshift;
|
---|
776 | bufs[1].len = (dead - (hw->samples - hw->wpos)) << hwshift;
|
---|
777 | }
|
---|
778 | else {
|
---|
779 | bufs[0].len = dead << hwshift;
|
---|
780 | }
|
---|
781 |
|
---|
782 |
|
---|
783 | for (i = 0; i < 2; ++i) {
|
---|
784 | ssize_t nread;
|
---|
785 |
|
---|
786 | if (bufs[i].len) {
|
---|
787 | void *p = advance (oss->pcm_buf, bufs[i].add << hwshift);
|
---|
788 | nread = read (oss->fd, p, bufs[i].len);
|
---|
789 |
|
---|
790 | if (nread > 0) {
|
---|
791 | if (nread & hw->info.align) {
|
---|
792 | dolog ("warning: Misaligned read %"
|
---|
793 | FMTZ "d (requested %d), "
|
---|
794 | "alignment %d\n", nread, bufs[i].add << hwshift,
|
---|
795 | hw->info.align + 1);
|
---|
796 | }
|
---|
797 | read_samples += nread >> hwshift;
|
---|
798 | #ifndef VBOX
|
---|
799 | hw->conv (hw->conv_buf + bufs[i].add, p, nread >> hwshift,
|
---|
800 | &nominal_volume);
|
---|
801 | #else
|
---|
802 | hw->conv (hw->conv_buf + bufs[i].add, p, nread >> hwshift,
|
---|
803 | &pcm_in_volume);
|
---|
804 | #endif
|
---|
805 | }
|
---|
806 |
|
---|
807 | if (bufs[i].len - nread) {
|
---|
808 | if (nread == -1) {
|
---|
809 | switch (errno) {
|
---|
810 | case EINTR:
|
---|
811 | case EAGAIN:
|
---|
812 | break;
|
---|
813 | default:
|
---|
814 | oss_logerr (
|
---|
815 | errno,
|
---|
816 | "Failed to read %d bytes of audio (to %p)\n",
|
---|
817 | bufs[i].len, p
|
---|
818 | );
|
---|
819 | break;
|
---|
820 | }
|
---|
821 | }
|
---|
822 | break;
|
---|
823 | }
|
---|
824 | }
|
---|
825 | }
|
---|
826 |
|
---|
827 | hw->wpos = (hw->wpos + read_samples) % hw->samples;
|
---|
828 | return read_samples;
|
---|
829 | }
|
---|
830 |
|
---|
831 | static int oss_read (SWVoiceIn *sw, void *buf, int size)
|
---|
832 | {
|
---|
833 | return audio_pcm_sw_read (sw, buf, size);
|
---|
834 | }
|
---|
835 |
|
---|
836 | static int oss_ctl_in (HWVoiceIn *hw, int cmd, ...)
|
---|
837 | {
|
---|
838 | (void) hw;
|
---|
839 | (void) cmd;
|
---|
840 | return 0;
|
---|
841 | }
|
---|
842 |
|
---|
843 | static void *oss_audio_init (void)
|
---|
844 | {
|
---|
845 | return &conf;
|
---|
846 | }
|
---|
847 |
|
---|
848 | static void oss_audio_fini (void *opaque)
|
---|
849 | {
|
---|
850 | (void) opaque;
|
---|
851 | }
|
---|
852 |
|
---|
853 | static struct audio_option oss_options[] = {
|
---|
854 | {"FRAGSIZE", AUD_OPT_INT, &conf.fragsize,
|
---|
855 | "Fragment size in bytes", NULL, 0},
|
---|
856 | {"NFRAGS", AUD_OPT_INT, &conf.nfrags,
|
---|
857 | "Number of fragments", NULL, 0},
|
---|
858 | #ifndef RT_OS_L4
|
---|
859 | {"MMAP", AUD_OPT_BOOL, &conf.try_mmap,
|
---|
860 | "Try using memory mapped access", NULL, 0},
|
---|
861 | #endif
|
---|
862 | {"DAC_DEV", AUD_OPT_STR, &conf.devpath_out,
|
---|
863 | "Path to DAC device", NULL, 0},
|
---|
864 | {"ADC_DEV", AUD_OPT_STR, &conf.devpath_in,
|
---|
865 | "Path to ADC device", NULL, 0},
|
---|
866 | {"DEBUG", AUD_OPT_BOOL, &conf.debug,
|
---|
867 | "Turn on some debugging messages", NULL, 0},
|
---|
868 | {NULL, 0, NULL, NULL, NULL, 0}
|
---|
869 | };
|
---|
870 |
|
---|
871 | static struct audio_pcm_ops oss_pcm_ops = {
|
---|
872 | oss_init_out,
|
---|
873 | oss_fini_out,
|
---|
874 | oss_run_out,
|
---|
875 | oss_write,
|
---|
876 | oss_ctl_out,
|
---|
877 |
|
---|
878 | oss_init_in,
|
---|
879 | oss_fini_in,
|
---|
880 | oss_run_in,
|
---|
881 | oss_read,
|
---|
882 | oss_ctl_in
|
---|
883 | };
|
---|
884 |
|
---|
885 | struct audio_driver oss_audio_driver = {
|
---|
886 | INIT_FIELD (name = ) "oss",
|
---|
887 | INIT_FIELD (descr = ) "OSS http://www.opensound.com",
|
---|
888 | INIT_FIELD (options = ) oss_options,
|
---|
889 | INIT_FIELD (init = ) oss_audio_init,
|
---|
890 | INIT_FIELD (fini = ) oss_audio_fini,
|
---|
891 | INIT_FIELD (pcm_ops = ) &oss_pcm_ops,
|
---|
892 | INIT_FIELD (can_be_default = ) 1,
|
---|
893 | INIT_FIELD (max_voices_out = ) INT_MAX,
|
---|
894 | INIT_FIELD (max_voices_in = ) INT_MAX,
|
---|
895 | INIT_FIELD (voice_size_out = ) sizeof (OSSVoiceOut),
|
---|
896 | INIT_FIELD (voice_size_in = ) sizeof (OSSVoiceIn)
|
---|
897 | };
|
---|