Compare commits
4 Commits
loewy/asyn
...
ivan/repla
| Author | SHA1 | Date | |
|---|---|---|---|
| b01e55488e | |||
| d7563cfd93 | |||
| 69a9159a43 | |||
| c6bd3e3ee4 |
@@ -34,6 +34,22 @@ enum RCTVideoAssetsUtils {
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static func getDuration(asset: AVAsset) async -> CMTime? {
|
||||||
|
if #available(iOS 15, tvOS 15, visionOS 1.0, *) {
|
||||||
|
return try? await asset.load(.duration)
|
||||||
|
} else {
|
||||||
|
#if !os(visionOS)
|
||||||
|
return await withCheckedContinuation { continuation in
|
||||||
|
asset.loadValuesAsynchronously(forKeys: ["duration"]) {
|
||||||
|
var error: NSError?
|
||||||
|
let status = asset.statusOfValue(forKey: "duration", error: &error)
|
||||||
|
continuation.resume(returning: status == .loaded ? asset.duration : nil)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// MARK: - RCTVideoUtils
|
// MARK: - RCTVideoUtils
|
||||||
|
|||||||
@@ -1420,7 +1420,10 @@ class RCTVideo: UIView, RCTVideoPlayerViewControllerDelegate, RCTPlayerObserverH
|
|||||||
}
|
}
|
||||||
|
|
||||||
if onVideoLoad != nil, self._videoLoadStarted {
|
if onVideoLoad != nil, self._videoLoadStarted {
|
||||||
var duration = Float(CMTimeGetSeconds(_playerItem.asset.duration))
|
let assetDuration = await RCTVideoAssetsUtils.getDuration(asset: _playerItem.asset)
|
||||||
|
guard self._playerItem === _playerItem,
|
||||||
|
self._source?.json === source.json else { return }
|
||||||
|
var duration = Float(CMTimeGetSeconds(assetDuration ?? .invalid))
|
||||||
|
|
||||||
if duration.isNaN || duration == 0 {
|
if duration.isNaN || duration == 0 {
|
||||||
// This is a safety check for live video.
|
// This is a safety check for live video.
|
||||||
@@ -1449,6 +1452,8 @@ class RCTVideo: UIView, RCTVideoPlayerViewControllerDelegate, RCTPlayerObserverH
|
|||||||
|
|
||||||
let audioTracks = await RCTVideoUtils.getAudioTrackInfo(self._player)
|
let audioTracks = await RCTVideoUtils.getAudioTrackInfo(self._player)
|
||||||
let textTracks = await RCTVideoUtils.getTextTrackInfo(self._player)
|
let textTracks = await RCTVideoUtils.getTextTrackInfo(self._player)
|
||||||
|
guard self._playerItem === _playerItem,
|
||||||
|
self._source?.json === source.json else { return }
|
||||||
self.onVideoLoad?(["duration": NSNumber(value: duration),
|
self.onVideoLoad?(["duration": NSNumber(value: duration),
|
||||||
"currentTime": NSNumber(value: Float(CMTimeGetSeconds(_playerItem.currentTime()))),
|
"currentTime": NSNumber(value: Float(CMTimeGetSeconds(_playerItem.currentTime()))),
|
||||||
"canPlayReverse": NSNumber(value: _playerItem.canPlayReverse),
|
"canPlayReverse": NSNumber(value: _playerItem.canPlayReverse),
|
||||||
@@ -1699,4 +1704,3 @@ class RCTVideo: UIView, RCTVideoPlayerViewControllerDelegate, RCTPlayerObserverH
|
|||||||
@objc
|
@objc
|
||||||
func setOnClick(_: Any) {}
|
func setOnClick(_: Any) {}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -4,7 +4,13 @@ import React
|
|||||||
@objc(RCTVideoManager)
|
@objc(RCTVideoManager)
|
||||||
class RCTVideoManager: RCTViewManager {
|
class RCTVideoManager: RCTViewManager {
|
||||||
override func view() -> UIView {
|
override func view() -> UIView {
|
||||||
return RCTVideo(eventDispatcher: (RCTBridge.current().eventDispatcher() as! RCTEventDispatcher))
|
// `RCTBridge.current()` returns nil under bridgeless (New Architecture), and it
|
||||||
|
// is nonnull-annotated, so Swift emits an unconditional nil-check trap around
|
||||||
|
// the call -- optional chaining does not avoid it. Every <Video> mount killed
|
||||||
|
// the app. RCTVideo only stores this dispatcher and never reads it (events go
|
||||||
|
// out through the RCT_EXPORT_VIEW_PROPERTY direct event blocks), so skip the
|
||||||
|
// lookup entirely.
|
||||||
|
return RCTVideo(eventDispatcher: nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
func methodQueue() -> DispatchQueue {
|
func methodQueue() -> DispatchQueue {
|
||||||
|
|||||||
@@ -32,17 +32,17 @@
|
|||||||
"react-native": "0.73.2",
|
"react-native": "0.73.2",
|
||||||
"react-native-windows": "^0.61.0-0",
|
"react-native-windows": "^0.61.0-0",
|
||||||
"release-it": "^16.2.1",
|
"release-it": "^16.2.1",
|
||||||
"typescript": "5.1.6",
|
"typescript": "5.1.6"
|
||||||
"patch-package": "^8.0.0"
|
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"shaka-player": "^4.11.7"
|
"hls.js": "^1.6.16"
|
||||||
},
|
},
|
||||||
"peerDependencies": {
|
"peerDependencies": {
|
||||||
"react": "*",
|
"react": "*",
|
||||||
"react-native": "*"
|
"react-native": "*"
|
||||||
},
|
},
|
||||||
"scripts": {
|
"scripts": {
|
||||||
|
"install": "tsc --noCheck",
|
||||||
"lint": "yarn eslint .",
|
"lint": "yarn eslint .",
|
||||||
"build": "yarn tsc",
|
"build": "yarn tsc",
|
||||||
"prepare": "yarn build",
|
"prepare": "yarn build",
|
||||||
@@ -61,6 +61,8 @@
|
|||||||
"windows",
|
"windows",
|
||||||
"src",
|
"src",
|
||||||
"lib",
|
"lib",
|
||||||
|
"tsconfig.json",
|
||||||
|
"tsconfig.build.json",
|
||||||
"react-native-video.podspec",
|
"react-native-video.podspec",
|
||||||
"app.plugin.js",
|
"app.plugin.js",
|
||||||
"!android/build",
|
"!android/build",
|
||||||
|
|||||||
@@ -1,39 +0,0 @@
|
|||||||
diff --git a/node_modules/shaka-player/dist/shaka-player.compiled.d.ts b/node_modules/shaka-player/dist/shaka-player.compiled.d.ts
|
|
||||||
index 19c0930..cc0a3fd 100644
|
|
||||||
--- a/node_modules/shaka-player/dist/shaka-player.compiled.d.ts
|
|
||||||
+++ b/node_modules/shaka-player/dist/shaka-player.compiled.d.ts
|
|
||||||
@@ -5117,3 +5117,5 @@ declare namespace shaka.extern {
|
|
||||||
declare namespace shaka.extern {
|
|
||||||
type TransmuxerPlugin = ( ) => shaka.extern.Transmuxer ;
|
|
||||||
}
|
|
||||||
+
|
|
||||||
+export default shaka;
|
|
||||||
diff --git a/node_modules/shaka-player/dist/shaka-player.ui.d.ts b/node_modules/shaka-player/dist/shaka-player.ui.d.ts
|
|
||||||
index 1618ca0..a6076c6 100644
|
|
||||||
--- a/node_modules/shaka-player/dist/shaka-player.ui.d.ts
|
|
||||||
+++ b/node_modules/shaka-player/dist/shaka-player.ui.d.ts
|
|
||||||
@@ -5830,3 +5830,5 @@ declare namespace shaka.extern {
|
|
||||||
declare namespace shaka.extern {
|
|
||||||
type UIVolumeBarColors = { base : string , level : string } ;
|
|
||||||
}
|
|
||||||
+
|
|
||||||
+export default shaka;
|
|
||||||
diff --git a/node_modules/shaka-player/index.d.ts b/node_modules/shaka-player/index.d.ts
|
|
||||||
new file mode 100644
|
|
||||||
index 0000000..3ebfd96
|
|
||||||
--- /dev/null
|
|
||||||
+++ b/node_modules/shaka-player/index.d.ts
|
|
||||||
@@ -0,0 +1,2 @@
|
|
||||||
+/// <reference path="./dist/shaka-player.compiled.d.ts" />
|
|
||||||
+/// <reference path="./dist/shaka-player.ui.d.ts" />
|
|
||||||
\ No newline at end of file
|
|
||||||
diff --git a/node_modules/shaka-player/ui.d.ts b/node_modules/shaka-player/ui.d.ts
|
|
||||||
new file mode 100644
|
|
||||||
index 0000000..84a3be0
|
|
||||||
--- /dev/null
|
|
||||||
+++ b/node_modules/shaka-player/ui.d.ts
|
|
||||||
@@ -0,0 +1,3 @@
|
|
||||||
+import shaka from 'shaka-player/dist/shaka-player.ui'
|
|
||||||
+export * from 'shaka-player/dist/shaka-player.ui'
|
|
||||||
+export default shaka;
|
|
||||||
\ No newline at end of file
|
|
||||||
@@ -734,7 +734,7 @@ const Video = forwardRef<VideoRef, ReactVideoProps>(
|
|||||||
|
|
||||||
// poster style
|
// poster style
|
||||||
const baseStyle: StyleProp<ImageStyle> = {
|
const baseStyle: StyleProp<ImageStyle> = {
|
||||||
...StyleSheet.absoluteFillObject,
|
...StyleSheet.absoluteFill,
|
||||||
resizeMode: _posterResizeMode,
|
resizeMode: _posterResizeMode,
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -787,7 +787,7 @@ const Video = forwardRef<VideoRef, ReactVideoProps>(
|
|||||||
|
|
||||||
const _style: StyleProp<ViewStyle> = useMemo(
|
const _style: StyleProp<ViewStyle> = useMemo(
|
||||||
() => ({
|
() => ({
|
||||||
...StyleSheet.absoluteFillObject,
|
...StyleSheet.absoluteFill,
|
||||||
...(showPoster ? {display: 'none'} : {}),
|
...(showPoster ? {display: 'none'} : {}),
|
||||||
}),
|
}),
|
||||||
[showPoster],
|
[showPoster],
|
||||||
|
|||||||
@@ -4,70 +4,26 @@ import React, {
|
|||||||
useEffect,
|
useEffect,
|
||||||
useImperativeHandle,
|
useImperativeHandle,
|
||||||
useRef,
|
useRef,
|
||||||
useState,
|
|
||||||
type RefObject,
|
type RefObject,
|
||||||
} from 'react';
|
} from 'react';
|
||||||
//@ts-ignore
|
import Hls from 'hls.js';
|
||||||
import shaka from 'shaka-player';
|
|
||||||
import type {VideoRef, ReactVideoProps, VideoMetadata} from './types';
|
import type {VideoRef, ReactVideoProps, VideoMetadata} from './types';
|
||||||
|
|
||||||
// Action Queue Class
|
const HLS_MIME = 'application/vnd.apple.mpegurl';
|
||||||
class ActionQueue {
|
|
||||||
private queue: { action: () => Promise<void>; name: string }[] = [];
|
|
||||||
private isRunning = false;
|
|
||||||
|
|
||||||
enqueue(action: () => Promise<void>, name: string) {
|
const isHlsSource = (uri: string) => /\.m3u8(\?|#|$)/i.test(uri);
|
||||||
this.queue.push({ action, name });
|
|
||||||
this.runNext();
|
// `poster` is either a (deprecated) plain URI string or a poster object whose
|
||||||
}
|
// `source` follows the RN image-source shape. The DOM only takes a URL string.
|
||||||
|
const resolvePosterUri = (
|
||||||
private async runNext() {
|
poster: ReactVideoProps['poster'],
|
||||||
if (this.isRunning || this.queue.length === 0) {
|
): string | undefined => {
|
||||||
console.log("Refusing to run in runNext", this.queue.length, this.isRunning);
|
if (typeof poster === 'string') {
|
||||||
return;
|
return poster;
|
||||||
}
|
|
||||||
this.isRunning = true;
|
|
||||||
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();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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];
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
const source = poster?.source;
|
||||||
|
return typeof source === 'object' && source !== null ? source.uri : undefined;
|
||||||
|
};
|
||||||
|
|
||||||
const Video = forwardRef<VideoRef, ReactVideoProps>(
|
const Video = forwardRef<VideoRef, ReactVideoProps>(
|
||||||
(
|
(
|
||||||
@@ -99,15 +55,10 @@ const Video = forwardRef<VideoRef, ReactVideoProps>(
|
|||||||
ref,
|
ref,
|
||||||
) => {
|
) => {
|
||||||
const nativeRef = useRef<HTMLVideoElement>(null);
|
const nativeRef = useRef<HTMLVideoElement>(null);
|
||||||
const shakaPlayerRef = useRef<shaka.Player | 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(
|
||||||
(time: number, _tolerance?: number) => {
|
async (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');
|
||||||
}
|
}
|
||||||
@@ -117,44 +68,33 @@ 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?.({
|
onSeek?.({seekTime: time, currentTime: nativeRef.current.currentTime});
|
||||||
seekTime: time,
|
|
||||||
currentTime: nativeRef.current.currentTime,
|
|
||||||
});
|
|
||||||
}, 'seek');
|
|
||||||
},
|
},
|
||||||
[onSeek],
|
[onSeek],
|
||||||
);
|
);
|
||||||
|
|
||||||
const pause = useCallback(() => {
|
const pause = useCallback(() => {
|
||||||
actionQueue.current.enqueue(async () => {
|
|
||||||
if (!nativeRef.current) {
|
if (!nativeRef.current) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
await nativeRef.current.pause();
|
nativeRef.current.pause();
|
||||||
}, 'pause');
|
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
const resume = useCallback(() => {
|
const resume = useCallback(() => {
|
||||||
actionQueue.current.enqueue(async () => {
|
|
||||||
if (!nativeRef.current) {
|
if (!nativeRef.current) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
try {
|
// play() rejects if it is called before the stream has data (common while
|
||||||
await nativeRef.current.play();
|
// hls.js is still parsing the manifest) or if autoplay is denied. Neither
|
||||||
} catch (e) {
|
// is fatal, so swallow it rather than surfacing an unhandled rejection.
|
||||||
console.error('Error playing video:', e);
|
nativeRef.current.play()?.catch(() => {});
|
||||||
}
|
|
||||||
}, 'resume');
|
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
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;
|
||||||
}, 'setVolume');
|
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
const getCurrentPosition = useCallback(async () => {
|
const getCurrentPosition = useCallback(async () => {
|
||||||
@@ -209,7 +149,7 @@ const Video = forwardRef<VideoRef, ReactVideoProps>(
|
|||||||
console.error('Could not toggle fullscreen/screen lock status', e);
|
console.error('Could not toggle fullscreen/screen lock status', e);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
actionQueue.current.enqueue(run, 'setFullScreen');
|
run();
|
||||||
},
|
},
|
||||||
[],
|
[],
|
||||||
);
|
);
|
||||||
@@ -272,7 +212,6 @@ const Video = forwardRef<VideoRef, ReactVideoProps>(
|
|||||||
resume();
|
resume();
|
||||||
}
|
}
|
||||||
}, [paused, pause, resume]);
|
}, [paused, pause, resume]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (volume === undefined) {
|
if (volume === undefined) {
|
||||||
return;
|
return;
|
||||||
@@ -307,107 +246,57 @@ const Video = forwardRef<VideoRef, ReactVideoProps>(
|
|||||||
nativeRef.current.playbackRate = rate;
|
nativeRef.current.playbackRate = rate;
|
||||||
}, [rate]);
|
}, [rate]);
|
||||||
|
|
||||||
const makeNewShaka = useCallback(() => {
|
// `onError` is typically a fresh closure on every render. Keep it in a ref so
|
||||||
console.log("makeNewShaka");
|
// the source effect below can depend on the URI alone -- callers pass
|
||||||
actionQueue.current.enqueue(async () => {
|
// `source={{uri}}` as a new object literal each render, so keying the effect
|
||||||
console.log("makeNewShaka actionQueue");
|
// on anything object-shaped would tear down and reload the stream constantly.
|
||||||
if (!nativeRef.current) {
|
const onErrorRef = useRef(onError);
|
||||||
console.warn('No video element to attach Shaka Player');
|
onErrorRef.current = onError;
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Pause the video before changing the source
|
const uri = source?.uri as string | undefined;
|
||||||
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();
|
|
||||||
|
|
||||||
shakaPlayerRef.current.attach(nativeRef.current);
|
|
||||||
|
|
||||||
if (source?.cropStart) {
|
|
||||||
shakaPlayerRef.current.configure({
|
|
||||||
playRangeStart: source?.cropStart / 1000,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
if (source?.cropEnd) {
|
|
||||||
shakaPlayerRef.current.configure({
|
|
||||||
playRangeEnd: source?.cropEnd / 1000,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
//@ts-ignore
|
|
||||||
shakaPlayerRef.current.addEventListener('error', event => {
|
|
||||||
//@ts-ignore
|
|
||||||
const shakaError = event.detail;
|
|
||||||
console.error('Shaka Player Error', shakaError);
|
|
||||||
onError?.({
|
|
||||||
error: {
|
|
||||||
errorString: shakaError.message,
|
|
||||||
code: shakaError.code,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
console.log('Initializing and attaching shaka');
|
|
||||||
|
|
||||||
// Load the new source
|
|
||||||
try {
|
|
||||||
//@ts-ignore
|
|
||||||
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: {
|
|
||||||
//@ts-ignore
|
|
||||||
errorString: e.message,
|
|
||||||
//@ts-ignore
|
|
||||||
code: e.code,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}, 'makeNewShaka');
|
|
||||||
}, [source, paused, onError]);
|
|
||||||
|
|
||||||
const nativeRefDefined = !!nativeRef.current;
|
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!nativeRef.current) {
|
const video = nativeRef.current;
|
||||||
console.log('Not starting shaka yet because video element is undefined');
|
if (!video || !uri) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (!shallowEqual(source, currentSource)) {
|
|
||||||
console.log(
|
// Safari (and iOS WebKit) plays HLS natively; everywhere else the browser
|
||||||
'Making new shaka, Old source: ',
|
// has no HLS support at all, so an .m3u8 has to go through hls.js.
|
||||||
currentSource,
|
const needsMse =
|
||||||
'New source',
|
isHlsSource(uri) && !video.canPlayType(HLS_MIME) && Hls.isSupported();
|
||||||
source,
|
|
||||||
);
|
if (!needsMse) {
|
||||||
//@ts-ignore
|
video.src = uri;
|
||||||
setCurrentSource(source);
|
return () => {
|
||||||
makeNewShaka();
|
video.removeAttribute('src');
|
||||||
|
video.load();
|
||||||
|
};
|
||||||
}
|
}
|
||||||
}, [source, nativeRefDefined, currentSource, makeNewShaka]);
|
|
||||||
|
const hls = new Hls();
|
||||||
|
hls.on(Hls.Events.ERROR, (_event, data) => {
|
||||||
|
// hls.js surfaces plenty of recoverable warnings; only fatal errors are
|
||||||
|
// worth propagating as a video error.
|
||||||
|
if (!data.fatal) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
onErrorRef.current?.({
|
||||||
|
error: {
|
||||||
|
errorString: `${data.type}: ${data.details}`,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
});
|
||||||
|
hls.loadSource(uri);
|
||||||
|
hls.attachMedia(video);
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
hls.destroy();
|
||||||
|
};
|
||||||
|
}, [uri]);
|
||||||
|
|
||||||
useMediaSession(source?.metadata, nativeRef, showNotificationControls);
|
useMediaSession(source?.metadata, nativeRef, showNotificationControls);
|
||||||
|
|
||||||
const cropStartSeconds = (source?.cropStart || 0) / 1000;
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<video
|
<video
|
||||||
ref={nativeRef}
|
ref={nativeRef}
|
||||||
@@ -416,8 +305,7 @@ const Video = forwardRef<VideoRef, ReactVideoProps>(
|
|||||||
controls={controls}
|
controls={controls}
|
||||||
loop={repeat}
|
loop={repeat}
|
||||||
playsInline
|
playsInline
|
||||||
//@ts-ignore
|
poster={resolvePosterUri(poster)}
|
||||||
poster={poster}
|
|
||||||
onCanPlay={() => onBuffer?.({isBuffering: false})}
|
onCanPlay={() => onBuffer?.({isBuffering: false})}
|
||||||
onWaiting={() => onBuffer?.({isBuffering: true})}
|
onWaiting={() => onBuffer?.({isBuffering: true})}
|
||||||
onRateChange={() => {
|
onRateChange={() => {
|
||||||
@@ -450,7 +338,7 @@ const Video = forwardRef<VideoRef, ReactVideoProps>(
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
onProgress?.({
|
onProgress?.({
|
||||||
currentTime: nativeRef.current.currentTime - cropStartSeconds,
|
currentTime: nativeRef.current.currentTime,
|
||||||
playableDuration: nativeRef.current.buffered.length
|
playableDuration: nativeRef.current.buffered.length
|
||||||
? nativeRef.current.buffered.end(
|
? nativeRef.current.buffered.end(
|
||||||
nativeRef.current.buffered.length - 1,
|
nativeRef.current.buffered.length - 1,
|
||||||
@@ -490,14 +378,13 @@ const Video = forwardRef<VideoRef, ReactVideoProps>(
|
|||||||
}
|
}
|
||||||
onSeeking={() => (isSeeking.current = true)}
|
onSeeking={() => (isSeeking.current = true)}
|
||||||
onSeeked={() => {
|
onSeeked={() => {
|
||||||
(isSeeking.current = false)
|
isSeeking.current = false;
|
||||||
|
const currentTime = nativeRef.current?.currentTime ?? 0;
|
||||||
onSeekComplete?.({
|
onSeekComplete?.({
|
||||||
currentTime: (nativeRef.current?.currentTime || 0.0) - cropStartSeconds,
|
currentTime,
|
||||||
seekTime: 0.0,
|
seekTime: currentTime,
|
||||||
target: 0.0,
|
target: 0,
|
||||||
|
});
|
||||||
})
|
|
||||||
}}
|
}}
|
||||||
onVolumeChange={() => {
|
onVolumeChange={() => {
|
||||||
if (!nativeRef.current) {
|
if (!nativeRef.current) {
|
||||||
@@ -515,7 +402,9 @@ const Video = forwardRef<VideoRef, ReactVideoProps>(
|
|||||||
const videoStyle = {
|
const videoStyle = {
|
||||||
position: 'absolute',
|
position: 'absolute',
|
||||||
inset: 0,
|
inset: 0,
|
||||||
objectFit: 'contain',
|
// Use 'fill' instead of 'contain' to force video to match container dimensions.
|
||||||
|
// This works around browsers miscalculating intrinsic dimensions from rotation matrices.
|
||||||
|
objectFit: 'fill',
|
||||||
width: '100%',
|
width: '100%',
|
||||||
height: '100%',
|
height: '100%',
|
||||||
} satisfies React.CSSProperties;
|
} satisfies React.CSSProperties;
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
|
|
||||||
{
|
{
|
||||||
"extends": "./tsconfig",
|
"extends": "./tsconfig",
|
||||||
"exclude": ["examples", "lib"]
|
"exclude": ["examples", "lib"]
|
||||||
|
|||||||
@@ -14,8 +14,8 @@
|
|||||||
"forceConsistentCasingInFileNames": true,
|
"forceConsistentCasingInFileNames": true,
|
||||||
"jsx": "react",
|
"jsx": "react",
|
||||||
"lib": ["esnext"],
|
"lib": ["esnext"],
|
||||||
"module": "CommonJS",
|
"module": "Node16",
|
||||||
"moduleResolution": "node",
|
"moduleResolution": "Node16",
|
||||||
"noFallthroughCasesInSwitch": true,
|
"noFallthroughCasesInSwitch": true,
|
||||||
"noImplicitReturns": true,
|
"noImplicitReturns": true,
|
||||||
"noImplicitUseStrict": false,
|
"noImplicitUseStrict": false,
|
||||||
|
|||||||
Reference in New Issue
Block a user