即使协商完成,远程上的WebRTC调用也不起作用

问题描述

我已经编写了代码来接收Web上由移动设备(Android / IOS)启动的WebRTC呼叫。当我在同一网络中同时使用移动设备和笔记本电脑尝试此操作时,都可以正常工作,但是当它们位于不同的网络上时,对等连接通常会失败,并且有时对等连接的信令状态会失败。

这是我的代码

 getPeerConnection() { //RTCPeerConnection
const servers = { 'iceServers': [{ 'urls': 'stun:stun.l.google.com:19302' }] }
this.peerConnection=null;
this.peerConnection = new RTCPeerConnection(servers);
this.dataChannelService.createDataChannel(this.peerConnection)
console.log(this.peerConnection);
this.addMediaToPeerConnection();

this.peerConnection.addEventListener('connectionstatechange',(event) => {
  console.log(this.peerConnection);
  console.log(event);
  
  if (this.peerConnection.connectionState == "connected")
    this.callStatus = CallStatus.CONNECTED;
  if (this.peerConnection.connectionState  == "Failed" || this.peerConnection.connectionState  == "closed" ){this.disconnectCall();
  }
})

//set local description & create offer
this.peerConnection.addEventListener('negotiationneeded',(event) => {
  let offerOptions = { 'offerToReceiveVideo': true,'offerToReceiveAudio': true };
  this.peerConnection
    .createOffer(offerOptions)
    .then((sdp: RTCSessionDescription) => {
      return this.peerConnection
        .setLocalDescription(sdp)
        .then(() => {
          let localSDP = sdp.sdp;
          console.log(sdpTransform.parse(localSDP));
          this.sendOffer(localSDP)
        })
    },(e) => {
      console.error(e);
    })
})

this.peerConnection.addEventListener('icecandidate',(event) => {
  let iceCandidate = event.candidate;
  if (iceCandidate != null) {
    let sdpMid = iceCandidate.sdpMid;
    let sdpMLineIndex = iceCandidate.sdpMLineIndex;
    let candidate = iceCandidate.candidate;
    this.sendICECandidate(this.callerUserId,sdpMid,sdpMLineIndex,candidate)
  }
});

this.peerConnection.addEventListener('renegotiationneeded',() => {
  console.log("renagotiation needed");
})

this.peerConnection.addEventListener('addstream',async (event) => {
  console.log(event);
  this.remoteVideoService.setRemoteMedia(event.stream);
  this.dialog.open(VideoCallComponent,{ panelClass: 'videoCallDialog',disableClose: true,data: { "callerName": this.callerName } }).afterClosed().subscribe(response => {
    if(response && response.data=="hangUp"){
      this.disconnectCall();
    }
  });
});

**要接收ICE候选人,我们正在使用webSocket,如**

   handleIceCandidate(data) {
if (data) {
  let candidate = { candidate: data.data.candidate,sdpMid: data.data.id,sdpMLineIndex: data.data.label }
  var iceCandidate: RTCIceCandidateInit = candidate;
  console.log(iceCandidate);
  this.peerConnection.addIceCandidate(iceCandidate);
}

}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)