2021-09-18 01:17:31 +08:00
|
|
|
import BaseAudioBridge from './base';
|
|
|
|
import Auth from '/imports/ui/services/auth';
|
|
|
|
import logger from '/imports/startup/client/logger';
|
2021-09-25 02:11:53 +08:00
|
|
|
import FullAudioBroker from '/imports/ui/services/bbb-webrtc-sfu/fullaudio-broker';
|
2021-09-18 01:17:31 +08:00
|
|
|
import loadAndPlayMediaStream from '/imports/ui/services/bbb-webrtc-sfu/load-play';
|
|
|
|
import {
|
|
|
|
fetchWebRTCMappedStunTurnServers,
|
2021-09-25 02:11:53 +08:00
|
|
|
getMappedFallbackStun,
|
2021-09-18 01:17:31 +08:00
|
|
|
} from '/imports/utils/fetchStunTurnServers';
|
|
|
|
import getFromMeetingSettings from '/imports/ui/services/meeting-settings';
|
|
|
|
|
|
|
|
const SFU_URL = Meteor.settings.public.kurento.wsUrl;
|
|
|
|
const MEDIA = Meteor.settings.public.media;
|
2021-10-22 03:20:13 +08:00
|
|
|
const DEFAULT_FULLAUDIO_MEDIA_SERVER = MEDIA.audio.fullAudioMediaServer;
|
2021-09-18 01:17:31 +08:00
|
|
|
const MEDIA_TAG = MEDIA.mediaTag.replace(/#/g, '');
|
|
|
|
const GLOBAL_AUDIO_PREFIX = 'GLOBAL_AUDIO_';
|
|
|
|
const RECONNECT_TIMEOUT_MS = MEDIA.listenOnlyCallTimeout || 15000;
|
|
|
|
const OFFERING = MEDIA.listenOnlyOffering;
|
2021-09-25 02:11:53 +08:00
|
|
|
const SENDRECV_ROLE = 'sendrecv';
|
2021-09-18 01:17:31 +08:00
|
|
|
const RECV_ROLE = 'recv';
|
2021-09-25 02:11:53 +08:00
|
|
|
const BRIDGE_NAME = 'fullaudio';
|
|
|
|
const AUDIO_SESSION_NUM_KEY = 'AudioSessionNumber';
|
2021-09-18 01:17:31 +08:00
|
|
|
|
|
|
|
// SFU's base broker has distinct error codes so that it can be reused by different
|
|
|
|
// modules. Errors that have a valid, localized counterpart in audio manager are
|
|
|
|
// mapped so that the user gets a localized error message.
|
|
|
|
// The ones that haven't (ie SFU's servers-side errors), aren't mapped.
|
|
|
|
const errorCodeMap = {
|
|
|
|
1301: 1001,
|
|
|
|
1302: 1002,
|
|
|
|
1305: 1005,
|
|
|
|
1307: 1007,
|
2021-09-25 02:11:53 +08:00
|
|
|
};
|
|
|
|
|
2021-09-18 01:17:31 +08:00
|
|
|
const mapErrorCode = (error) => {
|
|
|
|
const { errorCode } = error;
|
|
|
|
const mappedErrorCode = errorCodeMap[errorCode];
|
|
|
|
if (errorCode == null || mappedErrorCode == null) return error;
|
2021-09-25 02:11:53 +08:00
|
|
|
// eslint-disable-next-line no-param-reassign
|
2021-09-18 01:17:31 +08:00
|
|
|
error.errorCode = mappedErrorCode;
|
|
|
|
return error;
|
2021-09-25 02:11:53 +08:00
|
|
|
};
|
2021-09-18 01:17:31 +08:00
|
|
|
|
2021-09-25 02:11:53 +08:00
|
|
|
const getMediaServerAdapter = () => getFromMeetingSettings(
|
2021-10-22 03:20:13 +08:00
|
|
|
'media-server-fullaudio',
|
|
|
|
DEFAULT_FULLAUDIO_MEDIA_SERVER,
|
2021-09-25 02:11:53 +08:00
|
|
|
);
|
|
|
|
|
|
|
|
const getAudioSessionNumber = () => {
|
|
|
|
let currItem = parseInt(sessionStorage.getItem(AUDIO_SESSION_NUM_KEY), 10);
|
|
|
|
if (!currItem) {
|
|
|
|
currItem = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
currItem += 1;
|
|
|
|
sessionStorage.setItem(AUDIO_SESSION_NUM_KEY, currItem);
|
|
|
|
return currItem;
|
2021-09-18 01:17:31 +08:00
|
|
|
};
|
|
|
|
|
2021-09-25 02:11:53 +08:00
|
|
|
export default class FullAudioBridge extends BaseAudioBridge {
|
2021-09-18 01:17:31 +08:00
|
|
|
constructor(userData) {
|
|
|
|
super();
|
|
|
|
this.internalMeetingID = userData.meetingId;
|
|
|
|
this.voiceBridge = userData.voiceBridge;
|
|
|
|
this.userId = userData.userId;
|
|
|
|
this.name = userData.username;
|
|
|
|
this.sessionToken = userData.sessionToken;
|
|
|
|
this.media = {
|
|
|
|
inputDevice: {},
|
|
|
|
};
|
2021-09-25 02:11:53 +08:00
|
|
|
this.broker = null;
|
2021-09-18 01:17:31 +08:00
|
|
|
this.reconnecting = false;
|
2021-09-25 02:11:53 +08:00
|
|
|
this.iceServers = [];
|
2021-09-18 01:17:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
async changeOutputDevice(value) {
|
|
|
|
const audioContext = document.querySelector(`#${MEDIA_TAG}`);
|
|
|
|
if (audioContext.setSinkId) {
|
|
|
|
try {
|
|
|
|
await audioContext.setSinkId(value);
|
|
|
|
this.media.outputDeviceId = value;
|
|
|
|
} catch (error) {
|
|
|
|
logger.error({
|
2021-09-25 02:11:53 +08:00
|
|
|
logCode: 'fullaudio_changeoutputdevice_error',
|
|
|
|
extraInfo: { error, bridge: BRIDGE_NAME },
|
2021-09-18 01:17:31 +08:00
|
|
|
}, 'Audio bridge failed to change output device');
|
|
|
|
throw new Error(this.baseErrorCodes.MEDIA_ERROR);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.media.outputDeviceId || value;
|
|
|
|
}
|
|
|
|
|
|
|
|
getPeerConnection() {
|
|
|
|
if (!this.broker) return null;
|
|
|
|
|
2021-09-25 02:11:53 +08:00
|
|
|
const { webRtcPeer } = this.broker;
|
2021-09-18 01:17:31 +08:00
|
|
|
if (webRtcPeer) return webRtcPeer.peerConnection;
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
handleTermination() {
|
|
|
|
return this.callback({ status: this.baseCallStates.ended, bridge: BRIDGE_NAME });
|
|
|
|
}
|
|
|
|
|
|
|
|
clearReconnectionTimeout() {
|
|
|
|
this.reconnecting = false;
|
|
|
|
if (this.reconnectionTimeout) {
|
|
|
|
clearTimeout(this.reconnectionTimeout);
|
|
|
|
this.reconnectionTimeout = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
reconnect() {
|
|
|
|
this.broker.stop();
|
|
|
|
this.callback({ status: this.baseCallStates.reconnecting, bridge: BRIDGE_NAME });
|
|
|
|
this.reconnecting = true;
|
|
|
|
// Set up a reconnectionTimeout in case the server is unresponsive
|
|
|
|
// for some reason. If it gets triggered, end the session and stop
|
|
|
|
// trying to reconnect
|
|
|
|
this.reconnectionTimeout = setTimeout(() => {
|
|
|
|
this.callback({
|
|
|
|
status: this.baseCallStates.failed,
|
|
|
|
error: 1010,
|
|
|
|
bridgeError: 'Reconnection timeout',
|
|
|
|
bridge: BRIDGE_NAME,
|
|
|
|
});
|
|
|
|
this.broker.stop();
|
|
|
|
this.clearReconnectionTimeout();
|
|
|
|
}, RECONNECT_TIMEOUT_MS);
|
|
|
|
|
2021-09-25 02:11:53 +08:00
|
|
|
this.joinAudio({ isListenOnly: this.isListenOnly }, this.callback).then(
|
|
|
|
() => this.clearReconnectionTimeout(),
|
|
|
|
).catch((error) => {
|
2021-09-18 01:17:31 +08:00
|
|
|
// Error handling is a no-op because it will be "handled" in handleBrokerFailure
|
|
|
|
logger.debug({
|
2021-09-25 02:11:53 +08:00
|
|
|
logCode: 'fullaudio_reconnect_failed',
|
2021-09-18 01:17:31 +08:00
|
|
|
extraInfo: {
|
|
|
|
errorMessage: error.errorMessage,
|
|
|
|
reconnecting: this.reconnecting,
|
2021-09-25 02:11:53 +08:00
|
|
|
bridge: BRIDGE_NAME,
|
2021-09-18 01:17:31 +08:00
|
|
|
},
|
2021-09-25 02:11:53 +08:00
|
|
|
}, 'Fullaudio reconnect failed');
|
2021-09-18 01:17:31 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
handleBrokerFailure(error) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
mapErrorCode(error);
|
|
|
|
const { errorMessage, errorCause, errorCode } = error;
|
|
|
|
|
|
|
|
if (this.broker.started && !this.reconnecting) {
|
|
|
|
logger.error({
|
2021-09-25 02:11:53 +08:00
|
|
|
logCode: 'fullaudio_error_try_to_reconnect',
|
2021-09-18 01:17:31 +08:00
|
|
|
extraInfo: {
|
2021-09-25 02:11:53 +08:00
|
|
|
errorMessage,
|
|
|
|
errorCode,
|
|
|
|
errorCause,
|
|
|
|
bridge: BRIDGE_NAME,
|
2021-09-18 01:17:31 +08:00
|
|
|
},
|
2021-09-25 02:11:53 +08:00
|
|
|
}, 'Fullaudio failed, try to reconnect');
|
|
|
|
this.reconnect();
|
|
|
|
return resolve();
|
2021-09-18 01:17:31 +08:00
|
|
|
}
|
2021-09-25 02:11:53 +08:00
|
|
|
|
|
|
|
// Already tried reconnecting once OR the user handn't succesfully
|
|
|
|
// connected firsthand. Just finish the session and reject with error
|
|
|
|
logger.error({
|
|
|
|
logCode: 'fullaudio_error',
|
|
|
|
extraInfo: {
|
|
|
|
errorMessage,
|
|
|
|
errorCode,
|
|
|
|
errorCause,
|
|
|
|
reconnecting: this.reconnecting,
|
|
|
|
bridge: BRIDGE_NAME,
|
|
|
|
},
|
|
|
|
}, 'Fullaudio failed');
|
|
|
|
this.clearReconnectionTimeout();
|
|
|
|
this.broker.stop();
|
|
|
|
this.callback({
|
|
|
|
status: this.baseCallStates.failed,
|
|
|
|
error: errorCode,
|
|
|
|
bridgeError: errorMessage,
|
|
|
|
bridge: BRIDGE_NAME,
|
|
|
|
});
|
|
|
|
return reject(error);
|
2021-09-18 01:17:31 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
dispatchAutoplayHandlingEvent(mediaElement) {
|
|
|
|
const tagFailedEvent = new CustomEvent('audioPlayFailed', {
|
2021-09-25 02:11:53 +08:00
|
|
|
detail: { mediaElement },
|
2021-09-18 01:17:31 +08:00
|
|
|
});
|
|
|
|
window.dispatchEvent(tagFailedEvent);
|
|
|
|
this.callback({ status: this.baseCallStates.autoplayBlocked, bridge: BRIDGE_NAME });
|
|
|
|
}
|
|
|
|
|
|
|
|
handleStart() {
|
|
|
|
const stream = this.broker.webRtcPeer.getRemoteStream();
|
|
|
|
const mediaElement = document.getElementById(MEDIA_TAG);
|
|
|
|
|
2021-09-25 02:11:53 +08:00
|
|
|
return loadAndPlayMediaStream(stream, mediaElement, false).then(() => this
|
|
|
|
.callback({
|
|
|
|
status: this.baseCallStates.started,
|
|
|
|
bridge: BRIDGE_NAME,
|
|
|
|
})).catch((error) => {
|
2021-09-18 01:17:31 +08:00
|
|
|
// NotAllowedError equals autoplay issues, fire autoplay handling event.
|
|
|
|
// This will be handled in audio-manager.
|
|
|
|
if (error.name === 'NotAllowedError') {
|
|
|
|
logger.error({
|
2021-09-25 02:11:53 +08:00
|
|
|
logCode: 'fullaudio_error_autoplay',
|
2021-09-18 01:17:31 +08:00
|
|
|
extraInfo: { errorName: error.name, bridge: BRIDGE_NAME },
|
2021-09-25 02:11:53 +08:00
|
|
|
}, 'Fullaudio media play failed due to autoplay error');
|
2021-09-18 01:17:31 +08:00
|
|
|
this.dispatchAutoplayHandlingEvent(mediaElement);
|
|
|
|
} else {
|
|
|
|
const normalizedError = {
|
|
|
|
errorCode: 1004,
|
|
|
|
errorMessage: error.message || 'AUDIO_PLAY_FAILED',
|
|
|
|
};
|
|
|
|
this.callback({
|
|
|
|
status: this.baseCallStates.failed,
|
|
|
|
error: normalizedError.errorCode,
|
|
|
|
bridgeError: normalizedError.errorMessage,
|
|
|
|
bridge: BRIDGE_NAME,
|
2021-09-25 02:11:53 +08:00
|
|
|
});
|
2021-09-18 01:17:31 +08:00
|
|
|
throw normalizedError;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-09-25 02:11:53 +08:00
|
|
|
async _initBrokerEventsPromise() {
|
2021-09-18 01:17:31 +08:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
try {
|
2021-09-25 02:11:53 +08:00
|
|
|
if (!this.broker) resolve(null);
|
2021-09-18 01:17:31 +08:00
|
|
|
|
|
|
|
this.broker.onended = this.handleTermination.bind(this);
|
2021-09-25 02:11:53 +08:00
|
|
|
|
2021-09-18 01:17:31 +08:00
|
|
|
this.broker.onerror = (error) => {
|
|
|
|
this.handleBrokerFailure(error).catch(reject);
|
2021-09-25 02:11:53 +08:00
|
|
|
};
|
|
|
|
|
2021-09-18 01:17:31 +08:00
|
|
|
this.broker.onstart = () => {
|
|
|
|
this.handleStart().then(resolve).catch(reject);
|
|
|
|
};
|
2021-09-25 02:11:53 +08:00
|
|
|
} catch (error) {
|
|
|
|
reject(error);
|
2021-09-18 01:17:31 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-09-25 02:11:53 +08:00
|
|
|
async _startBroker(_options) {
|
|
|
|
try {
|
|
|
|
const { isListenOnly } = _options;
|
|
|
|
this.isListenOnly = isListenOnly;
|
|
|
|
|
|
|
|
const callerIdName = [
|
|
|
|
`${this.userId}_${getAudioSessionNumber()}`,
|
|
|
|
'bbbID',
|
|
|
|
isListenOnly ? `${GLOBAL_AUDIO_PREFIX}${this.voiceBridge}` : this.name,
|
|
|
|
].join('-').replace(/"/g, "'");
|
|
|
|
|
|
|
|
const options = {
|
|
|
|
userName: this.name,
|
|
|
|
caleeName: callerIdName,
|
|
|
|
iceServers: this.iceServers,
|
|
|
|
offering: OFFERING,
|
|
|
|
mediaServer: getMediaServerAdapter(),
|
|
|
|
};
|
|
|
|
|
|
|
|
this.broker = new FullAudioBroker(
|
|
|
|
Auth.authenticateURL(SFU_URL),
|
|
|
|
this.voiceBridge,
|
|
|
|
this.userId,
|
|
|
|
this.internalMeetingID,
|
|
|
|
isListenOnly ? RECV_ROLE : SENDRECV_ROLE,
|
|
|
|
options,
|
|
|
|
);
|
|
|
|
|
|
|
|
const initBrokerEventsPromise = this._initBrokerEventsPromise();
|
|
|
|
|
|
|
|
this.broker.listen();
|
|
|
|
|
|
|
|
return initBrokerEventsPromise;
|
|
|
|
} catch (error) {
|
|
|
|
logger.warn({ logCode: 'fullaudio_bridge_broker_init_fail' },
|
|
|
|
'Problem when initializing SFU broker for fullaudio bridge');
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async joinAudio(options, callback) {
|
|
|
|
try {
|
|
|
|
this.callback = callback;
|
|
|
|
|
|
|
|
this.iceServers = await fetchWebRTCMappedStunTurnServers(this.sessionToken);
|
|
|
|
} catch (error) {
|
|
|
|
logger.error({ logCode: 'fullaudio_stun-turn_fetch_failed' },
|
|
|
|
'SFU audio bridge failed to fetch STUN/TURN info, using default servers');
|
|
|
|
this.iceServers = getMappedFallbackStun();
|
|
|
|
} finally {
|
|
|
|
await this._startBroker(options);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// eslint-disable-next-line class-methods-use-this
|
|
|
|
async updateAudioConstraints() {
|
|
|
|
// TO BE IMPLEMENTED
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-09-18 01:17:31 +08:00
|
|
|
exitAudio() {
|
|
|
|
const mediaElement = document.getElementById(MEDIA_TAG);
|
|
|
|
|
|
|
|
this.broker.stop();
|
|
|
|
this.clearReconnectionTimeout();
|
|
|
|
|
|
|
|
if (mediaElement && typeof mediaElement.pause === 'function') {
|
|
|
|
mediaElement.pause();
|
|
|
|
mediaElement.srcObject = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Promise.resolve();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-25 02:11:53 +08:00
|
|
|
module.exports = FullAudioBridge;
|