balena-io-experimental / balena-sound

Build a single or multi-room streamer for an existing audio device using a Raspberry Pi! Supports Bluetooth, Airplay and Spotify Connect
https://balena.io/blog/turn-your-old-speakers-or-hi-fi-into-bluetooth-receivers-using-only-a-raspberry-pi/
MIT License
2.45k stars 432 forks source link

Track skipping due to upstream Spotify API issues. #669

Open brettmillerb opened 3 months ago

brettmillerb commented 3 months ago

Describe the problem When connecting to Balena raspberryPi device the tracks skip continuously.

To Reproduce Open Spotify and attempt to connect to Balena Airplay device. Tracks just begin skipping, some tracks play but the majority do not.

Expected behavior Track selected plays on Balena device

Screenshots

 spotify  [2024-09-04T11:50:44Z ERROR librespot_core::channel] channel error: 2 0
 spotify  [2024-09-04T11:50:44Z ERROR librespot_playback::player] Unable to load encrypted file: ChannelError
 spotify  [2024-09-04T11:50:44Z WARN  librespot_playback::player] Skipping to next track, unable to load track <SpotifyId { id: 11583470944873040899923648362564505491, audio_type: Track }>: ()
 spotify  [2024-09-04T11:50:44Z INFO  librespot_playback::player] Loading <Apéro Spritz> with Spotify URI <spotify:track:4K9CUUcvjacTnHdnAnzGRP>

Additional Errors:

 spotify  [2024-09-04T11:54:01Z ERROR librespot_playback::player] Unable to load encrypted file: ChannelError
 spotify  [2024-09-04T11:54:01Z WARN  librespot_playback::player] Skipping to next track, unable to load track <SpotifyId { id: 297718726312721626386930062394132188552, audio_type: Track }>: ()

Device information

Additional context Seems a Spotify API change is the cause and recommendation is to build librespot from dev branch which does not have the issue.

https://github.com/librespot-org/librespot/issues/1322

mivittek commented 3 months ago

I can see in /plugins/spotify/Dockerfile.template

we are using image FROM andrewn/librespot:0.4.2-pulseaudio

@andrewn is it possible for you to rereate docker image of librespot from dev branch ?

brettmillerb commented 3 months ago

I opened https://github.com/andrewn/librespot-docker/pull/2 which I think should allow a new image to be created from dev branch but I don't know how to validate.

brettmillerb commented 2 months ago

Because I am a masochist who doesn't know when to quite whilst he's ahead and I already possess a docker hub account I thought I'd try building the image myself and ran into a few issues:

Modified build-pulseaudio.sh

# not sure if this should align with the dev branch build but it was unique so 🤷 
# version = "0.5.0-dev" is dev branch version in cargo.toml
LIBRESPOT_VERSION="0.4.2-dev"
DOCKER_NAMESPACE="brettmillerit"

exec: bash build-pulseaudio.sh

=> WARN: FromAsCasing: 'as' and 'FROM' keywords' casing do not match (line 4)                                                      0.1s
 => ERROR [internal] load metadata for docker.io/balenalib/rpi-alpine:edge                                                          0.9s
 => CANCELED [internal] load metadata for docker.io/balenalib/rpi-alpine:3.14-run 

ERROR: failed to solve: balenalib/rpi-alpine:edge: failed to resolve source metadata for docker.io/balenalib/rpi-alpine:edge: error getting credentials - err: exit status 255, out: ``

I then tried rebuilding using :3.14 and received a cargo error (I don't remember why I thought changing from edge would work:

> [librespot-builder 6/6] RUN cargo build   --release   --features alsa-backend,pulseaudio-backend   --verbose:
0.997 error: failed to parse manifest at `/app/librespot-src/Cargo.toml`
0.998 
0.998 Caused by:
0.999   feature `edition2021` is required
0.999 
0.999   this Cargo does not support nightly features, but if you
0.999   switch to nightly channel you can add
0.999   `cargo-features = ["edition2021"]` to enable this feature
------
Dockerfile.pulseaudio.template:20
--------------------
  19 |     
  20 | >>> RUN cargo build \
  21 | >>>   --release \
  22 | >>>   --features alsa-backend,pulseaudio-backend \
  23 | >>>   --verbose
  24 |     
--------------------
ERROR: failed to solve: process "/bin/sh -c cargo build   --release   --features alsa-backend,pulseaudio-backend   --verbose" did not complete successfully: exit code: 101

I moved back to edge and I think I have it building....or it's stuck building and going to throw an error eventually 🤷

brettmillerb commented 2 months ago

So I spent more time than I care to admit figuring this out.

Was able to get the docker image built using @andrewn repo, pushed to my own docker hub repo for testing. Or at least I was able to get 2/4 images built.

I was using docker in docker on my Windows machine to do this so not sure why it took so long but left it over night and it still hadn't finished the remaining two images.

Switched out my BalenaSound project to use the image from my docker hub repo: https://github.com/brettmillerb/balena-sound-hole/blob/main/plugins/spotify/Dockerfile.template

Built a docker container for that project so I could install Balena CLI as I have no admin rights on this windows machine 🤦

Confirmed a push was using the correct image:

[airplay]           Step 1/10 : FROM mikebrady/shairport-sync:4.1.1 as shairport
[unbound]           Step 1/4 : FROM klutchell/unbound:v1.19.1@sha256:125cd0c03ea2e5386aec2d9e162de00d6c59b20ee2adf4529818ee025b59e79d
[sound-supervisor]   ---> 1b4441c7cf2c
[sound-supervisor]  Step 2/15 : WORKDIR /usr/src
[spotify]           Step 1/4 : FROM brettmillerit/librespot:0.4.2-dev-pulseaudio-rpi
[sound-supervisor]   ---> Running in f021eb48c5d9
[sound-supervisor]  Removing intermediate container f021eb48c5d9

Was able to connect from Spotify without skipping tracks. image

brettmillerb commented 2 months ago

Went back to check the original issue and seems Spotify have reverted their API version changes

https://github.com/librespot-org/librespot/issues/1322#issuecomment-2334271629

However, when Spotify decide to make those changes Balena will need to utilise the new 0.5.0 and the container images will need to be rebuilt and updated.

Guess I learned a tonne about the project 🤣

brettmillerb commented 2 months ago

Also looks like they're pushing for a 0.5.0 release:

https://github.com/librespot-org/librespot/issues/1279#issuecomment-2332543048

andrewn commented 2 months ago

Hi @brettmillerb, sorry for the delay. I can try and do a new build of the repo in the next couple of days.

I guess 0.5.0 is backwards compatible with the old API too?

How should I test it?

brettmillerb commented 2 months ago

Hi @andrewn

I guess 0.5.0 is backwards compatible with the old API too? Spotify rolled back the changes and I am still using the dev branch build I generated.

I am way past the realms of my existing knowledge as it is. I have noticed with the dev branch build that there are random disconnects when selecting my raspi from Spotify.

May be worth hanging fire for an official release of 0.5.0 before rebuilding the docker images since Spotify rolled back the changes their end 🤷‍♂️

mivittek commented 1 month ago

Hi there, currently, i am again getting a lot of:

librespot_core::session] Connection reset by peer (os error 104)
librespot_connect::spirc] subscription terminated

As suggested in docs, there is no actual solution, only to upgrade. balena-sound-spotify-connect-os-error-104

There is already new librespot release v0.5.0 avalible.

i forked balenasound and updated tag for docker image https://github.com/mivittek/balena-sound/blob/master/plugins/spotify/Dockerfile.template

and also andrewn librespot, where i bumed tag versions to 0.5.0 https://github.com/mivittek/librespot-docker

Bui i am unable to build docker image with github actions, there is some weird cargo build error. https://github.com/mivittek/librespot-docker/actions/runs/11425163023/job/31786487840

andrewn commented 1 month ago

It took a bit of fiddling around, but I have a version that seems to build v0.5.0 here: andrewn/librespot-docker#3

The GitHub actions have never worked, so I'm building them manually on my laptop.

The only thing is that I don't use balena-sound anymore so once they're up, someone's going to need to run them on a Pi to make sure they work.

I'll post here once they're on dockerhub.

mivittek commented 1 month ago

Cool, i have pi4 and pi3 at my disposal, so i can try to build it on device :)

wheredidthecostgo commented 1 month ago

It took a bit of fiddling around, but I have a version that seems to build v0.5.0 here: andrewn/librespot-docker#3

The GitHub actions have never worked, so I'm building them manually on my laptop.

The only thing is that I don't use balena-sound anymore so once they're up, someone's going to need to run them on a Pi to make sure they work.

I'll post here once they're on dockerhub.

I can give it a go on a pi2

andrewn commented 1 month ago

I left my laptop cross-compiling all the binaries and went out for the day.

I meant it would be great to check the compiled binaries work with balena-sound once they’re uploaded to docker.

andrewn commented 1 month ago

@mivittek @wheredidthecostgo The new v0.5.0 images are now published to docker hub as andrewn/librespot.

Please test them out and let me know how they work for you

mivittek commented 1 month ago

Well, i managed to build it and pull form docker repo, thanks for that. But there is error while running build. Some manifest missing ?

[airplay]           Step 1/10 : FROM mikebrady/shairport-sync:4.1.1 as shairport
[spotify]           Step 1/4 : FROM andrewn/librespot:0.5.0-pulseaudio
[spotify]           manifest for andrewn/librespot:0.5.0-pulseaudio not found: manifest unknown: manifest unknown

---- i manually deleted some of build data

[Error]             Some services failed to build:
[Error]               Service: spotify
[Error]                 Error: manifest for andrewn/librespot:0.5.0-pulseaudio not found: manifest unknown: manifest unknown
[Info]              Built on df9c640
[Error]             Not deploying release.

i tried to debug it, but for main packge - without platform there is no manifest

docker manifest inspect andrewn/librespot:0.5.0-pulseaudio      
no such manifest: docker.io/andrewn/librespot:0.5.0-pulseaudio

When i edit it manually as you cen see it in commit, it works like a charm. /mivittek/balena-sound/commit/1847d9f974714fd421f168e48bace59701ccfd92

andrewn commented 1 month ago

@mivittek Thanks for reporting that!

I forgot to enable the "create manifest" line in the script. 🤦

If you try again it should work.

wheredidthecostgo commented 1 month ago

@mivittek @wheredidthecostgo The new v0.5.0 images are now published to docker hub as andrewn/librespot.

Please test them out and let me know how they work for you

I've built and deployed! I'll test and see if it corrects the (os error 104)

mivittek commented 1 month ago

I redeployed image and since commit/f954c98 I am running 4+ hours and no error yet, so I got high hopes :)

Anyway, this runs at my home eveyday, so i'll get back in few days. Thanks @andrewn 🥇

wheredidthecostgo commented 1 month ago

@andrewn It has worked perfect for me all day! Thank you!!!

mivittek commented 1 month ago

Its still working like a charm, should we create PR with update + bumped release version ?

wheredidthecostgo commented 1 month ago

It worked fine for a while, now I'm getting status code 502 errors. See below. I'm guessing it is some type of bad gateway error, but not sure why it would be happening.

But os 104 errors are gone, so I'm not sure what to do.

502
mivittek commented 1 month ago

Hi there ! Error 502 happens after starting playing after some time without playback. It happens for me as well, but it does not

Log:

Supervisor starting
 spotify  [2024-11-02T17:13:30Z ERROR librespot_connect::spirc] ContextError: Error { kind: Unknown, error: StatusCode(502) }

This issue is documented in librespot , without any real solution :( Comments in this tread are saying that session handling has been improved and there is new librespot version, 0.6.0

reference: https://github.com/librespot-org/librespot/discussions/609

wheredidthecostgo commented 4 weeks ago

Thanks!

Hi there ! Error 502 happens after starting playing after some time without playback. It happens for me as well, but it does not

Log:

Supervisor starting
 spotify  [2024-11-02T17:13:30Z ERROR librespot_connect::spirc] ContextError: Error { kind: Unknown, error: StatusCode(502) }

This issue is documented in librespot , without any real solution :( Comments in this tread are saying that session handling has been improved and there is new librespot version, 0.6.0

reference: https://github.com/librespot-org/librespot/discussions/609