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
|
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';
|
2021-11-30 21:46:03 +01:00
|
|
|
|
|
|
|
import * as ICONS from 'constants/icons';
|
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';
|
|
|
|
import recsys from './plugins/videojs-recsys/plugin';
|
2021-11-12 15:56:46 +01:00
|
|
|
import runAds from './ads';
|
2021-11-30 21:46:03 +01:00
|
|
|
import videojs from 'video.js';
|
2020-04-16 01:21:17 +02:00
|
|
|
|
2021-12-07 12:14:58 +01:00
|
|
|
require('@silvermine/videojs-chromecast')(videojs);
|
|
|
|
|
2020-04-28 21:33:30 +02:00
|
|
|
export type Player = {
|
2021-11-30 21:46:03 +01:00
|
|
|
controlBar: { addChild: (string, any) => void },
|
|
|
|
loadingSpinner: any,
|
|
|
|
autoplay: (any) => boolean,
|
2021-12-07 12:14:58 +01:00
|
|
|
chromecast: (any) => void,
|
2020-04-30 09:49:52 +02:00
|
|
|
currentTime: (?number) => number,
|
2021-11-30 21:46:03 +01:00
|
|
|
dispose: () => void,
|
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,
|
|
|
|
mobileUi: (any) => void,
|
|
|
|
muted: (?boolean) => boolean,
|
|
|
|
on: (string, (any) => void) => void,
|
|
|
|
one: (string, (any) => void) => void,
|
|
|
|
overlay: (any) => 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,
|
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-09-02 22:05:32 +02:00
|
|
|
autoplaySetting: 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,
|
2021-09-02 22:05:32 +02:00
|
|
|
videoTheaterMode: boolean,
|
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,
|
2020-04-16 01:21:17 +02:00
|
|
|
};
|
|
|
|
|
2020-11-20 10:40:25 +01:00
|
|
|
const videoPlaybackRates = [0.25, 0.5, 0.75, 1, 1.1, 1.25, 1.5, 1.75, 2];
|
|
|
|
|
2020-05-25 16:36:17 +02:00
|
|
|
const IS_IOS =
|
|
|
|
(/iPad|iPhone|iPod/.test(navigator.platform) ||
|
2021-11-23 16:21:33 +01:00
|
|
|
// for iOS 13+ , platform is MacIntel, so use this to test
|
2020-05-25 16:36:17 +02:00
|
|
|
(navigator.platform === 'MacIntel' && navigator.maxTouchPoints > 1)) &&
|
|
|
|
!window.MSStream;
|
|
|
|
|
2021-07-19 18:54:05 +02:00
|
|
|
const VIDEO_JS_OPTIONS = {
|
2020-04-16 23:43:09 +02:00
|
|
|
preload: 'auto',
|
2020-11-20 10:40:25 +01:00
|
|
|
playbackRates: videoPlaybackRates,
|
2020-04-16 23:43:09 +02:00
|
|
|
responsive: true,
|
2020-04-28 21:33:30 +02:00
|
|
|
controls: true,
|
2021-01-14 20:43:16 +01:00
|
|
|
html5: {
|
2021-06-30 18:29:00 +02:00
|
|
|
vhs: {
|
2021-01-20 23:22:49 +01:00
|
|
|
overrideNative: !videojs.browser.IS_ANY_SAFARI,
|
2021-01-14 20:43:16 +01:00
|
|
|
},
|
|
|
|
},
|
2020-04-16 23:43:09 +02:00
|
|
|
};
|
|
|
|
|
2020-04-16 01:21:17 +02:00
|
|
|
if (!Object.keys(videojs.getPlugins()).includes('eventTracking')) {
|
|
|
|
videojs.registerPlugin('eventTracking', eventTracking);
|
|
|
|
}
|
|
|
|
|
2021-01-14 20:43:16 +01:00
|
|
|
if (!Object.keys(videojs.getPlugins()).includes('hlsQualitySelector')) {
|
2021-02-18 03:53:25 +01:00
|
|
|
videojs.registerPlugin('hlsQualitySelector', hlsQualitySelector);
|
2021-01-14 20:43:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!Object.keys(videojs.getPlugins()).includes('qualityLevels')) {
|
|
|
|
videojs.registerPlugin('qualityLevels', qualityLevels);
|
|
|
|
}
|
|
|
|
|
2021-06-29 03:51:04 +02:00
|
|
|
if (!Object.keys(videojs.getPlugins()).includes('recsys')) {
|
|
|
|
videojs.registerPlugin('recsys', recsys);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
allowPreRoll,
|
2021-04-12 18:43:47 +02:00
|
|
|
autoplay,
|
2021-09-02 22:05:32 +02:00
|
|
|
autoplaySetting,
|
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-11-30 21:46:03 +01:00
|
|
|
internalFeatureEnabled, // for people on the team to test new features internally
|
|
|
|
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,
|
2021-09-02 22:05:32 +02:00
|
|
|
videoTheaterMode,
|
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,
|
2021-04-12 18:43:47 +02:00
|
|
|
} = props;
|
2021-01-27 14:49:30 +01:00
|
|
|
|
2021-11-12 15:56:46 +01:00
|
|
|
// will later store the videojs player
|
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();
|
|
|
|
|
|
|
|
// initiate keyboard shortcuts
|
|
|
|
const { curried_function } = keyboardShorcuts({ toggleVideoTheaterMode, playNext, playPrevious });
|
|
|
|
|
|
|
|
const [reload, setReload] = useState('initial');
|
|
|
|
|
2021-01-20 21:10:16 +01:00
|
|
|
const videoJsOptions = {
|
2020-04-16 01:21:17 +02:00
|
|
|
...VIDEO_JS_OPTIONS,
|
2021-01-27 14:49:30 +01:00
|
|
|
autoplay: autoplay,
|
|
|
|
muted: startMuted,
|
2021-11-30 21:46:03 +01:00
|
|
|
sources: [{ src: source, type: sourceType }],
|
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 },
|
2021-06-22 21:24:15 +02:00
|
|
|
// fixes problem of errant CC button showing up on iOS
|
2021-06-25 18:26:00 +02:00
|
|
|
// the true fix here is to fix the m3u8 file, see: https://github.com/lbryio/lbry-desktop/pull/6315
|
2021-11-30 21:46:03 +01:00
|
|
|
controlBar: { subsCapsButton: false },
|
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 || '',
|
|
|
|
},
|
2020-04-16 01:21:17 +02:00
|
|
|
};
|
|
|
|
|
2021-11-12 15:56:46 +01:00
|
|
|
const { detectFileType, createVideoPlayerDOM } = functions({ source, sourceType, videoJsOptions, isAudio });
|
2021-01-23 20:38:55 +01:00
|
|
|
|
2021-11-30 21:46:03 +01:00
|
|
|
const { unmuteAndHideHint, retryVideoAfterFailure, initializeEvents } = events({
|
|
|
|
tapToUnmuteRef,
|
|
|
|
tapToRetryRef,
|
|
|
|
setReload,
|
|
|
|
videoTheaterMode,
|
|
|
|
playerRef,
|
|
|
|
autoplaySetting,
|
|
|
|
replay,
|
|
|
|
});
|
2021-06-30 18:29:00 +02:00
|
|
|
|
2021-01-23 20:38:55 +01:00
|
|
|
// Initialize video.js
|
|
|
|
function initializeVideoPlayer(el) {
|
2021-06-30 18:29:00 +02:00
|
|
|
if (!el) return;
|
2021-01-23 20:38:55 +01:00
|
|
|
|
2021-01-27 17:36:24 +01:00
|
|
|
const vjs = videojs(el, videoJsOptions, () => {
|
|
|
|
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-11-15 16:01:42 +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();
|
|
|
|
|
|
|
|
Chromecast.initialize(player);
|
2021-01-23 20:38:55 +01:00
|
|
|
|
2021-06-30 18:29:00 +02:00
|
|
|
// Add quality selector to player
|
|
|
|
player.hlsQualitySelector({
|
|
|
|
displayCurrentQuality: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
// 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', '');
|
|
|
|
|
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();
|
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
|
|
|
}
|
|
|
|
|
2021-11-12 15:56:46 +01:00
|
|
|
/** instantiate videoJS and dispose of it when done with code **/
|
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(() => {
|
2021-01-23 20:38:55 +01:00
|
|
|
const vjsElement = createVideoPlayerDOM(containerRef.current);
|
2020-04-28 21:33:30 +02:00
|
|
|
|
2021-06-30 18:29:00 +02:00
|
|
|
// Detect source file type via pre-fetch (async)
|
|
|
|
detectFileType().then(() => {
|
|
|
|
// Initialize Video.js
|
|
|
|
const vjsPlayer = initializeVideoPlayer(vjsElement);
|
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));
|
2021-06-30 18:29:00 +02: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
|
|
|
};
|
2021-09-02 22:05:32 +02:00
|
|
|
}, [isAudio, source]);
|
2021-01-04 16:12:46 +01:00
|
|
|
|
2021-01-23 21:28:31 +01:00
|
|
|
// Update video player and reload when source URL changes
|
2021-01-23 20:38:55 +01:00
|
|
|
useEffect(() => {
|
|
|
|
// For some reason the video player is responsible for detecting content type this way
|
2021-02-17 03:05:23 +01:00
|
|
|
fetch(source, { method: 'HEAD', cache: 'no-store' }).then((response) => {
|
2021-06-30 18:29:00 +02:00
|
|
|
let finalType = sourceType;
|
2021-06-11 19:32:56 +02:00
|
|
|
let finalSource = source;
|
2021-06-30 18:29:00 +02:00
|
|
|
|
2021-01-23 20:38:55 +01:00
|
|
|
// override type if we receive an .m3u8 (transcoded mp4)
|
2021-06-30 18:29:00 +02:00
|
|
|
// do we need to check if explicitly redirected
|
|
|
|
// or is checking extension only a safer method
|
2021-01-27 15:40:56 +01:00
|
|
|
if (response && response.redirected && response.url && response.url.endsWith('m3u8')) {
|
2021-06-30 18:29:00 +02:00
|
|
|
finalType = 'application/x-mpegURL';
|
2021-06-11 19:32:56 +02:00
|
|
|
finalSource = response.url;
|
2021-01-23 20:38:55 +01:00
|
|
|
}
|
2021-01-07 04:58:09 +01:00
|
|
|
|
2021-06-30 18:29:00 +02:00
|
|
|
// Modify video source in options
|
|
|
|
videoJsOptions.sources = [
|
|
|
|
{
|
|
|
|
src: finalSource,
|
|
|
|
type: finalType,
|
|
|
|
},
|
|
|
|
];
|
2021-01-07 04:58:09 +01:00
|
|
|
|
2021-06-19 04:55:44 +02:00
|
|
|
// Update player source
|
2021-06-30 18:29:00 +02:00
|
|
|
const player = playerRef.current;
|
|
|
|
if (!player) return;
|
2021-03-02 03:01:05 +01:00
|
|
|
|
|
|
|
// PR #5570: Temp workaround to avoid double Play button until the next re-architecture.
|
|
|
|
if (!player.paused()) {
|
|
|
|
player.bigPlayButton.hide();
|
|
|
|
}
|
2021-01-23 20:38:55 +01:00
|
|
|
});
|
2021-01-27 15:40:56 +01:00
|
|
|
}, [source, reload]);
|
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
|
|
|
});
|