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();
}
// 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) => {

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)
*/
@ -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
*/

View File

@ -62,6 +62,7 @@ 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);
};
@ -73,36 +74,32 @@ 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) {
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;
}
let error = false;
timeout = setInterval(() => {
try {
set(soundMeter.getVolume());
} catch (err) {
if (!error) {
console.error(err);
error = true;
timeout = setInterval(() => {
try {
set(soundMeter.getVolume());
} catch (err) {
if (!error) {
console.error(err);
error = true;
}
}
}
}, 100);
}, 100);
}
});
return () => {
console.log('UNSUBSCRIBE');
unsubscribe();
clearInterval(timeout);
}