libvirt and SPICE: audio passthrough

Posted on August 18, 2021

What now? Why?

I’ve been using libvirt on my workstation to host a variety of local VMs for a while. Recently, Amazon Music HD became a thing. It’s pretty slick! OPUS and FLAC codecs! However, this is only supported on their desktop players and mobile players - not on the web player.

Well, I don’t run Windows, and despite my trying, the desktop app simply won’t run in Wine (or more specifically, Codeweaver’s Crossover). So hey, I have this Windows VM I use for the occasional Active Directory work and a non-administrative user on it… so I installed the player only to realize that, despite having an audio device defined in the libvirt domain, nothing was making it to my ears.

I wasted far too much time trying to use QEMU arguments to make it talk to pulseaudio via UNIX socket… when I discovered that SPICE actually supports audio passthrough! I already use SPICE for the GUI… and apparently the defaults are to pass audio through if SPICE is in use. Despite this, no audio was happening.

I ended up needing to add some stuff to the domain XML:

    <sound model='ich9'>
      <codec type='output'/>
      <audio id='1'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x1b' function='0x0'/>
    </sound>
    <audio id='1' type='spice'>
      <output mixingEngine='yes' fixedSettings='no'/>
    </audio>

Note that I am using the output codec and only defining the output mixingEngine. Originally I tried to get the microphone working (with the ‘micro’ codec) but while it showed up, nothing seemed to make it into the VM. Since I don’t really need that, I just turned it off.

Just for reference, the rest of the SPICE settings are as follows.

    <channel type='spicevmc'>
      <target type='virtio' name='com.redhat.spice.0'/>
      <address type='virtio-serial' controller='0' bus='0' port='1'/>
    </channel>
    <graphics type='spice' autoport='yes'>
      <listen type='address'/>
      <image compression='off'/>
    </graphics>
    <redirdev bus='usb' type='spicevmc'>
      <address type='usb' bus='0' port='2'/>
    </redirdev>
    <redirdev bus='usb' type='spicevmc'>
      <address type='usb' bus='0' port='3'/>
    </redirdev>

I made sure to install the various drivers and the qemu guest agent via the virtio-win windows ISO.

RHEL8 update

I have since migrated to RHEL8, and when importing my KVM domains I ran into problems - specifically the <audio /> element is not available. I pulled my hear out for a while but found the solution was actually quite simple. All I had to do was remove the existing SPICE graphics, and re-add it. Upon doing so, I was left with a configuration like so and audio Just Worked. Note there is some device-specific video stuff in this as I have enabled OpenGL.

    <graphics type='spice'>
      <listen type='none'/>
      <image compression='off'/>
      <gl enable='yes' rendernode='/dev/dri/by-path/pci-0000:01:00.0-render'/>
    </graphics>
    <sound model='ich9'>
      <alias name='sound0'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x1b' function='0x0'/>
    </sound>
    <video>
      <model type='qxl' ram='65536' vram='65536' vgamem='16384' heads='1' primary='yes'/>
      <alias name='video0'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x0'/>
    </video>
Disclaimer/Copyright

The information, views, and opinions published on this website were done so in the author's personal capacity. The information, views, and opinions expressed in this article are the author's own and do not reflect the view of their employer, or any other entity unless explicitly stated otherwise.

All data and information provided on this site is for informational purposes only. This website and it's operators makes no representations as to accuracy, completeness, currentness, suitability, or validity of any information on this site and will not be liable for any errors, omissions, or delays in this information or any losses, injuries, or damages arising from its display or use. All information is provided on an as-is basis.

All original content on this website is, unless explicitly stated otherwise, licensed under the MIT license. Full license text is available here. Non-original content that is included on this website in whole or in part, linked, or otherwise made available remains under copyright of the original owners.