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

Bug: Music Assistant cannot play music in HASS.Agent media player #208

Open
2 of 4 tasks
felipecrs opened this issue Dec 24, 2024 · 14 comments
Open
2 of 4 tasks

Bug: Music Assistant cannot play music in HASS.Agent media player #208

felipecrs opened this issue Dec 24, 2024 · 14 comments
Labels
bug Something isn't working

Comments

@felipecrs
Copy link

Describe the bug
I am trying to make Music Assistant play music to my HASS.Agent media player but it simply doesn't work.

To Reproduce

  1. Install Music Assistant
  2. Import Media Player in Music Assistant using Home Assistant Player Provider
  3. Try to play some music
  4. Nothing happens

Expected behavior
To work :)

Screenshots
N/A

Misc info (please complete the following information):

  • Windows build (ideally screenshot/info of winver.exe output):
    image
  • Windows' UI language: Portuguese Brazil
  • HASS.Agent version: 2.1.0

Please check what's applicable (multiple answers possible):

  • Installed via installer
  • Installed manually
  • Problem occurs in HASS.Agent
  • Problem occurs in Satellite Service

Additional context
The same issue doesn't happen to other speakers imported from Home Assistant.

Logs

2024-12-24 15:59:18.272 -03:00 [ERR] [STORAGE] Error while downloading file!
Remote URI: http://192.168.1.10:8097/flow/media_player.felipe_desktop/e5d84c5e0b6f4c99b565d59b76aadd2a.mp3?ts=1735066600
Local file: C:\Users\felip\AppData\Local\HASS.Agent\Client\cache\audio\20241224155639_8210808a.mp3
Error: The response ended prematurely.
2024-12-24 15:59:18.272 -03:00 [ERR] [STORAGE] Error while downloading file!
Remote URI: http://192.168.1.10:8097/flow/media_player.felipe_desktop/29e657e16d334ab6b3eaa322e22617f1.mp3?ts=1735066579
Local file: C:\Users\felip\AppData\Local\HASS.Agent\Client\cache\audio\20241224155617_590cff46.mp3
Error: The response ended prematurely.
2024-12-24 15:59:18.272 -03:00 [ERR] [STORAGE] Error while downloading file!
Remote URI: http://192.168.1.10:8097/flow/media_player.felipe_desktop/29e657e16d334ab6b3eaa322e22617f1.mp3?ts=1735066446
Local file: C:\Users\felip\AppData\Local\HASS.Agent\Client\cache\audio\20241224155405_b232b4bf.mp3
Error: The response ended prematurely.
2024-12-24 15:59:18.272 -03:00 [ERR] [STORAGE] Error while downloading file!
Remote URI: http://192.168.1.10:8097/flow/media_player.felipe_desktop/e5d84c5e0b6f4c99b565d59b76aadd2a.mp3?ts=1735066598
Local file: C:\Users\felip\AppData\Local\HASS.Agent\Client\cache\audio\20241224155638_878fddca.mp3
Error: The response ended prematurely.
2024-12-24 15:59:18.272 -03:00 [ERR] [STORAGE] Error while downloading file!
Remote URI: http://192.168.1.10:8097/flow/media_player.felipe_desktop/29e657e16d334ab6b3eaa322e22617f1.mp3?ts=1735066597
Local file: C:\Users\felip\AppData\Local\HASS.Agent\Client\cache\audio\20241224155636_f58ebbeb.mp3
Error: The response ended prematurely.
@felipecrs felipecrs added the bug Something isn't working label Dec 24, 2024
@felipecrs
Copy link
Author

Fun thing, if I shut the Music Assistant server down, the music it was trying to play starts playing lol.

@whc2001
Copy link

whc2001 commented Dec 28, 2024

Fun thing, if I shut the Music Assistant server down, the music it was trying to play starts playing lol.

Same here, judging from the log and the partial audio clip after shutting down MASS, it seems somehow stuck in one of the streaming chunks. After killing the server the agent just plays what it received, which in my case varies between 20 sec and 1 min. I remember it was working in October so maybe MASS break something in the update?

@whc2001
Copy link

whc2001 commented Dec 28, 2024

Seems like the agent is trying to buffer the audio entirely to local when playing from an HTTP link. I tried to remove the following lines (which let the MediaPlayer to simply stream the audio from the URL instead of downloading it to local first) and recompile, seems like it's working for now but I don't really know why.

if (localFile.ToLower().StartsWith("http"))
{
// remote file, try to download
var (success, downloadedLocalFile) = await StorageManager.DownloadAudioAsync(mediaUri);
if (!success)
{
Log.Error("[MEDIA] Unable to download media");
return;
}
// done
localFile = downloadedLocalFile;
}

Also seems like the stream mode in MASS player settings must be set to "No content length" (which is the default):
image

@felipecrs
Copy link
Author

Maybe HASS.Agent can detect when the stream is being sent with no content length and avoid buffering in such case?

@whc2001
Copy link

whc2001 commented Dec 28, 2024

Maybe HASS.Agent can detect when the stream is being sent with no content length and avoid buffering in such case?

I believe the content length is referring to the HTTP header which we need to send the request first to check. Maybe when media URL received we can do a HEAD request to prefetch the headers and check if Content-Length is present?

@amadeo-alex
Copy link
Collaborator

Looks like I'll finally have the incentive to install Music Assistant :D
Interesting that Music Assistant won't send the full file, unless something changed as whc2001 mentioned and now it operates only on streams (pure speculation tho)
Thanks for the report, I'll try to install it, do some testing and find a way to fix it.

@amadeo-alex
Copy link
Collaborator

This is going to be an interesting journey I see.
I replicated this + noticed an interesting behaviour, if I play 2 items via MASS none play at the beginning and then after some time the first one starts to play. What is more interesting, when trying to use the media url in the browser I always get

404 (Unknown Queue item: a44a7fb82c274fcdae6bb89b2d4fc2b4)

@whc2001
Copy link

whc2001 commented Dec 31, 2024

This is going to be an interesting journey I see.
I replicated this + noticed an interesting behaviour, if I play 2 items via MASS none play at the beginning and then after some time the first one starts to play. What is more interesting, when trying to use the media url in the browser I always get

404 (Unknown Queue item: a44a7fb82c274fcdae6bb89b2d4fc2b4)

The "Unknown Queue Item" error is because MASS detected non-playing state after a short period (which it assumes playback already finished), and removed the song from the queue. You can try the following which is rather interesting to me:

  1. Turn on extended logging in agent
  2. Use a text editor with auto following function (like VSCode) to open the most recent agent log
  3. Start playing a song from MASS, the log should refresh the URL of that song
  4. IMMEDIATELY click the link to open with your browser (otherwise it will timeout and be removed)
  5. The song starts playing in the browser, the agent picks up the media session and it appears everything is normal as if the agent itself is playing the song

That's how I pinpointed that the problem is actually at the built-in player logic of the agent

@amadeo-alex
Copy link
Collaborator

amadeo-alex commented Dec 31, 2024

I was writing this comment while you posted yours :D
You're quite correct, I was fast enough one time and it started playing.
I have a sneaky suspicion that if we'd wait long enough if not other items are in the queue (duration of the music file - which would cause the MASS to close the stream) the file would play in HASS.Agent
edit: yeah it would - just tested with a very short track.
edit2: now I know why forced content length didn't work...MASS sets it to 12h

@amadeo-alex
Copy link
Collaborator

amadeo-alex commented Dec 31, 2024

So, good news and bad news.

Good: I was able to ducktape (for now of course) a crude logic that differentiates between a "file" and "stream" based on the content length properly (MASS doesn't provide it, I assume since it's stream), with that fix it works as expected

Bad: if you queue more than 1 media, MASS uses the same stream, i.e. HASS.Agent has no idea that the media changed
This is not consistent though since the http stream cannot be "sought" - track 1 going to 2 on it's own means no new stream, the first is reused but you scrolling to the end of track 1 means that new stream (new in terms of partice, URI stays the same) is created and request sent to HASS.Agent.

Feels more like a workaround than a fix but MASS has been pretty inconsistent for me during the testing so it's also sometimes hard to pinpoint stuff.

@whc2001
Copy link

whc2001 commented Dec 31, 2024

Thanks! I tried the "Force content length" option in MASS player settings and seems like it's sending a content length of 1728000000, not sure what it means but it seems pretty consistent. Also seems like there is an option to enable ICY metadata embedding into the stream, so maybe that can be ultilized to signal media changing? (Although I think for that the current implementation of the player would require a vastly change)

@amadeo-alex
Copy link
Collaborator

@whc2001 @felipecrs if you have time and can, please try the test build with the fix - https://github.com/amadeo-alex/HASS.Agent/releases/tag/2.1.1-beta3-musicassistant - and let me know if it works for you.
If so, I'll include it in the beta3 release.

Since it's a test build, please always remember to backup the config :)

@felipecrs
Copy link
Author

@amadeo-alex, it works like a charm! Thank you so much!

@whc2001
Copy link

whc2001 commented Jan 2, 2025

Sorry I'm not home right now, and I'll test it after returning. I think it should work fine

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants