Free, open-source screen sharing for private live streams with friends. Watch together in a browser or self-host Piik. 免费开源的私密屏幕共享,支持游戏直播、一起看电影与自部署。
fix: allow bounded NAT route recovery
AAA5e67d05a9097
diff --git a/docs/adr/0009-optional-nat-prediction.md b/docs/adr/0009-optional-nat-prediction.mdindex 0eea44b7..96f91986 100644--- a/docs/adr/0009-optional-nat-prediction.md+++ b/docs/adr/0009-optional-nat-prediction.md@@ -58,6 +58,11 @@ connection acquisition reuses the route controller's existing opportunity ledgerthe remaining attempts behind a working SFU route. A new connection canprovide another mapping opportunity, not guaranteed independent randomness:in particular, Native connections may retain the same shared UDP socket.+ When prediction is enabled, that route-owned Browser P2P edge may also use+ the existing bounded automatic-recovery budget (two ICE restarts) before+ reporting the edge failed. Each restart creates a fresh ICE gathering+ generation without adding a route candidate, SFU fallback, or second retry+ state; unsuccessful recovery follows the normal route-failed path.## Consequencesdiff --git a/docs/product/routing-transport.md b/docs/product/routing-transport.mdindex 40e96d96..bc74b52d 100644--- a/docs/product/routing-transport.md+++ b/docs/product/routing-transport.md@@ -98,6 +98,10 @@ begins only after that exact candidate commits as the active route.When NAT traversal is enabled, another operation may use the remainingconnection-attempt budget after rollback. The waiting display uses the actualserver-issued attempt ordinal; it never counts time as an attempted connection.+When NAT prediction is enabled, a route-owned P2P candidate may also use its+existing bounded automatic-recovery budget (two ICE restarts) before it reports+that failure; each restart creates a fresh mapping generation without reopening+the route candidate or introducing a second reconciliation loop.When a newly committed Host-root Viewer exposes unused downstream capacity whileanother Host root has at least two direct children, the same background operation maydiff --git a/src/client/webrtc/viewer-peer.ts b/src/client/webrtc/viewer-peer.tsindex 6f22f17d..a19a6a52 100644--- a/src/client/webrtc/viewer-peer.ts+++ b/src/client/webrtc/viewer-peer.ts@@ -440,7 +440,14 @@ export class ViewerPeer implements ViewerMediaPeer {if (this.recoveryOwner === "route") {this.clearDisconnectTimer();if (state === "failed") {- this.reportRecoveryExhausted();+ if (+ this.natPredictionEnabled &&+ this.automaticRecoveryRequests < VIEWER_MAX_AUTOMATIC_RECOVERY_REQUESTS+ ) {+ this.attemptAutomaticRecovery();+ } else {+ this.reportRecoveryExhausted();+ }}return;}@@ -475,7 +482,10 @@ export class ViewerPeer implements ViewerMediaPeer {this.reportRecoveryExhausted();return;}- const rebuild = this.automaticRecoveryRequests > 0;+ const rebuild =+ this.recoveryOwner === "route"+ ? false+ : this.automaticRecoveryRequests > 0;if (!this.events.sendRestartRequest(this.parentPeerId,diff --git a/tests/viewer-peer-generation.test.ts b/tests/viewer-peer-generation.test.tsindex c1d3ed6c..50d3f6d3 100644--- a/tests/viewer-peer-generation.test.ts+++ b/tests/viewer-peer-generation.test.ts@@ -471,7 +471,7 @@ describe("ViewerPeer connection generations", () => {expect(timeoutDelays.get(2)).toBe(3_000);});- it("leaves a pending route candidate under the route deadline", async () => {+ it("gives a NAT-enabled pending route one bounded ICE retry", async () => {const restartRequests: string[] = [];const exhausted: string[] = [];const peer = new ViewerPeer(@@ -489,7 +489,7 @@ describe("ViewerPeer connection generations", () => {return true;},},- { recoveryOwner: "route" },+ { recoveryOwner: "route", natPredictionEnabled: true },);await peer.acceptSignal("host", offer("route-candidate"));@@ -503,7 +503,14 @@ describe("ViewerPeer connection generations", () => {connection.connectionState = "failed";connection.dispatchEvent(new Event("connectionstatechange"));- expect(restartRequests).toEqual([]);+ expect(restartRequests).toEqual(["route-candidate"]);+ expect(exhausted).toEqual([]);++ timeoutCallbacks.get(1)!();+ expect(restartRequests).toEqual(["route-candidate", "route-candidate"]);+ expect(exhausted).toEqual([]);++ timeoutCallbacks.get(2)!();expect(exhausted).toEqual(["route-candidate"]);});