create stream url in js

This commit is contained in:
Roy Olav Purser 2021-05-20 18:33:51 +02:00
parent f201d8e803
commit dd9be53140
Signed by: roypur
GPG Key ID: E14D26A036F21656
2 changed files with 18 additions and 31 deletions

View File

@ -220,9 +220,7 @@ if icecast_server is not None and stream_server is not None:
logger.info(e) logger.info(e)
template_html = None template_html = None
template_js = None script_file = None
template_embed = tornado.template.Template('<iframe src="{{ origin }}?provider={{ provider }}&render=true" width="{{ width }}" height="{{ height }}" allow="fullscreen"></iframe>')
videojs_version = None videojs_version = None
font_awesome_version = None font_awesome_version = None
custom_style = None custom_style = None
@ -230,7 +228,9 @@ try:
with open("/app/index.html", "r") as f: with open("/app/index.html", "r") as f:
template_html = tornado.template.Template(f.read().strip()) template_html = tornado.template.Template(f.read().strip())
with open("/app/script.js", "r") as f: 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: with open("/app/version/video.js.txt", "r") as f:
videojs_version = f.read().strip() videojs_version = f.read().strip()
with open("/app/version/chromecast.txt", "r") as f: 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)") self.write("Stream not found. (provider missing)")
async def handle_render(self, handler): 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() meta = await handler.meta()
title = handler.render_url title = handler.render_url
video_creator = None video_creator = None
@ -329,17 +329,11 @@ class MainHandler(tornado.web.RequestHandler):
title = video_creator title = video_creator
elif isinstance(video_title, str): elif isinstance(video_title, str):
title = video_title title = video_title
data["script"] = script_file
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["videojs_version"] = videojs_version data["videojs_version"] = videojs_version
data["chromecast_version"] = chromecast_version data["chromecast_version"] = chromecast_version
data["font_awesome_version"] = font_awesome_version data["font_awesome_version"] = font_awesome_version
data["custom_style"] = custom_style 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) rendered_html = template_html.generate(data=data, meta=meta)
self.write(rendered_html) self.write(rendered_html)
else: else:

View File

@ -1,15 +1,14 @@
(() => { (() => {
const setRemotePlayer = () => { const findUpstream = () => {
class NewRemotePlayer extends cast.framework.RemotePlayer { const search = new URLSearchParams(location.search);
constructor() { search.set("render", "false");
super(); const url = new URL(location.origin);
this.canControlVolume = false; url.pathname = location.pathname;
} url.search = search.toString();
} return url.href;
cast.framework.RemotePlayer = NewRemotePlayer;
} }
const upstream = "{{ stream }}"; const upstream = findUpstream();
const xhr = new XMLHttpRequest(); const xhr = new XMLHttpRequest();
xhr.open("HEAD", upstream, true); xhr.open("HEAD", upstream, true);
xhr.send(); xhr.send();
@ -21,11 +20,6 @@
} }
} }
const handleLoaded = () => {
setRemotePlayer();
handleCount();
}
const handle = () => { const handle = () => {
const [body] = document.getElementsByTagName("body"); const [body] = document.getElementsByTagName("body");
const video = document.createElement("video"); const video = document.createElement("video");
@ -41,10 +35,6 @@
options.techOrder = ["chromecast", "html5"]; options.techOrder = ["chromecast", "html5"];
options.plugins = {}; options.plugins = {};
options.plugins.chromecast = {}; options.plugins.chromecast = {};
options.plugins.chromecast.requestTitleFn = (src) => {
return "{% raw title %}";
}
const player = videojs(video, options); const player = videojs(video, options);
const source = {}; const source = {};
source.type = ctype; source.type = ctype;
@ -66,7 +56,10 @@
player.one("play", () => { player.one("play", () => {
player.removeChild(button); player.removeChild(button);
}); });
player.on("chromecastConnected", () => {
player.volume(0.1);
});
} }
document.addEventListener("DOMContentLoaded", handleLoaded); document.addEventListener("DOMContentLoaded", handleCount);
xhr.addEventListener("load", handleCount); xhr.addEventListener("load", handleCount);
})(); })();