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

236 lines
5.9 KiB
React
Raw Normal View History

import React, { Component } from 'react';
import injectWbResizeEvent from '/imports/ui/components/presentation/resize-wrapper/component';
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 SYNC_INTERVAL_SECONDS = 2;
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.state = {
mutedByEchoTest: false,
playing: false,
playbackRate: 1,
};
this.opts = {
controls: isPresenter,
youtube: {
playerVars: {
autoplay: 1,
modestbranding: 1,
autohide: 1,
rel: 0,
ecver: 2,
2019-09-11 02:04:42 +08:00
controls: isPresenter ? 1 : 2,
},
},
preload: true,
};
this.registerVideoListeners = this.registerVideoListeners.bind(this);
this.clearVideoListeners = this.clearVideoListeners.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.resizeListener = () => {
setTimeout(this.handleResize, 0);
};
}
componentDidMount() {
window.addEventListener('resize', this.resizeListener);
2019-07-13 04:08:55 +08:00
clearInterval(this.syncInterval);
this.registerVideoListeners();
}
componentWillUnmount() {
window.removeEventListener('resize', this.resizeListener);
this.clearVideoListeners();
clearInterval(this.syncInterval);
this.player = null;
}
static getDerivedStateFromProps(props) {
const { inEchoTest } = props;
return { mutedByEchoTest: inEchoTest };
}
2019-09-11 02:04:42 +08:00
getCurrentPlaybackRate() {
const intPlayer = this.player.getInternalPlayer();
return (intPlayer && intPlayer.getPlaybackRate && intPlayer.getPlaybackRate()) || 1;
}
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');
}
registerVideoListeners() {
const { isPresenter } = this.props;
2019-09-11 02:04:42 +08:00
const { playing } = this.state;
if (isPresenter) {
this.syncInterval = setInterval(() => {
const curTime = this.player.getCurrentTime();
const rate = this.getCurrentPlaybackRate();
2019-09-11 02:04:42 +08:00
sendMessage('playerUpdate', { rate, time: curTime, state: playing });
}, SYNC_INTERVAL_SECONDS * 1000);
} else {
onMessage('play', ({ time }) => {
2019-07-13 04:08:55 +08:00
if (!this.player) {
return;
}
2019-07-13 04:08:55 +08:00
this.player.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');
});
onMessage('stop', ({ time }) => {
2019-07-13 04:08:55 +08:00
if (!this.player) {
return;
}
2019-07-13 04:08:55 +08:00
this.player.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('playerUpdate', (data) => {
if (!this.player) {
return;
}
if (data.rate !== this.player.props.playbackRate) {
2019-09-11 02:04:42 +08:00
this.setState({ playbackRate: data.rate });
logger.debug({
logCode: 'external_video_client_update_rate',
extraInfo: {
2019-07-03 00:54:10 +08:00
newRate: data.rate,
},
2019-07-03 00:54:10 +08:00
}, 'Change external video playback rate.');
}
if (Math.abs(this.player.getCurrentTime() - data.time) > SYNC_INTERVAL_SECONDS) {
this.player.seekTo(data.time, true);
logger.debug({
logCode: 'external_video_client_update_seek',
extraInfo: {
time: data.time,
},
}, 'Seek external video to:');
}
2019-09-11 02:04:42 +08:00
if (playing !== data.state) {
this.setState({ playing: data.state });
}
});
}
}
2019-09-11 02:04:42 +08:00
handleOnReady() {
const { isPresenter } = this.props;
if (!isPresenter) {
sendMessage('viewerJoined');
}
this.handleResize();
}
2019-07-13 04:08:55 +08:00
handleOnPlay() {
const { isPresenter } = this.props;
const curTime = this.player.getCurrentTime();
2019-07-13 04:08:55 +08:00
if (isPresenter) {
sendMessage('play', { time: curTime });
}
2019-09-11 02:04:42 +08:00
this.setState({ playing: true });
2019-07-13 04:08:55 +08:00
}
2019-07-13 04:08:55 +08:00
handleOnPause() {
const { isPresenter } = this.props;
const curTime = this.player.getCurrentTime();
2019-07-13 04:08:55 +08:00
if (isPresenter) {
sendMessage('stop', { time: curTime });
}
2019-09-11 02:04:42 +08:00
this.setState({ playing: false });
2018-12-11 07:07:20 +08:00
}
render() {
const { videoUrl } = this.props;
const { playing, playbackRate, mutedByEchoTest } = 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; }}
>
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 injectWbResizeEvent(VideoPlayer);