2020-04-16 23:43:09 +02:00
|
|
|
// @flow
|
2020-07-15 13:20:27 +02:00
|
|
|
import React, { useEffect, useRef, useState } from 'react';
|
2020-06-10 15:01:54 +02:00
|
|
|
import Button from 'component/button';
|
|
|
|
import * as ICONS from 'constants/icons';
|
2020-04-28 21:33:30 +02:00
|
|
|
import classnames from 'classnames';
|
2020-04-16 01:21:17 +02:00
|
|
|
import videojs from 'video.js/dist/alt/video.core.novtt.min.js';
|
|
|
|
import 'video.js/dist/alt/video-js-cdn.min.css';
|
|
|
|
import eventTracking from 'videojs-event-tracking';
|
2020-04-16 23:43:09 +02:00
|
|
|
import isUserTyping from 'util/detect-typing';
|
2020-11-02 18:00:49 +01:00
|
|
|
// import './adstest.js';
|
|
|
|
// import './adstest2.js';
|
|
|
|
// import './adstest.css';
|
2020-10-30 05:19:05 +01:00
|
|
|
|
2020-07-15 13:20:27 +02:00
|
|
|
const isDev = process.env.NODE_ENV !== 'production';
|
2020-04-16 01:21:17 +02:00
|
|
|
|
2020-04-28 21:33:30 +02:00
|
|
|
export type Player = {
|
|
|
|
on: (string, (any) => void) => void,
|
2020-06-10 15:01:54 +02:00
|
|
|
one: (string, (any) => void) => void,
|
2020-04-28 21:33:30 +02:00
|
|
|
isFullscreen: () => boolean,
|
|
|
|
exitFullscreen: () => boolean,
|
|
|
|
requestFullscreen: () => boolean,
|
|
|
|
play: () => Promise<any>,
|
|
|
|
volume: (?number) => number,
|
|
|
|
muted: (?boolean) => boolean,
|
|
|
|
dispose: () => void,
|
2020-04-30 09:49:52 +02:00
|
|
|
currentTime: (?number) => number,
|
2020-05-15 03:18:54 +02:00
|
|
|
ended: () => boolean,
|
2020-05-21 17:38:28 +02:00
|
|
|
error: () => any,
|
2020-07-15 10:50:25 +02:00
|
|
|
loadingSpinner: any,
|
2020-04-28 21:33:30 +02:00
|
|
|
};
|
|
|
|
|
2020-04-16 01:21:17 +02:00
|
|
|
type Props = {
|
|
|
|
source: string,
|
|
|
|
sourceType: string,
|
2020-04-28 21:33:30 +02:00
|
|
|
poster: ?string,
|
|
|
|
onPlayerReady: Player => void,
|
2020-04-16 01:21:17 +02:00
|
|
|
isAudio: boolean,
|
2020-04-24 22:40:31 +02:00
|
|
|
startMuted: boolean,
|
2020-10-30 05:19:05 +01:00
|
|
|
adsTest?: boolean,
|
2020-04-16 01:21:17 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
type VideoJSOptions = {
|
|
|
|
controls: boolean,
|
|
|
|
preload: string,
|
|
|
|
playbackRates: Array<number>,
|
|
|
|
responsive: boolean,
|
|
|
|
poster?: string,
|
|
|
|
muted?: boolean,
|
|
|
|
};
|
|
|
|
|
2020-05-25 16:36:17 +02:00
|
|
|
const IS_IOS =
|
|
|
|
(/iPad|iPhone|iPod/.test(navigator.platform) ||
|
|
|
|
(navigator.platform === 'MacIntel' && navigator.maxTouchPoints > 1)) &&
|
|
|
|
!window.MSStream;
|
|
|
|
|
2020-04-16 23:43:09 +02:00
|
|
|
const VIDEO_JS_OPTIONS: VideoJSOptions = {
|
|
|
|
preload: 'auto',
|
|
|
|
playbackRates: [0.25, 0.5, 0.75, 1, 1.1, 1.25, 1.5, 1.75, 2],
|
|
|
|
responsive: true,
|
2020-04-28 21:33:30 +02:00
|
|
|
controls: true,
|
2020-04-16 23:43:09 +02:00
|
|
|
};
|
|
|
|
|
2020-04-16 01:21:17 +02:00
|
|
|
const F11_KEYCODE = 122;
|
|
|
|
const SPACE_BAR_KEYCODE = 32;
|
|
|
|
const SMALL_F_KEYCODE = 70;
|
|
|
|
const SMALL_M_KEYCODE = 77;
|
|
|
|
const ARROW_LEFT_KEYCODE = 37;
|
|
|
|
const ARROW_RIGHT_KEYCODE = 39;
|
|
|
|
|
|
|
|
const FULLSCREEN_KEYCODE = SMALL_F_KEYCODE;
|
|
|
|
const MUTE_KEYCODE = SMALL_M_KEYCODE;
|
|
|
|
|
|
|
|
const SEEK_FORWARD_KEYCODE = ARROW_RIGHT_KEYCODE;
|
|
|
|
const SEEK_BACKWARD_KEYCODE = ARROW_LEFT_KEYCODE;
|
|
|
|
|
|
|
|
const SEEK_STEP = 10; // time to seek in seconds
|
|
|
|
|
|
|
|
if (!Object.keys(videojs.getPlugins()).includes('eventTracking')) {
|
|
|
|
videojs.registerPlugin('eventTracking', eventTracking);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
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) {
|
2020-11-02 18:00:49 +01:00
|
|
|
const { startMuted, source, sourceType, poster, isAudio, onPlayerReady } = props;
|
2020-07-15 13:20:27 +02:00
|
|
|
const [reload, setReload] = useState('initial');
|
2020-10-30 05:19:05 +01:00
|
|
|
|
2020-04-28 21:33:30 +02:00
|
|
|
let player: ?Player;
|
2020-04-16 23:43:09 +02:00
|
|
|
const containerRef = useRef();
|
2020-04-16 01:21:17 +02:00
|
|
|
const videoJsOptions = {
|
|
|
|
...VIDEO_JS_OPTIONS,
|
|
|
|
sources: [
|
|
|
|
{
|
|
|
|
src: source,
|
|
|
|
type: sourceType,
|
|
|
|
},
|
|
|
|
],
|
2020-04-26 22:32:39 +02:00
|
|
|
autoplay: false,
|
2020-04-16 01:21:17 +02:00
|
|
|
poster: poster, // thumb looks bad in app, and if autoplay, flashing poster is annoying
|
|
|
|
plugins: { eventTracking: true },
|
2020-10-29 19:33:38 +01:00
|
|
|
'webkit-playsinline': IS_IOS,
|
|
|
|
playsinline: IS_IOS,
|
2020-04-16 01:21:17 +02:00
|
|
|
};
|
|
|
|
|
2020-11-02 18:00:49 +01:00
|
|
|
// if (adsTest) {
|
|
|
|
// videoJsOptions.sources = [
|
|
|
|
// {
|
|
|
|
// src:
|
|
|
|
// 'https://cdn.lbryplayer.xyz/api/v3/streams/free/ted-cruz-obliterates-jack-dorsey/9c1d2dec8fd668a79966da4218b2c4d850f7e3c6/bd9c0e',
|
|
|
|
// type: 'video/mp4',
|
|
|
|
// },
|
|
|
|
// ];
|
|
|
|
// // videoJsOptions.plugins.vastClient = {
|
|
|
|
// // adTagUrl: 'https://rozamimo9za10.com/ceef/gdt3g0/tbt/1794126/tlk.xml',
|
|
|
|
// // adsCancelTimeout: 5000,
|
|
|
|
// // adsEnabled: true,
|
|
|
|
// // };
|
|
|
|
// }
|
2020-10-30 05:19:05 +01:00
|
|
|
|
2020-04-24 22:40:31 +02:00
|
|
|
videoJsOptions.muted = startMuted;
|
2020-04-16 01:21:17 +02:00
|
|
|
|
2020-06-10 15:01:54 +02:00
|
|
|
const tapToUnmuteRef = useRef();
|
2020-07-15 13:20:27 +02:00
|
|
|
const tapToRetryRef = useRef();
|
|
|
|
|
|
|
|
const TAP = {
|
2020-07-15 17:51:05 +02:00
|
|
|
NONE: 'NONE',
|
2020-07-15 13:20:27 +02:00
|
|
|
UNMUTE: 'UNMUTE',
|
|
|
|
RETRY: 'RETRY',
|
|
|
|
};
|
|
|
|
|
2020-07-15 17:51:05 +02:00
|
|
|
function showTapButton(tapButton) {
|
|
|
|
const setButtonVisibility = (theRef, newState) => {
|
|
|
|
// Use the DOM to control the state of the button to prevent re-renders.
|
|
|
|
if (theRef.current) {
|
|
|
|
const curState = theRef.current.style.visibility === 'visible';
|
|
|
|
if (newState !== curState) {
|
|
|
|
theRef.current.style.visibility = newState ? 'visible' : 'hidden';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-07-15 13:20:27 +02:00
|
|
|
switch (tapButton) {
|
2020-07-15 17:51:05 +02:00
|
|
|
case TAP.NONE:
|
|
|
|
setButtonVisibility(tapToUnmuteRef, false);
|
|
|
|
setButtonVisibility(tapToRetryRef, false);
|
|
|
|
break;
|
2020-07-15 13:20:27 +02:00
|
|
|
case TAP.UNMUTE:
|
2020-07-15 17:51:05 +02:00
|
|
|
setButtonVisibility(tapToUnmuteRef, true);
|
|
|
|
setButtonVisibility(tapToRetryRef, false);
|
2020-07-15 13:20:27 +02:00
|
|
|
break;
|
|
|
|
case TAP.RETRY:
|
2020-07-15 17:51:05 +02:00
|
|
|
setButtonVisibility(tapToUnmuteRef, false);
|
|
|
|
setButtonVisibility(tapToRetryRef, true);
|
2020-07-15 13:20:27 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if (isDev) throw new Error('showTapButton: unexpected ref');
|
2020-07-15 17:51:05 +02:00
|
|
|
break;
|
2020-06-10 15:01:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function unmuteAndHideHint() {
|
|
|
|
if (player) {
|
|
|
|
player.muted(false);
|
2020-06-11 20:55:30 +02:00
|
|
|
if (player.volume() === 0) {
|
|
|
|
player.volume(1.0);
|
|
|
|
}
|
2020-06-10 15:01:54 +02:00
|
|
|
}
|
2020-07-15 17:51:05 +02:00
|
|
|
showTapButton(TAP.NONE);
|
2020-07-15 13:20:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function retryVideoAfterFailure() {
|
|
|
|
if (player) {
|
|
|
|
setReload(Date.now());
|
2020-07-15 17:51:05 +02:00
|
|
|
showTapButton(TAP.NONE);
|
2020-07-15 13:20:27 +02:00
|
|
|
}
|
2020-06-10 15:01:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function onInitialPlay() {
|
2020-06-11 20:55:30 +02:00
|
|
|
if (player && (player.muted() || player.volume() === 0)) {
|
2020-06-10 15:01:54 +02:00
|
|
|
// The css starts as "hidden". We make it visible here without
|
|
|
|
// re-rendering the whole thing.
|
2020-07-15 17:51:05 +02:00
|
|
|
showTapButton(TAP.UNMUTE);
|
2020-06-10 15:01:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function onVolumeChange() {
|
|
|
|
if (player && !player.muted()) {
|
2020-07-15 17:51:05 +02:00
|
|
|
showTapButton(TAP.NONE);
|
2020-06-10 15:01:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function onError() {
|
2020-07-15 17:51:05 +02:00
|
|
|
showTapButton(TAP.RETRY);
|
2020-07-15 13:20:27 +02:00
|
|
|
|
2020-07-15 10:50:25 +02:00
|
|
|
if (player && player.loadingSpinner) {
|
|
|
|
player.loadingSpinner.hide();
|
|
|
|
}
|
2020-06-10 15:01:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function onEnded() {
|
2020-07-15 17:51:05 +02:00
|
|
|
showTapButton(TAP.NONE);
|
2020-06-10 15:01:54 +02:00
|
|
|
}
|
|
|
|
|
2020-04-16 01:21:17 +02:00
|
|
|
function handleKeyDown(e: KeyboardEvent) {
|
2020-04-28 21:33:30 +02:00
|
|
|
const videoNode: ?HTMLVideoElement = containerRef.current && containerRef.current.querySelector('video, audio');
|
2020-04-16 01:21:17 +02:00
|
|
|
|
|
|
|
if (!videoNode || isUserTyping()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (e.keyCode === SPACE_BAR_KEYCODE) {
|
|
|
|
videoNode.paused ? videoNode.play() : videoNode.pause();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fullscreen toggle shortcuts
|
2020-04-28 21:33:30 +02:00
|
|
|
if (player && (e.keyCode === FULLSCREEN_KEYCODE || e.keyCode === F11_KEYCODE)) {
|
|
|
|
if (!player.isFullscreen()) {
|
2020-04-16 01:21:17 +02:00
|
|
|
player.requestFullscreen();
|
|
|
|
} else {
|
|
|
|
player.exitFullscreen();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mute/Unmute Shortcuts
|
|
|
|
if (e.keyCode === MUTE_KEYCODE) {
|
|
|
|
videoNode.muted = !videoNode.muted;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Seeking Shortcuts
|
|
|
|
const duration = videoNode.duration;
|
|
|
|
const currentTime = videoNode.currentTime;
|
|
|
|
if (e.keyCode === SEEK_FORWARD_KEYCODE) {
|
|
|
|
const newDuration = currentTime + SEEK_STEP;
|
|
|
|
videoNode.currentTime = newDuration > duration ? duration : newDuration;
|
|
|
|
}
|
|
|
|
if (e.keyCode === SEEK_BACKWARD_KEYCODE) {
|
|
|
|
const newDuration = currentTime - SEEK_STEP;
|
|
|
|
videoNode.currentTime = newDuration < 0 ? 0 : newDuration;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-16 23:43:09 +02:00
|
|
|
// Create the video element. Note that a new videojs instantiation will happen on *every* render, so do not add props to this component!
|
|
|
|
useEffect(() => {
|
|
|
|
if (containerRef.current) {
|
|
|
|
const wrapper = document.createElement('div');
|
2020-04-28 21:33:30 +02:00
|
|
|
wrapper.setAttribute('data-vjs-player', 'true');
|
2020-04-16 23:43:09 +02:00
|
|
|
const el = document.createElement(isAudio ? 'audio' : 'video');
|
|
|
|
el.className = 'video-js';
|
|
|
|
wrapper.appendChild(el);
|
2020-04-28 21:33:30 +02:00
|
|
|
|
|
|
|
// $FlowFixMe
|
2020-04-16 23:43:09 +02:00
|
|
|
containerRef.current.appendChild(wrapper);
|
|
|
|
|
2020-04-17 16:17:43 +02:00
|
|
|
player = videojs(el, videoJsOptions, () => {
|
2020-04-28 21:33:30 +02:00
|
|
|
if (player) {
|
2020-06-10 15:01:54 +02:00
|
|
|
player.one('play', onInitialPlay);
|
|
|
|
player.on('volumechange', onVolumeChange);
|
|
|
|
player.on('error', onError);
|
|
|
|
player.on('ended', onEnded);
|
|
|
|
|
2020-04-28 21:33:30 +02:00
|
|
|
onPlayerReady(player);
|
|
|
|
}
|
2020-04-17 16:17:43 +02:00
|
|
|
});
|
2020-04-16 01:21:17 +02:00
|
|
|
|
2020-04-16 23:43:09 +02: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
|
|
|
|
// $FlowFixMe
|
|
|
|
player.on('fullscreenchange', () => document.activeElement && document.activeElement.blur());
|
|
|
|
|
2020-04-16 01:21:17 +02:00
|
|
|
window.addEventListener('keydown', handleKeyDown);
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
window.removeEventListener('keydown', handleKeyDown);
|
2020-04-28 21:33:30 +02:00
|
|
|
|
|
|
|
if (player) {
|
|
|
|
player.dispose();
|
|
|
|
}
|
2020-04-16 01:21:17 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-06-10 15:01:54 +02:00
|
|
|
return (
|
2020-07-15 13:20:27 +02:00
|
|
|
reload && (
|
|
|
|
// $FlowFixMe
|
2020-10-29 19:33:38 +01:00
|
|
|
<div className={classnames('video-js-parent')} ref={containerRef}>
|
2020-06-10 15:01:54 +02:00
|
|
|
<Button
|
|
|
|
label={__('Tap to unmute')}
|
|
|
|
button="link"
|
|
|
|
icon={ICONS.VOLUME_MUTED}
|
|
|
|
className="video-js--tap-to-unmute"
|
|
|
|
onClick={unmuteAndHideHint}
|
|
|
|
ref={tapToUnmuteRef}
|
|
|
|
/>
|
2020-07-15 13:20:27 +02:00
|
|
|
<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
|
|
|
});
|