This commit is contained in:
Josh Hawkins 2024-08-15 05:53:45 -05:00
parent 8f44cc3725
commit 0f53cbcc1b

View File

@ -32,7 +32,7 @@ function MSEPlayer({
onError, onError,
}: MSEPlayerProps) { }: MSEPlayerProps) {
const RECONNECT_TIMEOUT: number = 10000; const RECONNECT_TIMEOUT: number = 10000;
const BUFFERING_COOLDOWN_TIMEOUT: number = 5000; // const BUFFERING_COOLDOWN_TIMEOUT: number = 5000;
const CODECS: string[] = [ const CODECS: string[] = [
"avc1.640029", // H.264 high 4.1 (Chromecast 1st and 2nd Gen) "avc1.640029", // H.264 high 4.1 (Chromecast 1st and 2nd Gen)
@ -49,9 +49,9 @@ function MSEPlayer({
const [isPlaying, setIsPlaying] = useState(false); const [isPlaying, setIsPlaying] = useState(false);
const lastJumpTimeRef = useRef(0); const lastJumpTimeRef = useRef(0);
const MAX_BUFFER_ENTRIES = 10; // Size of the rolling window // const MAX_BUFFER_ENTRIES = 10; // Size of the rolling window
const bufferTimes = useRef<number[]>([]); // const bufferTimes = useRef<number[]>([]);
const bufferIndex = useRef(0); // const bufferIndex = useRef(0);
const [wsState, setWsState] = useState<number>(WebSocket.CLOSED); const [wsState, setWsState] = useState<number>(WebSocket.CLOSED);
const [connectTS, setConnectTS] = useState<number>(0); const [connectTS, setConnectTS] = useState<number>(0);
@ -315,12 +315,12 @@ function MSEPlayer({
return video.buffered.end(video.buffered.length - 1) - video.currentTime; return video.buffered.end(video.buffered.length - 1) - video.currentTime;
}; };
const calculateAdaptiveBufferThreshold = () => { // const calculateAdaptiveBufferThreshold = () => {
const filledEntries = bufferTimes.current.length; // const filledEntries = bufferTimes.current.length;
const sum = bufferTimes.current.reduce((a, b) => a + b, 0); // const sum = bufferTimes.current.reduce((a, b) => a + b, 0);
const averageBufferTime = filledEntries ? sum / filledEntries : 0; // const averageBufferTime = filledEntries ? sum / filledEntries : 0;
return averageBufferTime * 1.5; // return averageBufferTime * 1.5;
}; // };
useEffect(() => { useEffect(() => {
if (!playbackEnabled) { if (!playbackEnabled) {
@ -413,17 +413,17 @@ function MSEPlayer({
onProgress={() => { onProgress={() => {
const bufferTime = getBufferedTime(videoRef.current); const bufferTime = getBufferedTime(videoRef.current);
if (videoRef.current && videoRef.current.playbackRate === 1) { // if (videoRef.current && videoRef.current.playbackRate === 1) {
if (bufferTimes.current.length < MAX_BUFFER_ENTRIES) { // if (bufferTimes.current.length < MAX_BUFFER_ENTRIES) {
bufferTimes.current.push(bufferTime); // bufferTimes.current.push(bufferTime);
} else { // } else {
bufferTimes.current[bufferIndex.current] = bufferTime; // bufferTimes.current[bufferIndex.current] = bufferTime;
bufferIndex.current = // bufferIndex.current =
(bufferIndex.current + 1) % MAX_BUFFER_ENTRIES; // (bufferIndex.current + 1) % MAX_BUFFER_ENTRIES;
} // }
} // }
const bufferThreshold = calculateAdaptiveBufferThreshold(); // const bufferThreshold = calculateAdaptiveBufferThreshold();
// if we have > 3 seconds of buffered data and we're still not playing, // if we have > 3 seconds of buffered data and we're still not playing,
// something might be wrong - maybe codec issue, no audio, etc // something might be wrong - maybe codec issue, no audio, etc
@ -437,31 +437,31 @@ function MSEPlayer({
// if we're above our rolling average threshold or have > 3 seconds of // if we're above our rolling average threshold or have > 3 seconds of
// buffered data and we're playing, we may have drifted from actual live // buffered data and we're playing, we may have drifted from actual live
// time, so increase playback rate to compensate // time, so increase playback rate to compensate
if ( // if (
videoRef.current && // videoRef.current &&
isPlaying && // isPlaying &&
playbackEnabled && // playbackEnabled &&
(bufferTime > bufferThreshold || bufferTime > 3) && // (bufferTime > bufferThreshold || bufferTime > 3) &&
Date.now() - lastJumpTimeRef.current > BUFFERING_COOLDOWN_TIMEOUT // Date.now() - lastJumpTimeRef.current > BUFFERING_COOLDOWN_TIMEOUT
) { // ) {
videoRef.current.playbackRate = 1.1; // videoRef.current.playbackRate = 1.1;
console.log( // console.log(
camera, // camera,
"increasing playback rate", // "increasing playback rate",
bufferTime, // bufferTime,
bufferThreshold, // bufferThreshold,
); // );
} else { // } else {
if (videoRef.current) { // if (videoRef.current) {
console.log( // console.log(
camera, // camera,
"normal playback rate", // "normal playback rate",
bufferTime, // bufferTime,
bufferThreshold, // bufferThreshold,
); // );
videoRef.current.playbackRate = 1; // videoRef.current.playbackRate = 1;
} // }
} // }
if (onError != undefined) { if (onError != undefined) {
if (videoRef.current?.paused) { if (videoRef.current?.paused) {