Add TURN server to android webRtc native

You need to set the TURN server when creating the PeerConnection.

It will go something like this:

    // Set ICE servers
    List<PeerConnection.IceServer> iceServers = new ArrayList<>();
    iceServers.add(new org.webrtc.PeerConnection.IceServer("stun:xxx.xxx.xxx.xxx"));
    iceServers.add(new org.webrtc.PeerConnection.IceServer("turn:xxx.xxx.xxx.xxx:3478", "username", "credential"));

    // Create peer connection
    final PeerConnectionFactory.Options options = new PeerConnectionFactory.Options();
    PeerConnectionFactory factory = new PeerConnectionFactory(new PeerConnectionFactory.Options());
    MediaConstraints constraints = new MediaConstraints();
    PeerConnection peerConnection = factory.createPeerConnection(iceServers, constraints, new YourPeerConnectionObserver());

I have not run this code, but you should get the idea.


WebRTC deprecated old API to create ICE servers. (Answer which uses old API)

To create ICE server you need to use IceServer builder pattern.

 PeerConnection.IceServer stun =  PeerConnection.IceServer.builder("stun:stun.l.google.com:19302").createIceServer();
 PeerConnection.IceServer turn =  PeerConnection.IceServer.builder("turn:numb.viagenie.ca").setUsername("[email protected]").setPassword("muazkh").createIceServer();