master
c 1,782 lines 49.4 KB
Raw
1 /*
2 * SPDX-License-Identifier: MIT
3 *
4 * Copyright (c) 2003-2005 Vassili Karpov (malc)
5 */
6
7 #include "qemu/osdep.h"
8 #include "qemu/audio.h"
9 #include "migration/vmstate.h"
10 #include "qemu/bswap.h"
11 #include "qemu/timer.h"
12 #include "qapi/error.h"
13 #include "qapi/clone-visitor.h"
14 #include "qapi/qobject-input-visitor.h"
15 #include "qapi/qapi-visit-audio.h"
16 #include "qapi/qapi-commands-audio.h"
17 #include "qobject/qdict.h"
18 #include "qemu/error-report.h"
19 #include "qemu/log.h"
20 #include "qemu/module.h"
21 #include "qemu/help_option.h"
22 #include "qom/object.h"
23 #include "system/system.h"
24 #include "system/replay.h"
25 #include "system/runstate.h"
26 #include "trace.h"
27 #include "trace/control.h"
28
29 #include "audio_int.h"
30
31 #define SW_NAME(sw) (sw)->name ? (sw)->name : "unknown"
32
33 #define audio_bug(fmt, ...) error_report("%s: " fmt, __func__, ##__VA_ARGS__)
34
35 const struct mixeng_volume nominal_volume = {
36 .mute = 0,
37 #ifdef FLOAT_MIXENG
38 .r = 1.0,
39 .l = 1.0,
40 #else
41 .r = 1ULL << 32,
42 .l = 1ULL << 32,
43 #endif
44 };
45
46 /*
47 * Convert audio format to mixeng_clip index. Used by audio_pcm_sw_init_ and
48 * audio_mixeng_backend_add_capture()
49 */
50 static int audio_format_to_index(AudioFormat af)
51 {
52 switch (af) {
53 case AUDIO_FORMAT_U8:
54 case AUDIO_FORMAT_S8:
55 return 0;
56 case AUDIO_FORMAT_U16:
57 case AUDIO_FORMAT_S16:
58 return 1;
59 case AUDIO_FORMAT_U32:
60 case AUDIO_FORMAT_S32:
61 return 2;
62 case AUDIO_FORMAT_F32:
63 case AUDIO_FORMAT__MAX:
64 break;
65 }
66
67 g_assert_not_reached();
68 }
69
70 static char *audsettings_to_string(const struct audsettings *as)
71 {
72 return g_strdup_printf("frequency=%d nchannels=%d fmt=%s endian=%s",
73 as->freq, as->nchannels, AudioFormat_str(as->fmt),
74 as->big_endian ? "big" : "little");
75 }
76
77 static int audio_validate_settings (const struct audsettings *as)
78 {
79 int invalid;
80
81 invalid = as->nchannels < 1;
82
83 switch (as->fmt) {
84 case AUDIO_FORMAT_S8:
85 case AUDIO_FORMAT_U8:
86 case AUDIO_FORMAT_S16:
87 case AUDIO_FORMAT_U16:
88 case AUDIO_FORMAT_S32:
89 case AUDIO_FORMAT_U32:
90 case AUDIO_FORMAT_F32:
91 break;
92 default:
93 invalid = 1;
94 break;
95 }
96
97 invalid |= as->freq <= 0;
98 return invalid ? -1 : 0;
99 }
100
101 static int audio_pcm_info_eq (struct audio_pcm_info *info, const struct audsettings *as)
102 {
103 return info->af == as->fmt
104 && info->freq == as->freq
105 && info->nchannels == as->nchannels
106 && info->swap_endianness == (as->big_endian != HOST_BIG_ENDIAN);
107 }
108
109 void audio_pcm_init_info (struct audio_pcm_info *info, const struct audsettings *as)
110 {
111 info->af = as->fmt;
112 info->freq = as->freq;
113 info->nchannels = as->nchannels;
114 info->bytes_per_frame = as->nchannels * audio_format_bits(as->fmt) / 8;
115 info->bytes_per_second = info->freq * info->bytes_per_frame;
116 info->swap_endianness = (as->big_endian != HOST_BIG_ENDIAN);
117 }
118
119 void audio_pcm_info_clear_buf(struct audio_pcm_info *info, void *buf, int len)
120 {
121 if (!len) {
122 return;
123 }
124
125 switch (info->af) {
126 case AUDIO_FORMAT_U8:
127 memset(buf, 0x80, len * info->bytes_per_frame);
128 break;
129 case AUDIO_FORMAT_U16: {
130 int i;
131 uint16_t *p = buf;
132 short s = INT16_MAX;
133
134 if (info->swap_endianness) {
135 s = bswap16(s);
136 }
137
138 for (i = 0; i < len * info->nchannels; i++) {
139 p[i] = s;
140 }
141 break;
142 }
143 case AUDIO_FORMAT_U32: {
144 int i;
145 uint32_t *p = buf;
146 int32_t s = INT32_MAX;
147
148 if (info->swap_endianness) {
149 s = bswap32(s);
150 }
151
152 for (i = 0; i < len * info->nchannels; i++) {
153 p[i] = s;
154 }
155 break;
156 }
157 case AUDIO_FORMAT_S8:
158 case AUDIO_FORMAT_S16:
159 case AUDIO_FORMAT_S32:
160 case AUDIO_FORMAT_F32:
161 memset(buf, 0x00, len * info->bytes_per_frame);
162 break;
163 case AUDIO_FORMAT__MAX:
164 g_assert_not_reached();
165 }
166 }
167
168 /*
169 * Capture
170 */
171 static CaptureVoiceOut *audio_pcm_capture_find_specific(AudioMixengBackend *s,
172 const struct audsettings *as)
173 {
174 CaptureVoiceOut *cap;
175
176 for (cap = s->cap_head.lh_first; cap; cap = cap->entries.le_next) {
177 if (audio_pcm_info_eq (&cap->hw.info, as)) {
178 return cap;
179 }
180 }
181 return NULL;
182 }
183
184 static void audio_notify_capture (CaptureVoiceOut *cap, audcnotification_e cmd)
185 {
186 struct capture_callback *cb;
187
188 trace_audio_notify_capture(cmd);
189
190 for (cb = cap->cb_head.lh_first; cb; cb = cb->entries.le_next) {
191 cb->ops.notify (cb->opaque, cmd);
192 }
193 }
194
195 static void audio_capture_maybe_changed(CaptureVoiceOut *cap, bool enabled)
196 {
197 if (cap->hw.enabled != enabled) {
198 audcnotification_e cmd;
199 cap->hw.enabled = enabled;
200 cmd = enabled ? AUD_CNOTIFY_ENABLE : AUD_CNOTIFY_DISABLE;
201 audio_notify_capture (cap, cmd);
202 }
203 }
204
205 static void audio_recalc_and_notify_capture (CaptureVoiceOut *cap)
206 {
207 HWVoiceOut *hw = &cap->hw;
208 SWVoiceOut *sw;
209 bool enabled = false;
210
211 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
212 if (sw->active) {
213 enabled = true;
214 break;
215 }
216 }
217 audio_capture_maybe_changed (cap, enabled);
218 }
219
220 static void audio_detach_capture (HWVoiceOut *hw)
221 {
222 SWVoiceCap *sc = hw->cap_head.lh_first;
223
224 while (sc) {
225 SWVoiceCap *sc1 = sc->entries.le_next;
226 SWVoiceOut *sw = &sc->sw;
227 CaptureVoiceOut *cap = sc->cap;
228 int was_active = sw->active;
229
230 g_clear_pointer(&sw->name, g_free);
231 if (sw->rate) {
232 st_rate_stop (sw->rate);
233 sw->rate = NULL;
234 }
235
236 QLIST_REMOVE (sw, entries);
237 QLIST_REMOVE (sc, entries);
238 g_free (sc);
239 if (was_active) {
240 /* We have removed soft voice from the capture:
241 this might have changed the overall status of the capture
242 since this might have been the only active voice */
243 audio_recalc_and_notify_capture (cap);
244 }
245 sc = sc1;
246 }
247 }
248
249 static int audio_attach_capture (HWVoiceOut *hw)
250 {
251 AudioMixengBackend *s = hw->s;
252 CaptureVoiceOut *cap;
253
254 audio_detach_capture (hw);
255 for (cap = s->cap_head.lh_first; cap; cap = cap->entries.le_next) {
256 SWVoiceCap *sc;
257 SWVoiceOut *sw;
258 HWVoiceOut *hw_cap = &cap->hw;
259
260 sc = g_malloc0(sizeof(*sc));
261
262 sc->cap = cap;
263 sw = &sc->sw;
264 sw->hw = hw_cap;
265 sw->info = hw->info;
266 sw->empty = true;
267 sw->active = hw->enabled;
268 sw->vol = nominal_volume;
269 sw->rate = st_rate_start (sw->info.freq, hw_cap->info.freq);
270 QLIST_INSERT_HEAD (&hw_cap->sw_head, sw, entries);
271 QLIST_INSERT_HEAD (&hw->cap_head, sc, entries);
272 sw->name = g_strdup_printf("for %p %d,%s,%d",
273 hw, sw->info.freq, AudioFormat_str(sw->info.af),
274 sw->info.nchannels);
275 trace_audio_capture_attach(sw->name, sw->active);
276 if (sw->active) {
277 audio_capture_maybe_changed (cap, 1);
278 }
279 }
280 return 0;
281 }
282
283 /*
284 * Hard voice (capture)
285 */
286 static size_t audio_pcm_hw_find_min_in (HWVoiceIn *hw)
287 {
288 SWVoiceIn *sw;
289 size_t m = hw->total_samples_captured;
290
291 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
292 if (sw->active) {
293 m = MIN (m, sw->total_hw_samples_acquired);
294 }
295 }
296 return m;
297 }
298
299 static size_t audio_pcm_hw_get_live_in(HWVoiceIn *hw)
300 {
301 size_t live = hw->total_samples_captured - audio_pcm_hw_find_min_in (hw);
302 if (live > hw->conv_buf.size) {
303 audio_bug("live=%zu hw->conv_buf.size=%zu", live, hw->conv_buf.size);
304 return 0;
305 }
306 return live;
307 }
308
309 static size_t audio_pcm_hw_conv_in(HWVoiceIn *hw, void *pcm_buf, size_t samples)
310 {
311 size_t conv = 0;
312 STSampleBuffer *conv_buf = &hw->conv_buf;
313
314 while (samples) {
315 uint8_t *src = advance(pcm_buf, conv * hw->info.bytes_per_frame);
316 size_t proc = MIN(samples, conv_buf->size - conv_buf->pos);
317
318 hw->conv(conv_buf->buffer + conv_buf->pos, src, proc);
319 conv_buf->pos = (conv_buf->pos + proc) % conv_buf->size;
320 samples -= proc;
321 conv += proc;
322 }
323
324 return conv;
325 }
326
327 /*
328 * Soft voice (capture)
329 */
330 static void audio_pcm_sw_resample_in(SWVoiceIn *sw,
331 size_t frames_in_max, size_t frames_out_max,
332 size_t *total_in, size_t *total_out)
333 {
334 HWVoiceIn *hw = sw->hw;
335 struct st_sample *src, *dst;
336 size_t live, rpos, frames_in, frames_out;
337
338 live = hw->total_samples_captured - sw->total_hw_samples_acquired;
339 rpos = audio_ring_posb(hw->conv_buf.pos, live, hw->conv_buf.size);
340
341 /* resample conv_buf from rpos to end of buffer */
342 src = hw->conv_buf.buffer + rpos;
343 frames_in = MIN(frames_in_max, hw->conv_buf.size - rpos);
344 dst = sw->resample_buf.buffer;
345 frames_out = frames_out_max;
346 st_rate_flow(sw->rate, src, dst, &frames_in, &frames_out);
347 rpos += frames_in;
348 *total_in = frames_in;
349 *total_out = frames_out;
350
351 /* resample conv_buf from start of buffer if there are input frames left */
352 if (frames_in_max - frames_in && rpos == hw->conv_buf.size) {
353 src = hw->conv_buf.buffer;
354 frames_in = frames_in_max - frames_in;
355 dst += frames_out;
356 frames_out = frames_out_max - frames_out;
357 st_rate_flow(sw->rate, src, dst, &frames_in, &frames_out);
358 *total_in += frames_in;
359 *total_out += frames_out;
360 }
361 }
362
363 static size_t audio_pcm_sw_read(SWVoiceIn *sw, void *buf, size_t buf_len)
364 {
365 HWVoiceIn *hw = sw->hw;
366 size_t live, frames_out_max, total_in, total_out;
367
368 live = hw->total_samples_captured - sw->total_hw_samples_acquired;
369 if (!live) {
370 return 0;
371 }
372 if (live > hw->conv_buf.size) {
373 audio_bug("live=%zu hw->conv_buf.size=%zu", live, hw->conv_buf.size);
374 return 0;
375 }
376
377 frames_out_max = MIN(buf_len / sw->info.bytes_per_frame,
378 sw->resample_buf.size);
379
380 audio_pcm_sw_resample_in(sw, live, frames_out_max, &total_in, &total_out);
381
382 if (!AUDIO_MIXENG_BACKEND_GET_CLASS(hw->s)->volume_in) {
383 mixeng_volume(sw->resample_buf.buffer, total_out, &sw->vol);
384 }
385 sw->clip(buf, sw->resample_buf.buffer, total_out);
386
387 sw->total_hw_samples_acquired += total_in;
388 return total_out * sw->info.bytes_per_frame;
389 }
390
391 /*
392 * Hard voice (playback)
393 */
394 static size_t audio_pcm_hw_find_min_out (HWVoiceOut *hw, int *nb_livep)
395 {
396 SWVoiceOut *sw;
397 size_t m = SIZE_MAX;
398 int nb_live = 0;
399
400 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
401 if (sw->active || !sw->empty) {
402 m = MIN (m, sw->total_hw_samples_mixed);
403 nb_live += 1;
404 }
405 }
406
407 *nb_livep = nb_live;
408 return m;
409 }
410
411 static size_t audio_pcm_hw_get_live_out (HWVoiceOut *hw, int *nb_live)
412 {
413 size_t smin;
414 int nb_live1;
415
416 smin = audio_pcm_hw_find_min_out (hw, &nb_live1);
417 if (nb_live) {
418 *nb_live = nb_live1;
419 }
420
421 if (nb_live1) {
422 size_t live = smin;
423
424 if (live > hw->mix_buf.size) {
425 audio_bug("live=%zu hw->mix_buf.size=%zu", live, hw->mix_buf.size);
426 return 0;
427 }
428 return live;
429 }
430 return 0;
431 }
432
433 static size_t audio_pcm_hw_get_free(HWVoiceOut *hw)
434 {
435 AudioMixengBackendClass *k = AUDIO_MIXENG_BACKEND_GET_CLASS(hw->s);
436
437 return (k->buffer_get_free ? k->buffer_get_free(hw) : INT_MAX) /
438 hw->info.bytes_per_frame;
439 }
440
441 static void audio_pcm_hw_clip_out(HWVoiceOut *hw, void *pcm_buf, size_t len)
442 {
443 size_t clipped = 0;
444 size_t pos = hw->mix_buf.pos;
445
446 while (len) {
447 st_sample *src = hw->mix_buf.buffer + pos;
448 uint8_t *dst = advance(pcm_buf, clipped * hw->info.bytes_per_frame);
449 size_t samples_till_end_of_buf = hw->mix_buf.size - pos;
450 size_t samples_to_clip = MIN(len, samples_till_end_of_buf);
451
452 hw->clip(dst, src, samples_to_clip);
453
454 pos = (pos + samples_to_clip) % hw->mix_buf.size;
455 len -= samples_to_clip;
456 clipped += samples_to_clip;
457 }
458 }
459
460 /*
461 * Soft voice (playback)
462 */
463 static void audio_pcm_sw_resample_out(SWVoiceOut *sw,
464 size_t frames_in_max, size_t frames_out_max,
465 size_t *total_in, size_t *total_out)
466 {
467 HWVoiceOut *hw = sw->hw;
468 struct st_sample *src, *dst;
469 size_t live, wpos, frames_in, frames_out;
470
471 live = sw->total_hw_samples_mixed;
472 wpos = (hw->mix_buf.pos + live) % hw->mix_buf.size;
473
474 /* write to mix_buf from wpos to end of buffer */
475 src = sw->resample_buf.buffer;
476 frames_in = frames_in_max;
477 dst = hw->mix_buf.buffer + wpos;
478 frames_out = MIN(frames_out_max, hw->mix_buf.size - wpos);
479 st_rate_flow_mix(sw->rate, src, dst, &frames_in, &frames_out);
480 wpos += frames_out;
481 *total_in = frames_in;
482 *total_out = frames_out;
483
484 /* write to mix_buf from start of buffer if there are input frames left */
485 if (frames_in_max - frames_in > 0 && wpos == hw->mix_buf.size) {
486 src += frames_in;
487 frames_in = frames_in_max - frames_in;
488 dst = hw->mix_buf.buffer;
489 frames_out = frames_out_max - frames_out;
490 st_rate_flow_mix(sw->rate, src, dst, &frames_in, &frames_out);
491 *total_in += frames_in;
492 *total_out += frames_out;
493 }
494 }
495
496 static size_t audio_pcm_sw_write(SWVoiceOut *sw, void *buf, size_t buf_len)
497 {
498 HWVoiceOut *hw = sw->hw;
499 size_t live, dead, hw_free, sw_max, fe_max;
500 size_t frames_in_max, frames_out_max, total_in, total_out;
501
502 live = sw->total_hw_samples_mixed;
503 if (live > hw->mix_buf.size) {
504 audio_bug("live=%zu hw->mix_buf.size=%zu", live, hw->mix_buf.size);
505 return 0;
506 }
507
508 if (live == hw->mix_buf.size) {
509 trace_audio_out_full(sw->name, live);
510 return 0;
511 }
512
513 dead = hw->mix_buf.size - live;
514 hw_free = audio_pcm_hw_get_free(hw);
515 hw_free = hw_free > live ? hw_free - live : 0;
516 frames_out_max = MIN(dead, hw_free);
517 sw_max = st_rate_frames_in(sw->rate, frames_out_max);
518 fe_max = MIN(buf_len / sw->info.bytes_per_frame + sw->resample_buf.pos,
519 sw->resample_buf.size);
520 frames_in_max = MIN(sw_max, fe_max);
521
522 if (!frames_in_max) {
523 return 0;
524 }
525
526 if (frames_in_max > sw->resample_buf.pos) {
527 sw->conv(sw->resample_buf.buffer + sw->resample_buf.pos,
528 buf, frames_in_max - sw->resample_buf.pos);
529 if (!AUDIO_MIXENG_BACKEND_GET_CLASS(sw->hw->s)->volume_out) {
530 mixeng_volume(sw->resample_buf.buffer + sw->resample_buf.pos,
531 frames_in_max - sw->resample_buf.pos, &sw->vol);
532 }
533 }
534
535 audio_pcm_sw_resample_out(sw, frames_in_max, frames_out_max,
536 &total_in, &total_out);
537
538 sw->total_hw_samples_mixed += total_out;
539 sw->empty = sw->total_hw_samples_mixed == 0;
540
541 /*
542 * Upsampling may leave one audio frame in the resample buffer. Decrement
543 * total_in by one if there was a leftover frame from the previous resample
544 * pass in the resample buffer. Increment total_in by one if the current
545 * resample pass left one frame in the resample buffer.
546 */
547 if (frames_in_max - total_in == 1) {
548 /* copy one leftover audio frame to the beginning of the buffer */
549 *sw->resample_buf.buffer = *(sw->resample_buf.buffer + total_in);
550 total_in += 1 - sw->resample_buf.pos;
551 sw->resample_buf.pos = 1;
552 } else if (total_in >= sw->resample_buf.pos) {
553 total_in -= sw->resample_buf.pos;
554 sw->resample_buf.pos = 0;
555 }
556
557 trace_audio_sw_write(SW_NAME(sw), buf_len / sw->info.bytes_per_frame,
558 total_in, sw->total_hw_samples_mixed);
559
560 return total_in * sw->info.bytes_per_frame;
561 }
562
563 #define DAC
564 #include "audio_template.h"
565 #undef DAC
566 #include "audio_template.h"
567
568 /*
569 * Timer
570 */
571 static int audio_is_timer_needed(AudioMixengBackend *s)
572 {
573 HWVoiceIn *hwi = NULL;
574 HWVoiceOut *hwo = NULL;
575
576 while ((hwo = audio_pcm_hw_find_any_enabled_out(s, hwo))) {
577 if (!hwo->poll_mode) {
578 return 1;
579 }
580 }
581 while ((hwi = audio_pcm_hw_find_any_enabled_in(s, hwi))) {
582 if (!hwi->poll_mode) {
583 return 1;
584 }
585 }
586 return 0;
587 }
588
589 static void audio_reset_timer(AudioMixengBackend *s)
590 {
591 if (audio_is_timer_needed(s)) {
592 timer_mod_anticipate_ns(s->ts,
593 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + s->period_ticks);
594 if (!s->timer_running) {
595 s->timer_running = true;
596 s->timer_last = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
597 trace_audio_timer_start(s->period_ticks / SCALE_MS);
598 }
599 } else {
600 timer_del(s->ts);
601 if (s->timer_running) {
602 s->timer_running = false;
603 trace_audio_timer_stop();
604 }
605 }
606 }
607
608 static void audio_timer (void *opaque)
609 {
610 int64_t now, diff;
611 AudioMixengBackend *s = opaque;
612
613 now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
614 diff = now - s->timer_last;
615 if (diff > s->period_ticks * 3 / 2) {
616 trace_audio_timer_delayed(diff / SCALE_MS);
617 }
618 s->timer_last = now;
619
620 audio_run(s, "timer");
621 audio_reset_timer(s);
622 }
623
624 /*
625 * Public API
626 */
627 static size_t audio_mixeng_backend_write(AudioBackend *be, SWVoiceOut *sw,
628 void *buf, size_t size)
629 {
630 HWVoiceOut *hw;
631
632 if (!sw) {
633 /* XXX: Consider options */
634 return size;
635 }
636 hw = sw->hw;
637
638 if (!hw->enabled) {
639 warn_report("audio: Writing to disabled voice %s", SW_NAME(sw));
640 return 0;
641 }
642
643 if (audio_get_pdo_out(hw->s->dev)->mixing_engine) {
644 return audio_pcm_sw_write(sw, buf, size);
645 } else {
646 return AUDIO_MIXENG_BACKEND_GET_CLASS(hw->s)->write(hw, buf, size);
647 }
648 }
649
650 static size_t audio_mixeng_backend_read(AudioBackend *be,
651 SWVoiceIn *sw, void *buf, size_t size)
652 {
653 HWVoiceIn *hw;
654
655 if (!sw) {
656 /* XXX: Consider options */
657 return size;
658 }
659 hw = sw->hw;
660
661 if (!hw->enabled) {
662 warn_report("audio: Reading from disabled voice %s", SW_NAME(sw));
663 return 0;
664 }
665
666 if (audio_get_pdo_in(hw->s->dev)->mixing_engine) {
667 return audio_pcm_sw_read(sw, buf, size);
668 } else {
669 return AUDIO_MIXENG_BACKEND_GET_CLASS(hw->s)->read(hw, buf, size);
670 }
671
672 }
673
674 static int audio_mixeng_backend_get_buffer_size_out(AudioBackend *be, SWVoiceOut *sw)
675 {
676 if (!sw) {
677 return 0;
678 }
679
680 if (audio_get_pdo_out(sw->s->dev)->mixing_engine) {
681 return sw->resample_buf.size * sw->info.bytes_per_frame;
682 }
683
684 return sw->hw->samples * sw->hw->info.bytes_per_frame;
685 }
686
687 static void audio_mixeng_backend_set_active_out(AudioBackend *be, SWVoiceOut *sw,
688 bool on)
689 {
690 HWVoiceOut *hw;
691
692 if (!sw) {
693 return;
694 }
695
696 hw = sw->hw;
697 if (sw->active != on) {
698 AudioMixengBackend *s = sw->s;
699 SWVoiceOut *temp_sw;
700 SWVoiceCap *sc;
701
702 if (on) {
703 AudioMixengBackendClass *k = AUDIO_MIXENG_BACKEND_GET_CLASS(s);
704
705 hw->pending_disable = 0;
706 if (!hw->enabled) {
707 hw->enabled = true;
708 if (runstate_is_running()) {
709 if (k->enable_out) {
710 k->enable_out(hw, true);
711 }
712 audio_reset_timer (s);
713 }
714 }
715 } else {
716 if (hw->enabled) {
717 int nb_active = 0;
718
719 for (temp_sw = hw->sw_head.lh_first; temp_sw;
720 temp_sw = temp_sw->entries.le_next) {
721 nb_active += temp_sw->active != 0;
722 }
723
724 hw->pending_disable = nb_active == 1;
725 }
726 }
727
728 for (sc = hw->cap_head.lh_first; sc; sc = sc->entries.le_next) {
729 sc->sw.active = hw->enabled;
730 if (hw->enabled) {
731 audio_capture_maybe_changed (sc->cap, 1);
732 }
733 }
734 sw->active = on;
735 }
736
737 }
738
739 static void audio_mixeng_backend_set_active_in(AudioBackend *be, SWVoiceIn *sw, bool on)
740 {
741 HWVoiceIn *hw;
742
743 if (!sw) {
744 return;
745 }
746
747 hw = sw->hw;
748 if (sw->active != on) {
749 AudioMixengBackend *s = sw->s;
750 AudioMixengBackendClass *k = AUDIO_MIXENG_BACKEND_GET_CLASS(s);
751 SWVoiceIn *temp_sw;
752
753 if (on) {
754 if (!hw->enabled) {
755 hw->enabled = true;
756 if (runstate_is_running()) {
757 if (k->enable_in) {
758 k->enable_in(hw, true);
759 }
760 audio_reset_timer (s);
761 }
762 }
763 sw->total_hw_samples_acquired = hw->total_samples_captured;
764 } else {
765 if (hw->enabled) {
766 int nb_active = 0;
767
768 for (temp_sw = hw->sw_head.lh_first; temp_sw;
769 temp_sw = temp_sw->entries.le_next) {
770 nb_active += temp_sw->active != 0;
771 }
772
773 if (nb_active == 1) {
774 hw->enabled = false;
775 if (k->enable_in) {
776 k->enable_in(hw, false);
777 }
778 }
779 }
780 }
781 sw->active = on;
782 }
783 }
784
785 static size_t audio_get_avail(SWVoiceIn *sw)
786 {
787 size_t live;
788
789 if (!sw) {
790 return 0;
791 }
792
793 live = sw->hw->total_samples_captured - sw->total_hw_samples_acquired;
794 if (live > sw->hw->conv_buf.size) {
795 audio_bug("live=%zu sw->hw->conv_buf.size=%zu", live,
796 sw->hw->conv_buf.size);
797 return 0;
798 }
799
800 trace_audio_get_avail(SW_NAME(sw), live, st_rate_frames_out(sw->rate, live));
801
802 return live;
803 }
804
805 static size_t audio_get_free(SWVoiceOut *sw)
806 {
807 size_t live, dead;
808
809 if (!sw) {
810 return 0;
811 }
812
813 live = sw->total_hw_samples_mixed;
814
815 if (live > sw->hw->mix_buf.size) {
816 audio_bug("live=%zu sw->hw->mix_buf.size=%zu", live,
817 sw->hw->mix_buf.size);
818 return 0;
819 }
820
821 dead = sw->hw->mix_buf.size - live;
822
823 trace_audio_get_free(SW_NAME(sw), live, dead,
824 st_rate_frames_in(sw->rate, dead));
825
826 return dead;
827 }
828
829 static void audio_capture_mix_and_clear(HWVoiceOut *hw, size_t rpos,
830 size_t samples)
831 {
832 size_t n;
833
834 if (hw->enabled) {
835 SWVoiceCap *sc;
836
837 for (sc = hw->cap_head.lh_first; sc; sc = sc->entries.le_next) {
838 SWVoiceOut *sw = &sc->sw;
839 size_t rpos2 = rpos;
840
841 n = samples;
842 while (n) {
843 size_t till_end_of_hw = hw->mix_buf.size - rpos2;
844 size_t to_read = MIN(till_end_of_hw, n);
845 size_t live, frames_in, frames_out;
846
847 sw->resample_buf.buffer = hw->mix_buf.buffer + rpos2;
848 sw->resample_buf.size = to_read;
849 live = sw->total_hw_samples_mixed;
850
851 audio_pcm_sw_resample_out(sw,
852 to_read, sw->hw->mix_buf.size - live,
853 &frames_in, &frames_out);
854
855 sw->total_hw_samples_mixed += frames_out;
856 sw->empty = sw->total_hw_samples_mixed == 0;
857
858 if (to_read - frames_in) {
859 audio_bug("Could not mix %zu frames into a capture "
860 "buffer, mixed %zu", to_read, frames_in);
861 break;
862 }
863 n -= to_read;
864 rpos2 = (rpos2 + to_read) % hw->mix_buf.size;
865 }
866 }
867 }
868
869 n = MIN(samples, hw->mix_buf.size - rpos);
870 mixeng_clear(hw->mix_buf.buffer + rpos, n);
871 mixeng_clear(hw->mix_buf.buffer, samples - n);
872 }
873
874 static size_t audio_pcm_hw_run_out(HWVoiceOut *hw, size_t live)
875 {
876 AudioMixengBackendClass *k = AUDIO_MIXENG_BACKEND_GET_CLASS(hw->s);
877 size_t clipped = 0;
878
879 while (live) {
880 size_t size = live * hw->info.bytes_per_frame;
881 size_t decr, proc;
882 void *buf = k->get_buffer_out(hw, &size);
883
884 if (size == 0) {
885 break;
886 }
887
888 decr = MIN(size / hw->info.bytes_per_frame, live);
889 if (buf) {
890 audio_pcm_hw_clip_out(hw, buf, decr);
891 }
892 proc = k->put_buffer_out(hw, buf, decr * hw->info.bytes_per_frame) /
893 hw->info.bytes_per_frame;
894
895 live -= proc;
896 clipped += proc;
897 hw->mix_buf.pos = (hw->mix_buf.pos + proc) % hw->mix_buf.size;
898
899 if (proc == 0 || proc < decr) {
900 break;
901 }
902 }
903
904 if (k->run_buffer_out) {
905 k->run_buffer_out(hw);
906 }
907
908 return clipped;
909 }
910
911 static void audio_run_out(AudioMixengBackend *s)
912 {
913 AudioMixengBackendClass *k = AUDIO_MIXENG_BACKEND_GET_CLASS(s);
914 HWVoiceOut *hw = NULL;
915 SWVoiceOut *sw;
916
917 while ((hw = audio_pcm_hw_find_any_enabled_out(s, hw))) {
918 size_t played, live, prev_rpos;
919 size_t hw_free = audio_pcm_hw_get_free(hw);
920 int nb_live;
921
922 if (!audio_get_pdo_out(s->dev)->mixing_engine) {
923 /* there is exactly 1 sw for each hw with no mixeng */
924 sw = hw->sw_head.lh_first;
925
926 if (hw->pending_disable) {
927 hw->enabled = false;
928 hw->pending_disable = false;
929 if (k->enable_out) {
930 k->enable_out(hw, false);
931 }
932 }
933
934 if (sw->active) {
935 sw->callback.fn(sw->callback.opaque,
936 hw_free * sw->info.bytes_per_frame);
937 }
938
939 if (k->run_buffer_out) {
940 k->run_buffer_out(hw);
941 }
942
943 continue;
944 }
945
946 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
947 if (sw->active) {
948 size_t sw_free = audio_get_free(sw);
949 size_t free;
950
951 if (hw_free > sw->total_hw_samples_mixed) {
952 free = st_rate_frames_in(sw->rate,
953 MIN(sw_free, hw_free - sw->total_hw_samples_mixed));
954 } else {
955 free = 0;
956 }
957 if (free > sw->resample_buf.pos) {
958 free = MIN(free, sw->resample_buf.size)
959 - sw->resample_buf.pos;
960 sw->callback.fn(sw->callback.opaque,
961 free * sw->info.bytes_per_frame);
962 }
963 }
964 }
965
966 live = audio_pcm_hw_get_live_out (hw, &nb_live);
967 if (!nb_live) {
968 live = 0;
969 }
970
971 if (live > hw->mix_buf.size) {
972 audio_bug("live=%zu hw->mix_buf.size=%zu", live, hw->mix_buf.size);
973 continue;
974 }
975
976 if (hw->pending_disable && !nb_live) {
977 SWVoiceCap *sc;
978
979 trace_audio_out_disable();
980 hw->enabled = false;
981 hw->pending_disable = false;
982 if (k->enable_out) {
983 k->enable_out(hw, false);
984 }
985 for (sc = hw->cap_head.lh_first; sc; sc = sc->entries.le_next) {
986 sc->sw.active = false;
987 audio_recalc_and_notify_capture (sc->cap);
988 }
989 continue;
990 }
991
992 if (!live) {
993 if (k->run_buffer_out) {
994 k->run_buffer_out(hw);
995 }
996 continue;
997 }
998
999 prev_rpos = hw->mix_buf.pos;
1000 played = audio_pcm_hw_run_out(hw, live);
1001 replay_audio_out(&played);
1002 if (hw->mix_buf.pos >= hw->mix_buf.size) {
1003 audio_bug("hw->mix_buf.pos=%zu hw->mix_buf.size=%zu played=%zu",
1004 hw->mix_buf.pos, hw->mix_buf.size, played);
1005 hw->mix_buf.pos = 0;
1006 }
1007
1008 trace_audio_out_played(played);
1009
1010 if (played) {
1011 audio_capture_mix_and_clear (hw, prev_rpos, played);
1012 }
1013
1014 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
1015 if (!sw->active && sw->empty) {
1016 continue;
1017 }
1018
1019 if (played > sw->total_hw_samples_mixed) {
1020 audio_bug("played=%zu sw->total_hw_samples_mixed=%zu",
1021 played, sw->total_hw_samples_mixed);
1022 played = sw->total_hw_samples_mixed;
1023 }
1024
1025 sw->total_hw_samples_mixed -= played;
1026
1027 if (!sw->total_hw_samples_mixed) {
1028 sw->empty = true;
1029 }
1030 }
1031 }
1032 }
1033
1034 static size_t audio_pcm_hw_run_in(HWVoiceIn *hw, size_t samples)
1035 {
1036 AudioMixengBackendClass *k = AUDIO_MIXENG_BACKEND_GET_CLASS(hw->s);
1037 size_t conv = 0;
1038
1039 if (k->run_buffer_in) {
1040 k->run_buffer_in(hw);
1041 }
1042
1043 while (samples) {
1044 size_t proc;
1045 size_t size = samples * hw->info.bytes_per_frame;
1046 void *buf = k->get_buffer_in(hw, &size);
1047
1048 assert(size % hw->info.bytes_per_frame == 0);
1049 if (size == 0) {
1050 break;
1051 }
1052
1053 proc = audio_pcm_hw_conv_in(hw, buf, size / hw->info.bytes_per_frame);
1054
1055 samples -= proc;
1056 conv += proc;
1057 k->put_buffer_in(hw, buf, proc * hw->info.bytes_per_frame);
1058 }
1059
1060 return conv;
1061 }
1062
1063 static void audio_run_in(AudioMixengBackend *s)
1064 {
1065 HWVoiceIn *hw = NULL;
1066
1067 if (!audio_get_pdo_in(s->dev)->mixing_engine) {
1068 while ((hw = audio_pcm_hw_find_any_enabled_in(s, hw))) {
1069 /* there is exactly 1 sw for each hw with no mixeng */
1070 SWVoiceIn *sw = hw->sw_head.lh_first;
1071 if (sw->active) {
1072 sw->callback.fn(sw->callback.opaque, INT_MAX);
1073 }
1074 }
1075 return;
1076 }
1077
1078 while ((hw = audio_pcm_hw_find_any_enabled_in(s, hw))) {
1079 SWVoiceIn *sw;
1080 size_t captured = 0, min;
1081 int pos;
1082
1083 if (replay_mode != REPLAY_MODE_PLAY) {
1084 captured = audio_pcm_hw_run_in(
1085 hw, hw->conv_buf.size - audio_pcm_hw_get_live_in(hw));
1086 }
1087
1088 replay_audio_in_start(&captured);
1089 assert(captured <= hw->conv_buf.size);
1090 if (replay_mode == REPLAY_MODE_PLAY) {
1091 hw->conv_buf.pos = (hw->conv_buf.pos + captured) % hw->conv_buf.size;
1092 }
1093 for (pos = (hw->conv_buf.pos - captured + hw->conv_buf.size) % hw->conv_buf.size;
1094 pos != hw->conv_buf.pos;
1095 pos = (pos + 1) % hw->conv_buf.size) {
1096 uint64_t left, right;
1097
1098 if (replay_mode == REPLAY_MODE_RECORD) {
1099 audio_sample_to_uint64(hw->conv_buf.buffer, pos, &left, &right);
1100 }
1101 replay_audio_in_sample_lr(&left, &right);
1102 if (replay_mode == REPLAY_MODE_PLAY) {
1103 audio_sample_from_uint64(hw->conv_buf.buffer, pos, left, right);
1104 }
1105 }
1106 replay_audio_in_finish();
1107
1108 min = audio_pcm_hw_find_min_in (hw);
1109 hw->total_samples_captured += captured - min;
1110
1111 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
1112 sw->total_hw_samples_acquired -= min;
1113
1114 if (sw->active) {
1115 size_t sw_avail = audio_get_avail(sw);
1116 size_t avail;
1117
1118 avail = st_rate_frames_out(sw->rate, sw_avail);
1119 if (avail > 0) {
1120 avail = MIN(avail, sw->resample_buf.size);
1121 sw->callback.fn(sw->callback.opaque,
1122 avail * sw->info.bytes_per_frame);
1123 }
1124 }
1125 }
1126 }
1127 }
1128
1129 static void audio_run_capture(AudioMixengBackend *s)
1130 {
1131 CaptureVoiceOut *cap;
1132
1133 for (cap = s->cap_head.lh_first; cap; cap = cap->entries.le_next) {
1134 size_t live, rpos, captured;
1135 HWVoiceOut *hw = &cap->hw;
1136 SWVoiceOut *sw;
1137
1138 captured = live = audio_pcm_hw_get_live_out (hw, NULL);
1139 rpos = hw->mix_buf.pos;
1140 while (live) {
1141 size_t left = hw->mix_buf.size - rpos;
1142 size_t to_capture = MIN(live, left);
1143 struct st_sample *src;
1144 struct capture_callback *cb;
1145
1146 src = hw->mix_buf.buffer + rpos;
1147 hw->clip (cap->buf, src, to_capture);
1148 mixeng_clear (src, to_capture);
1149
1150 for (cb = cap->cb_head.lh_first; cb; cb = cb->entries.le_next) {
1151 cb->ops.capture (cb->opaque, cap->buf,
1152 to_capture * hw->info.bytes_per_frame);
1153 }
1154 rpos = (rpos + to_capture) % hw->mix_buf.size;
1155 live -= to_capture;
1156 }
1157 hw->mix_buf.pos = rpos;
1158
1159 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
1160 if (!sw->active && sw->empty) {
1161 continue;
1162 }
1163
1164 if (captured > sw->total_hw_samples_mixed) {
1165 audio_bug("captured=%zu sw->total_hw_samples_mixed=%zu",
1166 captured, sw->total_hw_samples_mixed);
1167 captured = sw->total_hw_samples_mixed;
1168 }
1169
1170 sw->total_hw_samples_mixed -= captured;
1171 sw->empty = sw->total_hw_samples_mixed == 0;
1172 }
1173 }
1174 }
1175
1176 void audio_run(AudioMixengBackend *s, const char *msg)
1177 {
1178 audio_run_out(s);
1179 audio_run_in(s);
1180 audio_run_capture(s);
1181
1182 if (trace_event_get_state(TRACE_AUDIO_RUN_POLL)) {
1183 /* Convert seconds to microseconds for trace event */
1184 int64_t elapsed_us = g_timer_elapsed(s->run_timer, NULL) * MICROSECONDS_PER_SECOND;
1185 trace_audio_run_poll(msg, elapsed_us);
1186 g_timer_start(s->run_timer);
1187 }
1188 }
1189
1190 void audio_generic_initialize_buffer_in(HWVoiceIn *hw)
1191 {
1192 g_free(hw->buf_emul);
1193 hw->size_emul = hw->samples * hw->info.bytes_per_frame;
1194 hw->buf_emul = g_malloc(hw->size_emul);
1195 hw->pos_emul = hw->pending_emul = 0;
1196 }
1197
1198 void audio_generic_run_buffer_in(HWVoiceIn *hw)
1199 {
1200 AudioMixengBackendClass *k = AUDIO_MIXENG_BACKEND_GET_CLASS(hw->s);
1201
1202 if (unlikely(!hw->buf_emul)) {
1203 audio_generic_initialize_buffer_in(hw);
1204 }
1205
1206 while (hw->pending_emul < hw->size_emul) {
1207 size_t read_len = MIN(hw->size_emul - hw->pos_emul,
1208 hw->size_emul - hw->pending_emul);
1209 size_t read = k->read(hw, hw->buf_emul + hw->pos_emul, read_len);
1210 hw->pending_emul += read;
1211 hw->pos_emul = (hw->pos_emul + read) % hw->size_emul;
1212 if (read < read_len) {
1213 break;
1214 }
1215 }
1216 }
1217
1218 void *audio_generic_get_buffer_in(HWVoiceIn *hw, size_t *size)
1219 {
1220 size_t start;
1221
1222 start = audio_ring_posb(hw->pos_emul, hw->pending_emul, hw->size_emul);
1223 assert(start < hw->size_emul);
1224
1225 *size = MIN(*size, hw->pending_emul);
1226 *size = MIN(*size, hw->size_emul - start);
1227 return hw->buf_emul + start;
1228 }
1229
1230 void audio_generic_put_buffer_in(HWVoiceIn *hw, void *buf, size_t size)
1231 {
1232 assert(size <= hw->pending_emul);
1233 hw->pending_emul -= size;
1234 }
1235
1236 void audio_generic_initialize_buffer_out(HWVoiceOut *hw)
1237 {
1238 g_free(hw->buf_emul);
1239 hw->size_emul = hw->samples * hw->info.bytes_per_frame;
1240 hw->buf_emul = g_malloc(hw->size_emul);
1241 hw->pos_emul = hw->pending_emul = 0;
1242 }
1243
1244 size_t audio_generic_buffer_get_free(HWVoiceOut *hw)
1245 {
1246 if (hw->buf_emul) {
1247 return hw->size_emul - hw->pending_emul;
1248 } else {
1249 return hw->samples * hw->info.bytes_per_frame;
1250 }
1251 }
1252
1253 void audio_generic_run_buffer_out(HWVoiceOut *hw)
1254 {
1255 AudioMixengBackendClass *k = AUDIO_MIXENG_BACKEND_GET_CLASS(hw->s);
1256
1257 while (hw->pending_emul) {
1258 size_t write_len, written, start;
1259
1260 start = audio_ring_posb(hw->pos_emul, hw->pending_emul, hw->size_emul);
1261 assert(start < hw->size_emul);
1262
1263 write_len = MIN(hw->pending_emul, hw->size_emul - start);
1264
1265 written = k->write(hw, hw->buf_emul + start, write_len);
1266 hw->pending_emul -= written;
1267
1268 if (written < write_len) {
1269 break;
1270 }
1271 }
1272 }
1273
1274 void *audio_generic_get_buffer_out(HWVoiceOut *hw, size_t *size)
1275 {
1276 if (unlikely(!hw->buf_emul)) {
1277 audio_generic_initialize_buffer_out(hw);
1278 }
1279
1280 *size = MIN(hw->size_emul - hw->pending_emul,
1281 hw->size_emul - hw->pos_emul);
1282 return hw->buf_emul + hw->pos_emul;
1283 }
1284
1285 size_t audio_generic_put_buffer_out(HWVoiceOut *hw, void *buf, size_t size)
1286 {
1287 assert(buf == hw->buf_emul + hw->pos_emul &&
1288 size + hw->pending_emul <= hw->size_emul);
1289
1290 hw->pending_emul += size;
1291 hw->pos_emul = (hw->pos_emul + size) % hw->size_emul;
1292
1293 return size;
1294 }
1295
1296 size_t audio_generic_write(HWVoiceOut *hw, void *buf, size_t size)
1297 {
1298 AudioMixengBackendClass *k = AUDIO_MIXENG_BACKEND_GET_CLASS(hw->s);
1299 size_t total = 0;
1300
1301 if (k->buffer_get_free) {
1302 size_t free = k->buffer_get_free(hw);
1303
1304 size = MIN(size, free);
1305 }
1306
1307 while (total < size) {
1308 size_t dst_size = size - total;
1309 size_t copy_size, proc;
1310 void *dst = k->get_buffer_out(hw, &dst_size);
1311
1312 if (dst_size == 0) {
1313 break;
1314 }
1315
1316 copy_size = MIN(size - total, dst_size);
1317 if (dst) {
1318 memcpy(dst, (char *)buf + total, copy_size);
1319 }
1320 proc = k->put_buffer_out(hw, dst, copy_size);
1321 total += proc;
1322
1323 if (proc == 0 || proc < copy_size) {
1324 break;
1325 }
1326 }
1327
1328 return total;
1329 }
1330
1331 size_t audio_generic_read(HWVoiceIn *hw, void *buf, size_t size)
1332 {
1333 AudioMixengBackendClass *k = AUDIO_MIXENG_BACKEND_GET_CLASS(hw->s);
1334 size_t total = 0;
1335
1336 if (k->run_buffer_in) {
1337 k->run_buffer_in(hw);
1338 }
1339
1340 while (total < size) {
1341 size_t src_size = size - total;
1342 void *src = k->get_buffer_in(hw, &src_size);
1343
1344 if (src_size == 0) {
1345 break;
1346 }
1347
1348 memcpy((char *)buf + total, src, src_size);
1349 k->put_buffer_in(hw, src, src_size);
1350 total += src_size;
1351 }
1352
1353 return total;
1354 }
1355
1356 static bool audio_mixeng_backend_realize(AudioBackend *abe,
1357 Audiodev *dev, Error **errp)
1358 {
1359 AudioMixengBackend *be = AUDIO_MIXENG_BACKEND(abe);
1360 AudioMixengBackendClass *k = AUDIO_MIXENG_BACKEND_GET_CLASS(be);
1361
1362 be->dev = dev;
1363 if (!k->get_buffer_in) {
1364 k->get_buffer_in = audio_generic_get_buffer_in;
1365 k->put_buffer_in = audio_generic_put_buffer_in;
1366 }
1367 if (!k->get_buffer_out) {
1368 k->get_buffer_out = audio_generic_get_buffer_out;
1369 k->put_buffer_out = audio_generic_put_buffer_out;
1370 }
1371
1372 audio_init_nb_voices_out(be, k, 1);
1373 audio_init_nb_voices_in(be, k, 0);
1374
1375 if (be->dev->timer_period <= 0) {
1376 be->period_ticks = 1;
1377 } else {
1378 be->period_ticks = be->dev->timer_period * (int64_t)SCALE_US;
1379 }
1380
1381 return true;
1382 }
1383
1384 static void audio_vm_change_state_handler (void *opaque, bool running,
1385 RunState state)
1386 {
1387 AudioMixengBackend *s = opaque;
1388 AudioMixengBackendClass *k = AUDIO_MIXENG_BACKEND_GET_CLASS(s);
1389 HWVoiceOut *hwo = NULL;
1390 HWVoiceIn *hwi = NULL;
1391
1392 while ((hwo = audio_pcm_hw_find_any_enabled_out(s, hwo))) {
1393 if (k->enable_out) {
1394 k->enable_out(hwo, running);
1395 }
1396 }
1397
1398 while ((hwi = audio_pcm_hw_find_any_enabled_in(s, hwi))) {
1399 if (k->enable_in) {
1400 k->enable_in(hwi, running);
1401 }
1402 }
1403 audio_reset_timer (s);
1404 }
1405
1406 static const VMStateDescription vmstate_audio;
1407
1408 static const char *audio_mixeng_backend_get_id(AudioBackend *be)
1409 {
1410 return AUDIO_MIXENG_BACKEND(be)->dev->id;
1411 }
1412
1413 static CaptureVoiceOut *audio_mixeng_backend_add_capture(
1414 AudioBackend *be,
1415 const struct audsettings *as,
1416 const struct audio_capture_ops *ops,
1417 void *cb_opaque);
1418
1419 static void audio_mixeng_backend_del_capture(
1420 AudioBackend *be,
1421 CaptureVoiceOut *cap,
1422 void *cb_opaque);
1423
1424 static void audio_mixeng_backend_set_volume_out(AudioBackend *be, SWVoiceOut *sw,
1425 Volume *vol);
1426 static void audio_mixeng_backend_set_volume_in(AudioBackend *be, SWVoiceIn *sw,
1427 Volume *vol);
1428
1429 static void audio_mixeng_backend_class_init(ObjectClass *klass, const void *data)
1430 {
1431 AudioBackendClass *be = AUDIO_BACKEND_CLASS(klass);
1432
1433 be->realize = audio_mixeng_backend_realize;
1434 be->get_id = audio_mixeng_backend_get_id;
1435 be->open_in = audio_mixeng_backend_open_in;
1436 be->open_out = audio_mixeng_backend_open_out;
1437 be->close_in = audio_mixeng_backend_close_in;
1438 be->close_out = audio_mixeng_backend_close_out;
1439 be->is_active_out = audio_mixeng_backend_is_active_out;
1440 be->is_active_in = audio_mixeng_backend_is_active_in;
1441 be->set_active_out = audio_mixeng_backend_set_active_out;
1442 be->set_active_in = audio_mixeng_backend_set_active_in;
1443 be->set_volume_out = audio_mixeng_backend_set_volume_out;
1444 be->set_volume_in = audio_mixeng_backend_set_volume_in;
1445 be->read = audio_mixeng_backend_read;
1446 be->write = audio_mixeng_backend_write;
1447 be->get_buffer_size_out = audio_mixeng_backend_get_buffer_size_out;
1448 be->add_capture = audio_mixeng_backend_add_capture;
1449 be->del_capture = audio_mixeng_backend_del_capture;
1450 }
1451
1452 static void audio_mixeng_backend_init(Object *obj)
1453 {
1454 AudioMixengBackend *s = AUDIO_MIXENG_BACKEND(obj);
1455
1456 QLIST_INIT(&s->hw_head_out);
1457 QLIST_INIT(&s->hw_head_in);
1458 QLIST_INIT(&s->cap_head);
1459 s->ts = timer_new_ns(QEMU_CLOCK_VIRTUAL, audio_timer, s);
1460 s->run_timer = g_timer_new();
1461
1462 s->vmse = qemu_add_vm_change_state_handler(audio_vm_change_state_handler, s);
1463 assert(s->vmse != NULL);
1464
1465 vmstate_register_any(NULL, &vmstate_audio, s);
1466 }
1467
1468 static void audio_mixeng_backend_finalize(Object *obj)
1469 {
1470 AudioMixengBackend *s = AUDIO_MIXENG_BACKEND(obj);
1471 AudioMixengBackendClass *k = AUDIO_MIXENG_BACKEND_GET_CLASS(s);
1472 HWVoiceOut *hwo, *hwon;
1473 HWVoiceIn *hwi, *hwin;
1474
1475 QLIST_FOREACH_SAFE(hwo, &s->hw_head_out, entries, hwon) {
1476 SWVoiceCap *sc;
1477
1478 if (hwo->enabled && k->enable_out) {
1479 k->enable_out(hwo, false);
1480 }
1481 k->fini_out(hwo);
1482
1483 for (sc = hwo->cap_head.lh_first; sc; sc = sc->entries.le_next) {
1484 CaptureVoiceOut *cap = sc->cap;
1485 struct capture_callback *cb;
1486
1487 for (cb = cap->cb_head.lh_first; cb; cb = cb->entries.le_next) {
1488 cb->ops.destroy (cb->opaque);
1489 }
1490 }
1491 QLIST_REMOVE(hwo, entries);
1492 }
1493
1494 QLIST_FOREACH_SAFE(hwi, &s->hw_head_in, entries, hwin) {
1495 if (hwi->enabled && k->enable_in) {
1496 k->enable_in(hwi, false);
1497 }
1498 k->fini_in(hwi);
1499 QLIST_REMOVE(hwi, entries);
1500 }
1501
1502 if (s->dev) {
1503 qapi_free_Audiodev(s->dev);
1504 s->dev = NULL;
1505 }
1506
1507 if (s->ts) {
1508 timer_free(s->ts);
1509 s->ts = NULL;
1510 }
1511
1512 g_clear_pointer(&s->run_timer, g_timer_destroy);
1513
1514 if (s->vmse) {
1515 qemu_del_vm_change_state_handler(s->vmse);
1516 s->vmse = NULL;
1517 }
1518
1519 vmstate_unregister(NULL, &vmstate_audio, s);
1520 }
1521
1522 static bool vmstate_audio_needed(void *opaque)
1523 {
1524 /*
1525 * Never needed, this vmstate only exists in case
1526 * an old qemu sends it to us.
1527 */
1528 return false;
1529 }
1530
1531 static const VMStateDescription vmstate_audio = {
1532 .name = "audio",
1533 .version_id = 1,
1534 .minimum_version_id = 1,
1535 .needed = vmstate_audio_needed,
1536 .fields = (const VMStateField[]) {
1537 VMSTATE_END_OF_LIST()
1538 }
1539 };
1540
1541 static CaptureVoiceOut *audio_mixeng_backend_add_capture(
1542 AudioBackend *be,
1543 const struct audsettings *as,
1544 const struct audio_capture_ops *ops,
1545 void *cb_opaque)
1546 {
1547 AudioMixengBackend *s = AUDIO_MIXENG_BACKEND(be);
1548 CaptureVoiceOut *cap;
1549 struct capture_callback *cb;
1550
1551 if (!s) {
1552 error_report("Capturing without setting an audiodev is not supported");
1553 abort();
1554 }
1555
1556 if (!audio_get_pdo_out(s->dev)->mixing_engine) {
1557 error_report("audio: Can't capture with mixeng disabled");
1558 return NULL;
1559 }
1560
1561 if (audio_validate_settings(as)) {
1562 g_autofree char *str = audsettings_to_string(as);
1563 error_report("audio: Invalid audio settings when trying to add capture: %s", str);
1564 return NULL;
1565 }
1566
1567 cb = g_malloc0(sizeof(*cb));
1568 cb->ops = *ops;
1569 cb->opaque = cb_opaque;
1570
1571 cap = audio_pcm_capture_find_specific(s, as);
1572 if (cap) {
1573 QLIST_INSERT_HEAD (&cap->cb_head, cb, entries);
1574 } else {
1575 HWVoiceOut *hw;
1576
1577 cap = g_malloc0(sizeof(*cap));
1578
1579 hw = &cap->hw;
1580 hw->s = s;
1581 QLIST_INIT (&hw->sw_head);
1582 QLIST_INIT (&cap->cb_head);
1583
1584 /* XXX find a more elegant way */
1585 hw->samples = 4096 * 4;
1586 audio_pcm_hw_alloc_resources_out(hw);
1587
1588 audio_pcm_init_info (&hw->info, as);
1589
1590 cap->buf = g_malloc0_n(hw->mix_buf.size, hw->info.bytes_per_frame);
1591
1592 if (audio_format_is_float(hw->info.af)) {
1593 hw->clip = mixeng_clip_float[hw->info.nchannels == 2]
1594 [hw->info.swap_endianness];
1595 } else {
1596 hw->clip = mixeng_clip
1597 [hw->info.nchannels == 2]
1598 [audio_format_is_signed(hw->info.af)]
1599 [hw->info.swap_endianness]
1600 [audio_format_to_index(hw->info.af)];
1601 }
1602
1603 QLIST_INSERT_HEAD (&s->cap_head, cap, entries);
1604 QLIST_INSERT_HEAD (&cap->cb_head, cb, entries);
1605
1606 QLIST_FOREACH(hw, &s->hw_head_out, entries) {
1607 audio_attach_capture (hw);
1608 }
1609 }
1610
1611 return cap;
1612 }
1613
1614 static void audio_mixeng_backend_del_capture(
1615 AudioBackend *be,
1616 CaptureVoiceOut *cap,
1617 void *cb_opaque)
1618 {
1619 struct capture_callback *cb;
1620
1621 for (cb = cap->cb_head.lh_first; cb; cb = cb->entries.le_next) {
1622 if (cb->opaque == cb_opaque) {
1623 cb->ops.destroy (cb_opaque);
1624 QLIST_REMOVE (cb, entries);
1625 g_free (cb);
1626
1627 if (!cap->cb_head.lh_first) {
1628 SWVoiceOut *sw = cap->hw.sw_head.lh_first, *sw1;
1629
1630 while (sw) {
1631 SWVoiceCap *sc = (SWVoiceCap *) sw;
1632
1633 trace_audio_capture_free_sw(sw->name);
1634 g_clear_pointer(&sw->name, g_free);
1635 sw1 = sw->entries.le_next;
1636 if (sw->rate) {
1637 st_rate_stop (sw->rate);
1638 sw->rate = NULL;
1639 }
1640 QLIST_REMOVE (sw, entries);
1641 QLIST_REMOVE (sc, entries);
1642 g_free (sc);
1643 sw = sw1;
1644 }
1645 QLIST_REMOVE (cap, entries);
1646 g_free(cap->hw.mix_buf.buffer);
1647 g_free (cap->buf);
1648 g_free (cap);
1649 }
1650 return;
1651 }
1652 }
1653 }
1654
1655 static void audio_mixeng_backend_set_volume_out(AudioBackend *be, SWVoiceOut *sw,
1656 Volume *vol)
1657 {
1658 if (sw) {
1659 HWVoiceOut *hw = sw->hw;
1660 AudioMixengBackendClass *k = AUDIO_MIXENG_BACKEND_GET_CLASS(hw->s);
1661
1662 sw->vol.mute = vol->mute;
1663 sw->vol.l = nominal_volume.l * vol->vol[0] / 255;
1664 sw->vol.r = nominal_volume.r * vol->vol[vol->channels > 1 ? 1 : 0] /
1665 255;
1666
1667 if (k->volume_out) {
1668 k->volume_out(hw, vol);
1669 }
1670 }
1671 }
1672
1673 static void audio_mixeng_backend_set_volume_in(AudioBackend *be, SWVoiceIn *sw,
1674 Volume *vol)
1675 {
1676 if (sw) {
1677 HWVoiceIn *hw = sw->hw;
1678 AudioMixengBackendClass *k = AUDIO_MIXENG_BACKEND_GET_CLASS(hw->s);
1679
1680 sw->vol.mute = vol->mute;
1681 sw->vol.l = nominal_volume.l * vol->vol[0] / 255;
1682 sw->vol.r = nominal_volume.r * vol->vol[vol->channels > 1 ? 1 : 0] /
1683 255;
1684
1685 if (k->volume_in) {
1686 k->volume_in(hw, vol);
1687 }
1688 }
1689 }
1690
1691 audsettings audiodev_to_audsettings(AudiodevPerDirectionOptions *pdo)
1692 {
1693 return (audsettings) {
1694 .freq = pdo->frequency,
1695 .nchannels = pdo->channels,
1696 .fmt = pdo->format,
1697 .big_endian = HOST_BIG_ENDIAN,
1698 };
1699 }
1700
1701 /* frames = freq * usec / 1e6 */
1702 int audio_buffer_frames(AudiodevPerDirectionOptions *pdo,
1703 audsettings *as, int def_usecs)
1704 {
1705 uint64_t usecs = pdo->has_buffer_length ? pdo->buffer_length : def_usecs;
1706 return (as->freq * usecs + 500000) / 1000000;
1707 }
1708
1709 /* samples = channels * frames = channels * freq * usec / 1e6 */
1710 int audio_buffer_samples(AudiodevPerDirectionOptions *pdo,
1711 audsettings *as, int def_usecs)
1712 {
1713 return as->nchannels * audio_buffer_frames(pdo, as, def_usecs);
1714 }
1715
1716 /*
1717 * bytes = bytes_per_sample * samples =
1718 * bytes_per_sample * channels * freq * usec / 1e6
1719 */
1720 int audio_buffer_bytes(AudiodevPerDirectionOptions *pdo,
1721 audsettings *as, int def_usecs)
1722 {
1723 return audio_buffer_samples(pdo, as, def_usecs) * audio_format_bits(as->fmt) / 8;
1724 }
1725
1726 void audio_rate_start(RateCtl *rate)
1727 {
1728 memset(rate, 0, sizeof(RateCtl));
1729 rate->start_ticks = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
1730 }
1731
1732 size_t audio_rate_peek_bytes(RateCtl *rate, struct audio_pcm_info *info)
1733 {
1734 int64_t now;
1735 int64_t ticks;
1736 int64_t bytes;
1737 int64_t frames;
1738
1739 now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
1740 ticks = now - rate->start_ticks;
1741 bytes = muldiv64(ticks, info->bytes_per_second, NANOSECONDS_PER_SECOND);
1742 frames = (bytes - rate->bytes_sent) / info->bytes_per_frame;
1743 rate->peeked_frames = frames;
1744
1745 return frames < 0 ? 0 : frames * info->bytes_per_frame;
1746 }
1747
1748 void audio_rate_add_bytes(RateCtl *rate, size_t bytes_used)
1749 {
1750 if (rate->peeked_frames < 0 || rate->peeked_frames > 65536) {
1751 trace_audio_rate_reset(rate->peeked_frames);
1752 audio_rate_start(rate);
1753 }
1754
1755 rate->bytes_sent += bytes_used;
1756 }
1757
1758 size_t audio_rate_get_bytes(RateCtl *rate, struct audio_pcm_info *info,
1759 size_t bytes_avail)
1760 {
1761 size_t bytes;
1762
1763 bytes = audio_rate_peek_bytes(rate, info);
1764 bytes = MIN(bytes, bytes_avail);
1765 audio_rate_add_bytes(rate, bytes);
1766
1767 return bytes;
1768 }
1769
1770 static const TypeInfo audio_types[] = {
1771 {
1772 .name = TYPE_AUDIO_MIXENG_BACKEND,
1773 .parent = TYPE_AUDIO_BACKEND,
1774 .instance_size = sizeof(AudioMixengBackend),
1775 .instance_init = audio_mixeng_backend_init,
1776 .instance_finalize = audio_mixeng_backend_finalize,
1777 .class_size = sizeof(AudioMixengBackendClass),
1778 .class_init = audio_mixeng_backend_class_init,
1779 },
1780 };
1781
1782 DEFINE_TYPES(audio_types)