2018-04-28 05:37:41 +08:00
|
|
|
import BaseAudioBridge from './base';
|
2018-07-19 05:04:20 +08:00
|
|
|
import Auth from '/imports/ui/services/auth';
|
2018-07-27 02:26:56 +08:00
|
|
|
import logger from '/imports/startup/client/logger';
|
2020-12-02 02:19:31 +08:00
|
|
|
import ListenOnlyBroker from '/imports/ui/services/bbb-webrtc-sfu/listenonly-broker';
|
|
|
|
import loadAndPlayMediaStream from '/imports/ui/services/bbb-webrtc-sfu/load-play';
|
|
|
|
import {
|
|
|
|
fetchWebRTCMappedStunTurnServers,
|
|
|
|
getMappedFallbackStun
|
|
|
|
} from '/imports/utils/fetchStunTurnServers';
|
2021-04-27 05:05:06 +08:00
|
|
|
import getFromMeetingSettings from '/imports/ui/services/meeting-settings';
|
2018-04-28 05:37:41 +08:00
|
|
|
|
2018-07-10 05:29:27 +08:00
|
|
|
const SFU_URL = Meteor.settings.public.kurento.wsUrl;
|
2021-04-27 02:55:07 +08:00
|
|
|
const DEFAULT_LISTENONLY_MEDIA_SERVER = Meteor.settings.public.kurento.listenOnlyMediaServer;
|
2021-09-24 03:32:14 +08:00
|
|
|
const SIGNAL_CANDIDATES = Meteor.settings.public.kurento.signalCandidates;
|
2018-04-29 08:18:54 +08:00
|
|
|
const MEDIA = Meteor.settings.public.media;
|
2018-05-08 01:17:48 +08:00
|
|
|
const MEDIA_TAG = MEDIA.mediaTag.replace(/#/g, '');
|
2019-04-11 04:48:10 +08:00
|
|
|
const GLOBAL_AUDIO_PREFIX = 'GLOBAL_AUDIO_';
|
2020-07-10 01:11:05 +08:00
|
|
|
const RECONNECT_TIMEOUT_MS = MEDIA.listenOnlyCallTimeout || 15000;
|
2021-08-12 03:37:04 +08:00
|
|
|
const OFFERING = MEDIA.listenOnlyOffering;
|
2020-12-02 02:19:31 +08:00
|
|
|
const RECV_ROLE = 'recv';
|
|
|
|
const BRIDGE_NAME = 'kurento';
|
|
|
|
|
|
|
|
// 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,
|
|
|
|
}
|
|
|
|
const mapErrorCode = (error) => {
|
|
|
|
const { errorCode } = error;
|
|
|
|
const mappedErrorCode = errorCodeMap[errorCode];
|
|
|
|
if (errorCode == null || mappedErrorCode == null) return error;
|
|
|
|
error.errorCode = mappedErrorCode;
|
|
|
|
return error;
|
|
|
|
}
|
2018-04-29 08:18:54 +08:00
|
|
|
|
2021-04-27 02:55:07 +08:00
|
|
|
// TODO Would be interesting to move this to a service along with the error mapping
|
|
|
|
const getMediaServerAdapter = () => {
|
2021-04-27 05:05:06 +08:00
|
|
|
return getFromMeetingSettings('media-server-listenonly', DEFAULT_LISTENONLY_MEDIA_SERVER);
|
|
|
|
};
|
2021-04-27 02:55:07 +08:00
|
|
|
|
2018-04-28 05:37:41 +08:00
|
|
|
export default class KurentoAudioBridge extends BaseAudioBridge {
|
|
|
|
constructor(userData) {
|
|
|
|
super();
|
2020-12-02 02:19:31 +08:00
|
|
|
this.internalMeetingID = userData.meetingId;
|
|
|
|
this.voiceBridge = userData.voiceBridge;
|
|
|
|
this.userId = userData.userId;
|
|
|
|
this.name = userData.username;
|
|
|
|
this.sessionToken = userData.sessionToken;
|
2018-08-30 03:12:34 +08:00
|
|
|
this.media = {
|
|
|
|
inputDevice: {},
|
|
|
|
};
|
2020-12-02 02:19:31 +08:00
|
|
|
this.broker;
|
|
|
|
this.reconnecting = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
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({
|
|
|
|
logCode: 'listenonly_changeoutputdevice_error',
|
|
|
|
extraInfo: { error, bridge: BRIDGE_NAME }
|
|
|
|
}, 'Audio bridge failed to change output device');
|
|
|
|
throw new Error(this.baseErrorCodes.MEDIA_ERROR);
|
|
|
|
}
|
|
|
|
}
|
2018-08-30 03:12:34 +08:00
|
|
|
|
2020-12-02 02:19:31 +08:00
|
|
|
return this.media.outputDeviceId || value;
|
|
|
|
}
|
2018-08-30 03:12:34 +08:00
|
|
|
|
2020-12-02 02:19:31 +08:00
|
|
|
getPeerConnection() {
|
2021-08-13 03:39:04 +08:00
|
|
|
if (!this.broker) return null;
|
|
|
|
|
2020-12-02 02:19:31 +08:00
|
|
|
const webRtcPeer = this.broker.webRtcPeer;
|
|
|
|
if (webRtcPeer) return webRtcPeer.peerConnection;
|
|
|
|
return null;
|
2018-04-28 05:37:41 +08:00
|
|
|
}
|
|
|
|
|
2020-12-02 02:19:31 +08:00
|
|
|
handleTermination() {
|
|
|
|
return this.callback({ status: this.baseCallStates.ended, bridge: BRIDGE_NAME });
|
|
|
|
}
|
2019-12-03 06:15:46 +08:00
|
|
|
|
2020-12-02 02:19:31 +08:00
|
|
|
clearReconnectionTimeout() {
|
|
|
|
this.reconnecting = false;
|
|
|
|
if (this.reconnectionTimeout) {
|
|
|
|
clearTimeout(this.reconnectionTimeout);
|
|
|
|
this.reconnectionTimeout = null;
|
2019-12-03 06:15:46 +08:00
|
|
|
}
|
2020-12-02 02:19:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
this.joinAudio({ isListenOnly: true }, this.callback).then(() => {
|
|
|
|
this.clearReconnectionTimeout();
|
|
|
|
}).catch(error => {
|
|
|
|
// Error handling is a no-op because it will be "handled" in handleBrokerFailure
|
|
|
|
logger.debug({
|
|
|
|
logCode: 'listenonly_reconnect_failed',
|
|
|
|
extraInfo: {
|
|
|
|
errorMessage: error.errorMessage,
|
|
|
|
reconnecting: this.reconnecting,
|
|
|
|
bridge: BRIDGE_NAME
|
|
|
|
},
|
|
|
|
}, 'Listen only reconnect failed');
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
handleBrokerFailure(error) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
mapErrorCode(error);
|
|
|
|
const { errorMessage, errorCause, errorCode } = error;
|
|
|
|
|
|
|
|
if (this.broker.started && !this.reconnecting) {
|
|
|
|
logger.error({
|
|
|
|
logCode: 'listenonly_error_try_to_reconnect',
|
|
|
|
extraInfo: { errorMessage, errorCode, errorCause, bridge: BRIDGE_NAME },
|
|
|
|
}, 'Listen only failed, try to reconnect');
|
|
|
|
this.reconnect();
|
|
|
|
return resolve();
|
|
|
|
} else {
|
|
|
|
// Already tried reconnecting once OR the user handn't succesfully
|
|
|
|
// connected firsthand. Just finish the session and reject with error
|
|
|
|
logger.error({
|
|
|
|
logCode: 'listenonly_error',
|
|
|
|
extraInfo: {
|
|
|
|
errorMessage, errorCode, errorCause,
|
|
|
|
reconnecting: this.reconnecting,
|
|
|
|
bridge: BRIDGE_NAME
|
|
|
|
},
|
|
|
|
}, 'Listen only failed');
|
|
|
|
this.clearReconnectionTimeout();
|
|
|
|
this.broker.stop();
|
|
|
|
this.callback({
|
|
|
|
status: this.baseCallStates.failed,
|
|
|
|
error: errorCode,
|
|
|
|
bridgeError: errorMessage,
|
|
|
|
bridge: BRIDGE_NAME,
|
|
|
|
});
|
|
|
|
return reject(error);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2019-09-07 02:58:22 +08:00
|
|
|
|
2020-12-02 02:19:31 +08:00
|
|
|
dispatchAutoplayHandlingEvent(mediaElement) {
|
|
|
|
const tagFailedEvent = new CustomEvent('audioPlayFailed', {
|
|
|
|
detail: { mediaElement }
|
|
|
|
});
|
|
|
|
window.dispatchEvent(tagFailedEvent);
|
|
|
|
this.callback({ status: this.baseCallStates.autoplayBlocked, bridge: BRIDGE_NAME });
|
2019-09-07 02:58:22 +08:00
|
|
|
}
|
|
|
|
|
2020-12-02 02:19:31 +08:00
|
|
|
handleStart() {
|
|
|
|
const stream = this.broker.webRtcPeer.getRemoteStream();
|
|
|
|
const mediaElement = document.getElementById(MEDIA_TAG);
|
|
|
|
|
|
|
|
return loadAndPlayMediaStream(stream, mediaElement, false).then(() => {
|
|
|
|
return this.callback({ status: this.baseCallStates.started, bridge: BRIDGE_NAME });
|
|
|
|
}).catch(error => {
|
|
|
|
// NotAllowedError equals autoplay issues, fire autoplay handling event.
|
|
|
|
// This will be handled in audio-manager.
|
|
|
|
if (error.name === 'NotAllowedError') {
|
|
|
|
logger.error({
|
|
|
|
logCode: 'listenonly_error_autoplay',
|
|
|
|
extraInfo: { errorName: error.name, bridge: BRIDGE_NAME },
|
|
|
|
}, 'Listen only media play failed due to autoplay error');
|
|
|
|
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,
|
|
|
|
})
|
|
|
|
throw normalizedError;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2019-09-11 00:20:40 +08:00
|
|
|
|
2021-06-30 01:47:59 +08:00
|
|
|
trickleIce() {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
try {
|
|
|
|
fetchWebRTCMappedStunTurnServers(this.sessionToken)
|
|
|
|
.then((iceServers) => {
|
|
|
|
const options = {
|
|
|
|
userName: this.name,
|
|
|
|
caleeName: `${GLOBAL_AUDIO_PREFIX}${this.voiceBridge}`,
|
|
|
|
iceServers,
|
2021-08-12 03:37:04 +08:00
|
|
|
offering: OFFERING,
|
2021-06-30 01:47:59 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
this.broker = new ListenOnlyBroker(
|
|
|
|
Auth.authenticateURL(SFU_URL),
|
|
|
|
this.voiceBridge,
|
|
|
|
this.userId,
|
|
|
|
this.internalMeetingID,
|
|
|
|
RECV_ROLE,
|
|
|
|
options,
|
|
|
|
);
|
|
|
|
|
|
|
|
this.broker.onstart = () => {
|
|
|
|
const { peerConnection } = this.broker.webRtcPeer;
|
|
|
|
|
|
|
|
if (!peerConnection) return resolve(null);
|
|
|
|
|
|
|
|
const selectedCandidatePair = peerConnection.getReceivers()[0]
|
|
|
|
.transport.iceTransport.getSelectedCandidatePair();
|
|
|
|
|
|
|
|
const validIceCandidate = [selectedCandidatePair.local];
|
|
|
|
|
|
|
|
this.broker.stop();
|
|
|
|
return resolve(validIceCandidate);
|
|
|
|
};
|
|
|
|
|
|
|
|
this.broker.listen();
|
|
|
|
});
|
|
|
|
} catch (e) {
|
|
|
|
reject(e);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-12-02 02:19:31 +08:00
|
|
|
joinAudio({ isListenOnly }, callback) {
|
2018-07-10 05:29:27 +08:00
|
|
|
return new Promise(async (resolve, reject) => {
|
2020-12-02 02:19:31 +08:00
|
|
|
if (!isListenOnly) return reject(new Error('Invalid bridge option'));
|
2018-04-29 08:18:54 +08:00
|
|
|
this.callback = callback;
|
2018-07-10 05:29:27 +08:00
|
|
|
let iceServers = [];
|
2018-05-07 21:39:39 +08:00
|
|
|
|
2018-07-10 05:29:27 +08:00
|
|
|
try {
|
2020-12-02 02:19:31 +08:00
|
|
|
iceServers = await fetchWebRTCMappedStunTurnServers(this.sessionToken);
|
2018-07-10 05:29:27 +08:00
|
|
|
} catch (error) {
|
2020-12-02 02:19:31 +08:00
|
|
|
logger.error({ logCode: 'listenonly_stunturn_fetch_failed' },
|
2019-07-16 04:59:00 +08:00
|
|
|
'SFU audio bridge failed to fetch STUN/TURN info, using default servers');
|
2020-05-21 12:20:46 +08:00
|
|
|
iceServers = getMappedFallbackStun();
|
2018-07-10 05:29:27 +08:00
|
|
|
} finally {
|
|
|
|
const options = {
|
2020-12-02 02:19:31 +08:00
|
|
|
userName: this.name,
|
2018-07-10 05:29:27 +08:00
|
|
|
caleeName: `${GLOBAL_AUDIO_PREFIX}${this.voiceBridge}`,
|
|
|
|
iceServers,
|
2021-08-12 03:37:04 +08:00
|
|
|
offering: OFFERING,
|
2021-04-27 02:55:07 +08:00
|
|
|
mediaServer: getMediaServerAdapter(),
|
2021-09-24 03:32:14 +08:00
|
|
|
signalCandidates: SIGNAL_CANDIDATES,
|
2019-09-07 02:58:22 +08:00
|
|
|
};
|
|
|
|
|
2020-12-02 02:19:31 +08:00
|
|
|
this.broker = new ListenOnlyBroker(
|
|
|
|
Auth.authenticateURL(SFU_URL),
|
2018-07-10 05:29:27 +08:00
|
|
|
this.voiceBridge,
|
2020-12-02 02:19:31 +08:00
|
|
|
this.userId,
|
2018-07-10 05:29:27 +08:00
|
|
|
this.internalMeetingID,
|
2020-12-02 02:19:31 +08:00
|
|
|
RECV_ROLE,
|
2019-04-11 04:48:10 +08:00
|
|
|
options,
|
2018-07-10 05:29:27 +08:00
|
|
|
);
|
2018-08-30 03:12:34 +08:00
|
|
|
|
2020-12-02 02:19:31 +08:00
|
|
|
this.broker.onended = this.handleTermination.bind(this);
|
|
|
|
this.broker.onerror = (error) => {
|
|
|
|
this.handleBrokerFailure(error).catch(reject);
|
|
|
|
}
|
|
|
|
this.broker.onstart = () => {
|
|
|
|
this.handleStart().then(resolve).catch(reject);
|
|
|
|
};
|
2018-08-30 03:12:34 +08:00
|
|
|
|
2020-12-02 02:19:31 +08:00
|
|
|
this.broker.listen().catch(reject);
|
|
|
|
}
|
|
|
|
});
|
2019-11-30 05:48:04 +08:00
|
|
|
}
|
2018-08-30 03:12:34 +08:00
|
|
|
|
2018-04-29 08:18:54 +08:00
|
|
|
exitAudio() {
|
2021-07-23 10:35:47 +08:00
|
|
|
const mediaElement = document.getElementById(MEDIA_TAG);
|
|
|
|
|
2020-12-02 02:19:31 +08:00
|
|
|
this.broker.stop();
|
|
|
|
this.clearReconnectionTimeout();
|
2021-07-23 10:35:47 +08:00
|
|
|
|
|
|
|
if (mediaElement && typeof mediaElement.pause === 'function') {
|
|
|
|
mediaElement.pause();
|
|
|
|
mediaElement.srcObject = null;
|
|
|
|
}
|
|
|
|
|
2020-12-02 02:19:31 +08:00
|
|
|
return Promise.resolve();
|
2018-04-28 05:37:41 +08:00
|
|
|
}
|
|
|
|
}
|
2021-09-01 02:50:53 +08:00
|
|
|
|
|
|
|
module.exports = KurentoAudioBridge;
|