From f369ab4c1a6754fc0907741c982eaf8b0ad604ff Mon Sep 17 00:00:00 2001 From: Filipi Fuchter Date: Tue, 29 Apr 2025 11:23:33 -0300 Subject: [PATCH] Printing each new ice candidate. --- examples/p2p-webrtc/voice-agent/index.html | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/examples/p2p-webrtc/voice-agent/index.html b/examples/p2p-webrtc/voice-agent/index.html index 4ee386627..ddc873398 100644 --- a/examples/p2p-webrtc/voice-agent/index.html +++ b/examples/p2p-webrtc/voice-agent/index.html @@ -56,6 +56,7 @@ iceServers: [{ urls: ["stun:stun.l.google.com:19302"] }], }; const pc = new RTCPeerConnection(config) + addPeerConnectionEventListeners(pc) pc.ontrack = e => audioEl.srcObject = e.streams[0] // SmallWebRTCTransport expects to receive both transceivers pc.addTransceiver(audioTrack, { direction: 'sendrecv' }) @@ -77,18 +78,28 @@ _onConnecting() const audioStream = await navigator.mediaDevices.getUserMedia({audio: true}) peerConnection= await createSmallWebRTCConnection(audioStream.getAudioTracks()[0]) - peerConnection.oniceconnectionstatechange = () => { - console.log("oniceconnectionstatechange", peerConnection?.iceConnectionState) + } + + const addPeerConnectionEventListeners = (pc) => { + pc.oniceconnectionstatechange = () => { + console.log("oniceconnectionstatechange", pc?.iceConnectionState) } - peerConnection.onconnectionstatechange = () => { - console.log("onconnectionstatechange", peerConnection?.connectionState) - let connectionState = peerConnection?.connectionState + pc.onconnectionstatechange = () => { + console.log("onconnectionstatechange", pc?.connectionState) + let connectionState = pc?.connectionState if (connectionState === 'connected') { _onConnected() } else if (connectionState === 'disconnected') { _onDisconnected() } } + pc.onicecandidate = (event) => { + if (event.candidate) { + console.log("New ICE candidate:", event.candidate); + } else { + console.log("All ICE candidates have been sent."); + } + }; } const _onConnecting = () => {