Commit 6a542771 authored by Sumner Evans's avatar Sumner Evans 💬
Browse files

Merge branch '107-setting-disable-server' into 'master'

Add setting to disable local server for playing on Chromecasts

Closes #107

See merge request sumner/sublime-music!21
parents 636f7ee9 7ff6025d
Pipeline #117115894 passed with stages
in 7 minutes and 23 seconds
......@@ -16,7 +16,7 @@ from .dbus_manager import DBusManager, dbus_propagate
from .state_manager import ApplicationState, RepeatType
from .cache_manager import CacheManager
from .server.api_objects import Child, Directory
from .ui.common.players import PlayerEvent, MPVPlayer, ChromecastPlayer
from .players import PlayerEvent, MPVPlayer, ChromecastPlayer
class SublimeMusicApp(Gtk.Application):
......@@ -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()
......
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')
......
......@@ -123,7 +123,7 @@ class MPVPlayer(Player):
super().__init__(*args)
self.mpv = mpv.MPV()
self.mpv.audio_client_name = 'Sublime Music'
self.mpv.audio_client_name = 'sublime-music'
self.progress_value_lock = threading.Lock()
self.progress_value_count = 0
self._muted = False
......@@ -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
......
......@@ -154,6 +154,7 @@ class ApplicationState:
def migrate(self):
"""Use this function to migrate any state storage that has changed."""
self.config.migrate()
self.save_config()
def save(self):
# Make the necessary directories before writing the state.
......
from typing import List, Tuple
from typing import List, Tuple, Optional
import gi
gi.require_version('Gtk', '3.0')
......@@ -14,6 +14,7 @@ class EditFormDialog(Gtk.Dialog):
text_fields: List[Tuple[str, str, bool]] = []
boolean_fields: List[Tuple[str, str]] = []
numeric_fields: List[NumericFieldDescription] = []
extra_label: Optional[str] = None
extra_buttons: List[Gtk.Button] = []
def get_object_name(self, obj):
......@@ -104,6 +105,12 @@ class EditFormDialog(Gtk.Dialog):
content_grid.attach(spin_button, 1, i, 1, 1)
i += 1
if self.extra_label:
label_box = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL)
label_box.add(self.extra_label)
content_grid.attach(label_box, 0, i, 2, 1)
i += 1
button_box = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL)
for button, response_id in self.extra_buttons:
if response_id is None:
......
......@@ -12,7 +12,7 @@ from sublime.cache_manager import CacheManager
from sublime.state_manager import ApplicationState, RepeatType
from sublime.ui import util
from sublime.ui.common import IconButton, SpinnerImage
from sublime.ui.common.players import ChromecastPlayer
from sublime.players import ChromecastPlayer
class PlayerControls(Gtk.ActionBar):
......
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
from .common.edit_form_dialog import EditFormDialog
......@@ -9,8 +10,7 @@ class SettingsDialog(EditFormDialog):
initial_size = (450, 250)
text_fields = [
(
'Port Number (for streaming to Chromecasts on the local network, '
'will take effect on restart)',
'Port Number (for streaming to Chromecasts on the LAN) *',
'port_number',
False,
),
......@@ -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 = [
(
......@@ -39,4 +43,10 @@ class SettingsDialog(EditFormDialog):
]
def __init__(self, *args, **kwargs):
self.extra_label = Gtk.Label(
label='<i>* Will be appplied after restarting Sublime Music</i>',
justify=Gtk.Justification.LEFT,
use_markup=True,
)
super().__init__(*args, **kwargs)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment