17

I have a webrtc application, it works fine, but for testing purposes, I need to test if my TURN server works, but because both the testing devices are within the same network, I am unable to test, thought below code would restrict candidates to only the ones using TURN server,

function onIceCandidate(event, targetSessionID, targetUserName) {
    if (event.candidate) {
    var candidate = event.candidate.candidate;
    if(candidate.indexOf("relay")<0){ // if no relay address is found, assuming it means no TURN server
        return;
    }
    sendMessage(candidate); // using socket.io to send to the otherside
...

but I noticed that( with much frustration), this does not work, because when peer is creating answer description,

....
a=candidate:0 1 UDP 2128609535 13.198.98.221 58779 typ host
a=candidate:0 2 UDP 2128609534 13.198.98.221 58780 typ host
....

this means, the communcation is direct and not through TURN server, am I correct in assuming this? Now, how do I force the webrtc to use the TURN server?

mido
  • 24,198
  • 15
  • 92
  • 117
  • Yes, the candidates show up in that callback, but they are also subsequently added to any SDP created after. You will also have to remove the unwanted candidates from the SDPs. – Benjamin Trent Dec 01 '14 at 14:08
  • @BenjaminTrent I looked into the SDP while offer and answer, so the only way I can prevent a direct p2p connection is to manually manipulate the offer and answer string( getting from ```createOffer``` and ```createAnswer```)? – mido Dec 02 '14 at 08:47
  • 1
    I believe so, if you remove the unwanted ice candidates before setting them locally and sending them off to the peer, that SHOULD work. – Benjamin Trent Dec 02 '14 at 14:17
  • 1
    @BenjaminTrent, i just realized that I made a mistake, the TURN configuration was correct, so it was working, but once I comment out the turn config, the remote video is coming blank... so I am assuming that the current piece of code works( checked with wireshark too, the remote peer's ip is no longer appearing in network, but not sure if that is proof enough that there is no direct communication.) – mido Dec 17 '14 at 07:40

4 Answers4

13

I have no idea if or when the browsers will support this but have a look at the "ICE candidate policy" in section 4.1.1 of draft-ietf-rtcweb-jsep-08, you can see how setting the policy to "relay" will do what you want. In the current W3C API draft this is set using an RTCIceTransportPolicy value of "relay" for the iceTranportPolicy field in the configuration. Search for RTCIceTransportPolicy in https://w3c.github.io/webrtc-pc/

relay: The ICE Agent uses only media relay candidates such as candidates passing through a TURN server.

Ben Butterworth
  • 22,056
  • 10
  • 114
  • 167
8

just adding this to close the question,

function onIceCandidate(event, targetSessionID, targetUserName) {
    if (event.candidate) {
    var candidate = event.candidate.candidate;
    if(candidate.indexOf("relay")<0){ // if no relay address is found, assuming it means no TURN server
        return;
    }...

The above code works, checked to wireshark,

after adding the if(candidate.indexOf("relay")<0) condition, communication takes place only through TURN server, if server is not present/ incorrect details, connection state get's struck at new

Edit: like cullen has said in his answer, according to w3 webrtc, passing relay as iceTransportPolicy should work, but I haven't checked if it is implemented in Firefox and Chrome yet...

mido
  • 24,198
  • 15
  • 92
  • 117
7

You can now force turn by using the iceTransportPolicy attribute

let pc = new RTCPeerConnection({ iceTransportPolicy : "relay" })

https://developer.mozilla.org/en-US/docs/Web/API/RTCConfiguration/iceTransportPolicy

Harkirat Singh
  • 115
  • 2
  • 3
4

For testing purposes on firefox you can force TURN relay.

Check my answer here!

Community
  • 1
  • 1
TomFrln
  • 309
  • 2
  • 8