2024-06-30 04:21:34 -06:00
|
|
|
import React, {
|
|
|
|
forwardRef,
|
|
|
|
useCallback,
|
|
|
|
useEffect,
|
|
|
|
useImperativeHandle,
|
|
|
|
useRef,
|
2024-10-17 18:19:05 -06:00
|
|
|
useState,
|
2024-06-30 07:25:49 -06:00
|
|
|
type RefObject,
|
2024-06-30 04:21:34 -06:00
|
|
|
} from 'react';
|
2024-10-13 22:48:51 -06:00
|
|
|
//@ts-ignore
|
2024-10-13 02:06:12 -06:00
|
|
|
import shaka from 'shaka-player';
|
2024-10-13 15:04:44 -06:00
|
|
|
import type {VideoRef, ReactVideoProps, VideoMetadata} from './types';
|
2024-06-30 04:21:34 -06:00
|
|
|
|
2024-10-17 18:56:38 -06:00
|
|
|
// Action Queue Class
|
|
|
|
class ActionQueue {
|
2024-10-17 19:21:06 -06:00
|
|
|
private queue: { action: () => Promise<void>; name: string }[] = [];
|
2024-10-17 18:56:38 -06:00
|
|
|
private isRunning = false;
|
|
|
|
|
2024-10-17 19:21:06 -06:00
|
|
|
enqueue(action: () => Promise<void>, name: string) {
|
|
|
|
this.queue.push({ action, name });
|
2024-10-17 18:56:38 -06:00
|
|
|
this.runNext();
|
|
|
|
}
|
|
|
|
|
|
|
|
private async runNext() {
|
|
|
|
if (this.isRunning || this.queue.length === 0) {
|
2024-10-17 19:11:22 -06:00
|
|
|
console.log("Refusing to run in runNext", this.queue.length, this.isRunning);
|
2024-10-17 18:56:38 -06:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.isRunning = true;
|
2024-10-17 19:21:06 -06:00
|
|
|
const { action, name } = this.queue.shift()!;
|
|
|
|
console.log(`Running action: ${name}`);
|
|
|
|
|
|
|
|
const actionPromise = action();
|
|
|
|
const timeoutPromise = new Promise<void>((_, reject) =>
|
|
|
|
setTimeout(() => reject(new Error(`Action ${name} timed out`)), 2000)
|
|
|
|
);
|
|
|
|
|
|
|
|
try {
|
|
|
|
await Promise.race([actionPromise, timeoutPromise]);
|
|
|
|
} catch (e) {
|
|
|
|
console.error('Error in queued action:', e);
|
|
|
|
} finally {
|
|
|
|
this.isRunning = false;
|
|
|
|
this.runNext();
|
2024-10-17 18:56:38 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-17 18:28:16 -06:00
|
|
|
function shallowEqual(obj1: any, obj2: any) {
|
|
|
|
// If both are strictly equal (covers primitive types and identical object references)
|
|
|
|
if (obj1 === obj2) return true;
|
|
|
|
|
|
|
|
// If one is not an object (meaning it's a primitive), they must be strictly equal
|
|
|
|
if (typeof obj1 !== 'object' || typeof obj2 !== 'object' || obj1 === null || obj2 === null) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the keys of both objects
|
|
|
|
const keys1 = Object.keys(obj1);
|
|
|
|
const keys2 = Object.keys(obj2);
|
|
|
|
|
|
|
|
// If the number of keys is different, the objects are not equal
|
|
|
|
if (keys1.length !== keys2.length) return false;
|
|
|
|
|
|
|
|
// Check that all keys and their corresponding values are the same
|
|
|
|
return keys1.every(key => {
|
|
|
|
// If the value is an object, we fall back to reference equality (shallow comparison)
|
|
|
|
return obj1[key] === obj2[key];
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-06-30 04:21:34 -06:00
|
|
|
const Video = forwardRef<VideoRef, ReactVideoProps>(
|
|
|
|
(
|
|
|
|
{
|
|
|
|
source,
|
|
|
|
paused,
|
2024-10-13 15:04:44 -06:00
|
|
|
muted,
|
2024-06-30 04:21:34 -06:00
|
|
|
volume,
|
2024-06-30 05:38:52 -06:00
|
|
|
rate,
|
|
|
|
repeat,
|
|
|
|
controls,
|
2024-06-30 07:25:49 -06:00
|
|
|
showNotificationControls = false,
|
2024-06-30 05:38:52 -06:00
|
|
|
poster,
|
2024-07-10 04:18:21 -06:00
|
|
|
fullscreen,
|
|
|
|
fullscreenAutorotate,
|
|
|
|
fullscreenOrientation,
|
2024-06-30 04:21:34 -06:00
|
|
|
onBuffer,
|
|
|
|
onLoad,
|
|
|
|
onProgress,
|
2024-10-13 15:04:44 -06:00
|
|
|
onPlaybackRateChange,
|
2024-06-30 04:21:34 -06:00
|
|
|
onError,
|
2024-06-30 05:25:43 -06:00
|
|
|
onReadyForDisplay,
|
|
|
|
onSeek,
|
2024-12-04 12:51:28 -07:00
|
|
|
onSeekComplete,
|
2024-10-13 15:04:44 -06:00
|
|
|
onVolumeChange,
|
2024-06-30 04:21:34 -06:00
|
|
|
onEnd,
|
2024-10-13 15:04:44 -06:00
|
|
|
onPlaybackStateChanged,
|
2024-06-30 04:21:34 -06:00
|
|
|
},
|
|
|
|
ref,
|
|
|
|
) => {
|
|
|
|
const nativeRef = useRef<HTMLVideoElement>(null);
|
2024-10-12 23:48:55 -06:00
|
|
|
const shakaPlayerRef = useRef<shaka.Player | null>(null);
|
2024-10-17 18:56:38 -06:00
|
|
|
const [currentSource, setCurrentSource] = useState<object | null>(null);
|
|
|
|
const actionQueue = useRef(new ActionQueue());
|
2024-06-30 04:21:34 -06:00
|
|
|
|
2024-10-13 15:04:44 -06:00
|
|
|
const isSeeking = useRef(false);
|
2024-10-17 18:56:38 -06:00
|
|
|
|
2024-06-30 05:25:43 -06:00
|
|
|
const seek = useCallback(
|
2024-10-17 18:56:38 -06:00
|
|
|
(time: number, _tolerance?: number) => {
|
|
|
|
actionQueue.current.enqueue(async () => {
|
|
|
|
if (isNaN(time)) {
|
|
|
|
throw new Error('Specified time is not a number');
|
|
|
|
}
|
|
|
|
if (!nativeRef.current) {
|
|
|
|
console.warn('Video Component is not mounted');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
time = Math.max(0, Math.min(time, nativeRef.current.duration));
|
|
|
|
nativeRef.current.currentTime = time;
|
|
|
|
onSeek?.({
|
|
|
|
seekTime: time,
|
|
|
|
currentTime: nativeRef.current.currentTime,
|
|
|
|
});
|
2024-10-17 19:21:06 -06:00
|
|
|
}, 'seek');
|
2024-06-30 05:25:43 -06:00
|
|
|
},
|
|
|
|
[onSeek],
|
|
|
|
);
|
2024-06-30 04:21:34 -06:00
|
|
|
|
|
|
|
const pause = useCallback(() => {
|
2024-10-17 18:56:38 -06:00
|
|
|
actionQueue.current.enqueue(async () => {
|
|
|
|
if (!nativeRef.current) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
await nativeRef.current.pause();
|
2024-10-17 19:21:06 -06:00
|
|
|
}, 'pause');
|
2024-06-30 04:21:34 -06:00
|
|
|
}, []);
|
|
|
|
|
|
|
|
const resume = useCallback(() => {
|
2024-10-17 18:56:38 -06:00
|
|
|
actionQueue.current.enqueue(async () => {
|
|
|
|
if (!nativeRef.current) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
await nativeRef.current.play();
|
|
|
|
} catch (e) {
|
|
|
|
console.error('Error playing video:', e);
|
|
|
|
}
|
2024-10-17 19:21:06 -06:00
|
|
|
}, 'resume');
|
2024-06-30 04:21:34 -06:00
|
|
|
}, []);
|
|
|
|
|
2024-06-30 05:25:43 -06:00
|
|
|
const setVolume = useCallback((vol: number) => {
|
2024-10-17 18:56:38 -06:00
|
|
|
actionQueue.current.enqueue(async () => {
|
|
|
|
if (!nativeRef.current) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
nativeRef.current.volume = Math.max(0, Math.min(vol, 100)) / 100;
|
2024-10-17 19:21:06 -06:00
|
|
|
}, 'setVolume');
|
2024-06-30 05:25:43 -06:00
|
|
|
}, []);
|
|
|
|
|
|
|
|
const getCurrentPosition = useCallback(async () => {
|
|
|
|
if (!nativeRef.current) {
|
|
|
|
throw new Error('Video Component is not mounted');
|
|
|
|
}
|
|
|
|
return nativeRef.current.currentTime;
|
|
|
|
}, []);
|
|
|
|
|
2024-06-30 04:21:34 -06:00
|
|
|
const unsupported = useCallback(() => {
|
|
|
|
throw new Error('This is unsupported on the web');
|
|
|
|
}, []);
|
|
|
|
|
2024-07-10 04:18:21 -06:00
|
|
|
// Stock this in a ref to not invalidate memoization when those changes.
|
|
|
|
const fsPrefs = useRef({
|
|
|
|
fullscreenAutorotate,
|
|
|
|
fullscreenOrientation,
|
|
|
|
});
|
|
|
|
fsPrefs.current = {
|
|
|
|
fullscreenOrientation,
|
|
|
|
fullscreenAutorotate,
|
|
|
|
};
|
|
|
|
const setFullScreen = useCallback(
|
|
|
|
(
|
|
|
|
newVal: boolean,
|
|
|
|
orientation?: ReactVideoProps['fullscreenOrientation'],
|
|
|
|
autorotate?: boolean,
|
|
|
|
) => {
|
|
|
|
orientation ??= fsPrefs.current.fullscreenOrientation;
|
|
|
|
autorotate ??= fsPrefs.current.fullscreenAutorotate;
|
|
|
|
|
|
|
|
const run = async () => {
|
|
|
|
try {
|
|
|
|
if (newVal) {
|
|
|
|
await nativeRef.current?.requestFullscreen({
|
|
|
|
navigationUI: 'hide',
|
|
|
|
});
|
|
|
|
if (orientation === 'all' || !orientation || autorotate) {
|
|
|
|
screen.orientation.unlock();
|
|
|
|
} else {
|
|
|
|
await screen.orientation.lock(orientation);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (document.fullscreenElement) {
|
|
|
|
await document.exitFullscreen();
|
|
|
|
}
|
|
|
|
screen.orientation.unlock();
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
// Changing fullscreen status without a button click is not allowed so it throws.
|
|
|
|
// Some browsers also used to throw when locking screen orientation was not supported.
|
|
|
|
console.error('Could not toggle fullscreen/screen lock status', e);
|
|
|
|
}
|
|
|
|
};
|
2024-10-17 19:21:06 -06:00
|
|
|
actionQueue.current.enqueue(run, 'setFullScreen');
|
2024-07-10 04:18:21 -06:00
|
|
|
},
|
|
|
|
[],
|
|
|
|
);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
setFullScreen(
|
|
|
|
fullscreen || false,
|
|
|
|
fullscreenOrientation,
|
|
|
|
fullscreenAutorotate,
|
|
|
|
);
|
|
|
|
}, [
|
|
|
|
setFullScreen,
|
|
|
|
fullscreen,
|
|
|
|
fullscreenAutorotate,
|
|
|
|
fullscreenOrientation,
|
|
|
|
]);
|
|
|
|
|
|
|
|
const presentFullscreenPlayer = useCallback(
|
|
|
|
() => setFullScreen(true),
|
|
|
|
[setFullScreen],
|
|
|
|
);
|
|
|
|
const dismissFullscreenPlayer = useCallback(
|
|
|
|
() => setFullScreen(false),
|
|
|
|
[setFullScreen],
|
|
|
|
);
|
|
|
|
|
2024-06-30 04:21:34 -06:00
|
|
|
useImperativeHandle(
|
|
|
|
ref,
|
|
|
|
() => ({
|
|
|
|
seek,
|
|
|
|
pause,
|
|
|
|
resume,
|
2024-06-30 05:25:43 -06:00
|
|
|
setVolume,
|
|
|
|
getCurrentPosition,
|
2024-07-10 04:18:21 -06:00
|
|
|
presentFullscreenPlayer,
|
|
|
|
dismissFullscreenPlayer,
|
|
|
|
setFullScreen,
|
2024-06-30 04:21:34 -06:00
|
|
|
save: unsupported,
|
|
|
|
restoreUserInterfaceForPictureInPictureStopCompleted: unsupported,
|
2024-07-08 23:44:01 -06:00
|
|
|
nativeHtmlVideoRef: nativeRef,
|
2024-06-30 04:21:34 -06:00
|
|
|
}),
|
2024-06-30 07:25:49 -06:00
|
|
|
[
|
|
|
|
seek,
|
|
|
|
pause,
|
|
|
|
resume,
|
|
|
|
unsupported,
|
|
|
|
setVolume,
|
|
|
|
getCurrentPosition,
|
|
|
|
nativeRef,
|
2024-07-10 04:18:21 -06:00
|
|
|
presentFullscreenPlayer,
|
|
|
|
dismissFullscreenPlayer,
|
|
|
|
setFullScreen,
|
2024-06-30 07:25:49 -06:00
|
|
|
],
|
2024-06-30 04:21:34 -06:00
|
|
|
);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (paused) {
|
|
|
|
pause();
|
|
|
|
} else {
|
|
|
|
resume();
|
|
|
|
}
|
|
|
|
}, [paused, pause, resume]);
|
2024-10-17 18:56:38 -06:00
|
|
|
|
2024-06-30 04:21:34 -06:00
|
|
|
useEffect(() => {
|
2024-06-30 05:25:43 -06:00
|
|
|
if (volume === undefined) {
|
2024-06-30 04:21:34 -06:00
|
|
|
return;
|
|
|
|
}
|
2024-06-30 05:25:43 -06:00
|
|
|
setVolume(volume);
|
|
|
|
}, [volume, setVolume]);
|
2024-06-30 04:21:34 -06:00
|
|
|
|
2024-10-13 15:04:44 -06:00
|
|
|
// we use a ref to prevent triggerring the useEffect when the component rerender with a non-stable `onPlaybackStateChanged`.
|
|
|
|
const playbackStateRef = useRef(onPlaybackStateChanged);
|
|
|
|
playbackStateRef.current = onPlaybackStateChanged;
|
|
|
|
useEffect(() => {
|
|
|
|
// Not sure about how to do this but we want to wait for nativeRef to be initialized
|
|
|
|
setTimeout(() => {
|
|
|
|
if (!nativeRef.current) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set play state to the player's value (if autoplay is denied)
|
|
|
|
// This is useful if our UI is in a play state but autoplay got denied so
|
|
|
|
// the video is actaully in a paused state.
|
|
|
|
playbackStateRef.current?.({
|
|
|
|
isPlaying: !nativeRef.current.paused,
|
|
|
|
isSeeking: isSeeking.current,
|
|
|
|
});
|
|
|
|
}, 500);
|
|
|
|
}, []);
|
|
|
|
|
2024-06-30 05:38:52 -06:00
|
|
|
useEffect(() => {
|
|
|
|
if (!nativeRef.current || rate === undefined) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
nativeRef.current.playbackRate = rate;
|
|
|
|
}, [rate]);
|
|
|
|
|
2024-10-17 18:19:05 -06:00
|
|
|
const makeNewShaka = useCallback(() => {
|
2024-10-17 19:07:39 -06:00
|
|
|
console.log("makeNewShaka");
|
2024-10-17 18:56:38 -06:00
|
|
|
actionQueue.current.enqueue(async () => {
|
2024-10-17 19:07:39 -06:00
|
|
|
console.log("makeNewShaka actionQueue");
|
2024-10-17 18:56:38 -06:00
|
|
|
if (!nativeRef.current) {
|
|
|
|
console.warn('No video element to attach Shaka Player');
|
|
|
|
return;
|
|
|
|
}
|
2024-10-17 18:19:05 -06:00
|
|
|
|
2024-10-17 18:56:38 -06:00
|
|
|
// Pause the video before changing the source
|
2024-10-18 03:25:50 -06:00
|
|
|
nativeRef.current.pause();
|
2024-10-17 18:19:05 -06:00
|
|
|
|
2024-10-17 18:56:38 -06:00
|
|
|
// 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
|
2024-10-18 03:25:50 -06:00
|
|
|
shakaPlayerRef.current = new shaka.Player();
|
|
|
|
|
|
|
|
shakaPlayerRef.current.attach(nativeRef.current);
|
2024-10-17 18:56:38 -06:00
|
|
|
|
|
|
|
if (source?.cropStart) {
|
|
|
|
shakaPlayerRef.current.configure({
|
|
|
|
playRangeStart: source?.cropStart / 1000,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if (source?.cropEnd) {
|
|
|
|
shakaPlayerRef.current.configure({
|
|
|
|
playRangeEnd: source?.cropEnd / 1000,
|
|
|
|
});
|
|
|
|
}
|
2024-10-17 18:34:34 -06:00
|
|
|
|
2024-10-17 18:58:17 -06:00
|
|
|
//@ts-ignore
|
2024-10-17 18:56:38 -06:00
|
|
|
shakaPlayerRef.current.addEventListener('error', event => {
|
2024-10-17 18:58:17 -06:00
|
|
|
//@ts-ignore
|
2024-10-17 18:56:38 -06:00
|
|
|
const shakaError = event.detail;
|
|
|
|
console.error('Shaka Player Error', shakaError);
|
|
|
|
onError?.({
|
|
|
|
error: {
|
|
|
|
errorString: shakaError.message,
|
|
|
|
code: shakaError.code,
|
|
|
|
},
|
|
|
|
});
|
2024-10-12 23:48:55 -06:00
|
|
|
});
|
2024-10-17 18:34:34 -06:00
|
|
|
|
2024-10-17 18:56:38 -06:00
|
|
|
console.log('Initializing and attaching shaka');
|
2024-10-13 15:14:48 -06:00
|
|
|
|
2024-10-17 18:56:38 -06:00
|
|
|
// Load the new source
|
|
|
|
try {
|
2024-10-17 18:58:17 -06:00
|
|
|
//@ts-ignore
|
2024-10-17 18:56:38 -06:00
|
|
|
await shakaPlayerRef.current.load(source?.uri);
|
|
|
|
console.log(`${source?.uri} finished loading`);
|
|
|
|
|
|
|
|
// 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: {
|
2024-10-17 18:58:17 -06:00
|
|
|
//@ts-ignore
|
2024-10-17 18:56:38 -06:00
|
|
|
errorString: e.message,
|
2024-10-17 18:58:17 -06:00
|
|
|
//@ts-ignore
|
2024-10-17 18:56:38 -06:00
|
|
|
code: e.code,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
2024-10-17 19:21:06 -06:00
|
|
|
}, 'makeNewShaka');
|
2024-10-17 18:56:38 -06:00
|
|
|
}, [source, paused, onError]);
|
2024-10-17 18:19:05 -06:00
|
|
|
|
2024-10-17 18:56:38 -06:00
|
|
|
const nativeRefDefined = !!nativeRef.current;
|
2024-10-17 18:19:05 -06:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (!nativeRef.current) {
|
2024-10-17 18:56:38 -06:00
|
|
|
console.log('Not starting shaka yet because video element is undefined');
|
2024-10-17 18:19:05 -06:00
|
|
|
return;
|
|
|
|
}
|
2024-10-17 18:34:34 -06:00
|
|
|
if (!shallowEqual(source, currentSource)) {
|
2024-10-17 18:56:38 -06:00
|
|
|
console.log(
|
|
|
|
'Making new shaka, Old source: ',
|
|
|
|
currentSource,
|
|
|
|
'New source',
|
|
|
|
source,
|
|
|
|
);
|
2024-10-17 18:58:17 -06:00
|
|
|
//@ts-ignore
|
2024-10-17 18:34:34 -06:00
|
|
|
setCurrentSource(source);
|
2024-10-17 18:56:38 -06:00
|
|
|
makeNewShaka();
|
2024-10-17 18:19:05 -06:00
|
|
|
}
|
2024-10-17 18:56:38 -06:00
|
|
|
}, [source, nativeRefDefined, currentSource, makeNewShaka]);
|
2024-10-12 23:48:55 -06:00
|
|
|
|
2024-06-30 21:45:36 -06:00
|
|
|
useMediaSession(source?.metadata, nativeRef, showNotificationControls);
|
2024-06-30 07:25:49 -06:00
|
|
|
|
2024-10-18 03:25:50 -06:00
|
|
|
const cropStartSeconds = (source?.cropStart || 0) / 1000;
|
|
|
|
|
2024-06-30 04:21:34 -06:00
|
|
|
return (
|
2024-06-30 07:25:49 -06:00
|
|
|
<video
|
|
|
|
ref={nativeRef}
|
2024-10-13 15:04:44 -06:00
|
|
|
muted={muted}
|
|
|
|
autoPlay={!paused}
|
2024-06-30 07:25:49 -06:00
|
|
|
controls={controls}
|
|
|
|
loop={repeat}
|
|
|
|
playsInline
|
2024-10-12 22:33:11 -06:00
|
|
|
//@ts-ignore
|
2024-06-30 07:25:49 -06:00
|
|
|
poster={poster}
|
2024-10-13 15:04:44 -06:00
|
|
|
onCanPlay={() => onBuffer?.({isBuffering: false})}
|
|
|
|
onWaiting={() => onBuffer?.({isBuffering: true})}
|
|
|
|
onRateChange={() => {
|
|
|
|
if (!nativeRef.current) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
onPlaybackRateChange?.({
|
|
|
|
playbackRate: nativeRef.current?.playbackRate,
|
|
|
|
});
|
|
|
|
}}
|
|
|
|
onDurationChange={() => {
|
|
|
|
if (!nativeRef.current) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
onLoad?.({
|
|
|
|
currentTime: nativeRef.current.currentTime,
|
|
|
|
duration: nativeRef.current.duration,
|
|
|
|
videoTracks: [],
|
|
|
|
textTracks: [],
|
|
|
|
audioTracks: [],
|
|
|
|
naturalSize: {
|
|
|
|
width: nativeRef.current.videoWidth,
|
|
|
|
height: nativeRef.current.videoHeight,
|
|
|
|
orientation: 'landscape',
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}}
|
2024-06-30 07:25:49 -06:00
|
|
|
onTimeUpdate={() => {
|
|
|
|
if (!nativeRef.current) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
onProgress?.({
|
2024-10-18 03:25:50 -06:00
|
|
|
currentTime: nativeRef.current.currentTime - cropStartSeconds,
|
2024-06-30 07:25:49 -06:00
|
|
|
playableDuration: nativeRef.current.buffered.length
|
|
|
|
? nativeRef.current.buffered.end(
|
2024-10-13 15:04:44 -06:00
|
|
|
nativeRef.current.buffered.length - 1,
|
2024-06-30 07:25:49 -06:00
|
|
|
)
|
|
|
|
: 0,
|
2024-10-13 15:04:44 -06:00
|
|
|
seekableDuration: 0,
|
2024-06-30 07:25:49 -06:00
|
|
|
});
|
|
|
|
}}
|
2024-10-13 15:04:44 -06:00
|
|
|
onLoadedData={() => onReadyForDisplay?.()}
|
2024-06-30 07:25:49 -06:00
|
|
|
onError={() => {
|
|
|
|
if (!nativeRef.current?.error) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
onError?.({
|
|
|
|
error: {
|
2024-10-13 15:04:44 -06:00
|
|
|
errorString: nativeRef.current.error.message ?? 'Unknown error',
|
2024-06-30 07:25:49 -06:00
|
|
|
code: nativeRef.current.error.code,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}}
|
2024-10-13 15:04:44 -06:00
|
|
|
onLoadedMetadata={() => {
|
|
|
|
if (source?.startPosition) {
|
|
|
|
seek(source.startPosition / 1000);
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
onPlay={() =>
|
|
|
|
onPlaybackStateChanged?.({
|
|
|
|
isPlaying: true,
|
|
|
|
isSeeking: isSeeking.current,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
onPause={() =>
|
|
|
|
onPlaybackStateChanged?.({
|
|
|
|
isPlaying: false,
|
|
|
|
isSeeking: isSeeking.current,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
onSeeking={() => (isSeeking.current = true)}
|
2024-12-04 12:51:28 -07:00
|
|
|
onSeeked={() => {
|
|
|
|
(isSeeking.current = false)
|
|
|
|
|
|
|
|
onSeekComplete?.({
|
|
|
|
currentTime: (nativeRef.current?.currentTime || 0.0) - cropStartSeconds,
|
|
|
|
seekTime: 0.0,
|
|
|
|
target: 0.0,
|
|
|
|
|
|
|
|
})
|
|
|
|
}}
|
2024-10-13 15:04:44 -06:00
|
|
|
onVolumeChange={() => {
|
|
|
|
if (!nativeRef.current) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
onVolumeChange?.({volume: nativeRef.current.volume});
|
|
|
|
}}
|
|
|
|
onEnded={onEnd}
|
2024-07-08 23:43:10 -06:00
|
|
|
style={videoStyle}
|
2024-06-30 07:25:49 -06:00
|
|
|
/>
|
2024-06-30 04:21:34 -06:00
|
|
|
);
|
2024-10-13 15:04:44 -06:00
|
|
|
},
|
2024-06-30 04:21:34 -06:00
|
|
|
);
|
|
|
|
|
2024-07-08 23:43:10 -06:00
|
|
|
const videoStyle = {
|
|
|
|
position: 'absolute',
|
|
|
|
inset: 0,
|
|
|
|
objectFit: 'contain',
|
|
|
|
width: '100%',
|
|
|
|
height: '100%',
|
|
|
|
} satisfies React.CSSProperties;
|
|
|
|
|
2024-06-30 07:25:49 -06:00
|
|
|
const useMediaSession = (
|
|
|
|
metadata: VideoMetadata | undefined,
|
|
|
|
nativeRef: RefObject<HTMLVideoElement>,
|
|
|
|
showNotification: boolean,
|
|
|
|
) => {
|
|
|
|
const isPlaying = !nativeRef.current?.paused ?? false;
|
|
|
|
const progress = nativeRef.current?.currentTime ?? 0;
|
2024-06-30 21:32:39 -06:00
|
|
|
const duration = Number.isFinite(nativeRef.current?.duration)
|
|
|
|
? nativeRef.current?.duration
|
|
|
|
: undefined;
|
2024-06-30 07:25:49 -06:00
|
|
|
const playbackRate = nativeRef.current?.playbackRate ?? 1;
|
|
|
|
|
|
|
|
const enabled = 'mediaSession' in navigator && showNotification;
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (enabled) {
|
|
|
|
navigator.mediaSession.metadata = new MediaMetadata({
|
|
|
|
title: metadata?.title,
|
|
|
|
artist: metadata?.artist,
|
|
|
|
artwork: metadata?.imageUri ? [{src: metadata.imageUri}] : undefined,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}, [enabled, metadata]);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (!enabled) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-06-30 21:45:36 -06:00
|
|
|
const seekTo = (time: number) => {
|
|
|
|
if (nativeRef.current) {
|
|
|
|
nativeRef.current.currentTime = time;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2024-06-30 07:25:49 -06:00
|
|
|
const seekRelative = (offset: number) => {
|
2024-06-30 21:45:36 -06:00
|
|
|
if (nativeRef.current) {
|
|
|
|
nativeRef.current.currentTime = nativeRef.current.currentTime + offset;
|
2024-06-30 07:25:49 -06:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const mediaActions: [
|
|
|
|
MediaSessionAction,
|
|
|
|
MediaSessionActionHandler | null,
|
|
|
|
][] = [
|
2024-06-30 21:45:36 -06:00
|
|
|
['play', () => nativeRef.current?.play()],
|
|
|
|
['pause', () => nativeRef.current?.pause()],
|
2024-06-30 07:25:49 -06:00
|
|
|
[
|
|
|
|
'seekbackward',
|
|
|
|
(evt: MediaSessionActionDetails) =>
|
|
|
|
seekRelative(evt.seekOffset ? -evt.seekOffset : -10),
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'seekforward',
|
|
|
|
(evt: MediaSessionActionDetails) =>
|
|
|
|
seekRelative(evt.seekOffset ? evt.seekOffset : 10),
|
|
|
|
],
|
2024-06-30 21:45:36 -06:00
|
|
|
['seekto', (evt: MediaSessionActionDetails) => seekTo(evt.seekTime!)],
|
2024-06-30 07:25:49 -06:00
|
|
|
];
|
|
|
|
|
|
|
|
for (const [action, handler] of mediaActions) {
|
|
|
|
try {
|
|
|
|
navigator.mediaSession.setActionHandler(action, handler);
|
|
|
|
} catch {
|
|
|
|
// ignored
|
|
|
|
}
|
|
|
|
}
|
2024-06-30 21:45:36 -06:00
|
|
|
}, [enabled, nativeRef]);
|
2024-06-30 07:25:49 -06:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (enabled) {
|
|
|
|
navigator.mediaSession.playbackState = isPlaying ? 'playing' : 'paused';
|
|
|
|
}
|
|
|
|
}, [isPlaying, enabled]);
|
|
|
|
useEffect(() => {
|
|
|
|
if (enabled && duration !== undefined) {
|
|
|
|
navigator.mediaSession.setPositionState({
|
|
|
|
position: Math.min(progress, duration),
|
|
|
|
duration,
|
|
|
|
playbackRate: playbackRate,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}, [progress, duration, playbackRate, enabled]);
|
|
|
|
};
|
|
|
|
|
2024-06-30 04:21:34 -06:00
|
|
|
Video.displayName = 'Video';
|
|
|
|
export default Video;
|