PipeWire and the future of Audio in Linux

2021-10-03

(last time edited: 2021-10-17)

tags: audio, linux

PipeWire is a project that aims to greatly improve handling of audio and video under Linux. It provides a low-latency, graph based processing engine on top of audio and video devices that can be used to support the use cases currently handled by both pulseaudio and JACK Audio Connection Kit. PipeWire was designed with a powerful security model that makes interacting with audio and video devices from containerized applications easy, with supporting Flatpak applications being the primary goal. Alongside Wayland and Flatpak we expect PipeWire to provide a core building block for the future of Linux application development.

I found myself messing around with Flatpak for a whole month, testing different permission settings and multiple sandboxed GUI programs and decided to give PipeWire a try. PipeWire? What does an audio interface has anything to do with Flatpak!?

Well, Flatpak and Pipewire are new technologies that appeared in the last few years trying to solve several security model problems in the Linux desktop environment. They relatively share new conventions of software oriented to sandboxed applications and being completely flexible.

For many years PulseAudio looked like the only option we should use for a fully functional Linux desktop system, but problems start to arise when we try new things and mess around with JACK and real-time multimedia media processing for music production.

PipeWire is still very new, not bloaty and it's incredibly stable and functional. It solves the strong dependency we have on PulseAudio and brings a whole new meta to the scene. When we use PipeWire we are not completely ditching PulseAudio, but using PulseAudio as a module. Woah calm down, you mean PipeWire is the new big daddy that controls everything? It is. The same applies to JACK and it will be used as a module and ALSA controlled by PipeWire.

PipeWire is not a replacement for PulseAudio, don't be silly!

Good thing about all this, is that we might finally start seeing competitors to PulseAudio in the next years.

Installation

In many distros PipeWire is already packaged. In Alpine Linux it is required to install the following packages:

pipewire
pipewire-pulse
pipewire-jack
pipewire-tools

I believe that most distributions follow a similar pattern.

Don't forget to uninstall PulseAudio from the system.

Anyways, I installed it and had to tinker a bit to execute it without errors.

Preparations

Elogind is the systemd project's "logind", extracted out to be a standalone daemon. It integrates with PAM to know the set of users that are logged in to a system and whether they are logged in graphically, on the console, or remotely. Elogind exposes this information via the standard org.freedesktop.login1 D-Bus interface, as well as through the file system using systemd's standard /run/systemd layout. Elogind also provides "libelogind", which is a subset of the facilities offered by "libsystemd". There is a "libelogind.pc" pkg-config file as well.

Elogind is a standalone program daemon that does magical things in the background while you are logged in. This tool will call D-Bus on-demand and most importantly will create the environment variable XDG_RUNTIME_DIR on the fly which is absolutely needed for many audio programs and Flatpak included. Elogind will also launch jackd when it's necessary. Programs such as SonicPi will work out of the box with this setup.

Add it to the boot runlevel in your init system. In Alpine Linux using OpenRC do not add it in the default runlevel because it won't work.

In Alpine Linux:

# rc-update add elogind boot && rc-service elogind start

In Void Linux:

# ln -s /etc/sv/elogind /var/service

And execute your Window Manager correctly. In my case I use Openbox. (~/.xinitrc)

#!/bin/sh

[ -f ~/.xprofile ] && . ~/.xprofile
# I source .xprofile manually because /etc/profile doesn't do it manually. 
# ~/.xprofile takes care of running PipeWire

openbox-session # no need to use dbus-run-session
#!/bin/sh

# ...

pipewire &

# ...

You can still use pavucontrol, pamixer, pulsemixer, pactl and other PulseAudio programs because we will be running PulseAudio inside PipeWire, but as a module!

How it works

PipeWire will read the default configurations stored in /usr/share/pipewire.

At the end of /usr/share/pipewire/pipewire.conf there are some binaries declared that will be executed. They look something like this.

context.exec = [
    { path = "/usr/bin/pipewire-media-session"  args = "" }
    { path = "/usr/bin/pipewire" args = "-c pipewire-pulse.conf" }
]

By executing pipewire in ~/.xinitrc you are basically bringing up the pipewire-media-session binary and then the Pulse module.

Also, if you want to use a per-user configuration, all files in /usr/share/pipewire should be stored in ~/.config/pipewire/. I do not recommend it unless you want to disable specific modules and tinker a lot with PipeWire.

Start your Window Manager session normally using startx and it should be working. To check if PipeWire is correctly running you can use many command tools, start PulseAudio Volume Control or use the PulseAudio tools.

$ pactl info

Debugging

If anything goes wrong you can always set up this variable and execute the program manually to find out what is going on.

$ PIPEWIRE_DEBUG=3 pipewire

There is no need to uncomment or change anything in the default configuration.

PipeWire Tools

PipeWire is a fully interactive framework. You can get information and do different things with it, for example:

$ pw-top

If for some reason you don't have that program then it might be packaged separately from the main PipeWire package.

Search for a package called pipewire-tools or similar.

You can make use of these different programs:

pw-cat
pw-dot
pw-dsdplay
pw-dump
pw-link
pw-loopback
pw-metadata
pw-mididump
pw-midiplay
pw-midirecord
pw-mon
pw-play
pw-profiler
pw-record
pw-reserve
pw-top

Final Thoughts

PipeWire just werks. I haven't had any problems with it. From a technical point of view we can say it's a low-level wrapper that works correctly as expected. So don't be afraid to ditch the standalone PulseAudio installation and replace it with PipeWire + PulseAudio module. I know I'm not spitting out much information about it but there isn't much to say since I'm not an audio professional tinkerer.

I didn't need to use dbus-x11 nor mess with Jack privileges to make PipeWire work correctly.

In Alpine Linux for newer PipeWire versions there is a wrapper for launching it. Click here to read it. With Alpine and newer versions of PipeWire you'd rather start PipeWire in ~/.xprofile using sh /usr/libexec/pipewire-launcher & rather than plain pipewire.

One thing for sure, PipeWire solved the biggest burden for audio makers. Changing between PulseAudio and Jack to run certain audio production software was a mess. Now with PipeWire there is no need to worry anymore about killing processes or putting processes on hold.

For more information you can look at the docs or the wiki and the source code.

The best information for PipeWire is actually found in the Gentoo documentation for PipeWire.

Bugs

In Pipewire version 0.3.29 there is a bug that will prevent from saving PulseAudio settings. This is terrible when you need to record stuff with some software and then you have to manually set up all your devices correctly each launch.

It has been fixed in newer versions from 0.3.30 to later. I'll check that out soon.