Stability simple peer

This commit is contained in:
Gregoire Parant 2020-06-06 19:52:34 +02:00 committed by David Négrier
parent 208b91e52a
commit eed5333d69

View File

@ -180,14 +180,13 @@ export class SimplePeer {
peer.on('data', (chunk: Buffer) => { peer.on('data', (chunk: Buffer) => {
let constraint = JSON.parse(chunk.toString('utf8')); let constraint = JSON.parse(chunk.toString('utf8'));
if (constraint.audio) { if (constraint.audio) {
mediaManager.enabledMicrophoneByUserId(user.userId); mediaManager.enabledMicrophoneByUserId(user.userId);
} else { } else {
mediaManager.disabledMicrophoneByUserId(user.userId); mediaManager.disabledMicrophoneByUserId(user.userId);
} }
if (constraint.video) { if (constraint.video || constraint.screen) {
mediaManager.enabledVideoByUserId(user.userId); mediaManager.enabledVideoByUserId(user.userId);
} else { } else {
this.stream(user.userId); this.stream(user.userId);
@ -295,18 +294,30 @@ export class SimplePeer {
try { try {
let localStream: MediaStream | null = mediaManager.localStream; let localStream: MediaStream | null = mediaManager.localStream;
let localScreenCapture: MediaStream | null = mediaManager.localScreenCapture; let localScreenCapture: MediaStream | null = mediaManager.localScreenCapture;
let peer = this.PeerConnectionArray.get(userId); let PeerConnection : any = this.PeerConnectionArray.get(userId);
if (peer === undefined) { if (PeerConnection === undefined) {
throw new Error('While adding media, cannot find user with ID ' + userId); throw new Error('While adding media, cannot find user with ID ' + userId);
} }
peer.write(new Buffer(JSON.stringify(mediaManager.constraintsMedia))); PeerConnection.write(new Buffer(JSON.stringify(Object.assign(mediaManager.constraintsMedia, {screen: localScreenCapture !== null}))));
//remove current stream
try {
if (PeerConnection._pc) {
PeerConnection._pc.getRemoteStreams().forEach((stream: MediaStream) => {
stream.getTracks().forEach((track: MediaStreamTrack) => {
PeerConnection.removeTrack(track, stream);
});
});
}
}catch (e) {}
if (localScreenCapture !== null) { if (localScreenCapture !== null) {
for (const track of localScreenCapture.getTracks()) { for (const track of localScreenCapture.getTracks()) {
peer.addTrack(track, localScreenCapture); PeerConnection.addTrack(track, localScreenCapture);
} }
} else if (localStream) { } else if (localStream) {
for (const track of localStream.getTracks()) { for (const track of localStream.getTracks()) {
peer.addTrack(track, localStream); PeerConnection.addTrack(track, localStream);
} }
} }
}catch (e) { }catch (e) {