From 4424c7cce1bcfc2a24b220ba523e3fdf08fef1f3 Mon Sep 17 00:00:00 2001 From: Hanusiak Piotr Date: Thu, 3 Feb 2022 12:58:31 +0100 Subject: [PATCH] prevent SoundMeter from being initialized if stream has no audio tracks available --- front/src/Phaser/Game/GameScene.ts | 12 ++--- front/src/Stores/MediaStore.ts | 79 ++++++++++++++---------------- front/src/WebRtc/VideoPeer.ts | 41 +++++++--------- 3 files changed, 63 insertions(+), 69 deletions(-) diff --git a/front/src/Phaser/Game/GameScene.ts b/front/src/Phaser/Game/GameScene.ts index 99db0d6c..bdcf976a 100644 --- a/front/src/Phaser/Game/GameScene.ts +++ b/front/src/Phaser/Game/GameScene.ts @@ -637,12 +637,12 @@ export class GameScene extends DirtyScene { this.connect(); } - // localVolumeStore.subscribe((volume) => { - // if (volume) { - // this.CurrentPlayer.showIconTalk(volume > 5); - // this.markDirty(); // should be dirty from animation - // } - // }); + localVolumeStore.subscribe((volume) => { + if (volume) { + this.CurrentPlayer.showIconTalk(volume > 5); + this.markDirty(); // should be dirty from animation + } + }); let oldPeerNumber = 0; this.peerStoreUnsubscribe = peerStore.subscribe((peers) => { diff --git a/front/src/Stores/MediaStore.ts b/front/src/Stores/MediaStore.ts index adc1ac2e..fe83d41a 100644 --- a/front/src/Stores/MediaStore.ts +++ b/front/src/Stores/MediaStore.ts @@ -397,47 +397,6 @@ async function toggleConstraints(track: MediaStreamTrack, constraints: MediaTrac } } -export const localVolumeStore = readable(null, (set) => { - let timeout: ReturnType; - const unsubscribe = localStreamStore.subscribe((localStreamStoreValue) => { - console.log('LOCAL VOLUME STORE SUBSCRIBE TO LOCAL STREAM STORE'); - if (localStreamStoreValue.type === "error") { - console.log('D1'); - set(null); - return; - } - const soundMeter = new SoundMeter(); - const mediaStream = localStreamStoreValue.stream; - if (mediaStream === null) { - console.log('D2'); - set(null); - return; - } - console.log('D3'); - console.log(localStreamStoreValue); - soundMeter.connectToSource(mediaStream, new AudioContext()); - let error = false; - - timeout = setInterval(() => { - console.log('local time interval'); - try { - set(soundMeter.getVolume()); - } catch (err) { - if (!error) { - console.error(err); - error = true; - } - } - }, 100); - }); - - return () => { - console.log('UNSUBSCRIBE FROM LOCAL STREAM STORE'); - unsubscribe(); - clearInterval(timeout); - } -}); - /** * A store containing the MediaStream object (or null if nothing requested, or Error if an error occurred) */ @@ -584,6 +543,44 @@ export const obtainedMediaConstraintStore = derived(null, (set) => { + let timeout: ReturnType; + const unsubscribe = localStreamStore.subscribe((localStreamStoreValue) => { + console.log('LOCAL VOLUME STORE SUBSCRIBE TO LOCAL STREAM STORE'); + if (localStreamStoreValue.type === "error") { + console.log('D1'); + set(null); + return; + } + const soundMeter = new SoundMeter(); + const mediaStream = localStreamStoreValue.stream; + if (mediaStream !== null && mediaStream.getAudioTracks().length > 0) { + console.log('D3'); + console.log(localStreamStoreValue); + soundMeter.connectToSource(mediaStream, new AudioContext()); + let error = false; + + timeout = setInterval(() => { + console.log('local time interval'); + try { + set(soundMeter.getVolume()); + } catch (err) { + if (!error) { + console.error(err); + error = true; + } + } + }, 100); + } + }); + + return () => { + console.log('UNSUBSCRIBE FROM LOCAL STREAM STORE'); + unsubscribe(); + clearInterval(timeout); + } +}); + /** * Device list */ diff --git a/front/src/WebRtc/VideoPeer.ts b/front/src/WebRtc/VideoPeer.ts index fab45a71..ca808365 100644 --- a/front/src/WebRtc/VideoPeer.ts +++ b/front/src/WebRtc/VideoPeer.ts @@ -62,6 +62,7 @@ export class VideoPeer extends Peer { this.uniqueId = "video_" + this.userId; this.streamStore = readable(null, (set) => { + console.log('STREAM STORE INITIALIZE'); const onStream = (stream: MediaStream | null) => { set(stream); }; @@ -73,36 +74,32 @@ export class VideoPeer extends Peer { }; }); + console.log('CREATE VOLUME STORE'); + this.volumeStore = readable(null, (set) => { let timeout: ReturnType; + console.log('VOLUME STORE INITIALIZE'); const unsubscribe = this.streamStore.subscribe((mediaStream) => { - if (mediaStream === null) { - set(null); - return; - } - const soundMeter = new SoundMeter(); - let error = false; - try { + if (mediaStream !== null && mediaStream.getAudioTracks().length > 0) { + const soundMeter = new SoundMeter(); soundMeter.connectToSource(mediaStream, new AudioContext()); - } catch (errMsg) { - console.warn(errMsg); - set(null); - return; - } - - timeout = setInterval(() => { - try { - set(soundMeter.getVolume()); - } catch (err) { - if (!error) { - console.error(err); - error = true; + let error = false; + + timeout = setInterval(() => { + try { + set(soundMeter.getVolume()); + } catch (err) { + if (!error) { + console.error(err); + error = true; + } } - } - }, 100); + }, 100); + } }); return () => { + console.log('UNSUBSCRIBE'); unsubscribe(); clearInterval(timeout); }