Receiving iceGatheringState complete takes a long time

Hi,

I’m using the latest version of this library and it’s great, thanks everyone involved building it.

I have once slight issue though I’m wondering if anyone can help me understand. I’m using this library to stream ip camera footage from go2rtc. When creating my offer and sending this over to go2rtc, I’m not using trickle ice but gathering all my ICE candidates at once.

My function below will resolve the promise once it times out after 5 seconds or all the candidates are gathered. My issue is that I can see all my candidates being gathered very fast maybe 1-2 seconds (if I use console.log) and no more are added. If I remove the timeout, the iceGatheringState will not be reporting “complete” until after 40 seconds which I assume is a timeout.

    const getOffer = useCallback(async (pc: RTCPeerConnection): Promise<string> => {
        return new Promise((resolve) => {
            const checkState = () => {
                if (pc.iceGatheringState === 'complete') {
                    resolve(pc.localDescription?.sdp || '')
                }
            }

            pc.addEventListener('icegatheringstatechange', checkState)
            pc.addEventListener('icecandidate', (event) => {
                if (!event.candidate) {
                    checkState()
                }
            })

            pc.createOffer({}).then((offer) => {
                pc.setLocalDescription(offer)
            })

            setTimeout(() => {
                if (pc.iceGatheringState !== 'complete') {
                    resolve(pc.localDescription?.sdp || '')
                }
            }, 5000)
        })
    }, [])

Does anyone know why I’m seeing this behavior? I was assuming that as soon as no more candidates will be added or it’s “done”, that the event will report “complete” state.

Appreciate any answers,
Berg