2019-01-16 04:44:41 +08:00
|
|
|
import React, { Component } from 'react';
|
2018-11-30 01:24:02 +08:00
|
|
|
import injectWbResizeEvent from '/imports/ui/components/presentation/resize-wrapper/component';
|
2019-10-08 01:45:16 +08:00
|
|
|
import { defineMessages, injectIntl } from 'react-intl';
|
2019-09-11 02:04:42 +08:00
|
|
|
import ReactPlayer from 'react-player';
|
2019-09-11 03:35:58 +08:00
|
|
|
import { sendMessage, onMessage, removeAllListeners } from './service';
|
2019-05-30 03:06:41 +08:00
|
|
|
import logger from '/imports/startup/client/logger';
|
2018-11-30 01:24:02 +08:00
|
|
|
|
2019-07-27 04:59:14 +08:00
|
|
|
import ArcPlayer from './custom-players/arc-player';
|
|
|
|
|
2019-07-17 04:56:07 +08:00
|
|
|
import { styles } from './styles';
|
|
|
|
|
2019-10-08 01:45:16 +08:00
|
|
|
const intlMessages = defineMessages({
|
|
|
|
autoPlayWarning: {
|
|
|
|
id: 'app.externalVideo.autoPlayWarning',
|
|
|
|
description: 'Shown when user needs to interact with player to make it work',
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2019-05-30 03:06:41 +08:00
|
|
|
const SYNC_INTERVAL_SECONDS = 2;
|
2019-10-08 01:45:16 +08:00
|
|
|
const AUTO_PLAY_BLOCK_DETECTION_TIMEOUT_SECONDS = 5;
|
2019-05-30 03:06:41 +08:00
|
|
|
|
2019-07-27 04:59:14 +08:00
|
|
|
ReactPlayer.addCustomPlayer(ArcPlayer);
|
|
|
|
|
2018-11-30 01:24:02 +08:00
|
|
|
class VideoPlayer extends Component {
|
2019-01-16 04:44:41 +08:00
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
|
2019-09-11 02:04:42 +08:00
|
|
|
const { isPresenter } = props;
|
2019-07-16 04:12:07 +08:00
|
|
|
|
2019-01-16 04:44:41 +08:00
|
|
|
this.player = null;
|
|
|
|
this.syncInterval = null;
|
2019-10-08 01:45:16 +08:00
|
|
|
this.autoPlayTimeout = null;
|
2019-02-28 03:59:45 +08:00
|
|
|
this.state = {
|
|
|
|
mutedByEchoTest: false,
|
2019-07-16 04:12:07 +08:00
|
|
|
playing: false,
|
2019-10-08 01:45:16 +08:00
|
|
|
hasPlayedBefore: false,
|
|
|
|
autoPlayBlocked: false,
|
2019-07-17 06:04:20 +08:00
|
|
|
playbackRate: 1,
|
2019-02-28 03:59:45 +08:00
|
|
|
};
|
|
|
|
|
2019-01-16 04:44:41 +08:00
|
|
|
this.opts = {
|
2019-07-16 04:12:07 +08:00
|
|
|
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,
|
2019-07-16 04:12:07 +08:00
|
|
|
},
|
2019-01-16 04:44:41 +08:00
|
|
|
},
|
2019-07-16 04:12:07 +08:00
|
|
|
preload: true,
|
2019-01-16 04:44:41 +08:00
|
|
|
};
|
|
|
|
|
2019-09-11 03:35:58 +08:00
|
|
|
this.registerVideoListeners = this.registerVideoListeners.bind(this);
|
2019-10-08 01:45:16 +08:00
|
|
|
this.autoPlayBlockDetected = this.autoPlayBlockDetected.bind(this);
|
2019-09-11 03:35:58 +08:00
|
|
|
this.clearVideoListeners = this.clearVideoListeners.bind(this);
|
2019-01-16 04:44:41 +08:00
|
|
|
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);
|
2019-01-16 04:44:41 +08:00
|
|
|
this.resizeListener = () => {
|
|
|
|
setTimeout(this.handleResize, 0);
|
|
|
|
};
|
|
|
|
}
|
2018-11-30 01:24:02 +08:00
|
|
|
|
2019-01-16 04:58:59 +08:00
|
|
|
componentDidMount() {
|
|
|
|
window.addEventListener('resize', this.resizeListener);
|
2019-07-13 04:08:55 +08:00
|
|
|
|
|
|
|
clearInterval(this.syncInterval);
|
2019-09-11 03:35:58 +08:00
|
|
|
this.registerVideoListeners();
|
2019-01-16 04:58:59 +08:00
|
|
|
}
|
|
|
|
|
2018-11-30 01:24:02 +08:00
|
|
|
componentWillUnmount() {
|
2019-01-16 04:44:41 +08:00
|
|
|
window.removeEventListener('resize', this.resizeListener);
|
2019-09-11 03:35:58 +08:00
|
|
|
this.clearVideoListeners();
|
2018-11-30 01:24:02 +08:00
|
|
|
|
2019-01-16 04:44:41 +08:00
|
|
|
clearInterval(this.syncInterval);
|
2019-10-08 01:45:16 +08:00
|
|
|
clearTimeout(this.autoPlayTimeout);
|
2019-01-16 04:44:41 +08:00
|
|
|
this.player = null;
|
2019-02-28 03:59:45 +08:00
|
|
|
}
|
|
|
|
|
2019-09-13 01:15:35 +08:00
|
|
|
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);
|
|
|
|
this.registerVideoListeners();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-11 04:15:25 +08:00
|
|
|
static getDerivedStateFromProps(props) {
|
|
|
|
const { inEchoTest } = props;
|
|
|
|
|
|
|
|
return { mutedByEchoTest: inEchoTest };
|
|
|
|
}
|
|
|
|
|
2019-10-08 01:45:16 +08:00
|
|
|
autoPlayBlockDetected() {
|
|
|
|
this.setState({autoPlayBlocked: true});
|
|
|
|
}
|
|
|
|
|
2019-09-11 02:04:42 +08:00
|
|
|
getCurrentPlaybackRate() {
|
|
|
|
const intPlayer = this.player.getInternalPlayer();
|
|
|
|
|
|
|
|
return (intPlayer && intPlayer.getPlaybackRate && intPlayer.getPlaybackRate()) || 1;
|
|
|
|
}
|
|
|
|
|
2019-01-16 04:44:41 +08:00
|
|
|
handleResize() {
|
2019-07-13 04:08:55 +08:00
|
|
|
if (!this.player || !this.playerParent) {
|
2018-11-30 01:24:02 +08:00
|
|
|
return;
|
2019-01-16 04:44:41 +08:00
|
|
|
}
|
2018-11-30 01:24:02 +08:00
|
|
|
|
2019-07-13 04:08:55 +08:00
|
|
|
const par = this.playerParent.parentElement;
|
|
|
|
const w = par.clientWidth;
|
|
|
|
const h = par.clientHeight;
|
2019-01-16 04:44:41 +08:00
|
|
|
const idealW = h * 16 / 9;
|
2018-11-30 01:24:02 +08:00
|
|
|
|
2019-09-11 02:04:42 +08:00
|
|
|
const style = {};
|
2018-11-30 01:24:02 +08:00
|
|
|
if (idealW > w) {
|
2019-07-13 04:08:55 +08:00
|
|
|
style.width = w;
|
|
|
|
style.height = w * 9 / 16;
|
2018-11-30 01:24:02 +08:00
|
|
|
} else {
|
2019-07-13 04:08:55 +08:00
|
|
|
style.width = idealW;
|
|
|
|
style.height = h;
|
2018-11-30 01:24:02 +08:00
|
|
|
}
|
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;
|
2018-11-30 01:24:02 +08:00
|
|
|
}
|
|
|
|
|
2019-09-11 03:35:58 +08:00
|
|
|
clearVideoListeners() {
|
|
|
|
removeAllListeners('play');
|
|
|
|
removeAllListeners('stop');
|
|
|
|
removeAllListeners('playerUpdate');
|
|
|
|
}
|
|
|
|
|
|
|
|
registerVideoListeners() {
|
2019-01-16 04:44:41 +08:00
|
|
|
const { isPresenter } = this.props;
|
2018-11-30 01:24:02 +08:00
|
|
|
|
|
|
|
if (isPresenter) {
|
|
|
|
this.syncInterval = setInterval(() => {
|
2019-01-16 04:44:41 +08:00
|
|
|
const curTime = this.player.getCurrentTime();
|
2019-07-17 06:04:20 +08:00
|
|
|
const rate = this.getCurrentPlaybackRate();
|
|
|
|
|
2019-09-12 05:43:25 +08:00
|
|
|
sendMessage('playerUpdate', { rate, time: curTime, state: this.state.playing });
|
2019-05-30 03:06:41 +08:00
|
|
|
}, SYNC_INTERVAL_SECONDS * 1000);
|
2018-11-30 01:24:02 +08:00
|
|
|
} else {
|
2019-01-16 04:44:41 +08:00
|
|
|
onMessage('play', ({ time }) => {
|
2019-10-08 01:45:16 +08:00
|
|
|
if (!this.player || !this.state.hasPlayedBefore) {
|
2019-07-13 04:08:55 +08:00
|
|
|
return;
|
2018-11-30 01:24:02 +08:00
|
|
|
}
|
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
|
|
|
|
2019-05-30 23:02:51 +08:00
|
|
|
logger.debug({ logCode: 'external_video_client_play' }, 'Play external video');
|
2018-11-30 01:24:02 +08:00
|
|
|
});
|
|
|
|
|
2019-01-16 04:44:41 +08:00
|
|
|
onMessage('stop', ({ time }) => {
|
2019-10-08 01:45:16 +08:00
|
|
|
if (!this.player || !this.state.hasPlayedBefore) {
|
2019-07-13 04:08:55 +08:00
|
|
|
return;
|
2018-11-30 01:24:02 +08:00
|
|
|
}
|
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
|
|
|
|
2019-05-30 23:02:51 +08:00
|
|
|
logger.debug({ logCode: 'external_video_client_stop' }, 'Stop external video');
|
2018-11-30 01:24:02 +08:00
|
|
|
});
|
|
|
|
|
2019-01-16 04:44:41 +08:00
|
|
|
onMessage('playerUpdate', (data) => {
|
2019-10-08 01:45:16 +08:00
|
|
|
if (!this.player || !this.state.hasPlayedBefore) {
|
2018-11-30 01:24:02 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-07-17 06:04:20 +08:00
|
|
|
if (data.rate !== this.player.props.playbackRate) {
|
2019-09-11 02:04:42 +08:00
|
|
|
this.setState({ playbackRate: data.rate });
|
2019-06-29 05:45:50 +08:00
|
|
|
logger.debug({
|
|
|
|
logCode: 'external_video_client_update_rate',
|
|
|
|
extraInfo: {
|
2019-07-03 00:54:10 +08:00
|
|
|
newRate: data.rate,
|
2019-06-29 05:45:50 +08:00
|
|
|
},
|
2019-07-03 00:54:10 +08:00
|
|
|
}, 'Change external video playback rate.');
|
2018-11-30 01:24:02 +08:00
|
|
|
}
|
|
|
|
|
2019-05-30 03:06:41 +08:00
|
|
|
if (Math.abs(this.player.getCurrentTime() - data.time) > SYNC_INTERVAL_SECONDS) {
|
2018-11-30 01:24:02 +08:00
|
|
|
this.player.seekTo(data.time, true);
|
2019-06-29 05:45:50 +08:00
|
|
|
logger.debug({
|
|
|
|
logCode: 'external_video_client_update_seek',
|
|
|
|
extraInfo: {
|
|
|
|
time: data.time,
|
|
|
|
},
|
|
|
|
}, 'Seek external video to:');
|
2018-11-30 01:24:02 +08:00
|
|
|
}
|
|
|
|
|
2019-09-12 05:43:25 +08:00
|
|
|
if (this.state.playing !== data.state) {
|
2019-09-11 02:04:42 +08:00
|
|
|
this.setState({ playing: data.state });
|
2018-11-30 01:24:02 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-11 02:04:42 +08:00
|
|
|
handleOnReady() {
|
2019-01-16 04:44:41 +08:00
|
|
|
const { isPresenter } = this.props;
|
2018-11-30 01:24:02 +08:00
|
|
|
|
|
|
|
if (!isPresenter) {
|
2019-01-16 04:44:41 +08:00
|
|
|
sendMessage('viewerJoined');
|
2018-11-30 01:24:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
this.handleResize();
|
2019-10-08 01:45:16 +08:00
|
|
|
|
|
|
|
this.autoPlayTimeout = setTimeout(this.autoPlayBlockDetected, AUTO_PLAY_BLOCK_DETECTION_TIMEOUT_SECONDS * 1000);
|
|
|
|
|
|
|
|
this.setState({ playing: true });
|
2019-01-16 04:44:41 +08:00
|
|
|
}
|
2018-11-30 01:24:02 +08:00
|
|
|
|
2019-07-13 04:08:55 +08:00
|
|
|
handleOnPlay() {
|
2019-01-16 04:44:41 +08:00
|
|
|
const { isPresenter } = this.props;
|
|
|
|
const curTime = this.player.getCurrentTime();
|
2018-11-30 01:24:02 +08:00
|
|
|
|
2019-07-13 04:08:55 +08:00
|
|
|
if (isPresenter) {
|
|
|
|
sendMessage('play', { time: curTime });
|
2018-11-30 01:24:02 +08:00
|
|
|
}
|
2019-09-11 02:04:42 +08:00
|
|
|
this.setState({ playing: true });
|
2019-10-08 01:45:16 +08:00
|
|
|
|
|
|
|
if (!this.state.hasPlayedBefore) {
|
|
|
|
this.setState({ hasPlayedBefore: true, autoPlayBlocked: false });
|
|
|
|
clearTimeout(this.autoPlayTimeout);
|
|
|
|
}
|
2019-07-13 04:08:55 +08:00
|
|
|
}
|
2018-11-30 01:24:02 +08:00
|
|
|
|
2019-07-13 04:08:55 +08:00
|
|
|
handleOnPause() {
|
|
|
|
const { isPresenter } = this.props;
|
|
|
|
const curTime = this.player.getCurrentTime();
|
2018-11-30 01:24:02 +08:00
|
|
|
|
2019-07-13 04:08:55 +08:00
|
|
|
if (isPresenter) {
|
|
|
|
sendMessage('stop', { time: curTime });
|
2018-11-30 01:24:02 +08:00
|
|
|
}
|
2019-09-11 02:04:42 +08:00
|
|
|
this.setState({ playing: false });
|
2019-10-08 01:45:16 +08:00
|
|
|
|
|
|
|
if (!this.state.hasPlayedBefore) {
|
|
|
|
this.setState({ hasPlayedBefore: true, autoPlayBlocked: false });
|
|
|
|
clearTimeout(this.autoPlayTimeout);
|
|
|
|
}
|
2018-12-11 07:07:20 +08:00
|
|
|
}
|
|
|
|
|
2019-01-18 01:12:32 +08:00
|
|
|
render() {
|
2019-10-08 01:45:16 +08:00
|
|
|
const { videoUrl, intl } = this.props;
|
|
|
|
const { playing, playbackRate, mutedByEchoTest, autoPlayBlocked } = this.state;
|
2019-01-16 04:44:41 +08:00
|
|
|
|
2018-11-30 01:24:02 +08:00
|
|
|
return (
|
|
|
|
<div
|
2019-07-13 04:08:55 +08:00
|
|
|
id="video-player"
|
2018-11-30 01:24:02 +08:00
|
|
|
data-test="videoPlayer"
|
2019-07-13 04:08:55 +08:00
|
|
|
ref={(ref) => { this.playerParent = ref; }}
|
2018-11-30 01:24:02 +08:00
|
|
|
>
|
2019-10-08 01:45:16 +08:00
|
|
|
{autoPlayBlocked ?
|
|
|
|
<p className={styles.autoPlayWarning}>
|
|
|
|
{intl.formatMessage(intlMessages.autoPlayWarning)}
|
|
|
|
</p>
|
|
|
|
: ''
|
|
|
|
}
|
2019-07-13 04:08:55 +08:00
|
|
|
<ReactPlayer
|
2019-07-17 04:56:07 +08:00
|
|
|
className={styles.videoPlayer}
|
2019-07-13 04:08:55 +08:00
|
|
|
url={videoUrl}
|
2019-07-16 04:12:07 +08:00
|
|
|
config={this.opts}
|
2019-07-13 04:08:55 +08:00
|
|
|
muted={mutedByEchoTest}
|
|
|
|
playing={playing}
|
2019-07-17 06:04:20 +08:00
|
|
|
playbackRate={playbackRate}
|
2019-07-13 04:08:55 +08:00
|
|
|
onReady={this.handleOnReady}
|
|
|
|
onPlay={this.handleOnPlay}
|
|
|
|
onPause={this.handleOnPause}
|
|
|
|
ref={(ref) => { this.player = ref; }}
|
2019-01-16 04:44:41 +08:00
|
|
|
/>
|
2018-11-30 01:24:02 +08:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-08 01:45:16 +08:00
|
|
|
export default injectIntl(injectWbResizeEvent(VideoPlayer));
|