VirtualBox

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

Last change on this file since 49983 was 45136, checked in by vboxsync, 12 years ago

Devices/Audio: mangle symbols of libasound and libpulse, otherwise they can be resolved by dynamic libraries if -fvisibility is not supported

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