webrtc onaddstream不会在第一个对端被调用

我创build了下面的脚本,这是混合应用程序的一部分,有时它的function正常,我可以接收/发送audio/video通话,但有时onaddstreamontrack甚至不被发送方调用,但spd数据包正在通过套接字,我已经尝试(onaddstream or ontrack)但没有成功:

这里从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(); }); } 

在这里接受其他设备的pc2回答:

  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"; }); } 

这里是最后的function,我打电话来结束对方连接双方通话结束时:

  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(); } } 

我使用的是带有socket.io的webrtc latest-adapter.js作为信令服务器。 首先我发送事件sdp-offer pc发送的SDP数据包,并在pc2我收到来自节点服务器的sdp-offer-incoming ,比pc2发射不offeraccepted和附加sdp数据与事件,在pc1我接收SDP数据包,它显示video/两个电脑的audio,因为它应该但有时发送者doest接收stream,但接收者总是有两个video。

创build优惠时,我必须通过限制:

  this.pc.createOffer({ offerToReceiveAudio: 1, offerToReceiveVideo: 1 })