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

934 lines
29 KiB
React
Raw Normal View History

2018-02-17 03:18:53 +08:00
import React, { Component } from 'react';
import { defineMessages, injectIntl } from 'react-intl';
import { notify } from '/imports/ui/services/notification';
import VisibilityEvent from '/imports/utils/visibilityEvent';
import { fetchWebRTCMappedStunTurnServers } from '/imports/utils/fetchStunTurnServers';
import ReconnectingWebSocket from 'reconnecting-websocket';
import logger from '/imports/startup/client/logger';
2018-11-08 03:30:36 +08:00
import { Session } from 'meteor/session';
2018-02-17 03:18:53 +08:00
import VideoService from './service';
import VideoList from './video-list/component';
2018-02-17 03:18:53 +08:00
const VIDEO_CONSTRAINTS = Meteor.settings.public.kurento.cameraConstraints;
const intlClientErrors = defineMessages({
2018-02-17 03:18:53 +08:00
iceCandidateError: {
id: 'app.video.iceCandidateError',
description: 'Error message for ice candidate fail',
},
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',
},
permissionError: {
id: 'app.video.permissionError',
description: 'Error message for webcam permission',
},
NotFoundError: {
id: 'app.video.notFoundError',
description: 'error message when can not get webcam video',
},
NotAllowedError: {
id: 'app.video.notAllowed',
description: 'error message when webcam had permission denied',
},
NotSupportedError: {
id: 'app.video.notSupportedError',
description: 'error message when origin do not have ssl valid',
},
NotReadableError: {
id: 'app.video.notReadableError',
description: 'error message When the webcam is being used by other software',
},
iceConnectionStateError: {
id: 'app.video.iceConnectionStateError',
description: 'Error message for ice connection state being failed',
},
mediaFlowTimeout: {
id: 'app.video.mediaFlowTimeout1020',
description: 'Error message when media could not go through the server within the specified period',
},
});
const intlSFUErrors = defineMessages({
2000: {
id: 'app.sfu.mediaServerConnectionError2000',
description: 'Error message fired when the SFU cannot connect to the media server',
},
2001: {
id: 'app.sfu.mediaServerOffline2001',
description: 'error message when SFU is offline',
},
2002: {
id: 'app.sfu.mediaServerNoResources2002',
description: 'Error message fired when the media server lacks disk, CPU or FDs',
},
2003: {
id: 'app.sfu.mediaServerRequestTimeout2003',
description: 'Error message fired when requests are timing out due to lack of resources',
},
2021: {
id: 'app.sfu.serverIceGatheringFailed2021',
description: 'Error message fired when the server cannot enact ICE gathering',
},
2022: {
id: 'app.sfu.serverIceStateFailed2022',
description: 'Error message fired when the server endpoint transitioned to a FAILED ICE state',
},
2200: {
id: 'app.sfu.mediaGenericError2200',
description: 'Error message fired when the SFU component generated a generic error',
},
2202: {
id: 'app.sfu.invalidSdp2202',
description: 'Error message fired when the clients provides an invalid SDP',
},
2203: {
id: 'app.sfu.noAvailableCodec2203',
description: 'Error message fired when the server has no available codec for the client',
},
2018-02-17 03:18:53 +08:00
});
const CAMERA_SHARE_FAILED_WAIT_TIME = 15000;
const MAX_CAMERA_SHARE_FAILED_WAIT_TIME = 60000;
const PING_INTERVAL = 15000;
2018-02-17 03:18:53 +08:00
class VideoProvider extends Component {
constructor(props) {
super(props);
this.state = {
socketOpen: false,
2018-05-03 03:03:36 +08:00
stats: [],
2018-02-17 03:18:53 +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.visibility = new VisibilityEvent();
this.restartTimeout = {};
this.restartTimer = {};
2018-02-17 03:18:53 +08:00
this.webRtcPeers = {};
this.monitoredTracks = {};
this.videoTags = {};
this.sharedWebcam = false;
2018-02-17 03:18:53 +08:00
2018-12-18 06:19:26 +08:00
this.createVideoTag = this.createVideoTag.bind(this);
this.getStats = this.getStats.bind(this);
this.stopGettingStats = this.stopGettingStats.bind(this);
2018-02-17 03:18:53 +08:00
this.onWsOpen = this.onWsOpen.bind(this);
this.onWsClose = this.onWsClose.bind(this);
this.onWsMessage = this.onWsMessage.bind(this);
this.unshareWebcam = this.unshareWebcam.bind(this);
this.shareWebcam = this.shareWebcam.bind(this);
this.pauseViewers = this.pauseViewers.bind(this);
this.unpauseViewers = this.unpauseViewers.bind(this);
2018-05-26 03:24:41 +08:00
this.customGetStats = this.customGetStats.bind(this);
}
2018-02-17 03:18:53 +08:00
componentWillMount() {
2018-12-18 06:19:26 +08:00
this.ws.onopen = this.onWsOpen;
this.ws.onclose = this.onWsClose;
2018-02-17 03:18:53 +08:00
window.addEventListener('online', this.openWs);
2018-02-17 03:18:53 +08:00
window.addEventListener('offline', this.onWsClose);
}
componentDidMount() {
document.addEventListener('joinVideo', this.shareWebcam); // TODO find a better way to do this
document.addEventListener('exitVideo', this.unshareWebcam);
this.ws.onmessage = this.onWsMessage;
2018-11-17 03:03:20 +08:00
window.addEventListener('beforeunload', this.unshareWebcam);
this.visibility.onVisible(this.unpauseViewers);
this.visibility.onHidden(this.pauseViewers);
2018-02-17 03:18:53 +08:00
}
componentWillUpdate({ users, userId }) {
const usersSharingIds = users.map(u => u.id);
const usersConnected = Object.keys(this.webRtcPeers);
const usersToConnect = usersSharingIds.filter(id => !usersConnected.includes(id));
const usersToDisconnect = usersConnected.filter(id => !usersSharingIds.includes(id));
usersToConnect.forEach(id => this.createWebRTCPeer(id, userId === id));
usersToDisconnect.forEach(id => this.stopWebRTCPeer(id));
}
2018-02-17 03:18:53 +08:00
componentWillUnmount() {
document.removeEventListener('joinVideo', this.shareWebcam);
document.removeEventListener('exitVideo', this.unshareWebcam);
this.ws.onmessage = null;
this.ws.onopen = null;
this.ws.onclose = null;
2018-02-17 03:18:53 +08:00
window.removeEventListener('online', this.openWs);
2018-02-17 03:18:53 +08:00
window.removeEventListener('offline', this.onWsClose);
window.removeEventListener('beforeunload', this.unshareWebcam);
2018-02-17 03:18:53 +08:00
this.visibility.removeEventListeners();
2018-02-17 03:18:53 +08:00
// Unshare user webcam
if (this.sharedWebcam) {
2018-02-17 03:18:53 +08:00
this.unshareWebcam();
}
Object.keys(this.webRtcPeers).forEach((id) => {
2018-04-27 07:35:05 +08:00
this.stopGettingStats(id);
this.stopWebRTCPeer(id);
2018-02-17 03:18:53 +08:00
});
// Close websocket connection to prevent multiple reconnects from happening
this.ws.close();
2018-02-17 03:18:53 +08:00
}
2018-12-18 06:19:26 +08:00
onWsMessage(msg) {
const parsedMessage = JSON.parse(msg.data);
2018-02-17 03:18:53 +08:00
2018-12-18 06:19:26 +08:00
this.logger('debug', `Received new message '${parsedMessage.id}'`, { topic: 'ws', message: parsedMessage });
2018-02-17 03:18:53 +08:00
2018-12-18 06:19:26 +08:00
switch (parsedMessage.id) {
case 'startResponse':
this.startResponse(parsedMessage);
break;
2018-12-18 06:19:26 +08:00
case 'playStart':
this.handlePlayStart(parsedMessage);
break;
case 'playStop':
this.handlePlayStop(parsedMessage);
break;
case 'iceCandidate':
this.handleIceCandidate(parsedMessage);
break;
case 'pong':
break;
case 'error':
default:
this.handleSFUError(parsedMessage);
break;
}
2018-02-17 03:18:53 +08:00
}
2018-12-18 06:19:26 +08:00
onWsClose() {
const {
intl,
} = this.props;
this.logger('debug', '------ Websocket connection closed.', { topic: 'ws' });
2018-02-17 03:18:53 +08:00
clearInterval(this.pingInterval);
if (this.sharedWebcam) {
this.unshareWebcam();
}
this.setState({ socketOpen: false });
2018-02-17 03:18:53 +08:00
}
2018-12-18 06:19:26 +08:00
onWsOpen() {
this.logger('debug', '------ Websocket connection opened.', { topic: 'ws' });
// -- Resend queued messages that happened when socket was not connected
while (this.wsQueue.length > 0) {
this.sendMessage(this.wsQueue.pop());
}
this.pingInterval = setInterval(this.ping.bind(this), PING_INTERVAL);
this.setState({ socketOpen: true });
}
2018-12-18 06:19:26 +08:00
getStats(id, video, callback) {
const peer = this.webRtcPeers[id];
2018-02-17 03:18:53 +08:00
2018-12-18 06:19:26 +08:00
const hasLocalStream = peer && peer.started === true
&& peer.peerConnection.getLocalStreams().length > 0;
const hasRemoteStream = peer && peer.started === true
&& peer.peerConnection.getRemoteStreams().length > 0;
2018-02-17 03:18:53 +08:00
2018-12-18 06:19:26 +08:00
if (hasLocalStream) {
this.monitorTrackStart(peer.peerConnection,
peer.peerConnection.getLocalStreams()[0].getVideoTracks()[0], true, callback);
} else if (hasRemoteStream) {
this.monitorTrackStart(peer.peerConnection,
peer.peerConnection.getRemoteStreams()[0].getVideoTracks()[0], false, callback);
}
}
2018-02-17 03:18:53 +08:00
2018-12-18 06:19:26 +08:00
logger(type, message, options = {}) {
const { userId, userName } = this.props;
const topic = options.topic || 'video';
2018-02-17 03:18:53 +08:00
2018-12-18 06:19:26 +08:00
logger[type]({ obj: Object.assign(options, { userId, userName, topic }) }, `[${topic}] ${message}`);
}
2018-02-17 03:18:53 +08:00
2018-12-18 06:19:26 +08:00
_sendPauseStream(id, role, state) {
this.sendMessage({
cameraId: id,
id: 'pause',
type: 'video',
role,
state,
});
}
2018-02-17 03:18:53 +08:00
2018-12-18 06:19:26 +08:00
pauseViewers() {
const { userId } = this.props;
this.logger('debug', 'Calling pause in viewer streams');
2018-12-18 06:19:26 +08:00
Object.keys(this.webRtcPeers).forEach((id) => {
if (userId !== id && this.webRtcPeers[id].started) {
this._sendPauseStream(id, 'viewer', true);
}
});
}
unpauseViewers() {
const { userId } = this.props;
this.logger('debug', 'Calling un-pause in viewer streams');
Object.keys(this.webRtcPeers).forEach((id) => {
if (id !== userId && this.webRtcPeers[id].started) {
this._sendPauseStream(id, 'viewer', false);
}
});
}
ping() {
const message = {
id: 'ping',
};
this.sendMessage(message);
2018-02-17 03:18:53 +08:00
}
sendMessage(message) {
2018-06-15 03:20:14 +08:00
const { ws } = this;
2018-02-17 03:18:53 +08:00
if (this.connectedToMediaServer()) {
const jsonMessage = JSON.stringify(message);
this.logger('debug', `Sending message '${message.id}'`, { topic: 'ws', message });
2018-02-17 03:18:53 +08:00
ws.send(jsonMessage, (error) => {
if (error) {
this.logger(`client: Websocket error '${error}' on message '${message.id}'`, { topic: 'ws' });
2018-02-17 03:18:53 +08:00
}
});
} else {
// No need to queue video stop messages
2018-06-15 03:20:14 +08:00
if (message.id !== 'stop') {
2018-02-17 03:18:53 +08:00
this.wsQueue.push(message);
}
}
}
connectedToMediaServer() {
return this.ws.readyState === WebSocket.OPEN;
}
startResponse(message) {
const id = message.cameraId;
const peer = this.webRtcPeers[id];
2018-02-17 03:18:53 +08:00
this.logger('debug', 'SDP answer received from server. Processing ...', { cameraId: id, sdpAnswer: message.sdpAnswer });
2018-02-17 03:18:53 +08:00
if (peer) {
peer.processAnswer(message.sdpAnswer, (error) => {
if (error) {
return this.logger('debug', JSON.stringify(error), { cameraId: id });
}
peer.didSDPAnswered = true;
this._processIceQueue(peer, id);
});
} else {
this.logger('warn', '[startResponse] Message arrived after the peer was already thrown out, discarding it...');
}
2018-02-17 03:18:53 +08:00
}
handleIceCandidate(message) {
const webRtcPeer = this.webRtcPeers[message.cameraId];
this.logger('debug', 'Received remote ice candidate', { topic: 'ice', candidate: message.candidate });
2018-02-17 03:18:53 +08:00
if (webRtcPeer) {
if (webRtcPeer.didSDPAnswered) {
webRtcPeer.addIceCandidate(message.candidate, (err) => {
if (err) {
return this.logger('error', `Error adding candidate: ${err}`, { cameraId: message.cameraId });
2018-02-17 03:18:53 +08:00
}
});
} else {
if (webRtcPeer.iceQueue == null) {
webRtcPeer.iceQueue = [];
}
2018-02-17 03:18:53 +08:00
webRtcPeer.iceQueue.push(message.candidate);
}
} else {
this.logger('warn', ' [iceCandidate] Message arrived after the peer was already thrown out, discarding it...', { cameraId: message.cameraId });
2018-02-17 03:18:53 +08:00
}
}
2018-12-18 22:54:17 +08:00
stopWebRTCPeer(id, restarting = false) {
this.logger('info', 'Stopping webcam', { cameraId: id });
2018-06-15 03:20:14 +08:00
const { userId } = this.props;
const shareWebcam = id === userId;
2018-02-17 03:18:53 +08:00
// in this case, 'closed' state is not caused by an error;
// we stop listening to prevent this from being treated as an error
if (this.webRtcPeers[id] && this.webRtcPeers[id].peerConnection) {
this.webRtcPeers[id].peerConnection.oniceconnectionstatechange = null;
}
if (shareWebcam) {
this.unshareWebcam();
}
this.sendMessage({
type: 'video',
role: shareWebcam ? 'share' : 'viewer',
id: 'stop',
cameraId: id,
});
// Clear the shared camera media flow timeout when destroying it
2018-12-18 22:54:17 +08:00
if (!restarting) {
if (this.restartTimeout[id]) {
clearTimeout(this.restartTimeout[id]);
}
2018-12-18 22:54:17 +08:00
if (this.restartTimer[id]) {
delete this.restartTimer[id];
}
}
this.destroyWebRTCPeer(id);
}
2018-02-17 03:18:53 +08:00
destroyWebRTCPeer(id) {
const webRtcPeer = this.webRtcPeers[id];
2018-02-17 03:18:53 +08:00
if (webRtcPeer) {
this.logger('info', 'Stopping WebRTC peer', { cameraId: id });
if (typeof webRtcPeer.dispose === 'function') {
webRtcPeer.dispose();
}
2018-02-17 03:18:53 +08:00
delete this.webRtcPeers[id];
} else {
this.logger('warn', 'No WebRTC peer to stop (not an error)', { cameraId: id });
2018-02-17 03:18:53 +08:00
}
}
async createWebRTCPeer(id, shareWebcam) {
const { meetingId, sessionToken, voiceBridge } = this.props;
let iceServers = [];
2018-02-17 03:18:53 +08:00
// Check if the peer is already being processed
if (this.webRtcPeers[id]) {
return;
}
this.webRtcPeers[id] = {};
try {
iceServers = await fetchWebRTCMappedStunTurnServers(sessionToken);
} catch (error) {
this.logger('error', 'Video provider failed to fetch ice servers, using default');
} finally {
2018-11-07 07:10:56 +08:00
if (Session.get('WebcamDeviceId')) {
VIDEO_CONSTRAINTS.deviceId = { exact: Session.get('WebcamDeviceId') };
}
const options = {
mediaConstraints: {
audio: false,
video: VIDEO_CONSTRAINTS,
},
onicecandidate: this._getOnIceCandidateCallback(id, shareWebcam),
};
2018-02-17 05:11:59 +08:00
if (iceServers.length > 0) {
options.configuration = {};
options.configuration.iceServers = iceServers;
}
let WebRtcPeerObj;
if (shareWebcam) {
WebRtcPeerObj = window.kurentoUtils.WebRtcPeer.WebRtcPeerSendonly;
this.shareWebcam();
} else {
WebRtcPeerObj = window.kurentoUtils.WebRtcPeer.WebRtcPeerRecvonly;
}
this.webRtcPeers[id] = new WebRtcPeerObj(options, (error) => {
const peer = this.webRtcPeers[id];
peer.started = false;
peer.attached = false;
peer.didSDPAnswered = false;
if (peer.iceQueue == null) {
peer.iceQueue = [];
}
2018-02-17 03:18:53 +08:00
if (error) {
return this._webRTCOnError(error, id, shareWebcam);
2018-02-17 03:18:53 +08:00
}
peer.generateOffer((errorGenOffer, offerSdp) => {
if (errorGenOffer) {
return this._webRTCOnError(errorGenOffer, id, shareWebcam);
}
this.logger('debug', `Invoking SDP offer callback function ${location.host}`, { cameraId: id, offerSdp });
const message = {
type: 'video',
role: shareWebcam ? 'share' : 'viewer',
id: 'start',
sdpOffer: offerSdp,
cameraId: id,
meetingId,
voiceBridge,
};
this.sendMessage(message);
});
2018-02-17 03:18:53 +08:00
});
if (this.webRtcPeers[id].peerConnection) {
2018-12-18 06:19:26 +08:00
this.webRtcPeers[id].peerConnection.oniceconnectionstatechange = this._getOnIceConnectionStateChangeCallback(id);
}
}
2018-02-17 03:18:53 +08:00
}
2018-12-18 06:19:26 +08:00
_getWebRTCStartTimeout(id, shareWebcam) {
const { intl, userId } = this.props;
return () => {
this.logger('error', `Camera share has not suceeded in ${CAMERA_SHARE_FAILED_WAIT_TIME}`, { cameraId: id });
2018-12-18 06:19:26 +08:00
if (userId === id) {
this.notifyError(intl.formatMessage(intlClientErrors.mediaFlowTimeout));
2018-12-18 22:54:17 +08:00
this.stopWebRTCPeer(id, false);
} else {
2018-12-18 22:54:17 +08:00
// Subscribers try to reconnect according to their timers if media could
// not reach the server. That's why we pass the restarting flag as true
// to the stop procedure as to not destroy the timers
this.stopWebRTCPeer(id, true);
this.createWebRTCPeer(id, shareWebcam);
// Increment reconnect interval
2018-06-15 03:20:14 +08:00
this.restartTimer[id] = Math.min(2 * this.restartTimer[id], MAX_CAMERA_SHARE_FAILED_WAIT_TIME);
2018-11-08 03:30:36 +08:00
this.logger('info', `Reconnecting peer ${id} with timer`, this.restartTimer);
}
};
}
_processIceQueue(peer, cameraId) {
const { intl } = this.props;
while (peer.iceQueue.length) {
const candidate = peer.iceQueue.shift();
peer.addIceCandidate(candidate, (err) => {
if (err) {
this.notifyError(intl.formatMessage(intlClientErrors.iceCandidateError));
return this.logger('error', `Error adding candidate: ${err}`, { cameraId });
}
});
}
}
2018-12-18 06:19:26 +08:00
_webRTCOnError(error, id) {
const { intl, userId } = this.props;
// We only display SFU connection errors to sharers, because it's guaranteed
// they should be connected. Viewers aren't connected synchronously related
// to the createWebRTCPeer procedure, so the error is ignored. If the connection
// closes unexpectedly, this error is deplayed globally in the onWsClose catch
if (error === 2001 && userId !== id) {
return;
}
this.logger('error', ' WebRTC peerObj create error', id);
const errorMessage = intlClientErrors[error.name]
|| intlSFUErrors[error] || intlClientErrors.permissionError;
this.notifyError(intl.formatMessage(errorMessage));
this.stopWebRTCPeer(id);
return this.logger('error', errorMessage, { cameraId: id });
}
_getOnIceCandidateCallback(id, shareWebcam) {
const peer = this.webRtcPeers[id];
return (candidate) => {
// Setup a timeout only when ice first is generated
if (!this.restartTimeout[id]) {
this.restartTimer[id] = this.restartTimer[id] || CAMERA_SHARE_FAILED_WAIT_TIME;
this.logger('debug', `Setting a camera connection restart in ${this.restartTimer[id]}`, { cameraId: id });
this.restartTimeout[id] = setTimeout(this._getWebRTCStartTimeout(id, shareWebcam, peer), this.restartTimer[id]);
}
2018-02-17 03:18:53 +08:00
this.logger('debug', 'Generated local ice candidate', { topic: 'ice', candidate });
2018-02-17 03:18:53 +08:00
const message = {
type: 'video',
role: shareWebcam ? 'share' : 'viewer',
id: 'onIceCandidate',
candidate,
cameraId: id,
};
this.sendMessage(message);
};
}
2018-02-17 03:18:53 +08:00
_getOnIceConnectionStateChangeCallback(id) {
const { intl } = this.props;
const peer = this.webRtcPeers[id];
2018-12-18 06:19:26 +08:00
return () => {
const connectionState = peer.peerConnection.iceConnectionState;
if (connectionState === 'failed' || connectionState === 'closed') {
// prevent the same error from being detected multiple times
peer.peerConnection.oniceconnectionstatechange = null;
this.logger('error', 'ICE connection state', id);
this.stopWebRTCPeer(id);
this.notifyError(intl.formatMessage(intlClientErrors.iceConnectionStateError));
}
};
}
attachVideoStream(id) {
2018-12-18 06:19:26 +08:00
const { userId } = this.props;
const video = this.videoTags[id];
if (video == null) {
this.logger('warn', 'Peer', id, 'has not been started yet');
return;
}
if (video.srcObject) {
delete this.videoTags[id];
return; // Skip if the stream is already attached
}
2018-02-17 03:18:53 +08:00
2018-12-18 06:19:26 +08:00
const isCurrent = id === userId;
const peer = this.webRtcPeers[id];
const attachVideoStreamHelper = () => {
const stream = isCurrent ? peer.getLocalStream() : peer.getRemoteStream();
video.pause();
video.srcObject = stream;
video.load();
peer.attached = true;
delete this.videoTags[id];
};
// If peer has started playing attach to tag, otherwise wait a while
if (peer) {
if (peer.started) {
attachVideoStreamHelper();
}
2018-02-17 03:18:53 +08:00
// So we can start it later when we get a playStart
// or if we need to do a restart timeout
peer.videoTag = video;
}
2018-02-17 03:18:53 +08:00
}
createVideoTag(id, video) {
const peer = this.webRtcPeers[id];
this.videoTags[id] = video;
if (peer) {
this.attachVideoStream(id);
}
}
2018-12-18 06:19:26 +08:00
customGetStats(peer, mediaStreamTrack, callback) {
const { stats } = this.state;
const statsState = stats;
2018-05-03 03:03:36 +08:00
let promise;
try {
promise = peer.getStats(mediaStreamTrack);
} catch (e) {
promise = Promise.reject(e);
}
2018-05-26 03:24:41 +08:00
promise.then((results) => {
2018-05-03 03:03:36 +08:00
let videoInOrOutbound = {};
2018-06-15 03:20:14 +08:00
results.forEach((res) => {
if (res.type === 'ssrc' || res.type === 'inbound-rtp' || res.type === 'outbound-rtp') {
2018-12-18 06:19:26 +08:00
res.packetsSent = parseInt(res.packetsSent, 10);
res.packetsLost = parseInt(res.packetsLost, 10) || 0;
res.packetsReceived = parseInt(res.packetsReceived, 10);
2018-05-03 02:42:36 +08:00
2018-06-15 03:20:14 +08:00
if ((isNaN(res.packetsSent) && res.packetsReceived === 0)
|| (res.type === 'outbound-rtp' && res.isRemote)) {
2018-05-03 02:42:36 +08:00
return; // Discard local video receiving
}
if (res.googFrameWidthReceived) {
2018-12-18 06:19:26 +08:00
res.width = parseInt(res.googFrameWidthReceived, 10);
res.height = parseInt(res.googFrameHeightReceived, 10);
2018-05-03 02:42:36 +08:00
} else if (res.googFrameWidthSent) {
2018-12-18 06:19:26 +08:00
res.width = parseInt(res.googFrameWidthSent, 10);
res.height = parseInt(res.googFrameHeightSent, 10);
}
2018-05-03 02:42:36 +08:00
// Extra fields available on Chrome
if (res.googCodecName) res.codec = res.googCodecName;
if (res.googDecodeMs) res.decodeDelay = res.googDecodeMs;
if (res.googEncodeUsagePercent) res.encodeUsagePercent = res.googEncodeUsagePercent;
if (res.googRtt) res.rtt = res.googRtt;
if (res.googCurrentDelayMs) res.currentDelay = res.googCurrentDelayMs;
videoInOrOutbound = res;
}
});
2018-05-03 03:03:36 +08:00
const videoStats = {
timestamp: videoInOrOutbound.timestamp,
bytesReceived: videoInOrOutbound.bytesReceived,
bytesSent: videoInOrOutbound.bytesSent,
packetsReceived: videoInOrOutbound.packetsReceived,
packetsLost: videoInOrOutbound.packetsLost,
packetsSent: videoInOrOutbound.packetsSent,
decodeDelay: videoInOrOutbound.decodeDelay,
codec: videoInOrOutbound.codec,
height: videoInOrOutbound.height,
width: videoInOrOutbound.width,
encodeUsagePercent: videoInOrOutbound.encodeUsagePercent,
rtt: videoInOrOutbound.rtt,
currentDelay: videoInOrOutbound.currentDelay,
};
2018-06-15 03:20:14 +08:00
const videoStatsArray = statsState;
videoStatsArray.push(videoStats);
2018-06-15 03:20:14 +08:00
while (videoStatsArray.length > 5) { // maximum interval to consider
videoStatsArray.shift();
}
2018-05-26 03:24:41 +08:00
this.setState({ stats: videoStatsArray });
2018-05-03 03:03:36 +08:00
const firstVideoStats = videoStatsArray[0];
const lastVideoStats = videoStatsArray[videoStatsArray.length - 1];
2018-05-03 03:03:36 +08:00
const videoIntervalPacketsLost = lastVideoStats.packetsLost - firstVideoStats.packetsLost;
const videoIntervalPacketsReceived = lastVideoStats.packetsReceived - firstVideoStats.packetsReceived;
const videoIntervalPacketsSent = lastVideoStats.packetsSent - firstVideoStats.packetsSent;
const videoIntervalBytesReceived = lastVideoStats.bytesReceived - firstVideoStats.bytesReceived;
const videoIntervalBytesSent = lastVideoStats.bytesSent - firstVideoStats.bytesSent;
2018-05-03 03:03:36 +08:00
const videoReceivedInterval = lastVideoStats.timestamp - firstVideoStats.timestamp;
const videoSentInterval = lastVideoStats.timestamp - firstVideoStats.timestamp;
2018-06-15 03:20:14 +08:00
const videoKbitsReceivedPerSecond = (videoIntervalBytesReceived * 8) / videoReceivedInterval;
const videoKbitsSentPerSecond = (videoIntervalBytesSent * 8) / videoSentInterval;
2018-12-18 06:19:26 +08:00
let videoLostPercentage;
let videoLostRecentPercentage;
let videoBitrate;
if (videoStats.packetsReceived > 0) { // Remote video
2018-06-15 03:20:14 +08:00
videoLostPercentage = ((videoStats.packetsLost / ((videoStats.packetsLost + videoStats.packetsReceived) * 100)) || 0).toFixed(1);
2018-05-03 03:03:36 +08:00
videoBitrate = Math.floor(videoKbitsReceivedPerSecond || 0);
2018-06-15 03:20:14 +08:00
videoLostRecentPercentage = ((videoIntervalPacketsLost / ((videoIntervalPacketsLost + videoIntervalPacketsReceived) * 100)) || 0).toFixed(1);
} else {
2018-06-15 03:20:14 +08:00
videoLostPercentage = (((videoStats.packetsLost / (videoStats.packetsLost + videoStats.packetsSent)) * 100) || 0).toFixed(1);
2018-05-03 03:03:36 +08:00
videoBitrate = Math.floor(videoKbitsSentPerSecond || 0);
2018-06-15 03:20:14 +08:00
videoLostRecentPercentage = ((videoIntervalPacketsLost / ((videoIntervalPacketsLost + videoIntervalPacketsSent) * 100)) || 0).toFixed(1);
}
2018-05-03 03:03:36 +08:00
const result = {
video: {
bytesReceived: videoStats.bytesReceived,
bytesSent: videoStats.bytesSent,
packetsLost: videoStats.packetsLost,
packetsReceived: videoStats.packetsReceived,
packetsSent: videoStats.packetsSent,
bitrate: videoBitrate,
lostPercentage: videoLostPercentage,
lostRecentPercentage: videoLostRecentPercentage,
height: videoStats.height,
width: videoStats.width,
codec: videoStats.codec,
decodeDelay: videoStats.decodeDelay,
encodeUsagePercent: videoStats.encodeUsagePercent,
rtt: videoStats.rtt,
currentDelay: videoStats.currentDelay,
2018-06-15 03:20:14 +08:00
},
};
callback(result);
2018-06-15 03:20:14 +08:00
}, (exception) => {
this.logger('error', `customGetStats() Promise rejected: ${exception.message}`);
callback(null);
});
}
2018-06-15 03:20:14 +08:00
monitorTrackStart(peer, track, local, callback) {
const that = this;
this.logger('info', 'Starting stats monitoring on', { cameraId: track.id });
const getStatsInterval = 2000;
const callGetStats = () => {
that.customGetStats(
peer,
track,
2018-06-15 03:20:14 +08:00
(results) => {
if (results == null || peer.signalingState === 'closed') {
that.monitorTrackStop(track.id);
} else {
callback(results);
}
},
2018-06-15 03:20:14 +08:00
getStatsInterval,
);
};
if (!this.monitoredTracks[track.id]) {
callGetStats();
this.monitoredTracks[track.id] = setInterval(
callGetStats,
getStatsInterval,
);
} else {
this.logger('info', 'Already monitoring this track');
}
}
2018-06-15 03:20:14 +08:00
monitorTrackStop(trackId) {
if (this.monitoredTracks[trackId]) {
clearInterval(this.monitoredTracks[trackId]);
delete this.monitoredTracks[trackId];
this.logger('info', `Track ${trackId} removed`);
} else {
this.logger('info', `Track ${trackId} is not monitored`);
}
}
2018-04-27 06:16:30 +08:00
stopGettingStats(id) {
const peer = this.webRtcPeers[id];
2018-12-18 06:19:26 +08:00
const hasLocalStream = peer && peer.started === true
&& peer.peerConnection.getLocalStreams().length > 0;
const hasRemoteStream = peer && peer.started === true
&& peer.peerConnection.getRemoteStreams().length > 0;
2018-05-26 03:24:41 +08:00
if (hasLocalStream) {
this.monitorTrackStop(peer.peerConnection.getLocalStreams()[0].getVideoTracks()[0].id);
} else if (hasRemoteStream) {
this.monitorTrackStop(peer.peerConnection.getRemoteStreams()[0].getVideoTracks()[0].id);
2018-04-27 06:16:30 +08:00
}
}
2018-02-17 03:18:53 +08:00
handlePlayStop(message) {
const { cameraId } = message;
this.logger('info', 'Handle play stop for camera', { cameraId });
this.stopWebRTCPeer(cameraId);
2018-02-17 03:18:53 +08:00
}
handlePlayStart(message) {
const id = message.cameraId;
const peer = this.webRtcPeers[id];
if (peer) {
2018-12-18 06:19:26 +08:00
const { userId } = this.props;
this.logger('info', 'Handle play start for camera', { cameraId: id });
// Clear camera shared timeout when camera succesfully starts
clearTimeout(this.restartTimeout[id]);
delete this.restartTimeout[id];
delete this.restartTimer[id];
2018-02-17 03:18:53 +08:00
peer.started = true;
if (!peer.attached) {
this.attachVideoStream(id);
}
2018-12-18 06:19:26 +08:00
if (id === userId) {
VideoService.sendUserShareWebcam(id);
VideoService.joinedVideo();
}
} else {
this.logger('warn', '[playStart] Message arrived after the peer was already thrown out, discarding it...');
2018-02-17 03:18:53 +08:00
}
}
handleSFUError(message) {
2018-02-17 03:18:53 +08:00
const { intl } = this.props;
const { userId } = this.props;
const { code, reason } = message;
this.logger('debug', 'Received error from SFU:', code, reason, message.streamId, userId);
if (message.streamId === userId) {
2018-02-17 03:18:53 +08:00
this.unshareWebcam();
this.notifyError(intl.formatMessage(intlSFUErrors[code]
|| intlSFUErrors[2200]));
2018-02-17 03:18:53 +08:00
} else {
this.stopWebRTCPeer(message.cameraId);
2018-02-17 03:18:53 +08:00
}
this.logger('error', `Handle error ---------------------> ${message.message}`);
2018-02-17 03:18:53 +08:00
}
notifyError(message) {
notify(message, 'error', 'video');
}
shareWebcam() {
if (this.connectedToMediaServer()) {
this.logger('info', 'Sharing webcam');
this.sharedWebcam = true;
VideoService.joiningVideo();
}
2018-02-17 03:18:53 +08:00
}
unshareWebcam() {
2018-12-18 06:19:26 +08:00
const { userId } = this.props;
this.logger('info', 'Unsharing webcam');
2018-02-17 05:11:59 +08:00
2018-12-18 06:19:26 +08:00
VideoService.sendUserUnshareWebcam(userId);
VideoService.exitedVideo();
this.sharedWebcam = false;
2018-02-17 03:18:53 +08:00
}
render() {
2018-12-18 06:19:26 +08:00
const { socketOpen } = this.state;
if (!socketOpen) return null;
2018-12-18 06:19:26 +08:00
const { users, enableVideoStats } = this.props;
2018-02-17 03:18:53 +08:00
return (
<VideoList
2018-12-18 06:19:26 +08:00
users={users}
onMount={this.createVideoTag}
getStats={this.getStats}
stopGettingStats={this.stopGettingStats}
enableVideoStats={enableVideoStats}
/>
2018-02-17 03:18:53 +08:00
);
}
}
2018-03-12 23:29:51 +08:00
export default injectIntl(VideoProvider);