2020-04-16 23:43:09 +02:00
|
|
|
// @flow
|
2021-11-12 15:56:46 +01:00
|
|
|
import 'videojs-contrib-ads'; // must be loaded in this order
|
|
|
|
import 'videojs-ima'; // loads directly after contrib-ads
|
2022-05-18 18:02:02 +02:00
|
|
|
import 'videojs-vtt-thumbnails';
|
2021-07-06 11:02:05 +02:00
|
|
|
import 'video.js/dist/alt/video-js-cdn.min.css';
|
2021-12-02 04:06:22 +01:00
|
|
|
import './plugins/videojs-mobile-ui/plugin';
|
2021-12-07 12:14:58 +01:00
|
|
|
import '@silvermine/videojs-chromecast/dist/silvermine-videojs-chromecast.css';
|
2022-03-25 14:16:44 +01:00
|
|
|
import '@silvermine/videojs-airplay/dist/silvermine-videojs-airplay.css';
|
2021-11-30 21:46:03 +01:00
|
|
|
import * as ICONS from 'constants/icons';
|
2022-05-02 08:06:37 +02:00
|
|
|
import { VIDEO_PLAYBACK_RATES } from 'constants/player';
|
2020-12-14 17:40:59 +01:00
|
|
|
import * as OVERLAY from './overlays';
|
2021-11-30 21:46:03 +01:00
|
|
|
import Button from 'component/button';
|
|
|
|
import classnames from 'classnames';
|
|
|
|
import events from './videojs-events';
|
|
|
|
import eventTracking from 'videojs-event-tracking';
|
|
|
|
import functions from './videojs-functions';
|
2021-02-18 03:53:25 +01:00
|
|
|
import hlsQualitySelector from './plugins/videojs-hls-quality-selector/plugin';
|
2021-11-30 21:46:03 +01:00
|
|
|
import keyboardShorcuts from './videojs-keyboard-shortcuts';
|
|
|
|
import LbryVolumeBarClass from './lbry-volume-bar';
|
2021-12-07 12:14:58 +01:00
|
|
|
import Chromecast from './chromecast';
|
2021-11-30 21:46:03 +01:00
|
|
|
import playerjs from 'player.js';
|
2021-01-14 20:43:16 +01:00
|
|
|
import qualityLevels from 'videojs-contrib-quality-levels';
|
2021-11-30 21:46:03 +01:00
|
|
|
import React, { useEffect, useRef, useState } from 'react';
|
2022-05-17 07:47:35 +02:00
|
|
|
import i18n from './plugins/videojs-i18n/plugin';
|
2021-11-30 21:46:03 +01:00
|
|
|
import recsys from './plugins/videojs-recsys/plugin';
|
2021-12-15 19:59:02 +01:00
|
|
|
// import runAds from './ads';
|
2021-11-30 21:46:03 +01:00
|
|
|
import videojs from 'video.js';
|
2022-03-15 17:18:08 +01:00
|
|
|
import { useIsMobile } from 'effects/use-screensize';
|
2022-04-14 14:04:22 +02:00
|
|
|
import { platform } from 'util/platform';
|
2022-04-25 15:28:36 +02:00
|
|
|
import usePersistedState from 'effects/use-persisted-state';
|
2022-03-15 17:18:08 +01:00
|
|
|
|
2022-01-06 20:28:27 +01:00
|
|
|
const canAutoplay = require('./plugins/canAutoplay');
|
2020-04-16 01:21:17 +02:00
|
|
|
|
2021-12-07 12:14:58 +01:00
|
|
|
require('@silvermine/videojs-chromecast')(videojs);
|
2022-03-25 14:16:44 +01:00
|
|
|
require('@silvermine/videojs-airplay')(videojs);
|
2021-12-07 12:14:58 +01:00
|
|
|
|
2020-04-28 21:33:30 +02:00
|
|
|
export type Player = {
|
2022-04-12 12:02:13 +02:00
|
|
|
// -- custom --
|
|
|
|
claimSrcOriginal: ?{ src: string, type: string },
|
|
|
|
claimSrcVhs: ?{ src: string, type: string },
|
|
|
|
isLivestream?: boolean,
|
2022-05-17 07:47:35 +02:00
|
|
|
// -- plugins ---
|
|
|
|
mobileUi: (any) => void,
|
|
|
|
chromecast: (any) => void,
|
|
|
|
overlay: (any) => void,
|
|
|
|
hlsQualitySelector: ?any,
|
|
|
|
i18n: (any) => void,
|
|
|
|
// -- base videojs --
|
2021-11-30 21:46:03 +01:00
|
|
|
controlBar: { addChild: (string, any) => void },
|
|
|
|
loadingSpinner: any,
|
|
|
|
autoplay: (any) => boolean,
|
2022-04-12 12:02:13 +02:00
|
|
|
tech: (?boolean) => { vhs: ?any },
|
2020-04-30 09:49:52 +02:00
|
|
|
currentTime: (?number) => number,
|
2021-11-30 21:46:03 +01:00
|
|
|
dispose: () => void,
|
2022-02-07 18:51:26 +01:00
|
|
|
duration: () => number,
|
2020-05-15 03:18:54 +02:00
|
|
|
ended: () => boolean,
|
2020-05-21 17:38:28 +02:00
|
|
|
error: () => any,
|
2021-11-30 21:46:03 +01:00
|
|
|
exitFullscreen: () => boolean,
|
2021-02-17 03:05:23 +01:00
|
|
|
getChild: (string) => any,
|
2021-11-30 21:46:03 +01:00
|
|
|
isFullscreen: () => boolean,
|
|
|
|
muted: (?boolean) => boolean,
|
|
|
|
on: (string, (any) => void) => void,
|
|
|
|
one: (string, (any) => void) => void,
|
|
|
|
play: () => Promise<any>,
|
2020-11-20 10:40:25 +01:00
|
|
|
playbackRate: (?number) => number,
|
2021-02-17 18:01:12 +01:00
|
|
|
readyState: () => number,
|
2021-11-30 21:46:03 +01:00
|
|
|
requestFullscreen: () => boolean,
|
2022-04-12 12:02:13 +02:00
|
|
|
src: ({ src: string, type: string }) => ?string,
|
|
|
|
currentSrc: () => string,
|
2020-11-20 10:40:25 +01:00
|
|
|
userActive: (?boolean) => boolean,
|
2021-11-30 21:46:03 +01:00
|
|
|
volume: (?number) => number,
|
2020-04-28 21:33:30 +02:00
|
|
|
};
|
|
|
|
|
2020-04-16 01:21:17 +02:00
|
|
|
type Props = {
|
2021-11-30 21:46:03 +01:00
|
|
|
adUrl: ?string,
|
|
|
|
allowPreRoll: ?boolean,
|
2021-01-27 14:49:30 +01:00
|
|
|
autoplay: boolean,
|
2021-06-29 03:51:04 +02:00
|
|
|
claimId: ?string,
|
2021-12-07 15:26:31 +01:00
|
|
|
title: ?string,
|
|
|
|
channelName: ?string,
|
2021-11-30 21:46:03 +01:00
|
|
|
embedded: boolean,
|
2021-10-13 17:04:03 +02:00
|
|
|
internalFeatureEnabled: ?boolean,
|
2021-11-30 21:46:03 +01:00
|
|
|
isAudio: boolean,
|
|
|
|
poster: ?string,
|
2021-09-02 22:05:32 +02:00
|
|
|
replay: boolean,
|
2021-11-30 21:46:03 +01:00
|
|
|
shareTelemetry: boolean,
|
|
|
|
source: string,
|
|
|
|
sourceType: string,
|
|
|
|
startMuted: boolean,
|
|
|
|
userId: ?number,
|
2022-04-19 18:55:32 +02:00
|
|
|
defaultQuality: ?string,
|
2021-11-30 21:46:03 +01:00
|
|
|
onPlayerReady: (Player, any) => void,
|
2021-09-02 22:05:32 +02:00
|
|
|
playNext: () => void,
|
|
|
|
playPrevious: () => void,
|
2021-11-30 21:46:03 +01:00
|
|
|
toggleVideoTheaterMode: () => void,
|
2022-01-06 20:28:27 +01:00
|
|
|
claimRewards: () => void,
|
|
|
|
doAnalyticsView: (string, number) => void,
|
2022-05-17 16:47:44 +02:00
|
|
|
doAnalyticsBuffer: (string, any) => void,
|
2022-01-06 20:28:27 +01:00
|
|
|
uri: string,
|
|
|
|
claimValues: any,
|
|
|
|
clearPosition: (string) => void,
|
2022-03-15 17:18:08 +01:00
|
|
|
isLivestreamClaim: boolean,
|
|
|
|
userClaimId: ?string,
|
|
|
|
activeLivestreamForChannel: any,
|
2022-04-25 15:28:36 +02:00
|
|
|
doToast: ({ message: string, linkText: string, linkTarget: string }) => void,
|
2020-04-16 01:21:17 +02:00
|
|
|
};
|
|
|
|
|
2022-04-14 14:04:22 +02:00
|
|
|
const IS_IOS = platform.isIOS();
|
2022-05-18 22:50:30 +02:00
|
|
|
const IS_MOBILE = platform.isMobile();
|
2020-05-25 16:36:17 +02:00
|
|
|
|
2022-05-17 07:47:35 +02:00
|
|
|
const PLUGIN_MAP = {
|
|
|
|
eventTracking: eventTracking,
|
|
|
|
hlsQualitySelector: hlsQualitySelector,
|
|
|
|
qualityLevels: qualityLevels,
|
|
|
|
recsys: recsys,
|
|
|
|
i18n: i18n,
|
|
|
|
};
|
2021-01-14 20:43:16 +01:00
|
|
|
|
2022-05-17 07:47:35 +02:00
|
|
|
Object.entries(PLUGIN_MAP).forEach(([pluginName, plugin]) => {
|
|
|
|
if (!Object.keys(videojs.getPlugins()).includes(pluginName)) {
|
|
|
|
videojs.registerPlugin(pluginName, plugin);
|
|
|
|
}
|
|
|
|
});
|
2021-06-29 03:51:04 +02:00
|
|
|
|
2020-12-14 17:40:59 +01:00
|
|
|
// ****************************************************************************
|
|
|
|
// VideoJs
|
|
|
|
// ****************************************************************************
|
2020-11-13 09:02:54 +01:00
|
|
|
|
2020-04-16 01:21:17 +02:00
|
|
|
/*
|
|
|
|
properties for this component should be kept to ONLY those that if changed should REQUIRE an entirely new videojs element
|
|
|
|
*/
|
2020-04-28 21:33:30 +02:00
|
|
|
export default React.memo<Props>(function VideoJs(props: Props) {
|
2021-04-12 18:43:47 +02:00
|
|
|
const {
|
2021-11-30 21:46:03 +01:00
|
|
|
// adUrl, // TODO: this ad functionality isn't used, can be pulled out
|
2021-12-15 19:59:02 +01:00
|
|
|
// allowPreRoll,
|
2021-04-12 18:43:47 +02:00
|
|
|
autoplay,
|
2021-11-30 21:46:03 +01:00
|
|
|
claimId,
|
2021-12-07 15:26:31 +01:00
|
|
|
title,
|
|
|
|
channelName,
|
2021-09-03 00:39:40 +02:00
|
|
|
embedded,
|
2021-12-15 19:59:02 +01:00
|
|
|
// internalFeatureEnabled, // for people on the team to test new features internally
|
2021-11-30 21:46:03 +01:00
|
|
|
isAudio,
|
|
|
|
poster,
|
|
|
|
replay,
|
|
|
|
shareTelemetry,
|
2021-04-12 18:43:47 +02:00
|
|
|
source,
|
|
|
|
sourceType,
|
2021-11-30 21:46:03 +01:00
|
|
|
startMuted,
|
2021-06-29 03:51:04 +02:00
|
|
|
userId,
|
2022-04-19 18:55:32 +02:00
|
|
|
defaultQuality,
|
2021-11-30 21:46:03 +01:00
|
|
|
onPlayerReady,
|
2021-09-02 22:05:32 +02:00
|
|
|
playNext,
|
|
|
|
playPrevious,
|
2021-11-30 21:46:03 +01:00
|
|
|
toggleVideoTheaterMode,
|
2022-01-06 20:28:27 +01:00
|
|
|
claimValues,
|
|
|
|
doAnalyticsView,
|
2022-05-17 16:47:44 +02:00
|
|
|
doAnalyticsBuffer,
|
2022-01-06 20:28:27 +01:00
|
|
|
claimRewards,
|
|
|
|
uri,
|
|
|
|
clearPosition,
|
2022-03-15 17:18:08 +01:00
|
|
|
userClaimId,
|
|
|
|
isLivestreamClaim,
|
|
|
|
activeLivestreamForChannel,
|
2022-04-25 15:28:36 +02:00
|
|
|
doToast,
|
2021-04-12 18:43:47 +02:00
|
|
|
} = props;
|
2021-01-27 14:49:30 +01:00
|
|
|
|
2022-04-25 15:28:36 +02:00
|
|
|
// used to notify about default quality setting
|
2022-04-25 19:54:33 +02:00
|
|
|
// if already has a quality set, no need to notify
|
|
|
|
const [initialQualityChange, setInitialQualityChange] = usePersistedState(
|
|
|
|
'initial-quality-change',
|
|
|
|
Boolean(defaultQuality)
|
|
|
|
);
|
2022-04-25 15:28:36 +02:00
|
|
|
|
2022-03-15 17:18:08 +01:00
|
|
|
const isMobile = useIsMobile();
|
|
|
|
|
2021-01-27 17:36:24 +01:00
|
|
|
const playerRef = useRef();
|
2020-04-16 23:43:09 +02:00
|
|
|
const containerRef = useRef();
|
2021-11-12 15:56:46 +01:00
|
|
|
const tapToUnmuteRef = useRef();
|
|
|
|
const tapToRetryRef = useRef();
|
2022-01-06 20:28:27 +01:00
|
|
|
const playerServerRef = useRef();
|
|
|
|
|
2022-03-25 20:34:39 +01:00
|
|
|
const { url: livestreamVideoUrl } = activeLivestreamForChannel || {};
|
2022-04-25 08:41:20 +02:00
|
|
|
const overrideNativeVhs = !platform.isIPhone();
|
2022-05-12 17:48:21 +02:00
|
|
|
const showQualitySelector = (!isLivestreamClaim && overrideNativeVhs) || livestreamVideoUrl;
|
2022-03-15 17:18:08 +01:00
|
|
|
|
2021-11-12 15:56:46 +01:00
|
|
|
// initiate keyboard shortcuts
|
2022-03-15 17:18:08 +01:00
|
|
|
const { curried_function } = keyboardShorcuts({
|
|
|
|
isMobile,
|
|
|
|
isLivestreamClaim,
|
|
|
|
toggleVideoTheaterMode,
|
|
|
|
playNext,
|
|
|
|
playPrevious,
|
|
|
|
});
|
2021-11-12 15:56:46 +01:00
|
|
|
|
|
|
|
const [reload, setReload] = useState('initial');
|
|
|
|
|
2022-01-06 20:28:27 +01:00
|
|
|
const { createVideoPlayerDOM } = functions({ isAudio });
|
|
|
|
|
|
|
|
const { unmuteAndHideHint, retryVideoAfterFailure, initializeEvents } = events({
|
|
|
|
tapToUnmuteRef,
|
|
|
|
tapToRetryRef,
|
|
|
|
setReload,
|
|
|
|
playerRef,
|
|
|
|
replay,
|
|
|
|
claimValues,
|
|
|
|
userId,
|
|
|
|
claimId,
|
|
|
|
embedded,
|
|
|
|
doAnalyticsView,
|
2022-05-17 16:47:44 +02:00
|
|
|
doAnalyticsBuffer,
|
2022-01-06 20:28:27 +01:00
|
|
|
claimRewards,
|
|
|
|
uri,
|
|
|
|
playerServerRef,
|
|
|
|
clearPosition,
|
2022-03-15 17:18:08 +01:00
|
|
|
isLivestreamClaim,
|
2022-01-06 20:28:27 +01:00
|
|
|
});
|
|
|
|
|
2021-01-20 21:10:16 +01:00
|
|
|
const videoJsOptions = {
|
2022-01-06 20:28:27 +01:00
|
|
|
preload: 'auto',
|
2022-05-02 08:06:37 +02:00
|
|
|
playbackRates: VIDEO_PLAYBACK_RATES,
|
2022-01-06 20:28:27 +01:00
|
|
|
responsive: true,
|
|
|
|
controls: true,
|
|
|
|
html5: {
|
2022-03-16 03:45:16 +01:00
|
|
|
vhs: {
|
2022-04-25 08:41:20 +02:00
|
|
|
overrideNative: overrideNativeVhs, // !videojs.browser.IS_ANY_SAFARI,
|
2022-03-15 17:18:08 +01:00
|
|
|
enableLowInitialPlaylist: false,
|
2022-03-16 03:45:16 +01:00
|
|
|
fastQualityChange: true,
|
2022-04-20 21:48:45 +02:00
|
|
|
useDtsForTimestampOffset: true,
|
2022-01-06 20:28:27 +01:00
|
|
|
},
|
|
|
|
},
|
2022-03-15 17:18:08 +01:00
|
|
|
liveTracker: {
|
|
|
|
trackingThreshold: 0,
|
|
|
|
liveTolerance: 10,
|
|
|
|
},
|
|
|
|
inactivityTimeout: 2000,
|
2021-01-27 14:49:30 +01:00
|
|
|
autoplay: autoplay,
|
|
|
|
muted: startMuted,
|
2020-04-16 01:21:17 +02:00
|
|
|
poster: poster, // thumb looks bad in app, and if autoplay, flashing poster is annoying
|
2021-11-30 21:46:03 +01:00
|
|
|
plugins: { eventTracking: true, overlay: OVERLAY.OVERLAY_DATA },
|
2022-03-15 17:18:08 +01:00
|
|
|
controlBar: {
|
|
|
|
currentTimeDisplay: !isLivestreamClaim,
|
|
|
|
timeDivider: !isLivestreamClaim,
|
|
|
|
durationDisplay: !isLivestreamClaim,
|
|
|
|
remainingTimeDisplay: !isLivestreamClaim,
|
|
|
|
},
|
2021-12-07 12:14:58 +01:00
|
|
|
techOrder: ['chromecast', 'html5'],
|
2021-12-07 15:26:31 +01:00
|
|
|
chromecast: {
|
|
|
|
requestTitleFn: (src) => title || '',
|
|
|
|
requestSubtitleFn: (src) => channelName || '',
|
|
|
|
},
|
2022-01-06 20:28:27 +01:00
|
|
|
bigPlayButton: embedded, // only show big play button if embedded
|
2022-03-23 05:19:53 +01:00
|
|
|
liveui: isLivestreamClaim,
|
2022-03-15 17:18:08 +01:00
|
|
|
suppressNotSupportedError: true,
|
2020-04-16 01:21:17 +02:00
|
|
|
};
|
|
|
|
|
2021-01-23 20:38:55 +01:00
|
|
|
// Initialize video.js
|
2022-01-06 20:28:27 +01:00
|
|
|
function initializeVideoPlayer(el, canAutoplayVideo) {
|
2021-06-30 18:29:00 +02:00
|
|
|
if (!el) return;
|
2021-01-23 20:38:55 +01:00
|
|
|
|
2022-01-06 20:28:27 +01:00
|
|
|
const vjs = videojs(el, videoJsOptions, async () => {
|
2021-01-27 17:36:24 +01:00
|
|
|
const player = playerRef.current;
|
2021-11-30 21:46:03 +01:00
|
|
|
const adapter = new playerjs.VideoJSAdapter(player);
|
2021-01-27 17:36:24 +01:00
|
|
|
|
2021-01-23 20:38:55 +01:00
|
|
|
// this seems like a weird thing to have to check for here
|
2021-06-30 18:29:00 +02:00
|
|
|
if (!player) return;
|
2021-01-26 00:50:11 +01:00
|
|
|
|
2021-12-15 19:29:06 +01:00
|
|
|
// runAds(internalFeatureEnabled, allowPreRoll, player, embedded);
|
2021-10-13 17:04:03 +02:00
|
|
|
|
2021-11-12 15:56:46 +01:00
|
|
|
initializeEvents();
|
2021-01-23 20:38:55 +01:00
|
|
|
|
|
|
|
// Replace volume bar with custom LBRY volume bar
|
|
|
|
LbryVolumeBarClass.replaceExisting(player);
|
2021-01-26 00:50:11 +01:00
|
|
|
|
2021-06-30 18:29:00 +02:00
|
|
|
// Add reloadSourceOnError plugin
|
|
|
|
player.reloadSourceOnError({ errorInterval: 10 });
|
|
|
|
|
2021-12-07 12:14:58 +01:00
|
|
|
// Initialize mobile UI.
|
|
|
|
player.mobileUi();
|
|
|
|
|
2022-05-17 07:47:35 +02:00
|
|
|
player.i18n();
|
|
|
|
|
2022-01-06 20:28:27 +01:00
|
|
|
if (!embedded) {
|
|
|
|
window.player.bigPlayButton && window.player.bigPlayButton.hide();
|
|
|
|
} else {
|
|
|
|
const bigPlayButton = document.querySelector('.vjs-big-play-button');
|
|
|
|
if (bigPlayButton) bigPlayButton.style.setProperty('display', 'block', 'important');
|
|
|
|
}
|
|
|
|
|
2021-06-30 18:29:00 +02:00
|
|
|
// Add quality selector to player
|
2022-04-11 12:20:50 +02:00
|
|
|
if (showQualitySelector) {
|
|
|
|
player.hlsQualitySelector({
|
|
|
|
displayCurrentQuality: true,
|
|
|
|
originalHeight: claimValues?.video?.height,
|
2022-04-19 18:55:32 +02:00
|
|
|
defaultQuality,
|
2022-04-25 15:28:36 +02:00
|
|
|
initialQualityChange,
|
|
|
|
setInitialQualityChange,
|
|
|
|
doToast,
|
2022-04-11 12:20:50 +02:00
|
|
|
});
|
|
|
|
}
|
2021-06-30 18:29:00 +02:00
|
|
|
|
|
|
|
// Add recsys plugin
|
2021-07-31 05:55:02 +02:00
|
|
|
if (shareTelemetry) {
|
|
|
|
player.recsys({
|
|
|
|
videoId: claimId,
|
|
|
|
userId: userId,
|
2021-09-03 00:39:40 +02:00
|
|
|
embedded: embedded,
|
2021-07-31 05:55:02 +02:00
|
|
|
});
|
|
|
|
}
|
2021-06-30 18:29:00 +02:00
|
|
|
|
|
|
|
// set playsinline for mobile
|
|
|
|
player.children_[0].setAttribute('playsinline', '');
|
|
|
|
|
2022-01-06 20:28:27 +01:00
|
|
|
if (canAutoplayVideo === true) {
|
|
|
|
// show waiting spinner as video is loading
|
|
|
|
player.addClass('vjs-waiting');
|
|
|
|
// document.querySelector('.vjs-big-play-button').style.setProperty('display', 'none', 'important');
|
|
|
|
} else {
|
|
|
|
// $FlowFixMe
|
|
|
|
document.querySelector('.vjs-big-play-button').style.setProperty('display', 'block', 'important');
|
|
|
|
}
|
|
|
|
|
2021-01-23 20:38:55 +01:00
|
|
|
// I think this is a callback function
|
2021-09-02 22:05:32 +02:00
|
|
|
const videoNode = containerRef.current && containerRef.current.querySelector('video, audio');
|
2021-10-13 17:04:03 +02:00
|
|
|
|
2021-09-02 22:05:32 +02:00
|
|
|
onPlayerReady(player, videoNode);
|
2021-11-30 21:46:03 +01:00
|
|
|
adapter.ready();
|
2022-01-06 20:28:27 +01:00
|
|
|
|
|
|
|
// sometimes video doesnt start properly, this addresses the edge case
|
|
|
|
if (autoplay) {
|
|
|
|
const videoDiv = window.player.children_[0];
|
|
|
|
if (videoDiv) {
|
|
|
|
videoDiv.click();
|
|
|
|
}
|
|
|
|
window.player.userActive(true);
|
|
|
|
}
|
2022-03-15 17:18:08 +01:00
|
|
|
|
|
|
|
Chromecast.initialize(player);
|
2022-03-25 14:16:44 +01:00
|
|
|
player.airPlay();
|
2021-01-23 20:38:55 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
// fixes #3498 (https://github.com/lbryio/lbry-desktop/issues/3498)
|
|
|
|
// summary: on firefox the focus would stick to the fullscreen button which caused buggy behavior with spacebar
|
2021-01-27 17:36:24 +01:00
|
|
|
vjs.on('fullscreenchange', () => document.activeElement && document.activeElement.blur());
|
2021-01-23 20:38:55 +01:00
|
|
|
|
2021-01-27 17:36:24 +01:00
|
|
|
return vjs;
|
2021-01-23 20:38:55 +01:00
|
|
|
}
|
|
|
|
|
2022-04-13 17:30:19 +02:00
|
|
|
// useEffect(() => {
|
|
|
|
// if (showQualitySelector) {
|
|
|
|
// // Add quality selector to player
|
|
|
|
// const player = playerRef.current;
|
|
|
|
// if (player) player.hlsQualitySelector({ displayCurrentQuality: true });
|
|
|
|
// }
|
|
|
|
// }, [showQualitySelector]);
|
2022-03-15 17:18:08 +01:00
|
|
|
|
2021-09-02 22:05:32 +02:00
|
|
|
// This lifecycle hook is only called once (on mount), or when `isAudio` or `source` changes.
|
2020-04-16 23:43:09 +02:00
|
|
|
useEffect(() => {
|
2022-02-07 18:51:26 +01:00
|
|
|
(async function () {
|
2022-01-06 22:20:42 +01:00
|
|
|
let canAutoplayVideo = await canAutoplay.video({ timeout: 2000, inline: true });
|
2022-01-06 20:28:27 +01:00
|
|
|
canAutoplayVideo = canAutoplayVideo.result === true;
|
|
|
|
|
|
|
|
const vjsElement = createVideoPlayerDOM(containerRef.current);
|
|
|
|
const vjsPlayer = initializeVideoPlayer(vjsElement, canAutoplayVideo);
|
2022-04-12 12:01:12 +02:00
|
|
|
if (!vjsPlayer) {
|
|
|
|
return;
|
|
|
|
}
|
2021-01-26 00:50:11 +01:00
|
|
|
|
2021-06-30 18:29:00 +02:00
|
|
|
// Add reference to player to global scope
|
|
|
|
window.player = vjsPlayer;
|
2021-01-27 17:36:24 +01:00
|
|
|
|
2021-06-30 18:29:00 +02:00
|
|
|
// Set reference in component state
|
|
|
|
playerRef.current = vjsPlayer;
|
|
|
|
|
2021-11-12 15:56:46 +01:00
|
|
|
window.addEventListener('keydown', curried_function(playerRef, containerRef));
|
2022-01-06 20:28:27 +01:00
|
|
|
|
2022-03-16 12:35:58 +01:00
|
|
|
const controlBar = document.querySelector('.vjs-control-bar');
|
2022-04-12 12:01:12 +02:00
|
|
|
if (controlBar) {
|
|
|
|
controlBar.style.setProperty('opacity', '1', 'important');
|
|
|
|
}
|
2022-01-06 20:28:27 +01:00
|
|
|
|
2022-03-15 17:18:08 +01:00
|
|
|
if (isLivestreamClaim && userClaimId) {
|
2022-04-12 12:02:13 +02:00
|
|
|
vjsPlayer.isLivestream = true;
|
2022-03-15 17:18:08 +01:00
|
|
|
vjsPlayer.addClass('livestreamPlayer');
|
2022-04-12 12:02:13 +02:00
|
|
|
vjsPlayer.src({ type: 'application/x-mpegURL', src: livestreamVideoUrl });
|
2022-01-06 20:28:27 +01:00
|
|
|
} else {
|
2022-04-12 12:02:13 +02:00
|
|
|
vjsPlayer.isLivestream = false;
|
2022-03-15 17:18:08 +01:00
|
|
|
vjsPlayer.removeClass('livestreamPlayer');
|
|
|
|
|
|
|
|
// change to m3u8 if applicable
|
|
|
|
const response = await fetch(source, { method: 'HEAD', cache: 'no-store' });
|
|
|
|
playerServerRef.current = response.headers.get('x-powered-by');
|
2022-04-12 12:02:13 +02:00
|
|
|
vjsPlayer.claimSrcOriginal = { type: sourceType, src: source };
|
2022-03-15 17:18:08 +01:00
|
|
|
|
|
|
|
if (response && response.redirected && response.url && response.url.endsWith('m3u8')) {
|
2022-04-12 12:02:13 +02:00
|
|
|
vjsPlayer.claimSrcVhs = { type: 'application/x-mpegURL', src: response.url };
|
|
|
|
vjsPlayer.src(vjsPlayer.claimSrcVhs);
|
2022-05-18 18:02:02 +02:00
|
|
|
|
|
|
|
const trimmedPath = response.url.substring(0, response.url.lastIndexOf('/'));
|
|
|
|
const thumbnailPath = trimmedPath + '/stream_sprite.vtt';
|
|
|
|
|
2022-05-18 22:50:30 +02:00
|
|
|
// disable thumbnails on mobile for now
|
|
|
|
if (!IS_MOBILE) {
|
|
|
|
vjsPlayer.vttThumbnails({
|
|
|
|
src: thumbnailPath,
|
|
|
|
showTimestamp: true,
|
|
|
|
});
|
|
|
|
}
|
2022-03-15 17:18:08 +01:00
|
|
|
} else {
|
2022-04-12 12:02:13 +02:00
|
|
|
vjsPlayer.src(vjsPlayer.claimSrcOriginal);
|
2022-03-15 17:18:08 +01:00
|
|
|
}
|
2022-01-06 20:28:27 +01:00
|
|
|
}
|
2022-03-15 17:18:08 +01:00
|
|
|
|
2022-01-06 20:28:27 +01:00
|
|
|
vjsPlayer.load();
|
2022-01-10 22:21:55 +01:00
|
|
|
|
2022-04-12 12:01:12 +02:00
|
|
|
// fix invisible vidcrunch overlay on IOS << TODO: does not belong here. Move to ads.jsx (#739)
|
2022-01-10 22:21:55 +01:00
|
|
|
if (IS_IOS) {
|
|
|
|
// ads video player
|
|
|
|
const adsClaimDiv = document.querySelector('.ads__claim-item');
|
|
|
|
|
|
|
|
if (adsClaimDiv) {
|
|
|
|
// hide ad video by default
|
|
|
|
adsClaimDiv.style.display = 'none';
|
|
|
|
|
|
|
|
// ad containing div, we can keep part on page
|
|
|
|
const adsClaimParentDiv = adsClaimDiv.parentNode;
|
|
|
|
|
|
|
|
// watch parent div for when it is on viewport
|
2022-02-07 18:51:26 +01:00
|
|
|
const observer = new IntersectionObserver(function (entries) {
|
2022-01-10 22:21:55 +01:00
|
|
|
// when ad div parent becomes visible by 1px, show the ad video
|
|
|
|
if (entries[0].isIntersecting === true) {
|
|
|
|
adsClaimDiv.style.display = 'block';
|
|
|
|
}
|
|
|
|
|
|
|
|
observer.disconnect();
|
|
|
|
});
|
|
|
|
|
|
|
|
// $FlowFixMe
|
|
|
|
observer.observe(adsClaimParentDiv);
|
|
|
|
}
|
|
|
|
}
|
2022-01-06 20:28:27 +01:00
|
|
|
})();
|
2020-04-16 23:43:09 +02:00
|
|
|
|
2021-01-23 20:38:55 +01:00
|
|
|
// Cleanup
|
|
|
|
return () => {
|
2021-11-12 15:56:46 +01:00
|
|
|
window.removeEventListener('keydown', curried_function);
|
2020-04-16 23:43:09 +02:00
|
|
|
|
2021-01-27 17:36:24 +01:00
|
|
|
const player = playerRef.current;
|
2021-01-23 20:38:55 +01:00
|
|
|
if (player) {
|
2021-12-10 01:57:26 +01:00
|
|
|
try {
|
|
|
|
window.cast.framework.CastContext.getInstance().getCurrentSession().endSession(false);
|
|
|
|
} catch {}
|
|
|
|
|
2021-01-27 17:36:24 +01:00
|
|
|
player.dispose();
|
2021-08-19 01:30:07 +02:00
|
|
|
window.player = undefined;
|
2021-01-23 20:38:55 +01:00
|
|
|
}
|
2021-01-26 00:50:11 +01:00
|
|
|
};
|
2022-03-15 17:18:08 +01:00
|
|
|
}, [isAudio, source, reload, userClaimId, isLivestreamClaim]);
|
2020-04-16 01:21:17 +02:00
|
|
|
|
2020-06-10 15:01:54 +02:00
|
|
|
return (
|
2021-01-27 17:29:18 +01:00
|
|
|
<div className={classnames('video-js-parent', { 'video-js-parent--ios': IS_IOS })} ref={containerRef}>
|
|
|
|
<Button
|
|
|
|
label={__('Tap to unmute')}
|
|
|
|
button="link"
|
|
|
|
icon={ICONS.VOLUME_MUTED}
|
|
|
|
className="video-js--tap-to-unmute"
|
|
|
|
onClick={unmuteAndHideHint}
|
|
|
|
ref={tapToUnmuteRef}
|
|
|
|
/>
|
|
|
|
<Button
|
|
|
|
label={__('Retry')}
|
|
|
|
button="link"
|
|
|
|
icon={ICONS.REFRESH}
|
|
|
|
className="video-js--tap-to-unmute"
|
|
|
|
onClick={retryVideoAfterFailure}
|
|
|
|
ref={tapToRetryRef}
|
|
|
|
/>
|
|
|
|
</div>
|
2020-06-10 15:01:54 +02:00
|
|
|
);
|
2020-04-16 18:10:47 +02:00
|
|
|
});
|