-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Jump to live when exceeding buffer time threshold in MSE player * clean up * Try adjusting playback rate instead of jumping to live * clean up * fallback to webrtc if enabled before jsmpeg * baseline * clean up * remove comments * adaptive playback rate and intelligent switching improvements * increase logging and reset live mode after camera is no longer active on dashboard only * jump to live on safari/iOS * clean up * clean up * refactor camera live mode hook * remove key listener * resolve conflicts
- Loading branch information
1 parent
758b0f9
commit ef46451
Showing
7 changed files
with
229 additions
and
121 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,65 @@ | ||
import { CameraConfig, FrigateConfig } from "@/types/frigateConfig"; | ||
import { useMemo } from "react"; | ||
import { useCallback, useEffect, useState } from "react"; | ||
import useSWR from "swr"; | ||
import { usePersistence } from "./use-persistence"; | ||
import { LivePlayerMode } from "@/types/live"; | ||
|
||
export default function useCameraLiveMode( | ||
cameraConfig: CameraConfig, | ||
preferredMode?: LivePlayerMode, | ||
): LivePlayerMode | undefined { | ||
cameras: CameraConfig[], | ||
windowVisible: boolean, | ||
) { | ||
const { data: config } = useSWR<FrigateConfig>("config"); | ||
const [preferredLiveModes, setPreferredLiveModes] = useState<{ | ||
[key: string]: LivePlayerMode; | ||
}>({}); | ||
|
||
const restreamEnabled = useMemo(() => { | ||
if (!config) { | ||
return false; | ||
} | ||
useEffect(() => { | ||
if (!cameras) return; | ||
|
||
return ( | ||
cameraConfig && | ||
Object.keys(config.go2rtc.streams || {}).includes( | ||
cameraConfig.live.stream_name, | ||
) | ||
const mseSupported = | ||
"MediaSource" in window || "ManagedMediaSource" in window; | ||
|
||
const newPreferredLiveModes = cameras.reduce( | ||
(acc, camera) => { | ||
const isRestreamed = | ||
config && | ||
Object.keys(config.go2rtc.streams || {}).includes( | ||
camera.live.stream_name, | ||
); | ||
|
||
if (!mseSupported) { | ||
acc[camera.name] = isRestreamed ? "webrtc" : "jsmpeg"; | ||
} else { | ||
acc[camera.name] = isRestreamed ? "mse" : "jsmpeg"; | ||
} | ||
return acc; | ||
}, | ||
{} as { [key: string]: LivePlayerMode }, | ||
); | ||
}, [config, cameraConfig]); | ||
const defaultLiveMode = useMemo<LivePlayerMode | undefined>(() => { | ||
if (config) { | ||
if (restreamEnabled) { | ||
return preferredMode || "mse"; | ||
} | ||
|
||
return "jsmpeg"; | ||
} | ||
|
||
return undefined; | ||
}, [config, preferredMode, restreamEnabled]); | ||
const [viewSource] = usePersistence<LivePlayerMode>( | ||
`${cameraConfig.name}-source`, | ||
defaultLiveMode, | ||
|
||
setPreferredLiveModes(newPreferredLiveModes); | ||
}, [cameras, config, windowVisible]); | ||
|
||
const resetPreferredLiveMode = useCallback( | ||
(cameraName: string) => { | ||
const mseSupported = | ||
"MediaSource" in window || "ManagedMediaSource" in window; | ||
const isRestreamed = | ||
config && Object.keys(config.go2rtc.streams || {}).includes(cameraName); | ||
|
||
setPreferredLiveModes((prevModes) => { | ||
const newModes = { ...prevModes }; | ||
|
||
if (!mseSupported) { | ||
newModes[cameraName] = isRestreamed ? "webrtc" : "jsmpeg"; | ||
} else { | ||
newModes[cameraName] = isRestreamed ? "mse" : "jsmpeg"; | ||
} | ||
|
||
return newModes; | ||
}); | ||
}, | ||
[config], | ||
); | ||
|
||
if ( | ||
restreamEnabled && | ||
(preferredMode == "mse" || preferredMode == "webrtc") | ||
) { | ||
return preferredMode; | ||
} else { | ||
return viewSource; | ||
} | ||
return { preferredLiveModes, setPreferredLiveModes, resetPreferredLiveMode }; | ||
} |
Oops, something went wrong.