Files
gods-eye-view/src/data/youtubeLivePlayer.js
T
Denis Groshev 9cc077964f ISS live telemetry, STATION and LIVE sections, Tiangong model, drawn through the renderer's draw port
Builds on the ISS 3D model (PR #1690) and brings it onto main's renderer
seam: the station models are items of a `models` draw set, posed by
heading, pitch and roll from the station's flight axes
(`headingPitchRollOfAxes`), with the camera, canvas and clicks read from
the renderer's ports; no satellites file reaches Cesium. The joint drivers
write column-major matrices into node poses the model part keeps, for a
renderer that can pose nodes; until the draw contract has that member the
models draw rigid.

- ISS joints take their angles from NASA's ISSLive telemetry, relayed by
  the server behind ISS_TELEMETRY_ENABLED.
- STATION and LIVE sections of Context replace the floating command panel
  and player; both collapse like the Radio section.
- Cockpit rides the ISS from mounts on its model; the LIVE page in the
  Cockpit briefing.
- Tiangong as a second station model, with computed joints.
- Installations: a label-box click selects the site.
2026-11-16 18:39:39 +01:00

124 lines
3.6 KiB
JavaScript

import { loadYouTubeApi } from './youtubeApi.js';
import { YOUTUBE_EMBED_HOST, youtubeLivePlayerVars } from './liveStreams.js';
/**
* One live stream in YouTube's embedded player, with its state read back
* through the player API: `loading` until the video plays, `playing`, and
* `offline` once the player says the video has ended, cannot be found or
* never starts. The player cannot say "this broadcast is off air" in so
* many words: a live stream that is off air is reported as a video that
* cannot play, or that never reaches playing, so both read as offline here.
* Embedding refused by the video's owner is `unavailable`, which no retry
* will change.
*/
/** A stream that has not started playing this long after its player was ready is offline. */
export const LIVE_START_TIMEOUT_MS = 25_000;
/** YouTube's player states. */
const ENDED = 0;
const PLAYING = 1;
/** Error codes: embedding not allowed on this page. */
const EMBED_REFUSED = new Set([101, 150]);
/**
* @param {{host: HTMLElement, videoId: string, documentRef?: Document,
* globalRef?: object, loadApi?: typeof loadYouTubeApi,
* onChange: (state: {state: 'loading'|'playing'|'offline'|'unavailable',
* reason: string|null}) => void}} options `host` is emptied and given the
* player's iframe.
* @returns {{destroy: () => void}}
*/
export function createYouTubeLivePlayer({
host,
videoId,
documentRef = globalThis.document,
globalRef = globalThis,
loadApi = loadYouTubeApi,
onChange,
}) {
let destroyed = false;
let player = null;
let timer = null;
let current = null;
const set = (state, reason = null) => {
if (destroyed || current === state) return;
current = state;
onChange({ state, reason });
};
const clearTimer = () => {
if (timer !== null) globalRef.clearTimeout(timer);
timer = null;
};
const armTimer = () => {
clearTimer();
timer = globalRef.setTimeout(() => {
timer = null;
if (current === 'loading') set('offline', 'timeout');
}, LIVE_START_TIMEOUT_MS);
};
set('loading');
host.replaceChildren();
const mount = documentRef.createElement('div');
host.append(mount);
loadApi(documentRef, globalRef).then(
(YT) => {
if (destroyed) return;
try {
player = new YT.Player(mount, {
host: YOUTUBE_EMBED_HOST,
videoId,
playerVars: youtubeLivePlayerVars(globalRef.location?.origin),
events: {
onReady: () => {
if (!destroyed) armTimer();
},
onStateChange: (event) => {
if (destroyed) return;
if (event?.data === PLAYING) {
clearTimer();
set('playing');
} else if (event?.data === ENDED) {
clearTimer();
set('offline', 'ended');
}
},
onError: (event) => {
if (destroyed) return;
clearTimer();
const code = Number(event?.data);
if (EMBED_REFUSED.has(code)) set('unavailable', `error-${code}`);
else set('offline', `error-${code}`);
},
},
});
// The API may never call onReady for a video it cannot load.
armTimer();
} catch {
set('offline', 'player');
}
},
() => {
if (!destroyed) set('unavailable', 'api');
},
);
return {
destroy() {
if (destroyed) return;
destroyed = true;
clearTimer();
try {
player?.destroy?.();
} catch {
// A player half-created has nothing to destroy.
}
player = null;
host.replaceChildren();
},
};
}