web: watch-party layer for the Discord Activity
party.js is inert unless a transport attaches. The host publishes route changes and playback snapshots; viewers mirror the route, load the host's stream without scraping and follow its clock. Also: forceProxy playback, bug-tv-tmdb/bug-tv-img meta overrides, relative API bases (/tv), trailer hidden in party mode, and the web package no longer ships web/test.
This commit is contained in:
1 parent
5ab3683f44
commit
ee0e36d2c7
14 files changed
+800
-17
No files matched your search
@@ -4,6 +4,7 @@
|
||||
*/
|
||||
import { h } from '../util.js';
|
||||
import { icon } from './icons.js';
|
||||
import { party } from '../party.js';
|
||||
|
||||
export function openModal(content, opts = {}) {
|
||||
const prevFocus = document.activeElement;
|
||||
@@ -57,6 +58,7 @@ export function openModal(content, opts = {}) {
|
||||
}
|
||||
|
||||
export function trailerModal(key, title) {
|
||||
if (party.mode !== 'solo') return () => {}; // the YouTube iframe can't be mapped inside the Activity
|
||||
const frame = h('iframe.modal__video', {
|
||||
src: `https://www.youtube-nocookie.com/embed/${encodeURIComponent(key)}?autoplay=1&rel=0&modestbranding=1`,
|
||||
title: title ? `${title} — trailer` : 'Trailer',
|
||||
|
||||
+33
-2
@@ -1,7 +1,7 @@
|
||||
/*
|
||||
PLAYER ENGINE — plays one Stream (docs/api.md "Stream") in a <video>.
|
||||
|
||||
createPlayer(video, { onState, onError, onLevels, onAudioTracks, onTorrent, onTime })
|
||||
createPlayer(video, { onState, onError, onLevels, onAudioTracks, onTorrent, onTime, forceProxy })
|
||||
load(stream, { startAt, autoplay }) start a source (tears down the previous one)
|
||||
play() / pause() / toggle()
|
||||
seek(t) absolute seconds (offset-aware for remux)
|
||||
@@ -25,6 +25,11 @@
|
||||
engine silently reloads the fallback at the same position. Anything that
|
||||
fails after that is reported to onError({ fatal: true }) so the caller can
|
||||
move on to the next source.
|
||||
|
||||
forceProxy (option, or <meta name="bug-tv-force-proxy" content="1">): always
|
||||
play playback.fallback when present (the direct url is dropped). The Discord
|
||||
Activity CSP only allows mapped origins. `player.stream` is then the proxied
|
||||
copy; `player.original` is the Stream object passed to load().
|
||||
*/
|
||||
import { resolveUrl } from '../config.js';
|
||||
|
||||
@@ -84,7 +89,26 @@ export function probeUrl(url) {
|
||||
return `${path.replace(/\/+$/, '')}/probe${s ? `?${s}` : ''}`;
|
||||
}
|
||||
|
||||
let metaForceProxy = null;
|
||||
function forceProxyMeta() {
|
||||
if (metaForceProxy == null) {
|
||||
const el = typeof document !== 'undefined' && document.querySelector('meta[name="bug-tv-force-proxy"]');
|
||||
const v = el && (el.getAttribute('content') || '').trim();
|
||||
metaForceProxy = !!v && /^(1|true|yes|on)$/i.test(v);
|
||||
}
|
||||
return metaForceProxy;
|
||||
}
|
||||
|
||||
/** Stream with playback.url replaced by playback.fallback (and fallback dropped). */
|
||||
export function proxiedStream(s) {
|
||||
const pb = s && s.playback;
|
||||
if (!pb || !pb.fallback) return s;
|
||||
const { fallback, ...rest } = pb;
|
||||
return { ...s, playback: { ...rest, url: fallback } };
|
||||
}
|
||||
|
||||
export function createPlayer(video, handlers = {}) {
|
||||
const forceProxy = handlers.forceProxy != null ? !!handlers.forceProxy : forceProxyMeta();
|
||||
const h = {
|
||||
onState() {},
|
||||
onError() {},
|
||||
@@ -96,6 +120,7 @@ export function createPlayer(video, handlers = {}) {
|
||||
|
||||
let token = 0;
|
||||
let stream = null;
|
||||
let original = null;
|
||||
let hls = null;
|
||||
let mode = ''; // hls | native-hls | file | remux
|
||||
let offset = 0;
|
||||
@@ -450,7 +475,9 @@ export function createPlayer(video, handlers = {}) {
|
||||
|
||||
function load(s, opts = {}) {
|
||||
reset();
|
||||
stream = s;
|
||||
original = s;
|
||||
stream = forceProxy ? proxiedStream(s) : s;
|
||||
s = stream;
|
||||
wantPlay = opts.autoplay !== false;
|
||||
const startAt = Math.max(0, Number(opts.startAt) || 0);
|
||||
const pb = s.playback || { kind: 'file', url: s.url };
|
||||
@@ -715,6 +742,7 @@ export function createPlayer(video, handlers = {}) {
|
||||
function stop() {
|
||||
reset();
|
||||
stream = null;
|
||||
original = null;
|
||||
}
|
||||
|
||||
function destroy() {
|
||||
@@ -748,6 +776,9 @@ export function createPlayer(video, handlers = {}) {
|
||||
get stream() {
|
||||
return stream;
|
||||
},
|
||||
get original() {
|
||||
return original;
|
||||
},
|
||||
get mode() {
|
||||
return mode;
|
||||
},
|
||||
|
||||
Reference in new issue
Block a user