From 33e78060d4267170a3c4baf35c22db4c4e478481 Mon Sep 17 00:00:00 2001 From: Alexis Faizeau Date: Thu, 17 Mar 2022 17:50:39 +0100 Subject: [PATCH] Stabilize screensharing (#1982) Co-authored-by: Alexis Faizeau --- front/src/WebRtc/SimplePeer.ts | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/front/src/WebRtc/SimplePeer.ts b/front/src/WebRtc/SimplePeer.ts index fdd95748..c2d17241 100644 --- a/front/src/WebRtc/SimplePeer.ts +++ b/front/src/WebRtc/SimplePeer.ts @@ -125,7 +125,6 @@ export class SimplePeer { if (!peerConnexionDeleted) { throw new Error("Error to delete peer connection"); } - //return this.createPeerConnection(user, localStream); } else { peerConnection.toClose = false; return null; @@ -171,6 +170,7 @@ export class SimplePeer { stream: MediaStream | null ): ScreenSharingPeer | null { const peerConnection = this.PeerScreenSharingConnectionArray.get(user.userId); + if (peerConnection) { if (peerConnection.destroyed) { peerConnection.toClose = true; @@ -182,8 +182,8 @@ export class SimplePeer { this.createPeerConnection(user); } else { peerConnection.toClose = false; + return null; } - return null; } // Enrich the user with last known credentials (if they are not set in the user object, which happens when a user triggers the screen sharing) @@ -201,6 +201,9 @@ export class SimplePeer { this.Connection, stream ); + + peer.toClose = false; + this.PeerScreenSharingConnectionArray.set(user.userId, peer); screenSharingPeerStore.pushNewPeer(peer); @@ -265,10 +268,13 @@ export class SimplePeer { } // FIXME: I don't understand why "Closing connection with" message is displayed TWICE before "Nb users in peerConnectionArray" // I do understand the method closeConnection is called twice, but I don't understand how they manage to run in parallel. + peer.toClose = true; peer.destroy(); } catch (err) { console.error("closeConnection", err); } + + screenSharingPeerStore.removePeer(userId); } public closeAllConnections() { @@ -376,11 +382,6 @@ export class SimplePeer { private sendLocalScreenSharingStreamToUser(userId: number, localScreenCapture: MediaStream): void { const uuid = playersStore.getPlayerById(userId)?.userUuid || ""; if (blackListManager.isBlackListed(uuid)) return; - // If a connection already exists with user (because it is already sharing a screen with us... let's use this connection) - if (this.PeerScreenSharingConnectionArray.has(userId)) { - this.pushScreenSharingToRemoteUser(userId, localScreenCapture); - return; - } const screenSharingUser: UserSimplePeerInterface = { userId,