2020-05-03 14:29:45 +02:00
|
|
|
const videoConstraint: {width : any, height: any, facingMode : string} = {
|
|
|
|
width: { ideal: 1280 },
|
|
|
|
height: { ideal: 720 },
|
|
|
|
facingMode: "user"
|
|
|
|
};
|
2020-04-19 19:32:38 +02:00
|
|
|
export class MediaManager {
|
|
|
|
localStream: MediaStream;
|
2020-04-25 20:29:03 +02:00
|
|
|
remoteVideo: Array<any> = new Array<any>();
|
2020-04-19 19:32:38 +02:00
|
|
|
myCamVideo: any;
|
|
|
|
cinemaClose: any = null;
|
|
|
|
cinema: any = null;
|
|
|
|
microphoneClose: any = null;
|
|
|
|
microphone: any = null;
|
2020-05-03 14:29:45 +02:00
|
|
|
constraintsMedia : {audio : any, video : any} = {
|
|
|
|
audio: true,
|
|
|
|
video: videoConstraint
|
|
|
|
};
|
2020-04-19 19:32:38 +02:00
|
|
|
getCameraPromise : Promise<any> = null;
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
this.myCamVideo = document.getElementById('myCamVideo');
|
|
|
|
this.microphoneClose = document.getElementById('microphone-close');
|
2020-04-26 20:55:20 +02:00
|
|
|
|
2020-04-19 19:32:38 +02:00
|
|
|
this.microphoneClose.addEventListener('click', (e: any) => {
|
|
|
|
e.preventDefault();
|
|
|
|
this.enabledMicrophone();
|
|
|
|
//update tracking
|
|
|
|
});
|
|
|
|
this.microphone = document.getElementById('microphone');
|
|
|
|
this.microphone.addEventListener('click', (e: any) => {
|
|
|
|
e.preventDefault();
|
|
|
|
this.disabledMicrophone();
|
|
|
|
//update tracking
|
|
|
|
});
|
|
|
|
|
|
|
|
this.cinemaClose = document.getElementById('cinema-close');
|
|
|
|
this.cinemaClose.addEventListener('click', (e: any) => {
|
|
|
|
e.preventDefault();
|
|
|
|
this.enabledCamera();
|
|
|
|
//update tracking
|
|
|
|
});
|
|
|
|
this.cinema = document.getElementById('cinema');
|
|
|
|
this.cinema.addEventListener('click', (e: any) => {
|
|
|
|
e.preventDefault();
|
|
|
|
this.disabledCamera();
|
|
|
|
//update tracking
|
|
|
|
});
|
2020-04-26 20:55:20 +02:00
|
|
|
}
|
2020-04-19 19:32:38 +02:00
|
|
|
|
2020-04-26 20:55:20 +02:00
|
|
|
activeVisio(){
|
2020-04-19 19:32:38 +02:00
|
|
|
let webRtc = document.getElementById('webRtc');
|
|
|
|
webRtc.classList.add('active');
|
|
|
|
}
|
|
|
|
|
|
|
|
enabledCamera() {
|
|
|
|
this.cinemaClose.style.display = "none";
|
|
|
|
this.cinema.style.display = "block";
|
2020-05-03 14:29:45 +02:00
|
|
|
this.constraintsMedia.video = videoConstraint;
|
2020-04-19 22:30:42 +02:00
|
|
|
this.localStream = null;
|
|
|
|
this.myCamVideo.srcObject = null;
|
2020-04-19 19:32:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
disabledCamera() {
|
|
|
|
this.cinemaClose.style.display = "block";
|
|
|
|
this.cinema.style.display = "none";
|
|
|
|
this.constraintsMedia.video = false;
|
2020-04-19 22:30:42 +02:00
|
|
|
|
|
|
|
this.myCamVideo.pause();
|
|
|
|
if(this.localStream) {
|
|
|
|
this.localStream.getTracks().forEach((MediaStreamTrack: MediaStreamTrack) => {
|
|
|
|
if (MediaStreamTrack.kind === "video") {
|
|
|
|
MediaStreamTrack.stop();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
this.localStream = null;
|
|
|
|
this.myCamVideo.srcObject = null;
|
2020-04-19 19:32:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
enabledMicrophone() {
|
|
|
|
this.microphoneClose.style.display = "none";
|
|
|
|
this.microphone.style.display = "block";
|
|
|
|
this.constraintsMedia.audio = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
disabledMicrophone() {
|
|
|
|
this.microphoneClose.style.display = "block";
|
|
|
|
this.microphone.style.display = "none";
|
|
|
|
this.constraintsMedia.audio = false;
|
2020-04-19 22:30:42 +02:00
|
|
|
if(this.localStream) {
|
|
|
|
this.localStream.getTracks().forEach((MediaStreamTrack: MediaStreamTrack) => {
|
|
|
|
if (MediaStreamTrack.kind === "audio") {
|
|
|
|
MediaStreamTrack.stop();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2020-04-19 19:32:38 +02:00
|
|
|
}
|
|
|
|
|
2020-04-26 20:55:20 +02:00
|
|
|
getElementActivePhone(){
|
|
|
|
return document.getElementById('phone-open');
|
|
|
|
}
|
|
|
|
|
|
|
|
activePhoneOpen(){
|
|
|
|
return this.getElementActivePhone().classList.add("active");
|
|
|
|
}
|
|
|
|
|
|
|
|
disablePhoneOpen(){
|
|
|
|
return this.getElementActivePhone().classList.remove("active");
|
|
|
|
}
|
|
|
|
|
2020-04-19 19:32:38 +02:00
|
|
|
//get camera
|
|
|
|
getCamera() {
|
2020-04-25 16:05:33 +02:00
|
|
|
return this.getCameraPromise = navigator.mediaDevices.getUserMedia(this.constraintsMedia)
|
2020-04-19 19:32:38 +02:00
|
|
|
.then((stream: MediaStream) => {
|
|
|
|
this.localStream = stream;
|
|
|
|
this.myCamVideo.srcObject = this.localStream;
|
2020-05-03 14:29:45 +02:00
|
|
|
|
|
|
|
//TODO resize remote cam
|
|
|
|
/*console.log(this.localStream.getTracks());
|
|
|
|
let videoMediaStreamTrack = this.localStream.getTracks().find((media : MediaStreamTrack) => media.kind === "video");
|
|
|
|
let {width, height} = videoMediaStreamTrack.getSettings();
|
|
|
|
console.info(`${width}x${height}`); // 6*/
|
|
|
|
|
2020-04-25 16:05:33 +02:00
|
|
|
return stream;
|
2020-04-19 19:32:38 +02:00
|
|
|
}).catch((err) => {
|
|
|
|
console.error(err);
|
|
|
|
this.localStream = null;
|
|
|
|
throw err;
|
|
|
|
});
|
|
|
|
}
|
2020-04-25 20:29:03 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param userId
|
|
|
|
*/
|
2020-04-26 19:12:01 +02:00
|
|
|
addActiveVideo(userId : string){
|
2020-04-25 20:29:03 +02:00
|
|
|
let elementRemoteVideo = document.getElementById("activeCam");
|
2020-04-26 17:42:49 +02:00
|
|
|
elementRemoteVideo.insertAdjacentHTML('beforeend', '<video id="'+userId+'" autoplay></video>');
|
2020-04-26 19:12:01 +02:00
|
|
|
this.remoteVideo[(userId as any)] = document.getElementById(userId);
|
|
|
|
}
|
|
|
|
|
2020-05-02 20:46:02 +02:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param userId
|
|
|
|
* @param stream
|
|
|
|
*/
|
|
|
|
addStreamRemoteVideo(userId : string, stream : MediaStream){
|
|
|
|
this.remoteVideo[(userId as any)].srcObject = stream;
|
|
|
|
}
|
|
|
|
|
2020-04-26 19:12:01 +02:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param userId
|
|
|
|
*/
|
|
|
|
removeActiveVideo(userId : string){
|
|
|
|
let element = document.getElementById(userId);
|
|
|
|
if(!element){
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
element.remove();
|
2020-04-25 20:29:03 +02:00
|
|
|
}
|
2020-04-19 19:32:38 +02:00
|
|
|
}
|