2020-04-25 16:05:33 +02:00
|
|
|
import {ConnexionInterface} from "../Connexion";
|
|
|
|
import {MediaManager} from "./MediaManager";
|
|
|
|
let Peer = require('simple-peer');
|
|
|
|
|
2020-04-26 19:59:51 +02:00
|
|
|
export interface SimplePeerInterface {
|
2020-04-26 20:55:20 +02:00
|
|
|
activePhone(): void;
|
|
|
|
disablePhone(): void;
|
2020-04-26 19:59:51 +02:00
|
|
|
}
|
|
|
|
|
2020-04-25 16:05:33 +02:00
|
|
|
export class SimplePeer {
|
|
|
|
Connexion: ConnexionInterface;
|
|
|
|
MediaManager: MediaManager;
|
|
|
|
RoomId: string;
|
|
|
|
|
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.RoomId = roomId;
|
2020-04-26 20:55:20 +02:00
|
|
|
this.MediaManager = new MediaManager();
|
|
|
|
this.MediaManager.getElementActivePhone().addEventListener("click", () => {
|
|
|
|
this.startWebRtc();
|
|
|
|
this.disablePhone();
|
|
|
|
});
|
2020-04-25 16:05:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* server has two person connected, start the meet
|
|
|
|
*/
|
2020-04-26 19:59:51 +02:00
|
|
|
startWebRtc() {
|
2020-04-26 20:55:20 +02:00
|
|
|
this.MediaManager.activeVisio();
|
2020-04-26 19:59:51 +02:00
|
|
|
return this.MediaManager.getCamera().then((stream: MediaStream) => {
|
|
|
|
this.MediaManager.localStream = stream;
|
2020-04-25 16:05:33 +02:00
|
|
|
//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);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
2020-04-26 17:42:49 +02:00
|
|
|
* @param message
|
2020-04-25 16:05:33 +02:00
|
|
|
*/
|
|
|
|
receiveWebrtcStart(message: string) {
|
|
|
|
let data = JSON.parse(message);
|
|
|
|
|
|
|
|
//create pear connexion of user stared
|
2020-04-26 17:42:49 +02:00
|
|
|
this.createPeerConnexion(data);
|
2020-04-25 16:05:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
2020-04-26 17:42:49 +02:00
|
|
|
* @param users
|
2020-04-25 16:05:33 +02:00
|
|
|
*/
|
2020-04-26 17:42:49 +02:00
|
|
|
createPeerConnexion(users : Array<any>) {
|
|
|
|
users.forEach((user: any) => {
|
|
|
|
if(this.PeerConnexionArray[user.userId]){
|
2020-04-25 17:14:05 +02:00
|
|
|
return;
|
|
|
|
}
|
2020-04-26 17:42:49 +02:00
|
|
|
this.MediaManager.addActiveVideo(user.userId);
|
2020-04-25 20:29:03 +02:00
|
|
|
|
2020-04-26 19:12:01 +02:00
|
|
|
this.PeerConnexionArray[user.userId] = new Peer({initiator: user.initiator});
|
2020-04-25 17:14:05 +02:00
|
|
|
|
2020-04-26 19:12:01 +02:00
|
|
|
this.PeerConnexionArray[user.userId].on('signal', (data: any) => {
|
|
|
|
this.sendWebrtcSignal(data, user.userId);
|
2020-04-25 17:14:05 +02:00
|
|
|
});
|
2020-04-25 16:05:33 +02:00
|
|
|
|
2020-04-26 19:12:01 +02:00
|
|
|
this.PeerConnexionArray[user.userId].on('stream', (stream: MediaStream) => {
|
2020-04-26 17:42:49 +02:00
|
|
|
this.stream(user.userId, stream);
|
2020-04-25 17:14:05 +02:00
|
|
|
});
|
2020-04-25 16:05:33 +02:00
|
|
|
|
2020-04-26 19:12:01 +02:00
|
|
|
this.PeerConnexionArray[user.userId].on('close', () => {
|
|
|
|
this.closeConnexion(user.userId);
|
|
|
|
});
|
|
|
|
|
2020-04-26 17:42:49 +02:00
|
|
|
this.addMedia(user.userId);
|
2020-04-25 16:05:33 +02:00
|
|
|
});
|
2020-04-26 17:42:49 +02:00
|
|
|
|
2020-04-26 19:12:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
closeConnexion(userId : string){
|
|
|
|
// @ts-ignore
|
|
|
|
this.PeerConnexionArray[userId] = null;
|
|
|
|
this.MediaManager.removeActiveVideo(userId)
|
2020-04-25 16:05:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-04-26 17:42:49 +02:00
|
|
|
*
|
|
|
|
* @param userId
|
2020-04-25 16:05:33 +02:00
|
|
|
* @param data
|
|
|
|
*/
|
2020-04-26 17:42:49 +02:00
|
|
|
sendWebrtcSignal(data: any, userId : string) {
|
2020-04-26 19:12:01 +02:00
|
|
|
this.Connexion.sendWebrtcSignal(data, this.RoomId, null, userId);
|
2020-04-25 16:05:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @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
|
|
|
}
|
2020-04-26 20:55:20 +02:00
|
|
|
|
|
|
|
activePhone(){
|
|
|
|
this.MediaManager.activePhoneOpen();
|
|
|
|
}
|
|
|
|
|
|
|
|
disablePhone(){
|
|
|
|
this.MediaManager.disablePhoneOpen();
|
|
|
|
}
|
2020-04-25 16:05:33 +02:00
|
|
|
}
|