javascript - WebRTC 从不触发 IceCandidate

标签 javascript webrtc

我开始使用 WebRTC 进行开发,但那东西从来没有给我 ICE 候选人。我设置了一切,我正在交换描述和东西,我还在那里做了一个 super 丑陋的功能,以确保一切都正确运行,一个接一个。两者的信号状态都是稳定的,onError 永远不会被触发(如预期的那样),但 onIceCandidate 也会(不像预期的那样),当我想发送一个随机的空 MediaStream 对象时 pc1.addStream (new webkitMediaStream());,它总是触发 onNegotiationNeeded

有谁知道我的代码到底出了什么问题?我花了几个小时浏览 Stack Overflow、HTML5 Rocks 和 W3C 文档,但我不明白。这是我的全部代码:

var config={
  'iceServers':[{
    'url':'stun:stun.l.google.com:19302'
  },{
    'url':'stun:stun1.l.google.com:19302'
  },{
    'url':'stun:stun2.l.google.com:19302'
  },{
    'url':'stun:stun3.l.google.com:19302'
  },{
    'url':'stun:stun4.l.google.com:19302'
  }]
};
var pc1=new webkitRTCPeerConnection(config);
var pc2=new webkitRTCPeerConnection(config);

var onError=function(error)
{
  console.error(error);
}

pc1.onicecandidate=function()
{
  console.log('PC1 onIceCandidate (finally) fired!');
}
pc2.onicecandidate=function()
{
  console.log('PC2 onIceCandidate (finally) fired!');
}

pc1.oniceconnectionstatechange=function()
{
  console.log('PC1 oniceconnectionstatechange fired!');
}
pc2.oniceconnectionstatechange=function()
{
  console.log('PC2 oniceconnectionstatechange fired!');
}
pc1.onnegotiationneeded=function()
{
  console.log('PC1 onnegotiationneeded fired!');
}
pc2.onnegotiationneeded=function()
{
  console.log('PC2 onnegotiationneeded fired!');
}

pc1.createOffer(function(offer){
  pc1.setLocalDescription(offer,function(){
    pc2.setRemoteDescription(new RTCSessionDescription(offer),function(){
      pc2.createAnswer(function(answer){
        pc2.setLocalDescription(answer,function(){
          pc1.setRemoteDescription(new RTCSessionDescription(answer),new Function()/*I don't need you, bro*/,onError);
        },onError);
      },onError);
    },onError);
  },onError);
},onError);

顺便说一句,我正在使用 Google Chrome 进行开发。我会确保它也能在 Firefox 中运行,但现在问题应该是跨浏览器的。我想在之前将其加入数据通道...(但我不反对使用 Firefox 或跨浏览器代码的工作解决方案)

最佳答案

在 Chrome 38 及更早版本中,OfferToReceiveAudio 默认为 true。从 Chrome 39 开始,OfferToReceiveAudio 默认为 false,正如 WebRTC 工程师在 PSA: Behavior change to PeerConnection.createOffer constraint OfferToReceiveAudio 宣布的那样(引述如下)。
由于此更改,createOffer 返回的 SDP 不包含任何媒体,因此 ICE 收集过程永远不会启动。您可以通过观察从未触发 ICE 事件,并且 PeerConnection 的 iceGatheringStateiceConnectionState 保持"new"状态来注意到此更改的结果。

为确保 ICE 聚会开始并完成,您必须在报价中添加媒体,例如通过在以下约束条件中设置 OfferToReceiveAudio:true(作为 PeerConnection constructor 的参数,或作为 peerConnection.createOffer 方法的参数):

{
    mandatory: {
        OfferToReceiveAudio: true
    }
}

(在 SDP 中获取媒体的其他方法包括设置 OfferToReceiveVideo:true,或使用从 getUserMedia 获取的媒体流调用 peerConnection.addStream)


webrtc-讨论:PSA: Behavior change to PeerConnection.createOffer constraint OfferToReceiveAudio :

I'm going to submit a change (https://webrtc-codereview.appspot.com/16309004/) to change the behavior of RTCPeerConnection.createOffer. The change is expected to be included in Chrome M39.

What's changed:

Currently if the OfferToReceiveAudio constraint is not specified in PeerConnection.createOffer, the resulted offer SDP will have an "m=audio" line even if there is no audio track attached to the PeerConnection. In other words, OfferToReceiveAudio defaults to true.

After my change, OfferToReceiveAudio no longer defaults to true. Whether the offer SDP has an "m=audio" line depends on if any audio track has been attached to the PeerConnection.

What's not changed:

The behavior of setting an explicit value for OfferToReceiveAudio remains the same, i.e. OfferToReceiveAudio:true will result in an "m=audio" line regardless of the existence of audio tracks; OfferToReceiveAudio:false will result in no "m=audio" line regardless of the existence of audio tracks, unless setLocalDescription has been called with an SDP containing an "m=audio" line, in which case the new offer SDP will mark the audio content inactive instead of removing the audio content.

关于javascript - WebRTC 从不触发 IceCandidate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27489881/

相关文章:

javascript - 在循环中迭代上一个下一个

javascript - AWS API 网关方法测试 : Error Authorization not configured

javascript - 在单元测试中自动允许 WebRTC 权限

node.js - 使用 WebRTC.io 和 NodeJS 到达时连接到房间

webrtc - 有没有办法访问 NAT 后面的 Web 服务器?

java - Kurento 示例错误

java - Tomcat 网络套接字不工作

javascript - jquery UI 工具提示的自定义背景图片

javascript - document.getElementById ('url_input' ).value 不打印该值

webrtc - 创建 WebRTC 接收器