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';
|
2022-04-20 22:13:37 +08:00
|
|
|
import AudioBroker from '/imports/ui/services/bbb-webrtc-sfu/audio-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';
|
2022-02-01 03:37:40 +08:00
|
|
|
import Storage from '/imports/ui/services/storage/session';
|
2022-02-02 03:29:21 +08:00
|
|
|
import browserInfo from '/imports/utils/browserInfo';
|
2022-02-01 03:37:40 +08:00
|
|
|
import {
|
2022-04-08 03:45:17 +08:00
|
|
|
DEFAULT_INPUT_DEVICE_ID,
|
2022-02-01 03:37:40 +08:00
|
|
|
DEFAULT_OUTPUT_DEVICE_ID,
|
|
|
|
INPUT_DEVICE_ID_KEY,
|
|
|
|
OUTPUT_DEVICE_ID_KEY,
|
|
|
|
getAudioSessionNumber,
|
|
|
|
getAudioConstraints,
|
2022-02-02 03:29:21 +08:00
|
|
|
filterSupportedConstraints,
|
2022-02-01 03:37:40 +08:00
|
|
|
} from '/imports/api/audio/client/bridge/service';
|
2022-03-11 01:31:42 +08:00
|
|
|
import { shouldForceRelay } from '/imports/ui/services/bbb-webrtc-sfu/utils';
|
2022-02-02 03:29:21 +08:00
|
|
|
|
2021-09-18 01:17:31 +08:00
|
|
|
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;
|
2022-04-20 03:16:22 +08:00
|
|
|
const DEFAULT_LISTENONLY_MEDIA_SERVER = Meteor.settings.public.kurento.listenOnlyMediaServer;
|
2022-04-26 00:22:49 +08:00
|
|
|
const LISTEN_ONLY_OFFERING = MEDIA.listenOnlyOffering;
|
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;
|
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';
|
2022-02-02 03:29:21 +08:00
|
|
|
const IS_CHROME = browserInfo.isChrome;
|
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
|
|
|
|
2022-04-20 03:16:22 +08:00
|
|
|
const getMediaServerAdapter = (listenOnly = false) => {
|
|
|
|
if (listenOnly) {
|
|
|
|
return getFromMeetingSettings(
|
|
|
|
'media-server-listenonly',
|
|
|
|
DEFAULT_LISTENONLY_MEDIA_SERVER,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return getFromMeetingSettings(
|
|
|
|
'media-server-fullaudio',
|
|
|
|
DEFAULT_FULLAUDIO_MEDIA_SERVER,
|
|
|
|
);
|
|
|
|
};
|
2021-09-25 02:11:53 +08:00
|
|
|
|
2022-04-21 04:38:52 +08:00
|
|
|
export default class SFUAudioBridge extends BaseAudioBridge {
|
2021-09-18 01:17:31 +08:00
|
|
|
constructor(userData) {
|
|
|
|
super();
|
|
|
|
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 = [];
|
2022-01-26 04:51:02 +08:00
|
|
|
this.inEchoTest = false;
|
|
|
|
this.bridgeName = BRIDGE_NAME;
|
2021-09-18 01:17:31 +08:00
|
|
|
}
|
|
|
|
|
2022-02-01 03:37:40 +08:00
|
|
|
get inputDeviceId() {
|
|
|
|
const sessionInputDeviceId = Storage.getItem(INPUT_DEVICE_ID_KEY);
|
|
|
|
|
|
|
|
if (sessionInputDeviceId) {
|
|
|
|
return sessionInputDeviceId;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.media.inputDeviceId) {
|
|
|
|
return this.media.inputDeviceId;
|
|
|
|
}
|
|
|
|
|
2022-04-08 03:45:17 +08:00
|
|
|
return DEFAULT_INPUT_DEVICE_ID;
|
2022-02-01 03:37:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
set inputDeviceId(deviceId) {
|
|
|
|
Storage.setItem(INPUT_DEVICE_ID_KEY, deviceId);
|
|
|
|
this.media.inputDeviceId = deviceId;
|
|
|
|
}
|
|
|
|
|
|
|
|
get outputDeviceId() {
|
|
|
|
const sessionOutputDeviceId = Storage.getItem(OUTPUT_DEVICE_ID_KEY);
|
|
|
|
if (sessionOutputDeviceId) {
|
|
|
|
return sessionOutputDeviceId;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.media.outputDeviceId) {
|
|
|
|
return this.media.outputDeviceId;
|
|
|
|
}
|
|
|
|
|
|
|
|
return DEFAULT_OUTPUT_DEVICE_ID;
|
|
|
|
}
|
|
|
|
|
|
|
|
set outputDeviceId(deviceId) {
|
|
|
|
Storage.setItem(OUTPUT_DEVICE_ID_KEY, deviceId);
|
|
|
|
this.media.outputDeviceId = deviceId;
|
|
|
|
}
|
|
|
|
|
|
|
|
get inputStream() {
|
|
|
|
if (this.broker) {
|
|
|
|
return this.broker.getLocalStream();
|
2021-09-18 01:17:31 +08:00
|
|
|
}
|
|
|
|
|
2022-02-01 03:37:40 +08:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2022-04-20 22:13:37 +08:00
|
|
|
get role() {
|
|
|
|
return this.broker?.role;
|
|
|
|
}
|
|
|
|
|
2022-02-01 03:37:40 +08:00
|
|
|
async setInputStream(stream) {
|
|
|
|
try {
|
|
|
|
if (this.broker == null) return null;
|
|
|
|
|
|
|
|
await this.broker.setLocalStream(stream);
|
|
|
|
|
|
|
|
return stream;
|
|
|
|
} catch (error) {
|
|
|
|
logger.warn({
|
2022-04-20 22:13:37 +08:00
|
|
|
logCode: 'sfuaudio_setinputstream_error',
|
2022-02-01 03:37:40 +08:00
|
|
|
extraInfo: {
|
|
|
|
errorCode: error.code,
|
|
|
|
errorMessage: error.message,
|
|
|
|
bridgeName: this.bridgeName,
|
2022-04-20 22:13:37 +08:00
|
|
|
role: this.role,
|
2022-02-01 03:37:40 +08:00
|
|
|
},
|
|
|
|
}, 'Failed to set input stream (mic)');
|
|
|
|
return null;
|
|
|
|
}
|
2021-09-18 01:17:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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() {
|
2022-01-26 04:51:02 +08:00
|
|
|
return this.callback({ status: this.baseCallStates.ended, bridge: this.bridgeName });
|
2021-09-18 01:17:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
clearReconnectionTimeout() {
|
|
|
|
this.reconnecting = false;
|
|
|
|
if (this.reconnectionTimeout) {
|
|
|
|
clearTimeout(this.reconnectionTimeout);
|
|
|
|
this.reconnectionTimeout = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
reconnect() {
|
|
|
|
this.broker.stop();
|
2022-01-26 04:51:02 +08:00
|
|
|
this.callback({ status: this.baseCallStates.reconnecting, bridge: this.bridgeName });
|
2021-09-18 01:17:31 +08:00
|
|
|
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',
|
2022-01-26 04:51:02 +08:00
|
|
|
bridge: this.bridgeName,
|
2021-09-18 01:17:31 +08:00
|
|
|
});
|
|
|
|
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({
|
2022-04-20 22:13:37 +08:00
|
|
|
logCode: 'sfuaudio_reconnect_failed',
|
2021-09-18 01:17:31 +08:00
|
|
|
extraInfo: {
|
|
|
|
errorMessage: error.errorMessage,
|
|
|
|
reconnecting: this.reconnecting,
|
2022-01-26 04:51:02 +08:00
|
|
|
bridge: this.bridgeName,
|
2022-04-20 22:13:37 +08:00
|
|
|
role: this.role,
|
2021-09-18 01:17:31 +08:00
|
|
|
},
|
2022-04-20 22:13:37 +08:00
|
|
|
}, 'SFU audio 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({
|
2022-04-20 22:13:37 +08:00
|
|
|
logCode: 'sfuaudio_error_try_to_reconnect',
|
2021-09-18 01:17:31 +08:00
|
|
|
extraInfo: {
|
2021-09-25 02:11:53 +08:00
|
|
|
errorMessage,
|
|
|
|
errorCode,
|
|
|
|
errorCause,
|
2022-01-26 04:51:02 +08:00
|
|
|
bridge: this.bridgeName,
|
2022-04-20 22:13:37 +08:00
|
|
|
role: this.role,
|
2021-09-18 01:17:31 +08:00
|
|
|
},
|
2022-04-20 22:13:37 +08:00
|
|
|
}, 'SFU audio failed, try to reconnect');
|
2021-09-25 02:11:53 +08:00
|
|
|
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({
|
2022-04-20 22:13:37 +08:00
|
|
|
logCode: 'sfuaudio_error',
|
2021-09-25 02:11:53 +08:00
|
|
|
extraInfo: {
|
|
|
|
errorMessage,
|
|
|
|
errorCode,
|
|
|
|
errorCause,
|
|
|
|
reconnecting: this.reconnecting,
|
2022-01-26 04:51:02 +08:00
|
|
|
bridge: this.bridgeName,
|
2022-04-20 22:13:37 +08:00
|
|
|
role: this.role,
|
2021-09-25 02:11:53 +08:00
|
|
|
},
|
2022-04-20 22:13:37 +08:00
|
|
|
}, 'SFU audio failed');
|
2021-09-25 02:11:53 +08:00
|
|
|
this.clearReconnectionTimeout();
|
|
|
|
this.broker.stop();
|
|
|
|
this.callback({
|
|
|
|
status: this.baseCallStates.failed,
|
|
|
|
error: errorCode,
|
|
|
|
bridgeError: errorMessage,
|
2022-01-26 04:51:02 +08:00
|
|
|
bridge: this.bridgeName,
|
2021-09-25 02:11:53 +08:00
|
|
|
});
|
|
|
|
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);
|
2022-01-26 04:51:02 +08:00
|
|
|
this.callback({ status: this.baseCallStates.autoplayBlocked, bridge: this.bridgeName });
|
2021-09-18 01:17:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
2022-01-26 04:51:02 +08:00
|
|
|
bridge: this.bridgeName,
|
2021-09-25 02:11:53 +08:00
|
|
|
})).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({
|
2022-04-20 22:13:37 +08:00
|
|
|
logCode: 'sfuaudio_error_autoplay',
|
|
|
|
extraInfo: {
|
|
|
|
errorName: error.name,
|
|
|
|
bridge: this.bridgeName,
|
|
|
|
role: this.role,
|
|
|
|
},
|
|
|
|
}, 'SFU audio 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,
|
2022-01-26 04:51:02 +08:00
|
|
|
bridge: this.bridgeName,
|
2021-09-25 02:11:53 +08:00
|
|
|
});
|
2021-09-18 01:17:31 +08:00
|
|
|
throw normalizedError;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-04-21 01:40:06 +08:00
|
|
|
async _startBroker(options) {
|
2021-09-18 01:17:31 +08:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
try {
|
2022-04-21 01:40:06 +08:00
|
|
|
const { isListenOnly, extension, inputStream } = options;
|
|
|
|
this.inEchoTest = !!extension;
|
|
|
|
this.isListenOnly = isListenOnly;
|
|
|
|
const callerIdName = [
|
|
|
|
`${this.userId}_${getAudioSessionNumber()}`,
|
|
|
|
'bbbID',
|
|
|
|
isListenOnly ? `${GLOBAL_AUDIO_PREFIX}` : this.name,
|
|
|
|
].join('-').replace(/"/g, "'");
|
|
|
|
|
|
|
|
const brokerOptions = {
|
|
|
|
caleeName: callerIdName,
|
|
|
|
extension,
|
|
|
|
iceServers: this.iceServers,
|
|
|
|
mediaServer: getMediaServerAdapter(isListenOnly),
|
|
|
|
constraints: getAudioConstraints({ deviceId: this.inputDeviceId }),
|
|
|
|
forceRelay: shouldForceRelay(),
|
|
|
|
stream: (inputStream && inputStream.active) ? inputStream : undefined,
|
2022-04-26 00:22:49 +08:00
|
|
|
offering: isListenOnly ? LISTEN_ONLY_OFFERING : true,
|
2022-04-21 01:40:06 +08:00
|
|
|
};
|
2021-09-18 01:17:31 +08:00
|
|
|
|
2022-04-21 01:40:06 +08:00
|
|
|
this.broker = new AudioBroker(
|
|
|
|
Auth.authenticateURL(SFU_URL),
|
|
|
|
isListenOnly ? RECV_ROLE : SENDRECV_ROLE,
|
|
|
|
brokerOptions,
|
|
|
|
);
|
2021-09-25 02:11:53 +08:00
|
|
|
|
2022-04-21 01:40:06 +08:00
|
|
|
this.broker.onended = this.handleTermination.bind(this);
|
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);
|
|
|
|
};
|
2022-04-21 01:40:06 +08:00
|
|
|
|
|
|
|
this.broker.joinAudio().catch(reject);
|
2021-09-25 02:11:53 +08:00
|
|
|
} catch (error) {
|
2022-04-21 01:40:06 +08:00
|
|
|
logger.warn({ logCode: 'sfuaudio_bridge_broker_init_fail' },
|
|
|
|
'Problem when initializing SFU broker for fullaudio bridge');
|
2021-09-25 02:11:53 +08:00
|
|
|
reject(error);
|
2021-09-18 01:17:31 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-09-25 02:11:53 +08:00
|
|
|
async joinAudio(options, callback) {
|
2022-04-20 22:13:37 +08:00
|
|
|
this.callback = callback;
|
|
|
|
|
2021-09-25 02:11:53 +08:00
|
|
|
try {
|
|
|
|
this.iceServers = await fetchWebRTCMappedStunTurnServers(this.sessionToken);
|
|
|
|
} catch (error) {
|
2022-04-20 22:13:37 +08:00
|
|
|
logger.error({ logCode: 'sfuaudio_stun-turn_fetch_failed' },
|
2021-09-25 02:11:53 +08:00
|
|
|
'SFU audio bridge failed to fetch STUN/TURN info, using default servers');
|
|
|
|
this.iceServers = getMappedFallbackStun();
|
|
|
|
}
|
2022-04-20 22:13:37 +08:00
|
|
|
|
|
|
|
return this._startBroker(options);
|
2021-09-25 02:11:53 +08:00
|
|
|
}
|
|
|
|
|
2022-01-26 04:51:02 +08:00
|
|
|
sendDtmf(tones) {
|
|
|
|
if (this.broker) {
|
|
|
|
this.broker.dtmf(tones);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
transferCall(onTransferSuccess) {
|
|
|
|
this.inEchoTest = false;
|
|
|
|
return this.trackTransferState(onTransferSuccess);
|
|
|
|
}
|
|
|
|
|
2022-02-01 03:37:40 +08:00
|
|
|
async liveChangeInputDevice(deviceId) {
|
|
|
|
try {
|
|
|
|
const constraints = {
|
|
|
|
audio: getAudioConstraints({ deviceId }),
|
|
|
|
};
|
|
|
|
|
|
|
|
this.inputStream.getAudioTracks().forEach((t) => t.stop());
|
|
|
|
const updatedStream = await navigator.mediaDevices.getUserMedia(constraints);
|
|
|
|
await this.setInputStream(updatedStream);
|
|
|
|
this.inputDeviceId = deviceId;
|
|
|
|
|
|
|
|
return updatedStream;
|
|
|
|
} catch (error) {
|
|
|
|
logger.warn({
|
2022-04-20 22:13:37 +08:00
|
|
|
logCode: 'sfuaudio_livechangeinputdevice_error',
|
2022-02-01 03:37:40 +08:00
|
|
|
extraInfo: {
|
|
|
|
errorCode: error.code,
|
|
|
|
errorMessage: error.message,
|
|
|
|
bridgeName: this.bridgeName,
|
2022-04-20 22:13:37 +08:00
|
|
|
role: this.role,
|
2022-02-01 03:37:40 +08:00
|
|
|
},
|
|
|
|
}, 'Failed to change input device (mic)');
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-02 03:29:21 +08:00
|
|
|
async updateAudioConstraints(constraints) {
|
|
|
|
try {
|
|
|
|
if (typeof constraints !== 'object') return;
|
|
|
|
|
|
|
|
const matchConstraints = filterSupportedConstraints(constraints);
|
|
|
|
|
|
|
|
if (IS_CHROME) {
|
|
|
|
matchConstraints.deviceId = this.inputDeviceId;
|
|
|
|
const stream = await navigator.mediaDevices.getUserMedia(
|
|
|
|
{ audio: matchConstraints },
|
|
|
|
);
|
|
|
|
this.setInputStream(stream);
|
|
|
|
} else {
|
2022-04-20 03:18:04 +08:00
|
|
|
this.inputStream.getAudioTracks()
|
|
|
|
.forEach((track) => track.applyConstraints(matchConstraints));
|
2022-02-02 03:29:21 +08:00
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
logger.error({
|
2022-04-20 22:13:37 +08:00
|
|
|
logCode: 'sfuaudio_audio_constraint_error',
|
2022-02-02 03:29:21 +08:00
|
|
|
extraInfo: {
|
|
|
|
errorCode: error.code,
|
|
|
|
errorMessage: error.message,
|
|
|
|
bridgeName: this.bridgeName,
|
2022-04-20 22:13:37 +08:00
|
|
|
role: this.role,
|
2022-02-02 03:29:21 +08:00
|
|
|
},
|
|
|
|
}, 'Failed to update audio constraint');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-26 00:34:39 +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,
|
|
|
|
offering: LISTEN_ONLY_OFFERING,
|
|
|
|
};
|
|
|
|
|
|
|
|
this.broker = new AudioBroker(
|
|
|
|
Auth.authenticateURL(SFU_URL),
|
|
|
|
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.joinAudio().catch(reject);
|
|
|
|
});
|
|
|
|
} catch (error) {
|
|
|
|
// Rollback
|
|
|
|
this.exitAudio();
|
|
|
|
reject(error);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-09-18 01:17:31 +08:00
|
|
|
exitAudio() {
|
|
|
|
const mediaElement = document.getElementById(MEDIA_TAG);
|
|
|
|
|
|
|
|
this.clearReconnectionTimeout();
|
2022-03-17 22:20:19 +08:00
|
|
|
this.broker.stop();
|
|
|
|
this.broker = null;
|
2021-09-18 01:17:31 +08:00
|
|
|
|
|
|
|
if (mediaElement && typeof mediaElement.pause === 'function') {
|
|
|
|
mediaElement.pause();
|
|
|
|
mediaElement.srcObject = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Promise.resolve();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-21 04:38:52 +08:00
|
|
|
module.exports = SFUAudioBridge;
|