Illegitimi non carborundum


KDE + PulseAudio != Sucks

So I've been spending altogether far too much time on getting this stuff working, but it's finally in a state where it no longer sucks.

For several releases we (Mandriva) have been patching KDE's phonon support to hide all the real devices if PulseAudio is used because the GUI really makes no sense (PulseAudio handles all the routing for us). This is an acceptable solution but it's far from ideal.

So with the latest set of three patches (one for pulseaudio, one for phonon and the final one for kdebase4-runtime) I now have a fully working system (albeit with some caveats!).

So how does it all work? Well The first step is module-device-manager. This is a pulseaudio module that does the following:

  • Store all devices we've ever seen and keep track of their description and their icon.
  • Provide a protocol extension to allow clients to query this database.
  • Implement a per-role priority list for devices (again exposing it via the protocol)
  • Implement a routing policy that will always uses the highest available device for streams with the relevant role.

With that in mind, I then patched Phonon to support querying this database and editing the priority list. Normally (e.g. when pulse is not in use) phonon will keep all it's configuration in a simple config file, so I've overridden this when pulse is detected (which now happens via trying to connect to the server, rather than relying on Mandriva specific configs) to instead use our little database.

The final step is a patch to kdebase4-runtime. There was some copied code kicking around which caused a lot of issues with this patch. In the end I had to export a new config class from phonon in order to make this all work (I don't want the KCM tweaking the config file as it will not be used if pulseaudio is in use... so by exporting the config class everying is kept abstracted).

All in all, this means that:

  • All the sinks/sources from PulseAudio are shown in the System Settings -> Multimedia dialog
  • You can order the devices and this will be honoured and saved to pulseaudio. Audio will be routed according to this preference.
  • Devices that are unavailable will be shown greyed out.
  • When devices appear/disappear, they will be automatically used if they are higher up the priority list than what's currently being used.

But with all good news there is some bad news! So what's not working?

  • Categories are not currently mapped to roles, so the only category list that works is the "default" one (although some applications may have their role guessed by PulseAudio because it parses the data in an applications .desktop file.
  • Testing a device will not test the specific device you've selected. PulseAudio handles all the routing, so there is no way to force a given device to be used. I'm not sure how to handle this ultimately.
  • Doesn't handle a PulseAudio restart (aka a crash and respawn!)

The phonon patch is being reviewed for upstream inclusion but the API exposed is liable to change, so these patches are not final. Likewise module-device-manager is not yet final and may yet undergo some API changes. But that aside, this is IMO great progress even if I do say so myself!

And what is a blog post without a screenshot eh? OK here you go:


All my git repositories can be found here.

Share and Enjoy:
  • Digg
  • StumbleUpon
  • Facebook
  • Yahoo! Buzz
  • Twitter
  • Google Bookmarks
  • Slashdot
  • Pingback: Akari (akari) 's status on Friday, 02-Oct-09 00:18:11 UTC -

  • Pingback: Lennart Poettering: Conferences | TuxWire : The Linux Blog()

  • aapgorilla

    I don’t have my second card now but can it now be controlled with the volume hotkeys or the kmix tray applet?

    • Colin

      I’ve not touched the kmix stuff so this work is independent of that. I doubt I’ll have time to look at kmix before 2010 is out, so I’m afraid this is unlikely to change from the current state of affairs.

    • aapgorilla

      interestingly in kmix->help->hardware info it says:
      Sound drivers supported: ALSA + OSS + PulseAudio
      Sound drivers used: ALSA

      Now how do I get it to use pulseaudio :s

    • Colin

      Yeah, Kmix’s PulseAudio support is not enabled by default. If I get time I’ll take a look but I’m not sure I will….

    • KMIx’s PulseAudio support doesn’t work yet, at least in 4.3. Most of the functions are dummy or commented out.

  • Thank you for putting in all the effort on this, it is greatly needed.

    Regarding the TODO’s – I do have to kill pulseaudio more than I’d like, so at this stage handling that would be useful. Regarding testing a device – can you send a ‘mute’ to all devices except the target, play the test, and then unmute the others?

    • Colin

      Hi. Sadly the mute others option wont work here. The priority list itself is used, and thus the test sound will only ever come out of the highest priority available device.

      I’ve disabled the routing at the phonon level which does break part of the API, but I’ll think about how I can restore that. It shouldn’t be too difficult really.

  • > Testing a device will not test the specific device you’ve selected. PulseAudio handles all the routing, so there is no way to force a given device to be used. I’m not sure how to handle this ultimately.

    Some ideas:
    * set device to test as default (the same way you tell PulseAudio about the device preferences the user sets), play test sound, restore default?
    * open stream, tell PulseAudio to move it to the sink you want (pavucontrol can do that, so there must be some API for it), then start the actual playback?

    • Colin

      Hi, Rejiging the priority is an option, but to do that would require that the KDE part was aware of pulseaudio. The problem is not getting pulse to move the stream to the appropriate device (as you said that bit is easy) but rather how to get the phonon API to do it nicely. I’ve off-loaded the routing to pulseaudio so currently the operations to move streams to appropriate device in phonon are NOOPs when pulse is used. But this principle of operation only applies to general routing, and specifically moving a single stream to a specific device *via the phonon API* is the bit I’ve not got working yet.

      The solution wont be too hard, so don’t despair 😀 I’ve just not gotten round to it yet.

  • Pingback: Lennart Poettering: Conferences |

  • Markus

    Looks nice.
    May I suggest that you add your blog to PlanetKDE? (At least the kde tagged posts)

    • Colin

      Yeah someone else suggested that too. I’ll look into it.

  • I really appreciate all your time and effort you put into this and I can’t wait seeing it implemented in Cooker. But the real test will be how all this works with the applications. As I stated before, users do not care much about the technical details, they want their amarok, kaffeine, browser plugins and Skype (and some of them also their games) to play the sound out-of-the-box. I’m looking forward to testing.

    • Colin

      Indeed 🙂 In my testing so far, Amarok and Kaffine work nicely, and I can easily use only KDE interfaces to tell Amarok to output to my Airport Express when it’s available which is certainly an improvement 😀

  • Dr.Who

    I’d like to thank you Colin for your hard work. Whilst PulseAudio has had it’s ups and downs in it’s short life, it is considerably better than ALSA, at least you can easily route certain sounds to certain sound cards (PCI or USB), and no longer limited to all sounds coming out of one device (short of voodoo coding). Oh, and unlike ALSA, you don’t have to hand edit config files to get a new sound device to work (those were the days).

    I’m pleased that FINALLY Skype got their finger out of their backside to support Pulse Audio, but it’s not perfect yet.

    • Colin

      Thanks dude 🙂 With regards to the Skype support, I’ve been working with the Skype developers to improve their support. The next beta will contain some bugfixes I helped to fix and better tagging of the audio streams so that the routing works even better. Currently I’m able to answer a skype call, flip open my bluetooth headset and have the input and output streams automatically jump across to that device automatically. Things are definitely getting better :p

  • KDS

    This is great! thanks very much for your work!

    KDE needs to work with PA better and you are making this happen!

  • ano

    Thanks for all your work! PulseAudio is a real improvement for Linux audio. I use Mandriva cooker with KDE4 and VirtualBox3. I’m glad that VirtualBox can use PulseAudio. But there is a problem when I star Windows through VirtualBox and I’ve sound through Windows, then the sound in Amarok doesn’t work (when I start amarok after the virtualbox session), sound in flash on firefox still works, I think this is strange and annoying. Maybe you can look into this?

  • Pingback: So how does the KDE PulseAudio support work anyway? «

  • Pingback: NEWS » KMix PulseAudio Integration()

  • Pingback: Mix it up «

  • Pazuzu

    And what about how does PulseAudio support games? Have you tried it? I suggest OpenArena, which crashed on every distro using PA I have tried

    • Colin

      I’m not aware of any specific issues, but in order to address any incompatibilities we need good bug reports, with solid information and backtraces of crashes (with full debug symbols) where appropriate. Open Source projects need feedback to thrive. If people don’t tell us something is wrong, then as far as we’re concerned nothing is broken and obviously nothing is going to be done about something that isn’t broken!

  • skierpage

    It seems your fine enhancements did not make it into Kubuntu 10.10 which defaults to Phonon > Xine > PulseAudio. My two audio devices show up as identically named “Internal Analog Audio Stereo” in System Settings > Multimedia > Phonon ; PA decided to mute one yet KMix shows it unmuted; if I change the device.descriptions in pacmd, System Settings and KMix don’t update, etc. It’s taken me three hours to figure out what to put in that should make audio work.

    • Colin

      I think the only enhancement that didn’t make it was the “Speaker Setup” GUI. I believe all other work is in there (with the possible exception of a couple of patches I kinda “lost” not that long ago :s)

      We’ve discussed this now in an upstream bug report but I think this results from a bug in the way PA detects Internal Audio. IMO this could be handled more gracefully, but not sure how best to do it (I’m not really an expert in that bit of the code). The “deciding to mute” scenario is interesting tho’. Not quite sure what the reason for that would be but I suspect it’s something non-obivous. I’d be very interested to get the “pacmd list” output for this muted device – and a screenshot from kmix at the same time.

      What did you put in to make things work? I wouldn’t have thought that any solution to this problem needed a change to (tho’ admittedly the GUIs to e.g. change the name are not easily found. which I need to address in some capacity)

  • I think pulseaudio is just to much of a mess. The interface is messy the way it handles audio can become really messy if you have a system with multiple sound cards.

    I believe the interface needs a ground up redesign to make it better.

    • Colin

      What do you mean by the “interface”? Do you mean GUI? If so, then I’ve integrated PA into KDE using the existing GUI in KDE. It in no way relates to PA itself, so to use this as an argument against PA is invalid.

      If by “interface” you are referring to pavucontrol, then it’s perfectly reasonable to make this suggestion but then we do not “market” pavucontrol as anything other than a relatively low level app to expose the functionality of PA to users. We don’t really care about it from a UI design point of view as we expect the Desktop Environment used to provide their own interfaces, integrated tightly into the Desktop Experience itself.

      If you meant something else by “interface” then please elaborate so we can discuss properly. But without more details, I cannot really comment.

      Just for complete reference and to make sure you’re not being short changed by your distro’s support for KDE+PA, this is what it should look like: