【问题标题】:WebRTC - RTCPeerConnection.localDescription returns null in Firefox, but works correctly in ChromeWebRTC - RTCPeerConnection.localDescription 在 Firefox 中返回 null,但在 Chrome 中正常工作
【发布时间】:2019-03-04 23:17:36
【问题描述】:

我使用 WebRTC 构建了一个简单的流媒体服务。我目前仍在通过 localhost 运行所有内容。目前使用 Chrome 浏览器时一切正常,但使用 Firefox 时无法连接。我正在使用 WebRTC-Adapter 垫片。

问题似乎源于 peerConnection.localDescription 始终等于 null,并且无法将我的 localDescription 发送给对等方,或正确设置 remoteDescription。

这是我的代码的 sn-p。这仅涵盖正在启动 p2p 连接的流的接收者。流媒体已经设置了本地流,并设置了自己的本地和远程描述,然后将 localDescription 发送给接收者。 sendRecipientDescription() 只处理通过套接字将 sdp 发送到流媒体。 PC_Config 只包含一个 STUN 服务器:

setUpRecipient = () => {
  this.createPeerConnection();
  this.pc
    .createOffer({ offerToReceiveVideo: true })
    .then(offer => {
      this.pc.setLocalDescription(offer);
    })
    .then(() => {
      this.sendRecipientDescription();
      console.log('recipient local description ', this.pc.localDescription);
    })
    .catch(e => {
      console.log('error recipient set up ', e);
    });
};

createPeerConnection = () => {
  try {
    this.pc = new RTCPeerConnection(PC_CONFIG);
    this.pc.onicecandidate = this.handleIceCandidate;
    this.pc.ontrack = this.handleRemoteStreamAdded;
    this.pc.onremovetrack = this.handleRemoteStreamRemoved;
    this.pc.oniceconnectionstatechange = this.handleIceStateChange;
    console.log('Created RTCPeerConnection', this.pc.localDescription);
  } catch (e) {
    console.log('Failed to create PeerConnection, exception: ', e.message);
  }
};

使用 Chrome 浏览器时,this.pc.localDescription 按预期返回。使用火狐浏览器时,this.pc.localDescription 始终为null,根本没有RTCSessionDescription。当我在 setLocalDescription 之后 console.log(this.pc) 时,似乎 localDescription 确实为空:RTCPeerConnection un-expanded

但是,当我展开 RTCPeerConnection 对象时,您会看到 localDescription 似乎设置正确:RTCPeerConnection expanded。但是,当我尝试发送 this.pc.localDescription 时,它只发送 null。

【问题讨论】:

    标签: javascript google-chrome firefox webrtc


    【解决方案1】:

    我找到了自己问题的答案。显然我需要返回 this.pc.setLocalDescription();

    我不知道为什么这是必要的。据我所知,pc.setLocalDescription 不返回任何东西,只有设置 pc.localDescription 的副作用。它在 Chrome 中运行良好,但在 Firefox 中却不行。

    【讨论】:

    • setLocalDescription 返回一个承诺。如果没有 return 语句,promise 链将不会等待异步操作完成,然后继续sendRecipientDescription。你有一个竞争条件,碰巧在 Chrome 中有效,但在 FF 中无效,因为在某些时候时间不同。
    • 感谢诺帕的解释,我很感激。
    猜你喜欢
    • 2016-09-16
    • 2020-12-30
    • 2016-07-13
    • 2018-02-24
    • 2014-08-12
    • 2016-10-06
    • 1970-01-01
    • 2013-07-31
    • 1970-01-01
    相关资源
    最近更新 更多