2020-08-20 16:56:10 +02:00
|
|
|
import * as SimplePeerNamespace from "simple-peer";
|
|
|
|
import {mediaManager} from "./MediaManager";
|
2020-08-28 13:52:25 +02:00
|
|
|
import {TURN_PASSWORD, TURN_SERVER, TURN_USER} from "../Enum/EnvironmentVariable";
|
2020-09-25 18:29:22 +02:00
|
|
|
import {RoomConnection} from "../Connexion/RoomConnection";
|
2020-08-20 16:56:10 +02:00
|
|
|
|
|
|
|
const Peer: SimplePeerNamespace.SimplePeer = require('simple-peer');
|
|
|
|
|
2020-10-25 21:59:14 +01:00
|
|
|
export const MESSAGE_TYPE_CONSTRAINT = 'constraint';
|
|
|
|
export const MESSAGE_TYPE_MESSAGE = 'message';
|
2020-08-20 16:56:10 +02:00
|
|
|
/**
|
|
|
|
* A peer connection used to transmit video / audio signals between 2 peers.
|
|
|
|
*/
|
|
|
|
export class VideoPeer extends Peer {
|
2020-10-20 18:02:44 +02:00
|
|
|
public toClose: boolean = false;
|
|
|
|
public _connected: boolean = false;
|
|
|
|
|
|
|
|
constructor(public userId: number, initiator: boolean, private connection: RoomConnection) {
|
2020-08-20 16:56:10 +02:00
|
|
|
super({
|
|
|
|
initiator: initiator ? initiator : false,
|
|
|
|
reconnectTimer: 10000,
|
|
|
|
config: {
|
|
|
|
iceServers: [
|
|
|
|
{
|
|
|
|
urls: 'stun:stun.l.google.com:19302'
|
|
|
|
},
|
|
|
|
{
|
2020-08-28 16:29:21 +02:00
|
|
|
urls: TURN_SERVER.split(','),
|
2020-08-28 13:52:25 +02:00
|
|
|
username: TURN_USER,
|
|
|
|
credential: TURN_PASSWORD
|
2020-08-20 16:56:10 +02:00
|
|
|
},
|
|
|
|
]
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-08-28 16:29:21 +02:00
|
|
|
console.log('PEER SETUP ', {
|
|
|
|
initiator: initiator ? initiator : false,
|
|
|
|
reconnectTimer: 10000,
|
|
|
|
config: {
|
|
|
|
iceServers: [
|
|
|
|
{
|
|
|
|
urls: 'stun:stun.l.google.com:19302'
|
|
|
|
},
|
|
|
|
{
|
2020-10-20 20:39:33 +02:00
|
|
|
urls: TURN_SERVER.split(','),
|
2020-08-28 16:29:21 +02:00
|
|
|
username: TURN_USER,
|
|
|
|
credential: TURN_PASSWORD
|
|
|
|
},
|
|
|
|
]
|
|
|
|
}
|
2020-10-20 20:39:33 +02:00
|
|
|
});
|
2020-08-28 16:29:21 +02:00
|
|
|
|
2020-08-20 16:56:10 +02:00
|
|
|
//start listen signal for the peer connection
|
|
|
|
this.on('signal', (data: unknown) => {
|
|
|
|
this.sendWebrtcSignal(data);
|
|
|
|
});
|
|
|
|
|
|
|
|
this.on('stream', (stream: MediaStream) => {
|
|
|
|
this.stream(stream);
|
|
|
|
});
|
|
|
|
|
|
|
|
/*peer.on('track', (track: MediaStreamTrack, stream: MediaStream) => {
|
|
|
|
});*/
|
|
|
|
|
|
|
|
this.on('close', () => {
|
2020-10-20 18:02:44 +02:00
|
|
|
this._connected = false;
|
|
|
|
this.toClose = true;
|
2020-08-20 22:23:22 +02:00
|
|
|
this.destroy();
|
2020-08-20 16:56:10 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
this.on('error', (err: any) => {
|
|
|
|
console.error(`error => ${this.userId} => ${err.code}`, err);
|
2020-09-18 13:57:38 +02:00
|
|
|
mediaManager.isError("" + userId);
|
2020-08-20 16:56:10 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
this.on('connect', () => {
|
2020-10-20 18:02:44 +02:00
|
|
|
this._connected = true;
|
2020-09-18 13:57:38 +02:00
|
|
|
mediaManager.isConnected("" + this.userId);
|
2020-08-20 16:56:10 +02:00
|
|
|
console.info(`connect => ${this.userId}`);
|
|
|
|
});
|
|
|
|
|
|
|
|
this.on('data', (chunk: Buffer) => {
|
2020-10-25 21:59:14 +01:00
|
|
|
const message = JSON.parse(chunk.toString('utf8'));
|
|
|
|
console.log("data", message);
|
|
|
|
|
|
|
|
if(message.type === MESSAGE_TYPE_CONSTRAINT) {
|
2020-11-27 14:51:50 +01:00
|
|
|
if (message.audio) {
|
2020-10-25 21:59:14 +01:00
|
|
|
mediaManager.enabledMicrophoneByUserId(this.userId);
|
|
|
|
} else {
|
|
|
|
mediaManager.disabledMicrophoneByUserId(this.userId);
|
|
|
|
}
|
|
|
|
|
2020-11-27 14:51:50 +01:00
|
|
|
if (message.video || message.screen) {
|
2020-10-25 21:59:14 +01:00
|
|
|
mediaManager.enabledVideoByUserId(this.userId);
|
|
|
|
} else {
|
|
|
|
mediaManager.disabledVideoByUserId(this.userId);
|
|
|
|
}
|
2020-11-27 14:51:50 +01:00
|
|
|
} else if(message.type === 'message') {
|
2020-10-25 21:59:14 +01:00
|
|
|
mediaManager.addNewMessage(message.name, message.message);
|
2020-08-20 16:56:10 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-10-20 18:02:44 +02:00
|
|
|
this.once('finish', () => {
|
|
|
|
this._onFinish();
|
|
|
|
});
|
|
|
|
|
2020-08-20 16:56:10 +02:00
|
|
|
this.pushVideoToRemoteUser();
|
|
|
|
}
|
|
|
|
|
|
|
|
private sendWebrtcSignal(data: unknown) {
|
|
|
|
try {
|
|
|
|
this.connection.sendWebrtcSignal(data, this.userId);
|
|
|
|
}catch (e) {
|
|
|
|
console.error(`sendWebrtcSignal => ${this.userId}`, e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sends received stream to screen.
|
|
|
|
*/
|
2020-11-27 14:51:50 +01:00
|
|
|
private stream(stream: MediaStream) {
|
|
|
|
try {
|
|
|
|
mediaManager.addStreamRemoteVideo("" + this.userId, stream);
|
|
|
|
}catch (err){
|
|
|
|
console.error(err);
|
|
|
|
//Force add streem video
|
|
|
|
/*setTimeout(() => {
|
|
|
|
this.stream(stream);
|
|
|
|
}, 500);*/ //todo: find a way to prevent infinite regression.
|
2020-08-20 16:56:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This is triggered twice. Once by the server, and once by a remote client disconnecting
|
|
|
|
*/
|
2020-08-20 22:23:22 +02:00
|
|
|
public destroy(error?: Error): void {
|
2020-08-20 16:56:10 +02:00
|
|
|
try {
|
2020-10-20 18:02:44 +02:00
|
|
|
this._connected = false
|
|
|
|
if(!this.toClose){
|
|
|
|
return;
|
|
|
|
}
|
2020-09-18 13:57:38 +02:00
|
|
|
mediaManager.removeActiveVideo("" + this.userId);
|
2020-08-20 16:56:10 +02:00
|
|
|
// 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.
|
2020-08-20 22:23:22 +02:00
|
|
|
super.destroy(error);
|
2020-08-20 16:56:10 +02:00
|
|
|
} catch (err) {
|
2020-08-20 22:23:22 +02:00
|
|
|
console.error("VideoPeer::destroy", err)
|
2020-08-20 16:56:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-20 18:02:44 +02:00
|
|
|
_onFinish () {
|
|
|
|
if (this.destroyed) return
|
|
|
|
const destroySoon = () => {
|
|
|
|
this.destroy();
|
|
|
|
}
|
|
|
|
if (this._connected) {
|
|
|
|
destroySoon();
|
|
|
|
} else {
|
|
|
|
this.once('connect', destroySoon);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-20 16:56:10 +02:00
|
|
|
private pushVideoToRemoteUser() {
|
|
|
|
try {
|
|
|
|
const localStream: MediaStream | null = mediaManager.localStream;
|
2020-10-25 21:59:14 +01:00
|
|
|
this.write(new Buffer(JSON.stringify({type: MESSAGE_TYPE_CONSTRAINT, ...mediaManager.constraintsMedia})));
|
2020-08-20 16:56:10 +02:00
|
|
|
|
|
|
|
if(!localStream){
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const track of localStream.getTracks()) {
|
|
|
|
this.addTrack(track, localStream);
|
|
|
|
}
|
|
|
|
}catch (e) {
|
|
|
|
console.error(`pushVideoToRemoteUser => ${this.userId}`, e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|