2019-08-02 08:28:14 +02:00
|
|
|
// @flow
|
2021-04-12 18:43:47 +02:00
|
|
|
import { ENABLE_PREROLL_ADS } from 'config';
|
|
|
|
import * as PAGES from 'constants/pages';
|
|
|
|
import * as ICONS from 'constants/icons';
|
2020-04-16 23:43:09 +02:00
|
|
|
import React, { useEffect, useState, useContext, useCallback } from 'react';
|
2019-08-02 08:28:14 +02:00
|
|
|
import { stopContextMenu } from 'util/context-menu';
|
2022-05-02 10:55:08 +02:00
|
|
|
import * as Chapters from './internal/chapters';
|
2020-04-28 21:33:30 +02:00
|
|
|
import type { Player } from './internal/videojs';
|
2021-07-06 11:02:05 +02:00
|
|
|
import VideoJs from './internal/videojs';
|
2020-01-22 18:19:49 +01:00
|
|
|
import analytics from 'analytics';
|
2021-07-19 16:12:47 +02:00
|
|
|
import { EmbedContext } from 'page/embedWrapper/view';
|
2020-04-14 01:48:11 +02:00
|
|
|
import classnames from 'classnames';
|
2020-03-27 17:49:41 +01:00
|
|
|
import { FORCE_CONTENT_TYPE_PLAYER } from 'constants/claim';
|
2020-04-14 01:48:11 +02:00
|
|
|
import AutoplayCountdown from 'component/autoplayCountdown';
|
|
|
|
import usePrevious from 'effects/use-previous';
|
2020-05-07 20:44:11 +02:00
|
|
|
import FileViewerEmbeddedEnded from 'web/component/fileViewerEmbeddedEnded';
|
2020-04-29 22:50:06 +02:00
|
|
|
import FileViewerEmbeddedTitle from 'component/fileViewerEmbeddedTitle';
|
2022-05-17 08:57:14 +02:00
|
|
|
import useAutoplayNext from './internal/effects/use-autoplay-next';
|
2022-05-17 09:10:28 +02:00
|
|
|
import useTheaterMode from './internal/effects/use-theater-mode';
|
2021-09-02 22:05:32 +02:00
|
|
|
import { addPlayNextButton } from './internal/play-next';
|
|
|
|
import { addPlayPreviousButton } from './internal/play-previous';
|
2021-04-12 18:43:47 +02:00
|
|
|
import { useGetAds } from 'effects/use-get-ads';
|
|
|
|
import Button from 'component/button';
|
|
|
|
import I18nMessage from 'component/i18nMessage';
|
|
|
|
import { useHistory } from 'react-router';
|
2021-07-17 00:20:22 +02:00
|
|
|
import { getAllIds } from 'util/buildHomepage';
|
|
|
|
import type { HomepageCat } from 'util/buildHomepage';
|
2021-11-29 15:57:46 +01:00
|
|
|
import debounce from 'util/debounce';
|
2021-09-10 19:27:21 +02:00
|
|
|
import { formatLbryUrlForWeb, generateListSearchUrlParams } from 'util/url';
|
2022-03-02 17:13:46 +01:00
|
|
|
import useInterval from 'effects/use-interval';
|
2022-04-12 12:35:46 +02:00
|
|
|
import { lastBandwidthSelector } from './internal/plugins/videojs-http-streaming--override/playlist-selectors';
|
2022-06-10 18:18:58 +02:00
|
|
|
import { platform } from 'util/platform';
|
2019-08-02 08:28:14 +02:00
|
|
|
|
2022-01-06 20:28:27 +01:00
|
|
|
// const PLAY_TIMEOUT_ERROR = 'play_timeout_error';
|
|
|
|
// const PLAY_TIMEOUT_LIMIT = 2000;
|
2022-03-03 03:25:14 +01:00
|
|
|
const PLAY_POSITION_SAVE_INTERVAL_MS = 15000;
|
2020-04-28 21:33:30 +02:00
|
|
|
|
2022-06-10 18:18:58 +02:00
|
|
|
const IS_IOS = platform.isIOS();
|
|
|
|
|
2019-08-02 08:28:14 +02:00
|
|
|
type Props = {
|
2019-09-06 02:26:03 +02:00
|
|
|
position: number,
|
2021-02-17 10:28:20 +01:00
|
|
|
changeVolume: (number) => void,
|
|
|
|
changeMute: (boolean) => void,
|
2019-08-02 08:28:14 +02:00
|
|
|
source: string,
|
|
|
|
contentType: string,
|
2019-11-28 09:14:07 +01:00
|
|
|
thumbnail: string,
|
2020-08-07 22:59:20 +02:00
|
|
|
claim: StreamClaim,
|
2020-04-16 23:43:09 +02:00
|
|
|
muted: boolean,
|
2021-01-14 14:16:04 +01:00
|
|
|
videoPlaybackRate: number,
|
2020-04-16 23:43:09 +02:00
|
|
|
volume: number,
|
2020-04-14 01:48:11 +02:00
|
|
|
uri: string,
|
2021-09-02 22:05:32 +02:00
|
|
|
autoplayNext: boolean,
|
2020-04-14 01:48:11 +02:00
|
|
|
autoplayIfEmbedded: boolean,
|
2020-08-07 22:59:20 +02:00
|
|
|
doAnalyticsBuffer: (string, any) => void,
|
2020-04-30 09:49:52 +02:00
|
|
|
savePosition: (string, number) => void,
|
2021-02-17 10:28:20 +01:00
|
|
|
clearPosition: (string) => void,
|
2021-01-08 16:21:27 +01:00
|
|
|
toggleVideoTheaterMode: () => void,
|
2021-09-02 22:05:32 +02:00
|
|
|
toggleAutoplayNext: () => void,
|
2021-02-17 10:28:20 +01:00
|
|
|
setVideoPlaybackRate: (number) => void,
|
2021-04-12 18:43:47 +02:00
|
|
|
authenticated: boolean,
|
2021-06-29 03:51:04 +02:00
|
|
|
userId: number,
|
2021-10-13 17:04:03 +02:00
|
|
|
internalFeature: boolean,
|
2021-07-17 00:20:22 +02:00
|
|
|
homepageData?: { [string]: HomepageCat },
|
2021-07-31 05:55:02 +02:00
|
|
|
shareTelemetry: boolean,
|
2021-09-02 22:05:32 +02:00
|
|
|
isFloating: boolean,
|
2021-09-10 19:27:21 +02:00
|
|
|
doPlayUri: (string, string) => void,
|
2021-09-02 22:05:32 +02:00
|
|
|
collectionId: string,
|
|
|
|
nextRecommendedUri: string,
|
|
|
|
previousListUri: string,
|
|
|
|
videoTheaterMode: boolean,
|
2021-09-13 17:24:35 +02:00
|
|
|
isMarkdownOrComment: boolean,
|
2022-01-06 20:28:27 +01:00
|
|
|
doAnalyticsView: (string, number) => void,
|
|
|
|
claimRewards: () => void,
|
2022-03-15 17:18:08 +01:00
|
|
|
isLivestreamClaim: boolean,
|
|
|
|
activeLivestreamForChannel: any,
|
2022-04-19 18:55:32 +02:00
|
|
|
defaultQuality: ?string,
|
2022-04-25 15:28:36 +02:00
|
|
|
doToast: ({ message: string, linkText: string, linkTarget: string }) => void,
|
2022-05-04 12:24:13 +02:00
|
|
|
doSetContentHistoryItem: (uri: string) => void,
|
|
|
|
doClearContentHistoryUri: (uri: string) => void,
|
2019-08-02 08:28:14 +02:00
|
|
|
};
|
|
|
|
|
2020-04-16 01:21:17 +02:00
|
|
|
/*
|
|
|
|
codesandbox of idealized/clean videojs and react 16+
|
|
|
|
https://codesandbox.io/s/71z2lm4ko6
|
|
|
|
*/
|
|
|
|
|
2019-08-02 08:28:14 +02:00
|
|
|
function VideoViewer(props: Props) {
|
2020-02-04 22:14:08 +01:00
|
|
|
const {
|
|
|
|
contentType,
|
|
|
|
source,
|
|
|
|
changeVolume,
|
|
|
|
changeMute,
|
2021-01-14 14:16:04 +01:00
|
|
|
videoPlaybackRate,
|
2020-02-04 22:14:08 +01:00
|
|
|
thumbnail,
|
2020-03-19 21:25:37 +01:00
|
|
|
position,
|
2020-02-04 22:14:08 +01:00
|
|
|
claim,
|
2020-04-14 01:48:11 +02:00
|
|
|
uri,
|
2020-04-16 23:43:09 +02:00
|
|
|
muted,
|
|
|
|
volume,
|
2021-09-02 22:05:32 +02:00
|
|
|
autoplayNext,
|
2020-04-14 01:48:11 +02:00
|
|
|
autoplayIfEmbedded,
|
2020-08-07 22:59:20 +02:00
|
|
|
doAnalyticsBuffer,
|
2022-01-06 20:28:27 +01:00
|
|
|
doAnalyticsView,
|
2020-04-14 01:48:11 +02:00
|
|
|
claimRewards,
|
2020-04-30 09:49:52 +02:00
|
|
|
savePosition,
|
2020-05-15 03:18:54 +02:00
|
|
|
clearPosition,
|
2021-01-08 16:21:27 +01:00
|
|
|
toggleVideoTheaterMode,
|
2021-09-02 22:05:32 +02:00
|
|
|
toggleAutoplayNext,
|
2021-01-14 14:16:04 +01:00
|
|
|
setVideoPlaybackRate,
|
2021-04-12 18:43:47 +02:00
|
|
|
homepageData,
|
|
|
|
authenticated,
|
2021-06-29 03:51:04 +02:00
|
|
|
userId,
|
2021-10-13 17:04:03 +02:00
|
|
|
internalFeature,
|
2021-07-31 05:55:02 +02:00
|
|
|
shareTelemetry,
|
2021-09-02 22:05:32 +02:00
|
|
|
isFloating,
|
|
|
|
doPlayUri,
|
|
|
|
collectionId,
|
|
|
|
nextRecommendedUri,
|
|
|
|
previousListUri,
|
|
|
|
videoTheaterMode,
|
2021-09-13 17:24:35 +02:00
|
|
|
isMarkdownOrComment,
|
2022-03-15 17:18:08 +01:00
|
|
|
isLivestreamClaim,
|
|
|
|
activeLivestreamForChannel,
|
2022-04-19 18:55:32 +02:00
|
|
|
defaultQuality,
|
2022-04-25 15:28:36 +02:00
|
|
|
doToast,
|
2022-05-04 12:24:13 +02:00
|
|
|
doSetContentHistoryItem,
|
2020-02-04 22:14:08 +01:00
|
|
|
} = props;
|
2022-03-15 17:18:08 +01:00
|
|
|
|
2021-09-02 22:05:32 +02:00
|
|
|
const permanentUrl = claim && claim.permanent_url;
|
2021-07-17 00:20:22 +02:00
|
|
|
const adApprovedChannelIds = homepageData ? getAllIds(homepageData) : [];
|
2020-01-22 18:19:49 +01:00
|
|
|
const claimId = claim && claim.claim_id;
|
2021-04-12 18:43:47 +02:00
|
|
|
const channelClaimId = claim && claim.signing_channel && claim.signing_channel.claim_id;
|
2022-05-30 07:27:41 +02:00
|
|
|
const channelTitle =
|
|
|
|
(claim && claim.signing_channel && claim.signing_channel.value && claim.signing_channel.value.title) || '';
|
2019-11-28 09:14:07 +01:00
|
|
|
const isAudio = contentType.includes('audio');
|
2020-03-27 17:49:41 +01:00
|
|
|
const forcePlayer = FORCE_CONTENT_TYPE_PLAYER.includes(contentType);
|
2021-04-12 18:43:47 +02:00
|
|
|
const {
|
2021-09-02 22:05:32 +02:00
|
|
|
push,
|
2021-04-12 18:43:47 +02:00
|
|
|
location: { pathname },
|
|
|
|
} = useHistory();
|
2021-09-02 22:05:32 +02:00
|
|
|
const [doNavigate, setDoNavigate] = useState(false);
|
|
|
|
const [playNextUrl, setPlayNextUrl] = useState(true);
|
2020-04-14 01:48:11 +02:00
|
|
|
const [isPlaying, setIsPlaying] = useState(false);
|
2021-09-02 22:05:32 +02:00
|
|
|
const [ended, setEnded] = useState(false);
|
2020-04-14 01:48:11 +02:00
|
|
|
const [showAutoplayCountdown, setShowAutoplayCountdown] = useState(false);
|
2021-09-03 00:39:40 +02:00
|
|
|
const [isEndedEmbed, setIsEndedEmbed] = useState(false);
|
2021-02-17 18:01:12 +01:00
|
|
|
const vjsCallbackDataRef: any = React.useRef();
|
2021-04-12 18:43:47 +02:00
|
|
|
const previousUri = usePrevious(uri);
|
|
|
|
const embedded = useContext(EmbedContext);
|
|
|
|
const approvedVideo = Boolean(channelClaimId) && adApprovedChannelIds.includes(channelClaimId);
|
|
|
|
const adsEnabled = ENABLE_PREROLL_ADS && !authenticated && !embedded && approvedVideo;
|
2021-04-26 22:28:25 +02:00
|
|
|
const [adUrl, setAdUrl, isFetchingAd] = useGetAds(approvedVideo, adsEnabled);
|
2020-04-26 22:32:39 +02: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 */
|
2021-09-02 22:05:32 +02:00
|
|
|
const [replay, setReplay] = useState(false);
|
|
|
|
const [videoNode, setVideoNode] = useState();
|
2022-02-07 19:35:17 +01:00
|
|
|
const [localAutoplayNext, setLocalAutoplayNext] = useState(autoplayNext);
|
|
|
|
const isFirstRender = React.useRef(true);
|
2022-03-03 05:12:11 +01:00
|
|
|
const playerRef = React.useRef(null);
|
2022-02-07 19:35:17 +01:00
|
|
|
|
2022-05-17 08:57:14 +02:00
|
|
|
const addAutoplayNextButton = useAutoplayNext(playerRef, autoplayNext);
|
2022-05-17 09:10:28 +02:00
|
|
|
const addTheaterModeButton = useTheaterMode(playerRef, videoTheaterMode);
|
2022-05-17 08:57:14 +02:00
|
|
|
|
2022-04-22 13:16:27 +02:00
|
|
|
React.useEffect(() => {
|
2022-05-04 12:24:13 +02:00
|
|
|
if (isPlaying) {
|
2022-06-10 18:18:58 +02:00
|
|
|
// save the updated watch time
|
2022-05-04 12:24:13 +02:00
|
|
|
doSetContentHistoryItem(claim.permanent_url);
|
2022-04-22 13:16:27 +02:00
|
|
|
}
|
|
|
|
}, [isPlaying]);
|
|
|
|
|
2022-02-07 19:35:17 +01:00
|
|
|
useEffect(() => {
|
|
|
|
if (isFirstRender.current) {
|
|
|
|
isFirstRender.current = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
toggleAutoplayNext();
|
|
|
|
}, [localAutoplayNext]);
|
2019-10-08 10:06:28 +02:00
|
|
|
|
2022-03-15 17:18:08 +01:00
|
|
|
useInterval(
|
|
|
|
() => {
|
|
|
|
if (playerRef.current && isPlaying && !isLivestreamClaim) {
|
|
|
|
handlePosition(playerRef.current);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
!isLivestreamClaim ? PLAY_POSITION_SAVE_INTERVAL_MS : null
|
|
|
|
);
|
2022-03-02 17:13:46 +01:00
|
|
|
|
2021-11-29 15:57:46 +01:00
|
|
|
const updateVolumeState = React.useCallback(
|
|
|
|
debounce((volume, muted) => {
|
|
|
|
changeVolume(volume);
|
|
|
|
changeMute(muted);
|
|
|
|
}, 500),
|
|
|
|
[]
|
|
|
|
);
|
|
|
|
|
2020-04-16 23:43:09 +02: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);
|
2021-09-03 00:39:40 +02:00
|
|
|
setIsEndedEmbed(false);
|
2020-04-16 23:43:09 +02:00
|
|
|
}
|
|
|
|
}, [uri, previousUri]);
|
|
|
|
|
2021-02-17 18:01:12 +01:00
|
|
|
// Update vjsCallbackDataRef (ensures videojs callbacks are not using stale values):
|
|
|
|
useEffect(() => {
|
|
|
|
vjsCallbackDataRef.current = {
|
|
|
|
embedded: embedded,
|
|
|
|
videoPlaybackRate: videoPlaybackRate,
|
|
|
|
};
|
2021-03-09 03:55:55 +01:00
|
|
|
}, [embedded, videoPlaybackRate]);
|
2021-02-17 18:01:12 +01:00
|
|
|
|
2021-09-02 22:05:32 +02:00
|
|
|
const doPlay = useCallback(
|
|
|
|
(playUri) => {
|
2022-06-10 18:18:58 +02:00
|
|
|
if (!playUri) return;
|
2021-09-10 19:27:21 +02:00
|
|
|
setDoNavigate(false);
|
2021-09-02 22:05:32 +02:00
|
|
|
if (!isFloating) {
|
2021-09-10 19:27:21 +02:00
|
|
|
const navigateUrl = formatLbryUrlForWeb(playUri);
|
|
|
|
push({
|
|
|
|
pathname: navigateUrl,
|
|
|
|
search: collectionId && generateListSearchUrlParams(collectionId),
|
|
|
|
state: { collectionId, forceAutoplay: true, hideFloatingPlayer: true },
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
doPlayUri(playUri, collectionId);
|
2021-09-02 22:05:32 +02:00
|
|
|
}
|
|
|
|
},
|
2021-09-10 19:27:21 +02:00
|
|
|
[collectionId, doPlayUri, isFloating, push]
|
2021-09-02 22:05:32 +02:00
|
|
|
);
|
2021-08-10 22:42:50 +02:00
|
|
|
|
2022-06-10 20:48:48 +02:00
|
|
|
/** handle play next/play previous buttons **/
|
2021-09-02 22:05:32 +02:00
|
|
|
useEffect(() => {
|
2021-09-13 17:24:35 +02:00
|
|
|
if (!doNavigate) return;
|
|
|
|
|
2022-06-10 20:48:48 +02:00
|
|
|
// playNextUrl is set (either true or false) when the Next/Previous buttons are clicked
|
2022-06-10 18:18:58 +02:00
|
|
|
const shouldPlayNextUrl = playNextUrl && nextRecommendedUri && permanentUrl !== nextRecommendedUri;
|
|
|
|
const shouldPlayPreviousUrl = !playNextUrl && previousListUri && permanentUrl !== previousListUri;
|
|
|
|
|
|
|
|
// play next video if someone hits Next button
|
|
|
|
if (shouldPlayNextUrl) {
|
|
|
|
doPlay(nextRecommendedUri);
|
|
|
|
// rewind if video is over 5 seconds and they hit the back button
|
|
|
|
} else if (videoNode && videoNode.currentTime > 5) {
|
|
|
|
videoNode.currentTime = 0;
|
|
|
|
// move to previous video when they hit back button if behind 5 seconds
|
|
|
|
} else if (shouldPlayPreviousUrl) {
|
|
|
|
doPlay(previousListUri);
|
2021-09-13 17:24:35 +02:00
|
|
|
} else {
|
2022-06-10 18:18:58 +02:00
|
|
|
setReplay(true);
|
2021-04-12 18:43:47 +02:00
|
|
|
}
|
2022-06-10 18:18:58 +02:00
|
|
|
|
|
|
|
setDoNavigate(false);
|
2021-09-13 17:24:35 +02:00
|
|
|
setEnded(false);
|
|
|
|
setPlayNextUrl(true);
|
2021-09-10 19:27:21 +02:00
|
|
|
}, [
|
|
|
|
collectionId,
|
|
|
|
doNavigate,
|
|
|
|
doPlay,
|
|
|
|
ended,
|
|
|
|
nextRecommendedUri,
|
|
|
|
permanentUrl,
|
|
|
|
playNextUrl,
|
|
|
|
previousListUri,
|
|
|
|
videoNode,
|
|
|
|
]);
|
2021-04-12 18:43:47 +02:00
|
|
|
|
2022-06-10 18:18:58 +02:00
|
|
|
// functionality to run on video end
|
2021-09-02 22:05:32 +02:00
|
|
|
React.useEffect(() => {
|
2021-09-13 17:24:35 +02:00
|
|
|
if (!ended) return;
|
2021-09-02 22:05:32 +02:00
|
|
|
|
2021-09-13 17:24:35 +02:00
|
|
|
analytics.videoIsPlaying(false);
|
2021-09-02 22:05:32 +02:00
|
|
|
|
2021-09-13 17:24:35 +02:00
|
|
|
if (adUrl) {
|
|
|
|
setAdUrl(null);
|
|
|
|
return;
|
|
|
|
}
|
2021-06-30 18:29:00 +02:00
|
|
|
|
2021-09-13 17:24:35 +02:00
|
|
|
if (embedded) {
|
|
|
|
setIsEndedEmbed(true);
|
2022-06-10 18:18:58 +02:00
|
|
|
// show autoplay countdown div if not playlist
|
2021-09-13 17:24:35 +02:00
|
|
|
} else if (!collectionId && autoplayNext) {
|
|
|
|
setShowAutoplayCountdown(true);
|
2022-06-10 18:18:58 +02:00
|
|
|
// if a playlist, navigate to next item
|
2021-09-13 17:24:35 +02:00
|
|
|
} else if (collectionId) {
|
|
|
|
setDoNavigate(true);
|
2021-09-02 22:05:32 +02:00
|
|
|
}
|
2021-09-13 17:24:35 +02:00
|
|
|
|
|
|
|
clearPosition(uri);
|
2022-06-10 18:18:58 +02:00
|
|
|
|
|
|
|
if (IS_IOS && !autoplayNext) {
|
|
|
|
// show play button on ios if video is paused with no autoplay on
|
|
|
|
// $FlowFixMe
|
|
|
|
document.querySelector('.vjs-touch-overlay')?.classList.add('show-play-toggle'); // eslint-disable-line no-unused-expressions
|
|
|
|
}
|
2021-09-13 17:24:35 +02:00
|
|
|
}, [adUrl, autoplayNext, clearPosition, collectionId, embedded, ended, setAdUrl, uri]);
|
2020-04-08 19:10:33 +02:00
|
|
|
|
2021-10-13 17:04:03 +02:00
|
|
|
// MORE ON PLAY STUFF
|
2021-06-29 03:51:04 +02:00
|
|
|
function onPlay(player) {
|
2021-09-02 22:05:32 +02:00
|
|
|
setEnded(false);
|
2020-04-16 01:21:17 +02:00
|
|
|
setIsPlaying(true);
|
|
|
|
setShowAutoplayCountdown(false);
|
2021-09-03 00:39:40 +02:00
|
|
|
setIsEndedEmbed(false);
|
2021-09-02 22:05:32 +02:00
|
|
|
setReplay(false);
|
|
|
|
setDoNavigate(false);
|
2021-08-10 22:42:50 +02:00
|
|
|
analytics.videoIsPlaying(true, player);
|
2020-04-16 01:21:17 +02:00
|
|
|
}
|
2020-04-08 19:10:33 +02:00
|
|
|
|
2021-06-30 18:29:00 +02:00
|
|
|
function onPause(event, player) {
|
2021-06-29 03:51:04 +02:00
|
|
|
setIsPlaying(false);
|
|
|
|
handlePosition(player);
|
2021-08-10 22:42:50 +02:00
|
|
|
analytics.videoIsPlaying(false, player);
|
2021-06-29 03:51:04 +02:00
|
|
|
}
|
|
|
|
|
2022-06-10 18:18:58 +02:00
|
|
|
function onPlayerClosed(event, player) {
|
2021-06-29 03:51:04 +02:00
|
|
|
handlePosition(player);
|
2021-08-10 22:42:50 +02:00
|
|
|
analytics.videoIsPlaying(false, player);
|
2021-06-29 03:51:04 +02:00
|
|
|
}
|
|
|
|
|
2020-05-15 03:18:54 +02:00
|
|
|
function handlePosition(player) {
|
2022-03-15 17:18:08 +01:00
|
|
|
if (!isLivestreamClaim) savePosition(uri, player.currentTime());
|
2020-05-15 03:18:54 +02:00
|
|
|
}
|
|
|
|
|
2021-03-09 03:55:55 +01:00
|
|
|
function restorePlaybackRate(player) {
|
2021-02-17 18:01:12 +01:00
|
|
|
if (!vjsCallbackDataRef.current.embedded) {
|
|
|
|
player.playbackRate(vjsCallbackDataRef.current.videoPlaybackRate);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-12 18:43:47 +02:00
|
|
|
const playerReadyDependencyList = [uri, adUrl, embedded, autoplayIfEmbedded];
|
|
|
|
|
2021-09-02 22:05:32 +02:00
|
|
|
const doPlayNext = () => {
|
|
|
|
setPlayNextUrl(true);
|
2021-09-10 19:27:21 +02:00
|
|
|
setEnded(true);
|
2021-09-02 22:05:32 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const doPlayPrevious = () => {
|
|
|
|
setPlayNextUrl(false);
|
2021-09-10 19:27:21 +02:00
|
|
|
setEnded(true);
|
2021-09-02 22:05:32 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const onPlayerReady = useCallback((player: Player, videoNode: any) => {
|
2022-06-10 18:18:58 +02:00
|
|
|
// add buttons and initialize some settings for the player
|
2021-04-12 18:43:47 +02:00
|
|
|
if (!embedded) {
|
2021-09-02 22:05:32 +02:00
|
|
|
setVideoNode(videoNode);
|
2021-04-12 18:43:47 +02:00
|
|
|
player.muted(muted);
|
|
|
|
player.volume(volume);
|
|
|
|
player.playbackRate(videoPlaybackRate);
|
2021-09-13 17:24:35 +02:00
|
|
|
if (!isMarkdownOrComment) {
|
|
|
|
addTheaterModeButton(player, toggleVideoTheaterMode);
|
2022-06-10 18:18:58 +02:00
|
|
|
// if part of a playlist
|
|
|
|
|
|
|
|
// remove old play next/previous buttons if they exist
|
|
|
|
const controlBar = player.controlBar;
|
|
|
|
if (controlBar) {
|
|
|
|
const existingPlayNextButton = controlBar.getChild('PlayNextButton');
|
|
|
|
if (existingPlayNextButton) controlBar.removeChild('PlayNextButton');
|
|
|
|
|
|
|
|
const existingPlayPreviousButton = controlBar.getChild('PlayPreviousButton');
|
|
|
|
if (existingPlayPreviousButton) controlBar.removeChild('PlayPreviousButton');
|
|
|
|
|
|
|
|
const existingAutoplayButton = controlBar.getChild('AutoplayNextButton');
|
|
|
|
if (existingAutoplayButton) controlBar.removeChild('AutoplayNextButton');
|
|
|
|
}
|
|
|
|
|
2021-09-13 17:24:35 +02:00
|
|
|
if (collectionId) {
|
|
|
|
addPlayNextButton(player, doPlayNext);
|
|
|
|
addPlayPreviousButton(player, doPlayPrevious);
|
|
|
|
} else {
|
2022-02-07 19:35:17 +01:00
|
|
|
addAutoplayNextButton(
|
|
|
|
player,
|
|
|
|
() => {
|
|
|
|
setLocalAutoplayNext((e) => !e);
|
|
|
|
},
|
|
|
|
autoplayNext
|
|
|
|
);
|
2021-09-13 17:24:35 +02:00
|
|
|
}
|
2021-09-02 22:05:32 +02:00
|
|
|
}
|
2021-04-12 18:43:47 +02:00
|
|
|
}
|
|
|
|
// PR: #5535
|
|
|
|
// 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.
|
2022-06-10 18:18:58 +02:00
|
|
|
const restorePlaybackRateEvent = () => restorePlaybackRate(player);
|
2021-04-12 18:43:47 +02:00
|
|
|
|
2022-04-22 14:04:14 +02:00
|
|
|
// Override the "auto" algorithm to post-process the result
|
2022-06-10 18:18:58 +02:00
|
|
|
const overrideAutoAlgorithm = () => {
|
2022-04-22 13:35:30 +02:00
|
|
|
const vhs = player.tech(true).vhs;
|
|
|
|
if (vhs) {
|
|
|
|
// https://github.com/videojs/http-streaming/issues/749#issuecomment-606972884
|
|
|
|
vhs.selectPlaylist = lastBandwidthSelector;
|
|
|
|
}
|
2022-06-10 18:18:58 +02:00
|
|
|
};
|
2021-08-10 22:42:50 +02:00
|
|
|
|
2022-06-10 18:18:58 +02:00
|
|
|
const onPauseEvent = (event) => onPause(event, player);
|
|
|
|
const onPlayerClosedEvent = (event) => onPlayerClosed(event, player);
|
|
|
|
const onVolumeChange = () => {
|
|
|
|
if (player) {
|
|
|
|
updateVolumeState(player.volume(), player.muted());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
const onPlayerEnded = () => setEnded(true);
|
|
|
|
const onError = () => {
|
2021-04-12 18:43:47 +02:00
|
|
|
const error = player.error();
|
|
|
|
if (error) {
|
|
|
|
analytics.sentryError('Video.js error', error);
|
2020-04-16 23:43:09 +02:00
|
|
|
}
|
2022-06-10 18:18:58 +02:00
|
|
|
};
|
|
|
|
const onRateChange = () => {
|
2021-04-12 18:43:47 +02: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.
|
|
|
|
setVideoPlaybackRate(player.playbackRate());
|
|
|
|
}
|
2022-06-10 18:18:58 +02:00
|
|
|
};
|
2021-04-12 18:43:47 +02:00
|
|
|
|
2022-06-10 18:18:58 +02:00
|
|
|
const moveToPosition = () => {
|
|
|
|
// update current time based on previous position
|
|
|
|
if (position && !isLivestreamClaim) {
|
|
|
|
player.currentTime(position);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// load events onto player
|
|
|
|
player.on('play', onPlay);
|
|
|
|
player.on('pause', onPauseEvent);
|
|
|
|
player.on('playerClosed', onPlayerClosedEvent);
|
|
|
|
player.on('ended', onPlayerEnded);
|
|
|
|
player.on('error', onError);
|
|
|
|
player.on('volumechange', onVolumeChange);
|
|
|
|
player.on('ratechange', onRateChange);
|
|
|
|
player.on('loadedmetadata', overrideAutoAlgorithm);
|
|
|
|
player.on('loadedmetadata', restorePlaybackRateEvent);
|
|
|
|
player.one('loadedmetadata', moveToPosition);
|
|
|
|
|
|
|
|
const cancelOldEvents = () => {
|
|
|
|
player.off('play', onPlay);
|
|
|
|
player.off('pause', onPauseEvent);
|
|
|
|
player.off('playerClosed', onPlayerClosedEvent);
|
|
|
|
player.off('ended', onPlayerEnded);
|
|
|
|
player.off('error', onError);
|
|
|
|
player.off('volumechange', onVolumeChange);
|
|
|
|
player.off('ratechange', onRateChange);
|
|
|
|
player.off('loadedmetadata', overrideAutoAlgorithm);
|
|
|
|
player.off('loadedmetadata', restorePlaybackRateEvent);
|
|
|
|
player.off('playerClosed', cancelOldEvents);
|
|
|
|
player.off('loadedmetadata', moveToPosition);
|
|
|
|
};
|
|
|
|
|
|
|
|
// turn off old events to prevent duplicate runs
|
|
|
|
player.on('playerClosed', cancelOldEvents);
|
2022-03-02 17:13:46 +01:00
|
|
|
|
2022-05-02 10:55:08 +02:00
|
|
|
Chapters.parseAndLoad(player, claim);
|
|
|
|
|
2022-03-03 05:12:11 +01:00
|
|
|
playerRef.current = player;
|
2021-06-29 03:51:04 +02:00
|
|
|
}, playerReadyDependencyList); // eslint-disable-line
|
2020-03-19 21:25:37 +01:00
|
|
|
|
2019-08-02 08:28:14 +02:00
|
|
|
return (
|
2020-04-14 01:48:11 +02:00
|
|
|
<div
|
|
|
|
className={classnames('file-viewer', {
|
|
|
|
'file-viewer--is-playing': isPlaying,
|
2021-09-03 00:39:40 +02:00
|
|
|
'file-viewer--ended-embed': isEndedEmbed,
|
2020-04-14 01:48:11 +02:00
|
|
|
})}
|
|
|
|
onContextMenu={stopContextMenu}
|
|
|
|
>
|
2021-09-02 22:05:32 +02:00
|
|
|
{showAutoplayCountdown && (
|
|
|
|
<AutoplayCountdown
|
|
|
|
nextRecommendedUri={nextRecommendedUri}
|
|
|
|
doNavigate={() => setDoNavigate(true)}
|
|
|
|
doReplay={() => setReplay(true)}
|
|
|
|
/>
|
|
|
|
)}
|
2021-09-03 00:39:40 +02:00
|
|
|
{isEndedEmbed && <FileViewerEmbeddedEnded uri={uri} />}
|
|
|
|
{embedded && !isEndedEmbed && <FileViewerEmbeddedTitle uri={uri} />}
|
2021-04-12 18:43:47 +02:00
|
|
|
|
|
|
|
{!isFetchingAd && adUrl && (
|
|
|
|
<>
|
|
|
|
<span className="ads__video-notify">
|
|
|
|
{__('Advertisement')}{' '}
|
|
|
|
<Button
|
|
|
|
className="ads__video-close"
|
|
|
|
icon={ICONS.REMOVE}
|
|
|
|
title={__('Close')}
|
|
|
|
onClick={() => setAdUrl(null)}
|
|
|
|
/>
|
|
|
|
</span>
|
|
|
|
<span className="ads__video-nudge">
|
|
|
|
<I18nMessage
|
|
|
|
tokens={{
|
|
|
|
sign_up: (
|
|
|
|
<Button
|
|
|
|
button="secondary"
|
|
|
|
className="ads__video-link"
|
|
|
|
label={__('Sign Up')}
|
|
|
|
navigate={`/$/${PAGES.AUTH}?redirect=${pathname}&src=video-ad`}
|
|
|
|
/>
|
|
|
|
),
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
%sign_up% to turn ads off.
|
|
|
|
</I18nMessage>
|
|
|
|
</span>
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
|
2021-10-13 17:04:03 +02:00
|
|
|
<VideoJs
|
|
|
|
adUrl={adUrl}
|
|
|
|
source={adUrl || source}
|
|
|
|
sourceType={forcePlayer || adUrl ? 'video/mp4' : contentType}
|
|
|
|
isAudio={isAudio}
|
|
|
|
poster={isAudio || (embedded && !autoplayIfEmbedded) ? thumbnail : ''}
|
|
|
|
onPlayerReady={onPlayerReady}
|
|
|
|
startMuted={autoplayIfEmbedded}
|
|
|
|
toggleVideoTheaterMode={toggleVideoTheaterMode}
|
|
|
|
autoplay={!embedded || autoplayIfEmbedded}
|
|
|
|
claimId={claimId}
|
2021-12-07 15:26:31 +01:00
|
|
|
title={claim && ((claim.value && claim.value.title) || claim.name)}
|
2022-05-29 11:54:47 +02:00
|
|
|
channelTitle={channelTitle}
|
2021-10-13 17:04:03 +02:00
|
|
|
userId={userId}
|
2021-11-15 16:01:42 +01:00
|
|
|
allowPreRoll={!authenticated} // TODO: pull this into ads functionality so it's self contained
|
2021-10-13 17:04:03 +02:00
|
|
|
internalFeatureEnabled={internalFeature}
|
|
|
|
shareTelemetry={shareTelemetry}
|
|
|
|
replay={replay}
|
|
|
|
playNext={doPlayNext}
|
|
|
|
playPrevious={doPlayPrevious}
|
|
|
|
embedded={embedded}
|
2022-01-06 20:28:27 +01:00
|
|
|
claimValues={claim.value}
|
|
|
|
doAnalyticsView={doAnalyticsView}
|
2022-05-17 16:47:44 +02:00
|
|
|
doAnalyticsBuffer={doAnalyticsBuffer}
|
2022-01-06 20:28:27 +01:00
|
|
|
claimRewards={claimRewards}
|
|
|
|
uri={uri}
|
2022-03-15 17:18:08 +01:00
|
|
|
userClaimId={claim && claim.signing_channel && claim.signing_channel.claim_id}
|
|
|
|
isLivestreamClaim={isLivestreamClaim}
|
|
|
|
activeLivestreamForChannel={activeLivestreamForChannel}
|
2022-04-19 18:55:32 +02:00
|
|
|
defaultQuality={defaultQuality}
|
2022-04-25 15:28:36 +02:00
|
|
|
doToast={doToast}
|
2021-10-13 17:04:03 +02:00
|
|
|
/>
|
2019-08-02 08:28:14 +02:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export default VideoViewer;
|