]> git.proxmox.com Git - mirror_qemu.git/blame - audio/audio.c
qapi, audio: add query-audiodev command
[mirror_qemu.git] / audio / audio.c
CommitLineData
85571bc7
FB
1/*
2 * QEMU Audio subsystem
1d14ffa9
FB
3 *
4 * Copyright (c) 2003-2005 Vassili Karpov (malc)
5 *
85571bc7
FB
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 */
0b8fa32f 24
6086a565 25#include "qemu/osdep.h"
87ecb68b 26#include "audio.h"
d6454270 27#include "migration/vmstate.h"
83c9089e 28#include "monitor/monitor.h"
1de7afc9 29#include "qemu/timer.h"
71830221 30#include "qapi/error.h"
637d1809 31#include "qapi/clone-visitor.h"
71830221
KZ
32#include "qapi/qobject-input-visitor.h"
33#include "qapi/qapi-visit-audio.h"
637d1809 34#include "qapi/qapi-commands-audio.h"
f348b6d1 35#include "qemu/cutils.h"
0b8fa32f 36#include "qemu/module.h"
5e03b6da 37#include "qemu/help_option.h"
89fc45d5 38#include "sysemu/sysemu.h"
3d4d16f4 39#include "sysemu/replay.h"
54d31236 40#include "sysemu/runstate.h"
f0c4555e 41#include "ui/qemu-spice.h"
1a961e78 42#include "trace.h"
85571bc7 43
1d14ffa9
FB
44#define AUDIO_CAP "audio"
45#include "audio_int.h"
85571bc7 46
1d14ffa9
FB
47/* #define DEBUG_LIVE */
48/* #define DEBUG_OUT */
8ead62cf 49/* #define DEBUG_CAPTURE */
713a98f8 50/* #define DEBUG_POLL */
85571bc7 51
c0fe3827
FB
52#define SW_NAME(sw) (sw)->name ? (sw)->name : "unknown"
53
2358a494
JQ
54
55/* Order of CONFIG_AUDIO_DRIVERS is import.
56 The 1st one is the one used by default, that is the reason
57 that we generate the list.
58*/
71830221 59const char *audio_prio_list[] = {
d3893a39 60 "spice",
2358a494 61 CONFIG_AUDIO_DRIVERS
d3893a39
GH
62 "none",
63 "wav",
71830221 64 NULL
1d14ffa9 65};
85571bc7 66
d3893a39 67static QLIST_HEAD(, audio_driver) audio_drivers;
71830221 68static AudiodevListHead audiodevs = QSIMPLEQ_HEAD_INITIALIZER(audiodevs);
d3893a39
GH
69
70void audio_driver_register(audio_driver *drv)
71{
72 QLIST_INSERT_HEAD(&audio_drivers, drv, next);
73}
74
75audio_driver *audio_driver_lookup(const char *name)
76{
77 struct audio_driver *d;
c551fb0b
CF
78 Error *local_err = NULL;
79 int rv;
d3893a39
GH
80
81 QLIST_FOREACH(d, &audio_drivers, next) {
82 if (strcmp(name, d->name) == 0) {
83 return d;
84 }
85 }
c551fb0b
CF
86 rv = audio_module_load(name, &local_err);
87 if (rv > 0) {
88 QLIST_FOREACH(d, &audio_drivers, next) {
89 if (strcmp(name, d->name) == 0) {
90 return d;
91 }
65ba8696 92 }
c551fb0b
CF
93 } else if (rv < 0) {
94 error_report_err(local_err);
65ba8696 95 }
d3893a39
GH
96 return NULL;
97}
98
ecd97e95
KZ
99static QTAILQ_HEAD(AudioStateHead, AudioState) audio_states =
100 QTAILQ_HEAD_INITIALIZER(audio_states);
c0fe3827 101
00e07679 102const struct mixeng_volume nominal_volume = {
14658cd1 103 .mute = 0,
1d14ffa9 104#ifdef FLOAT_MIXENG
14658cd1
JQ
105 .r = 1.0,
106 .l = 1.0,
1d14ffa9 107#else
14658cd1
JQ
108 .r = 1ULL << 32,
109 .l = 1ULL << 32,
1d14ffa9 110#endif
85571bc7
FB
111};
112
af2041ed
KZ
113static bool legacy_config = true;
114
1d14ffa9 115int audio_bug (const char *funcname, int cond)
85571bc7 116{
1d14ffa9
FB
117 if (cond) {
118 static int shown;
119
8ead62cf 120 AUD_log (NULL, "A bug was just triggered in %s\n", funcname);
1d14ffa9
FB
121 if (!shown) {
122 shown = 1;
123 AUD_log (NULL, "Save all your work and restart without audio\n");
1d14ffa9
FB
124 AUD_log (NULL, "I am sorry\n");
125 }
126 AUD_log (NULL, "Context:\n");
85571bc7
FB
127 }
128
1d14ffa9 129 return cond;
85571bc7
FB
130}
131
f941aa25
TS
132static inline int audio_bits_to_index (int bits)
133{
134 switch (bits) {
135 case 8:
136 return 0;
137
138 case 16:
139 return 1;
140
141 case 32:
142 return 2;
143
144 default:
145 audio_bug ("bits_to_index", 1);
146 AUD_log (NULL, "invalid bits %d\n", bits);
12f4abf6 147 return 0;
f941aa25
TS
148 }
149}
150
c0fe3827
FB
151void *audio_calloc (const char *funcname, int nmemb, size_t size)
152{
153 int cond;
154 size_t len;
155
156 len = nmemb * size;
157 cond = !nmemb || !size;
158 cond |= nmemb < 0;
159 cond |= len < size;
160
161 if (audio_bug ("audio_calloc", cond)) {
162 AUD_log (NULL, "%s passed invalid arguments to audio_calloc\n",
163 funcname);
541e0844 164 AUD_log (NULL, "nmemb=%d size=%zu (len=%zu)\n", nmemb, size, len);
12f4abf6 165 return NULL;
c0fe3827
FB
166 }
167
7267c094 168 return g_malloc0 (len);
c0fe3827
FB
169}
170
541e0844 171void AUD_vlog (const char *cap, const char *fmt, va_list ap)
85571bc7 172{
06ac27f6
KZ
173 if (cap) {
174 fprintf(stderr, "%s: ", cap);
541e0844 175 }
541e0844 176
06ac27f6 177 vfprintf(stderr, fmt, ap);
85571bc7
FB
178}
179
541e0844 180void AUD_log (const char *cap, const char *fmt, ...)
85571bc7 181{
541e0844
FB
182 va_list ap;
183
184 va_start (ap, fmt);
185 AUD_vlog (cap, fmt, ap);
186 va_end (ap);
85571bc7
FB
187}
188
1ea879e5 189static void audio_print_settings (struct audsettings *as)
c0fe3827
FB
190{
191 dolog ("frequency=%d nchannels=%d fmt=", as->freq, as->nchannels);
192
193 switch (as->fmt) {
85bc5852 194 case AUDIO_FORMAT_S8:
c0fe3827
FB
195 AUD_log (NULL, "S8");
196 break;
85bc5852 197 case AUDIO_FORMAT_U8:
c0fe3827
FB
198 AUD_log (NULL, "U8");
199 break;
85bc5852 200 case AUDIO_FORMAT_S16:
c0fe3827
FB
201 AUD_log (NULL, "S16");
202 break;
85bc5852 203 case AUDIO_FORMAT_U16:
c0fe3827
FB
204 AUD_log (NULL, "U16");
205 break;
85bc5852 206 case AUDIO_FORMAT_S32:
d50997f9 207 AUD_log (NULL, "S32");
208 break;
85bc5852 209 case AUDIO_FORMAT_U32:
d50997f9 210 AUD_log (NULL, "U32");
211 break;
ed2a4a79
KZ
212 case AUDIO_FORMAT_F32:
213 AUD_log (NULL, "F32");
214 break;
c0fe3827
FB
215 default:
216 AUD_log (NULL, "invalid(%d)", as->fmt);
217 break;
218 }
ec36b695
FB
219
220 AUD_log (NULL, " endianness=");
d929eba5
FB
221 switch (as->endianness) {
222 case 0:
223 AUD_log (NULL, "little");
224 break;
225 case 1:
226 AUD_log (NULL, "big");
227 break;
228 default:
229 AUD_log (NULL, "invalid");
230 break;
231 }
c0fe3827
FB
232 AUD_log (NULL, "\n");
233}
234
1ea879e5 235static int audio_validate_settings (struct audsettings *as)
c0fe3827
FB
236{
237 int invalid;
238
b5c7db3e 239 invalid = as->nchannels < 1;
d929eba5 240 invalid |= as->endianness != 0 && as->endianness != 1;
c0fe3827
FB
241
242 switch (as->fmt) {
85bc5852
KZ
243 case AUDIO_FORMAT_S8:
244 case AUDIO_FORMAT_U8:
245 case AUDIO_FORMAT_S16:
246 case AUDIO_FORMAT_U16:
247 case AUDIO_FORMAT_S32:
248 case AUDIO_FORMAT_U32:
ed2a4a79 249 case AUDIO_FORMAT_F32:
c0fe3827
FB
250 break;
251 default:
252 invalid = 1;
253 break;
254 }
255
256 invalid |= as->freq <= 0;
d929eba5 257 return invalid ? -1 : 0;
c0fe3827
FB
258}
259
1ea879e5 260static int audio_pcm_info_eq (struct audio_pcm_info *info, struct audsettings *as)
85571bc7 261{
ed2a4a79
KZ
262 int bits = 8;
263 bool is_signed = false, is_float = false;
85571bc7 264
c0fe3827 265 switch (as->fmt) {
85bc5852 266 case AUDIO_FORMAT_S8:
ed2a4a79 267 is_signed = true;
b4bd0b16 268 /* fall through */
85bc5852 269 case AUDIO_FORMAT_U8:
1d14ffa9
FB
270 break;
271
85bc5852 272 case AUDIO_FORMAT_S16:
ed2a4a79 273 is_signed = true;
b4bd0b16 274 /* fall through */
85bc5852 275 case AUDIO_FORMAT_U16:
1d14ffa9
FB
276 bits = 16;
277 break;
f941aa25 278
ed2a4a79
KZ
279 case AUDIO_FORMAT_F32:
280 is_float = true;
281 /* fall through */
85bc5852 282 case AUDIO_FORMAT_S32:
ed2a4a79 283 is_signed = true;
b4bd0b16 284 /* fall through */
85bc5852 285 case AUDIO_FORMAT_U32:
f941aa25
TS
286 bits = 32;
287 break;
85bc5852
KZ
288
289 default:
290 abort();
85571bc7 291 }
c0fe3827
FB
292 return info->freq == as->freq
293 && info->nchannels == as->nchannels
ed2a4a79
KZ
294 && info->is_signed == is_signed
295 && info->is_float == is_float
d929eba5
FB
296 && info->bits == bits
297 && info->swap_endianness == (as->endianness != AUDIO_HOST_ENDIANNESS);
1d14ffa9 298}
85571bc7 299
1ea879e5 300void audio_pcm_init_info (struct audio_pcm_info *info, struct audsettings *as)
1d14ffa9 301{
ed2a4a79
KZ
302 int bits = 8, mul;
303 bool is_signed = false, is_float = false;
1d14ffa9 304
c0fe3827 305 switch (as->fmt) {
85bc5852 306 case AUDIO_FORMAT_S8:
ed2a4a79 307 is_signed = true;
f7621fd1 308 /* fall through */
85bc5852 309 case AUDIO_FORMAT_U8:
2b9cce8c 310 mul = 1;
85571bc7
FB
311 break;
312
85bc5852 313 case AUDIO_FORMAT_S16:
ed2a4a79 314 is_signed = true;
e4634941 315 /* fall through */
85bc5852 316 case AUDIO_FORMAT_U16:
85571bc7 317 bits = 16;
2b9cce8c 318 mul = 2;
f941aa25
TS
319 break;
320
ed2a4a79
KZ
321 case AUDIO_FORMAT_F32:
322 is_float = true;
323 /* fall through */
85bc5852 324 case AUDIO_FORMAT_S32:
ed2a4a79 325 is_signed = true;
e4634941 326 /* fall through */
85bc5852 327 case AUDIO_FORMAT_U32:
f941aa25 328 bits = 32;
2b9cce8c 329 mul = 4;
85571bc7 330 break;
85bc5852
KZ
331
332 default:
333 abort();
85571bc7
FB
334 }
335
c0fe3827 336 info->freq = as->freq;
1d14ffa9 337 info->bits = bits;
ed2a4a79
KZ
338 info->is_signed = is_signed;
339 info->is_float = is_float;
c0fe3827 340 info->nchannels = as->nchannels;
2b9cce8c
KZ
341 info->bytes_per_frame = as->nchannels * mul;
342 info->bytes_per_second = info->freq * info->bytes_per_frame;
d929eba5 343 info->swap_endianness = (as->endianness != AUDIO_HOST_ENDIANNESS);
85571bc7
FB
344}
345
1d14ffa9 346void audio_pcm_info_clear_buf (struct audio_pcm_info *info, void *buf, int len)
85571bc7 347{
1d14ffa9
FB
348 if (!len) {
349 return;
350 }
351
ed2a4a79 352 if (info->is_signed || info->is_float) {
2b9cce8c 353 memset(buf, 0x00, len * info->bytes_per_frame);
6c6886bd 354 } else {
f941aa25
TS
355 switch (info->bits) {
356 case 8:
2b9cce8c 357 memset(buf, 0x80, len * info->bytes_per_frame);
f941aa25 358 break;
1d14ffa9 359
f941aa25
TS
360 case 16:
361 {
362 int i;
363 uint16_t *p = buf;
f941aa25
TS
364 short s = INT16_MAX;
365
366 if (info->swap_endianness) {
367 s = bswap16 (s);
368 }
369
2b9cce8c 370 for (i = 0; i < len * info->nchannels; i++) {
f941aa25
TS
371 p[i] = s;
372 }
1d14ffa9 373 }
f941aa25
TS
374 break;
375
376 case 32:
377 {
378 int i;
379 uint32_t *p = buf;
f941aa25
TS
380 int32_t s = INT32_MAX;
381
382 if (info->swap_endianness) {
383 s = bswap32 (s);
384 }
1d14ffa9 385
2b9cce8c 386 for (i = 0; i < len * info->nchannels; i++) {
f941aa25
TS
387 p[i] = s;
388 }
1d14ffa9 389 }
f941aa25
TS
390 break;
391
392 default:
393 AUD_log (NULL, "audio_pcm_info_clear_buf: invalid bits %d\n",
394 info->bits);
395 break;
1d14ffa9 396 }
85571bc7
FB
397 }
398}
399
8ead62cf
FB
400/*
401 * Capture
402 */
00e07679 403static void noop_conv (struct st_sample *dst, const void *src, int samples)
8ead62cf
FB
404{
405 (void) src;
406 (void) dst;
407 (void) samples;
8ead62cf
FB
408}
409
526fb058
KZ
410static CaptureVoiceOut *audio_pcm_capture_find_specific(AudioState *s,
411 struct audsettings *as)
8ead62cf
FB
412{
413 CaptureVoiceOut *cap;
8ead62cf
FB
414
415 for (cap = s->cap_head.lh_first; cap; cap = cap->entries.le_next) {
d929eba5 416 if (audio_pcm_info_eq (&cap->hw.info, as)) {
8ead62cf
FB
417 return cap;
418 }
419 }
420 return NULL;
421}
422
ec36b695 423static void audio_notify_capture (CaptureVoiceOut *cap, audcnotification_e cmd)
8ead62cf 424{
ec36b695
FB
425 struct capture_callback *cb;
426
427#ifdef DEBUG_CAPTURE
428 dolog ("notification %d sent\n", cmd);
429#endif
430 for (cb = cap->cb_head.lh_first; cb; cb = cb->entries.le_next) {
431 cb->ops.notify (cb->opaque, cmd);
432 }
433}
8ead62cf 434
ec36b695
FB
435static void audio_capture_maybe_changed (CaptureVoiceOut *cap, int enabled)
436{
437 if (cap->hw.enabled != enabled) {
438 audcnotification_e cmd;
8ead62cf 439 cap->hw.enabled = enabled;
ec36b695
FB
440 cmd = enabled ? AUD_CNOTIFY_ENABLE : AUD_CNOTIFY_DISABLE;
441 audio_notify_capture (cap, cmd);
8ead62cf
FB
442 }
443}
444
445static void audio_recalc_and_notify_capture (CaptureVoiceOut *cap)
446{
447 HWVoiceOut *hw = &cap->hw;
448 SWVoiceOut *sw;
449 int enabled = 0;
450
ec36b695 451 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
8ead62cf
FB
452 if (sw->active) {
453 enabled = 1;
454 break;
455 }
456 }
ec36b695 457 audio_capture_maybe_changed (cap, enabled);
8ead62cf
FB
458}
459
460static void audio_detach_capture (HWVoiceOut *hw)
461{
ec36b695
FB
462 SWVoiceCap *sc = hw->cap_head.lh_first;
463
464 while (sc) {
465 SWVoiceCap *sc1 = sc->entries.le_next;
466 SWVoiceOut *sw = &sc->sw;
467 CaptureVoiceOut *cap = sc->cap;
468 int was_active = sw->active;
8ead62cf 469
8ead62cf
FB
470 if (sw->rate) {
471 st_rate_stop (sw->rate);
472 sw->rate = NULL;
473 }
474
72cf2d4f
BS
475 QLIST_REMOVE (sw, entries);
476 QLIST_REMOVE (sc, entries);
7267c094 477 g_free (sc);
ec36b695
FB
478 if (was_active) {
479 /* We have removed soft voice from the capture:
480 this might have changed the overall status of the capture
481 since this might have been the only active voice */
482 audio_recalc_and_notify_capture (cap);
483 }
484 sc = sc1;
8ead62cf
FB
485 }
486}
487
1a7dafce 488static int audio_attach_capture (HWVoiceOut *hw)
8ead62cf 489{
526fb058 490 AudioState *s = hw->s;
8ead62cf
FB
491 CaptureVoiceOut *cap;
492
493 audio_detach_capture (hw);
494 for (cap = s->cap_head.lh_first; cap; cap = cap->entries.le_next) {
ec36b695 495 SWVoiceCap *sc;
8ead62cf 496 SWVoiceOut *sw;
ec36b695 497 HWVoiceOut *hw_cap = &cap->hw;
8ead62cf 498
e8d85444 499 sc = g_malloc0(sizeof(*sc));
8ead62cf 500
ec36b695
FB
501 sc->cap = cap;
502 sw = &sc->sw;
8ead62cf 503 sw->hw = hw_cap;
ec36b695 504 sw->info = hw->info;
8ead62cf
FB
505 sw->empty = 1;
506 sw->active = hw->enabled;
507 sw->conv = noop_conv;
508 sw->ratio = ((int64_t) hw_cap->info.freq << 32) / sw->info.freq;
83617103 509 sw->vol = nominal_volume;
8ead62cf
FB
510 sw->rate = st_rate_start (sw->info.freq, hw_cap->info.freq);
511 if (!sw->rate) {
512 dolog ("Could not start rate conversion for `%s'\n", SW_NAME (sw));
7267c094 513 g_free (sw);
8ead62cf
FB
514 return -1;
515 }
72cf2d4f
BS
516 QLIST_INSERT_HEAD (&hw_cap->sw_head, sw, entries);
517 QLIST_INSERT_HEAD (&hw->cap_head, sc, entries);
ec36b695 518#ifdef DEBUG_CAPTURE
457b6543
SW
519 sw->name = g_strdup_printf ("for %p %d,%d,%d",
520 hw, sw->info.freq, sw->info.bits,
521 sw->info.nchannels);
ec36b695
FB
522 dolog ("Added %s active = %d\n", sw->name, sw->active);
523#endif
8ead62cf 524 if (sw->active) {
ec36b695 525 audio_capture_maybe_changed (cap, 1);
8ead62cf
FB
526 }
527 }
528 return 0;
529}
530
1d14ffa9
FB
531/*
532 * Hard voice (capture)
533 */
7520462b 534static size_t audio_pcm_hw_find_min_in (HWVoiceIn *hw)
85571bc7 535{
1d14ffa9 536 SWVoiceIn *sw;
7520462b 537 size_t m = hw->total_samples_captured;
1d14ffa9
FB
538
539 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
540 if (sw->active) {
58935915 541 m = MIN (m, sw->total_hw_samples_acquired);
1d14ffa9 542 }
85571bc7 543 }
1d14ffa9 544 return m;
85571bc7
FB
545}
546
3f5bbfc2 547static size_t audio_pcm_hw_get_live_in(HWVoiceIn *hw)
85571bc7 548{
7520462b 549 size_t live = hw->total_samples_captured - audio_pcm_hw_find_min_in (hw);
dc88e38f
KZ
550 if (audio_bug(__func__, live > hw->conv_buf->size)) {
551 dolog("live=%zu hw->conv_buf->size=%zu\n", live, hw->conv_buf->size);
12f4abf6 552 return 0;
85571bc7 553 }
1d14ffa9 554 return live;
85571bc7
FB
555}
556
251f1549
VR
557static size_t audio_pcm_hw_conv_in(HWVoiceIn *hw, void *pcm_buf, size_t samples)
558{
559 size_t conv = 0;
560 STSampleBuffer *conv_buf = hw->conv_buf;
561
562 while (samples) {
563 uint8_t *src = advance(pcm_buf, conv * hw->info.bytes_per_frame);
564 size_t proc = MIN(samples, conv_buf->size - conv_buf->pos);
565
566 hw->conv(conv_buf->samples + conv_buf->pos, src, proc);
567 conv_buf->pos = (conv_buf->pos + proc) % conv_buf->size;
568 samples -= proc;
569 conv += proc;
570 }
571
572 return conv;
573}
574
1d14ffa9
FB
575/*
576 * Soft voice (capture)
577 */
7520462b 578static size_t audio_pcm_sw_read(SWVoiceIn *sw, void *buf, size_t size)
85571bc7 579{
1d14ffa9 580 HWVoiceIn *hw = sw->hw;
7520462b 581 size_t samples, live, ret = 0, swlim, isamp, osamp, rpos, total = 0;
1ea879e5 582 struct st_sample *src, *dst = sw->buf;
1d14ffa9 583
1d14ffa9 584 live = hw->total_samples_captured - sw->total_hw_samples_acquired;
0ceb26af
VR
585 if (!live) {
586 return 0;
587 }
dc88e38f
KZ
588 if (audio_bug(__func__, live > hw->conv_buf->size)) {
589 dolog("live_in=%zu hw->conv_buf->size=%zu\n", live, hw->conv_buf->size);
12f4abf6 590 return 0;
1d14ffa9
FB
591 }
592
0ceb26af
VR
593 rpos = audio_ring_posb(hw->conv_buf->pos, live, hw->conv_buf->size);
594
2b9cce8c 595 samples = size / sw->info.bytes_per_frame;
85571bc7 596
1d14ffa9 597 swlim = (live * sw->ratio) >> 32;
58935915 598 swlim = MIN (swlim, samples);
85571bc7 599
1d14ffa9 600 while (swlim) {
dc88e38f
KZ
601 src = hw->conv_buf->samples + rpos;
602 if (hw->conv_buf->pos > rpos) {
603 isamp = hw->conv_buf->pos - rpos;
7520462b 604 } else {
dc88e38f 605 isamp = hw->conv_buf->size - rpos;
1d14ffa9 606 }
85571bc7 607
1d14ffa9
FB
608 if (!isamp) {
609 break;
610 }
611 osamp = swlim;
85571bc7 612
1d14ffa9
FB
613 st_rate_flow (sw->rate, src, dst, &isamp, &osamp);
614 swlim -= osamp;
dc88e38f 615 rpos = (rpos + isamp) % hw->conv_buf->size;
1d14ffa9
FB
616 dst += osamp;
617 ret += osamp;
618 total += isamp;
619 }
85571bc7 620
669b9522 621 if (!hw->pcm_ops->volume_in) {
c01b2456
MAL
622 mixeng_volume (sw->buf, ret, &sw->vol);
623 }
00e07679 624
571ec3d6 625 sw->clip (buf, sw->buf, ret);
1d14ffa9 626 sw->total_hw_samples_acquired += total;
2b9cce8c 627 return ret * sw->info.bytes_per_frame;
85571bc7
FB
628}
629
1d14ffa9
FB
630/*
631 * Hard voice (playback)
632 */
7520462b 633static size_t audio_pcm_hw_find_min_out (HWVoiceOut *hw, int *nb_livep)
1d14ffa9 634{
c0fe3827 635 SWVoiceOut *sw;
7520462b 636 size_t m = SIZE_MAX;
c0fe3827 637 int nb_live = 0;
85571bc7 638
c0fe3827
FB
639 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
640 if (sw->active || !sw->empty) {
58935915 641 m = MIN (m, sw->total_hw_samples_mixed);
c0fe3827
FB
642 nb_live += 1;
643 }
85571bc7 644 }
c0fe3827
FB
645
646 *nb_livep = nb_live;
647 return m;
1d14ffa9 648}
85571bc7 649
7520462b 650static size_t audio_pcm_hw_get_live_out (HWVoiceOut *hw, int *nb_live)
1d14ffa9 651{
7520462b 652 size_t smin;
bdff253c 653 int nb_live1;
85571bc7 654
bdff253c 655 smin = audio_pcm_hw_find_min_out (hw, &nb_live1);
656 if (nb_live) {
657 *nb_live = nb_live1;
85571bc7 658 }
bdff253c 659
660 if (nb_live1) {
7520462b 661 size_t live = smin;
1d14ffa9 662
dc88e38f
KZ
663 if (audio_bug(__func__, live > hw->mix_buf->size)) {
664 dolog("live=%zu hw->mix_buf->size=%zu\n", live, hw->mix_buf->size);
12f4abf6 665 return 0;
85571bc7 666 }
1d14ffa9 667 return live;
85571bc7 668 }
bdff253c 669 return 0;
85571bc7
FB
670}
671
9833438e
VR
672static size_t audio_pcm_hw_get_free(HWVoiceOut *hw)
673{
674 return (hw->pcm_ops->buffer_get_free ? hw->pcm_ops->buffer_get_free(hw) :
675 INT_MAX) / hw->info.bytes_per_frame;
676}
677
8e56a172
VR
678static void audio_pcm_hw_clip_out(HWVoiceOut *hw, void *pcm_buf, size_t len)
679{
680 size_t clipped = 0;
681 size_t pos = hw->mix_buf->pos;
682
683 while (len) {
684 st_sample *src = hw->mix_buf->samples + pos;
685 uint8_t *dst = advance(pcm_buf, clipped * hw->info.bytes_per_frame);
686 size_t samples_till_end_of_buf = hw->mix_buf->size - pos;
687 size_t samples_to_clip = MIN(len, samples_till_end_of_buf);
688
689 hw->clip(dst, src, samples_to_clip);
690
691 pos = (pos + samples_to_clip) % hw->mix_buf->size;
692 len -= samples_to_clip;
693 clipped += samples_to_clip;
694 }
695}
696
1d14ffa9
FB
697/*
698 * Soft voice (playback)
699 */
7520462b 700static size_t audio_pcm_sw_write(SWVoiceOut *sw, void *buf, size_t size)
85571bc7 701{
9833438e
VR
702 size_t hwsamples, samples, isamp, osamp, wpos, live, dead, left, blck;
703 size_t hw_free;
7520462b 704 size_t ret = 0, pos = 0, total = 0;
85571bc7 705
1d14ffa9
FB
706 if (!sw) {
707 return size;
708 }
85571bc7 709
dc88e38f 710 hwsamples = sw->hw->mix_buf->size;
85571bc7 711
1d14ffa9 712 live = sw->total_hw_samples_mixed;
7520462b 713 if (audio_bug(__func__, live > hwsamples)) {
dc88e38f 714 dolog("live=%zu hw->mix_buf->size=%zu\n", live, hwsamples);
12f4abf6 715 return 0;
1d14ffa9 716 }
85571bc7 717
1d14ffa9 718 if (live == hwsamples) {
ec36b695 719#ifdef DEBUG_OUT
0c29b786 720 dolog ("%s is full %zu\n", sw->name, live);
ec36b695 721#endif
1d14ffa9
FB
722 return 0;
723 }
85571bc7 724
dc88e38f 725 wpos = (sw->hw->mix_buf->pos + live) % hwsamples;
85571bc7 726
1d14ffa9 727 dead = hwsamples - live;
9833438e
VR
728 hw_free = audio_pcm_hw_get_free(sw->hw);
729 hw_free = hw_free > live ? hw_free - live : 0;
730 samples = ((int64_t)MIN(dead, hw_free) << 32) / sw->ratio;
731 samples = MIN(samples, size / sw->info.bytes_per_frame);
732 if (samples) {
733 sw->conv(sw->buf, buf, samples);
c01b2456 734
669b9522 735 if (!sw->hw->pcm_ops->volume_out) {
9833438e 736 mixeng_volume(sw->buf, samples, &sw->vol);
c01b2456 737 }
1d14ffa9
FB
738 }
739
9833438e 740 while (samples) {
1d14ffa9
FB
741 dead = hwsamples - live;
742 left = hwsamples - wpos;
58935915 743 blck = MIN (dead, left);
1d14ffa9
FB
744 if (!blck) {
745 break;
746 }
9833438e 747 isamp = samples;
1d14ffa9
FB
748 osamp = blck;
749 st_rate_flow_mix (
750 sw->rate,
751 sw->buf + pos,
dc88e38f 752 sw->hw->mix_buf->samples + wpos,
1d14ffa9
FB
753 &isamp,
754 &osamp
755 );
756 ret += isamp;
9833438e 757 samples -= isamp;
1d14ffa9
FB
758 pos += isamp;
759 live += osamp;
760 wpos = (wpos + osamp) % hwsamples;
761 total += osamp;
762 }
763
764 sw->total_hw_samples_mixed += total;
765 sw->empty = sw->total_hw_samples_mixed == 0;
766
767#ifdef DEBUG_OUT
768 dolog (
7520462b 769 "%s: write size %zu ret %zu total sw %zu\n",
c0fe3827 770 SW_NAME (sw),
2b9cce8c 771 size / sw->info.bytes_per_frame,
1d14ffa9 772 ret,
c0fe3827 773 sw->total_hw_samples_mixed
1d14ffa9
FB
774 );
775#endif
776
2b9cce8c 777 return ret * sw->info.bytes_per_frame;
85571bc7
FB
778}
779
1d14ffa9
FB
780#ifdef DEBUG_AUDIO
781static void audio_pcm_print_info (const char *cap, struct audio_pcm_info *info)
85571bc7 782{
ed2a4a79
KZ
783 dolog("%s: bits %d, sign %d, float %d, freq %d, nchan %d\n",
784 cap, info->bits, info->is_signed, info->is_float, info->freq,
785 info->nchannels);
85571bc7 786}
1d14ffa9 787#endif
85571bc7 788
1d14ffa9
FB
789#define DAC
790#include "audio_template.h"
791#undef DAC
792#include "audio_template.h"
793
713a98f8 794/*
795 * Timer
796 */
526fb058 797static int audio_is_timer_needed(AudioState *s)
713a98f8 798{
799 HWVoiceIn *hwi = NULL;
800 HWVoiceOut *hwo = NULL;
801
526fb058 802 while ((hwo = audio_pcm_hw_find_any_enabled_out(s, hwo))) {
6c6886bd
ZH
803 if (!hwo->poll_mode) {
804 return 1;
805 }
713a98f8 806 }
526fb058 807 while ((hwi = audio_pcm_hw_find_any_enabled_in(s, hwi))) {
6c6886bd
ZH
808 if (!hwi->poll_mode) {
809 return 1;
810 }
713a98f8 811 }
812 return 0;
813}
814
39deb1e4 815static void audio_reset_timer (AudioState *s)
713a98f8 816{
526fb058 817 if (audio_is_timer_needed(s)) {
1ffc2665 818 timer_mod_anticipate_ns(s->ts,
71830221 819 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + s->period_ticks);
526fb058
KZ
820 if (!s->timer_running) {
821 s->timer_running = true;
822 s->timer_last = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
71830221 823 trace_audio_timer_start(s->period_ticks / SCALE_MS);
1a961e78
GH
824 }
825 } else {
826 timer_del(s->ts);
526fb058
KZ
827 if (s->timer_running) {
828 s->timer_running = false;
1a961e78
GH
829 trace_audio_timer_stop();
830 }
713a98f8 831 }
832}
833
39deb1e4 834static void audio_timer (void *opaque)
835{
1a961e78 836 int64_t now, diff;
71830221 837 AudioState *s = opaque;
1a961e78
GH
838
839 now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
526fb058 840 diff = now - s->timer_last;
71830221 841 if (diff > s->period_ticks * 3 / 2) {
1a961e78
GH
842 trace_audio_timer_delayed(diff / SCALE_MS);
843 }
526fb058 844 s->timer_last = now;
1a961e78 845
18e2c177 846 audio_run(s, "timer");
71830221 847 audio_reset_timer(s);
39deb1e4 848}
849
713a98f8 850/*
851 * Public API
852 */
7520462b 853size_t AUD_write(SWVoiceOut *sw, void *buf, size_t size)
85571bc7 854{
1930616b
KZ
855 HWVoiceOut *hw;
856
1d14ffa9
FB
857 if (!sw) {
858 /* XXX: Consider options */
859 return size;
860 }
1930616b 861 hw = sw->hw;
85571bc7 862
1930616b 863 if (!hw->enabled) {
c0fe3827 864 dolog ("Writing to disabled voice %s\n", SW_NAME (sw));
85571bc7
FB
865 return 0;
866 }
867
1930616b
KZ
868 if (audio_get_pdo_out(hw->s->dev)->mixing_engine) {
869 return audio_pcm_sw_write(sw, buf, size);
870 } else {
871 return hw->pcm_ops->write(hw, buf, size);
872 }
1d14ffa9
FB
873}
874
7520462b 875size_t AUD_read(SWVoiceIn *sw, void *buf, size_t size)
1d14ffa9 876{
1930616b
KZ
877 HWVoiceIn *hw;
878
1d14ffa9
FB
879 if (!sw) {
880 /* XXX: Consider options */
881 return size;
85571bc7 882 }
1930616b 883 hw = sw->hw;
1d14ffa9 884
1930616b 885 if (!hw->enabled) {
c0fe3827 886 dolog ("Reading from disabled voice %s\n", SW_NAME (sw));
1d14ffa9 887 return 0;
85571bc7 888 }
1d14ffa9 889
1930616b
KZ
890 if (audio_get_pdo_in(hw->s->dev)->mixing_engine) {
891 return audio_pcm_sw_read(sw, buf, size);
892 } else {
893 return hw->pcm_ops->read(hw, buf, size);
894 }
85571bc7
FB
895}
896
69ac0786 897int AUD_get_buffer_size_out(SWVoiceOut *sw)
85571bc7 898{
69ac0786 899 return sw->hw->samples * sw->hw->info.bytes_per_frame;
1d14ffa9
FB
900}
901
902void AUD_set_active_out (SWVoiceOut *sw, int on)
903{
904 HWVoiceOut *hw;
85571bc7 905
1d14ffa9 906 if (!sw) {
85571bc7 907 return;
1d14ffa9 908 }
85571bc7
FB
909
910 hw = sw->hw;
1d14ffa9 911 if (sw->active != on) {
526fb058 912 AudioState *s = sw->s;
1d14ffa9 913 SWVoiceOut *temp_sw;
ec36b695 914 SWVoiceCap *sc;
1d14ffa9
FB
915
916 if (on) {
1d14ffa9
FB
917 hw->pending_disable = 0;
918 if (!hw->enabled) {
919 hw->enabled = 1;
978dd635 920 if (s->vm_running) {
571a8c52
KZ
921 if (hw->pcm_ops->enable_out) {
922 hw->pcm_ops->enable_out(hw, true);
923 }
39deb1e4 924 audio_reset_timer (s);
978dd635 925 }
1d14ffa9 926 }
6c6886bd 927 } else {
1d14ffa9
FB
928 if (hw->enabled) {
929 int nb_active = 0;
930
931 for (temp_sw = hw->sw_head.lh_first; temp_sw;
932 temp_sw = temp_sw->entries.le_next) {
933 nb_active += temp_sw->active != 0;
934 }
935
936 hw->pending_disable = nb_active == 1;
937 }
85571bc7 938 }
ec36b695
FB
939
940 for (sc = hw->cap_head.lh_first; sc; sc = sc->entries.le_next) {
941 sc->sw.active = hw->enabled;
8ead62cf 942 if (hw->enabled) {
ec36b695 943 audio_capture_maybe_changed (sc->cap, 1);
8ead62cf
FB
944 }
945 }
1d14ffa9
FB
946 sw->active = on;
947 }
948}
949
950void AUD_set_active_in (SWVoiceIn *sw, int on)
951{
952 HWVoiceIn *hw;
953
954 if (!sw) {
955 return;
85571bc7
FB
956 }
957
1d14ffa9 958 hw = sw->hw;
85571bc7 959 if (sw->active != on) {
526fb058 960 AudioState *s = sw->s;
1d14ffa9
FB
961 SWVoiceIn *temp_sw;
962
85571bc7 963 if (on) {
85571bc7
FB
964 if (!hw->enabled) {
965 hw->enabled = 1;
978dd635 966 if (s->vm_running) {
571a8c52
KZ
967 if (hw->pcm_ops->enable_in) {
968 hw->pcm_ops->enable_in(hw, true);
969 }
39deb1e4 970 audio_reset_timer (s);
978dd635 971 }
85571bc7 972 }
1d14ffa9 973 sw->total_hw_samples_acquired = hw->total_samples_captured;
6c6886bd 974 } else {
1d14ffa9 975 if (hw->enabled) {
85571bc7 976 int nb_active = 0;
1d14ffa9
FB
977
978 for (temp_sw = hw->sw_head.lh_first; temp_sw;
979 temp_sw = temp_sw->entries.le_next) {
980 nb_active += temp_sw->active != 0;
85571bc7
FB
981 }
982
983 if (nb_active == 1) {
1d14ffa9 984 hw->enabled = 0;
571a8c52
KZ
985 if (hw->pcm_ops->enable_in) {
986 hw->pcm_ops->enable_in(hw, false);
987 }
85571bc7
FB
988 }
989 }
990 }
991 sw->active = on;
992 }
993}
994
0724c579
VR
995/**
996 * audio_frontend_frames_in() - returns the number of frames the resampling
997 * code generates from frames_in frames
998 *
999 * @sw: audio recording frontend
1000 * @frames_in: number of frames
1001 */
1002static size_t audio_frontend_frames_in(SWVoiceIn *sw, size_t frames_in)
1003{
b73ef11f 1004 return (int64_t)frames_in * sw->ratio >> 32;
0724c579
VR
1005}
1006
7520462b 1007static size_t audio_get_avail (SWVoiceIn *sw)
1d14ffa9 1008{
7520462b 1009 size_t live;
1d14ffa9
FB
1010
1011 if (!sw) {
1012 return 0;
1013 }
1014
1015 live = sw->hw->total_samples_captured - sw->total_hw_samples_acquired;
dc88e38f
KZ
1016 if (audio_bug(__func__, live > sw->hw->conv_buf->size)) {
1017 dolog("live=%zu sw->hw->conv_buf->size=%zu\n", live,
1018 sw->hw->conv_buf->size);
12f4abf6 1019 return 0;
1d14ffa9
FB
1020 }
1021
1022 ldebug (
0724c579 1023 "%s: get_avail live %zu frontend frames %zu\n",
c0fe3827 1024 SW_NAME (sw),
0724c579 1025 live, audio_frontend_frames_in(sw, live)
1d14ffa9
FB
1026 );
1027
0724c579 1028 return live;
1d14ffa9
FB
1029}
1030
c4e59264
VR
1031/**
1032 * audio_frontend_frames_out() - returns the number of frames needed to
1033 * get frames_out frames after resampling
1034 *
1035 * @sw: audio playback frontend
1036 * @frames_out: number of frames
1037 */
1038static size_t audio_frontend_frames_out(SWVoiceOut *sw, size_t frames_out)
9833438e 1039{
c4e59264 1040 return ((int64_t)frames_out << 32) / sw->ratio;
9833438e
VR
1041}
1042
7520462b 1043static size_t audio_get_free(SWVoiceOut *sw)
1d14ffa9 1044{
7520462b 1045 size_t live, dead;
1d14ffa9
FB
1046
1047 if (!sw) {
1048 return 0;
1049 }
1050
1051 live = sw->total_hw_samples_mixed;
1052
dc88e38f
KZ
1053 if (audio_bug(__func__, live > sw->hw->mix_buf->size)) {
1054 dolog("live=%zu sw->hw->mix_buf->size=%zu\n", live,
1055 sw->hw->mix_buf->size);
12f4abf6 1056 return 0;
1d14ffa9
FB
1057 }
1058
dc88e38f 1059 dead = sw->hw->mix_buf->size - live;
1d14ffa9
FB
1060
1061#ifdef DEBUG_OUT
c4e59264
VR
1062 dolog("%s: get_free live %zu dead %zu frontend frames %zu\n",
1063 SW_NAME(sw), live, dead, audio_frontend_frames_out(sw, dead));
85571bc7 1064#endif
1d14ffa9 1065
9833438e 1066 return dead;
1d14ffa9
FB
1067}
1068
7520462b
KZ
1069static void audio_capture_mix_and_clear(HWVoiceOut *hw, size_t rpos,
1070 size_t samples)
8ead62cf 1071{
7520462b 1072 size_t n;
8ead62cf
FB
1073
1074 if (hw->enabled) {
ec36b695 1075 SWVoiceCap *sc;
8ead62cf 1076
ec36b695
FB
1077 for (sc = hw->cap_head.lh_first; sc; sc = sc->entries.le_next) {
1078 SWVoiceOut *sw = &sc->sw;
8ead62cf
FB
1079 int rpos2 = rpos;
1080
1081 n = samples;
1082 while (n) {
dc88e38f 1083 size_t till_end_of_hw = hw->mix_buf->size - rpos2;
7520462b 1084 size_t to_write = MIN(till_end_of_hw, n);
2b9cce8c 1085 size_t bytes = to_write * hw->info.bytes_per_frame;
7520462b 1086 size_t written;
8ead62cf 1087
dc88e38f 1088 sw->buf = hw->mix_buf->samples + rpos2;
8ead62cf
FB
1089 written = audio_pcm_sw_write (sw, NULL, bytes);
1090 if (written - bytes) {
7520462b
KZ
1091 dolog("Could not mix %zu bytes into a capture "
1092 "buffer, mixed %zu\n",
1093 bytes, written);
8ead62cf
FB
1094 break;
1095 }
1096 n -= to_write;
dc88e38f 1097 rpos2 = (rpos2 + to_write) % hw->mix_buf->size;
8ead62cf
FB
1098 }
1099 }
1100 }
1101
dc88e38f
KZ
1102 n = MIN(samples, hw->mix_buf->size - rpos);
1103 mixeng_clear(hw->mix_buf->samples + rpos, n);
1104 mixeng_clear(hw->mix_buf->samples, samples - n);
8ead62cf
FB
1105}
1106
ff095e52
KZ
1107static size_t audio_pcm_hw_run_out(HWVoiceOut *hw, size_t live)
1108{
1109 size_t clipped = 0;
1110
1111 while (live) {
aec6d0dc
VR
1112 size_t size = live * hw->info.bytes_per_frame;
1113 size_t decr, proc;
ff095e52 1114 void *buf = hw->pcm_ops->get_buffer_out(hw, &size);
4c3356f9
VR
1115
1116 if (size == 0) {
fb35c2ce 1117 break;
ff095e52
KZ
1118 }
1119
2b9cce8c 1120 decr = MIN(size / hw->info.bytes_per_frame, live);
4c3356f9
VR
1121 if (buf) {
1122 audio_pcm_hw_clip_out(hw, buf, decr);
1123 }
2b9cce8c
KZ
1124 proc = hw->pcm_ops->put_buffer_out(hw, buf,
1125 decr * hw->info.bytes_per_frame) /
1126 hw->info.bytes_per_frame;
ff095e52
KZ
1127
1128 live -= proc;
1129 clipped += proc;
dc88e38f 1130 hw->mix_buf->pos = (hw->mix_buf->pos + proc) % hw->mix_buf->size;
ff095e52
KZ
1131
1132 if (proc == 0 || proc < decr) {
1133 break;
1134 }
1135 }
1136
fdc8c5f4
VR
1137 if (hw->pcm_ops->run_buffer_out) {
1138 hw->pcm_ops->run_buffer_out(hw);
1139 }
1140
ff095e52
KZ
1141 return clipped;
1142}
1143
c0fe3827 1144static void audio_run_out (AudioState *s)
1d14ffa9
FB
1145{
1146 HWVoiceOut *hw = NULL;
1147 SWVoiceOut *sw;
1148
4d31ff32
VR
1149 while ((hw = audio_pcm_hw_find_any_enabled_out(s, hw))) {
1150 size_t played, live, prev_rpos;
1151 size_t hw_free = audio_pcm_hw_get_free(hw);
1152 int nb_live;
1153
1154 if (!audio_get_pdo_out(s->dev)->mixing_engine) {
1930616b
KZ
1155 /* there is exactly 1 sw for each hw with no mixeng */
1156 sw = hw->sw_head.lh_first;
1157
1158 if (hw->pending_disable) {
1159 hw->enabled = 0;
1160 hw->pending_disable = 0;
1161 if (hw->pcm_ops->enable_out) {
1162 hw->pcm_ops->enable_out(hw, false);
1163 }
1164 }
1165
1166 if (sw->active) {
7099a6a2
VR
1167 sw->callback.fn(sw->callback.opaque,
1168 hw_free * sw->info.bytes_per_frame);
1930616b 1169 }
1930616b 1170
dd052dbf
VR
1171 if (hw->pcm_ops->run_buffer_out) {
1172 hw->pcm_ops->run_buffer_out(hw);
1173 }
1174
4d31ff32
VR
1175 continue;
1176 }
1d14ffa9 1177
a806f959
VR
1178 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
1179 if (sw->active) {
9833438e
VR
1180 size_t sw_free = audio_get_free(sw);
1181 size_t free;
1182
1183 if (hw_free > sw->total_hw_samples_mixed) {
c4e59264 1184 free = audio_frontend_frames_out(sw,
9833438e
VR
1185 MIN(sw_free, hw_free - sw->total_hw_samples_mixed));
1186 } else {
1187 free = 0;
1188 }
a806f959 1189 if (free > 0) {
c4e59264
VR
1190 sw->callback.fn(sw->callback.opaque,
1191 free * sw->info.bytes_per_frame);
a806f959
VR
1192 }
1193 }
1194 }
1195
bdff253c 1196 live = audio_pcm_hw_get_live_out (hw, &nb_live);
1d14ffa9
FB
1197 if (!nb_live) {
1198 live = 0;
1199 }
c0fe3827 1200
dc88e38f
KZ
1201 if (audio_bug(__func__, live > hw->mix_buf->size)) {
1202 dolog("live=%zu hw->mix_buf->size=%zu\n", live, hw->mix_buf->size);
12f4abf6 1203 continue;
1d14ffa9
FB
1204 }
1205
1206 if (hw->pending_disable && !nb_live) {
ec36b695 1207 SWVoiceCap *sc;
1d14ffa9
FB
1208#ifdef DEBUG_OUT
1209 dolog ("Disabling voice\n");
85571bc7 1210#endif
1d14ffa9
FB
1211 hw->enabled = 0;
1212 hw->pending_disable = 0;
571a8c52
KZ
1213 if (hw->pcm_ops->enable_out) {
1214 hw->pcm_ops->enable_out(hw, false);
1215 }
ec36b695
FB
1216 for (sc = hw->cap_head.lh_first; sc; sc = sc->entries.le_next) {
1217 sc->sw.active = 0;
1218 audio_recalc_and_notify_capture (sc->cap);
8ead62cf 1219 }
1d14ffa9
FB
1220 continue;
1221 }
1222
1223 if (!live) {
a8a98cfd
VR
1224 if (hw->pcm_ops->run_buffer_out) {
1225 hw->pcm_ops->run_buffer_out(hw);
1226 }
1d14ffa9
FB
1227 continue;
1228 }
1229
dc88e38f 1230 prev_rpos = hw->mix_buf->pos;
3f5bbfc2 1231 played = audio_pcm_hw_run_out(hw, live);
3d4d16f4 1232 replay_audio_out(&played);
dc88e38f
KZ
1233 if (audio_bug(__func__, hw->mix_buf->pos >= hw->mix_buf->size)) {
1234 dolog("hw->mix_buf->pos=%zu hw->mix_buf->size=%zu played=%zu\n",
1235 hw->mix_buf->pos, hw->mix_buf->size, played);
12f4abf6 1236 hw->mix_buf->pos = 0;
1d14ffa9
FB
1237 }
1238
1239#ifdef DEBUG_OUT
7520462b 1240 dolog("played=%zu\n", played);
85571bc7 1241#endif
1d14ffa9
FB
1242
1243 if (played) {
1244 hw->ts_helper += played;
8ead62cf 1245 audio_capture_mix_and_clear (hw, prev_rpos, played);
1d14ffa9
FB
1246 }
1247
1248 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
1d14ffa9
FB
1249 if (!sw->active && sw->empty) {
1250 continue;
1251 }
1252
470bcabd 1253 if (audio_bug(__func__, played > sw->total_hw_samples_mixed)) {
7520462b
KZ
1254 dolog("played=%zu sw->total_hw_samples_mixed=%zu\n",
1255 played, sw->total_hw_samples_mixed);
12f4abf6 1256 played = sw->total_hw_samples_mixed;
1d14ffa9
FB
1257 }
1258
1259 sw->total_hw_samples_mixed -= played;
1260
1261 if (!sw->total_hw_samples_mixed) {
1262 sw->empty = 1;
1d14ffa9 1263 }
1d14ffa9
FB
1264 }
1265 }
1266}
1267
ff095e52
KZ
1268static size_t audio_pcm_hw_run_in(HWVoiceIn *hw, size_t samples)
1269{
1270 size_t conv = 0;
1271
a2893c83
VR
1272 if (hw->pcm_ops->run_buffer_in) {
1273 hw->pcm_ops->run_buffer_in(hw);
1274 }
1275
ff095e52
KZ
1276 while (samples) {
1277 size_t proc;
2b9cce8c 1278 size_t size = samples * hw->info.bytes_per_frame;
ff095e52
KZ
1279 void *buf = hw->pcm_ops->get_buffer_in(hw, &size);
1280
2b9cce8c 1281 assert(size % hw->info.bytes_per_frame == 0);
ff095e52 1282 if (size == 0) {
ff095e52
KZ
1283 break;
1284 }
1285
251f1549 1286 proc = audio_pcm_hw_conv_in(hw, buf, size / hw->info.bytes_per_frame);
ff095e52
KZ
1287
1288 samples -= proc;
1289 conv += proc;
2b9cce8c 1290 hw->pcm_ops->put_buffer_in(hw, buf, proc * hw->info.bytes_per_frame);
ff095e52
KZ
1291 }
1292
1293 return conv;
1294}
1295
c0fe3827 1296static void audio_run_in (AudioState *s)
1d14ffa9
FB
1297{
1298 HWVoiceIn *hw = NULL;
1299
1930616b
KZ
1300 if (!audio_get_pdo_in(s->dev)->mixing_engine) {
1301 while ((hw = audio_pcm_hw_find_any_enabled_in(s, hw))) {
1302 /* there is exactly 1 sw for each hw with no mixeng */
1303 SWVoiceIn *sw = hw->sw_head.lh_first;
1304 if (sw->active) {
1305 sw->callback.fn(sw->callback.opaque, INT_MAX);
1306 }
1307 }
1308 return;
1309 }
1310
526fb058 1311 while ((hw = audio_pcm_hw_find_any_enabled_in(s, hw))) {
1d14ffa9 1312 SWVoiceIn *sw;
7520462b 1313 size_t captured = 0, min;
1d14ffa9 1314
3d4d16f4 1315 if (replay_mode != REPLAY_MODE_PLAY) {
3f5bbfc2 1316 captured = audio_pcm_hw_run_in(
dc88e38f 1317 hw, hw->conv_buf->size - audio_pcm_hw_get_live_in(hw));
3d4d16f4 1318 }
dc88e38f
KZ
1319 replay_audio_in(&captured, hw->conv_buf->samples, &hw->conv_buf->pos,
1320 hw->conv_buf->size);
1d14ffa9
FB
1321
1322 min = audio_pcm_hw_find_min_in (hw);
1323 hw->total_samples_captured += captured - min;
1324 hw->ts_helper += captured;
1325
1326 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
1327 sw->total_hw_samples_acquired -= min;
1328
1329 if (sw->active) {
0724c579 1330 size_t sw_avail = audio_get_avail(sw);
7520462b 1331 size_t avail;
1d14ffa9 1332
0724c579 1333 avail = audio_frontend_frames_in(sw, sw_avail);
1d14ffa9 1334 if (avail > 0) {
0724c579
VR
1335 sw->callback.fn(sw->callback.opaque,
1336 avail * sw->info.bytes_per_frame);
1d14ffa9
FB
1337 }
1338 }
1339 }
1340 }
1341}
1342
8ead62cf
FB
1343static void audio_run_capture (AudioState *s)
1344{
1345 CaptureVoiceOut *cap;
1346
1347 for (cap = s->cap_head.lh_first; cap; cap = cap->entries.le_next) {
7520462b 1348 size_t live, rpos, captured;
8ead62cf
FB
1349 HWVoiceOut *hw = &cap->hw;
1350 SWVoiceOut *sw;
1351
bdff253c 1352 captured = live = audio_pcm_hw_get_live_out (hw, NULL);
dc88e38f 1353 rpos = hw->mix_buf->pos;
8ead62cf 1354 while (live) {
dc88e38f 1355 size_t left = hw->mix_buf->size - rpos;
7520462b 1356 size_t to_capture = MIN(live, left);
1ea879e5 1357 struct st_sample *src;
8ead62cf
FB
1358 struct capture_callback *cb;
1359
dc88e38f 1360 src = hw->mix_buf->samples + rpos;
8ead62cf
FB
1361 hw->clip (cap->buf, src, to_capture);
1362 mixeng_clear (src, to_capture);
1363
1364 for (cb = cap->cb_head.lh_first; cb; cb = cb->entries.le_next) {
1365 cb->ops.capture (cb->opaque, cap->buf,
2b9cce8c 1366 to_capture * hw->info.bytes_per_frame);
8ead62cf 1367 }
dc88e38f 1368 rpos = (rpos + to_capture) % hw->mix_buf->size;
8ead62cf
FB
1369 live -= to_capture;
1370 }
dc88e38f 1371 hw->mix_buf->pos = rpos;
8ead62cf
FB
1372
1373 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
1374 if (!sw->active && sw->empty) {
1375 continue;
1376 }
1377
470bcabd 1378 if (audio_bug(__func__, captured > sw->total_hw_samples_mixed)) {
7520462b
KZ
1379 dolog("captured=%zu sw->total_hw_samples_mixed=%zu\n",
1380 captured, sw->total_hw_samples_mixed);
12f4abf6 1381 captured = sw->total_hw_samples_mixed;
8ead62cf
FB
1382 }
1383
1384 sw->total_hw_samples_mixed -= captured;
1385 sw->empty = sw->total_hw_samples_mixed == 0;
1386 }
1387 }
1388}
1389
18e2c177 1390void audio_run(AudioState *s, const char *msg)
571ec3d6 1391{
18e2c177
KZ
1392 audio_run_out(s);
1393 audio_run_in(s);
1394 audio_run_capture(s);
571ec3d6 1395
713a98f8 1396#ifdef DEBUG_POLL
1397 {
1398 static double prevtime;
1399 double currtime;
1400 struct timeval tv;
571ec3d6 1401
713a98f8 1402 if (gettimeofday (&tv, NULL)) {
1403 perror ("audio_run: gettimeofday");
1404 return;
1405 }
1406
1407 currtime = tv.tv_sec + tv.tv_usec * 1e-6;
1408 dolog ("Elapsed since last %s: %f\n", msg, currtime - prevtime);
1409 prevtime = currtime;
1410 }
1411#endif
571ec3d6
FB
1412}
1413
a2893c83 1414void audio_generic_run_buffer_in(HWVoiceIn *hw)
ff095e52 1415{
ff095e52 1416 if (unlikely(!hw->buf_emul)) {
1d8549ad
VR
1417 hw->size_emul = hw->samples * hw->info.bytes_per_frame;
1418 hw->buf_emul = g_malloc(hw->size_emul);
ff095e52
KZ
1419 hw->pos_emul = hw->pending_emul = 0;
1420 }
1421
1422 while (hw->pending_emul < hw->size_emul) {
1423 size_t read_len = MIN(hw->size_emul - hw->pos_emul,
1424 hw->size_emul - hw->pending_emul);
1425 size_t read = hw->pcm_ops->read(hw, hw->buf_emul + hw->pos_emul,
1426 read_len);
1427 hw->pending_emul += read;
7ffc90f3 1428 hw->pos_emul = (hw->pos_emul + read) % hw->size_emul;
ff095e52
KZ
1429 if (read < read_len) {
1430 break;
1431 }
1432 }
a2893c83
VR
1433}
1434
1435void *audio_generic_get_buffer_in(HWVoiceIn *hw, size_t *size)
1436{
18404ff1 1437 size_t start;
ff095e52 1438
18404ff1
VR
1439 start = audio_ring_posb(hw->pos_emul, hw->pending_emul, hw->size_emul);
1440 assert(start < hw->size_emul);
ff095e52 1441
599eac4e
VR
1442 *size = MIN(*size, hw->pending_emul);
1443 *size = MIN(*size, hw->size_emul - start);
ff095e52
KZ
1444 return hw->buf_emul + start;
1445}
1446
1447void audio_generic_put_buffer_in(HWVoiceIn *hw, void *buf, size_t size)
1448{
1449 assert(size <= hw->pending_emul);
1450 hw->pending_emul -= size;
1451}
1452
9833438e
VR
1453size_t audio_generic_buffer_get_free(HWVoiceOut *hw)
1454{
1455 if (hw->buf_emul) {
1456 return hw->size_emul - hw->pending_emul;
1457 } else {
1458 return hw->samples * hw->info.bytes_per_frame;
1459 }
1460}
1461
fdc8c5f4
VR
1462void audio_generic_run_buffer_out(HWVoiceOut *hw)
1463{
1464 while (hw->pending_emul) {
18404ff1 1465 size_t write_len, written, start;
fdc8c5f4 1466
18404ff1
VR
1467 start = audio_ring_posb(hw->pos_emul, hw->pending_emul, hw->size_emul);
1468 assert(start < hw->size_emul);
fdc8c5f4
VR
1469
1470 write_len = MIN(hw->pending_emul, hw->size_emul - start);
1471
1472 written = hw->pcm_ops->write(hw, hw->buf_emul + start, write_len);
1473 hw->pending_emul -= written;
1474
1475 if (written < write_len) {
1476 break;
1477 }
1478 }
1479}
1480
ff095e52
KZ
1481void *audio_generic_get_buffer_out(HWVoiceOut *hw, size_t *size)
1482{
1483 if (unlikely(!hw->buf_emul)) {
1d8549ad
VR
1484 hw->size_emul = hw->samples * hw->info.bytes_per_frame;
1485 hw->buf_emul = g_malloc(hw->size_emul);
ff095e52
KZ
1486 hw->pos_emul = hw->pending_emul = 0;
1487 }
1488
1489 *size = MIN(hw->size_emul - hw->pending_emul,
1490 hw->size_emul - hw->pos_emul);
1491 return hw->buf_emul + hw->pos_emul;
1492}
1493
fdc8c5f4 1494size_t audio_generic_put_buffer_out(HWVoiceOut *hw, void *buf, size_t size)
ff095e52
KZ
1495{
1496 assert(buf == hw->buf_emul + hw->pos_emul &&
1497 size + hw->pending_emul <= hw->size_emul);
1498
1499 hw->pending_emul += size;
1500 hw->pos_emul = (hw->pos_emul + size) % hw->size_emul;
1501
1502 return size;
1503}
1504
ff095e52
KZ
1505size_t audio_generic_write(HWVoiceOut *hw, void *buf, size_t size)
1506{
2d882307
VR
1507 size_t total = 0;
1508
9833438e
VR
1509 if (hw->pcm_ops->buffer_get_free) {
1510 size_t free = hw->pcm_ops->buffer_get_free(hw);
1511
1512 size = MIN(size, free);
1513 }
1514
2d882307
VR
1515 while (total < size) {
1516 size_t dst_size = size - total;
1517 size_t copy_size, proc;
1518 void *dst = hw->pcm_ops->get_buffer_out(hw, &dst_size);
ff095e52 1519
2d882307
VR
1520 if (dst_size == 0) {
1521 break;
1522 }
1523
1524 copy_size = MIN(size - total, dst_size);
1525 if (dst) {
1526 memcpy(dst, (char *)buf + total, copy_size);
1527 }
1528 proc = hw->pcm_ops->put_buffer_out(hw, dst, copy_size);
1529 total += proc;
1530
1531 if (proc == 0 || proc < copy_size) {
1532 break;
1533 }
1534 }
1535
2d882307 1536 return total;
ff095e52
KZ
1537}
1538
1539size_t audio_generic_read(HWVoiceIn *hw, void *buf, size_t size)
1540{
b9896dc5 1541 size_t total = 0;
ff095e52 1542
a2893c83
VR
1543 if (hw->pcm_ops->run_buffer_in) {
1544 hw->pcm_ops->run_buffer_in(hw);
1545 }
1546
b9896dc5
VR
1547 while (total < size) {
1548 size_t src_size = size - total;
1549 void *src = hw->pcm_ops->get_buffer_in(hw, &src_size);
ff095e52 1550
b9896dc5 1551 if (src_size == 0) {
b9896dc5
VR
1552 break;
1553 }
1554
1555 memcpy((char *)buf + total, src, src_size);
1556 hw->pcm_ops->put_buffer_in(hw, src, src_size);
1557 total += src_size;
1558 }
1559
1560 return total;
8d1439b6 1561}
ff095e52 1562
71830221
KZ
1563static int audio_driver_init(AudioState *s, struct audio_driver *drv,
1564 bool msg, Audiodev *dev)
85571bc7 1565{
71830221 1566 s->drv_opaque = drv->init(dev);
1d14ffa9 1567
c0fe3827 1568 if (s->drv_opaque) {
ff095e52
KZ
1569 if (!drv->pcm_ops->get_buffer_in) {
1570 drv->pcm_ops->get_buffer_in = audio_generic_get_buffer_in;
1571 drv->pcm_ops->put_buffer_in = audio_generic_put_buffer_in;
1572 }
1573 if (!drv->pcm_ops->get_buffer_out) {
1574 drv->pcm_ops->get_buffer_out = audio_generic_get_buffer_out;
1575 drv->pcm_ops->put_buffer_out = audio_generic_put_buffer_out;
1576 }
1577
526fb058
KZ
1578 audio_init_nb_voices_out(s, drv);
1579 audio_init_nb_voices_in(s, drv);
c0fe3827 1580 s->drv = drv;
1d14ffa9 1581 return 0;
6c6886bd 1582 } else {
fb37ce92
GH
1583 if (msg) {
1584 dolog("Could not init `%s' audio driver\n", drv->name);
1585 }
1d14ffa9 1586 return -1;
85571bc7
FB
1587 }
1588}
1589
538f0497 1590static void audio_vm_change_state_handler (void *opaque, bool running,
1dfb4dd9 1591 RunState state)
85571bc7 1592{
c0fe3827 1593 AudioState *s = opaque;
1d14ffa9
FB
1594 HWVoiceOut *hwo = NULL;
1595 HWVoiceIn *hwi = NULL;
1d14ffa9 1596
978dd635 1597 s->vm_running = running;
526fb058 1598 while ((hwo = audio_pcm_hw_find_any_enabled_out(s, hwo))) {
571a8c52
KZ
1599 if (hwo->pcm_ops->enable_out) {
1600 hwo->pcm_ops->enable_out(hwo, running);
1601 }
1d14ffa9 1602 }
85571bc7 1603
526fb058 1604 while ((hwi = audio_pcm_hw_find_any_enabled_in(s, hwi))) {
571a8c52
KZ
1605 if (hwi->pcm_ops->enable_in) {
1606 hwi->pcm_ops->enable_in(hwi, running);
1607 }
85571bc7 1608 }
39deb1e4 1609 audio_reset_timer (s);
85571bc7
FB
1610}
1611
ecd97e95 1612static void free_audio_state(AudioState *s)
85571bc7 1613{
a384c205
MAL
1614 HWVoiceOut *hwo, *hwon;
1615 HWVoiceIn *hwi, *hwin;
1d14ffa9 1616
526fb058 1617 QLIST_FOREACH_SAFE(hwo, &s->hw_head_out, entries, hwon) {
ec36b695 1618 SWVoiceCap *sc;
8ead62cf 1619
571a8c52
KZ
1620 if (hwo->enabled && hwo->pcm_ops->enable_out) {
1621 hwo->pcm_ops->enable_out(hwo, false);
aeb29b64 1622 }
1d14ffa9 1623 hwo->pcm_ops->fini_out (hwo);
8ead62cf 1624
ec36b695
FB
1625 for (sc = hwo->cap_head.lh_first; sc; sc = sc->entries.le_next) {
1626 CaptureVoiceOut *cap = sc->cap;
1627 struct capture_callback *cb;
1628
1629 for (cb = cap->cb_head.lh_first; cb; cb = cb->entries.le_next) {
1630 cb->ops.destroy (cb->opaque);
1631 }
8ead62cf 1632 }
a384c205 1633 QLIST_REMOVE(hwo, entries);
1d14ffa9 1634 }
85571bc7 1635
526fb058 1636 QLIST_FOREACH_SAFE(hwi, &s->hw_head_in, entries, hwin) {
571a8c52
KZ
1637 if (hwi->enabled && hwi->pcm_ops->enable_in) {
1638 hwi->pcm_ops->enable_in(hwi, false);
aeb29b64 1639 }
1d14ffa9 1640 hwi->pcm_ops->fini_in (hwi);
a384c205 1641 QLIST_REMOVE(hwi, entries);
85571bc7 1642 }
c0fe3827
FB
1643
1644 if (s->drv) {
1645 s->drv->fini (s->drv_opaque);
a384c205 1646 s->drv = NULL;
c0fe3827 1647 }
71830221
KZ
1648
1649 if (s->dev) {
1650 qapi_free_Audiodev(s->dev);
1651 s->dev = NULL;
1652 }
e76ba19a
KZ
1653
1654 if (s->ts) {
1655 timer_free(s->ts);
1656 s->ts = NULL;
1657 }
1658
ecd97e95
KZ
1659 g_free(s);
1660}
1661
1662void audio_cleanup(void)
1663{
ecd97e95
KZ
1664 while (!QTAILQ_EMPTY(&audio_states)) {
1665 AudioState *s = QTAILQ_FIRST(&audio_states);
1666 QTAILQ_REMOVE(&audio_states, s, list);
1667 free_audio_state(s);
1668 }
85571bc7
FB
1669}
1670
da77adba
DDAG
1671static bool vmstate_audio_needed(void *opaque)
1672{
1673 /*
1674 * Never needed, this vmstate only exists in case
1675 * an old qemu sends it to us.
1676 */
1677 return false;
1678}
1679
d959fce9
JQ
1680static const VMStateDescription vmstate_audio = {
1681 .name = "audio",
1682 .version_id = 1,
1683 .minimum_version_id = 1,
da77adba 1684 .needed = vmstate_audio_needed,
35d08458 1685 .fields = (VMStateField[]) {
d959fce9 1686 VMSTATE_END_OF_LIST()
1d14ffa9 1687 }
d959fce9 1688};
85571bc7 1689
71830221
KZ
1690static void audio_validate_opts(Audiodev *dev, Error **errp);
1691
1692static AudiodevListEntry *audiodev_find(
1693 AudiodevListHead *head, const char *drvname)
1694{
1695 AudiodevListEntry *e;
1696 QSIMPLEQ_FOREACH(e, head, next) {
1697 if (strcmp(AudiodevDriver_str(e->dev->driver), drvname) == 0) {
1698 return e;
1699 }
1700 }
1701
1702 return NULL;
1703}
1704
ecd97e95
KZ
1705/*
1706 * if we have dev, this function was called because of an -audiodev argument =>
1707 * initialize a new state with it
1708 * if dev == NULL => legacy implicit initialization, return the already created
1709 * state or create a new one
1710 */
af2041ed 1711static AudioState *audio_init(Audiodev *dev, const char *name)
85571bc7 1712{
ecd97e95 1713 static bool atexit_registered;
1d14ffa9 1714 size_t i;
85571bc7 1715 int done = 0;
71830221 1716 const char *drvname = NULL;
713a98f8 1717 VMChangeStateEntry *e;
ecd97e95 1718 AudioState *s;
d3893a39 1719 struct audio_driver *driver;
71830221
KZ
1720 /* silence gcc warning about uninitialized variable */
1721 AudiodevListHead head = QSIMPLEQ_HEAD_INITIALIZER(head);
1d14ffa9 1722
f0c4555e
GH
1723 if (using_spice) {
1724 /*
1725 * When using spice allow the spice audio driver being picked
1726 * as default.
1727 *
1728 * Temporary hack. Using audio devices without explicit
1729 * audiodev= property is already deprecated. Same goes for
1730 * the -soundhw switch. Once this support gets finally
1731 * removed we can also drop the concept of a default audio
1732 * backend and this can go away.
1733 */
1734 driver = audio_driver_lookup("spice");
06c8c375
GH
1735 if (driver) {
1736 driver->can_be_default = 1;
1737 }
f0c4555e
GH
1738 }
1739
71830221
KZ
1740 if (dev) {
1741 /* -audiodev option */
af2041ed 1742 legacy_config = false;
71830221 1743 drvname = AudiodevDriver_str(dev->driver);
ecd97e95 1744 } else if (!QTAILQ_EMPTY(&audio_states)) {
af2041ed 1745 if (!legacy_config) {
4b3b7793
KZ
1746 dolog("Device %s: audiodev default parameter is deprecated, please "
1747 "specify audiodev=%s\n", name,
1748 QTAILQ_FIRST(&audio_states)->dev->id);
af2041ed 1749 }
ecd97e95 1750 return QTAILQ_FIRST(&audio_states);
71830221
KZ
1751 } else {
1752 /* legacy implicit initialization */
1753 head = audio_handle_legacy_opts();
1754 /*
1755 * In case of legacy initialization, all Audiodevs in the list will have
e3a6e0da 1756 * the same configuration (except the driver), so it doesn't matter which
71830221
KZ
1757 * one we chose. We need an Audiodev to set up AudioState before we can
1758 * init a driver. Also note that dev at this point is still in the
1759 * list.
1760 */
1761 dev = QSIMPLEQ_FIRST(&head)->dev;
1762 audio_validate_opts(dev, &error_abort);
1763 }
ecd97e95 1764
b21e2380 1765 s = g_new0(AudioState, 1);
71830221
KZ
1766 s->dev = dev;
1767
72cf2d4f
BS
1768 QLIST_INIT (&s->hw_head_out);
1769 QLIST_INIT (&s->hw_head_in);
1770 QLIST_INIT (&s->cap_head);
ecd97e95
KZ
1771 if (!atexit_registered) {
1772 atexit(audio_cleanup);
1773 atexit_registered = true;
1774 }
571ec3d6 1775
bc72ad67 1776 s->ts = timer_new_ns(QEMU_CLOCK_VIRTUAL, audio_timer, s);
571ec3d6 1777
71830221
KZ
1778 s->nb_hw_voices_out = audio_get_pdo_out(dev)->voices;
1779 s->nb_hw_voices_in = audio_get_pdo_in(dev)->voices;
c0fe3827 1780
61ddafbc 1781 if (s->nb_hw_voices_out < 1) {
571ec3d6 1782 dolog ("Bogus number of playback voices %d, setting to 1\n",
1d14ffa9
FB
1783 s->nb_hw_voices_out);
1784 s->nb_hw_voices_out = 1;
1785 }
1786
a7b7802b 1787 if (s->nb_hw_voices_in < 0) {
571ec3d6 1788 dolog ("Bogus number of capture voices %d, setting to 0\n",
1d14ffa9 1789 s->nb_hw_voices_in);
571ec3d6 1790 s->nb_hw_voices_in = 0;
1d14ffa9 1791 }
85571bc7 1792
85571bc7 1793 if (drvname) {
d3893a39
GH
1794 driver = audio_driver_lookup(drvname);
1795 if (driver) {
71830221 1796 done = !audio_driver_init(s, driver, true, dev);
d3893a39 1797 } else {
85571bc7
FB
1798 dolog ("Unknown audio driver `%s'\n", drvname);
1799 }
0f957c53
MAL
1800 if (!done) {
1801 free_audio_state(s);
1802 return NULL;
1803 }
71830221
KZ
1804 } else {
1805 for (i = 0; audio_prio_list[i]; i++) {
1806 AudiodevListEntry *e = audiodev_find(&head, audio_prio_list[i]);
d3893a39 1807 driver = audio_driver_lookup(audio_prio_list[i]);
71830221
KZ
1808
1809 if (e && driver) {
1810 s->dev = dev = e->dev;
1811 audio_validate_opts(dev, &error_abort);
1812 done = !audio_driver_init(s, driver, false, dev);
1813 if (done) {
1814 e->dev = NULL;
1815 break;
1816 }
1d14ffa9 1817 }
85571bc7
FB
1818 }
1819 }
71830221 1820 audio_free_audiodev_list(&head);
85571bc7 1821
85571bc7 1822 if (!done) {
d3893a39 1823 driver = audio_driver_lookup("none");
71830221 1824 done = !audio_driver_init(s, driver, false, dev);
7e274652
MA
1825 assert(done);
1826 dolog("warning: Using timer based audio emulation\n");
85571bc7 1827 }
1d14ffa9 1828
71830221
KZ
1829 if (dev->timer_period <= 0) {
1830 s->period_ticks = 1;
0d9acba8 1831 } else {
40ad46d3 1832 s->period_ticks = dev->timer_period * (int64_t)SCALE_US;
1d14ffa9 1833 }
0d9acba8
PB
1834
1835 e = qemu_add_vm_change_state_handler (audio_vm_change_state_handler, s);
1836 if (!e) {
1837 dolog ("warning: Could not register change state handler\n"
1838 "(Audio can continue looping even after stopping the VM)\n");
1d14ffa9
FB
1839 }
1840
0f957c53 1841 QTAILQ_INSERT_TAIL(&audio_states, s, list);
72cf2d4f 1842 QLIST_INIT (&s->card_head);
0be71e32 1843 vmstate_register (NULL, 0, &vmstate_audio, s);
ecd97e95 1844 return s;
71830221
KZ
1845}
1846
1847void audio_free_audiodev_list(AudiodevListHead *head)
1848{
1849 AudiodevListEntry *e;
1850 while ((e = QSIMPLEQ_FIRST(head))) {
1851 QSIMPLEQ_REMOVE_HEAD(head, next);
1852 qapi_free_Audiodev(e->dev);
1853 g_free(e);
1854 }
85571bc7 1855}
8ead62cf 1856
1a7dafce 1857void AUD_register_card (const char *name, QEMUSoundCard *card)
1858{
ecd97e95 1859 if (!card->state) {
af2041ed 1860 card->state = audio_init(NULL, name);
ecd97e95
KZ
1861 }
1862
7267c094 1863 card->name = g_strdup (name);
1a7dafce 1864 memset (&card->entries, 0, sizeof (card->entries));
ecd97e95 1865 QLIST_INSERT_HEAD(&card->state->card_head, card, entries);
1a7dafce 1866}
1867
1868void AUD_remove_card (QEMUSoundCard *card)
1869{
72cf2d4f 1870 QLIST_REMOVE (card, entries);
7267c094 1871 g_free (card->name);
1a7dafce 1872}
1873
33940dd3 1874static struct audio_pcm_ops capture_pcm_ops;
1a7dafce 1875
ecd97e95
KZ
1876CaptureVoiceOut *AUD_add_capture(
1877 AudioState *s,
1ea879e5 1878 struct audsettings *as,
8ead62cf
FB
1879 struct audio_capture_ops *ops,
1880 void *cb_opaque
1881 )
1882{
1883 CaptureVoiceOut *cap;
1884 struct capture_callback *cb;
1885
ecd97e95 1886 if (!s) {
af2041ed 1887 if (!legacy_config) {
4b3b7793 1888 dolog("Capturing without setting an audiodev is deprecated\n");
af2041ed
KZ
1889 }
1890 s = audio_init(NULL, NULL);
ecd97e95
KZ
1891 }
1892
1930616b
KZ
1893 if (!audio_get_pdo_out(s->dev)->mixing_engine) {
1894 dolog("Can't capture with mixeng disabled\n");
1895 return NULL;
1896 }
1897
ec36b695 1898 if (audio_validate_settings (as)) {
8ead62cf
FB
1899 dolog ("Invalid settings were passed when trying to add capture\n");
1900 audio_print_settings (as);
e8d85444 1901 return NULL;
8ead62cf
FB
1902 }
1903
e8d85444 1904 cb = g_malloc0(sizeof(*cb));
8ead62cf
FB
1905 cb->ops = *ops;
1906 cb->opaque = cb_opaque;
1907
526fb058 1908 cap = audio_pcm_capture_find_specific(s, as);
8ead62cf 1909 if (cap) {
72cf2d4f 1910 QLIST_INSERT_HEAD (&cap->cb_head, cb, entries);
ec36b695 1911 return cap;
6c6886bd 1912 } else {
8ead62cf
FB
1913 HWVoiceOut *hw;
1914 CaptureVoiceOut *cap;
1915
e8d85444 1916 cap = g_malloc0(sizeof(*cap));
8ead62cf
FB
1917
1918 hw = &cap->hw;
526fb058 1919 hw->s = s;
33940dd3 1920 hw->pcm_ops = &capture_pcm_ops;
72cf2d4f
BS
1921 QLIST_INIT (&hw->sw_head);
1922 QLIST_INIT (&cap->cb_head);
8ead62cf
FB
1923
1924 /* XXX find a more elegant way */
1925 hw->samples = 4096 * 4;
dc88e38f 1926 audio_pcm_hw_alloc_resources_out(hw);
8ead62cf 1927
d929eba5 1928 audio_pcm_init_info (&hw->info, as);
8ead62cf 1929
2b9cce8c 1930 cap->buf = g_malloc0_n(hw->mix_buf->size, hw->info.bytes_per_frame);
8ead62cf 1931
ed2a4a79
KZ
1932 if (hw->info.is_float) {
1933 hw->clip = mixeng_clip_float[hw->info.nchannels == 2];
1934 } else {
1935 hw->clip = mixeng_clip
1936 [hw->info.nchannels == 2]
1937 [hw->info.is_signed]
1938 [hw->info.swap_endianness]
1939 [audio_bits_to_index(hw->info.bits)];
1940 }
8ead62cf 1941
72cf2d4f
BS
1942 QLIST_INSERT_HEAD (&s->cap_head, cap, entries);
1943 QLIST_INSERT_HEAD (&cap->cb_head, cb, entries);
8ead62cf 1944
526fb058 1945 QLIST_FOREACH(hw, &s->hw_head_out, entries) {
1a7dafce 1946 audio_attach_capture (hw);
8ead62cf 1947 }
ec36b695 1948 return cap;
ec36b695
FB
1949 }
1950}
1951
1952void AUD_del_capture (CaptureVoiceOut *cap, void *cb_opaque)
1953{
1954 struct capture_callback *cb;
1955
1956 for (cb = cap->cb_head.lh_first; cb; cb = cb->entries.le_next) {
1957 if (cb->opaque == cb_opaque) {
1958 cb->ops.destroy (cb_opaque);
72cf2d4f 1959 QLIST_REMOVE (cb, entries);
7267c094 1960 g_free (cb);
ec36b695
FB
1961
1962 if (!cap->cb_head.lh_first) {
1963 SWVoiceOut *sw = cap->hw.sw_head.lh_first, *sw1;
a3c25997 1964
ec36b695 1965 while (sw) {
a3c25997 1966 SWVoiceCap *sc = (SWVoiceCap *) sw;
ec36b695
FB
1967#ifdef DEBUG_CAPTURE
1968 dolog ("freeing %s\n", sw->name);
1969#endif
a3c25997 1970
ec36b695
FB
1971 sw1 = sw->entries.le_next;
1972 if (sw->rate) {
1973 st_rate_stop (sw->rate);
1974 sw->rate = NULL;
1975 }
72cf2d4f
BS
1976 QLIST_REMOVE (sw, entries);
1977 QLIST_REMOVE (sc, entries);
7267c094 1978 g_free (sc);
ec36b695
FB
1979 sw = sw1;
1980 }
72cf2d4f 1981 QLIST_REMOVE (cap, entries);
3268a845
GH
1982 g_free (cap->hw.mix_buf);
1983 g_free (cap->buf);
7267c094 1984 g_free (cap);
ec36b695
FB
1985 }
1986 return;
1987 }
8ead62cf
FB
1988 }
1989}
683efdcb
AZ
1990
1991void AUD_set_volume_out (SWVoiceOut *sw, int mute, uint8_t lvol, uint8_t rvol)
cecc1e79
KZ
1992{
1993 Volume vol = { .mute = mute, .channels = 2, .vol = { lvol, rvol } };
1994 audio_set_volume_out(sw, &vol);
1995}
1996
1997void audio_set_volume_out(SWVoiceOut *sw, Volume *vol)
683efdcb
AZ
1998{
1999 if (sw) {
6c95ab94
MAL
2000 HWVoiceOut *hw = sw->hw;
2001
cecc1e79
KZ
2002 sw->vol.mute = vol->mute;
2003 sw->vol.l = nominal_volume.l * vol->vol[0] / 255;
2004 sw->vol.r = nominal_volume.l * vol->vol[vol->channels > 1 ? 1 : 0] /
2005 255;
6c95ab94 2006
571a8c52 2007 if (hw->pcm_ops->volume_out) {
cecc1e79 2008 hw->pcm_ops->volume_out(hw, vol);
6c95ab94 2009 }
683efdcb
AZ
2010 }
2011}
2012
2013void AUD_set_volume_in (SWVoiceIn *sw, int mute, uint8_t lvol, uint8_t rvol)
cecc1e79
KZ
2014{
2015 Volume vol = { .mute = mute, .channels = 2, .vol = { lvol, rvol } };
2016 audio_set_volume_in(sw, &vol);
2017}
2018
2019void audio_set_volume_in(SWVoiceIn *sw, Volume *vol)
683efdcb
AZ
2020{
2021 if (sw) {
6c95ab94
MAL
2022 HWVoiceIn *hw = sw->hw;
2023
cecc1e79
KZ
2024 sw->vol.mute = vol->mute;
2025 sw->vol.l = nominal_volume.l * vol->vol[0] / 255;
2026 sw->vol.r = nominal_volume.r * vol->vol[vol->channels > 1 ? 1 : 0] /
2027 255;
6c95ab94 2028
571a8c52 2029 if (hw->pcm_ops->volume_in) {
cecc1e79 2030 hw->pcm_ops->volume_in(hw, vol);
6c95ab94 2031 }
683efdcb
AZ
2032 }
2033}
71830221
KZ
2034
2035void audio_create_pdos(Audiodev *dev)
2036{
2037 switch (dev->driver) {
2038#define CASE(DRIVER, driver, pdo_name) \
2039 case AUDIODEV_DRIVER_##DRIVER: \
ceb19c8f 2040 if (!dev->u.driver.in) { \
71830221
KZ
2041 dev->u.driver.in = g_malloc0( \
2042 sizeof(Audiodev##pdo_name##PerDirectionOptions)); \
71830221 2043 } \
ceb19c8f 2044 if (!dev->u.driver.out) { \
71830221 2045 dev->u.driver.out = g_malloc0( \
725662d6 2046 sizeof(Audiodev##pdo_name##PerDirectionOptions)); \
71830221
KZ
2047 } \
2048 break
2049
2050 CASE(NONE, none, );
2051 CASE(ALSA, alsa, Alsa);
2052 CASE(COREAUDIO, coreaudio, Coreaudio);
739362d4 2053 CASE(DBUS, dbus, );
71830221 2054 CASE(DSOUND, dsound, );
2e445703 2055 CASE(JACK, jack, Jack);
71830221
KZ
2056 CASE(OSS, oss, Oss);
2057 CASE(PA, pa, Pa);
5a0926c2 2058 CASE(SDL, sdl, Sdl);
663df1cc 2059 CASE(SNDIO, sndio, );
71830221
KZ
2060 CASE(SPICE, spice, );
2061 CASE(WAV, wav, );
2062
2063 case AUDIODEV_DRIVER__MAX:
2064 abort();
2065 };
2066}
2067
2068static void audio_validate_per_direction_opts(
2069 AudiodevPerDirectionOptions *pdo, Error **errp)
2070{
1930616b
KZ
2071 if (!pdo->has_mixing_engine) {
2072 pdo->has_mixing_engine = true;
2073 pdo->mixing_engine = true;
2074 }
71830221
KZ
2075 if (!pdo->has_fixed_settings) {
2076 pdo->has_fixed_settings = true;
1930616b 2077 pdo->fixed_settings = pdo->mixing_engine;
71830221
KZ
2078 }
2079 if (!pdo->fixed_settings &&
2080 (pdo->has_frequency || pdo->has_channels || pdo->has_format)) {
2081 error_setg(errp,
2082 "You can't use frequency, channels or format with fixed-settings=off");
2083 return;
2084 }
1930616b
KZ
2085 if (!pdo->mixing_engine && pdo->fixed_settings) {
2086 error_setg(errp, "You can't use fixed-settings without mixeng");
2087 return;
2088 }
71830221
KZ
2089
2090 if (!pdo->has_frequency) {
2091 pdo->has_frequency = true;
2092 pdo->frequency = 44100;
2093 }
2094 if (!pdo->has_channels) {
2095 pdo->has_channels = true;
2096 pdo->channels = 2;
2097 }
2098 if (!pdo->has_voices) {
2099 pdo->has_voices = true;
1930616b 2100 pdo->voices = pdo->mixing_engine ? 1 : INT_MAX;
71830221
KZ
2101 }
2102 if (!pdo->has_format) {
2103 pdo->has_format = true;
2104 pdo->format = AUDIO_FORMAT_S16;
2105 }
2106}
2107
2108static void audio_validate_opts(Audiodev *dev, Error **errp)
2109{
2110 Error *err = NULL;
2111
2112 audio_create_pdos(dev);
2113
2114 audio_validate_per_direction_opts(audio_get_pdo_in(dev), &err);
2115 if (err) {
2116 error_propagate(errp, err);
2117 return;
2118 }
2119
2120 audio_validate_per_direction_opts(audio_get_pdo_out(dev), &err);
2121 if (err) {
2122 error_propagate(errp, err);
2123 return;
2124 }
2125
2126 if (!dev->has_timer_period) {
2127 dev->has_timer_period = true;
2128 dev->timer_period = 10000; /* 100Hz -> 10ms */
2129 }
2130}
2131
5e03b6da
CF
2132void audio_help(void)
2133{
2134 int i;
2135
2136 printf("Available audio drivers:\n");
2137
2138 for (i = 0; i < AUDIODEV_DRIVER__MAX; i++) {
2139 audio_driver *driver = audio_driver_lookup(AudiodevDriver_str(i));
2140 if (driver) {
2141 printf("%s\n", driver->name);
2142 }
2143 }
2144}
2145
71830221
KZ
2146void audio_parse_option(const char *opt)
2147{
71830221
KZ
2148 Audiodev *dev = NULL;
2149
5e03b6da
CF
2150 if (is_help_option(opt)) {
2151 audio_help();
2152 exit(EXIT_SUCCESS);
2153 }
71830221
KZ
2154 Visitor *v = qobject_input_visitor_new_str(opt, "driver", &error_fatal);
2155 visit_type_Audiodev(v, NULL, &dev, &error_fatal);
2156 visit_free(v);
2157
039a6837
PB
2158 audio_define(dev);
2159}
2160
2161void audio_define(Audiodev *dev)
2162{
2163 AudiodevListEntry *e;
2164
71830221
KZ
2165 audio_validate_opts(dev, &error_fatal);
2166
b21e2380 2167 e = g_new0(AudiodevListEntry, 1);
71830221
KZ
2168 e->dev = dev;
2169 QSIMPLEQ_INSERT_TAIL(&audiodevs, e, next);
2170}
2171
0f957c53 2172bool audio_init_audiodevs(void)
71830221
KZ
2173{
2174 AudiodevListEntry *e;
2175
2176 QSIMPLEQ_FOREACH(e, &audiodevs, next) {
0f957c53
MAL
2177 if (!audio_init(e->dev, NULL)) {
2178 return false;
2179 }
71830221 2180 }
0f957c53
MAL
2181
2182 return true;
71830221
KZ
2183}
2184
2185audsettings audiodev_to_audsettings(AudiodevPerDirectionOptions *pdo)
2186{
2187 return (audsettings) {
2188 .freq = pdo->frequency,
2189 .nchannels = pdo->channels,
2190 .fmt = pdo->format,
2191 .endianness = AUDIO_HOST_ENDIANNESS,
2192 };
2193}
2194
2195int audioformat_bytes_per_sample(AudioFormat fmt)
2196{
2197 switch (fmt) {
2198 case AUDIO_FORMAT_U8:
2199 case AUDIO_FORMAT_S8:
2200 return 1;
2201
2202 case AUDIO_FORMAT_U16:
2203 case AUDIO_FORMAT_S16:
2204 return 2;
2205
2206 case AUDIO_FORMAT_U32:
2207 case AUDIO_FORMAT_S32:
ed2a4a79 2208 case AUDIO_FORMAT_F32:
71830221
KZ
2209 return 4;
2210
2211 case AUDIO_FORMAT__MAX:
2212 ;
2213 }
2214 abort();
2215}
2216
2217
2218/* frames = freq * usec / 1e6 */
2219int audio_buffer_frames(AudiodevPerDirectionOptions *pdo,
2220 audsettings *as, int def_usecs)
2221{
2222 uint64_t usecs = pdo->has_buffer_length ? pdo->buffer_length : def_usecs;
2223 return (as->freq * usecs + 500000) / 1000000;
2224}
2225
2226/* samples = channels * frames = channels * freq * usec / 1e6 */
2227int audio_buffer_samples(AudiodevPerDirectionOptions *pdo,
2228 audsettings *as, int def_usecs)
2229{
2230 return as->nchannels * audio_buffer_frames(pdo, as, def_usecs);
2231}
2232
2233/*
2234 * bytes = bytes_per_sample * samples =
2235 * bytes_per_sample * channels * freq * usec / 1e6
2236 */
2237int audio_buffer_bytes(AudiodevPerDirectionOptions *pdo,
2238 audsettings *as, int def_usecs)
2239{
2240 return audio_buffer_samples(pdo, as, def_usecs) *
2241 audioformat_bytes_per_sample(as->fmt);
2242}
ecd97e95
KZ
2243
2244AudioState *audio_state_by_name(const char *name)
2245{
2246 AudioState *s;
2247 QTAILQ_FOREACH(s, &audio_states, list) {
2248 assert(s->dev);
2249 if (strcmp(name, s->dev->id) == 0) {
2250 return s;
2251 }
2252 }
2253 return NULL;
2254}
2255
2256const char *audio_get_id(QEMUSoundCard *card)
2257{
2258 if (card->state) {
2259 assert(card->state->dev);
2260 return card->state->dev->id;
2261 } else {
2262 return "";
2263 }
2264}
857271a2 2265
37a54d05
VR
2266const char *audio_application_name(void)
2267{
2268 const char *vm_name;
2269
2270 vm_name = qemu_get_vm_name();
2271 return vm_name ? vm_name : "qemu";
2272}
2273
857271a2
KZ
2274void audio_rate_start(RateCtl *rate)
2275{
2276 memset(rate, 0, sizeof(RateCtl));
2277 rate->start_ticks = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
2278}
2279
02732641 2280size_t audio_rate_peek_bytes(RateCtl *rate, struct audio_pcm_info *info)
857271a2
KZ
2281{
2282 int64_t now;
2283 int64_t ticks;
2284 int64_t bytes;
02732641 2285 int64_t frames;
857271a2
KZ
2286
2287 now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
2288 ticks = now - rate->start_ticks;
2289 bytes = muldiv64(ticks, info->bytes_per_second, NANOSECONDS_PER_SECOND);
02732641
VR
2290 frames = (bytes - rate->bytes_sent) / info->bytes_per_frame;
2291 if (frames < 0 || frames > 65536) {
2292 AUD_log(NULL, "Resetting rate control (%" PRId64 " frames)\n", frames);
857271a2 2293 audio_rate_start(rate);
02732641 2294 frames = 0;
857271a2
KZ
2295 }
2296
02732641
VR
2297 return frames * info->bytes_per_frame;
2298}
2299
2300void audio_rate_add_bytes(RateCtl *rate, size_t bytes_used)
2301{
2302 rate->bytes_sent += bytes_used;
2303}
2304
613fe02b 2305size_t audio_rate_get_bytes(RateCtl *rate, struct audio_pcm_info *info,
02732641
VR
2306 size_t bytes_avail)
2307{
2308 size_t bytes;
2309
2310 bytes = audio_rate_peek_bytes(rate, info);
2311 bytes = MIN(bytes, bytes_avail);
2312 audio_rate_add_bytes(rate, bytes);
2313
2314 return bytes;
857271a2 2315}
637d1809
DB
2316
2317AudiodevList *qmp_query_audiodevs(Error **errp)
2318{
2319 AudiodevList *ret = NULL;
2320 AudiodevListEntry *e;
2321 QSIMPLEQ_FOREACH(e, &audiodevs, next) {
2322 QAPI_LIST_PREPEND(ret, QAPI_CLONE(Audiodev, e->dev));
2323 }
2324 return ret;
2325}