
PulseAudio under the hood - daenney
https://gavv.github.io/blog/pulseaudio-under-the-hood/
======
kabes
It would appear that I'm one of the few fans of pulseaudio here. Although I've
only started using it since version 9, it always worked fine and we can do
quite complex workflows easily. An example scenario:

1\. We have multiple incoming RTSP stream where the audio is added as pulse
audio streams.

2\. We mix a couple of these streams.

3\. We send this stream to an external echo cancellation device.

4\. We take back the output of the echo cancellation device.

5\. We mix some more streams into it

6\. We route this to a certain output, which can be dynamically changed
according to a certain algorithm.

We control PA using DBUS and the above scenario is accomplished with only a
few calls. Granted, there's some latency you can't really control, but all in
all it's an incredible powerful system that's been working really well for us.

~~~
majewsky
Seconded. When Pulse works, it's really magical. Like when you go to the
hackerspace, connect to the Wifi, and the room speakers appear in your mixer
and Just Work.

~~~
jerf
So, this is an honest question, not an attempt to be cruel: How exactly do you
get that to happen?

Because my reaction when I read the linked article was that pulse sure does
seem to have a lot of features that sound pretty useful, but after years and
years of nominally having it on my system in charge of my audio hardware, I
have, to be blunt, absolutely no clue how to use any of those features, except
a bit about network audio streaming. None whatsoever. Didn't even have a hint
the support for those autoconf protocols existed.

I did once try to stream a Pulseaudio stream from one linux system to another.
I never got so much as a peep to go across. I have no idea where any
diagnostic messages may have gone that might help me debug this. I have no
access to a working configuration to compare to. I have no idea what I did
wrong and no idea how to fix it. I can find documentation that assumes you're
writing C extensions and I can find documentation on how to slide sliders in
the Gnome volume app, but I can't find anything in between. And I'm a
professional software engineer with a minor in electronic music; there are
certainly people with more knowledge and more motivation on these matters than
me, but on the flip side, features that I can't figure out or get working
might as well not exist to the vast majority of the population.

So while I'm not asking you to write a manual page, how exactly did you get to
the point where you can do that? Again, I'm serious; the previous paragraphs
are not to just slag on a project, but to show where I am and what problems
I'm having. I've got a project I'm trying to do where it would be really
useful to understand these things, but I have no idea even where to look at
this point. (I've poked in every Google search I can think of and gotten the
aforementioned C-API level docs or "I just installed Ubuntu, how do I volume?"
docs.)

~~~
c0l0
PulseAudio (PA) is modular, and you can load new modules implementing new
functionality at runtime via `pacmd` (and presumably other clients/config
tools).

One of these modules that come with any standard distribution of recent-ish PA
can use avahi/zeroconf (probably in part because avahi was Lennart P.'s
previous focus of attention, before he started work on PA) to advertise PA
sinks on the network. As a consequence, running the avahi daemon is required
for PA network-wide advertising of its presence to work, and auto-discovery to
have any chance of working at all. Another module implements accepting PA-via-
TCP compliant clients via TCP. Both are included in PA's standard
distribution, if it's recent-ish (think 2012 and later). Distros still might
package them in extra packages; the Debian does so for the zeroconf-parts in a
package named "pulseaudio-module-zeroconf".

To load the TCP transport support module and specify a primitive ACL (based on
source IPv4 addresses - you need to adapt this ACL to your local environment
if it uses another IP range!), you can use this `pacmd` stanza:

    
    
        load-module module-native-protocol-tcp auth-ip-acl=127.0.0.1;192.168.0.0/16 auth-anonymous=1
    

(A presumably more secure, cookie-based authentication mechanism with a shared
secret involved is also available, but I've never used it.) This will make
pulse open a listening TCP socket on port 4713. At this point, remote hosts
with PA-ready applications should already be able to play back sound by
setting the PULSE_SERVER environment variable to the accepting PA server's
address - what's still missing is the avahi-based advertising of the service.

So, to make PA contact your system's avahi daemon and use it to advertise its
sinks on the network, you load another module into your PA instance:

    
    
        load-module module-zeroconf-publish
        

If both these operations have succeeded (`pacmd` will complain loudly if they
don't), a quick `avahi-browse -a` on a (avahi-enabled) host in the same
network as the one with your exposed PA server in it should yield something
like this (pasted from my local network):

    
    
        + enp0s31f6 IPv6 pulse@nas                                     PulseAudio Sound Server local
        + enp0s31f6 IPv6 pulse@nas: Jukebox                            PulseAudio Sound Sink local
        + enp0s31f6 IPv6 pulse@nas: Dummy Output                       PulseAudio Sound Sink local
        + enp0s31f6 IPv4 pulse@nas                                     PulseAudio Sound Server local
        + enp0s31f6 IPv4 pulse@nas: Jukebox                            PulseAudio Sound Sink local
        + enp0s31f6 IPv4 pulse@nas: Dummy Output                       PulseAudio Sound Sink local
        + enp0s31f6 IPv4 root@tv                                       PulseAudio Sound Server local
    

That is two hosts advertising PA sinks on the network - "tv" (running
libreelec) and "nas" (running Debian).

Now, when you make another avahi-enabled host on the same network load the
"module-zeroconf-discover" PA module, the advertised sinks should magically
show up in that PA instance's list of available sinks. You can then move
streams onto them with any of the standard utils, like pavucontrol.

If you've found a set of stanzas that set up your PA instances to your liking
(with modules loaded the way your setup requires it), you can make them re-
apply on daemon startup by persisting them in your user-specific rc files in
~/.config/pulse/.

Hth! :)

~~~
jerf
In addition to the upvote already granted, I wanted to publicly thank you for
trying to answer my question, because I love it when other people are polite
this way. Thank you.

~~~
moonbug22
So, basically impossible for normal people.

------
mmjaa
I've never been a fan of PulseAudio, and always remove it and replace it with
Jack on my Linux DAW systems, so this article wasn't really of interest to me
.. until I started reading it. I think its the first time I've actually had
any respect for PulseAudio as a framework .. but I'll be darned if I'm going
to ever try to use it again. Jack just works so much better and with far less
fuss and overhead .. still, I'm yet to see a "Jack under the hood" article
nearly as impressive as this one.

~~~
clouddrover
> _but I 'll be darned if I'm going to ever try to use it again_

You probably won't have to. I think the long term plan is for Pipewire to
replace PulseAudio (and be an alternative to JACK). Pipewire also does video.
The initial release of Pipewire is video only with audio support to come:

[https://blogs.gnome.org/uraeus/2017/09/19/launching-
pipewire...](https://blogs.gnome.org/uraeus/2017/09/19/launching-pipewire/)

[http://pipewire.org/](http://pipewire.org/)

[https://github.com/PipeWire/pipewire](https://github.com/PipeWire/pipewire)

~~~
baldfat
> I think the long term plan is for Pipewire to replace PulseAudio

No that's not it at all. It's a framework that will use Pulseaudio OR Jack.
Jack is only for low latency needs AKA Professional audio production.

"Pipewire is used to build a modular daemon that can be configured to: be a
low-latency audio server with features like pulseaudio and/or jack.

[https://github.com/PipeWire/pipewire/wiki](https://github.com/PipeWire/pipewire/wiki)

~~~
jskkk
What about that quote makes it seem like it will use pulse audio or JACK
rather than acting as a replacement for them?

~~~
baldfat
Checked on their Freenode channel. They are looking at not total replacement
but will replace certain features.

------
drcross
It took me about a month of on/off attempts to get audio to stop routing
through HDMI every time my desktop came out of suspend and I still have a
basic understanding on how the audio system works on my machine. While I
praise the open source community for everything they've done it's still really
hard for even an advanced user to understand how things work. Its
flabbergasting at how it has reached this situation.

~~~
SwellJoe
That's odd. I found my Linux system always got HDMI audio right while Windows
always got it wrong. I seem to recall I was able to force VLC to go to the
right place by adding it to a config file, but the system sound never did work
right on Windows...I had to manually select HDMI every time it got plugged
back in.

~~~
nolok
You had the wrong default selected in Windows mixer.

Need to select it as default when plugged and whenever it gets replugged it
retake its priority. Works as expected with multiple default / temporarily
plugged devices.

Interface might need a bit of a clean up though.

~~~
SwellJoe
I think it was a buggy driver more than a Windows problem, as I tried
selecting it as the default. It sometimes wouldn't show up when re-plugged in,
as well. I'd even have to reboot sometimes to get it to show up as an option.

------
sohkamyung
Just a note that PulseAudio maintainer Tanu Kaskinen is on Patreon [1]. He
publishes monthly notes on PulseAudio changes there first and wouldn't mind
people supporting him via Patreon.

[1] [https://www.patreon.com/tanuk](https://www.patreon.com/tanuk)

------
Aardwolf
Ugh, the annoying PulseAudio that sometimes makes your volume suddenly much
louder or much softer when you want to change it by just one notch, just
because it wants to try to be "smart" with different applications or whatever
thing I don't care about it is that it tries to do, there should be 1 volume
slider and it should work correctly period (and without lag - another thing
PulseAudio introduced).

Oh how I miss the simplicity and elegance of before PulseAudio (and its evil
twin "hi-let-me-put-your-text-logs-in-a-binary-format"-systemd) appeared in
ArchLinux.

~~~
gcp
The issue with the volume is one of your desktop environment, not of
PulseAudio.

------
petepete
PulseAudio posts are always filled with naysayers; the vast vast vast majority
of users (like systemd in this regard) have no idea it's even running and just
get on with whatever they're doing.

I've primarily run Linux over the last fifteen or so years and barely had an
issue that's _directly_ PA's fault.

I remember the bad old days where a playlist would finish and then GAIM's
notification sound would bleat fifteen times. PA saw an end to that.

~~~
dom0
Firefox stopped supporting ALSA, so I had to install PA. PA gives me: a noise
chirp when the PA daemon starts, which is probably some uninitialized garbage
buffer. Sometimes (I don't know what triggers it, seems to be random, and no,
it's not the auto-suspend module which is on by default but seems to cause a
similar bug for many people) audio starts to lag by .3-.5 seconds. Restarting
PA does not help; have to reboot, which I can't/won't do for days at a time.
This is supposed to be a Linux system, not Windows 98.

I don't hate PA. On another machine it was always installed. But it is quite
clear to me that it is yet-another layer on top of the existing
infrastructure, so it _statistically_ will cause more issues than just the
infra that's there already.

~~~
majewsky
The problem with PA is that (it appears like) there aren't really any simple
errors. For most software, you have a bunch of common problems that you can
find on Stackoverflow easily with a reliable solution.

Pulse, on the other hand, while working fine on 99% of systems, usually fails
catastrophically in creative ways on the remaining 1% and it's really hard to
even understand what the problem is. (And of course, these 1% will shape
public opinion. A large portion of the 99% is not even aware they're using
Pulse.)

~~~
hashkb
Two nines isn't very many nines. I've had problems with PA 100% of the time.
Must be me, right? The only PA solution I know is purging it from my system.

~~~
throwaway2048
what people mean when they say the vast majority of people dont have problems
is "I don't care about your objectively broken experience"

------
madez
I use headphones, and have tactile transducers on my chair. The transducers
make the audio sound much louder than it actually is. That is good for your
ears and your neighbours.

Because of this setup I need full-range stereo output on my headphones and
low-passed LFE for the transducers.

How can I do this PulseAudio? When I enable lfe-remixing, I get full-range
output on my transducers and I hear voices in my chair. If I set lfe-
crossover-freq, I lose the bass on my headphones.

I found this patch

[https://pw-emeril.freedesktop.org/patch/171424/](https://pw-
emeril.freedesktop.org/patch/171424/)

which seems solve the problem, but it was rejected.

~~~
madez
I'd like to add that the patch was rejected because the maintainer had a use-
case in mind, deemed the feature not a good solution, and therefore rejected
the patch.

I would comment directly on the site, but because the certificate is invalid,
I cannot register an account.

I don't like the attitude to reject a patch just because one can't come up
with a use-case. Obviously there is a use-case, else the patch wouldn't have
been developed.

Also, the feature would be hidden in a configuration file that is meant for
advanced configuration anyways.

On top of that, I have a AV-receiver that does support all of this bass
management. For every channel I can configure individual crossover
frequencies, and I can but don't need to use a subwoofer. Why doesn't
PulseAudio give me these freedoms?

Why would a free software developer deny others easy ways, or any ways at all,
to configure their system? This is equivalent to the walled-garden approach of
many closed systems. Why does this occur in free software?

~~~
awalton
> Why would a free software developer deny others easy ways, or any ways at
> all, to configure their system?

Because of the "drive-by contributor" problem. Come by, drop off a patch that
solves exactly your problem, never show up again to maintain the feature, even
if it breaks, even if it's been broken for years.

That forces me, the developer, to either continuously test your patch (which I
may not be able to do if I don't have your
hardware/software/configuration/use-case-in-mind), or to shrug and hope it
works. We have to just trust your initial work and have it no way weigh in on
the future of our product?

We've learned too much as Open Source Software Engineers to just trust random
drive-by contributions. We've gone from default-accept-all to default-deny,
because at the end of the day that's what keeps quality in our software.

------
onli
He writes: _A rich API provides methods for inspecting and controlling all
available objects and their run-time and persistent properties. This makes it
possible to replace configuration files with GUI tools. Many desktop
environments provide such tools._

That is wrong, if I don't misunderstand it. It does not matter much for the
possibility of GUI tools whether they talk to an API or whether they parse and
write configuration files. That is stuff you abstract away.

It is of course possible that pulseaudio allows settings to be set that didn't
exist before. It's API might be better for that - but that doesn't say you
couldn't do the same with writing to configuration files if they had the same
capabilities, like targeting a specific application (one of the use cases he
mentions below).

~~~
kuschku
There is a difference. Writing to config files doesn’t set properties at
runtime.

PulseAudio needs to allow UI tools to set properties with sub-second latency.

And just reparsing the configs every time something is changed would be
extremely wasteful.

~~~
mrob
Why do you need sub-second latency for something you're unlikely to do more
than once a year?

~~~
kuschku
Because I change parts of the config via the UI many times a day. In my case,
because I have multiple audio cards and need to change several attributes.

~~~
onli
That's minuscule compared to how easy it is to parse config files vs to
construct and speak to API. The volume slider UX is a better argument. That's
the one thing I can think of where faster changes are indeed useful.

~~~
kuschku
It’s not volume sliding, but close to it. Having a button to move a running
audio stream from speakers to headphones to network playback on my phone is
amazing.

------
bandrami
Sigh.

Once upon a time, sound cards were files in the /dev tree. To play sound, you
wrote pcm data to the file representing a sink. To record, you read pcm data
from a file representing a source. Things were better then. I'm sure there are
people with use cases that have required the four (and counting!) solutions
crufted on since then, but I've never been one of them, and it irks me that
the interfaces get more and more complex and brittle with each iteration.

~~~
ro_sharp
I think the use cases you're describing are quite distinct from a "Linux on
the desktop" user. The audio experience for this kind of user (myself
included) has improved dramatically in the past few years.

~~~
bandrami
I'm a desktop Linux user; I preferred the old way. I do get that most people
like multiplexing (I don't; I want exactly one sound source at a given time),
and I'll grant that has improved.

I banged my head against my desk for a couple of days when Slackware switched
to Pulse with 14.2, allegedly because it was needed for bluetooth. I still
have difficulty believing that people actually use bluetooth for audio, but
apparently some people love it.

~~~
adambyrtek
> I still have difficulty believing that people actually use bluetooth for
> audio

So you don't believe that somebody would like to be able to use wireless
headphones?

~~~
bandrami
Exactly. Why would you trade something that doesn't require power, has the
most reliable, secure, and lossless transmission channel known, and is
"configured" by plugging in hardware for something that requires power, has a
less reliable transmission channel that can be easily snooped or forged, has
to be configured in software? I literally don't understand why anyone would
ever choose the latter.

~~~
milcron
I want to own exactly one wireless headset/microphone so that I can get up
from my computer without having to excuse myself from voice chat.

------
esaym
I've grown to really like PA. Originally I did not like PA because I thought
it used more system resources over raw ALSA since it obviously came with a
daemon running the background.. What I didn't know was that the libraries that
applications used to interface with ALSA had all computation for sound mixing
done in userland. So in a real world example, your audio player app using raw
ALSA might use 10% of your cpu while playing audio. But using the pulse
backend would cause your audio app to use 2% cpu with the pulse daemon using
3%. So its actually using less even though it is not obvious.

Once I started using PA, I kind of went crazy with it and started trying to
add PA support to old linux software that was still ALSA only. The library and
docs were not hard to use. The PA framework is all reference counted as well
so when you create the pa-objects in C, you never really need to worry about
memory allocation. It was pretty neat. Unfortunately, before I had all my
commits and patches ready, I changed jobs and never got the free time to
commit them the rest of the way.

------
sprash
Loads of complexity. I don't see the value.

Just give me files I can write data into and suddenly everything becomes very
easy. Everybody knows how to write files.

I know this discussion is getting really old but when Alsa was a step in the
wrong direction, Pulse was the highspeed train in the wrong direction that
followed, trying to "fix" it. This overengineered mess is the result of the
tragedy allowing former Windows users to do systems programming for Unix like
operating systems.

~~~
dsr_
PulseAudio is the only audio system I have used that can stop recognizing
output hardware (on the motherboard!) without a reboot. On the worst of its
competitors, you can be assured that once you get it working, it continues to
work at least until reboot, and usually until you change config or upgrade the
wrong package.

~~~
hnlmorg
I get that occasionally when putting my laptop to sleep with a HDMI cable
plugged in, then waking it up with the HDMI cable unplugged. I've found
killing the PulseAudio pid (systemd restarts it) usually fixes it. Weirdly
trying to restart it properly through systemd doesn't work but YMMV

This is on ArchLinux running KDE4

------
jancsika
Thanks! This looks at first glance to be a useful guide for understanding
Pulse. I plan on spending some time digging into it later.

Does anybody know if there's a similar such document for ALSA similarly
written by someone who isn't an ALSA insider?

------
dijit
Pulseaudio definitely needs to be demystified. As an average user it sometimes
feels like black magic that sometimes doesn't do the right thing. But in
recent years is been leaps and bounds better.

I'm not sure whether that's due to distribution maintainers giving very sane
configurations or that pulseaudio development has been focusing on sane
defaults.

Eitherway, if you're one of the people involved in giving me a audio
experience on Archlinux/Fedora, please pat yourself on the back because you're
doing fine work.

~~~
SwellJoe
I've got 20 years of hating audio on Linux at my back, so it's hard to let go
of the pain, of which a significant amount was contributed by Pulseaudio. But,
I have to agree that we finally (as of a couple years ago) have a good audio
experience on Linux. It's good enough to where I don't even think about it
anymore. I can even use pro-oriented audio software effectively.

I can also use old devices that don't work on Windows, anymore, of which I own
several (a keyboard and an audio interface I'd planned to get rid of, but
don't really need to now that I can do most things under Linux). Which is
kinda funny...now Linux is _more_ likely to work flawlessly with a given piece
of audio equipment than Windows.

If only an official REAPER port would come along (I know it'll run under WINE,
but there's also been rumblings of a Linux port for years, and I'd much rather
run something that works natively)...

~~~
squarefoot
Reaper for Linux is already there. 100% native,no WINE required, and works
great.
[http://wiki.cockos.com/wiki/index.php/REAPER_for_Linux](http://wiki.cockos.com/wiki/index.php/REAPER_for_Linux)

~~~
SwellJoe
Cool. I didn't find that last time I went looking for the current state of
REAPER on Linux, but I did find blog posts that indicated it was being worked
on. That's exciting news.

------
drewg123
I've had mixed experiences with Pulse. Although kind of flaky, it has been
super useful for me to handle audio from my Chrome browser that I run in a VM
and display via X. I'm not aware of any other way to pipe audio across the
network these days, other than the pulse tcp module.

------
jlgaddis
Related: "Launching Pipewire" [0], which is meant to (among other things)
eventually replace PulseAudio.

[0]: [https://blogs.gnome.org/uraeus/2017/09/19/launching-
pipewire...](https://blogs.gnome.org/uraeus/2017/09/19/launching-pipewire/)

~~~
digi_owl
GDIt. This, this is why Linux is not on the desktop. Constant CADT churn.

~~~
gcp
Like MMAudio, DirectSound, Kernel Streaming, WASAPI?

AudioTrack, OpenSL, whatever-the-Android-8-thing-is-called?

------
kutkloon7
I am quite surprised that PulseAudio has a bad reputation. I couldn't get ALSA
working on Arch Linux, and simply installing PulseAudio resolved all my
issues.

From the sentiment here I get the feeling that it might stop working any
moment.

~~~
Latty
It's mostly historical - Ubuntu shipped it as the default when it was still
unstable. This lead to a lot of people learning about it and lots of 'try
killing pulseaudio if something goes wrong' advice.

The reality now is it is very stable and has a huge number of powerful
features. Personally I think it is great, and have no issues with it.

~~~
makomk
Part of the problem is that the early releases were pretty bad - sometimes
even requiring patches that were only published in the Fedora package to work
correctly (like, they literally hadn't upstreamed by the developers at all at
the time). PulseAudio also didn't do bugfix-only releases, you either
backported or waited for the next major release and hoped it didn't break
anything else. They just released what I think is probably the first bugfix-
only release in the project's history a couple of days ago. This certainly
wasn't a case of the prior releases being so good they didn't need fixing
either.

~~~
kutkloon7
> PulseAudio also didn't do bugfix-only releases, you either backported or
> waited for the next major release and hoped it didn't break anything else.

Haha, that is ridiculous!

------
moonbug22
Tentacles, all the way down.

