10

I've got a videoconference application that is working perfectly using HTML5 + WebRTC. The STUN/TURN server is provided by a third party company which is not for free. As you may know, WebRTC after some information exchange between browsers, it chooses the best way to connect both peers, and if possible it uses direct connection which doesn't involve the TURN server.

The question is, is it possible to detect when the RTCPeerConnection is stablished using direct connection or an intermidiate TURN server?

Keyne Viana
  • 6,194
  • 2
  • 24
  • 55
Gonzalo
  • 121
  • 2
  • 5
  • 1
    This may help https://testrtc.com/find-webrtc-active-connection/ – Keyne Viana Feb 28 '18 at 14:41
  • Thanks for the tip, it's been helpful to at least understand a little bit more what's going on – Gonzalo Feb 28 '18 at 20:21
  • You can check in the google chrome, hit chrome://webrtc-internals/ in the url and it will list the active connections, the connection in bold letters is your active connection and it will show whether it is local, reflexive or relay (TURN used). – Mhadonis Jul 20 '18 at 12:02

1 Answers1

6

This snippet works in chrome

    const stats = await pc.getStats()
    let selectedLocalCandidate
    for (const {type, state, localCandidateId} of stats.values())
        if (type === 'candidate-pair' && state === 'succeeded' && localCandidateId) {
            selectedLocalCandidate = localCandidateId
            break
        }
    return !!selectedLocalCandidate && stats.get(selectedLocalCandidate)?.candidateType === 'relay')

The idea is to iterate each report in the stats (via pc.getStats()), looking for the selected ICE candidates pair, check for the id of the local candidate and determine whether the connection uses TURN by looking at the candidate's type.

Sang
  • 4,049
  • 3
  • 37
  • 47