2020-11-05 21:48:37 +08:00
|
|
|
import logger from '/imports/startup/client/logger';
|
2020-12-02 02:06:50 +08:00
|
|
|
import { notifyStreamStateChange } from '/imports/ui/services/bbb-webrtc-sfu/stream-state-service';
|
2021-01-12 06:04:03 +08:00
|
|
|
import { SFU_BROKER_ERRORS } from '/imports/ui/services/bbb-webrtc-sfu/broker-base-errors';
|
2020-11-05 21:48:37 +08:00
|
|
|
|
2023-02-15 05:07:41 +08:00
|
|
|
const WS_HEARTBEAT_OPTS = {
|
|
|
|
interval: 15000,
|
|
|
|
delay: 3000,
|
|
|
|
};
|
2020-11-05 21:48:37 +08:00
|
|
|
|
|
|
|
class BaseBroker {
|
2024-06-05 19:26:27 +08:00
|
|
|
static getPeerRole(role) {
|
|
|
|
switch (role) {
|
|
|
|
case 'send':
|
|
|
|
case 'sendrecv':
|
|
|
|
case 'sendonly':
|
|
|
|
case 'recvonly':
|
|
|
|
case 'recv':
|
|
|
|
case 'passive-sendrecv':
|
|
|
|
return role;
|
|
|
|
default:
|
|
|
|
throw new Error(`Invalid role: ${role}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-02 02:06:50 +08:00
|
|
|
static assembleError(code, reason) {
|
|
|
|
const message = reason || SFU_BROKER_ERRORS[code];
|
|
|
|
const error = new Error(message);
|
|
|
|
error.errorCode = code;
|
|
|
|
// Duplicating key-vals because we can't settle on an error pattern... - prlanzarin
|
|
|
|
error.errorCause = error.message;
|
|
|
|
error.errorMessage = error.message;
|
|
|
|
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
2020-11-05 21:48:37 +08:00
|
|
|
constructor(sfuComponent, wsUrl) {
|
|
|
|
this.wsUrl = wsUrl;
|
|
|
|
this.sfuComponent = sfuComponent;
|
|
|
|
this.ws = null;
|
|
|
|
this.webRtcPeer = null;
|
2023-02-15 05:07:41 +08:00
|
|
|
this.wsHeartbeat = null;
|
2020-11-05 21:48:37 +08:00
|
|
|
this.started = false;
|
2020-12-02 02:06:50 +08:00
|
|
|
this.signallingTransportOpen = false;
|
2020-11-05 21:48:37 +08:00
|
|
|
this.logCodePrefix = `${this.sfuComponent}_broker`;
|
2021-12-01 04:31:09 +08:00
|
|
|
this.peerConfiguration = {};
|
2020-12-09 04:19:03 +08:00
|
|
|
|
|
|
|
this.onbeforeunload = this.onbeforeunload.bind(this);
|
2023-02-15 05:07:41 +08:00
|
|
|
this._onWSError = this._onWSError.bind(this);
|
2020-12-09 04:19:03 +08:00
|
|
|
window.addEventListener('beforeunload', this.onbeforeunload);
|
2020-11-05 21:48:37 +08:00
|
|
|
}
|
|
|
|
|
2020-12-02 02:06:50 +08:00
|
|
|
set started (val) {
|
|
|
|
this._started = val;
|
|
|
|
}
|
|
|
|
|
|
|
|
get started () {
|
|
|
|
return this._started;
|
|
|
|
}
|
|
|
|
|
2020-12-09 04:19:03 +08:00
|
|
|
onbeforeunload () {
|
|
|
|
return this.stop();
|
|
|
|
}
|
|
|
|
|
2020-12-02 02:06:50 +08:00
|
|
|
onstart () {
|
|
|
|
// To be implemented by inheritors
|
|
|
|
}
|
|
|
|
|
|
|
|
onerror (error) {
|
|
|
|
// To be implemented by inheritors
|
|
|
|
}
|
|
|
|
|
|
|
|
onended () {
|
|
|
|
// To be implemented by inheritors
|
|
|
|
}
|
|
|
|
|
2021-08-12 03:35:16 +08:00
|
|
|
handleSFUError (sfuResponse) {
|
|
|
|
// To be implemented by inheritors
|
|
|
|
}
|
|
|
|
|
|
|
|
sendLocalDescription (localDescription) {
|
|
|
|
// To be implemented by inheritors
|
|
|
|
}
|
|
|
|
|
2023-02-15 05:07:41 +08:00
|
|
|
_onWSMessage(message) {
|
|
|
|
this._updateLastMsgTime();
|
|
|
|
this.onWSMessage(message);
|
|
|
|
}
|
|
|
|
|
|
|
|
onWSMessage(message) {
|
|
|
|
// To be implemented by inheritors
|
|
|
|
}
|
|
|
|
|
|
|
|
_onWSError(error) {
|
|
|
|
let normalizedError;
|
|
|
|
|
|
|
|
logger.error({
|
|
|
|
logCode: `${this.logCodePrefix}_websocket_error`,
|
|
|
|
extraInfo: {
|
|
|
|
errorMessage: error.name || error.message || 'Unknown error',
|
|
|
|
sfuComponent: this.sfuComponent,
|
|
|
|
}
|
|
|
|
}, 'WebSocket connection to SFU failed');
|
|
|
|
|
|
|
|
if (this.signallingTransportOpen) {
|
|
|
|
// 1301: "WEBSOCKET_DISCONNECTED", transport was already open
|
|
|
|
normalizedError = BaseBroker.assembleError(1301);
|
|
|
|
} else {
|
|
|
|
// 1302: "WEBSOCKET_CONNECTION_FAILED", transport errored before establishment
|
|
|
|
normalizedError = BaseBroker.assembleError(1302);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.onerror(normalizedError);
|
|
|
|
return normalizedError;
|
|
|
|
}
|
|
|
|
|
2020-11-05 21:48:37 +08:00
|
|
|
openWSConnection () {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
this.ws = new WebSocket(this.wsUrl);
|
|
|
|
|
2023-02-15 05:07:41 +08:00
|
|
|
this.ws.onmessage = this._onWSMessage.bind(this);
|
2020-11-05 21:48:37 +08:00
|
|
|
|
|
|
|
this.ws.onclose = () => {
|
2020-12-02 02:06:50 +08:00
|
|
|
// 1301: "WEBSOCKET_DISCONNECTED",
|
|
|
|
this.onerror(BaseBroker.assembleError(1301));
|
2020-11-05 21:48:37 +08:00
|
|
|
};
|
|
|
|
|
2023-02-15 05:07:41 +08:00
|
|
|
this.ws.onerror = (error) => reject(this._onWSError(error));
|
2020-11-05 21:48:37 +08:00
|
|
|
|
|
|
|
this.ws.onopen = () => {
|
2023-02-15 05:07:41 +08:00
|
|
|
this.setupWSHeartbeat();
|
2020-12-02 02:06:50 +08:00
|
|
|
this.signallingTransportOpen = true;
|
2020-11-05 21:48:37 +08:00
|
|
|
return resolve();
|
|
|
|
};
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-02-15 05:07:41 +08:00
|
|
|
closeWs() {
|
|
|
|
this.clearWSHeartbeat();
|
|
|
|
|
|
|
|
if (this.ws !== null) {
|
|
|
|
this.ws.onclose = function (){};
|
|
|
|
this.ws.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_updateLastMsgTime() {
|
|
|
|
this.ws.isAlive = true;
|
|
|
|
this.ws.lastMsgTime = Date.now();
|
|
|
|
}
|
|
|
|
|
|
|
|
_getTimeSinceLastMsg() {
|
|
|
|
return Date.now() - this.ws.lastMsgTime;
|
|
|
|
}
|
|
|
|
|
|
|
|
setupWSHeartbeat() {
|
|
|
|
if (WS_HEARTBEAT_OPTS.interval === 0 || this.ws == null) return;
|
|
|
|
|
|
|
|
this.ws.isAlive = true;
|
|
|
|
this.wsHeartbeat = setInterval(() => {
|
|
|
|
if (this.ws.isAlive === false) {
|
|
|
|
logger.warn({
|
|
|
|
logCode: `${this.logCodePrefix}_ws_heartbeat_failed`,
|
|
|
|
}, `WS heartbeat failed (${this.sfuComponent})`);
|
|
|
|
this.closeWs();
|
|
|
|
this._onWSError(new Error('HeartbeatFailed'));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this._getTimeSinceLastMsg() < (
|
|
|
|
WS_HEARTBEAT_OPTS.interval - WS_HEARTBEAT_OPTS.delay
|
|
|
|
)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.ws.isAlive = false;
|
|
|
|
this.ping();
|
|
|
|
}, WS_HEARTBEAT_OPTS.interval);
|
|
|
|
|
|
|
|
this.ping();
|
|
|
|
}
|
|
|
|
|
|
|
|
clearWSHeartbeat() {
|
|
|
|
if (this.wsHeartbeat) {
|
|
|
|
clearInterval(this.wsHeartbeat);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-05 21:48:37 +08:00
|
|
|
sendMessage (message) {
|
|
|
|
const jsonMessage = JSON.stringify(message);
|
2023-02-15 05:07:41 +08:00
|
|
|
|
|
|
|
try {
|
|
|
|
this.ws.send(jsonMessage);
|
|
|
|
} catch (error) {
|
|
|
|
logger.error({
|
|
|
|
logCode: `${this.logCodePrefix}_ws_send_error`,
|
|
|
|
extraInfo: {
|
|
|
|
errorName: error.name,
|
|
|
|
errorMessage: error.message,
|
|
|
|
sfuComponent: this.sfuComponent,
|
|
|
|
},
|
|
|
|
}, `Failed to send WebSocket message (${this.sfuComponent})`);
|
|
|
|
}
|
2020-11-05 21:48:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ping () {
|
|
|
|
this.sendMessage({ id: 'ping' });
|
|
|
|
}
|
|
|
|
|
2022-07-12 22:19:57 +08:00
|
|
|
_processRemoteDescription(localDescription = null) {
|
2021-08-12 03:35:16 +08:00
|
|
|
// There is a new local description; send it back to the server
|
|
|
|
if (localDescription) this.sendLocalDescription(localDescription);
|
|
|
|
// Mark the peer as negotiated and flush the ICE queue
|
|
|
|
this.webRtcPeer.negotiated = true;
|
|
|
|
this.processIceQueue();
|
|
|
|
}
|
|
|
|
|
|
|
|
_validateStartResponse (sfuResponse) {
|
|
|
|
const { response, role } = sfuResponse;
|
2020-11-05 21:48:37 +08:00
|
|
|
|
2021-08-12 03:35:16 +08:00
|
|
|
if (response !== 'accepted') {
|
|
|
|
this.handleSFUError(sfuResponse);
|
|
|
|
return false;
|
|
|
|
}
|
2020-11-05 21:48:37 +08:00
|
|
|
|
|
|
|
logger.debug({
|
|
|
|
logCode: `${this.logCodePrefix}_start_success`,
|
|
|
|
extraInfo: {
|
|
|
|
role,
|
|
|
|
sfuComponent: this.sfuComponent,
|
|
|
|
}
|
|
|
|
}, `Start request accepted for ${this.sfuComponent}`);
|
|
|
|
|
2021-08-12 03:35:16 +08:00
|
|
|
return true;
|
|
|
|
}
|
2020-11-05 21:48:37 +08:00
|
|
|
|
2022-07-12 22:19:57 +08:00
|
|
|
processOffer(sfuResponse) {
|
2021-08-12 03:35:16 +08:00
|
|
|
if (this._validateStartResponse(sfuResponse)) {
|
2022-07-12 22:19:57 +08:00
|
|
|
this.webRtcPeer.processOffer(sfuResponse.sdpAnswer)
|
|
|
|
.then(this._processRemoteDescription.bind(this))
|
|
|
|
.catch((error) => {
|
|
|
|
logger.error({
|
|
|
|
logCode: `${this.logCodePrefix}_processoffer_error`,
|
|
|
|
extraInfo: {
|
|
|
|
errorMessage: error.name || error.message || 'Unknown error',
|
|
|
|
sfuComponent: this.sfuComponent,
|
|
|
|
},
|
|
|
|
}, `Error processing offer from SFU for ${this.sfuComponent}`);
|
|
|
|
// 1305: "PEER_NEGOTIATION_FAILED",
|
|
|
|
this.onerror(BaseBroker.assembleError(1305));
|
|
|
|
});
|
2021-08-12 03:35:16 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-12 22:19:57 +08:00
|
|
|
processAnswer(sfuResponse) {
|
2021-08-12 03:35:16 +08:00
|
|
|
if (this._validateStartResponse(sfuResponse)) {
|
2022-07-12 22:19:57 +08:00
|
|
|
this.webRtcPeer.processAnswer(sfuResponse.sdpAnswer)
|
|
|
|
.then(this._processRemoteDescription.bind(this))
|
|
|
|
.catch((error) => {
|
|
|
|
logger.error({
|
|
|
|
logCode: `${this.logCodePrefix}_processanswer_error`,
|
|
|
|
extraInfo: {
|
|
|
|
errorMessage: error.name || error.message || 'Unknown error',
|
|
|
|
sfuComponent: this.sfuComponent,
|
|
|
|
},
|
|
|
|
}, `Error processing answer from SFU for ${this.sfuComponent}`);
|
|
|
|
// 1305: "PEER_NEGOTIATION_FAILED",
|
|
|
|
this.onerror(BaseBroker.assembleError(1305));
|
|
|
|
});
|
2021-08-12 03:35:16 +08:00
|
|
|
}
|
2020-11-05 21:48:37 +08:00
|
|
|
}
|
|
|
|
|
2021-12-01 04:31:09 +08:00
|
|
|
populatePeerConfiguration () {
|
|
|
|
this.addIceServers();
|
|
|
|
if (this.forceRelay) {
|
|
|
|
this.setRelayTransportPolicy();
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.peerConfiguration;
|
|
|
|
}
|
|
|
|
|
|
|
|
addIceServers () {
|
2020-11-05 21:48:37 +08:00
|
|
|
if (this.iceServers && this.iceServers.length > 0) {
|
2021-12-01 04:31:09 +08:00
|
|
|
this.peerConfiguration.iceServers = this.iceServers;
|
2020-11-05 21:48:37 +08:00
|
|
|
}
|
2021-12-01 04:31:09 +08:00
|
|
|
}
|
2020-11-05 21:48:37 +08:00
|
|
|
|
2021-12-01 04:31:09 +08:00
|
|
|
setRelayTransportPolicy () {
|
|
|
|
this.peerConfiguration.iceTransportPolicy = 'relay';
|
2020-11-05 21:48:37 +08:00
|
|
|
}
|
|
|
|
|
2020-12-02 02:06:50 +08:00
|
|
|
handleConnectionStateChange (eventIdentifier) {
|
2020-11-05 21:48:37 +08:00
|
|
|
if (this.webRtcPeer) {
|
|
|
|
const { peerConnection } = this.webRtcPeer;
|
|
|
|
const connectionState = peerConnection.connectionState;
|
|
|
|
if (eventIdentifier) {
|
|
|
|
notifyStreamStateChange(eventIdentifier, connectionState);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (connectionState === 'failed' || connectionState === 'closed') {
|
2024-04-09 02:42:37 +08:00
|
|
|
if (this.webRtcPeer?.peerConnection) {
|
|
|
|
this.webRtcPeer.peerConnection.onconnectionstatechange = null;
|
|
|
|
}
|
2020-12-02 02:06:50 +08:00
|
|
|
// 1307: "ICE_STATE_FAILED",
|
|
|
|
const error = BaseBroker.assembleError(1307);
|
|
|
|
this.onerror(error);
|
2020-11-05 21:48:37 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-12 22:19:57 +08:00
|
|
|
addIceCandidate(candidate) {
|
|
|
|
this.webRtcPeer.addIceCandidate(candidate).catch((error) => {
|
|
|
|
// Just log the error. We can't be sure if a candidate failure on add is
|
|
|
|
// fatal or not, so that's why we have a timeout set up for negotiations and
|
|
|
|
// listeners for ICE state transitioning to failures, so we won't act on it here
|
|
|
|
logger.error({
|
|
|
|
logCode: `${this.logCodePrefix}_addicecandidate_error`,
|
|
|
|
extraInfo: {
|
|
|
|
errorMessage: error.name || error.message || 'Unknown error',
|
|
|
|
errorCode: error.code || 'Unknown code',
|
|
|
|
sfuComponent: this.sfuComponent,
|
|
|
|
started: this.started,
|
|
|
|
},
|
|
|
|
}, 'Adding ICE candidate failed');
|
2020-11-05 21:48:37 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
processIceQueue () {
|
|
|
|
const peer = this.webRtcPeer;
|
|
|
|
while (peer.iceQueue.length) {
|
|
|
|
const candidate = peer.iceQueue.shift();
|
|
|
|
this.addIceCandidate(candidate);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
handleIceCandidate (candidate) {
|
|
|
|
const peer = this.webRtcPeer;
|
|
|
|
|
|
|
|
if (peer.negotiated) {
|
|
|
|
this.addIceCandidate(candidate);
|
|
|
|
} 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 (as of 2018)
|
|
|
|
// fail if candidates were added before the offer/answer cycle was completed.
|
|
|
|
// IT STILL HAPPENS - prlanzarin sept 2019
|
|
|
|
// still happens - prlanzarin sept 2020
|
|
|
|
peer.iceQueue.push(candidate);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
disposePeer () {
|
|
|
|
if (this.webRtcPeer) {
|
|
|
|
this.webRtcPeer.dispose();
|
|
|
|
this.webRtcPeer = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
stop () {
|
2020-12-02 02:06:50 +08:00
|
|
|
this.onstart = function(){};
|
|
|
|
this.onerror = function(){};
|
2020-12-09 04:19:03 +08:00
|
|
|
window.removeEventListener('beforeunload', this.onbeforeunload);
|
2020-11-05 21:48:37 +08:00
|
|
|
|
2024-04-09 02:42:37 +08:00
|
|
|
if (this.webRtcPeer?.peerConnection) {
|
2020-11-05 21:48:37 +08:00
|
|
|
this.webRtcPeer.peerConnection.onconnectionstatechange = null;
|
|
|
|
}
|
|
|
|
|
2023-02-15 05:07:41 +08:00
|
|
|
this.closeWs();
|
2020-11-05 21:48:37 +08:00
|
|
|
this.disposePeer();
|
2020-12-02 02:06:50 +08:00
|
|
|
this.started = false;
|
2020-11-05 21:48:37 +08:00
|
|
|
|
|
|
|
logger.debug({
|
|
|
|
logCode: `${this.logCodePrefix}_stop`,
|
|
|
|
extraInfo: { sfuComponent: this.sfuComponent },
|
|
|
|
}, `Stopped broker session for ${this.sfuComponent}`);
|
2020-12-02 02:06:50 +08:00
|
|
|
|
|
|
|
this.onended();
|
|
|
|
this.onended = function(){};
|
2020-11-05 21:48:37 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default BaseBroker;
|