I'm trying to build a video-chat with a WebRTC. The problem which I have, is that the video-offer is going in both sides like:
PC1 -> send video-offer
PC2 <- handle video-offer message
PC2 -> send video-answer
PC1 <- handle video-answer (when there is debugger breakpoint, the connection is working)
PC2 -> send video-offer
...
I'm sending video-offer message only in negotiationEventHandler:
function handleNegotiationNeededEvent() {
logMessage('HandleNegotiationNeededEvent fired!');
myPeerConnection.createOffer() //tworzymy SDP offer dla drugiego uzytkownika
.then(function (offer) {
return myPeerConnection.setLocalDescription(offer);
})
.then(function () {
sendToWebSocket(msgTypeVideoOffer, senderId, receiverId, JSON.stringify(myPeerConnection.localDescription));
})
.catch(reportError);
}
and handling video-offer / video-answer in two functions
function handleVideoOfferMsg(msg){
logMessage("handle video-offer message call");
var localStream = null;
createPeerConnection();
var desc = new RTCSessionDescription(JSON.parse(msg.messageContent));
debugger;
myPeerConnection.setRemoteDescription(desc)
.then(function () {
return navigator.mediaDevices.getUserMedia(mediaConstraints);
})
.then(function (stream) {
localStream = stream;
document.getElementById("local_video").srcObject = localStream;
myPeerConnection.addStream(localStream);
})
.then(function () { //Utworz odpowiedz
return myPeerConnection.createAnswer();
})
.then(function (answer) { //Ustaw ja sobie jako lokalna
return myPeerConnection.setLocalDescription(answer);
})
.then(function () { //I wyslij SDP do peera
sendToWebSocket(msgTypeVideoAnswer, senderId, receiverId, JSON.stringify(myPeerConnection.localDescription));
})
.catch(handleGetUserMediaError);
}
function handleVideoAnswerMessage(message) {
logMessage("handle video answer message" + message);
var desc = message.messageContent;
myPeerConnection.setRemoteDescription(JSON.parse(desc));
}
The problem is, that handling video-offer is setting a new objects, what probably is a reason of losing a connection.