Work on using Libremsonic to serve local versions to chromecast

This commit is contained in:
Sumner Evans
2019-08-02 23:47:44 -06:00
parent 9b6fa579a9
commit 75bd0bf79f
3 changed files with 58 additions and 6 deletions

View File

@@ -326,10 +326,9 @@ class LibremsonicApp(Gtk.Application):
# Do this the old fashioned way so that we can have access to ``reset``
# in the callback.
def do_play_song(song: Child):
# TODO force stream for now and force mp3 while getting chromecast working.
# TODO force mp3 while getting chromecast working.
uri, stream = CacheManager.get_song_filename_or_stream(
song,
force_stream=True,
format='mp3',
)

View File

@@ -1,3 +1,8 @@
import threading
import mimetypes
from urllib.parse import urlparse, quote
import socket
from http.server import HTTPServer, SimpleHTTPRequestHandler
from typing import Callable, List, Any
from time import sleep
from concurrent.futures import ThreadPoolExecutor, Future
@@ -126,6 +131,27 @@ class ChromecastPlayer(Player):
media_status_listener = MediaStatusListener()
class ServerThread(threading.Thread):
def __init__(self, port, directory):
super().__init__()
self.port = port
self.directory = directory
def generate_handler(self, directory):
class ServerHandler(SimpleHTTPRequestHandler):
def __init__(self, *args, **kwargs):
super().__init__(*args, directory=directory, **kwargs)
return ServerHandler
def run(self):
self.server = HTTPServer(
('0.0.0.0', self.port),
self.generate_handler(self.directory),
)
# TODO figure out how to make this stop when the app closes.
self.server.serve_forever()
@classmethod
def get_chromecasts(self) -> Future:
def do_get_chromecasts():
@@ -148,6 +174,20 @@ class ChromecastPlayer(Player):
self.time_incrementor_running = False
ChromecastPlayer.media_status_listener.on_new_media_status = self.on_new_media_status
# Set host_ip
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(("8.8.8.8", 80))
self.host_ip = s.getsockname()[0]
s.close()
# TODO make this come from the app config
self.server_thread = ChromecastPlayer.ServerThread(
8080,
'/home/sumner/.local/share/libremsonic',
)
self.server_thread.daemon = True
self.server_thread.start()
def on_new_media_status(self, status):
# Detect the end of a track and go to the next one.
if (status.idle_reason == 'FINISHED' and status.player_state == 'IDLE'
@@ -187,8 +227,23 @@ class ChromecastPlayer(Player):
return self.chromecast.media_controller.status.player_is_playing
def play_media(self, file_or_url=None, progress=None):
self.chromecast.media_controller.play_media(file_or_url, 'audio/mp3')
self._timepos = 0
stream_scheme = urlparse(file_or_url).scheme
if not stream_scheme:
# TODO make this come from the app config
strlen = len('/home/sumner/.local/share/libremsonic/')
file_or_url = file_or_url[strlen:]
file_or_url = f'http://{self.host_ip}:8080/{quote(file_or_url)}'
print(
file_or_url,
'audio/mp3'
if stream_scheme else mimetypes.guess_type(file_or_url)[0],
)
self.chromecast.media_controller.play_media(
file_or_url,
'audio/m4a'
)
def on_play_begin():
self._song_loaded = True

View File

@@ -182,8 +182,6 @@ class PlayerControls(Gtk.ActionBar):
self.up_next_popover.show_all()
def on_device_click(self, button):
print('device click')
def chromecast_callback(f):
cast = next(cc for cc in f.result()
if cc.device.friendly_name == "Sumner's Bedroom")