4

In the standard specs it says you can set ENUM value to "relay" : http://dev.w3.org/2011/webrtc/editor/webrtc.html#idl-def-RTCIceServer

but, How do you set the enum to "relay" using Javascript of following? if (tmp.indexOf("typ relay ") >= 0) { never occur in my test. how can i force it to enum "relay"?

or

is it a BUG? https://code.google.com/p/webrtc/issues/detail?id=1179

Any idea.

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.');
  }
}
4

3 回答 3

6

这对我有用:

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'
    }
}
于 2017-08-26T21:29:50.817 回答
5

有一个 Chrome 扩展程序WebRTC Network Limiter,它通过更改 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.");
  }
}

该代码取自讨论-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)
});
于 2016-04-08T15:33:01.403 回答
2

To force the usage of a TURN server, you need to intercept the candidates found by the browser. Just like you are doing.

But if it never occurs in your testings, you may have some problems with your TURN server. If you have a working TURN server, you'll get the candidates with typ relay.

Remember that you need to configure TURN server with authentication. It is mandatory and the browser will only use the candidates "relay" if the request is authenticated. Your iceServers config for PeerConnection must have the credentials defined for TURN servers.

于 2014-03-03T11:51:29.997 回答