I have created below script which is part of a hybrid application, sometime it function properly and I can receive/send audio/video calls , but sometime onaddstream
or ontrack
isn't even called from the sender side, but spd packets are being sent via socket, I have tried both (onaddstream or ontrack)
but no success:
here to send the offer from pc
:
sendOffer() {
let that = this;
that.call_status = 'connecting';
let call_type;
if (that.call_type == 'audio')
call_type = { video: false, audio: true };
else
call_type = { video: true, audio: true };
that.pc = new RTCPeerConnection(that.peerConnectionConfig);
that.haveGum = navigator.mediaDevices.getUserMedia(call_type)
.then(stream => {
that.pc.addStream(that.from_video.nativeElement.srcObject = stream);
that.from_video.nativeElement.style.display = 'block';
}).then(() => that.pc.createOffer())
.then(d => that.pc.setLocalDescription(d))
.catch(log => { alert(log) });
that.pc.oniceconnectionstatechange = function (e) {
that.call_status = that.pc.iceConnectionState;
if (that.pc.iceConnectionState == 'disconnected') {
console.log('Disconnected');
}
}
that.pc.onaddstream = e => {
that.to_video.nativeElement.srcObject = e.stream;
};
that.pc.onicecandidate = e => {
if (e.candidate) {
return;
}
that.offerSent = true;
that.socket.emit('sdp-offer', {
from: that.user,
sdp: that.pc.localDescription.sdp,
call_type: call_type
});
};
that.socket.on('sdp-offer-reply', (sdp: any) => {
that.pc.setRemoteDescription(new RTCSessionDescription(({ type: "answer", sdp: sdp.sdp }))).catch(log => console.log(log));
});
that.socket.on('call-closed', (sdp: any) => {
that.closeConnection();
});
}
and here on other device pc2
when accept answer:
answerCall() {
let that = this;
let call_type;
if (this.call_type == 'audio')
call_type = { video: false, audio: true };
else
call_type = { video: true, audio: true };
that.pc2 = new RTCPeerConnection(this.peerConnectionConfig);
that.haveGum = navigator.mediaDevices.getUserMedia(call_type)
.then(stream => {
that.pc2.addStream(this.from_video.nativeElement.srcObject = stream);
});
that.pc2.oniceconnectionstatechange = function (e) {
console.log(that.pc2.iceConnectionState);
}
that.pc2.onaddstream = e => {
that.to_video.nativeElement.srcObject = e.stream;
that.to_video.nativeElement.style.display = 'block';
};
if (that.pc2.signalingState != "stable") {
that.call_status = that.pc2.signalingState;
alert("not stable");
return;
}
that.pc2.setRemoteDescription(new RTCSessionDescription(({ type: "offer", sdp: this.sdp.sdp })))
.then(() => that.pc2.createAnswer())
.then(d => {
that.sendSdpAnswer = d; that.pc2.setLocalDescription(d);
this.call_connected = true;
})
.catch(log => console.log(log));
that.pc2.onicecandidate = e => {
if (e.candidate) {
console.log("not e.candidate");
return;
}
that.socket.emit('offeraccepted', {
from: that.user,
sdp: that.sendSdpAnswer.sdp
});
};
that.socket.on('call-closed', (sdp: any) => {
that.closeConnection();
that.call_status = "Hung Up";
});
}
and here is final function I am calling to close the peer connection on both sides when call ends:
closeConnection() {
if (typeof this.pc !== "undefined" && this.pc.signalingState != "closed") {
this.pc.close();
}
if (typeof this.pc2 !== "undefined" && this.pc2.signalingState != "closed") {
this.pc2.close();
}
}
I am using webrtc latest-adapter.js with socket.io as signaling server. First I emit event sdp-offer
on pc
to send the sdp packets and on pc2
I receive sdp-offer-incoming
from node server, than pc2
emit offeraccepted
and attach sdp data with event, on pc1
i receive sdp packets and it display the video/audio on both pc's as it should but sometime sender doest receive the stream, But receiver always have both videos.