2021-09-18 01:17:31 +08:00
|
|
|
import logger from '/imports/startup/client/logger';
|
|
|
|
import BaseBroker from '/imports/ui/services/bbb-webrtc-sfu/sfu-base-broker';
|
|
|
|
|
|
|
|
const ON_ICE_CANDIDATE_MSG = 'iceCandidate';
|
|
|
|
const SUBSCRIBER_ANSWER = 'subscriberAnswer';
|
2022-01-26 04:51:02 +08:00
|
|
|
const DTMF = 'dtmf';
|
2021-09-18 01:17:31 +08:00
|
|
|
|
2021-09-25 02:11:53 +08:00
|
|
|
const SFU_COMPONENT_NAME = 'fullaudio';
|
|
|
|
|
|
|
|
class FullAudioBroker extends BaseBroker {
|
2021-09-18 01:17:31 +08:00
|
|
|
constructor(
|
|
|
|
wsUrl,
|
|
|
|
role,
|
|
|
|
options = {},
|
|
|
|
) {
|
|
|
|
super(SFU_COMPONENT_NAME, wsUrl);
|
|
|
|
this.role = role;
|
|
|
|
this.offering = true;
|
|
|
|
|
2022-03-11 01:59:43 +08:00
|
|
|
// Optional parameters are: caleeName, iceServers, offering,
|
2022-02-01 03:37:40 +08:00
|
|
|
// mediaServer, extension, constraints
|
2021-09-18 01:17:31 +08:00
|
|
|
Object.assign(this, options);
|
|
|
|
}
|
|
|
|
|
2022-02-01 03:37:40 +08:00
|
|
|
getLocalStream() {
|
|
|
|
if (this.webRtcPeer.peerConnection) {
|
|
|
|
return this.webRtcPeer.peerConnection.getLocalStreams()[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
setLocalStream(stream) {
|
|
|
|
if (this.webRtcPeer == null || this.webRtcPeer.peerConnection == null) {
|
|
|
|
throw new Error('Missing peer connection');
|
|
|
|
}
|
|
|
|
|
|
|
|
const { peerConnection } = this.webRtcPeer;
|
|
|
|
const newTracks = stream.getAudioTracks();
|
|
|
|
const localStream = this.getLocalStream();
|
|
|
|
const oldTracks = localStream ? localStream.getAudioTracks() : [];
|
|
|
|
|
|
|
|
peerConnection.getSenders().forEach((sender, index) => {
|
|
|
|
if (sender.track && sender.track.kind === 'audio') {
|
|
|
|
const newTrack = newTracks[index];
|
|
|
|
if (newTrack == null) return;
|
|
|
|
|
|
|
|
// Cleanup old tracks in the local MediaStream
|
|
|
|
const oldTrack = oldTracks[index];
|
|
|
|
sender.replaceTrack(newTrack);
|
|
|
|
if (oldTrack) {
|
|
|
|
oldTrack.stop();
|
|
|
|
localStream.removeTrack(oldTrack);
|
|
|
|
}
|
|
|
|
localStream.addTrack(newTrack);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return Promise.resolve();
|
|
|
|
}
|
|
|
|
|
2021-09-25 02:11:53 +08:00
|
|
|
joinAudio() {
|
2021-09-18 01:17:31 +08:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const options = {
|
|
|
|
mediaConstraints: {
|
2022-02-01 03:37:40 +08:00
|
|
|
audio: this.constraints ? this.constraints : true,
|
2021-09-18 01:17:31 +08:00
|
|
|
video: false,
|
|
|
|
},
|
2022-03-11 01:31:42 +08:00
|
|
|
configuration: this.populatePeerConfiguration(),
|
2021-09-18 01:17:31 +08:00
|
|
|
onicecandidate: (candidate) => {
|
|
|
|
this.onIceCandidate(candidate, this.role);
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2021-09-25 02:11:53 +08:00
|
|
|
const WebRTCPeer = (this.role === 'sendrecv')
|
|
|
|
? kurentoUtils.WebRtcPeer.WebRtcPeerSendrecv
|
|
|
|
: kurentoUtils.WebRtcPeer.WebRtcPeerRecvonly;
|
|
|
|
|
|
|
|
this.webRtcPeer = WebRTCPeer(options, (error) => {
|
2021-09-18 01:17:31 +08:00
|
|
|
if (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,
|
|
|
|
sfuComponent: this.sfuComponent,
|
|
|
|
started: this.started,
|
|
|
|
},
|
2021-09-25 02:11:53 +08:00
|
|
|
}, 'Audio peer creation failed');
|
2021-09-18 01:17:31 +08:00
|
|
|
this.onerror(normalizedError);
|
|
|
|
return reject(normalizedError);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.webRtcPeer.iceQueue = [];
|
|
|
|
|
|
|
|
if (this.offering) {
|
|
|
|
this.webRtcPeer.generateOffer(this.onOfferGenerated.bind(this));
|
|
|
|
} else {
|
2021-09-25 02:11:53 +08:00
|
|
|
this.sendStartReq();
|
2021-09-18 01:17:31 +08:00
|
|
|
}
|
2021-09-25 02:11:53 +08:00
|
|
|
|
|
|
|
return resolve();
|
2021-09-18 01:17:31 +08:00
|
|
|
});
|
|
|
|
|
2021-09-25 02:11:53 +08:00
|
|
|
this.webRtcPeer.peerConnection.onconnectionstatechange = this
|
|
|
|
.handleConnectionStateChange.bind(this);
|
2021-09-18 01:17:31 +08:00
|
|
|
return resolve();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-09-25 02:11:53 +08:00
|
|
|
listen() {
|
2021-09-18 01:17:31 +08:00
|
|
|
return this.openWSConnection()
|
2021-09-25 02:11:53 +08:00
|
|
|
.then(this.joinAudio.bind(this));
|
2021-09-18 01:17:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
onWSMessage (message) {
|
|
|
|
const parsedMessage = JSON.parse(message.data);
|
|
|
|
|
|
|
|
switch (parsedMessage.id) {
|
|
|
|
case 'startResponse':
|
|
|
|
this.onRemoteDescriptionReceived(parsedMessage);
|
|
|
|
break;
|
|
|
|
case 'iceCandidate':
|
|
|
|
this.handleIceCandidate(parsedMessage.candidate);
|
|
|
|
break;
|
|
|
|
case 'webRTCAudioSuccess':
|
|
|
|
this.onstart(parsedMessage.success);
|
|
|
|
this.started = true;
|
|
|
|
break;
|
|
|
|
case 'webRTCAudioError':
|
|
|
|
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 }
|
|
|
|
}, `Discarded invalid SFU message`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
handleSFUError (sfuResponse) {
|
|
|
|
const { code, reason, role } = sfuResponse;
|
|
|
|
const error = BaseBroker.assembleError(code, reason);
|
|
|
|
|
|
|
|
logger.error({
|
|
|
|
logCode: `${this.logCodePrefix}_sfu_error`,
|
|
|
|
extraInfo: {
|
|
|
|
errorCode: code,
|
|
|
|
errorMessage: error.errorMessage,
|
|
|
|
role,
|
|
|
|
sfuComponent: this.sfuComponent,
|
|
|
|
started: this.started,
|
|
|
|
},
|
|
|
|
}, `Listen only failed in SFU`);
|
|
|
|
this.onerror(error);
|
|
|
|
}
|
|
|
|
|
|
|
|
sendLocalDescription (localDescription) {
|
|
|
|
const message = {
|
|
|
|
id: SUBSCRIBER_ANSWER,
|
|
|
|
type: this.sfuComponent,
|
|
|
|
role: this.role,
|
|
|
|
sdpOffer: localDescription,
|
|
|
|
};
|
|
|
|
|
|
|
|
this.sendMessage(message);
|
|
|
|
}
|
|
|
|
|
|
|
|
onRemoteDescriptionReceived (sfuResponse) {
|
|
|
|
if (this.offering) {
|
|
|
|
return this.processAnswer(sfuResponse);
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.processOffer(sfuResponse);
|
|
|
|
}
|
|
|
|
|
|
|
|
sendStartReq (offer) {
|
|
|
|
const message = {
|
|
|
|
id: 'start',
|
|
|
|
type: this.sfuComponent,
|
|
|
|
role: this.role,
|
|
|
|
caleeName: this.caleeName,
|
|
|
|
sdpOffer: offer,
|
|
|
|
mediaServer: this.mediaServer,
|
2022-01-26 04:51:02 +08:00
|
|
|
extension: this.extension,
|
2021-09-18 01:17:31 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
logger.debug({
|
|
|
|
logCode: `${this.logCodePrefix}_offer_generated`,
|
|
|
|
extraInfo: { sfuComponent: this.sfuComponent, role: this.role },
|
|
|
|
}, `SFU audio offer generated`);
|
|
|
|
|
|
|
|
this.sendMessage(message);
|
|
|
|
}
|
|
|
|
|
|
|
|
onOfferGenerated (error, sdpOffer) {
|
|
|
|
if (error) {
|
|
|
|
logger.error({
|
|
|
|
logCode: `${this.logCodePrefix}_offer_failure`,
|
|
|
|
extraInfo: {
|
|
|
|
errorMessage: error.name || error.message || 'Unknown error',
|
|
|
|
sfuComponent: this.sfuComponent
|
|
|
|
},
|
|
|
|
}, `Listen only offer generation failed`);
|
|
|
|
// 1305: "PEER_NEGOTIATION_FAILED",
|
|
|
|
return this.onerror(error);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.sendStartReq(sdpOffer);
|
|
|
|
}
|
|
|
|
|
2022-01-26 04:51:02 +08:00
|
|
|
dtmf (tones) {
|
|
|
|
const message = {
|
|
|
|
id: DTMF,
|
|
|
|
type: this.sfuComponent,
|
|
|
|
tones,
|
|
|
|
};
|
|
|
|
|
|
|
|
this.sendMessage(message);
|
|
|
|
}
|
|
|
|
|
2021-09-18 01:17:31 +08:00
|
|
|
onIceCandidate (candidate, role) {
|
|
|
|
const message = {
|
|
|
|
id: ON_ICE_CANDIDATE_MSG,
|
|
|
|
role,
|
|
|
|
type: this.sfuComponent,
|
|
|
|
candidate,
|
|
|
|
};
|
|
|
|
|
|
|
|
this.sendMessage(message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-25 02:11:53 +08:00
|
|
|
export default FullAudioBroker;
|