From 476af0e3de1567acb07c42f4222c52c6461adb42 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Sat, 23 Jan 2021 20:46:31 +0100 Subject: [PATCH] Auto-release ressources --- assets/js/components/calls/window.js | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/assets/js/components/calls/window.js b/assets/js/components/calls/window.js index 6cdde74c..aaba082e 100644 --- a/assets/js/components/calls/window.js +++ b/assets/js/components/calls/window.js @@ -587,7 +587,9 @@ class CallWindow extends CustomEvents { this.audioContexts.delete(peerID); if (ctx) { - ctx.close(); + // The delay is here to ensure context has been initialized + // to make sure state update event is correctly propagated + setTimeout(() => ctx.close(), 100); } } @@ -770,20 +772,12 @@ class CallWindow extends CustomEvents { }) - // Setup audio context + // Setup audio context to determine whether the person is talking or not const audioContext = new AudioContext(); const gain_node = audioContext.createGain(); gain_node.connect(audioContext.destination); - /*const microphone_stream = audioContext.createMediaStreamSource(stream); - microphone_stream.connect(gain_node) - - const script_processor_node = audioContext.createScriptProcessor(16384, 1, 1); - script_processor_node.addEventListener("audioprocess", (e) => { - //e.data - });*/ - const script_processor_analysis_node = audioContext.createScriptProcessor(2048, 1, 1); script_processor_analysis_node.connect(gain_node); @@ -792,13 +786,14 @@ class CallWindow extends CustomEvents { const analyzer_node = audioContext.createAnalyser(); analyzer_node.smoothingTimeConstant = 0 - analyzer_node.fftSize = 2048 + analyzer_node.fftSize = 4096 analyzer_node.connect(script_processor_analysis_node); microphone_stream.connect(analyzer_node) const freq_data = new Uint8Array(analyzer_node.frequencyBinCount) + script_processor_analysis_node.onaudioprocess = function(e) { analyzer_node.getByteFrequencyData(freq_data); @@ -815,6 +810,17 @@ class CallWindow extends CustomEvents { console.log(avg > 50); } + audioContext.addEventListener("statechange", e => { + if (audioContext.state == "closed") + { + console.info("Release audio analysis ressources for peer " + peerID); + gain_node.disconnect(); + script_processor_analysis_node.disconnect(); + microphone_stream.disconnect(); + analyzer_node.disconnect(); + } + }) + this.videoEls.set(peerID, videoEl)