bigbluebutton-Github/bigbluebutton-html5/imports/ui/components/external-video-player/component.jsx

447 lines
12 KiB
React
Raw Normal View History

import React, { Component } from 'react';
import injectWbResizeEvent from '/imports/ui/components/presentation/resize-wrapper/component';
import { defineMessages, injectIntl } from 'react-intl';
2019-09-11 02:04:42 +08:00
import ReactPlayer from 'react-player';
import { sendMessage, onMessage, removeAllListeners } from './service';
import logger from '/imports/startup/client/logger';
2019-07-27 04:59:14 +08:00
import ArcPlayer from './custom-players/arc-player';
import { styles } from './styles';
const intlMessages = defineMessages({
autoPlayWarning: {
id: 'app.externalVideo.autoPlayWarning',
description: 'Shown when user needs to interact with player to make it work',
},
});
const SYNC_INTERVAL_SECONDS = 5;
const THROTTLE_INTERVAL_SECONDS = 0.5;
const AUTO_PLAY_BLOCK_DETECTION_TIMEOUT_SECONDS = 5;
2019-07-27 04:59:14 +08:00
ReactPlayer.addCustomPlayer(ArcPlayer);
class VideoPlayer extends Component {
constructor(props) {
super(props);
2019-09-11 02:04:42 +08:00
const { isPresenter } = props;
this.player = null;
this.syncInterval = null;
this.autoPlayTimeout = null;
this.hasPlayedBefore = false;
this.playerIsReady = false;
this.lastMessage = null;
this.lastMessageTimestamp = Date.now();
this.throttleTimeout = null;
this.state = {
mutedByEchoTest: false,
playing: false,
autoPlayBlocked: false,
playbackRate: 1,
};
this.opts = {
// default option for all players, can be overwritten
playerOptions: {
autoplay: true,
playsinline: true,
controls: true,
},
file: {
attributes: {
controls: true,
},
},
dailymotion: {
params: {
controls: true,
},
},
youtube: {
playerVars: {
autoplay: 1,
modestbranding: 1,
autohide: 1,
rel: 0,
ecver: 2,
2019-09-11 02:04:42 +08:00
controls: isPresenter ? 1 : 2,
},
},
twitch: {
options: {
controls: true,
},
player: 'twitchPlayerId',
},
preload: true,
};
this.registerVideoListeners = this.registerVideoListeners.bind(this);
this.autoPlayBlockDetected = this.autoPlayBlockDetected.bind(this);
this.clearVideoListeners = this.clearVideoListeners.bind(this);
this.handleFirstPlay = this.handleFirstPlay.bind(this);
this.handleResize = this.handleResize.bind(this);
this.handleOnReady = this.handleOnReady.bind(this);
2019-07-13 04:08:55 +08:00
this.handleOnPlay = this.handleOnPlay.bind(this);
this.handleOnPause = this.handleOnPause.bind(this);
this.sendSyncMessage = this.sendSyncMessage.bind(this);
this.getCurrentPlaybackRate = this.getCurrentPlaybackRate.bind(this);
this.getCurrentTime = this.getCurrentTime.bind(this);
this.setPlaybackRate = this.setPlaybackRate.bind(this);
this.resizeListener = () => {
setTimeout(this.handleResize, 0);
};
this.onBeforeUnload = this.onBeforeUnload.bind(this);
}
componentDidMount() {
window.addEventListener('resize', this.resizeListener);
window.addEventListener('beforeunload', this.onBeforeUnload);
2019-07-13 04:08:55 +08:00
clearInterval(this.syncInterval);
clearTimeout(this.autoPlayTimeout);
this.clearVideoListeners();
this.registerVideoListeners();
}
onBeforeUnload() {
const { isPresenter } = this.props;
if (isPresenter) {
this.sendSyncMessage('stop');
}
}
componentWillUnmount() {
window.removeEventListener('resize', this.resizeListener);
window.removeEventListener('beforeunload', this.onBeforeUnload);
this.clearVideoListeners();
clearInterval(this.syncInterval);
clearTimeout(this.autoPlayTimeout);
this.player = null;
}
componentDidUpdate(prevProp, prevState) {
// Detect presenter change and redo the sync and listeners to reassign video to the new one
if (this.props.isPresenter !== prevProp.isPresenter) {
this.clearVideoListeners();
clearInterval(this.syncInterval);
clearTimeout(this.autoPlayTimeout);
this.registerVideoListeners();
}
}
shouldComponentUpdate(nextProps, nextState) {
const { isPresenter } = this.props;
const { playing } = this.state;
// If user is presenter we don't re-render playing state changes
// Because he's in control of the play/pause status
if (nextProps.isPresenter && isPresenter && nextState.playing !== playing) {
return false;
}
return true;
}
static getDerivedStateFromProps(props) {
const { inEchoTest } = props;
return { mutedByEchoTest: inEchoTest };
}
sendSyncMessage(msg, params) {
const timestamp = Date.now();
// If message is just a quick pause/un-pause just send nothing
const sinceLastMessage = (timestamp - this.lastMessageTimestamp)/1000;
if ((msg === 'play' && this.lastMessage === 'stop' ||
msg === 'stop' && this.lastMessage === 'play') &&
sinceLastMessage < THROTTLE_INTERVAL_SECONDS) {
return clearTimeout(this.throttleTimeout);
}
// Ignore repeat presenter ready messages
if (this.lastMessage === msg && msg === 'presenterReady') {
logger.debug("Ignoring a repeated presenterReady message");
} else {
// Play/pause messages are sent with a delay, to permit cancelling it in case of
// quick sucessive play/pauses
const messageDelay = (msg === 'play' || msg === 'stop') ? THROTTLE_INTERVAL_SECONDS : 0;
this.throttleTimeout = setTimeout(() => {
2020-05-19 00:16:03 +08:00
sendMessage(msg, { ...params });
}, messageDelay*1000);
this.lastMessage = msg;
this.lastMessageTimestamp = timestamp;
}
}
autoPlayBlockDetected() {
this.setState({ autoPlayBlocked: true });
}
handleFirstPlay() {
const { isPresenter } = this.props;
const { hasPlayedBefore } = this;
if (!hasPlayedBefore) {
this.hasPlayedBefore = true;
this.setState({ autoPlayBlocked: false });
clearTimeout(this.autoPlayTimeout);
if (isPresenter) {
this.sendSyncMessage('presenterReady');
}
}
}
getCurrentTime() {
if (this.player && this.player.getCurrentTime) {
return Math.round(this.player.getCurrentTime());
}
}
2019-09-11 02:04:42 +08:00
getCurrentPlaybackRate() {
const intPlayer = this.player && this.player.getInternalPlayer();
2019-09-11 02:04:42 +08:00
return (intPlayer && intPlayer.getPlaybackRate && intPlayer.getPlaybackRate()) || 1;
}
setPlaybackRate(rate) {
const intPlayer = this.player && this.player.getInternalPlayer();
const currentRate = this.getCurrentPlaybackRate();
if (currentRate === rate) {
return;
}
this.setState({ playbackRate: rate });
if (intPlayer && intPlayer.setPlaybackRate) {
intPlayer.setPlaybackRate(rate);
}
}
handleResize() {
2019-07-13 04:08:55 +08:00
if (!this.player || !this.playerParent) {
return;
}
2019-07-13 04:08:55 +08:00
const par = this.playerParent.parentElement;
const w = par.clientWidth;
const h = par.clientHeight;
const idealW = h * 16 / 9;
2019-09-11 02:04:42 +08:00
const style = {};
if (idealW > w) {
2019-07-13 04:08:55 +08:00
style.width = w;
style.height = w * 9 / 16;
} else {
2019-07-13 04:08:55 +08:00
style.width = idealW;
style.height = h;
}
2019-07-13 04:08:55 +08:00
2019-09-11 02:04:42 +08:00
const styleStr = `width: ${style.width}px; height: ${style.height}px;`;
2019-07-13 04:08:55 +08:00
this.player.wrapper.style = styleStr;
this.playerParent.style = styleStr;
}
clearVideoListeners() {
removeAllListeners('play');
removeAllListeners('stop');
removeAllListeners('playerUpdate');
removeAllListeners('presenterReady');
}
registerVideoListeners() {
const { isPresenter } = this.props;
if (isPresenter) {
this.syncInterval = setInterval(() => {
const { playing } = this.state;
const curTime = this.getCurrentTime();
const rate = this.getCurrentPlaybackRate();
// Always pause video if presenter is has not started sharing, e.g., blocked by autoplay
const playingState = this.hasPlayedBefore ? playing : false;
this.sendSyncMessage('playerUpdate', { rate, time: curTime, state: playingState });
}, SYNC_INTERVAL_SECONDS * 1000);
} else {
2020-05-19 00:16:03 +08:00
onMessage('play', ({ time }) => {
const { hasPlayedBefore, player } = this;
if (!player || !hasPlayedBefore) {
2019-07-13 04:08:55 +08:00
return;
}
2020-05-19 00:16:03 +08:00
this.seekTo(time);
2019-09-11 02:04:42 +08:00
this.setState({ playing: true });
2019-07-13 04:08:55 +08:00
logger.debug({ logCode: 'external_video_client_play' }, 'Play external video');
});
2020-05-19 00:16:03 +08:00
onMessage('stop', ({ time }) => {
const { hasPlayedBefore, player } = this;
if (!player || !hasPlayedBefore) {
2019-07-13 04:08:55 +08:00
return;
}
2020-05-19 00:16:03 +08:00
this.seekTo(time);
2019-09-11 02:04:42 +08:00
this.setState({ playing: false });
2019-07-13 04:08:55 +08:00
logger.debug({ logCode: 'external_video_client_stop' }, 'Stop external video');
});
onMessage('presenterReady', (data) => {
const { hasPlayedBefore } = this;
logger.debug({ logCode: 'external_video_presenter_ready' }, 'Presenter is ready to sync');
if (!hasPlayedBefore) {
this.setState({ playing: true });
}
});
onMessage('playerUpdate', (data) => {
const { hasPlayedBefore, player } = this;
const { playing } = this.state;
2020-05-19 00:16:03 +08:00
const { time, rate, state } = data;
if (!player || !hasPlayedBefore) {
return;
}
if (rate !== this.getCurrentPlaybackRate()) {
this.setPlaybackRate(rate);
logger.debug({
logCode: 'external_video_client_update_rate',
extraInfo: {
newRate: rate,
},
2019-07-03 00:54:10 +08:00
}, 'Change external video playback rate.');
}
2020-05-19 00:16:03 +08:00
this.seekTo(time);
if (playing !== state) {
this.setState({ playing: state });
}
});
}
}
2020-05-19 00:16:03 +08:00
seekTo(time) {
const { player } = this;
if (!player) {
return logger.error("No player on seek");
}
// Seek if viewer has drifted too far away from presenter
2020-05-19 00:16:03 +08:00
if (Math.abs(this.getCurrentTime() - time) > SYNC_INTERVAL_SECONDS*0.75) {
player.seekTo(time, true);
logger.debug({
logCode: 'external_video_client_update_seek',
2020-05-19 00:16:03 +08:00
extraInfo: { time, },
}, `Seek external video to: ${time}`);
}
}
2019-09-11 02:04:42 +08:00
handleOnReady() {
const { hasPlayedBefore, playerIsReady } = this;
if (hasPlayedBefore || playerIsReady) {
return;
}
this.playerIsReady = true;
this.handleResize();
this.autoPlayTimeout = setTimeout(this.autoPlayBlockDetected, AUTO_PLAY_BLOCK_DETECTION_TIMEOUT_SECONDS * 1000);
}
2019-07-13 04:08:55 +08:00
handleOnPlay() {
const { isPresenter } = this.props;
const { playing } = this.state;
const curTime = this.getCurrentTime();
if (isPresenter && !playing) {
this.sendSyncMessage('play', { time: curTime });
}
2019-09-11 02:04:42 +08:00
this.setState({ playing: true });
this.handleFirstPlay();
2019-07-13 04:08:55 +08:00
}
2019-07-13 04:08:55 +08:00
handleOnPause() {
const { isPresenter } = this.props;
const { playing } = this.state;
const curTime = this.getCurrentTime();
if (isPresenter && playing) {
this.sendSyncMessage('stop', { time: curTime });
}
2019-09-11 02:04:42 +08:00
this.setState({ playing: false });
this.handleFirstPlay();
2018-12-11 07:07:20 +08:00
}
render() {
const { videoUrl, intl } = this.props;
const {
playing, playbackRate, mutedByEchoTest, autoPlayBlocked,
} = this.state;
return (
<div
2019-07-13 04:08:55 +08:00
id="video-player"
data-test="videoPlayer"
2019-07-13 04:08:55 +08:00
ref={(ref) => { this.playerParent = ref; }}
>
{autoPlayBlocked
? (
<p className={styles.autoPlayWarning}>
{intl.formatMessage(intlMessages.autoPlayWarning)}
</p>
)
: ''
}
2019-07-13 04:08:55 +08:00
<ReactPlayer
className={styles.videoPlayer}
2019-07-13 04:08:55 +08:00
url={videoUrl}
config={this.opts}
2019-07-13 04:08:55 +08:00
muted={mutedByEchoTest}
playing={playing}
playbackRate={playbackRate}
2019-07-13 04:08:55 +08:00
onReady={this.handleOnReady}
onPlay={this.handleOnPlay}
onPause={this.handleOnPause}
ref={(ref) => { this.player = ref; }}
/>
</div>
);
}
}
export default injectIntl(injectWbResizeEvent(VideoPlayer));