WebRTC - how to set always to use TURN server?

user285594 picture user285594 · Mar 2, 2014 · Viewed 12.9k times · Source

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

Answer

Jaguar Lakatos picture Jaguar Lakatos · Aug 26, 2017

This is working for me:

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'
    }
}