2022-04-13 09:51:07 +08:00
|
|
|
import LocalPCLoopback from '/imports/ui/services/webrtc-base/local-pc-loopback';
|
|
|
|
import browserInfo from '/imports/utils/browserInfo';
|
|
|
|
|
2024-03-07 01:28:18 +08:00
|
|
|
const MEDIA_TAG = window.meetingClientSettings.public.media.mediaTag;
|
|
|
|
const USE_RTC_LOOPBACK_CHR = window.meetingClientSettings.public.media.localEchoTest.useRtcLoopbackInChromium;
|
2023-01-24 03:31:53 +08:00
|
|
|
const {
|
|
|
|
enabled: DELAY_ENABLED = true,
|
|
|
|
delayTime = 0.5,
|
|
|
|
maxDelayTime = 2,
|
2024-03-07 01:28:18 +08:00
|
|
|
} = window.meetingClientSettings.public.media.localEchoTest.delay;
|
2023-01-24 03:31:53 +08:00
|
|
|
|
|
|
|
let audioContext = null;
|
|
|
|
let sourceContext = null;
|
2023-03-21 04:38:06 +08:00
|
|
|
let contextDestination = null;
|
|
|
|
let stubAudioElement = null;
|
2023-01-24 03:31:53 +08:00
|
|
|
let delayNode = null;
|
2022-04-13 09:51:07 +08:00
|
|
|
|
|
|
|
const useRTCLoopback = () => (browserInfo.isChrome || browserInfo.isEdge) && USE_RTC_LOOPBACK_CHR;
|
|
|
|
const createAudioRTCLoopback = () => new LocalPCLoopback({ audio: true });
|
|
|
|
|
2023-01-24 03:31:53 +08:00
|
|
|
const cleanupDelayNode = () => {
|
|
|
|
if (delayNode) {
|
|
|
|
delayNode.disconnect();
|
|
|
|
delayNode = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sourceContext) {
|
|
|
|
sourceContext.disconnect();
|
|
|
|
sourceContext = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (audioContext) {
|
|
|
|
audioContext.close();
|
|
|
|
audioContext = null;
|
|
|
|
}
|
2023-03-21 04:38:06 +08:00
|
|
|
|
|
|
|
if (contextDestination) {
|
|
|
|
contextDestination.disconnect();
|
|
|
|
contextDestination = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (stubAudioElement) {
|
|
|
|
stubAudioElement.pause();
|
|
|
|
stubAudioElement.srcObject = null;
|
|
|
|
stubAudioElement = null;
|
|
|
|
}
|
2023-01-24 03:31:53 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
const addDelayNode = (stream) => {
|
|
|
|
if (stream) {
|
|
|
|
if (delayNode || audioContext || sourceContext) cleanupDelayNode();
|
2023-03-21 04:38:06 +08:00
|
|
|
const audioElement = document.querySelector(MEDIA_TAG);
|
|
|
|
// Workaround: attach the stream to a muted stub audio element to be able to play it in
|
|
|
|
// Chromium-based browsers. See https://bugs.chromium.org/p/chromium/issues/detail?id=933677
|
|
|
|
stubAudioElement = new Audio();
|
|
|
|
stubAudioElement.muted = true;
|
|
|
|
stubAudioElement.srcObject = stream;
|
2023-01-24 03:31:53 +08:00
|
|
|
|
2023-03-21 04:38:06 +08:00
|
|
|
// Create a new AudioContext to be able to add a delay to the stream
|
2023-01-24 03:31:53 +08:00
|
|
|
audioContext = new AudioContext();
|
|
|
|
sourceContext = audioContext.createMediaStreamSource(stream);
|
2023-03-21 04:38:06 +08:00
|
|
|
contextDestination = audioContext.createMediaStreamDestination();
|
|
|
|
// Create a DelayNode to add a delay to the stream
|
2023-01-24 03:31:53 +08:00
|
|
|
delayNode = new DelayNode(audioContext, { delayTime, maxDelayTime });
|
2023-03-21 04:38:06 +08:00
|
|
|
// Connect the stream to the DelayNode and then to the MediaStreamDestinationNode
|
|
|
|
// to be able to play the stream.
|
2023-01-24 03:31:53 +08:00
|
|
|
sourceContext.connect(delayNode);
|
2023-03-21 04:38:06 +08:00
|
|
|
delayNode.connect(contextDestination);
|
2023-01-24 03:31:53 +08:00
|
|
|
delayNode.delayTime.setValueAtTime(delayTime, audioContext.currentTime);
|
2023-03-21 04:38:06 +08:00
|
|
|
// Play the stream with the delay in the default audio element (remote-media)
|
|
|
|
audioElement.srcObject = contextDestination.stream;
|
2023-01-24 03:31:53 +08:00
|
|
|
}
|
|
|
|
};
|
2023-03-21 04:38:06 +08:00
|
|
|
|
2022-04-13 09:51:07 +08:00
|
|
|
const deattachEchoStream = () => {
|
|
|
|
const audioElement = document.querySelector(MEDIA_TAG);
|
2023-01-24 03:31:53 +08:00
|
|
|
|
|
|
|
if (DELAY_ENABLED) {
|
|
|
|
audioElement.muted = false;
|
|
|
|
cleanupDelayNode();
|
|
|
|
}
|
|
|
|
|
2022-04-13 09:51:07 +08:00
|
|
|
audioElement.pause();
|
|
|
|
audioElement.srcObject = null;
|
|
|
|
};
|
|
|
|
|
|
|
|
const playEchoStream = async (stream, loopbackAgent = null) => {
|
|
|
|
if (stream) {
|
|
|
|
deattachEchoStream();
|
|
|
|
let streamToPlay = stream;
|
|
|
|
|
|
|
|
if (loopbackAgent) {
|
2023-03-21 04:38:06 +08:00
|
|
|
// Chromium based browsers need audio to go through PCs for echo cancellation
|
|
|
|
// to work. See https://bugs.chromium.org/p/chromium/issues/detail?id=687574
|
2022-04-13 09:51:07 +08:00
|
|
|
try {
|
|
|
|
await loopbackAgent.start(stream);
|
|
|
|
streamToPlay = loopbackAgent.loopbackStream;
|
|
|
|
} catch (error) {
|
|
|
|
loopbackAgent.stop();
|
|
|
|
}
|
|
|
|
}
|
2023-01-24 03:31:53 +08:00
|
|
|
|
|
|
|
if (DELAY_ENABLED) {
|
|
|
|
addDelayNode(streamToPlay);
|
2023-03-21 04:38:06 +08:00
|
|
|
} else {
|
|
|
|
// No delay: play the stream in the default audio element (remote-media),
|
|
|
|
// no strings attached.
|
|
|
|
const audioElement = document.querySelector(MEDIA_TAG);
|
|
|
|
audioElement.srcObject = streamToPlay;
|
|
|
|
audioElement.muted = false;
|
|
|
|
audioElement.play();
|
2023-01-24 03:31:53 +08:00
|
|
|
}
|
2022-04-13 09:51:07 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
export default {
|
|
|
|
useRTCLoopback,
|
|
|
|
createAudioRTCLoopback,
|
|
|
|
deattachEchoStream,
|
|
|
|
playEchoStream,
|
|
|
|
};
|