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,36 +546,35 @@ 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);
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);
if (mediaStream === null || mediaStream.getAudioTracks().length <= 0) {
set(null);
return;
}
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 () => {
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,32 +73,30 @@ 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) {
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);
if (mediaStream === null || mediaStream.getAudioTracks().length <= 0) {
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 () => {
console.log('UNSUBSCRIBE');
unsubscribe();
clearInterval(timeout);
}