Use an async queue
This commit is contained in:
parent
9138c3249d
commit
e5f182cda9
@ -11,6 +11,35 @@ import React, {
|
|||||||
import shaka from 'shaka-player';
|
import shaka from 'shaka-player';
|
||||||
import type {VideoRef, ReactVideoProps, VideoMetadata} from './types';
|
import type {VideoRef, ReactVideoProps, VideoMetadata} from './types';
|
||||||
|
|
||||||
|
// Action Queue Class
|
||||||
|
class ActionQueue {
|
||||||
|
private queue: (() => Promise<void>)[] = [];
|
||||||
|
private isRunning = false;
|
||||||
|
|
||||||
|
enqueue(action: () => Promise<void>) {
|
||||||
|
this.queue.push(action);
|
||||||
|
this.runNext();
|
||||||
|
}
|
||||||
|
|
||||||
|
private async runNext() {
|
||||||
|
if (this.isRunning || this.queue.length === 0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
this.isRunning = true;
|
||||||
|
const action = this.queue.shift();
|
||||||
|
if (action) {
|
||||||
|
try {
|
||||||
|
await action();
|
||||||
|
} catch (e) {
|
||||||
|
console.error('Error in queued action:', e);
|
||||||
|
} finally {
|
||||||
|
this.isRunning = false;
|
||||||
|
this.runNext();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function shallowEqual(obj1: any, obj2: any) {
|
function shallowEqual(obj1: any, obj2: any) {
|
||||||
// If both are strictly equal (covers primitive types and identical object references)
|
// If both are strictly equal (covers primitive types and identical object references)
|
||||||
if (obj1 === obj2) return true;
|
if (obj1 === obj2) return true;
|
||||||
@ -64,11 +93,14 @@ const Video = forwardRef<VideoRef, ReactVideoProps>(
|
|||||||
) => {
|
) => {
|
||||||
const nativeRef = useRef<HTMLVideoElement>(null);
|
const nativeRef = useRef<HTMLVideoElement>(null);
|
||||||
const shakaPlayerRef = useRef<shaka.Player | null>(null);
|
const shakaPlayerRef = useRef<shaka.Player | null>(null);
|
||||||
const [ currentSource, setCurrentSource ] = useState<object | null>(null);
|
const [currentSource, setCurrentSource] = useState<object | null>(null);
|
||||||
|
const actionQueue = useRef(new ActionQueue());
|
||||||
|
|
||||||
const isSeeking = useRef(false);
|
const isSeeking = useRef(false);
|
||||||
|
|
||||||
const seek = useCallback(
|
const seek = useCallback(
|
||||||
async (time: number, _tolerance?: number) => {
|
(time: number, _tolerance?: number) => {
|
||||||
|
actionQueue.current.enqueue(async () => {
|
||||||
if (isNaN(time)) {
|
if (isNaN(time)) {
|
||||||
throw new Error('Specified time is not a number');
|
throw new Error('Specified time is not a number');
|
||||||
}
|
}
|
||||||
@ -78,30 +110,44 @@ const Video = forwardRef<VideoRef, ReactVideoProps>(
|
|||||||
}
|
}
|
||||||
time = Math.max(0, Math.min(time, nativeRef.current.duration));
|
time = Math.max(0, Math.min(time, nativeRef.current.duration));
|
||||||
nativeRef.current.currentTime = time;
|
nativeRef.current.currentTime = time;
|
||||||
onSeek?.({seekTime: time, currentTime: nativeRef.current.currentTime});
|
onSeek?.({
|
||||||
|
seekTime: time,
|
||||||
|
currentTime: nativeRef.current.currentTime,
|
||||||
|
});
|
||||||
|
});
|
||||||
},
|
},
|
||||||
[onSeek],
|
[onSeek],
|
||||||
);
|
);
|
||||||
|
|
||||||
const pause = useCallback(() => {
|
const pause = useCallback(() => {
|
||||||
|
actionQueue.current.enqueue(async () => {
|
||||||
if (!nativeRef.current) {
|
if (!nativeRef.current) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
nativeRef.current.pause();
|
await nativeRef.current.pause();
|
||||||
|
});
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
const resume = useCallback(() => {
|
const resume = useCallback(() => {
|
||||||
|
actionQueue.current.enqueue(async () => {
|
||||||
if (!nativeRef.current) {
|
if (!nativeRef.current) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
nativeRef.current.play();
|
try {
|
||||||
|
await nativeRef.current.play();
|
||||||
|
} catch (e) {
|
||||||
|
console.error('Error playing video:', e);
|
||||||
|
}
|
||||||
|
});
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
const setVolume = useCallback((vol: number) => {
|
const setVolume = useCallback((vol: number) => {
|
||||||
|
actionQueue.current.enqueue(async () => {
|
||||||
if (!nativeRef.current) {
|
if (!nativeRef.current) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
nativeRef.current.volume = Math.max(0, Math.min(vol, 100)) / 100;
|
nativeRef.current.volume = Math.max(0, Math.min(vol, 100)) / 100;
|
||||||
|
});
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
const getCurrentPosition = useCallback(async () => {
|
const getCurrentPosition = useCallback(async () => {
|
||||||
@ -219,6 +265,7 @@ const Video = forwardRef<VideoRef, ReactVideoProps>(
|
|||||||
resume();
|
resume();
|
||||||
}
|
}
|
||||||
}, [paused, pause, resume]);
|
}, [paused, pause, resume]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (volume === undefined) {
|
if (volume === undefined) {
|
||||||
return;
|
return;
|
||||||
@ -253,24 +300,37 @@ const Video = forwardRef<VideoRef, ReactVideoProps>(
|
|||||||
nativeRef.current.playbackRate = rate;
|
nativeRef.current.playbackRate = rate;
|
||||||
}, [rate]);
|
}, [rate]);
|
||||||
|
|
||||||
|
|
||||||
const makeNewShaka = useCallback(() => {
|
const makeNewShaka = useCallback(() => {
|
||||||
if (shakaPlayerRef.current) {
|
actionQueue.current.enqueue(async () => {
|
||||||
shakaPlayerRef.current.unload()
|
if (!nativeRef.current) {
|
||||||
|
console.warn('No video element to attach Shaka Player');
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
shakaPlayerRef.current = new shaka.Player();
|
|
||||||
|
|
||||||
|
// Pause the video before changing the source
|
||||||
|
await nativeRef.current.pause();
|
||||||
|
|
||||||
|
// Unload the previous Shaka player if it exists
|
||||||
|
if (shakaPlayerRef.current) {
|
||||||
|
await shakaPlayerRef.current.unload();
|
||||||
|
shakaPlayerRef.current = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create a new Shaka player and attach it to the video element
|
||||||
|
shakaPlayerRef.current = new shaka.Player(nativeRef.current);
|
||||||
|
|
||||||
if (source?.cropStart) {
|
if (source?.cropStart) {
|
||||||
shakaPlayerRef.current.configure({playRangeStart: source?.cropStart / 1000})
|
shakaPlayerRef.current.configure({
|
||||||
|
playRangeStart: source?.cropStart / 1000,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
if (source?.cropEnd) {
|
if (source?.cropEnd) {
|
||||||
shakaPlayerRef.current.configure({playRangeEnd: source?.cropEnd / 1000})
|
shakaPlayerRef.current.configure({
|
||||||
|
playRangeEnd: source?.cropEnd / 1000,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
//@ts-ignore
|
shakaPlayerRef.current.addEventListener('error', event => {
|
||||||
shakaPlayerRef.current.addEventListener("error", (event) => {
|
|
||||||
//@ts-ignore
|
|
||||||
const shakaError = event.detail;
|
const shakaError = event.detail;
|
||||||
console.error('Shaka Player Error', shakaError);
|
console.error('Shaka Player Error', shakaError);
|
||||||
onError?.({
|
onError?.({
|
||||||
@ -281,31 +341,51 @@ const Video = forwardRef<VideoRef, ReactVideoProps>(
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
console.log("Initializing and attaching shaka")
|
console.log('Initializing and attaching shaka');
|
||||||
//@ts-ignore
|
|
||||||
shakaPlayerRef.current.attach(nativeRef.current);
|
|
||||||
|
|
||||||
//@ts-ignore
|
// Load the new source
|
||||||
shakaPlayerRef.current.load(source?.uri).then(
|
try {
|
||||||
() => console.log(`${source?.uri} finished loading`)
|
await shakaPlayerRef.current.load(source?.uri);
|
||||||
);
|
console.log(`${source?.uri} finished loading`);
|
||||||
console.log("Started shaka loading");
|
|
||||||
}, [source, setCurrentSource]);
|
|
||||||
|
|
||||||
const nativeRefDefined = nativeRef.current ? true : false;
|
// Optionally resume playback if not paused
|
||||||
|
if (!paused) {
|
||||||
|
try {
|
||||||
|
await nativeRef.current.play();
|
||||||
|
} catch (e) {
|
||||||
|
console.error('Error playing video:', e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
console.error('Error loading video with Shaka Player', e);
|
||||||
|
onError?.({
|
||||||
|
error: {
|
||||||
|
errorString: e.message,
|
||||||
|
code: e.code,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}, [source, paused, onError]);
|
||||||
|
|
||||||
|
const nativeRefDefined = !!nativeRef.current;
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!nativeRef.current) {
|
if (!nativeRef.current) {
|
||||||
console.log("Not starting shaka yet bc undefined")
|
console.log('Not starting shaka yet because video element is undefined');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (!shallowEqual(source, currentSource)) {
|
if (!shallowEqual(source, currentSource)) {
|
||||||
console.log("Making new shaka, Old source: ", currentSource, "New source", source);
|
console.log(
|
||||||
//@ts-ignore
|
'Making new shaka, Old source: ',
|
||||||
|
currentSource,
|
||||||
|
'New source',
|
||||||
|
source,
|
||||||
|
);
|
||||||
setCurrentSource(source);
|
setCurrentSource(source);
|
||||||
makeNewShaka()
|
makeNewShaka();
|
||||||
}
|
}
|
||||||
}, [source, nativeRefDefined, currentSource])
|
}, [source, nativeRefDefined, currentSource, makeNewShaka]);
|
||||||
|
|
||||||
useMediaSession(source?.metadata, nativeRef, showNotificationControls);
|
useMediaSession(source?.metadata, nativeRef, showNotificationControls);
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user