From b9cb50153dd1d7c2fb9357b7183ba3288e886615 Mon Sep 17 00:00:00 2001 From: Santiago Soto Date: Wed, 23 Oct 2024 16:04:49 -0300 Subject: [PATCH 1/3] Add delay before delete snapstream if short duration --- .../server/providers/snapcast/__init__.py | 21 ++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/music_assistant/server/providers/snapcast/__init__.py b/music_assistant/server/providers/snapcast/__init__.py index c2916893c..e0451cba0 100644 --- a/music_assistant/server/providers/snapcast/__init__.py +++ b/music_assistant/server/providers/snapcast/__init__.py @@ -40,7 +40,11 @@ from music_assistant.common.models.errors import SetupFailedError from music_assistant.common.models.media_items import AudioFormat from music_assistant.common.models.player import DeviceInfo, Player, PlayerMedia -from music_assistant.server.helpers.audio import FFMpeg, get_ffmpeg_stream, get_player_filter_params +from music_assistant.server.helpers.audio import ( + FFMpeg, + get_ffmpeg_stream, + get_player_filter_params, +) from music_assistant.server.helpers.process import AsyncProcess, check_output from music_assistant.server.models.player_provider import PlayerProvider @@ -554,17 +558,22 @@ async def _streamer() -> None: # we need to wait a bit for the stream status to become idle # to ensure that all snapclients have consumed the audio while stream.status != "idle": - await asyncio.sleep(0.25) + await asyncio.sleep(0.50) player.state = PlayerState.IDLE self.mass.players.update(player_id) self._set_childs_state(player_id) finally: - with suppress(TypeError, KeyError, AttributeError): - await self._snapserver.stream_remove_stream(stream.identifier) + await self._delete_current_snapstream(stream, media) # start streaming the queue (pcm) audio in a background task self._stream_tasks[player_id] = asyncio.create_task(_streamer()) + async def _delete_current_snapstream(self, stream, media): + with suppress(TypeError, KeyError, AttributeError): + if media.duration < 5: + await asyncio.sleep(5) + await self._snapserver.stream_remove_stream(stream.identifier) + def _get_snapgroup(self, player_id: str) -> Snapgroup: """Get snapcast group for given player_id.""" snap_client_id = self._get_snapclient_id(player_id) @@ -628,7 +637,9 @@ async def _create_default_stream(self) -> None: """Create new stream on snapcast server named default case not exist.""" all_streams = {stream.name for stream in self._snapserver.streams} if "default" not in all_streams: - await self._snapserver.stream_add_stream("pipe:///tmp/snapfifo?name=default") + await self._snapserver.stream_add_stream( + "pipe:///tmp/snapfifo?name=default&sampleformat=48000:16:2" + ) def _set_childs_state(self, player_id: str) -> None: """Set the state of the child`s of the player.""" From 8d738b6aed691b955536d5fbd90791e767b7579f Mon Sep 17 00:00:00 2001 From: Santiago Soto Date: Wed, 23 Oct 2024 16:11:23 -0300 Subject: [PATCH 2/3] revert waiting to 0.25 --- music_assistant/server/providers/snapcast/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/music_assistant/server/providers/snapcast/__init__.py b/music_assistant/server/providers/snapcast/__init__.py index e0451cba0..8385503ed 100644 --- a/music_assistant/server/providers/snapcast/__init__.py +++ b/music_assistant/server/providers/snapcast/__init__.py @@ -558,7 +558,7 @@ async def _streamer() -> None: # we need to wait a bit for the stream status to become idle # to ensure that all snapclients have consumed the audio while stream.status != "idle": - await asyncio.sleep(0.50) + await asyncio.sleep(0.25) player.state = PlayerState.IDLE self.mass.players.update(player_id) self._set_childs_state(player_id) From 25e3e77fcca3ec96b8f17181d746ecc75a8b3f50 Mon Sep 17 00:00:00 2001 From: Santiago Soto Date: Wed, 23 Oct 2024 17:14:37 -0300 Subject: [PATCH 3/3] add types --- music_assistant/server/providers/snapcast/__init__.py | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/music_assistant/server/providers/snapcast/__init__.py b/music_assistant/server/providers/snapcast/__init__.py index 8385503ed..c5a2da135 100644 --- a/music_assistant/server/providers/snapcast/__init__.py +++ b/music_assistant/server/providers/snapcast/__init__.py @@ -40,11 +40,7 @@ from music_assistant.common.models.errors import SetupFailedError from music_assistant.common.models.media_items import AudioFormat from music_assistant.common.models.player import DeviceInfo, Player, PlayerMedia -from music_assistant.server.helpers.audio import ( - FFMpeg, - get_ffmpeg_stream, - get_player_filter_params, -) +from music_assistant.server.helpers.audio import FFMpeg, get_ffmpeg_stream, get_player_filter_params from music_assistant.server.helpers.process import AsyncProcess, check_output from music_assistant.server.models.player_provider import PlayerProvider @@ -568,7 +564,7 @@ async def _streamer() -> None: # start streaming the queue (pcm) audio in a background task self._stream_tasks[player_id] = asyncio.create_task(_streamer()) - async def _delete_current_snapstream(self, stream, media): + async def _delete_current_snapstream(self, stream: Snapstream, media: PlayerMedia) -> None: with suppress(TypeError, KeyError, AttributeError): if media.duration < 5: await asyncio.sleep(5)