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
className={`rounded-2xl h-full ${liveReady ? "" : "hidden"}`}
camera={cameraConfig.live.stream_name}
playbackEnabled={cameraActive}
onPlaying={() => setLiveReady(true)}
/>
);

View File

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

View File

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