2017-09-09 01:00:00 +08:00
|
|
|
import Auth from '/imports/ui/services/auth';
|
2018-07-27 02:26:56 +08:00
|
|
|
import logger from '/imports/startup/client/logger';
|
2020-12-10 06:00:54 +08:00
|
|
|
import BridgeService from './service';
|
|
|
|
import ScreenshareBroker from '/imports/ui/services/bbb-webrtc-sfu/screenshare-broker';
|
2021-03-12 08:44:38 +08:00
|
|
|
import { setSharingScreen, screenShareEndAlert } from '/imports/ui/components/screenshare/service';
|
2021-01-12 23:24:01 +08:00
|
|
|
import { SCREENSHARING_ERRORS } from './errors';
|
2017-09-09 01:00:00 +08:00
|
|
|
|
2018-07-10 05:29:27 +08:00
|
|
|
const SFU_CONFIG = Meteor.settings.public.kurento;
|
|
|
|
const SFU_URL = SFU_CONFIG.wsUrl;
|
2020-12-10 06:00:54 +08:00
|
|
|
|
|
|
|
const BRIDGE_NAME = 'kurento'
|
2018-07-10 05:29:27 +08:00
|
|
|
const SCREENSHARE_VIDEO_TAG = 'screenshareVideo';
|
2020-12-10 06:00:54 +08:00
|
|
|
const SEND_ROLE = 'send';
|
|
|
|
const RECV_ROLE = 'recv';
|
2017-11-18 02:55:59 +08:00
|
|
|
|
2021-01-12 23:24:01 +08:00
|
|
|
// the error-code mapping is bridge specific; that's why it's not in the errors util
|
|
|
|
const ERROR_MAP = {
|
|
|
|
1301: SCREENSHARING_ERRORS.SIGNALLING_TRANSPORT_DISCONNECTED,
|
|
|
|
1302: SCREENSHARING_ERRORS.SIGNALLING_TRANSPORT_CONNECTION_FAILED,
|
|
|
|
1305: SCREENSHARING_ERRORS.PEER_NEGOTIATION_FAILED,
|
|
|
|
1307: SCREENSHARING_ERRORS.ICE_STATE_FAILED,
|
2020-12-10 06:00:54 +08:00
|
|
|
}
|
2017-09-09 01:00:00 +08:00
|
|
|
|
2020-12-10 06:00:54 +08:00
|
|
|
const mapErrorCode = (error) => {
|
|
|
|
const { errorCode } = error;
|
2021-01-12 23:24:01 +08:00
|
|
|
const mappedError = ERROR_MAP[errorCode];
|
|
|
|
|
|
|
|
if (errorCode == null || mappedError == null) return error;
|
|
|
|
error.errorCode = mappedError.errorCode;
|
|
|
|
error.errorMessage = mappedError.errorMessage;
|
|
|
|
error.message = mappedError.errorMessage;
|
|
|
|
|
2020-12-10 06:00:54 +08:00
|
|
|
return error;
|
|
|
|
}
|
2017-09-09 01:00:00 +08:00
|
|
|
|
2020-12-10 06:00:54 +08:00
|
|
|
export default class KurentoScreenshareBridge {
|
|
|
|
constructor() {
|
|
|
|
this.role;
|
|
|
|
this.broker;
|
|
|
|
this._gdmStream;
|
2020-12-16 01:45:20 +08:00
|
|
|
this.hasAudio = false;
|
2020-12-10 06:00:54 +08:00
|
|
|
this.connectionAttempts = 0;
|
|
|
|
this.reconnecting = false;
|
|
|
|
this.reconnectionTimeout;
|
|
|
|
this.restartIntervalMs = BridgeService.BASE_MEDIA_TIMEOUT;
|
|
|
|
}
|
2018-04-27 06:52:46 +08:00
|
|
|
|
2020-12-10 06:00:54 +08:00
|
|
|
get gdmStream() {
|
|
|
|
return this._gdmStream;
|
|
|
|
}
|
2019-09-11 21:17:23 +08:00
|
|
|
|
2020-12-10 06:00:54 +08:00
|
|
|
set gdmStream(stream) {
|
|
|
|
this._gdmStream = stream;
|
|
|
|
}
|
2018-07-10 05:29:27 +08:00
|
|
|
|
2020-12-10 06:00:54 +08:00
|
|
|
outboundStreamReconnect() {
|
|
|
|
const currentRestartIntervalMs = this.restartIntervalMs;
|
|
|
|
const stream = this.gdmStream;
|
|
|
|
|
|
|
|
logger.warn({
|
2020-12-16 01:45:20 +08:00
|
|
|
logCode: 'screenshare_presenter_reconnect',
|
|
|
|
extraInfo: {
|
|
|
|
reconnecting: this.reconnecting,
|
|
|
|
role: this.role,
|
|
|
|
bridge: BRIDGE_NAME
|
|
|
|
},
|
2020-12-10 06:00:54 +08:00
|
|
|
}, `Screenshare presenter session is reconnecting`);
|
2019-09-07 02:58:22 +08:00
|
|
|
|
2020-12-10 06:00:54 +08:00
|
|
|
this.stop();
|
|
|
|
this.restartIntervalMs = BridgeService.getNextReconnectionInterval(currentRestartIntervalMs);
|
|
|
|
this.share(stream, this.onerror).then(() => {
|
|
|
|
this.clearReconnectionTimeout();
|
|
|
|
}).catch(error => {
|
|
|
|
// Error handling is a no-op because it will be "handled" in handlePresenterFailure
|
|
|
|
logger.debug({
|
|
|
|
logCode: 'screenshare_reconnect_failed',
|
|
|
|
extraInfo: {
|
2021-01-12 23:24:01 +08:00
|
|
|
errorCode: error.errorCode,
|
2020-12-10 06:00:54 +08:00
|
|
|
errorMessage: error.errorMessage,
|
|
|
|
reconnecting: this.reconnecting,
|
|
|
|
role: this.role,
|
|
|
|
bridge: BRIDGE_NAME
|
|
|
|
},
|
|
|
|
}, 'Screensharing reconnect failed');
|
|
|
|
});
|
2019-09-07 02:58:22 +08:00
|
|
|
}
|
|
|
|
|
2020-12-10 06:00:54 +08:00
|
|
|
inboundStreamReconnect() {
|
|
|
|
const currentRestartIntervalMs = this.restartIntervalMs;
|
|
|
|
|
|
|
|
logger.warn({
|
|
|
|
logCode: 'screenshare_viewer_reconnect',
|
2020-12-16 01:45:20 +08:00
|
|
|
extraInfo: {
|
|
|
|
reconnecting: this.reconnecting,
|
|
|
|
role: this.role,
|
|
|
|
bridge: BRIDGE_NAME
|
|
|
|
},
|
2020-12-10 06:00:54 +08:00
|
|
|
}, `Screenshare viewer session is reconnecting`);
|
2020-12-16 01:45:20 +08:00
|
|
|
|
2020-12-10 06:00:54 +08:00
|
|
|
// Cleanly stop everything before triggering a reconnect
|
|
|
|
this.stop();
|
|
|
|
// Create new reconnect interval time
|
|
|
|
this.restartIntervalMs = BridgeService.getNextReconnectionInterval(currentRestartIntervalMs);
|
2020-12-16 01:45:20 +08:00
|
|
|
this.view(this.hasAudio).then(() => {
|
2020-12-10 06:00:54 +08:00
|
|
|
this.clearReconnectionTimeout();
|
|
|
|
}).catch(error => {
|
|
|
|
// Error handling is a no-op because it will be "handled" in handleViewerFailure
|
|
|
|
logger.debug({
|
|
|
|
logCode: 'screenshare_reconnect_failed',
|
|
|
|
extraInfo: {
|
2021-01-12 23:24:01 +08:00
|
|
|
errorCode: error.errorCode,
|
2020-12-10 06:00:54 +08:00
|
|
|
errorMessage: error.errorMessage,
|
|
|
|
reconnecting: this.reconnecting,
|
|
|
|
role: this.role,
|
|
|
|
bridge: BRIDGE_NAME
|
|
|
|
},
|
|
|
|
}, 'Screensharing reconnect failed');
|
|
|
|
});
|
2019-09-07 02:58:22 +08:00
|
|
|
}
|
|
|
|
|
2020-12-10 06:00:54 +08:00
|
|
|
handleConnectionTimeoutExpiry() {
|
|
|
|
this.reconnecting = true;
|
|
|
|
|
|
|
|
switch (this.role) {
|
|
|
|
case RECV_ROLE:
|
|
|
|
return this.inboundStreamReconnect();
|
|
|
|
case SEND_ROLE:
|
|
|
|
return this.outboundStreamReconnect();
|
|
|
|
default:
|
|
|
|
this.reconnecting = false;
|
|
|
|
logger.error({
|
|
|
|
logCode: 'screenshare_invalid_role'
|
|
|
|
}, 'Screen sharing with invalid role, wont reconnect');
|
|
|
|
break;
|
2019-09-11 00:20:40 +08:00
|
|
|
}
|
2019-09-07 02:58:22 +08:00
|
|
|
}
|
|
|
|
|
2020-12-10 06:00:54 +08:00
|
|
|
maxConnectionAttemptsReached () {
|
|
|
|
return this.connectionAttempts > BridgeService.MAX_CONN_ATTEMPTS;
|
|
|
|
}
|
2020-08-27 07:30:53 +08:00
|
|
|
|
2020-12-10 06:00:54 +08:00
|
|
|
scheduleReconnect () {
|
|
|
|
if (this.reconnectionTimeout == null) {
|
|
|
|
this.reconnectionTimeout = setTimeout(
|
|
|
|
this.handleConnectionTimeoutExpiry.bind(this),
|
|
|
|
this.restartIntervalMs
|
|
|
|
);
|
2020-08-27 07:30:53 +08:00
|
|
|
}
|
2020-12-10 06:00:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
clearReconnectionTimeout () {
|
|
|
|
this.reconnecting = false;
|
|
|
|
this.restartIntervalMs = BridgeService.BASE_MEDIA_TIMEOUT;
|
2020-08-27 07:30:53 +08:00
|
|
|
|
2020-12-10 06:00:54 +08:00
|
|
|
if (this.reconnectionTimeout) {
|
|
|
|
clearTimeout(this.reconnectionTimeout);
|
|
|
|
this.reconnectionTimeout = null;
|
|
|
|
}
|
2020-08-27 07:30:53 +08:00
|
|
|
}
|
|
|
|
|
2020-12-10 06:00:54 +08:00
|
|
|
handleViewerStart() {
|
|
|
|
const mediaElement = document.getElementById(SCREENSHARE_VIDEO_TAG);
|
2020-08-27 07:30:53 +08:00
|
|
|
|
2020-12-10 06:00:54 +08:00
|
|
|
if (mediaElement && this.broker && this.broker.webRtcPeer) {
|
|
|
|
const stream = this.broker.webRtcPeer.getRemoteStream();
|
|
|
|
BridgeService.screenshareLoadAndPlayMediaStream(stream, mediaElement, !this.broker.hasAudio);
|
2020-08-27 07:30:53 +08:00
|
|
|
}
|
2020-12-10 06:00:54 +08:00
|
|
|
|
|
|
|
this.clearReconnectionTimeout();
|
2020-08-27 07:30:53 +08:00
|
|
|
}
|
|
|
|
|
2020-12-10 06:00:54 +08:00
|
|
|
handleBrokerFailure(error) {
|
|
|
|
mapErrorCode(error);
|
2021-01-12 23:24:01 +08:00
|
|
|
const { errorMessage, errorCode } = error;
|
2020-12-16 01:45:20 +08:00
|
|
|
|
|
|
|
logger.error({
|
2021-01-12 23:24:01 +08:00
|
|
|
logCode: 'screenshare_broker_failure',
|
2020-12-16 01:45:20 +08:00
|
|
|
extraInfo: {
|
2021-01-12 23:24:01 +08:00
|
|
|
errorCode, errorMessage,
|
2020-12-16 01:45:20 +08:00
|
|
|
role: this.broker.role,
|
|
|
|
started: this.broker.started,
|
|
|
|
reconnecting: this.reconnecting,
|
|
|
|
bridge: BRIDGE_NAME
|
|
|
|
},
|
2021-07-08 03:04:56 +08:00
|
|
|
}, `Screenshare broker failure: ${errorMessage}`);
|
2020-12-16 01:45:20 +08:00
|
|
|
|
2020-12-10 06:00:54 +08:00
|
|
|
// Screensharing was already successfully negotiated and error occurred during
|
|
|
|
// during call; schedule a reconnect
|
|
|
|
// If the session has not yet started, a reconnect should already be scheduled
|
|
|
|
if (this.broker.started) {
|
|
|
|
this.scheduleReconnect();
|
2018-08-23 23:49:16 +08:00
|
|
|
}
|
2020-12-16 01:45:20 +08:00
|
|
|
|
|
|
|
return error;
|
2017-09-09 01:00:00 +08:00
|
|
|
}
|
|
|
|
|
2020-12-10 06:00:54 +08:00
|
|
|
async view(hasAudio = false) {
|
2020-12-16 01:45:20 +08:00
|
|
|
this.hasAudio = hasAudio;
|
2020-12-10 06:00:54 +08:00
|
|
|
this.role = RECV_ROLE;
|
|
|
|
const iceServers = await BridgeService.getIceServers(Auth.sessionToken);
|
|
|
|
const options = {
|
|
|
|
iceServers,
|
|
|
|
userName: Auth.fullname,
|
|
|
|
hasAudio,
|
|
|
|
};
|
|
|
|
|
|
|
|
this.broker = new ScreenshareBroker(
|
|
|
|
Auth.authenticateURL(SFU_URL),
|
|
|
|
BridgeService.getConferenceBridge(),
|
|
|
|
Auth.userID,
|
|
|
|
Auth.meetingID,
|
|
|
|
this.role,
|
|
|
|
options,
|
|
|
|
);
|
|
|
|
|
|
|
|
this.broker.onstart = this.handleViewerStart.bind(this);
|
|
|
|
this.broker.onerror = this.handleBrokerFailure.bind(this);
|
2021-03-12 08:44:38 +08:00
|
|
|
this.broker.onended = this.handleEnded.bind(this);
|
|
|
|
|
2020-12-10 06:00:54 +08:00
|
|
|
return this.broker.view().finally(this.scheduleReconnect.bind(this));
|
2017-09-09 01:00:00 +08:00
|
|
|
}
|
2017-09-13 04:47:06 +08:00
|
|
|
|
2020-12-10 06:00:54 +08:00
|
|
|
handlePresenterStart() {
|
|
|
|
logger.info({
|
|
|
|
logCode: 'screenshare_presenter_start_success',
|
|
|
|
}, 'Screenshare presenter started succesfully');
|
|
|
|
this.clearReconnectionTimeout();
|
|
|
|
this.reconnecting = false;
|
|
|
|
this.connectionAttempts = 0;
|
2017-09-13 04:47:06 +08:00
|
|
|
}
|
2017-11-06 23:39:55 +08:00
|
|
|
|
2021-03-12 08:44:38 +08:00
|
|
|
handleEnded() {
|
|
|
|
screenShareEndAlert();
|
|
|
|
}
|
|
|
|
|
2020-12-16 01:45:20 +08:00
|
|
|
share(stream, onFailure) {
|
|
|
|
return new Promise(async (resolve, reject) => {
|
|
|
|
this.onerror = onFailure;
|
|
|
|
this.connectionAttempts += 1;
|
|
|
|
this.role = SEND_ROLE;
|
|
|
|
this.hasAudio = BridgeService.streamHasAudioTrack(stream);
|
|
|
|
this.gdmStream = stream;
|
|
|
|
|
|
|
|
const onerror = (error) => {
|
|
|
|
const normalizedError = this.handleBrokerFailure(error);
|
|
|
|
if (this.maxConnectionAttemptsReached()) {
|
|
|
|
this.clearReconnectionTimeout();
|
|
|
|
this.connectionAttempts = 0;
|
2021-01-12 23:24:01 +08:00
|
|
|
onFailure(SCREENSHARING_ERRORS.MEDIA_TIMEOUT);
|
2020-12-16 01:45:20 +08:00
|
|
|
|
2021-01-12 23:24:01 +08:00
|
|
|
return reject(SCREENSHARING_ERRORS.MEDIA_TIMEOUT);
|
2020-12-16 01:45:20 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const iceServers = await BridgeService.getIceServers(Auth.sessionToken);
|
|
|
|
const options = {
|
|
|
|
iceServers,
|
|
|
|
userName: Auth.fullname,
|
|
|
|
stream,
|
|
|
|
hasAudio: this.hasAudio,
|
2021-04-25 03:12:59 +08:00
|
|
|
bitrate: BridgeService.BASE_BITRATE,
|
2020-12-16 01:45:20 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
this.broker = new ScreenshareBroker(
|
|
|
|
Auth.authenticateURL(SFU_URL),
|
|
|
|
BridgeService.getConferenceBridge(),
|
|
|
|
Auth.userID,
|
|
|
|
Auth.meetingID,
|
|
|
|
this.role,
|
|
|
|
options,
|
|
|
|
);
|
2020-12-10 06:00:54 +08:00
|
|
|
|
2020-12-16 01:45:20 +08:00
|
|
|
this.broker.onerror = onerror.bind(this);
|
|
|
|
this.broker.onstreamended = this.stop.bind(this);
|
|
|
|
this.broker.onstart = this.handlePresenterStart.bind(this);
|
2021-03-12 08:44:38 +08:00
|
|
|
this.broker.onended = this.handleEnded.bind(this);
|
2020-12-10 06:00:54 +08:00
|
|
|
|
2020-12-16 01:45:20 +08:00
|
|
|
this.broker.share().then(() => {
|
|
|
|
this.scheduleReconnect();
|
|
|
|
return resolve();
|
|
|
|
}).catch(reject);
|
|
|
|
});
|
2020-12-10 06:00:54 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
stop() {
|
2021-07-23 10:27:02 +08:00
|
|
|
const mediaElement = document.getElementById(SCREENSHARE_VIDEO_TAG);
|
|
|
|
|
2020-12-10 06:00:54 +08:00
|
|
|
if (this.broker) {
|
|
|
|
this.broker.stop();
|
|
|
|
// Checks if this session is a sharer and if it's not reconnecting
|
|
|
|
// If that's the case, clear the local sharing state in screen sharing UI
|
|
|
|
// component tracker to be extra sure we won't have any client-side state
|
|
|
|
// inconsistency - prlanzarin
|
2021-07-08 03:09:38 +08:00
|
|
|
if (this.broker && this.broker.role === SEND_ROLE && !this.reconnecting) {
|
|
|
|
setSharingScreen(false);
|
|
|
|
}
|
2020-12-10 06:00:54 +08:00
|
|
|
this.broker = null;
|
|
|
|
}
|
2021-07-23 10:27:02 +08:00
|
|
|
|
|
|
|
if (mediaElement && typeof mediaElement.pause === 'function') {
|
|
|
|
mediaElement.pause();
|
|
|
|
mediaElement.srcObject = null;
|
|
|
|
}
|
|
|
|
|
2020-12-10 06:00:54 +08:00
|
|
|
this.gdmStream = null;
|
|
|
|
this.clearReconnectionTimeout();
|
2017-11-06 23:39:55 +08:00
|
|
|
}
|
2017-09-09 01:00:00 +08:00
|
|
|
}
|