diff --git a/sublime/app.py b/sublime/app.py index 32de16e..e4fb315 100644 --- a/sublime/app.py +++ b/sublime/app.py @@ -406,6 +406,8 @@ class SublimeMusicApp(Gtk.Application): 'download_on_stream'].get_active() self.state.config.song_play_notification = dialog.data[ 'song_play_notification'].get_active() + self.state.config.serve_over_lan = dialog.data[ + 'serve_over_lan'].get_active() self.state.config.prefetch_amount = dialog.data[ 'prefetch_amount'].get_value_as_int() self.state.config.concurrent_download_limit = dialog.data[ @@ -557,7 +559,7 @@ class SublimeMusicApp(Gtk.Application): self.state.save_config() def on_connected_server_changed(self, action, current_server): - if self.state.config.current_server >= 0: + if self.state.config.server: self.state.save() self.state.config.current_server = current_server self.state.save_config() diff --git a/sublime/config.py b/sublime/config.py index 0e31955..285500e 100644 --- a/sublime/config.py +++ b/sublime/config.py @@ -1,4 +1,5 @@ import os +import logging import keyring from typing import List, Optional @@ -60,7 +61,8 @@ class AppConfiguration: prefetch_amount: int = 3 concurrent_download_limit: int = 5 port_number: int = 8282 - version: int = 1 + version: int = 2 + serve_over_lan: bool = True def to_json(self): exclude = ('servers') @@ -78,6 +80,13 @@ class AppConfiguration: for server in self.servers: server.migrate() + if (getattr(self, 'version') or 0) < 2: + logging.info('Migrating app configuration to version 2.') + logging.info('Setting serve_over_lan to True') + self.serve_over_lan = True + + self.version = 2 + @property def cache_location(self): if (hasattr(self, '_cache_location') diff --git a/sublime/players.py b/sublime/players.py index 25ce41e..da231e9 100644 --- a/sublime/players.py +++ b/sublime/players.py @@ -194,7 +194,7 @@ class MPVPlayer(Player): class ChromecastPlayer(Player): chromecasts: List[Any] = [] chromecast: pychromecast.Chromecast = None - executor: ThreadPoolExecutor = ThreadPoolExecutor(max_workers=50) + executor: ThreadPoolExecutor = ThreadPoolExecutor(max_workers=10) class CastStatusListener: on_new_cast_status: Optional[Callable] = None @@ -316,9 +316,12 @@ class ChromecastPlayer(Player): self.host_ip = None self.port = config.port_number - self.server_thread = ChromecastPlayer.ServerThread( - '0.0.0.0', self.port) - self.server_thread.start() + self.serve_over_lan = config.serve_over_lan + + if self.serve_over_lan: + self.server_thread = ChromecastPlayer.ServerThread( + '0.0.0.0', self.port) + self.server_thread.start() def on_new_cast_status(self, status): self.on_player_event( @@ -389,8 +392,16 @@ class ChromecastPlayer(Player): def play_media(self, file_or_url: str, progress: float, song: Child): stream_scheme = urlparse(file_or_url).scheme + # If it's a local file, then see if we can serve it over the LAN. if not stream_scheme: - file_or_url = f'http://{self.host_ip}:{self.port}/song/{song.id}' + if self.serve_over_lan: + host = f'{self.host_ip}:{self.port}' + file_or_url = f'http://{host}/song/{song.id}' + else: + file_or_url, _ = CacheManager.get_song_filename_or_stream( + song, + force_stream=True, + ) cover_art_url = CacheManager.get_cover_art_url(song.id, 1000) self.chromecast.media_controller.play_media( @@ -400,12 +411,12 @@ class ChromecastPlayer(Player): current_time=progress, title=song.title, thumb=cover_art_url, - metadata=dict( - metadataType=3, - albumName=song.album, - artist=song.artist, - trackNumber=song.track, - ), + metadata={ + 'metadataType': 3, + 'albumName': song.album, + 'artist': song.artist, + 'trackNumber': song.track, + }, ) self._timepos = progress diff --git a/sublime/state_manager.py b/sublime/state_manager.py index cc09a7b..e073dff 100644 --- a/sublime/state_manager.py +++ b/sublime/state_manager.py @@ -154,6 +154,10 @@ class ApplicationState: def migrate(self): """Use this function to migrate any state storage that has changed.""" self.config.migrate() + self.save_config() + + if self.config.server: + self.save() def save(self): # Make the necessary directories before writing the state. diff --git a/sublime/ui/settings.py b/sublime/ui/settings.py index 9b24065..0de1f18 100644 --- a/sublime/ui/settings.py +++ b/sublime/ui/settings.py @@ -22,6 +22,10 @@ class SettingsDialog(EditFormDialog): 'Show a notification when a song begins to play', 'song_play_notification', ), + ( + 'Serve locally cached files over the LAN to Chromecast devices.', + 'serve_over_lan', + ), ] numeric_fields = [ (