2023-02-27 03:54:28 +03:00
|
|
|
import React, { FC, useContext, useEffect } from 'react';
|
2022-06-03 00:23:51 +03:00
|
|
|
import { useRecoilState, useRecoilValue } from 'recoil';
|
2022-06-02 23:50:16 +03:00
|
|
|
import { useHotkeys } from 'react-hotkeys-hook';
|
2023-02-09 05:50:58 +03:00
|
|
|
import classNames from 'classnames';
|
2023-03-13 08:52:04 +03:00
|
|
|
import { ErrorBoundary } from 'react-error-boundary';
|
2022-09-07 10:00:28 +03:00
|
|
|
import { VideoJS } from '../VideoJS/VideoJS';
|
2022-09-03 21:38:52 +03:00
|
|
|
import ViewerPing from '../viewer-ping';
|
2022-09-07 10:00:28 +03:00
|
|
|
import { VideoPoster } from '../VideoPoster/VideoPoster';
|
2022-09-03 21:38:52 +03:00
|
|
|
import { getLocalStorage, setLocalStorage } from '../../../utils/localStorage';
|
|
|
|
import { isVideoPlayingAtom, clockSkewAtom } from '../../stores/ClientConfigStore';
|
|
|
|
import PlaybackMetrics from '../metrics/playback';
|
2023-05-21 07:15:25 +03:00
|
|
|
import { createVideoSettingsMenuButton } from '../settings-menu';
|
2022-09-03 21:38:52 +03:00
|
|
|
import LatencyCompensator from '../latencyCompensator';
|
2022-11-20 00:35:37 +03:00
|
|
|
import styles from './OwncastPlayer.module.scss';
|
2023-02-27 03:54:28 +03:00
|
|
|
import { VideoSettingsServiceContext } from '../../../services/video-settings-service';
|
2023-03-13 08:52:04 +03:00
|
|
|
import { ComponentError } from '../../ui/ComponentError/ComponentError';
|
2022-11-20 00:35:37 +03:00
|
|
|
|
2022-05-09 09:42:40 +03:00
|
|
|
const PLAYER_VOLUME = 'owncast_volume';
|
2022-06-20 07:20:24 +03:00
|
|
|
const LATENCY_COMPENSATION_ENABLED = 'latencyCompensatorEnabled';
|
2022-05-09 09:42:40 +03:00
|
|
|
|
|
|
|
const ping = new ViewerPing();
|
2022-06-03 00:23:51 +03:00
|
|
|
let playbackMetrics = null;
|
2022-06-20 07:20:24 +03:00
|
|
|
let latencyCompensator = null;
|
|
|
|
let latencyCompensatorEnabled = false;
|
|
|
|
|
2022-09-07 10:00:28 +03:00
|
|
|
export type OwncastPlayerProps = {
|
2022-05-11 01:36:09 +03:00
|
|
|
source: string;
|
|
|
|
online: boolean;
|
2023-01-01 03:08:54 +03:00
|
|
|
initiallyMuted?: boolean;
|
2023-01-24 06:28:32 +03:00
|
|
|
title: string;
|
2023-02-09 05:50:58 +03:00
|
|
|
className?: string;
|
2023-05-04 21:29:48 +03:00
|
|
|
embedded?: boolean;
|
2022-09-07 10:00:28 +03:00
|
|
|
};
|
2022-05-11 01:36:09 +03:00
|
|
|
|
2023-01-01 03:08:54 +03:00
|
|
|
export const OwncastPlayer: FC<OwncastPlayerProps> = ({
|
|
|
|
source,
|
|
|
|
online,
|
|
|
|
initiallyMuted = false,
|
2023-01-24 06:28:32 +03:00
|
|
|
title,
|
2023-02-09 05:50:58 +03:00
|
|
|
className,
|
2023-05-04 21:29:48 +03:00
|
|
|
embedded = false,
|
2023-01-01 03:08:54 +03:00
|
|
|
}) => {
|
2023-02-27 03:54:28 +03:00
|
|
|
const VideoSettingsService = useContext(VideoSettingsServiceContext);
|
2022-04-27 05:29:13 +03:00
|
|
|
const playerRef = React.useRef(null);
|
2022-05-26 06:38:40 +03:00
|
|
|
const [videoPlaying, setVideoPlaying] = useRecoilState<boolean>(isVideoPlayingAtom);
|
2022-06-03 00:23:51 +03:00
|
|
|
const clockSkew = useRecoilValue<Number>(clockSkewAtom);
|
2022-04-27 05:29:13 +03:00
|
|
|
|
2022-05-09 09:42:40 +03:00
|
|
|
const setSavedVolume = () => {
|
|
|
|
try {
|
|
|
|
playerRef.current.volume(getLocalStorage(PLAYER_VOLUME) || 1);
|
|
|
|
} catch (err) {
|
|
|
|
console.warn(err);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleVolume = () => {
|
|
|
|
setLocalStorage(PLAYER_VOLUME, playerRef.current.muted() ? 0 : playerRef.current.volume());
|
|
|
|
};
|
|
|
|
|
2022-06-02 23:50:16 +03:00
|
|
|
const togglePlayback = () => {
|
|
|
|
if (playerRef.current.paused()) {
|
|
|
|
playerRef.current.play();
|
|
|
|
} else {
|
|
|
|
playerRef.current.pause();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const toggleMute = () => {
|
|
|
|
if (playerRef.current.muted() || playerRef.current.volume() === 0) {
|
|
|
|
playerRef.current.volume(0.7);
|
|
|
|
} else {
|
|
|
|
playerRef.current.volume(0);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const toggleFullScreen = () => {
|
|
|
|
if (playerRef.current.isFullscreen()) {
|
|
|
|
playerRef.current.exitFullscreen();
|
|
|
|
} else {
|
|
|
|
playerRef.current.requestFullscreen();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-06-20 07:20:24 +03:00
|
|
|
const startLatencyCompensator = () => {
|
|
|
|
if (latencyCompensator) {
|
|
|
|
latencyCompensator.stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
latencyCompensatorEnabled = true;
|
|
|
|
|
|
|
|
latencyCompensator = new LatencyCompensator(playerRef.current);
|
|
|
|
latencyCompensator.setClockSkew(clockSkew);
|
|
|
|
latencyCompensator.enable();
|
|
|
|
setLocalStorage(LATENCY_COMPENSATION_ENABLED, true);
|
|
|
|
};
|
|
|
|
|
|
|
|
const stopLatencyCompensator = () => {
|
|
|
|
if (latencyCompensator) {
|
|
|
|
latencyCompensator.disable();
|
|
|
|
}
|
|
|
|
latencyCompensator = null;
|
|
|
|
latencyCompensatorEnabled = false;
|
|
|
|
setLocalStorage(LATENCY_COMPENSATION_ENABLED, false);
|
|
|
|
};
|
|
|
|
|
2023-06-26 19:40:28 +03:00
|
|
|
// Toggle minimized latency mode. Return the new state.
|
2022-06-20 07:20:24 +03:00
|
|
|
const toggleLatencyCompensator = () => {
|
|
|
|
if (latencyCompensatorEnabled) {
|
|
|
|
stopLatencyCompensator();
|
|
|
|
} else {
|
|
|
|
startLatencyCompensator();
|
|
|
|
}
|
2023-06-26 19:40:28 +03:00
|
|
|
return latencyCompensatorEnabled;
|
2022-06-20 07:20:24 +03:00
|
|
|
};
|
|
|
|
|
2022-06-22 01:38:37 +03:00
|
|
|
const setupLatencyCompensator = player => {
|
|
|
|
const tech = player.tech({ IWillNotUseThisInPlugins: true });
|
|
|
|
|
|
|
|
// VHS is required.
|
|
|
|
if (!tech || !tech.vhs) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const latencyCompensatorEnabledSaved = getLocalStorage(LATENCY_COMPENSATION_ENABLED);
|
|
|
|
|
|
|
|
if (latencyCompensatorEnabledSaved === 'true' && tech && tech.vhs) {
|
|
|
|
startLatencyCompensator();
|
|
|
|
} else {
|
|
|
|
stopLatencyCompensator();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const createSettings = async (player, videojs) => {
|
2023-02-27 03:54:28 +03:00
|
|
|
const videoQualities = await VideoSettingsService.getVideoQualities();
|
2022-06-22 01:38:37 +03:00
|
|
|
const menuButton = createVideoSettingsMenuButton(
|
|
|
|
player,
|
|
|
|
videojs,
|
|
|
|
videoQualities,
|
|
|
|
toggleLatencyCompensator,
|
|
|
|
);
|
|
|
|
player.controlBar.addChild(
|
|
|
|
menuButton,
|
|
|
|
{},
|
|
|
|
// eslint-disable-next-line no-underscore-dangle
|
|
|
|
player.controlBar.children_.length - 2,
|
|
|
|
);
|
|
|
|
setupLatencyCompensator(player);
|
|
|
|
};
|
|
|
|
|
|
|
|
const setupAirplay = (player, videojs) => {
|
|
|
|
// eslint-disable-next-line no-prototype-builtins
|
|
|
|
if (window.hasOwnProperty('WebKitPlaybackTargetAvailabilityEvent')) {
|
2022-12-16 08:26:23 +03:00
|
|
|
const VJSButtonClass = videojs.getComponent('Button');
|
|
|
|
|
|
|
|
class ConcreteButtonClass extends VJSButtonClass {
|
2022-06-22 01:38:37 +03:00
|
|
|
constructor() {
|
2022-12-16 08:26:23 +03:00
|
|
|
super(player);
|
|
|
|
}
|
2022-06-22 01:38:37 +03:00
|
|
|
|
2022-12-16 08:26:23 +03:00
|
|
|
// eslint-disable-next-line class-methods-use-this
|
2022-06-22 01:38:37 +03:00
|
|
|
handleClick() {
|
|
|
|
try {
|
|
|
|
const videoElement = document.getElementsByTagName('video')[0];
|
|
|
|
(videoElement as any).webkitShowPlaybackTargetPicker();
|
|
|
|
} catch (e) {
|
|
|
|
console.error(e);
|
|
|
|
}
|
2022-12-16 08:26:23 +03:00
|
|
|
}
|
|
|
|
}
|
2022-06-22 01:38:37 +03:00
|
|
|
|
2022-12-16 08:26:23 +03:00
|
|
|
const ccbc = new ConcreteButtonClass();
|
|
|
|
const concreteButtonInstance = player.controlBar.addChild(ccbc);
|
2022-06-22 01:38:37 +03:00
|
|
|
concreteButtonInstance.addClass('vjs-airplay');
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-06-02 23:50:16 +03:00
|
|
|
// Register keyboard shortcut for the space bar to toggle playback
|
2022-10-09 03:14:34 +03:00
|
|
|
useHotkeys('space', e => {
|
|
|
|
e.preventDefault();
|
|
|
|
togglePlayback();
|
2022-06-02 23:50:16 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
// Register keyboard shortcut for f to toggle full screen
|
|
|
|
useHotkeys('f', toggleFullScreen, {
|
|
|
|
enableOnContentEditable: false,
|
|
|
|
});
|
|
|
|
|
|
|
|
// Register keyboard shortcut for the "m" key to toggle mute
|
|
|
|
useHotkeys('m', toggleMute, {
|
|
|
|
enableOnContentEditable: false,
|
|
|
|
});
|
|
|
|
|
|
|
|
useHotkeys('0', () => playerRef.current.volume(playerRef.current.volume() + 0.1), {
|
|
|
|
enableOnContentEditable: false,
|
|
|
|
});
|
|
|
|
useHotkeys('9', () => playerRef.current.volume(playerRef.current.volume() - 0.1), {
|
|
|
|
enableOnContentEditable: false,
|
|
|
|
});
|
|
|
|
|
2022-04-27 05:29:13 +03:00
|
|
|
const videoJsOptions = {
|
|
|
|
autoplay: false,
|
|
|
|
controls: true,
|
|
|
|
responsive: true,
|
2022-05-03 08:13:36 +03:00
|
|
|
fluid: false,
|
2023-01-21 23:50:30 +03:00
|
|
|
fill: true,
|
2022-10-13 03:53:37 +03:00
|
|
|
playsinline: true,
|
2022-04-27 05:29:13 +03:00
|
|
|
liveui: true,
|
|
|
|
preload: 'auto',
|
2023-01-01 03:08:54 +03:00
|
|
|
muted: initiallyMuted,
|
2022-04-27 05:29:13 +03:00
|
|
|
controlBar: {
|
|
|
|
progressControl: {
|
|
|
|
seekBar: false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
html5: {
|
|
|
|
vhs: {
|
|
|
|
// used to select the lowest bitrate playlist initially. This helps to decrease playback start time. This setting is false by default.
|
|
|
|
enableLowInitialPlaylist: true,
|
|
|
|
experimentalBufferBasedABR: true,
|
|
|
|
useNetworkInformationApi: true,
|
|
|
|
maxPlaylistRetries: 30,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
liveTracker: {
|
|
|
|
trackingThreshold: 0,
|
|
|
|
liveTolerance: 15,
|
|
|
|
},
|
|
|
|
sources: [
|
|
|
|
{
|
2022-05-11 01:36:09 +03:00
|
|
|
src: source,
|
2022-04-27 05:29:13 +03:00
|
|
|
type: 'application/x-mpegURL',
|
|
|
|
},
|
|
|
|
],
|
2023-05-07 03:40:42 +03:00
|
|
|
};
|
2022-04-27 05:29:13 +03:00
|
|
|
|
2022-06-03 00:23:51 +03:00
|
|
|
const handlePlayerReady = (player, videojs) => {
|
2022-04-27 05:29:13 +03:00
|
|
|
playerRef.current = player;
|
2022-05-09 09:42:40 +03:00
|
|
|
setSavedVolume();
|
2022-06-22 01:38:37 +03:00
|
|
|
setupAirplay(player, videojs);
|
2022-06-20 07:20:24 +03:00
|
|
|
|
2022-04-27 05:29:13 +03:00
|
|
|
// You can handle player events here, for example:
|
|
|
|
player.on('waiting', () => {
|
2022-11-13 10:35:53 +03:00
|
|
|
console.debug('player is waiting');
|
2022-04-27 05:29:13 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
player.on('dispose', () => {
|
2022-11-13 10:35:53 +03:00
|
|
|
console.debug('player will dispose');
|
2022-05-09 09:42:40 +03:00
|
|
|
ping.stop();
|
2022-04-27 05:29:13 +03:00
|
|
|
});
|
2022-05-09 09:42:40 +03:00
|
|
|
|
|
|
|
player.on('playing', () => {
|
2022-11-13 10:35:53 +03:00
|
|
|
console.debug('player is playing');
|
2022-05-09 09:42:40 +03:00
|
|
|
ping.start();
|
2022-05-26 06:38:40 +03:00
|
|
|
setVideoPlaying(true);
|
2022-05-09 09:42:40 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
player.on('pause', () => {
|
2022-11-13 10:35:53 +03:00
|
|
|
console.debug('player is paused');
|
2022-05-09 09:42:40 +03:00
|
|
|
ping.stop();
|
2022-05-26 06:38:40 +03:00
|
|
|
setVideoPlaying(false);
|
2022-05-09 09:42:40 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
player.on('ended', () => {
|
2022-11-13 10:35:53 +03:00
|
|
|
console.debug('player is ended');
|
2022-05-09 09:42:40 +03:00
|
|
|
ping.stop();
|
2022-05-26 06:38:40 +03:00
|
|
|
setVideoPlaying(false);
|
2022-05-09 09:42:40 +03:00
|
|
|
});
|
|
|
|
|
2022-06-19 23:49:42 +03:00
|
|
|
videojs.hookOnce();
|
|
|
|
|
2022-05-09 09:42:40 +03:00
|
|
|
player.on('volumechange', handleVolume);
|
2022-06-03 00:23:51 +03:00
|
|
|
|
|
|
|
playbackMetrics = new PlaybackMetrics(player, videojs);
|
|
|
|
playbackMetrics.setClockSkew(clockSkew);
|
2022-06-19 23:49:42 +03:00
|
|
|
|
2022-06-22 01:38:37 +03:00
|
|
|
createSettings(player, videojs);
|
2022-04-27 05:29:13 +03:00
|
|
|
};
|
|
|
|
|
2022-06-03 00:23:51 +03:00
|
|
|
useEffect(() => {
|
|
|
|
if (playbackMetrics) {
|
|
|
|
playbackMetrics.setClockSkew(clockSkew);
|
|
|
|
}
|
|
|
|
}, [clockSkew]);
|
|
|
|
|
2022-10-09 03:51:12 +03:00
|
|
|
useEffect(
|
|
|
|
() => () => {
|
|
|
|
stopLatencyCompensator();
|
2022-11-13 10:35:53 +03:00
|
|
|
playbackMetrics?.stop();
|
2022-10-09 03:51:12 +03:00
|
|
|
},
|
|
|
|
[],
|
|
|
|
);
|
|
|
|
|
2022-05-11 01:36:09 +03:00
|
|
|
return (
|
2023-03-13 08:52:04 +03:00
|
|
|
<ErrorBoundary
|
|
|
|
// eslint-disable-next-line react/no-unstable-nested-components
|
|
|
|
fallbackRender={({ error, resetErrorBoundary }) => (
|
|
|
|
<ComponentError
|
|
|
|
componentName="OwncastPlayer"
|
|
|
|
message={error.message}
|
|
|
|
retryFunction={resetErrorBoundary}
|
|
|
|
/>
|
2022-05-11 01:36:09 +03:00
|
|
|
)}
|
2023-03-13 08:52:04 +03:00
|
|
|
>
|
2023-05-04 21:29:48 +03:00
|
|
|
<div
|
|
|
|
className={classNames(styles.container, className, embedded && styles.embedded)}
|
|
|
|
id="player"
|
|
|
|
>
|
2023-03-13 08:52:04 +03:00
|
|
|
{online && (
|
|
|
|
<div className={styles.player}>
|
|
|
|
<VideoJS options={videoJsOptions} onReady={handlePlayerReady} aria-label={title} />
|
|
|
|
</div>
|
2022-06-30 00:01:41 +03:00
|
|
|
)}
|
2023-03-13 08:52:04 +03:00
|
|
|
<div className={styles.poster}>
|
|
|
|
{!videoPlaying && (
|
|
|
|
<VideoPoster online={online} initialSrc="/thumbnail.jpg" src="/thumbnail.jpg" />
|
|
|
|
)}
|
|
|
|
</div>
|
2022-05-11 01:36:09 +03:00
|
|
|
</div>
|
2023-03-13 08:52:04 +03:00
|
|
|
</ErrorBoundary>
|
2022-05-11 01:36:09 +03:00
|
|
|
);
|
2022-09-07 10:00:28 +03:00
|
|
|
};
|