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';
|
2022-07-12 22:19:57 +08:00
|
|
|
import WebRtcPeer from '/imports/ui/services/webrtc-base/peer';
|
2021-09-18 01:17:31 +08:00
|
|
|
|
|
|
|
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
|
|
|
|
2022-04-20 22:13:37 +08:00
|
|
|
const SFU_COMPONENT_NAME = 'audio';
|
2021-09-25 02:11:53 +08:00
|
|
|
|
2022-04-20 22:13:37 +08:00
|
|
|
class AudioBroker 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-05-02 21:47:22 +08:00
|
|
|
// Optional parameters are:
|
2022-06-29 03:22:37 +08:00
|
|
|
// clientSessionNumber
|
2022-05-02 21:47:22 +08:00
|
|
|
// iceServers,
|
|
|
|
// offering,
|
|
|
|
// mediaServer,
|
|
|
|
// extension,
|
|
|
|
// constraints,
|
|
|
|
// stream,
|
|
|
|
// signalCandidates
|
2022-07-12 22:19:57 +08:00
|
|
|
// traceLogs
|
2022-08-16 05:24:05 +08:00
|
|
|
// networkPriority
|
2023-04-06 00:22:38 +08:00
|
|
|
// gatheringTimeout
|
2023-08-08 02:28:17 +08:00
|
|
|
// transparentListenOnly
|
2021-09-18 01:17:31 +08:00
|
|
|
Object.assign(this, options);
|
|
|
|
}
|
|
|
|
|
2022-02-01 03:37:40 +08:00
|
|
|
getLocalStream() {
|
2022-05-27 22:02:07 +08:00
|
|
|
if (this.webRtcPeer && typeof this.webRtcPeer.getLocalStream === 'function') {
|
|
|
|
return this.webRtcPeer.getLocalStream();
|
2022-02-01 03:37:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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() : [];
|
|
|
|
|
2024-06-05 19:26:27 +08:00
|
|
|
peerConnection.getSenders().forEach((sender) => {
|
|
|
|
if (sender.track == null || sender?.track?.kind === 'audio') {
|
|
|
|
const newTrack = newTracks.shift();
|
2022-02-01 03:37:40 +08:00
|
|
|
if (newTrack == null) return;
|
|
|
|
|
|
|
|
// Cleanup old tracks in the local MediaStream
|
2024-06-05 19:26:27 +08:00
|
|
|
const oldTrack = oldTracks.shift();
|
2022-02-01 03:37:40 +08:00
|
|
|
sender.replaceTrack(newTrack);
|
|
|
|
if (oldTrack) {
|
|
|
|
oldTrack.stop();
|
|
|
|
localStream.removeTrack(oldTrack);
|
|
|
|
}
|
|
|
|
localStream.addTrack(newTrack);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2024-06-05 19:26:27 +08:00
|
|
|
if (oldTracks.length > 0) {
|
|
|
|
oldTracks.forEach((track) => {
|
|
|
|
track.stop();
|
|
|
|
localStream.removeTrack(track);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-02-01 03:37:40 +08:00
|
|
|
return Promise.resolve();
|
|
|
|
}
|
|
|
|
|
2022-04-21 01:40:06 +08:00
|
|
|
_join() {
|
2021-09-18 01:17:31 +08:00
|
|
|
return new Promise((resolve, reject) => {
|
2022-07-12 22:19:57 +08:00
|
|
|
try {
|
|
|
|
const options = {
|
|
|
|
audioStream: this.stream,
|
|
|
|
mediaConstraints: {
|
|
|
|
audio: this.constraints ? this.constraints : true,
|
|
|
|
video: false,
|
|
|
|
},
|
|
|
|
configuration: this.populatePeerConfiguration(),
|
|
|
|
onicecandidate: !this.signalCandidates ? null : (candidate) => {
|
|
|
|
this.onIceCandidate(candidate, this.role);
|
|
|
|
},
|
|
|
|
trace: this.traceLogs,
|
2022-08-16 05:24:05 +08:00
|
|
|
networkPriorities: this.networkPriority ? { audio: this.networkPriority } : undefined,
|
2022-09-16 04:42:43 +08:00
|
|
|
mediaStreamFactory: this.mediaStreamFactory,
|
2023-04-06 00:22:38 +08:00
|
|
|
gatheringTimeout: this.gatheringTimeout,
|
2022-07-12 22:19:57 +08:00
|
|
|
};
|
|
|
|
|
2024-06-05 19:26:27 +08:00
|
|
|
const peerRole = BaseBroker.getPeerRole(this.role);
|
2022-07-12 22:19:57 +08:00
|
|
|
this.webRtcPeer = new WebRtcPeer(peerRole, options);
|
2024-06-05 19:26:27 +08:00
|
|
|
window.peers = window.peers || [];
|
|
|
|
window.peers.push(this.webRtcPeer);
|
2021-09-18 01:17:31 +08:00
|
|
|
this.webRtcPeer.iceQueue = [];
|
2022-07-12 22:19:57 +08:00
|
|
|
this.webRtcPeer.start();
|
|
|
|
this.webRtcPeer.peerConnection.onconnectionstatechange = this.handleConnectionStateChange.bind(this);
|
2021-09-18 01:17:31 +08:00
|
|
|
|
|
|
|
if (this.offering) {
|
2024-04-11 08:25:40 +08:00
|
|
|
// We are the offerer
|
2022-07-12 22:19:57 +08:00
|
|
|
this.webRtcPeer.generateOffer()
|
|
|
|
.then(this.sendStartReq.bind(this))
|
|
|
|
.catch(this._handleOfferGenerationFailure.bind(this));
|
2024-06-05 19:26:27 +08:00
|
|
|
} else if (peerRole === 'recvonly'
|
|
|
|
|| peerRole === 'recv'
|
|
|
|
|| peerRole === 'passive-sendrecv') {
|
2024-04-11 08:25:40 +08:00
|
|
|
// We are the answerer and we are only listening, so we don't need
|
|
|
|
// to acquire local media
|
2021-09-25 02:11:53 +08:00
|
|
|
this.sendStartReq();
|
2024-04-11 08:25:40 +08:00
|
|
|
} else {
|
|
|
|
// We are the answerer and we are sending audio, so we need to acquire
|
|
|
|
// local media before sending the start request
|
|
|
|
this.webRtcPeer.mediaStreamFactory()
|
|
|
|
.then(() => { this.sendStartReq(); })
|
|
|
|
.catch(this._handleOfferGenerationFailure.bind(this));
|
2021-09-18 01:17:31 +08:00
|
|
|
}
|
2021-09-25 02:11:53 +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,
|
|
|
|
sfuComponent: this.sfuComponent,
|
|
|
|
started: this.started,
|
|
|
|
},
|
|
|
|
}, 'Audio peer creation failed');
|
|
|
|
this.onerror(normalizedError);
|
|
|
|
reject(normalizedError);
|
|
|
|
}
|
2021-09-18 01:17:31 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-04-21 01:40:06 +08:00
|
|
|
joinAudio() {
|
2021-09-18 01:17:31 +08:00
|
|
|
return this.openWSConnection()
|
2022-04-21 01:40:06 +08:00
|
|
|
.then(this._join.bind(this));
|
2021-09-18 01:17:31 +08:00
|
|
|
}
|
|
|
|
|
2022-04-20 21:52:16 +08:00
|
|
|
onWSMessage(message) {
|
2021-09-18 01:17:31 +08:00
|
|
|
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;
|
feat: add experimental support for ICE restart (#21208)
We currently use full renegotiation for audio, video, and screen sharing
reconnections, which involves re-creating transports and signaling channels
from scratch. While effective in some scenarios, this approach is slow and,
especially with outbound cameras and screen sharing, prone to failures.
To counter that, WebRTC provides a mechanism to restart ICE without needing
to re-create the peer connection. This allows us to avoid full renegotiation
and bypass some server-side signaling limitations. Implementing ICE restart
should make outbound camera/screen sharing reconnections more reliable and
faster.
This commit implements the ICE restart procedure for all WebRTC components'
*outbound* peers. It is based on bbb-webrtc-sfu >= v2.15.0-beta.0, which
added support for ICE restart requests. This feature is *off by default*.
To enable it, adjust the following flags:
- `/etc/bigbluebutton/bbb-webrtc-sfu/production.yml`: `allowIceRestart: true`
- `/etc/bigbluebutton/bbb-html5.yml`: `public.kurento.restartIce`
* Refer to the inline documentation; this can be enabled on the client side
per media type.
* Note: The default max retries for audio is lower than for cameras/screen
sharing (1 vs 3). This is because the full renegotiation process for audio
is more reliable, so ICE restart is attempted first, followed by full
renegotiation if necessary. This approach is less suitable for cameras/
screen sharing, where longer retry periods for ICE restart make sense
since full renegotation there is... iffy.
Endpoints that are inbound/`recvonly` only (client's perspective) do *not*
support ICE restart yet. There are two main reasons:
- Server-side changes are required to support `recvonly` endpoints,
particularly the proper handling of the server’s `setup` role in the
its SDPs during an ICE restart. These changes are too broad for now,
so they are deferred to future releases (SFU@v2.16).
- Full reconnections for `recvonly` endpoints are currently reliable,
unlike for `send*` endpoints. ICE restarts could still provide benefits
for `recvonly` endpoints, but we need the server updates first.
2024-09-20 18:35:32 +08:00
|
|
|
case 'restartIceResponse':
|
|
|
|
this.handleRestartIceResponse(parsedMessage);
|
|
|
|
break;
|
2021-09-18 01:17:31 +08:00
|
|
|
case 'webRTCAudioError':
|
|
|
|
case 'error':
|
|
|
|
this.handleSFUError(parsedMessage);
|
|
|
|
break;
|
|
|
|
case 'pong':
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
logger.debug({
|
|
|
|
logCode: `${this.logCodePrefix}_invalid_req`,
|
2022-04-20 21:52:16 +08:00
|
|
|
extraInfo: { messageId: parsedMessage.id || 'Unknown', sfuComponent: this.sfuComponent },
|
|
|
|
}, 'Discarded invalid SFU message');
|
2021-09-18 01:17:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-20 21:52:16 +08:00
|
|
|
handleSFUError(sfuResponse) {
|
2021-09-18 01:17:31 +08:00
|
|
|
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,
|
|
|
|
},
|
2022-04-20 22:13:37 +08:00
|
|
|
}, 'Audio failed in SFU');
|
2021-09-18 01:17:31 +08:00
|
|
|
this.onerror(error);
|
|
|
|
}
|
|
|
|
|
2022-04-20 21:52:16 +08:00
|
|
|
sendLocalDescription(localDescription) {
|
2021-09-18 01:17:31 +08:00
|
|
|
const message = {
|
|
|
|
id: SUBSCRIBER_ANSWER,
|
|
|
|
type: this.sfuComponent,
|
|
|
|
role: this.role,
|
|
|
|
sdpOffer: localDescription,
|
|
|
|
};
|
|
|
|
|
|
|
|
this.sendMessage(message);
|
|
|
|
}
|
|
|
|
|
2022-04-20 21:52:16 +08:00
|
|
|
onRemoteDescriptionReceived(sfuResponse) {
|
2021-09-18 01:17:31 +08:00
|
|
|
if (this.offering) {
|
|
|
|
return this.processAnswer(sfuResponse);
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.processOffer(sfuResponse);
|
|
|
|
}
|
|
|
|
|
2022-04-20 21:52:16 +08:00
|
|
|
sendStartReq(offer) {
|
2021-09-18 01:17:31 +08:00
|
|
|
const message = {
|
|
|
|
id: 'start',
|
|
|
|
type: this.sfuComponent,
|
|
|
|
role: this.role,
|
2022-06-29 03:22:37 +08:00
|
|
|
clientSessionNumber: this.clientSessionNumber,
|
2021-09-18 01:17:31 +08:00
|
|
|
sdpOffer: offer,
|
|
|
|
mediaServer: this.mediaServer,
|
2022-01-26 04:51:02 +08:00
|
|
|
extension: this.extension,
|
2023-08-08 02:28:17 +08:00
|
|
|
transparentListenOnly: this.transparentListenOnly,
|
2021-09-18 01:17:31 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
logger.debug({
|
|
|
|
logCode: `${this.logCodePrefix}_offer_generated`,
|
|
|
|
extraInfo: { sfuComponent: this.sfuComponent, role: this.role },
|
2022-04-20 21:52:16 +08:00
|
|
|
}, 'SFU audio offer generated');
|
2021-09-18 01:17:31 +08:00
|
|
|
|
|
|
|
this.sendMessage(message);
|
|
|
|
}
|
|
|
|
|
2022-07-12 22:19:57 +08:00
|
|
|
_handleOfferGenerationFailure(error) {
|
2021-09-18 01:17:31 +08:00
|
|
|
if (error) {
|
|
|
|
logger.error({
|
|
|
|
logCode: `${this.logCodePrefix}_offer_failure`,
|
|
|
|
extraInfo: {
|
|
|
|
errorMessage: error.name || error.message || 'Unknown error',
|
2022-04-20 21:52:16 +08:00
|
|
|
sfuComponent: this.sfuComponent,
|
2021-09-18 01:17:31 +08:00
|
|
|
},
|
2022-04-20 22:13:37 +08:00
|
|
|
}, 'Audio offer generation failed');
|
2021-09-18 01:17:31 +08:00
|
|
|
// 1305: "PEER_NEGOTIATION_FAILED",
|
2022-04-20 21:52:16 +08:00
|
|
|
this.onerror(error);
|
2021-09-18 01:17:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-20 21:52:16 +08:00
|
|
|
dtmf(tones) {
|
2022-01-26 04:51:02 +08:00
|
|
|
const message = {
|
|
|
|
id: DTMF,
|
|
|
|
type: this.sfuComponent,
|
|
|
|
tones,
|
|
|
|
};
|
|
|
|
|
|
|
|
this.sendMessage(message);
|
|
|
|
}
|
|
|
|
|
2022-04-20 21:52:16 +08:00
|
|
|
onIceCandidate(candidate, role) {
|
2021-09-18 01:17:31 +08:00
|
|
|
const message = {
|
|
|
|
id: ON_ICE_CANDIDATE_MSG,
|
|
|
|
role,
|
|
|
|
type: this.sfuComponent,
|
|
|
|
candidate,
|
|
|
|
};
|
|
|
|
|
|
|
|
this.sendMessage(message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-20 22:13:37 +08:00
|
|
|
export default AudioBroker;
|