Skip to main content

Nico's blog

Volume Control in KDE Connect

Definitely one of the best use cases of KDE Connect is this: Watching a movie while laying in bed or on the couch and controlling your unreachable computer from your phone without needing to stand up. Thanks to our new media control notification you can pause and skip without even unlocking your phone. But what if the movie is too silent? If your media player is implementing the MPRIS2 specifications you are already lucky and can control your players volume from KDE Connect. Plasma Browser Integration even adds that for Browsers. But what if your player doesn’t support it (looking at you, Spotify)? Or you want to control your system volume? Then you are out of luck. Until soon. We are adding a plugin to KDE Connect that enables you to control your system volume from the phone. How cool is that? Another neat little trick is tapping on the volume icon to (un)mute the system. Screenshot_20180424-000806

Implementation

The idea for this has been around for a while, but it was not until fall 2017 that I actually started implementing it. The implementation relies on PulseAudio. We are aware that not everyone is using it, so we don’t require it and hope that packagers do not make KDE Connect depend on PulseAudio. The first implementation was using the command line tool pactl to set the volume. Assuming you only have one sink it was working pretty well, except there was no easy way to ask for the current volume, so our volume slider would be wrong pretty often. I got around this by parsing the output of pactl in a horrible, horrible line of bash, but it was clear that this was not stable enough to actually go into prod. Also it was still not working with multiple sinks. The logical step was to make use of the libpulse. For some reasons there are a great deal of people that like the C programming language, but I don’t. Especially not the library style that comes with it. What I really didn’t like was the idea of mixing low-level libpulse C code with high-level Qt code. Luckily, the Plasma guys thought the same. The Plasma Volume Applet contains code that was acting as a bridge between the two worlds. So I “borrowed’ that code (isn’t free software amazing?) and after some intense head-against-the-table banging (turns out both KDE Connect and Plasma PA both have a class ‘Device’ with the same include guard) it was working like a breeze, now even with multiple sinks.

Birth of a Framework

It was working really good, but the thought of having 20 files of duplicate code in two KDE projects wasn’t particularly appealing. We decided it would be best to extract the common code into a library that both projects can use. To make this possible the code needed quite some polish. The original author probably didn’t have in mind that it might become a library someday. After some work we now believe that the API can be declared stable and it can be released on KDE Extragear. We aim to make it an official KDE Framework once the code has matured enough. Extracting and releasing it as a library makes it easier for us to maintain it, but it also means that you can use it in your project! Furthermore, since it covers more use cases than just volume control it’s also possible to integrate more PulseAudio related features into KDE Connect easily. We are looking forward to your suggestions!

© Nicolas Fella, 2024 MastodonNicolas Fella
Powered by Hugo, theme Anubis.