Printing each new ice candidate.

This commit is contained in:
Filipi Fuchter
2025-04-29 11:23:33 -03:00
parent 9658b75a10
commit f369ab4c1a

View File

@@ -56,6 +56,7 @@
iceServers: [{ urls: ["stun:stun.l.google.com:19302"] }], iceServers: [{ urls: ["stun:stun.l.google.com:19302"] }],
}; };
const pc = new RTCPeerConnection(config) const pc = new RTCPeerConnection(config)
addPeerConnectionEventListeners(pc)
pc.ontrack = e => audioEl.srcObject = e.streams[0] pc.ontrack = e => audioEl.srcObject = e.streams[0]
// SmallWebRTCTransport expects to receive both transceivers // SmallWebRTCTransport expects to receive both transceivers
pc.addTransceiver(audioTrack, { direction: 'sendrecv' }) pc.addTransceiver(audioTrack, { direction: 'sendrecv' })
@@ -77,18 +78,28 @@
_onConnecting() _onConnecting()
const audioStream = await navigator.mediaDevices.getUserMedia({audio: true}) const audioStream = await navigator.mediaDevices.getUserMedia({audio: true})
peerConnection= await createSmallWebRTCConnection(audioStream.getAudioTracks()[0]) 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 = () => { pc.onconnectionstatechange = () => {
console.log("onconnectionstatechange", peerConnection?.connectionState) console.log("onconnectionstatechange", pc?.connectionState)
let connectionState = peerConnection?.connectionState let connectionState = pc?.connectionState
if (connectionState === 'connected') { if (connectionState === 'connected') {
_onConnected() _onConnected()
} else if (connectionState === 'disconnected') { } else if (connectionState === 'disconnected') {
_onDisconnected() _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 = () => { const _onConnecting = () => {