]> git.proxmox.com Git - qemu.git/blob - audio/ossaudio.c
merged 15a_aqemu.patch audio patch (malc)
[qemu.git] / audio / ossaudio.c
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 #include <sys/mman.h>
25 #include <sys/types.h>
26 #include <sys/ioctl.h>
27 #include <sys/soundcard.h>
28 #include "vl.h"
29
30 #define AUDIO_CAP "oss"
31 #include "audio_int.h"
32
33 typedef struct OSSVoiceOut {
34 HWVoiceOut hw;
35 void *pcm_buf;
36 int fd;
37 int nfrags;
38 int fragsize;
39 int mmapped;
40 int old_optr;
41 } OSSVoiceOut;
42
43 typedef struct OSSVoiceIn {
44 HWVoiceIn hw;
45 void *pcm_buf;
46 int fd;
47 int nfrags;
48 int fragsize;
49 int old_optr;
50 } OSSVoiceIn;
51
52 static struct {
53 int try_mmap;
54 int nfrags;
55 int fragsize;
56 const char *devpath_out;
57 const char *devpath_in;
58 } conf = {
59 .try_mmap = 0,
60 .nfrags = 4,
61 .fragsize = 4096,
62 .devpath_out = "/dev/dsp",
63 .devpath_in = "/dev/dsp"
64 };
65
66 struct oss_params {
67 int freq;
68 audfmt_e fmt;
69 int nchannels;
70 int nfrags;
71 int fragsize;
72 };
73
74 static void GCC_FMT_ATTR (2, 3) oss_logerr (int err, const char *fmt, ...)
75 {
76 va_list ap;
77
78 AUD_vlog (AUDIO_CAP, fmt, ap);
79
80 va_start (ap, fmt);
81 AUD_log (AUDIO_CAP, "Reason: %s\n", strerror (err));
82 va_end (ap);
83 }
84
85 static void GCC_FMT_ATTR (3, 4) oss_logerr2 (
86 int err,
87 const char *typ,
88 const char *fmt,
89 ...
90 )
91 {
92 va_list ap;
93
94 AUD_log (AUDIO_CAP, "Can not initialize %s\n", typ);
95
96 va_start (ap, fmt);
97 AUD_vlog (AUDIO_CAP, fmt, ap);
98 va_end (ap);
99
100 AUD_log (AUDIO_CAP, "Reason: %s\n", strerror (err));
101 }
102
103 static void oss_anal_close (int *fdp)
104 {
105 int err = close (*fdp);
106 if (err) {
107 oss_logerr (errno, "Failed to close file(fd=%d)\n", *fdp);
108 }
109 *fdp = -1;
110 }
111
112 static int oss_write (SWVoiceOut *sw, void *buf, int len)
113 {
114 return audio_pcm_sw_write (sw, buf, len);
115 }
116
117 static int aud_to_ossfmt (audfmt_e fmt)
118 {
119 switch (fmt) {
120 case AUD_FMT_S8:
121 return AFMT_S8;
122
123 case AUD_FMT_U8:
124 return AFMT_U8;
125
126 case AUD_FMT_S16:
127 return AFMT_S16_LE;
128
129 case AUD_FMT_U16:
130 return AFMT_U16_LE;
131
132 default:
133 dolog ("Internal logic error: Bad audio format %d\n", fmt);
134 #ifdef DEBUG_AUDIO
135 abort ();
136 #endif
137 return AFMT_U8;
138 }
139 }
140
141 static int oss_to_audfmt (int ossfmt, audfmt_e *fmt, int *endianness)
142 {
143 switch (ossfmt) {
144 case AFMT_S8:
145 *endianness =0;
146 *fmt = AUD_FMT_S8;
147 break;
148
149 case AFMT_U8:
150 *endianness = 0;
151 *fmt = AUD_FMT_U8;
152 break;
153
154 case AFMT_S16_LE:
155 *endianness = 0;
156 *fmt = AUD_FMT_S16;
157 break;
158
159 case AFMT_U16_LE:
160 *endianness = 0;
161 *fmt = AUD_FMT_U16;
162 break;
163
164 case AFMT_S16_BE:
165 *endianness = 1;
166 *fmt = AUD_FMT_S16;
167 break;
168
169 case AFMT_U16_BE:
170 *endianness = 1;
171 *fmt = AUD_FMT_U16;
172 break;
173
174 default:
175 dolog ("Unrecognized audio format %d\n", ossfmt);
176 return -1;
177 }
178
179 return 0;
180 }
181
182 #ifdef DEBUG_MISMATCHES
183 static void oss_dump_info (struct oss_params *req, struct oss_params *obt)
184 {
185 dolog ("parameter | requested value | obtained value\n");
186 dolog ("format | %10d | %10d\n", req->fmt, obt->fmt);
187 dolog ("channels | %10d | %10d\n",
188 req->nchannels, obt->nchannels);
189 dolog ("frequency | %10d | %10d\n", req->freq, obt->freq);
190 dolog ("nfrags | %10d | %10d\n", req->nfrags, obt->nfrags);
191 dolog ("fragsize | %10d | %10d\n",
192 req->fragsize, obt->fragsize);
193 }
194 #endif
195
196 static int oss_open (int in, struct oss_params *req,
197 struct oss_params *obt, int *pfd)
198 {
199 int fd;
200 int mmmmssss;
201 audio_buf_info abinfo;
202 int fmt, freq, nchannels;
203 const char *dspname = in ? conf.devpath_in : conf.devpath_out;
204 const char *typ = in ? "ADC" : "DAC";
205
206 fd = open (dspname, (in ? O_RDONLY : O_WRONLY) | O_NONBLOCK);
207 if (-1 == fd) {
208 oss_logerr2 (errno, typ, "Failed to open `%s'\n", dspname);
209 return -1;
210 }
211
212 freq = req->freq;
213 nchannels = req->nchannels;
214 fmt = req->fmt;
215
216 if (ioctl (fd, SNDCTL_DSP_SAMPLESIZE, &fmt)) {
217 oss_logerr2 (errno, typ, "Failed to set sample size %d\n", req->fmt);
218 goto err;
219 }
220
221 if (ioctl (fd, SNDCTL_DSP_CHANNELS, &nchannels)) {
222 oss_logerr2 (errno, typ, "Failed to set number of channels %d\n",
223 req->nchannels);
224 goto err;
225 }
226
227 if (ioctl (fd, SNDCTL_DSP_SPEED, &freq)) {
228 oss_logerr2 (errno, typ, "Failed to set frequency %d\n", req->freq);
229 goto err;
230 }
231
232 if (ioctl (fd, SNDCTL_DSP_NONBLOCK)) {
233 oss_logerr2 (errno, typ, "Failed to set non-blocking mode\n");
234 goto err;
235 }
236
237 mmmmssss = (req->nfrags << 16) | lsbindex (req->fragsize);
238 if (ioctl (fd, SNDCTL_DSP_SETFRAGMENT, &mmmmssss)) {
239 oss_logerr2 (errno, typ, "Failed to set buffer length (%d, %d)\n",
240 req->nfrags, req->fragsize);
241 goto err;
242 }
243
244 if (ioctl (fd, in ? SNDCTL_DSP_GETISPACE : SNDCTL_DSP_GETOSPACE, &abinfo)) {
245 oss_logerr2 (errno, typ, "Failed to get buffer length\n");
246 goto err;
247 }
248
249 obt->fmt = fmt;
250 obt->nchannels = nchannels;
251 obt->freq = freq;
252 obt->nfrags = abinfo.fragstotal;
253 obt->fragsize = abinfo.fragsize;
254 *pfd = fd;
255
256 if ((req->fmt != obt->fmt) ||
257 (req->nchannels != obt->nchannels) ||
258 (req->freq != obt->freq) ||
259 (req->fragsize != obt->fragsize) ||
260 (req->nfrags != obt->nfrags)) {
261 #ifdef DEBUG_MISMATCHES
262 dolog ("Audio parameters mismatch\n");
263 oss_dump_info (req, obt);
264 #endif
265 }
266
267 #ifdef DEBUG
268 oss_dump_info (req, obt);
269 #endif
270 return 0;
271
272 err:
273 oss_anal_close (&fd);
274 return -1;
275 }
276
277 static int oss_run_out (HWVoiceOut *hw)
278 {
279 OSSVoiceOut *oss = (OSSVoiceOut *) hw;
280 int err, rpos, live, decr;
281 int samples;
282 uint8_t *dst;
283 st_sample_t *src;
284 struct audio_buf_info abinfo;
285 struct count_info cntinfo;
286
287 live = audio_pcm_hw_get_live_out (hw);
288 if (!live) {
289 return 0;
290 }
291
292 if (oss->mmapped) {
293 int bytes;
294
295 err = ioctl (oss->fd, SNDCTL_DSP_GETOPTR, &cntinfo);
296 if (err < 0) {
297 oss_logerr (errno, "SNDCTL_DSP_GETOPTR failed\n");
298 return 0;
299 }
300
301 if (cntinfo.ptr == oss->old_optr) {
302 if (abs (hw->samples - live) < 64) {
303 dolog ("warning: overrun\n");
304 }
305 return 0;
306 }
307
308 if (cntinfo.ptr > oss->old_optr) {
309 bytes = cntinfo.ptr - oss->old_optr;
310 }
311 else {
312 bytes = hw->bufsize + cntinfo.ptr - oss->old_optr;
313 }
314
315 decr = audio_MIN (bytes >> hw->info.shift, live);
316 }
317 else {
318 err = ioctl (oss->fd, SNDCTL_DSP_GETOSPACE, &abinfo);
319 if (err < 0) {
320 oss_logerr (errno, "SNDCTL_DSP_GETOPTR failed\n");
321 return 0;
322 }
323
324 if (abinfo.bytes < 0 || abinfo.bytes > hw->bufsize) {
325 ldebug ("warning: invalid available size, size=%d bufsize=%d\n",
326 abinfo.bytes, hw->bufsize);
327 return 0;
328 }
329
330 decr = audio_MIN (abinfo.bytes >> hw->info.shift, live);
331 if (!decr) {
332 return 0;
333 }
334 }
335
336 samples = decr;
337 rpos = hw->rpos;
338 while (samples) {
339 int left_till_end_samples = hw->samples - rpos;
340 int convert_samples = audio_MIN (samples, left_till_end_samples);
341
342 src = hw->mix_buf + rpos;
343 dst = advance (oss->pcm_buf, rpos << hw->info.shift);
344
345 hw->clip (dst, src, convert_samples);
346 if (!oss->mmapped) {
347 int written;
348
349 written = write (oss->fd, dst, convert_samples << hw->info.shift);
350 /* XXX: follow errno recommendations ? */
351 if (written == -1) {
352 oss_logerr (
353 errno,
354 "Failed to write %d bytes of audio data from %p\n",
355 convert_samples << hw->info.shift,
356 dst
357 );
358 continue;
359 }
360
361 if (written != convert_samples << hw->info.shift) {
362 int wsamples = written >> hw->info.shift;
363 int wbytes = wsamples << hw->info.shift;
364 if (wbytes != written) {
365 dolog ("warning: misaligned write %d (requested %d), "
366 "alignment %d\n",
367 wbytes, written, hw->info.align + 1);
368 }
369 mixeng_clear (src, wsamples);
370 decr -= wsamples;
371 rpos = (rpos + wsamples) % hw->samples;
372 break;
373 }
374 }
375
376 mixeng_clear (src, convert_samples);
377
378 rpos = (rpos + convert_samples) % hw->samples;
379 samples -= convert_samples;
380 }
381 if (oss->mmapped) {
382 oss->old_optr = cntinfo.ptr;
383 }
384
385 hw->rpos = rpos;
386 return decr;
387 }
388
389 static void oss_fini_out (HWVoiceOut *hw)
390 {
391 int err;
392 OSSVoiceOut *oss = (OSSVoiceOut *) hw;
393
394 ldebug ("oss_fini\n");
395 oss_anal_close (&oss->fd);
396
397 if (oss->pcm_buf) {
398 if (oss->mmapped) {
399 err = munmap (oss->pcm_buf, hw->bufsize);
400 if (err) {
401 oss_logerr (errno, "Failed to unmap buffer %p, size %d\n",
402 oss->pcm_buf, hw->bufsize);
403 }
404 }
405 else {
406 qemu_free (oss->pcm_buf);
407 }
408 oss->pcm_buf = NULL;
409 }
410 }
411
412 static int oss_init_out (HWVoiceOut *hw, int freq, int nchannels, audfmt_e fmt)
413 {
414 OSSVoiceOut *oss = (OSSVoiceOut *) hw;
415 struct oss_params req, obt;
416 int endianness;
417 int err;
418 int fd;
419 audfmt_e effective_fmt;
420
421 req.fmt = aud_to_ossfmt (fmt);
422 req.freq = freq;
423 req.nchannels = nchannels;
424 req.fragsize = conf.fragsize;
425 req.nfrags = conf.nfrags;
426
427 if (oss_open (0, &req, &obt, &fd)) {
428 return -1;
429 }
430
431 err = oss_to_audfmt (obt.fmt, &effective_fmt, &endianness);
432 if (err) {
433 oss_anal_close (&fd);
434 return -1;
435 }
436
437 audio_pcm_init_info (
438 &hw->info,
439 obt.freq,
440 obt.nchannels,
441 effective_fmt,
442 audio_need_to_swap_endian (endianness)
443 );
444 oss->nfrags = obt.nfrags;
445 oss->fragsize = obt.fragsize;
446 hw->bufsize = obt.nfrags * obt.fragsize;
447
448 oss->mmapped = 0;
449 if (conf.try_mmap) {
450 oss->pcm_buf = mmap (0, hw->bufsize, PROT_READ | PROT_WRITE,
451 MAP_SHARED, fd, 0);
452 if (oss->pcm_buf == MAP_FAILED) {
453 oss_logerr (errno, "Failed to map %d bytes of DAC\n",
454 hw->bufsize);
455 } else {
456 int err;
457 int trig = 0;
458 if (ioctl (fd, SNDCTL_DSP_SETTRIGGER, &trig) < 0) {
459 oss_logerr (errno, "SNDCTL_DSP_SETTRIGGER 0 failed\n");
460 }
461 else {
462 trig = PCM_ENABLE_OUTPUT;
463 if (ioctl (fd, SNDCTL_DSP_SETTRIGGER, &trig) < 0) {
464 oss_logerr (
465 errno,
466 "SNDCTL_DSP_SETTRIGGER PCM_ENABLE_OUTPUT failed\n"
467 );
468 }
469 else {
470 oss->mmapped = 1;
471 }
472 }
473
474 if (!oss->mmapped) {
475 err = munmap (oss->pcm_buf, hw->bufsize);
476 if (err) {
477 oss_logerr (errno, "Failed to unmap buffer %p size %d\n",
478 oss->pcm_buf, hw->bufsize);
479 }
480 }
481 }
482 }
483
484 if (!oss->mmapped) {
485 oss->pcm_buf = qemu_mallocz (hw->bufsize);
486 if (!oss->pcm_buf) {
487 oss_anal_close (&fd);
488 return -1;
489 }
490 }
491
492 oss->fd = fd;
493 return 0;
494 }
495
496 static int oss_ctl_out (HWVoiceOut *hw, int cmd, ...)
497 {
498 int trig;
499 OSSVoiceOut *oss = (OSSVoiceOut *) hw;
500
501 if (!oss->mmapped) {
502 return 0;
503 }
504
505 switch (cmd) {
506 case VOICE_ENABLE:
507 ldebug ("enabling voice\n");
508 audio_pcm_info_clear_buf (&hw->info, oss->pcm_buf, hw->samples);
509 trig = PCM_ENABLE_OUTPUT;
510 if (ioctl (oss->fd, SNDCTL_DSP_SETTRIGGER, &trig) < 0) {
511 oss_logerr (
512 errno,
513 "SNDCTL_DSP_SETTRIGGER PCM_ENABLE_OUTPUT failed\n"
514 );
515 return -1;
516 }
517 break;
518
519 case VOICE_DISABLE:
520 ldebug ("disabling voice\n");
521 trig = 0;
522 if (ioctl (oss->fd, SNDCTL_DSP_SETTRIGGER, &trig) < 0) {
523 oss_logerr (errno, "SNDCTL_DSP_SETTRIGGER 0 failed\n");
524 return -1;
525 }
526 break;
527 }
528 return 0;
529 }
530
531 static int oss_init_in (HWVoiceIn *hw,
532 int freq, int nchannels, audfmt_e fmt)
533 {
534 OSSVoiceIn *oss = (OSSVoiceIn *) hw;
535 struct oss_params req, obt;
536 int endianness;
537 int err;
538 int fd;
539 audfmt_e effective_fmt;
540
541 req.fmt = aud_to_ossfmt (fmt);
542 req.freq = freq;
543 req.nchannels = nchannels;
544 req.fragsize = conf.fragsize;
545 req.nfrags = conf.nfrags;
546 if (oss_open (1, &req, &obt, &fd)) {
547 return -1;
548 }
549
550 err = oss_to_audfmt (obt.fmt, &effective_fmt, &endianness);
551 if (err) {
552 oss_anal_close (&fd);
553 return -1;
554 }
555
556 audio_pcm_init_info (
557 &hw->info,
558 obt.freq,
559 obt.nchannels,
560 effective_fmt,
561 audio_need_to_swap_endian (endianness)
562 );
563 oss->nfrags = obt.nfrags;
564 oss->fragsize = obt.fragsize;
565 hw->bufsize = obt.nfrags * obt.fragsize;
566 oss->pcm_buf = qemu_mallocz (hw->bufsize);
567 if (!oss->pcm_buf) {
568 oss_anal_close (&fd);
569 return -1;
570 }
571
572 oss->fd = fd;
573 return 0;
574 }
575
576 static void oss_fini_in (HWVoiceIn *hw)
577 {
578 OSSVoiceIn *oss = (OSSVoiceIn *) hw;
579
580 oss_anal_close (&oss->fd);
581
582 if (oss->pcm_buf) {
583 qemu_free (oss->pcm_buf);
584 oss->pcm_buf = NULL;
585 }
586 }
587
588 static int oss_run_in (HWVoiceIn *hw)
589 {
590 OSSVoiceIn *oss = (OSSVoiceIn *) hw;
591 int hwshift = hw->info.shift;
592 int i;
593 int live = audio_pcm_hw_get_live_in (hw);
594 int dead = hw->samples - live;
595 size_t read_samples = 0;
596 struct {
597 int add;
598 int len;
599 } bufs[2] = {
600 { hw->wpos, 0 },
601 { 0, 0 }
602 };
603
604 if (!dead) {
605 return 0;
606 }
607
608 if (hw->wpos + dead > hw->samples) {
609 bufs[0].len = (hw->samples - hw->wpos) << hwshift;
610 bufs[1].len = (dead - (hw->samples - hw->wpos)) << hwshift;
611 }
612 else {
613 bufs[0].len = dead << hwshift;
614 }
615
616
617 for (i = 0; i < 2; ++i) {
618 ssize_t nread;
619
620 if (bufs[i].len) {
621 void *p = advance (oss->pcm_buf, bufs[i].add << hwshift);
622 nread = read (oss->fd, p, bufs[i].len);
623
624 if (nread > 0) {
625 if (nread & hw->info.align) {
626 dolog ("warning: misaligned read %d (requested %d), "
627 "alignment %d\n", nread, bufs[i].add << hwshift,
628 hw->info.align + 1);
629 }
630 read_samples += nread >> hwshift;
631 hw->conv (hw->conv_buf + bufs[i].add, p, nread >> hwshift,
632 &nominal_volume);
633 }
634
635 if (bufs[i].len - nread) {
636 if (nread == -1) {
637 switch (errno) {
638 case EINTR:
639 case EAGAIN:
640 break;
641 default:
642 oss_logerr (
643 errno,
644 "Failed to read %d bytes of audio (to %p)\n",
645 bufs[i].len, p
646 );
647 break;
648 }
649 }
650 break;
651 }
652 }
653 }
654
655 hw->wpos = (hw->wpos + read_samples) % hw->samples;
656 return read_samples;
657 }
658
659 static int oss_read (SWVoiceIn *sw, void *buf, int size)
660 {
661 return audio_pcm_sw_read (sw, buf, size);
662 }
663
664 static int oss_ctl_in (HWVoiceIn *hw, int cmd, ...)
665 {
666 (void) hw;
667 (void) cmd;
668 return 0;
669 }
670
671 static void *oss_audio_init (void)
672 {
673 return &conf;
674 }
675
676 static void oss_audio_fini (void *opaque)
677 {
678 (void) opaque;
679 }
680
681 static struct audio_option oss_options[] = {
682 {"FRAGSIZE", AUD_OPT_INT, &conf.fragsize,
683 "Fragment size in bytes", NULL, 0},
684 {"NFRAGS", AUD_OPT_INT, &conf.nfrags,
685 "Number of fragments", NULL, 0},
686 {"MMAP", AUD_OPT_BOOL, &conf.try_mmap,
687 "Try using memory mapped access", NULL, 0},
688 {"DAC_DEV", AUD_OPT_STR, &conf.devpath_out,
689 "Path to DAC device", NULL, 0},
690 {"ADC_DEV", AUD_OPT_STR, &conf.devpath_in,
691 "Path to ADC device", NULL, 0},
692 {NULL, 0, NULL, NULL, NULL, 0}
693 };
694
695 static struct audio_pcm_ops oss_pcm_ops = {
696 oss_init_out,
697 oss_fini_out,
698 oss_run_out,
699 oss_write,
700 oss_ctl_out,
701
702 oss_init_in,
703 oss_fini_in,
704 oss_run_in,
705 oss_read,
706 oss_ctl_in
707 };
708
709 struct audio_driver oss_audio_driver = {
710 INIT_FIELD (name = ) "oss",
711 INIT_FIELD (descr = ) "OSS http://www.opensound.com",
712 INIT_FIELD (options = ) oss_options,
713 INIT_FIELD (init = ) oss_audio_init,
714 INIT_FIELD (fini = ) oss_audio_fini,
715 INIT_FIELD (pcm_ops = ) &oss_pcm_ops,
716 INIT_FIELD (can_be_default = ) 1,
717 INIT_FIELD (max_voices_out = ) INT_MAX,
718 INIT_FIELD (max_voices_in = ) INT_MAX,
719 INIT_FIELD (voice_size_out = ) sizeof (OSSVoiceOut),
720 INIT_FIELD (voice_size_in = ) sizeof (OSSVoiceIn)
721 };