Fixing setInterval not freed for some volume analyzers

Also: using a 256 fftSize instead of 2048 to save on CPU cycles.
This commit is contained in:
David Négrier 2022-03-16 15:27:58 +01:00
parent 53b184e82b
commit 37e824c494
3 changed files with 19 additions and 4 deletions

View File

@ -57,8 +57,8 @@ export class SoundMeter {
this.context = context; this.context = context;
this.analyser = this.context.createAnalyser(); this.analyser = this.context.createAnalyser();
this.analyser.fftSize = 2048; this.analyser.fftSize = 256;
const bufferLength = this.analyser.fftSize; const bufferLength = this.analyser.frequencyBinCount;
this.dataArray = new Uint8Array(bufferLength); this.dataArray = new Uint8Array(bufferLength);
} }

View File

@ -545,8 +545,12 @@ export const obtainedMediaConstraintStore = derived<Readable<MediaStreamConstrai
export const localVolumeStore = readable<number | undefined>(undefined, (set) => { export const localVolumeStore = readable<number | undefined>(undefined, (set) => {
let timeout: ReturnType<typeof setTimeout>; let timeout: ReturnType<typeof setTimeout>;
let soundMeter: SoundMeter;
const unsubscribe = localStreamStore.subscribe((localStreamStoreValue) => { const unsubscribe = localStreamStore.subscribe((localStreamStoreValue) => {
clearInterval(timeout); clearInterval(timeout);
if (soundMeter) {
soundMeter.stop();
}
if (localStreamStoreValue.type === "error") { if (localStreamStoreValue.type === "error") {
set(undefined); set(undefined);
return; return;
@ -557,7 +561,7 @@ export const localVolumeStore = readable<number | undefined>(undefined, (set) =>
set(undefined); set(undefined);
return; return;
} }
const soundMeter = new SoundMeter(mediaStream); soundMeter = new SoundMeter(mediaStream);
let error = false; let error = false;
timeout = setInterval(() => { timeout = setInterval(() => {
@ -575,6 +579,9 @@ export const localVolumeStore = readable<number | undefined>(undefined, (set) =>
return () => { return () => {
unsubscribe(); unsubscribe();
clearInterval(timeout); clearInterval(timeout);
if (soundMeter) {
soundMeter.stop();
}
}; };
}); });

View File

@ -74,12 +74,17 @@ export class VideoPeer extends Peer {
this.volumeStore = readable<number | undefined>(undefined, (set) => { this.volumeStore = readable<number | undefined>(undefined, (set) => {
let timeout: ReturnType<typeof setTimeout>; let timeout: ReturnType<typeof setTimeout>;
let soundMeter: SoundMeter;
const unsubscribe = this.streamStore.subscribe((mediaStream) => { const unsubscribe = this.streamStore.subscribe((mediaStream) => {
clearInterval(timeout);
if (soundMeter) {
soundMeter.stop();
}
if (mediaStream === null || mediaStream.getAudioTracks().length <= 0) { if (mediaStream === null || mediaStream.getAudioTracks().length <= 0) {
set(undefined); set(undefined);
return; return;
} }
const soundMeter = new SoundMeter(mediaStream); soundMeter = new SoundMeter(mediaStream);
let error = false; let error = false;
timeout = setInterval(() => { timeout = setInterval(() => {
@ -97,6 +102,9 @@ export class VideoPeer extends Peer {
return () => { return () => {
unsubscribe(); unsubscribe();
clearInterval(timeout); clearInterval(timeout);
if (soundMeter) {
soundMeter.stop();
}
}; };
}); });