Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MPV/MPG123 Buffer Stuttering Audio #748

Open
rogerxxxx opened this issue Jan 14, 2025 · 10 comments
Open

MPV/MPG123 Buffer Stuttering Audio #748

rogerxxxx opened this issue Jan 14, 2025 · 10 comments

Comments

@rogerxxxx
Copy link

Have been having long time problems with stuttering bluetooth audio after the bluetooth device/client has been power cycled on/off. Upon the bluetooth (automatically) reconnecting to the bluetooth server and playing audio using mpg123/mpv, audio then becomes stuttered/pops.

$ xbps-query -s bluez

[] bluez-5.78_1 Bluetooth tools and daemons
[
] bluez-alsa-3.1.0_1 Bluetooth Audio ALSA Backend
[] bluez-deprecated-5.78_1 Bluetooth tools and daemons - deprecated tools
[
] libbluetooth-5.78_1 Library to use the Bluez Linux Bluetooth Stack

Having some time for debugging finally after many years and happening upon some debug incantations whereas seemingly little easy debugging instructions (eg. aplay -D bluealsa:SRV=org.bluealsa,DEV=XX:XX:XX:XX:XX:XX,PROFILE=a2dp some.wav, and with mpg123/mpv using "-a"), I found the likely reason(s) for the playback stuttering when using mpv -v:

[ao/alsa] buffersize: 4410 samples
[ao/alsa] period size: 1102 samples
[ao/alsa] device buffer: 4410 samples.
[ao/alsa] using soft-buffer of 22050 samples.

And happened upon a working instance fluke when mpv happened to playback fine or without stuttering:

[ao/alsa] buffersize: 4800 samples
[ao/alsa] period size: 1200 samples
[ao/alsa] device buffer: 4800 samples.
[ao/alsa] using soft-buffer of 24000 samples.

Now setting mpv "--alsa-buffer-time=0 -v" mpv plays with no stutter too!

ao/alsa] buffersize: 16384 samples
[ao/alsa] period size: 4096 samples
[ao/alsa] device buffer: 16384 samples.
[ao/alsa] using soft-buffer of 22050 samples.

Mpg123 was also affect by playback stuttering and popping, and "--devbuffer 30" or "--devbuffer 60" significantly improves playback without stuttering however for intermittent popping and seemingly "--devbuffer 120" seems about right with no apparent popping noises.

So, long story short, is this a known bug fixed within later versions of ALSA Bluez, or likely not fixed? I have already tried compiling a recent version of bluez-alsa Void Linux package, so far 4.0 compiled but encountered subsequent dbus bugs, and likely as to why Void Linux developers elected not upgrading, likely due to incompatibility issues with the current Void operating system and/or possibly fluctuating compile/install routine.

Surprised nobody has written any really good debugging/troubleshooting webpages for Linux bluetooth.

@borine
Copy link
Collaborator

borine commented Jan 17, 2025

is this a known bug fixed within later versions of ALSA Bluez,

Very likely it is; although blueez-alsa v3.1.0 is now 4 years old and I do not remember clearly that far back. If you really need an answer to this question then you must search through the issues for mpv or mpg123 to see if this issue has been reported previously.

Given that you are using a distribution package, your first course of action should be to raise an issue with that distribution so that the package managers are made aware. They can then decide whether to upgrade to a more recent commit, or to produce their own patch to the old code.

@rogerxxxx
Copy link
Author

You're on the ball too, and I already did search for mpv/mpg123 issues within the github.com bluez-alsa closed/open/etc issues. I pretty much found nothing, along with searching google.com. Regardless, this bug is surely now well documented! :-)

And, already did make a post on the non-official Void Linux reddit.com site, posting bluez-alsa version was significantly out-dated. (Didn't get much feedback, if any at all.) However, like I stated, I'm guessing those packaging ran into packaging problems, as I could almost drop-in (compile via Void Linux xbps-src packaging) bluez-alsa-4.0 version (not being the latest version), but ran into further dbus problems possibly not easily/readily resolved. When things get finicky rather than simplified (eg. dbus complexity), people readily loose interest!

Go ahead close this bug, and I'll post/follow-up once either I install a recent version, or more likely Void Linux pushes a newer version... if I'm still alive. Guessing, most using bluetooth will likely just use pulseaudio, while those not depending on bluetooth devices will just use ALSA.

Usually when packages get stable, or the build scripts become stable, nice just having drop-in and compile/install packages/build scripts. Also makes debugging or falling back to earlier versions from sources extremely easier.

@arkq
Copy link
Owner

arkq commented Jan 18, 2025

If you have bluez-5.78 then definitively you should go for latest bluez-alsa (master) or at least 4.3.1.

but ran into further dbus problems possibly not easily/readily resolved

If you have some problems with D-Bus, it's most likely due to incorrect configuration file or config file being not installed at all. The easiest way to go forward is to uninstall distribution version of bluez-alsa, get fresh copy of master, configure it (without any additional options, for start) and install it (just like in the https://github.com/arkq/bluez-alsa/blob/master/INSTALL.md guide). If that will succeed, then you can try to enable some additional features.

After installation you should have something like:

$ cat /usr/share/dbus-1/system.d/org.bluealsa.conf
<!-- This configuration file specifies the required security policies
     for BlueALSA core daemon to work. -->

<!DOCTYPE busconfig PUBLIC "-//freedesktop//DTD D-BUS Bus Configuration 1.0//EN"
 "http://www.freedesktop.org/standards/dbus/1.0/busconfig.dtd">
<busconfig>

  <!-- ../system.conf have denied everything, so we just punch some holes -->

  <policy user="root">
    <allow own_prefix="org.bluealsa"/>
    <allow send_destination="org.bluealsa"/>
  </policy>

  <policy group="audio">
    <allow send_destination="org.bluealsa"/>
  </policy>

</busconfig>

Then make sure that your user is added to the audio group. Also, try to search for any other D-Bus system configuration files which might define something for org.bluealsa (use ripgrep or recursive grep in directories /etc/dbus-1 and /usr/share/dbus-1). If you will find some, remove it (or move it somewhere else if you want to keep it).

I'm not sure what hardware you are using, but just in case double check that Bluetooth on your system (source, sink, or both) does not perform any scans:

$ bluetoothctl show |grep Discovering
        Discovering: no

@rogerxxxx
Copy link
Author

I have most of the previously mentioned already.

Void Linux seems to be installing the dbus file into /etc/dbus-1/system.d/bluealsa.conf, where all the other /etc/dbus-1/ files are. (The file is mostly identical except for user root, likely an older bluez-alsa included version.) I just have a really tough time overall understand the entire dbus system, with udev already taxing my thinking skills.

Discovery is showing no, user already within the audio group.

Think we're getting side-tracked with this stuttering bug, posting software packaging bug(s) atop of the stuttering bug. Granted, good info trying to display how easily upgrading is, just no free time here for playing, why I moved from Gentoo (source-based) Linux distribution to a binary (Void Linux) distribution.

When I get a little free time and remember to, I'll retry rolling/compiling another (current stable version) bluez-alsa Void Linux package; for experimenting where the hang-up/delay is with the Void Linux package upgrading of bluez-alsa package, aside from click-n-play users being distracted by preferring pulse-audio, etc.

@arkq
Copy link
Owner

arkq commented Jan 19, 2025

Think we're getting side-tracked with this stuttering bug, posting software packaging bug(s) atop of the stuttering bug.

Not at all. The case is that since you are using old version of bluez-alsa, I'm not going to investigate the issue unless I'm sure that this bug still appears in the current master branch. This project is privately funded (by my and other active developers, which is @borine, spare time - and time is money) with warranty "rules" defined by MIT license: the software is provided "as is", without warranty of any kind. Anyway, as spare time allows, we are trying to answer questions and fix bugs which are discovered by others. Sometimes, we are even developing new features which are asked by others!

aside from click-n-play users being distracted by preferring pulse-audio

And to be honest that's what we are encouraging regular users to do - use PulseAudio (or PipeWire nowadays). These projects have much more funds, so community around them is a better choice to solve Linux general Bluetooth issues/questions. However, bluez-alsa should work for most cases and for users who have some ALSA knowledge and know what they want - UNIX-like Bluetooth integration with ALSA without sound server (this part is crucial) - bluez-alsa might be a perfect choice.

So, saying all this, I'm really encouraging you to try bluez-alsa current master. Prior-knowlege of D-Bus is really not required here. If master branch does not work, simply post logs from bluealsad (configure project with --enable-debug) or mpv/mpg123. Without actual logs, I'm afraid that there is nothing more that we can do.

@rogerxxxx
Copy link
Author

The "configure project with --enable-debug" was another item I needed for proper debugging!

I usually only briefly scan "configure --help", probably like most other packagers!

@rogerxxxx
Copy link
Author

rogerxxxx commented Jan 20, 2025

After compiling 4.3.1 release, manually entering a "/usr/share/dbus-1/system.d/org.bluealsa.conf", keep getting an error:

$ aplay -D bluealsa:DEV=FF:XX:XX:XX:XX:XX,PROFILE=a2dp,SRV=org.bluealsa /tmp/test.wav
ALSA lib bluealsa-pcm.c:1057:(_snd_pcm_bluealsa_open) Couldn't get BlueALSA PCM: No such method “GetPCMs”
aplay: main:850: audio open error: No such device

$ ~/src/bluez-alsa/bluez-alsa-4.3.1.mine/utils/aplay/bluealsa-aplay -l
**** List of PLAYBACK Bluetooth Devices ****
hci0: [JBL Charge 5], trusted audio-card
A2DP (SBC): S16_LE 2 channels 44100 Hz
**** List of CAPTURE Bluetooth Devices ****

$ ~/src/bluez-alsa/bluez-alsa-4.3.1.mine/utils/aplay/bluealsa-aplay -L
bluealsa:DEV=FF:XX:XX:XX:XX:XX,PROFILE=a2dp,SRV=org.bluealsa
JBL Charge 5, trusted audio-card, playback
A2DP (SBC): S16_LE 2 channels 44100 Hz

My only guess is, bluez-alsa package needs to be fully installed within /usr, with the original bluez-alsa package being uninstalled, as well as just stopping the service/daemon.

After uninstalling the distribution bluez-alsa package, installing (make install), still get an error:

$ aplay -D bluealsa:DEV=FF:XX:XX:XX:XX:XX,PROFILE=a2dp /tmp/test.wav
ALSA lib pcm.c:2722:(snd_pcm_open_noupdate) Unknown PCM bluealsa:DEV=FF:XX:XX:XX:XX:XX,PROFILE=a2dp
aplay: main:850: audio open error: No such file or directory

src/bluealsa -p a2dp-source
bluealsa: [10928] D: a2dp-aac.c:712: FDK-AAC encoder capabilities: aac=0x410b1 sbr=0x22
bluealsa: [10928] D: storage.c:90: Initializing persistent storage: /usr/local/var/lib/bluealsa
bluealsa: [10928] W: storage.c:93: Couldn't create storage directory: No such file or directory
bluealsa: [10928] D: main.c:663: Starting main dispatching loop
bluealsa: [10928] D: main.c:118: Acquired D-Bus service name: org.bluealsa
bluealsa: [10928] D: bluealsa-dbus.c:358: Registering BlueALSA D-Bus manager: /org/bluealsa
bluealsa: [10928] D: bluez.c:153: Registering media application: /org/bluez/hci0
bluealsa: [10928] D: bluez.c:743: Exporting media endpoint object: /org/bluez/hci0/A2DP/AAC/source/1
bluealsa: [10928] D: bluez.c:743: Exporting media endpoint object: /org/bluez/hci0/A2DP/AAC/source/2
bluealsa: [10928] D: bluez.c:743: Exporting media endpoint object: /org/bluez/hci0/A2DP/SBC/source/1
bluealsa: [10928] D: bluez.c:743: Exporting media endpoint object: /org/bluez/hci0/A2DP/SBC/source/2
bluealsa: [10928] D: bluez.c:1393: Signal: org.freedesktop.DBus.ObjectManager.InterfacesRemoved(/org/bluez/hci0/dev_FF_XX_XX_XX_XX_XX, ...)
bluealsa: [10928] D: bluez.c:1290: Signal: org.freedesktop.DBus.ObjectManager.InterfacesAdded(/org/bluez/hci0/dev_FF_XX_XX_XX_XX_XX, ...)
bluealsa: [10928] D: bluez.c:1290: Signal: org.freedesktop.DBus.ObjectManager.InterfacesAdded(/org/bluez/hci0/dev_FF_XX_XX_XX_XX_XX/sep1, ...)
bluealsa: [10928] D: bluez.c:1362: Adding new Stream End-Point: FF:XX:XX:XX:XX:XX: SNK: SBC
bluealsa: [10928] D: bluez.c:1290: Signal: org.freedesktop.DBus.ObjectManager.InterfacesAdded(/org/bluez/hci0/dev_FF_XX_XX_XX_XX_XX/sep2, ...)
bluealsa: [10928] D: bluez.c:1362: Adding new Stream End-Point: FF:XX:XX:XX:XX:XX: SNK: SBC
bluealsa: [10928] D: dbus.c:47: Called: org.bluez.MediaEndpoint1.SelectConfiguration() on /org/bluez/hci0/A2DP/SBC/source/1
bluealsa: [10928] D: bluez.c:429: A2DP peer capabilities blob [len=4]: ffff0228
bluealsa: [10928] D: bluez.c:1290: Signal: org.freedesktop.DBus.ObjectManager.InterfacesAdded(/org/bluez/hci0/dev_FF_XX_XX_XX_XX_XX/sep1/fd5, ...)
bluealsa: [10928] D: dbus.c:47: Called: org.bluez.MediaEndpoint1.SetConfiguration() on /org/bluez/hci0/A2DP/SBC/source/1
bluealsa: [10928] D: a2dp-sbc.c:536: SBC: Selected bit-pool range: [2, 40]
bluealsa: [10928] D: storage.c:123: Loading storage: /usr/local/var/lib/bluealsa/FF:XX:XX:XX:XX:XX
bluealsa: [10928] D: bluez.c:572: A2DP Source (SBC) configured for device FF:XX:XX:XX:XX:XX
bluealsa: [10928] D: bluez.c:575: A2DP selected configuration blob [len=4]: 11150228
bluealsa: [10928] D: bluez.c:577: PCM configuration: channels: 2, sampling: 48000
bluealsa: [10928] D: bluez.c:743: Exporting media endpoint object: /org/bluez/hci0/A2DP/SBC/source/3
bluealsa: [10928] D: bluez.c:1290: Signal: org.freedesktop.DBus.ObjectManager.InterfacesAdded(/org/bluez/hci0/dev_FF_XX_XX_XX_XX_XX/player0, ...)
bluealsa: [10928] D: bluez.c:1480: Signal: org.freedesktop.DBus.Properties.PropertiesChanged(): org.bluez.MediaTransport1: Volume
bluealsa: [10928] D: bluez.c:1498: Skipping A2DP volume update: 32

@arkq
Copy link
Owner

arkq commented Jan 21, 2025

ALSA lib pcm.c:2722:(snd_pcm_open_noupdate) Unknown PCM bluealsa:DEV=FF:XX:XX:XX:XX:XX,PROFILE=a2dp

It seems that the ALSA configuration was not installed properly. Or it was... Check whether you have directory /etc/alsa/conf.d and what is inside that directory. If you have (mostly) symlinks inside, and there is no 20-bluealsa.conf in it, add a symlink there to /usr/share/alsa/alsa.conf.d/20-bluealsa.conf (if such file exists). So you will have:

$ ls -l /etc/alsa/conf.d
...
lrwxrwxrwx 1 root root  44 Jan 1 2020 20-bluealsa.conf -> /usr/share/alsa/alsa.conf.d/20-bluealsa.conf

@rogerxxxx
Copy link
Author

rogerxxxx commented Jan 21, 2025

Now that I likely had the tarball manually built and possibly working, decided returning to making the packaging script for Void Linux.

Now get the following error:

bluealsa -p a2dp-sink
bluealsa: E: Couldn't acquire D-Bus name. Please check D-Bus configuration. Requested name: org.bluealsa

/usr/share/dbus-1/system.d/
/usr/share/dbus-1/system.d/bluealsa.conf

Tried restarting just dbus service, then restarted the entire computer...

Looking at prior bug/issues, recompiled with "--user" configure options:

configure_args="--enable-aac --enable-aptx --with-libopenaptx
--enable-manpages --disable-debug --disable-hcitop
--with-bluealsauser=bluealsa --with-bluealsaaplayuser=bluealsa"
hostmakedepends="pkg-config automake libtool python3-docutils"

Also presuming the "-p a2dp-sink" executable option is correct, with not needing the "-p a2dp-source" executable option.

Worked around that with changing the user to the Void Linux preferred/suppoed "_bluez_alsa" user from version bluez-alsa-3.1.0_1 package. And the bluealsa daemon is now starting.

I'm doing so manually via root command line:

chpst -u _bluez_alsa:audio bluealsa -p a2dp-sink
bluealsa: [9587] D: a2dp-aac.c:800: FDK-AAC decoder capabilities: aac=0x1a4ffff sbr=0x1bf dmx=0x7f
bluealsa: [9587] D: storage.c:90: Initializing persistent storage: /var/lib/bluealsa
bluealsa: [9587] D: main.c:663: Starting main dispatching loop
bluealsa: [9587] D: main.c:118: Acquired D-Bus service name: org.bluealsa
bluealsa: [9587] D: bluealsa-dbus.c:358: Registering BlueALSA D-Bus manager: /org/bluealsa
bluealsa: [9587] D: bluez.c:153: Registering media application: /org/bluez/hci0
bluealsa: [9587] D: bluez.c:743: Exporting media endpoint object: /org/bluez/hci0/A2DP/AAC/sink/1
bluealsa: [9587] D: bluez.c:743: Exporting media endpoint object: /org/bluez/hci0/A2DP/AAC/sink/2
bluealsa: [9587] D: bluez.c:743: Exporting media endpoint object: /org/bluez/hci0/A2DP/SBC/sink/1
bluealsa: [9587] D: bluez.c:743: Exporting media endpoint object: /org/bluez/hci0/A2DP/SBC/sink/2
bluealsa: [9587] D: bluez.c:1393: Signal: org.freedesktop.DBus.ObjectManager.InterfacesRemoved(/org/bluez/hci0/dev_F8_5C_7E_2F_CA_CA, ...)
bluealsa: [9587] D: bluez.c:1290: Signal: org.freedesktop.DBus.ObjectManager.InterfacesAdded(/org/bluez/hci0/dev_F8_5C_7E_2F_CA_CA, ...)
bluealsa: [9587] D: bluez.c:1393: Signal: org.freedesktop.DBus.ObjectManager.InterfacesRemoved(/org/bluez/hci0/dev_F8_5C_7E_2F_CA_CA, ...)
bluealsa: [9587] D: bluez.c:1290: Signal: org.freedesktop.DBus.ObjectManager.InterfacesAdded(/org/bluez/hci0/dev_F8_5C_7E_2F_CA_CA, ...)

However, trying to connect using bluetoothctl:

[JBL Charge 5]# Failed to connect: org.bluez.Error.Failed br-connection-profile-unavailable

Figured I was using the incorrect bluealsa profile (-p a2dp-sink), so switched to a2dp-source.

Restarted the bluealsa server:
chpst -u _bluez_alsa:audio bluealsa -p a2dp-source

Then bluetoothctl for connecting to the JBL speaker and voila... finally hearing Mozart 01._Allegro.wav!

However, when trying to use blu

@rogerxxxx
Copy link
Author

Just tried the following without using "--devbuffer 120" after powering cycling the JBL bluetooth speakers, subsequently automatically reconnecting to the bluetooth server, and I hear no stuttering after the speakers are reconnected. Or so far with initial testing.

mpg123 -a bluealsa:SRV=org.bluealsa,PROFILE=a2dp https://live.ideastream.org/wksu3.128.mp3

So this stuttering bug is likely isolated to older versions such as bluez-alsa-3.1.0_1, the version listed within the current Void Linux distribution package repositories.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants