Bug 415976 - KDEConnect increases bluetooth buffer size, leading to laggy sound in bluetooth headset via pulseaudio
Summary: KDEConnect increases bluetooth buffer size, leading to laggy sound in bluetoo...
Status: REPORTED
Alias: None
Product: kdeconnect
Classification: Applications
Component: common (show other bugs)
Version: 1.3.5
Platform: Gentoo Packages Linux
: NOR normal
Target Milestone: ---
Assignee: Albert Vaca Cintora
URL: https://gitlab.freedesktop.org/pulsea...
Keywords:
Depends on:
Blocks:
 
Reported: 2020-01-07 21:45 UTC by Florian Dittmer
Modified: 2020-01-10 21:44 UTC (History)
0 users

See Also:
Latest Commit:
Version Fixed In:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Florian Dittmer 2020-01-07 21:45:01 UTC
It took me many sleepless nights to figure this out - I hope it will help someone and/or result in a fix.

SUMMARY

I am using a bluetooth headset and an USB bluetooth dongle in a Pulseaudio KDE setup for audio. After recently doing a fresh Gentoo installation, I noticed audio lagging when playing gzdoom.

Starting pulseaudio in verbose mode (pulseaudio -v), I noticed that the following messages appear shortly after starting any audio stream (mpg123, gzdoom, youtube..) while having the bluetooth headset connected:

I: [bluetooth] module-bluez5-device.c: Changing bluetooth buffer size: Changed from 5120 to 6144
I: [bluetooth] module-bluez5-device.c: Changing bluetooth buffer size: Changed from 6144 to 7168
I: [bluetooth] module-bluez5-device.c: Changing bluetooth buffer size: Changed from 7168 to 8192

Trying out endless Pulseaudio options, I realized that this buffer increase only happens when the KDE desktop is running. Uninstalling and re-installing step-by-step all KDE software related bluetooth, I finally was able to identify KDEConnect as cause for these buffer changes. The buffer changes only happen when KDEConnect (compiled with bluetooth support) is running and connected to my phone.

Before discoverig this, I did not even know that KDEConnect has any bluetooth functions at all. My phone is connected to KDEConnect via a Wifi network. 

Compiling KDEConnect without bluetooth support solved my problem: Bluetooth buffer will not be increased anymore, and audio is not laggy anymore.




STEPS TO REPRODUCE
1. Install kdeconnect with bluetooth support (on gentoo:"USE=bluetooth emerge kdeconnect"), connect to a phone (LAN)
2. Connect Bluetooth headset to USB dongle (Pulseaudio KDE setup)
3. Play any audio stream on computer with output to bluetooth headset (e.g. mpg123, youtube, gzdoom)

OBSERVED RESULT

In the first ~15 seconds, audio will be in sync, not laggy.

Then, shortly after audio stream started, Pulseaudio will report that the buffer size was increased ("I: [bluetooth] module-bluez5-device.c: Changing bluetooth buffer size: Changed from ... to ...")

Audio will be out of sync with application and "laggy".


EXPECTED RESULT

The bluetooth buffer should not be increased. Especially if as in my I case, when not (knowingly/proactively) using any bluetooth functionality of KDEConnect.

As seen in PulseAudio bug report (https://bugs.freedesktop.org/show_bug.cgi?id=58746) and comments in the related source code of Pulseaudio, it is important to keep the buffer as small as possible in order to prevent audio lag.



SOFTWARE/OS VERSIONS
Linux/KDE Plasma: Linux sapphire 5.4.8-gentoo-x86_64 #1 SMP Sun Jan 5 16:18:12 CET 2020 x86_64 AMD Ryzen 7 3700X 8-Core Processor AuthenticAMD GNU/Linux

KDEConnect Vesion: kde-misc/kdeconnect-1.3.5:5::gentoo  USE="app bluetooth* handbook -debug -mousepad (-selinux) -test -wayland"
KDE Plasma Version: 5.17.4
KDE Frameworks Version: 5.64.0
Qt Version: 5.13.2

ADDITIONAL INFORMATION
Comment 1 Florian Dittmer 2020-01-08 23:33:40 UTC
The situation seems to be a bit more complex than I thought:

When I first started my desktop computer today (this is the device I experienced the audio lag), reinstalled KDEConnect with Bluetooth support and tried to reproduce the issue (prior to applying your code), nothing happened: No lag, no buffer increases anymore.
 
However, after booting up my second computer (a laptop also running KDE, KDEConnect and with an enabled bluetooth adapter), the buffer increases started on the desktop computer again when playing audio.

Watching the bluetooth systray of KDE on both computers, I noticed that both systems connected or tried to connect each other via bluetooth from time to time. Whenever this happened for the first time while playing audio, the buffer increased like this:

I: [bluetooth] module-bluez5-device.c: Changing bluetooth buffer size: Changed from 5120 to 6144 
I: [bluetooth] module-bluez5-device.c: Changing bluetooth buffer size: Changed from 6144 to 7168 
I: [bluetooth] module-bluez5-device.c: Changing bluetooth buffer size: Changed from 7168 to 8192

Now I am not sure if the KDEConnect tool is responsible for this after all, even though during my tests yesterday it was clearly related.

Anyway, I somehow managed to stop the automatic pairing tries of the systems and now the system behaviour regarding audio playback seems to be ok (even with KDEConnect with Bluetooth support running). BUT whenever I manually I press the "Connect" button in the KDE bluetooth systray applet to connect the desktop to the laptop via bluetooth while playing audio, I will immediately  reproduce the same 3 buffer increases. Since I usually don't use this function, and somehow was able to stop the automatic connection attempts, the problem seems to be solved for me right now.

Meanwhile, a pulseaudio developer told me that the buffer increases happen  because there is some delay in the audio, so it is a reaction to the delay, not the cause. And he suggested to me to change the line
size_t new_write_block_size = u->a2dp_codec->reduce_encoder_bitrate(u->encoder_info, u->write_link_mtu);
to
size_t new_write_block_size = 0;
in module-bluez5-device.c

I tried this out while the systems were still in the state that they tried to connect each other via bluetooth from time to time.

So, whenever this happened, the audio would stutter for a short time, before continuing to play normally - until the next connection attemp, when it stuttered again. However, this way the audio stayed in sync with the videogame. 

In general, I think the original pulseaudio behaviour, to increase the buffer size, seems to be good for the use case of listening music. Because it will only stutter once, then increase the buffer, and be OK for the rest of the time. (but might be out of sync with video or game)

The modified behaviour is not perfect because of regular stuttering that might occur, but that might be acceptable for gaming, because at least this way the audio will stay synchronized with the game.
 
I hope this documentation will help others experience similar issues.

This might not be a KDEConnect bug, but maybe it could be a general bug in the KDE bluetooth system (after all, just pressing the "Connect" button in the bluetooth systray applet to try to connect a different device will reproduce this) or just a general bluetooth software or hardware problem.
Comment 2 Florian Dittmer 2020-01-10 21:44:46 UTC
FYI, after discussion with a pulseaudio developer, I opened a bug report here:
https://gitlab.freedesktop.org/pulseaudio/pulseaudio/issues/791
I guess this topic will be further covered there.