VirtualBox

source: vbox/trunk/src/VBox/Devices/Audio/audio.c@ 5063

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

Only load the ALSA driver if ALSA audio is selected for the VM

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 49.7 KB
Line 
1/*
2 * QEMU Audio subsystem
3 *
4 * Copyright (c) 2003-2005 Vassili Karpov (malc)
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24#define LOG_GROUP LOG_GROUP_DEV_AUDIO
25#include <VBox/pdm.h>
26#include <VBox/err.h>
27#include <VBox/mm.h>
28
29#include <VBox/log.h>
30#include <iprt/assert.h>
31#include <iprt/uuid.h>
32#include <iprt/string.h>
33#include <iprt/alloc.h>
34
35#if defined(RT_OS_LINUX) && defined(VBOX_WITH_ALSA)
36# include <iprt/ldr.h>
37# define VBOX_LIB_ALSA "VBoxAlsa"
38#endif
39
40#include "Builtins.h"
41#include "../../vl_vbox.h"
42
43#include <ctype.h>
44#include <stdlib.h>
45
46#define AUDIO_CAP "audio"
47#include "audio.h"
48#include "audio_int.h"
49
50#ifdef RT_OS_WINDOWS
51#define strcasecmp stricmp
52#endif
53
54/* #define DEBUG_PLIVE */
55/* #define DEBUG_LIVE */
56/* #define DEBUG_OUT */
57/* #define DEBUG_CAPTURE */
58
59#define SW_NAME(sw) (sw)->name ? (sw)->name : "unknown"
60
61typedef struct DRVAUDIO
62{
63 /** The audio interface. */
64 PDMIAUDIOCONNECTOR IAudioConnector;
65 /** Pointer to the driver instance. */
66 PPDMDRVINS pDrvIns;
67} DRVAUDIO, *PDRVAUDIO;
68
69#if !(defined(RT_OS_LINUX) && defined(VBOX_WITH_ALSA))
70static struct audio_driver *drvtab[] = {
71# ifdef RT_OS_LINUX
72 &oss_audio_driver,
73# endif
74# ifdef RT_OS_DARWIN
75 &coreaudio_audio_driver,
76# endif
77# ifdef RT_OS_WINDOWS
78 &dsound_audio_driver,
79# endif
80# ifdef RT_OS_L4
81 &oss_audio_driver,
82# endif
83 &no_audio_driver
84};
85#else
86static struct audio_driver *drvtab[] = {
87 &oss_audio_driver,
88 NULL, /* alsa_audio_driver is now in a shared object */
89 &no_audio_driver
90};
91#endif
92
93struct fixed_settings {
94 int enabled;
95 int nb_voices;
96 int greedy;
97 audsettings_t settings;
98};
99
100static struct {
101 struct fixed_settings fixed_out;
102 struct fixed_settings fixed_in;
103 union {
104 int hz;
105 int64_t ticks;
106 } period;
107 int plive;
108} conf = {
109 { /* DAC fixed settings */
110 1, /* enabled */
111 1, /* nb_voices */
112 1, /* greedy */
113 {
114 44100, /* freq */
115 2, /* nchannels */
116 AUD_FMT_S16 /* fmt */
117 }
118 },
119
120 { /* ADC fixed settings */
121 1, /* enabled */
122 1, /* nb_voices */
123 1, /* greedy */
124 {
125 44100, /* freq */
126 2, /* nchannels */
127 AUD_FMT_S16 /* fmt */
128 }
129 },
130
131 { 100 }, /* period */
132 0, /* plive */
133};
134
135static AudioState glob_audio_state;
136
137volume_t nominal_volume = {
138 0,
139#ifdef FLOAT_MIXENG
140 1.0,
141 1.0
142#else
143#ifndef VBOX
144 UINT_MAX,
145 UINT_MAX
146#else
147 INT_MAX,
148 INT_MAX
149#endif
150#endif
151};
152
153#ifdef VBOX
154volume_t pcm_out_volume =
155{
156 0,
157 INT_MAX,
158 INT_MAX
159};
160volume_t pcm_in_volume =
161{
162 0,
163 INT_MAX,
164 INT_MAX
165};
166#endif
167
168/* http://www.df.lth.se/~john_e/gems/gem002d.html */
169/* http://www.multi-platforms.com/Tips/PopCount.htm */
170uint32_t popcount (uint32_t u)
171{
172 u = ((u&0x55555555) + ((u>>1)&0x55555555));
173 u = ((u&0x33333333) + ((u>>2)&0x33333333));
174 u = ((u&0x0f0f0f0f) + ((u>>4)&0x0f0f0f0f));
175 u = ((u&0x00ff00ff) + ((u>>8)&0x00ff00ff));
176 u = ( u&0x0000ffff) + (u>>16);
177 return u;
178}
179
180inline uint32_t lsbindex (uint32_t u)
181{
182 return popcount ((u&-u)-1);
183}
184
185uint64_t audio_get_clock (void)
186{
187 AudioState *s;
188
189 s = &glob_audio_state;
190 return s->pDrvIns->pDrvHlp->pfnTMGetVirtualTime (s->pDrvIns);
191}
192
193uint64_t audio_get_ticks_per_sec (void)
194{
195 AudioState *s;
196
197 s = &glob_audio_state;
198 return s->pDrvIns->pDrvHlp->pfnTMGetVirtualFreq (s->pDrvIns);
199}
200
201#ifdef AUDIO_IS_FLAWLESS_AND_NO_CHECKS_ARE_REQURIED
202#error No its not
203#else
204int audio_bug (const char *funcname, int cond)
205{
206 if (cond) {
207 static int shown;
208
209 AUD_log (NULL, "A bug was just triggered in %s\n", funcname);
210 if (!shown) {
211 shown = 1;
212 AUD_log (NULL, "Save all your work and restart without audio\n");
213 AUD_log (NULL, "Please send a bug report to innotek\n");
214 AUD_log (NULL, "I am sorry\n");
215 }
216 AUD_log (NULL, "Context:\n");
217
218#if defined AUDIO_BREAKPOINT_ON_BUG
219# if defined HOST_I386
220# if defined __GNUC__
221 __asm__ ("int3");
222# elif defined _MSC_VER
223 _asm _emit 0xcc;
224# else
225 abort ();
226# endif
227# else
228 abort ();
229# endif
230#endif
231 }
232
233 return cond;
234}
235#endif
236
237void *audio_calloc (const char *funcname, int nmemb, size_t size)
238{
239 int cond;
240 size_t len;
241
242 len = nmemb * size;
243 cond = !nmemb || !size;
244 cond |= nmemb < 0;
245 cond |= len < size;
246
247 if (audio_bug ("audio_calloc", cond)) {
248 AUD_log (NULL, "%s passed invalid arguments to audio_calloc\n",
249 funcname);
250 AUD_log (NULL, "nmemb=%d size=%" FMTZ "u (len=%" FMTZ "u)\n",
251 nmemb, size, len);
252 return NULL;
253 }
254
255 return qemu_mallocz (len);
256}
257
258static const char *audio_audfmt_to_string (audfmt_e fmt)
259{
260 switch (fmt) {
261 case AUD_FMT_U8:
262 return "U8";
263
264 case AUD_FMT_U16:
265 return "U16";
266
267 case AUD_FMT_S8:
268 return "S8";
269
270 case AUD_FMT_S16:
271 return "S16";
272 }
273
274 dolog ("Bogus audfmt %d returning S16\n", fmt);
275 return "S16";
276}
277
278static audfmt_e audio_string_to_audfmt (const char *s, audfmt_e defval,
279 int *defaultp)
280{
281 if (!strcasecmp (s, "u8")) {
282 *defaultp = 0;
283 return AUD_FMT_U8;
284 }
285 else if (!strcasecmp (s, "u16")) {
286 *defaultp = 0;
287 return AUD_FMT_U16;
288 }
289 else if (!strcasecmp (s, "s8")) {
290 *defaultp = 0;
291 return AUD_FMT_S8;
292 }
293 else if (!strcasecmp (s, "s16")) {
294 *defaultp = 0;
295 return AUD_FMT_S16;
296 }
297 else {
298 dolog ("Bogus audio format `%s' using %s\n",
299 s, audio_audfmt_to_string (defval));
300 *defaultp = 1;
301 return defval;
302 }
303}
304
305static audfmt_e audio_get_conf_fmt (const char *envname,
306 audfmt_e defval,
307 int *defaultp)
308{
309 const char *var = getenv (envname);
310 if (!var) {
311 *defaultp = 1;
312 return defval;
313 }
314 return audio_string_to_audfmt (var, defval, defaultp);
315}
316
317static int audio_get_conf_int (const char *key, int defval, int *defaultp)
318{
319 int val;
320 char *strval;
321
322 strval = getenv (key);
323 if (strval) {
324 *defaultp = 0;
325 val = atoi (strval);
326 return val;
327 }
328 else {
329 *defaultp = 1;
330 return defval;
331 }
332}
333
334static const char *audio_get_conf_str (const char *key,
335 const char *defval,
336 int *defaultp)
337{
338 const char *val = getenv (key);
339 if (!val) {
340 *defaultp = 1;
341 return defval;
342 }
343 else {
344 *defaultp = 0;
345 return val;
346 }
347}
348
349void AUD_vlog (const char *cap, const char *fmt, va_list va)
350{
351 va_list va2;
352 va_copy (va2, va); /* Have to make a copy here or GCC will break. */
353 if (cap) {
354 Log (("%s: %N", cap, fmt, &va2));
355 }
356 else {
357 Log (("%N", fmt, &va2));
358 }
359 va_end (va2);
360}
361
362void AUD_log (const char *cap, const char *fmt, ...)
363{
364 va_list va;
365
366 va_start (va, fmt);
367 AUD_vlog (cap, fmt, va);
368 va_end (va);
369}
370
371static void audio_process_options (const char *prefix,
372 struct audio_option *opt)
373{
374 char *optname;
375 const char vbox_prefix[] = "VBOX_";
376 size_t preflen;
377
378 if (audio_bug (AUDIO_FUNC, !prefix)) {
379 dolog ("prefix = NULL\n");
380 return;
381 }
382
383 if (audio_bug (AUDIO_FUNC, !opt)) {
384 dolog ("opt = NULL\n");
385 return;
386 }
387
388 preflen = strlen (prefix);
389
390 for (; opt->name; opt++) {
391 size_t len, i;
392 int def;
393
394 if (!opt->valp) {
395 dolog ("Option value pointer for `%s' is not set\n",
396 opt->name);
397 continue;
398 }
399
400 len = strlen (opt->name);
401 /* len of opt->name + len of prefix + size of vbox_prefix
402 * (includes trailing zero) + zero + underscore (on behalf of
403 * sizeof) */
404 optname = qemu_malloc (len + preflen + sizeof (vbox_prefix) + 1);
405 if (!optname) {
406 dolog ("Could not allocate memory for option name `%s'\n",
407 opt->name);
408 continue;
409 }
410
411 strcpy (optname, vbox_prefix);
412
413 /* copy while upcasing, including trailing zero */
414 for (i = 0; i <= preflen; ++i) {
415 optname[i + sizeof (vbox_prefix) - 1] = toupper (prefix[i]);
416 }
417 strcat (optname, "_");
418 strcat (optname, opt->name);
419
420 def = 1;
421 switch (opt->tag) {
422 case AUD_OPT_BOOL:
423 case AUD_OPT_INT:
424 {
425 int *intp = opt->valp;
426 *intp = audio_get_conf_int (optname, *intp, &def);
427 }
428 break;
429
430 case AUD_OPT_FMT:
431 {
432 audfmt_e *fmtp = opt->valp;
433 *fmtp = audio_get_conf_fmt (optname, *fmtp, &def);
434 }
435 break;
436
437 case AUD_OPT_STR:
438 {
439 const char **strp = opt->valp;
440 *strp = audio_get_conf_str (optname, *strp, &def);
441 }
442 break;
443
444 default:
445 dolog ("Bad value tag for option `%s' - %d\n",
446 optname, opt->tag);
447 break;
448 }
449
450 if (!opt->overridenp) {
451 opt->overridenp = &opt->overriden;
452 }
453 *opt->overridenp = !def;
454 qemu_free (optname);
455 }
456}
457
458static void audio_print_settings (audsettings_t *as)
459{
460 dolog ("frequency=%d nchannels=%d fmt=", as->freq, as->nchannels);
461
462 switch (as->fmt) {
463 case AUD_FMT_S8:
464 AUD_log (NULL, "S8");
465 break;
466 case AUD_FMT_U8:
467 AUD_log (NULL, "U8");
468 break;
469 case AUD_FMT_S16:
470 AUD_log (NULL, "S16");
471 break;
472 case AUD_FMT_U16:
473 AUD_log (NULL, "U16");
474 break;
475 default:
476 AUD_log (NULL, "invalid(%d)", as->fmt);
477 break;
478 }
479
480 AUD_log (NULL, " endianness=");
481 switch (as->endianness) {
482 case 0:
483 AUD_log (NULL, "little");
484 break;
485 case 1:
486 AUD_log (NULL, "big");
487 break;
488 default:
489 AUD_log (NULL, "invalid");
490 break;
491 }
492 AUD_log (NULL, "\n");
493}
494
495static int audio_validate_settings (audsettings_t *as)
496{
497 int invalid;
498
499 invalid = as->nchannels != 1 && as->nchannels != 2;
500 invalid |= as->endianness != 0 && as->endianness != 1;
501
502 switch (as->fmt) {
503 case AUD_FMT_S8:
504 case AUD_FMT_U8:
505 case AUD_FMT_S16:
506 case AUD_FMT_U16:
507 break;
508 default:
509 invalid = 1;
510 break;
511 }
512
513 invalid |= as->freq <= 0;
514 return invalid ? -1 : 0;
515}
516
517static int audio_pcm_info_eq (struct audio_pcm_info *info, audsettings_t *as)
518{
519 int bits = 8, sign = 0;
520
521 switch (as->fmt) {
522 case AUD_FMT_S8:
523 sign = 1;
524 case AUD_FMT_U8:
525 break;
526
527 case AUD_FMT_S16:
528 sign = 1;
529 case AUD_FMT_U16:
530 bits = 16;
531 break;
532 }
533 return info->freq == as->freq
534 && info->nchannels == as->nchannels
535 && info->sign == sign
536 && info->bits == bits
537 && info->swap_endianness == (as->endianness != AUDIO_HOST_ENDIANNESS);
538}
539
540void audio_pcm_init_info (struct audio_pcm_info *info, audsettings_t *as)
541{
542 int bits = 8, sign = 0;
543
544 switch (as->fmt) {
545 case AUD_FMT_S8:
546 sign = 1;
547 case AUD_FMT_U8:
548 break;
549
550 case AUD_FMT_S16:
551 sign = 1;
552 case AUD_FMT_U16:
553 bits = 16;
554 break;
555 }
556
557 info->freq = as->freq;
558 info->bits = bits;
559 info->sign = sign;
560 info->nchannels = as->nchannels;
561 info->shift = (as->nchannels == 2) + (bits == 16);
562 info->align = (1 << info->shift) - 1;
563 info->bytes_per_second = info->freq << info->shift;
564 info->swap_endianness = (as->endianness != AUDIO_HOST_ENDIANNESS);
565}
566
567void audio_pcm_info_clear_buf (struct audio_pcm_info *info, void *buf, int len)
568{
569 if (!len) {
570 return;
571 }
572
573 if (info->sign) {
574 memset (buf, 0x00, len << info->shift);
575 }
576 else {
577 if (info->bits == 8) {
578 memset (buf, 0x80, len << info->shift);
579 }
580 else {
581 int i;
582 uint16_t *p = buf;
583 int shift = info->nchannels - 1;
584 short s = INT16_MAX;
585
586 if (info->swap_endianness) {
587 s = bswap16 (s);
588 }
589
590 for (i = 0; i < len << shift; i++) {
591 p[i] = s;
592 }
593 }
594 }
595}
596
597/*
598 * Capture
599 */
600static void noop_conv (st_sample_t *dst, const void *src,
601 int samples, volume_t *vol)
602{
603 (void) src;
604 (void) dst;
605 (void) samples;
606 (void) vol;
607}
608
609static CaptureVoiceOut *audio_pcm_capture_find_specific (
610 AudioState *s,
611 audsettings_t *as
612 )
613{
614 CaptureVoiceOut *cap;
615
616 for (cap = s->cap_head.lh_first; cap; cap = cap->entries.le_next) {
617 if (audio_pcm_info_eq (&cap->hw.info, as)) {
618 return cap;
619 }
620 }
621 return NULL;
622}
623
624static void audio_notify_capture (CaptureVoiceOut *cap, audcnotification_e cmd)
625{
626 struct capture_callback *cb;
627
628#ifdef DEBUG_CAPTURE
629 dolog ("notification %d sent\n", cmd);
630#endif
631 for (cb = cap->cb_head.lh_first; cb; cb = cb->entries.le_next) {
632 cb->ops.notify (cb->opaque, cmd);
633 }
634}
635
636static void audio_capture_maybe_changed (CaptureVoiceOut *cap, int enabled)
637{
638 if (cap->hw.enabled != enabled) {
639 audcnotification_e cmd;
640 cap->hw.enabled = enabled;
641 cmd = enabled ? AUD_CNOTIFY_ENABLE : AUD_CNOTIFY_DISABLE;
642 audio_notify_capture (cap, cmd);
643 }
644}
645
646static void audio_recalc_and_notify_capture (CaptureVoiceOut *cap)
647{
648 HWVoiceOut *hw = &cap->hw;
649 SWVoiceOut *sw;
650 int enabled = 0;
651
652 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
653 if (sw->active) {
654 enabled = 1;
655 break;
656 }
657 }
658 audio_capture_maybe_changed (cap, enabled);
659}
660
661static void audio_detach_capture (HWVoiceOut *hw)
662{
663 SWVoiceCap *sc = hw->cap_head.lh_first;
664
665 while (sc) {
666 SWVoiceCap *sc1 = sc->entries.le_next;
667 SWVoiceOut *sw = &sc->sw;
668 CaptureVoiceOut *cap = sc->cap;
669 int was_active = sw->active;
670
671 if (sw->rate) {
672 st_rate_stop (sw->rate);
673 sw->rate = NULL;
674 }
675
676 LIST_REMOVE (sw, entries);
677 LIST_REMOVE (sc, entries);
678 qemu_free (sc);
679 if (was_active) {
680 /* We have removed soft voice from the capture:
681 this might have changed the overall status of the capture
682 since this might have been the only active voice */
683 audio_recalc_and_notify_capture (cap);
684 }
685 sc = sc1;
686 }
687}
688
689static int audio_attach_capture (AudioState *s, HWVoiceOut *hw)
690{
691 CaptureVoiceOut *cap;
692
693 audio_detach_capture (hw);
694 for (cap = s->cap_head.lh_first; cap; cap = cap->entries.le_next) {
695 SWVoiceCap *sc;
696 SWVoiceOut *sw;
697 HWVoiceOut *hw_cap = &cap->hw;
698
699 sc = audio_calloc (AUDIO_FUNC, 1, sizeof (*sc));
700 if (!sc) {
701 dolog ("Could not allocate soft capture voice (%u bytes)\n",
702 sizeof (*sc));
703 return -1;
704 }
705
706 sc->cap = cap;
707 sw = &sc->sw;
708 sw->hw = hw_cap;
709 sw->info = hw->info;
710 sw->empty = 1;
711 sw->active = hw->enabled;
712 sw->conv = noop_conv;
713 sw->ratio = ((int64_t) hw_cap->info.freq << 32) / sw->info.freq;
714 sw->rate = st_rate_start (sw->info.freq, hw_cap->info.freq);
715 if (!sw->rate) {
716 dolog ("Could not start rate conversion for `%s'\n", SW_NAME (sw));
717 qemu_free (sw);
718 return -1;
719 }
720 LIST_INSERT_HEAD (&hw_cap->sw_head, sw, entries);
721 LIST_INSERT_HEAD (&hw->cap_head, sc, entries);
722#ifdef DEBUG_CAPTURE
723 asprintf (&sw->name, "for %p %d,%d,%d",
724 hw, sw->info.freq, sw->info.bits, sw->info.nchannels);
725 dolog ("Added %s active = %d\n", sw->name, sw->active);
726#endif
727 if (sw->active) {
728 audio_capture_maybe_changed (cap, 1);
729 }
730 }
731 return 0;
732}
733
734/*
735 * Hard voice (capture)
736 */
737static int audio_pcm_hw_find_min_in (HWVoiceIn *hw)
738{
739 SWVoiceIn *sw;
740 int m = hw->total_samples_captured;
741
742 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
743 if (sw->active) {
744 m = audio_MIN (m, sw->total_hw_samples_acquired);
745 }
746 }
747 return m;
748}
749
750int audio_pcm_hw_get_live_in (HWVoiceIn *hw)
751{
752 int live = hw->total_samples_captured - audio_pcm_hw_find_min_in (hw);
753 if (audio_bug (AUDIO_FUNC, live < 0 || live > hw->samples)) {
754 dolog ("live=%d hw->samples=%d\n", live, hw->samples);
755 return 0;
756 }
757 return live;
758}
759
760/*
761 * Soft voice (capture)
762 */
763static int audio_pcm_sw_get_rpos_in (SWVoiceIn *sw)
764{
765 HWVoiceIn *hw = sw->hw;
766 int live = hw->total_samples_captured - sw->total_hw_samples_acquired;
767 int rpos;
768
769 if (audio_bug (AUDIO_FUNC, live < 0 || live > hw->samples)) {
770 dolog ("live=%d hw->samples=%d\n", live, hw->samples);
771 return 0;
772 }
773
774 rpos = hw->wpos - live;
775 if (rpos >= 0) {
776 return rpos;
777 }
778 else {
779 return hw->samples + rpos;
780 }
781}
782
783int audio_pcm_sw_read (SWVoiceIn *sw, void *buf, int size)
784{
785 HWVoiceIn *hw = sw->hw;
786 int samples, live, ret = 0, swlim, isamp, osamp, rpos, total = 0;
787 st_sample_t *src, *dst = sw->buf;
788
789 rpos = audio_pcm_sw_get_rpos_in (sw) % hw->samples;
790
791 live = hw->total_samples_captured - sw->total_hw_samples_acquired;
792 if (audio_bug (AUDIO_FUNC, live < 0 || live > hw->samples)) {
793 dolog ("live_in=%d hw->samples=%d\n", live, hw->samples);
794 return 0;
795 }
796
797 samples = size >> sw->info.shift;
798 if (!live) {
799 return 0;
800 }
801
802 swlim = (live * sw->ratio) >> 32;
803 swlim = audio_MIN (swlim, samples);
804
805 while (swlim) {
806 src = hw->conv_buf + rpos;
807 isamp = hw->wpos - rpos;
808 /* XXX: <= ? */
809 if (isamp <= 0) {
810 isamp = hw->samples - rpos;
811 }
812
813 if (!isamp) {
814 break;
815 }
816 osamp = swlim;
817
818 if (audio_bug (AUDIO_FUNC, osamp < 0)) {
819 dolog ("osamp=%d\n", osamp);
820 return 0;
821 }
822
823 st_rate_flow (sw->rate, src, dst, &isamp, &osamp);
824 swlim -= osamp;
825 rpos = (rpos + isamp) % hw->samples;
826 dst += osamp;
827 ret += osamp;
828 total += isamp;
829 }
830
831 sw->clip (buf, sw->buf, ret);
832 sw->total_hw_samples_acquired += total;
833 return ret << sw->info.shift;
834}
835
836/*
837 * Hard voice (playback)
838 */
839static int audio_pcm_hw_find_min_out (HWVoiceOut *hw, int *nb_livep)
840{
841 SWVoiceOut *sw;
842 int m = INT_MAX;
843 int nb_live = 0;
844
845 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
846 if (sw->active || !sw->empty) {
847 m = audio_MIN (m, sw->total_hw_samples_mixed);
848 nb_live += 1;
849 }
850 }
851
852 *nb_livep = nb_live;
853 return m;
854}
855
856int audio_pcm_hw_get_live_out2 (HWVoiceOut *hw, int *nb_live)
857{
858 int smin;
859
860 smin = audio_pcm_hw_find_min_out (hw, nb_live);
861
862 if (!*nb_live) {
863 return 0;
864 }
865 else {
866 int live = smin;
867
868 if (audio_bug (AUDIO_FUNC, live < 0 || live > hw->samples)) {
869 dolog ("live=%d hw->samples=%d\n", live, hw->samples);
870 return 0;
871 }
872 return live;
873 }
874}
875
876int audio_pcm_hw_get_live_out (HWVoiceOut *hw)
877{
878 int nb_live;
879 int live;
880
881 live = audio_pcm_hw_get_live_out2 (hw, &nb_live);
882 if (audio_bug (AUDIO_FUNC, live < 0 || live > hw->samples)) {
883 dolog ("live=%d hw->samples=%d\n", live, hw->samples);
884 return 0;
885 }
886 return live;
887}
888
889/*
890 * Soft voice (playback)
891 */
892int audio_pcm_sw_write (SWVoiceOut *sw, void *buf, int size)
893{
894 int hwsamples, samples, isamp, osamp, wpos, live, dead, left, swlim, blck;
895 int ret = 0, pos = 0, total = 0;
896
897 if (!sw) {
898 return size;
899 }
900
901 hwsamples = sw->hw->samples;
902
903 live = sw->total_hw_samples_mixed;
904 if (audio_bug (AUDIO_FUNC, live < 0 || live > hwsamples)){
905 dolog ("live=%d hw->samples=%d\n", live, hwsamples);
906 return 0;
907 }
908
909 if (live == hwsamples) {
910#ifdef DEBUG_OUT
911 dolog ("%s is full %d\n", sw->name, live);
912#endif
913 return 0;
914 }
915
916 wpos = (sw->hw->rpos + live) % hwsamples;
917 samples = size >> sw->info.shift;
918
919 dead = hwsamples - live;
920 swlim = ((int64_t) dead << 32) / sw->ratio;
921 swlim = audio_MIN (swlim, samples);
922 if (swlim) {
923#ifndef VBOX
924 sw->conv (sw->buf, buf, swlim, &sw->vol);
925#else
926 sw->conv (sw->buf, buf, swlim, &pcm_out_volume);
927#endif
928 }
929
930 while (swlim) {
931 dead = hwsamples - live;
932 left = hwsamples - wpos;
933 blck = audio_MIN (dead, left);
934 if (!blck) {
935 break;
936 }
937 isamp = swlim;
938 osamp = blck;
939 st_rate_flow_mix (
940 sw->rate,
941 sw->buf + pos,
942 sw->hw->mix_buf + wpos,
943 &isamp,
944 &osamp
945 );
946 ret += isamp;
947 swlim -= isamp;
948 pos += isamp;
949 live += osamp;
950 wpos = (wpos + osamp) % hwsamples;
951 total += osamp;
952 }
953
954 sw->total_hw_samples_mixed += total;
955 sw->empty = sw->total_hw_samples_mixed == 0;
956
957#ifdef DEBUG_OUT
958 dolog (
959 "%s: write size %d ret %d total sw %d\n",
960 SW_NAME (sw),
961 size >> sw->info.shift,
962 ret,
963 sw->total_hw_samples_mixed
964 );
965#endif
966
967 return ret << sw->info.shift;
968}
969
970#ifdef DEBUG_AUDIO
971static void audio_pcm_print_info (const char *cap, struct audio_pcm_info *info)
972{
973 dolog ("%s: bits %d, sign %d, freq %d, nchan %d\n",
974 cap, info->bits, info->sign, info->freq, info->nchannels);
975}
976#endif
977
978#define DAC
979#include "audio_template.h"
980#undef DAC
981#include "audio_template.h"
982
983int AUD_write (SWVoiceOut *sw, void *buf, int size)
984{
985 int bytes;
986
987 if (!sw) {
988 /* XXX: Consider options */
989 return size;
990 }
991
992 if (!sw->hw->enabled) {
993 dolog ("Writing to disabled voice %s\n", SW_NAME (sw));
994 return 0;
995 }
996
997 bytes = sw->hw->pcm_ops->write (sw, buf, size);
998 return bytes;
999}
1000
1001int AUD_read (SWVoiceIn *sw, void *buf, int size)
1002{
1003 int bytes;
1004
1005 if (!sw) {
1006 /* XXX: Consider options */
1007 return size;
1008 }
1009
1010 if (!sw->hw->enabled) {
1011 dolog ("Reading from disabled voice %s\n", SW_NAME (sw));
1012 return 0;
1013 }
1014
1015 bytes = sw->hw->pcm_ops->read (sw, buf, size);
1016 return bytes;
1017}
1018
1019int AUD_get_buffer_size_out (SWVoiceOut *sw)
1020{
1021 return sw->hw->samples << sw->hw->info.shift;
1022}
1023
1024void AUD_set_active_out (SWVoiceOut *sw, int on)
1025{
1026 HWVoiceOut *hw;
1027
1028 if (!sw) {
1029 return;
1030 }
1031
1032 hw = sw->hw;
1033 if (sw->active != on) {
1034 SWVoiceOut *temp_sw;
1035 SWVoiceCap *sc;
1036
1037 if (on) {
1038 hw->pending_disable = 0;
1039 if (!hw->enabled) {
1040 hw->enabled = 1;
1041 hw->pcm_ops->ctl_out (hw, VOICE_ENABLE);
1042 }
1043 }
1044 else {
1045 if (hw->enabled) {
1046 int nb_active = 0;
1047
1048 for (temp_sw = hw->sw_head.lh_first; temp_sw;
1049 temp_sw = temp_sw->entries.le_next) {
1050 nb_active += temp_sw->active != 0;
1051 }
1052
1053 hw->pending_disable = nb_active == 1;
1054 }
1055 }
1056
1057 for (sc = hw->cap_head.lh_first; sc; sc = sc->entries.le_next) {
1058 sc->sw.active = hw->enabled;
1059 if (hw->enabled) {
1060 audio_capture_maybe_changed (sc->cap, 1);
1061 }
1062 }
1063 sw->active = on;
1064 }
1065}
1066
1067void AUD_set_active_in (SWVoiceIn *sw, int on)
1068{
1069 HWVoiceIn *hw;
1070
1071 if (!sw) {
1072 return;
1073 }
1074
1075 hw = sw->hw;
1076 if (sw->active != on) {
1077 SWVoiceIn *temp_sw;
1078
1079 if (on) {
1080 if (!hw->enabled) {
1081 hw->enabled = 1;
1082 hw->pcm_ops->ctl_in (hw, VOICE_ENABLE);
1083 }
1084 sw->total_hw_samples_acquired = hw->total_samples_captured;
1085 }
1086 else {
1087 if (hw->enabled) {
1088 int nb_active = 0;
1089
1090 for (temp_sw = hw->sw_head.lh_first; temp_sw;
1091 temp_sw = temp_sw->entries.le_next) {
1092 nb_active += temp_sw->active != 0;
1093 }
1094
1095 if (nb_active == 1) {
1096 hw->enabled = 0;
1097 hw->pcm_ops->ctl_in (hw, VOICE_DISABLE);
1098 }
1099 }
1100 }
1101 sw->active = on;
1102 }
1103}
1104
1105static int audio_get_avail (SWVoiceIn *sw)
1106{
1107 int live;
1108
1109 if (!sw) {
1110 return 0;
1111 }
1112
1113 live = sw->hw->total_samples_captured - sw->total_hw_samples_acquired;
1114 if (audio_bug (AUDIO_FUNC, live < 0 || live > sw->hw->samples)) {
1115 dolog ("live=%d sw->hw->samples=%d\n", live, sw->hw->samples);
1116 return 0;
1117 }
1118
1119 ldebug (
1120 "%s: get_avail live %d ret %lld\n",
1121 SW_NAME (sw),
1122 live, (((int64_t) live << 32) / sw->ratio) << sw->info.shift
1123 );
1124
1125 return (((int64_t) live << 32) / sw->ratio) << sw->info.shift;
1126}
1127
1128static int audio_get_free (SWVoiceOut *sw)
1129{
1130 int live, dead;
1131
1132 if (!sw) {
1133 return 0;
1134 }
1135
1136 live = sw->total_hw_samples_mixed;
1137
1138 if (audio_bug (AUDIO_FUNC, live < 0 || live > sw->hw->samples)) {
1139 dolog ("live=%d sw->hw->samples=%d\n", live, sw->hw->samples);
1140 return 0;
1141 }
1142
1143 dead = sw->hw->samples - live;
1144
1145#ifdef DEBUG_OUT
1146 dolog ("%s: get_free live %d dead %d ret %lld\n",
1147 SW_NAME (sw),
1148 live, dead, (((int64_t) dead << 32) / sw->ratio) << sw->info.shift);
1149#endif
1150
1151 return (((int64_t) dead << 32) / sw->ratio) << sw->info.shift;
1152}
1153
1154static void audio_capture_mix_and_clear (HWVoiceOut *hw, int rpos, int samples)
1155{
1156 int n;
1157
1158 if (hw->enabled) {
1159 SWVoiceCap *sc;
1160
1161 for (sc = hw->cap_head.lh_first; sc; sc = sc->entries.le_next) {
1162 SWVoiceOut *sw = &sc->sw;
1163 int rpos2 = rpos;
1164
1165 n = samples;
1166 while (n) {
1167 int till_end_of_hw = hw->samples - rpos2;
1168 int to_write = audio_MIN (till_end_of_hw, n);
1169 int bytes = to_write << hw->info.shift;
1170 int written;
1171
1172 sw->buf = hw->mix_buf + rpos2;
1173 written = audio_pcm_sw_write (sw, NULL, bytes);
1174 if (written - bytes) {
1175 dolog ("Could not mix %d bytes into a capture "
1176 "buffer, mixed %d\n",
1177 bytes, written);
1178 break;
1179 }
1180 n -= to_write;
1181 rpos2 = (rpos2 + to_write) % hw->samples;
1182 }
1183 }
1184 }
1185
1186 n = audio_MIN (samples, hw->samples - rpos);
1187 mixeng_sniff_and_clear (hw, hw->mix_buf + rpos, n);
1188 mixeng_sniff_and_clear (hw, hw->mix_buf, samples - n);
1189}
1190
1191static void audio_run_out (AudioState *s)
1192{
1193 HWVoiceOut *hw = NULL;
1194 SWVoiceOut *sw;
1195
1196 while ((hw = audio_pcm_hw_find_any_enabled_out (s, hw))) {
1197 int played;
1198 int live, free, nb_live, cleanup_required, prev_rpos;
1199
1200 live = audio_pcm_hw_get_live_out2 (hw, &nb_live);
1201 if (!nb_live) {
1202 live = 0;
1203 }
1204
1205 if (audio_bug (AUDIO_FUNC, live < 0 || live > hw->samples)) {
1206 dolog ("live=%d hw->samples=%d\n", live, hw->samples);
1207 continue;
1208 }
1209
1210 if (hw->pending_disable && !nb_live) {
1211 SWVoiceCap *sc;
1212#ifdef DEBUG_OUT
1213 dolog ("Disabling voice\n");
1214#endif
1215 hw->enabled = 0;
1216 hw->pending_disable = 0;
1217 hw->pcm_ops->ctl_out (hw, VOICE_DISABLE);
1218 for (sc = hw->cap_head.lh_first; sc; sc = sc->entries.le_next) {
1219 sc->sw.active = 0;
1220 audio_recalc_and_notify_capture (sc->cap);
1221 }
1222 continue;
1223 }
1224
1225 if (!live) {
1226 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
1227 if (sw->active) {
1228 free = audio_get_free (sw);
1229 if (free > 0) {
1230 sw->callback.fn (sw->callback.opaque, free);
1231 }
1232 }
1233 }
1234 continue;
1235 }
1236
1237 prev_rpos = hw->rpos;
1238 played = hw->pcm_ops->run_out (hw);
1239 if (audio_bug (AUDIO_FUNC, hw->rpos >= hw->samples)) {
1240 dolog ("hw->rpos=%d hw->samples=%d played=%d\n",
1241 hw->rpos, hw->samples, played);
1242 hw->rpos = 0;
1243 }
1244
1245#ifdef DEBUG_OUT
1246 dolog ("played=%d\n", played);
1247#endif
1248
1249 if (played) {
1250 hw->ts_helper += played;
1251 audio_capture_mix_and_clear (hw, prev_rpos, played);
1252 }
1253
1254 cleanup_required = 0;
1255 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
1256 if (!sw->active && sw->empty) {
1257 continue;
1258 }
1259
1260 if (audio_bug (AUDIO_FUNC, played > sw->total_hw_samples_mixed)) {
1261 dolog ("played=%d sw->total_hw_samples_mixed=%d\n",
1262 played, sw->total_hw_samples_mixed);
1263 played = sw->total_hw_samples_mixed;
1264 }
1265
1266 sw->total_hw_samples_mixed -= played;
1267
1268 if (!sw->total_hw_samples_mixed) {
1269 sw->empty = 1;
1270 cleanup_required |= !sw->active && !sw->callback.fn;
1271 }
1272
1273 if (sw->active) {
1274 free = audio_get_free (sw);
1275 if (free > 0) {
1276 sw->callback.fn (sw->callback.opaque, free);
1277 }
1278 }
1279 }
1280
1281 if (cleanup_required) {
1282 SWVoiceOut *sw1;
1283
1284 sw = hw->sw_head.lh_first;
1285 while (sw) {
1286 sw1 = sw->entries.le_next;
1287 if (!sw->active && !sw->callback.fn) {
1288#ifdef DEBUG_PLIVE
1289 dolog ("Finishing with old voice\n");
1290#endif
1291 audio_close_out (s, sw);
1292 }
1293 sw = sw1;
1294 }
1295 }
1296 }
1297}
1298
1299static void audio_run_in (AudioState *s)
1300{
1301 HWVoiceIn *hw = NULL;
1302
1303 while ((hw = audio_pcm_hw_find_any_enabled_in (s, hw))) {
1304 SWVoiceIn *sw;
1305 int captured, min;
1306
1307 captured = hw->pcm_ops->run_in (hw);
1308
1309 min = audio_pcm_hw_find_min_in (hw);
1310 hw->total_samples_captured += captured - min;
1311 hw->ts_helper += captured;
1312
1313 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
1314 sw->total_hw_samples_acquired -= min;
1315
1316 if (sw->active) {
1317 int avail;
1318
1319 avail = audio_get_avail (sw);
1320 if (avail > 0) {
1321 sw->callback.fn (sw->callback.opaque, avail);
1322 }
1323 }
1324 }
1325 }
1326}
1327
1328static void audio_run_capture (AudioState *s)
1329{
1330 CaptureVoiceOut *cap;
1331
1332 for (cap = s->cap_head.lh_first; cap; cap = cap->entries.le_next) {
1333 int live, rpos, captured;
1334 HWVoiceOut *hw = &cap->hw;
1335 SWVoiceOut *sw;
1336
1337 captured = live = audio_pcm_hw_get_live_out (hw);
1338 rpos = hw->rpos;
1339 while (live) {
1340 int left = hw->samples - rpos;
1341 int to_capture = audio_MIN (live, left);
1342 st_sample_t *src;
1343 struct capture_callback *cb;
1344
1345 src = hw->mix_buf + rpos;
1346 hw->clip (cap->buf, src, to_capture);
1347 mixeng_sniff_and_clear (hw, src, to_capture);
1348
1349 for (cb = cap->cb_head.lh_first; cb; cb = cb->entries.le_next) {
1350 cb->ops.capture (cb->opaque, cap->buf,
1351 to_capture << hw->info.shift);
1352 }
1353 rpos = (rpos + to_capture) % hw->samples;
1354 live -= to_capture;
1355 }
1356 hw->rpos = rpos;
1357
1358 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
1359 if (!sw->active && sw->empty) {
1360 continue;
1361 }
1362
1363 if (audio_bug (AUDIO_FUNC, captured > sw->total_hw_samples_mixed)) {
1364 dolog ("captured=%d sw->total_hw_samples_mixed=%d\n",
1365 captured, sw->total_hw_samples_mixed);
1366 captured = sw->total_hw_samples_mixed;
1367 }
1368
1369 sw->total_hw_samples_mixed -= captured;
1370 sw->empty = sw->total_hw_samples_mixed == 0;
1371 }
1372 }
1373}
1374
1375static void audio_timer (void *opaque)
1376{
1377 AudioState *s = opaque;
1378
1379 audio_run_out (s);
1380 audio_run_in (s);
1381 audio_run_capture (s);
1382
1383 TMTimerSet (s->ts, TMTimerGet (s->ts) + conf.period.ticks);
1384}
1385
1386static struct audio_option audio_options[] = {
1387 /* DAC */
1388 {"DAC_FIXED_SETTINGS", AUD_OPT_BOOL, &conf.fixed_out.enabled,
1389 "Use fixed settings for host DAC", NULL, 0},
1390
1391 {"DAC_FIXED_FREQ", AUD_OPT_INT, &conf.fixed_out.settings.freq,
1392 "Frequency for fixed host DAC", NULL, 0},
1393
1394 {"DAC_FIXED_FMT", AUD_OPT_FMT, &conf.fixed_out.settings.fmt,
1395 "Format for fixed host DAC", NULL, 0},
1396
1397 {"DAC_FIXED_CHANNELS", AUD_OPT_INT, &conf.fixed_out.settings.nchannels,
1398 "Number of channels for fixed DAC (1 - mono, 2 - stereo)", NULL, 0},
1399
1400 {"DAC_VOICES", AUD_OPT_INT, &conf.fixed_out.nb_voices,
1401 "Number of voices for DAC", NULL, 0},
1402
1403 /* ADC */
1404 {"ADC_FIXED_SETTINGS", AUD_OPT_BOOL, &conf.fixed_in.enabled,
1405 "Use fixed settings for host ADC", NULL, 0},
1406
1407 {"ADC_FIXED_FREQ", AUD_OPT_INT, &conf.fixed_in.settings.freq,
1408 "Frequency for fixed host ADC", NULL, 0},
1409
1410 {"ADC_FIXED_FMT", AUD_OPT_FMT, &conf.fixed_in.settings.fmt,
1411 "Format for fixed host ADC", NULL, 0},
1412
1413 {"ADC_FIXED_CHANNELS", AUD_OPT_INT, &conf.fixed_in.settings.nchannels,
1414 "Number of channels for fixed ADC (1 - mono, 2 - stereo)", NULL, 0},
1415
1416 {"ADC_VOICES", AUD_OPT_INT, &conf.fixed_in.nb_voices,
1417 "Number of voices for ADC", NULL, 0},
1418
1419 /* Misc */
1420 {"TIMER_PERIOD", AUD_OPT_INT, &conf.period.hz,
1421 "Timer period in HZ (0 - use lowest possible)", NULL, 0},
1422
1423 {"PLIVE", AUD_OPT_BOOL, &conf.plive,
1424 "(undocumented)", NULL, 0},
1425
1426 {NULL, 0, NULL, NULL, NULL, 0}
1427};
1428
1429static int audio_driver_init (AudioState *s, struct audio_driver *drv)
1430{
1431 if (drv->options) {
1432 audio_process_options (drv->name, drv->options);
1433 }
1434 s->drv_opaque = drv->init ();
1435
1436 if (s->drv_opaque) {
1437 audio_init_nb_voices_out (s, drv);
1438 audio_init_nb_voices_in (s, drv);
1439 s->drv = drv;
1440 return 0;
1441 }
1442 else {
1443 dolog ("Could not init `%s' audio driver\n", drv->name);
1444 return -1;
1445 }
1446}
1447
1448static void audio_vm_change_state_handler (void *opaque, int running)
1449{
1450 AudioState *s = opaque;
1451 HWVoiceOut *hwo = NULL;
1452 HWVoiceIn *hwi = NULL;
1453 int op = running ? VOICE_ENABLE : VOICE_DISABLE;
1454
1455 while ((hwo = audio_pcm_hw_find_any_enabled_out (s, hwo))) {
1456 hwo->pcm_ops->ctl_out (hwo, op);
1457 }
1458
1459 while ((hwi = audio_pcm_hw_find_any_enabled_in (s, hwi))) {
1460 hwi->pcm_ops->ctl_in (hwi, op);
1461 }
1462}
1463
1464static void audio_atexit (void)
1465{
1466 AudioState *s = &glob_audio_state;
1467 HWVoiceOut *hwo = NULL;
1468 HWVoiceIn *hwi = NULL;
1469
1470 while ((hwo = audio_pcm_hw_find_any_enabled_out (s, hwo))) {
1471 SWVoiceCap *sc;
1472
1473 hwo->pcm_ops->ctl_out (hwo, VOICE_DISABLE);
1474 hwo->pcm_ops->fini_out (hwo);
1475
1476 for (sc = hwo->cap_head.lh_first; sc; sc = sc->entries.le_next) {
1477 CaptureVoiceOut *cap = sc->cap;
1478 struct capture_callback *cb;
1479
1480 for (cb = cap->cb_head.lh_first; cb; cb = cb->entries.le_next) {
1481 cb->ops.destroy (cb->opaque);
1482 }
1483 }
1484 }
1485
1486 while ((hwi = audio_pcm_hw_find_any_enabled_in (s, hwi))) {
1487 hwi->pcm_ops->ctl_in (hwi, VOICE_DISABLE);
1488 hwi->pcm_ops->fini_in (hwi);
1489 }
1490
1491 if (s->drv) {
1492 s->drv->fini (s->drv_opaque);
1493 }
1494}
1495
1496void AUD_register_card (const char *name, QEMUSoundCard *card)
1497{
1498 AudioState *s = &glob_audio_state;
1499 card->audio = s;
1500 card->name = qemu_strdup (name);
1501 memset (&card->entries, 0, sizeof (card->entries));
1502 LIST_INSERT_HEAD (&s->card_head, card, entries);
1503}
1504
1505void AUD_remove_card (QEMUSoundCard *card)
1506{
1507 LIST_REMOVE (card, entries);
1508 card->audio = NULL;
1509 qemu_free (card->name);
1510}
1511
1512static void audio_timer_helper (PPDMDRVINS pDrvIns, PTMTIMER pTimer)
1513{
1514 AudioState *s = &glob_audio_state;
1515 audio_timer (s);
1516}
1517
1518static int AUD_init (PPDMDRVINS pDrvIns, const char *drvname)
1519{
1520 size_t i;
1521 int done = 0;
1522 AudioState *s = &glob_audio_state;
1523 int rc;
1524
1525 LIST_INIT (&s->hw_head_out);
1526 LIST_INIT (&s->hw_head_in);
1527 LIST_INIT (&s->cap_head);
1528
1529 rc = pDrvIns->pDrvHlp->pfnTMTimerCreate (pDrvIns, TMCLOCK_VIRTUAL,
1530 audio_timer_helper, "Audio timer", &s->ts);
1531 if (VBOX_FAILURE (rc))
1532 return rc;
1533
1534 audio_process_options ("AUDIO", audio_options);
1535
1536 s->nb_hw_voices_out = conf.fixed_out.nb_voices;
1537 s->nb_hw_voices_in = conf.fixed_in.nb_voices;
1538
1539 if (s->nb_hw_voices_out <= 0) {
1540 dolog ("Bogus number of playback voices %d, setting to 1\n",
1541 s->nb_hw_voices_out);
1542 s->nb_hw_voices_out = 1;
1543 }
1544
1545 if (s->nb_hw_voices_in <= 0) {
1546 dolog ("Bogus number of capture voices %d, setting to 0\n",
1547 s->nb_hw_voices_in);
1548 s->nb_hw_voices_in = 0;
1549 }
1550
1551 LogRel(("Audio: Trying driver '%s'.\n", drvname));
1552
1553 if (drvname) {
1554 int found = 0;
1555
1556 for (i = 0; i < sizeof (drvtab) / sizeof (drvtab[0]); i++) {
1557 /* The ALSA entry can be null if we couldn't load the library */
1558 if (drvtab[i] != NULL) {
1559 if (!strcmp (drvname, drvtab[i]->name)) {
1560 done = !audio_driver_init (s, drvtab[i]);
1561 found = 1;
1562 break;
1563 }
1564 }
1565 }
1566
1567 if (!found) {
1568 dolog ("Unknown audio driver `%s'\n", drvname);
1569 }
1570 }
1571
1572 if (!done) {
1573 for (i = 0; !done && i < sizeof (drvtab) / sizeof (drvtab[0]); i++) {
1574 if (drvtab[i] != NULL) {
1575 if (drvtab[i]->can_be_default) {
1576 LogRel(("Audio: Initialization of driver '%s' failed, trying '%s'.\n",
1577 drvname, drvtab[i]->name));
1578 drvname = drvtab[i]->name;
1579 done = !audio_driver_init (s, drvtab[i]);
1580 }
1581 }
1582 }
1583 }
1584
1585 if (!done) {
1586 done = !audio_driver_init (s, &no_audio_driver);
1587 if (!done) {
1588 dolog ("Could not initialize audio subsystem\n");
1589 }
1590 else {
1591 LogRel(("Audio: Initialization of driver '%s' failed, using NULL driver.\n", drvname));
1592 dolog ("warning: Using timer based audio emulation\n");
1593 }
1594 }
1595
1596 if (done) {
1597 if (conf.period.hz <= 0) {
1598 if (conf.period.hz < 0) {
1599 dolog ("warning: Timer period is negative - %d "
1600 "treating as zero\n",
1601 conf.period.hz);
1602 }
1603 conf.period.ticks = 1;
1604 }
1605 else {
1606 conf.period.ticks = pDrvIns->pDrvHlp->pfnTMGetVirtualFreq (pDrvIns)
1607 / conf.period.hz;
1608 }
1609 }
1610 else {
1611 /* XXX */
1612 rc = TMTimerDestroy (s->ts);
1613 return rc;
1614 }
1615
1616 LIST_INIT (&s->card_head);
1617 TMTimerSet (s->ts, TMTimerGet (s->ts) + conf.period.ticks);
1618 return VINF_SUCCESS;
1619}
1620
1621int AUD_init_null(void)
1622{
1623 AudioState *s = &glob_audio_state;
1624
1625 return audio_driver_init (s, &no_audio_driver);
1626}
1627
1628CaptureVoiceOut *AUD_add_capture (
1629 AudioState *s,
1630 audsettings_t *as,
1631 struct audio_capture_ops *ops,
1632 void *cb_opaque
1633 )
1634{
1635 CaptureVoiceOut *cap;
1636 struct capture_callback *cb;
1637
1638 if (!s) {
1639 /* XXX suppress */
1640 s = &glob_audio_state;
1641 }
1642
1643 if (audio_validate_settings (as)) {
1644 dolog ("Invalid settings were passed when trying to add capture\n");
1645 audio_print_settings (as);
1646 goto err0;
1647 }
1648
1649 cb = audio_calloc (AUDIO_FUNC, 1, sizeof (*cb));
1650 if (!cb) {
1651 dolog ("Could not allocate capture callback information, size %u\n",
1652 sizeof (*cb));
1653 goto err0;
1654 }
1655 cb->ops = *ops;
1656 cb->opaque = cb_opaque;
1657
1658 cap = audio_pcm_capture_find_specific (s, as);
1659 if (cap) {
1660 LIST_INSERT_HEAD (&cap->cb_head, cb, entries);
1661 return cap;
1662 }
1663 else {
1664 HWVoiceOut *hw;
1665 CaptureVoiceOut *cap;
1666
1667 cap = audio_calloc (AUDIO_FUNC, 1, sizeof (*cap));
1668 if (!cap) {
1669 dolog ("Could not allocate capture voice, size %u\n",
1670 sizeof (*cap));
1671 goto err1;
1672 }
1673
1674 hw = &cap->hw;
1675 LIST_INIT (&hw->sw_head);
1676 LIST_INIT (&cap->cb_head);
1677
1678 /* XXX find a more elegant way */
1679 hw->samples = 4096 * 4;
1680 hw->mix_buf = audio_calloc (AUDIO_FUNC, hw->samples,
1681 sizeof (st_sample_t));
1682 if (!hw->mix_buf) {
1683 dolog ("Could not allocate capture mix buffer (%d samples)\n",
1684 hw->samples);
1685 goto err2;
1686 }
1687
1688 audio_pcm_init_info (&hw->info, as);
1689
1690 cap->buf = audio_calloc (AUDIO_FUNC, hw->samples, 1 << hw->info.shift);
1691 if (!cap->buf) {
1692 dolog ("Could not allocate capture buffer "
1693 "(%d samples, each %d bytes)\n",
1694 hw->samples, 1 << hw->info.shift);
1695 goto err3;
1696 }
1697
1698 hw->clip = mixeng_clip
1699 [hw->info.nchannels == 2]
1700 [hw->info.sign]
1701 [hw->info.swap_endianness]
1702 [hw->info.bits == 16];
1703
1704 LIST_INSERT_HEAD (&s->cap_head, cap, entries);
1705 LIST_INSERT_HEAD (&cap->cb_head, cb, entries);
1706
1707 hw = NULL;
1708 while ((hw = audio_pcm_hw_find_any_out (s, hw))) {
1709 audio_attach_capture (s, hw);
1710 }
1711 return cap;
1712
1713 err3:
1714 qemu_free (cap->hw.mix_buf);
1715 err2:
1716 qemu_free (cap);
1717 err1:
1718 qemu_free (cb);
1719 err0:
1720 return NULL;
1721 }
1722}
1723
1724void AUD_del_capture (CaptureVoiceOut *cap, void *cb_opaque)
1725{
1726 struct capture_callback *cb;
1727
1728 for (cb = cap->cb_head.lh_first; cb; cb = cb->entries.le_next) {
1729 if (cb->opaque == cb_opaque) {
1730 cb->ops.destroy (cb_opaque);
1731 LIST_REMOVE (cb, entries);
1732 qemu_free (cb);
1733
1734 if (!cap->cb_head.lh_first) {
1735 SWVoiceOut *sw = cap->hw.sw_head.lh_first, *sw1;
1736
1737 while (sw) {
1738 SWVoiceCap *sc = (SWVoiceCap *) sw;
1739#ifdef DEBUG_CAPTURE
1740 dolog ("freeing %s\n", sw->name);
1741#endif
1742
1743 sw1 = sw->entries.le_next;
1744 if (sw->rate) {
1745 st_rate_stop (sw->rate);
1746 sw->rate = NULL;
1747 }
1748 LIST_REMOVE (sw, entries);
1749 LIST_REMOVE (sc, entries);
1750 qemu_free (sc);
1751 sw = sw1;
1752 }
1753 LIST_REMOVE (cap, entries);
1754 qemu_free (cap);
1755 }
1756 return;
1757 }
1758 }
1759}
1760
1761void AUD_set_volume_out (SWVoiceOut *sw, int mute, uint8_t lvol, uint8_t rvol)
1762{
1763 if (sw)
1764 {
1765 sw->vol.mute = mute;
1766 sw->vol.l = (uint32_t)lvol * 0x808080; /* maximum is INT_MAX = 0x7fffffff */
1767 sw->vol.r = (uint32_t)rvol * 0x808080; /* maximum is INT_MAX = 0x7fffffff */
1768 }
1769}
1770
1771void AUD_set_volume (audmixerctl_t mt, int *mute, uint8_t *lvol, uint8_t *rvol)
1772{
1773 volume_t *vol = NULL;
1774 const char *name;
1775
1776 switch (mt)
1777 {
1778 case AUD_MIXER_VOLUME:
1779 name = "MASTER";
1780 vol = &pcm_out_volume;
1781 break;
1782 case AUD_MIXER_PCM:
1783 name = "PCM_OUT";
1784 break;
1785 case AUD_MIXER_LINE_IN:
1786 name = "LINE_IN";
1787 vol = &pcm_in_volume;
1788 break;
1789 default:
1790 return;
1791
1792 }
1793
1794 if (vol)
1795 {
1796 vol->mute = *mute;
1797 vol->l = ((uint32_t)*lvol) * 0x808080; /* maximum is INT_MAX = 0x7fffffff */
1798 vol->r = ((uint32_t)*rvol) * 0x808080; /* maximum is INT_MAX = 0x7fffffff */
1799 }
1800#if 0
1801 LogRel(("AUDIO: Set '%s' volume to %d%%/%d%%\n", name, (*lvol*100)/255, (*rvol*100)/255l));
1802#endif
1803}
1804
1805void AUD_set_record_source (audrecsource_t *ars, audrecsource_t *als)
1806{
1807 LogRel(("Audio: set_record_source ars=%d als=%d (not implemented)\n", *ars, *als));
1808}
1809
1810/**
1811 * Queries an interface to the driver.
1812 *
1813 * @returns Pointer to interface.
1814 * @returns NULL if the interface was not supported by the driver.
1815 * @param pInterface Pointer to this interface structure.
1816 * @param enmInterface The requested interface identification.
1817 * @thread Any thread.
1818 */
1819static DECLCALLBACK(void *) drvAudioQueryInterface(PPDMIBASE pInterface,
1820 PDMINTERFACE enmInterface)
1821{
1822 PPDMDRVINS pDrvIns = PDMIBASE_2_PDMDRV(pInterface);
1823 PDRVAUDIO pData = PDMINS2DATA(pDrvIns, PDRVAUDIO);
1824 switch (enmInterface)
1825 {
1826 case PDMINTERFACE_BASE:
1827 return &pDrvIns->IBase;
1828 case PDMINTERFACE_AUDIO_CONNECTOR:
1829 return &pData->IAudioConnector;
1830 default:
1831 return NULL;
1832 }
1833}
1834
1835/**
1836 * Power Off notification.
1837 *
1838 * @param pDrvIns The driver instance data.
1839 */
1840static DECLCALLBACK(void) drvAudioPowerOff(PPDMDRVINS pDrvIns)
1841{
1842 AudioState *s = &glob_audio_state;
1843 audio_vm_change_state_handler (s, 0);
1844}
1845
1846/**
1847 * Destruct a driver instance.
1848 *
1849 * Most VM resources are freed by the VM. This callback is provided so that any non-VM
1850 * resources can be freed correctly.
1851 *
1852 * @param pDrvIns The driver instance data.
1853 */
1854static DECLCALLBACK(void) drvAudioDestruct(PPDMDRVINS pDrvIns)
1855{
1856 LogFlow(("drvAUDIODestruct:\n"));
1857
1858 audio_atexit ();
1859}
1860
1861/**
1862 * Construct an AUDIO driver instance.
1863 *
1864 * @returns VBox status.
1865 * @param pDrvIns The driver instance data.
1866 * If the registration structure is needed, pDrvIns->pDrvReg points to it.
1867 * @param pCfgHandle Configuration node handle for the driver. Use this to obtain the configuration
1868 * of the driver instance. It's also found in pDrvIns->pCfgHandle, but like
1869 * iInstance it's expected to be used a bit in this function.
1870 */
1871static DECLCALLBACK(int) drvAudioConstruct(PPDMDRVINS pDrvIns, PCFGMNODE pCfgHandle)
1872{
1873 int rc;
1874 PDRVAUDIO pData = PDMINS2DATA(pDrvIns, PDRVAUDIO);
1875 char *drvname;
1876#if defined(RT_OS_LINUX) && defined(VBOX_WITH_ALSA)
1877 RTLDRMOD hLibVBoxAlsa;
1878#endif
1879
1880 LogFlow(("drvAUDIOConstruct:\n"));
1881 /*
1882 * Validate the config.
1883 */
1884 if (!CFGMR3AreValuesValid(pCfgHandle, "AudioDriver\0"))
1885 return VERR_PDM_DRVINS_UNKNOWN_CFG_VALUES;
1886
1887 /*
1888 * Init the static parts.
1889 */
1890 pData->pDrvIns = pDrvIns;
1891 /* IBase */
1892 pDrvIns->IBase.pfnQueryInterface = drvAudioQueryInterface;
1893 /* IAudio */
1894 /* pData->IAudioConnector.pfn; */
1895
1896 glob_audio_state.pDrvIns = pDrvIns;
1897
1898 rc = CFGMR3QueryStringAlloc (pCfgHandle, "AudioDriver", &drvname);
1899 if (VBOX_FAILURE (rc))
1900 return rc;
1901
1902#if defined(RT_OS_LINUX) && defined(VBOX_WITH_ALSA)
1903 /*
1904 * Load the ALSA driver if needed. This is a hack to remove the
1905 * hard dependency on libasound.so.
1906 */
1907 if (0 == strcmp(drvname, "alsa")) {
1908 Log2(("Loading ALSA library %s\n", VBOX_LIB_ALSA));
1909 rc = (RTLdrLoad(VBOX_LIB_ALSA, &hLibVBoxAlsa));
1910 if (RT_FAILURE(rc)) {
1911 Log(("Failed to load ALSA library %s. Reason: %Rrc\n", VBOX_LIB_ALSA, rc));
1912 } else {
1913 rc = RTLdrGetSymbol(hLibVBoxAlsa, "alsa_audio_driver", (void **) &drvtab[1]);
1914 if (RT_FAILURE(rc)) {
1915 Log(("Failed to get symbol \"alsa_audio_driver\" from library %s. Reason: %Rrc\n",
1916 VBOX_LIB_ALSA, rc));
1917 drvtab[1] = NULL;
1918 }
1919 }
1920 }
1921#endif
1922
1923 rc = AUD_init (pDrvIns, drvname);
1924 if (VBOX_FAILURE (rc))
1925 return rc;
1926
1927 MMR3HeapFree (drvname);
1928
1929 return VINF_SUCCESS;
1930}
1931
1932/**
1933 * Suspend notification.
1934 *
1935 * @returns VBox status.
1936 * @param pDrvIns The driver instance data.
1937 */
1938static DECLCALLBACK(void) drvAudioSuspend(PPDMDRVINS pDrvIns)
1939{
1940 AudioState *s = &glob_audio_state;
1941 audio_vm_change_state_handler (s, 0);
1942}
1943
1944/**
1945 * Resume notification.
1946 *
1947 * @returns VBox status.
1948 * @param pDrvIns The driver instance data.
1949 */
1950static DECLCALLBACK(void) audioResume(PPDMDRVINS pDrvIns)
1951{
1952 AudioState *s = &glob_audio_state;
1953 audio_vm_change_state_handler (s, 1);
1954}
1955
1956/**
1957 * Audio driver registration record.
1958 */
1959const PDMDRVREG g_DrvAUDIO =
1960{
1961 /* u32Version */
1962 PDM_DRVREG_VERSION,
1963 /* szDriverName */
1964 "AUDIO",
1965 /* pszDescription */
1966 "AUDIO Driver",
1967 /* fFlags */
1968 PDM_DRVREG_FLAGS_HOST_BITS_DEFAULT,
1969 /* fClass. */
1970 PDM_DRVREG_CLASS_AUDIO,
1971 /* cMaxInstances */
1972 1,
1973 /* cbInstance */
1974 sizeof(DRVAUDIO),
1975 /* pfnConstruct */
1976 drvAudioConstruct,
1977 /* pfnDestruct */
1978 drvAudioDestruct,
1979 /* pfnIOCtl */
1980 NULL,
1981 /* pfnPowerOn */
1982 NULL,
1983 /* pfnReset */
1984 NULL,
1985 /* pfnSuspend */
1986 drvAudioSuspend,
1987 /* pfnResume */
1988 audioResume,
1989 /* pfnDetach */
1990 NULL,
1991 /* pfnPowerOff */
1992 drvAudioPowerOff
1993};
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