From 18745321afc66cc04b397fc32503dff464421b1f Mon Sep 17 00:00:00 2001 From: Marcel van der Veldt Date: Tue, 28 May 2019 21:58:14 +0200 Subject: [PATCH] some cleanup another attempt to make the chromecast not crashing --- music_assistant/modules/http_streamer.py | 48 ++++++++++--------- .../modules/musicproviders/qobuz.py | 1 - .../modules/musicproviders/spotify.py | 1 - music_assistant/modules/player.py | 7 ++- .../modules/playerproviders/chromecast.py | 15 +++--- .../modules/playerproviders/pylms.py | 4 +- music_assistant/modules/web.py | 5 +- music_assistant/utils.py | 1 + 8 files changed, 43 insertions(+), 39 deletions(-) diff --git a/music_assistant/modules/http_streamer.py b/music_assistant/modules/http_streamer.py index 52fd01d3..4b6c4f69 100755 --- a/music_assistant/modules/http_streamer.py +++ b/music_assistant/modules/http_streamer.py @@ -180,9 +180,10 @@ class HTTPStreamer(): await buf.drain() buf.write_eof() fd.close() - # successfull completion, send tmpfile to be processed in the background in main loop - self.mass.event_loop.create_task(self.__analyze_audio(tmpfile, track_id, provider, content_type)) - LOGGER.info("fill_audio_buffer complete for track %s" % track_id) + LOGGER.debug("fill_audio_buffer complete for track %s" % track_id) + # successfull completion, process temp file for analysis + self.mass.event_loop.create_task( + self.__analyze_audio(tmpfile, track_id, provider, content_type)) return def __get_track_cache_filename(self, track_id, provider): @@ -194,27 +195,28 @@ class HTTPStreamer(): @run_periodic(3600) async def __cache_cleanup(self): ''' calculate size of cache folder and cleanup if needed ''' - size_limit = self.mass.config['base']['http_streamer']['audio_cache_max_size_gb'] - total_size_gb = get_folder_size(self._audio_cache_dir) - LOGGER.info("current size of cache folder is %s GB" % total_size_gb) - if size_limit and total_size_gb > size_limit: - LOGGER.info("Cache folder size exceeds threshold, start cleanup...") - from pathlib import Path - import time - days = 14 - while total_size_gb > size_limit: - time_in_secs = time.time() - (days * 24 * 60 * 60) - for i in Path(self._audio_cache_dir).iterdir(): - if i.is_file(): - if i.stat().st_atime <= time_in_secs: - total_size_gb -= i.stat().st_size/float(1<<30) - i.unlink() - if total_size_gb < size_limit: - break - days -= 1 - LOGGER.info("Cache folder size cleanup completed") + def cleanup(): + size_limit = self.mass.config['base']['http_streamer']['audio_cache_max_size_gb'] + total_size_gb = get_folder_size(self._audio_cache_dir) + LOGGER.info("current size of cache folder is %s GB" % total_size_gb) + if size_limit and total_size_gb > size_limit: + LOGGER.info("Cache folder size exceeds threshold, start cleanup...") + from pathlib import Path + import time + days = 14 + while total_size_gb > size_limit: + time_in_secs = time.time() - (days * 24 * 60 * 60) + for i in Path(self._audio_cache_dir).iterdir(): + if i.is_file(): + if i.stat().st_atime <= time_in_secs: + total_size_gb -= i.stat().st_size/float(1<<30) + i.unlink() + if total_size_gb < size_limit: + break + days -= 1 + LOGGER.info("Cache folder size cleanup completed") + await self.mass.event_loop.run_in_executor(None, cleanup) - @staticmethod def __get_bs1770_binary(): ''' get the path to the bs1770 binary for the current OS ''' diff --git a/music_assistant/modules/musicproviders/qobuz.py b/music_assistant/modules/musicproviders/qobuz.py index 1e0b9993..5ad9689e 100644 --- a/music_assistant/modules/musicproviders/qobuz.py +++ b/music_assistant/modules/musicproviders/qobuz.py @@ -272,7 +272,6 @@ class QobuzProvider(MusicProvider): if not chunk: break yield chunk - LOGGER.info("end of stream for track_id %s" % track_id) async def __parse_artist(self, artist_obj): ''' parse spotify artist object to generic layout ''' diff --git a/music_assistant/modules/musicproviders/spotify.py b/music_assistant/modules/musicproviders/spotify.py index 6742a5e0..7fe22cbc 100644 --- a/music_assistant/modules/musicproviders/spotify.py +++ b/music_assistant/modules/musicproviders/spotify.py @@ -258,7 +258,6 @@ class SpotifyProvider(MusicProvider): break yield chunk await process.wait() - LOGGER.info("end of stream for track_id %s" % track_id) async def __parse_artist(self, artist_obj): ''' parse spotify artist object to generic layout ''' diff --git a/music_assistant/modules/player.py b/music_assistant/modules/player.py index 0ca694c6..fb70db91 100755 --- a/music_assistant/modules/player.py +++ b/music_assistant/modules/player.py @@ -66,7 +66,7 @@ class Player(): cmd_args = 0 if cmd == 'volume' and player.is_group and player.settings['apply_group_volume']: # group volume - return await self.__player_command_group_volume(player, cmd, cmd_args) + return await self.__player_command_group_volume(player, cmd_args) # redirect playlist related commands to parent player if player.group_parent and cmd not in ['power', 'volume', 'mute']: @@ -115,7 +115,10 @@ class Player(): cur_volume = player.volume_level new_volume = try_parse_int(new_volume) volume_dif = new_volume - cur_volume - volume_dif_percent = volume_dif/cur_volume + if cur_volume == 0: + volume_dif_percent = 1+(new_volume/100) + else: + volume_dif_percent = volume_dif/cur_volume player_childs = [item for item in self._players.values() if item.group_parent == player.player_id] for child_player in player_childs: if child_player.enabled and child_player.powered: diff --git a/music_assistant/modules/playerproviders/chromecast.py b/music_assistant/modules/playerproviders/chromecast.py index 006eefa9..ac871066 100644 --- a/music_assistant/modules/playerproviders/chromecast.py +++ b/music_assistant/modules/playerproviders/chromecast.py @@ -106,11 +106,6 @@ class ChromecastProvider(PlayerProvider): ''' play media on a player ''' - count = 0 - while self._chromecasts[player_id].is_busy and count < 10: - await asyncio.sleep(0.1) - count += 1 - self._chromecasts[player_id].is_busy = True castplayer = self._chromecasts[player_id] cur_queue_index = await self.__get_cur_queue_index(player_id) @@ -140,7 +135,6 @@ class ChromecastProvider(PlayerProvider): # add new items at end of queue self._player_queue[player_id] = self._player_queue[player_id] + media_items await self.__queue_insert(player_id, media_items) - self._chromecasts[player_id].is_busy = False ### Provider specific (helper) methods ##### @@ -246,13 +240,16 @@ class ChromecastProvider(PlayerProvider): '''send new data to the CC queue''' media_controller = castplayer.media_controller receiver_ctrl = media_controller._socket_client.receiver_controller - def app_launched_callback(): + def send_queue(): """Plays media after chromecast has switched to requested app.""" queuedata['mediaSessionId'] = media_controller.status.media_session_id media_controller.send_message(queuedata, inc_session_id=False) castplayer.wait() - receiver_ctrl.launch_app(media_controller.app_id, - callback_function=app_launched_callback) + if not media_controller.status.media_session_id: + receiver_ctrl.launch_app(media_controller.app_id, callback_function=send_queue) + else: + send_queue() + await asyncio.sleep(0.2) async def __handle_player_state(self, chromecast, caststatus=None, mediastatus=None): ''' handle a player state message from the socket ''' diff --git a/music_assistant/modules/playerproviders/pylms.py b/music_assistant/modules/playerproviders/pylms.py index 0e5dbc18..63fcd949 100644 --- a/music_assistant/modules/playerproviders/pylms.py +++ b/music_assistant/modules/playerproviders/pylms.py @@ -119,7 +119,9 @@ class PyLMSServer(PlayerProvider): async def __queue_play(self, player_id, index, send_flush=False): ''' send play command to player ''' - if not player_id in self._player_queue: + if not player_id in self._player_queue or not player_id in self._player_queue_index: + return + if not self._player_queue[player_id]: return if index == None: index = self._player_queue_index[player_id] diff --git a/music_assistant/modules/web.py b/music_assistant/modules/web.py index 41686c69..94e6da86 100755 --- a/music_assistant/modules/web.py +++ b/music_assistant/modules/web.py @@ -211,10 +211,11 @@ class Web(): async def websocket_handler(self, request): ''' websockets handler ''' - ws = web.WebSocketResponse() - await ws.prepare(request) cb_id = None + ws = None try: + ws = web.WebSocketResponse() + await ws.prepare(request) # register callback for internal events async def send_event(msg, msg_details): ws_msg = {"message": msg, "message_details": msg_details } diff --git a/music_assistant/utils.py b/music_assistant/utils.py index 32098437..2d84052e 100755 --- a/music_assistant/utils.py +++ b/music_assistant/utils.py @@ -33,6 +33,7 @@ def run_async_background_task(executor, corofn, *args): def run_task(corofn, *args): LOGGER.debug('running %s in background task' % corofn.__name__) new_loop = asyncio.new_event_loop() + asyncio.set_event_loop(new_loop) coro = corofn(*args) res = new_loop.run_until_complete(coro) new_loop.close() -- 2.34.1