2020-04-25 16:05:33 +02:00
|
|
|
import {ConnexionInterface} from "../Connexion";
|
|
|
|
import {MediaManager} from "./MediaManager";
|
|
|
|
let Peer = require('simple-peer');
|
|
|
|
|
|
|
|
export class SimplePeer {
|
|
|
|
Connexion: ConnexionInterface;
|
|
|
|
MediaManager: MediaManager;
|
|
|
|
RoomId: string;
|
|
|
|
|
|
|
|
PeerConnexion: any;
|
2020-04-25 17:14:05 +02:00
|
|
|
PeerConnexionArray: Array<any> = new Array<any>();
|
2020-04-25 16:05:33 +02:00
|
|
|
|
|
|
|
constructor(Connexion: ConnexionInterface, roomId: string = "test-webrtc") {
|
|
|
|
this.Connexion = Connexion;
|
|
|
|
this.MediaManager = new MediaManager();
|
|
|
|
this.RoomId = roomId;
|
|
|
|
this.initialise();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* server has two person connected, start the meet
|
|
|
|
*/
|
|
|
|
initialise() {
|
|
|
|
return this.MediaManager.getCamera().then(() => {
|
|
|
|
//send message to join a room
|
|
|
|
this.Connexion.sendWebrtcRomm(this.RoomId);
|
|
|
|
|
|
|
|
//receive message start
|
2020-04-25 20:29:03 +02:00
|
|
|
this.Connexion.receiveWebrtcStart((message: string) => {
|
2020-04-25 16:05:33 +02:00
|
|
|
this.receiveWebrtcStart(message);
|
|
|
|
});
|
|
|
|
|
|
|
|
//receive signal by gemer
|
2020-04-25 20:29:03 +02:00
|
|
|
this.Connexion.receiveWebrtcSignal((message: string) => {
|
2020-04-25 16:05:33 +02:00
|
|
|
this.receiveWebrtcSignal(message);
|
|
|
|
});
|
|
|
|
}).catch((err) => {
|
|
|
|
console.error(err);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
receiveWebrtcStart(message: string) {
|
|
|
|
let data = JSON.parse(message);
|
|
|
|
|
|
|
|
//create pear connexion of user stared
|
2020-04-25 17:14:05 +02:00
|
|
|
this.createPeerConnexion(data.usersId, data.initiator);
|
2020-04-25 16:05:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param userId
|
|
|
|
* @param initiator
|
|
|
|
*/
|
2020-04-25 17:14:05 +02:00
|
|
|
createPeerConnexion(usersId : Array<string>, initiator : boolean = false) {
|
|
|
|
usersId.forEach((userId: any) => {
|
|
|
|
if(this.PeerConnexionArray[userId]){
|
|
|
|
return;
|
|
|
|
}
|
2020-04-25 20:29:03 +02:00
|
|
|
this.MediaManager.addActiveVideo(userId);
|
|
|
|
|
2020-04-25 17:14:05 +02:00
|
|
|
this.PeerConnexion = new Peer({initiator: initiator});
|
|
|
|
|
|
|
|
this.PeerConnexion.on('signal', (data: any) => {
|
|
|
|
this.sendWebrtcSignal(data);
|
|
|
|
});
|
2020-04-25 16:05:33 +02:00
|
|
|
|
2020-04-25 17:14:05 +02:00
|
|
|
this.PeerConnexion.on('stream', (stream: MediaStream) => {
|
2020-04-25 20:29:03 +02:00
|
|
|
this.stream(userId, stream);
|
2020-04-25 17:14:05 +02:00
|
|
|
});
|
2020-04-25 16:05:33 +02:00
|
|
|
|
2020-04-25 17:14:05 +02:00
|
|
|
this.PeerConnexionArray[userId] = this.PeerConnexion;
|
|
|
|
this.addMedia(userId);
|
2020-04-25 16:05:33 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* permit to send signal
|
|
|
|
* @param data
|
|
|
|
*/
|
|
|
|
sendWebrtcSignal(data: any) {
|
|
|
|
this.Connexion.sendWebrtcSignal(data, this.RoomId);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param message
|
|
|
|
*/
|
|
|
|
receiveWebrtcSignal(message: string) {
|
|
|
|
let data = JSON.parse(message);
|
2020-04-25 17:14:05 +02:00
|
|
|
if(!this.PeerConnexionArray[data.userId]){
|
2020-04-25 16:05:33 +02:00
|
|
|
return;
|
|
|
|
}
|
2020-04-25 17:14:05 +02:00
|
|
|
this.PeerConnexionArray[data.userId].signal(data.signal);
|
2020-04-25 16:05:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-04-25 20:29:03 +02:00
|
|
|
*
|
|
|
|
* @param userId
|
2020-04-25 16:05:33 +02:00
|
|
|
* @param stream
|
|
|
|
*/
|
2020-04-25 20:29:03 +02:00
|
|
|
stream(userId : any, stream: MediaStream) {
|
|
|
|
this.MediaManager.remoteVideo[userId].srcObject = stream;
|
2020-04-25 16:05:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-04-25 20:29:03 +02:00
|
|
|
*
|
|
|
|
* @param userId
|
2020-04-25 16:05:33 +02:00
|
|
|
*/
|
2020-04-25 17:14:05 +02:00
|
|
|
addMedia (userId : any) {
|
|
|
|
this.PeerConnexionArray[userId].addStream(this.MediaManager.localStream) // <- add streams to peer dynamically
|
2020-04-25 16:05:33 +02:00
|
|
|
}
|
|
|
|
}
|