From 2c880c65b6cfe5c07372d9936b9515e7c2842ead Mon Sep 17 00:00:00 2001 From: Hanusiak Piotr Date: Wed, 2 Feb 2022 15:47:38 +0100 Subject: [PATCH] talk indicators wip --- front/src/Phaser/Game/GameScene.ts | 15 +++++++++++- front/src/WebRtc/VideoPeer.ts | 37 ++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 1 deletion(-) diff --git a/front/src/Phaser/Game/GameScene.ts b/front/src/Phaser/Game/GameScene.ts index 39f25f9f..28558e18 100644 --- a/front/src/Phaser/Game/GameScene.ts +++ b/front/src/Phaser/Game/GameScene.ts @@ -168,6 +168,7 @@ export class GameScene extends DirtyScene { private peerStoreUnsubscribe!: Unsubscriber; private emoteUnsubscribe!: Unsubscriber; private emoteMenuUnsubscribe!: Unsubscriber; + private volumeStoreUnsubscribes: Map = new Map(); private followUsersColorStoreUnsubscribe!: Unsubscriber; private biggestAvailableAreaStoreUnsubscribe!: () => void; @@ -637,7 +638,19 @@ export class GameScene extends DirtyScene { let oldPeerNumber = 0; this.peerStoreUnsubscribe = peerStore.subscribe((peers) => { - console.log(peers); + this.volumeStoreUnsubscribes.forEach(unsubscribe => unsubscribe()); + this.volumeStoreUnsubscribes.clear(); + + for (const [key, videoStream] of peers) { + this.volumeStoreUnsubscribes.set(key, videoStream.volumeStore.subscribe((volume) => { + if (volume) { + console.log(volume); + this.MapPlayersByKey.get(key)?.showIconTalk(volume > 5); + this.markDirty(); + } + })); + } + const newPeerNumber = peers.size; if (newPeerNumber > oldPeerNumber) { this.playSound("audio-webrtc-in"); diff --git a/front/src/WebRtc/VideoPeer.ts b/front/src/WebRtc/VideoPeer.ts index c3b6d45e..e29cfa23 100644 --- a/front/src/WebRtc/VideoPeer.ts +++ b/front/src/WebRtc/VideoPeer.ts @@ -10,6 +10,9 @@ import { playersStore } from "../Stores/PlayersStore"; import { chatMessagesStore, newChatMessageSubject } from "../Stores/ChatStore"; import { getIceServersConfig } from "../Components/Video/utils"; import { isMediaBreakpointUp } from "../Utils/BreakpointsUtils"; +import { SoundMeter } from '../Phaser/Components/SoundMeter'; +import { AudioContext } from 'standardized-audio-context'; +import { Console } from 'console'; const Peer: SimplePeerNamespace.SimplePeer = require("simple-peer"); @@ -33,6 +36,7 @@ export class VideoPeer extends Peer { private onBlockSubscribe: Subscription; private onUnBlockSubscribe: Subscription; public readonly streamStore: Readable; + public readonly volumeStore: Readable; public readonly statusStore: Readable; public readonly constraintsStore: Readable; private newMessageSubscribtion: Subscription | undefined; @@ -58,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); }; @@ -69,6 +74,38 @@ 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(); + soundMeter.connectToSource(mediaStream, new AudioContext()); + let error = false; + + timeout = setInterval(() => { + try { + set(soundMeter.getVolume()); + } catch (err) { + if (!error) { + console.error(err); + error = true; + } + } + }, 100); + }); + + return () => { + unsubscribe(); + clearInterval(timeout); + } + }); + this.constraintsStore = readable(null, (set) => { const onData = (chunk: Buffer) => { const message = JSON.parse(chunk.toString("utf8"));