Difference between revisions of "Using JACK and PipeWire"

From Looking Glass
Jump to navigation Jump to search
(qemu 6.1 is now in official backports)
Tags: Mobile web edit, Mobile edit
 
(6 intermediate revisions by 5 users not shown)
Line 1: Line 1:
 
__FORCETOC__
 
__FORCETOC__
 +
 +
----
 +
 +
'''NOTE: Since the release of Looking Glass B6 which has native audio support built in, this is no longer the preferred method unless you require audio from the VM while the LG client application is not running'''
 +
 +
----
 +
 
QEMU's JACK backend provides the best audio quality out of all the available options (Scream, PulseAudio, etc.). One of the easiest ways to leverage the JACK support is by using PipeWire, which emulates JACK.
 
QEMU's JACK backend provides the best audio quality out of all the available options (Scream, PulseAudio, etc.). One of the easiest ways to leverage the JACK support is by using PipeWire, which emulates JACK.
  
Line 7: Line 14:
 
Normally, you can run applications with PipeWire's JACK support by running it with <code>pw-jack</code>, but this is not possible when using libvirt. Instead, you need PipeWire to provide drop-in JACK support. To do this:
 
Normally, you can run applications with PipeWire's JACK support by running it with <code>pw-jack</code>, but this is not possible when using libvirt. Instead, you need PipeWire to provide drop-in JACK support. To do this:
 
* on Debian: run <code>sudo cp /usr/share/doc/pipewire/examples/ld.so.conf.d/pipewire-jack-*.conf /etc/ld.so.conf.d/; sudo ldconfig</code>
 
* on Debian: run <code>sudo cp /usr/share/doc/pipewire/examples/ld.so.conf.d/pipewire-jack-*.conf /etc/ld.so.conf.d/; sudo ldconfig</code>
* on Arch: install the [https://aur.archlinux.org/packages/pipewire-jack-dropin/ pipewire-jack-dropin] AUR package.
+
* on Arch: install the <code>qemu-audio-jack</code> package, which contains the jack driver for qemu, and the <code>pipewire-jack</code> package.
  
 
Once this is done, you have two options. You could run your virtual machines under your user, or you could set it up so that the QEMU user has access to your PipeWire socket. The former is easier, while the latter is more secure.
 
Once this is done, you have two options. You could run your virtual machines under your user, or you could set it up so that the QEMU user has access to your PipeWire socket. The former is easier, while the latter is more secure.
Line 21: Line 28:
 
Then, you can run <code>virsh edit win10</code>, and perform the following changes:
 
Then, you can run <code>virsh edit win10</code>, and perform the following changes:
  
# If the <code><domain></code> line doesn't already contain <code>xmlns:qemu</code>, add <code><nowiki>xmlns:qemu='http://libvirt.org/schemas/domain/qemu/1.0'</nowiki></code>. The line should look like:
+
# If the <code><domain></code> line doesn't already contain <code>xmlns:qemu='http://libvirt.org/schemas/domain/qemu/1.0'</code>, add it.
#: <syntaxhighlight lang="xml"><domain type='kvm' xmlns:qemu='http://libvirt.org/schemas/domain/qemu/1.0'></syntaxhighlight>
 
# Add the following block to the <code><devices></code> section:
 
#: <syntaxhighlight lang="xml">
 
  <audio id="1" type="jack">
 
    <input clientName="win10" connectPorts="input-name"/>
 
    <output clientName="win10" connectPorts="output-name"/>
 
  </audio>
 
  <sound model="ich9">
 
    <alias name="sound0"/>
 
  </sound>
 
</syntaxhighlight>
 
#: <code>input-name</code> and <code>output-name</code> should be replaced with a regular expression that matches the name of the JACK input and output ports. Essentially, these are the names of microphone and speaker devices that the VM would connect to. You can get the names by looking at your patchbay, or run <code>jack_lsp</code>.
 
 
#: If there is an existing <code><sound></code> block, it should be removed.
 
#: If there is an existing <code><sound></code> block, it should be removed.
 
# Finally, we need to set PipeWire's runtime directory and latency. These are configured by environment variables. To do this, the following block right before <code></domain></code>:
 
# Finally, we need to set PipeWire's runtime directory and latency. These are configured by environment variables. To do this, the following block right before <code></domain></code>:
Line 51: Line 46:
 
   /run/user/1000/pipewire-0 rw,
 
   /run/user/1000/pipewire-0 rw,
 
</source>
 
</source>
 +
 +
 +
  
 
Once you shutdown your VM and restart it, you should see an emulated HD audio device, and any sound should go into PipeWire. If you don't hear anything, check your patchbay connections.
 
Once you shutdown your VM and restart it, you should see an emulated HD audio device, and any sound should go into PipeWire. If you don't hear anything, check your patchbay connections.
  
== Allow QEMU user access to your PipeWire socket ==
+
== Allow QEMU user access your PipeWire socket ==
  
 
We will assuming your username is <code>example</code>, your user ID is 1000, and your virtual machine is called <code>win10</code>. We will also assume the QEMU user is <code>libvirt-qemu</code>, but this varies based on distro. We will be duplicating the socket as <code>/srv/win10/pipewire-0</code>, but the directory can be changed.
 
We will assuming your username is <code>example</code>, your user ID is 1000, and your virtual machine is called <code>win10</code>. We will also assume the QEMU user is <code>libvirt-qemu</code>, but this varies based on distro. We will be duplicating the socket as <code>/srv/win10/pipewire-0</code>, but the directory can be changed.
Line 114: Line 112:
 
   /srv/win10/pipewire-0 rw,
 
   /srv/win10/pipewire-0 rw,
 
</source>
 
</source>
 +
 +
If you are using NixOS, you'll also need to add <code><qemu:env name="LD_LIBRARY_PATH" value="$LD_LIBRARY_PATH:/run/current-system/sw/lib/pipewire"/></code> to your <code><qemu:commandline></code> section.
  
 
Once you shutdown your VM and restart it, you should see an emulated HD audio device, and any sound should go into PipeWire. If you don't hear anything, check your patchbay connections.
 
Once you shutdown your VM and restart it, you should see an emulated HD audio device, and any sound should go into PipeWire. If you don't hear anything, check your patchbay connections.

Latest revision as of 13:05, 9 November 2024



NOTE: Since the release of Looking Glass B6 which has native audio support built in, this is no longer the preferred method unless you require audio from the VM while the LG client application is not running


QEMU's JACK backend provides the best audio quality out of all the available options (Scream, PulseAudio, etc.). One of the easiest ways to leverage the JACK support is by using PipeWire, which emulates JACK.

First, you must make sure your distro's QEMU is built with JACK support:

  • On Debian, this is not the case until QEMU 6.1. This is currently only in unstable and bullseye-backports. You should enable backports if you are using bullseye and upgrade if you are using something older.

Normally, you can run applications with PipeWire's JACK support by running it with pw-jack, but this is not possible when using libvirt. Instead, you need PipeWire to provide drop-in JACK support. To do this:

  • on Debian: run sudo cp /usr/share/doc/pipewire/examples/ld.so.conf.d/pipewire-jack-*.conf /etc/ld.so.conf.d/; sudo ldconfig
  • on Arch: install the qemu-audio-jack package, which contains the jack driver for qemu, and the pipewire-jack package.

Once this is done, you have two options. You could run your virtual machines under your user, or you could set it up so that the QEMU user has access to your PipeWire socket. The former is easier, while the latter is more secure.

Run VMs under your user[edit]

We will assuming your username is example, your user ID is 1000, and your virtual machine is called win10.

You can configure libvirt to run QEMU virtual machines under your user by adding the following line to /etc/libvirt/qemu.conf:

user = "example"

Then, you can run virsh edit win10, and perform the following changes:

  1. If the <domain> line doesn't already contain xmlns:qemu='http://libvirt.org/schemas/domain/qemu/1.0', add it.
    If there is an existing <sound> block, it should be removed.
  2. Finally, we need to set PipeWire's runtime directory and latency. These are configured by environment variables. To do this, the following block right before </domain>:
      <qemu:commandline>
        <qemu:env name="PIPEWIRE_RUNTIME_DIR" value="/run/user/1000"/>
        <qemu:env name="PIPEWIRE_LATENCY" value="512/48000"/>
      </qemu:commandline>
    If you already have a <qemu:commandline> section, merge this block with it. Remember to change 1000 to your actual user ID. You can tune the latency and sampling rate as you like.

If you are using a distro with apparmor, you will need to configure new rules. To do this, append the following lines to your /etc/apparmor.d/local/abstractions/libvirt-qemu (if this file doesn't exist, create it):

  # PipeWire/JACK
  /etc/pipewire/* r,
  /usr/share/pipewire/* r,
  /run/user/1000/pipewire-0 rw,



Once you shutdown your VM and restart it, you should see an emulated HD audio device, and any sound should go into PipeWire. If you don't hear anything, check your patchbay connections.

Allow QEMU user access your PipeWire socket[edit]

We will assuming your username is example, your user ID is 1000, and your virtual machine is called win10. We will also assume the QEMU user is libvirt-qemu, but this varies based on distro. We will be duplicating the socket as /srv/win10/pipewire-0, but the directory can be changed.

First, we must prepare the /srv/win10 directory. Run the following commands:

$ sudo mkdir /srv/win10
$ sudo chown example: /srv/win10
$ chmod 700 /srv/win10
$ setfacl -m u:libvirt-qemu:rx /srv/win10
$ touch /srv/win10/pipewire-0

We then need to allow the socket to be bind mounted to the new location. To do this, edit /etc/fstab, and add:

/run/user/1000/pipewire-0 /srv/win10/pipewire-0 none bind,rw,user,noauto 0 0

Then, we configure this socket to be mounted and unmounted when systemd starts pipewire.socket. To do this, run systemctl --user edit pipewire.socket and put in:

[Socket]
ExecStartPost=/bin/mount /srv/win10/pipewire-0
ExecStopPre=/bin/umount /srv/win10/pipewire-0

Then, run systemctl --user restart pipewire.socket, and the socket should be mirrored. Now we can tell libvirt to use this socket.

Run virsh edit win10, and perform the following changes:

  1. If the <domain> line doesn't already contain xmlns:qemu, add xmlns:qemu='http://libvirt.org/schemas/domain/qemu/1.0'. The line should look like:
    <domain type='kvm' xmlns:qemu='http://libvirt.org/schemas/domain/qemu/1.0'>
  2. Add the following block to the <devices> section:
      <audio id="1" type="jack">
        <input clientName="win10" connectPorts="input-name"/>
        <output clientName="win10" connectPorts="output-name"/>
      </audio>
      <sound model="ich9">
        <alias name="sound0"/>
      </sound>
    input-name and output-name should be replaced with a regular expression that matches the name of the JACK input and output ports. Essentially, these are the names of microphone and speaker devices that the VM would connect to. You can get the names by looking at your patchbay, or run jack_lsp.
    If there is an existing <sound> block, it should be removed.
  3. Finally, we need to set PipeWire's runtime directory and latency. These are configured by environment variables. To do this, the following block right before </domain>:
      <qemu:commandline>
        <qemu:env name="PIPEWIRE_RUNTIME_DIR" value="/srv/win10"/>
        <qemu:env name="PIPEWIRE_LATENCY" value="512/48000"/>
      </qemu:commandline>
    If you already have a <qemu:commandline> section, merge this block with it. Remember to change 1000 to your actual user ID. You can tune the latency and sampling rate as you like.

If you are using a distro with apparmor, you will need to configure new rules. To do this, append the following lines to your /etc/apparmor.d/local/abstractions/libvirt-qemu (if this file doesn't exist, create it):

  # PipeWire/JACK
  /etc/pipewire/* r,
  /usr/share/pipewire/* r,
  /srv/win10/pipewire-0 rw,

If you are using NixOS, you'll also need to add <qemu:env name="LD_LIBRARY_PATH" value="$LD_LIBRARY_PATH:/run/current-system/sw/lib/pipewire"/> to your <qemu:commandline> section.

Once you shutdown your VM and restart it, you should see an emulated HD audio device, and any sound should go into PipeWire. If you don't hear anything, check your patchbay connections.