From 37e824c494dc1cf6765ebf7befd779e4d73c7640 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20N=C3=A9grier?= Date: Wed, 16 Mar 2022 15:27:58 +0100 Subject: [PATCH] Fixing setInterval not freed for some volume analyzers Also: using a 256 fftSize instead of 2048 to save on CPU cycles. --- front/src/Phaser/Components/SoundMeter.ts | 4 ++-- front/src/Stores/MediaStore.ts | 9 ++++++++- front/src/WebRtc/VideoPeer.ts | 10 +++++++++- 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/front/src/Phaser/Components/SoundMeter.ts b/front/src/Phaser/Components/SoundMeter.ts index 6e12912f..86ea610f 100644 --- a/front/src/Phaser/Components/SoundMeter.ts +++ b/front/src/Phaser/Components/SoundMeter.ts @@ -57,8 +57,8 @@ export class SoundMeter { this.context = context; this.analyser = this.context.createAnalyser(); - this.analyser.fftSize = 2048; - const bufferLength = this.analyser.fftSize; + this.analyser.fftSize = 256; + const bufferLength = this.analyser.frequencyBinCount; this.dataArray = new Uint8Array(bufferLength); } diff --git a/front/src/Stores/MediaStore.ts b/front/src/Stores/MediaStore.ts index 9494eb7e..b86a97ce 100644 --- a/front/src/Stores/MediaStore.ts +++ b/front/src/Stores/MediaStore.ts @@ -545,8 +545,12 @@ export const obtainedMediaConstraintStore = derived(undefined, (set) => { let timeout: ReturnType; + let soundMeter: SoundMeter; const unsubscribe = localStreamStore.subscribe((localStreamStoreValue) => { clearInterval(timeout); + if (soundMeter) { + soundMeter.stop(); + } if (localStreamStoreValue.type === "error") { set(undefined); return; @@ -557,7 +561,7 @@ export const localVolumeStore = readable(undefined, (set) => set(undefined); return; } - const soundMeter = new SoundMeter(mediaStream); + soundMeter = new SoundMeter(mediaStream); let error = false; timeout = setInterval(() => { @@ -575,6 +579,9 @@ export const localVolumeStore = readable(undefined, (set) => return () => { unsubscribe(); clearInterval(timeout); + if (soundMeter) { + soundMeter.stop(); + } }; }); diff --git a/front/src/WebRtc/VideoPeer.ts b/front/src/WebRtc/VideoPeer.ts index a1ffa14c..50c3e19f 100644 --- a/front/src/WebRtc/VideoPeer.ts +++ b/front/src/WebRtc/VideoPeer.ts @@ -74,12 +74,17 @@ export class VideoPeer extends Peer { this.volumeStore = readable(undefined, (set) => { let timeout: ReturnType; + let soundMeter: SoundMeter; const unsubscribe = this.streamStore.subscribe((mediaStream) => { + clearInterval(timeout); + if (soundMeter) { + soundMeter.stop(); + } if (mediaStream === null || mediaStream.getAudioTracks().length <= 0) { set(undefined); return; } - const soundMeter = new SoundMeter(mediaStream); + soundMeter = new SoundMeter(mediaStream); let error = false; timeout = setInterval(() => { @@ -97,6 +102,9 @@ export class VideoPeer extends Peer { return () => { unsubscribe(); clearInterval(timeout); + if (soundMeter) { + soundMeter.stop(); + } }; });