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