librespot does not release ALSA device after Spotify Connect stream finishes
Version
librespot 0.6.0 VERGEN_IDEMPOTENT_OUTPUT (Built on 2025-05-31, Build ID: 8j6X3B4v, Profile: release)
How to reproduce
When librespot has finished playing a stream using Spotify Connect, I then attempt to play a local file using mpd (running on the same sever as librespot and configured to output to the same ALSA device) but no sound is heard.
Command
usr/bin/librespot --name librespot --cache /tmp/librespot --bitrate 320 --autoplay off --disable-gapless --enable-volume-normalisation --normalisation-gain-type track --backend rodio --device plughw:CARD=Loopback,DEV=1 --format S16 -mixer softvol --volume-ctrl fixed --initial-volume 100 --dither none
Host
Linux pi 6.6.51+rpt-rpi-v8 #1 SMP PREEMPT Debian 1:6.6.51-1+rpt3 (2024-10-08) aarch64 GNU/Linux
Additional context
If I disable librespot and enable librespot-player (Java) then this operation works as expected. The librespot-player has a configurable parameter releaseLineDelay which demonstrates that it is designed to release the ALSA device after it has finished playing.
This sounds quite similar to the observation in #1500. Could you give https://github.com/photovoltex/librespot/tree/reduced-sink-usage a try? The TL;DR; would be that we don't close the audio sink while we don't play any audio. We could also add a similar parameter to the binary and the delay after stopping playback. But the branch previously mentioned is just a poc if it would fix the issue. Thanks in advance for the testing :)
I have built the release of librespot from branch https://github.com/photovoltex/librespot/tree/reduced-sink-usage and when run with the same options it encounters the same problem. While librespot is running Spotify streams play fine but when a stream is finished it is not possible to switch to mpd and play (hear) a local file. I have waited 30secs and 60secs after the stream was finished. In order to play and hear the local files via mpd it is necessary to stop the librespot process.
This problem does not occur when using librespot-player, it is possible to switch between playing Spotify streams and local files from mpd while these two services are both up and running continuously. (Subject only to waiting for releaseLineDelay after a stream has finished.)
Thanks for the quick testing. A bummer that it isn't such an easy fix. I will look into it when I'm back home. The best case would be to just carry over what librespot-java does, but could be a bit more tricky depending on the infrastructural differences.
I have librespot and mpd running on the same server.
I do not see this problem. As soon as I stop librespot playback I can play a local file with mpd.
My librespot options are:
--backend alsa --bitrate 320 --device hw:0,0 --device-type computer --disable-audio-cache --alsa-mixer-control Master --initial-volume 100
OK, looking at your options I see that you are using the alsa backend whereas I have been using the rodio backend because this seems to the preferred (default) backend for librespot. When I get time, I will do a test with alsa and report back.
I can confirm having the same issue.
Does #1552 help?
@roderickvd Yes! I can confirm that the patch from #1552 fixes this issue as well.
Thanks!
Was #1552 supposed to have this side effect?! From a quick glance it doesn't look related! Given I don't understand the fix, can someone explain when does librespot now release the sink? After any track finishes, or only after all the tracks in the current context finish?
😅 it was a hunch that the queue would be cleared and playback stopped when we properly handle a disconnect event. And so it did…
From a quick glance it doesn't look related!
Understandably, the underlying issue is here: https://github.com/librespot-org/librespot/blob/dev/connect/src/spirc.rs#L901
handle_disconnect early returns on a error. Tho previously this didn't had the option to return an error, so I'm also surprised this should be resolved now.