prevent SoundMeter from being initialized if stream has no audio tracks available

This commit is contained in:
Hanusiak Piotr 2022-02-03 12:58:31 +01:00
parent e0f5529fa7
commit 4424c7cce1
3 changed files with 63 additions and 69 deletions

View File

@ -637,12 +637,12 @@ export class GameScene extends DirtyScene {
this.connect(); this.connect();
} }
// localVolumeStore.subscribe((volume) => { localVolumeStore.subscribe((volume) => {
// if (volume) { if (volume) {
// this.CurrentPlayer.showIconTalk(volume > 5); this.CurrentPlayer.showIconTalk(volume > 5);
// this.markDirty(); // should be dirty from animation this.markDirty(); // should be dirty from animation
// } }
// }); });
let oldPeerNumber = 0; let oldPeerNumber = 0;
this.peerStoreUnsubscribe = peerStore.subscribe((peers) => { this.peerStoreUnsubscribe = peerStore.subscribe((peers) => {

View File

@ -397,47 +397,6 @@ async function toggleConstraints(track: MediaStreamTrack, constraints: MediaTrac
} }
} }
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');
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) * 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<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');
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 * Device list
*/ */

View File

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