VirtualBox

source: vbox/trunk/src/VBox/Devices/Audio/alsaaudio.c@ 239

Last change on this file since 239 was 1, checked in by vboxsync, 55 years ago

import

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 26.5 KB
Line 
1/*
2 * QEMU ALSA audio driver
3 *
4 * Copyright (c) 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 <alsa/asoundlib.h>
25
26#include "Builtins.h"
27#include "../../vl_vbox.h"
28#include "audio.h"
29#include <iprt/alloc.h>
30
31#define AUDIO_CAP "alsa"
32#include "audio_int.h"
33
34typedef struct ALSAVoiceOut {
35 HWVoiceOut hw;
36 void *pcm_buf;
37 snd_pcm_t *handle;
38} ALSAVoiceOut;
39
40typedef struct ALSAVoiceIn {
41 HWVoiceIn hw;
42 snd_pcm_t *handle;
43 void *pcm_buf;
44} ALSAVoiceIn;
45
46static struct {
47 int size_in_usec_in;
48 int size_in_usec_out;
49 const char *pcm_name_in;
50 const char *pcm_name_out;
51 unsigned int buffer_size_in;
52 unsigned int period_size_in;
53 unsigned int buffer_size_out;
54 unsigned int period_size_out;
55 unsigned int threshold;
56
57 int buffer_size_in_overriden;
58 int period_size_in_overriden;
59
60 int buffer_size_out_overriden;
61 int period_size_out_overriden;
62 int verbose;
63} conf = {
64#ifdef HIGH_LATENCY
65 INIT_FIELD (.size_in_usec_in =) 1,
66 INIT_FIELD (.size_in_usec_out =) 1,
67#else
68 INIT_FIELD (.size_in_usec_in =) 0,
69 INIT_FIELD (.size_in_usec_out =) 0,
70#endif
71 INIT_FIELD (.pcm_name_out =) "default",
72 INIT_FIELD (.pcm_name_in =) "default",
73#ifdef HIGH_LATENCY
74 INIT_FIELD (.buffer_size_in =) 400000,
75 INIT_FIELD (.period_size_in =) 400000 / 4,
76 INIT_FIELD (.buffer_size_out =) 400000,
77 INIT_FIELD (.period_size_out =) 400000 / 4,
78#else
79#define DEFAULT_BUFFER_SIZE 1024
80#define DEFAULT_PERIOD_SIZE 256
81 INIT_FIELD (.buffer_size_in =) DEFAULT_BUFFER_SIZE * 4,
82 INIT_FIELD (.period_size_in =) DEFAULT_PERIOD_SIZE * 4,
83 INIT_FIELD (.buffer_size_out =) DEFAULT_BUFFER_SIZE,
84 INIT_FIELD (.period_size_out =) DEFAULT_PERIOD_SIZE,
85#endif
86 INIT_FIELD (.threshold =) 0,
87 INIT_FIELD (.buffer_size_in_overriden =) 0,
88 INIT_FIELD (.period_size_in_overriden =) 0,
89 INIT_FIELD (.buffer_size_out_overriden =) 0,
90 INIT_FIELD (.period_size_out_overriden =) 0,
91 INIT_FIELD (.verbose =) 0
92};
93
94struct alsa_params_req {
95 int freq;
96 audfmt_e fmt;
97 int nchannels;
98 unsigned int buffer_size;
99 unsigned int period_size;
100};
101
102struct alsa_params_obt {
103 int freq;
104 audfmt_e fmt;
105 int nchannels;
106 snd_pcm_uframes_t samples;
107};
108
109static void GCC_FMT_ATTR (2, 3) alsa_logerr (int err, const char *fmt, ...)
110{
111 va_list ap;
112
113 va_start (ap, fmt);
114 AUD_vlog (AUDIO_CAP, fmt, ap);
115 va_end (ap);
116
117 AUD_log (AUDIO_CAP, "Reason: %s\n", snd_strerror (err));
118}
119
120static void GCC_FMT_ATTR (3, 4) alsa_logerr2 (
121 int err,
122 const char *typ,
123 const char *fmt,
124 ...
125 )
126{
127 va_list ap;
128
129 AUD_log (AUDIO_CAP, "Could not initialize %s\n", typ);
130
131 va_start (ap, fmt);
132 AUD_vlog (AUDIO_CAP, fmt, ap);
133 va_end (ap);
134
135 AUD_log (AUDIO_CAP, "Reason: %s\n", snd_strerror (err));
136}
137
138static void alsa_anal_close (snd_pcm_t **handlep)
139{
140 int err = snd_pcm_close (*handlep);
141 if (err) {
142 alsa_logerr (err, "Failed to close PCM handle %p\n",
143 (void *) *handlep);
144 }
145 *handlep = NULL;
146}
147
148static int alsa_write (SWVoiceOut *sw, void *buf, int len)
149{
150 return audio_pcm_sw_write (sw, buf, len);
151}
152
153static int aud_to_alsafmt (audfmt_e fmt)
154{
155 switch (fmt) {
156 case AUD_FMT_S8:
157 return SND_PCM_FORMAT_S8;
158
159 case AUD_FMT_U8:
160 return SND_PCM_FORMAT_U8;
161
162 case AUD_FMT_S16:
163 return SND_PCM_FORMAT_S16_LE;
164
165 case AUD_FMT_U16:
166 return SND_PCM_FORMAT_U16_LE;
167
168 default:
169 dolog ("Internal logic error: Bad audio format %d\n", fmt);
170#ifdef DEBUG_AUDIO
171 abort ();
172#endif
173 return SND_PCM_FORMAT_U8;
174 }
175}
176
177static int alsa_to_audfmt (int alsafmt, audfmt_e *fmt, int *endianness)
178{
179 switch (alsafmt) {
180 case SND_PCM_FORMAT_S8:
181 *endianness = 0;
182 *fmt = AUD_FMT_S8;
183 break;
184
185 case SND_PCM_FORMAT_U8:
186 *endianness = 0;
187 *fmt = AUD_FMT_U8;
188 break;
189
190 case SND_PCM_FORMAT_S16_LE:
191 *endianness = 0;
192 *fmt = AUD_FMT_S16;
193 break;
194
195 case SND_PCM_FORMAT_U16_LE:
196 *endianness = 0;
197 *fmt = AUD_FMT_U16;
198 break;
199
200 case SND_PCM_FORMAT_S16_BE:
201 *endianness = 1;
202 *fmt = AUD_FMT_S16;
203 break;
204
205 case SND_PCM_FORMAT_U16_BE:
206 *endianness = 1;
207 *fmt = AUD_FMT_U16;
208 break;
209
210 default:
211 dolog ("Unrecognized audio format %d\n", alsafmt);
212 return -1;
213 }
214
215 return 0;
216}
217
218#if defined DEBUG_MISMATCHES || defined DEBUG
219static void alsa_dump_info (struct alsa_params_req *req,
220 struct alsa_params_obt *obt)
221{
222 dolog ("parameter | requested value | obtained value\n");
223 dolog ("format | %10d | %10d\n", req->fmt, obt->fmt);
224 dolog ("channels | %10d | %10d\n",
225 req->nchannels, obt->nchannels);
226 dolog ("frequency | %10d | %10d\n", req->freq, obt->freq);
227 dolog ("============================================\n");
228 dolog ("requested: buffer size %d period size %d\n",
229 req->buffer_size, req->period_size);
230 dolog ("obtained: samples %ld\n", obt->samples);
231}
232#endif
233
234static void alsa_set_threshold (snd_pcm_t *handle, snd_pcm_uframes_t threshold)
235{
236 int err;
237 snd_pcm_sw_params_t *sw_params;
238
239 snd_pcm_sw_params_alloca (&sw_params);
240
241 err = snd_pcm_sw_params_current (handle, sw_params);
242 if (err < 0) {
243 dolog ("Could not fully initialize DAC\n");
244 alsa_logerr (err, "Failed to get current software parameters\n");
245 return;
246 }
247
248 err = snd_pcm_sw_params_set_start_threshold (handle, sw_params, threshold);
249 if (err < 0) {
250 dolog ("Could not fully initialize DAC\n");
251 alsa_logerr (err, "Failed to set software threshold to %ld\n",
252 threshold);
253 return;
254 }
255
256 err = snd_pcm_sw_params (handle, sw_params);
257 if (err < 0) {
258 dolog ("Could not fully initialize DAC\n");
259 alsa_logerr (err, "Failed to set software parameters\n");
260 return;
261 }
262}
263
264static int alsa_open (int in, struct alsa_params_req *req,
265 struct alsa_params_obt *obt, snd_pcm_t **handlep)
266{
267 snd_pcm_t *handle;
268 snd_pcm_hw_params_t *hw_params;
269 int err;
270 unsigned int freq, nchannels;
271 const char *pcm_name = in ? conf.pcm_name_in : conf.pcm_name_out;
272 unsigned int period_size, buffer_size;
273 snd_pcm_uframes_t obt_buffer_size;
274 const char *typ = in ? "ADC" : "DAC";
275
276 freq = req->freq;
277 period_size = req->period_size;
278 buffer_size = req->buffer_size;
279 nchannels = req->nchannels;
280
281 snd_pcm_hw_params_alloca (&hw_params);
282
283 err = snd_pcm_open (
284 &handle,
285 pcm_name,
286 in ? SND_PCM_STREAM_CAPTURE : SND_PCM_STREAM_PLAYBACK,
287 SND_PCM_NONBLOCK
288 );
289 if (err < 0) {
290 alsa_logerr2 (err, typ, "Failed to open `%s':\n", pcm_name);
291 LogRel(("Audio/ALSA: Failed to open '%s' as %s\n", pcm_name, typ));
292 return -1;
293 }
294
295 err = snd_pcm_hw_params_any (handle, hw_params);
296 if (err < 0) {
297 alsa_logerr2 (err, typ, "Failed to initialize hardware parameters\n");
298 goto err;
299 }
300
301 err = snd_pcm_hw_params_set_access (
302 handle,
303 hw_params,
304 SND_PCM_ACCESS_RW_INTERLEAVED
305 );
306 if (err < 0) {
307 alsa_logerr2 (err, typ, "Failed to set access type\n");
308 goto err;
309 }
310
311 err = snd_pcm_hw_params_set_format (handle, hw_params, req->fmt);
312 if (err < 0) {
313 alsa_logerr2 (err, typ, "Failed to set format %d\n", req->fmt);
314 goto err;
315 }
316
317 err = snd_pcm_hw_params_set_rate_near (handle, hw_params, &freq, 0);
318 if (err < 0) {
319 alsa_logerr2 (err, typ, "Failed to set frequency %d\n", req->freq);
320 goto err;
321 }
322
323 err = snd_pcm_hw_params_set_channels_near (
324 handle,
325 hw_params,
326 &nchannels
327 );
328 if (err < 0) {
329 alsa_logerr2 (err, typ, "Failed to set number of channels %d\n",
330 req->nchannels);
331 goto err;
332 }
333
334 if (nchannels != 1 && nchannels != 2) {
335 alsa_logerr2 (err, typ,
336 "Can not handle obtained number of channels %d\n",
337 nchannels);
338 goto err;
339 }
340
341 if (!((in && conf.size_in_usec_in) || (!in && conf.size_in_usec_out))) {
342 if (!buffer_size) {
343 buffer_size = DEFAULT_BUFFER_SIZE;
344 period_size= DEFAULT_PERIOD_SIZE;
345 }
346 }
347
348 if (buffer_size) {
349 if ((in && conf.size_in_usec_in) || (!in && conf.size_in_usec_out)) {
350 if (period_size) {
351 err = snd_pcm_hw_params_set_period_time_near (
352 handle,
353 hw_params,
354 &period_size,
355 0
356 );
357 if (err < 0) {
358 alsa_logerr2 (err, typ,
359 "Failed to set period time %d\n",
360 req->period_size);
361 goto err;
362 }
363 }
364
365 err = snd_pcm_hw_params_set_buffer_time_near (
366 handle,
367 hw_params,
368 &buffer_size,
369 0
370 );
371
372 if (err < 0) {
373 alsa_logerr2 (err, typ,
374 "Failed to set buffer time %d\n",
375 req->buffer_size);
376 goto err;
377 }
378 }
379 else {
380 int dir;
381 snd_pcm_uframes_t minval;
382
383 if (period_size) {
384 minval = period_size;
385 dir = 0;
386
387 err = snd_pcm_hw_params_get_period_size_min (
388 hw_params,
389 &minval,
390 &dir
391 );
392 if (err < 0) {
393 alsa_logerr (
394 err,
395 "Could not get minmal period size for %s\n",
396 typ
397 );
398 }
399 else {
400 if (period_size < minval) {
401 if ((in && conf.period_size_in_overriden)
402 || (!in && conf.period_size_out_overriden)) {
403 dolog ("%s period size(%d) is less "
404 "than minmal period size(%ld)\n",
405 typ,
406 period_size,
407 minval);
408 }
409 period_size = minval;
410 }
411 }
412
413 err = snd_pcm_hw_params_set_period_size (
414 handle,
415 hw_params,
416 period_size,
417 0
418 );
419 if (err < 0) {
420 alsa_logerr2 (err, typ, "Failed to set period size %d\n",
421 req->period_size);
422 goto err;
423 }
424 }
425
426 minval = buffer_size;
427 err = snd_pcm_hw_params_get_buffer_size_min (
428 hw_params,
429 &minval
430 );
431 if (err < 0) {
432 alsa_logerr (err, "Could not get minmal buffer size for %s\n",
433 typ);
434 }
435 else {
436 if (buffer_size < minval) {
437 if ((in && conf.buffer_size_in_overriden)
438 || (!in && conf.buffer_size_out_overriden)) {
439 dolog (
440 "%s buffer size(%d) is less "
441 "than minimal buffer size(%ld)\n",
442 typ,
443 buffer_size,
444 minval
445 );
446 }
447 buffer_size = minval;
448 }
449 }
450
451 err = snd_pcm_hw_params_set_buffer_size (
452 handle,
453 hw_params,
454 buffer_size
455 );
456 if (err < 0) {
457 alsa_logerr2 (err, typ, "Failed to set buffer size %d\n",
458 req->buffer_size);
459 goto err;
460 }
461 }
462 }
463 else {
464 dolog ("warning: Buffer size is not set\n");
465 }
466
467 err = snd_pcm_hw_params (handle, hw_params);
468 if (err < 0) {
469 alsa_logerr2 (err, typ, "Failed to apply audio parameters\n");
470 goto err;
471 }
472
473 err = snd_pcm_hw_params_get_buffer_size (hw_params, &obt_buffer_size);
474 if (err < 0) {
475 alsa_logerr2 (err, typ, "Failed to get buffer size\n");
476 goto err;
477 }
478
479 err = snd_pcm_prepare (handle);
480 if (err < 0) {
481 alsa_logerr2 (err, typ, "Could not prepare handle %p\n",
482 (void *) handle);
483 goto err;
484 }
485
486 if (!in && conf.threshold) {
487 snd_pcm_uframes_t threshold;
488 int bytes_per_sec;
489
490 bytes_per_sec = freq
491 << (nchannels == 2)
492 << (req->fmt == AUD_FMT_S16 || req->fmt == AUD_FMT_U16);
493
494 threshold = (conf.threshold * bytes_per_sec) / 1000;
495 alsa_set_threshold (handle, threshold);
496 }
497
498 obt->fmt = req->fmt;
499 obt->nchannels = nchannels;
500 obt->freq = freq;
501 obt->samples = obt_buffer_size;
502 *handlep = handle;
503
504#if defined DEBUG_MISMATCHES || defined DEBUG
505 if (obt->fmt != req->fmt ||
506 obt->nchannels != req->nchannels ||
507 obt->freq != req->freq) {
508 dolog ("Audio paramters mismatch for %s\n", typ);
509 alsa_dump_info (req, obt);
510 }
511#endif
512
513#ifdef DEBUG
514 alsa_dump_info (req, obt);
515#endif
516 return 0;
517
518 err:
519 alsa_anal_close (&handle);
520 return -1;
521}
522
523static int alsa_recover (snd_pcm_t *handle)
524{
525 int err = snd_pcm_prepare (handle);
526 if (err < 0) {
527 alsa_logerr (err, "Failed to prepare handle %p\n",
528 (void *) handle);
529 return -1;
530 }
531 return 0;
532}
533
534static snd_pcm_sframes_t alsa_get_avail (snd_pcm_t *handle)
535{
536 snd_pcm_sframes_t avail;
537
538 avail = snd_pcm_avail_update (handle);
539 if (avail < 0) {
540 if (avail == -EPIPE) {
541 if (!alsa_recover (handle)) {
542 avail = snd_pcm_avail_update (handle);
543 }
544 }
545
546 if (avail < 0) {
547 alsa_logerr (avail,
548 "Could not obtain number of available frames\n");
549 return -1;
550 }
551 }
552
553 return avail;
554}
555
556static int alsa_run_out (HWVoiceOut *hw)
557{
558 ALSAVoiceOut *alsa = (ALSAVoiceOut *) hw;
559 int rpos, live, decr;
560 int samples;
561 uint8_t *dst;
562 st_sample_t *src;
563 snd_pcm_sframes_t avail;
564
565 live = audio_pcm_hw_get_live_out (hw);
566 if (!live) {
567 return 0;
568 }
569
570 avail = alsa_get_avail (alsa->handle);
571 if (avail < 0) {
572 dolog ("Could not get number of available playback frames\n");
573 return 0;
574 }
575
576 decr = audio_MIN (live, avail);
577 samples = decr;
578 rpos = hw->rpos;
579 while (samples) {
580 int left_till_end_samples = hw->samples - rpos;
581 int len = audio_MIN (samples, left_till_end_samples);
582 snd_pcm_sframes_t written;
583
584 src = hw->mix_buf + rpos;
585 dst = advance (alsa->pcm_buf, rpos << hw->info.shift);
586
587 hw->clip (dst, src, len);
588
589 while (len) {
590 written = snd_pcm_writei (alsa->handle, dst, len);
591
592 if (written <= 0) {
593 switch (written) {
594 case 0:
595 if (conf.verbose) {
596 dolog ("Failed to write %d frames (wrote zero)\n", len);
597 }
598 goto exit;
599
600 case -EPIPE:
601 if (alsa_recover (alsa->handle)) {
602 alsa_logerr (written, "Failed to write %d frames\n",
603 len);
604 goto exit;
605 }
606 if (conf.verbose) {
607 dolog ("Recovering from playback xrun\n");
608 }
609 continue;
610
611 case -EAGAIN:
612 goto exit;
613
614 default:
615 alsa_logerr (written, "Failed to write %d frames to %p\n",
616 len, dst);
617 goto exit;
618 }
619 }
620
621#if 0
622 mixeng_sniff_and_clear (hw, src, dst, written);
623#endif
624 rpos = (rpos + written) % hw->samples;
625 samples -= written;
626 len -= written;
627 dst = advance (dst, written << hw->info.shift);
628 src += written;
629 }
630 }
631
632 exit:
633 hw->rpos = rpos;
634 return decr;
635}
636
637static void alsa_fini_out (HWVoiceOut *hw)
638{
639 ALSAVoiceOut *alsa = (ALSAVoiceOut *) hw;
640
641 ldebug ("alsa_fini\n");
642 alsa_anal_close (&alsa->handle);
643
644 if (alsa->pcm_buf) {
645 qemu_free (alsa->pcm_buf);
646 alsa->pcm_buf = NULL;
647 }
648}
649
650static int alsa_init_out (HWVoiceOut *hw, audsettings_t *as)
651{
652 ALSAVoiceOut *alsa = (ALSAVoiceOut *) hw;
653 struct alsa_params_req req;
654 struct alsa_params_obt obt;
655 audfmt_e effective_fmt;
656 int endianness;
657 int err;
658 snd_pcm_t *handle;
659 audsettings_t obt_as;
660
661 req.fmt = aud_to_alsafmt (as->fmt);
662 req.freq = as->freq;
663 req.nchannels = as->nchannels;
664 req.period_size = conf.period_size_out;
665 req.buffer_size = conf.buffer_size_out;
666
667 if (alsa_open (0, &req, &obt, &handle)) {
668 return -1;
669 }
670
671 err = alsa_to_audfmt (obt.fmt, &effective_fmt, &endianness);
672 if (err) {
673 alsa_anal_close (&handle);
674 return -1;
675 }
676
677 obt_as.freq = obt.freq;
678 obt_as.nchannels = obt.nchannels;
679 obt_as.fmt = effective_fmt;
680 obt_as.endianness = endianness;
681
682 audio_pcm_init_info (&hw->info, &obt_as);
683 hw->samples = obt.samples;
684
685 alsa->pcm_buf = audio_calloc (AUDIO_FUNC, obt.samples, 1 << hw->info.shift);
686 if (!alsa->pcm_buf) {
687 dolog ("Could not allocate DAC buffer (%d samples, each %d bytes)\n",
688 hw->samples, 1 << hw->info.shift);
689 alsa_anal_close (&handle);
690 return -1;
691 }
692
693 alsa->handle = handle;
694 return 0;
695}
696
697static int alsa_voice_ctl (snd_pcm_t *handle, const char *typ, int pause)
698{
699 int err;
700
701 if (pause) {
702 err = snd_pcm_drop (handle);
703 if (err < 0) {
704 alsa_logerr (err, "Could not stop %s\n", typ);
705 return -1;
706 }
707 }
708 else {
709 err = snd_pcm_prepare (handle);
710 if (err < 0) {
711 alsa_logerr (err, "Could not prepare handle for %s\n", typ);
712 return -1;
713 }
714 }
715
716 return 0;
717}
718
719static int alsa_ctl_out (HWVoiceOut *hw, int cmd, ...)
720{
721 ALSAVoiceOut *alsa = (ALSAVoiceOut *) hw;
722
723 switch (cmd) {
724 case VOICE_ENABLE:
725 ldebug ("enabling voice\n");
726 return alsa_voice_ctl (alsa->handle, "playback", 0);
727
728 case VOICE_DISABLE:
729 ldebug ("disabling voice\n");
730 return alsa_voice_ctl (alsa->handle, "playback", 1);
731 }
732
733 return -1;
734}
735
736static int alsa_init_in (HWVoiceIn *hw, audsettings_t *as)
737{
738 ALSAVoiceIn *alsa = (ALSAVoiceIn *) hw;
739 struct alsa_params_req req;
740 struct alsa_params_obt obt;
741 int endianness;
742 int err;
743 audfmt_e effective_fmt;
744 snd_pcm_t *handle;
745 audsettings_t obt_as;
746
747 req.fmt = aud_to_alsafmt (as->fmt);
748 req.freq = as->freq;
749 req.nchannels = as->nchannels;
750 req.period_size = conf.period_size_in;
751 req.buffer_size = conf.buffer_size_in;
752
753 if (alsa_open (1, &req, &obt, &handle)) {
754 return -1;
755 }
756
757 err = alsa_to_audfmt (obt.fmt, &effective_fmt, &endianness);
758 if (err) {
759 alsa_anal_close (&handle);
760 return -1;
761 }
762
763 obt_as.freq = obt.freq;
764 obt_as.nchannels = obt.nchannels;
765 obt_as.fmt = effective_fmt;
766 obt_as.endianness = endianness;
767
768 audio_pcm_init_info (&hw->info, &obt_as);
769 hw->samples = obt.samples;
770
771 alsa->pcm_buf = audio_calloc (AUDIO_FUNC, hw->samples, 1 << hw->info.shift);
772 if (!alsa->pcm_buf) {
773 dolog ("Could not allocate ADC buffer (%d samples, each %d bytes)\n",
774 hw->samples, 1 << hw->info.shift);
775 alsa_anal_close (&handle);
776 return -1;
777 }
778
779 alsa->handle = handle;
780 return 0;
781}
782
783static void alsa_fini_in (HWVoiceIn *hw)
784{
785 ALSAVoiceIn *alsa = (ALSAVoiceIn *) hw;
786
787 alsa_anal_close (&alsa->handle);
788
789 if (alsa->pcm_buf) {
790 qemu_free (alsa->pcm_buf);
791 alsa->pcm_buf = NULL;
792 }
793}
794
795static int alsa_run_in (HWVoiceIn *hw)
796{
797 ALSAVoiceIn *alsa = (ALSAVoiceIn *) hw;
798 int hwshift = hw->info.shift;
799 int i;
800 int live = audio_pcm_hw_get_live_in (hw);
801 int dead = hw->samples - live;
802 int decr;
803 struct {
804 int add;
805 int len;
806 } bufs[2];
807
808 snd_pcm_sframes_t avail;
809 snd_pcm_uframes_t read_samples = 0;
810
811 bufs[0].add = hw->wpos;
812 bufs[0].len = 0;
813 bufs[1].add = 0;
814 bufs[1].len = 0;
815
816 if (!dead) {
817 return 0;
818 }
819
820 avail = alsa_get_avail (alsa->handle);
821 if (avail < 0) {
822 dolog ("Could not get number of captured frames\n");
823 return 0;
824 }
825
826 if (!avail && (snd_pcm_state (alsa->handle) == SND_PCM_STATE_PREPARED)) {
827 avail = hw->samples;
828 }
829
830 decr = audio_MIN (dead, avail);
831 if (!decr) {
832 return 0;
833 }
834
835 if (hw->wpos + decr > hw->samples) {
836 bufs[0].len = (hw->samples - hw->wpos);
837 bufs[1].len = (decr - (hw->samples - hw->wpos));
838 }
839 else {
840 bufs[0].len = decr;
841 }
842
843 for (i = 0; i < 2; ++i) {
844 void *src;
845 st_sample_t *dst;
846 snd_pcm_sframes_t nread;
847 snd_pcm_uframes_t len;
848
849 len = bufs[i].len;
850
851 src = advance (alsa->pcm_buf, bufs[i].add << hwshift);
852 dst = hw->conv_buf + bufs[i].add;
853
854 while (len) {
855 nread = snd_pcm_readi (alsa->handle, src, len);
856
857 if (nread <= 0) {
858 switch (nread) {
859 case 0:
860 if (conf.verbose) {
861 dolog ("Failed to read %ld frames (read zero)\n", len);
862 }
863 goto exit;
864
865 case -EPIPE:
866 if (alsa_recover (alsa->handle)) {
867 alsa_logerr (nread, "Failed to read %ld frames\n", len);
868 goto exit;
869 }
870 if (conf.verbose) {
871 dolog ("Recovering from capture xrun\n");
872 }
873 continue;
874
875 case -EAGAIN:
876 goto exit;
877
878 default:
879 alsa_logerr (
880 nread,
881 "Failed to read %ld frames from %p\n",
882 len,
883 src
884 );
885 goto exit;
886 }
887 }
888
889 hw->conv (dst, src, nread, &nominal_volume);
890
891 src = advance (src, nread << hwshift);
892 dst += nread;
893
894 read_samples += nread;
895 len -= nread;
896 }
897 }
898
899 exit:
900 hw->wpos = (hw->wpos + read_samples) % hw->samples;
901 return read_samples;
902}
903
904static int alsa_read (SWVoiceIn *sw, void *buf, int size)
905{
906 return audio_pcm_sw_read (sw, buf, size);
907}
908
909static int alsa_ctl_in (HWVoiceIn *hw, int cmd, ...)
910{
911 ALSAVoiceIn *alsa = (ALSAVoiceIn *) hw;
912
913 switch (cmd) {
914 case VOICE_ENABLE:
915 ldebug ("enabling voice\n");
916 return alsa_voice_ctl (alsa->handle, "capture", 0);
917
918 case VOICE_DISABLE:
919 ldebug ("disabling voice\n");
920 return alsa_voice_ctl (alsa->handle, "capture", 1);
921 }
922
923 return -1;
924}
925
926static void *alsa_audio_init (void)
927{
928 return &conf;
929}
930
931static void alsa_audio_fini (void *opaque)
932{
933 (void) opaque;
934}
935
936static struct audio_option alsa_options[] = {
937 {"DAC_SIZE_IN_USEC", AUD_OPT_BOOL, &conf.size_in_usec_out,
938 "DAC period/buffer size in microseconds (otherwise in frames)", NULL, 0},
939 {"DAC_PERIOD_SIZE", AUD_OPT_INT, &conf.period_size_out,
940 "DAC period size", &conf.period_size_out_overriden, 0},
941 {"DAC_BUFFER_SIZE", AUD_OPT_INT, &conf.buffer_size_out,
942 "DAC buffer size", &conf.buffer_size_out_overriden, 0},
943
944 {"ADC_SIZE_IN_USEC", AUD_OPT_BOOL, &conf.size_in_usec_in,
945 "ADC period/buffer size in microseconds (otherwise in frames)", NULL, 0},
946 {"ADC_PERIOD_SIZE", AUD_OPT_INT, &conf.period_size_in,
947 "ADC period size", &conf.period_size_in_overriden, 0},
948 {"ADC_BUFFER_SIZE", AUD_OPT_INT, &conf.buffer_size_in,
949 "ADC buffer size", &conf.buffer_size_in_overriden, 0},
950
951 {"THRESHOLD", AUD_OPT_INT, &conf.threshold,
952 "(undocumented)", NULL, 0},
953
954 {"DAC_DEV", AUD_OPT_STR, &conf.pcm_name_out,
955 "DAC device name (for instance dmix)", NULL, 0},
956
957 {"ADC_DEV", AUD_OPT_STR, &conf.pcm_name_in,
958 "ADC device name", NULL, 0},
959
960 {"VERBOSE", AUD_OPT_BOOL, &conf.verbose,
961 "Behave in a more verbose way", NULL, 0},
962
963 {NULL, 0, NULL, NULL, NULL, 0}
964};
965
966static struct audio_pcm_ops alsa_pcm_ops = {
967 alsa_init_out,
968 alsa_fini_out,
969 alsa_run_out,
970 alsa_write,
971 alsa_ctl_out,
972
973 alsa_init_in,
974 alsa_fini_in,
975 alsa_run_in,
976 alsa_read,
977 alsa_ctl_in
978};
979
980struct audio_driver alsa_audio_driver = {
981 INIT_FIELD (name = ) "alsa",
982 INIT_FIELD (descr = ) "ALSA http://www.alsa-project.org",
983 INIT_FIELD (options = ) alsa_options,
984 INIT_FIELD (init = ) alsa_audio_init,
985 INIT_FIELD (fini = ) alsa_audio_fini,
986 INIT_FIELD (pcm_ops = ) &alsa_pcm_ops,
987 INIT_FIELD (can_be_default = ) 1,
988 INIT_FIELD (max_voices_out = ) INT_MAX,
989 INIT_FIELD (max_voices_in = ) INT_MAX,
990 INIT_FIELD (voice_size_out = ) sizeof (ALSAVoiceOut),
991 INIT_FIELD (voice_size_in = ) sizeof (ALSAVoiceIn)
992};
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette