2022-07-12 21:34:03 +08:00
|
|
|
/* eslint react/sort-comp: 0 */
|
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';
|
2020-09-17 22:37:28 +08:00
|
|
|
import { defineMessages, injectIntl } from 'react-intl';
|
2023-08-10 00:26:42 +08:00
|
|
|
import { debounce } from '/imports/utils/debounce';
|
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';
|
2020-05-21 12:20:46 +08:00
|
|
|
import {
|
|
|
|
fetchWebRTCMappedStunTurnServers,
|
|
|
|
getMappedFallbackStun,
|
|
|
|
} from '/imports/utils/fetchStunTurnServers';
|
2019-11-28 21:13:06 +08:00
|
|
|
import logger from '/imports/startup/client/logger';
|
2021-01-21 03:03:12 +08:00
|
|
|
import { notifyStreamStateChange } from '/imports/ui/services/bbb-webrtc-sfu/stream-state-service';
|
2021-07-20 04:19:06 +08:00
|
|
|
import VideoPreviewService from '../video-preview/service';
|
|
|
|
import MediaStreamUtils from '/imports/utils/media-stream-utils';
|
2022-07-13 01:50:16 +08:00
|
|
|
import BBBVideoStream from '/imports/ui/services/webrtc-base/bbb-video-stream';
|
2021-07-20 04:19:06 +08:00
|
|
|
import {
|
2021-11-11 22:33:55 +08:00
|
|
|
EFFECT_TYPES,
|
|
|
|
getSessionVirtualBackgroundInfo,
|
|
|
|
} from '/imports/ui/services/virtual-background/service';
|
2021-11-19 19:10:24 +08:00
|
|
|
import { notify } from '/imports/ui/services/notification';
|
2021-12-01 04:31:09 +08:00
|
|
|
import { shouldForceRelay } from '/imports/ui/services/bbb-webrtc-sfu/utils';
|
2022-07-12 21:34:03 +08:00
|
|
|
import WebRtcPeer from '/imports/ui/services/webrtc-base/peer';
|
2018-02-17 03:18:53 +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
|
|
|
},
|
2021-07-08 23:10:22 +08:00
|
|
|
mediaTimedOutError: {
|
|
|
|
id: 'app.video.mediaTimedOutError',
|
|
|
|
description: 'Media was ejected by the server due to lack of valid media',
|
|
|
|
},
|
2021-07-20 04:19:06 +08:00
|
|
|
virtualBgGenericError: {
|
|
|
|
id: 'app.video.virtualBackground.genericError',
|
|
|
|
description: 'Failed to apply camera effect',
|
|
|
|
},
|
2022-07-13 05:44:20 +08:00
|
|
|
inactiveError: {
|
|
|
|
id: 'app.video.inactiveError',
|
|
|
|
description: 'Camera stopped unexpectedly',
|
|
|
|
},
|
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,
|
2023-02-24 00:21:20 +08:00
|
|
|
isMeteorConnected: PropTypes.bool.isRequired,
|
2024-01-26 21:29:52 +08:00
|
|
|
playStart: PropTypes.func.isRequired,
|
2024-01-27 00:21:58 +08:00
|
|
|
sendUserUnshareWebcam: PropTypes.func.isRequired,
|
2019-07-24 06:24:31 +08:00
|
|
|
};
|
|
|
|
|
2018-02-17 03:18:53 +08:00
|
|
|
class VideoProvider extends Component {
|
2024-01-27 00:21:58 +08:00
|
|
|
onBeforeUnload() {
|
|
|
|
const { sendUserUnshareWebcam } = this.props;
|
|
|
|
VideoService.onBeforeUnload(sendUserUnshareWebcam);
|
2020-04-23 22:07:44 +08:00
|
|
|
}
|
|
|
|
|
2023-04-10 22:44:59 +08:00
|
|
|
static shouldAttachVideoStream(peer, videoElement) {
|
2023-02-24 00:21:20 +08:00
|
|
|
// Conditions to safely attach a stream to a video element in all browsers:
|
2023-04-10 22:44:59 +08:00
|
|
|
// 1 - Peer exists, video element exists
|
|
|
|
// 2 - Target stream differs from videoElement's (diff)
|
2023-02-24 00:21:20 +08:00
|
|
|
// 3a - If the stream is a remote one, the safest (*ahem* Safari) moment to
|
|
|
|
// do so is waiting for the server to confirm that media has flown out of it
|
|
|
|
// towards te remote end (peer.started)
|
|
|
|
// 3b - If the stream is a local one (webcam sharer) and is started
|
|
|
|
// 4 - If the stream is local one, check if there area video tracks there are
|
|
|
|
// video tracks: attach it
|
2023-04-10 22:44:59 +08:00
|
|
|
if (peer == null || videoElement == null) return false;
|
|
|
|
const stream = peer.isPublisher ? peer.getLocalStream() : peer.getRemoteStream();
|
|
|
|
const diff = stream && (stream.id !== videoElement.srcObject?.id || !videoElement.paused);
|
|
|
|
|
|
|
|
if (peer.started && diff) return true;
|
|
|
|
|
2023-02-24 00:21:20 +08:00
|
|
|
return peer.isPublisher
|
|
|
|
&& peer.getLocalStream()
|
2023-04-10 22:44:59 +08:00
|
|
|
&& peer.getLocalStream().getVideoTracks().length > 0
|
|
|
|
&& diff;
|
2023-02-24 00:21:20 +08:00
|
|
|
}
|
|
|
|
|
2018-02-17 03:18:53 +08:00
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
|
2021-09-01 20:23:39 +08:00
|
|
|
// socketOpen state is there to force update when the signaling socket opens or closes
|
|
|
|
this.state = {
|
|
|
|
socketOpen: false,
|
|
|
|
};
|
2021-09-02 03:13:22 +08:00
|
|
|
this._isMounted = false;
|
2019-11-28 21:13:06 +08:00
|
|
|
this.info = VideoService.getInfo();
|
2023-02-24 00:21:20 +08:00
|
|
|
// Signaling message queue arrays indexed by stream (== cameraId)
|
|
|
|
this.wsQueues = {};
|
2018-05-23 04:44:30 +08:00
|
|
|
this.restartTimeout = {};
|
|
|
|
this.restartTimer = {};
|
2021-12-16 23:12:52 +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);
|
2021-01-21 03:03:12 +08:00
|
|
|
this.destroyVideoTag = this.destroyVideoTag.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.updateStreams = this.updateStreams.bind(this);
|
2023-08-11 21:57:39 +08:00
|
|
|
this.connectStreams = this.connectStreams.bind(this);
|
2023-03-01 21:39:04 +08:00
|
|
|
this.debouncedConnectStreams = debounce(
|
2023-08-10 00:26:42 +08:00
|
|
|
this.connectStreams,
|
|
|
|
VideoService.getPageChangeDebounceTime(),
|
|
|
|
{ leading: false, trailing: true },
|
2020-08-20 23:43:02 +08:00
|
|
|
);
|
2022-04-12 01:09:13 +08:00
|
|
|
this.startVirtualBackgroundByDrop = this.startVirtualBackgroundByDrop.bind(this);
|
2024-01-27 00:21:58 +08:00
|
|
|
this.onBeforeUnload = this.onBeforeUnload.bind(this);
|
2018-05-03 01:53:53 +08:00
|
|
|
}
|
|
|
|
|
2019-11-28 21:13:06 +08:00
|
|
|
componentDidMount() {
|
2021-09-02 03:13:22 +08:00
|
|
|
this._isMounted = true;
|
2021-12-16 23:12:52 +08:00
|
|
|
VideoService.updatePeerDictionaryReference(this.webRtcPeers);
|
2023-02-24 00:21:20 +08:00
|
|
|
this.ws = this.openWs();
|
2024-01-27 00:21:58 +08:00
|
|
|
window.addEventListener('beforeunload', this.onBeforeUnload);
|
2018-02-17 03:18:53 +08:00
|
|
|
}
|
|
|
|
|
2019-11-28 21:13:06 +08:00
|
|
|
componentDidUpdate(prevProps) {
|
2023-02-24 00:21:20 +08:00
|
|
|
const {
|
|
|
|
isUserLocked,
|
|
|
|
streams,
|
|
|
|
currentVideoPageIndex,
|
2024-01-27 00:21:58 +08:00
|
|
|
isMeteorConnected,
|
|
|
|
sendUserUnshareWebcam,
|
2023-02-24 00:21:20 +08:00
|
|
|
} = this.props;
|
|
|
|
const { socketOpen } = this.state;
|
2020-08-20 23:43:02 +08:00
|
|
|
|
2024-03-18 21:58:53 +08:00
|
|
|
// Only debounce when page changes to avoid unnecessary debouncing
|
2020-08-20 23:43:02 +08:00
|
|
|
const shouldDebounce = VideoService.isPaginationEnabled()
|
|
|
|
&& prevProps.currentVideoPageIndex !== currentVideoPageIndex;
|
2018-04-12 02:50:14 +08:00
|
|
|
|
2023-02-24 00:21:20 +08:00
|
|
|
if (isMeteorConnected && socketOpen) this.updateStreams(streams, shouldDebounce);
|
2024-01-27 00:21:58 +08:00
|
|
|
if (!prevProps.isUserLocked && isUserLocked) VideoService.lockUser(sendUserUnshareWebcam);
|
2023-02-24 00:21:20 +08:00
|
|
|
|
|
|
|
// Signaling socket expired its retries and meteor is connected - create
|
|
|
|
// a new signaling socket instance from scratch
|
|
|
|
if (!socketOpen
|
|
|
|
&& isMeteorConnected
|
|
|
|
&& this.ws == null) {
|
|
|
|
this.ws = this.openWs();
|
|
|
|
}
|
2019-06-03 22:05:09 +08:00
|
|
|
}
|
|
|
|
|
2018-02-17 03:18:53 +08:00
|
|
|
componentWillUnmount() {
|
2024-01-27 00:21:58 +08:00
|
|
|
const { sendUserUnshareWebcam } = this.props;
|
2023-02-15 04:54:26 +08:00
|
|
|
this._isMounted = false;
|
2021-12-16 23:12:52 +08:00
|
|
|
VideoService.updatePeerDictionaryReference({});
|
|
|
|
|
2024-04-09 04:09:47 +08:00
|
|
|
if (this.ws) {
|
|
|
|
this.ws.onmessage = null;
|
|
|
|
this.ws.onopen = null;
|
|
|
|
this.ws.onclose = null;
|
|
|
|
}
|
2018-02-17 03:18:53 +08:00
|
|
|
|
2024-01-27 00:21:58 +08:00
|
|
|
window.removeEventListener('beforeunload', this.onBeforeUnload);
|
|
|
|
VideoService.exitVideo(sendUserUnshareWebcam);
|
2021-04-09 10:58:13 +08:00
|
|
|
Object.keys(this.webRtcPeers).forEach((stream) => {
|
|
|
|
this.stopWebRTCPeer(stream, false);
|
2018-02-17 03:18:53 +08:00
|
|
|
});
|
2023-02-24 00:21:20 +08:00
|
|
|
this.terminateWs();
|
2023-02-15 04:54:26 +08:00
|
|
|
}
|
2018-02-17 03:18:53 +08:00
|
|
|
|
2023-02-15 04:54:26 +08:00
|
|
|
openWs() {
|
2024-05-29 21:26:11 +08:00
|
|
|
// Default values and default empty object to be backwards compat with 2.2.
|
|
|
|
// FIXME Remove hardcoded defaults 2.3.
|
|
|
|
const {
|
|
|
|
connectionTimeout: WS_CONN_TIMEOUT = 4000,
|
|
|
|
maxRetries: WS_MAX_RETRIES = 5,
|
|
|
|
debug: WS_DEBUG,
|
|
|
|
} = window.meetingClientSettings.public.kurento.cameraWsOptions;
|
|
|
|
|
2023-02-24 00:21:20 +08:00
|
|
|
const ws = new ReconnectingWebSocket(
|
|
|
|
VideoService.getAuthenticatedURL(), [], {
|
|
|
|
connectionTimeout: WS_CONN_TIMEOUT,
|
|
|
|
debug: WS_DEBUG,
|
|
|
|
maxRetries: WS_MAX_RETRIES,
|
|
|
|
maxEnqueuedMessages: 0,
|
2024-05-29 21:26:11 +08:00
|
|
|
},
|
2023-02-15 04:54:26 +08:00
|
|
|
);
|
2023-02-24 00:21:20 +08:00
|
|
|
ws.onopen = this.onWsOpen;
|
|
|
|
ws.onclose = this.onWsClose;
|
|
|
|
ws.onmessage = this.onWsMessage;
|
2023-02-15 04:54:26 +08:00
|
|
|
|
2023-02-24 00:21:20 +08:00
|
|
|
return ws;
|
2023-02-15 04:54:26 +08:00
|
|
|
}
|
|
|
|
|
2023-02-24 00:21:20 +08:00
|
|
|
terminateWs() {
|
|
|
|
if (this.ws) {
|
|
|
|
this.clearWSHeartbeat();
|
|
|
|
this.ws.close();
|
|
|
|
this.ws = null;
|
|
|
|
}
|
2023-02-15 04:54:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
_updateLastMsgTime() {
|
|
|
|
this.ws.isAlive = true;
|
|
|
|
this.ws.lastMsgTime = Date.now();
|
|
|
|
}
|
|
|
|
|
|
|
|
_getTimeSinceLastMsg() {
|
|
|
|
return Date.now() - this.ws.lastMsgTime;
|
|
|
|
}
|
|
|
|
|
|
|
|
setupWSHeartbeat() {
|
2024-05-29 21:26:11 +08:00
|
|
|
// Default values and default empty object to be backwards compat with 2.2.
|
|
|
|
// FIXME Remove hardcoded defaults 2.3.
|
|
|
|
const {
|
|
|
|
heartbeat: WS_HEARTBEAT_OPTS = {
|
|
|
|
interval: 15000,
|
|
|
|
delay: 3000,
|
|
|
|
reconnectOnFailure: true,
|
|
|
|
},
|
|
|
|
} = window.meetingClientSettings.public.kurento.cameraWsOptions;
|
|
|
|
|
2023-02-24 00:21:20 +08:00
|
|
|
if (WS_HEARTBEAT_OPTS.interval === 0 || this.ws == null || this.ws.wsHeartbeat) return;
|
2023-02-15 04:54:26 +08:00
|
|
|
|
|
|
|
this.ws.isAlive = true;
|
2023-02-24 00:21:20 +08:00
|
|
|
this.ws.wsHeartbeat = setInterval(() => {
|
2023-02-15 04:54:26 +08:00
|
|
|
if (this.ws.isAlive === false) {
|
|
|
|
logger.warn({
|
|
|
|
logCode: 'video_provider_ws_heartbeat_failed',
|
|
|
|
}, 'Video provider WS heartbeat failed.');
|
|
|
|
|
|
|
|
if (WS_HEARTBEAT_OPTS.reconnectOnFailure) this.ws.reconnect();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-02-24 00:21:20 +08:00
|
|
|
if (this._getTimeSinceLastMsg() < (
|
2023-02-15 04:54:26 +08:00
|
|
|
WS_HEARTBEAT_OPTS.interval - WS_HEARTBEAT_OPTS.delay
|
|
|
|
)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.ws.isAlive = false;
|
|
|
|
this.ping();
|
|
|
|
}, WS_HEARTBEAT_OPTS.interval);
|
|
|
|
|
|
|
|
this.ping();
|
|
|
|
}
|
|
|
|
|
|
|
|
clearWSHeartbeat() {
|
2023-02-24 00:21:20 +08:00
|
|
|
if (this.ws?.wsHeartbeat) {
|
|
|
|
clearInterval(this.ws.wsHeartbeat);
|
|
|
|
this.ws.wsHeartbeat = null;
|
2023-02-15 04:54:26 +08:00
|
|
|
}
|
2018-02-17 03:18:53 +08:00
|
|
|
}
|
|
|
|
|
2019-11-28 21:13:06 +08:00
|
|
|
onWsMessage(message) {
|
2023-02-15 04:54:26 +08:00
|
|
|
this._updateLastMsgTime();
|
2019-11-28 21:13:06 +08:00
|
|
|
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() {
|
2024-01-27 00:21:58 +08:00
|
|
|
const { sendUserUnshareWebcam } = this.props;
|
2021-01-21 04:25:02 +08:00
|
|
|
logger.info({
|
2019-11-28 21:13:06 +08:00
|
|
|
logCode: 'video_provider_onwsclose',
|
|
|
|
}, 'Multiple video provider websocket connection closed.');
|
2018-02-17 03:18:53 +08:00
|
|
|
|
2023-02-15 04:54:26 +08:00
|
|
|
this.clearWSHeartbeat();
|
2024-01-27 00:21:58 +08:00
|
|
|
VideoService.exitVideo(sendUserUnshareWebcam);
|
2023-02-24 00:21:20 +08:00
|
|
|
// Media is currently tied to signaling state - so if signaling shuts down,
|
|
|
|
// media will shut down server-side. This cleans up our local state faster
|
|
|
|
// and notify the state change as failed so the UI rolls back to the placeholder
|
|
|
|
// avatar UI in the camera container
|
|
|
|
Object.keys(this.webRtcPeers).forEach((stream) => {
|
|
|
|
if (this.stopWebRTCPeer(stream, false)) {
|
|
|
|
notifyStreamStateChange(stream, 'failed');
|
|
|
|
}
|
|
|
|
});
|
2021-09-01 20:23:39 +08:00
|
|
|
this.setState({ socketOpen: false });
|
2023-02-24 00:21:20 +08:00
|
|
|
|
2024-05-29 21:26:11 +08:00
|
|
|
const {
|
|
|
|
maxRetries: WS_MAX_RETRIES = 5,
|
|
|
|
} = window.meetingClientSettings.public.kurento.cameraWsOptions;
|
|
|
|
|
2023-02-24 00:21:20 +08:00
|
|
|
if (this.ws && this.ws.retryCount >= WS_MAX_RETRIES) {
|
|
|
|
this.terminateWs();
|
|
|
|
}
|
2018-02-17 03:18:53 +08:00
|
|
|
}
|
|
|
|
|
2018-12-18 06:19:26 +08:00
|
|
|
onWsOpen() {
|
2021-01-21 04:25:02 +08:00
|
|
|
logger.info({
|
2019-11-28 21:13:06 +08:00
|
|
|
logCode: 'video_provider_onwsopen',
|
|
|
|
}, 'Multiple video provider websocket connection opened.');
|
2018-12-18 06:19:26 +08:00
|
|
|
|
2023-02-15 04:54:26 +08:00
|
|
|
this._updateLastMsgTime();
|
|
|
|
this.setupWSHeartbeat();
|
2021-09-01 20:23:39 +08:00
|
|
|
this.setState({ socketOpen: true });
|
2019-11-28 21:13:06 +08:00
|
|
|
// Resend queued messages that happened when socket was not connected
|
2023-02-24 00:21:20 +08:00
|
|
|
Object.entries(this.wsQueues).forEach(([stream, queue]) => {
|
|
|
|
if (this.webRtcPeers[stream]) {
|
|
|
|
// Peer - send enqueued
|
|
|
|
while (queue.length > 0) {
|
|
|
|
this.sendMessage(queue.pop());
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// No peer - delete queue
|
|
|
|
this.wsQueues[stream] = null;
|
|
|
|
}
|
|
|
|
});
|
2018-05-03 21:57:03 +08:00
|
|
|
}
|
|
|
|
|
2021-12-09 06:53:12 +08:00
|
|
|
findAllPrivilegedStreams () {
|
|
|
|
const { streams } = this.props;
|
2022-03-14 22:58:53 +08:00
|
|
|
// Privileged streams are: floor holders, pinned users
|
2021-12-21 03:02:59 +08:00
|
|
|
return streams.filter(stream => stream.floor || stream.pin);
|
2021-12-09 06:53:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
updateQualityThresholds(numberOfPublishers) {
|
2021-06-22 04:59:55 +08:00
|
|
|
const { threshold, profile } = VideoService.getThreshold(numberOfPublishers);
|
2024-05-29 21:26:11 +08:00
|
|
|
|
|
|
|
const {
|
|
|
|
privilegedStreams: CAMERA_QUALITY_THR_PRIVILEGED = true,
|
|
|
|
} = window.meetingClientSettings.public.kurento.cameraQualityThresholds;
|
|
|
|
|
2021-06-22 04:59:55 +08:00
|
|
|
if (profile) {
|
2021-12-09 06:53:12 +08:00
|
|
|
const privilegedStreams = this.findAllPrivilegedStreams();
|
|
|
|
Object.values(this.webRtcPeers)
|
2021-06-22 04:59:55 +08:00
|
|
|
.filter(peer => peer.isPublisher)
|
|
|
|
.forEach((peer) => {
|
2022-03-14 22:58:53 +08:00
|
|
|
// Conditions which make camera revert their original profile
|
2021-12-09 06:53:12 +08:00
|
|
|
// 1) Threshold 0 means original profile/inactive constraint
|
2022-03-14 22:58:53 +08:00
|
|
|
// 2) Privileged streams
|
|
|
|
const exempt = threshold === 0
|
|
|
|
|| (CAMERA_QUALITY_THR_PRIVILEGED && privilegedStreams.some(vs => vs.stream === peer.stream))
|
2021-12-09 06:53:12 +08:00
|
|
|
const profileToApply = exempt ? peer.originalProfileId : profile;
|
2021-06-22 04:59:55 +08:00
|
|
|
VideoService.applyCameraProfile(peer, profileToApply);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-19 01:00:47 +08:00
|
|
|
getStreamsToConnectAndDisconnect(streams) {
|
2023-07-26 00:20:45 +08:00
|
|
|
const streamsCameraIds = streams.filter(s => !s?.isGridItem).map(s => s.stream);
|
2019-11-28 21:13:06 +08:00
|
|
|
const streamsConnected = Object.keys(this.webRtcPeers);
|
2018-02-17 03:18:53 +08:00
|
|
|
|
2021-06-22 04:59:55 +08:00
|
|
|
const streamsToConnect = streamsCameraIds.filter(stream => {
|
|
|
|
return !streamsConnected.includes(stream);
|
|
|
|
});
|
2019-11-29 04:28:41 +08:00
|
|
|
|
2021-06-22 04:59:55 +08:00
|
|
|
const streamsToDisconnect = streamsConnected.filter(stream => {
|
|
|
|
return !streamsCameraIds.includes(stream);
|
|
|
|
});
|
2018-02-17 03:18:53 +08:00
|
|
|
|
2020-08-19 01:00:47 +08:00
|
|
|
return [streamsToConnect, streamsToDisconnect];
|
|
|
|
}
|
|
|
|
|
2021-06-22 04:59:55 +08:00
|
|
|
connectStreams(streamsToConnect) {
|
|
|
|
streamsToConnect.forEach((stream) => {
|
|
|
|
const isLocal = VideoService.isLocalStream(stream);
|
|
|
|
this.createWebRTCPeer(stream, isLocal);
|
|
|
|
});
|
|
|
|
}
|
2021-06-22 04:16:59 +08:00
|
|
|
|
2021-06-22 04:59:55 +08:00
|
|
|
disconnectStreams(streamsToDisconnect) {
|
|
|
|
streamsToDisconnect.forEach(stream => this.stopWebRTCPeer(stream, false));
|
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);
|
|
|
|
|
2024-05-29 21:26:11 +08:00
|
|
|
const {
|
|
|
|
enabled: CAMERA_QUALITY_THRESHOLDS_ENABLED = true,
|
|
|
|
} = window.meetingClientSettings.public.kurento.cameraQualityThresholds;
|
|
|
|
|
2020-09-17 22:37:28 +08:00
|
|
|
if (shouldDebounce) {
|
2020-08-20 23:43:02 +08:00
|
|
|
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) {
|
2021-12-09 06:53:12 +08:00
|
|
|
this.updateQualityThresholds(this.props.totalNumberOfStreams);
|
2020-05-27 01:05:04 +08:00
|
|
|
}
|
2018-12-18 06:19:26 +08:00
|
|
|
}
|
2018-02-17 03:18:53 +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);
|
2023-02-15 04:54:26 +08:00
|
|
|
try {
|
|
|
|
ws.send(jsonMessage);
|
|
|
|
} catch (error) {
|
|
|
|
logger.error({
|
|
|
|
logCode: 'video_provider_ws_send_error',
|
|
|
|
extraInfo: {
|
|
|
|
errorMessage: error.message || 'Unknown',
|
|
|
|
errorCode: error.code,
|
|
|
|
},
|
|
|
|
}, 'Camera request failed to be sent to SFU');
|
|
|
|
}
|
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
|
2023-02-24 00:21:20 +08:00
|
|
|
const { cameraId } = message;
|
|
|
|
if (cameraId) {
|
|
|
|
if (this.wsQueues[cameraId] == null) this.wsQueues[cameraId] = [];
|
|
|
|
this.wsQueues[cameraId].push(message);
|
|
|
|
}
|
2018-02-17 03:18:53 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
connectedToMediaServer() {
|
2023-02-24 00:21:20 +08:00
|
|
|
return this.ws && this.ws.readyState === ReconnectingWebSocket.OPEN;
|
2018-02-17 03:18:53 +08:00
|
|
|
}
|
|
|
|
|
2021-04-09 10:58:13 +08:00
|
|
|
processOutboundIceQueue(peer, role, stream) {
|
|
|
|
const queue = this.outboundIceQueues[stream];
|
2019-11-28 23:27:34 +08:00
|
|
|
while (queue && queue.length) {
|
|
|
|
const candidate = queue.shift();
|
2021-04-09 10:58:13 +08:00
|
|
|
this.sendIceCandidateToSFU(peer, role, candidate, stream);
|
2019-11-28 23:27:34 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-11 01:31:43 +08:00
|
|
|
sendLocalAnswer (peer, stream, answer) {
|
|
|
|
const message = {
|
|
|
|
id: 'subscriberAnswer',
|
|
|
|
type: 'video',
|
|
|
|
role: VideoService.getRole(peer.isPublisher),
|
|
|
|
cameraId: stream,
|
|
|
|
answer,
|
|
|
|
};
|
|
|
|
|
|
|
|
this.sendMessage(message);
|
|
|
|
}
|
|
|
|
|
2018-02-17 03:18:53 +08:00
|
|
|
startResponse(message) {
|
2021-04-09 10:58:13 +08:00
|
|
|
const { cameraId: stream, role } = message;
|
|
|
|
const peer = this.webRtcPeers[stream];
|
2018-02-17 03:18:53 +08:00
|
|
|
|
2021-01-21 04:25:02 +08:00
|
|
|
logger.debug({
|
2019-07-19 04:44:21 +08:00
|
|
|
logCode: 'video_provider_start_response_success',
|
2021-04-09 10:58:13 +08:00
|
|
|
extraInfo: { cameraId: stream, role },
|
2021-01-21 04:25:02 +08:00
|
|
|
}, `Camera start request accepted by SFU. Role: ${role}`);
|
2018-02-17 03:18:53 +08:00
|
|
|
|
2018-07-12 00:32:19 +08:00
|
|
|
if (peer) {
|
2021-08-11 01:31:43 +08:00
|
|
|
const processorFunc = peer.isPublisher
|
|
|
|
? peer.processAnswer.bind(peer)
|
|
|
|
: peer.processOffer.bind(peer);
|
|
|
|
|
2022-07-12 21:34:03 +08:00
|
|
|
processorFunc(message.sdpAnswer).then((answer) => {
|
2021-08-11 01:31:43 +08:00
|
|
|
if (answer) this.sendLocalAnswer(peer, stream, answer);
|
|
|
|
|
2018-12-22 03:47:15 +08:00
|
|
|
peer.didSDPAnswered = true;
|
2021-04-09 10:58:13 +08:00
|
|
|
this.processOutboundIceQueue(peer, role, stream);
|
|
|
|
VideoService.processInboundIceQueue(peer, stream);
|
2022-07-12 21:34:03 +08:00
|
|
|
}).catch((error) => {
|
|
|
|
logger.error({
|
|
|
|
logCode: 'video_provider_peerconnection_process_error',
|
|
|
|
extraInfo: {
|
|
|
|
cameraId: stream,
|
|
|
|
role,
|
|
|
|
errorMessage: error.message,
|
|
|
|
errorCode: error.code,
|
|
|
|
},
|
|
|
|
}, 'Camera answer processing failed');
|
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',
|
2021-04-09 10:58:13 +08:00
|
|
|
extraInfo: { cameraId: stream, role },
|
2021-01-21 04:25:02 +08:00
|
|
|
}, 'No peer on SFU camera start response handler');
|
2018-07-12 00:32:19 +08:00
|
|
|
}
|
2018-02-17 03:18:53 +08:00
|
|
|
}
|
|
|
|
|
2021-06-22 04:59:55 +08:00
|
|
|
handleIceCandidate(message) {
|
|
|
|
const { cameraId: stream, candidate } = message;
|
|
|
|
const peer = this.webRtcPeers[stream];
|
|
|
|
|
|
|
|
if (peer) {
|
|
|
|
if (peer.didSDPAnswered) {
|
|
|
|
VideoService.addCandidateToPeer(peer, candidate, stream);
|
|
|
|
} else {
|
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
logger.warn({
|
|
|
|
logCode: 'video_provider_addicecandidate_no_peer',
|
|
|
|
extraInfo: { cameraId: stream },
|
|
|
|
}, 'Trailing camera ICE candidate, discarded');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-22 04:16:59 +08:00
|
|
|
clearRestartTimers(stream) {
|
2021-04-09 10:58:13 +08:00
|
|
|
if (this.restartTimeout[stream]) {
|
|
|
|
clearTimeout(this.restartTimeout[stream]);
|
|
|
|
delete this.restartTimeout[stream];
|
2021-01-21 03:03:12 +08:00
|
|
|
}
|
|
|
|
|
2021-04-09 10:58:13 +08:00
|
|
|
if (this.restartTimer[stream]) {
|
|
|
|
delete this.restartTimer[stream];
|
2021-01-21 03:03:12 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-09 10:58:13 +08:00
|
|
|
stopWebRTCPeer(stream, restarting = false) {
|
|
|
|
const isLocal = VideoService.isLocalStream(stream);
|
2024-01-27 00:21:58 +08:00
|
|
|
const { sendUserUnshareWebcam } = this.props;
|
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
|
2021-04-09 10:58:13 +08:00
|
|
|
const peer = this.webRtcPeers[stream];
|
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) {
|
2024-01-27 00:21:58 +08:00
|
|
|
VideoService.stopVideo(stream, sendUserUnshareWebcam);
|
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',
|
2021-04-09 10:58:13 +08:00
|
|
|
extraInfo: { role, cameraId: stream, restarting },
|
2021-01-21 04:25:02 +08:00
|
|
|
}, `Camera feed stop requested. Role ${role}, 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',
|
2021-04-09 10:58:13 +08:00
|
|
|
cameraId: stream,
|
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) {
|
2021-04-09 10:58:13 +08:00
|
|
|
this.clearRestartTimers(stream);
|
2018-12-18 01:45:57 +08:00
|
|
|
}
|
2018-05-23 04:44:30 +08:00
|
|
|
|
2023-02-24 00:21:20 +08:00
|
|
|
return this.destroyWebRTCPeer(stream);
|
2018-04-19 02:16:26 +08:00
|
|
|
}
|
2018-02-17 03:18:53 +08:00
|
|
|
|
2021-04-09 10:58:13 +08:00
|
|
|
destroyWebRTCPeer(stream) {
|
2023-02-24 00:21:20 +08:00
|
|
|
let stopped = false;
|
2021-04-09 10:58:13 +08:00
|
|
|
const peer = this.webRtcPeers[stream];
|
|
|
|
const isLocal = VideoService.isLocalStream(stream);
|
2021-01-28 02:31:17 +08:00
|
|
|
const role = VideoService.getRole(isLocal);
|
|
|
|
|
2019-11-28 21:13:06 +08:00
|
|
|
if (peer) {
|
2021-07-20 04:19:06 +08:00
|
|
|
if (peer && peer.bbbVideoStream) {
|
2023-04-06 00:31:59 +08:00
|
|
|
if (typeof peer.inactivationHandler === 'function') {
|
|
|
|
peer.bbbVideoStream.removeListener('inactive', peer.inactivationHandler);
|
|
|
|
}
|
2021-07-20 04:19:06 +08:00
|
|
|
peer.bbbVideoStream.stop();
|
|
|
|
}
|
|
|
|
|
2019-11-28 21:13:06 +08:00
|
|
|
if (typeof peer.dispose === 'function') {
|
|
|
|
peer.dispose();
|
2019-05-25 03:55:35 +08:00
|
|
|
}
|
2021-07-20 04:19:06 +08:00
|
|
|
|
2021-04-09 10:58:13 +08:00
|
|
|
delete this.webRtcPeers[stream];
|
2023-02-24 00:21:20 +08:00
|
|
|
stopped = true;
|
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',
|
2021-04-09 10:58:13 +08:00
|
|
|
extraInfo: { cameraId: stream, role },
|
2021-01-21 04:25:02 +08:00
|
|
|
}, 'Trailing camera destroy request.');
|
2019-07-19 04:44:21 +08:00
|
|
|
}
|
2023-02-24 00:21:20 +08:00
|
|
|
|
|
|
|
delete this.outboundIceQueues[stream];
|
|
|
|
delete this.wsQueues[stream];
|
|
|
|
|
|
|
|
return stopped;
|
2019-07-19 04:44:21 +08:00
|
|
|
}
|
|
|
|
|
2022-07-12 21:34:03 +08:00
|
|
|
_createPublisher(stream, peerOptions) {
|
2021-08-11 01:31:43 +08:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
try {
|
|
|
|
const { id: profileId } = VideoService.getCameraProfile();
|
|
|
|
let bbbVideoStream = VideoService.getPreloadedStream();
|
|
|
|
|
|
|
|
if (bbbVideoStream) {
|
|
|
|
peerOptions.videoStream = bbbVideoStream.mediaStream;
|
|
|
|
}
|
|
|
|
|
2022-07-12 21:34:03 +08:00
|
|
|
const peer = new WebRtcPeer('sendonly', peerOptions);
|
2022-10-27 00:40:42 +08:00
|
|
|
peer.bbbVideoStream = bbbVideoStream;
|
2022-07-12 21:34:03 +08:00
|
|
|
this.webRtcPeers[stream] = peer;
|
|
|
|
peer.stream = stream;
|
|
|
|
peer.started = false;
|
|
|
|
peer.didSDPAnswered = false;
|
|
|
|
peer.inboundIceQueue = [];
|
|
|
|
peer.isPublisher = true;
|
|
|
|
peer.originalProfileId = profileId;
|
|
|
|
peer.currentProfileId = profileId;
|
|
|
|
peer.start();
|
2022-07-16 01:07:15 +08:00
|
|
|
peer.generateOffer().then((offer) => {
|
|
|
|
// Store the media stream if necessary. The scenario here is one where
|
|
|
|
// there is no preloaded stream stored.
|
2022-10-27 00:40:42 +08:00
|
|
|
if (peer.bbbVideoStream == null) {
|
2022-07-16 01:07:15 +08:00
|
|
|
bbbVideoStream = new BBBVideoStream(peer.getLocalStream());
|
|
|
|
VideoPreviewService.storeStream(
|
|
|
|
MediaStreamUtils.extractDeviceIdFromStream(
|
|
|
|
bbbVideoStream.mediaStream,
|
|
|
|
'video',
|
|
|
|
),
|
|
|
|
bbbVideoStream,
|
|
|
|
);
|
2022-07-12 21:34:03 +08:00
|
|
|
}
|
|
|
|
|
2022-07-16 01:07:15 +08:00
|
|
|
peer.bbbVideoStream = bbbVideoStream;
|
|
|
|
bbbVideoStream.on('streamSwapped', ({ newStream }) => {
|
|
|
|
if (newStream && newStream instanceof MediaStream) {
|
|
|
|
this.replacePCVideoTracks(stream, newStream);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
peer.inactivationHandler = () => this._handleLocalStreamInactive(stream);
|
|
|
|
bbbVideoStream.once('inactive', peer.inactivationHandler);
|
|
|
|
resolve(offer);
|
|
|
|
}).catch(reject);
|
2021-08-11 01:31:43 +08:00
|
|
|
} catch (error) {
|
2022-07-12 21:34:03 +08:00
|
|
|
reject(error);
|
2021-08-11 01:31:43 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-07-12 21:34:03 +08:00
|
|
|
_createSubscriber(stream, peerOptions) {
|
2021-08-11 01:31:43 +08:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
try {
|
2022-07-12 21:34:03 +08:00
|
|
|
const peer = new WebRtcPeer('recvonly', peerOptions);
|
|
|
|
this.webRtcPeers[stream] = peer;
|
|
|
|
peer.stream = stream;
|
|
|
|
peer.started = false;
|
|
|
|
peer.didSDPAnswered = false;
|
|
|
|
peer.inboundIceQueue = [];
|
|
|
|
peer.isPublisher = false;
|
|
|
|
peer.start();
|
|
|
|
resolve();
|
2021-08-11 01:31:43 +08:00
|
|
|
} catch (error) {
|
2022-07-12 21:34:03 +08:00
|
|
|
reject(error);
|
2021-08-11 01:31:43 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-04-09 10:58:13 +08:00
|
|
|
async createWebRTCPeer(stream, isLocal) {
|
2024-05-29 21:26:11 +08:00
|
|
|
const {
|
|
|
|
webcam: NETWORK_PRIORITY,
|
|
|
|
} = window.meetingClientSettings.public.media.networkPriorities || {};
|
|
|
|
|
|
|
|
const TRACE_LOGS = window.meetingClientSettings.public.kurento.traceLogs;
|
|
|
|
const GATHERING_TIMEOUT = window.meetingClientSettings.public.kurento.gatheringTimeout;
|
|
|
|
|
2018-07-10 05:29:27 +08:00
|
|
|
let iceServers = [];
|
2021-01-28 02:31:17 +08:00
|
|
|
const role = VideoService.getRole(isLocal);
|
2021-08-11 01:31:43 +08:00
|
|
|
const peerBuilderFunc = isLocal
|
|
|
|
? this._createPublisher.bind(this)
|
|
|
|
: this._createSubscriber.bind(this);
|
2018-02-17 03:18:53 +08:00
|
|
|
|
2018-11-10 00:25:48 +08:00
|
|
|
// Check if the peer is already being processed
|
2021-04-09 10:58:13 +08:00
|
|
|
if (this.webRtcPeers[stream]) {
|
2018-11-10 00:25:48 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-04-09 10:58:13 +08:00
|
|
|
this.webRtcPeers[stream] = {};
|
2021-08-11 01:31:43 +08:00
|
|
|
this.outboundIceQueues[stream] = [];
|
2022-07-12 21:34:03 +08:00
|
|
|
const { constraints, bitrate } = VideoService.getCameraProfile();
|
2021-04-03 11:06:39 +08:00
|
|
|
const peerOptions = {
|
|
|
|
mediaConstraints: {
|
|
|
|
audio: false,
|
|
|
|
video: constraints,
|
|
|
|
},
|
|
|
|
onicecandidate: this._getOnIceCandidateCallback(stream, isLocal),
|
2021-12-01 04:31:09 +08:00
|
|
|
configuration: {
|
2022-07-12 21:34:03 +08:00
|
|
|
},
|
|
|
|
trace: TRACE_LOGS,
|
2022-08-16 05:24:05 +08:00
|
|
|
networkPriorities: NETWORK_PRIORITY ? { video: NETWORK_PRIORITY } : undefined,
|
2023-04-06 00:22:38 +08:00
|
|
|
gatheringTimeout: GATHERING_TIMEOUT,
|
2021-04-03 11:06:39 +08:00
|
|
|
};
|
2018-11-10 00:25:48 +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: {
|
2021-04-09 10:58:13 +08:00
|
|
|
cameraId: stream,
|
2021-01-28 02:31:17 +08:00
|
|
|
role,
|
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 {
|
2022-12-19 06:09:30 +08:00
|
|
|
// we need to set iceTransportPolicy after `fetchWebRTCMappedStunTurnServers`
|
|
|
|
// because `shouldForceRelay` uses the information from the stun API
|
|
|
|
peerOptions.configuration.iceTransportPolicy = shouldForceRelay() ? 'relay' : undefined;
|
2018-07-10 05:29:27 +08:00
|
|
|
if (iceServers.length > 0) {
|
2019-07-19 04:44:21 +08:00
|
|
|
peerOptions.configuration.iceServers = iceServers;
|
2018-07-10 05:29:27 +08:00
|
|
|
}
|
2018-04-12 02:50:14 +08:00
|
|
|
|
2021-08-11 01:31:43 +08:00
|
|
|
peerBuilderFunc(stream, peerOptions).then((offer) => {
|
2021-09-02 03:13:22 +08:00
|
|
|
if (!this._isMounted) {
|
|
|
|
return this.stopWebRTCPeer(stream, false);
|
|
|
|
}
|
2021-04-09 10:58:13 +08:00
|
|
|
const peer = this.webRtcPeers[stream];
|
2019-11-28 21:13:06 +08:00
|
|
|
|
2021-08-11 01:31:43 +08:00
|
|
|
if (peer && peer.peerConnection) {
|
|
|
|
const conn = peer.peerConnection;
|
|
|
|
conn.onconnectionstatechange = () => {
|
|
|
|
this._handleIceConnectionStateChange(stream, isLocal);
|
|
|
|
};
|
2021-07-20 04:19:06 +08:00
|
|
|
}
|
|
|
|
|
2021-08-11 01:31:43 +08:00
|
|
|
const message = {
|
|
|
|
id: 'start',
|
|
|
|
type: 'video',
|
|
|
|
cameraId: stream,
|
|
|
|
role,
|
|
|
|
sdpOffer: offer,
|
|
|
|
bitrate,
|
|
|
|
record: VideoService.getRecord(),
|
2021-04-27 03:05:53 +08:00
|
|
|
mediaServer: VideoService.getMediaServerAdapter(),
|
2021-08-11 01:31:43 +08:00
|
|
|
};
|
2018-07-10 05:29:27 +08:00
|
|
|
|
2021-08-11 01:31:43 +08:00
|
|
|
logger.info({
|
|
|
|
logCode: 'video_provider_sfu_request_start_camera',
|
|
|
|
extraInfo: {
|
2021-04-09 10:58:13 +08:00
|
|
|
cameraId: stream,
|
2021-01-21 04:25:02 +08:00
|
|
|
role,
|
2021-08-11 01:31:43 +08:00
|
|
|
},
|
|
|
|
}, `Camera offer generated. Role: ${role}`);
|
2019-07-19 04:44:21 +08:00
|
|
|
|
2021-08-11 01:31:43 +08:00
|
|
|
this.setReconnectionTimeout(stream, isLocal, false);
|
2023-02-24 00:21:20 +08:00
|
|
|
this.sendMessage(message);
|
2019-07-24 06:24:31 +08:00
|
|
|
|
2021-08-11 01:31:43 +08:00
|
|
|
return;
|
|
|
|
}).catch(error => {
|
|
|
|
return this._onWebRTCError(error, stream, isLocal);
|
2018-02-17 03:18:53 +08:00
|
|
|
});
|
2018-07-10 05:29:27 +08:00
|
|
|
}
|
2018-02-17 03:18:53 +08:00
|
|
|
}
|
|
|
|
|
2021-04-09 10:58:13 +08:00
|
|
|
_getWebRTCStartTimeout(stream, isLocal) {
|
2019-11-28 21:13:06 +08:00
|
|
|
const { intl } = this.props;
|
2018-05-23 04:44:30 +08:00
|
|
|
|
2024-05-29 21:26:11 +08:00
|
|
|
const {
|
|
|
|
maxTimeout: MAX_CAMERA_SHARE_FAILED_WAIT_TIME = 60000,
|
|
|
|
} = window.meetingClientSettings.public.kurento.cameraTimeouts || {};
|
|
|
|
|
2018-05-23 04:44:30 +08:00
|
|
|
return () => {
|
2021-01-28 02:31:17 +08:00
|
|
|
const role = VideoService.getRole(isLocal);
|
2021-01-21 03:03:12 +08:00
|
|
|
if (!isLocal) {
|
|
|
|
// Peer that timed out is a subscriber/viewer
|
|
|
|
// 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
|
|
|
// Create new reconnect interval time
|
2021-04-09 10:58:13 +08:00
|
|
|
const oldReconnectTimer = this.restartTimer[stream];
|
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
|
|
|
);
|
2021-04-09 10:58:13 +08:00
|
|
|
this.restartTimer[stream] = 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
|
2021-04-09 10:58:13 +08:00
|
|
|
if (this.restartTimeout[stream]) {
|
|
|
|
delete this.restartTimeout[stream];
|
2019-11-29 01:44:39 +08:00
|
|
|
}
|
|
|
|
|
2019-07-19 04:44:21 +08:00
|
|
|
logger.error({
|
|
|
|
logCode: 'video_provider_camera_view_timeout',
|
|
|
|
extraInfo: {
|
2021-04-09 10:58:13 +08:00
|
|
|
cameraId: stream,
|
2021-01-28 02:31:17 +08:00
|
|
|
role,
|
2019-11-29 01:44:39 +08:00
|
|
|
oldReconnectTimer,
|
|
|
|
newReconnectTimer,
|
2019-07-24 06:24:31 +08:00
|
|
|
},
|
2021-01-21 04:25:02 +08:00
|
|
|
}, 'Camera VIEWER failed. Reconnecting.');
|
2019-11-28 21:13:06 +08:00
|
|
|
|
2021-04-09 10:58:13 +08:00
|
|
|
this.reconnect(stream, isLocal);
|
2021-01-21 03:03:12 +08:00
|
|
|
} else {
|
|
|
|
// Peer that timed out is a sharer/publisher, clean it up, stop.
|
|
|
|
logger.error({
|
|
|
|
logCode: 'video_provider_camera_share_timeout',
|
2021-01-28 02:31:17 +08:00
|
|
|
extraInfo: {
|
2021-04-09 10:58:13 +08:00
|
|
|
cameraId: stream,
|
2021-01-28 02:31:17 +08:00
|
|
|
role,
|
|
|
|
},
|
2021-01-21 04:25:02 +08:00
|
|
|
}, 'Camera SHARER failed.');
|
2021-01-21 03:03:12 +08:00
|
|
|
VideoService.notify(intl.formatMessage(intlClientErrors.mediaFlowTimeout));
|
2021-04-09 10:58:13 +08:00
|
|
|
this.stopWebRTCPeer(stream, false);
|
2018-05-23 04:44:30 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-04-09 10:58:13 +08:00
|
|
|
_onWebRTCError(error, stream, isLocal) {
|
2023-02-24 00:21:20 +08:00
|
|
|
const { intl, streams } = this.props;
|
2022-07-13 05:44:20 +08:00
|
|
|
const { name: errorName, message: errorMessage } = error;
|
|
|
|
const errorLocale = intlClientErrors[errorName]
|
|
|
|
|| intlClientErrors[errorMessage]
|
|
|
|
|| intlSFUErrors[error];
|
2018-12-22 05:25:47 +08:00
|
|
|
|
2021-01-21 03:03:12 +08:00
|
|
|
logger.error({
|
|
|
|
logCode: 'video_provider_webrtc_peer_error',
|
|
|
|
extraInfo: {
|
2021-04-09 10:58:13 +08:00
|
|
|
cameraId: stream,
|
2021-01-28 02:31:17 +08:00
|
|
|
role: VideoService.getRole(isLocal),
|
2021-01-21 03:03:12 +08:00
|
|
|
errorName: error.name,
|
2021-01-21 04:25:02 +08:00
|
|
|
errorMessage: error.message,
|
2021-01-21 03:03:12 +08:00
|
|
|
},
|
2021-01-21 04:25:02 +08:00
|
|
|
}, 'Camera peer failed');
|
2018-05-23 04:44:30 +08:00
|
|
|
|
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
|
2021-01-21 03:03:12 +08:00
|
|
|
if (isLocal) {
|
2021-04-09 10:58:13 +08:00
|
|
|
this.stopWebRTCPeer(stream, false);
|
2022-07-13 05:44:20 +08:00
|
|
|
if (errorLocale) VideoService.notify(intl.formatMessage(errorLocale));
|
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.
|
2021-04-09 10:58:13 +08:00
|
|
|
const peer = this.webRtcPeers[stream];
|
2023-02-24 00:21:20 +08:00
|
|
|
const stillExists = streams.some(({ stream: streamId }) => streamId === stream);
|
|
|
|
|
|
|
|
if (stillExists) {
|
|
|
|
const isEstablishedConnection = peer && peer.started;
|
|
|
|
this.setReconnectionTimeout(stream, isLocal, isEstablishedConnection);
|
|
|
|
}
|
|
|
|
|
2021-01-21 03:03:12 +08:00
|
|
|
// second argument means it will only try to reconnect if
|
|
|
|
// it's a viewer instance (see stopWebRTCPeer restarting argument)
|
2023-02-24 00:21:20 +08:00
|
|
|
this.stopWebRTCPeer(stream, stillExists);
|
2019-11-29 01:44:39 +08:00
|
|
|
}
|
2021-01-21 03:03:12 +08:00
|
|
|
}
|
2019-11-29 01:44:39 +08:00
|
|
|
|
2021-04-09 10:58:13 +08:00
|
|
|
reconnect(stream, isLocal) {
|
|
|
|
this.stopWebRTCPeer(stream, true);
|
|
|
|
this.createWebRTCPeer(stream, isLocal);
|
2018-05-23 04:44:30 +08:00
|
|
|
}
|
|
|
|
|
2021-06-22 04:59:55 +08:00
|
|
|
setReconnectionTimeout(stream, isLocal, isEstablishedConnection) {
|
|
|
|
const peer = this.webRtcPeers[stream];
|
|
|
|
const shouldSetReconnectionTimeout = !this.restartTimeout[stream] && !isEstablishedConnection;
|
2019-11-29 01:44:39 +08:00
|
|
|
|
2021-06-22 04:59:55 +08:00
|
|
|
// This is an ongoing reconnection which succeeded in the first place but
|
|
|
|
// then failed mid call. Try to reconnect it right away. Clear the restart
|
|
|
|
// timers since we don't need them in this case.
|
|
|
|
if (isEstablishedConnection) {
|
|
|
|
this.clearRestartTimers(stream);
|
|
|
|
return this.reconnect(stream, isLocal);
|
|
|
|
}
|
2019-11-29 01:44:39 +08:00
|
|
|
|
2021-06-22 04:59:55 +08:00
|
|
|
// This is a reconnection timer for a peer that hasn't succeeded in the first
|
|
|
|
// place. Set reconnection timeouts with random intervals between them to try
|
|
|
|
// and reconnect without flooding the server
|
2024-05-29 21:26:11 +08:00
|
|
|
const {
|
|
|
|
baseTimeout: CAMERA_SHARE_FAILED_WAIT_TIME = 15000,
|
|
|
|
} = window.meetingClientSettings.public.kurento.cameraTimeouts || {};
|
|
|
|
|
2021-06-22 04:59:55 +08:00
|
|
|
if (shouldSetReconnectionTimeout) {
|
|
|
|
const newReconnectTimer = this.restartTimer[stream] || CAMERA_SHARE_FAILED_WAIT_TIME;
|
|
|
|
this.restartTimer[stream] = newReconnectTimer;
|
|
|
|
|
|
|
|
this.restartTimeout[stream] = setTimeout(
|
|
|
|
this._getWebRTCStartTimeout(stream, isLocal),
|
|
|
|
this.restartTimer[stream]
|
|
|
|
);
|
2019-11-29 01:44:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-09 10:58:13 +08:00
|
|
|
_getOnIceCandidateCallback(stream, isLocal) {
|
2024-05-29 21:26:11 +08:00
|
|
|
const SIGNAL_CANDIDATES = window.meetingClientSettings.public.kurento.signalCandidates;
|
|
|
|
|
2021-09-24 03:32:14 +08:00
|
|
|
if (SIGNAL_CANDIDATES) {
|
|
|
|
return (candidate) => {
|
|
|
|
const peer = this.webRtcPeers[stream];
|
|
|
|
const role = VideoService.getRole(isLocal);
|
2018-02-17 03:18:53 +08:00
|
|
|
|
2021-09-24 03:32:14 +08:00
|
|
|
if (peer && !peer.didSDPAnswered) {
|
|
|
|
this.outboundIceQueues[stream].push(candidate);
|
|
|
|
return;
|
|
|
|
}
|
2019-11-28 23:27:34 +08:00
|
|
|
|
2021-09-24 03:32:14 +08:00
|
|
|
this.sendIceCandidateToSFU(peer, role, candidate, stream);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
2019-11-28 23:27:34 +08:00
|
|
|
}
|
|
|
|
|
2021-04-09 10:58:13 +08:00
|
|
|
sendIceCandidateToSFU(peer, role, candidate, stream) {
|
2019-11-28 23:27:34 +08:00
|
|
|
const message = {
|
|
|
|
type: 'video',
|
|
|
|
role,
|
|
|
|
id: 'onIceCandidate',
|
|
|
|
candidate,
|
2021-04-09 10:58:13 +08:00
|
|
|
cameraId: stream,
|
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
|
|
|
|
2022-07-13 05:44:20 +08:00
|
|
|
_handleLocalStreamInactive(stream) {
|
|
|
|
const peer = this.webRtcPeers[stream];
|
|
|
|
const isLocal = VideoService.isLocalStream(stream);
|
|
|
|
const role = VideoService.getRole(isLocal);
|
|
|
|
|
|
|
|
// Peer == null: this is a trailing event.
|
|
|
|
// !isLocal: someone is misusing this handler - local streams only.
|
|
|
|
if (peer == null || !isLocal) return;
|
|
|
|
|
|
|
|
logger.error({
|
|
|
|
logCode: 'video_provider_local_stream_inactive',
|
|
|
|
extraInfo: {
|
|
|
|
cameraId: stream,
|
|
|
|
role,
|
|
|
|
},
|
|
|
|
}, 'Local camera stream stopped unexpectedly');
|
|
|
|
|
|
|
|
const error = new Error('inactiveError');
|
|
|
|
this._onWebRTCError(error, stream, isLocal);
|
|
|
|
}
|
|
|
|
|
2021-06-22 04:16:59 +08:00
|
|
|
_handleIceConnectionStateChange(stream, isLocal) {
|
2018-08-13 06:39:39 +08:00
|
|
|
const { intl } = this.props;
|
2021-04-09 10:58:13 +08:00
|
|
|
const peer = this.webRtcPeers[stream];
|
2021-01-28 02:31:17 +08:00
|
|
|
const role = VideoService.getRole(isLocal);
|
2018-08-13 06:39:39 +08:00
|
|
|
|
2021-01-21 03:03:12 +08:00
|
|
|
if (peer && peer.peerConnection) {
|
|
|
|
const pc = peer.peerConnection;
|
2021-06-22 04:59:55 +08:00
|
|
|
const connectionState = pc.connectionState;
|
2021-04-09 10:58:13 +08:00
|
|
|
notifyStreamStateChange(stream, connectionState);
|
2021-01-21 03:03:12 +08:00
|
|
|
|
|
|
|
if (connectionState === 'failed' || connectionState === 'closed') {
|
|
|
|
const error = new Error('iceConnectionStateError');
|
|
|
|
// prevent the same error from being detected multiple times
|
|
|
|
pc.onconnectionstatechange = null;
|
2021-01-21 04:25:02 +08:00
|
|
|
|
2021-01-21 03:03:12 +08:00
|
|
|
logger.error({
|
|
|
|
logCode: 'video_provider_ice_connection_failed_state',
|
|
|
|
extraInfo: {
|
2021-04-09 10:58:13 +08:00
|
|
|
cameraId: stream,
|
2021-01-21 03:03:12 +08:00
|
|
|
connectionState,
|
2021-01-21 04:25:02 +08:00
|
|
|
role,
|
2021-01-21 03:03:12 +08:00
|
|
|
},
|
2021-01-21 04:25:02 +08:00
|
|
|
}, `Camera ICE connection state changed: ${connectionState}. Role: ${role}.`);
|
2019-11-28 21:13:06 +08:00
|
|
|
|
2021-04-09 10:58:13 +08:00
|
|
|
this._onWebRTCError(error, stream, isLocal);
|
2021-01-21 03:03:12 +08:00
|
|
|
}
|
|
|
|
} else {
|
2019-11-28 21:13:06 +08:00
|
|
|
logger.error({
|
2021-01-28 02:31:17 +08:00
|
|
|
logCode: 'video_provider_ice_connection_nopeer',
|
2021-04-09 10:58:13 +08:00
|
|
|
extraInfo: { cameraId: stream, role },
|
|
|
|
}, `No peer at ICE connection state handler. Camera: ${stream}. Role: ${role}`);
|
2021-01-21 03:03:12 +08:00
|
|
|
}
|
2018-08-13 06:39:39 +08:00
|
|
|
}
|
|
|
|
|
2021-07-20 04:19:06 +08:00
|
|
|
attach (peer, videoElement) {
|
|
|
|
if (peer && videoElement) {
|
|
|
|
const stream = peer.isPublisher ? peer.getLocalStream() : peer.getRemoteStream();
|
|
|
|
videoElement.pause();
|
|
|
|
videoElement.srcObject = stream;
|
|
|
|
videoElement.load();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
getVideoElement(streamId) {
|
|
|
|
return this.videoTags[streamId];
|
|
|
|
}
|
|
|
|
|
2021-04-09 10:58:13 +08:00
|
|
|
attachVideoStream(stream) {
|
2023-04-10 22:44:59 +08:00
|
|
|
const videoElement = this.getVideoElement(stream);
|
2021-04-09 10:58:13 +08:00
|
|
|
const isLocal = VideoService.isLocalStream(stream);
|
|
|
|
const peer = this.webRtcPeers[stream];
|
2018-03-20 01:52:39 +08:00
|
|
|
|
2023-04-10 22:44:59 +08:00
|
|
|
if (VideoProvider.shouldAttachVideoStream(peer, videoElement)) {
|
|
|
|
const pc = peer.peerConnection;
|
|
|
|
// Notify current stream state again on attachment since the
|
|
|
|
// video-list-item component may not have been mounted before the stream
|
|
|
|
// reached the connected state.
|
|
|
|
// This is necessary to ensure that the video element is properly
|
|
|
|
// hidden/shown when the stream is attached.
|
|
|
|
notifyStreamStateChange(stream, pc.connectionState);
|
|
|
|
this.attach(peer, videoElement);
|
2021-11-11 22:33:55 +08:00
|
|
|
|
|
|
|
if (isLocal) {
|
2022-10-27 00:40:42 +08:00
|
|
|
if (peer.bbbVideoStream == null) {
|
|
|
|
this.handleVirtualBgError(new TypeError('Undefined media stream'));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-04-05 05:09:35 +08:00
|
|
|
const deviceId = MediaStreamUtils.extractDeviceIdFromStream(
|
|
|
|
peer.bbbVideoStream.mediaStream,
|
|
|
|
'video',
|
|
|
|
);
|
2021-11-19 19:10:24 +08:00
|
|
|
const { type, name } = getSessionVirtualBackgroundInfo(deviceId);
|
|
|
|
|
|
|
|
this.restoreVirtualBackground(peer.bbbVideoStream, type, name).catch((error) => {
|
|
|
|
this.handleVirtualBgError(error, type, name);
|
|
|
|
});
|
2021-11-11 22:33:55 +08:00
|
|
|
}
|
2021-07-20 04:19:06 +08:00
|
|
|
}
|
2018-02-17 03:18:53 +08:00
|
|
|
}
|
|
|
|
|
2022-04-12 01:09:13 +08:00
|
|
|
startVirtualBackgroundByDrop(stream, type, name, data) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const peer = this.webRtcPeers[stream];
|
|
|
|
const { bbbVideoStream } = peer;
|
|
|
|
const video = this.getVideoElement(stream);
|
|
|
|
|
2023-05-17 01:46:57 +08:00
|
|
|
if (peer && video && video.srcObject) {
|
2022-04-12 01:09:13 +08:00
|
|
|
bbbVideoStream.startVirtualBackground(type, name, { file: data })
|
|
|
|
.then(resolve)
|
|
|
|
.catch(reject);
|
|
|
|
}
|
|
|
|
}).catch((error) => {
|
|
|
|
this.handleVirtualBgErrorByDropping(error, type, name);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
handleVirtualBgErrorByDropping(error, type, name) {
|
|
|
|
logger.error({
|
|
|
|
logCode: `video_provider_virtualbg_error`,
|
|
|
|
extraInfo: {
|
|
|
|
errorName: error.name,
|
|
|
|
errorMessage: error.message,
|
|
|
|
virtualBgType: type,
|
|
|
|
virtualBgName: name,
|
|
|
|
},
|
|
|
|
}, `Failed to start virtual background by dropping image: ${error.message}`);
|
|
|
|
}
|
|
|
|
|
2021-11-19 19:10:24 +08:00
|
|
|
restoreVirtualBackground(stream, type, name) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
if (type !== EFFECT_TYPES.NONE_TYPE) {
|
|
|
|
stream.startVirtualBackground(type, name).then(() => {
|
|
|
|
resolve();
|
|
|
|
}).catch((error) => {
|
|
|
|
reject(error);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
handleVirtualBgError(error, type, name) {
|
|
|
|
const { intl } = this.props;
|
|
|
|
logger.error({
|
|
|
|
logCode: `video_provider_virtualbg_error`,
|
|
|
|
extraInfo: {
|
|
|
|
errorName: error.name,
|
|
|
|
errorMessage: error.message,
|
|
|
|
virtualBgType: type,
|
|
|
|
virtualBgName: name,
|
|
|
|
},
|
|
|
|
}, `Failed to restore virtual background after reentering the room: ${error.message}`);
|
|
|
|
|
2022-10-27 00:54:00 +08:00
|
|
|
notify(intl.formatMessage(intlClientErrors.virtualBgGenericError), 'error', 'video');
|
2021-11-19 19:10:24 +08:00
|
|
|
}
|
|
|
|
|
2021-04-09 10:58:13 +08:00
|
|
|
createVideoTag(stream, video) {
|
|
|
|
const peer = this.webRtcPeers[stream];
|
|
|
|
this.videoTags[stream] = video;
|
2018-07-10 05:29:27 +08:00
|
|
|
|
2023-04-10 22:44:59 +08:00
|
|
|
if (peer && peer.stream === stream) {
|
2021-04-09 10:58:13 +08:00
|
|
|
this.attachVideoStream(stream);
|
2018-04-27 06:16:30 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-09 10:58:13 +08:00
|
|
|
destroyVideoTag(stream) {
|
2021-07-23 10:15:12 +08:00
|
|
|
const videoElement = this.videoTags[stream];
|
|
|
|
|
|
|
|
if (videoElement == null) return;
|
|
|
|
|
|
|
|
if (typeof videoElement.pause === 'function') {
|
|
|
|
videoElement.pause();
|
|
|
|
videoElement.srcObject = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
delete this.videoTags[stream];
|
2021-06-22 04:59:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
handlePlayStop(message) {
|
2021-07-08 23:10:22 +08:00
|
|
|
const { intl } = this.props;
|
2021-06-22 04:59:55 +08:00
|
|
|
const { cameraId: stream, role } = message;
|
|
|
|
|
|
|
|
logger.info({
|
|
|
|
logCode: 'video_provider_handle_play_stop',
|
|
|
|
extraInfo: {
|
|
|
|
cameraId: stream,
|
|
|
|
role,
|
|
|
|
},
|
|
|
|
}, `Received request from SFU to stop camera. Role: ${role}`);
|
2021-07-08 23:10:22 +08:00
|
|
|
|
|
|
|
VideoService.notify(intl.formatMessage(intlClientErrors.mediaTimedOutError));
|
2021-06-22 04:59:55 +08:00
|
|
|
this.stopWebRTCPeer(stream, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
handlePlayStart(message) {
|
|
|
|
const { cameraId: stream, role } = message;
|
|
|
|
const peer = this.webRtcPeers[stream];
|
2024-01-26 21:29:52 +08:00
|
|
|
const { playStart } = this.props;
|
2021-06-22 04:59:55 +08:00
|
|
|
|
|
|
|
if (peer) {
|
|
|
|
logger.info({
|
|
|
|
logCode: 'video_provider_handle_play_start_flowing',
|
|
|
|
extraInfo: {
|
|
|
|
cameraId: stream,
|
|
|
|
role,
|
|
|
|
},
|
|
|
|
}, `Camera media is flowing (server). Role: ${role}`);
|
|
|
|
|
|
|
|
peer.started = true;
|
|
|
|
|
2024-03-18 21:58:53 +08:00
|
|
|
// Clear camera shared timeout when camera successfully starts
|
2021-06-22 04:59:55 +08:00
|
|
|
this.clearRestartTimers(stream);
|
2023-04-10 22:44:59 +08:00
|
|
|
this.attachVideoStream(stream);
|
2021-06-22 04:59:55 +08:00
|
|
|
|
2024-01-26 21:29:52 +08:00
|
|
|
playStart(stream);
|
2021-06-22 04:59:55 +08:00
|
|
|
} else {
|
|
|
|
logger.warn({
|
|
|
|
logCode: 'video_provider_playstart_no_peer',
|
|
|
|
extraInfo: { cameraId: stream, role },
|
|
|
|
}, 'Trailing camera playStart response.');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
handleSFUError(message) {
|
2024-01-27 00:21:58 +08:00
|
|
|
const { intl, streams, sendUserUnshareWebcam } = this.props;
|
2021-06-22 04:59:55 +08:00
|
|
|
const { code, reason, streamId } = message;
|
|
|
|
const isLocal = VideoService.isLocalStream(streamId);
|
|
|
|
const role = VideoService.getRole(isLocal);
|
|
|
|
|
|
|
|
logger.error({
|
|
|
|
logCode: 'video_provider_handle_sfu_error',
|
|
|
|
extraInfo: {
|
|
|
|
errorCode: code,
|
|
|
|
errorReason: reason,
|
|
|
|
cameraId: streamId,
|
|
|
|
role,
|
|
|
|
},
|
|
|
|
}, `SFU returned an error. Code: ${code}, reason: ${reason}`);
|
|
|
|
|
|
|
|
if (isLocal) {
|
|
|
|
// The publisher instance received an error from the server. There's no reconnect,
|
|
|
|
// stop it.
|
|
|
|
VideoService.notify(intl.formatMessage(intlSFUErrors[code] || intlSFUErrors[2200]));
|
2024-01-27 00:21:58 +08:00
|
|
|
VideoService.stopVideo(streamId, sendUserUnshareWebcam);
|
2021-06-22 04:59:55 +08:00
|
|
|
} else {
|
2023-02-24 00:21:20 +08:00
|
|
|
const peer = this.webRtcPeers[streamId];
|
|
|
|
const stillExists = streams.some(({ stream }) => streamId === stream);
|
|
|
|
|
|
|
|
if (stillExists) {
|
|
|
|
const isEstablishedConnection = peer && peer.started;
|
|
|
|
this.setReconnectionTimeout(streamId, isLocal, isEstablishedConnection);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.stopWebRTCPeer(streamId, stillExists);
|
2021-06-22 04:59:55 +08:00
|
|
|
}
|
2018-02-17 03:18:53 +08:00
|
|
|
}
|
|
|
|
|
2023-02-24 00:21:20 +08:00
|
|
|
replacePCVideoTracks(streamId, mediaStream) {
|
2021-07-20 04:19:06 +08:00
|
|
|
const peer = this.webRtcPeers[streamId];
|
|
|
|
const videoElement = this.getVideoElement(streamId);
|
2021-07-20 21:41:14 +08:00
|
|
|
|
2021-07-20 04:19:06 +08:00
|
|
|
if (peer == null || mediaStream == null || videoElement == null) return;
|
2021-07-20 21:41:14 +08:00
|
|
|
|
2021-07-20 04:19:06 +08:00
|
|
|
const pc = peer.peerConnection;
|
|
|
|
const newTracks = mediaStream.getVideoTracks();
|
2021-07-20 21:41:14 +08:00
|
|
|
|
2021-07-20 04:19:06 +08:00
|
|
|
if (pc) {
|
2023-02-24 00:21:20 +08:00
|
|
|
const trackReplacers = pc.getSenders().map(async (sender, index) => {
|
|
|
|
if (sender.track == null || sender.track.kind !== 'video') return false;
|
|
|
|
const newTrack = newTracks[index];
|
|
|
|
if (newTrack == null) return false;
|
|
|
|
try {
|
|
|
|
await sender.replaceTrack(newTrack);
|
|
|
|
return true;
|
|
|
|
} catch (error) {
|
|
|
|
logger.warn({
|
|
|
|
logCode: 'video_provider_replacepc_error',
|
|
|
|
extraInfo: { errorMessage: error.message, cameraId: streamId },
|
|
|
|
}, `Failed to replace peer connection tracks: ${error.message}`);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
Promise.all(trackReplacers).then(() => {
|
|
|
|
this.attach(peer, videoElement);
|
|
|
|
});
|
2021-07-20 21:41:14 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-17 03:18:53 +08:00
|
|
|
render() {
|
2021-06-22 04:16:59 +08:00
|
|
|
const {
|
|
|
|
swapLayout,
|
|
|
|
currentVideoPageIndex,
|
|
|
|
streams,
|
|
|
|
cameraDockBounds,
|
2022-02-05 02:08:19 +08:00
|
|
|
focusedId,
|
|
|
|
handleVideoFocus,
|
2023-05-12 04:20:26 +08:00
|
|
|
isGridEnabled,
|
2024-02-23 23:01:53 +08:00
|
|
|
users,
|
2021-06-22 04:16:59 +08:00
|
|
|
} = 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
|
2021-06-22 04:16:59 +08:00
|
|
|
{...{
|
|
|
|
streams,
|
|
|
|
swapLayout,
|
|
|
|
currentVideoPageIndex,
|
|
|
|
cameraDockBounds,
|
2022-02-05 02:08:19 +08:00
|
|
|
focusedId,
|
|
|
|
handleVideoFocus,
|
2023-05-12 04:20:26 +08:00
|
|
|
isGridEnabled,
|
2024-02-23 23:01:53 +08:00
|
|
|
users,
|
2021-06-22 04:16:59 +08:00
|
|
|
}}
|
2021-01-21 03:03:12 +08:00
|
|
|
onVideoItemMount={this.createVideoTag}
|
|
|
|
onVideoItemUnmount={this.destroyVideoTag}
|
2022-04-12 01:09:13 +08:00
|
|
|
onVirtualBgDrop={this.startVirtualBackgroundByDrop}
|
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);
|