From bd56d378842c238c8901536c06c20a4a51ee9761 Mon Sep 17 00:00:00 2001 From: Gerd Hoffmann Date: Thu, 24 Jan 2019 12:08:10 +0100 Subject: [PATCH 1/7] audio: fix pc speaker init MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Get rid of the pcspk_state global, allow pc speaker be added using "-device isa-pcspk". Signed-off-by: Gerd Hoffmann Reviewed-by: Philippe Mathieu-Daudé Message-id: 20190124110810.1040-1-kraxel@redhat.com --- hw/audio/pcspk.c | 35 +++++++++++++++-------------------- 1 file changed, 15 insertions(+), 20 deletions(-) diff --git a/hw/audio/pcspk.c b/hw/audio/pcspk.c index 908696d483..b80a62ce90 100644 --- a/hw/audio/pcspk.c +++ b/hw/audio/pcspk.c @@ -57,7 +57,6 @@ typedef struct { } PCSpkState; static const char *s_spk = "pcspk"; -static PCSpkState *pcspk_state; static inline void generate_samples(PCSpkState *s) { @@ -111,22 +110,6 @@ static void pcspk_callback(void *opaque, int free) } } -static int pcspk_audio_init(ISABus *bus) -{ - PCSpkState *s = pcspk_state; - struct audsettings as = {PCSPK_SAMPLE_RATE, 1, AUD_FMT_U8, 0}; - - AUD_register_card(s_spk, &s->card); - - s->voice = AUD_open_out(&s->card, s->voice, s_spk, s, pcspk_callback, &as); - if (!s->voice) { - AUD_log(s_spk, "Could not open voice\n"); - return -1; - } - - return 0; -} - static uint64_t pcspk_io_read(void *opaque, hwaddr addr, unsigned size) { @@ -179,12 +162,20 @@ static void pcspk_initfn(Object *obj) static void pcspk_realizefn(DeviceState *dev, Error **errp) { + struct audsettings as = {PCSPK_SAMPLE_RATE, 1, AUD_FMT_U8, 0}; ISADevice *isadev = ISA_DEVICE(dev); PCSpkState *s = PC_SPEAKER(dev); isa_register_ioport(isadev, &s->ioport, s->iobase); - pcspk_state = s; + AUD_register_card(s_spk, &s->card); + + s->voice = AUD_open_out(&s->card, s->voice, s_spk, s, pcspk_callback, &as); + if (!s->voice) { + error_setg(errp, "Initializing audio voice failed"); + AUD_remove_card(&s->card); + return; + } } static bool migrate_needed(void *opaque) @@ -221,8 +212,6 @@ static void pcspk_class_initfn(ObjectClass *klass, void *data) set_bit(DEVICE_CATEGORY_SOUND, dc->categories); dc->vmsd = &vmstate_spk; dc->props = pcspk_properties; - /* Reason: realize sets global pcspk_state */ - dc->user_creatable = false; } static const TypeInfo pcspk_info = { @@ -233,6 +222,12 @@ static const TypeInfo pcspk_info = { .class_init = pcspk_class_initfn, }; +static int pcspk_audio_init(ISABus *bus) +{ + isa_create_simple(bus, TYPE_PC_SPEAKER); + return 0; +} + static void pcspk_register(void) { type_register_static(&pcspk_info); From c80a867fa06ec1dcc8fe3460a8295160d68b8608 Mon Sep 17 00:00:00 2001 From: Gerd Hoffmann Date: Thu, 24 Jan 2019 12:20:50 +0100 Subject: [PATCH 2/7] audio: use pkg-config MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Use pkg-config to probe for alsa and pulseaudio. Signed-off-by: Gerd Hoffmann Reviewed-by: Philippe Mathieu-Daudé Message-id: 20190124112055.547-2-kraxel@redhat.com --- configure | 39 +++++++++++++-------------------------- 1 file changed, 13 insertions(+), 26 deletions(-) diff --git a/configure b/configure index 8f312ac3e2..5fcd0f4795 100755 --- a/configure +++ b/configure @@ -3342,39 +3342,26 @@ fi ########################################## # Sound support libraries probe -audio_drv_probe() -{ - drv=$1 - hdr=$2 - lib=$3 - exp=$4 - cfl=$5 - cat > $TMPC << EOF -#include <$hdr> -int main(void) { $exp } -EOF - if compile_prog "$cfl" "$lib" ; then - : - else - error_exit "$drv check failed" \ - "Make sure to have the $drv libs and headers installed." - fi -} - audio_drv_list=$(echo "$audio_drv_list" | sed -e 's/,/ /g') for drv in $audio_drv_list; do case $drv in alsa) - audio_drv_probe $drv alsa/asoundlib.h -lasound \ - "return snd_pcm_close((snd_pcm_t *)0);" - alsa_libs="-lasound" + if $pkg_config alsa --exists; then + alsa_libs=$($pkg_config alsa --libs) + else + error_exit "$drv check failed" \ + "Make sure to have the $drv libs and headers installed." + fi ;; pa) - audio_drv_probe $drv pulse/pulseaudio.h "-lpulse" \ - "pa_context_set_source_output_volume(NULL, 0, NULL, NULL, NULL); return 0;" - pulse_libs="-lpulse" - audio_pt_int="yes" + if $pkg_config libpulse --exists; then + pulse_libs=$($pkg_config libpulse --libs) + audio_pt_int="yes" + else + error_exit "$drv check failed" \ + "Make sure to have the $drv libs and headers installed." + fi ;; sdl) From e42975a153dc7d1d281b67dd31a4998d8df475f7 Mon Sep 17 00:00:00 2001 From: Gerd Hoffmann Date: Thu, 24 Jan 2019 12:20:51 +0100 Subject: [PATCH 3/7] audio: allow optional audio drivers. For those audio drivers which can be probed (sdl, alsa, pulse) add a try-$name variants. Unlike the variants without try- prefix they will not error out on probe failure, the driver will be dropped from the list instead. Mainly useful for the audio_drv_list default values. Signed-off-by: Gerd Hoffmann Message-id: 20190124112055.547-3-kraxel@redhat.com --- configure | 34 ++++++++++++++++++++++++++++------ 1 file changed, 28 insertions(+), 6 deletions(-) diff --git a/configure b/configure index 5fcd0f4795..6a2aa3b533 100755 --- a/configure +++ b/configure @@ -3345,22 +3345,36 @@ fi audio_drv_list=$(echo "$audio_drv_list" | sed -e 's/,/ /g') for drv in $audio_drv_list; do case $drv in - alsa) + alsa | try-alsa) if $pkg_config alsa --exists; then alsa_libs=$($pkg_config alsa --libs) + if test "$drv" = "try-alsa"; then + audio_drv_list=$(echo "$audio_drv_list" | sed -e 's/try-alsa/alsa/') + fi else - error_exit "$drv check failed" \ - "Make sure to have the $drv libs and headers installed." + if test "$drv" = "try-alsa"; then + audio_drv_list=$(echo "$audio_drv_list" | sed -e 's/try-alsa//') + else + error_exit "$drv check failed" \ + "Make sure to have the $drv libs and headers installed." + fi fi ;; - pa) + pa | try-pa) if $pkg_config libpulse --exists; then pulse_libs=$($pkg_config libpulse --libs) audio_pt_int="yes" + if test "$drv" = "try-pa"; then + audio_drv_list=$(echo "$audio_drv_list" | sed -e 's/try-pa/pa/') + fi else - error_exit "$drv check failed" \ - "Make sure to have the $drv libs and headers installed." + if test "$drv" = "try-pa"; then + audio_drv_list=$(echo "$audio_drv_list" | sed -e 's/try-pa//') + else + error_exit "$drv check failed" \ + "Make sure to have the $drv libs and headers installed." + fi fi ;; @@ -3370,6 +3384,14 @@ for drv in $audio_drv_list; do fi ;; + try-sdl) + if test "$sdl" = "no"; then + audio_drv_list=$(echo "$audio_drv_list" | sed -e 's/try-sdl//') + else + audio_drv_list=$(echo "$audio_drv_list" | sed -e 's/try-sdl/sdl/') + fi + ;; + coreaudio) coreaudio_libs="-framework CoreAudio" ;; From f92c7168cdb2a08e183b64f193cdd10535c68c2d Mon Sep 17 00:00:00 2001 From: Gerd Hoffmann Date: Thu, 24 Jan 2019 12:20:52 +0100 Subject: [PATCH 4/7] audio: use try-sdl for openbsd Fixes the openbsd build failure with SDL disabled. Signed-off-by: Gerd Hoffmann Message-id: 20190124112055.547-4-kraxel@redhat.com --- configure | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/configure b/configure index 6a2aa3b533..1eff5946f6 100755 --- a/configure +++ b/configure @@ -829,7 +829,7 @@ NetBSD) OpenBSD) bsd="yes" make="${MAKE-gmake}" - audio_drv_list="sdl" + audio_drv_list="try-sdl" audio_possible_drivers="sdl" HOST_VARIANT_DIR="openbsd" supported_os="yes" From d175505bd68dc35150664a50f51eb9ec1eeee333 Mon Sep 17 00:00:00 2001 From: Gerd Hoffmann Date: Thu, 24 Jan 2019 12:20:53 +0100 Subject: [PATCH 5/7] audio: check for pulseaudio daemon pidfile Check whenever the pulseaudio daemon pidfile is present before trying to initialize the pulseaudio backend. Just return NULL if that is not the case, so qemu will check the next backend in line. In case the user explicitly configured a non-default pulseaudio server skip the check. Signed-off-by: Gerd Hoffmann Message-id: 20190124112055.547-5-kraxel@redhat.com --- audio/paaudio.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/audio/paaudio.c b/audio/paaudio.c index 4c100bc318..6153b908da 100644 --- a/audio/paaudio.c +++ b/audio/paaudio.c @@ -814,6 +814,21 @@ static PAConf glob_conf = { static void *qpa_audio_init (void) { + if (glob_conf.server == NULL) { + char pidfile[64]; + char *runtime; + struct stat st; + + runtime = getenv("XDG_RUNTIME_DIR"); + if (!runtime) { + return NULL; + } + snprintf(pidfile, sizeof(pidfile), "%s/pulse/pid", runtime); + if (stat(pidfile, &st) != 0) { + return NULL; + } + } + paaudio *g = g_malloc(sizeof(paaudio)); g->conf = glob_conf; g->mainloop = NULL; From fb37ce92c8261d30e44760a12ede39957ddb59aa Mon Sep 17 00:00:00 2001 From: Gerd Hoffmann Date: Thu, 24 Jan 2019 12:20:54 +0100 Subject: [PATCH 6/7] audio: error message tweak MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Only print a message about the failed driver initialization in case it was the driver explicitly requested by the user via QEMU_AUDIO_DRV=$drv. Signed-off-by: Gerd Hoffmann Reviewed-by: Philippe Mathieu-Daudé Message-id: 20190124112055.547-6-kraxel@redhat.com --- audio/audio.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/audio/audio.c b/audio/audio.c index 1ace47f510..d163ffbc88 100644 --- a/audio/audio.c +++ b/audio/audio.c @@ -1762,7 +1762,7 @@ void AUD_help (void) ); } -static int audio_driver_init (AudioState *s, struct audio_driver *drv) +static int audio_driver_init(AudioState *s, struct audio_driver *drv, bool msg) { if (drv->options) { audio_process_options (drv->name, drv->options); @@ -1776,7 +1776,9 @@ static int audio_driver_init (AudioState *s, struct audio_driver *drv) return 0; } else { - dolog ("Could not init `%s' audio driver\n", drv->name); + if (msg) { + dolog("Could not init `%s' audio driver\n", drv->name); + } return -1; } } @@ -1901,7 +1903,7 @@ static void audio_init (void) if (drvname) { driver = audio_driver_lookup(drvname); if (driver) { - done = !audio_driver_init(s, driver); + done = !audio_driver_init(s, driver, true); } else { dolog ("Unknown audio driver `%s'\n", drvname); dolog ("Run with -audio-help to list available drivers\n"); @@ -1912,14 +1914,14 @@ static void audio_init (void) for (i = 0; !done && i < ARRAY_SIZE(audio_prio_list); i++) { driver = audio_driver_lookup(audio_prio_list[i]); if (driver && driver->can_be_default) { - done = !audio_driver_init(s, driver); + done = !audio_driver_init(s, driver, false); } } } if (!done) { driver = audio_driver_lookup("none"); - done = !audio_driver_init(s, driver); + done = !audio_driver_init(s, driver, false); assert(done); dolog("warning: Using timer based audio emulation\n"); } From 6a48541873f14b597630283f8f5397674ad82ea9 Mon Sep 17 00:00:00 2001 From: Gerd Hoffmann Date: Thu, 24 Jan 2019 12:20:55 +0100 Subject: [PATCH 7/7] audio: probe audio drivers by default Add the drivers listed in audio_possible_drivers to audio_drv_list, using the try-* variants. That way the probable drivers are compiled by default if possible. Additioal tweaks: linux: reorder to: pa alsa sdl oss. *bsd: drop pa. Signed-off-by: Gerd Hoffmann Message-id: 20190124112055.547-7-kraxel@redhat.com --- configure | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/configure b/configure index 1eff5946f6..b18281c61f 100755 --- a/configure +++ b/configure @@ -794,13 +794,13 @@ MINGW32*) ;; GNU/kFreeBSD) bsd="yes" - audio_drv_list="oss" + audio_drv_list="oss try-sdl" audio_possible_drivers="oss sdl pa" ;; FreeBSD) bsd="yes" make="${MAKE-gmake}" - audio_drv_list="oss" + audio_drv_list="oss try-sdl" audio_possible_drivers="oss sdl pa" # needed for kinfo_getvmmap(3) in libutil.h LIBS="-lutil $LIBS" @@ -813,14 +813,14 @@ FreeBSD) DragonFly) bsd="yes" make="${MAKE-gmake}" - audio_drv_list="oss" + audio_drv_list="oss try-sdl" audio_possible_drivers="oss sdl pa" HOST_VARIANT_DIR="dragonfly" ;; NetBSD) bsd="yes" make="${MAKE-gmake}" - audio_drv_list="oss" + audio_drv_list="oss try-sdl" audio_possible_drivers="oss sdl" oss_lib="-lossaudio" HOST_VARIANT_DIR="netbsd" @@ -845,7 +845,7 @@ Darwin) LDFLAGS="-arch x86_64 $LDFLAGS" fi cocoa="yes" - audio_drv_list="coreaudio" + audio_drv_list="coreaudio try-sdl" audio_possible_drivers="coreaudio sdl" LDFLAGS="-framework CoreFoundation -framework IOKit $LDFLAGS" libs_softmmu="-F/System/Library/Frameworks -framework Cocoa -framework IOKit $libs_softmmu" @@ -861,7 +861,7 @@ SunOS) install="${INSTALL-ginstall}" smbd="${SMBD-/usr/sfw/sbin/smbd}" if test -f /usr/include/sys/soundcard.h ; then - audio_drv_list="oss" + audio_drv_list="oss try-sdl" fi audio_possible_drivers="oss sdl" # needed for CMSG_ macros in sys/socket.h @@ -879,7 +879,7 @@ Haiku) LIBS="-lposix_error_mapper -lnetwork $LIBS" ;; Linux) - audio_drv_list="oss" + audio_drv_list="try-pa try-alsa try-sdl oss" audio_possible_drivers="oss alsa sdl pa" linux="yes" linux_user="yes"