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

Fix default Snapserver config #1163

Merged
merged 2 commits into from
Mar 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 26 additions & 7 deletions music_assistant/server/providers/snapcast/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
from __future__ import annotations

import asyncio
import pathlib
import random
import socket
import time
from contextlib import suppress
from typing import TYPE_CHECKING, cast
from typing import TYPE_CHECKING, Final, cast

from snapcast.control import create_server
from snapcast.control.client import Snapclient
Expand Down Expand Up @@ -60,6 +61,8 @@
}
DEFAULT_SNAPSERVER_PORT = 1705

SNAPWEB_DIR: Final[pathlib.Path] = pathlib.Path(__file__).parent.resolve().joinpath("snapweb")


async def setup(
mass: MusicAssistant, manifest: ProviderManifest, config: ProviderConfig
Expand Down Expand Up @@ -136,9 +139,14 @@ def supported_features(self) -> tuple[ProviderFeature, ...]:

async def handle_async_init(self) -> None:
"""Handle async initialization of the provider."""
self._snapcast_server_host = self.config.get_value(CONF_SERVER_HOST)
self._snapcast_server_control_port = self.config.get_value(CONF_SERVER_CONTROL_PORT)
self._use_builtin_server = not self.config.get_value(CONF_USE_EXTERNAL_SERVER)
if self._use_builtin_server:
self._snapcast_server_host = "127.0.0.1"
self._snapcast_server_control_port = DEFAULT_SNAPSERVER_PORT
else:
self._snapcast_server_host = self.config.get_value(CONF_SERVER_HOST)
self._snapcast_server_control_port = self.config.get_value(CONF_SERVER_CONTROL_PORT)

self._stream_tasks = {}

if self._use_builtin_server:
Expand Down Expand Up @@ -174,10 +182,10 @@ async def unload(self) -> None:
"""Handle close/cleanup of the provider."""
for client in self._snapserver.clients:
await self.cmd_stop(client.identifier)
self._snapserver.stop()
if self._snapserver_runner and not self._snapserver_runner.done():
self._snapserver_runner.cancel()
await asyncio.sleep(6) # prevent race conditions when reloading
self._snapserver.stop()
await asyncio.sleep(10) # prevent race conditions when reloading
self._snapserver_started.clear()

def on_player_config_removed(self, player_id: str) -> None:
Expand Down Expand Up @@ -442,7 +450,7 @@ async def _builtin_server_runner(self) -> None:
properties={"is_mass": "true"},
addresses=[await get_ip_pton(self.mass.webserver.publish_ip)],
port=port,
server=f"{socket.gethostname()}",
server=f"{socket.gethostname()}.local",
)
attr_name = f"zc_service_set{name}"
if getattr(self, attr_name, None):
Expand All @@ -458,8 +466,19 @@ async def _builtin_server_runner(self) -> None:
self.logger.exception(
"Could not register mdns record for %s: %s", zeroconf_type, str(err)
)
args = [
"snapserver",
# config settings taken from
# https://raw.githubusercontent.com/badaix/snapcast/86cd4b2b63e750a72e0dfe6a46d47caf01426c8d/server/etc/snapserver.conf
f"--server.datadir={self.mass.storage_path}",
"--http.enabled=true",
"--http.port=1780",
f"--http.doc_root={SNAPWEB_DIR}",
"--tcp.enabled=true",
"--tcp.port=1705",
]
async with AsyncProcess(
["snapserver"], enable_stdin=False, enable_stdout=True, enable_stderr=False
args, enable_stdin=False, enable_stdout=True, enable_stderr=False
) as snapserver_proc:
# keep reading from stderr until exit
async for data in snapserver_proc.iter_any():
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Large diffs are not rendered by default.

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
23 changes: 23 additions & 0 deletions music_assistant/server/providers/snapcast/snapweb/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="utf-8" />
<link rel="icon" href="/favicon.ico" sizes="48x48">
<link rel="icon" href="/logo.svg" sizes="any" type="image/svg+xml">
<link rel="apple-touch-icon" href="/apple-touch-icon-180x180.png">
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="theme-color" content="#000000" />
<meta name="author" content="Johannes M. Pohl" />
<meta name="description" content="Snapcast web client" />
<title>Snapweb</title>
<script type="module" crossorigin src="/assets/index-DDLZhQdU.js"></script>
<link rel="stylesheet" crossorigin href="/assets/index-BDBx6C_I.css">
<link rel="manifest" href="/manifest.webmanifest"><script id="vite-plugin-pwa:register-sw" src="/registerSW.js"></script></head>

<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="root"></div>
</body>

</html>
192 changes: 192 additions & 0 deletions music_assistant/server/providers/snapcast/snapweb/logo.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"name":"Snapweb - Snapcast web client","short_name":"Snapweb","start_url":"/","display":"standalone","background_color":"#ffffff","lang":"en","scope":"/","theme_color":"#ffffff","icons":[{"src":"pwa-64x64.png","sizes":"64x64","type":"image/png"},{"src":"pwa-192x192.png","sizes":"192x192","type":"image/png"},{"src":"pwa-512x512.png","sizes":"512x512","type":"image/png"},{"src":"maskable-icon-512x512.png","sizes":"512x512","type":"image/png","purpose":"maskable"}]}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
if('serviceWorker' in navigator) {window.addEventListener('load', () => {navigator.serviceWorker.register('/sw.js', { scope: '/' })})}
3 changes: 3 additions & 0 deletions music_assistant/server/providers/snapcast/snapweb/robots.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# https://www.robotstxt.org/robotstxt.html
User-agent: *
Disallow:
1 change: 1 addition & 0 deletions music_assistant/server/providers/snapcast/snapweb/sw.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading