【问题标题】:[InvalidStateError: "setRemoteDescription needs to called before addIceCandidate" code: 11[InvalidStateError:“setRemoteDescription 需要在 addIceCandidate 之前调用”代码:11
【发布时间】:2017-08-08 14:58:47
【问题描述】:

我使用 web Rtc 和 websockets 创建了一个简单的视频通话应用程序。 但是当我运行代码时,出现以下错误。

DOMException [InvalidStateError: "setRemoteDescription 需要在 addIceCandidate 之前调用" 代码:11

我不知道如何解决这个错误。 下面是我的代码:

enter code here

var localVideo;
var remoteVideo;
var peerConnection;
var uuid;
var localStream;
var peerConnectionConfig = {
'iceServers': [
    {'urls': 'stun:stun.services.mozilla.com'},
    {'urls': 'stun:stun.l.google.com:19302'},
]
};

function pageReady() {
    uuid = uuid();
    console.log('Inside Page Ready');
    localVideo = document.getElementById('localVideo');
    remoteVideo = document.getElementById('remoteVideo');

   serverConnection = new WebSocket('wss://' + window.location.hostname + 
   ':8443');
   serverConnection.onmessage = gotMessageFromServer;

   var constraints = {
       video: true,
       audio: true,
   };

   if(navigator.mediaDevices.getUserMedia) {

   navigator.mediaDevices.getUserMedia(constraints)
   .then(getUserMediaSuccess).catch(errorHandler);
   }else
   {
       alert('Your browser does not support getUserMedia API');
   }
   }

   function getUserMediaSuccess(stream) {
        localStream = stream;
        localVideo.src = window.URL.createObjectURL(stream); 
   }

   function start(isCaller) {
       console.log('Inside isCaller');
       peerConnection = new RTCPeerConnection(peerConnectionConfig);
       peerConnection.onicecandidate = gotIceCandidate;
       peerConnection.onaddstream = gotRemoteStream;
       peerConnection.addStream(localStream);

       if(isCaller) {
            console.log('Inside Caller to create offer');
            peerConnection.createOffer().
            then(createdDescription).catch(errorHandler);
       }
      }

   function gotMessageFromServer(message) {
   console.log('Message from Server');
   if(!peerConnection) 
   {
        console.log('Inside !Peer Conn');
        start(false);
   }

   var signal = JSON.parse(message.data);

   // Ignore messages from ourself
   if(signal.uuid == uuid) return;

   if(signal.sdp) {
        console.log('Inside SDP');
        peerConnection.setRemoteDescription(new 
        RTCSessionDescription(signal.sdp)).then(function() {
        // Only create answers in response to offers
        if(signal.sdp.type == 'offer') {
            console.log('Before Create Answer');
            peerConnection.createAnswer().then(createdDescription)
            .catch(errorHandler);
        }
     }).catch(errorHandler);
     } else if(signal.ice) {
           console.log('Inside Signal Ice');
           peerConnection.addIceCandidate(new 
           RTCIceCandidate(signal.ice)).catch(errorHandler);
     }

    }

    function gotIceCandidate(event) {
         console.log('Inside Got Ice Candi');
         if(event.candidate != null) {
         serverConnection.send(JSON.stringify({'ice': event.candidate, 
         'uuid': uuid}));
    }
  }

  function createdDescription(description) {
  console.log('got description');

    peerConnection.setLocalDescription(description).then(function() {
    console.log('Inside Setting ');
    serverConnection.send(JSON.stringify({'sdp': 
    peerConnection.localDescription, 'uuid': uuid}));
   }).catch(errorHandler);
  }

  function gotRemoteStream(event) {
  console.log('got remote stream');
  remoteVideo.src = window.URL.createObjectURL(event.stream);
  }

  function errorHandler(error) {
     console.log(error);
  }

  // Taken from http://stackoverflow.com/a/105074/515584
  // Strictly speaking, it's not a real UUID, but it gets the job done here
  function uuid() {
      function s4() {
      return Math.floor((1 + Math.random()) * 
      0x10000).toString(16).substring(1);
      }

  return s4() + s4() + '-' + s4() + '-' + s4() + '-' + s4() + '-' + s4() + 
  s4() + s4();
  }

这是我的代码,我不知道addIceCandidate和addRemoteDescription函数怎么安排。

【问题讨论】:

    标签: websocket webrtc videochat


    【解决方案1】:

    您需要确保 peerConnection.addIceCandidate(new RTCIceCandidate(signal.ice)) 在设置描述后调用。 在 peerConnection 完成设置描述之前,您会收到 ice 候选并尝试将其添加到 peerConnection。

    我有类似的情况,我创建了一个数组来存储在设置描述完成之前到达的候选者,以及一个检查描述是否设置的变量。如果设置了描述,我会将候选者添加到 peerConnection,否则我会将它们添加到数组中。 (当您将变量设置为 true 时,您还可以遍历数组并将所有存储的候选对象添加到 peerConnection。

    【讨论】:

    • 嗨!我正是这样做的,我的沟通开始起作用了。非常感谢。
    【解决方案2】:

    WebRTC 的工作方式(据我所知)是您必须让两个对等方达成协议,以便按照向您的对等方提供报价的顺序相互交流如果您想发送媒体流进行视频对话,请继续沟通

    让您有一个很好的例子来了解如何实现这些功能,以及您可以访问https://github.com/alexan1/SignalRTC 的顺序,他对如何执行此操作有很好的理解。

    此时您可能已经有解决问题的方法,但如果您没有,我会回复您。

    【讨论】:

      【解决方案3】:

      编辑:正如我被告知的那样,此解决方案是一种反模式,您应该以这种方式实现它。有关我如何在保持合理流程的同时解决它的更多信息,请关注此答案和评论部分:https://stackoverflow.com/a/57257449/779483

      TLDR:不要在信令信息到达后立即调用addIceCandidate,而是将候选者添加到队列中。调用setRemoteDescription 后,通过候选人队列并在每个候选人上调用addIceCandidate

      --

      this answer 我了解到,在添加 Ice Candidates 数据之前,我们必须调用 setRemoteDescription(offer)

      所以,扩展@Luxior 的答案,我做了以下事情:

      • 当带有候选人的信令消息到达时:
        • 检查是否设置了远程(通过布尔标志,即:remoteIsReady
        • 如果是,请致电addIceCandidate
        • 如果不是,请添加到队列中
      • 在调用setRemoteDescription 之后(在应答信号或应答客户端操作中):
        • 调用一个方法来遍历候选队列并在每个队列上调用addIceCandidate
        • 将布尔标志 (remoteIsReady) 设置为 true
        • 空队列

      【讨论】:

      • W3C 建议不要这样做。可能值得在 GitHub 上询问以进行澄清。 IMO WebRTC 应该更新以允许这样做,但我无法说服人们。 github.com/w3c/webrtc-pc/issues/2519
      • @SeanDuBois 这是我让它工作的唯一方法=/
      • 是的,我个人的感觉和你一样。我希望引起对 GitHub 的关注,这样我就可以在幕后进行排队(所以用户不需要在 Javascript 中这样做)
      • @SeanDuBois 我能够按照此答案和评论部分中的提示删除反模式而不会弄乱我的流程:stackoverflow.com/a/57257449/779483
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-04-01
      • 1970-01-01
      • 2015-09-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-10
      相关资源
      最近更新 更多