Fix sharing peer connection

This commit is contained in:
Gregoire Parant 2020-10-20 20:39:33 +02:00
parent c03dd4c551
commit 2ee6d43274
3 changed files with 49 additions and 28 deletions

View file

@ -13,6 +13,8 @@ export class ScreenSharingPeer extends Peer {
* Whether this connection is currently receiving a video stream from a remote user. * Whether this connection is currently receiving a video stream from a remote user.
*/ */
private isReceivingStream:boolean = false; private isReceivingStream:boolean = false;
public toClose: boolean = false;
public _connected: boolean = false;
constructor(private userId: number, initiator: boolean, private connection: RoomConnection) { constructor(private userId: number, initiator: boolean, private connection: RoomConnection) {
super({ super({
@ -42,6 +44,8 @@ export class ScreenSharingPeer extends Peer {
}); });
this.on('close', () => { this.on('close', () => {
this._connected = false;
this.toClose = true;
this.destroy(); this.destroy();
}); });
@ -62,11 +66,16 @@ export class ScreenSharingPeer extends Peer {
}); });
this.on('connect', () => { this.on('connect', () => {
this._connected = true;
// FIXME: we need to put the loader on the screen sharing connection // FIXME: we need to put the loader on the screen sharing connection
mediaManager.isConnected("" + this.userId); mediaManager.isConnected("" + this.userId);
console.info(`connect => ${this.userId}`); console.info(`connect => ${this.userId}`);
}); });
this.once('finish', () => {
this._onFinish();
});
this.pushScreenSharingToRemoteUser(); this.pushScreenSharingToRemoteUser();
} }
@ -100,6 +109,10 @@ export class ScreenSharingPeer extends Peer {
public destroy(error?: Error): void { public destroy(error?: Error): void {
try { try {
this._connected = false
if(!this.toClose){
return;
}
mediaManager.removeActiveScreenSharingVideo("" + this.userId); mediaManager.removeActiveScreenSharingVideo("" + this.userId);
// FIXME: I don't understand why "Closing connection with" message is displayed TWICE before "Nb users in peerConnectionArray" // 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. // I do understand the method closeConnection is called twice, but I don't understand how they manage to run in parallel.
@ -111,6 +124,18 @@ export class ScreenSharingPeer extends Peer {
} }
} }
_onFinish () {
if (this.destroyed) return
const destroySoon = () => {
this.destroy();
}
if (this._connected) {
destroySoon();
} else {
this.once('connect', destroySoon);
}
}
private pushScreenSharingToRemoteUser() { private pushScreenSharingToRemoteUser() {
const localScreenCapture: MediaStream | null = mediaManager.localScreenCapture; const localScreenCapture: MediaStream | null = mediaManager.localScreenCapture;
if(!localScreenCapture){ if(!localScreenCapture){

View file

@ -108,44 +108,30 @@ export class SimplePeer {
this.createPeerConnection(user); this.createPeerConnection(user);
} }
/**
* server has two people connected, start the meet
*/
private startWebRtc() {
console.warn('startWebRtc startWebRtc');
this.Users.forEach((user: UserSimplePeerInterface) => {
//if it's not an initiator, peer connection will be created when gamer will receive offer signal
if(!user.initiator){
return;
}
this.createPeerConnection(user);
});
}
/** /**
* create peer connection to bind users * create peer connection to bind users
*/ */
private createPeerConnection(user : UserSimplePeerInterface) : VideoPeer | null{ private createPeerConnection(user : UserSimplePeerInterface) : VideoPeer | null {
const peerConnection = this.PeerConnectionArray.get(user.userId) const peerConnection = this.PeerConnectionArray.get(user.userId)
if(peerConnection){ if (peerConnection) {
if(peerConnection.destroyed){ if (peerConnection.destroyed) {
peerConnection.toClose = true; peerConnection.toClose = true;
peerConnection.destroy(); peerConnection.destroy();
const peerConnexionDeleted = this.PeerConnectionArray.delete(user.userId); const peerConnexionDeleted = this.PeerConnectionArray.delete(user.userId);
if(!peerConnexionDeleted){ if (!peerConnexionDeleted) {
throw 'Error to delete peer connection'; throw 'Error to delete peer connection';
} }
this.createPeerConnection(user); this.createPeerConnection(user);
}else { } else {
peerConnection.toClose = false; peerConnection.toClose = false;
} }
return null; return null;
} }
let name = user.name; let name = user.name;
if(!name){ if (!name) {
const userSearch = this.Users.find((userSearch: UserSimplePeerInterface) => userSearch.userId === user.userId); const userSearch = this.Users.find((userSearch: UserSimplePeerInterface) => userSearch.userId === user.userId);
if(userSearch) { if (userSearch) {
name = userSearch.name; name = userSearch.name;
} }
} }
@ -153,8 +139,8 @@ export class SimplePeer {
mediaManager.removeActiveVideo("" + user.userId); mediaManager.removeActiveVideo("" + user.userId);
const reportCallback = this.enableReporting ? (comment: string) => { const reportCallback = this.enableReporting ? (comment: string) => {
this.reportUser(user.userId, comment); this.reportUser(user.userId, comment);
}: undefined; } : undefined;
mediaManager.addActiveVideo("" + user.userId, reportCallback, name); mediaManager.addActiveVideo("" + user.userId, reportCallback, name);
@ -179,9 +165,19 @@ export class SimplePeer {
* create peer connection to bind users * create peer connection to bind users
*/ */
private createPeerScreenSharingConnection(user : UserSimplePeerInterface) : ScreenSharingPeer | null{ private createPeerScreenSharingConnection(user : UserSimplePeerInterface) : ScreenSharingPeer | null{
if( const peerConnection = this.PeerScreenSharingConnectionArray.get(user.userId);
this.PeerScreenSharingConnectionArray.has(user.userId) if(peerConnection){
){ if(peerConnection.destroyed){
peerConnection.toClose = true;
peerConnection.destroy();
const peerConnexionDeleted = this.PeerScreenSharingConnectionArray.delete(user.userId);
if(!peerConnexionDeleted){
throw 'Error to delete peer connection';
}
this.createPeerConnection(user);
}else {
peerConnection.toClose = false;
}
return null; return null;
} }

View file

@ -39,13 +39,13 @@ export class VideoPeer extends Peer {
urls: 'stun:stun.l.google.com:19302' urls: 'stun:stun.l.google.com:19302'
}, },
{ {
urls: TURN_SERVER, urls: TURN_SERVER.split(','),
username: TURN_USER, username: TURN_USER,
credential: TURN_PASSWORD credential: TURN_PASSWORD
}, },
] ]
} }
}) });
//start listen signal for the peer connection //start listen signal for the peer connection
this.on('signal', (data: unknown) => { this.on('signal', (data: unknown) => {