refactor: rewrite video player
This commit is contained in:
34 files changed
+3766
-3007
No files matched your search
@@ -0,0 +1,34 @@
|
||||
import { CircularProgress } from "@mui/material";
|
||||
import React from "react";
|
||||
import { useShallow } from "zustand/shallow";
|
||||
import { usePlaybackStore } from "@/utils/store/playback";
|
||||
|
||||
const LoadingIndicator = () => {
|
||||
const { isBuffering } = usePlaybackStore(
|
||||
useShallow((state) => ({
|
||||
isBuffering: state.playerState.isBuffering,
|
||||
})),
|
||||
);
|
||||
if (!isBuffering) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return (
|
||||
<div
|
||||
style={{
|
||||
zIndex: 2,
|
||||
position: "absolute",
|
||||
height: "100vh",
|
||||
width: "100vw",
|
||||
top: 0,
|
||||
left: 0,
|
||||
display: "flex",
|
||||
justifyContent: "center",
|
||||
alignItems: "center",
|
||||
}}
|
||||
>
|
||||
<CircularProgress size={72} thickness={1.4} />
|
||||
</div>
|
||||
);
|
||||
};
|
||||
export default LoadingIndicator;
|
||||
@@ -0,0 +1,27 @@
|
||||
import { Typography } from "@mui/material";
|
||||
import React from "react";
|
||||
import { useShallow } from "zustand/shallow";
|
||||
import ticksDisplay from "@/utils/methods/ticksDisplay";
|
||||
import { usePlaybackStore } from "@/utils/store/playback";
|
||||
|
||||
const ProgressDislay = () => {
|
||||
const { currentTime, itemDuration, isUserSeeking, seekValue } =
|
||||
usePlaybackStore(
|
||||
useShallow((state) => ({
|
||||
currentTime: state.playerState.currentTime,
|
||||
itemDuration: state.metadata.itemDuration,
|
||||
isUserSeeking: state.playerState.isUserSeeking,
|
||||
seekValue: state.playerState.seekValue,
|
||||
})),
|
||||
);
|
||||
return (
|
||||
<div className="video-player-osd-controls-timeline-text">
|
||||
<Typography>
|
||||
{ticksDisplay(isUserSeeking ? (seekValue ?? 0) : (currentTime ?? 0))}
|
||||
</Typography>
|
||||
<Typography>{ticksDisplay(itemDuration)}</Typography>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
export default ProgressDislay;
|
||||
@@ -0,0 +1,36 @@
|
||||
import { LinearProgress } from "@mui/material";
|
||||
import { AnimatePresence, motion } from "motion/react";
|
||||
import React from "react";
|
||||
import { useShallow } from "zustand/shallow";
|
||||
import { usePlaybackStore } from "@/utils/store/playback";
|
||||
|
||||
const VolumeChangeOverlay = () => {
|
||||
const { playerVolume, showVolumeIndicator } = usePlaybackStore(
|
||||
useShallow((state) => ({
|
||||
playerVolume: state.playerState.volume,
|
||||
showVolumeIndicator: state.isVolumeInidcatorVisible,
|
||||
})),
|
||||
);
|
||||
return (
|
||||
<AnimatePresence>
|
||||
{showVolumeIndicator && (
|
||||
<motion.div
|
||||
initial={{ opacity: 0 }}
|
||||
animate={{ opacity: 1 }}
|
||||
exit={{ opacity: 0 }}
|
||||
className="video-volume-indicator glass"
|
||||
>
|
||||
<div className="material-symbols-rounded">
|
||||
{playerVolume > 0.7 ? "volume_up" : "volume_down"}
|
||||
</div>
|
||||
<LinearProgress
|
||||
style={{ width: "100%" }}
|
||||
value={playerVolume * 100}
|
||||
variant="determinate"
|
||||
/>
|
||||
</motion.div>
|
||||
)}
|
||||
</AnimatePresence>
|
||||
);
|
||||
};
|
||||
export default VolumeChangeOverlay;
|
||||
@@ -0,0 +1,307 @@
|
||||
import type { TrickplayInfo } from "@jellyfin/sdk/lib/generated-client";
|
||||
import { Slider, Tooltip, Typography } from "@mui/material";
|
||||
import type { Instance } from "@popperjs/core";
|
||||
import React, { type MouseEvent, useMemo, useRef, useState } from "react";
|
||||
import { useShallow } from "zustand/shallow";
|
||||
import { ticksToMs } from "@/utils/date/time";
|
||||
import { useApiInContext } from "@/utils/store/api";
|
||||
import { usePlaybackStore } from "@/utils/store/playback";
|
||||
|
||||
const ticksDisplay = (ticks: number) => {
|
||||
const time = Math.round(ticks / 10000);
|
||||
let formatedTime = "";
|
||||
let timeSec = Math.floor(time / 1000);
|
||||
let timeMin = Math.floor(timeSec / 60);
|
||||
timeSec -= timeMin * 60;
|
||||
timeSec = timeSec === 0 ? 0o0 : timeSec;
|
||||
const timeHr = Math.floor(timeMin / 60);
|
||||
timeMin -= timeHr * 60;
|
||||
formatedTime = `${timeHr.toLocaleString([], {
|
||||
minimumIntegerDigits: 2,
|
||||
useGrouping: false,
|
||||
})}:${timeMin.toLocaleString([], {
|
||||
minimumIntegerDigits: 2,
|
||||
useGrouping: false,
|
||||
})}:${timeSec.toLocaleString([], {
|
||||
minimumIntegerDigits: 2,
|
||||
useGrouping: false,
|
||||
})}`;
|
||||
return formatedTime;
|
||||
};
|
||||
|
||||
const BubbleSlider = () => {
|
||||
const api = useApiInContext((state) => state.api);
|
||||
const {
|
||||
seekValue,
|
||||
currentTime,
|
||||
isUserSeeking,
|
||||
// setIsUserSeeking,
|
||||
// setSeekValue,
|
||||
// seekTo,
|
||||
itemDuration,
|
||||
itemChapters,
|
||||
mediaSource,
|
||||
itemTrickplay,
|
||||
itemId,
|
||||
handleStartSeek,
|
||||
handleStopSeek,
|
||||
} = usePlaybackStore(
|
||||
useShallow((state) => ({
|
||||
seekValue: state.playerState.seekValue,
|
||||
currentTime: state.playerState.currentTime,
|
||||
isUserSeeking: state.playerState.isUserSeeking,
|
||||
// setIsUserSeeking: state.setIsUserSeeking,
|
||||
// setSeekValue: state.setSeekValue,
|
||||
// seekTo: state.seekTo,
|
||||
itemDuration: state.metadata.item.RunTimeTicks,
|
||||
itemChapters: state.metadata.item.Chapters,
|
||||
mediaSource: state.mediaSource,
|
||||
itemTrickplay: state.metadata.item.Trickplay,
|
||||
itemId: state.metadata.item.Id,
|
||||
handleStartSeek: state.handleStartSeek,
|
||||
handleStopSeek: state.handleStopSeek,
|
||||
})),
|
||||
);
|
||||
|
||||
const positionRef = useRef<{ x: number; y: number }>({
|
||||
x: 0,
|
||||
y: 0,
|
||||
});
|
||||
const popperRef = useRef<Instance>(null);
|
||||
const areaRef = useRef<HTMLDivElement>(null);
|
||||
|
||||
const [hoverProgress, setHoverProgress] = useState(0);
|
||||
|
||||
const handleSliderHover = (event: MouseEvent) => {
|
||||
positionRef.current = { x: event.clientX, y: event.clientY };
|
||||
const rect = areaRef.current!.getBoundingClientRect();
|
||||
const width = rect.width;
|
||||
const distX = event.clientX - rect.left;
|
||||
const percentageCovered = distX / width;
|
||||
setHoverProgress(percentageCovered * (itemDuration ?? 0));
|
||||
if (popperRef.current != null) {
|
||||
popperRef.current.update();
|
||||
}
|
||||
};
|
||||
|
||||
const chapterMarks = useMemo(() => {
|
||||
const marks: { value: number }[] = [];
|
||||
itemChapters?.map((val) => {
|
||||
marks.push({ value: val.StartPositionTicks ?? 0 });
|
||||
});
|
||||
return marks;
|
||||
}, [itemChapters]);
|
||||
|
||||
const sliderDisplayFormat = (value: number) => {
|
||||
const currentChapter = itemChapters?.filter((chapter, index) => {
|
||||
if (index + 1 === itemChapters?.length) {
|
||||
return chapter;
|
||||
}
|
||||
if (
|
||||
(itemChapters?.[index + 1]?.StartPositionTicks ?? value) - value >= 0 &&
|
||||
(chapter.StartPositionTicks ?? value) - value < 0
|
||||
) {
|
||||
return chapter;
|
||||
}
|
||||
});
|
||||
|
||||
let trickplayResolution: TrickplayInfo | undefined;
|
||||
const trickplayResolutions = mediaSource.id
|
||||
? itemTrickplay?.[mediaSource.id]
|
||||
: null;
|
||||
if (trickplayResolutions) {
|
||||
let bestWidth: number | undefined;
|
||||
const maxWidth = window.screen.width * window.devicePixelRatio * 0.2;
|
||||
for (const [_, trickInfo] of Object.entries(trickplayResolutions)) {
|
||||
if (
|
||||
!bestWidth ||
|
||||
(trickInfo.Width &&
|
||||
((trickInfo.Width < bestWidth && bestWidth > maxWidth) ||
|
||||
(trickInfo.Width > bestWidth && bestWidth <= maxWidth)))
|
||||
) {
|
||||
bestWidth = trickInfo.Width;
|
||||
}
|
||||
}
|
||||
if (bestWidth) {
|
||||
trickplayResolution = trickplayResolutions[bestWidth];
|
||||
}
|
||||
}
|
||||
if (
|
||||
trickplayResolution?.TileWidth &&
|
||||
trickplayResolution.TileHeight &&
|
||||
trickplayResolution.Width &&
|
||||
trickplayResolution.Height
|
||||
) {
|
||||
const currentTrickplayImage = trickplayResolution?.Interval
|
||||
? Math.floor(ticksToMs(value) / trickplayResolution?.Interval)
|
||||
: 0;
|
||||
const trickplayImageSize =
|
||||
trickplayResolution?.TileWidth * trickplayResolution?.TileHeight; // this gives the area of a single tile
|
||||
|
||||
const trickplayImageOffset = currentTrickplayImage % trickplayImageSize; // this gives the tile index inside a trickplay image
|
||||
const index = Math.floor(currentTrickplayImage / trickplayImageSize); // this gives the index of trickplay image
|
||||
|
||||
const imageOffsetX =
|
||||
trickplayImageOffset % trickplayResolution?.TileWidth; // this gives the x coordinate of tile in trickplay image
|
||||
const imageOffsetY = Math.floor(
|
||||
trickplayImageOffset / trickplayResolution?.TileWidth,
|
||||
); // this gives the y coordinate of tile in trickplay image
|
||||
const backgroundOffsetX = -(imageOffsetX * trickplayResolution?.Width);
|
||||
const backgroundOffsetY = -(imageOffsetY * trickplayResolution?.Height);
|
||||
|
||||
const imgUrlParamsObject: Record<string, string> = {
|
||||
api_key: String(api?.accessToken),
|
||||
MediaSourceId: mediaSource.id ?? "",
|
||||
};
|
||||
const imgUrlParams = new URLSearchParams(imgUrlParamsObject).toString();
|
||||
|
||||
const imageAspectRatio =
|
||||
trickplayResolution.Width / trickplayResolution.Height;
|
||||
|
||||
if (currentChapter?.[0]?.Name) {
|
||||
return (
|
||||
<div className="flex flex-column video-osb-bubble">
|
||||
<div
|
||||
className="video-osd-trickplayBubble"
|
||||
style={{
|
||||
background: `url(${api?.basePath}/Videos/${itemId}/Trickplay/${trickplayResolution.Width}/${index}.jpg?${imgUrlParams})`,
|
||||
backgroundPositionX: `${backgroundOffsetX}px`,
|
||||
backgroundPositionY: `${backgroundOffsetY}px`,
|
||||
width: "100%",
|
||||
aspectRatio: imageAspectRatio,
|
||||
}}
|
||||
/>
|
||||
<Typography variant="h6" px="12px" pt={1}>
|
||||
{currentChapter?.[0]?.Name}
|
||||
</Typography>
|
||||
<Typography px="12px" pb={1}>
|
||||
{ticksDisplay(value)}
|
||||
</Typography>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
return (
|
||||
<div className="flex flex-column video-osb-bubble">
|
||||
<div
|
||||
className="video-osd-trickplayBubble"
|
||||
style={{
|
||||
background: `url(${api?.basePath}/Videos/${itemId}/Trickplay/${trickplayResolution.Width}/${index}.jpg?${imgUrlParams})`,
|
||||
backgroundPositionX: `${backgroundOffsetX}px`,
|
||||
backgroundPositionY: `${backgroundOffsetY}px`,
|
||||
width: "100%",
|
||||
aspectRatio: imageAspectRatio,
|
||||
}}
|
||||
/>
|
||||
<Typography variant="h6" px="12px" py={1}>
|
||||
{ticksDisplay(value)}
|
||||
</Typography>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
if (currentChapter?.[0]?.Name) {
|
||||
return (
|
||||
<div className="flex flex-column video-osb-bubble">
|
||||
<Typography variant="h6" px="12px" pt={1}>
|
||||
{currentChapter?.[0]?.Name}
|
||||
</Typography>
|
||||
<Typography px={2} pb={1}>
|
||||
{ticksDisplay(value)}
|
||||
</Typography>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
return (
|
||||
<div className="flex flex-column video-osb-bubble ">
|
||||
<Typography variant="h6" px="12px" py={1}>
|
||||
{ticksDisplay(value)}
|
||||
</Typography>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
return (
|
||||
<Tooltip
|
||||
// title={sliderDisplayFormat(
|
||||
// positionRef.current.x - areaRef.current!.getBoundingClientRect().x,
|
||||
// )}
|
||||
key={`${positionRef.current}`}
|
||||
title={sliderDisplayFormat(hoverProgress)}
|
||||
placement="top"
|
||||
slotProps={{
|
||||
popper: {
|
||||
popperRef,
|
||||
anchorEl: {
|
||||
getBoundingClientRect: () => {
|
||||
return new DOMRect(
|
||||
positionRef.current.x,
|
||||
areaRef.current!.getBoundingClientRect().y,
|
||||
0,
|
||||
0,
|
||||
);
|
||||
},
|
||||
},
|
||||
// disablePortal: true,
|
||||
},
|
||||
tooltip: {
|
||||
// className: "glass",
|
||||
style: {
|
||||
width: "34em",
|
||||
overflow: "hidden",
|
||||
padding: "12px",
|
||||
borderRadius: "20px",
|
||||
},
|
||||
},
|
||||
}}
|
||||
>
|
||||
<Slider
|
||||
value={isUserSeeking ? seekValue : currentTime}
|
||||
max={itemDuration ?? 0}
|
||||
step={1}
|
||||
onChange={(_, newValue) => {
|
||||
// setIsUserSeeking(true);
|
||||
Array.isArray(newValue)
|
||||
? handleStartSeek(newValue[0])
|
||||
: handleStartSeek(newValue);
|
||||
}}
|
||||
onChangeCommitted={(_, newValue) => {
|
||||
// setIsUserSeeking(false);
|
||||
// Array.isArray(newValue)
|
||||
// ? setSeekValue(newValue[0])
|
||||
// : setSeekValue(newValue);
|
||||
if (Array.isArray(newValue)) {
|
||||
handleStopSeek(newValue[0]);
|
||||
} else {
|
||||
handleStopSeek(newValue);
|
||||
}
|
||||
}}
|
||||
sx={{
|
||||
"& .MuiSlider-thumb": {
|
||||
width: 14,
|
||||
height: 14,
|
||||
transition: "0.1s ease-in-out",
|
||||
opacity: 0,
|
||||
"&.Mui-active": {
|
||||
width: 20,
|
||||
height: 20,
|
||||
opacity: 1,
|
||||
},
|
||||
},
|
||||
"&:hover .MuiSlider-thumb": {
|
||||
opacity: 1,
|
||||
},
|
||||
"& .MuiSlider-rail": {
|
||||
opacity: 0.28,
|
||||
background: "white",
|
||||
},
|
||||
}}
|
||||
marks={chapterMarks}
|
||||
valueLabelDisplay="off"
|
||||
ref={areaRef}
|
||||
onMouseMove={handleSliderHover}
|
||||
/>
|
||||
</Tooltip>
|
||||
);
|
||||
};
|
||||
|
||||
export default BubbleSlider;
|
||||
@@ -0,0 +1,25 @@
|
||||
import { IconButton } from "@mui/material";
|
||||
import React, { useTransition } from "react";
|
||||
import { useShallow } from "zustand/shallow";
|
||||
import { toggleSubtitleTrack, usePlaybackStore } from "@/utils/store/playback";
|
||||
|
||||
const CaptionsButton = () => {
|
||||
const { subtitle } = usePlaybackStore(
|
||||
useShallow((state) => ({ subtitle: state.mediaSource.subtitle })),
|
||||
);
|
||||
|
||||
const [subtitleIsChanging, startSubtitleChange] = useTransition();
|
||||
|
||||
return (
|
||||
<IconButton
|
||||
disabled={subtitle?.allTracks?.length === 0 || subtitleIsChanging}
|
||||
onClick={() => startSubtitleChange(toggleSubtitleTrack)}
|
||||
>
|
||||
<span className={"material-symbols-rounded"}>
|
||||
{subtitle?.enable ? "closed_caption" : "closed_caption_disabled"}
|
||||
</span>
|
||||
</IconButton>
|
||||
);
|
||||
};
|
||||
|
||||
export default CaptionsButton;
|
||||
@@ -0,0 +1,58 @@
|
||||
import { IconButton, Menu, MenuItem } from "@mui/material";
|
||||
import React from "react";
|
||||
import { useShallow } from "zustand/shallow";
|
||||
import { ticksToSec } from "@/utils/date/time";
|
||||
import { usePlaybackStore } from "@/utils/store/playback";
|
||||
|
||||
const ChaptersListButton = () => {
|
||||
const { itemChapters, seekTo } = usePlaybackStore(
|
||||
useShallow((state) => ({
|
||||
itemChapters: state.metadata.item?.Chapters,
|
||||
seekTo: state.seekTo,
|
||||
})),
|
||||
);
|
||||
const [showChapterList, setShowChapterList] =
|
||||
React.useState<null | HTMLElement>(null);
|
||||
const handleShowChapterList = (event: React.MouseEvent<HTMLElement>) => {
|
||||
setShowChapterList(event.currentTarget);
|
||||
};
|
||||
|
||||
return (
|
||||
<>
|
||||
<IconButton
|
||||
disabled={itemChapters?.length === 0}
|
||||
onClick={handleShowChapterList}
|
||||
>
|
||||
<span className="material-symbols-rounded">list</span>
|
||||
</IconButton>
|
||||
<Menu
|
||||
open={Boolean(showChapterList)}
|
||||
anchorEl={showChapterList}
|
||||
onClose={() => setShowChapterList(null)}
|
||||
anchorOrigin={{
|
||||
vertical: "top",
|
||||
horizontal: "center",
|
||||
}}
|
||||
transformOrigin={{
|
||||
vertical: "bottom",
|
||||
horizontal: "center",
|
||||
}}
|
||||
>
|
||||
{itemChapters?.length &&
|
||||
itemChapters?.map((chapter) => (
|
||||
<MenuItem
|
||||
key={chapter.Name}
|
||||
onClick={() => {
|
||||
seekTo(ticksToSec(chapter.StartPositionTicks ?? 0));
|
||||
setShowChapterList(null); // Close the chapter list menu
|
||||
}}
|
||||
>
|
||||
{chapter.Name}
|
||||
</MenuItem>
|
||||
))}
|
||||
</Menu>
|
||||
</>
|
||||
);
|
||||
};
|
||||
|
||||
export default ChaptersListButton;
|
||||
@@ -0,0 +1,20 @@
|
||||
import { IconButton } from "@mui/material";
|
||||
import React from "react";
|
||||
import { useShallow } from "zustand/shallow";
|
||||
import { usePlaybackStore } from "@/utils/store/playback";
|
||||
|
||||
const ForwardButton = () => {
|
||||
const { seekForward } = usePlaybackStore(
|
||||
useShallow((state) => ({
|
||||
seekForward: state.seekForward,
|
||||
})),
|
||||
);
|
||||
|
||||
return (
|
||||
<IconButton onClick={() => seekForward(15)}>
|
||||
<span className="material-symbols-rounded fill">fast_forward</span>
|
||||
</IconButton>
|
||||
);
|
||||
};
|
||||
|
||||
export default ForwardButton;
|
||||
@@ -0,0 +1,23 @@
|
||||
import { IconButton } from "@mui/material";
|
||||
import React from "react";
|
||||
import { useShallow } from "zustand/shallow";
|
||||
import { usePlaybackStore } from "@/utils/store/playback";
|
||||
|
||||
const FullscreenButton = () => {
|
||||
const { isPlayerFullscreen, toggleIsPlayerFullscreen } = usePlaybackStore(
|
||||
useShallow((state) => ({
|
||||
isPlayerFullscreen: state.playerState.isPlayerFullscreen,
|
||||
toggleIsPlayerFullscreen: state.toggleIsPlayerFullscreen,
|
||||
})),
|
||||
);
|
||||
|
||||
return (
|
||||
<IconButton onClick={toggleIsPlayerFullscreen}>
|
||||
<span className="material-symbols-rounded fill">
|
||||
{isPlayerFullscreen ? "fullscreen_exit" : "fullscreen"}
|
||||
</span>
|
||||
</IconButton>
|
||||
);
|
||||
};
|
||||
|
||||
export default FullscreenButton;
|
||||
@@ -0,0 +1,19 @@
|
||||
import { IconButton } from "@mui/material";
|
||||
import React from "react";
|
||||
import { useShallow } from "zustand/shallow";
|
||||
import { usePlaybackStore } from "@/utils/store/playback";
|
||||
|
||||
const NextChapterButton = () => {
|
||||
const { seekToNextChapter } = usePlaybackStore(
|
||||
useShallow((state) => ({
|
||||
seekToNextChapter: state.seekToNextChapter,
|
||||
})),
|
||||
);
|
||||
return (
|
||||
<IconButton onClick={() => seekToNextChapter()}>
|
||||
<span className="material-symbols-rounded fill">last_page</span>
|
||||
</IconButton>
|
||||
);
|
||||
};
|
||||
|
||||
export default NextChapterButton;
|
||||
@@ -0,0 +1,22 @@
|
||||
import { IconButton } from "@mui/material";
|
||||
import React from "react";
|
||||
import { useShallow } from "zustand/shallow";
|
||||
import { usePlaybackStore } from "@/utils/store/playback";
|
||||
|
||||
const PlayPauseButton = () => {
|
||||
const { isPlayerPlaying, toggleIsPlaying } = usePlaybackStore(
|
||||
useShallow((state) => ({
|
||||
isPlayerPlaying: state.playerState.isPlayerPlaying,
|
||||
toggleIsPlaying: state.toggleIsPlaying,
|
||||
})),
|
||||
);
|
||||
return (
|
||||
<IconButton onClick={toggleIsPlaying}>
|
||||
<span className="material-symbols-rounded fill">
|
||||
{isPlayerPlaying ? "pause" : "play_arrow"}
|
||||
</span>
|
||||
</IconButton>
|
||||
);
|
||||
};
|
||||
|
||||
export default PlayPauseButton;
|
||||
@@ -0,0 +1,20 @@
|
||||
import { IconButton } from "@mui/material";
|
||||
import React from "react";
|
||||
import { useShallow } from "zustand/shallow";
|
||||
import { usePlaybackStore } from "@/utils/store/playback";
|
||||
|
||||
const PrevChapterButton = () => {
|
||||
const { seekToPrevChapter } = usePlaybackStore(
|
||||
useShallow((state) => ({
|
||||
seekToPrevChapter: state.seekToPrevChapter,
|
||||
})),
|
||||
);
|
||||
|
||||
return (
|
||||
<IconButton onClick={seekToPrevChapter}>
|
||||
<span className="material-symbols-rounded fill">first_page</span>
|
||||
</IconButton>
|
||||
);
|
||||
};
|
||||
|
||||
export default PrevChapterButton;
|
||||
@@ -0,0 +1,23 @@
|
||||
import { IconButton } from "@mui/material";
|
||||
import React from "react";
|
||||
import { useShallow } from "zustand/shallow";
|
||||
import { usePlaybackStore } from "@/utils/store/playback";
|
||||
|
||||
const RewindButton = () => {
|
||||
const { seekBackward } = usePlaybackStore(
|
||||
useShallow((state) => ({
|
||||
seekBackward: state.seekBackward,
|
||||
})),
|
||||
);
|
||||
return (
|
||||
<IconButton
|
||||
onClick={() => {
|
||||
seekBackward(15);
|
||||
}}
|
||||
>
|
||||
<span className="material-symbols-rounded fill">fast_rewind</span>
|
||||
</IconButton>
|
||||
);
|
||||
};
|
||||
|
||||
export default RewindButton;
|
||||
@@ -0,0 +1,41 @@
|
||||
import { Button } from "@mui/material";
|
||||
import React from "react";
|
||||
import { useShallow } from "zustand/shallow";
|
||||
import { usePlaybackStore } from "@/utils/store/playback";
|
||||
|
||||
const SkipSegmentButton = () => {
|
||||
const { mediaSegments, currentSegmentIndex, skipSegment } = usePlaybackStore(
|
||||
useShallow((state) => ({
|
||||
mediaSegments: state.metadata.mediaSegments,
|
||||
currentSegmentIndex: state.nextSegmentIndex - 1,
|
||||
skipSegment: state.skipSegment,
|
||||
})),
|
||||
);
|
||||
|
||||
if (!mediaSegments?.Items?.length || currentSegmentIndex < 0) {
|
||||
return <></>; // No segments to skip
|
||||
}
|
||||
|
||||
console.log("Current Segment Index:", currentSegmentIndex);
|
||||
console.log("Media Segments:", mediaSegments);
|
||||
|
||||
return (
|
||||
<Button
|
||||
variant="outlined"
|
||||
size="large"
|
||||
//@ts-ignore
|
||||
color="white"
|
||||
style={{
|
||||
position: "absolute",
|
||||
bottom: "18vh",
|
||||
right: "2em",
|
||||
zIndex: 10000,
|
||||
}}
|
||||
onClick={skipSegment}
|
||||
>
|
||||
Skip {mediaSegments?.Items?.[currentSegmentIndex].Type}
|
||||
</Button>
|
||||
);
|
||||
};
|
||||
|
||||
export default SkipSegmentButton;
|
||||
@@ -0,0 +1,60 @@
|
||||
.video-player {
|
||||
background: black;
|
||||
position: absolute;
|
||||
width: 100vw;
|
||||
height: 100vh;
|
||||
top: 0;
|
||||
left: 0;
|
||||
&-osd {
|
||||
position: absolute;
|
||||
width: 100vw;
|
||||
height: 100vh;
|
||||
top: 0;
|
||||
left: 0;
|
||||
cursor: none;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-self: stretch;
|
||||
justify-content: space-between;
|
||||
padding: 1em 2em;
|
||||
background: linear-gradient(to top, rgb(0 0 0 / 0.7) 5vh, transparent 26vh 90vh, rgb(0 0 0 / 0.7));
|
||||
&.hovering {
|
||||
cursor: default;
|
||||
}
|
||||
&-controls {
|
||||
&-timeline-text {
|
||||
display: flex;
|
||||
align-items: stretch;
|
||||
justify-content: space-between;
|
||||
}
|
||||
&-progress {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 0.2em;
|
||||
align-items: stretch;
|
||||
justify-content: center;
|
||||
margin-bottom: 0.5em;
|
||||
&-bubble {
|
||||
width: 14em;
|
||||
top: 0;
|
||||
left: 0;
|
||||
transform-origin: bottom center;
|
||||
transform: translate(var(--left, 0), var(--top, 0));
|
||||
position: fixed;
|
||||
}
|
||||
}
|
||||
&-buttons {
|
||||
display: flex;
|
||||
gap: 0.4em;
|
||||
align-items: center;
|
||||
}
|
||||
}
|
||||
.MuiSlider-valueLabel {
|
||||
padding: 0;
|
||||
background: transparent;
|
||||
overflow: hidden;
|
||||
border-radius: $border-radius-default;
|
||||
border: 1.2px solid rgb(255 255 255 / 0.1);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,334 @@
|
||||
import { getPlaystateApi } from "@jellyfin/sdk/lib/utils/api/playstate-api";
|
||||
import { IconButton, Typography } from "@mui/material";
|
||||
import { WebviewWindow as appWindow } from "@tauri-apps/api/webviewWindow";
|
||||
import { motion } from "motion/react";
|
||||
import React, {
|
||||
type MouseEvent,
|
||||
useCallback,
|
||||
useEffect,
|
||||
useRef,
|
||||
useState,
|
||||
} from "react";
|
||||
import { useShallow } from "zustand/shallow";
|
||||
import PlayNextButton from "@/components/buttons/playNextButton";
|
||||
import PlayPreviousButton from "@/components/buttons/playPreviousButtom";
|
||||
import QueueButton from "@/components/buttons/queueButton";
|
||||
import BubbleSlider from "@/components/playback/videoPlayer/bubbleSlider";
|
||||
import { endsAt, secToTicks } from "@/utils/date/time";
|
||||
import { useApiInContext } from "@/utils/store/api";
|
||||
import { usePlaybackStore } from "@/utils/store/playback";
|
||||
import CaptionsButton from "./buttons/CaptionsButton";
|
||||
import ChaptersListButton from "./buttons/ChaptersListButton";
|
||||
import ForwardButton from "./buttons/ForwardButton";
|
||||
import FullscreenButton from "./buttons/FullscreenButton";
|
||||
import NextChapterButton from "./buttons/NextChapterButton";
|
||||
import PlayPauseButton from "./buttons/PlayPauseButton";
|
||||
import PrevChapterButton from "./buttons/PrevChapterButton";
|
||||
import RewindButton from "./buttons/RewindButton";
|
||||
import VideoPlayerSettingsMenu from "./settingsMenu";
|
||||
|
||||
import "./controls.scss";
|
||||
import { clearQueue } from "@/utils/store/queue";
|
||||
import ProgressDislay from "./ProgressDisplay";
|
||||
|
||||
/**
|
||||
* Constant for the volume change interval when using the mouse wheel.
|
||||
* This value determines how much the volume will change with each scroll step.
|
||||
*/
|
||||
const VOLUME_SCROLL_INTERVAL = 0.02;
|
||||
|
||||
type VideoPlayerControlsProps = {
|
||||
isVisible: boolean;
|
||||
onHover?: (event: MouseEvent<HTMLDivElement>) => void;
|
||||
onLeave?: (event: MouseEvent<HTMLDivElement>) => void;
|
||||
};
|
||||
|
||||
const VideoPlayerControls = ({
|
||||
isVisible,
|
||||
onHover,
|
||||
onLeave,
|
||||
}: VideoPlayerControlsProps) => {
|
||||
const playerOSDRef = useRef<HTMLDivElement>(null);
|
||||
|
||||
const api = useApiInContext((state) => state.api);
|
||||
const {
|
||||
mediaSourceId,
|
||||
itemId,
|
||||
itemDuration,
|
||||
itemName,
|
||||
episodeTitle,
|
||||
playsessionId,
|
||||
isPlayerPlaying,
|
||||
toggleIsPlaying,
|
||||
toggleIsPlayerFullscreen,
|
||||
isUserSeeking,
|
||||
// seekValue,
|
||||
isPlayerMuted,
|
||||
volume,
|
||||
toggleIsPlayerMuted,
|
||||
increaseVolumeByStep,
|
||||
decreaseVolumeByStep,
|
||||
getCurrentTime,
|
||||
} = usePlaybackStore(
|
||||
useShallow((state) => ({
|
||||
mediaSourceId: state.mediaSource.id,
|
||||
itemChapters: state.metadata.item.Chapters,
|
||||
itemId: state.metadata.item.Id,
|
||||
itemDuration: state.metadata.itemDuration,
|
||||
itemName: state.metadata.itemName,
|
||||
episodeTitle: state.metadata.episodeTitle,
|
||||
// mediaSegments: state.metadata.mediaSegments,
|
||||
isPlayerPlaying: state.playerState.isPlayerPlaying,
|
||||
playsessionId: state.playsessionId,
|
||||
toggleIsPlaying: state.toggleIsPlaying,
|
||||
toggleIsPlayerFullscreen: state.toggleIsPlayerFullscreen,
|
||||
isUserSeeking: state.playerState.isUserSeeking,
|
||||
seekValue: state.playerState.seekValue,
|
||||
isPlayerMuted: state.playerState.isPlayerMuted,
|
||||
volume: state.playerState.volume,
|
||||
toggleIsPlayerMuted: state.toggleIsPlayerMuted,
|
||||
increaseVolumeByStep: state.increaseVolumeByStep,
|
||||
decreaseVolumeByStep: state.decreaseVolumeByStep,
|
||||
getCurrentTime: state.getCurrentTime,
|
||||
})),
|
||||
);
|
||||
|
||||
// Volume control with mouse wheel
|
||||
useEffect(() => {
|
||||
const handleMouseWheel = (event: WheelEvent) => {
|
||||
if (event.deltaY < 0) {
|
||||
increaseVolumeByStep(VOLUME_SCROLL_INTERVAL);
|
||||
} else if (event.deltaY > 0) {
|
||||
decreaseVolumeByStep(VOLUME_SCROLL_INTERVAL);
|
||||
}
|
||||
};
|
||||
|
||||
const currentRef = playerOSDRef.current;
|
||||
// attach the event listener
|
||||
currentRef?.addEventListener("wheel", handleMouseWheel);
|
||||
|
||||
// remove the event listener
|
||||
return () => {
|
||||
currentRef?.removeEventListener("wheel", handleMouseWheel);
|
||||
};
|
||||
}, [increaseVolumeByStep, decreaseVolumeByStep]);
|
||||
|
||||
const [clickTimeout, setClickTimeout] = useState<NodeJS.Timeout | null>(null);
|
||||
|
||||
const [settingsMenuRef, setSettingsMenuRef] =
|
||||
useState<HTMLButtonElement | null>(null);
|
||||
|
||||
// const creditInfo = mediaSegments?.Items?.find(
|
||||
// (segment) => segment.Type === "Outro",
|
||||
// );
|
||||
|
||||
// Credits and Next Episode Card
|
||||
// const [forceShowCredits, setForceShowCredits] = useState(false);
|
||||
// const showUpNextCard = useMemo(() => {
|
||||
// if (queue?.[currentQueueItemIndex]?.Id === queue?.[queue.length - 1]?.Id) {
|
||||
// return false; // Check if the current playing episode is last episode in queue
|
||||
// }
|
||||
// if (creditInfo) {
|
||||
// if (
|
||||
// currentTime >= (creditInfo.StartTicks ?? currentTime + 1) &&
|
||||
// currentTime < (creditInfo.EndTicks ?? 0)
|
||||
// )
|
||||
// return true;
|
||||
// }
|
||||
// if (
|
||||
// Math.ceil(ticksToSec(itemDuration) - ticksToSec(currentTime)) <= 30 &&
|
||||
// Math.ceil(ticksToSec(itemDuration) - ticksToSec(currentTime)) > 0
|
||||
// ) {
|
||||
// return true;
|
||||
// }
|
||||
// return false;
|
||||
// }, [currentTime, creditInfo, itemDuration, queue, currentQueueItemIndex]);
|
||||
|
||||
const handleExitPlayer = useCallback(async () => {
|
||||
appWindow.getCurrent().setFullscreen(false);
|
||||
|
||||
history.back();
|
||||
if (!api) {
|
||||
throw Error("API is not available, cannot report playback stopped.");
|
||||
}
|
||||
// Report Jellyfin server: Playback has ended/stopped
|
||||
getPlaystateApi(api).reportPlaybackStopped({
|
||||
playbackStopInfo: {
|
||||
Failed: false,
|
||||
ItemId: itemId,
|
||||
MediaSourceId: mediaSourceId,
|
||||
PlaySessionId: playsessionId,
|
||||
PositionTicks: secToTicks(getCurrentTime() ?? 0),
|
||||
},
|
||||
});
|
||||
usePlaybackStore.setState(usePlaybackStore.getInitialState());
|
||||
clearQueue();
|
||||
}, [api, mediaSourceId, playsessionId, itemId]);
|
||||
|
||||
return (
|
||||
<motion.div
|
||||
className={
|
||||
isVisible || isUserSeeking || !isPlayerPlaying
|
||||
? "video-player-osd hovering"
|
||||
: "video-player-osd"
|
||||
}
|
||||
onMouseEnter={onHover}
|
||||
onMouseLeave={onLeave}
|
||||
ref={playerOSDRef}
|
||||
initial={{
|
||||
opacity: 0,
|
||||
}}
|
||||
animate={{
|
||||
opacity: isVisible || isUserSeeking || !isPlayerPlaying ? 1 : 0,
|
||||
}}
|
||||
style={{
|
||||
zIndex: 2,
|
||||
}}
|
||||
onClick={(event) => {
|
||||
if (event.currentTarget !== event.target) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (event.detail === 1) {
|
||||
setClickTimeout(
|
||||
setTimeout(() => {
|
||||
toggleIsPlaying();
|
||||
}, 200),
|
||||
);
|
||||
} else if (event.detail === 2 && clickTimeout) {
|
||||
clearTimeout(clickTimeout);
|
||||
toggleIsPlayerFullscreen();
|
||||
}
|
||||
}}
|
||||
>
|
||||
<div className="video-player-osd-header flex flex-justify-spaced-between flex-align-center">
|
||||
<IconButton onClick={handleExitPlayer}>
|
||||
<span className="material-symbols-rounded">arrow_back</span>
|
||||
</IconButton>
|
||||
<IconButton onClick={(e) => setSettingsMenuRef(e.currentTarget)}>
|
||||
<span className="material-symbols-rounded">settings</span>
|
||||
</IconButton>
|
||||
<VideoPlayerSettingsMenu
|
||||
settingsMenuRef={settingsMenuRef}
|
||||
handleClose={() => setSettingsMenuRef(null)}
|
||||
/>
|
||||
</div>
|
||||
<div className="video-player-osd-info">
|
||||
<div>
|
||||
<Typography variant="h4" fontWeight={500} mb={2}>
|
||||
{String(itemName)}
|
||||
</Typography>
|
||||
{episodeTitle && (
|
||||
<Typography variant="h6" fontWeight={300} mt={1}>
|
||||
{String(episodeTitle)}
|
||||
</Typography>
|
||||
)}
|
||||
</div>
|
||||
<div className="video-player-osd-controls">
|
||||
<div className="video-player-osd-controls-timeline">
|
||||
<BubbleSlider />
|
||||
<ProgressDislay />
|
||||
</div>
|
||||
<div className="flex flex-row flex-justify-spaced-between">
|
||||
<div className="video-player-osd-controls-buttons">
|
||||
<PlayPreviousButton />
|
||||
<RewindButton />
|
||||
<PrevChapterButton />
|
||||
|
||||
<PlayPauseButton />
|
||||
|
||||
<NextChapterButton />
|
||||
|
||||
<ForwardButton />
|
||||
<PlayNextButton />
|
||||
|
||||
<Typography variant="subtitle1">
|
||||
{endsAt(itemDuration)}
|
||||
</Typography>
|
||||
</div>
|
||||
<div className="video-player-osd-controls-buttons">
|
||||
{/* <motion.div
|
||||
style={{
|
||||
width: "13em",
|
||||
padding: "0.5em 1.5em",
|
||||
paddingLeft: "0.8em",
|
||||
gap: "0.4em",
|
||||
background: "black",
|
||||
borderRadius: "100px",
|
||||
display: "grid",
|
||||
justifyContent: "center",
|
||||
alignItems: "center",
|
||||
gridTemplateColumns: "2em 1fr",
|
||||
opacity: 0,
|
||||
}}
|
||||
animate={{
|
||||
opacity: showVolumeControl ? 1 : 0,
|
||||
}}
|
||||
whileHover={{
|
||||
opacity: 1,
|
||||
}}
|
||||
onMouseLeave={() => setShowVolumeControl(false)}
|
||||
>
|
||||
<Typography textAlign="center">
|
||||
{isPlayerMuted ? 0 : Math.round(playerVolume * 100)}
|
||||
</Typography>
|
||||
<Slider
|
||||
step={0.01}
|
||||
max={1}
|
||||
size="small"
|
||||
value={isPlayerMuted ? 0 : playerVolume}
|
||||
onChange={async (_, newValue) => {
|
||||
if (api && Array.isArray(newValue)) {
|
||||
dispatch({
|
||||
type: VideoPlayerActionKind.SET_PLAYER_VOLUME,
|
||||
payload: newValue[0],
|
||||
});
|
||||
} else {
|
||||
dispatch({
|
||||
type: VideoPlayerActionKind.SET_PLAYER_VOLUME,
|
||||
payload: newValue,
|
||||
});
|
||||
}
|
||||
if (newValue === 0)
|
||||
dispatch({
|
||||
type: VideoPlayerActionKind.SET_PLAYER_MUTED,
|
||||
payload: true,
|
||||
});
|
||||
else
|
||||
dispatch({
|
||||
type: VideoPlayerActionKind.SET_PLAYER_MUTED,
|
||||
payload: true,
|
||||
});
|
||||
}}
|
||||
/>
|
||||
</motion.div> */}
|
||||
{/* TODO: Volume menu */}
|
||||
<IconButton
|
||||
onClick={toggleIsPlayerMuted}
|
||||
// onMouseMoveCapture={() => {
|
||||
// setShowVolumeControl(true);
|
||||
// }}
|
||||
>
|
||||
<span className="material-symbols-rounded">
|
||||
{isPlayerMuted
|
||||
? "volume_off"
|
||||
: volume < 0.4
|
||||
? "volume_down"
|
||||
: "volume_up"}
|
||||
</span>
|
||||
</IconButton>
|
||||
<QueueButton />
|
||||
|
||||
<ChaptersListButton />
|
||||
|
||||
<CaptionsButton />
|
||||
<FullscreenButton />
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</motion.div>
|
||||
);
|
||||
};
|
||||
|
||||
export default VideoPlayerControls;
|
||||
@@ -0,0 +1,138 @@
|
||||
import { MenuItem, Popover, TextField } from "@mui/material";
|
||||
import { toNumber } from "lodash";
|
||||
import React, {
|
||||
type ChangeEventHandler,
|
||||
useCallback,
|
||||
useMemo,
|
||||
useTransition,
|
||||
} from "react";
|
||||
import { useShallow } from "zustand/shallow";
|
||||
import { useApiInContext } from "@/utils/store/api";
|
||||
import {
|
||||
changeAudioTrack,
|
||||
changeSubtitleTrack,
|
||||
usePlaybackStore,
|
||||
} from "@/utils/store/playback";
|
||||
|
||||
type VideoPlayerSettingsMenuProps = {
|
||||
settingsMenuRef: HTMLButtonElement | null;
|
||||
handleClose: () => void;
|
||||
};
|
||||
|
||||
const VideoPlayerSettingsMenu = ({
|
||||
settingsMenuRef,
|
||||
handleClose,
|
||||
}: VideoPlayerSettingsMenuProps) => {
|
||||
const settingsMenuOpen = useMemo(() => {
|
||||
return Boolean(settingsMenuRef);
|
||||
}, [settingsMenuRef]);
|
||||
|
||||
const api = useApiInContext((state) => state.api);
|
||||
|
||||
const { mediaSource, setIsBuffering } = usePlaybackStore(
|
||||
useShallow((state) => ({
|
||||
mediaSource: state.mediaSource,
|
||||
setIsBuffering: state.setIsBuffering,
|
||||
})),
|
||||
);
|
||||
|
||||
const [subtitleIsChanging, startSubtitleChange] = useTransition();
|
||||
const handleSubtitleChange: ChangeEventHandler<
|
||||
HTMLInputElement | HTMLTextAreaElement
|
||||
> = useCallback(
|
||||
(e) => {
|
||||
startSubtitleChange(() => {
|
||||
if (mediaSource.subtitle.allTracks) {
|
||||
changeSubtitleTrack(
|
||||
toNumber(e.target.value),
|
||||
mediaSource.subtitle.allTracks,
|
||||
);
|
||||
handleClose();
|
||||
}
|
||||
});
|
||||
},
|
||||
[mediaSource.subtitle?.allTracks],
|
||||
);
|
||||
|
||||
const [audioTackIsChanging, startAudioTrackChange] = useTransition();
|
||||
const handleAudioTrackChange: ChangeEventHandler<
|
||||
HTMLInputElement | HTMLTextAreaElement
|
||||
> = (e) => {
|
||||
// setPlaying(false);
|
||||
startAudioTrackChange(() => {
|
||||
if (api && mediaSource.audio.allTracks) {
|
||||
changeAudioTrack(toNumber(e.target.value), api);
|
||||
handleClose();
|
||||
setIsBuffering(true);
|
||||
}
|
||||
});
|
||||
|
||||
// setPlaying(true);
|
||||
};
|
||||
|
||||
return (
|
||||
<Popover
|
||||
anchorEl={settingsMenuRef}
|
||||
open={settingsMenuOpen}
|
||||
onClose={handleClose}
|
||||
slotProps={{
|
||||
paper: {
|
||||
style: {
|
||||
maxHeight: "20em",
|
||||
display: "flex",
|
||||
flexDirection: "column",
|
||||
gap: "1em",
|
||||
width: "24em",
|
||||
padding: "1em",
|
||||
borderRadius: "12px",
|
||||
},
|
||||
className: "glass",
|
||||
},
|
||||
}}
|
||||
anchorOrigin={{
|
||||
vertical: "bottom",
|
||||
horizontal: "right",
|
||||
}}
|
||||
transformOrigin={{
|
||||
vertical: "top",
|
||||
horizontal: "right",
|
||||
}}
|
||||
>
|
||||
<TextField
|
||||
select
|
||||
label="Audio"
|
||||
variant="outlined"
|
||||
value={mediaSource.audio?.track}
|
||||
onChange={handleAudioTrackChange}
|
||||
fullWidth
|
||||
disabled={audioTackIsChanging}
|
||||
>
|
||||
{mediaSource.audio?.allTracks?.map((sub) => (
|
||||
<MenuItem key={sub.Index} value={sub.Index}>
|
||||
{sub.DisplayTitle}
|
||||
</MenuItem>
|
||||
))}
|
||||
</TextField>
|
||||
<TextField
|
||||
select
|
||||
label="Subtitles"
|
||||
variant="outlined"
|
||||
value={mediaSource.subtitle?.track}
|
||||
onChange={handleSubtitleChange}
|
||||
fullWidth
|
||||
disabled={mediaSource.subtitle?.track === -2 || subtitleIsChanging}
|
||||
>
|
||||
<MenuItem key={-1} value={-1}>
|
||||
No Subtitle
|
||||
</MenuItem>
|
||||
{mediaSource.subtitle?.allTracks?.map((sub) => (
|
||||
<MenuItem key={sub.Index} value={sub.Index}>
|
||||
{sub.DisplayTitle}
|
||||
</MenuItem>
|
||||
))}
|
||||
</TextField>
|
||||
</Popover>
|
||||
);
|
||||
};
|
||||
|
||||
export default VideoPlayerSettingsMenu;
|
||||
Reference in new issue
Block a user