listen to local volume change only if in bubble conversation

This commit is contained in:
Hanusiak Piotr 2022-02-03 13:43:20 +01:00
parent 4424c7cce1
commit 40aae25e11
3 changed files with 54 additions and 51 deletions

View File

@ -169,7 +169,9 @@ export class GameScene extends DirtyScene {
private peerStoreUnsubscribe!: Unsubscriber;
private emoteUnsubscribe!: Unsubscriber;
private emoteMenuUnsubscribe!: Unsubscriber;
private volumeStoreUnsubscribes: Map<number, Unsubscriber> = new Map<number, Unsubscriber>();
private volumeStoreUnsubscribers: Map<number, Unsubscriber> = new Map<number, Unsubscriber>();
private localVolumeStoreUnsubscriber: Unsubscriber | undefined;
private followUsersColorStoreUnsubscribe!: Unsubscriber;
private biggestAvailableAreaStoreUnsubscribe!: () => void;
@ -637,20 +639,13 @@ export class GameScene extends DirtyScene {
this.connect();
}
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) => {
this.volumeStoreUnsubscribes.forEach(unsubscribe => unsubscribe());
this.volumeStoreUnsubscribes.clear();
this.volumeStoreUnsubscribers.forEach(unsubscribe => unsubscribe());
this.volumeStoreUnsubscribers.clear();
for (const [key, videoStream] of peers) {
this.volumeStoreUnsubscribes.set(key, videoStream.volumeStore.subscribe((volume) => {
this.volumeStoreUnsubscribers.set(key, videoStream.volumeStore.subscribe((volume) => {
if (volume) {
console.log(`${key}: ${volume}`);
this.MapPlayersByKey.get(key)?.showIconTalk(volume > 5);
@ -665,6 +660,18 @@ export class GameScene extends DirtyScene {
} else if (newPeerNumber < oldPeerNumber) {
this.playSound("audio-webrtc-out");
}
if (newPeerNumber > 0) {
this.localVolumeStoreUnsubscriber = localVolumeStore.subscribe((volume) => {
if (volume) {
this.CurrentPlayer.showIconTalk(volume > 5);
this.markDirty(); // should be dirty from animation
}
});
} else {
if (this.localVolumeStoreUnsubscriber) {
this.localVolumeStoreUnsubscriber();
}
}
oldPeerNumber = newPeerNumber;
});

View File

@ -546,22 +546,23 @@ export const obtainedMediaConstraintStore = derived<Readable<MediaStreamConstrai
export const localVolumeStore = readable<number | null>(null, (set) => {
let timeout: ReturnType<typeof setTimeout>;
const unsubscribe = localStreamStore.subscribe((localStreamStoreValue) => {
console.log('LOCAL VOLUME STORE SUBSCRIBE TO LOCAL STREAM STORE');
clearInterval(timeout);
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);
if (mediaStream === null || mediaStream.getAudioTracks().length <= 0) {
set(null);
return;
}
soundMeter.connectToSource(mediaStream, new AudioContext());
let error = false;
timeout = setInterval(() => {
console.log('local time interval');
try {
set(soundMeter.getVolume());
} catch (err) {
@ -571,11 +572,9 @@ export const localVolumeStore = readable<number | null>(null, (set) => {
}
}
}, 100);
}
});
return () => {
console.log('UNSUBSCRIBE FROM LOCAL STREAM STORE');
unsubscribe();
clearInterval(timeout);
}

View File

@ -62,7 +62,6 @@ export class VideoPeer extends Peer {
this.uniqueId = "video_" + this.userId;
this.streamStore = readable<MediaStream | null>(null, (set) => {
console.log('STREAM STORE INITIALIZE');
const onStream = (stream: MediaStream | null) => {
set(stream);
};
@ -74,13 +73,13 @@ export class VideoPeer extends Peer {
};
});
console.log('CREATE VOLUME STORE');
this.volumeStore = readable<number | null>(null, (set) => {
let timeout: ReturnType<typeof setTimeout>;
console.log('VOLUME STORE INITIALIZE');
const unsubscribe = this.streamStore.subscribe((mediaStream) => {
if (mediaStream !== null && mediaStream.getAudioTracks().length > 0) {
if (mediaStream === null || mediaStream.getAudioTracks().length <= 0) {
set(null);
return;
}
const soundMeter = new SoundMeter();
soundMeter.connectToSource(mediaStream, new AudioContext());
let error = false;
@ -95,11 +94,9 @@ export class VideoPeer extends Peer {
}
}
}, 100);
}
});
return () => {
console.log('UNSUBSCRIBE');
unsubscribe();
clearInterval(timeout);
}