]> git.ipfire.org Git - thirdparty/qemu.git/blob - audio/audio.c
audio: basic support for multi backend audio
[thirdparty/qemu.git] / audio / audio.c
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
25 #include "qemu/osdep.h"
26 #include "audio.h"
27 #include "migration/vmstate.h"
28 #include "monitor/monitor.h"
29 #include "qemu/timer.h"
30 #include "qapi/error.h"
31 #include "qapi/qobject-input-visitor.h"
32 #include "qapi/qapi-visit-audio.h"
33 #include "qemu/cutils.h"
34 #include "qemu/module.h"
35 #include "sysemu/replay.h"
36 #include "sysemu/runstate.h"
37 #include "trace.h"
38
39 #define AUDIO_CAP "audio"
40 #include "audio_int.h"
41
42 /* #define DEBUG_LIVE */
43 /* #define DEBUG_OUT */
44 /* #define DEBUG_CAPTURE */
45 /* #define DEBUG_POLL */
46
47 #define SW_NAME(sw) (sw)->name ? (sw)->name : "unknown"
48
49
50 /* Order of CONFIG_AUDIO_DRIVERS is import.
51 The 1st one is the one used by default, that is the reason
52 that we generate the list.
53 */
54 const char *audio_prio_list[] = {
55 "spice",
56 CONFIG_AUDIO_DRIVERS
57 "none",
58 "wav",
59 NULL
60 };
61
62 static QLIST_HEAD(, audio_driver) audio_drivers;
63 static AudiodevListHead audiodevs = QSIMPLEQ_HEAD_INITIALIZER(audiodevs);
64
65 void audio_driver_register(audio_driver *drv)
66 {
67 QLIST_INSERT_HEAD(&audio_drivers, drv, next);
68 }
69
70 audio_driver *audio_driver_lookup(const char *name)
71 {
72 struct audio_driver *d;
73
74 QLIST_FOREACH(d, &audio_drivers, next) {
75 if (strcmp(name, d->name) == 0) {
76 return d;
77 }
78 }
79
80 audio_module_load_one(name);
81 QLIST_FOREACH(d, &audio_drivers, next) {
82 if (strcmp(name, d->name) == 0) {
83 return d;
84 }
85 }
86
87 return NULL;
88 }
89
90 static QTAILQ_HEAD(AudioStateHead, AudioState) audio_states =
91 QTAILQ_HEAD_INITIALIZER(audio_states);
92
93 const struct mixeng_volume nominal_volume = {
94 .mute = 0,
95 #ifdef FLOAT_MIXENG
96 .r = 1.0,
97 .l = 1.0,
98 #else
99 .r = 1ULL << 32,
100 .l = 1ULL << 32,
101 #endif
102 };
103
104 #ifdef AUDIO_IS_FLAWLESS_AND_NO_CHECKS_ARE_REQURIED
105 #error No its not
106 #else
107 int audio_bug (const char *funcname, int cond)
108 {
109 if (cond) {
110 static int shown;
111
112 AUD_log (NULL, "A bug was just triggered in %s\n", funcname);
113 if (!shown) {
114 shown = 1;
115 AUD_log (NULL, "Save all your work and restart without audio\n");
116 AUD_log (NULL, "I am sorry\n");
117 }
118 AUD_log (NULL, "Context:\n");
119
120 #if defined AUDIO_BREAKPOINT_ON_BUG
121 # if defined HOST_I386
122 # if defined __GNUC__
123 __asm__ ("int3");
124 # elif defined _MSC_VER
125 _asm _emit 0xcc;
126 # else
127 abort ();
128 # endif
129 # else
130 abort ();
131 # endif
132 #endif
133 }
134
135 return cond;
136 }
137 #endif
138
139 static inline int audio_bits_to_index (int bits)
140 {
141 switch (bits) {
142 case 8:
143 return 0;
144
145 case 16:
146 return 1;
147
148 case 32:
149 return 2;
150
151 default:
152 audio_bug ("bits_to_index", 1);
153 AUD_log (NULL, "invalid bits %d\n", bits);
154 return 0;
155 }
156 }
157
158 void *audio_calloc (const char *funcname, int nmemb, size_t size)
159 {
160 int cond;
161 size_t len;
162
163 len = nmemb * size;
164 cond = !nmemb || !size;
165 cond |= nmemb < 0;
166 cond |= len < size;
167
168 if (audio_bug ("audio_calloc", cond)) {
169 AUD_log (NULL, "%s passed invalid arguments to audio_calloc\n",
170 funcname);
171 AUD_log (NULL, "nmemb=%d size=%zu (len=%zu)\n", nmemb, size, len);
172 return NULL;
173 }
174
175 return g_malloc0 (len);
176 }
177
178 void AUD_vlog (const char *cap, const char *fmt, va_list ap)
179 {
180 if (cap) {
181 fprintf(stderr, "%s: ", cap);
182 }
183
184 vfprintf(stderr, fmt, ap);
185 }
186
187 void AUD_log (const char *cap, const char *fmt, ...)
188 {
189 va_list ap;
190
191 va_start (ap, fmt);
192 AUD_vlog (cap, fmt, ap);
193 va_end (ap);
194 }
195
196 static void audio_print_settings (struct audsettings *as)
197 {
198 dolog ("frequency=%d nchannels=%d fmt=", as->freq, as->nchannels);
199
200 switch (as->fmt) {
201 case AUDIO_FORMAT_S8:
202 AUD_log (NULL, "S8");
203 break;
204 case AUDIO_FORMAT_U8:
205 AUD_log (NULL, "U8");
206 break;
207 case AUDIO_FORMAT_S16:
208 AUD_log (NULL, "S16");
209 break;
210 case AUDIO_FORMAT_U16:
211 AUD_log (NULL, "U16");
212 break;
213 case AUDIO_FORMAT_S32:
214 AUD_log (NULL, "S32");
215 break;
216 case AUDIO_FORMAT_U32:
217 AUD_log (NULL, "U32");
218 break;
219 default:
220 AUD_log (NULL, "invalid(%d)", as->fmt);
221 break;
222 }
223
224 AUD_log (NULL, " endianness=");
225 switch (as->endianness) {
226 case 0:
227 AUD_log (NULL, "little");
228 break;
229 case 1:
230 AUD_log (NULL, "big");
231 break;
232 default:
233 AUD_log (NULL, "invalid");
234 break;
235 }
236 AUD_log (NULL, "\n");
237 }
238
239 static int audio_validate_settings (struct audsettings *as)
240 {
241 int invalid;
242
243 invalid = as->nchannels != 1 && as->nchannels != 2;
244 invalid |= as->endianness != 0 && as->endianness != 1;
245
246 switch (as->fmt) {
247 case AUDIO_FORMAT_S8:
248 case AUDIO_FORMAT_U8:
249 case AUDIO_FORMAT_S16:
250 case AUDIO_FORMAT_U16:
251 case AUDIO_FORMAT_S32:
252 case AUDIO_FORMAT_U32:
253 break;
254 default:
255 invalid = 1;
256 break;
257 }
258
259 invalid |= as->freq <= 0;
260 return invalid ? -1 : 0;
261 }
262
263 static int audio_pcm_info_eq (struct audio_pcm_info *info, struct audsettings *as)
264 {
265 int bits = 8, sign = 0;
266
267 switch (as->fmt) {
268 case AUDIO_FORMAT_S8:
269 sign = 1;
270 /* fall through */
271 case AUDIO_FORMAT_U8:
272 break;
273
274 case AUDIO_FORMAT_S16:
275 sign = 1;
276 /* fall through */
277 case AUDIO_FORMAT_U16:
278 bits = 16;
279 break;
280
281 case AUDIO_FORMAT_S32:
282 sign = 1;
283 /* fall through */
284 case AUDIO_FORMAT_U32:
285 bits = 32;
286 break;
287
288 default:
289 abort();
290 }
291 return info->freq == as->freq
292 && info->nchannels == as->nchannels
293 && info->sign == sign
294 && info->bits == bits
295 && info->swap_endianness == (as->endianness != AUDIO_HOST_ENDIANNESS);
296 }
297
298 void audio_pcm_init_info (struct audio_pcm_info *info, struct audsettings *as)
299 {
300 int bits = 8, sign = 0, shift = 0;
301
302 switch (as->fmt) {
303 case AUDIO_FORMAT_S8:
304 sign = 1;
305 case AUDIO_FORMAT_U8:
306 break;
307
308 case AUDIO_FORMAT_S16:
309 sign = 1;
310 /* fall through */
311 case AUDIO_FORMAT_U16:
312 bits = 16;
313 shift = 1;
314 break;
315
316 case AUDIO_FORMAT_S32:
317 sign = 1;
318 /* fall through */
319 case AUDIO_FORMAT_U32:
320 bits = 32;
321 shift = 2;
322 break;
323
324 default:
325 abort();
326 }
327
328 info->freq = as->freq;
329 info->bits = bits;
330 info->sign = sign;
331 info->nchannels = as->nchannels;
332 info->shift = (as->nchannels == 2) + shift;
333 info->align = (1 << info->shift) - 1;
334 info->bytes_per_second = info->freq << info->shift;
335 info->swap_endianness = (as->endianness != AUDIO_HOST_ENDIANNESS);
336 }
337
338 void audio_pcm_info_clear_buf (struct audio_pcm_info *info, void *buf, int len)
339 {
340 if (!len) {
341 return;
342 }
343
344 if (info->sign) {
345 memset (buf, 0x00, len << info->shift);
346 }
347 else {
348 switch (info->bits) {
349 case 8:
350 memset (buf, 0x80, len << info->shift);
351 break;
352
353 case 16:
354 {
355 int i;
356 uint16_t *p = buf;
357 int shift = info->nchannels - 1;
358 short s = INT16_MAX;
359
360 if (info->swap_endianness) {
361 s = bswap16 (s);
362 }
363
364 for (i = 0; i < len << shift; i++) {
365 p[i] = s;
366 }
367 }
368 break;
369
370 case 32:
371 {
372 int i;
373 uint32_t *p = buf;
374 int shift = info->nchannels - 1;
375 int32_t s = INT32_MAX;
376
377 if (info->swap_endianness) {
378 s = bswap32 (s);
379 }
380
381 for (i = 0; i < len << shift; i++) {
382 p[i] = s;
383 }
384 }
385 break;
386
387 default:
388 AUD_log (NULL, "audio_pcm_info_clear_buf: invalid bits %d\n",
389 info->bits);
390 break;
391 }
392 }
393 }
394
395 /*
396 * Capture
397 */
398 static void noop_conv (struct st_sample *dst, const void *src, int samples)
399 {
400 (void) src;
401 (void) dst;
402 (void) samples;
403 }
404
405 static CaptureVoiceOut *audio_pcm_capture_find_specific(AudioState *s,
406 struct audsettings *as)
407 {
408 CaptureVoiceOut *cap;
409
410 for (cap = s->cap_head.lh_first; cap; cap = cap->entries.le_next) {
411 if (audio_pcm_info_eq (&cap->hw.info, as)) {
412 return cap;
413 }
414 }
415 return NULL;
416 }
417
418 static void audio_notify_capture (CaptureVoiceOut *cap, audcnotification_e cmd)
419 {
420 struct capture_callback *cb;
421
422 #ifdef DEBUG_CAPTURE
423 dolog ("notification %d sent\n", cmd);
424 #endif
425 for (cb = cap->cb_head.lh_first; cb; cb = cb->entries.le_next) {
426 cb->ops.notify (cb->opaque, cmd);
427 }
428 }
429
430 static void audio_capture_maybe_changed (CaptureVoiceOut *cap, int enabled)
431 {
432 if (cap->hw.enabled != enabled) {
433 audcnotification_e cmd;
434 cap->hw.enabled = enabled;
435 cmd = enabled ? AUD_CNOTIFY_ENABLE : AUD_CNOTIFY_DISABLE;
436 audio_notify_capture (cap, cmd);
437 }
438 }
439
440 static void audio_recalc_and_notify_capture (CaptureVoiceOut *cap)
441 {
442 HWVoiceOut *hw = &cap->hw;
443 SWVoiceOut *sw;
444 int enabled = 0;
445
446 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
447 if (sw->active) {
448 enabled = 1;
449 break;
450 }
451 }
452 audio_capture_maybe_changed (cap, enabled);
453 }
454
455 static void audio_detach_capture (HWVoiceOut *hw)
456 {
457 SWVoiceCap *sc = hw->cap_head.lh_first;
458
459 while (sc) {
460 SWVoiceCap *sc1 = sc->entries.le_next;
461 SWVoiceOut *sw = &sc->sw;
462 CaptureVoiceOut *cap = sc->cap;
463 int was_active = sw->active;
464
465 if (sw->rate) {
466 st_rate_stop (sw->rate);
467 sw->rate = NULL;
468 }
469
470 QLIST_REMOVE (sw, entries);
471 QLIST_REMOVE (sc, entries);
472 g_free (sc);
473 if (was_active) {
474 /* We have removed soft voice from the capture:
475 this might have changed the overall status of the capture
476 since this might have been the only active voice */
477 audio_recalc_and_notify_capture (cap);
478 }
479 sc = sc1;
480 }
481 }
482
483 static int audio_attach_capture (HWVoiceOut *hw)
484 {
485 AudioState *s = hw->s;
486 CaptureVoiceOut *cap;
487
488 audio_detach_capture (hw);
489 for (cap = s->cap_head.lh_first; cap; cap = cap->entries.le_next) {
490 SWVoiceCap *sc;
491 SWVoiceOut *sw;
492 HWVoiceOut *hw_cap = &cap->hw;
493
494 sc = g_malloc0(sizeof(*sc));
495
496 sc->cap = cap;
497 sw = &sc->sw;
498 sw->hw = hw_cap;
499 sw->info = hw->info;
500 sw->empty = 1;
501 sw->active = hw->enabled;
502 sw->conv = noop_conv;
503 sw->ratio = ((int64_t) hw_cap->info.freq << 32) / sw->info.freq;
504 sw->vol = nominal_volume;
505 sw->rate = st_rate_start (sw->info.freq, hw_cap->info.freq);
506 if (!sw->rate) {
507 dolog ("Could not start rate conversion for `%s'\n", SW_NAME (sw));
508 g_free (sw);
509 return -1;
510 }
511 QLIST_INSERT_HEAD (&hw_cap->sw_head, sw, entries);
512 QLIST_INSERT_HEAD (&hw->cap_head, sc, entries);
513 #ifdef DEBUG_CAPTURE
514 sw->name = g_strdup_printf ("for %p %d,%d,%d",
515 hw, sw->info.freq, sw->info.bits,
516 sw->info.nchannels);
517 dolog ("Added %s active = %d\n", sw->name, sw->active);
518 #endif
519 if (sw->active) {
520 audio_capture_maybe_changed (cap, 1);
521 }
522 }
523 return 0;
524 }
525
526 /*
527 * Hard voice (capture)
528 */
529 static int audio_pcm_hw_find_min_in (HWVoiceIn *hw)
530 {
531 SWVoiceIn *sw;
532 int m = hw->total_samples_captured;
533
534 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
535 if (sw->active) {
536 m = audio_MIN (m, sw->total_hw_samples_acquired);
537 }
538 }
539 return m;
540 }
541
542 int audio_pcm_hw_get_live_in (HWVoiceIn *hw)
543 {
544 int live = hw->total_samples_captured - audio_pcm_hw_find_min_in (hw);
545 if (audio_bug(__func__, live < 0 || live > hw->samples)) {
546 dolog ("live=%d hw->samples=%d\n", live, hw->samples);
547 return 0;
548 }
549 return live;
550 }
551
552 int audio_pcm_hw_clip_out (HWVoiceOut *hw, void *pcm_buf,
553 int live, int pending)
554 {
555 int left = hw->samples - pending;
556 int len = audio_MIN (left, live);
557 int clipped = 0;
558
559 while (len) {
560 struct st_sample *src = hw->mix_buf + hw->rpos;
561 uint8_t *dst = advance (pcm_buf, hw->rpos << hw->info.shift);
562 int samples_till_end_of_buf = hw->samples - hw->rpos;
563 int samples_to_clip = audio_MIN (len, samples_till_end_of_buf);
564
565 hw->clip (dst, src, samples_to_clip);
566
567 hw->rpos = (hw->rpos + samples_to_clip) % hw->samples;
568 len -= samples_to_clip;
569 clipped += samples_to_clip;
570 }
571 return clipped;
572 }
573
574 /*
575 * Soft voice (capture)
576 */
577 static int audio_pcm_sw_get_rpos_in (SWVoiceIn *sw)
578 {
579 HWVoiceIn *hw = sw->hw;
580 int live = hw->total_samples_captured - sw->total_hw_samples_acquired;
581 int rpos;
582
583 if (audio_bug(__func__, live < 0 || live > hw->samples)) {
584 dolog ("live=%d hw->samples=%d\n", live, hw->samples);
585 return 0;
586 }
587
588 rpos = hw->wpos - live;
589 if (rpos >= 0) {
590 return rpos;
591 }
592 else {
593 return hw->samples + rpos;
594 }
595 }
596
597 int audio_pcm_sw_read (SWVoiceIn *sw, void *buf, int size)
598 {
599 HWVoiceIn *hw = sw->hw;
600 int samples, live, ret = 0, swlim, isamp, osamp, rpos, total = 0;
601 struct st_sample *src, *dst = sw->buf;
602
603 rpos = audio_pcm_sw_get_rpos_in (sw) % hw->samples;
604
605 live = hw->total_samples_captured - sw->total_hw_samples_acquired;
606 if (audio_bug(__func__, live < 0 || live > hw->samples)) {
607 dolog ("live_in=%d hw->samples=%d\n", live, hw->samples);
608 return 0;
609 }
610
611 samples = size >> sw->info.shift;
612 if (!live) {
613 return 0;
614 }
615
616 swlim = (live * sw->ratio) >> 32;
617 swlim = audio_MIN (swlim, samples);
618
619 while (swlim) {
620 src = hw->conv_buf + rpos;
621 isamp = hw->wpos - rpos;
622 /* XXX: <= ? */
623 if (isamp <= 0) {
624 isamp = hw->samples - rpos;
625 }
626
627 if (!isamp) {
628 break;
629 }
630 osamp = swlim;
631
632 if (audio_bug(__func__, osamp < 0)) {
633 dolog ("osamp=%d\n", osamp);
634 return 0;
635 }
636
637 st_rate_flow (sw->rate, src, dst, &isamp, &osamp);
638 swlim -= osamp;
639 rpos = (rpos + isamp) % hw->samples;
640 dst += osamp;
641 ret += osamp;
642 total += isamp;
643 }
644
645 if (!(hw->ctl_caps & VOICE_VOLUME_CAP)) {
646 mixeng_volume (sw->buf, ret, &sw->vol);
647 }
648
649 sw->clip (buf, sw->buf, ret);
650 sw->total_hw_samples_acquired += total;
651 return ret << sw->info.shift;
652 }
653
654 /*
655 * Hard voice (playback)
656 */
657 static int audio_pcm_hw_find_min_out (HWVoiceOut *hw, int *nb_livep)
658 {
659 SWVoiceOut *sw;
660 int m = INT_MAX;
661 int nb_live = 0;
662
663 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
664 if (sw->active || !sw->empty) {
665 m = audio_MIN (m, sw->total_hw_samples_mixed);
666 nb_live += 1;
667 }
668 }
669
670 *nb_livep = nb_live;
671 return m;
672 }
673
674 static int audio_pcm_hw_get_live_out (HWVoiceOut *hw, int *nb_live)
675 {
676 int smin;
677 int nb_live1;
678
679 smin = audio_pcm_hw_find_min_out (hw, &nb_live1);
680 if (nb_live) {
681 *nb_live = nb_live1;
682 }
683
684 if (nb_live1) {
685 int live = smin;
686
687 if (audio_bug(__func__, live < 0 || live > hw->samples)) {
688 dolog ("live=%d hw->samples=%d\n", live, hw->samples);
689 return 0;
690 }
691 return live;
692 }
693 return 0;
694 }
695
696 /*
697 * Soft voice (playback)
698 */
699 int audio_pcm_sw_write (SWVoiceOut *sw, void *buf, int size)
700 {
701 int hwsamples, samples, isamp, osamp, wpos, live, dead, left, swlim, blck;
702 int ret = 0, pos = 0, total = 0;
703
704 if (!sw) {
705 return size;
706 }
707
708 hwsamples = sw->hw->samples;
709
710 live = sw->total_hw_samples_mixed;
711 if (audio_bug(__func__, live < 0 || live > hwsamples)) {
712 dolog ("live=%d hw->samples=%d\n", live, hwsamples);
713 return 0;
714 }
715
716 if (live == hwsamples) {
717 #ifdef DEBUG_OUT
718 dolog ("%s is full %d\n", sw->name, live);
719 #endif
720 return 0;
721 }
722
723 wpos = (sw->hw->rpos + live) % hwsamples;
724 samples = size >> sw->info.shift;
725
726 dead = hwsamples - live;
727 swlim = ((int64_t) dead << 32) / sw->ratio;
728 swlim = audio_MIN (swlim, samples);
729 if (swlim) {
730 sw->conv (sw->buf, buf, swlim);
731
732 if (!(sw->hw->ctl_caps & VOICE_VOLUME_CAP)) {
733 mixeng_volume (sw->buf, swlim, &sw->vol);
734 }
735 }
736
737 while (swlim) {
738 dead = hwsamples - live;
739 left = hwsamples - wpos;
740 blck = audio_MIN (dead, left);
741 if (!blck) {
742 break;
743 }
744 isamp = swlim;
745 osamp = blck;
746 st_rate_flow_mix (
747 sw->rate,
748 sw->buf + pos,
749 sw->hw->mix_buf + wpos,
750 &isamp,
751 &osamp
752 );
753 ret += isamp;
754 swlim -= isamp;
755 pos += isamp;
756 live += osamp;
757 wpos = (wpos + osamp) % hwsamples;
758 total += osamp;
759 }
760
761 sw->total_hw_samples_mixed += total;
762 sw->empty = sw->total_hw_samples_mixed == 0;
763
764 #ifdef DEBUG_OUT
765 dolog (
766 "%s: write size %d ret %d total sw %d\n",
767 SW_NAME (sw),
768 size >> sw->info.shift,
769 ret,
770 sw->total_hw_samples_mixed
771 );
772 #endif
773
774 return ret << sw->info.shift;
775 }
776
777 #ifdef DEBUG_AUDIO
778 static void audio_pcm_print_info (const char *cap, struct audio_pcm_info *info)
779 {
780 dolog ("%s: bits %d, sign %d, freq %d, nchan %d\n",
781 cap, info->bits, info->sign, info->freq, info->nchannels);
782 }
783 #endif
784
785 #define DAC
786 #include "audio_template.h"
787 #undef DAC
788 #include "audio_template.h"
789
790 /*
791 * Timer
792 */
793 static int audio_is_timer_needed(AudioState *s)
794 {
795 HWVoiceIn *hwi = NULL;
796 HWVoiceOut *hwo = NULL;
797
798 while ((hwo = audio_pcm_hw_find_any_enabled_out(s, hwo))) {
799 if (!hwo->poll_mode) return 1;
800 }
801 while ((hwi = audio_pcm_hw_find_any_enabled_in(s, hwi))) {
802 if (!hwi->poll_mode) return 1;
803 }
804 return 0;
805 }
806
807 static void audio_reset_timer (AudioState *s)
808 {
809 if (audio_is_timer_needed(s)) {
810 timer_mod_anticipate_ns(s->ts,
811 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + s->period_ticks);
812 if (!s->timer_running) {
813 s->timer_running = true;
814 s->timer_last = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
815 trace_audio_timer_start(s->period_ticks / SCALE_MS);
816 }
817 } else {
818 timer_del(s->ts);
819 if (s->timer_running) {
820 s->timer_running = false;
821 trace_audio_timer_stop();
822 }
823 }
824 }
825
826 static void audio_timer (void *opaque)
827 {
828 int64_t now, diff;
829 AudioState *s = opaque;
830
831 now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
832 diff = now - s->timer_last;
833 if (diff > s->period_ticks * 3 / 2) {
834 trace_audio_timer_delayed(diff / SCALE_MS);
835 }
836 s->timer_last = now;
837
838 audio_run("timer");
839 audio_reset_timer(s);
840 }
841
842 /*
843 * Public API
844 */
845 int AUD_write (SWVoiceOut *sw, void *buf, int size)
846 {
847 if (!sw) {
848 /* XXX: Consider options */
849 return size;
850 }
851
852 if (!sw->hw->enabled) {
853 dolog ("Writing to disabled voice %s\n", SW_NAME (sw));
854 return 0;
855 }
856
857 return sw->hw->pcm_ops->write(sw, buf, size);
858 }
859
860 int AUD_read (SWVoiceIn *sw, void *buf, int size)
861 {
862 if (!sw) {
863 /* XXX: Consider options */
864 return size;
865 }
866
867 if (!sw->hw->enabled) {
868 dolog ("Reading from disabled voice %s\n", SW_NAME (sw));
869 return 0;
870 }
871
872 return sw->hw->pcm_ops->read(sw, buf, size);
873 }
874
875 int AUD_get_buffer_size_out (SWVoiceOut *sw)
876 {
877 return sw->hw->samples << sw->hw->info.shift;
878 }
879
880 void AUD_set_active_out (SWVoiceOut *sw, int on)
881 {
882 HWVoiceOut *hw;
883
884 if (!sw) {
885 return;
886 }
887
888 hw = sw->hw;
889 if (sw->active != on) {
890 AudioState *s = sw->s;
891 SWVoiceOut *temp_sw;
892 SWVoiceCap *sc;
893
894 if (on) {
895 hw->pending_disable = 0;
896 if (!hw->enabled) {
897 hw->enabled = 1;
898 if (s->vm_running) {
899 hw->pcm_ops->ctl_out(hw, VOICE_ENABLE);
900 audio_reset_timer (s);
901 }
902 }
903 }
904 else {
905 if (hw->enabled) {
906 int nb_active = 0;
907
908 for (temp_sw = hw->sw_head.lh_first; temp_sw;
909 temp_sw = temp_sw->entries.le_next) {
910 nb_active += temp_sw->active != 0;
911 }
912
913 hw->pending_disable = nb_active == 1;
914 }
915 }
916
917 for (sc = hw->cap_head.lh_first; sc; sc = sc->entries.le_next) {
918 sc->sw.active = hw->enabled;
919 if (hw->enabled) {
920 audio_capture_maybe_changed (sc->cap, 1);
921 }
922 }
923 sw->active = on;
924 }
925 }
926
927 void AUD_set_active_in (SWVoiceIn *sw, int on)
928 {
929 HWVoiceIn *hw;
930
931 if (!sw) {
932 return;
933 }
934
935 hw = sw->hw;
936 if (sw->active != on) {
937 AudioState *s = sw->s;
938 SWVoiceIn *temp_sw;
939
940 if (on) {
941 if (!hw->enabled) {
942 hw->enabled = 1;
943 if (s->vm_running) {
944 hw->pcm_ops->ctl_in(hw, VOICE_ENABLE);
945 audio_reset_timer (s);
946 }
947 }
948 sw->total_hw_samples_acquired = hw->total_samples_captured;
949 }
950 else {
951 if (hw->enabled) {
952 int nb_active = 0;
953
954 for (temp_sw = hw->sw_head.lh_first; temp_sw;
955 temp_sw = temp_sw->entries.le_next) {
956 nb_active += temp_sw->active != 0;
957 }
958
959 if (nb_active == 1) {
960 hw->enabled = 0;
961 hw->pcm_ops->ctl_in (hw, VOICE_DISABLE);
962 }
963 }
964 }
965 sw->active = on;
966 }
967 }
968
969 static int audio_get_avail (SWVoiceIn *sw)
970 {
971 int live;
972
973 if (!sw) {
974 return 0;
975 }
976
977 live = sw->hw->total_samples_captured - sw->total_hw_samples_acquired;
978 if (audio_bug(__func__, live < 0 || live > sw->hw->samples)) {
979 dolog ("live=%d sw->hw->samples=%d\n", live, sw->hw->samples);
980 return 0;
981 }
982
983 ldebug (
984 "%s: get_avail live %d ret %" PRId64 "\n",
985 SW_NAME (sw),
986 live, (((int64_t) live << 32) / sw->ratio) << sw->info.shift
987 );
988
989 return (((int64_t) live << 32) / sw->ratio) << sw->info.shift;
990 }
991
992 static int audio_get_free (SWVoiceOut *sw)
993 {
994 int live, dead;
995
996 if (!sw) {
997 return 0;
998 }
999
1000 live = sw->total_hw_samples_mixed;
1001
1002 if (audio_bug(__func__, live < 0 || live > sw->hw->samples)) {
1003 dolog ("live=%d sw->hw->samples=%d\n", live, sw->hw->samples);
1004 return 0;
1005 }
1006
1007 dead = sw->hw->samples - live;
1008
1009 #ifdef DEBUG_OUT
1010 dolog ("%s: get_free live %d dead %d ret %" PRId64 "\n",
1011 SW_NAME (sw),
1012 live, dead, (((int64_t) dead << 32) / sw->ratio) << sw->info.shift);
1013 #endif
1014
1015 return (((int64_t) dead << 32) / sw->ratio) << sw->info.shift;
1016 }
1017
1018 static void audio_capture_mix_and_clear (HWVoiceOut *hw, int rpos, int samples)
1019 {
1020 int n;
1021
1022 if (hw->enabled) {
1023 SWVoiceCap *sc;
1024
1025 for (sc = hw->cap_head.lh_first; sc; sc = sc->entries.le_next) {
1026 SWVoiceOut *sw = &sc->sw;
1027 int rpos2 = rpos;
1028
1029 n = samples;
1030 while (n) {
1031 int till_end_of_hw = hw->samples - rpos2;
1032 int to_write = audio_MIN (till_end_of_hw, n);
1033 int bytes = to_write << hw->info.shift;
1034 int written;
1035
1036 sw->buf = hw->mix_buf + rpos2;
1037 written = audio_pcm_sw_write (sw, NULL, bytes);
1038 if (written - bytes) {
1039 dolog ("Could not mix %d bytes into a capture "
1040 "buffer, mixed %d\n",
1041 bytes, written);
1042 break;
1043 }
1044 n -= to_write;
1045 rpos2 = (rpos2 + to_write) % hw->samples;
1046 }
1047 }
1048 }
1049
1050 n = audio_MIN (samples, hw->samples - rpos);
1051 mixeng_clear (hw->mix_buf + rpos, n);
1052 mixeng_clear (hw->mix_buf, samples - n);
1053 }
1054
1055 static void audio_run_out (AudioState *s)
1056 {
1057 HWVoiceOut *hw = NULL;
1058 SWVoiceOut *sw;
1059
1060 while ((hw = audio_pcm_hw_find_any_enabled_out(s, hw))) {
1061 int played;
1062 int live, free, nb_live, cleanup_required, prev_rpos;
1063
1064 live = audio_pcm_hw_get_live_out (hw, &nb_live);
1065 if (!nb_live) {
1066 live = 0;
1067 }
1068
1069 if (audio_bug(__func__, live < 0 || live > hw->samples)) {
1070 dolog ("live=%d hw->samples=%d\n", live, hw->samples);
1071 continue;
1072 }
1073
1074 if (hw->pending_disable && !nb_live) {
1075 SWVoiceCap *sc;
1076 #ifdef DEBUG_OUT
1077 dolog ("Disabling voice\n");
1078 #endif
1079 hw->enabled = 0;
1080 hw->pending_disable = 0;
1081 hw->pcm_ops->ctl_out (hw, VOICE_DISABLE);
1082 for (sc = hw->cap_head.lh_first; sc; sc = sc->entries.le_next) {
1083 sc->sw.active = 0;
1084 audio_recalc_and_notify_capture (sc->cap);
1085 }
1086 continue;
1087 }
1088
1089 if (!live) {
1090 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
1091 if (sw->active) {
1092 free = audio_get_free (sw);
1093 if (free > 0) {
1094 sw->callback.fn (sw->callback.opaque, free);
1095 }
1096 }
1097 }
1098 continue;
1099 }
1100
1101 prev_rpos = hw->rpos;
1102 played = hw->pcm_ops->run_out (hw, live);
1103 replay_audio_out(&played);
1104 if (audio_bug(__func__, hw->rpos >= hw->samples)) {
1105 dolog ("hw->rpos=%d hw->samples=%d played=%d\n",
1106 hw->rpos, hw->samples, played);
1107 hw->rpos = 0;
1108 }
1109
1110 #ifdef DEBUG_OUT
1111 dolog ("played=%d\n", played);
1112 #endif
1113
1114 if (played) {
1115 hw->ts_helper += played;
1116 audio_capture_mix_and_clear (hw, prev_rpos, played);
1117 }
1118
1119 cleanup_required = 0;
1120 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
1121 if (!sw->active && sw->empty) {
1122 continue;
1123 }
1124
1125 if (audio_bug(__func__, played > sw->total_hw_samples_mixed)) {
1126 dolog ("played=%d sw->total_hw_samples_mixed=%d\n",
1127 played, sw->total_hw_samples_mixed);
1128 played = sw->total_hw_samples_mixed;
1129 }
1130
1131 sw->total_hw_samples_mixed -= played;
1132
1133 if (!sw->total_hw_samples_mixed) {
1134 sw->empty = 1;
1135 cleanup_required |= !sw->active && !sw->callback.fn;
1136 }
1137
1138 if (sw->active) {
1139 free = audio_get_free (sw);
1140 if (free > 0) {
1141 sw->callback.fn (sw->callback.opaque, free);
1142 }
1143 }
1144 }
1145
1146 if (cleanup_required) {
1147 SWVoiceOut *sw1;
1148
1149 sw = hw->sw_head.lh_first;
1150 while (sw) {
1151 sw1 = sw->entries.le_next;
1152 if (!sw->active && !sw->callback.fn) {
1153 audio_close_out (sw);
1154 }
1155 sw = sw1;
1156 }
1157 }
1158 }
1159 }
1160
1161 static void audio_run_in (AudioState *s)
1162 {
1163 HWVoiceIn *hw = NULL;
1164
1165 while ((hw = audio_pcm_hw_find_any_enabled_in(s, hw))) {
1166 SWVoiceIn *sw;
1167 int captured = 0, min;
1168
1169 if (replay_mode != REPLAY_MODE_PLAY) {
1170 captured = hw->pcm_ops->run_in(hw);
1171 }
1172 replay_audio_in(&captured, hw->conv_buf, &hw->wpos, hw->samples);
1173
1174 min = audio_pcm_hw_find_min_in (hw);
1175 hw->total_samples_captured += captured - min;
1176 hw->ts_helper += captured;
1177
1178 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
1179 sw->total_hw_samples_acquired -= min;
1180
1181 if (sw->active) {
1182 int avail;
1183
1184 avail = audio_get_avail (sw);
1185 if (avail > 0) {
1186 sw->callback.fn (sw->callback.opaque, avail);
1187 }
1188 }
1189 }
1190 }
1191 }
1192
1193 static void audio_run_capture (AudioState *s)
1194 {
1195 CaptureVoiceOut *cap;
1196
1197 for (cap = s->cap_head.lh_first; cap; cap = cap->entries.le_next) {
1198 int live, rpos, captured;
1199 HWVoiceOut *hw = &cap->hw;
1200 SWVoiceOut *sw;
1201
1202 captured = live = audio_pcm_hw_get_live_out (hw, NULL);
1203 rpos = hw->rpos;
1204 while (live) {
1205 int left = hw->samples - rpos;
1206 int to_capture = audio_MIN (live, left);
1207 struct st_sample *src;
1208 struct capture_callback *cb;
1209
1210 src = hw->mix_buf + rpos;
1211 hw->clip (cap->buf, src, to_capture);
1212 mixeng_clear (src, to_capture);
1213
1214 for (cb = cap->cb_head.lh_first; cb; cb = cb->entries.le_next) {
1215 cb->ops.capture (cb->opaque, cap->buf,
1216 to_capture << hw->info.shift);
1217 }
1218 rpos = (rpos + to_capture) % hw->samples;
1219 live -= to_capture;
1220 }
1221 hw->rpos = rpos;
1222
1223 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
1224 if (!sw->active && sw->empty) {
1225 continue;
1226 }
1227
1228 if (audio_bug(__func__, captured > sw->total_hw_samples_mixed)) {
1229 dolog ("captured=%d sw->total_hw_samples_mixed=%d\n",
1230 captured, sw->total_hw_samples_mixed);
1231 captured = sw->total_hw_samples_mixed;
1232 }
1233
1234 sw->total_hw_samples_mixed -= captured;
1235 sw->empty = sw->total_hw_samples_mixed == 0;
1236 }
1237 }
1238 }
1239
1240 void audio_run (const char *msg)
1241 {
1242 AudioState *s;
1243
1244 QTAILQ_FOREACH(s, &audio_states, list) {
1245 audio_run_out(s);
1246 audio_run_in(s);
1247 audio_run_capture(s);
1248 }
1249
1250 #ifdef DEBUG_POLL
1251 {
1252 static double prevtime;
1253 double currtime;
1254 struct timeval tv;
1255
1256 if (gettimeofday (&tv, NULL)) {
1257 perror ("audio_run: gettimeofday");
1258 return;
1259 }
1260
1261 currtime = tv.tv_sec + tv.tv_usec * 1e-6;
1262 dolog ("Elapsed since last %s: %f\n", msg, currtime - prevtime);
1263 prevtime = currtime;
1264 }
1265 #endif
1266 }
1267
1268 static int audio_driver_init(AudioState *s, struct audio_driver *drv,
1269 bool msg, Audiodev *dev)
1270 {
1271 s->drv_opaque = drv->init(dev);
1272
1273 if (s->drv_opaque) {
1274 audio_init_nb_voices_out(s, drv);
1275 audio_init_nb_voices_in(s, drv);
1276 s->drv = drv;
1277 return 0;
1278 }
1279 else {
1280 if (msg) {
1281 dolog("Could not init `%s' audio driver\n", drv->name);
1282 }
1283 return -1;
1284 }
1285 }
1286
1287 static void audio_vm_change_state_handler (void *opaque, int running,
1288 RunState state)
1289 {
1290 AudioState *s = opaque;
1291 HWVoiceOut *hwo = NULL;
1292 HWVoiceIn *hwi = NULL;
1293 int op = running ? VOICE_ENABLE : VOICE_DISABLE;
1294
1295 s->vm_running = running;
1296 while ((hwo = audio_pcm_hw_find_any_enabled_out(s, hwo))) {
1297 hwo->pcm_ops->ctl_out(hwo, op);
1298 }
1299
1300 while ((hwi = audio_pcm_hw_find_any_enabled_in(s, hwi))) {
1301 hwi->pcm_ops->ctl_in(hwi, op);
1302 }
1303 audio_reset_timer (s);
1304 }
1305
1306 static bool is_cleaning_up;
1307
1308 bool audio_is_cleaning_up(void)
1309 {
1310 return is_cleaning_up;
1311 }
1312
1313 static void free_audio_state(AudioState *s)
1314 {
1315 HWVoiceOut *hwo, *hwon;
1316 HWVoiceIn *hwi, *hwin;
1317
1318 QLIST_FOREACH_SAFE(hwo, &s->hw_head_out, entries, hwon) {
1319 SWVoiceCap *sc;
1320
1321 if (hwo->enabled) {
1322 hwo->pcm_ops->ctl_out (hwo, VOICE_DISABLE);
1323 }
1324 hwo->pcm_ops->fini_out (hwo);
1325
1326 for (sc = hwo->cap_head.lh_first; sc; sc = sc->entries.le_next) {
1327 CaptureVoiceOut *cap = sc->cap;
1328 struct capture_callback *cb;
1329
1330 for (cb = cap->cb_head.lh_first; cb; cb = cb->entries.le_next) {
1331 cb->ops.destroy (cb->opaque);
1332 }
1333 }
1334 QLIST_REMOVE(hwo, entries);
1335 }
1336
1337 QLIST_FOREACH_SAFE(hwi, &s->hw_head_in, entries, hwin) {
1338 if (hwi->enabled) {
1339 hwi->pcm_ops->ctl_in (hwi, VOICE_DISABLE);
1340 }
1341 hwi->pcm_ops->fini_in (hwi);
1342 QLIST_REMOVE(hwi, entries);
1343 }
1344
1345 if (s->drv) {
1346 s->drv->fini (s->drv_opaque);
1347 s->drv = NULL;
1348 }
1349
1350 if (s->dev) {
1351 qapi_free_Audiodev(s->dev);
1352 s->dev = NULL;
1353 }
1354 g_free(s);
1355 }
1356
1357 void audio_cleanup(void)
1358 {
1359 is_cleaning_up = true;
1360 while (!QTAILQ_EMPTY(&audio_states)) {
1361 AudioState *s = QTAILQ_FIRST(&audio_states);
1362 QTAILQ_REMOVE(&audio_states, s, list);
1363 free_audio_state(s);
1364 }
1365 }
1366
1367 static const VMStateDescription vmstate_audio = {
1368 .name = "audio",
1369 .version_id = 1,
1370 .minimum_version_id = 1,
1371 .fields = (VMStateField[]) {
1372 VMSTATE_END_OF_LIST()
1373 }
1374 };
1375
1376 static void audio_validate_opts(Audiodev *dev, Error **errp);
1377
1378 static AudiodevListEntry *audiodev_find(
1379 AudiodevListHead *head, const char *drvname)
1380 {
1381 AudiodevListEntry *e;
1382 QSIMPLEQ_FOREACH(e, head, next) {
1383 if (strcmp(AudiodevDriver_str(e->dev->driver), drvname) == 0) {
1384 return e;
1385 }
1386 }
1387
1388 return NULL;
1389 }
1390
1391 /*
1392 * if we have dev, this function was called because of an -audiodev argument =>
1393 * initialize a new state with it
1394 * if dev == NULL => legacy implicit initialization, return the already created
1395 * state or create a new one
1396 */
1397 static AudioState *audio_init(Audiodev *dev)
1398 {
1399 static bool atexit_registered;
1400 size_t i;
1401 int done = 0;
1402 const char *drvname = NULL;
1403 VMChangeStateEntry *e;
1404 AudioState *s;
1405 struct audio_driver *driver;
1406 /* silence gcc warning about uninitialized variable */
1407 AudiodevListHead head = QSIMPLEQ_HEAD_INITIALIZER(head);
1408
1409 if (dev) {
1410 /* -audiodev option */
1411 drvname = AudiodevDriver_str(dev->driver);
1412 } else if (!QTAILQ_EMPTY(&audio_states)) {
1413 /*
1414 * todo: check for -audiodev once we have normal audiodev selection
1415 * support
1416 */
1417 return QTAILQ_FIRST(&audio_states);
1418 } else {
1419 /* legacy implicit initialization */
1420 head = audio_handle_legacy_opts();
1421 /*
1422 * In case of legacy initialization, all Audiodevs in the list will have
1423 * the same configuration (except the driver), so it does't matter which
1424 * one we chose. We need an Audiodev to set up AudioState before we can
1425 * init a driver. Also note that dev at this point is still in the
1426 * list.
1427 */
1428 dev = QSIMPLEQ_FIRST(&head)->dev;
1429 audio_validate_opts(dev, &error_abort);
1430 }
1431
1432 s = g_malloc0(sizeof(AudioState));
1433 s->dev = dev;
1434
1435 QLIST_INIT (&s->hw_head_out);
1436 QLIST_INIT (&s->hw_head_in);
1437 QLIST_INIT (&s->cap_head);
1438 if (!atexit_registered) {
1439 atexit(audio_cleanup);
1440 atexit_registered = true;
1441 }
1442 QTAILQ_INSERT_TAIL(&audio_states, s, list);
1443
1444 s->ts = timer_new_ns(QEMU_CLOCK_VIRTUAL, audio_timer, s);
1445
1446 s->nb_hw_voices_out = audio_get_pdo_out(dev)->voices;
1447 s->nb_hw_voices_in = audio_get_pdo_in(dev)->voices;
1448
1449 if (s->nb_hw_voices_out <= 0) {
1450 dolog ("Bogus number of playback voices %d, setting to 1\n",
1451 s->nb_hw_voices_out);
1452 s->nb_hw_voices_out = 1;
1453 }
1454
1455 if (s->nb_hw_voices_in <= 0) {
1456 dolog ("Bogus number of capture voices %d, setting to 0\n",
1457 s->nb_hw_voices_in);
1458 s->nb_hw_voices_in = 0;
1459 }
1460
1461 if (drvname) {
1462 driver = audio_driver_lookup(drvname);
1463 if (driver) {
1464 done = !audio_driver_init(s, driver, true, dev);
1465 } else {
1466 dolog ("Unknown audio driver `%s'\n", drvname);
1467 }
1468 } else {
1469 for (i = 0; audio_prio_list[i]; i++) {
1470 AudiodevListEntry *e = audiodev_find(&head, audio_prio_list[i]);
1471 driver = audio_driver_lookup(audio_prio_list[i]);
1472
1473 if (e && driver) {
1474 s->dev = dev = e->dev;
1475 audio_validate_opts(dev, &error_abort);
1476 done = !audio_driver_init(s, driver, false, dev);
1477 if (done) {
1478 e->dev = NULL;
1479 break;
1480 }
1481 }
1482 }
1483 }
1484 audio_free_audiodev_list(&head);
1485
1486 if (!done) {
1487 driver = audio_driver_lookup("none");
1488 done = !audio_driver_init(s, driver, false, dev);
1489 assert(done);
1490 dolog("warning: Using timer based audio emulation\n");
1491 }
1492
1493 if (dev->timer_period <= 0) {
1494 s->period_ticks = 1;
1495 } else {
1496 s->period_ticks = dev->timer_period * SCALE_US;
1497 }
1498
1499 e = qemu_add_vm_change_state_handler (audio_vm_change_state_handler, s);
1500 if (!e) {
1501 dolog ("warning: Could not register change state handler\n"
1502 "(Audio can continue looping even after stopping the VM)\n");
1503 }
1504
1505 QLIST_INIT (&s->card_head);
1506 vmstate_register (NULL, 0, &vmstate_audio, s);
1507 return s;
1508 }
1509
1510 void audio_free_audiodev_list(AudiodevListHead *head)
1511 {
1512 AudiodevListEntry *e;
1513 while ((e = QSIMPLEQ_FIRST(head))) {
1514 QSIMPLEQ_REMOVE_HEAD(head, next);
1515 qapi_free_Audiodev(e->dev);
1516 g_free(e);
1517 }
1518 }
1519
1520 void AUD_register_card (const char *name, QEMUSoundCard *card)
1521 {
1522 if (!card->state) {
1523 card->state = audio_init(NULL);
1524 }
1525
1526 card->name = g_strdup (name);
1527 memset (&card->entries, 0, sizeof (card->entries));
1528 QLIST_INSERT_HEAD(&card->state->card_head, card, entries);
1529 }
1530
1531 void AUD_remove_card (QEMUSoundCard *card)
1532 {
1533 QLIST_REMOVE (card, entries);
1534 g_free (card->name);
1535 }
1536
1537
1538 CaptureVoiceOut *AUD_add_capture(
1539 AudioState *s,
1540 struct audsettings *as,
1541 struct audio_capture_ops *ops,
1542 void *cb_opaque
1543 )
1544 {
1545 CaptureVoiceOut *cap;
1546 struct capture_callback *cb;
1547
1548 if (!s) {
1549 /* todo: remove when we have normal audiodev selection support */
1550 s = audio_init(NULL);
1551 }
1552
1553 if (audio_validate_settings (as)) {
1554 dolog ("Invalid settings were passed when trying to add capture\n");
1555 audio_print_settings (as);
1556 return NULL;
1557 }
1558
1559 cb = g_malloc0(sizeof(*cb));
1560 cb->ops = *ops;
1561 cb->opaque = cb_opaque;
1562
1563 cap = audio_pcm_capture_find_specific(s, as);
1564 if (cap) {
1565 QLIST_INSERT_HEAD (&cap->cb_head, cb, entries);
1566 return cap;
1567 }
1568 else {
1569 HWVoiceOut *hw;
1570 CaptureVoiceOut *cap;
1571
1572 cap = g_malloc0(sizeof(*cap));
1573
1574 hw = &cap->hw;
1575 hw->s = s;
1576 QLIST_INIT (&hw->sw_head);
1577 QLIST_INIT (&cap->cb_head);
1578
1579 /* XXX find a more elegant way */
1580 hw->samples = 4096 * 4;
1581 hw->mix_buf = g_new0(struct st_sample, hw->samples);
1582
1583 audio_pcm_init_info (&hw->info, as);
1584
1585 cap->buf = g_malloc0_n(hw->samples, 1 << hw->info.shift);
1586
1587 hw->clip = mixeng_clip
1588 [hw->info.nchannels == 2]
1589 [hw->info.sign]
1590 [hw->info.swap_endianness]
1591 [audio_bits_to_index (hw->info.bits)];
1592
1593 QLIST_INSERT_HEAD (&s->cap_head, cap, entries);
1594 QLIST_INSERT_HEAD (&cap->cb_head, cb, entries);
1595
1596 QLIST_FOREACH(hw, &s->hw_head_out, entries) {
1597 audio_attach_capture (hw);
1598 }
1599 return cap;
1600 }
1601 }
1602
1603 void AUD_del_capture (CaptureVoiceOut *cap, void *cb_opaque)
1604 {
1605 struct capture_callback *cb;
1606
1607 for (cb = cap->cb_head.lh_first; cb; cb = cb->entries.le_next) {
1608 if (cb->opaque == cb_opaque) {
1609 cb->ops.destroy (cb_opaque);
1610 QLIST_REMOVE (cb, entries);
1611 g_free (cb);
1612
1613 if (!cap->cb_head.lh_first) {
1614 SWVoiceOut *sw = cap->hw.sw_head.lh_first, *sw1;
1615
1616 while (sw) {
1617 SWVoiceCap *sc = (SWVoiceCap *) sw;
1618 #ifdef DEBUG_CAPTURE
1619 dolog ("freeing %s\n", sw->name);
1620 #endif
1621
1622 sw1 = sw->entries.le_next;
1623 if (sw->rate) {
1624 st_rate_stop (sw->rate);
1625 sw->rate = NULL;
1626 }
1627 QLIST_REMOVE (sw, entries);
1628 QLIST_REMOVE (sc, entries);
1629 g_free (sc);
1630 sw = sw1;
1631 }
1632 QLIST_REMOVE (cap, entries);
1633 g_free (cap->hw.mix_buf);
1634 g_free (cap->buf);
1635 g_free (cap);
1636 }
1637 return;
1638 }
1639 }
1640 }
1641
1642 void AUD_set_volume_out (SWVoiceOut *sw, int mute, uint8_t lvol, uint8_t rvol)
1643 {
1644 if (sw) {
1645 HWVoiceOut *hw = sw->hw;
1646
1647 sw->vol.mute = mute;
1648 sw->vol.l = nominal_volume.l * lvol / 255;
1649 sw->vol.r = nominal_volume.r * rvol / 255;
1650
1651 if (hw->pcm_ops->ctl_out) {
1652 hw->pcm_ops->ctl_out (hw, VOICE_VOLUME, sw);
1653 }
1654 }
1655 }
1656
1657 void AUD_set_volume_in (SWVoiceIn *sw, int mute, uint8_t lvol, uint8_t rvol)
1658 {
1659 if (sw) {
1660 HWVoiceIn *hw = sw->hw;
1661
1662 sw->vol.mute = mute;
1663 sw->vol.l = nominal_volume.l * lvol / 255;
1664 sw->vol.r = nominal_volume.r * rvol / 255;
1665
1666 if (hw->pcm_ops->ctl_in) {
1667 hw->pcm_ops->ctl_in (hw, VOICE_VOLUME, sw);
1668 }
1669 }
1670 }
1671
1672 void audio_create_pdos(Audiodev *dev)
1673 {
1674 switch (dev->driver) {
1675 #define CASE(DRIVER, driver, pdo_name) \
1676 case AUDIODEV_DRIVER_##DRIVER: \
1677 if (!dev->u.driver.has_in) { \
1678 dev->u.driver.in = g_malloc0( \
1679 sizeof(Audiodev##pdo_name##PerDirectionOptions)); \
1680 dev->u.driver.has_in = true; \
1681 } \
1682 if (!dev->u.driver.has_out) { \
1683 dev->u.driver.out = g_malloc0( \
1684 sizeof(AudiodevAlsaPerDirectionOptions)); \
1685 dev->u.driver.has_out = true; \
1686 } \
1687 break
1688
1689 CASE(NONE, none, );
1690 CASE(ALSA, alsa, Alsa);
1691 CASE(COREAUDIO, coreaudio, Coreaudio);
1692 CASE(DSOUND, dsound, );
1693 CASE(OSS, oss, Oss);
1694 CASE(PA, pa, Pa);
1695 CASE(SDL, sdl, );
1696 CASE(SPICE, spice, );
1697 CASE(WAV, wav, );
1698
1699 case AUDIODEV_DRIVER__MAX:
1700 abort();
1701 };
1702 }
1703
1704 static void audio_validate_per_direction_opts(
1705 AudiodevPerDirectionOptions *pdo, Error **errp)
1706 {
1707 if (!pdo->has_fixed_settings) {
1708 pdo->has_fixed_settings = true;
1709 pdo->fixed_settings = true;
1710 }
1711 if (!pdo->fixed_settings &&
1712 (pdo->has_frequency || pdo->has_channels || pdo->has_format)) {
1713 error_setg(errp,
1714 "You can't use frequency, channels or format with fixed-settings=off");
1715 return;
1716 }
1717
1718 if (!pdo->has_frequency) {
1719 pdo->has_frequency = true;
1720 pdo->frequency = 44100;
1721 }
1722 if (!pdo->has_channels) {
1723 pdo->has_channels = true;
1724 pdo->channels = 2;
1725 }
1726 if (!pdo->has_voices) {
1727 pdo->has_voices = true;
1728 pdo->voices = 1;
1729 }
1730 if (!pdo->has_format) {
1731 pdo->has_format = true;
1732 pdo->format = AUDIO_FORMAT_S16;
1733 }
1734 }
1735
1736 static void audio_validate_opts(Audiodev *dev, Error **errp)
1737 {
1738 Error *err = NULL;
1739
1740 audio_create_pdos(dev);
1741
1742 audio_validate_per_direction_opts(audio_get_pdo_in(dev), &err);
1743 if (err) {
1744 error_propagate(errp, err);
1745 return;
1746 }
1747
1748 audio_validate_per_direction_opts(audio_get_pdo_out(dev), &err);
1749 if (err) {
1750 error_propagate(errp, err);
1751 return;
1752 }
1753
1754 if (!dev->has_timer_period) {
1755 dev->has_timer_period = true;
1756 dev->timer_period = 10000; /* 100Hz -> 10ms */
1757 }
1758 }
1759
1760 void audio_parse_option(const char *opt)
1761 {
1762 AudiodevListEntry *e;
1763 Audiodev *dev = NULL;
1764
1765 Visitor *v = qobject_input_visitor_new_str(opt, "driver", &error_fatal);
1766 visit_type_Audiodev(v, NULL, &dev, &error_fatal);
1767 visit_free(v);
1768
1769 audio_validate_opts(dev, &error_fatal);
1770
1771 e = g_malloc0(sizeof(AudiodevListEntry));
1772 e->dev = dev;
1773 QSIMPLEQ_INSERT_TAIL(&audiodevs, e, next);
1774 }
1775
1776 void audio_init_audiodevs(void)
1777 {
1778 AudiodevListEntry *e;
1779
1780 QSIMPLEQ_FOREACH(e, &audiodevs, next) {
1781 audio_init(e->dev);
1782 }
1783 }
1784
1785 audsettings audiodev_to_audsettings(AudiodevPerDirectionOptions *pdo)
1786 {
1787 return (audsettings) {
1788 .freq = pdo->frequency,
1789 .nchannels = pdo->channels,
1790 .fmt = pdo->format,
1791 .endianness = AUDIO_HOST_ENDIANNESS,
1792 };
1793 }
1794
1795 int audioformat_bytes_per_sample(AudioFormat fmt)
1796 {
1797 switch (fmt) {
1798 case AUDIO_FORMAT_U8:
1799 case AUDIO_FORMAT_S8:
1800 return 1;
1801
1802 case AUDIO_FORMAT_U16:
1803 case AUDIO_FORMAT_S16:
1804 return 2;
1805
1806 case AUDIO_FORMAT_U32:
1807 case AUDIO_FORMAT_S32:
1808 return 4;
1809
1810 case AUDIO_FORMAT__MAX:
1811 ;
1812 }
1813 abort();
1814 }
1815
1816
1817 /* frames = freq * usec / 1e6 */
1818 int audio_buffer_frames(AudiodevPerDirectionOptions *pdo,
1819 audsettings *as, int def_usecs)
1820 {
1821 uint64_t usecs = pdo->has_buffer_length ? pdo->buffer_length : def_usecs;
1822 return (as->freq * usecs + 500000) / 1000000;
1823 }
1824
1825 /* samples = channels * frames = channels * freq * usec / 1e6 */
1826 int audio_buffer_samples(AudiodevPerDirectionOptions *pdo,
1827 audsettings *as, int def_usecs)
1828 {
1829 return as->nchannels * audio_buffer_frames(pdo, as, def_usecs);
1830 }
1831
1832 /*
1833 * bytes = bytes_per_sample * samples =
1834 * bytes_per_sample * channels * freq * usec / 1e6
1835 */
1836 int audio_buffer_bytes(AudiodevPerDirectionOptions *pdo,
1837 audsettings *as, int def_usecs)
1838 {
1839 return audio_buffer_samples(pdo, as, def_usecs) *
1840 audioformat_bytes_per_sample(as->fmt);
1841 }
1842
1843 AudioState *audio_state_by_name(const char *name)
1844 {
1845 AudioState *s;
1846 QTAILQ_FOREACH(s, &audio_states, list) {
1847 assert(s->dev);
1848 if (strcmp(name, s->dev->id) == 0) {
1849 return s;
1850 }
1851 }
1852 return NULL;
1853 }
1854
1855 const char *audio_get_id(QEMUSoundCard *card)
1856 {
1857 if (card->state) {
1858 assert(card->state->dev);
1859 return card->state->dev->id;
1860 } else {
1861 return "";
1862 }
1863 }