4 * Copyright (c) 2003-2005 Vassili Karpov (malc)
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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
25 #include "qemu/osdep.h"
27 #include "migration/vmstate.h"
28 #include "monitor/monitor.h"
29 #include "qemu/timer.h"
30 #include "qapi/error.h"
31 #include "qapi/clone-visitor.h"
32 #include "qapi/qobject-input-visitor.h"
33 #include "qapi/qapi-visit-audio.h"
34 #include "qapi/qapi-commands-audio.h"
35 #include "qemu/cutils.h"
36 #include "qemu/module.h"
37 #include "qemu/help_option.h"
38 #include "sysemu/sysemu.h"
39 #include "sysemu/replay.h"
40 #include "sysemu/runstate.h"
41 #include "ui/qemu-spice.h"
44 #define AUDIO_CAP "audio"
45 #include "audio_int.h"
47 /* #define DEBUG_LIVE */
48 /* #define DEBUG_OUT */
49 /* #define DEBUG_CAPTURE */
50 /* #define DEBUG_POLL */
52 #define SW_NAME(sw) (sw)->name ? (sw)->name : "unknown"
55 /* Order of CONFIG_AUDIO_DRIVERS is import.
56 The 1st one is the one used by default, that is the reason
57 that we generate the list.
59 const char *audio_prio_list
[] = {
67 static QLIST_HEAD(, audio_driver
) audio_drivers
;
68 static AudiodevListHead audiodevs
= QSIMPLEQ_HEAD_INITIALIZER(audiodevs
);
70 void audio_driver_register(audio_driver
*drv
)
72 QLIST_INSERT_HEAD(&audio_drivers
, drv
, next
);
75 audio_driver
*audio_driver_lookup(const char *name
)
77 struct audio_driver
*d
;
78 Error
*local_err
= NULL
;
81 QLIST_FOREACH(d
, &audio_drivers
, next
) {
82 if (strcmp(name
, d
->name
) == 0) {
86 rv
= audio_module_load(name
, &local_err
);
88 QLIST_FOREACH(d
, &audio_drivers
, next
) {
89 if (strcmp(name
, d
->name
) == 0) {
94 error_report_err(local_err
);
99 static QTAILQ_HEAD(AudioStateHead
, AudioState
) audio_states
=
100 QTAILQ_HEAD_INITIALIZER(audio_states
);
102 const struct mixeng_volume nominal_volume
= {
113 static bool legacy_config
= true;
115 int audio_bug (const char *funcname
, int cond
)
120 AUD_log (NULL
, "A bug was just triggered in %s\n", funcname
);
123 AUD_log (NULL
, "Save all your work and restart without audio\n");
124 AUD_log (NULL
, "I am sorry\n");
126 AUD_log (NULL
, "Context:\n");
132 static inline int audio_bits_to_index (int bits
)
145 audio_bug ("bits_to_index", 1);
146 AUD_log (NULL
, "invalid bits %d\n", bits
);
151 void *audio_calloc (const char *funcname
, int nmemb
, size_t size
)
157 cond
= !nmemb
|| !size
;
161 if (audio_bug ("audio_calloc", cond
)) {
162 AUD_log (NULL
, "%s passed invalid arguments to audio_calloc\n",
164 AUD_log (NULL
, "nmemb=%d size=%zu (len=%zu)\n", nmemb
, size
, len
);
168 return g_malloc0 (len
);
171 void AUD_vlog (const char *cap
, const char *fmt
, va_list ap
)
174 fprintf(stderr
, "%s: ", cap
);
177 vfprintf(stderr
, fmt
, ap
);
180 void AUD_log (const char *cap
, const char *fmt
, ...)
185 AUD_vlog (cap
, fmt
, ap
);
189 static void audio_print_settings (struct audsettings
*as
)
191 dolog ("frequency=%d nchannels=%d fmt=", as
->freq
, as
->nchannels
);
194 case AUDIO_FORMAT_S8
:
195 AUD_log (NULL
, "S8");
197 case AUDIO_FORMAT_U8
:
198 AUD_log (NULL
, "U8");
200 case AUDIO_FORMAT_S16
:
201 AUD_log (NULL
, "S16");
203 case AUDIO_FORMAT_U16
:
204 AUD_log (NULL
, "U16");
206 case AUDIO_FORMAT_S32
:
207 AUD_log (NULL
, "S32");
209 case AUDIO_FORMAT_U32
:
210 AUD_log (NULL
, "U32");
212 case AUDIO_FORMAT_F32
:
213 AUD_log (NULL
, "F32");
216 AUD_log (NULL
, "invalid(%d)", as
->fmt
);
220 AUD_log (NULL
, " endianness=");
221 switch (as
->endianness
) {
223 AUD_log (NULL
, "little");
226 AUD_log (NULL
, "big");
229 AUD_log (NULL
, "invalid");
232 AUD_log (NULL
, "\n");
235 static int audio_validate_settings (struct audsettings
*as
)
239 invalid
= as
->nchannels
< 1;
240 invalid
|= as
->endianness
!= 0 && as
->endianness
!= 1;
243 case AUDIO_FORMAT_S8
:
244 case AUDIO_FORMAT_U8
:
245 case AUDIO_FORMAT_S16
:
246 case AUDIO_FORMAT_U16
:
247 case AUDIO_FORMAT_S32
:
248 case AUDIO_FORMAT_U32
:
249 case AUDIO_FORMAT_F32
:
256 invalid
|= as
->freq
<= 0;
257 return invalid
? -1 : 0;
260 static int audio_pcm_info_eq (struct audio_pcm_info
*info
, struct audsettings
*as
)
263 bool is_signed
= false, is_float
= false;
266 case AUDIO_FORMAT_S8
:
269 case AUDIO_FORMAT_U8
:
272 case AUDIO_FORMAT_S16
:
275 case AUDIO_FORMAT_U16
:
279 case AUDIO_FORMAT_F32
:
282 case AUDIO_FORMAT_S32
:
285 case AUDIO_FORMAT_U32
:
292 return info
->freq
== as
->freq
293 && info
->nchannels
== as
->nchannels
294 && info
->is_signed
== is_signed
295 && info
->is_float
== is_float
296 && info
->bits
== bits
297 && info
->swap_endianness
== (as
->endianness
!= AUDIO_HOST_ENDIANNESS
);
300 void audio_pcm_init_info (struct audio_pcm_info
*info
, struct audsettings
*as
)
303 bool is_signed
= false, is_float
= false;
306 case AUDIO_FORMAT_S8
:
309 case AUDIO_FORMAT_U8
:
313 case AUDIO_FORMAT_S16
:
316 case AUDIO_FORMAT_U16
:
321 case AUDIO_FORMAT_F32
:
324 case AUDIO_FORMAT_S32
:
327 case AUDIO_FORMAT_U32
:
336 info
->freq
= as
->freq
;
338 info
->is_signed
= is_signed
;
339 info
->is_float
= is_float
;
340 info
->nchannels
= as
->nchannels
;
341 info
->bytes_per_frame
= as
->nchannels
* mul
;
342 info
->bytes_per_second
= info
->freq
* info
->bytes_per_frame
;
343 info
->swap_endianness
= (as
->endianness
!= AUDIO_HOST_ENDIANNESS
);
346 void audio_pcm_info_clear_buf (struct audio_pcm_info
*info
, void *buf
, int len
)
352 if (info
->is_signed
|| info
->is_float
) {
353 memset(buf
, 0x00, len
* info
->bytes_per_frame
);
355 switch (info
->bits
) {
357 memset(buf
, 0x80, len
* info
->bytes_per_frame
);
366 if (info
->swap_endianness
) {
370 for (i
= 0; i
< len
* info
->nchannels
; i
++) {
380 int32_t s
= INT32_MAX
;
382 if (info
->swap_endianness
) {
386 for (i
= 0; i
< len
* info
->nchannels
; i
++) {
393 AUD_log (NULL
, "audio_pcm_info_clear_buf: invalid bits %d\n",
403 static void noop_conv (struct st_sample
*dst
, const void *src
, int samples
)
410 static CaptureVoiceOut
*audio_pcm_capture_find_specific(AudioState
*s
,
411 struct audsettings
*as
)
413 CaptureVoiceOut
*cap
;
415 for (cap
= s
->cap_head
.lh_first
; cap
; cap
= cap
->entries
.le_next
) {
416 if (audio_pcm_info_eq (&cap
->hw
.info
, as
)) {
423 static void audio_notify_capture (CaptureVoiceOut
*cap
, audcnotification_e cmd
)
425 struct capture_callback
*cb
;
428 dolog ("notification %d sent\n", cmd
);
430 for (cb
= cap
->cb_head
.lh_first
; cb
; cb
= cb
->entries
.le_next
) {
431 cb
->ops
.notify (cb
->opaque
, cmd
);
435 static void audio_capture_maybe_changed (CaptureVoiceOut
*cap
, int enabled
)
437 if (cap
->hw
.enabled
!= enabled
) {
438 audcnotification_e cmd
;
439 cap
->hw
.enabled
= enabled
;
440 cmd
= enabled
? AUD_CNOTIFY_ENABLE
: AUD_CNOTIFY_DISABLE
;
441 audio_notify_capture (cap
, cmd
);
445 static void audio_recalc_and_notify_capture (CaptureVoiceOut
*cap
)
447 HWVoiceOut
*hw
= &cap
->hw
;
451 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
457 audio_capture_maybe_changed (cap
, enabled
);
460 static void audio_detach_capture (HWVoiceOut
*hw
)
462 SWVoiceCap
*sc
= hw
->cap_head
.lh_first
;
465 SWVoiceCap
*sc1
= sc
->entries
.le_next
;
466 SWVoiceOut
*sw
= &sc
->sw
;
467 CaptureVoiceOut
*cap
= sc
->cap
;
468 int was_active
= sw
->active
;
471 st_rate_stop (sw
->rate
);
475 QLIST_REMOVE (sw
, entries
);
476 QLIST_REMOVE (sc
, entries
);
479 /* We have removed soft voice from the capture:
480 this might have changed the overall status of the capture
481 since this might have been the only active voice */
482 audio_recalc_and_notify_capture (cap
);
488 static int audio_attach_capture (HWVoiceOut
*hw
)
490 AudioState
*s
= hw
->s
;
491 CaptureVoiceOut
*cap
;
493 audio_detach_capture (hw
);
494 for (cap
= s
->cap_head
.lh_first
; cap
; cap
= cap
->entries
.le_next
) {
497 HWVoiceOut
*hw_cap
= &cap
->hw
;
499 sc
= g_malloc0(sizeof(*sc
));
506 sw
->active
= hw
->enabled
;
507 sw
->conv
= noop_conv
;
508 sw
->ratio
= ((int64_t) hw_cap
->info
.freq
<< 32) / sw
->info
.freq
;
509 sw
->vol
= nominal_volume
;
510 sw
->rate
= st_rate_start (sw
->info
.freq
, hw_cap
->info
.freq
);
512 dolog ("Could not start rate conversion for `%s'\n", SW_NAME (sw
));
516 QLIST_INSERT_HEAD (&hw_cap
->sw_head
, sw
, entries
);
517 QLIST_INSERT_HEAD (&hw
->cap_head
, sc
, entries
);
519 sw
->name
= g_strdup_printf ("for %p %d,%d,%d",
520 hw
, sw
->info
.freq
, sw
->info
.bits
,
522 dolog ("Added %s active = %d\n", sw
->name
, sw
->active
);
525 audio_capture_maybe_changed (cap
, 1);
532 * Hard voice (capture)
534 static size_t audio_pcm_hw_find_min_in (HWVoiceIn
*hw
)
537 size_t m
= hw
->total_samples_captured
;
539 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
541 m
= MIN (m
, sw
->total_hw_samples_acquired
);
547 static size_t audio_pcm_hw_get_live_in(HWVoiceIn
*hw
)
549 size_t live
= hw
->total_samples_captured
- audio_pcm_hw_find_min_in (hw
);
550 if (audio_bug(__func__
, live
> hw
->conv_buf
->size
)) {
551 dolog("live=%zu hw->conv_buf->size=%zu\n", live
, hw
->conv_buf
->size
);
557 static size_t audio_pcm_hw_conv_in(HWVoiceIn
*hw
, void *pcm_buf
, size_t samples
)
560 STSampleBuffer
*conv_buf
= hw
->conv_buf
;
563 uint8_t *src
= advance(pcm_buf
, conv
* hw
->info
.bytes_per_frame
);
564 size_t proc
= MIN(samples
, conv_buf
->size
- conv_buf
->pos
);
566 hw
->conv(conv_buf
->samples
+ conv_buf
->pos
, src
, proc
);
567 conv_buf
->pos
= (conv_buf
->pos
+ proc
) % conv_buf
->size
;
576 * Soft voice (capture)
578 static size_t audio_pcm_sw_read(SWVoiceIn
*sw
, void *buf
, size_t size
)
580 HWVoiceIn
*hw
= sw
->hw
;
581 size_t samples
, live
, ret
= 0, swlim
, isamp
, osamp
, rpos
, total
= 0;
582 struct st_sample
*src
, *dst
= sw
->buf
;
584 live
= hw
->total_samples_captured
- sw
->total_hw_samples_acquired
;
588 if (audio_bug(__func__
, live
> hw
->conv_buf
->size
)) {
589 dolog("live_in=%zu hw->conv_buf->size=%zu\n", live
, hw
->conv_buf
->size
);
593 rpos
= audio_ring_posb(hw
->conv_buf
->pos
, live
, hw
->conv_buf
->size
);
595 samples
= size
/ sw
->info
.bytes_per_frame
;
597 swlim
= (live
* sw
->ratio
) >> 32;
598 swlim
= MIN (swlim
, samples
);
601 src
= hw
->conv_buf
->samples
+ rpos
;
602 if (hw
->conv_buf
->pos
> rpos
) {
603 isamp
= hw
->conv_buf
->pos
- rpos
;
605 isamp
= hw
->conv_buf
->size
- rpos
;
613 st_rate_flow (sw
->rate
, src
, dst
, &isamp
, &osamp
);
615 rpos
= (rpos
+ isamp
) % hw
->conv_buf
->size
;
621 if (!hw
->pcm_ops
->volume_in
) {
622 mixeng_volume (sw
->buf
, ret
, &sw
->vol
);
625 sw
->clip (buf
, sw
->buf
, ret
);
626 sw
->total_hw_samples_acquired
+= total
;
627 return ret
* sw
->info
.bytes_per_frame
;
631 * Hard voice (playback)
633 static size_t audio_pcm_hw_find_min_out (HWVoiceOut
*hw
, int *nb_livep
)
639 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
640 if (sw
->active
|| !sw
->empty
) {
641 m
= MIN (m
, sw
->total_hw_samples_mixed
);
650 static size_t audio_pcm_hw_get_live_out (HWVoiceOut
*hw
, int *nb_live
)
655 smin
= audio_pcm_hw_find_min_out (hw
, &nb_live1
);
663 if (audio_bug(__func__
, live
> hw
->mix_buf
->size
)) {
664 dolog("live=%zu hw->mix_buf->size=%zu\n", live
, hw
->mix_buf
->size
);
672 static size_t audio_pcm_hw_get_free(HWVoiceOut
*hw
)
674 return (hw
->pcm_ops
->buffer_get_free
? hw
->pcm_ops
->buffer_get_free(hw
) :
675 INT_MAX
) / hw
->info
.bytes_per_frame
;
678 static void audio_pcm_hw_clip_out(HWVoiceOut
*hw
, void *pcm_buf
, size_t len
)
681 size_t pos
= hw
->mix_buf
->pos
;
684 st_sample
*src
= hw
->mix_buf
->samples
+ pos
;
685 uint8_t *dst
= advance(pcm_buf
, clipped
* hw
->info
.bytes_per_frame
);
686 size_t samples_till_end_of_buf
= hw
->mix_buf
->size
- pos
;
687 size_t samples_to_clip
= MIN(len
, samples_till_end_of_buf
);
689 hw
->clip(dst
, src
, samples_to_clip
);
691 pos
= (pos
+ samples_to_clip
) % hw
->mix_buf
->size
;
692 len
-= samples_to_clip
;
693 clipped
+= samples_to_clip
;
698 * Soft voice (playback)
700 static size_t audio_pcm_sw_write(SWVoiceOut
*sw
, void *buf
, size_t size
)
702 size_t hwsamples
, samples
, isamp
, osamp
, wpos
, live
, dead
, left
, blck
;
704 size_t ret
= 0, pos
= 0, total
= 0;
710 hwsamples
= sw
->hw
->mix_buf
->size
;
712 live
= sw
->total_hw_samples_mixed
;
713 if (audio_bug(__func__
, live
> hwsamples
)) {
714 dolog("live=%zu hw->mix_buf->size=%zu\n", live
, hwsamples
);
718 if (live
== hwsamples
) {
720 dolog ("%s is full %zu\n", sw
->name
, live
);
725 wpos
= (sw
->hw
->mix_buf
->pos
+ live
) % hwsamples
;
727 dead
= hwsamples
- live
;
728 hw_free
= audio_pcm_hw_get_free(sw
->hw
);
729 hw_free
= hw_free
> live
? hw_free
- live
: 0;
730 samples
= ((int64_t)MIN(dead
, hw_free
) << 32) / sw
->ratio
;
731 samples
= MIN(samples
, size
/ sw
->info
.bytes_per_frame
);
733 sw
->conv(sw
->buf
, buf
, samples
);
735 if (!sw
->hw
->pcm_ops
->volume_out
) {
736 mixeng_volume(sw
->buf
, samples
, &sw
->vol
);
741 dead
= hwsamples
- live
;
742 left
= hwsamples
- wpos
;
743 blck
= MIN (dead
, left
);
752 sw
->hw
->mix_buf
->samples
+ wpos
,
760 wpos
= (wpos
+ osamp
) % hwsamples
;
764 sw
->total_hw_samples_mixed
+= total
;
765 sw
->empty
= sw
->total_hw_samples_mixed
== 0;
769 "%s: write size %zu ret %zu total sw %zu\n",
771 size
/ sw
->info
.bytes_per_frame
,
773 sw
->total_hw_samples_mixed
777 return ret
* sw
->info
.bytes_per_frame
;
781 static void audio_pcm_print_info (const char *cap
, struct audio_pcm_info
*info
)
783 dolog("%s: bits %d, sign %d, float %d, freq %d, nchan %d\n",
784 cap
, info
->bits
, info
->is_signed
, info
->is_float
, info
->freq
,
790 #include "audio_template.h"
792 #include "audio_template.h"
797 static int audio_is_timer_needed(AudioState
*s
)
799 HWVoiceIn
*hwi
= NULL
;
800 HWVoiceOut
*hwo
= NULL
;
802 while ((hwo
= audio_pcm_hw_find_any_enabled_out(s
, hwo
))) {
803 if (!hwo
->poll_mode
) {
807 while ((hwi
= audio_pcm_hw_find_any_enabled_in(s
, hwi
))) {
808 if (!hwi
->poll_mode
) {
815 static void audio_reset_timer (AudioState
*s
)
817 if (audio_is_timer_needed(s
)) {
818 timer_mod_anticipate_ns(s
->ts
,
819 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) + s
->period_ticks
);
820 if (!s
->timer_running
) {
821 s
->timer_running
= true;
822 s
->timer_last
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
823 trace_audio_timer_start(s
->period_ticks
/ SCALE_MS
);
827 if (s
->timer_running
) {
828 s
->timer_running
= false;
829 trace_audio_timer_stop();
834 static void audio_timer (void *opaque
)
837 AudioState
*s
= opaque
;
839 now
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
840 diff
= now
- s
->timer_last
;
841 if (diff
> s
->period_ticks
* 3 / 2) {
842 trace_audio_timer_delayed(diff
/ SCALE_MS
);
846 audio_run(s
, "timer");
847 audio_reset_timer(s
);
853 size_t AUD_write(SWVoiceOut
*sw
, void *buf
, size_t size
)
858 /* XXX: Consider options */
864 dolog ("Writing to disabled voice %s\n", SW_NAME (sw
));
868 if (audio_get_pdo_out(hw
->s
->dev
)->mixing_engine
) {
869 return audio_pcm_sw_write(sw
, buf
, size
);
871 return hw
->pcm_ops
->write(hw
, buf
, size
);
875 size_t AUD_read(SWVoiceIn
*sw
, void *buf
, size_t size
)
880 /* XXX: Consider options */
886 dolog ("Reading from disabled voice %s\n", SW_NAME (sw
));
890 if (audio_get_pdo_in(hw
->s
->dev
)->mixing_engine
) {
891 return audio_pcm_sw_read(sw
, buf
, size
);
893 return hw
->pcm_ops
->read(hw
, buf
, size
);
897 int AUD_get_buffer_size_out(SWVoiceOut
*sw
)
899 return sw
->hw
->samples
* sw
->hw
->info
.bytes_per_frame
;
902 void AUD_set_active_out (SWVoiceOut
*sw
, int on
)
911 if (sw
->active
!= on
) {
912 AudioState
*s
= sw
->s
;
917 hw
->pending_disable
= 0;
921 if (hw
->pcm_ops
->enable_out
) {
922 hw
->pcm_ops
->enable_out(hw
, true);
924 audio_reset_timer (s
);
931 for (temp_sw
= hw
->sw_head
.lh_first
; temp_sw
;
932 temp_sw
= temp_sw
->entries
.le_next
) {
933 nb_active
+= temp_sw
->active
!= 0;
936 hw
->pending_disable
= nb_active
== 1;
940 for (sc
= hw
->cap_head
.lh_first
; sc
; sc
= sc
->entries
.le_next
) {
941 sc
->sw
.active
= hw
->enabled
;
943 audio_capture_maybe_changed (sc
->cap
, 1);
950 void AUD_set_active_in (SWVoiceIn
*sw
, int on
)
959 if (sw
->active
!= on
) {
960 AudioState
*s
= sw
->s
;
967 if (hw
->pcm_ops
->enable_in
) {
968 hw
->pcm_ops
->enable_in(hw
, true);
970 audio_reset_timer (s
);
973 sw
->total_hw_samples_acquired
= hw
->total_samples_captured
;
978 for (temp_sw
= hw
->sw_head
.lh_first
; temp_sw
;
979 temp_sw
= temp_sw
->entries
.le_next
) {
980 nb_active
+= temp_sw
->active
!= 0;
983 if (nb_active
== 1) {
985 if (hw
->pcm_ops
->enable_in
) {
986 hw
->pcm_ops
->enable_in(hw
, false);
996 * audio_frontend_frames_in() - returns the number of frames the resampling
997 * code generates from frames_in frames
999 * @sw: audio recording frontend
1000 * @frames_in: number of frames
1002 static size_t audio_frontend_frames_in(SWVoiceIn
*sw
, size_t frames_in
)
1004 return (int64_t)frames_in
* sw
->ratio
>> 32;
1007 static size_t audio_get_avail (SWVoiceIn
*sw
)
1015 live
= sw
->hw
->total_samples_captured
- sw
->total_hw_samples_acquired
;
1016 if (audio_bug(__func__
, live
> sw
->hw
->conv_buf
->size
)) {
1017 dolog("live=%zu sw->hw->conv_buf->size=%zu\n", live
,
1018 sw
->hw
->conv_buf
->size
);
1023 "%s: get_avail live %zu frontend frames %zu\n",
1025 live
, audio_frontend_frames_in(sw
, live
)
1032 * audio_frontend_frames_out() - returns the number of frames needed to
1033 * get frames_out frames after resampling
1035 * @sw: audio playback frontend
1036 * @frames_out: number of frames
1038 static size_t audio_frontend_frames_out(SWVoiceOut
*sw
, size_t frames_out
)
1040 return ((int64_t)frames_out
<< 32) / sw
->ratio
;
1043 static size_t audio_get_free(SWVoiceOut
*sw
)
1051 live
= sw
->total_hw_samples_mixed
;
1053 if (audio_bug(__func__
, live
> sw
->hw
->mix_buf
->size
)) {
1054 dolog("live=%zu sw->hw->mix_buf->size=%zu\n", live
,
1055 sw
->hw
->mix_buf
->size
);
1059 dead
= sw
->hw
->mix_buf
->size
- live
;
1062 dolog("%s: get_free live %zu dead %zu frontend frames %zu\n",
1063 SW_NAME(sw
), live
, dead
, audio_frontend_frames_out(sw
, dead
));
1069 static void audio_capture_mix_and_clear(HWVoiceOut
*hw
, size_t rpos
,
1077 for (sc
= hw
->cap_head
.lh_first
; sc
; sc
= sc
->entries
.le_next
) {
1078 SWVoiceOut
*sw
= &sc
->sw
;
1083 size_t till_end_of_hw
= hw
->mix_buf
->size
- rpos2
;
1084 size_t to_write
= MIN(till_end_of_hw
, n
);
1085 size_t bytes
= to_write
* hw
->info
.bytes_per_frame
;
1088 sw
->buf
= hw
->mix_buf
->samples
+ rpos2
;
1089 written
= audio_pcm_sw_write (sw
, NULL
, bytes
);
1090 if (written
- bytes
) {
1091 dolog("Could not mix %zu bytes into a capture "
1092 "buffer, mixed %zu\n",
1097 rpos2
= (rpos2
+ to_write
) % hw
->mix_buf
->size
;
1102 n
= MIN(samples
, hw
->mix_buf
->size
- rpos
);
1103 mixeng_clear(hw
->mix_buf
->samples
+ rpos
, n
);
1104 mixeng_clear(hw
->mix_buf
->samples
, samples
- n
);
1107 static size_t audio_pcm_hw_run_out(HWVoiceOut
*hw
, size_t live
)
1112 size_t size
= live
* hw
->info
.bytes_per_frame
;
1114 void *buf
= hw
->pcm_ops
->get_buffer_out(hw
, &size
);
1120 decr
= MIN(size
/ hw
->info
.bytes_per_frame
, live
);
1122 audio_pcm_hw_clip_out(hw
, buf
, decr
);
1124 proc
= hw
->pcm_ops
->put_buffer_out(hw
, buf
,
1125 decr
* hw
->info
.bytes_per_frame
) /
1126 hw
->info
.bytes_per_frame
;
1130 hw
->mix_buf
->pos
= (hw
->mix_buf
->pos
+ proc
) % hw
->mix_buf
->size
;
1132 if (proc
== 0 || proc
< decr
) {
1137 if (hw
->pcm_ops
->run_buffer_out
) {
1138 hw
->pcm_ops
->run_buffer_out(hw
);
1144 static void audio_run_out (AudioState
*s
)
1146 HWVoiceOut
*hw
= NULL
;
1149 while ((hw
= audio_pcm_hw_find_any_enabled_out(s
, hw
))) {
1150 size_t played
, live
, prev_rpos
;
1151 size_t hw_free
= audio_pcm_hw_get_free(hw
);
1154 if (!audio_get_pdo_out(s
->dev
)->mixing_engine
) {
1155 /* there is exactly 1 sw for each hw with no mixeng */
1156 sw
= hw
->sw_head
.lh_first
;
1158 if (hw
->pending_disable
) {
1160 hw
->pending_disable
= 0;
1161 if (hw
->pcm_ops
->enable_out
) {
1162 hw
->pcm_ops
->enable_out(hw
, false);
1167 sw
->callback
.fn(sw
->callback
.opaque
,
1168 hw_free
* sw
->info
.bytes_per_frame
);
1171 if (hw
->pcm_ops
->run_buffer_out
) {
1172 hw
->pcm_ops
->run_buffer_out(hw
);
1178 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
1180 size_t sw_free
= audio_get_free(sw
);
1183 if (hw_free
> sw
->total_hw_samples_mixed
) {
1184 free
= audio_frontend_frames_out(sw
,
1185 MIN(sw_free
, hw_free
- sw
->total_hw_samples_mixed
));
1190 sw
->callback
.fn(sw
->callback
.opaque
,
1191 free
* sw
->info
.bytes_per_frame
);
1196 live
= audio_pcm_hw_get_live_out (hw
, &nb_live
);
1201 if (audio_bug(__func__
, live
> hw
->mix_buf
->size
)) {
1202 dolog("live=%zu hw->mix_buf->size=%zu\n", live
, hw
->mix_buf
->size
);
1206 if (hw
->pending_disable
&& !nb_live
) {
1209 dolog ("Disabling voice\n");
1212 hw
->pending_disable
= 0;
1213 if (hw
->pcm_ops
->enable_out
) {
1214 hw
->pcm_ops
->enable_out(hw
, false);
1216 for (sc
= hw
->cap_head
.lh_first
; sc
; sc
= sc
->entries
.le_next
) {
1218 audio_recalc_and_notify_capture (sc
->cap
);
1224 if (hw
->pcm_ops
->run_buffer_out
) {
1225 hw
->pcm_ops
->run_buffer_out(hw
);
1230 prev_rpos
= hw
->mix_buf
->pos
;
1231 played
= audio_pcm_hw_run_out(hw
, live
);
1232 replay_audio_out(&played
);
1233 if (audio_bug(__func__
, hw
->mix_buf
->pos
>= hw
->mix_buf
->size
)) {
1234 dolog("hw->mix_buf->pos=%zu hw->mix_buf->size=%zu played=%zu\n",
1235 hw
->mix_buf
->pos
, hw
->mix_buf
->size
, played
);
1236 hw
->mix_buf
->pos
= 0;
1240 dolog("played=%zu\n", played
);
1244 hw
->ts_helper
+= played
;
1245 audio_capture_mix_and_clear (hw
, prev_rpos
, played
);
1248 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
1249 if (!sw
->active
&& sw
->empty
) {
1253 if (audio_bug(__func__
, played
> sw
->total_hw_samples_mixed
)) {
1254 dolog("played=%zu sw->total_hw_samples_mixed=%zu\n",
1255 played
, sw
->total_hw_samples_mixed
);
1256 played
= sw
->total_hw_samples_mixed
;
1259 sw
->total_hw_samples_mixed
-= played
;
1261 if (!sw
->total_hw_samples_mixed
) {
1268 static size_t audio_pcm_hw_run_in(HWVoiceIn
*hw
, size_t samples
)
1272 if (hw
->pcm_ops
->run_buffer_in
) {
1273 hw
->pcm_ops
->run_buffer_in(hw
);
1278 size_t size
= samples
* hw
->info
.bytes_per_frame
;
1279 void *buf
= hw
->pcm_ops
->get_buffer_in(hw
, &size
);
1281 assert(size
% hw
->info
.bytes_per_frame
== 0);
1286 proc
= audio_pcm_hw_conv_in(hw
, buf
, size
/ hw
->info
.bytes_per_frame
);
1290 hw
->pcm_ops
->put_buffer_in(hw
, buf
, proc
* hw
->info
.bytes_per_frame
);
1296 static void audio_run_in (AudioState
*s
)
1298 HWVoiceIn
*hw
= NULL
;
1300 if (!audio_get_pdo_in(s
->dev
)->mixing_engine
) {
1301 while ((hw
= audio_pcm_hw_find_any_enabled_in(s
, hw
))) {
1302 /* there is exactly 1 sw for each hw with no mixeng */
1303 SWVoiceIn
*sw
= hw
->sw_head
.lh_first
;
1305 sw
->callback
.fn(sw
->callback
.opaque
, INT_MAX
);
1311 while ((hw
= audio_pcm_hw_find_any_enabled_in(s
, hw
))) {
1313 size_t captured
= 0, min
;
1315 if (replay_mode
!= REPLAY_MODE_PLAY
) {
1316 captured
= audio_pcm_hw_run_in(
1317 hw
, hw
->conv_buf
->size
- audio_pcm_hw_get_live_in(hw
));
1319 replay_audio_in(&captured
, hw
->conv_buf
->samples
, &hw
->conv_buf
->pos
,
1320 hw
->conv_buf
->size
);
1322 min
= audio_pcm_hw_find_min_in (hw
);
1323 hw
->total_samples_captured
+= captured
- min
;
1324 hw
->ts_helper
+= captured
;
1326 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
1327 sw
->total_hw_samples_acquired
-= min
;
1330 size_t sw_avail
= audio_get_avail(sw
);
1333 avail
= audio_frontend_frames_in(sw
, sw_avail
);
1335 sw
->callback
.fn(sw
->callback
.opaque
,
1336 avail
* sw
->info
.bytes_per_frame
);
1343 static void audio_run_capture (AudioState
*s
)
1345 CaptureVoiceOut
*cap
;
1347 for (cap
= s
->cap_head
.lh_first
; cap
; cap
= cap
->entries
.le_next
) {
1348 size_t live
, rpos
, captured
;
1349 HWVoiceOut
*hw
= &cap
->hw
;
1352 captured
= live
= audio_pcm_hw_get_live_out (hw
, NULL
);
1353 rpos
= hw
->mix_buf
->pos
;
1355 size_t left
= hw
->mix_buf
->size
- rpos
;
1356 size_t to_capture
= MIN(live
, left
);
1357 struct st_sample
*src
;
1358 struct capture_callback
*cb
;
1360 src
= hw
->mix_buf
->samples
+ rpos
;
1361 hw
->clip (cap
->buf
, src
, to_capture
);
1362 mixeng_clear (src
, to_capture
);
1364 for (cb
= cap
->cb_head
.lh_first
; cb
; cb
= cb
->entries
.le_next
) {
1365 cb
->ops
.capture (cb
->opaque
, cap
->buf
,
1366 to_capture
* hw
->info
.bytes_per_frame
);
1368 rpos
= (rpos
+ to_capture
) % hw
->mix_buf
->size
;
1371 hw
->mix_buf
->pos
= rpos
;
1373 for (sw
= hw
->sw_head
.lh_first
; sw
; sw
= sw
->entries
.le_next
) {
1374 if (!sw
->active
&& sw
->empty
) {
1378 if (audio_bug(__func__
, captured
> sw
->total_hw_samples_mixed
)) {
1379 dolog("captured=%zu sw->total_hw_samples_mixed=%zu\n",
1380 captured
, sw
->total_hw_samples_mixed
);
1381 captured
= sw
->total_hw_samples_mixed
;
1384 sw
->total_hw_samples_mixed
-= captured
;
1385 sw
->empty
= sw
->total_hw_samples_mixed
== 0;
1390 void audio_run(AudioState
*s
, const char *msg
)
1394 audio_run_capture(s
);
1398 static double prevtime
;
1402 if (gettimeofday (&tv
, NULL
)) {
1403 perror ("audio_run: gettimeofday");
1407 currtime
= tv
.tv_sec
+ tv
.tv_usec
* 1e-6;
1408 dolog ("Elapsed since last %s: %f\n", msg
, currtime
- prevtime
);
1409 prevtime
= currtime
;
1414 void audio_generic_run_buffer_in(HWVoiceIn
*hw
)
1416 if (unlikely(!hw
->buf_emul
)) {
1417 hw
->size_emul
= hw
->samples
* hw
->info
.bytes_per_frame
;
1418 hw
->buf_emul
= g_malloc(hw
->size_emul
);
1419 hw
->pos_emul
= hw
->pending_emul
= 0;
1422 while (hw
->pending_emul
< hw
->size_emul
) {
1423 size_t read_len
= MIN(hw
->size_emul
- hw
->pos_emul
,
1424 hw
->size_emul
- hw
->pending_emul
);
1425 size_t read
= hw
->pcm_ops
->read(hw
, hw
->buf_emul
+ hw
->pos_emul
,
1427 hw
->pending_emul
+= read
;
1428 hw
->pos_emul
= (hw
->pos_emul
+ read
) % hw
->size_emul
;
1429 if (read
< read_len
) {
1435 void *audio_generic_get_buffer_in(HWVoiceIn
*hw
, size_t *size
)
1439 start
= audio_ring_posb(hw
->pos_emul
, hw
->pending_emul
, hw
->size_emul
);
1440 assert(start
< hw
->size_emul
);
1442 *size
= MIN(*size
, hw
->pending_emul
);
1443 *size
= MIN(*size
, hw
->size_emul
- start
);
1444 return hw
->buf_emul
+ start
;
1447 void audio_generic_put_buffer_in(HWVoiceIn
*hw
, void *buf
, size_t size
)
1449 assert(size
<= hw
->pending_emul
);
1450 hw
->pending_emul
-= size
;
1453 size_t audio_generic_buffer_get_free(HWVoiceOut
*hw
)
1456 return hw
->size_emul
- hw
->pending_emul
;
1458 return hw
->samples
* hw
->info
.bytes_per_frame
;
1462 void audio_generic_run_buffer_out(HWVoiceOut
*hw
)
1464 while (hw
->pending_emul
) {
1465 size_t write_len
, written
, start
;
1467 start
= audio_ring_posb(hw
->pos_emul
, hw
->pending_emul
, hw
->size_emul
);
1468 assert(start
< hw
->size_emul
);
1470 write_len
= MIN(hw
->pending_emul
, hw
->size_emul
- start
);
1472 written
= hw
->pcm_ops
->write(hw
, hw
->buf_emul
+ start
, write_len
);
1473 hw
->pending_emul
-= written
;
1475 if (written
< write_len
) {
1481 void *audio_generic_get_buffer_out(HWVoiceOut
*hw
, size_t *size
)
1483 if (unlikely(!hw
->buf_emul
)) {
1484 hw
->size_emul
= hw
->samples
* hw
->info
.bytes_per_frame
;
1485 hw
->buf_emul
= g_malloc(hw
->size_emul
);
1486 hw
->pos_emul
= hw
->pending_emul
= 0;
1489 *size
= MIN(hw
->size_emul
- hw
->pending_emul
,
1490 hw
->size_emul
- hw
->pos_emul
);
1491 return hw
->buf_emul
+ hw
->pos_emul
;
1494 size_t audio_generic_put_buffer_out(HWVoiceOut
*hw
, void *buf
, size_t size
)
1496 assert(buf
== hw
->buf_emul
+ hw
->pos_emul
&&
1497 size
+ hw
->pending_emul
<= hw
->size_emul
);
1499 hw
->pending_emul
+= size
;
1500 hw
->pos_emul
= (hw
->pos_emul
+ size
) % hw
->size_emul
;
1505 size_t audio_generic_write(HWVoiceOut
*hw
, void *buf
, size_t size
)
1509 if (hw
->pcm_ops
->buffer_get_free
) {
1510 size_t free
= hw
->pcm_ops
->buffer_get_free(hw
);
1512 size
= MIN(size
, free
);
1515 while (total
< size
) {
1516 size_t dst_size
= size
- total
;
1517 size_t copy_size
, proc
;
1518 void *dst
= hw
->pcm_ops
->get_buffer_out(hw
, &dst_size
);
1520 if (dst_size
== 0) {
1524 copy_size
= MIN(size
- total
, dst_size
);
1526 memcpy(dst
, (char *)buf
+ total
, copy_size
);
1528 proc
= hw
->pcm_ops
->put_buffer_out(hw
, dst
, copy_size
);
1531 if (proc
== 0 || proc
< copy_size
) {
1539 size_t audio_generic_read(HWVoiceIn
*hw
, void *buf
, size_t size
)
1543 if (hw
->pcm_ops
->run_buffer_in
) {
1544 hw
->pcm_ops
->run_buffer_in(hw
);
1547 while (total
< size
) {
1548 size_t src_size
= size
- total
;
1549 void *src
= hw
->pcm_ops
->get_buffer_in(hw
, &src_size
);
1551 if (src_size
== 0) {
1555 memcpy((char *)buf
+ total
, src
, src_size
);
1556 hw
->pcm_ops
->put_buffer_in(hw
, src
, src_size
);
1563 static int audio_driver_init(AudioState
*s
, struct audio_driver
*drv
,
1564 bool msg
, Audiodev
*dev
)
1566 s
->drv_opaque
= drv
->init(dev
);
1568 if (s
->drv_opaque
) {
1569 if (!drv
->pcm_ops
->get_buffer_in
) {
1570 drv
->pcm_ops
->get_buffer_in
= audio_generic_get_buffer_in
;
1571 drv
->pcm_ops
->put_buffer_in
= audio_generic_put_buffer_in
;
1573 if (!drv
->pcm_ops
->get_buffer_out
) {
1574 drv
->pcm_ops
->get_buffer_out
= audio_generic_get_buffer_out
;
1575 drv
->pcm_ops
->put_buffer_out
= audio_generic_put_buffer_out
;
1578 audio_init_nb_voices_out(s
, drv
);
1579 audio_init_nb_voices_in(s
, drv
);
1584 dolog("Could not init `%s' audio driver\n", drv
->name
);
1590 static void audio_vm_change_state_handler (void *opaque
, bool running
,
1593 AudioState
*s
= opaque
;
1594 HWVoiceOut
*hwo
= NULL
;
1595 HWVoiceIn
*hwi
= NULL
;
1597 s
->vm_running
= running
;
1598 while ((hwo
= audio_pcm_hw_find_any_enabled_out(s
, hwo
))) {
1599 if (hwo
->pcm_ops
->enable_out
) {
1600 hwo
->pcm_ops
->enable_out(hwo
, running
);
1604 while ((hwi
= audio_pcm_hw_find_any_enabled_in(s
, hwi
))) {
1605 if (hwi
->pcm_ops
->enable_in
) {
1606 hwi
->pcm_ops
->enable_in(hwi
, running
);
1609 audio_reset_timer (s
);
1612 static void free_audio_state(AudioState
*s
)
1614 HWVoiceOut
*hwo
, *hwon
;
1615 HWVoiceIn
*hwi
, *hwin
;
1617 QLIST_FOREACH_SAFE(hwo
, &s
->hw_head_out
, entries
, hwon
) {
1620 if (hwo
->enabled
&& hwo
->pcm_ops
->enable_out
) {
1621 hwo
->pcm_ops
->enable_out(hwo
, false);
1623 hwo
->pcm_ops
->fini_out (hwo
);
1625 for (sc
= hwo
->cap_head
.lh_first
; sc
; sc
= sc
->entries
.le_next
) {
1626 CaptureVoiceOut
*cap
= sc
->cap
;
1627 struct capture_callback
*cb
;
1629 for (cb
= cap
->cb_head
.lh_first
; cb
; cb
= cb
->entries
.le_next
) {
1630 cb
->ops
.destroy (cb
->opaque
);
1633 QLIST_REMOVE(hwo
, entries
);
1636 QLIST_FOREACH_SAFE(hwi
, &s
->hw_head_in
, entries
, hwin
) {
1637 if (hwi
->enabled
&& hwi
->pcm_ops
->enable_in
) {
1638 hwi
->pcm_ops
->enable_in(hwi
, false);
1640 hwi
->pcm_ops
->fini_in (hwi
);
1641 QLIST_REMOVE(hwi
, entries
);
1645 s
->drv
->fini (s
->drv_opaque
);
1650 qapi_free_Audiodev(s
->dev
);
1662 void audio_cleanup(void)
1664 while (!QTAILQ_EMPTY(&audio_states
)) {
1665 AudioState
*s
= QTAILQ_FIRST(&audio_states
);
1666 QTAILQ_REMOVE(&audio_states
, s
, list
);
1667 free_audio_state(s
);
1671 static bool vmstate_audio_needed(void *opaque
)
1674 * Never needed, this vmstate only exists in case
1675 * an old qemu sends it to us.
1680 static const VMStateDescription vmstate_audio
= {
1683 .minimum_version_id
= 1,
1684 .needed
= vmstate_audio_needed
,
1685 .fields
= (VMStateField
[]) {
1686 VMSTATE_END_OF_LIST()
1690 static void audio_validate_opts(Audiodev
*dev
, Error
**errp
);
1692 static AudiodevListEntry
*audiodev_find(
1693 AudiodevListHead
*head
, const char *drvname
)
1695 AudiodevListEntry
*e
;
1696 QSIMPLEQ_FOREACH(e
, head
, next
) {
1697 if (strcmp(AudiodevDriver_str(e
->dev
->driver
), drvname
) == 0) {
1706 * if we have dev, this function was called because of an -audiodev argument =>
1707 * initialize a new state with it
1708 * if dev == NULL => legacy implicit initialization, return the already created
1709 * state or create a new one
1711 static AudioState
*audio_init(Audiodev
*dev
, const char *name
)
1713 static bool atexit_registered
;
1716 const char *drvname
= NULL
;
1717 VMChangeStateEntry
*e
;
1719 struct audio_driver
*driver
;
1720 /* silence gcc warning about uninitialized variable */
1721 AudiodevListHead head
= QSIMPLEQ_HEAD_INITIALIZER(head
);
1725 * When using spice allow the spice audio driver being picked
1728 * Temporary hack. Using audio devices without explicit
1729 * audiodev= property is already deprecated. Same goes for
1730 * the -soundhw switch. Once this support gets finally
1731 * removed we can also drop the concept of a default audio
1732 * backend and this can go away.
1734 driver
= audio_driver_lookup("spice");
1736 driver
->can_be_default
= 1;
1741 /* -audiodev option */
1742 legacy_config
= false;
1743 drvname
= AudiodevDriver_str(dev
->driver
);
1744 } else if (!QTAILQ_EMPTY(&audio_states
)) {
1745 if (!legacy_config
) {
1746 dolog("Device %s: audiodev default parameter is deprecated, please "
1747 "specify audiodev=%s\n", name
,
1748 QTAILQ_FIRST(&audio_states
)->dev
->id
);
1750 return QTAILQ_FIRST(&audio_states
);
1752 /* legacy implicit initialization */
1753 head
= audio_handle_legacy_opts();
1755 * In case of legacy initialization, all Audiodevs in the list will have
1756 * the same configuration (except the driver), so it doesn't matter which
1757 * one we chose. We need an Audiodev to set up AudioState before we can
1758 * init a driver. Also note that dev at this point is still in the
1761 dev
= QSIMPLEQ_FIRST(&head
)->dev
;
1762 audio_validate_opts(dev
, &error_abort
);
1765 s
= g_new0(AudioState
, 1);
1768 QLIST_INIT (&s
->hw_head_out
);
1769 QLIST_INIT (&s
->hw_head_in
);
1770 QLIST_INIT (&s
->cap_head
);
1771 if (!atexit_registered
) {
1772 atexit(audio_cleanup
);
1773 atexit_registered
= true;
1776 s
->ts
= timer_new_ns(QEMU_CLOCK_VIRTUAL
, audio_timer
, s
);
1778 s
->nb_hw_voices_out
= audio_get_pdo_out(dev
)->voices
;
1779 s
->nb_hw_voices_in
= audio_get_pdo_in(dev
)->voices
;
1781 if (s
->nb_hw_voices_out
< 1) {
1782 dolog ("Bogus number of playback voices %d, setting to 1\n",
1783 s
->nb_hw_voices_out
);
1784 s
->nb_hw_voices_out
= 1;
1787 if (s
->nb_hw_voices_in
< 0) {
1788 dolog ("Bogus number of capture voices %d, setting to 0\n",
1789 s
->nb_hw_voices_in
);
1790 s
->nb_hw_voices_in
= 0;
1794 driver
= audio_driver_lookup(drvname
);
1796 done
= !audio_driver_init(s
, driver
, true, dev
);
1798 dolog ("Unknown audio driver `%s'\n", drvname
);
1801 free_audio_state(s
);
1805 for (i
= 0; audio_prio_list
[i
]; i
++) {
1806 AudiodevListEntry
*e
= audiodev_find(&head
, audio_prio_list
[i
]);
1807 driver
= audio_driver_lookup(audio_prio_list
[i
]);
1810 s
->dev
= dev
= e
->dev
;
1811 audio_validate_opts(dev
, &error_abort
);
1812 done
= !audio_driver_init(s
, driver
, false, dev
);
1820 audio_free_audiodev_list(&head
);
1823 driver
= audio_driver_lookup("none");
1824 done
= !audio_driver_init(s
, driver
, false, dev
);
1826 dolog("warning: Using timer based audio emulation\n");
1829 if (dev
->timer_period
<= 0) {
1830 s
->period_ticks
= 1;
1832 s
->period_ticks
= dev
->timer_period
* (int64_t)SCALE_US
;
1835 e
= qemu_add_vm_change_state_handler (audio_vm_change_state_handler
, s
);
1837 dolog ("warning: Could not register change state handler\n"
1838 "(Audio can continue looping even after stopping the VM)\n");
1841 QTAILQ_INSERT_TAIL(&audio_states
, s
, list
);
1842 QLIST_INIT (&s
->card_head
);
1843 vmstate_register (NULL
, 0, &vmstate_audio
, s
);
1847 void audio_free_audiodev_list(AudiodevListHead
*head
)
1849 AudiodevListEntry
*e
;
1850 while ((e
= QSIMPLEQ_FIRST(head
))) {
1851 QSIMPLEQ_REMOVE_HEAD(head
, next
);
1852 qapi_free_Audiodev(e
->dev
);
1857 void AUD_register_card (const char *name
, QEMUSoundCard
*card
)
1860 card
->state
= audio_init(NULL
, name
);
1863 card
->name
= g_strdup (name
);
1864 memset (&card
->entries
, 0, sizeof (card
->entries
));
1865 QLIST_INSERT_HEAD(&card
->state
->card_head
, card
, entries
);
1868 void AUD_remove_card (QEMUSoundCard
*card
)
1870 QLIST_REMOVE (card
, entries
);
1871 g_free (card
->name
);
1874 static struct audio_pcm_ops capture_pcm_ops
;
1876 CaptureVoiceOut
*AUD_add_capture(
1878 struct audsettings
*as
,
1879 struct audio_capture_ops
*ops
,
1883 CaptureVoiceOut
*cap
;
1884 struct capture_callback
*cb
;
1887 if (!legacy_config
) {
1888 dolog("Capturing without setting an audiodev is deprecated\n");
1890 s
= audio_init(NULL
, NULL
);
1893 if (!audio_get_pdo_out(s
->dev
)->mixing_engine
) {
1894 dolog("Can't capture with mixeng disabled\n");
1898 if (audio_validate_settings (as
)) {
1899 dolog ("Invalid settings were passed when trying to add capture\n");
1900 audio_print_settings (as
);
1904 cb
= g_malloc0(sizeof(*cb
));
1906 cb
->opaque
= cb_opaque
;
1908 cap
= audio_pcm_capture_find_specific(s
, as
);
1910 QLIST_INSERT_HEAD (&cap
->cb_head
, cb
, entries
);
1914 CaptureVoiceOut
*cap
;
1916 cap
= g_malloc0(sizeof(*cap
));
1920 hw
->pcm_ops
= &capture_pcm_ops
;
1921 QLIST_INIT (&hw
->sw_head
);
1922 QLIST_INIT (&cap
->cb_head
);
1924 /* XXX find a more elegant way */
1925 hw
->samples
= 4096 * 4;
1926 audio_pcm_hw_alloc_resources_out(hw
);
1928 audio_pcm_init_info (&hw
->info
, as
);
1930 cap
->buf
= g_malloc0_n(hw
->mix_buf
->size
, hw
->info
.bytes_per_frame
);
1932 if (hw
->info
.is_float
) {
1933 hw
->clip
= mixeng_clip_float
[hw
->info
.nchannels
== 2];
1935 hw
->clip
= mixeng_clip
1936 [hw
->info
.nchannels
== 2]
1937 [hw
->info
.is_signed
]
1938 [hw
->info
.swap_endianness
]
1939 [audio_bits_to_index(hw
->info
.bits
)];
1942 QLIST_INSERT_HEAD (&s
->cap_head
, cap
, entries
);
1943 QLIST_INSERT_HEAD (&cap
->cb_head
, cb
, entries
);
1945 QLIST_FOREACH(hw
, &s
->hw_head_out
, entries
) {
1946 audio_attach_capture (hw
);
1952 void AUD_del_capture (CaptureVoiceOut
*cap
, void *cb_opaque
)
1954 struct capture_callback
*cb
;
1956 for (cb
= cap
->cb_head
.lh_first
; cb
; cb
= cb
->entries
.le_next
) {
1957 if (cb
->opaque
== cb_opaque
) {
1958 cb
->ops
.destroy (cb_opaque
);
1959 QLIST_REMOVE (cb
, entries
);
1962 if (!cap
->cb_head
.lh_first
) {
1963 SWVoiceOut
*sw
= cap
->hw
.sw_head
.lh_first
, *sw1
;
1966 SWVoiceCap
*sc
= (SWVoiceCap
*) sw
;
1967 #ifdef DEBUG_CAPTURE
1968 dolog ("freeing %s\n", sw
->name
);
1971 sw1
= sw
->entries
.le_next
;
1973 st_rate_stop (sw
->rate
);
1976 QLIST_REMOVE (sw
, entries
);
1977 QLIST_REMOVE (sc
, entries
);
1981 QLIST_REMOVE (cap
, entries
);
1982 g_free (cap
->hw
.mix_buf
);
1991 void AUD_set_volume_out (SWVoiceOut
*sw
, int mute
, uint8_t lvol
, uint8_t rvol
)
1993 Volume vol
= { .mute
= mute
, .channels
= 2, .vol
= { lvol
, rvol
} };
1994 audio_set_volume_out(sw
, &vol
);
1997 void audio_set_volume_out(SWVoiceOut
*sw
, Volume
*vol
)
2000 HWVoiceOut
*hw
= sw
->hw
;
2002 sw
->vol
.mute
= vol
->mute
;
2003 sw
->vol
.l
= nominal_volume
.l
* vol
->vol
[0] / 255;
2004 sw
->vol
.r
= nominal_volume
.l
* vol
->vol
[vol
->channels
> 1 ? 1 : 0] /
2007 if (hw
->pcm_ops
->volume_out
) {
2008 hw
->pcm_ops
->volume_out(hw
, vol
);
2013 void AUD_set_volume_in (SWVoiceIn
*sw
, int mute
, uint8_t lvol
, uint8_t rvol
)
2015 Volume vol
= { .mute
= mute
, .channels
= 2, .vol
= { lvol
, rvol
} };
2016 audio_set_volume_in(sw
, &vol
);
2019 void audio_set_volume_in(SWVoiceIn
*sw
, Volume
*vol
)
2022 HWVoiceIn
*hw
= sw
->hw
;
2024 sw
->vol
.mute
= vol
->mute
;
2025 sw
->vol
.l
= nominal_volume
.l
* vol
->vol
[0] / 255;
2026 sw
->vol
.r
= nominal_volume
.r
* vol
->vol
[vol
->channels
> 1 ? 1 : 0] /
2029 if (hw
->pcm_ops
->volume_in
) {
2030 hw
->pcm_ops
->volume_in(hw
, vol
);
2035 void audio_create_pdos(Audiodev
*dev
)
2037 switch (dev
->driver
) {
2038 #define CASE(DRIVER, driver, pdo_name) \
2039 case AUDIODEV_DRIVER_##DRIVER: \
2040 if (!dev->u.driver.in) { \
2041 dev->u.driver.in = g_malloc0( \
2042 sizeof(Audiodev##pdo_name##PerDirectionOptions)); \
2044 if (!dev->u.driver.out) { \
2045 dev->u.driver.out = g_malloc0( \
2046 sizeof(Audiodev##pdo_name##PerDirectionOptions)); \
2051 #ifdef CONFIG_AUDIO_ALSA
2052 CASE(ALSA
, alsa
, Alsa
);
2054 #ifdef CONFIG_AUDIO_COREAUDIO
2055 CASE(COREAUDIO
, coreaudio
, Coreaudio
);
2057 #ifdef CONFIG_DBUS_DISPLAY
2060 #ifdef CONFIG_AUDIO_DSOUND
2061 CASE(DSOUND
, dsound
, );
2063 #ifdef CONFIG_AUDIO_JACK
2064 CASE(JACK
, jack
, Jack
);
2066 #ifdef CONFIG_AUDIO_OSS
2067 CASE(OSS
, oss
, Oss
);
2069 #ifdef CONFIG_AUDIO_PA
2072 #ifdef CONFIG_AUDIO_SDL
2073 CASE(SDL
, sdl
, Sdl
);
2075 #ifdef CONFIG_AUDIO_SNDIO
2076 CASE(SNDIO
, sndio
, );
2079 CASE(SPICE
, spice
, );
2083 case AUDIODEV_DRIVER__MAX
:
2088 static void audio_validate_per_direction_opts(
2089 AudiodevPerDirectionOptions
*pdo
, Error
**errp
)
2091 if (!pdo
->has_mixing_engine
) {
2092 pdo
->has_mixing_engine
= true;
2093 pdo
->mixing_engine
= true;
2095 if (!pdo
->has_fixed_settings
) {
2096 pdo
->has_fixed_settings
= true;
2097 pdo
->fixed_settings
= pdo
->mixing_engine
;
2099 if (!pdo
->fixed_settings
&&
2100 (pdo
->has_frequency
|| pdo
->has_channels
|| pdo
->has_format
)) {
2102 "You can't use frequency, channels or format with fixed-settings=off");
2105 if (!pdo
->mixing_engine
&& pdo
->fixed_settings
) {
2106 error_setg(errp
, "You can't use fixed-settings without mixeng");
2110 if (!pdo
->has_frequency
) {
2111 pdo
->has_frequency
= true;
2112 pdo
->frequency
= 44100;
2114 if (!pdo
->has_channels
) {
2115 pdo
->has_channels
= true;
2118 if (!pdo
->has_voices
) {
2119 pdo
->has_voices
= true;
2120 pdo
->voices
= pdo
->mixing_engine
? 1 : INT_MAX
;
2122 if (!pdo
->has_format
) {
2123 pdo
->has_format
= true;
2124 pdo
->format
= AUDIO_FORMAT_S16
;
2128 static void audio_validate_opts(Audiodev
*dev
, Error
**errp
)
2132 audio_create_pdos(dev
);
2134 audio_validate_per_direction_opts(audio_get_pdo_in(dev
), &err
);
2136 error_propagate(errp
, err
);
2140 audio_validate_per_direction_opts(audio_get_pdo_out(dev
), &err
);
2142 error_propagate(errp
, err
);
2146 if (!dev
->has_timer_period
) {
2147 dev
->has_timer_period
= true;
2148 dev
->timer_period
= 10000; /* 100Hz -> 10ms */
2152 void audio_help(void)
2156 printf("Available audio drivers:\n");
2158 for (i
= 0; i
< AUDIODEV_DRIVER__MAX
; i
++) {
2159 audio_driver
*driver
= audio_driver_lookup(AudiodevDriver_str(i
));
2161 printf("%s\n", driver
->name
);
2166 void audio_parse_option(const char *opt
)
2168 Audiodev
*dev
= NULL
;
2170 if (is_help_option(opt
)) {
2174 Visitor
*v
= qobject_input_visitor_new_str(opt
, "driver", &error_fatal
);
2175 visit_type_Audiodev(v
, NULL
, &dev
, &error_fatal
);
2181 void audio_define(Audiodev
*dev
)
2183 AudiodevListEntry
*e
;
2185 audio_validate_opts(dev
, &error_fatal
);
2187 e
= g_new0(AudiodevListEntry
, 1);
2189 QSIMPLEQ_INSERT_TAIL(&audiodevs
, e
, next
);
2192 bool audio_init_audiodevs(void)
2194 AudiodevListEntry
*e
;
2196 QSIMPLEQ_FOREACH(e
, &audiodevs
, next
) {
2197 if (!audio_init(e
->dev
, NULL
)) {
2205 audsettings
audiodev_to_audsettings(AudiodevPerDirectionOptions
*pdo
)
2207 return (audsettings
) {
2208 .freq
= pdo
->frequency
,
2209 .nchannels
= pdo
->channels
,
2211 .endianness
= AUDIO_HOST_ENDIANNESS
,
2215 int audioformat_bytes_per_sample(AudioFormat fmt
)
2218 case AUDIO_FORMAT_U8
:
2219 case AUDIO_FORMAT_S8
:
2222 case AUDIO_FORMAT_U16
:
2223 case AUDIO_FORMAT_S16
:
2226 case AUDIO_FORMAT_U32
:
2227 case AUDIO_FORMAT_S32
:
2228 case AUDIO_FORMAT_F32
:
2231 case AUDIO_FORMAT__MAX
:
2238 /* frames = freq * usec / 1e6 */
2239 int audio_buffer_frames(AudiodevPerDirectionOptions
*pdo
,
2240 audsettings
*as
, int def_usecs
)
2242 uint64_t usecs
= pdo
->has_buffer_length
? pdo
->buffer_length
: def_usecs
;
2243 return (as
->freq
* usecs
+ 500000) / 1000000;
2246 /* samples = channels * frames = channels * freq * usec / 1e6 */
2247 int audio_buffer_samples(AudiodevPerDirectionOptions
*pdo
,
2248 audsettings
*as
, int def_usecs
)
2250 return as
->nchannels
* audio_buffer_frames(pdo
, as
, def_usecs
);
2254 * bytes = bytes_per_sample * samples =
2255 * bytes_per_sample * channels * freq * usec / 1e6
2257 int audio_buffer_bytes(AudiodevPerDirectionOptions
*pdo
,
2258 audsettings
*as
, int def_usecs
)
2260 return audio_buffer_samples(pdo
, as
, def_usecs
) *
2261 audioformat_bytes_per_sample(as
->fmt
);
2264 AudioState
*audio_state_by_name(const char *name
)
2267 QTAILQ_FOREACH(s
, &audio_states
, list
) {
2269 if (strcmp(name
, s
->dev
->id
) == 0) {
2276 const char *audio_get_id(QEMUSoundCard
*card
)
2279 assert(card
->state
->dev
);
2280 return card
->state
->dev
->id
;
2286 const char *audio_application_name(void)
2288 const char *vm_name
;
2290 vm_name
= qemu_get_vm_name();
2291 return vm_name
? vm_name
: "qemu";
2294 void audio_rate_start(RateCtl
*rate
)
2296 memset(rate
, 0, sizeof(RateCtl
));
2297 rate
->start_ticks
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
2300 size_t audio_rate_peek_bytes(RateCtl
*rate
, struct audio_pcm_info
*info
)
2307 now
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
2308 ticks
= now
- rate
->start_ticks
;
2309 bytes
= muldiv64(ticks
, info
->bytes_per_second
, NANOSECONDS_PER_SECOND
);
2310 frames
= (bytes
- rate
->bytes_sent
) / info
->bytes_per_frame
;
2311 if (frames
< 0 || frames
> 65536) {
2312 AUD_log(NULL
, "Resetting rate control (%" PRId64
" frames)\n", frames
);
2313 audio_rate_start(rate
);
2317 return frames
* info
->bytes_per_frame
;
2320 void audio_rate_add_bytes(RateCtl
*rate
, size_t bytes_used
)
2322 rate
->bytes_sent
+= bytes_used
;
2325 size_t audio_rate_get_bytes(RateCtl
*rate
, struct audio_pcm_info
*info
,
2330 bytes
= audio_rate_peek_bytes(rate
, info
);
2331 bytes
= MIN(bytes
, bytes_avail
);
2332 audio_rate_add_bytes(rate
, bytes
);
2337 AudiodevList
*qmp_query_audiodevs(Error
**errp
)
2339 AudiodevList
*ret
= NULL
;
2340 AudiodevListEntry
*e
;
2341 QSIMPLEQ_FOREACH(e
, &audiodevs
, next
) {
2342 QAPI_LIST_PREPEND(ret
, QAPI_CLONE(Audiodev
, e
->dev
));