Add support for trickle ice (#193)

* cherry-pick trickle-ice

* Add code

* Add code

* format
This commit is contained in:
Freddy Boulton
2025-03-20 20:50:45 -04:00
committed by GitHub
parent 3fed4cb2ad
commit 6742894d3d
6 changed files with 136 additions and 46 deletions

View File

@@ -129,29 +129,22 @@ async function negotiate(
webrtc_id: string,
reject_cb: (msg: object) => void = () => {},
): Promise<void> {
pc.onicecandidate = ({ candidate }) => {
if (candidate) {
console.debug("Sending ICE candidate", candidate);
server_fn({
candidate: candidate.toJSON(),
webrtc_id: webrtc_id,
type: "ice-candidate",
}).catch((err) => console.error("Error sending ICE candidate:", err));
}
};
return pc
.createOffer()
.then((offer) => {
return pc.setLocalDescription(offer);
})
.then(() => {
// wait for ICE gathering to complete
return new Promise<void>((resolve) => {
console.debug("ice gathering state", pc.iceGatheringState);
if (pc.iceGatheringState === "complete") {
resolve();
} else {
const checkState = () => {
if (pc.iceGatheringState === "complete") {
console.debug("ice complete");
pc.removeEventListener("icegatheringstatechange", checkState);
resolve();
}
};
pc.addEventListener("icegatheringstatechange", checkState);
}
});
})
.then(() => {
var offer = pc.localDescription;
return make_offer(