Baeldung Pro – Linux – NPI EA (cat = Baeldung on Linux)
announcement - icon

Learn through the super-clean Baeldung Pro experience:

>> Membership and Baeldung Pro.

No ads, dark-mode and 6 months free of IntelliJ Idea Ultimate to start with.

1. Overview

PipeWire is a multimedia framework (Sound Server) that offers capturing and playback capabilities for audio and video streams. It’s relatively more performant and light on resources as compared to PulseAudio. In addition, it also implements pipelining for other audio systems like PulseAudio, JACK, ALSA, and more.

It also provides niceties like switching from HFP (Hands-Free Profile) to AD2P (Advanced Audio Distribution Profile) for most headsets. Not only that, it also supports video streaming, which we can for tasks like screen sharing under Wayland.

The recent versions of Ubuntu and other major distributions ship with PipeWire. However, there are still some distributions that are stuck with PulseAudio. Therefore, in this article, we’ll focus on replacing PulseAudio with PipeWire for those distributions. Moreover, we’ll also explore how to keep both PulseAudio and PipeWire installed and switch between them when needed.

2. Setting up PipeWire

We can check for the presence of PipeWire by checking the status of the pipewire service:

$ systemctl status --user pipewire
● pipewire.service - PipeWire Multimedia Service
Active: active (running) since Wed 2024-01-17 17:00:52 PKT; 1min 25s ago
...

In this case, PipeWire is already up and running. Therefore, we don’t need to install it. However, if there is no such service, then the system is probably running PulseAudio or another sound server:

$ systemctl status --user pipewire
Unit pipewire.service could not be found.

Notably, the system doesn’t have PipeWire installed. In the next sections, we’ll remove PulseAudio if it exists on the system and configure PipeWire instead.

2.1. Removing PulseAudio

It’s recommended that PulseAudio be removed from the system to avoid any conflicts that may arise. However, we can stop and disable the pulseaudio daemon beforehand:

$ systemctl --user stop pulseaudio
$ systemctl --user disable pulseaudio

Then, we can remove the pulseaudio package entirely:

# Debian and derivatives
$ sudo apt remove --purge pulseaudio

# Fedora and Red Hat
$ sudo dnf remove pulseaudio

# Arch Linux and derivatives
$ sudo pacman -Rns pulseaudio

However, if we prefer to keep PulseAudio and PipeWire installed and switch between them, we can refer to later sections.

2.2. PipeWire

PipeWire is available on most official package repositories:

# Debian and derivatives
$ sudo apt install -y pipewire pipewire-audio-client-libraries pulseaudio-utils

# Fedora and Red Hat
$ sudo dnf install -y pipewire pipewire-pulseaudio pipewire-utils pulseaudio-utils

# Arch Linux and derivatives
$ sudo pacman -S --noconfirm pipewire pipewire-pulse

We also installed pipewire-pulse, which is a compatibility layer between PipeWire and PulseAudio. Therefore, if we have applications that were originally developed to work with PulseAudio, then pipewire-pulse takes care of that. In contrast, we can also choose compatibility layers for other audio systems.

Furthermore, pulseaudio-utils include useful tools like pactl for managing PulseAudio.

2.3. WirePlumber

WirePlumber manages and routes videos and audio streams to appropriate channels. It’s also a policy manager which makes decisions about how the video and audio are handled.

For instance, when we record audio on a browser, WirePlumber detects the available audio recording devices attached to the system. Then, it decides where to route the audio stream. In the case of mic disconnection, WirePlumber channels the audio stream from another input device.

Like PipeWire, WirePlumber is available on most package repositories:

# Debian and derivatives
$ sudo apt install -y wireplumber

# Fedora and Red Hat
$ sudo dnf install -y wireplumber

# Arch Linux and derivatives
$ sudo pacman -S --noconfirm wireplumber

Once all the required packages are installed, we can now configure them.

2.4. Configuration

First, we’ll need to enable the required services:

$ systemctl enable --user pipewire
$ systemctl enable --user wireplumber

Once enabled, let’s start these services:

$ systemctl start --user pipewire
$ systemctl start --user wireplumber

Now, let’s ensure that PipeWire is running using pactl:

$ pactl info | grep Server
Server String: /run/user/1000/pulse/native
Server Protocol Version: 35
Server Name: PulseAudio (on PipeWire 0.3.65)
Server Version: 15.0.0

That’s it! We can now play sounds through PipeWire.

3. Switching Between PulseAudio and PipeWire

If we want to keep PulseAudio and PipeWire installed, we can switch between them easily. Here, we create bash scripts to switch between them.

Let’s first create the script and name it switch-audio.sh. Then, let’s include the following commands inside the created script using nano command:

#!/bin/bash

if [ "$1" == "pipewire" ]; then
    echo "Switching to PipeWire..."
    systemctl --user stop pulseaudio
    systemctl --user disable pulseaudio
    systemctl --user enable pipewire
    systemctl --user enable wireplumber
    systemctl --user start pipewire
    systemctl --user start wireplumber
    echo "Switched to PipeWire."

elif [ "$1" == "pulseaudio" ]; then
    echo "Switching to PulseAudio..."
    systemctl --user stop pipewire
    systemctl --user stop wireplumber
    systemctl --user disable pipewire
    systemctl --user disable wireplumber
    systemctl --user enable pulseaudio
    systemctl --user start pulseaudio
    echo "Switched to PulseAudio."

else
    echo "Usage: $0 [pipewire|pulseaudio]"
    echo "Specify 'pipewire' or 'pulseaudio' to switch between them."
fi

This script allows easy switching between Pulseaudio and PipeWire sound servers without uninstalling either.

Next, let’s make the script executable:

$ chmod +x switch-audio.sh

Further, the script checks the first argument ($1) passed during execution to determine whether we want to switch to pipewire or pulseaudio.

Moving forward, let’s test the script and switch back to pulseaudio:

$ ./switch-audio.sh pulseaudio
Switching to PulseAudio...
Stopping 'pipewire.service', but its triggering units are still active:
...
wireplumber.service
Created symlink '/home/kali/.config/systemd/user/default.target.wants/pulseaudio.service' → '/usr/lib/systemd/user/pulseaudio.service'.
Created symlink '/home/kali/.config/systemd/user/sockets.target.wants/pulseaudio.socket' → '/usr/lib/systemd/user/pulseaudio.socket'.
Switched to PulseAudio.

Now, let’s confirm by checking the status of the pulseaudio service:

systemctl --user status pulseaudio
● pulseaudio.service - Sound Service
     Loaded: loaded (/usr/lib/systemd/user/pulseaudio.service; enabled; preset: enabled)
    Drop-In: /usr/lib/systemd/user/pulseaudio.service.d
             └─kali_pulseaudio.conf
     Active: active (running) since Fri 2025-01-17 11:37:48 EST; 6s ago
...

As seen pulseaudio service is now running. This script makes it convenient to toggle between PulseAudio and PipeWire depending on the system’s needs without needing to reinstall or manually configure either sound server.

4. Conclusion

PipeWire is the new multimedia framework intended to replace PulseAudio and other frameworks. Some distributions already ship with PipeWire installed by default.

In this article, we learned how to replace PulseAudio with PipeWire. We removed the existing PulseAudio installation and installed the appropriate packages to run PipeWire seamlessly. Additionally, we’ve seen how to keep both PulseAudio and PipeWire and how to switch between them as needed seamlessly.