2024-01-04 00:38:52 +01:00
|
|
|
import { baseUrl } from "@/api/baseUrl";
|
|
|
|
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
|
|
|
|
|
|
|
|
type MSEPlayerProps = {
|
|
|
|
camera: string;
|
2024-02-10 13:30:53 +01:00
|
|
|
className?: string;
|
2024-02-13 02:28:36 +01:00
|
|
|
playbackEnabled?: boolean;
|
2024-02-10 13:30:53 +01:00
|
|
|
onPlaying?: () => void;
|
2024-01-04 00:38:52 +01:00
|
|
|
};
|
|
|
|
|
2024-02-13 02:28:36 +01:00
|
|
|
function MSEPlayer({
|
|
|
|
camera,
|
|
|
|
className,
|
|
|
|
playbackEnabled = true,
|
|
|
|
onPlaying,
|
|
|
|
}: MSEPlayerProps) {
|
2024-01-04 00:38:52 +01:00
|
|
|
let connectTS: number = 0;
|
|
|
|
|
|
|
|
const RECONNECT_TIMEOUT: number = 30000;
|
|
|
|
|
|
|
|
const CODECS: string[] = [
|
|
|
|
"avc1.640029", // H.264 high 4.1 (Chromecast 1st and 2nd Gen)
|
|
|
|
"avc1.64002A", // H.264 high 4.2 (Chromecast 3rd Gen)
|
|
|
|
"avc1.640033", // H.264 high 5.1 (Chromecast with Google TV)
|
|
|
|
"hvc1.1.6.L153.B0", // H.265 main 5.1 (Chromecast Ultra)
|
|
|
|
"mp4a.40.2", // AAC LC
|
|
|
|
"mp4a.40.5", // AAC HE
|
|
|
|
"flac", // FLAC (PCM compatible)
|
|
|
|
"opus", // OPUS Chrome, Firefox
|
|
|
|
];
|
|
|
|
|
|
|
|
const visibilityThreshold: number = 0;
|
|
|
|
const visibilityCheck: boolean = true;
|
|
|
|
|
|
|
|
const [wsState, setWsState] = useState<number>(WebSocket.CLOSED);
|
|
|
|
|
|
|
|
const videoRef = useRef<HTMLVideoElement>(null);
|
|
|
|
const wsRef = useRef<WebSocket | null>(null);
|
|
|
|
const reconnectTIDRef = useRef<number | null>(null);
|
2024-02-28 23:23:56 +01:00
|
|
|
const ondataRef = useRef<((data: ArrayBufferLike) => void) | null>(null);
|
|
|
|
const onmessageRef = useRef<{
|
|
|
|
[key: string]: (msg: { value: string; type: string }) => void;
|
|
|
|
}>({});
|
2024-01-04 00:38:52 +01:00
|
|
|
const msRef = useRef<MediaSource | null>(null);
|
|
|
|
|
|
|
|
const wsURL = useMemo(() => {
|
|
|
|
return `${baseUrl.replace(/^http/, "ws")}live/webrtc/api/ws?src=${camera}`;
|
|
|
|
}, [camera]);
|
|
|
|
|
|
|
|
const play = () => {
|
|
|
|
const currentVideo = videoRef.current;
|
|
|
|
|
|
|
|
if (currentVideo) {
|
2024-02-28 23:23:56 +01:00
|
|
|
currentVideo.play().catch((er: { name: string }) => {
|
2024-01-04 00:38:52 +01:00
|
|
|
if (er.name === "NotAllowedError" && !currentVideo.muted) {
|
|
|
|
currentVideo.muted = true;
|
|
|
|
currentVideo.play().catch(() => {});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const send = useCallback(
|
2024-02-28 23:23:56 +01:00
|
|
|
(value: object) => {
|
2024-01-04 00:38:52 +01:00
|
|
|
if (wsRef.current) wsRef.current.send(JSON.stringify(value));
|
|
|
|
},
|
2024-02-28 23:23:56 +01:00
|
|
|
[wsRef],
|
2024-01-04 00:38:52 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
const codecs = useCallback((isSupported: (type: string) => boolean) => {
|
|
|
|
return CODECS.filter((codec) =>
|
2024-02-28 23:23:56 +01:00
|
|
|
isSupported(`video/mp4; codecs="${codec}"`),
|
2024-01-04 00:38:52 +01:00
|
|
|
).join();
|
2024-02-28 23:23:56 +01:00
|
|
|
|
|
|
|
// we know that these deps are correct
|
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
2024-01-04 00:38:52 +01:00
|
|
|
}, []);
|
|
|
|
|
|
|
|
const onConnect = useCallback(() => {
|
|
|
|
if (!videoRef.current?.isConnected || !wsURL || wsRef.current) return false;
|
|
|
|
|
|
|
|
setWsState(WebSocket.CONNECTING);
|
|
|
|
|
2024-02-28 23:23:56 +01:00
|
|
|
// TODO may need to check this later
|
|
|
|
// eslint-disable-next-line
|
2024-01-04 00:38:52 +01:00
|
|
|
connectTS = Date.now();
|
|
|
|
|
|
|
|
wsRef.current = new WebSocket(wsURL);
|
|
|
|
wsRef.current.binaryType = "arraybuffer";
|
|
|
|
wsRef.current.addEventListener("open", () => onOpen());
|
|
|
|
wsRef.current.addEventListener("close", () => onClose());
|
|
|
|
}, [wsURL]);
|
|
|
|
|
|
|
|
const onDisconnect = useCallback(() => {
|
|
|
|
setWsState(WebSocket.CLOSED);
|
|
|
|
if (wsRef.current) {
|
|
|
|
wsRef.current.close();
|
|
|
|
wsRef.current = null;
|
|
|
|
}
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
const onOpen = useCallback(() => {
|
|
|
|
setWsState(WebSocket.OPEN);
|
|
|
|
|
|
|
|
wsRef.current?.addEventListener("message", (ev) => {
|
|
|
|
if (typeof ev.data === "string") {
|
|
|
|
const msg = JSON.parse(ev.data);
|
|
|
|
for (const mode in onmessageRef.current) {
|
|
|
|
onmessageRef.current[mode](msg);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ondataRef.current?.(ev.data);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
ondataRef.current = null;
|
|
|
|
onmessageRef.current = {};
|
|
|
|
|
|
|
|
onMse();
|
2024-02-28 23:23:56 +01:00
|
|
|
// only run once
|
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
2024-01-04 00:38:52 +01:00
|
|
|
}, []);
|
|
|
|
|
|
|
|
const onClose = useCallback(() => {
|
|
|
|
if (wsState === WebSocket.CLOSED) return;
|
|
|
|
|
|
|
|
setWsState(WebSocket.CONNECTING);
|
|
|
|
wsRef.current = null;
|
|
|
|
|
|
|
|
const delay = Math.max(RECONNECT_TIMEOUT - (Date.now() - connectTS), 0);
|
|
|
|
|
|
|
|
reconnectTIDRef.current = window.setTimeout(() => {
|
|
|
|
reconnectTIDRef.current = null;
|
|
|
|
onConnect();
|
|
|
|
}, delay);
|
|
|
|
}, [wsState, connectTS, onConnect]);
|
|
|
|
|
|
|
|
const onMse = () => {
|
|
|
|
if ("ManagedMediaSource" in window) {
|
|
|
|
const MediaSource = window.ManagedMediaSource;
|
|
|
|
|
|
|
|
msRef.current?.addEventListener(
|
|
|
|
"sourceopen",
|
|
|
|
() => {
|
|
|
|
send({
|
|
|
|
type: "mse",
|
2024-02-28 23:23:56 +01:00
|
|
|
// @ts-expect-error for typing
|
2024-01-04 00:38:52 +01:00
|
|
|
value: codecs(MediaSource.isTypeSupported),
|
|
|
|
});
|
|
|
|
},
|
2024-02-28 23:23:56 +01:00
|
|
|
{ once: true },
|
2024-01-04 00:38:52 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
if (videoRef.current) {
|
|
|
|
videoRef.current.disableRemotePlayback = true;
|
|
|
|
videoRef.current.srcObject = msRef.current;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
msRef.current?.addEventListener(
|
|
|
|
"sourceopen",
|
|
|
|
() => {
|
|
|
|
URL.revokeObjectURL(videoRef.current?.src || "");
|
|
|
|
send({
|
|
|
|
type: "mse",
|
|
|
|
value: codecs(MediaSource.isTypeSupported),
|
|
|
|
});
|
|
|
|
},
|
2024-02-28 23:23:56 +01:00
|
|
|
{ once: true },
|
2024-01-04 00:38:52 +01:00
|
|
|
);
|
|
|
|
videoRef.current!.src = URL.createObjectURL(msRef.current!);
|
|
|
|
videoRef.current!.srcObject = null;
|
|
|
|
}
|
|
|
|
play();
|
|
|
|
|
|
|
|
onmessageRef.current["mse"] = (msg) => {
|
|
|
|
if (msg.type !== "mse") return;
|
|
|
|
|
|
|
|
const sb = msRef.current?.addSourceBuffer(msg.value);
|
|
|
|
sb?.addEventListener("updateend", () => {
|
|
|
|
if (sb.updating) return;
|
|
|
|
|
|
|
|
try {
|
|
|
|
if (bufLen > 0) {
|
|
|
|
const data = buf.slice(0, bufLen);
|
|
|
|
bufLen = 0;
|
|
|
|
sb.appendBuffer(data);
|
|
|
|
} else if (sb.buffered && sb.buffered.length) {
|
|
|
|
const end = sb.buffered.end(sb.buffered.length - 1) - 15;
|
|
|
|
const start = sb.buffered.start(0);
|
|
|
|
if (end > start) {
|
|
|
|
sb.remove(start, end);
|
|
|
|
msRef.current?.setLiveSeekableRange(end, end + 15);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (e) {
|
2024-02-28 23:23:56 +01:00
|
|
|
// eslint-disable-next-line no-console
|
2024-01-04 00:38:52 +01:00
|
|
|
console.debug(e);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
const buf = new Uint8Array(2 * 1024 * 1024);
|
|
|
|
let bufLen = 0;
|
|
|
|
|
|
|
|
ondataRef.current = (data) => {
|
|
|
|
if (sb?.updating || bufLen > 0) {
|
|
|
|
const b = new Uint8Array(data);
|
|
|
|
buf.set(b, bufLen);
|
|
|
|
bufLen += b.byteLength;
|
|
|
|
// console.debug("VideoRTC.buffer", b.byteLength, bufLen);
|
|
|
|
} else {
|
|
|
|
try {
|
|
|
|
sb?.appendBuffer(data);
|
|
|
|
} catch (e) {
|
2024-02-28 23:23:56 +01:00
|
|
|
// eslint-disable-next-line no-console
|
2024-01-04 00:38:52 +01:00
|
|
|
console.debug(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
useEffect(() => {
|
2024-02-13 02:28:36 +01:00
|
|
|
if (!playbackEnabled) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-01-04 00:38:52 +01:00
|
|
|
// iOS 17.1+ uses ManagedMediaSource
|
|
|
|
const MediaSourceConstructor =
|
|
|
|
"ManagedMediaSource" in window ? window.ManagedMediaSource : MediaSource;
|
|
|
|
|
2024-02-28 23:23:56 +01:00
|
|
|
// @ts-expect-error for typing
|
2024-01-04 00:38:52 +01:00
|
|
|
msRef.current = new MediaSourceConstructor();
|
|
|
|
|
|
|
|
if ("hidden" in document && visibilityCheck) {
|
|
|
|
document.addEventListener("visibilitychange", () => {
|
|
|
|
if (document.hidden) {
|
|
|
|
onDisconnect();
|
|
|
|
} else if (videoRef.current?.isConnected) {
|
|
|
|
onConnect();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if ("IntersectionObserver" in window && visibilityThreshold) {
|
|
|
|
const observer = new IntersectionObserver(
|
|
|
|
(entries) => {
|
|
|
|
entries.forEach((entry) => {
|
|
|
|
if (!entry.isIntersecting) {
|
|
|
|
onDisconnect();
|
|
|
|
} else if (videoRef.current?.isConnected) {
|
|
|
|
onConnect();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
2024-02-28 23:23:56 +01:00
|
|
|
{ threshold: visibilityThreshold },
|
2024-01-04 00:38:52 +01:00
|
|
|
);
|
|
|
|
observer.observe(videoRef.current!);
|
|
|
|
}
|
|
|
|
|
|
|
|
onConnect();
|
2024-02-13 00:34:45 +01:00
|
|
|
|
|
|
|
return () => {
|
|
|
|
onDisconnect();
|
|
|
|
};
|
2024-02-28 23:23:56 +01:00
|
|
|
// we know that these deps are correct
|
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
2024-02-13 02:28:36 +01:00
|
|
|
}, [playbackEnabled, onDisconnect, onConnect]);
|
2024-01-04 00:38:52 +01:00
|
|
|
|
|
|
|
return (
|
|
|
|
<video
|
|
|
|
ref={videoRef}
|
2024-02-10 13:30:53 +01:00
|
|
|
className={className}
|
2024-01-04 00:38:52 +01:00
|
|
|
playsInline
|
|
|
|
preload="auto"
|
2024-02-10 13:30:53 +01:00
|
|
|
onLoadedData={onPlaying}
|
2024-01-04 00:38:52 +01:00
|
|
|
muted
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export default MSEPlayer;
|