Update video style and start peer connexion

This commit is contained in:
gparant 2020-05-01 21:15:00 +02:00
parent dbf0bef149
commit e06b20fe96
7 changed files with 140 additions and 131 deletions

View file

@ -8,7 +8,7 @@ import {SECRET_KEY} from "../Enum/EnvironmentVariable"; //TODO fix import by "_E
import {ExtRooms, RefreshUserPositionFunction} from "../Model/Websocket/ExtRoom"; import {ExtRooms, RefreshUserPositionFunction} from "../Model/Websocket/ExtRoom";
import {ExtRoomsInterface} from "../Model/Websocket/ExtRoomsInterface"; import {ExtRoomsInterface} from "../Model/Websocket/ExtRoomsInterface";
import {World} from "../Model/World"; import {World} from "../Model/World";
import { uuid } from 'uuidv4'; import {Group} from "_Model/Group";
enum SockerIoEvent { enum SockerIoEvent {
CONNECTION = "connection", CONNECTION = "connection",
@ -44,10 +44,10 @@ export class IoSocketController{
this.shareUsersPosition(); this.shareUsersPosition();
//don't send only function because the context will be not this //don't send only function because the context will be not this
this.World = new World((user1 : string, user2 : string) => { this.World = new World((user1 : string, user2 : string, group: Group) => {
this.connectedUser(user1, user2); this.connectedUser(user1, user2, group);
}, (user1 : string, user2 : string) => { }, (user1 : string, user2 : string, group: Group) => {
this.disConnectedUser(user1, user2); this.disConnectedUser(user1, user2, group);
}); });
} }
@ -230,24 +230,27 @@ export class IoSocketController{
} }
//connected user //connected user
connectedUser(user1 : string, user2 : string){ connectedUser(user1 : string, user2 : string, group : Group) {
/* TODO manager room and group user to enter and leave */ if(!group){
let roomId = uuid(); return;
let clients : Array<any> = Object.values(this.Io.sockets.sockets);
let User1 = clients.find((user : ExSocketInterface) => user.userId === user1);
let User2 = clients.find((user : ExSocketInterface) => user.userId === user2);
if(User1) {
this.joinWebRtcRoom(User1, roomId);
} }
if(User2) { /* TODO manager room and group user to enter and leave */
this.joinWebRtcRoom(User2, roomId); let clients: Array<any> = Object.values(this.Io.sockets.sockets);
let User1 = clients.find((user: ExSocketInterface) => user.userId === user1);
let User2 = clients.find((user: ExSocketInterface) => user.userId === user2);
if (User1) {
this.joinWebRtcRoom(User1, group.getId());
}
if (User2) {
this.joinWebRtcRoom(User2, group.getId());
} }
} }
//connected user //connected user
disConnectedUser(user1 : string, user2 : string){ disConnectedUser(user1 : string, user2 : string, group : Group){
console.log("disConnectedUser => user1", user1); console.log("disConnectedUser => user1", user1);
console.log("disConnectedUser => user2", user2); console.log("disConnectedUser => user2", user2);
console.log("group", group);
} }
} }

View file

@ -1,19 +1,25 @@
import {MessageUserPosition} from "./Websocket/MessageUserPosition"; import {MessageUserPosition} from "./Websocket/MessageUserPosition";
import { World } from "./World"; import { World } from "./World";
import { UserInterface } from "./UserInterface"; import { UserInterface } from "./UserInterface";
import {uuid} from "uuidv4";
export class Group { export class Group {
static readonly MAX_PER_GROUP = 4; static readonly MAX_PER_GROUP = 4;
private id: string;
private users: UserInterface[]; private users: UserInterface[];
private connectCallback: (user1: string, user2: string) => void; private connectCallback: (user1: string, user2: string, group: Group) => void;
private disconnectCallback: (user1: string, user2: string) => void; private disconnectCallback: (user1: string, user2: string, group: Group) => void;
constructor(users: UserInterface[], connectCallback: (user1: string, user2: string) => void, disconnectCallback: (user1: string, user2: string) => void) { constructor(users: UserInterface[],
connectCallback: (user1: string, user2: string, group: Group) => void,
disconnectCallback: (user1: string, user2: string, group: Group) => void
) {
this.users = []; this.users = [];
this.connectCallback = connectCallback; this.connectCallback = connectCallback;
this.disconnectCallback = disconnectCallback; this.disconnectCallback = disconnectCallback;
this.id = uuid();
users.forEach((user: UserInterface) => { users.forEach((user: UserInterface) => {
this.join(user); this.join(user);
@ -24,6 +30,10 @@ export class Group {
return this.users; return this.users;
} }
getId() : string{
return this.id;
}
isFull(): boolean { isFull(): boolean {
return this.users.length >= Group.MAX_PER_GROUP; return this.users.length >= Group.MAX_PER_GROUP;
} }
@ -31,9 +41,10 @@ export class Group {
join(user: UserInterface): void join(user: UserInterface): void
{ {
// Broadcast on the right event // Broadcast on the right event
this.users.forEach((groupUser: UserInterface) => { for(let i = 0; i < this.users.length; i++){
this.connectCallback(user.id, groupUser.id); let groupUser : UserInterface = this.users[i];
}); this.connectCallback(user.id, groupUser.id, this);
}
this.users.push(user); this.users.push(user);
user.group = this; user.group = this;
} }

View file

@ -12,10 +12,10 @@ export class World {
private users: Map<string, UserInterface>; private users: Map<string, UserInterface>;
private groups: Group[]; private groups: Group[];
private connectCallback: (user1: string, user2: string) => void; private connectCallback: (user1: string, user2: string, group: Group) => void;
private disconnectCallback: (user1: string, user2: string) => void; private disconnectCallback: (user1: string, user2: string, group: Group) => void;
constructor(connectCallback: (user1: string, user2: string) => void, disconnectCallback: (user1: string, user2: string) => void) constructor(connectCallback: (user1: string, user2: string, group: Group) => void, disconnectCallback: (user1: string, user2: string, group: Group) => void)
{ {
this.users = new Map<string, UserInterface>(); this.users = new Map<string, UserInterface>();
this.groups = []; this.groups = [];
@ -48,7 +48,6 @@ export class World {
// If the user is not part of a group: // If the user is not part of a group:
// should he join a group? // should he join a group?
let closestUser: UserInterface|null = this.searchClosestAvailableUser(user); let closestUser: UserInterface|null = this.searchClosestAvailableUser(user);
if (closestUser !== null) { if (closestUser !== null) {
// Is the closest user part of a group? // Is the closest user part of a group?
if (typeof closestUser.group === 'undefined') { if (typeof closestUser.group === 'undefined') {
@ -60,9 +59,8 @@ export class World {
closestUser.group.join(user); closestUser.group.join(user);
} }
} }
} }
// TODO : vérifier qu'ils ne sont pas déja dans un groupe plein // TODO : vérifier qu'ils ne sont pas déja dans un groupe plein
} }
/** /**

View file

@ -12,8 +12,6 @@
<script src="bundle.js"></script> <script src="bundle.js"></script>
<div id="webRtc" class="webrtc"> <div id="webRtc" class="webrtc">
<div id="activeCam" class="activeCam"> <div id="activeCam" class="activeCam">
</div>
<div id="myCam" class="myCam">
<video id="myCamVideo" autoplay></video> <video id="myCamVideo" autoplay></video>
</div> </div>
<div class="btn-cam-action active"> <div class="btn-cam-action active">

View file

@ -1,77 +1,44 @@
.webrtc{ .webrtc{
display: none; display: none;
position: absolute;
right: 0px;
height: 100%;
width: 300px;
} }
.webrtc.active{ .webrtc.active{
display: block; display: block;
} }
.webrtc, .activeCam{ .webrtc, .activeCam{}
position: absolute;
left: 0;
top: 0;
width: 100%;
height: 100%;
background: black;
}
.activeCam video{ .activeCam video{
position: absolute; position: absolute;
width: 100%; width: 300px;
height: 100%; height: 20%;
top: 10px;
right: 10px;
margin: 5px;
background-color: white;
}
.activeCam video#myCamVideo{
width: 200px;
height: 100px;
} }
/*CSS size for 2 - 3 elements*/ /*CSS size for 2 - 3 elements*/
video:nth-child(1):nth-last-child(3), .activeCam video:nth-child(1){
video:nth-child(2):nth-last-child(2), /*this is for camera of user*/
video:nth-child(3):nth-last-child(1), top: 75%;
video:nth-child(1):nth-last-child(2),
video:nth-child(2):nth-last-child(1){
width: 50%;
} }
video:nth-child(1):nth-last-child(3), .activeCam video:nth-child(2){
video:nth-child(2):nth-last-child(2), top: 0%;
video:nth-child(3):nth-last-child(1){
height: 50%;
} }
.activeCam video:nth-child(3){
/*CSS position for 2 elements*/ top: 25%;
video:nth-child(1):nth-last-child(2){
left: 0;
} }
video:nth-child(2):nth-last-child(1){ .activeCam video:nth-child(4) {
left: 50%;
}
/*CSS position for 3 elements*/
video:nth-child(1):nth-last-child(3){
top: 0;
left: 0;
}
video:nth-child(2):nth-last-child(2){
top: 0;
left: 50%;
}
video:nth-child(3):nth-last-child(1) {
top: 50%; top: 50%;
left: 25%;
} }
.myCam{ /*btn animation*/
height: 200px;
width: 300px;
position: absolute;
right: 10px;
background: black;
border: none;
bottom: 20px;
max-height: 17%;
max-width: 17%;
opacity: 1;
display: block;
transition: opacity 1s;
}
.myCam video{
width: 100%;
height: 100%;
}
.btn-cam-action div{ .btn-cam-action div{
cursor: pointer; cursor: pointer;
position: absolute; position: absolute;
@ -79,14 +46,14 @@ video:nth-child(3):nth-last-child(1) {
width: 64px; width: 64px;
height: 64px; height: 64px;
background: #666; background: #666;
left: 6vw;
box-shadow: 2px 2px 24px #444; box-shadow: 2px 2px 24px #444;
border-radius: 48px; border-radius: 48px;
transform: translateX(calc(-6vw - 96px)); transform: translateY(12vw);
transition-timing-function: ease-in-out; transition-timing-function: ease-in-out;
bottom: 20px;
} }
.webrtc:hover .btn-cam-action.active div{ .webrtc:hover .btn-cam-action.active div{
transform: translateX(0); transform: translateY(0);
} }
.btn-cam-action div:hover{ .btn-cam-action div:hover{
background: #407cf7; background: #407cf7;
@ -94,16 +61,16 @@ video:nth-child(3):nth-last-child(1) {
transition: 280ms; transition: 280ms;
} }
.btn-micro{ .btn-micro{
bottom: 277px;
transition: all .3s; transition: all .3s;
left: 168px;
} }
.btn-video{ .btn-video{
bottom: 177px;
transition: all .2s; transition: all .2s;
left: 84px;
} }
.btn-call{ .btn-call{
bottom: 77px;
transition: all .1s; transition: all .1s;
left: 0px;
} }
.btn-cam-action div img{ .btn-cam-action div img{
height: 32px; height: 32px;
@ -112,6 +79,8 @@ video:nth-child(3):nth-last-child(1) {
left: calc(48px - 35px); left: calc(48px - 35px);
position: relative; position: relative;
} }
/*Phone Animation*/
.phone-open{ .phone-open{
position: absolute; position: absolute;
border-radius: 50%; border-radius: 50%;
@ -136,7 +105,6 @@ video:nth-child(3):nth-last-child(1) {
animation: none; animation: none;
cursor: pointer; cursor: pointer;
} }
@keyframes phone-move { @keyframes phone-move {
0% { 0% {
left: calc(50% - 70px); left: calc(50% - 70px);

View file

@ -25,6 +25,7 @@ export class Player extends PlayableCaracter implements CurrentGamerInterface, G
userId: string; userId: string;
PlayerValue: string; PlayerValue: string;
userInputManager: UserInputManager; userInputManager: UserInputManager;
previousMove: string;
constructor( constructor(
userId: string, userId: string,
@ -90,7 +91,10 @@ export class Player extends PlayableCaracter implements CurrentGamerInterface, G
direction = PlayerAnimationNames.None; direction = PlayerAnimationNames.None;
this.stop(); this.stop();
} }
this.sharePosition(direction); if(this.previousMove !== PlayerAnimationNames.None || direction !== PlayerAnimationNames.None){
this.sharePosition(direction);
}
this.previousMove = direction;
} }
private sharePosition(direction: string) { private sharePosition(direction: string) {

View file

@ -4,14 +4,19 @@ let Peer = require('simple-peer');
export interface SimplePeerInterface { export interface SimplePeerInterface {
} }
enum PeerConnexionStatus{
DISABLED = 1,
ACTIVATED = 2
}
export class SimplePeer { export class SimplePeer {
Connexion: ConnexionInterface; private Connexion: ConnexionInterface;
MediaManager: MediaManager; private MediaManager: MediaManager;
WebRtcRoomId: string; private WebRtcRoomId: string;
Users: Array<any>; private Users: Array<any>;
PeerConnexionArray: Array<any> = new Array<any>(); private PeerConnexionArray: Array<any> = new Array<any>();
private PeerConnexionStatus : number = PeerConnexionStatus.DISABLED;
constructor(Connexion: ConnexionInterface, WebRtcRoomId: string = "test-webrtc") { constructor(Connexion: ConnexionInterface, WebRtcRoomId: string = "test-webrtc") {
this.Connexion = Connexion; this.Connexion = Connexion;
@ -31,55 +36,71 @@ export class SimplePeer {
}); });
//when button to call is clicked, start video //when button to call is clicked, start video
this.MediaManager.getElementActivePhone().addEventListener("click", () => { /*this.MediaManager.getElementActivePhone().addEventListener("click", () => {
this.startWebRtc(); this.startWebRtc();
this.disablePhone(); this.disablePhone();
});*/
return this.MediaManager.getCamera().then((stream: MediaStream) => {
this.MediaManager.activeVisio();
this.MediaManager.localStream = stream;
}); });
} }
/** /**
* server has two person connected, start the meet * server has two person connected, start the meet
*/ */
startWebRtc() { private startWebRtc() {
this.MediaManager.activeVisio(); //create pear connexion
return this.MediaManager.getCamera().then((stream: MediaStream) => { this.createPeerConnexion();
this.MediaManager.localStream = stream;
//create pear connexion //receive signal by gemer
this.createPeerConnexion(); this.Connexion.receiveWebrtcSignal((message: string) => {
this.receiveWebrtcSignal(message);
//receive signal by gemer
this.Connexion.receiveWebrtcSignal((message: string) => {
this.receiveWebrtcSignal(message);
});
}).catch((err) => {
console.error(err);
}); });
// add media or new media for all peer connexion
this.Users.forEach((user: any) => {
this.addMedia(user.userId);
});
//change status to manage other user
this.PeerConnexionStatus = PeerConnexionStatus.ACTIVATED;
} }
/** /**
* *
* @param message * @param message
*/ */
receiveWebrtcStart(message: string) { private receiveWebrtcStart(message: string) {
let data = JSON.parse(message); let data = JSON.parse(message);
this.WebRtcRoomId = data.roomId; this.WebRtcRoomId = data.roomId;
this.Users = data.clients; this.Users = data.clients;
//active button for player console.log("receiveWebrtcStart", this.Users);
this.activePhone();
//start connexion
this.startWebRtc();
} }
createPeerConnexion() { private createPeerConnexion() {
this.Users.forEach((user: any) => { this.Users.forEach((user: any) => {
if(this.PeerConnexionArray[user.userId]){ if (this.PeerConnexionArray[user.userId]) {
return; return;
} }
this.MediaManager.addActiveVideo(user.userId); this.MediaManager.addActiveVideo(user.userId);
this.PeerConnexionArray[user.userId] = new Peer({initiator: user.initiator}); console.info("createPeerConnexion => create peerConexion", user);
this.PeerConnexionArray[user.userId] = new Peer({
initiator: user.initiator,
config: { iceServers: [{ urls: 'stun:stun.l.google.com:19302' }, { urls: 'stun:global.stun.twilio.com:3478?transport=udp' }] }
});
//add lof info PeerConnexionArray
this.PeerConnexionArray[user.userId]._debug = console.info;
this.PeerConnexionArray[user.userId].on('signal', (data: any) => { this.PeerConnexionArray[user.userId].on('signal', (data: any) => {
console.info("createPeerConnexion => sendWebrtcSignal : "+user.userId, data);
this.sendWebrtcSignal(data, user.userId); this.sendWebrtcSignal(data, user.userId);
}); });
@ -88,15 +109,15 @@ export class SimplePeer {
}); });
this.PeerConnexionArray[user.userId].on('close', () => { this.PeerConnexionArray[user.userId].on('close', () => {
console.info("createPeerConnexion => close", user.userId);
this.closeConnexion(user.userId); this.closeConnexion(user.userId);
}); });
this.addMedia(user.userId); this.addMedia(user.userId);
}); });
} }
closeConnexion(userId : string){ private closeConnexion(userId : string){
// @ts-ignore // @ts-ignore
this.PeerConnexionArray[userId] = null; this.PeerConnexionArray[userId] = null;
this.MediaManager.removeActiveVideo(userId) this.MediaManager.removeActiveVideo(userId)
@ -107,7 +128,7 @@ export class SimplePeer {
* @param userId * @param userId
* @param data * @param data
*/ */
sendWebrtcSignal(data: any, userId : string) { private sendWebrtcSignal(data: any, userId : string) {
this.Connexion.sendWebrtcSignal(data, this.WebRtcRoomId, null, userId); this.Connexion.sendWebrtcSignal(data, this.WebRtcRoomId, null, userId);
} }
@ -115,8 +136,10 @@ export class SimplePeer {
* *
* @param message * @param message
*/ */
receiveWebrtcSignal(message: string) { private receiveWebrtcSignal(message: string) {
let data = JSON.parse(message); let data = JSON.parse(message);
console.log("receiveWebrtcSignal", data);
console.log("this.PeerConnexionArray[data.userId]", this.PeerConnexionArray[data.userId]);
if(!this.PeerConnexionArray[data.userId]){ if(!this.PeerConnexionArray[data.userId]){
return; return;
} }
@ -128,7 +151,7 @@ export class SimplePeer {
* @param userId * @param userId
* @param stream * @param stream
*/ */
stream(userId : any, stream: MediaStream) { private stream(userId : any, stream: MediaStream) {
this.MediaManager.remoteVideo[userId].srcObject = stream; this.MediaManager.remoteVideo[userId].srcObject = stream;
} }
@ -136,15 +159,19 @@ export class SimplePeer {
* *
* @param userId * @param userId
*/ */
addMedia (userId : any) { private addMedia (userId : any = null) {
this.PeerConnexionArray[userId].addStream(this.MediaManager.localStream) // <- add streams to peer dynamically if (!this.MediaManager.localStream || !this.PeerConnexionArray[userId]) {
return;
}
this.PeerConnexionArray[userId].addStream(this.MediaManager.localStream) // <- add streams to peer dynamically
return;
} }
activePhone(){ private activePhone(){
this.MediaManager.activePhoneOpen(); this.MediaManager.activePhoneOpen();
} }
disablePhone(){ private disablePhone(){
this.MediaManager.disablePhoneOpen(); this.MediaManager.disablePhoneOpen();
} }
} }