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';
|
2018-07-28 04:42:45 +08:00
|
|
|
import ReconnectingWebSocket from 'reconnecting-websocket';
|
2018-02-19 12:23:05 +08:00
|
|
|
import VideoService from './service';
|
2020-08-19 01:00:47 +08:00
|
|
|
import VideoListContainer from './video-list/container';
|
2018-02-17 03:18:53 +08:00
|
|
|
import { defineMessages, injectIntl } from 'react-intl';
|
2020-05-21 12:20:46 +08:00
|
|
|
import {
|
|
|
|
fetchWebRTCMappedStunTurnServers,
|
|
|
|
getMappedFallbackStun,
|
|
|
|
} from '/imports/utils/fetchStunTurnServers';
|
2019-11-15 03:07:35 +08:00
|
|
|
import { tryGenerateIceCandidates } from '/imports/utils/safari-webrtc';
|
2019-11-28 21:13:06 +08:00
|
|
|
import logger from '/imports/startup/client/logger';
|
2020-08-20 23:43:02 +08:00
|
|
|
import _ from 'lodash';
|
2018-02-17 03:18:53 +08:00
|
|
|
|
2020-07-09 23:22:14 +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;
|
2020-05-27 01:05:04 +08:00
|
|
|
|
2020-07-09 23:22:14 +08:00
|
|
|
const {
|
|
|
|
baseTimeout: CAMERA_SHARE_FAILED_WAIT_TIME = 15000,
|
|
|
|
maxTimeout: MAX_CAMERA_SHARE_FAILED_WAIT_TIME = 60000,
|
|
|
|
} = Meteor.settings.public.kurento.cameraTimeouts || {};
|
2020-05-27 01:05:04 +08:00
|
|
|
const CAMERA_QUALITY_THRESHOLDS_ENABLED = Meteor.settings.public.kurento.cameraQualityThresholds.enabled;
|
2019-11-28 21:13:06 +08:00
|
|
|
const PING_INTERVAL = 15000;
|
2018-08-21 02:08:10 +08:00
|
|
|
|
2018-07-21 03:34:50 +08:00
|
|
|
const intlClientErrors = defineMessages({
|
2018-05-17 02:15:46 +08:00
|
|
|
permissionError: {
|
|
|
|
id: 'app.video.permissionError',
|
2019-11-28 21:13:06 +08:00
|
|
|
description: 'Webcam permission error',
|
2018-05-17 21:23:18 +08:00
|
|
|
},
|
2018-08-13 06:39:39 +08:00
|
|
|
iceConnectionStateError: {
|
|
|
|
id: 'app.video.iceConnectionStateError',
|
2019-11-28 21:13:06 +08:00
|
|
|
description: 'Ice connection state failed',
|
2018-08-13 06:39:39 +08:00
|
|
|
},
|
2018-12-18 01:45:57 +08:00
|
|
|
mediaFlowTimeout: {
|
|
|
|
id: 'app.video.mediaFlowTimeout1020',
|
2019-11-28 21:13:06 +08:00
|
|
|
description: 'Media flow timeout',
|
2018-12-18 01:45:57 +08:00
|
|
|
},
|
2018-07-21 03:34:50 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
const intlSFUErrors = defineMessages({
|
|
|
|
2000: {
|
|
|
|
id: 'app.sfu.mediaServerConnectionError2000',
|
2019-11-28 21:13:06 +08:00
|
|
|
description: 'SFU connection to the media server',
|
2018-07-21 03:34:50 +08:00
|
|
|
},
|
|
|
|
2001: {
|
|
|
|
id: 'app.sfu.mediaServerOffline2001',
|
2019-11-28 21:13:06 +08:00
|
|
|
description: 'SFU is offline',
|
2018-05-17 02:15:46 +08:00
|
|
|
},
|
2018-07-21 03:34:50 +08:00
|
|
|
2002: {
|
|
|
|
id: 'app.sfu.mediaServerNoResources2002',
|
2019-11-28 21:13:06 +08:00
|
|
|
description: 'Media server lacks disk, CPU or FDs',
|
2018-07-21 03:34:50 +08:00
|
|
|
},
|
|
|
|
2003: {
|
|
|
|
id: 'app.sfu.mediaServerRequestTimeout2003',
|
2019-11-28 21:13:06 +08:00
|
|
|
description: 'Media requests timeout due to lack of resources',
|
2018-07-21 03:34:50 +08:00
|
|
|
},
|
|
|
|
2021: {
|
|
|
|
id: 'app.sfu.serverIceGatheringFailed2021',
|
2019-11-28 21:13:06 +08:00
|
|
|
description: 'Server cannot enact ICE gathering',
|
2018-07-21 03:34:50 +08:00
|
|
|
},
|
|
|
|
2022: {
|
|
|
|
id: 'app.sfu.serverIceStateFailed2022',
|
2019-11-28 21:13:06 +08:00
|
|
|
description: 'Server endpoint transitioned to a FAILED ICE state',
|
2018-07-21 03:34:50 +08:00
|
|
|
},
|
2018-12-18 01:45:57 +08:00
|
|
|
2200: {
|
|
|
|
id: 'app.sfu.mediaGenericError2200',
|
2019-11-28 21:13:06 +08:00
|
|
|
description: 'SFU component generated a generic error',
|
2018-12-18 01:45:57 +08:00
|
|
|
},
|
2018-07-21 03:34:50 +08:00
|
|
|
2202: {
|
|
|
|
id: 'app.sfu.invalidSdp2202',
|
2019-11-28 21:13:06 +08:00
|
|
|
description: 'Client provided an invalid SDP',
|
2018-07-21 03:34:50 +08:00
|
|
|
},
|
|
|
|
2203: {
|
|
|
|
id: 'app.sfu.noAvailableCodec2203',
|
2019-11-28 21:13:06 +08:00
|
|
|
description: 'Server has no available codec for the client',
|
2018-08-13 06:40:53 +08:00
|
|
|
},
|
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,
|
2020-08-20 23:43:02 +08:00
|
|
|
currentVideoPageIndex: PropTypes.number.isRequired,
|
2020-08-27 10:16:17 +08:00
|
|
|
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 = {
|
2018-03-20 01:52:39 +08:00
|
|
|
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
|
2020-07-09 22:18:56 +08:00
|
|
|
this.ws = new ReconnectingWebSocket(
|
2020-08-05 22:20:58 +08:00
|
|
|
VideoService.getAuthenticatedURL(),
|
2020-07-09 22:18:56 +08:00
|
|
|
[],
|
|
|
|
{ connectionTimeout: WS_CONN_TIMEOUT },
|
|
|
|
);
|
2018-02-17 03:18:53 +08:00
|
|
|
this.wsQueue = [];
|
|
|
|
|
2018-05-23 04:44:30 +08:00
|
|
|
this.restartTimeout = {};
|
|
|
|
this.restartTimer = {};
|
2018-02-17 03:18:53 +08:00
|
|
|
this.webRtcPeers = {};
|
2019-11-28 23:27:34 +08:00
|
|
|
this.outboundIceQueues = {};
|
2018-07-10 05:29:27 +08:00
|
|
|
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);
|
2018-05-03 01:53:53 +08:00
|
|
|
|
2019-11-28 21:13:06 +08:00
|
|
|
this.updateStreams = this.updateStreams.bind(this);
|
2020-08-20 23:43:02 +08:00
|
|
|
this.debouncedConnectStreams = _.debounce(
|
|
|
|
this.connectStreams,
|
|
|
|
VideoService.getPageChangeDebounceTime(),
|
|
|
|
{ leading: false, trailing: true, }
|
|
|
|
);
|
2018-05-03 01:53:53 +08:00
|
|
|
}
|
|
|
|
|
2019-11-28 21:13:06 +08:00
|
|
|
componentDidMount() {
|
2018-12-18 06:19:26 +08:00
|
|
|
this.ws.onopen = this.onWsOpen;
|
2018-12-18 01:45:57 +08:00
|
|
|
this.ws.onclose = this.onWsClose;
|
2018-02-17 03:18:53 +08:00
|
|
|
|
2018-04-14 03:16:06 +08:00
|
|
|
window.addEventListener('online', this.openWs);
|
2018-02-17 03:18:53 +08:00
|
|
|
window.addEventListener('offline', this.onWsClose);
|
|
|
|
|
2018-12-18 01:45:57 +08:00
|
|
|
this.ws.onmessage = this.onWsMessage;
|
2018-04-12 02:50:14 +08:00
|
|
|
|
2019-11-28 21:13:06 +08:00
|
|
|
window.addEventListener('beforeunload', this.onBeforeUnload);
|
2018-04-12 02:50:14 +08:00
|
|
|
}
|
|
|
|
|
2019-06-03 22:05:09 +08:00
|
|
|
componentDidUpdate(prevProps) {
|
2020-08-20 23:43:02 +08:00
|
|
|
const { isUserLocked, streams, currentVideoPageIndex } = this.props;
|
|
|
|
|
|
|
|
// Only debounce when page changes to avoid unecessary debouncing
|
|
|
|
const shouldDebounce = VideoService.isPaginationEnabled()
|
|
|
|
&& prevProps.currentVideoPageIndex !== currentVideoPageIndex;
|
2018-04-12 02:50:14 +08:00
|
|
|
|
2020-08-20 23:43:02 +08:00
|
|
|
this.updateStreams(streams, shouldDebounce);
|
2018-04-12 02:50:14 +08:00
|
|
|
|
2019-11-28 21:13:06 +08:00
|
|
|
if (!prevProps.isUserLocked && isUserLocked) VideoService.lockUser();
|
2019-06-03 22:05:09 +08:00
|
|
|
}
|
|
|
|
|
2018-02-17 03:18:53 +08:00
|
|
|
componentWillUnmount() {
|
2018-12-18 01:45:57 +08:00
|
|
|
this.ws.onmessage = null;
|
|
|
|
this.ws.onopen = null;
|
|
|
|
this.ws.onclose = null;
|
2018-02-17 03:18:53 +08:00
|
|
|
|
2018-04-14 03:16:06 +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);
|
2018-05-03 01:53:53 +08:00
|
|
|
|
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
|
2018-04-26 23:39:24 +08:00
|
|
|
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-05-03 21:57:03 +08:00
|
|
|
|
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
|
|
|
|
2018-05-03 21:57:03 +08:00
|
|
|
clearInterval(this.pingInterval);
|
2018-03-20 01:52:39 +08:00
|
|
|
|
2019-11-28 21:13:06 +08:00
|
|
|
VideoService.exitVideo();
|
2018-12-18 01:45:57 +08:00
|
|
|
|
2018-03-20 01:52:39 +08:00
|
|
|
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 });
|
2018-05-03 21:57:03 +08:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2020-05-27 01:05:04 +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)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-19 01:00:47 +08:00
|
|
|
getStreamsToConnectAndDisconnect(streams) {
|
2019-11-28 21:13:06 +08:00
|
|
|
const streamsCameraIds = streams.map(s => s.cameraId);
|
|
|
|
const streamsConnected = Object.keys(this.webRtcPeers);
|
2018-05-03 21:57:03 +08:00
|
|
|
|
2019-11-29 04:28:41 +08:00
|
|
|
const streamsToConnect = streamsCameraIds.filter(cameraId => {
|
|
|
|
return !streamsConnected.includes(cameraId);
|
2018-12-18 06:19:26 +08:00
|
|
|
});
|
|
|
|
|
2019-11-29 04:28:41 +08:00
|
|
|
const streamsToDisconnect = streamsConnected.filter(cameraId => {
|
|
|
|
return !streamsCameraIds.includes(cameraId);
|
|
|
|
});
|
2018-12-18 06:19:26 +08:00
|
|
|
|
2020-08-19 01:00:47 +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
|
|
|
});
|
2020-08-19 01:00:47 +08:00
|
|
|
}
|
2019-11-29 04:28:41 +08:00
|
|
|
|
2020-08-19 01:00:47 +08:00
|
|
|
disconnectStreams(streamsToDisconnect) {
|
2019-11-28 21:13:06 +08:00
|
|
|
streamsToDisconnect.forEach(cameraId => this.stopWebRTCPeer(cameraId));
|
2020-08-19 01:00:47 +08:00
|
|
|
}
|
|
|
|
|
2020-08-20 23:43:02 +08:00
|
|
|
updateStreams(streams, shouldDebounce = false) {
|
2020-08-19 01:00:47 +08:00
|
|
|
const [streamsToConnect, streamsToDisconnect] = this.getStreamsToConnectAndDisconnect(streams);
|
|
|
|
|
2020-08-20 23:43:02 +08:00
|
|
|
if(shouldDebounce) {
|
|
|
|
this.debouncedConnectStreams(streamsToConnect);
|
|
|
|
} else {
|
|
|
|
this.connectStreams(streamsToConnect);
|
|
|
|
}
|
|
|
|
|
2020-08-19 01:00:47 +08:00
|
|
|
this.disconnectStreams(streamsToDisconnect);
|
2020-05-27 01:05:04 +08:00
|
|
|
|
|
|
|
if (CAMERA_QUALITY_THRESHOLDS_ENABLED) {
|
2020-08-25 02:30:24 +08:00
|
|
|
this.updateThreshold(this.props.totalNumberOfStreams);
|
2020-05-27 01:05:04 +08:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
});
|
2019-04-24 04:23:32 +08:00
|
|
|
} else if (message.id !== 'stop') {
|
2018-02-17 03:18:53 +08:00
|
|
|
// No need to queue video stop messages
|
2019-04-24 04:23:32 +08:00
|
|
|
this.wsQueue.push(message);
|
2018-02-17 03:18:53 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
connectedToMediaServer() {
|
|
|
|
return this.ws.readyState === WebSocket.OPEN;
|
|
|
|
}
|
|
|
|
|
2019-11-28 23:27:34 +08:00
|
|
|
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
|
|
|
|
2018-07-12 00:32:19 +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;
|
2018-07-12 00:32:19 +08:00
|
|
|
}
|
2018-12-22 03:47:15 +08:00
|
|
|
|
|
|
|
peer.didSDPAnswered = true;
|
2019-11-28 21:13:06 +08:00
|
|
|
this.processOutboundIceQueue(peer, role, cameraId);
|
|
|
|
VideoService.processInboundIceQueue(peer, cameraId);
|
2018-07-12 00:32:19 +08:00
|
|
|
});
|
|
|
|
} 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-07-12 00:32:19 +08:00
|
|
|
}
|
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)}`);
|
2018-06-20 01:36:12 +08:00
|
|
|
|
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
|
2019-11-28 23:27:34 +08:00
|
|
|
if (peer.inboundIceQueue == null) {
|
|
|
|
peer.inboundIceQueue = [];
|
2018-07-21 03:34:50 +08:00
|
|
|
}
|
2019-11-28 23:27:34 +08:00
|
|
|
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
|
|
|
|
2018-08-14 04:26:45 +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];
|
2019-11-28 23:27:34 +08:00
|
|
|
if (peer && peer.peerConnection) {
|
2019-11-28 21:13:06 +08:00
|
|
|
const conn = peer.peerConnection;
|
|
|
|
conn.oniceconnectionstatechange = null;
|
2018-08-21 02:08:10 +08:00
|
|
|
}
|
2018-08-14 04:26:45 +08:00
|
|
|
|
2019-11-28 21:13:06 +08:00
|
|
|
if (isLocal) {
|
2019-11-29 04:28:41 +08:00
|
|
|
VideoService.stopVideo(cameraId);
|
2018-04-12 02:50:14 +08:00
|
|
|
}
|
|
|
|
|
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}`);
|
2018-04-14 03:16:06 +08:00
|
|
|
this.sendMessage({
|
2019-11-28 21:13:06 +08:00
|
|
|
id: 'stop',
|
2018-04-14 03:16:06 +08:00
|
|
|
type: 'video',
|
2019-11-28 21:13:06 +08:00
|
|
|
cameraId,
|
2019-07-19 04:44:21 +08:00
|
|
|
role,
|
2018-04-14 03:16:06 +08:00
|
|
|
});
|
|
|
|
|
2019-11-29 01:44:39 +08:00
|
|
|
// 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
|
|
|
}
|
2018-12-18 01:45:57 +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
|
|
|
}
|
2018-12-18 01:45:57 +08:00
|
|
|
}
|
2018-05-23 04:44:30 +08:00
|
|
|
|
2019-11-28 21:13:06 +08:00
|
|
|
this.destroyWebRTCPeer(cameraId);
|
2018-04-19 02:16:26 +08:00
|
|
|
}
|
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-05-25 03:55:35 +08:00
|
|
|
}
|
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) {
|
2018-07-10 05:29:27 +08:00
|
|
|
let iceServers = [];
|
2018-02-17 03:18:53 +08:00
|
|
|
|
2018-11-10 00:25:48 +08:00
|
|
|
// Check if the peer is already being processed
|
2019-11-28 21:13:06 +08:00
|
|
|
if (this.webRtcPeers[cameraId]) {
|
2018-11-10 00:25:48 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-11-28 21:13:06 +08:00
|
|
|
this.webRtcPeers[cameraId] = {};
|
2018-11-10 00:25:48 +08:00
|
|
|
|
2019-11-15 00:35:56 +08:00
|
|
|
// 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) {
|
2019-11-15 00:35:56 +08:00
|
|
|
try {
|
|
|
|
await tryGenerateIceCandidates();
|
|
|
|
} catch (error) {
|
|
|
|
logger.error({
|
|
|
|
logCode: 'video_provider_no_valid_candidate_gum_failure',
|
|
|
|
extraInfo: {
|
|
|
|
errorName: error.name,
|
|
|
|
errorMessage: error.message,
|
2019-11-15 03:07:35 +08:00
|
|
|
},
|
2019-11-15 00:35:56 +08:00
|
|
|
}, `Forced gUM to release additional ICE candidates failed due to ${error.name}.`);
|
2019-11-15 03:07:35 +08:00
|
|
|
}
|
2019-11-15 00:35:56 +08:00
|
|
|
}
|
|
|
|
|
2018-07-10 05:29:27 +08:00
|
|
|
try {
|
2019-11-28 21:13:06 +08:00
|
|
|
iceServers = await fetchWebRTCMappedStunTurnServers(this.info.sessionToken);
|
2018-07-10 05:29:27 +08:00
|
|
|
} 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: {
|
2020-05-21 12:20:46 +08:00
|
|
|
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');
|
2020-05-21 12:20:46 +08:00
|
|
|
// Use fallback STUN server
|
|
|
|
iceServers = getMappedFallbackStun();
|
2018-07-10 05:29:27 +08:00
|
|
|
} 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 = {
|
2018-07-10 05:29:27 +08:00
|
|
|
mediaConstraints: {
|
|
|
|
audio: false,
|
2019-04-09 06:07:26 +08:00
|
|
|
video: constraints,
|
2018-07-10 05:29:27 +08:00
|
|
|
},
|
2019-11-28 21:13:06 +08:00
|
|
|
onicecandidate: this._getOnIceCandidateCallback(cameraId, isLocal),
|
2018-07-10 05:29:27 +08:00
|
|
|
};
|
2018-02-17 05:11:59 +08:00
|
|
|
|
2018-07-10 05:29:27 +08:00
|
|
|
if (iceServers.length > 0) {
|
2019-07-19 04:44:21 +08:00
|
|
|
peerOptions.configuration = {};
|
|
|
|
peerOptions.configuration.iceServers = iceServers;
|
2018-07-10 05:29:27 +08:00
|
|
|
}
|
2018-04-12 02:50:14 +08:00
|
|
|
|
2018-10-04 02:39:55 +08:00
|
|
|
let WebRtcPeerObj;
|
2019-11-28 21:13:06 +08:00
|
|
|
if (isLocal) {
|
2018-07-10 05:29:27 +08:00
|
|
|
WebRtcPeerObj = window.kurentoUtils.WebRtcPeer.WebRtcPeerSendonly;
|
2018-10-04 02:39:55 +08:00
|
|
|
} else {
|
|
|
|
WebRtcPeerObj = window.kurentoUtils.WebRtcPeer.WebRtcPeerRecvonly;
|
2018-07-10 05:29:27 +08:00
|
|
|
}
|
2018-05-23 04:44:30 +08:00
|
|
|
|
2019-11-28 21:13:06 +08:00
|
|
|
this.webRtcPeers[cameraId] = new WebRtcPeerObj(peerOptions, (error) => {
|
|
|
|
const peer = this.webRtcPeers[cameraId];
|
|
|
|
|
2018-07-10 05:29:27 +08:00
|
|
|
peer.started = false;
|
|
|
|
peer.attached = false;
|
|
|
|
peer.didSDPAnswered = false;
|
2020-05-27 01:05:04 +08:00
|
|
|
peer.isPublisher = isLocal;
|
|
|
|
peer.originalProfileId = profileId;
|
|
|
|
peer.currentProfileId = profileId;
|
|
|
|
|
2019-11-28 23:27:34 +08:00
|
|
|
if (peer.inboundIceQueue == null) {
|
|
|
|
peer.inboundIceQueue = [];
|
2018-07-21 03:34:50 +08:00
|
|
|
}
|
2018-02-17 03:18:53 +08:00
|
|
|
|
2018-07-10 05:29:27 +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
|
|
|
}
|
|
|
|
|
2018-07-10 05:29:27 +08:00
|
|
|
peer.generateOffer((errorGenOffer, offerSdp) => {
|
|
|
|
if (errorGenOffer) {
|
2019-11-28 21:13:06 +08:00
|
|
|
return this._onWebRTCError(errorGenOffer, cameraId, isLocal);
|
2018-07-10 05:29:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const message = {
|
|
|
|
id: 'start',
|
2019-11-28 21:13:06 +08:00
|
|
|
type: 'video',
|
|
|
|
cameraId,
|
|
|
|
role: VideoService.getRole(isLocal),
|
2018-07-10 05:29:27 +08:00
|
|
|
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,
|
2019-06-14 02:24:54 +08:00
|
|
|
bitrate,
|
2020-08-22 06:10:25 +08:00
|
|
|
record: VideoService.getRecord(),
|
2018-07-10 05:29:27 +08:00
|
|
|
};
|
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
|
|
|
|
2018-07-10 05:29:27 +08:00
|
|
|
this.sendMessage(message);
|
2019-07-24 06:24:31 +08:00
|
|
|
|
|
|
|
return false;
|
2018-07-10 05:29:27 +08:00
|
|
|
});
|
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);
|
2019-11-30 05:48:04 +08:00
|
|
|
VideoService.monitor(conn);
|
2019-05-25 03:55:35 +08:00
|
|
|
}
|
2018-07-10 05:29:27 +08:00
|
|
|
}
|
2018-02-17 03:18:53 +08:00
|
|
|
}
|
|
|
|
|
2019-11-28 21:13:06 +08:00
|
|
|
_getWebRTCStartTimeout(cameraId, isLocal) {
|
|
|
|
const { intl } = this.props;
|
2018-05-23 04:44:30 +08:00
|
|
|
|
|
|
|
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));
|
2019-11-29 04:28:41 +08:00
|
|
|
this.stopWebRTCPeer(cameraId);
|
2018-05-23 04:44:30 +08:00
|
|
|
} 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(
|
2019-11-29 01:44:39 +08:00
|
|
|
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
|
|
|
|
2019-11-29 01:44:39 +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-11-29 01:44:39 +08:00
|
|
|
}
|
|
|
|
|
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,
|
2019-11-29 01:44:39 +08:00
|
|
|
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);
|
2018-05-23 04:44:30 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-11-28 21:13:06 +08:00
|
|
|
_onWebRTCError(error, cameraId, isLocal) {
|
2019-11-29 01:44:39 +08:00
|
|
|
const { intl } = this.props;
|
2018-12-22 05:25:47 +08:00
|
|
|
|
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) {
|
2018-12-22 05:25:47 +08:00
|
|
|
return;
|
|
|
|
}
|
2018-05-23 04:44:30 +08:00
|
|
|
|
2018-07-21 03:34:50 +08:00
|
|
|
const errorMessage = intlClientErrors[error.name]
|
2018-12-18 01:45:57 +08:00
|
|
|
|| intlSFUErrors[error] || intlClientErrors.permissionError;
|
2019-11-29 01:44:39 +08:00
|
|
|
// 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));
|
2019-11-29 01:44:39 +08:00
|
|
|
} 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);
|
2019-11-29 01:44:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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);
|
2018-05-23 04:44:30 +08:00
|
|
|
|
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}`);
|
2018-05-23 04:44:30 +08:00
|
|
|
}
|
|
|
|
|
2019-11-28 21:13:06 +08:00
|
|
|
setReconnectionTimeout(cameraId, isLocal) {
|
|
|
|
const peer = this.webRtcPeers[cameraId];
|
2019-11-29 01:44:39 +08:00
|
|
|
const peerHasStarted = peer && peer.started === true;
|
2019-11-28 21:13:06 +08:00
|
|
|
const shouldSetReconnectionTimeout = !this.restartTimeout[cameraId] && !peerHasStarted;
|
2019-11-29 01:44:39 +08:00
|
|
|
|
|
|
|
if (shouldSetReconnectionTimeout) {
|
2019-11-28 21:13:06 +08:00
|
|
|
const newReconnectTimer = this.restartTimer[cameraId] || CAMERA_SHARE_FAILED_WAIT_TIME;
|
|
|
|
this.restartTimer[cameraId] = newReconnectTimer;
|
2019-11-29 01:44:39 +08:00
|
|
|
|
|
|
|
logger.info({
|
|
|
|
logCode: 'video_provider_setup_reconnect',
|
|
|
|
extraInfo: {
|
2019-11-28 21:13:06 +08:00
|
|
|
cameraId,
|
2019-11-29 01:44:39 +08:00
|
|
|
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-29 01:44:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-28 21:13:06 +08:00
|
|
|
_getOnIceCandidateCallback(cameraId, isLocal) {
|
2018-05-23 04:44:30 +08:00
|
|
|
return (candidate) => {
|
2020-08-05 22:20:58 +08:00
|
|
|
const peer = this.webRtcPeers[cameraId];
|
|
|
|
const role = VideoService.getRole(isLocal);
|
2019-11-29 01:44:39 +08:00
|
|
|
// Setup a timeout only when the first candidate is generated and if the peer wasn't
|
2019-06-27 03:16:28 +08:00
|
|
|
// 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
|
|
|
|
2019-11-28 23:27:34 +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);
|
2019-11-28 23:27:34 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-11-28 21:13:06 +08:00
|
|
|
this.sendIceCandidateToSFU(peer, role, candidate, cameraId);
|
2019-11-28 23:27:34 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
2018-03-20 01:52:39 +08:00
|
|
|
};
|
2019-11-28 23:27:34 +08:00
|
|
|
this.sendMessage(message);
|
2018-03-20 01:52:39 +08:00
|
|
|
}
|
2018-02-17 03:18:53 +08:00
|
|
|
|
2019-11-28 21:13:06 +08:00
|
|
|
_getOnIceConnectionStateChangeCallback(cameraId, isLocal) {
|
2018-08-13 06:39:39 +08:00
|
|
|
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;
|
2018-08-13 06:39:39 +08:00
|
|
|
|
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));
|
2018-08-13 06:40:53 +08:00
|
|
|
};
|
2018-08-13 06:39:39 +08:00
|
|
|
}
|
|
|
|
|
2019-11-28 21:13:06 +08:00
|
|
|
attachVideoStream(cameraId) {
|
|
|
|
const video = this.videoTags[cameraId];
|
2018-07-10 05:29:27 +08:00
|
|
|
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}`);
|
2018-07-12 00:32:19 +08:00
|
|
|
return;
|
2018-07-10 05:29:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (video.srcObject) {
|
2019-11-28 21:13:06 +08:00
|
|
|
delete this.videoTags[cameraId];
|
2018-07-10 05:29:27 +08:00
|
|
|
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];
|
2018-03-20 01:52:39 +08:00
|
|
|
|
2018-05-30 02:54:01 +08:00
|
|
|
const attachVideoStreamHelper = () => {
|
2019-11-28 21:13:06 +08:00
|
|
|
const stream = isLocal ? peer.getLocalStream() : peer.getRemoteStream();
|
2018-04-12 02:50:14 +08:00
|
|
|
video.pause();
|
|
|
|
video.srcObject = stream;
|
|
|
|
video.load();
|
2018-05-23 04:55:24 +08:00
|
|
|
|
|
|
|
peer.attached = true;
|
2019-11-28 21:13:06 +08:00
|
|
|
delete this.videoTags[cameraId];
|
2018-04-12 02:50:14 +08:00
|
|
|
};
|
|
|
|
|
2018-05-23 04:55:24 +08:00
|
|
|
|
|
|
|
// If peer has started playing attach to tag, otherwise wait a while
|
2018-04-19 02:16:26 +08:00
|
|
|
if (peer) {
|
2018-05-23 04:55:24 +08:00
|
|
|
if (peer.started) {
|
2018-05-30 02:54:01 +08:00
|
|
|
attachVideoStreamHelper();
|
2018-04-19 02:16:26 +08:00
|
|
|
}
|
2018-02-17 03:18:53 +08:00
|
|
|
|
2018-05-23 04:55:24 +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-04-19 02:16:26 +08:00
|
|
|
}
|
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;
|
2018-07-10 05:29:27 +08:00
|
|
|
|
|
|
|
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) {
|
2018-08-13 06:40:53 +08:00
|
|
|
const { cameraId } = message;
|
2018-04-19 02:16:26 +08:00
|
|
|
|
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}`);
|
2019-11-29 01:44:39 +08:00
|
|
|
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];
|
2018-04-14 03:16:06 +08:00
|
|
|
|
2018-07-12 00:32:19 +08:00
|
|
|
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}`);
|
2018-05-23 04:55:24 +08:00
|
|
|
|
2019-06-27 03:25:05 +08:00
|
|
|
peer.started = true;
|
|
|
|
|
2018-07-12 00:32:19 +08:00
|
|
|
// 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
|
|
|
|
2018-07-12 00:32:19 +08:00
|
|
|
if (!peer.attached) {
|
2019-07-19 04:44:21 +08:00
|
|
|
this.attachVideoStream(cameraId);
|
2018-07-12 00:32:19 +08:00
|
|
|
}
|
2018-05-23 04:55:24 +08:00
|
|
|
|
2019-11-28 21:13:06 +08:00
|
|
|
VideoService.playStart(cameraId);
|
2018-07-12 00:32:19 +08:00
|
|
|
} 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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-21 03:34:50 +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) {
|
2019-11-29 01:44:39 +08:00
|
|
|
// The publisher instance received an error from the server. There's no reconnect,
|
|
|
|
// stop it.
|
2019-11-29 04:28:41 +08:00
|
|
|
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() {
|
2020-08-27 10:16:17 +08:00
|
|
|
const { swapLayout, currentVideoPageIndex, streams } = this.props;
|
2018-04-12 02:50:14 +08:00
|
|
|
|
2018-02-17 03:18:53 +08:00
|
|
|
return (
|
2020-08-19 01:00:47 +08:00
|
|
|
<VideoListContainer
|
2019-11-28 21:13:06 +08:00
|
|
|
streams={streams}
|
2018-12-18 06:19:26 +08:00
|
|
|
onMount={this.createVideoTag}
|
2019-08-28 03:36:21 +08:00
|
|
|
swapLayout={swapLayout}
|
2020-08-20 23:43:02 +08:00
|
|
|
currentVideoPageIndex={currentVideoPageIndex}
|
2018-03-20 01:52:39 +08:00
|
|
|
/>
|
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);
|