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';
|
|
|
|
|
|
|
|
const MEDIA_TAG = Meteor.settings.public.media.mediaTag;
|
|
|
|
const USE_RTC_LOOPBACK_CHR = Meteor.settings.public.media.localEchoTest.useRtcLoopbackInChromium;
|
2023-01-24 03:31:53 +08:00
|
|
|
const {
|
|
|
|
enabled: DELAY_ENABLED = true,
|
|
|
|
delayTime = 0.5,
|
|
|
|
maxDelayTime = 2,
|
|
|
|
} = Meteor.settings.public.media.localEchoTest.delay;
|
|
|
|
|
|
|
|
let audioContext = null;
|
|
|
|
let sourceContext = null;
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const addDelayNode = (stream) => {
|
|
|
|
if (stream) {
|
|
|
|
if (delayNode || audioContext || sourceContext) cleanupDelayNode();
|
|
|
|
|
|
|
|
audioContext = new AudioContext();
|
|
|
|
sourceContext = audioContext.createMediaStreamSource(stream);
|
|
|
|
delayNode = new DelayNode(audioContext, { delayTime, maxDelayTime });
|
|
|
|
sourceContext.connect(delayNode);
|
|
|
|
delayNode.connect(audioContext.destination);
|
|
|
|
delayNode.delayTime.setValueAtTime(delayTime, audioContext.currentTime);
|
|
|
|
}
|
|
|
|
};
|
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) {
|
|
|
|
const audioElement = document.querySelector(MEDIA_TAG);
|
|
|
|
deattachEchoStream();
|
|
|
|
let streamToPlay = stream;
|
|
|
|
|
|
|
|
if (loopbackAgent) {
|
|
|
|
try {
|
|
|
|
await loopbackAgent.start(stream);
|
|
|
|
streamToPlay = loopbackAgent.loopbackStream;
|
|
|
|
} catch (error) {
|
|
|
|
loopbackAgent.stop();
|
|
|
|
}
|
|
|
|
}
|
2023-01-24 03:31:53 +08:00
|
|
|
|
|
|
|
if (DELAY_ENABLED) {
|
|
|
|
// Start muted to avoid weird artifacts and prevent playing the stream twice (Chromium)
|
|
|
|
audioElement.muted = true;
|
|
|
|
addDelayNode(streamToPlay);
|
|
|
|
}
|
|
|
|
audioElement.srcObject = streamToPlay;
|
2022-04-13 09:51:07 +08:00
|
|
|
audioElement.play();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
export default {
|
|
|
|
useRTCLoopback,
|
|
|
|
createAudioRTCLoopback,
|
|
|
|
deattachEchoStream,
|
|
|
|
playEchoStream,
|
|
|
|
};
|