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

900 lines
27 KiB
React
Raw Normal View History

2018-02-17 03:18:53 +08:00
import React, { Component } from 'react';
2019-07-24 06:24:31 +08:00
import PropTypes from 'prop-types';
import ReconnectingWebSocket from 'reconnecting-websocket';
import VideoService from './service';
import VideoListContainer from './video-list/container';
2018-02-17 03:18:53 +08:00
import { defineMessages, injectIntl } from 'react-intl';
import {
fetchWebRTCMappedStunTurnServers,
getMappedFallbackStun,
} from '/imports/utils/fetchStunTurnServers';
import { tryGenerateIceCandidates } from '/imports/utils/safari-webrtc';
2019-11-28 21:13:06 +08:00
import logger from '/imports/startup/client/logger';
import _ from 'lodash';
2018-02-17 03:18:53 +08:00
// Default values and default empty object to be backwards compat with 2.2.
// FIXME Remove hardcoded defaults 2.3.
const WS_CONN_TIMEOUT = Meteor.settings.public.kurento.wsConnectionTimeout || 4000;
const {
baseTimeout: CAMERA_SHARE_FAILED_WAIT_TIME = 15000,
maxTimeout: MAX_CAMERA_SHARE_FAILED_WAIT_TIME = 60000,
} = Meteor.settings.public.kurento.cameraTimeouts || {};
const CAMERA_QUALITY_THRESHOLDS_ENABLED = Meteor.settings.public.kurento.cameraQualityThresholds.enabled;
2019-11-28 21:13:06 +08:00
const PING_INTERVAL = 15000;
const intlClientErrors = defineMessages({
permissionError: {
id: 'app.video.permissionError',
2019-11-28 21:13:06 +08:00
description: 'Webcam permission error',
},
iceConnectionStateError: {
id: 'app.video.iceConnectionStateError',
2019-11-28 21:13:06 +08:00
description: 'Ice connection state failed',
},
mediaFlowTimeout: {
id: 'app.video.mediaFlowTimeout1020',
2019-11-28 21:13:06 +08:00
description: 'Media flow timeout',
},
});
const intlSFUErrors = defineMessages({
2000: {
id: 'app.sfu.mediaServerConnectionError2000',
2019-11-28 21:13:06 +08:00
description: 'SFU connection to the media server',
},
2001: {
id: 'app.sfu.mediaServerOffline2001',
2019-11-28 21:13:06 +08:00
description: 'SFU is offline',
},
2002: {
id: 'app.sfu.mediaServerNoResources2002',
2019-11-28 21:13:06 +08:00
description: 'Media server lacks disk, CPU or FDs',
},
2003: {
id: 'app.sfu.mediaServerRequestTimeout2003',
2019-11-28 21:13:06 +08:00
description: 'Media requests timeout due to lack of resources',
},
2021: {
id: 'app.sfu.serverIceGatheringFailed2021',
2019-11-28 21:13:06 +08:00
description: 'Server cannot enact ICE gathering',
},
2022: {
id: 'app.sfu.serverIceStateFailed2022',
2019-11-28 21:13:06 +08:00
description: 'Server endpoint transitioned to a FAILED ICE state',
},
2200: {
id: 'app.sfu.mediaGenericError2200',
2019-11-28 21:13:06 +08:00
description: 'SFU component generated a generic error',
},
2202: {
id: 'app.sfu.invalidSdp2202',
2019-11-28 21:13:06 +08:00
description: 'Client provided an invalid SDP',
},
2203: {
id: 'app.sfu.noAvailableCodec2203',
2019-11-28 21:13:06 +08:00
description: 'Server has no available codec for the client',
},
2018-02-17 03:18:53 +08:00
});
2019-07-24 06:24:31 +08:00
const propTypes = {
2019-11-28 21:13:06 +08:00
streams: PropTypes.arrayOf(Array).isRequired,
2019-07-24 06:24:31 +08:00
intl: PropTypes.objectOf(Object).isRequired,
2019-11-28 21:13:06 +08:00
isUserLocked: PropTypes.bool.isRequired,
swapLayout: PropTypes.bool.isRequired,
currentVideoPageIndex: PropTypes.number.isRequired,
totalNumberOfStreams: PropTypes.number.isRequired,
2019-07-24 06:24:31 +08:00
};
2018-02-17 03:18:53 +08:00
class VideoProvider extends Component {
constructor(props) {
super(props);
this.state = {
socketOpen: false,
2018-02-17 03:18:53 +08:00
};
2019-11-28 21:13:06 +08:00
this.info = VideoService.getInfo();
2018-02-17 03:18:53 +08:00
// Set a valid bbb-webrtc-sfu application server socket in the settings
this.ws = new ReconnectingWebSocket(
VideoService.getAuthenticatedURL(),
[],
{ connectionTimeout: WS_CONN_TIMEOUT },
);
2018-02-17 03:18:53 +08:00
this.wsQueue = [];
this.restartTimeout = {};
this.restartTimer = {};
2018-02-17 03:18:53 +08:00
this.webRtcPeers = {};
this.outboundIceQueues = {};
this.videoTags = {};
2018-02-17 03:18:53 +08:00
2018-12-18 06:19:26 +08:00
this.createVideoTag = this.createVideoTag.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);
2019-11-28 21:13:06 +08:00
this.onBeforeUnload = this.onBeforeUnload.bind(this);
2019-11-28 21:13:06 +08:00
this.updateStreams = this.updateStreams.bind(this);
this.debouncedConnectStreams = _.debounce(
this.connectStreams,
VideoService.getPageChangeDebounceTime(),
{ leading: false, trailing: true, }
);
}
2019-11-28 21:13:06 +08:00
componentDidMount() {
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);
this.ws.onmessage = this.onWsMessage;
2019-11-28 21:13:06 +08:00
window.addEventListener('beforeunload', this.onBeforeUnload);
}
componentDidUpdate(prevProps) {
const { isUserLocked, streams, currentVideoPageIndex } = this.props;
// Only debounce when page changes to avoid unecessary debouncing
const shouldDebounce = VideoService.isPaginationEnabled()
&& prevProps.currentVideoPageIndex !== currentVideoPageIndex;
this.updateStreams(streams, shouldDebounce);
2019-11-28 21:13:06 +08:00
if (!prevProps.isUserLocked && isUserLocked) VideoService.lockUser();
}
2018-02-17 03:18:53 +08:00
componentWillUnmount() {
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);
2019-11-28 21:13:06 +08:00
window.removeEventListener('beforeunload', this.onBeforeUnload);
2019-11-28 21:13:06 +08:00
VideoService.exitVideo();
2018-02-17 03:18:53 +08:00
2019-11-28 21:13:06 +08:00
Object.keys(this.webRtcPeers).forEach((cameraId) => {
this.stopWebRTCPeer(cameraId);
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
}
2019-11-28 21:13:06 +08:00
onWsMessage(message) {
const parsedMessage = JSON.parse(message.data);
2018-02-17 03:18:53 +08:00
2019-01-22 04:05:52 +08:00
if (parsedMessage.id === 'pong') return;
2019-11-28 21:13:06 +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() {
2019-11-28 21:13:06 +08:00
logger.debug({
logCode: 'video_provider_onwsclose',
}, 'Multiple video provider websocket connection closed.');
2018-02-17 03:18:53 +08:00
clearInterval(this.pingInterval);
2019-11-28 21:13:06 +08:00
VideoService.exitVideo();
this.setState({ socketOpen: false });
2018-02-17 03:18:53 +08:00
}
2018-12-18 06:19:26 +08:00
onWsOpen() {
2019-11-28 21:13:06 +08:00
logger.debug({
logCode: 'video_provider_onwsopen',
}, 'Multiple video provider websocket connection opened.');
2018-12-18 06:19:26 +08:00
2019-11-28 21:13:06 +08:00
// Resend queued messages that happened when socket was not connected
2018-12-18 06:19:26 +08:00
while (this.wsQueue.length > 0) {
this.sendMessage(this.wsQueue.pop());
}
this.pingInterval = setInterval(this.ping.bind(this), PING_INTERVAL);
this.setState({ socketOpen: true });
}
2019-11-28 21:13:06 +08:00
onBeforeUnload() {
VideoService.onBeforeUnload();
2018-12-18 06:19:26 +08:00
}
2018-02-17 03:18:53 +08:00
updateThreshold (numberOfPublishers) {
const { threshold, profile } = VideoService.getThreshold(numberOfPublishers);
if (profile) {
const publishers = Object.values(this.webRtcPeers)
.filter(peer => peer.isPublisher)
.forEach(peer => {
// 0 means no threshold in place. Reapply original one if needed
let profileToApply = (threshold === 0) ? peer.originalProfileId : profile;
VideoService.applyCameraProfile(peer, profileToApply)
});
}
}
getStreamsToConnectAndDisconnect(streams) {
2019-11-28 21:13:06 +08:00
const streamsCameraIds = streams.map(s => s.cameraId);
const streamsConnected = Object.keys(this.webRtcPeers);
const streamsToConnect = streamsCameraIds.filter(cameraId => {
return !streamsConnected.includes(cameraId);
2018-12-18 06:19:26 +08:00
});
const streamsToDisconnect = streamsConnected.filter(cameraId => {
return !streamsCameraIds.includes(cameraId);
});
2018-12-18 06:19:26 +08:00
return [streamsToConnect, streamsToDisconnect];
}
connectStreams(streamsToConnect) {
2019-11-28 21:13:06 +08:00
streamsToConnect.forEach((cameraId) => {
const isLocal = VideoService.isLocalStream(cameraId);
this.createWebRTCPeer(cameraId, isLocal);
2018-12-18 06:19:26 +08:00
});
}
disconnectStreams(streamsToDisconnect) {
2019-11-28 21:13:06 +08:00
streamsToDisconnect.forEach(cameraId => this.stopWebRTCPeer(cameraId));
}
updateStreams(streams, shouldDebounce = false) {
const [streamsToConnect, streamsToDisconnect] = this.getStreamsToConnectAndDisconnect(streams);
if(shouldDebounce) {
this.debouncedConnectStreams(streamsToConnect);
} else {
this.connectStreams(streamsToConnect);
}
this.disconnectStreams(streamsToDisconnect);
if (CAMERA_QUALITY_THRESHOLDS_ENABLED) {
this.updateThreshold(this.props.totalNumberOfStreams);
}
2018-12-18 06:19:26 +08:00
}
ping() {
2019-11-28 21:13:06 +08:00
const message = { id: 'ping' };
2018-12-18 06:19:26 +08:00
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);
ws.send(jsonMessage, (error) => {
if (error) {
2019-07-03 03:51:35 +08:00
logger.error({
2019-07-19 04:44:21 +08:00
logCode: 'video_provider_ws_send_error',
2019-07-03 03:51:35 +08:00
extraInfo: {
2019-07-19 04:44:21 +08:00
sfuRequest: message,
2019-11-28 21:13:06 +08:00
error,
2019-07-03 03:51:35 +08:00
},
2019-07-19 04:44:21 +08:00
}, `WebSocket failed when sending request to SFU due to ${error.message}`);
2018-02-17 03:18:53 +08:00
}
});
} else if (message.id !== 'stop') {
2018-02-17 03:18:53 +08:00
// No need to queue video stop messages
this.wsQueue.push(message);
2018-02-17 03:18:53 +08:00
}
}
connectedToMediaServer() {
return this.ws.readyState === WebSocket.OPEN;
}
processOutboundIceQueue(peer, role, cameraId) {
const queue = this.outboundIceQueues[cameraId];
while (queue && queue.length) {
const candidate = queue.shift();
this.sendIceCandidateToSFU(peer, role, candidate, cameraId);
}
}
2018-02-17 03:18:53 +08:00
startResponse(message) {
2019-12-10 21:02:18 +08:00
const { cameraId, role } = message;
2019-11-28 21:13:06 +08:00
const peer = this.webRtcPeers[cameraId];
2018-02-17 03:18:53 +08:00
2019-07-19 04:44:21 +08:00
logger.info({
logCode: 'video_provider_start_response_success',
2019-07-03 03:51:35 +08:00
extraInfo: {
2019-11-28 21:13:06 +08:00
cameraId,
2019-07-19 04:44:21 +08:00
sfuResponse: message,
2019-07-03 03:51:35 +08:00
},
2019-11-28 21:13:06 +08:00
}, `Camera start request was accepted by SFU, processing response for ${cameraId}`);
2018-02-17 03:18:53 +08:00
if (peer) {
peer.processAnswer(message.sdpAnswer, (error) => {
if (error) {
2019-07-19 04:44:21 +08:00
logger.error({
logCode: 'video_provider_peerconnection_processanswer_error',
2019-07-03 03:51:35 +08:00
extraInfo: {
2019-11-28 21:13:06 +08:00
cameraId,
2019-07-03 03:51:35 +08:00
error,
},
2019-11-28 21:13:06 +08:00
}, `Processing SDP answer from SFU for ${cameraId} failed due to ${error.message}`);
2019-07-19 04:44:21 +08:00
return;
}
peer.didSDPAnswered = true;
2019-11-28 21:13:06 +08:00
this.processOutboundIceQueue(peer, role, cameraId);
VideoService.processInboundIceQueue(peer, cameraId);
});
} else {
2019-11-28 21:13:06 +08:00
logger.warn({
logCode: 'video_provider_startresponse_no_peer',
}, `SFU start response for ${cameraId} arrived after the peer was discarded, ignore it.`);
}
2018-02-17 03:18:53 +08:00
}
handleIceCandidate(message) {
2019-07-19 04:44:21 +08:00
const { cameraId, candidate } = message;
const peer = this.webRtcPeers[cameraId];
2018-02-17 03:18:53 +08:00
2019-07-03 03:51:35 +08:00
logger.debug({
logCode: 'video_provider_ice_candidate_received',
extraInfo: {
2019-07-19 04:44:21 +08:00
candidate,
2019-07-03 03:51:35 +08:00
},
2019-07-19 04:44:21 +08:00
}, `video-provider received candidate for ${cameraId}: ${JSON.stringify(candidate)}`);
2019-07-19 04:44:21 +08:00
if (peer) {
if (peer.didSDPAnswered) {
2019-11-28 21:13:06 +08:00
VideoService.addCandidateToPeer(peer, candidate, cameraId);
2018-02-17 03:18:53 +08:00
} else {
2019-07-19 04:44:21 +08:00
// ICE candidates are queued until a SDP answer has been processed.
// This was done due to a long term iOS/Safari quirk where it'd
// fail if candidates were added before the offer/answer cycle was completed.
// Dunno if that still happens, but it works even if it slows the ICE checks
// a bit - prlanzarin july 2019
if (peer.inboundIceQueue == null) {
peer.inboundIceQueue = [];
}
peer.inboundIceQueue.push(candidate);
2018-02-17 03:18:53 +08:00
}
} else {
2019-11-28 21:13:06 +08:00
logger.warn({
logCode: 'video_provider_addicecandidate_no_peer',
}, `SFU ICE candidate for ${cameraId} arrived after the peer was discarded, ignore it.`);
2018-02-17 03:18:53 +08:00
}
}
2019-11-28 21:13:06 +08:00
stopWebRTCPeer(cameraId, restarting = false) {
const isLocal = VideoService.isLocalStream(cameraId);
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
2019-11-28 21:13:06 +08:00
const peer = this.webRtcPeers[cameraId];
if (peer && peer.peerConnection) {
2019-11-28 21:13:06 +08:00
const conn = peer.peerConnection;
conn.oniceconnectionstatechange = null;
}
2019-11-28 21:13:06 +08:00
if (isLocal) {
VideoService.stopVideo(cameraId);
}
2019-11-28 21:13:06 +08:00
const role = VideoService.getRole(isLocal);
2019-07-19 04:44:21 +08:00
2019-11-28 21:13:06 +08:00
logger.info({
logCode: 'video_provider_stopping_webcam_sfu',
}, `Sending stop request to SFU. Camera: ${cameraId}, role ${role} and flag restarting ${restarting}`);
this.sendMessage({
2019-11-28 21:13:06 +08:00
id: 'stop',
type: 'video',
2019-11-28 21:13:06 +08:00
cameraId,
2019-07-19 04:44:21 +08:00
role,
});
// Clear the shared camera media flow timeout and current reconnect period
// when destroying it if the peer won't restart
2018-12-18 22:54:17 +08:00
if (!restarting) {
2019-11-28 21:13:06 +08:00
if (this.restartTimeout[cameraId]) {
clearTimeout(this.restartTimeout[cameraId]);
delete this.restartTimeout[cameraId];
2018-12-18 22:54:17 +08:00
}
2019-11-28 21:13:06 +08:00
if (this.restartTimer[cameraId]) {
delete this.restartTimer[cameraId];
2018-12-18 22:54:17 +08:00
}
}
2019-11-28 21:13:06 +08:00
this.destroyWebRTCPeer(cameraId);
}
2018-02-17 03:18:53 +08:00
2019-11-28 21:13:06 +08:00
destroyWebRTCPeer(cameraId) {
const peer = this.webRtcPeers[cameraId];
if (peer) {
logger.info({
logCode: 'video_provider_destroywebrtcpeer',
}, `Disposing WebRTC peer ${cameraId}`);
if (typeof peer.dispose === 'function') {
peer.dispose();
}
2019-11-28 21:13:06 +08:00
delete this.outboundIceQueues[cameraId];
delete this.webRtcPeers[cameraId];
2018-02-17 03:18:53 +08:00
} else {
2019-11-28 21:13:06 +08:00
logger.warn({
logCode: 'video_provider_destroywebrtcpeer_no_peer',
}, `Peer ${cameraId} was already disposed (glare), ignore it.`);
2019-07-19 04:44:21 +08:00
}
}
2019-11-28 21:13:06 +08:00
async createWebRTCPeer(cameraId, isLocal) {
let iceServers = [];
2018-02-17 03:18:53 +08:00
// Check if the peer is already being processed
2019-11-28 21:13:06 +08:00
if (this.webRtcPeers[cameraId]) {
return;
}
2019-11-28 21:13:06 +08:00
this.webRtcPeers[cameraId] = {};
// WebRTC restrictions may need a capture device permission to release
// useful ICE candidates on recvonly/no-gUM peers
2019-11-28 21:13:06 +08:00
if (!isLocal) {
try {
await tryGenerateIceCandidates();
} catch (error) {
logger.error({
logCode: 'video_provider_no_valid_candidate_gum_failure',
extraInfo: {
errorName: error.name,
errorMessage: error.message,
},
}, `Forced gUM to release additional ICE candidates failed due to ${error.name}.`);
}
}
try {
2019-11-28 21:13:06 +08:00
iceServers = await fetchWebRTCMappedStunTurnServers(this.info.sessionToken);
} catch (error) {
2019-07-03 03:51:35 +08:00
logger.error({
2019-07-19 04:44:21 +08:00
logCode: 'video_provider_fetchstunturninfo_error',
2019-07-03 03:51:35 +08:00
extraInfo: {
errorCode: error.code,
errorMessage: error.message,
2019-07-24 06:24:31 +08:00
},
2019-07-19 04:44:21 +08:00
}, 'video-provider failed to fetch STUN/TURN info, using default');
// Use fallback STUN server
iceServers = getMappedFallbackStun();
} finally {
2019-11-28 21:13:06 +08:00
const { constraints, bitrate, id: profileId } = VideoService.getCameraProfile();
this.outboundIceQueues[cameraId] = [];
2019-07-19 04:44:21 +08:00
const peerOptions = {
mediaConstraints: {
audio: false,
2019-04-09 06:07:26 +08:00
video: constraints,
},
2019-11-28 21:13:06 +08:00
onicecandidate: this._getOnIceCandidateCallback(cameraId, isLocal),
};
2018-02-17 05:11:59 +08:00
if (iceServers.length > 0) {
2019-07-19 04:44:21 +08:00
peerOptions.configuration = {};
peerOptions.configuration.iceServers = iceServers;
}
let WebRtcPeerObj;
2019-11-28 21:13:06 +08:00
if (isLocal) {
WebRtcPeerObj = window.kurentoUtils.WebRtcPeer.WebRtcPeerSendonly;
} else {
WebRtcPeerObj = window.kurentoUtils.WebRtcPeer.WebRtcPeerRecvonly;
}
2019-11-28 21:13:06 +08:00
this.webRtcPeers[cameraId] = new WebRtcPeerObj(peerOptions, (error) => {
const peer = this.webRtcPeers[cameraId];
peer.started = false;
peer.attached = false;
peer.didSDPAnswered = false;
peer.isPublisher = isLocal;
peer.originalProfileId = profileId;
peer.currentProfileId = profileId;
if (peer.inboundIceQueue == null) {
peer.inboundIceQueue = [];
}
2018-02-17 03:18:53 +08:00
if (error) {
2019-11-28 21:13:06 +08:00
return this._onWebRTCError(error, cameraId, isLocal);
2018-02-17 03:18:53 +08:00
}
peer.generateOffer((errorGenOffer, offerSdp) => {
if (errorGenOffer) {
2019-11-28 21:13:06 +08:00
return this._onWebRTCError(errorGenOffer, cameraId, isLocal);
}
const message = {
id: 'start',
2019-11-28 21:13:06 +08:00
type: 'video',
cameraId,
role: VideoService.getRole(isLocal),
sdpOffer: offerSdp,
2019-11-28 21:13:06 +08:00
meetingId: this.info.meetingId,
voiceBridge: this.info.voiceBridge,
userId: this.info.userId,
userName: this.info.userName,
bitrate,
record: VideoService.getRecord(),
};
2019-07-19 04:44:21 +08:00
logger.info({
2019-07-24 06:24:31 +08:00
logCode: 'video_provider_sfu_request_start_camera',
2019-07-19 04:44:21 +08:00
extraInfo: {
sfuRequest: message,
cameraProfile: profileId,
},
2019-11-28 21:13:06 +08:00
}, `Camera offer generated. Sending start request to SFU for ${cameraId}`);
2019-07-19 04:44:21 +08:00
this.sendMessage(message);
2019-07-24 06:24:31 +08:00
return false;
});
2019-07-24 06:24:31 +08:00
return false;
2018-02-17 03:18:53 +08:00
});
2019-11-28 21:13:06 +08:00
const peer = this.webRtcPeers[cameraId];
if (peer && peer.peerConnection) {
const conn = peer.peerConnection;
conn.oniceconnectionstatechange = this._getOnIceConnectionStateChangeCallback(cameraId, isLocal);
VideoService.monitor(conn);
}
}
2018-02-17 03:18:53 +08:00
}
2019-11-28 21:13:06 +08:00
_getWebRTCStartTimeout(cameraId, isLocal) {
const { intl } = this.props;
return () => {
2019-07-19 04:44:21 +08:00
// Peer that timed out is a sharer/publisher
2019-11-28 21:13:06 +08:00
if (isLocal) {
2019-07-19 04:44:21 +08:00
logger.error({
logCode: 'video_provider_camera_share_timeout',
2019-11-28 21:13:06 +08:00
extraInfo: { cameraId },
}, `Camera SHARER has not succeeded in ${CAMERA_SHARE_FAILED_WAIT_TIME} for ${cameraId}`);
VideoService.notify(intl.formatMessage(intlClientErrors.mediaFlowTimeout));
this.stopWebRTCPeer(cameraId);
} else {
2019-07-19 04:44:21 +08:00
// Create new reconnect interval time
2019-11-28 21:13:06 +08:00
const oldReconnectTimer = this.restartTimer[cameraId];
2019-07-19 04:44:21 +08:00
const newReconnectTimer = Math.min(
2 * oldReconnectTimer,
2019-07-24 06:24:31 +08:00
MAX_CAMERA_SHARE_FAILED_WAIT_TIME,
2019-07-19 04:44:21 +08:00
);
2019-11-28 21:13:06 +08:00
this.restartTimer[cameraId] = newReconnectTimer;
2019-07-19 04:44:21 +08:00
// Clear the current reconnect interval so it can be re-set in createWebRTCPeer
2019-11-28 21:13:06 +08:00
if (this.restartTimeout[cameraId]) {
delete this.restartTimeout[cameraId];
}
2019-07-19 04:44:21 +08:00
// Peer that timed out is a subscriber/viewer
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
2019-07-19 04:44:21 +08:00
logger.error({
logCode: 'video_provider_camera_view_timeout',
extraInfo: {
2019-11-28 21:13:06 +08:00
cameraId,
oldReconnectTimer,
newReconnectTimer,
2019-07-24 06:24:31 +08:00
},
2019-11-28 21:13:06 +08:00
}, `Camera VIEWER has not succeeded in ${oldReconnectTimer} for ${cameraId}. Reconnecting.`);
this.stopWebRTCPeer(cameraId, true);
this.createWebRTCPeer(cameraId, isLocal);
}
};
}
2019-11-28 21:13:06 +08:00
_onWebRTCError(error, cameraId, isLocal) {
const { intl } = this.props;
2019-07-19 04:44:21 +08:00
// 2001 means MEDIA_SERVER_OFFLINE. It's a server-wide error.
// We only display it to a sharer/publisher instance to avoid popping up
// redundant toasts.
// If the client only has viewer instances, the WS will close unexpectedly
// and an error will be shown there for them.
2019-11-28 21:13:06 +08:00
if (error === 2001 && !isLocal) {
return;
}
const errorMessage = intlClientErrors[error.name]
|| intlSFUErrors[error] || intlClientErrors.permissionError;
// Only display WebRTC negotiation error toasts to sharers. The viewer streams
// will try to autoreconnect silently, but the error will log nonetheless
2019-11-28 21:13:06 +08:00
if (isLocal) {
VideoService.notify(intl.formatMessage(errorMessage));
} else {
// If it's a viewer, set the reconnection timeout. There's a good chance
// no local candidate was generated and it wasn't set.
2019-11-28 21:13:06 +08:00
this.setReconnectionTimeout(cameraId, isLocal);
}
// shareWebcam as the second argument means it will only try to reconnect if
// it's a viewer instance (see stopWebRTCPeer restarting argument)
2019-11-28 21:13:06 +08:00
this.stopWebRTCPeer(cameraId, !isLocal);
2019-07-03 03:51:35 +08:00
logger.error({
2019-07-19 04:44:21 +08:00
logCode: 'video_provider_webrtc_peer_error',
2019-07-03 03:51:35 +08:00
extraInfo: {
2019-07-19 04:44:21 +08:00
cameraId,
2019-11-28 21:13:06 +08:00
normalizedError: errorMessage,
error,
2019-07-03 03:51:35 +08:00
},
2019-07-19 04:44:21 +08:00
}, `Camera peer creation failed for ${cameraId} due to ${error.message}`);
}
2019-11-28 21:13:06 +08:00
setReconnectionTimeout(cameraId, isLocal) {
const peer = this.webRtcPeers[cameraId];
const peerHasStarted = peer && peer.started === true;
2019-11-28 21:13:06 +08:00
const shouldSetReconnectionTimeout = !this.restartTimeout[cameraId] && !peerHasStarted;
if (shouldSetReconnectionTimeout) {
2019-11-28 21:13:06 +08:00
const newReconnectTimer = this.restartTimer[cameraId] || CAMERA_SHARE_FAILED_WAIT_TIME;
this.restartTimer[cameraId] = newReconnectTimer;
logger.info({
logCode: 'video_provider_setup_reconnect',
extraInfo: {
2019-11-28 21:13:06 +08:00
cameraId,
reconnectTimer: newReconnectTimer,
},
2019-11-28 21:13:06 +08:00
}, `Camera has a new reconnect timer of ${newReconnectTimer} ms for ${cameraId}`);
this.restartTimeout[cameraId] = setTimeout(
this._getWebRTCStartTimeout(cameraId, isLocal),
this.restartTimer[cameraId]
);
}
}
2019-11-28 21:13:06 +08:00
_getOnIceCandidateCallback(cameraId, isLocal) {
return (candidate) => {
const peer = this.webRtcPeers[cameraId];
const role = VideoService.getRole(isLocal);
// Setup a timeout only when the first candidate is generated and if the peer wasn't
// marked as started already (which is done on handlePlayStart after
// it was verified that media could circle through the server)
2019-11-28 21:13:06 +08:00
this.setReconnectionTimeout(cameraId, isLocal);
2018-02-17 03:18:53 +08:00
if (peer && !peer.didSDPAnswered) {
logger.debug({
logCode: 'video_provider_client_candidate',
extraInfo: { candidate },
2019-11-28 21:13:06 +08:00
}, `video-provider client-side candidate queued for ${cameraId}`);
this.outboundIceQueues[cameraId].push(candidate);
return;
}
2019-11-28 21:13:06 +08:00
this.sendIceCandidateToSFU(peer, role, candidate, cameraId);
};
}
sendIceCandidateToSFU(peer, role, candidate, cameraId) {
logger.debug({
logCode: 'video_provider_client_candidate',
extraInfo: { candidate },
}, `video-provider client-side candidate generated for ${cameraId}: ${JSON.stringify(candidate)}`);
const message = {
type: 'video',
role,
id: 'onIceCandidate',
candidate,
cameraId,
};
this.sendMessage(message);
}
2018-02-17 03:18:53 +08:00
2019-11-28 21:13:06 +08:00
_getOnIceConnectionStateChangeCallback(cameraId, isLocal) {
const { intl } = this.props;
2019-11-28 21:13:06 +08:00
const peer = this.webRtcPeers[cameraId];
if (peer && peer.peerConnection) {
const conn = peer.peerConnection;
const { iceConnectionState } = conn;
2019-11-28 21:13:06 +08:00
return () => {
if (iceConnectionState === 'failed' || iceConnectionState === 'closed') {
// prevent the same error from being detected multiple times
conn.oniceconnectionstatechange = null;
logger.error({
logCode: 'video_provider_ice_connection_failed_state',
extraInfo: {
cameraId,
iceConnectionState,
},
}, `ICE connection state transitioned to ${iceConnectionState} for ${cameraId}`);
this.stopWebRTCPeer(cameraId);
VideoService.notify(intl.formatMessage(intlClientErrors.iceConnectionStateError));
}
};
}
2018-12-18 06:19:26 +08:00
return () => {
2019-11-28 21:13:06 +08:00
logger.error({
logCode: 'video_provider_ice_connection_failed_state',
extraInfo: {
cameraId,
iceConnectionState: undefined,
},
}, `Missing peer at ICE connection state transition for ${cameraId}`);
2019-04-24 02:26:28 +08:00
2019-12-10 21:02:18 +08:00
// isLocal as the second argument means it will only try to reconnect if
// it's a viewer instance (see stopWebRTCPeer restarting argument)
this.stopWebRTCPeer(cameraId, !isLocal);
VideoService.notify(intl.formatMessage(intlClientErrors.iceConnectionStateError));
};
}
2019-11-28 21:13:06 +08:00
attachVideoStream(cameraId) {
const video = this.videoTags[cameraId];
if (video == null) {
2019-07-03 03:51:35 +08:00
logger.warn({
2019-07-19 04:44:21 +08:00
logCode: 'video_provider_delay_attach_video_stream',
2019-11-28 21:13:06 +08:00
extraInfo: { cameraId },
}, `Will attach stream later because camera has not started yet for ${cameraId}`);
return;
}
if (video.srcObject) {
2019-11-28 21:13:06 +08:00
delete this.videoTags[cameraId];
return; // Skip if the stream is already attached
}
2018-02-17 03:18:53 +08:00
2019-11-28 21:13:06 +08:00
const isLocal = VideoService.isLocalStream(cameraId);
const peer = this.webRtcPeers[cameraId];
const attachVideoStreamHelper = () => {
2019-11-28 21:13:06 +08:00
const stream = isLocal ? peer.getLocalStream() : peer.getRemoteStream();
video.pause();
video.srcObject = stream;
video.load();
peer.attached = true;
2019-11-28 21:13:06 +08:00
delete this.videoTags[cameraId];
};
// 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
}
2019-11-28 21:13:06 +08:00
createVideoTag(cameraId, video) {
const peer = this.webRtcPeers[cameraId];
this.videoTags[cameraId] = video;
if (peer) {
2019-11-28 21:13:06 +08:00
this.attachVideoStream(cameraId);
2018-04-27 06:16:30 +08:00
}
}
2018-02-17 03:18:53 +08:00
handlePlayStop(message) {
const { cameraId } = message;
2019-07-03 03:51:35 +08:00
logger.info({
logCode: 'video_provider_handle_play_stop',
extraInfo: {
cameraId,
2019-07-19 04:44:21 +08:00
sfuRequest: message,
2019-07-03 03:51:35 +08:00
},
2019-07-19 04:44:21 +08:00
}, `Received request from SFU to stop camera ${cameraId}`);
this.stopWebRTCPeer(cameraId, false);
2018-02-17 03:18:53 +08:00
}
handlePlayStart(message) {
2019-07-19 04:44:21 +08:00
const { cameraId } = message;
const peer = this.webRtcPeers[cameraId];
if (peer) {
2019-07-03 03:51:35 +08:00
logger.info({
2019-07-19 04:44:21 +08:00
logCode: 'video_provider_handle_play_start_flowing',
2019-07-03 03:51:35 +08:00
extraInfo: {
2019-07-19 04:44:21 +08:00
cameraId,
sfuResponse: message,
2019-07-03 03:51:35 +08:00
},
2019-07-19 04:44:21 +08:00
}, `SFU says that media is flowing for camera ${cameraId}`);
peer.started = true;
// Clear camera shared timeout when camera succesfully starts
2019-07-19 04:44:21 +08:00
clearTimeout(this.restartTimeout[cameraId]);
delete this.restartTimeout[cameraId];
delete this.restartTimer[cameraId];
2018-02-17 03:18:53 +08:00
if (!peer.attached) {
2019-07-19 04:44:21 +08:00
this.attachVideoStream(cameraId);
}
2019-11-28 21:13:06 +08:00
VideoService.playStart(cameraId);
} else {
2019-07-19 04:44:21 +08:00
logger.warn({ logCode: 'video_provider_playstart_no_peer' },
`SFU playStart response for ${cameraId} arrived after the peer was discarded, ignore it.`);
2018-02-17 03:18:53 +08:00
}
}
handleSFUError(message) {
2018-02-17 03:18:53 +08:00
const { intl } = this.props;
2019-11-28 21:13:06 +08:00
const { code, reason, streamId } = message;
const cameraId = streamId;
2019-07-03 03:51:35 +08:00
logger.error({
2019-07-19 04:44:21 +08:00
logCode: 'video_provider_handle_sfu_error',
2019-07-03 03:51:35 +08:00
extraInfo: {
2019-07-19 04:44:21 +08:00
error: message,
2019-11-28 21:13:06 +08:00
cameraId,
2019-07-03 03:51:35 +08:00
},
2019-11-28 21:13:06 +08:00
}, `SFU returned error for camera ${cameraId}. Code: ${code}, reason: ${reason}`);
2019-07-03 03:51:35 +08:00
2019-11-28 21:13:06 +08:00
const isLocal = VideoService.isLocalStream(cameraId);
if (isLocal) {
// The publisher instance received an error from the server. There's no reconnect,
// stop it.
VideoService.stopVideo(cameraId);
2019-11-28 21:13:06 +08:00
VideoService.notify(intl.formatMessage(intlSFUErrors[code] || intlSFUErrors[2200]));
2018-02-17 03:18:53 +08:00
} else {
2019-11-28 21:13:06 +08:00
this.stopWebRTCPeer(cameraId, true);
2018-02-17 03:18:53 +08:00
}
}
render() {
const { swapLayout, currentVideoPageIndex, streams } = this.props;
2018-02-17 03:18:53 +08:00
return (
<VideoListContainer
2019-11-28 21:13:06 +08:00
streams={streams}
2018-12-18 06:19:26 +08:00
onMount={this.createVideoTag}
swapLayout={swapLayout}
currentVideoPageIndex={currentVideoPageIndex}
/>
2018-02-17 03:18:53 +08:00
);
}
}
2019-07-24 06:24:31 +08:00
VideoProvider.propTypes = propTypes;
2018-03-12 23:29:51 +08:00
export default injectIntl(VideoProvider);