134 lines
3.9 KiB
TypeScript
134 lines
3.9 KiB
TypeScript
import { useEffect, useRef } from 'react';
|
|
import { appStore } from '../lib/app';
|
|
import {
|
|
loadYouTubeApi,
|
|
YOUTUBE_EMBED_HOST,
|
|
youtubeEmbedVars,
|
|
} from '../lib/youtube';
|
|
import { shouldCorrectVideoDrift } from '../lib/nowPlayingVideoSync';
|
|
|
|
// The muted YouTube video of the current track, shown in Now Playing when the
|
|
// user enables it (item 11). The bot's audio is the source of truth, so the
|
|
// video is always muted and only follows the server position — it resyncs
|
|
// with a hard seek when it drifts past the threshold, and pauses/plays to
|
|
// match. Purely a local visual aid; it sends nothing to the server.
|
|
const SEEK_LEAD_SECONDS = 0.75;
|
|
|
|
export function NowPlayingVideo({ videoId }: { videoId: string }) {
|
|
const mountRef = useRef<HTMLDivElement>(null);
|
|
const playerRef = useRef<YT.Player | null>(null);
|
|
const currentVideo = useRef<string>('');
|
|
const pendingVideo = useRef(videoId);
|
|
const lastSeekAt = useRef(0);
|
|
const playIntent = useRef<boolean | null>(null);
|
|
|
|
useEffect(() => {
|
|
let cancelled = false;
|
|
loadYouTubeApi().then(() => {
|
|
if (cancelled || !mountRef.current) return;
|
|
const el = document.createElement('div');
|
|
mountRef.current.appendChild(el);
|
|
playerRef.current = new YT.Player(el, {
|
|
width: '100%',
|
|
height: '100%',
|
|
videoId,
|
|
host: YOUTUBE_EMBED_HOST,
|
|
playerVars: youtubeEmbedVars({ mute: 1 }),
|
|
events: {
|
|
onReady: () => {
|
|
const p = playerRef.current;
|
|
if (!p) return;
|
|
const requested = pendingVideo.current;
|
|
const startSeconds = appStore.get().positionMs / 1000;
|
|
if (requested !== videoId) {
|
|
p.loadVideoById({ videoId: requested, startSeconds });
|
|
} else if (startSeconds > 0) {
|
|
p.seekTo(startSeconds, true);
|
|
}
|
|
p.mute();
|
|
currentVideo.current = requested;
|
|
lastSeekAt.current = performance.now();
|
|
playIntent.current = null;
|
|
},
|
|
},
|
|
});
|
|
});
|
|
return () => {
|
|
cancelled = true;
|
|
playerRef.current?.destroy();
|
|
playerRef.current = null;
|
|
if (mountRef.current) mountRef.current.innerHTML = '';
|
|
};
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
}, []);
|
|
|
|
// Load a new video id in place without recreating the player.
|
|
useEffect(() => {
|
|
pendingVideo.current = videoId;
|
|
const p = playerRef.current;
|
|
if (!p || !p.loadVideoById || currentVideo.current === videoId) return;
|
|
p.loadVideoById({
|
|
videoId,
|
|
startSeconds: appStore.get().positionMs / 1000,
|
|
});
|
|
p.mute();
|
|
currentVideo.current = videoId;
|
|
lastSeekAt.current = performance.now();
|
|
playIntent.current = null;
|
|
}, [videoId]);
|
|
|
|
// Follow the server position: resync on drift, match play/pause state.
|
|
useEffect(() => {
|
|
const id = setInterval(() => {
|
|
const p = playerRef.current;
|
|
if (!p || !p.getCurrentTime || !p.seekTo) return;
|
|
const s = appStore.get();
|
|
const targetSec = s.positionMs / 1000;
|
|
const playing = !!(
|
|
s.status &&
|
|
s.status.isPlaying &&
|
|
!s.status.isPaused
|
|
);
|
|
try {
|
|
const st = p.getPlayerState();
|
|
const now = performance.now();
|
|
// Seeking while CUED/BUFFERING repeatedly restarts YouTube's
|
|
// buffer. Drift is meaningful only once the playback clock moves.
|
|
if (shouldCorrectVideoDrift(
|
|
st,
|
|
p.getCurrentTime(),
|
|
targetSec,
|
|
now - lastSeekAt.current
|
|
)) {
|
|
p.seekTo(targetSec + SEEK_LEAD_SECONDS, true);
|
|
lastSeekAt.current = now;
|
|
}
|
|
if (
|
|
playing &&
|
|
playIntent.current !== true &&
|
|
(st === YT.PlayerState.CUED ||
|
|
st === YT.PlayerState.PAUSED ||
|
|
st === YT.PlayerState.ENDED)
|
|
) {
|
|
p.playVideo();
|
|
playIntent.current = true;
|
|
}
|
|
else if (
|
|
!playing &&
|
|
playIntent.current !== false &&
|
|
(st === YT.PlayerState.PLAYING || st === YT.PlayerState.BUFFERING)
|
|
) {
|
|
p.pauseVideo();
|
|
playIntent.current = false;
|
|
}
|
|
p.mute(); // guard against unmute on state changes
|
|
} catch {
|
|
/* player not ready yet */
|
|
}
|
|
}, 1000);
|
|
return () => clearInterval(id);
|
|
}, []);
|
|
|
|
return <div className="np-video" ref={mountRef} />;
|
|
}
|