Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve Recordings loading #10462

Merged
merged 9 commits into from
Mar 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions web/src/components/player/HlsVideoPlayer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ type HlsVideoPlayerProps = {
onClipEnded?: () => void;
onPlayerLoaded?: () => void;
onTimeUpdate?: (time: number) => void;
onPlaying?: () => void;
};
export default function HlsVideoPlayer({
className,
Expand All @@ -51,6 +52,7 @@ export default function HlsVideoPlayer({
onClipEnded,
onPlayerLoaded,
onTimeUpdate,
onPlaying,
}: HlsVideoPlayerProps) {
// playback

Expand Down Expand Up @@ -183,6 +185,7 @@ export default function HlsVideoPlayer({
setMobileCtrlTimeout(setTimeout(() => setControls(false), 4000));
}
}}
onPlaying={onPlaying}
onPause={() => {
setIsPlaying(false);

Expand Down
27 changes: 13 additions & 14 deletions web/src/components/player/PreviewPlayer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -272,26 +272,25 @@ class PreviewVideoController extends PreviewController {
}

if (this.timeToSeek) {
if (
Math.round(this.previewRef.current.currentTime + this.preview.start) !=
Math.round(this.timeToSeek)
) {
if (isAndroid) {
const currentTs =
this.previewRef.current.currentTime + this.preview.start;
const diff = this.timeToSeek - currentTs;
const diff =
Math.round(this.timeToSeek) -
Math.round(this.previewRef.current.currentTime + this.preview.start);

if (Math.abs(diff) > 1) {
let seekTime;
if (isAndroid) {
if (diff < 30) {
this.previewRef.current.currentTime =
this.previewRef.current.currentTime + diff / 2;
seekTime = Math.round(
this.previewRef.current.currentTime + diff / 2,
);
} else {
this.previewRef.current.currentTime =
this.timeToSeek - this.preview.start;
seekTime = Math.round(this.timeToSeek - this.preview.start);
}
} else {
this.previewRef.current.currentTime =
this.timeToSeek - this.preview.start;
seekTime = this.timeToSeek - this.preview.start;
}

this.previewRef.current.currentTime = seekTime;
} else {
this.seeking = false;
this.timeToSeek = undefined;
Expand Down
16 changes: 15 additions & 1 deletion web/src/components/player/dynamic/DynamicVideoController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,27 @@ export class DynamicVideoController {
this.playerController.currentTime = seekSeconds;

if (play) {
this.playerController.play();
this.waitAndPlay();
} else {
this.playerController.pause();
}
}
}

waitAndPlay() {
return new Promise((resolve) => {
const onSeekedHandler = () => {
this.playerController.removeEventListener("seeked", onSeekedHandler);
this.playerController.play();
resolve(undefined);
};

this.playerController.addEventListener("seeked", onSeekedHandler, {
once: true,
});
});
}

seekToTimelineItem(timeline: Timeline) {
this.playerController.pause();
this.seekToTimestamp(timeline.timestamp + this.annotationOffset);
Expand Down
18 changes: 15 additions & 3 deletions web/src/components/player/dynamic/DynamicVideoPlayer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,19 @@ export default function DynamicVideoPlayer({

// initial state

const [isLoading, setIsLoading] = useState(false);
const [source, setSource] = useState(
`${apiHost}vod/${camera}/start/${timeRange.start}/end/${timeRange.end}/master.m3u8`,
);

// start at correct time

useEffect(() => {
if (isScrubbing) {
setIsLoading(true);
}
}, [isScrubbing]);

const onPlayerLoaded = useCallback(() => {
if (!controller || !startTimestamp) {
return;
Expand Down Expand Up @@ -140,6 +147,7 @@ export default function DynamicVideoPlayer({
setSource(
`${apiHost}vod/${camera}/start/${timeRange.start}/end/${timeRange.end}/master.m3u8`,
);
setIsLoading(true);

controller.newPlayback({
recordings: recordings ?? [],
Expand All @@ -151,14 +159,17 @@ export default function DynamicVideoPlayer({

return (
<div className={`relative ${className ?? ""} cursor-pointer`}>
<div className={`w-full relative ${isScrubbing ? "hidden" : "visible"}`}>
<div
className={`w-full relative ${isScrubbing || isLoading ? "hidden" : "visible"}`}
>
<HlsVideoPlayer
className={` ${wideVideo ? "" : "aspect-video"}`}
className={`${wideVideo ? "" : "aspect-video"}`}
videoRef={playerRef}
currentSource={source}
onTimeUpdate={onTimeUpdate}
onPlayerLoaded={onPlayerLoaded}
onClipEnded={onClipEnded}
onPlaying={() => setIsLoading(false)}
>
{config && focusedItem && (
<TimelineEventOverlay
Expand All @@ -169,10 +180,11 @@ export default function DynamicVideoPlayer({
</HlsVideoPlayer>
</div>
<PreviewPlayer
className={`${isScrubbing ? "visible" : "hidden"} ${className ?? ""}`}
className={`${isScrubbing || isLoading ? "visible" : "hidden"} ${className ?? ""}`}
camera={camera}
timeRange={timeRange}
cameraPreviews={cameraPreviews}
startTime={startTimestamp}
onControllerReady={(previewController) => {
setPreviewController(previewController);
}}
Expand Down
17 changes: 17 additions & 0 deletions web/src/views/events/RecordingView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,7 @@ export function MobileRecordingView({
relevantPreviews,
allCameras,
}: MobileRecordingViewProps) {
const { data: config } = useSWR<FrigateConfig>("config");
const navigate = useNavigate();
const contentRef = useRef<HTMLDivElement | null>(null);

Expand All @@ -310,6 +311,21 @@ export function MobileRecordingView({
const [playbackCamera, setPlaybackCamera] = useState(startCamera);
const [playbackStart, setPlaybackStart] = useState(startTime);

const grow = useMemo(() => {
if (!config) {
return "aspect-video";
}

const aspectRatio =
config.cameras[playbackCamera].detect.width /
config.cameras[playbackCamera].detect.height;
if (aspectRatio > 2) {
return "aspect-wide";
} else {
return "aspect-video";
}
}, [config, playbackCamera]);

// timeline time

const timeRange = useMemo(() => getChunkedTimeDay(startTime), [startTime]);
Expand Down Expand Up @@ -453,6 +469,7 @@ export function MobileRecordingView({

<div>
<DynamicVideoPlayer
className={`w-full ${grow}`}
camera={playbackCamera}
timeRange={currentTimeRange}
cameraPreviews={relevantPreviews || []}
Expand Down
Loading