Reputation: 121
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?
Upvotes: 10
Views: 4466
Reputation: 4446
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.
Upvotes: 6