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!

18 thoughts on “Volume Control in KDE Connect

  1. Thank you for all the great work KDE connect + plasma browser integration is a match made in heaven. Get a call and youtube is paused automatically! One thing I would like in the remote is if the area with the album art could also be a virtual touhpad. When I am using the remote there are some things I need a mouse for and I find myself switching back and forth. If the album area doubled as a mouse touchpad it would increase the usefulness and functionality of the remote immensely. Thanks again for such a great project.

    Like

  2. Can you tell me more about *Plasma Browser Integration*? What is it and where can I get it?
    I have been thinking about controlling youtube and netflix from kde connect for long. I even though of implementing the MPRIS spec for chrome in a plugin but I thought that the DBUS API wouldn’t have been available there. How does this Plasma Browser Integration work?
    (I am using Kubuntu 16.04.)

    Like

  3. Nice, I’ll have to have a look at that library, as I have one use case for it: A volume indicator on my keyboard, for which I reverse-engineered the protocol. It’s currently implemented by polling Alsa, which is no good, obviously (and I haven’t been using it for that reason). A nice advantage of modularizing this is that more backends could be added in the future 🙂

    Mmm… Did you think about implementing this volume control trough the device’s volume keys? It would be nice to change that setting from the main KDEConnect screen just by pressing the keys 🙂

    Like

  4. As this new library would allow to use more features of PulseAudio, one of them I would really appreciate is the possibility to change the audio output device used by an application running on my laptop from my smartphone.

    Here is my use case: I have KODI installed on a computer in my living room and on a tablet in my kitchen. Both are configured to be DLNA receivers. On my laptop, I use pulseaudio-dlna to see both KODI instances as audio output devices. When I listen to music from some websites on my laptop, I can use the plasma audio applet to indicate that the web browser must use one of the KODI instances as audio output devices (by drag&dropping the web browser app from the Applications tab in the plasma audio applet menu to one of the output devices in the Devices tab). Doing so, I can listen to the same music either in my living room or in my kitchen, just by switching the output device.

    If I could do that directly from KDE Connect on my smartphone, it would be awesome!

    Liked by 1 person

  5. Not to be too negative, but there’s a reason why a lot of people “like writing libraries in C”, and, among other things, it has to do with how binary compatibility works, which is crucial for libraries. You only have to go as far as to look up to what lengths Qt developers go to get stuff in C++ working that you basically get for free in C. I understand the sentiment, and i prefer writing in higher level languages myself given the choice, but don’t be dissing something you seemingly have little understanding of. Staying power of C is not an accident.

    Like

  6. As the original person who filed the issue for KDEconnect asking for global volume integration, I have to say THANK YOU! An often overlooked benefit of a free software community, is the tight feedback loop between users (as long as they’re prepared to file issues) and developers. It’s amazing to be able to spend a few minutes describing what I wish I had (with no ability to code it myself), and then one day just read in a blog that it’s been made real and will be coming to my devices soon.

    Amazing.

    Much gratitude to everyone involved!

    Like

  7. Yeah it’s cool that you can control multiple sinks, but there are two things that come to my mind, that should be implemented:
    – Indicate which sinks are used atm (volume > 0)
    – Route the audio from selected sinks (or all sinks) to android (doable with pulseaudio)

    Like

Leave a comment