From dd9be531409741d4867dfcb2d16d457d774a4066 Mon Sep 17 00:00:00 2001 From: Roy Olav Purser Date: Thu, 20 May 2021 18:33:51 +0200 Subject: [PATCH] create stream url in js --- backend/stream.py | 18 ++++++------------ frontend/script.js | 31 ++++++++++++------------------- 2 files changed, 18 insertions(+), 31 deletions(-) diff --git a/backend/stream.py b/backend/stream.py index 9f680df..e6fae55 100755 --- a/backend/stream.py +++ b/backend/stream.py @@ -220,9 +220,7 @@ if icecast_server is not None and stream_server is not None: logger.info(e) template_html = None -template_js = None -template_embed = tornado.template.Template('') - +script_file = None videojs_version = None font_awesome_version = None custom_style = None @@ -230,7 +228,9 @@ try: with open("/app/index.html", "r") as f: template_html = tornado.template.Template(f.read().strip()) with open("/app/script.js", "r") as f: - template_js = tornado.template.Template(f.read().strip()) + script_raw = bytes(f.read().strip(), "utf-8") + b64 = str(base64.b64encode(script_raw), "ascii") + script_file = f'data:text/javascript;charset=utf-8;base64,{b64}' with open("/app/version/video.js.txt", "r") as f: videojs_version = f.read().strip() with open("/app/version/chromecast.txt", "r") as f: @@ -311,7 +311,7 @@ class MainHandler(tornado.web.RequestHandler): self.write("Stream not found. (provider missing)") async def handle_render(self, handler): - if template_js is not None and template_html is not None: + if script_file is not None and template_html is not None: meta = await handler.meta() title = handler.render_url video_creator = None @@ -329,17 +329,11 @@ class MainHandler(tornado.web.RequestHandler): title = video_creator elif isinstance(video_title, str): title = video_title - - rendered_js = template_js.generate(stream=handler.stream_url, title=title); - b64_js = str(base64.b64encode(rendered_js), "ascii") - script = f'data:text/javascript;charset=utf-8;base64,{b64_js}' - data["script"] = script + data["script"] = script_file data["videojs_version"] = videojs_version data["chromecast_version"] = chromecast_version data["font_awesome_version"] = font_awesome_version data["custom_style"] = custom_style - data["stream_url"] = handler.stream_url - data["render_url"] = handler.render_url rendered_html = template_html.generate(data=data, meta=meta) self.write(rendered_html) else: diff --git a/frontend/script.js b/frontend/script.js index 0a379a3..4f8fa77 100644 --- a/frontend/script.js +++ b/frontend/script.js @@ -1,15 +1,14 @@ (() => { - const setRemotePlayer = () => { - class NewRemotePlayer extends cast.framework.RemotePlayer { - constructor() { - super(); - this.canControlVolume = false; - } - } - cast.framework.RemotePlayer = NewRemotePlayer; + const findUpstream = () => { + const search = new URLSearchParams(location.search); + search.set("render", "false"); + const url = new URL(location.origin); + url.pathname = location.pathname; + url.search = search.toString(); + return url.href; } - const upstream = "{{ stream }}"; + const upstream = findUpstream(); const xhr = new XMLHttpRequest(); xhr.open("HEAD", upstream, true); xhr.send(); @@ -21,11 +20,6 @@ } } - const handleLoaded = () => { - setRemotePlayer(); - handleCount(); - } - const handle = () => { const [body] = document.getElementsByTagName("body"); const video = document.createElement("video"); @@ -41,10 +35,6 @@ options.techOrder = ["chromecast", "html5"]; options.plugins = {}; options.plugins.chromecast = {}; - options.plugins.chromecast.requestTitleFn = (src) => { - return "{% raw title %}"; - } - const player = videojs(video, options); const source = {}; source.type = ctype; @@ -66,7 +56,10 @@ player.one("play", () => { player.removeChild(button); }); + player.on("chromecastConnected", () => { + player.volume(0.1); + }); } - document.addEventListener("DOMContentLoaded", handleLoaded); + document.addEventListener("DOMContentLoaded", handleCount); xhr.addEventListener("load", handleCount); })();