|
|
@ -1,9 +1,9 @@
|
|
|
|
"use client";
|
|
|
|
"use client";
|
|
|
|
|
|
|
|
|
|
|
|
import screenfull from "screenfull";
|
|
|
|
import screenfull from "screenfull";
|
|
|
|
import { useDebounce } from "use-debounce";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
|
|
|
|
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
|
|
|
|
|
|
|
|
import { useHotkeys } from "react-hotkeys-hook";
|
|
|
|
import {
|
|
|
|
import {
|
|
|
|
FiMaximize as MaximizeIcon,
|
|
|
|
FiMaximize as MaximizeIcon,
|
|
|
|
FiMinimize as MinimizeIcon,
|
|
|
|
FiMinimize as MinimizeIcon,
|
|
|
@ -37,19 +37,8 @@ export const Player: Component<{ streams: Stream[] }> = ({ streams }) => {
|
|
|
|
|
|
|
|
|
|
|
|
const videoPlayerId = "video-player";
|
|
|
|
const videoPlayerId = "video-player";
|
|
|
|
|
|
|
|
|
|
|
|
const [progress, setProgress] = useState(0);
|
|
|
|
// TODO: Make framerate based on video, not a set number
|
|
|
|
const [loaded, setLoaded] = useState(0);
|
|
|
|
const framerate = 60;
|
|
|
|
const [duration, setDuration] = useState(0);
|
|
|
|
|
|
|
|
const [maximized, setMaximized] = useState(false);
|
|
|
|
|
|
|
|
const [volume, setVolume] = useState(40);
|
|
|
|
|
|
|
|
const [playbackRate, setPlaybackRate] = useState(1.0);
|
|
|
|
|
|
|
|
const [playing, setPlaying] = useState(false);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const [userSetProgress, setUserSetProgress] = useState(0);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
|
|
|
playerRef.current?.seekTo(userSetProgress);
|
|
|
|
|
|
|
|
}, [userSetProgress]);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const volumeIcons = useMemo(
|
|
|
|
const volumeIcons = useMemo(
|
|
|
|
() => [
|
|
|
|
() => [
|
|
|
@ -60,15 +49,111 @@ export const Player: Component<{ streams: Stream[] }> = ({ streams }) => {
|
|
|
|
[]
|
|
|
|
[]
|
|
|
|
);
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
const playbackRateCategories = useMemo(
|
|
|
|
const [playbackRateMenuItems, playbackRateCategories] = useMemo(() => {
|
|
|
|
() =>
|
|
|
|
const categories = [0.25, 0.5, 1, 1.25, 1.5, 2];
|
|
|
|
[0.25, 0.5, 1, 1.25, 1.5, 2].map((speed) => ({
|
|
|
|
|
|
|
|
|
|
|
|
return [
|
|
|
|
|
|
|
|
categories.map((speed) => ({
|
|
|
|
key: speed,
|
|
|
|
key: speed,
|
|
|
|
label: speed.toString()
|
|
|
|
label: speed.toString()
|
|
|
|
})),
|
|
|
|
})),
|
|
|
|
[]
|
|
|
|
categories
|
|
|
|
|
|
|
|
];
|
|
|
|
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const [progress, setProgress] = useState(0);
|
|
|
|
|
|
|
|
const [loaded, setLoaded] = useState(0);
|
|
|
|
|
|
|
|
const [duration, setDuration] = useState(0);
|
|
|
|
|
|
|
|
const [maximized, setMaximized] = useState(false);
|
|
|
|
|
|
|
|
const [volume, setVolume] = useState(40);
|
|
|
|
|
|
|
|
const [muted, setMuted] = useState(false);
|
|
|
|
|
|
|
|
const [playbackRate, setPlaybackRate] = useState(1.0);
|
|
|
|
|
|
|
|
const [playing, setPlaying] = useState(false);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const [userSetProgress, setUserSetProgress] = useState(0);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const seekForward = useCallback(
|
|
|
|
|
|
|
|
(seconds: number) => {
|
|
|
|
|
|
|
|
if (duration >= seconds) {
|
|
|
|
|
|
|
|
const newProgress = progress + seconds / duration;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
setUserSetProgress(newProgress);
|
|
|
|
|
|
|
|
setProgress(newProgress);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
[progress, duration]
|
|
|
|
);
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const increaseVolume = useCallback((amount: number) => {
|
|
|
|
|
|
|
|
setVolume((state) => {
|
|
|
|
|
|
|
|
const newVolume = state + amount;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (newVolume >= 0 && newVolume <= 100) return newVolume;
|
|
|
|
|
|
|
|
else return state;
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const increasePlaybackRate = useCallback(
|
|
|
|
|
|
|
|
(amount: number) => {
|
|
|
|
|
|
|
|
const indexOfCurrentRate = playbackRateCategories.indexOf(playbackRate);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (indexOfCurrentRate < 0) return;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const newRateIndex = indexOfCurrentRate + amount;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (newRateIndex < 0 || newRateIndex > playbackRateCategories.length - 1)
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
setPlaybackRate(playbackRateCategories[newRateIndex]);
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
[playbackRate, playbackRateCategories]
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
useHotkeys(["k", "space"], () => setPlaying((state) => !state), {
|
|
|
|
|
|
|
|
preventDefault: true
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
useHotkeys(["f"], () => setMaximized((state) => !state));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
useHotkeys(["m"], () => setMuted((state) => !state));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
useHotkeys(["arrowup"], () => increaseVolume(5), { preventDefault: true });
|
|
|
|
|
|
|
|
useHotkeys(["arrowdown"], () => increaseVolume(-5), { preventDefault: true });
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
useHotkeys(["arrowright"], () => seekForward(5));
|
|
|
|
|
|
|
|
useHotkeys(["arrowleft"], () => seekForward(-5));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
useHotkeys(["shift+."], () => increasePlaybackRate(1));
|
|
|
|
|
|
|
|
useHotkeys(["shift+,"], () => increasePlaybackRate(-1));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
useHotkeys(["l"], () => seekForward(10));
|
|
|
|
|
|
|
|
useHotkeys(["j"], () => seekForward(-10));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
useHotkeys(
|
|
|
|
|
|
|
|
["."],
|
|
|
|
|
|
|
|
() => {
|
|
|
|
|
|
|
|
if (!playing) seekForward(1 / framerate);
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
[seekForward, playing, framerate]
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
useHotkeys(
|
|
|
|
|
|
|
|
[","],
|
|
|
|
|
|
|
|
() => {
|
|
|
|
|
|
|
|
if (!playing) seekForward(-(1 / framerate));
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
[seekForward, playing, framerate]
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Mute if volume is 0
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
|
|
|
if (volume === 0) setMuted(true);
|
|
|
|
|
|
|
|
else setMuted(false);
|
|
|
|
|
|
|
|
}, [volume]);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
|
|
|
playerRef.current?.seekTo(userSetProgress);
|
|
|
|
|
|
|
|
}, [userSetProgress]);
|
|
|
|
|
|
|
|
|
|
|
|
const updateMaximized = useCallback(() => {
|
|
|
|
const updateMaximized = useCallback(() => {
|
|
|
|
setMaximized(screenfull.isFullscreen);
|
|
|
|
setMaximized(screenfull.isFullscreen);
|
|
|
|
}, [setMaximized]);
|
|
|
|
}, [setMaximized]);
|
|
|
@ -146,7 +231,7 @@ export const Player: Component<{ streams: Stream[] }> = ({ streams }) => {
|
|
|
|
<Dropdown>
|
|
|
|
<Dropdown>
|
|
|
|
<DropdownTrigger>
|
|
|
|
<DropdownTrigger>
|
|
|
|
<Button variant="light" isIconOnly className="text-xl">
|
|
|
|
<Button variant="light" isIconOnly className="text-xl">
|
|
|
|
{volume === 0 ? (
|
|
|
|
{muted ? (
|
|
|
|
<MutedIcon />
|
|
|
|
<MutedIcon />
|
|
|
|
) : (
|
|
|
|
) : (
|
|
|
|
volumeIcons[
|
|
|
|
volumeIcons[
|
|
|
@ -158,6 +243,7 @@ export const Player: Component<{ streams: Stream[] }> = ({ streams }) => {
|
|
|
|
<DropdownMenu aria-label="Volume menu">
|
|
|
|
<DropdownMenu aria-label="Volume menu">
|
|
|
|
<DropdownItem>
|
|
|
|
<DropdownItem>
|
|
|
|
<Slider
|
|
|
|
<Slider
|
|
|
|
|
|
|
|
aria-label="Volume slider"
|
|
|
|
className="h-48"
|
|
|
|
className="h-48"
|
|
|
|
value={volume}
|
|
|
|
value={volume}
|
|
|
|
onChange={(value) => {
|
|
|
|
onChange={(value) => {
|
|
|
@ -190,7 +276,7 @@ export const Player: Component<{ streams: Stream[] }> = ({ streams }) => {
|
|
|
|
onAction={(key) => {
|
|
|
|
onAction={(key) => {
|
|
|
|
setPlaybackRate(parseFloat(key as string));
|
|
|
|
setPlaybackRate(parseFloat(key as string));
|
|
|
|
}}
|
|
|
|
}}
|
|
|
|
items={playbackRateCategories}
|
|
|
|
items={playbackRateMenuItems}
|
|
|
|
>
|
|
|
|
>
|
|
|
|
{(item) => (
|
|
|
|
{(item) => (
|
|
|
|
<DropdownItem key={item.key}>
|
|
|
|
<DropdownItem key={item.key}>
|
|
|
@ -217,7 +303,7 @@ export const Player: Component<{ streams: Stream[] }> = ({ streams }) => {
|
|
|
|
<ReactPlayer
|
|
|
|
<ReactPlayer
|
|
|
|
playing={playing}
|
|
|
|
playing={playing}
|
|
|
|
volume={volume / 100}
|
|
|
|
volume={volume / 100}
|
|
|
|
muted={volume === 0}
|
|
|
|
muted={muted}
|
|
|
|
playbackRate={playbackRate}
|
|
|
|
playbackRate={playbackRate}
|
|
|
|
ref={playerRef}
|
|
|
|
ref={playerRef}
|
|
|
|
className="absolute top-0 left-0"
|
|
|
|
className="absolute top-0 left-0"
|
|
|
|