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