2019-08-02 02:28:14 -04:00
|
|
|
// @flow
|
2020-04-16 17:43:09 -04:00
|
|
|
import React, { useEffect, useState, useContext, useCallback } from 'react';
|
2019-08-02 02:28:14 -04:00
|
|
|
import { stopContextMenu } from 'util/context-menu';
|
2020-04-28 15:33:30 -04:00
|
|
|
import type { Player } from './internal/videojs';
|
2020-04-15 19:21:17 -04:00
|
|
|
import VideoJs from './internal/videojs';
|
2020-01-22 12:19:49 -05:00
|
|
|
import analytics from 'analytics';
|
2020-01-31 13:25:48 -05:00
|
|
|
import { EmbedContext } from 'page/embedWrapper/view';
|
2020-04-13 19:48:11 -04:00
|
|
|
import classnames from 'classnames';
|
2020-03-27 12:49:41 -04:00
|
|
|
import { FORCE_CONTENT_TYPE_PLAYER } from 'constants/claim';
|
2020-04-13 19:48:11 -04:00
|
|
|
import AutoplayCountdown from 'component/autoplayCountdown';
|
|
|
|
import usePrevious from 'effects/use-previous';
|
2020-05-07 14:44:11 -04:00
|
|
|
import FileViewerEmbeddedEnded from 'web/component/fileViewerEmbeddedEnded';
|
2020-04-29 16:50:06 -04:00
|
|
|
import FileViewerEmbeddedTitle from 'component/fileViewerEmbeddedTitle';
|
2020-04-16 17:43:09 -04:00
|
|
|
import LoadingScreen from 'component/common/loading-screen';
|
2021-01-08 10:21:27 -05:00
|
|
|
import { addTheaterModeButton } from './internal/theater-mode';
|
2019-08-02 02:28:14 -04:00
|
|
|
|
2020-04-28 15:33:30 -04:00
|
|
|
const PLAY_TIMEOUT_ERROR = 'play_timeout_error';
|
2021-01-27 05:49:30 -08:00
|
|
|
const PLAY_TIMEOUT_LIMIT = 2000;
|
2020-04-28 15:33:30 -04:00
|
|
|
|
2019-08-02 02:28:14 -04:00
|
|
|
type Props = {
|
2019-09-05 20:26:03 -04:00
|
|
|
position: number,
|
2021-02-17 17:28:20 +08:00
|
|
|
changeVolume: (number) => void,
|
|
|
|
changeMute: (boolean) => void,
|
2019-08-02 02:28:14 -04:00
|
|
|
source: string,
|
|
|
|
contentType: string,
|
2019-11-28 03:14:07 -05:00
|
|
|
thumbnail: string,
|
2020-08-07 16:59:20 -04:00
|
|
|
claim: StreamClaim,
|
2020-04-16 17:43:09 -04:00
|
|
|
muted: boolean,
|
2021-01-14 21:16:04 +08:00
|
|
|
videoPlaybackRate: number,
|
2020-04-16 17:43:09 -04:00
|
|
|
volume: number,
|
2020-04-13 19:48:11 -04:00
|
|
|
uri: string,
|
|
|
|
autoplaySetting: boolean,
|
|
|
|
autoplayIfEmbedded: boolean,
|
2020-05-05 14:02:12 -04:00
|
|
|
desktopPlayStartTime?: number,
|
2020-04-13 19:48:11 -04:00
|
|
|
doAnalyticsView: (string, number) => Promise<any>,
|
2020-08-07 16:59:20 -04:00
|
|
|
doAnalyticsBuffer: (string, any) => void,
|
2020-04-13 19:48:11 -04:00
|
|
|
claimRewards: () => void,
|
2020-04-30 00:49:52 -07:00
|
|
|
savePosition: (string, number) => void,
|
2021-02-17 17:28:20 +08:00
|
|
|
clearPosition: (string) => void,
|
2021-01-08 10:21:27 -05:00
|
|
|
toggleVideoTheaterMode: () => void,
|
2021-02-17 17:28:20 +08:00
|
|
|
setVideoPlaybackRate: (number) => void,
|
2019-08-02 02:28:14 -04:00
|
|
|
};
|
|
|
|
|
2020-04-15 19:21:17 -04:00
|
|
|
/*
|
|
|
|
codesandbox of idealized/clean videojs and react 16+
|
|
|
|
https://codesandbox.io/s/71z2lm4ko6
|
|
|
|
*/
|
|
|
|
|
2019-08-02 02:28:14 -04:00
|
|
|
function VideoViewer(props: Props) {
|
2020-02-04 16:14:08 -05:00
|
|
|
const {
|
|
|
|
contentType,
|
|
|
|
source,
|
|
|
|
changeVolume,
|
|
|
|
changeMute,
|
2021-01-14 21:16:04 +08:00
|
|
|
videoPlaybackRate,
|
2020-02-04 16:14:08 -05:00
|
|
|
thumbnail,
|
2020-03-19 16:25:37 -04:00
|
|
|
position,
|
2020-02-04 16:14:08 -05:00
|
|
|
claim,
|
2020-04-13 19:48:11 -04:00
|
|
|
uri,
|
2020-04-16 17:43:09 -04:00
|
|
|
muted,
|
|
|
|
volume,
|
2020-04-13 19:48:11 -04:00
|
|
|
autoplaySetting,
|
|
|
|
autoplayIfEmbedded,
|
|
|
|
doAnalyticsView,
|
2020-08-07 16:59:20 -04:00
|
|
|
doAnalyticsBuffer,
|
2020-04-13 19:48:11 -04:00
|
|
|
claimRewards,
|
2020-04-30 00:49:52 -07:00
|
|
|
savePosition,
|
2020-05-14 18:18:54 -07:00
|
|
|
clearPosition,
|
2020-05-05 14:02:12 -04:00
|
|
|
desktopPlayStartTime,
|
2021-01-08 10:21:27 -05:00
|
|
|
toggleVideoTheaterMode,
|
2021-01-14 21:16:04 +08:00
|
|
|
setVideoPlaybackRate,
|
2020-02-04 16:14:08 -05:00
|
|
|
} = props;
|
2020-01-22 12:19:49 -05:00
|
|
|
const claimId = claim && claim.claim_id;
|
2019-11-28 03:14:07 -05:00
|
|
|
const isAudio = contentType.includes('audio');
|
2020-03-27 12:49:41 -04:00
|
|
|
const forcePlayer = FORCE_CONTENT_TYPE_PLAYER.includes(contentType);
|
2020-04-13 19:48:11 -04:00
|
|
|
const [isPlaying, setIsPlaying] = useState(false);
|
|
|
|
const [showAutoplayCountdown, setShowAutoplayCountdown] = useState(false);
|
|
|
|
const [isEndededEmbed, setIsEndededEmbed] = useState(false);
|
2021-02-18 01:01:12 +08:00
|
|
|
const vjsCallbackDataRef: any = React.useRef();
|
2020-04-26 16:32:39 -04:00
|
|
|
|
|
|
|
/* isLoading was designed to show loading screen on first play press, rather than completely black screen, but
|
|
|
|
breaks because some browsers (e.g. Firefox) block autoplay but leave the player.play Promise pending */
|
2020-04-16 17:43:09 -04:00
|
|
|
const [isLoading, setIsLoading] = useState(false);
|
2019-10-08 10:06:28 +02:00
|
|
|
|
2020-04-15 19:21:17 -04:00
|
|
|
const previousUri = usePrevious(uri);
|
|
|
|
const embedded = useContext(EmbedContext);
|
2019-08-02 02:28:14 -04:00
|
|
|
|
2020-04-16 17:43:09 -04:00
|
|
|
// force everything to recent when URI changes, can cause weird corner cases otherwise (e.g. navigate while autoplay is true)
|
|
|
|
useEffect(() => {
|
|
|
|
if (uri && previousUri && uri !== previousUri) {
|
|
|
|
setShowAutoplayCountdown(false);
|
|
|
|
setIsEndededEmbed(false);
|
|
|
|
setIsLoading(false);
|
|
|
|
}
|
|
|
|
}, [uri, previousUri]);
|
|
|
|
|
2021-02-18 01:01:12 +08:00
|
|
|
// Update vjsCallbackDataRef (ensures videojs callbacks are not using stale values):
|
|
|
|
useEffect(() => {
|
|
|
|
vjsCallbackDataRef.current = {
|
|
|
|
embedded: embedded,
|
|
|
|
videoPlaybackRate: videoPlaybackRate,
|
|
|
|
};
|
2021-03-09 10:55:55 +08:00
|
|
|
}, [embedded, videoPlaybackRate]);
|
2021-02-18 01:01:12 +08:00
|
|
|
|
2020-04-15 19:21:17 -04:00
|
|
|
function doTrackingBuffered(e: Event, data: any) {
|
2021-02-17 17:28:20 +08:00
|
|
|
fetch(source, { method: 'HEAD' }).then((response) => {
|
2020-09-09 14:54:51 -04:00
|
|
|
data.playerPoweredBy = response.headers.get('x-powered-by');
|
|
|
|
doAnalyticsBuffer(uri, data);
|
|
|
|
});
|
2020-04-15 19:21:17 -04:00
|
|
|
}
|
2019-08-13 01:35:13 -04:00
|
|
|
|
2020-04-15 19:21:17 -04:00
|
|
|
function doTrackingFirstPlay(e: Event, data: any) {
|
2020-05-05 14:02:12 -04:00
|
|
|
let timeToStartInMs = data.secondsToLoad * 1000;
|
2020-05-05 16:45:59 -04:00
|
|
|
|
2020-05-05 14:02:12 -04:00
|
|
|
if (desktopPlayStartTime !== undefined) {
|
|
|
|
const differenceToAdd = Date.now() - desktopPlayStartTime;
|
|
|
|
timeToStartInMs += differenceToAdd;
|
|
|
|
}
|
2020-11-26 16:06:58 -05:00
|
|
|
analytics.playerStartedEvent(embedded);
|
2020-05-05 10:09:53 -04:00
|
|
|
analytics.videoStartEvent(claimId, timeToStartInMs);
|
|
|
|
doAnalyticsView(uri, timeToStartInMs).then(() => {
|
2020-04-15 19:21:17 -04:00
|
|
|
claimRewards();
|
|
|
|
});
|
|
|
|
}
|
2020-04-13 19:48:11 -04:00
|
|
|
|
2020-04-15 19:21:17 -04:00
|
|
|
function onEnded() {
|
|
|
|
if (embedded) {
|
|
|
|
setIsEndededEmbed(true);
|
|
|
|
} else if (autoplaySetting) {
|
|
|
|
setShowAutoplayCountdown(true);
|
2020-01-22 12:19:49 -05:00
|
|
|
}
|
2020-04-15 19:21:17 -04:00
|
|
|
}
|
2020-04-08 13:10:33 -04:00
|
|
|
|
2020-04-15 19:21:17 -04:00
|
|
|
function onPlay() {
|
2020-04-16 17:43:09 -04:00
|
|
|
setIsLoading(false);
|
2020-04-15 19:21:17 -04:00
|
|
|
setIsPlaying(true);
|
|
|
|
setShowAutoplayCountdown(false);
|
|
|
|
setIsEndededEmbed(false);
|
|
|
|
}
|
2020-04-08 13:10:33 -04:00
|
|
|
|
2020-05-14 18:18:54 -07:00
|
|
|
function handlePosition(player) {
|
|
|
|
if (player.ended()) {
|
|
|
|
clearPosition(uri);
|
|
|
|
} else {
|
|
|
|
savePosition(uri, player.currentTime());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-09 10:55:55 +08:00
|
|
|
function restorePlaybackRate(player) {
|
2021-02-18 01:01:12 +08:00
|
|
|
if (!vjsCallbackDataRef.current.embedded) {
|
|
|
|
player.playbackRate(vjsCallbackDataRef.current.videoPlaybackRate);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-29 10:00:48 -04:00
|
|
|
const onPlayerReady = useCallback(
|
|
|
|
(player: Player) => {
|
|
|
|
if (!embedded) {
|
2021-03-09 10:55:55 +08:00
|
|
|
player.muted(muted);
|
|
|
|
player.volume(volume);
|
|
|
|
player.playbackRate(videoPlaybackRate);
|
2021-01-08 10:21:27 -05:00
|
|
|
addTheaterModeButton(player, toggleVideoTheaterMode);
|
2020-04-29 10:00:48 -04:00
|
|
|
}
|
2020-04-27 15:23:07 -04:00
|
|
|
|
2020-04-29 10:00:48 -04:00
|
|
|
const shouldPlay = !embedded || autoplayIfEmbedded;
|
|
|
|
// https://blog.videojs.com/autoplay-best-practices-with-video-js/#Programmatic-Autoplay-and-Success-Failure-Detection
|
|
|
|
if (shouldPlay) {
|
|
|
|
const playPromise = player.play();
|
2021-02-17 17:28:20 +08:00
|
|
|
const timeoutPromise = new Promise((resolve, reject) =>
|
|
|
|
setTimeout(() => reject(PLAY_TIMEOUT_ERROR), PLAY_TIMEOUT_LIMIT)
|
|
|
|
);
|
2020-04-28 13:25:13 -04:00
|
|
|
|
2021-02-17 17:28:20 +08:00
|
|
|
Promise.race([playPromise, timeoutPromise]).catch((error) => {
|
2020-04-29 10:36:43 -04:00
|
|
|
if (PLAY_TIMEOUT_ERROR) {
|
|
|
|
const retryPlayPromise = player.play();
|
2021-02-17 17:28:20 +08:00
|
|
|
Promise.race([retryPlayPromise, timeoutPromise]).catch((error) => {
|
2020-04-29 10:36:43 -04:00
|
|
|
setIsLoading(false);
|
|
|
|
setIsPlaying(false);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
setIsLoading(false);
|
|
|
|
setIsPlaying(false);
|
|
|
|
}
|
2020-04-29 10:00:48 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
setIsLoading(shouldPlay); // if we are here outside of an embed, we're playing
|
2021-02-17 17:28:20 +08:00
|
|
|
|
2021-03-09 10:55:55 +08:00
|
|
|
// PR: #5535
|
2021-02-18 01:01:12 +08:00
|
|
|
// Move the restoration to a later `loadedmetadata` phase to counter the
|
|
|
|
// delay from the header-fetch. This is a temp change until the next
|
|
|
|
// re-factoring.
|
2021-03-09 10:55:55 +08:00
|
|
|
player.on('loadedmetadata', () => restorePlaybackRate(player));
|
2021-02-18 01:01:12 +08:00
|
|
|
|
2020-04-29 10:00:48 -04:00
|
|
|
player.on('tracking:buffered', doTrackingBuffered);
|
|
|
|
player.on('tracking:firstplay', doTrackingFirstPlay);
|
|
|
|
player.on('ended', onEnded);
|
|
|
|
player.on('play', onPlay);
|
2020-04-30 21:31:40 -07:00
|
|
|
player.on('pause', () => {
|
|
|
|
setIsPlaying(false);
|
2020-05-14 18:18:54 -07:00
|
|
|
handlePosition(player);
|
2020-04-30 21:31:40 -07:00
|
|
|
});
|
2021-02-17 17:28:20 +08:00
|
|
|
player.on('error', () => {
|
2020-05-21 11:38:28 -04:00
|
|
|
const error = player.error();
|
|
|
|
if (error) {
|
|
|
|
analytics.sentryError('Video.js error', error);
|
|
|
|
}
|
|
|
|
});
|
2020-04-29 10:00:48 -04:00
|
|
|
player.on('volumechange', () => {
|
2020-07-06 20:13:25 +08:00
|
|
|
if (player) {
|
2020-04-29 10:00:48 -04:00
|
|
|
changeVolume(player.volume());
|
|
|
|
changeMute(player.muted());
|
2020-04-28 13:25:13 -04:00
|
|
|
}
|
|
|
|
});
|
2021-01-14 21:16:04 +08:00
|
|
|
player.on('ratechange', () => {
|
2021-02-18 01:01:12 +08:00
|
|
|
const HAVE_NOTHING = 0; // https://docs.videojs.com/player#readyState
|
|
|
|
if (player && player.readyState() !== HAVE_NOTHING) {
|
|
|
|
// The playbackRate occasionally resets to 1, typically when loading a fresh video or when 'src' changes.
|
|
|
|
// Videojs says it's a browser quirk (https://github.com/videojs/video.js/issues/2516).
|
|
|
|
// [x] Don't update 'videoPlaybackRate' in this scenario.
|
|
|
|
// [ ] Ideally, the controlBar should be hidden to prevent users from changing the rate while loading.
|
2021-01-14 21:16:04 +08:00
|
|
|
setVideoPlaybackRate(player.playbackRate());
|
|
|
|
}
|
|
|
|
});
|
2020-04-24 16:40:31 -04:00
|
|
|
|
2020-04-29 10:00:48 -04:00
|
|
|
if (position) {
|
|
|
|
player.currentTime(position);
|
2020-04-16 17:43:09 -04:00
|
|
|
}
|
2020-05-14 18:18:54 -07:00
|
|
|
player.on('dispose', () => {
|
|
|
|
handlePosition(player);
|
|
|
|
});
|
2020-04-29 10:00:48 -04:00
|
|
|
},
|
2020-05-05 16:45:59 -04:00
|
|
|
IS_WEB ? [uri] : [uri, desktopPlayStartTime]
|
2020-04-29 10:00:48 -04:00
|
|
|
);
|
2020-03-19 16:25:37 -04:00
|
|
|
|
2019-08-02 02:28:14 -04:00
|
|
|
return (
|
2020-04-13 19:48:11 -04:00
|
|
|
<div
|
|
|
|
className={classnames('file-viewer', {
|
|
|
|
'file-viewer--is-playing': isPlaying,
|
2020-04-16 17:43:09 -04:00
|
|
|
'file-viewer--ended-embed': isEndededEmbed,
|
2020-04-13 19:48:11 -04:00
|
|
|
})}
|
|
|
|
onContextMenu={stopContextMenu}
|
|
|
|
>
|
|
|
|
{showAutoplayCountdown && <AutoplayCountdown uri={uri} />}
|
|
|
|
{isEndededEmbed && <FileViewerEmbeddedEnded uri={uri} />}
|
|
|
|
{embedded && !isEndededEmbed && <FileViewerEmbeddedTitle uri={uri} />}
|
2020-04-26 16:32:39 -04:00
|
|
|
{/* disable this loading behavior because it breaks when player.play() promise hangs */}
|
2020-04-28 13:25:13 -04:00
|
|
|
{isLoading && <LoadingScreen status={__('Loading')} />}
|
2020-04-15 19:21:17 -04:00
|
|
|
<VideoJs
|
|
|
|
source={source}
|
|
|
|
isAudio={isAudio}
|
2021-01-27 05:49:30 -08:00
|
|
|
poster={isAudio || (embedded && !autoplayIfEmbedded) ? thumbnail : ''}
|
2020-04-15 19:21:17 -04:00
|
|
|
sourceType={forcePlayer ? 'video/mp4' : contentType}
|
2020-04-16 12:10:47 -04:00
|
|
|
onPlayerReady={onPlayerReady}
|
2020-04-24 16:40:31 -04:00
|
|
|
startMuted={autoplayIfEmbedded}
|
2021-01-20 16:50:16 +08:00
|
|
|
toggleVideoTheaterMode={toggleVideoTheaterMode}
|
2021-01-27 05:49:30 -08:00
|
|
|
autoplay={!embedded || autoplayIfEmbedded}
|
2020-04-15 19:21:17 -04:00
|
|
|
/>
|
2019-08-02 02:28:14 -04:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export default VideoViewer;
|