VirtualBox

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

Last change on this file since 5050 was 5050, checked in by vboxsync, 17 years ago

Moved the ALSA code into a separate shared object loaded at runtime

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