Configuring the voice-agent example to use ice-servers and wait for all the ice candidates.
This commit is contained in:
@@ -24,27 +24,44 @@
|
|||||||
let connected = false
|
let connected = false
|
||||||
let peerConnection = null
|
let peerConnection = null
|
||||||
|
|
||||||
/*const waitForIceGatheringComplete = async (pc) => {
|
const waitForIceGatheringComplete = async (pc, timeoutMs = 5000) => {
|
||||||
if (pc.iceGatheringState === 'complete') return;
|
if (pc.iceGatheringState === 'complete') return;
|
||||||
|
console.log("Waiting for ICE gathering to complete...");
|
||||||
return new Promise((resolve) => {
|
return new Promise((resolve) => {
|
||||||
|
let timeoutId;
|
||||||
const checkState = () => {
|
const checkState = () => {
|
||||||
|
console.log("icegatheringstatechange:", pc.iceGatheringState);
|
||||||
if (pc.iceGatheringState === 'complete') {
|
if (pc.iceGatheringState === 'complete') {
|
||||||
pc.removeEventListener('icegatheringstatechange', checkState);
|
cleanup();
|
||||||
resolve();
|
resolve();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
const onTimeout = () => {
|
||||||
|
console.warn(`ICE gathering timed out after ${timeoutMs} ms.`);
|
||||||
|
cleanup();
|
||||||
|
resolve();
|
||||||
|
};
|
||||||
|
const cleanup = () => {
|
||||||
|
pc.removeEventListener('icegatheringstatechange', checkState);
|
||||||
|
clearTimeout(timeoutId);
|
||||||
|
};
|
||||||
pc.addEventListener('icegatheringstatechange', checkState);
|
pc.addEventListener('icegatheringstatechange', checkState);
|
||||||
|
timeoutId = setTimeout(onTimeout, timeoutMs);
|
||||||
});
|
});
|
||||||
}*/
|
};
|
||||||
|
|
||||||
|
|
||||||
const createSmallWebRTCConnection = async (audioTrack) => {
|
const createSmallWebRTCConnection = async (audioTrack) => {
|
||||||
const pc = new RTCPeerConnection()
|
const config = {
|
||||||
|
iceServers: [{ urls: ["stun:stun.l.google.com:19302"] }],
|
||||||
|
};
|
||||||
|
const pc = new RTCPeerConnection(config)
|
||||||
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' })
|
||||||
pc.addTransceiver('video', { direction: 'sendrecv' })
|
pc.addTransceiver('video', { direction: 'sendrecv' })
|
||||||
await pc.setLocalDescription(await pc.createOffer())
|
await pc.setLocalDescription(await pc.createOffer())
|
||||||
//await waitForIceGatheringComplete(pc)
|
await waitForIceGatheringComplete(pc)
|
||||||
const offer = pc.localDescription
|
const offer = pc.localDescription
|
||||||
const response = await fetch('/api/offer', {
|
const response = await fetch('/api/offer', {
|
||||||
body: JSON.stringify({ sdp: offer.sdp, type: offer.type}),
|
body: JSON.stringify({ sdp: offer.sdp, type: offer.type}),
|
||||||
@@ -57,9 +74,14 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
const connect = async () => {
|
const connect = async () => {
|
||||||
|
_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)
|
||||||
|
}
|
||||||
peerConnection.onconnectionstatechange = () => {
|
peerConnection.onconnectionstatechange = () => {
|
||||||
|
console.log("onconnectionstatechange", peerConnection?.connectionState)
|
||||||
let connectionState = peerConnection?.connectionState
|
let connectionState = peerConnection?.connectionState
|
||||||
if (connectionState === 'connected') {
|
if (connectionState === 'connected') {
|
||||||
_onConnected()
|
_onConnected()
|
||||||
@@ -69,6 +91,12 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const _onConnecting = () => {
|
||||||
|
statusEl.textContent = "Connecting"
|
||||||
|
buttonEl.textContent = "Disconnect"
|
||||||
|
connected = true
|
||||||
|
}
|
||||||
|
|
||||||
const _onConnected = () => {
|
const _onConnected = () => {
|
||||||
statusEl.textContent = "Connected"
|
statusEl.textContent = "Connected"
|
||||||
buttonEl.textContent = "Disconnect"
|
buttonEl.textContent = "Disconnect"
|
||||||
|
|||||||
Reference in New Issue
Block a user