2017-06-04 10:40:14 +08:00
|
|
|
import React, { Component } from 'react';
|
2018-01-08 14:17:18 +08:00
|
|
|
import { styles } from './styles';
|
2018-01-06 03:43:53 +08:00
|
|
|
import { defineMessages, injectIntl } from 'react-intl';
|
2018-01-11 09:15:44 +08:00
|
|
|
import VideoService from './service';
|
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';
|
|
|
|
import Toast from '/imports/ui/components/toast/component';
|
2016-05-04 04:40:46 +08:00
|
|
|
|
2018-01-06 03:43:53 +08:00
|
|
|
const intlMessages = defineMessages({
|
|
|
|
iceCandidateError: {
|
|
|
|
id: 'app.video.iceCandidateError',
|
|
|
|
description: 'Error message for ice candidate fail',
|
|
|
|
},
|
|
|
|
permissionError: {
|
|
|
|
id: 'app.video.permissionError',
|
|
|
|
description: 'Error message for webcam permission',
|
|
|
|
},
|
|
|
|
sharingError: {
|
|
|
|
id: 'app.video.sharingError',
|
|
|
|
description: 'Error on sharing webcam',
|
|
|
|
},
|
|
|
|
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',
|
|
|
|
},
|
|
|
|
});
|
2016-05-04 04:40:46 +08:00
|
|
|
|
2018-01-23 02:23:55 +08:00
|
|
|
const RECONNECT_WAIT_TIME = 5000;
|
|
|
|
const INITIAL_SHARE_WAIT_TIME = 2000;
|
2018-02-01 03:35:15 +08:00
|
|
|
const CAMERA_SHARE_FAILED_WAIT_TIME = 10000;
|
2018-01-23 02:23:55 +08:00
|
|
|
|
2017-11-06 23:42:00 +08:00
|
|
|
class VideoElement extends Component {
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
2017-09-01 23:26:57 +08:00
|
|
|
}
|
2017-12-20 00:29:09 +08:00
|
|
|
|
|
|
|
render() {
|
2018-01-29 19:52:07 +08:00
|
|
|
return (
|
|
|
|
<video
|
|
|
|
id={`video-elem-${this.props.videoId}`}
|
|
|
|
width={320}
|
|
|
|
height={240}
|
|
|
|
autoPlay="autoPlay"
|
|
|
|
playsInline="playsInline"
|
|
|
|
/>
|
|
|
|
);
|
2017-12-20 00:29:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
this.props.onMount(this.props.videoId, false);
|
|
|
|
}
|
2017-09-01 23:26:57 +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);
|
|
|
|
|
2017-12-09 00:35:46 +08:00
|
|
|
// Set a valid bbb-webrtc-sfu application server socket in the settings
|
|
|
|
this.ws = new ReconnectingWebSocket(Meteor.settings.public.kurento.wsUrl);
|
|
|
|
this.wsQueue = [];
|
|
|
|
this.webRtcPeers = {};
|
2017-12-15 03:09:45 +08:00
|
|
|
this.reconnectWebcam = false;
|
2018-01-11 21:55:03 +08:00
|
|
|
this.reconnectList = [];
|
2018-01-25 11:40:16 +08:00
|
|
|
this.cameraTimeouts = {};
|
2017-12-15 03:09:45 +08:00
|
|
|
|
2017-09-01 23:26:57 +08:00
|
|
|
this.state = {
|
2017-12-15 02:42:13 +08:00
|
|
|
videos: {},
|
2018-01-29 19:52:07 +08:00
|
|
|
sharedWebcam: false,
|
2017-09-01 23:26:57 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
this.unshareWebcam = this.unshareWebcam.bind(this);
|
|
|
|
this.shareWebcam = this.shareWebcam.bind(this);
|
2017-12-20 01:02:54 +08:00
|
|
|
|
|
|
|
this.onWsOpen = this.onWsOpen.bind(this);
|
|
|
|
this.onWsClose = this.onWsClose.bind(this);
|
|
|
|
this.onWsMessage = this.onWsMessage.bind(this);
|
2017-09-01 23:26:57 +08:00
|
|
|
}
|
|
|
|
|
2017-12-02 07:40:25 +08:00
|
|
|
setupReconnectVideos() {
|
2017-12-15 03:09:45 +08:00
|
|
|
for (id in this.webRtcPeers) {
|
2017-12-02 07:40:25 +08:00
|
|
|
this.disconnected(id);
|
|
|
|
this.stop(id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
reconnectVideos() {
|
2017-12-15 03:09:45 +08:00
|
|
|
for (i in this.reconnectList) {
|
|
|
|
const id = this.reconnectList[i];
|
2017-12-02 07:40:25 +08:00
|
|
|
|
2017-12-15 02:42:13 +08:00
|
|
|
// TODO: base this on BBB API users instead of using memory
|
2017-12-15 03:09:45 +08:00
|
|
|
if (id != this.myId) {
|
2017-12-15 02:42:13 +08:00
|
|
|
setTimeout(() => {
|
|
|
|
log('debug', ` [camera] Trying to reconnect camera ${id}`);
|
2017-12-20 00:29:09 +08:00
|
|
|
this.start(id, false);
|
2018-01-23 02:23:55 +08:00
|
|
|
}, RECONNECT_WAIT_TIME);
|
2017-12-02 07:40:25 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-15 03:09:45 +08:00
|
|
|
if (this.reconnectWebcam) {
|
|
|
|
log('debug', ` [camera] Trying to re-share ${this.myId} after reconnect.`);
|
2017-12-20 00:29:09 +08:00
|
|
|
this.start(this.myId, true);
|
2017-12-02 07:40:25 +08:00
|
|
|
}
|
|
|
|
|
2017-12-15 03:09:45 +08:00
|
|
|
this.reconnectWebcam = false;
|
|
|
|
this.reconnectList = [];
|
2017-12-02 07:40:25 +08:00
|
|
|
}
|
|
|
|
|
2017-09-01 23:26:57 +08:00
|
|
|
componentDidMount() {
|
2017-12-09 00:35:46 +08:00
|
|
|
const ws = this.ws;
|
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-01-24 00:05:47 +08:00
|
|
|
this.start(user.userId, false);
|
2018-01-23 02:23:55 +08:00
|
|
|
}, INITIAL_SHARE_WAIT_TIME);
|
2017-09-20 00:29:48 +08:00
|
|
|
}
|
2018-01-29 19:52:07 +08:00
|
|
|
});
|
2017-09-01 23:26:57 +08:00
|
|
|
|
2018-01-29 19:52:07 +08:00
|
|
|
document.addEventListener('joinVideo', this.shareWebcam.bind(this)); // TODO find a better way to do this
|
2017-12-09 00:35:46 +08:00
|
|
|
document.addEventListener('exitVideo', this.unshareWebcam.bind(this));
|
2018-01-06 01:13:22 +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);
|
2017-09-01 23:26:57 +08:00
|
|
|
|
2017-12-20 01:02:54 +08:00
|
|
|
ws.addEventListener('message', this.onWsMessage);
|
2017-12-09 00:35:46 +08:00
|
|
|
}
|
2017-09-01 23:26:57 +08:00
|
|
|
|
2018-01-29 19:52:07 +08:00
|
|
|
componentWillMount() {
|
2017-12-20 01:02:54 +08:00
|
|
|
this.ws.addEventListener('open', this.onWsOpen);
|
|
|
|
this.ws.addEventListener('close', this.onWsClose);
|
2018-01-06 03:13:30 +08:00
|
|
|
|
|
|
|
window.addEventListener('online', this.ws.open.bind(this.ws));
|
2018-01-25 11:44:08 +08:00
|
|
|
window.addEventListener('offline', this.onWsClose);
|
2017-12-09 00:35:46 +08:00
|
|
|
}
|
2017-09-01 23:26:57 +08:00
|
|
|
|
2018-01-29 19:52:07 +08:00
|
|
|
componentWillUpdate(nextProps) {
|
|
|
|
const { isLocked } = nextProps;
|
|
|
|
if (isLocked && VideoService.isConnected()) {
|
|
|
|
this.unshareWebcam();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
2017-12-09 00:35:46 +08:00
|
|
|
document.removeEventListener('joinVideo', this.shareWebcam);
|
2018-01-06 01:13:22 +08:00
|
|
|
document.removeEventListener('exitVideo', this.unshareWebcam);
|
|
|
|
document.removeEventListener('installChromeExtension', this.installChromeExtension);
|
2017-12-09 00:35:46 +08:00
|
|
|
window.removeEventListener('resize', this.adjustVideos);
|
2017-12-20 01:02:54 +08:00
|
|
|
|
2017-12-09 00:35:46 +08:00
|
|
|
this.ws.removeEventListener('message', this.onWsMessage);
|
2017-12-20 01:02:54 +08:00
|
|
|
this.ws.removeEventListener('open', this.onWsOpen);
|
|
|
|
this.ws.removeEventListener('close', this.onWsClose);
|
2017-12-20 00:29:09 +08:00
|
|
|
// Close websocket connection to prevent multiple reconnects from happening
|
2018-01-06 03:13:30 +08:00
|
|
|
|
2018-01-25 11:44:08 +08:00
|
|
|
window.removeEventListener('online', this.ws.open.bind(this.ws));
|
|
|
|
window.removeEventListener('offline', this.onWsClose);
|
2018-01-06 03:13:30 +08:00
|
|
|
|
2017-12-20 00:29:09 +08:00
|
|
|
this.ws.close();
|
2017-12-09 00:35:46 +08:00
|
|
|
}
|
2017-09-01 23:26:57 +08:00
|
|
|
|
2018-01-29 19:52:07 +08:00
|
|
|
adjustVideos() {
|
2018-01-11 04:57:20 +08:00
|
|
|
setTimeout(() => {
|
|
|
|
window.adjustVideos('webcamArea', true);
|
|
|
|
}, 0);
|
2017-12-09 00:35:46 +08:00
|
|
|
}
|
2017-09-01 23:26:57 +08:00
|
|
|
|
2018-01-29 19:52:07 +08:00
|
|
|
onWsOpen() {
|
2017-12-20 01:02:54 +08:00
|
|
|
log('debug', '------ Websocket connection opened.');
|
|
|
|
|
|
|
|
// -- Resend queued messages that happened when socket was not connected
|
|
|
|
while (this.wsQueue.length > 0) {
|
|
|
|
this.sendMessage(this.wsQueue.pop());
|
|
|
|
}
|
|
|
|
|
|
|
|
this.reconnectVideos();
|
|
|
|
}
|
|
|
|
|
2018-01-29 19:52:07 +08:00
|
|
|
onWsClose(error) {
|
2017-12-20 01:02:54 +08:00
|
|
|
log('debug', '------ Websocket connection closed.');
|
|
|
|
|
|
|
|
this.setupReconnectVideos();
|
|
|
|
}
|
|
|
|
|
2018-01-29 19:52:07 +08:00
|
|
|
onWsMessage(msg) {
|
2018-01-06 03:43:53 +08:00
|
|
|
const { intl } = this.props;
|
2017-12-09 00:35:46 +08:00
|
|
|
const parsedMessage = JSON.parse(msg.data);
|
2017-09-01 23:26:57 +08:00
|
|
|
|
2017-12-09 00:35:46 +08:00
|
|
|
console.log('Received message new ws message: ');
|
|
|
|
console.log(parsedMessage);
|
2017-09-01 23:26:57 +08:00
|
|
|
|
2017-12-09 00:35:46 +08:00
|
|
|
switch (parsedMessage.id) {
|
|
|
|
case 'startResponse':
|
|
|
|
this.startResponse(parsedMessage);
|
|
|
|
break;
|
2017-09-01 23:26:57 +08:00
|
|
|
|
2017-12-09 00:35:46 +08:00
|
|
|
case 'playStart':
|
|
|
|
this.handlePlayStart(parsedMessage);
|
|
|
|
break;
|
2017-12-15 02:42:13 +08:00
|
|
|
|
2017-12-09 00:35:46 +08:00
|
|
|
case 'playStop':
|
|
|
|
this.handlePlayStop(parsedMessage);
|
2017-09-01 23:26:57 +08:00
|
|
|
|
2017-12-09 00:35:46 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'iceCandidate':
|
|
|
|
const webRtcPeer = this.webRtcPeers[parsedMessage.cameraId];
|
|
|
|
|
2018-01-23 02:23:55 +08:00
|
|
|
if (!!webRtcPeer) {
|
2017-12-09 00:35:46 +08:00
|
|
|
if (webRtcPeer.didSDPAnswered) {
|
|
|
|
webRtcPeer.addIceCandidate(parsedMessage.candidate, (err) => {
|
|
|
|
if (err) {
|
2018-01-06 03:43:53 +08:00
|
|
|
this.notifyError(intl.formatMessage(intlMessages.iceCandidateError));
|
2017-12-09 00:35:46 +08:00
|
|
|
return log('error', `Error adding candidate: ${err}`);
|
|
|
|
}
|
|
|
|
});
|
2017-09-01 23:26:57 +08:00
|
|
|
} else {
|
2017-12-09 00:35:46 +08:00
|
|
|
webRtcPeer.iceQueue.push(parsedMessage.candidate);
|
2017-09-01 23:26:57 +08:00
|
|
|
}
|
2017-12-09 00:35:46 +08:00
|
|
|
} else {
|
2018-01-16 05:19:01 +08:00
|
|
|
log('error', ' [ICE] Message arrived after the peer was already thrown out, discarding it...');
|
2017-12-09 00:35:46 +08:00
|
|
|
}
|
|
|
|
break;
|
2018-01-25 11:40:16 +08:00
|
|
|
|
|
|
|
case 'error':
|
|
|
|
default:
|
|
|
|
this.handleError(parsedMessage);
|
|
|
|
break;
|
2017-12-09 00:35:46 +08:00
|
|
|
}
|
2018-01-29 19:52:07 +08:00
|
|
|
}
|
2017-09-01 23:26:57 +08:00
|
|
|
|
2017-12-20 00:29:09 +08:00
|
|
|
start(id, shareWebcam) {
|
2017-09-01 23:26:57 +08:00
|
|
|
const that = this;
|
2018-01-25 11:40:16 +08:00
|
|
|
const { intl } = this.props;
|
2017-09-01 23:26:57 +08:00
|
|
|
|
2017-12-20 00:29:09 +08:00
|
|
|
console.log(`Starting video call for video: ${id} with ${shareWebcam}`);
|
2017-09-01 23:26:57 +08:00
|
|
|
|
2018-01-25 11:40:16 +08:00
|
|
|
this.cameraTimeouts[id] = setTimeout(() => {
|
|
|
|
log('error', `Camera share has not suceeded in ${CAMERA_SHARE_FAILED_WAIT_TIME}`);
|
|
|
|
if (that.myId == id) {
|
2018-02-01 03:35:15 +08:00
|
|
|
that.notifyError(intl.formatMessage(intlMessages.sharingError));
|
|
|
|
that.unshareWebcam();
|
2018-01-25 11:40:16 +08:00
|
|
|
} else {
|
|
|
|
that.stop(id);
|
|
|
|
that.start(id, shareWebcam);
|
|
|
|
}
|
|
|
|
}, CAMERA_SHARE_FAILED_WAIT_TIME);
|
|
|
|
|
2017-12-20 00:29:09 +08:00
|
|
|
if (shareWebcam) {
|
2018-01-11 09:15:44 +08:00
|
|
|
VideoService.joiningVideo();
|
2018-01-29 19:52:07 +08:00
|
|
|
this.setState({ sharedWebcam: true });
|
2018-01-10 21:56:52 +08:00
|
|
|
this.myId = id;
|
2017-12-20 00:29:09 +08:00
|
|
|
this.initWebRTC(id, true);
|
|
|
|
} else {
|
|
|
|
// initWebRTC with shareWebcam false will be called after react mounts the element
|
|
|
|
this.createVideoTag(id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
initWebRTC(id, shareWebcam) {
|
2018-01-29 19:52:07 +08:00
|
|
|
const that = this;
|
2018-01-06 03:43:53 +08:00
|
|
|
const { intl } = this.props;
|
2017-09-01 23:26:57 +08:00
|
|
|
|
|
|
|
const onIceCandidate = function (candidate) {
|
|
|
|
const message = {
|
2017-11-25 02:59:40 +08:00
|
|
|
type: 'video',
|
2017-12-21 03:20:56 +08:00
|
|
|
role: shareWebcam ? 'share' : 'viewer',
|
2017-09-01 23:26:57 +08:00
|
|
|
id: 'onIceCandidate',
|
|
|
|
candidate,
|
|
|
|
cameraId: id,
|
|
|
|
};
|
|
|
|
that.sendMessage(message);
|
|
|
|
};
|
|
|
|
|
2017-12-20 00:29:09 +08:00
|
|
|
let videoConstraints = {};
|
2018-01-29 19:52:07 +08:00
|
|
|
if (navigator.userAgent.match(/Version\/[\d\.]+.*Safari/)) {
|
|
|
|
// Custom constraints for Safari
|
2017-11-29 03:31:36 +08:00
|
|
|
videoConstraints = {
|
2018-01-29 19:52:07 +08:00
|
|
|
width: {
|
|
|
|
min: 320,
|
|
|
|
max: 640,
|
|
|
|
},
|
|
|
|
height: {
|
|
|
|
min: 240,
|
|
|
|
max: 480,
|
|
|
|
},
|
|
|
|
};
|
2017-11-29 03:31:36 +08:00
|
|
|
} else {
|
|
|
|
videoConstraints = {
|
2018-01-29 19:52:07 +08:00
|
|
|
width: {
|
|
|
|
min: 320,
|
|
|
|
ideal: 320,
|
|
|
|
},
|
|
|
|
height: {
|
|
|
|
min: 240,
|
|
|
|
ideal: 240,
|
|
|
|
},
|
|
|
|
frameRate: {
|
|
|
|
min: 5,
|
|
|
|
ideal: 10,
|
|
|
|
},
|
2017-11-29 03:31:36 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-01-29 19:52:07 +08:00
|
|
|
const options = {
|
2017-12-06 03:13:42 +08:00
|
|
|
mediaConstraints: {
|
|
|
|
audio: false,
|
2018-01-29 19:52:07 +08:00
|
|
|
video: videoConstraints,
|
2017-11-06 23:42:00 +08:00
|
|
|
},
|
2017-09-01 23:26:57 +08:00
|
|
|
onicecandidate: onIceCandidate,
|
|
|
|
};
|
|
|
|
|
|
|
|
let peerObj;
|
|
|
|
if (shareWebcam) {
|
2017-12-20 00:29:09 +08:00
|
|
|
options.localVideo = this.refs.videoInput;
|
2017-09-01 23:26:57 +08:00
|
|
|
peerObj = kurentoUtils.WebRtcPeer.WebRtcPeerSendonly;
|
|
|
|
} else {
|
|
|
|
peerObj = kurentoUtils.WebRtcPeer.WebRtcPeerRecvonly;
|
2017-12-20 00:29:09 +08:00
|
|
|
options.remoteVideo = document.getElementById(`video-elem-${id}`);
|
2017-09-01 23:26:57 +08:00
|
|
|
}
|
|
|
|
|
2018-01-29 19:52:07 +08:00
|
|
|
const webRtcPeer = new peerObj(options, function (error) {
|
2017-09-01 23:26:57 +08:00
|
|
|
if (error) {
|
2017-12-06 03:13:42 +08:00
|
|
|
log('error', ' WebRTC peerObj create error');
|
2018-01-06 01:13:22 +08:00
|
|
|
log('error', error);
|
2018-01-10 21:56:52 +08:00
|
|
|
that.notifyError(intl.formatMessage(intlMessages.permissionError));
|
2018-01-29 19:52:07 +08:00
|
|
|
/* This notification error is displayed considering kurento-utils
|
|
|
|
* returned the error 'The request is not allowed by the user agent
|
2018-01-06 01:13:22 +08:00
|
|
|
* or the platform in the current context.', but there are other
|
|
|
|
* errors that could be returned. */
|
2017-12-06 03:13:42 +08:00
|
|
|
|
|
|
|
that.destroyWebRTCPeer(id);
|
|
|
|
that.destroyVideoTag(id);
|
2018-01-11 09:15:44 +08:00
|
|
|
VideoService.resetState();
|
2017-12-06 03:13:42 +08:00
|
|
|
return log('error', error);
|
2017-09-01 23:26:57 +08:00
|
|
|
}
|
|
|
|
|
2017-11-29 03:31:36 +08:00
|
|
|
this.didSDPAnswered = false;
|
|
|
|
this.iceQueue = [];
|
|
|
|
|
2017-12-15 03:09:45 +08:00
|
|
|
that.webRtcPeers[id] = webRtcPeer;
|
2017-10-14 05:13:41 +08:00
|
|
|
if (shareWebcam) {
|
2017-12-15 03:09:45 +08:00
|
|
|
that.sharedWebcam = webRtcPeer;
|
2017-10-14 05:13:41 +08:00
|
|
|
}
|
|
|
|
|
2017-09-01 23:26:57 +08:00
|
|
|
this.generateOffer((error, offerSdp) => {
|
|
|
|
if (error) {
|
2017-12-06 03:13:42 +08:00
|
|
|
log('error', ' WebRtc generate offer error');
|
|
|
|
|
|
|
|
that.destroyWebRTCPeer(id);
|
|
|
|
that.destroyVideoTag(id);
|
|
|
|
|
2017-11-18 02:55:59 +08:00
|
|
|
return log('error', error);
|
2017-09-01 23:26:57 +08:00
|
|
|
}
|
|
|
|
|
2017-11-25 02:59:40 +08:00
|
|
|
console.log(`Invoking SDP offer callback function ${location.host}`);
|
2017-09-01 23:26:57 +08:00
|
|
|
const message = {
|
2017-11-25 02:59:40 +08:00
|
|
|
type: 'video',
|
2017-12-21 03:20:56 +08:00
|
|
|
role: shareWebcam ? 'share' : 'viewer',
|
2017-09-01 23:26:57 +08:00
|
|
|
id: 'start',
|
|
|
|
sdpOffer: offerSdp,
|
|
|
|
cameraId: id,
|
|
|
|
};
|
|
|
|
that.sendMessage(message);
|
|
|
|
});
|
2017-11-29 03:31:36 +08:00
|
|
|
while (this.iceQueue.length) {
|
2018-01-29 19:52:07 +08:00
|
|
|
const candidate = this.iceQueue.shift();
|
2017-11-29 03:31:36 +08:00
|
|
|
this.addIceCandidate(candidate, (err) => {
|
|
|
|
if (err) {
|
2018-01-06 03:43:53 +08:00
|
|
|
this.notifyError(intl.formatMessage(intlMessages.iceCandidateError));
|
2017-11-29 03:31:36 +08:00
|
|
|
return console.error(`Error adding candidate: ${err}`);
|
2017-12-06 03:58:58 +08:00
|
|
|
}
|
2017-11-29 03:31:36 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
this.didSDPAnswered = true;
|
2017-09-01 23:26:57 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-12-02 07:40:25 +08:00
|
|
|
disconnected(id) {
|
2017-12-15 03:09:45 +08:00
|
|
|
if (this.sharedWebcam) {
|
2017-12-15 02:42:13 +08:00
|
|
|
log('debug', ' [camera] Webcam disconnected, will try re-share webcam later.');
|
2017-12-15 03:09:45 +08:00
|
|
|
this.reconnectWebcam = true;
|
2017-12-02 07:40:25 +08:00
|
|
|
} else {
|
2017-12-15 03:09:45 +08:00
|
|
|
this.reconnectList.push(id);
|
2017-12-02 07:40:25 +08:00
|
|
|
|
2017-12-06 03:13:42 +08:00
|
|
|
log('debug', ` [camera] ${id} disconnected, will try re-subscribe later.`);
|
2017-12-02 07:40:25 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-01 23:26:57 +08:00
|
|
|
stop(id) {
|
2018-01-13 02:51:07 +08:00
|
|
|
const { userId } = this.props;
|
2017-12-21 01:05:05 +08:00
|
|
|
this.sendMessage({
|
|
|
|
type: 'video',
|
2018-01-13 02:51:07 +08:00
|
|
|
role: id == userId ? 'share' : 'viewer',
|
2017-12-21 01:05:05 +08:00
|
|
|
id: 'stop',
|
|
|
|
cameraId: id,
|
|
|
|
});
|
2017-12-06 03:13:42 +08:00
|
|
|
|
2018-01-16 05:19:01 +08:00
|
|
|
if (id === userId) {
|
|
|
|
VideoService.exitedVideo();
|
|
|
|
}
|
|
|
|
|
2017-12-06 03:13:42 +08:00
|
|
|
this.destroyWebRTCPeer(id);
|
|
|
|
this.destroyVideoTag(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
createVideoTag(id) {
|
2018-01-29 19:52:07 +08:00
|
|
|
const videos = this.state.videos;
|
2017-12-06 03:13:42 +08:00
|
|
|
|
2017-12-20 00:29:09 +08:00
|
|
|
videos[id] = true;
|
2018-01-29 19:52:07 +08:00
|
|
|
this.setState({ videos });
|
2017-12-06 03:13:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
destroyVideoTag(id) {
|
2018-01-29 19:52:07 +08:00
|
|
|
const videos = this.state.videos;
|
2017-12-20 00:29:09 +08:00
|
|
|
|
|
|
|
delete videos[id];
|
2018-01-29 19:52:07 +08:00
|
|
|
this.setState({ videos });
|
2017-12-21 04:43:33 +08:00
|
|
|
|
|
|
|
if (id == this.myId) {
|
2018-01-29 19:52:07 +08:00
|
|
|
this.setState({ sharedWebcam: false });
|
2017-11-06 23:42:00 +08:00
|
|
|
}
|
2017-12-06 03:13:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
destroyWebRTCPeer(id) {
|
2017-12-09 00:35:46 +08:00
|
|
|
const webRtcPeer = this.webRtcPeers[id];
|
2017-09-01 23:26:57 +08:00
|
|
|
|
2018-01-25 11:40:16 +08:00
|
|
|
// Clear the shared camera fail timeout when destroying
|
|
|
|
clearTimeout(this.cameraTimeouts[id]);
|
|
|
|
this.cameraTimeouts[id] = null;
|
|
|
|
|
2017-09-01 23:26:57 +08:00
|
|
|
if (webRtcPeer) {
|
2017-11-18 02:55:59 +08:00
|
|
|
log('info', 'Stopping WebRTC peer');
|
2017-09-01 23:26:57 +08:00
|
|
|
|
2017-12-15 03:09:45 +08:00
|
|
|
if (id == this.myId && this.sharedWebcam) {
|
2017-12-09 00:35:46 +08:00
|
|
|
this.sharedWebcam.dispose();
|
|
|
|
this.sharedWebcam = null;
|
2017-10-14 05:13:41 +08:00
|
|
|
}
|
|
|
|
|
2017-09-01 23:26:57 +08:00
|
|
|
webRtcPeer.dispose();
|
2017-12-09 00:35:46 +08:00
|
|
|
delete this.webRtcPeers[id];
|
2017-09-01 23:26:57 +08:00
|
|
|
} else {
|
2017-12-06 03:13:42 +08:00
|
|
|
log('info', 'No WebRTC peer to stop (not an error)');
|
2017-09-01 23:26:57 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
shareWebcam() {
|
2018-01-13 02:39:16 +08:00
|
|
|
const { users, userId } = this.props;
|
2017-09-01 23:26:57 +08:00
|
|
|
|
2017-12-15 02:42:13 +08:00
|
|
|
if (this.connectedToMediaServer()) {
|
2018-01-13 02:39:16 +08:00
|
|
|
this.start(userId, true);
|
2017-12-15 02:42:13 +08:00
|
|
|
} else {
|
2018-01-29 19:52:07 +08:00
|
|
|
log('error', 'Not connected to media server');
|
2017-12-15 02:42:13 +08:00
|
|
|
}
|
2017-09-01 23:26:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
unshareWebcam() {
|
2018-01-11 09:15:44 +08:00
|
|
|
VideoService.exitingVideo();
|
2017-12-06 03:13:42 +08:00
|
|
|
log('info', 'Unsharing webcam');
|
2018-01-29 19:52:07 +08:00
|
|
|
console.warn(this.props);
|
2018-01-13 05:31:01 +08:00
|
|
|
const { userId } = this.props;
|
|
|
|
VideoService.sendUserUnshareWebcam(userId);
|
2017-09-01 23:26:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
startResponse(message) {
|
|
|
|
const id = message.cameraId;
|
2017-12-09 00:35:46 +08:00
|
|
|
const webRtcPeer = this.webRtcPeers[id];
|
2017-09-01 23:26:57 +08:00
|
|
|
|
|
|
|
if (message.sdpAnswer == null) {
|
2017-11-18 02:55:59 +08:00
|
|
|
return log('debug', 'Null sdp answer. Camera unplugged?');
|
2017-09-01 23:26:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (webRtcPeer == null) {
|
2017-11-18 02:55:59 +08:00
|
|
|
return log('debug', 'Null webrtc peer ????');
|
2017-09-01 23:26:57 +08:00
|
|
|
}
|
|
|
|
|
2017-11-18 02:55:59 +08:00
|
|
|
log('info', 'SDP answer received from server. Processing ...');
|
2017-09-01 23:26:57 +08:00
|
|
|
|
|
|
|
webRtcPeer.processAnswer(message.sdpAnswer, (error) => {
|
|
|
|
if (error) {
|
2017-12-06 03:13:42 +08:00
|
|
|
return log('error', error);
|
2017-09-01 23:26:57 +08:00
|
|
|
}
|
|
|
|
|
2018-01-25 11:40:16 +08:00
|
|
|
if (message.cameraId == this.props.userId) {
|
2018-02-06 23:21:22 +08:00
|
|
|
log('info', 'camera id sendusershare ', id);
|
2018-01-25 11:40:16 +08:00
|
|
|
VideoService.sendUserShareWebcam(id);
|
|
|
|
}
|
|
|
|
});
|
2017-09-01 23:26:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
sendMessage(message) {
|
2017-12-09 00:35:46 +08:00
|
|
|
const ws = this.ws;
|
2017-09-01 23:26:57 +08:00
|
|
|
|
2017-12-02 07:40:25 +08:00
|
|
|
if (this.connectedToMediaServer()) {
|
2017-10-13 03:47:36 +08:00
|
|
|
const jsonMessage = JSON.stringify(message);
|
2017-11-25 02:59:40 +08:00
|
|
|
console.log(`Sending message: ${jsonMessage}`);
|
2017-10-13 03:47:36 +08:00
|
|
|
ws.send(jsonMessage, (error) => {
|
|
|
|
if (error) {
|
2017-11-25 02:59:40 +08:00
|
|
|
console.error(`client: Websocket error "${error}" on message "${jsonMessage.id}"`);
|
2017-10-13 03:47:36 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
2017-12-02 07:40:25 +08:00
|
|
|
// No need to queue video stop messages
|
2017-12-06 03:13:42 +08:00
|
|
|
if (message.id != 'stop') {
|
2017-12-15 03:09:45 +08:00
|
|
|
this.wsQueue.push(message);
|
2017-12-02 07:40:25 +08:00
|
|
|
}
|
2017-10-13 03:47:36 +08:00
|
|
|
}
|
2017-09-01 23:26:57 +08:00
|
|
|
}
|
|
|
|
|
2017-12-02 07:40:25 +08:00
|
|
|
connectedToMediaServer() {
|
2017-12-15 03:09:45 +08:00
|
|
|
return this.ws.readyState === WebSocket.OPEN;
|
2017-12-02 07:40:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
connectionStatus() {
|
2017-12-15 03:09:45 +08:00
|
|
|
return this.ws.readyState;
|
2017-12-02 07:40:25 +08:00
|
|
|
}
|
|
|
|
|
2017-09-01 23:26:57 +08:00
|
|
|
handlePlayStop(message) {
|
2017-11-18 02:55:59 +08:00
|
|
|
log('info', 'Handle play stop <--------------------');
|
2017-12-15 02:42:13 +08:00
|
|
|
log('error', message);
|
2017-09-01 23:26:57 +08:00
|
|
|
|
2018-01-16 05:19:01 +08:00
|
|
|
if (message.cameraId == this.props.userId) {
|
2017-12-21 01:05:05 +08:00
|
|
|
this.unshareWebcam();
|
|
|
|
} else {
|
|
|
|
this.stop(message.cameraId);
|
|
|
|
}
|
2017-09-01 23:26:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
handlePlayStart(message) {
|
2017-11-18 02:55:59 +08:00
|
|
|
log('info', 'Handle play start <===================');
|
2018-01-13 03:44:56 +08:00
|
|
|
|
2018-01-25 11:40:16 +08:00
|
|
|
// Clear camera shared timeout when camera succesfully starts
|
|
|
|
clearTimeout(this.cameraTimeouts[message.cameraId]);
|
|
|
|
this.cameraTimeouts[message.cameraId] = null;
|
|
|
|
|
2018-01-13 05:31:01 +08:00
|
|
|
if (message.cameraId == this.props.userId) {
|
2018-01-13 03:44:56 +08:00
|
|
|
VideoService.joinedVideo();
|
|
|
|
}
|
2017-09-01 23:26:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
handleError(message) {
|
2018-01-25 12:08:00 +08:00
|
|
|
const { intl, userId } = this.props;
|
2018-01-06 01:13:22 +08:00
|
|
|
|
2018-01-25 12:08:00 +08:00
|
|
|
if (message.cameraId == userId) {
|
2018-01-16 05:19:01 +08:00
|
|
|
this.unshareWebcam();
|
2018-01-25 11:40:16 +08:00
|
|
|
this.notifyError(intl.formatMessage(intlMessages.sharingError));
|
2018-01-16 05:19:01 +08:00
|
|
|
} else {
|
|
|
|
this.stop(message.cameraId);
|
|
|
|
}
|
2018-01-06 01:13:22 +08:00
|
|
|
|
2017-12-06 03:13:42 +08:00
|
|
|
console.error(' Handle error --------------------->');
|
|
|
|
log('debug', message.message);
|
2017-09-01 23:26:57 +08:00
|
|
|
}
|
|
|
|
|
2018-01-06 01:13:22 +08:00
|
|
|
notifyError(message) {
|
|
|
|
notify(message, 'error', 'video');
|
|
|
|
}
|
|
|
|
|
|
|
|
installChromeExtension() {
|
2018-01-06 03:43:53 +08:00
|
|
|
const { intl } = this.props;
|
2018-01-06 01:13:22 +08:00
|
|
|
const CHROME_EXTENSION_LINK = Meteor.settings.public.kurento.chromeExtensionLink;
|
|
|
|
|
2018-01-29 19:52:07 +08:00
|
|
|
this.notifyError(<div>
|
|
|
|
{intl.formatMessage(intlMessages.chromeExtensionError)}{' '}
|
|
|
|
<a href={CHROME_EXTENSION_LINK} target="_blank">
|
|
|
|
{intl.formatMessage(intlMessages.chromeExtensionErrorLink)}
|
|
|
|
</a>
|
|
|
|
</div>);
|
2018-01-06 01:13:22 +08:00
|
|
|
}
|
|
|
|
|
2017-12-20 00:29:09 +08:00
|
|
|
componentDidUpdate() {
|
|
|
|
this.adjustVideos();
|
|
|
|
}
|
|
|
|
|
2016-05-04 04:40:46 +08:00
|
|
|
render() {
|
2017-12-21 04:06:27 +08:00
|
|
|
let cssClass;
|
|
|
|
if (this.state.sharedWebcam) {
|
|
|
|
cssClass = styles.sharedWebcamVideoLocal;
|
2018-01-29 19:52:07 +08:00
|
|
|
} else {
|
2017-12-21 04:06:27 +08:00
|
|
|
cssClass = styles.sharedWebcamVideo;
|
|
|
|
}
|
|
|
|
|
2016-05-04 04:40:46 +08:00
|
|
|
return (
|
|
|
|
<div className={styles.videoDock}>
|
2017-12-20 00:29:09 +08:00
|
|
|
<div id="webcamArea">
|
2018-01-29 19:52:07 +08:00
|
|
|
{Object.keys(this.state.videos).map(id => (
|
|
|
|
<VideoElement videoId={id} key={id} onMount={this.initWebRTC.bind(this)} />
|
|
|
|
))}
|
|
|
|
<video
|
|
|
|
autoPlay="autoPlay"
|
|
|
|
playsInline="playsInline"
|
|
|
|
muted="muted"
|
|
|
|
id="shareWebcamVideo"
|
|
|
|
className={cssClass}
|
|
|
|
ref="videoInput"
|
|
|
|
/>
|
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-01-25 11:48:49 +08:00
|
|
|
const { userId } = this.props;
|
|
|
|
const currentUsers = this.props.users || {};
|
2017-09-01 23:26:57 +08:00
|
|
|
const nextUsers = nextProps.users;
|
|
|
|
|
2018-01-25 11:48:49 +08:00
|
|
|
let users = {};
|
|
|
|
let present = {};
|
2017-09-01 23:26:57 +08:00
|
|
|
|
2018-01-25 11:48:49 +08:00
|
|
|
if (!currentUsers)
|
|
|
|
return false;
|
2017-09-01 23:26:57 +08:00
|
|
|
|
|
|
|
|
2018-01-25 11:48:49 +08:00
|
|
|
// Map user objectos to an object in the form {userId: has_stream}
|
|
|
|
currentUsers.forEach((user) => {
|
|
|
|
users[user.userId] = user.has_stream;
|
|
|
|
});
|
2017-09-01 23:26:57 +08:00
|
|
|
|
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) => {
|
|
|
|
let 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
|
|
|
}
|
|
|
|
|
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++) {
|
|
|
|
let id = userIds[i];
|
|
|
|
|
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) {
|
|
|
|
this.start(id, false);
|
2017-09-01 23:26:57 +08:00
|
|
|
}
|
|
|
|
}
|
2018-01-25 11:48:49 +08:00
|
|
|
else {
|
|
|
|
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);
|