bigbluebutton-Github/bigbluebutton-html5/imports/ui/components/video-provider/video-dock/component.jsx

254 lines
6.8 KiB
React
Raw Normal View History

import React, { Component } from 'react';
import { styles } from '../styles';
import { defineMessages, injectIntl } from 'react-intl';
import { log } from '/imports/ui/services/api';
import { notify } from '/imports/ui/services/notification';
import { toast } from 'react-toastify';
import { styles as mediaStyles } from '/imports/ui/components/media/styles';
import Toast from '/imports/ui/components/toast/component';
2018-02-14 03:45:03 +08:00
import _ from 'lodash';
import VideoElement from '../video-element/component';
2018-02-15 02:34:00 +08:00
const INITIAL_SHARE_WAIT_TIME = 2000;
const intlMessages = defineMessages({
chromeExtensionError: {
id: 'app.video.chromeExtensionError',
description: 'Error message for Chrome Extension not installed',
},
chromeExtensionErrorLink: {
id: 'app.video.chromeExtensionErrorLink',
description: 'Error message for Chrome Extension not installed',
},
});
class VideoDock extends Component {
2017-09-01 23:26:57 +08:00
constructor(props) {
super(props);
this.state = {
videos: {},
2018-03-12 23:29:51 +08:00
sharingWebcamBefore: false,
2018-01-23 02:02:09 +08:00
userNames: {},
2017-09-01 23:26:57 +08:00
};
}
2017-09-01 23:26:57 +08:00
componentDidMount() {
const { users, userId } = this.props;
2018-01-17 00:04:34 +08:00
users.forEach((user) => {
if (user.has_stream && user.userId !== userId) {
// FIX: Really ugly hack, but sometimes the ICE candidates aren't
// generated properly when we send videos right after componentDidMount
setTimeout(() => {
2018-02-17 05:11:59 +08:00
this.start(user.userId);
}, INITIAL_SHARE_WAIT_TIME);
}
});
2017-09-01 23:26:57 +08:00
document.addEventListener('installChromeExtension', this.installChromeExtension.bind(this));
2017-09-20 11:12:10 +08:00
window.addEventListener('resize', this.adjustVideos);
2018-02-07 22:44:11 +08:00
window.addEventListener('orientationchange', this.adjustVideos);
2018-01-29 19:52:07 +08:00
}
componentWillUnmount() {
window.removeEventListener('resize', this.adjustVideos);
2018-02-09 00:32:12 +08:00
window.removeEventListener('orientationchange', this.adjustVideos);
document.removeEventListener('installChromeExtension', this.installChromeExtension.bind(this));
}
componentDidUpdate() {
this.adjustVideos();
}
2017-09-01 23:26:57 +08:00
notifyError(message) {
notify(message, 'error', 'video');
}
installChromeExtension() {
console.log(intlMessages);
const { intl } = this.props;
const CHROME_EXTENSION_LINK = Meteor.settings.public.kurento.chromeExtensionLink;
this.notifyError(<div>
{intl.formatMessage(intlMessages.chromeExtensionError)}{' '}
<a href={CHROME_EXTENSION_LINK} target="_blank">
{intl.formatMessage(intlMessages.chromeExtensionErrorLink)}
</a>
</div>);
}
// TODO
// Find a better place to put this piece of code
2018-01-29 19:52:07 +08:00
adjustVideos() {
setTimeout(() => {
window.adjustVideos('webcamArea', true, mediaStyles.moreThan4Videos, mediaStyles.container, mediaStyles.overlayWrapper, 'presentationAreaData', 'screenshareVideo');
}, 0);
}
2017-09-01 23:26:57 +08:00
2017-12-06 03:13:42 +08:00
createVideoTag(id) {
2018-02-17 03:18:53 +08:00
let newState = {...this.state};
newState.videos[id] = true;
this.setState(newState);
2017-12-06 03:13:42 +08:00
}
destroyVideoTag(id) {
2018-02-17 03:18:53 +08:00
const { videos } = this.state;
2018-03-12 23:29:51 +08:00
if (id !== this.props.userId) {
2018-02-17 03:18:53 +08:00
this.setState({
videos: _.omit(videos, id)
});
2017-12-21 01:05:05 +08:00
}
2018-03-12 23:29:51 +08:00
console.log(_.omit(videos, id));
2017-09-01 23:26:57 +08:00
}
2018-02-17 03:18:53 +08:00
start(id) {
const { users } = this.props;
2018-02-17 03:18:53 +08:00
log('info', `Starting video call for video: ${id}`);
2018-02-17 03:18:53 +08:00
this.createVideoTag(id);
2017-09-01 23:26:57 +08:00
}
2018-02-17 03:18:53 +08:00
stop(id) {
this.destroyVideoTag(id);
this.props.onStop(id);
}
2018-03-12 23:29:51 +08:00
stopMany(ids) {
const { videos } = this.state;
2018-03-12 23:29:51 +08:00
ids.forEach((id) => {
if (id === this.props.userId) {
this.setState({ sharingWebcamBefore: true });
delete ids[id];
2018-02-17 03:18:53 +08:00
}
});
2018-03-12 23:29:51 +08:00
this.setState({
videos: _.omit(videos, ids)
});
ids.forEach((id) => {
this.props.onStop(id);
});
}
getNameFromId(id) {
const { users } = this.props;
let name = users.find(u => u.userId === id).name;
2018-02-17 03:18:53 +08:00
return name;
}
render() {
return (
2018-03-12 23:29:51 +08:00
<div className={styles.videoDock} id={this.props.sharedWebcam.toString()}>
2018-02-06 03:52:07 +08:00
<div id="webcamArea" className={styles.webcamArea}>
2018-01-29 19:52:07 +08:00
{Object.keys(this.state.videos).map(id => (
2018-02-17 03:18:53 +08:00
<VideoElement
videoId={id}
key={id}
name={this.getNameFromId(id)}
localCamera={false}
2018-02-17 05:11:59 +08:00
onMount={this.props.onStart.bind(this)} />
2018-02-17 03:18:53 +08:00
))}
{this.props.sharedWebcam &&
<VideoElement
shared={this.props.sharedWebcam}
name={this.getNameFromId(this.props.userId)}
videoId={this.props.userId}
localCamera
2018-02-17 05:11:59 +08:00
onMount={this.props.onStart.bind(this)} />
2018-02-17 03:18:53 +08:00
}
</div>
</div>
);
}
2017-09-01 23:26:57 +08:00
shouldComponentUpdate(nextProps, nextState) {
2018-03-12 23:29:51 +08:00
const { userId, sharedWebcam, socketOpen } = this.props;
const currentUsers = this.props.users || {};
2017-09-01 23:26:57 +08:00
const nextUsers = nextProps.users;
const users = {};
const present = {};
2017-09-01 23:26:57 +08:00
2018-02-17 05:11:59 +08:00
sharedWebcam = sharedWebcam || false;
2018-03-12 23:29:51 +08:00
// If disconnected, stop all webcams
if (socketOpen && !nextProps.socketOpen) {
this.stopMany(nextUsers.filter(u => u.has_stream).map(u => u.userId));
}
// When reconnecting, restart all webcams.
if (!socketOpen && nextProps.socketOpen) {
nextUsers.forEach((user) => {
if (user.has_stream && user.userId != userId) {
this.start(user.userId);
}
});
if (this.state.sharingWebcamBefore){
this.props.onShareWebcam(this);
this.setState({ sharingWebcamBefore: false });
}
}
// If the user un-shared a webcam we'll stop it
2018-02-17 05:11:59 +08:00
if (sharedWebcam !== nextProps.sharedWebcam && !nextProps.sharedWebcam) {
this.stop(userId);
}
if (!currentUsers) { return false; }
2017-09-01 23:26:57 +08:00
2018-02-17 05:11:59 +08:00
// Map user objects to an object in the form {userId: has_stream}
currentUsers.forEach((user) => {
users[user.userId] = user.has_stream;
});
// Keep instances where the flag has changed or next user adds it
nextUsers.forEach((user) => {
const id = user.userId;
// The case when a user exists and stream status has not changed
if (users[id] === user.has_stream) {
delete users[id];
} else {
// Case when a user has been added to the list
users[id] = user.has_stream;
2017-09-01 23:26:57 +08:00
}
// Mark the ids which are present in nextUsers
present[id] = true;
});
const userIds = Object.keys(users);
for (let i = 0; i < userIds.length; i++) {
const id = userIds[i];
// If a userId is not present in nextUsers let's stop it
if (!present[id]) {
this.stop(id);
continue;
}
2017-09-01 23:26:57 +08:00
console.log(`User ${users[id] ? '' : 'un'}shared webcam ${id}`);
// If a user stream is true, changed and was shared by other
// user we'll start it. If it is false and changed we stop it
if (users[id]) {
if (userId !== id) {
2018-02-17 03:18:53 +08:00
this.start(id);
2017-09-01 23:26:57 +08:00
}
} else {
this.stop(id);
}
2017-09-01 23:26:57 +08:00
}
return true;
2017-09-01 23:26:57 +08:00
}
}
export default injectIntl(VideoDock);