78bd6a9Add TURN relay and an ICE self-test indicatorJeremy Magland 1// A one-shot ICE self-test: gather candidates on a throwaway connection and
2// report what this browser can produce. Zero candidates means WebRTC is being
3// blocked (privacy extension / browser policy) and no peer connection can ever
4// form — the UI surfaces that instead of failing silently.
6import {ICE_SERVERS} from './peer'
8export interface IceTestResult {
9 total: number
10 host: boolean
11 srflx: boolean
12 relay: boolean
13}
15export const runIceTest = (ms = 6000): Promise<IceTestResult> =>
16 new Promise(resolve => {
17 const res: IceTestResult = {total: 0, host: false, srflx: false, relay: false}
18 let pc: RTCPeerConnection
19 try {
20 pc = new RTCPeerConnection({iceServers: ICE_SERVERS})
21 } catch {
22 resolve(res)
23 return
24 }
25 let settled = false
26 const done = () => {
27 if (settled) return
28 settled = true
29 clearTimeout(timer)
30 try {
31 pc.close()
32 } catch {
33 /* ignore */
34 }
35 resolve(res)
36 }
37 const timer = setTimeout(done, ms)
38 pc.onicecandidate = e => {
39 if (!e.candidate) {
40 done()
41 return
42 }
43 res.total++
44 const c = e.candidate.candidate
45 if (c.includes(' typ host')) res.host = true
46 if (c.includes(' typ srflx')) res.srflx = true
47 if (c.includes(' typ relay')) res.relay = true
48 }
49 pc.createDataChannel('icetest')
50 pc.createOffer()
51 .then(o => pc.setLocalDescription(o))
52 .catch(done)
53 })