Don't stop mse when pip is enabled and not visible

This commit is contained in:
Nicolas Mowen 2024-04-02 08:15:24 -06:00
parent d1082ec305
commit ff849b6f7d

View File

@ -33,8 +33,7 @@ function MSEPlayer({
"opus", // OPUS Chrome, Firefox "opus", // OPUS Chrome, Firefox
]; ];
const visibilityThreshold: number = 0; const visibilityCheck: boolean = !pip;
const visibilityCheck: boolean = true;
const [wsState, setWsState] = useState<number>(WebSocket.CLOSED); const [wsState, setWsState] = useState<number>(WebSocket.CLOSED);
@ -235,6 +234,22 @@ function MSEPlayer({
// @ts-expect-error for typing // @ts-expect-error for typing
msRef.current = new MediaSourceConstructor(); msRef.current = new MediaSourceConstructor();
onConnect();
return () => {
onDisconnect();
};
// we know that these deps are correct
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [playbackEnabled, onDisconnect, onConnect]);
// check visibility
useEffect(() => {
if (!playbackEnabled) {
return;
}
if ("hidden" in document && visibilityCheck) { if ("hidden" in document && visibilityCheck) {
document.addEventListener("visibilitychange", () => { document.addEventListener("visibilitychange", () => {
if (document.hidden) { if (document.hidden) {
@ -244,31 +259,7 @@ function MSEPlayer({
} }
}); });
} }
}, [playbackEnabled, visibilityCheck, onConnect, onDisconnect]);
if ("IntersectionObserver" in window && visibilityThreshold) {
const observer = new IntersectionObserver(
(entries) => {
entries.forEach((entry) => {
if (!entry.isIntersecting) {
onDisconnect();
} else if (videoRef.current?.isConnected) {
onConnect();
}
});
},
{ threshold: visibilityThreshold },
);
observer.observe(videoRef.current!);
}
onConnect();
return () => {
onDisconnect();
};
// we know that these deps are correct
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [playbackEnabled, onDisconnect, onConnect]);
// control pip // control pip