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

427 lines
11 KiB
React
Raw Normal View History

import React, { Component } from 'react';
import ScreenshareContainer from '/imports/ui/components/screenshare/container';
import styles from './styles';
import { log } from '/imports/ui/services/api';
window.addEventListener('resize', () => {
window.adjustVideos('webcamArea', true);
});
2017-10-11 06:08:51 +08:00
class VideoElement extends Component {
constructor(props) {
super(props);
2017-09-01 23:26:57 +08:00
}
}
export default class VideoDock extends Component {
2017-09-01 23:26:57 +08:00
constructor(props) {
super(props);
this.state = {
2017-12-06 03:13:42 +08:00
videos: {},
};
2017-09-01 23:26:57 +08:00
this.state = {
// Set a valid kurento application server socket in the settings
ws: new ReconnectingWebSocket(Meteor.settings.public.kurento.wsUrl),
webRtcPeers: {},
wsQueue: [],
reconnectWebcam: false,
reconnectList: [],
sharedCameraTimeout: null,
2017-12-06 03:13:42 +08:00
subscribedCamerasTimeouts: [],
2017-09-01 23:26:57 +08:00
};
window.ws = this.state.ws;
this.state.ws.addEventListener('open', () => {
2017-12-06 03:13:42 +08:00
log('debug', '------ Websocket connection opened.');
// -- Resend queued messages that happened when socket was not connected
while (this.state.wsQueue.length > 0) {
this.sendMessage(this.state.wsQueue.pop());
}
this.reconnectVideos();
});
this.state.ws.addEventListener('close', (error) => {
2017-12-06 03:13:42 +08:00
log('debug', '------ Websocket connection closed.');
this.setupReconnectVideos();
});
2017-09-01 23:26:57 +08:00
this.sendUserShareWebcam = props.sendUserShareWebcam.bind(this);
this.sendUserUnshareWebcam = props.sendUserUnshareWebcam.bind(this);
this.unshareWebcam = this.unshareWebcam.bind(this);
this.shareWebcam = this.shareWebcam.bind(this);
}
setupReconnectVideos() {
for (id in this.state.webRtcPeers) {
this.disconnected(id);
this.stop(id);
}
}
reconnectVideos() {
2017-12-06 03:13:42 +08:00
for (i in this.state.reconnectList) {
const id = this.state.reconnectList[i];
if (id != this.state.myId) {
2017-12-06 03:13:42 +08:00
log('debug', ` [camera] Trying to reconnect camera ${id}`);
this.start(id, false, this.refs.videoInput);
}
}
if (this.state.reconnectWebcam) {
2017-12-06 03:13:42 +08:00
log('debug', ` [camera] Trying to re-share ${this.state.myId} after reconnect.`);
this.start(this.state.myId, true, this.refs.videoInput);
}
2017-12-06 03:13:42 +08:00
this.setState({ reconnectWebcam: false, reconnectList: [] });
}
2017-09-01 23:26:57 +08:00
componentDidMount() {
2017-09-20 11:12:10 +08:00
const that = this;
2017-09-01 23:26:57 +08:00
const ws = this.state.ws;
const { users } = this.props;
for (let i = 0; i < users.length; i++) {
if (users[i].has_stream) {
this.start(users[i].userId, false, this.refs.videoInput);
}
}
2017-09-01 23:26:57 +08:00
document.addEventListener('joinVideo', () => { that.shareWebcam(); });// TODO find a better way to do this
document.addEventListener('exitVideo', () => { that.unshareWebcam(); });
2017-09-20 11:12:10 +08:00
2017-09-01 23:26:57 +08:00
ws.addEventListener('message', (msg) => {
const parsedMessage = JSON.parse(msg.data);
console.log('Received message new ws message: ');
console.log(parsedMessage);
2017-09-01 23:26:57 +08:00
switch (parsedMessage.id) {
case 'startResponse':
this.startResponse(parsedMessage);
break;
case 'error':
this.handleError(parsedMessage);
break;
case 'playStart':
this.handlePlayStart(parsedMessage);
break;
case 'playStop':
this.handlePlayStop(parsedMessage);
break;
case 'iceCandidate':
const webRtcPeer = this.state.webRtcPeers[parsedMessage.cameraId];
if (webRtcPeer !== null) {
webRtcPeer.addIceCandidate(parsedMessage.candidate, (err) => {
if (err) {
console.error(`Error adding candidate: ${err}`);
2017-09-01 23:26:57 +08:00
}
});
} else {
log('error', ' [ICE] Message arrived before webRtcPeer?');
2017-09-01 23:26:57 +08:00
}
break;
}
});
}
start(id, shareWebcam, videoInput) {
const that = this;
const ws = this.state.ws;
console.log(`Starting video call for video: ${id}`);
log('info', 'Creating WebRtcPeer and generating local sdp offer ...');
2017-09-01 23:26:57 +08:00
const onIceCandidate = function (candidate) {
const message = {
type: 'video',
2017-12-06 03:13:42 +08:00
role: shareWebcam ? 'share' : 'viewer',
2017-09-01 23:26:57 +08:00
id: 'onIceCandidate',
candidate,
cameraId: id,
};
that.sendMessage(message);
};
const options = {
2017-12-06 03:13:42 +08:00
mediaConstraints: {
audio: false,
video: {
2017-12-06 03:13:42 +08:00
width: { min: 320, ideal: 320 },
height: { min: 240, ideal: 240 },
frameRate: { min: 5, ideal: 10 },
},
},
2017-09-01 23:26:57 +08:00
onicecandidate: onIceCandidate,
};
let peerObj;
if (shareWebcam) {
options.localVideo = videoInput;
peerObj = kurentoUtils.WebRtcPeer.WebRtcPeerSendonly;
} else {
peerObj = kurentoUtils.WebRtcPeer.WebRtcPeerRecvonly;
2017-12-06 03:13:42 +08:00
options.remoteVideo = this.createVideoTag(id);
2017-09-01 23:26:57 +08:00
}
this.state.webRtcPeers[id] = 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');
that.destroyWebRTCPeer(id);
that.destroyVideoTag(id);
return log('error', error);
2017-09-01 23:26:57 +08:00
}
if (shareWebcam) {
that.state.sharedWebcam = that.state.webRtcPeers[id];
that.state.myId = id;
}
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);
return log('error', error);
2017-09-01 23:26:57 +08:00
}
console.log(`Invoking SDP offer callback function ${location.host}`);
2017-09-01 23:26:57 +08:00
const message = {
type: 'video',
2017-12-06 03:13:42 +08:00
role: shareWebcam ? 'share' : 'viewer',
2017-09-01 23:26:57 +08:00
id: 'start',
sdpOffer: offerSdp,
cameraId: id,
cameraShared: shareWebcam,
};
that.sendMessage(message);
});
});
}
disconnected(id) {
if (this.state.sharedWebcam && this.state.myId == id) {
2017-12-06 03:13:42 +08:00
log('debug', ' [camera] Disconnected, will try re-share webcam later.');
this.setState({ reconnectWebcam: true });
} else {
2017-12-06 03:13:42 +08:00
const reconnectList = this.state.reconnectList;
reconnectList.push(id);
2017-12-06 03:13:42 +08:00
log('debug', ` [camera] ${id} disconnected, will try re-subscribe later.`);
2017-12-06 03:13:42 +08:00
this.setState({ reconnectList });
}
}
2017-09-01 23:26:57 +08:00
stop(id) {
const { users } = this.props;
if (id == users[0].userId) {
this.unshareWebcam();
}
2017-12-06 03:13:42 +08:00
this.destroyWebRTCPeer(id);
this.destroyVideoTag(id);
this.sendMessage({
type: 'video',
role: 'any',
id: 'stop',
cameraId: id,
});
window.adjustVideos('webcamArea', true);
}
createVideoTag(id) {
let remoteVideo = {};
remoteVideo = document.createElement('video');
remoteVideo.id = `video-elem-${id}`;
remoteVideo.width = 320;
remoteVideo.height = 240;
remoteVideo.autoplay = true;
remoteVideo.playsinline = true;
document.getElementById('webcamArea').appendChild(remoteVideo);
return remoteVideo;
}
destroyVideoTag(id) {
const videoTag = document.getElementById(`video-elem-${id}`);
if (videoTag) {
document.getElementById('webcamArea').removeChild(videoTag);
}
}
destroyWebRTCPeer(id) {
2017-09-01 23:26:57 +08:00
const webRtcPeer = this.state.webRtcPeers[id];
if (webRtcPeer) {
log('info', 'Stopping WebRTC peer');
2017-09-01 23:26:57 +08:00
webRtcPeer.dispose();
delete this.state.webRtcPeers[id];
} 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
}
2017-12-06 03:13:42 +08:00
if (id == this.state.myId && this.state.sharedWebcam) {
this.state.sharedWebcam.dispose();
this.state.sharedWebcam = null;
this.state.myId = null;
} else {
log('info', 'No shared camera WebRTC peer to stop (not an error)');
2017-09-01 23:26:57 +08:00
}
}
shareWebcam() {
const { users } = this.props;
2017-09-19 21:53:27 +08:00
const id = users[0].userId;
2017-09-01 23:26:57 +08:00
this.start(id, true, this.refs.videoInput);
}
unshareWebcam() {
2017-12-06 03:13:42 +08:00
log('info', 'Unsharing webcam');
2017-09-19 21:53:27 +08:00
const { users } = this.props;
const id = users[0].userId;
this.sendUserUnshareWebcam(id);
2017-09-01 23:26:57 +08:00
}
startResponse(message) {
const id = message.cameraId;
const webRtcPeer = this.state.webRtcPeers[id];
if (message.sdpAnswer == null) {
return log('debug', 'Null sdp answer. Camera unplugged?');
2017-09-01 23:26:57 +08:00
}
if (webRtcPeer == null) {
return log('debug', 'Null webrtc peer ????');
2017-09-01 23:26:57 +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
}
});
2017-09-19 21:53:27 +08:00
this.sendUserShareWebcam(id);
2017-09-01 23:26:57 +08:00
}
sendMessage(message) {
const ws = this.state.ws;
if (this.connectedToMediaServer()) {
const jsonMessage = JSON.stringify(message);
console.log(`Sending message: ${jsonMessage}`);
ws.send(jsonMessage, (error) => {
if (error) {
console.error(`client: Websocket error "${error}" on message "${jsonMessage.id}"`);
}
});
} else {
// No need to queue video stop messages
2017-12-06 03:13:42 +08:00
if (message.id != 'stop') {
this.state.wsQueue.push(message);
}
}
2017-09-01 23:26:57 +08:00
}
connectedToMediaServer() {
return ws.readyState == WebSocket.OPEN;
}
connectionStatus() {
return ws.readyState;
}
2017-09-01 23:26:57 +08:00
handlePlayStop(message) {
log('info', 'Handle play stop <--------------------');
2017-09-01 23:26:57 +08:00
this.stop(message.cameraId);
}
handlePlayStart(message) {
log('info', 'Handle play start <===================');
2017-09-01 23:26:57 +08:00
window.adjustVideos('webcamArea', true);
2017-09-01 23:26:57 +08:00
}
handleError(message) {
2017-12-06 03:13:42 +08:00
console.error(' Handle error --------------------->');
log('debug', message.message);
2017-09-01 23:26:57 +08:00
}
render() {
return (
2017-09-01 23:26:57 +08:00
<div className={styles.videoDock}>
2017-09-01 23:26:57 +08:00
<div id="webcamArea" />
<video id="shareWebcamVideo" className={styles.sharedWebcamVideo} ref="videoInput" />
</div>
);
}
2017-09-01 23:26:57 +08:00
componentWillUnmount() {
// Close websocket connection to prevent multiple reconnects from happening
this.state.ws.close();
this.state.ws.removeEventListener('message', () => {});
}
2017-09-01 23:26:57 +08:00
shouldComponentUpdate(nextProps, nextState) {
const { users } = this.props;
const nextUsers = nextProps.users;
if (users) {
let suc = false;
for (let i = 0; i < users.length; i++) {
2017-09-19 21:53:27 +08:00
if (users && users[i] &&
nextUsers && nextUsers[i]) {
if (users[i].has_stream !== nextUsers[i].has_stream) {
console.log(`User ${nextUsers[i].has_stream ? '' : 'un'}shared webcam ${users[i].userId}`);
2017-09-01 23:26:57 +08:00
2017-09-19 21:53:27 +08:00
if (nextUsers[i].has_stream) {
this.start(users[i].userId, false, this.refs.videoInput);
2017-09-01 23:26:57 +08:00
} else {
2017-09-19 21:53:27 +08:00
this.stop(users[i].userId);
2017-09-01 23:26:57 +08:00
}
suc = suc || true;
}
}
}
return true;
}
return false;
}
}