Merge pull request #463 from thecodingmachine/hotfix/timeouts

Hotfix/timeouts
This commit is contained in:
Kharhamel 2020-11-30 11:43:36 +01:00 committed by GitHub
commit d36cec23a7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 29 additions and 44 deletions

View File

@ -109,7 +109,7 @@ export class MediaManager {
this.previousConstraint = JSON.parse(JSON.stringify(this.constraintsMedia)); this.previousConstraint = JSON.parse(JSON.stringify(this.constraintsMedia));
this.pingCameraStatus(); this.pingCameraStatus();
this.checkActiveUser(); this.checkActiveUser(); //todo: desactivated in case of bug
this.discussionManager = new DiscussionManager(this,''); this.discussionManager = new DiscussionManager(this,'');
} }
@ -486,7 +486,7 @@ export class MediaManager {
if(!element){ if(!element){
return; return;
} }
element.classList.add('active') element.classList.add('active') //todo: why does a method 'disable' add a class 'active'?
} }
enabledMicrophoneByUserId(userId: number){ enabledMicrophoneByUserId(userId: number){
@ -494,7 +494,7 @@ export class MediaManager {
if(!element){ if(!element){
return; return;
} }
element.classList.remove('active') element.classList.remove('active') //todo: why does a method 'enable' remove a class 'active'?
} }
disabledVideoByUserId(userId: number) { disabledVideoByUserId(userId: number) {
@ -519,7 +519,7 @@ export class MediaManager {
} }
} }
addStreamRemoteVideo(userId: string, stream : MediaStream){ addStreamRemoteVideo(userId: string, stream : MediaStream): void {
const remoteVideo = this.remoteVideo.get(userId); const remoteVideo = this.remoteVideo.get(userId);
if (remoteVideo === undefined) { if (remoteVideo === undefined) {
throw `Unable to find video for ${userId}`; throw `Unable to find video for ${userId}`;
@ -686,11 +686,10 @@ export class MediaManager {
* Here, every 30 seconds, we are "reseting" the streams and sending again the constraints to the other peers via the data channel again (see SimplePeer::pushVideoToRemoteUser) * Here, every 30 seconds, we are "reseting" the streams and sending again the constraints to the other peers via the data channel again (see SimplePeer::pushVideoToRemoteUser)
**/ **/
private pingCameraStatus(){ private pingCameraStatus(){
setTimeout(() => { /*setInterval(() => {
console.log('ping camera status'); console.log('ping camera status');
this.triggerUpdatedLocalStreamCallbacks(this.localStream); this.triggerUpdatedLocalStreamCallbacks(this.localStream);
this.pingCameraStatus(); }, 30000);*/
}, 30000);
} }
public addNewMessage(name: string, message: string, isMe: boolean = false){ public addNewMessage(name: string, message: string, isMe: boolean = false){

View File

@ -1,7 +1,6 @@
import { import {
WebRtcDisconnectMessageInterface, WebRtcDisconnectMessageInterface,
WebRtcSignalReceivedMessageInterface, WebRtcSignalReceivedMessageInterface,
WebRtcStartMessageInterface
} from "../Connexion/ConnexionModels"; } from "../Connexion/ConnexionModels";
import { import {
mediaManager, mediaManager,
@ -29,7 +28,7 @@ export interface PeerConnectionListener {
* This class manages connections to all the peers in the same group as me. * This class manages connections to all the peers in the same group as me.
*/ */
export class SimplePeer { export class SimplePeer {
private Users: Array<UserSimplePeerInterface> = new Array<UserSimplePeerInterface>(); private Users: Array<UserSimplePeerInterface> = new Array<UserSimplePeerInterface>(); //todo: this array should be fusionned with PeerConnectionArray
private PeerScreenSharingConnectionArray: Map<number, ScreenSharingPeer> = new Map<number, ScreenSharingPeer>(); private PeerScreenSharingConnectionArray: Map<number, ScreenSharingPeer> = new Map<number, ScreenSharingPeer>();
private PeerConnectionArray: Map<number, VideoPeer> = new Map<number, VideoPeer>(); private PeerConnectionArray: Map<number, VideoPeer> = new Map<number, VideoPeer>();
@ -95,12 +94,9 @@ export class SimplePeer {
this.Users.push(user); this.Users.push(user);
// Note: the clients array contain the list of all clients (even the ones we are already connected to in case a user joints a group) // Note: the clients array contain the list of all clients (even the ones we are already connected to in case a user joints a group)
// So we can receive a request we already had before. (which will abort at the first line of createPeerConnection) // So we can receive a request we already had before. (which will abort at the first line of createPeerConnection)
// TODO: refactor this to only send a message to connect to one user (rather than several users). => DONE
// This would be symmetrical to the way we handle disconnection. // This would be symmetrical to the way we handle disconnection.
//console.log('Start message', data);
//start connection //start connection
//this.startWebRtc();
console.log('receiveWebrtcStart. Initiator: ', user.initiator) console.log('receiveWebrtcStart. Initiator: ', user.initiator)
if(!user.initiator){ if(!user.initiator){
return; return;
@ -204,8 +200,6 @@ export class SimplePeer {
/** /**
* This is triggered twice. Once by the server, and once by a remote client disconnecting * This is triggered twice. Once by the server, and once by a remote client disconnecting
*
* @param userId
*/ */
private closeConnection(userId : number) { private closeConnection(userId : number) {
try { try {
@ -226,6 +220,12 @@ export class SimplePeer {
for (const peerConnectionListener of this.peerConnectionListeners) { for (const peerConnectionListener of this.peerConnectionListeners) {
peerConnectionListener.onDisconnect(userId); peerConnectionListener.onDisconnect(userId);
} }
const userIndex = this.Users.findIndex(user => user.userId === userId);
if(userIndex < 0){
throw 'Couln\'t delete user';
} else {
this.Users.splice(userIndex, 1);
}
} catch (err) { } catch (err) {
console.error("closeConnection", err) console.error("closeConnection", err)
} }
@ -233,8 +233,6 @@ export class SimplePeer {
/** /**
* This is triggered twice. Once by the server, and once by a remote client disconnecting * This is triggered twice. Once by the server, and once by a remote client disconnecting
*
* @param userId
*/ */
private closeScreenSharingConnection(userId : number) { private closeScreenSharingConnection(userId : number) {
try { try {
@ -246,7 +244,6 @@ export class SimplePeer {
} }
// 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.
//console.log('Closing connection with '+userId);
peer.destroy(); peer.destroy();
if(!this.PeerScreenSharingConnectionArray.delete(userId)){ if(!this.PeerScreenSharingConnectionArray.delete(userId)){
throw 'Couln\'t delete peer screen sharing connexion'; throw 'Couln\'t delete peer screen sharing connexion';
@ -313,10 +310,6 @@ export class SimplePeer {
} }
} }
/**
*
* @param userId
*/
private pushVideoToRemoteUser(userId : number) { private pushVideoToRemoteUser(userId : number) {
try { try {
const PeerConnection = this.PeerConnectionArray.get(userId); const PeerConnection = this.PeerConnectionArray.get(userId);
@ -331,6 +324,9 @@ export class SimplePeer {
} }
for (const track of localStream.getTracks()) { for (const track of localStream.getTracks()) {
//todo: this is a ugly hack to reduce the amount of error in console. Find a better way.
if ((track as any).added !== undefined) continue; // eslint-disable-line @typescript-eslint/no-explicit-any
(track as any).added = true; // eslint-disable-line @typescript-eslint/no-explicit-any
PeerConnection.addTrack(track, localStream); PeerConnection.addTrack(track, localStream);
} }
}catch (e) { }catch (e) {

View File

@ -84,22 +84,18 @@ export class VideoPeer extends Peer {
console.log("data", message); console.log("data", message);
if(message.type === MESSAGE_TYPE_CONSTRAINT) { if(message.type === MESSAGE_TYPE_CONSTRAINT) {
const constraint = message; if (message.audio) {
if (constraint.audio) {
mediaManager.enabledMicrophoneByUserId(this.userId); mediaManager.enabledMicrophoneByUserId(this.userId);
} else { } else {
mediaManager.disabledMicrophoneByUserId(this.userId); mediaManager.disabledMicrophoneByUserId(this.userId);
} }
if (constraint.video || constraint.screen) { if (message.video || message.screen) {
mediaManager.enabledVideoByUserId(this.userId); mediaManager.enabledVideoByUserId(this.userId);
} else { } else {
this.stream(undefined);
mediaManager.disabledVideoByUserId(this.userId); mediaManager.disabledVideoByUserId(this.userId);
} }
} } else if(message.type === 'message') {
if(message.type === 'message') {
mediaManager.addNewMessage(message.name, message.message); mediaManager.addNewMessage(message.name, message.message);
} }
}); });
@ -122,21 +118,15 @@ export class VideoPeer extends Peer {
/** /**
* Sends received stream to screen. * Sends received stream to screen.
*/ */
private stream(stream?: MediaStream) { private stream(stream: MediaStream) {
//console.log(`VideoPeer::stream => ${this.userId}`, stream);
if(!stream){
mediaManager.disabledVideoByUserId(this.userId);
mediaManager.disabledMicrophoneByUserId(this.userId);
} else {
try { try {
mediaManager.addStreamRemoteVideo("" + this.userId, stream); mediaManager.addStreamRemoteVideo("" + this.userId, stream);
}catch (err){ }catch (err){
console.error(err); console.error(err);
//Force add streem video //Force add streem video
setTimeout(() => { /*setTimeout(() => {
this.stream(stream); this.stream(stream);
}, 500); }, 500);*/ //todo: find a way to prevent infinite regression.
}
} }
} }