Added setting to disable the server for LAN chromecast devices

This commit is contained in:
Sumner Evans
2020-02-11 16:40:50 -07:00
parent 3626e29525
commit be8fecbd9b
5 changed files with 43 additions and 13 deletions

View File

@@ -406,6 +406,8 @@ class SublimeMusicApp(Gtk.Application):
'download_on_stream'].get_active() 'download_on_stream'].get_active()
self.state.config.song_play_notification = dialog.data[ self.state.config.song_play_notification = dialog.data[
'song_play_notification'].get_active() '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[ self.state.config.prefetch_amount = dialog.data[
'prefetch_amount'].get_value_as_int() 'prefetch_amount'].get_value_as_int()
self.state.config.concurrent_download_limit = dialog.data[ self.state.config.concurrent_download_limit = dialog.data[
@@ -557,7 +559,7 @@ class SublimeMusicApp(Gtk.Application):
self.state.save_config() self.state.save_config()
def on_connected_server_changed(self, action, current_server): 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.save()
self.state.config.current_server = current_server self.state.config.current_server = current_server
self.state.save_config() self.state.save_config()

View File

@@ -1,4 +1,5 @@
import os import os
import logging
import keyring import keyring
from typing import List, Optional from typing import List, Optional
@@ -60,7 +61,8 @@ class AppConfiguration:
prefetch_amount: int = 3 prefetch_amount: int = 3
concurrent_download_limit: int = 5 concurrent_download_limit: int = 5
port_number: int = 8282 port_number: int = 8282
version: int = 1 version: int = 2
serve_over_lan: bool = True
def to_json(self): def to_json(self):
exclude = ('servers') exclude = ('servers')
@@ -78,6 +80,13 @@ class AppConfiguration:
for server in self.servers: for server in self.servers:
server.migrate() 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 @property
def cache_location(self): def cache_location(self):
if (hasattr(self, '_cache_location') if (hasattr(self, '_cache_location')

View File

@@ -194,7 +194,7 @@ class MPVPlayer(Player):
class ChromecastPlayer(Player): class ChromecastPlayer(Player):
chromecasts: List[Any] = [] chromecasts: List[Any] = []
chromecast: pychromecast.Chromecast = None chromecast: pychromecast.Chromecast = None
executor: ThreadPoolExecutor = ThreadPoolExecutor(max_workers=50) executor: ThreadPoolExecutor = ThreadPoolExecutor(max_workers=10)
class CastStatusListener: class CastStatusListener:
on_new_cast_status: Optional[Callable] = None on_new_cast_status: Optional[Callable] = None
@@ -316,9 +316,12 @@ class ChromecastPlayer(Player):
self.host_ip = None self.host_ip = None
self.port = config.port_number self.port = config.port_number
self.server_thread = ChromecastPlayer.ServerThread( self.serve_over_lan = config.serve_over_lan
'0.0.0.0', self.port)
self.server_thread.start() 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): def on_new_cast_status(self, status):
self.on_player_event( self.on_player_event(
@@ -389,8 +392,16 @@ class ChromecastPlayer(Player):
def play_media(self, file_or_url: str, progress: float, song: Child): def play_media(self, file_or_url: str, progress: float, song: Child):
stream_scheme = urlparse(file_or_url).scheme 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: 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) cover_art_url = CacheManager.get_cover_art_url(song.id, 1000)
self.chromecast.media_controller.play_media( self.chromecast.media_controller.play_media(
@@ -400,12 +411,12 @@ class ChromecastPlayer(Player):
current_time=progress, current_time=progress,
title=song.title, title=song.title,
thumb=cover_art_url, thumb=cover_art_url,
metadata=dict( metadata={
metadataType=3, 'metadataType': 3,
albumName=song.album, 'albumName': song.album,
artist=song.artist, 'artist': song.artist,
trackNumber=song.track, 'trackNumber': song.track,
), },
) )
self._timepos = progress self._timepos = progress

View File

@@ -154,6 +154,10 @@ class ApplicationState:
def migrate(self): def migrate(self):
"""Use this function to migrate any state storage that has changed.""" """Use this function to migrate any state storage that has changed."""
self.config.migrate() self.config.migrate()
self.save_config()
if self.config.server:
self.save()
def save(self): def save(self):
# Make the necessary directories before writing the state. # Make the necessary directories before writing the state.

View File

@@ -22,6 +22,10 @@ class SettingsDialog(EditFormDialog):
'Show a notification when a song begins to play', 'Show a notification when a song begins to play',
'song_play_notification', 'song_play_notification',
), ),
(
'Serve locally cached files over the LAN to Chromecast devices.',
'serve_over_lan',
),
] ]
numeric_fields = [ numeric_fields = [
( (