WebRTC-如何设置始终使用TURN服务器?

2 javascript enums google-chrome webrtc rfc5766turnserver

在标准规格中,您可以将ENUM值设置为“中继”:http : //dev.w3.org/2011/webrtc/editor/webrtc.html#idl-def-RTCIceServer

但是,如何使用以下Javascript将枚举设置为“中继”? if (tmp.indexOf("typ relay ") >= 0) {从来没有发生在我的测试中。我怎么能强迫它枚举“继电器”?

要么

这是一个错误吗?https://code.google.com/p/webrtc/issues/detail?id=1179

任何的想法。

function createPeerConnection() {
  try {
    // Create an RTCPeerConnection via the polyfill (adapter.js).
    pc = new RTCPeerConnection(pcConfig, pcConstraints);
    pc.onicecandidate = onIceCandidate;
    console.log('Created RTCPeerConnnection with:\n' +
                '  config: \'' + JSON.stringify(pcConfig) + '\';\n' +
                '  constraints: \'' + JSON.stringify(pcConstraints) + '\'.');
  } catch (e) {
    console.log('Failed to create PeerConnection, exception: ' + e.message);
    alert('Cannot create RTCPeerConnection object; \
          WebRTC is not supported by this browser.');
      return;
  }
  pc.onaddstream = onRemoteStreamAdded;
  pc.onremovestream = onRemoteStreamRemoved;
  pc.onsignalingstatechange = onSignalingStateChanged;
  pc.oniceconnectionstatechange = onIceConnectionStateChanged;
}

function onIceCandidate(event) {
  if (event.candidate) {
    var tmp = event.candidate.candidate;
    if (tmp.indexOf("typ relay ") >= 0) {
      /////////////////////////////////////////// NEVER happens
      sendMessage({type: 'candidate',
                   label: event.candidate.sdpMLineIndex,
                   id: event.candidate.sdpMid,        
                   candidate: tmp}); 
      noteIceCandidate("Local", iceCandidateType(tmp));   

    }    
  } else {
    console.log('End of candidates.');
  }
}
Run Code Online (Sandbox Code Playgroud)

igr*_*cia 5

有一个 Chrome 扩展程序WebRTC 网络限制器,它通过更改 Chrome 的隐私设置来配置 WebRTC 的网络流量的路由方式。您可以强制流量通过 TURN,而无需进行任何 SDP 修改。


编辑 1

在扩展中提供此功能的目的是针对担心其安全性的用户。您可以查看这篇关于 WebRTC 安全性的优秀文章。与此扩展相同,也可以在 FF 中通过更改标志来完成media.peerconnection.ice.relay_only。这可以在about:config. 不知道其他两个,但我敢打赌他们确实有类似的东西。

另一方面,如果您分发应用程序并希望您的所有客户都通过 TURN,您将无法访问他们的浏览器,也不能指望他们更改这些标志或安装任何扩展程序。在这种情况下,您将不得不破坏您的 SDP。您可以使用此代码

function onIceCandidate(event) {
  if (event.candidate) {
    var type = event.candidate.candidate.split(" ")[7];
    if (type != "relay") {
      trace("ICE  -  Created " + type + " candidate ignored: TURN only mode.");
      return;
    } else {
      sendCandidateMessage(candidate);
      trace("ICE  - Sending " + type + " candidate.");
    }
  } else {
    trace("ICE  - End of candidate generation.");
  }
}
Run Code Online (Sandbox Code Playgroud)

该代码取自Discussion-webrtc列表。

如果你从来没有得到这些候选者,很可能是你的 TURN 服务器没有正确配置。您可以在页面中检查您的 TURN 服务器。不要忘记删除该演示中配置的现有 STUN 服务器。


编辑 2

更简单:iceTransportPolicy: "relay"在您的 WebRtcPeer 配置中设置以强制 TURN:

var options = {
    localVideo: videoInput, //if you want to see what you are sharing
    onicecandidate: onIceCandidate,
    mediaConstraints: constraints,
    sendSource: 'screen',
    iceTransportPolicy: 'relay',
    iceServers: [{ urls: 'turn:XX.XX.XX.XX', username:'user', credential:'pass' }]
}

webRtcPeerScreencast = kurentoUtils.WebRtcPeer.WebRtcPeerSendrecv(options, function(error) {
    if (error) return onError(error) //use whatever you use for handling errors

    this.generateOffer(onOffer)
});
Run Code Online (Sandbox Code Playgroud)


小智 5

这为我工作:

iceServers = [
     { "url": "turn:111.111.111.111:1234?transport=tcp",
       "username": "xxxx",
       "credential": "xxxxx"
     }
   ]

optionsForWebRtc = {
    remoteVideo : localVideoElement,
    mediaConstraints : videoParams,
    onicecandidate : onLocalIceCandidate,
    configuration: {
        iceServers: iceServers,
        iceTransportPolicy: 'relay'
    }
}
Run Code Online (Sandbox Code Playgroud)