From eed5333d693d57183f0895d81109b1c21fdeba1f Mon Sep 17 00:00:00 2001 From: Gregoire Parant Date: Sat, 6 Jun 2020 19:52:34 +0200 Subject: [PATCH] Stability simple peer --- front/src/WebRtc/SimplePeer.ts | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/front/src/WebRtc/SimplePeer.ts b/front/src/WebRtc/SimplePeer.ts index 96f047b7..78dc41f9 100644 --- a/front/src/WebRtc/SimplePeer.ts +++ b/front/src/WebRtc/SimplePeer.ts @@ -180,14 +180,13 @@ export class SimplePeer { peer.on('data', (chunk: Buffer) => { let constraint = JSON.parse(chunk.toString('utf8')); - if (constraint.audio) { mediaManager.enabledMicrophoneByUserId(user.userId); } else { mediaManager.disabledMicrophoneByUserId(user.userId); } - if (constraint.video) { + if (constraint.video || constraint.screen) { mediaManager.enabledVideoByUserId(user.userId); } else { this.stream(user.userId); @@ -295,18 +294,30 @@ export class SimplePeer { try { let localStream: MediaStream | null = mediaManager.localStream; let localScreenCapture: MediaStream | null = mediaManager.localScreenCapture; - let peer = this.PeerConnectionArray.get(userId); - if (peer === undefined) { + let PeerConnection : any = this.PeerConnectionArray.get(userId); + if (PeerConnection === undefined) { 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) { for (const track of localScreenCapture.getTracks()) { - peer.addTrack(track, localScreenCapture); + PeerConnection.addTrack(track, localScreenCapture); } } else if (localStream) { for (const track of localStream.getTracks()) { - peer.addTrack(track, localStream); + PeerConnection.addTrack(track, localStream); } } }catch (e) {