2017-06-04 10:40:14 +08:00
|
|
|
import React, { Component } from 'react';
|
2018-02-19 12:23:05 +08:00
|
|
|
import { styles } from '../styles';
|
2018-01-06 03:43:53 +08:00
|
|
|
import { defineMessages, injectIntl } from 'react-intl';
|
2017-11-18 02:55:59 +08:00
|
|
|
import { log } from '/imports/ui/services/api';
|
2018-01-06 01:13:22 +08:00
|
|
|
import { notify } from '/imports/ui/services/notification';
|
2018-01-11 09:15:44 +08:00
|
|
|
import { toast } from 'react-toastify';
|
2018-02-08 03:20:10 +08:00
|
|
|
import { styles as mediaStyles } from '/imports/ui/components/media/styles';
|
2018-01-11 09:15:44 +08:00
|
|
|
import Toast from '/imports/ui/components/toast/component';
|
2018-02-14 03:45:03 +08:00
|
|
|
import _ from 'lodash';
|
2016-05-04 04:40:46 +08:00
|
|
|
|
2018-02-19 12:23:05 +08:00
|
|
|
import VideoElement from '../video-element/component';
|
2018-02-15 02:34:00 +08:00
|
|
|
|
2018-02-19 11:43:30 +08:00
|
|
|
const INITIAL_SHARE_WAIT_TIME = 2000;
|
|
|
|
|
2018-01-06 03:43:53 +08:00
|
|
|
const intlMessages = defineMessages({
|
2018-02-19 11:43:30 +08:00
|
|
|
chromeExtensionError: {
|
|
|
|
id: 'app.video.chromeExtensionError',
|
|
|
|
description: 'Error message for Chrome Extension not installed',
|
2018-01-06 03:43:53 +08:00
|
|
|
},
|
2018-02-19 11:43:30 +08:00
|
|
|
chromeExtensionErrorLink: {
|
|
|
|
id: 'app.video.chromeExtensionErrorLink',
|
|
|
|
description: 'Error message for Chrome Extension not installed',
|
2018-01-06 03:43:53 +08:00
|
|
|
},
|
|
|
|
});
|
2016-05-04 04:40:46 +08:00
|
|
|
|
2018-01-06 03:43:53 +08:00
|
|
|
class VideoDock extends Component {
|
2017-09-01 23:26:57 +08:00
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
|
|
|
|
this.state = {
|
2017-12-15 02:42:13 +08:00
|
|
|
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-12-02 07:40:25 +08:00
|
|
|
}
|
|
|
|
|
2017-09-01 23:26:57 +08:00
|
|
|
componentDidMount() {
|
2018-01-13 02:39:16 +08:00
|
|
|
const { users, userId } = this.props;
|
2017-12-20 01:02:54 +08:00
|
|
|
|
2018-01-17 00:04:34 +08:00
|
|
|
users.forEach((user) => {
|
|
|
|
if (user.has_stream && user.userId !== userId) {
|
2018-01-18 22:03:06 +08:00
|
|
|
// 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);
|
2018-01-23 02:23:55 +08:00
|
|
|
}, INITIAL_SHARE_WAIT_TIME);
|
2017-09-20 00:29:48 +08:00
|
|
|
}
|
2018-01-24 00:05:47 +08:00
|
|
|
});
|
2017-09-01 23:26:57 +08:00
|
|
|
|
2018-02-19 11:43:30 +08:00
|
|
|
document.addEventListener('installChromeExtension', this.installChromeExtension.bind(this));
|
2017-09-20 11:12:10 +08:00
|
|
|
|
2017-12-09 00:35:46 +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() {
|
2017-12-09 00:35:46 +08:00
|
|
|
window.removeEventListener('resize', this.adjustVideos);
|
2018-02-09 00:32:12 +08:00
|
|
|
window.removeEventListener('orientationchange', this.adjustVideos);
|
2018-02-19 11:43:30 +08:00
|
|
|
document.removeEventListener('installChromeExtension', this.installChromeExtension.bind(this));
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidUpdate() {
|
|
|
|
this.adjustVideos();
|
2017-12-09 00:35:46 +08:00
|
|
|
}
|
2017-09-01 23:26:57 +08:00
|
|
|
|
2018-02-19 11:43:30 +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() {
|
2018-01-11 04:57:20 +08:00
|
|
|
setTimeout(() => {
|
2018-02-14 00:20:59 +08:00
|
|
|
window.adjustVideos('webcamArea', true, mediaStyles.moreThan4Videos, mediaStyles.container, mediaStyles.overlayWrapper, 'presentationAreaData', 'screenshareVideo');
|
2018-01-11 04:57:20 +08:00
|
|
|
}, 0);
|
2017-12-09 00:35:46 +08:00
|
|
|
}
|
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;
|
2017-12-21 04:43:33 +08:00
|
|
|
|
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-10-13 03:47:36 +08:00
|
|
|
});
|
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) {
|
2018-02-19 11:43:30 +08:00
|
|
|
const { users } = this.props;
|
2018-01-06 01:13:22 +08:00
|
|
|
|
2018-02-17 03:18:53 +08:00
|
|
|
log('info', `Starting video call for video: ${id}`);
|
2018-01-16 05:19:01 +08:00
|
|
|
|
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-01-06 01:13:22 +08:00
|
|
|
}
|
|
|
|
|
2018-03-12 23:29:51 +08:00
|
|
|
stopMany(ids) {
|
|
|
|
const { videos } = this.state;
|
2018-01-06 01:13:22 +08:00
|
|
|
|
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-01-06 01:13:22 +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;
|
2017-12-20 00:29:09 +08:00
|
|
|
}
|
|
|
|
|
2016-05-04 04:40:46 +08:00
|
|
|
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
|
|
|
}
|
2017-12-20 00:29:09 +08:00
|
|
|
</div>
|
2016-05-04 04:40:46 +08:00
|
|
|
</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;
|
2018-01-25 11:48:49 +08:00
|
|
|
const currentUsers = this.props.users || {};
|
2017-09-01 23:26:57 +08:00
|
|
|
const nextUsers = nextProps.users;
|
|
|
|
|
2018-02-14 00:20:59 +08:00
|
|
|
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 });
|
|
|
|
}
|
|
|
|
}
|
2018-02-19 11:43:30 +08:00
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
|
2018-02-14 00:20:59 +08:00
|
|
|
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}
|
2018-01-25 11:48:49 +08:00
|
|
|
currentUsers.forEach((user) => {
|
|
|
|
users[user.userId] = user.has_stream;
|
|
|
|
});
|
2017-12-15 02:42:13 +08:00
|
|
|
|
2018-01-25 11:48:49 +08:00
|
|
|
// Keep instances where the flag has changed or next user adds it
|
|
|
|
nextUsers.forEach((user) => {
|
2018-02-14 00:20:59 +08:00
|
|
|
const id = user.userId;
|
2018-01-25 11:48:49 +08:00
|
|
|
// 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
|
|
|
}
|
|
|
|
|
2018-01-25 11:48:49 +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++) {
|
2018-02-14 00:20:59 +08:00
|
|
|
const id = userIds[i];
|
2018-01-25 11:48:49 +08:00
|
|
|
|
2018-02-01 04:55:56 +08:00
|
|
|
// If a userId is not present in nextUsers let's stop it
|
2018-01-25 11:48:49 +08:00
|
|
|
if (!present[id]) {
|
|
|
|
this.stop(id);
|
|
|
|
continue;
|
|
|
|
}
|
2017-09-01 23:26:57 +08:00
|
|
|
|
2018-01-25 11:48:49 +08:00
|
|
|
console.log(`User ${users[id] ? '' : 'un'}shared webcam ${id}`);
|
|
|
|
|
2018-02-01 04:55:56 +08:00
|
|
|
// 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
|
2018-01-25 11:48:49 +08:00
|
|
|
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
|
|
|
}
|
2018-02-14 00:20:59 +08:00
|
|
|
} else {
|
2018-01-25 11:48:49 +08:00
|
|
|
this.stop(id);
|
|
|
|
}
|
2017-09-01 23:26:57 +08:00
|
|
|
}
|
2018-01-25 11:48:49 +08:00
|
|
|
return true;
|
2017-09-01 23:26:57 +08:00
|
|
|
}
|
2016-05-04 04:40:46 +08:00
|
|
|
}
|
2018-01-06 03:43:53 +08:00
|
|
|
|
|
|
|
export default injectIntl(VideoDock);
|