diff --git a/src/qemu/qemu_command.c b/src/qemu/qemu_command.c index abbf4bc8cd..51d74e89af 100644 --- a/src/qemu/qemu_command.c +++ b/src/qemu/qemu_command.c @@ -7642,7 +7642,6 @@ static int qemuBuildGraphicsSDLCommandLine(virQEMUDriverConfigPtr cfg G_GNUC_UNUSED, virCommandPtr cmd, virQEMUCapsPtr qemuCaps G_GNUC_UNUSED, - virDomainDefPtr def, virDomainGraphicsDefPtr graphics) { g_auto(virBuffer) opt = VIR_BUFFER_INITIALIZER; @@ -7654,15 +7653,6 @@ qemuBuildGraphicsSDLCommandLine(virQEMUDriverConfigPtr cfg G_GNUC_UNUSED, if (graphics->data.sdl.fullscreen) virCommandAddArg(cmd, "-full-screen"); - if (def->naudios == 0) { - /* If using SDL for video, then we should just let it - * use QEMU's host audio drivers, possibly SDL too - * User can set these two before starting libvirtd - */ - virCommandAddEnvPass(cmd, "QEMU_AUDIO_DRV"); - virCommandAddEnvPass(cmd, "SDL_AUDIODRIVER"); - } - virCommandAddArg(cmd, "-display"); virBufferAddLit(&opt, "sdl"); @@ -7680,7 +7670,6 @@ static int qemuBuildGraphicsVNCCommandLine(virQEMUDriverConfigPtr cfg, virCommandPtr cmd, virQEMUCapsPtr qemuCaps, - virDomainDefPtr def, virDomainGraphicsDefPtr graphics) { g_auto(virBuffer) opt = VIR_BUFFER_INITIALIZER; @@ -7807,17 +7796,6 @@ qemuBuildGraphicsVNCCommandLine(virQEMUDriverConfigPtr cfg, if (graphics->data.vnc.keymap) virCommandAddArgList(cmd, "-k", graphics->data.vnc.keymap, NULL); - if (def->naudios == 0) { - /* Unless user requested it, set the audio backend to none, to - * prevent it opening the host OS audio devices, since that causes - * security issues and might not work when using VNC. - */ - if (cfg->vncAllowHostAudio) - virCommandAddEnvPass(cmd, "QEMU_AUDIO_DRV"); - else - virCommandAddEnvString(cmd, "QEMU_AUDIO_DRV=none"); - } - return 0; } @@ -7825,7 +7803,6 @@ qemuBuildGraphicsVNCCommandLine(virQEMUDriverConfigPtr cfg, static int qemuBuildGraphicsSPICECommandLine(virQEMUDriverConfigPtr cfg, virCommandPtr cmd, - virDomainDefPtr def, virDomainGraphicsDefPtr graphics) { g_auto(virBuffer) opt = VIR_BUFFER_INITIALIZER; @@ -8024,13 +8001,6 @@ qemuBuildGraphicsSPICECommandLine(virQEMUDriverConfigPtr cfg, virCommandAddArgList(cmd, "-k", graphics->data.spice.keymap, NULL); - if (def->naudios == 0) { - /* SPICE includes native support for tunnelling audio, so we - * set the audio backend to point at SPICE's own driver - */ - virCommandAddEnvString(cmd, "QEMU_AUDIO_DRV=spice"); - } - return 0; } @@ -8071,19 +8041,19 @@ qemuBuildGraphicsCommandLine(virQEMUDriverConfigPtr cfg, switch (graphics->type) { case VIR_DOMAIN_GRAPHICS_TYPE_SDL: if (qemuBuildGraphicsSDLCommandLine(cfg, cmd, - qemuCaps, def, graphics) < 0) + qemuCaps, graphics) < 0) return -1; break; case VIR_DOMAIN_GRAPHICS_TYPE_VNC: if (qemuBuildGraphicsVNCCommandLine(cfg, cmd, - qemuCaps, def, graphics) < 0) + qemuCaps, graphics) < 0) return -1; break; case VIR_DOMAIN_GRAPHICS_TYPE_SPICE: if (qemuBuildGraphicsSPICECommandLine(cfg, cmd, - def, graphics) < 0) + graphics) < 0) return -1; break; @@ -10081,13 +10051,6 @@ qemuBuildCommandLine(virQEMUDriverPtr driver, if (!def->ngraphics) { virCommandAddArg(cmd, "-display"); virCommandAddArg(cmd, "none"); - - if (def->naudios == 0) { - if (cfg->nogfxAllowHostAudio) - virCommandAddEnvPass(cmd, "QEMU_AUDIO_DRV"); - else - virCommandAddEnvString(cmd, "QEMU_AUDIO_DRV=none"); - } } /* Disable global config files and default devices */ diff --git a/src/qemu/qemu_domain.c b/src/qemu/qemu_domain.c index 73e2473dce..df332ad870 100644 --- a/src/qemu/qemu_domain.c +++ b/src/qemu/qemu_domain.c @@ -3573,9 +3573,112 @@ qemuDomainDefAddImplicitInputDevice(virDomainDef *def) return 0; } +static int +qemuDomainDefAddDefaultAudioBackend(virQEMUDriverPtr driver, + virDomainDefPtr def) +{ + size_t i; + bool addAudio = false; + bool audioPassthrough = false; + int audioBackend = VIR_DOMAIN_AUDIO_TYPE_NONE; + g_autoptr(virQEMUDriverConfig) cfg = virQEMUDriverGetConfig(driver); + + if (def->naudios > 0) { + return 0; + } + + for (i = 0; i < def->ngraphics; i++) { + virDomainGraphicsDefPtr graph = def->graphics[i]; + + switch ((virDomainGraphicsType)graph->type) { + case VIR_DOMAIN_GRAPHICS_TYPE_VNC: + if (cfg->vncAllowHostAudio) { + audioPassthrough = true; + } else { + audioPassthrough = false; + audioBackend = VIR_DOMAIN_AUDIO_TYPE_NONE; + } + addAudio = true; + break; + case VIR_DOMAIN_GRAPHICS_TYPE_SPICE: + audioPassthrough = false; + audioBackend = VIR_DOMAIN_AUDIO_TYPE_SPICE; + addAudio = true; + break; + case VIR_DOMAIN_GRAPHICS_TYPE_SDL: + audioPassthrough = true; + addAudio = true; + break; + + case VIR_DOMAIN_GRAPHICS_TYPE_RDP: + case VIR_DOMAIN_GRAPHICS_TYPE_DESKTOP: + case VIR_DOMAIN_GRAPHICS_TYPE_EGL_HEADLESS: + break; + case VIR_DOMAIN_GRAPHICS_TYPE_LAST: + default: + virReportEnumRangeError(virDomainGraphicsType, graph->type); + return -1; + } + } + + if (!def->ngraphics) { + if (cfg->nogfxAllowHostAudio) { + audioPassthrough = true; + } else { + audioPassthrough = false; + audioBackend = VIR_DOMAIN_AUDIO_TYPE_NONE; + } + addAudio = true; + } + + if (addAudio && audioPassthrough) { + const char *audioenv = g_getenv("QEMU_AUDIO_DRV"); + if (audioenv == NULL) { + addAudio = false; + } else { + /* + * QEMU audio driver names are mostly the same as + * libvirt XML audio backend names + */ + if (STREQ(audioenv, "pa")) { + audioBackend = VIR_DOMAIN_AUDIO_TYPE_PULSEAUDIO; + } else { + if ((audioBackend = virDomainAudioTypeTypeFromString(audioenv)) < 0) { + virReportError(VIR_ERR_CONFIG_UNSUPPORTED, + _("unknown QEMU_AUDIO_DRV setting %s"), audioenv); + return -1; + } + } + } + } + if (addAudio) { + virDomainAudioDefPtr audio = g_new0(virDomainAudioDef, 1); + + audio->type = audioBackend; + audio->id = 1; + + def->naudios = 1; + def->audios = g_new0(virDomainAudioDefPtr, def->naudios); + def->audios[0] = audio; + + if (audioBackend == VIR_DOMAIN_AUDIO_TYPE_SDL) { + const char *sdldriver = g_getenv("SDL_AUDIODRIVER"); + if (sdldriver != NULL && + ((audio->backend.sdl.driver = + virDomainAudioSDLDriverTypeFromString(sdldriver)) <= 0)) { + virReportError(VIR_ERR_CONFIG_UNSUPPORTED, + _("unknown SDL_AUDIODRIVER setting %s"), sdldriver); + return -1; + } + } + } + + return 0; +} static int -qemuDomainDefAddDefaultDevices(virDomainDefPtr def, +qemuDomainDefAddDefaultDevices(virQEMUDriverPtr driver, + virDomainDefPtr def, virQEMUCapsPtr qemuCaps) { bool addDefaultUSB = true; @@ -3827,6 +3930,9 @@ qemuDomainDefAddDefaultDevices(virDomainDefPtr def, } } + if (qemuDomainDefAddDefaultAudioBackend(driver, def) < 0) + return -1; + return 0; } @@ -4419,7 +4525,7 @@ qemuDomainDefPostParse(virDomainDefPtr def, qemuDomainNVRAMPathGenerate(cfg, def); - if (qemuDomainDefAddDefaultDevices(def, qemuCaps) < 0) + if (qemuDomainDefAddDefaultDevices(driver, def, qemuCaps) < 0) return -1; if (qemuCanonicalizeMachine(def, qemuCaps) < 0) diff --git a/tests/qemudomaincheckpointxml2xmlout/redefine.xml b/tests/qemudomaincheckpointxml2xmlout/redefine.xml index 524360fb4c..b7c9d9dc6c 100644 --- a/tests/qemudomaincheckpointxml2xmlout/redefine.xml +++ b/tests/qemudomaincheckpointxml2xmlout/redefine.xml @@ -57,6 +57,7 @@ +