2020-12-10 02:46:30 +08:00
|
|
|
import logger from '/imports/startup/client/logger';
|
|
|
|
import BaseBroker from '/imports/ui/services/bbb-webrtc-sfu/sfu-base-broker';
|
2022-07-12 22:19:57 +08:00
|
|
|
import WebRtcPeer from '/imports/ui/services/webrtc-base/peer';
|
2020-12-10 02:46:30 +08:00
|
|
|
|
|
|
|
const ON_ICE_CANDIDATE_MSG = 'iceCandidate';
|
2021-08-12 03:36:32 +08:00
|
|
|
const SUBSCRIBER_ANSWER = 'subscriberAnswer';
|
2020-12-10 02:46:30 +08:00
|
|
|
const SFU_COMPONENT_NAME = 'screenshare';
|
|
|
|
|
|
|
|
class ScreenshareBroker extends BaseBroker {
|
|
|
|
constructor(
|
|
|
|
wsUrl,
|
|
|
|
voiceBridge,
|
|
|
|
userId,
|
|
|
|
internalMeetingId,
|
|
|
|
role,
|
|
|
|
options = {},
|
|
|
|
) {
|
|
|
|
super(SFU_COMPONENT_NAME, wsUrl);
|
|
|
|
this.voiceBridge = voiceBridge;
|
|
|
|
this.userId = userId;
|
|
|
|
this.internalMeetingId = internalMeetingId;
|
|
|
|
this.role = role;
|
|
|
|
this.ws = null;
|
|
|
|
this.webRtcPeer = null;
|
|
|
|
this.hasAudio = false;
|
2023-03-24 20:23:17 +08:00
|
|
|
this.contentType = "camera";
|
2021-08-12 03:36:32 +08:00
|
|
|
this.offering = true;
|
2021-09-24 03:32:14 +08:00
|
|
|
this.signalCandidates = true;
|
2022-05-08 00:25:31 +08:00
|
|
|
this.ending = false;
|
2020-12-10 02:46:30 +08:00
|
|
|
|
2022-07-12 22:19:57 +08:00
|
|
|
// Optional parameters are:
|
|
|
|
// userName,
|
|
|
|
// caleeName,
|
|
|
|
// iceServers,
|
|
|
|
// hasAudio,
|
2023-03-24 20:23:17 +08:00
|
|
|
// contentType,
|
2022-07-12 22:19:57 +08:00
|
|
|
// bitrate,
|
|
|
|
// offering,
|
|
|
|
// mediaServer,
|
|
|
|
// signalCandidates,
|
|
|
|
// traceLogs
|
2022-08-16 05:24:05 +08:00
|
|
|
// networkPriority
|
2023-04-06 00:22:38 +08:00
|
|
|
// gatheringTimeout
|
2020-12-10 02:46:30 +08:00
|
|
|
Object.assign(this, options);
|
|
|
|
}
|
|
|
|
|
2022-05-08 00:25:31 +08:00
|
|
|
_onstreamended() {
|
|
|
|
// Flag the broker as ending; we want to abort processing start responses
|
|
|
|
this.ending = true;
|
|
|
|
this.onstreamended();
|
|
|
|
}
|
|
|
|
|
|
|
|
onstreamended() {
|
2020-12-10 02:46:30 +08:00
|
|
|
// To be implemented by instantiators
|
|
|
|
}
|
|
|
|
|
2022-05-08 00:25:31 +08:00
|
|
|
async share () {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
if (this.stream == null) {
|
|
|
|
logger.error({
|
|
|
|
logCode: `${this.logCodePrefix}_missing_stream`,
|
|
|
|
extraInfo: { role: this.role, sfuComponent: this.sfuComponent },
|
|
|
|
}, 'Screenshare broker start failed: missing stream');
|
|
|
|
return reject(BaseBroker.assembleError(1305));
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.openWSConnection()
|
|
|
|
.then(this.startScreensharing.bind(this))
|
|
|
|
.then(resolve)
|
|
|
|
.catch(reject);
|
|
|
|
});
|
2020-12-10 02:46:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
view () {
|
|
|
|
return this.openWSConnection()
|
|
|
|
.then(this.subscribeToScreenStream.bind(this));
|
|
|
|
}
|
|
|
|
|
|
|
|
onWSMessage (message) {
|
|
|
|
const parsedMessage = JSON.parse(message.data);
|
|
|
|
|
|
|
|
switch (parsedMessage.id) {
|
|
|
|
case 'startResponse':
|
2022-05-08 00:25:31 +08:00
|
|
|
if (!this.ending && !this.started) {
|
|
|
|
this.onRemoteDescriptionReceived(parsedMessage);
|
|
|
|
}
|
2020-12-10 02:46:30 +08:00
|
|
|
break;
|
|
|
|
case 'playStart':
|
2022-05-08 00:25:31 +08:00
|
|
|
if (!this.ending && !this.started) {
|
|
|
|
this.onstart();
|
|
|
|
this.started = true;
|
|
|
|
}
|
2020-12-10 02:46:30 +08:00
|
|
|
break;
|
|
|
|
case 'stopSharing':
|
|
|
|
this.stop();
|
|
|
|
break;
|
|
|
|
case 'iceCandidate':
|
|
|
|
this.handleIceCandidate(parsedMessage.candidate);
|
|
|
|
break;
|
|
|
|
case 'error':
|
|
|
|
this.handleSFUError(parsedMessage);
|
|
|
|
break;
|
|
|
|
case 'pong':
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
logger.debug({
|
|
|
|
logCode: `${this.logCodePrefix}_invalid_req`,
|
|
|
|
extraInfo: {
|
|
|
|
messageId: parsedMessage.id || 'Unknown',
|
|
|
|
sfuComponent: this.sfuComponent,
|
|
|
|
role: this.role,
|
|
|
|
}
|
|
|
|
}, `Discarded invalid SFU message`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
handleSFUError (sfuResponse) {
|
|
|
|
const { code, reason } = sfuResponse;
|
|
|
|
const error = BaseBroker.assembleError(code, reason);
|
|
|
|
|
|
|
|
logger.error({
|
|
|
|
logCode: `${this.logCodePrefix}_sfu_error`,
|
|
|
|
extraInfo: {
|
|
|
|
errorCode: code,
|
|
|
|
errorMessage: error.errorMessage,
|
|
|
|
role: this.role,
|
|
|
|
sfuComponent: this.sfuComponent,
|
|
|
|
started: this.started,
|
|
|
|
},
|
|
|
|
}, `Screen sharing failed in SFU`);
|
|
|
|
this.onerror(error);
|
|
|
|
}
|
|
|
|
|
2021-08-12 03:36:32 +08:00
|
|
|
sendLocalDescription (localDescription) {
|
|
|
|
const message = {
|
|
|
|
id: SUBSCRIBER_ANSWER,
|
|
|
|
type: this.sfuComponent,
|
|
|
|
role: this.role,
|
|
|
|
voiceBridge: this.voiceBridge,
|
|
|
|
callerName: this.userId,
|
|
|
|
answer: localDescription,
|
|
|
|
};
|
|
|
|
|
|
|
|
this.sendMessage(message);
|
|
|
|
}
|
|
|
|
|
|
|
|
onRemoteDescriptionReceived (sfuResponse) {
|
|
|
|
if (this.offering) {
|
|
|
|
return this.processAnswer(sfuResponse);
|
2020-12-10 02:46:30 +08:00
|
|
|
}
|
|
|
|
|
2021-08-12 03:36:32 +08:00
|
|
|
return this.processOffer(sfuResponse);
|
|
|
|
}
|
|
|
|
|
2022-07-12 22:19:57 +08:00
|
|
|
sendStartReq(offer) {
|
2020-12-10 02:46:30 +08:00
|
|
|
const message = {
|
|
|
|
id: 'start',
|
|
|
|
type: this.sfuComponent,
|
|
|
|
role: this.role,
|
|
|
|
internalMeetingId: this.internalMeetingId,
|
|
|
|
voiceBridge: this.voiceBridge,
|
|
|
|
userName: this.userName,
|
|
|
|
callerName: this.userId,
|
2021-08-12 03:36:32 +08:00
|
|
|
sdpOffer: offer,
|
2020-12-10 02:46:30 +08:00
|
|
|
hasAudio: !!this.hasAudio,
|
2023-03-24 20:23:17 +08:00
|
|
|
contentType: this.contentType,
|
2021-04-25 03:12:59 +08:00
|
|
|
bitrate: this.bitrate,
|
2021-04-27 02:39:29 +08:00
|
|
|
mediaServer: this.mediaServer,
|
2020-12-10 02:46:30 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
this.sendMessage(message);
|
|
|
|
}
|
|
|
|
|
2022-07-12 22:19:57 +08:00
|
|
|
_handleOfferGenerationFailure(error) {
|
|
|
|
logger.error({
|
|
|
|
logCode: `${this.logCodePrefix}_offer_failure`,
|
|
|
|
extraInfo: {
|
|
|
|
errorMessage: error.name || error.message || 'Unknown error',
|
|
|
|
role: this.role,
|
|
|
|
sfuComponent: this.sfuComponent,
|
|
|
|
},
|
|
|
|
}, 'Screenshare offer generation failed');
|
|
|
|
// 1305: "PEER_NEGOTIATION_FAILED",
|
|
|
|
return this.onerror(error);
|
2021-08-12 03:36:32 +08:00
|
|
|
}
|
|
|
|
|
2022-05-08 00:25:31 +08:00
|
|
|
startScreensharing() {
|
2020-12-10 02:46:30 +08:00
|
|
|
return new Promise((resolve, reject) => {
|
2022-07-12 22:19:57 +08:00
|
|
|
try {
|
|
|
|
const options = {
|
|
|
|
onicecandidate: this.signalCandidates ? this.onIceCandidate.bind(this) : null,
|
|
|
|
videoStream: this.stream,
|
|
|
|
configuration: this.populatePeerConfiguration(),
|
|
|
|
trace: this.traceLogs,
|
2022-08-16 05:24:05 +08:00
|
|
|
networkPriorities: this.networkPriority ? { video: this.networkPriority } : undefined,
|
2023-04-06 00:22:38 +08:00
|
|
|
gatheringTimeout: this.gatheringTimeout,
|
2022-07-12 22:19:57 +08:00
|
|
|
};
|
|
|
|
this.webRtcPeer = new WebRtcPeer('sendonly', options);
|
2020-12-10 02:46:30 +08:00
|
|
|
this.webRtcPeer.iceQueue = [];
|
2022-07-12 22:19:57 +08:00
|
|
|
this.webRtcPeer.start();
|
|
|
|
this.webRtcPeer.peerConnection.onconnectionstatechange = () => {
|
|
|
|
this.handleConnectionStateChange('screenshare');
|
|
|
|
};
|
2021-08-12 03:36:32 +08:00
|
|
|
|
|
|
|
if (this.offering) {
|
2022-07-12 22:19:57 +08:00
|
|
|
this.webRtcPeer.generateOffer()
|
|
|
|
.then(this.sendStartReq.bind(this))
|
|
|
|
.catch(this._handleOfferGenerationFailure.bind(this));
|
2021-08-12 03:36:32 +08:00
|
|
|
} else {
|
|
|
|
this.sendStartReq();
|
|
|
|
}
|
2020-12-10 02:46:30 +08:00
|
|
|
|
2022-07-12 22:19:57 +08:00
|
|
|
resolve();
|
|
|
|
} catch (error) {
|
|
|
|
// 1305: "PEER_NEGOTIATION_FAILED",
|
|
|
|
const normalizedError = BaseBroker.assembleError(1305);
|
|
|
|
logger.error({
|
|
|
|
logCode: `${this.logCodePrefix}_peer_creation_failed`,
|
|
|
|
extraInfo: {
|
|
|
|
errorMessage: error.name || error.message || 'Unknown error',
|
|
|
|
errorCode: normalizedError.errorCode,
|
|
|
|
role: this.role,
|
|
|
|
sfuComponent: this.sfuComponent,
|
|
|
|
started: this.started,
|
|
|
|
},
|
|
|
|
}, 'Screenshare peer creation failed');
|
|
|
|
this.onerror(normalizedError);
|
|
|
|
reject(normalizedError);
|
|
|
|
}
|
2020-12-10 02:46:30 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-09-24 03:32:14 +08:00
|
|
|
onIceCandidate (candidate) {
|
2020-12-10 02:46:30 +08:00
|
|
|
const message = {
|
|
|
|
id: ON_ICE_CANDIDATE_MSG,
|
2021-09-24 03:32:14 +08:00
|
|
|
role: this.role,
|
2020-12-10 02:46:30 +08:00
|
|
|
type: this.sfuComponent,
|
|
|
|
voiceBridge: this.voiceBridge,
|
|
|
|
candidate,
|
|
|
|
callerName: this.userId,
|
|
|
|
};
|
|
|
|
|
|
|
|
this.sendMessage(message);
|
|
|
|
}
|
|
|
|
|
2022-07-12 22:19:57 +08:00
|
|
|
subscribeToScreenStream() {
|
2020-12-10 02:46:30 +08:00
|
|
|
return new Promise((resolve, reject) => {
|
2022-07-12 22:19:57 +08:00
|
|
|
try {
|
|
|
|
const options = {
|
|
|
|
mediaConstraints: {
|
|
|
|
audio: !!this.hasAudio,
|
|
|
|
},
|
|
|
|
onicecandidate: this.signalCandidates ? this.onIceCandidate.bind(this) : null,
|
|
|
|
configuration: this.populatePeerConfiguration(),
|
|
|
|
trace: this.traceLogs,
|
2023-04-06 00:22:38 +08:00
|
|
|
gatheringTimeout: this.gatheringTimeout,
|
2022-07-12 22:19:57 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
this.webRtcPeer = new WebRtcPeer('recvonly', options);
|
2020-12-10 02:46:30 +08:00
|
|
|
this.webRtcPeer.iceQueue = [];
|
2022-07-12 22:19:57 +08:00
|
|
|
this.webRtcPeer.start();
|
|
|
|
this.webRtcPeer.peerConnection.onconnectionstatechange = () => {
|
|
|
|
this.handleConnectionStateChange('screenshare');
|
|
|
|
};
|
2021-08-12 03:36:32 +08:00
|
|
|
|
|
|
|
if (this.offering) {
|
2022-07-12 22:19:57 +08:00
|
|
|
this.webRtcPeer.generateOffer()
|
|
|
|
.then(this.sendStartReq.bind(this))
|
|
|
|
.catch(this._handleOfferGenerationFailure.bind(this));
|
2021-08-12 03:36:32 +08:00
|
|
|
} else {
|
|
|
|
this.sendStartReq();
|
|
|
|
}
|
2020-12-10 02:46:30 +08:00
|
|
|
|
2022-07-12 22:19:57 +08:00
|
|
|
resolve();
|
|
|
|
} catch (error) {
|
|
|
|
// 1305: "PEER_NEGOTIATION_FAILED",
|
|
|
|
const normalizedError = BaseBroker.assembleError(1305);
|
|
|
|
logger.error({
|
|
|
|
logCode: `${this.logCodePrefix}_peer_creation_failed`,
|
|
|
|
extraInfo: {
|
|
|
|
errorMessage: error.name || error.message || 'Unknown error',
|
|
|
|
errorCode: normalizedError.errorCode,
|
|
|
|
role: this.role,
|
|
|
|
sfuComponent: this.sfuComponent,
|
|
|
|
started: this.started,
|
|
|
|
},
|
|
|
|
}, 'Screenshare peer creation failed');
|
|
|
|
this.onerror(normalizedError);
|
|
|
|
reject(normalizedError);
|
|
|
|
|
|
|
|
}
|
2020-12-10 02:46:30 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default ScreenshareBroker;
|