0

我创建了 webrtc 视频聊天应用程序,但是当我想连接对等方时,有时连接会正确建立,但有时会抛出错误。这是不同错误的列表。有人能帮我吗。什么是不正确的。

错误错误:未捕获(承诺中):OperationError:无法在'RTCPeerConnection'上执行'setLocalDescription':无法设置本地答案sdp:在错误状态下调用:稳定错误:无法在'RTCPeerConnection'上执行'setLocalDescription':无法设置本地答案 sdp:在错误状态下调用:稳定

DOMException:无法在“RTCPeerConnection”上执行“setRemoteDescription”:无法设置远程应答 sdp:在错误状态下调用:稳定

错误错误:未捕获(承诺中):InvalidModificationError:无法在“RTCPeerConnection”上执行“setLocalDescription”:SDP 与先前为该类型生成的 SDP 不匹配错误:无法在“RTCPeerConnection”上执行“setLocalDescription”:SDP 确实与之前为该类型生成的 SDP 不匹配

DOMException:处理 ICE 候选时出错

这是我的代码:

 private gotIceCandidate(event: any, peerUuid: string, currentUserId: string) {
    if (event.candidate != null) {
      const participant = this.currentGroup.participants?.filter((el: User) => el.id === peerUuid);
      const eventData = {
        applicationId: { 'ice': event.candidate, 'uuid': currentUserId, 'dest': peerUuid, 'userId': peerUuid },
        participants: participant
      };
      this.requestService.addEvent("av", "messageToSpecificUser", JSON.stringify(eventData), eventData.participants!);
    }
  }

  private createdDescription(description: any, peerUuid: string) {
    this.peerConnections[peerUuid].pc.setLocalDescription(description).then(() => {
      const participant = this.currentGroup.participants?.filter((el: User) => el.id === peerUuid);
      const eventData = {
        applicationId: { 'sdp': this.peerConnections[peerUuid].pc.localDescription, 'uuid': this.currentUser.id, 'dest': peerUuid, 'userId': peerUuid },
        participants: participant
      };
      this.requestService.addEvent("av", "messageToSpecificUser", JSON.stringify(eventData), eventData.participants!);
    });
  }


private gotMessageFromServer(message: any) {
    let signal = JSON.parse(message);
    let peerUuid = signal.uuid;
    // Ignore messages that are not for us or from ourselves
    if (peerUuid == this.currentUser.id || (signal.dest != this.currentUser.id && signal.dest != 'all')) return;

    if (signal.displayName && signal.dest == 'all') {
      // set up peer connection object for a newcomer peer
      this.setUpPeer(peerUuid, signal.displayName, false);
      const participant = this.currentGroup.participants?.filter((el: User) => el.id === peerUuid);
      const eventData = {
        applicationId: { 'displayName': this.currentUser.username, 'uuid': this.currentUser.id, 'dest': peerUuid, 'userId': peerUuid },
        participants: participant
      };
      this.requestService.addEvent("av", "messageToSpecificUser", JSON.stringify(eventData), eventData.participants!);
    }
    else if (signal.displayName && signal.dest == this.currentUser.id) {
      // initiate call if we are the newcomer peer
      this.setUpPeer(peerUuid, signal.displayName, true);
    }
    else if (signal.sdp) {
      this.peerConnections[peerUuid].pc.setRemoteDescription(new RTCSessionDescription(signal.sdp))
        .then(() => {
          // Only create answers in response to offers
          if (signal.sdp.type == 'offer') {
            this.peerConnections[peerUuid].pc.createAnswer()
              .then((description: RTCSessionDescription) =>
                this.createdDescription(description, peerUuid))
              .catch((err: any) => {
                console.error(err);
              });
          }
        }).catch((err: any) => {
          console.error(err);
        });
    }
    else if (signal.ice) {
      this.peerConnections[peerUuid].pc.addIceCandidate(new RTCIceCandidate(signal.ice))
        .catch((err: any) => {
          console.error(err);
        });
    }
  }

  private setUpPeer(peerUuid: string, displayName: string, initCall = false) {
    this.peerConnections[peerUuid] = { 'displayName': displayName, 'pc': new RTCPeerConnection(this.peerConnectionConfig) };
    this.peerConnections[peerUuid].pc.onicecandidate = (event: any) => this.gotIceCandidate(event, peerUuid, this.currentUser.id);
    this.peerConnections[peerUuid].pc.ontrack = (event: any) => this.gotRemoteStream(event, peerUuid);
    this.peerConnections[peerUuid].pc.oniceconnectionstatechange = (event: any) => this.checkPeerDisconnect(event, peerUuid);
    this.peerConnections[peerUuid].pc.addStream(this.localStream);
    if (initCall) {
       this.peerConnections[peerUuid].pc.createOffer()
        .then((description: RTCSessionDescription) =>
           this.createdDescription(description, peerUuid))
        .catch((err: any) => {
          console.error(err);
        });
    }
  }
4

0 回答 0