Fix video call

This commit is contained in:
gparant 2020-04-19 22:30:42 +02:00
parent 5b62ac39fb
commit 89816a13e4
2 changed files with 60 additions and 33 deletions

View file

@ -54,32 +54,58 @@ export class MediaManager {
this.cinemaClose.style.display = "none"; this.cinemaClose.style.display = "none";
this.cinema.style.display = "block"; this.cinema.style.display = "block";
this.constraintsMedia.video = true; this.constraintsMedia.video = true;
this.localStream = null;
this.myCamVideo.srcObject = null;
this.getCamera();
} }
disabledCamera() { disabledCamera() {
this.cinemaClose.style.display = "block"; this.cinemaClose.style.display = "block";
this.cinema.style.display = "none"; this.cinema.style.display = "none";
this.constraintsMedia.video = false; this.constraintsMedia.video = false;
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;
this.getCamera();
} }
enabledMicrophone() { enabledMicrophone() {
this.microphoneClose.style.display = "none"; this.microphoneClose.style.display = "none";
this.microphone.style.display = "block"; this.microphone.style.display = "block";
this.constraintsMedia.audio = true; this.constraintsMedia.audio = true;
this.getCamera();
} }
disabledMicrophone() { disabledMicrophone() {
this.microphoneClose.style.display = "block"; this.microphoneClose.style.display = "block";
this.microphone.style.display = "none"; this.microphone.style.display = "none";
this.constraintsMedia.audio = false; this.constraintsMedia.audio = false;
if(this.localStream) {
this.localStream.getTracks().forEach((MediaStreamTrack: MediaStreamTrack) => {
if (MediaStreamTrack.kind === "audio") {
MediaStreamTrack.stop();
}
});
}
this.getCamera();
} }
//get camera //get camera
getCamera() { getCamera() {
this.getCameraPromise = navigator.mediaDevices.getUserMedia(this.constraintsMedia) this.getCameraPromise = navigator.mediaDevices.getUserMedia(this.constraintsMedia)
.then((stream: MediaStream) => { .then((stream: MediaStream) => {
console.log("constraintsMedia", stream);
this.localStream = stream; this.localStream = stream;
this.myCamVideo.srcObject = this.localStream; this.myCamVideo.srcObject = this.localStream;
this.myCamVideo.play();
}).catch((err) => { }).catch((err) => {
console.error(err); console.error(err);
this.localStream = null; this.localStream = null;

View file

@ -19,27 +19,27 @@ export class PeerConnexionManager {
} }
createPeerConnection(data: any = null): Promise<any> { createPeerConnection(data: any = null): Promise<any> {
this.peerConnection = new RTCPeerConnection(); return this.MediaManager.getCameraPromise.then(() => {
this.peerConnection = new RTCPeerConnection();
//init all events peer connection //init all events peer connection
this.createEventPeerConnection(); this.createEventPeerConnection();
this.MediaManager.getCameraPromise.then(() => {
this.MediaManager.localStream.getTracks().forEach( this.MediaManager.localStream.getTracks().forEach(
(track : MediaStreamTrack) => this.peerConnection.addTrack(track, this.MediaManager.localStream) (track: MediaStreamTrack) => this.peerConnection.addTrack(track, this.MediaManager.localStream)
); );
//if no data, create offer
if (!data || !data.message) {
return this.createOffer();
}
let description = new RTCSessionDescription(data.message);
return this.peerConnection.setRemoteDescription(description).catch((err) => {
console.error("createPeerConnection => setRemoteDescription", err);
throw err;
});
}); });
//if no data, create offer
if (!data || !data.message) {
return this.createOffer();
}
let description = new RTCSessionDescription(data.message);
return this.peerConnection.setRemoteDescription(description).catch((err) => {
console.error("createPeerConnection => setRemoteDescription", err);
throw err;
})
} }
createOffer(): Promise<any> { createOffer(): Promise<any> {
@ -102,30 +102,31 @@ export class PeerConnexionManager {
createEventPeerConnection(){ createEventPeerConnection(){
//define creator of offer //define creator of offer
this.peerConnection.onicecandidate = (event: RTCPeerConnectionIceEvent) => { this.peerConnection.addEventListener('icecandidate', ({candidate}) => {
let message = {message: event.candidate}; let message = {message: candidate};
if (!event.candidate) { if (!candidate) {
return; return;
} }
this.WebRtcEventManager.emitIceCandidate(message); this.WebRtcEventManager.emitIceCandidate(message);
}; });
this.peerConnection.ontrack = (e:RTCTrackEvent) => { this.peerConnection.addEventListener('iceconnectionstatechange', (e : Event) => {
console.info('Event:track', e); console.info('oniceconnectionstatechange => iceConnectionState', this.peerConnection.iceConnectionState);
this.MediaManager.remoteVideo.srcObject = e.streams[0]; });
this.MediaManager.myCamVideo.srcObject = e.streams[0];
};
this.peerConnection.onnegotiationneeded = (e : Event) => { this.peerConnection.addEventListener('negotiationneeded', (e : Event) => {
console.info("Event:negotiationneeded => call()", e); console.info("Event:negotiationneeded => call()", e);
this.createOffer() this.createOffer()
}; });
this.peerConnection.oniceconnectionstatechange = (e) => {
console.info('ICE state change event: ', e); this.peerConnection.addEventListener("track", (e:RTCTrackEvent) => {
}; console.info('Event:track', e);
this.peerConnection.oniceconnectionstatechange = (e:Event) => { if (this.MediaManager.remoteVideo.srcObject !== e.streams[0]) {
console.info('oniceconnectionstatechange => iceConnectionState', this.peerConnection.iceConnectionState); this.MediaManager.remoteVideo.srcObject = e.streams[0];
}; console.log('pc1 received remote stream');
}
});
this.peerConnection.onicegatheringstatechange = () => { this.peerConnection.onicegatheringstatechange = () => {
console.info('onicegatheringstatechange => iceConnectionState', this.peerConnection.iceConnectionState); console.info('onicegatheringstatechange => iceConnectionState', this.peerConnection.iceConnectionState);
}; };