From 15dcf1fcc890ea16e548e2bab0b36d41b2e86c22 Mon Sep 17 00:00:00 2001 From: Nicolas Mowen Date: Tue, 2 Apr 2024 13:39:11 -0600 Subject: [PATCH] Don't stop mse in background when using pip (#10794) * Don't stop mse when pip is enabled and not visible * Cleanup visibility listener --- web/src/components/player/MsePlayer.tsx | 55 ++++++++++++------------- 1 file changed, 27 insertions(+), 28 deletions(-) diff --git a/web/src/components/player/MsePlayer.tsx b/web/src/components/player/MsePlayer.tsx index 7c88780a8..f25557d16 100644 --- a/web/src/components/player/MsePlayer.tsx +++ b/web/src/components/player/MsePlayer.tsx @@ -33,8 +33,7 @@ function MSEPlayer({ "opus", // OPUS Chrome, Firefox ]; - const visibilityThreshold: number = 0; - const visibilityCheck: boolean = true; + const visibilityCheck: boolean = !pip; const [wsState, setWsState] = useState(WebSocket.CLOSED); @@ -235,32 +234,6 @@ function MSEPlayer({ // @ts-expect-error for typing 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(); - } - }); - }, - { threshold: visibilityThreshold }, - ); - observer.observe(videoRef.current!); - } - onConnect(); return () => { @@ -270,6 +243,32 @@ function MSEPlayer({ // eslint-disable-next-line react-hooks/exhaustive-deps }, [playbackEnabled, onDisconnect, onConnect]); + // check visibility + + useEffect(() => { + if (!playbackEnabled || !visibilityCheck) { + return; + } + + if (!("hidden" in document)) { + return; + } + + const listener = () => { + if (document.hidden) { + onDisconnect(); + } else if (videoRef.current?.isConnected) { + onConnect(); + } + }; + + document.addEventListener("visibilitychange", listener); + + return () => { + document.removeEventListener("visibilitychange", listener); + }; + }, [playbackEnabled, visibilityCheck, onConnect, onDisconnect]); + // control pip useEffect(() => {