Use webrtc for safari

This commit is contained in:
Nicolas Mowen 2024-02-13 07:50:30 -07:00
parent 2806d1b027
commit 51ca862683
3 changed files with 22 additions and 12 deletions

View File

@ -87,6 +87,7 @@ export default function LivePlayer({
<WebRtcPlayer <WebRtcPlayer
className={`rounded-2xl h-full ${liveReady ? "" : "hidden"}`} className={`rounded-2xl h-full ${liveReady ? "" : "hidden"}`}
camera={cameraConfig.live.stream_name} camera={cameraConfig.live.stream_name}
playbackEnabled={cameraActive}
onPlaying={() => setLiveReady(true)} onPlaying={() => setLiveReady(true)}
/> />
); );

View File

@ -4,16 +4,21 @@ import { useCallback, useEffect, useRef } from "react";
type WebRtcPlayerProps = { type WebRtcPlayerProps = {
className?: string; className?: string;
camera: string; camera: string;
playbackEnabled?: boolean;
onPlaying?: () => void; onPlaying?: () => void;
}; };
export default function WebRtcPlayer({ export default function WebRtcPlayer({
className, className,
camera, camera,
playbackEnabled = true,
onPlaying, onPlaying,
}: WebRtcPlayerProps) { }: WebRtcPlayerProps) {
// camera states
const pcRef = useRef<RTCPeerConnection | undefined>(); const pcRef = useRef<RTCPeerConnection | undefined>();
const videoRef = useRef<HTMLVideoElement | null>(null); const videoRef = useRef<HTMLVideoElement | null>(null);
const PeerConnection = useCallback( const PeerConnection = useCallback(
async (media: string) => { async (media: string) => {
if (!videoRef.current) { if (!videoRef.current) {
@ -129,6 +134,10 @@ export default function WebRtcPlayer({
return; return;
} }
if (!playbackEnabled) {
return;
}
const url = `${baseUrl.replace( const url = `${baseUrl.replace(
/^http/, /^http/,
"ws" "ws"
@ -143,10 +152,9 @@ export default function WebRtcPlayer({
pcRef.current = undefined; pcRef.current = undefined;
} }
}; };
}, [camera, connect, PeerConnection, pcRef, videoRef]); }, [camera, connect, PeerConnection, pcRef, videoRef, playbackEnabled]);
return ( return (
<div>
<video <video
ref={videoRef} ref={videoRef}
className={className} className={className}
@ -155,6 +163,5 @@ export default function WebRtcPlayer({
muted muted
onLoadedData={onPlaying} onLoadedData={onPlaying}
/> />
</div>
); );
} }

View File

@ -5,6 +5,7 @@ import { ScrollArea, ScrollBar } from "@/components/ui/scroll-area";
import { TooltipProvider } from "@/components/ui/tooltip"; import { TooltipProvider } from "@/components/ui/tooltip";
import { Event as FrigateEvent } from "@/types/event"; import { Event as FrigateEvent } from "@/types/event";
import { FrigateConfig } from "@/types/frigateConfig"; import { FrigateConfig } from "@/types/frigateConfig";
import { isSafari } from "@/utils/browserUtil";
import { useCallback, useEffect, useMemo, useState } from "react"; import { useCallback, useEffect, useMemo, useState } from "react";
import useSWR from "swr"; import useSWR from "swr";
@ -64,6 +65,7 @@ function Live() {
.sort((aConf, bConf) => aConf.ui.order - bConf.ui.order); .sort((aConf, bConf) => aConf.ui.order - bConf.ui.order);
}, [config]); }, [config]);
const safari = isSafari();
const [windowVisible, setWindowVisible] = useState(true); const [windowVisible, setWindowVisible] = useState(true);
const visibilityListener = useCallback(() => { const visibilityListener = useCallback(() => {
setWindowVisible(document.visibilityState == "visible"); setWindowVisible(document.visibilityState == "visible");
@ -109,7 +111,7 @@ function Live() {
className={`mb-2 md:mb-0 rounded-2xl bg-black ${grow}`} className={`mb-2 md:mb-0 rounded-2xl bg-black ${grow}`}
windowVisible={windowVisible} windowVisible={windowVisible}
cameraConfig={camera} cameraConfig={camera}
preferredLiveMode="mse" preferredLiveMode={safari ? "webrtc" : "mse"}
/> />
); );
})} })}