Compare commits
10 Commits
ivan/repla
...
loewy/fix-
| Author | SHA1 | Date | |
|---|---|---|---|
| 7c0669190f | |||
| 452a9748d5 | |||
| b81dae8878 | |||
| ba3dea885d | |||
| a1579eebad | |||
| 42608bd8d1 | |||
| 04682d02c7 | |||
| ec3cb15984 | |||
| ff6c4316cf | |||
| dfe2957087 |
38
.gitea/workflows/check-js.yaml
Normal file
38
.gitea/workflows/check-js.yaml
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
name: Check JS
|
||||||
|
|
||||||
|
on:
|
||||||
|
pull_request:
|
||||||
|
push:
|
||||||
|
branches:
|
||||||
|
- railbird-v6
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
typescript:
|
||||||
|
name: Check TS (tsc)
|
||||||
|
runs-on: nixos-x86_64-linux
|
||||||
|
defaults:
|
||||||
|
run:
|
||||||
|
shell: nix shell nixpkgs#nodejs_22 nixpkgs#yarn -c bash -e {0}
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v4
|
||||||
|
- name: Install node_modules and build package
|
||||||
|
run: yarn install --immutable
|
||||||
|
- name: Check TypeScript
|
||||||
|
run: yarn tsc
|
||||||
|
- name: Test web HLS
|
||||||
|
run: yarn test:web
|
||||||
|
|
||||||
|
lint:
|
||||||
|
name: Lint JS (eslint, prettier)
|
||||||
|
runs-on: nixos-x86_64-linux
|
||||||
|
defaults:
|
||||||
|
run:
|
||||||
|
shell: nix shell nixpkgs#nodejs_22 nixpkgs#yarn -c bash -e {0}
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v4
|
||||||
|
- name: Install node_modules and build package
|
||||||
|
run: yarn install --immutable
|
||||||
|
- name: Run ESLint
|
||||||
|
run: yarn lint
|
||||||
|
- name: Verify auto-fix produces no changes
|
||||||
|
run: yarn lint --fix && git diff --exit-code HEAD
|
||||||
@@ -31,11 +31,11 @@
|
|||||||
"react": "18.2.0",
|
"react": "18.2.0",
|
||||||
"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"
|
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"hls.js": "^1.6.16"
|
"hls.js": "^1.6.16",
|
||||||
|
"typescript": "5.6.3"
|
||||||
},
|
},
|
||||||
"peerDependencies": {
|
"peerDependencies": {
|
||||||
"react": "*",
|
"react": "*",
|
||||||
@@ -50,6 +50,7 @@
|
|||||||
"docs": "yarn --cwd docs build",
|
"docs": "yarn --cwd docs build",
|
||||||
"release": "release-it",
|
"release": "release-it",
|
||||||
"test": "echo no test available",
|
"test": "echo no test available",
|
||||||
|
"test:web": "yarn tsc -p test/tsconfig.json && node --test test/web-hls.test.js",
|
||||||
"check-ios": "scripts/swift-format.sh && scripts/swift-lint.sh && scripts/clang-format.sh",
|
"check-ios": "scripts/swift-format.sh && scripts/swift-lint.sh && scripts/clang-format.sh",
|
||||||
"check-android": "scripts/kotlin-lint.sh",
|
"check-android": "scripts/kotlin-lint.sh",
|
||||||
"check-all": "yarn check-android; yarn check-ios; yarn lint",
|
"check-all": "yarn check-android; yarn check-ios; yarn lint",
|
||||||
|
|||||||
@@ -475,7 +475,7 @@ const Video = forwardRef<VideoRef, ReactVideoProps>(
|
|||||||
(e: NativeSyntheticEvent<OnSeekCompleteData>) => {
|
(e: NativeSyntheticEvent<OnSeekCompleteData>) => {
|
||||||
onSeekComplete?.(e.nativeEvent);
|
onSeekComplete?.(e.nativeEvent);
|
||||||
},
|
},
|
||||||
[onSeekComplete]
|
[onSeekComplete],
|
||||||
);
|
);
|
||||||
|
|
||||||
const onVideoPlaybackStateChanged = useCallback(
|
const onVideoPlaybackStateChanged = useCallback(
|
||||||
@@ -733,10 +733,10 @@ const Video = forwardRef<VideoRef, ReactVideoProps>(
|
|||||||
}
|
}
|
||||||
|
|
||||||
// poster style
|
// poster style
|
||||||
const baseStyle: StyleProp<ImageStyle> = {
|
const baseStyle: StyleProp<ImageStyle> = [
|
||||||
...StyleSheet.absoluteFill,
|
StyleSheet.absoluteFill,
|
||||||
resizeMode: _posterResizeMode,
|
{resizeMode: _posterResizeMode},
|
||||||
};
|
];
|
||||||
|
|
||||||
let posterStyle: StyleProp<ImageStyle> = baseStyle;
|
let posterStyle: StyleProp<ImageStyle> = baseStyle;
|
||||||
|
|
||||||
@@ -786,10 +786,10 @@ const Video = forwardRef<VideoRef, ReactVideoProps>(
|
|||||||
]);
|
]);
|
||||||
|
|
||||||
const _style: StyleProp<ViewStyle> = useMemo(
|
const _style: StyleProp<ViewStyle> = useMemo(
|
||||||
() => ({
|
() => [
|
||||||
...StyleSheet.absoluteFill,
|
StyleSheet.absoluteFill,
|
||||||
...(showPoster ? {display: 'none'} : {}),
|
showPoster ? {display: 'none'} : undefined,
|
||||||
}),
|
],
|
||||||
[showPoster],
|
[showPoster],
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|||||||
@@ -8,11 +8,17 @@ import React, {
|
|||||||
} from 'react';
|
} from 'react';
|
||||||
import Hls from 'hls.js';
|
import Hls from 'hls.js';
|
||||||
import type {VideoRef, ReactVideoProps, VideoMetadata} from './types';
|
import type {VideoRef, ReactVideoProps, VideoMetadata} from './types';
|
||||||
|
import {
|
||||||
|
createWebPlaybackErrorReporter,
|
||||||
|
describeHlsError,
|
||||||
|
describeMediaError,
|
||||||
|
isAppleWebKit,
|
||||||
|
selectWebHlsPlaybackMode,
|
||||||
|
type WebHlsPlaybackMode,
|
||||||
|
} from './webHls';
|
||||||
|
|
||||||
const HLS_MIME = 'application/vnd.apple.mpegurl';
|
const HLS_MIME = 'application/vnd.apple.mpegurl';
|
||||||
|
|
||||||
const isHlsSource = (uri: string) => /\.m3u8(\?|#|$)/i.test(uri);
|
|
||||||
|
|
||||||
// `poster` is either a (deprecated) plain URI string or a poster object whose
|
// `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.
|
// `source` follows the RN image-source shape. The DOM only takes a URL string.
|
||||||
const resolvePosterUri = (
|
const resolvePosterUri = (
|
||||||
@@ -127,21 +133,31 @@ const Video = forwardRef<VideoRef, ReactVideoProps>(
|
|||||||
autorotate ??= fsPrefs.current.fullscreenAutorotate;
|
autorotate ??= fsPrefs.current.fullscreenAutorotate;
|
||||||
|
|
||||||
const run = async () => {
|
const run = async () => {
|
||||||
|
const browserOrientation = screen.orientation as
|
||||||
|
| {
|
||||||
|
lock?: (orientation: 'landscape' | 'portrait') => Promise<void>;
|
||||||
|
unlock?: () => void;
|
||||||
|
}
|
||||||
|
| undefined;
|
||||||
try {
|
try {
|
||||||
if (newVal) {
|
if (newVal) {
|
||||||
await nativeRef.current?.requestFullscreen({
|
await nativeRef.current?.requestFullscreen({
|
||||||
navigationUI: 'hide',
|
navigationUI: 'hide',
|
||||||
});
|
});
|
||||||
if (orientation === 'all' || !orientation || autorotate) {
|
if (orientation === 'all' || !orientation || autorotate) {
|
||||||
screen.orientation.unlock();
|
if (typeof browserOrientation?.unlock === 'function') {
|
||||||
} else {
|
browserOrientation.unlock();
|
||||||
await screen.orientation.lock(orientation);
|
}
|
||||||
|
} else if (typeof browserOrientation?.lock === 'function') {
|
||||||
|
await browserOrientation.lock(orientation);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (document.fullscreenElement) {
|
if (document.fullscreenElement) {
|
||||||
await document.exitFullscreen();
|
await document.exitFullscreen();
|
||||||
}
|
}
|
||||||
screen.orientation.unlock();
|
if (typeof browserOrientation?.unlock === 'function') {
|
||||||
|
browserOrientation.unlock();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
// Changing fullscreen status without a button click is not allowed so it throws.
|
// Changing fullscreen status without a button click is not allowed so it throws.
|
||||||
@@ -252,8 +268,20 @@ const Video = forwardRef<VideoRef, ReactVideoProps>(
|
|||||||
// on anything object-shaped would tear down and reload the stream constantly.
|
// on anything object-shaped would tear down and reload the stream constantly.
|
||||||
const onErrorRef = useRef(onError);
|
const onErrorRef = useRef(onError);
|
||||||
onErrorRef.current = onError;
|
onErrorRef.current = onError;
|
||||||
|
const playbackErrorReporterRef =
|
||||||
|
useRef<ReturnType<typeof createWebPlaybackErrorReporter>>();
|
||||||
|
if (!playbackErrorReporterRef.current) {
|
||||||
|
playbackErrorReporterRef.current = createWebPlaybackErrorReporter(
|
||||||
|
(error) => onErrorRef.current?.({error}),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
const playbackDiagnosticsRef = useRef<{
|
||||||
|
mode: WebHlsPlaybackMode;
|
||||||
|
nativeHlsSupport: string;
|
||||||
|
}>();
|
||||||
|
|
||||||
const uri = source?.uri as string | undefined;
|
const uri = source?.uri as string | undefined;
|
||||||
|
const sourceType = source?.type;
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const video = nativeRef.current;
|
const video = nativeRef.current;
|
||||||
@@ -261,14 +289,19 @@ const Video = forwardRef<VideoRef, ReactVideoProps>(
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Safari (and iOS WebKit) plays HLS natively; everywhere else the browser
|
playbackErrorReporterRef.current?.reset();
|
||||||
// has no HLS support at all, so an .m3u8 has to go through hls.js.
|
const nativeHlsSupport = video.canPlayType(HLS_MIME);
|
||||||
const needsMse =
|
const mode = selectWebHlsPlaybackMode(uri, sourceType, {
|
||||||
isHlsSource(uri) && !video.canPlayType(HLS_MIME) && Hls.isSupported();
|
hlsJsSupported: Hls.isSupported(),
|
||||||
|
nativeHlsSupport,
|
||||||
|
preferNativeHls: isAppleWebKit(navigator.userAgent, navigator.vendor),
|
||||||
|
});
|
||||||
|
playbackDiagnosticsRef.current = {mode, nativeHlsSupport};
|
||||||
|
|
||||||
if (!needsMse) {
|
if (mode === 'native') {
|
||||||
video.src = uri;
|
video.src = uri;
|
||||||
return () => {
|
return () => {
|
||||||
|
playbackErrorReporterRef.current?.reset();
|
||||||
video.removeAttribute('src');
|
video.removeAttribute('src');
|
||||||
video.load();
|
video.load();
|
||||||
};
|
};
|
||||||
@@ -281,19 +314,22 @@ const Video = forwardRef<VideoRef, ReactVideoProps>(
|
|||||||
if (!data.fatal) {
|
if (!data.fatal) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
onErrorRef.current?.({
|
const errorString = `${describeHlsError(
|
||||||
error: {
|
data,
|
||||||
errorString: `${data.type}: ${data.details}`,
|
)}; mode=hls.js; nativeHlsSupport=${nativeHlsSupport || 'none'}`;
|
||||||
},
|
playbackErrorReporterRef.current?.addHls(
|
||||||
});
|
errorString,
|
||||||
|
`hls.js/${data.type}`,
|
||||||
|
);
|
||||||
});
|
});
|
||||||
hls.loadSource(uri);
|
hls.loadSource(uri);
|
||||||
hls.attachMedia(video);
|
hls.attachMedia(video);
|
||||||
|
|
||||||
return () => {
|
return () => {
|
||||||
|
playbackErrorReporterRef.current?.reset();
|
||||||
hls.destroy();
|
hls.destroy();
|
||||||
};
|
};
|
||||||
}, [uri]);
|
}, [sourceType, uri]);
|
||||||
|
|
||||||
useMediaSession(source?.metadata, nativeRef, showNotificationControls);
|
useMediaSession(source?.metadata, nativeRef, showNotificationControls);
|
||||||
|
|
||||||
@@ -349,15 +385,23 @@ const Video = forwardRef<VideoRef, ReactVideoProps>(
|
|||||||
}}
|
}}
|
||||||
onLoadedData={() => onReadyForDisplay?.()}
|
onLoadedData={() => onReadyForDisplay?.()}
|
||||||
onError={() => {
|
onError={() => {
|
||||||
if (!nativeRef.current?.error) {
|
const video = nativeRef.current;
|
||||||
|
if (!video?.error) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
onError?.({
|
const diagnostics = playbackDiagnosticsRef.current;
|
||||||
error: {
|
const mediaError = describeMediaError({
|
||||||
errorString: nativeRef.current.error.message ?? 'Unknown error',
|
currentSrc: video.currentSrc,
|
||||||
code: nativeRef.current.error.code,
|
networkState: video.networkState,
|
||||||
},
|
readyState: video.readyState,
|
||||||
|
error: video.error,
|
||||||
|
playbackMode: diagnostics?.mode,
|
||||||
|
nativeHlsSupport: diagnostics?.nativeHlsSupport,
|
||||||
});
|
});
|
||||||
|
playbackErrorReporterRef.current?.addMedia(
|
||||||
|
mediaError,
|
||||||
|
video.error.code,
|
||||||
|
);
|
||||||
}}
|
}}
|
||||||
onLoadedMetadata={() => {
|
onLoadedMetadata={() => {
|
||||||
if (source?.startPosition) {
|
if (source?.startPosition) {
|
||||||
@@ -414,7 +458,7 @@ const useMediaSession = (
|
|||||||
nativeRef: RefObject<HTMLVideoElement>,
|
nativeRef: RefObject<HTMLVideoElement>,
|
||||||
showNotification: boolean,
|
showNotification: boolean,
|
||||||
) => {
|
) => {
|
||||||
const isPlaying = !nativeRef.current?.paused ?? false;
|
const isPlaying = nativeRef.current ? !nativeRef.current.paused : false;
|
||||||
const progress = nativeRef.current?.currentTime ?? 0;
|
const progress = nativeRef.current?.currentTime ?? 0;
|
||||||
const duration = Number.isFinite(nativeRef.current?.duration)
|
const duration = Number.isFinite(nativeRef.current?.duration)
|
||||||
? nativeRef.current?.duration
|
? nativeRef.current?.duration
|
||||||
|
|||||||
248
src/webHls.ts
Normal file
248
src/webHls.ts
Normal file
@@ -0,0 +1,248 @@
|
|||||||
|
export type WebHlsPlaybackMode = 'hls.js' | 'native';
|
||||||
|
|
||||||
|
export interface WebHlsPlaybackCapabilities {
|
||||||
|
hlsJsSupported: boolean;
|
||||||
|
nativeHlsSupport: string;
|
||||||
|
preferNativeHls: boolean;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface HlsErrorDetails {
|
||||||
|
type: string;
|
||||||
|
details: string;
|
||||||
|
reason?: string;
|
||||||
|
error?: {message?: string};
|
||||||
|
response?: {
|
||||||
|
code?: number;
|
||||||
|
text?: string;
|
||||||
|
url?: string;
|
||||||
|
data?: unknown;
|
||||||
|
};
|
||||||
|
url?: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface MediaElementDiagnostics {
|
||||||
|
currentSrc: string;
|
||||||
|
networkState: number;
|
||||||
|
readyState: number;
|
||||||
|
error: {code: number; message?: string} | null;
|
||||||
|
playbackMode?: WebHlsPlaybackMode;
|
||||||
|
nativeHlsSupport?: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface WebVideoError {
|
||||||
|
errorString: string;
|
||||||
|
code?: number;
|
||||||
|
domain: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
const HLS_SOURCE_PATTERN = /\.m3u8(\?|#|$)/i;
|
||||||
|
const HLS_SOURCE_TYPES = new Set([
|
||||||
|
'application/vnd.apple.mpegurl',
|
||||||
|
'application/x-mpegurl',
|
||||||
|
'hls',
|
||||||
|
'm3u8',
|
||||||
|
]);
|
||||||
|
|
||||||
|
const MEDIA_ERROR_NAMES: Record<number, string> = {
|
||||||
|
1: 'MEDIA_ERR_ABORTED',
|
||||||
|
2: 'MEDIA_ERR_NETWORK',
|
||||||
|
3: 'MEDIA_ERR_DECODE',
|
||||||
|
4: 'MEDIA_ERR_SRC_NOT_SUPPORTED',
|
||||||
|
};
|
||||||
|
|
||||||
|
export const isHlsSource = (uri: string, sourceType?: string): boolean => {
|
||||||
|
const normalizedType = sourceType?.split(';', 1)[0]?.trim().toLowerCase();
|
||||||
|
return (
|
||||||
|
HLS_SOURCE_PATTERN.test(uri) ||
|
||||||
|
(normalizedType !== undefined && HLS_SOURCE_TYPES.has(normalizedType))
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
export const isAppleWebKit = (userAgent: string, vendor: string): boolean =>
|
||||||
|
/AppleWebKit/i.test(userAgent) && /Apple/i.test(vendor);
|
||||||
|
|
||||||
|
export const selectWebHlsPlaybackMode = (
|
||||||
|
uri: string,
|
||||||
|
sourceType: string | undefined,
|
||||||
|
{
|
||||||
|
hlsJsSupported,
|
||||||
|
nativeHlsSupport,
|
||||||
|
preferNativeHls,
|
||||||
|
}: WebHlsPlaybackCapabilities,
|
||||||
|
): WebHlsPlaybackMode => {
|
||||||
|
if (!isHlsSource(uri, sourceType)) {
|
||||||
|
return 'native';
|
||||||
|
}
|
||||||
|
|
||||||
|
if (preferNativeHls && nativeHlsSupport) {
|
||||||
|
return 'native';
|
||||||
|
}
|
||||||
|
|
||||||
|
return hlsJsSupported ? 'hls.js' : 'native';
|
||||||
|
};
|
||||||
|
|
||||||
|
const compactText = (value: string): string =>
|
||||||
|
value.replace(/\s+/g, ' ').trim();
|
||||||
|
|
||||||
|
const limitedText = (value: string, maxLength = 160): string => {
|
||||||
|
const compact = compactText(value);
|
||||||
|
return compact.length > maxLength
|
||||||
|
? `${compact.slice(0, maxLength)}…`
|
||||||
|
: compact;
|
||||||
|
};
|
||||||
|
|
||||||
|
const SAFE_RESPONSE_MESSAGES = new Set([
|
||||||
|
'Playlist does not exist',
|
||||||
|
'Video stream not found',
|
||||||
|
]);
|
||||||
|
|
||||||
|
export const sanitizeDiagnosticUrl = (value: string): string => {
|
||||||
|
try {
|
||||||
|
const url = new URL(value);
|
||||||
|
url.username = '';
|
||||||
|
url.password = '';
|
||||||
|
url.search = url.search ? '?<redacted>' : '';
|
||||||
|
url.hash = url.hash ? '#<redacted>' : '';
|
||||||
|
return url.toString().replaceAll('%3Credacted%3E', '<redacted>');
|
||||||
|
} catch {
|
||||||
|
return value.replace(/[?#].*$/, '?<redacted>');
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const sanitizeHlsText = (value: string, data: HlsErrorDetails): string => {
|
||||||
|
let sanitized = value;
|
||||||
|
for (const url of [data.response?.url, data.url]) {
|
||||||
|
if (url) {
|
||||||
|
sanitized = sanitized.replaceAll(url, sanitizeDiagnosticUrl(url));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return limitedText(sanitized);
|
||||||
|
};
|
||||||
|
|
||||||
|
export const describeHlsError = (data: HlsErrorDetails): string => {
|
||||||
|
const parts = [`hls.js fatal ${data.type}: ${data.details}`];
|
||||||
|
|
||||||
|
if (data.reason && data.reason !== data.details) {
|
||||||
|
parts.push(`reason=${sanitizeHlsText(data.reason, data)}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (data.error?.message && data.error.message !== data.reason) {
|
||||||
|
parts.push(`message=${sanitizeHlsText(data.error.message, data)}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
const response = data.response;
|
||||||
|
if (response) {
|
||||||
|
if (response.code !== undefined) {
|
||||||
|
const statusText = response.text ? ` ${limitedText(response.text)}` : '';
|
||||||
|
parts.push(`HTTP ${response.code}${statusText}`);
|
||||||
|
}
|
||||||
|
if (typeof response.data === 'string' && response.data.trim()) {
|
||||||
|
const responseBody = compactText(response.data);
|
||||||
|
if (SAFE_RESPONSE_MESSAGES.has(responseBody)) {
|
||||||
|
parts.push(`response=${responseBody}`);
|
||||||
|
} else {
|
||||||
|
parts.push(`responseBodyLength=${response.data.length}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (response.url) {
|
||||||
|
parts.push(`url=${sanitizeDiagnosticUrl(response.url)}`);
|
||||||
|
}
|
||||||
|
} else if (data.url) {
|
||||||
|
parts.push(`url=${sanitizeDiagnosticUrl(data.url)}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
return parts.join('; ');
|
||||||
|
};
|
||||||
|
|
||||||
|
export const createWebPlaybackErrorReporter = (
|
||||||
|
report: (error: WebVideoError) => void,
|
||||||
|
debounceMs = 50,
|
||||||
|
) => {
|
||||||
|
let hlsError: string | undefined;
|
||||||
|
let hlsDomain: string | undefined;
|
||||||
|
let mediaError: string | undefined;
|
||||||
|
let mediaCode: number | undefined;
|
||||||
|
let timeout: ReturnType<typeof setTimeout> | undefined;
|
||||||
|
let reported = false;
|
||||||
|
|
||||||
|
const clearTimer = () => {
|
||||||
|
if (timeout !== undefined) {
|
||||||
|
clearTimeout(timeout);
|
||||||
|
timeout = undefined;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const schedule = () => {
|
||||||
|
if (reported) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
clearTimer();
|
||||||
|
timeout = setTimeout(() => {
|
||||||
|
timeout = undefined;
|
||||||
|
reported = true;
|
||||||
|
const errorString = [hlsError, mediaError].filter(Boolean).join('; ');
|
||||||
|
report({
|
||||||
|
errorString,
|
||||||
|
code: mediaCode,
|
||||||
|
domain: hlsError
|
||||||
|
? mediaError
|
||||||
|
? 'hls.js/HTMLMediaElement'
|
||||||
|
: hlsDomain || 'hls.js'
|
||||||
|
: 'HTMLMediaElement',
|
||||||
|
});
|
||||||
|
}, debounceMs);
|
||||||
|
};
|
||||||
|
|
||||||
|
const reset = () => {
|
||||||
|
clearTimer();
|
||||||
|
hlsError = undefined;
|
||||||
|
hlsDomain = undefined;
|
||||||
|
mediaError = undefined;
|
||||||
|
mediaCode = undefined;
|
||||||
|
reported = false;
|
||||||
|
};
|
||||||
|
|
||||||
|
return {
|
||||||
|
addHls(errorString: string, domain: string) {
|
||||||
|
if (reported) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
hlsError = errorString;
|
||||||
|
hlsDomain = domain;
|
||||||
|
schedule();
|
||||||
|
},
|
||||||
|
addMedia(errorString: string, code: number) {
|
||||||
|
if (reported) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
mediaError = errorString;
|
||||||
|
mediaCode = code;
|
||||||
|
schedule();
|
||||||
|
},
|
||||||
|
reset,
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
export const describeMediaError = (media: MediaElementDiagnostics): string => {
|
||||||
|
const code = media.error?.code ?? 0;
|
||||||
|
const errorName = MEDIA_ERROR_NAMES[code] ?? 'MEDIA_ERR_UNKNOWN';
|
||||||
|
const parts = [`HTMLMediaElement ${errorName} (${code})`];
|
||||||
|
const message = media.error?.message?.trim();
|
||||||
|
|
||||||
|
if (message) {
|
||||||
|
parts.push(`message=${limitedText(message)}`);
|
||||||
|
}
|
||||||
|
if (media.playbackMode) {
|
||||||
|
parts.push(`mode=${media.playbackMode}`);
|
||||||
|
}
|
||||||
|
if (media.nativeHlsSupport !== undefined) {
|
||||||
|
parts.push(`nativeHlsSupport=${media.nativeHlsSupport || 'none'}`);
|
||||||
|
}
|
||||||
|
if (media.currentSrc) {
|
||||||
|
parts.push(`src=${sanitizeDiagnosticUrl(media.currentSrc)}`);
|
||||||
|
}
|
||||||
|
parts.push(`networkState=${media.networkState}`);
|
||||||
|
parts.push(`readyState=${media.readyState}`);
|
||||||
|
|
||||||
|
return parts.join('; ');
|
||||||
|
};
|
||||||
1
test/.gitignore
vendored
Normal file
1
test/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
lib/
|
||||||
9
test/tsconfig.json
Normal file
9
test/tsconfig.json
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
{
|
||||||
|
"extends": "../tsconfig.json",
|
||||||
|
"compilerOptions": {
|
||||||
|
"composite": false,
|
||||||
|
"rootDir": "../src",
|
||||||
|
"outDir": "./lib"
|
||||||
|
},
|
||||||
|
"include": ["../src/webHls.ts"]
|
||||||
|
}
|
||||||
228
test/web-hls.test.js
Normal file
228
test/web-hls.test.js
Normal file
@@ -0,0 +1,228 @@
|
|||||||
|
/* eslint-disable @typescript-eslint/no-var-requires */
|
||||||
|
const assert = require('node:assert/strict');
|
||||||
|
const test = require('node:test');
|
||||||
|
|
||||||
|
const {
|
||||||
|
createWebPlaybackErrorReporter,
|
||||||
|
describeHlsError,
|
||||||
|
describeMediaError,
|
||||||
|
isAppleWebKit,
|
||||||
|
isHlsSource,
|
||||||
|
sanitizeDiagnosticUrl,
|
||||||
|
selectWebHlsPlaybackMode,
|
||||||
|
} = require('./lib/webHls');
|
||||||
|
|
||||||
|
test('recognizes HLS playlist URLs with query strings', () => {
|
||||||
|
assert.equal(isHlsSource('https://api.example/playlist/42.m3u8?vod=1'), true);
|
||||||
|
assert.equal(isHlsSource('https://cdn.example/video.mp4'), false);
|
||||||
|
assert.equal(
|
||||||
|
isHlsSource('https://api.example/playlist/42', 'application/x-mpegURL'),
|
||||||
|
true,
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('uses hls.js in Chromium even when native HLS support reports maybe', () => {
|
||||||
|
const userAgent =
|
||||||
|
'Mozilla/5.0 Chrome/146.0.0.0 Safari/537.36 AppleWebKit/537.36';
|
||||||
|
assert.equal(isAppleWebKit(userAgent, 'Google Inc.'), false);
|
||||||
|
assert.equal(
|
||||||
|
selectWebHlsPlaybackMode(
|
||||||
|
'https://api.example/playlist/42.m3u8',
|
||||||
|
undefined,
|
||||||
|
{
|
||||||
|
hlsJsSupported: true,
|
||||||
|
nativeHlsSupport: 'maybe',
|
||||||
|
preferNativeHls: isAppleWebKit(userAgent, 'Google Inc.'),
|
||||||
|
},
|
||||||
|
),
|
||||||
|
'hls.js',
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('uses hls.js for an extensionless HLS source with an explicit type', () => {
|
||||||
|
assert.equal(
|
||||||
|
selectWebHlsPlaybackMode(
|
||||||
|
'https://api.example/playlist/42',
|
||||||
|
'application/vnd.apple.mpegurl; charset=utf-8',
|
||||||
|
{
|
||||||
|
hlsJsSupported: true,
|
||||||
|
nativeHlsSupport: '',
|
||||||
|
preferNativeHls: false,
|
||||||
|
},
|
||||||
|
),
|
||||||
|
'hls.js',
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('preserves native HLS in Apple WebKit browsers', () => {
|
||||||
|
const userAgent =
|
||||||
|
'Mozilla/5.0 Version/18.6 Safari/605.1.15 AppleWebKit/605.1.15';
|
||||||
|
assert.equal(isAppleWebKit(userAgent, 'Apple Computer, Inc.'), true);
|
||||||
|
assert.equal(
|
||||||
|
selectWebHlsPlaybackMode(
|
||||||
|
'https://api.example/playlist/42.m3u8',
|
||||||
|
undefined,
|
||||||
|
{
|
||||||
|
hlsJsSupported: true,
|
||||||
|
nativeHlsSupport: 'maybe',
|
||||||
|
preferNativeHls: isAppleWebKit(userAgent, 'Apple Computer, Inc.'),
|
||||||
|
},
|
||||||
|
),
|
||||||
|
'native',
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('falls back to native playback when hls.js is unavailable', () => {
|
||||||
|
assert.equal(
|
||||||
|
selectWebHlsPlaybackMode(
|
||||||
|
'https://api.example/playlist/42.m3u8',
|
||||||
|
undefined,
|
||||||
|
{
|
||||||
|
hlsJsSupported: false,
|
||||||
|
nativeHlsSupport: 'probably',
|
||||||
|
preferNativeHls: false,
|
||||||
|
},
|
||||||
|
),
|
||||||
|
'native',
|
||||||
|
);
|
||||||
|
assert.equal(
|
||||||
|
selectWebHlsPlaybackMode('https://cdn.example/video.mp4', undefined, {
|
||||||
|
hlsJsSupported: true,
|
||||||
|
nativeHlsSupport: '',
|
||||||
|
preferNativeHls: false,
|
||||||
|
}),
|
||||||
|
'native',
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('includes fatal HLS network and response details', () => {
|
||||||
|
assert.equal(
|
||||||
|
describeHlsError({
|
||||||
|
type: 'networkError',
|
||||||
|
details: 'manifestParsingError',
|
||||||
|
reason: 'no EXTM3U delimiter',
|
||||||
|
error: {message: 'manifest parsing failed'},
|
||||||
|
response: {
|
||||||
|
code: 200,
|
||||||
|
text: 'OK',
|
||||||
|
data: 'Playlist does not exist',
|
||||||
|
url: 'https://api.example/playlist/42.m3u8',
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
'hls.js fatal networkError: manifestParsingError; ' +
|
||||||
|
'reason=no EXTM3U delimiter; message=manifest parsing failed; HTTP 200 OK; ' +
|
||||||
|
'response=Playlist does not exist; ' +
|
||||||
|
'url=https://api.example/playlist/42.m3u8',
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('redacts URL credentials, query values, and unknown response bodies', () => {
|
||||||
|
assert.equal(
|
||||||
|
sanitizeDiagnosticUrl(
|
||||||
|
'https://user:password@cdn.example/42.m3u8?token=secret#private',
|
||||||
|
),
|
||||||
|
'https://cdn.example/42.m3u8?<redacted>#<redacted>',
|
||||||
|
);
|
||||||
|
assert.equal(
|
||||||
|
describeHlsError({
|
||||||
|
type: 'networkError',
|
||||||
|
details: 'manifestLoadError',
|
||||||
|
response: {
|
||||||
|
code: 403,
|
||||||
|
data: 'internal credential: secret',
|
||||||
|
url: 'https://cdn.example/42.m3u8?token=secret',
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
'hls.js fatal networkError: manifestLoadError; HTTP 403; ' +
|
||||||
|
'responseBodyLength=27; url=https://cdn.example/42.m3u8?<redacted>',
|
||||||
|
);
|
||||||
|
assert.equal(
|
||||||
|
describeHlsError({
|
||||||
|
type: 'keySystemError',
|
||||||
|
details: 'licenseRequestFailed',
|
||||||
|
error: {
|
||||||
|
message:
|
||||||
|
'License request failed (https://license.example/key?token=secret)',
|
||||||
|
},
|
||||||
|
url: 'https://license.example/key?token=secret',
|
||||||
|
}),
|
||||||
|
'hls.js fatal keySystemError: licenseRequestFailed; ' +
|
||||||
|
'message=License request failed (https://license.example/key?<redacted>); ' +
|
||||||
|
'url=https://license.example/key?<redacted>',
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
const wait = (milliseconds) =>
|
||||||
|
new Promise((resolve) => setTimeout(resolve, milliseconds));
|
||||||
|
|
||||||
|
for (const order of ['hls-first', 'media-first']) {
|
||||||
|
test(`combines HLS and media errors once when ${order}`, async () => {
|
||||||
|
const reports = [];
|
||||||
|
const reporter = createWebPlaybackErrorReporter(
|
||||||
|
(error) => reports.push(error),
|
||||||
|
5,
|
||||||
|
);
|
||||||
|
const addHls = () =>
|
||||||
|
reporter.addHls(
|
||||||
|
'hls.js fatal mediaError: bufferAppendError',
|
||||||
|
'hls.js/mediaError',
|
||||||
|
);
|
||||||
|
const addMedia = () =>
|
||||||
|
reporter.addMedia('HTMLMediaElement MEDIA_ERR_DECODE (3)', 3);
|
||||||
|
|
||||||
|
if (order === 'hls-first') {
|
||||||
|
addHls();
|
||||||
|
addMedia();
|
||||||
|
} else {
|
||||||
|
addMedia();
|
||||||
|
addHls();
|
||||||
|
}
|
||||||
|
await wait(15);
|
||||||
|
|
||||||
|
assert.deepEqual(reports, [
|
||||||
|
{
|
||||||
|
errorString:
|
||||||
|
'hls.js fatal mediaError: bufferAppendError; ' +
|
||||||
|
'HTMLMediaElement MEDIA_ERR_DECODE (3)',
|
||||||
|
code: 3,
|
||||||
|
domain: 'hls.js/HTMLMediaElement',
|
||||||
|
},
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
test('reset cancels a pending source error and permits the next source', async () => {
|
||||||
|
const reports = [];
|
||||||
|
const reporter = createWebPlaybackErrorReporter(
|
||||||
|
(error) => reports.push(error),
|
||||||
|
5,
|
||||||
|
);
|
||||||
|
reporter.addHls('stale source failure', 'hls.js/networkError');
|
||||||
|
reporter.reset();
|
||||||
|
reporter.addMedia('current source failure', 4);
|
||||||
|
await wait(15);
|
||||||
|
|
||||||
|
assert.deepEqual(reports, [
|
||||||
|
{
|
||||||
|
errorString: 'current source failure',
|
||||||
|
code: 4,
|
||||||
|
domain: 'HTMLMediaElement',
|
||||||
|
},
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('names media error code 4 and records the selected playback path', () => {
|
||||||
|
assert.equal(
|
||||||
|
describeMediaError({
|
||||||
|
currentSrc: 'https://api.example/playlist/42.m3u8',
|
||||||
|
networkState: 3,
|
||||||
|
readyState: 0,
|
||||||
|
error: {code: 4},
|
||||||
|
playbackMode: 'native',
|
||||||
|
nativeHlsSupport: 'maybe',
|
||||||
|
}),
|
||||||
|
'HTMLMediaElement MEDIA_ERR_SRC_NOT_SUPPORTED (4); mode=native; ' +
|
||||||
|
'nativeHlsSupport=maybe; src=https://api.example/playlist/42.m3u8; ' +
|
||||||
|
'networkState=3; readyState=0',
|
||||||
|
);
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user